|
|
|
@@ -1167,6 +1167,13 @@ class RouteSources(unittest.TestCase):
|
|
|
|
|
self.assertEqual(chunk_start.plan_item(plan, later, FakeEffects().effects()).id, later)
|
|
|
|
|
# An item that left the plan, as once it shipped.
|
|
|
|
|
self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects()))
|
|
|
|
|
with unittest.mock.patch.object(chunk_start, "session_open", return_value=True), unittest.mock.patch.object(chunk_start, "fetched_plan", return_value=plan) as fetched:
|
|
|
|
|
for window, routed in ((tmux.Window("loop-proj", f"item-{later}", "%4"), later), (tmux.Window("loop-proj", "no-item", "%4"), 3)):
|
|
|
|
|
with self.subTest(window=window.name):
|
|
|
|
|
loop = a_loop(FakeEffects().effects(), window=window)
|
|
|
|
|
facts, reader = chunk_start.chunk_start(loop)
|
|
|
|
|
self.assertEqual((facts, reader().id), ("", routed))
|
|
|
|
|
fetched.assert_called_with(loop)
|
|
|
|
|
unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8")
|
|
|
|
|
for plan, said in ((DefaultPlan(plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), NO_CUT), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan(unreadable, NO_CUT), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")):
|
|
|
|
|
with self.subTest(said=said):
|
|
|
|
@@ -1264,12 +1271,15 @@ class TmuxCommands(unittest.TestCase):
|
|
|
|
|
self.assertEqual(tmux.session_name("claude-config"), "loop-claude-config")
|
|
|
|
|
# tmux reads `.` and `:` in a target as separators.
|
|
|
|
|
self.assertEqual(tmux.session_name("my.site:2 x"), "loop-my-site-2-x")
|
|
|
|
|
self.assertEqual(tmux.session_name("Acme/Billing"), "loop-Acme-Billing")
|
|
|
|
|
|
|
|
|
|
def test_lists_the_windows_without_the_waiting_mark(self):
|
|
|
|
|
fake = FakeEffects(windows="item-372 ?\t0\t@1\nitem-380\t1\t@2\nno-item\t0\t@5\n")
|
|
|
|
|
self.assertEqual(tmux.listed("loop-proj", fake.tmux), [tmux.Listed("item-372", "@1", False), tmux.Listed("item-380", "@2", True), tmux.Listed("no-item", "@5", False)])
|
|
|
|
|
self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-proj", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]])
|
|
|
|
|
self.assertEqual(tmux.listed("loop-proj", FakeEffects().tmux), [])
|
|
|
|
|
failed = lambda args: subprocess.CompletedProcess(args, 1, "item-1\t0\t@1\n", "no server")
|
|
|
|
|
self.assertEqual(tmux.listed("loop-proj", failed), [])
|
|
|
|
|
|
|
|
|
|
def test_the_first_window_makes_the_session_and_each_later_one_joins_it(self):
|
|
|
|
|
fake = FakeEffects()
|
|
|
|
@@ -1331,6 +1341,114 @@ class ChunkWindows(unittest.TestCase):
|
|
|
|
|
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):
|
|
|
|
|
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"])
|
|
|
|
|
|
|
|
|
|
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, and item-9's dead one hold nothing for it.
|
|
|
|
|
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, 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 = ChunkWindows.plan(self, "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):
|
|
|
|
|
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:
|
|
|
|
|
started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, fake.effects())
|
|
|
|
|
self.said = said.getvalue()
|
|
|
|
|
self.planned = planned.call_count
|
|
|
|
|
return started
|
|
|
|
|
|
|
|
|
|
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"])
|
|
|
|
|
|
|
|
|
|
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, []))
|
|
|
|
|
|
|
|
|
|
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_closes_the_dead_window_of_a_chunk_before_starting_it_again(self):
|
|
|
|
|
fake = FakeEffects(windows="item-1\t1\t@3\n")
|
|
|
|
|
self.assertEqual(self.start(fake), ["item-1"])
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
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_no_window_could_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"))
|
|
|
|
|
|
|
|
|
|
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_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
|
|
@@ -2013,6 +2131,10 @@ class LoopRuns(unittest.TestCase):
|
|
|
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")
|
|
|
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state) | {"items_taken_in": [12, 14]})), state)
|
|
|
|
|
|
|
|
|
|
def test_drops_the_slot_that_a_loop_started_before_chunks_ran_in_windows_hands_on(self):
|
|
|
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")
|
|
|
|
|
self.assertEqual(reload.decode_handed(json.dumps({key: value for key, value in dataclasses.asdict(state).items() if key != "chunk"} | {"slot": 1})), state)
|
|
|
|
|
|
|
|
|
|
def test_stops_when_the_reload_cannot_start_python(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
|
launcher = pathlib.Path(root, "loop-claude")
|
|
|
|
@@ -2051,6 +2173,36 @@ class WindowStarts(unittest.TestCase):
|
|
|
|
|
self.assertEqual(fake.tmux_calls, [["-V"], shown])
|
|
|
|
|
self.assertEqual(self.asked, [(main.LoopRun(0, "/r", "do it", ""), False)])
|
|
|
|
|
|
|
|
|
|
def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self):
|
|
|
|
|
fake = FakeEffects(git=["/r\n"], on_terminal=False)
|
|
|
|
|
effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude")
|
|
|
|
|
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()):
|
|
|
|
|
main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
|
|
|
|
self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects))
|
|
|
|
|
|
|
|
|
|
def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self):
|
|
|
|
|
fake = FakeEffects(git=["/r\n"])
|
|
|
|
|
effects, env = fake.effects(), {"TMUX_PANE": "%4"}
|
|
|
|
|
launcher = pathlib.Path("/b/loop-claude")
|
|
|
|
|
cycle = unittest.mock.Mock(project="claude-opus-5-5")
|
|
|
|
|
unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished))
|
|
|
|
|
finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished))
|
|
|
|
|
closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")]
|
|
|
|
|
with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \
|
|
|
|
|
unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \
|
|
|
|
|
unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on:
|
|
|
|
|
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
|
|
|
|
loop = this_loop.Loop(root="/r", session=session.session_named("A r loop-claude item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects)
|
|
|
|
|
self.assertEqual(stop, Stop(0, ""))
|
|
|
|
|
self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)])
|
|
|
|
|
handed_on.assert_called_once_with(closed[1], loop)
|
|
|
|
|
|
|
|
|
|
def test_a_window_loop_given_no_task_prints_the_help(self):
|
|
|
|
|
self.assertEqual(main.loop_run_from(["--chunk", "item-3"], CWD, FakeEffects(git=["/r\n"]).effects()), Stop(1, usage.HELP))
|
|
|
|
|
|
|
|
|
|
def test_a_window_loop_outside_a_checkout_stops(self):
|
|
|
|
|
self.assertIsInstance(main.loop_run_from(["--chunk", "item-3", "do it"], CWD, FakeEffects(git=[Failure("git cannot start")]).effects()), Stop)
|
|
|
|
|
|
|
|
|
|
def test_says_that_nothing_started_where_the_session_already_runs_every_chunk_that_may(self):
|
|
|
|
|
self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=())[1], f"{STAMP} the tmux session loop-r runs every chunk that may run now\n")
|
|
|
|
|
|
|
|
|
@@ -2082,12 +2234,13 @@ class WindowStarts(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def test_a_finished_chunk_starts_each_chunk_that_may_start_and_stops_without_a_reason(self):
|
|
|
|
|
loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7")
|
|
|
|
|
for started, said, stop in ((["item-9"], "the chunk finished; started item-9", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")),
|
|
|
|
|
for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")),
|
|
|
|
|
(Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))):
|
|
|
|
|
with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed:
|
|
|
|
|
self.assertEqual(main.handed_on(loop_run, a_loop(FakeEffects().effects())), stop)
|
|
|
|
|
loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"})
|
|
|
|
|
self.assertEqual(main.handed_on(loop_run, loop), stop)
|
|
|
|
|
self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "")
|
|
|
|
|
self.assertEqual(start.call_args.args[:2], (loop_run, True))
|
|
|
|
|
self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StopDms(unittest.TestCase):
|
|
|
|
|