Skip to content

Fix: prevent cross-file member-call name collisions in extract resolver - #599

Merged
safishamsi merged 1 commit into
Graphify-Labs:v5from
llongo-allora:fix/member-call-resolver-collisions
May 2, 2026
Merged

safishamsi merged 1 commit into
Graphify-Labs:v5from
llongo-allora:fix/member-call-resolver-collisions

Conversation

@llongo-allora

Copy link
Copy Markdown

Fix: prevent cross-file member-call name collisions in extract resolver

What

The cross-file call resolver in graphify/extract.py matched callees
against a global lowercase-name → id map. Because every per-language
extractor strips member-expression callees (x.foo(), obj.bar(),
Pkg::baz()) down to the trailing identifier before queuing them, the
resolver would routinely link a this.logger.log(...) call to a
completely unrelated function log(...) defined in some other file in
the corpus, producing phantom god nodes with hundreds of bogus INFERRED
edges.

This PR closes the cross-file leg of the bug:

  1. A new module-level constant _MEMBER_CALL_NODE_TYPES enumerates the
    tree-sitter node types that represent member-expression callees across
    all supported languages (JS/TS, Go, Rust, Swift, Kotlin, Scala, C++,
    PHP, Java, Elixir, Zig).
  2. Every raw_calls.append(...) site in every per-language extractor now
    records callee_node_type (the AST node type of the callee).
  3. The cross-file resolver in extract() skips entries whose
    callee_node_type is in _MEMBER_CALL_NODE_TYPES, since name-only
    resolution of member calls is fundamentally unsafe without
    receiver-type information.

In-file resolution behavior is intentionally unchanged. In-file matches
require the callee to actually exist as a top-level symbol in the SAME
file, which is rarely a phantom in practice.

Why

See linked issue. On a real production codebase (~1,668 TS/JS files,
NestJS + Nuxt) this bug pulled 255 phantom INFERRED edges onto a single
1-line helper in a smoke-test script, making it the #2 god node and
distorting cohesion + suggested-questions output. After the fix:

Metric Before After
Total edges 7,603 6,341
log() god-node rank #2 (265 edges) not present
Top god node useApi() (561) useApi() (561)
INFERRED ratio (whole graph) ~75% ~25%

The ~1,260 dropped edges are predominantly cross-file member-call
phantoms. Real edges (intra-file calls, bare-identifier cross-file
calls, imports) are unaffected.

How

_MEMBER_CALL_NODE_TYPES

_MEMBER_CALL_NODE_TYPES = frozenset({
    "member_expression",       # JS / TS / Python attribute calls
    "selector_expression",     # Go
    "field_expression",        # Rust, C++, Scala
    "navigation_expression",   # Swift, Kotlin
    "qualified_identifier",    # C++ (Foo::bar())
    "scoped_identifier",       # Rust (foo::bar())
    "scoped_call_expression",  # PHP (Foo::bar())
    "member_call_expression",  # PHP ($obj->method())
    "field_access",            # Java
    "method_invocation",       # Java (when receiver-qualified)
    "dot",                     # Elixir
    "_zig_dotted_callee",      # synthetic marker for Zig
})

Tag at every raw_calls.append

Six call sites get one new field each:

raw_calls.append({
    "caller_nid": caller_nid,
    "callee": callee_name,
    "source_file": str_path,
    "source_location": f"L{node.start_point[0] + 1}",
    "callee_node_type": func_node.type if func_node is not None else None,
})

(For the Zig branch, the original callee name is already pre-split by .,
so the patch synthesises a _zig_dotted_callee marker when the source
text contained a .. For the PowerShell branch, all calls are bare
command names and get the literal "command_name". For the Elixir
branch, the surrounding loop already inspects the callee child node, so
child.type is used directly.)

Resolver guard

for rc in result.get("raw_calls", []):
    if rc.get("callee_node_type") in _MEMBER_CALL_NODE_TYPES:
        continue
    callee = rc.get("callee", "")
    ...

Tests

