Repository navigation
Commit db46566
feat(spec,service-record-context): the record citation shape, and the verifier that checks a quote as the asking user (ADR-0160 D9) (#37396)
Fixes #37364
Clause-②: yes
Metadata-protocol: added
Skills-docs: follow-up #37177
## 维护者速读
- 用户能看到什么变化:用户无感。本次只交付"引用"的格式定义和核对器;AI 回答里的引用还没有产生方,也没有界面展示。
- 为什么做:记录范围 AI 问询专题的第 10
行前半(引用格式与核对器),按维护者已定的三条裁决实现;界面展示和云端产生引用两张后续卡依赖它。
-
出错会怎样:最坏是核对器把一条真实的引用判为"未核对通过"而丢掉,回答少一条引用;它以提问人本人的权限读记录,不会让提问人看到无权看的内容;撤回此
PR 即可,没有数据需要迁移。
- 要你做什么:无。
## What changes
Governing text: ADR-0160 D9 as amended by #37370 (third amendment),
ruling 7177410380 on #37144 (Q1 A, Q2 A, Q3 A), ADR point 23.
**Spec (`@objectstack/spec/ai`)**
- `record-citation.zod.ts` (new): `RecordCitationSchema` = `{ object,
id, context?, anchor?, quote }`, closed with `strictObject` (unknown key
refused, near-miss aliases such as
`objectName`/`recordId`/`pack`/`section`/`text`), every string `min(1)`.
`RecordCitationsSchema` = an array of at most `RECORD_CITATIONS_MAX`
(20). `RECORD_CITATIONS_PART_TYPE = 'data-record-citations'`: the
UI-message data part that carries the verified list (the precedent is
`BUILD_PROGRESS_FRAME_TYPE`).
- `record-body-anchor.ts` (new): `headingsOf` and `slugOf` moved here
unchanged from `service-record-context/src/body-outline.ts`, together
with the private readers they need. This way the server and the console
derive the same anchor. `slugOf` stays module-private, as before. The
tag reader `headingsOf` already used is exported as `stripBodyTags`,
because the verifier matches an html body with the same notion of a tag.
- Generated: `authorable-surface/ai.json` (5 `RecordCitation` rows),
`json-schema.manifest`, `api-surface`, `export-origins`,
`declaration-map`, `content/docs/references/ai/*`, and the
strictness-ledger count (`ai/` 113 to 114), all written by
`check:generated --fix`.
**Service (`@objectstack/service-record-context`)**
- `body-outline.ts` re-imports `headingsOf` and `BodyHeading` from spec
and re-exports them. `body-outline.test.ts` passes untouched.
- `verify-citations.ts` (new), `verifyRecordCitations(citations, door)`,
exported from `src/index.ts`. It never persists or renders.
**The matching rule, which ADR-0160 D9 leaves to row 10 and which the
card's What 3 settles.** A citation survives only when its trimmed
`quote` is a substring of one of these data-member strings of
`get_record_context`'s answer for its `(object, id, context, anchor)`,
read through the door bound to the asking user:
- a field entry's `value` when it is a string, and its `display`, at the
root and in every related-list cell (Q3: a related-row quote is cited on
the root record);
- `body.text` (the whole body, or the section `anchor` named). An
`html`/`richtext` body (judged by the type the caller is served) is also
matched with its tags removed.
Whitespace runs collapse to one space on both sides. Case and every
other character must agree. These are never matched: notes, the framing
sentence, any label, the pack/record header (`pack.*`, `record.*`, a
row's `displayName`), outline headings, and a value that is not a string
(a number, boolean, list or object; its `display` is matched where
present). A quote that is empty once trimmed verifies nothing.
**What the verifier does with the list.** It makes one
`executeGetRecordContext` call per distinct tuple. Any refused call
drops its citations (point 23): a `context` no pack has, several packs
and no `context` (Q2), an anchor that does not resolve, a record the
caller cannot read or that does not exist, an object not served, guarded
or unexposed. No retry without the offending argument. An entry that
does not parse is dropped, and only the first 20 entries are read. The
result is the verified subset in input order, with no reason attached,
so a hidden record and a missing one look alike.
<details><summary>Read first (by risk)</summary>
1. `packages/services/service-record-context/src/verify-citations.ts`:
`haystackOf` holds the matching rule above. The risky part is which
strings it collects.
2. `packages/spec/src/ai/record-citation.zod.ts`: the public shape, the
aliases and the cap of 20.
3. `packages/services/service-record-context/src/body-outline.ts`: the
move. Byte-identical logic now lives in
`packages/spec/src/ai/record-body-anchor.ts` (only `stripTags` was
renamed to the exported `stripBodyTags`).
</details>
## Tests
- `packages/spec/src/ai/record-citation.test.ts` (6): the shape refuses
an unknown key (`unrecognized_keys`), an empty `quote` (`too_small`), a
missing `quote` and `null` (`invalid_type`), an empty
`context`/`anchor`. It admits `context` and `anchor`. The list is capped
at 20 (`too_big`). The part type is pinned.
-
`packages/services/service-record-context/src/__tests__/verify-citations.test.ts`
(16), one per acceptance bullet plus the rule. Each "dropped" case has a
control leg:
- section by anchor verified, stale anchor refused, anchor scopes the
match;
- related-list cell verified on the root, while another policy's row is
not;
- note, framing and labels refused (the control shows the answer carries
each);
- withheld and hidden field refused (the control verifies the same quote
unhidden);
- outline body without anchor refused, the same quote with its anchor
verified;
- two packs without `context` refused, one pack verified, named packs
verified, unknown pack refused;
- no read access (unreadable row, missing id, unserved, guarded,
unexposed object) all answer `[]`;
- one call per distinct tuple (`listCalls` = 2 for 5 citations),
unparsable entries dropped, cap of 20, html/richtext tag-stripped and
markdown not, whitespace collapse, non-string value not matched.
<details><summary>Readings (head 36ec0cf)</summary>
- `pnpm --filter @objectstack/service-record-context test`: Test Files 3
passed (3), Tests 115 passed (115). `typecheck`: exit 0.
- `pnpm --filter @objectstack/spec test`: first run on c46e26d had 1
failure, `alias-integrity.test.ts` (aliases `recordId` and `record_id`
collapse onto one probe). Fixed in 36ec0cf by dropping `record_id`.
Re-run of that file plus `record-citation.test.ts`: 26/26. The final
full-suite reading is in the report.
- `pnpm --filter @objectstack/spec typecheck`: exit 0.
- Ablation, one behaviour (the data-member restriction):
`scripts/ablation-replace.mjs` made `haystackOf` also collect
`answer.framing`, the note texts and `pack.label`. Result: anchor 1 to
0, blob c35fc72 to dcec1804, and `refuses a quote that appears only in
a note, the framing, or a label` went red (1 failed / 15 passed).
Restored: blob equals HEAD, `git diff HEAD` empty. A first attempt was a
no-op that the tool refused before running: its replacement contained
the anchor. The test runs `src` directly, so no dist rebuild was needed
for the mutated file.
</details>
## Acceptance notes
- **The cap of 20** is the card's "an array with a cap" turned into a
number. It bounds both how many calls a model's list can trigger and the
persisted list. Easy to change before 10b/10c read it.
- **Tag-stripping removes a tag with nothing in its place**, as
`headingsOf` reads heading text. So a quote that spans two paragraph
elements, written with a space where the two paragraphs meet, is not
verified. HTML entities in the body are not decoded either: an ampersand
entity has to be quoted as the entity. Both follow the card's rule as
written. They only make verification stricter, never looser.
- **Overlap**: #37366 also writes the generated
`packages/spec/export-origins/ai.json`. Whichever PR lands second
regenerates it.
- Out of scope, as the card says: an MCP verify tool, rendering (10b),
producing and persisting citations (cloud 10c).
## Verification
Two minutes: `pnpm --filter @objectstack/service-record-context exec
vitest run --maxWorkers=2 src/__tests__/verify-citations.test.ts`.
Expect 16 passed. The `context (Q2)` block shows a two-pack object
dropping a citation that has no `context`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01Utp6CbeF2saT6wUBV4E9uc)_
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6CbeF2saT6wUBV4E9uc -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 51b67f1 commit db46566
20 files changed
Lines changed: 853 additions & 177 deletions
File tree
- .changeset
- content/docs/references
- ai
- docs/audits/2026-07-unknown-key-strictness-ledger.counts
- packages
- services/service-record-context/src
- __tests__
- spec
- api-surface
- authorable-surface
- declaration-map
- export-origins
- json-schema.manifest
- src/ai
| 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 | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
0 commit comments