Files

118 lines
6.9 KiB
Python

"""The next chunk's pick: the plan's items weighed against the last cut, with the item the next chunk takes first, or why there is none, as chunk-preflight states it and loop-claude starts chunks by it."""
import dataclasses
from release_plan.release_standing import Cut, dated_cut, LastCut, picked, ReadyToCut, ReviewDue, standing_of, Unknown, Unplanned
from failure import Failure
from plan.todo_table import Item, NoItemsTable
from text_files.markdown_text import listed
from plan_store.plans import Plan
from plan.release import Release, version
from project_config import templates
@dataclasses.dataclass(frozen=True)
class CarriedOver:
"""The items, by ID in the plan's order, that the plan files under a release already cut, and that count as `into`'s, the next release: release_standing.Standing.moved."""
into: Release
ids: tuple[int, ...]
@dataclasses.dataclass(frozen=True)
class NextChunk:
"""The plan's items, each with its detail, and `first`: the item the next chunk takes first."""
items: list[Item]
first: Item
# first's tagline and detail.
text: str
branch_template: str
# The items of a release already cut, which `items` and `first` hold as the next release's. None where every item's release is after the last cut.
carried: CarriedOver | None = None
# Whether --item named first, in place of the next release's first item that does not wait.
named: bool = False
@property
def branch(self) -> str:
"""The branch first's worktree is on."""
return templates.branch_of(self.branch_template, self.first.id)
@dataclasses.dataclass(frozen=True)
class NoItem:
"""Why the plan names no item for the next chunk, as a sentence the worker reads."""
why: str
# Whether why names pull requests that await a person's merge, so that no chunk starts until one is merged.
awaits_merge: bool = False
def cut_name(cut: LastCut) -> str | None:
"""How the preflight names the last cut: the tag, or the version under its dated CHANGELOG.md heading; None where nothing is cut."""
if cut.tag is not None:
return None if isinstance(cut.tag, Failure) else cut.tag
headed = dated_cut(cut.tag, cut.changelog)
return None if headed is None else f"{version(headed)} (dated in CHANGELOG.md)"
def plan_name(default: str, outside: str | None) -> str:
"""How the preflight names the plan it read: the default branch's todo.md, or the file or Jira epic, where the project's config puts it outside the checkout."""
return outside if outside else f"{default}'s todo.md"
def planned(read: Plan | Failure, cut: LastCut, default: str, branch: str, outside: str | None = None, item_id: int | None = None) -> NextChunk | NoItem:
"""read, which is the project's plan as its store read it and which plan_name names, with the item the next chunk takes first: the item of item_id where one is given, else release_standing.chunk_item's pick. Else why there is none. branch is the project's `branch` template."""
where = plan_name(default, outside)
if isinstance(read, NoItemsTable):
return NoItem(f"{where} holds no items table.")
if isinstance(read, Failure):
return NoItem(f"{where} cannot be read: {read.reason}.")
found = read.items
if isinstance(cut.tag, Failure):
return NoItem(f"{default}'s tags cannot be read: {cut.tag.reason}. So which release is next cannot be told. Find the next release yourself.")
named = cut_name(cut)
last_cut = f"the last cut, {named}" if named is not None else "the start, since no release is cut yet"
weighed = standing_of(found, cut)
pick = picked(weighed, cut)
if isinstance(pick, Cut):
return NoItem(f"{where} files items under release {version(pick.release)}, which is not after {last_cut}; 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 {last_cut}: todo.md's texts since then, or the count of changes since then, cannot be read. Find the next release yourself.")
if isinstance(pick, ReadyToCut | ReviewDue | Unplanned):
upcoming = f"release {version(pick.release)}, the next after {last_cut}" if pick.release else f"the next release after {last_cut}"
if isinstance(pick, ReviewDue):
return NoItem(f"{where} holds no item for {upcoming}, and changes are unreleased, so that release's release review is due: no commit titled `Release review of <release>` has landed {'since the cut' if named is not None else 'yet'}. This chunk is that review, as `~/.claude/skills/larv-review/SKILL.md` → \"Release review\" says; once it merges and the release holds no item, the release is ready to cut.")
if isinstance(pick, ReadyToCut):
return NoItem(f"{where} holds no item for {upcoming}, and changes are unreleased, and its release review has landed, so that release is finished and ready to cut. The working loop stops here. If todo.md renamed that release instead, add `- <old release> renamed to <new release>` to todo.md's `## Renamed` section.")
return NoItem(f"{where} holds no item for {upcoming}, and no change is unreleased, so that release is to be planned first.")
first = pick.item
if first is None and pick.release is not None:
pulls = awaited_merges(weighed.carried, pick.release)
return NoItem(f"every item of release {version(pick.release)} in {where} waits.{merge_stop(pulls)}", awaits_merge=bool(pulls))
if first is None:
return NoItem(f"{where}'s items table holds no item.")
held = weighed.carried
# Every carried item takes one release, the next.
over = CarriedOver(next(item.release for item in held if item.id == weighed.moved[0]), weighed.moved) if weighed.moved else None
if item_id is not None:
first = next((item for item in held if item.id == item_id), None)
if first is None:
return NoItem(f"{where} holds no item {item_id}, the one --item names; it may have shipped.")
if first.awaits_merge:
return NoItem(f"item {item_id}, the one --item names, is finished on the branch of pull request {first.awaits_merge}, which awaits a person's merge.")
return NextChunk(held, first, f"{first.tagline}\n\n{first.detail}".strip(), branch, over, item_id is not None)
def awaited_merges(found: list[Item], release: Release) -> dict[str, list[int]]:
"""Each pull request, as `#<number> <URL>`, that finishes items of release and awaits a person's merge, with those items' IDs."""
pulls: dict[str, list[int]] = {}
for item in found:
if item.release == release and item.awaits_merge:
pulls.setdefault(item.awaits_merge, []).append(item.id)
return pulls
def merge_stop(pulls: dict[str, list[int]]) -> str:
"""The sentences that stop the working loop, naming each of pulls, the pull requests that await a person's merge, with the items it finishes; empty where pulls is empty."""
if not pulls:
return ""
named = "; ".join(f"{pull}, which finishes item{'s' if len(ids) > 1 else ''} {listed([str(item_id) for item_id in ids], 'and')}" for pull, ids in pulls.items())
return f" Under `merge = \"human\"`, these pull requests await a person's merge: {named}. The working loop stops here. Once a person has merged one, or turned one back into a draft, run loop-claude again."