Repository navigation
Commit 62eecbc
feat(knowledge): a knowledge source declares its document unit (
<!-- claude-projects-attribution: {"github_login":"os-project-manager"}
-->
_Requested by **Leehom** · [project
thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XDXvJPPtrTGfdDYhWWpihq5)_
Part of #38537
Clause-②: yes (widening)
Metadata-protocol: added
## 维护者速读
- 用户能看到什么变化:知识源可以声明“文档单位”(指向父记录的字段);AI
按记录检索时,同一份父记录(如一份制度)下的多条子记录(条款)只返回一条命中,原来是每条子记录各一条;Studio
知识源表单多了一个“文档单位”选择框。
-
为什么做:属于“知识源声明文档单位、按父记录计数与聚合”专题,替把段落写成子记录的应用(例如制度下的条款)解决按记录检索时同一份制度刷屏的问题,也是云端按父记录计文档数的前置。
- 出错会怎样:只有声明了该字段的知识源受影响,索引内容不变、不触发重建;若分组有误,AI 检索会多返回或少返回命中,删掉该字段即恢复原行为。
- 要你做什么:无。
## What changes
PR 1 of 2 for the card. This PR lands the key, its parse rule, its
save-time refusal and its first reader; the `os validate` rule (in the
lint package, held by another seat until it is released) is PR 2, which
closes the card.
- **Spec** — `ObjectKnowledgeSourceSchema` gains `documentBy?: string`.
A refinement refuses a value that `metadataFields` does not list, with a
message that names the field, the list, and the fix (add it to
`metadataFields`, name a listed field, or remove the key). The
refinement is a cross-key rule JSON Schema cannot state, so it is
ledgered in `dropped-refinements.baseline.json` (two schemas plus the
four installed-package response schemas that embed a source; header
totals updated).
- **Parent-link rule, exported** — `knowledge-search-scope.ts` now
exports `parentLinkRule(schema)` (the rule `scopeTermsFor` already used,
factored out of the private `parentLinksOf`, which now calls it; scope
behaviour unchanged) and `documentUnitOf(field, schema)` (the rule plus
"not `multiple: true`"). Both are pure functions of the object
definition, so PR 2 can call them with no knowledge service.
- **Save door** — `admitAuthoredSource` refuses a `documentBy` that
`metadataFields` lists and that is not a single-valued parent link: `400
VALIDATION_ERROR`, `field` set, after the "does not exist" arm and
before every permission arm. The sentence is built by
`documentUnitRefusalText` in `knowledge-source-admission.ts`, one
wording per reason (undeclared, withheld, not a link, no readable
target, multiple). A `documentBy` that `metadataFields` does not list is
left to the parse, which already names that fix.
- **Reader (ADR-0049, ADR-0161 K4)** — in `searchByKey`, with record
granularity and no `groupBy`, `groupKeyOf` now asks `documentKeyOf(hit,
documentBy)`: `parent:SOURCE#VALUE` when the hit's source declares
`documentBy` and the hit carries a parent, else the existing per-record
key. The explicit `groupBy` path, `sourcesGroupableBy` and chunk
granularity are untouched.
- **Tool** — `hitOf` sets `groupValue` to the parent under the default
grouping too (absent for a record with no parent). `documentUnitValueOf`
is the one predicate both the service key and the tool read. Like its
`groupBy` arm, the tool serves the parent only for a source that lists
`documentBy` in `metadataFields`.
- **Hashes** — `documentBy` is in neither the index fingerprint nor the
recipe hash (both enumerate their inputs explicitly);
`__tests__/index-fingerprint.test.ts` is unchanged and green.
- **Form** — the form-coverage gate
(`metadata-form-zod-reconciliation.test.ts`) refused the key without a
form row, so the single picker is added beside `metadataFields`
(`widget: 'field-ref'`, `dependsOn: 'object'`) with its form test.
**This folds in the form half of #38538**; the docs and `skills/**` half
stays there. The row brings its regenerated translation bundles (ja-JP,
es-ES and zh-CN authored, the rest the extractor's fill) and the
catalog's positive-control count (866 to 867).
- Regenerated: authorable-surface, the reference page, the liveness
ledger row and counts.
- **Changeset** — `minor` for `@objectstack/spec` (the new authorable
key), `@objectstack/service-knowledge` (the reader and the save refusal)
and `@objectstack/platform-objects` (the form row's translated label and
help), with `Metadata-protocol: added`: a new key a metadata author can
write.
## Zone 2 choices
- **Where the parent value is read from:** the hit's own
`metadata[documentBy]`, the value assembly stored beside the passage.
The parse holds `documentBy` in `metadataFields`, so every hit of such a
source carries it; grouping runs over the hits the adapters already
answered and reads no record (SQL first: no per-hit read was added). The
freshness check and the permission re-check are unchanged.
- **Default key spelling:** `parent:` + index key of the hit's source +
`#` + `String(value)`. The source key keeps two sources apart on equal
parent ids; the `parent:` prefix keeps it apart from `record:` and
`document:` keys; `String()` compares values as a scope term does. A
value that is absent, null, empty, or not a string or finite number
falls back to the per-record key, so those records are never dropped.
- **Form row:** forced by the gate (it reported
`knowledge_source.source: accepted by the Zod but unauthorable in the
form — documentBy`), so it is in this PR.
## Acceptance
| Card acceptance line | Test |
|:--|:--|
| Parse refuses `documentBy` absent from `metadataFields` |
`knowledge-source.test.ts` › "refuses a `documentBy` that
`metadataFields` does not list…", "…when `metadataFields` is omitted…" |
| Save refuses a text field, a `user` field, an `internal` lookup and a
`multiple` lookup, each naming the fix | `document-by-38537.test.ts` ›
"refuses a text field / a user field / an internal lookup / a multiple
lookup, 400 VALIDATION_ERROR, naming the field and the fix" (+ positive
controls: lookup and master_detail admitted) |
| 6 records under 2 parents + 1 null parent, record granularity, topK 10
→ 3 hits | `document-by-38537.test.ts` › "6 records under 2 parents plus
1 with no parent…: 3 hits" |
| Explicit `groupBy: 'clause_hash'` unchanged; a source without
`documentBy` unchanged | `document-by-38537.test.ts` › "an explicit
groupBy is answered as before…", "a source without documentBy is
answered as before…", "chunk granularity is answered as before" |
| Fingerprint pins unchanged | `index-fingerprint.test.ts` unchanged and
green; `document-by-38537.test.ts` › "the fingerprint and the recipe
hash are the same with and without documentBy" |
| Default key includes the source; `hitOf` sets `groupValue` | "two
sources never share a group…", "search_knowledge: each hit carries its
parent as groupValue…" |
## Ablation
Both legs run with `scripts/ablation-replace.mjs` (mutation verified on
disk, restore verified as blob equal to HEAD and an empty `git diff
HEAD`). No build was needed: both suites import the subject from `src/`.
<details><summary>Reader reverted (default key back to the per-record
key): 4 red</summary>
Anchor `if (groupBy === undefined) return documentKeyOf(...)` replaced
with `return recordKeyOf(hit)` in `knowledge-service.ts`.
`document-by-38537.test.ts`: 4 failed / 12 passed — the 3-hit test, the
empty-parent test, the two-sources test and the `groupValue` test.
Restored: blob c98a036 equals HEAD.
</details>
<details><summary>Parse rule removed (the refinement dropped): 2
red</summary>
Anchor `.superRefine(refuseDocumentByOutsideMetadataFields)` removed
from `knowledge-source.zod.ts`. `knowledge-source.test.ts`: 2 failed /
35 passed — both refusal tests. Restored: blob 211cb6d equals HEAD.
(A first attempt was refused by the tool before running anything,
because its replacement text already occurred in the file; it was re-run
with a distinct replacement.)
</details>
<details><summary>Delta round 1: the tool's metadataFields guard
removed: 1 red</summary>
The condition `&&
objSource.metadataFields?.includes(objSource.documentBy)` removed from
`hitOf` in `knowledge-tools.ts` (on-disk count 0).
`document-by-38537.test.ts`: 1 failed / 16 passed, the stub-service test
("expected … to not have property groupValue"). Restored from a copy
(on-disk count 1) before commit `12cd7dc8a6`, whose blob `e49165a005`
carries the condition. This leg used a copy-and-restore, not
`ablation-replace.mjs`.
</details>
## Tests
**Delta round 1 (contract review 7254573293, PASS with S1 open), head
`e0ebb9ad8e`.** S1: the `documentBy` docblock names the save refusal
alone, and says a source registered in code or installed with a package
meets no save and is not judged yet. N1: the docblock and the changeset
say where each surface carries the parent: `metadata[documentBy]` on
`IKnowledgeService.search`, and also `groupValue` on a
`search_knowledge` hit. N2: the form help reads "Setting it rebuilds
nothing; adding the field to Filter fields does." The changeset says the
same, and the zh-CN, ja-JP and es-ES help is retranslated. The eleven
bundles are regenerated: `check:i18n` reports all in sync, and
`check:i18n-stale-fill` finds no new stale fill. N3: the `hitOf` guard,
pinned by one stub-service test with a positive control. Claim widening:
the contract TSDoc and the tool description above. Readings on
`e0ebb9ad8e`:
- `@objectstack/service-knowledge`: test 23/23 files, 803/803 tests; the
new test file 17/17 after its type fix; typecheck exit 0.
- `@objectstack/spec` test: 913/913 files, 28896 passed, 1 todo.
- `@objectstack/platform-objects` test: 130/130 files, 2218/2218 tests.
- spec `check:generated`: all 14 artifacts up to date, against a dist
built after the spec edits.
- Changeset and byte gates, each exit 0: `check-changeset-no-major` with
this body as its event payload (level axis `yes (widening)` / `added`),
`check-empty-changeset`, `check-adr-0087-registration`,
`check-changeset-fixed`, `check-nul-bytes`,
`check-spec-docblock-symbol-anchors`.
- overlap-check: origin/main merges cleanly; it conflicts only with
#38550 and #38497, on the hand-maintained counters as before.
**Delta round 1, second commit, head `43de68d58f`.** The claim was
amended to add the `topK` TSDoc and the tool's `granularity` input
description. Both now say what the fixed docblocks say: "for a source
that declares `documentBy` (its document unit), one hit per parent
record instead". So do the other record-unit sentences in the two files
(see Acceptance notes). Readings on `43de68d58f`:
- `@objectstack/service-knowledge`: test 23/23 files, 803/803 tests;
typecheck exit 0. This covers the `granularity` refusal sentence: no
test pins its text.
- spec `check:generated`: all 14 artifacts up to date, against a dist
built after the edit.
- `check-spec-docblock-symbol-anchors` and `check-nul-bytes`: each exit
0.
- overlap-check: unchanged (origin/main clean; conflicts only with
#38550 and #38497 on the counters).
The `@objectstack/mcp` knowledge-tool tests compare the listed
description with the definition itself and pin no text, so they were not
re-run here.
Earlier, the head was `5bbac4b457`, a clean merge of main that brings in
#38509 (the record sync change in `knowledge-service.ts`). Re-checked on
that head: `@objectstack/service-knowledge` test 23/23 files, 802/802
tests; its typecheck exits 0; `@objectstack/spec` `check:generated`
reports all 14 artifacts up to date; overlap-check finds origin/main
merges cleanly. Below it, `231e7c6339` changes only the changeset (level
`minor`, `Metadata-protocol: added`); the changeset families were re-run
there (`check-changeset-no-major` with this body as its event payload,
`check-empty-changeset`, `check-adr-0087-registration`, all exit 0). The
gate union was re-run after the last code commit, at `a92874657e` (the
translation commit landed while it ran, and the families that read it,
`check:i18n`, `check:i18n-stale-fill` and `check:nul-bytes`, were run
again on `a92874657e`).
<details><summary>Package suites and typecheck</summary>
- `pnpm --filter @objectstack/service-knowledge test` — Test Files 23
passed (23), Tests 788 passed (788). `typecheck` (tsc + test layer) —
exit 0.
- `pnpm --filter @objectstack/spec test` — Test Files 912 passed / 1
failed (913), Tests 28895 passed / 1 failed: the dropped-refinements
ledger's header totals; corrected, `scripts/dropped-refinements.test.ts`
re-run 30/30. `typecheck` — exit 0.
- `pnpm --filter @objectstack/platform-objects test` — Test Files 130
passed (130), Tests 2218 passed (2218). `typecheck` — exit 0.
- `pnpm --filter @objectstack/spec check:generated` — two stale
artifacts found (reference page, liveness counts), regenerated with
`--fix`, re-checked green.
</details>
<details><summary>Derived gate families (dispatch-gates --commands,
120)</summary>
`node scripts/pm/dispatch-gates.mjs --ran` — "120 derived, 120 run, 0
NOT-MEASURED, 0 UNRUN"; every family exit 0 on the re-run. On the first
run three families refused with exit 3 (prerequisite not met: no built
CLI, client-react or package dists); each was built and re-run green,
and `check:i18n` then reported the form row's bundles drifted,
regenerated in this PR.
</details>
Not run locally: `check:i18n-coverage` (not derived for this diff;
prerequisite needs the example apps built) and every CI-only family; CI
reports them on this PR. Dogfood: n/a (no `!` in the title, no
`(narrowing)` arm).
## Acceptance notes
- **Files beyond the original claim's list, each forced by a Zone 1 line
(recorded by the claim amendment on the card):** the save refusal is
wired in `admitAuthoredSource` in `knowledge-service.ts` (the save-time
admission lives there, not in `knowledge-source-admission.ts`, which
holds the sentence); `dropped-refinements.baseline.json` (the parse rule
is a refinement, which the spec build refuses unledgered); the
platform-objects translation bundles and the catalog positive control
(forced by the form row the gate required).
- **Spec contract prose:** with the widened claim, this PR now updates
the TSDoc of `KnowledgeSearchOptions.granularity` and `groupBy`
(`packages/spec/src/contracts/knowledge-service.ts`) and the
`search_knowledge` description: a source's `documentBy` sets the unit
for record-level results. After the claim's second amendment, every
other sentence in those two files that stated the record-level unit as
"record" now names the document unit too: the `topK` TSDoc and the
`groupBy` "instead of by record" line in the contract; in the tool, the
`topK`, `granularity` and `groupBy` input descriptions, the matching
`SearchKnowledgeInput` and `SearchKnowledgeOutput.hits` TSDoc, and the
`granularity` refusal sentence. Left for #38538, outside those two
files: `content/docs/ai/knowledge-rag.mdx` (the `granularity` row) and
`content/docs/protocol/knowledge.mdx` (the record-level paragraph).
- **Not judged:** a source registered in code passes the parse
(membership in `metadataFields`) but no save door, so the parent-link
rule does not judge it; PR 2's `os validate` rule covers packaged
sources.
- **For PR 2 (the `os validate` rule), left undecided here:**
`@objectstack/lint` does not depend on `@objectstack/service-knowledge`,
and `parentLinkRule` / `documentUnitOf` are pure over the object
definition (they read `@objectstack/core`'s withheld-field collectors
and the spec's `referenceTargetOf`). Two ways for PR 2 to call them: (A)
add the `@objectstack/lint` to `@objectstack/service-knowledge`
dependency and import the functions, which first need a line in that
package's `index.ts`: today they are module exports only, not on its
`exports` surface; (B) move the pure rule to a package lint already
reads (for example `@objectstack/core`) and have
`knowledge-search-scope.ts` import it from there. PR 2 chooses.
- **Live check:** no pgvector or turso live suite touches record
granularity or `groupBy` (grouping runs in the service over adapter
answers), so no live cluster was started.
- **Overlap:** `overlap-check` is clean against `main` and #38509
(shares `knowledge-service.ts`). It reports textual conflicts with two
open drafts on hand-maintained counters: #38550 (the dropped-refinements
ledger and the catalog positive control) and #38497 (the positive
control). Whichever lands second re-applies its lines.
## Read first
1. `packages/services/service-knowledge/src/knowledge-service.ts` —
`groupKeyOf` / `documentKeyOf` (the reader) and the new arm in
`admitAuthoredSource`.
2. `packages/services/service-knowledge/src/knowledge-search-scope.ts` —
`parentLinkRule` replaces the body of `parentLinksOf`; check the scope
still behaves the same.
3. `packages/spec/src/ai/knowledge-source.zod.ts` — the key and its
refinement.
**Two-minute check:** `pnpm --filter @objectstack/service-knowledge exec
vitest run src/__tests__/document-by-38537.test.ts` — 16 passed,
including "…: 3 hits, each parent's best and the orphan".
Session: https://claude.ai/code/session_01FEkQ5oQLfL8thb6wtUHnTb
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: repo:objectstack#5 · os-project-manager ·
session_01FEkQ5oQLfL8thb6wtUHnTb -->
---------
Co-authored-by: Claude <noreply@anthropic.com>source.documentBy); record search groups by it by default (#38564)1 parent 3014176 commit 62eecbc
36 files changed
Lines changed: 784 additions & 41 deletions
File tree
- .changeset
- content/docs/references/ai
- packages
- platform-objects/src/apps/translations
- services/service-knowledge/src
- __tests__
- spec
- authorable-surface
- liveness
- state-counts
- 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| |||
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| 130 | + | |
129 | 131 | | |
130 | 132 | | |
131 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3553 | 3553 | | |
3554 | 3554 | | |
3555 | 3555 | | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3556 | 3560 | | |
3557 | 3561 | | |
3558 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3553 | 3553 | | |
3554 | 3554 | | |
3555 | 3555 | | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3556 | 3560 | | |
3557 | 3561 | | |
3558 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3553 | 3553 | | |
3554 | 3554 | | |
3555 | 3555 | | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3556 | 3560 | | |
3557 | 3561 | | |
3558 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3553 | 3553 | | |
3554 | 3554 | | |
3555 | 3555 | | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3556 | 3560 | | |
3557 | 3561 | | |
3558 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3553 | 3553 | | |
3554 | 3554 | | |
3555 | 3555 | | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
3556 | 3560 | | |
3557 | 3561 | | |
3558 | 3562 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
125 | 127 | | |
126 | 128 | | |
127 | 129 | | |
| |||
0 commit comments