123 lines
7.1 KiB
Python
123 lines
7.1 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. after_unfinished raises the route after an unfinished verdict, after_ruling after 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.
|
|
- Judge. judge_rung picks the rung of the judge of a JUDGE_REPLAYED model.
|
|
"""
|
|
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 loop_claude import escalation_mark
|
|
from failure import Failure
|
|
from loop_claude.verdicts import Interrupt, QuotedAnswer, Unfinished
|
|
from 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 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, 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 kept_rung(project, route) == route.rung
|
|
|
|
|
|
def kept_rung(project: 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(project, route))
|
|
|
|
|
|
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 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)
|
|
|
|
|
|
def after_unfinished(project: Rung, route: Route, verdict: Unfinished) -> Route:
|
|
"""The route the next cycle runs on after an unfinished verdict.
|
|
|
|
Its rung never falls. It first rises to the rung that the verdict's highest L taken in picks, where that is higher. From there it steps one rung up on every second unfinished verdict, or on an escalation while below escalation_cap; where both fall on one verdict, it steps once. A step on an escalation moves the mark, as loop_claude/escalation_mark.py says; a step on the count of unfinished verdicts alone keeps it. Last, the route takes in the verdict's highest E + I, as Route.taking_in does.
|
|
"""
|
|
rung = raised(project, route, verdict.highest_likelihood_taken_in)
|
|
mark = escalation_mark.moved_to(verdict)
|
|
if route.unfinished + 1 >= UNFINISHED_PER_STEP or (mark is not None and below_cap(project, route, rung)):
|
|
after = one_rung_up(route, rung, route.escalated_at if mark is None else mark)
|
|
else:
|
|
after = dataclasses.replace(route, rung=rung, unfinished=route.unfinished + 1)
|
|
return after.taking_in(verdict.highest_escape_and_impact_taken_in)
|
|
|
|
|
|
def after_ruling(project: Rung, route: Route, verdict: Interrupt | QuotedAnswer) -> Route:
|
|
"""The route after the judge's interrupt or quoted answer: unchanged without an escalation. With one, raised to the rung the highest L taken in picks, and one rung up from there while below escalation_cap, moving the mark as after_unfinished does. Neither is an unfinished verdict, so the count of those restarts only with a step."""
|
|
mark = escalation_mark.moved_to(verdict)
|
|
if mark is None:
|
|
return route
|
|
rung = raised(project, route, verdict.highest_likelihood_taken_in)
|
|
return one_rung_up(route, rung, mark) if below_cap(project, route, rung) else dataclasses.replace(route, rung=rung)
|
|
|
|
|
|
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 below_cap(project: 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(project, route))
|
|
|
|
|
|
def raised(project: Rung, route: Route, likelihood_taken_in: int) -> Rung:
|
|
"""kept_rung, raised to the rung that likelihood_taken_in picks from project. likelihood_taken_in is the highest L the worker took into the chunk, or 0 for none."""
|
|
return highest(kept_rung(project, route), worker_rung(project, likelihood_taken_in)) if likelihood_taken_in else kept_rung(project, 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.
|
|
"""
|
|
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())
|