570 lines
34 KiB
Python
570 lines
34 KiB
Python
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"
|
|
GOALS = "## 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"
|
|
ANSWER = {"L": 2, "E": 1, "I": 2, "S": 3, "moves": "epic 1 by 9", "comment": "Scored to decide what starts first."}
|
|
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, parent: str = "ABC-12") -> 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, parent=parent), 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)
|
|
self.epic = epic
|
|
issues, stopped = planning.planned(epic, read)
|
|
return issues if stopped is None else stopped, store, out.getvalue()
|
|
|
|
def rule(self) -> None:
|
|
"""Writes the rule every plan item is scored by."""
|
|
(self.home / planning.RULE).parent.mkdir(parents=True, exist_ok=True)
|
|
(self.home / planning.RULE).write_text("L is how much the item changes.\n", encoding="utf-8")
|
|
|
|
def repo(self, name: str) -> None:
|
|
"""Checks out the repo called name, whose goals file voids code health and states one achieve goal."""
|
|
checkout = self.home / "code" / name
|
|
configured(self, checkout, home=self.home)
|
|
(checkout / "README.md").write_text(GOALS, encoding="utf-8")
|
|
|
|
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.description = RANKED
|
|
self.repo("api")
|
|
self.issues = [scored("ABC-20", "epic 1 by 9", "repo:api"), scored("ABC-21", "epic 1 by 2", "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_an_epic_shaped_as_a_teams_is_planned_without_claude_and_without_a_write(self) -> None:
|
|
with (self.home / ".claude-projects/do-epic/config.toml").open("a", encoding="utf-8") as config:
|
|
config.write('\n[label-repos]\ntwu-4 = "tenant-web-ui-4"\ncore-4 = "core-4"\n')
|
|
self.description = RANKED
|
|
self.repo("tenant-web-ui-4")
|
|
self.repo("core-4")
|
|
self.issues = [
|
|
scored("ABC-19", "epic 1 by 5", "twu-4", by_loop=False),
|
|
issue("ABC-20"),
|
|
scored("ABC-23", "epic 1 by 5", "core-4", by_loop=False, parent="ABC-20"),
|
|
scored("ABC-24", "epic 1 by 5", "twu-4", by_loop=False, parent="ABC-20"),
|
|
issue("ABC-21", "urgent"),
|
|
scored("ABC-25", "epic 1 by 5", "core-4", by_loop=False, parent="ABC-21"),
|
|
]
|
|
fake = FakeEffects()
|
|
found, store, out = self.plan(fake)
|
|
self.assertEqual((found, store.issues, out, fake.made("claude")), (self.issues, 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."})])
|
|
_, store, out = self.plan(fake)
|
|
self.assertEqual([(each.key, each.parent, each.labels) for each in store.issues], [("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_a_failed_runs_sub_task_under_the_teams_label_is_not_filed_again_and_another_label_is_no_pick(self) -> None:
|
|
with (self.home / ".claude-projects/do-epic/config.toml").open("a", encoding="utf-8") as config:
|
|
config.write('\n[label-repos]\nbackend = "api"\n')
|
|
configured(self, self.home / "code" / "api", home=self.home)
|
|
self.issues = [issue("ABC-20", "urgent"), dataclasses.replace(issue("ABC-23", "backend", parent="ABC-20"), by_loop=True), issue("ABC-21", "urgent")]
|
|
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"})]))
|
|
read = store.read()
|
|
assert not isinstance(read, Failure)
|
|
self.assertEqual(([task.labels for task in read if task.parent == "ABC-20"], self.labels(store)["ABC-21"]), ([("backend",), ("repo:web",)], ("do-epic-human", "urgent")))
|
|
|
|
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_for_one_of_the_teams_labels_too_and_takes_it(self) -> None:
|
|
with (self.home / ".claude-projects/do-epic/config.toml").open("a", encoding="utf-8") as config:
|
|
config.write('\n[label-repos]\nbackend = "api"\n')
|
|
self.issues = [issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "unclear", "question": "Is the importer in api or in web?"})], inputs=["backend"])
|
|
found, _, _ = self.plan(fake)
|
|
self.assertEqual((found, fake.made("input")), ([issue("ABC-20", "backend")], ["The label that says where ABC-20 is worked, `repo:<name>`, `do-epic-claude` or `do-epic-human` (or the team's `backend`), or Enter alone to leave it waiting: "]))
|
|
|
|
def configure_team(self, *above: str) -> None:
|
|
"""Gives the config the team's label `backend` for api, and the settings in above."""
|
|
path = self.home / ".claude-projects/do-epic/config.toml"
|
|
path.write_text("".join(f"{line}\n" for line in above) + path.read_text(encoding="utf-8") + '\n[label-repos]\nbackend = "api"\n', encoding="utf-8")
|
|
|
|
def test_files_sub_tasks_under_the_teams_label_and_summary_template(self) -> None:
|
|
self.configure_team('sub-task-summary = "{label}: {Summary}"')
|
|
self.issues = [issue("ABC-20")]
|
|
parts = says({"parts": [{"repo": "api", "change": "Add the endpoint."}, {"repo": "web", "change": "Call it."}], "comment": "Delat per repo."})
|
|
_, store, _ = self.plan(FakeEffects(claude=[parts]))
|
|
self.assertEqual([(each.summary, each.labels) for each in store.issues if each.parent == "ABC-20"], [("backend: Do ABC-20", ("backend",)), ("web: Do ABC-20", ("repo:web",))])
|
|
|
|
def test_records_the_teams_label_for_a_repo_the_human_types_by_name_and_names_it(self) -> None:
|
|
self.configure_team()
|
|
self.issues = [issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "unclear", "question": "api or web?"})], inputs=["api", "backend"])
|
|
found, _, _ = self.plan(fake)
|
|
self.assertEqual(found, [issue("ABC-20", "backend")])
|
|
self.assertIn("the repo api's label is `backend`", self.asked.getvalue())
|
|
|
|
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:<name>`, `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-human"), 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 ABC-23 is a human's task, which holds no scores.
|
|
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:
|
|
self.repo("api")
|
|
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_orders_issues_of_two_jira_projects_that_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")]
|
|
found = self.plan(FakeEffects())[0]
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertEqual(planning.start_order(self.epic, found), ["XYZ-20", "ABC-20"])
|
|
|
|
def test_issues_that_score_the_same_start_in_the_trackers_order(self) -> None:
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-30", "epic 1 by 5", "do-epic-claude", by_loop=False), scored("ABC-20", "epic 1 by 5", "do-epic-claude", by_loop=False)]
|
|
self.plan(FakeEffects())
|
|
self.assertEqual(planning.start_order(self.epic, self.issues), ["ABC-30", "ABC-20"])
|
|
|
|
def test_names_the_issue_by_its_key_where_the_order_of_score_fails(self) -> None:
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-30", "epic 1 by 5", "do-epic-claude", by_loop=False), scored("ABC-20", "epic 7 by 5", "do-epic-claude", by_loop=False)]
|
|
self.plan(FakeEffects())
|
|
self.assertEqual(planning.start_order(self.epic, self.issues), Failure("ABC-20 names epic 7, which the epic's ## Goals does not number"))
|
|
|
|
def test_quotes_a_formula_that_names_an_item_as_the_epic_states_it(self) -> None:
|
|
self.description = RANKED.replace("Score = V", "Score = item 1")
|
|
self.issues = [scored("ABC-30", "epic 1 by 5", "do-epic-claude", by_loop=False), scored("ABC-20", "epic 1 by 5", "do-epic-claude", by_loop=False)]
|
|
self.plan(FakeEffects())
|
|
failed = planning.start_order(self.epic, self.issues)
|
|
assert isinstance(failed, Failure), failed
|
|
self.assertIn("item 1", failed.reason)
|
|
self.assertNotIn("ABC-30", failed.reason)
|
|
|
|
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-human")]
|
|
self.assertEqual(self.plan(FakeEffects())[0], self.issues)
|
|
|
|
def test_scores_each_bare_issue_whoever_filed_it_and_tells_its_team_without_ranking_a_persons(self) -> None:
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude", by_loop=False), issue("ABC-21", "do-epic-claude")]
|
|
# An earlier run made the folder the scoring runs in.
|
|
(self.home / planning.STATE / "ABC-12").mkdir(parents=True, exist_ok=True)
|
|
fake = FakeEffects(claude=[says(ANSWER)])
|
|
found, store, out = self.plan(fake)
|
|
assert not isinstance(found, Failure | Limited), found
|
|
# ABC-20's scores stay, and a person filed both, so neither changes its position in the backlog.
|
|
self.assertEqual([(each.key, each.scores) for each in found], [("ABC-20", {"L": 1, "E": 1, "I": 1, "S": 1}), ("ABC-21", {"L": 2, "E": 1, "I": 2, "S": 3})])
|
|
self.assertEqual(store.writes, ["score ABC-21 L 2, E 1, I 2, S 3, Moves epic 1 by 9, with the comment: Scored to decide what starts first."])
|
|
self.assertEqual(out, "<1790000000> scoring ABC-21\n<1790000000> ABC-21 is scored L 2, E 1, I 2, S 3, Moves epic 1 by 9\n")
|
|
self.assertEqual(planning.start_order(self.epic, found), ["ABC-21", "ABC-20"])
|
|
(asked,) = fake.made("claude")
|
|
for part in ("L is how much the item changes.", "Write the comment in English", "An item moves only these: `epic 1`.", "## Item ABC-21: Do ABC-21", "Labels: do-epic-claude"):
|
|
self.assertIn(part, asked[1])
|
|
self.assertIn("--json-schema", asked[0])
|
|
|
|
def test_scores_no_human_task_and_no_issue_that_is_the_only_one_to_start(self) -> None:
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-human")]
|
|
fake = FakeEffects()
|
|
found, store, out = self.plan(fake)
|
|
self.assertEqual((found, store.writes, out, fake.made("claude")), (self.issues, [], "", []))
|
|
|
|
def test_scores_no_issue_whose_work_has_landed(self) -> None:
|
|
with (self.home / ".claude-projects/do-epic/config.toml").open("a", encoding="utf-8") as config:
|
|
config.write('\njira-unblocking-statuses = ["Release Ready"]\n')
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [issue("ABC-20", "do-epic-claude", status="release ready"), issue("ABC-21", "do-epic-claude"), scored("ABC-22", "epic 1 by 2", "do-epic-claude")]
|
|
fake = FakeEffects(claude=[says(ANSWER)])
|
|
found, store, _ = self.plan(fake)
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertEqual([write.split(" L ")[0] for write in store.writes], ["score ABC-21"])
|
|
self.assertEqual(planning.start_order(self.epic, found), ["ABC-21", "ABC-22"])
|
|
|
|
def test_names_an_issue_whose_scoring_fails_and_scores_the_next(self) -> None:
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-claude"), issue("ABC-22", "do-epic-claude"), issue("ABC-23", "do-epic-claude")]
|
|
fake = FakeEffects(claude=[says({"L": "big"}), says(ANSWER | {"moves": "epic 7 by 5"}), says(ANSWER | {"L": 99}), says(ANSWER)])
|
|
found, store, out = self.plan(fake)
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertIn('ABC-20 is not scored: the scoring answered {"L": "big"}; expected a whole number for each of L, E, I, S, the moves as text and a comment\n', out)
|
|
self.assertIn("ABC-21 is not scored: ABC-21 names epic 7, which the epic's ## Goals does not number\n", out)
|
|
self.assertIn("ABC-22 is not scored: ", out)
|
|
self.assertEqual([each.key for each in found if each.scores], ["ABC-23"])
|
|
self.assertEqual([write.split(" L ")[0] for write in store.writes], ["score ABC-23"])
|
|
# An issue without scores starts after each that holds them, in the backlog's order.
|
|
self.assertEqual(planning.start_order(self.epic, found), ["ABC-23"])
|
|
|
|
def test_names_each_issue_whose_goals_cannot_be_read_and_asks_claude_about_none(self) -> None:
|
|
self.rule()
|
|
self.description = RANKED.replace("1. **Achieve:** ", "")
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-claude")]
|
|
fake = FakeEffects()
|
|
out = self.plan(fake)[2]
|
|
self.assertIn("ABC-20 is not scored: the epic's ## Goals has no numbered goal\n", out)
|
|
self.assertIn("ABC-21 is not scored: the epic's ## Goals has no numbered goal\n", out)
|
|
self.assertEqual(fake.made("claude"), [])
|
|
|
|
def test_the_scoring_call_runs_in_the_epics_state_folder(self) -> None:
|
|
self.rule()
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-claude")]
|
|
effects, folders = FakeEffects(claude=[says(ANSWER), says(ANSWER)]).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(RANKED)), self.home, dataclasses.replace(effects, claude=claude)) # type: ignore[arg-type]
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
planning.planned(epic, list(self.issues))
|
|
self.assertEqual(folders, [str(self.home / ".local" / "state" / "do-epic" / "ABC-12")] * 2)
|
|
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-claude")]
|
|
fake = FakeEffects(claude=[subprocess.CompletedProcess([], 0, json.dumps(LIMIT), "")])
|
|
found, store, _ = self.plan(fake)
|
|
self.assertIsInstance(found, Limited)
|
|
self.assertEqual((store.writes, len(fake.made("claude"))), ([], 1))
|
|
|
|
def test_names_a_score_the_tracker_refuses(self) -> None:
|
|
self.rule()
|
|
self.description = RANKED
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-human"), scored("ABC-22", "epic 1 by 2", "do-epic-claude")]
|
|
store = MemoryEpic("ABC-12", list(self.issues))
|
|
store.fails = "Jira answered 503"
|
|
out = io.StringIO()
|
|
config = load(self.home)
|
|
assert not isinstance(config, Failure), config
|
|
epic = planning.Planned("ABC-12", config, store, EpicRecord(Description(self.description)), self.home, FakeEffects(claude=[says(ANSWER)]).effects()) # type: ignore[arg-type]
|
|
with contextlib.redirect_stdout(out):
|
|
found, stopped = planning.scored(epic, list(self.issues))
|
|
self.assertEqual((found, stopped, out.getvalue()), (self.issues, None, "<1790000000> scoring ABC-20\n<1790000000> ABC-20 is not scored: Jira answered 503\n"))
|
|
|
|
def test_scores_no_issue_without_the_scoring_rule_or_the_epics_formula(self) -> None:
|
|
self.issues = [issue("ABC-20", "do-epic-claude"), issue("ABC-21", "do-epic-claude")]
|
|
self.description = RANKED
|
|
fake = FakeEffects()
|
|
found, _, _ = self.plan(fake)
|
|
assert isinstance(found, Failure), found
|
|
self.assertIn(f"cannot read the scoring rule {self.home}/.claude/rules/todo-items.md, so no issue is scored: ", found.reason)
|
|
self.rule()
|
|
self.description = "## Goals\n1. **Achieve:** Ship the import.\n\n## Audience\nThe client's team.\n\n## Scoring\n"
|
|
found, _, _ = self.plan(fake)
|
|
assert isinstance(found, Failure), found
|
|
self.assertIn("states `Score = ...` 0 times under ## Scoring", found.reason)
|
|
self.assertEqual(fake.made("claude"), [])
|
|
|
|
def test_scores_a_sub_task_on_its_own_against_its_repos_goals_in_the_epics_language(self) -> None:
|
|
self.rule()
|
|
self.repo("api")
|
|
self.description = RANKED + "\n**Language:** Swedish\n"
|
|
self.issues = [issue("ABC-20", "split-by-repo"), issue("ABC-30", "repo:api", parent="ABC-20"), scored("ABC-21", "epic 1 by 2", "do-epic-claude")]
|
|
fake = FakeEffects(claude=[says(ANSWER | {"moves": "repo 1 by 4"})])
|
|
found, store, _ = self.plan(fake)
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertEqual(store.writes, ["score ABC-30 L 2, E 1, I 2, S 3, Moves repo 1 by 4, with the comment: Scored to decide what starts first."])
|
|
(asked,) = fake.made("claude")
|
|
for part in ("The goals of the repo api, each named `repo <its number>`:", "Serve the API.", "An item moves only these: `epic 1`, `repo 1`.", "Write the comment in Swedish"):
|
|
self.assertIn(part, asked[1])
|
|
self.assertNotIn("`code health` is the goal", asked[1])
|
|
|
|
def test_starts_what_blocks_an_issue_or_its_parent_before_it_and_a_split_blockers_sub_tasks_too(self) -> None:
|
|
self.repo("api")
|
|
self.description = RANKED
|
|
self.issues = [
|
|
scored("ABC-20", "epic 1 by 9", "do-epic-claude", blockers=("ABC-21",)),
|
|
dataclasses.replace(issue("ABC-21", "split-by-repo"), blockers=("ABC-22",)),
|
|
scored("ABC-30", "epic 1 by 5", "repo:api", parent="ABC-21"),
|
|
scored("ABC-22", "epic 1 by 1", "do-epic-claude"),
|
|
]
|
|
found, _, _ = self.plan(FakeEffects())
|
|
assert not isinstance(found, Failure | Limited), found
|
|
# By score alone the order is ABC-20, ABC-30, ABC-22.
|
|
self.assertEqual(planning.start_order(self.epic, found), ["ABC-22", "ABC-30", "ABC-20"])
|
|
|
|
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)
|