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
+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()