Skip to content

fix(dedup): keep an AST definer's id when another file's node also defines it (#4859) - #4872

Closed
TanmayShah29 wants to merge 1 commit into
Graphify-Labs:v8from
TanmayShah29:fix/4859-ast-definer-wins
Closed

TanmayShah29 wants to merge 1 commit into
Graphify-Labs:v8from
TanmayShah29:fix/4859-ast-definer-wins

Conversation

@TanmayShah29

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #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 came down to label length. A short doc label kept the id, the code node took pkg.md as its source_file, and the next re-extract of pkg.md deleted 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_rank ranks 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 in build.py, because fresh semantic chunks arrive unstamped.

Unchanged:

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: extracted fact is kept separate from inference. A code node keeps its id and source_file against 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 _origin from 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).

# final tests, v8's dedup.py
pytest tests/test_dedup.py            -> 4 failed, 149 passed
  failed: two short-label cases, test_doc_reextract_keeps_ast_node_with_shared_id
  (assert 'pkg.md' == 'pkg/test_x.py'), test_cross_file_doc_loses_but_same_file_pair_keeps_its_label
# with the fix
pytest tests/test_dedup.py            -> 153 passed
pytest tests (two parts)              -> 8495 passed, 8 failed, 323 skipped
  same 8 failures with v8's dedup.py: test_security.py x7 (no DNS in the VM),
  test_install_references (build module not installed)
  tests/test_skillgen_input_path_injection.py excluded: does not finish, with or without the patch
ruff check .                          -> All checks passed!
pyright graphify/dedup.py tests/test_dedup.py -> same 6 errors before and after

Not run: whole-project pyright on the final commit, --all-extras, other platforms.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no fragments touched.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

…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]>
@github-actions

Copy link
Copy Markdown

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.

@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 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 — 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

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).

@safishamsi

Copy link
Copy Markdown
Member

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.

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

Labels

None yet

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

2 participants