Score risk as L + E + I, and route a worker on its item's L and its reviewers on its E + I

This commit is contained in:
2026-10-16 07:53:48 +02:00
parent d1b0fe3a44
commit 152972d84b
34 changed files with 699 additions and 514 deletions
+6 -6
View File
@@ -8,11 +8,11 @@ from claude_code.reply import claude_reply, Limited
from loop_claude.effects import Effects
from failure import Failure
from loop_claude.project.records import HUMAN_RULE_NAMES
from todo_table import LEVELS
from loop_claude.cycle.retry import retried
from model_routing.ladder import Rung
from loop_claude.stop import Stop
from claude_code.entries import structured_output
from todo_table import TOP_SCORE
# At 3 to 4 characters a token, 100,000 to 133,000 tokens, which leaves a 200,000-token context room for the reply; the largest session rendered here was 364,000 characters on 2026-10-03.
JUDGE_PROMPT_CHARS = 400000
@@ -54,7 +54,7 @@ JUDGE_RULES = (
class Reply:
answer: str = dataclasses.field(metadata={"description": "With interrupt false, the passage of README.md or the records that answers the agent's question, quoted verbatim, without elisions. Empty otherwise."})
# Routes the next cycle's worker. Counting the item the chunk was routed on is harmless: loop-claude never steps the worker down.
bundled: int = dataclasses.field(metadata={"description": "The highest R + C the agent states for a todo.md item it took into this chunk; 0 when it states none."})
bundled: int = dataclasses.field(metadata={"description": "The highest L the agent states for a todo.md item it took into this chunk; 0 when it states none."})
# Routes the next cycle's worker one rung up: docs/decisions.md → `routing: a worker whose run ends asking for a higher rung`.
escalation: str = dataclasses.field(metadata={"description": "Empty unless the chunk is unfinished and the agent's run ends asking loop-claude for a higher rung, citing an escalation trigger. Then: a verbatim quote, without elisions, of an earlier transcript passage showing the trigger fired after any earlier such request, such as a review round's commit title or the agent's report of the finding or failing test. Never the request itself."})
evidence: str = dataclasses.field(metadata={"description": "A verbatim quote of the one transcript passage the verdict rests on, without elisions: what triggers the interrupt; for a quoted answer, the agent's question; else what shows the chunk finished or the work remaining."})
@@ -65,7 +65,7 @@ class Reply:
REPLY_TYPES = typing.get_type_hints(Reply)
JSON_TYPES = {bool: "boolean", int: "integer", str: "string"}
HIGHEST_R_PLUS_C = 2 * TOP_SCORE
HIGHEST_L = LEVELS["L"]
JUDGE_SCHEMA = {
"properties": {field.name: {**field.metadata, "type": JSON_TYPES[REPLY_TYPES[field.name]]} for field in dataclasses.fields(Reply)},
"required": list(REPLY_TYPES),
@@ -95,7 +95,7 @@ class Finished:
@dataclasses.dataclass(frozen=True)
class Unfinished:
evidence: str
# The bundled R + C the judge saw, 0 for none.
# The bundled L the judge saw, 0 for none.
bundled: int = 0
# The judge's quote showing that the worker's cited escalation trigger fired; empty for none.
escalation: str = ""
@@ -121,8 +121,8 @@ def verdict_from(output: object) -> Verdict | Failure:
return QuotedAnswer(output["evidence"], output["question"], output["answer"])
if output["finished"]:
return Finished(output["evidence"])
if not 0 <= output["bundled"] <= HIGHEST_R_PLUS_C:
return Failure(f"the judge's bundled R + C {output['bundled']} is not from 0 to {HIGHEST_R_PLUS_C}")
if not 0 <= output["bundled"] <= HIGHEST_L:
return Failure(f"the judge's bundled L {output['bundled']} is not from 0 to {HIGHEST_L}")
return Unfinished(output["evidence"], output["bundled"], output["escalation"])
+22 -13
View File
@@ -1,4 +1,4 @@
from model_routing.ladder import climbed, LADDER, review_rung, Rung
from model_routing.ladder import climbed, LADDER, position, review_rung, reviewers_rung, ReviewKind, Rung
from model_routing.route import ESCALATION_MARKER, Route
from loop_claude.routing import escalation_rungs
from loop_claude.project.records import HUMAN_RULE_NAMES, RECORD_FILES
@@ -23,18 +23,26 @@ def fresh(task: str, premises: str, route: Route, anchor: Rung | None) -> str:
def routed_by(route: Route, anchor: Rung | None) -> str:
return f"loop-claude runs you on {route.rung}, routed by item {route.item}'s R + C of {route.risk_and_complexity}. Name each todo.md item this chunk takes, with its R + C, whether or not it is item {route.item}.{off_the_projects_rung(route, anchor)}{escalation(route, anchor)}{reruns(route, anchor)}"
return f"loop-claude runs you on {route.rung}, routed by item {route.item}'s L of {route.likelihood}. Name each todo.md item this chunk takes, with its L and E + I, whether or not it is item {route.item}.{review_launches(route, anchor)}{escalation(route, anchor)}{reruns(route, anchor)}"
def off_the_projects_rung(route: Route, anchor: Rung | None) -> str:
"""Which subagents to launch on the project's rung, anchor, and below it which on the --plan rung; empty while the worker runs on anchor, as every chunk no item routed does until it steps up."""
if anchor is None or route.rung == anchor:
def review_launches(route: Route, anchor: Rung | None) -> str:
"""Which reviews to launch on a rung other than the worker's, and on which; below the project's rung, anchor, also where to launch a subagent that plans. Empty off the ladder, and where every review runs on the worker's rung."""
if anchor is None:
return ""
# Below the project's rung, the reviewers stay on it to hold quality; above it, the seats score every chunk on one rung, and the security pass spends no more than the project chose.
below = review_rung("reviewer", anchor, route.rung) == anchor
launched = "each reviewer, each comprehension seat and the security pass's subagent" if below else "each comprehension seat and the security pass's subagent"
planning = f" Launch each subagent that files or scores todo.md items on the rung `~/.claude/bin/subagent-rung --plan {anchor.model} <R+C>` prints." if below else ""
return f" Your rung is {'below' if below else 'above'} the project's, {anchor}: launch {launched} with the Agent tool set to {anchor.agent_args()}. Run a full scan only from such a subagent, since the scan's agents run on the model that starts them.{planning}"
reviewers = reviewers_rung(anchor, route.rung, route.escape_and_impact)
# The seats score every chunk on one rung, and the security pass spends no more than the project chose. Where the reviewers' rung is anchor too, anchor's name wins.
named = {reviewers: f"the rung item {route.item}'s E + I of {route.escape_and_impact} picks", anchor: "the project's rung"}
launched: dict[Rung, list[str]] = {}
kinds: tuple[tuple[str, ReviewKind], ...] = (("each reviewer", "reviewer"), ("each comprehension seat and the security pass's subagent", "seat"))
for reviews, kind in kinds:
rung = review_rung(kind, anchor, reviewers)
if rung != route.rung:
launched.setdefault(rung, []).append(reviews)
launches = "".join(f" Launch {', '.join(reviews)} with the Agent tool set to {rung.agent_args()}, {named[rung]}." for rung, reviews 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 anchor != 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}"
def escalation(route: Route, anchor: Rung | None) -> str:
@@ -55,8 +63,9 @@ def reruns(route: Route, anchor: Rung | None) -> str:
"""The prompt's sentence naming the rung one above a reviewer's and a seat's, on which each reruns when it asks; empty off the ladder, and leaving out one already on the top rung."""
if anchor is None or route.rung not in LADDER:
return ""
# The rungs off_the_projects_rung launches them on: the seats always on anchor, the reviewers on anchor only while the worker runs below it.
launched = (("reviewer", review_rung("reviewer", anchor, route.rung)), ("comprehension seat", review_rung("seat", anchor, route.rung)))
# The rungs review_launches launches them on.
reviewers = reviewers_rung(anchor, route.rung, route.escape_and_impact)
launched = (("reviewer", review_rung("reviewer", anchor, reviewers)), ("comprehension seat", review_rung("seat", anchor, reviewers)))
rerun = {name: climbed(rung, 1) for name, rung in launched if rung != LADDER[-1]}
if not rerun:
return ""
@@ -69,7 +78,7 @@ def resumed(unfinished_evidence: str, route: Route, anchor: Rung | None) -> str:
"""Names the worker's rung, so it learns whether its request for a higher rung went through, and the project's again, since a step-up between cycles may have taken the worker above it."""
on = f" loop-claude runs you on {route.rung}." if anchor else ""
ruled = f" A judge ruled the chunk unfinished, quoting this passage of your transcript as the work remaining:\n\n{quoted(unfinished_evidence)}\n\n" if unfinished_evidence else " "
return f"Continue the task; the previous run ended before the chunk was finished.{ruled}The chunk is finished once it is merged, or nothing remains that you could do on your own. Even then, it is finished only once {ANSWERS_RECORDED}.{on}{off_the_projects_rung(route, anchor)}{escalation(route, anchor)}{reruns(route, anchor)}"
return f"Continue the task; the previous run ended before the chunk was finished.{ruled}The chunk is finished once it is merged, or nothing remains that you could do on your own. Even then, it is finished only once {ANSWERS_RECORDED}.{on}{review_launches(route, anchor)}{escalation(route, anchor)}{reruns(route, anchor)}"
def answered(question: str, answer: str) -> str:
+1 -1
View File
@@ -93,7 +93,7 @@ def work(cycle: Cycle, answered: QuotedAnswer | None) -> WorkerRan | Stop:
def worker_environment(cycle: Cycle) -> dict[str, str]:
"""The rungs the worker's claude is handed, which rule-hook holds its review launches to."""
return worker_rungs.environment(cycle.anchor, cycle.route.rung)
return worker_rungs.environment(cycle.anchor, cycle.route)
def worker_start(cycle: Cycle, answered: QuotedAnswer | None) -> list[str]: