diff --git a/bin/chunk_preflight/gathered.py b/bin/chunk_preflight/gathered.py index 83f82a4e..f7bc8786 100644 --- a/bin/chunk_preflight/gathered.py +++ b/bin/chunk_preflight/gathered.py @@ -4,16 +4,13 @@ import pathlib import re import typing +from todo_table import chunk_item, Item, item_details, items from failure import Failure -from todo_table import Item, item_details, items, next_item # The CLI that lists a forge's pull requests: gh for a github.com origin, tea for any other. Forge = typing.Literal["gh", "tea"] # The command each forge's CLI lists pull requests with; pulls_command adds the flags that pick their rows and fields. PULLS_LIST: dict[Forge, tuple[str, ...]] = {"gh": ("gh", "pr", "list"), "tea": ("tea", "pulls", "list")} -# How the plan's earliest release stands to the version x.y.z the newest tag names. "next": it is x.y.z+1, x.y+1.0 or x+1.0.0, or no tag names a version. "cut": it is x.y.z or earlier. -# Else the release after the tag holds no item: "finished" where commits followed the tag, "unplanned" where none did, and "unknown" where they cannot be counted. -Standing = typing.Literal["next", "cut", "finished", "unplanned", "unknown"] @dataclasses.dataclass(frozen=True) @@ -94,7 +91,7 @@ class Gathered: def planned(todo: str | Failure, release: Release, default: str) -> Plan | NoItem: - """The plan in todo, the default branch's todo.md, with the item the next chunk takes first, todo_table.next_item's pick, where standing finds the plan's earliest release next; else why there is none.""" + """The plan in todo, the default branch's todo.md, with the item the next chunk takes first, todo_table.chunk_item's pick, which routing makes too; else why there is none.""" where = f"{default}'s todo.md" if isinstance(todo, Failure): return NoItem(f"{where} cannot be read: {todo.reason}.") @@ -104,7 +101,7 @@ def planned(todo: str | Failure, release: Release, default: str) -> Plan | NoIte if found is None: return NoItem(f"{where} holds no items table.") earliest = min((item.release for item in found), default=None) - stands = standing(release, earliest) + stands, first = chunk_item(found, release.tag, release.commits) if earliest is not None and stands == "cut": return NoItem(f"{where} files items under release {version(earliest)}, which is not after the last cut, {release.tag}.") gap = f"{where} holds no item for the release after the last cut, {release.tag}, and" @@ -114,7 +111,6 @@ def planned(todo: str | Failure, release: Release, default: str) -> Plan | NoIte return NoItem(f"{gap} no commit has landed since, so that release is to be planned first.") if stands == "unknown": return NoItem(f"{gap} the commits since it cannot be counted.") - first = next_item(found) if first is None: return NoItem(f"every item of release {version(earliest)} in {where} waits." if earliest is not None else f"{where}'s items table holds no item.") details = item_details(todo) @@ -122,21 +118,6 @@ def planned(todo: str | Failure, release: Release, default: str) -> Plan | NoIte return Plan(found, first, f"{first.tagline}\n\n{detail}".strip()) -def standing(release: Release, earliest: tuple[int, int, int] | None) -> Standing: - """How the plan's earliest release, None where the plan holds no item, stands to the version the release's tag names, as Standing says.""" - cut = re.fullmatch(r"v([0-9]+)\.([0-9]+)\.([0-9]+)", release.tag) if isinstance(release.tag, str) else None - if cut is None: - return "next" - major, minor, patch = (int(part) for part in cut.groups()) - if earliest in ((major, minor, patch + 1), (major, minor + 1, 0), (major + 1, 0, 0)): - return "next" - if earliest is not None and earliest <= (major, minor, patch): - return "cut" - if isinstance(release.commits, Failure): - return "unknown" - return "finished" if release.commits else "unplanned" - - def version(release: tuple[int, int, int]) -> str: return ".".join(map(str, release)) diff --git a/bin/chunk_preflight/main.py b/bin/chunk_preflight/main.py index d78042cd..cc0bc3ee 100644 --- a/bin/chunk_preflight/main.py +++ b/bin/chunk_preflight/main.py @@ -44,7 +44,7 @@ import subprocess import sys import typing -from git_repo import commit_exists, commits_since, default_branch, default_branch_todo, file_at, git, newest_release_tag, top +from git_repo import commit_exists, default_branch, default_plan, file_at, git, top from failure import Failure from chunk_preflight.gathered import Forge, forge_of, Gathered, ItemFacts, named_paths, NamedPath, NoItem, Plan, planned, pulls_command, Ran, Release, Worktree, worktree_add, worktree_on from chunk_preflight.facts import render @@ -96,18 +96,15 @@ def preflight(root: pathlib.Path, timeout: float) -> list[str] | Failure: if isinstance(default, Failure): return default ref, note = default - cut = release(ref, root) - plan = planned(default_branch_todo(str(root), timeout), cut, ref) + held = default_plan(str(root), timeout) + if isinstance(held, Failure): + return held + cut = Release(held.tag, file_at(ref, "CHANGELOG.md", str(root)), held.commits) + plan = planned(held.todo, cut, ref) item = plan if isinstance(plan, NoItem) else item_facts(root, ref, plan, timeout) return render(Gathered(ref, note, cut, item)) -def release(default: str, root: pathlib.Path) -> Release: - tag = newest_release_tag(default) - commits = tag if isinstance(tag, Failure) else commits_since(tag, default) - return Release(tag, file_at(default, "CHANGELOG.md", str(root)), commits) - - def item_facts(root: pathlib.Path, default: str, plan: Plan, timeout: float) -> ItemFacts: worktree = item_worktree(root, plan.branch, default) path = worktree.path if isinstance(worktree, Worktree) else None diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index c515ccdb..48a641a6 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -9,7 +9,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure -from chunk_preflight.gathered import forge_of, Gathered, ItemFacts, named_paths, NamedPath, NoItem, planned, Ran, Release, standing, Worktree, worktree_add +from chunk_preflight.gathered import forge_of, Gathered, ItemFacts, named_paths, NamedPath, NoItem, planned, Ran, Release, Worktree, worktree_add from test_env import isolated_env from chunk_preflight.main import line_count, ran, seconds, TIMEOUT from chunk_preflight.facts import open_pulls, render, unreleased_entries @@ -387,31 +387,6 @@ class Plans(unittest.TestCase): self.assertEqual(planned(TODO[:TODO.index("## Details")], CUT, "origin/main").text, "**Do the next thing.**") -class Standing(unittest.TestCase): - def test_takes_the_earliest_release_where_it_is_the_next_patch_minor_or_major_or_no_tag_names_a_version(self): - cases = ( - (Release("v1.2.3", "", 4), (1, 2, 4), "next"), - (Release("v1.2.3", "", 4), (1, 3, 0), "next"), - (Release("v1.2.3", "", 4), (2, 0, 0), "next"), - (Release(None, "", 4), (0, 2, 0), "next"), - (Release(None, "", 4), None, "next"), - (Release("v1.2.3-rc.1", "", 4), (1, 2, 3), "next"), - (Release(Failure("tags unreadable"), "", 4), (0, 2, 0), "next"), - (Release("v1.2.3", "", 4), (1, 2, 3), "cut"), - (Release("v1.2.3", "", 4), (1, 2, 2), "cut"), - (Release("v1.2.3", "", 4), (1, 2, 5), "finished"), - (Release("v1.2.3", "", 4), (1, 3, 1), "finished"), - (Release("v1.2.3", "", 4), (2, 0, 1), "finished"), - (Release("v1.2.3", "", 4), None, "finished"), - (Release("v1.2.3", "", 0), (1, 4, 0), "unplanned"), - (Release("v1.2.3", "", 0), None, "unplanned"), - (Release("v1.2.3", "", Failure("count unreadable")), (1, 4, 0), "unknown"), - ) - for release, earliest, stands in cases: - with self.subTest(release=release, earliest=earliest): - self.assertEqual(standing(release, earliest), stands) - - class Gathering(unittest.TestCase): def test_counts_the_top_level_entries_under_unreleased(self): self.assertEqual(unreleased_entries(CHANGELOG), 2) diff --git a/bin/git_repo.py b/bin/git_repo.py index 20bee97c..85e5605e 100644 --- a/bin/git_repo.py +++ b/bin/git_repo.py @@ -1,4 +1,5 @@ """Run git, in the current directory or a given one, and read the checkout, its default branch and its files.""" +import dataclasses import re import subprocess @@ -60,22 +61,37 @@ def origin_head(cwd: str | None, timeout: float | None) -> str | Failure: return Failure("origin names no default branch; set it with `git remote set-head origin --auto`") -def default_branch_todo(root: str, timeout: float) -> str | Failure: - """todo.md on the default branch as last fetched, read in the checkout at root; empty where that branch holds none. Asking origin, where the checkout records no default branch for it, fails after timeout seconds.""" +@dataclasses.dataclass(frozen=True) +class DefaultPlan: + """What the default branch, as last fetched, holds for picking the next chunk's item: todo_table.chunk_item's inputs.""" + ref: str + # todo.md's text, empty where the branch holds none. + todo: str | Failure + # The newest v* tag on the branch, None where it has none. + tag: str | None | Failure + # How many commits follow the tag, or make up the branch where it has none. + commits: int | Failure + + +def default_plan(root: str, timeout: float) -> DefaultPlan | Failure: + """What the default branch as last fetched holds for the pick, read in the checkout at root. Asking origin, where the checkout records no default branch for it, fails after timeout seconds.""" ref = default_ref(root, timeout) - return ref if isinstance(ref, Failure) else file_at(ref, "todo.md", root) + if isinstance(ref, Failure): + return ref + tag = newest_release_tag(ref, root) + return DefaultPlan(ref, file_at(ref, "todo.md", root), tag, tag if isinstance(tag, Failure) else commits_since(tag, ref, root)) -def newest_release_tag(branch: str) -> str | None | Failure: - """The newest `v*` tag merged into branch, None when it has none.""" +def newest_release_tag(branch: str, cwd: str | None = None) -> str | None | Failure: + """The newest `v*` tag merged into branch, read in cwd, else in the current directory; None when it has none.""" # Sorts v1.0.0-rc.1 below v1.0.0. - tags = git("-c", "versionsort.suffix=-", "tag", "--list", "v*", "--merged", branch, "--sort=-v:refname") + tags = git("-c", "versionsort.suffix=-", "tag", "--list", "v*", "--merged", branch, "--sort=-v:refname", cwd=cwd) return tags if isinstance(tags, Failure) else next(iter(tags.split()), None) -def commits_since(tag: str | None, branch: str) -> int | Failure: - """How many commits branch holds after tag, or holds in all where tag is None.""" - count = git("rev-list", "--count", f"{tag}..{branch}" if tag else branch) +def commits_since(tag: str | None, branch: str, cwd: str | None = None) -> int | Failure: + """How many commits branch holds after tag, or holds in all where tag is None; read in cwd, else in the current directory.""" + count = git("rev-list", "--count", f"{tag}..{branch}" if tag else branch, cwd=cwd) return count if isinstance(count, Failure) else int(count) diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 25d0b542..a8919d2b 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -50,9 +50,10 @@ for the worker and the interrupt; with both set, nothing is asked. Where that model is on the routing ladder (bin/model_routing/ladder.py), the worker and the interrupt run on one of the ladder's rungs: a model at an effort. As a chunk starts, todo.md on the -default branch, as chunk-preflight has just fetched it, routes it: its earliest release's top item -that does not wait, the item the preflight states where that release is the next. An item waits -while it is marked question, or while an item that blocks it is still in the table. The item's L +default branch, as chunk-preflight has just fetched it, routes it on the item the preflight names +(todo_table.chunk_item): the earliest release's top item that does not wait, where that release is +the one after the newest v* tag, or there is no such tag. An item waits while it is marked +question, or while an item that blocks it is still in the table. The item's L picks the worker's rung, stepping from the model's cheapest rung by WORKER_STEPS in bin/model_routing/ladder.py, below it for a low L; without such an item, or with its L, E or I unscored, the chunk runs on the model's cheapest rung. The @@ -154,7 +155,7 @@ from loop_claude.loop.routing_check import check_routing from loop_claude.project.checkout import checkout_root from loop_claude.cycle.run import Cycle, run_cycle from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG -from git_repo import default_branch_todo +from git_repo import default_plan, DefaultPlan from loop_claude.effects import Effects from failure import Failure from loop_claude.cycle.judge import Finished, Unfinished @@ -213,7 +214,7 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc facts = "" if session_open(session) else chunk_facts(launcher.with_name("chunk-preflight"), loop_run.root, effects) anchor = project_rung(models.worker) root = loop_run.root - loop_run = routed(loop_run, anchor, lambda: archived_route(archive_folder, session), lambda: plan_item(default_branch_todo(root, ORIGIN_TIMEOUT))) + loop_run = routed(loop_run, anchor, lambda: archived_route(archive_folder, session), lambda: plan_item(default_plan(root, ORIGIN_TIMEOUT))) route = loop_run.route or Route(Rung(models.worker)) print(f"\n=== cycle {loop_run.cycle_number}, worker on {route.rung} ===", flush=True) verdict = run_cycle(Cycle(anchor=anchor, effects=effects, facts=facts, judge_model=models.judge, paths=paths, premises=premises, project_archive=archive_folder, records=records, root=loop_run.root, route=route, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence)) @@ -269,9 +270,14 @@ def chunk_facts(preflight: pathlib.Path, root: str, effects: Effects) -> str: return prompts.preflight_facts(done.stdout.strip()) -def plan_item(todo: str | Failure) -> Item | None: - """The item that routes a chunk, from todo.md as git_repo.default_branch_todo reads it, as chunk-preflight does, so both take todo_table.next_item's pick. None without one, so the chunk runs on the project's rung.""" - item = todo if isinstance(todo, Failure) else routed_item(todo) +def plan_item(plan: DefaultPlan | Failure) -> Item | None: + """The item that routes a chunk, from the default branch as git_repo.default_plan reads it, as chunk-preflight does, so both take todo_table.chunk_item's pick. None without one, so the chunk runs on the project's rung.""" + if isinstance(plan, Failure): + item: Item | Failure | None = plan + elif isinstance(plan.todo, Failure): + item = plan.todo + else: + item = routed_item(plan.todo, plan.tag, plan.commits) if isinstance(item, Failure): print(f"routing on the project's rung: {item.reason}", file=sys.stderr) return None diff --git a/bin/loop_claude/routing.py b/bin/loop_claude/routing.py index 92ba42ac..06224c2f 100644 --- a/bin/loop_claude/routing.py +++ b/bin/loop_claude/routing.py @@ -1,9 +1,9 @@ """Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`.""" import dataclasses +from todo_table import chunk_item, Item, items from model_routing.ladder import climbed, highest, LADDER, position, project_rung, review_floor, Rung, stepped_up, worker_rung from failure import Failure -from todo_table import Item, items, next_item from model_routing.route import Route UNFINISHED_PER_STEP = 2 @@ -13,12 +13,12 @@ ESCALATION_CAP = 2 JUDGE_REPLAYED = ("claude-opus-5-5",) -def routed_item(todo: str) -> Item | Failure | None: - """The item todo_table.next_item names; None when there is none, or it lacks L, E or I.""" +def routed_item(todo: str, tag: str | None | Failure, commits: int | Failure) -> Item | Failure | None: + """The item todo_table.chunk_item picks from todo.md, given the newest v* tag and the commits since it; None when there is none, or it lacks L, E or I.""" found = items(todo) if isinstance(found, Failure): return found - takes = next_item(found or []) + _, takes = chunk_item(found or [], tag, commits) return takes if takes and takes.likelihood and takes.escape_and_impact else None diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index c5329bc0..0c546bdc 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -18,6 +18,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from routing_figures import archive from loop_claude.project import checkout, model_choice, records +from git_repo import DefaultPlan from loop_claude.effects import Effects, PYTHON_TIMEOUT, run_python from claude_code import entries, reply, session from failure import Failure @@ -587,24 +588,32 @@ class Routing(unittest.TestCase): SONNET = Rung("claude-sonnet-5-5", "high") def test_routes_on_the_next_releases_top_item_that_waits_on_no_question(self): - self.assertEqual(routing.routed_item(TODO).id, 3) - self.assertIsNone(routing.routed_item(TODO.replace("| | **Do", "| question | **Do"))) - self.assertIsNone(routing.routed_item("# todo\n")) - self.assertIsNone(routing.routed_item(TODO[:TODO.index("| 7 |")])) + self.assertEqual(routing.routed_item(TODO, None, 0).id, 3) + self.assertIsNone(routing.routed_item(TODO.replace("| | **Do", "| question | **Do"), None, 0)) + self.assertIsNone(routing.routed_item("# todo\n", None, 0)) + self.assertIsNone(routing.routed_item(TODO[:TODO.index("| 7 |")], None, 0)) old_shape = "## Items\n\n| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 3 | 0.2.0 | | **Do the next thing.** | 5 | 3 | 6 | 8 | 9 | 1 | 25 |\n" - self.assertIsNone(routing.routed_item(old_shape)) - self.assertEqual(routing.routed_item(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), Failure("item 3's L 'x' is not a whole number from 1 to 4")) + self.assertIsNone(routing.routed_item(old_shape, None, 0)) + self.assertEqual(routing.routed_item(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), None, 0), Failure("item 3's L 'x' is not a whole number from 1 to 4")) + + def test_routes_on_no_item_where_the_release_after_the_last_cut_has_none(self): + self.assertEqual(routing.routed_item(TODO, "v0.1.0", 3).id, 3) + self.assertIsNone(routing.routed_item(TODO, "v0.2.0", 3)) + later_only = TODO[:TODO.index("| 7 |")] + TODO[TODO.index("| 1 | 0.3.0 |"):] + self.assertIsNone(routing.routed_item(later_only, "v0.1.0", 3)) + self.assertIsNone(routing.routed_item(later_only, "v0.1.0", 0)) + self.assertEqual(routing.routed_item(later_only, "v0.2.0", 3).id, 1) def test_routes_past_an_item_a_waiting_item_blocks(self): todo = "## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n| 8 | 0.2.0 | 7 | | **Use the answer.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 9 | 0.2.0 | 8 | | **Build on it.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 3 | 0.2.0 | | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 |\n" - self.assertEqual(routing.routed_item(todo).id, 3) - self.assertEqual(routing.routed_item(todo.replace("| question |", "| |")).id, 7) + self.assertEqual(routing.routed_item(todo, None, 0).id, 3) + self.assertEqual(routing.routed_item(todo.replace("| question |", "| |"), None, 0).id, 7) first = "| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n" - self.assertEqual(routing.routed_item(todo.replace(first, "") + first).id, 3) + self.assertEqual(routing.routed_item(todo.replace(first, "") + first, None, 0).id, 3) def test_starts_a_chunk_on_the_rung_its_items_l_picks_below_the_projects_too(self): - self.assertEqual(routing.first_route(self.OPUS, routing.routed_item(TODO)), Route(self.HIGH, 3, 4, 5)) - cheap = routing.routed_item(TODO.replace("| 4 | 2 | 3 |", "| 1 | 2 | 3 |")) + self.assertEqual(routing.first_route(self.OPUS, routing.routed_item(TODO, None, 0)), Route(self.HIGH, 3, 4, 5)) + cheap = routing.routed_item(TODO.replace("| 4 | 2 | 3 |", "| 1 | 2 | 3 |"), None, 0) self.assertEqual(routing.first_route(self.OPUS, cheap), Route(Rung("claude-haiku-5-5", "max"), 3, 1, 5)) self.assertEqual(routing.first_route(self.OPUS, None), Route(self.OPUS)) @@ -679,7 +688,7 @@ class Routing(unittest.TestCase): def test_routes_nothing_on_an_item_without_e_or_i(self): without_i = TODO.replace("| E | I | R |", "| E | R |").replace("|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|").replace("| 4 | 3 | 5 | 10 |", "| 4 | 3 | 10 |").replace("| 4 | 2 | 3 | 7 |", "| 4 | 2 | 7 |").replace("| 1 | 1 | 1 | 1 |", "| 1 | 1 | 1 |") - self.assertIsNone(routing.routed_item(without_i)) + self.assertIsNone(routing.routed_item(without_i, None, 0)) def test_archives_the_route_in_a_line_it_reads_back(self): route = Route(self.HIGH, 3, 4, 5) @@ -705,7 +714,7 @@ class Routing(unittest.TestCase): def test_keeps_a_chunks_route_or_its_archived_one_at_or_above_the_rung_its_item_picks_and_else_routes_afresh(self): def run_with(route=None): return main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=route) - item = routing.routed_item(TODO) + item = routing.routed_item(TODO, None, 0) archived = Route(self.XHIGH, 3, 4, 5) def never(): raise AssertionError("not asked") @@ -721,12 +730,14 @@ class Routing(unittest.TestCase): class RouteSources(unittest.TestCase): def test_routes_on_the_plan_item_and_says_why_it_routes_on_the_projects_rung(self): - self.assertIsNone(main.plan_item("")) - self.assertEqual(main.plan_item(TODO).id, 3) - for todo, said in ((TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (Failure("git show origin/main:todo.md printed text that is not UTF-8"), "routing on the project's rung: git show origin/main:todo.md printed text that is not UTF-8\n")): + self.assertIsNone(main.plan_item(DefaultPlan("origin/main", "", None, 0))) + self.assertEqual(main.plan_item(DefaultPlan("origin/main", TODO, "v0.1.0", 3)).id, 3) + self.assertIsNone(main.plan_item(DefaultPlan("origin/main", TODO, "v0.2.0", 3))) + unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8") + for plan, said in ((DefaultPlan("origin/main", TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), None, 0), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan("origin/main", unreadable, None, 0), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")): with self.subTest(said=said): with contextlib.redirect_stderr(io.StringIO()) as stderr: - self.assertIsNone(main.plan_item(todo)) + self.assertIsNone(main.plan_item(plan)) self.assertTrue(stderr.getvalue().startswith(said), stderr.getvalue()) def test_resumes_on_the_archived_route_and_says_why_it_routes_afresh(self): diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index ae5d1ef9..f01c1e3c 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -415,6 +415,31 @@ class Blockers(unittest.TestCase): self.assertEqual(todo_table.items(f"## Items\n\n{header}"), Failure(f"the table under {header} has no |---| divider line")) +class Standing(unittest.TestCase): + def test_takes_the_earliest_release_where_it_is_the_next_patch_minor_or_major_or_no_tag_names_a_version(self): + cases = ( + ("v1.2.3", 4, (1, 2, 4), "next"), + ("v1.2.3", 4, (1, 3, 0), "next"), + ("v1.2.3", 4, (2, 0, 0), "next"), + (None, 4, (0, 2, 0), "next"), + (None, 4, None, "next"), + ("v1.2.3-rc.1", 4, (1, 2, 3), "next"), + (Failure("tags unreadable"), 4, (0, 2, 0), "next"), + ("v1.2.3", 4, (1, 2, 3), "cut"), + ("v1.2.3", 4, (1, 2, 2), "cut"), + ("v1.2.3", 4, (1, 2, 5), "finished"), + ("v1.2.3", 4, (1, 3, 1), "finished"), + ("v1.2.3", 4, (2, 0, 1), "finished"), + ("v1.2.3", 4, None, "finished"), + ("v1.2.3", 0, (1, 4, 0), "unplanned"), + ("v1.2.3", 0, None, "unplanned"), + ("v1.2.3", Failure("count unreadable"), (1, 4, 0), "unknown"), + ) + for tag, commits, earliest, stands in cases: + with self.subTest(tag=tag, commits=commits, earliest=earliest): + self.assertEqual(todo_table.standing(tag, commits, earliest), stands) + + class Main(unittest.TestCase): def run_main(self, *argv): with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said: diff --git a/bin/todo_table.py b/bin/todo_table.py index 6e797230..c41376cf 100644 --- a/bin/todo_table.py +++ b/bin/todo_table.py @@ -17,6 +17,9 @@ BLOCKED_BY = "Blocked by" MOVES = "Moves" # How the items table's header opens, which tells it from any other table in todo.md. HEADER_START = "| ID | Release |" +# How the plan's earliest release stands to the version x.y.z the newest tag names. "next": it is x.y.z+1, x.y+1.0 or x+1.0.0, or no tag names a version. "cut": it is x.y.z or earlier. +# Else the release after the tag holds no item: "finished" where commits followed the tag, "unplanned" where none did, and "unknown" where they cannot be counted. +Standing = typing.Literal["next", "cut", "finished", "unplanned", "unknown"] # todo.md's ## Details section, its body the first group. DETAILS = re.compile(r"^## Details\n(.*?)(?=^## |\Z)", re.M | re.S) # Each goal an item names, in its order, with how far the item moves it; None in OLD_SHAPE, whose Goals cell names no move. @@ -173,6 +176,27 @@ def next_item(found: list[Item]) -> Item | None: return next((item for item in found if item.release == release and item.id not in waits), None) +def standing(tag: str | None | Failure, commits: int | Failure, earliest: tuple[int, int, int] | None) -> Standing: + """How the plan's earliest release, None where the plan holds no item, stands to the newest v* tag, as Standing says; commits counts those after the tag.""" + cut = re.fullmatch(r"v([0-9]+)\.([0-9]+)\.([0-9]+)", tag) if isinstance(tag, str) else None + if cut is None: + return "next" + major, minor, patch = (int(part) for part in cut.groups()) + if earliest in ((major, minor, patch + 1), (major, minor + 1, 0), (major + 1, 0, 0)): + return "next" + if earliest is not None and earliest <= (major, minor, patch): + return "cut" + if isinstance(commits, Failure): + return "unknown" + return "finished" if commits else "unplanned" + + +def chunk_item(found: list[Item], tag: str | None | Failure, commits: int | Failure) -> tuple[Standing, Item | None]: + """The item the next chunk takes first, next_item's pick, where standing finds the plan's earliest release the next; else None. Routing and chunk-preflight both pick with it, so they name one item; standing comes with it, so a caller can say why there is none.""" + stands = standing(tag, commits, min((item.release for item in found), default=None)) + return stands, next_item(found) if stands == "next" else None + + def bundle_candidates(found: list[Item], first: Item) -> list[Item]: """The items of first's release, in the table's order, that a chunk taking first may take too: those that do not wait once the chunk's own items count as done.""" chunk = {item.id for item in found if item.release == first.release}