graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Class/type references across files never rewire to canonical definition — #1781 fix excluded "Types" by design

Open ivenpalma opened this issue 3 months ago • 3 comments

Summary

Per the v0.9.13 changelog, #1781 fixed cross-file function references dangling on sourceless name-only stubs instead of resolving to the real definition — but explicitly excluded types: "Top-level function defs are now eligible rewire targets... Types are unchanged."

This means a class referenced across multiple files still fragments into N disconnected nodes (one per referencing file, each with no source location) instead of resolving to the one canonical definition. Reporting with a concrete repro in case this scope exclusion wasn't intentional long-term, or to make it discoverable for others hitting the same thing.

Steps to reproduce

  1. Python corpus (~93 files), a class StateFrame defined once in types.py and imported/used in session.py, state.py, store.py, actos.py, fases.py, safety.py, validator.py.
  2. Clean extract: graphify extract <dir> --backend claude-cli --mode deep
  3. graphify query "StateFrame" --graph graph.json

Actual behavior

9 nodes labeled StateFrame, 8 with empty source:

nevi_v3_py_types_stateframe source: nevi-v3/py/types.py L1034 degree: 127 (canonical, has real edges) nevi_v3_py_session_py_stateframe source: (empty) degree: 51 nevi_v3_py_state_py_stateframe source: (empty) degree: 40 nevi_v3_py_actos_py_stateframe source: (empty) degree: 9 nevi_v3_py_store_py_stateframe source: (empty) degree: 6 nevi_v3_py_validator_py_stateframe source: (empty) degree: 6 nevi_v3_py_fases_py_stateframe source: (empty) degree: 4 nevi_v3_py_safety_py_stateframe source: (empty) degree: 1 nevi_v3_src_types_stateframe source: nevi-v3/src/types.ts L51 degree: 9 (unrelated TS file, correctly separate)

The 7 Python-side sourceless nodes carry almost the same underlying references (functions using StateFrame) already present on the canonical node's edge list, just re-scoped per file and disconnected from the real definition. Consequences:

  • explain <canonical_id> under-reports true fan-in.
  • god-node ranking in GRAPH_REPORT.md is unreliable for any class used in 3+ files.
  • community detection scatters the fragments across unrelated communities, producing spurious "this class bridges 20 communities" signals that are actually just its own split.

What I tried

--dedup-llm doesn't help without AWS Bedrock creds configured (AWS_PROFILE/AWS_REGION not set → silently no-ops, logs "Deduplicated 1 node(s)" with no indication the type-fragment case wasn't attempted). Confirmed with --backend claude-cli throughout, not an AWS-only backend issue.

Ask

Given #1781 already built the rewire-target machinery for functions (gated by unique-label + language-family match, excluding supertype edges), extending eligibility to type/class definitions seems like the natural next step rather than a new mechanism — unless there's a reason types were deliberately left out (e.g. avoiding collisions with inheritance edges) that's worth documenting in the README's "ghost duplicates" section, since right now it reads as if all same-symbol duplicates are handled.

Environment

  • graphifyy via uv tool install, --backend claude-cli --mode deep, GRAPHIFY_MAX_OUTPUT_TOKENS=32768
  • Windows 11, PowerShell
  • 93 code + 28 doc files, ~848k input tokens per full extract

ivenpalma avatar Sep 01 '26 02:09 ivenpalma

Confirming this outside Python — same behaviour from the C++ extractor on 0.9.53, so the Types exclusion isn't language-specific. I also have a proposed fix for the part of this that survives whichever way the Types question goes.

Proposal: relativize origin_file instead of dropping it.

However the Types exclusion is resolved, some stubs will always survive — a genuinely external base class has no definition to rewire onto. Those ship with no provenance at all, because extract.py pops the one field that knows where they came from:

for n in all_nodes:
    n.pop("origin_file", None)

The comment there is right that an absolute machine-specific path must not ship. But source_file faces the same constraint a few lines earlier and is relativized against the root rather than dropped. Doing the same for origin_file would let a surviving stub report the file that referenced it, so explain can say "referenced from src/user.cpp" instead of showing a candidate with an empty source. It changes no resolution behaviour, adds no absolute paths, and is independent of the rewire decision.

Evidence it's not Python-specific:

src/alpha.h    class Widget { public: int value() const; };
src/beta.h     class Widget { public: int other() const; };
src/user.cpp   class Consumer : public Widget { public: int run() const; };

graphify extract src --out out gives:

alpha_widget  Widget  source_file='alpha.h'
beta_widget   Widget  source_file='beta.h'
widget        Widget  source_file=''      <-- stub
user_consumer -> widget  [inherits]

Control: delete beta.h and re-extract. The label is unique, the stub is rewired, the edge becomes user_consumer -> alpha_widget, and there are zero sourceless nodes. So the rewire machinery works; only the ambiguous case falls through. On a large C++ corpus this leaves 16,510 sourceless stubs against 77,291 C++ nodes with a real source_file — 17.6% of the subgraph.

One more: #3133 traced the per-file salting mechanism in detail, but was self-closed as "already handled in some prior issue" with no issue named, and it still reproduces on 0.9.53. Might be worth reopening or folding into this issue.

mallyskies avatar Sep 04 '26 23:09 mallyskies

This issue describes a case I can reproduce consistently, and which still reproduces on the latest published release. Posting the minimal repro in case it is useful for validating the fix.

Minimal reproduction

Fixture (2 files):

package/
  __init__.py    0 bytes
  views.py       9 lines

views.py:

from typing import Any
from django.contrib.auth.models import AbstractUser


def detail(request: Any, user: AbstractUser) -> Any:
    return user

Both referenced types are defined outside the scanned corpus: typing.Any and django.contrib.auth.models.AbstractUser.

Command:

graphify extract ./fixture --out ./out --code-only

Result

The type references are emitted as code nodes with no provenance at all:

id=abstractuser   file_type=code  _origin=ast  source_file=""  source_location=""
id=any            file_type=code  _origin=ast  source_file=""  source_location=""

Empty source_file, empty source_location, and no origin_file either — there is no anchor left to resolve.

The interesting part is visible in the same graph. The modules are recorded correctly as external concepts:

id=typing                     file_type=concept  type=external
id=django_contrib_auth_models  file_type=concept  type=external

So the module is classified as an external reference, while the type it exposes is emitted as an orphan code node. That is the inconsistency described in this issue: the type reference is neither rewired to a canonical definition nor classified as an external reference.

Versions tested

version source_file="" type nodes
graphifyy 0.9.72 2
graphifyy 0.9.74 2

The two runs produce identical nodes (same ids, same order, same fields). 0.9.74, the latest published release, does not fix this.

Wider observation

The same signature is not isolated to this fixture. In a Python codebase I scanned, the identical shape (file_type=code, _origin=ast, no source_file, no source_location) appears repeatedly, with node ids matching the bare lowercased type names (abstractuser, any, etc.). The count scales with the number of cross-module type references, which is consistent with this being a systematic extraction issue rather than a one-off.

No proprietary data is included here; the fixture is synthetic and the larger observation is described only as a pattern.

Relation to #3254

PR #3254 appears related to the broader sourceless-type-stub family, but this external-definition case was not tested against that PR, so I cannot say whether it would be covered.

Environment

graphify 0.9.72 / 0.9.74
Python 3.13
Windows

geraldymakosso-cpu avatar Oct 03 '26 19:10 geraldymakosso-cpu

This looks fixed on the v8 branch by b2825e05 (fix: resolve Python type reference stubs across imports (#3252)). Could a maintainer confirm and close?

omsenjalia avatar Oct 18 '26 08:10 omsenjalia