import contextlib import dataclasses import io import json import pathlib import shutil import subprocess import sys sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.doubles import configured from do_epic.test_main import Description, DoEpicTest, structured as says from do_epic.backend.protocol import EpicIssue from do_epic.epic_record import EpicRecord from failure import Failure from loop_effects.doubles import FakeEffects from do_epic.test_scheduler import issue from claude_code.reply import Limited from do_epic.config import load from do_epic.backend.fake import MemoryEpic from plan.todo_table import parse_moves from do_epic import planning RANKED = "## Goals\n1. **Achieve:** Ship the import.\n\n## Audience\nThe client's team.\n\n## Scoring\n`Score = V`\n" LIMIT = {"type": "result", "is_error": True, "api_error_status": 429, "result": "You've hit your limit"} def scored(key: str, moves: str, *labels: str, blockers: tuple[str, ...] = (), by_loop: bool = True) -> EpicIssue: """An open issue the loop filed, holding scores and moving the goals moves names.""" parsed = parse_moves(key, moves) assert not isinstance(parsed, Failure), parsed return dataclasses.replace(issue(key, *labels, blockers=blockers), scores={"L": 1, "E": 1, "I": 1, "S": 1}, moves=parsed, by_loop=by_loop) class Planning(DoEpicTest): def plan(self, fake: FakeEffects) -> tuple[list[EpicIssue] | Failure | Limited, MemoryEpic, str]: """What planning answers for self.issues, the store it changed and what it printed on stdout. self.asked holds what it asked the human, on stderr.""" config = load(self.home) assert not isinstance(config, Failure), config store = MemoryEpic("ABC-12", list(self.issues)) epic = planning.Planned("ABC-12", config, store, EpicRecord(Description(self.description)), self.home, fake.effects()) # type: ignore[arg-type] out, self.asked = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(self.asked): read = store.read() assert not isinstance(read, Failure) issues, stopped = planning.planned(epic, read) return issues if stopped is None else stopped, store, out.getvalue() def labels(self, store: MemoryEpic) -> dict[str, tuple[str, ...]]: return {each.key: each.labels for each in store.issues} def test_an_issue_a_label_places_is_planned_without_claude(self) -> None: self.issues = [issue("ABC-20", "repo:api"), issue("ABC-21", "do-epic-claude"), issue("ABC-22", "split-by-repo"), issue("ABC-23", done=True)] fake = FakeEffects() found, _, out = self.plan(fake) self.assertEqual((found, out, fake.made("claude")), (self.issues, "", [])) def test_labels_an_unplaced_issue_with_the_repo_claude_picks_on_the_repoless_models_rung(self) -> None: self.issues = [issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": [{"repo": "api", "change": "Add the endpoint."}]}), says({"pick": "repo", "repo": "api"})]) found, store, out = self.plan(fake) self.assertEqual(found, [issue("ABC-20", "repo:api")]) self.assertEqual(self.labels(store), {"ABC-20": ("repo:api",)}) self.assertIn("planning ABC-20\n", out) self.assertIn("ABC-20 is labelled repo:api\n", out) for args, _ in fake.made("claude"): self.assertEqual(args[args.index("--model") + 1], "claude-opus-5-5") self.assertIn("A label that equals a repo's name names that repo.", fake.made("claude")[1][1]) def test_labels_work_without_a_repo_as_claudes_or_a_humans(self) -> None: for pick, label in (("claude", "do-epic-claude"), ("human", "do-epic-human")): with self.subTest(pick=pick): self.issues = [issue("ABC-20")] _, store, _ = self.plan(FakeEffects(claude=[says({"parts": []}), says({"pick": pick})])) self.assertEqual(self.labels(store), {"ABC-20": (label,)}) def test_splits_an_issue_that_changes_several_repos_into_a_sub_task_for_each_and_picks_no_repo_for_it(self) -> None: self.issues = [issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": [{"repo": "api", "change": "Add the endpoint."}, {"repo": "web", "change": "Call it."}], "comment": "Split by repo: api and web."})]) found, store, out = self.plan(fake) assert not isinstance(found, Failure | Limited), found self.assertEqual([(each.key, each.parent, each.labels) for each in found], [("ABC-20", "ABC-12", ("split-by-repo",)), ("ABC-101", "ABC-20", ("repo:api",)), ("ABC-102", "ABC-20", ("repo:web",))]) # A person filed ABC-20, so the team is told. self.assertEqual(store.comments, {"ABC-20": ["Split by repo: api and web."]}) self.assertIn("ABC-20 is split into a sub-task for each of api, web\n", out) self.assertIn("in English, that tells the team why it is split by repo", fake.made("claude")[0][1]) def test_plans_the_issue_after_one_that_is_split(self) -> None: self.issues = [issue("ABC-20"), issue("ABC-21")] parts = says({"parts": [{"repo": "api", "change": "Add the endpoint."}, {"repo": "web", "change": "Call it."}], "comment": "Split by repo: api and web."}) _, store, _ = self.plan(FakeEffects(claude=[parts, says({"parts": []}), says({"pick": "human"})])) self.assertEqual(self.labels(store)["ABC-21"], ("do-epic-human",)) def test_writes_to_the_team_in_the_epics_language(self) -> None: self.description = "**Language:** Swedish\n" self.issues = [issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"})]) self.plan(fake) self.assertIn("in Swedish, that tells the team why it is split by repo", fake.made("claude")[0][1]) def test_each_claude_call_runs_in_the_epics_state_folder(self) -> None: self.issues = [issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"})]) effects, folders = fake.effects(), [] def claude(args: object, cwd: str, stdin: object) -> object: folders.append(cwd) return effects.claude(args, cwd, stdin) # type: ignore[arg-type] config = load(self.home) epic = planning.Planned("ABC-12", config, MemoryEpic("ABC-12", list(self.issues)), EpicRecord(Description("")), self.home, dataclasses.replace(effects, claude=claude)) # type: ignore[arg-type] with contextlib.redirect_stdout(io.StringIO()): planning.planned(epic, list(self.issues)) folder = self.home / ".local" / "state" / "do-epic" / "ABC-12" self.assertEqual((folders, folder.is_dir()), ([str(folder)] * 2, True)) def test_a_model_off_the_routing_ladder_has_no_rung(self) -> None: found = planning.planning_rung("claude-nope-1") assert isinstance(found, Failure), found self.assertIn("`repoless-model` names claude-nope-1, which is not on the routing ladder ", found.reason) def test_fails_where_a_labelled_repo_has_no_checkout_to_read(self) -> None: self.issues = [issue("ABC-19", "repo:ghost"), issue("ABC-20")] fake = FakeEffects() found, _, _ = self.plan(fake) assert isinstance(found, Failure), found self.assertIn("ghost", found.reason) self.assertEqual(fake.made("claude"), []) def test_the_split_reads_the_checkout_of_each_repo_a_label_of_the_epic_names(self) -> None: checkout = self.home / "code" / "api" configured(self, checkout, home=self.home) self.issues = [issue("ABC-19", "repo:api", done=True), issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"})]) self.plan(fake) args = fake.made("claude")[0][0] self.assertEqual(args[args.index("--add-dir") + 1], str(checkout)) def test_asks_the_human_for_the_label_of_an_issue_the_rule_leaves_unclear_until_one_is_typed(self) -> None: self.issues = [issue("ABC-20")] fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "unclear", "question": "Is the importer in api or in web?"})], inputs=["api", "a b", " repo:web "]) found, store, out = self.plan(fake) self.assertEqual(found, [issue("ABC-20", "repo:web")]) self.assertIn("ABC-20, Do ABC-20: Is the importer in api or in web?\n", self.asked.getvalue()) self.assertIn("`api` is none of those labels; the repo api's label is `repo:api`\n", self.asked.getvalue()) self.assertIn("`a b` is none of those labels\n", self.asked.getvalue()) self.assertEqual(fake.made("input"), ["The label that says where ABC-20 is worked, `repo:`, `do-epic-claude` or `do-epic-human`, or Enter alone to leave it waiting: "] * 3) def unclear_then_claudes(self, **fake: object) -> tuple[dict[str, tuple[str, ...]], str]: """The labels and the output after planning ABC-20, which the rule leaves unclear, and ABC-21, which is Claude's, on a FakeEffects made with fake.""" self.issues = [issue("ABC-20"), issue("ABC-21")] unclear = says({"pick": "unclear", "question": "Is the importer in api or in web?"}) _, store, out = self.plan(FakeEffects(claude=[says({"parts": []}), unclear, says({"parts": []}), says({"pick": "claude"})], **fake)) # type: ignore[arg-type] return self.labels(store), out def test_an_unclear_issue_waits_off_a_terminal_and_the_next_is_planned(self) -> None: labels, out = self.unclear_then_claudes(on_terminal=False) self.assertEqual(labels, {"ABC-20": (), "ABC-21": ("do-epic-claude",)}) self.assertNotIn("ABC-20 is labelled", out) self.assertIn("ABC-20 waits for your answer: Is the importer in api or in web? Run `do-epic ABC-12` on a terminal to answer, or label the issue in Jira.\n", out) def test_an_unclear_issue_waits_where_the_human_answers_with_enter_alone(self) -> None: labels, out = self.unclear_then_claudes(inputs=[" "]) self.assertEqual((labels, "ABC-20 is labelled" in out), ({"ABC-20": (), "ABC-21": ("do-epic-claude",)}, False)) def test_an_unclear_issue_waits_where_the_terminals_input_ends(self) -> None: labels, out = self.unclear_then_claudes(inputs=[EOFError()]) self.assertEqual((labels, "ABC-20 is labelled" in out), ({"ABC-20": (), "ABC-21": ("do-epic-claude",)}, False)) def test_clones_a_labelled_repo_without_a_checkout_beside_the_origin_of_the_others(self) -> None: configured(self, self.home / "code" / "api", home=self.home) configured(self, self.home / "code" / "web", home=self.home) shutil.rmtree(self.home / "code" / "web") self.issues = [issue("ABC-18", "repo:api", done=True), issue("ABC-19", "repo:web", done=True), issue("ABC-20")] fake = FakeEffects(git=["https://git.example/acme/api.git\n", ""], claude=[says({"parts": []}), says({"pick": "claude"})]) self.plan(fake) origin, clone = fake.made("git") self.assertEqual((origin, clone[-4:]), (["remote", "get-url", "origin"], ["clone", "--", "https://git.example/acme/web", str(self.home / "code" / "web")])) def health_first(self, floor: str) -> list[str]: """The order of ABC-20, which moves the epic's goal by 9, and ABC-21, which moves code health by 3 in a repo that scored 7.5 and whose config ends with the line floor.""" checkout = self.home / "code" / "api" folder = configured(self, checkout, home=self.home) with (folder / "config.toml").open("a", encoding="utf-8") as config: config.write(floor) (checkout / "README.md").write_text("## Goals\n\n1. **Achieve:** Serve the API.\n\n## Audience\nIts callers.\n", encoding="utf-8") (checkout / "docs").mkdir() (checkout / "docs" / "comprehension-history.md").write_text("# Comprehension history\n\n## 2026-10-08T12:00:00Z, PR #1 at aaaaaaa\n\n| Seat | Overall |\n|---|---|\n| Mid A | 6 |\n| Mean | 7.5 |\n", encoding="utf-8") self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 9", "do-epic-claude"), scored("ABC-21", "code health by 3", "repo:api")] found = self.plan(FakeEffects())[0] assert not isinstance(found, Failure | Limited), found return [each.key for each in found] def test_code_health_weighs_four_times_in_a_repo_under_the_floor_its_config_sets(self) -> None: # 4 * 3 * 0.97 is above the 9 of the epic's first goal. self.assertEqual(self.health_first("readability-floor = 8.0\n"), ["ABC-21", "ABC-20"]) def test_code_health_weighs_once_in_a_repo_at_its_floor(self) -> None: # 3 * 0.97 is under the 9 of the epic's first goal. self.assertEqual(self.health_first(""), ["ABC-20", "ABC-21"]) def test_an_issue_claude_fails_on_is_named_and_the_next_is_planned(self) -> None: self.issues = [issue("ABC-20"), issue("ABC-21")] fake = FakeEffects(claude=[subprocess.CompletedProcess([], 1, "", ""), says({"parts": []}), says({"pick": "claude"})]) _, store, out = self.plan(fake) self.assertEqual(self.labels(store), {"ABC-20": (), "ABC-21": ("do-epic-claude",)}) self.assertIn("ABC-20 is not planned: the repo split exited 1\n", out) def test_a_usage_limit_ends_the_planning(self) -> None: self.issues = [issue("ABC-20"), issue("ABC-21")] fake = FakeEffects(claude=[subprocess.CompletedProcess([], 0, json.dumps(LIMIT), "")]) found, _, _ = self.plan(fake) self.assertIsInstance(found, Limited) self.assertEqual(len(fake.made("claude")), 1) def test_re_ranks_the_loops_scored_issues_by_score_with_a_blocker_first_and_leaves_every_other_issue_in_place(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude", by_loop=False), scored("ABC-22", "epic 1 by 5", "do-epic-claude", blockers=("ABC-24",)), dataclasses.replace(issue("ABC-23", "do-epic-claude"), by_loop=True), scored("ABC-24", "epic 1 by 3", "do-epic-claude")] found, store, out = self.plan(FakeEffects()) assert not isinstance(found, Failure | Limited), found # ABC-22 scores highest, and ABC-24 blocks it. ABC-21 is a person's, and nobody scored ABC-23. self.assertEqual([each.key for each in found], ["ABC-24", "ABC-21", "ABC-22", "ABC-23", "ABC-20"]) self.assertIn("re-ranking the issues do-epic filed in ABC-12's backlog\n", out) def test_ranks_an_issue_in_a_repo_by_that_repos_goals_after_the_epics(self) -> None: checkout = self.home / "code" / "api" configured(self, checkout, home=self.home) (checkout / "README.md").write_text("## Goals\n\n**Void:** code health, since it is a one-off script.\n\n1. **Achieve:** Serve the API.\n\n## Audience\nIts callers.\n", encoding="utf-8") self.description = RANKED self.issues = [scored("ABC-20", "repo 1 by 9", "repo:api"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] found, _, _ = self.plan(FakeEffects()) assert not isinstance(found, Failure | Limited), found self.assertEqual([each.key for each in found], ["ABC-21", "ABC-20"]) def test_issues_already_in_their_order_are_not_ranked_again(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 9", "do-epic-claude"), scored("ABC-21", "epic 1 by 2", "do-epic-claude")] found, _, out = self.plan(FakeEffects()) self.assertEqual((found, out), (self.issues, "")) def test_fails_where_the_epic_states_no_formula_to_rank_two_scored_issues_by(self) -> None: self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] found, store, _ = self.plan(FakeEffects()) self.assertEqual(found, Failure("the epic ABC-12's description states `Score = ...` 0 times under ## Scoring; state the formula that ranks its issues once, on a line of its own and inside backticks, such as `Score = 100 * V / (2**S + 32)`")) self.assertEqual([each.key for each in store.issues], ["ABC-20", "ABC-21"]) def test_fails_where_the_formula_names_a_score_no_issue_holds(self) -> None: self.description = RANKED.replace("Score = V", "Score = Q") self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] self.assertIsInstance(self.plan(FakeEffects())[0], Failure) def test_fails_where_two_issues_to_rank_share_a_number(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("XYZ-20", "epic 1 by 9", "do-epic-claude")] self.assertEqual(self.plan(FakeEffects())[0], Failure("ABC-20 and XYZ-20 share a number in different Jira projects, so the issues do-epic filed in the epic ABC-12 cannot be ranked; move one of the two to the other's Jira project")) def test_fails_where_the_description_cannot_be_read_to_rank_by(self) -> None: self.description = Failure("Jira answered 503") self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] self.assertEqual(self.plan(FakeEffects())[0], Failure("the epic ABC-12's description: Jira answered 503")) def test_fails_naming_the_repo_whose_goals_cannot_be_read(self) -> None: checkout = self.home / "code" / "api" configured(self, checkout, home=self.home) (checkout / "README.md").unlink(missing_ok=True) self.description = RANKED self.issues = [scored("ABC-20", "repo 1 by 9", "repo:api"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] found = self.plan(FakeEffects())[0] assert isinstance(found, Failure), found self.assertRegex(found.reason, r"^api: .*missing or empty") def test_ranks_a_security_issue_first(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 9", "do-epic-claude"), dataclasses.replace(scored("ABC-21", "epic 1 by 2", "do-epic-claude"), exempt="security")] found = self.plan(FakeEffects())[0] assert not isinstance(found, Failure | Limited), found self.assertEqual([each.key for each in found], ["ABC-21", "ABC-20"]) def test_one_scored_issue_needs_no_formula(self) -> None: self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), issue("ABC-21", "do-epic-claude")] self.assertEqual(self.plan(FakeEffects())[0], self.issues) def planned_on(self, store: MemoryEpic, fake: FakeEffects) -> tuple[list[EpicIssue], Failure | Limited | None]: """What planning answers for self.issues on a store that doesn't read as MemoryEpic does.""" config = load(self.home) assert not isinstance(config, Failure), config epic = planning.Planned("ABC-12", config, store, EpicRecord(Description(self.description)), self.home, fake.effects()) # type: ignore[arg-type] with contextlib.redirect_stdout(io.StringIO()): return planning.planned(epic, list(self.issues)) def test_a_label_it_added_counts_where_the_tracker_cannot_be_read_afterwards(self) -> None: self.issues = [issue("ABC-20")] found = self.planned_on(Unreadable("ABC-12", list(self.issues)), FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"})])) self.assertEqual(found, ([issue("ABC-20", "do-epic-claude")], Failure("Jira answered 503"))) def test_a_label_and_an_order_it_wrote_count_where_the_trackers_search_lags(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9"), issue("ABC-22")] parts = says({"parts": [{"repo": "api", "change": "Add the endpoint."}, {"repo": "web", "change": "Call it."}], "comment": "Split by repo: api and web."}) found, stopped = self.planned_on(Lagging("ABC-12", list(self.issues)), FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"}), parts])) self.assertEqual(([(each.key, each.labels) for each in found], stopped), ([("ABC-21", ("do-epic-claude",)), ("ABC-20", ("do-epic-claude",)), ("ABC-22", ("split-by-repo",))], None)) def test_keeps_the_order_and_says_why_where_the_tracker_refuses_the_ranking(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")] store = MemoryEpic("ABC-12", list(self.issues)) store.fails = "Jira answered 503" found, stopped = self.planned_on(store, FakeEffects()) assert isinstance(stopped, Failure), stopped self.assertEqual(([each.key for each in found], "Jira answered 503" in stopped.reason), (["ABC-20", "ABC-21"], True)) def test_an_issue_without_a_pick_is_not_ranked(self) -> None: self.description = RANKED self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "code health by 9")] found, _, out = self.plan(FakeEffects(claude=[subprocess.CompletedProcess([], 1, "", "")])) self.assertEqual((found, "re-ranking" in out), (self.issues, False)) class Unreadable(MemoryEpic): """A tracker that fails each read once an issue is labelled.""" def add_label(self, key: str, label: str) -> None | Failure: self.fails = "Jira answered 503" return None class Lagging(MemoryEpic): """A tracker whose search shows the issues as they were before any write.""" def read(self) -> list[EpicIssue] | Failure: return list(self.before) def __init__(self, key: str, issues: list[EpicIssue]) -> None: super().__init__(key, issues) self.before = list(issues)