Archive the raw transcript by content hash with its verdict, and stop on a failed archive

This commit is contained in:
2026-09-28 16:57:24 +02:00
parent 5e3b66232b
commit 6e31e0a833
7 changed files with 75 additions and 41 deletions
+33 -5
View File
@@ -1,4 +1,5 @@
import dataclasses
import hashlib
import json
import os
import pathlib
@@ -229,8 +230,19 @@ class Checkout:
def transcripts(self):
return sorted((self.home / ".claude" / "projects").glob("*/*.jsonl"))
def archive(self):
return self.home / ".claude" / "loop-claude-transcripts"
def archived(self):
return sorted((self.home / ".claude" / "loop-claude-transcripts" / "proj").glob("*"))
"""Each archived transcript's verdict, after checking the copy is named by its content hash."""
verdicts = {}
for copy in sorted((self.archive() / "proj").glob("*.jsonl")):
self.test.assertEqual(copy.stem, hashlib.sha256(copy.read_bytes()).hexdigest())
verdict = copy.with_suffix(".verdict.md")
for archived in (copy, verdict):
self.test.assertEqual(stat.S_IMODE(archived.stat().st_mode), 0o600)
verdicts[copy.read_text()] = verdict.read_text()
return verdicts
def argument(call, flag):
@@ -292,9 +304,22 @@ class Cycles(unittest.TestCase):
self.assertIn("landed-it", run.calls[3]["stdin"])
self.assertNotIn("landed-it", run.calls[5]["stdin"])
self.assertIn("=== cycle 3 ===", run.stdout)
self.assertTrue(checkout.archived())
for archived in checkout.archived():
self.assertEqual(stat.S_IMODE(archived.stat().st_mode), 0o600)
[(copy, verdict)] = checkout.archived().items()
self.assertIn('"text": "landed-it"', copy)
self.assertEqual(verdict, f"Task: {TASK}\n\nVerdict: finished\n")
def test_exits_1_when_the_transcript_cannot_be_archived_and_resumes_on_the_next_run(self):
checkout = Checkout(self)
checkout.archive().parent.mkdir(parents=True)
checkout.archive().write_text("in the way")
run = checkout.run([worker(say="landed-it"), judge(finished=True, evidence="landed-it")])
self.assertEqual((run.code, run.roles()), (1, ["worker", "judge"]))
self.assertRegex(run.stderr.strip().splitlines()[-1], r"^could not archive the transcript .*\.jsonl: ")
session = argument(run.calls[0], "--session-id")
checkout.archive().unlink()
run = checkout.run([worker(), judge(finished=True)])
self.assertEqual(argument(run.calls[0], "--resume"), session)
self.assertEqual(len(checkout.archived()), 1)
def test_stops_after_three_failures_running_retrying_a_minute_apart(self):
checkout = Checkout(self)
@@ -377,11 +402,14 @@ class Interrupt(unittest.TestCase):
run = checkout.run(steps)
self.assertEqual((run.code, run.roles()), (2, ["worker", "judge"]))
self.assertEqual(run.stderr.strip().splitlines()[-2:], ["A or B?", "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal"])
self.assertTrue(checkout.archived())
[(copy, verdict)] = checkout.archived().items()
self.assertEqual(copy, (checkout.transcripts()[0]).read_text())
self.assertEqual(verdict, f"Task: {TASK}\n\nVerdict: interrupt\n\nA or B?\n")
session = argument(run.calls[0], "--session-id")
run = checkout.run(steps, terminal_input="", stdout_on_terminal=False)
self.assertEqual((run.code, run.roles()), (2, ["worker", "judge"]))
self.assertEqual(argument(run.calls[0], "--resume"), session)
self.assertEqual(len(checkout.archived()), 2)
self.assertEqual(run.stderr.strip().splitlines()[-1], "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal")
def test_hands_the_terminal_over_and_exits_2_when_nothing_is_typed(self):