Repository navigation
Commit 69bb8ae
perf(knowledge-turso): a narrowly scoped vector search tries a wider approximate pass before the exact scan (#38229)
Fixes #38066
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:托管版大知识源上,按上级记录(例如某个客户名下)限定、只覆盖一到两成内容的检索,从约 0.9 秒降到约 0.35
秒;按具体记录限定的检索保持原样。
- 为什么做:属于“云上知识库(Turso)”主线,替检索范围窄的用户提速;席位已按实测把范围收窄到这一种限定方式。
- 出错会怎样:最坏是多做一次近似检索后照旧逐条比对,结果与以前一致、只慢约 0.2 秒;召回率实测与原规则相同;改动可以直接撤回,无需迁移。
- 要你做的:无。
## What changes
`TursoKnowledgeAdapter.search`, the approximate vector leg
(`packages/plugins/knowledge-turso/src/turso-adapter.ts`).
Today, the leg runs the exact scan when the filter leaves fewer than 8 x
`topK` of its `topK x vectorIndexOverfetch` candidates in scope. Such a
leg now first runs one more approximate pass over 10 times the
candidates (`VECTOR_RETRY_POOL`, not exported), with the same `WHERE`
and the same floor. It keeps that answer only if it clears the floor;
otherwise the exact scan answers, as before.
The second pass is tried only when both hold:
- **The scope cannot use the record index.** That means no scope, or a
scope with at least one parent (`metadata`) term. A scope of record
terms alone keeps today's path, because since #38160 its exact scan
reads only the rows in scope through the record index. This is the
seat's ruling B on this card, comment 7236023531 (option A of
os-dev-report 7235999186). The ruling also files a separate card for a
narrow record-only scope that skips the one-bit pass; this PR does not
touch that path.
- **The first pool says the wider pass can clear the floor**, that is
pool x 10 is at least 8 x `topK`. A scope too narrow for that goes to
the exact scan without a pass that could not answer.
A failed second statement is treated like a failed first one: the index
memory is forgotten and the exact scan answers. No option, export or
stored shape changes. The README Search section and the jsdoc carry the
rule and the numbers.
## Tests
`src/__tests__/acceptance.test.ts` gets a new case, run on each store
kind, with an explicit 30 s timeout. The corpus is 400 passages with tag
lists that a parent scope term matches; `topK` is 2 and over-fetch 8, so
there are 16 candidates, a floor of 16 and 160 for the second pass.
Every scope's hits equal the exact scan's.
- `wide` (the 2 best plus half the far passages): `{ approximate: 2,
exact: 0 }`. The bound candidate counts are `[16, 160]`, and each pass
binds exactly the values it reads.
- `short` (5 passages): `{ approximate: 2, exact: 1 }`. Both passes fall
short, then the exact scan answers.
- `thin` (one far passage): `{ approximate: 1, exact: 1 }`. The pool is
too thin for a second pass.
- **Record-only control** (the `wide` passages as record ids): `{
approximate: 1, exact: 1 }`. There is no second pass.
The two existing pins that run a record scope under the floor, the
over-fetch case and the term-less binding case, keep their expectations
unchanged. That is the record-only path staying as it is.
**Ablation**, an on-disk edit replacing the second pass's condition with
`false`: `Tests 2 failed | 126 skipped` (the new case on both store
kinds, `{ approximate: 1, exact: 1 }` instead of `{ approximate: 2,
exact: 0 }`). The file was restored to a blob equal to `HEAD:` (`git
hash-object` 5573a5c on both sides), with an empty `git status`.
<details><summary>Local runs on the final head</summary>
All ran on head `07a84d2e10` (origin/main `2d95781690` merged), under
`os-verify-lock` (`VERDICT command-exit 0`, held 1,194 s).
- **Typecheck:** `pnpm --filter @objectstack/knowledge-turso typecheck`
exits 0. `check:test-typecheck` is OK (0 errors).
- **Tests:** `pnpm --filter @objectstack/knowledge-turso exec vitest run
--maxWorkers=2` exits 0, with `Test Files 4 passed (4)` and `Tests 278
passed (278)`.
- **Derived gates:** `dispatch-gates --commands` derived 67 commands at
`07a84d2e10`. 66 ran, and each exited 0.
- `check:dual-build-cjs-loads` was left to CI. A first pass refused it
with exit 3 (`PREREQUISITE NOT MET`: 60 packages have no `dist` in this
worktree; it needs a whole-repo build).
- Reconciled with `--ran`: `67 derived, 66 run, 0 NOT-MEASURED, 1
UNRUN`. The one UNRUN is that gate.
- **Tests on `5447b42c76`:** the run there found a TS18048 in the new
test's fixture (`d.metadata` possibly undefined). `07a84d2e10` fixes it.
</details>
## Acceptance notes
**Real-store bench, restated to the parent scope by the ruling.**
200,000 passages of 1,536 dimensions, clustered synthetic (the data
model of `scoped-recall-floor8-retry-sim.py`: latent 384, 200 clusters,
sigma 1.0). The `file:` store was built through the adapter: upsert 149
s, `completeBuild` with the one-bit index 8.1 s, 1.72 GB.
- Each share ran 50 vector-only queries at `topK` 10. Each query was run
by the exact scan (an adapter at `vectorIndexThreshold: Infinity`, which
is also the recall truth) and by the adapter under test, interleaved.
- The parent scope is one `metadata` term over a `bucket` key holding
10, 15 or 20 of 100 buckets.
- "Floor 8" is this head with the second pass ablated on disk, which
equals `main`'s rule.
- Everything ran under `os-verify-lock` (`VERDICT command-exit 0`) on
head 6c530b0; later commits change only a test line. The box was
shared (load 1.6 to 3.3 on 4 cores), and each row's exact column is that
same run's.
| Parent scope | Exact scan p95 | Floor 8 p95 | Second pass p95 |
recall@10, floor 8 / second pass | Sent exact, floor 8 / second pass |
|---|---|---|---|---|---|
| 10% | 810 / 848 ms | 947 ms | **351 ms** | 1.000 / 1.000 | 100% / 0% |
| 15% | 824 / 907 ms | 948 ms | **328 ms** | 1.000 / 1.000 | 100% / 0% |
| 20% | 910 / 908 ms | 974 ms | **337 ms** | 0.986 / 0.986 (worst 0.8 on
both) | 44% / 0% |
- The exact column gives two readings: the one from the floor-8 run,
then the one from the second-pass run.
- **Ablation pin on the bench.** With the second pass removed, the share
of queries sent exact goes back up from 0% to 100%, 100% and 44%.
- **Statement costs (p50):** the first one-bit pass (400 candidates)
takes 115 to 119 ms, and the second (4,000) takes 168 to 175 ms.
- **The recall at 20% is the first pass's.** The 56% of queries that
clear the floor in the first pass are answered there, under both rules.
The second pass answered the other 44% with the exact scan's top 10.
**Record-scope control** (this head): the second pass never runs (0 of
150), and the path is the floor-8 rule.
| Record scope | Exact scan p95 | This head p95 | Sent exact |
|---|---|---|---|
| 10% | 135 ms | 262 ms | 100% |
| 15% | 177 ms | 345 ms | 98% |
| 20% | 269 ms | 422 ms | 48% |
Under a record scope, the ungated second pass measured 333, 313 and 407
ms p95 against 128, 207 and 265 ms for the exact scan (os-dev-report
7235999186). That is why the record-only path is left alone here. The
floor-8 path's own excess over the exact scan in the table above belongs
to the separate card the ruling files.
- **Caveats.**
- Synthetic vectors only, as in #38056.
- The bench is a scratch script and is not committed. Its data was
deleted after the run; rebuilding it is `gen.py` (5 s) plus a
200,000-passage build (about 160 s).
- A hybrid query also re-runs its keyword leg in the second statement.
This bench measured vector-only queries.
- **Out of scope, noted:** none beyond the ruling's separate card.
## Verification
Read first, by risk:
1. `turso-adapter.ts`, `searchAttempt`: the `recordScope` flag set in
the scope loop, the `approximate` and `stands` closures, and the order
first pass, second pass, exact.
2. `turso-adapter.ts`, `VECTOR_RETRY_POOL`: the gate: the first pool
times `VECTOR_RETRY_POOL` is at least `VECTOR_MIN_POOL * topK`.
3. `acceptance.test.ts`, the fixture of the new case: which tags hold
which passages.
Two-minute check:
```
pnpm --filter @objectstack/knowledge-turso exec vitest run --maxWorkers=2 src/__tests__/acceptance.test.ts -t "tries 10"
```
Expected: 2 passed, one per store kind. Then, in `searchAttempt`,
replace the whole condition of the `if` that guards
`approximate(candidates * VECTOR_RETRY_POOL)` with `false` and run it
again: both turn red with `{ approximate: 1, exact: 1 }`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent bd5407e commit 69bb8ae
4 files changed
Lines changed: 137 additions & 20 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 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1305 | 1305 | | |
1306 | 1306 | | |
1307 | 1307 | | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
1308 | 1366 | | |
1309 | 1367 | | |
1310 | 1368 | | |
| |||
0 commit comments