@syncular/server 0.77.3 → 0.78.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/push.js CHANGED
@@ -10,8 +10,9 @@ import { retainRollbackFailure } from './storage-errors.js';
10
10
  * - a lost `baseVersion = 0` insert race re-authorizes the winner's row
11
11
  * before disclosing it in a conflict record (§6.2).
12
12
  *
13
- * Per-commit atomicity (§6.4): one storage transaction per commit; the
14
- * idempotency record persists in the same transaction as the writes.
13
+ * Atomicity (§6.4): one storage transaction per push request, one savepoint
14
+ * per commit; every idempotency record persists in the same transaction as
15
+ * the writes.
15
16
  *
16
17
  * Optional per-table write validation (§6.7) runs after decode + the §3.4
17
18
  * scope check, on the row that will persist (post scope-strip, post CRDT
@@ -25,7 +26,7 @@ import { emitEvent } from './events.js';
25
26
  import { prepareReactions, toNewReactions, } from './reactions.js';
26
27
  import { authorizeWrite, renderScopeValue, storedScopesForRow } from './scopes.js';
27
28
  import { decodeColumnVersions, encodeColumnVersions } from './relational-rows.js';
28
- import { readCandidateRows, serveGateRefusal } from './storage.js';
29
+ import { commitDeleteFloor, readCandidateRows, serveGateRefusal, } from './storage.js';
29
30
  import { StorageConstraintError } from './storage-errors.js';
30
31
  import { transactionQuery } from './storage-query.js';
31
32
  import { serveNotReadyError } from './readiness.js';
@@ -1130,11 +1131,10 @@ function resultFrame(clientCommitId, stored, replay) {
1130
1131
  results: [...stored.results],
1131
1132
  };
1132
1133
  }
