Answer the principles audit: keep every session file under session/, and stop on a folder the archive cannot read

This commit is contained in:
2026-10-02 17:10:03 +02:00
parent 6ccd5b190f
commit 4620d2e29b
8 changed files with 76 additions and 28 deletions
+6 -2
View File
@@ -35,14 +35,18 @@ def session_open(session: Session) -> bool:
return transcript_file(session) is not None
# <session id>.jsonl, <session id>/subagents/, and from older Claude Code versions <session id>.superseded-1789290815002/ and <session id>.orphaned-*.jsonl
def session_files(transcript: str) -> list[str]:
return sorted(glob.glob(glob.escape(transcript.removesuffix(".jsonl")) + "*"))
def delete_session(session: Session) -> Failure | None:
path = transcript_file(session)
if path is None:
return None
stem = path.removesuffix(".jsonl")
# <session id>.jsonl, <session id>/subagents/, and from older versions <session id>.superseded-1789290815002/ and <session id>.orphaned-*.jsonl
# The transcript goes last: a failed delete leaves a session that session_open still sees, never a new session with this id beside the old one's files.
for sibling in sorted(glob.glob(glob.escape(stem) + "*"), key=lambda sibling: sibling == path):
for sibling in sorted(session_files(path), key=lambda sibling: sibling == path):
try:
shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling)
except OSError as failure:
+29 -13
View File
@@ -1,7 +1,9 @@
import hashlib
import os
import pathlib
import sys
from loop_claude.claude_code.session import session_files
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
@@ -12,52 +14,66 @@ def archive(project_key: str, path: str, task: str, outcome: str, paths: Paths)
try:
with open(path, "rb") as handle:
raw = handle.read()
# Each chunk's first timestamped line is its own: a finished chunk's session is deleted, so the next chunk's transcript starts afresh.
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.
if unkept := mirror(pathlib.Path(path.removesuffix(".jsonl")), folder):
return unkept
return unarchived(failure.filename, failure.strerror)
for kept in session_files(path):
if kept != path and (unkept := mirror(pathlib.Path(kept), folder / "session" / os.path.basename(kept))):
return unkept
copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl"
verdict = copy.with_suffix(".verdict.md")
if copy.exists():
return add_outcome(verdict, outcome)
# The copy lands last, so a copy on disk always has its verdict and its session folder beside it.
# The copy lands last, so a copy on disk always has its verdict and its session's files 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}")
return unarchived(dest, unwritten.reason)
print(f"transcript: {copy}", file=sys.stderr)
return None
def unarchived(where: object, why: str | None) -> Failure:
return Failure(f"could not archive the transcript: {where}: {why}")
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}")
return unarchived(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 unarchived(verdict, unwritten.reason)
return None
def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
for file in sorted(source.rglob("*")):
try:
files = [source] if source.is_file() else files_under(source)
except OSError as failure:
return unarchived(failure.filename, failure.strerror)
for file in files:
try:
if not file.is_file():
continue
data = file.read_bytes()
copy = copy_for(dest / file.relative_to(source), data)
if copy is None:
continue
make_private(copy.parent)
except OSError as failure:
return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}")
return unarchived(failure.filename, failure.strerror)
if unwritten := replace_file(copy, data):
return Failure(f"could not archive the transcript: {copy}: {unwritten.reason}")
return unarchived(copy, unwritten.reason)
return None
# Path.rglob skips a folder it cannot read; os.walk with onerror reports it.
def files_under(folder: pathlib.Path) -> list[pathlib.Path]:
def fail(error: OSError) -> None:
raise error
return sorted(pathlib.Path(root, name) for root, _, names in os.walk(folder, onerror=fail) for name in names)
# 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():
+2 -2
View File
@@ -45,7 +45,7 @@ def run_cycle(cycle: Cycle) -> Stop | None:
def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
path = transcript_file(cycle.session)
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.
# A failed archive exits 1 whatever stopped the loop; one that is the stop itself fails the same way again, so say it once.
return Stop(1, f"{failure.reason}\n{stop.reason}".rstrip()) if failure and failure.reason != stop.reason else stop
@@ -80,7 +80,7 @@ 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.
# Archived now, so the transcript the question was asked on is kept with it; the human's answer then grows the transcript.
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()
+2 -2
View File
@@ -17,8 +17,8 @@ copied to $LOOP_CLAUDE_TRANSCRIPTS/<key>/<chunk>/<sha256>.jsonl (default
~/.claude/loop-claude-transcripts), named by its hash. <chunk> is the hash of the transcript's first
line with a timestamp, the same for every cycle of a chunk. Beside the copy, <sha256>.verdict.md
holds the verdict, and why the loop stopped when it did. Claude Code keeps a folder per session
beside its transcript, holding the subagents' transcripts among other files; those files are copied
into <chunk>/. A file there is replaced only by a version that grew from it. A file Claude Code
beside its transcript, holding the subagents' transcripts among other files; it and every other file
named after the session are copied into <chunk>/session/. A file there is replaced only by a version that grew from it. A file Claude Code
rewrote lands beside its copy as <name>.<hash of its content>.
Every claude loop-claude starts runs the project's model, saved per <key> in