@pgxsinkit/client 0.3.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE-APACHE-2.0 +202 -0
  3. package/NOTICE +81 -0
  4. package/README.md +10 -3
  5. package/dist/boot-report.d.ts +8 -8
  6. package/dist/circuits/envelope-to-change.d.ts +9 -5
  7. package/dist/circuits/envelope-to-change.d.ts.map +1 -1
  8. package/dist/circuits/group-sync.d.ts +2 -2
  9. package/dist/circuits/group-sync.d.ts.map +1 -1
  10. package/dist/circuits/stream-source.d.ts +6 -0
  11. package/dist/circuits/stream-source.d.ts.map +1 -1
  12. package/dist/circuits/sync-engine.d.ts +2 -2
  13. package/dist/circuits/sync-engine.d.ts.map +1 -1
  14. package/dist/event-lane.d.ts +32 -1
  15. package/dist/event-lane.d.ts.map +1 -1
  16. package/dist/export-data.d.ts +3 -3
  17. package/dist/export-dump.d.ts +10 -10
  18. package/dist/export-dump.d.ts.map +1 -1
  19. package/dist/export-store.d.ts +13 -13
  20. package/dist/index.d.ts +196 -100
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1044 -487
  23. package/dist/index.js.map +39 -36
  24. package/dist/local-store.d.ts +1 -1
  25. package/dist/local-store.d.ts.map +1 -1
  26. package/dist/local-tables.d.ts +58 -0
  27. package/dist/local-tables.d.ts.map +1 -1
  28. package/dist/mutation.d.ts +2 -2
  29. package/dist/mutation.d.ts.map +1 -1
  30. package/dist/mutations-api.d.ts +1 -1
  31. package/dist/mutations-api.d.ts.map +1 -1
  32. package/dist/opfs-effects.d.ts +30 -2
  33. package/dist/opfs-effects.d.ts.map +1 -1
  34. package/dist/schema.d.ts +12 -1
  35. package/dist/schema.d.ts.map +1 -1
  36. package/dist/store-boot.d.ts +30 -2
  37. package/dist/store-boot.d.ts.map +1 -1
  38. package/dist/store-build.d.ts +19 -0
  39. package/dist/store-build.d.ts.map +1 -0
  40. package/dist/store-engine.d.ts +22 -0
  41. package/dist/store-engine.d.ts.map +1 -0
  42. package/dist/store-meta.d.ts +29 -3
  43. package/dist/store-meta.d.ts.map +1 -1
  44. package/dist/store-ownership-lock.d.ts +33 -0
  45. package/dist/store-ownership-lock.d.ts.map +1 -0
  46. package/dist/store-path.d.ts +43 -31
  47. package/dist/store-path.d.ts.map +1 -1
  48. package/dist/sync/apply.d.ts +5 -5
  49. package/dist/sync/apply.d.ts.map +1 -1
  50. package/dist/sync/copy.d.ts +82 -6
  51. package/dist/sync/copy.d.ts.map +1 -1
  52. package/dist/sync/drizzle-executor.d.ts +4 -4
  53. package/dist/sync/drizzle-executor.d.ts.map +1 -1
  54. package/dist/sync/subscription-state.d.ts +5 -5
  55. package/dist/sync/subscription-state.d.ts.map +1 -1
  56. package/dist/testing.d.ts +1 -1
  57. package/dist/testing.js +2 -1
  58. package/dist/testing.js.map +4 -4
  59. package/dist/worker/attach-sync-client.d.ts +24 -13
  60. package/dist/worker/attach-sync-client.d.ts.map +1 -1
  61. package/dist/worker/define-sync-worker.d.ts +35 -18
  62. package/dist/worker/define-sync-worker.d.ts.map +1 -1
  63. package/dist/worker/live-query-manager.d.ts +3 -3
  64. package/dist/worker/live-query-manager.d.ts.map +1 -1
  65. package/dist/worker/live-query-params-guard.d.ts +5 -5
  66. package/dist/worker/live-query-params-guard.d.ts.map +1 -1
  67. package/dist/worker/protocol.d.ts +26 -7
  68. package/dist/worker/protocol.d.ts.map +1 -1
  69. package/package.json +11 -8
  70. package/skills/core/SKILL.md +80 -15
  71. package/skills/operating/SKILL.md +129 -129
  72. package/skills/operating/references/postgres-builds.md +48 -0
  73. package/src/boot-report.ts +12 -12
  74. package/src/circuits/envelope-to-change.ts +121 -12
  75. package/src/circuits/group-sync.ts +2 -2
  76. package/src/circuits/stream-source.ts +35 -9
  77. package/src/circuits/sync-engine.ts +6 -4
  78. package/src/event-lane.ts +132 -14
  79. package/src/export-data.ts +4 -4
  80. package/src/export-dump.ts +31 -36
  81. package/src/export-store.ts +20 -20
  82. package/src/index.ts +576 -340
  83. package/src/live-rows-sql.ts +1 -1
  84. package/src/local-store.ts +2 -2
  85. package/src/local-tables.ts +70 -6
  86. package/src/mutation.ts +9 -8
  87. package/src/mutations-api.ts +4 -4
  88. package/src/opfs-effects.ts +74 -10
  89. package/src/schema.ts +124 -18
  90. package/src/store-boot.ts +71 -7
  91. package/src/store-build.ts +37 -0
  92. package/src/store-engine.ts +94 -0
  93. package/src/store-lifecycle.ts +1 -1
  94. package/src/store-meta.ts +155 -4
  95. package/src/store-ownership-lock.ts +109 -0
  96. package/src/store-path.ts +59 -43
  97. package/src/sync/apply.ts +32 -46
  98. package/src/sync/copy.ts +108 -13
  99. package/src/sync/drizzle-executor.ts +18 -19
  100. package/src/sync/metadata-tables.ts +1 -1
  101. package/src/sync/subscription-state.ts +7 -7
  102. package/src/testing.ts +3 -3
  103. package/src/worker/attach-sync-client.ts +146 -60
  104. package/src/worker/define-sync-worker.ts +212 -83
  105. package/src/worker/engine-router.ts +1 -1
  106. package/src/worker/live-query-fingerprint.ts +4 -4
  107. package/src/worker/live-query-manager.ts +12 -12
  108. package/src/worker/live-query-params-guard.ts +16 -15
  109. package/src/worker/protocol.ts +38 -7
