Repository navigation
Commit 039b288
fix(service-knowledge): search refills after the permission filter; a hit's title is the object's display field (#37301)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8HtXJcvyDnGVcP1BL7DwnBY)_
Fixes #36993
Fixes #36994
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:在挂了知识库的应用里,一个人能看的记录很少时,AI
检索不再返回空或只剩一两条,而是继续往后找,补满到要的条数;命中结果的标题改为取对象的名称字段(比如客户的「名称」、发票的编号),不再只认名为
title 的字段。
- 为什么做:知识检索平台化专题的第一、四阶段两张卡,同一个文件一起修;「按记录返回」那张卡要在这次的补齐逻辑上继续做。
-
出错会怎样:最坏是检索多读一轮、稍慢,或某条命中没有标题。权限检查只多不少:名称字段对某人不可查时,这个知识源对他不检索;旧索引里的标题还取自名为
title 的字段,所以在索引重建之前,title 字段对某人不可查时同样不检索。可以直接撤回。
- 要你做的:无。
## What changes
**Search refills after the permission filter (#36993).**
`KnowledgeService.search` re-checks every object-source hit as the
caller *after* ranking, so a caller who may read few of the matching
rows was answered from what was left of `topK` ranked hits. Now:
- When the re-check can remove hits (a caller who is not the system
identity, with a data engine bound), each `object` source is asked for
`topK × SEARCH_FIRST_ROUND_FACTOR` (5).
- When the survivors name fewer than `topK` records (several passages of
one record count once: an object hit is its source and record id, any
other hit its document), every `object` source that answered as many
hits as it was asked (the only ones that may hold more) is asked once
more, for `topK × SEARCH_REFILL_FACTOR` (20, the cap). That round's
answer replaces the source's first-round hits; a source whose refill
throws keeps its first-round survivors. At most two rounds; an adapter
that caps `topK` answers fewer than asked and is never asked again.
- A `file` / `http` source is asked for `topK` once (the re-check never
removes its hits). The system identity gets one round of `topK`, no
refill. No context: object sources stay unsearched, as before (fail
closed).
- The answer is still at most `topK` chunk-level hits, sorted by score;
`search`'s return type is unchanged (`expectTypeOf` pin). The count the
re-check removed is logged at `debug` only, never returned.
- The adapter loop moved into a private `searchRound`, unchanged in
behaviour (foreign-source hits dropped and warned, a throwing adapter
logged and skipped).
**A hit's title is the object's display field (#36994).**
`recordToDocument` read `record.title`. Per ADR-0161 K5 ("the title is
the object's display field"), it now reads the field
`documentTitleField(schema)` names: `resolveDisplayField` (ADR-0079:
`nameField`, its deprecated alias, else the derivation), and with no
object definition a field named `title`, as before. A withheld field
(credential-class or `internal`), an empty value, a value that is not
text or a finite number, a definition that resolves no display field, or
a `nameField` the definition does not declare gives no title.
`resolveRecordDisplayName` is not used: its `Record #id` floor is a UI
label.
The source admission's field gate (`servedFieldsOf`) asks about the same
field through the same `documentTitleField`. So a source whose object's
display field the caller may not query is not searched for that caller
at all. It also keeps asking about a declared field named `title`, as
before: documents indexed under the earlier rule carry that field's
value as their title and stay in the index until a complete reindex
writes them again, and nothing reindexes at boot. That arm is retired
once the index is proven rebuilt under the display-field rule (the
recipe hash that names the title rule, ADR-0161 K7, and the rebuild of a
stale source, #37099); the code comment says so.
Docs: `content/docs/protocol/knowledge.mdx` states the refill and its
cap, and the display-field title (three places that said "the record
`title`"); `content/docs/ai/knowledge-rag.mdx` says the same in two
sentences. `content/docs/permissions/system-context.mdx` row 46 keys
`search` too (it now reads `isSystem`: the system identity skips the
over-fetch and the refill), and its generated register is regenerated
(`check:system-context-census --write`).
## Tests
`packages/services/service-knowledge`: `pnpm test` 8 files / 219 tests
passed at 4621738; `pnpm typecheck` exit 0 (incl.
`check:test-typecheck`). New pins in `knowledge-service.test.ts` (two
top-level describes) and `knowledge-source-admission.test.ts` (one):
- 100 hits, the caller may read 5 spread across them → those 5, best
first; asked `[s1, 25]` then `[s1, 100]`.
- A readable row beyond the cap → fewer than `topK`, never a third
round. Enough survivors → no refill. An adapter capping at 10 → asked
once. Several sources → only the saturated one refilled. The refill
decision counts records: topK 3, one source answers two passages of one
readable record, another holds a readable record past its first round
that outranks both → it is refilled and answers `[s1-c0, s1-c15, s2-a]`.
A failed refill keeps round one. System identity → one round of `topK`,
no re-check. No context → only the file source, asked `topK`. A file
source beside an object source → asked `topK` once.
- Removed count only in the `debug` log; answer and return type
unchanged.
- Title: explicit `nameField`; derived `name` over `title`; formula
(text) and autonumber name fields (a number value → its text); withheld
internal / secret name fields; empty, non-text, unresolvable and
undeclared-`nameField` cases; no definition → `title`; the event sync
and the reindex walk read the definition off the engine.
- Field gate: hiding the display field refuses the source even with
`title` visible; hiding a declared `title` that is not the display field
refuses it too (the adapter returns a stored hit carrying an old-rule
title, and the answer is empty); a caller who may query both gets the
hit.
<details><summary>Ablations: each committed first, mutated through
scripts/ablation-replace.mjs, restored to HEAD with git diff HEAD
empty</summary>
- Refill disabled (`if (false && filtered && kept.length < topK)`): 6
refill tests red, 5 green (the ones that pin "no refill").
- Title back to `record.title`: 5 title tests red.
- Field gate back to `title` only: both display-field gate tests red.
- Refill decision back to counting hits (`kept.length`): the
record-counting test red, 150 green.
- The declared-`title` arm deleted from the gate: its refusal pin red,
24 green.
</details>
## Acceptance notes
- **#36993 point 2 is applied to the refill decision only.** The answer
stays at most `topK` chunk-level hits (`KnowledgeSearchOptions.topK` is
"Max number of hits to return"; `search_knowledge` returns "at most
`topK`" passages; the card: "what is returned stays chunk-level hits").
Counting records decides whether to refill, and with several sources
that changes the answer: another source's refill can outrank a second
passage of a record already counted (pinned above).
- **The declared `title` arm of the field gate is temporary.** Retire it
when the index is proven rebuilt under the display-field rule (ADR-0161
K7 recipe hash, #37099).
- **Title egress** (the question raised on #36994): ADR-0161 K5 declares
the title as the object's display field, so it is sent whether or not
the source lists that field among its content fields. It is withheld
when the field is credential-class or `internal`. The field-egress
declaration of cloud ADR-0119 does not exist yet; when it does,
`documentTitleField` is the one place assembly and the gate both read.
- The title enters `docHash`, so the first complete reindex after this
lands rewrites the documents whose title changed.
- Seat 4's #37287 edits the same file; `scripts/pm/overlap-check.mjs`
merges this branch with its head cleanly (the search factors sit beside
`LIST_DOCUMENTS_PAGE_SIZE` to stay clear of its new constant).
## Verification
Files to read first:
`packages/services/service-knowledge/src/knowledge-service.ts`
(`search`, `searchRound`, `documentTitle`),
`knowledge-source-admission.ts` (`documentTitleField`,
`servedFieldsOf`).
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run src/__tests__/knowledge-service.test.ts -t refills` — 11
tests pass; the first shows a caller reading 5 of 100 hits getting all
5.
<details><summary>Local gate families: the derived list
(scripts/pm/dispatch-gates.mjs --commands, 98 commands, derived at
ecb0842), run in order; CI runs the full farm</summary>
- Rework round (4621738): package test 8 files / 219, typecheck 0,
eslint on the four changed source and test files 0, check:doc-anchors 0,
check:system-context-census 0, overlap-check clean against origin/main,
#37287 and #37265.
- 79 of 98 run: 76 exit 0; 3 PREREQUISITE NOT MET (not verdicts, NOT
MEASURED locally, CI builds them): check-plugin-teardown-shape
--self-test (shallow clone cannot reach its pinned fixture commit), spec
check:skill-examples (client-react dist not built),
check:dual-build-cjs-loads (hono / account dist not built).
- The remaining 19 (the repo-wide eslint-backed ratchets onward) were
still running when this was written: NOT MEASURED locally; their CI
check-runs are the reading.
- Also run: package typecheck (exit 0, incl. check:test-typecheck),
eslint on the four changed source and test files (exit 0),
check:doc-anchors (exit 0), check:system-context-census (exit 0 after
row 46 below), scripts/pm/overlap-check.mjs (clean against origin/main,
#37287 and #37265).
</details>
## 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.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 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_01JQwxbWqwmAwx7eD4Xf3dv1
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbWqwmAwx7eD4Xf3dv1)_
<!-- signed: repo:cloud#1 · os-warren · session_01JQwxbWqwmAwx7eD4Xf3dv1
-->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent d88c41e commit 039b288
8 files changed
Lines changed: 502 additions & 28 deletions
File tree
- .changeset
- content/docs
- ai
- permissions
- protocol
- packages/services/service-knowledge/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
| 192 | + | |
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| |||
703 | 703 | | |
704 | 704 | | |
705 | 705 | | |
| 706 | + | |
706 | 707 | | |
707 | 708 | | |
708 | 709 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
186 | 210 | | |
187 | 211 | | |
188 | 212 | | |
| |||
379 | 403 | | |
380 | 404 | | |
381 | 405 | | |
382 | | - | |
383 | | - | |
| 406 | + | |
| 407 | + | |
384 | 408 | | |
385 | 409 | | |
386 | 410 | | |
| |||
394 | 418 | | |
395 | 419 | | |
396 | 420 | | |
397 | | - | |
| 421 | + | |
| 422 | + | |
398 | 423 | | |
399 | 424 | | |
400 | 425 | | |
| |||
444 | 469 | | |
445 | 470 | | |
446 | 471 | | |
447 | | - | |
| 472 | + | |
| 473 | + | |
448 | 474 | | |
449 | 475 | | |
450 | 476 | | |
| |||
0 commit comments