@syncular/client 0.91.0 → 0.93.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/client.ts CHANGED
@@ -68,6 +68,7 @@ import { EncryptError } from '@syncular/core';
68
68
  import { EncodeError } from '@syncular/core';
69
69
  import {
70
70
  applyCommitFrame,
71
+ type ApplyStep,
71
72
  applyRowsSegment,
72
73
  applySqliteSegment,
73
74
  deleteLocalRow,
@@ -106,7 +107,6 @@ import {
106
107
  type DiagnosticLastChange,
107
108
  type DiagnosticLastRound,
108
109
  type DiagnosticQueryFailure,
109
- type DiagnosticRoundCounters,
110
110
  MAX_DIAGNOSTIC_DOMAINS,
111
111
  MAX_DIAGNOSTIC_QUERY_FAILURES,
112
112
  type RealtimePolicy,
@@ -967,6 +967,12 @@ function isFinalPushResult(frame: PushResultFrame): boolean {
967
967
  );
968
968
  }
969
969
 
970
+ function transportFlag(value: unknown): boolean {
971
+ if (typeof value !== 'boolean')
972
+ throw invalidRequest('transportEnabled must be a boolean');
973
+ return value;
974
+ }
975
+
970
976
  /** The stable `code` a thrown value carries, when it has one. */
971
977
  function thrownCode(error: unknown): string | undefined {
972
978
  const code = (error as { code?: unknown } | undefined)?.code;
@@ -1138,6 +1144,9 @@ export class SyncClient {
1138
1144
  readonly #queryFailures = new Map<string, DiagnosticQueryFailure>();
1139
1145
  /** The batch accumulator; non-undefined only inside `#applyBatch`. */
1140
1146
  #batch: ChangeAccumulator | undefined;
1147
+ /** §1.4 rule 4: the change batches an open coalesced apply transaction
1148
+ * publishes after its COMMIT, in revision order. */
1149
+ #held: (() => void)[] | undefined;
1141
1150
  /**
1142
1151
  * Operation-serialization mutex (the core owns one loop). Every
1143
1152
  * transaction-entering ASYNC operation — `sync`, the delta-apply body, and
@@ -1160,13 +1169,9 @@ export class SyncClient {
1160
1169
  #preflightBarrier: Promise<void> | undefined;
1161
1170
 
1162
1171
  constructor(config: SyncClientConfig) {
1163
- if (
1164
- config.transportEnabled !== undefined &&
1165
- typeof config.transportEnabled !== 'boolean'
1166
- ) {
1167
- throw invalidRequest('transportEnabled must be a boolean');
1168
- }
1169
- this.#transportEnabled = config.transportEnabled !== false;
1172
+ this.#transportEnabled =
1173
+ config.transportEnabled === undefined ||
1174
+ transportFlag(config.transportEnabled);
1170
1175
  for (const name of [
1171
1176
  'maxPushCommitsPerRequest',
1172
1177
  'maxPushOperationsPerRequest',
@@ -1443,17 +1448,18 @@ export class SyncClient {
1443
1448
  // earlier WITHOUT a schema bump.
1444
1449
  this.#db.transaction(() => {
1445
1450
  ensureLocalSyncedSchema(this.#db, this.#schema);
1446
- if (marker === undefined)
1447
- setMeta(
1448
- this.#db,
1449
- LOCAL_SCHEMA_VERSION_KEY,
1450
- String(this.#schema.version),
1451
- );
1452
- setLocalSchemaDescriptor(this.#db, this.#schema);
1453
- saveUnitScopeSchema(this.#db, this.#schema);
1451
+ this.#stampSchema(marker === undefined);
1454
1452
  });
1455
1453
  }
1456
1454
 
1455
+ /** Write the schema marker (when `version`), descriptor and unit scopes. */
1456
+ #stampSchema(version: boolean): void {
1457
+ if (version)
1458
+ setMeta(this.#db, LOCAL_SCHEMA_VERSION_KEY, String(this.#schema.version));
1459
+ setLocalSchemaDescriptor(this.#db, this.#schema);
1460
+ saveUnitScopeSchema(this.#db, this.#schema);
1461
+ }
1462
+
1457
1463
  /**
1458
1464
  * §7.4.3 reset: whole-database local reset EXCEPT the outbox, clientId,
1459
1465
  * leaseState, and the tables and units a bump keeps (equal generations).
@@ -1491,13 +1497,7 @@ export class SyncClient {
1491
1497
  }
1492
1498
  changed = resetLocalReplica(this.#db, this.#schema, true);
1493
1499
  this.#replayOutbox();
1494
- setMeta(
1495
- this.#db,
1496
- LOCAL_SCHEMA_VERSION_KEY,
1497
- String(this.#schema.version),
1498
- );
1499
- setLocalSchemaDescriptor(this.#db, this.#schema);
1500
- saveUnitScopeSchema(this.#db, this.#schema);
1500
+ this.#stampSchema(true);
1501
1501
  });
1502
1502
  // Every table the reset dropped changed (I1 eviction-shaped).
1503
1503
  for (const table of changed) batch.table(table);
@@ -1581,7 +1581,7 @@ export class SyncClient {
1581
1581
  /** §2.1 reset after the server reports a different log continuity. */
1582
1582
  #runLogEpochReset(logEpoch: string): string[] {
1583
1583
  if (getMeta(this.#db, LOG_EPOCH_META_KEY) === undefined) {
1584
- this.#applyBatch(() => setMeta(this.#db, LOG_EPOCH_META_KEY, logEpoch));
1584
+ setMeta(this.#db, LOG_EPOCH_META_KEY, logEpoch);
1585
1585
  this.#emitSyncNeeded('startup');
1586
1586
  return [];
1587
1587
  }
@@ -1695,9 +1695,7 @@ export class SyncClient {
1695
1695
  /** Host-owned transport policy; the local replica remains available. */
1696
1696
  setTransportEnabled(enabled: boolean): void {
1697
1697
  this.#requireStarted();
1698
- if (typeof enabled !== 'boolean')
1699
- throw invalidRequest('transportEnabled must be a boolean');
1700
- if (this.#transportEnabled === enabled) return;
1698
+ if (this.#transportEnabled === transportFlag(enabled)) return;
1701
1699
  this.#transportEnabled = enabled;
1702
1700
  this.#transportEpoch += 1;
1703
1701
  this.#disarm();
@@ -1860,26 +1858,18 @@ export class SyncClient {
1860
1858
  */
1861
1859
  querySnapshot<Row = SqlRow>(spec: QueryReadSpec): QuerySnapshot<Row>;
1862
1860
  querySnapshot(spec: QueryReadSpec): QuerySnapshot {
1863
- const read = this.#readSnapshot({
1864
- statements: [
1865
- {
1866
- sql: spec.sql,
1867
- ...(spec.params !== undefined ? { params: spec.params } : {}),
1868
- },
1869
- ],
1870
- ...(spec.coverage !== undefined ? { coverage: spec.coverage } : {}),
1871
- ...(spec.owner !== undefined ? { owner: spec.owner } : {}),
1872
- });
1873
- const rows = read.queries[0];
1874
- if (read.queries.length !== 1 || rows === undefined) {
1875
- throw new ClientSyncError(
1876
- 'client.failed',
1877
- 'querySnapshot produced no statement result',
1878
- );
1879
- }
1861
+ // A statement reads only `sql` and `params`; one statement yields one
1862
+ // result.
1863
+ const read = this.#readSnapshot(
1864
+ definedObject({
1865
+ statements: [spec],
1866
+ coverage: spec.coverage,
1867
+ owner: spec.owner,
1868
+ }),
1869
+ );
1880
1870
  return {
1881
1871
  revision: read.revision,
1882
- rows,
1872
+ rows: read.queries[0]!,
1883
1873
  coverage: read.coverage,
1884
1874
  };
1885
1875
  }
@@ -2137,17 +2127,12 @@ export class SyncClient {
2137
2127
  return withPreviousVersionContainer(this.#db, readPreviousVersionContainer);
2138
2128
  }
2139
2129
 
2140
- /** RFC 0005 A2: presence for `statusSnapshot()`, without creating the file. */
2141
- #previousVersionStatus(): { present: boolean; createdAtMs?: number } {
2142
- const record = this.#readPreviousVersionRecord();
2143
- if (record === undefined) return { present: this.#previousVersionExists() };
2144
- return { present: true, createdAtMs: record.createdAtMs };
2145
- }
2146
-
2147
2130
  #previousVersionLeaseInactive(nowMs: number): boolean {
2148
2131
  const lease = this.#leaseState;
2149
- if (lease?.errorCode !== undefined) return true;
2150
- return lease?.expiresAtMs !== undefined && lease.expiresAtMs <= nowMs;
2132
+ return (
2133
+ lease?.errorCode !== undefined ||
2134
+ (lease?.expiresAtMs ?? Infinity) <= nowMs
2135
+ );
2151
2136
  }
2152
2137
 
2153
2138
  /**
@@ -2316,12 +2301,13 @@ export class SyncClient {
2316
2301
  ? 'offline'
2317
2302
  : round?.status === 'succeeded'
2318
2303
  ? 'online'
2319
- : round?.status === 'failed' &&
2320
- (round.causeKind === 'network' || round.causeKind === 'connect')
2321
- ? 'offline'
2322
- : round?.status === 'failed' && round.httpStatus !== undefined
2323
- ? 'online'
2324
- : 'unknown';
2304
+ : round?.status !== 'failed'
2305
+ ? 'unknown'
2306
+ : round.causeKind === 'network' || round.causeKind === 'connect'
2307
+ ? 'offline'
2308
+ : round.httpStatus !== undefined
2309
+ ? 'online'
2310
+ : 'unknown';
2325
2311
  return definedObject({
2326
2312
  version: CLIENT_DIAGNOSTICS_VERSION,
2327
2313
  capturedAtMs,
@@ -2359,7 +2345,7 @@ export class SyncClient {
2359
2345
  ),
2360
2346
  ].sort(),
2361
2347
  },
2362
- activeUnits: registered.filter((unit) => unit.status === 'active').length,
2348
+ activeUnits: registered.length - units.revoked - units.failed,
2363
2349
  replicaCursor: replicaCursor(this.#db),
2364
2350
  lastRound: this.#lastRound,
2365
2351
  lastChange: this.#lastChange,
@@ -2386,12 +2372,9 @@ export class SyncClient {
2386
2372
 
2387
2373
  #diagnosticStorage(): ClientDiagnosticsStorage {
2388
2374
  try {
2389
- const pageCount = Number(
2390
- this.#db.query('PRAGMA page_count')[0]?.page_count ?? 0,
2391
- );
2392
- const pageSize = Number(
2393
- this.#db.query('PRAGMA page_size')[0]?.page_size ?? 0,
2394
- );
2375
+ const pragma = (name: string) =>
2376
+ Number(this.#db.query(`PRAGMA ${name}`)[0]?.[name] ?? 0);
2377
+ const databaseBytes = pragma('page_count') * pragma('page_size');
2395
2378
  // One statement for the outbox and outcome-journal sums keeps the
2396
2379
  // snapshot at its pre-version-4 statement count.
2397
2380
  const sums = this.#db.query(
@@ -2409,7 +2392,7 @@ export class SyncClient {
2409
2392
  const size = (value: unknown) => Math.max(0, Number(value ?? 0));
2410
2393
  return {
2411
2394
  status: pressure ? 'pressure' : 'healthy',
2412
- databaseBytesApprox: Math.max(0, pageCount * pageSize),
2395
+ databaseBytesApprox: Math.max(0, databaseBytes),
2413
2396
  pendingOutboxBytesApprox: size(sums?.outbox),
2414
2397
  retainedOutcomeBytesApprox: size(sums?.bytes),
2415
2398
  retainedOutcomeEntries: size(sums?.entries),
@@ -2458,6 +2441,8 @@ export class SyncClient {
2458
2441
  }
2459
2442
 
2460
2443
  #statusSnapshot(outboxCount?: number): SyncStatusSnapshot {
2444
+ // RFC 0005 A2: previous-version presence, without creating the file.
2445
+ const record = this.#readPreviousVersionRecord();
2461
2446
  return {
2462
2447
  currentSchemaVersion: this.#config.schema.version,
2463
2448
  outbox: outboxCount ?? countOutbox(this.#db),
@@ -2465,7 +2450,10 @@ export class SyncClient {
2465
2450
  leaseState: this.#leaseState,
2466
2451
  schemaFloor: this.#schemaFloor,
2467
2452
  syncNeeded: this.#needsPull,
2468
- previousVersionContext: this.#previousVersionStatus(),
2453
+ previousVersionContext:
2454
+ record === undefined
2455
+ ? { present: this.#previousVersionExists() }
2456
+ : { present: true, createdAtMs: record.createdAtMs },
2469
2457
  securityLifecycle: this.#securityLifecycle,
2470
2458
  };
2471
2459
  }
@@ -2514,14 +2502,14 @@ export class SyncClient {
2514
2502
  onRollback?.(error);
2515
2503
  throw error;
2516
2504
  }
2517
- if (revision !== undefined) {
2518
- const event = batch.finish(revision, status);
2505
+ const publish = () => {
2506
+ const event = batch.finish(revision!, status);
2519
2507
  const tables = [...new Set(event.tables.map((entry) => entry.table))];
2520
2508
  const windows = [
2521
2509
  ...new Set(event.windows.map((entry) => entry.baseKey.split('\0')[0]!)),
2522
2510
  ];
2523
2511
  this.#lastChange = {
2524
- revision: revision.toString(),
2512
+ revision: revision!.toString(),
2525
2513
  recordedAtMs: this.#now(),
2526
2514
  tables: tables.slice(0, MAX_DIAGNOSTIC_DOMAINS),
2527
2515
  windows: windows.slice(0, MAX_DIAGNOSTIC_DOMAINS),
@@ -2537,7 +2525,10 @@ export class SyncClient {
2537
2525
  const legacy = invalidationFromChange(event);
2538
2526
  if (legacy !== undefined) this.#invalidation.emit(legacy);
2539
2527
  this.#emitDiagnostics();
2540
- }
2528
+ };
2529
+ if (revision !== undefined)
2530
+ if (this.#held) this.#held.push(publish);
2531
+ else publish();
2541
2532
  return result;
2542
2533
  }
2543
2534
 
@@ -2587,28 +2578,23 @@ export class SyncClient {
2587
2578
  options?: { readonly mediaType?: string; readonly name?: string },
2588
2579
  ): Promise<BlobRef> {
2589
2580
  const owned = bytes.slice();
2590
- return this.#runProtectedAsync(() => this.#uploadBlob(owned, options));
2591
- }
2592
-
2593
- async #uploadBlob(
2594
- bytes: Uint8Array,
2595
- options?: { readonly mediaType?: string; readonly name?: string },
2596
- ): Promise<BlobRef> {
2597
- if (this.#config.blobs === undefined) {
2598
- throw invalidRequest('uploadBlob requires a blob transport');
2599
- }
2600
- const blobId = await computeBlobId(bytes);
2601
- const nowMs = this.#now();
2602
- this.#db.transaction(() => {
2603
- putCachedBlob(this.#db, blobId, bytes, nowMs, options?.mediaType);
2604
- recordPendingUpload(this.#db, blobId, nowMs, options?.mediaType);
2605
- });
2606
- this.#enforceBlobCacheCap();
2607
- return definedObject({
2608
- blobId,
2609
- byteLength: bytes.length,
2610
- mediaType: options?.mediaType,
2611
- name: options?.name,
2581
+ return this.#runProtectedAsync(async () => {
2582
+ if (this.#config.blobs === undefined) {
2583
+ throw invalidRequest('uploadBlob requires a blob transport');
2584
+ }
2585
+ const blobId = await computeBlobId(owned);
2586
+ const nowMs = this.#now();
2587
+ this.#db.transaction(() => {
2588
+ putCachedBlob(this.#db, blobId, owned, nowMs, options?.mediaType);
2589
+ recordPendingUpload(this.#db, blobId, nowMs, options?.mediaType);
2590
+ });
2591
+ this.#enforceBlobCacheCap();
2592
+ return definedObject({
2593
+ blobId,
2594
+ byteLength: owned.length,
2595
+ mediaType: options?.mediaType,
2596
+ name: options?.name,
2597
+ });
2612
2598
  });
2613
2599
  }
2614
2600
 
@@ -2624,65 +2610,63 @@ export class SyncClient {
2624
2610
  * Accepts a raw `blob_ref` column string or a bare `blobId`.
2625
2611
  */
2626
2612
  fetchBlob(blobIdOrRef: string): Promise<CachedBlob> {
2627
- return this.#runProtectedAsync(() => this.#fetchBlob(blobIdOrRef));
2628
- }
2629
-
2630
- async #fetchBlob(blobIdOrRef: string): Promise<CachedBlob> {
2631
- // A BlobRef declares the size that caps the download (§5.9.5); a bare
2632
- // blobId declares none.
2633
- const ref = blobIdOrRef.startsWith('sha256:')
2634
- ? undefined
2635
- : parseBlobRef(blobIdOrRef);
2636
- const blobId = ref?.blobId ?? blobIdOrRef;
2637
- const cached = getCachedBlob(this.#db, blobId);
2638
- if (cached !== undefined) return cached;
2639
- const transport = this.#config.blobs;
2640
- if (transport === undefined) {
2641
- throw invalidRequest('fetchBlob requires a blob transport');
2642
- }
2643
- // §5.9.5: the authorized endpoint serves bytes inline OR (always-issue,
2644
- // presign configured) a signed url the client fetches directly. On a url
2645
- // arm the client MUST NOT attach host auth and MUST NOT fall through:
2646
- // failure => re-request, the caller's next fetchBlob mints a fresh url.
2647
- this.#requireTransport();
2648
- const response = await transport.download(blobId, ref?.byteLength);
2649
- let bytes: Uint8Array;
2650
- if (response.kind === 'url') {
2651
- if (transport.fetchUrl === undefined) {
2652
- throw invalidRequest('blob transport cannot fetch urls');
2613
+ return this.#runProtectedAsync(async () => {
2614
+ // A BlobRef declares the size that caps the download (§5.9.5); a bare
2615
+ // blobId declares none.
2616
+ const ref = blobIdOrRef.startsWith('sha256:')
2617
+ ? undefined
2618
+ : parseBlobRef(blobIdOrRef);
2619
+ const blobId = ref?.blobId ?? blobIdOrRef;
2620
+ const cached = getCachedBlob(this.#db, blobId);
2621
+ if (cached !== undefined) return cached;
2622
+ const transport = this.#config.blobs;
2623
+ if (transport === undefined) {
2624
+ throw invalidRequest('fetchBlob requires a blob transport');
2653
2625
  }
2654
- if (
2655
- response.urlExpiresAtMs !== undefined &&
2656
- response.urlExpiresAtMs <= this.#now()
2657
- ) {
2658
- // §5.9.5: MUST NOT start a fetch at/past expiry — re-request recovers.
2659
- throw new ClientSyncError(
2660
- 'sync.segment_expired',
2661
- 'blob url expired before fetch',
2662
- true,
2663
- );
2626
+ // §5.9.5: the authorized endpoint serves bytes inline OR (always-issue,
2627
+ // presign configured) a signed url the client fetches directly. On a url
2628
+ // arm the client MUST NOT attach host auth and MUST NOT fall through:
2629
+ // failure => re-request, the caller's next fetchBlob mints a fresh url.
2630
+ this.#requireTransport();
2631
+ const response = await transport.download(blobId, ref?.byteLength);
2632
+ let bytes: Uint8Array;
2633
+ if (response.kind === 'url') {
2634
+ if (transport.fetchUrl === undefined) {
2635
+ throw invalidRequest('blob transport cannot fetch urls');
2636
+ }
2637
+ if (
2638
+ response.urlExpiresAtMs !== undefined &&
2639
+ response.urlExpiresAtMs <= this.#now()
2640
+ ) {
2641
+ // §5.9.5: MUST NOT start a fetch at/past expiry — re-request recovers.
2642
+ throw new ClientSyncError(
2643
+ 'sync.segment_expired',
2644
+ 'blob url expired before fetch',
2645
+ true,
2646
+ );
2647
+ }
2648
+ bytes = await transport.fetchUrl(response.url, ref?.byteLength);
2649
+ } else {
2650
+ bytes = response.bytes;
2664
2651
  }
2665
- bytes = await transport.fetchUrl(response.url, ref?.byteLength);
2666
- } else {
2667
- bytes = response.bytes;
2668
- }
2669
- // §5.9.5 inherits §5.1: the declared size caps the body and the content
2670
- // address must match. On the url path either failure invalidates the
2671
- // fetch (no fall-through); the next fetchBlob re-requests the endpoint.
2672
- if (bytes.byteLength > (ref?.byteLength ?? bytes.byteLength)) {
2673
- throw invalidRequest('blob exceeds its byteLength');
2674
- }
2675
- if ((await computeBlobId(bytes)) !== blobId) {
2676
- throw invalidRequest('blob content address mismatch');
2677
- }
2678
- putCachedBlob(this.#db, blobId, bytes, this.#now());
2679
- this.#db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
2680
- this.#enforceBlobCacheCap();
2681
- const stored = getCachedBlob(this.#db, blobId);
2682
- if (stored === undefined) {
2683
- throw invalidRequest('blob cache write failed');
2684
- }
2685
- return stored;
2652
+ // §5.9.5 inherits §5.1: the declared size caps the body and the content
2653
+ // address must match. On the url path either failure invalidates the
2654
+ // fetch (no fall-through); the next fetchBlob re-requests the endpoint.
2655
+ if (bytes.byteLength > (ref?.byteLength ?? bytes.byteLength)) {
2656
+ throw invalidRequest('blob exceeds its byteLength');
2657
+ }
2658
+ if ((await computeBlobId(bytes)) !== blobId) {
2659
+ throw invalidRequest('blob content address mismatch');
2660
+ }
2661
+ putCachedBlob(this.#db, blobId, bytes, this.#now());
2662
+ this.#db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
2663
+ this.#enforceBlobCacheCap();
2664
+ const stored = getCachedBlob(this.#db, blobId);
2665
+ if (stored === undefined) {
2666
+ throw invalidRequest('blob cache write failed');
2667
+ }
2668
+ return stored;
2669
+ });
2686
2670
  }
2687
2671
 
2688
2672
  /** §5.9.7 B1: trim the blob cache to the configured cap (no-op if unset). */
@@ -3193,12 +3177,10 @@ export class SyncClient {
3193
3177
  if (resolved === undefined) {
3194
3178
  throw outcomeNotFound();
3195
3179
  }
3196
- this.#conflicts = this.#conflicts.filter(
3197
- (record) => record.clientCommitId !== input.clientCommitId,
3198
- );
3199
- this.#rejections = this.#rejections.filter(
3200
- (record) => record.clientCommitId !== input.clientCommitId,
3201
- );
3180
+ const kept = (record: { readonly clientCommitId: string }) =>
3181
+ record.clientCommitId !== input.clientCommitId;
3182
+ this.#conflicts = this.#conflicts.filter(kept);
3183
+ this.#rejections = this.#rejections.filter(kept);
3202
3184
  pruneCommitOutcomes(this.#db, this.#outcomeRetentionMaxEntries);
3203
3185
  this.#replayOutbox(scope, false);
3204
3186
  for (const operation of current.operations ?? [])
@@ -3434,21 +3416,25 @@ export class SyncClient {
3434
3416
  const baseKey = windowBaseKey(base);
3435
3417
  const wanted = new Set(units);
3436
3418
  const live = loadWindowUnits(this.#db, baseKey);
3437
- // §7.8: a widen registers like `subscribe` and never waits for a
3438
- // running round.
3439
- for (const value of wanted) {
3440
- if (live.some((entry) => entry.value === value)) continue;
3419
+ // §7.8: a widen registers its values like `subscribeMany`, in one
3420
+ // transaction, and never waits for a running round.
3421
+ const added = [...wanted].filter(
3422
+ (value) => !live.some((entry) => entry.value === value),
3423
+ );
3424
+ if (added.length > 0) {
3441
3425
  this.#applyBatch((batch) => {
3442
- const { id, changed: registered } = this.#holdUnit(
3443
- windowUnitScopes(base, value),
3444
- false,
3445
- );
3446
- this.#db.exec(
3447
- `INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`,
3448
- [baseKey, value, id],
3449
- );
3450
- this.#needsPull ||= registered;
3451
- batch.window(baseKey, value);
3426
+ for (const value of added) {
3427
+ const { id, changed: registered } = this.#holdUnit(
3428
+ windowUnitScopes(base, value),
3429
+ false,
3430
+ );
3431
+ this.#db.exec(
3432
+ `INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`,
3433
+ [baseKey, value, id],
3434
+ );
3435
+ this.#needsPull ||= registered;
3436
+ batch.window(baseKey, value);
3437
+ }
3452
3438
  batch.status();
3453
3439
  });
3454
3440
  changed = true;
@@ -3818,9 +3804,7 @@ export class SyncClient {
3818
3804
  );
3819
3805
  for (const operation of operations) {
3820
3806
  if (operation.op !== 'upsert') continue;
3821
- const table = this.#schema.tables.get(operation.table);
3822
- if (table === undefined) continue;
3823
- for (const column of table.columns) {
3807
+ for (const column of this.#table(operation.table).columns) {
3824
3808
  if (column.type !== 'blob_ref') continue;
3825
3809
  const raw = operation.values?.[column.name];
3826
3810
  if (typeof raw !== 'string') continue;
@@ -4227,13 +4211,6 @@ export class SyncClient {
4227
4211
  if (next.errorCode !== undefined) this.#discardPreviousVersion();
4228
4212
  }
4229
4213
 
4230
- /** The request-level lease error codes (§7.3.4): stop-and-surface. */
4231
- #isLeaseErrorCode(code: string): boolean {
4232
- return (
4233
- code === 'sync.auth_lease_required' || code === 'sync.auth_lease_revoked'
4234
- );
4235
- }
4236
-
4237
4214
  /**
4238
4215
  * §7.4.4: encode every pending outbox commit with the current codec. A
4239
4216
  * commit that cannot re-encode under the new schema (an
@@ -4407,7 +4384,7 @@ export class SyncClient {
4407
4384
  kind,
4408
4385
  limit,
4409
4386
  size,
4410
- ...(clientCommitId !== undefined ? { clientCommitId } : {}),
4387
+ clientCommitId,
4411
4388
  }),
4412
4389
  );
4413
4390
  }
@@ -4591,7 +4568,20 @@ export class SyncClient {
4591
4568
  this.#lastRound = {
4592
4569
  status: 'succeeded',
4593
4570
  ...round(),
4594
- counters: this.#diagnosticRoundCounters(summary),
4571
+ counters: {
4572
+ pushed: summary.pushed,
4573
+ applied: summary.applied.length,
4574
+ rejected: summary.rejected.length,
4575
+ retryable: summary.retryable.length,
4576
+ conflicts: summary.conflicts.length,
4577
+ commitsApplied: summary.commitsApplied,
4578
+ segmentRowsApplied: summary.segmentRowsApplied,
4579
+ bootstrapping: summary.bootstrapping.length,
4580
+ resets: summary.resets.length,
4581
+ revoked: summary.revoked.length,
4582
+ failed: summary.failed.length,
4583
+ deferredCommits: summary.deferredCommits ?? 0,
4584
+ },
4595
4585
  };
4596
4586
  this.#emitDiagnostics();
4597
4587
  return summary;
@@ -4661,23 +4651,6 @@ export class SyncClient {
4661
4651
  });
4662
4652
  }
4663
4653
 
4664
- #diagnosticRoundCounters(summary: SyncSummary): DiagnosticRoundCounters {
4665
- return {
4666
- pushed: summary.pushed,
4667
- applied: summary.applied.length,
4668
- rejected: summary.rejected.length,
4669
- retryable: summary.retryable.length,
4670
- conflicts: summary.conflicts.length,
4671
- commitsApplied: summary.commitsApplied,
4672
- segmentRowsApplied: summary.segmentRowsApplied,
4673
- bootstrapping: summary.bootstrapping.length,
4674
- resets: summary.resets.length,
4675
- revoked: summary.revoked.length,
4676
- failed: summary.failed.length,
4677
- deferredCommits: summary.deferredCommits ?? 0,
4678
- };
4679
- }
4680
-
4681
4654
  #diagnosticCode(code: string): string {
4682
4655
  return isStableCode(code) ? code : 'client.unknown_failure';
4683
4656
  }
@@ -4738,7 +4711,13 @@ export class SyncClient {
4738
4711
  frameBytes,
4739
4712
  deferred: encodedDeferred,
4740
4713
  } = logEpoch === undefined
4741
- ? { pushFrames: [], outbox: [], frameBytes: [], deferred: 0 }
4714
+ ? {
4715
+ pushFrames: [],
4716
+ outbox: [],
4717
+ frameBytes: [],
4718
+ // §2.1: the outbox waits for the next, epoch-bound request.
4719
+ deferred: countOutbox(this.#db),
4720
+ }
4742
4721
  : await this.#encodeOutboxForPush(header, pull, byteCap);
4743
4722
  // §5.9.7 B4: upload pending blobs BEFORE pushing rows that reference
4744
4723
  // them, so the server-side existence check (§6.6) passes.
@@ -4781,6 +4760,8 @@ export class SyncClient {
4781
4760
  ...(report !== undefined
4782
4761
  ? [{ type: 'ROUND_FAILURE' as const, code: report }]
4783
4762
  : []),
4763
+ // §2.1: a replica without an epoch holds nothing bound to one.
4764
+ ...(logEpoch === undefined ? [{ type: 'ADOPT_EPOCH' as const }] : []),
4784
4765
  pull,
4785
4766
  ...(travels
4786
4767
  ? registered.map(
@@ -4874,8 +4855,20 @@ export class SyncClient {
4874
4855
  } catch (caught) {
4875
4856
  // §1.5: a round that ended without a complete, decodable response is a
4876
4857
  // failure the server could not see — keep its code for the next request.
4877
- const unseen = this.#unseenRoundFailureCode(caught);
4878
- if (unseen !== undefined) this.#roundFailureCode = unseen;
4858
+ // An undecodable/truncated body, a rejected fetch, a failed body read,
4859
+ // or a socket round lost mid-flight; an HTTP-status rejection and an
4860
+ // in-band `ERROR` frame were recorded by the server and stay
4861
+ // unreported. The frame carries only codes matching its grammar; a
4862
+ // code that does not match would turn the whole request into a decode
4863
+ // error.
4864
+ if (
4865
+ (caught instanceof DecodeError ||
4866
+ (caught instanceof ClientSyncError &&
4867
+ caught.code === 'sync.transport_failed' &&
4868
+ caught.details?.['causeKind'] !== 'status')) &&
4869
+ isRoundFailureCode(caught.code)
4870
+ )
4871
+ this.#roundFailureCode = caught.code;
4879
4872
  // §7.8: a round the drive deadline aborted ends with the deadline's
4880
4873
  // error, whichever transport call observed the abort.
4881
4874
  const error: unknown = signal?.aborted ? signal.reason : caught;
@@ -4884,8 +4877,12 @@ export class SyncClient {
4884
4877
  // silent retry: the app drives recovery to a live resolver. The
4885
4878
  // error may arrive as a ClientSyncError or as a transport error
4886
4879
  // carrying the server's `.code` (HTTP-JSON / loopback surface, §1.1).
4880
+ // The request-level lease error codes (§7.3.4).
4887
4881
  const code = (error as { code?: unknown }).code;
4888
- if (typeof code === 'string' && this.#isLeaseErrorCode(code)) {
4882
+ if (
4883
+ code === 'sync.auth_lease_required' ||
4884
+ code === 'sync.auth_lease_revoked'
4885
+ ) {
4889
4886
  this.#setLeaseState({
4890
4887
  ...(this.#leaseState ?? {}),
4891
4888
  errorCode: code,
@@ -5016,7 +5013,7 @@ export class SyncClient {
5016
5013
  return {
5017
5014
  ...summary,
5018
5015
  ...(deferredCommits > 0 ? { deferredCommits } : {}),
5019
- ...(schemaFloor !== undefined ? { schemaFloor } : {}),
5016
+ ...definedObject({ schemaFloor }),
5020
5017
  budgetExhausted,
5021
5018
  };
5022
5019
  };
@@ -5193,7 +5190,12 @@ export class SyncClient {
5193
5190
  if (this.#realtimePolicy === 'required') {
5194
5191
  // §8.8: the designated realtime path is down. Refuse the round; the
5195
5192
  // HTTP binding is not an alternative for a `required` client.
5196
- throw this.#realtimeUnavailableError();
5193
+ this.#realtimeRetryDelayMs = this.#retryDelayMs;
5194
+ throw new RealtimeUnavailableError(
5195
+ this.#realtimeState,
5196
+ this.#realtimeReasonCode,
5197
+ this.#retryDelayMs,
5198
+ );
5197
5199
  }
5198
5200
  return Promise.resolve()
5199
5201
  .then(() => this.#config.transport(request, signal))
@@ -5210,27 +5212,6 @@ export class SyncClient {
5210
5212
  });
5211
5213
  }
5212
5214
 
5213
- /**
5214
- * §1.5: the code to report for a round that ended without a complete,
5215
- * decodable response — an undecodable/truncated body, a rejected fetch, a
5216
- * failed body read, or a socket round lost mid-flight. An HTTP-status
5217
- * rejection and an in-band `ERROR` frame were recorded by the server and
5218
- * stay unreported.
5219
- */
5220
- #unseenRoundFailureCode(error: unknown): string | undefined {
5221
- const code =
5222
- error instanceof DecodeError
5223
- ? error.code
5224
- : error instanceof ClientSyncError &&
5225
- error.code === 'sync.transport_failed' &&
5226
- error.details?.['causeKind'] !== 'status'
5227
- ? error.code
5228
- : undefined;
5229
- // §1.5: the frame carries only codes matching its grammar; a code that
5230
- // does not match would turn the whole request into a decode error.
5231
- return code !== undefined && isRoundFailureCode(code) ? code : undefined;
5232
- }
5233
-
5234
5215
  /** Abort the in-flight socket round (socket closed or disconnected). */
5235
5216
  #abortPendingRound(reason: string | ClientSyncError): void {
5236
5217
  const round = this.#pendingRound;
@@ -5260,15 +5241,6 @@ export class SyncClient {
5260
5241
  this.#emitDiagnostics();
5261
5242
  }
5262
5243
 
5263
- #realtimeUnavailableError(): RealtimeUnavailableError {
5264
- this.#realtimeRetryDelayMs = this.#retryDelayMs;
5265
- return new RealtimeUnavailableError(
5266
- this.#realtimeState,
5267
- this.#realtimeReasonCode,
5268
- this.#retryDelayMs,
5269
- );
5270
- }
5271
-
5272
5244
  connectRealtime(): Promise<void> {
5273
5245
  this.#requireActive();
5274
5246
  this.#requireTransport();
@@ -5556,12 +5528,6 @@ export class SyncClient {
5556
5528
  this.#socket?.send(JSON.stringify({ type: 'ack', cursor }));
5557
5529
  }
5558
5530
 
5559
- /** §8.2: after a pull round, ack the replica cursor while it is `>= 0`. */
5560
- #ackAfterPull(): void {
5561
- const cursor = replicaCursor(this.#db);
5562
- if (this.#socket !== undefined && cursor >= 0) this.#sendAck(cursor);
5563
- }
5564
-
5565
5531
  // -- response processing ------------------------------------------------------
5566
5532
 
5567
5533
  /**
@@ -5628,7 +5594,7 @@ export class SyncClient {
5628
5594
  if (header.resetRequired) {
5629
5595
  if (mode !== 'pull' || message.frames.length !== 1) {
5630
5596
  throw invalidHostResponse(
5631
- 'a log-epoch reset response must contain only RESP_HEADER',
5597
+ 'a log-epoch reset response carries frames after RESP_HEADER',
5632
5598
  );
5633
5599
  }
5634
5600
  return {
@@ -5637,7 +5603,10 @@ export class SyncClient {
5637
5603
  bootstrapping: [],
5638
5604
  };
5639
5605
  }
5640
- if (currentLogEpoch === undefined || currentLogEpoch !== header.logEpoch) {
5606
+ // §2.1 adoption: the epoch is stored before any section applies.
5607
+ if (currentLogEpoch === undefined) {
5608
+ setMeta(this.#db, LOG_EPOCH_META_KEY, header.logEpoch);
5609
+ } else if (currentLogEpoch !== header.logEpoch) {
5641
5610
  throw invalidHostResponse(
5642
5611
  'the server changed logEpoch without requiring a reset',
5643
5612
  );
@@ -5715,13 +5684,19 @@ export class SyncClient {
5715
5684
  prefetch();
5716
5685
  }
5717
5686
 
5718
- // Each durable observer transaction emits its own revisioned batch.
5719
- // Async decrypt/download work happens outside SQLite transactions.
5720
- this.#beginDiagnosticsDeferral();
5721
- try {
5687
+ // §1.4 rule 4: the frames of unit sections run in coalesced apply
5688
+ // transactions of at most 1,024 rows, each frame or block in its own
5689
+ // savepoint with its own revision, published after the COMMIT; the host
5690
+ // yields to local reads after each COMMIT. Every other frame runs outside
5691
+ // one, and downloads, decryption, images and COMMIT frames are awaited
5692
+ // outside any transaction.
5693
+ const steps = function* (
5694
+ this: SyncClient,
5695
+ ): Generator<ApplyStep, void, unknown> {
5722
5696
  for (let index = 1; index < message.frames.length; index += 1) {
5723
5697
  const frame = message.frames[index];
5724
5698
  if (frame === undefined) break;
5699
+ if (!/^(SUB|SEGMENT|UNIT)_/.test(frame.type)) yield -1;
5725
5700
  switch (frame.type) {
5726
5701
  case 'RESP_HEADER':
5727
5702
  break;
@@ -5871,14 +5846,16 @@ export class SyncClient {
5871
5846
  const pending = prefetched.get(frame);
5872
5847
  prefetched.delete(frame);
5873
5848
  if (pending !== undefined) reporting = frame;
5849
+ const { scopes } = section.unit;
5874
5850
  try {
5875
- bytes = await (pending ??
5851
+ bytes = (yield () =>
5852
+ pending ??
5876
5853
  this.#downloadSegment(
5877
5854
  frame,
5878
- section.unit.scopes,
5855
+ scopes,
5879
5856
  (bytesReceived) => this.#progress.update({ bytesReceived }),
5880
5857
  signal,
5881
- ));
5858
+ )) as Uint8Array;
5882
5859
  } catch (error) {
5883
5860
  // §5.5: access changed since the response. The unit stays
5884
5861
  // catching up with its cursor unpersisted, and the next
@@ -5907,7 +5884,7 @@ export class SyncClient {
5907
5884
  0,
5908
5885
  ),
5909
5886
  });
5910
- await this.#applySegmentOrFail(
5887
+ yield* this.#applySegmentOrFail(
5911
5888
  section,
5912
5889
  summary,
5913
5890
  image?.table ?? segment!.table,
@@ -5921,27 +5898,27 @@ export class SyncClient {
5921
5898
  ? image.rowCount > 0
5922
5899
  : segment!.blocks.some((block) => block.length > 0),
5923
5900
  resetEpoch,
5924
- image !== undefined,
5925
5901
  );
5926
5902
  return image !== undefined
5927
- ? applySqliteSegment(
5928
- this.#db,
5929
- this.#schema,
5930
- table,
5931
- bytes,
5932
- image,
5933
- {
5934
- ...options,
5935
- appliedRows,
5936
- onChunk: (rows) =>
5937
- recordAppliedRows(
5938
- this.#db,
5939
- unitId,
5940
- image.segmentId,
5941
- rows,
5942
- ),
5943
- },
5944
- )
5903
+ ? () =>
5904
+ applySqliteSegment(
5905
+ this.#db,
5906
+ this.#schema,
5907
+ table,
5908
+ bytes,
5909
+ image,
5910
+ {
5911
+ ...options,
5912
+ appliedRows,
5913
+ onChunk: (rows) =>
5914
+ recordAppliedRows(
5915
+ this.#db,
5916
+ unitId,
5917
+ image.segmentId,
5918
+ rows,
5919
+ ),
5920
+ },
5921
+ )
5945
5922
  : applyRowsSegment(
5946
5923
  this.#db,
5947
5924
  this.#schema,
@@ -6033,7 +6010,8 @@ export class SyncClient {
6033
6010
  );
6034
6011
  });
6035
6012
  if (changes.length > 0)
6036
- await this.#applyCommit({ ...frame, changes }, summary, mode);
6013
+ yield () =>
6014
+ this.#applyCommit({ ...frame, changes }, summary, mode);
6037
6015
  break;
6038
6016
  }
6039
6017
  case 'LIVE_END': {
@@ -6125,6 +6103,52 @@ export class SyncClient {
6125
6103
  }
6126
6104
  if (errorFrame !== undefined) break;
6127
6105
  }
6106
+ }.call(this);
6107
+ this.#beginDiagnosticsDeferral();
6108
+ try {
6109
+ for (let step = steps.next(); !step.done;) {
6110
+ const work = step.value;
6111
+ if (typeof work === 'function') {
6112
+ try {
6113
+ step = steps.next(await work());
6114
+ } catch (error) {
6115
+ step = steps.throw(error);
6116
+ }
6117
+ } else if (work < 0) step = steps.next();
6118
+ else {
6119
+ const held: (() => void)[] = (this.#held = []);
6120
+ const rejections = this.#rejections.length;
6121
+ let failure: { error: unknown } | undefined;
6122
+ try {
6123
+ failure = this.#db.transaction(() => {
6124
+ try {
6125
+ for (let rows = work; ;) {
6126
+ step = steps.next();
6127
+ const next = step.value;
6128
+ if (
6129
+ typeof next !== 'number' ||
6130
+ next < 0 ||
6131
+ (rows > 0 && rows + next > 1024)
6132
+ )
6133
+ return undefined;
6134
+ rows += next;
6135
+ }
6136
+ } catch (error) {
6137
+ // §1.4 rule 5: the frames before the failing one commit.
6138
+ return { error };
6139
+ }
6140
+ });
6141
+ } catch (error) {
6142
+ this.#rejections.length = rejections;
6143
+ throw error;
6144
+ } finally {
6145
+ this.#held = undefined;
6146
+ }
6147
+ for (const publish of held) publish();
6148
+ if (failure) throw failure.error;
6149
+ await yieldToHost();
6150
+ }
6151
+ }
6128
6152
  } finally {
6129
6153
  this.#endDiagnosticsDeferral();
6130
6154
  }
@@ -6138,7 +6162,9 @@ export class SyncClient {
6138
6162
  this.#emitSyncNeeded('catchup-required');
6139
6163
  }
6140
6164
  } else {
6141
- this.#ackAfterPull();
6165
+ // §8.2: after a pull round, ack the replica cursor while it is `>= 0`.
6166
+ const cursor = replicaCursor(this.#db);
6167
+ if (this.#socket !== undefined && cursor >= 0) this.#sendAck(cursor);
6142
6168
  }
6143
6169
 
6144
6170
  const catchingUp = loadUnits(this.#db)
@@ -6343,7 +6369,23 @@ export class SyncClient {
6343
6369
  this.#encryption,
6344
6370
  (fn) =>
6345
6371
  this.#applyBatch((batch) => {
6346
- this.#recordCommitChanges(batch, frame);
6372
+ // Record before + after scope keys while the transaction is open.
6373
+ for (const change of frame.changes) {
6374
+ const tableName = frame.tables[change.tableIndex];
6375
+ if (tableName === undefined) continue;
6376
+ const table = this.#schema.tables.get(tableName);
6377
+ if (table === undefined) continue;
6378
+ let precise = this.#recordStoredRowScopes(
6379
+ batch,
6380
+ table,
6381
+ change.rowId,
6382
+ );
6383
+ for (const variable of Object.keys(change.scopes)) {
6384
+ if (table.scopePrefixByVariable.has(variable)) precise = true;
6385
+ }
6386
+ batch.changeScopes(table, change.scopes);
6387
+ if (!precise) batch.table(tableName);
6388
+ }
6347
6389
  const result = fn();
6348
6390
  this.#replayOutbox(result, false);
6349
6391
  for (const table of new Set(
@@ -6362,22 +6404,6 @@ export class SyncClient {
6362
6404
  summary.commitsApplied += 1;
6363
6405
  }
6364
6406
 
6365
- /** Record before + after scope keys while the commit transaction is open. */
6366
- #recordCommitChanges(batch: ChangeAccumulator, frame: CommitFrame): void {
6367
- for (const change of frame.changes) {
6368
- const tableName = frame.tables[change.tableIndex];
6369
- if (tableName === undefined) continue;
6370
- const table = this.#schema.tables.get(tableName);
6371
- if (table === undefined) continue;
6372
- let precise = this.#recordStoredRowScopes(batch, table, change.rowId);
6373
- for (const variable of Object.keys(change.scopes)) {
6374
- if (table.scopePrefixByVariable.has(variable)) precise = true;
6375
- }
6376
- batch.changeScopes(table, change.scopes);
6377
- if (!precise) batch.table(tableName);
6378
- }
6379
- }
6380
-
6381
6407
  /** Add the currently materialized row's scope keys; returns whether known. */
6382
6408
  #recordStoredRowScopes(
6383
6409
  batch: ChangeAccumulator,
@@ -6430,7 +6456,6 @@ export class SyncClient {
6430
6456
  owners: () => readonly ScopeMap[],
6431
6457
  hasRows: boolean,
6432
6458
  resetEpoch: number,
6433
- image: boolean,
6434
6459
  ): Parameters<typeof applySqliteSegment>[5] {
6435
6460
  let writeMs = 0;
6436
6461
  let commitMs = 0;
@@ -6458,14 +6483,13 @@ export class SyncClient {
6458
6483
  written = performance.now();
6459
6484
  return result;
6460
6485
  });
6461
- // §7.7: image payloads report their import cost.
6486
+ // §7.7: a payload reports its import cost.
6462
6487
  writeMs += written - started;
6463
6488
  commitMs += performance.now() - written;
6464
- if (image)
6465
- this.#progress.update({
6466
- importWriteMs: Math.round(writeMs),
6467
- importCommitMs: Math.round(commitMs),
6468
- });
6489
+ this.#progress.update({
6490
+ importWriteMs: Math.round(writeMs),
6491
+ importCommitMs: Math.round(commitMs),
6492
+ });
6469
6493
  return result;
6470
6494
  },
6471
6495
  };
@@ -6477,7 +6501,7 @@ export class SyncClient {
6477
6501
  * pair's scope first (§5.6). A fail-closed error (no local scope-column
6478
6502
  * mapping) marks the unit `failed` without failing the whole response.
6479
6503
  */
6480
- async #applySegmentOrFail(
6504
+ *#applySegmentOrFail(
6481
6505
  section: OpenSection,
6482
6506
  summary: MutableSummary,
6483
6507
  tableName: string,
@@ -6486,8 +6510,8 @@ export class SyncClient {
6486
6510
  clearFirst: boolean,
6487
6511
  effective: ScopeMap,
6488
6512
  owners: () => readonly ScopeMap[],
6489
- ) => number | Promise<number>,
6490
- ): Promise<void> {
6513
+ ) => Generator<ApplyStep, number, unknown> | (() => Promise<number>),
6514
+ ): Generator<ApplyStep, void, unknown> {
6491
6515
  const { unit } = section;
6492
6516
  const table = this.#table(tableName);
6493
6517
  const effective = covers(table, unit.scopes)
@@ -6506,7 +6530,7 @@ export class SyncClient {
6506
6530
  unit.id,
6507
6531
  ]);
6508
6532
  try {
6509
- summary.segmentRowsApplied += await apply(
6533
+ const work = apply(
6510
6534
  table,
6511
6535
  clearFirst,
6512
6536
  effective,
@@ -6519,6 +6543,8 @@ export class SyncClient {
6519
6543
  ? unitOwners(loadUnits(this.#db), table, unit.id)
6520
6544
  : []),
6521
6545
  );
6546
+ summary.segmentRowsApplied +=
6547
+ typeof work === 'function' ? ((yield work) as number) : yield* work;
6522
6548
  section.seeded.add(tableName);
6523
6549
  } catch (error) {
6524
6550
  if (
@@ -6597,7 +6623,7 @@ export class SyncClient {
6597
6623
  ),
6598
6624
  byteLength: frame.byteLength,
6599
6625
  onProgress,
6600
- ...(signal !== undefined ? { signal } : {}),
6626
+ ...definedObject({ signal }),
6601
6627
  });
6602
6628
  }
6603
6629
  // §5.4: the descriptor's byteLength caps the body; §5.1: verify the
@@ -6870,26 +6896,6 @@ export class SyncClient {
6870
6896
  upsertLocalRow(this.#db, table, values, image.syncVersion);
6871
6897
  }
6872
6898
 
6873
- #legacyUndoOptimisticRows(
6874
- commit: OutboxCommit,
6875
- batch: ChangeAccumulator,
6876
- ): void {
6877
- for (const operation of commit.operations) {
6878
- if (operation.op !== 'upsert') continue;
6879
- const table = this.#schema.tables.get(operation.table);
6880
- if (table === undefined) continue;
6881
- const row = this.#rowById(
6882
- table,
6883
- operation.rowId,
6884
- `${quoteIdent(SYNC_VERSION_COLUMN)} AS v`,
6885
- );
6886
- if (row !== undefined && row.v === OPTIMISTIC_VERSION) {
6887
- batch.table(table.name);
6888
- deleteLocalRow(this.#db, table, operation.rowId);
6889
- }
6890
- }
6891
- }
6892
-
6893
6899
  #rollbackFailedCommit(
6894
6900
  commit: OutboxCommit,
6895
6901
  batch: ChangeAccumulator,
@@ -6912,7 +6918,21 @@ export class SyncClient {
6912
6918
  } else {
6913
6919
  // Pending commits written by older clients have no before-images.
6914
6920
  // Preserve the old fail-closed behavior rather than guessing a base.
6915
- this.#legacyUndoOptimisticRows(commit, batch);
6921
+ for (const operation of commit.operations) {
6922
+ if (operation.op !== 'upsert') continue;
6923
+ const table = this.#schema.tables.get(operation.table);
6924
+ if (
6925
+ table !== undefined &&
6926
+ this.#rowById(
6927
+ table,
6928
+ operation.rowId,
6929
+ `${quoteIdent(SYNC_VERSION_COLUMN)} AS v`,
6930
+ )?.v === OPTIMISTIC_VERSION
6931
+ ) {
6932
+ batch.table(table.name);
6933
+ deleteLocalRow(this.#db, table, operation.rowId);
6934
+ }
6935
+ }
6916
6936
  }
6917
6937
  deleteOutboxCommit(this.#db, commit.clientCommitId);
6918
6938
 
@@ -7268,9 +7288,7 @@ export class SyncClient {
7268
7288
 
7269
7289
  function requireRowId(value: unknown): string {
7270
7290
  if (typeof value === 'string' && value.length > 0) return value;
7271
- if (typeof value === 'number' && Number.isSafeInteger(value)) {
7291
+ if (Number.isSafeInteger(value) || typeof value === 'boolean')
7272
7292
  return String(value);
7273
- }
7274
- if (typeof value === 'boolean') return String(value);
7275
7293
  throw invalidRequest('a mutation requires a non-empty primary key');
7276
7294
  }