Archive each judge reply the loop rejects, with why and the reply

This commit is contained in:
2026-10-16 23:11:29 +02:00
parent 5747d2792a
commit 23dac2c1d1
9 changed files with 68 additions and 16 deletions
+11 -4
View File
@@ -74,6 +74,12 @@ JUDGE_SCHEMA = {
}
@dataclasses.dataclass(frozen=True)
class Rejected(Failure):
"""The judge replied, and loop-claude refused the reply for the reason; reply is what the judge returned, as json."""
reply: str
@dataclasses.dataclass(frozen=True)
class Interrupt:
evidence: str
@@ -127,8 +133,9 @@ def verdict_from(output: object) -> Verdict | Failure:
return Unfinished(output["evidence"], bundled, output["escalation"])
def judge(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects) -> Verdict | Stop:
return retried("judge", lambda: judge_once(root, task, premises, records, rendered_transcript, rung, effects), effects)
def judge(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects, keep: typing.Callable[[Failure], Stop | None]) -> Verdict | Stop:
"""keep sees each failure of a judge run, such as a Rejected reply, before the retry."""
return retried("judge", lambda: judge_once(root, task, premises, records, rendered_transcript, rung, effects), effects, keep)
def judge_once(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects) -> Verdict | Failure | Limited:
@@ -159,9 +166,9 @@ def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str
return reply
verdict = verdict_from(structured_output(reply))
if isinstance(verdict, Failure):
return Failure(f"{verdict.reason}; it replied: {json.dumps(reply, ensure_ascii=False)}")
return Rejected(verdict.reason, json.dumps(reply, ensure_ascii=False))
if not grounded(verdict.evidence, shown):
return Failure(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 ""))
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 ""), json.dumps(reply, ensure_ascii=False))
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):
+4 -1
View File
@@ -14,7 +14,8 @@ RETRY_DELAY = 60
UNKNOWN_RESET_POLL = 600
def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effects: Effects) -> T | Stop:
def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effects: Effects, keep: typing.Callable[[Failure], Stop | None] = lambda failure: None) -> T | Stop:
"""The attempt's result, retried on a failure; keep sees each failure before the retry, and a Stop from it ends the retrying."""
failures = 0
while True:
result = attempt()
@@ -28,6 +29,8 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effec
continue
if not isinstance(result, Failure):
return result
if stop := keep(result):
return stop
failures += 1
if failures == FAILURES_RUNNING:
return Stop(1, f"the {who} failed three times running: {result.reason}")
+9 -2
View File
@@ -7,7 +7,7 @@ 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, Unfinished
from loop_claude.cycle.judge import Finished, Interrupt, judge, QuotedAnswer, Rejected, Unfinished
from loop_claude.cycle.interrupt import interrupt
from loop_claude.routing import judge_rung
from transcript_archive import outcomes
@@ -115,10 +115,17 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
entries = read_transcript(transcript_path)
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)
verdict = judge(cycle.root, cycle.task, cycle.premises, cycle.records, render(entries), judge_of(cycle), cycle.effects, lambda failure: keep_rejection(cycle, transcript_path, failure))
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
def keep_rejection(cycle: Cycle, transcript_path: str, failure: Failure) -> Stop | None:
"""Archives a judge reply that loop-claude refused, so the archive answers why a run exited 1 or a verdict took a retry."""
if isinstance(failure, Rejected) and (unkept := archive(cycle.project_archive, transcript_path, cycle.task, outcomes.rejected(failure.reason, failure.reply, str(judge_of(cycle))), cycle.effects.now())):
return Stop(1, unkept.reason)
return None
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.