Repository navigation
Commit 3801775
feat(service-knowledge): search answers records, not passages — granularity, groupBy and a freshness check (#37335)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8KwgBifx8qqSVYdsjkfntXZ)_
Fixes #37103
Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: none — no page under `skills/**` teaches the knowledge
search options or the hit's shape (the one knowledge eval asks about
source authoring); `content/docs/ai/knowledge-rag.mdx` and
`content/docs/protocol/knowledge.mdx` teach them, updated here.
## 维护者速读
-
用户能看到什么变化:应用检索知识时可以选「按记录返回」:同一份制度命中多个片段只返回一条(取最高分),也可以按某个字段分组(比如按制度)直接得到「哪几份制度」;默认行为不变。
- 为什么做:知识检索平台化专题第二阶段,让制度问询先找到「哪几份制度」再交给模型,应用不用再自己去重。
- 出错会怎样:只影响主动选了按记录返回的检索;建索引后被删、改过或不再符合条件的记录不会返回旧片段,并在后台自动修一次索引;可直接撤回本改动。
- 要你做什么:无。
## What changes
- `KnowledgeSearchOptions`
(`packages/spec/src/contracts/knowledge-service.ts`) gains
`granularity?: 'chunk' | 'record'` (default `chunk`: the same hits, asks
and reads as today, no extra read; on `knowledge-memory` each hit now
also carries `docHash` and, when its passage has one, `headingPath`, the
K8 items 3 and 14 members) and `groupBy?: string`. The return type stays
`KnowledgeHit[]`.
- `KnowledgeHitSchema` gains `docHash?` (ADR-0161 K8 item 3) and
`headingPath?` (item 14); `knowledge-memory` returns both on its hits.
- `KnowledgeService.search()` under record granularity:
- one hit per record, its best passage; `topK` counts records. Every
source is asked `topK × 5` and the saturated ones refilled once at `topK
× 20` when fewer than `topK` groups survive (the existing refill of
#36993, widened to every source and caller in this mode). That refill is
the stated cap; fewer records within it answer fewer, logged at `debug`.
- `groupBy` groups by `metadata[groupBy]`; same value = one group across
sources; an object source without the field in `metadataFields` is not
searched; a record with no value is not answered; file / http sources
group by document. `groupBy` implies record mode; with `granularity:
'chunk'` it is refused (`VALIDATION_ERROR`, 400, never ignored), as are
an empty `groupBy` and an unknown `granularity`; the caller's `groupBy`
is JSON-quoted and cut at 64 characters in log lines and refusals.
- The permission re-read stays one query as the caller and also reads
the display field (`documentTitleField`, the `resolveDisplayField`
reading shared with #36994); a caller's hit carries the live `title`.
The system identity is not re-checked, so its hits keep the indexed
title (equal whenever the hash matched).
- Freshness: only for the hits about to be answered, one system-context
read per source, `{ $and: [where, { id: { $in } }] }`, projected to the
fields assembly reads (the whole row when the source names a field the
object does not declare), its system context in the trailing options
argument. Deleted / out-of-`where` / recomputed-`docHash`-differs ⇒
dropped, next group moves up (at most `SEARCH_FRESHNESS_ROUNDS` = 3
rounds; unchecked after that ⇒ not answered). Repair: `syncRecord`
through the per-document serialization (#36991's path), not awaited,
system context, single-flight, once per document until a later search
finds its hash matching; never throws into the search. A source with
`refresh.onRecordChange: false` (external indexer) is never repaired:
`repairStale` asks `objectSourcesFor` first, and the stale hit is only
dropped. A hit without a stored hash is checked for membership only. A
failed freshness read drops that source's checked hits (fail-closed),
`warn`.
- Contract suite (`@objectstack/service-knowledge/testing`): an adapter
with `listDocuments` must return each hit's stored `docHash` (plus a
record-level freshness case through a real service); an adapter that
takes chunks must return `headingPath`. The chunk stub in
`contract-stubs.test.ts` now carries `headingPath`.
- Docs: `content/docs/protocol/knowledge.mdx` gains "Records, not
passages"; `content/docs/ai/knowledge-rag.mdx` shows `granularity` /
`groupBy`; `content/docs/references/ai/knowledge-document.mdx` and
`packages/spec/authorable-surface/ai.json` regenerated.
## Why
Epic #37000 phase 2 row 10, ADR-0161 K1 (record-level return; the
service groups, K8 item 13 stays out). Out of scope as the card says:
`search_knowledge` tool arguments (row 12), grouping inside the adapter,
the scope filter (row 11).
## Acceptance notes
- Reading choice, stated: card item 3's "File and http sources group by
`documentId`" is applied under `groupBy` too (a file document is its own
group beside the field's values), and the rule "a source without it
answers no hits" applies to object sources.
- Reading choice, stated: freshness and the live title run only under
record granularity, so the default `chunk` mode is today's behaviour
(card item 1).
- Coordination: #37100 (`knowledge-pgvector`) runs the contract suite;
with `listDocuments` it now owes `docHash` on hits, and with chunks
`headingPath`. Told to that thread.
- Declaration corrected from the claim's `Metadata-protocol: none` to
`added` (and `@objectstack/spec` to `minor`): `Check Changeset` reads
the two new `KnowledgeHit` keys in `authorable-surface/ai.json` as
authorable keys added. Seat told to align the claim.
- `freshnessRepairs` holds one entry per stale document until its hash
is seen matching; a record deleted for good keeps its entry for the
process lifetime (bounded by distinct stale records seen). Row 15 moves
repair to the queue.
## Tests
<details><summary>Local runs at f5c87f7</summary>
- `pnpm --filter @objectstack/service-knowledge test` → Test Files 11
passed, Tests 296 passed (275 before; +20 record-level cases in
`knowledge-service.test.ts`, +1 contract headingPath case on the chunk
stub).
- `pnpm --filter @objectstack/knowledge-memory exec vitest run
--maxWorkers=2` → Tests 37 passed (contract: hit docHash, record-level
freshness through a real service with background repair, headingPath).
- `pnpm --filter @objectstack/knowledge-ragflow exec vitest run
--maxWorkers=2` → Tests 10 passed.
- `pnpm --filter @objectstack/service-knowledge typecheck`, `pnpm
--filter @objectstack/knowledge-memory typecheck` → exit 0,
test-typecheck OK.
- `pnpm --filter @objectstack/spec check:generated` → 1 stale
(`check:docs`), fixed with `--fix`, re-check green; other 13 green.
- `check:system-context-census`, `check:where-matcher`,
`check:doc-anchors` → green (the first two were red in CI at 4267fda
and are fixed: the contract double no longer reads `isSystem`, and the
record-level suite's table engine refuses operators it does not
implement).
- eslint on the changed `.ts` files → exit 0. `check:doc-authoring`,
`check:durability-log-level`, `check:startup-registry-verdict`,
`check:adr-anchors`, `check:adr-0087-registration`,
`check:pm-skill-id-lint`, `check-nul-bytes` → exit 0.
- Ablation (restored, `git diff HEAD` empty): hash compare replaced by
`false` → 2 freshness cases red; grouping map read disabled → 3 grouping
cases red. Review round (at f5c87f7): repair skip removed → the
external-indexer case red; `SEARCH_FRESHNESS_ROUNDS` 3 → 50 → the
round-cap case red; undeclared-field filter restored → the
undeclared-field case red; title dropped from the projection → 2 cases
red; the cap-aware debug guard removed → the round-cap case red.
- NOT MEASURED locally: the full `pnpm test` / repo `check:*` farm (117
derived commands) — declared to CI.
</details>
## Verification
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run --maxWorkers=2 src/__tests__/knowledge-service.test.ts -t
"record-level"` → 20 passed.
Read first: `knowledge-service.ts` `answerByRecord` + `checkFreshness`
(drop/repair decisions), `search()` (refill widening under record mode),
`applyPermissionFilter` (live title projection).
## 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; eslint on changed files passes (repo `pnpm
lint` in CI).
- [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.
- [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.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01JQwxbZmvWffL8FZzRX8ZSb
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbZmvWffL8FZzRX8ZSb)_
<!-- signed: repo:cloud#1 · os-warren · session_01JQwxbZmvWffL8FZzRX8ZSb
-->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 68250f6 commit 3801775
13 files changed
Lines changed: 985 additions & 41 deletions
File tree
- .changeset
- content/docs
- ai
- protocol
- references/ai
- packages
- plugins/knowledge-memory/src
- services/service-knowledge/src
- __tests__
- spec
- authorable-surface
- src
- ai
- 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 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
70 | 89 | | |
71 | 90 | | |
72 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
189 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
190 | 192 | | |
191 | 193 | | |
192 | 194 | | |
| |||
209 | 211 | | |
210 | 212 | | |
211 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
212 | 250 | | |
213 | 251 | | |
214 | 252 | | |
| |||
244 | 282 | | |
245 | 283 | | |
246 | 284 | | |
247 | | - | |
| 285 | + | |
| 286 | + | |
248 | 287 | | |
249 | | - | |
| 288 | + | |
| 289 | + | |
250 | 290 | | |
251 | 291 | | |
252 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| 97 | + | |
| 98 | + | |
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| |||
116 | 120 | | |
117 | 121 | | |
118 | 122 | | |
| 123 | + | |
| 124 | + | |
119 | 125 | | |
120 | 126 | | |
121 | 127 | | |
| |||
136 | 142 | | |
137 | 143 | | |
138 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
139 | 149 | | |
140 | 150 | | |
141 | 151 | | |
| |||
148 | 158 | | |
149 | 159 | | |
150 | 160 | | |
151 | | - | |
| 161 | + | |
| 162 | + | |
152 | 163 | | |
153 | 164 | | |
154 | 165 | | |
| |||
158 | 169 | | |
159 | 170 | | |
160 | 171 | | |
| 172 | + | |
| 173 | + | |
161 | 174 | | |
162 | 175 | | |
163 | 176 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
0 commit comments