Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/37233-record-context-outline-pages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/service-record-context': patch
'@objectstack/spec': minor
---

feat(service-record-context): get_record_context lists a long body's outline in pages, so every heading's anchor is issued however many the body holds

Clause-②: yes (widening)
Metadata-protocol: added

ADR-0160 D3 "The body". No pack key and no error code changes. The tool's published input widens by one optional member, `outlineFrom`, and its body answer by one, `outlineNext`.

- **The outline comes in pages.** An outline is still held to 80 entries and to the body bound per answer. Where a body has more headings than one answer lists, the answer's body carries `outlineNext`, and calling `get_record_context` again with the same arguments and `outlineFrom` set to it lists the next page. The pages run through every heading shallowest level first, then in body order, and each page lists its headings in body order: a nested body's first page is its top levels, as before; a body of many headings at one level is listed in runs. Before, a body whose headings all sat at one level issued no anchor for any heading past the first 80, so those sections could not be read.
- **The `outline_truncated` note now says how many headings the page lists, how many it does not, and the `outlineFrom` that lists the next page** (or that no later page lists more). It no longer says that a listed section's text holds the headings left out, which was false when every heading sat at one level.
- **A heading too long to list by itself no longer empties the outline.** No page lists it, the pages pass over it, and the note counts it; the other headings are listed. A body whose only headings are such headings is carried to the bound and marked truncated, as before.
- An `outlineFrom` past the last page, or on a body that is not an outline, is refused `RESOURCE_NOT_FOUND` (404), as an unresolved `anchor` is. A call naming both `anchor` and `outlineFrom` is refused `VALIDATION_ERROR` (400) before anything is read.
7 changes: 5 additions & 2 deletions content/docs/references/ai/record-context-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const result = GetRecordContextInputSchema.parse(data);
| **recordId** | `string` | ✅ | The record's `id`, as create_record and query_records answer it |
| **context** | `string` | optional | The context pack to read the record through, by name. Omit it when the object has one pack |
| **anchor** | `string` | optional | One section of the record's body, by the `anchor` an earlier answer's body outline issued |
| **outlineFrom** | `integer` | optional | Where the body outline's page starts: the `outlineNext` an earlier answer's body gave. Omit it for the first page. Not with `anchor` |


---
Expand Down Expand Up @@ -78,7 +79,8 @@ const result = GetRecordContextInputSchema.parse(data);
| **label** | `string` | ✅ | Its label at the caller's locale |
| **text** | `string` | optional | The body text, or the one section `anchor` asked for |
| **anchor** | `string` | optional | The section `text` holds, when the call named one |
| **outline** | `{ anchor: string; heading: string; level: integer }[]` | optional | The body's headings in order, when the body is past the bound and is not carried whole. Held to the service's outline bounds: a cut leaves out the deepest headings first and is stated by an `outline_truncated` note |
| **outline** | `{ anchor: string; heading: string; level: integer }[]` | optional | The body's headings in body order, when the body is past the bound and is not carried whole. One page, held to the service's outline bounds: the pages run through every heading short enough to list, shallowest level first, so a cut leaves the deepest headings to later pages, and is stated by an `outline_truncated` note, which also counts any heading too long for any page to list |
| **outlineNext** | `integer` | optional | Present when a heading a later page lists remains past this outline page: pass it as `outlineFrom` to list the next page. Absent on the last page, even when headings too long to list remain |
| **truncated** | `true` | optional | Present when `text` was cut at the bound: a long body with no headings, or a section past the bound |

### Nested Shape: `RecordContextBody.outline[number]`
Expand Down Expand Up @@ -148,7 +150,8 @@ const result = GetRecordContextInputSchema.parse(data);
| **label** | `string` | ✅ | Its label at the caller's locale |
| **text** | `string` | optional | The body text, or the one section `anchor` asked for |
| **anchor** | `string` | optional | The section `text` holds, when the call named one |
| **outline** | `{ anchor: string; heading: string; level: integer }[]` | optional | The body's headings in order, when the body is past the bound and is not carried whole. Held to the service's outline bounds: a cut leaves out the deepest headings first and is stated by an `outline_truncated` note |
| **outline** | `{ anchor: string; heading: string; level: integer }[]` | optional | The body's headings in body order, when the body is past the bound and is not carried whole. One page, held to the service's outline bounds: the pages run through every heading short enough to list, shallowest level first, so a cut leaves the deepest headings to later pages, and is stated by an `outline_truncated` note, which also counts any heading too long for any page to list |
| **outlineNext** | `integer` | optional | Present when a heading a later page lists remains past this outline page: pass it as `outlineFrom` to list the next page. Absent on the last page, even when headings too long to list remain |
| **truncated** | `true` | optional | Present when `text` was cut at the bound: a long body with no headings, or a section past the bound |


