diff --git a/bin/loop_claude/loop/chunk_windows.py b/bin/loop_claude/loop/chunk_windows.py index da6f577f..36d214c5 100644 --- a/bin/loop_claude/loop/chunk_windows.py +++ b/bin/loop_claude/loop/chunk_windows.py @@ -41,7 +41,7 @@ def window_name(item_id: int) -> str: def item_of(chunk: str) -> int | None: """The ID of the plan item a chunk starts from; None for NO_ITEM.""" - named = re.fullmatch(r"item-([0-9]+)", chunk, re.ASCII) + named = re.fullmatch(r"item-([0-9]+)", chunk) return int(named[1]) if named else None diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 31f8aa9c..46e1a908 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -1174,6 +1174,11 @@ class RouteSources(unittest.TestCase): facts, reader = chunk_start.chunk_start(loop) self.assertEqual((facts, reader().id), ("", routed)) fetched.assert_called_with(loop) + # Where the plan cannot be read, the reader says so through the loop's effects, and routes on the project's rung. + fetched.return_value = Failure("no origin remote") + with contextlib.redirect_stderr(io.StringIO()) as said: + self.assertIsNone(chunk_start.chunk_start(a_loop(FakeEffects().effects()))[1]()) + self.assertIn("routing on the project's rung: no origin remote", said.getvalue()) 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): @@ -1391,9 +1396,12 @@ class StartChunks(unittest.TestCase): 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()) + effects = fake.effects() + 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) return started def starts(self, fake): @@ -1409,6 +1417,16 @@ class StartChunks(unittest.TestCase): 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}"]) + + 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() @@ -1433,6 +1451,10 @@ class StartChunks(unittest.TestCase): 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") + # Another chunk's dead window stays, with its last lines, until that chunk starts again. + fake = FakeEffects(windows="item-5\t1\t@7\n") + self.assertEqual(self.start(fake), ["item-1"]) + self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], []) def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self): fake = FakeEffects() diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index 697f5466..45a0c479 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -98,10 +98,15 @@ class TmuxEffect(unittest.TestCase): done = system_effects().tmux(["list-windows", "-t", "=loop-proj"]) self.assertEqual((done.returncode, done.stdout), (4, "list-windows -t =loop-proj\n")) + def test_reads_output_that_is_not_utf_8_with_each_bad_byte_replaced(self): + with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder): + pathlib.Path(folder, "tmux").write_text(f"#!{sys.executable}\nimport sys\nsys.stdout.buffer.write(b'caf\\xe9')\n") + self.assertEqual(system_effects().tmux(["list-windows"]).stdout, "caf\ufffd") + def test_reads_a_tmux_that_cannot_start_as_exit_127(self): with tempfile.TemporaryDirectory() as folder, unittest.mock.patch.dict(os.environ, {"PATH": folder}): done = system_effects().tmux(["-V"]) - self.assertEqual((done.returncode, done.stdout), (127, "")) + self.assertEqual((done.args, done.returncode, done.stdout), (["tmux", "-V"], 127, "")) self.assertIn("could not start", done.stderr) def test_runs_tmux_on_the_terminal_and_returns_its_exit_code(self):