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 release_plan.todo_table import items, NoItemsTable from project_config.main import located 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(config.paths["plan"]): return None # No commit holds a file outside the checkout. 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. plan, history = config.tracked("plan"), config.tracked("comprehension-history") if plan is None or history is None: name, setting = ("plan", "plan") if plan is None else ("comprehension history", "comprehension-history") warn(f"larv-review doesn't support a {name} outside the checkout, so it cannot tell whether the chunk owes a scoring run: {config.paths[setting]}") 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(todo: pathlib.Path) -> bool: """Whether todo, the project's plan, exists and holds an items table. A file that can't be read counts, so `plan_reasons` reports it.""" try: return not isinstance(items(todo.read_text(encoding="utf-8")), NoItemsTable) except FileNotFoundError: return False except (OSError, UnicodeDecodeError): return True def scoring_runs(history: str) -> list[str]: return [entry.heading for entry in entries(history) if not entry.paired]