"""Which ladder rung a chunk's worker runs on: docs/decisions.md → `routing:`.""" from failure import Failure from model_routing.ladder import climbed, steps from todo_table import Item, items UNFINISHED_PER_STEP = 2 def routed_item(todo: str) -> Item | None: """The next release's first item that waits on no question, as rank_todo.py ranks the table; None when there is none, or it has no C.""" found = items(todo) if not found or isinstance(found, Failure): return None next_release = min(item.release for item in found) takes = next((item for item in found if item.release == next_release and item.exempt != "question"), None) return takes if takes and "C" in takes.scores else None def risk_and_complexity(item: Item) -> int: return item.scores["R"] + item.scores["C"] def first_rung(anchor: int, item: Item | None) -> int: """The rung a chunk starts on: the one its item's R + C picks from anchor, the project's rung, never below it.""" return anchor if item is None else max(anchor, climbed(anchor, steps(risk_and_complexity(item)))) def stepped(anchor: int, rung: int, unfinished: int, bundled: int) -> tuple[int, int]: """The rung and the unfinished verdicts since it last stepped, after one more unfinished verdict. bundled is the highest R + C the judge saw the worker say an item it took into the chunk scores, 0 for none. """ if bundled: rung = max(rung, climbed(anchor, steps(bundled))) unfinished += 1 return (climbed(rung, 1), 0) if unfinished == UNFINISHED_PER_STEP else (rung, unfinished)