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

72 lines
3.9 KiB
Python

"""`task-bench record-figures`: a bench's figures over every bug, 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 functools
import pathlib
import re
import typing
from review_rules.comprehension_history import appended
from task_bench.gather import DIGEST
from failure import Failure
from git_cli import git_repo
from task_bench.bench_folder import held
from record_store.held import Held
from larv_review.history_commands import held_history
from review_rules.review_folder import made_review_folder
from record_store.stores import RecordStore
from git_cli.git_repo import top
from text_files.utc_stamp import utc
# A panel of three readers, seated as the README's audience, picked on 2026-11-23 the subcommand's name, 3 to 0, and the heading's wording, `task-bench of <branch>: head <commit> against base <commit>`, 2 to 1 for the bench's name first.
# 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 bug open, as task_bench.figures.table writes them: the base, the head and the head's change.
OVER_ALL = "| All |"
COMMIT_LENGTH = 8
def record_figures(env: typing.Mapping[str, str], home: pathlib.Path, now: float) -> str | Failure:
"""Appends the entry of the bench whose digest the checkout's review folder holds, stamped with now, and returns the line the tool prints. It waits for a bench that still runs there, and leaves the folder for gather."""
git = functools.partial(git_repo.git, env=env)
checkout = top(git)
if isinstance(checkout, Failure):
return checkout
branch = git("rev-parse", "--abbrev-ref", "HEAD")
if isinstance(branch, Failure):
return branch
review = made_review_folder(checkout, home)
if isinstance(review, Failure):
return review
with held(review) as folder:
if isinstance(folder, Failure):
return folder
try:
digest = (folder / DIGEST).read_text(encoding="utf-8")
except FileNotFoundError:
return Failure(f"{folder} holds no digest: run task-bench in this checkout first, and record its figures before `task-bench gather`")
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read {folder / DIGEST}: {failure}")
entry = bench_entry(digest, branch.strip(), 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 appended_to(*read, *entry)
def appended_to(history: RecordStore, held: Held, name: str, entry: str) -> str | Failure:
"""Writes entry, of the bench the history calls name, onto held, the history as history's store read it, over held's version; the line the tool prints, or why the entry isn't written."""
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:
"""What the history calls the bench of branch that digest tells of, and its entry, stamped with now: the table's header and its rows over every bug. 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())
name = f"task-bench of {branch}: head {head} against base {base}"
return name, "\n".join([f"## {utc(now)}, {name}", "", *table[:2], *rows]) + "\n"