Read the routing ladder from routing-ladder.json, which a project replaces with a file of its own in its config's folder
CI / tests (pull_request) Successful in 2m37s

This commit is contained in:
2026-11-15 18:51:59 +01:00
parent 9d0e2ad853
commit 0a20b6d4f6
42 changed files with 848 additions and 453 deletions
+2 -2
View File
@@ -81,9 +81,9 @@ def main(argv: list[str], stdin: str) -> int:
def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tuple[str, str, list[Breach]] | None:
"""The text that may carry an override, what the log names the call by, and its breaches; None for a call no rule checks."""
if call.tool == "Agent":
return text(call.given, "prompt"), text(call.given, "subagent_type"), reviewer_breaches(call, environ)
return text(call.given, "prompt"), text(call.given, "subagent_type"), reviewer_breaches(call, environ, home)
if call.tool == "Skill":
return text(call.given, "args"), text(call.given, "skill"), skill_breaches(call, environ)
return text(call.given, "args"), text(call.given, "skill"), skill_breaches(call, environ, home)
if call.tool == "Bash":
command = text(call.given, "command")
return command, command.split("\n", 1)[0], bash_breaches(command, call.cwd, home) + read_breaches(call, home)
+33 -16
View File
@@ -1,4 +1,5 @@
"""Refuse an Agent or Skill 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:`."""
"""Refuse an Agent or Skill call that launches a review below the rung it needs, worked out on the project's ladder 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
@@ -6,8 +7,9 @@ from rule_hook.call import Call, text
from failure import Failure
from git_cli import git_repo
from rule_hook.git import guarded_git
from model_routing.worker_rungs import handed
from model_routing.ladder import position, review_rung, ReviewKind, Rung
from model_routing.worker_rungs import handed, hands_rungs
from model_routing.ladder import Ladder, ReviewKind, Rung
from model_routing.ladder_file import project_ladder
from review_rules.review_passes import prose, REVIEWERS
from claude_code.transcript import read_transcript
from claude_code.entries import UserText
@@ -23,29 +25,44 @@ SUBJECTS: dict[ReviewKind, str] = {"seat": "a comprehension seat", "security": "
SECURITY_SKILLS = {"claude-security", "security-review"}
def reviewer_breaches(call: Call, environ: typing.Mapping[str, str]) -> list[Breach]:
def reviewer_breaches(call: Call, environ: typing.Mapping[str, str], home: 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:
if kind is None or not hands_rungs(environ):
return []
required = review_rung(project, worker, escape_and_impact, kind, markdown_only(call.cwd))
named = named_rung(call.given, worker)
if position(named) >= position(required):
ladder = project_ladder(call.cwd, pathlib.Path(home))
if isinstance(ladder, Failure):
return [unread(ladder, call.cwd)]
project, worker, escape_and_impact = handed(environ, ladder)
if project is None or worker is None:
return []
required = ladder.review_rung(project, worker, escape_and_impact, kind, markdown_only(call.cwd))
named = named_rung(call.given, worker, ladder)
if ladder.position(named) >= ladder.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 skill_breaches(call: Call, environ: typing.Mapping[str, str]) -> list[Breach]:
def unread(ladder: Failure, cwd: str) -> Breach:
"""The breach of a review launched where loop-claude handed rungs and the project's ladder cannot be read, so nothing tells which rung the review needs."""
return Breach("reviewer-rung", f"the rung this review needs cannot be worked out: {ladder.reason}", cwd)
def skill_breaches(call: Call, environ: typing.Mapping[str, str], home: str) -> list[Breach]:
"""The breach of a Skill call that runs the security pass below the project's rung; none where loop-claude handed no rungs.
The skill runs on its caller's rung. A subagent launched as the security pass was held to the project's rung by reviewer_breaches. The worker's own rung is known, so its call is refused only below the project's rung. Any other subagent's rung is unknown, so its call is refused."""
project, worker, _ = handed(environ)
skill = text(call.given, "skill").lstrip("/").rsplit(":", 1)[-1]
if skill not in SECURITY_SKILLS or project is None or worker is None or opening_prompt(call.transcript).startswith(SECURITY_OPENER):
if skill not in SECURITY_SKILLS or not hands_rungs(environ) or opening_prompt(call.transcript).startswith(SECURITY_OPENER):
return []
if not call.subagent and position(worker) >= position(project):
ladder = project_ladder(call.cwd, pathlib.Path(home))
if isinstance(ladder, Failure):
return [unread(ladder, call.cwd)]
project, worker, _ = handed(environ, ladder)
if project is None or worker is None:
return []
if not call.subagent and ladder.position(worker) >= ladder.position(project):
return []
runs_on = "on a rung rule-hook cannot see" if call.subagent else f"in this context on {worker}, below {project}"
return [Breach("reviewer-rung", f"{skill} would run {runs_on}: invoke it from a subagent launched with {project.agent_args()}, its prompt opening with `{SECURITY_OPENER}`", call.cwd)]
@@ -70,12 +87,12 @@ def review_kind(given: dict[str, typing.Any]) -> ReviewKind | None:
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."""
def named_rung(given: dict[str, typing.Any], worker: Rung, ladder: Ladder) -> 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 on ladder."""
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)
return ladder.from_agent(model, effort)
def markdown_only(directory: str) -> bool:
+54 -10
View File
@@ -216,6 +216,10 @@ class OverrideLog(unittest.TestCase):
self.assertEqual(this_chunks(LogLine("t", branch, commit, "image-pin", "r", "w"), {"base", "c1"}, "chunk", on_default), expected)
# A home that holds no project config, so a review launched there is checked on the ladder this setup ships.
HOME = "/h"
class ReviewerRung(unittest.TestCase):
# A worker routed to Sonnet 5.5 at high, on a project whose rung is Opus 5.5 at medium, with its reviewers on the project's rung.
ENVIRON = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-sonnet-5-5 at high effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "4"}
@@ -223,7 +227,7 @@ class ReviewerRung(unittest.TestCase):
ABOVE = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-fable-5-2 at max effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "4"}
def refused(self, given: dict, environ: dict[str, str] = ENVIRON) -> list[str]:
return rules(reviewer_breaches(Call("Agent", given, "/r"), environ))
return rules(reviewer_breaches(Call("Agent", given, "/r"), environ, HOME))
def test_a_branch_changing_only_markdown_reviews_on_the_projects_rung_whatever_the_e_plus_i(self):
repo = tempfile.mkdtemp()
@@ -238,24 +242,24 @@ class ReviewerRung(unittest.TestCase):
git(repo, "commit", "-q", "-m", "prose")
high = self.ENVIRON | {"LOOP_CLAUDE_ESCAPE_AND_IMPACT": "7"}
on_project = {"subagent_type": "stability-reviewer", "model": "opus", "effort": "medium"}
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high)), [])
self.assertEqual(rules(reviewer_breaches(Call("Agent", {**on_project, "model": "sonnet", "effort": "high"}, repo), high)), ["reviewer-rung"])
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), self.ABOVE)), [], "a worker that climbed doesn't lift a Markdown-only review either")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high, HOME)), [])
self.assertEqual(rules(reviewer_breaches(Call("Agent", {**on_project, "model": "sonnet", "effort": "high"}, repo), high, HOME)), ["reviewer-rung"])
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), self.ABOVE, HOME)), [], "a worker that climbed doesn't lift a Markdown-only review either")
for instructions in ("CLAUDE.md", "AGENTS.md", "skills/review/SKILL.md", "agents/reviewer.md", "rules/todo.md", "principles/tests.md", "workflow-bench/run-prompt.md"):
with self.subTest(instructions=instructions):
path = pathlib.Path(repo, instructions)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("Do this.\n")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high)), ["reviewer-rung"], "Markdown an agent follows is config")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high, HOME)), ["reviewer-rung"], "Markdown an agent follows is config")
path.unlink()
pathlib.Path(repo, "gate.yml").write_text("on: push\n")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high)), ["reviewer-rung"], "an uncommitted change to another file counts")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high, HOME)), ["reviewer-rung"], "an uncommitted change to another file counts")
git(repo, "add", ".")
git(repo, "commit", "-q", "-m", "config")
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high)), ["reviewer-rung"])
self.assertEqual(rules(reviewer_breaches(Call("Agent", on_project, repo), high, HOME)), ["reviewer-rung"])
def test_a_reviewer_below_the_project_rung_is_refused_naming_the_arguments_to_pass(self):
breaches = reviewer_breaches(Call("Agent", {"subagent_type": "stability-reviewer", "model": "sonnet", "effort": "high", "prompt": "Review."}, "/r"), self.ENVIRON)
breaches = reviewer_breaches(Call("Agent", {"subagent_type": "stability-reviewer", "model": "sonnet", "effort": "high", "prompt": "Review."}, "/r"), self.ENVIRON, HOME)
self.assertEqual(rules(breaches), ["reviewer-rung"])
self.assertIn("stability-reviewer", breaches[0].detail)
self.assertIn("claude-sonnet-5-5 at high effort", breaches[0].detail)
@@ -312,13 +316,53 @@ class ReviewerRung(unittest.TestCase):
def test_a_claude_loop_claude_did_not_start_is_not_checked(self):
self.assertEqual(self.refused({"subagent_type": "stability-reviewer", "model": "haiku", "effort": "low"}, {}), [])
def project(self, ladder: str) -> tuple[str, str]:
"""A checkout whose config's folder holds ladder as its routing ladder, and its home."""
home = pathlib.Path(tempfile.mkdtemp()).resolve()
self.addCleanup(subprocess.run, ["rm", "-rf", str(home)])
checkout = home / "code" / "acme"
checkout.mkdir(parents=True)
(configured(self, checkout, home=home) / "routing-ladder.json").write_text(ladder)
return str(checkout), str(home)
# A project that runs on Fable 5.2 at low and at high alone, with a worker on its rung and an E + I of 5, which picks the rung one up.
TWO_RUNGS = '[{"model": "claude-fable-5-2", "effort": "low"}, {"model": "claude-fable-5-2", "effort": "high"}]'
ON_TWO_RUNGS = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-fable-5-2 at low effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-fable-5-2 at low effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "5"}
def test_a_review_is_held_to_the_rung_picked_on_the_projects_own_ladder(self):
checkout, home = self.project(self.TWO_RUNGS)
reviewer = {"subagent_type": "stability-reviewer", "model": "fable"}
self.assertEqual(rules(reviewer_breaches(Call("Agent", {**reviewer, "effort": "high"}, checkout), self.ON_TWO_RUNGS, home)), [])
# One up from Fable 5.2 at low is its medium effort on the shipped ladder, and its high effort on the project's, which holds no medium.
breaches = reviewer_breaches(Call("Agent", {**reviewer, "effort": "medium"}, checkout), self.ON_TWO_RUNGS, home)
self.assertEqual([breach.detail for breach in breaches], ["stability-reviewer would run on claude-fable-5-2 at medium effort, below claude-fable-5-2 at high effort: launch it with model fable, effort high"])
self.assertEqual(rules(reviewer_breaches(Call("Agent", {**reviewer, "effort": "medium"}, "/r"), self.ON_TWO_RUNGS, HOME)), [])
def test_a_security_skill_is_held_to_the_projects_rung_on_the_projects_own_ladder(self):
checkout, home = self.project('[{"model": "claude-fable-5-2", "effort": "high"}, {"model": "claude-fable-5-2", "effort": "low"}]')
# On this ladder the worker's low effort ranks above the project's high one; on the shipped ladder it ranks below.
environ = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-fable-5-2 at high effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-fable-5-2 at low effort"}
self.assertEqual(skill_breaches(Call("Skill", {"skill": "security-review"}, checkout), environ, home), [])
self.assertEqual(rules(skill_breaches(Call("Skill", {"skill": "security-review"}, "/r"), environ, HOME)), ["reviewer-rung"])
def test_a_review_is_refused_where_the_projects_ladder_cannot_be_read_and_loop_claude_handed_rungs(self):
checkout, home = self.project("[]")
own = pathlib.Path(home, ".claude-projects", "acme", "routing-ladder.json")
reviewer = Call("Agent", {"subagent_type": "stability-reviewer", "model": "fable", "effort": "max"}, checkout)
for breaches in (reviewer_breaches(reviewer, self.ON_TWO_RUNGS, home), skill_breaches(Call("Skill", {"skill": "security-review"}, checkout), self.ON_TWO_RUNGS, home)):
self.assertEqual(rules(breaches), ["reviewer-rung"])
self.assertTrue(breaches[0].detail.startswith(f"the rung this review needs cannot be worked out: {own}: it holds no list of rungs. "), breaches[0].detail)
self.assertEqual(reviewer_breaches(reviewer, {}, home), [], "a claude loop-claude did not start is not checked")
self.assertEqual(reviewer_breaches(Call("Agent", {"subagent_type": "Explore"}, checkout), self.ON_TWO_RUNGS, home), [], "nor is a subagent that reviews nothing")
self.assertEqual(skill_breaches(Call("Skill", {"skill": "simplify"}, checkout), self.ON_TWO_RUNGS, home), [])
def skill_refused(self, given: dict, environ: dict[str, str] = ENVIRON, subagent_prompt: str | list | None = None) -> list[Breach]:
transcript = ""
if subagent_prompt is not None:
transcript = os.path.join(tempfile.mkdtemp(), "agent-a1.jsonl")
self.addCleanup(subprocess.run, ["rm", "-rf", os.path.dirname(transcript)])
pathlib.Path(transcript).write_text(json.dumps({"type": "user", "message": {"role": "user", "content": subagent_prompt}}) + "\n")
return skill_breaches(Call("Skill", given, "/r", "general-purpose" if subagent_prompt is not None else "", transcript, subagent_prompt is not None), environ)
return skill_breaches(Call("Skill", given, "/r", "general-purpose" if subagent_prompt is not None else "", transcript, subagent_prompt is not None), environ, HOME)
def test_a_security_skill_from_the_sessions_own_agent_is_refused_below_the_projects_rung(self):
for skill in ("/security-review", "claude-security", "claude-security:claude-security", "security-review"):
@@ -338,7 +382,7 @@ class ReviewerRung(unittest.TestCase):
with self.subTest(prompt=prompt):
self.assertEqual(rules(self.skill_refused({"skill": "security-review"}, self.ENVIRON, prompt)), [])
self.assertEqual(rules(self.skill_refused({"skill": "security-review"}, self.ABOVE, "Invoke /security-review.")), ["reviewer-rung"], "a subagent's rung is unknown, whatever the worker's")
unreadable = skill_breaches(Call("Skill", {"skill": "security-review"}, "/r", "general-purpose", "/nonexistent/agent-a1.jsonl", True), self.ENVIRON)
unreadable = skill_breaches(Call("Skill", {"skill": "security-review"}, "/r", "general-purpose", "/nonexistent/agent-a1.jsonl", True), self.ENVIRON, HOME)
self.assertEqual(rules(unreadable), ["reviewer-rung"])
def test_any_other_skill_or_a_claude_loop_claude_did_not_start_is_not_checked(self):