@lunora/client 1.0.0-alpha.4 → 1.0.0-alpha.41

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 (85) hide show
  1. package/LICENSE.md +6 -0
  2. package/README.md +2 -0
  3. package/dist/auth/index.d.mts +11 -10
  4. package/dist/auth/index.d.ts +11 -10
  5. package/dist/auth/index.mjs +1 -60
  6. package/dist/index.d.mts +736 -172
  7. package/dist/index.d.ts +736 -172
  8. package/dist/index.mjs +1 -14
  9. package/dist/packem_shared/CONFLICT_ERROR_CODE-B2SU0CaJ.mjs +1 -0
  10. package/dist/packem_shared/ClientServiceWorker-CRQ5yl7-.mjs +1 -0
  11. package/dist/packem_shared/DEFAULT_MAX_BUFFER-TBn2s7Rz.mjs +1 -0
  12. package/dist/packem_shared/LunoraClient-CB5y0HME.mjs +1 -0
  13. package/dist/packem_shared/OfflineQueue-y4mymc5n.mjs +1 -0
  14. package/dist/packem_shared/RETIRE_AFTER_DURABLE_SEQ_ADVANCE-D96iRJDY.mjs +1 -0
  15. package/dist/packem_shared/SKIP-CArzT3Vl.mjs +1 -0
  16. package/dist/packem_shared/SubscriptionRegistry-C72rCp44.mjs +1 -0
  17. package/dist/packem_shared/TabCoordinator-CMFYQCL-.mjs +1 -0
  18. package/dist/packem_shared/anyApi-BD4dgvDY.mjs +1 -0
  19. package/dist/packem_shared/applyDelta-DXqQORbM.mjs +1 -0
  20. package/dist/packem_shared/createAsyncStoragePersistence-BRLqeXsC.mjs +1 -0
  21. package/dist/packem_shared/createClientQuery-CrdRoicO.mjs +1 -0
  22. package/dist/packem_shared/createInMemoryBookmarkStorage-DCd5Ej-t.mjs +1 -0
  23. package/dist/packem_shared/createInMemoryPersistence-CQYXy8UP.mjs +1 -0
  24. package/dist/packem_shared/createInMemoryQueryCache-DjCKrLjk.mjs +1 -0
  25. package/dist/packem_shared/createLocalStore-CwuKPK9x.mjs +1 -0
  26. package/dist/packem_shared/createMutationRunner-lQXH8Amk.mjs +1 -0
  27. package/dist/packem_shared/createMutatorRunner-BVzo0FBZ.mjs +1 -0
  28. package/dist/packem_shared/createReconnect-CFT7YRSR.mjs +1 -0
  29. package/dist/packem_shared/createReply-DF0jRsv-.mjs +1 -0
  30. package/dist/packem_shared/createServerClient-C1At_rrh.mjs +1 -0
  31. package/dist/packem_shared/createSnapshotPrecondition-CKvCNKyD.mjs +1 -0
  32. package/dist/packem_shared/deserializePreloaded-CHfOFpka.mjs +1 -0
  33. package/dist/packem_shared/getServerSession-CGjgRBAE.mjs +1 -0
  34. package/dist/packem_shared/httpStream-Cz4_QyjV.mjs +11 -0
  35. package/dist/packem_shared/idb-utility-C-DR8nG1.mjs +1 -0
  36. package/dist/packem_shared/local-store-DFMSEM2j.mjs +1 -0
  37. package/dist/packem_shared/lunora-client.d-D_WLwpn0.d.mts +2175 -0
  38. package/dist/packem_shared/lunora-client.d-cFPjoyDb.d.ts +2175 -0
  39. package/dist/packem_shared/offline-queue-BeWay5u5.mjs +1 -0
  40. package/dist/packem_shared/preload.d-BkbCVtZe.d.ts +21 -0
  41. package/dist/packem_shared/preload.d-DNxm6niZ.d.mts +21 -0
  42. package/dist/packem_shared/preloadQuery-DjwSjFwt.mjs +1 -0
  43. package/dist/packem_shared/types.d-CA7ZeJm9.d.mts +942 -0
  44. package/dist/packem_shared/types.d-CA7ZeJm9.d.ts +942 -0
  45. package/dist/packem_shared/wire-codec-D_fiax9d.mjs +1 -0
  46. package/dist/packem_shared/wire-key-j8BZ65HQ.mjs +1 -0
  47. package/dist/pagination/index.d.mts +42 -42
  48. package/dist/pagination/index.d.ts +42 -42
  49. package/dist/pagination/index.mjs +1 -61
  50. package/dist/query/index.d.mts +43 -42
  51. package/dist/query/index.d.ts +43 -42
  52. package/dist/query/index.mjs +1 -1
  53. package/dist/service.d.mts +49 -0
  54. package/dist/service.d.ts +49 -0
  55. package/dist/service.mjs +1 -0
  56. package/dist/ssr/index.d.mts +80 -79
  57. package/dist/ssr/index.d.ts +80 -79
  58. package/dist/ssr/index.mjs +1 -4
  59. package/dist/upload.d.mts +35 -0
  60. package/dist/upload.d.ts +35 -0
  61. package/dist/upload.mjs +1 -0
  62. package/package.json +13 -1
  63. package/dist/packem_shared/CONFLICT_ERROR_CODE-aUdVbEDw.mjs +0 -4
  64. package/dist/packem_shared/DEFAULT_MAX_BUFFER-BDkqO5PW.mjs +0 -107
  65. package/dist/packem_shared/LunoraClient-DHMV-94j.mjs +0 -2198
  66. package/dist/packem_shared/OfflineQueue-D5p_QgF_.mjs +0 -127
  67. package/dist/packem_shared/SKIP-vItZChkw.mjs +0 -50
  68. package/dist/packem_shared/SubscriptionRegistry-Dn-7k7eo.mjs +0 -1
  69. package/dist/packem_shared/applyDelta-4jFGTPA3.mjs +0 -61
  70. package/dist/packem_shared/createAsyncStoragePersistence-1Z5BZ8RC.mjs +0 -45
  71. package/dist/packem_shared/createInMemoryBookmarkStorage-BoN7a7TH.mjs +0 -11
  72. package/dist/packem_shared/createInMemoryPersistence-CW82inU5.mjs +0 -105
  73. package/dist/packem_shared/createInMemoryQueryCache-B1PQ9Twl.mjs +0 -138
  74. package/dist/packem_shared/createLocalStore-DSUfoLqY.mjs +0 -36
  75. package/dist/packem_shared/createMutationRunner-BqsavzvG.mjs +0 -21
  76. package/dist/packem_shared/createReconnect-Di_-oHH7.mjs +0 -22
  77. package/dist/packem_shared/createServerClient-DMh-2UDv.mjs +0 -11
  78. package/dist/packem_shared/deserializePreloaded-C0eJTY_W.mjs +0 -4
  79. package/dist/packem_shared/getServerSession-8jXewqxd.mjs +0 -13
  80. package/dist/packem_shared/lunora-client.d-CbnxmuqG.d.mts +0 -1623
  81. package/dist/packem_shared/lunora-client.d-CbnxmuqG.d.ts +0 -1623
  82. package/dist/packem_shared/preload.d-Bhq6DBSe.d.mts +0 -20
  83. package/dist/packem_shared/preload.d-C73DT6bL.d.ts +0 -20
  84. package/dist/packem_shared/preloadQuery-lobFkD2Z.mjs +0 -13
  85. package/dist/packem_shared/subscription-C1Jy7HiF.mjs +0 -55
