Files
claude-config/bin/transcript_archive/write.py
T

88 lines
3.5 KiB
Python

import hashlib
import os
import pathlib
import typing
from transcript_archive.location import chunk_folder, outcomes_of
from failure import Failure
from text_files.file_writes import make_private, replace_file
from claude_code.session import session_files
from transcript_archive.outcomes import TASK
from text_files.utc_stamp import utc
def archive(project_archive: pathlib.Path, path: str, task: str, outcome: str, at: float, note: typing.Callable[[str], None] = lambda text: None) -> Failure | None:
"""Keeps the transcript at path, its outcomes file and its session's files in project_archive, in the layout this package's docstring gives; the failure that stopped it, or None. The outcome is stamped and appended to the outcomes file, which opens with the task only when the copy is new. Where a new copy lands, note is told where."""
try:
with open(path, "rb") as handle:
raw = handle.read()
folder = chunk_folder(project_archive, raw)
make_private(folder)
except OSError as failure:
return unarchived(failure.filename, failure.strerror)
for sibling in session_files(path):
if sibling != path and (unkept := mirror(pathlib.Path(sibling), folder / "session" / os.path.basename(sibling))):
return unkept
copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl"
outcomes = outcomes_of(copy)
stamped = f"{utc(at)} {outcome}"
if copy.exists() and outcomes.exists():
return add_outcome(outcomes, stamped)
# The copy lands last, so a copy on disk always has its outcomes and its session's files beside it.
for dest, data in ((outcomes, f"{TASK}{task}\n\n{stamped}\n".encode()), (copy, raw)):
if unwritten := replace_file(dest, data):
return unarchived(dest, unwritten.reason)
note(f"transcript: {copy}")
return None
def unarchived(where: object, why: str | None) -> Failure:
return Failure(f"could not archive the transcript: {where}: {why}")
def add_outcome(outcomes: pathlib.Path, outcome: str) -> Failure | None:
try:
kept = outcomes.read_bytes()
except OSError as failure:
return unarchived(failure.filename, failure.strerror)
if unwritten := replace_file(outcomes, kept + f"\n{outcome}\n".encode()):
return unarchived(outcomes, unwritten.reason)
return None
def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
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:
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 unarchived(failure.filename, failure.strerror)
if unwritten := replace_file(copy, data):
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():
return copy
kept = copy.read_bytes()
if data.startswith(kept):
return copy if data != kept else None
beside = copy.with_name(f"{copy.name}.{hashlib.sha256(data).hexdigest()}")
return None if beside.exists() else beside