120 lines
5.2 KiB
Python
120 lines
5.2 KiB
Python
"""The routing ladder, and the rungs a task's L and E + I pick 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<model>\S+) at (?P<effort>\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. It is persisted, so changing it routes every stored chunk afresh: 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"),
|
|
)
|
|
# The rungs a task's L steps its worker from the project's rung.
|
|
WORKER_STEPS = {1: -2, 2: -1, 3: 0, 4: 1}
|
|
# The rungs a task's E + I steps a review up from the project's rung; an E + I under 5 steps none.
|
|
REVIEW_STEPS = {5: 1, 6: 2, 7: 3, 8: 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 worker_rung(project: Rung, likelihood: int) -> Rung:
|
|
"""The rung a task's L picks for its worker, counted from the project's rung, stopping at either end of the ladder."""
|
|
return climbed(project, WORKER_STEPS[likelihood])
|
|
|
|
|
|
def lowest_worker_rung(project: Rung) -> Rung:
|
|
"""The lowest rung any L routes a worker to, counted from the project's rung."""
|
|
return climbed(project, min(WORKER_STEPS.values()))
|
|
|
|
|
|
def planning_rung(project: Rung, likelihood: int) -> Rung:
|
|
"""The rung of a task that files or scores todo.md items: the one its L picks, never below the project's rung."""
|
|
return highest(project, worker_rung(project, likelihood))
|
|
|
|
|
|
def review_floor(project: Rung, escape_and_impact: int) -> Rung:
|
|
"""The rung a task's E + I picks for a review, counted up from the project's rung. An E + I of 0, for no task, picks the project's rung."""
|
|
return climbed(project, REVIEW_STEPS.get(escape_and_impact, 0))
|
|
|
|
|
|
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_rungs(project: Rung, worker: Rung, escape_and_impact: int) -> dict[ReviewKind, Rung]:
|
|
"""The rung each kind of review in a chunk runs on, where its worker runs on worker and its item has that E + I. The reviewers run on the higher of the worker's rung and review_floor's."""
|
|
reviewers = highest(worker, review_floor(project, escape_and_impact))
|
|
return {kind: review_rung(kind, project, reviewers) for kind in typing.get_args(ReviewKind)}
|
|
|
|
|
|
def review_rung(kind: ReviewKind, project: Rung, reviewers: Rung) -> Rung:
|
|
"""The rung a review of kind runs on: a reviewer on the reviewers' rung; a seat or the security pass on the project's, so the seats score every chunk on one rung, and the security pass spends no more than the project chose."""
|
|
return reviewers 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
|