Skip to content

fix(embeddings): stop embeds starving under continuous edits, and report/repair chunks with no vector - #467

Open
fstrasz wants to merge 8 commits into
chunkhound:mainfrom
fstrasz:pr/embedding-repair
Open

fstrasz wants to merge 8 commits into
chunkhound:mainfrom
fstrasz:pr/embedding-repair

Conversation

@fstrasz

@fstrasz fstrasz commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Problem

A chunk whose embedding is never written stays invisible to semantic search, while a regex search still finds it. Three gaps let that happen silently:

  1. Embeds starve under continuous edits. Realtime embed mutations have the lowest priority (_MUTATION_PRIORITIES["embed"] = 2), so they run only when no change/scan/dir_index is queued. While files keep changing, that never happens. On a live server we observed embeds waiting over two hours, with ~530 chunks lacking vectors.
  2. Nothing repairs missing vectors later. The realtime server embeds only in response to file events, so vectors missing from before a restart, or from a failed or starved pass, are never filled. chunkhound index doesn't fill them either: after a clean Rust run, DirectoryIndexingService skips the missing-embeddings pass, and the Rust pipeline embeds only files it (re)processes.
  3. It is not visible. daemon_status reports ready throughout, with no coverage figure.

Changes

  • Anti-starvation (fix(realtime)): once a pending embed has waited _EMBED_MAX_WAIT_SECONDS (60 s), the loop runs one generate_missing_embeddings() pass ahead of the next non-embed job. That pass covers every chunk missing a vector, so it settles all waiting embeds. Their queue entries are released and skipped as unowned when dequeued. A failed pass (compaction busy, provider error) leaves them pending and isn't retried until the interval passes again.
  • Coverage + self-repair (fix(embeddings)):
    • IndexingCoordinator.count_chunks_missing_embeddings() reuses the same anti-join that generate_missing_embeddings works from, so the count and the repair can't disagree.
    • When the realtime queue goes idle after any work, and once at start-up, the server measures that count and publishes it as realtime.embedding_coverage = {missing_chunks, checked_at}. If any chunks are missing and no embed is queued, it queues one. A check the database can't answer yet (Rust pipeline or compaction holds it) is retried after 30 s of idle. The check never raises, because an escaping error with the flag still set would spin the loop without yielding.
    • DirectoryIndexingService always runs the missing-embeddings pass. When nothing is missing, it costs one query.

Tests

  • test_overdue_embed_runs_ahead_of_queued_changes: an embed queued 5 minutes ago runs before a newer change, and its queue entry doesn't run again.
  • test_idle_server_repairs_chunks_missing_vectors_and_reports_coverage: an idle server with 531 missing chunks fills them with no file event and reports 0.
  • test_clean_rust_run_still_repairs_chunks_missing_vectors: a clean Rust run that skips every unchanged file still calls the repair pass.

Each test fails when its change is removed. The realtime and directory-indexing unit tests (21) pass on this branch, which is based on main at e5f7e70.

Live check

Run on a Windows box against a ~16k-chunk index with Voyage voyage-code-4:

  • After the fix, the realtime queue drained continuous edits within seconds.
  • chunkhound index reported 16,139 chunks / 16,139 embeddings.
  • daemon_status showed embedding_coverage.missing_chunks: 0.

fstrasz and others added 3 commits October 31, 2026 16:22
Embeds have the lowest queue priority, so they run only when no change, scan or
dir_index is waiting. While files keep being edited that never happens: one live
server held 918 files unembedded for over two hours, so semantic search silently
missed them while regex search found them.

Once a pending embed has waited _EMBED_MAX_WAIT_SECONDS (60 s), the loop runs one
embed pass ahead of the next non-embed job. generate_missing_embeddings covers every
chunk missing a vector, so the pass settles all waiting embeds; their queue entries
are released and skipped as unowned when dequeued. A failed pass (compaction busy,
provider error) leaves them pending and is not retried until the interval passes again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A chunk whose embedding was never written stays invisible to semantic search while
regex search still finds it, and nothing reported or repaired it:

