Skip to content

Bug: Nodes and edges from deleted/moved files persist in graph — "already clean" false negative #1007

Closed

Description

@sub4biz

Summary

When files are moved out of the scan root, graphify detects them as deleted but fails to remove their nodes and edges from the graph. Both extract and update report "already clean — no matching nodes or edges in graph", yet the stale nodes remain.


Steps to Reproduce

1. Create a minimal corpus:

mkdir -p corpus/module-a corpus/module-b

corpus/module-a/auth.py:

def login(user, password):
    return validate(user, password)
def validate(user, password):
    return user == "admin"
def logout(user):
    pass

corpus/module-b/utils.py:

def format_date(date):
    return str(date)
def parse_date(s):
    return s.strip()

2. Build initial graph:

cd corpus
graphify extract .
# → graph.json: 7 nodes, 6 edges (4 from module-a, 3 from module-b)

3. Move module-b out of scan root:

mv module-b ../module-b-moved

4. Re-run extraction:

graphify extract .

5. Alternatively — same result with update:

graphify update .

Expected Behavior

After steps 4 or 5 — graph updated to 4 nodes, 4 edges, only module-a nodes remain. module-b/utils.py, format_date(), parse_date() and their edges removed.


Actual Behavior

graphify extract . output:

[graphify extract] 0 code changed; 1 unchanged; 1 deleted
[graphify] 1 source file(s) deleted since last run — no matching nodes or edges in graph, already clean.
[graphify extract] wrote graph.json: 7 nodes, 6 edges   ← unchanged

graphify update . output:

[graphify watch] No code-graph topology changes detected; outputs left untouched.

In both cases graph still contains 7 nodes, 6 edges including all 3 nodes from the deleted file:

module-b/utils.py  |  utils.py
module-b/utils.py  |  format_date()
module-b/utils.py  |  parse_date()

Root Cause Hypothesis

Likely a path normalization mismatch: the manifest stores absolute paths (e.g. D:\corpus\module-b\utils.py) while node source_file attributes store relative paths (e.g. module-b/utils.py). The pruning lookup fails to match them, reports "already clean", and skips deletion.


Impact

The only workaround is to delete both graphify-out/manifest.json and graphify-out/cache/ and rebuild from scratch. Incremental pruning is silently broken — the graph accumulates ghost nodes indefinitely with no warning to the user.


Environment

  • graphify: 0.8.17
  • OS: Windows 11
  • Python: 3.13
  • Reproduced with both graphify extract and graphify update

Related Issues

#777 confirms the root cause hypothesis: manifest.json stores absolute paths while graph.json nodes store relative paths (correctly relativized via watch._relativize_source_files). This mismatch is likely why the pruning lookup fails to match deleted files to their nodes, producing the "already clean" false negative.

