Generalise stability-replay to review-replay of any pass, and route each pass on its own rung so a replay may lower one pass alone

This commit is contained in:
2026-10-18 17:04:33 +02:00
parent 2911d3f4cc
commit fc13a1100e
26 changed files with 279 additions and 189 deletions
+33 -16
View File
@@ -1,4 +1,4 @@
from model_routing.ladder import climbed, LADDER, position, review_rungs, ReviewKind, Rung
from model_routing.ladder import climbed, LADDER, position, review_rungs, REVIEWERS, ReviewKind, Rung
from model_routing.route import ESCALATION_MARKER, HIGHEST_ESCAPE_AND_IMPACT, Route
from loop_claude.routing import escalation_rungs
from loop_claude.project.records import HUMAN_RULE_NAMES, RECORD_FILES
@@ -10,8 +10,8 @@ QUOTE_SOURCES = listed(("README.md's Goals and Audience", *RECORD_FILES, *HUMAN_
# Where the worker may write an answer the human typed, so it counts as recorded.
ANSWER_DESTINATIONS = "README.md's Goals or Audience, " + ", ".join(RECORD_FILES) + ", todo.md or a comment"
ANSWERS_RECORDED = f"every answer the human typed in it that a later chunk could need is recorded in {ANSWER_DESTINATIONS} at the one place it governs"
# Each kind of review, as review_launches names those it launches.
REVIEWS: tuple[tuple[str, ReviewKind], ...] = (("each reviewer", "reviewer"), ("each comprehension seat", "seat"), ("the security pass's subagent", "security"))
# Each kind of review, as review_launches names those it launches: a row per pass, since a replay may lower one pass alone.
REVIEWS: tuple[tuple[str, ReviewKind], ...] = (*((f"each {agent}", kind) for kind, agent in REVIEWERS.items()), ("each comprehension seat", "seat"), ("the security pass's subagent", "security"))
def fresh(task: str, premises: str, route: Route, anchor: Rung | None, facts: str) -> str:
@@ -48,11 +48,11 @@ def review_launches(route: Route, anchor: Rung | None) -> str:
if anchor is None:
return ""
rungs = review_rungs(anchor, route.rung, route.reviewed_on())
launched: dict[Rung, list[str]] = {}
for reviews, kind in REVIEWS:
launched: dict[Rung, list[ReviewKind]] = {}
for _, kind in REVIEWS:
if rungs[kind] != route.rung:
launched.setdefault(rungs[kind], []).append(reviews)
launches = "".join(f" Launch {listed(reviews, 'and')} with the Agent tool set to {rung.agent_args()}, {rung_named(rung, route, anchor)}." for rung, reviews in launched.items())
launched.setdefault(rungs[kind], []).append(kind)
launches = "".join(f" Launch {listed(named(kinds), 'and')} with the Agent tool set to {rung.agent_args()}, {rung_named(rung, route, anchor)}." for rung, kinds in launched.items())
scan = " Run a full scan only from such a subagent, since the scan's agents run on the model that starts them." if rungs["security"] != route.rung else ""
planning = f" Your rung is below the project's: launch each subagent that files or scores todo.md items on the rung `~/.claude/bin/subagent-rung --plan {anchor.model} <L>` prints." if position(route.rung) < position(anchor) else ""
return f"{launches}{scan}{planning}{taken_in_launches(route, anchor)}"
@@ -63,11 +63,12 @@ def taken_in_launches(route: Route, anchor: Rung) -> str:
For a worker on Opus 5.5 at medium, routed by an item of E + I 6, it reads: ` Where an item you take into the chunk has an E + I of 7 or more, launch each reviewer with the Agent tool set to `model opus, effort max` from 7 and `model fable, effort max` from 8.`
"""
reviewers = review_rungs(anchor, route.rung, route.reviewed_on())["reviewer"]
lowest = min(position(review_rungs(anchor, route.rung, route.reviewed_on())[kind]) for kind in REVIEWERS)
higher: dict[Rung, int] = {}
for escape_and_impact in range(route.reviewed_on() + 1, HIGHEST_ESCAPE_AND_IMPACT + 1):
rung = review_rungs(anchor, route.rung, escape_and_impact)["reviewer"]
if position(rung) > position(reviewers):
# An E + I that steps reviews up puts every pass on one rung; one that does not leaves each pass where it is.
rungs = {review_rungs(anchor, route.rung, escape_and_impact)[kind] for kind in REVIEWERS}
if len(rungs) == 1 and position(rung := rungs.pop()) > lowest:
higher.setdefault(rung, escape_and_impact)
if not higher:
return ""
@@ -75,10 +76,18 @@ def taken_in_launches(route: Route, anchor: Rung) -> str:
return f" Where an item you take into the chunk has an E + I of {min(higher.values())} or more, launch each reviewer with the Agent tool set to {listed(picks, 'and')}."
def named(kinds: list[ReviewKind]) -> list[str]:
"""How review_launches names kinds: by their rows in REVIEWS, with "each reviewer" for every pass where all are among them."""
every = all(kind in kinds for kind in REVIEWERS)
return [*(["each reviewer"] if every else []), *(name for name, kind in REVIEWS if kind in kinds and not (every and kind in REVIEWERS))]
def rung_named(rung: Rung, route: Route, anchor: Rung) -> str:
"""Why a review runs on rung: it is the project's, anchor, or the one the route's reviewed_on E + I picks, the item's or one taken in."""
"""Why a review runs on rung: it is the project's, anchor, the one a pass's replay chose, which alone lies below anchor, or the one the route's reviewed_on E + I picks, the item's or one taken in."""
if rung == anchor:
return "the project's rung"
if position(rung) < position(anchor):
return "the rung the replay of its pass chose"
if route.escape_and_impact_taken_in:
return f"the rung picked by an item of E + I {route.escape_and_impact_taken_in} taken into the chunk"
return f"the rung item {route.item}'s E + I of {route.escape_and_impact} picks"
@@ -112,13 +121,21 @@ def reruns(route: Route, anchor: Rung | None) -> str:
if anchor is None or route.rung not in LADDER:
return ""
rungs = review_rungs(anchor, route.rung, route.reviewed_on())
launched = (("reviewer", rungs["reviewer"]), ("comprehension seat", rungs["seat"]))
rerun = {name: climbed(rung, 1) for name, rung in launched if rung != LADDER[-1]}
rerun: dict[Rung, list[ReviewKind]] = {}
for _, kind in REVIEWS:
if kind != "security" and rungs[kind] != LADDER[-1]:
rerun.setdefault(climbed(rungs[kind], 1), []).append(kind)
if not rerun:
return ""
if len(set(rerun.values())) == 1:
return f" A {listed(list(rerun), 'or')} that asks for a rerun one rung up reruns with the Agent tool set to `{next(iter(rerun.values())).agent_args()}`."
return f" A reviewer that asks for a rerun one rung up reruns with the Agent tool set to `{rerun['reviewer'].agent_args()}`, and a comprehension seat with `{rerun['comprehension seat'].agent_args()}`."
(first, kinds), *others = rerun.items()
withs = [f"a {listed(nouns(more), 'or')} with `{rung.agent_args()}`" for rung, more in others]
rest = f", {'and ' if len(withs) == 1 else ''}{listed(withs, 'and')}" if withs else ""
return f" A {listed(nouns(kinds), 'or')} that asks for a rerun one rung up reruns with the Agent tool set to `{first.agent_args()}`{rest}."
def nouns(kinds: list[ReviewKind]) -> list[str]:
"""named's names without their "each"."""
return [name.removeprefix("each ") for name in named(kinds)]
def resumed(unfinished_evidence: str, route: Route, anchor: Rung | None) -> str:
+7 -6
View File
@@ -68,19 +68,20 @@ picks the worker's rung, stepping from the model's cheapest rung by WORKER_STEPS
bin/model_routing/ladder.py, below it for a low L; without such an item, or with its L, E or I
unscored, the chunk runs on the model's cheapest rung. The
item's E + I picks the reviewers' rung, stepping up from the cheapest rung by REVIEW_STEPS there,
never below the worker's rung. The worker's first prompt names the rung and the item, and asks the
never below the worker's rung. Where it steps none, a pass that a review-replay run lowered runs on
the rung REPLAYED there names, even below the worker's. The worker's first prompt names the rung and the item, and asks the
worker to name each item the chunk takes, with its L and E + I, and to count the item's size again
once it has read the code (prompts.recount). The prompt names the rung of each review that runs on
a rung other than the worker's: the reviewers' rung for the reviewers, and the cheapest rung for
each comprehension seat and the security pass. It also names the reviewers' rung
a rung other than the worker's: the reviewers' rung or the replay's for each pass's reviewer, and
the cheapest rung for each comprehension seat and the security pass. It also names the reviewers' rung
each higher E + I picks, so the worker reviews an item it takes in on that rung in the same run.
When the worker runs below the cheapest rung, the prompt has it launch each subagent that files or
scores todo.md items on the rung subagent-rung --plan prints. The prompt after
each unfinished verdict says this again, since a step-up may have moved the worker. A reviewer or
seat that rules the work below standard may ask to rerun one rung up; each prompt names that rung.
loop-claude hands the worker's claude its own rung, the project's and the reviewers', in
LOOP_CLAUDE_WORKER_RUNG, LOOP_CLAUDE_PROJECT_RUNG and LOOP_CLAUDE_REVIEWER_RUNG, and rule-hook
refuses a review launched below the rung named for it. The judge reports the highest L and the
loop-claude hands the worker's claude its own rung, the project's and the reviews' E + I, in
LOOP_CLAUDE_WORKER_RUNG, LOOP_CLAUDE_PROJECT_RUNG and LOOP_CLAUDE_ESCAPE_AND_IMPACT, and rule-hook
refuses a review launched below the rung those pick for it. The judge reports the highest L and the
highest E + I the worker states for an item it takes in. The next cycle steps the worker up to the
rung that L picks, never down, and the reviewers to the rung picked by the higher of two: that
E + I, or the routed item's (Route.reviewed_on in bin/model_routing/route.py). A level off its
+15 -2
View File
@@ -12,6 +12,7 @@ import sys
import tempfile
import time
import unittest
import unittest.mock
import uuid
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -24,6 +25,7 @@ from claude_code import entries, reply, session
from failure import Failure
from loop_claude.cycle.judge import Finished, Interrupt, QuotedAnswer, Unfinished, verdict_from
from loop_claude.cycle import interrupt, judge, prompts, retry, run, states, worker
from model_routing import ladder
from model_routing.ladder import LADDER, Rung
from claude_code.reply import Limited
from loop_claude.loop import main, reload, routing_check
@@ -220,11 +222,11 @@ class Cycles(unittest.TestCase):
self.assertIn("- A preflight fact.", fresh)
self.assertNotIn("- A preflight fact.", resumed)
def test_a_worker_is_handed_the_projects_rung_its_own_and_its_reviewers_in_its_environment(self):
def test_a_worker_is_handed_the_projects_rung_its_own_and_its_reviews_e_plus_i_in_its_environment(self):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
cycle = dataclasses.replace(self.cycle(fake), anchor=Rung("claude-opus-5-5", "medium"), route=Route(Rung("claude-haiku-5-5", "max"), 163, 1, 5))
self.assertEqual(run.run_cycle(cycle).verdict, Finished("working"))
self.assertEqual(fake.worker_environments, [{"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort", "LOOP_CLAUDE_REVIEWER_RUNG": "claude-opus-5-5 at high effort"}])
self.assertEqual(fake.worker_environments, [{"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "5"}])
def test_the_claude_an_interrupt_resumes_is_handed_the_same_rungs_as_the_worker(self):
def answer(args):
@@ -1143,6 +1145,17 @@ class WorkerArgs(unittest.TestCase):
self.assertIn(f"`Escalation trigger <n>:`, n the trigger's number.{offer}", prompts.resumed("", Route(Rung("claude-opus-5-5", "high"), 3, 3, 4), opus))
self.assertNotIn("Where the hard part", prompts.resumed("", Route(Rung("claude-opus-5-5", "xhigh"), 3, 3, 4), opus))
def test_launches_each_pass_a_replay_lowered_on_its_replays_rung_and_reruns_it_one_above(self):
opus, haiku = Rung("claude-opus-5-5", "medium"), Rung("claude-haiku-5-5", "low")
with unittest.mock.patch.dict(ladder.REPLAYED, {("prose", "claude-opus-5-5"): haiku}):
below = prompts.fresh("Do it.", "## Goals", Route(Rung("claude-sonnet-5-5", "high"), 3, 2, 4), opus, self.FACTS)
on_it = prompts.resumed("", Route(haiku, 3, 1, 4), opus)
self.assertIn("whether or not it is item 3. Launch each systems-architect, each principles-auditor, each override-reviewer, each stability-reviewer, each product-owner-reviewer, each comprehension seat and the security pass's subagent with the Agent tool set to model opus, effort medium, the project's rung. Launch each prose-reviewer with the Agent tool set to model haiku, effort low, the rung the replay of its pass chose.", below)
self.assertIn(" Where an item you take into the chunk has an E + I of 5 or more, launch each reviewer with the Agent tool set to `model opus, effort high` from 5,", below)
self.assertIn(" A systems-architect, principles-auditor, override-reviewer, stability-reviewer, product-owner-reviewer or comprehension seat that asks for a rerun one rung up reruns with the Agent tool set to `model opus, effort high`, and a prose-reviewer with `model haiku, effort medium`.", below)
self.assertNotIn("each prose-reviewer", on_it)
self.assertIn(" Where an item you take into the chunk has an E + I of 5 or more,", on_it)
def test_names_the_rung_a_reviewer_or_seat_reruns_on_one_above_its_own(self):
opus = Rung("claude-opus-5-5", "medium")
both = " A reviewer or comprehension seat that asks for a rerun one rung up reruns with the Agent tool set to `model opus, effort high`."