@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
@@ -1,16 +1,8 @@
1
- import { retainRollbackFailure, rollbackAfterError } from './storage-errors';
2
- import { validateCommitPruneQuery } from './prune';
3
- import { StorageQueryError } from './storage-errors';
4
- import type { CommitPruneQuery, CommitPruneResult } from './storage';
5
- import {
6
- clientRoundState,
7
- PARTITION_TOUCH_INTERVAL_MS,
8
- rowsLiveAfterDelete,
9
- serveGateRefusal,
10
- } from './storage';
11
- import { serveNotReadyError } from './readiness';
12
1
  /**
13
- * SQLite server storage over the shared synchronous driver.
2
+ * SQLite server storage over the shared synchronous driver. The statements
3
+ * it shares with D1 live in `SqliteFamilyStorage` (`sqlite-dialect.ts`);
4
+ * this class adds the request lock, interactive `BEGIN IMMEDIATE`
5
+ * transactions, in-place schema migration and backfill checkpoints.
14
6
  *
15
7
  * Scope fanout is index-first: both the commit log and the
16
8
  * current-row table carry a (table, variable, value) inverted index; reads
@@ -18,119 +10,66 @@ import { serveNotReadyError } from './readiness';
18
10
  * match against the stored scope map — never a log scan.
19
11
  */
20
12
  import {
21
- bindAuthoritativePartition,
22
- prepareAuthoritativeQuery,
23
- } from './authoritative-query';
24
- import type { ScopeMap } from '@syncular/core';
25
- import { syncError } from './errors';
13
+ isSqliteConstraintError,
14
+ retainRollbackFailure,
15
+ rollbackAfterError,
16
+ StorageConstraintError,
17
+ StorageQueryError,
18
+ } from './storage-errors';
19
+ import { commitDeleteFloor, serveGateRefusal } from './storage';
20
+ import { serveNotReadyError } from './readiness';
26
21
  import {
27
22
  assertPhysicalColumns,
28
23
  assertStoredLayouts,
29
- commitWindowPageSql,
30
- LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL,
31
- deleteRowSql,
32
- deleteSqliteRowScopesSql,
33
24
  dropTableDdl,
34
- indexRowPageStatement,
35
25
  layoutsOf,
36
26
  migratePayload,
37
27
  parseLayouts,
38
- quoteIdent,
39
28
  retiredTableNames,
40
29
  rewritePlan,
41
30
  rewriteRowSql,
42
31
  rewriteValues,
43
32
  SCHEMA_META_DDL_SQLITE,
44
- SYNC_COLUMN_VERSIONS_COLUMN,
45
- SYNC_PARTITION_COLUMN,
46
- SYNC_PAYLOAD_COLUMN,
47
- SYNC_ROW_ID_COLUMN,
48
- SYNC_SCOPES_COLUMN,
49
- SYNC_VERSION_COLUMN,
50
33
  type StoredColumnLayout,
51
- scanRowPageSql,
52
34
  schemaDdl,
53
35
  serverIndexDdl,
54
36
  SQLITE_TABLE_INDEXES_SQL,
55
37
  type SqliteTableIndexRow,
56
38
  sqliteIndexSignatures,
57
- selectRowScopesSql,
58
- selectRowSql,
59
39
  selectRowsForRewriteSql,
60
- upsertSql,
61
- upsertValues,
62
40
  } from './relational-rows';
63
41
  import type { CompiledSchema, CompiledTable } from './schema';
64
- import { matchesEffective } from './scopes';
65
42
  import {
66
- collectCommitWindowPage,
67
- deserializePushResult,
68
- ERASE_SCOPE_VALUE_HISTORY_SQLITE_SQL,
69
- eraseScopeValueHistoryParams,
70
- LIST_SCOPE_VALUE_ROWS_SQLITE_SQL,
71
- placeholders,
72
43
  SQLITE_DDL,
73
- SYNC_CLIENT_ROUND_COLUMNS,
74
- type SqliteChangeRecord,
75
- type SqliteCommitWindowRecord,
76
- type SqliteRowRecord,
77
- serializePushResult,
78
- toStoredChange,
79
- toStoredRow,
44
+ SQLITE_UPGRADE_PROBE,
45
+ SqliteFamilyStorage,
46
+ SqliteFamilyTransaction,
47
+ type SqliteStatementSpec,
48
+ sqliteUpgradeDdl,
80
49
  } from './sqlite-dialect';
81
50
  import {
82
51
  SqliteAdapterRequiredError,
83
52
  type SqliteDatabase,
53
+ type SqliteValue,
84
54
  } from './sqlite-driver';
85
55
  import type {
86
56
  AuthoritativeQueryRequest,
87
57
  AuthoritativeQueryResult,
88
58
  AuthoritativeQueryValue,
89
59
  CheckpointDeclaration,
90
- ClientCursorInfo,
91
- ClientPushResult,
92
- ClientRecord,
93
- ClientRecordWriteOptions,
94
- ClientSubscription,
95
- CommitChangeSummary,
96
- CommitDetail,
97
- CommitMetadata,
98
- CommitMetadataQuery,
99
- CommitWindowQuery,
100
- DurableJsonValue,
101
60
  IndexRowScanQuery,
102
61
  NewCommit,
103
- NewReaction,
62
+ OutboxSequence,
63
+ OutboxSettlement,
104
64
  PreparedPushApply,
105
- PartitionRegistryEntry,
106
- PrunedReactionCounts,
107
- ReactionClaimQuery,
108
- ReactionFailure,
109
- ReactionFailureUpdate,
110
- ReactionListQuery,
111
- ReactionPruneQuery,
112
- RowChangeRecord,
113
- RowChangesQuery,
114
- RowListQuery,
115
65
  RowScanQuery,
116
- ScopeActivityQuery,
117
- ScopeCommitActivity,
118
- ScopeValueHistoryQuery,
119
- ScopeValueQuery,
120
66
  ServerStorage,
121
67
  ServeGate,
122
68
  StorageTransaction,
123
- StoredCommit,
124
69
  StoredCheckpoint,
125
70
  StoredPushResult,
126
- StoredReaction,
127
71
  StoredRow,
128
72
  } from './storage';
129
- import {
130
- isSqliteConstraintError,
131
- StorageConstraintError,
132
- } from './storage-errors';
133
- import { assertScopeIndexedScan, resolveIndexRowScan } from './storage-query';
134
73
 
135
74
  interface SqliteCheckpointRecord {
136
75
  partition: string;
@@ -156,59 +95,13 @@ function toStoredCheckpoint(record: SqliteCheckpointRecord): StoredCheckpoint {
156
95
  };
157
96
  }
158
97
 
