Step the worker up as the judge interrupts when the run also cites an escalation trigger

This commit is contained in:
2026-10-17 21:57:11 +02:00
parent 4474182174
commit 1c503e8818
14 changed files with 142 additions and 57 deletions
+20 -13
View File
@@ -59,8 +59,8 @@ class Reply:
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 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 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."})
# Routes the worker one rung up, as the judge interrupts 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 or interrupt is true, 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."})
finished: bool = dataclasses.field(metadata={"description": "Whether the chunk is finished."})
interrupt: bool = dataclasses.field(metadata={"description": "Whether the human must now be interrupted."})
@@ -87,6 +87,8 @@ class Rejected(Failure):
class Interrupt:
evidence: str
question: str
# The judge's quote showing that the worker's cited escalation trigger fired; empty for none.
escalation: str = ""
@dataclasses.dataclass(frozen=True)
@@ -126,7 +128,7 @@ def verdict_from(output: object) -> Verdict | Failure:
if output["interrupt"]:
if not output["question"].strip():
return Failure("the judge interrupted without a question")
return Interrupt(output["evidence"], output["question"])
return Interrupt(output["evidence"], output["question"], output["escalation"])
if output["answer"].strip():
if not output["question"].strip():
return Failure("the judge quoted an answer without a question")
@@ -160,12 +162,7 @@ def judge_args(rung: Rung) -> list[str]:
def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str, effects: Effects) -> Verdict | Failure | Limited:
"""One judge run, its three quotes each checked against what they quote:
- evidence, against shown, its fragments in order: a misquote is a Failure, which judge retries;
- a quoted answer, against quotable, word for word: a misquote puts the question to the human;
- an escalation, against shown, word for word: a misquote drops it, so the worker's rung holds.
A misquoted answer or escalation is no Failure, which would otherwise exit an unattended loop. Neither may be elided, since stitched fragments can say what the records or the transcript do not.
"""
"""One judge run. Its evidence is checked against shown, its fragments in order: a misquote is a Failure, which judge retries."""
reply = claude_reply(effects.claude, args, root, prompt, "the judge")
if isinstance(reply, Failure | Limited):
return reply
@@ -175,10 +172,20 @@ def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str
return Rejected(verdict.reason, replied)
if not grounded(verdict.evidence, 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)
if isinstance(verdict, QuotedAnswer) and not verbatim(verdict.answer, quotable):
return Interrupt(verdict.evidence, verdict.question)
if isinstance(verdict, Unfinished) and verdict.escalation and not verbatim(verdict.escalation, shown):
return dataclasses.replace(verdict, escalation="")
return quotes_checked(verdict, shown, quotable)
def quotes_checked(verdict: Verdict, shown: str, quotable: str) -> Verdict:
"""The verdict with its quotes checked word for word, since stitched fragments can say what the records or the transcript do not:
- a quoted answer, against quotable: a misquote puts the question to the human;
- an escalation, on an interrupt or an unfinished verdict, against shown: a misquote drops it, so the worker's rung holds.
A misquote is no Failure, which would otherwise exit an unattended loop.
"""
match verdict:
case QuotedAnswer() if not verbatim(verdict.answer, quotable):
return Interrupt(verdict.evidence, verdict.question)
case Interrupt() | Unfinished() if verdict.escalation and not verbatim(verdict.escalation, shown):
return dataclasses.replace(verdict, escalation="")
return verdict
+31 -6
View File
@@ -7,9 +7,9 @@ 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 failure import Failure
from loop_claude.cycle.judge import Finished, Interrupt, judge, QuotedAnswer, Rejected, Unfinished
from loop_claude.cycle.judge import Finished, Interrupt, judge, plain, QuotedAnswer, Rejected, Unfinished, Verdict
from loop_claude.cycle.interrupt import interrupt
from loop_claude.routing import judge_rung
from loop_claude.routing import judge_rung, stepped_by_interrupt
from transcript_archive import outcomes
from loop_claude.paths import Paths
from loop_claude.cycle import prompts
@@ -40,19 +40,37 @@ class Cycle:
anchor: Rung | None
# Empty when the previous cycle's verdict was not Unfinished.
unfinished_evidence: str
# The judge's quote that stepped the route up as an interrupt; a later ruling quoting it again steps nothing.
escalation_spent: str = ""
def run_cycle(cycle: Cycle) -> Stop | Finished | Unfinished:
@dataclasses.dataclass(frozen=True)
class Concluded:
"""How a cycle ended: its verdict, and the route it ended on, which an interrupt's escalation may have stepped up from the route it began on."""
verdict: Finished | Unfinished
route: Route
def run_cycle(cycle: Cycle) -> Stop | Concluded:
state: State = Working()
try:
while not isinstance(state, Ended):
event = step(cycle, state)
if isinstance(event, Stop):
return stopped(cycle, event, event.reason)
cycle = escalated(cycle, event)
state = transition(state, event)
except KeyboardInterrupt:
return stopped(cycle, Stop(130, ""), "Ctrl-C")
return state.verdict
return Concluded(state.verdict, cycle.route)
def escalated(cycle: Cycle, event: Event) -> Cycle:
"""The cycle after the judge interrupted with an escalation: its route stepped up before the human is asked, so the work after the answer runs on it."""
if not (isinstance(event, Ruled) and isinstance(event.verdict, Interrupt) and event.verdict.escalation):
return cycle
route = stepped_by_interrupt(cycle.anchor, cycle.route) if cycle.anchor else cycle.route
return dataclasses.replace(cycle, route=route, escalation_spent=event.verdict.escalation)
def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
@@ -118,7 +136,14 @@ 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, render(entries), judge_of(cycle), cycle.effects, lambda rejected: keep_rejection(cycle, transcript_path, rejected))
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
return verdict if isinstance(verdict, Stop) else Ruled(unspent(verdict, cycle.escalation_spent))
def unspent(verdict: Verdict, spent: str) -> Verdict:
"""The verdict without an escalation that already stepped the route as an interrupt, so one trigger buys one step."""
if isinstance(verdict, Interrupt | Unfinished) and verdict.escalation and plain(verdict.escalation) == plain(spent):
return dataclasses.replace(verdict, escalation="")
return verdict
def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> Stop | None:
@@ -131,7 +156,7 @@ def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> St
def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer) -> HumanAnswered | 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.
if failure := archive(cycle.project_archive, transcript_path, cycle.task, outcomes.interrupted(question, ruling.evidence, str(judge_of(cycle))), cycle.effects.now()):
if failure := archive(cycle.project_archive, transcript_path, cycle.task, outcomes.interrupted(question, ruling.evidence, str(judge_of(cycle)), ruling.escalation if isinstance(ruling, Interrupt) else ""), cycle.effects.now()):
return Stop(1, failure.reason)
return interrupt(cycle.root, cycle.session.id, cycle.route.rung, transcript_path, question, cycle.paths, worker_environment(cycle), cycle.effects) or HumanAnswered()