diff --git a/.changeset/37486-related-undefined-object-states-nothing.md b/.changeset/37486-related-undefined-object-states-nothing.md new file mode 100644 index 00000000000..874fe23ebd9 --- /dev/null +++ b/.changeset/37486-related-undefined-object-states-nothing.md @@ -0,0 +1,12 @@ +--- +'@objectstack/service-record-context': patch +--- + +fix(service-record-context): `get_record_context` states nothing for a `related` object the stack does not define + +Clause-②: no +Metadata-protocol: none + +A context pack whose `related` entry names an object no package and no environment defines (a misspelt name, or a package removed after the pack was written) is an author defect: `os validate` reports it (`ai-context-object-unknown`), and ADR-0160 D1 says the tool answer says nothing about it. Until now the section was left out and every caller was told "Some of the material this pack covers is not visible to you", which reads as a permission narrowing nobody configured. + +The section is still left out. The not-visible statement now fires only when the stack publishes the object and this caller is not served it, as it did before for a defined child. The judgement reads the published definition, which is the same for every caller, so the answer for one caller reveals nothing about another caller's access: two callers get the same bytes for a pack with an undefined `related` object, on the `/mcp` door and the stdio door alike. Nothing of the published definition is carried in the answer. diff --git a/packages/runtime/src/mcp-record-context-tools-bridge.test.ts b/packages/runtime/src/mcp-record-context-tools-bridge.test.ts index 338f96d412e..df1c273995b 100644 --- a/packages/runtime/src/mcp-record-context-tools-bridge.test.ts +++ b/packages/runtime/src/mcp-record-context-tools-bridge.test.ts @@ -453,6 +453,8 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel: }, child('rc_closed', 'Closed', { enable: { apiEnabled: false } }), child('rc_hidden', 'Hidden'), + // Served to `usr_member` alone (its `rc_unmasker` grant), not to `usr_plain`. + child('rc_partner', 'Partner'), { name: 'rc_vault', label: 'Vault', sharingModel: 'public_read_write', enable: { apiEnabled: false }, fields: { label: { name: 'label', type: 'text', label: 'Label' } }, @@ -494,6 +496,15 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel: }, { name: 'rc_exposure_child', label: 'Exposure child', object: 'rc_policy', fields: ['title'], related: [{ object: 'rc_closed', fields: ['version'] }] }, { name: 'rc_disabled_child', label: 'Disabled child', object: 'rc_policy', fields: ['title'], related: [{ object: 'rc_off', fields: ['version'] }] }, + // [ADR-0160 D1] An author defect: `rc_ghost` is an object the stack does not define. + { + name: 'rc_misspelt_child', label: 'Misspelt child', object: 'rc_policy', fields: ['title'], + related: [{ object: 'rc_ghost', fields: ['version'] }, { object: 'rc_step', fields: ['label'], orderBy: [{ field: 'rank', order: 'asc' }] }], + }, + { + name: 'rc_role_child', label: 'Role child', object: 'rc_policy', fields: ['title'], + related: [{ object: 'rc_partner', fields: ['version'] }, { object: 'rc_step', fields: ['label'], orderBy: [{ field: 'rank', order: 'asc' }] }], + }, ], } as never); engine.registerApp({ @@ -516,6 +527,7 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel: 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); + await engine.insert('rc_partner', { id: 'pt1', version: 'pt', policy: 'p1', organization_id: 'org_1' }, sys); for (const [id, stage, rank, at] of [ ['s1', 'open', 1, '2026-11-01T00:00:00.000Z'], ['s2', 'open', 3, '2026-11-02T00:00:00.000Z'], @@ -530,12 +542,12 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel: const sets = [PermissionSetSchema.parse({ name: 'rc_member', label: 'Member', isDefault: true, - objects: { '*': READ_ALL, rc_hidden: READ_NONE }, + objects: { '*': READ_ALL, rc_hidden: READ_NONE, rc_partner: READ_NONE }, fields: { 'rc_policy.secret_note': { readable: false, editable: false } }, rowLevelSecurity: [{ name: 'rc_own_revisions', object: 'rc_revision', operation: 'select', using: 'owner_id == current_user.id' }], }), PermissionSetSchema.parse({ // Granted to `usr_member` alone (its `sys_user_permission_set` row below). - name: 'rc_unmasker', label: 'Unmasker', systemPermissions: ['rc_unmask'], objects: {}, + name: 'rc_unmasker', label: 'Unmasker', systemPermissions: ['rc_unmask'], objects: { rc_partner: READ_ALL }, })]; const services: Record = { manifest: { register: vi.fn() }, @@ -763,6 +775,38 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel: expect(answer.text).not.toContain('Hidden'); }); + it('[ADR-0160 D1/D3] a related entry naming an object the stack does not define leaves its section out and states nothing: the same bytes for both callers, on both doors', async () => { + // Precondition: no package and no environment defines it, so it is an author defect (`os validate`). + expect(engine.getSchema('rc_ghost')).toBeUndefined(); + expect(await protocol.getMetaItems({ type: 'object' } as never)).not.toContainEqual(expect.objectContaining({ name: 'rc_ghost' })); + const member = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_misspelt_child' }); + const plain = await bothAsPlain({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_misspelt_child' }); + for (const answer of [member, plain]) { + expect(answer.isError).toBe(false); + // The other entry still answers; the undefined one has no section, no name and no statement. + expect(answer.payload.data.related.map((l: { object: string }) => l.object)).toEqual(['rc_step']); + expect(answer.payload.data.related[0].rows.map((r: { id: string }) => r.id)).toEqual(['s1', 's3', 's2', 's4']); + expect(answer.payload.notes).toEqual([]); + expect(answer.text).not.toContain('rc_ghost'); + } + // Who asks changes nothing: one answer, byte for byte. + expect(plain.text).toBe(member.text); + }); + + it('[ADR-0160 D3 as amended] a defined child served to one caller and not the other: its section for the one, the not-visible statement for the other, on both doors', async () => { + const member = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_role_child' }); + expect(member.isError).toBe(false); + expect(member.payload.data.related.map((l: { object: string }) => l.object)).toEqual(['rc_partner', 'rc_step']); + expect(member.payload.data.related[0].rows.map((r: { id: string }) => r.id)).toEqual(['pt1']); + expect(member.payload.notes).toEqual([]); + const plain = await bothAsPlain({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_role_child' }); + expect(plain.isError).toBe(false); + expect(plain.payload.data.related.map((l: { object: string }) => l.object)).toEqual(['rc_step']); + expect(plain.payload.notes.map((n: { kind: string }) => n.kind)).toEqual(['not_visible']); + expect(plain.payload.notes[0]).not.toHaveProperty('object'); + for (const word of ['rc_partner', 'Partner', 'pt1']) expect(plain.text).not.toContain(word); + }); + it('a related child its API exposure refuses refuses the call, named (the caller\'s schema shows it)', async () => { const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_exposure_child' }); expect(answer.isError).toBe(true); diff --git a/packages/services/service-record-context/src/__tests__/record-context-tools.test.ts b/packages/services/service-record-context/src/__tests__/record-context-tools.test.ts index f09869dbd9e..b02eb34b299 100644 --- a/packages/services/service-record-context/src/__tests__/record-context-tools.test.ts +++ b/packages/services/service-record-context/src/__tests__/record-context-tools.test.ts @@ -273,15 +273,60 @@ describe('get_record_context — object level: a gate that refuses everyone refu const text = JSON.stringify(out); expect(text).not.toContain('policy_revision'); expect(text).not.toContain('Policy revision'); - // Nothing of the child is read after its object-level judgement, its published definition included. + // Nothing of the child is read after its object-level judgement, which asks its published + // definition once, last, for whether the stack defines it at all (ADR-0160 D1). expect(door.asked.filter((a) => a.object === 'policy_revision').map((a) => a.member)) - .toEqual(['readGuard', 'assertApiExposed', 'servedObjectDefinitionOf']); + .toEqual(['readGuard', 'assertApiExposed', 'servedObjectDefinitionOf', 'publishedObjectDefinitionOf']); // Control: served, both children answer and nothing is stated. const control = await run(twoChildren()); expect(control.data.related!.map((l) => l.object)).toEqual(['policy_revision', 'claim']); expect(control.notes).toEqual([]); }); + it('[ADR-0160 D1/D3] a child the stack does not define is left out stating nothing, the same answer whoever asks', async () => { + // `policy_ghost` has no published definition: an author defect, reported by `os validate`. + const ghost = (overrides: Partial = {}): FakeApp => { + const app = twoChildren(overrides); + (app.packs[0] as { related: Array> }).related[0] = { object: 'policy_ghost', field: 'policy', fields: ['version'] }; + return app; + }; + const door = fakeDoor(ghost()); + const out = await executeGetRecordContext(CALL, door); + expect(out.data.related!.map((l) => l.object)).toEqual(['claim']); + expect(out.notes).toEqual([]); + expect(JSON.stringify(out)).not.toContain('policy_ghost'); + expect(door.asked.filter((a) => a.object === 'policy_ghost').map((a) => a.member)) + .toEqual(['readGuard', 'assertApiExposed', 'servedObjectDefinitionOf', 'publishedObjectDefinitionOf']); + // A second caller, one the root's other child is hidden from, gets the same answer for the ghost: + // whether it states anything is the stack's published definitions, never the caller's schema. + const other = await run(ghost({ hiddenFields: { claim: ['number'] } })); + expect(other.data.related!.map((l) => l.object)).toEqual(['claim']); + expect(other.notes).toEqual([{ kind: 'not_visible', text: sentencesFor([]).notVisible }]); + const alone = await run(ghost({ unservedObjects: ['claim'] })); + expect(alone.data.related ?? []).toEqual([]); + // Only the defined child the caller is not served is stated. + expect(alone.notes).toEqual([{ kind: 'not_visible', text: sentencesFor([]).notVisible }]); + const withoutClaim = ghost(); + (withoutClaim.packs[0] as { related: unknown[] }).related.pop(); + expect((await run(withoutClaim)).notes).toEqual([]); + }); + + it('[ADR-0160 D1/D3] a left-out child\'s published-definition read that throws: a coded refusal refuses unnamed, a failure fails, never stated or dropped', async () => { + const coded = fakeDoor(twoChildren({ unservedObjects: ['policy_revision'], unpublishedObjects: ['policy_revision'] })); + const refused = await refusalOf(executeGetRecordContext(CALL, coded)); + expect(refused).toMatchObject({ code: 'PERMISSION_DENIED', status: 403 }); + expect(refused.message).not.toContain('policy_revision'); + expect(coded.reads).toEqual([]); + const failing = fakeDoor(twoChildren({ unservedObjects: ['policy_revision'] })); + const publishedOf = failing.publishedObjectDefinitionOf.bind(failing); + const thrown = new Error('store down: policy_revision'); + failing.publishedObjectDefinitionOf = async (object) => { + if (object === 'policy_revision') throw thrown; + return publishedOf(object); + }; + await expect(executeGetRecordContext(CALL, failing)).rejects.toMatchObject({ code: 'INTERNAL_ERROR', status: 500, cause: thrown }); + }); + it('[as amended] a lookup target reached only through a left-out child is not judged: it refuses nothing', async () => { const app = twoChildren({ unservedObjects: ['policy_revision'], guardedObjects: ['account'] }); (app.packs[0] as { related: Array<{ fields: string[] }> }).related[0].fields = ['version', 'reviewer']; @@ -358,10 +403,13 @@ describe('get_record_context — object level: a gate that refuses everyone refu await refusalOf(executeGetRecordContext(CALL, door)); expect(door.asked).not.toContainEqual({ member: 'publishedObjectDefinitionOf', object: 'policy_revision' }); } - // [as amended] A child left out for this caller is not read either. + // [as amended] A child left out for this caller is asked its published definition once, after + // its gates and its served read, and nothing of it is read after that. const leftOut = fakeDoor(policyApp({ unservedObjects: ['policy_revision'] })); await executeGetRecordContext(CALL, leftOut); - expect(leftOut.asked).not.toContainEqual({ member: 'publishedObjectDefinitionOf', object: 'policy_revision' }); + expect(leftOut.asked.filter((a) => a.object === 'policy_revision').map((a) => a.member)) + .toEqual(['readGuard', 'assertApiExposed', 'servedObjectDefinitionOf', 'publishedObjectDefinitionOf']); + expect(leftOut.reads.map((r) => r.object)).not.toContain('policy_revision'); const door = fakeDoor(policyApp()); await executeGetRecordContext(CALL, door); const at = (member: string) => door.asked.findIndex((a) => a.member === member && a.object === 'policy_revision'); diff --git a/packages/services/service-record-context/src/record-context-tools.ts b/packages/services/service-record-context/src/record-context-tools.ts index dc26a891b58..344191189ba 100644 --- a/packages/services/service-record-context/src/record-context-tools.ts +++ b/packages/services/service-record-context/src/record-context-tools.ts @@ -69,7 +69,8 @@ import { sentencesFor, type Sentences } from './sentences.js'; * the caller's served schema shows it (the naming bound). A related child * that passes both and that THIS caller is not served is left out: nothing of * it is read again, its section is not named, and the notes carry the one - * not-visible statement. A field the caller's served schema hides is not + * not-visible statement, when the stack publishes the object; a child the + * published definitions lack is left out stating nothing (D1). A field the caller's served schema hides is not * named: the entry, a related section whose link it is, or the body, is left * out, and the notes carry the same statement. A field the served * schema shows and field-level security withholds keeps its name and label, @@ -494,7 +495,9 @@ export async function executeGetRecordContext(input: unknown, door: RecordContex // to anyone either, so judging exposure after the served definition // would read it as this caller's narrowing). A child that passes both // and that THIS caller is not served is left out: never named, never - // read again, with the one not-visible statement. + // read again, with the one not-visible statement when its published + // definition exists, and with none when it does not (an author defect, + // D1): the one fact asked of the published definition here. const childServed = new Map>(); const leftOut = new Set(); for (const entry of pack.related ?? []) { @@ -525,8 +528,20 @@ export async function executeGetRecordContext(input: unknown, door: RecordContex return refusedOrFailed(err, () => refuseObject(entry.object, false)); } if (!served) { + // [ADR-0160 D1/D3] Left out either way, and never read again. Stated + // only when the stack publishes the object: a name the published + // definitions lack is an author defect (`os validate`), which states + // nothing. The published definition is the same for every caller, so + // the statement depends on this caller's served schema alone, and the + // definition is asked for that one fact: nothing of it is carried. + let published: Record | null; + try { + published = await door.publishedObjectDefinitionOf(entry.object); + } catch (err) { + return refusedOrFailed(err, () => refuseObject(entry.object, false)); + } leftOut.add(entry.object); - hidden = true; + if (published) hidden = true; continue; } childServed.set(entry.object, served); @@ -536,8 +551,8 @@ export async function executeGetRecordContext(input: unknown, door: RecordContex // already passed; one that still throws a coded refusal refuses the call // unnamed, and one that fails fails it, naming nothing, so no door's own // rendering of either can name what the caller is not shown. A child left - // out for this caller is never read, its definition included: the plan - // reads it as having no published definition. + // out for this caller is never read again, its definition included: the + // plan reads it as having no published definition. const publishedOf = async (name: string): Promise | null> => { if (leftOut.has(name)) return null; try {