Repository navigation
Commit 58695bb
perf(knowledge): embed the search question once per search, the refill round included (#38798)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8WwwNJsfXjR7KkaXqjQxjFx)_
Fixes #38676
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
-
员工在知识库里搜索、且需要补查第二轮时(例如按单位分组或权限复查后命中不足),问题现在只生成一次向量;以前补查轮会再请求一次嵌入服务(DashScope
约 200 毫秒),搜索结果本身不变,运维和管理员无感。
-
这是“知识检索慢”那张卡的第三个原因(补查轮重复生成问题向量);前两个原因已在之前的改动里修好,这次按你选定的“写进约定,一次提问只转一次”收尾。
-
若出错,碰到的是搜索知识库的员工:最坏情况是某个知识源这次搜索被拒答并写明是哪个来源,不会静默答空;“需要重建索引”这类提示照旧排在前面,不会被嵌入服务的报错盖掉;改动可以直接撤回。
- 无。
## What changes
The question of one knowledge search is embedded at most once for each
adapter instance, by the adapter, and the knowledge service carries it
back to every `search` call of that search, the refill round included.
Built to the ADR-0161 dated note of 2026-12-01 under K8 (ruling record
7296268226 on the card, with its condition: an adapter's own refusal
stays ahead of the embed).
- **Contract (`@objectstack/spec/contracts`)**: `IKnowledgeAdapter`
gains an optional `embedQuery(query)`, which embeds a question exactly
as that adapter's `search` does (its embedder, its query prefix); it
takes no source. `AdapterSearchOptions` gains an optional `queryVector`,
typed as a function of no arguments answering a promise of a read-only
number array (`() => Promise`, answering `readonly number[]`). It is the
function, not the vector. Both docblocks cite the note and no longer say
the knowledge service embeds. Both members additive and optional.
- **Service (`knowledge-service-search.ts`,
`knowledge-service-top-level.ts`)**: `planSearches` gives every target
whose adapter instance has `embedQuery` one slot per instance for the
whole search (`SearchQuestion`). The first `searchRound` call to that
instance puts one memoised function in it (`memoisedQuestion`): its
first call asks the instance's `embedQuery` for the query, and every
later call in that search answers the same promise, a rejected one too.
That one function is handed on every `search` call to that instance in
that search. The service no longer embeds ahead of `search`, no longer
judges whether a call embeds (the earlier `keyword` and "round ranks
nothing" guards are gone) and reads no vector. A rejection is
`embedQuery`'s own error, never wrapped.
- **`knowledge-turso`, `knowledge-pgvector`**: each calls the handed
function at the exact line where its `search` embeds the query today, in
place of that call, after every gate it runs first (nothing to rank,
store and embedder present, ready, active generation, the fingerprint
and width refusal `KNOWLEDGE_REBUILD_REQUIRED`, `topK`, the mode,
`keyword`). Absent: it embeds as before, through its own `embedQuery`.
The width check is unchanged, and the answered array is never changed. A
failed embed therefore fails inside `search`, where it failed on `main`,
with `main`'s code and wording (pgvector's `searchRefusalOf` included).
- **Contract suite (`@objectstack/service-knowledge/testing`)**: an
optional `implements.embedQuery` declaration, checked against the
adapter, and one case for it (below). `knowledge-turso` and
`knowledge-pgvector` declare it.
- **Docs**: §3.4 of `content/docs/protocol/knowledge.mdx` is rewritten
to the note: the function type, where the adapter calls it, what costs
no embedding call, and the suite case.
- An adapter without `embedQuery` (for example `knowledge-memory`) is
handed nothing and searched exactly as before.
- `main` merged first (`9e729ceaad`). #38817's behaviour is kept: an
embedder error reaches the caller unwrapped, and its test file
`unconfigured-embedder-code-38775.test.ts` is unchanged and green (6 of
6).
Review findings closed by this round: M1 by the note (merged as
`13a0d3fedf`); S1 by the suite case; S2 by the new shape and its pins (a
refusal ahead of the embed is answered, with 0 embedder calls); N4 by
the new shape (the embed is back inside `search`, so pgvector's socket
wording is `main`'s again). Nits of contract review 7290045524: N1 by
the new shape (the function is handed on every call, so "every call" is
literal); N2 by the new shape (an embedder that answers no vector is one
embedding call, memoised, and the width check refuses it, where the
earlier head made two); N3 by the new shape, wording kept: on the
service's path the handed vector is what the adapter's own embedder
answered, so "the embedder answered" stays true; a direct caller answers
for its function (the note's second diamond).
## Tests
All pins count calls; none is wall-clock.
-
`packages/services/service-knowledge/src/__tests__/search-question-vector.test.ts`
(rewritten, 11 cases, over a fake adapter shaped as the two stores): a
refilled search hands both rounds one function and calls `embedQuery`
once; two sources of one instance, one function for four calls, one
call; the function answers the same promise on every call; two
instances, two functions, one call each; two searches, two functions;
`keyword` is handed the function and embeds nothing; a source the
adapter refuses behind a failing embedder answers its 409 with 0 calls;
no `embedQuery`, nothing handed; an uncoded embed failure fails the call
that asked (503 platform sentence, text to the operator line); a coded
one reaches the adapter as the same error object and keeps code and
message; a failed embed is shared (one call, both sources refused).
- `packages/plugins/knowledge-turso/src/__tests__/acceptance.test.ts`
(one top-level describe, 6 cases, real adapter behind
`KnowledgeService`): refilled search, one embedder call, prefixed;
refilled search over two sources of one instance, one call; `keyword`, 0
calls; **a source that needs a rebuild (embedder model changed) behind
an embedder that fails answers `KNOWLEDGE_REBUILD_REQUIRED` 409 with 0
embedder calls**; the control, the same failing embedder behind a
healthy source, called once and refused 503; a handed function is ranked
with, a function of another width is refused.
-
`packages/plugins/knowledge-pgvector/src/__tests__/question-once.test.ts`
(new, 5 cases, real adapter and `KnowledgeService` over a fake pool):
the same four pins as turso (the rebuild-required source is an index
held under another fingerprint) and the same control.
-
`packages/plugins/knowledge-pgvector/src/__tests__/retrieval-modes.test.ts`
(3 cases, adapter alone): `embedQuery` embeds the prefixed query; a
handed function is asked once and its answer is bound into the ranking
statement, embedder not called, with a control; another width is
refused.
-
`packages/services/service-knowledge/src/testing.ts#runKnowledgeAdapterContract`,
one case, run only for `implements.embedQuery`: handed a function
answering its own `embedQuery`, a search answers what it answers without
the member and makes no embedding call of its own; handed one answering
a vector of another width (wider, then narrower), the search rejects.
The count is a **spy on the instance's `embedQuery`** (review 7297709290
N1), with a control (the search without the member is seen by the spy
once). It asserts a rejection, not a code (N2); no code is added.
- `packages/plugins/knowledge-pgvector/bench/instrument.test.mjs`:
unchanged since `56feee7fa7`, still one embed for a refilled search.
<details><summary>Ablation (each through scripts/ablation-replace.mjs;
landing and restore proven by blob hash, git diff HEAD empty after
each)</summary>
- The service calls the function ahead of `search` (the earlier head's
order): service 6 failed of 11; turso 2 failed of 6 (keyword,
rebuild-required); pgvector 2 failed of 5 (keyword, rebuild-required).
- The function not memoised: service 5 failed of 11; turso 2 failed of 6
(refilled, two sources); pgvector 2 failed of 5 (refilled, two sources).
- `knowledge-turso` ignores the handed function: the contract case fails
on both libSQL cells (2 of 2).
Every pin goes red under at least one ablation. The `keyword` pins also
hold on `main` and on the earlier head: they guard the order, not the
count.
</details>
<details><summary>Runs at e434583</summary>
- `pnpm --filter @objectstack/service-knowledge exec vitest run`: Test
Files 31 passed (31), Tests 896 passed (896).
- `pnpm --filter @objectstack/knowledge-turso exec vitest run`: Test
Files 6 passed (6), Tests 335 passed (335).
- `pnpm --filter @objectstack/knowledge-pgvector exec vitest run`: Test
Files 10 passed, 10 skipped (20); Tests 75 passed, 117 skipped (192).
The live-PostgreSQL files skip here (no pgvector server in this
container), so the contract case on pgvector is NOT MEASURED locally:
CI's live cell runs it.
- `pnpm --filter @objectstack/knowledge-memory exec vitest run
src/__tests__/contract.test.ts`: 54 passed (the suite change, an adapter
without the member).
- typecheck of spec, service-knowledge, knowledge-turso,
knowledge-pgvector: exit 0, test layers included.
- `pnpm --filter @objectstack/spec build` then `check:generated`: all 14
generated artifacts up to date.
- eslint over the changed source and test files: exit 0.
- Derived gates (`dispatch-gates.mjs --commands`, 117 families): 116
exit 0, 1 NOT MEASURED (`check:dual-build-cjs-loads`, exit 3: needs
every package built); `--ran`: 117 derived, 116 run, 1 NOT-MEASURED, 0
UNRUN.
</details>
## Acceptance notes
- The earlier head's note that a source with no built index cost one
embed per search is gone: the adapter now asks only past its own gates.
- One embed serves every source of one adapter instance, so a failed
embed refuses each source of that instance that asked, with the same
error, where on `main` each made its own attempt (the note says so).
- The contract case counts by a spy on `embedQuery`, so it reads an
adapter whose `search` embeds the query through that method, as both
stores do; one that embeds by another path fails the case's control, and
the suite's docblock says so.
- `knowledge-memory` has no `embedQuery` and still embeds on every call;
adding it there is outside this card.
## Verification
Read first, by risk:
1.
`packages/services/service-knowledge/src/knowledge-service-search.ts`:
`memoisedQuestion` and the `queryVector` lines at the top of the loop in
`searchRound`. Risk: a call of the function there, ahead of `search`,
would undo the condition.
2. `packages/plugins/knowledge-turso/src/turso-adapter.ts` and
`packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts`: the one
changed line where `search` embeds. Risk: it must stay after every
refusal above it.
3. `packages/spec/src/contracts/knowledge-adapter.ts`: the two members
and their docblocks (the contract text).
Two-minute check: `pnpm --filter @objectstack/knowledge-turso exec
vitest run src/__tests__/acceptance.test.ts -t 'embedded at most once'`
prints `Tests 6 passed`; the fourth case asserts a 409 with 0 embedder
calls behind a failing embedder.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01JQwxbXfgA45f3y5YpDPT5h
<!-- signed: repo:objectstack#2 · os-warren ·
session_01JQwxbXfgA45f3y5YpDPT5h -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 13a0d3f commit 58695bb
15 files changed
Lines changed: 791 additions & 14 deletions
File tree
- .changeset
- content/docs/protocol
- packages
- plugins
- knowledge-pgvector
- bench
- src
- __tests__
- knowledge-turso/src
- __tests__
- services/service-knowledge/src
- __tests__
- spec/src/contracts
| 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 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
526 | 526 | | |
527 | 527 | | |
528 | 528 | | |
| 529 | + | |
529 | 530 | | |
530 | 531 | | |
531 | 532 | | |
| |||
564 | 565 | | |
565 | 566 | | |
566 | 567 | | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
567 | 587 | | |
568 | 588 | | |
569 | 589 | | |
| |||
615 | 635 | | |
616 | 636 | | |
617 | 637 | | |
618 | | - | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
619 | 642 | | |
620 | 643 | | |
621 | 644 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
138 | | - | |
| 138 | + | |
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
| 148 | + | |
| 149 | + | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| 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 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
117 | 167 | | |
118 | 168 | | |
119 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
591 | 591 | | |
592 | 592 | | |
593 | 593 | | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
594 | 605 | | |
595 | 606 | | |
596 | 607 | | |
| |||
650 | 661 | | |
651 | 662 | | |
652 | 663 | | |
653 | | - | |
654 | | - | |
655 | | - | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
656 | 670 | | |
657 | | - | |
| 671 | + | |
658 | 672 | | |
659 | 673 | | |
660 | 674 | | |
| |||
0 commit comments