Skip to content

fix: evict stale nodes in _rebuild_code for graphify update (#1007) - #1036

Closed
sub4biz wants to merge 6 commits into
Graphify-Labs:v8from
sub4biz:fix/1007-rebuild-code-eviction
Closed

sub4biz wants to merge 6 commits into
Graphify-Labs:v8from
sub4biz:fix/1007-rebuild-code-eviction

Conversation

@sub4biz

@sub4biz sub4biz commented May 26, 2026 •

Copy link
Copy Markdown

Fix graphify update failing to evict stale nodes from deleted files (#1007)

Problem

graphify update (_rebuild_code with no changed_paths) silently preserved nodes
from deleted files. The fix in build_merge (eef623a) covered graphify extract
but _rebuild_code has its own eviction logic that was not touched.

Reproduction with v8 branch after eef623a:

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

When changed_paths is None (plain graphify update), evict_sources is never
populated — deleted files are simply absent from the new extraction and end up in
preserved_nodes unchanged:

# watch.py — before this fix
evict_sources: set[str] = set(deleted_paths)   # always empty when changed_paths is None
if changed_paths is not None:
    ...                                         # evict_sources never filled for plain update
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)  # guard is no-op
]

Additionally, str(cand.relative_to(project_root)) produces backslash paths on
Windows (module-b\utils.py) while graph.json stores forward-slash paths
(module-b/utils.py) — causing eviction to silently fail even when
changed_paths is provided.

Fix

watch.py — three changes:

  1. _relativize_source_files (line 141): str(...) → .as_posix() —
    fixes forward-slash normalization for absolute-path conversion.

  2. deleted_paths / evict_sources (lines 379, 415): str(...) →
    _norm_source_file(str(p), root) — same function used in build_merge,
    so prune keys are normalized identically to node source_file attrs.

  3. _rebuild_code reconciliation: when changed_paths is None, iterate
    existing graph nodes and build current_sources — both sides normalized
    via _norm_source_file so prune keys and node source_file attrs are
    compared identically (mirrors build_merge fix in eef623a).
    Any code node whose normalized source is absent from disk is added to
    evict_sources and deleted_paths (to pass the shrink-guard).

Test

tests/test_watch.py::test_rebuild_code_evicts_nodes_from_deleted_files —
builds a two-file corpus, deletes one file, calls _rebuild_code directly,
asserts the deleted file's nodes are gone and the surviving file's nodes remain.

Full test run: 47 passed, 7 skipped across test_build.py and test_watch.py
(skipped: POSIX fcntl + watchdog — not applicable on Windows).

@safishamsi

Copy link
Copy Markdown
Member

Implemented the else: reconciliation block directly in v8 (commit d1d5751) to avoid merge conflicts — v8 already had the as_posix() fixes in slightly different form. The final code uses _norm_source_file (aliased as _nsf) on both the eviction and current-sources sides, mirroring exactly what build_merge does. Also ported your test (test_rebuild_code_evicts_nodes_from_deleted_files) to tests/test_watch.py — all 1387 tests pass. Thanks for diagnosing the missing else: block!

@safishamsi safishamsi closed this May 27, 2026
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.

2 participants