Repository navigation
Commit 80ddfb5
perf(knowledge-turso): compute the keyword leg once a search; send a scope too narrow for the approximate pass straight to the exact scan (#38697)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8Es8AjkqFCZ7RyXPBkdgAD3)_
Part of #38676
**Stacked on #38693.** The base is that PR's branch, so this diff shows
only #38676's commit. After #38693 lands, main is merged in and the PR
is retargeted to `main`.
## What changes
`packages/plugins/knowledge-turso` only (`turso-adapter.ts`, `sql.ts`,
tests, README).
1. **The keyword leg is computed once a search.** The first statement of
a search computes it, as before. When the approximate pass falls short
and a wider pass or the exact scan follows, that statement binds the
keyword ranks the first one answered (`kw AS (SELECT … FROM
json_each(?n))`) instead of matching up to 16 terms again. A statement
binds only the shared values (query vector, scope and filter values,
`topK`), the ranks, and its candidate count.
2. **A scope too narrow for the approximate pass goes straight to the
exact scan, judged by its share of the rows.** Before, only a scope of
record terms alone was checked (through the record index). Now a scope
with a parent (metadata) term, or a metadata `filter`, has its share
estimated by `scopeSampleSql`: 256 rows at evenly spread rowids, with
`min(rowid)`/`max(rowid)` seeks for the bounds, so no pass over the
table. The cut is the record scope's (`VECTOR_RECORD_SCOPE_POOL`: 25% of
the passages at the default over-fetch of 40).
**Not built here: cause 3, the question embedded again on the second
round.** The adapter embeds once per `search()` call. The repeat comes
from `service-knowledge`'s second `searchRound` calling `adapter.search`
again. That file is held by #38660 and is outside this order's surface,
so #38676 stays open for it (see the os-dev report on the card).
## Why
Re-measured on #38693's head (d3c750a), on the same local corpus as
#38693: a `file:` libSQL with 59,621 one-passage records, 1,024
dimensions, approximate index built, and the 16-term Chinese question.
Total per search (statements):
| Search, `topK` 40 | #38693 head | This PR |
|---|---|---|
| 3 policies (0.15%) | 190 ms (approx+kw 120, exact+kw 74) | 80 ms
(sample 5, exact+kw 75) |
| Leaf unit (1.7%) | 310–680 ms (approx+kw, wide+kw, exact+kw) | 150 ms
(sample 5, exact+kw 145) |
| Division (10%) | 570–700 ms (approx+kw 240, wide+kw 330–440) | 230–280
ms (sample 5, exact+kw 225–274) |
| Root (100%) | 810–900 ms (approx+kw) | 820–880 ms (sample 5,
approx+kw) |
| No scope | 620–690 ms | 620–660 ms (unchanged path) |
`topK` 8 shows the same pattern: the leaf unit went from 318–372 to
143–147 ms and the division from 227–507 to 210–214 ms.
- Cause 1 held: every vector statement of one search carried the keyword
CTE.
- Cause 2 held: under a parent-term scope of 1.7% or 10%, the search ran
2–3 statements where 1 exact scan answered.
- Cause 3: the adapter embedded once per call (31 embeds for 31
searches), so the repeat lives in the caller.
## Verification
- **Pins**, all from plans and statement shape, none on wall-clock. With
`turso-adapter.ts` and `sql.ts` reverted to #38693's head, 10 of 301
tests fail:
- `scope-plan.test.ts`: across one search's statements, `MATERIALIZE
cand` appears once, in the first.
- `acceptance.test.ts` retry case, rewritten for the new routing: the
wide pass and the exact scan after a short first pass bind the keyword
ranks (`computed: 1, bound: 1`); a 1% scope goes exact alone; the
sample's plan reads the table by `SEARCH` only, never `SCAN`; each pass
binds exactly what it reads.
- Filter, record-cut and termless cases: a parent term or filter under
the cut now runs `{approximate: 0, exact: 1}`, and a parent-term scope
over the cut still takes the first pass.
- Hits still equal the exact-scan adapter's in every routed case (the
existing `exact` twin comparisons).
- `pnpm --filter @objectstack/knowledge-turso test`: 5 files, 301 tests
pass. `pnpm typecheck` passes. eslint on the changed files is clean.
`check:issue-citations`, `check:doc-authoring`,
`check:objectql-double-limit`, `check:where-matcher` and
`check:empty-changeset` exit 0.
Clause-②: no
Metadata-protocol: none
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(not touched)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran. eslint is clean on the changed files; the
repo-wide `pnpm lint` was not run locally.
- [x] 4. One issue, one branch, one pull request. It is stacked on
#38693 by the seat's order.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: not touched.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
- [ ] The *Before Submitting* list in CONTRIBUTING.md is done. The full
repo-wide suite was not run; CI runs it.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01JQwxbaAVoqUQiPmxMVpLst
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2d6102c commit 80ddfb5
6 files changed
Lines changed: 175 additions & 59 deletions
File tree
- .changeset
- packages/plugins/knowledge-turso
- src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
0 commit comments