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

66 lines
3.5 KiB
Python

import pathlib
import typing
from larv_review.plan import EMPTIED_WORDS, plan_reasons
from review_rules.comprehension_history import entries
from failure import Failure
from git_cli.git_repo import file_at, git
from project_config.main import located
from plan.todo_table import NoItemsTable
from plan_store.stores import store as plan_store
from plan_store.plans import PlanStore
from record_store.stores import store
def owed_scoring_run(default: str, home: pathlib.Path, warn: typing.Callable[[str], None]) -> Failure | None:
"""A Failure saying which scoring runs the chunk owes and has not recorded; None where it owes none.
The chunk owes the runs `plan_reasons` calls, two where it empties a release, else one. It has recorded the scoring runs its committed history holds beyond those at the merge base, so a default branch that has moved on since the branch began doesn't count. A project with no plan owes none. Neither does one whose plan or history is outside the checkout: it warns that it cannot tell. A missing run blocks the merge and a low score never does, which keeps the decision `review: the comprehension check records …`: it bars only scores from blocking.
"""
config = located(None, home)
if isinstance(config, Failure):
return config
checkout = str(config.top)
if not has_plan(plan_store(config)):
return None
# No commit holds a file outside the checkout, nor a record kept outside git. So the plan as the chunk found it cannot be read, nor the runs the branch recorded counted; the merge goes on, as `docs/decisions.md` → "project config: a tool that reads a file's commits …" says.
record = store(config, "comprehension-history")
plan, history = config.tracked("plan"), record.tracked
if plan is None or history is None:
name, where = ("plan", config.plan_label) if plan is None else ("comprehension history", record.label)
warn(f"larv-review doesn't support a {name} outside the checkout, so it cannot tell whether the chunk owes a scoring run: {where}")
return None
base = git("merge-base", default, "HEAD", cwd=checkout)
if isinstance(base, Failure):
return base
reasons = plan_reasons(base.strip(), None, home)
if isinstance(reasons, Failure):
return reasons
if not reasons:
return None
before = file_at(base.strip(), history, checkout)
if isinstance(before, Failure):
return before
after = file_at("HEAD", history, checkout)
if isinstance(after, Failure):
return after
return shortfall(reasons, len(scoring_runs(after)) - len(scoring_runs(before)))
def shortfall(reasons: list[str], recorded: int) -> Failure | None:
"""Which scoring runs the plan's reasons call that recorded, the count the branch added to the history, leaves unrecorded; None where it holds them all."""
needed = 2 if any(reason.endswith(EMPTIED_WORDS) for reason in reasons) else 1
if recorded >= needed:
return None
return Failure(f"the chunk owes {needed} scoring run{'s' if needed > 1 else ''} and the branch's committed history records {max(recorded, 0)}: it {' and '.join(reasons)}. larv-review's SKILL.md → 'Scoring run' says how to run and record {'them' if needed > 1 else 'it'}{"; → 'Second run' says how the second is titled" if needed > 1 else ""}")
def has_plan(plan: PlanStore) -> bool:
"""Whether the project has set a plan up. One that can't be read counts, so `plan_reasons` reports it."""
return not isinstance(plan.read(), NoItemsTable)
def scoring_runs(history: str) -> list[str]:
return [entry.heading for entry in entries(history) if not entry.paired]