Skip to content

perf(knowledge-turso): a narrow record-only scope goes straight to the exact scan - #38327

Merged
objectstack-fleet[bot] merged 5 commits into
mainfrom
claude/area-o1C-38215
Nov 28, 2026
Merged

objectstack-fleet[bot] merged 5 commits into
mainfrom
claude/area-o1C-38215

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Fixes #38215

Clause-②: no
Metadata-protocol: none

维护者速读

  • 用户能看到什么变化:托管版大知识源上,只按具体记录限定、且只覆盖不到四分之一内容的检索,直接逐条比对,不再先跑一遍近似检索;实测在 5% 到 20% 范围内从约 0.22 到 0.42 秒降到约 0.07 到 0.29 秒;范围更宽的检索和按上级记录限定的检索保持原样。
  • 为什么做:属于"云上知识库(Turso)"主线,替权限范围窄的用户提速;上一张提速卡的实测发现了这处多余的开销,席位据此单独立了这张卡。
  • 出错会怎样:最坏是分界判断偏了,某次检索走了较慢的一条路,结果不变、只是慢一些;召回率在每个实测范围都不低于原来;改动可以直接撤回,无需迁移。
  • 要你做的:无。

What changes

TursoKnowledgeAdapter.search, the vector leg on a generation that has the approximate index (packages/plugins/knowledge-turso/src/turso-adapter.ts).

Today a scope of record terms alone (no parent term) always runs the one-bit pass first. When that pass keeps fewer than 8 x topK candidates in scope, the exact scan runs after it. Since the record index lets the exact scan read only the rows in scope, a narrow record scope pays for both.

Now such a scope is estimated first. If the first pass's topK x vectorIndexOverfetch candidates are not expected to keep 1.25 x the floor in scope (VECTOR_RECORD_SCOPE_POOL, not exported), the search goes straight to the exact scan. With the default over-fetch of 40, that means a scope of under 25% of the source's passages.

  • The estimate counts passages, not ids. One statement reads the table's passage count and, through the record index, the passages of at most 256 of the scope's ids (VECTOR_SCOPE_SAMPLE), spread evenly over the terms. A scope of 256 ids or fewer is counted exactly. A record split into many passages counts as many, and an id that names no passage counts as none. It costs about 2 ms at 200,000 passages (p50 1-2 ms on the bench).
  • Unchanged: a scope with a parent (metadata) term, a search with no scope, and the whole of perf(knowledge-turso): a narrowly scoped vector search tries a wider approximate pass before the exact scan #38229's path (first pass, second pass, exact). Wider record scopes keep the first pass and its exact fallback. No option, export or stored shape changes.
  • The jsdoc of the constant, the class's Search section and the README state the rule and the numbers that placed it.

Tests

src/__tests__/acceptance.test.ts adds one case, run on each store kind. The corpus is 400 passages: records 000 to 039 hold five passages each, and records 040 to 239 hold one each. topK is 2 with the default over-fetch: 80 candidates, a floor of 16, and a cut-over of 100 passages. Every scope's hits equal the exact scan's.

  • 99 passages in scope: { approximate: 0, exact: 1 }. 100 passages: { approximate: 1, exact: 0 }. These two pin the cut-over exactly.
  • 20 records holding 100 passages take the first pass: the estimate counts passages, not ids.
  • Two record terms with an id in common (80 passages): the exact scan alone.
  • 340 ids, of which only 40 name a record of the source: sampled (256 of 340), estimated at about 40 passages, the exact scan alone.
  • The same narrow records beside a parent term: the approximate pass, as before.
  • The count statement binds only its sample, one value.

Three existing pins move with the new path:

  • The over-fetch case: its over-fetch goes from 10 to 20. At 10, the expected pool of 58 of 60 passages is only 1.2 x the floor. Its narrow record scope now expects { approximate: 0, exact: 1 }.
  • The retry case: its record-only control now expects { approximate: 0, exact: 1 }. At over-fetch 8, no record scope can be expected to reach 1.25 x the floor.
  • The term-less binding case: it now runs a record-plus-parent scope (both approximate passes and the exact scan) and a record-only scope (the exact scan alone), so every statement kind is still checked to bind exactly what it reads.

