Repository navigation
fix(dedup): keep an AST definer's id when another file's node also defines it (#4859) - #4872
TanmayShah29 wants to merge 1 commit into
Conversation
…fines it (Graphify-Labs#4859) When a doc's own path prefixes a code entity's id (pkg.md beside pkg/test_x.py, id pkg_test_x), both colliding nodes pass the definer key of _collision_rank and the survivor fell to label length. A terse doc label took the id, the node carried pkg.md as its source_file, and the next re-extract of pkg.md deleted it although the code file never changed. Rank a non-AST definer right behind the other definers when an AST node from a file with a different full stem also defines the id and the non-AST node's own file has no AST definer. The rule applies to any such non-AST definer, not only docs. The pairs are collected once per dedup call (_outranked_by_ast), so the rank stays a total order independent of arrival (Graphify-Labs#1851). The check is strict on _origin == "ast", like the ghost pass in build.py, because fresh semantic chunks arrive unstamped. Unchanged: collisions with at most one definer; a file's AST node and its same-file semantic enrichment (Graphify-Labs#2091), even when another file also has an AST definer; and same-stem twins (x.py/x.html, or slug twins such as pkg/service.py and pkg_service.py), where an AST twin never outranks the other. A twin can still be outranked by an AST node from a third file with a different stem, like any other definer. Fixes Graphify-Labs#4859 Co-Authored-By: Claude <[email protected]>
|
Thanks for the pull request, @TanmayShah29. 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 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: \_collision\_rank (unsupported), deduplicate\_entities (vacuous: never exercised).
Graphify review — findings
Fixes a bug where a doc node reusing a code entity's id could take over the id and delete the code node on the next re-extract. For example, pkg.md beside pkg/test_x.py, where the pkg_ prefix of the id also counts as defined by the doc's path, let the doc win on a shorter label. _collision_rank now takes an outranked set from the new _outranked_by_ast, and a non-AST definer in that set ranks right behind the other definers. A definer is only listed when an AST node from a different file defines the same id and its own file has no AST definer. Same-file AST and semantic enrichment pairs, same-stem twins (x.py/x.html, slug twins) and collisions with only one definer rank exactly as before. New tests cover order independence, the boundary cases above, and an end-to-end build_merge run that re-extracts only the 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 — 754 functions depend on the 356 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
deduplicate_entities()— 118 callers, 32 callees - worse:
build_merge()— 102 callers, 16 callees
Verification — 754 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: 501 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
Could not verify: Could not verify deduplicate\_entities.
The verifier did not have enough to check deduplicate\_entities, 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 11 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)
· 2 more finding(s) on lines outside this diff (see the check run).
|
Landed in v0.9.98, now on PyPI (pip install graphifyy==0.9.98). When a cross-file document mention and an AST node collide on a defining id, the AST node keeps the id. Thanks for the fix, @TanmayShah29. |
What does this PR do?
Fixes #4859.
When a doc's own path prefixes a code entity's id (
pkg.mdbesidepkg/test_x.py, idpkg_test_x), both colliding nodes pass the definer key of_collision_rank, and the survivor came down to label length. A short doc label kept the id, the code node tookpkg.mdas itssource_file, and the next re-extract ofpkg.mddeleted it although the code file never changed.A new helper,
_outranked_by_ast, lists the non-AST definers of an id that an AST node from a file with a different full stem also defines, when the non-AST node's own file has no AST definer._collision_rankranks those right behind the other definers. The set is built once per dedup call, so the rank stays a total order (#1851). The check is strict on_origin == "ast", like the ghost pass inbuild.py, because fresh semantic chunks arrive unstamped.Unchanged:
dedup=Truediscards a same-ID node's attributes instead of merging them — LLM semantic summaries are silently lost #2091), even when another file also has an AST definer;x.py/x.html, or slug twins such aspkg/service.pyandpkg_service.py), whether or not the twin rename pass splits them.Type of change
Verification & Invariants
Invariant: extracted fact is kept separate from inference. A code node keeps its id and
source_fileagainst another file's non-AST copy, so that copy's re-extract cannot delete it.Limitations:
A graph already damaged by this bug (code node carrying the doc as
source_file) is only repaired when the code file is re-extracted or the graph is rebuilt.A twin can still be outranked by an AST node from a third file with a different stem, like any other definer.
With x.py (AST), x.html, and a third-file definer that wins the twin pass's rank, the twin split is skipped and one twin is dropped. This is the same as on v8 and is not tested here.
The rule trusts
_origin. Nothing strips a model-supplied_originfrom semantic chunks today. That gap predates this PR and could be a separate issue.Read the CONTRIBUTING.md guide.
Reproduced the issue and identified the invariant.
Made the smallest fix necessary.
Added a regression test (if bug fix) or isolated boundary test.
Kept the PR description synchronized with the final implementation.
Documented any limitations / unsupported cases explicitly.
How was this tested?
Linux, Python 3.10,
uv pip install -e . pytest ruff tree-sitter-sql tree-sitter-hcl pyright(not--all-extras).Not run: whole-project pyright on the final commit,
--all-extras, other platforms.Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no fragments touched.)