Archive the raw transcript by content hash with its verdict, and stop on a failed archive
This commit is contained in:
+19
-20
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user