125 lines
7.2 KiB
Python
125 lines
7.2 KiB
Python
"""Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`.
|
|
|
|
The worker, and the session /talk opens, run on a Route (model_routing/route.py). A model off the ladder gets a route without an item, and it is never stepped.
|
|
|
|
- Start. routed_item finds the plan item that routes a fresh chunk, if there is one with L, E and I scored; first_route picks the worker's rung from its L, or the project's rung without an item.
|
|
- Rerun. kept decides whether a rerun resumes the route its archive last names.
|
|
- Step-up. next_route raises the route after an unfinished verdict, or an interrupt or a quoted answer that carries an escalation; escalation_cap bounds an escalation, and loop_claude/escalation_mark.py states the escalation mark's rule. step_up names the change where it changes the worker line, which the archive records.
|
|
- Judge. judge_rung picks the rung of the judge of a JUDGE_REPLAYED model.
|
|
"""
|
|
import dataclasses
|
|
|
|
from release_plan.release_standing import chunk_item, LastCut, Next
|
|
from model_routing.ladder import climbed, highest, LADDER, position, project_rung, review_floor, Rung, stepped_up, worker_rung
|
|
from loop_claude import escalation_mark
|
|
from failure import Failure
|
|
from loop_claude.verdicts import Interrupt, QuotedAnswer, Unfinished
|
|
from release_plan.todo_table import Item, items, NoItemsTable
|
|
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 release_standing.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, NoItemsTable):
|
|
return None
|
|
if isinstance(found, Failure):
|
|
return found
|
|
pick = chunk_item(found, 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(project: Rung, item: Item | None) -> Route:
|
|
"""The route a chunk starts on: the rung its item's L picks from the project's rung, or the project's rung without an item."""
|
|
if item is None:
|
|
return Route(project)
|
|
return Route(worker_rung(project, item.likelihood), item.id, item.likelihood, item.escape_and_impact)
|
|
|
|
|
|
def kept(route: Route, project: Rung) -> bool:
|
|
"""Whether a chunk keeps route: its rung is on the ladder and at or above item_rung."""
|
|
return floor_rung(project, route) == route.rung
|
|
|
|
|
|
def item_rung(project: Rung, route: Route) -> Rung:
|
|
"""The rung the route's item's L picks from the project's rung, or the project's rung itself when no item routed it. The worker may run on a rung below the project's."""
|
|
return worker_rung(project, route.likelihood) if route.item else project
|
|
|
|
|
|
def floor_rung(project: Rung, route: Route, likelihood_taken_in: int = 0) -> Rung:
|
|
"""The lowest rung the worker may run on: the route's own, raised to item_rung where it is lower or off the ladder, and to the rung likelihood_taken_in picks from project. likelihood_taken_in is the highest L the worker took into the chunk, 0 for none."""
|
|
floor = highest(route.rung, item_rung(project, route))
|
|
return highest(floor, worker_rung(project, likelihood_taken_in)) if likelihood_taken_in else floor
|
|
|
|
|
|
def escalation_rungs(project: 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(project, route)) + 1]
|
|
|
|
|
|
def escalation_cap(project: 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(project, route), ESCALATION_CAP)
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class StepUp:
|
|
"""A verdict changing the route's worker line: the route the worker ran on until the verdict, and the one it runs on after. The archive names both, so the figures charge each stretch of work to its own rung."""
|
|
before: Route
|
|
after: Route
|
|
|
|
|
|
def next_route(project: Rung | None, route: Route, verdict: Unfinished | Interrupt | QuotedAnswer) -> Route:
|
|
"""The route the worker runs on after the judge's verdict. Off the ladder, project is None, and the route holds.
|
|
|
|
The rung never falls. It first rises to floor_rung, which the verdict's highest L taken in may raise. From there it steps one rung up on an escalation while below escalation_cap, and moves the mark, as loop_claude/escalation_mark.py says.
|
|
An unfinished verdict also counts towards UNFINISHED_PER_STEP, and the second such verdict steps the rung once, keeping the mark; where both fall on one verdict, it steps once. Last, it takes in the verdict's highest E + I, as Route.taking_in does.
|
|
An interrupt or a quoted answer without an escalation leaves the route as it is, since neither is an unfinished verdict.
|
|
"""
|
|
if project is None:
|
|
return route
|
|
mark = escalation_mark.moved_to(verdict)
|
|
floor = floor_rung(project, route, verdict.taken_in.highest_likelihood)
|
|
escalated_at = route.escalated_at if mark is None else mark
|
|
escalates = mark is not None and position(floor) < position(escalation_cap(project, route))
|
|
if isinstance(verdict, Unfinished):
|
|
counted = route.unfinished + 1
|
|
after = one_rung_up(route, floor, escalated_at) if counted >= UNFINISHED_PER_STEP or escalates else dataclasses.replace(route, rung=floor, unfinished=counted)
|
|
return after.taking_in(verdict.taken_in.highest_escape_and_impact)
|
|
if mark is None:
|
|
return route
|
|
return one_rung_up(route, floor, escalated_at) if escalates else dataclasses.replace(route, rung=floor)
|
|
|
|
|
|
def step_up(before: Route, after: Route) -> StepUp | None:
|
|
"""The step-up from before to after; None where the worker line holds, as when only the count of unfinished verdicts changed."""
|
|
return StepUp(before, after) if after.worker_line() != before.worker_line() else None
|
|
|
|
|
|
def one_rung_up(route: Route, rung: Rung, escalated_at: int) -> Route:
|
|
"""The route one rung above rung, with the count of unfinished verdicts restarted and the mark at escalated_at."""
|
|
return dataclasses.replace(route, rung=stepped_up(rung), unfinished=0, escalated_at=escalated_at)
|
|
|
|
|
|
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.
|
|
"""
|
|
cheapest = project_rung(judge_model)
|
|
if cheapest is None or judge_model not in JUDGE_REPLAYED:
|
|
return Rung(judge_model)
|
|
return review_floor(cheapest, route.reviewed_on())
|