"""Hold larv-review's mechanical rules: the paired ruling's base and vote count, each pass's rounds, the scoring run's trigger, and the comprehension history's format. usage: larv-review ruling-base | round-count | needs-scoring-run ... | record-ruling | record-scoring Run it inside a project checkout or one of its worktrees. It first fetches origin, when the checkout has that remote, and takes origin's default branch, or else the local main or master, as the default branch. The history is docs/comprehension-history.md in the checkout. ruling-base prints the commit a paired ruling reads against, then how it was found: the default branch's newest merged change that adds a scoring run to the history, or whose last added paired ruling, by its heading's time, is better; a merge commit is one change against its first parent. With none, the base the history's first paired ruling names; with no ruling either, the merge base of HEAD and the default branch. round-count prints, for each pass, the highest among this branch's commit titles opening `Answer round :`, counting commits the default branch does not hold, and marks a pass at the cap of 3 rounds. needs-scoring-run prints why the chunk shipping the given todo.md item IDs takes a scoring run, or `No scoring run due.` It is due when the chunk ships an item whose tagline names the comprehension floor, or when a release the default branch's todo.md names has no item left in the checkout's todo.md, less the items shipped, and work has landed towards it: the chunk ships one of its items, or it is the earliest release the default branch's todo.md names and the default branch has a commit after its newest v* tag, or has no such tag. record-ruling and record-scoring read a seats file: a first line naming what the seats read, then each seat's line, each followed by its places, one `- ` bullet each, as the history writes them: PR #231 at abc1234, against def5678 Mid A: worse - `bin/x.py:12` `run`: three branches to hold at once Inherited architect: worse - `bin/x.py:12` `run`: ... record-ruling takes Mid A's and the inherited architect's votes, better, same or worse; a better or worse vote naming no `path:line` place counts as same. When the two counted votes differ, it appends nothing and says to add the maintainability senior's and Junior A's lines. Otherwise the ruling is the vote that leads each other vote by two or more, else same, and it appends the ruling. record-scoring takes Junior A's, Mid A's, the maintainability senior's and the inherited architect's lines in that order, each `Navigation , Locality , Shape , Self-sufficiency , Overall `, each score from 1 to 10 and the overall at most 1 above the seat's lowest dimension. A first line names no `, against`. It appends the seats' scores and their means, rounded half up to two decimals, and prints the means and each one under the floor of 7.0. Each record is appended to the history, created when missing, under a heading of the current UTC time and the seats file's first line. Exit 1, saying why, on unknown arguments, an unknown item ID, a seats file it cannot read or that breaks the shape above, or when git cannot start, finds no checkout or no default branch, or the history or todo.md cannot be read or written. """ import pathlib import sys import time from larv_review.history import appended, entry_text, HEADING from larv_review.rules import CAP, first_ruling, means, measured_commit, plan_items, round_counts, scoring_run_due, under_floor from larv_review.seats import DIMENSIONS, Report, report, ruling, scores, votes from failure import Failure from larv_review.git import file_at, git, HISTORY, history_changes, landed_since_release, Repository, repository from file_writes import replace_file from loop_claude.transcript_archive import utc USAGE = "usage: larv-review ruling-base | round-count | needs-scoring-run ... | record-ruling | record-scoring " def main(argv: list[str]) -> int: if argv in (["-h"], ["--help"]): print(__doc__.strip()) return 0 command, *given = argv or [""] valid = {"ruling-base": not given, "round-count": not given, "needs-scoring-run": bool(given) and all(id_.isdigit() for id_ in given), "record-ruling": len(given) == 1, "record-scoring": len(given) == 1} if not valid.get(command): print(f"unknown arguments {' '.join(argv)}; {USAGE}", file=sys.stderr) return 1 repo = repository() if isinstance(repo, Failure): return failed(repo) if command == "ruling-base": done = ruling_base(repo) elif command == "round-count": done = round_count(repo) elif command == "needs-scoring-run": done = needs_scoring_run(repo, [int(id_) for id_ in given]) else: done = record(repo, pathlib.Path(given[0]), command == "record-ruling", utc(time.time())) return failed(done) if isinstance(done, Failure) else 0 def ruling_base(repo: Repository) -> Failure | None: changes = history_changes(repo.default) if isinstance(changes, Failure): return changes if found := measured_commit(changes): print(f"{found}\nThe newest merged change on {repo.default} that adds a scoring run to {HISTORY}, or whose last added paired ruling is better.") return None history = file_at(repo.default, HISTORY) if isinstance(history, Failure): return history if (first := first_ruling(history)) and first.against: print(f"{first.against}\nNo merged change on {repo.default} adds a scoring run or a better ruling; the base its first paired ruling names.") return None merge_base = git("merge-base", "HEAD", repo.default) if isinstance(merge_base, Failure): return merge_base print(f"{merge_base.strip()}\nNo merged change on {repo.default} adds a scoring run or a paired ruling; the merge base of HEAD and {repo.default}.") return None def round_count(repo: Repository) -> Failure | None: titles = git("log", "--format=%s", f"{repo.default}..HEAD") if isinstance(titles, Failure): return titles for name, count in round_counts(titles.splitlines()).items(): print(f"{name}: {count}{', at the cap' if count >= CAP else ''}") return None def needs_scoring_run(repo: Repository, ids: list[int]) -> Failure | None: before = file_at(repo.default, "todo.md") if isinstance(before, Failure): return before try: after = pathlib.Path(repo.top, "todo.md").read_text(encoding="utf-8") except FileNotFoundError: after = "" except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read todo.md: {failure}") planned = plan_items(before) if isinstance(planned, Failure): return planned chunk = plan_items(after) if isinstance(chunk, Failure): return chunk if unknown := sorted(set(ids) - {item.id for item in planned + chunk}): return Failure(f"neither {repo.default}'s todo.md nor the checkout's holds item {', '.join(map(str, unknown))}") landed = landed_since_release(repo.default) if isinstance(landed, Failure): return landed reasons = scoring_run_due(planned, chunk, ids, landed) print(f"Scoring run due: the chunk {' and '.join(reasons)}." if reasons else "No scoring run due.") return None def record(repo: Repository, seats_file: pathlib.Path, paired: bool, now: str) -> Failure | None: try: given = report(seats_file.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {seats_file}: {failure}") if isinstance(given, Failure): return given heading = f"{now}, {given.read}" if not (named := HEADING.match(heading)) or bool(named.group(4)) != paired: return Failure(f"the seats file's first line is not ` at {', against ' if paired else ''}`: {given.read}") entry = ruling_entry(heading, given) if paired else scoring_entry(heading, given) if isinstance(entry, Failure) or entry is None: return entry history = pathlib.Path(repo.top, HISTORY) try: text = history.read_text(encoding="utf-8") if history.exists() else "" history.parent.mkdir(parents=True, exist_ok=True) except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {history}: {failure}") unwritten = replace_file(history, appended(text, entry).encode()) if unwritten: return Failure(f"could not write {history}: {unwritten.reason}") print(f"Appended to {history}.") return None def ruling_entry(heading: str, given: Report) -> str | Failure | None: """The ruling's entry, or None, after saying which seats to add, when the first two counted votes differ.""" counted = votes(given.seats) if isinstance(counted, Failure): return counted tally = ", ".join(f"{seat} {vote}" for seat, vote in counted.items()) result = ruling(list(counted.values())) if result is None: print(f"Split: {tally}. Launch the maintainability senior and Junior A together, add their lines to the seats file, and run this again.") return None print(f"Ruling: {result}. Votes as counted: {tally}.") cells = [[seat.name, counted[seat.name] if counted[seat.name] == seat.said else f"same (voted {seat.said}, naming no place)"] for seat in given.seats] return entry_text(heading, [f"Ruling: {result}"], [["Seat", "Vote"], *cells], [(f"{seat.name}, decided by:", seat.places) for seat in given.seats]) def scoring_entry(heading: str, given: Report) -> str | Failure: found = scores(given.seats) if isinstance(found, Failure): return found mean = means(found) under = under_floor(mean) print(f"Means: {', '.join(f'{name} {value}' for name, value in zip(DIMENSIONS, mean))}. {'Under the floor of 7.0: ' + ', '.join(under) if under else 'None under the floor of 7.0'}.") rows = [[seat, *values] for seat, values in found.items()] + [["Mean", *map(str, mean)]] labels = [(f"{seat.name}, {'would restructure first' if seat.name == 'Inherited architect' else 'hardest first'}:", seat.places) for seat in given.seats] return entry_text(heading, [], [["Seat", *DIMENSIONS], *rows], labels) def failed(failure: Failure) -> int: print(failure.reason, file=sys.stderr) return 1