fix(retrieval): ensure store initialization before hybrid routing#563
fix(retrieval): ensure store initialization before hybrid routing#563Robinzhh wants to merge 1 commit intoCortexReach:masterfrom
Conversation
AliceLJY
left a comment
There was a problem hiding this comment.
LGTM — clean, minimal fix for a real cold-start race.
What I verified:
-
Race condition is real:
retrieve()readsstore.hasFtsSupportfor routing before callingvectorSearch()/bm25Search(), which are the ones that internally callensureInitialized(). So on first call,hasFtsSupportcould still befalsewhile init is in flight — correctly diagnosed. -
Fix is correct: Adding
await this.store.ensureInitialized()before the routing check guaranteesftsIndexCreatedreflects the actual FTS state. -
No performance impact: After first init,
ensureInitialized()hits theif (this.table) returnguard — effectively a single property check, zero overhead on subsequent calls. -
No concurrency issue: The existing
initPromisepattern inensureInitialized()correctly coalesces concurrent callers into a single init. -
Visibility change is appropriate:
ensureInitialized()was already the standard pattern for all store public methods. Exposing it to the retriever layer is a natural extension.
|
Thanks for identifying this cold-start race — the problem is real and worth fixing. That said, CI is currently red and there are a few things to address before this is Must fix
Nice to have
Fix the above and this should be straightforward to merge. |
Summary
This fixes a cold-start race in hybrid retrieval.
On the first retrieval after startup, the store may still be finishing initialization
(particularly FTS setup). In that window,
hasFtsSupportcan still read asfalse,so
retrieve()incorrectly routes to vector-only retrieval instead of hybrid retrieval.This patch makes
retrieve()await store initialization before making the routing decision.Changes
ensureInitialized()onMemoryStoreawait this.store.ensureInitialized()at the start ofretrieve()before hybrid vs vector-only routing is decided
Root cause
The issue was not caused by BM25 itself, CLI formatting, or retrieval filtering.
The actual problem was timing:
hybridstore.hasFtsSupportcould still befalseon the first callValidation
Validated locally with a minimal reproduction:
Scope
This PR only contains the minimal fix for the hybrid cold-start routing race.
It does not include unrelated local changes such as autoCapture, smart-extractor,
or other workspace-only modifications.