diff --git a/bin/do_epic/main.py b/bin/do_epic/main.py index 1b18fad1..dc9caf51 100644 --- a/bin/do_epic/main.py +++ b/bin/do_epic/main.py @@ -257,12 +257,14 @@ def repoless_models(epic: EpicRun) -> Models: def premises_read(root: str | None, epic: EpicRun) -> str | Failure: - """The epic's Goals and Audience, from its description, then those of the repo checked out at root, where the chunk has one.""" + """The epic's Goals and Audience, from its description, then those of the repo checked out at root, where the chunk has one. A Failure where the epic lacks either: no chunk runs without premises.""" bodies: list[str] = [] for name in ("Goals", "Audience"): body = epic.record.section(name) if isinstance(body, Failure): return Failure(f"the epic {epic.key}'s description: {body.reason} - no premises.") + if not body.strip(): + return Failure(f"the epic {epic.key}'s description has no text under a `## {name}` heading - no premises.") bodies.append(body) theirs = read_premises(root, epic.paths.home) if root else "" if isinstance(theirs, Failure): diff --git a/bin/do_epic/test_main.py b/bin/do_epic/test_main.py index 07b7b7b5..713239eb 100644 --- a/bin/do_epic/test_main.py +++ b/bin/do_epic/test_main.py @@ -9,11 +9,12 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from do_epic import chunk_prompt, main +from project_config.test_project_config import configured from do_epic.backend.protocol import EpicIssue from do_epic.epic_record import EpicRecord from agent_cycle import escalation_mark, judge, tmux from failure import Failure -from loop_claude.test_loop_claude import FakeEffects +from loop_claude.test_loop_claude import FakeEffects, judged, Worker from do_epic.test_scheduler import issue from loop_claude.loop.this_loop import Loop from loop_claude.loop.reload import LoopRun @@ -140,6 +141,147 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest): self.assertEqual(cycle.route, Route(cycle.project.rung)) +UNREAD = [Failure("no git")] * 20 +MODELS = {"LOOP_CLAUDE_JUDGE_MODEL": "claude-opus-5-5", "LOOP_CLAUDE_WORKER_MODEL": "claude-opus-5-5", "TMUX_PANE": "%1"} + + +class TheHumansRun(DoEpicTest): + def test_shows_the_session_on_a_terminal_and_says_when_tmux_cannot(self) -> None: + fake = FakeEffects(windows="ABC-21\t0\t@9\n") + self.assertEqual(self.run_main(["ABC-12"], fake)[0], 0) + self.assertEqual(fake.tmux_calls[-1], ["attach-session", "-t", "=epic-ABC-12"]) + fake.effects = lambda made=fake.effects(): dataclasses.replace(made, tmux_on_terminal=lambda args: 1) # type: ignore[method-assign] + code, _, err = self.run_main(["ABC-12"], fake, {"TMUX": "/tmp/tmux"}) + self.assertEqual((code, err.split(" ", 1)[1]), (1, "`tmux switch-client -t =epic-ABC-12` failed; the chunks go on in the tmux session\n")) + + def test_off_a_terminal_it_says_how_to_see_the_chunks(self) -> None: + code, out, err = self.run_main(["ABC-12"], FakeEffects(on_terminal=False, windows="ABC-21\t0\t@9\n")) + self.assertEqual((code, err), (0, "")) + self.assertIn("not on a terminal; see the chunks with `tmux attach -t =epic-ABC-12`\n", out) + + def test_with_no_window_in_the_session_it_shows_none(self) -> None: + self.issues = self.issues[:1] + fake = FakeEffects() + code, out, _ = self.run_main(["ABC-12"], fake) + self.assertEqual(code, 0) + self.assertIn("started no chunk in the tmux session epic-ABC-12\n", out) + self.assertNotIn("attach-session", [args[0] for args in fake.tmux_calls]) + + def test_stops_without_its_settings_or_on_a_cap_it_cannot_read(self) -> None: + self.description = "**Max parallel chunks:** many\n" + code, _, err = self.run_main(["ABC-12"], FakeEffects()) + self.assertEqual(code, 1) + self.assertIn("`**Max parallel chunks:** many`, which is not a whole number of 1 or more", err) + (self.home / ".claude-projects/do-epic/config.toml").unlink() + code, out, err = self.run_main(["ABC-12"], FakeEffects()) + self.assertEqual((code, out), (1, "")) + self.assertIn("config.toml", err) + + def test_an_interrupt_from_the_keyboard_ends_it_quietly_and_a_crash_is_sent_on(self) -> None: + fake = FakeEffects() + fake.tmux = lambda args: (_ for _ in ()).throw(KeyboardInterrupt()) # type: ignore[method-assign] + self.assertEqual(self.run_main(["ABC-12"], fake), (130, "", "")) + fake.tmux = lambda args: (_ for _ in ()).throw(ValueError("boom")) # type: ignore[method-assign] + with self.assertRaises(ValueError): + self.run_main(["ABC-12"], fake) + self.assertEqual(fake.dms, ["do-epic ABC-12 crashed: ValueError('boom')"]) + + def test_starts_each_chunk_in_its_repos_checkout(self) -> None: + for name in ("api", "web"): + configured(self, self.home / "code" / name, home=self.home) + self.issues = [issue("ABC-20", "repo:api"), issue("ABC-22", "repo:web")] + fake = FakeEffects(on_terminal=False) + code, out, _ = self.run_main(["ABC-12"], fake) + self.assertEqual(code, 0) + self.assertIn("started ABC-20, ABC-22 in the tmux session epic-ABC-12\n", out) + self.assertEqual([args[args.index("-c") + 1] for args in fake.tmux_calls if args[0] in ("new-session", "new-window")], [str(self.home / "code/api"), str(self.home / "code/web")]) + + +class AChunksRun(DoEpicTest): + def worker(self) -> Worker: + return Worker(paths_from(self.home).claude_code_transcripts.parent) + + def test_runs_cycles_until_the_judge_rules_it_finished_then_starts_the_next_chunk(self) -> None: + self.issues.append(issue("ABC-22", "do-epic-claude")) + self.description = DESCRIPTION.replace("2", "1") + fake = FakeEffects(streams=[self.worker(), self.worker()], claude=[judged("unfinished"), judged("finished")], git=UNREAD, windows="ABC-21\t0\t@9\n") + code, out, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], fake, MODELS) + self.assertEqual(code, 0, err) + self.assertIn("=== cycle 1, worker on claude-", out) + self.assertIn("=== cycle 2, worker on claude-", out) + self.assertNotIn("=== cycle 3", out) + self.assertIn("the chunk finished; started ABC-22\n", out) + self.assertEqual(fake.dms, []) + archive = self.home / ".claude/do-epic-transcripts/ABC-12" + self.assertTrue(any(archive.iterdir())) + self.assertFalse((self.home / ".claude/loop-claude-transcripts").exists()) + streamed = [what for seam, what in fake.calls if seam == "stream_claude"] + self.assertIn("Work the Jira issue ABC-21 of the epic ABC-12 as one chunk", " ".join(streamed[0])) + self.assertNotIn("Work the Jira issue", " ".join(streamed[1])) + + def test_every_text_of_a_run_names_do_epic_as_what_drives_it(self) -> None: + fake = FakeEffects(streams=[self.worker()] * 3, claude=[judged("unfinished"), judged("interrupt"), judged("finished")], inputs=["Use B."], git=UNREAD) + code, _, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], fake, MODELS) + self.assertEqual(code, 0, err) + prompts = [" ".join(what) for seam, what in fake.calls if seam == "stream_claude"] + judged_with = [" ".join(what[0]) + (what[1] or "") for seam, what in fake.calls if seam == "claude"] + self.assertEqual((len(prompts), len(judged_with)), (3, 3)) + for text in (*prompts, *judged_with, *fake.dms, *[str(what) for seam, what in fake.calls if seam == "input"]): + self.assertNotIn("loop-claude", text) + self.assertNotIn("None", text) + self.assertIn(" do-epic runs you on claude-", prompts[1]) + self.assertIn("The human answered your question at do-epic's terminal:\nUse B.", prompts[2]) + for text in judged_with: + self.assertIn("what do-epic told it on [loop] lines, latest last:\n", text) + self.assertIn(escalation_mark.asks_higher_rung(chunk_prompt.DRIVER), text) + self.assertIn("do-epic in ", fake.dms[0]) + + def test_stops_for_the_human_after_six_unfinished_cycles_naming_do_epic(self) -> None: + fake = FakeEffects(streams=[self.worker()] * 6, claude=[judged("unfinished")] * 6, git=UNREAD) + code, out, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], fake, MODELS) + self.assertEqual(code, 2, out) + self.assertIn("the judge ruled it unfinished 6 cycles running; finish the chunk yourself, or rerun do-epic to give it 6 more cycles", err + out) + + def test_a_finished_chunk_says_why_the_next_could_not_start(self) -> None: + store = MemoryEpic("ABC-12", list(self.issues)) + reads = iter([self.issues, self.issues, Failure("Jira answered 503")]) + store.read = lambda: next(reads) # type: ignore[method-assign] + self.backend = lambda config, key: (store, EpicRecord(Description(self.description))) # type: ignore[method-assign,arg-type] + fake = FakeEffects(streams=[self.worker()], claude=[judged("finished")], git=UNREAD) + code, _, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], fake, MODELS) + self.assertEqual(code, 1) + self.assertIn("the chunk finished, but the next could not start: Jira answered 503\n", err) + + def test_a_chunk_in_a_repo_works_in_its_checkout_on_the_repos_premises_and_records(self) -> None: + checkout = self.home / "code" / "api" + configured(self, checkout, home=self.home) + (checkout / "README.md").write_text("## Goals\nServe the API.\n\n## Audience\nIts callers.\n", encoding="utf-8") + self.issues = [dataclasses.replace(issue("ABC-20", "repo:api"), scores={"L": 2, "E": 1, "I": 1})] + fake = FakeEffects(streams=[self.worker()], claude=[judged("finished")], git=UNREAD) + code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], fake, MODELS) + self.assertEqual(code, 0, err) + prompt = " ".join([what for seam, what in fake.calls if seam == "stream_claude"][0]) + self.assertIn(f"Work the Jira issue ABC-20 of the epic ABC-12 as one chunk, in the checkout {checkout}, on the branch ABC-20.", prompt) + self.assertIn("Ship the import.", prompt) + self.assertIn("The repo's goals, which rank after the epic's and after code health, are from", prompt) + self.assertIn("Serve the API.", prompt) + + def test_a_chunk_whose_repo_or_premises_cannot_be_read_stops(self) -> None: + self.issues = [issue("ABC-20", "repo:api"), issue("ABC-23", "repo:api", "repo:web")] + code, _, err = self.run_main(["--chunk", "ABC-23", "ABC-12"], FakeEffects(), MODELS) + self.assertEqual(code, 1) + self.assertIn("ABC-23 is labelled with 2 picks", err) + self.issues = [self.issues[0]] + code, _, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], FakeEffects(git=UNREAD), MODELS) + self.assertEqual(code, 1) + self.assertIn("api", err) + self.issues = [issue("ABC-21", "do-epic-claude")] + self.description = "## Goals\nShip.\n" + code, _, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], FakeEffects(git=UNREAD), MODELS) + self.assertEqual(code, 1) + self.assertIn("the epic ABC-12's description has no text under a `## Audience` heading - no premises.\n", err) + + class WhatAWorkerJudgeAndHumanRead(DoEpicTest): def test_every_prompt_names_do_epic_and_never_loop_claude(self) -> None: cycle = ACycleOfAChunkWithoutARepo.cycle(self) # type: ignore[arg-type] diff --git a/bin/do_epic/test_scheduler.py b/bin/do_epic/test_scheduler.py index 9053f81b..a5e15582 100644 --- a/bin/do_epic/test_scheduler.py +++ b/bin/do_epic/test_scheduler.py @@ -7,9 +7,13 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from do_epic.scheduler import Chunk, CHUNK_FLAG, chunks, Epic, refusal, RepoRoom, start_chunks, Started, Waits +from do_epic import chunk_prompt from do_epic.backend.protocol import EpicIssue from failure import Failure from do_epic.epic_record import parallel_chunks_in +from loop_claude.cycle import prompts +from model_routing.route import Route +from model_routing.ladder import Rung from agent_cycle import tmux @@ -56,6 +60,10 @@ class WhichIssuesAreChunks(unittest.TestCase): Waits("ABC-23", "ABC-20 and XYZ-1 block it"), ])) + def test_what_blocks_a_split_item_blocks_its_sub_tasks(self) -> None: + found = chunks([issue("ABC-19", "repo:api"), issue("ABC-20", "split-by-repo", blockers=("ABC-19",)), issue("ABC-23", "repo:api", parent="ABC-20")]) + self.assertEqual(found, ([Chunk("ABC-19", "api")], [Waits("ABC-23", "ABC-19 blocks it")])) + class RoomInARepo(unittest.TestCase): def test_a_repo_with_room_and_no_loop_claude_takes_a_chunk(self) -> None: @@ -160,11 +168,51 @@ class StartingAnEpicsChunks(unittest.TestCase): self.assertTrue((self.home / ".local/state/loop-claude/api/starting").is_file()) self.assertTrue((self.home / ".local/state/do-epic/ABC-12/starting").is_file()) + def test_the_waits_keep_the_trackers_rank_order(self) -> None: + run = Tmux(("epic-ABC-12", "ABC-20", True, str(self.home / "code/api")), ("epic-ABC-12", "ABC-22", False, str(self.home / "code/api"))) + found = self.start(self.epic(1, api=1), [issue("ABC-20", "repo:api"), issue("ABC-21"), issue("ABC-22", "repo:api")], run, own="ABC-22") + self.assertEqual([wait.issue for wait in found.waits], ["ABC-20", "ABC-21"]) + + def test_a_chunks_window_never_marks_the_session_as_a_checkouts(self) -> None: + run = Tmux() + self.start(self.epic(2, api=1), [issue("ABC-20", "repo:api"), issue("ABC-21", "do-epic-claude")], run) + options = [args[-2] for args in run.calls if args[0] == "set-option"] + self.assertIn(tmux.ROOT, options) + self.assertNotIn(tmux.OWNER, options) + + def test_says_which_lock_it_could_not_take(self) -> None: + (self.home / ".local/state/do-epic").mkdir(parents=True) + (self.home / ".local/state/do-epic/ABC-12").write_text("", encoding="utf-8") + found = start_chunks(self.epic(1, api=1), [issue("ABC-20", "repo:api")], None, Tmux()) + assert isinstance(found, Failure) + self.assertRegex(found.reason, f"^cannot lock {self.home}/.local/state/do-epic/ABC-12(/starting)?: \\w") + + +class TheFirstPrompt(unittest.TestCase): + def test_the_task_names_the_checkout_and_branch_and_quotes_a_description_only_where_there_is_one(self) -> None: + self.assertEqual(chunk_prompt.task(issue("ABC-20"), "ABC-12", "/code/api"), "Work the Jira issue ABC-20 of the epic ABC-12 as one chunk, in the checkout /code/api, on the branch ABC-20. Its summary: Do ABC-20") + + def test_opens_with_the_task_then_the_rung_then_the_premises(self) -> None: + rest = f"The issue sets the chunk's scope. The goals below choose between ways to deliver that scope, and never widen it: the epic's first, then code health ({chunk_prompt.CODE_HEALTH}), then the repo's.\n\nFrom P" + start = f"T\n\nContinue any open draft PR or worktree for the chunk before creating a new one. {prompts.READING_RULE}\n\n" + self.assertEqual(chunk_prompt.fresh("T", "P", Route(Rung("off-the-ladder")), None, ""), start + rest) + routed = chunk_prompt.fresh("T", "P", Route(Rung("off-the-ladder"), 21, 3, 5), None, "") + self.assertTrue(routed.startswith(start + "do-epic runs you on off-the-ladder, routed by the issue's L of 3. Where a sentence below names item 21, it means the issue."), routed) + self.assertTrue(routed.endswith("\n\n" + rest), routed) + class WindowsAcrossSessions(unittest.TestCase): def test_lists_each_window_with_its_session_and_root(self) -> None: run = Tmux(("epic-ABC-12", "ABC-20 ?", False, "/code/api"), ("loop-api", "item-3", True, "")) self.assertEqual(tmux.everywhere(run), [tmux.Placed("epic-ABC-12", tmux.Listed("ABC-20", "@0", False), "/code/api"), tmux.Placed("loop-api", tmux.Listed("item-3", "@1", True), "")]) + self.assertEqual(run.calls, [["list-windows", "-a", "-F", f"#{{session_name}}\t#{{window_name}}\t#{{pane_dead}}\t#{{window_id}}\t#{{{tmux.ROOT}}}"]]) + + def test_without_a_tmux_server_there_are_none(self) -> None: + self.assertEqual(tmux.everywhere(lambda args: subprocess.CompletedProcess(args, 1, "no server running\n", "")), []) + + def test_a_started_chunk_joins_the_sessions_windows_as_running(self) -> None: + found = tmux.start_chunk(tmux.Window("epic-ABC-12", "ABC-20", ""), "/code/api", {}, ["do-epic"], [tmux.Listed("ABC-20", "@0", True), tmux.Listed("ABC-21", "@1", False)], False, Tmux(("epic-ABC-12", "ABC-20", True, "/code/api"), ("epic-ABC-12", "ABC-21", False, "/code/api"))) + self.assertEqual(found, [tmux.Listed("ABC-21", "@1", False), tmux.Listed("ABC-20", "", False)]) if __name__ == "__main__":