Skip to content

Commit 751c15a

Browse files
committed
docs(adr-0120): add the per-object batch upsert by id to the create doors; pin the edit-without-read class
The ADR-0120 2026-11-01 amendment gains a dated note: `POST /data/:object/batch` with `operation: 'upsert'` is a create door for a row naming an id the caller cannot read, its collision and refusal answers, and the driver-memory persisted-duplicate residual. The dogfood pin adds the edit-without-read principal class (with and without create) on an open-write object to its HIDDEN table, and the fork comment names the old fork's third outcome. Claude-Session: https://claude.ai/code/session_017xRBcVy4VXmkQjn2MGeaWk Co-authored-by: Claude <noreply@anthropic.com>
1 parent b48eeb1 commit 751c15a

3 files changed

Lines changed: 127 additions & 22 deletions

File tree

‎docs/adr/0120-unique-scope-vocabulary-and-null-safe-tenant-uniqueness.md‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ Two properties of this table are the ADR's acceptance criteria:
183183
> - **Every create door accepts a caller-supplied `id`, and a fresh one lands as given:**
184184
> `POST /data/:object`, `POST /data/:object/batch` with `operation: 'create'`, `createMany`,
185185
> the cross-object `POST /batch`, import in `insert` mode and in `upsert` mode (an upsert
186-
> whose match, run as the caller, finds no row creates), and clone's `overrides.id`.
186+
> whose match, run as the caller, finds no row creates), and clone's `overrides.id`; and
187+
> `POST /data/:object/batch` with `operation: 'upsert'` for a row naming an `id` the caller
188+
> cannot read or an `id` that names no row (added by the dated note at the end of this
189+
> amendment).
187190
> - **A create that collides on `id` answers `409 UNIQUE_VIOLATION` and writes nothing**, on a
188191
> store that enforces the primary key — the SQL-family drivers do (`driver-sql`, and
189192
> `driver-sqlite-wasm` and `driver-turso`, which extend it); `driver-memory`, the
@@ -244,6 +247,37 @@ Two properties of this table are the ADR's acceptance criteria:
244247
> (same organization, the stock showcase) and
245248
> [`create-caller-id-collision-walled.dogfood.test.ts`](../../packages/qa/dogfood/test/create-caller-id-collision-walled.dogfood.test.ts)
246249
> (another organization, `isolated` and `group`).
250+
>
251+
> **Amendment (2026-11-01, ruling record
252+
> [`5985885287`](https://github.com/objectstack-ai/objectstack/issues/21771#issuecomment-5985885287)
253+
> applied to the per-object batch `upsert` by commit `7b62f0a77`) — `POST /data/:object/batch`
254+
> with `operation: 'upsert'` is a create door for a row naming an `id` the caller cannot read,
255+
> and joins the list above.**
256+
>
257+
> The by-id upsert matches on the row's `id`, read as the caller — the object-level read
258+
> admission, then the caller's own read of that id, under the context its write runs with. A
259+
> row the caller can read is matched and takes the update arm, where the engine still judges
260+
> the write. A row the caller cannot read is, on the write doors, a row that does not exist
261+
> (ruling record `5985885287`), so it takes the create arm: the same insert and the same row
262+
> answer as `operation: 'create'` naming that `id`. Every fact above holds for that arm, and two
263+
> are particular to it:
264+
>
265+
> - **A held `id` the caller cannot read meets the collision, never a match.** Import `upsert`
266+
> matches on the mapping's declared key fields; this door's match key is the `id` itself, so
267+
> the only answer a held-but-unreadable `id` can get here is the create's: `409
268+
> UNIQUE_VIOLATION` on a store that enforces the primary key, the same row error a batch
269+
> create naming that `id` carries, identical whether the row is hidden inside the caller's
270+
> organization or held by another one. A caller without the create grant gets the create's
271+
> refusal instead, the same for a held `id` and for one that names no row.
272+
> - **On `driver-memory` the create arm lands a second row with that `id`**, as every create
273+
> door does there; when that driver runs with persistence (its opt-in `persistence` option
274+
> or a custom `persistenceAdapter`), the duplicate is also written to that development store.
275+
> Accepted as the same residual: the development and test driver declares no primary key.
276+
>
277+
> Pinned at the HTTP door, atomic and not, by
278+
> [`upsert-by-id-caller-read.dogfood.test.ts`](../../packages/qa/dogfood/test/upsert-by-id-caller-read.dogfood.test.ts)
279+
> (the stock showcase, and a walled two-organization boot), and at the protocol seam by
280+
> [`protocol.upsert-existence.test.ts`](../../packages/metadata-protocol/src/protocol.upsert-existence.test.ts).
247281
248282
## Posture portability: one app package, every tenancy posture
249283

‎packages/metadata-protocol/src/protocol-batch-data.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ export async function runBatchDataLoop(
404404
// row — so a caller holding create learned that a
405405
// row it cannot read exists, which ruling
406406
// 5985885287 closed on the by-id update and delete
407-
// doors;
407+
// doors — and that fork could reach the update arm
408+
// for a principal the caller-read rule now sends
409+
// to the create arm;
408410
// - the key collision #5187 called a misdirection is
409411
// the answer the maintainer ratified on 2026-11-01
410412
// as the accepted cost of a deployment-global key,

‎packages/qa/dogfood/test/upsert-by-id-caller-read.dogfood.test.ts‎

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
//
1616
// 1. ⭐ an upsert naming a row the caller cannot read answers the SAME body as
1717
// a batch create naming that id (the row's `id` echo aside), whose code and
18-
// status are the plain `POST /data/:object` create's `409
19-
// UNIQUE_VIOLATION`; the stored row is byte-identical afterwards and the
20-
// answer names no other organization;
21-
// 2. an upsert naming an id that exists nowhere is created, as the create is;
18+
// status are the plain `POST /data/:object` create's — `409
19+
// UNIQUE_VIOLATION` for a caller holding the create grant, the create's
20+
// `403 PERMISSION_DENIED` for one without it; the stored row is
21+
// byte-identical afterwards and the answer names no other organization;
22+
// 2. an upsert naming an id that exists nowhere answers as the create does:
23+
// created with the create grant, the create's refusal without it (and
24+
// then the same refusal as for a held id);
2225
// 3. a row the caller CAN read keeps the update arm: written with the edit
2326
// grant, the engine's refusal without it.
2427
//
@@ -32,6 +35,12 @@
3235
// reads: the update arm's refusal, unchanged;
3336
// - a create-only principal on `showcase_contact` (no read at all) — every
3437
// row is one it cannot read;
38+
// - the edit-without-read class on `showcase_semantic_zoo`: a principal
39+
// holding edit and no read, once without and once with create. The object
40+
// declares `public_read_write`, so the platform's `update` ownership floor
41+
// does not bind it, and no baseline set grants anything on it; an edit
42+
// grant makes no row readable, so every row is one the principal cannot
43+
// read;
3544
// - control: member, `showcase_account` (read only) — one refusal for a row
3645
// that exists and for one that does not, unchanged.
3746
//
@@ -100,13 +109,22 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
100109
let adminTok: string;
101110
let memberTok: string;
102111
let creatorTok: string;
112+
let editorTok: string;
113+
let editCreatorTok: string;
103114
const d = doors(() => stack);
104115
const rows: Record<string, string> = {};
105116
let projectId = '';
106117

107118
const CREATE_ONLY = 'qa_upsert_read_contact_create_only';
108119
const CREATOR = 'upsert-read-creator@verify.test';
109120
const MEMBER = 'upsert-read-member@verify.test';
121+
// The edit-without-read class, on an object whose OWD keeps the platform's
122+
// `update` ownership floor out of play and that no baseline set grants.
123+
const OPEN_WRITE = 'showcase_semantic_zoo';
124+
const EDIT_ONLY = 'qa_upsert_read_zoo_edit_only';
125+
const EDIT_CREATE = 'qa_upsert_read_zoo_edit_create';
126+
const EDITOR = 'upsert-read-editor@verify.test';
127+
const EDIT_CREATOR = 'upsert-read-edit-creator@verify.test';
110128

111129
const stored = async (object: string, id: string) =>
112130
JSON.stringify(await ql.findOne(object, { where: { id }, context: SYS }));
@@ -117,6 +135,27 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
117135
showcase_task: () => ({ title: 'upsert probe', project: projectId, status: 'todo' }),
118136
showcase_contact: () => ({ name: 'Upsert Probe', email: 'upsert-probe@verify.test' }),
119137
showcase_account: () => ({ name: 'Upsert Probe', status: 'prospect' }),
138+
[OPEN_WRITE]: () => ({ name: 'Upsert Probe' }),
139+
};
140+
141+
/** Save a permission set granting `grant` on one object, and bind it to a fresh user. */
142+
const principalWith = async (set: string, object: string, grant: Record<string, boolean>, email: string): Promise<string> => {
143+
const saved = await stack.apiAs(adminTok, 'PUT', `/meta/permission/${set}`, {
144+
name: set, label: set, systemPermissions: [], objects: { [object]: grant },
145+
});
146+
expect(saved.status, await saved.clone().text()).toBe(200);
147+
const token = await stack.signUp(email, PASS);
148+
const userId = (await ql.findOne('sys_user', { where: { email }, context: SYS }))?.id;
149+
expect(userId, `${email} provisioned`).toBeTruthy();
150+
await ql.insert('sys_user_permission_set', { user_id: userId, permission_set: set }, { context: SYS });
151+
return token;
152+
};
153+
154+
/** The `object_crud` layer's verdict `/security/explain` gives the caller for one operation. */
155+
const crudVerdict = async (token: string, object: string, operation: string): Promise<string> => {
156+
const res = await d.call(token, 'GET', `/security/explain?object=${object}&operation=${operation}`);
157+
expect(res.status, JSON.stringify(res.body)).toBe(200);
158+
return String((res.body?.layers ?? []).find((l: any) => l?.layer === 'object_crud')?.verdict);
120159
};
121160

