Archive each chunk in its own folder, and add a stop to a verdict already archived

This commit is contained in:
2026-10-02 16:40:30 +02:00
parent 236ee08a59
commit aca7594658
8 changed files with 62 additions and 31 deletions
+19 -8
View File
@@ -2,34 +2,45 @@ import hashlib
import pathlib
import sys
from loop_claude.claude_code.transcript import first_timestamped_line
from loop_claude.failure import Failure
from loop_claude.file_writes import make_private, replace_file
from loop_claude.paths import Paths
def archive(project_key: str, path: str, note: str, paths: Paths) -> Failure | None:
folder = paths.transcript_archive / project_key
def archive(project_key: str, path: str, task: str, outcome: str, paths: Paths) -> Failure | None:
try:
with open(path, "rb") as handle:
raw = handle.read()
folder = paths.transcript_archive / project_key / hashlib.sha256(first_timestamped_line(raw)).hexdigest()
make_private(folder)
except OSError as failure:
return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}")
# <session id>/ holds the subagents' transcripts and the tool results too long for the transcript.
session_folder = pathlib.Path(path.removesuffix(".jsonl"))
if unkept := mirror(session_folder, folder / session_folder.name):
if unkept := mirror(pathlib.Path(path.removesuffix(".jsonl")), folder):
return unkept
copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl"
# The copy lands last, so a copy on disk always has its verdict and its session folder beside it.
verdict = copy.with_suffix(".verdict.md")
if copy.exists():
return None
for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\n".encode()), (copy, raw)):
return add_outcome(verdict, outcome)
# The copy lands last, so a copy on disk always has its verdict and its session folder beside it.
for dest, data in ((verdict, f"Task: {task}\n\n{outcome}\n".encode()), (copy, raw)):
if unwritten := replace_file(dest, data):
return Failure(f"could not archive the transcript: {dest}: {unwritten.reason}")
print(f"transcript: {copy}", file=sys.stderr)
return None
def add_outcome(verdict: pathlib.Path, outcome: str) -> Failure | None:
try:
outcomes = verdict.read_bytes()
except OSError as failure:
return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}")
if unwritten := replace_file(verdict, outcomes + f"\n{outcome}\n".encode()):
return Failure(f"could not archive the transcript: {verdict}: {unwritten.reason}")
return None
def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
for file in sorted(source.rglob("*")):
try:
@@ -47,7 +58,7 @@ def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
return None
# Every chunk of a project reuses its session id, so an earlier chunk's file can share a name with this one's.
# Claude Code rewrites some files whole, such as custom-title.json, so a copy is replaced only by a file that grew from it.
def copy_for(copy: pathlib.Path, data: bytes) -> pathlib.Path | None:
if not copy.is_file():
return copy
+6 -6
View File
@@ -35,16 +35,16 @@ def run_cycle(cycle: Cycle) -> Stop | None:
while not isinstance(state, Ended):
event = step(cycle, state)
if isinstance(event, Stop):
return stopped(cycle, event)
return stopped(cycle, event, event.reason)
state = transition(state, event)
except KeyboardInterrupt:
return stopped(cycle, Stop(130, ""))
return stopped(cycle, Stop(130, ""), "Ctrl-C")
return None
def stopped(cycle: Cycle, stop: Stop) -> Stop:
def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
path = transcript_file(cycle.session)
failure = archive(cycle.project_key, path, f"Task: {cycle.task}\n\nStopped: {'Ctrl-C' if stop.code == 130 else stop.reason}", cycle.paths) if path else None
failure = archive(cycle.project_key, path, cycle.task, f"Stopped: {why}", cycle.paths) if path else None
# When the stop is itself a failed archive, archiving again fails the same way: say it once.
return Stop(1, f"{failure.reason}\n{stop.reason}".rstrip()) if failure and failure.reason != stop.reason else stop
@@ -81,14 +81,14 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
# Archived now, so the transcript the question was asked on is kept with it; the interrupt appends the answer.
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: interrupt\n\n{question}", cycle.paths):
if failure := archive(cycle.project_key, transcript_path, cycle.task, f"Verdict: interrupt\n\n{question}", cycle.paths):
return Stop(1, failure.reason)
return interrupt(cycle.root, cycle.session.id, cycle.models.worker, transcript_path, question, cycle.paths, cycle.effects) or Answered()
def close(cycle: Cycle, transcript_path: str, verdict: Finished | Unfinished) -> Archived | Stop:
finished = isinstance(verdict, Finished)
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: {'finished' if finished else 'unfinished'}", cycle.paths):
if failure := archive(cycle.project_key, transcript_path, cycle.task, f"Verdict: {'finished' if finished else 'unfinished'}", cycle.paths):
return Stop(1, failure.reason)
if finished and (failure := delete_session(cycle.session)):
return Stop(1, failure.reason)