-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_experiment.py
More file actions
581 lines (546 loc) · 20.3 KB
/
Copy pathrun_experiment.py
File metadata and controls
581 lines (546 loc) · 20.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/usr/bin/env python3
"""
Graph-Augmented Manager — MVP Experiment Runner
Compares graph-based file retrieval vs RAG baselines on Flask issues from SWE-bench.
Usage:
1. Copy .env.example to .env and add your Gemini API key
2. pip install -r requirements.txt
3. python run_experiment.py [--n-issues 10]
"""
import argparse
import json
import os
import random
import statistics
import sys
import time
from pathlib import Path
from dotenv import load_dotenv
from src.deterministic_config import load_deterministic_config
from src.run_ids import build_suite_id
ALL_METHODS = [
"gm_deterministic",
"gm_progressive",
"gm_baseline",
"rag_progressive",
"rag_baseline",
"raw_rag_function",
"raw_rag_fixed",
"bm25",
"repomap_like",
"agentless_like_localization",
]
def _bootstrap_ci_95_mean(values: list[float], n_bootstrap: int = 2000, seed: int = 17) -> tuple[float, float]:
"""Compute deterministic bootstrap 95% CI for the sample mean."""
if not values:
return (0.0, 0.0)
if len(values) == 1:
return (values[0], values[0])
rng = random.Random(seed)
sample_means = []
n = len(values)
for _ in range(n_bootstrap):
draw = [values[rng.randrange(0, n)] for _ in range(n)]
sample_means.append(statistics.fmean(draw))
sample_means.sort()
lo_idx = int(0.025 * (len(sample_means) - 1))
hi_idx = int(0.975 * (len(sample_means) - 1))
return (sample_means[lo_idx], sample_means[hi_idx])
def _compute_pairwise_deltas_with_ci(
summaries: list[dict],
metric_key: str = "mean_f1",
) -> dict:
pairwise = {}
for left_method in ALL_METHODS:
for right_method in ALL_METHODS:
if left_method == right_method:
continue
deltas = []
for summary in summaries:
left = summary.get(left_method, {}).get(metric_key)
right = summary.get(right_method, {}).get(metric_key)
if left is None or right is None:
continue
deltas.append(float(left) - float(right))
if not deltas:
continue
ci_low, ci_high = _bootstrap_ci_95_mean(deltas)
key = f"{left_method}__minus__{right_method}__{metric_key}"
pairwise[key] = {
"n_runs": len(deltas),
"mean_delta": statistics.fmean(deltas),
"bootstrap_ci_95": [ci_low, ci_high],
}
return pairwise
def _aggregate_amortization(summaries: list[dict]) -> dict:
"""Aggregate amortization metadata across repeated runs."""
blocks = [
summary.get("_amortization", {})
for summary in summaries
if isinstance(summary, dict) and isinstance(summary.get("_amortization"), dict)
]
if not blocks:
return {}
def _float_values(key: str) -> list[float]:
values = []
for block in blocks:
if block.get(key) is None:
continue
values.append(float(block.get(key)))
return values
track_names = sorted(
{
str(block.get("track_name"))
for block in blocks
if block.get("track_name")
}
)
n_issues_values = [int(block.get("n_issues", 0) or 0) for block in blocks]
n_unique_values = [int(block.get("n_unique_commits", 0) or 0) for block in blocks]
repeat_values = _float_values("commit_repeat_ratio")
cache_values = _float_values("cache_hit_rate")
return {
"track_name": track_names[0] if len(track_names) == 1 else "mixed",
"n_issues": max(n_issues_values) if n_issues_values else 0,
"n_unique_commits": round(statistics.fmean(n_unique_values)) if n_unique_values else 0,
"commit_repeat_ratio": statistics.fmean(repeat_values) if repeat_values else 0.0,
"cache_hit_rate": statistics.fmean(cache_values) if cache_values else 0.0,
}
def aggregate_repeat_summaries(summaries: list[dict]) -> dict:
"""Aggregate multiple run summaries into mean/std statistics."""
metric_keys = [
"mean_precision",
"mean_recall",
"mean_f1",
"n_errors",
"error_rate",
"total_llm_tokens",
"total_query_embedding_tokens",
"avg_llm_tokens_per_issue",
"avg_query_embedding_tokens_per_issue",
"setup_embedding_tokens",
"total_cost_tokens",
]
aggregated = {}
for method in ALL_METHODS:
method_metrics = {}
for key in metric_keys:
values = [
float(summary.get(method, {}).get(key, 0.0))
for summary in summaries
if method in summary
]
if not values:
continue
method_metrics[key] = {
"mean": statistics.fmean(values),
"std": statistics.stdev(values) if len(values) > 1 else 0.0,
"min": min(values),
"max": max(values),
}
aggregated[method] = method_metrics
run_ids = [
summary.get("_meta", {}).get("run_id", "")
for summary in summaries
if isinstance(summary, dict)
]
pairwise_deltas = _compute_pairwise_deltas_with_ci(
summaries=summaries,
metric_key="mean_f1",
)
amortization = _aggregate_amortization(summaries)
payload = {
"n_runs": len(summaries),
"run_ids": [rid for rid in run_ids if rid],
"methods": aggregated,
"pairwise_deltas": pairwise_deltas,
"gates": {
"min_repeats_met": len(summaries) >= 3,
"pairwise_bootstrap_available": bool(pairwise_deltas),
"ci_ready": len(summaries) >= 3 and bool(pairwise_deltas),
},
}
if amortization:
payload["_amortization"] = amortization
return payload
def main():
load_dotenv()
parser = argparse.ArgumentParser(
description="Run the Graph-Manager vs RAG comparison experiment"
)
parser.add_argument(
"--n-issues",
type=int,
default=10,
help="Number of issues to evaluate (default: 10)",
)
parser.add_argument(
"--results-dir",
type=str,
default="results",
help="Directory to save results (default: results/)",
)
parser.add_argument(
"--flat-results",
action="store_true",
help="Write outputs directly into --results-dir instead of results/runs/<timestamp>/",
)
parser.add_argument(
"--source-prefix",
action="append",
default=None,
help="Restrict indexed files to these repo-relative prefixes (repeatable). Default: index all .py files",
)
parser.add_argument(
"--max-turns",
type=int,
default=6,
help="Default max tool-calling turns for both agents (default: 6)",
)
parser.add_argument(
"--manager-max-turns",
type=int,
default=None,
help="Override max tool-calling turns for Graph-Manager (default: --max-turns)",
)
parser.add_argument(
"--rag-max-turns",
type=int,
default=None,
help="Override max tool-calling turns for RAG-Agent (default: --max-turns)",
)
parser.add_argument(
"--repeats",
type=int,
default=1,
help="Number of repeated runs with identical settings (default: 1)",
)
parser.add_argument(
"--task-family",
type=str,
default="swe-bench",
help="High-level task family label in metadata (default: swe-bench)",
)
parser.add_argument(
"--dataset-name",
type=str,
default="SWE-bench/SWE-bench",
help="Dataset label in metadata (default: SWE-bench/SWE-bench)",
)
parser.add_argument(
"--repo-name",
type=str,
default="pallets/flask",
help="Repository label in metadata (default: pallets/flask)",
)
parser.add_argument(
"--issue-set-id",
type=str,
default=None,
help="Optional issue set identifier. If omitted, a stable ID is inferred from selected instances.",
)
parser.add_argument(
"--suite-id",
type=str,
default=None,
help="Optional suite identifier for grouping runs. If omitted, a stable ID is inferred.",
)
parser.add_argument(
"--notes",
type=str,
default="",
help="Optional free-form notes stored in run metadata",
)
parser.add_argument(
"--no-redact-issue-paths",
action="store_true",
help="Disable path redaction in issue text before retrieval (default: redaction enabled)",
)
parser.add_argument(
"--evaluation-track",
type=str,
default="strict_commit_fidelity",
choices=["strict_commit_fidelity", "same_snapshot_amortized"],
help="Evaluation track (default: strict_commit_fidelity)",
)
parser.add_argument(
"--snapshot-commit",
type=str,
default=None,
help="Optional fixed snapshot commit for same_snapshot_amortized track.",
)
parser.add_argument(
"--instance-id",
action="append",
default=None,
help="Optional manifest-pinned issue instance ID (repeatable).",
)
parser.add_argument(
"--domain",
type=str,
default="",
help="Optional domain label for matrix reporting (e.g., web_framework, library).",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="Optional reproducibility seed recorded in metadata.",
)
parser.add_argument(
"--deterministic-seed-k",
type=int,
default=8,
help="Deterministic retriever seed node count (default: 8).",
)
parser.add_argument(
"--deterministic-depth",
type=int,
default=2,
help="Deterministic retriever BFS depth cap (default: 2).",
)
parser.add_argument(
"--deterministic-neighbor-cap",
type=int,
default=12,
help="Deterministic retriever per-node neighbor cap (default: 12).",
)
parser.add_argument(
"--deterministic-min-return-files",
type=int,
default=1,
help="Minimum files returned by deterministic retriever (default: 1).",
)
parser.add_argument(
"--deterministic-score-ratio-cutoff",
type=float,
default=0.70,
help="Keep files with score >= cutoff * top score (default: 0.70).",
)
parser.add_argument(
"--deterministic-min-score-cutoff",
type=float,
default=0.0,
help="Absolute minimum score cutoff for deterministic retrieval (default: 0.0).",
)
parser.add_argument(
"--deterministic-hub-degree-threshold",
type=int,
default=20,
help="Degree threshold where hub penalty starts applying (default: 20).",
)
parser.add_argument(
"--deterministic-hub-penalty-scale",
type=float,
default=0.35,
help="Scale for hub-file penalty contribution (default: 0.35).",
)
parser.add_argument(
"--deterministic-w-sem",
type=float,
default=0.35,
help="Deterministic retriever semantic evidence weight (default: 0.35).",
)
parser.add_argument(
"--deterministic-w-graph",
type=float,
default=0.30,
help="Deterministic retriever graph evidence weight (default: 0.30).",
)
parser.add_argument(
"--deterministic-w-conf",
type=float,
default=0.20,
help="Deterministic retriever confidence evidence weight (default: 0.20).",
)
parser.add_argument(
"--deterministic-w-hint",
type=float,
default=0.10,
help="Deterministic retriever path hint weight (default: 0.10).",
)
parser.add_argument(
"--deterministic-w-pen",
type=float,
default=0.05,
help="Deterministic retriever low-confidence penalty weight (default: 0.05).",
)
parser.add_argument(
"--deterministic-config-path",
type=str,
default=None,
help=(
"Optional JSON/YAML file with deterministic retrieval tuning parameters. "
"Values in this file override CLI deterministic_* flags."
),
)
parser.add_argument(
"--methods",
type=str,
default=None,
help=(
"Optional comma-separated subset of methods to execute. "
f"Allowed: {', '.join(ALL_METHODS)}"
),
)
args = parser.parse_args()
if args.deterministic_config_path:
deterministic_overrides = load_deterministic_config(args.deterministic_config_path)
for key, value in deterministic_overrides.items():
setattr(args, key, value)
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("ERROR: GEMINI_API_KEY not found.")
print("Please set it in your .env file or as an environment variable.")
print(" cp .env.example .env")
print(" # Then edit .env and add your key")
sys.exit(1)
from src.evaluation import run_experiment
source_prefixes = args.source_prefix if args.source_prefix else None
manifest_instance_ids = tuple(dict.fromkeys(args.instance_id)) if args.instance_id else None
requested_n_issues = len(manifest_instance_ids) if manifest_instance_ids else args.n_issues
manager_max_turns = args.manager_max_turns if args.manager_max_turns is not None else args.max_turns
rag_max_turns = args.rag_max_turns if args.rag_max_turns is not None else args.max_turns
enabled_methods = tuple(
method.strip()
for method in (args.methods or "").split(",")
if method.strip()
) if args.methods else tuple(ALL_METHODS)
unknown_methods = sorted(set(enabled_methods) - set(ALL_METHODS))
if unknown_methods:
print(f"ERROR: unknown methods in --methods: {', '.join(unknown_methods)}")
sys.exit(1)
if args.repeats < 1:
print("ERROR: --repeats must be >= 1")
sys.exit(1)
if args.repeats > 1 and args.flat_results:
print("ERROR: --flat-results cannot be used with --repeats > 1 (results would overwrite).")
sys.exit(1)
issue_set_id = args.issue_set_id
suite_id = args.suite_id
if suite_id is None and issue_set_id is not None:
suite_id = build_suite_id(
task_family=args.task_family,
dataset_name=args.dataset_name,
repo_name=args.repo_name,
issue_set_id=issue_set_id,
n_issues_requested=requested_n_issues,
source_prefixes=tuple(dict.fromkeys(source_prefixes)) if source_prefixes else (),
manager_max_turns=manager_max_turns,
rag_max_turns=rag_max_turns,
evaluation_track=args.evaluation_track,
snapshot_commit=args.snapshot_commit,
seed=args.seed,
)
summaries = []
for i in range(args.repeats):
if args.repeats > 1:
print(f"\n########## REPEAT {i + 1}/{args.repeats} ##########\n")
summary = run_experiment(
gemini_api_key=api_key,
n_issues=args.n_issues,
results_dir=args.results_dir,
create_run_subdir=not args.flat_results,
source_prefixes=tuple(dict.fromkeys(source_prefixes)) if source_prefixes else None,
manager_max_turns=manager_max_turns,
rag_max_turns=rag_max_turns,
task_family=args.task_family,
dataset_name=args.dataset_name,
repo_name=args.repo_name,
issue_set_id=issue_set_id,
suite_id=suite_id,
repeat_count=args.repeats,
repeat_index=i + 1,
experiment_notes=args.notes,
redact_paths_in_issue_text=not args.no_redact_issue_paths,
evaluation_track=args.evaluation_track,
snapshot_commit=args.snapshot_commit,
instance_ids=manifest_instance_ids,
domain=args.domain,
seed=args.seed,
deterministic_seed_k=args.deterministic_seed_k,
deterministic_depth=args.deterministic_depth,
deterministic_neighbor_cap=args.deterministic_neighbor_cap,
deterministic_min_return_files=args.deterministic_min_return_files,
deterministic_score_ratio_cutoff=args.deterministic_score_ratio_cutoff,
deterministic_min_score_cutoff=args.deterministic_min_score_cutoff,
deterministic_hub_degree_threshold=args.deterministic_hub_degree_threshold,
deterministic_hub_penalty_scale=args.deterministic_hub_penalty_scale,
deterministic_w_sem=args.deterministic_w_sem,
deterministic_w_graph=args.deterministic_w_graph,
deterministic_w_conf=args.deterministic_w_conf,
deterministic_w_hint=args.deterministic_w_hint,
deterministic_w_pen=args.deterministic_w_pen,
methods=enabled_methods,
)
if isinstance(summary, dict):
summaries.append(summary)
meta = summary.get("_meta", {})
if issue_set_id is None:
issue_set_id = meta.get("issue_set_id")
if suite_id is None:
suite_id = meta.get("suite_id")
if args.repeats > 1 and summaries:
aggregate = aggregate_repeat_summaries(summaries)
evaluated_counts = [
int(summary.get("_meta", {}).get("n_issues_evaluated", 0))
for summary in summaries
if isinstance(summary, dict)
]
inferred_n_eval = max(evaluated_counts) if evaluated_counts else args.n_issues
first_meta = summaries[0].get("_meta", {}) if summaries else {}
aggregate["_meta"] = {
"created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"n_issues": requested_n_issues,
"n_issues_requested": requested_n_issues,
"n_issues_evaluated": inferred_n_eval,
"source_prefixes": list(dict.fromkeys(source_prefixes)) if source_prefixes else [],
"manager_max_turns": manager_max_turns,
"rag_max_turns": rag_max_turns,
"repeats": args.repeats,
"task_family": args.task_family,
"dataset_name": args.dataset_name,
"repo_name": args.repo_name,
"issue_set_id": issue_set_id or first_meta.get("issue_set_id", "unknown"),
"suite_id": suite_id or first_meta.get("suite_id", "unknown"),
"notes": args.notes,
"domain": args.domain,
"evaluation_track": args.evaluation_track,
"snapshot_commit": args.snapshot_commit,
"seed": args.seed,
"enabled_methods": list(enabled_methods),
"manifest_instance_ids": list(manifest_instance_ids or []),
"deterministic_retrieval": {
"config_path": args.deterministic_config_path,
"seed_k": args.deterministic_seed_k,
"depth": args.deterministic_depth,
"neighbor_cap": args.deterministic_neighbor_cap,
"min_return_files": args.deterministic_min_return_files,
"score_ratio_cutoff": args.deterministic_score_ratio_cutoff,
"min_score_cutoff": args.deterministic_min_score_cutoff,
"hub_degree_threshold": args.deterministic_hub_degree_threshold,
"hub_penalty_scale": args.deterministic_hub_penalty_scale,
"w_sem": args.deterministic_w_sem,
"w_graph": args.deterministic_w_graph,
"w_conf": args.deterministic_w_conf,
"w_hint": args.deterministic_w_hint,
"w_pen": args.deterministic_w_pen,
},
}
repeat_sets_dir = Path(args.results_dir) / "repeat_sets"
repeat_sets_dir.mkdir(parents=True, exist_ok=True)
repeat_id = time.strftime("%Y%m%d_%H%M%S")
output_path = repeat_sets_dir / f"{repeat_id}.json"
output_path.write_text(json.dumps(aggregate, indent=2))
print(f"\nRepeat aggregate saved to {output_path}")
print("Summary (mean ± std F1):")
for method in ALL_METHODS:
f1 = aggregate.get("methods", {}).get(method, {}).get("mean_f1", {})
if not f1:
continue
print(
f" {method:<18} {f1.get('mean', 0):.3f} ± {f1.get('std', 0):.3f} "
f"(min={f1.get('min', 0):.3f}, max={f1.get('max', 0):.3f})"
)
if __name__ == "__main__":
main()