Skip to content

fix(extract): keep a symbol that spells a sibling's file id (#4449) - #4452

Closed
Deepjyoti-Sarmah wants to merge 1 commit into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4449-ext-tagged-id-clash
Closed

Deepjyoti-Sarmah wants to merge 1 commit into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4449-ext-tagged-id-clash

Conversation

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4449.

When a JS file and a file with another extension share a stem in one folder (x.mjs plus x.sql) and the .mjs declares a variable named like the sibling's extension (const sql = ...), the .mjs file node ends up labelled sql. graphify explain "x.mjs" finds nothing.

extract() renames each file's old file-level id (its path with the extension: esquema_x_sql for esquema/x.sql) to the canonical one (esquema_x). It did that on every node, edge endpoint and raw call that spells the string, whichever file it belonged to. The JS variable sql is minted {stem}_{name}, which is also esquema_x_sql. So it was renamed too, salted next to the file node, and dedup dropped one of the two.

This PR renames a key only for the file that minted it, and only when another file's own symbol carries the same id. Every other key renames exactly as before.

 extract.py, the id_remap pass          (key esquema_x_sql -> esquema_x, minted by x.sql)

   node   sql           (x.mjs)  id esquema_x_sql
-    renamed to esquema_x         (its file did not mint the key)
+    kept as esquema_x_sql

   edge   contains  x.mjs -> sql        endpoint esquema_x_sql, source_file x.mjs
-    renamed: now x.mjs -> x.mjs, a self-loop
+    kept: still points at the sql variable

   edge   imports_from  x.mjs -> x.sql  endpoint esquema_x_sql, target_file x.sql
     renamed (x.sql minted the key), then salted onto the x.sql file node

   edge   imports  main.pas -> util.pas   (no target_file, main.pas holds no such node)
     renamed, as before

A key is contested only when a node of a different file carries it as one of that file's own symbol ids. For a contested key, an item is renamed if its file minted the key, kept if its file holds a node with that id, and renamed otherwise. Which file an item belongs to: a node, its source_file; an edge source, the edge's source_file; an edge target, the target_file stamp if there is one, else the edge's source_file; a raw_calls caller_nid, the record's source_file. Paths are compared resolved. Swift extension nids and Objective-C field-table keys follow the same rule.

The later ext_id_remap pass already has an ownership guard (owned_ids, from #2231 and #2243). The first pass lacked it.

Before / after

The issue's repro, graphify extract . --code-only:

before:  [graphify] note: node 'esquema_x_mjs_esquema_x' was extracted twice from 'esquema/x.mjs'
         under different labels (keeping 'sql', dropping 'x.mjs').
         5 nodes, 4 edges
         esquema_x_mjs_esquema_x | sql          <- should be the file node "x.mjs"
         esquema_x_prueba        | prueba()
         esquema_x_sql_esquema_x | x.sql
         esquema_x_t             | t

after:   no note.   6 nodes, 5 edges
         esquema_x_mjs_esquema_x | x.mjs
         esquema_x_sql           | sql
         esquema_x_prueba        | prueba()
         esquema_x_sql_esquema_x | x.sql
         esquema_x_t             | t

Tests, tests/test_ext_tagged_id_clash.py:

before fix:  8 failed, 6 passed   (the 6 are guards that must not move)
after fix:   14 passed

The same cause hit other pairs. Each is a test:

  • x.ts + x.css (export const css = ...): css took the file node's id, and nothing was salted because x.css has no nodes.
  • x.js + x.js.map (const js_map): the variable was renamed to the id of x.js.map, a node that does not exist.
  • x.py + x.sql (def sql()): the function was lost and its calls edge started at the file node.
  • x.swift + x.sql with struct sql and extension sql: without the Swift hunk the fix would delete both file nodes, so the hunk is required, and a test covers it.

Merge danger

Door: two-way. No stored format changes. Reverting is one commit.

Blast radius: narrow. The rename changes only for a key that a node of another file also carries as its own symbol id; every other key takes the old path.

Not covered

  • The Objective-C hunk has no test that reaches it. I could not build a case where an Objective-C class node holds a contested id. It runs only when some key is contested (if contested:), and the 5 existing objc tests pass with it.
  • Unstamped cross-file imports to a same-stem file (for example Pascal uses util next to util.js) already end on a node that does not exist after salting, with or without this bug. A contested key now behaves like an uncontested one there. That dangling edge is not repaired.
  • A symbol named like its own file's extension (const js in x.js, def py() in z.py) is lost. The same file owns the key, so this rename fix does not touch it. It needs its own issue.
  • Stub nodes: I scanned graphify's own repo and its test fixtures for a node whose file did not mint the key. The one hit is sample in tests/fixtures/sample.lpk, a Lazarus unit reference minted with sample.pas's old id. Before this fix it collided with the lpk's own file node; now it keeps its own id and its edge follows. Other languages and extractors were not scanned.
  • Through graphify extract ., only siblings of a supported type (such as .sql) trigger the bug. collect_files skips .css and .map, so those two pairs need an explicit extract([...]) call.

Type of change

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

Verification & Invariants

Invariant: a symbol is never renamed because of a file it does not belong to, and everything that refers to a file node still ends on that file node.

  • The issue repro runs with relative and absolute paths: three distinct ids for the file node, the variable and the x.sql file node.

  • After build(), the labels x.mjs, sql and x.sql all exist.

  • With import raw from "./x.sql" in x.mjs: imports_from ends on the x.sql file node, contains and calls end on the variable, and there is no self-loop.

  • An unstamped Pascal import on a contested key ends exactly where the same edge ends with no clashing variable.

  • Controls from the issue (const db, const sql = 1, no sibling, a sibling with another stem), and a Lazarus stub from a different stem, keep their ids.

  • Gating only the node rename, and leaving edge endpoints on the old id-only rule, was tried and does not work: it leaves a contains x.mjs -> x.mjs self-loop and a calls edge that starts at the file node.

  • 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?

Run with PYTHONPATH=/var/tmp/wt-4449 and the repo venv's Python, so the code under test is this branch and not an editable install of another checkout. graphify.__file__ was checked to be inside the worktree.

# the issue repro (in /var/tmp/scratch-4449/repro2)
python -m graphify extract . --code-only
  before: 5 nodes, 4 edges, "extracted twice ... keeping 'sql', dropping 'x.mjs'"
  after:  6 nodes, 5 edges, no note

python -m pytest tests/test_ext_tagged_id_clash.py -q --basetemp=/var/tmp/pt-4449 -p no:cacheprovider
  before fix: 8 failed, 6 passed, 12 warnings in 1.61s
  after fix:  14 passed, 12 warnings in 2.31s

python -m pytest tests/test_swift_cross_file_calls.py tests/test_objc_field_table_remap.py -q --basetemp=/var/tmp/pt-4449b -p no:cacheprovider
  22 passed, 21 warnings in 1.55s

python -m pytest <the 18 covering files> -q --basetemp=/var/tmp/pt-4449c -p no:cacheprovider
  1183 passed, 295 warnings in 38.41s
  (test_extract, test_incremental, test_js_import_resolution, test_node_id_canonical,
   test_file_node_id_spec, test_astro_import_ids, test_extract_path_memo,
   test_cross_extension_reexport_self_cycle, test_id_normalization_contract,
   test_objc_field_table_remap, test_dotnet, test_languages, test_incomplete_build_guard,
   test_extraction_spec_ids, test_jsconfig_baseurl, test_package_json_subpath_imports,
   test_cjs_module_extension, test_typescript_module_extensions)

python -m pytest -q --basetemp=/var/tmp/pt-4449-full -p no:cacheprovider
  8011 passed, 14 skipped, 2055 warnings in 541.55s (0:09:01)

ruff check graphify/extract.py tests/test_ext_tagged_id_clash.py
  All checks passed!
pyright graphify/extract.py
  131 errors, same as upstream/v8 (041771be); tests/test_ext_tagged_id_clash.py: 0

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No fragments changed.)
  • 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.