package/dist/index.d.ts CHANGED
@@ -1,13 +1,104 @@
1
- import { c as PersistenceAdapter, B as BookmarkStorage, F as FunctionReference, A as ArgsOf, M as MutationCallOptions, R as ReturnOf, O as OfflineQueueOptions, Q as QueryCacheAdapter, d as ReconnectOptions } from "./packem_shared/lunora-client.d-CbnxmuqG.js";
2
- export { type C as CachedQuery, type e as ClientMessage, type f as ConnectionStatus, D as DEFAULT_MAX_BUFFER, type g as FunctionArgumentDescriptor, type h as FunctionDescriptor, type G as GlobalFacetResult, type i as GlobalFacetValue, type j as GlobalFilterClause, type k as GlobalTableInfo, type l as GlobalTablePage, L as LunoraClient, type m as LunoraClientOptions, type n as OptimisticLocalStore, type o as OptimisticUpdate, type p as PersistedMutation, type P as Preloaded, type q as RpcEnvelope, type r as RpcResponseBody, type s as ScheduleRecord, type t as SchedulerPoolStatus, type u as SchedulerStatus, type v as ServerMessage, type w as ShardTrafficEntry, type x as ShardTrafficResult, type y as StorageListPage, type z as StorageObject, type E as StreamHandle, type H as StreamIterable, type I as SubscriptionCallback, type S as SubscriptionError, type b as SubscriptionErrorCallback, J as SubscriptionRegistry, type K as SubscriptionState, type a as Unsubscribe, type U as User, type W as WorkflowInstanceAction, type N as WorkflowInstanceDetail, type T as WorkflowInstancePage, type V as WorkflowInstanceStatus, type X as WorkflowInstanceSummary, type Y as WorkflowStepDetail, Z as createLocalStore, _ as createStream } from "./packem_shared/lunora-client.d-CbnxmuqG.js";
3
- export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-C73DT6bL.js";
4
- export type { AuthCapabilities, AuthImpersonation, AuthPage, AuthSession, AuthUser, CronJobInfo, VectorIndexSummary, VectorQueryMatch } from '@lunora/runtime';
5
- /**
6
- * The slice of React Native's `AsyncStorage` (or any async key/value store
7
- * Expo `SecureStore`, a wrapped `localForage`, an in-memory map in tests) this
8
- * adapter needs. Matches `@react-native-async-storage/async-storage`'s core
9
- * surface, so you can pass the module straight in.
10
- */
1
+ import { o as PersistenceAdapter, B as BookmarkStorage, H as HttpStreamRef, m as HttpStreamArgsOf, n as HttpStreamChunkOf, F as FunctionReference, A as ArgsOf, R as ReturnOf, O as OfflineQueueOptions, Q as QueryCacheAdapter, p as ReconnectOptions } from "./packem_shared/types.d-CA7ZeJm9.js";
2
+ export type { C as CachedQuery, q as ClientMessage, r as ClientShapeSubscribeMessage, s as ClientShapeUnsubscribeMessage, t as FunctionArgumentDescriptor, h as FunctionDescriptor, l as GlobalFacetResult, u as GlobalFacetValue, j as GlobalFilterClause, G as GlobalTableInfo, k as GlobalTablePage, v as HttpStreamCallArgs, L as LunoraClientOptions, w as OutboxMutation, x as OutboxSink, y as PersistedMutation, P as Preloaded, z as RowOp, D as RpcEnvelope, E as RpcResponseBody, b as ScheduleRecord, I as SchedulerPoolStatus, c as SchedulerStatus, J as ServerMessage, K as ServerPokeEndMessage, M as ServerPokePartMessage, N as ServerPokeStartMessage, T as ShardTrafficEntry, S as ShardTrafficResult, i as StorageListPage, V as StorageObject, a as Unsubscribe, U as User, g as WorkflowInstanceAction, f as WorkflowInstanceDetail, e as WorkflowInstancePage, d as WorkflowInstanceStatus, X as WorkflowInstanceSummary, Y as WorkflowStepDetail, W as WsTokenProvider } from "./packem_shared/types.d-CA7ZeJm9.js";
3
+ import { C as ConnectionStatus, S as SubscriptionError, b as StreamIterable, M as MutationCallOptions, L as LunoraClient } from "./packem_shared/lunora-client.d-cFPjoyDb.js";
4
+ export { type B as BatchSlot, c as CONFLICT_ERROR_CODE, type d as ClientDebugShard, type e as ClientDebugSnapshot, type f as ClientDebugSubscription, type g as ClientQueryRef, D as DEFAULT_MAX_BUFFER, type h as LunoraClientError, type i as LunoraErrorCode, type j as MutationSettledEvent, type O as OptimisticLocalStore, type k as OptimisticUpdate, type l as StreamHandle, type m as SubscriptionCallback, type a as SubscriptionErrorCallback, n as SubscriptionRegistry, type o as SubscriptionState, type p as SyncWatermark, q as createClientQuery, r as createLocalStore, s as createStream, t as getErrorCode, u as getRetryAfterMs, v as isConflictError, w as isForbiddenError, x as isRateLimitedError, y as isUnauthorizedError } from "./packem_shared/lunora-client.d-cFPjoyDb.js";
5
+ export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-BkbCVtZe.js";
6
+ export type { AuthCapabilities, AuthConfigInfo, AuthImpersonation, AuthPage, AuthSession, AuthUser, AuthUserFieldSpec, CronJobInfo, KvKeyEntry, KvKeyListResult, KvNamespaceSummary, KvValueResult, PipelineLogColumnMap, PipelineLogCursor, PipelineLogField, PipelineLogPage, PipelineLogQuery, PipelineLogRow, VectorIndexSummary, VectorQueryMatch } from '@lunora/runtime';
7
+ declare const anyApi: Record<string, Record<string, unknown>>;
8
+ /**
9
+ * Optimistic-reconcile logic shared by every `@lunora/*` framework adapter's
10
+ * agent-chat surface (`@lunora/react` `useAgentChat`, plus the `@lunora/vue`,
11
+ * `@lunora/solid`, `@lunora/svelte`, and `@lunora/angular` counterparts). The
12
+ * adapters keep only their framework-specific state plumbing
13
+ * (`useState` / `.value` / `.set()` / a store) and delegate the pure merge decision
14
+ * here, so the heuristic lives — and is tested — in exactly one place.
15
+ */
16
+ /**
17
+ * The durable-message shape the reconcile reads: a structural subset of each
18
+ * adapter's `AgentChatMessage` (itself a client-safe mirror of `@lunora/agent`'s
19
+ * `AgentMessageRow`). Only `content`, `role`, and the globally-monotonic `seq` are
20
+ * consulted; adapters pass their fuller row type, which is assignable to this.
21
+ */
22
+ interface ReconcileDurableMessage {
23
+ content: string;
24
+ role: "assistant" | "system" | "tool" | "user";
25
+ seq: number;
26
+ }
27
+ /**
28
+ * A local optimistic user turn awaiting server acknowledgement. `id` is a
29
+ * client-generated handle the adapter uses to roll the row back on a failed send;
30
+ * the reconcile itself reads only `content` and `maxDurableSeqAtSend`.
31
+ */
32
+ interface OptimisticMessage {
33
+ content: string;
34
+ id: number;
35
+ /**
36
+ * The highest durable `seq` present when this row was sent. The reconcile
37
+ * retires the row when a durable `user` row with matching `content` lands at a
38
+ * STRICTLY GREATER `seq` (i.e. after the send) — window-independent because
39
+ * `seq` is globally monotonic, not a positional count. Also the age baseline for
40
+ * the {@link RETIRE_AFTER_DURABLE_SEQ_ADVANCE} fallback.
41
+ */
42
+ maxDurableSeqAtSend: number;
43
+ }
44
+ /**
45
+ * The highest `seq` across `messages`, or `-1` when empty. Used both to capture an
46
+ * optimistic row's `maxDurableSeqAtSend` at send time and to base the synthetic
47
+ * seqs of rendered optimistic rows above the highest real durable seq (not just
48
+ * `messages.length`, which under-counts when durable rows have gaps), so a
49
+ * placeholder seq never collides with a real one.
50
+ */
51
+ declare const maxSeq: (messages: ReadonlyArray<{
52
+ seq: number;
53
+ }>) => number;
54
+ /**
55
+ * Secondary, windowed fallback: retire a pending optimistic row once the durable
56
+ * history's highest `seq` has advanced at least this far past the value seen at
57
+ * send, even though no matching-content user row is currently visible to claim it.
58
+ * This covers the pathological "identical content already present at send time"
59
+ * case, where no user row with a STRICTLY GREATER `seq` than the send-time max will
60
+ * ever appear for the primary content match to consume (e.g. the acknowledging row
61
+ * was evicted by a bounded `limit` before reconcile could see it). `seq` is globally
62
+ * monotonic, so it keeps climbing even when the visible user-row COUNT does not.
63
+ *
64
+ * The `2` is a heuristic threshold, NOT an invariant about turn shape. It is
65
+ * tempting to read it as "one turn == a user row and an assistant row (+2)", but
66
+ * `@lunora/agent`'s tool-loop turns can persist MANY rows, and an ERRORED turn
67
+ * persists only the user row (+1). Those non-(+2) turns are retired by the PRIMARY
68
+ * seq-based content match below (which sees the user row land at a greater `seq`),
69
+ * never by this count-based fallback. The fully robust fix is a server-echoed
70
+ * correlation id on each persisted user row (deferred — see plan 188).
71
+ *
72
+ * KNOWN LIMITATION (inherent to a client-only heuristic): on an ownerless /
73
+ * `instanceId`-less thread a FOREIGN writer that advances `seq` by >= 2 between this
74
+ * row's send and its own acknowledgement can trip this fallback and retire the row a
75
+ * beat early. The correlation id closes that gap; until then it is the accepted
76
+ * residual edge.
77
+ */
78
+ declare const RETIRE_AFTER_DURABLE_SEQ_ADVANCE = 2;
79
+ /**
80
+ * Drop the optimistic user turns the durable history has now caught up on. Pure —
81
+ * it reads only the two arrays plus values captured on each pending row at send
82
+ * time (no clock, no state mutation), so it is safe to run in render.
83
+ *
84
+ * The primary retire condition: a durable `user` row with the same `content` exists
85
+ * whose `seq` is STRICTLY GREATER than the row's `maxDurableSeqAtSend` — i.e. a user
86
+ * row that landed AFTER the send. This is window-independent (it matches on monotonic
87
+ * `seq`, not a positional count), so it retires the normal turn AND an errored
88
+ * single-row (+1) turn even under a saturated, sliding `limit`. Each durable row is
89
+ * consumed at most once (the `consumed` set), so repeated identical prompts sent
90
+ * back-to-back each wait for their OWN durable row instead of collapsing onto one.
91
+ *
92
+ * Failing that, the {@link RETIRE_AFTER_DURABLE_SEQ_ADVANCE} windowed fallback fires
93
+ * (see its doc) for the "identical content already present" pathological case.
94
+ */
95
+ declare const reconcileOptimistic: (optimistic: ReadonlyArray<OptimisticMessage>, durable: ReadonlyArray<ReconcileDurableMessage>) => OptimisticMessage[];
96
+ /**
97
+ * The slice of React Native's `AsyncStorage` (or any async key/value store —
98
+ * Expo `SecureStore`, a wrapped `localForage`, an in-memory map in tests) this
99
+ * adapter needs. Matches `@react-native-async-storage/async-storage`'s core
100
+ * surface, so you can pass the module straight in.
101
+ */
11
102
  interface AsyncStorageLike {
12
103
  getItem: (key: string) => Promise<string | null>;
13
104
  removeItem: (key: string) => Promise<void>;
@@ -20,46 +111,259 @@ interface AsyncStoragePersistenceOptions {
20
111
  storage: AsyncStorageLike;
21
112
  }
22
113
  /**
23
- * Builds a {@link PersistenceAdapter} over an async key/value store — the React
24
- * Native / Expo counterpart to the IndexedDB adapter (`createIndexedDbPersistence`).
25
- * The whole FIFO mutation log is serialized to JSON under a single key (`key`),
26
- * so enqueue order is preserved and `load()` returns freshly-parsed records that
27
- * callers can't alias.
28
- *
29
- * AsyncStorage has no transactions, so every read-modify-write is funnelled
30
- * through a single promise chain — concurrent `append`/`remove` calls run one at
31
- * a time and can't clobber each other's writes.
32
- */
114
+ * Builds a {@link PersistenceAdapter} over an async key/value store — the React
115
+ * Native / Expo counterpart to the IndexedDB adapter (`createIndexedDbPersistence`).
116
+ * The whole FIFO mutation log is serialized to JSON under a single key (`key`),
117
+ * so enqueue order is preserved and `load()` returns freshly-parsed records that
118
+ * callers can't alias.
119
+ *
120
+ * AsyncStorage has no transactions, so every read-modify-write is funnelled
121
+ * through a single promise chain — concurrent `append`/`remove` calls run one at
122
+ * a time and can't clobber each other's writes.
123
+ */
33
124
  declare const createAsyncStoragePersistence: (options: AsyncStoragePersistenceOptions) => PersistenceAdapter;
34
125
  /** Default in-memory bookmark store. Survives the lifetime of the client. */
35
126
  declare const createInMemoryBookmarkStorage: () => BookmarkStorage;
127
+ interface TabCoordinatorOptions {
128
+ /**
129
+ * BroadcastChannel name. Defaults to `"lunora-bridge"`.
130
+ */
131
+ channelName?: string;
132
+ /**
133
+ * Interval (ms) between leader heartbeats. Defaults to 1000.
134
+ */
135
+ heartbeatInterval?: number;
136
+ /**
137
+ * Milliseconds without a heartbeat to consider the leader dead. Must be
138
+ * larger than `heartbeatInterval`. Defaults to 3000.
139
+ */
140
+ leaderTimeout?: number;
141
+ /**
142
+ * Called when this tab becomes the leader (should open WS connections).
143
+ */
144
+ onBecomeLeader?: () => void;
145
+ /**
146
+ * Called when the leader broadcasts its aggregate `ConnectionStatus`
147
+ * (see `LunoraClient.emitConnectionStatus`) — a follower owns no socket
148
+ * of its own, so this is its only truthful signal for a status indicator
149
+ * or the offline-queue gate. Sent on every leader-side status change and
150
+ * once more right after a new leader takes over, so a follower already
151
+ * mid-mirroring isn't stuck on a stale value from the PREVIOUS leader.
152
+ *
153
+ * `identity` is the leader's own `identityFingerprint()` (`null` = signed
154
+ * out) — the channel-name scoping (see `LunoraClient.createTabCoordinator`)
155
+ * is the primary defence against a stale frame from a since-changed
156
+ * identity, but `setAuthToken` in one tab can move it to a new channel
157
+ * while this frame is already queued in another tab's message task queue.
158
+ * A follower drops the frame when `identity` is present and doesn't match
159
+ * its own; an **absent** `identity` (mixed-version leader) is accepted —
160
+ * today's behavior, and the channel split already separates version groups.
161
+ */
162
+ onConnectionStatus?: (status: ConnectionStatus, identity?: string | null) => void;
163
+ /**
164
+ * Called when this tab loses leadership (should close WS connections).
165
+ */
166
+ onStopBeingLeader?: () => void;
167
+ /**
168
+ * Called when the leader broadcasts subscription data. `cursor`/`epoch`
169
+ * ride along when the leader is CLIENT-01-aware, so the follower can drop
170
+ * its own confirmed optimistic layers instead of just displaying the raw
171
+ * value; both are absent on a mixed-version leader that hasn't shipped the
172
+ * cursor yet (the follower falls back to its historical behavior — see
173
+ * `lunora-client.ts`'s `onSubscriptionData` wiring).
174
+ *
175
+ * `identity` is the leader's `identityFingerprint()` (see
176
+ * `onConnectionStatus`'s docblock for the full rationale and the
177
+ * absent-field mixed-version rule — identical here).
178
+ */
179
+ onSubscriptionData?: (key: string, data: unknown, cursor?: number, epoch?: string, identity?: string | null) => void;
180
+ /**
181
+ * Called when the leader broadcasts a subscription error.
182
+ */
183
+ onSubscriptionError?: (key: string, error: SubscriptionError) => void;
184
+ /**
185
+ * Called when the leader broadcasts a `settled` frame's checkpoint advance
186
+ * (no value change, but the resume cursor moved). A follower needs this
187
+ * too — otherwise a `setQuery`/per-call optimistic overlay that a
188
+ * byte-identical write just confirmed stays masked on follower tabs until
189
+ * the next VISIBLE data frame arrives, even though the leader already
190
+ * dropped it.
191
+ *
192
+ * `lastMutationId` is the LEADER's own per-client `__client_watermark` —
193
+ * scoped server-side to the socket's announced `clientId` — so it only
194
+ * means anything to a follower whose `clientId` matches the leader's
195
+ * (`clientId` rides along for exactly that comparison; see
196
+ * `LunoraClient`'s wiring). An **absent** `clientId` (a mixed-version
197
+ * leader that hasn't shipped this field yet) also skips the
198
+ * `mutationId` half — safe, because the follower's own gates still
199
+ * resolve via its own RPC-ack watermark path and the `CheckpointRegistry`
200
+ * bounded fallback. The checkpoint (cursor) half of this callback fires
201
+ * unconditionally regardless of `clientId` — only the `mutationId` half
202
+ * is scoped.
203
+ *
204
+ * `identity` is the leader's `identityFingerprint()` — see
205
+ * `onConnectionStatus`'s docblock for the drop rule (identical here).
206
+ */
207
+ onSubscriptionSettled?: (key: string, cursor?: number, epoch?: string, lastMutationId?: number, clientId?: string, identity?: string | null) => void;
208
+ }
209
+ declare class TabCoordinator {
210
+ private readonly bc;
211
+ private readonly tabId;
212
+ private readonly heartbeatInterval;
213
+ private readonly leaderTimeout;
214
+ /** The tab id of the current known leader, or `undefined` if no leader. */
215
+ private knownLeader;
216
+ /** `true` when this tab believes it is the leader. */
217
+ private leader;
218
+ /** `true` once `start()` has been called. */
219
+ private running;
220
+ /**
221
+ * `true` while a `claimAndPromote()` claim window is open. Guards the yield
222
+ * handler and `checkLeaderHealth`'s belt-and-braces else-branch so the two
223
+ * promotion paths can't both arm a `becomeLeader` timeout for the same gap.
224
+ */
225
+ private promotionPending;
226
+ /** Timestamp of the most recent leader heartbeat. */
227
+ private lastHeartbeat;
228
+ private heartbeatTimer;
229
+ private leaderCheckTimer;
230
+ /** Callbacks set via constructor options. */
231
+ private readonly onBecomeLeader;
232
+ private readonly onStopBeingLeader;
233
+ private readonly onConnectionStatus;
234
+ private readonly onSubscriptionData;
235
+ private readonly onSubscriptionError;
236
+ private readonly onSubscriptionSettled;
237
+ constructor(options?: TabCoordinatorOptions);
238
+ /**
239
+ * Start the coordinator: attempt to claim leadership and begin the
240
+ * heartbeat/leader-check cycle. Safe to call multiple times.
241
+ */
242
+ start(): void;
243
+ /**
244
+ * Stop the coordinator: yield leadership (if held), close the channel, and
245
+ * clear all timers. Safe to call multiple times.
246
+ */
247
+ stop(): void;
248
+ /** `true` when this tab is the current WebSocket leader. */
249
+ isLeader(): boolean;
250
+ /**
251
+ * Force this (freshly `start()`-ed) tab to become leader immediately,
252
+ * skipping the normal claim-then-`leaderTimeout` dance. Safe to call
253
+ * right after `start()` when the caller already knows this tab was the
254
+ * SOLE leader of the group it's replacing (e.g. `LunoraClient`'s
255
+ * identity-change coordinator restart — waiting out the full
256
+ * `leaderTimeout` there would freeze every live query for no reason,
257
+ * since this tab is overwhelmingly likely to be alone on the freshly
258
+ * derived channel). A sibling tab going through the normal `start()`
259
+ * dance for the SAME transition observes this tab's `becomeLeader`
260
+ * heartbeat and defers before its own claim-timeout fires; if another
261
+ * tab ALSO force-promotes at the same moment (e.g. two tabs both
262
+ * transitioning to the same new identity), the existing
263
+ * `resolveLeaderVsLeaderTieBreak` resolves the rare double-promotion
264
+ * the same way it resolves any other split-brain.
265
+ */
266
+ promoteImmediately(): void;
267
+ /** The tab id of the current leader, or `undefined` if unknown / no leader. */
268
+ get leaderTabId(): string | undefined;
269
+ /** The id of this tab. */
270
+ get id(): string;
271
+ /** `true` when the coordinator has been started and is not yet stopped. */
272
+ get isRunning(): boolean;
273
+ /**
274
+ * Broadcast subscription data to all follower tabs. Only the leader should
275
+ * call this. `cursor`/`epoch` are omitted from the wire frame when
276
+ * `undefined` (e.g. a CDC-off shard) — a follower on ANY version treats a
277
+ * missing `cursor` as "no confirmed-layer drop this frame", so omitting it
278
+ * here is equivalent to sending it as `undefined`. `identity` is this
279
+ * (leader) tab's own `identityFingerprint()` (`null` = signed out) —
280
+ * stamped so a follower can drop a frame from a since-changed identity
281
+ * (see the `onSubscriptionData` docblock); spread-included whenever
282
+ * supplied, since a fixed leader always knows its own identity (never
283
+ * omits it) and `null` must round-trip distinctly from "field absent".
284
+ */
285
+ broadcastSubscriptionData(key: string, data: unknown, cursor?: number, epoch?: string, identity?: string | null): void;
286
+ /**
287
+ * Broadcast a subscription error to all follower tabs. Only the leader
288
+ * should call this.
289
+ */
290
+ broadcastSubscriptionError(key: string, error: SubscriptionError): void;
291
+ /**
292
+ * Broadcast a `settled` frame's checkpoint advance to all follower tabs
293
+ * (no value change, but the resume cursor/epoch moved — see
294
+ * `LunoraClient.handleSettledMessage`). `clientId` is this (leader) tab's
295
+ * own client id, stamped so a follower can tell whether the echoed
296
+ * `lastMutationId` watermark is genuinely its own (see the
297
+ * `onSubscriptionSettled` docblock). `identity` is this tab's own
298
+ * `identityFingerprint()` — see `broadcastSubscriptionData`'s docblock for
299
+ * the same round-tripping rule. Only the leader should call this.
300
+ */
301
+ broadcastSubscriptionSettled(key: string, cursor?: number, epoch?: string, lastMutationId?: number, clientId?: string, identity?: string | null): void;
302
+ /**
303
+ * Broadcast this tab's aggregate `ConnectionStatus` to follower tabs, so
304
+ * they can mirror a truthful status without a socket of their own (see
305
+ * `LunoraClient.computeStatus`/`emitConnectionStatus`). `identity` is this
306
+ * tab's own `identityFingerprint()` — see `broadcastSubscriptionData`'s
307
+ * docblock for the same round-tripping rule. Only the leader should call
308
+ * this.
309
+ */
310
+ broadcastConnectionStatus(status: ConnectionStatus, identity?: string | null): void;
311
+ private broadcast;
312
+ private handleMessage;
313
+ private handleClaimLeadership;
314
+ private handleHeartbeat;
315
+ /**
316
+ * Resolve two leaders existing at once — e.g. this tab was backgrounded
317
+ * and its heartbeat/health timers were throttled while a foreground
318
+ * follower's were not, so the follower's `checkLeaderHealth` timed out
319
+ * the (still-alive) leader and self-promoted. `BroadcastChannel` message
320
+ * delivery isn't subject to the same timer-throttling clamp, so even a
321
+ * backgrounded leader eventually observes the pretender's heartbeat here
322
+ * — resolve the split-brain deterministically with the same
323
+ * lexicographically-smaller-tabId rule used at claim-adoption. If the
324
+ * other tab wins, step down; if we win, reassert immediately so the
325
+ * pretender demotes itself the moment it processes our heartbeat.
326
+ */
327
+ private resolveLeaderVsLeaderTieBreak;
328
+ private becomeLeader;
329
+ private sendHeartbeat;
330
+ private checkLeaderHealth;
331
+ /**
332
+ * Broadcast a leadership claim and arm a `becomeLeader` fallback: if no
333
+ * other tab has asserted itself as leader by the time the window elapses,
334
+ * self-promote. Shared by `checkLeaderHealth`'s stale-leader path and the
335
+ * `yield-leadership` handler so both promotion triggers use one claim
336
+ * window instead of each arming its own timeout.
337
+ */
338
+ private claimAndPromote;
339
+ }
36
340
  /**
37
- * Client-side incremental merging of structured mutation deltas.
38
- *
39
- * Lunora's live-query fan-out has two server paths:
40
- *
41
- * 1. Server re-execution (subscriptions carrying a `functionPath`) pushes a
42
- * full `data` snapshot whenever a write touches a table the query reads. These
43
- * already carry the authoritative result and are applied wholesale.
44
- * 2. Legacy delta fan-out (`broadcastDelta`) pushes a structured `MutationDelta`
45
- * as a `delta` frame to subscribers matched by table + args. The delta describes
46
- * a single row change (`insert` / `update` / `delete`) keyed by row id, so the
47
- * client can splice it into the cached list result without a full re-send.
48
- *
49
- * Historically the client treated the `delta` field as an opaque blob and
50
- * replaced the whole cached value with it on every message — which only made
51
- * sense for the rare delta payloads that already carried the full result. This
52
- * module lets the client recognise a structured delta and merge it into the
53
- * existing array (preserving order, no dup/loss), falling back to full
54
- * replacement when the payload isn't a recognisable row delta or can't be
55
- * applied cleanly against the current cached shape.
56
- */
57
- /**
58
- * One row change as emitted by `@lunora/do`'s `broadcastDelta`. Mirrors
59
- * `MutationDelta` in `@lunora/do` structurally so the client carries no
60
- * dependency on it. `row` is absent on `delete` events (and may be absent on
61
- * older servers for any op).
62
- */
341
+ * Client-side incremental merging of structured mutation deltas.
342
+ *
343
+ * Lunora's live-query fan-out has two server paths:
344
+ *
345
+ * 1. Server re-execution (subscriptions carrying a `functionPath`) pushes a
346
+ * full `data` snapshot whenever a write touches a table the query reads. These
347
+ * already carry the authoritative result and are applied wholesale.
348
+ * 2. Legacy delta fan-out (`broadcastDelta`) pushes a structured `MutationDelta`
349
+ * as a `delta` frame to subscribers matched by table + args. The delta describes
350
+ * a single row change (`insert` / `update` / `delete`) keyed by row id, so the
351
+ * client can splice it into the cached list result without a full re-send.
352
+ *
353
+ * Historically the client treated the `delta` field as an opaque blob and
354
+ * replaced the whole cached value with it on every message — which only made
355
+ * sense for the rare delta payloads that already carried the full result. This
356
+ * module lets the client recognise a structured delta and merge it into the
357
+ * existing array (preserving order, no dup/loss), falling back to full
358
+ * replacement when the payload isn't a recognisable row delta or can't be
359
+ * applied cleanly against the current cached shape.
360
+ */
361
+ /**
362
+ * One row change as emitted by `@lunora/do`'s `broadcastDelta`. Mirrors
363
+ * `MutationDelta` in `@lunora/do` structurally so the client carries no
364
+ * dependency on it. `row` is absent on `delete` events (and may be absent on
365
+ * older servers for any op).
366
+ */
63
367
  interface MutationDelta {
64
368
  /** Row id (`_id`) the change applies to. */
65
369
  key: string;
@@ -68,54 +372,80 @@ interface MutationDelta {
68
372
  table: string;
69
373
  }
70
374
  /**
71
- * Structural guard: is `value` a `MutationDelta` the client knows how to merge?
72
- * We require `op`, `table`, and a string `key` so opaque payloads that merely
73
- * happen to be objects (e.g. an aggregate `{ count: 1 }` a query returns
74
- * verbatim) are never mistaken for a row delta and keep replacing the cached
75
- * value wholesale.
76
- */
375
+ * Structural guard: is `value` a `MutationDelta` the client knows how to merge?
376
+ * We require `op`, `table`, and a string `key` so opaque payloads that merely
377
+ * happen to be objects (e.g. an aggregate `{ count: 1 }` a query returns
378
+ * verbatim) are never mistaken for a row delta and keep replacing the cached
379
+ * value wholesale.
380
+ */
77
381
  declare const isMutationDelta: (value: unknown) => value is MutationDelta;
78
382
  /**
79
- * Apply a structured `MutationDelta` to a cached array result, returning a new
80
- * array (never mutating the input). Returns `undefined` when the delta can't be
81
- * applied cleanly — the caller should then fall back to the existing
82
- * full-replacement behaviour (or trust the next snapshot to reconcile).
83
- *
84
- * Mergeable shape: a plain array of id-bearing row objects, e.g. the result of
85
- * `db.query().collect()`.
86
- *
87
- * Insert / update / delete are matched by row `_id`:
88
- * - `insert`: appended (or placed by `_creationTime` order) if absent; treated
89
- * as an update if a row with the same id already exists (idempotent — guards
90
- * against a delta replayed after a snapshot already included it).
91
- * - `update`: replaces the matching row in place, preserving its position.
92
- * - `delete`: removes the matching row.
93
- *
94
- * Returns `undefined` when `current` isn't an array of id-keyable objects, or
95
- * when an `insert`/`update` delta carries no `row` to splice in.
96
- */
383
+ * Apply a structured `MutationDelta` to a cached array result, returning a new
384
+ * array (never mutating the input). Returns `undefined` when the delta can't be
385
+ * applied cleanly — the caller should then fall back to the existing
386
+ * full-replacement behaviour (or trust the next snapshot to reconcile).
387
+ *
388
+ * Mergeable shape: a plain array of id-bearing row objects, e.g. the result of
389
+ * `db.query().collect()`.
390
+ *
391
+ * Insert / update / delete are matched by row `_id`:
392
+ * - `insert`: appended (or placed by `_creationTime` order) if absent; treated
393
+ * as an update if a row with the same id already exists (idempotent — guards
394
+ * against a delta replayed after a snapshot already included it).
395
+ * - `update`: replaces the matching row in place, preserving its position.
396
+ * - `delete`: removes the matching row.
397
+ *
398
+ * Returns `undefined` when `current` isn't an array of id-keyable objects, or
399
+ * when an `insert`/`update` delta carries no `row` to splice in.
400
+ */
97
401
  declare const applyDelta: (current: unknown, delta: MutationDelta) => undefined | unknown[];
98
- /** Error code the server uses for optimistic-concurrency conflicts (HTTP 409). */
99
- declare const CONFLICT_ERROR_CODE = "CONFLICT";
100
- /**
101
- * Whether an unknown rejection is an optimistic-concurrency conflict — the
102
- * server lost a write race and the caller should refetch and retry (or surface
103
- * the conflict). Structural check on the `code` property the client attaches
104
- * when decoding the worker's `{ error: { code, message } }` envelope.
105
- */
106
- declare const isConflictError: (error: unknown) => error is Error & {
107
- code: "CONFLICT";
108
- };
402
+ /**
403
+ * Options accepted by {@link httpStream}.
404
+ * @experimental Part of the HTTP-SSE stream surface.
405
+ */
406
+ interface HttpStreamOptions {
407
+ /**
408
+ * Origin (or origin + prefix) the route path is appended to, e.g.
409
+ * `https://my-app.example.com`. Defaults to `""` — a relative URL, which
410
+ * resolves against the page origin in a browser.
411
+ */
412
+ baseUrl?: string;
413
+ /** `fetch` implementation override; defaults to the global `fetch`. */
414
+ fetch?: typeof fetch;
415
+ /** Extra request headers (e.g. `authorization`). `accept: text/event-stream` is always sent. */
416
+ headers?: Record<string, string>;
417
+ /** Caps the in-flight chunk buffer (see `createStream`); exceeding it fails the stream. */
418
+ maxBuffer?: number;
419
+ /** External abort signal — aborting it cancels the fetch (→ the server handler's `signal`). */
420
+ signal?: AbortSignal;
421
+ }
422
+ /**
423
+ * Open a typed HTTP-SSE stream route and iterate its chunks:
424
+ *
425
+ * ```ts
426
+ * const stream = httpStream(httpStreams.http.tokens, { searchParams: { prompt } }, { baseUrl });
427
+ * for await (const token of stream) {
428
+ * render(token); // typed as the route handler's yielded chunk
429
+ * }
430
+ * ```
431
+ *
432
+ * The returned iterable terminates when the server writes `event: complete`;
433
+ * an `event: error` frame (or a transport failure) surfaces as a coded
434
+ * rejection on the next `next()`. `.cancel()` — or aborting `options.signal` —
435
+ * aborts the underlying fetch, which the server observes via `request.signal`.
436
+ * @experimental Reconnect/POST-body/wire-fidelity design questions are still open, so the shape may change.
437
+ */
438
+ declare const httpStream: <Ref extends HttpStreamRef>(route: Ref, args?: HttpStreamArgsOf<Ref>, options?: HttpStreamOptions) => StreamIterable<HttpStreamChunkOf<Ref>>;
109
439
  /** The single transport method a mutation runner needs — narrowed so adapters can test against a stub. */
110
440
  interface MutationCapableClient<F extends FunctionReference> {
111
441
  mutation: (function_: F, args: ArgsOf<F>, options?: MutationCallOptions<unknown, unknown, ArgsOf<F>>) => Promise<ReturnOf<F>>;
112
442
  }
113
443
  /**
114
- * Reactive sinks an adapter binds to its own primitive's setters (a Solid
115
- * signal, a Vue ref, a Svelte store). The runner pushes into them; how they
116
- * store the value is the adapter's concern (e.g. Solid wraps function-valued
117
- * results in a thunk).
118
- */
444
+ * Reactive sinks an adapter binds to its own primitive's setters (a Solid
445
+ * signal, a Vue ref, a Svelte store). The runner pushes into them; how they
446
+ * store the value is the adapter's concern (e.g. Solid wraps function-valued
447
+ * results in a thunk).
448
+ */
119
449
  interface MutationRunnerSinks<R> {
120
450
  /** Receives the normalized {@link Error} when an invocation rejects. */
121
451
  setError: (error: Error) => void;
@@ -125,27 +455,102 @@ interface MutationRunnerSinks<R> {
125
455
  setResult: (result: R) => void;
126
456
  }
127
457
  /**
128
- * Build the framework-neutral `mutate` half of an adapter's mutation hook.
129
- *
130
- * Owns the orchestration every adapter otherwise copy-pastes: ref-counts
131
- * overlapping invocations into `setPending` (so it only clears once the last
132
- * settles), normalizes a thrown non-`Error`, and routes success/failure to
133
- * `setResult`/`setError` before re-throwing. Each adapter (`@lunora/react`,
134
- * `/solid`, `/svelte`, `/vue`) binds the three sinks to its own reactive
135
- * setters, so this logic lives in exactly one place. Optimistic-update options
136
- * pass straight through to `client.mutation`.
137
- */
458
+ * Build the framework-neutral `mutate` half of an adapter's mutation hook.
459
+ *
460
+ * Owns the orchestration every adapter otherwise copy-pastes: ref-counts
461
+ * overlapping invocations into `setPending` (so it only clears once the last
462
+ * settles), normalizes a thrown non-`Error`, and routes success/failure to
463
+ * `setResult`/`setError` before re-throwing. Each adapter (`@lunora/react`,
464
+ * `/solid`, `/svelte`, `/vue`) binds the three sinks to its own reactive
465
+ * setters, so this logic lives in exactly one place. Optimistic-update options
466
+ * pass straight through to `client.mutation`.
467
+ */
138
468
  declare const createMutationRunner: <F extends FunctionReference>(client: MutationCapableClient<F>, function_: F, sinks: MutationRunnerSinks<ReturnOf<F>>) => ((args: ArgsOf<F>, options?: MutationCallOptions<unknown, unknown, ArgsOf<F>>) => Promise<ReturnOf<F>>);
469
+ /**
470
+ * The structural surface of a TanStack `Transaction` a bound custom mutator
471
+ * returns — its `isPersisted.promise` resolves once the write is persisted and
472
+ * rejects on failure. Typed structurally so the framework adapters need not
473
+ * depend on `@tanstack/db` or `@lunora/db` (the handle is created app-side by
474
+ * `bindMutators`).
475
+ */
476
+ interface MutatorTransaction {
477
+ isPersisted: {
478
+ promise: Promise<unknown>;
479
+ };
480
+ }
481
+ /**
482
+ * A bound custom-mutator handle produced by `bindMutators(client, ctx, mutators)`
483
+ * in `@lunora/db`. Calling it applies the optimistic overlay to the local
484
+ * collections and pushes the authoritative server write; it returns the TanStack
485
+ * transaction whose `isPersisted` promise tracks completion.
486
+ */
487
+ type MutatorHandle<TArgs> = (args: TArgs) => MutatorTransaction;
488
+ /**
489
+ * Reactive sinks an adapter binds to its own primitive's setters (a React
490
+ * `useState`, a Solid signal, a Vue ref, a Svelte store). The runner pushes into
491
+ * them; how they store the value is the adapter's concern.
492
+ */
493
+ interface MutatorRunnerSinks {
494
+ /** Receives the normalized {@link Error} when an invocation rejects, or `undefined` on success / reset. */
495
+ setError: (error: Error | undefined) => void;
496
+ /** Receives `true` while at least one invocation is in flight (ref-counted across overlapping calls), else `false`. */
497
+ setPending: (pending: boolean) => void;
498
+ }
499
+ /**
500
+ * Build the framework-neutral `mutate` / `reset` pair of an adapter's
501
+ * custom-mutator hook (`useMutator` / `createMutator` / `mutator`).
502
+ *
503
+ * Owns the orchestration every adapter otherwise copy-pastes: ref-counts
504
+ * overlapping invocations into `setPending` (so it only clears once the last
505
+ * settles), awaits the bound handle's `isPersisted` promise, normalizes a thrown
506
+ * non-`Error`, and routes failure to `setError` (clearing it on success) before
507
+ * re-throwing. Each adapter (`@lunora/react`, `/solid`, `/svelte`, `/vue`) binds
508
+ * the two sinks to its own reactive setters, so this logic lives in exactly one
509
+ * place. The optimistic overlay + server push are owned by the bound handle.
510
+ *
511
+ * `error` tracks the LATEST invocation, not the last to settle: overlapping
512
+ * calls can resolve out of order, so an earlier call that finishes later must
513
+ * not clobber a newer call's outcome. Each invocation takes a monotonic token
514
+ * and only writes `setError` while it is still the most recent one — otherwise
515
+ * `error`/`isError` could surface a stale success or failure (the documented
516
+ * "latest invocation's error" contract every adapter advertises).
517
+ */
518
+ declare const createMutatorRunner: <TArgs>(handle: MutatorHandle<TArgs>, sinks: MutatorRunnerSinks) => {
519
+ mutate: (args: TArgs) => Promise<void>;
520
+ reset: () => void;
521
+ };
139
522
  interface QueuedMutation<T = unknown> {
140
523
  readonly args: Record<string, unknown>;
141
524
  readonly functionPath: string;
142
525
  /** Stable id used to remove the entry from durable storage once replayed; assigned by the queue when absent. */
143
526
  id?: string;
144
527
  /**
145
- * Issuing identity fingerprint carried through to durable storage (`null` =
146
- * signed out). Absent on hydrated legacy records, which replay ambiently.
147
- */
528
+ * Issuing identity fingerprint carried through to durable storage (`null` =
529
+ * signed out). Absent on hydrated legacy records, which replay ambiently.
530
+ */
148
531
  readonly identity?: string | null;
532
+ /**
533
+ * `true` when a live caller is still awaiting this write's `mutation()`
534
+ * Promise; `false`/absent for a write restored from durable storage after a
535
+ * reload (its original awaiter is gone). Carried so terminal-verdict
536
+ * observers can distinguish "the caller already saw this" from "nothing else
537
+ * will report this". Maps to the public `MutationSettledEvent.hadAwaiter`.
538
+ */
539
+ liveAwaiter?: boolean;
540
+ /**
541
+ * Invoked on a successful replay with the server's echoed commit CDC cursor,
542
+ * so a live per-call optimistic layer drops gaplessly once a frame reaches it.
543
+ * Absent on hydrated records (the optimistic write lived in a prior session).
544
+ */
545
+ readonly onCommit?: (commitCursor: number | undefined) => void;
546
+ /**
547
+ * Optional sync predicate evaluated just before replay. When it returns
548
+ * `false` the write is dropped instead of replaying, handling the case
549
+ * where the mutation's preconditions are no longer valid (e.g. the
550
+ * document it referred to was deleted while offline). Absent or `true`
551
+ * means "ok to replay".
552
+ */
553
+ readonly precondition?: () => boolean;
149
554
  /** Rejects if the mutation can no longer be replayed. */
150
555
  readonly reject: (error: unknown) => void;
151
556
  /** Resolves once the mutation has been replayed against the server. */
@@ -153,61 +558,116 @@ interface QueuedMutation<T = unknown> {
153
558
  readonly shardKey?: string;
154
559
  }
155
560
  /**
156
- * Bounded FIFO queue. Mutations issued while the client is offline are
157
- * enqueued and replayed in the order they were submitted once the WS
158
- * reconnects and identifies. If the queue exceeds `maxItems` the oldest
159
- * entry is rejected with `OFFLINE_QUEUE_OVERFLOW`.
160
- *
161
- * When a {@link PersistenceAdapter} is supplied, enqueued mutations are mirrored
162
- * to durable storage so they survive a reload — {@link OfflineQueue.hydrate} restores them on
163
- * the next startup and the client replays them on reconnect. Durable removal is
164
- * the caller's responsibility *after* a successful replay (see `LunoraClient`);
165
- * the queue only persists on enqueue and un-persists on overflow.
166
- */
561
+ * Invoked when the queue itself discards an entry on overflow (capacity
562
+ * eviction), so the client can surface the dropped write on its
563
+ * terminal-verdict observer even when the entry has no live awaiter (a hydrated
564
+ * record). The `error` carries the `OFFLINE_QUEUE_OVERFLOW` code.
565
+ */
566
+ type EvictHandler = (entry: QueuedMutation, error: Error & {
567
+ code?: string;
568
+ }) => void;
569
+ /** Injected dependencies for {@link OfflineQueue} (kept off the user-facing {@link OfflineQueueOptions}). */
570
+ interface OfflineQueueDeps {
571
+ /** Invoked when an entry is discarded on capacity overflow (carries `OFFLINE_QUEUE_OVERFLOW`). */
572
+ onEvict?: EvictHandler;
573
+ /** Invoked with the new depth after any size change (drives the client's pending-sync count). */
574
+ onSizeChange?: (size: number) => void;
575
+ /** Durable store; when present, writes are mirrored and restored across reloads. */
576
+ persistence?: PersistenceAdapter;
577
+ /** App/schema version stamped on persisted writes; mismatched records are purged on hydrate. */
578
+ version?: string;
579
+ }
580
+ /**
581
+ * Bounded FIFO queue. Mutations issued while the client is offline are
582
+ * enqueued and replayed in the order they were submitted once the WS
583
+ * reconnects and identifies. If the queue exceeds `maxItems` the oldest
584
+ * entry is rejected with `OFFLINE_QUEUE_OVERFLOW`.
585
+ *
586
+ * When a {@link PersistenceAdapter} is supplied, enqueued mutations are mirrored
587
+ * to durable storage so they survive a reload — {@link OfflineQueue.hydrate} restores them on
588
+ * the next startup and the client replays them on reconnect. Durable removal is
589
+ * the caller's responsibility *after* a successful replay (see `LunoraClient`);
590
+ * the queue only persists on enqueue and un-persists on overflow.
591
+ */
167
592
  declare class OfflineQueue {
168
593
  /** Opt-in to queueing mutations before the targeted shard's first connect. */
169
594
  readonly queueBeforeFirstConnect: boolean;
170
595
  private readonly maxItems;
171
596
  private readonly onPersistenceError;
172
597
  private readonly persistence;
598
+ private readonly onEvict;
599
+ private readonly onSizeChange;
600
+ /** App/schema version stamped on persisted writes; mismatched records are purged on hydrate. */
601
+ private readonly version;
173
602
  private readonly items;
174
- constructor(options?: OfflineQueueOptions, persistence?: PersistenceAdapter);
603
+ constructor(options?: OfflineQueueOptions, deps?: OfflineQueueDeps);
175
604
  get size(): number;
176
605
  enqueue<T>(entry: QueuedMutation<T>): void;
177
606
  /**
178
- * Restore mutations persisted in a prior session and re-queue them in FIFO
179
- * order. Restored entries already live in durable storage, so they are not
180
- * re-appended; they carry no-op `resolve`/`reject` (the original awaiter is
181
- * gone after a reload). No-op when no persistence adapter is configured.
182
- * Returns the distinct shard keys of the restored writes so the caller can
183
- * open their sockets to trigger a flush.
184
- */
607
+ * Restore mutations persisted in a prior session and re-queue them in FIFO
608
+ * order. Restored entries already live in durable storage, so they are not
609
+ * re-appended; they carry no-op `resolve`/`reject` (the original awaiter is
610
+ * gone after a reload). No-op when no persistence adapter is configured.
611
+ * Returns the distinct shard keys of the restored writes so the caller can
612
+ * open their sockets to trigger a flush.
613
+ *
614
+ * `hydrate()` runs post-construction (the caller awaits an async durable-store
615
+ * load), so a mutation issued while offline during that boot window is
616
+ * enqueued into `items` *before* this method's `await` resolves. Restored
617
+ * records are therefore `unshift`-ed ahead of whatever is already queued
618
+ * rather than `push`-ed to the end: the durable store's persist order is
619
+ * authoritative (a prior-session write is always older than anything from
620
+ * this session), so replaying a same-session boot-time write before an
621
+ * older restored write on the same document would let last-writer-wins
622
+ * silently clobber the newer data with the stale one.
623
+ */
185
624
  hydrate(): Promise<(string | undefined)[]>;
186
625
  /**
187
- * Remove and return queued mutations. With no `predicate`, drains the whole
188
- * queue. With one, drains only matching entries (preserving FIFO order) and
189
- * leaves the rest queued — used to flush a single shard's writes when its
190
- * socket reconnects while other shards are still down.
191
- */
626
+ * Remove and return queued mutations. With no `predicate`, drains the whole
627
+ * queue. With one, drains only matching entries (preserving FIFO order) and
628
+ * leaves the rest queued — used to flush a single shard's writes when its
629
+ * socket reconnects while other shards are still down.
630
+ */
192
631
  drain(predicate?: (item: QueuedMutation) => boolean): QueuedMutation[];
193
632
  /**
194
- * Return previously-drained mutations to the front of the queue, preserving
195
- * their FIFO order, without re-persisting them — they were never unpersisted,
196
- * so durable storage still holds them. Used when a flush aborts on a transient
197
- * transport failure: the unreplayed writes stay queued for the next reconnect.
198
- */
633
+ * Return previously-drained mutations to the front of the queue, preserving
634
+ * their FIFO order, without re-persisting them — they were never unpersisted,
635
+ * so durable storage still holds them. Used when a flush aborts on a transient
636
+ * transport failure: the unreplayed writes stay queued for the next reconnect.
637
+ */
199
638
  requeue(items: QueuedMutation[]): void;
639
+ /**
640
+ * Remove mutations whose precondition evaluates to `false` (stale/dirty
641
+ * writes that should not replay) and reject each with an
642
+ * `OFFLINE_PRECONDITION_FAILED` error. The valid (admitted) mutations stay
643
+ * queued in FIFO order. Returns the drained stale entries.
644
+ *
645
+ * Called during reconnect before the flush cycle to weed out writes whose
646
+ * assumptions no longer hold (e.g. a document was deleted by another client).
647
+ */
648
+ drainConflict(): QueuedMutation[];
200
649
  clear(): void;
650
+ /**
651
+ * Evict entries from the FRONT of `items` (the oldest — FIFO order) until
652
+ * the queue is at or under `maxItems`, rejecting each with
653
+ * `OFFLINE_QUEUE_OVERFLOW`, un-persisting it, and firing `onEvict`. Shared
654
+ * by `enqueue` (a live write pushes past capacity) and `hydrate` (a durable
655
+ * store restored more than `maxItems` records — CLIENT-03) so an overflow
656
+ * always drops the same way regardless of which caller triggered it.
657
+ */
658
+ private evictOverflow;
659
+ /** Notify the size observer (the client's pending-sync count) after any change. */
660
+ private notifySize;
201
661
  }
202
662
  /**
203
- * In-memory {@link PersistenceAdapter}. Doesn't survive a reload — it exists so
204
- * the persistence wiring can be exercised without IndexedDB (tests, SSR, or as
205
- * a deliberate "no durable store" choice that still satisfies the interface).
206
- * Preserves enqueue order; `clone` keeps callers from mutating stored args.
207
- */
663
+ * In-memory {@link PersistenceAdapter}. Doesn't survive a reload — it exists so
664
+ * the persistence wiring can be exercised without IndexedDB (tests, SSR, or as
665
+ * a deliberate "no durable store" choice that still satisfies the interface).
666
+ * Preserves enqueue order; `clone` keeps callers from mutating stored args.
667
+ */
208
668
  declare const createInMemoryPersistence: () => PersistenceAdapter;
209
669
  interface IndexedDbPersistenceOptions {
210
- /** Database name; defaults to `"lunora"`. */
670
+ /** Database name; defaults to `"lunora-outbox"` (its own DB, separate from the read cache). */
211
671
  databaseName?: string;
212
672
  /** Injectable `IDBFactory` (e.g. `fake-indexeddb` in tests); defaults to the global `indexedDB`. */
213
673
  indexedDB?: IDBFactory;
@@ -215,34 +675,34 @@ interface IndexedDbPersistenceOptions {
215
675
  storeName?: string;
216
676
  }
217
677
  /**
218
- * IndexedDB-backed {@link PersistenceAdapter}. Each mutation is stored under an
219
- * autoincrementing key (so `load()` returns them in enqueue order regardless of
220
- * the string ids) with a unique secondary index on `id` for `remove()`.
221
- *
222
- * The store handle is opened lazily and the open promise is cached, so repeated
223
- * ops reuse one connection. Throws eagerly if no `IDBFactory` is available —
224
- * callers in non-browser environments should use {@link createInMemoryPersistence}.
225
- */
678
+ * IndexedDB-backed {@link PersistenceAdapter}. Each mutation is stored under an
679
+ * autoincrementing key (so `load()` returns them in enqueue order regardless of
680
+ * the string ids) with a unique secondary index on `id` for `remove()`.
681
+ *
682
+ * The store handle is opened lazily and the open promise is cached, so repeated
683
+ * ops reuse one connection. Throws eagerly if no `IDBFactory` is available —
684
+ * callers in non-browser environments should use {@link createInMemoryPersistence}.
685
+ */
226
686
  declare const createIndexedDbPersistence: (options?: IndexedDbPersistenceOptions) => PersistenceAdapter;
227
687
  /**
228
- * Compose the read-cache key for a subscription. Mirrors how
229
- * `SubscriptionRegistry` keys live subscriptions so a hydrated value lines up
230
- * with the subscription that will consume it. `shardKey` defaults to `""` (the
231
- * root shard) exactly as the registry does.
232
- */
688
+ * Compose the read-cache key for a subscription. Mirrors how
689
+ * `SubscriptionRegistry` keys live subscriptions so a hydrated value lines up
690
+ * with the subscription that will consume it. `shardKey` defaults to `""` (the
691
+ * root shard) exactly as the registry does.
692
+ */
233
693
  declare const queryCacheKey: (functionPath: string, argsKey: string, shardKey?: string) => string;
234
694
  /**
235
- * In-memory {@link QueryCacheAdapter}. Doesn't survive a reload — it exists so
236
- * the read-cache wiring can be exercised without IndexedDB (tests, SSR, or as a
237
- * deliberate "no durable store" choice that still satisfies the interface).
238
- * Enforces the same LRU row cap as the IndexedDB adapter; `clone` keeps callers
239
- * from mutating stored values.
240
- */
695
+ * In-memory {@link QueryCacheAdapter}. Doesn't survive a reload — it exists so
696
+ * the read-cache wiring can be exercised without IndexedDB (tests, SSR, or as a
697
+ * deliberate "no durable store" choice that still satisfies the interface).
698
+ * Enforces the same LRU row cap as the IndexedDB adapter; `clone` keeps callers
699
+ * from mutating stored values.
700
+ */
241
701
  declare const createInMemoryQueryCache: (options?: {
242
702
  maxEntries?: number;
243
703
  }) => QueryCacheAdapter;
244
704
  interface IndexedDbQueryCacheOptions {
245
- /** Database name; defaults to `"lunora"` (shared with the offline-mutation store). */
705
+ /** Database name; defaults to `"lunora-query-cache"` (its own DB, separate from the offline outbox). */
246
706
  databaseName?: string;
247
707
  /** Injectable `IDBFactory` (e.g. `fake-indexeddb` in tests); defaults to the global `indexedDB`. */
248
708
  indexedDB?: IDBFactory;
@@ -252,25 +712,25 @@ interface IndexedDbQueryCacheOptions {
252
712
  storeName?: string;
253
713
  }
254
714
  /**
255
- * IndexedDB-backed {@link QueryCacheAdapter}. Each query is stored under its
256
- * composite key (`functionPath::argsKey::shardKey`) with a `ts` index driving
257
- * LRU eviction. The store handle is opened lazily and cached, so repeated ops
258
- * reuse one connection.
259
- *
260
- * The store lives in the same `lunora` database as the offline-mutation queue
261
- * (bumped to schema v2). Opening it upgrades a v1 database in place, adding the
262
- * `query-cache` store without touching `offline-mutations`. Throws eagerly if no
263
- * `IDBFactory` is available — callers in non-browser environments should use
264
- * {@link createInMemoryQueryCache}.
265
- */
715
+ * IndexedDB-backed {@link QueryCacheAdapter}. Each query is stored under its
716
+ * composite key (`functionPath::argsKey::shardKey`) with a `ts` index driving
717
+ * LRU eviction. The store handle is opened lazily and cached, so repeated ops
718
+ * reuse one connection.
719
+ *
720
+ * The store lives in its own `lunora-query-cache` database deliberately
721
+ * separate from the offline-mutation outbox's `lunora-outbox` database so the two
722
+ * independently-toggleable adapters never share (and drift on) a schema version.
723
+ * Throws eagerly if no `IDBFactory` is available — callers in non-browser
724
+ * environments should use {@link createInMemoryQueryCache}.
725
+ */
266
726
  declare const createIndexedDbQueryCache: (options?: IndexedDbQueryCacheOptions) => QueryCacheAdapter;
267
727
  /**
268
- * Exponential backoff calculator with optional jitter.
269
- *
270
- * `next()` doubles the delay each call up to `maxDelayMs`. When `jitter` is
271
- * enabled the returned value is randomized in `[delay/2, delay]` so a fleet
272
- * of clients reconnecting at the same time spread out their retries.
273
- */
728
+ * Exponential backoff calculator with optional jitter.
729
+ *
730
+ * `next()` doubles the delay each call up to `maxDelayMs`. When `jitter` is
731
+ * enabled the returned value is randomized in `[delay/2, delay]` so a fleet
732
+ * of clients reconnecting at the same time spread out their retries.
733
+ */
274
734
  interface ReconnectCalculator {
275
735
  /** Returns the delay to wait before the next reconnect attempt. */
276
736
  next: () => number;
@@ -278,4 +738,108 @@ interface ReconnectCalculator {
278
738
  reset: () => void;
279
739
  }
280
740
  declare const createReconnect: (options?: ReconnectOptions, random?: () => number) => ReconnectCalculator;
281
- export { type ArgsOf, type AsyncStorageLike, type AsyncStoragePersistenceOptions, type BookmarkStorage, CONFLICT_ERROR_CODE, type FunctionReference, type IndexedDbPersistenceOptions, type IndexedDbQueryCacheOptions, type MutationCallOptions, type MutationDelta, type MutationRunnerSinks, OfflineQueue, type OfflineQueueOptions, type PersistenceAdapter, type QueryCacheAdapter, type QueuedMutation, type ReconnectCalculator, type ReconnectOptions, type ReturnOf, applyDelta, createAsyncStoragePersistence, createInMemoryBookmarkStorage, createInMemoryPersistence, createInMemoryQueryCache, createIndexedDbPersistence, createIndexedDbQueryCache, createMutationRunner, createReconnect, isConflictError, isMutationDelta, queryCacheKey };
741
+ /**
742
+ * Capture a snapshot of the current live query value at call time and produce a
743
+ * `() => boolean` precondition that compares it against the value at replay time.
744
+ *
745
+ * When the precondition is checked (on queue drain / reconnect) it re-reads the
746
+ * query's current value via {@link LunoraClient.peekActiveQueryValue}. If the
747
+ * value differs from what was captured at call time the precondition returns
748
+ * `false` and the offline mutation is dropped as stale.
749
+ * @example
750
+ * ```ts
751
+ * client.mutation(api.todos.update, { id, text }, {
752
+ * precondition: createSnapshotPrecondition(client, api.todos.list, { userId }),
753
+ * });
754
+ * ```
755
+ */
756
+ declare const createSnapshotPrecondition: (client: LunoraClient, functionRef: FunctionReference, args: Record<string, unknown>, shardKey?: string) => (() => boolean);
757
+ /**
758
+ * Client-side service-worker registration and lifecycle management.
759
+ *
760
+ * Usage:
761
+ * ```ts
762
+ * const sw = new ClientServiceWorker({ swUrl: "/sw.js" });
763
+ * await sw.register();
764
+ *
765
+ * if (sw.active) {
766
+ * sw.postMessage({ type: "sync" });
767
+ * }
768
+ * ```
769
+ */
770
+ type ServiceWorkerStatus = "unsupported" | "unregistered" | "registering" | "active" | "error";
771
+ interface ClientSwOptions {
772
+ /** Called when the status changes. */
773
+ onStatusChange?: (status: ServiceWorkerStatus) => void;
774
+ /** Optional registration scope. Defaults to `/`. */
775
+ scope?: string;
776
+ /** URL of the service worker script (relative to origin). */
777
+ swUrl: string;
778
+ }
779
+ /**
780
+ * Manages service-worker registration and provides a simple API for
781
+ * sending messages and listening for responses.
782
+ */
783
+ declare class ClientServiceWorker {
784
+ #private;
785
+ readonly swUrl: string;
786
+ readonly scope: string;
787
+ constructor(options: ClientSwOptions);
788
+ /** Current registration status. */
789
+ get status(): ServiceWorkerStatus;
790
+ /** The underlying `ServiceWorkerRegistration`, if registered. */
791
+ get registration(): ServiceWorkerRegistration | undefined;
792
+ /** Whether the SW is currently controlling this page. */
793
+ get active(): boolean;
794
+ /**
795
+ * Register the service worker.
796
+ *
797
+ * Returns `false` when the browser does not support service workers.
798
+ */
799
+ register(): Promise<boolean>;
800
+ /**
801
+ * Remove the active service worker registration and clear listeners.
802
+ *
803
+ * Returns `false` when no registration is currently held.
804
+ */
805
+ unregister(): Promise<boolean>;
806
+ /**
807
+ * Send a message to the active service worker.
808
+ */
809
+ postMessage(message: unknown): void;
810
+ /**
811
+ * Register a handler for messages **from** the service worker.
812
+ * @returns Unsubscribe function.
813
+ */
814
+ onMessage(handler: (event: MessageEvent) => void): () => void;
815
+ }
816
+ /**
817
+ * Outbound message from the client to the SW.
818
+ */
819
+ interface ClientToSwMessage {
820
+ /** Opaque correlation ID for request/response patterns. */
821
+ correlationId?: string;
822
+ payload?: unknown;
823
+ type: string;
824
+ }
825
+ /**
826
+ * Inbound message from the SW to the client.
827
+ */
828
+ interface SwToClientMessage {
829
+ /** Echoes the correlation ID from the client request, if any. */
830
+ correlationId?: string;
831
+ payload?: unknown;
832
+ type: string;
833
+ }
834
+ /**
835
+ * Send a typed message to the service worker and optionally await a
836
+ * matching response.
837
+ * @returns A promise that resolves when the SW sends a reply with the
838
+ * same `correlationId` (if `expectResponse` is true).
839
+ */
840
+ declare const sendToSw: (sw: ServiceWorker | null, message: ClientToSwMessage, expectResponse?: boolean) => Promise<unknown>;
841
+ /**
842
+ * Create a reply for a client message (call from inside the SW).
843
+ */
844
+ declare const createReply: (original: ClientToSwMessage, payload?: unknown) => SwToClientMessage;
845
+ export { type ArgsOf, type AsyncStorageLike, type AsyncStoragePersistenceOptions, type BookmarkStorage, ClientServiceWorker, type ClientSwOptions, type ClientToSwMessage, type ConnectionStatus, type FunctionReference, type HttpStreamArgsOf, type HttpStreamChunkOf, type HttpStreamOptions, type HttpStreamRef, type IndexedDbPersistenceOptions, type IndexedDbQueryCacheOptions, LunoraClient, type MutationCallOptions, type MutationDelta, type MutationRunnerSinks, type MutatorHandle, type MutatorRunnerSinks, type MutatorTransaction, OfflineQueue, type OfflineQueueOptions, type OptimisticMessage, type PersistenceAdapter, type QueryCacheAdapter, type QueuedMutation, RETIRE_AFTER_DURABLE_SEQ_ADVANCE, type ReconcileDurableMessage, type ReconnectCalculator, type ReconnectOptions, type ReturnOf, type ServiceWorkerStatus, type StreamIterable, type SubscriptionError, type SwToClientMessage, TabCoordinator, anyApi, applyDelta, createAsyncStoragePersistence, createInMemoryBookmarkStorage, createInMemoryPersistence, createInMemoryQueryCache, createIndexedDbPersistence, createIndexedDbQueryCache, createMutationRunner, createMutatorRunner, createReconnect, createReply, createSnapshotPrecondition, httpStream, isMutationDelta, maxSeq, queryCacheKey, reconcileOptimistic, sendToSw };