-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_all.py
More file actions
305 lines (250 loc) · 11.3 KB
/
Copy pathrun_all.py
File metadata and controls
305 lines (250 loc) · 11.3 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
#!/usr/bin/env python3
"""Run arena tests across configured models.
Uses subprocess calls to `inspect eval` for parallelism — each run is
an independent process, avoiding Inspect's single-eval_async limitation.
After each run, validates the result: if the agent made 0 tool calls
and finished in under 30 seconds, or if the session contains provider
errors, the run is considered invalid and automatically retried.
"""
import argparse
import json
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from inspect_ai.log import read_eval_log
ALL_TASKS = {
"staffing_analysis": "src/adu_arena/tasks/staffing_analysis.py@staffing_analysis",
"culture_spending_analysis": "src/adu_arena/tasks/culture_spending.py@culture_spending_analysis",
"gov_contracts_scraper": "src/adu_arena/tasks/gov_contracts.py@gov_contracts_scraper",
"csv_deduplicator": "src/adu_arena/tasks/csv_deduplicator.py@csv_deduplicator",
"boundary_crosswalk_funding": "src/adu_arena/tasks/boundary_crosswalk.py@boundary_crosswalk_funding",
"climate_price_impact": "src/adu_arena/tasks/climate_price.py@climate_price_impact",
}
MODELS_FILE = Path("models.json")
DEFAULT_PARALLEL = 5
MAX_RETRIES = 3
LOGS_DIR = Path("logs")
SESSIONS_DIR = Path("sessions")
def load_models_config(path: Path) -> dict:
return json.loads(path.read_text())
def load_models(path: Path) -> list[str]:
data = load_models_config(path)
return [m["id"] for m in data["models"]]
def get_completed_counts(log_dir: Path) -> dict[tuple[str, str], int]:
"""Return run counts per (model, task_name) pair."""
counts: dict[tuple[str, str], int] = {}
if not log_dir.exists():
return counts
for log_file in log_dir.glob("*.eval"):
try:
log = read_eval_log(str(log_file), header_only=True)
if log.status == "success":
task_name = log.eval.task.split("/")[-1] if "/" in log.eval.task else log.eval.task
key = (log.eval.model, task_name)
counts[key] = counts.get(key, 0) + 1
except Exception:
continue
return counts
def validate_run(log_dir: Path, model: str, task_name: str) -> str | None:
"""Check the latest run for infrastructure failures.
Returns None if valid, or an error description if invalid.
"""
# Find the latest eval log for this model+task
candidates = []
for f in log_dir.glob("*.eval"):
try:
log = read_eval_log(str(f), header_only=True)
t = log.eval.task.split("/")[-1] if "/" in log.eval.task else log.eval.task
if log.eval.model == model and t == task_name:
candidates.append(f)
except Exception:
continue
if not candidates:
return "no log file found"
latest = sorted(candidates)[-1]
log = read_eval_log(str(latest))
if log.status != "success":
return f"run status: {log.status}"
# Check timing — runs under 30s with 0% score are suspicious
duration = None
if log.stats and log.stats.started_at and log.stats.completed_at:
from datetime import datetime
start = datetime.fromisoformat(log.stats.started_at)
end = datetime.fromisoformat(log.stats.completed_at)
duration = (end - start).total_seconds()
# Check for file_exists = 0 (agent produced nothing)
file_exists = None
for sample in log.samples or []:
for scorer_name, score in (sample.scores or {}).items():
if isinstance(score.value, dict) and "file_exists" in score.value:
file_exists = score.value["file_exists"]
if file_exists == 0.0 and duration is not None and duration < 30:
# Check session for provider errors
session_error = _check_session_for_errors(model, task_name)
if session_error:
# Delete the invalid log
latest.unlink()
return f"infrastructure failure ({session_error})"
return None
def _check_session_for_errors(model: str, task_name: str) -> str | None:
"""Check the session JSONL for provider/infrastructure errors."""
from adu_arena.scorers.judge import DIMENSIONS # just to get archetype mapping
archetype_map = {
"staffing_analysis": "pipeline-stage",
"culture_spending_analysis": "notebook-analysis",
"gov_contracts_scraper": "scrape-and-structure",
"csv_deduplicator": "full-project-reproduction",
}
if "/" in model:
provider, model_id = model.split("/", 1)
else:
provider, model_id = model, model
archetype = archetype_map.get(task_name, "unknown")
safe_model_id = model_id.replace("/", "_")
pattern = f"{provider}_{safe_model_id}_{archetype}_*.jsonl"
matches = sorted(SESSIONS_DIR.glob(pattern))
if not matches:
return None
session_file = matches[-1]
try:
for line in open(session_file):
event = json.loads(line)
if event.get("type") == "message_end":
msg = event.get("message", {})
if msg.get("stopReason") == "error":
error_msg = msg.get("errorMessage", "")
if any(phrase in error_msg.lower() for phrase in [
"provider returned error",
"no endpoints found",
"operation was aborted",
"tool use",
"rate limit",
"timeout",
]):
return error_msg[:100]
except Exception:
pass
return None
def run_single(model: str, task_spec: str, log_dir: str) -> tuple[str, str, bool, str]:
"""Run one model+test via subprocess."""
task_name = task_spec.split("@")[-1]
try:
result = subprocess.run(
["uv", "run", "inspect", "eval", task_spec, "--model", model, "--log-dir", log_dir],
capture_output=True,
text=True,
timeout=1800, # 30 minutes
)
if result.returncode == 0:
return (model, task_name, True, "success")
else:
err = result.stderr.strip().split("\n")[-1] if result.stderr else "unknown error"
return (model, task_name, False, err[:200])
except subprocess.TimeoutExpired:
return (model, task_name, False, "timed out after 30 minutes")
def main() -> None:
parser = argparse.ArgumentParser(description="Run arena tests across models")
parser.add_argument("--model", nargs="+", default=None, help="Model(s) to run (default: all from models.json)")
parser.add_argument("--test", nargs="+", default=None, help="Test(s) to run (default: all)")
parser.add_argument("--parallel", type=int, default=DEFAULT_PARALLEL, help=f"Max parallel runs (default: {DEFAULT_PARALLEL})")
parser.add_argument("--min-runs", type=int, default=None, help="Minimum runs per model+test pair (overrides models.json)")
parser.add_argument("--rerun", action="store_true", help="Run all tests again (accumulates results for averaging)")
parser.add_argument("--clear", action="store_true", help="Delete all existing results before running")
parser.add_argument("--list", action="store_true", help="Show what would run without running")
args = parser.parse_args()
# Resolve models and min_runs
models_config = load_models_config(MODELS_FILE) if MODELS_FILE.exists() else None
if args.model:
models = args.model
elif models_config:
models = [m["id"] for m in models_config["models"]]
print(f"Loaded {len(models)} models from {MODELS_FILE}")
else:
print(f"No --model specified and {MODELS_FILE} not found", file=sys.stderr)
sys.exit(1)
min_runs = args.min_runs or (models_config or {}).get("min_runs", 1)
# Resolve tests
if args.test:
tasks = {k: v for k, v in ALL_TASKS.items() if k in args.test}
if not tasks:
print(f"No matching tests. Available: {list(ALL_TASKS.keys())}", file=sys.stderr)
sys.exit(1)
else:
tasks = ALL_TASKS
# Handle --clear: delete all existing results
if args.clear:
LOGS_DIR.mkdir(parents=True, exist_ok=True)
for f in LOGS_DIR.glob("*.eval"):
f.unlink()
for d in LOGS_DIR.iterdir():
if d.is_dir():
import shutil
shutil.rmtree(d)
print("Cleared all existing results")
# --rerun: run everything once more (results accumulate)
# default: top up each pair to min_runs
if args.rerun or args.clear:
counts: dict[tuple[str, str], int] = {}
else:
counts = get_completed_counts(LOGS_DIR)
# Build run plan
plan: list[tuple[str, str, str]] = []
skipped = 0
for model in models:
for task_name, task_spec in tasks.items():
existing = counts.get((model, task_name), 0)
needed = max(0, min_runs - existing) if not args.rerun else 1
if needed == 0:
skipped += 1
else:
for _ in range(needed):
plan.append((model, task_name, task_spec))
print(f"\n{len(plan)} runs to do, {skipped} pairs already at {min_runs}+ runs")
if args.list:
for model, task_name, _ in plan:
print(f" {model} x {task_name}")
return
if not plan:
print("Nothing to run. Use --rerun to add more, or increase --min-runs.")
return
LOGS_DIR.mkdir(parents=True, exist_ok=True)
# Run with retries for infrastructure failures
for attempt in range(1, MAX_RETRIES + 1):
if not plan:
break
label = f"(attempt {attempt}/{MAX_RETRIES})" if attempt > 1 else ""
print(f"\nRunning {len(plan)} tests with --parallel {args.parallel} {label}\n")
succeeded = 0
failed = 0
total = len(plan)
retry_plan: list[tuple[str, str, str]] = []
with ThreadPoolExecutor(max_workers=args.parallel) as executor:
futures = {
executor.submit(run_single, model, task_spec, str(LOGS_DIR)): (model, task_name, task_spec)
for model, task_name, task_spec in plan
}
for future in as_completed(futures):
model, task_name, success, message = future.result()
_, _, task_spec = futures[future]
if success:
# Validate the run for infrastructure failures
invalid_reason = validate_run(LOGS_DIR, model, task_name)
if invalid_reason:
print(f" [{succeeded + failed + 1}/{total}] INVALID {model} x {task_name}: {invalid_reason}")
retry_plan.append((model, task_name, task_spec))
failed += 1
else:
succeeded += 1
print(f" [{succeeded + failed}/{total}] OK {model} x {task_name}")
else:
failed += 1
print(f" [{succeeded + failed}/{total}] FAIL {model} x {task_name}: {message}")
print(f"\nAttempt {attempt}: {succeeded} succeeded, {failed} failed")
if not retry_plan:
break
plan = retry_plan
print(f"Retrying {len(plan)} infrastructure failures...")
print(f"\nDone.")
if __name__ == "__main__":
main()