…s own node (Graphify-Labs#4449)

extract() renames each file's old file-level id (its path with the extension, esquema_x_sql for x.sql) to the canonical one, on every node, edge endpoint and raw call that spells it. A symbol of another file can spell it too: const sql in x.mjs, so it took the file node's id and was dropped by dedup.
The rename now follows only the file that minted the key, wherever another file's own symbol carries the same id; a file that only refers to the owner renames as before, so every key nobody contests behaves exactly as today.
Swift extension nids and Objective-C field-table keys follow the same rule; the Objective-C path has no test that reaches it.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Deepjyoti-Sarmah. 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 14, 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 sampled, not proven).

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


Graphify review — findings

Fixes extract() renaming a symbol to a same-stem sibling's file id, so const sql in x.mjs no longer takes the id of x.sql and the x.mjs file node keeps its own label. Each old file-level id is now tied to the input files that minted it, and a key that another file's own symbol also spells is treated as contested. A contested key is renamed only on the minting file's items and on items that merely refer to it, across nodes, edges, raw calls, Swift extensions and ObjC field tables. Uncontested keys rename exactly as before, but the contested-key scan resolves every node's path and the ObjC remap is rebuilt per file when any key is contested, so both add cost on large inputs.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3026 functions depend on the 347 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 957 callers, 58 callees
  • worse: extract_js() — 93 callers, 7 callees
  • new: _run() — 11 callers, 3 callees

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

Test selection

Test selection

193 of 419 test file(s) selected (46%) via static blast radius.

  • tests/test_affected_overrides.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_foreach_loop_variable.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_underscore_collisions.py — impact
  • tests/test_csharp_var_call_return_type.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_defdelegate.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_keyword_def_calls.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_erlang_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact, changed-test
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_path_memo.py — impact
  • … and 143 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.

Docs that may be stale (advisory)

Formal verification

No difference found (not proven): No behavior difference found in extract (not a proof).

The verifier ran both versions of extract on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

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

return path


def _run(root: Path, files: dict[str, str], monkeypatch, *, absolute: bool = False) -> dict:

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 — _run()

11 callers depend on it (afferent coupling).

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

safishamsi added a commit that referenced this pull request Oct 14, 2026
Eight community PRs: Windows MCP-stdio hang (#4441), Rust use->declaration
(#4443), JS/TS file-scope callback param shadowing (#4457), sibling-file-id
symbol retention (#4452), TS class-field receiver scoping (#4297), Ollama
/api/chat num_ctx (#4414), cluster label retention on majority overlap (#4451),
and an ingest symlink test fix (#4442). AST cache schema bumped to 16.

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

Copy link
Copy Markdown
Member

Landed in 0.9.91 as c29950f (cherry-picked with your authorship preserved, so it shows on your contributions). Thanks @Deepjyoti-Sarmah! Released to PyPI: https://pypi.org/project/graphifyy/0.9.91/

@safishamsi safishamsi closed this Oct 14, 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.

File node takes the label of a module-level variable when a same-stem sibling file exists (x.mjs + x.sql, const sql)

2 participants