Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -6,15 +6,18 @@ 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.file_writes import make_private
|
||||
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
|
||||
|
||||
# Writes a file whole, as text_files.file_writes.replace_file does.
|
||||
Replace = typing.Callable[[pathlib.Path, bytes], Failure | None]
|
||||
|
||||
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."""
|
||||
|
||||
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, *, replace: Replace) -> 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. replace writes each file, as replace_file does. 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()
|
||||
@@ -23,24 +26,24 @@ def archive(project_archive: pathlib.Path, path: str, task: str, outcome: str, a
|
||||
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))):
|
||||
if sibling != path and (unkept := mirror(pathlib.Path(sibling), folder / "session" / os.path.basename(sibling), replace)):
|
||||
return unkept
|
||||
if judge_run and (unkept := keep_judge_run(folder, judge_run)):
|
||||
if judge_run and (unkept := keep_judge_run(folder, judge_run, replace)):
|
||||
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)
|
||||
return add_outcome(outcomes, stamped, replace)
|
||||
# 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):
|
||||
if unwritten := replace(dest, data):
|
||||
return unarchived(dest, unwritten.reason)
|
||||
note(f"transcript: {copy}")
|
||||
return None
|
||||
|
||||
|
||||
def keep_judge_run(folder: pathlib.Path, run: JudgeRun) -> Failure | None:
|
||||
def keep_judge_run(folder: pathlib.Path, run: JudgeRun, replace: Replace) -> Failure | None:
|
||||
kept = folder / run.file
|
||||
if stored(kept):
|
||||
return None
|
||||
@@ -48,7 +51,7 @@ def keep_judge_run(folder: pathlib.Path, run: JudgeRun) -> Failure | None:
|
||||
make_private(kept.parent)
|
||||
except OSError as failure:
|
||||
return unarchived(failure.filename, failure.strerror)
|
||||
if unwritten := replace_file(kept, run.content()):
|
||||
if unwritten := replace(kept, run.content()):
|
||||
return unarchived(kept, unwritten.reason)
|
||||
return None
|
||||
|
||||
@@ -57,17 +60,17 @@ 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:
|
||||
def add_outcome(outcomes: pathlib.Path, outcome: str, replace: Replace) -> 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()):
|
||||
if unwritten := replace(outcomes, kept + f"\n{outcome}\n".encode()):
|
||||
return unarchived(outcomes, unwritten.reason)
|
||||
return None
|
||||
|
||||
|
||||
def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
|
||||
def mirror(source: pathlib.Path, dest: pathlib.Path, replace: Replace) -> Failure | None:
|
||||
try:
|
||||
files = [source] if source.is_file() else files_under(source)
|
||||
except OSError as failure:
|
||||
@@ -81,7 +84,7 @@ def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
|
||||
make_private(copy.parent)
|
||||
except OSError as failure:
|
||||
return unarchived(failure.filename, failure.strerror)
|
||||
if unwritten := replace_file(copy, data):
|
||||
if unwritten := replace(copy, data):
|
||||
return unarchived(copy, unwritten.reason)
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user