tests/test_member_call_resolution.py adds 3 regression tests:

  1. test_member_call_node_types_constant_present — sanity check that the
    constant exists and covers every per-language extractor.
  2. test_smoke_test_log_does_not_steal_nestjs_logger_calls — full
    end-to-end: writes a 3-file corpus that triggers the bug, calls
    extract(), asserts that NO cross-file edges land on the smoke-test
    log() node, and asserts that legitimate intra-file calls are
    preserved.
  3. test_raw_calls_emit_callee_node_type_for_member_expressions —
    sanity check that the corpus exercises the relevant code paths.

Verified that all 3 tests fail on main (4 phantom cross-file edges
detected in the test corpus) and pass with this patch.

Trade-off (acknowledged)

This is a deliberately conservative fix. Legitimate cross-file member
calls (e.g. import { service } from './x'; service.method() where
method happens to be a top-level identifier in some unrelated file)
will no longer produce edges. In practice:

  • These were always low-confidence INFERRED edges (0.8) and could already
    be wrong without anyone knowing.
  • A follow-up using the existing per-file import maps (_load_tsconfig_aliases
    etc.) could re-introduce these as high-confidence EXTRACTED edges with
    receiver-type tracking. That's a larger change and out of scope here.
  • The graph quality improvement (no more phantom god nodes) substantially
    outweighs the loss.

Files changed

  • graphify/extract.py — add _MEMBER_CALL_NODE_TYPES, tag 6
    raw_calls.append(...) sites, guard the cross-file resolver.
    Net: +25 lines.
  • tests/test_member_call_resolution.py — new file, 3 regression tests.

Backward compatibility

  • No public API changed.
  • raw_calls is an internal data structure not exposed to users.
  • Existing graphs are unaffected; only new extractions benefit.
  • For users who want the legacy behavior, a follow-up could expose a
    LanguageConfig flag — but I'd argue the legacy behavior was always a
    bug.

Closes #598

graphify per-language AST extractors strip member-expression callees
(`x.foo()`, `obj.bar()`, `Pkg::baz()`) down to the trailing identifier,
queue them in `raw_calls`, and the cross-file resolver matches by bare
lowercase name against a global symbol table. When any file in the
corpus exposes a top-level helper with a common method name (e.g. a
`function log(...)` in a one-off smoke-test script), every unrelated
`Logger.log(...)` / `this.x.find(...)` / `Pkg::baz(...)` call across the
codebase resolves to that single helper, producing phantom god nodes
with hundreds of bogus INFERRED edges.

This patch:

  * Adds `_MEMBER_CALL_NODE_TYPES` enumerating member-expression AST
    node types across all supported languages.
  * Tags every `raw_calls.append(...)` site with `callee_node_type`.
  * Skips entries whose `callee_node_type` is in
    `_MEMBER_CALL_NODE_TYPES` from the cross-file resolver in
    `extract()`. In-file resolution behaviour is intentionally
    preserved.

On a real production codebase (~1,668 TS/JS files, NestJS + Nuxt) this
eliminates 1,262 phantom edges and removes a phantom `log()` god node
that previously sat at Graphify-Labs#2 with 265 inbound edges (255 of them bogus).

Includes 3 regression tests that fail on the original code and pass with
this patch.
@safishamsi
safishamsi merged commit 832dc53 into Graphify-Labs:v5 May 2, 2026
phreakocious added a commit to phreakocious/graphify that referenced this pull request May 6, 2026
PR Graphify-Labs#599 (cherry-picked as 58199eb) added `callee_node_type: func_node.type`
to the unresolved-call payload, but only the C++ and generic branches of
walk_calls ever assign `func_node`. Swift / Kotlin / Scala / C# / PHP
extract callee_name directly without going through that variable, so the
new line raised UnboundLocalError on every call site in those languages.

Init `func_node = None` once at the top of the call-handling block so
the existing `if func_node is not None` guard at the append site works.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
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.

Phantom god nodes from member-call name collisions in cross-file resolver

2 participants