"""The routing ladder, and the rung an R + C picks from the project's rung: docs/decisions.md → `routing:`.""" import dataclasses import re import typing # How Rung.__str__ spells a rung that names an effort, and Rung.parse reads it back. WORDING = re.compile(r"(?P\S+) at (?P\S+) effort") # What a review launch reviews: a reviewer, a comprehension seat or the security pass. ReviewKind = typing.Literal["reviewer", "seat", "security"] @dataclasses.dataclass(frozen=True) class Rung: model: str # Empty leaves the effort to claude's settings, as for a model off the ladder. effort: str = "" def flags(self) -> list[str]: return ["--model", self.model, *(["--effort", self.effort] if self.effort else [])] def family(self) -> str: """The Agent tool's name for the model, such as opus for claude-opus-5-5.""" return self.model.split("-")[1] def agent_args(self) -> str: """The rung in the Agent tool's words. The tool takes a family name, such as opus, and runs that family's latest model, which is the model each rung names.""" return f"model {self.family()}, effort {self.effort}" @staticmethod def from_agent(model: str, effort: str) -> "Rung": """The rung the Agent tool's model and effort name: a family such as opus is its ladder model, at the effort given or else its cheapest. A family off the ladder, or an effort off it, gives a rung that is not on LADDER.""" family = next((rung for rung in LADDER if rung.family() == model), None) if family is None: return Rung(model, effort) return Rung(family.model, effort) if effort else family # The one spelling of a rung. The archive and the worker's environment store it, and parse reads it back. Changing it routes every archived chunk afresh, since parse no longer reads their rungs. def __str__(self) -> str: return f"{self.model} at {self.effort} effort" if self.effort else self.model @staticmethod def parse(text: str) -> "Rung | None": """The rung that names an effort, which str wrote, whether or not it is on the ladder; None for any other text, such as a bare model.""" found = WORDING.fullmatch(text) return Rung(found["model"], found["effort"]) if found else None # Least competent first. LADDER = ( Rung("claude-haiku-5-5", "low"), Rung("claude-haiku-5-5", "medium"), Rung("claude-haiku-5-5", "high"), Rung("claude-haiku-5-5", "xhigh"), Rung("claude-haiku-5-5", "max"), Rung("claude-sonnet-5-5", "high"), Rung("claude-opus-5-5", "medium"), Rung("claude-opus-5-5", "high"), Rung("claude-opus-5-5", "xhigh"), Rung("claude-opus-5-5", "max"), Rung("claude-fable-5-1", "max"), ) # (highest R + C in the band, rungs it steps from the project's rung); a higher R + C steps ABOVE_THE_BANDS. BANDS = ((4, -3), (6, -2), (8, -1), (10, 0), (12, 1), (14, 2), (16, 3)) ABOVE_THE_BANDS = 4 def project_rung(model: str) -> Rung | None: """The model's cheapest rung, which for Opus 5.5 is its default effort; None for a model off the ladder.""" return next((rung for rung in LADDER if rung.model == model), None) def picked(anchor: Rung, risk_and_complexity: int) -> Rung: """The rung a task's R + C picks from anchor, the project's rung, stopping at either end of the ladder.""" return climbed(anchor, next((step for highest, step in BANDS if risk_and_complexity <= highest), ABOVE_THE_BANDS)) def floored(anchor: Rung, risk_and_complexity: int) -> Rung: """The rung a task's R + C picks from anchor, the project's rung, never below anchor.""" return highest(anchor, picked(anchor, risk_and_complexity)) def stepped_up(rung: Rung) -> Rung: """The rung above, or the top one; a rung off the ladder steps to the bottom one.""" return climbed(rung, 1) def review_rung(kind: ReviewKind, project: Rung, worker: Rung) -> Rung: """The rung a review runs on: a reviewer on the higher of the worker's rung and the project's; a seat or the security pass on the project's.""" return highest(project, worker) if kind == "reviewer" else project def highest(*rungs: Rung) -> Rung: """The most competent of the rungs; one off the ladder ranks below every rung on it.""" return max(rungs, key=position) def climbed(rung: Rung, by: int) -> Rung: return LADDER[max(0, min(len(LADDER) - 1, position(rung) + by))] def position(rung: Rung) -> int: return LADDER.index(rung) if rung in LADDER else -1