import contextlib import hashlib import io import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_key import checkout_of, project_key from failure import Failure from transcript_archive import outcomes, read, write from transcript_archive.location import project_archive from utc_stamp import utc NOW = 1790000000 class Archive(unittest.TestCase): def test_writes_afresh_a_copy_whose_outcomes_file_is_missing(self): with tempfile.TemporaryDirectory() as root: kept = pathlib.Path(root, "archive") transcript = pathlib.Path(root, "t.jsonl") transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') with contextlib.redirect_stderr(io.StringIO()): self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("m", "working", "j", ""), NOW)) (kept_outcomes,) = kept.glob("*/*.outcomes.md") kept_outcomes.unlink() self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("m", "Ctrl-C"), NOW)) self.assertEqual(kept_outcomes.read_text(), "Task: Do it.\n\n2026-09-21T14:13:20Z Stopped: Ctrl-C\n\nWorker: m\n") class CopyFor(unittest.TestCase): def test_skips_a_version_already_kept_beside_its_copy(self): with tempfile.TemporaryDirectory() as temp: copy = pathlib.Path(temp, "custom-title.json") copy.write_bytes(b"first") beside = write.copy_for(copy, b"second") self.assertEqual(beside, copy.with_name(f"custom-title.json.{hashlib.sha256(b'second').hexdigest()}")) beside.write_bytes(b"second") self.assertIsNone(write.copy_for(copy, b"second")) self.assertEqual(write.copy_for(copy, b"first, grown"), copy) class Outcomes(unittest.TestCase): def test_the_archive_stamps_what_it_reads_back(self): self.assertRegex(f"{utc(NOW)} {outcomes.finished('m', 'merged', 'j')}", outcomes.FINISHED_AT) def test_reads_back_the_worker_of_each_outcome_that_names_one(self): kept = "Task: Do it.\n\n" + "\n".join(f"{utc(NOW + offset)} {outcome}\n" for offset, outcome in enumerate([ outcomes.unfinished("w1", "Worker: not this", "j", ""), outcomes.interrupted("Which one?\nWorker: nor this", "e", "j", "", "w1b"), outcomes.stopped("w2", "the judge failed\nthree times"), outcomes.finished("w3", "merged", "j"), ])) self.assertEqual(outcomes.workers(kept), [(utc(NOW), "w1"), (utc(NOW + 1), "w1b"), (utc(NOW + 2), "w2"), (utc(NOW + 3), "w3")]) def test_archives_the_escalation_an_unfinished_verdict_quotes_beside_its_evidence(self): self.assertEqual(outcomes.unfinished("w", "e", "j", "round 2: 🔴"), "Verdict: unfinished\n\nEvidence:\n> e\n\nEscalation:\n> round 2: 🔴\n\nJudge: j\nWorker: w") self.assertEqual(outcomes.unfinished("w", "e", "j", ""), "Verdict: unfinished\n\nEvidence:\n> e\n\nJudge: j\nWorker: w") def test_a_recorded_judge_cost_follows_the_judge_line_and_reads_back_with_its_rung(self): self.assertEqual(outcomes.finished("w", "e", "j", judge_cost=0.12345), "Verdict: finished\n\nEvidence:\n> e\n\nJudge: j\nJudge cost: 0.1235\nWorker: w") self.assertEqual(outcomes.rejected("why", "{}", "j", judge_cost=2.0), "Judge reply rejected: why\n\nReply:\n> {}\n\nJudge: j\nJudge cost: 2.0000") kept = f"{utc(NOW)} " + outcomes.unfinished("w", "e", "j", "", judge_cost=0.5) + "\n" + f"{utc(NOW)} " + outcomes.finished("w", "e", "k") + "\n" + f"{utc(NOW)} " + outcomes.rejected("why", "{}", "k", judge_cost=0.25) self.assertEqual(outcomes.judge_costs(kept), [("j", 0.5), ("k", 0.25)]) self.assertEqual(outcomes.workers(f"{utc(NOW)} " + outcomes.finished("w", "e", "j", judge_cost=0.5)), [(utc(NOW), "w")]) def test_archives_the_escalation_an_interrupt_quotes_beside_its_evidence(self): self.assertEqual(outcomes.interrupted("q?", "e", "j", "round 2: 🔴", "w"), "Verdict: interrupt\n\nq?\n\nEvidence:\n> e\n\nEscalation:\n> round 2: 🔴\n\nJudge: j\nWorker: w") self.assertEqual(outcomes.interrupted("q?", "e", "j", "", "w"), "Verdict: interrupt\n\nq?\n\nEvidence:\n> e\n\nJudge: j\nWorker: w") def test_archives_the_escalation_and_the_worker_a_quoted_answer_names(self): self.assertEqual(outcomes.answered("q?", "AGENTS.md", "A.", "e", "round 2: 🔴", "j", "w"), "Verdict: quoted answer\n\nq?\n\nAGENTS.md says:\n> A.\n\nEvidence:\n> e\n\nEscalation:\n> round 2: 🔴\n\nJudge: j\nWorker: w") self.assertEqual(outcomes.answered("q?", "AGENTS.md", "A.", "e", "", "j", "w"), "Verdict: quoted answer\n\nq?\n\nAGENTS.md says:\n> A.\n\nEvidence:\n> e\n\nJudge: j\nWorker: w") self.assertEqual(outcomes.workers(f"{utc(NOW)} {outcomes.answered('q?', 'AGENTS.md', 'A.', 'e', '', 'j', 'w')}\n"), [(utc(NOW), "w")]) def test_reads_no_worker_from_an_outcome_written_before_the_worker_line(self): kept = f"Task: Do it.\n\n{utc(NOW)} Stopped: Ctrl-C\n\n{utc(NOW)} Verdict: unfinished\n\nEvidence:\n> Worker: no\n" self.assertEqual(outcomes.workers(kept), []) self.assertEqual(outcomes.workers(f"{utc(NOW)} {outcomes.stepped_up('w')}\n"), [(utc(NOW), "w")]) self.assertEqual(outcomes.workers(f"{utc(NOW)} Verdict: quoted answer\n\nq?\n\nAGENTS.md says:\n> A.\n\nEvidence:\n> e\n\nJudge: j\n"), []) def test_takes_the_outcome_written_last_within_one_second(self): with tempfile.TemporaryDirectory() as root: kept = pathlib.Path(root, "archive") transcript = pathlib.Path(root, "t.jsonl") transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') with contextlib.redirect_stderr(io.StringIO()): for worker in ("z-first", "a-last"): self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working", "j", ""), NOW)) self.assertEqual(read.latest_worker(kept, str(transcript)), "a-last") def test_names_the_worker_the_latest_outcome_of_the_transcripts_chunk_records(self): with tempfile.TemporaryDirectory() as root: kept = pathlib.Path(root, "archive") transcript = pathlib.Path(root, "t.jsonl") transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') self.assertIsNone(read.latest_worker(kept, str(transcript))) with contextlib.redirect_stderr(io.StringIO()): self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w1", "working", "j", ""), NOW)) transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n{"more": 1}\n') self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w2", "Ctrl-C"), NOW + 1)) self.assertEqual(read.latest_worker(kept, str(transcript)), "w2") self.assertIsInstance(read.latest_worker(kept, str(pathlib.Path(root, "gone.jsonl"))), Failure) class Location(unittest.TestCase): def test_keys_a_project_by_its_path_under_code_or_else_under_the_root(self): home = pathlib.Path("/h") self.assertEqual(project_key("/h/code/log", home), "log") self.assertEqual(project_key("/h/code", home), "h/code") self.assertEqual(project_key("/srv/log", home), "srv/log") self.assertEqual(project_archive("/h/code/log", home, {}), pathlib.Path("/h/.claude/loop-claude-transcripts/log")) self.assertEqual(project_archive("/h/code/log", home, {"LOOP_CLAUDE_TRANSCRIPTS": "/a"}), pathlib.Path("/a/log")) def test_finds_the_checkout_a_key_names_under_code_or_else_under_the_root(self): with tempfile.TemporaryDirectory() as home: (pathlib.Path(home) / "code" / "log").mkdir(parents=True) self.assertEqual(checkout_of("log", pathlib.Path(home)), f"{home}/code/log") self.assertEqual(checkout_of("home/me/.claude", pathlib.Path(home)), "/home/me/.claude") if __name__ == "__main__": unittest.main()