@syncular/server 0.58.0 → 0.60.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.
Files changed (103) hide show
  1. package/README.md +49 -36
  2. package/dist/admin.d.ts +32 -20
  3. package/dist/admin.js +46 -40
  4. package/dist/blob-handlers.d.ts +25 -31
  5. package/dist/blob-handlers.js +63 -41
  6. package/dist/blob-store.d.ts +42 -24
  7. package/dist/blob-store.js +0 -0
  8. package/dist/bucket-blob-store.d.ts +12 -8
  9. package/dist/bucket-blob-store.js +54 -45
  10. package/dist/bucket-segment-store.js +18 -16
  11. package/dist/content-encoding.d.ts +5 -2
  12. package/dist/content-encoding.js +42 -13
  13. package/dist/context.d.ts +19 -5
  14. package/dist/context.js +19 -1
  15. package/dist/d1-storage.d.ts +14 -98
  16. package/dist/d1-storage.js +249 -1119
  17. package/dist/errors.js +16 -0
  18. package/dist/events.d.ts +36 -18
  19. package/dist/frame-bytes.js +24 -6
  20. package/dist/handler.js +503 -208
  21. package/dist/local-authority.d.ts +2 -0
  22. package/dist/local-authority.js +24 -12
  23. package/dist/object-bucket.d.ts +2 -0
  24. package/dist/operations-realtime.js +69 -50
  25. package/dist/operations.d.ts +5 -2
  26. package/dist/operations.js +20 -29
  27. package/dist/pg-executor-pglite.d.ts +5 -6
  28. package/dist/pg-executor-pglite.js +22 -14
  29. package/dist/pg-executor.d.ts +4 -1
  30. package/dist/postgres-storage.d.ts +24 -19
  31. package/dist/postgres-storage.js +420 -262
  32. package/dist/pull.d.ts +70 -47
  33. package/dist/pull.js +270 -301
  34. package/dist/push.d.ts +12 -3
  35. package/dist/push.js +361 -173
  36. package/dist/realtime.d.ts +85 -39
  37. package/dist/realtime.js +355 -219
  38. package/dist/relational-rows.d.ts +21 -66
  39. package/dist/relational-rows.js +28 -109
  40. package/dist/s3-blob-store.d.ts +11 -12
  41. package/dist/s3-blob-store.js +20 -13
  42. package/dist/s3-object-bucket.d.ts +8 -1
  43. package/dist/s3-object-bucket.js +16 -3
  44. package/dist/schema.d.ts +14 -0
  45. package/dist/schema.js +23 -0
  46. package/dist/scopes.d.ts +6 -2
  47. package/dist/scopes.js +22 -4
  48. package/dist/seed.js +10 -16
  49. package/dist/segment-download.d.ts +4 -1
  50. package/dist/segment-download.js +22 -6
  51. package/dist/signed-url.d.ts +2 -2
  52. package/dist/signed-url.js +9 -2
  53. package/dist/sigv4.d.ts +8 -2
  54. package/dist/sigv4.js +8 -4
  55. package/dist/sqlite-blob-store.d.ts +1 -1
  56. package/dist/sqlite-blob-store.js +23 -22
  57. package/dist/sqlite-dialect.d.ts +164 -84
  58. package/dist/sqlite-dialect.js +1145 -150
  59. package/dist/sqlite-lease-store.js +13 -18
  60. package/dist/sqlite-storage.d.ts +9 -62
  61. package/dist/sqlite-storage.js +303 -1123
  62. package/dist/storage-query.d.ts +30 -1
  63. package/dist/storage-query.js +68 -0
  64. package/dist/storage.d.ts +185 -63
  65. package/dist/storage.js +15 -0
  66. package/package.json +2 -2
  67. package/src/admin.ts +99 -57
  68. package/src/blob-handlers.ts +91 -72
  69. package/src/blob-store.ts +0 -0
  70. package/src/bucket-blob-store.ts +75 -48
  71. package/src/bucket-segment-store.ts +20 -15
  72. package/src/content-encoding.ts +48 -16
  73. package/src/context.ts +33 -6
  74. package/src/d1-storage.ts +350 -1700
  75. package/src/errors.ts +16 -0
  76. package/src/events.ts +37 -18
  77. package/src/frame-bytes.ts +26 -6
  78. package/src/handler.ts +654 -261
  79. package/src/local-authority.ts +27 -14
  80. package/src/object-bucket.ts +2 -0
  81. package/src/operations-realtime.ts +85 -61
  82. package/src/operations.ts +44 -42
  83. package/src/pg-executor-pglite.ts +27 -23
  84. package/src/pg-executor.ts +5 -1
  85. package/src/postgres-storage.ts +557 -354
  86. package/src/pull.ts +406 -440
  87. package/src/push.ts +466 -220
  88. package/src/realtime.ts +447 -290
  89. package/src/relational-rows.ts +43 -121
  90. package/src/s3-blob-store.ts +23 -13
  91. package/src/s3-object-bucket.ts +20 -2
  92. package/src/schema.ts +31 -0
  93. package/src/scopes.ts +23 -3
  94. package/src/seed.ts +13 -20
  95. package/src/segment-download.ts +33 -7
  96. package/src/signed-url.ts +10 -4
  97. package/src/sigv4.ts +17 -4
  98. package/src/sqlite-blob-store.ts +27 -26
  99. package/src/sqlite-dialect.ts +1851 -199
  100. package/src/sqlite-lease-store.ts +12 -23
  101. package/src/sqlite-storage.ts +390 -1821
  102. package/src/storage-query.ts +102 -0
  103. package/src/storage.ts +215 -67
package/src/push.ts CHANGED
@@ -48,6 +48,8 @@ import { authorizeWrite, renderScopeValue, storedScopesForRow } from './scopes';
48
48
  import { decodeColumnVersions, encodeColumnVersions } from './relational-rows';
