"""Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`.""" import dataclasses from chunk_pick import chunk_item, LastCut, Next from model_routing.ladder import climbed, highest, LADDER, position, project_rung, review_floor, Rung, stepped_up, worker_rung from failure import Failure from todo_table import Item, items 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, cut: LastCut) -> Item | Failure | None: """The item chunk_pick.chunk_item picks from todo.md, weighed against the last cut; None when there is none, or it lacks L, E or I.""" found = items(todo) if isinstance(found, Failure): return found pick = chunk_item(found or [], cut) takes = pick.item if isinstance(pick, Next) else None return takes if takes and takes.likelihood and takes.escape_and_impact else None def first_route(anchor: Rung, item: Item | None) -> Route: """The route a chunk starts on: the rung its item's L picks from anchor, the project's rung.""" if item is None: return Route(anchor) return Route(worker_rung(anchor, item.likelihood), item.id, item.likelihood, item.escape_and_impact) 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 L 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 worker_rung(anchor, route.likelihood) 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. Its rung never falls below taken_in's. It steps up one rung on every second unfinished verdict, or on an escalation the judge found while the rung is below escalation_cap; where both fall on one verdict, it steps once. 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 if unfinished >= UNFINISHED_PER_STEP or (escalated and below_cap(anchor, route, rung)): return stepped_from(route, rung) return dataclasses.replace(route, rung=rung, unfinished=unfinished) def stepped_by_ruling(anchor: Rung, route: Route, bundled: int) -> Route: """The route after the judge's interrupt or quoted answer carried an escalation: taken_in's rung, one rung up while that is below escalation_cap. Neither is an unfinished verdict, so the count of those restarts only with a step.""" rung = taken_in(anchor, route, bundled) return stepped_from(route, rung) if below_cap(anchor, route, rung) else dataclasses.replace(route, rung=rung) def stepped_from(route: Route, rung: Rung) -> Route: """The route one rung above rung, with the count of unfinished verdicts restarted.""" return dataclasses.replace(route, rung=stepped_up(rung), unfinished=0) def below_cap(anchor: Rung, route: Route, rung: Rung) -> bool: """Whether an escalation trigger may take the worker one rung up from rung without the human's yes: rung is below escalation_cap, two rungs above item_rung.""" return position(rung) < position(escalation_cap(anchor, route)) def taken_in(anchor: Rung, route: Route, bundled: int) -> Rung: """kept_rung, raised to the rung bundled picks from anchor; bundled is the highest L the judge saw the worker take into the chunk, 0 for none.""" return highest(kept_rung(anchor, route), worker_rung(anchor, bundled)) if bundled else kept_rung(anchor, route) def judge_rung(judge_model: str, route: Route) -> Rung: """The rung the route's reviewed_on E + I picks for a review, counted from the judge model's cheapest rung and never below it, whatever rung the worker stepped to. 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 review_floor(anchor, route.reviewed_on())