diff --git a/bin/chunk_pick.py b/bin/chunk_pick.py index 95c6de2f..4eb3edb0 100644 --- a/bin/chunk_pick.py +++ b/bin/chunk_pick.py @@ -25,10 +25,10 @@ class Upcoming: """The next release, and whether work has landed towards it, as `~/.claude/CLAUDE.md` → "Working loop" uses both.""" # The earliest release after the last cut that todo.md has named since that cut or the plan holds; without a cut, the plan's earliest. None where there is none. release: tuple[int, int, int] | None + # Whether it is the plan's earliest release, as it always is without a cut, even where the plan holds no item. + is_earliest: bool # Whether work has landed towards it: one or more changes are unreleased, as unreleased_changes counts them. landed: bool | Failure - # The last cut's version x.y.z, None where the newest v* tag names none. - cut: tuple[int, int, int] | None @dataclasses.dataclass(frozen=True) @@ -102,38 +102,50 @@ def todos_since(tag: str | None | Failure, ref: str, cwd: str) -> list[str] | Fa 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.""" - earliest = min((item.release for item in found), default=None) - upcoming = next_release(cut, earliest) - return standing(upcoming, earliest, next_item(found)) if isinstance(upcoming, Upcoming) else upcoming + upcoming = next_release(found, cut) + return standing(upcoming, next_item(found)) if isinstance(upcoming, Upcoming) else upcoming -def next_release(cut: LastCut, earliest: tuple[int, int, int] | None) -> Upcoming | Cut | Unknown: - """The next release after cut, and whether work has landed towards it. earliest is the plan's earliest release, None where the plan holds no item. +def next_release(found: list[Item], cut: LastCut) -> Upcoming | Cut | Unknown: + """The next release after cut, and whether work has landed towards it, for the plan whose items are found. Cut where the plan files items under the last cut's version or earlier, and Unknown where todo.md's texts since the cut cannot be read. """ - tagged = re.fullmatch(r"v([0-9]+\.[0-9]+\.[0-9]+)", cut.tag) if isinstance(cut.tag, str) else None - changes = unreleased_changes(cut) - landed = changes if isinstance(changes, Failure) else changes > 0 - if tagged is None: - return Upcoming(earliest, landed, None) - cut_version = release_of(tagged[1]) + earliest = min((item.release for item in found), default=None) + cut_version = version_of(cut.tag) + if cut_version is None: + return Upcoming(earliest, True, landed(cut)) if earliest is not None and earliest <= cut_version: return Cut(earliest) named = named_releases(cut.todos_since) if isinstance(named, Failure): return Unknown() - after = {release for release in named if release > cut_version} | ({earliest} if earliest is not None else set()) - return Upcoming(min(after, default=None), landed, cut_version) + after = {release for release in named if release > cut_version} + if earliest is not None: + after.add(earliest) + release = min(after, default=None) + return Upcoming(release, earliest is not None and earliest == release, landed(cut)) -def standing(upcoming: Upcoming, earliest: tuple[int, int, int] | None, first: Item | None) -> Next | ReadyToCut | Unplanned | Unknown: - """How the plan stands to the next release, upcoming. earliest is the plan's earliest release, None where the plan holds no item, and first is next_item's pick. +def version_of(tag: str | None | Failure) -> tuple[int, int, int] | None: + """The version x.y.z that tag, the newest v* tag, names; None where it names none.""" + tagged = re.fullmatch(r"v([0-9]+\.[0-9]+\.[0-9]+)", tag) if isinstance(tag, str) else None + return None if tagged is None else release_of(tagged[1]) - Without a cut, or where the plan's earliest release is the next, the next chunk takes first. Else the next release holds no item, and is ready to cut once work has landed towards it, or else to plan. + +def landed(cut: LastCut) -> bool | Failure: + """Whether work has landed since cut: one or more changes are unreleased.""" + changes = unreleased_changes(cut) + return changes if isinstance(changes, Failure) else changes > 0 + + +def standing(upcoming: Upcoming, first: Item | None) -> Next | ReadyToCut | Unplanned | Unknown: + """How the plan stands to the next release, upcoming, with first, next_item's pick. + + Where the next release is the plan's earliest, the pick is Next, holding first. Else the next release holds no item: it is ready to cut once work has landed towards it, and else it is to plan. """ - if upcoming.cut is None or (earliest is not None and earliest == upcoming.release): - return Next(earliest, first) + if upcoming.is_earliest: + return Next(upcoming.release, first) if isinstance(upcoming.landed, Failure): return Unknown() return ReadyToCut(upcoming.release) if upcoming.landed else Unplanned(upcoming.release) diff --git a/bin/larv_review/plan.py b/bin/larv_review/plan.py index b064c8e1..6cccaf70 100644 --- a/bin/larv_review/plan.py +++ b/bin/larv_review/plan.py @@ -32,8 +32,8 @@ def work_landed(release: tuple[int, int, int], shipped: list[Item], landed: tupl def landed_towards(found: list[Item], cut: LastCut) -> tuple[int, int, int] | None | Failure: - """The next release where work has landed towards it on the default branch, as `chunk_pick.next_release` tells both; None where there is none, or work has not landed. found is the default branch's plan.""" - upcoming = next_release(cut, min((item.release for item in found), default=None)) + """The next release on the default branch, once work has landed towards it; None where there is none, or work has not landed. `chunk_pick.next_release` tells the next release and whether work landed. found is the default branch's plan.""" + upcoming = next_release(found, cut) if isinstance(upcoming, Unknown): return Failure("could not tell the next release: the default branch's todo.md history cannot be read") # Cut's release is the last cut's version or earlier, so no work lands towards it. diff --git a/bin/test_chunk_pick.py b/bin/test_chunk_pick.py index f7b6bb72..a11c2faa 100644 --- a/bin/test_chunk_pick.py +++ b/bin/test_chunk_pick.py @@ -1,4 +1,3 @@ -import dataclasses import pathlib import sys import unittest @@ -21,45 +20,50 @@ def naming(named: set[tuple[int, int, int]] | Failure) -> list[str] | Failure: return [HEADER + "".join(ROW.format(id=id_, release=todo_table.version(release)) for id_, release in enumerate(sorted(named), 1))] -def planned(earliest: tuple[int, int, int] | None) -> list[todo_table.Item]: - return [] if earliest is None else [todo_table.Item(1, earliest, "", "**Do 1.**", {}, (), ())] +def one(release: tuple[int, int, int]) -> todo_table.Item: + """The plan's only item, filed under release.""" + return todo_table.Item(1, release, "", "**Do 1.**", {}, (), ()) + + +def plan_under(earliest: tuple[int, int, int] | None) -> list[todo_table.Item]: + """A plan holding one item under earliest, or none where earliest is None.""" + return [] if earliest is None else [one(earliest)] class NextRelease(unittest.TestCase): def test_names_the_earliest_release_after_the_last_cut_that_todo_md_named_since_it_or_the_plan_holds(self): cases = ( - ("v1.2.3", (1, 3, 0), {(1, 2, 4), (1, 3, 0)}, chunk_pick.Upcoming((1, 2, 4), True, (1, 2, 3))), - ("v1.2.3", (1, 2, 4), {(1, 3, 0)}, chunk_pick.Upcoming((1, 2, 4), True, (1, 2, 3))), - ("v1.2.3", None, {(1, 2, 2), (1, 3, 0)}, chunk_pick.Upcoming((1, 3, 0), True, (1, 2, 3))), - ("v1.2.3", None, set(), chunk_pick.Upcoming(None, True, (1, 2, 3))), - (None, (0, 2, 0), set(), chunk_pick.Upcoming((0, 2, 0), True, None)), - ("v1.2.3-rc.1", (1, 2, 3), set(), chunk_pick.Upcoming((1, 2, 3), True, None)), - ("v1.2.3", (1, 2, 3), Failure("log unreadable"), chunk_pick.Cut((1, 2, 3))), - ("v1.2.3", (1, 3, 0), Failure("log unreadable"), chunk_pick.Unknown()), + ("v1.2.3", (1, 3, 0), {(1, 2, 4), (1, 3, 0)}, chunk_pick.Upcoming((1, 2, 4), False, True)), + ("v1.2.3", (1, 2, 4), {(1, 3, 0)}, chunk_pick.Upcoming((1, 2, 4), True, True)), + ("v1.2.3", None, {(1, 2, 2), (1, 3, 0)}, chunk_pick.Upcoming((1, 3, 0), False, True)), + ("v1.2.3", None, set(), chunk_pick.Upcoming(None, False, True)), + (None, (0, 2, 0), set(), chunk_pick.Upcoming((0, 2, 0), True, True)), + (None, None, set(), chunk_pick.Upcoming(None, True, True)), + ("v1.2.3-rc.1", (1, 2, 3), set(), chunk_pick.Upcoming((1, 2, 3), True, True)), ) for tag, earliest, named, upcoming in cases: with self.subTest(tag=tag, earliest=earliest, named=named): - self.assertEqual(chunk_pick.next_release(chunk_pick.LastCut(tag, "", 4, naming(named)), earliest), upcoming) + self.assertEqual(chunk_pick.next_release(plan_under(earliest), chunk_pick.LastCut(tag, "", 4, naming(named))), upcoming) def test_says_work_has_landed_towards_it_where_a_change_is_unreleased(self): cut = lambda changes: chunk_pick.LastCut("v1.2.3", "", changes, naming({(1, 3, 0)})) - self.assertEqual(chunk_pick.next_release(cut(4), (1, 3, 0)), chunk_pick.Upcoming((1, 3, 0), True, (1, 2, 3))) - self.assertEqual(chunk_pick.next_release(cut(0), (1, 3, 0)), chunk_pick.Upcoming((1, 3, 0), False, (1, 2, 3))) - self.assertEqual(chunk_pick.next_release(cut(Failure("count unreadable")), (1, 3, 0)), chunk_pick.Upcoming((1, 3, 0), Failure("count unreadable"), (1, 2, 3))) + self.assertEqual(chunk_pick.next_release([one((1, 3, 0))], cut(4)), chunk_pick.Upcoming((1, 3, 0), True, True)) + self.assertEqual(chunk_pick.next_release([one((1, 3, 0))], cut(0)), chunk_pick.Upcoming((1, 3, 0), True, False)) + self.assertEqual(chunk_pick.next_release([one((1, 3, 0))], cut(Failure("count unreadable"))), chunk_pick.Upcoming((1, 3, 0), True, Failure("count unreadable"))) class ChunkItem(unittest.TestCase): def test_takes_the_earliest_release_where_it_is_the_next_and_else_says_whether_the_next_is_ready_to_cut_or_to_plan(self): cases = ( - ("v1.2.3", 4, (1, 2, 4), set(), chunk_pick.Next((1, 2, 4), None)), - ("v1.2.3", 4, (1, 3, 0), {(1, 3, 0)}, chunk_pick.Next((1, 3, 0), None)), - ("v1.2.3", 4, (1, 2, 4), {(1, 3, 0)}, chunk_pick.Next((1, 2, 4), None)), - ("v1.2.3", 4, (1, 3, 0), {(1, 2, 3), (1, 3, 0)}, chunk_pick.Next((1, 3, 0), None)), - ("v0.2.0", 4, (0, 4, 0), set(), chunk_pick.Next((0, 4, 0), None)), - (None, 4, (0, 2, 0), set(), chunk_pick.Next((0, 2, 0), None)), + ("v1.2.3", 4, (1, 2, 4), set(), chunk_pick.Next((1, 2, 4), one((1, 2, 4)))), + ("v1.2.3", 4, (1, 3, 0), {(1, 3, 0)}, chunk_pick.Next((1, 3, 0), one((1, 3, 0)))), + ("v1.2.3", 4, (1, 2, 4), {(1, 3, 0)}, chunk_pick.Next((1, 2, 4), one((1, 2, 4)))), + ("v1.2.3", 4, (1, 3, 0), {(1, 2, 3), (1, 3, 0)}, chunk_pick.Next((1, 3, 0), one((1, 3, 0)))), + ("v0.2.0", 4, (0, 4, 0), set(), chunk_pick.Next((0, 4, 0), one((0, 4, 0)))), + (None, 4, (0, 2, 0), set(), chunk_pick.Next((0, 2, 0), one((0, 2, 0)))), (None, 4, None, set(), chunk_pick.Next(None, None)), - ("v1.2.3-rc.1", 4, (1, 2, 3), set(), chunk_pick.Next((1, 2, 3), None)), - (Failure("tags unreadable"), 4, (0, 2, 0), set(), chunk_pick.Next((0, 2, 0), None)), + ("v1.2.3-rc.1", 4, (1, 2, 3), set(), chunk_pick.Next((1, 2, 3), one((1, 2, 3)))), + (Failure("tags unreadable"), 4, (0, 2, 0), set(), chunk_pick.Next((0, 2, 0), one((0, 2, 0)))), ("v1.2.3", 4, (1, 2, 3), set(), chunk_pick.Cut((1, 2, 3))), ("v1.2.3", 4, (1, 2, 2), {(1, 2, 4)}, chunk_pick.Cut((1, 2, 2))), ("v1.2.3", 4, (1, 3, 0), {(1, 2, 4), (1, 3, 0)}, chunk_pick.ReadyToCut((1, 2, 4))), @@ -70,15 +74,13 @@ class ChunkItem(unittest.TestCase): ("v1.2.3", 0, (1, 3, 0), {(1, 2, 4)}, chunk_pick.Unplanned((1, 2, 4))), ("v1.2.3", 0, None, set(), chunk_pick.Unplanned(None)), ("v1.2.3", Failure("count unreadable"), None, set(), chunk_pick.Unknown()), - ("v1.2.3", Failure("count unreadable"), (1, 3, 0), {(1, 3, 0)}, chunk_pick.Next((1, 3, 0), None)), + ("v1.2.3", Failure("count unreadable"), (1, 3, 0), {(1, 3, 0)}, chunk_pick.Next((1, 3, 0), one((1, 3, 0)))), ("v1.2.3", 4, (1, 3, 0), Failure("log unreadable"), chunk_pick.Unknown()), ("v1.2.3", 4, (1, 2, 3), Failure("log unreadable"), chunk_pick.Cut((1, 2, 3))), ) for tag, changes, earliest, named, stands in cases: with self.subTest(tag=tag, changes=changes, earliest=earliest, named=named): - found = planned(earliest) - first = found[0] if earliest is not None and stands == chunk_pick.Next(earliest, None) else None - self.assertEqual(chunk_pick.chunk_item(found, chunk_pick.LastCut(tag, "", changes, naming(named))), dataclasses.replace(stands, item=first) if first else stands) + self.assertEqual(chunk_pick.chunk_item(plan_under(earliest), chunk_pick.LastCut(tag, "", changes, naming(named))), stands) def test_counts_the_changes_unreleased_since_the_last_cut_by_unreleased_entries_and_else_by_commits(self): changelog = "# Changelog\n\n## Unreleased\n\n- One.\n - A nested line.\n- Two.\n\n## 0.1.0\n\n- Old.\n"