49
49
  import type {
50
50
  NewChange,
51
+ OutboxSequence,
52
+ OutboxSettlement,
51
53
  PreparedPushApply,
52
54
  StorageTransaction,
53
55
  StoredCommit,
@@ -99,6 +101,8 @@ interface StagedWrite {
99
101
  readonly opIndex: number;
100
102
  /** Final post-merge, post-scope-strip values (upsert only). */
101
103
  readonly values?: readonly RowValue[];
104
+ /** Columns the operation presents (upsert only, §6.11 present reference). */
105
+ readonly present?: readonly boolean[];
102
106
  /** Stored row observed immediately before the write; undefined on insert. */
103
107
  readonly stored: StoredRow | undefined;
104
108
  }
@@ -188,6 +192,26 @@ function conflictColumnsBitmap(
188
192
  interface BlobApplyContext {
189
193
  readonly store: BlobStore | undefined;
190
194
  readonly partition: string;
195
+ /** §6.6 per-commit existence answers, keyed by blobId. */
196
+ readonly presence: Map<string, boolean>;
197
+ }
198
+
199
+ /** §6.6: answer every not-yet-known blobId with one concurrent round of
200
+ * existence lookups and remember the answers for the rest of the commit. */
201
+ async function resolveBlobPresence(
202
+ blobCtx: BlobApplyContext,
203
+ blobIds: Iterable<string>,
204
+ ): Promise<void> {
205
+ const store = blobCtx.store;
206
+ if (store === undefined) return;
207
+ const unknown = [...new Set(blobIds)].filter(
208
+ (blobId) => !blobCtx.presence.has(blobId),
209
+ );
210
+ await Promise.all(
211
+ unknown.map(async (blobId) => {
212
+ blobCtx.presence.set(blobId, await store.has(blobCtx.partition, blobId));
213
+ }),
214
+ );
191
215
  }
192
216
 
193
217
  /**
@@ -322,6 +346,8 @@ async function applyOperation(
322
346
  validators: ValidatorRegistry | undefined,
323
347
  partition: string,
324
348
  actorId: string,
349
+ /** §2.2: the partition's delete floor, raised by this commit's deletes. */
350
+ deleteFloor: number,
325
351
  ): Promise<OperationOutcome> {
326
352
  const table = schema.tables.get(op.table);
327
353
  const wireTable = wireSchema.tables.get(op.table);
@@ -339,12 +365,17 @@ async function applyOperation(
339
365
  'scope resolution failed (§3.4 step 4)',
340
366
  );
341
367
  }
368
+ if (op.rowId.includes('\u0000')) {
369
+ // §6.1: no store sees a rowId holding U+0000; PostgreSQL refuses it.
370
+ return errorRecord(opIndex, 'sync.invalid_request', 'rowId holds U+0000');
371
+ }
342
372
  const stored = await tx.getRow(op.table, op.rowId);
343
373
 
344
374
  if (op.op === 'rescope') {
345
375
  // §2.2 explicit scope move. One stored row changes its scope values;
346
- // apply rewrites it in place, keeps `server_version`, and emits the
347
- // delete under the old scopes plus the upsert under the new ones.
376
+ // apply rewrites it in place, advances `server_version` by 1, and emits
377
+ // the delete under the old scopes plus the upsert under the new ones,
378
+ // both at the new version.
348
379
  if (stored === undefined) {
349
380
  return errorRecord(
350
381
  opIndex,
@@ -476,6 +507,22 @@ async function applyOperation(
476
507
  );
477
508
  if (blobCheck !== undefined) return blobCheck;
478
509
  const payload = encodeRow(table.columns, values);
510
+ // §2.2: the moved scope columns take the new version as their column
511
+ // version; every other column keeps its own (a row stored before column
512
+ // versions holds every column at its current version).
513
+ const movedVersion = stored.serverVersion + 1;
514
+ const storedVersions = decodeColumnVersions(stored.columnVersions);
515
+ const movedVersions = new Map<number, number>();
516
+ for (let i = 0; i < table.columns.length; i++) {
517
+ const version =
518
+ scopeIndexes.has(i) &&
519
+ !sameValue(values[i] ?? null, storedValues[i] ?? null)
520
+ ? movedVersion
521
+ : (storedVersions.get(i) ??
522
+ (stored.columnVersions === undefined ? stored.serverVersion : 1));
523
+ if (version > 1) movedVersions.set(i, version);
524
+ }
525
+ const columnVersions = encodeColumnVersions(movedVersions);
479
526
  // The move clears any tombstone from an earlier incarnation: the row is
480
527
  // live at the end of this commit, so the next move must not hit §5.2.
481
528
  await tx.clearTombstone(op.table, op.rowId);
@@ -483,12 +530,10 @@ async function applyOperation(
483
530
  op.table,
484
531
  {
485
532
  rowId: op.rowId,
486
- serverVersion: stored.serverVersion,
533
+ serverVersion: movedVersion,
487
534
  scopes: extracted.scopes,
488
535
  payload,
489
- ...(stored.columnVersions !== undefined
490
- ? { columnVersions: stored.columnVersions }
491
- : {}),
536
+ ...(columnVersions !== undefined ? { columnVersions } : {}),
492
537
  },
493
538
  { opIndex },
494
539
  );
@@ -500,13 +545,14 @@ async function applyOperation(
500
545
  table: op.table,
501
546
  rowId: op.rowId,
502
547
  op: 'delete',
548
+ rowVersion: movedVersion,
503
549
  scopes: stored.scopes,
504
550
  },
505
551
  {
506
552
  table: op.table,
507
553
  rowId: op.rowId,
508
554
  op: 'upsert',
509
- rowVersion: stored.serverVersion,
555
+ rowVersion: movedVersion,
510
556
  scopes: extracted.scopes,
511
557
  payload,
512
558
  },
@@ -529,7 +575,7 @@ async function applyOperation(
529
575
  row: toValidateRow(table.columns, values),
530
576
  stored: storedRow,
531
577
  storedServerVersion: stored.serverVersion,
532
- nextServerVersion: stored.serverVersion,
578
+ nextServerVersion: movedVersion,
533
579
  },
534
580
  ],
535
581
  staged: {
@@ -538,6 +584,7 @@ async function applyOperation(
538
584
  rowId: op.rowId,
539
585
  opIndex,
540
586
  values,
587
+ present: table.columns.map(() => true),
541
588
  stored,
542
589
  },
543
590
  };
@@ -608,6 +655,9 @@ async function applyOperation(
608
655
  table: op.table,
609
656
  rowId: op.rowId,
610
657
  op: 'delete',
658
+ // §2.2: a delete advances the version by 1; its tombstone records
659
+ // it and the delete floor rises to it.
660
+ rowVersion: stored.serverVersion + 1,
611
661
  scopes: stored.scopes,
612
662
  },
613
663
  ],
@@ -835,20 +885,41 @@ async function applyOperation(
835
885
  nextServerVersion: newVersion,
836
886
  },
837
887
  ],
838
- staged: { table, op: 'upsert', rowId: op.rowId, opIndex, values, stored },
888
+ staged: {
889
+ table,
890
+ op: 'upsert',
891
+ rowId: op.rowId,
892
+ opIndex,
893
+ values,
894
+ present,
895
+ stored,
896
+ },
839
897
  };