Activity

  1. changed the title [-]Bug: Nodes from deleted/moved files persist in graph — "already clean" false negative[/-] [+]Bug: Nodes and edges from deleted/moved files persist in graph — "already clean" false negative[/+] on May 24, 2026
  2. sub4biz commented on May 26, 2026

    @sub4biz
    Author

    Root cause analysis (v0.8.19)

    The pruning failure has two independent layers, both in watch.py::_rebuild_code.


    Layer 1 — Call order: _relativize_source_files runs after eviction

    The existing graph is loaded at line 408, and node eviction happens at lines 418–422:

    # watch.py:418–422
    preserved_nodes = [
        n for n in existing.get("nodes", [])
        if n["id"] not in new_ast_ids
        and (not evict_sources or n.get("source_file") not in evict_sources)
    ]

    But _relativize_source_files — which converts absolute source_file values to relative — is only called at line 438, after the eviction list is already built:

    # watch.py:438
    _relativize_source_files(result, project_root)

    So if graph.json contains absolute paths in source_file, they are never normalized before the comparison, and no nodes are evicted.

    Fix: call _relativize_source_files(existing, project_root) on the loaded graph before line 418.


    Layer 2 — Windows path separator mismatch

    Even with the call order fixed, the comparison still fails on Windows.

    deleted_paths is built using str() on a Path object (line 379):

    # watch.py:379
    deleted_paths.add(str(cand.relative_to(project_root)))
    # → "module-b\utils.py"  (backslashes on Windows)

    Same for evict_sources at line 415:

    # watch.py:415
    evict_sources.add(str(p.relative_to(project_root)))
    # → "module-b\utils.py"

    Meanwhile _relativize_source_files also uses str() (line 141):

    # watch.py:141
    item["source_file"] = str(source_path.resolve().relative_to(root))
    # → "module-b\utils.py"

    But build.py::_norm_source_file normalizes via .as_posix(), so graph.json stores forward-slash paths (module-b/utils.py). The comparison between "module-b\utils.py" and "module-b/utils.py" always fails → nodes are never evicted → silent ghost accumulation.

    The same pattern appears in extract.py:8640:

    # extract.py:8640
    item["source_file"] = str(sf_path.relative_to(root))

    Fix: replace str(...) with .as_posix() at watch.py:141, watch.py:379, watch.py:415, and extract.py:8640 to match the forward-slash convention already established by build.py.


    Relation to #777

    Issue #777 addresses a related but distinct layer: absolute paths being persisted to manifest.json, .graphify_root, and cache/ast/*.json at write time. The fixes above are about the comparison logic at read/eviction time and would be needed even after #777 is fully resolved, because the separator mismatch is independent of whether paths are stored as absolute or relative.

  3. added a commit that references this issue on May 26, 2026
  4. sub4biz commented on May 26, 2026

    @sub4biz
    Author

    graphify update still broken after eef623a (v8 branch)

    Tested with the v8 branch installed directly from git (uv tool install "graphifyy[gemini] @ git+https://github.com/safishamsi/graphify.git@v8").

    Reproduction

    mkdir corpus\module-a corpus\module-b
    # corpus\module-a\auth.py  — 4 functions
    # corpus\module-b\utils.py — 2 functions
    
    graphify extract corpus
    # → 7 nodes, 6 edges ✓
    
    Move-Item corpus\module-b ..\module-b-moved
    
    graphify update corpus
    # → [graphify watch] No code-graph topology changes detected; outputs left untouched.
    # → graph.json still has 7 nodes, 6 edges ✗
    

    Why

    The fix in build_merge (build.py) correctly handles the absolute→relative path mismatch for the graphify extract code path. But graphify update goes through _rebuild_code in watch.py, which has its own eviction logic that was not touched:

    # watch.py:379 — deleted_paths built with str(), backslashes on Windows
    deleted_paths.add(str(cand.relative_to(project_root)))
    
    # watch.py:418–422 — comparison against source_file from graph.json
    preserved_nodes = [
        n for n in existing.get("nodes", [])
        if n["id"] not in new_ast_ids
        and (not evict_sources or n.get("source_file") not in evict_sources)
    ]
    
    # watch.py:438 — _relativize_source_files called AFTER eviction, not before
    _relativize_source_files(result, project_root)

    Two issues remain in _rebuild_code:

    1. Call order — _relativize_source_files(existing, project_root) needs to be called on the loaded graph before line 418, not on result after the eviction list is already built.

    2. Separator mismatch — str(cand.relative_to(project_root)) produces backslashes on Windows (module-b\utils.py), while graph.json stores forward slashes (module-b/utils.py) via _norm_source_file. Replacing str(...) with .as_posix() at lines 379 and 415 — or routing through _norm_source_file the same way the new build_merge fix does — would resolve this.

  5. safishamsi commented on May 26, 2026

    @safishamsi
    Member
  6. added a commit that references this issue on May 26, 2026
    a26f24e
  7. added a commit that references this issue on May 26, 2026
    3f8efae
  8. safishamsi commented on May 26, 2026

    @safishamsi
    Member

    Good catch — both layers are now fixed on v8 (3f8efae).

    watch.py fixes:

    • _relativize_source_files(existing, project_root) now called on the loaded graph before the eviction list is built, so absolute source_file values in an existing graph.json are normalized before the comparison
    • deleted_paths and evict_sources now use .as_posix() instead of str() — consistent forward slashes on Windows
    • _relativize_source_files itself now uses .as_posix() internally (line 141)

    extract.py fix:

    • source_file relativization at line 8640 also switched to .as_posix()

    All 1355 tests pass. Will ship in the next PyPI release.

  9. sub4biz commented on May 26, 2026

    @sub4biz
    Author

    by the way — this was my first ever pull request 🎉 a bit nerve-wracking but worth it!

    Image
  10. sub4biz commented on May 26, 2026

    @sub4biz
    Author

    Thanks for staying on this — quick verification of v0.8.20 to close the loop. The _relativize_source_files call-order change and .as_posix() on deleted_paths/evict_sources are exactly right and verified working for the watcher path.

    Timeline:

    Issue filed graphify update leaves stale nodes after file deletion
    eef623a fixed build_merge → covers graphify extract path ✅
    my earlier comment showed graphify update still broken after eef623a
    3f8efae / v0.8.20 fixed _relativize_source_files call order + .as_posix() on deleted_paths/evict_sources → watcher path ✅
    #1036 fix for the remaining case below (submitted before v0.8.20; the changed_paths is None case was not included in that release)

    One case is still open: plain graphify update (changed_paths is None).

    Reproduction on v0.8.20:

    graphify extract corpus        # → 7 nodes (module-a + module-b)
    rm -r corpus/module-b
    graphify update corpus         # → "No code-graph topology changes detected" ✗
                                   #    graph.json still has 7 nodes
    

    Root cause: In _rebuild_code, when changed_paths is None, deleted_paths is never populated — so evict_sources stays empty and the guard short-circuits, preserving all non-new-id nodes unconditionally:

    # watch.py — v0.8.20, ~line 412
    evict_sources: set[str] = set(deleted_paths)      # always empty for plain update
    
    if changed_paths is not None:
        for p in extract_targets:
            evict_sources.add(p.relative_to(project_root).as_posix())
    
    preserved_nodes = [
        n for n in existing.get("nodes", [])
        if n["id"] not in new_ast_ids
        and (not evict_sources or n.get("source_file") not in evict_sources)
        #    ^^^^^^^^^^^^^^^^^^ True — all stale nodes survive
    ]

    The fix: for the else branch, walk existing graph nodes, compare their source_file against current disk state (normalised via _norm_source_file), and add any whose file is gone to both evict_sources and deleted_paths (so the shrink-guard also passes without --force).

    Fix + test in #1036 (47 passed on Windows).

    Note: #1036 targets v0.8.19 and may need a rebase onto current v8 before merge.

  11. added 5 commits that reference this issue on Aug 13, 2026
    2725fa2
    104d42d
    63354aa
    420c551
    e9ad3e7
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