"""Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`.""" import dataclasses from model_routing.ladder import climbed, highest, LADDER, picked, position, project_rung, Rung, stepped_up from failure import Failure from todo_table import Item, items, waiting from model_routing.route import Route UNFINISHED_PER_STEP = 2 # How many rungs above item_rung an escalation trigger takes the worker or a task without the human's yes, as escalation_cap counts them. ESCALATION_CAP = 2 # The models `judge-replay` found returning, on their cheapest rung, every verdict the reference judge reproduced: docs/decisions.md → `routing: on a model judge-replay covered`. JUDGE_REPLAYED = ("claude-opus-5-5",) def routed_item(todo: str) -> Item | Failure | None: """The next release's first row that does not wait, as todo_table.waiting says, in the table's order; 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) waits = waiting(found) takes = next((item for item in found if item.release == next_release and item.id not in waits), 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 and at or above item_rung.""" return kept_rung(anchor, route) == route.rung def kept_rung(anchor: Rung, route: Route) -> Rung: """The route's rung, raised to item_rung where it is lower or off the ladder.""" return highest(route.rung, item_rung(anchor, route)) def item_rung(anchor: Rung, route: Route) -> Rung: """The rung the route's item's R + C picks from anchor, the project's rung, or anchor itself when no item routed it. The worker may run on a rung below anchor.""" return picked(anchor, route.risk_and_complexity) if route.item else anchor def escalation_rungs(anchor: Rung, route: Route) -> tuple[Rung, ...]: """The rungs an escalation trigger opens to the worker, least competent first: from one above its own up to escalation_cap; none off the ladder.""" if route.rung not in LADDER: return () return LADDER[position(route.rung) + 1:position(escalation_cap(anchor, route)) + 1] def escalation_cap(anchor: Rung, route: Route) -> Rung: """The highest rung an escalation trigger takes the worker or a task to without the human's yes: docs/decisions.md → `routing: a worker hands a task one rung up`. It counts from item_rung, the rung the chunk started on, so a step-up between cycles narrows what the worker may climb without a yes, and never widens it. """ return climbed(item_rung(anchor, route), ESCALATION_CAP) def stepped(anchor: Rung, route: Route, bundled: int, escalated: bool) -> Route: """The route after one more unfinished verdict, never on a lower rung. bundled and escalated are the verdict's fields, not the verdict, since loop_claude.cycle.judge imports this module through prompts. """ rung = taken_in(anchor, route, bundled) unfinished = route.unfinished + 1 counted_out = unfinished >= UNFINISHED_PER_STEP # The judge found the escalation trigger the worker cited in asking for a higher rung. escalates = escalated and position(rung) < position(escalation_cap(anchor, route)) if counted_out or escalates: return dataclasses.replace(route, rung=stepped_up(rung), unfinished=0) return dataclasses.replace(route, rung=rung, unfinished=unfinished) def taken_in(anchor: Rung, route: Route, bundled: int) -> Rung: """kept_rung, raised to the rung bundled picks from anchor; bundled is the highest R + C the judge saw the worker take into the chunk, 0 for none.""" return highest(kept_rung(anchor, route), picked(anchor, bundled)) if bundled else kept_rung(anchor, route) def judge_rung(judge_model: str, route: Route) -> Rung: """The rung the route's item picks, counted from the judge model's cheapest rung and never below it, whatever rung the worker stepped to. An item the worker takes in beyond the routed one lifts the worker's rung, through taken_in, but not the judge's, until todo.md item 153 lifts the judge too. A model off the ladder, or not in JUDGE_REPLAYED, gets no effort flag, so claude runs it at the effort its settings give. """ anchor = project_rung(judge_model) if anchor is None or judge_model not in JUDGE_REPLAYED: return Rung(judge_model) return highest(anchor, item_rung(anchor, route))