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: