diff --git a/bin/loop_claude/loop/chunk_windows.py b/bin/loop_claude/loop/chunk_windows.py index 7de34517..92a4acea 100644 --- a/bin/loop_claude/loop/chunk_windows.py +++ b/bin/loop_claude/loop/chunk_windows.py @@ -76,13 +76,16 @@ def room_of(windows: list[tmux.Listed], own: str | None, leaving: bool, limit: i return Room(running, stopped, restart, room - len(restart)) -def may_start(plan: NextChunk | NoItem, running: list[str], held: set[int], room: int) -> list[str] | Failure: - """The chunks to start now beside the running ones, whose chunks hold the items in held: the plan's next item and the items touch_predictions picks to run beside it, in the plan's order, up to room of them. Each leaves out an item in held, and one whose `Touches:` prediction overlaps a held item's, or where either has none. A plan without an item starts NO_ITEM, and only alone. A plan whose NoItem names pull requests that await a person's merge starts none: where nothing runs, a Failure names them.""" - if isinstance(plan, NoItem) and plan.awaits_merge: - # Where nothing runs, the loop stops on the pull requests plan.why names, rather than start a worker only to report them. - return [] if running else Failure(plan.why) +@dataclasses.dataclass(frozen=True) +class AwaitedMerges: + """Why no chunk starts and none runs: every item left awaits a person's merge of the pull requests that why names.""" + why: str + + +def may_start(plan: NextChunk | NoItem, running: list[str], held: set[int], room: int) -> list[str]: + """The chunks to start now beside the running ones, whose chunks hold the items in held: the plan's next item and the items touch_predictions picks to run beside it, in the plan's order, up to room of them. Each leaves out an item in held, and one whose `Touches:` prediction overlaps a held item's, or where either has none. A plan without an item starts NO_ITEM, and only alone; none where its NoItem names pull requests that await a person's merge.""" if isinstance(plan, NoItem): - return [] if running or room < 1 else [NO_ITEM] + return [] if running or room < 1 or plan.awaits_merge else [NO_ITEM] beside = chunks_beside(plan.items, plan.first) picked = [plan.first.id, *(beside.picked if isinstance(beside, Beside) else [])] details = {item.id: item.detail for item in plan.items} @@ -111,8 +114,8 @@ def next_plan(root: str, home: pathlib.Path, effects: Effects) -> NextChunk | No return planned(held.plan, held.cut, ref, config.branch, None if config.tracked("plan") else config.plan_label) -def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Mapping[str, str], launcher: pathlib.Path, effects: Effects) -> list[str] | Failure: - """Starts a loop-claude in a new window for each chunk of loop_run's project that may start now, and returns the chunks started. leaving says that loop_run's own chunk finished, so its window no longer counts as running; without it, the human started loop-claude. A Failure says why no chunk may start, or why one could not; those before it run.""" +def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Mapping[str, str], launcher: pathlib.Path, effects: Effects) -> list[str] | AwaitedMerges | Failure: + """Starts a loop-claude in a new window for each chunk of loop_run's project that may start now, and returns the chunks started. leaving says that loop_run's own chunk finished, so its window no longer counts as running; without it, the human started loop-claude. AwaitedMerges where chunks_to_start returns it. A Failure says why no chunk may start, or why one could not; those before it run.""" config = located(loop_run.root, paths.home) if isinstance(config, Failure): return config @@ -125,7 +128,7 @@ def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Map if refused := refusal(session, windows, loop_run.root, effects.tmux): return refused chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects) - if isinstance(chunks, Failure): + if isinstance(chunks, AwaitedMerges): return chunks for chunk in chunks: started = tmux.start_chunk(tmux.Window(session, chunk, ""), loop_run.root, handed, [str(launcher), CHUNK_FLAG, chunk, loop_run.task], windows, True, effects.tmux) @@ -144,8 +147,8 @@ def refusal(session: str, windows: list[tmux.Listed], root: str, run: tmux.Tmux) return None -def chunks_to_start(windows: list[tmux.Listed], loop_run: LoopRun, leaving: bool, limit: int, paths: Paths, effects: Effects) -> list[str] | Failure: - """The chunks to start beside windows, the session's: the stopped ones room_of restarts, then those of the plan that may_start picks. A plan that cannot be read starts NO_ITEM, as a plan without an item does. A Failure where no chunk runs or restarts and every item left waits on a person's merge: it names the pull requests.""" +def chunks_to_start(windows: list[tmux.Listed], loop_run: LoopRun, leaving: bool, limit: int, paths: Paths, effects: Effects) -> list[str] | AwaitedMerges: + """The chunks to start beside windows, the session's: the stopped ones room_of restarts, then those of the plan that may_start picks. A plan that cannot be read starts NO_ITEM, as a plan without an item does. AwaitedMerges where no chunk runs or restarts and every item left awaits a person's merge.""" room = room_of(windows, loop_run.chunk, leaving, limit) if not room.left: return room.restart @@ -155,8 +158,10 @@ def chunks_to_start(windows: list[tmux.Listed], loop_run: LoopRun, leaving: bool say(f"starting a chunk without a plan item, since the plan cannot be read: {plan.reason}", effects) plan = NoItem(plan.reason) held = held_items(room.running + room.stopped, project_key(loop_run.root, paths.home), paths.claude_code_transcripts) + if isinstance(plan, NoItem) and plan.awaits_merge and not room.running + room.restart: + return AwaitedMerges(plan.why) picked = may_start(plan, room.running + room.restart, held, room.left) - return picked if isinstance(picked, Failure) else room.restart + [chunk for chunk in picked if chunk not in room.stopped] + return room.restart + [chunk for chunk in picked if chunk not in room.stopped] def held_beside(chunk: str, root: str, paths: Paths, effects: Effects) -> list[int]: diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 8cf9a848..d85ef18b 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -4,11 +4,11 @@ import typing from chunk_driver.cycle_start import announced_route, cycle_of, project_routing from transcript_archive.location import archive_root, project_archive +from loop_claude.loop.chunk_windows import AwaitedMerges, chunk_session, item_of, start_chunks from loop_claude.loop.routing_check import check_routing from loop_claude.loop.checkout import checkout_root from chunk_driver.claims import CHUNK_FLAG, loop_session from loop_claude.chunk import chunk_items, fresh_prompt -from loop_claude.loop.chunk_windows import chunk_session, item_of, start_chunks from loop_claude.loop.chunk_start import chunk_start, plan_item, scored_plan from chunk_driver.chunk_run import closed_cycle from agent_cycle.run import Concluded, Cycle, run_cycle @@ -30,7 +30,7 @@ from agent_cycle import tmux # Starts the chunks that may start now, as chunk_windows.start_chunks does. -StartChunks = typing.Callable[[LoopRun, bool, Paths, typing.Mapping[str, str], pathlib.Path, Effects], list[str] | Failure] +StartChunks = typing.Callable[[LoopRun, bool, Paths, typing.Mapping[str, str], pathlib.Path, Effects], list[str] | AwaitedMerges | Failure] @dataclasses.dataclass(frozen=True) @@ -107,6 +107,8 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects, start: StartChunks) -> Stop: """What the loop-claude the human runs does: starts, through start, each chunk that may start now in a window of the project's tmux session, then puts that session on the terminal until the human detaches.""" started = start(loop_run, False, paths, env, launcher, effects) + if isinstance(started, AwaitedMerges): + return Stop(0, started.why) if isinstance(started, Failure): return Stop(1, started.reason) session = loop_session(loop_run.root, paths.home) @@ -121,6 +123,8 @@ def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Path def handed_on(loop_run: LoopRun, loop: Loop, start: StartChunks) -> Stop: """The stop of a loop whose chunk finished, once it has started, through start, each chunk that may start now. Its window closes as it exits.""" started = start(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects) + if isinstance(started, AwaitedMerges): + return Stop(0, f"the chunk finished; {started.why}") if isinstance(started, Failure): return Stop(1, f"the chunk finished, but the next could not start: {started.reason}") say(f"the chunk finished; started {', '.join(started) or 'no other, since the chunks running fill what may run now'}", loop.effects, stdout=True) diff --git a/bin/loop_claude/loop/test_chunk_start.py b/bin/loop_claude/loop/test_chunk_start.py index addc76cf..a0eccaa1 100644 --- a/bin/loop_claude/loop/test_chunk_start.py +++ b/bin/loop_claude/loop/test_chunk_start.py @@ -100,7 +100,7 @@ class RouteSources(unittest.TestCase): window = chunk_windows.next_plan(root, folder.parents[1], dataclasses.replace(fake.effects(), git=git)) self.assertEqual((window.awaits_merge, fake.made("forge")[1][:4]), (True, [root, "gh", "pr", "list"]), "a window starts by the same pick") stopped = chunk_windows.chunks_to_start([], reload.LoopRun(1, root, "do it", ""), False, 1, paths.paths_from(folder.parents[1]), dataclasses.replace(fake.effects(), git=git)) - self.assertEqual(stopped, Failure(window.why), "so the loop starts no chunk, and says which pull requests it waits on") + self.assertEqual(stopped, chunk_windows.AwaitedMerges(window.why), "so the loop starts no chunk, and says which pull requests it waits on") def test_reads_a_plan_outside_the_checkout_as_it_stands_and_stops_without_a_config(self): with tempfile.TemporaryDirectory() as root: diff --git a/bin/loop_claude/loop/test_chunk_windows.py b/bin/loop_claude/loop/test_chunk_windows.py index ee35d0fa..53733a5a 100644 --- a/bin/loop_claude/loop/test_chunk_windows.py +++ b/bin/loop_claude/loop/test_chunk_windows.py @@ -61,9 +61,9 @@ class ChunkWindows(unittest.TestCase): 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_stops_on_the_pull_requests_where_every_item_left_awaits_a_persons_merge_and_nothing_runs(self): + def test_starts_no_chunk_without_an_item_where_every_item_left_awaits_a_persons_merge(self): awaited = next_chunk.NoItem("every item of release 1.0.0 waits. Pull request #7 awaits a person's merge.", True) - self.assertEqual(chunk_windows.may_start(awaited, [], set(), 1), Failure(awaited.why)) + self.assertEqual(chunk_windows.may_start(awaited, [], set(), 1), [], "chunks_to_start stops the loop there") self.assertEqual(chunk_windows.may_start(awaited, ["item-5"], set(), 1), [], "a running or restarted chunk goes on, and no chunk without an item joins it") def test_starts_no_item_that_overlaps_a_running_chunks_items(self): diff --git a/bin/loop_claude/loop/test_main.py b/bin/loop_claude/loop/test_main.py index ba5a6346..a0bf48db 100644 --- a/bin/loop_claude/loop/test_main.py +++ b/bin/loop_claude/loop/test_main.py @@ -11,10 +11,10 @@ import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) from chunk_driver.doubles import a_loop +from loop_claude.loop import chunk_windows, main, reload, usage from loop_effects.doubles import CWD, FakeEffects, ran, STAMP from failure import Failure from agent_cycle.verdicts import Finished, Unfinished -from loop_claude.loop import main, reload, usage from loop_claude.doubles import OPUS_PROJECT from loop_state import paths from model_routing.route import Route @@ -240,6 +240,7 @@ class WindowStarts(unittest.TestCase): def test_stops_where_no_chunk_could_start(self): self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=Failure("no plan"))[0], Stop(1, "no plan")) + self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=chunk_windows.AwaitedMerges("#7 awaits a merge."))[0], Stop(0, "#7 awaits a merge."), "nothing failed, so the stop exits 0") def test_stops_where_tmux_cannot_show_the_session(self): fake = FakeEffects(git=["/r\n"]) @@ -260,7 +261,8 @@ 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", "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"))): + (Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan")), + (chunk_windows.AwaitedMerges("#7 awaits a merge."), "", Stop(0, "the chunk finished; #7 awaits a merge."))): with self.subTest(started=started), contextlib.redirect_stdout(io.StringIO()) as printed: loop, asked = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}), [] self.assertEqual(main.handed_on(loop_run, loop, lambda *args: asked.append(args) or started), stop) diff --git a/bin/loop_claude/loop/usage.py b/bin/loop_claude/loop/usage.py index 0b73d47f..e008f98e 100644 --- a/bin/loop_claude/loop/usage.py +++ b/bin/loop_claude/loop/usage.py @@ -160,6 +160,10 @@ measurement. A change to bin/loop-claude or any bin/**/*.py takes effect at the next cycle boundary, once loop-claude --help succeeds: the loop reloads into the edited script. +Under merge = "human", where no chunk runs and every item left awaits a person's merge, loop-claude +doesn't start a chunk: it names those pull requests, in a DM too, and exits 0. Its window closes, +so the next loop-claude reads the plan again. + Exit 1, saying why, when: - no task is given - git cannot start or finds no checkout