|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#11552] The per-row dispatch signal, and D2's `input.options` visibility, |
| 5 | + * OBSERVED FROM INSIDE A SHIPPED BODY — the conformance face of the maintainer |
| 6 | + * ruling that closed ADR-0058 Addendum II's declared≠observable gap for |
| 7 | + * body-only hooks. |
| 8 | + * |
| 9 | + * ## What was measured broken (and is pinned fixed here) |
| 10 | + * |
| 11 | + * D3 names three routes for row-specific work — throw, `ctx.api` per row, or |
| 12 | + * caller-side pagination — and routes 1 and 2 both require the handler to KNOW |
| 13 | + * it is on the per-row predicate path. The signal existed on the engine context |
| 14 | + * (`dispatch`, #6966; `input.options`, D2) and was dropped at the sandbox |
| 15 | + * boundary: `unwrapProxyToPlain` materialises only what `installFlatInput`'s |
| 16 | + * `ownKeys` enumerates (payload fields), and `dispatch` was never marshalled. |
| 17 | + * So the natural guard — `ctx.dispatch?.mode === 'per-row'` — lowered cleanly, |
| 18 | + * passed in-process handler tests, and evaluated `false` on EVERY production |
| 19 | + * dispatch: the inert-guard shape, shipped. |
| 20 | + * |
| 21 | + * ## Why this harness and not a unit mock |
| 22 | + * |
| 23 | + * The drop happened between two real components whose composition no unit |
| 24 | + * mock exercises: objectql's flat-input proxy (its `ownKeys`/descriptor |
| 25 | + * hiding) and the QuickJS marshalling. So this drives the REAL `ObjectQL` + |
| 26 | + * REAL `SqlDriver` (better-sqlite3) + REAL `QuickJSScriptRunner` behind |
| 27 | + * `hookBodyRunnerFactory` — the same wiring `AppPlugin` performs — and every |
| 28 | + * assertion lands on what a body OBSERVED, reported out through the `log` |
| 29 | + * capability. It mirrors the tripwire test on |
| 30 | + * `hotcrm@claude/issue-1265-batch-scoped-payload` |
| 31 | + * (`#1265 — the shipped hook body cannot tell it is on a per-row predicate |
| 32 | + * dispatch`), which asserts the four broken facts and is written to go red as |
| 33 | + * this lands; this file is the framework-side twin asserting the fixed ones. |
| 34 | + * |
| 35 | + * ## The contract pinned, member by member |
| 36 | + * |
| 37 | + * - `ctx.dispatch` = frozen `{ mode, index }` — `'per-row'` + row index on |
| 38 | + * the predicate path, `'record'` on single-record writes. NOT `scope`: |
| 39 | + * shared-identity scratch cannot survive a JSON copy into an isolated heap, |
| 40 | + * so marshalling it would ship a silently-inert write channel (see |
| 41 | + * `ScriptContext.dispatch`). |
| 42 | + * - `ctx.input.options` = frozen, NON-ENUMERABLE `{ multi?, where? }` — the |
| 43 | + * projection D2 declares `before*`-visible, not the whole caller bag (the |
| 44 | + * host-error-allowlist reasoning in `quickjs-runner.ts`: everything |
| 45 | + * marshalled becomes readable by untrusted code). |
| 46 | + * - Enumeration stays flat-only: `Object.keys(ctx.input)` lists payload |
| 47 | + * fields, exactly as the #7254 witness pins for bodies — so the payload |
| 48 | + * diff idiom cannot pick up a phantom `options` field. |
| 49 | + * - `ctx.input.id` stays ABSENT on the body face (not part of the ruling); |
| 50 | + * the row id a per-row body needs is `ctx.previous.id`, bound since #5574. |
| 51 | + * - The write-back channel still works and still cannot carry `options`: |
| 52 | + * payload writes land on the batch payload; the caller's live bag is never |
| 53 | + * overwritten by a JSON copy (non-enumerable ⇒ excluded from the post-run |
| 54 | + * `JSON.stringify` dump `applyMutationsToInput` consumes). |
| 55 | + */ |
| 56 | + |
| 57 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 58 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 59 | +import { tmpdir } from 'node:os'; |
| 60 | +import { join } from 'node:path'; |
| 61 | +import { ObjectQL, bindHooksToEngine } from '@objectstack/objectql'; |
| 62 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 63 | +import { hookBodyRunnerFactory } from './body-runner.js'; |
| 64 | +import { QuickJSScriptRunner } from './quickjs-runner.js'; |
| 65 | +import { |
| 66 | + captureExpectedReadRefusals, |
| 67 | + type ExpectedReadRefusalCapture, |
| 68 | +} from '../expected-read-refusal-noise.js'; |
| 69 | + |
| 70 | +const ARTICLE = { |
| 71 | + name: 'probe_article', |
| 72 | + fields: { |
| 73 | + title: { type: 'text' }, |
| 74 | + status: { type: 'text' }, |
| 75 | + published_at: { type: 'text' }, |
| 76 | + }, |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Reports what the body can OBSERVE, then attempts the mutations the contract |
| 81 | + * forbids, then reports what it observes AFTER the attempts — so the frozen |
| 82 | + * halves are asserted from inside the VM rather than inferred. |
| 83 | + */ |
| 84 | +const PROBE_SOURCE = ` |
| 85 | + const o = { |
| 86 | + event: ctx.event, |
| 87 | + dispatchType: typeof ctx.dispatch, |
| 88 | + dispatchMode: ctx.dispatch ? ctx.dispatch.mode : null, |
| 89 | + dispatchIndex: ctx.dispatch ? ctx.dispatch.index : null, |
| 90 | + dispatchScopeType: ctx.dispatch ? typeof ctx.dispatch.scope : null, |
| 91 | + inputKeys: Object.keys(ctx.input).sort(), |
| 92 | + inputIdType: typeof ctx.input.id, |
| 93 | + optionsType: typeof ctx.input.options, |
| 94 | + optionsMulti: ctx.input.options ? ctx.input.options.multi : null, |
| 95 | + optionsWhere: ctx.input.options ? ctx.input.options.where : null, |
| 96 | + previousId: ctx.previous ? typeof ctx.previous.id : null, |
| 97 | + }; |
| 98 | + try { ctx.dispatch.mode = 'record'; } catch (e) { /* frozen */ } |
| 99 | + try { ctx.input.options.multi = false; } catch (e) { /* frozen */ } |
| 100 | + try { ctx.input.options = { multi: false } } catch (e) { /* non-writable */ } |
| 101 | + o.postDispatchMode = ctx.dispatch ? ctx.dispatch.mode : null; |
| 102 | + o.postOptionsMulti = ctx.input.options ? ctx.input.options.multi : null; |
| 103 | + ctx.log.info('probe', o); |
| 104 | +`; |
| 105 | + |
| 106 | +const ABSENT_TENANCY_TABLE = 'sys_organization'; |
| 107 | + |
| 108 | +describe('#11552 — a shipped body observes the per-row dispatch signal and the D2 options projection', () => { |
| 109 | + let engine: ObjectQL | null = null; |
| 110 | + let dir: string | null = null; |
| 111 | + let noise: ExpectedReadRefusalCapture | null = null; |
| 112 | + |
| 113 | + afterEach(async () => { |
| 114 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 115 | + engine = null; |
| 116 | + if (dir) { rmSync(dir, { recursive: true, force: true }); dir = null; } |
| 117 | + }); |
| 118 | + |
| 119 | + it('per-row: mode/index/options visible, frozen, and invisible to enumeration; single-record: mode is record', async () => { |
| 120 | + dir = mkdtempSync(join(tmpdir(), 'os-11552-')); |
| 121 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: join(dir, 'data.sqlite') }, useNullAsDefault: true }); |
| 122 | + noise = captureExpectedReadRefusals([ABSENT_TENANCY_TABLE]); |
| 123 | + noise.captureDriver(driver); |
| 124 | + await driver.initObjects([ARTICLE]); |
| 125 | + engine = new ObjectQL(); |
| 126 | + noise.captureEngine(engine); |
| 127 | + engine.registerDriver(driver, true); |
| 128 | + await engine.init(); |
| 129 | + // `packageId` is a required parameter (`registerObject(schema, packageId, …)`) |
| 130 | + // — the sibling harness's 1-arg spelling is frozen TEST_DEBT, not a template. |
| 131 | + engine.registry.registerObject(ARTICLE as any, 'probe'); |
| 132 | + |
| 133 | + const seen: any[] = []; |
| 134 | + const logger = { |
| 135 | + debug: () => {}, |
| 136 | + info: (_msg: string, meta?: any) => { seen.push(meta); }, |
| 137 | + warn: () => {}, |
| 138 | + error: () => {}, |
| 139 | + }; |
| 140 | + engine.setDefaultBodyRunner( |
| 141 | + hookBodyRunnerFactory(new QuickJSScriptRunner(), { ql: engine, appId: 'probe', logger }), |
| 142 | + ); |
| 143 | + bindHooksToEngine(engine, [{ |
| 144 | + name: 'probe_perrow_signal', |
| 145 | + object: 'probe_article', |
| 146 | + events: ['beforeInsert', 'beforeUpdate'], |
| 147 | + body: { language: 'js', source: PROBE_SOURCE, capabilities: ['log'] }, |
| 148 | + } as any], { packageId: 'probe' }); |
| 149 | + |
| 150 | + await engine.insert('probe_article', { title: 'a', status: 'draft', published_at: 'x' }); |
| 151 | + await engine.insert('probe_article', { title: 'b', status: 'draft', published_at: 'y' }); |
| 152 | + await engine.insert('probe_article', { title: 'c', status: 'live', published_at: 'z' }); |
| 153 | + const inserts = seen.splice(0); |
| 154 | + expect(inserts.length).toBe(3); |
| 155 | + for (const o of inserts) { |
| 156 | + // An insert is the caller's whole write: the marker says so. |
| 157 | + expect(o.dispatchMode).toBe('record'); |
| 158 | + expect(o.dispatchIndex).toBe(0); |
| 159 | + } |
| 160 | + |
| 161 | + // ── The predicate path (multi: true + where) — one write, two matched rows. |
| 162 | + const callerOptions = { multi: true, where: { status: 'draft' } }; |
| 163 | + await engine.update('probe_article', { title: 'renamed' }, callerOptions as any); |
| 164 | + const perRow = seen.splice(0); |
| 165 | + expect(perRow.length).toBe(2); |
| 166 | + |
| 167 | + for (const o of perRow) { |
| 168 | + expect(o.event).toBe('beforeUpdate'); |
| 169 | + // Route 1/2's precondition — the signal, now observable (was |
| 170 | + // `dispatchType: 'undefined'` before #11552, measured on this exact |
| 171 | + // harness). |
| 172 | + expect(o.dispatchType).toBe('object'); |
| 173 | + expect(o.dispatchMode).toBe('per-row'); |
| 174 | + // `scope` deliberately does not cross — see the module doc. |
| 175 | + expect(o.dispatchScopeType).toBe('undefined'); |
| 176 | + // D2's projection, under D2's own spelling. |
| 177 | + expect(o.optionsType).toBe('object'); |
| 178 | + expect(o.optionsMulti).toBe(true); |
| 179 | + expect(o.optionsWhere).toEqual({ status: 'draft' }); |
| 180 | + // Enumeration is STILL flat-only — no phantom `options` in a payload |
| 181 | + // diff, exactly the #7254 witness contract. |
| 182 | + expect(o.inputKeys).toEqual(['title']); |
| 183 | + // `input.id` stays absent (not part of the ruling); the row id channel |
| 184 | + // on the per-row path is `previous.id`. |
| 185 | + expect(o.inputIdType).toBe('undefined'); |
| 186 | + expect(o.previousId).toBe('string'); |
| 187 | + // Frozen: the body's own mutation attempts changed nothing it can read. |
| 188 | + expect(o.postDispatchMode).toBe('per-row'); |
| 189 | + expect(o.postOptionsMulti).toBe(true); |
| 190 | + } |
| 191 | + expect(perRow.map((o) => o.dispatchIndex).sort()).toEqual([0, 1]); |
| 192 | + |
| 193 | + // The caller's live bag was not clobbered by any write-back of the graft |
| 194 | + // (non-enumerable ⇒ excluded from the mutatedInput dump), nor by the |
| 195 | + // body's frozen-write attempts. `toMatchObject`, not `toEqual`: the |
| 196 | + // engine's post-`before*` driver merge is allowed to ADD keys, never to |
| 197 | + // flip these. |
| 198 | + expect(callerOptions).toMatchObject({ multi: true, where: { status: 'draft' } }); |
| 199 | + |
| 200 | + // The payload write channel itself still works under the graft: both |
| 201 | + // matched rows took the batch payload. |
| 202 | + const renamed = (await engine.find('probe_article', { where: { title: 'renamed' } })) as any[]; |
| 203 | + expect(renamed.length).toBe(2); |
| 204 | + |
| 205 | + // ── The single-record path: same hook, by-id write. |
| 206 | + const live = ((await engine.find('probe_article', { where: { status: 'live' } })) as any[])[0]; |
| 207 | + await engine.update('probe_article', { id: live.id, title: 'single' }); |
| 208 | + const single = seen.splice(0); |
| 209 | + expect(single.length).toBe(1); |
| 210 | + expect(single[0].dispatchMode).toBe('record'); |
| 211 | + expect(single[0].dispatchIndex).toBe(0); |
| 212 | + // Whatever options bag a by-id write carries, it must not read as a |
| 213 | + // predicate write from inside a body. |
| 214 | + expect(single[0].optionsMulti).not.toBe(true); |
| 215 | + |
| 216 | + // [#10629] Withheld-noise pin, same as the sibling real-SQLite harness. |
| 217 | + expect(noise?.silentChannels() ?? ['no capture was installed']).toEqual([]); |
| 218 | + }, 30000); |
| 219 | +}); |
0 commit comments