@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.
@@ -204,14 +204,14 @@ export declare abstract class SqliteFamilyTransaction {
204
204
  appendCommit(commit: NewCommit): Promise<number>;
205
205
  putPushResult(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
206
206
  /**
207
- * The §9 settlement and §6.3.2 steps 1 and 2 for one request, then the
208
- * idempotency read of its first commit. The reads run before the writes,
209
- * so D1, which buffers the writes until commit, answers exactly as SQLite:
210
- * the first commit's result counts as deleted when the writes delete it.
207
+ * The §2.3 records of a request's commits and, with `outbox`, the §9
208
+ * settlement and §6.3.2 steps 1 and 2. The read runs before the writes, so
209
+ * D1, which buffers the writes, answers exactly as SQLite: a result the
210
+ * writes delete counts as absent.
211
211
  */
212
- protected settleOutbox(clientId: string, clientCommitId: string, outbox: OutboxSettlement): Promise<{
213
- readonly watermark: number;
214
- readonly persisted: StoredPushResult | undefined;
212
+ protected admitPushRequest(clientId: string, clientCommitIds: readonly string[], outbox: OutboxSettlement | undefined): Promise<{
213
+ readonly watermark?: number;
214
+ readonly persisted: (clientCommitId: string) => StoredPushResult | undefined;
215
215
  }>;
216
216
  enqueueReactions(reactions: readonly NewReaction[]): Promise<void>;
217
217
  }
@@ -1328,27 +1328,40 @@ export class SqliteFamilyTransaction {
1328
1328
  ]);
1329
1329
  }
1330
1330
  /**
1331
- * The §9 settlement and §6.3.2 steps 1 and 2 for one request, then the
1332
- * idempotency read of its first commit. The reads run before the writes,
1333
- * so D1, which buffers the writes until commit, answers exactly as SQLite:
1334
- * the first commit's result counts as deleted when the writes delete it.
1331
+ * The §2.3 records of a request's commits and, with `outbox`, the §9
1332
+ * settlement and §6.3.2 steps 1 and 2. The read runs before the writes, so
1333
+ * D1, which buffers the writes, answers exactly as SQLite: a result the
1334
+ * writes delete counts as absent.
1335
1335
  */
