diff --git a/.gitignore b/.gitignore index bacea1ec..74797a35 100644 --- a/.gitignore +++ b/.gitignore @@ -18,11 +18,11 @@ !agents/*.md !bin/ !bin/chunk-preflight +!bin/chunk_pick.py !bin/chunk_preflight/ !bin/chunk_preflight/*.py !bin/claude_code/ !bin/claude_code/*.py -!bin/chunk_pick.py !bin/comprehension_history.py !bin/failure.py !bin/file_writes.py diff --git a/bin/chunk_pick.py b/bin/chunk_pick.py index 186c49b1..e06fc11a 100644 --- a/bin/chunk_pick.py +++ b/bin/chunk_pick.py @@ -1,28 +1,12 @@ """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.""" @@ -37,15 +21,43 @@ class LastCut: @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. +class Next: + """The plan's earliest release is the next release: the earliest after the last cut that todo.md has named since it, or there is no cut to weigh it against.""" + # None where the plan holds no item. release: tuple[int, int, int] | None - # next_item's pick where standing is next; else 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 work has landed since the cut.""" + # 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 work has landed since the cut.""" + # 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.""" @@ -79,26 +91,25 @@ 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.""" - 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) + 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, 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: earliest where it is next, else the earliest release todo.md named since the cut, None for cut and unknown. 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 +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 earliest, the plan's earliest release, None where the plan holds no item, stands to tag, the newest v* tag. first is the earliest release's first item that does not wait, changes counts the changes since the tag, and named holds each release todo.md named since it.""" + 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", None - first_named = min((release for release in named if release > version), default=None) + 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 + return Next(earliest, first) if isinstance(changes, Failure): - return "unknown", None - return ("finished" if changes else "unplanned"), first_named + return Unknown() + return Finished(first_named) if changes else Unplanned(first_named) def unreleased_changes(cut: LastCut) -> int | Failure: diff --git a/bin/chunk_preflight/choices.py b/bin/chunk_preflight/choices.py index 30e6c479..d15ae2ae 100644 --- a/bin/chunk_preflight/choices.py +++ b/bin/chunk_preflight/choices.py @@ -2,7 +2,7 @@ import pathlib import re -from chunk_pick import chunk_item, LastCut +from chunk_pick import chunk_item, Cut, Finished, LastCut, Unknown, Unplanned from failure import Failure from chunk_preflight.gathered import Forge, NoItem, Plan from todo_table import item_details, items, version @@ -21,20 +21,19 @@ def planned(todo: str | Failure, cut: LastCut, default: str) -> Plan | NoItem: return NoItem(f"{where} cannot be read: {found.reason}.") if found is None: return NoItem(f"{where} holds no items table.") - earliest = min((item.release for item in found), default=None) pick = chunk_item(found, cut) - if earliest is not None and pick.standing == "cut": - return NoItem(f"{where} files items under release {version(earliest)}, which is not after the last cut, {cut.tag}; ask the human which release they belong to.") - upcoming = f"release {version(pick.release)}, the next after the last cut, {cut.tag}" if pick.release else f"the next release after the last cut, {cut.tag}" - if pick.standing == "finished": - return NoItem(f"{where} holds no item for {upcoming}, and work has landed since, so that release is finished and ready to cut, and the working loop stops here. If todo.md renamed that release instead, add `- renamed to ` to todo.md's `## Renamed` section.") - if pick.standing == "unplanned": - return NoItem(f"{where} holds no item for {upcoming}, and no work has landed since, so that release is to be planned first.") - if pick.standing == "unknown": + if isinstance(pick, Cut): + return NoItem(f"{where} files items under release {version(pick.release)}, which is not after the last cut, {cut.tag}; ask the human which release they belong to.") + if isinstance(pick, Unknown): return NoItem(f"{where} may hold no item for the next release after the last cut, {cut.tag}: todo.md's texts since then, or the count of changes since then, cannot be read. Find the next release yourself.") + if isinstance(pick, Finished | Unplanned): + upcoming = f"release {version(pick.release)}, the next after the last cut, {cut.tag}" if pick.release else f"the next release after the last cut, {cut.tag}" + if isinstance(pick, Finished): + return NoItem(f"{where} holds no item for {upcoming}, and work has landed since, so that release is finished and ready to cut, and the working loop stops here. If todo.md renamed that release instead, add `- renamed to ` to todo.md's `## Renamed` section.") + return NoItem(f"{where} holds no item for {upcoming}, and no work has landed since, so that release is to be planned first.") first = pick.item 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.") + return NoItem(f"every item of release {version(pick.release)} in {where} waits." if pick.release is not None else f"{where}'s items table holds no item.") details = item_details(todo) detail = details.get(first.id, "") if isinstance(details, dict) else f"(todo.md's details cannot be read: {details.reason})" return Plan(found, first, f"{first.tagline}\n\n{detail}".strip()) diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index c169baa6..07d91a54 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -422,20 +422,21 @@ class Plans(unittest.TestCase): self.assertNotIn("no items table", empty) def test_finds_the_release_after_the_last_cut_finished_or_to_be_planned_where_the_plan_holds_no_item_for_it(self): + named = "release 0.2.0, the next after the last cut, v0.1.0" + unnamed = "the next release after the last cut, v0.1.0" cases = ( - (LATER, 4, "finished and ready to cut"), - (LATER, 0, "planned first"), - (EMPTY, 4, "finished and ready to cut"), - (EMPTY, 0, "planned first"), - (LATER, Failure("count unreadable"), "cannot be read"), + (LATER, 4, [TODO], "finished and ready to cut", named), + (LATER, 0, [TODO], "planned first", named), + (EMPTY, 4, [TODO], "finished and ready to cut", named), + (EMPTY, 0, [TODO], "planned first", named), + (EMPTY, 4, [EMPTY], "finished and ready to cut", unnamed), + (LATER, Failure("count unreadable"), [TODO], "cannot be read", unnamed), ) - for todo, commits, fact in cases: - with self.subTest(todo=todo, commits=commits): - why = planned(todo, LastCut("v0.1.0", "", commits, [TODO]), "origin/main").why + for todo, commits, todos, fact, release in cases: + with self.subTest(todo=todo, commits=commits, todos=todos): + why = planned(todo, LastCut("v0.1.0", "", commits, todos), "origin/main").why self.assertIn(fact, why) - self.assertIn("v0.1.0", why) - if fact != "cannot be read": - self.assertIn("release 0.2.0", why) + self.assertIn(release, why) def test_says_what_todo_md_named_since_the_last_cut_cannot_be_read(self): self.assertIn("cannot be read", planned(LATER, LastCut("v0.1.0", "", 4, Failure("log unreadable")), "origin/main").why) diff --git a/bin/larv_review/plan.py b/bin/larv_review/plan.py index 4a801125..b8e69753 100644 --- a/bin/larv_review/plan.py +++ b/bin/larv_review/plan.py @@ -3,7 +3,7 @@ import pathlib from larv_review.git import committed_since_release from failure import Failure from git_repo import file_at, top -from todo_table import Item, items, renames +from todo_table import Item, items, renames, version def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: bool, renamed: set[tuple[int, int, int]]) -> list[str] | Failure: @@ -20,7 +20,7 @@ def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], lan for release in emptied(before, after, shipped, renamed): if any(item.release == release for item in shipped) or (release == next_release and landed): # SKILL.md → "Second run" keys on this wording. - reasons.append(f"leaves release {'.'.join(map(str, release))} with no plan item") + reasons.append(f"leaves release {version(release)} with no plan item") return reasons diff --git a/bin/loop_claude/routing.py b/bin/loop_claude/routing.py index 839a0f36..4c50216f 100644 --- a/bin/loop_claude/routing.py +++ b/bin/loop_claude/routing.py @@ -1,7 +1,7 @@ """Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`.""" import dataclasses -from chunk_pick import chunk_item, LastCut +from chunk_pick import chunk_item, LastCut, Next 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 @@ -19,7 +19,8 @@ def routed_item(todo: str, cut: LastCut) -> Item | Failure | None: found = items(todo) if isinstance(found, Failure): return found - takes = chunk_item(found or [], cut).item + pick = chunk_item(found or [], cut) + takes = pick.item if isinstance(pick, Next) else None 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 3ddedbec..62ca1aec 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -960,6 +960,19 @@ class ChunkFacts(unittest.TestCase): self.assertIn("yourself", said) self.assertIn(why, said) + def test_runs_the_preflight_only_where_the_session_is_not_open(self): + with tempfile.TemporaryDirectory() as home: + opened = session.session_named("A proj loop-claude", pathlib.Path(home, "projects")) + fresh = FakeEffects(python=[ran("- A fact.\n")]) + facts, _ = main.chunk_start(opened, self.PREFLIGHT, "/r", fresh.effects()) + self.assertEqual((fresh.made("python"), "- A fact." in facts), ([(["/b/chunk-preflight"], "/r")], True)) + transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text("") + resumed = FakeEffects() + facts, _ = main.chunk_start(opened, self.PREFLIGHT, "/r", resumed.effects()) + self.assertEqual((resumed.made("python"), facts), ([], "")) + class PythonEffect(unittest.TestCase): def test_runs_a_script_in_the_given_directory(self): diff --git a/bin/rank_todo.py b/bin/rank_todo.py index 02827ba6..48e71b6f 100755 --- a/bin/rank_todo.py +++ b/bin/rank_todo.py @@ -208,7 +208,7 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure: if isinstance(marked := renames(text), Failure): return marked if live := next((item for item in items if item.release in marked), None): - return Failure(f"todo.md marks release {release_name(live)} renamed but files item {live.id} under it; drop the mark or move the item") + return Failure(f"todo.md marks release {version(live.release)} renamed but files item {live.id} under it; drop the mark or move the item") scored = ordered(items, scoring) if isinstance(scored, Failure): return scored @@ -367,7 +367,7 @@ def blockers_first(scored: list[tuple[Item, float]]) -> list[tuple[Item, float]] for item, _ in scored: for blocker in item.blocked_by: if blocker in held and held[blocker][0].release > item.release: - later, earlier = release_name(held[blocker][0]), release_name(item) + later, earlier = version(held[blocker][0].release), version(item.release) return Failure(f"item {item.id}, in release {earlier}, is blocked by item {blocker}, in later release {later}; move item {blocker} to release {earlier} or earlier, or item {item.id} to release {later} or later") result: list[tuple[Item, float]] = [] for item, _ in scored: @@ -396,10 +396,6 @@ def place(id_: int, held: dict[int, tuple[Item, float]], position: dict[int, int return [*added, (dataclasses.replace(item, blocked_by=tuple(sorted(blockers))), value)] -def release_name(item: Item) -> str: - return version(item.release) - - def check_citations(items: list[Item], decisions: str) -> Failure | None: """Holds todo.md and docs/decisions.md to each other: a cited item is exempt, a decision row is cited.""" cited = set(cited_items(decisions)) @@ -581,7 +577,7 @@ def parse_item(row: str, scoring: Scoring, table: Table) -> Item | Failure: def render_item(item: Item, value: float, shape: Shape, columns: tuple[str, ...]) -> str: """The item's row, each cell under its column.""" - written = {"ID": str(item.id), "Release": release_name(item), BLOCKED_BY: ", ".join(map(str, item.blocked_by)), "Exempt": item.exempt, "Item": item.tagline, **{name: str(score) for name, score in item.scores.items()}, shape.goals_column: shape.write_goals(item.moves), "Score": f"{value:.1f}"} + written = {"ID": str(item.id), "Release": version(item.release), BLOCKED_BY: ", ".join(map(str, item.blocked_by)), "Exempt": item.exempt, "Item": item.tagline, **{name: str(score) for name, score in item.scores.items()}, shape.goals_column: shape.write_goals(item.moves), "Score": f"{value:.1f}"} return "| " + " | ".join(written[column] for column in columns) + " |" diff --git a/bin/test_chunk_pick.py b/bin/test_chunk_pick.py index 06844660..7d599d0b 100644 --- a/bin/test_chunk_pick.py +++ b/bin/test_chunk_pick.py @@ -6,35 +6,42 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) import chunk_pick from failure import Failure +import todo_table class Standing(unittest.TestCase): def test_takes_the_earliest_release_where_it_is_the_earliest_named_since_the_last_cut_and_names_the_next_release(self): cases = ( - ("v1.2.3", 4, (1, 2, 4), set(), ("next", (1, 2, 4))), - ("v1.2.3", 4, (1, 3, 0), {(1, 3, 0)}, ("next", (1, 3, 0))), - ("v1.2.3", 4, (1, 3, 0), {(1, 2, 3), (1, 3, 0)}, ("next", (1, 3, 0))), - ("v0.2.0", 4, (0, 4, 0), set(), ("next", (0, 4, 0))), - (None, 4, (0, 2, 0), set(), ("next", (0, 2, 0))), - (None, 4, None, set(), ("next", None)), - ("v1.2.3-rc.1", 4, (1, 2, 3), set(), ("next", (1, 2, 3))), - (Failure("tags unreadable"), 4, (0, 2, 0), set(), ("next", (0, 2, 0))), - ("v1.2.3", 4, (1, 2, 3), set(), ("cut", None)), - ("v1.2.3", 4, (1, 2, 2), {(1, 2, 4)}, ("cut", None)), - ("v1.2.3", 4, (1, 3, 0), {(1, 2, 4), (1, 3, 0)}, ("finished", (1, 2, 4))), - ("v0.9.2", 4, (1, 0, 0), {(0, 9, 3)}, ("finished", (0, 9, 3))), - ("v1.2.3", 4, (1, 4, 0), {(1, 2, 2), (1, 3, 0)}, ("finished", (1, 3, 0))), - ("v1.2.3", 4, None, {(1, 3, 0)}, ("finished", (1, 3, 0))), - ("v1.2.3", 4, None, set(), ("finished", None)), - ("v1.2.3", 0, (1, 3, 0), {(1, 2, 4)}, ("unplanned", (1, 2, 4))), - ("v1.2.3", 0, None, set(), ("unplanned", None)), - ("v1.2.3", Failure("count unreadable"), None, set(), ("unknown", None)), - ("v1.2.3", 4, (1, 3, 0), Failure("log unreadable"), ("unknown", None)), - ("v1.2.3", 4, (1, 2, 3), Failure("log unreadable"), ("cut", None)), + ("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)), + (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", 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.Finished((1, 2, 4))), + ("v0.9.2", 4, (1, 0, 0), {(0, 9, 3)}, chunk_pick.Finished((0, 9, 3))), + ("v1.2.3", 4, (1, 4, 0), {(1, 2, 2), (1, 3, 0)}, chunk_pick.Finished((1, 3, 0))), + ("v1.2.3", 4, None, {(1, 3, 0)}, chunk_pick.Finished((1, 3, 0))), + ("v1.2.3", 4, None, set(), chunk_pick.Finished(None)), + ("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", 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, landed, earliest, named, stands in cases: with self.subTest(tag=tag, landed=landed, earliest=earliest, named=named): - self.assertEqual(chunk_pick.standing(tag, landed, earliest, named), stands) + self.assertEqual(chunk_pick.standing(tag, landed, earliest, None, named), stands) + + def test_takes_the_first_item_only_where_the_earliest_release_is_next(self): + first = todo_table.Item(1, (1, 2, 4), "", "**Do 1.**", {}, (), ()) + self.assertEqual(chunk_pick.standing("v1.2.3", 4, (1, 2, 4), first, set()), chunk_pick.Next((1, 2, 4), first)) + self.assertEqual(chunk_pick.standing("v1.2.3", 4, (1, 2, 3), first, set()), chunk_pick.Cut((1, 2, 3))) 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"