Repository navigation
fix(dedup): prefer an AST node over a semantic one in a defining collision - #4871
ayushcodes10 wants to merge 2 commits into
Conversation
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.
|
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. |
There was a problem hiding this comment.
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— impacttests/test_build_located_semantic_identity.py— impacttests/test_build_merge_dedup_scope.py— impacttests/test_build_merge_hyperedges_and_prune.py— impacttests/test_build_merge_shrink_guard.py— impacttests/test_c_include_resolution.py— impacttests/test_carried_hyperedge_remap.py— impacttests/test_chunking.py— impacttests/test_corrupt_graph_json.py— impacttests/test_cross_extension_reexport_self_cycle.py— impacttests/test_dedup.py— impact, changed-testtests/test_dedup_remaps_hyperedges.py— impacttests/test_dedup_shrink_refuses_force_write.py— impacttests/test_dedup_survivor_richness.py— impacttests/test_ext_tagged_id_clash.py— impacttests/test_global_graph.py— impacttests/test_go_qualified_resolution.py— impacttests/test_issue_3472_source_file_collision.py— impacttests/test_js_member_collisions.py— impacttests/test_merge_sweeps_reextract_orphans.py— impacttests/test_no_dedup_flag.py— impacttests/test_non_string_node_ids.py— impacttests/test_prune_sweeps_orphans.py— impacttests/test_python_underscore_resolution.py— impacttests/test_r_extractor.py— impacttests/test_semantic_origin_stamp.py— impacttests/test_swift_import_resolution.py— impacttests/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).
|
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 It is tests only, no production code. The branch is What it adds: the current tests check 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: Neither path defines 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. |
… 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]>
|
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. |
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_idreturns 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--updatethat re-extracts the doc without that node,build_mergereplaces the doc's nodes and the code node is deleted outright, although the code file never changed.Layout that triggers it:
pkg.mdbesidepkg/test_x.py. The AST id for the file ispkg_test_x. The extraction spec asks doc subagents to reuse the AST id for a code entity they describe, sopkg.mdemitspkg_test_x, andpkg_is also a prefix ofpkg.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 fromgraphify.buildto 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:test_ast_node_survives_a_defining_doc_namesake_order_independent: the exact scenario from 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 #4859 — an AST code node with a longer label survives a defining doc namesake with a shorter one, in both arrival orders.test_semantic_reference_still_loses_to_a_defining_doc(control): a semantic node that only references the id still loses to a doc that defines it, tier aside — confirming the new key only matters once the first key ties.Verification
build_mergetwice: 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)._collision_rank/_defines_idtest intests/test_dedup.py: none sets_originor an AST-shapedsource_location, so the new key ties for all of them and changes no existing outcome.2 failed, 8892 passed, 81 skipped, 1 xfailed— the 2 failures are the pre-existing flakes intest_erlang_extractor.pyand the skill backtick test, unrelated to this change.python3 -m tools.skillgen --check:check OK: 134 artifact(s) match committed output and expected/.