Skip to content

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

Merged
objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-slovhl-38676
Nov 30, 2026
Merged

objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-slovhl-38676

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by Warren · project thread

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 fix(knowledge-turso): a scoped search reads its passage table once a statement; a request past its bound answers TIMEOUT #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

  • 1. Schemas start as Zod; packages/spec holds no runtime logic. (not touched)
  • 2. Every name I added uses the casing for its kind.
  • 3. No formatter ran. eslint is clean on the changed files; the repo-wide pnpm lint was not run locally.
  • 4. One issue, one branch, one pull request. It is stacked on fix(knowledge-turso): a scoped search reads its passage table once a statement; a request past its bound answers TIMEOUT #38693 by the seat's order.
  • 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset.
  • 6. packages/spec touched: not touched.
  • 7. No hand edits to content/docs/references/, packages/*/CHANGELOG.md or content/docs/releases/.
  • 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.ai/code/session_01JQwxbaAVoqUQiPmxMVpLst

…statement; a request past its bound answers TIMEOUT

A search with a record scope or a metadata filter read its WHERE row by
row in the vector leg, the keyword candidates and the keyword count, and
the keyword candidates were computed twice in one statement. Each was a
pass over the passage table; on a remote libSQL database a scoped
statement went past the 10 s request bound. The WHERE is now read once a
statement into a materialized scope set that every other read looks up
by chunk id, and the keyword candidates are materialized.

A request past requestTimeoutMs answers TIMEOUT (504), "did not answer in
time", instead of SERVICE_UNAVAILABLE "is not ready".

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbaAVoqUQiPmxMVpLst
…scope too narrow for the approximate pass straight to the exact scan

The first statement of a search computes the keyword leg; a wider
approximate pass or the exact scan after it binds the ranks it answered.
A scope with a parent term or a metadata filter has its share estimated
from 256 rows read by rowid and, under the record scope's cut, goes
straight to the exact scan.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbaAVoqUQiPmxMVpLst
@github-actions github-actions Bot added 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 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

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 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 — 3 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 751e83941b805879e98a210539d242511021a505 → packageMentionDocs.

Which tree this was computed on

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

node scripts/docs-audit/affected-docs.mjs --json 751e83941b805879e98a210539d242511021a505

⚠️ 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#2 · os-warren · session_01JQwxbXyLoBzNYY1U8SNPFu

维护者速读

席位核对

ACCEPT · L1 · area o2-A · Head-sha 40596897a680c5eec337ae581e10f48d979ede8f · 2026-11-30T05:51Z

  • Face: plugin-internal only, not governed (check-governed-merges --pr 38697). Clause-②: no / Metadata-protocol: none / @objectstack/knowledge-turso patch accepted.
  • Design read against the diff (base claude/project-thread-slovhl):
    • The first statement of a search computes the keyword leg. A later statement (the wider pass or the exact scan) binds those ranks through json_each instead of matching the terms again.
    • A scope with a parent term or a metadata filter gets its share estimated from a 256-row sample (scopeSampleSql). Under the record-scope cut, it goes straight to the exact scan.
    • Both directions of a wrong estimate are safe for hits. Too low takes the exact scan, which is slower but exact. Too high takes the approximate pass, which falls back as before.
  • Cause 3 (embedding the question again on the service's second round) is not built here. The seat ruled A in amendment 7269702419. It is the card's remainder, so this PR is Part of #38676, and it waits for the maintainer's knowledge pause (2026-11-30T05:39Z) to lift.
  • Pins: 10 red with the source reverted, 301 pass with the fix (report 7269683344). The pins are plan- and call-shaped, not wall-clock.
  • Checks at this head: all green (check suites done 2026-11-30T05:51Z).
  • Changeset read sentence by sentence against the diff: true. It names the remaining double embed.
  • Landing: Tier S (not governed). Stacked on fix(knowledge-turso): a scoped search reads its passage table once a statement; a request past its bound answers TIMEOUT #38693: it retargets to main after fix(knowledge-turso): a scoped search reads its passage table once a statement; a request past its bound answers TIMEOUT #38693 lands, merges main, and then the seat readies and enqueues it when the freeze lifts.

…-slovhl-38676

# Conflicts:
#	packages/plugins/knowledge-turso/README.md
#	packages/plugins/knowledge-turso/src/__tests__/scope-plan.test.ts
#	packages/plugins/knowledge-turso/src/turso-adapter.ts
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#2 · os-warren · session_01JQwxbXyLoBzNYY1U8SNPFu

2026-11-30T08:09Z · Landing · seat ACCEPT 7270081145 at 40596897a6 carried to 7eed5616d6. The head moved by merge only: the PR was retargeted to main after #38693 merged, and 7eed5616d6 merges origin/main into the branch. The PR's diff against main is the same six files with the same line counts as d3c750a86e..40596897a6, the commit the ACCEPT read (+175/−59). All checks green (39 passed, 16 skipped, 0 failed), mergeable_state clean.

  • The release freeze does not apply: Warren (os-warren) answered in os-warren's fleet project timeline at 2026-11-30T07:08Z 「目前已经开发中的不用冻结」, which exempts work already in development.
  • Tier S (not governed, 234 lines): ready for review and auto-merge in this stroke. The queue re-runs every check on the merged tree.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 30, 2026 08:09
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 80ddfb5 Nov 30, 2026
56 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/project-thread-slovhl-38676 branch November 30, 2026 08:33
Nenzyz pushed a commit to Nenzyz/objectstack that referenced this pull request Dec 1, 2026
… mode (objectstack-ai#38721)

<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8QCBjsNDZa4iwo35VN5fKKg)_

Part of objectstack-ai#38692

Clause-②: yes (widening)
Metadata-protocol: none

## 维护者速读

- 使用 turso 存储的知识源可以按选定方式检索:只按意思、只按字词或两者结合;不选仍是两者结合,结果与现在逐位相同。
- 属于知识管理专题「一期 a:检索方式」。
- 出错会是某种方式漏掉或多出段落;未声明方式的知识源不受影响,可直接撤回。
- 无。

## Stack

Base: `main`. objectstack-ai#38697, the spec PR objectstack-ai#38720 and the phase-0 ADR note objectstack-ai#38714
are all merged and `main` is merged in, so this diff is the adapter's
own 6 files. It lands in the same release as the service PR objectstack-ai#38722,
which is the first reader of the mode.

## What changes

- `knowledge-turso` reports `capabilities.retrieval = { modes:
['vector', 'keyword', 'hybrid'], default: 'hybrid' }`.
- A search runs only the legs its mode names, in SQL: `keyword` binds no
vector and never calls the embedder (nor reads the one-bit approximate
index); `vector` drops the FTS5 leg.
- A single-leg answer is scored on the same RRF scale: rank n scores
61/(60+n) after normalising, so the first hit is 1. `hybrid` is
unchanged; `fuse(rows, topK, legs)` keeps the two-leg expression
bit-identical.
- README "Retrieval modes" paragraph, protocol doc §4.2 sentence, patch
changeset.
- A mode the store does not list is refused (`400 VALIDATION_ERROR`)
before anything is embedded or read, rather than run on the one-leg
scale (review S1). `Clause-②: yes (widening)`: the exported adapter's
`capabilities` grows `retrieval` (review M1).

## Tests

- New `retrieval-modes.test.ts` on a real libSQL store: legs per mode,
embedder calls, tie order by chunk id, single-leg scores, keyword never
reads `vector1bit(` while vector does, hybrid fusion pinned bit-for-bit.
- Contract suite with `implements.retrieval: true`.
- `knowledge-turso`: 319 passed at 2496e51 (the refused-mode case
added); typecheck clean. Ablation: keeping the keyword leg under
`vector` turns 3 cases red, restored.

## Acceptance notes

- With the service PR applied on top, the full turso suite (326,
including the service-level mode cases) passes.

## Verification

- Two-minute check: `pnpm --filter @objectstack/knowledge-turso exec
vitest run src/__tests__/retrieval-modes.test.ts` → all pass.

## Human Minimum

- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [ ] 4. One issue, one branch off an up-to-date `main`, one pull
request. (Stacked: see "Stack" above.)
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [ ] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed. (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.
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01JQwxbaASqBozmmj1n5tbUf

<!-- signed: repo:objectstack#2 · os-warren ·
session_01JQwxbaASqBozmmj1n5tbUf -->

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

2 participants