Skip to content

fix(watch): bypass shrink-guard when caller declared explicit deletions - #1000

Merged
safishamsi merged 1 commit into
Graphify-Labs:v8from
mohamadkattan12:fix/post-commit-deletes
May 24, 2026
Merged

safishamsi merged 1 commit into
Graphify-Labs:v8from
mohamadkattan12:fix/post-commit-deletes

Conversation

@mohamadkattan12

Copy link
Copy Markdown
Contributor

Summary

The post-commit hook (graphify hook install) silently fails to prune nodes when a commit deletes a tracked file. After every delete-only commit, the rebuilt graph is correct in memory but never written to disk — the shrink-guard refuses to overwrite a larger graph.json with a smaller one.

Reproducible end-to-end on the current v8 tip:

$ rm scripts/my_script.py
$ git commit -m "remove unused script" scripts/my_script.py
[graphify hook] launching background rebuild ...
[graphify] WARNING: new graph has 64703 nodes but existing graph.json has 64705.
Refusing to overwrite — you may be missing chunk files from a previous session.
Pass --force to override.

The nodes for the deleted file linger forever unless the user manually runs graphify update . --force. Multiple cleanup commits compound the drift.

Root cause

graphify/watch.py::_rebuild_code already correctly handles deletions (lines 352-367): when changed_paths contains a path that no longer exists on disk, it adds the path to deleted_paths and uses it to evict the stale nodes from the preserved set. The rebuilt graph is intentionally smaller and correct.

The bug is downstream: _check_shrink (line 243) only sees the node-count delta and can't tell silent corruption ("a semantic chunk failed mid-extraction") from intentional shrinkage ("the user deleted a file"). It refuses both, and the in-memory result is dropped.

Fix

Thread an opt-in had_explicit_deletions flag from _rebuild_code into _check_shrink:

def _check_shrink(force, existing_data, new_data, tmp=None, *,
                  had_explicit_deletions: bool = False) -> bool:
    if force or not existing_data or had_explicit_deletions:
        return True
    ...

Both _check_shrink call sites in _rebuild_code now pass had_explicit_deletions=bool(deleted_paths). The guard remains intact for SILENT shrinkage (its actual purpose) — only callers that explicitly track deletions get the bypass. The post-checkout full-rebuild path doesn't pass changed_paths, so it keeps the old conservative behavior unchanged.

The fix is the minimum needed to close the bug:

  • No new public API surface
  • No change to default behavior for callers that don't pass changed_paths
  • No change to --force semantics
  • No change to the warning message users see when the guard does legitimately fire

Tests

8 new tests in tests/test_watch.py. All pass; no regressions across test_watch.py, test_build.py, test_export.py (74 tests total, 72 pass, 2 pre-existing skips).

Test Covers
test_check_shrink_blocks_silent_shrink Pre-existing behavior intact when no deletions declared
test_check_shrink_allows_force_override force=True still bypasses
test_check_shrink_allows_explicit_deletions New: deletion bypass works
test_check_shrink_allows_no_existing_data First-run case
test_check_shrink_allows_growth Sanity: growing graphs always allowed
test_check_shrink_unlinks_tmp_on_refuse Tmp cleanup on refusal
test_check_shrink_keeps_tmp_when_deletions_declared No spurious unlink when bypassing
test_rebuild_code_prunes_deleted_file_nodes End-to-end: git init → build → delete → re-run with deleted path in changed_paths → graph shrinks correctly

The integration test is the load-bearing one — it reproduces the exact scenario the post-commit hook triggers, using a real git init + two-file project, and verifies the deleted file's nodes are pruned while the surviving file's nodes are preserved.

Why this is the right shape

Three properties I tried to preserve:

  1. The guard still catches its actual target. Failed semantic chunks during a full re-extraction don't pass changed_paths at all — so had_explicit_deletions is False, the guard fires, the user sees the warning. The protection against silent corruption is unchanged.
  2. No false positives on intentional refactors. The post-commit hook already passes git diff --name-only HEAD~1 HEAD which includes deletions. Now they propagate through correctly.
  3. No new state. No config flag, no env var, no behavior toggle. The bypass is implicit from the data the caller already provides.

I considered an alternative — computing an "expected shrink budget" from the average nodes-per-file and only bypassing within that budget — but it adds heuristic policy where the simpler trust-the-caller approach is correct: if the caller said deletions happened, they're declaring intent.

Test plan

  • uv run pytest tests/test_watch.py -v -k "check_shrink or prunes_deleted" — 8/8 pass
  • uv run pytest tests/test_watch.py tests/test_build.py tests/test_export.py — 72 pass, 2 pre-existing skips
  • uv run ruff check graphify/watch.py tests/test_watch.py — clean
  • End-to-end reproduction on a real repo with graphify hook install + commit that deletes a file → graph.json now updates correctly

