95 lines
4.9 KiB
Python
95 lines
4.9 KiB
Python
"""Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`."""
|
|
import dataclasses
|
|
|
|
from model_routing.ladder import climbed, floored, highest, LADDER, picked, position, project_rung, Rung, stepped_up
|
|
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 applies it.
|
|
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 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 and at or above item_rung."""
|
|
return floor(anchor, route) == route.rung
|
|
|
|
|
|
def floor(anchor: Rung, route: Route) -> Rung:
|
|
"""The route's rung, raised to item_rung where it is lower or off the ladder; kept and stepped both hold a route to it."""
|
|
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: from taken_in's rung, one rung up when counted_out or escalates, else there.
|
|
|
|
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
|
|
# Every UNFINISHED_PER_STEP unfinished verdicts.
|
|
counted_out = unfinished >= UNFINISHED_PER_STEP
|
|
# The judge found the escalation trigger the worker cited in asking for a higher rung, and escalation_cap leaves room.
|
|
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:
|
|
"""floor, 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(floor(anchor, route), picked(anchor, bundled)) if bundled else floor(anchor, route)
|
|
|
|
|
|
def judge_rung(judge_model: str, route: Route) -> Rung:
|
|
"""The rung the route's item picks from the judge model's cheapest rung, never below that rung, whatever rung the worker stepped to.
|
|
|
|
An item the worker takes in moves the worker's rung but not the judge's, until todo.md item 136 routes the chunk on the item the worker takes.
|
|
|
|
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 floored(anchor, route.risk_and_complexity) if route.item else anchor
|