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
@@ -14,6 +14,12 @@ def read_transcript(path: str) -> list[dict] | Failure:
return Failure(f"could not read the transcript {path}: {failure.strerror}")
# The lines before it, such as the session's title, are the same for every chunk of a project.
def first_timestamped_line(raw: bytes) -> bytes:
lines = raw.splitlines()
return next((line for line in lines if "timestamp" in (parse_entry(line) or {})), lines[0] if lines else b"")
def human_turns(entries: list[dict]) -> int:
return sum(1 for entry in entries if human_text(entry) is not None)
+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)
+7 -6
View File
@@ -13,12 +13,13 @@ Cycles continue until an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "V
Each project has one session, named "A <key> loop-claude", where <key> is the checkout's path
relative to ~/code, or to / outside it. The session is deleted when its chunk finishes. At every
interrupt, and as every cycle ends, Ctrl-C and a failure included, the session's transcript is
copied to $LOOP_CLAUDE_TRANSCRIPTS/<key>/<sha256>.jsonl (default ~/.claude/loop-claude-transcripts),
named by its hash. Beside it, <sha256>.verdict.md holds the verdict, or why the loop stopped.
Claude Code keeps a folder per session beside its transcript, holding the subagents' transcripts
among other files; those files are copied to <key>/<session id>/. A file there is replaced only by
a version that grew from it. A file with other content under the same name, such as one an earlier
chunk left, lands beside it as <name>.<hash of its content>.
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
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
~/.claude/loop-claude-models.json and read each cycle; a project without an entry, as on its first