@@ -1,6 +1,6 @@
1
1
  // Canonical fingerprint for a live query (ADR-0040 decisions 2 & 3). Two identical live queries — same
2
- // POST-WRAP SQL, same bound params, same key mode — must map to ONE PGlite registration; anything that
3
- // changes what PGlite actually runs (or how the diff is keyed) must map to a DISTINCT one. The fingerprint
2
+ // POST-WRAP SQL, same bound params, same key mode — must map to ONE pgwasm registration; anything that
3
+ // changes what pgwasm actually runs (or how the diff is keyed) must map to a DISTINCT one. The fingerprint
4
4
  // covers EXECUTION-relevant inputs only: the materialized SQL, a TYPED encoding of the params, and the PK
5
5
  // columns (which pick `live.incrementalQuery` vs `live.query` and drive the diff key). `use` is deliberately
6
6
  // NOT an input — activation/hydration are per-subscriber pre-steps that never influence the registration
@@ -111,9 +111,9 @@ export function fingerprintLiveQuery(
111
111
  params: readonly unknown[],
112
112
  pkColumns?: readonly string[],
113
113
  ): LiveQueryFingerprint {
114
- // Single-column PK → PGlite `live.incrementalQuery`; composite/keyless → `live.query` + worker-side diff.
114
+ // Single-column PK → pgwasm `live.incrementalQuery`; composite/keyless → `live.query` + worker-side diff.
115
115
  // The mode AND the exact pk columns both matter: same SQL with a different pk column is a different
116
- // registration (different diff key), and incremental vs full are different PGlite machines.
116
+ // registration (different diff key), and incremental vs full are different pgwasm machines.
117
117
  const mode = pkColumns && pkColumns.length === 1 ? "inc" : "full";
118
118
  // ONE `JSON.stringify` over a fixed-shape object of type-tagged encoding STRUCTURES: the SQL is its own JSON
119
119
  // string element (cannot bleed into params), and JSON escaping makes the whole key unambiguous — no manual
@@ -1,4 +1,4 @@
1
- // The worker-owned live-query manager (ADR-0040). It owns the PGlite `live` registration lifecycle, the
1
+ // The worker-owned live-query manager (ADR-0040). It owns the pgwasm `live` registration lifecycle, the
2
2
  // single change listener + shared `LiveDiffState` per query, and the awaited-teardown bookkeeping (decision
3
3
  // 1). Slice 3 added DEDUPLICATION: identical live queries (same fingerprint — see live-query-fingerprint.ts)
4
4
  // share ONE registration and ONE diff computation, fanned out to every subscriber (decision 2). Slice 4 adds
@@ -6,7 +6,7 @@
6
6
  // effective keep-alive retains the registration for a grace period so a matching resubscribe reuses it
7
7
  // verbatim (no ~400 ms re-materialization); retention is bounded by explicit count/row budgets and defaults
8
8
  // to OFF (keep-alive 0 → tear down the instant the last subscriber leaves). The 0 default is deliberate:
9
- // a retained entry still pays a full SQL rerun + diff on every dependent write (PGlite live queries cannot
9
+ // a retained entry still pays a full SQL rerun + diff on every dependent write (pgwasm live queries cannot
10
10
  // be paused), so keeping one alive is only worthwhile for a genuinely hot, re-mounted query — the default
11
11
  // keeps worker memory bounded and never runs surprise standing SQL.
12
12
  //
@@ -17,7 +17,7 @@
17
17
  // are per-subscriber pre-/post-steps that never influence the shared registration or its rows; in particular
18
18
  // `use` NEVER reaches here, so differing `use` sets share one registration.
19
19
 
20
- import type { LiveNamespace, LiveQuery, LiveQueryResults } from "@electric-sql/pglite/live";
20
+ import type { LiveNamespace, LiveQuery, LiveQueryResults } from "@pgxsinkit/pgwasm/live";
21
21
 
22
22
  import { syncDebug } from "../debug";
23
23
  import { computeLiveDiff, type LiveDiffState, seedLiveDiffState } from "./live-diff";
@@ -76,7 +76,7 @@ export interface LiveQueryPolicy {
76
76
  * Default retention (ms) for a zero-subscriber entry, floored by each subscriber's own `keepAliveMs` hint.
77
77
  * Default 0 — the current route-scoped behaviour (tear a query down the moment its last consumer leaves).
78
78
  * The standing argument for 0: a retained zero-subscriber entry STILL pays a full SQL rerun + diff on every
79
- * dependent-table write — PGlite live queries cannot be paused, only torn down — so retention trades that
79
+ * dependent-table write — pgwasm live queries cannot be paused, only torn down — so retention trades that
80
80
  * standing cost for avoiding re-materialization, and is only worth it for a genuinely hot, re-mounted query.
81
81
  */
82
82
  defaultKeepAliveMs?: number;
@@ -142,7 +142,7 @@ export interface LiveQueryDiagnostics {
142
142
  lastUsedAt: number | null;
143
143
  /** How long (ms) the entry has been retained (now − lastUsedAt), or `null` when not retained. */
144
144
  retainedSinceMs: number | null;
145
- /** A teardown is in flight (the entry is unsubscribing from PGlite). */
145
+ /** A teardown is in flight (the entry is unsubscribing from pgwasm). */
146
146
  teardownPending: boolean;
147
147
  }
148
148
 
@@ -240,7 +240,7 @@ export function createLiveQueryManager(deps: LiveQueryManagerDeps): LiveQueryMan
240
240
  deps.clearTimer ?? ((handle: TimerHandle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
241
241
 
242
242
  // ADR-0040 decision 1 (originally define-sync-worker's Slice 1): a fire-and-forget live `unsubscribe()`
243
- // still in flight when PGlite closes wedges the process forever. Every teardown promise is retained here so
243
+ // still in flight when pgwasm closes wedges the process forever. Every teardown promise is retained here so
244
244
  // `dispose()` settles them ALL before the caller closes the engine; rejections are swallowed so an
245
245
  // interrupted teardown never becomes an unhandled rejection.
246
246
  const pendingTeardowns = new Set<Promise<void>>();
@@ -276,7 +276,7 @@ export function createLiveQueryManager(deps: LiveQueryManagerDeps): LiveQueryMan
276
276
  (entry as { setup: Promise<void> }).setup = (async () => {
277
277
  const setupStartedAt = now();
278
278
  const { materialSql, params, pkColumns } = spec;
279
- // Single-column PK → PGlite's incremental machinery (`live.incrementalQuery`); composite/keyless →
279
+ // Single-column PK → pgwasm's incremental machinery (`live.incrementalQuery`); composite/keyless →
280
280
  // `live.query` (full ordered result each fire) + worker-side diff. Identical selection to the pre-dedup
281
281
  // path; the fingerprint already encoded this choice as its `mode`.
282
282
  const registered =
@@ -298,8 +298,8 @@ export function createLiveQueryManager(deps: LiveQueryManagerDeps): LiveQueryMan
298
298
  }
299
299
  }
300
300
  // A retained (zero-subscriber) entry stays live and its rows can GROW past the row budget on a write.
301
- // Re-enforce OUTSIDE this callback (a queued microtask) — eviction's PGlite `unsubscribe()` must not
302
- // run inside PGlite's own notification call (ADR-0040 decision 4 — the budget stays authoritative).
301
+ // Re-enforce OUTSIDE this callback (a queued microtask) — eviction's pgwasm `unsubscribe()` must not
302
+ // run inside pgwasm's own notification call (ADR-0040 decision 4 — the budget stays authoritative).
303
303
  if (retained.has(entry)) queueMicrotask(() => enforceBudgets());
304
304
  };
305
305
  registered.subscribe(listener);
@@ -456,10 +456,10 @@ export function createLiveQueryManager(deps: LiveQueryManagerDeps): LiveQueryMan
456
456
  `[pgxsinkit] keepAliveMs must be <= ${MAX_KEEP_ALIVE_MS} ms (the platform setTimeout ceiling; got ${hint})`,
457
457
  );
458
458
  }
459
- // Reject a param shape that would trip PGlite bug #1055's broken live-query param inlining BEFORE any
459
+ // Reject a param shape that would trip pgwasm bug #1055's broken live-query param inlining BEFORE any
460
460
  // fingerprinting or registration (see live-query-params-guard.ts). Synchronous, so every subscriber —
461
461
  // in-process and worker alike — fails loudly rather than binding wrong rows or hitting a cryptic
462
- // `format()` error deep inside PGlite.
462
+ // `format()` error deep inside pgwasm.
463
463
  assertLiveQueryParamsSafe(spec.materialSql, spec.params);
464
464
  const keepAliveMs = hint ?? 0;
465
465
  const scope = opts?.scope;
@@ -512,7 +512,7 @@ export function createLiveQueryManager(deps: LiveQueryManagerDeps): LiveQueryMan
512
512
  // membership write. A listener fire (computeLiveDiff → deliverDiff) is synchronous, so with no yield here
513
513
  // no diff can land between the two: the joiner can neither miss a diff (it would be added to the map
514
514
  // first) nor be handed one its snapshot already reflects (the snapshot is taken first). The initial rows
515
- // come from the diff state, NOT a fresh PGlite read — the registration is already current.
515
+ // come from the diff state, NOT a fresh pgwasm read — the registration is already current.
516
516
  //
517
517
  // Keep-alive GENERATION rule (ADR-0040 decision 4): a 0→1 join opens a new generation and RESETS the
518
518
  // entry's generation hint to this joiner's; any later join `max`'s it. The last-out effective keep-alive
@@ -1,7 +1,7 @@
1
- // A defensive guard against PGlite upstream bug https://github.com/electric-sql/pglite/issues/1055 on the
2
- // live-query path. WHY this exists:
1
+ // A defensive guard against upstream bug https://github.com/electric-sql/pglite/issues/1055 on the
2
+ // live-query path, which the `live` extension carried until the pgwasm switch (ADR-0062). WHY this exists:
3
3
  //
4
- // PGlite 0.5.4's `live.query` / `live.incrementalQuery` inline bound params into a `CREATE TEMP VIEW`
4
+ // The inherited `live.query` / `live.incrementalQuery` inline bound params into a `CREATE TEMP VIEW`
5
5
  // (via `formatQuery`) WHENEVER `params.length > 0`. That inlining rewrites every `$N` placeholder with
6
6
  // `sql.replace(/\$([0-9]+)/g, (_, n) => "%" + n + "L")` — emitting `%NL`, which is Postgres `format()`'s
7
7
  // "pad to width N, consume the NEXT argument sequentially" directive, NOT the positional `%N$L` it means to
@@ -9,34 +9,35 @@
9
9
  // - out-of-order placeholders (`… $2 … $1 …`) silently bind params in TEXTUAL order → wrong rows;
10
10
  // - a repeated placeholder (`$1 … $1`) throws `too few arguments for format()`;
11
11
  // - a skipped placeholder silently mis-binds.
12
- // The ONLY input shape under which PGlite's broken inlining is still correct: the placeholders, read in
12
+ // The ONLY input shape under which that broken inlining is still correct: the placeholders, read in
13
13
  // textual order, are exactly `$1, $2, …, $n` (n = params.length) — each appearing exactly once, strictly
14
14
  // ascending. Drizzle-compiled SQL always satisfies this (its serializer numbers params in the same
15
15
  // left-to-right pass that emits them); hand-written raw SQL may not.
16
16
  //
17
17
  // So this guard fails LOUDLY at the client boundary for any other shape, turning a silent wrong-rows /
18
- // cryptic-`format()` failure into an actionable error naming the upstream bug. It can be DELETED once
19
- // transcrobes/pgxsinkit pin a PGlite release whose `formatQuery` emits the positional `%N$L`.
18
+ // cryptic-`format()` failure into an actionable error naming the upstream bug. pgwasm's own `formatQuery`
19
+ // emits the positional `%N$L` (tests/unit/pgwasm-live-format-query.test.ts), so the guard no longer
20
+ // protects pgwasm itself; whether to delete it is an open decision.
20
21
 
21
22
  /**
22
- * Throw if `sql` + `params` would hit PGlite bug #1055's broken param inlining (see the module header).
23
+ * Throw if `sql` + `params` would hit bug #1055's broken param inlining (see the module header).
23
24
  *
24
- * `params.length === 0` returns immediately: PGlite skips `formatQuery` entirely in that case, so any `$N`
25
+ * `params.length === 0` returns immediately: pgwasm skips `formatQuery` entirely in that case, so any `$N`
25
26
  * tokens are Postgres's own concern, not this guard's. Otherwise the SQL is scanned with the SAME regex
26
- * PGlite's rewrite uses (`/\$(\d+)/g`) — deliberately, so the guard sees EXACTLY the tokens PGlite would
27
- * touch. That includes a `$1` sitting inside a string literal: PGlite would corrupt that too, so a loud
28
- * false positive here is a feature, not a bug — it flags SQL PGlite cannot inline safely regardless.
27
+ * pgwasm's rewrite uses (`/\$(\d+)/g`) — deliberately, so the guard sees EXACTLY the tokens pgwasm would
28
+ * touch. That includes a `$1` sitting inside a string literal: pgwasm would corrupt that too, so a loud
29
+ * false positive here is a feature, not a bug — it flags SQL pgwasm cannot inline safely regardless.
29
30
  *
30
31
  * Safe iff the collected numbers (in textual order) are exactly `[1, 2, …, params.length]`. Anything else —
31
32
  * out-of-order, repeated, skipped, or a count mismatch in either direction — throws.
32
33
  */
33
34
  export function assertLiveQueryParamsSafe(sql: string, params: readonly unknown[]): void {
34
- // No params → PGlite never calls formatQuery, so its broken inlining never runs. Placeholders without
35
+ // No params → pgwasm never calls formatQuery, so its broken inlining never runs. Placeholders without
35
36
  // params are Postgres's problem to report, not this guard's.
36
37
  if (params.length === 0) return;
37
38
 
38
- // The SAME regex PGlite's `formatQuery` applies — so we see exactly the tokens its rewrite would touch,
39
- // including any `$N` inside a string literal (which PGlite would corrupt too; flagging it is intentional).
39
+ // The SAME regex pgwasm's `formatQuery` applies — so we see exactly the tokens its rewrite would touch,
40
+ // including any `$N` inside a string literal (which pgwasm would corrupt too; flagging it is intentional).
40
41
  const numbers: number[] = [];
41
42
  for (const match of sql.matchAll(/\$(\d+)/g)) numbers.push(Number(match[1]));
42
43
 
@@ -46,7 +47,7 @@ export function assertLiveQueryParamsSafe(sql: string, params: readonly unknown[
46
47
 
47
48
  const found = numbers.length === 0 ? "(none)" : numbers.map((n) => `$${n}`).join(", ");
48
49
  throw new Error(
49
- `[pgxsinkit] live query hits PGlite bug #1055 ` +
50
+ `[pgxsinkit] live query hits upstream bug #1055 ` +
50
51
  `(https://github.com/electric-sql/pglite/issues/1055): its live-query param inlining binds $N ` +
51
52
  `placeholders SEQUENTIALLY (in textual order), not positionally, so anything but a strictly ` +
52
53
  `ascending $1..$n each used once silently mis-binds or throws. Found placeholders [${found}] for ` +
@@ -106,7 +106,7 @@ export interface AuthTokenSnapshot {
106
106
 
107
107
  /**
108
108
  * tab → worker: pre-spawn the store WITHOUT booting the engine (ADR-0032 decision 5). Sent at the
109
- * login screen against the freshly-named spare worker: the worker only runs PGlite `create`/initdb
109
+ * login screen against the freshly-named spare worker: the worker only runs pgwasm `create`/initdb
110
110
  * (off every thread that matters) and holds the raw store idle — no schema, no shape streams, no token
111
111
  * — until the real {@link AttachPayload} arrives (with config + token) and adopts the provisioned store.
112
112
  * Role-agnostic on purpose: the spare is minted before the user (and role) is known, so provisioning
@@ -154,7 +154,7 @@ export interface ProvisionAckPayload {
154
154
  export interface RestoreArtefactWire {
155
155
  /** The backup tarball bytes — transferred, not copied. */
156
156
  buffer: ArrayBuffer;
157
- /** The backup file name (informational; PGlite's `loadDataDir` reads the bytes, not the name). */
157
+ /** The backup file name (informational; pgwasm's `loadDataDir` reads the bytes, not the name). */
158
158
  fileName: string;
159
159
  /** The backup MIME type (`application/x-gzip` / `application/x-tar`), used to rebuild the `Blob`. */
160
160
  mimeType: string;
@@ -252,6 +252,15 @@ export interface AttachAckPayload {
252
252
  */
253
253
  writeReadyError?: BridgeErrorWire;
254
254
  bootSettledError?: BridgeErrorWire;
255
+ /**
256
+ * The started-state snapshot at ack time (ADR-0059): `isSynced` per registry key, computed on the
257
+ * worker's OWN client. It rides the ack because `attachSyncClient` RESOLVES here — the worker's first
258
+ * `status` event is posted after the ack, so `const c = await attachSyncClient(…); c.isSynced(k)` runs
259
+ * before any broadcast is delivered, and only an ack field makes that first synchronous read faithful.
260
+ * A late attach folds the current snapshot the same way it folds the milestones above. Absent on an
261
+ * error ack (there is no engine to ask).
262
+ */
263
+ synced?: Record<string, boolean>;
255
264
  /**
256
265
  * Present when the engine boot REJECTED for this attach: the tab rejects `attachSyncClient` with this
257
266
  * message instead of hanging forever on the ack (ADR-0032 FIX 1). A later attach retries the boot. This
@@ -274,7 +283,13 @@ export interface TokenResponsePayload {
274
283
 
275
284
  /**
276
285
  * The RPC ops the attach facade proxies to the worker's booted client — the write API (mirrors
277
- * `client.mutate`/flush), mutation-state reads, and the one-shot raw inspection reads (`rawQuery`/`rawExec`).
286
+ * `client.mutate`/flush), mutation-state reads, and the raw inspection surface (the one-shot `rawQuery`/
287
+ * `rawExec`, plus the atomic `rawTransaction`).
288
+ *
289
+ * Most dispatches are pure structured clone. The exception is a raw statement's COPY body (ADR-0061): the
290
+ * tab lists each `blob`'s `ArrayBuffer` on the dispatch's transfer list, so the bytes cross ZERO-COPY and
291
+ * the tab's buffers are detached. No wire type changes for it — a `Uint8Array` survives structured clone
292
+ * as a `Uint8Array` over the transferred buffer, so the worker reads `RawStatement.blob` unchanged.
278
293
  */
279
294
  export type RpcOp =
280
295
  | "create"
@@ -304,13 +319,21 @@ export type RpcOp =
304
319
  | "ensureSynced"
305
320
  | "readMutationDetails"
306
321
  | "diagnostics"
322
+ // Carries `[sql, params?, options?]`. `options.blob` (ADR-0061) is the single-statement COPY bulk load:
323
+ // the COPY TEXT body of a `COPY … FROM '/dev/blob'`, TRANSFERRED (not cloned) with the dispatch.
307
324
  | "rawQuery"
308
325
  | "rawExec"
326
+ // The ATOMIC raw seam: carries `[statements, options?]` — the whole `RawStatement[]` in ONE round trip, so
327
+ // the transaction opens and closes inside the worker. It cannot be composed tab-side out of `rawQuery`
328
+ // calls (each would be its own implicit transaction, and a relocation between two of them would tear the
329
+ // write), which is exactly why it is its own op. Any statement may carry a COPY `blob` (ADR-0061); every
330
+ // such buffer rides this ONE dispatch's transfer list, so a chunked bulk load crosses zero-copy.
331
+ | "rawTransaction"
309
332
  // Guarded one-shot Drizzle read (ADR-0032 decision 4): the attach client's Drizzle-over-bridge compiles a
310
333
  // read to SQL on the tab and routes it here as the {@link GuardedQueryWireArgs} tuple
311
334
  // `[sql, params?, { rowMode? }, use?]`. Unlike `rawQuery` (raw inspection, no guard), the worker runs the
312
335
  // engine's `guardedRawQuery` — the ADR-0041 read gate + the ADR-0021 lazy-group guard (activating any lazy
313
- // relation the SQL/`use` reference) — then returns the full PGlite `Results` over `rpc-result`, so Drizzle's
336
+ // relation the SQL/`use` reference) — then returns the full pgwasm `Results` over `rpc-result`, so Drizzle's
314
337
  // own result mapping runs on the tab. Only `rowMode` crosses in the options: drizzle's `parsers` map is
315
338
  // FUNCTIONS (non-serializable) and is stripped tab-side, then re-applied worker-side so the identity-parsed
316
339
  // OIDs (drizzle's identity parsers — temporal OIDs + numeric[]) round-trip as raw strings exactly as in-process.
@@ -472,6 +495,13 @@ export type BridgeEvent =
472
495
  // the tab's matching `writeReady` / `bootSettled` promise so an awaiter fails loudly rather than hanging;
473
496
  // `localReadReady` (and the resolved attach) are unaffected — the engine reached local-read readiness.
474
497
  | { kind: "milestone-error"; stage: BootMilestone; error: BridgeErrorWire }
498
+ // The started-state snapshot (ADR-0059): what the WORKER's own client answers for `isSynced(key)`, one
499
+ // entry per registry key. `isSynced` is a SYNCHRONOUS peek, so it can never be an RPC; the worker pushes
500
+ // the answer instead and the tab reads it out of a cache. Computed by asking the in-process client, never
501
+ // re-derived tab-side — per-group catch-up readiness (`status`/`groupReady`) is the strictly weaker
502
+ // question and reads a promoted-but-still-catching-up group as not started. Broadcast whenever the answer
503
+ // changes (deduped against the last one sent); a tab attaching later folds {@link AttachAckPayload.synced}.
504
+ | { kind: "synced"; tables: Record<string, boolean> }
475
505
  | { kind: "conflict"; details: unknown }
476
506
  | { kind: "quarantine"; details: unknown }
477
507
  | { kind: "reject"; details: unknown }
@@ -556,9 +586,10 @@ export function isBridgeEnvelope(data: unknown): data is BridgeEnvelope {
556
586
 
557
587
  /**
558
588
  * Send a typed message over a port: encode → post (with any transferables). The one write choke point.
559
- * `transfer` lets a caller declare payload-specific transferables (e.g. a store-backup's `ArrayBuffer`,
560
- * ADR-0035) WITHOUT teaching the shared codec about that op — they are merged after any the codec itself
561
- * produces, so both the codec's future zero-copy path and per-message transfers coexist.
589
+ * `transfer` lets a caller declare payload-specific transferables (a store-backup's `ArrayBuffer`,
590
+ * ADR-0035; a raw statement's COPY body, ADR-0061) WITHOUT teaching the shared codec about that op — they
591
+ * are merged after any the codec itself produces, so both the codec's future zero-copy path and
592
+ * per-message transfers coexist. Every listed buffer is DETACHED in the sending realm once this returns.
562
593
  */
563
594
  export function postBridgeMessage(
564
595
  port: BridgePort,