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:
@@ -10,11 +10,12 @@ import unittest
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from transcript_archive.project_key import checkout_of, project_key
|
||||
from text_files import compressed
|
||||
from text_files.doubles import compress
|
||||
from failure import Failure
|
||||
from transcript_archive.judge_run import JudgeRun
|
||||
from transcript_archive import outcomes, read, write
|
||||
from transcript_archive.location import project_archive
|
||||
from text_files.file_writes import replace_file
|
||||
from text_files.utc_stamp import utc
|
||||
|
||||
NOW = 1790000000
|
||||
@@ -27,10 +28,10 @@ class Archive(unittest.TestCase):
|
||||
transcript = pathlib.Path(root, "t.jsonl")
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("m", "working", "j", ""), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("m", "working", "j", ""), NOW, replace=replace_file))
|
||||
(kept_outcomes,) = kept.glob("*/*.outcomes.md")
|
||||
kept_outcomes.unlink()
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("m", "Ctrl-C"), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("m", "Ctrl-C"), NOW, replace=replace_file))
|
||||
self.assertEqual(kept_outcomes.read_text(), "Task: Do it.\n\n2026-09-21T14:13:20Z Stopped: Ctrl-C\n\nWorker: m\n")
|
||||
|
||||
|
||||
@@ -45,7 +46,7 @@ class JudgeRuns(unittest.TestCase):
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
for offset in (0, 1):
|
||||
outcome = outcomes.finished("w", "merged", "j", judge_run=self.judged)
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcome, NOW + offset, judge_run=self.judged))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcome, NOW + offset, judge_run=self.judged, replace=replace_file))
|
||||
(folder,) = kept.glob("*/")
|
||||
(file,) = folder.glob("judge/*")
|
||||
self.assertEqual(file, folder / self.judged.file)
|
||||
@@ -91,7 +92,7 @@ class JudgeRuns(unittest.TestCase):
|
||||
def test_names_the_folder_it_cannot_make_in_the_failure_to_keep_a_run(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
(pathlib.Path(root) / "judge").write_text("a file where the folder should be")
|
||||
failure = write.keep_judge_run(pathlib.Path(root), self.judged)
|
||||
failure = write.keep_judge_run(pathlib.Path(root), self.judged, replace_file)
|
||||
self.assertRegex(failure.reason, f"^could not archive the transcript: {root}/judge: ")
|
||||
self.assertNotIn("None", failure.reason)
|
||||
|
||||
@@ -101,10 +102,10 @@ class JudgeRuns(unittest.TestCase):
|
||||
transcript = pathlib.Path(root, "t.jsonl")
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "x"), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "x"), NOW, replace=replace_file))
|
||||
(folder,) = kept.glob("*/")
|
||||
(folder / "judge").write_text("a file where the folder should be")
|
||||
failure = write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "e", "j", judge_run=self.judged), NOW + 1, judge_run=self.judged)
|
||||
failure = write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "e", "j", judge_run=self.judged), NOW + 1, judge_run=self.judged, replace=replace_file)
|
||||
self.assertIsInstance(failure, Failure)
|
||||
self.assertNotIn("Verdict: finished", next(folder.glob("*.outcomes.md")).read_text())
|
||||
|
||||
@@ -124,7 +125,7 @@ class CopyFor(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
copy = pathlib.Path(temp, "agent-a.jsonl")
|
||||
copy.write_bytes(b"first\n")
|
||||
self.assertIsNone(compressed.compress(copy))
|
||||
self.assertIsNone(compress(copy))
|
||||
self.assertIsNone(write.copy_for(copy, b"first\n"))
|
||||
self.assertEqual(write.copy_for(copy, b"first\nsecond\n"), copy)
|
||||
self.assertEqual(write.copy_for(copy, b"other\n"), copy.with_name(f"agent-a.jsonl.{hashlib.sha256(b'other' + bytes([10])).hexdigest()}"))
|
||||
@@ -141,9 +142,9 @@ class Compressed(unittest.TestCase):
|
||||
subagent.parent.mkdir(parents=True)
|
||||
subagent.write_text('{"sub": 1}\n')
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "merged", "j"), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "merged", "j"), NOW, replace=replace_file))
|
||||
for copy in kept.rglob("*.jsonl"):
|
||||
self.assertIsNone(compressed.compress(copy))
|
||||
self.assertIsNone(compress(copy))
|
||||
return transcript
|
||||
|
||||
def test_a_compressed_copy_takes_the_next_outcome_and_is_not_copied_again(self):
|
||||
@@ -151,7 +152,7 @@ class Compressed(unittest.TestCase):
|
||||
transcript = self.archived(root)
|
||||
kept = pathlib.Path(root, "archive")
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1, replace=replace_file))
|
||||
self.assertEqual([path.name.endswith(".gz") for path in kept.rglob("*.jsonl*")], [True, True])
|
||||
self.assertIn("Stopped: Ctrl-C", next(kept.glob("*/*.outcomes.md")).read_text())
|
||||
|
||||
@@ -208,7 +209,7 @@ class Outcomes(unittest.TestCase):
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
for worker in ("z-first", "a-last"):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working", "j", ""), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working", "j", ""), NOW, replace=replace_file))
|
||||
self.assertEqual(read.latest_worker(kept, str(transcript)), "a-last")
|
||||
|
||||
def test_names_the_worker_the_latest_outcome_of_the_transcripts_chunk_records(self):
|
||||
@@ -218,9 +219,9 @@ class Outcomes(unittest.TestCase):
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
self.assertIsNone(read.latest_worker(kept, str(transcript)))
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w1", "working", "j", ""), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w1", "working", "j", ""), NOW, replace=replace_file))
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n{"more": 1}\n')
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w2", "Ctrl-C"), NOW + 1))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w2", "Ctrl-C"), NOW + 1, replace=replace_file))
|
||||
self.assertEqual(read.latest_worker(kept, str(transcript)), "w2")
|
||||
self.assertIsInstance(read.latest_worker(kept, str(pathlib.Path(root, "gone.jsonl"))), Failure)
|
||||
|
||||
@@ -247,8 +248,8 @@ class Outcomes(unittest.TestCase):
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
self.assertEqual(read.last_spent(kept, str(transcript)), "")
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW, replace=replace_file))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1, replace=replace_file))
|
||||
self.assertEqual(read.last_spent(kept, str(transcript)), utc(NOW))
|
||||
gone = read.last_spent(kept, str(pathlib.Path(root, "gone.jsonl")))
|
||||
self.assertIsInstance(gone, Failure)
|
||||
|
||||
@@ -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