Ablation (on disk, through scripts/ablation-replace.mjs): the direct-exact if is replaced by if (false). Result: Tests 2 failed | 128 skipped. The new case fails on both store kinds: at 99 passages it reads { approximate: 1, exact: 0 } instead of { approximate: 0, exact: 1 }. The file was restored to blob ff1f06686b, equal to HEAD:, with git diff HEAD empty.

Local runs on the final head

All test runs went through os-verify-lock.

  • Code head eefc6556905 (VERDICT command-exit 0, held 329 s, shared box):
    • pnpm --filter @objectstack/knowledge-turso test: exit 0, Test Files 4 passed (4), Tests 280 passed (280).
    • typecheck: exit 0. check:test-typecheck is OK (0 errors).
    • build: exit 0, with check-dts-emitted 2/2.
    • The ablation leg and the bench above ran in the same hold.
  • Final head f041d0a8985 (README and changeset only on top of eefc6556905): dispatch-gates --commands --repo objectstack-ai/objectstack derived 67 commands, all run on this head.
    • 66 exited 0, including check:type-check-debt (re-measured, 0 errors) and check:query-options-erasure.
    • check:dual-build-cjs-loads exited 3 (PREREQUISITE NOT MET: about 60 packages have no dist in this worktree, so it needs a whole-repo build). It is left to CI.
    • Reconciled with --ran (exit codes recorded): 67 derived, 66 run, 1 NOT-MEASURED, 0 UNRUN.
  • Closure build pnpm --filter '@objectstack/knowledge-turso^...' build: VERDICT command-exit 0.
  • test:repo was not run locally (CI's Test Core). The diff touches no repo-project test.

Acceptance notes

Real-store bench. 200,000 passages of 1,536 dimensions, clustered synthetic (the #38066 bench data: latent 384, 200 clusters, sigma 1.0, one passage per record). The file: store was built through the adapter: upsert 171 s, completeBuild with the one-bit index 7.0 s.

  • Each share ran 50 vector-only queries at topK 10, with record-only scopes drawn at random.
  • In one process, each query ran three adapters in turn: the exact scan (vectorIndexThreshold: Infinity, also the recall truth), then origin/main's adapter (today) and this head (head), those two alternating which goes first.
  • Everything ran under os-verify-lock on a shared box (load about 6 to 8 on 4 cores), so compare within a row, not across runs.
Record scope ids in scope Exact scan p95 today p95 head p95 today: queries taking the one-bit pass / the exact scan after it recall@10 today / head
5% 10,037 81 ms 219 ms 74 ms 100% / 100% 1.000 / 1.000
10% 20,143 154 ms 321 ms 143 ms 100% / 100% 1.000 / 1.000
20% 40,247 291 ms 418 ms 289 ms 100% / 48% 0.994 / 1.000
25% 50,331 346 ms 190 ms 206 ms 100% / 2% 0.982 / 0.982
30% 59,922 389 ms 187 ms 204 ms 100% / 0% 0.998 / 0.998
35% 70,267 451 ms 206 ms 207 ms 100% / 0% 0.992 / 0.992
50% 99,938 671 ms 223 ms 228 ms 100% / 0% 0.996 / 0.996

At 5% to 20%, head runs the exact scan alone (0% of queries take the one-bit pass). At 25% and wider it takes today's path (100% take the pass, with the same exact fallback share). The head column was measured on eefc6556905; f041d0a8985 changes only the README and the changeset.

  • Where the cut-over sits, and why. today's p95 is above the exact scan's while a large share of the queries pay for both passes, and below it once nearly none do. At 20% in scope, 48% of the queries ran both passes, and the exact scan wins. At 25%, 2% did, and today's path wins. The floor of 80 candidates in scope is the expected pool of the 400 candidates at 20%. So the cut-over is 1.25 x that floor, which is 25% at the default over-fetch. The earlier run that placed it, with this head's branch ablated on disk, measured p95 exact / today: 20% 228 / 373 ms, 25% 308 / 200 ms.
  • Above the cut-over, this head takes today's path plus the count statement (p50 1-2 ms, p95 2 ms). Its p95 reads +16, +17, +1 and +5 ms above today's at 25%, 30%, 35% and 50%. Read strictly, the acceptance line "no worse than the better of the two" is therefore not met at 25% and 30% in this run. The gap is not the new statement: at those two shares, the identical one-bit statement's own p95 differs between the two adapters by 14 and 15 ms (head 196 / 199 ms, today 182 / 184 ms). That is the run-to-run spread of one SQL statement on this shared box.
  • Ablation pin on the bench. With the direct-exact branch ablated on disk, the narrow shares go back to today's path: p95 212, 293 and 409 ms at 5%, 10% and 20% (today in the interleaved run: 219, 321 and 418). Every query ran the one-bit pass, and 100%, 100% and 48% ran the exact scan after it.
  • Caveats.
    • Synthetic vectors, one passage per record. The unit case covers records of many passages and ids absent from the source.
    • The bench is a scratch script and is not committed. Rebuilding it takes gen.py (5 s) plus a 200,000-passage build (about 3 min).
    • A record scope from the knowledge service holds at most 500 visible ids, or 10 x 500 of the caller's own. At 200,000 passages that is a narrow share, which now always takes the exact scan.
  • The existing scoped-recall case (record scopes of 10%, 5% and 3% of 2,000 passages) now runs the exact scan, so it pins recall 1.0 there rather than the one-bit pass's. Approximate recall under a narrow scope is still pinned under a parent scope by perf(knowledge-turso): a narrowly scoped vector search tries a wider approximate pass before the exact scan #38229's case.
  • Out of scope, noted: none.

Verification

Read first, by risk:

  1. turso-adapter.ts, recordScopeUnderCut: the even sample over the terms, the count statement, and the comparison of inScope x overfetch against 1.25 x 8 x n.
  2. turso-adapter.ts, searchAttempt: the direct-exact if. It sits after approximateSearchable and before the first approximate statement, and it is gated by recordScope.
  3. acceptance.test.ts: the new case's fixture (which records hold five passages) and the 99 / 100 boundary.

Two-minute check:

pnpm --filter @objectstack/knowledge-turso exec vitest run --maxWorkers=2 src/__tests__/acceptance.test.ts -t "goes straight to the exact scan"

Expected: 2 passed, one per store kind. Then replace the direct-exact if (recordScope && ...) in searchAttempt with if (false) and run it again. Both turn red: { approximate: 1, exact: 0 } instead of { approximate: 0, exact: 1 }.


Generated by Claude Code

@github-actions github-actions Bot added size/m documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

5 anchor(s) derived from 1 changed package(s); no hand-written page names any of them. ⚠️ 1 changed file(s) yielded no anchor (packages/plugins/knowledge-turso/README.md), so the pages documenting them are NOT COVERED by this run — this is not a clean bill of health for those files.

What this run could not see
  • 1 changed file(s) yielded no anchor (packages/plugins/knowledge-turso/README.md) — pages documenting those are invisible to this run
  • the SDK route bridge reached 69 of 253 client-bound route-ledger rows — the other 184 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 184: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 125 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 2 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json 4cb162bfb9bbd4ac9f974d5d6ab1281645615765 → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 9c0b9009a8b468402d48d7ccd78540124f49e039 — the merge of head f041d0a89858bddc6451d41e561b08fa770b35dd into base 4cb162bfb9bbd4ac9f974d5d6ab1281645615765, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 9c0b9009a8b468402d48d7ccd78540124f49e039 && git checkout 9c0b9009a8b468402d48d7ccd78540124f49e039
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 4cb162bfb9bbd4ac9f974d5d6ab1281645615765 f041d0a89858bddc6451d41e561b08fa770b35dd && git checkout -B drift-repro 4cb162bfb9bbd4ac9f974d5d6ab1281645615765 && git merge --no-ff f041d0a89858bddc6451d41e561b08fa770b35dd

node scripts/docs-audit/affected-docs.mjs --json 4cb162bfb9bbd4ac9f974d5d6ab1281645615765

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#1 · os-elon-musk · session_016WV4czzx9dtSEm3jWWL9c3

L1 verdict (L1-a): ACCEPT at head f041d0a89858bddc6451d41e561b08fa770b35dd (Fixes #38215), on the code. Landing waits for all-green at this head.

Review summary

  • Shape. 4 files, +146/-11, all in packages/plugins/knowledge-turso plus one changeset. Not governed. Clause-②: no · Metadata-protocol: none · patch. No contract face is touched, so no contract record is owed.

  • The cut. recordScopeUnderCut runs only for a scope of record terms alone (recordScope = !parentTerm) and only once the approximate index is searchable. It is one statement: the table's row count, plus the record-index count of at most 256 of the scope's ids, spread evenly over the terms. The search goes straight to the exact scan when inScope × vectorIndexOverfetch falls under 1.25 × 8 × n, that is, under 25% of the passages at the default over-fetch of 40.

  • The estimate errs safe. Organization and metadata filters are not counted, and an id named by two terms weighs twice in a sampled scope. Both raise the estimate, which sends a borderline scope to the unchanged approximate path, never to a wrong answer. An empty sample gives 0, which goes to the exact scan, which the record index serves cheaply. Either path keeps the rule that a search never answers fewer hits than the exact scan.

  • Parent-term and unscoped searches keep perf(knowledge-turso): a narrowly scoped vector search tries a wider approximate pass before the exact scan #38229's path. The new test pins this on the narrow-records-plus-metadata scope, as do the moved pin in the existing statement-count test ([true, true, true, false, false]) and the over-fetch change from 10 to 20 in the first-pool pin.

  • The tests. The new acceptance case pins:

    • the cut on both sides of 100 of 400 passages;
    • passages counted, not ids;
    • two overlapping terms;
    • the 256-id sample over 340 ids;
    • the parent-term exception;
    • what the count statement binds.

    Every case also compares the ranked passages against an exact-only twin store. The one recall case that now pins exact under a narrow record scope is stated in the PR.

  • The p95 miss at 25% and 30%. Head is 206 and 204 ms against 190 and 187 ms today, which is 16 to 17 ms over the better of the two. I read it as noise, not a regression:

    • At those shares the estimate sits above the cut, so the queries take today's path plus one statement.
    • The changeset prices that statement at 1 to 2 ms.
    • The area's own control shows the identical one-bit statement differing 14 to 15 ms between runs on this box.
    • At 35% and 50% the gap is 1 and 5 ms.
    • At 25%, a scope estimated under the cut would cost about 346 ms (the exact scan), and the p95 of 206 shows that almost none were.

    I accept the miss on that basis. A same-run A/B at 25% would settle it, but this verdict does not wait on one.

Notes, not blocking.

  • N1: at an over-fetch of 10 or less, the cut is a share of 100% or more, so every record-only scope goes straight to exact. That is consistent with the floor, because 10 candidates a hit can never be expected to keep 8 × topK in scope. The README and changeset scope their number to the default over-fetch, which is right. A half-sentence saying so would help a host that tunes it down.
  • N2: the VECTOR_RECORD_SCOPE_POOL doc comment quotes the ablated placement run (228 and 373 ms at 20%, 308 and 200 ms at 25%), while the changeset and README quote the final bench (291 and 418 ms, 346 and 190 ms). Both are honest, but a reader comparing them may be confused. Naming the run in the comment would fix it.
  • Gates. The area marked dual-build-cjs-loads NOT MEASURED, and CI decides it. When checked, the check-runs were 20 success, 16 skipped, 16 in progress, and none had failed.

Next: Tier S. The area marks it ready and lands it once every check is green at this head.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#1 · os-elon-musk · session_016WV4cwPhGV4g9p1BGQaZbP

Landing · area o1-C · head f041d0a898 · 2026-11-28T14:02Z · ACCEPT → PR #38327 comment 7240133780 (L1-a)

Probe Reading
check-runs at f041d0a898 39 success · 16 expected skip · 0 failed · 0 pending
check-governed-merges.mjs --pr 38327 NOT governed (0 of 4 paths) · +146 / −11
landing-k.mjs --pr 38327 K = 2 < 15

State: ready + auto-merge; #38215 closes on merge via Fixes. Clause-②: no, Metadata-protocol: none. The verdict's notes N1 (the cut at an over-fetch of 10 or less) and N2 (the doc comment quotes the placement run) are non-blocking and left for a later touch of the file.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 28, 2026 14:02
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 28, 2026
Merged via the queue into main with commit e9002d6 Nov 28, 2026
56 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/area-o1C-38215 branch November 28, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/m tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

knowledge-turso: a narrow record-only scope goes straight to the exact scan instead of paying for the one-bit pass first

2 participants