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
+19 -20
View File
@@ -1,9 +1,8 @@
import os
import hashlib
import sys
import time
from loop_claude.checkout import project_name
from loop_claude.file_writes import make_private
from loop_claude.file_writes import make_private, replace_file
from loop_claude.interrupt import interrupt
from loop_claude.judge import judge
from loop_claude.outcome import Continue, Failure, Retry, Stop
@@ -20,7 +19,7 @@ def run_cycle(root, task, premises, models, paths):
path = transcript_file(session)
if not path:
return Stop(1, f"claude left no transcript for session {session.id}")
# The interrupt appends to this transcript, so each pass judges and archives it anew; archive skips a stamp it already wrote.
# The interrupt appends to this transcript, so each pass judges and archives it anew.
while True:
entries = read(path)
if isinstance(entries, Failure):
@@ -30,30 +29,30 @@ def run_cycle(root, task, premises, models, paths):
return verdict
if not verdict["interrupt"]:
break
archive(root, path, f"Task: {task}\n\nVerdict: interrupt\n\n{verdict['question']}", paths)
if failure := archive(root, path, f"Task: {task}\n\nVerdict: interrupt\n\n{verdict['question']}", paths):
return Stop(1, failure.reason)
if stop := interrupt(root, session.id, models.worker, path, verdict["question"], paths):
return stop
if verdict["finished"]:
archive(root, path, f"Task: {task}\n\nVerdict: finished", paths)
if failure := archive(root, path, f"Task: {task}\n\nVerdict: finished", paths):
return Stop(1, failure.reason)
delete_session(session)
return Continue()
def archive(root, path, note, paths):
# Stamped by mtime, so a transcript unchanged since its last archive maps to the file already written.
name = project_name(root, paths.code)
folder = paths.archive / name
folder = paths.archive / project_name(root, paths.code)
try:
stamp = time.strftime("%Y-%m-%dT%H-%M-%SZ", time.gmtime(os.path.getmtime(path)))
with open(path, "rb") as handle:
raw = handle.read()
make_private(folder)
dest = folder / f"{stamp}.md"
if not os.path.exists(dest):
entries = read(path)
if isinstance(entries, Failure):
print(f"could not archive the transcript: {entries.reason}", file=sys.stderr)
return
with open(os.open(dest, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as copy:
copy.write(f"# {stamp} {name}\n\n{note}\n\n{render(entries)}\n")
print(f"transcript: {dest}", file=sys.stderr)
except OSError as failure:
print(f"could not archive the transcript: {failure}", file=sys.stderr)
return Failure(f"could not archive the transcript {path}: {failure.strerror}")
copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl"
# The copy lands last, so a copy on disk always has its verdict beside it.
if not copy.exists():
for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\n".encode()), (copy, raw)):
if failure := replace_file(dest, data):
return Failure(f"could not archive the transcript {path}: {failure.strerror}")
print(f"transcript: {copy}", file=sys.stderr)
return None
+6 -2
View File
@@ -11,12 +11,16 @@ def make_private(folder):
def replace_json(path, data, **dump):
return replace_file(path, json.dumps(data, **dump).encode())
def replace_file(path, data):
staged = f"{path}.{os.getpid()}"
try:
mode = os.stat(path).st_mode & 0o7777 if os.path.exists(path) else 0o600
with open(os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as handle:
with open(os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as handle:
os.fchmod(handle.fileno(), mode)
json.dump(data, handle, **dump)
handle.write(data)
os.replace(staged, path)
except OSError as failure:
if os.path.exists(staged):
+7 -7
View File
@@ -11,19 +11,19 @@ its session next cycle; the next chunk starts a fresh session.
Cycles continue until that interrupt or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary".
Exit 1: no task, not inside a checkout, no premises, a models file it cannot read or write, claude
leaves no transcript or one it cannot read, claude cannot start for an interrupt, a reload with
unreadable state, or the worker or the judge fails three times running. A worker fails when claude
cannot start, exits non-zero, is killed, or ends without a result event; a judge when claude cannot
start or exits non-zero, or its verdict misses or mistypes a field, interrupts without a question,
leaves no transcript or one it cannot read or archive, claude cannot start for an interrupt, a
reload with unreadable state, or the worker or the judge fails three times running. A worker fails
when claude cannot start, exits non-zero, is killed, or ends without a result event; a judge when
claude cannot start or exits non-zero, or its verdict misses or mistypes a field, interrupts without a question,
or quotes nothing in the transcript.
Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin or stdout is not a
terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any
open session.
Each project has one session, "A <path> loop-claude", <path> being the checkout relative to ~/code,
or to / outside it, deleted when the chunk finishes. Its transcript is archived under
$LOOP_CLAUDE_TRANSCRIPTS/<path>/ (default ~/.claude/loop-claude-transcripts) at every interrupt and
when the chunk finishes.
or to / outside it, deleted when the chunk finishes. At every interrupt and when the chunk finishes
its transcript is copied to $LOOP_CLAUDE_TRANSCRIPTS/<path>/<sha256>.jsonl (default
~/.claude/loop-claude-transcripts), the verdict beside it in <sha256>.verdict.md.
Every claude it starts runs the project's model, saved per <path> in ~/.claude/loop-claude-models.json
and read each cycle; a project without an entry, as on its first run, is asked for one.