import pathlib from larv_review.plan import EMPTIED_WORDS, plan_reasons from review_rules.comprehension_history import entries, HISTORY from failure import Failure from git_cli.git_repo import file_at, git, top from release_plan.todo_table import items, NoItemsTable def owed_scoring_run(default: str) -> 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 counts for nothing. A project with no plan owes none. 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. """ checkout = top() if isinstance(checkout, Failure): return checkout if not has_plan(checkout): return None base = git("merge-base", default, "HEAD", cwd=checkout) if isinstance(base, Failure): return base reasons = plan_reasons(base.strip(), None) 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(checkout: str) -> bool: """Whether the checkout has a todo.md holding an items table. A file that can't be read counts, so `plan_reasons` reports it.""" try: return not isinstance(items(pathlib.Path(checkout, "todo.md").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]