Expand Down
2 changes: 1 addition & 1 deletion packages/mcp/src/mcp-record-context-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('[ADR-0160 D4] get_record_context — registration', () => {
expect(stdio).toEqual(http);
expect(http.description).toBe(createRecordContextTools().definitions[0].description);
expect(http.annotations).toMatchObject({ readOnlyHint: true, destructiveHint: false });
expect(Object.keys(http.inputSchema.properties).sort()).toEqual(['anchor', 'context', 'objectName', 'recordId']);
expect(Object.keys(http.inputSchema.properties).sort()).toEqual(['anchor', 'context', 'objectName', 'outlineFrom', 'recordId']);
});

it('is not listed on a bridge without the record-context service, nor for a grant without data:read', async () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/runtime/src/mcp-record-context-tools-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel:
id: 'p1', title: 'Fire cover', status: 'active', secret_note: 'hush', vault: 'v1',
content: '# Scope\nCovers fire.\n# Exclusions\nFlood.', organization_id: 'org_1',
}, sys);
// A body of 300 headings at one level, past the body bound: its outline comes in pages.
await engine.insert('rc_policy', {
id: 'p2', title: 'Long cover', status: 'active', secret_note: 'hush', vault: 'v1',
content: Array.from({ length: 300 }, (_, i) => `## Clause ${i + 1}\nThe insurer pays clause ${i + 1}.\n`).join(''),
organization_id: 'org_1',
}, sys);
await engine.insert('rc_revision', { id: 'r1', version: 'v1', policy: 'p1', owner_id: 'usr_member', organization_id: 'org_1' }, sys);
await engine.insert('rc_revision', { id: 'r2', version: 'v2', policy: 'p1', owner_id: 'usr_other', organization_id: 'org_1' }, sys);
await engine.insert('rc_hidden', { id: 'h1', version: 'h', policy: 'p1', organization_id: 'org_1' }, sys);
Expand Down Expand Up @@ -871,6 +877,26 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel:
expect(answer.payload.error).toMatchObject({ code: 'RESOURCE_NOT_FOUND', status: 404 });
});

