Answer stability round 1: 1 🔴, 4 🟡 (0 misread); check a new chunk's Touches against every held item, keep a stopped chunk's items, test the fetch and fold the slot loop back
This commit is contained in:
@@ -93,12 +93,14 @@ def a_loop(effects, **fields):
|
||||
class FakeEffects:
|
||||
"""Effects that record each call and answer from a script, one answer per call."""
|
||||
|
||||
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=(), dm_failures=(), tmux_version="tmux 3.2", windows=None):
|
||||
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=(), dm_failures=(), tmux_version="tmux 3.2", windows=None, owner="/r"):
|
||||
self.calls = []
|
||||
# Each tmux command run. They stay out of self.calls, as the DMs do. tmux prints `windows` for list-windows, or without them finds no session.
|
||||
self.tmux_calls = []
|
||||
self.tmux_version = tmux_version
|
||||
self.windows = windows
|
||||
# The checkout the session's @loop-root option names.
|
||||
self.owner = owner
|
||||
# Each direct message sent. They stay out of self.calls, so a test that lists every call doesn't list them. Without a script, each is sent.
|
||||
self.dms = []
|
||||
self.dm_failures = list(dm_failures)
|
||||
@@ -134,7 +136,7 @@ class FakeEffects:
|
||||
self.tmux_calls.append(args)
|
||||
if args[0] == "list-windows":
|
||||
return subprocess.CompletedProcess(args, 1 if self.windows is None else 0, self.windows or "", "no server")
|
||||
return subprocess.CompletedProcess(args, 0, {"-V": self.tmux_version + "\n"}.get(args[0], "@9\n"), "")
|
||||
return subprocess.CompletedProcess(args, 0, {"-V": self.tmux_version + "\n", "show-options": self.owner + "\n"}.get(args[0], "@9\n"), "")
|
||||
|
||||
def dm(self, text):
|
||||
self.dms.append(text)
|
||||
@@ -1293,6 +1295,7 @@ class TmuxCommands(unittest.TestCase):
|
||||
self.assertIsNone(tmux.start_window(window, "/r", {}, ["/b/loop-claude"], False, fake.tmux))
|
||||
self.assertEqual(fake.tmux_calls, [
|
||||
["new-session", "-d", "-s", "loop-proj", "-n", "item-7", "-c", "/r", "-e", "LOOP_CLAUDE_X=1", "-P", "-F", "#{window_id}", "--", "/b/loop-claude", "--chunk", "item-7", "ship it\;"],
|
||||
["set-option", "-t", "=loop-proj", "@loop-root", "/r"],
|
||||
["set-option", "-w", "-t", "@9", "remain-on-exit", "failed"],
|
||||
["new-window", "-d", "-t", "=loop-proj:", "-n", "item-7", "-c", "/r", "-P", "-F", "#{window_id}", "--", "/b/loop-claude"],
|
||||
["set-option", "-w", "-t", "@9", "remain-on-exit", "failed"],
|
||||
@@ -1305,6 +1308,11 @@ class TmuxCommands(unittest.TestCase):
|
||||
self.assertEqual(tmux.start_window(window, "/r", {}, ["x"], True, failed), Failure("tmux could not start the window loop-proj:item-7: duplicate session: loop-proj"))
|
||||
self.assertEqual(tmux.start_window(window, "/r", {}, ["x"], True, silent), Failure("tmux could not start the window loop-proj:item-7: it exited 2"))
|
||||
|
||||
def test_reads_the_checkout_the_session_names(self):
|
||||
fake = FakeEffects(owner="/c/proj")
|
||||
self.assertEqual(tmux.owner("loop-proj", fake.tmux), "/c/proj")
|
||||
self.assertEqual(fake.tmux_calls, [["show-options", "-qv", "-t", "=loop-proj", "@loop-root"]])
|
||||
|
||||
def test_marks_a_waiting_window_by_its_pane_and_clears_the_mark(self):
|
||||
fake = FakeEffects()
|
||||
tmux.mark(WINDOW, True, fake.tmux)
|
||||
@@ -1338,6 +1346,17 @@ class ChunkWindows(unittest.TestCase):
|
||||
self.assertEqual(chunk_windows.may_start(plan, ["item-1", "item-3"], {1, 3}, 2), [])
|
||||
self.assertEqual(chunk_windows.may_start(plan, ["item-1", "item-3", "item-4"], {1, 3, 4}, 2), [])
|
||||
|
||||
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 = gathered.Plan(items=items, first=items[0], text="", branch_template="")
|
||||
self.assertEqual(chunk_windows.may_start(plan, ["item-3"], {3}, 3), ["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(gathered.Plan(items=unpredicted, first=unpredicted[0], text="", branch_template=""), ["item-3"], {3}, 3), [])
|
||||
# A held item that left the plan, as once it merged, overlaps nothing.
|
||||
self.assertEqual(chunk_windows.may_start(plan, ["item-9"], {9}, 3), ["item-2", "item-4"])
|
||||
|
||||
def test_a_plan_without_an_item_starts_one_chunk_and_only_alone(self):
|
||||
none = gathered.NoItem("release 1.0.0 has no items")
|
||||
self.assertEqual(chunk_windows.may_start(none, [], set(), 3), ["no-item"])
|
||||
@@ -1370,9 +1389,9 @@ class ChunkWindows(unittest.TestCase):
|
||||
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.
|
||||
# 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, 10, 11])
|
||||
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}"]])
|
||||
|
||||
|
||||
@@ -1393,7 +1412,9 @@ class StartChunks(unittest.TestCase):
|
||||
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):
|
||||
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)
|
||||
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()
|
||||
@@ -1418,6 +1439,15 @@ class StartChunks(unittest.TestCase):
|
||||
# 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), [])
|
||||
|
||||
def test_leaves_out_the_items_a_running_chunk_names_on_its_chunk_items_line(self):
|
||||
self.limit(2)
|
||||
@@ -1445,23 +1475,36 @@ class StartChunks(unittest.TestCase):
|
||||
# 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"])
|
||||
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")
|
||||
# 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.limit(2)
|
||||
fake = FakeEffects(windows="item-5\t1\t@3\n")
|
||||
self.assertEqual(self.start(fake), ["item-5", "item-1"])
|
||||
|
||||
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=gathered.NoItem("release 1.0.0 has no items")), [])
|
||||
|
||||
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):
|
||||
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"))
|
||||
|
||||
@@ -1564,11 +1607,15 @@ class ChunkFacts(unittest.TestCase):
|
||||
self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree"], "/r")])
|
||||
|
||||
def test_the_facts_end_with_the_items_the_projects_other_running_chunks_hold(self):
|
||||
# The loop's own window, a dead one and two that run, one of them waiting for the human.
|
||||
# The loop's own window, a stopped one and two that run, one of them waiting for the human.
|
||||
fake = FakeEffects(python=[ran("- A fact.\n")], windows="item-7\t0\t@1\nitem-5\t1\t@2\nitem-12 ?\t0\t@3\nitem-9\t0\t@4\n")
|
||||
said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER))
|
||||
self.assertTrue(said.endswith("- A fact.\n\nOther chunks of this project run now, and hold items 9, 12. Leave those out of this chunk, whatever else may join it."), said)
|
||||
self.assertEqual(prompts.held_by_others([9]), "\n\nOther chunks of this project run now, and hold item 9. Leave those out of this chunk, whatever else may join it.")
|
||||
self.assertTrue(said.endswith("- A fact.\n\nOther chunks of this project, running or stopped, hold items 5, 9, 12. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said)
|
||||
self.assertEqual(prompts.held_by_others([9]), "\n\nOther chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it.")
|
||||
# A worker whose preflight failed is told them too.
|
||||
fake = FakeEffects(python=[ran(code=1, stderr="git could not start\n")], windows="item-9\t0\t@4\n")
|
||||
said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER))
|
||||
self.assertTrue(said.endswith("Other chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said)
|
||||
|
||||
def test_has_the_worker_find_the_facts_itself_where_the_preflight_failed(self):
|
||||
for failed, why in ((ran(code=1, stderr="git could not start\n"), "git could not start"), (ran(code=1), "exited 1")):
|
||||
@@ -2226,7 +2273,7 @@ class WindowStarts(unittest.TestCase):
|
||||
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")
|
||||
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, so `do it` started in no new window\n")
|
||||
|
||||
def test_off_a_terminal_it_prints_how_to_attach(self):
|
||||
fake = FakeEffects(git=["/r\n"], on_terminal=False)
|
||||
@@ -2243,7 +2290,7 @@ class WindowStarts(unittest.TestCase):
|
||||
effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1)
|
||||
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()):
|
||||
stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects)
|
||||
self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the session"))
|
||||
self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session"))
|
||||
|
||||
def test_a_window_loop_outside_tmux_stops(self):
|
||||
fake = FakeEffects(git=["/r\n"])
|
||||
|
||||
Reference in New Issue
Block a user