Skip to content

Semantic nodes silently evicted when a change set contains a present-but-unextractable file; had_explicit_deletions disables the shrink guard that would catch it #2056

Closed

Description

@fahmizainundin

Semantic nodes are silently evicted when a commit touches a non-extractable file, with the shrink guard disabled

Version: graphifyy 0.9.20 · Python 3.10.12 · Linux (Ubuntu 22.04) · uv tool install

Summary

When an incremental rebuild's change set contains a file that detect() filters out
(no AST extractor — e.g. .yml, .json, .txt), _rebuild_code() classifies that
file as deleted and evicts every node claiming it as source_file — including
LLM-extracted concept/rationale/document nodes from a prior semantic pass.

The same condition sets had_explicit_deletions=True, which makes _check_shrink()
return early and skip the shrink guard entirely. So the node loss is both caused
and un-caught by the same flag, and nothing is printed to warn the user.

This is a data-loss bug specifically for the expensive half of the graph: AST nodes
are free to regenerate, semantic nodes cost real LLM tokens (~600k input for our
~90-file corpus).

Root cause

graphify/watch.py:724 — first line of _check_shrink():

if force or not existing_data or had_explicit_deletions:
    return True

The caller at watch.py:1232 passes had_explicit_deletions=bool(deleted_paths).

deleted_paths is populated at watch.py:1017 for files that still exist on disk:

existing_in_root = next(
    (cand for cand in candidates if cand.exists() and _is_relative_to(cand, watch_root)),
    None,
)
if existing_in_root is not None:
    # The path exists under the watched root but detect filtered it out.
    # Evict any stale nodes that still claim it.
    _add_deleted_source(existing_in_root)
    continue

The comment says "evict any stale nodes", but the eviction is unconditional: it
also removes current, valid semantic nodes whose only fault is that their source file
has no AST extractor. Docstring intent ("the caller has declared which files were
removed … a smaller graph is the expected outcome") does not hold here — nothing was
removed.

Reproduction

Corpus: a repo with a semantic pass over docker-compose.yml (5 nodes: 3 code,
2 rationale), plus normal TS sources. Existing graph: 3,811 nodes / 536 semantic.

Instrument _check_shrink to report its inputs and abort the write:

from pathlib import Path
import graphify.watch as w

def spy(force, existing, new, tmp=None, *, had_explicit_deletions=False, rebuilt_sources=None):
    new_ids = {n.get('id') for n in new.get('nodes', [])}
    lost = [n for n in existing.get('nodes', []) if n.get('id') not in new_ids]
    print('had_explicit_deletions:', had_explicit_deletions)
    for n in lost:
        print(f"  LOST {n.get('file_type'):10s} {n.get('source_file')} :: {n.get('label')}")
    return False   # abort write

w._check_shrink = spy
w._rebuild_code(
    Path('/path/to/repo'),
    changed_paths=[Path('src/auth/guard.ts'), Path('docker-compose.yml')],
    force=False,
)

Actual output:

had_explicit_deletions: True
  LOST code       docker-compose.yml :: <service node A>
  LOST code       docker-compose.yml :: <service node B>
  LOST code       docker-compose.yml :: <service node C>
  LOST rationale  docker-compose.yml :: <rationale node A>
  LOST rationale  docker-compose.yml :: <rationale node B>

Graph goes 3,811 → 3,806 nodes. With the real _check_shrink, this write is
permitted (guard skipped) and nothing is logged.

docker-compose.yml was neither deleted nor renamed — it is present and unmodified.

Comparison across change-set shapes (same instrumentation):

change set had_explicit_deletions guard skipped nodes
code file only — — no topology change
code + real deletion — — no topology change
code + existing non-code file True True 3811 → 3806
code + gitignored file — — no topology change

Impact

The loss is cumulative and silent. Each commit touching a different non-extractable
file evicts that file's semantic nodes, unguarded. Over ~30 commits our graph
degraded from ~4,000 nodes to a 50-node stub containing only doc-derived nodes:

$ grep -E "Rebuilt:" ~/.cache/graphify-rebuild.log
[graphify watch] Rebuilt: 4072 nodes, 8891 edges, 320 communities
[graphify watch] Rebuilt: 29 nodes, 27 edges, 6 communities
[graphify watch] Rebuilt: 50 nodes, 45 edges, 8 communities

Zero WARNING/Refusing lines across the whole log — the guard was never consulted.

Compounding it, the collapse ratchets: incremental rebuilds preserve unchanged
nodes from the existing graph.json, so once small it stays small until a
post-checkout full rebuild. Verified: rebuilding from a sabotaged 42-node graph
produced 63 nodes, not ~3,800.

Because post-commit fires on every commit, projects whose convention is to touch a
config/doc file alongside code hit this constantly.

Suggested fix

Two independent issues; either fix alone reduces the severity.

  1. Don't classify a present file as deleted (watch.py:1017). A file that exists
    but was filtered by detect is not a deletion. If the intent is to drop stale AST
    nodes, scope the eviction to _origin == "ast" nodes and leave semantic nodes
    alone — they were produced by a pass that detect's AST filter has no authority
    over.

  2. Narrow the had_explicit_deletions bypass (watch.py:724). Rather than
    skipping the guard wholesale, reuse the existing rebuilt_sources accounting
    already implemented a few lines below — a shrink is legitimate when every lost node
    belongs to a genuinely deleted file, and suspicious otherwise. The bypass makes the
    rebuilt_sources logic unreachable in exactly the case it was written for.

A cheap third mitigation: log at INFO when the guard is skipped, so the loss is at
least visible in ~/.cache/graphify-rebuild.log.

Workaround

We wrapped both hooks with an external guard that snapshots a known-good graph and
restores it when node count drops below max(1000, 50% of known-good) or the
semantic layer goes to zero. Effective, but it repairs after the bad write rather
than preventing it, since the write happens via atomic replace inside the package.

Related issues

Activity

  1. safishamsi commented on Jul 20, 2026

    @safishamsi
    Member

    Fixed in v0.9.21: a present-but-unextractable file in an incremental change set is preserved rather than treated as a deletion, and the shrink guard no longer waves the loss through on the mere presence of a deletion. https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.21

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