🤖 Generated with Claude Code

The post-commit hook passes `git diff --name-only HEAD~1 HEAD` as
changed_paths to `_rebuild_code`. That list includes deletions, and
`_rebuild_code` correctly identifies them (lines 352-367 in watch.py)
and evicts the stale nodes from the preserved set. The rebuilt graph
is intentionally smaller.

`_check_shrink` then refuses to overwrite the existing graph.json
because it only sees the node-count delta, not the cause. The guard
fires with "Refusing to overwrite — you may be missing chunk files
from a previous session. Pass --force to override."

Result: every commit that deletes a tracked file silently leaves stale
nodes in graph.json. The user must either pass --force (which also
disables the guard for legitimate failure modes) or manually re-run
`graphify update . --force` after delete-heavy commits.

Fix: thread a `had_explicit_deletions` flag from `_rebuild_code` into
`_check_shrink`. When the caller has declared the deletions, the
smaller graph is the expected outcome and the guard is skipped. The
guard remains intact for SILENT shrinkage — its actual purpose —
from failed semantic chunks or corrupted runs.

The fix is opt-in by design: callers that don't pass `changed_paths`
(e.g. the post-checkout full rebuild path) keep the old conservative
behavior. Only paths that explicitly track deletions get the bypass.

Tests added (tests/test_watch.py):
- `test_check_shrink_blocks_silent_shrink` — pre-existing behavior intact
- `test_check_shrink_allows_force_override` — pre-existing behavior intact
- `test_check_shrink_allows_explicit_deletions` — new: deletion bypass
- `test_check_shrink_allows_no_existing_data` — first-run case
- `test_check_shrink_allows_growth` — sanity
- `test_check_shrink_unlinks_tmp_on_refuse` — cleanup on refusal
- `test_check_shrink_keeps_tmp_when_deletions_declared` — no spurious unlink
- `test_rebuild_code_prunes_deleted_file_nodes` — end-to-end probe of the
  exact scenario the post-commit hook triggers (git init, build, delete
  one file, re-run with the deleted path in changed_paths, verify the
  graph shrinks and the surviving file's nodes are preserved)

All 8 new tests pass; full test_watch.py + test_build.py + test_export.py
(74 tests) pass with no regressions.
@safishamsi
safishamsi merged commit 6fba4e4 into Graphify-Labs:v8 May 24, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…ns (Graphify-Labs#1000)

The post-commit hook passes `git diff --name-only HEAD~1 HEAD` as
changed_paths to `_rebuild_code`. That list includes deletions, and
`_rebuild_code` correctly identifies them (lines 352-367 in watch.py)
and evicts the stale nodes from the preserved set. The rebuilt graph
is intentionally smaller.

`_check_shrink` then refuses to overwrite the existing graph.json
because it only sees the node-count delta, not the cause. The guard
fires with "Refusing to overwrite — you may be missing chunk files
from a previous session. Pass --force to override."

Result: every commit that deletes a tracked file silently leaves stale
nodes in graph.json. The user must either pass --force (which also
disables the guard for legitimate failure modes) or manually re-run
`graphify update . --force` after delete-heavy commits.

Fix: thread a `had_explicit_deletions` flag from `_rebuild_code` into
`_check_shrink`. When the caller has declared the deletions, the
smaller graph is the expected outcome and the guard is skipped. The
guard remains intact for SILENT shrinkage — its actual purpose —
from failed semantic chunks or corrupted runs.

The fix is opt-in by design: callers that don't pass `changed_paths`
(e.g. the post-checkout full rebuild path) keep the old conservative
behavior. Only paths that explicitly track deletions get the bypass.

Tests added (tests/test_watch.py):
- `test_check_shrink_blocks_silent_shrink` — pre-existing behavior intact
- `test_check_shrink_allows_force_override` — pre-existing behavior intact
- `test_check_shrink_allows_explicit_deletions` — new: deletion bypass
- `test_check_shrink_allows_no_existing_data` — first-run case
- `test_check_shrink_allows_growth` — sanity
- `test_check_shrink_unlinks_tmp_on_refuse` — cleanup on refusal
- `test_check_shrink_keeps_tmp_when_deletions_declared` — no spurious unlink
- `test_rebuild_code_prunes_deleted_file_nodes` — end-to-end probe of the
  exact scenario the post-commit hook triggers (git init, build, delete
  one file, re-run with the deleted path in changed_paths, verify the
  graph shrinks and the surviving file's nodes are preserved)

All 8 new tests pass; full test_watch.py + test_build.py + test_export.py
(74 tests) pass with no regressions.
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