diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index c006f6fa..2f493037 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -15,6 +15,8 @@ from project_config.test_project_config import configured from loop_effects.effects import Effects from failure import Failure from loop_claude.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished +from loop_claude.cycle.judge import TRANSCRIPT_HEADING, Rejected, judge_archived +from claude_code.entries import AssistantTurn, HumanTurn, LoopPrompt, Text from judge_replay import main from rung_replay.search import Miss, Replay, Tried from model_routing.ladder import Rung @@ -177,6 +179,8 @@ class Main(unittest.TestCase): self.assertIn("The reference judge, claude-opus-5-5 at xhigh effort, reproduced 2.", out) # The first replay, on the finished verdict, reads the archived prompt; the second, on the interrupt, is rebuilt from the records of today. self.assertEqual(self.prompts[0], prompt) + self.assertEqual(self.calls[0][2], self.calls[1][2]) + self.assertTrue(self.calls[0][2].endswith("/code/proj")) self.assertIn("The task the worker was given:\nDo it.", self.prompts[1]) self.assertIn("1. Ship.", self.prompts[1]) @@ -253,6 +257,55 @@ class Main(unittest.TestCase): self.assertEqual(ran.returncode, 0) self.assertIn("usage: judge-replay ", ran.stdout) + def test_exits_1_on_a_usage_limit_met_on_an_archived_prompt(self): + kept = self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1" + (kept / "judge").mkdir() + (kept / "judge" / "run.json").write_text(json.dumps({"prompt": "P", "reply": {}})) + (kept / "t0.outcomes.md").write_text("Task: Do it.\n\n2026-10-14T10:00:00Z Verdict: finished\n\nJudge: j\nJudge run: judge/run.json\nWorker: x\n") + code, _, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: {"type": "result", "api_error_status": 429, "result": "You've hit your limit"}) + self.assertEqual((code, "usage limit: You've hit your limit" in err), (1, True)) + + +class JudgeArchivedRuns(unittest.TestCase): + """judge_archived on a prompt of two parts, as the archive keeps it: HEAD, the premises, and what follows TRANSCRIPT_HEADING, the transcript the judge was shown.""" + PROMPT = f"HEAD: Ship fast.\n\n{TRANSCRIPT_HEADING}Worker chose option B.\n[human] Use Postgres.\n" + ENTRIES = [AssistantTurn(parts=(Text("Worker chose option B."),)), HumanTurn(text="Use Postgres."), LoopPrompt(text="Keep going.")] + + def judged(self, **fields): + """What judge_archived returns where the judge's structured output is fields over an unfinished verdict, and the cwd claude ran in.""" + output = {"answer": "", "escalation": "", "evidence": "Worker chose option B.", "finished": False, "interrupt": False, "question": "", "answered_before": "", "left_open": "", "reversed": "", "release_ready": False, **fields} + cwds = [] + + def claude(args, cwd, prompt): + cwds.append(cwd) + return subprocess.CompletedProcess(args, 0, json.dumps({"type": "result", "structured_output": output, "total_cost_usd": 0.25}), "") + + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=unused, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, tmux=unused, tmux_on_terminal=unused) + return judge_archived("/code/p", self.PROMPT, self.ENTRIES, REFERENCE, effects), cwds + + def test_runs_in_the_projects_folder_and_stamps_the_verdict_with_the_entries_read(self): + verdict, cwds = self.judged() + self.assertEqual((cwds, verdict.entries_read), (["/code/p"], 3)) + + def test_checks_the_evidence_against_the_transcript_the_judge_was_shown_and_not_the_premises(self): + self.assertIsInstance(self.judged(evidence="Ship fast.")[0], Rejected) + self.assertIsInstance(self.judged(evidence="Worker chose option B.")[0], Unfinished) + + def test_keeps_the_cost_and_the_run_of_a_rejected_reply(self): + rejected, _ = self.judged(evidence="Never said.") + self.assertEqual((rejected.cost, rejected.run.prompt), (0.25, self.PROMPT)) + + def test_checks_a_quoted_answer_against_the_premises(self): + self.assertIsInstance(self.judged(answer="Ship fast.", question="Which?")[0], QuotedAnswer) + self.assertIsInstance(self.judged(answer="Use Postgres.", question="Which?")[0], Interrupt) + + def test_keeps_an_escalation_that_the_worker_wrote_after_the_start_of_the_transcript(self): + self.assertEqual(self.judged(escalation="Worker chose option B.")[0].escalation, "Worker chose option B.") + self.assertEqual(self.judged(escalation="Use Postgres.")[0].escalation, "") + + def test_names_the_typed_answer_that_holds_a_reversal(self): + self.assertEqual(self.judged(reversed="Use Postgres.")[0].judged.reversed, "Use Postgres.") + if __name__ == "__main__": unittest.main() diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 08c02979..802c7e05 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -426,6 +426,7 @@ Worker: {OFF_LADDER} run.run_cycle(dataclasses.replace(self.cycle(fake, records="From AGENTS.md:\nUse A."), project_archive=pathlib.Path(archive))) kept = "".join(path.read_text() for path in pathlib.Path(archive).glob("*/*.outcomes.md")) self.assertEqual(kept.count("Judge cost: 0.2500\n"), 1, kept) + self.assertIn("Judge cost: 0.2500\nJudge run: judge/", kept) def test_stops_with_exit_1_where_a_subagents_transcript_cannot_be_read_for_the_spend(self): for verdict in ("interrupt", "finished"): diff --git a/bin/transcript_archive/test_transcript_archive.py b/bin/transcript_archive/test_transcript_archive.py index b40e8008..a191c332 100644 --- a/bin/transcript_archive/test_transcript_archive.py +++ b/bin/transcript_archive/test_transcript_archive.py @@ -62,6 +62,26 @@ class JudgeRuns(unittest.TestCase): self.assertEqual(outcomes.judge_run_at(kept, utc(NOW + 1)), self.judged.file) self.assertIsNone(outcomes.judge_run_at(kept, utc(NOW + 2))) + def test_keeps_a_run_as_readable_utf_8_json_indented_by_one_space(self): + run = JudgeRun("Prompt é.", {"a": 1}) + self.assertEqual(run.content(), b'{\n "prompt": "Prompt \xc3\xa9.",\n "reply": {\n "a": 1\n }\n}') + + def test_names_the_run_on_each_kind_of_outcome_that_has_a_judge(self): + named = f"Judge: j\nJudge run: {self.judged.file}\n" + kept = [ + outcomes.unfinished("w", "e", "j", "", judge_run=self.judged), + outcomes.interrupted("Q?", "e", "j", "", "w", judge_run=self.judged), + outcomes.answered(question="Q?", sources="S", answer="a", evidence="e", escalation="", judge="j", worker="w", judge_run=self.judged), + ] + self.assertEqual([named in outcome for outcome in kept], [True, True, True]) + + def test_names_the_folder_it_cannot_make_in_the_failure_to_keep_a_run(self): + with tempfile.TemporaryDirectory() as root: + (pathlib.Path(root) / "judge").write_text("a file where the folder should be") + failure = write.keep_judge_run(pathlib.Path(root), self.judged) + self.assertRegex(failure.reason, f"^could not archive the transcript: {root}/judge: ") + self.assertNotIn("None", failure.reason) + def test_writes_no_outcome_where_the_run_cannot_be_kept(self): with tempfile.TemporaryDirectory() as root: kept = pathlib.Path(root, "archive")