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