Skip to content

fix(dedup): prefer an AST node over a semantic one in a defining collision - #4871

Closed
ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-dedup-collision-origin-preference
Closed

ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-dedup-collision-origin-preference

Conversation

@ayushcodes10

Copy link
Copy Markdown
Contributor

Fixes #4859.

Bug

When a semantic (doc) node and an AST code node share an id, and the doc's own path is a prefix of that id, _defines_id returns True for both. _collision_rank's first key (definer wins) then ties, and nothing after it spoke to tier either, so the comparison fell through "has a source_file" (both do) and the lifecycle penalty (equal) straight to label length. A doc node with a shorter label than the code node it describes could then win.

The code node then carried the doc as its source_file. On the next --update that re-extracts the doc without that node, build_merge replaces the doc's nodes and the code node is deleted outright, although the code file never changed.

Layout that triggers it: pkg.md beside pkg/test_x.py. The AST id for the file is pkg_test_x. The extraction spec asks doc subagents to reuse the AST id for a code entity they describe, so pkg.md emits pkg_test_x, and pkg_ is also a prefix of pkg.md's own path — so both sides define the id.

Reported in #4859 with a self-contained repro and a suggested fix; I independently reproduced it and verified the fix. No open or closed PR touches _collision_rank's return tuple.

Fix

Added a key to _collision_rank's tuple right after the first (definer) key: an AST-tier node now beats a semantic-tier one, read via _is_ast_tier (imported locally from graphify.build to avoid a circular import, mirroring the existing lazy-import pattern between these two modules). This mirrors the project convention that extracted fact outranks inference about it (CONTRIBUTING.md: "Keep inference visibly separate from extracted fact"). It changes no collision where the two sides already differ on the first key.

Tests

Added to tests/test_dedup.py:

Verification

  • Reproduced the bug pre-fix with the issue's own repro script (build_merge twice: once to merge, once to re-extract the doc without the node) — confirmed the code node was lost both times, and that the new regression test fails against the unmodified _collision_rank (restored byte-identical after).
  • Checked every existing _collision_rank/_defines_id test in tests/test_dedup.py: none sets _origin or an AST-shaped source_location, so the new key ties for all of them and changes no existing outcome.
  • Full suite: 2 failed, 8892 passed, 81 skipped, 1 xfailed — the 2 failures are the pre-existing flakes in test_erlang_extractor.py and the skill backtick test, unrelated to this change.
  • python3 -m tools.skillgen --check: check OK: 134 artifact(s) match committed output and expected/.

The first collision rank key only separates a definer from a mere
reference, and ties when both colliding nodes define the id, which a
doc and the code file it describes can both do for the id the
extraction spec asks the doc to reuse. Nothing after that key spoke to
tier either, so the tie fell all the way to label length, and a doc
node with a shorter label than the real AST node it describes could
outrank it. The AST node then carried the doc as its own source file,
and the doc's own next re extract, run without that node, deleted it
outright even though the code file never changed.

An AST tier node now beats a semantic tier one right after the first
key, mirroring the project convention that extracted fact outranks an
inference about it. It changes no collision where the two sides
already differ on the first key.
Covers an AST code node surviving a defining doc namesake with a
shorter label, in both arrival orders, plus a control confirming a
semantic node that only references the id still loses to a doc that
defines it regardless of tier.
@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 30, 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.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

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

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

Not verified on this run: \_collision\_rank (unsupported).


Graphify review — findings

Tightens dedup collision ranking so an AST-tier node now beats a semantic-tier one whenever both define the same id. Previously that tie fell through to label length, so a doc node with a short label (e.g. "Tests") could win and the real AST node for the entity it only describes got dropped (#4859). The new key sits right after the "defines vs. merely references" check, so collisions already decided by that first key don't change. Adds tests covering the AST-over-doc case in every arrival order, plus a control showing a semantic reference still loses to a defining doc.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 738 functions depend on the 340 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: deduplicate_entities() — 111 callers, 31 callees
  • worse: _collision_rank() — 5 callers, 4 callees

Verification — 738 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: 485 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

28 of 469 test file(s) selected (6%) via static blast radius.

  • 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_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_chunking.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_dedup.py — impact, changed-test
  • 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_ext_tagged_id_clash.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_issue_3472_source_file_collision.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_merge_sweeps_reextract_orphans.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_non_string_node_ids.py — impact
  • tests/test_prune_sweeps_orphans.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_r_extractor.py — impact
  • tests/test_semantic_origin_stamp.py — impact
  • tests/test_swift_import_resolution.py — impact
  • tests/test_unverified_semantic_shrink.py — impact

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 \_collision\_rank.

The verifier did not have enough to check \_collision\_rank, 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 `mentions` is annotated `'frozenset | set'` — outside the synthesizable primitive/collection set

· 2 more finding(s) on lines outside this diff (see the check run).

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

I reproduced #4859 and ran the full suite on this branch with an end-to-end test commit: 8975 passed, 17 skipped, 1 xfailed on Linux, Python 3.13.11.

The test commit is separate on purpose. It checks the failure path the current tests do not, and you can cherry-pick or drop it without changing your fix:

Deepjyoti-Sarmah@bd687626
https://github.com/Deepjyoti-Sarmah/graphify/commit/bd687626.patch

It is tests only, no production code. The branch is test/4871-4859-roundtrip on my fork. Cherry-picking bd687626 keeps the original authorship.

What it adds: the current tests check deduplicate_entities, but the reported loss happens in build_merge. It checks doc labels Tests, test_x.py, and test_x.py (tests), both insertion orders, and the full round trip. After the doc is re-extracted without the node, pkg_test_x must still exist with source_file=pkg/test_x.py.

One thing to decide before merge. The new tier key is unconditional, so it also changes AST-versus-semantic collisions where neither path defines the id:

AST: id=shared, source=src/code.py, label="code thing"
doc: id=shared, source=docs/doc.md, label="x"

Neither path defines shared. On v8 the doc wins because x is shorter. On this branch the AST wins because of the tier key.

If that broader change is intended, please add a test for it. If not, gate the key to the both-definers case:

defines = _defines_id(node)
...
(0 if _is_ast_tier(node) else 1) if defines else 0,

Either way, the fix itself is correct.

safishamsi added a commit that referenced this pull request Oct 30, 2026
… js imports, extractors, serve/export/pdf)

13 PRs folded into the unreleased 0.9.98; dups #4869/#4852/#4871 to close
at release. One combined _AST_CACHE_SCHEMA bump (29->30) covers the
output-changing extractors and the file/declaration id change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

The AST-wins-defining-collision fix shipped in v0.9.98 via #4872, the surgical version that preserves same-file enrichment and extension/slug twins. Closing this as a duplicate of the same #4859 work; thanks @ayushcodes10.

@safishamsi safishamsi closed this Oct 30, 2026
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

Development

Successfully merging this pull request may close these issues.

ID collision: a doc whose path prefixes a code node's id outranks the AST node on label length, and its next re-extract deletes the code node

3 participants