-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_benchmark.py
More file actions
283 lines (249 loc) · 12.1 KB
/
Copy pathrun_benchmark.py
File metadata and controls
283 lines (249 loc) · 12.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
#!/usr/bin/env python3
"""B2 — Call Graph Resolution Quality benchmark runner.
Usage:
benchmarks/.venv/bin/python benchmarks/b2_call_graph_quality/run_benchmark.py [--repo PATH]
Scope (this implementation): **Rust only**. Uses `rust-analyzer scip` as the
ground-truth oracle for the Rust call graph and measures how well `calm`'s
Tier-0/Tier-2 syntactic resolver (Phase A of the Rust support plan) agrees
with it, broken down by `edge_confidence`.
Requires:
- `rust-analyzer` on PATH (or resolvable via rustup/VS Code — same
detection `calm_core::scip::runner::resolve_binary` uses).
- `calm` built with the `scip-overlay` feature, for the hidden `scip-dump`
subcommand that decodes the oracle `.scip` file to JSON (reuses
`calm_core::scip::parse` instead of re-implementing SCIP protobuf decoding
in Python):
cargo build --release -p calm-cli --features scip-overlay
Methodology:
1. Run `rust-analyzer scip <repo> --output oracle.scip`.
2. Decode it via `ci scip-dump oracle.scip` -> flat occurrences.
3. Build the oracle edge set: for every non-local reference occurrence,
resolve its symbol to its (non-local) definition occurrence, giving
(ref_file, ref_line) -> (def_file, def_line). This mirrors
`calm_core::scip::ingest::ingest_occurrences`'s own matching exactly, so
the oracle here is built the same way Phase B's real ingest would use
it — this benchmark and Phase B are measuring the same underlying
correspondence.
4. Run `calm index --project-root <repo>` (default features -- i.e. Phase A
only, no SCIP overlay applied) and read `call_edges` for Rust files.
5. precision = |ci ∩ oracle| / |ci|, recall = |ci ∩ oracle| / |oracle|,
precision also broken down per `edge_confidence` bucket.
"""
from __future__ import annotations
import argparse
import json
import subprocess
import sqlite3
import sys
import tempfile
from collections import defaultdict
from pathlib import Path
def repo_root_from_here() -> Path:
# benchmarks/b2_call_graph_quality/run_benchmark.py -> repo root is 2 levels up
return Path(__file__).resolve().parents[2]
def run(cmd: list[str], **kw) -> subprocess.CompletedProcess:
return subprocess.run(cmd, check=True, capture_output=True, text=True, **kw)
def find_rust_analyzer() -> str:
for candidate in ("rust-analyzer",):
try:
run([candidate, "--version"])
return candidate
except (OSError, subprocess.CalledProcessError):
continue
try:
out = run(["rustup", "which", "--toolchain", "stable", "rust-analyzer"])
path = out.stdout.strip()
if path:
return path
except (OSError, subprocess.CalledProcessError):
pass
sys.exit(
"rust-analyzer not found on PATH or via rustup. "
"Install it (e.g. `rustup component add rust-analyzer`) to run this benchmark."
)
def build_oracle(occurrences: list[dict]) -> set[tuple[str, int, str, int]]:
"""(ref_file, ref_line) -> (def_file, def_line) edges, mirroring
calm_core::scip::ingest::ingest_occurrences's own matching.
2026-08-18 fix: rust-analyzer's scip symbol-naming scheme does not
disambiguate private/local functions by which independently-compiled
`tests/*.rs` integration-test binary they belong to -- two files each
defining their own local `fn run_calm()` (a common, encouraged pattern:
every integration test binary is self-contained) can get the IDENTICAL
scip symbol string. The old plain `dict` keyed by that string silently
let whichever occurrence got processed last "win" as ground truth for
EVERY file's call sites of that name -- wrongly failing a tool that
correctly resolved same-file. Verified live on
`crates/calm-cli/tests/{permissions,hooks}_doctor_fix.rs`'s `run_calm`/
`calm_bin`/`fresh_project` and `crates/calm-core/tests/
{golden_graph_equivalence,derived_artifact_versions}.rs`'s
`index_fresh` -- all 4 collide 2-way. A symbol with >1 distinct def
location is unusable as ground truth (nothing in the scip dump alone
says which one a given reference actually targets), so it's dropped
from the oracle entirely instead of arbitrarily picking one and
silently mismeasuring every tool that "gets it wrong" against that
arbitrary pick."""
def_locations: dict[str, set[tuple[str, int]]] = defaultdict(set)
for o in occurrences:
if o["is_def"] and not o["is_local"]:
def_locations[o["symbol"]].add((o["file"], o["line"]))
collided = {sym for sym, locs in def_locations.items() if len(locs) > 1}
def_of = {sym: next(iter(locs)) for sym, locs in def_locations.items() if len(locs) == 1}
if collided:
preview = ", ".join(sorted(collided)[:5])
print(
f"build_oracle: excluded {len(collided)} symbol(s) with colliding scip def "
f"locations (ambiguous ground truth, not scored either way): {preview}"
f"{', ...' if len(collided) > 5 else ''}"
)
oracle: set[tuple[str, int, str, int]] = set()
for o in occurrences:
if o["is_def"] or o["is_local"] or o["symbol"] in collided:
continue
target = def_of.get(o["symbol"])
if target is None:
continue
oracle.add((o["file"], o["line"], target[0], target[1]))
return oracle
def load_calm_edges(db_path: Path) -> list[tuple[str, int, str, int, str]]:
conn = sqlite3.connect(db_path)
rows = conn.execute(
"SELECT ce.from_path, ce.call_site_line, ce.to_path, s.line_start, ce.edge_confidence "
"FROM call_edges ce "
"JOIN symbols s ON s.qualified_name = ce.to_symbol "
"WHERE ce.from_path LIKE '%.rs' AND ce.to_path LIKE '%.rs' "
" AND ce.call_site_line IS NOT NULL"
).fetchall()
conn.close()
return rows
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--repo",
type=Path,
default=repo_root_from_here(),
help="Rust project to measure (default: this repo)",
)
parser.add_argument(
"--calm-bin",
type=Path,
default=repo_root_from_here() / "target" / "release" / "calm",
help="Path to a `calm` binary built with --features scip-overlay",
)
parser.add_argument(
"--ra-features",
default="all",
help=(
"Cargo features to activate for the rust-analyzer oracle run. "
"Default 'all' (--all-features) so the oracle covers feature-gated "
"source files CALM's tree-sitter indexer always parses (issue #72); "
"pass a comma-separated list to narrow it."
),
)
args = parser.parse_args()
repo = args.repo.resolve()
calm_bin = args.calm_bin.resolve()
if not calm_bin.exists():
sys.exit(
f"{calm_bin} not found. Build it first:\n"
" cargo build --release -p calm-cli --features scip-overlay"
)
ra_bin = find_rust_analyzer()
with tempfile.TemporaryDirectory() as tmp:
scip_path = Path(tmp) / "oracle.scip"
# Oracle coverage fix (issue #72): a bare `rust-analyzer scip <repo>`
# compiles only DEFAULT Cargo features, so it emits ZERO occurrences
# for source files gated behind non-default features (bundle.rs behind
# `index-bundles`, lsp/*.rs behind `lsp-overlay`, http.rs behind
# `http`, ...). CALM's tree-sitter indexer parses those files
# UNCONDITIONALLY, so any CALM edge touching them could never match the
# oracle -- inflating the false-positive count independently of
# resolver quality. Activating all features aligns the oracle's file
# coverage with CALM's. `"all"` (not a hand-listed set) because the
# gating features live on different workspace members (`http` on
# calm-server, not calm-core) and a per-name feature list would error
# on the crate that lacks it; --all-features cannot.
feats = "all" if args.ra_features == "all" else args.ra_features.split(",")
ra_config = Path(tmp) / "ra-config.json"
ra_config.write_text(json.dumps({"cargo": {"features": feats}}))
print(f"Running rust-analyzer scip on {repo} (features={args.ra_features!r}) ...")
run([ra_bin, "scip", str(repo), "--output", str(scip_path),
"--config-path", str(ra_config)])
dump = run([str(calm_bin), "scip-dump", str(scip_path)])
occurrences = [json.loads(line) for line in dump.stdout.splitlines() if line.strip()]
print(f"Decoded {len(occurrences)} SCIP occurrences.")
oracle = build_oracle(occurrences)
print(f"Oracle edges (non-local ref -> def): {len(oracle)}")
print(f"Indexing {repo} with `calm index` (Phase A syntactic resolver only) ...")
run([str(calm_bin), "index", "--project-root", str(repo)])
db_path = repo / ".calm" / "index.db"
calm_edges = load_calm_edges(db_path)
print(f"ci call_edges (Rust, with a call site line): {len(calm_edges)}")
matched = [e for e in calm_edges if (e[0], e[1], e[2], e[3]) in oracle]
precision = len(matched) / len(calm_edges) if calm_edges else 0.0
oracle_hit = {(e[0], e[1], e[2], e[3]) for e in matched}
recall = len(oracle_hit) / len(oracle) if oracle else 0.0
# Oracle coverage audit (issue #72): a file the oracle never emitted an
# occurrence for (still feature-gated even under --all-features, generated,
# or otherwise excluded) can't corroborate ANY CALM edge originating in it,
# so those edges depress precision for reasons unrelated to resolver
# quality. Report coverage explicitly, plus a `precision_on_covered` that
# scores only edges whose from-file the oracle can actually see -- the
# honest number check-b2-thresholds.sh's floors should eventually track.
oracle_files = {o["file"] for o in occurrences}
calm_from_files = {e[0] for e in calm_edges}
uncovered_from_files = sorted(f for f in calm_from_files if f not in oracle_files)
covered_edges = [e for e in calm_edges if e[0] in oracle_files]
covered_matched = [e for e in covered_edges if (e[0], e[1], e[2], e[3]) in oracle]
precision_on_covered = (
len(covered_matched) / len(covered_edges) if covered_edges else 0.0
)
by_conf: dict[str, list[tuple]] = defaultdict(list)
for e in calm_edges:
by_conf[e[4]].append(e)
conf_precision = {}
for conf, edges in sorted(by_conf.items()):
hit = sum(1 for e in edges if (e[0], e[1], e[2], e[3]) in oracle)
conf_precision[conf] = {
"count": len(edges),
"precision": hit / len(edges) if edges else 0.0,
}
print()
print(f"Overall precision: {precision:.3f} ({len(matched)}/{len(calm_edges)})")
print(f"Overall recall: {recall:.3f} ({len(oracle_hit)}/{len(oracle)})")
print()
print(f"{'confidence':<12} {'count':>8} {'precision':>10}")
for conf, stats in conf_precision.items():
print(f"{conf:<12} {stats['count']:>8} {stats['precision']:>10.3f}")
print()
print(
f"Oracle file coverage: {len(oracle_files)} file(s) with >=1 occurrence; "
f"{len(uncovered_from_files)}/{len(calm_from_files)} CALM from-file(s) "
f"invisible to the oracle"
)
print(
f"Precision on oracle-covered files only: {precision_on_covered:.3f} "
f"({len(covered_matched)}/{len(covered_edges)})"
)
if uncovered_from_files:
preview = ", ".join(uncovered_from_files[:8])
print(
f" uncovered from-files (first 8): {preview}"
f"{', ...' if len(uncovered_from_files) > 8 else ''}"
)
result = {
"repo": str(repo),
"oracle_edges": len(oracle),
"calm_edges": len(calm_edges),
"precision": precision,
"recall": recall,
"by_confidence": conf_precision,
"oracle_covered_files": len(oracle_files),
"calm_from_files": len(calm_from_files),
"uncovered_from_files": len(uncovered_from_files),
"precision_on_covered": precision_on_covered,
}
out_path = Path(__file__).parent / "results.json"
out_path.write_text(json.dumps(result, indent=2))
print(f"\nWrote {out_path}")
if __name__ == "__main__":
main()