Repository navigation
fix: make grounding a lookup instead of an area scan - #72
Merged
Merged
Conversation
Stored-cosine lookup filtered on scope and model, so SQLite used the area index and reread every chunk once per candidate. Look those rows up by primary key and remember short stems. Labels and scores stay the same. Co-authored-by: Nick Kosarev <nick@kosarev.space>
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.
Summary
Search grounding was rereading the chunk table once per candidate. The stored-cosine query filtered on
scopeandmodel, and SQLite pickedembedding_chunks_areainstead of the primary key(kind, record_id, chunk). On a corpus the size of prod (about 2500 sources, 2500 knowledge items, several chunks each) that scan was almost the entiregroundingMs.The lookup now uses the primary key and drops other scopes and models in process. Short token stems are remembered so passage windows do not run Snowball on the same word for every overlapping chunk. Full documents are not cached. Labels and scores are unchanged: same rows, same
readFloatLEdots, same stem function.searchstderr now also reportscentroidMs,frequencyMs, andpassageMs. Those three are parts ofgroundingMs, not extra time. No new MCP tools.Timings
Same synthetic corpus both times: 2500 sources + 2500 knowledge items, about 7.7k characters each, 8 chunks per record (40,000 chunks, 384-d). Warm centroid, so this matches the prod shape where retrieval was already about half a second and grounding was the rest. Fake embedder, so
embedMsis noise. Measured withnode /tmp/profile-grounding2.mjsbefore and after the change.groundingMscentroidMsfrequencyMspassageMsretrievalMsProd line was
tookMs 5544 = embed 13 + retrieval 472 + grounding 4854. The vector scan was already in that 472 ms. Replacing the per-candidate area scan leaves grounding around the 178 ms measured here (centroid stamp, document frequency, passage scoring). A search of that shape lands under 1 s, and grounding under 300 ms.Retrieval was not rewritten. It dropped some because lexical coverage now hits the same stem cache.
Tests
pnpm run check(lint,tsc --noEmit, full suite): 293 tests passed.test/grounding-speed.test.tscompares the old area-index SQL with the new lookup on a fixture that includes a below-0.80 cosine, a wrong scope, and a wrong model. Best scores match exactly.explainStoredGroundingreports are deep-equal andstrong. The live package reports cosine0.5andcosineSource: "vector"(that cosine is not in the retrieval hits, so a missed lookup would change the label).groundingMs< 1000.Friction
dist/, so every probe waited ontsc. The profile script stayed in/tmpand is not part of the change.src/embedding-index.tssaid stored cosines were "point lookups on the chunk primary key" and "cheap next to the full scan" while the SQL could not use that key.docs/search.mddescribedgroundingMsas passage scoring; the centroid and the chunk reread were in the same number. DefaulttestTimeoutinvite.config.tsis 20s, which is tight for a 5,000-row fixture; the perf test sets its own 60s timeout.EXPLAIN QUERY PLANassertion onembedding_chunkslookups, or a checked-in 2,500-doc perf fixture, would have made the next pass skip the false cache design. A runner that typechecks TypeScript in place would have removed thetscloop between probes.