Skip to content

fix(build): rewire doc-to-code edges whose endpoint was replaced by re-extraction (#1711) - #4604

Open
ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-1711-doc-code-edge-survival
Open

ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-1711-doc-code-edge-survival

Conversation

@ayushcodes10

Copy link
Copy Markdown
Contributor

Fixes #1711.

build_merge's and merge_raw_extraction's replace-per-source step correctly drops a re-extracted file's own stale nodes/edges, keyed on the item's OWN source_file. An edge owned by an UNCHANGED file (e.g. a doc referencing a function) survives that filter untouched, since the edge's own source_file wasn't re-extracted — but if the edge's target lives in a file that WAS re-extracted, that target node was just dropped by the same filter. Extraction is not id-stable across runs (#1695), so the re-extracted file's new node for the same symbol often gets a different id (e.g. requalified with a file-stem prefix). The surviving edge keeps naming the old id, which no longer exists.

That dangling reference then either vanishes silently in build_from_json's generic dangling-edge drop (the build_merge path) or rides along as an unresolvable raw reference (merge_raw_extraction, the --no-cluster incremental path). Because the surviving endpoint's own file was never re-extracted, nothing ever re-emits the edge against the target's new id on a later run either — the loss is permanent, not just until the next rebuild, matching what both the original report and the independent production-corpus comment on the issue describe.

Fix

Both functions now re-resolve a dropped endpoint by (source_file, label) against the fresh chunk — the same identity build_from_json's own ghost-merge already trusts for AST/LLM twin detection — and rewrite the edge onto the one fresh node that claims that pair. Matching both the raw source_file string and its _norm_source_file form, same as the rest of this replace machinery.

Conservative by construction: an edge is only rewired when EXACTLY ONE fresh node matches; zero or 2+ candidates leave it exactly as before, so it still reaches the ordinary dangling-edge drop rather than guessing. A genuinely deleted endpoint (via prune_sources) is unaffected — the new logic only ever looks at nodes dropped for being replaced, never ones dropped for being pruned — so deletion still removes the edge as before.

merge_raw_extraction's own docstring already states its replace/prune semantics "mirror build_merge exactly, so the raw and clustered incremental paths can't drift" — it has the identical bug via the identical mechanism, so this fixes both rather than just the one the issue's own repro exercises.

Out of scope, left as a documented follow-up: the issue's "residual hyperedge gap" (item 2 of its proposed fix) — a dropped hyperedge whose full member set is still resolvable in the merged graph. That's a related but separate carry-forward mechanism from ordinary edges and deserves its own focused change.

Verification

  • Reproduced the issue's own minimal repro against current v8 before the fix: a doc→code edge disappears entirely after re-extracting only the code file (0 edges after, where 1 is expected).
  • Verified via git show+file-swap (never git stash) that the pre-fix code reproduces this exactly, and that restoring the fixed file is byte-identical to the working tree.
  • Added tests/test_build_merge_edge_survival.py: the repro for both build_merge and merge_raw_extraction, an ambiguous-candidates control (left unresolved), a genuine-deletion control (still pruned), and a no-op-update control (nothing spuriously rewritten).
  • Full suite (python3 -m pytest tests/ -q) and python3 -m tools.skillgen --check both clean.

An edge owned by a file that was not freshly extracted this run
survived build_merge's and merge_raw_extraction's replace per
source filter untouched, since that filter only looks at an edge's
own source file. But the filter had already dropped the OTHER
endpoint's node when that endpoint's own file WAS extracted again
this run, and extraction is not id stable across runs, so the
surviving edge kept naming an id that no longer existed under that
spelling even though the symbol itself was still there. The
dangling reference then either vanished silently in the generic
drop that follows (build_merge) or rode along as an unresolvable
raw reference (merge_raw_extraction). An unchanged doc citing a
function that got requalified this run lost that citation
permanently, since the doc is never extracted again on its own to
emit it a second time.

Both functions now resolve such an endpoint again by its source
file and label against the fresh chunk, the same identity
build_from_json's own ghost merge already trusts for AST and LLM
twin detection, and rewrite the edge onto the one fresh node that
claims that pair. Zero or two or more candidates leave the edge
exactly as it was before this change, so an ambiguous case still
reaches the ordinary dangling edge drop rather than guessing. A
genuinely deleted endpoint is unaffected: the rewiring only ever
looks at nodes dropped for being replaced, never ones dropped for
being pruned, so deletion still removes the edge as before.

merge_raw_extraction's own docstring already promises its replace
and prune semantics mirror build_merge exactly so the raw and
clustered incremental paths cannot drift, so both needed the same
fix rather than just the one the report's own repro exercised.
Covers both merge functions against the report's own repro: a doc
edge into a code symbol survives once that symbol's own file is
extracted again under a new id. Two controls pin down the
boundaries the fix must respect: a dropped node with two equally
plausible fresh candidates stays unresolved rather than guessing,
and a genuinely deleted file still loses its edges exactly as
before, since deletion was never meant to be rescued by this. A
fifth test is a plain no op update, confirming nothing about an
untouched graph changes when nothing was extracted again at all.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @ayushcodes10. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 20, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (2 not verified).

Formal verification. PR-changed functions: 0/2 verified (0 proven, 0 may-equivalent, 0 distinguished) · 2 not verified (1 vacuous, 1 unsupported).

Not verified on this run: build\_merge (vacuous: never exercised), merge\_raw\_extraction (unsupported).


Graphify review — findings

Fixes incremental merges losing edges from unchanged files into a re-extracted file's nodes when extraction mints a different id for the same symbol (#1711). A new _rewire_edges_through_replaced_nodes re-points each surviving edge endpoint onto the fresh node that shares its (source_file, label), trying the raw path first and then the normalised one, and runs in both build_merge and merge_raw_extraction. It only rewrites when exactly one fresh node claims that pair, so endpoints that are ambiguous, unmatched or genuinely pruned fall through to the ordinary dangling-edge drop as before, but the label-keyed match means distinct old nodes sharing a (source_file, label) get collapsed onto one fresh node. Splits the new _replaced check out of _dropped so rewiring applies only to nodes superseded by their own tier's re-extraction, never to deletion or exclusion prunes, and prints a stderr count of rewired edges. The new tests cover rewiring in both merge paths, the unchanged-edge no-op, a genuinely deleted endpoint still pruned, and an ambiguous replacement left to drop.

Worth a look

  • Rewire collapses distinct old nodes sharing (source_file, label) onto one fresh node — graphify/build.py:2179 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • Rewiring crashes with TypeError on unhashable edge endpoints or node ids — graphify/build.py:2183 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 1782 functions depend on the 120 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: build_merge() — 106 callers, 17 callees
  • worse: to_json() — 85 callers, 11 callees
  • worse: build() — 84 callers, 6 callees
  • worse: merge_raw_extraction() — 11 callers, 12 callees

Verification — 1782 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1212 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

129 of 437 test file(s) selected (30%) via static blast radius.

  • tests/test_alias_folding.py — impact
  • tests/test_analyze.py — impact
  • tests/test_atomic_canvas_export.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_backend_opt_in.py — impact
  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_benchmark_real_query.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_edge_survival.py — impact, changed-test
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cluster.py — impact
  • tests/test_community_labels_skill.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_confidence.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_dedup.py — impact
  • tests/test_dedup_remaps_hyperedges.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_dedup_survivor_richness.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_export.py — impact
  • tests/test_export_control_characters.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_export_idempotent_writes.py — impact
  • tests/test_export_path_length.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_external_stub_endpoints.py — impact
  • tests/test_extract.py — impact
  • tests/test_falkordb_integration.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_god_nodes_exclude_hubs.py — impact
  • … and 79 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify build\_merge.

The verifier did not have enough to check build\_merge, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 224 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify merge\_raw\_extraction.

The verifier did not have enough to check merge\_raw\_extraction, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `prune_sources` is annotated `'list[str] | None'` — outside the synthesizable primitive/collection set

· 1 grounded finding(s) anchored inline below; 3 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/build.py
return rescued


def merge_raw_extraction(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — merge_raw_extraction()

fans out to 12 callees (efferent coupling); 11 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

1 participant