Files
claude-config/bin/rule_hook/reviewer_rung.py
T

55 lines
2.7 KiB
Python

"""Hold each Agent call that launches a review to the rung loop-claude hands its worker: docs/decisions.md → `routing:`."""
import re
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, LADDER, position, Rung
# larv-review §3's reviewers, by the subagent type its passes launch.
REVIEWERS = frozenset(("override-reviewer", "principles-auditor", "product-owner-reviewer", "prose-reviewer", "stability-reviewer", "systems-architect"))
# 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 security pass's subagent invokes one of these skills.
SECURITY = re.compile(r"/security-review\b|/claude-security\b")
SUBJECTS = {"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 = handed(environ)
kind = review_kind(call.given)
if kind is None or project is None or worker is None:
return []
# A reviewer runs on the higher of the worker's rung and the project's; a seat or the security pass on the project's.
required = highest(project, worker) if kind == "reviewer" else project
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]) -> str | 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"
prompt = text(given, "prompt")
if SEAT in prompt:
return "seat"
if SECURITY.search(prompt):
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)
family = [rung for rung in LADDER if rung.family() == model]
if not family:
return Rung(model, effort)
return Rung(family[0].model, effort) if effort else family[0]