@syncular/server 0.95.0 → 0.96.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/README.md CHANGED
@@ -195,20 +195,21 @@ fail at construction. Diagnostic prose stays in `message`; apps should map
195
195
  the stable code/details to localized copy instead of displaying that message.
196
196
 
197
197
  Validators are per-operation. A validator must not mutate the row it receives;
198
- a value only the server decides belongs in a server-owned column, which a
199
- `serverColumns` hook sets before the validator runs
198
+ a value only the server decides belongs in a server-owned column, whose value
199
+ the validator returns
200
200
  ([Server-owned columns](https://syncular.dev/guide-server/#server-owned-columns)).
201
201
 
202
202
  A validator that decides from other rows (a membership, a role, a grant) calls
203
- `context.queryAuthoritative` with the request `storage.queryAuthoritative`
203
+ `context.read.queryAuthoritative` with the request `storage.queryAuthoritative`
204
204
  takes, built from a generated query descriptor. The storage binds every
205
205
  relation to the commit's partition and runs the statement on the push
206
206
  transaction's connection, so the read sees the operations staged before this
207
207
  one and needs no second pool connection. Never call
208
208
  `storage.queryAuthoritative` from a validator: on SQLite and PGlite it waits
209
209
  for the push transaction and the push never completes, and on a pool it reads
210
- committed state. The whole-commit reader, the reaction planner reader, and the
211
- remote command context expose the same method. See
210
+ committed state. `context.read` is the whole-commit candidate-state reader;
211
+ the reaction planner reader and the remote command context expose the same
212
+ method. See
212
213
  [Read a generated query inside a push transaction](https://syncular.dev/guide-remote-operations/#read-a-generated-query-inside-a-push-transaction).
213
214
 
214
215
  For a multi-row or multi-table invariant, install `commitValidator`. It runs
package/dist/context.d.ts CHANGED
@@ -16,7 +16,7 @@ import type { SegmentStore } from './segment-store.js';
16
16
  import type { BlobPresignConfig, BlobUploadPresignConfig, SegmentUrlConfig } from './signed-url.js';
17
17
  import type { SqliteImageBuilder } from './sqlite-image.js';
18
18
  import type { CheckpointDeclaration, PartitionTouch, ServerStorage, StoredCommit } from './storage.js';
19
- import type { CommitValidator, ServerColumnsRegistry, ValidatorRegistry } from './validate.js';
19
+ import type { CommitValidator, ValidatorRegistry } from './validate.js';
20
20
  /** SSP2 body content type (§1.1). */
21
21
  export declare const SSP2_CONTENT_TYPE = "application/vnd.syncular.sync.v2";
22
22
  /** Internal idempotency namespace. Ordinary SSP2 client IDs cannot use it. */
@@ -102,12 +102,6 @@ export interface ServerLimits {
102
102
  * bound gets an image of its own.
103
103
  */
104
104
  readonly maxImageSegmentBytes: number;
105
- /**
106
- * Segment puts one pull keeps in flight. The pull scans and encodes its
107
- * pages in order and stores up to this many at once, so it holds this
108
- * many encoded segments in memory.
109
- */
110
- readonly maxConcurrentSegmentPuts: number;
111
105
  }
112
106
  export declare const DEFAULT_LIMITS: ServerLimits;
113
107
  /** Receives applied commits for realtime fanout (§8.2). */
@@ -155,19 +149,13 @@ export interface SyncServerConfig {
155
149
  * builds no validator context — zero cost. Present ⇒ each push operation
156
150
  * on a listed table runs its validator after decode + §3.4 scope
157
151
  * authorization, inside the commit transaction; a throw rejects the
158
- * whole commit atomically (§6.4) with the host code (§6.3). Kept a host
152
+ * whole commit atomically (§6.4) with the host code (§6.3). On an insert
153
+ * or update the validator may return values of the table's server-owned
154
+ * columns, which are stored and fanned out in the same commit. Kept a host
159
155
  * callback (like `resolveScopes`) — business rules live in the host
160
156
  * process, never on the wire.
161
157
  */
162
158
  readonly validators?: ValidatorRegistry;
163
- /**
164
- * §6.12 per-table server column hooks (`table` → hook). Each accepted
165
- * upsert on a listed table runs its hook inside the push transaction,
166
- * after §3.4 authorization and before §6.7 validation; the returned values
167
- * of the table's server-owned columns are stored and fanned out in the
168
- * same commit.
169
- */
170
- readonly serverColumns?: ServerColumnsRegistry;
171
159
  /**
172
160
  * §6.8 whole-commit validator. When present, the storage serializes this
173
161
  * partition before operation reads/writes, then invokes the callback once
package/dist/context.js CHANGED
@@ -25,9 +25,6 @@ export const DEFAULT_LIMITS = {
25
25
  maxLiveSectionBytes: 16 * 1024 * 1024,
26
26
  // §5.3 DECISION (2026-11-17): a client holds about three images at once.
27
27
  maxImageSegmentBytes: 32 * 1024 * 1024,
28
- // A Workers invocation holds six connections open at once, and an R2 put
29
- // uses one; past six the runtime queues the put.
30
- maxConcurrentSegmentPuts: 6,
31
28
  };
32
29
  /** §1.1: `authenticate` returned a context longer than 256 UTF-8 bytes. */
33
30
  export class AuthContextTooLargeError extends Error {
package/dist/errors.js CHANGED
@@ -226,6 +226,14 @@ export const ERROR_CATALOG = {
226
226
  recommendedAction: 'forceResync',
227
227
  httpStatus: 404,
228
228
  },
229
+ // §1.6: a response still building at the 300 s build deadline. In-band
230
+ // after its header frames, HTTP 503 before them.
231
+ 'sync.segment_build_timeout': {
232
+ category: 'internal',
233
+ retryable: true,
234
+ recommendedAction: 'retryLater',
235
+ httpStatus: 503,
236
+ },
229
237
  'sync.segment_expired': {
230
238
  category: 'not-found',
231
239
  retryable: true,
@@ -53,6 +53,7 @@ function wrapperFor(frame) {
53
53
  switch (frame.type) {
54
54
  case 'RESP_HEADER':
55
55
  return { frames: [frame], index: 0 };
56
+ case 'KEEPALIVE':
56
57
  case 'LEASE':
57
58
  // §7.3.2: LEASE rides immediately after RESP_HEADER.
58
59
  return { frames: [stubHeader(), frame], index: 1 };
package/dist/handler.d.ts CHANGED
@@ -1,9 +1,27 @@
1
1
  import type { SyncRequestContext } from './context.js';
2
+ /**
3
+ * A response's chunks, one per encoded frame (§1.4). `live` turns true when
4
+ * the response goes out before it is built: its header frames and
5
+ * `KEEPALIVE` frames (§1.6) precede the rest, and a later failure is an
6
+ * in-band `ERROR`.
7
+ */
8
+ export interface SyncResponseStream extends AsyncIterable<Uint8Array> {
9
+ readonly live: boolean;
10
+ }
2
11
  /**
3
12
  * Decode and validate the request, then return the streaming response.
4
13
  * Request-validation failures (§1.7) throw `SyncError` here, before any
5
- * bytes are produced; adapters map them to HTTP JSON errors (§1.1).
14
+ * bytes are produced; adapters map them to HTTP JSON errors (§1.1). The
15
+ * first chunk arrives once the response is built or its first `KEEPALIVE`
16
+ * is due (§1.6); a failure before then throws from it, before any byte.
17
+ */
18
+ export declare function createSyncResponseStream(bytes: Uint8Array, ctx: SyncRequestContext): Promise<SyncResponseStream>;
19
+ /**
20
+ * The `POST /sync` body (§1.1): the whole response when it was built before
21
+ * its first `KEEPALIVE` was due, otherwise a stream that carries the header
22
+ * frames and `KEEPALIVE` frames first (§1.6). Adapters compress only the
23
+ * whole response (§1.3).
6
24
  */
7
- export declare function createSyncResponseStream(bytes: Uint8Array, ctx: SyncRequestContext): Promise<AsyncIterable<Uint8Array>>;
25
+ export declare function createSyncResponseBody(bytes: Uint8Array, ctx: SyncRequestContext): Promise<Uint8Array | ReadableStream<Uint8Array>>;
8
26
  /** Byte-concatenating wrapper over the streaming core (§1.4). */
9
27
  export declare function handleSyncRequest(bytes: Uint8Array, ctx: SyncRequestContext): Promise<Uint8Array>;
package/dist/handler.js CHANGED
@@ -11,7 +11,7 @@
11
11
  */
12
12
  import { DecodeError, decodeRequest, echoDigest, registrationDigest, unitId, } from '@syncular/core';
13
13
  import { clockOf, limitsOf, REMOTE_COMMAND_CLIENT_ID_PREFIX, RESOLVER_OUTAGE, touchAuthenticatedPartition, } from './context.js';
14
- import { reportError, SyncError, syncError } from './errors.js';
14
+ import { adapterSyncError, reportError, SyncError, syncError } from './errors.js';
15
15
  import { emitEvent, } from './events.js';
16
16
  import { END_FRAME_BYTES, encodeResponseFrame, responseEnvelopeHeader, } from './frame-bytes.js';
17
17
  import { ACCEPT_EXTERNAL_ROWS, ACCEPT_INLINE_ROWS, bootstrapSegments, clampPullLimits, commitFrame, coveredTables, planBootstrap, planUnit, } from './pull.js';
@@ -558,7 +558,8 @@ async function* streamResponse(plan, ctx, schema, report) {
558
558
  }
559
559
  }
560
560
  else {
561
- const horizonSeq = await ctx.storage.getHorizonSeq(ctx.partition);
561
+ const horizons = await ctx.storage.getHorizons(ctx.partition);
562
+ const { seq: horizonSeq, tables: tableHorizons } = horizons;
562
563
  const R = pull.header.cursor;
563
564
  const echo = echoDigest(units.map(({ plan: unit }) => ({
564
565
  id: unit.id,
@@ -572,14 +573,25 @@ async function* streamResponse(plan, ctx, schema, report) {
572
573
  const liveUnits = units.some((unit) => unit.progress === undefined && unit.plan.status === 'active');
573
574
  // §4.6 replica reset: a live unit with R outside [horizon, max].
574
575
  let liveReset = liveUnits && (R < horizonSeq || R > maxSeq);
575
- const states = units.map(({ plan: unit, progress, restrict }) => {
576
+ // §4.6 unit reset: `cursor` lies below the table horizon of one of
577
+ // the unit's active pairs outside its RESTRICT tables.
578
+ const belowTable = (index, cursor, horizons) => {
579
+ const { plan: unit, restrict } = units[index];
580
+ return [...unit.pairs.keys()].some((table) => cursor < (horizons.get(table) ?? -1) &&
581
+ restrict?.tables.includes(table) !== true);
582
+ };
583
+ const states = units.map(({ plan: unit, progress, restrict }, index) => {
576
584
  if (unit.status === 'revoked')
577
585
  return { kind: 'revoked' };
578
586
  if (progress === undefined)
579
- return { kind: 'live' };
587
+ return !liveReset && belowTable(index, R, tableHorizons)
588
+ ? { kind: 'reset' }
589
+ : { kind: 'live' };
580
590
  // §4.6: a kept cursor outside [horizon, max] cannot join.
581
591
  if (restrict !== undefined)
582
- return restrict.cursor < horizonSeq || restrict.cursor > maxSeq
592
+ return restrict.cursor < horizonSeq ||
593
+ restrict.cursor > maxSeq ||
594
+ belowTable(index, restrict.cursor, tableHorizons)
583
595
  ? { kind: 'reset' }
584
596
  : {
585
597
  kind: 'bootstrap',
@@ -592,7 +604,8 @@ async function* streamResponse(plan, ctx, schema, report) {
592
604
  if (progress.bootstrapState === undefined &&
593
605
  progress.cursor >= 0 &&
594
606
  progress.cursor <= maxSeq) {
595
- return progress.cursor < horizonSeq
607
+ return progress.cursor < horizonSeq ||
608
+ belowTable(index, progress.cursor, tableHorizons)
596
609
  ? { kind: 'reset' }
597
610
  : { kind: 'post', cursor: progress.cursor };
598
611
  }
@@ -619,7 +632,6 @@ async function* streamResponse(plan, ctx, schema, report) {
619
632
  // pull holds the encoded bytes of the puts in flight.
620
633
  const puts = [];
621
634
  const inFlight = new Set();
622
- const maxPuts = limitsOf(ctx).maxConcurrentSegmentPuts;
623
635
  let failed = false;
624
636
  for (let next = 0; next < bootstrapping.length;) {
625
637
  const window = [];
@@ -645,7 +657,7 @@ async function* streamResponse(plan, ctx, schema, report) {
645
657
  ...unit,
646
658
  tables: unit.tables.filter((table) => only.has(table.name)),
647
659
  pairs: new Map([...unit.pairs].filter(([name]) => only.has(name))),
648
- }, progress?.bootstrapState, maxSeq, horizonSeq, plan.logEpoch, restrict?.cursor),
660
+ }, progress?.bootstrapState, maxSeq, horizons, plan.logEpoch, restrict?.cursor),
649
661
  };
650
662
  });
651
663
  for (const { index, plan: bootstrap } of batch) {
@@ -656,7 +668,9 @@ async function* streamResponse(plan, ctx, schema, report) {
656
668
  const frame = step.value;
657
669
  if (frame.type === 'PENDING_REF') {
658
670
  // A rows segment's put runs while later pages are scanned;
659
- // the next page waits while `maxPuts` puts are in flight.
671
+ // the next page waits while six puts are in flight: a
672
+ // Workers invocation holds six connections open at once and
673
+ // an R2 put uses one, so past six the runtime queues the put.
660
674
  const settled = frame.ref.then(() => void inFlight.delete(settled), () => {
661
675
  failed = true;
662
676
  inFlight.delete(settled);
@@ -664,7 +678,7 @@ async function* streamResponse(plan, ctx, schema, report) {
664
678
  inFlight.add(settled);
665
679
  puts.push(frame.ref);
666
680
  frames.push(frame.ref);
667
- while (inFlight.size >= maxPuts) {
681
+ while (inFlight.size >= 6) {
668
682
  await Promise.race(inFlight);
669
683
  if (failed)
670
684
  break;
@@ -709,10 +723,22 @@ async function* streamResponse(plan, ctx, schema, report) {
709
723
  let live;
710
724
  for (;;) {
711
725
  const scanned = units.flatMap(({ plan: unit }, index) => {
712
- const afterSeq = bound(states[index]);
713
- return afterSeq === undefined
714
- ? []
715
- : [{ index, scopes: unit.scopes, afterSeq, tables: unit.pairs }];
726
+ const bounded = bound(states[index]);
727
+ if (bounded === undefined)
728
+ return [];
729
+ // §4.5: a table reaches the unit only above its table horizon,
730
+ // which lies above the bound only for a RESTRICT table.
731
+ const byBound = new Map();
732
+ for (const [table, scopes] of unit.pairs) {
733
+ const afterSeq = Math.max(bounded, tableHorizons.get(table) ?? -1);
734
+ byBound.set(afterSeq, (byBound.get(afterSeq) ?? new Map()).set(table, scopes));
735
+ }
736
+ return [...byBound].map(([afterSeq, tables]) => ({
737
+ index,
738
+ scopes: unit.scopes,
739
+ afterSeq,
740
+ tables,
741
+ }));
716
742
  });
717
743
  if (liveReset || scanned.length === 0)
718
744
  break;
@@ -735,18 +761,24 @@ async function* streamResponse(plan, ctx, schema, report) {
735
761
  live = undefined;
736
762
  continue;
737
763
  }
738
- const recheck = await ctx.storage.getHorizonSeq(ctx.partition);
764
+ const { seq: recheck, tables: recheckTables } = await ctx.storage.getHorizons(ctx.partition);
739
765
  let expired = false;
740
766
  for (const [index, state] of states.entries()) {
741
- // §4.6: a restricted bootstrap's kept cursor is rechecked too.
742
- const afterSeq = bound(state) ??
743
- (state.kind === 'bootstrap' && state.restricted
744
- ? state.pin
745
- : undefined);
746
- if (afterSeq === undefined || afterSeq >= recheck)
767
+ // §4.6: a restricted bootstrap's kept cursor is rechecked too,
768
+ // and a table horizon that rose during the round resets it: its
769
+ // segments were read before the re-key.
770
+ const restricted = state.kind === 'bootstrap' && state.restricted;
771
+ const afterSeq = bound(state) ?? (restricted ? state.pin : undefined);
772
+ if (afterSeq === undefined ||
773
+ (afterSeq >= recheck &&
774
+ !belowTable(index, afterSeq, recheckTables) &&
775
+ !(restricted &&
776
+ [...units[index].plan.pairs.keys()].some((table) => (recheckTables.get(table) ?? -1) >
777
+ (tableHorizons.get(table) ?? -1)))))
747
778
  continue;
748
779
  expired = true;
749
- if (state.kind === 'live')
780
+ // A live unit below a table horizon alone takes a unit reset.
781
+ if (state.kind === 'live' && afterSeq < recheck)
750
782
  liveReset = true;
751
783
  else
752
784
  states[index] = { kind: 'reset' };
@@ -937,15 +969,7 @@ async function* streamResponse(plan, ctx, schema, report) {
937
969
  report.errorCode = error.code;
938
970
  await recordRoundFailure(ctx, clientId, error);
939
971
  // §1.6: in-band ERROR, then END, nothing else.
940
- yield encodeResponseFrame({
941
- type: 'ERROR',
942
- code: error.code,
943
- message: error.message,
944
- category: error.category,
945
- retryable: error.retryable,
946
- recommendedAction: error.recommendedAction,
947
- ...(error.details !== undefined ? { details: error.details } : {}),
948
- }, plan.wireVersion);
972
+ yield errorFrameBytes(error, plan.wireVersion);
949
973
  yield END_FRAME_BYTES;
950
974
  return;
951
975
  }
@@ -953,10 +977,24 @@ async function* streamResponse(plan, ctx, schema, report) {
953
977
  }
954
978
  yield END_FRAME_BYTES;
955
979
  }
980
+ /** The in-band `ERROR` frame of a failure (§1.6). */
981
+ function errorFrameBytes(error, wireVersion) {
982
+ return encodeResponseFrame({
983
+ type: 'ERROR',
984
+ code: error.code,
985
+ message: error.message,
986
+ category: error.category,
987
+ retryable: error.retryable,
988
+ recommendedAction: error.recommendedAction,
989
+ ...(error.details !== undefined ? { details: error.details } : {}),
990
+ }, wireVersion);
991
+ }
956
992
  /**
957
993
  * Decode and validate the request, then return the streaming response.
958
994
  * Request-validation failures (§1.7) throw `SyncError` here, before any
959
- * bytes are produced; adapters map them to HTTP JSON errors (§1.1).
995
+ * bytes are produced; adapters map them to HTTP JSON errors (§1.1). The
996
+ * first chunk arrives once the response is built or its first `KEEPALIVE`
997
+ * is due (§1.6); a failure before then throws from it, before any byte.
960
998
  */
961
999
  export async function createSyncResponseStream(bytes, ctx) {
962
1000
  const sink = ctx.events;
@@ -1093,19 +1131,24 @@ async function createStreamCore(bytes, ctx, events, startedAtMs = 0) {
1093
1131
  await recordRoundFailureCode(ctx, plan.header.clientId, plan.roundFailure.code);
1094
1132
  }
1095
1133
  const report = { outcome: 'ok' };
1096
- // RFC 0007 read-verify-refuse for the streamed read path: the generator
1097
- // builds the whole response (its data reads) before yielding the first
1098
- // frame, then compares the gate token across those reads. Nothing escapes
1099
- // before the verification, and the buffered size is bounded by the request's
1100
- // pull limits and the server's inline segment cap. Every request verifies,
1134
+ // RFC 0007 read-verify-refuse for the streamed read path: the build
1135
+ // buffers the whole response (its data reads), then compares the gate token
1136
+ // across those reads. No push result or section escapes before the
1137
+ // verification, and the buffered size is bounded by the request's pull
1138
+ // limits and the server's inline segment cap. Every request verifies,
1101
1139
  // including a mixed push+pull: a write that already applied when the token
1102
1140
  // changed is durable and replayable under the same commit id / idempotency
1103
1141
  // key, so refusing the request is safe (proved in the serve-gate lane).
1104
- const verified = async function* () {
1105
- const buffered = [];
1142
+ const buffered = [];
1143
+ // Set at the build deadline: a build that finishes later stops before the
1144
+ // serve-gate read and persists nothing.
1145
+ let abandoned = false;
1146
+ const built = (async () => {
1106
1147
  for await (const chunk of streamResponse(plan, ctx, served, report)) {
1107
1148
  buffered.push(chunk);
1108
1149
  }
1150
+ if (abandoned)
1151
+ return false;
1109
1152
  const after = await ctx.storage.readServeGate(ctx.partition, schema.version);
1110
1153
  const changed = serveGateTokenChanged(gate, after);
1111
1154
  const afterRefusal = serveGateRefusal(after, schema.version, ctx.checkpoints);
@@ -1114,13 +1157,117 @@ async function createStreamCore(bytes, ctx, events, startedAtMs = 0) {
1114
1157
  if (changed !== undefined)
1115
1158
  throw serveNotReadyError(changed);
1116
1159
  await report.persist?.();
1117
- for (const chunk of buffered)
1118
- yield chunk;
1160
+ return true;
1161
+ })();
1162
+ // The races below observe the failure; this keeps an abandoned stream's
1163
+ // failure from surfacing as unhandled.
1164
+ built.catch(() => { });
1165
+ // §1.6 header frames: the envelope header, RESP_HEADER and its LEASE.
1166
+ const headerChunks = plan.leaseToEmit !== undefined ? 3 : 2;
1167
+ let live = false;
1168
+ const verified = async function* () {
1169
+ let timer;
1170
+ let elapsedMs = 0;
1171
+ try {
1172
+ for (;;) {
1173
+ let done;
1174
+ try {
1175
+ done = await Promise.race([
1176
+ built,
1177
+ // §1.6: every 10 s, a third of the native client's 30 s socket
1178
+ // round silence bound (§1.8).
1179
+ new Promise((resolve) => {
1180
+ timer = setTimeout(() => resolve(false), 10_000);
1181
+ }),
1182
+ ]);
1183
+ if (!done)
1184
+ elapsedMs += 10_000;
1185
+ // §1.6 build deadline: 300 s, 2.5 times the slowest cold build
1186
+ // measured in production (119.7 s for 67 segment puts). A build
1187
+ // still running then is abandoned like a failed put.
1188
+ if (elapsedMs === 300_000) {
1189
+ abandoned = true;
1190
+ throw syncError('sync.segment_build_timeout', 'the response was not built within the server build deadline');
1191
+ }
1192
+ }
1193
+ catch (thrown) {
1194
+ if (!live)
1195
+ throw thrown;
1196
+ // The header frames went out: the failure is in-band (§1.6), and
1197
+ // one outside the catalog answers `sync.internal_error`.
1198
+ const error = adapterSyncError(thrown, ctx.onError, 'sync', ctx.mapError);
1199
+ report.outcome = 'error';
1200
+ report.errorCode = error.code;
1201
+ await recordRoundFailure(ctx, plan.header.clientId, error);
1202
+ yield errorFrameBytes(error, plan.wireVersion);
1203
+ yield END_FRAME_BYTES;
1204
+ return;
1205
+ }
1206
+ clearTimeout(timer);
1207
+ if (done)
1208
+ break;
1209
+ if (!live && buffered.length < headerChunks)
1210
+ continue;
1211
+ if (!live) {
1212
+ live = true;
1213
+ yield* buffered.slice(0, headerChunks);
1214
+ }
1215
+ yield encodeResponseFrame({ type: 'KEEPALIVE' }, plan.wireVersion);
1216
+ }
1217
+ }
1218
+ finally {
1219
+ clearTimeout(timer);
1220
+ }
1221
+ yield* buffered.slice(live ? headerChunks : 0);
1119
1222
  };
1120
- if (events === undefined) {
1121
- return recordedStream(verified(), ctx, plan.header.clientId);
1223
+ const stream = recordedStream(events === undefined
1224
+ ? verified()
1225
+ : instrumentedStream(verified(), ctx, events, plan, report, bytes.length, startedAtMs), ctx, plan.header.clientId);
1226
+ return {
1227
+ get live() {
1228
+ return live;
1229
+ },
1230
+ [Symbol.asyncIterator]: () => stream,
1231
+ };
1232
+ }
1233
+ /**
1234
+ * The `POST /sync` body (§1.1): the whole response when it was built before
1235
+ * its first `KEEPALIVE` was due, otherwise a stream that carries the header
1236
+ * frames and `KEEPALIVE` frames first (§1.6). Adapters compress only the
1237
+ * whole response (§1.3).
1238
+ */
1239
+ export async function createSyncResponseBody(bytes, ctx) {
1240
+ const stream = await createSyncResponseStream(bytes, ctx);
1241
+ const iterator = stream[Symbol.asyncIterator]();
1242
+ const first = await iterator.next();
1243
+ if (stream.live) {
1244
+ return new ReadableStream({
1245
+ start(controller) {
1246
+ if (!first.done)
1247
+ controller.enqueue(first.value);
1248
+ },
1249
+ async pull(controller) {
1250
+ const step = await iterator.next();
1251
+ if (step.done)
1252
+ controller.close();
1253
+ else
1254
+ controller.enqueue(step.value);
1255
+ },
1256
+ async cancel() {
1257
+ await iterator.return?.();
1258
+ },
1259
+ });
1122
1260
  }
1123
- return recordedStream(instrumentedStream(verified(), ctx, events, plan, report, bytes.length, startedAtMs), ctx, plan.header.clientId);
1261
+ const chunks = first.done ? [] : [first.value];
1262
+ for (let step = await iterator.next(); !step.done; step = await iterator.next())
1263
+ chunks.push(step.value);
1264
+ const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
1265
+ let offset = 0;
1266
+ for (const chunk of chunks) {
1267
+ out.set(chunk, offset);
1268
+ offset += chunk.length;
1269
+ }
1270
+ return out;
1124
1271
  }
1125
1272
  /** Counts response bytes and emits `request.handled` when the stream
1126
1273
  * finishes — including a thrown host failure mid-stream (fail loud). */
@@ -1163,18 +1310,8 @@ async function* instrumentedStream(stream, ctx, events, plan, report, bytesIn, s
1163
1310
  }
1164
1311
  /** Byte-concatenating wrapper over the streaming core (§1.4). */
1165
1312
  export async function handleSyncRequest(bytes, ctx) {
1166
- const stream = await createSyncResponseStream(bytes, ctx);
1167
- const chunks = [];
1168
- let total = 0;
1169
- for await (const chunk of stream) {
1170
- chunks.push(chunk);
1171
- total += chunk.length;
1172
- }
1173
- const out = new Uint8Array(total);
1174
- let offset = 0;
1175
- for (const chunk of chunks) {
1176
- out.set(chunk, offset);
1177
- offset += chunk.length;
1178
- }
1179
- return out;
1313
+ const body = await createSyncResponseBody(bytes, ctx);
1314
+ return body instanceof Uint8Array
1315
+ ? body
1316
+ : new Uint8Array(await new Response(body).arrayBuffer());
1180
1317
  }
@@ -1,4 +1,4 @@
1
- import type { CommitPruneQuery, CommitPruneResult } from './storage.js';
1
+ import type { CommitPruneQuery, CommitPruneResult, PartitionHorizons } from './storage.js';
2
2
  /**
3
3
  * Postgres server storage: the production database path.
4
4
  *
@@ -74,7 +74,7 @@ import type { AuthoritativeQueryRequest, AuthoritativeQueryResult, CheckpointDec
74
74
  * set, §5.9.5) as an index range, never a scan. `postgres-explain
75
75
  * .test.ts` asserts an `Index` node here too.
76
76
  */
77
- export declare const POSTGRES_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq BIGINT NOT NULL DEFAULT 0,\n horizon_seq BIGINT NOT NULL DEFAULT 0,\n delete_floor BIGINT NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required BOOLEAN NOT NULL DEFAULT FALSE,\n last_authenticated_at_ms BIGINT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\nCREATE INDEX IF NOT EXISTS sync_row_scopes_by_row\n ON sync_row_scopes(partition, tbl, row_id);\nCREATE TABLE IF NOT EXISTS sync_commits(\n partition TEXT NOT NULL, commit_seq BIGINT NOT NULL,\n client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,\n actor_id TEXT NOT NULL, created_at_ms BIGINT NOT NULL,\n writer_version BIGINT,\n PRIMARY KEY(partition, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_commits_by_time\n ON sync_commits(partition, created_at_ms);\nCREATE TABLE IF NOT EXISTS sync_changes(\n partition TEXT NOT NULL, commit_seq BIGINT NOT NULL, idx INTEGER NOT NULL,\n tbl TEXT NOT NULL, row_id TEXT NOT NULL, op SMALLINT NOT NULL,\n row_version BIGINT, scopes JSONB NOT NULL, payload BYTEA,\n PRIMARY KEY(partition, commit_seq, idx)\n);\nCREATE INDEX IF NOT EXISTS sync_changes_by_table\n ON sync_changes(partition, commit_seq, tbl, idx);\nCREATE TABLE IF NOT EXISTS sync_change_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, commit_seq BIGINT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_change_scopes_by_key\n ON sync_change_scopes(partition, var, value, commit_seq);\nCREATE TABLE IF NOT EXISTS sync_push_results(\n partition TEXT NOT NULL, client_id TEXT NOT NULL,\n client_commit_id TEXT NOT NULL, result JSONB NOT NULL,\n outbox_id TEXT, client_seq BIGINT,\n PRIMARY KEY(partition, client_id, client_commit_id)\n);\nALTER TABLE sync_push_results ADD COLUMN IF NOT EXISTS outbox_id TEXT;\nALTER TABLE sync_push_results ADD COLUMN IF NOT EXISTS client_seq BIGINT;\nCREATE TABLE IF NOT EXISTS sync_push_watermarks(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, outbox_id TEXT NOT NULL,\n through_seq BIGINT NOT NULL,\n PRIMARY KEY(partition, client_id, outbox_id)\n);\nCREATE TABLE IF NOT EXISTS sync_reactions(\n partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,\n type TEXT NOT NULL, version INTEGER NOT NULL, payload JSONB NOT NULL,\n source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,\n source_commit_seq BIGINT NOT NULL, created_at_ms BIGINT NOT NULL,\n available_at_ms BIGINT NOT NULL, status TEXT NOT NULL,\n attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,\n lease_owner TEXT, lease_expires_at_ms BIGINT, completed_at_ms BIGINT,\n last_failure JSONB,\n PRIMARY KEY(partition, idempotency_key),\n CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))\n);\nCREATE INDEX IF NOT EXISTS sync_reactions_due\n ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_lease\n ON sync_reactions(partition, status, lease_expires_at_ms);\nCREATE INDEX IF NOT EXISTS sync_reactions_completed\n ON sync_reactions(partition, status, completed_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_dead_letter\n ON sync_reactions(partition, status, available_at_ms, idempotency_key);\nCREATE TABLE IF NOT EXISTS sync_clients(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,\n wire_version INTEGER NOT NULL DEFAULT 1,\n cursor BIGINT NOT NULL,\n updated_at_ms BIGINT NOT NULL,\n last_round_at_ms BIGINT, last_error_code TEXT,\n consecutive_failures INTEGER NOT NULL DEFAULT 0,\n schema_version INTEGER, units JSONB, registration_digest BYTEA,\n catching_up JSONB NOT NULL DEFAULT '[]',\n PRIMARY KEY(partition, client_id)\n);\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS wire_version INTEGER NOT NULL DEFAULT 1;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS last_round_at_ms BIGINT;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS last_error_code TEXT;\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS consecutive_failures INTEGER NOT NULL DEFAULT 0;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS schema_version INTEGER;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS units JSONB;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS registration_digest BYTEA;\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS catching_up JSONB NOT NULL DEFAULT '[]';\n-- \u00A79: a database from before wire version 4 (its client records still hold\n-- subscriptions) starts the horizon at its last commit.\nUPDATE sync_partitions SET horizon_seq = max_commit_seq\n WHERE EXISTS (SELECT 1 FROM information_schema.columns\n WHERE table_schema = current_schema()\n AND table_name = 'sync_clients'\n AND column_name = 'subscriptions');\nALTER TABLE sync_clients DROP COLUMN IF EXISTS subscriptions;\nCREATE TABLE IF NOT EXISTS sync_blob_refs(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n blob_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, row_id, blob_id)\n);\nCREATE INDEX IF NOT EXISTS sync_blob_refs_by_blob\n ON sync_blob_refs(partition, blob_id);\nCREATE TABLE IF NOT EXISTS sync_tombstones(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n commit_seq BIGINT NOT NULL, server_version BIGINT, scopes JSONB,\n PRIMARY KEY(partition, tbl, row_id)\n);\nALTER TABLE sync_tombstones ADD COLUMN IF NOT EXISTS server_version BIGINT;\nALTER TABLE sync_tombstones ADD COLUMN IF NOT EXISTS scopes JSONB;\nALTER TABLE sync_partitions\n ADD COLUMN IF NOT EXISTS delete_floor BIGINT NOT NULL DEFAULT 0;\nUPDATE sync_partitions SET delete_floor = t.version\n FROM (SELECT partition, max(server_version) AS version\n FROM sync_tombstones GROUP BY partition) t\n WHERE t.partition = sync_partitions.partition\n AND t.version > sync_partitions.delete_floor;\nCREATE TABLE IF NOT EXISTS sync_backfill_checkpoints(\n partition TEXT NOT NULL, name TEXT NOT NULL,\n schema_version INTEGER NOT NULL,\n state TEXT NOT NULL CHECK(state IN ('declared','backfilling','activated')),\n watermark BIGINT NOT NULL DEFAULT 0,\n owner_epoch BIGINT NOT NULL DEFAULT 0,\n observed_rows BIGINT NOT NULL DEFAULT 0,\n updated_at_ms BIGINT NOT NULL,\n PRIMARY KEY(partition, name)\n);\nCREATE TABLE IF NOT EXISTS sync_writer_fence(\n partition TEXT PRIMARY KEY,\n required_writer_version INTEGER NOT NULL\n);\n";
77
+ export declare const POSTGRES_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq BIGINT NOT NULL DEFAULT 0,\n horizon_seq BIGINT NOT NULL DEFAULT 0,\n delete_floor BIGINT NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required BOOLEAN NOT NULL DEFAULT FALSE,\n last_authenticated_at_ms BIGINT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_table_horizons(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n commit_seq BIGINT NOT NULL,\n PRIMARY KEY(partition, tbl)\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\nCREATE INDEX IF NOT EXISTS sync_row_scopes_by_row\n ON sync_row_scopes(partition, tbl, row_id);\nCREATE TABLE IF NOT EXISTS sync_commits(\n partition TEXT NOT NULL, commit_seq BIGINT NOT NULL,\n client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,\n actor_id TEXT NOT NULL, created_at_ms BIGINT NOT NULL,\n writer_version BIGINT,\n PRIMARY KEY(partition, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_commits_by_time\n ON sync_commits(partition, created_at_ms);\nCREATE TABLE IF NOT EXISTS sync_changes(\n partition TEXT NOT NULL, commit_seq BIGINT NOT NULL, idx INTEGER NOT NULL,\n tbl TEXT NOT NULL, row_id TEXT NOT NULL, op SMALLINT NOT NULL,\n row_version BIGINT, scopes JSONB NOT NULL, payload BYTEA,\n PRIMARY KEY(partition, commit_seq, idx)\n);\nCREATE INDEX IF NOT EXISTS sync_changes_by_table\n ON sync_changes(partition, commit_seq, tbl, idx);\nCREATE TABLE IF NOT EXISTS sync_change_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, commit_seq BIGINT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_change_scopes_by_key\n ON sync_change_scopes(partition, var, value, commit_seq);\nCREATE TABLE IF NOT EXISTS sync_push_results(\n partition TEXT NOT NULL, client_id TEXT NOT NULL,\n client_commit_id TEXT NOT NULL, result JSONB NOT NULL,\n outbox_id TEXT, client_seq BIGINT,\n PRIMARY KEY(partition, client_id, client_commit_id)\n);\nALTER TABLE sync_push_results ADD COLUMN IF NOT EXISTS outbox_id TEXT;\nALTER TABLE sync_push_results ADD COLUMN IF NOT EXISTS client_seq BIGINT;\nCREATE TABLE IF NOT EXISTS sync_push_watermarks(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, outbox_id TEXT NOT NULL,\n through_seq BIGINT NOT NULL,\n PRIMARY KEY(partition, client_id, outbox_id)\n);\nCREATE TABLE IF NOT EXISTS sync_reactions(\n partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,\n type TEXT NOT NULL, version INTEGER NOT NULL, payload JSONB NOT NULL,\n source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,\n source_commit_seq BIGINT NOT NULL, created_at_ms BIGINT NOT NULL,\n available_at_ms BIGINT NOT NULL, status TEXT NOT NULL,\n attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,\n lease_owner TEXT, lease_expires_at_ms BIGINT, completed_at_ms BIGINT,\n last_failure JSONB,\n PRIMARY KEY(partition, idempotency_key),\n CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))\n);\nCREATE INDEX IF NOT EXISTS sync_reactions_due\n ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_lease\n ON sync_reactions(partition, status, lease_expires_at_ms);\nCREATE INDEX IF NOT EXISTS sync_reactions_completed\n ON sync_reactions(partition, status, completed_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_dead_letter\n ON sync_reactions(partition, status, available_at_ms, idempotency_key);\nCREATE TABLE IF NOT EXISTS sync_clients(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,\n wire_version INTEGER NOT NULL DEFAULT 1,\n cursor BIGINT NOT NULL,\n updated_at_ms BIGINT NOT NULL,\n last_round_at_ms BIGINT, last_error_code TEXT,\n consecutive_failures INTEGER NOT NULL DEFAULT 0,\n schema_version INTEGER, units JSONB, registration_digest BYTEA,\n catching_up JSONB NOT NULL DEFAULT '[]',\n PRIMARY KEY(partition, client_id)\n);\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS wire_version INTEGER NOT NULL DEFAULT 1;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS last_round_at_ms BIGINT;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS last_error_code TEXT;\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS consecutive_failures INTEGER NOT NULL DEFAULT 0;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS schema_version INTEGER;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS units JSONB;\nALTER TABLE sync_clients ADD COLUMN IF NOT EXISTS registration_digest BYTEA;\nALTER TABLE sync_clients\n ADD COLUMN IF NOT EXISTS catching_up JSONB NOT NULL DEFAULT '[]';\n-- \u00A79: a database from before wire version 4 (its client records still hold\n-- subscriptions) starts the horizon at its last commit.\nUPDATE sync_partitions SET horizon_seq = max_commit_seq\n WHERE EXISTS (SELECT 1 FROM information_schema.columns\n WHERE table_schema = current_schema()\n AND table_name = 'sync_clients'\n AND column_name = 'subscriptions');\nALTER TABLE sync_clients DROP COLUMN IF EXISTS subscriptions;\nCREATE TABLE IF NOT EXISTS sync_blob_refs(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n blob_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, row_id, blob_id)\n);\nCREATE INDEX IF NOT EXISTS sync_blob_refs_by_blob\n ON sync_blob_refs(partition, blob_id);\nCREATE TABLE IF NOT EXISTS sync_tombstones(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n commit_seq BIGINT NOT NULL, server_version BIGINT, scopes JSONB,\n PRIMARY KEY(partition, tbl, row_id)\n);\nALTER TABLE sync_tombstones ADD COLUMN IF NOT EXISTS server_version BIGINT;\nALTER TABLE sync_tombstones ADD COLUMN IF NOT EXISTS scopes JSONB;\nALTER TABLE sync_partitions\n ADD COLUMN IF NOT EXISTS delete_floor BIGINT NOT NULL DEFAULT 0;\nUPDATE sync_partitions SET delete_floor = t.version\n FROM (SELECT partition, max(server_version) AS version\n FROM sync_tombstones GROUP BY partition) t\n WHERE t.partition = sync_partitions.partition\n AND t.version > sync_partitions.delete_floor;\nCREATE TABLE IF NOT EXISTS sync_backfill_checkpoints(\n partition TEXT NOT NULL, name TEXT NOT NULL,\n schema_version INTEGER NOT NULL,\n state TEXT NOT NULL CHECK(state IN ('declared','backfilling','activated')),\n watermark BIGINT NOT NULL DEFAULT 0,\n owner_epoch BIGINT NOT NULL DEFAULT 0,\n observed_rows BIGINT NOT NULL DEFAULT 0,\n updated_at_ms BIGINT NOT NULL,\n PRIMARY KEY(partition, name)\n);\nCREATE TABLE IF NOT EXISTS sync_writer_fence(\n partition TEXT PRIMARY KEY,\n required_writer_version INTEGER NOT NULL\n);\n";
78
78
  export declare class PostgresServerStorage implements ServerStorage {
79
79
  #private;
80
80
  constructor(exec: PgExecutor);
@@ -105,6 +105,7 @@ export declare class PostgresServerStorage implements ServerStorage {
105
105
  queryAuthoritative(partition: string, query: AuthoritativeQueryRequest, checkpoints?: readonly CheckpointDeclaration[]): Promise<AuthoritativeQueryResult>;
106
106
  getPartitionLogEpoch(partition: string): Promise<string | undefined>;
107
107
  getHorizonSeq(partition: string): Promise<number>;
108
+ getHorizons(partition: string): Promise<PartitionHorizons>;
108
109
  setHorizonSeq(partition: string, seq: number): Promise<void>;
109
110
  pruneCommitsThrough(partition: string, query: CommitPruneQuery): Promise<CommitPruneResult>;
110
111
  getCommitSeqBefore(partition: string, createdBeforeMs: number): Promise<number>;
@@ -94,6 +94,11 @@ CREATE TABLE IF NOT EXISTS sync_partition_registry(
94
94
  epoch_required BOOLEAN NOT NULL DEFAULT FALSE,
95
95
  last_authenticated_at_ms BIGINT NOT NULL
96
96
  );
97
+ CREATE TABLE IF NOT EXISTS sync_table_horizons(
98
+ partition TEXT NOT NULL, tbl TEXT NOT NULL,
99
+ commit_seq BIGINT NOT NULL,
100
+ PRIMARY KEY(partition, tbl)
101
+ );
97
102
  CREATE TABLE IF NOT EXISTS sync_row_scopes(
98
103
  partition TEXT NOT NULL, tbl TEXT NOT NULL,
99
104
  var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,
@@ -1930,6 +1935,14 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
1930
1935
  const { rows } = await this.#exec.query('SELECT horizon_seq FROM sync_partitions WHERE partition=$1', [partition]);
1931
1936
  return rows[0] === undefined ? 0 : asNumber(rows[0].horizon_seq);
1932
1937
  }
1938
+ async getHorizons(partition) {
1939
+ const { rows } = await this.#exec.query(`SELECT coalesce((SELECT horizon_seq FROM sync_partitions WHERE partition=$1),0) AS seq,
1940
+ coalesce((SELECT jsonb_object_agg(tbl, commit_seq) FROM sync_table_horizons WHERE partition=$1),'{}')::text AS tables`, [partition]);
1941
+ return {
1942
+ seq: asNumber(rows[0]?.seq),
1943
+ tables: new Map(Object.entries(asJson(rows[0]?.tables ?? '{}'))),
1944
+ };
1945
+ }
1933
1946
  async setHorizonSeq(partition, seq) {
1934
1947
  await this.#exec.query(`INSERT INTO sync_partitions(partition, horizon_seq) VALUES ($1,$2)
1935
1948
  ON CONFLICT (partition) DO UPDATE SET horizon_seq=GREATEST(sync_partitions.horizon_seq,EXCLUDED.horizon_seq)`, [partition, seq]);
@@ -1956,8 +1969,11 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
1956
1969
  WHERE s.partition=$1 AND s.tbl=k.tbl AND s.var=k.key
1957
1970
  AND s.value=k.value AND s.commit_seq=k.commit_seq`, [partition, horizonSeq]);
1958
1971
  await client.query('DELETE FROM sync_changes WHERE partition=$1 AND commit_seq<=$2', [partition, horizonSeq]);
1959
- // §4.6: tombstones prune in the same pass as the commit log.
1960
- await client.query('DELETE FROM sync_tombstones WHERE partition=$1 AND commit_seq<=$2', [partition, horizonSeq]);
1972
+ // §4.6: tombstones and table horizons prune in the same pass as the
1973
+ // commit log.
1974
+ for (const table of ['sync_tombstones', 'sync_table_horizons']) {
1975
+ await client.query(`DELETE FROM ${table} WHERE partition=$1 AND commit_seq<=$2`, [partition, horizonSeq]);
1976
+ }
1961
1977
  return {
1962
1978
  previousHorizonSeq,
1963
1979
  horizonSeq,
package/dist/pull.d.ts CHANGED
@@ -7,7 +7,7 @@ import type { PullSegmentSummary } from './events.js';
7
7
  import { type CompiledSchema, type CompiledTable } from './schema.js';
8
8
  import { type ResolvedScopes } from './scopes.js';
9
9
  import type { SegmentRecord } from './segment-store.js';
10
- import type { StoredCommit, StoredRow } from './storage.js';
10
+ import type { PartitionHorizons, StoredCommit, StoredRow } from './storage.js';
11
11
  /** §4.2 accept bitmask. */
12
12
  export declare const ACCEPT_INLINE_ROWS: number;
13
13
  export declare const ACCEPT_EXTERNAL_ROWS: number;
@@ -99,7 +99,7 @@ export interface BootstrapPlan {
99
99
  * is awaited, so a storage that batches concurrent page reads (PostgreSQL,
100
100
  * D1) answers them in one statement per table.
101
101
  */
102
- export declare function planBootstrap(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, unit: UnitPlan, bootstrapState: string | undefined, maxSeq: number, horizonSeq: number, logEpoch: string, keptCursor?: number): BootstrapPlan;
102
+ export declare function planBootstrap(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, unit: UnitPlan, bootstrapState: string | undefined, maxSeq: number, horizon: PartitionHorizons, logEpoch: string, keptCursor?: number): BootstrapPlan;
103
103
  /**
104
104
  * The segments of one bootstrap section (§4.7): tables in bootstrap order,
105
105
  * each active pair's rows or image, up to `maxSnapshotPages` pages. Returns