Skip to content

[Bug]: ambiguous_python_import_* node ids depend on the absolute checkout path, so the same commit in two directories gives a different graph.json (and different community counts) #5044

Closed

Description

@AhowCsharp

Pre-flight checks

  • I have checked the Troubleshooting section in the README

What happened?

_suppress_ambiguous_python_imports gives an unresolved Python import a placeholder target id (extract.py:435):

identity = "\0".join((
    str(module_name), str(edge.get("source_file", "")),
    str(edge.get("source_location", "")), str(edge.get("source", "")),
))
target = _make_id(f"ambiguous_python_import_{hashlib.sha1(identity.encode()).hexdigest()[:12]}")

All four components look checkout-independent, and on the persisted edge they are — source_file comes out repo-relative (ci/autofix/collect_500.py), source_location is L28, source is the importer's node id. But the resulting id is in practice a function of the absolute path of the project directory: build the same tree in two different directories and you get different node ids. So graph.json is not reproducible across checkouts or machines.

I could not tell from the output which component carries the absolute path, because when I diffed the two builds every persisted field on those edges was byte-identical and only target differed — so whatever absolute path enters identity has already been relativised by the time the edge is written.

Why this matters. We deliberately do not commit graphify-out/ (our graph.json is 144 MB, over our host's 100 MiB per-file push limit) and instead rely on "same commit -> same graph" so two engineers get the same query / path / affected answers. That property otherwise holds exactly: in our repo (60,974 nodes / 242,222 edges, 0.9.103, PYTHONHASHSEED=0), two clean worktrees of one commit produce identical node-set and edge-set sha256 once these placeholders are excluded. These 4 nodes are the only thing that differs between the two builds, and they cost us two things:

  1. graph.json is not byte-reproducible across checkouts, so a plain fingerprint comparison between two machines raises a false alarm. We had to teach our tooling to exclude ambiguous_python_import_* before hashing.
  2. They perturb community detection, because node ids feed the clustering order. Same commit, two clean checkouts: 1,188 vs 1,186 communities with the networkx Louvain fallback. This is not fixable by installing the native backend — I installed graspologic-native==1.3.1 and checked: Rust Leiden is perfectly reproducible (3 separate processes, identical partition hash, and it does not even care about PYTHONHASHSEED), but substituting just these 4 placeholder ids with the values from another checkout still moves the partition, from 253 communities to 251. The input differs, so no clusterer can help.

Suggested fix. Hash only checkout-independent material. The three persisted values — repo-relative source_file, source_location, and the importer's source node id — are already identical across checkouts and already unique per unresolved import site, so they look sufficient on their own to keep the placeholder distinct within a graph. Dropping whatever absolute-path-derived part is currently in identity would make graph.json byte-reproducible across checkouts, and would make community ids comparable between two machines on the same commit.

Steps to reproduce

# Minimal: 3 files, two directories, identical content and identical relative structure.
mkdir -p seed/app seed/one seed/two
cat > seed/app/main.py <<'PY'
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "one"))
from helper import f  # noqa: E402


def run():
    return f()
PY
printf 'def f():\n    return 1\n' > seed/one/helper.py
printf 'def f():\n    return 2\n' > seed/two/helper.py

# same content, different directory name
for d in checkout-aaa checkout-bbbbbbbb; do
  cp -R seed "$d"
  (cd "$d" && PYTHONHASHSEED=0 graphify update . >/dev/null)
done

# same BASENAME, different parent directory
mkdir -p nested && cp -R seed nested/checkout-aaa
(cd nested/checkout-aaa && PYTHONHASHSEED=0 graphify update . >/dev/null)

for d in checkout-aaa checkout-bbbbbbbb nested/checkout-aaa; do
  printf '%-22s ' "$d"
  python3 -c "import json;g=json.load(open('$d/graphify-out/graph.json'));print([n['id'] for n in g['nodes'] if str(n['id']).startswith('ambiguous_python_import_')])"
done

# control: rebuild one of them in place -- the id does not change
(cd checkout-aaa && rm -rf graphify-out && PYTHONHASHSEED=0 graphify update . >/dev/null)

Error output or graph output

checkout-aaa           ['ambiguous_python_import_31016ffddee6']
checkout-bbbbbbbb      ['ambiguous_python_import_949996e0edbb']   <- different directory name
nested/checkout-aaa    ['ambiguous_python_import_b3abb0f04efd']   <- SAME basename, different parent

control, checkout-aaa rebuilt in place:
checkout-aaa           ['ambiguous_python_import_31016ffddee6']   <- unchanged

So it is the whole absolute path that matters, not just the directory name, and the id is
stable per path -- this is path dependence, not randomness. PYTHONHASHSEED=0 throughout.

Running the same tree again under a different scratch parent gave yet another id for the very
same `checkout-aaa` basename (ambiguous_python_import_e437232230d3), which is the same effect.

In our real repo, two clean `git worktree` checkouts of one commit:
  node sha256  ea840b159809831f  ==  ea840b159809831f      (placeholders excluded)
  edge sha256  f27b75536717317e  ==  f27b75536717317e      (placeholders excluded)
  placeholders ['..._14bcf869d13d', '..._1dd121162efc',    vs  ['..._1247628ec504', '..._3d05567fa645',
                '..._5d22a5b19c0a', '..._d418c5510cfb']         '..._e466203ca3ef', '..._ebe44d9a63b6']
  communities  1188                vs  1186

Graphify version

0.9.103

Operating System

macOS

Activity

  1. github-actions commented on Nov 6, 2026

    @github-actions

    Thanks for opening this issue, @AhowCsharp. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Nov 8, 2026
    444c164
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