#!/usr/bin/env python3 """Run larv-review bench runs: BENCH_SOURCES= bench.py -- ... The 2026-09-30 bench ran two versions of larv-review, main and rebuild. Each reviewed four real chunks, the cases, three times; compare.py then rules whether each version reviews at least as well as the other. The bench's inputs are untracked: - versions// beside this script: a copy of ~/.claude at that version, which each run reads in place of ~/.claude (run-prompt.md). - $BENCH_SOURCES/-main/: a clone of the case's project; CASES names the chunk's base and head commits in it. - $BENCH_SOURCES/-main.md: main's own review of the case, run on 2026-09-29. - $BENCH_SOURCES/comparison.md: what each case's real review found, one table row per issue, its ID led by the case's letter in compare.py's PREFIX. Each run leaves its clone in /, its report in .md and its claude stream in logs/. """ import concurrent.futures import json import os import pathlib import shutil import subprocess import sys import time BENCH = pathlib.Path(__file__).resolve().parent SOURCES = pathlib.Path(os.environ["BENCH_SOURCES"]) CASES = { "cjk": ("151bf38^", "6c49912"), "35b": ("692433b", "1d369ec"), "brlan": ("59ce55b^", "92e466a"), "mypy": ("89a66d3", "b32d131"), } ATTEMPTS = 4 def permission_flags(clone, version_dir, report): """The tools a review needs, each limited to what it may touch; the run denies every other call.""" # A permission rule writes an absolute path as "//"; a trailing "/**" matches everything under it. trees = (f"/{clone}/**", f"/{version_dir}/**") git = ("log", "diff", "show", "status", "ls-files", "add", "commit -m", "commit -am") allowed = [ "Agent", "Skill(security-review)", f"Edit(/{clone}/**)", f"Edit(/{report})", *(f"Bash(git {command} *)" for command in git), *(f"{tool}({tree})" for tool in ("Read", "Grep", "Glob") for tree in trees), ] # git reads or writes any path through these options, and runs any command through a hook or config. denied = [f"Edit(/{clone}/.git/**)", *(f"Bash(git * {option}*)" for option in ("--no-index", "--output", "-F", "--file", "-t", "--template"))] # Only the clone's own settings: the user's add every checkout under ~/code to the readable trees. return ["--setting-sources", "project,local", "--permission-mode", "dontAsk", "--permission-prompts", "none", "--allowedTools", *allowed, "--disallowedTools", *denied] def git(repo, *args): return subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True).stdout.strip() def fresh_clone(case, dest): if dest.exists(): shutil.rmtree(dest) source = SOURCES / f"{case}-main" base_ref, head_ref = CASES[case] base = git(source, "rev-parse", base_ref) head = git(source, "rev-parse", head_ref) subprocess.run(["git", "clone", "-q", "--no-local", str(source), str(dest)], check=True) git(dest, "checkout", "-q", "-B", "chunk", head) for branch in git(dest, "for-each-ref", "--format=%(refname:short)", "refs/heads").split(): if branch != "chunk": git(dest, "branch", "-D", branch) for tag in git(dest, "tag").split(): git(dest, "tag", "-d", tag) git(dest, "remote", "remove", "origin") git(dest, "reflog", "expire", "--expire=now", "--all") git(dest, "gc", "-q", "--prune=now") return base, head def rate_limit_reset(log): resets = [0] for line in log.read_text().splitlines(): if '"status":"rejected"' in line: resets.append(json.loads(line)["rate_limit_info"]["resetsAt"]) return max(resets) def run(name): case, version, _ = name.split("-") clone = BENCH / name report = BENCH / f"{name}.md" for attempt in range(1, ATTEMPTS + 1): if report.exists(): return f"{name}: done" base, head = fresh_clone(case, clone) version_dir = BENCH / "versions" / version prompt = (BENCH / "run-prompt.md").read_text().format( version_dir=version_dir, base=base, head=head, case=case, report=report) log = BENCH / "logs" / f"{name}.{attempt}.jsonl" log.parent.mkdir(exist_ok=True) with log.open("w") as out: subprocess.run( ["claude", "-p", prompt, *permission_flags(clone, version_dir, report), "--model", "claude-opus-5-5", "--output-format", "stream-json", "--verbose"], cwd=clone, stdout=out, stderr=subprocess.STDOUT) if report.exists(): return f"{name}: done on attempt {attempt}" time.sleep(max(1200, rate_limit_reset(log) + 120 - time.time())) return f"{name}: FAILED after {ATTEMPTS} attempts" def main(): with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool: for result in pool.map(run, sys.argv[1:]): print(result, flush=True) if __name__ == "__main__": main()