122161
beforeAll(async () => {
@@ -137,12 +176,16 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
137176
expect(creatorId, 'creator provisioned').toBeTruthy();
138177
await ql.insert('sys_user_permission_set', { user_id: creatorId, permission_set: CREATE_ONLY }, { context: SYS });
139178

179+
// The edit-without-read class: edit and no read, without and with create.
180+
editorTok = await principalWith(EDIT_ONLY, OPEN_WRITE, { allowRead: false, allowCreate: false, allowEdit: true, allowDelete: false }, EDITOR);
181+
editCreatorTok = await principalWith(EDIT_CREATE, OPEN_WRITE, { allowRead: false, allowCreate: true, allowEdit: true, allowDelete: false }, EDIT_CREATOR);
182+
140183
projectId = String((await ql.find('showcase_project', { limit: 1, context: SYS }))?.[0]?.id ?? '');
141184
expect(projectId, 'the showcase seed provides a project').toBeTruthy();
142185

143186
// Rows the admin owns: hidden from the member by the private OWD, and from
144-
// the creator by having no read at all.
145-
for (const object of ['showcase_inquiry', 'showcase_private_note', 'showcase_contact']) {
187+
// the creator and the edit-without-read principals by having no read at all.
188+
for (const object of ['showcase_inquiry', 'showcase_private_note', 'showcase_contact', OPEN_WRITE]) {
146189
const made = await d.create(adminTok, object, unknownId(), PAYLOAD[object]());
147190
expect(made.status, `admin ${object} created — ${JSON.stringify(made.body)}`).toBeLessThan(300);
148191
rows[`hidden:${object}`] = createdId(made);
@@ -171,15 +214,32 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
171214
expect((await d.create(memberTok, 'showcase_account', unknownId(), PAYLOAD.showcase_account())).status, 'the member may not create accounts').toBe(403);
172215
expect((await d.call(creatorTok, 'GET', `/data/showcase_contact/${rows['hidden:showcase_contact']}`)).status, 'the creator reads no contact').toBe(403);
173216
expect((await d.create(creatorTok, 'showcase_contact', unknownId(), PAYLOAD.showcase_contact())).status, 'the creator creates contacts').toBe(201);
217+
218+
// The edit-without-read class: the edit grant is real, the read grant is
219+
// absent, and the create grant is exactly where the table says.
220+
const zooRow = rows[`hidden:${OPEN_WRITE}`];
221+
expect((await d.call(adminTok, 'GET', `/data/${OPEN_WRITE}/${zooRow}`)).status).toBe(200);
222+
for (const [who, tok, mayCreate] of [['edit only', editorTok, false], ['edit+create', editCreatorTok, true]] as const) {
223+
expect(await crudVerdict(tok, OPEN_WRITE, 'update'), `${who}: holds edit`).toBe('grants');
224+
expect(await crudVerdict(tok, OPEN_WRITE, 'read'), `${who}: holds no read`).toBe('denies');
225+
expect((await d.call(tok, 'GET', `/data/${OPEN_WRITE}/${zooRow}`)).status, `${who}: reads no row`).toBe(403);
226+
expect((await d.create(tok, OPEN_WRITE, unknownId(), PAYLOAD[OPEN_WRITE]())).status, `${who}: the create grant`).toBe(mayCreate ? 201 : 403);
227+
}
174228
});
175229

176-
const HIDDEN: Array<[string, string, () => string]> = [
177-
['member, read+create without edit, private row', 'showcase_inquiry', () => memberTok],
178-
['member, read+create+edit, private row', 'showcase_private_note', () => memberTok],
179-
['create-only principal, no read at all', 'showcase_contact', () => creatorTok],
230+
/** The plain create's answer for an id already held, by whether the caller holds the create grant. */
231+
const COLLISION = { status: 409, code: 'UNIQUE_VIOLATION', mayCreate: true } as const;
232+
const CREATE_REFUSED = { status: 403, code: 'PERMISSION_DENIED', mayCreate: false } as const;
233+
234+
const HIDDEN: Array<[string, string, () => string, typeof COLLISION | typeof CREATE_REFUSED]> = [
235+
['member, read+create without edit, private row', 'showcase_inquiry', () => memberTok, COLLISION],
236+
['member, read+create+edit, private row', 'showcase_private_note', () => memberTok, COLLISION],
237+
['create-only principal, no read at all', 'showcase_contact', () => creatorTok, COLLISION],
238+
['edit without read or create, open-write OWD', OPEN_WRITE, () => editorTok, CREATE_REFUSED],
239+
['edit+create without read, open-write OWD', OPEN_WRITE, () => editCreatorTok, COLLISION],
180240
];
181241

182-
for (const [label, object, tok] of HIDDEN) {
242+
for (const [label, object, tok, answer] of HIDDEN) {
183243
for (const atomic of [false, true]) {
184244
it(`${label} (${object}, ${atomic ? 'atomic' : 'non-atomic'}): the unreadable row answers as a create naming it`, async () => {
185245
const id = rows[`hidden:${object}`];
@@ -192,25 +252,34 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
192252
// ⭐ The create's answer, whole: same status, same envelope, same row
193253
// refusal — nothing of the upsert's own.
194254
expect(comparable(ups), JSON.stringify(ups.body)).toEqual(comparable(batchCreate));
195-
expect(create.status, JSON.stringify(create.body)).toBe(409);
196-
expect(create.body?.code).toBe('UNIQUE_VIOLATION');
255+
expect(create.status, JSON.stringify(create.body)).toBe(answer.status);
256+
expect(create.body?.code).toBe(answer.code);
197257
expect(ups.body?.results?.[0]?.errors?.[0]).toMatchObject({ code: create.body.code, httpStatus: create.status });
198258
expect(ups.body?.results?.[0]?.id).toBe(id);
199259
// Nothing moved.
200260
expect(await stored(object, id)).toBe(before);
201261
});
202262

203-
it(`${label} (${object}, ${atomic ? 'atomic' : 'non-atomic'}): an id that names no row is created, as the create is`, async () => {
263+
it(`${label} (${object}, ${atomic ? 'atomic' : 'non-atomic'}): an id that names no row answers as the create does`, async () => {
204264
const id = unknownId();
205265
const ups = await d.upsert(tok(), object, id, PAYLOAD[object](), atomic);
206-
expect(ups.status, JSON.stringify(ups.body)).toBe(200);
207-
expect(ups.body?.success, JSON.stringify(ups.body)).toBe(true);
208-
expect(ups.body?.results?.[0]).toMatchObject({ id, success: true });
209-
expect(JSON.parse(await stored(object, id))?.id).toBe(id);
210-
211266
const other = unknownId();
212267
const batchCreate = await d.batchCreate(tok(), object, other, PAYLOAD[object](), atomic);
213-
expect(batchCreate.body?.success, JSON.stringify(batchCreate.body)).toBe(true);
268+
if (answer.mayCreate) {
269+
expect(ups.status, JSON.stringify(ups.body)).toBe(200);
270+
expect(ups.body?.success, JSON.stringify(ups.body)).toBe(true);
271+
expect(ups.body?.results?.[0]).toMatchObject({ id, success: true });
272+
expect(JSON.parse(await stored(object, id))?.id).toBe(id);
273+
expect(batchCreate.body?.success, JSON.stringify(batchCreate.body)).toBe(true);
274+
return;
275+
}
276+
// Without the create grant: the create's refusal, nothing stored, and
277+
// the same answer an upsert naming a held id gets.
278+
expect(comparable(ups), JSON.stringify(ups.body)).toEqual(comparable(batchCreate));
279+
expect(ups.body?.results?.[0]?.errors?.[0], JSON.stringify(ups.body)).toMatchObject({ code: answer.code, httpStatus: answer.status });
280+
expect(await ql.findOne(object, { where: { id }, context: SYS })).toBeFalsy();
281+
const held = await d.upsert(tok(), object, rows[`hidden:${object}`], PAYLOAD[object](), atomic);
282+
expect(comparable(held), JSON.stringify(held.body)).toEqual(comparable(ups));
214283
});
215284
}
216285
}

0 commit comments

Comments
 (0)