it('[ADR-0160 D3] a long body\'s outline pages on both doors: outlineFrom reaches the executor through each, and the page it lists issues anchors that read their sections', async () => {
const first = await both({ objectName: 'rc_policy', recordId: 'p2', context: 'rc_main' });
expect(first.isError).toBe(false);
const next = first.payload.data.body.outlineNext;
expect(next).toBe(first.payload.data.body.outline.length);
const page = await both({ objectName: 'rc_policy', recordId: 'p2', context: 'rc_main', outlineFrom: next });
expect(page.isError).toBe(false);
// A door that dropped the key would answer the first page again.
expect(page.payload.data.body.outline[0]).toEqual({ anchor: `clause-${next + 1}`, heading: `Clause ${next + 1}`, level: 2 });
const anchor = page.payload.data.body.outline.at(-1).anchor;
const section = await both({ objectName: 'rc_policy', recordId: 'p2', context: 'rc_main', anchor });
expect(section.payload.data.body.text).toMatch(/^## Clause \d+\nThe insurer pays clause \d+\.\n$/);
});

it('[ADR-0160 D3] an outlineFrom on a body carried whole is one coded answer on both doors', async () => {
const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main', outlineFrom: 80 });
expect(answer.isError).toBe(true);
expect(answer.payload.error).toMatchObject({ code: 'RESOURCE_NOT_FOUND', status: 404 });
});

it('a pack served from the metadata registry (saved at run time, not shipped by the stack) is read', async () => {
await protocol.saveMetaItem({
type: 'ai_context', name: 'rc_registry',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('[ADR-0160 D3] the outline is bounded whatever the body holds', () => {

it('a heading too long to list within the bound is left out, never carried past it', () => {
const headings = md([`# ${'x'.repeat(RECORD_CONTEXT_BODY_MAX_CHARS)}`]);
expect(boundedOutline(headings)).toEqual({ outline: [], omitted: 1 });
expect(boundedOutline(headings)).toEqual({ outline: [], omitted: 1, unlistable: 1 });
});

it('a listed anchor is the one the reader gave, cut or not', () => {
Expand All @@ -160,3 +160,72 @@ describe('[ADR-0160 D3] the outline is bounded whatever the body holds', () => {
expect(outline.map((h) => h.anchor)).toEqual(['notes', ...Array.from({ length: 79 }, (_, i) => `notes-${i + 2}`)]);
});
});

describe('[ADR-0160 D3] the outline comes in pages that list every heading', () => {
const md = (lines: string[]) => headingsOf(lines.join('\nbody text\n'), false);
/** Every page from the first, following `next`, each held to both bounds. */
const pagesOf = (headings: ReturnType<typeof md>) => {
const pages: Array<ReturnType<typeof boundedOutline>['outline']> = [];
let from: number | undefined = 0;
// Bounded, so pages that never advance fail here rather than loop.
while (from !== undefined && pages.length <= headings.length) {
const page = boundedOutline(headings, from);
expect(page.outline.length).toBeGreaterThan(0);
expect(page.outline.length).toBeLessThanOrEqual(RECORD_CONTEXT_OUTLINE_MAX_ENTRIES);
expect(JSON.stringify(page.outline).length).toBeLessThanOrEqual(RECORD_CONTEXT_BODY_MAX_CHARS);
expect(page.omitted).toBe(headings.length - page.outline.length);
pages.push(page.outline);
from = page.next;
}
return pages;
};

it('a flat body\'s pages are runs of it in body order, and together list every heading once', () => {
const headings = md(Array.from({ length: 300 }, (_, i) => `## Clause ${i + 1}`));
const pages = pagesOf(headings);
expect(pages.map((p) => p.length)).toEqual([80, 80, 80, 60]);
expect(pages.flat().map((h) => h.anchor)).toEqual(headings.map((h) => h.anchor));
expect(boundedOutline(headings, 80).next).toBe(160);
expect(boundedOutline(headings, 240)).not.toHaveProperty('next');
});

it('a nested body\'s first page is its top levels, and later pages list the deeper headings left out', () => {
const lines: string[] = [];
for (let a = 1; a <= 10; a++) {
lines.push(`# Part ${a}`);
for (let b = 1; b <= 20; b++) lines.push(`## Part ${a} clause ${b}`);
}
const headings = md(lines);
const pages = pagesOf(headings);
expect(pages[0].filter((h) => h.level === 1)).toHaveLength(10);
expect(pages.slice(1).flat().every((h) => h.level === 2)).toBe(true);
const listed = pages.flat().map((h) => h.anchor);
expect(new Set(listed).size).toBe(headings.length);
expect([...listed].sort()).toEqual(headings.map((h) => h.anchor).sort());
// Each page lists its headings in body order.
for (const page of pages) {
const at = page.map((h) => headings.findIndex((x) => x.anchor === h.anchor));
expect(at).toEqual([...at].sort((x, y) => x - y));
}
});

it('a page is held to the character bound before the entry cap, and the next page starts where it stopped', () => {
const headings = md(Array.from({ length: 40 }, (_, i) => `# ${'Long heading '.repeat(25)}${i}`));
const first = boundedOutline(headings);
expect(first.next).toBe(first.outline.length);
expect(pagesOf(headings).flat().map((h) => h.anchor)).toEqual(headings.map((h) => h.anchor));
});

it('a heading too long to list by itself is on no page, counted, and never empties a page the others fit on', () => {
// The shallowest heading, first in the order the pages run in, is the one too long to list.
const headings = md([`# ${'x'.repeat(RECORD_CONTEXT_BODY_MAX_CHARS)}`, ...Array.from({ length: 100 }, (_, i) => `## Clause ${i + 1}`)]);
const first = boundedOutline(headings);
expect(first.outline).toHaveLength(RECORD_CONTEXT_OUTLINE_MAX_ENTRIES);
expect(first.outline[0]).toEqual({ anchor: 'clause-1', heading: 'Clause 1', level: 2 });
expect(first).toMatchObject({ omitted: 101 - RECORD_CONTEXT_OUTLINE_MAX_ENTRIES, next: 81, unlistable: 1 });
const last = boundedOutline(headings, 81);
expect(last.outline.map((h) => h.anchor)).toEqual(headings.slice(81).map((h) => h.anchor));
expect(last).not.toHaveProperty('next');
expect(last.unlistable).toBe(1);
});
});
Loading
Loading