Skip to content

Commit 33388f9

Browse files
claude[bot]claude
andauthored
fix(objectql): engine refusals stamp httpStatus beside status so a locally caught refusal carries its HTTP status (#16154)
* fix(objectql): engine refusals stamp `httpStatus` beside `status` Every producer in `packages/objectql` that stamps a numeric HTTP status on a thrown error now stamps it under both spellings. `status` is unchanged and kept: it is what every HTTP door in this repo reads. `httpStatus` is the ADR-0112 D5 spelling, and it is what a consumer holding the THROWN error reads — the CLI's `--json` error envelope was emitting `code` with no status at all for a locally thrown engine refusal. 20 producer sites: 12 inline `err.status = N` stamps (engine.ts, filter-comparand-shape.ts, summary-backfill.ts) and 8 error classes declaring `readonly status`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ARYe3yQTQCUFm5qPYNgKaJ * test(objectql,cli): pin both status spellings at the producer and in the CLI envelope - `summary-backfill.test.ts`: the real-producer refusal test now asserts `httpStatus` beside `status` — this is the card's own producer, exercised through `backfillSummaryNulls` rather than mocked. - `error-http-status-spelling.test.ts` (new): constructs every engine error class that declares a status and asserts the two spellings agree. Nothing else enforces the pairing — the two keys are plain data on a thrown value, and `check:error-status-conformance`'s deriver reads `status`/`statusCode` only. Carries a bare-`Error` control so an all-undefined read cannot pass as agreement. - `summary-nulls.test.ts`: the pin the ruling names widens from `code` only to `code` + `httpStatus: 400`, and its fixture now mirrors what the producer really stamps. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ARYe3yQTQCUFm5qPYNgKaJ * docs(permissions): re-anchor the system-context census to engine.ts's new line numbers Mechanical, by `node scripts/check-system-context-census.mjs --fix`. Stamping `httpStatus` inserted 8 lines into `engine.ts`, so every `isSystem` anchor below line 1001 shifted by the cumulative insertion count at its position (+9, +12, +13, +14 — each verified against the insert points). No prose changed; only the cited line numbers move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ARYe3yQTQCUFm5qPYNgKaJ * docs(permissions): regenerate the system-context census from the merged tree The merge brought in #16087, which inserted its own lines into `engine.ts` and re-anchored this page for them. Both sides had edited this `merge=os-regen` artifact, so the driver merged it with exit 0 while silently keeping one side; `scripts/pm/os-regen-merge.sh` took main's bytes and this commit re-derives the page from the merged tree with `pnpm gen:system-context-census`. Blast radius measured, not assumed: 105 rows before and 105 after, row SET identical once integers are normalised, 12 changed lines and all 12 identical apart from line numbers — no row dropped, none added, no prose moved. The deltas (+9/+12/+13/+14) are this branch's own cumulative insertion offsets. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ARYe3yQTQCUFm5qPYNgKaJ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9f39897 commit 33388f9

14 files changed

Lines changed: 166 additions & 23 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@objectstack/objectql': minor
3+
---
4+
5+
Engine refusals now declare their HTTP status under both spellings: `httpStatus` beside the existing `status`, same number, at every producer in the package.
6+
7+
`status` is unchanged and stays. It is what every HTTP door in this repo reads — `resolveThrownHttpError` (`@objectstack/types`) resolves `.status` then `.statusCode` and knows no other spelling — so nothing about what the REST or dispatcher doors answer changes.
8+
9+
What changes is what a consumer holding the **thrown** error can read. ADR-0112 D5 records the destination as "the HTTP status lives on the transport and (optionally) `error.httpStatus`", and `httpStatus` is the key the client SDK already stamps on every wire failure. A consumer that caught an engine refusal locally had no status at all: `os migrate summary-nulls --json --recompute-undefined-on-empty customer.nope` emitted `{ error, code: 'INVALID_FIELD' }` with no status field, while the same refusal arriving over the wire carried `httpStatus: 400`. It now carries `httpStatus: 400` on both paths.
10+
11+
Additive on thrown errors, so no caller that reads `status` needs to change. The 20 producers: the `INVALID_SORT` / `INVALID_FIELD` / `VALIDATION_ERROR` / `INVALID_METADATA` / `DELETE_RESTRICTED` refusals in `engine.ts`, the `INVALID_FILTER` / `INVALID_FIELD` refusals in `filter-comparand-shape.ts`, `resolveRecomputeScope` in `summary-backfill.ts`, and the eight error classes declaring a `readonly status` (`DuplicateRecordError`, `HookUnscopedDataAccessError`, `MultiUpdateHookKeyDivergenceError`, `EmptyCredentialWriteError`, `SystemWriteOrganizationRequiredError`, `NamespaceConflictError`, `ArtifactObjectNameConflictError`, `ObjectOwnershipConflictError`).

content/docs/permissions/system-context.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ that silently does not happen.
109109

110110
| # | Behaviour when `isSystem` | Package | What you get / what you lose | Anchor |
111111
|:--|:---|:---|:---|:---|
112-
| 18 | **`readonly` strip bypassed — UPDATE, single row** | objectql | Get: a `readonly` field CAN be written. Lose: the protection that stops a caller seeding e.g. `approval_status` | `objectql/src/engine.ts:11733` |
113-
| 19 | **`readonly` strip bypassed — UPDATE, bulk/predicate** | objectql | Same, on the multi-row path | `objectql/src/engine.ts:11916` |
114-
| 20 | **`readonly` strip bypassed — INSERT** | objectql | Same, on create — one gate over BOTH create-side passes since the 2026-09-03 ruling moved the static-`readonly` strip in beside the runtime-owned one and deleted the DataProtocol ingress copy. `isSystem` is the **only** exemption on this path: `preserveAudit` is deliberately not read on create, so a non-system historical import is still stripped | `objectql/src/engine.ts:10381` |
115-
| 21 | Strict-drop refusal never fires | objectql | Lose: a caller that opted into loud refusal gets **silence** — strict refuses exactly what the strip would have taken, and the strip took nothing | `objectql/src/engine.ts:10514`, `readonly-strict-errors.ts:66` |
116-
| 22 | **Referential-integrity check skipped** | objectql | Get: writes proceed against unreachable/unresolvable targets. Lose: an `isSystem` caller can write a **dangling reference** | `objectql/src/engine.ts:6240` |
117-
| 23 | Tenant-audit warning silenced; `bypassTenantAudit` threaded to the driver | objectql | Get: unscoped system writes stop warning. Lose: the signal that would flag a genuine user-path scoping bug | `objectql/src/engine.ts:3920`, `:3930`, `:3957` |
112+
| 18 | **`readonly` strip bypassed — UPDATE, single row** | objectql | Get: a `readonly` field CAN be written. Lose: the protection that stops a caller seeding e.g. `approval_status` | `objectql/src/engine.ts:11746` |
113+
| 19 | **`readonly` strip bypassed — UPDATE, bulk/predicate** | objectql | Same, on the multi-row path | `objectql/src/engine.ts:11929` |
114+
| 20 | **`readonly` strip bypassed — INSERT** | objectql | Same, on create — one gate over BOTH create-side passes since the 2026-09-03 ruling moved the static-`readonly` strip in beside the runtime-owned one and deleted the DataProtocol ingress copy. `isSystem` is the **only** exemption on this path: `preserveAudit` is deliberately not read on create, so a non-system historical import is still stripped | `objectql/src/engine.ts:10394` |
115+
| 21 | Strict-drop refusal never fires | objectql | Lose: a caller that opted into loud refusal gets **silence** — strict refuses exactly what the strip would have taken, and the strip took nothing | `objectql/src/engine.ts:10527`, `readonly-strict-errors.ts:66` |
116+
| 22 | **Referential-integrity check skipped** | objectql | Get: writes proceed against unreachable/unresolvable targets. Lose: an `isSystem` caller can write a **dangling reference** | `objectql/src/engine.ts:6252` |
117+
| 23 | Tenant-audit warning silenced; `bypassTenantAudit` threaded to the driver | objectql | Get: unscoped system writes stop warning. Lose: the signal that would flag a genuine user-path scoping bug | `objectql/src/engine.ts:3929`, `:3939`, `:3966` |
118118
| 24 | Engine-owned / append-only write guard bypassed | plugin-security | Get: generic writes to `managedBy` engine-owned objects | `system-write-guard.ts:96`, `:120` |
119119
| 25 | Identity write guard bypassed (ADR-0092) | plugin-auth | Get: direct writes to identity tables through the generic data path | `identity-write-guard.ts:99` |
120-
| 26 | Search-companion column **kept** in a read's rows when it was explicitly requested | objectql | Get: the internal companion column is readable. Lose: nothing for app code — this is the engine reading its own index | `objectql/src/engine.ts:6939` |
121-
| 27 | Dependent-count disclosure on a blocked delete | objectql | Get: the count of blocking children. Nothing was elevated past the caller, so nothing is withheld | `objectql/src/engine.ts:12535` |
122-
| 28 | Reference-cleanup log attributes the write to `'system'` | objectql | Get: an honest actor label instead of `anonymous` when the context carries neither `userId` nor `actor` | `objectql/src/engine.ts:12464` |
120+
| 26 | Search-companion column **kept** in a read's rows when it was explicitly requested | objectql | Get: the internal companion column is readable. Lose: nothing for app code — this is the engine reading its own index | `objectql/src/engine.ts:6951` |
121+
| 27 | Dependent-count disclosure on a blocked delete | objectql | Get: the count of blocking children. Nothing was elevated past the caller, so nothing is withheld | `objectql/src/engine.ts:12548` |
122+
| 28 | Reference-cleanup log attributes the write to `'system'` | objectql | Get: an honest actor label instead of `anonymous` when the context carries neither `userId` nor `actor` | `objectql/src/engine.ts:12477` |
123123
| 29 | **Bulk data event `organizationId` OMITTED** — the batch is published "not asserted" | plugin-security | Get: nothing — the `data.records.*` event still publishes. Lose: the per-organization attribution: this exit is taken before the security middleware composes any tenant wall, so it records no Layer 0 verdict on the operation (`OperationContext.tenantLayer0Verdict`, #15813), and the engine's bulk producer — which reads that recorded verdict and nothing else — omits the key rather than filling it from the caller's `tenantId`; a tenant-scoped consumer then does not deliver the event inside an organization wall (#15225) | `security-plugin.ts:1686` |
124124

125125
### 3. Sharing (`plugin-sharing`)
@@ -179,8 +179,8 @@ a reader tracing where elevation travels needs them.
179179

180180
| # | Site | Package | What it does |
181181
|:--|:---|:---|:---|
182-
| 62 | `objectql/src/engine.ts:3727` | objectql | Propagates `isSystem` into the hook session so hooks can tell engine self-writes from user writes |
183-
| 63 | `objectql/src/engine.ts:14981` | objectql | `ScopedContext.isSystem` getter — re-exposes the underlying execution context's flag |
182+
| 62 | `objectql/src/engine.ts:3736` | objectql | Propagates `isSystem` into the hook session so hooks can tell engine self-writes from user writes |
183+
| 63 | `objectql/src/engine.ts:14995` | objectql | `ScopedContext.isSystem` getter — re-exposes the underlying execution context's flag |
184184
| 64 | `plugin-reports/src/report-service.ts:556` | plugin-reports | Threads the flag into the engine call that runs a report |
185185
| 65 | `body-runner.ts:279` | runtime | Rebuilds an `ExecutionContext` from a hook session, carrying the flag across |
186186

@@ -195,7 +195,7 @@ assuming `isSystem` covers it is a documented source of bugs.
195195
|:---|:---|:---|
196196
| "It suppresses triggers / record-change automation" | **No.** Only `skipTriggers` does. A bare `{ isSystem: true }` on a seed write re-fired automation on freshly seeded rows and wedged first boot | `metadata-protocol/src/seed-loader.ts:2032` (rationale at `:1942``1944`, #3760), `flow.zod.ts:743` |
197197
| "It skips the state machine" | **No.** That is `skipStateMachine`, carried by seed replay and by `treatAsHistorical` imports | `objectql/src/engine.ts` FSM gate; see [State Machine](/docs/protocol/objectql/state-machine) |
198-
| "It skips validation rules" | **No.** Field shape, `format`, `script` and the rest still run. The `readonly` strip runs *before* validation precisely so a discarded value is not judged | `objectql/src/engine.ts:10364``10381` |
198+
| "It skips validation rules" | **No.** Field shape, `format`, `script` and the rest still run. The `readonly` strip runs *before* validation precisely so a discarded value is not judged | `objectql/src/engine.ts:10377``10394` |
199199
| "It preserves a supplied `updated_at` / `updated_by`" | **No.** That is `preserveAudit`, a separate opt-in — and an UPDATE-path exemption only | `field.zod.ts:1590` (#3493 / #6640) |
200200
| "It stamps `created_by`" | **No.** Audit stamping reads `userId` from the context. A user-less system write stamps nothing — that is today's behaviour, not an error | `runtime-identity.ts:280``281` |
201201
| "It bypasses every guard" | **No.** The last-admin guard applies to **every** context, `isSystem` included — the deprovision path that actually locks an org out is the system one | `last-admin-guard.ts:299` |

packages/cli/src/commands/migrate/summary-nulls.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,15 @@ describe('os migrate summary-nulls', () => {
129129
expect(options).toEqual({ apply: false, objects: undefined, recomputeUndefinedOnEmpty: undefined, maxRecordsPerObject: undefined });
130130
}, RUN_TIMEOUT);
131131

132-
it('a refused scope entry (INVALID_FIELD) reaches the --json error envelope with its code, and the command exits 1', async () => {
132+
it('a refused scope entry (INVALID_FIELD) reaches the --json error envelope with its code AND its httpStatus, and the command exits 1', async () => {
133+
// The fixture mirrors what `resolveRecomputeScope` really stamps
134+
// (`packages/objectql/src/summary-backfill.ts`): BOTH status spellings,
135+
// same number. `status` is what the HTTP doors read; `httpStatus` is what a
136+
// consumer holding the thrown error reads, and it is the one
137+
// `errorCodeFields` forwards. Asserting `code` alone was the fossil of the
138+
// gap this pin now covers — the envelope carried no status at all.
133139
const refusal = Object.assign(new Error('[summary-backfill] recomputeUndefinedOnEmpty names 1 roll-up(s) this run cannot find: customer.nope.'), {
134-
code: 'INVALID_FIELD', status: 400, field: 'customer.nope', fields: ['customer.nope'],
140+
code: 'INVALID_FIELD', status: 400, httpStatus: 400, field: 'customer.nope', fields: ['customer.nope'],
135141
});
136142
vi.mocked(backfillSummaryNulls).mockRejectedValue(refusal);
137143

@@ -144,7 +150,7 @@ describe('os migrate summary-nulls', () => {
144150
expect((err as { oclif?: { exit?: number } }).oclif?.exit).toBe(1);
145151
const emitted = stdout.mock.calls.map((c: unknown[]) => String(c[0])).join('');
146152
const payload = JSON.parse(emitted);
147-
expect(payload).toMatchObject({ code: 'INVALID_FIELD' });
153+
expect(payload).toMatchObject({ code: 'INVALID_FIELD', httpStatus: 400 });
148154
expect(payload.error).toContain('customer.nope');
149155
}, RUN_TIMEOUT);
150156
});

packages/objectql/src/duplicate-record-error.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const DUPLICATE_RECORD_STATUS = 409 as const;
7979
export class DuplicateRecordError extends Error {
8080
readonly code = DUPLICATE_RECORD_CODE;
8181
readonly status = DUPLICATE_RECORD_STATUS;
82+
/** The same number under ADR-0112 D5's spelling — what a consumer holding the THROWN error reads (the CLI `--json` envelope). `status` stays for the HTTP doors, which read it. */
83+
readonly httpStatus = DUPLICATE_RECORD_STATUS;
8284
/**
8385
* The driver's own error, whole.
8486
*

packages/objectql/src/engine.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,13 @@ function assertOrderByIsMaterializable(
10001000
// surfaces engine errors over HTTP therefore answers the same envelope on
10011001
// both doors instead of turning the direct path into an unhandled 500.
10021002
err.status = 400;
1003+
// …and `httpStatus`, the SAME number under ADR-0112 D5's spelling. `status` is
1004+
// what every HTTP door in this repo reads (`resolveThrownHttpError`), so it
1005+
// stays; `httpStatus` is what a consumer holding the THROWN error reads —
1006+
// the CLI's `--json` error envelope (`errorCodeFields`) is the measured one,
1007+
// and it saw `code` with no status at all until both spellings were stamped.
1008+
// Two keys, one value, written together at every producer in this package.
1009+
err.httpStatus = 400;
10031010
err.code = 'INVALID_SORT';
10041011
err.field = first;
10051012
err.fields = unmaterialized;
@@ -1101,6 +1108,7 @@ function assertProjectionHasNoDottedPaths(
11011108
// code however the caller reached it, so a host surfacing engine errors over
11021109
// HTTP answers the same envelope on both doors.
11031110
err.status = 400;
1111+
err.httpStatus = 400;
11041112
err.code = 'INVALID_FIELD';
11051113
err.field = first;
11061114
err.fields = dotted;
@@ -1245,6 +1253,7 @@ function undeclaredWriteFieldErrors(
12451253
if (undeclared.length === 0) continue;
12461254
const err: any = new Error(`Unknown field '${undeclared[0]}' on object '${object}'`);
12471255
err.status = 400;
1256+
err.httpStatus = 400;
12481257
err.code = 'INVALID_FIELD';
12491258
err.field = undeclared[0];
12501259
err.fields = undeclared;
@@ -5479,7 +5488,7 @@ export class ObjectQL implements IObjectQLEngine {
54795488
&& item.name
54805489
&& item.name !== itemName
54815490
) {
5482-
const err: Error & { code?: string; status?: number } = new Error(
5491+
const err: Error & { code?: string; status?: number; httpStatus?: number } = new Error(
54835492
`Invalid \`views:\` container from ${sourceLabel} '${ownerId}': the container's own `
54845493
+ `\`name\` is '${item.name}', which disagrees with the object key it binds to, `
54855494
+ `'${itemName}' (derived from its own \`object\`, else \`list.data.object\` / `
@@ -5491,6 +5500,7 @@ export class ObjectQL implements IObjectQLEngine {
54915500
);
54925501
err.code = 'VALIDATION_ERROR';
54935502
err.status = 400;
5503+
err.httpStatus = 400;
54945504
throw err;
54955505
}
54965506
const toRegister = item.name === itemName ? item : { ...item, name: itemName };
@@ -5508,7 +5518,7 @@ export class ObjectQL implements IObjectQLEngine {
55085518
// internals are well-formed stays the authoring/publish doors'
55095519
// job (defineStack, `os validate`, the metadata door).
55105520
if (key === 'views' && !isViewContainerShaped(toRegister)) {
5511-
const err: Error & { code?: string; status?: number } = new Error(
5521+
const err: Error & { code?: string; status?: number; httpStatus?: number } = new Error(
55125522
`Invalid \`views:\` entry '${itemName}' from ${sourceLabel} '${ownerId}': the stack `
55135523
+ '`views:` collection carries view CONTAINERS only. `viewKind`/`config`/inline view '
55145524
+ 'config belong to a single VIEW, not to the container — wrap it: '
@@ -5520,6 +5530,7 @@ export class ObjectQL implements IObjectQLEngine {
55205530
);
55215531
err.code = 'INVALID_METADATA';
55225532
err.status = 422;
5533+
err.httpStatus = 422;
55235534
throw err;
55245535
}
55255536
this._registry.registerItem(pluralToSingular(key), toRegister, 'name' as any, ownerId);
@@ -5558,7 +5569,7 @@ export class ObjectQL implements IObjectQLEngine {
55585569
const parsed = AssembledViewArtifactSchema.safeParse(item);
55595570
if (!parsed.success) {
55605571
const itemName = resolveMetadataItemName('views', item) ?? '(unnamed)';
5561-
const err: Error & { code?: string; status?: number } = new Error(
5572+
const err: Error & { code?: string; status?: number; httpStatus?: number } = new Error(
55625573
`Invalid \`${ASSEMBLED_VIEW_ITEMS_KEY}:\` entry '${itemName}' from ${sourceLabel} '${ownerId}': `
55635574
+ 'the assembled-manifest channel carries non-container view artifacts only — a ViewItem '
55645575
+ 'record (`viewKind` + `config`) or a flattened list/form overlay '
@@ -5567,6 +5578,7 @@ export class ObjectQL implements IObjectQLEngine {
55675578
);
55685579
err.code = 'INVALID_METADATA';
55695580
err.status = 422;
5581+
err.httpStatus = 422;
55705582
throw err;
55715583
}
55725584
const body = parsed.data as Record<string, unknown>;
@@ -7134,7 +7146,7 @@ export class ObjectQL implements IObjectQLEngine {
71347146
): Promise<Map<string, unknown>> {
71357147
const schema = this._registry.getObject(object);
71367148
if (!collectInternalReadFields(schema).includes(field)) {
7137-
const err: Error & { code?: string; status?: number; object?: string; field?: string } =
7149+
const err: Error & { code?: string; status?: number; httpStatus?: number; object?: string; field?: string } =
71387150
new Error(
71397151
`Cannot resolve internal field "${object}.${field}": it is not declared \`internal: true\`. `
71407152
+ 'Only fields the engine omits from the generic read path are dereferenceable here — '
@@ -7144,6 +7156,7 @@ export class ObjectQL implements IObjectQLEngine {
71447156
);
71457157
err.code = 'INVALID_FIELD';
71467158
err.status = 400;
7159+
err.httpStatus = 400;
71477160
err.object = object;
71487161
err.field = field;
71497162
throw err;
@@ -12978,6 +12991,7 @@ export class ObjectQL implements IObjectQLEngine {
1297812991
`Delete or reassign them first, or set deleteBehavior:'cascade' on ${childName}.${fieldName}.`;
1297912992
err.code = 'DELETE_RESTRICTED';
1298012993
err.status = 409;
12994+
err.httpStatus = 409;
1298112995
err.object = object;
1298212996
// Constraint 2's REQUIRED half — the referenced object is named
1298312997
// unconditionally, because "which table is blocking me" is the one

0 commit comments

Comments
 (0)