"""The next chunk's pick: the item it takes first, weighed against the last release cut, and what the default branch holds for it.""" import dataclasses import re import typing 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 # How the plan's earliest release stands to the last cut, the version x.y.z the newest v* tag names. Standing = typing.Literal[ # It is the next release: the earliest after x.y.z that todo.md has named since the cut, or there is no cut to weigh it against. "next", # It is x.y.z or earlier. "cut", # The next release holds no item, and work has landed since the cut. "finished", # The next release holds no item, and no work has landed since the cut. "unplanned", # todo.md's texts since the cut, or the changes since it, cannot be read. "unknown", ] @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 Pick: """How the plan stands to the last cut, the next release, and the item the next chunk takes first.""" standing: Standing # None where standing is cut or unknown, or todo.md names no next release. release: tuple[int, int, int] | None # next_item's pick where standing is next; else None. item: Item | None @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.""" stands, release = standing(cut.tag, unreleased_changes(cut), min((item.release for item in found), default=None), named_releases(cut.todos_since)) return Pick(stands, release, next_item(found) if stands == "next" else None) def standing(tag: str | None | Failure, changes: int | Failure, earliest: tuple[int, int, int] | None, named: set[tuple[int, int, int]] | Failure) -> tuple[Standing, tuple[int, int, int] | None]: """How earliest, the plan's earliest release, None where the plan holds no item, stands to tag, the newest v* tag; and the next release, as Pick.release says. changes counts the changes since the tag, and named holds each release todo.md named since it.""" cut = re.fullmatch(r"v([0-9]+)\.([0-9]+)\.([0-9]+)", tag) if isinstance(tag, str) else None if cut is None: return "next", earliest version = tuple(int(part) for part in cut.groups()) if earliest is not None and earliest <= version: return "cut", None if isinstance(named, Failure): return "unknown", None upcoming = min({release for release in named if release > version} | ({earliest} if earliest is not None else set()), default=None) if earliest is not None and earliest == upcoming: return "next", earliest if isinstance(changes, Failure): return "unknown", None return ("finished" if changes else "unplanned"), upcoming 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.""" 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: its marks failing to parse fails the whole, since the release they should have excluded would read as finished. larv_review/plan.py's `emptied` skips the releases marked renamed too.""" 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