Answer architecture round 1: 0 🔴, 2 🟡; rename Step to StepUp and let escalation.py own the judge's step-line sentence

This commit is contained in:
2026-10-23 04:54:45 +02:00
parent f0e14f3fdf
commit bf8b642ead
6 changed files with 62 additions and 59 deletions
+2 -2
View File
@@ -68,7 +68,7 @@ class Reply:
# Routes the next cycle's judge and reviewers, as highest_likelihood_taken_in routes the worker.
highest_escape_and_impact_taken_in: int = dataclasses.field(metadata={"description": "The highest E + I the worker states for a todo.md item it took into this chunk; 0 when it states none."})
# Routes the worker one rung up, as the judge interrupts or quotes an answer, or on the next cycle: loop_claude/escalation.py.
escalation: str = dataclasses.field(metadata={"description": "Empty unless the chunk is unfinished, interrupt is true or answer quotes a passage, and the worker's run ends asking loop-claude for a higher rung, citing an escalation trigger. Then: a verbatim quote, without elisions, of the transcript passage where the trigger fired, such as a review round's commit title or the worker's report of the finding or failing test. Where a [step] line says loop-claude stepped the worker up a rung, quote only the worker's own words after it, and leave this empty where the trigger fired only before it. Never a later mention of the trigger, and never the request itself."})
escalation: str = dataclasses.field(metadata={"description": "Empty unless the chunk is unfinished, interrupt is true or answer quotes a passage, and the worker's run ends asking loop-claude for a higher rung, citing an escalation trigger. Then: a verbatim quote, without elisions, of the transcript passage where the trigger fired, such as a review round's commit title or the worker's report of the finding or failing test. " + escalation.QUOTE_AFTER_STEP + " Never a later mention of the trigger, and 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 worker's question; else what shows the chunk finished or the work remaining."})
finished: bool = dataclasses.field(metadata={"description": "Whether the chunk is finished."})
interrupt: bool = dataclasses.field(metadata={"description": "Whether the human must now be interrupted."})
@@ -167,7 +167,7 @@ def quotes_checked(verdict: Verdict, grounds: Grounds) -> Verdict:
"""The verdict with its quoted answer and its escalation checked. A quoted answer the records do not hold goes to the human as an interrupt, keeping the escalation it carried; escalation.checked then checks that escalation. Neither misquote is a Failure, which would otherwise exit an unattended loop."""
if isinstance(verdict, QuotedAnswer) and not verbatim(verdict.answer, grounds.quotable):
verdict = Interrupt(verdict.evidence, verdict.question, escalation=verdict.escalation, highest_likelihood_taken_in=verdict.highest_likelihood_taken_in)
return escalation.checked(verdict, grounds.since_step, grounds.shown)
return escalation.checked(verdict, since=grounds.since_step, shown=grounds.shown)
def judge_prompt(task: str, premises: str, records: str, entries: list[dict], escalated_at: int) -> tuple[str, Grounds]:
+26 -26
View File
@@ -19,7 +19,7 @@ from loop_claude.cycle.retry import retried
from model_routing.route import Route
from loop_claude.cycle.worker import run_worker, worker_args
from model_routing.ladder import Rung
from loop_claude.escalation import Step
from loop_claude.escalation import StepUp
from loop_claude.stop import Stop
from model_routing import worker_rungs
@@ -58,21 +58,21 @@ def run_cycle(cycle: Cycle) -> Stop | Concluded:
event = step(cycle, state)
if isinstance(event, Stop):
return stopped(cycle, event, event.reason)
# Only a ruling's step changes the route within a cycle. No transition reads the route, so it stays out of the states.
if isinstance(event, Ruled) and event.step:
cycle = dataclasses.replace(cycle, route=event.step.after)
# Only a ruling's step-up changes the route within a cycle. No transition reads the route, so it stays out of the states.
if isinstance(event, Ruled) and event.step_up:
cycle = dataclasses.replace(cycle, route=event.step_up.after)
state = transition(state, event)
except KeyboardInterrupt:
return stopped(cycle, Stop(130, ""), "Ctrl-C")
return Concluded(state.verdict, cycle.route)
def stepped(cycle: Cycle, verdict: Verdict) -> Step | None:
"""The step an interrupt's or a quoted answer's escalation makes, before the human is asked or the worker resumes, so the work after the answer runs on it; None where the route holds."""
def ruling_step_up(cycle: Cycle, verdict: Verdict) -> StepUp | None:
"""The step-up an interrupt's or a quoted answer's escalation makes, before the human is asked or the worker resumes, so the work after the answer runs on it; None where the route holds."""
if not (isinstance(verdict, Interrupt | QuotedAnswer) and cycle.project):
return None
after = after_ruling(cycle.project, cycle.route, verdict)
return Step(cycle.route, after) if after != cycle.route else None
return StepUp(cycle.route, after) if after != cycle.route else None
def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
@@ -103,13 +103,13 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
typing.assert_never(state)
def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step: Step | None) -> WorkerRan | Stop:
def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step_up: StepUp | None) -> WorkerRan | Stop:
if isinstance(answered, QuotedAnswer):
print(f"\n=== {prompts.QUOTE_SOURCES} answered: {answered.question} ===\n{answered.answer}", flush=True)
if stop := keep_answer(cycle, answered, step):
if stop := keep_answer(cycle, answered, step_up):
return stop
# worker_start runs inside the lambda: a failed attempt may have opened the session.
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, step), worker_environment(cycle), cycle.effects), cycle.effects):
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, step_up), worker_environment(cycle), cycle.effects), cycle.effects):
return stop
path = transcript_file(cycle.session)
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
@@ -120,23 +120,23 @@ def worker_environment(cycle: Cycle) -> dict[str, str]:
return worker_rungs.environment(cycle.project, cycle.route)
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step: Step | None) -> list[str]:
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step_up: StepUp | None) -> list[str]:
if isinstance(answered, QuotedAnswer):
return worker_args(cycle.session, cycle.route.rung, prompts.answered(answered.question, answered.answer, cycle.route, cycle.project, step is not None), resume=True)
return worker_args(cycle.session, cycle.route.rung, prompts.answered(answered.question, answered.answer, cycle.route, cycle.project, step_up is not None), resume=True)
if isinstance(answered, TypedAnswer):
return worker_args(cycle.session, cycle.route.rung, prompts.typed(answered.question, answered.answer, cycle.route, cycle.project, step is not None), resume=True)
return worker_args(cycle.session, cycle.route.rung, prompts.typed(answered.question, answered.answer, cycle.route, cycle.project, step_up is not None), resume=True)
if session_open(cycle.session):
return worker_args(cycle.session, cycle.route.rung, prompts.resumed(cycle.unfinished_evidence, cycle.route, cycle.project), resume=True)
return worker_args(cycle.session, cycle.route.rung, prompts.fresh(cycle.task, cycle.premises, cycle.route, cycle.project, cycle.facts), resume=False)
def keep_answer(cycle: Cycle, answered: QuotedAnswer, step: Step | None) -> Stop | None:
def keep_answer(cycle: Cycle, answered: QuotedAnswer, step_up: StepUp | None) -> Stop | None:
path = transcript_file(cycle.session)
if not path:
return None
# As in ask: the answer names the rung the worker ran on until then, and a step-up archived after it names the rung it runs on from then.
quoted = outcomes.answered(answered.question, prompts.QUOTE_SOURCES, answered.answer, answered.evidence, answered.escalation, str(judge_of(cycle)), ran_on(cycle, step).record())
if failure := archive_stepped(cycle, path, quoted, step):
quoted = outcomes.answered(answered.question, prompts.QUOTE_SOURCES, answered.answer, answered.evidence, answered.escalation, str(judge_of(cycle)), ran_on(cycle, step_up).record())
if failure := archive_stepped(cycle, path, quoted, step_up):
return Stop(1, failure.reason)
return None
@@ -146,7 +146,7 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
if isinstance(entries, Failure):
return Stop(1, entries.reason)
verdict = judge(cycle.root, cycle.task, cycle.premises, cycle.records, entries, cycle.route.escalated_at, judge_of(cycle), cycle.effects, lambda rejected: keep_rejection(cycle, transcript_path, rejected))
return verdict if isinstance(verdict, Stop) else Ruled(verdict, stepped(cycle, verdict))
return verdict if isinstance(verdict, Stop) else Ruled(verdict, ruling_step_up(cycle, verdict))
def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> Stop | None:
@@ -156,12 +156,12 @@ def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> St
return None
def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer, step: Step | None) -> HumanTyped | TalkChosen | Stop:
def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer, step_up: StepUp | None) -> HumanTyped | TalkChosen | Stop:
question = question_for(ruling)
# Archived now, so the transcript the question was asked on is kept with it; the human's answer then grows the transcript.
# The interrupt names the rung the worker ran on before it; a step-up archived after it names the stepped rung, so the figures charge each stretch to its own rung.
interrupted = outcomes.interrupted(question, ruling.evidence, str(judge_of(cycle)), ruling.escalation, ran_on(cycle, step).record())
if failure := archive_stepped(cycle, transcript_path, interrupted, step):
interrupted = outcomes.interrupted(question, ruling.evidence, str(judge_of(cycle)), ruling.escalation, ran_on(cycle, step_up).record())
if failure := archive_stepped(cycle, transcript_path, interrupted, step_up):
return Stop(1, failure.reason)
# Only the human may cut a release, so a release ready to cut opens claude's session at once.
answer = put_question(question, not (isinstance(ruling, Interrupt) and ruling.release_ready), cycle.effects)
@@ -171,14 +171,14 @@ def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer, st
return TalkChosen() if isinstance(answer, Talk) else HumanTyped(TypedAnswer(ruling.question, answer))
def ran_on(cycle: Cycle, step: Step | None) -> Route:
"""The route the worker ran on until the ruling: the one before its step, if it made one."""
return step.before if step else cycle.route
def ran_on(cycle: Cycle, step_up: StepUp | None) -> Route:
"""The route the worker ran on until the ruling: the one before its step-up, if it made one."""
return step_up.before if step_up else cycle.route
def archive_stepped(cycle: Cycle, transcript_path: str, outcome: str, step: Step | None) -> Failure | None:
"""Archives the outcome of a ruling, then the step up it made, if it made one. The first failure is returned."""
for kept in (outcome, outcomes.stepped_up(step.after.record())) if step else (outcome,):
def archive_stepped(cycle: Cycle, transcript_path: str, outcome: str, step_up: StepUp | None) -> Failure | None:
"""Archives the outcome of a ruling, then the step-up it made, if it made one. The first failure is returned."""
for kept in (outcome, outcomes.stepped_up(step_up.after.record())) if step_up else (outcome,):
if failure := archive(cycle.project_archive, transcript_path, cycle.task, kept, cycle.effects.now()):
return failure
return None
+15 -15
View File
@@ -1,7 +1,7 @@
import dataclasses
from loop_claude.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
from loop_claude.escalation import Step
from loop_claude.escalation import StepUp
# A backstop for what the judge misses, such as a reworded repeat, so the worker cannot loop unattended on answers quoted from README.md or the records.
QUOTED_ANSWERS_PER_CYCLE = 3
@@ -19,8 +19,8 @@ class Working:
"""The worker runs, resumed with an answer quoted from README.md or the records, or typed by the human, when there is one."""
answered: QuotedAnswer | TypedAnswer | None = None
quoted_answers: int = 0
# The step up the ruling behind answered made, so the worker is told of it; None for none.
step: Step | None = None
# The step-up the ruling behind answered made, so the worker is told of it; None for none.
step_up: StepUp | None = None
@dataclasses.dataclass(frozen=True)
@@ -36,8 +36,8 @@ class Interrupting:
transcript_path: str
ruling: Interrupt | QuotedAnswer
quoted_answers: int = 0
# As on Working: the step holds through the answer the human types, and drops on /talk.
step: Step | None = None
# As on Working: the step-up holds through the answer the human types, and drops on /talk.
step_up: StepUp | None = None
@dataclasses.dataclass(frozen=True)
@@ -70,8 +70,8 @@ class WorkerRan:
@dataclasses.dataclass(frozen=True)
class Ruled:
verdict: Verdict
# The step up an interrupt's or a quoted answer's escalation made; None for none, and always for a finished or unfinished verdict.
step: Step | None = None
# The step-up an interrupt's or a quoted answer's escalation made; None for none, and always for a finished or unfinished verdict.
step_up: StepUp | None = None
@dataclasses.dataclass(frozen=True)
@@ -101,16 +101,16 @@ def transition(state: State, event: Event) -> State:
match state, event:
case Working(_, quoted_answers), WorkerRan(transcript_path):
return Judging(transcript_path, quoted_answers)
case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered, step) if quoted_answers < QUOTED_ANSWERS_PER_CYCLE:
return Working(answered, quoted_answers + 1, step)
case Judging(transcript_path, quoted_answers), Ruled(QuotedAnswer() as ruling, step) if quoted_answers >= QUOTED_ANSWERS_PER_CYCLE:
return Interrupting(transcript_path, ruling, quoted_answers, step)
case Judging(transcript_path, quoted_answers), Ruled(Interrupt() as ruling, step):
return Interrupting(transcript_path, ruling, quoted_answers, step)
case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered, step_up) if quoted_answers < QUOTED_ANSWERS_PER_CYCLE:
return Working(answered, quoted_answers + 1, step_up)
case Judging(transcript_path, quoted_answers), Ruled(QuotedAnswer() as ruling, step_up) if quoted_answers >= QUOTED_ANSWERS_PER_CYCLE:
return Interrupting(transcript_path, ruling, quoted_answers, step_up)
case Judging(transcript_path, quoted_answers), Ruled(Interrupt() as ruling, step_up):
return Interrupting(transcript_path, ruling, quoted_answers, step_up)
case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict):
return Closing(transcript_path, verdict)
case Interrupting(_, _, quoted_answers, step), HumanTyped(typed):
return Working(typed, quoted_answers, step)
case Interrupting(_, _, quoted_answers, step_up), HumanTyped(typed):
return Working(typed, quoted_answers, step_up)
case Interrupting(transcript_path, _, quoted_answers), TalkChosen():
return Talking(transcript_path, quoted_answers)
# The human worked in the worker's claude session, so the judge rules again on the transcript it leaves.
+9 -6
View File
@@ -3,8 +3,8 @@
A worker whose run ends asking loop-claude for a higher rung cites the trigger that fired, and the judge quotes the passage where it fired as the verdict's escalation. The mark, Route.escalated_at, is how many transcript entries the session held when an escalation last stepped the worker up, 0 before any. The session's transcript only grows, so the mark holds across cycles, and the archive's Worker: records keep it for a rerun.
- Stamp. stamped records on the judge's verdict how many entries the judge read.
- Step. routing.after_unfinished and routing.after_ruling step the worker up on an escalation and move the mark to mark(verdict). A ruling's Step travels through the cycle's states, so the archive and the worker's next prompt name it.
- Show. transcript puts STEP_LINE after the mark, so the next judge sees that the triggers before it are spent.
- Step. routing.after_unfinished and routing.after_ruling step the worker up on an escalation and move the mark to mark(verdict). A ruling's StepUp travels through the cycle's states, so the archive and the worker's next prompt name it.
- Show. transcript puts STEP_LINE after the mark, and QUOTE_AFTER_STEP, in the judge's schema, tells the next judge that the triggers before it are spent.
- Check. checked keeps an escalation only where it quotes since_step, the worker's own lines after the mark, so a passage that bought one step cannot buy another.
"""
import dataclasses
@@ -15,12 +15,15 @@ from loop_claude.verdicts import Interrupt, QuotedAnswer, Unfinished, Verdict
from claude_code.transcript import render, Typed, verbatim
from model_routing.route import Route
# Reply.escalation's description in cycle/judge.py points at this line. Only the judge reads it, so it is no [loop] line.
STEP_LINE = "[step] loop-claude stepped the worker up a rung here, so the escalation triggers that fired before this line are spent."
# Only the judge reads the step line, so it is no [loop] line.
STEP_TAG = "[step]"
STEP_LINE = f"{STEP_TAG} loop-claude stepped the worker up a rung here, so the escalation triggers that fired before this line are spent."
# Reply.escalation's description in cycle/judge.py carries this sentence, so the judge reads STEP_LINE as the mark.
QUOTE_AFTER_STEP = f"Where a {STEP_TAG} line says loop-claude stepped the worker up a rung, quote only the worker's own words after it, and leave this empty where the trigger fired only before it."
@dataclasses.dataclass(frozen=True)
class Step:
class StepUp:
"""An interrupt's or a quoted answer's escalation stepping the route within a cycle: the route the worker ran on until the ruling, and the one it runs on after."""
before: Route
after: Route
@@ -46,7 +49,7 @@ def since_step(entries: list[dict], at: int, typed_answer: typing.Callable[[str]
return render([entry for entry in entries[at:] if human_text(entry) is None and loop_text(entry) is None], typed_answer)
def checked(verdict: Verdict, since: str, shown: str) -> Verdict:
def checked(verdict: Verdict, *, since: str, shown: str) -> Verdict:
"""The verdict without its escalation unless the escalation quotes both since, since_step's lines, and the transcript the judge was shown. A misquote, or a quote of a passage before the mark, drops the escalation, so the worker's rung holds; it is no failure, which would otherwise exit an unattended loop."""
if isinstance(verdict, Interrupt | QuotedAnswer | Unfinished) and verdict.escalation and not (verbatim(verdict.escalation, since) and verbatim(verdict.escalation, shown)):
return dataclasses.replace(verdict, escalation="")
+1 -1
View File
@@ -4,7 +4,7 @@ The worker, and the session /talk opens, run on a Route (model_routing/route.py)
- Start. routed_item finds the plan item that routes a fresh chunk, if there is one with L, E and I scored; first_route picks the worker's rung from its L, or the project's rung without an item.
- Rerun. kept decides whether a rerun resumes the route its archive last names.
- Step-up. after_unfinished raises the route after an unfinished verdict, after_ruling after an interrupt or a quoted answer that carries an escalation; escalation_cap bounds an escalation.
- Step-up. after_unfinished raises the route after an unfinished verdict, after_ruling after an interrupt or a quoted answer that carries an escalation; escalation_cap bounds an escalation, and loop_claude/escalation.py states the escalation mark's rule.
- Judge. judge_rung picks the rung of the judge of a JUDGE_REPLAYED model.
"""
import dataclasses
+9 -9
View File
@@ -196,15 +196,15 @@ class Transitions(unittest.TestCase):
def test_a_rulings_step_up_reaches_the_worker_it_resumes_through_the_answer_the_human_types_and_no_further(self):
step = escalation.Step(Route(Rung("claude-opus-5-5", "medium")), Route(Rung("claude-opus-5-5", "high"), escalated_at=3))
step_up = escalation.StepUp(Route(Rung("claude-opus-5-5", "medium")), Route(Rung("claude-opus-5-5", "high"), escalated_at=3))
interrupt, quoted = Interrupt("e", "q", "round 2: 🔴"), QuotedAnswer("e", "q", "A.", "round 2: 🔴")
for state, event, after in (
(states.Judging("/t"), states.Ruled(quoted, step), states.Working(quoted, 1, step)),
(states.Judging("/t", 3), states.Ruled(quoted, step), states.Interrupting("/t", quoted, 3, step)),
(states.Judging("/t"), states.Ruled(interrupt, step), states.Interrupting("/t", interrupt, 0, step)),
(states.Interrupting("/t", interrupt, 0, step), states.HumanTyped(TYPED), states.Working(TYPED, 0, step)),
(states.Interrupting("/t", interrupt, 0, step), states.TalkChosen(), states.Talking("/t")),
(states.Working(TYPED, 0, step), states.WorkerRan("/t"), states.Judging("/t")),
(states.Judging("/t"), states.Ruled(quoted, step_up), states.Working(quoted, 1, step_up)),
(states.Judging("/t", 3), states.Ruled(quoted, step_up), states.Interrupting("/t", quoted, 3, step_up)),
(states.Judging("/t"), states.Ruled(interrupt, step_up), states.Interrupting("/t", interrupt, 0, step_up)),
(states.Interrupting("/t", interrupt, 0, step_up), states.HumanTyped(TYPED), states.Working(TYPED, 0, step_up)),
(states.Interrupting("/t", interrupt, 0, step_up), states.TalkChosen(), states.Talking("/t")),
(states.Working(TYPED, 0, step_up), states.WorkerRan("/t"), states.Judging("/t")),
):
with self.subTest(state=state, event=event):
self.assertEqual(states.transition(state, event), after)
@@ -407,8 +407,8 @@ class Cycles(unittest.TestCase):
cycle = dataclasses.replace(self.cycle(FakeEffects()), project=Routing.OPUS, route=Route(Routing.OPUS, 3, 3, 4))
for ruling in (Interrupt("e", "q", entries_read=3), QuotedAnswer("e", "q", "A.", entries_read=3)):
with self.subTest(ruling=ruling):
self.assertIsNone(run.stepped(cycle, ruling))
self.assertEqual(run.stepped(cycle, Interrupt("e", "q", "round 2: 🔴", 4, entries_read=3)), escalation.Step(cycle.route, Route(Routing.XHIGH, 3, 3, 4, escalated_at=3)))
self.assertIsNone(run.ruling_step_up(cycle, ruling))
self.assertEqual(run.ruling_step_up(cycle, Interrupt("e", "q", "round 2: 🔴", 4, entries_read=3)), escalation.StepUp(cycle.route, Route(Routing.XHIGH, 3, 3, 4, escalated_at=3)))
def test_an_escalation_after_an_interrupt_stepped_the_worker_steps_it_again_in_the_same_cycle(self):
streams = [Worker(self.paths.claude_code_transcripts.parent, text="round 2: 🔴"), Worker(self.paths.claude_code_transcripts.parent), Worker(self.paths.claude_code_transcripts.parent)]