"""Which ladder rung a chunk's worker runs on: docs/decisions.md → `routing:`.""" import dataclasses from failure import Failure from model_routing.ladder import highest, LADDER, picked, Rung, stepped_up from todo_table import Item, items from model_routing.route import Route UNFINISHED_PER_STEP = 2 def routed_item(todo: str) -> Item | Failure | None: """The next release's first row that waits on no question, in the table's order, which rank_todo.py keeps ranked; None when there is none, or it has no C.""" found = items(todo) if isinstance(found, Failure) or not found: return found if isinstance(found, Failure) else 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 takes.risk_and_complexity else None def first_route(anchor: Rung, item: Item | None) -> Route: """The route a chunk starts on: the rung its item's R + C picks from anchor, the project's rung.""" if item is None: return Route(anchor) return Route(picked(anchor, item.risk_and_complexity), item.id, item.risk_and_complexity) def kept(route: Route, anchor: Rung) -> bool: """Whether a chunk keeps route: its rung is on the ladder at or above the rung its item's R + C picks from anchor, the project's rung, or anchor itself when no item routed it.""" return highest(route.rung, picked(anchor, route.risk_and_complexity) if route.item else anchor) == route.rung def stepped(anchor: Rung, route: Route, bundled: int) -> Route: """The route after one more unfinished verdict; bundled is the highest R + C the judge saw the worker take into the chunk, 0 for none.""" # An archived rung since removed from LADDER restarts from the project's rung. rung = route.rung if route.rung in LADDER else anchor rung = highest(rung, picked(anchor, bundled)) if bundled else rung if route.unfinished + 1 >= UNFINISHED_PER_STEP: return dataclasses.replace(route, rung=stepped_up(rung), unfinished=0) return dataclasses.replace(route, rung=rung, unfinished=route.unfinished + 1)