"""A chunk's route: its worker's rung and the plan item that picked it, in the words the archive records each verdict's worker in.""" import dataclasses import re import typing from todo_table import LEVELS from model_routing.ladder import Rung # How a worker opens the prompt of a subagent it escalates a task to, `Escalation trigger 2: …`: docs/decisions.md → `routing: a worker hands a task one rung up`. ESCALATION_MARKER = "Escalation trigger" # An archived worker line: the rung, then the item and the levels that routed it, if any, then the highest E + I taken in, if any. route_from reads only LEVELS_ROUTED's levels, so a line an older loop-claude wrote, naming an R + C, routes its chunk afresh; archived_worker still reads its rung for the routing figures. ROUTED = re.compile(r"(?P.+?)(?:, routed by item (?P[0-9]+)'s (?P.+?))?(?:, taking in an E \+ I of (?P[0-9]+))?") LEVELS_ROUTED = re.compile(r"L of (?P[0-9]+) and E \+ I of (?P[0-9]+)") class ArchivedWorker(typing.NamedTuple): """What an archived worker line names: the worker's rung, the item that routed it, 0 for none, the text naming that item's levels, which an older loop-claude wrote as an R + C, and the highest E + I taken in, 0 for none.""" rung: Rung item: int levels: str taken_in: int = 0 def on_its_scale(escape_and_impact: int) -> bool: """Whether E + I is a level on the scales todo_table.LEVELS gives; E and I each start at 1.""" return 2 <= escape_and_impact <= LEVELS["E"] + LEVELS["I"] def on_their_scales(likelihood: int, escape_and_impact: int) -> bool: """Whether L and E + I are levels on the scales todo_table.LEVELS gives.""" return 1 <= likelihood <= LEVELS["L"] and on_its_scale(escape_and_impact) def levels_fit(item: int, likelihood: int, escape_and_impact: int, taken_in: int = 0) -> bool: """Whether a route may hold these: an item with levels on their scales, or no item and no levels; and no E + I taken in, or one on its scale.""" routed = on_their_scales(likelihood, escape_and_impact) if item else not (likelihood or escape_and_impact) return routed and (taken_in == 0 or on_its_scale(taken_in)) @dataclasses.dataclass(frozen=True) class Route: """A chunk's worker rung and the plan item that picked it, 0 for none. The item's L picked the worker's rung; reviewed_on, the higher of its E + I and the highest E + I the worker took in, picks the reviewers' and the judge's. A route holds only levels that levels_fit, so no rung is picked from a level off its scale. unfinished counts the unfinished verdicts towards the next one-rung step; a reload hands it over with the rung. """ # The fields a loop-claude from before L and E + I replaced R + C hands on reload; the reloaded script drops such a route, and the chunk routes afresh. RETIRED: typing.ClassVar[frozenset[str]] = frozenset({"risk_and_complexity"}) rung: Rung item: int = 0 likelihood: int = 0 escape_and_impact: int = 0 unfinished: int = 0 # The highest E + I the judge saw the worker take into the chunk, 0 for none. escape_and_impact_taken_in: int = 0 def __post_init__(self) -> None: # Raises on a programming error only, so nothing catches it: route_from and the reload decoder check levels_fit before they build a route. if not levels_fit(self.item, self.likelihood, self.escape_and_impact, self.escape_and_impact_taken_in): raise ValueError(f"a route by item {self.item} cannot hold an L of {self.likelihood}, an E + I of {self.escape_and_impact} and an E + I taken in of {self.escape_and_impact_taken_in}") @staticmethod def fits(fields: typing.Mapping[str, typing.Any]) -> bool: """Whether a route may be built from these fields, as the reload decoder asks of a handed one.""" return levels_fit(fields.get("item", 0), fields.get("likelihood", 0), fields.get("escape_and_impact", 0), fields.get("escape_and_impact_taken_in", 0)) def reviewed_on(self) -> int: """The E + I that picks the reviewers' and the judge's rung: the routed item's or the highest taken in, whichever is higher; 0 for neither.""" return max(self.escape_and_impact, self.escape_and_impact_taken_in) def __str__(self) -> str: """What the archive records as each verdict's worker; ROUTED reads it back.""" routed = f", routed by item {self.item}'s L of {self.likelihood} and E + I of {self.escape_and_impact}" if self.item else "" taken_in = f", taking in an E + I of {self.escape_and_impact_taken_in}" if self.escape_and_impact_taken_in else "" return f"{self.rung}{routed}{taken_in}" def archived_worker(worker: str) -> ArchivedWorker | None: """What an archived worker line names; None for a model run off the ladder.""" found = ROUTED.fullmatch(worker) rung = Rung.parse(found["rung"]) if found else None return ArchivedWorker(rung, int(found["item"] or 0), found["levels"] or "", int(found["taken_in"] or 0)) if found and rung else None def route_from(worker: str) -> Route | None: """The route an archived worker line names; None for a model run off the ladder, or a route by anything but levels on their scales.""" archived = archived_worker(worker) if archived is None: return None levels = LEVELS_ROUTED.fullmatch(archived.levels) if archived.item else None if archived.item and not levels: return None likelihood, escape_and_impact = (int(levels["likelihood"]), int(levels["escape_and_impact"])) if levels else (0, 0) if not levels_fit(archived.item, likelihood, escape_and_impact, archived.taken_in): return None return Route(archived.rung, archived.item, likelihood, escape_and_impact, escape_and_impact_taken_in=archived.taken_in)