- daemon_status had no coverage figure, so a live server showed healthy while ~531
  chunks lacked vectors.
- The realtime server embeds only in response to file events, so missing vectors
  from before a restart (or a starved/failed pass) were never filled.
- `chunkhound index` skipped the missing-embeddings pass after a clean Rust run,
  and the Rust pipeline embeds only files it (re)processes, so unchanged files'
  missing vectors were never repaired by a rebuild either.

Changes:
- IndexingCoordinator.count_chunks_missing_embeddings(): the same anti-join
  generate_missing_embeddings works from, so count and repair cannot disagree.
- Realtime: when the queue goes idle after any work (and once at start-up), measure
  the count, publish it as realtime.embedding_coverage {missing_chunks, checked_at},
  and queue an embed pass if any are missing. A check the database cannot answer
  yet (Rust pipeline/compaction owns it) is retried after 30 s idle. The check can
  never raise: an escaping error would spin the loop without yielding.
- DirectoryIndexingService: always run the missing-embeddings pass; when nothing
  is missing it is one query.

Tests: the idle server fills missing vectors with no file event and reports 0; a
clean Rust run still calls the repair pass. Each goes red when its change is
removed. Unit suite: the 7 failures / 9 errors are identical without this change
(environmental: config and API-key tests).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…not embed

When an embed pass left chunks without a vector (provider down, a chunk the
model rejects), the idle coverage check queued another embed pass, whose
completion re-ran the check, and so on: CI logged the check ~400 times a
second, and test_stale_delete_retry_is_dropped_after_newer_change never
settled on any OS.

An embed pass covers every chunk missing a vector, so a second pass with no
change in between can only repeat it. Queue a repair pass only when no embed
pass has run since the last non-embed mutation (or start-up). Chunks still
missing are reported in embedding_coverage; the next change or a restart
retries them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fstrasz

fstrasz commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

A note on the red CI before anyone reviews this. test_stale_delete_retry_is_dropped_after_newer_change failed on all three OSes because of a real defect in this PR, which is now fixed in d3c4fab, on top of 301d19f.

The cause was the idle coverage check. When an embed pass left chunks without a vector, the check queued another embed pass, and when that pass finished the check ran again, so the cycle never ended. The test's no-op embed hit this every time, and the CI log shows the check running about 400 times a second. In production, the same thing would happen whenever chunks can't be embedded (provider down, a chunk the model rejects).

The fix relies on the fact that an embed pass already covers every chunk missing a vector, so a second pass with nothing changed in between can only repeat it. A repair pass is now queued only if no embed pass has run since the last non-embed mutation, or since start-up. Chunks still missing are reported in embedding_coverage, and the next change or a restart retries them.

test_chunks_an_embed_pass_cannot_fix_are_reported_not_retried_in_a_loop pins this. Without the fix it records 308 embed passes in 0.3 s; with it, exactly 1.

🤖 Generated with Claude Code

@fstrasz

fstrasz commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

CI on d3c4fab: the tests matrix is green on all three OSes, including test_stale_delete_retry_is_dropped_after_newer_change, which failed everywhere on 301d19f.

One job failed: watchman-runtime-validation (ubuntu-latest), in test_daemon_smoke.py::test_watchman_start_failure_cleans_up_after_eager_publication (log). I believe that one is a timing race, not this change:

  • The test accepts three wordings for the caller-visible failure. This run surfaced a fourth: "ChunkHound daemon IPC lock file removed before registration — daemon may have shut down". The test's own comment says a fast Watchman failure can resolve at different layers depending on timing.
  • The failure happens at the daemon's startup barrier, before the realtime processing loop runs. d3c4fab only changes that loop.
  • The same job passed on 301d19f, and the Windows run of it passed on this head.

I can't re-run upstream jobs from a fork. Could a maintainer re-run that job?

🤖 Generated with Claude Code

…llow-up is never stranded

