Skip to content

Commit cdd7ca4

Browse files
committed
fix(objectql, runtime): a converted stale-roll-up refusal keeps its lead and cause through a sandboxed hook, and is hedged under a failed rollback
Claude-Session: https://claude.ai/code/session_01JQwxbZhgmEnK3HMysTCHGU Co-authored-by: Claude <noreply@anthropic.com>
1 parent 744211f commit cdd7ca4

6 files changed

Lines changed: 327 additions & 9 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/runtime': patch
4+
---
5+
6+
fix(objectql, runtime): a converted stale-roll-up refusal that a sandboxed hook body lets leave keeps its lead and its `cause`, and one that escapes a transaction whose rollback threw no longer claims a rollback
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
- A sandboxed hook body whose `ctx.api` write was refused because a nested write's roll-up could not be recomputed (a delete whose cascade stopped, a transaction that rolled back) used to hand that refusal out of the QuickJS runner as a `SandboxError`. The hook-dispatch seam did not recognise it, so the outer write's caller read the hook's own write ("The delete of …") and the original `ERR_SUMMARY_RECOMPUTE` refusal was gone from `cause`. The runner now hands the engine's own refusal back out when the body let it leave unchanged, as it already did for the duplicate-record refusal, so the seam leads with the write the hook is bound to (every hook event, the read events included) and `cause` keeps the original refusal. A body that catches it and throws its own error, or rewrites it first, is relayed as before.
12+
- `ObjectQL.transaction` whose rollback threw now hedges an already-converted refusal too: it leads with "The transaction did not complete, and its rollback failed too", instead of keeping the cascade's or hook's "under a transaction that rolls back nothing was written".
13+
- Status and code are unchanged at every door: `500 INTERNAL_ERROR`.
14+
- `@objectstack/objectql` exports `isUnlandedWriteRefusal(e)`, the predicate the runner asks of a host call's rejection.

