-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_control.py
More file actions
345 lines (286 loc) · 11.1 KB
/
Copy pathrun_control.py
File metadata and controls
345 lines (286 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
"""Run control arms for needle-bench benchmarks.
Two controls:
1. solution-applied: Apply .bench/solution.patch, run test.sh -> should PASS
2. no-agent: Run test.sh with zero changes -> should FAIL
These prove the benchmarks are valid: the bug is real and the fix works.
Usage:
python3 run_control.py # run all benchmarks
python3 run_control.py --benchmark off-by-one-pagination # one benchmark
python3 run_control.py --dry-run # show what would run
"""
import argparse
import json
import os
import subprocess
import sys
import time
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BENCHMARKS_DIR = os.path.join(BASE_DIR, "benchmarks")
RUNS_DIR = os.path.join(BASE_DIR, "runs", "control")
# Benchmarks to skip (not real code-solve benchmarks)
SKIP = {"_template", "haystack-boot", "haystack-mint"}
def list_benchmarks():
"""Return sorted list of benchmark names, excluding skipped ones."""
names = []
for name in sorted(os.listdir(BENCHMARKS_DIR)):
if name in SKIP or name.startswith("_"):
continue
bench_dir = os.path.join(BENCHMARKS_DIR, name)
if os.path.isdir(bench_dir):
names.append(name)
return names
def docker_build(bench_name, bench_dir):
"""Build the Docker image for a benchmark. Returns True on success."""
tag = f"nb-ctrl-{bench_name}"
result = subprocess.run(
["docker", "build", "-t", tag, bench_dir],
capture_output=True, text=True, timeout=300,
)
if result.returncode != 0:
print(f" BUILD FAILED: {result.stderr[-500:]}", file=sys.stderr)
return False
return True
def docker_run_container(bench_name):
"""Start a fresh container. Returns container name."""
tag = f"nb-ctrl-{bench_name}"
ts = str(int(time.time()))
container = f"nb-ctrl-{bench_name}-{ts}"
subprocess.run(
["docker", "run", "-d", "--name", container, tag, "sleep", "3600"],
capture_output=True, text=True, check=True, timeout=30,
)
return container
def docker_exec(container, cmd, timeout=120):
"""Execute a command inside the container. Returns (returncode, stdout, stderr)."""
result = subprocess.run(
["docker", "exec", container, "bash", "-c", cmd],
capture_output=True, text=True, timeout=timeout,
)
stdout = result.stdout[-4000:] if len(result.stdout) > 4000 else result.stdout
stderr = result.stderr[-2000:] if len(result.stderr) > 2000 else result.stderr
return result.returncode, stdout, stderr
def docker_rm(container):
"""Remove a container."""
subprocess.run(
["docker", "rm", "-f", container],
capture_output=True, text=True,
)
def detect_workdir(bench_dir):
"""Detect the final WORKDIR from a Dockerfile.
Reads the Dockerfile and returns the last WORKDIR directive.
Falls back to /workspace if none found.
"""
dockerfile = os.path.join(bench_dir, "Dockerfile")
workdir = "/workspace"
try:
with open(dockerfile) as f:
for line in f:
stripped = line.strip()
if stripped.upper().startswith("WORKDIR"):
parts = stripped.split(None, 1)
if len(parts) == 2:
workdir = parts[1]
except FileNotFoundError:
pass
return workdir
def run_test(container, workdir):
"""Run test.sh in the container's workdir. Returns (passed: bool, wall_clock: float)."""
t0 = time.time()
rc, stdout, stderr = docker_exec(container, f"cd {workdir} && bash test.sh")
elapsed = time.time() - t0
return rc == 0, elapsed
def write_score(bench_name, agent, resolved, wall_clock, control_type):
"""Write a control score JSON file."""
os.makedirs(RUNS_DIR, exist_ok=True)
score = {
"benchmark": bench_name,
"agent": agent,
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"resolved": resolved,
"turns_to_fix": 0,
"token_cost": 0,
"wall_clock": round(wall_clock, 1),
"control_type": control_type,
}
# Use agent-prefixed filename to avoid collisions between the two controls
score_path = os.path.join(RUNS_DIR, f"{bench_name}.{agent}.score.json")
with open(score_path, "w") as f:
json.dump(score, f, indent=2)
f.write("\n")
return score
def run_no_agent(bench_name, bench_dir):
"""No-agent control: run test.sh with zero modifications.
Expected: test FAILS (resolved=False). If it passes, the benchmark is broken.
"""
workdir = detect_workdir(bench_dir)
container = docker_run_container(bench_name)
try:
passed, elapsed = run_test(container, workdir)
score = write_score(
bench_name,
agent="control-baseline",
resolved=passed,
wall_clock=elapsed,
control_type="no-agent",
)
return score
finally:
docker_rm(container)
def run_solution_applied(bench_name, bench_dir):
"""Solution-applied control: apply .bench/solution.patch, then run test.sh.
Expected: test PASSES (resolved=True). If it fails, the solution patch is broken.
"""
patch_path = os.path.join(bench_dir, ".bench", "solution.patch")
if not os.path.exists(patch_path):
print(f" SKIP solution-applied: no .bench/solution.patch", file=sys.stderr)
return None
workdir = detect_workdir(bench_dir)
container = docker_run_container(bench_name)
try:
# Copy the solution patch into the container
subprocess.run(
["docker", "cp", patch_path, f"{container}:/tmp/solution.patch"],
capture_output=True, text=True, check=True, timeout=30,
)
# Apply the patch
rc, stdout, stderr = docker_exec(
container, f"cd {workdir} && git apply /tmp/solution.patch"
)
if rc != 0:
# Some benchmarks may not have git initialized; try plain patch
rc2, stdout2, stderr2 = docker_exec(
container, f"cd {workdir} && patch -p1 < /tmp/solution.patch"
)
print(f"DEBUG: git apply rc={rc} stderr={stderr.strip()} stdout={stdout.strip()}")
print(f"DEBUG: patch rc={rc2} stderr={stderr2.strip()} stdout={stdout2.strip()}")
if rc2 != 0:
print(f" PATCH FAILED: git apply: {stderr.strip()}", file=sys.stderr)
print(f" PATCH FAILED: patch -p1: {stderr2.strip()}", file=sys.stderr)
return write_score(
bench_name,
agent="control-solution",
resolved=False,
wall_clock=0.0,
control_type="solution-applied",
)
# Run the test
passed, elapsed = run_test(container, workdir)
score = write_score(
bench_name,
agent="control-solution",
resolved=passed,
wall_clock=elapsed,
control_type="solution-applied",
)
return score
finally:
docker_rm(container)
def run_control(bench_name, dry_run=False):
"""Run both control arms for a single benchmark."""
bench_dir = os.path.join(BENCHMARKS_DIR, bench_name)
if not os.path.isdir(bench_dir):
print(f"ERROR: benchmark not found: {bench_name}", file=sys.stderr)
return None, None
if dry_run:
patch = os.path.join(bench_dir, ".bench", "solution.patch")
has_patch = os.path.exists(patch)
print(f" [dry-run] Would build image nb-ctrl-{bench_name}")
print(f" [dry-run] Would run no-agent control (expect FAIL)")
print(f" [dry-run] Would run solution-applied control (expect PASS) — patch exists: {has_patch}")
return None, None
print(f"\n{'='*60}")
print(f" {bench_name}")
print(f"{'='*60}")
# Build Docker image
print(f" Building image...", end=" ", flush=True)
if not docker_build(bench_name, bench_dir):
return None, None
print("done")
# No-agent control
print(f" Running no-agent control...", end=" ", flush=True)
baseline = run_no_agent(bench_name, bench_dir)
if baseline:
status = "PASS (UNEXPECTED)" if baseline["resolved"] else "FAIL (expected)"
print(status)
# Solution-applied control
print(f" Running solution-applied control...", end=" ", flush=True)
solution = run_solution_applied(bench_name, bench_dir)
if solution:
status = "PASS (expected)" if solution["resolved"] else "FAIL (UNEXPECTED)"
print(status)
elif solution is None:
print("SKIPPED (no patch)")
return baseline, solution
def print_summary(results):
"""Print a summary table of all control results."""
print(f"\n{'='*72}")
print(f" CONTROL SUMMARY")
print(f"{'='*72}")
print(f"{'Benchmark':<40} {'Baseline':>10} {'Solution':>10}")
print(f"{'-'*40} {'-'*10} {'-'*10}")
invalid_count = 0
valid_count = 0
skipped_count = 0
for bench_name, (baseline, solution) in sorted(results.items()):
if baseline is None and solution is None:
skipped_count += 1
print(f"{bench_name:<40} {'SKIP':>10} {'SKIP':>10}")
continue
# Baseline: should FAIL (resolved=False is correct)
if baseline:
baseline_ok = not baseline["resolved"]
b_str = f"FAIL \u2713" if baseline_ok else f"PASS \u2717"
else:
b_str = "N/A"
baseline_ok = False
# Solution: should PASS (resolved=True is correct)
if solution:
solution_ok = solution["resolved"]
s_str = f"PASS \u2713" if solution_ok else f"FAIL \u2717"
else:
s_str = "N/A"
solution_ok = False
is_valid = baseline_ok and solution_ok
suffix = ""
if not is_valid:
suffix = " <- INVALID"
invalid_count += 1
else:
valid_count += 1
print(f"{bench_name:<40} {b_str:>10} {s_str:>10}{suffix}")
print(f"\n Valid: {valid_count} Invalid: {invalid_count} Skipped: {skipped_count}")
print(f" Total: {valid_count + invalid_count + skipped_count}")
def main():
parser = argparse.ArgumentParser(
description="Run control arms for needle-bench benchmarks."
)
parser.add_argument(
"--benchmark",
help="Run a single benchmark by name",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would run without executing",
)
args = parser.parse_args()
if args.benchmark:
benchmarks = [args.benchmark]
else:
benchmarks = list_benchmarks()
if args.dry_run:
print(f"[dry-run] Would run {len(benchmarks)} benchmarks:\n")
for bench in benchmarks:
print(f" {bench}")
run_control(bench, dry_run=True)
return
print(f"Running control arms for {len(benchmarks)} benchmarks")
print(f"Results will be written to: {RUNS_DIR}")
results = {}
for bench in benchmarks:
baseline, solution = run_control(bench)
results[bench] = (baseline, solution)
print_summary(results)
if __name__ == "__main__":
main()