"""The next chunk's pick: the item the chunk takes first, weighed against the last release cut, and what the default branch holds for the pick.""" import dataclasses import re from git_repo import commits_since, file_at, git, newest_release_tag from failure import Failure from todo_table import Item, items, next_item, renames from markdown_text import sections @dataclasses.dataclass(frozen=True) class LastCut: """What the default branch holds about its last release, which chunk_item weighs the plan against.""" # The newest v* tag, None where the branch has none. tag: str | None | Failure # CHANGELOG.md's text, empty where the branch holds none. changelog: str | Failure # How many commits follow the tag, or make up the branch where it has none. commits: int | Failure # todo.md's text at the tag and at each later commit that changed it, oldest first, as named_releases reads them; empty without a tag. todos_since: list[str] | Failure @dataclasses.dataclass(frozen=True) class Next: """The plan's earliest release is the next release: the earliest after the last cut that todo.md has named since that cut. Without a cut, the plan's earliest release is the next.""" # None where the plan holds no item. release: tuple[int, int, int] | None # The item the next chunk takes first, next_item's pick; None where every item waits or the plan holds none. item: Item | None @dataclasses.dataclass(frozen=True) class Cut: """The plan's earliest release is the last cut's version or earlier.""" release: tuple[int, int, int] @dataclasses.dataclass(frozen=True) class Finished: """The next release holds no item, and changes are unreleased.""" # The next release, None where todo.md has named none since the cut. release: tuple[int, int, int] | None @dataclasses.dataclass(frozen=True) class Unplanned: """The next release holds no item, and no change is unreleased.""" # The next release, None where todo.md has named none since the cut. release: tuple[int, int, int] | None @dataclasses.dataclass(frozen=True) class Unknown: """todo.md's texts since the cut, or the changes since it, cannot be read.""" # How the plan stands to the last cut, the version x.y.z the newest v* tag names, with the item the next chunk takes first where there is one. Pick = Next | Cut | Finished | Unplanned | Unknown @dataclasses.dataclass(frozen=True) class DefaultPlan: """What the default branch, as last fetched, holds for chunk_item.""" # todo.md's text, empty where the branch holds none. todo: str | Failure cut: LastCut def default_plan(root: str, ref: str) -> DefaultPlan: """What ref, the default branch as last fetched, holds for the pick, read in the checkout at root.""" tag = newest_release_tag(ref, root) commits = tag if isinstance(tag, Failure) else commits_since(tag, ref, root) return DefaultPlan(file_at(ref, "todo.md", root), LastCut(tag, file_at(ref, "CHANGELOG.md", root), commits, todos_since(tag, ref, root))) def todos_since(tag: str | None | Failure, ref: str, cwd: str) -> list[str] | Failure: """LastCut.todos_since, read from ref in cwd. A pull request's own commits do not count, only the merge that landed it: --first-parent.""" if not isinstance(tag, str): return [] changed = git("log", "--first-parent", "--reverse", "--format=%H", f"{tag}..{ref}", "--", "todo.md", cwd=cwd) if isinstance(changed, Failure): return changed texts = [] for commit in [tag, *changed.split()]: text = file_at(commit, "todo.md", cwd) if isinstance(text, Failure): return text texts.append(text) return texts def chunk_item(found: list[Item], cut: LastCut) -> Pick: """The pick from todo.md's items, weighed against the last cut. Routing and chunk-preflight both pick with it, so they name one item.""" return standing(cut.tag, unreleased_changes(cut), min((item.release for item in found), default=None), next_item(found), named_releases(cut.todos_since)) def standing(tag: str | None | Failure, changes: int | Failure, earliest: tuple[int, int, int] | None, first: Item | None, named: set[tuple[int, int, int]] | Failure) -> Pick: """How the plan stands to tag, the newest v* tag. earliest is the plan's earliest release, None where the plan holds no item, and first is next_item's pick. changes counts the unreleased changes. named holds each release todo.md named since the tag. """ tagged = re.fullmatch(r"v([0-9]+)\.([0-9]+)\.([0-9]+)", tag) if isinstance(tag, str) else None if tagged is None: return Next(earliest, first) cut_version = tuple(int(part) for part in tagged.groups()) if earliest is not None and earliest <= cut_version: return Cut(earliest) if isinstance(named, Failure): return Unknown() first_named = min((release for release in named if release > cut_version), default=None) if earliest is not None and (first_named is None or earliest <= first_named): return Next(earliest, first) # The plan's earliest release is past the next release, or the plan holds none: the next release holds no item. if isinstance(changes, Failure): return Unknown() return Finished(first_named) if changes else Unplanned(first_named) def unreleased_changes(cut: LastCut) -> int | Failure: """How many changes are unreleased, as `~/.claude/CLAUDE.md` → "Working loop" counts them: the entries under ## Unreleased, or the commits since the tag where CHANGELOG.md has no such section or cannot be read.""" entries = unreleased_entries(cut.changelog) if isinstance(cut.changelog, str) else None return cut.commits if entries is None else entries def unreleased_entries(changelog: str) -> int | None: """How many top-level `- ` entries CHANGELOG.md holds under ## Unreleased; None without that section.""" found = sections(changelog).get("Unreleased") return None if found is None else sum(line.startswith("- ") for line in found[0].splitlines()) def named_releases(todos: list[str] | Failure) -> set[tuple[int, int, int]] | Failure: """Each release the todo.md texts, oldest first, file items under. A release leaves the set when a text marks it renamed, and returns when a later text files items under it again. A text whose items or marks do not parse names none, except the newest: where its marks do not parse, it returns that Failure, since the release they should have excluded would read as finished.""" if isinstance(todos, Failure): return todos if todos and isinstance(newest := renames(todos[-1]), Failure): return newest named: set[tuple[int, int, int]] = set() for text in todos: plan, marked = items(text), renames(text) if isinstance(plan, list) and isinstance(marked, set): named = (named - marked) | {item.release for item in plan} return named