Route each chunk's worker by its item's R + C on one ladder of model and effort rungs

This commit is contained in:
2026-10-14 22:48:31 +02:00
parent 224c52e3cc
commit e2e7a0688f
17 changed files with 256 additions and 85 deletions
+48 -6
View File
@@ -40,6 +40,16 @@ Every claude loop-claude starts runs the project's model, saved per <key> in
run, is asked for one. LOOP_CLAUDE_JUDGE_MODEL overrides it for the judge, LOOP_CLAUDE_WORKER_MODEL
for the worker and the interrupt; with both set, nothing is asked.
Where that model is on the routing ladder (bin/model_routing/ladder.py), the worker and the
interrupt run on a rung of it instead: a model at an effort. As a chunk starts, the R + C of
todo.md's top item in the next release, skipping a question, picks the rung from the model's
cheapest one, and never below it; without such an item, or with a C missing, the chunk runs on the
model's cheapest rung. The worker's first prompt names the rung and asks it to say when the chunk
takes in an item of higher R + C. The judge reports the highest such R + C it sees the worker say,
and the next cycle steps up to the rung that picks, never down. Every second unfinished verdict on
a chunk steps it up one rung. A rerun routes an open chunk afresh. The archive records each
verdict's rung.
A change to bin/loop-claude or any bin/**/*.py takes effect at the next cycle boundary, once
loop-claude --help succeeds: the loop reloads into the edited script.
@@ -60,8 +70,9 @@ Exit 1, saying why, when:
- the worker or the judge fails three times running, retried a minute apart within the cycle. A
worker fails when claude cannot start, exits non-zero, is killed, or ends without a result event;
a judge when claude cannot start or exits non-zero, or its reply misses or mistypes a field,
interrupts or quotes an answer without a question, or quotes a passage the transcript does not
hold; it holds an elided passage when each fragment appears in order.
interrupts or quotes an answer without a question, gives a bundled R + C outside 0 to 20, or
quotes a passage the transcript does not hold; it holds an elided passage when each fragment
appears in order.
A usage limit is not a failure, and restarts the count of three: the worker or the judge is
retried 2 minutes after the limit resets, or every 10 minutes when the reset time is unknown or
more than 2 minutes past. The judge's reply gives its reset time only as text, so the judge is
@@ -83,15 +94,18 @@ from loop_claude.project.checkout import checkout_root
from loop_claude.cycle.run import Cycle, run_cycle
from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG
from loop_claude.effects import Effects
from loop_claude.cycle.judge import Finished, Unfinished
from loop_claude.cycle.routing import first_rung, routed_item, stepped
from loop_claude.project.model_choice import get_or_ask_models
from model_routing.ladder import LADDER, project_rung, Rung
from loop_claude.paths import Paths
from transcript_archive.location import project_archive
from project_key import project_key
from loop_claude.cycle import prompts
from loop_claude.project.premises import read_premises
from loop_claude.project.records import read_records
from claude_code.session import session_named
from loop_claude.stop import Stop
from loop_claude.cycle.judge import Unfinished
def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> int:
@@ -126,11 +140,39 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
models = get_or_ask_models(key, paths, env, effects)
if isinstance(models, Stop):
return models
print(f"\n=== cycle {loop_run.cycle_number} ===", flush=True)
verdict = run_cycle(Cycle(effects=effects, models=models, paths=paths, premises=premises, project_archive=archive, records=records, root=loop_run.root, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence))
anchor = project_rung(models.worker)
loop_run, worker, routed = route_worker(loop_run, anchor, models.worker)
print(f"\n=== cycle {loop_run.cycle_number}, worker on {worker} ===", flush=True)
verdict = run_cycle(Cycle(effects=effects, judge_model=models.judge, paths=paths, premises=premises, project_archive=archive, records=records, root=loop_run.root, routed=routed, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence, worker=worker))
if isinstance(verdict, Stop):
return verdict
loop_run = dataclasses.replace(loop_run, unfinished_evidence=verdict.evidence if isinstance(verdict, Unfinished) else "")
loop_run = after_verdict(loop_run, anchor, verdict)
def route_worker(loop_run: LoopRun, anchor: int | None, project_model: str) -> tuple[LoopRun, Rung, str]:
"""The loop run, the worker's rung, and what the fresh prompt says of its route; a chunk is routed once, as it starts."""
if anchor is None:
return loop_run, Rung(project_model), ""
if loop_run.rung >= 0:
return loop_run, LADDER[loop_run.rung], ""
item = routed_item(read_todo(loop_run.root))
loop_run = dataclasses.replace(loop_run, rung=first_rung(anchor, item), unfinished=0)
return loop_run, LADDER[loop_run.rung], prompts.routed(LADDER[loop_run.rung], item) if item else ""
def after_verdict(loop_run: LoopRun, anchor: int | None, verdict: Finished | Unfinished) -> LoopRun:
if isinstance(verdict, Finished):
return dataclasses.replace(loop_run, rung=-1, unfinished=0, unfinished_evidence="")
rung, unfinished = stepped(anchor, loop_run.rung, loop_run.unfinished, verdict.bundled) if anchor is not None else (loop_run.rung, 0)
return dataclasses.replace(loop_run, rung=rung, unfinished=unfinished, unfinished_evidence=verdict.evidence)
def read_todo(root: str) -> str:
"""The checkout's todo.md, empty where it cannot be read: a chunk without a plan item runs on the project's rung."""
try:
return pathlib.Path(root, "todo.md").read_text(encoding="utf-8")
except (OSError, ValueError):
return ""
def loop_run_from(argv: list[str], cwd: pathlib.Path, effects: Effects) -> LoopRun | Stop:
+3
View File
@@ -20,6 +20,9 @@ class LoopRun:
root: str
task: str
unfinished_evidence: str
# The chunk's worker rung on the ladder, -1 until the chunk is routed; and the unfinished verdicts since it last stepped up.
rung: int = -1
unfinished: int = 0
def decode_handed(handed: str, kind: type[T]) -> T | None: