Repository navigation
Commit 59571b2
feat(service-record-context): get_record_context lists a long body's outline in pages (outlineFrom / outlineNext) (#37484)
Fixes #37233
Clause-②: yes (widening)
Metadata-protocol: added
## 维护者速读
- 用户能看到什么变化:外接 AI
读一条正文很长的记录时,目录超过一页上限的部分可以分页继续列出,每个章节都能读到;此前如果几百个标题都在同一层,第 80 个之后的章节 AI
根本读不到。
- 为什么做:记录级 AI 问答专题(资料包读取工具)的后续收尾,补上目录截断后剩余章节无法到达的缺口,并改正一句会误导 AI 的截断提示。
- 出错会怎样:最坏是 AI 读长文时多翻一页或少列几个标题,读到的内容仍受原有长度上限和权限约束;可以直接撤回本次改动。
- 要你做的:无。
## What changes
ADR-0160 D3 "The body"; L1 ACCEPT 7153812992 on PR #37222, nit N2 (this
card), N3, N4.
- **The outline comes in pages** (`boundedOutline(headings, from)`,
`body-outline.ts`). The pages run through every heading in ONE order —
shallowest level first, body order within a level — and a page takes
that order from `from` until the next heading would pass either existing
bound (80 entries, 8,000 serialized characters); each page lists its
headings in body order. Page 1 is today's outline, bar the N3 case
below. Every heading is on exactly one page, so following the
continuation from page 1 lists every heading with the anchor
`headingsOf` gives it. One mechanism serves both cuts: a flat body's
pages are runs of it in body order; a nested body's first page is its
top levels and later pages carry the deeper headings (which matters too:
a top section past the body bound is itself cut, so its sub-headings
past the cut were unreachable before as well).
- **Input `outlineFrom`, output `body.outlineNext`**
(`record-context-tools.zod.ts`). `outlineNext` is present only when a
heading remains that a later page lists; pass it as `outlineFrom`.
`outlineFrom` 0 is page 1. An `outlineFrom` past the last page, or on a
body that is no outline (carried whole, absent from the pack, withheld
from the caller), is ONE answer, `RESOURCE_NOT_FOUND` 404, as an
unresolved `anchor` is. `anchor` with `outlineFrom` is refused
`VALIDATION_ERROR` 400 at step 1, before any read.
- **The `outline_truncated` sentence, en and zh-Hans** (`sentences.ts`),
now says how many headings this page lists, how many it does not, that
pages run shallowest levels first then body order, and the `outlineFrom`
that lists the next page (or that no later page lists more). The clause
"A listed section's text includes the headings under it" is gone: in a
flat cut nothing left out sits under a listed section. The sentence
carries numbers only, never an anchor (an anchor is record text, and a
note never carries record text) — which is why the cursor is a position
and not an anchor.
- **N3, revisited because the paging made it cheap.** A heading whose
entry alone passes the character bound fits no page. Without a change it
would end the pages (the page that must start with it lists nothing), so
the pages now pass over it and the note counts it ("1 heading is too
long for any page to list, so its anchor is not given" / "K headings are
too long ..."). As a side effect one such heading no longer empties page
1. A body whose ONLY headings are such headings is still carried to the
bound and marked truncated, as before. Trade-off stated: such a heading
(about 3,950 characters or more of heading text) gets no anchor; in a
nested body its text is still inside its parent's section.
- **N4**: no change needed (the changeset it named has merged).
- Tool description: names `outlineNext` / `outlineFrom` beside `anchor`.
- **The `body_outline` note, en and zh-Hans, on every page** now says to
read a section with the same objectName, recordId and context, plus the
anchor and no `outlineFrom`. Before, it said "the same arguments", and
on a page reached through `outlineFrom` those arguments plus an anchor
are the pair this PR refuses (review S1).
- The MCP doors needed no code: both pass the input through whole, and
the `/mcp` door's registration validates against the spec's
`GetRecordContextInputSchema`, so the key is admitted there by the spec
change alone. The real-kernel parity cases below prove both doors carry
it.
### Design, and the alternative weighed
Continuation (chosen) vs listing every omitted heading's anchor
compactly: a compact list is still O(headings) in one answer, so for a
large enough body it passes the same bound and needs a cut of its own;
the continuation keeps every answer within the existing bounds however
big the body, with one mechanism for flat and nested cuts. Cursor: a
position in the page order, not an anchor, because the note that tells
the model where to continue may not carry record text, and an anchor is
derived from heading text.
### Declaration
`Metadata-protocol: added`, `@objectstack/spec` graded `minor` (PM
ruling, option A, on this PR's open question): the spec's
authorable-surface ledger records `GetRecordContextInput:outlineFrom`
and `RecordContextBody:outlineNext` as entered keys, and Check Changeset
reads that shard as a metadata-protocol addition (precedent: #37140,
#37354). `@objectstack/service-record-context` stays `patch`.
### ADR text this will need (governed, not edited here)
- D4 "Definition", the input line `{ objectName: string, recordId:
string, context?: string, anchor?: string }` gains `outlineFrom?:
integer` (the output's body gains `outlineNext`).
- D3 "The body", the sentence "A longer body is carried as an outline:
its headings in order, each with an `anchor`." needs: an outline past
its bounds comes in pages, continued by `outlineFrom`, every heading
listed on one page (bar a heading too long to list by itself, stated).
Carried by #37490, which should also say that the cursor is a position
in the current body, so "exactly one page" holds for one state of the
body.
## Tests
See Verification for the commands and readings.
- `body-outline.test.ts`: a flat body of 300 headings pages as
80/80/80/60 and the pages together are exactly the body's headings in
order; a nested body's page 1 holds every top heading and later pages
only deeper ones, each page in body order, union = all headings, no
duplicates; the character bound before the entry cap; an over-long
shallowest heading is on no page, counted, and does not empty page 1.
- `record-context-tools.test.ts` (executor): **the acceptance pin** — a
body of 300 headings at one level, every page walked by the
`outlineNext` the tool gave, all 300 anchors issued once, every section
read by its issued anchor and equal to its text, every answer parsing
against `GetRecordContextOutputSchema` and within the bounds; the nested
cut whose top section is itself cut; the `outline_truncated` sentence
for a flat cut AND a nested cut in en AND zh-Hans (counts, `outlineFrom
N`, no record text, the old clause absent); the last page; `outlineFrom:
0`; the one 404 answer (past the end / body whole / no body / withheld
body); `anchor` + `outlineFrom` refused before any read; negative /
fractional / string refused at parse.
- `record-context-tools.zod.test.ts` (spec, new): admits and refuses
`outlineFrom` and `outlineNext`, asserting zod's issue code and path;
the JSON Schema projection carries `outlineFrom` as an integer of at
least 0.
- `mcp-record-context-tools-bridge.test.ts` (runtime, real kernel): page
2 over both doors, byte-equal, starts at `clause-(N+1)` (a door that
dropped the key would answer page 1), and its anchor reads its section;
`outlineFrom` on a body carried whole is the same coded 404 on both
doors.
- `mcp-record-context-tools.test.ts`: the registration's input property
list gains `outlineFrom`.
## Acceptance notes
- Acceptance bullet 1 (300 same-level headings, every section reached by
issued anchors, each answer bounded): pinned in
`record-context-tools.test.ts`, "a body of 300 headings at one level".
- Acceptance bullet 2 (the note sentence true for nested and flat cuts,
both pinned): "the cut note is true for a flat cut and for a nested cut,
in en and zh-Hans".
- Re-reading `outlineFrom` against a body edited between calls simply
answers the page at that position of the current body; anchors on it are
the current body's, so nothing stale is served.
## Verification
Read first (by risk):
`packages/services/service-record-context/src/body-outline.ts`
(`boundedOutline`: the skip of an unlistable heading and where `next` is
set), `src/record-context-tools.ts` (the body block: the one 404 for an
outline page that does not start where asked), `src/sentences.ts`
(`outlineTruncated`).
Two-minute check: `pnpm --filter @objectstack/service-record-context
exec vitest run --maxWorkers=2
src/__tests__/record-context-tools.test.ts -t "300 headings"` — one test
passes; it walks four pages and reads 300 sections.
All readings at head `5f6f72fd82540c66a716be2a3a874bbd89e58bbd`.
<details><summary>Suites of every touched package (under the verify
lock: VERDICT command-exit 0)</summary>
- Build of the three closures exit 0; `pnpm --filter @objectstack/spec
check:generated`: all 14 generated artifacts up to date.
- `@objectstack/service-record-context` test: Test Files 4 passed (4),
Tests 139 passed (139); typecheck exit 0.
- `@objectstack/mcp` test: Test Files 85 passed (85), Tests 1521 passed
(1521); typecheck exit 0.
- `@objectstack/runtime` test (`--project local`): Test Files 649 passed
(649), Tests 12061 passed, 25 skipped (12086); typecheck exit 0.
- `@objectstack/spec` test: Test Files 899 passed (899), Tests 28494
passed, 1 todo (28495); typecheck exit 0.
</details>
<details><summary>Derived gate families (dispatch-gates --commands,
111)</summary>
- 109 exit 0 (11 of them re-run after the build, having first refused
with no built output to read).
- NOT MEASURED, `check:skill-examples` and `check:dual-build-cjs-loads`:
PREREQUISITE NOT MET, they read built output of packages this diff does
not touch. CI's Lint and Repo Gates measures both.
</details>
<details><summary>Review S1 pin, and the ablation of the first
round</summary>
- S1: "a section listed on page 2 is read exactly as that page's
body_outline note says" reads page 2's first anchor with the record's
arguments, the anchor and no `outlineFrom` (en and zh-Hans), and gets
the section. The control, page 2's arguments plus the anchor, is refused
`VALIDATION_ERROR` 400.
- Ablation (first round, unchanged code path): `for (let at = from; ...`
replaced by `for (let at = 0; ...` in `boundedOutline` turned the
300-heading acceptance pin red (24080 anchors issued, not 300), along
with three other executor pins; restored blob equals HEAD.
</details>
---
_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 98cc0e4 commit 59571b2
12 files changed
Lines changed: 477 additions & 46 deletions
File tree
- .changeset
- content/docs/references/ai
- packages
- mcp/src
- runtime/src
- services/service-record-context/src
- __tests__
- spec
- authorable-surface
- 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
81 | | - | |
| 82 | + | |
| 83 | + | |
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
| |||
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
151 | | - | |
| 153 | + | |
| 154 | + | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
189 | | - | |
| 189 | + | |
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
508 | 514 | | |
509 | 515 | | |
510 | 516 | | |
| |||
871 | 877 | | |
872 | 878 | | |
873 | 879 | | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
874 | 900 | | |
875 | 901 | | |
876 | 902 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 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 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
0 commit comments