"""Refuse an Agent call that launches a review below the rung it needs, worked out from the rungs loop-claude hands its worker: docs/decisions.md → `routing:`.""" import pathlib import typing from rule_hook.rules import Breach from rule_hook.call import Call, text from model_routing.worker_rungs import handed from model_routing.ladder import highest, position, review_rung, ReviewKind, Rung # A reviewer is an agent under agents/, as CLAUDE.md → "Vocabulary" defines it. REVIEWERS = frozenset(path.stem for path in (pathlib.Path(__file__).resolve().parents[2] / "agents").glob("*.md")) # The opening of every comprehension seat's prompt: skills/comprehension-panel/seat-prompts.md → "Opening". SEAT = "You are one seat of a comprehension panel." # The opening of the security pass's prompt: skills/larv-review/SKILL.md → "Security". SECURITY_OPENER = "You are the security pass for this chunk." SUBJECTS: dict[ReviewKind, str] = {"seat": "a comprehension seat", "security": "the security pass's subagent"} def reviewer_breaches(call: Call, environ: typing.Mapping[str, str]) -> list[Breach]: """The breach of an Agent call that launches a review below its rung; none where loop-claude handed no rungs.""" project, worker, reviewers = handed(environ) kind = review_kind(call.given) if kind is None or project is None or worker is None: return [] # A worker an older loop-claude started was handed no reviewers' rung; its reviewers are held to the rung that loop-claude gave them. required = review_rung(kind, project, reviewers or highest(project, worker)) named = named_rung(call.given, worker) if position(named) >= position(required): return [] subject = text(call.given, "subagent_type") if kind == "reviewer" else SUBJECTS[kind] return [Breach("reviewer-rung", f"{subject} would run on {named}, below {required}: launch it with {required.agent_args()}", call.cwd)] def review_kind(given: dict[str, typing.Any]) -> ReviewKind | None: """What a launch reviews, a reviewer, a comprehension seat or the security pass; None for any other subagent.""" if text(given, "subagent_type") in REVIEWERS: return "reviewer" opening = text(given, "prompt").lstrip() if opening.startswith(SEAT): return "seat" if opening.startswith(SECURITY_OPENER): return "security" return None def named_rung(given: dict[str, typing.Any], worker: Rung) -> Rung: """The rung an Agent call's model and effort name. A call naming no model runs on the worker's model, at the effort it names or else the worker's; one naming a model but no effort runs on that model's cheapest rung.""" model, effort = text(given, "model"), text(given, "effort") if not model: return Rung(worker.model, effort or worker.effort) return Rung.from_agent(model, effort)