‎packages/objectql/src/engine-unlanded-refusal-lead.test.ts‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
* so nothing written inside it was stored". Now, when the rollback threw,
1818
* the lead says that nothing is known about what is stored.
1919
*
20+
* [#29527] …and that holds for a refusal an earlier conversion already
21+
* made: a cascade-stopped or hook-stopped refusal escaping the transaction
22+
* said "under a transaction that rolls back nothing was written". It is
23+
* re-led too, its `cause` kept.
24+
*
2025
* Controls: a delete of the sub itself still answers `ERR_SUMMARY_RECOMPUTE`
2126
* with its own result; a transaction whose rollback succeeds still says it
22-
* rolled back.
27+
* rolled back, and leaves a converted refusal exactly as it escaped.
2328
*
2429
* The driver rolls back for real (a snapshot per transaction), refuses every
2530
* write to `bucket` while `refuseBucket` is set (the roll-up recompute's parent
@@ -29,7 +34,14 @@
2934

3035
import { describe, it, expect } from 'vitest';
3136
import { ObjectQL } from './engine.js';
32-
import { SummaryRecomputeError, cascadeStoppedRefusal, hookStoppedRefusal } from './summary-errors.js';
37+
import {
38+
SummaryRecomputeError,
39+
cascadeStoppedRefusal,
40+
hookStoppedRefusal,
41+
isUnlandedWriteRefusal,
42+
rollbackFailedRefusal,
43+
rolledBackRefusal,
44+
} from './summary-errors.js';
3345

3446
type Row = Record<string, unknown>;
3547

@@ -222,4 +234,58 @@ describe('ObjectQL.transaction never claims "rolled back" when its rollback thre
222234
expect(driver.rolledBack).toHaveLength(1);
223235
expect(ids(driver.rowsOf('sub'))).toEqual(['s1']);
224236
});
237+
238+
it('[#29527] a cascade-stopped refusal escaping it, the rollback throwing: re-led, the clause about a rollback gone', async () => {
239+
const { engine, driver } = await makeEngine();
240+
driver.state.failRollback = true;
241+
const err = await refusal(engine.transaction(async (trx: any) => engine.delete('line', { where: { id: 'l1' }, context: trx })));
242+
expect(err).toBeInstanceOf(Error);
243+
expect(err.code).toBe('INTERNAL_ERROR');
244+
expect(err.status).toBe(500);
245+
expect(err.message.startsWith(
246+
'The transaction did not complete, and its rollback failed too, so what was written inside it may or may not be stored.',
247+
), err.message).toBe(true);
248+
expect(err.message).not.toContain('nothing was written');
249+
expect(err.message).toContain('bucket.sub_count');
250+
expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE');
251+
expect(err.cause?.failures?.[0]?.childObject).toBe('sub');
252+
expect(driver.rolledBack).toHaveLength(0);
253+
});
254+
255+
it('[#29527] CONTROL the rollback succeeds: the cascade-stopped refusal escapes exactly as it was made', async () => {
256+
const { engine, driver } = await makeEngine();
257+
const err = await refusal(engine.transaction(async (trx: any) => engine.delete('line', { where: { id: 'l1' }, context: trx })));
258+
expect(err.message.startsWith('The delete of "line" did not complete: its cascade stopped'), err.message).toBe(true);
259+
expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE');
260+
expect(driver.rolledBack).toHaveLength(1);
261+
});
262+
263+
it('[#29527] every converted refusal is hedged when the rollback threw, and kept when it ran', () => {
264+
const original = new SummaryRecomputeError([{ childObject: 'sub', parentObject: 'bucket', parentId: 'b1', field: 'sub_count', error: null }], true);
265+
for (const converted of [cascadeStoppedRefusal(original, 'line'), hookStoppedRefusal(original, 'afterInsert', 'case', 'cleanup')] as any[]) {
266+
const failed = rollbackFailedRefusal(converted) as any;
267+
expect(failed.message.startsWith('The transaction did not complete, and its rollback failed too'), failed.message).toBe(true);
268+
expect(failed.message).not.toContain('nothing was written');
269+
expect(failed.cause).toBe(original);
270+
expect(failed.code).toBe('INTERNAL_ERROR');
271+
expect(failed.status).toBe(500);
272+
expect(rolledBackRefusal(converted)).toBe(converted);
273+
}
274+
const other = new Error('something else');
275+
expect(rollbackFailedRefusal(other)).toBe(other);
276+
});
277+
});
278+
279+
describe('[#29527] isUnlandedWriteRefusal answers the conversion and nothing else', () => {
280+
it('a conversion, at every depth, is one; the original refusal, a plain error and a look-alike are not', () => {
281+
const original = new SummaryRecomputeError([{ childObject: 'sub', parentObject: 'bucket', parentId: 'b1', field: 'sub_count', error: null }], true);
282+
const inner = cascadeStoppedRefusal(original, 'line');
283+
for (const e of [inner, cascadeStoppedRefusal(inner, 'acct'), hookStoppedRefusal(inner, 'beforeFind', 'case', 'h'), rollbackFailedRefusal(inner)]) {
284+
expect(isUnlandedWriteRefusal(e)).toBe(true);
285+
}
286+
const lookAlike = Object.assign(new Error((inner as Error).message), { status: 500, code: 'INTERNAL_ERROR', cause: original });
287+
for (const e of [original, new Error('x'), lookAlike, { ...(inner as object) }, undefined]) {
288+
expect(isUnlandedWriteRefusal(e)).toBe(false);
289+
}
290+
});
225291
});

‎packages/objectql/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ export { RELATION_FILTER_ID_CAP } from './relation-filter-lowering.js';
194194
// instead. The class stays exported exactly as it was — this adds an
195195
// affordance, it removes nothing.
196196
export { SummaryRecomputeError, SUMMARY_RECOMPUTE_CODE } from './summary-errors.js';
197+
// [#29527] The QuickJS runner asks it of a host call's rejection, so a sandboxed
198+
// body that lets a converted refusal leave hands back the engine's own error.
199+
export { isUnlandedWriteRefusal } from './summary-errors.js';
197200
export type { SummaryRecomputeFailure } from './summary-errors.js';
198201
// [#5126] Thrown by `update` when `options.strictReadonlyWrites` is set and the
199202
// payload would have had read-only fields stripped. Exported so an in-process

