Files
claude-config/bin/loop_claude/loop/routing.py
T

51 lines
2.1 KiB
Python

"""Which ladder rung a chunk's worker runs on: docs/decisions.md → `routing:`."""
import dataclasses
from model_routing.ladder import climbed, index_of, LADDER, picked, Rung
from failure import Failure
from todo_table import Item, items
UNFINISHED_PER_STEP = 2
@dataclasses.dataclass(frozen=True)
class Route:
"""A chunk's worker rung, the plan item whose R + C picked it (0 for none), and the unfinished verdicts since it last stepped up."""
rung: Rung
item: int = 0
risk_and_complexity: int = 0
unfinished: int = 0
def __str__(self) -> str:
return f"{self.rung}, routed by item {self.item}'s R + C of {self.risk_and_complexity}" if self.item else str(self.rung)
def routed_item(todo: str) -> Item | None:
"""The next release's first item that waits on no question, as rank_todo.py ranks the table; None when there is none, or it has no C."""
found = items(todo)
if not found or isinstance(found, Failure):
return 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: int, item: Item | None) -> Route:
"""The route a chunk starts on: the rung its item's R + C picks from anchor, the project's rung, never below it."""
if item is None:
return Route(LADDER[anchor])
return Route(LADDER[max(anchor, picked(anchor, item.risk_and_complexity))], item.id, item.risk_and_complexity)
def stepped(anchor: int, route: Route, bundled: int) -> Route:
"""The route after one more unfinished verdict; bundled is the highest R + C the judge saw the worker take into the chunk, 0 for none.
A rung an edit took off the ladder counts as the project's rung.
"""
rung = index_of(route.rung)
rung = max(anchor if rung is None else rung, picked(anchor, bundled) if bundled else anchor)
unfinished = route.unfinished + 1
if unfinished >= UNFINISHED_PER_STEP:
return dataclasses.replace(route, rung=LADDER[climbed(rung, 1)], unfinished=0)
return dataclasses.replace(route, rung=LADDER[rung], unfinished=unfinished)