1133
- function processedPushCommit(clientCommitId, stored, replayed, watermark) {
1134
+ function processedPushCommit(clientCommitId, stored, replayed) {
1134
1135
  return {
1135
1136
  frame: resultFrame(clientCommitId, stored, replayed),
1136
1137
  replayed,
1137
- ...(watermark !== undefined ? { watermark } : {}),
1138
1138
  ...(stored.recordedAtMs !== undefined
1139
1139
  ? { recordedAtMs: stored.recordedAtMs }
1140
1140
  : {}),
@@ -1184,43 +1184,26 @@ function commitExpiredFrame(clientCommitId) {
1184
1184
  };
1185
1185
  }
1186
1186
  /**
1187
- * Process one `PUSH_COMMIT` frame: idempotency replay (§2.3), sequential
1188
- * atomic apply (§6.4), realtime notification for applied commits.
1187
+ * The serialized apply of one commit outside an SSP2 request: an
1188
+ * authoritative command or an erasure.
1189
1189
  */
1190
- export async function processPushCommit(ctx, schema, resolved, clientId, frame) {
1191
- return (await processPushCommitWithTrace(ctx, schema, resolved, clientId, frame)).frame;
1190
+ export async function processPushOperationsWithTrace(ctx, schema, resolved, clientId, clientCommitId, buildOperations, wireSchema = schema) {
1191
+ const [processed] = await processPushCommitsWithTrace(ctx, schema, resolved, clientId, [{ clientCommitId, buildOperations }], wireSchema);
1192
+ return processed;
1192
1193
  }
1193
1194
  /**
1194
- * Host-observable variant of `processPushCommit`. The SSP2 wire frame keeps
1195
- * rejected replays as `status: rejected`; this companion result preserves the
1196
- * cache provenance needed by structured events and server helpers.
1197
- */
1198
- export async function processPushCommitWithTrace(ctx, schema, resolved, clientId, frame, wireSchema = schema, outbox) {
1199
- // The frame names its rows before the transaction opens, so the admission
1200
- // read carries the commit's read-ahead and the apply issues no separate
1201
- // prefetch statement.
1202
- const targets = new Map();
1203
- for (const op of frame.operations) {
1204
- // A rowId holding U+0000 is rejected per operation (§6.1) before any
1205
- // store reads it.
1206
- if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
1207
- continue;
1208
- const ids = targets.get(op.table) ?? [];
1209
- ids.push(op.rowId);
1210
- targets.set(op.table, ids);
1211
- }
1212
- return processPushOperationsWithTrace(ctx, schema, resolved, clientId, frame.clientCommitId, async () => frame.operations, wireSchema, targets.size === 0
1213
- ? undefined
1214
- : [...targets].map(([table, rowIds]) => ({ table, rowIds })), outbox);
1215
- }
1216
- /**
1217
- * Shared serialized apply path for SSP2 commits and authoritative commands.
1218
- * The builder runs after the partition lock and idempotency re-check, so its
1219
- * reads and the returned operations share the transaction that is committed.
1195
+ * Apply one push request (§6.4): one storage transaction under one partition
1196
+ * lock, one serve-gate and idempotency read for every commit, each commit
1197
+ * inside its own savepoint in frame order. A commit's rejection rolls back
1198
+ * only that commit; any other failure rolls back the whole request and
1199
+ * throws. Reaction events and realtime notifications leave after the
1200
+ * transaction commits, once per applied commit in `commitSeq` order.
1201
+ *
1202
+ * `outbox` lists every commit of the request in the same order as
1203
+ * `commits`; it carries the §6.3.2 sequences and the §9 settlement.
1220
1204
  */
1221
- export async function processPushOperationsWithTrace(ctx, schema, resolved, clientId, clientCommitId, buildOperations, wireSchema = schema, prefetchTargets, outbox) {
1205
+ export async function processPushCommitsWithTrace(ctx, schema, resolved, clientId, commits, wireSchema = schema, outbox) {
1222
1206
  const { partition } = ctx;
1223
- const createdAtMs = clockOf(ctx)();
1224
1207
  const blobCtx = {
1225
1208
  store: ctx.blobs,
1226
1209
  partition,
@@ -1230,348 +1213,375 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
1230
1213
  const validators = ctx.validators;
1231
1214
  const commitValidator = ctx.commitValidator;
1232
1215
  const reactionPlanner = ctx.reactionPlanner;
1216
+ const processed = [];
1217
+ const applied = [];
1218
+ // Reaction events and realtime notifications of the applied commits, in
1219
+ // commitSeq order, once they are durable.
1220
+ const announce = async () => {
1221
+ for (const commit of applied) {
1222
+ if (ctx.events !== undefined) {
1223
+ const atMs = clockOf(ctx)();
1224
+ for (const reaction of commit.reactions) {
1225
+ emitEvent(ctx.events, {
1226
+ type: 'reaction.queued',
1227
+ atMs,
1228
+ partition,
1229
+ actorId: ctx.actorId,
1230
+ clientId,
1231
+ clientCommitId: commit.clientCommitId,
1232
+ commitSeq: commit.commitSeq,
1233
+ idempotencyKey: reaction.idempotencyKey,
1234
+ reactionType: reaction.type,
1235
+ version: reaction.version,
1236
+ });
1237
+ }
1238
+ }
1239
+ if (ctx.realtime !== undefined && commit.changes.length > 0) {
1240
+ // RFC 0007 fanout decision: the notification carries the durable commit
1241
+ // entry itself (this commit's `changes`), not a projection read or a pull
1242
+ // window, so fanning it out before the request's buffered read-verify can
1243
+ // never serve a mixed result. Each peer's next pull is gated at the
1244
+ // storage seam; a refused mixed push+pull is safe because the push is
1245
+ // durable and replayable under its commit id. This is commit-entry
1246
+ // durability, not gate inheritance.
1247
+ await ctx.realtime.notifyCommit(partition, {
1248
+ commitSeq: commit.commitSeq,
1249
+ createdAtMs: commit.createdAtMs,
1250
+ actorId: ctx.actorId,
1251
+ changes: commit.changes,
1252
+ });
1253
+ }
1254
+ }
1255
+ };
1233
1256
  const tx = await ctx.storage.begin(partition);
1234
- // §6.3.2: the settlement's writes commit whatever the commit's outcome.
1235
- const release = () => outbox?.settlement !== undefined ? tx.commit() : tx.rollback();
1236
- const sequence = outbox === undefined
1237
- ? undefined
1238
- : { outboxId: outbox.outboxId, clientSeq: outbox.clientSeq };
1239
1257
  try {
1240
1258
  if (reactionPlanner !== undefined && tx.enqueueReactions === undefined) {
1241
1259
  throw new Error('storage transaction does not support configured durable reactions');
1242
1260
  }
1243
1261
  // The storage takes the partition lock and then reads the admission token
1244
1262
  // on the transaction's own connection: the RFC 0007 serve gate and the
1245
- // §2.3 idempotency record, plus any read-ahead the caller already knows.
1246
- // A racing duplicate's result commits before the lock releases, so the
1247
- // later read is exact and a duplicate never reruns an operation,
1248
- // validator, or merge.
1249
- let prepared;
1250
- try {
1251
- prepared = await tx.preparePushApply(clientId, clientCommitId, schema.version, prefetchTargets, outbox?.settlement);
1252
- }
1253
- catch (error) {
1254
- if (error instanceof SyncError &&
1255
- error.code === 'sync.idempotency_cache_miss') {
1256
- // §6.3: answer the retryable cache-miss for this commit rather than
1257
- // re-applying. Not persisted — a retry may find a readable record.
1258
- await release();
1259
- return {
1260
- frame: idempotencyCacheMissFrame(clientCommitId, error),
1261
- replayed: false,
1262
- };
1263
- }
1264
- throw error;
1265
- }
1263
+ // §2.3 idempotency record of every commit, plus any read-ahead the
1264
+ // commits already name. A racing duplicate's result commits before the
1265
+ // lock releases, so the later read is exact and a duplicate never reruns
1266
+ // an operation, validator, or merge.
1267
+ const prepared = await tx.preparePushApply(clientId, commits.map((commit) => ({
1268
+ clientCommitId: commit.clientCommitId,
1269
+ ...(commit.targets === undefined ? {} : { targets: commit.targets }),
1270
+ })), schema.version, outbox);
1266
1271
  // RFC 0007: the gate is evaluated while the partition lock is held, so
1267
- // the migration check and the write share one transaction and no
1272
+ // the migration check and the writes share one transaction and no
1268
1273
  // migration can interleave in between.
1269
1274
  const refusal = serveGateRefusal(prepared.gate, schema.version, ctx.checkpoints);
1270
1275
  if (refusal !== undefined) {
1271
- await release();
1276
+ // §6.3.2: the settlement commits whatever the request's outcome.
1277
+ await tx.commit();
1272
1278
  throw serveNotReadyError(refusal);
1273
1279
  }
1274
- const watermark = prepared.watermark ?? outbox?.watermark;
1275
- if (prepared.persisted !== undefined) {
1276
- const conflict = prepared.persisted.results.find((result) => result.status === 'conflict');
1277
- const authorization = prepared.persisted.conflictAuthorization;
1278
- const table = authorization && schema.tables.get(authorization.table);
1279
- if (conflict !== undefined &&
1280
- (authorization === undefined ||
1281
- authorization.schemaVersion !== schema.version ||
1282
- table === undefined ||
1283
- !authorizeWrite(table, authorization.scopes, resolved))) {
1284
- await release();
1285
- return processedPushCommit(clientCommitId, {
1286
- ...prepared.persisted,
1287
- results: [
1288
- {
1289
- opIndex: conflict.opIndex,
1290
- status: 'error',
1291
- code: 'sync.forbidden',
1292
- message: 'conflict replay denied by scope authorization (§3.4)',
1293
- retryable: false,
1294
- },
1295
- ],
1296
- }, true, watermark);
1280
+ const watermark = prepared.watermark;
1281
+ // §2.2: inserts start above the floor, which applied deletes raise.
1282
+ let requestDeleteFloor = prepared.deleteFloor;
1283
+ // §6.3: a clientCommitId repeated within the request replays the outcome
1284
+ // its first occurrence recorded in this transaction.
1285
+ const recorded = new Map();
1286
+ for (const [index, commit] of commits.entries()) {
1287
+ const { clientCommitId } = commit;
1288
+ const sequence = outbox === undefined
1289
+ ? undefined
1290
+ : {
1291
+ outboxId: outbox.outboxId,
1292
+ clientSeq: outbox.commits[index].clientSeq,
1293
+ };
1294
+ let persisted;
1295
+ try {
1296
+ persisted =
1297
+ recorded.get(clientCommitId) ?? prepared.persisted(clientCommitId);
1297
1298
  }
1298
- await release();
1299
- return processedPushCommit(clientCommitId, prepared.persisted, true, watermark);
1300
- }
1301
- // §6.3.2 step 3: below the watermark without a retained result.
1302
- if (outbox !== undefined &&
1303
- watermark !== undefined &&
1304
- outbox.clientSeq < watermark) {
1305
- await release();
1306
- return {
1307
- frame: commitExpiredFrame(clientCommitId),
1308
- replayed: false,
1309
- watermark,
1310
- };
1311
- }
1312
- const results = [];
1313
- const changes = [];
1314
- const validatedOperations = [];
1315
- const stagedWrites = [];
1316
- let terminated;
1317
- const operations = await buildOperations(tx);
1318
- if (tx.prefetchRows !== undefined) {
1319
- const targets = new Map();
1320
- for (const op of operations) {
1321
- if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
1299
+ catch (error) {
1300
+ if (error instanceof SyncError &&
1301
+ error.code === 'sync.idempotency_cache_miss') {
1302
+ // §6.3: answer the retryable cache-miss for this commit rather than
1303
+ // re-applying. Not persisted: a retry may find a readable record.
1304
+ processed.push({
1305
+ frame: idempotencyCacheMissFrame(clientCommitId, error),
1306
+ replayed: false,
1307
+ });
1322
1308
  continue;
1323
- const ids = targets.get(op.table) ?? [];
1324
- ids.push(op.rowId);
1325
- targets.set(op.table, ids);
1326
- }
1327
- if (targets.size > 0) {
1328
- await tx.prefetchRows([...targets].map(([table, rowIds]) => ({ table, rowIds })));
1309
+ }
1310
+ throw error;
1329
1311
  }
1330
- }
1331
- // §6.6: the blobIds the commit pushes are checked once, concurrently,
1332
- // before the operation loop. A payload that fails decode is skipped here
1333
- // and rejected by its operation.
1334
- const pushedBlobIds = [];
1335
- for (const op of operations) {
1336
- const table = schema.tables.get(op.table);
1337
- const wireTable = wireSchema.tables.get(op.table);
1338
- if (op.payload === undefined ||
1339
- table === undefined ||
1340
- wireTable === undefined ||
1341
- table.blobRefColumnIndices.length === 0) {
1312
+ if (persisted !== undefined) {
1313
+ const conflict = persisted.results.find((result) => result.status === 'conflict');
1314
+ const authorization = persisted.conflictAuthorization;
1315
+ const table = authorization && schema.tables.get(authorization.table);
1316
+ processed.push(processedPushCommit(clientCommitId, conflict !== undefined &&
1317
+ (authorization === undefined ||
1318
+ authorization.schemaVersion !== schema.version ||
1319
+ table === undefined ||
1320
+ !authorizeWrite(table, authorization.scopes, resolved))
1321
+ ? {
1322
+ ...persisted,
1323
+ results: [
1324
+ {
1325
+ opIndex: conflict.opIndex,
1326
+ status: 'error',
1327
+ code: 'sync.forbidden',
1328
+ message: 'conflict replay denied by scope authorization (§3.4)',
1329
+ retryable: false,
1330
+ },
1331
+ ],
1332
+ }
1333
+ : persisted, true));
1342
1334
  continue;
1343
1335
  }
1344
- let sparse;
1345
- try {
1346
- sparse = decodeSparseRow(wireTable.columns, wireTable.primaryKeyIndex, op.payload);
1347
- }
1348
- catch {
1336
+ // §6.3.2 step 3: below the watermark without a retained result.
1337
+ if (sequence !== undefined &&
1338
+ watermark !== undefined &&
1339
+ sequence.clientSeq < watermark) {
1340
+ processed.push({
1341
+ frame: commitExpiredFrame(clientCommitId),
1342
+ replayed: false,
1343
+ });
1349
1344
  continue;
1350
1345
  }
1351
- for (const index of table.blobRefColumnIndices) {
1352
- const value = sparse[index];
1353
- if (typeof value === 'string') {
1354
- pushedBlobIds.push(parseBlobRef(value).blobId);
1346
+ const createdAtMs = clockOf(ctx)();
1347
+ let stored;
1348
+ try {
1349
+ const results = [];
1350
+ const changes = [];
1351
+ const validatedOperations = [];
1352
+ const stagedWrites = [];
1353
+ let terminated;
1354
+ const operations = await commit.buildOperations(tx);
1355
+ if (tx.prefetchRows !== undefined) {
1356
+ const targets = new Map();
1357
+ for (const op of operations) {
1358
+ if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
1359
+ continue;
1360
+ const ids = targets.get(op.table) ?? [];
1361
+ ids.push(op.rowId);
1362
+ targets.set(op.table, ids);
1363
+ }
1364
+ if (targets.size > 0) {
1365
+ await tx.prefetchRows([...targets].map(([table, rowIds]) => ({ table, rowIds })));
1366
+ }
1355
1367
  }
1356
- }
1357
- }
1358
- await resolveBlobPresence(blobCtx, pushedBlobIds);
1359
- // §6.4: the row each touched (table, rowId) held before this commit, so a
1360
- // conflict reports the row as stored once the commit rolls back.
1361
- const storedBeforeCommit = new Map();
1362
- // §2.2: inserts start above the floor, which this commit's deletes raise
1363
- // as they stage.
1364
- let deleteFloor = prepared.deleteFloor;
1365
- for (let opIndex = 0; opIndex < operations.length; opIndex++) {
1366
- const op = operations[opIndex];
1367
- if (op === undefined)
1368
- continue;
1369
- const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, ctx.context, deleteFloor);
1370
- if (outcome.kind === 'terminate') {
1371
- terminated = outcome.record;
1372
- break;
1373
- }
1374
- for (const change of outcome.changes) {
1375
- if (change.op === 'delete') {
1376
- deleteFloor = Math.max(deleteFloor, change.rowVersion);
1368
+ // §6.6: the blobIds the commit pushes are checked once, concurrently,
1369
+ // before the operation loop. A payload that fails decode is skipped
1370
+ // here and rejected by its operation.
1371
+ const pushedBlobIds = [];
1372
+ for (const op of operations) {
1373
+ const table = schema.tables.get(op.table);
1374
+ const wireTable = wireSchema.tables.get(op.table);
1375
+ if (op.payload === undefined ||
1376
+ table === undefined ||
1377
+ wireTable === undefined ||
1378
+ table.blobRefColumnIndices.length === 0) {
1379
+ continue;
1380
+ }
1381
+ let sparse;
1382
+ try {
1383
+ sparse = decodeSparseRow(wireTable.columns, wireTable.primaryKeyIndex, op.payload);
1384
+ }
1385
+ catch {
1386
+ continue;
1387
+ }
1388
+ for (const index of table.blobRefColumnIndices) {
1389
+ const value = sparse[index];
1390
+ if (typeof value === 'string') {
1391
+ pushedBlobIds.push(parseBlobRef(value).blobId);
1392
+ }
1393
+ }
1377
1394
  }
1378
- }
1379
- const rowKey = `${op.table}\u0000${op.rowId}`;
1380
- if (!storedBeforeCommit.has(rowKey)) {
1381
- storedBeforeCommit.set(rowKey, outcome.staged.stored);
1382
- }
1383
- results.push({ opIndex, status: 'applied' });
1384
- validatedOperations.push(...outcome.operations);
1385
- stagedWrites.push(outcome.staged);
1386
- changes.push(...outcome.changes);
1387
- }
1388
- // §6.11: reference enforcement runs after every client operation is
1389
- // staged (so the candidate state is final for the client's own writes)
1390
- // and before whole-commit validation.
1391
- let appendedOrigin = [];
1392
- if (terminated === undefined) {
1393
- const referencePass = await enforceReferences(tx, schema, resolved, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId, ctx.context);
1394
- if (referencePass.kind === 'terminate') {
1395
- terminated = referencePass.record;
1396
- }
1397
- else {
1398
- changes.push(...referencePass.changes);
1399
- validatedOperations.push(...referencePass.operations);
1400
- appendedOrigin = referencePass.originByIndex;
1401
- }
1402
- }
1403
- if (terminated === undefined) {
1404
- const commitReject = await runCommitValidator(commitValidator, ctx.onError, tx, schema, clientId, clientCommitId, ctx.actorId, partition, validatedOperations, ctx.context);
1405
- if (commitReject?.kind === 'terminate') {
1406
- // An appended §6.11 cascade operation carries a synthetic opIndex;
1407
- // surface the originating client operation instead.
1408
- const rejected = commitReject.record;
1409
- terminated =
1410
- rejected.opIndex >= operations.length
1411
- ? {
1412
- ...rejected,
1413
- opIndex: appendedOrigin[rejected.opIndex - operations.length] ?? 0,
1395
+ await resolveBlobPresence(blobCtx, pushedBlobIds);
1396
+ // §6.4: the row each touched (table, rowId) held before this commit,
1397
+ // so a conflict reports the row as stored once the commit rolls back.
1398
+ const storedBeforeCommit = new Map();
1399
+ // §2.2: this commit's deletes raise the floor as they stage.
1400
+ let deleteFloor = requestDeleteFloor;
1401
+ for (let opIndex = 0; opIndex < operations.length; opIndex++) {
1402
+ const op = operations[opIndex];
1403
+ if (op === undefined)
1404
+ continue;
1405
+ const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, ctx.context, deleteFloor);
1406
+ if (outcome.kind === 'terminate') {
1407
+ terminated = outcome.record;
1408
+ break;
1409
+ }
1410
+ for (const change of outcome.changes) {
1411
+ if (change.op === 'delete') {
1412
+ deleteFloor = Math.max(deleteFloor, change.rowVersion);
1414
1413
  }
1415
- : rejected;
1416
- }
1417
- }
1418
- let preparedReactions = [];
1419
- if (terminated === undefined && reactionPlanner !== undefined) {
1420
- preparedReactions = await prepareReactions(reactionPlanner, {
1421
- clientId,
1422
- clientCommitId,
1423
- actorId: ctx.actorId,
1424
- partition,
1425
- operations: validatedOperations,
1426
- read: commitValidationReader(tx, schema),
1427
- });
1428
- }
1429
- if (terminated !== undefined) {
1430
- // §6.3 rejected: only the terminating operation's record; §6.4:
1431
- // every write of the commit rolls back.
1432
- const conflictOperation = terminated.status === 'conflict'
1433
- ? operations[terminated.opIndex]
1434
- : undefined;
1435
- // §6.4: a conflict only arises in the operation loop, so a row no
1436
- // earlier operation touched still reads as stored before the commit.
1437
- const conflictKey = conflictOperation === undefined
1438
- ? undefined
1439
- : `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
1440
- const conflictRow = conflictOperation === undefined || conflictKey === undefined
1441
- ? undefined
1442
- : storedBeforeCommit.has(conflictKey)
1443
- ? storedBeforeCommit.get(conflictKey)
1444
- : await tx.getRow(conflictOperation.table, conflictOperation.rowId);
1445
- if (terminated.status === 'conflict') {
1446
- terminated =
1447
- conflictRow === undefined
1448
- ? errorRecord(terminated.opIndex, 'sync.row_missing', 'the conflicting row exists only through an earlier operation of the same commit (§6.4)').record
1449
- : {
1450
- ...terminated,
1451
- serverVersion: conflictRow.serverVersion,
1452
- serverRow: conflictRow.payload,
1453
- };
1454
- }
1455
- const stored = newStoredPushResult(createdAtMs, {
1456
- status: 'rejected',
1457
- ...(terminated.status === 'conflict' &&
1458
- conflictOperation !== undefined &&
1459
- conflictRow !== undefined
1460
- ? {
1461
- conflictAuthorization: {
1462
- table: conflictOperation.table,
1463
- schemaVersion: schema.version,
1464
- scopes: conflictRow.scopes,
1465
- },
1466
1414
  }
1467
- : {}),
1468
- results: [terminated],
1469
- });
1470
- // Discard candidates and persist the rejection while retaining the same
1471
- // partition lock. There is no unlock gap in which a duplicate can rerun,
1472
- // and the finalization returns the canonical outcome it persisted.
1473
- const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored, sequence);
1474
- return processedPushCommit(clientCommitId, canonical, canonical.cacheIdentity !== stored.cacheIdentity, watermark);
1475
- }
1476
- const commitSeq = await tx.appendCommit({
1477
- clientId,
1478
- clientCommitId,
1479
- actorId: ctx.actorId,
1480
- createdAtMs,
1481
- changes,
1482
- });
1483
- const newReactions = toNewReactions(preparedReactions, {
1484
- clientId,
1485
- clientCommitId,
1486
- commitSeq,
1487
- createdAtMs,
1488
- });
1489
- if (newReactions.length > 0) {
1490
- const enqueueReactions = tx.enqueueReactions;
1491
- if (enqueueReactions === undefined) {
1492
- throw new Error('storage lost durable reaction enqueue support');
1415
+ const rowKey = `${op.table}\u0000${op.rowId}`;
1416
+ if (!storedBeforeCommit.has(rowKey)) {
1417
+ storedBeforeCommit.set(rowKey, outcome.staged.stored);
1418
+ }
1419
+ results.push({ opIndex, status: 'applied' });
1420
+ validatedOperations.push(...outcome.operations);
1421
+ stagedWrites.push(outcome.staged);
1422
+ changes.push(...outcome.changes);
1423
+ }
1424
+ // §6.11: reference enforcement runs after every client operation is
1425
+ // staged (so the candidate state is final for the client's own
1426
+ // writes) and before whole-commit validation.
1427
+ let appendedOrigin = [];
1428
+ if (terminated === undefined) {
1429
+ const referencePass = await enforceReferences(tx, schema, resolved, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId, ctx.context);
1430
+ if (referencePass.kind === 'terminate') {
1431
+ terminated = referencePass.record;
1432
+ }
1433
+ else {
1434
+ changes.push(...referencePass.changes);
1435
+ validatedOperations.push(...referencePass.operations);
1436
+ appendedOrigin = referencePass.originByIndex;
1437
+ }
1438
+ }
1439
+ if (terminated === undefined) {
1440
+ const commitReject = await runCommitValidator(commitValidator, ctx.onError, tx, schema, clientId, clientCommitId, ctx.actorId, partition, validatedOperations, ctx.context);
1441
+ if (commitReject?.kind === 'terminate') {
1442
+ // An appended §6.11 cascade operation carries a synthetic
1443
+ // opIndex; surface the originating client operation instead.
1444
+ const rejected = commitReject.record;
1445
+ terminated =
1446
+ rejected.opIndex >= operations.length
1447
+ ? {
1448
+ ...rejected,
1449
+ opIndex: appendedOrigin[rejected.opIndex - operations.length] ?? 0,
1450
+ }
1451
+ : rejected;
1452
+ }
1453
+ }
1454
+ let preparedReactions = [];
1455
+ if (terminated === undefined && reactionPlanner !== undefined) {
1456
+ preparedReactions = await prepareReactions(reactionPlanner, {
1457
+ clientId,
1458
+ clientCommitId,
1459
+ actorId: ctx.actorId,
1460
+ partition,
1461
+ operations: validatedOperations,
1462
+ read: commitValidationReader(tx, schema),
1463
+ });
1464
+ }
1465
+ if (terminated !== undefined) {
1466
+ // §6.3 rejected: only the terminating operation's record; §6.4:
1467
+ // every write of the commit rolls back.
1468
+ const conflictOperation = terminated.status === 'conflict'
1469
+ ? operations[terminated.opIndex]
1470
+ : undefined;
1471
+ // §6.4: a conflict only arises in the operation loop, so a row no
1472
+ // earlier operation touched still reads as stored before the commit.
1473
+ const conflictKey = conflictOperation === undefined
1474
+ ? undefined
1475
+ : `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
1476
+ const conflictRow = conflictOperation === undefined || conflictKey === undefined
1477
+ ? undefined
1478
+ : storedBeforeCommit.has(conflictKey)
1479
+ ? storedBeforeCommit.get(conflictKey)
1480
+ : await tx.getRow(conflictOperation.table, conflictOperation.rowId);
1481
+ if (terminated.status === 'conflict') {
1482
+ terminated =
1483
+ conflictRow === undefined
1484
+ ? errorRecord(terminated.opIndex, 'sync.row_missing', 'the conflicting row exists only through an earlier operation of the same commit (§6.4)').record
1485
+ : {
1486
+ ...terminated,
1487
+ serverVersion: conflictRow.serverVersion,
1488
+ serverRow: conflictRow.payload,
1489
+ };
1490
+ }
1491
+ stored = newStoredPushResult(createdAtMs, {
1492
+ status: 'rejected',
1493
+ ...(terminated.status === 'conflict' &&
1494
+ conflictOperation !== undefined &&
1495
+ conflictRow !== undefined
1496
+ ? {
1497
+ conflictAuthorization: {
1498
+ table: conflictOperation.table,
1499
+ schemaVersion: schema.version,
1500
+ scopes: conflictRow.scopes,
1501
+ },
1502
+ }
1503
+ : {}),
1504
+ results: [terminated],
1505
+ });
1506
+ }
1507
+ else {
1508
+ const commitSeq = await tx.appendCommit({
1509
+ clientId,
1510
+ clientCommitId,
1511
+ actorId: ctx.actorId,
1512
+ createdAtMs,
1513
+ changes,
1514
+ });
1515
+ const reactions = toNewReactions(preparedReactions, {
1516
+ clientId,
1517
+ clientCommitId,
1518
+ commitSeq,
1519
+ createdAtMs,
1520
+ });
1521
+ if (reactions.length > 0) {
1522
+ const enqueueReactions = tx.enqueueReactions;
1523
+ if (enqueueReactions === undefined) {
1524
+ throw new Error('storage lost durable reaction enqueue support');
1525
+ }
1526
+ await enqueueReactions.call(tx, reactions);
1527
+ }
1528
+ stored = newStoredPushResult(createdAtMs, {
1529
+ status: 'applied',
1530
+ commitSeq,
1531
+ results,
1532
+ });
1533
+ // Closes the commit; D1 writes the commit's batch here.
1534
+ await tx.putPushResult(clientId, clientCommitId, stored, sequence);
1535
+ applied.push({
1536
+ clientCommitId,
1537
+ commitSeq,
1538
+ createdAtMs,
1539
+ changes,
1540
+ reactions,
1541
+ });
1542
+ requestDeleteFloor = Math.max(requestDeleteFloor, commitDeleteFloor(changes));
1543
+ }
1493
1544
  }
1494
- await enqueueReactions.call(tx, newReactions);
1495
- }
1496
- const stored = newStoredPushResult(createdAtMs, {
1497
- status: 'applied',
1498
- commitSeq,
1499
- results,
1500
- });
1501
- await tx.putPushResult(clientId, clientCommitId, stored, sequence);
1502
- await tx.commit();
1503
- if (ctx.events !== undefined) {
1504
- const atMs = clockOf(ctx)();
1505
- for (const reaction of newReactions) {
1506
- emitEvent(ctx.events, {
1507
- type: 'reaction.queued',
1508
- atMs,
1509
- partition,
1510
- actorId: ctx.actorId,
1511
- clientId,
1512
- clientCommitId,
1513
- commitSeq,
1514
- idempotencyKey: reaction.idempotencyKey,
1515
- reactionType: reaction.type,
1516
- version: reaction.version,
1545
+ catch (error) {
1546
+ if (!(error instanceof StorageConstraintError))
1547
+ throw error;
1548
+ stored = newStoredPushResult(createdAtMs, {
1549
+ status: 'rejected',
1550
+ results: [
1551
+ {
1552
+ opIndex: error.opIndex ?? 0,
1553
+ status: 'error',
1554
+ code: 'sync.constraint_violation',
1555
+ message: 'write violates a relational constraint',
1556
+ retryable: false,
1557
+ },
1558
+ ],
1517
1559
  });
1518
1560
  }
1561
+ if (stored.status === 'rejected') {
1562
+ // Discard the commit's candidates and record the rejection while the
1563
+ // partition lock is retained: no unlock gap lets a duplicate rerun.
1564
+ await tx.rejectPushCommit(clientId, clientCommitId, stored, sequence);
1565
+ }
1566
+ recorded.set(clientCommitId, stored);
1567
+ processed.push(processedPushCommit(clientCommitId, stored, false));
1519
1568
  }
1520
- if (ctx.realtime !== undefined && changes.length > 0) {
1521
- // RFC 0007 fanout decision: the notification carries the durable commit
1522
- // entry itself (this commit's `changes`), not a projection read or a pull
1523
- // window, so fanning it out before the request's buffered read-verify can
1524
- // never serve a mixed result. Each peer's next pull is gated at the
1525
- // storage seam; a refused mixed push+pull is safe because the push is
1526
- // durable and replayable under its commit id. This is commit-entry
1527
- // durability, not gate inheritance.
1528
- await ctx.realtime.notifyCommit(partition, {
1529
- commitSeq,
1530
- createdAtMs,
1531
- actorId: ctx.actorId,
1532
- changes,
1533
- });
1534
- }
1535
- return processedPushCommit(clientCommitId, stored, false, watermark);
1569
+ await tx.commit();
1536
1570
  }
1537
1571
  catch (error) {
1538
- if (error instanceof StorageConstraintError) {
1539
- const stored = newStoredPushResult(createdAtMs, {
1540
- status: 'rejected',
1541
- results: [
1542
- {
1543
- opIndex: error.opIndex ?? 0,
1544
- status: 'error',
1545
- code: 'sync.constraint_violation',
1546
- message: 'write violates a relational constraint',
1547
- retryable: false,
1548
- },
1549
- ],
1550
- });
1551
- try {
1552
- const canonical = await tx.commitRejectedPushResult(clientId, clientCommitId, stored, sequence);
1553
- return processedPushCommit(clientCommitId, canonical, canonical.cacheIdentity !== stored.cacheIdentity);
1554
- }
1555
- catch (finalizationError) {
1556
- // A failed finalization must still release the transaction: on
1557
- // SQLite an unreleased BEGIN wedges the storage's global begin()
1558
- // queue, on Postgres it leaks the pinned pool client. rollback() is
1559
- // a no-op when the finalization already committed.
1560
- try {
1561
- await tx.rollback();
1562
- }
1563
- catch (rollbackError) {
1564
- retainRollbackFailure(finalizationError, rollbackError);
1565
- }
1566
- throw finalizationError;
1567
- }
1568
- }
1569
1572
  try {
1570
1573
  await tx.rollback();
1571
1574
  }
1572
1575
  catch (rollbackError) {
1573
1576
  retainRollbackFailure(error, rollbackError);
1574
1577
  }
1578
+ // §6.4: a storage that persists each commit when it closes (D1) keeps the
1579
+ // commits that closed before the failure; they are durable, so peers
1580
+ // learn of them now rather than at their next pull.
1581
+ if (tx.persistsCommitsOnClose === true)
1582
+ await announce();
1575
1583
  throw error;
1576
1584
  }
1585
+ await announce();
1586
+ return processed;
1577
1587
  }