codebase-memory-mcp icon indicating copy to clipboard operation
codebase-memory-mcp copied to clipboard

pass_semantic_edges: SEMANTICALLY_RELATED edges are nondeterministic across identical runs (~50% churn)

Open DeusData opened this issue 4 months ago • 2 comments

Three identical full-mode indexes of the same flask checkout with the same binary produced SEMANTICALLY_RELATED counts of 74 / 59 / 126, with only ~50% of the edge set shared between runs (HANDLES: 214/213/214 = fine; SIMILAR_TO: 18/18/18 byte-stable). On django: 79 vs 58 across runs.

The comment at src/pipeline/pass_semantic_edges.c:74 shows one worker-scheduling dependence was already fixed ("per-node max_edges budget no longer depends on worker scheduling") — but the remaining selection still varies with scheduling order, most plausibly the per-node edge budget consuming LSH candidates in completion order.

Impact assessment: absolute scale is tiny (~0.02% of the graph; advisory edge class), so not release-blocking — but it violates the deterministic-output property the indexer otherwise holds (MT indexing is byte-identical since the kernel-perf work), makes A/B comparisons noisy, and churns incremental artifacts.

Fix direction: make candidate ordering deterministic before applying the per-node budget (sort candidates by (score, qn) instead of arrival order), same recipe as the earlier fix at :74.

DeusData avatar Jul 10 '26 11:07 DeusData

Hey, I'd like to work on this one! The fix looks straightforward: in cmp_deferred_edge_canonical (pass_semantic_edges.c), the current sort key (i, c) uses the candidate's LSH arrival rank, which can vary with worker scheduling. Replacing it with (i, score desc, j) makes the per-node budget always admit the strongest edges first, with the partner func index (already sorted by qn in phase 1a) as a stable tie-break. PR coming shortly!

harshitaajoshi avatar Jul 10 '26 20:07 harshitaajoshi

Re-measured on current main (db330bf) and this no longer reproduces — closing as fixed by intervening merges.

Evidence: six full-mode indexes of the same flask checkout (fresh store each run): three back-to-back runs plus CBM_WORKERS=2/8/16 stress variants. All six produce a byte-identical SEMANTICALLY_RELATED edge set — 129 edges, identical SHA over the sorted source_qn|target_qn fingerprint — versus the 74/59/126 churn measured when this was filed.

Two findings from the investigation worth recording:

  1. The original measurements were taken on a pre-#1036-era tree; the deferred-admission rework referenced in the pass_semantic_edges.c header comment (which removed the per-node budget's dependence on worker-completion order) appears to be what fixed it.
  2. A hypothesis worth ruling out for posterity: duplicate qualified names among Function/Method nodes (e.g. @property/@setter pairs) making the (qualified_name, id) sort fall through to the run-dependent node id. Refuted empirically — the graph buffer upserts by QN, so property/setter pairs merge into a single node; no duplicate Function/Method QNs exist at the store level.

If anyone observes churn again on ≥ v0.9.1, please reopen with the fingerprint procedure above (it's cheap and decisive).

DeusData avatar Jul 12 '26 18:07 DeusData