1336
- async settleOutbox(clientId, clientCommitId, outbox) {
1336
+ async admitPushRequest(clientId, clientCommitIds, outbox) {
1337
+ const p = this.partition;
1338
+ const ids = JSON.stringify(clientCommitIds);
1339
+ const persistedOf = (records) => (clientCommitId) => {
1340
+ const record = records.get(clientCommitId);
1341
+ return record === undefined ? undefined : deserializePushResult(record);
1342
+ };
1343
+ if (outbox === undefined) {
1344
+ const rows = await this.all(`SELECT client_commit_id, result FROM sync_push_results
1345
+ WHERE partition=? AND client_id=? AND client_commit_id IN (SELECT value FROM json_each(?))`, [p, clientId, ids]);
1346
+ return {
1347
+ persisted: persistedOf(new Map(rows.map((row) => [row.client_commit_id, row.result]))),
1348
+ };
1349
+ }
1337
1350
  const first = outbox.commits[0];
1338
1351
  const last = outbox.commits.at(-1);
1339
- if (first === undefined ||
1340
- last === undefined ||
1341
- first.clientCommitId !== clientCommitId) {
1342
- throw new Error("outbox settlement rides the request's first commit");
1352
+ if (first === undefined || last === undefined) {
1353
+ throw new Error('outbox settlement needs the request commits');
1343
1354
  }
1344
- const p = this.partition;
1345
- const [state] = await this.all(`SELECT (SELECT through_seq FROM sync_push_watermarks
1355
+ const rows = await this.all(`SELECT (SELECT through_seq FROM sync_push_watermarks
1346
1356
  WHERE partition=?1 AND client_id=?2 AND outbox_id=?3) AS stored,
1347
1357
  EXISTS (SELECT 1 FROM sync_push_results WHERE partition=?1
1348
1358
  AND client_id=?2 AND client_commit_id=?5 AND outbox_id IS NULL) AS last_sequenceless,
1349
- r.result AS result, r.outbox_id AS outbox_id, r.client_seq AS client_seq
1359
+ r.client_commit_id AS client_commit_id, r.result AS result,
1360
+ r.outbox_id AS outbox_id, r.client_seq AS client_seq
1350
1361
  FROM (SELECT 1) LEFT JOIN sync_push_results AS r
1351
- ON r.partition=?1 AND r.client_id=?2 AND r.client_commit_id=?4`, [p, clientId, outbox.outboxId, first.clientCommitId, last.clientCommitId]);
1362
+ ON r.partition=?1 AND r.client_id=?2
1363
+ AND r.client_commit_id IN (SELECT value FROM json_each(?4))`, [p, clientId, outbox.outboxId, ids, last.clientCommitId]);
1364
+ const state = rows[0];
1352
1365
  const watermark = Math.max(state?.stored ?? 0, first.clientSeq);
1353
1366
  const commits = JSON.stringify(outbox.commits.map((commit) => [commit.clientCommitId, commit.clientSeq]));
1354
1367
  // §9 (b), then (a): with no pre-upgrade result on the request's last
@@ -1367,18 +1380,21 @@ export class SqliteFamilyTransaction {
1367
1380
  this.run(`INSERT INTO sync_push_watermarks(partition, client_id, outbox_id, through_seq) VALUES (?,?,?,?)
1368
1381
  ON CONFLICT(partition, client_id, outbox_id) DO UPDATE SET through_seq=max(through_seq, excluded.through_seq)`, [p, clientId, outbox.outboxId, watermark]);
1369
1382
  this.run('DELETE FROM sync_push_results WHERE partition=? AND client_id=? AND outbox_id=? AND client_seq<?', [p, clientId, outbox.outboxId, watermark]);
1370
- // The sequence step 2 sees: (a) gives a pre-upgrade result the first
1371
- // commit's sequence; a result of this outbox keeps its stored one.
1372
- const deleted = state?.outbox_id === null
1373
- ? first.clientSeq < watermark
1374
- : state?.outbox_id === outbox.outboxId &&
1375
- (state.client_seq ?? 0) < watermark;
1376
- return {
1377
- watermark,
1378
- persisted: state?.result === null || state?.result === undefined || deleted
1379
- ? undefined
1380
- : deserializePushResult(state.result),
1381
- };
1383
+ // The sequence step 2 sees: (a) gives a pre-upgrade result its commit's
1384
+ // sequence in the request; a result of this outbox keeps its stored one.
1385
+ const sequences = new Map(outbox.commits.map((commit) => [commit.clientCommitId, commit.clientSeq]));
1386
+ const records = new Map();
1387
+ for (const row of rows) {
1388
+ if (row.client_commit_id === null || row.result === null)
1389
+ continue;
1390
+ const deleted = row.outbox_id === null
1391
+ ? (sequences.get(row.client_commit_id) ?? 0) < watermark
1392
+ : row.outbox_id === outbox.outboxId &&
1393
+ (row.client_seq ?? 0) < watermark;
1394
+ if (!deleted)
1395
+ records.set(row.client_commit_id, row.result);
1396
+ }
1397
+ return { watermark, persisted: persistedOf(records) };
1382
1398
  }
1383
1399
  async enqueueReactions(reactions) {
1384
1400
  this.assertOpen();
@@ -68,25 +68,17 @@ class SqliteTransaction extends SqliteFamilyTransaction {
68
68
  // BEGIN IMMEDIATE and observes every row staged so far.
69
69
  return this.#storage.runAuthoritativeQuery(this.partition, query);
70
70
  }
71
- async preparePushApply(clientId, clientCommitId, runningSchemaVersion, _targets = [], outbox) {
71
+ async preparePushApply(clientId, commits, runningSchemaVersion, outbox) {
72
72
  this.assertOpen();
73
73
  // BEGIN IMMEDIATE in the constructor already owns SQLite's writer lock.
74
- // The settlement precedes the savepoint, so a rejection keeps it.
75
- const settled = outbox === undefined
76
- ? undefined
77
- : await this.settleOutbox(clientId, clientCommitId, outbox);
74
+ // The settlement precedes the first commit's savepoint, so no rejection
75
+ // discards it.
76
+ const admitted = await this.admitPushRequest(clientId, commits.map((commit) => commit.clientCommitId), outbox);
78
77
  this.#storage.db.exec('SAVEPOINT syncular_push_candidate');
79
78
  this.#pushApplySavepoint = true;
80
79
  const gate = await this.#storage.readServeGate(this.partition, runningSchemaVersion);
81
80
  const [floor] = await this.all('SELECT delete_floor FROM sync_partitions WHERE partition=?', [this.partition]);
82
- const deleteFloor = floor?.delete_floor ?? 0;
83
- return settled === undefined
84
- ? {
85
- gate,
86
- deleteFloor,
87
- persisted: await this.#storage.getPushResult(this.partition, clientId, clientCommitId),
88
- }
89
- : { gate, deleteFloor, ...settled };
81
+ return { gate, deleteFloor: floor?.delete_floor ?? 0, ...admitted };
90
82
  }
91
83
  async lockPartition() {
92
84
  this.assertOpen();
@@ -107,19 +99,24 @@ class SqliteTransaction extends SqliteFamilyTransaction {
107
99
  // BEGIN IMMEDIATE, so the gate and the write share one transaction.
108
100
  return this.#storage.readServeGate(this.partition, runningSchemaVersion);
109
101
  }
110
- async commitRejectedPushResult(clientId, clientCommitId, result, sequence) {
102
+ async rejectPushCommit(clientId, clientCommitId, result, sequence) {
111
103
  this.assertOpen();
112
104
  if (!this.#pushApplySavepoint) {
113
105
  throw new Error('push rejection requires its apply savepoint');
114
106
  }
115
107
  this.#storage.db.exec('ROLLBACK TO SAVEPOINT syncular_push_candidate');
116
- this.#storage.db.exec('RELEASE SAVEPOINT syncular_push_candidate');
117
- this.#pushApplySavepoint = false;
118
108
  await this.putPushResult(clientId, clientCommitId, result, sequence);
119
- await this.commit();
120
- // The idempotency re-check found no record under the writer lock, so the
121
- // insert above is the canonical outcome.
122
- return result;
109
+ }
110
+ /**
111
+ * Closes the request's current commit: its writes and result join the
112
+ * transaction, and the next commit writes behind a new savepoint (§6.4).
113
+ */
114
+ async putPushResult(clientId, clientCommitId, result, sequence) {
115
+ await super.putPushResult(clientId, clientCommitId, result, sequence);
116
+ if (this.#pushApplySavepoint) {
117
+ this.#storage.db.exec('RELEASE SAVEPOINT syncular_push_candidate');
118
+ this.#storage.db.exec('SAVEPOINT syncular_push_candidate');
119
+ }
123
120
  }
124
121
  async upsertRow(table, row, context) {
125
122
  this.assertOpen();
package/dist/storage.d.ts CHANGED
@@ -69,15 +69,19 @@ export interface ServeGate {
69
69
  }[];
70
70
  }
71
71
  /**
72
- * The push apply's admission token: the partition lock, the serve gate, and
73
- * the idempotency record read in one round trip. A storage with per-statement
74
- * network cost answers all three with one statement (see
72
+ * One push request's admission token (§6.4): the partition lock, the serve
73
+ * gate, and the idempotency records of every commit of the request. A storage
74
+ * with per-statement network cost reads them in one statement (see
75
75
  * `PostgresServerStorage`); the lock is held from this call through commit.
76
76
  */
77
77
  export interface PreparedPushApply {
78
78
  readonly gate: ServeGate;
79
- /** The already-persisted outcome for this key, or `undefined` on a miss. */
80
- readonly persisted: StoredPushResult | undefined;
79
+ /**
80
+ * The already-persisted outcome for one of the request's keys, or
81
+ * `undefined` on a miss. Throws `SyncError sync.idempotency_cache_miss`
82
+ * when the stored record is unreadable (§6.3).
83
+ */
84
+ readonly persisted: (clientCommitId: string) => StoredPushResult | undefined;
81
85
  /**
82
86
  * The partition's delete floor (§2.2): the highest version any applied
83
87
  * delete recorded, `0` before the first. An insert starts one above it.
@@ -91,6 +95,15 @@ export interface PreparedPushApply {
91
95
  */
92
96
  readonly watermark?: number;
93
97
  }
98
+ /** One commit of a push request, as `preparePushApply` admits it. */
99
+ export interface PushApplyCommit {
100
+ readonly clientCommitId: string;
101
+ /** The rows the commit is known to touch before its operations are built. */
102
+ readonly targets?: readonly {
103
+ readonly table: string;
104
+ readonly rowIds: readonly string[];
105
+ }[];
106
+ }
94
107
  /** The outbox position of one pushed commit (§6.3.2). */
95
108
  export interface OutboxSequence {
96
109
  readonly outboxId: string;
@@ -541,6 +554,13 @@ export interface AuthoritativeQueryResult {
541
554
  * land together or not at all.
542
555
  */
543
556
  export interface StorageTransaction {
557
+ /**
558
+ * True when `putPushResult` and `rejectPushCommit` persist the closed
559
+ * commit at once (D1, one atomic batch per commit), so a later failure of
560
+ * the request leaves it applied and the push layer still notifies it
561
+ * (§6.4). Absent means the request's commits persist only at `commit()`.
562
+ */
563
+ readonly persistsCommitsOnClose?: boolean;
544
564
  getRow(table: string, rowId: string): Promise<StoredRow | undefined>;
545
565
  /**
546
566
  * Optional candidate-state batch read by primary key. A storage with
@@ -611,35 +631,31 @@ export interface StorageTransaction {
611
631
  lockPartition?(): Promise<void>;
612
632
  /**
613
633
  * Serialize every push apply for this partition before any operation read,
614
- * validation, merge, or write, and read the admission token in the same
615
- * round trip: the RFC 0007 serve gate and the §2.3 idempotency record for
616
- * `clientCommitId`. The lock is retained through terminal-result commit,
617
- * so the idempotency read is exact and a racing duplicate cannot rerun.
618
- * `targets` are the rows the commit is known to touch before its operations
619
- * are built; a storage with per-statement network cost folds that read-ahead
620
- * into the admission read and answers the following `getRow` calls from it.
621
- * Missing support fails closed before an app-row mutation.
634
+ * validation, merge, or write, and read the admission token once for the
635
+ * whole request (§6.4): the RFC 0007 serve gate and the §2.3 idempotency
636
+ * record of every commit in `commits`. The lock is retained until the
637
+ * transaction finishes, so the idempotency read is exact and a racing
638
+ * duplicate cannot rerun. A commit's `targets` are the rows it is known to
639
+ * touch before its operations are built; a storage with per-statement
640
+ * network cost folds that read-ahead into the admission read and answers
641
+ * the following `getRow` calls from it. Missing support fails closed
642
+ * before an app-row mutation.
622
643
  *
623
- * `outbox`, passed with the first commit of a request that carries an
624
- * `outboxId`, settles the client's results stored before wire version 4
625
- * (§9) and runs §6.3.2 steps 1 and 2 under the same lock, before the read
626
- * of `persisted`. Those writes survive `commitRejectedPushResult`; the
627
- * caller commits this transaction whatever the commit's outcome, so the
628
- * settlement runs exactly once per request.
629
- */
630
- preparePushApply(clientId: string, clientCommitId: string, runningSchemaVersion: number, targets?: readonly {
631
- readonly table: string;
632
- readonly rowIds: readonly string[];
633
- }[], outbox?: OutboxSettlement): Promise<PreparedPushApply>;
644
+ * `outbox`, passed when the request carries an `outboxId`, settles the
645
+ * client's results stored before wire version 4 (§9) and runs §6.3.2 steps
646
+ * 1 and 2 under the same lock, before the read of `persisted`. Those
647
+ * writes precede every commit's savepoint, so no rejection discards them.
648
+ */
649
+ preparePushApply(clientId: string, commits: readonly PushApplyCommit[], runningSchemaVersion: number, outbox?: OutboxSettlement): Promise<PreparedPushApply>;
634
650
  /**
635
- * Rejection finalization while the push-apply serialization lock is still
636
- * held: discard every candidate write, persist the rejected idempotency
637
- * result, and finish the transaction atomically. Returns the persisted
638
- * canonical outcome, so the caller never re-reads it. Required for every
639
- * push so a concurrent duplicate cannot rerun operations, validators, or
640
- * merges.
651
+ * Reject the request's current commit while the push-apply serialization
652
+ * lock is still held: discard every candidate write the commit staged since
653
+ * the previous commit closed (its savepoint), keep the request's earlier
654
+ * commits, and record the rejected idempotency result. Like
655
+ * `putPushResult`, it closes the commit; the transaction stays open for the
656
+ * request's next commit.
641
657
  */
642
- commitRejectedPushResult(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<StoredPushResult>;
658
+ rejectPushCommit(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
643
659
  upsertRow(table: string, row: StoredRow, context?: {
644
660
  readonly opIndex: number;
645
661
  }): Promise<void>;
@@ -657,6 +673,12 @@ export interface StorageTransaction {
657
673
  * first writer wins; callers read the canonical value after commit. A
658
674
  * result without `sequence` (no `outboxId`, §6.10, §12.3) is never deleted
659
675
  * by a watermark.
676
+ *
677
+ * The call closes the request's current push commit: the next write opens
678
+ * the next commit's savepoint. PostgreSQL writes the request's results as
679
+ * one statement at `commit()`. D1, whose transaction cannot read its own
680
+ * staged writes across commits, flushes the commit's writes and this
681
+ * result as one atomic batch here (§6.4).
660
682
  */
661
683
  putPushResult(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
662
684
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/server",
3
- "version": "0.77.3",
3
+ "version": "0.78.0",
4
4
  "description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -68,7 +68,7 @@
68
68
  "verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
69
69
  },
70
70
  "dependencies": {
71
- "@syncular/core": "0.77.3"
71
+ "@syncular/core": "0.78.0"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@electric-sql/pglite": "^0.5.4"
package/src/d1-storage.ts CHANGED
@@ -21,9 +21,11 @@
21
21
  *
22
22
  * - reads (`getRow`) execute immediately against D1 (autocommit);
23
23
  * - writes are **buffered** as prepared statements;
24
- * - `commit()` flushes the whole buffer as one `db.batch(...)` — the same
25
- * atomicity §6.4 requires (a rejected op rolls back by simply never
26
- * flushing);
24
+ * - `putPushResult` closes a push commit and flushes the buffer as one
25
+ * `db.batch(...)` (§6.4: one atomic batch per commit, because a later
26
+ * commit of the request must read the earlier one's writes from D1);
27
+ * `commit()` flushes whatever remains. A rejected commit rolls back by
28
+ * never flushing its part of the buffer;
27
29
  * - `rollback()` drops the buffer unsent.
28
30
  *
29
31
  * `appendCommit` allocates the dense per-partition `commitSeq` (§2.1) by
@@ -103,6 +105,7 @@ import type {
103
105
  OutboxSequence,
104
106
  OutboxSettlement,
105
107
  PreparedPushApply,
108
+ PushApplyCommit,
106
109
  RowScanQuery,
107
110
  ScopeValueHistoryQuery,
108
111
  ServerStorage,
@@ -208,6 +211,8 @@ class D1Transaction
208
211
  ) => BoundAuthoritativeQuery;
209
212
  readonly #pushApplySerialized: boolean;
210
213
  readonly #scanVariables: Map<string, string>;
214
+ /** Each push commit reaches D1 as its own batch when it closes (§6.4). */
215
+ readonly persistsCommitsOnClose = true;
211
216
  readonly #buffer: SqliteStatementSpec[] = [];
212
217
  /** Live snapshot of `max_commit_seq`, advanced within this transaction. */
213
218
  #maxCommitSeq: number | undefined;
@@ -545,12 +550,8 @@ class D1Transaction
545
550
 
546
551
  async preparePushApply(
547
552
  clientId: string,
548
- clientCommitId: string,
553
+ commits: readonly PushApplyCommit[],
549
554
  runningSchemaVersion: number,
550
- _targets: readonly {
551
- readonly table: string;
552
- readonly rowIds: readonly string[];
553
- }[] = [],
554
555
  outbox?: OutboxSettlement,
555
556
  ): Promise<PreparedPushApply> {
556
557
  this.assertOpen();
@@ -561,12 +562,13 @@ class D1Transaction
561
562
  }
562
563
  // D1 has no interactive lock. The caller explicitly asserted that every
563
564
  // write for this partition is already serialized (normally by its DO).
564
- // The settlement's writes sit below the checkpoint, so a rejection keeps
565
- // them.
566
- const settled =
567
- outbox === undefined
568
- ? undefined
569
- : await this.settleOutbox(clientId, clientCommitId, outbox);
565
+ // The settlement's writes sit below the first commit's checkpoint, so a
566
+ // rejection keeps them.
567
+ const admitted = await this.admitPushRequest(
568
+ clientId,
569
+ commits.map((commit) => commit.clientCommitId),
570
+ outbox,
571
+ );
570
572
  this.#pushApplyCheckpoint = this.#buffer.length;
571
573
  // The serve gate and the delete floor (§2.2) in one round trip.
572
574
  const read = await this.#db
@@ -580,14 +582,7 @@ class D1Transaction
580
582
  logEpoch: undefined,
581
583
  pending: [],
582
584
  };
583
- const deleteFloor = read?.delete_floor ?? 0;
584
- return settled === undefined
585
- ? {
586
- gate,
587
- deleteFloor,
588
- persisted: await this.getPushResult(clientId, clientCommitId),
589
- }
590
- : { gate, deleteFloor, ...settled };
585
+ return { gate, deleteFloor: read?.delete_floor ?? 0, ...admitted };
591
586
  }
592
587
 
593
588
  advanceCheckpoint(
@@ -615,12 +610,12 @@ class D1Transaction
615
610
  };
616
611
  }
617
612
 
618
- async commitRejectedPushResult(
613
+ async rejectPushCommit(
619
614
  clientId: string,
620
615
  clientCommitId: string,
621
616
  result: StoredPushResult,
622
617
  sequence?: OutboxSequence,
623
- ): Promise<StoredPushResult> {
618
+ ): Promise<void> {
624
619
  this.assertOpen();
625
620
  const checkpoint = this.#pushApplyCheckpoint;
626
621
  if (checkpoint === undefined) {
@@ -632,10 +627,27 @@ class D1Transaction
632
627
  this.#maxCommitSeq = undefined;
633
628
  this.#applicationOpIndexes.clear();
634
629
  await this.putPushResult(clientId, clientCommitId, result, sequence);
635
- await this.commit();
636
- // The idempotency re-check found no record under the serialized apply, so
637
- // the insert above is the canonical outcome.
638
- return result;
630
+ }
631
+
632
+ /**
633
+ * Closes the request's current commit (§6.4): its buffered writes and this
634
+ * result reach D1 as one atomic batch. A later commit of the request then
635
+ * reads its rows, tombstones and registered queries from D1 itself, which
636
+ * a buffer spanning commits could not answer.
637
+ */
638
+ override async putPushResult(
639
+ clientId: string,
640
+ clientCommitId: string,
641
+ result: StoredPushResult,
642
+ sequence?: OutboxSequence,
643
+ ): Promise<void> {
644
+ await super.putPushResult(clientId, clientCommitId, result, sequence);
645
+ await this.#flush();
646
+ this.#pending.clear();
647
+ this.#clearedTombstones.clear();
648
+ this.#prefetched.clear();
649
+ this.#applicationOpIndexes.clear();
650
+ if (this.#pushApplyCheckpoint !== undefined) this.#pushApplyCheckpoint = 0;
639
651
  }
640
652
 
641
653
  async #assertNoUniqueCollision(
@@ -776,17 +788,19 @@ class D1Transaction
776
788
 
777
789
  async commit(): Promise<void> {
778
790
  this.assertOpen();
779
- if (this.#buffer.length === 0) {
780
- this.open = false;
781
- return;
782
- }
791
+ await this.#flush();
792
+ this.open = false;
793
+ }
794
+
795
+ /** Send the buffer as one atomic D1 batch: the §6.4 all-or-nothing write. */
796
+ async #flush(): Promise<void> {
797
+ if (this.#buffer.length === 0) return;
783
798
  const statements = this.#buffer.map((entry) =>
784
799
  this.#db.prepare(entry.sql).bind(...entry.params),
785
800
  );
786
- // One atomic D1 batch — the §6.4 all-or-nothing commit.
787
801
  try {
788
802
  await this.#db.batch(statements);
789
- this.open = false;
803
+ this.#buffer.length = 0;
790
804
  } catch (error) {
791
805
  if (isD1ConstraintError(error)) {
792
806
  // D1 batches are atomic. Keep this logical transaction open so the
package/src/handler.ts CHANGED
@@ -61,7 +61,7 @@ import {
61
61
  planUnit,
62
62
  type UnitPlan,
63
63
  } from './pull';
64
- import { type ProcessedPushCommit, processPushCommitWithTrace } from './push';
64
+ import { type ProcessedPushCommit, processPushCommitsWithTrace } from './push';
65
65
  import { serveNotReadyError } from './readiness';
66
66
  import type { CompiledSchema, CompiledTable } from './schema';
67
67
  import { compileSchema } from './schema';
@@ -621,39 +621,61 @@ async function* streamResponse(
621
621
  }
622
622
  const { clientId, outboxId } = plan.header;
623
623
  try {
624
- // Push half (§6): one PUSH_RESULT per PUSH_COMMIT, in request order.
624
+ // Push half (§6): one PUSH_RESULT per PUSH_COMMIT, in request order,
625
+ // sent after the request's one transaction commits (§6.4).
625
626
  const currentSchema = compileSchema(ctx.schema);
626
627
  let acceptedThrough = 0;
627
- // §6.3.2: the request's first commit settles the outbox and reads `W`.
628
- let watermark = 0;
629
- for (const [index, push] of plan.pushes.entries()) {
630
- const processed = await processPushCommitWithTrace(
631
- ctx,
632
- currentSchema,
633
- plan.resolved,
634
- clientId,
635
- push,
636
- schema,
637
- outboxId === undefined
638
- ? undefined
639
- : {
640
- outboxId,
641
- clientSeq: push.clientSeq,
642
- watermark,
643
- ...(index === 0
644
- ? {
645
- settlement: {
646
- outboxId,
647
- commits: plan.pushes.map((commit) => ({
648
- clientCommitId: commit.clientCommitId,
649
- clientSeq: commit.clientSeq,
628
+ const pushed =
629
+ plan.pushes.length === 0
630
+ ? []
631
+ : await processPushCommitsWithTrace(
632
+ ctx,
633
+ currentSchema,
634
+ plan.resolved,
635
+ clientId,
636
+ plan.pushes.map((push) => {
637
+ // The frame names its rows before the transaction opens, so the
638
+ // admission read carries the commit's read-ahead. A rowId
639
+ // holding U+0000 is rejected per operation (§6.1) before any
640
+ // store reads it.
641
+ const targets = new Map<string, string[]>();
642
+ for (const op of push.operations) {
643
+ if (
644
+ !currentSchema.tables.has(op.table) ||
645
+ op.rowId.includes('\u0000')
646
+ ) {
647
+ continue;
648
+ }
649
+ const ids = targets.get(op.table) ?? [];
650
+ ids.push(op.rowId);
651
+ targets.set(op.table, ids);
652
+ }
653
+ return {
654
+ clientCommitId: push.clientCommitId,
655
+ buildOperations: async () => push.operations,
656
+ ...(targets.size === 0
657
+ ? {}
658
+ : {
659
+ targets: [...targets].map(([table, rowIds]) => ({
660
+ table,
661
+ rowIds,
650
662
  })),
651
- },
652
- }
653
- : {}),
654
- },
655
- );
656
- watermark = processed.watermark ?? watermark;
663
+ }),
664
+ };
665
+ }),
666
+ schema,
667
+ outboxId === undefined
668
+ ? undefined
669
+ : {
670
+ outboxId,
671
+ commits: plan.pushes.map((commit) => ({
672
+ clientCommitId: commit.clientCommitId,
673
+ clientSeq: commit.clientSeq,
674
+ })),
675
+ },
676
+ );
677
+ for (const [index, push] of plan.pushes.entries()) {
678
+ const processed = pushed[index]!;
657
679
  acceptedThrough = Math.max(
658
680
  acceptedThrough,
659
681
  processed.frame.commitSeq ?? 0,