import contextlib import dataclasses import io import json import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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 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): def plan(self, *touched): """A plan of items 1, 2 and on, each predicted to touch the path given for it.""" items = touch_plan(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1))) return next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") def test_starts_the_next_item_and_those_picked_beside_it_up_to_the_limit(self): # Item 2 overlaps item 1, so it waits for it; items 3 and 4 run beside it. plan = self.plan("a.py", "a.py", "c.py", "d.py") self.assertEqual(chunk_windows.may_start(plan, [], set(), 1), ["item-1"]) self.assertEqual(chunk_windows.may_start(plan, [], set(), 2), ["item-1", "item-3"]) self.assertEqual(chunk_windows.may_start(plan, [], set(), 9), ["item-1", "item-3", "item-4"]) def test_leaves_the_items_running_chunks_hold_and_starts_only_as_many_as_the_room_left(self): plan = self.plan("a.py", "a.py", "c.py", "d.py") self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1, 3}, 1), ["item-4"]) self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 2), ["item-3", "item-4"]) self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 0), []) def test_counts_the_running_chunks_against_the_limit_and_restarts_stopped_ones_first(self): windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True), tmux.Listed("item-4", "", True)] self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 3), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 0)) self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3"], 0)) self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 5), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 2)) def test_a_loop_whose_chunk_finished_leaves_its_room_and_restarts_no_stopped_chunk(self): windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True)] self.assertEqual(chunk_windows.room_of(windows, "item-1", True, 2), chunk_windows.Room([], ["item-3"], [], 2)) def test_leaves_no_room_where_more_chunks_run_than_the_limit(self): windows = [tmux.Listed(f"item-{n}", "", False) for n in (1, 2, 3)] self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2).left, 0) def test_starts_no_item_that_overlaps_a_running_chunks_items(self): # Item 1 merged, so 2 is the plan's next item; it overlaps item 3, which still runs, at c.py. items = touch_plan((2, "1.0.0", "", "- `a.py`\n- `c.py`"), (3, "1.0.0", "", "- `c.py`"), (4, "1.0.0", "", "- `d.py`")) plan = next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") self.assertEqual(chunk_windows.may_start(plan, ["item-3"], {3}, 2), ["item-4"]) # A running chunk's item without a prediction leaves no other item free to start beside it. unpredicted = touch_plan((2, "1.0.0", "", "- `a.py`"), (3, "1.0.0", "", None)) self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=unpredicted, first=unpredicted[0], text="", branch_template=""), ["item-3"], {3}, 2), []) # A held item that left the plan, as once it merged, overlaps nothing. self.assertEqual(chunk_windows.may_start(plan, ["item-9"], {9}, 2), ["item-2", "item-4"]) def test_a_plan_without_an_item_starts_one_chunk_and_only_alone(self): none = next_chunk.NoItem("release 1.0.0 has no items") self.assertEqual(chunk_windows.may_start(none, [], set(), 3), ["no-item"]) self.assertEqual(chunk_windows.may_start(none, ["item-1"], {1}, 3), []) def test_names_a_window_for_its_item_and_reads_the_item_back(self): self.assertEqual(chunk_windows.window_name(372), "item-372") self.assertEqual([chunk_windows.item_of(chunk) for chunk in ("item-372", "no-item", "item-", "item-3x", "xitem-3")], [372, None, None, None, None]) # Only ASCII digits name an item, as window_name writes them. self.assertIsNone(chunk_windows.item_of("item-\u0663")) def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self): 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: transcripts = pathlib.Path(temp) folder = transcripts / "-r" folder.mkdir() # A folder in place of the transcript file, so reading it fails. (folder / f"{chunk_windows.chunk_session('proj', 'item-7', transcripts).id}.jsonl").mkdir() self.assertEqual(chunk_windows.held_items(["item-7"], "proj", transcripts), {7}) def test_tells_a_chunk_the_items_the_projects_other_live_chunks_hold(self): with tempfile.TemporaryDirectory() as temp: home = pathlib.Path(temp) held = paths.paths_from(home) folder = held.claude_code_transcripts / "-r" folder.mkdir(parents=True) lines = [assistant({"type": "text", "text": "Chunk items: 8 (L 1, E + I 2), 11 (L 2, E + I 3)"})] (folder / f"{chunk_windows.chunk_session('r', 'item-8', held.claude_code_transcripts).id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) # Its own window, item-7, holds nothing for it; item-9's stopped chunk holds its item. fake = FakeEffects(windows="item-7\t0\t@1\nitem-8 ?\t0\t@2\nitem-9\t1\t@3\nitem-10\t0\t@4\n") self.assertEqual(chunk_windows.held_beside("item-7", "/r", held, fake.effects()), [8, 9, 10, 11]) self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-r", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]]) class StartChunks(unittest.TestCase): """start_chunks: the windows a loop opens for the chunks that may start now.""" LAUNCHER = pathlib.Path("/b/loop-claude") def setUp(self): made = tempfile.TemporaryDirectory() self.addCleanup(made.cleanup) self.root = pathlib.Path(made.name, "r") 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 = plan_text("a.py", "a.py", "c.py") def limit(self, chunks): config = self.home / ".claude-projects" / self.root.name / "config.toml" config.write_text(config.read_text() + f"parallel-chunks = {chunks}\n") def start(self, fake, chunk=None, leaving=False, plan=None, env=None, owner=None): """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) 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() 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")] def test_opens_a_window_for_each_chunk_that_may_start_the_first_making_the_session(self): self.limit(2) fake = FakeEffects() self.assertEqual(self.start(fake, env={"LOOP_CLAUDE_WORKER_MODEL": "m", "PATH": "/bin"}), ["item-1", "item-3"]) self.assertEqual(self.starts(fake), [("new-session", "item-1", [str(self.LAUNCHER), "--chunk", "item-1", "ship it"]), ("new-window", "item-3", [str(self.LAUNCHER), "--chunk", "item-3", "ship it"])]) first = next(call for call in fake.tmux_calls if call[0] == "new-session") self.assertEqual((first[first.index("-s") + 1], first[first.index("-c") + 1]), (self.session, str(self.root))) # Only loop-claude's own variables are handed on, since a window gets the tmux server's environment. self.assertEqual([word for word in first if "=" in word], ["LOOP_CLAUDE_WORKER_MODEL=m"]) self.assertEqual(fake.tmux_calls[0], ["list-windows", "-t", f"={self.session}", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]) # The session names the checkout whose chunks it runs. self.assertIn(["set-option", "-t", f"={self.session}:", "@loop-root", str(self.root)], fake.tmux_calls) def test_refuses_a_session_that_runs_another_checkouts_chunks(self): for owner, named in (("/c/acme.api", "/c/acme.api"), ("", "another checkout")): with self.subTest(owner=owner): fake = FakeEffects(windows="item-5\t0\t@1\n") self.assertEqual(self.start(fake, owner=owner), Failure(f"the tmux session {self.session} runs the chunks of {named}, whose key maps to the same session name; rename one of the two checkouts' folders")) self.assertEqual(self.starts(fake), []) self.assertIn(["show-options", "-qv", "-t", f"={self.session}:", "@loop-root"], fake.tmux_calls) def test_refuses_a_checkout_do_epic_runs_chunks_in(self): fake = FakeEffects(windows=f"epic-ABC-12\tABC-34 ?\t0\t@4\t{self.root}\nepic-ABC-12\tABC-35\t0\t@5\t/c/other\nepic-ABC-12\tABC-36\t1\t@6\t{self.root}\nloop-x\titem-1\t0\t@7\t{self.root}\n") self.assertEqual(self.start(fake), Failure(f"do-epic runs chunks in {self.root}: ABC-34 of epic-ABC-12; loop-claude doesn't start a chunk there until those chunks finish")) self.assertEqual(self.starts(fake), []) def test_leaves_out_the_items_a_running_chunk_names_on_its_chunk_items_line(self): self.limit(2) transcripts = paths.paths_from(self.home).claude_code_transcripts folder = transcripts / "-r" folder.mkdir(parents=True) opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") self.assertEqual(self.start(FakeEffects(windows="item-5\t0\t@1\n")), ["item-3"]) def test_starts_one_chunk_without_parallel_chunks(self): fake = FakeEffects() self.assertEqual(self.start(fake), ["item-1"]) def test_starts_none_and_reads_no_plan_while_the_running_chunks_fill_the_limit(self): self.limit(2) fake = FakeEffects(windows="item-5\t0\t@1\nitem-6 ?\t0\t@2\n") self.assertEqual((self.start(fake), self.planned, self.starts(fake)), ([], 0, [])) # More chunks run than the limit allows, as after it was lowered; a stopped chunk still waits. fake = FakeEffects(windows="item-5\t0\t@1\nitem-6\t0\t@2\nitem-7\t0\t@3\nitem-8\t1\t@4\nitem-9\t1\t@5\n") self.assertEqual((self.start(fake), self.planned), ([], 0)) def test_a_finishing_chunks_own_window_counts_as_no_longer_running(self): fake = FakeEffects(windows="item-5\t0\t@1\n") self.assertEqual(self.start(fake, chunk="item-5"), []) fake = FakeEffects(windows="item-5\t0\t@1\n") self.assertEqual(self.start(fake, chunk="item-5", leaving=True), ["item-1"]) # The session exists, since the finishing loop's window is still in it. self.assertEqual(self.starts(fake)[0][0], "new-window") def test_the_humans_loop_claude_starts_a_stopped_chunk_again_first_closing_its_dead_window(self): fake = FakeEffects(windows="item-5\t1\t@3\n") self.assertEqual((self.start(fake), self.planned), (["item-5"], 0)) self.assertIn(["kill-window", "-t", "@3"], fake.tmux_calls) # Its dead window was the session's last, so the session is made again. self.assertEqual(self.starts(fake)[0][0], "new-session") self.limit(2) fake = FakeEffects(windows="item-5\t1\t@3\n") self.assertEqual(self.start(fake), ["item-5", "item-1"]) 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=plan_text()), ["item-5"]) def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self): self.limit(2) transcripts = paths.paths_from(self.home).claude_code_transcripts folder = transcripts / "-r" folder.mkdir(parents=True) opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") fake = FakeEffects(windows="item-7\t0\t@1\nitem-5\t1\t@3\n") # Item 1 is the stopped chunk's, so item 3 starts; the stopped one doesn't count against parallel-chunks. 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=plan_text()), []) def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self): fake = FakeEffects() self.assertEqual(self.start(fake, plan=Failure("no origin/main")), ["no-item"]) 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): 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) (self.home / ".local" / "state" / "loop-claude").write_text("") started = self.start(FakeEffects()) 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) opened = chunk_windows.chunk_session("proj", "item-7", transcripts) self.assertEqual(opened.name, "A proj loop-claude item-7") folder = transcripts / "-r" folder.mkdir() lines = [assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2)"}), assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2), 9 (L 2, E + I 3)"})] (folder / f"{opened.id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) self.assertEqual(chunk_windows.held_items(["item-7", "item-8", "no-item"], "proj", transcripts), {7, 8, 9}) if __name__ == "__main__": unittest.main()