Files
claude-config/bin/task_bench/record_figures.py
T

74 lines
4.5 KiB
Python

"""`task-bench --record-figures`: a bench's figures over every task, appended to the comprehension history from the bench's digest. The entry is a report beside the panel's: its heading names no `<what> at <commit>`, so nothing that reads the history's rulings and scoring runs takes it for one."""
import pathlib
import re
import typing
from review_rules.comprehension_history import appended
from task_bench.tasks import BUG, KINDS
from task_bench.gather import DIGEST
from failure import Failure
from git_cli.git_repo import Git
from record_store.held import Held
from larv_review.history_commands import held_history
from record_store.stores import RecordStore
from text_files.utc_stamp import utc
# The digest's line that names the commits, as task_bench.digest.digest writes it.
SIDES = re.compile(r"^Base ([0-9a-f]{7,40}), head ([0-9a-f]{7,40})\.", re.M)
# How the table's three rows over every task open, as task_bench.figures.table writes them: the base, the head and the head's change.
OVER_ALL = "| All |"
COMMIT_LENGTH = 8
# What a bench's name says its tasks are, by how its table's header opens, as task_bench.figures.table writes it. A bench of
# bugs has the bare name.
TASKS_NAMED = {f"| {kind.noun.capitalize()} |": f" {tasks}" for tasks, kind in KINDS.items() if kind is not BUG}
def recorded_bench(run: typing.Callable[[], list[str] | Failure], folder: pathlib.Path, git: Git, home: pathlib.Path, now: typing.Callable[[], float]) -> list[str] | Failure:
"""The report of the bench that run runs in folder, plus the line that says where its figures went. It refuses before the bench where the history cannot be read, since a bench takes hours. A bench that fails or stops records nothing."""
branch = git("rev-parse", "--abbrev-ref", "HEAD")
if isinstance(branch, Failure):
return branch
if isinstance(unreadable := held_history(home), Failure):
return unreadable
report = run()
if isinstance(report, Failure):
return report
recorded = record_figures(folder, branch.strip(), home, now())
if isinstance(recorded, Failure):
return Failure(f"the bench finished and {folder} holds its results, but its figures are not recorded: {recorded.reason}")
return [*report, recorded]
def record_figures(folder: pathlib.Path, branch: str, home: pathlib.Path, now: float) -> str | Failure:
"""Appends the entry of the bench whose digest folder holds, stamped with now, and returns the line the tool prints."""
try:
digest = (folder / DIGEST).read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read {folder / DIGEST}: {failure}")
entry = bench_entry(digest, branch, now)
if entry is None:
return Failure(f"{folder / DIGEST} is no task-bench digest: it lacks the line that names its base and head, or the table's three All rows")
read = held_history(home)
return read if isinstance(read, Failure) else bench_appended(*read, *entry)
def bench_appended(history: RecordStore, held: Held, name: str, entry: str) -> str | Failure:
"""Writes a bench's entry at the end of the history and returns the line the tool prints. held is the history as its store read it, and name is what the entry's heading calls the bench. It refuses a bench the history already holds."""
if f", {name}\n" in held.text:
return Failure(f"{history.label} already holds {name}; record each bench once")
written = history.write(appended(held.text, entry), held.version)
return written if isinstance(written, Failure) else f"Appended to {history.label}."
def bench_entry(digest: str, branch: str, now: float) -> tuple[str, str] | None:
"""A bench's name in the history and its entry there, stamped with now: the digest's table header and its three rows over every task. The name says where the bench built features or added data sources, so the history holds it beside a bench of bugs at the same commits. branch is the branch the bench ran on. None where digest is no task-bench digest."""
sides = SIDES.search(digest)
table = [line for line in digest.splitlines() if line.startswith("|")]
rows = [line for line in table if line.startswith(OVER_ALL)]
if not sides or len(rows) != 3:
return None
base, head = (commit[:COMMIT_LENGTH] for commit in sides.groups())
# A panel of three readers, seated as the README's audience, picked this wording on 2026-11-23. Two of the three wanted the bench's name before the commits.
name = f"task-bench{next((named for opening, named in TASKS_NAMED.items() if table[0].startswith(opening)), '')} of {branch}: head {head} against base {base}"
return name, "\n".join([f"## {utc(now)}, {name}", "", *table[:2], *rows]) + "\n"