840
898
  }
841
899
 
842
- // Insert path: no stored row (§5.2).
900
+ // Insert path: no stored row (§5.2). The authorization row is the payload
901
+ // plus the primary key (§3.4 step 2); a missing or empty scope column value
902
+ // ⇒ deny.
843
903
  const values: RowValue[] = sparse.map((value) => value ?? null);
904
+ const extracted = storedScopesForRow(table, values);
905
+ const insert =
906
+ 'missing' in extracted
907
+ ? errorRecord(
908
+ opIndex,
909
+ 'sync.forbidden',
910
+ 'insert missing scope column value (§3.4)',
911
+ )
912
+ : authorizeWrite(table, extracted.scopes, resolved)
913
+ ? extracted
914
+ : errorRecord(
915
+ opIndex,
916
+ 'sync.forbidden',
917
+ 'insert denied by scope authorization (§3.4)',
918
+ );
844
919
  if (op.baseVersion !== undefined && op.baseVersion !== 0) {
845
920
  // Authorize the payload first so absence is not disclosed to actors
846
921
  // without the scope; then §6.2: baseVersion ≠ 0, row absent.
847
- const extractedFirst = storedScopesForRow(table, values);
848
- if (
849
- 'missing' in extractedFirst ||
850
- !authorizeWrite(table, extractedFirst.scopes, resolved)
851
- ) {
922
+ if ('kind' in insert) {
852
923
  return errorRecord(
853
924
  opIndex,
854
925
  'sync.forbidden',
@@ -861,17 +932,31 @@ async function applyOperation(
861
932
  'upsert with baseVersion targets an absent row (§6.2)',
862
933
  );
863
934
  }
864
- if (op.baseVersion === undefined) {
935
+ // §6.2: a payload that presents every scope column is authorized before
936
+ // the tombstone read, so the answer never discloses a deletion to an actor
937
+ // who could not write the row.
938
+ if (
939
+ op.baseVersion === undefined &&
940
+ 'kind' in insert &&
941
+ table.scopePatterns.every((pattern) => present[pattern.columnIndex])
942
+ ) {
943
+ return insert;
944
+ }
945
+ // §6.2: a tombstone whose stored scopes the actor fails reads as absent.
946
+ const tombstone = await tx.getTombstone(op.table, op.rowId);
947
+ const deleted =
948
+ tombstone?.scopes === undefined ||
949
+ authorizeWrite(table, tombstone.scopes, resolved)
950
+ ? tombstone
951
+ : undefined;
952
+ if (op.baseVersion === undefined && deleted !== undefined) {
865
953
  // §5.2: a delete within the retention horizon beats an unversioned
866
954
  // upsert; an explicit insert (baseVersion 0) recreates instead.
867
- const tombstoneSeq = await tx.getTombstoneSeq(op.table, op.rowId);
868
- if (tombstoneSeq !== undefined) {
869
- return errorRecord(
870
- opIndex,
871
- 'sync.row_deleted',
872
- 'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)',
873
- );
874
- }
955
+ return errorRecord(
956
+ opIndex,
957
+ 'sync.row_deleted',
958
+ 'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)',
959
+ );
875
960
  }
876
961
  if (presentCount < table.columns.length) {
877
962
  // §6.3: an insert requires every column present.
@@ -881,22 +966,7 @@ async function applyOperation(
881
966
  'a partial payload targets an absent row (§6.3)',
882
967
  );
883
968
  }
884
- const extracted = storedScopesForRow(table, values);
885
- if ('missing' in extracted) {
886
- // §3.4 step 2: a missing or empty scope column value ⇒ deny.
887
- return errorRecord(
888
- opIndex,
889
- 'sync.forbidden',
890
- `insert missing scope column value for ${JSON.stringify(extracted.missing)} (§3.4)`,
891
- );
892
- }
893
- if (!authorizeWrite(table, extracted.scopes, resolved)) {
894
- return errorRecord(
895
- opIndex,
896
- 'sync.forbidden',
897
- 'insert denied by scope authorization (§3.4)',
898
- );
899
- }
969
+ if ('kind' in insert) return insert;
900
970
  // §5.10.3: on insert a crdt column merges against the empty document
901
971
  // (stored = null) — normalizes the initial state through the merger.
902
972
  const insertMerge = await mergeCrdtColumns(
@@ -937,16 +1007,15 @@ async function applyOperation(
937
1007
  actorId,
938
1008
  );
939
1009
  if (insertReject !== undefined) return insertReject;
940
- // §5.2: an explicit insert recreates — the tombstone goes with it. An
941
- // unversioned insert already proved above, under the partition lock, that
942
- // no tombstone exists (this commit writes its own only in `appendCommit`).
943
- if (op.baseVersion !== undefined) {
944
- await tx.clearTombstone(op.table, op.rowId);
945
- }
1010
+ // §2.2, §6.2: every insert starts one above the delete floor, over a
1011
+ // tombstone or not; without a column-versions blob every column sits at
1012
+ // that version.
1013
+ if (tombstone !== undefined) await tx.clearTombstone(op.table, op.rowId);
1014
+ const serverVersion = deleteFloor + 1;
946
1015
  const newRow = {
947
1016
  rowId: op.rowId,
948
- serverVersion: 1,
949
- scopes: extracted.scopes,
1017
+ serverVersion,
1018
+ scopes: insert.scopes,
950
1019
  payload: insertPayload,
951
1020
  };
952
1021
  await tx.upsertRow(op.table, newRow, { opIndex });
@@ -957,8 +1026,8 @@ async function applyOperation(
957
1026
  table: op.table,
958
1027
  rowId: op.rowId,
959
1028
  op: 'upsert',
960
- rowVersion: 1,
961
- scopes: extracted.scopes,
1029
+ rowVersion: serverVersion,
1030
+ scopes: insert.scopes,
962
1031
  payload: insertPayload,
963
1032
  },
964
1033
  ],
@@ -970,7 +1039,7 @@ async function applyOperation(
970
1039
  rowId: op.rowId,
971
1040
  row: toValidateRow(table.columns, values),
972
1041
  stored: undefined,
973
- nextServerVersion: 1,
1042
+ nextServerVersion: serverVersion,
974
1043
  },
975
1044
  ],
976
1045
  staged: {
@@ -979,6 +1048,7 @@ async function applyOperation(
979
1048
  rowId: op.rowId,
980
1049
  opIndex,
981
1050
  values,
1051
+ present,
982
1052
  stored: undefined,
983
1053
  },
984
1054
  };
@@ -1009,14 +1079,15 @@ async function checkAndRecordBlobs(
1009
1079
  'row references a blob but the server has no blob store (§5.9.6)',
1010
1080
  );
1011
1081
  }
1012
- for (const blobId of blobIds) {
1013
- if (!(await blobCtx.store.has(blobCtx.partition, blobId))) {
1014
- return errorRecord(
1015
- opIndex,
1016
- 'blob.not_found',
1017
- `push references blob ${blobId} which has not been uploaded (§5.9.6)`,
1018
- );
1019
- }
1082
+ // Pushed blobIds were answered before the operation loop; a blobId the
1083
+ // row keeps from its stored values is answered here.
1084
+ await resolveBlobPresence(blobCtx, blobIds);
1085
+ if (blobIds.some((blobId) => blobCtx.presence.get(blobId) !== true)) {
1086
+ return errorRecord(
1087
+ opIndex,
1088
+ 'blob.not_found',
1089
+ 'push references a blob that has not been uploaded (§5.9.6)',
1090
+ );
1020
1091
  }
1021
1092
  }
1022
1093
  // Update the reference index for this row (empty set clears it, §5.9.4).
@@ -1295,16 +1366,29 @@ async function enforceReferences(
1295
1366
  const operations: ValidateCommitOperation[] = [];
1296
1367
  const originByIndex: number[] = [];
1297
1368
 
1369
+ // A reference column counts as present when any staged upsert of the row
1370
+ // presents it; its value is the row's final value (§6.11).
1298
1371
  const finalByRow = new Map<
1299
1372
  string,
1300
- { readonly first: StagedWrite; readonly last: StagedWrite }
1373
+ {
1374
+ readonly first: StagedWrite;
1375
+ readonly last: StagedWrite;
1376
+ readonly presentReferences: ReadonlySet<number>;
1377
+ }
1301
1378
  >();
1302
1379
  for (const write of staged) {
1303
1380
  const rowKey = `${write.table.name}\u0000${write.rowId}`;
1304
1381
  const existing = finalByRow.get(rowKey);
1382
+ const presentReferences = new Set(existing?.presentReferences);
1383
+ for (const reference of write.table.references) {
1384
+ if (write.present?.[reference.columnIndex] === true) {
1385
+ presentReferences.add(reference.columnIndex);
1386
+ }
1387
+ }
1305
1388
  finalByRow.set(rowKey, {
1306
1389
  first: existing?.first ?? write,
1307
1390
  last: write,
1391
+ presentReferences,
1308
1392
  });
1309
1393
  }
1310
1394
 
@@ -1313,9 +1397,10 @@ async function enforceReferences(
1313
1397
  // combatants, its chronicle) reads that parent once. Postgres binds the
1314
1398
  // ids as one array per table; SQLite and D1 answer with one lookup per id.
1315
1399
  const parentIds = new Map<string, Set<string>>();
1316
- for (const { last: write } of finalByRow.values()) {
1400
+ for (const { last: write, presentReferences } of finalByRow.values()) {
1317
1401
  if (write.op !== 'upsert' || write.values === undefined) continue;
1318
1402
  for (const reference of write.table.references) {
1403
+ if (!presentReferences.has(reference.columnIndex)) continue;
1319
1404
  const value = write.values[reference.columnIndex];
1320
1405
  if (value === null || value === undefined) continue;
1321
1406
  const parentRowId = renderScopeValue(value);
@@ -1334,14 +1419,23 @@ async function enforceReferences(
1334
1419
  );
1335
1420
  }
1336
1421
 
1337
- for (const { last: write } of finalByRow.values()) {
1422
+ for (const { last: write, presentReferences } of finalByRow.values()) {
1338
1423
  if (write.op !== 'upsert' || write.values === undefined) continue;
1339
1424
  for (const reference of write.table.references) {
1425
+ if (!presentReferences.has(reference.columnIndex)) continue;
1340
1426
  const value = write.values[reference.columnIndex];
1341
1427
  if (value === null || value === undefined) continue;
1342
1428
  const parentRowId = renderScopeValue(value);
1343
1429
  if (parentRowId === undefined) continue;
1344
- if (parents.get(reference.parentTable)?.has(parentRowId) === true) {
1430
+ // A parent the actor cannot authorize (§3.4, on its stored scopes) is
1431
+ // an absent parent: the rejection below is identical either way.
1432
+ const parent = parents.get(reference.parentTable)?.get(parentRowId);
1433
+ const parentTable = schema.tables.get(reference.parentTable);
1434
+ if (
1435
+ parent !== undefined &&
1436
+ parentTable !== undefined &&
1437
+ authorizeWrite(parentTable, parent.scopes, resolved)
1438
+ ) {
1345
1439
  continue;
1346
1440
  }
1347
1441
  return {
@@ -1409,22 +1503,16 @@ async function enforceReferences(
1409
1503
  'storage transaction does not support declared-reference enforcement (scanRowsByIndex)',
1410
1504
  );
1411
1505
  }
1412
- const children: StoredRow[] = [];
1413
- let afterRowId: string | null = null;
1414
- for (;;) {
1415
- const page = await tx.scanRowsByIndex({
1506
+ if (reference.onDelete === 'RESTRICT') {
1507
+ // One remaining child decides RESTRICT.
1508
+ const blocking = await tx.scanRowsByIndex({
1416
1509
  table: reference.table,
1417
1510
  index: reference.index,
1418
1511
  values: [parentKeyValue],
1419
- afterRowId,
1420
- limit: 1_000,
1512
+ afterRowId: null,
1513
+ limit: 1,
1421
1514
  });
1422
- children.push(...page);
1423
- if (page.length < 1_000) break;
1424
- afterRowId = page[page.length - 1]?.rowId ?? null;
1425
- }
1426
- if (children.length === 0) continue;
1427
- if (reference.onDelete === 'RESTRICT') {
1515
+ if (blocking.length === 0) continue;
1428
1516
  return {
1429
1517
  kind: 'terminate',
1430
1518
  record: referenceRejection(
@@ -1443,141 +1531,161 @@ async function enforceReferences(
1443
1531
  `unreachable: reference child table ${reference.table} is not compiled`,
1444
1532
  );
1445
1533
  }
1446
- for (const childRow of children) {
1447
- if (!authorizeWrite(child, childRow.scopes, resolved)) {
1448
- return errorRecord(
1449
- item.originOpIndex,
1450
- 'sync.forbidden',
1451
- 'cascade denied by scope authorization (§3.4)',
1452
- );
1453
- }
1454
- if (reference.onDelete === 'CASCADE') {
1455
- if (
1456
- !schedule(
1457
- child,
1458
- childRow.rowId,
1459
- item.originOpIndex,
1460
- scheduled,
1461
- queue,
1462
- )
1463
- ) {
1464
- continue;
1465
- }
1466
- } else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
1467
- continue;
1468
- }
1469
- if (originByIndex.length >= cascadeLimit) {
1470
- return {
1471
- kind: 'terminate',
1472
- record: referenceRejection(
1534
+ // Each page reads no more than the remaining cascade budget plus one
1535
+ // child, so a cascade past the cap stops after one extra row. A page is
1536
+ // applied before the next is read; keyset pagination by rowId is
1537
+ // unaffected by deleting or nulling rows already read.
1538
+ let afterRowId: string | null = null;
1539
+ for (;;) {
1540
+ const limit = Math.min(1_000, cascadeLimit - originByIndex.length + 1);
1541
+ const page = await tx.scanRowsByIndex({
1542
+ table: reference.table,
1543
+ index: reference.index,
1544
+ values: [parentKeyValue],
1545
+ afterRowId,
1546
+ limit,
1547
+ });
1548
+ for (const childRow of page) {
1549
+ if (!authorizeWrite(child, childRow.scopes, resolved)) {
1550
+ return errorRecord(
1473
1551
  item.originOpIndex,
1474
- 'declared-reference cascade exceeded the per-commit operation cap (§6.11)',
1475
- { reason: 'cascade_limit' },
1476
- ),
1477
- };
1478
- }
1479
- const syntheticIndex = clientOpCount + originByIndex.length;
1480
- const storedValues = decodeRow(child.columns, childRow.payload);
1481
- if (reference.onDelete === 'CASCADE') {
1482
- const reject = await runValidator(
1483
- validators,
1484
- child,
1485
- 'delete',
1486
- childRow.rowId,
1487
- undefined,
1488
- storedValues,
1489
- item.originOpIndex,
1490
- tx,
1491
- partition,
1492
- actorId,
1493
- );
1494
- if (reject !== undefined) {
1495
- return { kind: 'terminate', record: reject.record };
1552
+ 'sync.forbidden',
1553
+ 'cascade denied by scope authorization (§3.4)',
1554
+ );
1496
1555
  }
1497
- await tx.deleteRow(child.name, childRow.rowId);
1498
- changes.push({
1499
- table: child.name,
1500
- rowId: childRow.rowId,
1501
- op: 'delete',
1502
- scopes: childRow.scopes,
1503
- });
1504
- operations.push({
1505
- opIndex: syntheticIndex,
1506
- op: 'delete',
1507
- table: child.name,
1508
- rowId: childRow.rowId,
1509
- row: undefined,
1510
- stored: toValidateRow(child.columns, storedValues),
1511
- storedServerVersion: childRow.serverVersion,
1512
- });
1513
- } else {
1514
- const values = [...storedValues];
1515
- values[reference.columnIndex] = null;
1516
- const reject = await runValidator(
1517
- validators,
1518
- child,
1519
- 'upsert',
1520
- childRow.rowId,
1521
- values,
1522
- storedValues,
1523
- item.originOpIndex,
1524
- tx,
1525
- partition,
1526
- actorId,
1527
- );
1528
- if (reject !== undefined) {
1529
- return { kind: 'terminate', record: reject.record };
1556
+ if (reference.onDelete === 'CASCADE') {
1557
+ if (
1558
+ !schedule(
1559
+ child,
1560
+ childRow.rowId,
1561
+ item.originOpIndex,
1562
+ scheduled,
1563
+ queue,
1564
+ )
1565
+ ) {
1566
+ continue;
1567
+ }
1568
+ } else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
1569
+ continue;
1530
1570
  }
1531
- const nextServerVersion = childRow.serverVersion + 1;
1532
- // §6.2: the SET NULL upsert writes only the reference column, so
1533
- // only that column's version moves.
1534
- const storedVersions = decodeColumnVersions(childRow.columnVersions);
1535
- const nextVersions = new Map<number, number>();
1536
- for (let i = 0; i < child.columns.length; i++) {
1537
- const version =
1538
- i === reference.columnIndex
1539
- ? nextServerVersion
1540
- : (storedVersions.get(i) ??
1541
- (childRow.columnVersions === undefined
1542
- ? childRow.serverVersion
1543
- : 1));
1544
- if (version > 1) nextVersions.set(i, version);
1571
+ if (originByIndex.length >= cascadeLimit) {
1572
+ return {
1573
+ kind: 'terminate',
1574
+ record: referenceRejection(
1575
+ item.originOpIndex,
1576
+ 'declared-reference cascade exceeded the per-commit operation cap (§6.11)',
1577
+ { reason: 'cascade_limit' },
1578
+ ),
1579
+ };
1545
1580
  }
1546
- const payload = encodeRow(child.columns, values);
1547
- const cascadeVersions = encodeColumnVersions(nextVersions);
1548
- await tx.upsertRow(
1549
- child.name,
1550
- {
1581
+ const syntheticIndex = clientOpCount + originByIndex.length;
1582
+ const storedValues = decodeRow(child.columns, childRow.payload);
1583
+ if (reference.onDelete === 'CASCADE') {
1584
+ const reject = await runValidator(
1585
+ validators,
1586
+ child,
1587
+ 'delete',
1588
+ childRow.rowId,
1589
+ undefined,
1590
+ storedValues,
1591
+ item.originOpIndex,
1592
+ tx,
1593
+ partition,
1594
+ actorId,
1595
+ );
1596
+ if (reject !== undefined) {
1597
+ return { kind: 'terminate', record: reject.record };
1598
+ }
1599
+ await tx.deleteRow(child.name, childRow.rowId);
1600
+ changes.push({
1601
+ table: child.name,
1551
1602
  rowId: childRow.rowId,
1552
- serverVersion: nextServerVersion,
1603
+ op: 'delete',
1604
+ rowVersion: childRow.serverVersion + 1,
1605
+ scopes: childRow.scopes,
1606
+ });
1607
+ operations.push({
1608
+ opIndex: syntheticIndex,
1609
+ op: 'delete',
1610
+ table: child.name,
1611
+ rowId: childRow.rowId,
1612
+ row: undefined,
1613
+ stored: toValidateRow(child.columns, storedValues),
1614
+ storedServerVersion: childRow.serverVersion,
1615
+ });
1616
+ } else {
1617
+ const values = [...storedValues];
1618
+ values[reference.columnIndex] = null;
1619
+ const reject = await runValidator(
1620
+ validators,
1621
+ child,
1622
+ 'upsert',
1623
+ childRow.rowId,
1624
+ values,
1625
+ storedValues,
1626
+ item.originOpIndex,
1627
+ tx,
1628
+ partition,
1629
+ actorId,
1630
+ );
1631
+ if (reject !== undefined) {
1632
+ return { kind: 'terminate', record: reject.record };
1633
+ }
1634
+ const nextServerVersion = childRow.serverVersion + 1;
1635
+ // §6.2: the SET NULL upsert writes only the reference column, so
1636
+ // only that column's version moves.
1637
+ const storedVersions = decodeColumnVersions(
1638
+ childRow.columnVersions,
1639
+ );
1640
+ const nextVersions = new Map<number, number>();
1641
+ for (let i = 0; i < child.columns.length; i++) {
1642
+ const version =
1643
+ i === reference.columnIndex
1644
+ ? nextServerVersion
1645
+ : (storedVersions.get(i) ??
1646
+ (childRow.columnVersions === undefined
1647
+ ? childRow.serverVersion
1648
+ : 1));
1649
+ if (version > 1) nextVersions.set(i, version);
1650
+ }
1651
+ const payload = encodeRow(child.columns, values);
1652
+ const cascadeVersions = encodeColumnVersions(nextVersions);
1653
+ await tx.upsertRow(
1654
+ child.name,
1655
+ {
1656
+ rowId: childRow.rowId,
1657
+ serverVersion: nextServerVersion,
1658
+ scopes: childRow.scopes,
1659
+ payload,
1660
+ ...(cascadeVersions !== undefined
1661
+ ? { columnVersions: cascadeVersions }
1662
+ : {}),
1663
+ },
1664
+ { opIndex: syntheticIndex },
1665
+ );
1666
+ changes.push({
1667
+ table: child.name,
1668
+ rowId: childRow.rowId,
1669
+ op: 'upsert',
1670
+ rowVersion: nextServerVersion,
1553
1671
  scopes: childRow.scopes,
1554
1672
  payload,
1555
- ...(cascadeVersions !== undefined
1556
- ? { columnVersions: cascadeVersions }
1557
- : {}),
1558
- },
1559
- { opIndex: syntheticIndex },
1560
- );
1561
- changes.push({
1562
- table: child.name,
1563
- rowId: childRow.rowId,
1564
- op: 'upsert',
1565
- rowVersion: nextServerVersion,
1566
- scopes: childRow.scopes,
1567
- payload,
1568
- });
1569
- operations.push({
1570
- opIndex: syntheticIndex,
1571
- op: 'upsert',
1572
- table: child.name,
1573
- rowId: childRow.rowId,
1574
- row: toValidateRow(child.columns, values),
1575
- stored: toValidateRow(child.columns, storedValues),
1576
- storedServerVersion: childRow.serverVersion,
1577
- nextServerVersion,
1578
- });
1673
+ });
1674
+ operations.push({
1675
+ opIndex: syntheticIndex,
1676
+ op: 'upsert',
1677
+ table: child.name,
1678
+ rowId: childRow.rowId,
1679
+ row: toValidateRow(child.columns, values),
1680
+ stored: toValidateRow(child.columns, storedValues),
1681
+ storedServerVersion: childRow.serverVersion,
1682
+ nextServerVersion,
1683
+ });
1684
+ }
1685
+ originByIndex.push(item.originOpIndex);
1579
1686
  }
1580
- originByIndex.push(item.originOpIndex);
1687
+ if (page.length < limit) break;
1688
+ afterRowId = page[page.length - 1]?.rowId ?? null;
1581
1689
  }
1582
1690
  }
1583
1691
  }
@@ -1608,16 +1716,28 @@ export interface ProcessedPushCommit {
1608
1716
  readonly replayed: boolean;
1609
1717
  readonly recordedAtMs?: number;
1610
1718
  readonly cacheIdentity?: string;
1719
+ /** The outbox watermark `W` the commit's settlement read (§6.3.2). */
1720
+ readonly watermark?: number;
1721
+ }
1722
+
1723
+ /** A pushed commit's outbox position and the request's watermark (§6.3.2). */
1724
+ export interface PushOutbox extends OutboxSequence {
1725
+ /** `W` after the request's first commit. */
1726
+ readonly watermark: number;
1727
+ /** Present with the request's first commit: steps 1 and 2 run with it. */
1728
+ readonly settlement?: OutboxSettlement;
1611
1729
  }
1612
1730
 
1613
1731
  function processedPushCommit(
1614
1732
  clientCommitId: string,
1615
1733
  stored: StoredPushResult,
1616
1734
  replayed: boolean,
1735
+ watermark?: number,
1617
1736
  ): ProcessedPushCommit {
1618
1737
  return {
1619
1738
  frame: resultFrame(clientCommitId, stored, replayed),
1620
1739
  replayed,
1740
+ ...(watermark !== undefined ? { watermark } : {}),
1621
1741
  ...(stored.recordedAtMs !== undefined
1622
1742
  ? { recordedAtMs: stored.recordedAtMs }
1623
1743
  : {}),
@@ -1658,6 +1778,24 @@ function idempotencyCacheMissFrame(
1658
1778
  };
1659
1779
  }
1660
1780
 
1781
+ /** §6.3: the unpersisted refusal of a commit below its outbox's watermark. */
1782
+ function commitExpiredFrame(clientCommitId: string): PushResultFrame {
1783
+ return {
1784
+ type: 'PUSH_RESULT',
1785
+ clientCommitId,
1786
+ status: 'rejected',
1787
+ results: [
1788
+ {
1789
+ opIndex: 0,
1790
+ status: 'error',
1791
+ code: 'sync.commit_expired',
1792
+ message: 'the commit lies below its outbox watermark (§6.3.2)',
1793
+ retryable: false,
1794
+ },
1795
+ ],
1796
+ };
1797
+ }
1798
+
1661
1799
  export interface AppliedCommitEvent {
1662
1800
  readonly commit: StoredCommit;
1663
1801
  }
@@ -1690,13 +1828,16 @@ export async function processPushCommitWithTrace(
1690
1828
  clientId: string,
1691
1829
  frame: PushCommitFrame,
1692
1830
  wireSchema: CompiledSchema = schema,
1831
+ outbox?: PushOutbox,
1693
1832
  ): Promise<ProcessedPushCommit> {
1694
1833
  // The frame names its rows before the transaction opens, so the admission
1695
1834
  // read carries the commit's read-ahead and the apply issues no separate
1696
1835
  // prefetch statement.
1697
1836
  const targets = new Map<string, string[]>();
1698
1837
  for (const op of frame.operations) {
1699
- if (!schema.tables.has(op.table)) continue;
1838
+ // A rowId holding U+0000 is rejected per operation (§6.1) before any
1839
+ // store reads it.
1840
+ if (!schema.tables.has(op.table) || op.rowId.includes('\u0000')) continue;
1700
1841
  const ids = targets.get(op.table) ?? [];
1701
1842
  ids.push(op.rowId);
1702
1843
  targets.set(op.table, ids);
@@ -1712,6 +1853,7 @@ export async function processPushCommitWithTrace(
1712
1853
  targets.size === 0
1713
1854
  ? undefined
1714
1855
  : [...targets].map(([table, rowIds]) => ({ table, rowIds })),
1856
+ outbox,
1715
1857
  );
1716
1858
  }
1717
1859
 
@@ -1734,15 +1876,27 @@ export async function processPushOperationsWithTrace(
1734
1876
  readonly table: string;
1735
1877
  readonly rowIds: readonly string[];
1736
1878
  }[],
1879
+ outbox?: PushOutbox,
1737
1880
  ): Promise<ProcessedPushCommit> {
1738
1881
  const { partition } = ctx;
1739
1882
  const createdAtMs = clockOf(ctx)();
1740
- const blobCtx: BlobApplyContext = { store: ctx.blobs, partition };
1883
+ const blobCtx: BlobApplyContext = {
1884
+ store: ctx.blobs,
1885
+ partition,
1886
+ presence: new Map(),
1887
+ };
1741
1888
  const crdtMergers = ctx.crdtMergers;
1742
1889
  const validators = ctx.validators;
1743
1890
  const commitValidator = ctx.commitValidator;
1744
1891
  const reactionPlanner = ctx.reactionPlanner;
1745
1892
  const tx = await ctx.storage.begin(partition);
1893
+ // §6.3.2: the settlement's writes commit whatever the commit's outcome.
1894
+ const release = (): Promise<void> =>
1895
+ outbox?.settlement !== undefined ? tx.commit() : tx.rollback();
1896
+ const sequence =
1897
+ outbox === undefined
1898
+ ? undefined
1899
+ : { outboxId: outbox.outboxId, clientSeq: outbox.clientSeq };
1746
1900
  try {
1747
1901
  if (reactionPlanner !== undefined && tx.enqueueReactions === undefined) {
1748
1902
  throw new Error(
@@ -1762,6 +1916,7 @@ export async function processPushOperationsWithTrace(
1762
1916
  clientCommitId,
1763
1917
  schema.version,
1764
1918
  prefetchTargets,
1919
+ outbox?.settlement,
1765
1920
  );
1766
1921
  } catch (error) {
1767
1922
  if (
@@ -1770,7 +1925,7 @@ export async function processPushOperationsWithTrace(
1770
1925
  ) {
1771
1926
  // §6.3: answer the retryable cache-miss for this commit rather than
1772
1927
  // re-applying. Not persisted — a retry may find a readable record.
1773
- await tx.rollback();
1928
+ await release();
1774
1929
  return {
1775
1930
  frame: idempotencyCacheMissFrame(clientCommitId, error),
1776
1931
  replayed: false,
@@ -1787,9 +1942,10 @@ export async function processPushOperationsWithTrace(
1787
1942
  ctx.checkpoints,
1788
1943
  );
1789
1944
  if (refusal !== undefined) {
1790
- await tx.rollback();
1945
+ await release();
1791
1946
  throw serveNotReadyError(refusal);
1792
1947
  }
1948
+ const watermark = prepared.watermark ?? outbox?.watermark;
1793
1949
  if (prepared.persisted !== undefined) {
1794
1950
  const conflict = prepared.persisted.results.find(
1795
1951
  (result) => result.status === 'conflict',
@@ -1803,7 +1959,7 @@ export async function processPushOperationsWithTrace(
1803
1959
  table === undefined ||
1804
1960
  !authorizeWrite(table, authorization.scopes, resolved))
1805
1961
  ) {
1806
- await tx.rollback();
1962
+ await release();
1807
1963
  return processedPushCommit(
1808
1964
  clientCommitId,
1809
1965
  {
@@ -1819,10 +1975,29 @@ export async function processPushOperationsWithTrace(
1819
1975
  ],
1820
1976
  },
1821
1977
  true,
1978
+ watermark,
1822
1979
  );
1823
1980
  }
1824
- await tx.rollback();
1825
- return processedPushCommit(clientCommitId, prepared.persisted, true);
1981
+ await release();
1982
+ return processedPushCommit(
1983
+ clientCommitId,
1984
+ prepared.persisted,
1985
+ true,
1986
+ watermark,
1987
+ );
1988
+ }
1989
+ // §6.3.2 step 3: below the watermark without a retained result.
1990
+ if (
1991
+ outbox !== undefined &&
1992
+ watermark !== undefined &&
1993
+ outbox.clientSeq < watermark
1994
+ ) {
1995
+ await release();
1996
+ return {
1997
+ frame: commitExpiredFrame(clientCommitId),
1998
+ replayed: false,
1999
+ watermark,
2000
+ };
1826
2001
  }
1827
2002
  const results: PushOperationResult[] = [];
1828
2003
  const changes: NewChange[] = [];
@@ -1833,7 +2008,8 @@ export async function processPushOperationsWithTrace(
1833
2008
  if (tx.prefetchRows !== undefined) {
1834
2009
  const targets = new Map<string, string[]>();
1835
2010
  for (const op of operations) {
1836
- if (!schema.tables.has(op.table)) continue;
2011
+ if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
2012
+ continue;
1837
2013
  const ids = targets.get(op.table) ?? [];
1838
2014
  ids.push(op.rowId);
1839
2015
  targets.set(op.table, ids);
@@ -1844,6 +2020,45 @@ export async function processPushOperationsWithTrace(
1844
2020
  );
1845
2021
  }
1846
2022
  }
2023
+ // §6.6: the blobIds the commit pushes are checked once, concurrently,
2024
+ // before the operation loop. A payload that fails decode is skipped here
2025
+ // and rejected by its operation.
2026
+ const pushedBlobIds: string[] = [];
2027
+ for (const op of operations) {
2028
+ const table = schema.tables.get(op.table);
2029
+ const wireTable = wireSchema.tables.get(op.table);
2030
+ if (
2031
+ op.payload === undefined ||
2032
+ table === undefined ||
2033
+ wireTable === undefined ||
2034
+ table.blobRefColumnIndices.length === 0
2035
+ ) {
2036
+ continue;
2037
+ }
2038
+ let sparse: SparseRowValue[];
2039
+ try {
2040
+ sparse = decodeSparseRow(
2041
+ wireTable.columns,
2042
+ wireTable.primaryKeyIndex,
2043
+ op.payload,
2044
+ );
2045
+ } catch {
2046
+ continue;
2047
+ }
2048
+ for (const index of table.blobRefColumnIndices) {
2049
+ const value = sparse[index];
2050
+ if (typeof value === 'string') {
2051
+ pushedBlobIds.push(parseBlobRef(value).blobId);
2052
+ }
2053
+ }
2054
+ }
2055
+ await resolveBlobPresence(blobCtx, pushedBlobIds);
2056
+ // §6.4: the row each touched (table, rowId) held before this commit, so a
2057
+ // conflict reports the row as stored once the commit rolls back.
2058
+ const storedBeforeCommit = new Map<string, StoredRow | undefined>();
2059
+ // §2.2: inserts start above the floor, which this commit's deletes raise
2060
+ // as they stage.
2061
+ let deleteFloor = prepared.deleteFloor;
1847
2062
  for (let opIndex = 0; opIndex < operations.length; opIndex++) {
1848
2063
  const op = operations[opIndex];
1849
2064
  if (op === undefined) continue;
@@ -1859,11 +2074,21 @@ export async function processPushOperationsWithTrace(
1859
2074
  validators,
1860
2075
  partition,
1861
2076
  ctx.actorId,
2077
+ deleteFloor,
1862
2078
  );
1863
2079
  if (outcome.kind === 'terminate') {
1864
2080
  terminated = outcome.record;
1865
2081
  break;
1866
2082
  }
2083
+ for (const change of outcome.changes) {
2084
+ if (change.op === 'delete') {
2085
+ deleteFloor = Math.max(deleteFloor, change.rowVersion);
2086
+ }
2087
+ }
2088
+ const rowKey = `${op.table}\u0000${op.rowId}`;
2089
+ if (!storedBeforeCommit.has(rowKey)) {
2090
+ storedBeforeCommit.set(rowKey, outcome.staged.stored);
2091
+ }
1867
2092
  results.push({ opIndex, status: 'applied' });
1868
2093
  validatedOperations.push(...outcome.operations);
1869
2094
  stagedWrites.push(outcome.staged);
@@ -1941,19 +2166,37 @@ export async function processPushOperationsWithTrace(
1941
2166
  terminated.status === 'conflict'
1942
2167
  ? operations[terminated.opIndex]
1943
2168
  : undefined;
1944
- const conflictRow =
2169
+ // §6.4: a conflict only arises in the operation loop, so a row no
2170
+ // earlier operation touched still reads as stored before the commit.
2171
+ const conflictKey =
1945
2172
  conflictOperation === undefined
1946
2173
  ? undefined
1947
- : await tx.getRow(conflictOperation.table, conflictOperation.rowId);
1948
- if (
1949
- terminated.status === 'conflict' &&
1950
- (conflictOperation === undefined || conflictRow === undefined)
1951
- ) {
1952
- throw new Error('conflict snapshot has no authorization row');
2174
+ : `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
2175
+ const conflictRow =
2176
+ conflictOperation === undefined || conflictKey === undefined
2177
+ ? undefined
2178
+ : storedBeforeCommit.has(conflictKey)
2179
+ ? storedBeforeCommit.get(conflictKey)
2180
+ : await tx.getRow(conflictOperation.table, conflictOperation.rowId);
2181
+ if (terminated.status === 'conflict') {
2182
+ terminated =
2183
+ conflictRow === undefined
2184
+ ? errorRecord(
2185
+ terminated.opIndex,
2186
+ 'sync.row_missing',
2187
+ 'the conflicting row exists only through an earlier operation of the same commit (§6.4)',
2188
+ ).record
2189
+ : {
2190
+ ...terminated,
2191
+ serverVersion: conflictRow.serverVersion,
2192
+ serverRow: conflictRow.payload,
2193
+ };
1953
2194
  }
1954
2195
  const stored = newStoredPushResult(createdAtMs, {
1955
2196
  status: 'rejected',
1956
- ...(conflictOperation !== undefined && conflictRow !== undefined
2197
+ ...(terminated.status === 'conflict' &&
2198
+ conflictOperation !== undefined &&
2199
+ conflictRow !== undefined
1957
2200
  ? {
1958
2201
  conflictAuthorization: {
1959
2202
  table: conflictOperation.table,
@@ -1971,11 +2214,13 @@ export async function processPushOperationsWithTrace(
1971
2214
  clientId,
1972
2215
  clientCommitId,
1973
2216
  stored,
2217
+ sequence,
1974
2218
  );
1975
2219
  return processedPushCommit(
1976
2220
  clientCommitId,
1977
2221
  canonical,
1978
2222
  canonical.cacheIdentity !== stored.cacheIdentity,
2223
+ watermark,
1979
2224
  );
1980
2225
  }
1981
2226
 
@@ -2004,7 +2249,7 @@ export async function processPushOperationsWithTrace(
2004
2249
  commitSeq,
2005
2250
  results,
2006
2251
  });
2007
- await tx.putPushResult(clientId, clientCommitId, stored);
2252
+ await tx.putPushResult(clientId, clientCommitId, stored, sequence);
2008
2253
  await tx.commit();
2009
2254
  if (ctx.events !== undefined) {
2010
2255
  const atMs = clockOf(ctx)();
@@ -2038,7 +2283,7 @@ export async function processPushOperationsWithTrace(
2038
2283
  changes,
2039
2284
  });
2040
2285
  }
2041
- return processedPushCommit(clientCommitId, stored, false);
2286
+ return processedPushCommit(clientCommitId, stored, false, watermark);
2042
2287
  } catch (error) {
2043
2288
  if (error instanceof StorageConstraintError) {
2044
2289
  const stored = newStoredPushResult(createdAtMs, {
@@ -2058,6 +2303,7 @@ export async function processPushOperationsWithTrace(
2058
2303
  clientId,
2059
2304
  clientCommitId,
2060
2305
  stored,
2306
+ sequence,
2061
2307
  );
2062
2308
  return processedPushCommit(
2063
2309
  clientCommitId,