import hashlib import os import pathlib import typing from transcript_archive.location import chunk_folder, outcomes_of from failure import Failure from transcript_archive.judge_run import JudgeRun from text_files.file_writes import make_private, replace_file from text_files.compressed import read_stored, stored 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, judge_run: JudgeRun | None = 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. A judge_run the outcome names is kept first, so the outcome never names a run the archive lacks.""" 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 if judge_run and (unkept := keep_judge_run(folder, judge_run)): return unkept copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl" outcomes = outcomes_of(copy) stamped = f"{utc(at)} {outcome}" if stored(copy) 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 keep_judge_run(folder: pathlib.Path, run: JudgeRun) -> Failure | None: kept = folder / run.file if stored(kept): return None try: make_private(kept.parent) except OSError as failure: return unarchived(failure.filename, failure.strerror) if unwritten := replace_file(kept, run.content()): return unarchived(kept, unwritten.reason) 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. # A grown copy lands plain beside its gzip, which compress-archives then replaces without losing anything, since the gzip holds a prefix of it. def copy_for(copy: pathlib.Path, data: bytes) -> pathlib.Path | None: if not stored(copy): return copy kept = read_stored(copy) 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 stored(beside) else beside