159
- interface SqliteReactionRecord {
160
- partition: string;
161
- idempotency_key: string;
162
- type: string;
163
- version: number;
164
- payload: string;
165
- source_client_id: string;
166
- source_client_commit_id: string;
167
- source_commit_seq: number;
168
- created_at_ms: number;
169
- available_at_ms: number;
170
- status: StoredReaction['status'];
171
- attempts: number;
172
- max_attempts: number;
173
- lease_owner: string | null;
174
- lease_expires_at_ms: number | null;
175
- completed_at_ms: number | null;
176
- last_failure: string | null;
177
- }
178
-
179
- function toStoredReaction(record: SqliteReactionRecord): StoredReaction {
180
- return {
181
- idempotencyKey: record.idempotency_key,
182
- type: record.type,
183
- version: record.version,
184
- payload: JSON.parse(record.payload) as DurableJsonValue,
185
- sourceClientId: record.source_client_id,
186
- sourceClientCommitId: record.source_client_commit_id,
187
- sourceCommitSeq: record.source_commit_seq,
188
- createdAtMs: record.created_at_ms,
189
- maxAttempts: record.max_attempts,
190
- status: record.status,
191
- attempts: record.attempts,
192
- availableAtMs: record.available_at_ms,
193
- ...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
194
- ...(record.lease_expires_at_ms !== null
195
- ? { leaseExpiresAtMs: record.lease_expires_at_ms }
196
- : {}),
197
- ...(record.completed_at_ms !== null
198
- ? { completedAtMs: record.completed_at_ms }
199
- : {}),
200
- ...(record.last_failure !== null
201
- ? { lastFailure: JSON.parse(record.last_failure) as ReactionFailure }
202
- : {}),
203
- };
204
- }
205
-
206
- class SqliteTransaction implements StorageTransaction {
98
+ class SqliteTransaction
99
+ extends SqliteFamilyTransaction
100
+ implements StorageTransaction
101
+ {
207
102
  #storage: SqliteServerStorage;
208
- #partition: string;
209
103
  /** Schema version this transaction writes; rides every appended commit row. */
210
104
  #writerVersion: number;
211
- #open = true;
212
105
  #pushApplySavepoint = false;
213
106
  readonly #release: () => void;
214
107
 
@@ -218,75 +111,50 @@ class SqliteTransaction implements StorageTransaction {
218
111
  writerVersion: number,
219
112
  release: () => void,
220
113
  ) {
114
+ super(partition);
221
115
  this.#storage = storage;
222
- this.#partition = partition;
223
116
  this.#writerVersion = writerVersion;
224
117
  this.#release = release;
225
118
  storage.db.exec('BEGIN IMMEDIATE');
226
119
  }
227
120
 
228
- #assertOpen(): void {
229
- if (!this.#open) throw new Error('transaction already finished');
230
- }
231
-
232
- getRow(table: string, rowId: string): Promise<StoredRow | undefined> {
233
- this.#assertOpen();
234
- return this.#storage.getRow(this.#partition, table, rowId);
121
+ protected run(sql: string, params: readonly SqliteValue[]): void {
122
+ this.#storage.db.run(sql, params);
235
123
  }
236
124
 
237
- async getRows(
238
- table: string,
239
- rowIds: readonly string[],
240
- ): Promise<ReadonlyMap<string, StoredRow>> {
241
- this.#assertOpen();
242
- // SQLite is in-process: one indexed lookup per id costs no round trip.
243
- const rows = new Map<string, StoredRow>();
244
- for (const rowId of new Set(rowIds)) {
245
- const row = await this.#storage.getRow(this.#partition, table, rowId);
246
- if (row !== undefined) rows.set(rowId, row);
247
- }
248
- return rows;
125
+ protected async all<T>(
126
+ sql: string,
127
+ params: readonly SqliteValue[],
128
+ ): Promise<T[]> {
129
+ return this.#storage.db.query<T, SqliteValue[]>(sql).all(...params);
249
130
  }
250
131
 
251
- async getTombstoneSeq(
252
- table: string,
253
- rowId: string,
254
- ): Promise<number | undefined> {
255
- this.#assertOpen();
256
- const row = this.#storage.db
257
- .query<{ commit_seq: number }, [string, string, string]>(
258
- 'SELECT commit_seq FROM sync_tombstones WHERE partition=? AND tbl=? AND row_id=?',
259
- )
260
- .get(this.#partition, table, rowId);
261
- return row?.commit_seq;
132
+ protected table(name: string): CompiledTable {
133
+ return this.#storage.table(name);
262
134
  }
263
135
 
264
- async clearTombstone(table: string, rowId: string): Promise<void> {
265
- this.#assertOpen();
266
- this.#storage.db
267
- .query(
268
- 'DELETE FROM sync_tombstones WHERE partition=? AND tbl=? AND row_id=?',
269
- )
270
- .run(this.#partition, table, rowId);
136
+ getRow(table: string, rowId: string): Promise<StoredRow | undefined> {
137
+ this.assertOpen();
138
+ return this.#storage.getRow(this.partition, table, rowId);
271
139
  }
272
140
 
273
141
  scanRows(query: RowScanQuery): Promise<StoredRow[]> {
274
- this.#assertOpen();
275
- return this.#storage.scanRows(this.#partition, query);
142
+ this.assertOpen();
143
+ return this.#storage.scanRows(this.partition, query);
276
144
  }
277
145
 
278
146
  scanRowsByIndex(query: IndexRowScanQuery): Promise<StoredRow[]> {
279
- this.#assertOpen();
280
- return this.#storage.scanRowsByIndex(this.#partition, query);
147
+ this.assertOpen();
148
+ return this.#storage.scanRowsByIndex(this.partition, query);
281
149
  }
282
150
 
283
151
  async queryAuthoritative(
284
152
  query: AuthoritativeQueryRequest,
285
153
  ): Promise<AuthoritativeQueryResult> {
286
- this.#assertOpen();
154
+ this.assertOpen();
287
155
  // Same shared connection: the query runs inside this transaction's
288
156
  // BEGIN IMMEDIATE and observes every row staged so far.
289
- return this.#storage.runAuthoritativeQuery(this.#partition, query);
157
+ return this.#storage.runAuthoritativeQuery(this.partition, query);
290
158
  }
291
159
 
292
160
  async preparePushApply(
@@ -297,26 +165,41 @@ class SqliteTransaction implements StorageTransaction {
297
165
  readonly table: string;
298
166
  readonly rowIds: readonly string[];
299
167
  }[] = [],
168
+ outbox?: OutboxSettlement,
300
169
  ): Promise<PreparedPushApply> {
301
- this.#assertOpen();
170
+ this.assertOpen();
302
171
  // BEGIN IMMEDIATE in the constructor already owns SQLite's writer lock.
172
+ // The settlement precedes the savepoint, so a rejection keeps it.
173
+ const settled =
174
+ outbox === undefined
175
+ ? undefined
176
+ : await this.settleOutbox(clientId, clientCommitId, outbox);
303
177
  this.#storage.db.exec('SAVEPOINT syncular_push_candidate');
304
178
  this.#pushApplySavepoint = true;
305
- return {
306
- gate: await this.#storage.readServeGate(
307
- this.#partition,
308
- runningSchemaVersion,
309
- ),
310
- persisted: await this.#storage.getPushResult(
311
- this.#partition,
312
- clientId,
313
- clientCommitId,
314
- ),
315
- };
179
+ const gate = await this.#storage.readServeGate(
180
+ this.partition,
181
+ runningSchemaVersion,
182
+ );
183
+ const [floor] = await this.all<{ delete_floor: number }>(
184
+ 'SELECT delete_floor FROM sync_partitions WHERE partition=?',
185
+ [this.partition],
186
+ );
187
+ const deleteFloor = floor?.delete_floor ?? 0;
188
+ return settled === undefined
189
+ ? {
190
+ gate,
191
+ deleteFloor,
192
+ persisted: await this.#storage.getPushResult(
193
+ this.partition,
194
+ clientId,
195
+ clientCommitId,
196
+ ),
197
+ }
198
+ : { gate, deleteFloor, ...settled };
316
199
  }
317
200
 
318
201
  async lockPartition(): Promise<void> {
319
- this.#assertOpen();
202
+ this.assertOpen();
320
203
  // BEGIN IMMEDIATE in the constructor already owns SQLite's writer lock.
321
204
  }
322
205
 
@@ -327,37 +210,38 @@ class SqliteTransaction implements StorageTransaction {
327
210
  observedRows: number,
328
211
  nowMs: number,
329
212
  ): Promise<boolean> {
330
- this.#assertOpen();
213
+ this.assertOpen();
331
214
  const result = this.#storage.db
332
215
  .query(
333
216
  `UPDATE sync_backfill_checkpoints
334
217
  SET watermark=?, observed_rows=?, updated_at_ms=?
335
218
  WHERE partition=? AND name=? AND owner_epoch=?`,
336
219
  )
337
- .run(watermark, observedRows, nowMs, this.#partition, name, ownerEpoch);
220
+ .run(watermark, observedRows, nowMs, this.partition, name, ownerEpoch);
338
221
  return Number(result.changes) === 1;
339
222
  }
340
223
 
341
224
  readServeGate(runningSchemaVersion: number): Promise<ServeGate> {
342
- this.#assertOpen();
225
+ this.assertOpen();
343
226
  // Same shared connection: this read runs inside this transaction's
344
227
  // BEGIN IMMEDIATE, so the gate and the write share one transaction.
345
- return this.#storage.readServeGate(this.#partition, runningSchemaVersion);
228
+ return this.#storage.readServeGate(this.partition, runningSchemaVersion);
346
229
  }
347
230
 
348
231
  async commitRejectedPushResult(
349
232
  clientId: string,
350
233
  clientCommitId: string,
351
234
  result: StoredPushResult,
235
+ sequence?: OutboxSequence,
352
236
  ): Promise<StoredPushResult> {
353
- this.#assertOpen();
237
+ this.assertOpen();
354
238
  if (!this.#pushApplySavepoint) {
355
239
  throw new Error('push rejection requires its apply savepoint');
356
240
  }
357
241
  this.#storage.db.exec('ROLLBACK TO SAVEPOINT syncular_push_candidate');
358
242
  this.#storage.db.exec('RELEASE SAVEPOINT syncular_push_candidate');
359
243
  this.#pushApplySavepoint = false;
360
- await this.putPushResult(clientId, clientCommitId, result);
244
+ await this.putPushResult(clientId, clientCommitId, result, sequence);
361
245
  await this.commit();
362
246
  // The idempotency re-check found no record under the writer lock, so the
363
247
  // insert above is the canonical outcome.
@@ -369,9 +253,9 @@ class SqliteTransaction implements StorageTransaction {
369
253
  row: StoredRow,
370
254
  context?: { readonly opIndex: number },
371
255
  ): Promise<void> {
372
- this.#assertOpen();
256
+ this.assertOpen();
373
257
  try {
374
- this.#storage.writeRow(this.#partition, table, row);
258
+ this.writeRow(table, row);
375
259
  } catch (error) {
376
260
  if (isSqliteConstraintError(error)) {
377
261
  throw new StorageConstraintError(error, context?.opIndex);
@@ -380,151 +264,37 @@ class SqliteTransaction implements StorageTransaction {
380
264
  }
381
265
  }
382
266
 
383
- async deleteRow(table: string, rowId: string): Promise<void> {
384
- this.#assertOpen();
385
- const db = this.#storage.db;
386
- const compiled = this.#storage.table(table);
387
- db.query(deleteSqliteRowScopesSql(compiled)).run(
388
- this.#partition,
389
- table,
390
- rowId,
391
- this.#partition,
392
- rowId,
393
- );
394
- db.query(deleteRowSql(compiled, 'sqlite')).run(this.#partition, rowId);
395
- // §5.9.4: a deleted row references no blobs.
396
- db.query(
397
- 'DELETE FROM sync_blob_refs WHERE partition=? AND tbl=? AND row_id=?',
398
- ).run(this.#partition, table, rowId);
399
- }
400
-
401
- async setBlobRefs(
402
- table: string,
403
- rowId: string,
404
- blobIds: readonly string[],
405
- ): Promise<void> {
406
- this.#assertOpen();
407
- const db = this.#storage.db;
408
- // Replace the row's reference set atomically (§5.9.4).
409
- db.query(
410
- 'DELETE FROM sync_blob_refs WHERE partition=? AND tbl=? AND row_id=?',
411
- ).run(this.#partition, table, rowId);
412
- for (const blobId of blobIds) {
413
- db.query(
414
- 'INSERT OR IGNORE INTO sync_blob_refs(partition, tbl, row_id, blob_id) VALUES (?,?,?,?)',
415
- ).run(this.#partition, table, rowId, blobId);
416
- }
417
- }
418
-
419
- async appendCommit(commit: NewCommit): Promise<number> {
420
- this.#assertOpen();
421
- const db = this.#storage.db;
422
- const p = this.#partition;
423
- db.query('INSERT OR IGNORE INTO sync_partitions(partition) VALUES (?)').run(
267
+ protected async appendCommitRow(commit: NewCommit): Promise<number> {
268
+ const p = this.partition;
269
+ this.run('INSERT OR IGNORE INTO sync_partitions(partition) VALUES (?)', [
424
270
  p,
271
+ ]);
272
+ const [row] = await this.all<{ max_commit_seq: number }>(
273
+ 'SELECT max_commit_seq FROM sync_partitions WHERE partition=?',
274
+ [p],
425
275
  );
426
- const row = db
427
- .query<{ max_commit_seq: number }, [string]>(
428
- 'SELECT max_commit_seq FROM sync_partitions WHERE partition=?',
429
- )
430
- .get(p);
431
276
  const commitSeq = (row?.max_commit_seq ?? 0) + 1;
432
- db.query(
433
- 'UPDATE sync_partitions SET max_commit_seq=? WHERE partition=?',
434
- ).run(commitSeq, p);
435
- db.query(
436
- 'INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms, writer_version) VALUES (?,?,?,?,?,?,?)',
437
- ).run(
438
- p,
439
- commitSeq,
440
- commit.clientId,
441
- commit.clientCommitId,
442
- commit.actorId,
443
- commit.createdAtMs,
444
- this.#writerVersion,
277
+ this.run(
278
+ 'UPDATE sync_partitions SET max_commit_seq=?, delete_floor=max(delete_floor, ?) WHERE partition=?',
279
+ [commitSeq, commitDeleteFloor(commit.changes), p],
445
280
  );
446
- const liveAfterDelete = rowsLiveAfterDelete(commit.changes);
447
- commit.changes.forEach((change, idx) => {
448
- db.query(
449
- 'INSERT INTO sync_changes(partition, commit_seq, idx, tbl, row_id, op, row_version, scopes, payload) VALUES (?,?,?,?,?,?,?,?,?)',
450
- ).run(
281
+ this.run(
282
+ 'INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms, writer_version) VALUES (?,?,?,?,?,?,?)',
283
+ [
451
284
  p,
452
285
  commitSeq,
453
- idx,
454
- change.table,
455
- change.rowId,
456
- change.op === 'upsert' ? 1 : 2,
457
- change.rowVersion ?? null,
458
- JSON.stringify(change.scopes),
459
- change.payload ?? null,
460
- );
461
- if (
462
- change.op === 'delete' &&
463
- !liveAfterDelete.has(`${change.table}\u0000${change.rowId}`)
464
- ) {
465
- // §5 delete precedence: every applied delete leaves a tombstone,
466
- // pruned with the commit log (§4.6). A delete that the same commit
467
- // follows with an upsert is a §2.2 scope move, not a deletion.
468
- db.query(
469
- `INSERT INTO sync_tombstones(partition, tbl, row_id, commit_seq) VALUES (?,?,?,?)
470
- ON CONFLICT(partition, tbl, row_id) DO UPDATE SET commit_seq=excluded.commit_seq`,
471
- ).run(p, change.table, change.rowId, commitSeq);
472
- }
473
- for (const [variable, value] of Object.entries(change.scopes)) {
474
- db.query(
475
- 'INSERT OR IGNORE INTO sync_change_scopes(partition, tbl, var, value, commit_seq) VALUES (?,?,?,?,?)',
476
- ).run(p, change.table, variable, value, commitSeq);
477
- }
478
- });
479
- return commitSeq;
480
- }
481
-
482
- async putPushResult(
483
- clientId: string,
484
- clientCommitId: string,
485
- result: StoredPushResult,
486
- ): Promise<void> {
487
- this.#assertOpen();
488
- this.#storage.db
489
- .query(
490
- 'INSERT OR IGNORE INTO sync_push_results(partition, client_id, client_commit_id, result) VALUES (?,?,?,?)',
491
- )
492
- .run(
493
- this.#partition,
494
- clientId,
495
- clientCommitId,
496
- serializePushResult(result),
497
- );
498
- }
499
-
500
- async enqueueReactions(reactions: readonly NewReaction[]): Promise<void> {
501
- this.#assertOpen();
502
- const statement = this.#storage.db.query(
503
- `INSERT INTO sync_reactions(
504
- partition, idempotency_key, type, version, payload,
505
- source_client_id, source_client_commit_id, source_commit_seq,
506
- created_at_ms, available_at_ms, status, attempts, max_attempts
507
- ) VALUES (?,?,?,?,?,?,?,?,?,?,'pending',0,?)`,
286
+ commit.clientId,
287
+ commit.clientCommitId,
288
+ commit.actorId,
289
+ commit.createdAtMs,
290
+ this.#writerVersion,
291
+ ],
508
292
  );
509
- for (const reaction of reactions) {
510
- statement.run(
511
- this.#partition,
512
- reaction.idempotencyKey,
513
- reaction.type,
514
- reaction.version,
515
- JSON.stringify(reaction.payload),
516
- reaction.sourceClientId,
517
- reaction.sourceClientCommitId,
518
- reaction.sourceCommitSeq,
519
- reaction.createdAtMs,
520
- reaction.createdAtMs,
521
- reaction.maxAttempts,
522
- );
523
- }
293
+ return commitSeq;
524
294
  }
525
295
 
526
296
  async commit(): Promise<void> {
527
- this.#assertOpen();
297
+ this.assertOpen();
528
298
  try {
529
299
  this.#storage.db.exec('COMMIT');
530
300
  } catch (error) {
@@ -539,14 +309,14 @@ class SqliteTransaction implements StorageTransaction {
539
309
  }
540
310
  throw error;
541
311
  } finally {
542
- this.#open = false;
312
+ this.open = false;
543
313
  this.#release();
544
314
  }
545
315
  }
546
316
 
547
317
  async rollback(): Promise<void> {
548
- if (!this.#open) return;
549
- this.#open = false;
318
+ if (!this.open) return;
319
+ this.open = false;
550
320
  try {
551
321
  this.#storage.db.exec('ROLLBACK');
552
322
  } finally {
@@ -555,50 +325,55 @@ class SqliteTransaction implements StorageTransaction {
555
325
  }
556
326
  }
557
327
 
558
- export class SqliteServerStorage implements ServerStorage {
328
+ export class SqliteServerStorage
329
+ extends SqliteFamilyStorage
330
+ implements ServerStorage
331
+ {
559
332
  readonly db: SqliteDatabase;
560
- /** One SQLite connection can own only one transaction at a time. */
561
- #transactionTail: Promise<void> = Promise.resolve();
562
- /** Set by `ensureSchema`: app-table lookup for the relational row store. */
563
- #tables: ReadonlyMap<string, CompiledTable> | undefined;
564
- #schemaVersion: number | undefined;
333
+ protected readonly maxBindParams = Number.POSITIVE_INFINITY;
334
+ /**
335
+ * §6.4: one SQLite connection serves one caller at a time. A transaction
336
+ * holds it from `begin` until commit or rollback; every other call waits
337
+ * behind it in FIFO order, so no request reads another's uncommitted rows
338
+ * and no write joins another's open transaction. A call on an idle
339
+ * connection takes it synchronously and runs at call time.
340
+ */
341
+ #busy = false;
342
+ readonly #waiters: (() => void)[] = [];
565
343
 
566
- async #serializeWrite<T>(operation: () => T): Promise<T> {
567
- const previous = this.#transactionTail;
568
- let release!: () => void;
569
- this.#transactionTail = new Promise<void>((resolve) => {
570
- release = resolve;
571
- });
572
- await previous;
573
- try {
574
- return operation();
575
- } finally {
576
- release();
344
+ /** `undefined` when the caller owns the connection now. */
345
+ #acquire(): Promise<void> | undefined {
346
+ if (!this.#busy) {
347
+ this.#busy = true;
348
+ return undefined;
577
349
  }
350
+ return new Promise<void>((resolve) => {
351
+ this.#waiters.push(resolve);
352
+ });
353
+ }
354
+
355
+ /** Hand the connection to the next waiter, or mark it idle. */
356
+ #release(): void {
357
+ const next = this.#waiters.shift();
358
+ if (next === undefined) this.#busy = false;
359
+ else next();
578
360
  }
579
361
 
580
362
  constructor(db: SqliteDatabase | string = ':memory:') {
581
363
  if (typeof db === 'string') {
582
364
  throw new SqliteAdapterRequiredError();
583
365
  }
366
+ super();
584
367
  this.db = db;
585
368
  this.db.exec(SQLITE_DDL);
586
- const clientColumns = this.db
587
- .query<{ name: string }, []>('PRAGMA table_info("sync_clients")')
588
- .all();
589
- if (!clientColumns.some((column) => column.name === 'wire_version')) {
590
- this.db.exec(
591
- 'ALTER TABLE sync_clients ADD COLUMN wire_version INTEGER NOT NULL DEFAULT 1',
592
- );
593
- }
594
- for (const [name, definition] of Object.entries(
595
- SYNC_CLIENT_ROUND_COLUMNS,
369
+ for (const statement of sqliteUpgradeDdl(
370
+ this.db
371
+ .query<{ tbl: string; name: string }, SqliteValue[]>(
372
+ SQLITE_UPGRADE_PROBE.sql,
373
+ )
374
+ .all(...SQLITE_UPGRADE_PROBE.params),
596
375
  )) {
597
- if (!clientColumns.some((column) => column.name === name)) {
598
- this.db.exec(
599
- `ALTER TABLE sync_clients ADD COLUMN ${name} ${definition}`,
600
- );
601
- }
376
+ this.db.exec(statement);
602
377
  }
603
378
  const commitColumns = this.db
604
379
  .query<{ name: string }, []>('PRAGMA table_info("sync_commits")')
@@ -626,17 +401,69 @@ WHEN EXISTS (
626
401
  BEGIN
627
402
  SELECT RAISE(ABORT, 'sync.storage.writer_fence_rejected');
628
403
  END`);
404
+ // Callers receive this proxy: every method except the ones below runs
405
+ // under `#acquire`. Methods execute against the raw instance, so a
406
+ // transaction (constructed with the raw instance) and a method's own
407
+ // internal calls never queue behind themselves.
408
+ return new Proxy(this, {
409
+ get: (target, property) => {
410
+ const value: unknown = Reflect.get(target, property, target);
411
+ // An own property is a caller's override (a test double or host
412
+ // wrapper); it reaches the queue through the original it calls.
413
+ if (typeof value !== 'function' || Object.hasOwn(target, property)) {
414
+ return value;
415
+ }
416
+ if (
417
+ property === 'begin' ||
418
+ property === 'table' ||
419
+ property === 'runAuthoritativeQuery' ||
420
+ property === 'constructor'
421
+ ) {
422
+ return value.bind(target);
423
+ }
424
+ return (...args: unknown[]): Promise<unknown> => {
425
+ const run = async () => {
426
+ try {
427
+ return await Reflect.apply(value, target, args);
428
+ } finally {
429
+ target.#release();
430
+ }
431
+ };
432
+ return target.#acquire()?.then(run) ?? run();
433
+ };
434
+ },
435
+ });
629
436
  }
630
437
 
631
- /** Resolve a table's compiled schema; row operations require `ensureSchema`. */
632
- table(name: string): CompiledTable {
633
- const table = this.#tables?.get(name);
634
- if (table === undefined) {
635
- throw new Error(
636
- `unknown table ${JSON.stringify(name)} — ensureSchema(schema) must run before row operations`,
438
+ protected async all<T>(
439
+ sql: string,
440
+ params: readonly SqliteValue[],
441
+ ): Promise<T[]> {
442
+ return this.db.query<T, SqliteValue[]>(sql).all(...params);
443
+ }
444
+
445
+ protected guardedAll<T>(
446
+ sql: string,
447
+ params: readonly SqliteValue[],
448
+ ): Promise<T[]> {
449
+ return this.all<T>(sql, params);
450
+ }
451
+
452
+ protected async atomic(
453
+ statements: readonly SqliteStatementSpec[],
454
+ ): Promise<unknown[][]> {
455
+ this.db.exec('BEGIN IMMEDIATE');
456
+ try {
457
+ const results = statements.map(({ sql, params }) =>
458
+ this.db.query<unknown, SqliteValue[]>(sql).all(...params),
637
459
  );
460
+ this.db.exec('COMMIT');
461
+ return results;
462
+ } catch (error) {
463
+ rollbackAfterError(() => {
464
+ this.db.exec('ROLLBACK');
465
+ }, error);
638
466
  }
639
- return table;
640
467
  }
641
468
 
642
469
  async ensureSchema(
@@ -644,7 +471,7 @@ END`);
644
471
  checkpoints?: readonly CheckpointDeclaration[],
645
472
  ): Promise<void> {
646
473
  // Memoized fast path: same instance, same schema version.
647
- if (this.#schemaVersion === schema.version) return;
474
+ if (this.schemaVersion === schema.version) return;
648
475
  // The barrier reads stored state: a checkpoint at this schema version that
649
476
  // is not activated and that this caller did not declare means this process
650
477
  // must not serve. An omitted or empty declaration set declares nothing and
@@ -721,17 +548,15 @@ END`);
721
548
  ),
722
549
  );
723
550
  if (reconcile.length > 0) {
724
- await this.#serializeWrite(() => {
725
- this.db.exec('BEGIN IMMEDIATE');
726
- try {
727
- for (const statement of reconcile) this.db.exec(statement);
728
- this.db.exec('COMMIT');
729
- } catch (error) {
730
- rollbackAfterError(() => {
731
- this.db.exec('ROLLBACK');
732
- }, error);
733
- }
734
- });
551
+ this.db.exec('BEGIN IMMEDIATE');
552
+ try {
553
+ for (const statement of reconcile) this.db.exec(statement);
554
+ this.db.exec('COMMIT');
555
+ } catch (error) {
556
+ rollbackAfterError(() => {
557
+ this.db.exec('ROLLBACK');
558
+ }, error);
559
+ }
735
560
  }
736
561
  }
737
562
  if (marker === null || marker.schema_version < schema.version) {
@@ -768,206 +593,95 @@ END`);
768
593
  }
769
594
  }
770
595
  // The migration rewrites application rows (`#rewriteRows`) with no
771
- // commit log entry. Serialize it through the same writer queue as
772
- // pushes and prune (`#serializeWrite`) rather than trusting the
773
- // migration as privileged; `BEGIN IMMEDIATE` then owns SQLite's writer
774
- // lock for the whole rewrite.
775
- await this.#serializeWrite(() => {
776
- this.db.exec('BEGIN IMMEDIATE');
777
- try {
778
- // Another process sharing the database may have bumped between the
779
- // marker read above and the writer lock; the locked marker decides
780
- // (SPEC "Schema upgrade writer fence").
781
- const locked = this.db
782
- .query<{ schema_version: number; layouts: string }, []>(
783
- 'SELECT schema_version, layouts FROM sync_schema_meta WHERE id=1',
784
- )
785
- .get();
786
- if (locked !== null && locked.schema_version > schema.version) {
787
- throw new Error(
788
- `stored schema version ${locked.schema_version} is newer than the configured schema (${schema.version}) — refusing to run an older server against a migrated database`,
789
- );
790
- }
791
- if (locked !== null && locked.schema_version === schema.version) {
792
- this.db.exec('COMMIT');
793
- return;
794
- }
795
- const layouts = parseLayouts(locked?.layouts);
796
- const retiredTables = retiredTableNames(schema, layouts);
797
- for (const tableName of retiredTables) {
798
- this.db
799
- .query('DELETE FROM sync_row_scopes WHERE tbl=?')
800
- .run(tableName);
801
- this.db
802
- .query('DELETE FROM sync_blob_refs WHERE tbl=?')
803
- .run(tableName);
804
- this.db.exec(dropTableDdl(tableName));
805
- }
806
- for (const statement of schemaDdl(
807
- schema,
808
- existing,
809
- 'sqlite',
810
- existingIndexes,
811
- )) {
812
- this.db.exec(statement);
813
- }
814
- for (const table of schema.tables.values()) {
815
- const oldLayout = layouts[table.name];
816
- const plan = rewritePlan(
817
- table,
818
- oldLayout,
819
- existing.get(table.name),
820
- );
821
- if (!plan.migrate && !plan.backfill) continue;
822
- this.#rewriteRows(table, plan.migrate ? oldLayout : undefined);
823
- }
596
+ // commit log entry. It runs behind the same connection queue as pushes
597
+ // and prune rather than trusting the migration as privileged;
598
+ // `BEGIN IMMEDIATE` then owns SQLite's writer lock for the whole
599
+ // rewrite.
600
+ this.db.exec('BEGIN IMMEDIATE');
601
+ try {
602
+ // Another process sharing the database may have bumped between the
603
+ // marker read above and the writer lock; the locked marker decides
604
+ // (SPEC "Schema upgrade writer fence").
605
+ const locked = this.db
606
+ .query<{ schema_version: number; layouts: string }, []>(
607
+ 'SELECT schema_version, layouts FROM sync_schema_meta WHERE id=1',
608
+ )
609
+ .get();
610
+ if (locked !== null && locked.schema_version > schema.version) {
611
+ throw new Error(
612
+ `stored schema version ${locked.schema_version} is newer than the configured schema (${schema.version}) — refusing to run an older server against a migrated database`,
613
+ );
614
+ }
615
+ if (locked !== null && locked.schema_version === schema.version) {
616
+ this.db.exec('COMMIT');
617
+ this.tables = schema.tables;
618
+ this.schemaVersion = schema.version;
619
+ return;
620
+ }
621
+ const layouts = parseLayouts(locked?.layouts);
622
+ const retiredTables = retiredTableNames(schema, layouts);
623
+ for (const tableName of retiredTables) {
824
624
  this.db
825
- .query(
826
- 'INSERT INTO sync_schema_meta(id, schema_version, layouts) VALUES (1, ?, ?) ON CONFLICT(id) DO UPDATE SET schema_version=excluded.schema_version, layouts=excluded.layouts',
827
- )
828
- .run(schema.version, layoutsOf(schema));
829
- // A writer of the previous schema would append payloads in the
830
- // previous layout; the bump fences every partition at the new
831
- // version (see PostgresServerStorage.ensureSchema).
625
+ .query('DELETE FROM sync_row_scopes WHERE tbl=?')
626
+ .run(tableName);
832
627
  this.db
833
- .query(
834
- `INSERT INTO sync_writer_fence(partition, required_writer_version)
835
- SELECT partition, ? FROM sync_partitions WHERE true
836
- ON CONFLICT(partition) DO UPDATE SET
837
- required_writer_version=max(
838
- sync_writer_fence.required_writer_version,
839
- excluded.required_writer_version)`,
840
- )
841
- .run(schema.version);
842
- // The host's declared checkpoints install in the schema-bump
843
- // transaction, so no observer sees the bumped marker without the
844
- // fence, or the fence without its declaration row.
845
- const installedAtMs = Date.now();
846
- for (const declaration of checkpoints ?? []) {
847
- this.#installCheckpoint(
848
- declaration.partition,
849
- declaration.name,
850
- declaration.schemaVersion,
851
- installedAtMs,
852
- );
853
- }
854
- this.db.exec('COMMIT');
855
- } catch (error) {
856
- rollbackAfterError(() => {
857
- this.db.exec('ROLLBACK');
858
- }, error);
628
+ .query('DELETE FROM sync_blob_refs WHERE tbl=?')
629
+ .run(tableName);
630
+ this.db.exec(dropTableDdl(tableName));
631
+ }
632
+ for (const statement of schemaDdl(
633
+ schema,
634
+ existing,
635
+ 'sqlite',
636
+ existingIndexes,
637
+ )) {
638
+ this.db.exec(statement);
639
+ }
640
+ for (const table of schema.tables.values()) {
641
+ const oldLayout = layouts[table.name];
642
+ const plan = rewritePlan(table, oldLayout, existing.get(table.name));
643
+ if (!plan.migrate && !plan.backfill) continue;
644
+ this.#rewriteRows(table, plan.migrate ? oldLayout : undefined);
859
645
  }
860
- });
861
- }
862
- this.#tables = schema.tables;
863
- this.#schemaVersion = schema.version;
864
- }
865
-
866
- async touchPartition(
867
- partition: string,
868
- authenticatedAtMs: number,
869
- initialLogEpoch: string,
870
- ): Promise<PartitionRegistryEntry> {
871
- if (initialLogEpoch.length === 0) {
872
- throw new Error('initial log epoch must be non-empty');
873
- }
874
- const select = this.db.query<
875
- {
876
- log_epoch: string;
877
- epoch_required: number;
878
- last_authenticated_at_ms: number;
879
- },
880
- [string]
881
- >(
882
- `SELECT log_epoch, epoch_required, last_authenticated_at_ms
883
- FROM sync_partition_registry WHERE partition=?`,
884
- );
885
- let row = select.get(partition);
886
- if (
887
- row === null ||
888
- authenticatedAtMs - row.last_authenticated_at_ms >=
889
- PARTITION_TOUCH_INTERVAL_MS
890
- ) {
891
- this.db
892
- .query(
893
- `INSERT INTO sync_partition_registry(
894
- partition, log_epoch, last_authenticated_at_ms
895
- ) VALUES (?,?,?)
896
- ON CONFLICT(partition) DO UPDATE SET
897
- last_authenticated_at_ms=excluded.last_authenticated_at_ms`,
898
- )
899
- .run(partition, initialLogEpoch, authenticatedAtMs);
900
- row = select.get(partition);
901
- }
902
- if (row === null)
903
- throw new Error('partition registry write did not persist');
904
- return {
905
- partition,
906
- logEpoch: row.log_epoch,
907
- epochRequired: row.epoch_required === 1,
908
- lastAuthenticatedAtMs: row.last_authenticated_at_ms,
909
- };
910
- }
911
-
912
- async rotatePartitionLogEpoch(
913
- partition: string,
914
- logEpoch: string,
915
- authenticatedAtMs: number,
916
- ): Promise<PartitionRegistryEntry> {
917
- if (logEpoch.length === 0) throw new Error('log epoch must be non-empty');
918
- return this.#serializeWrite(() => {
919
- this.db.exec('BEGIN IMMEDIATE');
920
- try {
921
646
  this.db
922
647
  .query(
923
- `INSERT INTO sync_partition_registry(
924
- partition, log_epoch, epoch_required, last_authenticated_at_ms
925
- ) VALUES (?,?,1,?)
926
- ON CONFLICT(partition) DO UPDATE SET
927
- log_epoch=excluded.log_epoch,
928
- epoch_required=1,
929
- last_authenticated_at_ms=excluded.last_authenticated_at_ms`,
648
+ 'INSERT INTO sync_schema_meta(id, schema_version, layouts) VALUES (1, ?, ?) ON CONFLICT(id) DO UPDATE SET schema_version=excluded.schema_version, layouts=excluded.layouts',
930
649
  )
931
- .run(partition, logEpoch, authenticatedAtMs);
650
+ .run(schema.version, layoutsOf(schema));
651
+ // A writer of the previous schema would append payloads in the
652
+ // previous layout; the bump fences every partition at the new
653
+ // version (see PostgresServerStorage.ensureSchema).
932
654
  this.db
933
- .query('DELETE FROM sync_clients WHERE partition=?')
934
- .run(partition);
655
+ .query(
656
+ `INSERT INTO sync_writer_fence(partition, required_writer_version)
657
+ SELECT partition, ? FROM sync_partitions WHERE true
658
+ ON CONFLICT(partition) DO UPDATE SET
659
+ required_writer_version=max(
660
+ sync_writer_fence.required_writer_version,
661
+ excluded.required_writer_version)`,
662
+ )
663
+ .run(schema.version);
664
+ // The host's declared checkpoints install in the schema-bump
665
+ // transaction, so no observer sees the bumped marker without the
666
+ // fence, or the fence without its declaration row.
667
+ const installedAtMs = Date.now();
668
+ for (const declaration of checkpoints ?? []) {
669
+ this.#installCheckpoint(
670
+ declaration.partition,
671
+ declaration.name,
672
+ declaration.schemaVersion,
673
+ installedAtMs,
674
+ );
675
+ }
935
676
  this.db.exec('COMMIT');
936
677
  } catch (error) {
937
678
  rollbackAfterError(() => {
938
679
  this.db.exec('ROLLBACK');
939
680
  }, error);
940
681
  }
941
- return {
942
- partition,
943
- logEpoch,
944
- epochRequired: true,
945
- lastAuthenticatedAtMs: authenticatedAtMs,
946
- };
947
- });
948
- }
949
-
950
- async listPartitionRegistry(): Promise<PartitionRegistryEntry[]> {
951
- return this.db
952
- .query<
953
- {
954
- partition: string;
955
- log_epoch: string;
956
- epoch_required: number;
957
- last_authenticated_at_ms: number;
958
- },
959
- []
960
- >(
961
- `SELECT partition, log_epoch, epoch_required, last_authenticated_at_ms
962
- FROM sync_partition_registry ORDER BY partition`,
963
- )
964
- .all()
965
- .map((row) => ({
966
- partition: row.partition,
967
- logEpoch: row.log_epoch,
968
- epochRequired: row.epoch_required === 1,
969
- lastAuthenticatedAtMs: row.last_authenticated_at_ms,
970
- }));
682
+ }
683
+ this.tables = schema.tables;
684
+ this.schemaVersion = schema.version;
971
685
  }
972
686
 
973
687
  /**
@@ -999,13 +713,7 @@ END`);
999
713
  ? migratePayload(oldLayout, table, row.payload)
1000
714
  : row.payload;
1001
715
  update.run(
1002
- ...(rewriteValues(
1003
- table,
1004
- row.partition,
1005
- row.row_id,
1006
- payload,
1007
- 'sqlite',
1008
- ) as (string | number | boolean | Uint8Array | null)[]),
716
+ ...rewriteValues(table, row.partition, row.row_id, payload, 'sqlite'),
1009
717
  );
1010
718
  }
1011
719
  const last = rows[rows.length - 1];
@@ -1020,60 +728,20 @@ END`);
1020
728
  // database file, so one FIFO over the shared connection is the correct
1021
729
  // serialization unit — a per-partition queue would still contend on the
1022
730
  // same BEGIN IMMEDIATE writer lock.
1023
- const previous = this.#transactionTail;
1024
- let release!: () => void;
1025
- this.#transactionTail = new Promise<void>((resolve) => {
1026
- release = resolve;
1027
- });
1028
- await previous;
731
+ await this.#acquire();
1029
732
  try {
1030
733
  return new SqliteTransaction(
1031
734
  this,
1032
735
  partition,
1033
- this.#schemaVersion ?? 0,
1034
- release,
736
+ this.schemaVersion ?? 0,
737
+ () => this.#release(),
1035
738
  );
1036
739
  } catch (error) {
1037
- release();
740
+ this.#release();
1038
741
  throw error;
1039
742
  }
1040
743
  }
1041
744
 
1042
- /** Internal: write a row + refresh its scope-index entries. */
1043
- writeRow(partition: string, table: string, row: StoredRow): void {
1044
- const compiled = this.table(table);
1045
- this.db
1046
- .query(deleteSqliteRowScopesSql(compiled))
1047
- .run(partition, table, row.rowId, partition, row.rowId);
1048
- this.db
1049
- .query(upsertSql(compiled, 'sqlite'))
1050
- .run(
1051
- ...(upsertValues(compiled, partition, row, 'sqlite') as (
1052
- | string
1053
- | number
1054
- | boolean
1055
- | Uint8Array
1056
- | null
1057
- )[]),
1058
- );
1059
- for (const [variable, value] of Object.entries(row.scopes)) {
1060
- this.db
1061
- .query(
1062
- 'INSERT OR IGNORE INTO sync_row_scopes(partition, tbl, var, value, row_id) VALUES (?,?,?,?,?)',
1063
- )
1064
- .run(partition, table, variable, value, row.rowId);
1065
- }
1066
- }
1067
-
1068
- async getMaxCommitSeq(partition: string): Promise<number> {
1069
- const row = this.db
1070
- .query<{ max_commit_seq: number }, [string]>(
1071
- 'SELECT max_commit_seq FROM sync_partitions WHERE partition=?',
1072
- )
1073
- .get(partition);
1074
- return row?.max_commit_seq ?? 0;
1075
- }
1076
-
1077
745
  /**
1078
746
  * Internal: rewrite and run one registered query on the shared connection.
1079
747
  * The caller owns the transaction: `queryAuthoritative` opens a read
@@ -1084,20 +752,7 @@ END`);
1084
752
  partition: string,
1085
753
  query: AuthoritativeQueryRequest,
1086
754
  ): AuthoritativeQueryResult {
1087
- if (this.#tables === undefined) {
1088
- throw new Error(
1089
- 'ensureSchema(schema) must run before registered queries',
1090
- );
1091
- }
1092
- const prepared = bindAuthoritativePartition(
1093
- prepareAuthoritativeQuery(
1094
- query.plan,
1095
- query.params,
1096
- query.tables,
1097
- this.#tables,
1098
- ),
1099
- partition,
1100
- );
755
+ const prepared = this.prepareAuthoritativeQuery(partition, query);
1101
756
  const rows = this.db
1102
757
  .query<Readonly<Record<string, unknown>>, AuthoritativeQueryValue[]>(
1103
758
  prepared.sql,
@@ -1116,12 +771,6 @@ END`);
1116
771
  query: AuthoritativeQueryRequest,
1117
772
  checkpoints?: readonly CheckpointDeclaration[],
1118
773
  ): Promise<AuthoritativeQueryResult> {
1119
- const previous = this.#transactionTail;
1120
- let release!: () => void;
1121
- this.#transactionTail = new Promise<void>((resolve) => {
1122
- release = resolve;
1123
- });
1124
- await previous;
1125
774
  let open = false;
1126
775
  try {
1127
776
  this.db.exec('BEGIN');
@@ -1129,7 +778,7 @@ END`);
1129
778
  // RFC 0007: evaluate the gate on this connection after BEGIN. SQLite is
1130
779
  // single-writer and the shared connection serializes writers, so the
1131
780
  // gate read and the query read cannot straddle a migration.
1132
- const runningVersion = this.#schemaVersion ?? 0;
781
+ const runningVersion = this.schemaVersion ?? 0;
1133
782
  const gate = await this.readServeGate(partition, runningVersion);
1134
783
  const refusal = serveGateRefusal(gate, runningVersion, checkpoints);
1135
784
  if (refusal !== undefined) throw serveNotReadyError(refusal);
@@ -1141,133 +790,26 @@ END`);
1141
790
  rollbackAfterError(() => {
1142
791
  if (open) this.db.exec('ROLLBACK');
1143
792
  }, error);
1144
- } finally {
1145
- release();
1146
793
  }
1147
794
  }
1148
795
 
1149
- async getPartitionLogEpoch(partition: string): Promise<string | undefined> {
1150
- return this.db
1151
- .query<{ log_epoch: string }, [string]>(
1152
- 'SELECT log_epoch FROM sync_partition_registry WHERE partition=?',
1153
- )
1154
- .get(partition)?.log_epoch;
1155
- }
1156
-
1157
- async getHorizonSeq(partition: string): Promise<number> {
1158
- const row = this.db
1159
- .query<{ horizon_seq: number }, [string]>(
1160
- 'SELECT horizon_seq FROM sync_partitions WHERE partition=?',
1161
- )
1162
- .get(partition);
1163
- return row?.horizon_seq ?? 0;
1164
- }
1165
-
1166
- async setHorizonSeq(partition: string, seq: number): Promise<void> {
1167
- await this.#serializeWrite(() => {
1168
- this.db
1169
- .query(`INSERT INTO sync_partitions(partition, horizon_seq) VALUES (?,?)
1170
- ON CONFLICT(partition) DO UPDATE SET horizon_seq=max(horizon_seq,excluded.horizon_seq)`)
1171
- .run(partition, seq);
1172
- });
1173
- }
1174
-
1175
- async pruneCommitsThrough(
1176
- partition: string,
1177
- query: CommitPruneQuery,
1178
- ): Promise<CommitPruneResult> {
1179
- validateCommitPruneQuery(query);
1180
- return this.#serializeWrite(() => {
1181
- this.db.exec('BEGIN IMMEDIATE');
1182
- try {
1183
- const epoch = this.db
1184
- .query<{ log_epoch: string }, [string]>(
1185
- 'SELECT log_epoch FROM sync_partition_registry WHERE partition=?',
1186
- )
1187
- .get(partition)?.log_epoch;
1188
- if (epoch !== query.logEpoch)
1189
- throw new StorageQueryError('sync.storage.prune_epoch_mismatch');
1190
- const previousHorizonSeq =
1191
- this.db
1192
- .query<{ horizon_seq: number }, [string]>(
1193
- 'SELECT horizon_seq FROM sync_partitions WHERE partition=?',
1194
- )
1195
- .get(partition)?.horizon_seq ?? 0;
1196
- const horizonSeq = Math.max(previousHorizonSeq, query.throughSeq);
1197
- this.db
1198
- .query(`INSERT INTO sync_partitions(partition, horizon_seq) VALUES (?,?)
1199
- ON CONFLICT(partition) DO UPDATE SET horizon_seq=excluded.horizon_seq`)
1200
- .run(partition, horizonSeq);
1201
- const removed = this.db
1202
- .query('DELETE FROM sync_commits WHERE partition=? AND commit_seq<=?')
1203
- .run(partition, horizonSeq);
1204
- this.db
1205
- .query('DELETE FROM sync_changes WHERE partition=? AND commit_seq<=?')
1206
- .run(partition, horizonSeq);
1207
- this.db
1208
- .query(
1209
- 'DELETE FROM sync_change_scopes WHERE partition=? AND commit_seq<=?',
1210
- )
1211
- .run(partition, horizonSeq);
1212
- // §4.6: tombstones prune in the same pass as the commit log.
1213
- this.db
1214
- .query(
1215
- 'DELETE FROM sync_tombstones WHERE partition=? AND commit_seq<=?',
1216
- )
1217
- .run(partition, horizonSeq);
1218
- this.db.exec('COMMIT');
1219
- return {
1220
- previousHorizonSeq,
1221
- horizonSeq,
1222
- removedCommits: Number(removed.changes),
1223
- };
1224
- } catch (error) {
1225
- rollbackAfterError(() => {
1226
- this.db.exec('ROLLBACK');
1227
- }, error);
1228
- }
1229
- });
1230
- }
1231
-
1232
- async getCommitSeqBefore(
1233
- partition: string,
1234
- createdBeforeMs: number,
1235
- ): Promise<number> {
1236
- const row = this.db
1237
- .query<{ seq: number | null }, [string, number]>(
1238
- 'SELECT max(commit_seq) AS seq FROM sync_commits WHERE partition=? AND created_at_ms<?',
1239
- )
1240
- .get(partition, createdBeforeMs);
1241
- return row?.seq ?? 0;
1242
- }
1243
-
1244
796
  async readServeGate(
1245
797
  partition: string,
1246
798
  runningSchemaVersion: number,
1247
799
  ): Promise<ServeGate> {
1248
- const marker = this.db
1249
- .query<{ schema_version: number }, []>(
1250
- 'SELECT schema_version FROM sync_schema_meta WHERE id=1',
1251
- )
1252
- .get();
1253
- const epoch = this.db
1254
- .query<{ log_epoch: string }, [string]>(
1255
- 'SELECT log_epoch FROM sync_partition_registry WHERE partition=?',
1256
- )
1257
- .get(partition);
1258
- const pending = this.db
1259
- .query<{ partition: string; name: string }, [number]>(
1260
- `SELECT partition, name FROM sync_backfill_checkpoints
1261
- WHERE schema_version=? AND state<>'activated'`,
1262
- )
1263
- .all(runningSchemaVersion);
800
+ const [marker] = await this.all<{ schema_version: number }>(
801
+ 'SELECT schema_version FROM sync_schema_meta WHERE id=1',
802
+ [],
803
+ );
804
+ const pending = await this.all<{ partition: string; name: string }>(
805
+ `SELECT partition, name FROM sync_backfill_checkpoints
806
+ WHERE schema_version=? AND state<>'activated'`,
807
+ [runningSchemaVersion],
808
+ );
1264
809
  return {
1265
810
  storedSchemaVersion: marker?.schema_version ?? 0,
1266
- logEpoch: epoch?.log_epoch,
1267
- pending: pending.map((row) => ({
1268
- partition: row.partition,
1269
- name: row.name,
1270
- })),
811
+ logEpoch: await this.getPartitionLogEpoch(partition),
812
+ pending,
1271
813
  };
1272
814
  }
1273
815
 
@@ -1324,27 +866,25 @@ END`);
1324
866
  // One transaction: the checkpoint becomes visible and the fence that
1325
867
  // protects it is raised together. The fence is raised at declaration, not
1326
868
  // at activation, so old writers are rejected for the whole backfill.
1327
- return this.#serializeWrite(() => {
1328
- this.db.exec('BEGIN IMMEDIATE');
1329
- try {
1330
- this.#installCheckpoint(partition, name, schemaVersion, nowMs);
1331
- const row = this.db
1332
- .query<SqliteCheckpointRecord, [string, string]>(
1333
- `SELECT partition, name, schema_version, state, watermark,
1334
- owner_epoch, observed_rows, updated_at_ms
1335
- FROM sync_backfill_checkpoints WHERE partition=? AND name=?`,
1336
- )
1337
- .get(partition, name);
1338
- if (row === null)
1339
- throw new StorageQueryError('sync.storage.checkpoint_not_declared');
1340
- this.db.exec('COMMIT');
1341
- return toStoredCheckpoint(row);
1342
- } catch (error) {
1343
- rollbackAfterError(() => {
1344
- this.db.exec('ROLLBACK');
1345
- }, error);
1346
- }
1347
- });
869
+ this.db.exec('BEGIN IMMEDIATE');
870
+ try {
871
+ this.#installCheckpoint(partition, name, schemaVersion, nowMs);
872
+ const row = this.db
873
+ .query<SqliteCheckpointRecord, [string, string]>(
874
+ `SELECT partition, name, schema_version, state, watermark,
875
+ owner_epoch, observed_rows, updated_at_ms
876
+ FROM sync_backfill_checkpoints WHERE partition=? AND name=?`,
877
+ )
878
+ .get(partition, name);
879
+ if (row === null)
880
+ throw new StorageQueryError('sync.storage.checkpoint_not_declared');
881
+ this.db.exec('COMMIT');
882
+ return toStoredCheckpoint(row);
883
+ } catch (error) {
884
+ rollbackAfterError(() => {
885
+ this.db.exec('ROLLBACK');
886
+ }, error);
887
+ }
1348
888
  }
1349
889
 
1350
890
  async activateCheckpoint(
@@ -1355,55 +895,53 @@ END`);
1355
895
  sources: readonly string[],
1356
896
  nowMs: number,
1357
897
  ): Promise<'activated' | 'stale' | 'unverifiable'> {
1358
- return this.#serializeWrite(() => {
1359
- // `BEGIN IMMEDIATE` is SQLite's partition write lock; the same writer
1360
- // lock a push takes. Nothing may observe activation without the fence.
1361
- this.db.exec('BEGIN IMMEDIATE');
1362
- try {
1363
- const fence = this.db
1364
- .query<{ required_writer_version: number }, [string]>(
1365
- 'SELECT required_writer_version FROM sync_writer_fence WHERE partition=?',
1366
- )
1367
- .get(partition);
1368
- const checkpoint = this.db
1369
- .query<{ schema_version: number }, [string, string]>(
1370
- 'SELECT schema_version FROM sync_backfill_checkpoints WHERE partition=? AND name=?',
1371
- )
1372
- .get(partition, name);
1373
- if (
1374
- fence === null ||
1375
- checkpoint === null ||
1376
- fence.required_writer_version < checkpoint.schema_version
1377
- ) {
1378
- // The barrier is not installed. Activating would certify a state old
1379
- // writers can still decay; refuse instead of silently proceeding.
1380
- throw new StorageQueryError('sync.storage.checkpoint_fence_missing');
1381
- }
1382
- const coverage = this.#hasSourceChangesAbove(
1383
- partition,
1384
- sources,
1385
- watermark,
1386
- );
1387
- if (coverage !== 'clean') {
1388
- this.db.exec('ROLLBACK');
1389
- return coverage === 'changed' ? 'stale' : 'unverifiable';
1390
- }
1391
- const updated = this.db
1392
- .query(
1393
- `UPDATE sync_backfill_checkpoints
1394
- SET state='activated', watermark=?, updated_at_ms=?
1395
- WHERE partition=? AND name=? AND owner_epoch=?
1396
- AND state<>'activated'`,
1397
- )
1398
- .run(watermark, nowMs, partition, name, ownerEpoch);
1399
- this.db.exec('COMMIT');
1400
- return Number(updated.changes) === 1 ? 'activated' : 'stale';
1401
- } catch (error) {
1402
- rollbackAfterError(() => {
1403
- this.db.exec('ROLLBACK');
1404
- }, error);
898
+ // `BEGIN IMMEDIATE` is SQLite's partition write lock; the same writer
899
+ // lock a push takes. Nothing may observe activation without the fence.
900
+ this.db.exec('BEGIN IMMEDIATE');
901
+ try {
902
+ const fence = this.db
903
+ .query<{ required_writer_version: number }, [string]>(
904
+ 'SELECT required_writer_version FROM sync_writer_fence WHERE partition=?',
905
+ )
906
+ .get(partition);
907
+ const checkpoint = this.db
908
+ .query<{ schema_version: number }, [string, string]>(
909
+ 'SELECT schema_version FROM sync_backfill_checkpoints WHERE partition=? AND name=?',
910
+ )
911
+ .get(partition, name);
912
+ if (
913
+ fence === null ||
914
+ checkpoint === null ||
915
+ fence.required_writer_version < checkpoint.schema_version
916
+ ) {
917
+ // The barrier is not installed. Activating would certify a state old
918
+ // writers can still decay; refuse instead of silently proceeding.
919
+ throw new StorageQueryError('sync.storage.checkpoint_fence_missing');
1405
920
  }
1406
- });
921
+ const coverage = await this.hasSourceChangesAbove(
922
+ partition,
923
+ sources,
924
+ watermark,
925
+ );
926
+ if (coverage !== 'clean') {
927
+ this.db.exec('ROLLBACK');
928
+ return coverage === 'changed' ? 'stale' : 'unverifiable';
929
+ }
930
+ const updated = this.db
931
+ .query(
932
+ `UPDATE sync_backfill_checkpoints
933
+ SET state='activated', watermark=?, updated_at_ms=?
934
+ WHERE partition=? AND name=? AND owner_epoch=?
935
+ AND state<>'activated'`,
936
+ )
937
+ .run(watermark, nowMs, partition, name, ownerEpoch);
938
+ this.db.exec('COMMIT');
939
+ return Number(updated.changes) === 1 ? 'activated' : 'stale';
940
+ } catch (error) {
941
+ rollbackAfterError(() => {
942
+ this.db.exec('ROLLBACK');
943
+ }, error);
944
+ }
1407
945
  }
1408
946
 
1409
947
  async claimCheckpoint(
@@ -1412,23 +950,21 @@ END`);
1412
950
  schemaVersion: number,
1413
951
  nowMs: number,
1414
952
  ): Promise<StoredCheckpoint> {
1415
- return this.#serializeWrite(() => {
1416
- const claimed = this.db
1417
- .query<SqliteCheckpointRecord, (string | number)[]>(
1418
- `UPDATE sync_backfill_checkpoints
1419
- SET owner_epoch=owner_epoch+1,
1420
- state=CASE WHEN state='declared' THEN 'backfilling' ELSE state END,
1421
- schema_version=?, updated_at_ms=?
1422
- WHERE partition=? AND name=? AND state<>'activated'
1423
- RETURNING partition, name, schema_version, state, watermark,
1424
- owner_epoch, observed_rows, updated_at_ms`,
1425
- )
1426
- .get(schemaVersion, nowMs, partition, name);
1427
- if (claimed === null) {
1428
- throw new StorageQueryError('sync.storage.checkpoint_not_declared');
1429
- }
1430
- return toStoredCheckpoint(claimed);
1431
- });
953
+ const claimed = this.db
954
+ .query<SqliteCheckpointRecord, (string | number)[]>(
955
+ `UPDATE sync_backfill_checkpoints
956
+ SET owner_epoch=owner_epoch+1,
957
+ state=CASE WHEN state='declared' THEN 'backfilling' ELSE state END,
958
+ schema_version=?, updated_at_ms=?
959
+ WHERE partition=? AND name=? AND state<>'activated'
960
+ RETURNING partition, name, schema_version, state, watermark,
961
+ owner_epoch, observed_rows, updated_at_ms`,
962
+ )
963
+ .get(schemaVersion, nowMs, partition, name);
964
+ if (claimed === null) {
965
+ throw new StorageQueryError('sync.storage.checkpoint_not_declared');
966
+ }
967
+ return toStoredCheckpoint(claimed);
1432
968
  }
1433
969
 
1434
970
  async advanceCheckpoint(
@@ -1439,980 +975,13 @@ END`);
1439
975
  observedRows: number,
1440
976
  nowMs: number,
1441
977
  ): Promise<boolean> {
1442
- return this.#serializeWrite(() => {
1443
- const result = this.db
1444
- .query(
1445
- `UPDATE sync_backfill_checkpoints
1446
- SET watermark=?, observed_rows=?, updated_at_ms=?
1447
- WHERE partition=? AND name=? AND owner_epoch=?`,
1448
- )
1449
- .run(watermark, observedRows, nowMs, partition, name, ownerEpoch);
1450
- return Number(result.changes) === 1;
1451
- });
1452
- }
1453
-
1454
- async sourceCoverageSeq(
1455
- partition: string,
1456
- tables: readonly string[],
1457
- ): Promise<number> {
1458
- if (tables.length === 0) return 0;
1459
- const row = this.db
1460
- .query<{ seq: number | null }, (string | number)[]>(
1461
- `SELECT max(commit_seq) AS seq FROM sync_changes
1462
- WHERE partition=? AND tbl IN (${placeholders(tables.length)})`,
1463
- )
1464
- .get(partition, ...tables);
1465
- return row?.seq ?? 0;
1466
- }
1467
-
1468
- #hasSourceChangesAbove(
1469
- partition: string,
1470
- tables: readonly string[],
1471
- seq: number,
1472
- ): 'clean' | 'changed' | 'unverifiable' {
1473
- // Read the horizon before the window scan: a horizon past `seq` means the
1474
- // history that would answer the question is gone, so an empty scan proves
1475
- // nothing and must not be reported as `clean`.
1476
- const horizon =
1477
- this.db
1478
- .query<{ horizon_seq: number }, [string]>(
1479
- 'SELECT horizon_seq FROM sync_partitions WHERE partition=?',
1480
- )
1481
- .get(partition)?.horizon_seq ?? 0;
1482
- if (tables.length === 0) return 'clean';
1483
- const hit = this.db
1484
- .query<{ hit: number }, (string | number)[]>(
1485
- `SELECT 1 AS hit FROM sync_changes
1486
- WHERE partition=? AND tbl IN (${placeholders(tables.length)})
1487
- AND commit_seq>? LIMIT 1`,
1488
- )
1489
- .get(partition, ...tables, seq);
1490
- if (hit !== null && hit !== undefined) return 'changed';
1491
- return horizon > seq ? 'unverifiable' : 'clean';
1492
- }
1493
-
1494
- async hasSourceChangesAbove(
1495
- partition: string,
1496
- tables: readonly string[],
1497
- seq: number,
1498
- ): Promise<'clean' | 'changed' | 'unverifiable'> {
1499
- return this.#hasSourceChangesAbove(partition, tables, seq);
1500
- }
1501
-
1502
- async getRow(
1503
- partition: string,
1504
- table: string,
1505
- rowId: string,
1506
- ): Promise<StoredRow | undefined> {
1507
- const record = this.db
1508
- .query<SqliteRowRecord, [string, string]>(
1509
- selectRowSql(this.table(table), 'sqlite'),
1510
- )
1511
- .get(partition, rowId);
1512
- return record === null ? undefined : toStoredRow(record);
1513
- }
1514
-
1515
- async getPushResult(
1516
- partition: string,
1517
- clientId: string,
1518
- clientCommitId: string,
1519
- ): Promise<StoredPushResult | undefined> {
1520
- const record = this.db
1521
- .query<{ result: string }, [string, string, string]>(
1522
- 'SELECT result FROM sync_push_results WHERE partition=? AND client_id=? AND client_commit_id=?',
1523
- )
1524
- .get(partition, clientId, clientCommitId);
1525
- if (record === null) return undefined;
1526
- try {
1527
- return deserializePushResult(record.result);
1528
- } catch {
1529
- throw syncError(
1530
- 'sync.idempotency_cache_miss',
1531
- 'persisted push result unreadable (§6.3)',
1532
- );
1533
- }
1534
- }
1535
-
1536
- async claimReactions(
1537
- partition: string,
1538
- query: ReactionClaimQuery,
1539
- ): Promise<StoredReaction[]> {
1540
- if (query.types.length === 0 || query.limit <= 0) return [];
1541
- return this.#serializeWrite(() => {
1542
- const typeParams = query.types.map(() => '?').join(',');
1543
- const records = this.db
1544
- .query<SqliteReactionRecord, (string | number)[]>(
1545
- `UPDATE sync_reactions
1546
- SET status='leased', attempts=attempts+1,
1547
- lease_owner=?, lease_expires_at_ms=?, completed_at_ms=NULL
1548
- WHERE (partition, idempotency_key) IN (
1549
- SELECT partition, idempotency_key
1550
- FROM sync_reactions
1551
- WHERE partition=? AND type IN (${typeParams})
1552
- AND ((status='pending' AND available_at_ms<=?)
1553
- OR (status='leased' AND lease_expires_at_ms<=?))
1554
- ORDER BY CASE WHEN status='leased' THEN lease_expires_at_ms
1555
- ELSE available_at_ms END,
1556
- created_at_ms, idempotency_key
1557
- LIMIT ?
1558
- )
1559
- RETURNING *`,
1560
- )
1561
- .all(
1562
- query.leaseOwner,
1563
- Math.min(
1564
- Number.MAX_SAFE_INTEGER,
1565
- query.nowMs + query.leaseDurationMs,
1566
- ),
1567
- partition,
1568
- ...query.types,
1569
- query.nowMs,
1570
- query.nowMs,
1571
- query.limit,
1572
- );
1573
- return records
1574
- .map(toStoredReaction)
1575
- .sort(
1576
- (a, b) =>
1577
- a.createdAtMs - b.createdAtMs ||
1578
- a.idempotencyKey.localeCompare(b.idempotencyKey),
1579
- );
1580
- });
1581
- }
1582
-
1583
- async completeReaction(
1584
- partition: string,
1585
- idempotencyKey: string,
1586
- leaseOwner: string,
1587
- completedAtMs: number,
1588
- ): Promise<boolean> {
1589
- return this.#serializeWrite(() => {
1590
- const result = this.db
1591
- .query(
1592
- `UPDATE sync_reactions
1593
- SET status='completed', completed_at_ms=?,
1594
- lease_owner=NULL, lease_expires_at_ms=NULL
1595
- WHERE partition=? AND idempotency_key=?
1596
- AND status='leased' AND lease_owner=?`,
1597
- )
1598
- .run(completedAtMs, partition, idempotencyKey, leaseOwner);
1599
- return Number(result.changes) === 1;
1600
- });
1601
- }
1602
-
1603
- async extendReactionLease(
1604
- partition: string,
1605
- idempotencyKey: string,
1606
- leaseOwner: string,
1607
- leaseExpiresAtMs: number,
1608
- ): Promise<boolean> {
1609
- return this.#serializeWrite(() => {
1610
- const result = this.db
1611
- .query(
1612
- `UPDATE sync_reactions SET lease_expires_at_ms=?
1613
- WHERE partition=? AND idempotency_key=?
1614
- AND status='leased' AND lease_owner=?`,
1615
- )
1616
- .run(leaseExpiresAtMs, partition, idempotencyKey, leaseOwner);
1617
- return Number(result.changes) === 1;
1618
- });
1619
- }
1620
-
1621
- async failReaction(
1622
- partition: string,
1623
- idempotencyKey: string,
1624
- update: ReactionFailureUpdate,
1625
- ): Promise<boolean> {
1626
- const retry = update.retryAtMs !== undefined;
1627
- return this.#serializeWrite(() => {
1628
- const result = this.db
1629
- .query(
1630
- `UPDATE sync_reactions
1631
- SET status=?, available_at_ms=?, last_failure=?,
1632
- lease_owner=NULL, lease_expires_at_ms=NULL
1633
- WHERE partition=? AND idempotency_key=?
1634
- AND status='leased' AND lease_owner=?`,
1635
- )
1636
- .run(
1637
- retry ? 'pending' : 'dead-letter',
1638
- update.retryAtMs ?? update.failure.atMs,
1639
- JSON.stringify(update.failure),
1640
- partition,
1641
- idempotencyKey,
1642
- update.leaseOwner,
1643
- );
1644
- return Number(result.changes) === 1;
1645
- });
1646
- }
1647
-
1648
- async retryReaction(
1649
- partition: string,
1650
- idempotencyKey: string,
1651
- nowMs: number,
1652
- ): Promise<boolean> {
1653
- return this.#serializeWrite(() => {
1654
- const result = this.db
1655
- .query(
1656
- `UPDATE sync_reactions
1657
- SET status='pending', attempts=0, available_at_ms=?,
1658
- last_failure=NULL, lease_owner=NULL, lease_expires_at_ms=NULL,
1659
- completed_at_ms=NULL
1660
- WHERE partition=? AND idempotency_key=? AND status='dead-letter'`,
1661
- )
1662
- .run(nowMs, partition, idempotencyKey);
1663
- return Number(result.changes) === 1;
1664
- });
1665
- }
1666
-
1667
- async getReaction(
1668
- partition: string,
1669
- idempotencyKey: string,
1670
- ): Promise<StoredReaction | undefined> {
1671
- const record = this.db
1672
- .query<SqliteReactionRecord, [string, string]>(
1673
- 'SELECT * FROM sync_reactions WHERE partition=? AND idempotency_key=?',
1674
- )
1675
- .get(partition, idempotencyKey);
1676
- return record === null ? undefined : toStoredReaction(record);
1677
- }
1678
-
1679
- async listReactions(
1680
- partition: string,
1681
- query: ReactionListQuery,
1682
- ): Promise<StoredReaction[]> {
1683
- const where = ['partition=?'];
1684
- const params: (string | number)[] = [partition];
1685
- if (query.statuses !== undefined && query.statuses.length > 0) {
1686
- where.push(`status IN (${query.statuses.map(() => '?').join(',')})`);
1687
- params.push(...query.statuses);
1688
- }
1689
- if (query.types !== undefined && query.types.length > 0) {
1690
- where.push(`type IN (${query.types.map(() => '?').join(',')})`);
1691
- params.push(...query.types);
1692
- }
1693
- params.push(query.limit);
1694
- const records = this.db
1695
- .query<SqliteReactionRecord, (string | number)[]>(
1696
- `SELECT * FROM sync_reactions WHERE ${where.join(' AND ')}
1697
- ORDER BY created_at_ms DESC, idempotency_key DESC LIMIT ?`,
1698
- )
1699
- .all(...params);
1700
- return records.map(toStoredReaction);
1701
- }
1702
-
1703
- async pruneReactions(
1704
- partition: string,
1705
- query: ReactionPruneQuery,
1706
- ): Promise<PrunedReactionCounts> {
1707
- if (query.limit <= 0) return { completed: 0, deadLetter: 0 };
1708
- return this.#serializeWrite(() => {
1709
- const records = this.db
1710
- .query<
1711
- { status: 'completed' | 'dead-letter' },
1712
- [string, string, number, number, number, number, number]
1713
- >(
1714
- `DELETE FROM sync_reactions
1715
- WHERE partition=? AND idempotency_key IN (
1716
- SELECT idempotency_key FROM sync_reactions
1717
- WHERE partition=?
1718
- AND ((status='completed' AND completed_at_ms IS NOT NULL
1719
- AND completed_at_ms<?)
1720
- OR (status='dead-letter' AND available_at_ms<?))
1721
- ORDER BY CASE WHEN status='completed' THEN completed_at_ms
1722
- ELSE available_at_ms END,
1723
- idempotency_key
1724
- LIMIT ?
1725
- )
1726
- AND ((status='completed' AND completed_at_ms IS NOT NULL
1727
- AND completed_at_ms<?)
1728
- OR (status='dead-letter' AND available_at_ms<?))
1729
- RETURNING status`,
1730
- )
1731
- .all(
1732
- partition,
1733
- partition,
1734
- query.completedBeforeMs,
1735
- query.deadLetterBeforeMs,
1736
- query.limit,
1737
- query.completedBeforeMs,
1738
- query.deadLetterBeforeMs,
1739
- );
1740
- return {
1741
- completed: records.filter((record) => record.status === 'completed')
1742
- .length,
1743
- deadLetter: records.filter((record) => record.status === 'dead-letter')
1744
- .length,
1745
- };
1746
- });
1747
- }
1748
-
1749
- async latestScopedChangeSeq(
1750
- partition: string,
1751
- query: { readonly table: string; readonly scopeFilter: ScopeMap },
1752
- ): Promise<number> {
1753
- const firstVariable = Object.keys(query.scopeFilter).sort()[0];
1754
- const values =
1755
- firstVariable === undefined
1756
- ? []
1757
- : (query.scopeFilter[firstVariable] ?? []);
1758
- if (firstVariable === undefined || values.length === 0) return 0;
1759
- const row = this.db
1760
- .query<{ seq: number | null }, string[]>(
1761
- LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL,
1762
- )
1763
- .get(partition, query.table, firstVariable, JSON.stringify(values));
1764
- return row?.seq ?? 0;
1765
- }
1766
-
1767
- async readCommitWindow(
1768
- partition: string,
1769
- query: CommitWindowQuery,
1770
- ): Promise<StoredCommit[]> {
1771
- const variables = Object.keys(query.scopeFilter).sort();
1772
- const firstVariable = variables[0];
1773
- if (firstVariable === undefined) return [];
1774
- const firstValues = query.scopeFilter[firstVariable] ?? [];
1775
- if (firstValues.length === 0) return [];
1776
- // Candidates via the inverted index (one variable) LEFT JOINed to the
1777
- // commit meta + the table's changes — one statement per page, never two
1778
- // per candidate (see `commitWindowPageSql`). Exact multi-variable
1779
- // verification against the stored scope map in `collectCommitWindowPage`.
1780
- const sql = commitWindowPageSql(firstValues.length, 'sqlite');
1781
- const commits: StoredCommit[] = [];
1782
- let deliveredChanges = 0;
1783
- let afterSeq = query.afterSeq;
1784
- const batchSize = Math.max(64, query.limitChanges);
1785
- while (deliveredChanges < query.limitChanges) {
1786
- const records = this.db
1787
- .query<SqliteCommitWindowRecord, (string | number)[]>(sql)
1788
- .all(
1789
- partition,
1790
- query.table,
1791
- firstVariable,
1792
- ...firstValues,
1793
- afterSeq,
1794
- query.throughSeq,
1795
- batchSize,
1796
- partition,
1797
- partition,
1798
- query.table,
1799
- );
1800
- if (records.length === 0) break;
1801
- const page = collectCommitWindowPage(
1802
- records,
1803
- query.scopeFilter,
1804
- query.limitChanges - deliveredChanges,
1805
- );
1806
- commits.push(...page.commits);
1807
- deliveredChanges += page.delivered;
1808
- afterSeq = page.lastSeq;
1809
- if (page.candidateCount < batchSize) break;
1810
- }
1811
- return commits;
1812
- }
1813
-
1814
- async scanRows(partition: string, query: RowScanQuery): Promise<StoredRow[]> {
1815
- const firstVariable = assertScopeIndexedScan(query);
1816
- const firstValues = query.scopeFilter[firstVariable] ?? [];
1817
- if (firstValues.length === 0) return [];
1818
- // Candidates via the inverted index LEFT JOINed to the row table — one
1819
- // statement per page, never one per row (see `scanRowPageSql`). Exact
1820
- // multi-variable verification against the stored scope map below.
1821
- const sql = scanRowPageSql(
1822
- this.table(query.table),
1823
- firstValues.length,
1824
- 'sqlite',
1825
- );
1826
- const rows: StoredRow[] = [];
1827
- let afterRowId = query.afterRowId ?? '';
1828
- const batchSize = Math.max(64, query.limit);
1829
- while (rows.length < query.limit) {
1830
- const records = this.db
1831
- .query<
1832
- SqliteRowRecord & { payload: Uint8Array | null },
1833
- (string | number)[]
1834
- >(sql)
1835
- .all(
1836
- partition,
1837
- query.table,
1838
- firstVariable,
1839
- ...firstValues,
1840
- afterRowId,
1841
- batchSize,
1842
- partition,
1843
- );
1844
- if (records.length === 0) break;
1845
- for (const record of records) {
1846
- afterRowId = record.row_id;
1847
- // NULL payload: an index candidate whose row vanished — it still
1848
- // advances the keyset cursor (LEFT JOIN contract) but yields no row.
1849
- if (record.payload === null) continue;
1850
- const stored = toStoredRow(record);
1851
- if (!matchesEffective(stored.scopes, query.scopeFilter)) continue;
1852
- rows.push(stored);
1853
- if (rows.length >= query.limit) break;
1854
- }
1855
- if (records.length < batchSize) break;
1856
- }
1857
- return rows;
1858
- }
1859
-
1860
- async scanRowsByIndex(
1861
- partition: string,
1862
- query: IndexRowScanQuery,
1863
- ): Promise<StoredRow[]> {
1864
- const table = this.table(query.table);
1865
- const index = resolveIndexRowScan(table, query);
1866
- const statement = indexRowPageStatement(
1867
- table,
1868
- index,
1869
- query.values,
1870
- partition,
1871
- query.afterRowId,
1872
- query.limit,
1873
- 'sqlite',
1874
- );
1875
- const params = statement.params as readonly (
1876
- | string
1877
- | number
1878
- | Uint8Array
1879
- | null
1880
- )[];
1881
- const records = this.db
1882
- .query<SqliteRowRecord, (string | number | Uint8Array | null)[]>(
1883
- statement.sql,
1884
- )
1885
- .all(...params);
1886
- return records.map(toStoredRow);
1887
- }
1888
-
1889
- async getClientRecord(
1890
- partition: string,
1891
- clientId: string,
1892
- ): Promise<ClientRecord | undefined> {
1893
- const record = this.db
1894
- .query<
1895
- {
1896
- client_id: string;
1897
- actor_id: string;
1898
- wire_version: number;
1899
- cursor: number;
1900
- subscriptions: string;
1901
- updated_at_ms: number;
1902
- last_round_at_ms: number | null;
1903
- last_error_code: string | null;
1904
- consecutive_failures: number;
1905
- },
1906
- [string, string]
1907
- >(
1908
- 'SELECT client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms, last_error_code, consecutive_failures FROM sync_clients WHERE partition=? AND client_id=?',
1909
- )
1910
- .get(partition, clientId);
1911
- if (record === null) return undefined;
1912
- return {
1913
- clientId: record.client_id,
1914
- actorId: record.actor_id,
1915
- wireVersion: record.wire_version,
1916
- cursor: record.cursor,
1917
- updatedAtMs: record.updated_at_ms,
1918
- subscriptions: JSON.parse(record.subscriptions) as ClientSubscription[],
1919
- ...clientRoundState(
1920
- record.last_round_at_ms,
1921
- record.last_error_code,
1922
- record.consecutive_failures,
1923
- ),
1924
- };
1925
- }
1926
-
1927
- async putClientRecord(
1928
- partition: string,
1929
- record: ClientRecord,
1930
- options?: ClientRecordWriteOptions,
1931
- ): Promise<void> {
1932
- this.db
1933
- .query(
1934
- `INSERT INTO sync_clients(partition, client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms) VALUES (?,?,?,?,?,?,?,?)
1935
- ON CONFLICT(partition, client_id) DO UPDATE SET
1936
- actor_id=excluded.actor_id,
1937
- wire_version=excluded.wire_version,
1938
- cursor=excluded.cursor,
1939
- subscriptions=excluded.subscriptions,
1940
- updated_at_ms=excluded.updated_at_ms,
1941
- last_round_at_ms=excluded.last_round_at_ms${
1942
- options?.keepRoundOutcome === true
1943
- ? ''
1944
- : `,
1945
- last_error_code=NULL,
1946
- consecutive_failures=0`
1947
- }`,
1948
- )
1949
- .run(
1950
- partition,
1951
- record.clientId,
1952
- record.actorId,
1953
- record.wireVersion,
1954
- record.cursor,
1955
- JSON.stringify(record.subscriptions),
1956
- record.updatedAtMs,
1957
- record.updatedAtMs,
1958
- );
1959
- }
1960
-
1961
- async recordClientRoundFailure(
1962
- partition: string,
1963
- clientId: string,
1964
- actorId: string,
1965
- errorCode: string,
1966
- atMs: number,
1967
- ): Promise<void> {
1968
- this.db
1969
- .query(
1970
- `UPDATE sync_clients
1971
- SET last_round_at_ms=?, last_error_code=?,
1972
- consecutive_failures=consecutive_failures+1
1973
- WHERE partition=? AND client_id=? AND actor_id=?`,
1974
- )
1975
- .run(atMs, errorCode, partition, clientId, actorId);
1976
- }
1977
-
1978
- async advanceClientCursor(
1979
- partition: string,
1980
- clientId: string,
1981
- actorId: string,
1982
- logEpoch: string,
1983
- cursor: number,
1984
- updatedAtMs: number,
1985
- ): Promise<void> {
1986
- this.db
1987
- .query(`UPDATE sync_clients
1988
- SET cursor=MAX(cursor, ?), updated_at_ms=MAX(updated_at_ms, ?)
1989
- WHERE partition=? AND client_id=? AND actor_id=?
1990
- AND EXISTS (SELECT 1 FROM sync_partition_registry
1991
- WHERE partition=sync_clients.partition AND log_epoch=?)`)
1992
- .run(cursor, updatedAtMs, partition, clientId, actorId, logEpoch);
1993
- }
1994
-
1995
- async updateClientCursor(
1996
- partition: string,
1997
- clientId: string,
1998
- cursor: number,
1999
- updatedAtMs: number,
2000
- ): Promise<void> {
2001
- this.db
978
+ const result = this.db
2002
979
  .query(
2003
- `UPDATE sync_clients
2004
- SET cursor=MAX(cursor, ?), updated_at_ms=MAX(updated_at_ms, ?)
2005
- WHERE partition=? AND client_id=?`,
2006
- )
2007
- .run(cursor, updatedAtMs, partition, clientId);
2008
- }
2009
-
2010
- async getActiveClientCursorFloor(
2011
- partition: string,
2012
- cutoffMs: number,
2013
- ): Promise<number | null> {
2014
- const row = this.db
2015
- .query<{ cursor: number | null }, [string, number]>(
2016
- 'SELECT MIN(cursor) AS cursor FROM sync_clients WHERE partition=? AND updated_at_ms>=?',
2017
- )
2018
- .get(partition, cutoffMs);
2019
- return row!.cursor;
2020
- }
2021
-
2022
- async listClientCursors(partition: string): Promise<ClientCursorInfo[]> {
2023
- const records = this.db
2024
- .query<
2025
- { client_id: string; cursor: number; updated_at_ms: number },
2026
- [string]
2027
- >(
2028
- 'SELECT client_id, cursor, updated_at_ms FROM sync_clients WHERE partition=?',
2029
- )
2030
- .all(partition);
2031
- return records.map((r) => ({
2032
- clientId: r.client_id,
2033
- cursor: r.cursor,
2034
- updatedAtMs: r.updated_at_ms,
2035
- }));
2036
- }
2037
-
2038
- async listRowsReferencingBlob(
2039
- partition: string,
2040
- blobId: string,
2041
- ): Promise<
2042
- {
2043
- readonly table: string;
2044
- readonly rowId: string;
2045
- readonly scopes: Record<string, string>;
2046
- }[]
2047
- > {
2048
- // Candidate rows via the by-blob index (§5.9.4/§5.9.5); each row's
2049
- // stored scopes come from sync_rows for the §3.4 authorization test.
2050
- const refs = this.db
2051
- .query<{ tbl: string; row_id: string }, [string, string]>(
2052
- 'SELECT tbl, row_id FROM sync_blob_refs WHERE partition=? AND blob_id=?',
2053
- )
2054
- .all(partition, blobId);
2055
- const out: {
2056
- table: string;
2057
- rowId: string;
2058
- scopes: Record<string, string>;
2059
- }[] = [];
2060
- for (const ref of refs) {
2061
- const compiled = this.#tables?.get(ref.tbl);
2062
- if (compiled === undefined) continue; // table no longer in the schema
2063
- const row = this.db
2064
- .query<{ scopes: string }, [string, string]>(
2065
- selectRowScopesSql(compiled, 'sqlite'),
2066
- )
2067
- .get(partition, ref.row_id);
2068
- if (row === null) continue;
2069
- out.push({
2070
- table: ref.tbl,
2071
- rowId: ref.row_id,
2072
- scopes: JSON.parse(row.scopes) as Record<string, string>,
2073
- });
2074
- }
2075
- return out;
2076
- }
2077
-
2078
- async listReferencedBlobIds(partition: string): Promise<string[]> {
2079
- const rows = this.db
2080
- .query<{ blob_id: string }, [string]>(
2081
- 'SELECT DISTINCT blob_id FROM sync_blob_refs WHERE partition=?',
2082
- )
2083
- .all(partition);
2084
- return rows.map((r) => r.blob_id);
2085
- }
2086
-
2087
- async listScopeValueRows(
2088
- partition: string,
2089
- query: ScopeValueQuery,
2090
- ): Promise<string[]> {
2091
- return this.db
2092
- .query<{ row_id: string }, [string, string, string, string, number]>(
2093
- LIST_SCOPE_VALUE_ROWS_SQLITE_SQL,
2094
- )
2095
- .all(partition, query.table, query.variable, query.value, query.limit)
2096
- .map((row) => row.row_id);
2097
- }
2098
-
2099
- async eraseScopeValueHistory(
2100
- partition: string,
2101
- query: ScopeValueHistoryQuery,
2102
- ): Promise<number> {
2103
- const params = eraseScopeValueHistoryParams(partition, query);
2104
- return this.#serializeWrite(() => {
2105
- this.db.exec('BEGIN IMMEDIATE');
2106
- try {
2107
- let removed = 0;
2108
- for (const sql of ERASE_SCOPE_VALUE_HISTORY_SQLITE_SQL) {
2109
- removed += this.db.query(sql).all(...params).length;
2110
- }
2111
- this.db.exec('COMMIT');
2112
- return removed;
2113
- } catch (error) {
2114
- rollbackAfterError(() => {
2115
- this.db.exec('ROLLBACK');
2116
- }, error);
2117
- }
2118
- });
2119
- }
2120
-
2121
- // -- admin/console read surface --------------------------------------------
2122
-
2123
- async listClientRecords(partition: string): Promise<ClientRecord[]> {
2124
- const records = this.db
2125
- .query<
2126
- {
2127
- client_id: string;
2128
- actor_id: string;
2129
- wire_version: number;
2130
- cursor: number;
2131
- subscriptions: string;
2132
- updated_at_ms: number;
2133
- last_round_at_ms: number | null;
2134
- last_error_code: string | null;
2135
- consecutive_failures: number;
2136
- },
2137
- [string]
2138
- >(
2139
- 'SELECT client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms, last_error_code, consecutive_failures FROM sync_clients WHERE partition=? ORDER BY updated_at_ms DESC',
2140
- )
2141
- .all(partition);
2142
- return records.map((record) => ({
2143
- clientId: record.client_id,
2144
- actorId: record.actor_id,
2145
- wireVersion: record.wire_version,
2146
- cursor: record.cursor,
2147
- updatedAtMs: record.updated_at_ms,
2148
- subscriptions: JSON.parse(record.subscriptions) as ClientSubscription[],
2149
- ...clientRoundState(
2150
- record.last_round_at_ms,
2151
- record.last_error_code,
2152
- record.consecutive_failures,
2153
- ),
2154
- }));
2155
- }
2156
-
2157
- async listCommitMetadata(
2158
- partition: string,
2159
- query: CommitMetadataQuery,
2160
- ): Promise<CommitMetadata[]> {
2161
- // Newest-first window, resumed by `afterSeq` (exclusive lower bound) and
2162
- // ended by `beforeSeq` (exclusive upper bound) when paging older. An
2163
- // omitted bound is the largest safe commitSeq, so one statement serves
2164
- // both the bounded and unbounded read.
2165
- const beforeSeq = query.beforeSeq ?? Number.MAX_SAFE_INTEGER;
2166
- const rows = query.table
2167
- ? this.db
2168
- .query<
2169
- {
2170
- commit_seq: number;
2171
- client_id: string;
2172
- client_commit_id: string;
2173
- actor_id: string;
2174
- created_at_ms: number;
2175
- },
2176
- [string, number, number, string, number]
2177
- >(
2178
- `SELECT c.commit_seq, c.client_id, c.client_commit_id, c.actor_id, c.created_at_ms
2179
- FROM sync_commits c
2180
- WHERE c.partition=? AND c.commit_seq>? AND c.commit_seq<?
2181
- AND EXISTS (SELECT 1 FROM sync_changes ch
2182
- WHERE ch.partition=c.partition AND ch.commit_seq=c.commit_seq AND ch.tbl=?)
2183
- ORDER BY c.commit_seq DESC LIMIT ?`,
2184
- )
2185
- .all(partition, query.afterSeq, beforeSeq, query.table, query.limit)
2186
- : this.db
2187
- .query<
2188
- {
2189
- commit_seq: number;
2190
- client_id: string;
2191
- client_commit_id: string;
2192
- actor_id: string;
2193
- created_at_ms: number;
2194
- },
2195
- [string, number, number, number]
2196
- >(
2197
- `SELECT commit_seq, client_id, client_commit_id, actor_id, created_at_ms
2198
- FROM sync_commits
2199
- WHERE partition=? AND commit_seq>? AND commit_seq<?
2200
- ORDER BY commit_seq DESC LIMIT ?`,
2201
- )
2202
- .all(partition, query.afterSeq, beforeSeq, query.limit);
2203
- // `changeSample`: one extra statement for the whole page — the first
2204
- // `sample` changes (change-index order) of every listed commit.
2205
- const sampleCount =
2206
- query.changeSample !== undefined &&
2207
- Number.isInteger(query.changeSample) &&
2208
- query.changeSample > 0
2209
- ? query.changeSample
2210
- : 0;
2211
- const samples = new Map<number, CommitChangeSummary[]>();
2212
- if (sampleCount > 0 && rows.length > 0) {
2213
- const seqs = rows.map((row) => row.commit_seq);
2214
- const records = this.db
2215
- .query<
2216
- { commit_seq: number; tbl: string; row_id: string; op: number },
2217
- (string | number)[]
2218
- >(
2219
- `SELECT commit_seq, tbl, row_id, op FROM sync_changes
2220
- WHERE partition=? AND commit_seq IN (${placeholders(seqs.length)}) AND idx<?
2221
- ORDER BY commit_seq, idx`,
2222
- )
2223
- .all(partition, ...seqs, sampleCount);
2224
- for (const record of records) {
2225
- const sample = samples.get(record.commit_seq) ?? [];
2226
- sample.push({
2227
- table: record.tbl,
2228
- rowId: record.row_id,
2229
- op: record.op === 1 ? 'upsert' : 'delete',
2230
- });
2231
- samples.set(record.commit_seq, sample);
2232
- }
2233
- }
2234
- return rows.map((row) => {
2235
- const changes = this.db
2236
- .query<{ tbl: string; n: number }, [string, number]>(
2237
- 'SELECT tbl, count(*) AS n FROM sync_changes WHERE partition=? AND commit_seq=? GROUP BY tbl',
2238
- )
2239
- .all(partition, row.commit_seq);
2240
- return {
2241
- commitSeq: row.commit_seq,
2242
- clientId: row.client_id,
2243
- clientCommitId: row.client_commit_id,
2244
- actorId: row.actor_id,
2245
- createdAtMs: row.created_at_ms,
2246
- changeCount: changes.reduce((sum, c) => sum + c.n, 0),
2247
- tables: changes.map((c) => c.tbl),
2248
- ...(sampleCount > 0
2249
- ? { sample: samples.get(row.commit_seq) ?? [] }
2250
- : {}),
2251
- };
2252
- });
2253
- }
2254
-
2255
- async getCommit(
2256
- partition: string,
2257
- commitSeq: number,
2258
- ): Promise<CommitDetail | undefined> {
2259
- const meta = this.db
2260
- .query<
2261
- {
2262
- client_id: string;
2263
- client_commit_id: string;
2264
- actor_id: string;
2265
- created_at_ms: number;
2266
- },
2267
- [string, number]
2268
- >(
2269
- 'SELECT client_id, client_commit_id, actor_id, created_at_ms FROM sync_commits WHERE partition=? AND commit_seq=?',
2270
- )
2271
- .get(partition, commitSeq);
2272
- if (meta === null) return undefined;
2273
- const changes = this.db
2274
- .query<SqliteChangeRecord, [string, number]>(
2275
- 'SELECT tbl, row_id, op, row_version, scopes, payload FROM sync_changes WHERE partition=? AND commit_seq=? ORDER BY idx',
2276
- )
2277
- .all(partition, commitSeq)
2278
- .map(toStoredChange);
2279
- return {
2280
- commitSeq,
2281
- clientId: meta.client_id,
2282
- clientCommitId: meta.client_commit_id,
2283
- actorId: meta.actor_id,
2284
- createdAtMs: meta.created_at_ms,
2285
- changeCount: changes.length,
2286
- tables: [...new Set(changes.map((change) => change.table))].sort(),
2287
- changes,
2288
- };
2289
- }
2290
-
2291
- async listRowChanges(
2292
- partition: string,
2293
- query: RowChangesQuery,
2294
- ): Promise<RowChangeRecord[]> {
2295
- // Retained-log read for one row: no dedicated index (the write path pays
2296
- // nothing for it), bounded by retention (§4.6).
2297
- return this.db
2298
- .query<
2299
- {
2300
- commit_seq: number;
2301
- op: number;
2302
- row_version: number | null;
2303
- actor_id: string;
2304
- client_id: string;
2305
- created_at_ms: number;
2306
- },
2307
- [string, string, string, number]
2308
- >(
2309
- `SELECT ch.commit_seq, ch.op, ch.row_version, c.actor_id, c.client_id, c.created_at_ms
2310
- FROM sync_changes ch
2311
- JOIN sync_commits c
2312
- ON c.partition=ch.partition AND c.commit_seq=ch.commit_seq
2313
- WHERE ch.partition=? AND ch.tbl=? AND ch.row_id=?
2314
- ORDER BY ch.commit_seq DESC, ch.idx DESC LIMIT ?`,
2315
- )
2316
- .all(partition, query.table, query.rowId, query.limit)
2317
- .map((row) => ({
2318
- commitSeq: row.commit_seq,
2319
- op: row.op === 1 ? 'upsert' : 'delete',
2320
- ...(row.row_version !== null ? { rowVersion: row.row_version } : {}),
2321
- actorId: row.actor_id,
2322
- clientId: row.client_id,
2323
- createdAtMs: row.created_at_ms,
2324
- }));
2325
- }
2326
-
2327
- async listRows(partition: string, query: RowListQuery): Promise<StoredRow[]> {
2328
- // Keyset page over the row table's (partition, rowId) primary key: an
2329
- // index range read, no scope filter, no per-row lookup.
2330
- return this.db
2331
- .query<SqliteRowRecord, [string, string, number]>(
2332
- `SELECT ${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, ${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, ${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, ${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, ${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions
2333
- FROM ${quoteIdent(this.table(query.table).name)}
2334
- WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=? AND ${quoteIdent(SYNC_ROW_ID_COLUMN)}>?
2335
- ORDER BY ${quoteIdent(SYNC_ROW_ID_COLUMN)} LIMIT ?`,
2336
- )
2337
- .all(partition, query.afterRowId ?? '', query.limit)
2338
- .map(toStoredRow);
2339
- }
2340
-
2341
- async countRows(partition: string, table: string): Promise<number> {
2342
- const record = this.db
2343
- .query<{ n: number }, [string]>(
2344
- `SELECT count(*) AS n FROM ${quoteIdent(this.table(table).name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?`,
2345
- )
2346
- .get(partition);
2347
- return record?.n ?? 0;
2348
- }
2349
-
2350
- async listPushResults(
2351
- partition: string,
2352
- clientCommitId: string,
2353
- ): Promise<ClientPushResult[]> {
2354
- // Retained outcomes for one clientCommitId: no dedicated index, ordered
2355
- // by client so the read is deterministic.
2356
- return this.db
2357
- .query<{ client_id: string; result: string }, [string, string]>(
2358
- 'SELECT client_id, result FROM sync_push_results WHERE partition=? AND client_commit_id=? ORDER BY client_id',
2359
- )
2360
- .all(partition, clientCommitId)
2361
- .map((record) => {
2362
- try {
2363
- return {
2364
- clientId: record.client_id,
2365
- result: deserializePushResult(record.result),
2366
- };
2367
- } catch {
2368
- throw syncError(
2369
- 'sync.idempotency_cache_miss',
2370
- 'persisted push result unreadable (§6.3)',
2371
- );
2372
- }
2373
- });
2374
- }
2375
-
2376
- async scopeActivity(
2377
- partition: string,
2378
- query: ScopeActivityQuery,
2379
- ): Promise<ScopeCommitActivity[]> {
2380
- // Candidate commits via the change-scope index (§3.1) — never a scan.
2381
- const rows = this.db
2382
- .query<
2383
- { commit_seq: number; tbl: string },
2384
- [string, string, string, number]
2385
- >(
2386
- `SELECT DISTINCT commit_seq, tbl FROM sync_change_scopes
2387
- WHERE partition=? AND var=? AND value=?
2388
- ORDER BY commit_seq DESC LIMIT ?`,
980
+ `UPDATE sync_backfill_checkpoints
981
+ SET watermark=?, observed_rows=?, updated_at_ms=?
982
+ WHERE partition=? AND name=? AND owner_epoch=?`,
2389
983
  )
2390
- .all(partition, query.variable, query.value, query.limit);
2391
- const out: ScopeCommitActivity[] = [];
2392
- for (const row of rows) {
2393
- const meta = this.db
2394
- .query<{ actor_id: string; created_at_ms: number }, [string, number]>(
2395
- 'SELECT actor_id, created_at_ms FROM sync_commits WHERE partition=? AND commit_seq=?',
2396
- )
2397
- .get(partition, row.commit_seq);
2398
- if (meta === null) continue;
2399
- const count = this.db
2400
- .query<{ n: number }, [string, number, string]>(
2401
- 'SELECT count(*) AS n FROM sync_changes WHERE partition=? AND commit_seq=? AND tbl=?',
2402
- )
2403
- .get(partition, row.commit_seq, row.tbl);
2404
- out.push({
2405
- commitSeq: row.commit_seq,
2406
- table: row.tbl,
2407
- createdAtMs: meta.created_at_ms,
2408
- actorId: meta.actor_id,
2409
- changeCount: count?.n ?? 0,
2410
- });
2411
- }
2412
- return out;
2413
- }
2414
-
2415
- async listPartitions(): Promise<string[]> {
2416
- return (await this.listPartitionRegistry()).map((entry) => entry.partition);
984
+ .run(watermark, observedRows, nowMs, partition, name, ownerEpoch);
985
+ return Number(result.changes) === 1;
2417
986
  }
2418
987
  }