@syncular/client 0.74.0 → 0.76.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/apply.d.ts CHANGED
@@ -89,6 +89,11 @@ export declare function applySqliteSegment(db: ClientDatabase, schema: CompiledC
89
89
  readonly isCurrent?: () => boolean;
90
90
  /** Monotonic milliseconds that time each chunk. */
91
91
  readonly now?: () => number;
92
+ /**
93
+ * Table → its next chunk size, carried from one range image of a table
94
+ * to the next (§5.3).
95
+ */
96
+ readonly chunkRows?: Map<string, number>;
92
97
  }): Promise<number>;
93
98
  /**
94
99
  * Apply a decoded rows segment: each block in one local transaction
package/dist/apply.js CHANGED
@@ -364,11 +364,12 @@ export async function applySqliteSegment(db, schema, table, bytes, descriptor, o
364
364
  };
365
365
  let [boundary, where, params] = range(undefined, 1024);
366
366
  let applied = 0;
367
- // §5.3: the first chunk holds 1,024 rows. A later chunk holds at least
367
+ // §5.3: a table's first chunk holds 1,024 rows; a later range image
368
+ // starts at the size its previous range reached. A later chunk holds at least
368
369
  // 1,024 rows, at most twice the rows of the one before, at most 65,536,
369
370
  // and at most the rows the one before applied per 250 ms from its start
370
371
  // to its COMMIT; a replaying chunk holds one statement.
371
- for (let size = 1024;;) {
372
+ for (let size = (!options.clearFirst && options.chunkRows?.get(table.name)) || 1024;;) {
372
373
  if (options.isCurrent?.() === false)
373
374
  return applied;
374
375
  const started = now();
@@ -424,6 +425,7 @@ export async function applySqliteSegment(db, schema, table, bytes, descriptor, o
424
425
  if (boundary === undefined || applied === count)
425
426
  break;
426
427
  size = Math.max(1024, Math.min(2 * chunk, 65_536, Math.floor((chunk * 250) / duration)));
428
+ options.chunkRows?.set(table.name, size);
427
429
  [boundary, where, params] = range(boundary, Math.min(1024, size));
428
430
  }
429
431
  if (applied !== count)
package/dist/client.js CHANGED
@@ -69,6 +69,10 @@ async function sha256Hex(bytes) {
69
69
  .map((b) => b.toString(16).padStart(2, '0'))
70
70
  .join('');
71
71
  }
72
+ const invalidHostResponse = (message) => new ClientSyncError('client.invalid_host_response', message);
73
+ const outcomeNotFound = () => new ClientSyncError('sync.outcome_not_found', 'no durable outcome exists for the commit');
74
+ /** The position a unit presents in a pull (§5.6 applied-segment rule). */
75
+ const position = (unit) => JSON.stringify([unit.id, unit.cursor, unit.bootstrapState]);
72
76
  function emptySummary(pushed) {
73
77
  return {
74
78
  pushed,
@@ -192,6 +196,13 @@ export class SyncClient {
192
196
  #pendingRound;
193
197
  #needsPull = false;
194
198
  #progress = new ProgressEmitter();
199
+ /**
200
+ * §5.6 applied-segment rule: unit position → the segments applied in
201
+ * sections whose SUB_END did not persist.
202
+ */
203
+ #appliedSegments = new Map();
204
+ /** §5.3: a table's next image chunk size, carried across its ranges. */
205
+ #chunkRows = new Map();
195
206
  #progressAttempt = 0;
196
207
  /**
197
208
  * True from the synchronous entry of `sync()` until its serialized round
@@ -1847,7 +1858,7 @@ export class SyncClient {
1847
1858
  this.#requireActive();
1848
1859
  const current = readCommitOutcome(this.#db, input.clientCommitId);
1849
1860
  if (current === undefined) {
1850
- throw new ClientSyncError('sync.outcome_not_found', `no durable outcome exists for ${JSON.stringify(input.clientCommitId)}`);
1861
+ throw outcomeNotFound();
1851
1862
  }
1852
1863
  if (current.resolution !== 'active')
1853
1864
  return current;
@@ -1879,7 +1890,7 @@ export class SyncClient {
1879
1890
  ]);
1880
1891
  const resolved = persistCommitOutcomeResolution(this.#db, input, this.#now());
1881
1892
  if (resolved === undefined) {
1882
- throw new ClientSyncError('sync.outcome_not_found', `no durable outcome exists for ${JSON.stringify(input.clientCommitId)}`);
1893
+ throw outcomeNotFound();
1883
1894
  }
1884
1895
  this.#conflicts = this.#conflicts.filter((record) => record.clientCommitId !== input.clientCommitId);
1885
1896
  this.#rejections = this.#rejections.filter((record) => record.clientCommitId !== input.clientCommitId);
@@ -3850,7 +3861,7 @@ export class SyncClient {
3850
3861
  const currentLogEpoch = getMeta(this.#db, LOG_EPOCH_META_KEY);
3851
3862
  if (header.resetRequired) {
3852
3863
  if (mode !== 'pull' || message.frames.length !== 1) {
3853
- throw new ClientSyncError('client.invalid_host_response', 'a log-epoch reset response must contain only RESP_HEADER');
3864
+ throw invalidHostResponse('a log-epoch reset response must contain only RESP_HEADER');
3854
3865
  }
3855
3866
  return {
3856
3867
  ...summary,
@@ -3859,7 +3870,7 @@ export class SyncClient {
3859
3870
  };
3860
3871
  }
3861
3872
  if (currentLogEpoch === undefined || currentLogEpoch !== header.logEpoch) {
3862
- throw new ClientSyncError('client.invalid_host_response', 'the server changed logEpoch without requiring a reset');
3873
+ throw invalidHostResponse('the server changed logEpoch without requiring a reset');
3863
3874
  }
3864
3875
  let section;
3865
3876
  let errorFrame;
@@ -3874,7 +3885,9 @@ export class SyncClient {
3874
3885
  // bootstrap overlaps its page fetches instead of paying one blocking round
3875
3886
  // trip per page. At most four downloads run at once, the Rust native
3876
3887
  // transport's pool size: enough to overlap round trips, while the client
3877
- // holds at most four bodies, the one being applied and three ahead. The
3888
+ // holds at most four bodies, the one being applied and three ahead. An
3889
+ // image starts only beside at most one other body, so at most two are
3890
+ // images (§5.3 client memory); later descriptors wait in order. The
3878
3891
  // loop takes each body out of the map before applying it and tops the
3879
3892
  // window up at the next descriptor. Eligibility mirrors the loop: a
3880
3893
  // descriptor outside an applied section or from an unconfigured
@@ -3884,7 +3897,9 @@ export class SyncClient {
3884
3897
  const queued = [];
3885
3898
  const prefetched = new Map();
3886
3899
  const prefetch = () => {
3887
- for (let next; prefetched.size < 4 && (next = queued.shift());) {
3900
+ for (let next; prefetched.size < 4 &&
3901
+ !(queued[0]?.[0].mediaType === 'sqlite' && prefetched.size > 1) &&
3902
+ (next = queued.shift());) {
3888
3903
  const [frame, scopes] = next;
3889
3904
  const pending = this.#downloadSegment(frame, scopes, (bytes) => {
3890
3905
  // Only the descriptor the loop is applying reports bytes; a
@@ -3913,8 +3928,7 @@ export class SyncClient {
3913
3928
  else if (frame.type === 'SEGMENT_REF' &&
3914
3929
  open !== undefined &&
3915
3930
  this.#config.segments !== undefined &&
3916
- !(frame.mediaType === 'sqlite' &&
3917
- (this.#acceptMask() & ACCEPT_SQLITE) === 0)) {
3931
+ !this.#appliedSegments.get(position(open))?.has(frame.segmentId)) {
3918
3932
  queued.push([frame, open.scopes]);
3919
3933
  }
3920
3934
  }
@@ -4009,10 +4023,21 @@ export class SyncClient {
4009
4023
  if (section === undefined || section.skip)
4010
4024
  break;
4011
4025
  const unitId = section.unit.id;
4012
- if (frame.type === 'SEGMENT_REF' &&
4013
- frame.mediaType === 'sqlite' &&
4014
- (this.#acceptMask() & ACCEPT_SQLITE) === 0) {
4015
- throw invalidRequest('SEGMENT_REF mediaType sqlite was not advertised in accept (§4.2)');
4026
+ const image = frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite'
4027
+ ? frame
4028
+ : undefined;
4029
+ // §4.2, §5.3: an image needs accept bit 2 and carries no cursors.
4030
+ if (image !== undefined &&
4031
+ ((this.#acceptMask() & ACCEPT_SQLITE) === 0 ||
4032
+ image.rowCursor !== undefined ||
4033
+ image.nextRowCursor !== undefined))
4034
+ throw invalidRequest('unadvertised or paged sqlite segment (§4.2, §5.3)');
4035
+ const applied = this.#appliedSegments.get(position(section.unit)) ??
4036
+ new Set();
4037
+ if (frame.type === 'SEGMENT_REF' && applied.has(frame.segmentId)) {
4038
+ // §5.6: applied in an earlier attempt at this position.
4039
+ section.seeded.add(frame.table);
4040
+ break;
4016
4041
  }
4017
4042
  let bytes;
4018
4043
  if (frame.type === 'SEGMENT_REF') {
@@ -4052,14 +4077,6 @@ export class SyncClient {
4052
4077
  else {
4053
4078
  bytes = frame.payload;
4054
4079
  }
4055
- const image = frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite'
4056
- ? frame
4057
- : undefined;
4058
- // Images are whole-table; paged image descriptors are invalid.
4059
- if (image !== undefined &&
4060
- (image.rowCursor !== undefined ||
4061
- image.nextRowCursor !== undefined))
4062
- throw invalidRequest('sqlite segments are whole-table: rowCursor/nextRowCursor must be absent (§5.3)');
4063
4080
  const segment = image === undefined ? decodeRowsSegment(bytes) : undefined;
4064
4081
  if (frame.type === 'SEGMENT_INLINE' && segment !== undefined)
4065
4082
  this.#progress.update({
@@ -4079,6 +4096,8 @@ export class SyncClient {
4079
4096
  ? applySqliteSegment(this.#db, this.#schema, table, bytes, image, options)
4080
4097
  : applyRowsSegment(this.#db, this.#schema, table, segment, options, this.#encryption);
4081
4098
  });
4099
+ if (frame.type === 'SEGMENT_REF' && !section.skip)
4100
+ this.#appliedSegments.set(position(section.unit), applied.add(frame.segmentId));
4082
4101
  break;
4083
4102
  }
4084
4103
  case 'SUB_END': {
@@ -4087,8 +4106,10 @@ export class SyncClient {
4087
4106
  // unpersisted so the next round runs the fresh bootstrap.
4088
4107
  if (section !== undefined &&
4089
4108
  !section.skip &&
4090
- this.#localResetEpoch === resetEpoch)
4109
+ this.#localResetEpoch === resetEpoch) {
4091
4110
  this.#finishSection(section, frame, summary);
4111
+ this.#appliedSegments.delete(position(section.unit));
4112
+ }
4092
4113
  section = undefined;
4093
4114
  // The counters stay: `complete` and `failed` report them.
4094
4115
  this.#progress.update({
@@ -4283,7 +4304,7 @@ export class SyncClient {
4283
4304
  // §6.3: applied and cached both drain the outbox — cached means
4284
4305
  // "already applied, you may have missed the ack".
4285
4306
  if (frame.commitSeq === undefined)
4286
- throw new ClientSyncError('client.invalid_host_response', 'ACK lacks a commit sequence');
4307
+ throw invalidHostResponse('ACK lacks a commit sequence');
4287
4308
  retainFailedRows(this.#db, commit, listOutboxBeforeImages(this.#db, commit.clientCommitId), frame.commitSeq);
4288
4309
  this.#setSyncNeeded(true);
4289
4310
  recordCommitOutcome(this.#db, {
@@ -4488,6 +4509,7 @@ export class SyncClient {
4488
4509
  let writeMs = 0;
4489
4510
  let commitMs = 0;
4490
4511
  return {
4512
+ chunkRows: this.#chunkRows,
4491
4513
  clearFirst,
4492
4514
  effective,
4493
4515
  owners,
@@ -4608,7 +4630,7 @@ export class SyncClient {
4608
4630
  onProgress(bytes.byteLength);
4609
4631
  const hash = await sha256Hex(bytes);
4610
4632
  if (`sha256:${hash}` !== frame.segmentId) {
4611
- throw invalidRequest(`segment ${frame.segmentId} failed content-address verification (§5.1)`, true);
4633
+ throw invalidRequest('segment failed content-address verification (§5.1)', true);
4612
4634
  }
4613
4635
  return bytes;
4614
4636
  }
@@ -1055,6 +1055,10 @@ export class ReactiveClientStore {
1055
1055
  waiter.resolve();
1056
1056
  }
1057
1057
  catch (error) {
1058
+ // §5.11: the gate refused the edit before it changed anything. The
1059
+ // claim waits; activation applies it again.
1060
+ if (refusedByPreflight(error))
1061
+ return;
1058
1062
  // A failed window edit may already have committed its first eviction chunk.
1059
1063
  // Reconcile through the core before acknowledging any later claim.
1060
1064
  group.appliedKey = '';
@@ -1092,12 +1096,16 @@ export class ReactiveClientStore {
1092
1096
  this.#queries.onChange(batch);
1093
1097
  this.#windows.onChange(batch);
1094
1098
  if (batch.status !== undefined) {
1099
+ const held = this.status.getSnapshot().status?.securityLifecycle === 'preflight';
1095
1100
  this.status.set({
1096
1101
  status: batch.status,
1097
1102
  leadership: this.client.leadershipSnapshot?.(),
1098
1103
  error: undefined,
1099
1104
  isLoading: false,
1100
1105
  });
1106
+ // Window edits held through the preflight apply on activation.
1107
+ if (held && batch.status.securityLifecycle === 'active')
1108
+ this.#reclaimWindows();
1101
1109
  }
1102
1110
  if (batch.conflictsChanged || batch.rejectionsChanged) {
1103
1111
  this.conflicts.refresh();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/client",
3
- "version": "0.74.0",
3
+ "version": "0.76.0",
4
4
  "description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -121,11 +121,11 @@
121
121
  },
122
122
  "dependencies": {
123
123
  "@sqlite.org/sqlite-wasm": "^3.53.0-build1",
124
- "@syncular/core": "0.74.0"
124
+ "@syncular/core": "0.76.0"
125
125
  },
126
126
  "devDependencies": {
127
127
  "@electric-sql/pglite": "^0.5.4",
128
- "@syncular/server": "0.74.0",
128
+ "@syncular/server": "0.76.0",
129
129
  "playwright": "1.63.0",
130
130
  "vite": "8.3.4"
131
131
  }
package/src/apply.ts CHANGED
@@ -521,6 +521,11 @@ export async function applySqliteSegment(
521
521
  readonly isCurrent?: () => boolean;
522
522
  /** Monotonic milliseconds that time each chunk. */
523
523
  readonly now?: () => number;
524
+ /**
525
+ * Table → its next chunk size, carried from one range image of a table
526
+ * to the next (§5.3).
527
+ */
528
+ readonly chunkRows?: Map<string, number>;
524
529
  },
525
530
  ): Promise<number> {
526
531
  const now = options.now ?? (() => performance.now());
@@ -616,11 +621,16 @@ export async function applySqliteSegment(
616
621
  };
617
622
  let [boundary, where, params] = range(undefined, 1024);
618
623
  let applied = 0;
619
- // §5.3: the first chunk holds 1,024 rows. A later chunk holds at least
624
+ // §5.3: a table's first chunk holds 1,024 rows; a later range image
625
+ // starts at the size its previous range reached. A later chunk holds at least
620
626
  // 1,024 rows, at most twice the rows of the one before, at most 65,536,
621
627
  // and at most the rows the one before applied per 250 ms from its start
622
628
  // to its COMMIT; a replaying chunk holds one statement.
623
- for (let size = 1024; ;) {
629
+ for (
630
+ let size =
631
+ (!options.clearFirst && options.chunkRows?.get(table.name)) || 1024;
632
+ ;
633
+ ) {
624
634
  if (options.isCurrent?.() === false) return applied;
625
635
  const started = now();
626
636
  let chunk = 0;
@@ -710,6 +720,7 @@ export async function applySqliteSegment(
710
720
  1024,
711
721
  Math.min(2 * chunk, 65_536, Math.floor((chunk * 250) / duration)),
712
722
  );
723
+ options.chunkRows?.set(table.name, size);
713
724
  [boundary, where, params] = range(boundary, Math.min(1024, size));
714
725
  }
715
726
  if (applied !== count)
package/src/client.ts CHANGED
@@ -738,6 +738,19 @@ async function sha256Hex(bytes: Uint8Array): Promise<string> {
738
738
  .join('');
739
739
  }
740
740
 
741
+ const invalidHostResponse = (message: string) =>
742
+ new ClientSyncError('client.invalid_host_response', message);
743
+
744
+ const outcomeNotFound = () =>
745
+ new ClientSyncError(
746
+ 'sync.outcome_not_found',
747
+ 'no durable outcome exists for the commit',
748
+ );
749
+
750
+ /** The position a unit presents in a pull (§5.6 applied-segment rule). */
751
+ const position = (unit: UnitRecord) =>
752
+ JSON.stringify([unit.id, unit.cursor, unit.bootstrapState]);
753
+
741
754
  interface OpenSection {
742
755
  readonly start: SubStartFrame;
743
756
  readonly unit: UnitRecord;
@@ -970,6 +983,13 @@ export class SyncClient {
970
983
  #pendingRound: PendingRound | undefined;
971
984
  #needsPull = false;
972
985
  readonly #progress = new ProgressEmitter();
986
+ /**
987
+ * §5.6 applied-segment rule: unit position → the segments applied in
988
+ * sections whose SUB_END did not persist.
989
+ */
990
+ readonly #appliedSegments = new Map<string, Set<string>>();
991
+ /** §5.3: a table's next image chunk size, carried across its ranges. */
992
+ readonly #chunkRows = new Map<string, number>();
973
993
  #progressAttempt = 0;
974
994
  /**
975
995
  * True from the synchronous entry of `sync()` until its serialized round
@@ -2945,10 +2965,7 @@ export class SyncClient {
2945
2965
  this.#requireActive();
2946
2966
  const current = readCommitOutcome(this.#db, input.clientCommitId);
2947
2967
  if (current === undefined) {
2948
- throw new ClientSyncError(
2949
- 'sync.outcome_not_found',
2950
- `no durable outcome exists for ${JSON.stringify(input.clientCommitId)}`,
2951
- );
2968
+ throw outcomeNotFound();
2952
2969
  }
2953
2970
  if (current.resolution !== 'active') return current;
2954
2971
  const replacement = input.replacementClientCommitId;
@@ -2993,10 +3010,7 @@ export class SyncClient {
2993
3010
  this.#now(),
2994
3011
  );
2995
3012
  if (resolved === undefined) {
2996
- throw new ClientSyncError(
2997
- 'sync.outcome_not_found',
2998
- `no durable outcome exists for ${JSON.stringify(input.clientCommitId)}`,
2999
- );
3013
+ throw outcomeNotFound();
3000
3014
  }
3001
3015
  this.#conflicts = this.#conflicts.filter(
3002
3016
  (record) => record.clientCommitId !== input.clientCommitId,
@@ -5380,8 +5394,7 @@ export class SyncClient {
5380
5394
  const currentLogEpoch = getMeta(this.#db, LOG_EPOCH_META_KEY);
5381
5395
  if (header.resetRequired) {
5382
5396
  if (mode !== 'pull' || message.frames.length !== 1) {
5383
- throw new ClientSyncError(
5384
- 'client.invalid_host_response',
5397
+ throw invalidHostResponse(
5385
5398
  'a log-epoch reset response must contain only RESP_HEADER',
5386
5399
  );
5387
5400
  }
@@ -5392,8 +5405,7 @@ export class SyncClient {
5392
5405
  };
5393
5406
  }
5394
5407
  if (currentLogEpoch === undefined || currentLogEpoch !== header.logEpoch) {
5395
- throw new ClientSyncError(
5396
- 'client.invalid_host_response',
5408
+ throw invalidHostResponse(
5397
5409
  'the server changed logEpoch without requiring a reset',
5398
5410
  );
5399
5411
  }
@@ -5412,7 +5424,9 @@ export class SyncClient {
5412
5424
  // bootstrap overlaps its page fetches instead of paying one blocking round
5413
5425
  // trip per page. At most four downloads run at once, the Rust native
5414
5426
  // transport's pool size: enough to overlap round trips, while the client
5415
- // holds at most four bodies, the one being applied and three ahead. The
5427
+ // holds at most four bodies, the one being applied and three ahead. An
5428
+ // image starts only beside at most one other body, so at most two are
5429
+ // images (§5.3 client memory); later descriptors wait in order. The
5416
5430
  // loop takes each body out of the map before applying it and tops the
5417
5431
  // window up at the next descriptor. Eligibility mirrors the loop: a
5418
5432
  // descriptor outside an applied section or from an unconfigured
@@ -5422,7 +5436,12 @@ export class SyncClient {
5422
5436
  const queued: [SegmentRefFrame, UnitScopes][] = [];
5423
5437
  const prefetched = new Map<SegmentRefFrame, Promise<Uint8Array>>();
5424
5438
  const prefetch = () => {
5425
- for (let next; prefetched.size < 4 && (next = queued.shift());) {
5439
+ for (
5440
+ let next;
5441
+ prefetched.size < 4 &&
5442
+ !(queued[0]?.[0].mediaType === 'sqlite' && prefetched.size > 1) &&
5443
+ (next = queued.shift());
5444
+ ) {
5426
5445
  const [frame, scopes] = next;
5427
5446
  const pending = this.#downloadSegment(
5428
5447
  frame,
@@ -5454,10 +5473,7 @@ export class SyncClient {
5454
5473
  frame.type === 'SEGMENT_REF' &&
5455
5474
  open !== undefined &&
5456
5475
  this.#config.segments !== undefined &&
5457
- !(
5458
- frame.mediaType === 'sqlite' &&
5459
- (this.#acceptMask() & ACCEPT_SQLITE) === 0
5460
- )
5476
+ !this.#appliedSegments.get(position(open))?.has(frame.segmentId)
5461
5477
  ) {
5462
5478
  queued.push([frame, open.scopes]);
5463
5479
  }
@@ -5575,14 +5591,27 @@ export class SyncClient {
5575
5591
  case 'SEGMENT_REF': {
5576
5592
  if (section === undefined || section.skip) break;
5577
5593
  const unitId = section.unit.id;
5594
+ const image =
5595
+ frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite'
5596
+ ? frame
5597
+ : undefined;
5598
+ // §4.2, §5.3: an image needs accept bit 2 and carries no cursors.
5578
5599
  if (
5579
- frame.type === 'SEGMENT_REF' &&
5580
- frame.mediaType === 'sqlite' &&
5581
- (this.#acceptMask() & ACCEPT_SQLITE) === 0
5582
- ) {
5600
+ image !== undefined &&
5601
+ ((this.#acceptMask() & ACCEPT_SQLITE) === 0 ||
5602
+ image.rowCursor !== undefined ||
5603
+ image.nextRowCursor !== undefined)
5604
+ )
5583
5605
  throw invalidRequest(
5584
- 'SEGMENT_REF mediaType sqlite was not advertised in accept (§4.2)',
5606
+ 'unadvertised or paged sqlite segment (§4.2, §5.3)',
5585
5607
  );
5608
+ const applied =
5609
+ this.#appliedSegments.get(position(section.unit)) ??
5610
+ new Set<string>();
5611
+ if (frame.type === 'SEGMENT_REF' && applied.has(frame.segmentId)) {
5612
+ // §5.6: applied in an earlier attempt at this position.
5613
+ section.seeded.add(frame.table);
5614
+ break;
5586
5615
  }
5587
5616
  let bytes: Uint8Array;
5588
5617
  if (frame.type === 'SEGMENT_REF') {
@@ -5626,19 +5655,6 @@ export class SyncClient {
5626
5655
  } else {
5627
5656
  bytes = frame.payload;
5628
5657
  }
5629
- const image =
5630
- frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite'
5631
- ? frame
5632
- : undefined;
5633
- // Images are whole-table; paged image descriptors are invalid.
5634
- if (
5635
- image !== undefined &&
5636
- (image.rowCursor !== undefined ||
5637
- image.nextRowCursor !== undefined)
5638
- )
5639
- throw invalidRequest(
5640
- 'sqlite segments are whole-table: rowCursor/nextRowCursor must be absent (§5.3)',
5641
- );
5642
5658
  const segment =
5643
5659
  image === undefined ? decodeRowsSegment(bytes) : undefined;
5644
5660
  if (frame.type === 'SEGMENT_INLINE' && segment !== undefined)
@@ -5689,6 +5705,11 @@ export class SyncClient {
5689
5705
  );
5690
5706
  },
5691
5707
  );
5708
+ if (frame.type === 'SEGMENT_REF' && !section.skip)
5709
+ this.#appliedSegments.set(
5710
+ position(section.unit),
5711
+ applied.add(frame.segmentId),
5712
+ );
5692
5713
  break;
5693
5714
  }
5694
5715
  case 'SUB_END': {
@@ -5699,8 +5720,10 @@ export class SyncClient {
5699
5720
  section !== undefined &&
5700
5721
  !section.skip &&
5701
5722
  this.#localResetEpoch === resetEpoch
5702
- )
5723
+ ) {
5703
5724
  this.#finishSection(section, frame, summary);
5725
+ this.#appliedSegments.delete(position(section.unit));
5726
+ }
5704
5727
  section = undefined;
5705
5728
  // The counters stay: `complete` and `failed` report them.
5706
5729
  this.#progress.update({
@@ -5918,10 +5941,7 @@ export class SyncClient {
5918
5941
  // §6.3: applied and cached both drain the outbox — cached means
5919
5942
  // "already applied, you may have missed the ack".
5920
5943
  if (frame.commitSeq === undefined)
5921
- throw new ClientSyncError(
5922
- 'client.invalid_host_response',
5923
- 'ACK lacks a commit sequence',
5924
- );
5944
+ throw invalidHostResponse('ACK lacks a commit sequence');
5925
5945
  retainFailedRows(
5926
5946
  this.#db,
5927
5947
  commit,
@@ -6186,10 +6206,11 @@ export class SyncClient {
6186
6206
  hasRows: boolean,
6187
6207
  resetEpoch: number,
6188
6208
  image: boolean,
6189
- ): Parameters<typeof applyRowsSegment>[4] {
6209
+ ): Parameters<typeof applySqliteSegment>[5] {
6190
6210
  let writeMs = 0;
6191
6211
  let commitMs = 0;
6192
6212
  return {
6213
+ chunkRows: this.#chunkRows,
6193
6214
  clearFirst,
6194
6215
  effective,
6195
6216
  owners,
@@ -6354,7 +6375,7 @@ export class SyncClient {
6354
6375
  const hash = await sha256Hex(bytes);
6355
6376
  if (`sha256:${hash}` !== frame.segmentId) {
6356
6377
  throw invalidRequest(
6357
- `segment ${frame.segmentId} failed content-address verification (§5.1)`,
6378
+ 'segment failed content-address verification (§5.1)',
6358
6379
  true,
6359
6380
  );
6360
6381
  }
@@ -1251,6 +1251,9 @@ export class ReactiveClientStore {
1251
1251
  }
1252
1252
  for (const waiter of group.waiters.splice(0)) waiter.resolve();
1253
1253
  } catch (error) {
1254
+ // §5.11: the gate refused the edit before it changed anything. The
1255
+ // claim waits; activation applies it again.
1256
+ if (refusedByPreflight(error)) return;
1254
1257
  // A failed window edit may already have committed its first eviction chunk.
1255
1258
  // Reconcile through the core before acknowledging any later claim.
1256
1259
  group.appliedKey = '';
@@ -1289,12 +1292,17 @@ export class ReactiveClientStore {
1289
1292
  this.#queries.onChange(batch);
1290
1293
  this.#windows.onChange(batch);
1291
1294
  if (batch.status !== undefined) {
1295
+ const held =
1296
+ this.status.getSnapshot().status?.securityLifecycle === 'preflight';
1292
1297
  (this.status as ValueEntry<StatusStoreSnapshot>).set({
1293
1298
  status: batch.status,
1294
1299
  leadership: this.client.leadershipSnapshot?.(),
1295
1300
  error: undefined,
1296
1301
  isLoading: false,
1297
1302
  });
1303
+ // Window edits held through the preflight apply on activation.
1304
+ if (held && batch.status.securityLifecycle === 'active')
1305
+ this.#reclaimWindows();
1298
1306
  }
1299
1307
  if (batch.conflictsChanged || batch.rejectionsChanged) {
1300
1308
  this.conflicts.refresh();