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

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
+5 -4
View File
@@ -5,10 +5,11 @@ import dataclasses
import json
import pathlib
import re
import typing
from routing_figures.calls import Call, SENTENCES
from failure import Failure
from text_files.file_writes import replace_file, replace_json
from text_files.file_writes import replace_json
from text_files.utc_stamp import STAMPED
CYCLES_PER_CHECK = 20
@@ -100,8 +101,8 @@ def raised(root: pathlib.Path) -> Raised | Failure:
return Raised(since, frozenset(entry.call for entry in kept.raised if entry.at > since))
def mark_raised(root: pathlib.Path, calls: list[Call], at: str) -> Failure | None:
"""Keeps the calls in STATE, and adds a line for each to RAISED."""
def mark_raised(root: pathlib.Path, calls: list[Call], at: str, replace: typing.Callable[[pathlib.Path, bytes], Failure | None]) -> Failure | None:
"""Keeps the calls in STATE, and adds a line for each to RAISED, which replace writes as replace_file does."""
kept = read_state(root)
if isinstance(kept, Failure):
return kept
@@ -112,7 +113,7 @@ def mark_raised(root: pathlib.Path, calls: list[Call], at: str) -> Failure | Non
return unwritten
# The first line opens a list below the heading; each later one joins it.
gap = "\n" if RAISED_LINE.search(text) else "\n\n"
unwritten = replace_file(root / RAISED, (text.rstrip("\n") + gap + "".join(f"- {at} {call.sentence()}\n" for call in calls)).encode())
unwritten = replace(root / RAISED, (text.rstrip("\n") + gap + "".join(f"- {at} {call.sentence()}\n" for call in calls)).encode())
return Failure(f"could not write {root / RAISED}: {unwritten.reason}") if unwritten else None
+18 -25
View File
@@ -10,7 +10,6 @@ import subprocess
import sys
import tempfile
import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -20,7 +19,7 @@ from project_config.templates import BARE, Titles
from branch_chunks.cut import BranchChunk
from routing_figures.calls import Call, calls, totals
from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Reports, Subagent
from text_files import compressed
from text_files.doubles import compress
from project_config.doubles import configured, write_config
from routing_figures.state import due
from claude_code.entries import entry_from
@@ -30,6 +29,7 @@ from plan.todo_table import Item
from transcript_archive import outcomes, write
from model_routing.ladder_file import read_ladder, SHIPPED
from routing_figures.render import render
from text_files.file_writes import replace_file
from model_routing.route import Route
from model_routing.ladder import Rung
@@ -356,7 +356,7 @@ class Archive(unittest.TestCase):
def test_measures_a_chunk_whose_transcripts_are_compressed(self):
self.chunk("home/x", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z", call("Agent", "a1", subagent_type="Explore"))], ({"agentType": "Explore", "toolUseId": "a1"}, [message("s1", "2026-10-14T09:00:01Z")]))
for copy in self.root.rglob("*.jsonl"):
self.assertIsNone(compressed.compress(copy))
self.assertIsNone(compress(copy))
found = archive.measure_archive(self.root, "/h", "", no_checkout)
self.assertEqual(found.chunks[0].dollars["subagent type"], {"worker": 2.0, "Explore": 2.0})
@@ -365,8 +365,7 @@ class Archive(unittest.TestCase):
env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(home)}
self.ship_then_fix(home / "code" / "log", env)
self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")])
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
self.assertEqual([(chunk.dollars["project"].keys(), chunk.fixed_later) for chunk in found.chunks], [({"log": 0}.keys(), 1)])
def test_reads_a_projects_round_titles_and_its_branchs_history_through_its_configs_templates(self):
@@ -377,10 +376,9 @@ class Archive(unittest.TestCase):
config = write_config(home, home / "code" / "log") / "config.toml"
worker = [message(f"m{n}", "2026-10-14T09:00:00Z", call("Bash", command=f"git commit -m '{round_title.format(n)}'")) for n in (1, 2)]
self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), worker)
with unittest.mock.patch.dict(os.environ, env, clear=True):
bare = archive.measure_archive(self.root, str(home), "", git_repo.git)
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
templated = archive.measure_archive(self.root, str(home), "", git_repo.git)
bare = archive.measure_archive(self.root, str(home), "", git_repo.git)
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
templated = archive.measure_archive(self.root, str(home), "", git_repo.git)
self.assertEqual([(chunk.rounds_past_first, chunk.fixed_later) for chunk in bare.chunks], [(0, 0)])
self.assertEqual([(chunk.rounds_past_first, chunk.fixed_later) for chunk in templated.chunks], [(1, 1)])
@@ -391,8 +389,7 @@ class Archive(unittest.TestCase):
config = write_config(home, home / "code" / "log") / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n')
self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")])
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
self.assertEqual(found.chunks, [])
self.assertEqual(len(found.unread), 1)
self.assertRegex(found.unread[0], r"^log: none of its chunks is measured: .*`commit-title` is 'PAY-\{id\}'")
@@ -407,8 +404,7 @@ class Archive(unittest.TestCase):
def recording(*args, **options):
made.append((args[0], options))
return git_repo.git(*args, **options)
with unittest.mock.patch.dict(os.environ, env, clear=True):
archive.measure_archive(self.root, str(home), "", recording)
archive.measure_archive(self.root, str(home), "", recording)
self.assertTrue(all(options.get("guarded") is True for _, options in made))
read = [options.get("timeout") for command, options in made if command in ("log", "ls-tree", "show", "diff", "blame")]
self.assertEqual(set(read), {archive.GIT_TIMEOUT})
@@ -420,8 +416,7 @@ class Archive(unittest.TestCase):
(home / "code" / "log").mkdir(parents=True)
subprocess.run(["git", "init", "-q"], cwd=home / "code" / "log", check=True, env=env, capture_output=True)
self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")])
with unittest.mock.patch.dict(os.environ, env, clear=True):
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
found = archive.measure_archive(self.root, str(home), "", git_repo.git)
self.assertEqual(found.unread, ("log: no origin remote, and no main or master branch to take as the default branch",))
def test_names_each_project_whose_history_it_cannot_read(self):
@@ -458,9 +453,9 @@ class Archive(unittest.TestCase):
first = message("m1", "2026-09-21T00:00:00Z")
transcript.write_text(json.dumps(first) + "\n")
with contextlib.redirect_stderr(io.StringIO()):
self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.unfinished(LOW_ROUTE, "e", "j", ""), 1790000000))
self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.unfinished(LOW_ROUTE, "e", "j", ""), 1790000000, replace=replace_file))
transcript.write_text(json.dumps(first) + "\n" + json.dumps(message("m2", "2099-01-01T00:00:00Z")) + "\n")
self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.finished(Route(HIGH).worker_line(), "m", "j"), 4070908800))
self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.finished(Route(HIGH).worker_line(), "m", "j"), 4070908800, replace=replace_file))
(found,) = archive.measure_archive(kept, "/h", "", no_checkout).chunks
self.assertEqual((found.start, found.unfinished), (LOW, 1))
self.assertEqual(found.dollars["rung"], {str(LOW): 2.0, str(HIGH): 2.0})
@@ -485,14 +480,14 @@ class Archive(unittest.TestCase):
def test_reads_the_calls_raised_since_the_latest_reset(self):
a, b, c = Call("reports", ("Explore",)), Call("rounds", ("x", "y")), Call("unfinished", ("x", "y"))
self.assertEqual(state.raised(self.root), state.Raised("", frozenset()))
self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T00:00:00Z"))
self.assertIsNone(state.mark_raised(self.root, [b, c], "2026-10-15T01:00:00Z"))
self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T00:00:00Z", replace_file))
self.assertIsNone(state.mark_raised(self.root, [b, c], "2026-10-15T01:00:00Z", replace_file))
self.assertEqual((self.root / state.RAISED).read_text(), f"# Routing calls raised\n\n- 2026-10-15T00:00:00Z {a.sentence()}\n- 2026-10-15T01:00:00Z {b.sentence()}\n- 2026-10-15T01:00:00Z {c.sentence()}\n")
self.assertEqual(state.raised(self.root), state.Raised("", frozenset({a, b, c})))
with open(self.root / state.RAISED, "a") as handle:
handle.write("- 2026-10-15T01:00:00Z Reset\n")
self.assertEqual(state.raised(self.root), state.Raised("2026-10-15T01:00:00Z", frozenset()), "a call stamped at the reset's second was raised before it")
self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T02:00:00Z"))
self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T02:00:00Z", replace_file))
self.assertEqual(state.raised(self.root), state.Raised("2026-10-15T01:00:00Z", frozenset({a})))
def test_fails_when_it_cannot_keep_the_state(self):
@@ -500,17 +495,15 @@ class Archive(unittest.TestCase):
self.root.chmod(0o555)
self.addCleanup(self.root.chmod, 0o755)
self.assertIsInstance(state.mark_measured(self.root, 40), Failure)
self.assertIsInstance(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z"), Failure)
self.assertIsInstance(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z", replace_file), Failure)
def test_fails_when_it_cannot_list_a_raised_call_for_the_human(self):
self.assertIsNone(state.mark_measured(self.root, 40))
written = state.replace_file
with unittest.mock.patch.object(state, "replace_file", lambda path, data: Failure("full") if path.name == state.RAISED else written(path, data)):
self.assertEqual(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z"), Failure(f"could not write {self.root / state.RAISED}: full"))
self.assertEqual(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z", replace=lambda path, data: Failure("full")), Failure(f"could not write {self.root / state.RAISED}: full"))
def test_knows_a_raised_call_by_its_fields_whatever_its_line_reads(self):
call = Call("reports", ("Explore",))
self.assertIsNone(state.mark_raised(self.root, [call], "2026-10-15T00:00:00Z"))
self.assertIsNone(state.mark_raised(self.root, [call], "2026-10-15T00:00:00Z", replace_file))
(self.root / state.RAISED).write_text("# Routing calls raised\n\n- 2026-10-15T00:00:00Z Reworded.\n")
self.assertEqual(state.raised(self.root).calls, frozenset({call}))