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

142 lines
7.3 KiB
Python

"""`task-bench --record-figures`: the entry a bench's digest becomes in the comprehension history, and what it refuses."""
import contextlib
import pathlib
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench.record_figures import bench_appended, bench_entry, recorded_bench
from review_rules.comprehension_history import entries
from failure import Failure
from record_store.held import Held
from test_env import isolated_env
from record_store.fakes import MemoryStore
SCORING = "## 2026-10-08T12:00:00Z, PR #1 at aaaaaaa\n\n| Seat | Overall |\n|---|---|\n| Mean | 7.00 |\n"
TABLE = "| Bug | Side | Passed | Output |\n|---|---|---|---|\n"
PER_BUG = "| 1: pick.go:12 | base | 3/4 | 900 |\n| 1: pick.go:12 | head | 4/4 | 700 |\n"
OVER_ALL = "| All | base | 3/4 | 900 |\n| All | head | 4/4 | 700 |\n| All | change | +1 | -22% |\n"
DIGEST = f"# task-bench digest\n\nBase {'a' * 40}, head {'b' * 40}. A bug's name gives its line at the base.\n\n{TABLE}{PER_BUG}{OVER_ALL}\n## 1: pick.go:12\n\n| not | the | table |\n"
PROJECT_CONFIG = """checkout = "~/code/calc"
plan = "checkout:todo.md"
working-rules = "checkout:AGENTS.md"
decisions = "checkout:docs/decisions.md"
score-anchors = "checkout:docs/score-anchors.md"
comprehension-history = "checkout:docs/comprehension-history.md"
reading-cost = "checkout:docs/reading-cost.md"
changelog = "checkout:CHANGELOG.md"
goals-and-audience = "checkout:README.md"
principles = "~/principles"
standards = "~/standards"
"""
# 2026-11-23T10:00:00Z
NOW = 1795428000.0
class RecordFiguresTest(unittest.TestCase):
def setUp(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
self.home = pathlib.Path(folder.name).resolve() / "home"
self.repo = self.home / "code/calc"
self.repo.mkdir(parents=True)
# The project's config is found from the checkout's top, which git names.
subprocess.run(["git", "init", "--quiet"], cwd=self.repo, env={**isolated_env(), "HOME": str(self.home)}, check=True)
config = self.home / ".claude-projects/calc/config.toml"
config.parent.mkdir(parents=True)
config.write_text(PROJECT_CONFIG)
self.bench = self.home / ".local/state/larv-review/code/calc/task-bench"
self.bench.mkdir(parents=True)
self.history = self.repo / "docs/comprehension-history.md"
self.ran = 0
self.asked: list[tuple[str, ...]] = []
self.enterContext(contextlib.chdir(self.repo))
def git(self, *args: str) -> str | Failure:
self.asked.append(args)
return "item-7\n"
def left(self, digest: str | None = DIGEST, report: list[str] | Failure = ["the table"]) -> list[str] | Failure:
"""A bench that leaves digest in its folder and returns report."""
self.ran += 1
if digest is not None:
(self.bench / "digest.md").write_text(digest)
return report
def recorded(self, run=None) -> list[str] | str:
found = recorded_bench(run or self.left, self.bench, self.git, self.home, lambda: NOW)
return found.reason if isinstance(found, Failure) else found
def unrecorded(self, why: str) -> str:
return f"the bench finished and {self.bench} holds its results, but its figures are not recorded: {why}"
def test_appends_the_rows_over_every_bug_under_a_heading_that_is_no_ruling_or_scoring_run_and_keeps_the_benchs_folder(self):
self.history.parent.mkdir()
self.history.write_text("# Comprehension history\n\n" + SCORING)
self.assertEqual(self.recorded(), ["the table", f"Appended to {self.history}."])
entry = f"## 2026-11-23T10:00:00Z, task-bench of item-7: head bbbbbbbb against base aaaaaaaa\n\n{TABLE}{OVER_ALL}"
self.assertEqual(self.history.read_text(), f"# Comprehension history\n\n{SCORING}\n{entry}")
self.assertEqual([entry.heading for entry in entries(self.history.read_text())], ["2026-10-08T12:00:00Z, PR #1 at aaaaaaa"])
self.assertEqual((self.bench / "digest.md").read_text(), DIGEST)
self.assertEqual(self.asked, [("rev-parse", "--abbrev-ref", "HEAD")])
def test_starts_a_history_where_the_project_has_none(self):
self.recorded()
self.assertTrue(self.history.read_text().startswith("# Comprehension history\n\n## 2026-11-23T10:00:00Z, task-bench of item-7: head bbbbbbbb"))
def test_a_bench_that_fails_or_stops_records_nothing_though_it_left_a_digest(self):
self.assertEqual(self.recorded(lambda: self.left(report=Failure("stopped after 2 of 4 runs"))), "stopped after 2 of 4 runs")
self.assertFalse(self.history.exists())
def test_refuses_before_the_bench_where_git_names_no_branch_or_no_config_names_the_checkout(self):
self.git = lambda *args: Failure("git failed")
self.assertEqual(self.recorded(), "git failed")
del self.git
(self.home / ".claude-projects/calc/config.toml").unlink()
self.assertTrue(self.recorded().startswith(f"no config names the checkout {self.repo}"), self.recorded())
self.assertEqual(self.ran, 0)
def test_refuses_a_bench_the_history_already_holds(self):
self.recorded()
held = self.history.read_text()
self.assertEqual(self.recorded(), self.unrecorded(f"{self.history} already holds task-bench of item-7: head bbbbbbbb against base aaaaaaaa; record each bench once"))
self.assertEqual(self.history.read_text(), held)
def test_names_a_bench_of_features_as_one_so_the_history_holds_it_beside_the_bugs_of_the_same_commits(self):
name, entry = bench_entry(DIGEST.replace("| Bug |", "| Feature |"), "item-7", 0)
self.assertEqual(name, "task-bench features of item-7: head bbbbbbbb against base aaaaaaaa")
self.assertTrue(entry.startswith(f"## 1970-01-01T00:00:00Z, {name}\n\n| Feature | Side |"))
def test_names_a_bench_of_data_sources_as_one(self):
name, _ = bench_entry(DIGEST.replace("| Bug |", "| Data source |"), "item-7", 0)
self.assertEqual(name, "task-bench data-sources of item-7: head bbbbbbbb against base aaaaaaaa")
def test_refuses_a_digest_without_its_commits_or_without_its_three_rows_over_every_bug(self):
for digest in (DIGEST.replace("Base ", "From "), DIGEST.replace("| All | change | +1 | -22% |\n", "")):
with self.subTest(digest=digest):
self.assertEqual(self.recorded(lambda: self.left(digest)), self.unrecorded(f"{self.bench / 'digest.md'} is no task-bench digest: it lacks the line that names its base and head, or the table's three All rows"))
self.assertFalse(self.history.exists())
def test_refuses_a_digest_it_cannot_read(self):
self.assertTrue(self.recorded(lambda: self.left(None)).startswith(self.unrecorded(f"could not read {self.bench / 'digest.md'}: [Errno ")), self.recorded())
def test_says_why_where_the_history_cannot_be_read_once_the_bench_has_run(self):
def run():
(self.home / ".claude-projects/calc/config.toml").unlink()
return self.left()
self.assertTrue(self.recorded(run).startswith(self.unrecorded(f"no config names the checkout {self.repo}")))
def test_writes_over_the_version_the_history_was_read_at_and_says_why_a_write_fails(self):
history = MemoryStore(label="history.md", version=5)
self.assertEqual(bench_appended(history, Held("", "5"), "task-bench of item-7: head b against base a", "## an entry\n"), "Appended to history.md.")
self.assertEqual(history.overs, ["5"])
refused = bench_appended(history, Held("", "5"), "task-bench of item-8: head b against base a", "## an entry\n")
self.assertEqual(refused.reason if isinstance(refused, Failure) else refused, "someone saved history.md since version 5")
if __name__ == "__main__":
unittest.main()