The promoted embed pass this PR adds awaits after a change's path is made
active and before the "file no longer exists" check. While the path is
active, add_file turns a newer write into a reserved follow-up, and only
the end of the active span queues it. That end lived in a finally around
process_file alone; the missing-file branch released the path and
continued without draining the reservation, so a write whose event arrived
during the promoted pass (for example a re-create) was not indexed until
the file changed again. Before the promoted pass nothing awaited between
activation and the check, so no reservation could be made there.

The change/scan handling moves into _process_change_mutation, which
activates the path and runs the whole active span (promoted embed pass,
existence check, process_file) in one try/finally. That finally calls
_end_active_change, now the only place an active path is released, which
pops it and drains the reserved follow-up. process_file errors are still
re-raised to the loop's handlers (compaction retry, failed_files). The
loop's fall-through after dir_index was reached only by change/scan and
is gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fstrasz

fstrasz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Added 1c0e01c to this PR. It fixes a defect the promoted embed pass introduces.

The window. _process_loop marks a change's path active, then (new in this PR) awaits _run_promoted_embed_pass when embeds are overdue, and only then checks whether the file still exists. While the path is active, add_file turns a newer write into a reserved follow-up, which only the end of the active span queues. That end was a finally around process_file alone. The "file no longer exists" branch released the path and continued without draining the reservation. So a write whose event arrived during the promoted pass (for example a re-create) was stranded, and was not indexed until the file changed again. On main nothing awaits between activation and the check, so no reservation can be made there.

The fix. The change/scan handling moves into _process_change_mutation, which activates the path and runs the whole active span (promoted pass, existence check, process_file) in one try/finally. That finally calls _end_active_change, now the only place an active path is released. It pops the path and drains the reserved follow-up. process_file errors are still re-raised to the loop's handlers (compaction retry, failed_files). The loop's fall-through after dir_index was reachable only by change/scan and is gone.

Test. tests/test_realtime_functional.py::test_follow_up_reserved_before_missing_file_check_is_queued. The change is held in the promoted pass, the file is re-created and its created event goes through the real _consume_events (reserving a follow-up), the next exists() for that path returns False once, and the pass is released. The test asserts the follow-up processes the new content.

  • At d3c4fab (this PR before the commit): fails. It times out waiting for the follow-up.
  • With the commit: passes.
  • With the drain removed from _end_active_change: fails.

Not reproduced end to end. It needs the file to be missing at the check while a follow-up was reserved during the promoted pass. That takes fault injection, so no end-to-end run was attempted.

Run on Windows, Python 3.13, serially (-n 0): tests/realtime, tests/unit/test_realtime_compaction_retry.py, tests/unit/test_realtime_embed_starvation.py, tests/test_debounce.py, tests/test_realtime_functional.py, tests/test_realtime_failures.py.

passed failed skipped
d3c4fab (with the new test) 141 1 (the new test) 34
1c0e01c 142 0 34

Two conflicts, both from chunkhound#508 landing on main while this branch moved the
file-processing block into _process_file_mutation (its "end every active
change in one place" commit):

- chunkhound/services/realtime/pipeline.py: main changed the old inline block
  in _process_loop; this branch had removed that block. Kept this branch's
  structure and carried chunkhound#508's change into the helper: the event dedup entry
  is now cleared in the processing attempt's finally, whether or not
  processing succeeded, instead of only after a successful index. Same
  semantics as main; no other behaviour change.
- tests/test_realtime_functional.py: both sides inserted tests at the same
  place. Kept all three: main's
  test_write_during_processing_inside_dedup_window_is_indexed and
  test_write_after_failed_processing_inside_dedup_window_is_indexed, and this
  branch's test_follow_up_reserved_before_missing_file_check_is_queued.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fstrasz

fstrasz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Merged main into this branch (097499f, an ordinary merge; nothing rebased). #508 had changed the inline processing block in _process_loop, which this branch moves into _process_file_mutation:

Upstream's CI matrix (Ubuntu, macOS, Windows) passed on this commit on my fork before I pushed it here.

🤖 Generated with Claude Code

Brings in the 13 commits merged to main since this branch was last updated, including chunkhound#534's rust-checks fix. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

1 participant