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
+49 -14
View File
@@ -1,11 +1,12 @@
import contextlib
import dataclasses
import io
import json
import pathlib
import subprocess
import sys
import tempfile
import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
@@ -13,11 +14,18 @@ from loop_effects.doubles import assistant, FakeEffects
from loop_claude.loop import chunk_windows
from project_config.doubles import configured
from failure import Failure
from git_cli import git_repo
from test_env import isolated_env
from loop_claude.loop import main
from release_plan import next_chunk
from loop_state import paths
from agent_cycle import tmux
from touch_predictions.doubles import plan as touch_plan
from touch_predictions.doubles import todo, plan as touch_plan
def plan_text(*touched):
"""A todo.md of items 1, 2 and on, each predicted to touch the path given for it."""
return todo(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1)))
class ChunkWindows(unittest.TestCase):
@@ -76,8 +84,8 @@ class ChunkWindows(unittest.TestCase):
self.assertIsNone(chunk_windows.item_of("item-\u0663"))
def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self):
with unittest.mock.patch.object(chunk_windows, "chunks_beside", return_value="item 1's Touches: list cannot be read"):
self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"])
items = touch_plan((1, "1.0.0", "", "- a.py"), (2, "1.0.0", "", "- `c.py`"))
self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=items, first=items[0], text="", branch_template=""), [], set(), 3), ["item-1"])
def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self):
with tempfile.TemporaryDirectory() as temp:
@@ -113,7 +121,7 @@ class StartChunks(unittest.TestCase):
self.home = configured(self, self.root).parents[1]
self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-")
# Items 1 and 2 overlap, so 2 waits; 3 runs beside 1.
self.plan = ChunkWindows.plan(self, "a.py", "a.py", "c.py")
self.plan = plan_text("a.py", "a.py", "c.py")
def limit(self, chunks):
config = self.home / ".claude-projects" / self.root.name / "config.toml"
@@ -123,15 +131,31 @@ class StartChunks(unittest.TestCase):
"""start_chunks' answer, for a session whose @loop-root names owner, by default this checkout."""
fake.owner = str(self.root) if owner is None else owner
run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk)
with unittest.mock.patch.object(chunk_windows, "next_plan", return_value=plan or self.plan) as planned, contextlib.redirect_stderr(io.StringIO()) as said:
effects = fake.effects()
ran = []
effects = dataclasses.replace(fake.effects(), git=self.git_reading(self.plan if plan is None else plan, ran))
with contextlib.redirect_stderr(io.StringIO()) as said:
started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects)
self.said = said.getvalue()
self.planned = planned.call_count
if planned.call_count:
planned.assert_called_once_with(str(self.root), self.home, effects)
fetches = [args for args in ran if args[0] == "fetch"]
self.planned = len(fetches)
self.assertIn(fetches, ([], [["fetch", "--quiet", "origin"]]))
return started
def git_reading(self, plan, ran):
"""A git that notes each call in ran, and reads plan, a todo.md's text, on the checkout's default branch; a Failure answers every call instead, and an error is raised by every call."""
if isinstance(plan, str):
(self.root / "todo.md").write_text(plan)
for command in (["add", "todo.md"], ["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "--quiet", "--allow-empty", "-m", "plan"]):
subprocess.run(["git", "-C", str(self.root), *command], check=True, env=isolated_env())
def git(*args, **options):
ran.append(list(args))
if isinstance(plan, BaseException):
raise plan
return plan if isinstance(plan, Failure) else git_repo.git(*args, **options, env=isolated_env())
return git
def starts(self, fake):
"""The window starts of fake's tmux calls, each as (command, window name, what it runs)."""
return [(call[0], call[call.index("-n") + 1], call[call.index("--") + 1:]) for call in fake.tmux_calls if call[0] in ("new-session", "new-window")]
@@ -204,7 +228,7 @@ class StartChunks(unittest.TestCase):
def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self):
self.limit(2)
fake = FakeEffects(windows="item-5\t1\t@3\n")
self.assertEqual(self.start(fake, plan=next_chunk.NoItem("release 1.0.0 has no items")), ["item-5"])
self.assertEqual(self.start(fake, plan=plan_text()), ["item-5"])
def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self):
self.limit(2)
@@ -218,7 +242,7 @@ class StartChunks(unittest.TestCase):
self.assertEqual(self.start(fake, chunk="item-7", leaving=True), ["item-3"])
self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], [])
fake = FakeEffects(windows="item-7\t0\t@1\nno-item\t1\t@3\n")
self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=next_chunk.NoItem("release 1.0.0 has no items")), [])
self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=plan_text()), [])
def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self):
fake = FakeEffects()
@@ -226,8 +250,9 @@ class StartChunks(unittest.TestCase):
self.assertIn("starting a chunk without a plan item, since the plan cannot be read: no origin/main", self.said)
def test_says_why_a_window_could_not_start(self):
with unittest.mock.patch.object(tmux, "start_window", return_value=Failure("tmux could not start the window")):
self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window"))
fake = FakeEffects()
fake.tmux = lambda args: subprocess.CompletedProcess(args, 1, "", "no room") if args[0] == "new-session" else FakeEffects.tmux(fake, args)
self.assertEqual(self.start(fake), Failure(f"tmux could not start the window {self.session}:item-1: no room"))
def test_says_why_it_cannot_take_the_lock_on_starting_windows(self):
(self.home / ".local" / "state").mkdir(parents=True)
@@ -236,6 +261,16 @@ class StartChunks(unittest.TestCase):
self.assertIsInstance(started, Failure)
self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason)
def test_an_error_raised_under_the_lock_is_not_reported_as_the_locks(self):
with self.assertRaisesRegex(OSError, "the plan's disk is gone"):
self.start(FakeEffects(), plan=OSError("the plan's disk is gone"))
def test_a_chunk_reads_the_other_windows_where_it_cannot_take_the_lock(self):
(self.home / ".local" / "state").mkdir(parents=True)
(self.home / ".local" / "state" / "loop-claude").write_text("")
fake = FakeEffects(windows="item-7\t0\t@1\nitem-9\t1\t@3\n")
self.assertEqual(chunk_windows.held_beside("item-7", str(self.root), paths.paths_from(self.home), fake.effects()), [9])
def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self):
with tempfile.TemporaryDirectory() as temp:
transcripts = pathlib.Path(temp)