-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_bugfix_history_python.py
More file actions
1407 lines (1194 loc) · 57.3 KB
/
scan_bugfix_history_python.py
File metadata and controls
1407 lines (1194 loc) · 57.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Bugfix-Driven Improvement Pipeline for a3-python using BugsInPy.
Uses the BugsInPy benchmark (17 real-world Python projects with curated bugs)
as ground truth. For each bug, checks out the buggy and fixed commits,
scans with a3-python, classifies false positives / false negatives,
and iteratively invokes copilot-cli to improve a3's detectors.
Iterative loop:
1. SCAN — Checkout BugsInPy buggy/fixed commits, scan with a3-python
2. DIAGNOSE — Classify FP/FN, identify pathological detector gaps
3. IMPROVE — Feed targeted fix prompts to copilot CLI, re-scan to verify
4. LOOP — Re-scans to measure improvement; stops on plateau or max iterations
Usage:
python3 scan_bugfix_history_python.py # Full pipeline (default 5 iters)
python3 scan_bugfix_history_python.py --max-iterations 10 # More iterations
python3 scan_bugfix_history_python.py --scan-only # Phase 1+2 only (no improvements)
python3 scan_bugfix_history_python.py --project keras # Single BugsInPy project
python3 scan_bugfix_history_python.py --max-bugs 20 # Limit bugs per project
python3 scan_bugfix_history_python.py --dry-run # Print prompts, don't execute
python3 scan_bugfix_history_python.py --resume # Resume from last checkpoint
python3 scan_bugfix_history_python.py --repo-url https://github.com/pallets/flask # Ad-hoc repo
"""
from __future__ import annotations
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import textwrap
import time
from collections import defaultdict
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple
# ── paths ──────────────────────────────────────────────────────────────────────
WORKSPACE = Path(__file__).parent.absolute()
BUGSINPY_REPO = "https://github.com/soarsmu/BugsInPy.git"
BUGSINPY_DIR = WORKSPACE / "BugsInPy"
CHECKOUT_DIR = WORKSPACE / "bugsinpy_workspace"
RESULTS_DIR = WORKSPACE / "results" / "bugfix_scan_python"
STATE_FILE = RESULTS_DIR / "pipeline_state.json"
LOG_FILE = WORKSPACE / "bugfix_scan_python.log"
TEST_REPOS = WORKSPACE / "test_repos_python"
# All 17 BugsInPy projects
PROJECTS = [
"PySnooper", "ansible", "black", "cookiecutter", "fastapi",
"httpie", "keras", "luigi", "matplotlib", "pandas",
"sanic", "scrapy", "spacy", "thefuck", "tornado",
"tqdm", "youtube-dl",
]
# Maps a3-python bug types → detector source files
DETECTOR_FILE_MAP = {
"ASSERT_FAIL": "a3_python/unsafe/assert_fail.py",
"DIV_ZERO": "a3_python/unsafe/div_zero.py",
"FP_DOMAIN": "a3_python/unsafe/fp_domain.py",
"INTEGER_OVERFLOW": "a3_python/unsafe/integer_overflow.py",
"BOUNDS": "a3_python/unsafe/bounds.py",
"NULL_PTR": "a3_python/unsafe/null_ptr.py",
"TYPE_CONFUSION": "a3_python/unsafe/type_confusion.py",
"STACK_OVERFLOW": "a3_python/unsafe/stack_overflow.py",
"MEMORY_LEAK": "a3_python/unsafe/memory_leak.py",
"NON_TERMINATION": "a3_python/unsafe/non_termination.py",
"ITERATOR_INVALID": "a3_python/unsafe/iterator_invalid.py",
"USE_AFTER_FREE": "a3_python/unsafe/use_after_free.py",
"DOUBLE_FREE": "a3_python/unsafe/double_free.py",
"UNINIT_MEMORY": "a3_python/unsafe/uninit_memory.py",
"DATA_RACE": "a3_python/unsafe/data_race.py",
"DEADLOCK": "a3_python/unsafe/deadlock.py",
"SEND_SYNC": "a3_python/unsafe/send_sync.py",
"INFO_LEAK": "a3_python/unsafe/info_leak.py",
"TIMING_CHANNEL": "a3_python/unsafe/timing_channel.py",
"PANIC": "a3_python/unsafe/panic.py",
"SQL_INJECTION": "a3_python/unsafe/security/sql_injection.py",
"COMMAND_INJECTION": "a3_python/unsafe/security/command_injection.py",
"CODE_INJECTION": "a3_python/unsafe/security/code_injection.py",
"PATH_INJECTION": "a3_python/unsafe/security/path_injection.py",
"REFLECTED_XSS": "a3_python/unsafe/security/xss.py",
"SSRF": "a3_python/unsafe/security/ssrf.py",
"UNSAFE_DESERIALIZATION": "a3_python/unsafe/security/deserialization.py",
"XXE": "a3_python/unsafe/security/xxe.py",
"CLEARTEXT_LOGGING": "a3_python/unsafe/security/cleartext.py",
"CLEARTEXT_STORAGE": "a3_python/unsafe/security/cleartext.py",
}
# Maps commit/patch keywords → a3-python bug types for classification
BUG_KEYWORD_MAP = {
"overflow": "INTEGER_OVERFLOW", "underflow": "INTEGER_OVERFLOW",
"indexerror": "BOUNDS", "index error": "BOUNDS",
"out of bounds": "BOUNDS", "out-of-bounds": "BOUNDS",
"keyerror": "BOUNDS", "key error": "BOUNDS",
"typeerror": "TYPE_CONFUSION", "type error": "TYPE_CONFUSION",
"valueerror": "ASSERT_FAIL", "value error": "ASSERT_FAIL",
"assertionerror": "ASSERT_FAIL", "assertion": "ASSERT_FAIL",
"zerodivisionerror": "DIV_ZERO", "division by zero": "DIV_ZERO",
"divide by zero": "DIV_ZERO",
"attributeerror": "NULL_PTR", "attribute error": "NULL_PTR",
"nonetype": "NULL_PTR", "null": "NULL_PTR",
"recursionerror": "STACK_OVERFLOW", "recursion": "STACK_OVERFLOW",
"stack overflow": "STACK_OVERFLOW",
"memoryerror": "MEMORY_LEAK", "memory leak": "MEMORY_LEAK", "leak": "MEMORY_LEAK",
"race": "DATA_RACE", "data race": "DATA_RACE",
"deadlock": "DEADLOCK",
"infinite loop": "NON_TERMINATION", "hang": "NON_TERMINATION",
"sql injection": "SQL_INJECTION", "sqli": "SQL_INJECTION",
"command injection": "COMMAND_INJECTION",
"xss": "REFLECTED_XSS", "cross-site": "REFLECTED_XSS",
"deserialization": "UNSAFE_DESERIALIZATION", "pickle": "UNSAFE_DESERIALIZATION",
"xxe": "XXE",
}
PLATEAU_THRESHOLD = 0.02
PLATEAU_PATIENCE = 2
DEFAULT_MAX_ITERS = 5
MAX_FIX_RETRIES = 3
A3_SCAN_TIMEOUT = 120
COPILOT_TIMEOUT = 900
SKIP_DIRS = {
"__pycache__", ".git", "venv", ".venv", "node_modules",
".egg-info", "dist", "build", ".tox", ".mypy_cache",
"tests", "test", "testing", "docs", "doc", "examples",
"benchmarks", "scripts",
}
# ── logging ────────────────────────────────────────────────────────────────────
def log(msg: str, level: str = "INFO"):
ts = datetime.now().strftime("%H:%M:%S")
line = f"[{ts}] [{level}] {msg}"
print(line)
with open(LOG_FILE, "a") as f:
f.write(line + "\n")
def log_phase(phase: str, iteration: int):
print(f"\n{'='*70}")
print(f" ITERATION {iteration} — PHASE: {phase}")
print(f"{'='*70}\n")
# ── BugsInPy helpers ───────────────────────────────────────────────────────────
def ensure_bugsinpy_cloned() -> None:
"""Clone BugsInPy if not already present."""
if BUGSINPY_DIR.exists():
log(f"BugsInPy already at {BUGSINPY_DIR}")
return
log(f"Cloning BugsInPy to {BUGSINPY_DIR} ...")
subprocess.run(
["git", "clone", "--depth=1", BUGSINPY_REPO, str(BUGSINPY_DIR)],
check=True, timeout=120,
)
log("BugsInPy cloned.")
def list_bugs(project: str) -> list[int]:
"""Return sorted list of bug IDs for a BugsInPy project."""
bugs_dir = BUGSINPY_DIR / "projects" / project / "bugs"
if not bugs_dir.exists():
return []
return sorted(int(d.name) for d in bugs_dir.iterdir()
if d.is_dir() and d.name.isdigit())
def get_bug_info(project: str, bug_id: int) -> dict:
"""Parse bug.info for a specific bug."""
info_file = BUGSINPY_DIR / "projects" / project / "bugs" / str(bug_id) / "bug.info"
info = {}
if info_file.exists():
for line in info_file.read_text().splitlines():
line = line.strip()
if "=" in line:
key, _, val = line.partition("=")
info[key.strip()] = val.strip().strip('"')
return info
def get_project_info(project: str) -> dict:
"""Parse project.info for a BugsInPy project."""
info_file = BUGSINPY_DIR / "projects" / project / "project.info"
info = {}
if info_file.exists():
for line in info_file.read_text().splitlines():
line = line.strip()
if "=" in line:
key, _, val = line.partition("=")
info[key.strip()] = val.strip().strip('"')
return info
def get_patch_files(project: str, bug_id: int) -> list[str]:
"""Extract changed source file paths from bug_patch.txt."""
patch_file = BUGSINPY_DIR / "projects" / project / "bugs" / str(bug_id) / "bug_patch.txt"
if not patch_file.exists():
return []
changed = []
for line in patch_file.read_text().splitlines():
if line.startswith("diff --git"):
parts = line.split()
if len(parts) >= 4:
path = parts[3].lstrip("b/")
if path.endswith(".py") and not path.startswith("test"):
changed.append(path)
elif line.startswith("+++ b/"):
path = line[6:]
if path.endswith(".py") and not path.startswith("test"):
changed.append(path)
seen = set()
result = []
for p in changed:
if p not in seen:
seen.add(p)
result.append(p)
return result
def get_patch_context(project: str, bug_id: int) -> str:
"""Return raw patch text for context in prompts."""
patch_file = BUGSINPY_DIR / "projects" / project / "bugs" / str(bug_id) / "bug_patch.txt"
if patch_file.exists():
return patch_file.read_text()[:3000]
return ""
def checkout_bug(project: str, bug_id: int, buggy: bool = True) -> Path:
"""Checkout a BugsInPy bug (buggy or fixed version) into workspace."""
bug_info = get_bug_info(project, bug_id)
project_info = get_project_info(project)
github_url = project_info.get("github_url", "")
if not github_url:
raise ValueError(f"No github_url for project {project}")
commit_id = bug_info.get("buggy_commit_id" if buggy else "fixed_commit_id", "")
if not commit_id:
raise ValueError(f"No {'buggy' if buggy else 'fixed'}_commit_id for {project} bug {bug_id}")
version_label = "buggy" if buggy else "fixed"
checkout_dir = CHECKOUT_DIR / f"{project}_bug{bug_id}_{version_label}"
if checkout_dir.exists():
shutil.rmtree(checkout_dir)
checkout_dir.mkdir(parents=True, exist_ok=True)
log(f" Cloning {project} at {version_label} commit {commit_id[:8]}...")
try:
subprocess.run(
["git", "clone", "--depth=50", github_url, str(checkout_dir)],
check=True, capture_output=True, timeout=120,
)
subprocess.run(
["git", "-C", str(checkout_dir), "fetch", "--depth=50", "origin", commit_id],
capture_output=True, timeout=60,
)
subprocess.run(
["git", "-C", str(checkout_dir), "checkout", commit_id],
check=True, capture_output=True, timeout=30,
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
log(f" WARNING: Could not checkout {project} bug {bug_id}: {e}", "WARN")
if checkout_dir.exists():
shutil.rmtree(checkout_dir)
raise
return checkout_dir
def cleanup_checkout(checkout_dir: Path) -> None:
"""Remove a checked-out project directory."""
if checkout_dir.exists():
shutil.rmtree(checkout_dir, ignore_errors=True)
def find_python_files(directory: Path, max_files: int = 20,
priority_files: list[str] | None = None) -> list[Path]:
"""Find Python source files, prioritizing patch-affected files."""
py_files: list[Path] = []
priority_set: set[Path] = set()
if priority_files:
for rel in priority_files:
p = directory / rel
if p.exists() and p.suffix == ".py":
py_files.append(p)
priority_set.add(p.resolve())
if len(py_files) >= max_files:
return py_files[:max_files]
for root, dirs, files in os.walk(directory):
dirs[:] = [d for d in dirs if d not in SKIP_DIRS and not d.startswith(".")]
for f in sorted(files):
if (f.endswith(".py") and not f.startswith("test_")
and f != "setup.py" and f != "conftest.py"):
candidate = Path(root) / f
if candidate.resolve() not in priority_set:
py_files.append(candidate)
if len(py_files) >= max_files:
return py_files
return py_files
# ── a3-python scanning ────────────────────────────────────────────────────────
def scan_file_with_a3(filepath: Path) -> dict:
"""Scan a single Python file with a3. Returns structured results."""
try:
result = subprocess.run(
[sys.executable, "-m", "a3_python", str(filepath),
"--no-intent-filter", "--min-confidence", "0.3"],
capture_output=True, text=True,
timeout=A3_SCAN_TIMEOUT, cwd=str(WORKSPACE),
)
exit_code = result.returncode
stdout = result.stdout + result.stderr
except subprocess.TimeoutExpired:
return {"exit_code": 3, "stdout": "TIMEOUT", "bugs_found": [], "verdict": "ERROR"}
except Exception as e:
return {"exit_code": 3, "stdout": str(e), "bugs_found": [], "verdict": "ERROR"}
bugs_found = []
for line in stdout.splitlines():
if any(kw in line.upper() for kw in ["BUG:", "VULNERABILITY:", "FINDING:", "⚠"]):
bugs_found.append(line.strip())
verdict_map = {0: "SAFE", 1: "BUG", 2: "UNKNOWN"}
verdict = verdict_map.get(exit_code, "ERROR")
return {"exit_code": exit_code, "stdout": stdout, "bugs_found": bugs_found, "verdict": verdict}
def scan_files_aggregate(py_files: list[Path]) -> dict:
"""Scan multiple files and aggregate results."""
results = [scan_file_with_a3(pf) for pf in py_files]
if not results:
return {"exit_code": 0, "stdout": "", "bugs_found": [], "verdict": "SAFE"}
return {
"exit_code": max(r["exit_code"] for r in results),
"stdout": "\n---\n".join(r["stdout"] for r in results)[:4000],
"bugs_found": [b for r in results for b in r["bugs_found"]],
"verdict": (
"BUG" if any(r["verdict"] == "BUG" for r in results) else
"UNKNOWN" if any(r["verdict"] in ("UNKNOWN", "ERROR") for r in results) else
"SAFE"
),
}
# ── FP/FN classification ──────────────────────────────────────────────────────
def classify_bugsinpy_result(
project: str, bug_id: int,
buggy_result: dict, fixed_result: dict,
bug_info: dict,
) -> dict | None:
"""
Compare scan results for buggy vs fixed commits to detect FP/FN.
Ground truth:
- Buggy commit has a real bug → a3 should report BUG (else FN)
- Fixed commit is correct → a3 should NOT report that bug (else FP)
Returns a dict describing the misclassification, or None if correct.
"""
test_file = bug_info.get("test_file", "")
# FALSE NEGATIVE: buggy code scans clean
if buggy_result["verdict"] == "SAFE" and fixed_result["verdict"] == "SAFE":
return {
"type": "false_negative",
"project": project,
"bug_id": bug_id,
"description": (
f"a3 reported SAFE on BOTH buggy and fixed versions of "
f"{project} bug #{bug_id}. The buggy version has a known bug "
f"(test: {test_file}) that a3 failed to detect."
),
"buggy_output": buggy_result["stdout"][:2000],
"fixed_output": fixed_result["stdout"][:2000],
"buggy_bugs": buggy_result["bugs_found"],
"fixed_bugs": fixed_result["bugs_found"],
}
# FALSE POSITIVE: fixed code still flagged
if fixed_result["verdict"] == "BUG" and fixed_result["bugs_found"]:
buggy_bug_set = set(buggy_result.get("bugs_found", []))
fixed_bug_set = set(fixed_result.get("bugs_found", []))
# Bugs only in fixed version (not in buggy) → FP
fixed_only_bugs = [b for b in fixed_result["bugs_found"] if b not in buggy_bug_set]
if fixed_only_bugs:
return {
"type": "false_positive",
"project": project,
"bug_id": bug_id,
"description": (
f"a3 reported bugs on the FIXED version of {project} bug #{bug_id} "
f"that weren't in the buggy version: {fixed_only_bugs[:5]}"
),
"buggy_output": buggy_result["stdout"][:2000],
"fixed_output": fixed_result["stdout"][:2000],
"buggy_bugs": buggy_result["bugs_found"],
"fixed_bugs": fixed_result["bugs_found"],
"false_positive_bugs": fixed_only_bugs,
}
# Same bugs in both versions → a3 didn't recognize the fix
if fixed_bug_set == buggy_bug_set and len(fixed_bug_set) > 0:
return {
"type": "same_findings",
"project": project,
"bug_id": bug_id,
"description": (
f"a3 reported the EXACT SAME bugs on both buggy and fixed "
f"versions of {project} bug #{bug_id}. It failed to recognize "
f"the fix — false positives on fixed code."
),
"buggy_output": buggy_result["stdout"][:2000],
"fixed_output": fixed_result["stdout"][:2000],
"buggy_bugs": buggy_result["bugs_found"],
"fixed_bugs": fixed_result["bugs_found"],
}
return None
def classify_simple(pre_verdict: str, post_verdict: str) -> str:
"""Simple pre/post classification for reporting."""
if pre_verdict == "BUG" and post_verdict == "SAFE":
return "DETECTED"
if pre_verdict == "BUG" and post_verdict == "BUG":
return "PARTIAL"
if pre_verdict == "SAFE" and post_verdict == "SAFE":
return "MISSED"
if pre_verdict == "SAFE" and post_verdict == "BUG":
return "INVERTED"
return "INCONCLUSIVE"
def infer_bug_type(message: str) -> str:
"""Infer a3-python bug type from commit message or patch context."""
msg_lower = message.lower()
for keyword, bug_type in BUG_KEYWORD_MAP.items():
try:
pattern = r"\b" + re.escape(keyword) + r"\b"
if re.search(pattern, msg_lower):
return bug_type
except re.error:
if keyword in msg_lower:
return bug_type
return "unknown"
# ── ad-hoc git repo scanning (fallback for non-BugsInPy repos) ────────────────
def discover_repos(specific_repo: str = "") -> list[Path]:
"""Find local repos to scan, optionally clone new ones."""
repos = []
if specific_repo:
p = Path(specific_repo)
if p.exists():
repos.append(p.resolve())
elif specific_repo.startswith("http"):
name = specific_repo.rstrip("/").split("/")[-1].replace(".git", "")
dest = TEST_REPOS / name
if not dest.exists():
log(f"Cloning {specific_repo} → {dest}")
TEST_REPOS.mkdir(parents=True, exist_ok=True)
subprocess.run(
["git", "clone", "--depth", "200", specific_repo, str(dest)],
capture_output=True, timeout=120,
)
if dest.exists():
repos.append(dest)
elif TEST_REPOS.exists():
for child in sorted(TEST_REPOS.iterdir()):
if child.is_dir() and (child / ".git").exists():
repos.append(child)
return repos
def find_bugfix_commits(repo: Path, max_commits: int = 50) -> list[dict]:
"""Search git history for bug-fix commits (ad-hoc repo mode)."""
grep_terms = [
"fix", "bug", "crash", "error", "exception", "overflow", "leak",
"race", "null", "security", "vulnerability", "CVE",
"IndexError", "KeyError", "TypeError", "ValueError",
]
grep_args = []
for term in grep_terms:
grep_args.extend(["--grep", term])
try:
result = subprocess.run(
["git", "--no-pager", "log", "--oneline", "--all", "-i",
f"--max-count={max_commits * 2}"] + grep_args,
cwd=str(repo), capture_output=True, text=True, timeout=30,
)
except Exception:
return []
commits = []
for line in result.stdout.strip().splitlines():
if not line.strip():
continue
parts = line.strip().split(" ", 1)
sha, msg = parts[0], (parts[1] if len(parts) > 1 else "")
try:
diff_result = subprocess.run(
["git", "--no-pager", "diff", "--name-only", f"{sha}~1", sha, "--", "*.py"],
cwd=str(repo), capture_output=True, text=True, timeout=10,
)
py_files = [f for f in diff_result.stdout.strip().splitlines() if f.endswith(".py")]
if not py_files:
continue
except Exception:
continue
commits.append({
"sha": sha, "message": msg, "py_files": py_files,
"bug_type": infer_bug_type(msg),
})
if len(commits) >= max_commits:
break
return commits
def extract_file_at_commit(repo: Path, sha: str, filepath: str) -> Optional[str]:
"""Extract file content at a specific commit."""
try:
result = subprocess.run(
["git", "--no-pager", "show", f"{sha}:{filepath}"],
cwd=str(repo), capture_output=True, text=True, timeout=10,
)
return result.stdout if result.returncode == 0 else None
except Exception:
return None
# ══════════════════════════════════════════════════════════════════════════════
# PHASE 1: SCAN — BugsInPy-driven evaluation
# ══════════════════════════════════════════════════════════════════════════════
def phase_scan_bugsinpy(projects: list[str], max_bugs: int = 50) -> list[dict]:
"""Scan BugsInPy bugs: checkout buggy/fixed, scan with a3, classify."""
all_results = []
for project in projects:
bug_ids = list_bugs(project)
if not bug_ids:
log(f" No bugs found for {project}, skipping.")
continue
log(f"Scanning {project} ({len(bug_ids)} bugs)...")
for bug_id in bug_ids[:max_bugs]:
bug_info = get_bug_info(project, bug_id)
patch_files = get_patch_files(project, bug_id)
patch_context = get_patch_context(project, bug_id)
bug_type_inferred = infer_bug_type(
patch_context + " " + bug_info.get("test_file", ""))
log(f" {project} bug #{bug_id} (test: {bug_info.get('test_file', '?')[:40]})")
buggy_dir = None
fixed_dir = None
try:
buggy_dir = checkout_bug(project, bug_id, buggy=True)
py_files_buggy = find_python_files(
buggy_dir, max_files=10, priority_files=patch_files)
if not py_files_buggy:
log(f" No Python files found, skipping.")
continue
buggy_agg = scan_files_aggregate(py_files_buggy)
fixed_dir = checkout_bug(project, bug_id, buggy=False)
py_files_fixed = find_python_files(
fixed_dir, max_files=10, priority_files=patch_files)
fixed_agg = scan_files_aggregate(py_files_fixed)
classification = classify_simple(buggy_agg["verdict"], fixed_agg["verdict"])
misclass = classify_bugsinpy_result(
project, bug_id, buggy_agg, fixed_agg, bug_info)
entry = {
"source": "bugsinpy",
"project": project,
"bug_id": bug_id,
"test_file": bug_info.get("test_file", ""),
"patch_files": patch_files,
"bug_type_inferred": bug_type_inferred,
"buggy_verdict": buggy_agg["verdict"],
"fixed_verdict": fixed_agg["verdict"],
"buggy_bugs": buggy_agg["bugs_found"][:20],
"fixed_bugs": fixed_agg["bugs_found"][:20],
"buggy_output": buggy_agg["stdout"][:500],
"fixed_output": fixed_agg["stdout"][:500],
"classification": classification,
"misclass_type": misclass["type"] if misclass else None,
"misclass_description": misclass["description"] if misclass else None,
"should_detect": True,
}
all_results.append(entry)
symbol = "✓" if misclass is None else "✗"
log(f" {symbol} {classification} "
f"(buggy={buggy_agg['verdict']}, fixed={fixed_agg['verdict']})"
+ (f" [{misclass['type']}]" if misclass else ""))
except Exception as e:
log(f" ERROR: {e}", "WARN")
all_results.append({
"source": "bugsinpy",
"project": project,
"bug_id": bug_id,
"classification": "ERROR",
"error": str(e),
"should_detect": True,
"bug_type_inferred": bug_type_inferred,
})
finally:
if buggy_dir:
cleanup_checkout(buggy_dir)
if fixed_dir:
cleanup_checkout(fixed_dir)
return all_results
def phase_scan_adhoc(repos: list[Path], max_commits: int = 50) -> list[dict]:
"""Scan ad-hoc repos by git history (fallback for non-BugsInPy repos)."""
all_results = []
for repo in repos:
repo_name = repo.name
log(f"Scanning {repo_name} (ad-hoc)...")
try:
subprocess.run(
["git", "fetch", "--deepen", "500"],
cwd=str(repo), capture_output=True, timeout=60,
)
except Exception:
pass
commits = find_bugfix_commits(repo, max_commits=max_commits)
log(f" Found {len(commits)} bugfix commits in {repo_name}")
for commit in commits:
for py_file in commit["py_files"][:3]:
pre_src = extract_file_at_commit(repo, f"{commit['sha']}~1", py_file)
post_src = extract_file_at_commit(repo, commit["sha"], py_file)
if not pre_src or not post_src:
continue
# Write to temp files and scan
import tempfile
pre_result = _scan_source(pre_src)
post_result = _scan_source(post_src)
classification = classify_simple(pre_result["verdict"], post_result["verdict"])
entry = {
"source": "git_history",
"project": repo_name,
"commit": commit["sha"],
"message": commit["message"],
"file": py_file,
"bug_type_inferred": commit["bug_type"],
"buggy_verdict": pre_result["verdict"],
"fixed_verdict": post_result["verdict"],
"buggy_bugs": pre_result["bugs_found"][:10],
"fixed_bugs": post_result["bugs_found"][:10],
"buggy_output": pre_result["stdout"][:300],
"fixed_output": post_result["stdout"][:300],
"classification": classification,
"should_detect": commit["bug_type"] != "unknown",
}
all_results.append(entry)
log(f" {commit['sha'][:7]} {py_file}: {classification} "
f"(pre={pre_result['verdict']}, post={post_result['verdict']})")
return all_results
def _scan_source(source: str) -> dict:
"""Scan Python source string with a3 via temp file."""
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(source)
tmp = Path(f.name)
try:
return scan_file_with_a3(tmp)
finally:
tmp.unlink(missing_ok=True)
# ══════════════════════════════════════════════════════════════════════════════
# PHASE 2: DIAGNOSE — analyze gaps, identify pathological patterns
# ══════════════════════════════════════════════════════════════════════════════
def phase_diagnose(results: list[dict]) -> dict:
"""Analyze scan results to find pathological gaps in a3-python."""
total = len(results)
by_class = defaultdict(list)
detector_gaps = defaultdict(list)
for r in results:
by_class[r.get("classification", "ERROR")].append(r)
detectable = [r for r in results if r.get("should_detect")]
detected = [r for r in detectable if r["classification"] == "DETECTED"]
partial = [r for r in detectable if r["classification"] == "PARTIAL"]
missed = [r for r in detectable if r["classification"] == "MISSED"]
inverted = [r for r in detectable if r["classification"] == "INVERTED"]
false_negatives = [r for r in results if r.get("misclass_type") == "false_negative"]
false_positives = [r for r in results if r.get("misclass_type") in ("false_positive", "same_findings")]
detection_rate = len(detected) / max(len(detectable), 1)
# Group missed/FN by inferred bug type → detector gaps
for r in missed + false_negatives:
bug_type = r.get("bug_type_inferred", "unknown")
detector_file = DETECTOR_FILE_MAP.get(bug_type, "unknown")
detector_gaps[detector_file].append(r)
diagnosis = {
"total_analyzed": total,
"detectable": len(detectable),
"detected": len(detected),
"partial": len(partial),
"missed": len(missed),
"inverted": len(inverted),
"false_negatives": len(false_negatives),
"false_positives": len(false_positives),
"inconclusive": len(by_class.get("INCONCLUSIVE", [])),
"errors": len(by_class.get("ERROR", [])),
"detection_rate": detection_rate,
"detector_gaps": {
det: [{"project": r.get("project", "?"),
"bug_id": r.get("bug_id", r.get("commit", "?")),
"classification": r["classification"],
"bug_type": r.get("bug_type_inferred", "?"),
"description": r.get("misclass_description", ""),
"buggy_output": r.get("buggy_output", "")[:300]}
for r in entries]
for det, entries in detector_gaps.items()
},
"pathological_patterns": _identify_pathological_patterns(missed + false_negatives),
"false_positive_entries": [
{"project": r.get("project"), "bug_id": r.get("bug_id"),
"description": r.get("misclass_description", "")}
for r in false_positives[:20]
],
}
log(f"Diagnosis: {len(detected)} DETECTED, {len(partial)} PARTIAL, "
f"{len(missed)} MISSED, {len(inverted)} INVERTED")
log(f" FN: {len(false_negatives)}, FP: {len(false_positives)}, "
f"Errors: {len(by_class.get('ERROR', []))}")
log(f" Detection rate: {detection_rate:.1%}")
return diagnosis
def _identify_pathological_patterns(missed_entries: list[dict]) -> list[dict]:
"""Identify recurring weakness patterns."""
patterns = []
type_counts = defaultdict(int)
for r in missed_entries:
type_counts[r.get("bug_type_inferred", "unknown")] += 1
for bug_type, count in sorted(type_counts.items(), key=lambda x: -x[1]):
examples = [r for r in missed_entries if r.get("bug_type_inferred") == bug_type]
detector = DETECTOR_FILE_MAP.get(bug_type, "unknown")
patterns.append({
"bug_type": bug_type,
"count": count,
"detector_file": detector,
"severity": "HIGH" if count >= 3 else "MEDIUM" if count >= 2 else "LOW",
"examples": [
{"project": e.get("project", "?"),
"bug_id": e.get("bug_id", e.get("commit", "?")[:7]),
"description": (e.get("misclass_description") or e.get("message", ""))[:80]}
for e in examples[:5]
],
})
return patterns
# ══════════════════════════════════════════════════════════════════════════════
# PHASE 3: IMPROVE — copilot-driven targeted fixes with verify loop
# ══════════════════════════════════════════════════════════════════════════════
def phase_improve(diagnosis: dict, results: list[dict], iteration: int,
dry_run: bool = False, model: str = "claude-sonnet-4") -> bool:
"""Generate and apply improvements, then verify with re-scan."""
gaps = diagnosis.get("detector_gaps", {})
patterns = diagnosis.get("pathological_patterns", [])
fp_entries = diagnosis.get("false_positive_entries", [])
if not gaps and not patterns and not fp_entries:
log("No gaps to fix — all bugs detected!")
return False
# Pick the worst misclassification to fix first (BugsInPy-style)
worst = _pick_worst_misclass(results)
if worst:
prompt = _build_targeted_fix_prompt(worst, diagnosis, iteration)
else:
prompt = _build_improvement_prompt(diagnosis, iteration)
prompt_file = RESULTS_DIR / f"improvement_prompt_iter{iteration}.md"
prompt_file.write_text(prompt)
log(f"Improvement prompt saved to {prompt_file}")
if dry_run:
log("DRY RUN — skipping copilot execution")
return False
# Try fix with verification loop (like iterative_bugsinpy_improve.py)
for retry in range(MAX_FIX_RETRIES):
log(f"Fix attempt {retry + 1}/{MAX_FIX_RETRIES}...")
success = _run_copilot(prompt, model=model)
if not success:
log(f"Copilot did not apply fix on attempt {retry + 1}", "WARN")
continue
# Verify: re-scan the specific bug that failed
if worst and worst.get("source") == "bugsinpy":
verified = _verify_bugsinpy_fix(worst)
if verified:
log("Fix verified! Re-scan confirms improvement.")
return True
else:
log("Fix not verified — re-scan still shows same issue.")
else:
log("Improvement applied (no targeted verification available)")
return True
log(f"Max retries ({MAX_FIX_RETRIES}) exhausted")
return True # still proceed to next iteration
def _pick_worst_misclass(results: list[dict]) -> dict | None:
"""Pick the first false negative or false positive to fix."""
for r in results:
if r.get("misclass_type") == "false_negative":
return r
for r in results:
if r.get("misclass_type") in ("false_positive", "same_findings"):
return r
# Fallback: first MISSED
for r in results:
if r.get("classification") == "MISSED" and r.get("should_detect"):
return r
return None
def _verify_bugsinpy_fix(entry: dict) -> bool:
"""Re-scan a specific BugsInPy bug to verify the fix worked."""
project = entry.get("project")
bug_id = entry.get("bug_id")
if not project or not bug_id:
return False
patch_files = get_patch_files(project, bug_id)
bug_info = get_bug_info(project, bug_id)
buggy_dir = None
fixed_dir = None
try:
if entry.get("misclass_type") == "false_negative":
buggy_dir = checkout_bug(project, bug_id, buggy=True)
py_files = find_python_files(buggy_dir, max_files=10, priority_files=patch_files)
result = scan_files_aggregate(py_files)
return result["verdict"] == "BUG"
elif entry.get("misclass_type") in ("false_positive", "same_findings"):
fixed_dir = checkout_bug(project, bug_id, buggy=False)
py_files = find_python_files(fixed_dir, max_files=10, priority_files=patch_files)
result = scan_files_aggregate(py_files)
return result["verdict"] != "BUG" or len(result["bugs_found"]) == 0
return False
except Exception as e:
log(f" Verification error: {e}", "WARN")
return False
finally:
if buggy_dir:
cleanup_checkout(buggy_dir)
if fixed_dir:
cleanup_checkout(fixed_dir)
def _build_targeted_fix_prompt(entry: dict, diagnosis: dict, iteration: int) -> str:
"""Build a targeted prompt for a specific FP/FN (BugsInPy-style)."""
project = entry.get("project", "?")
bug_id = entry.get("bug_id", "?")
misclass_type = entry.get("misclass_type", "false_negative")
patch_context = ""
if entry.get("source") == "bugsinpy":
patch_context = get_patch_context(project, bug_id)
if misclass_type == "false_negative":
return textwrap.dedent(f"""\
I am improving a3-python, a Python static analysis tool in this workspace (a3_python/ directory).
FALSE NEGATIVE: a3-python missed a known bug from the BugsInPy benchmark.
Project : {project}
Bug ID : {bug_id}
Bug category: {entry.get('bug_type_inferred', 'unknown')}
Expected a3 bug type: {entry.get('bug_type_inferred', 'unknown')}
Test file : {entry.get('test_file', '?')}
a3 output on buggy code (excerpt):
{entry.get('buggy_output', 'N/A')[:1500]}
Bug patch (what the fix changed):
{patch_context[:2000]}
The a3 analyzer failed to detect this bug. Please investigate the a3-python
source code and improve the analysis to detect this class of bug.
Detection rate this iteration: {diagnosis['detection_rate']:.1%}
({diagnosis['detected']}/{diagnosis['detectable']} detected,
{diagnosis['missed']} missed, {diagnosis.get('false_negatives', 0)} FN)
Common causes of false negatives:
- Missing bug pattern in is_unsafe_* predicate
- Analysis not reaching the buggy code path
- Insufficient symbolic execution depth
- Missing bytecode/AST pattern recognition
- Overly strict preconditions in the detector
Make targeted but potentially large changes. Do NOT break existing functionality.
After changes, verify: python3 -m pytest tests/ -x -q 2>&1 | tail -20
""")
else:
return textwrap.dedent(f"""\
I am improving a3-python, a Python static analysis tool in this workspace (a3_python/ directory).
FALSE POSITIVE: a3-python incorrectly reports bugs on fixed code.
Project : {project}
Bug ID : {bug_id}
Bug category: {entry.get('bug_type_inferred', 'unknown')}
Misclass : {misclass_type}
a3 output on FIXED code (excerpt):
{entry.get('fixed_output', 'N/A')[:1500]}
Bugs reported on fixed code: {entry.get('fixed_bugs', [])[:10]}
Bug patch (what the fix changed):
{patch_context[:2000]}
The a3 analyzer incorrectly reports bugs on code that is actually correct.
Please investigate and fix the analysis logic to avoid this false positive.
Common causes:
- Over-aggressive bug detection patterns
- Missing guard/sanitizer recognition
- Missing control flow analysis proving the path infeasible
- Missing recognition of safe coding patterns
Make targeted but potentially large changes. Do NOT break existing functionality.
After changes, verify: python3 -m pytest tests/ -x -q 2>&1 | tail -20
""")
def _build_improvement_prompt(diagnosis: dict, iteration: int) -> str:
"""Build a general improvement prompt from diagnosis."""
patterns = diagnosis.get("pathological_patterns", [])
gaps = diagnosis.get("detector_gaps", {})
detector_sections = []
for pattern in patterns:
if pattern["severity"] == "LOW":
continue
det_file = pattern["detector_file"]
bug_type = pattern["bug_type"]
example_text = "\n".join(
f" - {e['project']} (bug {e['bug_id']}): {e['description']}"
for e in pattern["examples"]
)
gap_entries = gaps.get(det_file, [])
detail_text = ""
for g in gap_entries[:3]:
detail_text += f"\n a3 output: {g.get('buggy_output', 'N/A')[:200]}"
detector_sections.append(f"""
### Fix {det_file} — missed {pattern['count']} {bug_type} bug(s) [{pattern['severity']}]
**Missed examples:**
{example_text}
{detail_text}