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
18 changes: 18 additions & 0 deletions .changeset/37463-record-context-outage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/service-record-context': patch
---

fix(record-context): a related object's read that fails is answered as a failure, not as "no access"

Clause-②: no
Metadata-protocol: none

`get_record_context` reads each related object of a pack before it answers: the object's API exposure, its definition as the caller is served it, its published definition, and the related list itself. When one of the first three reads failed for a reason that is not a refusal (the database or the metadata store down, a bug), the call was refused `PERMISSION_DENIED` (403), as if the caller could not read the object. Such a failure now fails the call `INTERNAL_ERROR` (500), and the thrown error carries the original on `cause`. At the related-list read, the raw error left the executor and the MCP doors classified it. That is still so for every throw the `/data` door answers with a code or sentence of its own. `INTERNAL_ERROR` (500) applies there to an uncoded failure only, one the doors answered `INTERNAL_ERROR` (500) before, which now fails the call the same way and carries the original on `cause`.

What an MCP caller sees for an uncoded failure, on either door: `{ code: 'INTERNAL_ERROR', status: 500 }` with the message `Internal server error`. The doors withhold a 5xx's own sentence, so the executor's sentence (which names the pack and the root record's object only) is read in-process only, for example by the host of `verifyRecordCitations`. Nothing names the related object, a field, a package or what the failed read said. Each door logs the failure at `error` on every call, with the thrown error and its `cause`.

A refusal at the same reads is answered as before. At the first three reads a coded refusal (a string `code` with a declared 4xx status, a disabled package's included) refuses `PERMISSION_DENIED` (403). At the related-list read, a throw the `/data` door answers itself is left to the door as before, with its own code and sentence. A refusal: a sandboxed `beforeFind` hook's plain throw still answers `400 HOOK_ABORTED`, and a `PERMISSION_DENIED` with no status still answers 403. A coded fault: an unavailable datasource still answers `ERR_DATASOURCE_UNAVAILABLE` (503) with its sentence, a driver error the door recognises `DATABASE_ERROR` (500), a driver's declared `NOT_IMPLEMENTED` (501) and a declared 5xx their own code, with the sentence the producer declared for the caller.

`verifyRecordCitations` therefore reports such a failure through `onError` (its citations are still dropped), where at the first three reads it used to drop them without a report. A host that retries or logs on `onError` now sees outages behind a pack with related lists.

The package gains one dependency, `@objectstack/types` (already a dependency of `@objectstack/core`), for the `/data` door's classifier the related-list read asks.
99 changes: 99 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 @@ -776,6 +776,105 @@ describe('[ADR-0149 D7 · ADR-0160 D4] get_record_context over one real kernel:
expect(answer.text).not.toContain('rc_off');
});

it('[ADR-0112] a related child whose list read fails (an outage, no coded refusal) fails the call INTERNAL_ERROR on both doors, naming nothing, never as a refusal', async () => {
const OUTAGE = 'disk I/O error reading rc_revision.version';
const real = engine.find.bind(engine);
const failing = vi.spyOn(engine, 'find').mockImplementation(async (object: string, ...rest: any[]) => {
if (object === 'rc_revision') throw new Error(OUTAGE);
return (real as any)(object, ...rest);
});
// The doors' fault log (`logServerFault`, `@objectstack/types`) writes to the console here.
const errorLines = vi.spyOn(console, 'error').mockImplementation(() => {});
const warnLines = vi.spyOn(console, 'warn').mockImplementation(() => {});
try {
const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
// An outage is a fault, never a declared capability absence: each door logs it at error,
// with the outage as its cause, on every call, the second included.
expect(warnLines.mock.calls.filter(([line]) => String(line).includes('capability absence'))).toEqual([]);
const faults = errorLines.mock.calls.filter(([line]) =>
typeof line === 'string' && line.startsWith('[5xx] 500') && line.includes('get_record_context rc_policy'));
expect(faults).toHaveLength(4);
for (const [, meta] of faults) {
expect(meta).toMatchObject({ status: 500, code: 'INTERNAL_ERROR' });
expect(String((meta as { stack?: unknown }).stack)).toContain(`Caused by: Error: ${OUTAGE}`);
}
// Reached through each door, so the answer is the outage's on both.
expect(failing.mock.calls.filter(([object]) => object === 'rc_revision').length).toBeGreaterThanOrEqual(4);
expect(answer.isError).toBe(true);
expect(answer.payload.error).toMatchObject({ code: 'INTERNAL_ERROR', status: 500 });
for (const word of ['rc_revision', 'disk I/O', 'version']) expect(answer.text).not.toContain(word);
} finally {
failing.mockRestore();
errorLines.mockRestore();
warnLines.mockRestore();
}
// Control: the same call reads once the list read answers.
const control = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
expect(control.isError).toBe(false);
expect(control.payload.data.related[0]).toMatchObject({ object: 'rc_revision' });
});

it.each<[string, () => unknown, Record<string, unknown>]>([
[
'a sandboxed beforeFind hook\'s plain throw stays 400 HOOK_ABORTED, in the hook\'s words',
() => Object.assign(new Error("beforeFind hook 'rc_gate' threw: Revisions are closed for review"), { innerMessage: 'Revisions are closed for review' }),
{ code: 'HOOK_ABORTED', status: 400, message: 'Revisions are closed for review' },
],
[
'a PERMISSION_DENIED with no status stays PERMISSION_DENIED, as the door renders it',
() => Object.assign(new Error('Permission denied'), { code: 'PERMISSION_DENIED' }),
{ code: 'PERMISSION_DENIED' },
],
])('[ADR-0112] at the related list read, a throw the /data door answers as a refusal stays that refusal on both doors: %s', async (_label, thrown, expected) => {
const real = engine.find.bind(engine);
const refusing = vi.spyOn(engine, 'find').mockImplementation(async (object: string, ...rest: any[]) => {
if (object === 'rc_revision') throw thrown();
return (real as any)(object, ...rest);
});
try {
const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
expect(refusing.mock.calls.filter(([object]) => object === 'rc_revision').length).toBeGreaterThanOrEqual(2);
expect(answer.isError).toBe(true);
expect(answer.payload.error).toMatchObject(expected);
// The door's refusal, as main answered it: never the fault's 500.
expect([undefined, 400, 403]).toContain(answer.payload.error.status);
} finally {
refusing.mockRestore();
}
const control = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
expect(control.isError).toBe(false);
expect(control.payload.data.related[0]).toMatchObject({ object: 'rc_revision' });
});

it('[ADR-0112] at the related list read, a fault the /data door codes stays the door\'s answer on both doors: ERR_DATASOURCE_UNAVAILABLE 503 with its sentence, never INTERNAL_ERROR', async () => {
const SENTENCE = 'The datasource for this object is not available';
const real = engine.find.bind(engine);
const unavailable = vi.spyOn(engine, 'find').mockImplementation(async (object: string, ...rest: any[]) => {
if (object === 'rc_revision') throw Object.assign(new Error(SENTENCE), { code: 'ERR_DATASOURCE_UNAVAILABLE' });
return (real as any)(object, ...rest);
});
const errorLines = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
expect(unavailable.mock.calls.filter(([object]) => object === 'rc_revision').length).toBeGreaterThanOrEqual(2);
expect(answer.isError).toBe(true);
expect(answer.payload.error).toMatchObject({ code: 'ERR_DATASOURCE_UNAVAILABLE', status: 503, message: SENTENCE });
// Each door still logs the fault it answered, under the door's own code.
const faults = errorLines.mock.calls.filter(([line]) =>
typeof line === 'string' && line.startsWith('[5xx]') && line.includes('get_record_context rc_policy'));
expect(faults).toHaveLength(2);
for (const [line] of faults) expect(String(line)).toMatch(/^\[5xx\] 503\b/);
expect(JSON.stringify(errorLines.mock.calls.map(([, meta]) => meta))).not.toContain('INTERNAL_ERROR');
} finally {
unavailable.mockRestore();
errorLines.mockRestore();
}
const control = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_main' });
expect(control.isError).toBe(false);
expect(control.payload.data.related[0]).toMatchObject({ object: 'rc_revision' });
});

it('[ADR-0160 D3, second amendment] where, orderBy and limit on a related list answer the same bytes on both doors: filtered, ordered, cut at the limit and stated', async () => {
const answer = await both({ objectName: 'rc_policy', recordId: 'p1', context: 'rc_filtered' });
expect(answer.isError).toBe(false);
Expand Down
3 changes: 2 additions & 1 deletion packages/services/service-record-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"dependencies": {
"@objectstack/core": "workspace:*",
"@objectstack/spec": "workspace:*"
"@objectstack/spec": "workspace:*",
"@objectstack/types": "workspace:*"
},
"devDependencies": {
"@types/node": "^26.6.5",
Expand Down
Loading
Loading