Answer architecture round 1: 0 🔴, 6 🟡; show the judge the mark, carry the entries read on the verdict, name the checked texts

This commit is contained in:
2026-10-18 13:50:06 +02:00
parent 1ed305f1e2
commit 2bba66ac0a
10 changed files with 158 additions and 104 deletions
+41 -22
View File
@@ -56,6 +56,8 @@ FINISHED_RULES = (
"continue: an open PR, a review not yet run, CI not yet reported, an answer not yet recorded."
)
JUDGE_RULES = "\n\n".join((INTERRUPT_RULES, QUOTE_RULES, FINISHED_RULES))
# Where an escalation last stepped the worker up, judge_prompt shows the judge this line at the mark, and Reply.escalation's description points at it.
STEP_LINE = "[loop] loop-claude stepped the agent up a rung here. The escalation triggers that fired before this line are spent."
@dataclasses.dataclass(frozen=True)
@@ -66,7 +68,7 @@ class Reply:
# Routes the next cycle's judge and reviewers, as bundled routes the worker.
bundled_escape_and_impact: int = dataclasses.field(metadata={"description": "The highest E + I the agent 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: 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, interrupt is true or answer quotes a passage, and the agent's run ends asking loop-claude for a higher rung, citing an escalation trigger. Then: a verbatim quote, without elisions, of the earlier transcript passage where the trigger fired, such as a review round's commit title or the agent's report of the finding or failing test. Never a later mention of it, 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 agent'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 agent's report of the finding or failing test. Where a [loop] line says loop-claude stepped the agent up a rung, quote only from after it. 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 agent'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."})
@@ -100,6 +102,8 @@ class Interrupt:
# The bundled L the judge saw, 0 for none, as on Unfinished: an escalation steps up from the rung that item picks.
bundled: int = 0
release_ready: bool = False
# How many transcript entries the judge read, which judge_once stamps: the mark an escalation the verdict carries sets on the route.
entries_read: int = 0
@dataclasses.dataclass(frozen=True)
@@ -111,6 +115,7 @@ class QuotedAnswer:
# As on Interrupt.
escalation: str = ""
bundled: int = 0
entries_read: int = 0
@dataclasses.dataclass(frozen=True)
@@ -127,11 +132,28 @@ class Unfinished:
escalation: str = ""
# The bundled E + I the judge saw, 0 for none.
bundled_escape_and_impact: int = 0
# As on Interrupt.
entries_read: int = 0
Verdict = Interrupt | QuotedAnswer | Finished | Unfinished
class Grounds(typing.NamedTuple):
"""What run_judge checks the judge's quotes against, word for word, since stitched fragments can say what their source does not."""
# The transcript as the judge saw it. The evidence must quote it, its fragments in order: a misquote is Rejected, which judge retries.
shown: str
# The part of shown after STEP_LINE, all of shown where it holds none. An escalation must quote it: a misquote, or a quote of a trigger that already bought a step, drops the escalation, so the worker's rung holds.
since_step: str
# README.md's premises and the records. A quoted answer must quote them: a misquote puts the question to the human.
quotable: str
def escalation_mark(verdict: Interrupt | QuotedAnswer | Unfinished) -> int | None:
"""The mark the verdict's escalation sets on the route; None where it carries none."""
return verdict.entries_read if verdict.escalation else None
def verdict_from(output: object) -> Verdict | Failure:
if not isinstance(output, dict):
return Failure("the judge returned no reply")
@@ -161,10 +183,9 @@ def judge(root: str, task: str, premises: str, records: str, entries: list[dict]
def judge_once(root: str, task: str, premises: str, records: str, entries: list[dict], escalated_at: int, rung: Rung, effects: Effects) -> Verdict | Failure | Limited:
"""One judge run on the transcript's entries, as judge retries it; judge-replay replays it. escalated_at is the route's mark: an escalation quote counts only from the entries after it."""
prompt, shown = judge_prompt(task, premises, records, render(entries, typed_answer))
since_step = render(entries[escalated_at:], typed_answer)
# render joins each entry's lines, so both are tails of one text, and the shorter is the part of shown rendered from the entries after the mark.
return run_judge(root, judge_args(rung), prompt, shown, min(shown, since_step, key=len), f"{premises}\n\n{records}", effects)
prompt, grounds = judge_prompt(task, premises, records, entries, escalated_at)
verdict = run_judge(root, judge_args(rung), prompt, grounds, effects)
return dataclasses.replace(verdict, entries_read=len(entries)) if isinstance(verdict, Interrupt | QuotedAnswer | Unfinished) else verdict
def judge_args(rung: Rung) -> list[str]:
@@ -177,13 +198,8 @@ def judge_args(rung: Rung) -> list[str]:
]
def run_judge(root: str, args: list[str], prompt: str, shown: str, since_step: str, quotable: str, effects: Effects) -> Verdict | Failure | Limited:
"""One judge run. Each quote in its verdict is checked word for word, since stitched fragments can say what their source does not:
- the evidence, against shown (the transcript as the judge saw it), its fragments in order: a misquote is Rejected, which judge retries;
- a quoted answer, against quotable: a misquote puts the question to the human;
- an escalation, against since_step (the part of shown after the worker's last step-up): a misquote, or a quote of a trigger that already bought a step, drops it, so the worker's rung holds.
quotes_checked checks the last two. Neither misquote is a Failure, which would otherwise exit an unattended loop.
"""
def run_judge(root: str, args: list[str], prompt: str, grounds: Grounds, effects: Effects) -> Verdict | Failure | Limited:
"""One judge run, each quote in its verdict checked against grounds as Grounds says. quotes_checked checks the quoted answer and the escalation: neither misquote is a Failure, which would otherwise exit an unattended loop."""
reply = claude_reply(effects.claude, args, root, prompt, "the judge")
if isinstance(reply, Failure | Limited):
return reply
@@ -191,22 +207,24 @@ def run_judge(root: str, args: list[str], prompt: str, shown: str, since_step: s
replied = json.dumps(reply, ensure_ascii=False)
if isinstance(verdict, Failure):
return Rejected(verdict.reason, replied)
if not grounded(verdict.evidence, shown):
if not grounded(verdict.evidence, grounds.shown):
return Rejected(f"the judge quoted {verdict.evidence!r}, which is not in the transcript" + (f", for the question: {verdict.question}" if isinstance(verdict, Interrupt | QuotedAnswer) else ""), replied)
return quotes_checked(verdict, since_step, quotable)
return quotes_checked(verdict, grounds)
def quotes_checked(verdict: Verdict, since_step: str, quotable: str) -> Verdict:
"""The verdict with its quoted answer and its escalation checked, as run_judge says; a quoted answer put to the human keeps the escalation it carried."""
if isinstance(verdict, QuotedAnswer) and not verbatim(verdict.answer, quotable):
def quotes_checked(verdict: Verdict, grounds: Grounds) -> Verdict:
"""The verdict with its quoted answer and its escalation checked, as Grounds says; a quoted answer put to the human keeps the escalation it carried."""
if isinstance(verdict, QuotedAnswer) and not verbatim(verdict.answer, grounds.quotable):
verdict = Interrupt(verdict.evidence, verdict.question, verdict.escalation, verdict.bundled)
if isinstance(verdict, Interrupt | QuotedAnswer | Unfinished) and verdict.escalation and not verbatim(verdict.escalation, since_step):
if isinstance(verdict, Interrupt | QuotedAnswer | Unfinished) and verdict.escalation and not verbatim(verdict.escalation, grounds.since_step):
return dataclasses.replace(verdict, escalation="")
return verdict
def judge_prompt(task: str, premises: str, records: str, rendered_transcript: str) -> tuple[str, str]:
"""The prompt, and the part of the transcript it shows, which the judge's evidence must quote."""
def judge_prompt(task: str, premises: str, records: str, entries: list[dict], escalated_at: int) -> tuple[str, Grounds]:
"""The prompt, with STEP_LINE between the entries before the mark, escalated_at, and those after it, and what the judge's quotes are checked against."""
since_step = render(entries[escalated_at:], typed_answer)
transcript = "\n\n".join(part for part in (render(entries[:escalated_at], typed_answer), STEP_LINE if escalated_at else "", since_step) if part)
head = (
f"From README.md:\n{premises}\n\n"
f"The records: the project's working rules and decisions, then the human's rules for every project:\n{records or 'none'}\n\n"
@@ -215,8 +233,9 @@ def judge_prompt(task: str, premises: str, records: str, rendered_transcript: st
)
room = max(0, JUDGE_PROMPT_CHARS - len(head) - len(JUDGE_RULES) - 2)
# The tail, where an interrupt or a finish lands; the session's transcript grows cycle over cycle.
shown = rendered_transcript[max(0, len(rendered_transcript) - room):]
return f"{head}{shown}\n\n{JUDGE_RULES}", shown
shown = transcript[max(0, len(transcript) - room):]
# shown and since_step both end transcript, so the shorter is the part of shown after STEP_LINE.
return f"{head}{shown}\n\n{JUDGE_RULES}", Grounds(shown, min(shown, since_step, key=len), f"{premises}\n\n{records}")
def grounded(evidence: str, rendered_transcript: str) -> bool:
+15 -6
View File
@@ -85,17 +85,26 @@ def rung_named(rung: Rung, route: Route, anchor: Rung) -> str:
def escalation(route: Route, anchor: Rung | None) -> str:
"""The prompt's escalation sentences, the marker's among them. Empty off the ladder or on its top rung. Where escalation_rungs opens none, the worker escalates only with the human's yes.
Else they name the rungs escalation_rungs opens to a subagent, and offer to resume the worker one rung up for the rest of the chunk, up to the highest of those rungs."""
"""The prompt's escalation sentences, the marker's among them, each led by a space. Empty off the ladder or on its top rung. Where escalation_rungs opens none, the worker escalates only with the human's yes.
Else they name the rungs escalation_rungs opens to a subagent, and offer to resume the worker one rung up for the rest of the chunk, up to the highest of those rungs.
For a worker on Opus 5.5 at high, on an Opus 5.5 project, routed by item 3 of L 4, they read: ` When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to `model opus, effort xhigh`, one rung above yours, and on each further trigger one rung higher, up to `model opus, effort max`; above that, ask the human first. Open the subagent's prompt with `Escalation trigger <n>:`, n the trigger's number. Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to `model opus, effort max`.`
"""
if anchor is None or route.rung not in LADDER[:-1]:
return ""
marked = f" Open the subagent's prompt with `{ESCALATION_MARKER} <n>:`, n the trigger's number."
marked = f"Open the subagent's prompt with `{ESCALATION_MARKER} <n>:`, n the trigger's number."
rungs = [f"`{rung.agent_args()}`" for rung in escalation_rungs(anchor, route)]
if not rungs:
return f" An escalation trigger in `~/.claude/CLAUDE.md` lets you hand the hard part of a task to a subagent with the Agent tool set to `{climbed(route.rung, 1).agent_args()}`, one rung above yours, only once the human agrees; loop-claude will not resume you higher for a trigger either, so ask the human for that yes.{marked}"
return " " + " ".join((
f"An escalation trigger in `~/.claude/CLAUDE.md` lets you hand the hard part of a task to a subagent with the Agent tool set to `{climbed(route.rung, 1).agent_args()}`, one rung above yours, only once the human agrees; loop-claude will not resume you higher for a trigger either, so ask the human for that yes.",
marked,
))
further = f", and on each further trigger one rung higher, up to {rungs[-1]}" if len(rungs) > 1 else ""
resumed_higher = f" Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to {rungs[-1]}."
return f" When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to {rungs[0]}, one rung above yours{further}; above that, ask the human first.{marked}{resumed_higher}"
return " " + " ".join((
f"When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to {rungs[0]}, one rung above yours{further}; above that, ask the human first.",
marked,
f"Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to {rungs[-1]}.",
))
def reruns(route: Route, anchor: Rung | None) -> str:
+6 -12
View File
@@ -6,8 +6,8 @@ from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, Hu
from transcript_archive.write import archive
from claude_code.session import delete_session, Session, session_open, transcript_file
from loop_claude.effects import Effects
from loop_claude.cycle.judge import escalation_mark, Finished, Interrupt, judge, QuotedAnswer, Rejected, Unfinished
from failure import Failure
from loop_claude.cycle.judge import Finished, Interrupt, judge, QuotedAnswer, Rejected, Unfinished
from loop_claude.routing import judge_rung, stepped_by_ruling
from transcript_archive import outcomes
from loop_claude.paths import Paths
@@ -43,16 +43,13 @@ class Cycle:
# Only an interrupt's or a quoted answer's escalation changes the route within a cycle. No transition reads the route or the fields below, so they stay out of the states.
# The route before that ruling stepped it up, for ask and keep_answer to archive and for worker_start to tell the worker; None unless the latest event is that ruling, or the answer the human typed to it.
stepped_from: Route | None = None
# How many transcript entries the latest ruling read; 0 before the first.
judged: int = 0
@dataclasses.dataclass(frozen=True)
class Concluded:
"""How a cycle ended: its verdict, the route it ended on, which an interrupt's or a quoted answer's escalation may have stepped up from the route it began on, and how many transcript entries the verdict's judge read, which its escalation marks."""
"""How a cycle ended: its verdict, and the route it ended on, which an interrupt's or a quoted answer's escalation may have stepped up from the route it began on."""
verdict: Finished | Unfinished
route: Route
judged: int
def run_cycle(cycle: Cycle) -> Stop | Concluded:
@@ -66,7 +63,7 @@ def run_cycle(cycle: Cycle) -> Stop | Concluded:
state = transition(state, event)
except KeyboardInterrupt:
return stopped(cycle, Stop(130, ""), "Ctrl-C")
return Concluded(state.verdict, cycle.route, cycle.judged)
return Concluded(state.verdict, cycle.route)
def escalated(cycle: Cycle, event: Event) -> Cycle:
@@ -74,12 +71,9 @@ def escalated(cycle: Cycle, event: Event) -> Cycle:
if isinstance(event, HumanTyped):
return cycle
cycle = dataclasses.replace(cycle, stepped_from=None)
if not isinstance(event, Ruled):
if not (isinstance(event, Ruled) and isinstance(event.verdict, Interrupt | QuotedAnswer) and cycle.anchor):
return cycle
cycle = dataclasses.replace(cycle, judged=event.entries)
if not (isinstance(event.verdict, Interrupt | QuotedAnswer) and event.verdict.escalation):
return cycle
route = stepped_by_ruling(cycle.anchor, cycle.route, event.verdict.bundled, event.entries) if cycle.anchor else cycle.route
route = stepped_by_ruling(cycle.anchor, cycle.route, event.verdict.bundled, escalation_mark(event.verdict))
return dataclasses.replace(cycle, route=route, stepped_from=cycle.route if route != cycle.route else None)
@@ -154,7 +148,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, len(entries))
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> Stop | None:
-2
View File
@@ -65,8 +65,6 @@ class WorkerRan:
@dataclasses.dataclass(frozen=True)
class Ruled:
verdict: Verdict
# How many transcript entries the judge read, which an escalation the verdict carries marks on the route; no transition reads it.
entries: int
@dataclasses.dataclass(frozen=True)