‎packages/objectql/src/summary-errors.ts‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ export class SummaryRecomputeError extends Error {
108108
* converts naming the child, then the parent's walk receives that) or a hook
109109
* whose own write was such a delete. The caller of the outer write is told
110110
* about the outer write, and `cause` stays the original refusal, so the
111-
* summaries named are the same at every level. A transaction does not re-lead:
112-
* a conversion that escapes it already names the write that did not complete.
111+
* summaries named are the same at every level. A transaction whose rollback
112+
* ran does not re-lead: a conversion that escapes it already names the write
113+
* that did not complete, and its "under a transaction that rolls back" clause
114+
* holds. [#29527] One whose rollback threw does ({@link rollbackFailedRefusal}).
113115
*/
114116
function unlandedWriteRefusal(e: unknown, lead: string, relead = false): unknown {
115-
const original = relead && e instanceof UnlandedWriteRefusal ? e.cause : e;
117+
const original = relead && isUnlandedWriteRefusal(e) ? (e as UnlandedWriteRefusal).cause : e;
116118
if (!(original instanceof Error) || (original as { code?: unknown }).code !== SUMMARY_RECOMPUTE_CODE) return e;
117119
const failures = (original as { failures?: unknown }).failures;
118120
const named = Array.isArray(failures)
@@ -142,8 +144,8 @@ function unlandedWriteRefusal(e: unknown, lead: string, relead = false): unknown
142144
* declared 5xx with no code drops `code` from the data door's body.
143145
*
144146
* A class so a later conversion can tell an earlier one apart (the re-lead
145-
* above). Module-private and never exported: no reader outside this file
146-
* matches it, so the published surface does not move. `name` stays `'Error'`.
147+
* above). The class itself stays module-private, and `name` stays `'Error'`;
148+
* a reader outside this file asks {@link isUnlandedWriteRefusal}.
147149
*/
148150
class UnlandedWriteRefusal extends Error {
149151
readonly status = 500;
@@ -152,9 +154,33 @@ class UnlandedWriteRefusal extends Error {
152154
constructor(message: string, original: Error) {
153155
super(message);
154156
(this as { cause?: unknown }).cause = original;
157+
Object.defineProperty(this, UNLANDED_WRITE_REFUSAL_BRAND, { value: true });
155158
}
156159
}
157160

161+
/**
162+
* [#29527] The brand {@link isUnlandedWriteRefusal} reads. A registered symbol,
163+
* not `instanceof`: this package ships two realms (ESM and CJS) and a reader
164+
* holding the other realm's copy of the class would get `false`, silently.
165+
* Not enumerable, so no serialiser and no `{ ...err }` spread carries it.
166+
*/
167+
const UNLANDED_WRITE_REFUSAL_BRAND = Symbol.for('@objectstack/objectql.unlanded-write-refusal');
168+
169+
/**
170+
* [#29527] Is `e` the refusal {@link unlandedWriteRefusal} built: a write that
171+
* did not land because a nested write's stale-roll-up refusal stopped it?
172+
*
173+
* Exported for the one host-side reader that has to tell it apart: the QuickJS
174+
* runner, which hands this refusal back out of a sandboxed body unchanged when
175+
* the body let it leave (as it does the engine's duplicate-record refusal), so
176+
* the hook-dispatch seam that converts next still re-leads it and keeps its
177+
* `cause`. Matched on the HOST-side error a host call rejected with, never on
178+
* anything a body wrote.
179+
*/
180+
export function isUnlandedWriteRefusal(e: unknown): boolean {
181+
return e instanceof Error && (e as unknown as Record<symbol, unknown>)[UNLANDED_WRITE_REFUSAL_BRAND] === true;
182+
}
183+
158184
/** {@link unlandedWriteRefusal} for a delete whose cascade walk the refusal stopped. */
159185
export function cascadeStoppedRefusal(e: unknown, object: string): unknown {
160186
return unlandedWriteRefusal(
@@ -212,5 +238,9 @@ export function rollbackFailedRefusal(e: unknown): unknown {
212238
e,
213239
'The transaction did not complete, and its rollback failed too, so what was written inside it may or may ' +
214240
'not be stored.',
241+
// [#29527] Re-lead an earlier conversion too. A cascade- or hook-stopped
242+
// refusal that escapes this transaction says "under a transaction that
243+
// rolls back nothing was written", which the failed rollback made unknown.
244+
true,
215245
);
216246
}

0 commit comments

Comments
 (0)