Skip to content

perf(knowledge-turso): a narrowly scoped vector search tries a wider approximate pass before the exact scan - #38229

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

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

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

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 feat(service-knowledge): a narrow caller's search ranks only the records they may read #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 5573a5c3e2 on both sides), with an empty git status.

Local runs on the final head

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.

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.

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

…oximate pass before the exact scan

Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
Co-authored-by: Claude <noreply@anthropic.com>
…ecord terms alone

Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
Co-authored-by: Claude <noreply@anthropic.com>
…roximate pass before the exact scan

The second pass runs only when the record index cannot serve the exact
scan: no scope, or a scope with a parent term. Bench numbers, README and
a patch changeset.

Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
Co-authored-by: Claude <noreply@anthropic.com>
@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

2 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 252 client-bound route-ledger rows — the other 183 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 183: 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; 124 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 2d7bf92cce918aa1553f0efa1b9d016fa9495a3f → packageMentionDocs.

Which tree this was computed on

This run read content/docs from e7a6d2cf76caf6a5e0d4d94c1c0e4d2ac82c97ec — the merge of head 07a84d2e105cdac7bd81453fa57be88e8ed6bd6e into base 2d7bf92cce918aa1553f0efa1b9d016fa9495a3f, 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 e7a6d2cf76caf6a5e0d4d94c1c0e4d2ac82c97ec && git checkout e7a6d2cf76caf6a5e0d4d94c1c0e4d2ac82c97ec
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 2d7bf92cce918aa1553f0efa1b9d016fa9495a3f 07a84d2e105cdac7bd81453fa57be88e8ed6bd6e && git checkout -B drift-repro 2d7bf92cce918aa1553f0efa1b9d016fa9495a3f && git merge --no-ff 07a84d2e105cdac7bd81453fa57be88e8ed6bd6e

node scripts/docs-audit/affected-docs.mjs --json 2d7bf92cce918aa1553f0efa1b9d016fa9495a3f

⚠️ 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 07a84d2e105cdac7bd81453fa57be88e8ed6bd6e (Fixes #38066, option A of the seat's ruling 7236023531). Full read.

Review summary

Shape.

  • The PR touches 4 files, +137/-20, and is NOT governed.
  • It declares Clause-②: no and Metadata-protocol: none, and the face reader decides no contract face, so no contract review is owed.
  • A @objectstack/knowledge-turso patch is the right level: no option, export or default changes.

Code read against the ruling.

  • When the first approximate pass does not stand, the leg makes ONE second approximate pass over VECTOR_RETRY_POOL (10) times the candidates. The first pass stands when its pool is at least 8 × topK and it returns at least topK vector hits.
  • The wider pass is judged by the same stands floor. If it falls short or throws, the exact scan answers, so the answer is never worse than today's.
  • The pass is gated two ways:
    • It is skipped when the first pool × 10 cannot reach the floor, which saves a pass that could not answer.
    • It is skipped under a scope of record terms alone (recordScope), whose exact scan the record index serves. No scope, an organization filter alone, or any parent term keeps the pass eligible. This also covers the narrowed record term a filtered caller now gets from the service.
  • Either pass that throws drops the index memory for that generation instance, exactly as before, and then runs exact.
  • The candidate count is bound only on the approximate statement.

Evidence.

  • The new acceptance case runs on both store kinds with a 30 s timeout, and its ablation goes red. The two record-scope pins are unchanged.
  • The bench numbers in the changeset and jsdoc agree with the dev's report: p95 351/328/337 ms against 848/907/908 ms for the exact scan, recall at the floor's, and 0 second passes in 150 record-scope queries.
  • The README sentence is true of the diff.

Gates. CI on this head at 08:43Z: 11 success, 0 failed, 13 pending. dual-build-cjs-loads was NOT MEASURED locally, and CI covers it. This ACCEPT is on the code. The landing waits on every check at this head concluding green, which the area reads before it lands.

Next: the area lands it on all-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 07a84d2e10 · 2026-11-28T09:10Z · ACCEPT → PR #38229 comment 7236761608 (L1-a), under the seat's ruling 7236023531

Probe Reading
check-runs at 07a84d2e10 37 success · 16 expected skip · 0 failed · 0 pending
check-governed-merges.mjs --pr 38229 NOT governed (0 of 4 paths) · +137 / −20
landing-k.mjs --pr 38229 K = 10 < 15

State: ready + auto-merge; #38066 closes on merge via Fixes. Clause-②: no, Metadata-protocol: none. The narrow record-only scope that goes straight to exact is follow-up card #38215.

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 scoped vector search under the 8 x topK floor tries a second, wider approximate pass before the exact scan

2 participants