Answer architecture round 2: split record into its two commands, accept a dated first line only with a Mean line alone, read a default branch without a plan table as empty, and fetch where an answer needs origin as it is

This commit is contained in:
2026-10-09 01:24:48 +02:00
parent 05efbb1d8a
commit 9c2f982741
6 changed files with 108 additions and 57 deletions
+64 -35
View File
@@ -64,8 +64,9 @@ import typing
from larv_review.history import appended, heading, ruling_entry, scoring_entry
from larv_review.rules import base, CAP, FLOOR, means, PASSES, plan_items, round_counts, ruling, scoring_run_due, under_floor, votes
from larv_review.git import commit_exists, default_branch, file_at, git, HISTORY, history_changes, landed_since_release, top
from larv_review.seats import DIMENSIONS, report, scores
from larv_review.seats import DIMENSIONS, MEAN, Report, report, scores
from failure import Failure
from rank_todo import ITEMS_HEADER
from file_writes import replace_file
USAGE = "usage: larv-review ruling-base | round-count | needs-scoring-run <item ID>... | record-ruling <seats file> | record-scoring <seats file>"
@@ -76,12 +77,16 @@ def main(argv: list[str]) -> int:
print(__doc__.strip().format(cap=CAP, floor=FLOOR))
return 0
command, *given = argv or [""]
if command in ("ruling-base", "round-count") and not given:
done = with_default_branch(ruling_base if command == "ruling-base" else round_count)
if command == "ruling-base" and not given:
done = with_default_branch(ruling_base, fetched=True)
elif command == "round-count" and not given:
done = with_default_branch(round_count, fetched=False)
elif command == "needs-scoring-run" and given and all(id_.isdigit() for id_ in given):
done = with_default_branch(lambda default: needs_scoring_run(default, [int(id_) for id_ in given]))
elif command in ("record-ruling", "record-scoring") and len(given) == 1:
done = record(pathlib.Path(given[0]), command == "record-ruling")
done = with_default_branch(lambda default: needs_scoring_run(default, [int(id_) for id_ in given]), fetched=True)
elif command == "record-ruling" and len(given) == 1:
done = record_ruling(pathlib.Path(given[0]))
elif command == "record-scoring" and len(given) == 1:
done = record_scoring(pathlib.Path(given[0]))
else:
print(f"unknown arguments {' '.join(argv)}; {USAGE}", file=sys.stderr)
return 1
@@ -91,9 +96,15 @@ def main(argv: list[str]) -> int:
return 0
def with_default_branch(command: typing.Callable[[str], Failure | None]) -> Failure | None:
default = default_branch()
return default if isinstance(default, Failure) else command(default)
def with_default_branch(command: typing.Callable[[str], Failure | None], fetched: bool) -> Failure | None:
"""fetched: whether the command needs origin as it is now, or may read it as last fetched."""
default = default_branch(fetched)
if isinstance(default, Failure):
return default
branch, note = default
if note:
print(note, file=sys.stderr)
return command(branch)
def ruling_base(default: str) -> Failure | None:
@@ -103,10 +114,13 @@ def ruling_base(default: str) -> Failure | None:
history = file_at(default, HISTORY)
if isinstance(history, Failure):
return history
if found := base(changes, history, default):
print("\n".join(found))
return None
merge_base = git("merge-base", "HEAD", default)
if isinstance(merge_base, Failure):
return merge_base
print("\n".join(base(changes, history, merge_base.strip(), default)))
print(f"{merge_base.strip()}\nNo merged change on {default} adds a scoring run or a paired ruling; the merge base of HEAD and {default}.")
return None
@@ -130,7 +144,8 @@ def needs_scoring_run(default: str, ids: list[int]) -> Failure | None:
after = pathlib.Path(checkout, "todo.md").read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read todo.md: {failure}")
planned = plan_items(before)
# A plan a chunk is reshaping into one table has none yet on the default branch.
planned = plan_items(before) if ITEMS_HEADER in before.splitlines() else []
if isinstance(planned, Failure):
return Failure(f"{default}'s todo.md: {planned.reason}")
chunk = plan_items(after)
@@ -146,30 +161,28 @@ def needs_scoring_run(default: str, ids: list[int]) -> Failure | None:
return None
def record(seats_file: pathlib.Path, paired: bool) -> 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
named = heading(time.time(), given.read, paired)
if isinstance(named, Failure):
return named
title = named.group(0)
if unknown := [commit for commit in named.group(3, 4) if commit and not commit_exists(commit)]:
return Failure(f"the seats file names {', '.join(unknown)}, which is no commit in this checkout")
if paired:
counted = votes(given.seats)
if isinstance(counted, Failure):
return counted
tally = ", ".join(f"{seat} {vote}" for seat, vote in counted.items())
ruled = ruling(list(counted.values()))
if ruled 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: {ruled}. Votes as counted: {tally}.")
return append(ruling_entry(title, given.seats, counted, ruled))
def record_ruling(seats_file: pathlib.Path) -> Failure | None:
read = read_seats(seats_file, paired=True)
if isinstance(read, Failure):
return read
title, given = read
counted = votes(given.seats)
if isinstance(counted, Failure):
return counted
tally = ", ".join(f"{seat} {vote}" for seat, vote in counted.items())
ruled = ruling(list(counted.values()))
if ruled 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: {ruled}. Votes as counted: {tally}.")
return append(ruling_entry(title, given.seats, counted, ruled))
def record_scoring(seats_file: pathlib.Path) -> Failure | None:
read = read_seats(seats_file, paired=False)
if isinstance(read, Failure):
return read
title, given = read
found = scores(given.seats)
if isinstance(found, Failure):
return found
@@ -179,6 +192,22 @@ def record(seats_file: pathlib.Path, paired: bool) -> Failure | None:
return append(scoring_entry(title, given.seats, found, [str(value) for value in mean]))
def read_seats(seats_file: pathlib.Path, paired: bool) -> tuple[str, Report] | Failure:
"""The seats file, and the heading its record goes under, once each commit it names is in the checkout."""
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
named = heading(time.time(), given.read, paired, [seat.name for seat in given.seats] == [MEAN])
if isinstance(named, Failure):
return named
if unknown := [commit for commit in named.group(3, 4) if commit and not commit_exists(commit)]:
return Failure(f"the seats file names {', '.join(unknown)}, which is no commit in this checkout")
return named.group(0), given
def append(entry: str) -> Failure | None:
checkout = top()
if isinstance(checkout, Failure):