Skip to content

Fix collapsed import self-loops - #2038

Closed
Endogen wants to merge 1 commit into
Graphify-Labs:v8from
Endogen:fix/import-self-loops
Closed

Endogen wants to merge 1 commit into
Graphify-Labs:v8from
Endogen:fix/import-self-loops

Conversation

@Endogen

@Endogen Endogen commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • drop file-level imports, imports_from, and re_exports edges when endpoint resolution collapses them into self-loops
  • cover Python stdlib/package basename collisions and Rust parent/external module cases
  • verify that meaningful recursive calls self-loops remain intact

Fixes #2037.

Root cause

AST extraction can emit unresolved bare import targets such as builtins, contracting, or poseidon. The legacy-ID alias index in build_from_json() may then resolve that bare target to the importing file's own canonical node. Materializing the resolved edge creates a file-level self-loop that carries no connectivity and is flagged by graphify diagnose multigraph.

The filter is intentionally relation-specific. It removes only collapsed import/re-export edges and preserves other self-edges, including recursive calls.

Impact

Rebuilt graphs no longer contain false import self-loops caused by basename/legacy-alias resolution. This eliminates diagnostic noise without changing application code or hiding real recursive program structure.

Validation

  • uv run pytest tests/ -q — 3,478 passed, 3 skipped
  • uv run ruff check . — passed
  • focused build/extraction suite — 148 passed
  • rebuilt two affected real-world graphs — zero self-loops, dangling endpoints, missing endpoints, or endpoint-collapse groups

@safishamsi

Copy link
Copy Markdown
Member

@safishamsi safishamsi closed this Jul 20, 2026
shivamtiwari3 added a commit to shivamtiwari3/graphify that referenced this pull request Sep 10, 2026
…abs#3350)

The direct-call path in the tree-sitter engine emitted an edge only when
`tgt_nid != caller_nid`, so a call whose callee resolved to the caller
itself was silently dropped. In the reporter's repro `entry -> factorial`
is extracted and `factorial -> factorial` is not, so the extracted call
structure does not match the source. `build_from_json` has preserved a
supplied recursive `calls` self-edge since Graphify-Labs#2038 — only extraction was
withholding it.

There is exactly one way a name that resolves to the caller is not
recursion: a local binding or parameter of the same name shadows the
definition, so the call names that value rather than the enclosing
function. That case is rejected via the existing `local_bound_names` /
`extra_locals` shadow tables, and rejected *at the emit site* rather than
by clearing `tgt_nid` — clearing it would push the name into the
cross-file raw_call queue, where it could bind to an unrelated same-named
definition elsewhere in the corpus.

The shadow tables are populated for Python and JS/TS. For other languages
they are empty, which is the correct default: without a local binding, a
bare name resolving to the enclosing function can only be recursion.

The pair-dedup guard is unchanged, so a function with several recursive
call sites still yields one edge.
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.

Bare import aliases can collapse into file-level self-loops

2 participants