Repository navigation
fix(pipeline): an incremental run with nothing to write never takes the database from readers - #500
Merged
HananKavitzAmat merged 3 commits intoNov 11, 2026
Conversation
…he database from readers Every incremental Rust run handed the database to Rust before Rust's own diff ran: set_rust_pipeline_in_progress(True), close Python's connections, run, reconnect. Every search in that window is refused with DatabaseRustPipelineInProgressError. An MCP server's start-up catch-up does this on every start, and the common start changes nothing -- a fresh worktree seeded from another checkout's index has new mtimes and identical bytes. Measured on a 1174-file index (real `chunkhound mcp`, regex search every 2 s): a start with 0 content changes refused searches for 46 s, and 39 s on the next start. Rust spent ~1.5 s diffing and the rest opening the file and compacting it on a run that wrote nothing. process_directory() now decides on Python's own connection, before any handoff, whether the Rust run has anything to write. The new rust_incremental_precheck reads the same file-state columns as the Rust differ's snapshot and classifies files by its rules (mtime, then xxh3_64 against the stored content_hash, then size), off the event loop: - nothing changed or removed: Rust is not started; HNSW indexes a crashed run left missing are restored through the provider, as Rust's open() would have done; - changes and no removals: Rust gets only the changed files with cleanup off, so its own diff no longer hashes the whole tree while it owns the file; - removals: unchanged, the full list as before. A file is called unchanged only when Rust would call it unchanged; a matching hash means identical bytes. force_reindex is unchanged. An otherwise-empty run no longer gives Rust a chance to compact. Python-side writes are still compacted by the provider's sampled compact_if_needed, and Rust still checks after every run that writes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sh; guard the narrowed run compute_file_hash raises ValueError for a path that stopped being a regular file after stat(); count that file as changed, as Rust does, instead of failing the scan. The docstring now states the unchanged rule exactly. New contract test: editing one file with nothing removed reindexes it and keeps every other file. It guards the cleanup-off requirement of the narrowed Rust run; removing that line deletes the unedited rows and fails the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
|
Summary: The fix is sound and its classification matches the Rust differ, but there are two small duplications and a few coverage gaps that I'd like addressed or acknowledged before merge. Strengths:
Issues:
Reusability:
|
…lives with the precheck - pipeline_bridge.rust_mtime_epsilon(config) is now the single reader of indexing.mtime_epsilon_seconds for the Rust pipeline and the precheck; the coordinator no longer imports the private _cfg_or. - plan_rust_run / RustRunPlan move the narrow-or-skip decision out of process_directory into rust_incremental_precheck.py, unchanged in behaviour. - PEP 257 docstrings for has_work and find_rust_incremental_work. - Contract tests: the precheck agrees with the Rust differ (NULL stored mtime, same mtime with a new size, no stored hash, same bytes, untouched), and an unchanged run that skips Rust still recreates a missing HNSW index. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
Author
|
@HananKavitzAmat thanks. Both items you asked for before merge are in bb53735, a new commit on top of a1eff26; nothing was rebased. Major: logic duplicated across two languages.
Minor: the epsilon accessor.
The other minors:
Reusability:
Checks (serial, Windows):
🤖 Generated with Claude Code |
Collaborator
|
looks good. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every incremental Rust run hands the database to Rust before Rust's own diff has decided whether there is anything to do.
run_rust_indexing_phase()publishesset_rust_pipeline_in_progress(True), closes Python's connections, runs the pipeline and reconnects, and every DB-backed MCP call in that window fails withDatabaseRustPipelineInProgressError. An MCP server's start-up catch-up does this on every start. The most common start changes nothing: a fresh git worktree seeded from another checkout's index has new mtimes and identical bytes.Measured on Windows against a copy of a real 1174-file index, starting
chunkhound mcpon a fresh checkout and probing a regex search every 2 s:The pipeline log for these runs reports
files=0, chunks=0, embeds=0. Rust spent about 1.5 s diffing; the remaining ~35 s went to opening the file and compacting it, on a run that wrote nothing.Fix
process_directory()now decides on Python's own connection, before any handoff, whether the Rust run has anything to write. The newchunkhound/services/rust_incremental_precheck.py:FILE_STATE_SELECT, including the::TIMESTAMPTZmtime cast);differ.rs::compute_diff: no row or a NULL mtime means changed; a different mtime means changed unless the xxh3_64 digest matches the storedcontent_hash; an equal mtime with a different known size means changed;asyncio.to_thread, so hashing a fresh worktree never blocks the event loop;pipeline_bridge.rust_mtime_epsilon(config), the same function that sets the value Rust receives, so the two can't disagree. (The Python pipeline's own reader stays separate: it turns an explicit0.0into0.01, which Rust does not.)plan_rust_run()in the same module turns that classification into the run Rust is given (or none), so the Rust-parity rules and the decision built on them live in one place.Cost. Every start with stale mtimes hashes the files whose mtime differs, on a worker thread: in a fresh worktree that's the whole tree, about 16 s on the 1174-file index above. Searches keep answering during it; before this change, the same hashing happened inside Rust's diff while readers were refused.
Files Rust indexes with no chunks (an unknown file type matched by the include patterns, an empty file) still get a
filesrow with their size and hash, so they count as unchanged on the next start and don't force a handoff. Checked with a repo containing both: the second and third runs loggedDiff: 0/5 changedand never started Rust.Then:
ensure_all_hnsw_indexes_async), as Rust'sopen()would have done.force_reindexis unchanged. A file is called unchanged only when its bytes are: either its hash matches, or its mtime and size both match (the same trust Rust's mtime fast path already places in them).One behaviour changes deliberately: an otherwise-empty run no longer gives Rust a chance to compact. Python-side writes are still compacted by the provider's sampled
compact_if_needed, and Rust still checks after every run that writes. Note that Rust's waste metric currently reports compaction as due on every run for an index with embeddings (compaction_statscounts theFLOAT[N]column's list-child segment as rows: 2,095,104 "stored" against 20,547 live, ratio 0.981). That is a separate fix to the native crate, which is tracked separately.Alternative considered: Python deletes removed rows and Rust always gets the changed subset
Rejected. The provider's batch delete refuses to run while a realtime transaction is open, which would fail the start-up scan. It also snapshots every deleted row for rollback, and it would leave a removal-only run uncompacted. Removals are rare at start-up, and today's full path already handles them correctly.
After
The same harness and inputs:
Tests
tests/contracts/test_rust_startup_reader_access.py:test_unchanged_reindex_never_takes_the_database_from_readersindexes a repo, gives every file a new mtime without changing its bytes, re-runs, and asserts the run never published Rust ownership, a search answers, and every row survives.a. Before the fix it fails (
assert True not in [True, False]).b. After, it passes.
test_editing_one_file_reindexes_it_and_keeps_every_other_fileguards the cleanup-off requirement of the narrowed run. Deleting that line makes it fail, because the unedited files' rows are removed.test_precheck_agrees_with_the_rust_differis the parity contract: for each case it takes the precheck's verdict, then hands the real Rust pipeline the full file list (precheck bypassed) and asserts Rust re-indexes exactly what the precheck called changed. Cases: a NULL stored mtime, the same mtime with a different size, a new mtime with no stored hash (all re-indexed), a new mtime with identical bytes, and an untouched file (neither re-indexed). Deleting the size check from the precheck fails only the size case; treating a NULL mtime as unchanged fails only that case.test_unchanged_reindex_restores_an_hnsw_index_a_crash_left_missingdrops the HNSW index, runs an unchanged incremental run, and asserts Rust was never started and the index is back. Replacing the skip path'sensure_all_hnsw_indexes_async()withpassmakes it fail.On this branch (on
main), run serially on Windows: both new tests, the Rust contract tests (orphan-cleanup parity, HNSW kept on empty diff, skipped/filtered, unknown files, disk usage), the runner unit tests, skip-unchanged, the LanceDB fallback and smoke. Result: 45 passed and 5 failed. All 5 failures aretest_cli_help_commandshitting its 5 s limit onuv run chunkhound ... --help.The same commits were also tested on my fork, whose base carries some of my other open PRs. There the full suite ran serially. Every file with a failure was then re-run with and without this change under identical settings: 32 failed with it, 36 without it, and 65 errors each. No failure appears only with the change. The failures are environment-bound (site build packages, CLI subprocess timeouts, MCP handshake timeouts).
test_cli_help_commandstimes out at its 5 s subprocess limit on a different parameter from run to run.Two tests in
tests/integration/test_directory_indexing_hnsw.py([ensure_all_hnsw_indexes]) fail on this machine identically with and without this change. They run the Python pipeline (CHUNKHOUND_USE_RUST=0) and wait 5 s forensure_all_hnsw_indexesto start. With the wait raised to 60 s, both pass.Related: #238, #83 (worktree support): a worktree seeded from another checkout's index is the start this makes cheap.
🤖 Generated with Claude Code