"""Refuse an Agent call that launches a review below the rung it needs, worked out from the rungs and the E + I 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 failure import Failure from rule_hook.git import git import git_repo from model_routing.worker_rungs import handed from model_routing.ladder import position, review_rungs, ReviewKind, Rung from review_passes import REVIEWERS # The pass each agent under agents/ reviews. PASS_OF = {agent: kind for kind, agent in REVIEWERS.items()} # 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"} # Markdown an agent follows, which counts as config: Claude Code's memory files, and the folders that hold Claude Code's skills, agents and rules and this repo's principles. INSTRUCTION_FILES = {"CLAUDE.md", "AGENTS.md"} INSTRUCTION_FOLDERS = {"agents", "principles", "rules", "skills"} 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, escape_and_impact = handed(environ) kind = review_kind(call.given) if kind is None or project is None or worker is None: return [] required = review_rungs(project, worker, escape_and_impact)[kind] # A branch that changes only Markdown no agent follows holds no code or config for a reviewer on a higher rung to catch a defect in: skills/larv-review/SKILL.md → "Markdown only". if kind in REVIEWERS and position(required) > position(project) and markdown_only(call.cwd): required = project named = named_rung(call.given, worker) if position(named) >= position(required): return [] subject = SUBJECTS.get(kind) or text(call.given, "subagent_type") 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's pass, a comprehension seat or the security pass; None for any other subagent.""" if pass_reviewed := PASS_OF.get(text(given, "subagent_type")): return pass_reviewed 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) def markdown_only(directory: str) -> bool: """Whether every file the branch in directory changes, against the default branch as last fetched, is Markdown that no agent follows, counting uncommitted and untracked files; False where it changes none or git cannot say.""" default = git_repo.default_ref(directory, guarded=True) base = default if isinstance(default, Failure) else git(directory, "merge-base", default, "HEAD") changed = base if isinstance(base, Failure) else git(directory, "diff", "--name-only", base) untracked = git(directory, "ls-files", "--others", "--exclude-standard") if isinstance(changed, Failure) or isinstance(untracked, Failure): return False paths = [*changed.splitlines(), *untracked.splitlines()] return bool(paths) and all(prose(path) for path in paths) def prose(path: str) -> bool: """Whether path names Markdown that no agent follows.""" parts = pathlib.PurePosixPath(path).parts return path.endswith(".md") and parts[-1] not in INSTRUCTION_FILES and not INSTRUCTION_FOLDERS & set(parts[:-1])