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

110 lines
5.5 KiB
Python

import dataclasses
import decimal
import itertools
from larv_review.seats import DIMENSIONS
from larv_review.history import entries, Entry
from failure import Failure
from rank_todo import Item, ITEMS_HEADER, parse_row, table_rows
FLOOR = decimal.Decimal("7.0")
def chunk_entries(default_history: str, history: str) -> list[Entry]:
"""The checkout's entries whose headings the default branch's history lacks, oldest first; entries sharing a time keep their file order."""
held = {entry.heading for entry in entries(default_history)}
return sorted((entry for entry in entries(history) if entry.heading not in held), key=lambda entry: entry.time)
@dataclasses.dataclass(frozen=True)
class Fall:
"""Where the chunk stands in the fall sequence: a paired ruling with a `Fell:` line leaves its dimensions pending until a scoring run follows; that run records a `Ratchet:` line when a fallen dimension is under the floor, and the ratchet stands until the next paired ruling."""
pending: list[str]
ratchet: str | None
scored_last: bool
def fall(chunk: list[Entry]) -> Fall:
newest = chunk[-1] if chunk else None
since_ruling = list(itertools.takewhile(lambda entry: not entry.paired, reversed(chunk)))
ratchet = next((entry.ratchet for entry in since_ruling if entry.ratchet), None)
return Fall(newest.fell if newest and newest.paired else [], ratchet, newest is not None and not newest.paired)
def fall_reasons(state: Fall) -> list[str]:
return [f"has a paired ruling in which {' and '.join(state.pending)} fell"] if state.pending else []
def uncalled(state: Fall) -> str:
"""Why a scoring run nothing calls fails, and what to do instead."""
if state.scored_last:
return "the chunk's newest entry is already a scoring run; record a paired ruling first, or name the item IDs the chunk ships"
return "no plan item, release or fall calls this scoring run; name the item IDs the chunk ships, as needs-scoring-run took them"
def floor_verdict(under: list[str], reasons: list[str]) -> str:
"""Whether the means under the floor block the merge: they do on a run the plan calls, its reasons given."""
if not under:
return f"None under the floor of {FLOOR}."
if reasons:
return f"Floor: {', '.join(under)} under the floor of {FLOOR} block the merge, as the chunk {' and '.join(reasons)}."
return f"Under the floor of {FLOOR}, blocking nothing, as only a fall called this run: {', '.join(under)}."
def fall_outcome(fell: list[str], found: list[decimal.Decimal]) -> tuple[str, list[str]] | None:
"""What a scoring run says of a pending fall, and the lines it records: a `Ratchet:` line when a fallen dimension's mean is under the floor."""
if not fell:
return None
if dropped := [f"{name} is {value}" for name, value in zip(DIMENSIONS, found) if name in fell and value < FLOOR]:
line = f"Ratchet: the paired ruling counts as worse, as {' and '.join(fell)} fell and {', '.join(dropped)}, under the floor of {FLOOR}."
return line, [line]
return f"The paired ruling stands: {' and '.join(fell)} fell and each stays at or above the floor of {FLOOR}.", []
def means(lines: list[list[str]]) -> list[decimal.Decimal]:
"""The mean of each score across the lines, in DIMENSIONS' order, rounded half up to two decimals."""
return [mean([decimal.Decimal(score) for score in column]) for column in zip(*lines)]
def mean(values: list[decimal.Decimal]) -> decimal.Decimal:
return (sum(values, decimal.Decimal(0)) / len(values)).quantize(decimal.Decimal("0.01"), decimal.ROUND_HALF_UP)
def under_floor(found: list[decimal.Decimal]) -> list[str]:
return [f"{name} {value}" for name, value in zip(DIMENSIONS, found) if value < FLOOR]
def plan_items(todo: str) -> list[Item] | Failure:
"""Each row of a todo.md's items table, read as `rank_todo.py` reads it."""
table = table_rows(todo.splitlines(), ITEMS_HEADER)
if isinstance(table, Failure):
return table
items = []
for row in table.rows:
item = parse_row(row)
if isinstance(item, Failure):
return item
items.append(item)
return items
def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: bool) -> list[str] | Failure:
"""Why the plan calls a scoring run, empty when it does not. before is the default branch's plan, after the chunk's, ships the IDs it ships, and landed whether the default branch holds a commit after its newest release tag. Work has landed towards an emptied release when the chunk ships one of its items, or when it is the next release, the earliest before names, and landed holds."""
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}, which names the comprehension floor" for item in shipped if "comprehension floor" in item.tagline.lower()]
next_release = min((item.release for item in before), default=None)
for release in emptied(before, after, shipped):
if any(item.release == release for item in shipped) or (release == next_release and landed):
reasons.append(f"leaves release {'.'.join(map(str, release))} with no plan item")
return reasons
def emptied(before: list[Item], after: list[Item], shipped: list[Item]) -> list[tuple[int, int, int]]:
"""Each release the default branch's plan or a shipped item names that the chunk's plan, less the shipped items, names no longer."""
named = {item.release for item in before + shipped}
left = {item.release for item in after if item.id not in {ship.id for ship in shipped}}
return sorted(named - left)