diff --git a/bin/loop_claude/cycle/judge.py b/bin/loop_claude/cycle/judge.py index 697f5f64..b3f73c69 100644 --- a/bin/loop_claude/cycle/judge.py +++ b/bin/loop_claude/cycle/judge.py @@ -133,9 +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, 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(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects, keep: typing.Callable[[Rejected], Stop | None]) -> Verdict | Stop: + """keep sees each reply loop-claude rejected, the last of three included; a Stop from it ends the retrying.""" + return retried("judge", lambda: judge_once(root, task, premises, records, rendered_transcript, rung, effects), effects, lambda failure: keep(failure) if isinstance(failure, Rejected) else None) def judge_once(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects) -> Verdict | Failure | Limited: @@ -165,10 +165,11 @@ def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str if isinstance(reply, Failure | Limited): return reply verdict = verdict_from(structured_output(reply)) + replied = json.dumps(reply, ensure_ascii=False) if isinstance(verdict, Failure): - return Rejected(verdict.reason, json.dumps(reply, ensure_ascii=False)) + 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 ""), json.dumps(reply, ensure_ascii=False)) + 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): diff --git a/bin/loop_claude/cycle/retry.py b/bin/loop_claude/cycle/retry.py index e2272d76..149d52d0 100644 --- a/bin/loop_claude/cycle/retry.py +++ b/bin/loop_claude/cycle/retry.py @@ -15,7 +15,7 @@ UNKNOWN_RESET_POLL = 600 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.""" + """The attempt's result, retried on a failure; keep sees each failure, the last of three included, and a Stop from it ends the retrying.""" failures = 0 while True: result = attempt() diff --git a/bin/loop_claude/cycle/run.py b/bin/loop_claude/cycle/run.py index d0d67520..3fc69489 100644 --- a/bin/loop_claude/cycle/run.py +++ b/bin/loop_claude/cycle/run.py @@ -115,13 +115,13 @@ 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, lambda failure: keep_rejection(cycle, transcript_path, failure)) + 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) -def keep_rejection(cycle: Cycle, transcript_path: str, failure: Failure) -> Stop | None: +def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> 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())): + if unkept := archive(cycle.project_archive, transcript_path, cycle.task, outcomes.rejected(rejected.reason, rejected.reply, str(judge_of(cycle))), cycle.effects.now()): return Stop(1, unkept.reason) return None diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 5d2094d5..70faf288 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -243,6 +243,15 @@ class Cycles(unittest.TestCase): f"{stamp} Judge reply rejected: the judge quoted 'never said', which is not in the transcript\n\nReply:\n> {misquoted.stdout}\n\nJudge: judge-model\n\n" f"{stamp} Verdict: finished\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n"]) + def test_stops_when_a_rejected_reply_cannot_be_archived(self): + self.archive.parent.mkdir(parents=True) + self.archive.write_text("") + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[ran(json.dumps({"structured_output": {"interrupt": False}})), judged("finished")]) + stop = run.run_cycle(self.cycle(fake)) + self.assertRegex(stop.reason, f"^could not archive the transcript: {self.archive}/[0-9a-f]{{64}}: Not a directory$") + self.assertEqual(stop.code, 1) + self.assertEqual(len(fake.made("claude")), 1) + def test_archives_the_judges_rejected_replies_though_it_fails_three_times_running(self): unfilled = ran(json.dumps({"structured_output": {"interrupt": False}})) fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[unfilled] * 3)