Skip to content

fix(pipeline): leave the HNSW index alone when a run has nothing to write - #433

Merged
gero-robert merged 2 commits into
chunkhound:mainfrom
WynandVStaden:fix/skip-hnsw-rebuild-on-empty-diff
Oct 9, 2026
Merged

gero-robert merged 2 commits into
chunkhound:mainfrom
WynandVStaden:fix/skip-hnsw-rebuild-on-empty-diff

Conversation

@WynandVStaden

Copy link
Copy Markdown
Contributor

Fixes #431.

Why

Re-indexing an unchanged checkout still dropped every HNSW index at the start of the store thread and rebuilt them at the end. The diff already says there is nothing to write, so that is a full index build and checkpoint for no change (17 s of a 20.7 s run in the issue's measurement).

What changes

The store thread now skips the drop_all_hnsw_indexes() / ensure_all_hnsw_indexes() bracket when all three hold:

  • no batch to write (batch_count == 0)
  • no orphan to delete (delete_paths is empty)
  • no compaction due (needs_compaction() is false)

The decision lives in a small helper, can_skip_hnsw_bracket, so it can be unit-tested with the existing FakeBackend.

Three things I checked before skipping anything:

  • Crash recovery is unaffected. open() already calls ensure_all_hnsw_indexes(), so an index a killed run left missing is recreated before the store thread reaches this point. Covered by a test that drops the index out-of-band and then runs an empty incremental index.
  • Compaction keeps the bracket. run_compaction() rebuilds the indexes through reopen() using saved_hnsw_metrics, which only drop_all_hnsw_indexes() fills. Skipping the drop on an empty-but-compacting run would rebuild a non-cosine index as cosine, so that case takes the old path. needs_compaction() is asked right after open(); nothing is written before the post-write check, so both calls give the same answer.
  • Progress bars still resolve. rust_pipeline_runner.py resolves the index and compact bars when write-index or write-compact fires, so write-index is still emitted. It arrives as (1, 1) when the indexes were left in place and as (0, 1) when they are about to be rebuilt; the phase doc comment says so. The Python handler ignores those two numbers today, so nothing else changes.

Tests

tests/contracts/test_hnsw_kept_on_empty_diff.py (Rust pipeline, mock embeddings, real DuckDB):

Case Expectation
empty incremental run write-index is (1, 1), index and row counts unchanged
one file modified write-index is (0, 1), index present
one file removed (orphan delete only) write-index is (0, 1), index present
empty run with compaction forced write-compact fires, write-index does not, index present
empty run after the index was dropped out-of-band index is back

On main the first and last cases fail and the three controls pass; with this change all five pass. I use the progress tuple as the probe because nothing in DuckDB distinguishes a rebuilt index from the one it replaced (duckdb_indexes().index_oid survives a drop and recreate), and the [hnsw-drop] log line is not reliable to capture because pyo3-log caches logger levels per process.

Four unit tests in pipeline.rs cover the decision itself, including that a failing compaction check still closes the backend.

Run on Windows 11, Python 3.14, extension built from this branch:

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings: clean
  • cargo test: 135 passed
  • ruff check / ruff format --check on the new test file: clean
  • pytest tests/test_smoke.py: 14 passed
  • pytest tests/contracts: 108 passed, 4 skipped, 1 failed (test_symlink_preserves_logical_path, which needs a Windows symlink privilege my account lacks: WinError 1314)
  • pytest tests/ -n 4: 4984 passed, 128 skipped, 23 failed, 65 errors. The 65 errors are tests/site, which need the site build I did not do. The 23 failures are that symlink test plus MCP/daemon/CLI start-up tests timing out under parallel load on this machine; tests/test_daemon_smoke.py and tests/test_smoke.py pass when run on their own.

Not measured: I did not reproduce the issue's 17 s figure on a large real index. The tests show the rebuild no longer happens; the time saved follows from that.

…rite

The store thread dropped every HNSW index at the start of a run and rebuilt
them at the end even when the incremental diff was empty, so re-indexing an
unchanged checkout paid for a full index build and checkpoint.

Skip the drop/rebuild bracket when there is no batch to write, no orphan to
delete and no compaction due. open() already recreates an index a crashed
run left missing, so recovery is unaffected. Compaction keeps the bracket
because it rebuilds the indexes with the metrics the drop captures.

The write-index progress phase still fires, as (1, 1) instead of (0, 1),
so the progress bars resolve as before.

Fixes chunkhound#431
Comment thread src/pipeline/pipeline.rs Resolved
Comment thread src/pipeline/pipeline.rs Resolved
A run that skipped the bracket no longer asks needs_compaction() a second time after the write loop: compaction rebuilds the indexes from the metrics the drop captured, so it only belongs to a run that dropped them.

The disk-limit log no longer says the indexes are rebuilt when they were left in place.

Add a contract test for a non-incremental second run over unchanged files, which still writes and must keep the bracket.

@gero-robert gero-robert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the follow-up. The three notes on the diff are in 4652553:

  • bug (log): the disk-limit message on the skip path says the existing indexes were left in place.
  • The post-write compaction check reuses the skip decision.
  • The non-incremental contract test expects a rebuild.

I've marked those threads resolved.

The descriptions in pipeline_parse_embed_store, src/AGENTS.md, and the write-index comment still say the store thread always rebuilds the index. Approving with that left as it is.

@gero-robert
gero-robert added this pull request to the merge queue Oct 9, 2026
Merged via the queue into chunkhound:main with commit 0db0fb4 Oct 9, 2026
23 checks passed
huangyingting pushed a commit to repomesh/chunkhound that referenced this pull request Oct 14, 2026
Three descriptions still said the store thread always rebuilds the HNSW indexes after the final batch. Since chunkhound#433 a run with nothing to write, delete or compact leaves them in place.
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.

Incremental index rebuilds the HNSW index when nothing changed

2 participants