Skip to content

Bug: AST extractor collapses same-basename files into one node (file_nid built from stem only) #211

Closed

Description

@justmumu

Summary

graphify.extract assigns file-node IDs from path.stem alone. When a corpus contains multiple files with the same basename in different directories (e.g. two config.py, two __main__.py, two errors.py), all of them collapse into
a single graph node. Every top-level symbol from either physical file gets a contains edge pointing to the same node, which manufactures false cross-community bridges and inflates betweenness centrality on innocent symbols.

Reproduction

Any Python project with nested modules that reuse common filenames. In my case, the repo has:

lib/XXX/mcps/browser_mcp/config.py
lib/XXX/mcps/config_mcp/config.py
lib/XXX/mcps/browser_mcp/main.py
lib/XXX/mcps/config_mcp/main.py
lib/XXX/mcps/browser_mcp/errors.py
lib/XXX/mcps/config_mcp/errors.py

Running /graphify . produces exactly one node per basename:

from graph.json

config → source_file = lib/XXX/mcps/browser_mcp/config.py
main → source_file = lib/XXX/mcps/config_mcp/main.py
errors → source_file = lib/XXX/mcps/config_mcp/errors.py
init → source_file = lib/XXX/mcps/config_mcp/tools/init.py

The second physical file in each pair silently disappears, and all its symbols are reparented under the first file's node.

Observable symptom

In my 1,665-node graph, BrowserServerConfig (defined in browser_mcp/config.py) showed up as the node with the highest betweenness centrality (0.069) in the entire graph, appearing to bridge three unrelated communities. BFS from that
node produced:

BrowserServerConfig → config.py → load_config()
BrowserServerConfig → config.py → save_config()
BrowserServerConfig → config.py → deep_get()
BrowserServerConfig → config.py → find_by_id()

But load_config, save_config, deep_get, find_by_id live in config_mcp/config.py, which has zero real relationship to browser_mcp/config.py. The "bridge" is entirely an artifact of the file-node collision.

Root cause

graphify/extract.py around line 676:

stem = path.stem            # line 646
...
file_nid = _make_id(stem)   # line 676

The file node ID is derived from the file stem only, so any directory structure is discarded.

Proposed fix

Qualify the file ID with the relative path so siblings with the same basename get distinct IDs:

# instead of: file_nid = _make_id(stem)
rel = str(path.relative_to(root)) if root else str(path)
file_nid = _make_id(rel.replace("/", "_").replace(".", "_"))

Since contains edges and all import* helpers already reference file_nid by variable, this is a localized change — no edge-side updates needed. The stem variable can stay for label purposes (path.name is what's displayed).

Activity

  1. added a commit that references this issue on Apr 11, 2026
  2. safishamsi commented on Apr 11, 2026

    @safishamsi
    Member

    Fixed in v0.4.2. The node ID now uses the full file path instead of just the stem, so two files with the same filename in different directories no longer collide.

  3. added a commit that references this issue on Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions