import hashlib import sys from loop_claude.file_writes import make_private, replace_file from loop_claude.outcome import Failure def archive(project, path, note, paths) -> Failure | None: folder = paths.archive / project try: with open(path, "rb") as handle: raw = handle.read() make_private(folder) except OSError as failure: return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}") copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl" # The copy lands last, so a copy on disk always has its verdict beside it. if not copy.exists(): for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\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