Skip to content

fix: make grounding a lookup instead of an area scan - #72

Merged
hmbanan666 merged 1 commit into
mainfrom
fast-grounding
Nov 18, 2026
Merged

hmbanan666 merged 1 commit into
mainfrom
fast-grounding

Conversation

@hmbanan666

Copy link
Copy Markdown
Owner

Summary

Search grounding was rereading the chunk table once per candidate. The stored-cosine query filtered on scope and model, and SQLite picked embedding_chunks_area instead 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 entire groundingMs.

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 readFloatLE dots, same stem function.

search stderr now also reports centroidMs, frequencyMs, and passageMs. Those three are parts of groundingMs, 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 embedMs is noise. Measured with node /tmp/profile-grounding2.mjs before and after the change.

before after
stored cosines, 101 candidates (808 chunks) 1617 ms 10 ms
score 101 long passages 131 ms 17 ms
groundingMs 3458 178
centroidMs (inside grounding) 79
frequencyMs (inside grounding) 39
passageMs (inside grounding) 59
retrievalMs 982 526
package wall 4568 ms 745 ms
warm vector scan alone 487 ms 447 ms

Prod 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.ts compares 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. explainStoredGrounding reports are deep-equal and strong. The live package reports cosine 0.5 and cosineSource: "vector" (that cosine is not in the retrieval hits, so a missed lookup would change the label).
  • The same file builds 2500 + 2500 records and asserts warm groundingMs < 1000.

Friction

  1. The first synthetic corpus (short notes, one chunk) made document frequency 14 ms and the centroid cache look fine, so the multi-second path did not show up. It only appeared once each record had several chunks: about 15 minutes building the wrong corpus and chasing a df/centroid cache the timing line did not need. Cause: the area index only loses to the primary key when the area is large, and the code comment already called the lookup a point query.
  2. Measurements imported dist/, so every probe waited on tsc. The profile script stayed in /tmp and is not part of the change.
  3. src/embedding-index.ts said 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.md described groundingMs as passage scoring; the centroid and the chunk reread were in the same number. Default testTimeout in vite.config.ts is 20s, which is tight for a 5,000-row fixture; the perf test sets its own 60s timeout.
  4. An EXPLAIN QUERY PLAN assertion on embedding_chunks lookups, 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 the tsc loop between probes.
Open in Web Open in Cursor 

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>
@hmbanan666
hmbanan666 merged commit b85b40a into main Nov 18, 2026
2 checks passed
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.

2 participants