Files
claude-config/bin/larv_review/plan.py
T

119 lines
6.8 KiB
Python

import pathlib
from release_plan.release_standing import Cut, LastCut, next_release, project_plan, standing_of, Unknown
from failure import Failure
from larv_review.scoring import is_floor_item
from plan.todo_table import Item, NoItemsTable
from project_config.main import located
from plan.goals_file import OPTED_OUT
from plan_store.plans import Plan, PlanStore
from plan_store import stores as plan_stores
from project_config.settings import ProjectConfig
from plan.release import Release, version
from record_store import stores
from larv_review.unmeasured import unmeasured_cut
# The words that end the reason a chunk empties a release; SKILL.md → "Second run" quotes them.
EMPTIED_WORDS = "with no plan item"
class Emptied(str):
"""The reason a chunk empties a release, which makes it due two scoring runs; a chunk with only other reasons is due one."""
def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed_release: Release | None, renamed: frozenset[Release], unmeasured: str | None = None) -> list[str] | Failure:
"""Why the plan calls a scoring run, empty when it does not.
A chunk that ships a floor item is due one, so it can prove the floor met. So is a chunk that empties a release after work has landed towards it, so no release ends unmeasured. A release the chunk's plan marks renamed is due none. A chunk after a release `unmeasured_cut` names is due one too, so a release cut with no scoring run is caught up.
before is the default branch's plan and after the chunk's. ships holds the IDs of the items the chunk ships. landed_release is the next release where work has landed towards it, else None (`landed_towards`). renamed holds the releases the chunk's plan marks renamed. unmeasured is `unmeasured_cut`'s answer.
"""
plan = {item.id: item for item in before + after}
if unknown := sorted(set(ships) - set(plan)):
return Failure(f"neither plan holds item {', '.join(map(str, unknown))}")
shipped = [plan[id_] for id_ in ships]
reasons = [f"ships item {item.id}, a floor item" for item in shipped if is_floor_item(item)]
if unmeasured:
reasons.append(f"follows {unmeasured}")
for release in emptied(before, after, shipped, renamed):
if work_landed(release, shipped, landed_release):
reasons.append(Emptied(f"leaves release {version(release)} {EMPTIED_WORDS}"))
return reasons
# larv-review --help points here rather than restating the rule, so the rule is written once.
def work_landed(release: Release, shipped: list[Item], landed_release: Release | None) -> bool:
"""Whether work has landed towards release, for the scoring-run trigger. It has when the chunk ships one of the release's items. It has too when release equals landed_release: the default branch's next release, once work has landed towards it."""
return any(item.release == release for item in shipped) or release == landed_release
def landed_towards(found: list[Item], cut: LastCut) -> Release | None | Failure:
"""The next release on the default branch, once work has landed towards it: once one or more changes are unreleased, as `release_standing.unreleased_changes` counts them. None where there is no next release, or no change is unreleased. found is the default branch's plan."""
if isinstance(cut.tag, Failure):
return Failure(f"could not tell the next release: the default branch's tags cannot be read: {cut.tag.reason}")
upcoming = next_release(standing_of(found, cut), 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.
if isinstance(upcoming, Cut):
return None
if isinstance(upcoming.unreleased, Failure):
return Failure(f"could not tell whether work landed towards the next release: {upcoming.unreleased.reason}")
return upcoming.release if upcoming.unreleased else None
def emptied(before: list[Item], after: list[Item], shipped: list[Item], renamed: frozenset[Release]) -> list[Release]:
"""Each release the default branch's plan or a shipped item names that the chunk's plan, less the shipped items, names no longer, unless the chunk's plan marks it renamed."""
named = {item.release for item in before + shipped}
kept = [item for item in after if item.id not in {ship.id for ship in shipped}]
return sorted(named - {item.release for item in kept} - renamed)
def finished_unseen(config: ProjectConfig, ids: list[int], chunk: list[Item]) -> Failure | None:
"""Why the items under ids cannot all be read, where the plan lies outside the checkout: such a plan is read as it stands for both plans, so a finished item is in neither."""
if config.tracked("plan") is None and (gone := sorted(set(ids) - {item.id for item in chunk})):
return Failure(f"the plan as it stands holds no item {', '.join(map(str, gone))}, and larv-review doesn't support a plan outside the checkout, so it cannot read the plan as it stood before the item was finished: {config.plan_label}")
return None
def plan_reasons(ref: str, ids: list[int] | None, home: pathlib.Path) -> list[str] | Failure:
"""Why the plan calls a scoring run, from todo.md at ref and the checkout's, each file where the project's config puts it; empty where the project opts out of the comprehension check. ref is the default branch, or the commit where the chunk began. Where ids is None, the chunk ships each item the plan at ref holds and the checkout's does not."""
config = located(None, home)
if isinstance(config, Failure):
return config
checkout = str(config.top)
decisions = stores.held_at(ref, stores.store(config, "decisions"), checkout)
if isinstance(decisions, Failure):
return decisions
if OPTED_OUT.search(decisions):
return []
held = project_plan(config, ref, checkout)
before = held.plan
read = checkout_plan(plan_stores.store(config))
if isinstance(read, Failure):
return read
chunk, renamed = read.items, read.renamed
# While a chunk reshapes todo.md into one table, the default branch's todo.md has no items table.
if isinstance(before, NoItemsTable):
planned = []
elif isinstance(before, Failure):
return Failure(f"{ref}'s todo.md: {before.reason}")
else:
planned = before.items
landed_release = landed_towards(planned, held.cut)
if isinstance(landed_release, Failure):
return landed_release
unmeasured = unmeasured_cut(ref, stores.store(config, "comprehension-history"), checkout)
if isinstance(unmeasured, Failure):
return unmeasured
if ids is None:
ids = sorted({item.id for item in planned} - {item.id for item in chunk})
return finished_unseen(config, ids, chunk) or scoring_run_due(planned, chunk, ids, landed_release, renamed, unmeasured)
def checkout_plan(plan: PlanStore) -> Plan | Failure:
"""The project's plan as it stands."""
read = plan.read()
return Failure(f"the project's plan: {read.reason}") if isinstance(read, Failure) else read