Answer principles round 2: 0 🔴, 2 🟡 (0 misread); workflow-bench hands its effects in as arguments, and no Python file of the repo patches a global
CI / tests (pull_request) Successful in 3m46s

This commit is contained in:
2026-11-19 12:55:35 +01:00
parent cb3135bc8c
commit 762614353c
18 changed files with 335 additions and 231 deletions
+41 -22
View File
@@ -13,11 +13,13 @@ Run each arm of each case once; the table shows a direction, not a significance.
"""
import concurrent.futures
import dataclasses
import functools
import json
import pathlib
import re
import subprocess
import sys
import typing
BENCH = pathlib.Path(__file__).resolve().parent
REPO = BENCH.parent
@@ -52,6 +54,17 @@ VERDICTS = ("pass", "partial", "fail")
JUDGE = Arm("claude-opus-5-5", "medium")
@dataclasses.dataclass(frozen=True)
class Bench:
"""What a bench's steps act on, which the script's entry point hands them."""
# The repo whose archived chunks the bench reruns.
repo: pathlib.Path
# Each case's start commit and the commit it merged at, by todo item.
cases: dict
# Runs claude and the tests, as subprocess.run does.
process: typing.Callable
@dataclasses.dataclass(frozen=True)
class Outcome:
cost: float
@@ -124,51 +137,51 @@ def git(repo, *args):
return subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True).stdout
def fresh_clone(base, dest):
"""A clone of this repo at base, with no remote to push to."""
subprocess.run(["git", "clone", "-q", "--no-local", str(REPO), str(dest)], check=True)
def fresh_clone(repo, base, dest):
"""A clone of repo at base, with no remote to push to."""
subprocess.run(["git", "clone", "-q", "--no-local", str(repo), str(dest)], check=True)
git(dest, "checkout", "-q", "--detach", base)
git(dest, "remote", "remove", "origin")
def run(work, name):
def run(bench, work, name):
"""One worker run of <case>-<arm>-<n>; the files it leaves are in the module docstring."""
case, arm, _ = split_name(name)
base = CASES[case][0]
base = bench.cases[case][0]
clone = work / name
if clone.exists():
raise ValueError(f"{name} already ran: remove {clone}")
fresh_clone(base, clone)
prompt = (BENCH / "advisor-run-prompt.md").read_text().format(base=base, item=case, text=item_text(git(REPO, "show", f"{base}:todo.md"), case))
fresh_clone(bench.repo, base, clone)
prompt = (BENCH / "advisor-run-prompt.md").read_text().format(base=base, item=case, text=item_text(git(bench.repo, "show", f"{base}:todo.md"), case))
with (work / f"{name}.jsonl").open("w") as out:
subprocess.run(
bench.process(
["claude", "-p", prompt, *ARMS[arm].flags(), "--setting-sources", "project", "--permission-mode", "bypassPermissions",
"--no-session-persistence", "--output-format", "stream-json", "--verbose"],
cwd=clone, stdout=out, stderr=subprocess.STDOUT)
read_result((work / f"{name}.jsonl").read_text().splitlines())
save_change(work, name, clone, CASES[case])
save_change(bench, work, name, clone, bench.cases[case])
return f"{name}: ran"
def save_change(work, name, clone, commits):
def save_change(bench, work, name, clone, commits):
"""Save the clone's change since the case's start commit as <name>.diff, and as <name>.tests the result of the tests of the packages that the clone's change or the merged change touches."""
base, merged = commits
git(clone, "add", "--all")
(work / f"{name}.diff").write_text(git(clone, "diff", "--cached", base))
changed = [*git(clone, "diff", "--cached", "--name-only", base).split(), *git(REPO, "diff", "--name-only", base, merged).split()]
changed = [*git(clone, "diff", "--cached", "--name-only", base).split(), *git(bench.repo, "diff", "--name-only", base, merged).split()]
targets = [target for target in suite_targets(changed) if (clone / target).exists()] or ["bin"]
tests = subprocess.run([str(RUN_HEAVY), *SUITE, *targets], cwd=clone, capture_output=True, text=True)
tests = bench.process([str(RUN_HEAVY), *SUITE, *targets], cwd=clone, capture_output=True, text=True)
(work / f"{name}.tests").write_text(f"exit {tests.returncode}\n{tests.stdout[-1500:]}")
def grade(work, name, cases=None):
"""Have the judge rule on one run's diff, blind to its arm, and save the verdict beside it. cases stands in for CASES where another bench grades its own."""
def grade(bench, work, name):
"""Have the judge rule on one run's diff, blind to its arm, and save the verdict beside it."""
case = split_name(name)[0]
base, merged = (cases or CASES)[case]
base, merged = bench.cases[case]
prompt = (BENCH / "advisor-judge-prompt.md").read_text().format(
text=item_text(git(REPO, "show", f"{base}:todo.md"), case), reference=git(REPO, "diff", base, merged, "--", ".", ":!todo.md", ":!docs", ":!CHANGELOG.md"),
text=item_text(git(bench.repo, "show", f"{base}:todo.md"), case), reference=git(bench.repo, "diff", base, merged, "--", ".", ":!todo.md", ":!docs", ":!CHANGELOG.md"),
diff=(work / f"{name}.diff").read_text(), tests=(work / f"{name}.tests").read_text())
reply = subprocess.run(
reply = bench.process(
["claude", "-p", prompt, *JUDGE.flags(), "--setting-sources", "", "--no-session-persistence", "--output-format", "json", "--tools", ""],
capture_output=True, text=True, check=True).stdout
verdict = verdict_of(reply)
@@ -196,15 +209,21 @@ def rows_of(work):
return rows
def main(argv):
def steps_of(bench):
"""The steps main runs by name, each on bench."""
return {"run": functools.partial(run, bench), "grade": functools.partial(grade, bench)}
def main(argv, steps, say):
"""Runs the step argv names on each run it names, and hands say each result line; or hands say the summary."""
work, step, names = pathlib.Path(argv[0]), argv[1], argv[2:]
if step == "summary":
print(summary(rows_of(work)))
say(summary(rows_of(work)))
return
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:
for result in pool.map(lambda name: attempted({"run": run, "grade": grade}[step], work, name), names):
print(result, flush=True)
for result in pool.map(lambda name: attempted(steps[step], work, name), names):
say(result)
if __name__ == "__main__":
main(sys.argv[1:])
main(sys.argv[1:], steps_of(Bench(REPO, CASES, subprocess.run)), functools.partial(print, flush=True))
+12 -10
View File
@@ -14,6 +14,7 @@ other. The bench's inputs are untracked:
Each run leaves its clone in <run>/, its report in <run>.md and its claude stream in logs/.
"""
import concurrent.futures
import functools
import json
import os
import pathlib
@@ -24,7 +25,6 @@ import time
BENCH = pathlib.Path(__file__).resolve().parent
REVIEW_GIT = BENCH / "review_git.py"
SOURCES = pathlib.Path(os.environ["BENCH_SOURCES"])
CASES = {
"cjk": ("151bf38^", "6c49912"),
"35b": ("692433b", "1d369ec"),
@@ -61,10 +61,10 @@ 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):
def fresh_clone(case, dest, sources):
if dest.exists():
shutil.rmtree(dest)
source = SOURCES / f"{case}-main"
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)
@@ -89,14 +89,15 @@ def rate_limit_reset(log):
return max(resets)
def run(name):
def run(name, sources):
"""One review run of <case>-<version>-<n>, whose case's clone is in the folder sources."""
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)
base, head = fresh_clone(case, clone, sources)
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, review_git=REVIEW_GIT)
@@ -113,19 +114,20 @@ def run(name):
return f"{name}: FAILED after {ATTEMPTS} attempts"
def attempted(name):
"""run's result line, or a line naming the failure, so one failed run doesn't stop the other runs' results printing."""
def attempted(name, run):
"""run's result line for name, or a line naming the failure, so one failed run doesn't stop the other runs' results printing."""
try:
return run(name)
except (KeyError, ValueError, subprocess.CalledProcessError, OSError) as failure:
return f"{name}: FAILED: {type(failure).__name__}: {failure}"
def main():
def main(names, run):
"""Prints the result line of each run named, which run makes."""
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
for result in pool.map(attempted, sys.argv[1:]):
for result in pool.map(lambda name: attempted(name, run), names):
print(result, flush=True)
if __name__ == "__main__":
main()
main(sys.argv[1:], functools.partial(run, sources=pathlib.Path(os.environ["BENCH_SOURCES"])))
+44 -22
View File
@@ -18,12 +18,15 @@ Run each arm of each case once; the table shows a direction, not a significance.
"""
import collections
import concurrent.futures
import dataclasses
import functools
import json
import pathlib
import shutil
import subprocess
import sys
import threading
import typing
import uuid
import advisor_bench
@@ -54,6 +57,18 @@ START_BUDGET = 0.36
CLAUDE_FLAGS = ["--setting-sources", "project", "--permission-mode", "bypassPermissions", "--output-format", "stream-json", "--verbose"]
@dataclasses.dataclass(frozen=True)
class Handoff:
"""What this bench's steps act on, which the script's entry point hands them."""
bench: advisor_bench.Bench
# Dollars WEAK may spend before the handoff.
budget: float
# Where Claude Code keeps each session's transcript.
projects: pathlib.Path
# Makes the ID of the session `start` opens, as uuid.uuid4 does.
new_session: typing.Callable
def stopped_at_budget(lines):
"""Whether a claude stream's first result line, the worker's own, says the run stopped at --max-budget-usd."""
for line in lines:
@@ -66,34 +81,35 @@ def stopped_at_budget(lines):
return False
def run_prompt(case):
def run_prompt(bench, case):
"""advisor_bench's prompt for the case's item, as the item's text stood at the start commit."""
base = CASES[case][0]
text = advisor_bench.item_text(advisor_bench.git(advisor_bench.REPO, "show", f"{base}:todo.md"), case)
base = bench.cases[case][0]
text = advisor_bench.item_text(advisor_bench.git(bench.repo, "show", f"{base}:todo.md"), case)
return (BENCH / "advisor-run-prompt.md").read_text().format(base=base, item=case, text=text)
def claude(prompt, flags, clone, stream):
def claude(process, prompt, flags, clone, stream):
with stream.open("w") as out:
subprocess.run(["claude", "-p", prompt, *flags, *CLAUDE_FLAGS], cwd=clone, stdin=subprocess.DEVNULL, stdout=out, stderr=subprocess.STDOUT)
process(["claude", "-p", prompt, *flags, *CLAUDE_FLAGS], cwd=clone, stdin=subprocess.DEVNULL, stdout=out, stderr=subprocess.STDOUT)
def start(work, name):
def start(handoff, work, name):
"""WEAK's capped run of <case>-<n>, and the tree and session it leaves the arms; the files are in the module docstring."""
case, number = name.split("-")
base = CASES[case][0]
bench = handoff.bench
base = bench.cases[case][0]
clone, saved = work / name, work / f"{case}-start-{number}"
if clone.exists():
raise ValueError(f"{name} already started: remove {clone}")
advisor_bench.fresh_clone(base, clone)
session = str(uuid.uuid4())
advisor_bench.fresh_clone(bench.repo, base, clone)
session = str(handoff.new_session())
stream = saved.with_suffix(".jsonl")
claude(run_prompt(case), [*WEAK.flags(), "--session-id", session, "--max-budget-usd", str(START_BUDGET)], clone, stream)
claude(bench.process, run_prompt(bench, case), [*WEAK.flags(), "--session-id", session, "--max-budget-usd", str(handoff.budget)], clone, stream)
if not stopped_at_budget(stream.read_text().splitlines()):
raise ValueError("the weak worker finished within its budget, or its run failed, so there is no handoff to bench")
transcripts = list(CLAUDE_PROJECTS.glob(f"*/{session}.jsonl"))
transcripts = list(handoff.projects.glob(f"*/{session}.jsonl"))
if len(transcripts) != 1:
raise ValueError(f"expected one transcript of session {session} under {CLAUDE_PROJECTS}, found {len(transcripts)}")
raise ValueError(f"expected one transcript of session {session} under {handoff.projects}, found {len(transcripts)}")
shutil.copy(transcripts[0], saved.with_suffix(".session.jsonl"))
saved.with_suffix(".session").write_text(str(transcripts[0]))
# The worker may have committed against the bench rules, so the arms get its whole change since base, uncommitted.
@@ -104,9 +120,10 @@ def start(work, name):
return f"{name}: started"
def run(work, name):
def run(handoff, work, name):
"""STRONG's run of one arm, <case>-<arm>-<n>, from the tree and session `start` left."""
case, arm, number = advisor_bench.split_name(name)
bench = handoff.bench
if arm not in ARMS:
raise ValueError(f"the arm {arm!r} is none of {ARMS}")
clone, saved = work / f"{case}-{number}", work / f"{case}-start-{number}"
@@ -122,15 +139,15 @@ def run(work, name):
shutil.copy(saved.with_suffix(".session.jsonl"), transcript)
resumed = ["--resume", transcript.stem]
if arm == "compact":
claude("/compact", [*WEAK.flags(), *resumed], clone, work / f"{name}.compact.jsonl")
claude(bench.process, "/compact", [*WEAK.flags(), *resumed], clone, work / f"{name}.compact.jsonl")
finished(work / f"{name}.compact.jsonl")
if arm == "brief":
claude(run_prompt(case) + "\n" + (BENCH / "handoff-brief-prompt.md").read_text(), [*STRONG.flags(), "--no-session-persistence"], clone, stream)
claude(bench.process, run_prompt(bench, case) + "\n" + (BENCH / "handoff-brief-prompt.md").read_text(), [*STRONG.flags(), "--no-session-persistence"], clone, stream)
else:
prompt = (BENCH / "handoff-resume-prompt.md").read_text().format(rung=f"{STRONG.model} at {STRONG.effort} effort")
claude(prompt, [*STRONG.flags(), *resumed], clone, stream)
claude(bench.process, prompt, [*STRONG.flags(), *resumed], clone, stream)
outcome(work, name)
advisor_bench.save_change(work, name, clone, CASES[case])
advisor_bench.save_change(bench, work, name, clone, bench.cases[case])
return f"{name}: ran"
@@ -185,16 +202,21 @@ def summary(rows):
return "\n".join(lines)
def main(argv):
def steps_of(handoff):
"""The steps main runs by name, each on handoff; the grade is advisor_bench's, over handoff's cases."""
return {"start": functools.partial(start, handoff), "run": functools.partial(run, handoff), "grade": functools.partial(advisor_bench.grade, handoff.bench)}
def main(argv, steps, say):
"""Runs the step argv names on each run it names, and hands say each result line; or hands say the summary."""
work, step, names = pathlib.Path(argv[0]), argv[1], argv[2:]
if step == "summary":
print(summary(rows_of(work)))
say(summary(rows_of(work)))
return
steps = {"start": start, "run": run, "grade": lambda work, name: advisor_bench.grade(work, name, CASES)}
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:
for result in pool.map(lambda name: advisor_bench.attempted(steps[step], work, name), names):
print(result, flush=True)
say(result)
if __name__ == "__main__":
main(sys.argv[1:])
main(sys.argv[1:], steps_of(Handoff(advisor_bench.Bench(advisor_bench.REPO, CASES, subprocess.run), START_BUDGET, CLAUDE_PROJECTS, uuid.uuid4)), functools.partial(print, flush=True))
+38 -38
View File
@@ -1,11 +1,9 @@
import contextlib
import io
import json
import pathlib
import shutil
import subprocess
import tempfile
import unittest
import unittest.mock
import advisor_bench
@@ -149,15 +147,11 @@ class Orchestration(unittest.TestCase):
merged = self.commit({"todo.md": TODO + "\nmore\n", "bin/pkg/mod.py": "x = 2\n", "bin/other/mod.py": "z = 1\n", "docs/notes.md": "b\n", "CHANGELOG.md": "b\n"})
self.commit({"bin/pkg/mod.py": "x = 5\n", "bin/later/mod.py": "y = 1\n"})
self.calls = []
real_run = subprocess.run
for patch in (
unittest.mock.patch.object(advisor_bench, "REPO", self.repo), unittest.mock.patch.object(advisor_bench, "CASES", {"7": (base, merged)}),
unittest.mock.patch.object(advisor_bench.subprocess, "run", side_effect=lambda args, **kwargs: self.fake(real_run, args, **kwargs)),
):
patch.start()
self.addCleanup(patch.stop)
self.bench = advisor_bench.Bench(self.repo, {"7": (base, merged)}, self.fake)
self.tests_output = "3 passed\n"
self.worker_result = {}
# Whether the fake worker removes bin/ in place of changing it.
self.worker_removes_bin = False
self.base = base
def commit(self, files):
@@ -168,26 +162,30 @@ class Orchestration(unittest.TestCase):
commit(self.repo, "a commit")
return advisor_bench.git(self.repo, "rev-parse", "HEAD").strip()
def fake(self, real_run, args, **kwargs):
def fake(self, args, **kwargs):
"""Stands in for subprocess.run where the bench runs claude or the tests."""
if args[0] == "claude":
self.calls.append((args, kwargs))
if "--tools" in args:
return subprocess.CompletedProcess(args, 0, stdout=json.dumps({"result": '{"verdict": "pass", "why": "ok"}'}), stderr="")
pathlib.Path(kwargs["cwd"], "bin/pkg/mod.py").write_text("x = 3\n")
pathlib.Path(kwargs["cwd"], "bin/pkg/new.py").write_text("n = 1\n")
pathlib.Path(kwargs["cwd"], "bin/third").mkdir()
pathlib.Path(kwargs["cwd"], "bin/third/mod.py").write_text("t = 1\n")
subprocess.run(["git", "-C", str(kwargs["cwd"]), "add", "bin/third"], check=True)
if self.worker_removes_bin:
shutil.rmtree(pathlib.Path(kwargs["cwd"], "bin"))
else:
pathlib.Path(kwargs["cwd"], "bin/pkg/mod.py").write_text("x = 3\n")
pathlib.Path(kwargs["cwd"], "bin/pkg/new.py").write_text("n = 1\n")
pathlib.Path(kwargs["cwd"], "bin/third").mkdir()
pathlib.Path(kwargs["cwd"], "bin/third/mod.py").write_text("t = 1\n")
subprocess.run(["git", "-C", str(kwargs["cwd"]), "add", "bin/third"], check=True)
commit(kwargs["cwd"], "the worker commits")
kwargs["stdout"].write(result_line(**self.worker_result) + "\n")
return subprocess.CompletedProcess(args, 0)
if args[0] == str(advisor_bench.RUN_HEAVY):
self.calls.append((args, kwargs))
return subprocess.CompletedProcess(args, 4, stdout=self.tests_output, stderr="")
return real_run(args, **kwargs)
raise AssertionError(f"the bench ran {args}")
def test_run_clones_at_the_start_commit_and_saves_the_stream_the_diff_and_the_tests(self):
self.assertEqual(advisor_bench.run(self.work, "7-sonnet-advisor-1"), "7-sonnet-advisor-1: ran")
self.assertEqual(advisor_bench.run(self.bench, self.work, "7-sonnet-advisor-1"), "7-sonnet-advisor-1: ran")
(claude, claude_kwargs), (tests, tests_kwargs) = self.calls
clone = self.work / "7-sonnet-advisor-1"
self.assertEqual((claude_kwargs["cwd"], tests_kwargs["cwd"]), (clone, clone))
@@ -211,39 +209,39 @@ class Orchestration(unittest.TestCase):
self.assertEqual((self.work / "7-sonnet-advisor-1.tests").read_text(), "exit 4\n3 passed\n")
def test_run_tests_all_of_bin_where_no_touched_package_is_in_the_clone(self):
with unittest.mock.patch.object(advisor_bench, "suite_targets", return_value=["bin/absent"]):
advisor_bench.run(self.work, "7-sonnet-1")
self.worker_removes_bin = True
advisor_bench.run(self.bench, self.work, "7-sonnet-1")
self.assertEqual(self.calls[1][0][-1:], ["bin"])
def test_run_refuses_a_run_that_ended_in_an_error_before_it_tests(self):
self.worker_result = {"is_error": True, "result": "API Error"}
with self.assertRaisesRegex(ValueError, "API Error"):
advisor_bench.run(self.work, "7-sonnet-1")
advisor_bench.run(self.bench, self.work, "7-sonnet-1")
self.assertEqual(len(self.calls), 1)
self.assertFalse((self.work / "7-sonnet-1.tests").exists())
def test_run_keeps_the_last_1500_characters_of_the_tests(self):
self.tests_output = "a" * 2000 + "b" * 1500
advisor_bench.run(self.work, "7-sonnet-1")
advisor_bench.run(self.bench, self.work, "7-sonnet-1")
self.assertEqual((self.work / "7-sonnet-1.tests").read_text(), "exit 4\n" + "b" * 1500)
def test_run_refuses_a_name_that_already_ran(self):
advisor_bench.run(self.work, "7-sonnet-1")
advisor_bench.run(self.bench, self.work, "7-sonnet-1")
with self.assertRaisesRegex(ValueError, "7-sonnet-1 already ran: remove .*7-sonnet-1"):
advisor_bench.run(self.work, "7-sonnet-1")
advisor_bench.run(self.bench, self.work, "7-sonnet-1")
def test_a_clone_of_a_missing_repo_fails(self):
with unittest.mock.patch.object(advisor_bench, "REPO", self.work / "missing"), self.assertRaises(subprocess.CalledProcessError):
advisor_bench.fresh_clone(self.base, self.work / "clone")
with self.assertRaises(subprocess.CalledProcessError):
advisor_bench.fresh_clone(self.work / "missing", self.base, self.work / "clone")
def test_git_fails_where_git_does(self):
with self.assertRaises(subprocess.CalledProcessError):
advisor_bench.git(self.repo, "rev-parse", "no-such-revision")
def test_grade_saves_the_verdict_and_shows_the_judge_both_changes_but_not_the_arm(self):
advisor_bench.run(self.work, "7-sonnet-advisor-1")
advisor_bench.run(self.bench, self.work, "7-sonnet-advisor-1")
self.calls.clear()
self.assertEqual(advisor_bench.grade(self.work, "7-sonnet-advisor-1"), "7-sonnet-advisor-1: pass")
self.assertEqual(advisor_bench.grade(self.bench, self.work, "7-sonnet-advisor-1"), "7-sonnet-advisor-1: pass")
((judge, kwargs),) = self.calls
self.assertEqual(judge[:2] + judge[3:], [
"claude", "-p", *advisor_bench.JUDGE.flags(), "--setting-sources", "", "--no-session-persistence", "--output-format", "json", "--tools", ""])
@@ -281,28 +279,30 @@ class Orchestration(unittest.TestCase):
with self.assertRaisesRegex(ValueError, "^7-sonnet-1: the stream has no result line$"):
advisor_bench.rows_of(self.work)
def run_main(self, *argv):
out = io.StringIO()
with contextlib.redirect_stdout(out):
advisor_bench.main([str(self.work), *argv])
return out.getvalue()
def run_main(self, steps, *argv):
said = []
advisor_bench.main([str(self.work), *argv], steps, said.append)
return "".join(f"{line}\n" for line in said)
def test_main_prints_each_runs_line_though_an_earlier_run_failed(self):
def step(work, name):
if name == "7-a-1":
raise ValueError("no")
return f"{name}: ran"
with unittest.mock.patch.object(advisor_bench, "run", step):
self.assertEqual(self.run_main("run", "7-a-1", "7-b-1"), "7-a-1: FAILED: ValueError: no\n7-b-1: ran\n")
self.assertEqual(self.run_main({"run": step}, "run", "7-a-1", "7-b-1"), "7-a-1: FAILED: ValueError: no\n7-b-1: ran\n")
def test_main_grades_on_grade(self):
with unittest.mock.patch.object(advisor_bench, "grade", lambda work, name: f"{work.name} {name}: graded"):
self.assertEqual(self.run_main("grade", "7-a-1"), "work 7-a-1: graded\n")
self.assertEqual(self.run_main({"grade": lambda work, name: f"{work.name} {name}: graded"}, "grade", "7-a-1"), "work 7-a-1: graded\n")
def test_the_steps_run_and_grade_on_the_bench_they_are_given(self):
steps = advisor_bench.steps_of(self.bench)
self.assertEqual(self.run_main(steps, "run", "7-sonnet-1"), "7-sonnet-1: ran\n")
self.assertEqual(self.run_main(steps, "grade", "7-sonnet-1"), "7-sonnet-1: pass\n")
def test_main_prints_the_work_dirs_summary_on_summary(self):
(self.work / "7-sonnet-1.jsonl").write_text(result_line() + "\n")
(self.work / "7-sonnet-1.verdict").write_text(json.dumps({"verdict": "pass"}))
self.assertIn("| sonnet | 1 | 1 | 0 | 0.50 | 0.0 | 12 |", self.run_main("summary"))
self.assertIn("| sonnet | 1 | 1 | 0 | 0.50 | 0.0 | 12 |", self.run_main({}, "summary"))
if __name__ == "__main__":
+22 -12
View File
@@ -1,13 +1,23 @@
import contextlib
import functools
import io
import os
import pathlib
import subprocess
import sys
import unittest
import unittest.mock
with unittest.mock.patch.dict(os.environ, {"BENCH_SOURCES": "/nonexistent"}):
import bench
import bench
def answering(*answers):
"""A run that gives the next of answers on each call, raising the one that is a failure."""
left = list(answers)
def run(name):
answer = left.pop(0)
if isinstance(answer, Exception):
raise answer
return answer
return run
class Main(unittest.TestCase):
@@ -17,27 +27,27 @@ class Main(unittest.TestCase):
raise KeyError("a")
return f"{name}: done"
out = io.StringIO()
with unittest.mock.patch.object(bench, "run", side_effect=run), unittest.mock.patch.object(sys, "argv", ["bench.py", "a-v-1", "b-v-1"]), contextlib.redirect_stdout(out):
bench.main()
with contextlib.redirect_stdout(out):
bench.main(["a-v-1", "b-v-1"], run)
self.assertEqual(out.getvalue().splitlines(), ["a-v-1: FAILED: KeyError: 'a'", "b-v-1: done"])
class Attempted(unittest.TestCase):
def test_returns_the_run_result_of_a_run_that_finished(self):
with unittest.mock.patch.object(bench, "run", return_value="a-v-1: done"):
self.assertEqual(bench.attempted("a-v-1"), "a-v-1: done")
self.assertEqual(bench.attempted("a-v-1", answering("a-v-1: done")), "a-v-1: done")
def test_returns_a_failed_run_as_a_line_naming_the_run_and_its_error_so_later_runs_still_report(self):
failures = [KeyError("nope"), ValueError("not enough values to unpack"), subprocess.CalledProcessError(128, ["git", "clone"]), FileNotFoundError("claude")]
for failure in failures:
with self.subTest(failure=type(failure).__name__), unittest.mock.patch.object(bench, "run", side_effect=[failure, "b-v-1: done"]):
results = [bench.attempted("a-v-1"), bench.attempted("b-v-1")]
with self.subTest(failure=type(failure).__name__):
run = answering(failure, "b-v-1: done")
results = [bench.attempted("a-v-1", run), bench.attempted("b-v-1", run)]
self.assertTrue(results[0].startswith("a-v-1: FAILED: "), results[0])
self.assertIn(type(failure).__name__, results[0])
self.assertEqual(results[1], "b-v-1: done")
def test_a_run_of_an_unknown_case_fails_as_a_line(self):
self.assertRegex(bench.attempted("nope-v-1"), r"^nope-v-1: FAILED: KeyError")
self.assertRegex(bench.attempted("nope-v-1", functools.partial(bench.run, sources=pathlib.Path("/nonexistent"))), r"^nope-v-1: FAILED: KeyError")
if __name__ == "__main__":
+41 -44
View File
@@ -3,7 +3,6 @@ import pathlib
import subprocess
import tempfile
import unittest
import unittest.mock
import uuid
import advisor_bench
@@ -62,17 +61,9 @@ class Orchestration(unittest.TestCase):
self.calls, self.heavy = [], []
self.start_result = {"subtype": "error_max_budget_usd", "is_error": True, "total_cost_usd": 1.0}
self.compact_result, self.strong_result = {}, {}
real_run = subprocess.run
for patch in (
unittest.mock.patch.object(advisor_bench, "REPO", self.repo),
unittest.mock.patch.object(handoff_bench, "CASES", {"7": (self.base, merged)}),
unittest.mock.patch.object(handoff_bench, "START_BUDGET", 0.25),
unittest.mock.patch.object(handoff_bench, "CLAUDE_PROJECTS", self.projects),
unittest.mock.patch.object(handoff_bench.uuid, "uuid4", return_value=SESSION),
unittest.mock.patch.object(advisor_bench.subprocess, "run", side_effect=lambda args, **kwargs: self.fake(real_run, args, **kwargs)),
):
patch.start()
self.addCleanup(patch.stop)
# The prompts the judge was given.
self.judged = []
self.handoff = handoff_bench.Handoff(advisor_bench.Bench(self.repo, {"7": (self.base, merged)}, self.fake), 0.25, self.projects, lambda: SESSION)
self.transcript = self.projects / "slug" / f"{SESSION}.jsonl"
def commit(self, files):
@@ -83,7 +74,11 @@ class Orchestration(unittest.TestCase):
commit(self.repo, "a commit")
return advisor_bench.git(self.repo, "rev-parse", "HEAD").strip()
def fake(self, real_run, args, **kwargs):
def fake(self, args, **kwargs):
"""Stands in for subprocess.run where the bench runs claude or the tests."""
if args[0] == "claude" and "--tools" in args:
self.judged.append(args[2])
return subprocess.CompletedProcess(args, 0, stdout=json.dumps({"result": '{"verdict": "pass"}'}), stderr="")
if args[0] == "claude":
cwd = pathlib.Path(kwargs["cwd"])
# What the run found as it began: the session's transcript and the files under bin/pkg.
@@ -92,7 +87,7 @@ class Orchestration(unittest.TestCase):
if "--max-budget-usd" in args:
(cwd / "bin/pkg/mod.py").write_text("x = 3\n")
# Against the bench rules, the worker commits a part of its change.
real_run(["git", "-C", str(cwd), "add", "bin/pkg/mod.py"], check=True)
subprocess.run(["git", "-C", str(cwd), "add", "bin/pkg/mod.py"], check=True)
commit(cwd, "against the rules")
(cwd / "bin/pkg/new.py").write_text("n = 1\n")
self.transcript.parent.mkdir(parents=True)
@@ -113,14 +108,14 @@ class Orchestration(unittest.TestCase):
if args[0] == str(advisor_bench.RUN_HEAVY):
self.heavy.append(args)
return subprocess.CompletedProcess(args, 0, stdout="3 passed\n", stderr="")
return real_run(args, **kwargs)
raise AssertionError(f"the bench ran {args}")
def started(self):
handoff_bench.start(self.work, "7-1")
handoff_bench.start(self.handoff, self.work, "7-1")
self.calls.clear()
def test_start_runs_the_weak_worker_to_its_budget_and_saves_the_tree_and_the_session(self):
self.assertEqual(handoff_bench.start(self.work, "7-1"), "7-1: started")
self.assertEqual(handoff_bench.start(self.handoff, self.work, "7-1"), "7-1: started")
(claude, kwargs, _), = self.calls
self.assertEqual(kwargs["cwd"], self.work / "7-1")
self.assertEqual((kwargs["stdin"], kwargs["stderr"]), (subprocess.DEVNULL, subprocess.STDOUT))
@@ -142,21 +137,21 @@ class Orchestration(unittest.TestCase):
def test_start_refuses_a_worker_that_finished_within_its_budget(self):
self.start_result = {}
with self.assertRaisesRegex(ValueError, "^the weak worker finished within its budget, or its run failed, so there is no handoff to bench$"):
handoff_bench.start(self.work, "7-1")
handoff_bench.start(self.handoff, self.work, "7-1")
def test_start_refuses_a_name_that_already_started(self):
(self.work / "7-1").mkdir()
with self.assertRaisesRegex(ValueError, "already started"):
handoff_bench.start(self.work, "7-1")
handoff_bench.start(self.handoff, self.work, "7-1")
def test_start_refuses_a_session_whose_transcript_it_cannot_find(self):
self.transcript = self.work / "elsewhere" / f"{SESSION}.jsonl"
with self.assertRaisesRegex(ValueError, "found 0"):
handoff_bench.start(self.work, "7-1")
handoff_bench.start(self.handoff, self.work, "7-1")
def test_the_resume_arm_resumes_the_weak_workers_session_on_the_strong_rung(self):
self.started()
self.assertEqual(handoff_bench.run(self.work, "7-resume-1"), "7-resume-1: ran")
self.assertEqual(handoff_bench.run(self.handoff, self.work, "7-resume-1"), "7-resume-1: ran")
(claude, kwargs, _), = self.calls
self.assertEqual(kwargs["cwd"], self.work / "7-1")
self.assertEqual(claude[:2] + claude[3:], [
@@ -174,7 +169,7 @@ class Orchestration(unittest.TestCase):
def test_the_compact_arm_has_the_weak_model_compact_the_session_before_the_strong_one_resumes_it(self):
self.started()
handoff_bench.run(self.work, "7-compact-1")
handoff_bench.run(self.handoff, self.work, "7-compact-1")
(compact, _, _), (claude, _, found) = self.calls
self.assertEqual(compact[:2] + compact[3:], [
"claude", "-p", *handoff_bench.WEAK.flags(), "--resume", str(SESSION), "--setting-sources", "project",
@@ -187,7 +182,7 @@ class Orchestration(unittest.TestCase):
def test_the_brief_arm_starts_a_fresh_session_with_the_item_and_the_brief(self):
self.started()
self.assertEqual(handoff_bench.run(self.work, "7-brief-1"), "7-brief-1: ran")
self.assertEqual(handoff_bench.run(self.handoff, self.work, "7-brief-1"), "7-brief-1: ran")
(claude, kwargs, _), = self.calls
self.assertEqual(kwargs["cwd"], self.work / "7-1")
self.assertEqual(claude[:2] + claude[3:], [
@@ -200,36 +195,36 @@ class Orchestration(unittest.TestCase):
def test_each_arm_begins_on_the_tree_and_the_session_that_start_left(self):
self.started()
for arm in ("resume", "compact", "brief"):
handoff_bench.run(self.work, f"7-{arm}-1")
handoff_bench.run(self.handoff, self.work, f"7-{arm}-1")
self.assertEqual([found for _, _, found in self.calls if found[0] != "weak\ncompacted\n"], [("weak\n", ["mod.py", "new.py"])] * 3)
self.assertNotIn("done_1.py", (self.work / "7-brief-1.diff").read_text())
def test_run_refuses_an_arm_that_already_ran_or_never_started(self):
with self.assertRaisesRegex(ValueError, "start 7-1 first"):
handoff_bench.run(self.work, "7-brief-1")
handoff_bench.run(self.handoff, self.work, "7-brief-1")
self.started()
handoff_bench.run(self.work, "7-brief-1")
handoff_bench.run(self.handoff, self.work, "7-brief-1")
with self.assertRaisesRegex(ValueError, "already ran"):
handoff_bench.run(self.work, "7-brief-1")
handoff_bench.run(self.handoff, self.work, "7-brief-1")
def test_run_refuses_an_unknown_arm(self):
self.started()
with self.assertRaisesRegex(ValueError, "arm"):
handoff_bench.run(self.work, "7-sonnet-1")
handoff_bench.run(self.handoff, self.work, "7-sonnet-1")
def test_run_refuses_an_arm_whose_run_ended_in_an_error_before_it_tests(self):
self.started()
self.strong_result = {"is_error": True, "result": "overloaded"}
with unittest.mock.patch.object(advisor_bench, "save_change") as save_change:
with self.assertRaisesRegex(ValueError, "ended in an error: overloaded"):
handoff_bench.run(self.work, "7-brief-1")
save_change.assert_not_called()
with self.assertRaisesRegex(ValueError, "ended in an error: overloaded"):
handoff_bench.run(self.handoff, self.work, "7-brief-1")
self.assertEqual(self.heavy, [])
self.assertFalse((self.work / "7-brief-1.diff").exists())
def test_run_refuses_a_compact_arm_whose_compaction_failed_before_the_strong_model_runs(self):
self.started()
self.compact_result = {"is_error": True, "result": "no conversation to compact"}
with self.assertRaisesRegex(ValueError, "^the run ended in an error: no conversation to compact$"):
handoff_bench.run(self.work, "7-compact-1")
handoff_bench.run(self.handoff, self.work, "7-compact-1")
self.assertEqual([args[2] for args, _, _ in self.calls], ["/compact"])
self.assertFalse((self.work / "7-compact-1.jsonl").exists())
@@ -240,18 +235,20 @@ class Orchestration(unittest.TestCase):
handoff_bench.outcome(self.work, "7-brief-1")
def test_main_grades_with_the_handoff_cases_and_prints_the_summary(self):
with unittest.mock.patch("builtins.print") as printed:
handoff_bench.main([str(self.work), "start", "7-1"])
handoff_bench.main([str(self.work), "run", "7-brief-1"])
self.assertEqual([call.args[0] for call in printed.call_args_list], ["7-1: started", "7-brief-1: ran"])
with unittest.mock.patch.object(advisor_bench, "grade", return_value="7-brief-1: pass") as grade, unittest.mock.patch("builtins.print") as printed:
handoff_bench.main([str(self.work), "grade", "7-brief-1"])
grade.assert_called_once_with(self.work, "7-brief-1", handoff_bench.CASES)
printed.assert_called_once_with("7-brief-1: pass", flush=True)
steps, printed = handoff_bench.steps_of(self.handoff), []
handoff_bench.main([str(self.work), "start", "7-1"], steps, printed.append)
handoff_bench.main([str(self.work), "run", "7-brief-1"], steps, printed.append)
self.assertEqual(printed, ["7-1: started", "7-brief-1: ran"])
printed.clear()
handoff_bench.main([str(self.work), "grade", "7-brief-1"], steps, printed.append)
self.assertEqual(printed, ["7-brief-1: pass"])
# The judge saw the handoff case's merged change, so the grade ran over the handoff's cases.
(prompt,) = self.judged
self.assertIn("-x = 1\n+x = 2\n", prompt)
(self.work / "7-brief-1.verdict").write_text(json.dumps({"verdict": "pass"}))
with unittest.mock.patch("builtins.print") as printed:
handoff_bench.main([str(self.work), "summary"])
self.assertIn("| brief | 1 | 1 | 0 | 0.50 | 12 |", printed.call_args.args[0])
printed.clear()
handoff_bench.main([str(self.work), "summary"], steps, printed.append)
self.assertIn("| brief | 1 | 1 | 0 | 0.50 | 12 |", printed[-1])
if __name__ == "__main__":