@spooky-sync/core 0.0.1-canary.17 → 0.0.1-canary.170

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 (139) hide show
  1. package/AGENTS.md +57 -0
  2. package/dist/index.d.ts +2046 -365
  3. package/dist/index.js +11016 -2476
  4. package/dist/otel/index.d.ts +21 -0
  5. package/dist/otel/index.js +86 -0
  6. package/dist/sqlite-open.js +276 -0
  7. package/dist/sqlite-worker.d.ts +1 -0
  8. package/dist/sqlite-worker.js +420 -0
  9. package/dist/tabs-broker-worker.d.ts +8 -0
  10. package/dist/tabs-broker-worker.js +434 -0
  11. package/dist/types.d.ts +997 -0
  12. package/package.json +14 -7
  13. package/scripts/check-broker-bundle.mjs +33 -0
  14. package/skills/{spooky-core → sp00ky-core}/SKILL.md +94 -18
  15. package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
  16. package/skills/{spooky-core → sp00ky-core}/references/config.md +4 -4
  17. package/src/build-globals.d.ts +12 -0
  18. package/src/events/events.test.ts +2 -1
  19. package/src/events/index.ts +3 -0
  20. package/src/index.ts +24 -2
  21. package/src/modules/app-release/index.test.ts +125 -0
  22. package/src/modules/app-release/index.ts +201 -0
  23. package/src/modules/auth/events/index.ts +2 -1
  24. package/src/modules/auth/index.ts +59 -20
  25. package/src/modules/cache/index.ts +112 -32
  26. package/src/modules/cache/types.ts +2 -2
  27. package/src/modules/crdt/crdt-field.ts +294 -0
  28. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  29. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  30. package/src/modules/crdt/index.ts +463 -0
  31. package/src/modules/crdt/loro-loader.ts +25 -0
  32. package/src/modules/data/data.hydration.test.ts +142 -0
  33. package/src/modules/data/data.membership.test.ts +297 -0
  34. package/src/modules/data/data.rebind.test.ts +147 -0
  35. package/src/modules/data/data.run.test.ts +113 -0
  36. package/src/modules/data/data.status.test.ts +249 -0
  37. package/src/modules/data/id-set-plan.test.ts +122 -0
  38. package/src/modules/data/index.ts +1403 -129
  39. package/src/modules/data/mutation-id.test.ts +25 -0
  40. package/src/modules/data/mutation-id.ts +35 -0
  41. package/src/modules/data/window-query.test.ts +52 -0
  42. package/src/modules/data/window-query.ts +194 -0
  43. package/src/modules/devtools/flags.ts +349 -0
  44. package/src/modules/devtools/index.ts +382 -37
  45. package/src/modules/devtools/notify-throttle.test.ts +149 -0
  46. package/src/modules/devtools/storage-info.test.ts +79 -0
  47. package/src/modules/devtools/storage-info.ts +168 -0
  48. package/src/modules/devtools/versions.test.ts +74 -0
  49. package/src/modules/devtools/versions.ts +81 -0
  50. package/src/modules/feature-flag/index.test.ts +251 -0
  51. package/src/modules/feature-flag/index.ts +308 -0
  52. package/src/modules/ref-tables.test.ts +91 -0
  53. package/src/modules/ref-tables.ts +88 -0
  54. package/src/modules/sync/engine.ts +101 -37
  55. package/src/modules/sync/events/index.ts +9 -2
  56. package/src/modules/sync/queue/queue-down.ts +12 -5
  57. package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
  58. package/src/modules/sync/queue/queue-up.ts +216 -56
  59. package/src/modules/sync/scheduler.pause.test.ts +109 -0
  60. package/src/modules/sync/scheduler.ts +77 -9
  61. package/src/modules/sync/sync.health.test.ts +149 -0
  62. package/src/modules/sync/sync.live-removal.test.ts +134 -0
  63. package/src/modules/sync/sync.reconnect.test.ts +145 -0
  64. package/src/modules/sync/sync.subquery.test.ts +82 -0
  65. package/src/modules/sync/sync.ts +1456 -92
  66. package/src/modules/sync/utils.test.ts +269 -2
  67. package/src/modules/sync/utils.ts +182 -17
  68. package/src/otel/index.ts +127 -0
  69. package/src/services/blobs/blob-cache.test.ts +359 -0
  70. package/src/services/blobs/blob-cache.ts +603 -0
  71. package/src/services/blobs/blob-manifest.ts +227 -0
  72. package/src/services/blobs/blob-store.test.ts +77 -0
  73. package/src/services/blobs/blob-store.ts +359 -0
  74. package/src/services/blobs/blob.fixture.ts +90 -0
  75. package/src/services/blobs/index.ts +70 -0
  76. package/src/services/database/cache-engine.ts +160 -0
  77. package/src/services/database/connection-supervisor.test.ts +289 -0
  78. package/src/services/database/connection-supervisor.ts +415 -0
  79. package/src/services/database/database.query-timeout.test.ts +83 -0
  80. package/src/services/database/database.ts +32 -12
  81. package/src/services/database/engine-factory.ts +33 -0
  82. package/src/services/database/events/index.ts +2 -1
  83. package/src/services/database/index.ts +7 -0
  84. package/src/services/database/local-migrator.ts +30 -27
  85. package/src/services/database/local.test.ts +64 -0
  86. package/src/services/database/local.ts +478 -67
  87. package/src/services/database/plan-render.test.ts +159 -0
  88. package/src/services/database/plan-render.ts +108 -0
  89. package/src/services/database/relation-resolver.test.ts +413 -0
  90. package/src/services/database/relation-resolver.ts +0 -0
  91. package/src/services/database/remote.ts +110 -14
  92. package/src/services/database/sqlite-cache-engine.test.ts +464 -0
  93. package/src/services/database/sqlite-cache-engine.ts +1159 -0
  94. package/src/services/database/sqlite-open.test.ts +150 -0
  95. package/src/services/database/sqlite-open.ts +164 -0
  96. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  97. package/src/services/database/sqlite-plan-sql.ts +106 -0
  98. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  99. package/src/services/database/sqlite-select.test.ts +246 -0
  100. package/src/services/database/sqlite-select.ts +113 -0
  101. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  102. package/src/services/database/sqlite-transport.ts +219 -0
  103. package/src/services/database/sqlite-worker.ts +437 -0
  104. package/src/services/database/surql-translate.ts +291 -0
  105. package/src/services/database/surreal-cache-engine.ts +141 -0
  106. package/src/services/logger/index.ts +6 -109
  107. package/src/services/persistence/localstorage.ts +2 -2
  108. package/src/services/persistence/resilient.ts +11 -4
  109. package/src/services/persistence/surrealdb.ts +10 -10
  110. package/src/services/stream-processor/index.ts +444 -52
  111. package/src/services/stream-processor/permissions.test.ts +47 -0
  112. package/src/services/stream-processor/permissions.ts +53 -0
  113. package/src/services/stream-processor/stream-processor.batch.test.ts +136 -0
  114. package/src/services/stream-processor/stream-processor.reset.test.ts +216 -0
  115. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  116. package/src/services/stream-processor/wasm-types.ts +23 -2
  117. package/src/services/tabs/broker-client.ts +283 -0
  118. package/src/services/tabs/broker.test.ts +278 -0
  119. package/src/services/tabs/coordinator.test.ts +244 -0
  120. package/src/services/tabs/coordinator.ts +576 -0
  121. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  122. package/src/services/tabs/leader-locks.ts +75 -0
  123. package/src/services/tabs/protocol.ts +242 -0
  124. package/src/services/tabs/support.ts +36 -0
  125. package/src/services/tabs/tabs-broker-worker.ts +586 -0
  126. package/src/sp00ky.auth-order.test.ts +92 -0
  127. package/src/sp00ky.init-query.test.ts +183 -0
  128. package/src/sp00ky.ts +1424 -0
  129. package/src/types.ts +467 -15
  130. package/src/utils/error-classification.test.ts +44 -0
  131. package/src/utils/error-classification.ts +7 -0
  132. package/src/utils/index.ts +73 -13
  133. package/src/utils/parser.ts +3 -2
  134. package/src/utils/semver.test.ts +32 -0
  135. package/src/utils/semver.ts +30 -0
  136. package/src/utils/surql.ts +30 -18
  137. package/src/utils/withRetry.test.ts +1 -1
  138. package/tsdown.config.ts +86 -1
  139. package/src/spooky.ts +0 -395
package/dist/index.d.ts CHANGED
@@ -1,231 +1,240 @@
1
+ import { A as Sp00kyQueryResultPromise, B as LocalStore, C as ReconnectConfig, D as RunOptions, E as RegistrationTimings, F as SyncHealthConfig, G as SyncEventSystem, H as DatabaseEventSystem, I as SyncHealthStatus, K as EventDefinition, L as TimingPhase, M as StorageHealthStatus, N as StoreType, O as Sp00kyConfig, P as SyncHealth, R as UpdateOptions, S as QueryUpdateCallback, T as RecordVersionDiff, U as DatabaseEventTypes, V as SealedQuery, W as Logger$1, _ as QueryState, a as MATERIALIZATION_SAMPLE_WINDOW, b as QueryTimeToLive, c as MutationEventType, d as PinoTransmit, f as PreloadOptions, g as QueryHash, h as QueryConfigRecord, i as Level, j as StorageHealth, k as Sp00kyQueryResult, l as PersistenceClient, m as QueryConfig, n as DebounceOptions, o as MutationCallback, p as PreloadRefresh, q as EventSystem, r as EventSubscriptionOptions, s as MutationEvent, t as ConnectionState, u as PhaseStat, v as QueryStatus, w as RecordVersionArray, x as QueryTimings, y as QueryStatusCallback, z as UpEvent } from "./types.js";
1
2
  import * as surrealdb0 from "surrealdb";
2
- import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
3
- import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RecordId as RecordId$1, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
4
- import { Level, Level as Level$1, Logger } from "pino";
3
+ import { Duration, RecordId, Surreal as Surreal$1, SurrealEvents, SurrealTransaction } from "surrealdb";
4
+ import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, FinalQuery, GetTable, QueryBuilder, QueryOptions, QueryPlan, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
5
+ import { Logger } from "pino";
6
+ import { LoroDoc } from "loro-crdt";
5
7
 
6
- //#region src/events/index.d.ts
7
- /**
8
- * Utility type to define the payload structure of an event.
9
- * If the payload type P is never, it defines payload as undefined.
10
- */
11
- type EventPayloadDefinition<P> = [P] extends [never] ? {
12
- payload: undefined;
13
- } : {
14
- payload: P;
15
- };
16
- /**
17
- * Defines the structure of an event with a specific type and payload.
18
- * @template T The string literal type of the event.
19
- * @template P The type of the event payload.
20
- */
21
- type EventDefinition<T extends string, P> = {
22
- type: T;
23
- } & EventPayloadDefinition<P>;
24
- /**
25
- * A map of event types to their definitions.
26
- * Keys are event names, values are EventDefinitions.
27
- */
28
- type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
29
- /**
30
- * Options for pushing/emitting events.
31
- */
32
- interface PushEventOptions {
33
- /** Configuration for debouncing the event. */
34
- debounced?: {
35
- key: string;
36
- delay: number;
37
- };
38
- }
39
- /**
40
- * Extracts the full Event object type from the map for a given key.
41
- */
42
- type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
43
- /**
44
- * Extracts the payload type from the map for a given key.
45
- */
46
- type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
47
- /**
48
- * Array of available event type keys.
49
- */
50
- type EventTypes<E extends EventTypeMap> = (keyof E)[];
51
- /**
52
- * Represents a valid key (event name) from the EventTypeMap.
53
- */
54
- type EventType<E extends EventTypeMap> = keyof E;
55
- /**
56
- * Function signature for an event handler.
57
- */
58
- type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
59
- /**
60
- * Options when subscribing to an event.
61
- */
62
- type EventSubscriptionOptions$1 = {
63
- /** If true, the handler will be called immediately with the last emitted event of this type (if any). */
64
- immediately?: boolean;
65
- /** If true, the subscription will be automatically removed after the first event is handled. */
66
- once?: boolean;
67
- };
68
- /**
69
- * A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
70
- * @template E The EventTypeMap defining all supported events.
71
- */
72
- declare class EventSystem<E extends EventTypeMap> {
73
- private _eventTypes;
74
- private subscriberId;
75
- private isProcessing;
76
- private buffer;
77
- private subscribers;
78
- private subscribersTypeMap;
79
- private lastEvents;
80
- private debouncedEvents;
81
- constructor(_eventTypes: EventTypes<E>);
82
- get eventTypes(): EventTypes<E>;
83
- /**
84
- * Subscribes a handler to a specific event type.
85
- * @param type The event type to subscribe to.
86
- * @param handler The function to call when the event occurs.
87
- * @param options Subscription options (once, immediately).
88
- * @returns A subscription ID that can be used to unsubscribe.
89
- */
90
- subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
91
- /**
92
- * Subscribes a handler to multiple event types.
93
- * @param types An array of event types to subscribe to.
94
- * @param handler The function to call when any of the events occur.
95
- * @param options Subscription options.
96
- * @returns An array of subscription IDs.
97
- */
98
- subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
99
- /**
100
- * Unsubscribes a specific subscription by ID.
101
- * @param id The subscription ID returned by subscribe().
102
- * @returns True if the subscription was found and removed, false otherwise.
103
- */
104
- unsubscribe(id: number): boolean;
105
- /**
106
- * Emits an event with the given type and payload.
107
- * @param type The type of event to emit.
108
- * @param payload The data associated with the event.
109
- */
110
- emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
111
- /**
112
- * Adds a fully constructed event object to the system.
113
- * Similar to emit, but takes the full event object directly.
114
- * Supports debouncing if options are provided.
115
- * @param event The event object.
116
- * @param options Options for the event push (e.g., debouncing).
117
- */
118
- addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
119
- private handleDebouncedEvent;
120
- private scheduleProcessing;
121
- private processEvents;
122
- private dequeue;
123
- private setLastEvent;
124
- private broadcastEvent;
125
- }
126
- //#endregion
127
- //#region src/services/logger/index.d.ts
128
- type Logger$1 = Logger;
129
- //#endregion
130
- //#region src/services/database/events/index.d.ts
131
- declare const DatabaseEventTypes: {
132
- readonly LocalQuery: "DATABASE_LOCAL_QUERY";
133
- readonly RemoteQuery: "DATABASE_REMOTE_QUERY";
134
- };
135
- interface DatabaseQueryEventPayload {
136
- query: string;
137
- vars?: Record<string, unknown>;
138
- duration: number;
139
- success: boolean;
140
- error?: string;
141
- timestamp: number;
142
- }
143
- type DatabaseEventTypeMap = {
144
- [DatabaseEventTypes.LocalQuery]: EventDefinition<typeof DatabaseEventTypes.LocalQuery, DatabaseQueryEventPayload>;
145
- [DatabaseEventTypes.RemoteQuery]: EventDefinition<typeof DatabaseEventTypes.RemoteQuery, DatabaseQueryEventPayload>;
146
- };
147
- type DatabaseEventSystem = EventSystem<DatabaseEventTypeMap>;
148
- //#endregion
149
- //#region src/utils/surql.d.ts
150
- interface SealedQuery<T = void> {
151
- readonly sql: string;
152
- readonly extract: (results: unknown[]) => T;
153
- }
154
- //#endregion
155
8
  //#region src/services/database/database.d.ts
156
9
  declare abstract class AbstractDatabaseService {
157
- protected client: Surreal;
10
+ protected client: Surreal$1;
158
11
  protected logger: Logger$1;
159
12
  protected events: DatabaseEventSystem;
13
+ /**
14
+ * Per-query deadline in ms; `0` disables. Only the remote service sets this
15
+ * (see `RemoteDatabaseService`) — a local query can be legitimately slow and
16
+ * has its own retry ladders, and there is no half-open-socket failure mode
17
+ * for an in-process engine.
18
+ */
19
+ protected queryTimeoutMs: number;
160
20
  protected abstract eventType: typeof DatabaseEventTypes.LocalQuery | typeof DatabaseEventTypes.RemoteQuery;
161
- constructor(client: Surreal, logger: Logger$1, events: DatabaseEventSystem);
21
+ constructor(client: Surreal$1, logger: Logger$1, events: DatabaseEventSystem);
162
22
  abstract connect(): Promise<void>;
163
- getClient(): Surreal;
23
+ getClient(): Surreal$1;
164
24
  getEvents(): DatabaseEventSystem;
165
25
  tx(): Promise<SurrealTransaction>;
166
26
  private queryQueue;
167
27
  /**
168
28
  * Execute a query with serialized execution to prevent WASM transaction issues.
29
+ *
30
+ * Serialization means every query waits on the previous one, so a call that
31
+ * never settles blocks the whole chain forever. {@link queryTimeoutMs} bounds
32
+ * each link: on expiry this promise rejects and the chain moves on, even
33
+ * though the underlying RPC is still parked in the SDK's pending map.
169
34
  */
170
35
  query<T extends unknown[]>(query: string, vars?: Record<string, unknown>): Promise<T>;
171
36
  execute<T>(query: SealedQuery<T>, vars?: Record<string, unknown>): Promise<T>;
172
37
  close(): Promise<void>;
173
38
  }
174
39
  //#endregion
175
- //#region src/services/database/local.d.ts
176
- declare class LocalDatabaseService extends AbstractDatabaseService {
177
- private config;
178
- protected eventType: "DATABASE_LOCAL_QUERY";
179
- constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
180
- getConfig(): SpookyConfig<any>['database'];
181
- connect(): Promise<void>;
182
- }
183
- //#endregion
184
40
  //#region src/services/database/remote.d.ts
41
+ /** Transport events the SDK publishes, mapped 1:1 to {@link ConnectionState}. */
42
+ type RemoteConnectionEvent = ConnectionState | 'error';
185
43
  declare class RemoteDatabaseService extends AbstractDatabaseService {
186
44
  private config;
187
45
  protected eventType: "DATABASE_REMOTE_QUERY";
188
- constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
189
- getConfig(): SpookyConfig<any>['database'];
46
+ private readonly reconnectConfig;
47
+ /**
48
+ * In-flight `connect()`, so concurrent callers (boot + supervisor revive +
49
+ * an `online` event landing at the same moment) share one attempt instead of
50
+ * racing two sockets. Cleared on settle, so a later call always reconnects.
51
+ */
52
+ private connecting;
53
+ constructor(config: Sp00kyConfig<any>['database'], logger: Logger$1);
54
+ getConfig(): Sp00kyConfig<any>['database'];
55
+ /** Resolved reconnect tunables; the supervisor reads its own knobs here. */
56
+ getReconnectConfig(): Required<ReconnectConfig>;
57
+ /** Current transport state as reported by the SDK. */
58
+ getStatus(): ConnectionState;
59
+ /**
60
+ * Observe transport events. Thin passthrough so callers (the supervisor,
61
+ * sync, CRDT) don't have to reach through `getClient()`.
62
+ */
63
+ subscribeConnection<K extends RemoteConnectionEvent>(event: K, cb: (...payload: SurrealEvents[K]) => void): () => void;
64
+ /**
65
+ * Tear the socket down on purpose. Used by the heartbeat watchdog when a
66
+ * socket stops answering but never closes: `close()` makes the SDK publish
67
+ * `disconnected`, which is what drives the supervisor's revive loop.
68
+ */
69
+ forceClose(): Promise<void>;
70
+ /**
71
+ * Open (or re-open) the remote connection.
72
+ *
73
+ * Safe to call repeatedly: concurrent calls share the in-flight attempt, and
74
+ * a call after a `disconnected` builds a fresh socket. `use()` and
75
+ * `authenticate()` are re-applied here for the cold path; the SDK also
76
+ * replays them itself on its own internal reconnects.
77
+ */
190
78
  connect(): Promise<void>;
79
+ private doConnect;
191
80
  signin(params: any): Promise<any>;
192
81
  signup(params: any): Promise<any>;
193
82
  authenticate(token: string): Promise<any>;
194
83
  invalidate(): Promise<void>;
195
84
  }
196
85
  //#endregion
197
- //#region src/modules/sync/queue/queue-up.d.ts
198
- type CreateEvent = {
199
- type: 'create';
200
- mutation_id: RecordId;
201
- record_id: RecordId;
202
- data: Record<string, unknown>;
203
- record?: Record<string, unknown>;
204
- tableName?: string;
205
- options?: PushEventOptions;
86
+ //#region src/services/database/connection-supervisor.d.ts
87
+ /**
88
+ * Keeps the remote WebSocket alive for the whole life of the page.
89
+ *
90
+ * The SurrealDB SDK reconnects on its own after a socket `close`, but that
91
+ * covers only one of three ways the connection dies:
92
+ *
93
+ * 1. **Socket closes, SDK recovers.** Handled entirely by the SDK. This
94
+ * supervisor only observes it (to report `reconnecting` upward).
95
+ * 2. **Socket closes, SDK gives up.** With `attempts: -1` this shouldn't happen
96
+ * from exhaustion — but the SDK also terminates the engine permanently when
97
+ * its post-reconnect handshake throws (it re-runs `version()`, `use()`,
98
+ * `authenticate()` on every reconnect and closes the engine on any error).
99
+ * One transient hiccup there would otherwise kill the page's connection for
100
+ * good. The revive loop re-opens from scratch.
101
+ * 3. **Socket never closes at all.** A half-open connection: the peer is gone
102
+ * (NAT timeout, wifi switch, laptop sleep) but no FIN ever arrives, so
103
+ * `readyState` stays OPEN and the SDK's own 30s ping — fire-and-forget, no
104
+ * response deadline — never notices. Nothing ever fires a `close` event, so
105
+ * nothing ever triggers a reconnect. The heartbeat detects this and forces
106
+ * the teardown that case 2's loop then repairs.
107
+ *
108
+ * Plus wake triggers: coming back `online` or un-hiding the tab probes
109
+ * immediately rather than waiting out a backoff that was scheduled while the
110
+ * network was known-down.
111
+ */
112
+ declare class ConnectionSupervisor {
113
+ private readonly remote;
114
+ private readonly logger;
115
+ private readonly config;
116
+ private state;
117
+ private subscribers;
118
+ private started;
119
+ private disposed;
120
+ private heartbeatTimer;
121
+ private heartbeatInFlight;
122
+ /** Consecutive failed probes. See {@link FAILURES_BEFORE_TEARDOWN}. */
123
+ private heartbeatFailures;
124
+ private reviveTimer;
125
+ private reviveAttempts;
126
+ /** Timestamp of the last wake-triggered probe, for rate limiting. */
127
+ private lastWakeProbeAt;
128
+ private reviving;
129
+ /**
130
+ * Set while the browser reports itself offline. Retrying a socket against a
131
+ * down interface only burns backoff, so the loop parks until `online` fires.
132
+ */
133
+ private suspended;
134
+ private teardown;
135
+ private static readonly REVIVE_BASE_MS;
136
+ /**
137
+ * How many consecutive heartbeat failures it takes to tear the socket down.
138
+ *
139
+ * The probe rides the same serialized queue as every other RPC (deliberately
140
+ * — see {@link beat}), which means it cannot distinguish a WEDGED queue from
141
+ * a merely BUSY one. A single slow window (a large sync burst, one heavy
142
+ * app query) used to be enough to force-close a perfectly healthy socket,
143
+ * and the resulting reconnect re-registered every active query about a
144
+ * second later. That self-inflicted teardown manufactured the very reconnect
145
+ * storms this class exists to survive. A genuinely dead socket still fails
146
+ * every probe, so it is torn down one interval later than before.
147
+ */
148
+ private static readonly FAILURES_BEFORE_TEARDOWN;
149
+ /** Retry delay after an inconclusive (first) heartbeat failure. */
150
+ private static readonly HEARTBEAT_RETRY_MS;
151
+ /** Floor between probes triggered by wake events (tab focus, pageshow). */
152
+ private static readonly WAKE_PROBE_MIN_INTERVAL_MS;
153
+ constructor(remote: RemoteDatabaseService, logger: Logger$1, config?: Required<ReconnectConfig>);
154
+ /** Latest observed transport state. */
155
+ get connection(): ConnectionState;
156
+ /**
157
+ * Observe transport state. Fires immediately with the current value and again
158
+ * on every change. Returns an unsubscribe.
159
+ */
160
+ subscribe(cb: (state: ConnectionState) => void): () => void;
161
+ /**
162
+ * Begin supervising. Call once, after the initial {@link
163
+ * RemoteDatabaseService.connect}. Idempotent.
164
+ */
165
+ start(): void;
166
+ /** Stop all timers and listeners. Safe to call more than once. */
167
+ dispose(): void;
168
+ private setState;
169
+ private clearReviveTimer;
170
+ /**
171
+ * Queue the next `connect()` attempt on exponential backoff, capped at
172
+ * `superviseRetryDelayMaxMs`. Never gives up — the page is expected to
173
+ * outlive any outage.
174
+ */
175
+ private scheduleRevive;
176
+ private revive;
177
+ private stopHeartbeat;
178
+ private startHeartbeat;
179
+ /**
180
+ * Probe the server end-to-end. Deliberately goes through
181
+ * `remote.query` — the same serialized queue every other remote call uses —
182
+ * so a queue wedged behind a stuck RPC also fails the heartbeat instead of
183
+ * being invisible to it.
184
+ */
185
+ private beat;
186
+ /**
187
+ * A restored network or an un-hidden tab is the strongest available hint that
188
+ * a reconnect will now succeed, so probe immediately instead of waiting out a
189
+ * backoff scheduled under worse conditions.
190
+ */
191
+ private installWakeTriggers;
192
+ /**
193
+ * Reset the backoff and act on whichever problem is present: reconnect if the
194
+ * socket is gone, otherwise probe it (it may be half-open — which is exactly
195
+ * what a sleep/wake cycle produces).
196
+ */
197
+ private wake;
198
+ }
199
+ //#endregion
200
+ //#region src/modules/sync/queue/queue-down.d.ts
201
+ type RegisterEvent = {
202
+ type: 'register';
203
+ payload: {
204
+ hash: string;
205
+ };
206
206
  };
207
- type UpdateEvent = {
208
- type: 'update';
209
- mutation_id: RecordId;
210
- record_id: RecordId;
211
- data: Record<string, unknown>;
212
- record?: Record<string, unknown>;
213
- beforeRecord?: Record<string, unknown>;
214
- options?: PushEventOptions;
207
+ type SyncEvent = {
208
+ type: 'sync';
209
+ payload: {
210
+ hash: string;
211
+ };
215
212
  };
216
- type DeleteEvent = {
217
- type: 'delete';
218
- mutation_id: RecordId;
219
- record_id: RecordId;
220
- options?: PushEventOptions;
213
+ type HeartbeatEvent = {
214
+ type: 'heartbeat';
215
+ payload: {
216
+ hash: string;
217
+ };
221
218
  };
222
- type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
219
+ type CleanupEvent = {
220
+ type: 'cleanup';
221
+ payload: {
222
+ hash: string;
223
+ };
224
+ };
225
+ type DownEvent = RegisterEvent | SyncEvent | HeartbeatEvent | CleanupEvent;
223
226
  //#endregion
224
227
  //#region src/services/stream-processor/wasm-types.d.ts
225
228
  interface WasmStreamUpdate {
226
229
  query_id: string;
227
230
  result_hash: string;
228
231
  result_data: RecordVersionArray;
232
+ timing_store_apply_ms?: number;
233
+ timing_circuit_step_ms?: number;
234
+ timing_transform_ms?: number;
235
+ timing_parse_ms?: number;
236
+ timing_plan_ms?: number;
237
+ timing_snapshot_ms?: number;
229
238
  }
230
239
  //#endregion
231
240
  //#region src/services/stream-processor/index.d.ts
@@ -246,6 +255,25 @@ interface StreamUpdate {
246
255
  queryHash: string;
247
256
  localArray: RecordVersionArray;
248
257
  op?: 'CREATE' | 'UPDATE' | 'DELETE';
258
+ /**
259
+ * End-to-end ingest latency for the WASM call that produced this update,
260
+ * in milliseconds. Populated by StreamProcessorService.ingest. Undefined
261
+ * for the initial register_view snapshot.
262
+ */
263
+ materializationTimeMs?: number;
264
+ /** SSP internal sub-phase timings (ms) for this ingest, from the WASM binding. */
265
+ storeApplyMs?: number;
266
+ circuitStepMs?: number;
267
+ transformMs?: number;
268
+ /**
269
+ * One-shot registration timings (ms). Only set on the StreamUpdate returned
270
+ * by `registerQueryPlan` (the register_view snapshot), not on ingest updates.
271
+ */
272
+ registration?: {
273
+ parseMs: number;
274
+ planMs: number;
275
+ snapshotMs: number;
276
+ };
249
277
  }
250
278
  type StreamProcessorEvents = {
251
279
  stream_update: EventDefinition<'stream_update', StreamUpdate[]>;
@@ -265,19 +293,123 @@ declare class StreamProcessorService {
265
293
  private processor;
266
294
  private isInitialized;
267
295
  private receivers;
268
- constructor(events: EventSystem<StreamProcessorEvents>, db: LocalDatabaseService, persistenceClient: PersistenceClient, logger: Logger);
296
+ private batching;
297
+ private batchBuffer;
298
+ private sessionAuth;
299
+ private stateKeySuffix;
300
+ private stateGeneration;
301
+ private persistState;
302
+ private persistCircuit;
303
+ private checkpointMs;
304
+ private checkpointTimer;
305
+ private snapshotDirty;
306
+ private pagehideHandler;
307
+ constructor(events: EventSystem<StreamProcessorEvents>, db: LocalStore, persistenceClient: PersistenceClient, logger: Logger);
269
308
  /**
270
309
  * Add a receiver for stream updates.
271
310
  * Multiple receivers can be registered (DataManager, DevTools, etc.)
272
311
  */
273
312
  addReceiver(receiver: StreamUpdateReceiver): void;
274
313
  private notifyUpdates;
314
+ private dispatchUpdates;
315
+ /**
316
+ * Ingest a batch of record changes as a single bulk operation, firing only
317
+ * one coalesced `StreamUpdate` per affected query once every record has been
318
+ * ingested (instead of one update per record). Use this whenever multiple
319
+ * records land at once — e.g. sync fetching N missing rows — so a list query
320
+ * re-runs and the UI re-renders once for the whole batch rather than
321
+ * row-by-row.
322
+ *
323
+ * Internally opens a coalescing window, ingests each record, then flushes;
324
+ * processor state is persisted once for the whole batch. No-op for an empty
325
+ * batch.
326
+ */
327
+ ingestMany(records: Array<{
328
+ table: string;
329
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
330
+ id: string;
331
+ record: any;
332
+ }>): void;
333
+ /**
334
+ * Open a coalescing window. While open, the per-record stream updates
335
+ * emitted by `ingest` are buffered (one entry per queryHash) instead of
336
+ * dispatched. Always paired with `flushCoalescing()` in a try/finally by
337
+ * `ingestMany` so the window always closes — otherwise the processor stays
338
+ * stuck buffering forever.
339
+ *
340
+ * No-op if a window is already open (nested batches aren't expected here).
341
+ */
342
+ private beginCoalescing;
343
+ /**
344
+ * Close the coalescing window and flush: dispatch one coalesced
345
+ * `StreamUpdate` per buffered queryHash, then persist processor state once
346
+ * for the whole batch (instead of once per ingest).
347
+ */
348
+ private flushCoalescing;
275
349
  /**
276
350
  * Initialize the WASM module and processor.
277
351
  * This must be called before using other methods.
278
352
  */
279
353
  init(): Promise<void>;
354
+ /** Route the persisted circuit snapshot to a per-bucket key. */
355
+ setStateKeySuffix(bucketId: string): void;
356
+ private stateKey;
357
+ /**
358
+ * Drop the current WASM processor and start a fresh, empty circuit. Used on
359
+ * local-bucket switches: the old circuit holds the previous user's rows AND
360
+ * views registered with the previous `$auth` context, so neither may survive.
361
+ * Deliberately does NOT `loadState()` — a persisted snapshot references views
362
+ * under a dead sessionId salt; the DataModule rebind re-registers every live
363
+ * view against this fresh processor. Caller must re-seed `setPermissions`
364
+ * afterwards (a fresh circuit default-denies every table).
365
+ */
366
+ reset(): Promise<void>;
367
+ /**
368
+ * Release the wasm circuit and stop checkpointing. Call when the client is
369
+ * torn down; a recreated client (provider remount, HMR) would otherwise stack
370
+ * one full circuit per instance.
371
+ */
372
+ dispose(): void;
373
+ /**
374
+ * Explicitly run the wasm-bindgen destructor. Guarded: stale wasm builds may
375
+ * not expose `free`, and a double free must not take the app down.
376
+ */
377
+ private freeProcessor;
378
+ /** Toggle circuit-state persistence (shared-tabs follower/leader role). */
379
+ setPersistenceEnabled(enabled: boolean): void;
380
+ /**
381
+ * Opt into snapshot persistence (`persistCircuit`). Off by default: see the
382
+ * `persistCircuit` field comment for why per-ingest snapshots were removed.
383
+ * Must be called before `init()` for a snapshot to be restored at boot.
384
+ */
385
+ configureCircuitPersistence(enabled: boolean, checkpointMs?: number): void;
386
+ /**
387
+ * Record that the circuit changed. Cheap and O(1), the expensive snapshot is
388
+ * deferred to the checkpoint timer, and skipped entirely when
389
+ * `persistCircuit` is off (the default).
390
+ */
391
+ private markSnapshotDirty;
392
+ private startCheckpoints;
393
+ /** Stop checkpointing and drop the `pagehide` listener. */
394
+ stopCheckpoints(): void;
280
395
  loadState(): Promise<void>;
396
+ /**
397
+ * Seed per-table `select` permission predicates ({ [table]: whereText }).
398
+ * Must run after the processor exists and before any `register_view`, else
399
+ * non-`_00_` tables are default-denied and registration fails.
400
+ */
401
+ setPermissions(permissions: Record<string, string>): void;
402
+ /**
403
+ * Set the current session's auth identity for permission injection,
404
+ * mirroring the server's `fn::query::register`
405
+ * (`object::extend(params, { auth: { id: $auth.id }, access: $access })`).
406
+ * Stored as strings (empty when logged out) and applied to every
407
+ * `register_view` in {@link registerQueryPlan}. Must be set before a
408
+ * `$auth`-gated query registers (and re-set on auth state changes), or the
409
+ * in-browser SSP's `permission_inject` rejects it with
410
+ * "requires $auth but registration params lack it".
411
+ */
412
+ setSessionAuth(authId: string | null, access: string | null): void;
281
413
  saveState(): Promise<void>;
282
414
  /**
283
415
  * Ingest a record change into the processor.
@@ -297,203 +429,960 @@ declare class StreamProcessorService {
297
429
  private normalizeValue;
298
430
  }
299
431
  //#endregion
300
- //#region src/types.d.ts
301
- /**
302
- * The type of storage backend to use for the local database.
303
- * - 'memory': In-memory storage (transient).
304
- * - 'indexeddb': IndexedDB storage (persistent).
305
- */
306
- type StoreType = 'memory' | 'indexeddb';
432
+ //#region src/modules/cache/types.d.ts
433
+ type RecordWithId = Record<string, any> & {
434
+ id: RecordId<string>;
435
+ };
436
+ interface QueryConfig$1 {
437
+ queryHash: string;
438
+ surql: string;
439
+ params: Record<string, any>;
440
+ ttl: QueryTimeToLive | Duration;
441
+ lastActiveAt: Date;
442
+ }
443
+ interface CacheRecord {
444
+ table: string;
445
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
446
+ record: RecordWithId;
447
+ version: number;
448
+ }
449
+ //#endregion
450
+ //#region src/modules/cache/index.d.ts
307
451
  /**
308
- * Interface for a custom persistence client.
309
- * Allows providing a custom storage mechanism for the local database.
452
+ * CacheModule - Centralized storage and DBSP ingestion
453
+ *
454
+ * Single responsibility: Handle all local storage operations and DBSP ingestion.
455
+ * This module acts as the bridge between data operations and persistence.
310
456
  */
311
- interface PersistenceClient {
457
+ /** One ingested change, in exactly the shape `ingestMany` consumes. Shared
458
+ * with the tabs protocol so a leader can relay its ingests to followers. */
459
+ interface CacheIngestTuple {
460
+ table: string;
461
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
462
+ id: string;
463
+ record: Record<string, unknown>;
464
+ }
465
+ declare class CacheModule implements StreamUpdateReceiver {
466
+ private local;
467
+ private streamProcessor;
468
+ private logger;
469
+ private streamUpdateCallback;
470
+ private versionLookups;
471
+ /** Shared-tabs leader: fan every committed ingest out to follower circuits.
472
+ * Fired AFTER the local tx (the rows are already in the shared store, so a
473
+ * follower only needs the circuit feed). Never set on followers. */
474
+ private ingestRelay;
475
+ constructor(local: LocalStore, streamProcessor: StreamProcessorService, streamUpdateCallback: (update: StreamUpdate) => void, logger: Logger$1);
312
476
  /**
313
- * Sets a value in the storage.
314
- * @param key The key to set.
315
- * @param value The value to store.
477
+ * Implements StreamUpdateReceiver interface
478
+ * Called directly by StreamProcessor when views change
316
479
  */
317
- set<T>(key: string, value: T): Promise<void>;
480
+ onStreamUpdate(update: StreamUpdate): void;
481
+ setIngestRelay(cb: ((tuples: CacheIngestTuple[]) => void) | null): void;
318
482
  /**
319
- * Gets a value from the storage.
320
- * @param key The key to retrieve.
321
- * @returns The stored value or null if not found.
483
+ * Shared-tabs follower: feed relayed tuples into THIS tab's circuit only.
484
+ * The rows are already in the shared store (the leader wrote them), so no
485
+ * local write happens here; the normal chain then runs: SSP -> stream update
486
+ * -> DataModule debounce -> materializeRecords (re-reads via the port
487
+ * transport) -> this tab's subscriptions fire with this tab's hashes.
322
488
  */
323
- get<T>(key: string): Promise<T | null>;
489
+ applyRelayedIngest(tuples: CacheIngestTuple[]): void;
490
+ lookup(recordId: string): number;
491
+ /** Drop the version cache on a bucket switch — a stale version would make
492
+ * the sync diff skip fetching a body the new bucket legitimately needs. */
493
+ clearVersionLookups(): void;
324
494
  /**
325
- * Removes a value from the storage.
326
- * @param key The key to remove.
495
+ * Save a single record to local DB and ingest into DBSP
496
+ * Used by mutations (create/update)
327
497
  */
328
- remove(key: string): Promise<void>;
329
- }
330
- /**
331
- * Supported Time-To-Live (TTL) values for cached queries.
332
- * Format: number + unit (m=minutes, h=hours, d=days).
333
- */
334
- type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
335
- /**
336
- * Result object returned when a query is registered or executed.
337
- */
338
- interface SpookyQueryResult {
339
- /** The unique hash identifier for the query. */
340
- hash: string;
341
- }
342
- type SpookyQueryResultPromise = Promise<SpookyQueryResult>;
343
- interface EventSubscriptionOptions {
344
- priority?: number;
345
- }
346
- /**
347
- * Configuration options for the Spooky client.
348
- * @template S The schema structure type.
349
- */
350
- interface SpookyConfig<S extends SchemaStructure> {
351
- /** Database connection configuration. */
352
- database: {
353
- /** The SurrealDB endpoint URL. */
354
- endpoint?: string;
355
- /** The namespace to use. */
356
- namespace: string;
357
- /** The database name. */
358
- database: string;
359
- /** The local store type implementation. */
360
- store?: StoreType;
361
- /** Authentication token. */
362
- token?: string;
363
- };
364
- /** Unique client identifier. If not provided, one will be generated. */
365
- clientId?: string;
366
- /** The schema definition. */
367
- schema: S;
368
- /** The compiled SURQL schema string. */
369
- schemaSurql: string;
370
- /** Logging level. */
371
- logLevel: Level$1;
498
+ save(cacheRecord: CacheRecord, skipDbInsert?: boolean): Promise<void>;
499
+ /**
500
+ * Save multiple records in a batch
501
+ * More efficient than calling save() multiple times
502
+ * Used by sync operations
503
+ */
504
+ saveBatch(records: CacheRecord[], skipDbInsert?: boolean): Promise<void>;
372
505
  /**
373
- * Persistence client to use.
374
- * Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
506
+ * Delete a record from local DB and ingest deletion into DBSP
375
507
  */
376
- persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
377
- /** OpenTelemetry collector endpoint for telemetry data. */
378
- otelEndpoint?: string;
508
+ delete(table: string, id: string, skipDbDelete?: boolean, recordData?: Record<string, any>): Promise<void>;
509
+ /**
510
+ * Register a query with DBSP to create a materialized view
511
+ * Returns the initial result array
512
+ */
513
+ registerQuery(config: QueryConfig$1): {
514
+ localArray: RecordVersionArray;
515
+ registrationTimings?: {
516
+ parseMs: number;
517
+ planMs: number;
518
+ snapshotMs: number;
519
+ };
520
+ };
379
521
  /**
380
- * Debounce time in milliseconds for stream updates.
381
- * Defaults to 100ms.
522
+ * Unregister a query from DBSP
382
523
  */
383
- streamDebounceTime?: number;
524
+ unregisterQuery(queryHash: string): void;
384
525
  }
385
- type QueryHash = string;
386
- type RecordVersionArray = Array<[string, number]>;
526
+ //#endregion
527
+ //#region src/modules/data/index.d.ts
387
528
  /**
388
- * Represents the difference between two record version sets.
389
- * Used for synchronizing local and remote states.
529
+ * DataModule - Unified query and mutation management
530
+ *
531
+ * Merges the functionality of QueryManager and MutationManager.
532
+ * Uses CacheModule for all storage operations.
390
533
  */
391
- interface RecordVersionDiff {
392
- /** List of records added. */
393
- added: Array<{
394
- id: RecordId$1<string>;
395
- version: number;
396
- }>;
397
- /** List of records updated. */
398
- updated: Array<{
399
- id: RecordId$1<string>;
400
- version: number;
534
+ declare class DataModule<S extends SchemaStructure> {
535
+ private cache;
536
+ private local;
537
+ private schema;
538
+ private streamDebounceTime;
539
+ /** Tab identity baked into mutation ids (shared-tabs rollback routing);
540
+ * undefined in solo mode, where mutation-id falls back to a session id. */
541
+ private tabId;
542
+ private activeQueries;
543
+ private pendingQueries;
544
+ private subscriptions;
545
+ private statusSubscriptions;
546
+ private mutationCallbacks;
547
+ private debounceTimers;
548
+ private pendingStreamUpdates;
549
+ private fetchDepth;
550
+ private logger;
551
+ /**
552
+ * Optional observer notified whenever a query's fetch status changes.
553
+ * Wired by Sp00kyClient to push status changes into DevTools. Kept as a
554
+ * settable field (rather than a constructor arg) because DevTools is
555
+ * constructed after DataModule.
556
+ */
557
+ onQueryStatusChange?: (hash: QueryHash, status: QueryStatus) => void;
558
+ /**
559
+ * Optional observer invoked when a still-subscribed query's TTL heartbeat
560
+ * fires (~90% of the TTL). Wired by Sp00kyClient to
561
+ * `Sp00kySync.heartbeatQuery`, which refreshes the remote `_00_query`
562
+ * row's `lastActiveAt` so an actively-watched query never expires. Settable
563
+ * field (not a constructor arg) because the sync engine is wired after
564
+ * DataModule is constructed — mirrors `onQueryStatusChange`.
565
+ */
566
+ onHeartbeat?: (hash: QueryHash) => void;
567
+ /**
568
+ * Optional hook fired by {@link deregisterQuery} when an opt-in query (e.g. a
569
+ * viewport-windowed list cancelling an off-screen window) loses its last
570
+ * subscriber. Wired by Sp00kyClient to enqueue a `cleanup` down-event, which
571
+ * tears the remote `_00_query` view down (releasing its `_00_list_ref` edges)
572
+ * instead of leaving it for the TTL sweep. The local view + state are freed in
573
+ * {@link finalizeDeregister} only after that remote delete, so a fast
574
+ * re-subscribe (scroll back) can abort/heal the teardown — see `cleanupQuery`.
575
+ */
576
+ onDeregister?: (hash: QueryHash) => void;
577
+ private sessionId;
578
+ private currentUserId;
579
+ constructor(cache: CacheModule, local: LocalStore, schema: S, logger: Logger$1, streamDebounceTime?: number);
580
+ init(sessionId: string): Promise<void>;
581
+ /**
582
+ * Update the session salt used in query-id hashing. Call this when the
583
+ * SurrealDB session changes (sign-in, sign-out, reconnect). Subsequently
584
+ * registered queries will get fresh, session-scoped IDs.
585
+ */
586
+ setSessionId(sessionId: string): void;
587
+ /** Shared-tabs: bake this tab's identity into mutation ids so a rollback of
588
+ * a follower's mutation routes back to the tab that made it. */
589
+ setTabId(tabId: string): void;
590
+ /**
591
+ * Update the authenticated user record id. Pass `null` on sign-out.
592
+ * Read by `Sp00kySync.listRefTable()` so the LIVE subscription and
593
+ * the poll route to the same per-user `_00_list_ref_user_<id>` the
594
+ * SSP writes to.
595
+ */
596
+ setCurrentUserId(userId: string | null): void;
597
+ /** Read-only view of the authenticated user id used for per-user
598
+ * `_00_list_ref` routing. Other modules consult this so they pick the
599
+ * same table name DataModule does. */
600
+ getCurrentUserId(): string | null;
601
+ /**
602
+ * Register a query and return its hash for subscriptions
603
+ */
604
+ query<T extends TableNames<S>>(tableName: T, surqlString: string, params: Record<string, any>, ttl: QueryTimeToLive, plan?: QueryPlan): Promise<QueryHash>;
605
+ /**
606
+ * Subscribe to query updates
607
+ */
608
+ subscribe(queryHash: string, callback: QueryUpdateCallback, options?: {
609
+ immediate?: boolean;
610
+ }): () => void;
611
+ /**
612
+ * Subscribe to a query's fetch-status changes (idle/fetching).
613
+ * With `{ immediate: true }` the callback fires synchronously with the
614
+ * current status (defaults to `idle` if the query isn't registered yet).
615
+ */
616
+ subscribeStatus(queryHash: string, callback: QueryStatusCallback, options?: {
617
+ immediate?: boolean;
618
+ }): () => void;
619
+ /**
620
+ * Set a query's fetch status and notify status observers (DevTools +
621
+ * `subscribeStatus` listeners). No-op when the status is unchanged or the
622
+ * query is unknown.
623
+ */
624
+ setQueryStatus(queryHash: string, status: QueryStatus): void;
625
+ /**
626
+ * Enter a fetch cycle for a query. Refcounted: registration and concurrent
627
+ * poll/LIVE sync rounds can overlap on the same hash, and only the OUTERMOST
628
+ * cycle may flip the status — 0→1 emits `fetching`, and `endFetching`'s 1→0
629
+ * emits `idle`. Always pair with `endFetching` in a `finally`.
630
+ */
631
+ beginFetching(queryHash: string): void;
632
+ /** Leave a fetch cycle started with {@link beginFetching}; emits `idle` on the last exit. */
633
+ endFetching(queryHash: string): void;
634
+ /**
635
+ * Subscribe to mutations (for sync)
636
+ */
637
+ onMutation(callback: MutationCallback): () => void;
638
+ /**
639
+ * Handle stream updates from DBSP (via CacheModule)
640
+ */
641
+ onStreamUpdate(update: StreamUpdate): Promise<void>;
642
+ /**
643
+ * Process a query's pending (debounced) stream update NOW instead of on the
644
+ * trailing edge. Called by the sync engine before it flips a query back to
645
+ * `idle`, so the status change never races ahead of the rows it fetched.
646
+ * No-op when nothing is pending. The pending entry is removed before the
647
+ * await so a concurrently-firing timer can't process it twice.
648
+ */
649
+ flushPendingStreamUpdate(queryHash: string): Promise<void>;
650
+ /**
651
+ * Materialize a query's result rows from the local store.
652
+ *
653
+ * A query's rows are its MEMBERSHIP — the id-set the server put in
654
+ * `_00_list_ref` (`remoteArray`) — not "every local body that matches the
655
+ * WHERE". Those two disagree, and the disagreement was the bug: when a row
656
+ * leaves a query's window but still exists upstream, `handleRemovedRecords`
657
+ * keeps its local body and never re-fetches it, so a predicate re-scan finds
658
+ * that stale body still matching and keeps rendering the row. Selecting the
659
+ * id-set directly is also the only correct thing for a windowed query, where
660
+ * re-applying `START m` against the shared local store skips the window's own
661
+ * rows entirely (sparse windowing) and returns nothing.
662
+ *
663
+ * The rendered set is:
664
+ *
665
+ * (membership ∪ (pendingWrites ∩ localArray)) − pendingDeletes
666
+ *
667
+ * The middle term keeps optimistic writes visible without re-admitting stale
668
+ * rows. Every local write is fed to the SSP (`cache.saveBatch` →
669
+ * `ingestMany`), so `localArray` answers "does this row match the predicate
670
+ * per LOCAL truth". A pending write that moves a row into the window is in
671
+ * `localArray` and shows; one that moves a row out is absent and does not; a
672
+ * stale body the server dropped has no pending write at all, so it stays out.
673
+ * `pendingDeletes` covers the reverse lag — the server still lists a row whose
674
+ * DELETE is sitting in our outbox.
675
+ *
676
+ * Falls back to the predicate scan only when membership has never been
677
+ * established (a query first run on this device), so an offline first paint
678
+ * still shows something.
679
+ */
680
+ private materializeRecords;
681
+ /**
682
+ * The materialization itself, without the DevTools timing wrapper. Split out so
683
+ * cold-start seeding can use it before a `QueryState` exists.
684
+ */
685
+ private materializeFromConfig;
686
+ /**
687
+ * The authoritative membership list to render from, or `null` when membership
688
+ * has never been established and the caller must fall back to a scan.
689
+ *
690
+ * A windowed query has no usable fallback — re-running its `START m` locally
691
+ * returns the wrong rows — so it renders from whatever id-set is on hand
692
+ * (SSP's included) rather than degrading to a scan. That is the pre-existing
693
+ * behavior for windows and is preserved.
694
+ */
695
+ private resolveMembership;
696
+ /** Apply the pending-write union and pending-delete subtraction, and map to
697
+ * RecordIds for the engines' id-set path. */
698
+ private buildRenderIds;
699
+ private processStreamUpdate;
700
+ /**
701
+ * Compute p55/p90/p99 from a rolling window of materialization samples.
702
+ * Returns nulls for any percentile that has no samples yet so SurrealDB
703
+ * `option<float>` columns stay NONE rather than 0 before the first ingest.
704
+ */
705
+ private computeMaterializationPercentiles;
706
+ /** Record a per-phase timing sample (ms) on a query's rolling window. */
707
+ private recordPhase;
708
+ /** Record the remote record-fetch time (ms) for a query. Called by the sync engine. */
709
+ recordRemoteFetch(hash: string, ms: number): void;
710
+ /**
711
+ * Record the frontend reconcile time (ms) for a query. Called from `useQuery`
712
+ * via `Sp00kyClient.reportFrontendTiming` after it applies an update to its store.
713
+ */
714
+ recordFrontendTiming(hash: string, ms: number): void;
715
+ /**
716
+ * Build the per-query processing-time breakdown surfaced to the DevTools panel
717
+ * and the MCP. `ssp` is the WASM-ingest wall time (from `materializationSamples`);
718
+ * the rest come from the per-phase rolling windows + one-shot registration timings.
719
+ */
720
+ phaseTimings(q: QueryState): QueryTimings;
721
+ /**
722
+ * Get query state (for sync and devtools)
723
+ */
724
+ getQueryByHash(hash: string): QueryState | undefined;
725
+ /**
726
+ * Cold-query guard for instant-hydrate: true when the query exists, hasn't been
727
+ * hydrated, and has NOT yet fetched its server result (`remoteArray` empty).
728
+ * We gate on `remoteArray`, not local `records`: a windowed query is often
729
+ * partially pre-seeded from the circuit (e.g. the dashboard's 5-row preview),
730
+ * but it still hasn't loaded its own full window from the server — so it should
731
+ * still hydrate. A warm re-subscribe (remoteArray already populated) is skipped.
732
+ */
733
+ isCold(hash: string): boolean;
734
+ /**
735
+ * Walk a hydrated record's fields and append any EMBEDDED child records to
736
+ * `batch` (recursing for nested related fields). An embedded child is a
737
+ * value that is itself a record — a non-null object whose `id` is a
738
+ * `RecordId` — or an array of such records (one-to-many vs one-to-one). A
739
+ * bare `RecordId` (a foreign-key reference) or any other value is skipped,
740
+ * so this never mistakes a FK column for an embedded body. Children are
741
+ * keyed by their own `record.id.table`, versioned by `_00_rv`, and cleaned
742
+ * to their table's real columns (which strips the alias/related fields).
743
+ * `seen` dedupes within the batch.
744
+ */
745
+ private collectEmbeddedChildren;
746
+ /**
747
+ * Prepare a subquery-bearing row (preload / hydration) for the schemafull
748
+ * local store: replace an embedded FORWARD-relation object (`author = { id, … }`)
749
+ * with its RecordId so a `record<…>` field coerces, and DROP reverse-subquery
750
+ * ARRAYS (`comments = [ … ]`) since their rows are cached separately as their
751
+ * own bodies. A flat record — as the live `SELECT * FROM $ids` sync returns,
752
+ * with relations already RecordIds — passes through unchanged.
753
+ */
754
+ private flattenRelationsForStorage;
755
+ /**
756
+ * Instant-hydrate: ingest rows fetched one-shot from the remote (the query's own
757
+ * surql run directly) so the query DISPLAYS immediately, while the full realtime
758
+ * registration proceeds in the background. Ingests with versions (`_00_rv`) so the
759
+ * later `syncRecords` dedup skips re-pulling unchanged bodies, and seeds
760
+ * `remoteArray` so windowed queries materialize the correct window (no sparse
761
+ * local-circuit issue). Runs at most once per query (the `hydrated` flag).
762
+ */
763
+ applyHydration(hash: string, rows: RecordWithId[]): Promise<void>;
764
+ /**
765
+ * Build the cache batch for a set of one-shot rows and persist it to the
766
+ * local DB + in-browser SSP. Maps each row to a `CREATE` op on its own table
767
+ * and extracts EMBEDDED related children (any nesting depth) as their own
768
+ * records — a `.related()` query returns its children embedded, and a later
769
+ * correlated re-materialization needs them present as standalone rows.
770
+ * Shared by `applyHydration` (live registration) and `persistSnapshot`
771
+ * (preload).
772
+ */
773
+ private buildAndSaveCacheBatch;
774
+ /**
775
+ * Preload/prewarm: persist one-shot rows (and their embedded related children)
776
+ * into the local cache WITHOUT registering a query — no `activeQueries` entry,
777
+ * no `_00_query` view, no TTL heartbeat. The rows live in the local DB as
778
+ * ordinary bodies (never GC'd on their own) so a later `useQuery` seeds its
779
+ * first paint from them instantly, then registers a live view to freshen.
780
+ */
781
+ persistSnapshot(tableName: string, rows: RecordWithId[]): Promise<void>;
782
+ /**
783
+ * Read the durable preload freshness marker for a query hash, or null if this
784
+ * query was never preloaded in the current bucket. Co-located with the cached
785
+ * rows (per-bucket `_00_preload` table) so a bucket switch that clears the
786
+ * data also clears the marker — a stale marker can't claim "warm" when the
787
+ * rows are gone. Any read error is treated as cold.
788
+ */
789
+ getPreloadMarker(hash: string): Promise<{
790
+ fetchedAt: number;
791
+ rowCount: number;
792
+ } | null>;
793
+ /** Stamp the preload freshness marker after a successful snapshot fetch. */
794
+ writePreloadMarker(hash: string, rowCount: number): Promise<void>;
795
+ /**
796
+ * Read the durable membership row, or `null` if this query has never had
797
+ * authoritative membership on this device. Any read error is treated as
798
+ * "unknown" so a broken row degrades to the predicate scan rather than
799
+ * rendering an empty list.
800
+ */
801
+ getWindowMembership(key: string): Promise<RecordVersionArray | null>;
802
+ /** Persist the durable membership row. Best-effort: callers must not fail a
803
+ * sync round because the mirror write failed. */
804
+ writeWindowMembership(key: string, ids: RecordVersionArray): Promise<void>;
805
+ /**
806
+ * Record ids with a mutation still in the outbox, split by direction.
807
+ *
808
+ * Both halves feed {@link materializeRecords}: `writes` keeps optimistic
809
+ * creates/updates visible before the server has acknowledged them, and
810
+ * `deletes` suppresses rows the server still lists because our DELETE hasn't
811
+ * been processed yet. Reading `_00_pending_mutations` (rather than tracking
812
+ * ids in memory) is what makes both survive a reload.
813
+ *
814
+ * On failure returns empty sets: membership alone then decides, which can
815
+ * briefly hide an optimistic write but never resurrects a deleted row.
816
+ */
817
+ getPendingRecordIds(): Promise<{
818
+ writes: Set<string>;
819
+ deletes: Set<string>;
401
820
  }>;
402
- /** List of record IDs removed. */
403
- removed: RecordId$1<string>[];
821
+ /** True while ≥1 live subscriber is watching this query (refcount guard). */
822
+ hasSubscribers(hash: string): boolean;
823
+ /**
824
+ * Opt-in eager teardown for a query whose LAST subscriber just left — used by
825
+ * viewport-windowed lists to cancel off-screen windows instead of leaving
826
+ * their remote views to expire on the TTL sweep. No-op while any subscriber
827
+ * remains (refcount). Only enqueues the remote cleanup here; the local WASM
828
+ * view + in-memory state are freed in {@link finalizeDeregister} after the
829
+ * remote delete completes, so a re-subscribe in between aborts/heals it.
830
+ *
831
+ * NOTE: most queries should NOT use this — the default keep-alive on
832
+ * unsubscribe avoids re-registration churn on navigation.
833
+ */
834
+ deregisterQuery(hash: string): void;
835
+ /**
836
+ * Final local teardown after the remote `_00_query` row was deleted: free the
837
+ * WASM view, heartbeat timer, debounce timer, and in-memory state. Caller
838
+ * (`cleanupQuery`) guarantees no subscriber remains.
839
+ */
840
+ finalizeDeregister(hash: string): void;
841
+ /**
842
+ * Get query state by id (for sync and devtools)
843
+ */
844
+ getQueryById(id: RecordId<string>): QueryState | undefined;
845
+ /**
846
+ * Get all active queries (for devtools)
847
+ */
848
+ getActiveQueries(): QueryState[];
849
+ getActiveQueryHashes(): QueryHash[];
850
+ updateQueryLocalArray(id: string, localArray: RecordVersionArray): Promise<void>;
851
+ updateQueryRemoteArray(hash: string, remoteArray: RecordVersionArray): Promise<void>;
852
+ /**
853
+ * Cancel every armed timer ahead of a local-bucket switch: stream-update
854
+ * debounce timers (their pending updates carry the OLD bucket's id-sets) and
855
+ * per-query TTL heartbeats (they'd refresh the previous user's remote
856
+ * `_00_query` rows under the new session). The rebind re-arms heartbeats.
857
+ */
858
+ quiesce(): void;
859
+ /**
860
+ * Re-home every active query in a freshly-opened bucket, KEEPING its hash —
861
+ * `useQuery` subscriptions are keyed by hash and don't re-register on auth
862
+ * changes, so the hooks must stay attached. Per query:
863
+ * 1. reset the sync arrays + hydration flag and drop the previous user's
864
+ * records, notifying subscribers with the new-bucket materialization
865
+ * (usually empty) so their rows leave the UI immediately;
866
+ * 2. recreate the `_00_query` row in the new bucket;
867
+ * 3. re-register the SSP view on the (fresh, post-reset) processor — this
868
+ * also rebinds the view to the NEW `$auth` context;
869
+ * 4. restart the TTL heartbeat.
870
+ * Returns the hashes so the caller can enqueue remote re-registration, which
871
+ * refills records from the server via the normal register→sync→notify path.
872
+ */
873
+ rebindAfterBucketSwitch(): Promise<QueryHash[]>;
874
+ /**
875
+ * Called after a query's initial sync completes.
876
+ * Ensures subscribers are notified even if no stream updates fired (e.g. empty result set).
877
+ */
878
+ notifyQuerySynced(queryHash: string): Promise<void>;
879
+ run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, data: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
880
+ /**
881
+ * Build the outbox job record + resolve its table for a backend route.
882
+ *
883
+ * Every job is a single execution. Recurring work is declared server-side
884
+ * (`schedules:` in sp00ky.yml) and the scheduler creates a fresh row per cycle,
885
+ * so nothing here needs to know about schedules.
886
+ */
887
+ private buildJobRecord;
888
+ /**
889
+ * Create a new record
890
+ */
891
+ create<T extends Record<string, unknown>>(id: string, data: T): Promise<T>;
892
+ /**
893
+ * Update an existing record
894
+ */
895
+ update<T extends Record<string, unknown>>(table: string, id: string, data: Partial<T>, options?: UpdateOptions): Promise<T>;
896
+ /**
897
+ * Delete a record
898
+ */
899
+ delete(table: string, id: string): Promise<void>;
900
+ /**
901
+ * Rollback a failed optimistic create by deleting the record locally
902
+ */
903
+ rollbackCreate(recordId: RecordId, tableName: string): Promise<void>;
904
+ /**
905
+ * Rollback a failed optimistic update by restoring the previous record state
906
+ */
907
+ rollbackUpdate(recordId: RecordId, tableName: string, beforeRecord: Record<string, unknown>): Promise<void>;
908
+ /**
909
+ * Remove a record from all active query states and notify subscribers
910
+ */
911
+ private removeRecordFromQueries;
912
+ private createAndRegisterQuery;
913
+ private createNewQuery;
914
+ private calculateHash;
915
+ /**
916
+ * Session-independent counterpart of {@link calculateHash}: the key for a
917
+ * query's durable `_00_window` membership row.
918
+ *
919
+ * Deliberately the SAME inputs minus the `session::id()` salt, so the two keys
920
+ * can never drift apart. The salt is right for `_00_query` (two tabs must not
921
+ * fight over one row) and wrong for membership, which has to be recognizable
922
+ * after a reload — a reload mints a new session id, and offline the salt is
923
+ * `''`, so a salted key can never match what the previous session wrote.
924
+ */
925
+ private calculateMembershipKey;
926
+ private sha256;
927
+ private startTTLHeartbeat;
928
+ private replaceRecordInQueries;
404
929
  }
405
930
  /**
406
- * Configuration for a specific query instance.
407
- * Stores metadata about the query's state, parameters, and versioning.
931
+ * Parse update options to generate push event options
408
932
  */
409
- interface QueryConfig {
410
- /** The unique ID of the query config record. */
411
- id: RecordId$1<string>;
412
- /** The SURQL query string. */
413
- surql: string;
414
- /** Parameters used in the query. */
415
- params: Record<string, any>;
416
- /** The version array representing the local state of results. */
417
- localArray: RecordVersionArray;
418
- /** The version array representing the remote (server) state of results. */
419
- remoteArray: RecordVersionArray;
420
- /** Time-To-Live for this query. */
421
- ttl: QueryTimeToLive;
422
- /** Timestamp when the query was last accessed/active. */
423
- lastActiveAt: Date;
424
- /** The name of the table this query targets (if applicable). */
425
- tableName: string;
426
- }
427
- type QueryConfigRecord = QueryConfig & {
933
+ //#endregion
934
+ //#region src/services/tabs/protocol.d.ts
935
+ type TabId = string;
936
+ type TabRole = 'solo' | 'leader' | 'follower';
937
+ /** Broker pings every tab at this cadence. */
938
+
939
+ /** Matches `CacheIngestTuple` (modules/cache): exactly what `ingestMany`
940
+ * consumes, so relayed batches feed follower circuits without reshaping. */
941
+ interface IngestTuple {
942
+ table: string;
943
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
428
944
  id: string;
945
+ record: Record<string, unknown>;
946
+ }
947
+ type FollowerToLeaderMessage = {
948
+ type: 'sync-hello';
949
+ tabId: TabId;
950
+ }
951
+ /** The follower committed an outbox row (through the shared store) and the
952
+ * leader should drain it. Idempotent; a new leader's loadFromDatabase is
953
+ * the backstop for a notify lost in a failover window. */ | {
954
+ type: 'mutation-enqueued';
955
+ mutationId: string;
956
+ } | {
957
+ type: 'request-poll';
429
958
  };
430
- /**
431
- * Internal state of a live query.
432
- */
433
- interface QueryState {
434
- /** The configuration for this query. */
435
- config: QueryConfig;
436
- /** The current cached records for this query. */
437
- records: Record<string, any>[];
438
- /** Timer for TTL expiration. */
439
- ttlTimer: NodeJS.Timeout | null;
440
- /** TTL duration in milliseconds. */
441
- ttlDurationMs: number;
442
- /** Number of times the query has been updated. */
443
- updateCount: number;
444
- }
445
- type QueryUpdateCallback = (records: Record<string, any>[]) => void;
446
- type MutationCallback = (mutations: UpEvent[]) => void;
447
- type MutationEventType = 'create' | 'update' | 'delete';
448
- /**
449
- * Represents a mutation event (create, update, delete) to be synchronized.
450
- */
451
- interface MutationEvent {
452
- /** Example: 'create', 'update', or 'delete'. */
453
- type: MutationEventType;
454
- /** unique id of the mutation */
455
- mutation_id: RecordId$1<string>;
456
- /** The ID of the record being mutated. */
457
- record_id: RecordId$1<string>;
458
- /** The data payload for create/update operations. */
459
- data?: any;
460
- /** The full record data (optional context). */
461
- record?: any;
462
- /** Options for the mutation event (e.g., debounce settings). */
463
- options?: PushEventOptions;
464
- /** Timestamp when the event was created. */
465
- createdAt: Date;
959
+ type LeaderToFollowerMessage = {
960
+ type: 'db-ready';
961
+ leadershipId: number;
962
+ bucketId: string;
963
+ storageHealth: StorageHealth;
466
964
  }
467
- /**
468
- * Options for run operations.
469
- */
470
- interface RunOptions {
471
- assignedTo?: string;
472
- max_retries?: number;
473
- retry_strategy?: 'linear' | 'exponential';
965
+ /** Every ingest the leader's CacheModule committed, so follower circuits
966
+ * stay live without their own fetch. seq detects gaps. */ | {
967
+ type: 'ingest-relay';
968
+ tuples: IngestTuple[];
969
+ leadershipId: number;
970
+ seq: number;
971
+ }
972
+ /** A `_00_list_ref` LIVE event, relayed verbatim. Each follower resolves the
973
+ * queryId against its own DataModule and ignores foreign queries. */ | {
974
+ type: 'list-ref-change';
975
+ action: 'CREATE' | 'UPDATE' | 'DELETE';
976
+ queryId: string;
977
+ recordId: string;
978
+ version: number;
979
+ parent: boolean;
474
980
  }
981
+ /** The leader's drain rolled back a mutation owned by this tab. */ | {
982
+ type: 'mutation-rolled-back';
983
+ mutationId: string;
984
+ recordId: string;
985
+ eventType: 'create' | 'update' | 'delete';
986
+ error: string;
987
+ };
988
+ //#endregion
989
+ //#region src/services/tabs/coordinator.d.ts
990
+ /** Leader-side fan-out surface handed to the sync layer. The sync router
991
+ * (modules/sync/tab-router.ts) registers itself as the message handler. */
992
+ declare class LeaderSyncHub {
993
+ readonly leadershipId: number;
994
+ private logger;
995
+ private followers;
996
+ private seq;
997
+ onFollowerMessage: ((tabId: TabId, msg: FollowerToLeaderMessage) => void) | null;
998
+ onFollowerDetached: ((tabId: TabId) => void) | null;
999
+ constructor(leadershipId: number, logger: Logger$1);
1000
+ attach(tabId: TabId, port: MessagePort): void;
1001
+ detach(tabId: TabId): void;
1002
+ detachAll(): void;
1003
+ sendTo(tabId: TabId, msg: LeaderToFollowerMessage): void;
1004
+ broadcast(msg: LeaderToFollowerMessage, exceptTabId?: TabId): void;
1005
+ /** Stamped ingest relay; seq lets followers detect gaps. */
1006
+ relayIngest(tuples: IngestTuple[], exceptTabId?: TabId): void;
1007
+ get followerCount(): number;
1008
+ get relayedBatches(): number;
1009
+ }
1010
+ /** Follower half of the syncPort. Queues while detached (leaderless window)
1011
+ * and flushes on rebind; a lost-in-flight mutation notify is additionally
1012
+ * backstopped by the new leader reloading the shared outbox from the store. */
1013
+ declare class SyncForwarder {
1014
+ private tabId;
1015
+ private port;
1016
+ private queued;
1017
+ onLeaderMessage: ((msg: LeaderToFollowerMessage) => void) | null;
1018
+ constructor(tabId: TabId);
1019
+ rebind(port: MessagePort): void;
1020
+ unbind(): void;
1021
+ private post;
1022
+ mutationEnqueued(mutationId: string): void;
1023
+ requestPoll(): void;
1024
+ }
1025
+ //#endregion
1026
+ //#region src/modules/sync/sync.d.ts
475
1027
  /**
476
- * Options for update operations.
1028
+ * Tunables for `Sp00kySync` construction.
477
1029
  */
478
- interface UpdateOptions {
1030
+ interface Sp00kySyncOptions {
1031
+ /**
1032
+ * Cadence (ms) for the `_00_list_ref` poll fallback that catches
1033
+ * cross-session UPDATEs the LIVE-permission gap drops. Non-positive
1034
+ * values fall back to the default; see
1035
+ * {@link resolveListRefPollInterval}.
1036
+ */
1037
+ refSyncIntervalMs?: number;
1038
+ /**
1039
+ * Enable realtime sync for unauthenticated clients against the shared
1040
+ * `_00_list_ref_anon` table. See {@link Sp00kyConfig.enableAnonymousLiveQueries}.
1041
+ * Defaults to `false`.
1042
+ */
1043
+ anonymousLiveQueries?: boolean;
479
1044
  /**
480
- * Debounce configuration for the update.
481
- * If boolean, enables default debounce behavior.
1045
+ * Consecutive failed sync rounds before sync health flips to `degraded`.
1046
+ * `0` disables degraded reporting. See {@link Sp00kyConfig.syncHealth}.
1047
+ * Defaults to `3`.
482
1048
  */
483
- debounced?: boolean | DebounceOptions;
1049
+ degradeAfterConsecutiveFailures?: number;
1050
+ /**
1051
+ * Max time a single mutation push may take before it is treated as a network
1052
+ * failure and retried. Guards against an RPC that never settles wedging the
1053
+ * up-queue for the session. Defaults to 30000; `0` disables the timeout.
1054
+ */
1055
+ pushTimeoutMs?: number;
1056
+ /**
1057
+ * Transport supervisor. Sync reads its state to report `connection` in
1058
+ * {@link SyncHealth} so a UI can show "reconnecting…" the instant the socket
1059
+ * drops, without waiting for the degrade threshold. Optional: omitted in
1060
+ * tests, where `connection` then reports `connected`.
1061
+ */
1062
+ connectionSupervisor?: ConnectionSupervisor;
484
1063
  }
485
1064
  /**
486
- * Configuration options for debouncing updates.
1065
+ * The main synchronization engine for Sp00ky.
1066
+ * Handles the bidirectional synchronization between the local database and the remote backend.
1067
+ * Uses a queue-based architecture with 'up' (local to remote) and 'down' (remote to local) queues.
1068
+ * @template S The schema structure type.
487
1069
  */
488
- interface DebounceOptions {
1070
+ declare class Sp00kySync<S extends SchemaStructure> {
1071
+ private local;
1072
+ private remote;
1073
+ private cache;
1074
+ private dataModule;
1075
+ private schema;
1076
+ private upQueue;
1077
+ private downQueue;
1078
+ private isInit;
1079
+ private logger;
1080
+ private syncEngine;
1081
+ /** Engine-level events (e.g. `SYNC_REMOTE_DATA_INGESTED`). Distinct
1082
+ * from `this.events`, which carries Sp00kySync-level events like
1083
+ * `SYNC_QUERY_UPDATED` and `SYNC_MUTATION_ROLLED_BACK`. */
1084
+ get engineEvents(): SyncEventSystem;
1085
+ private scheduler;
1086
+ /**
1087
+ * Set by any event that means the socket we registered on is gone, so the
1088
+ * next `connected` knows it must re-subscribe rather than treat itself as the
1089
+ * initial connect. See {@link subscribeToReconnect}.
1090
+ */
1091
+ private needsResubscribe;
1092
+ /** When the last reconnect-driven full refetch ran, for burst coalescing. */
1093
+ private lastReconnectRefetchAt;
1094
+ /**
1095
+ * Minimum gap between reconnect-driven full refetches. Long enough to absorb
1096
+ * a flapping socket (the SDK reconnect ladder starts at 1s), short enough
1097
+ * that a genuine drop minutes later still refetches.
1098
+ */
1099
+ private static readonly RECONNECT_REFETCH_COOLDOWN_MS;
1100
+ events: SyncEventSystem;
1101
+ private currentUserId;
1102
+ private tabRole;
1103
+ private tabId;
1104
+ private hub;
1105
+ private forwarder;
1106
+ private refMode;
1107
+ private readonly anonLiveEnabled;
1108
+ private currentLiveQueryUuid;
1109
+ private liveQueryUnsubscribe;
1110
+ private listRefPollTimer;
1111
+ private listRefPollRunning;
1112
+ private listRefPollInFlight;
1113
+ readonly refSyncIntervalMs: number;
1114
+ private listRefIdleStreak;
1115
+ private stillRemoteStreaks;
1116
+ private lastLiveEventAt;
1117
+ private _liveRetryCount;
1118
+ get liveRetryCount(): number;
1119
+ get isSyncing(): boolean;
1120
+ get pendingMutationCount(): number;
1121
+ subscribeToPendingMutations(cb: (count: number) => void): () => void;
1122
+ private readonly degradeAfterFailures;
1123
+ /** Per-push RPC deadline; see {@link withPushTimeout}. */
1124
+ private readonly pushTimeoutMs;
1125
+ private consecutiveSyncFailures;
1126
+ private syncHealthStatus;
1127
+ private lastSyncErrorKind;
1128
+ private lastSyncErrorMessage;
1129
+ private hasSyncedOnce;
1130
+ private selfHealTimer;
1131
+ private selfHealAttempts;
1132
+ private static readonly SELF_HEAL_BASE_MS;
1133
+ private static readonly SELF_HEAL_MAX_MS;
1134
+ /**
1135
+ * Transport supervisor, when one was supplied. Sync only reads state from it;
1136
+ * it never drives reconnects itself.
1137
+ */
1138
+ private readonly connectionSupervisor?;
1139
+ /**
1140
+ * Mirror of the supervisor's state. Defaults to `connected` so a client
1141
+ * constructed without a supervisor (tests, embedders) reports the same health
1142
+ * shape it always has rather than a permanent false "disconnected".
1143
+ */
1144
+ private connectionState;
1145
+ /** Current sync-health snapshot. */
1146
+ get syncHealth(): SyncHealth;
1147
+ /**
1148
+ * Observe sync health. The callback fires immediately with the current
1149
+ * status and again on every healthy↔degraded transition. Returns an
1150
+ * unsubscribe. Mirrors {@link subscribeToPendingMutations}.
1151
+ */
1152
+ subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void;
1153
+ private emitSyncHealth;
1154
+ /**
1155
+ * Mirror the supervisor's transport state into {@link SyncHealth} and emit on
1156
+ * every change, so a UI can react to a dropped socket immediately instead of
1157
+ * waiting for `degradeAfterFailures` failed rounds. `status` is untouched:
1158
+ * a brief reconnect is not a degradation.
1159
+ *
1160
+ * No explicit unsubscribe: the supervisor is owned by the same client and
1161
+ * drops all subscribers in its own `dispose()`, which `Sp00kyClient.close()`
1162
+ * calls first.
1163
+ */
1164
+ private subscribeToConnectionState;
1165
+ /**
1166
+ * Fed by the scheduler once per drained sync round. Individual failures are
1167
+ * absorbed by the queue's retry; only a run of `degradeAfterFailures`
1168
+ * consecutive failures flips the status to `degraded`, and the next clean
1169
+ * round flips it back. No-op when reporting is disabled (`degradeAfterFailures`
1170
+ * is 0).
1171
+ */
1172
+ private recordSyncOutcome;
1173
+ /**
1174
+ * Begin self-heal retries (no-op if already running). Started on the
1175
+ * healthy→degraded transition; {@link recordSyncOutcome} stops it on recovery.
1176
+ */
1177
+ private startSelfHeal;
1178
+ private scheduleSelfHeal;
1179
+ private stopSelfHeal;
1180
+ constructor(local: LocalStore, remote: RemoteDatabaseService, cache: CacheModule, dataModule: DataModule<S>, schema: S, logger: Logger$1, options?: Sp00kySyncOptions);
1181
+ /**
1182
+ * Initializes the synchronization system.
1183
+ * Starts the scheduler and initiates the initial sync cycles.
1184
+ * @throws Error if already initialized.
1185
+ */
1186
+ init(): Promise<void>;
1187
+ /** Set BEFORE init(): shapes what init boots (a follower loads no outbox and
1188
+ * never starts LIVE; its own registration/poll paths stay untouched). */
1189
+ setTabContext(role: 'solo' | 'leader' | 'follower', tabId: string | null): void;
1190
+ /** Leader duties: drain the shared outbox, own the single list_ref LIVE,
1191
+ * relay LIVE events and rollbacks to followers via `hub`. Idempotent for a
1192
+ * boot-time leader; a runtime promotion (failover) reloads the outbox,
1193
+ * which now holds EVERY tab's rows, and restarts LIVE under this session. */
1194
+ promoteToLeader(hub: LeaderSyncHub): Promise<void>;
1195
+ /** Follower duties: no outbox drain, no LIVE. Mutations forward to the
1196
+ * leader; everything else (registration, per-query sync, poll) runs
1197
+ * against this tab's own remote session as usual. */
1198
+ demoteToFollower(forwarder: SyncForwarder): void;
1199
+ /**
1200
+ * A pending mutation was discarded because it can never be sent.
1201
+ *
1202
+ * This is a lost write, so it must not stay invisible. Every failure in this
1203
+ * chain used to be a `logger.error` an app running `logLevel: 'fatal'` never
1204
+ * shows, which is how an outbox could sit undrained for hours with the UI
1205
+ * reporting nothing. Surfaces as a rollback event (the mutation will never
1206
+ * apply, which is what a subscriber needs to know) and degrades sync health.
1207
+ */
1208
+ private onMutationDropped;
1209
+ /** A forwarded outbox row from a follower: load + drain it. Idempotent. */
1210
+ enqueueForwardedMutation(mutationId: string): Promise<void>;
1211
+ /** A relayed `_00_list_ref` LIVE event: resolve against THIS tab's queries
1212
+ * and run the exact same handling the LIVE subscription would have. */
1213
+ private applyRelayedListRefChange;
1214
+ /** One immediate poll cycle (failover convergence). */
1215
+ forcePollRound(): Promise<void>;
1216
+ /**
1217
+ * Quiesce all sync activity ahead of a local-bucket switch. After this
1218
+ * resolves, nothing in the sync module writes to the local store: the poll
1219
+ * loop is stopped AND its in-flight tick awaited, LIVE is killed, debounce
1220
+ * timers are cancelled (their outbox rows are already persisted), and the
1221
+ * scheduler has drained its in-flight queue item — including that item's
1222
+ * outbox-row delete, which must land in the OLD bucket. Queued down-events
1223
+ * are dropped (they reference old-bucket query rows; the post-switch rebind
1224
+ * re-enqueues registrations). The old user's un-pushed outbox is deliberately
1225
+ * NOT drained: the remote session already belongs to the next user.
1226
+ */
1227
+ prepareBucketSwitch(): Promise<void>;
1228
+ /**
1229
+ * Resume syncing against the freshly-opened bucket: reload the mutation
1230
+ * outbox from ITS `_00_pending_mutations` (the new user's own un-pushed
1231
+ * offline work) and restart the scheduler. LIVE + the list_ref poll restart
1232
+ * via the `setCurrentUserId` call that follows in the auth listener.
1233
+ */
1234
+ completeBucketSwitch(): Promise<void>;
1235
+ /**
1236
+ * Push the authenticated user's record id from the parent client's
1237
+ * auth subscription. Tears down the existing `_00_list_ref` LIVE (if
1238
+ * any) and re-registers it under the new user's dedicated table so
1239
+ * SurrealDB binds the permission rule under the post-flip auth
1240
+ * context. Pass `null` on sign-out.
1241
+ *
1242
+ * The dedicated `_00_list_ref_user_<id>` table is created lazily by
1243
+ * the SSP when the first query registration arrives, which may be
1244
+ * concurrent with this call. We retry the LIVE registration with a
1245
+ * short backoff so a "table not found" race resolves without
1246
+ * surfacing as a permanent auth-loading hang.
1247
+ */
1248
+ setCurrentUserId(userId: string | null): Promise<void>;
1249
+ private startListRefPoll;
1250
+ private stopListRefPoll;
1251
+ /**
1252
+ * One poll cycle: refetch `_00_list_ref` for every active query. Returns
1253
+ * whether ANY query's remoteArray actually changed — the scheduler uses this
1254
+ * to drive the adaptive idle backoff.
1255
+ *
1256
+ * Also the ONLY health signal that runs while the page is idle. Sync health is
1257
+ * otherwise activity-driven (mutations/registrations via the scheduler,
1258
+ * reconnect re-registration, self-heal), so on a quiet page a stale `degraded`
1259
+ * would linger until the next mutation and a genuine idle drop would be
1260
+ * invisible. We fold the cycle's aggregate reachability into `recordSyncOutcome`
1261
+ * so idle health self-recovers (and self-degrades) with no user action. A clean
1262
+ * cycle is idempotent when already healthy (`recordSyncOutcome` early-returns at
1263
+ * `consecutiveSyncFailures === 0`), so a healthy idle page pays nothing.
1264
+ */
1265
+ private pollListRefForActiveQueries;
1266
+ /**
1267
+ * Pull the upstream list_ref entries for `queryHash`, diff them
1268
+ * against the local `remoteArray` cache, sync any added/updated rows
1269
+ * through the SyncEngine, then persist the new remoteArray. This is
1270
+ * the same shape `createRemoteQuery` does for its initial fetch and
1271
+ * what `handleRemoteListRefChange` does per-LIVE-event — we reuse
1272
+ * it on a timer as a fallback for missed LIVE notifications.
1273
+ */
1274
+ private refetchListRefForQuery;
1275
+ /**
1276
+ * Resolve the current `_00_list_ref` table name for the active auth
1277
+ * context. Public so the `createRemoteQuery` initial-fetch path can
1278
+ * read from the right per-user table.
1279
+ *
1280
+ * Reads the user id from `DataModule` rather than the local mirror,
1281
+ * because `DataModule.setCurrentUserId` runs synchronously from the
1282
+ * auth callback (before any `await`), whereas `sync.setCurrentUserId`
1283
+ * is async — the userQuery's initial fetch can fire between those
1284
+ * two points and we need the correct table name immediately.
1285
+ */
1286
+ listRefTable(): string;
1287
+ private killRefLiveQuery;
1288
+ private restartRefLiveQuery;
1289
+ /**
1290
+ * Drop local LIVE bookkeeping without issuing a `KILL`.
1291
+ *
1292
+ * Called when the socket dies. The server-side subscription is scoped to that
1293
+ * WebSocket session and died with it, so there is nothing left to kill — and
1294
+ * by the time the reconnect handler runs, the client reports `connected`
1295
+ * again, which would otherwise send a `KILL` for a stale uuid on the *new*
1296
+ * session and hold up the restart queued behind it.
1297
+ */
1298
+ private invalidateRefLiveQuery;
1299
+ private subscribeToReconnect;
1300
+ private startRefLiveQueries;
1301
+ private handleRemoteListRefChange;
489
1302
  /**
490
- * The key to use for debouncing.
491
- * - 'recordId': Debounce based on the specific record ID. WARNING: IT WILL ONLY ACCEPT THE LATEST CHANGE AND DOES *NOT* MERGE THE PREVIOUS ONCES. IF YOU ARE UNSURE JUST USE 'recordId_x_fields'.
492
- * - 'recordId_x_fields': Debounce based on record ID and specific fields.
1303
+ * Handle a LIVE change to a SUBQUERY child edge (a `_00_list_ref` row with
1304
+ * `parent` set) for a `.related()` query. Unlike primary rows, child rows
1305
+ * must NOT touch the query's `localArray`/`remoteArray`/`rowCount`; we only
1306
+ * keep the child BODY fresh in the local cache so the in-browser SSP's
1307
+ * subquery-table dependency re-materializes the parent view.
1308
+ *
1309
+ * CREATE/UPDATE fetch+upsert the child body. DELETE is intentionally a
1310
+ * no-op: a child leaving this query's set must not delete a body another
1311
+ * query may still show (see `syncSubqueryChildren` deletion-safety note);
1312
+ * a genuine record delete propagates via the normal delete path.
493
1313
  */
494
- key?: 'recordId' | 'recordId_x_fields';
495
- /** The debounce delay in milliseconds. */
496
- delay?: number;
1314
+ private handleRemoteSubqueryChange;
1315
+ /**
1316
+ * Enqueues a 'down' event (from remote to local) for processing.
1317
+ * @param event The DownEvent to enqueue.
1318
+ */
1319
+ enqueueDownEvent(event: DownEvent): void;
1320
+ /**
1321
+ * Bound a mutation push so it always settles.
1322
+ *
1323
+ * `SyncScheduler.syncUp` early-returns while `isSyncingUp` is true, and that
1324
+ * flag only clears in the `finally` of the drain loop. A push whose RPC never
1325
+ * settles (socket dropped mid-flight, response lost) therefore wedges the
1326
+ * up-queue for the rest of the session: no retry, no error, no further
1327
+ * mutation ever sent. A timeout turns that into an ordinary network failure,
1328
+ * which `UpQueue.next` re-queues for the next trigger. The message deliberately
1329
+ * contains "timed out" so `classifySyncError` treats it as `network` and
1330
+ * retries rather than rolling the mutation back.
1331
+ */
1332
+ private withPushTimeout;
1333
+ private processUpEvent;
1334
+ private handleRollback;
1335
+ private processDownEvent;
1336
+ /**
1337
+ * Synchronizes a specific query by hash.
1338
+ * Compares local and remote version arrays and fetches differences.
1339
+ * @param hash The hash of the query to sync.
1340
+ */
1341
+ syncQuery(hash: string): Promise<void>;
1342
+ /**
1343
+ * Run a sync for a single query while reflecting its fetch status. Marks the
1344
+ * query `fetching` for the duration when the diff actually pulls records
1345
+ * (added/updated), then resets to `idle` in a `finally` so a failed sync
1346
+ * never leaves a query stuck `fetching`. Part A's notification coalescing
1347
+ * means the single resulting UI update lands after this completes.
1348
+ */
1349
+ private runSyncForQuery;
1350
+ /**
1351
+ * Record ids with a pending local DELETE in the outbox (`_00_pending_mutations`).
1352
+ * Sync must not re-fetch/re-insert these — the remote delete is async, so the
1353
+ * server's `_00_list_ref` still lists them until it's processed, and the diff
1354
+ * would otherwise resurrect a just-deleted record.
1355
+ */
1356
+ private getPendingDeleteIds;
1357
+ /**
1358
+ * Enqueues a list of mutations (up events) to be sent to the remote.
1359
+ * @param mutations Array of UpEvents (create/update/delete) to enqueue.
1360
+ */
1361
+ enqueueMutation(mutations: UpEvent[]): Promise<void>;
1362
+ private registerQuery;
1363
+ private createRemoteQuery;
1364
+ /**
1365
+ * Sync the BODIES of a `.related()` query's subquery child rows into the
1366
+ * local cache, separately from the primary window array. The SSP writes
1367
+ * each matched child as a `_00_list_ref` edge tagged `parent`/`parent_rel`;
1368
+ * `buildSubqueryListRefSelect` pulls those `out`+`version` pairs (any
1369
+ * nesting depth). We diff against the in-memory `subqueryRemoteArray` and
1370
+ * fetch added/updated bodies through the SyncEngine — which `saveBatch`s
1371
+ * them into the local DB AND the in-browser SSP, whose subquery-table
1372
+ * dependency then re-materializes the parent view (no explicit notify).
1373
+ *
1374
+ * Deletion safety: we pass `removed: []` deliberately. A child body can be
1375
+ * shared by other queries; letting `handleRemovedRecords` delete one that
1376
+ * merely left THIS query's child set would clobber data another query still
1377
+ * shows. Genuine record deletes flow through the normal delete path; a
1378
+ * lingering orphan body is invisible (the correlated WHERE stops matching).
1379
+ *
1380
+ * Kept off `runSyncForQuery` on purpose so child fetches never flip the
1381
+ * query to `fetching` or skew its DevTools timings.
1382
+ */
1383
+ private syncSubqueryChildren;
1384
+ heartbeatQuery(queryHash: string): Promise<void>;
1385
+ private cleanupQuery;
497
1386
  }
498
1387
  //#endregion
499
1388
  //#region src/modules/auth/events/index.d.ts
@@ -518,6 +1407,13 @@ declare class AuthService<S extends SchemaStructure> {
518
1407
  token: string | null;
519
1408
  currentUser: any | null;
520
1409
  isAuthenticated: boolean;
1410
+ /**
1411
+ * The record-access method name for the current session (e.g. `"account"`),
1412
+ * derived from the token's `AC` claim. Consumed by the in-browser SSP's
1413
+ * permission injection so `$access`-gated table predicates resolve locally,
1414
+ * mirroring the server's `$access`. Null when logged out.
1415
+ */
1416
+ access: string | null;
521
1417
  isLoading: boolean;
522
1418
  private events;
523
1419
  get eventSystem(): AuthEventSystem;
@@ -539,17 +1435,617 @@ declare class AuthService<S extends SchemaStructure> {
539
1435
  */
540
1436
  signOut(): Promise<void>;
541
1437
  private setSession;
1438
+ /** Fallback when the token carries no `AC` claim: if the schema defines
1439
+ * exactly one record-access method, assume the session used it. */
1440
+ private defaultAccessName;
542
1441
  signUp<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signup'>): Promise<void>;
543
1442
  signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
544
1443
  }
545
1444
  //#endregion
546
- //#region src/spooky.d.ts
1445
+ //#region src/modules/crdt/crdt-field.d.ts
1446
+ declare const CURSOR_COLORS: string[];
1447
+ declare function cursorColorFromName(name: string): string;
1448
+ declare class CrdtField {
1449
+ private fieldName;
1450
+ private doc;
1451
+ private pushTimer;
1452
+ private local;
1453
+ private remote;
1454
+ private recordId;
1455
+ private sessionId;
1456
+ private unsubscribe;
1457
+ private lastPushTime;
1458
+ private lastCursorPushTime;
1459
+ private loadedFromCrdt;
1460
+ private pushRetryCount;
1461
+ private logger;
1462
+ private cursorsEnabled;
1463
+ /** Remote-push debounce. Local writes happen immediately on every Loro
1464
+ * update; the remote UPSERT is coalesced over this window. Configured
1465
+ * via `Sp00kyConfig.crdtDebounceMs`, default 500. */
1466
+ private remoteDebounceMs;
1467
+ private _onCursorUpdate;
1468
+ private pendingCursorUpdate;
1469
+ /** Callback set by the editor to receive remote cursor updates.
1470
+ * Any cursor data that arrived before this callback was set will be replayed. */
1471
+ set onCursorUpdate(cb: ((data: Uint8Array) => void) | null);
1472
+ get onCursorUpdate(): ((data: Uint8Array) => void) | null;
1473
+ /**
1474
+ * @param LoroDocClass the `LoroDoc` constructor, injected by the caller after
1475
+ * awaiting {@link loadLoro} — keeps `loro-crdt` out of this module's static
1476
+ * import graph so it only ships to apps that use CRDT fields.
1477
+ */
1478
+ constructor(fieldName: string, cursorsEnabled: boolean, LoroDocClass: typeof LoroDoc, initialState?: Uint8Array, logger?: Logger$1 | null);
1479
+ getDoc(): LoroDoc;
1480
+ /** Whether the LoroDoc was loaded from saved CRDT state */
1481
+ hasContent(): boolean;
1482
+ startSync(local: LocalStore, remote: RemoteDatabaseService, recordId: string, sessionId: string, debounceMs: number): void;
1483
+ /**
1484
+ * Stop syncing this field. Flushes one final remote push by default so the
1485
+ * last keystrokes aren't lost. Pass `{ flush: false }` on a bucket switch —
1486
+ * the remote session already belongs to the NEXT user, and pushing this
1487
+ * (previous user's) snapshot under it would clobber the record remotely.
1488
+ */
1489
+ stopSync(options?: {
1490
+ flush?: boolean;
1491
+ }): void;
1492
+ importRemote(state: Uint8Array): void;
1493
+ exportSnapshot(): Uint8Array;
1494
+ /** Push this session's cursor blob into the parent row at
1495
+ * `<field>.cursors[$sid]`. No-op when cursors aren't enabled on this
1496
+ * field — the editor still calls this method optimistically, but
1497
+ * without `@cursor` on the schema there's nowhere to store the blob.
1498
+ * The UPDATE itself fires the parent table's LIVE feed, so other
1499
+ * browsers receive the cursor change without a separate `_00_rv` bump. */
1500
+ pushCursorState(encoded: Uint8Array): Promise<void>;
1501
+ /** Import remote cursor state (called by CrdtManager from LIVE SELECT) */
1502
+ importRemoteCursor(base64State: string): void;
1503
+ private scheduleRemotePush;
1504
+ /** SET path inside a parent row for the current snapshot. `@crdt`-only
1505
+ * fields hold the snapshot directly (`<field>`); `@crdt @cursor`
1506
+ * fields hold a `{ state, cursors }` object so the snapshot lives at
1507
+ * `<field>.state` next to per-session cursor blobs. */
1508
+ private statePath;
1509
+ /** Mirror the LoroDoc snapshot into the parent row locally. Runs on
1510
+ * every local update and every remote import so reloads (online or
1511
+ * offline) see the freshest content immediately. Failures are
1512
+ * swallowed — a stale local write must never block user input. */
1513
+ private persistLocal;
1514
+ private pushToRemote;
1515
+ }
1516
+ //#endregion
1517
+ //#region src/modules/crdt/index.d.ts
1518
+ /**
1519
+ * CrdtManager manages active CrdtField instances and their sync channels.
1520
+ *
1521
+ * Collaborative state lives in two dedicated tables (defined in
1522
+ * `apps/cli/src/meta_tables_remote.surql`):
1523
+ * - `_00_crdt` { record_id, field, state } — one row per (record, field)
1524
+ * - `_00_cursor` { record_id, session_id, field, state } — one row per
1525
+ * (record, session, field)
1526
+ *
1527
+ * Splitting them off the parent row is what makes offline edits mergeable:
1528
+ * each (record, field) gets its own row, so concurrent offline writes don't
1529
+ * collide on the parent's last-write-wins semantics.
1530
+ *
1531
+ * Cross-browser delivery still rides the parent table's existing LIVE feed
1532
+ * to avoid SurrealDB v3 LIVE bugs around dereference-based permission rules
1533
+ * (issues 3602, 4026). On every meta UPSERT the writer also bumps the
1534
+ * parent's `_00_rv` (a no-op assignment); that fires the parent's LIVE
1535
+ * feed, and the receiver pulls the matching `_00_crdt` / `_00_cursor` rows
1536
+ * via subquery. Permission inheritance happens server-side via
1537
+ * `record_id.id != NONE` (SELECT) and `fn::can_update_record` (UPDATE).
1538
+ */
1539
+ declare class CrdtManager {
1540
+ private schema;
1541
+ private local;
1542
+ private remote;
1543
+ private debounceMs;
1544
+ private fields;
1545
+ private liveByTable;
1546
+ private pendingLive;
1547
+ private staleTables;
1548
+ private connectionGeneration;
1549
+ private connectionUnsubscribes;
1550
+ private logger;
1551
+ private sessionId;
1552
+ constructor(schema: SchemaStructure, local: LocalStore, remote: RemoteDatabaseService, logger: Logger$1, debounceMs?: number);
1553
+ /**
1554
+ * Re-establish table LIVEs after a socket drop.
1555
+ *
1556
+ * A LIVE subscription lives and dies with its WebSocket session, and
1557
+ * `ensureTableSubscription` is memoized on `liveByTable` — so without this,
1558
+ * the first reconnect leaves CRDT realtime permanently dead: the map still
1559
+ * holds a uuid for a subscription the server has forgotten, so every later
1560
+ * `open()` short-circuits and no LIVE is ever re-issued.
1561
+ *
1562
+ * Both drop events matter: the SDK publishes `reconnecting` (not
1563
+ * `disconnected`) when it intends to recover on its own, and `disconnected`
1564
+ * only once it has given up.
1565
+ */
1566
+ private subscribeToReconnect;
1567
+ /** Stop observing transport events. Separate from {@link closeAll}, which also
1568
+ * runs on a bucket switch where the manager keeps being used. */
1569
+ dispose(): void;
1570
+ private hasOpenFieldFor;
1571
+ /** Set the session id that scopes this client's cursor entries. Must be
1572
+ * called before `open()` for cursors to be pushed under a stable key.
1573
+ * Passed in from `sp00ky.ts` at boot (it already fetches `session::id()`
1574
+ * for the data-module salt). */
1575
+ setSessionId(sessionId: string): void;
1576
+ /**
1577
+ * Open a CRDT field for collaborative editing.
1578
+ *
1579
+ * @param table - Table name
1580
+ * @param recordId - Full record ID (e.g., "thread:abc")
1581
+ * @param field - Field name (e.g., "title", "content")
1582
+ * @param fallbackText - Current plain text from the record, used to seed the
1583
+ * LoroDoc if no CRDT state exists yet (migration path)
1584
+ */
1585
+ open(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
1586
+ close(table: string, recordId: string, field: string): void;
1587
+ /**
1588
+ * Close every open field + table LIVE. Fields flush a final remote push by
1589
+ * default; pass `{ flush: false }` on a bucket switch, where that flush
1590
+ * would push the previous user's snapshot under the next user's session.
1591
+ */
1592
+ closeAll(options?: {
1593
+ flush?: boolean;
1594
+ }): void;
1595
+ /** Ensure a single `LIVE SELECT * FROM <table>` is running, shared across
1596
+ * every open CrdtField on `table`. */
1597
+ private ensureTableSubscription;
1598
+ /** Apply a parent-row payload from a non-LIVE source (e.g. the
1599
+ * list_ref-driven sync engine, when the cross-user LIVE on the
1600
+ * parent table is filtered out by the SurrealDB cross-session
1601
+ * permission gap). Same semantics as the internal `dispatchRow`. */
1602
+ applyRow(table: string, row: Record<string, unknown>): void;
1603
+ /** Dispatch a parent-row LIVE event to every open CrdtField on that
1604
+ * record. Each open field reads its slice of the row directly — the
1605
+ * CRDT snapshot is a column on the parent now, so there is no
1606
+ * follow-up subquery. */
1607
+ private dispatchRow;
1608
+ /** One-shot remote fetch for a row whose CRDT field hasn't synced
1609
+ * locally yet (fresh device, memory-backed local DB after reload, …).
1610
+ * Used by `open()` when the local read came up empty. Subsequent
1611
+ * cross-browser updates ride `dispatchRow` via the parent LIVE feed. */
1612
+ private fetchAndDispatchRow;
1613
+ /** Schema lookup: does `<table>.<field>` carry a `@cursor` annotation?
1614
+ * Determines the on-disk shape (plain snapshot vs. `{ state, cursors }`). */
1615
+ private fieldHasCursor;
1616
+ /** Pull the LoroDoc snapshot bytes out of a row slice. For `@crdt`-only
1617
+ * the slice IS the snapshot (Uint8Array); for `@crdt @cursor` it's
1618
+ * `{ state, cursors }` where `state` carries the snapshot bytes. */
1619
+ private extractSnapshot;
1620
+ private killTableSubscription;
1621
+ private makeKey;
1622
+ /**
1623
+ * Throws if `<table>.<field>` is not annotated `@crdt` in the schema. Catches
1624
+ * typos, removed annotations, and stale schema codegen at the call site instead
1625
+ * of silently producing a non-CRDT writer.
1626
+ */
1627
+ private assertCrdtField;
1628
+ }
1629
+ //#endregion
1630
+ //#region src/modules/feature-flag/index.d.ts
1631
+ interface FeatureFlagSnapshot {
1632
+ variant: string | undefined;
1633
+ payload: unknown | undefined;
1634
+ }
1635
+ interface FeatureFlagOptions {
1636
+ fallback?: string;
1637
+ ttl?: QueryTimeToLive;
1638
+ }
1639
+ /**
1640
+ * A locally forced variant. Applies to THIS browser only and is never sent to
1641
+ * the server — the assignment in `_00_user_feature` is untouched, so clearing
1642
+ * the override restores whatever the server says.
1643
+ */
1644
+ interface FeatureFlagOverride {
1645
+ variant: string;
1646
+ payload?: unknown;
1647
+ }
1648
+ declare class FeatureFlagHandle {
1649
+ readonly key: string;
1650
+ readonly fallback: string | undefined;
1651
+ private latest;
1652
+ private listeners;
1653
+ private unsubscribeFn;
1654
+ private onCloseFn;
1655
+ private closed;
1656
+ constructor(key: string, fallback: string | undefined);
1657
+ attach(unsubscribe: () => void): void;
1658
+ detach(): void;
1659
+ set(snapshot: FeatureFlagSnapshot): void;
1660
+ variant(): string | undefined;
1661
+ payload<T = unknown>(): T | undefined;
1662
+ enabled(): boolean;
1663
+ subscribe(cb: (s: FeatureFlagSnapshot) => void): () => void;
1664
+ onClose(cb: () => void): void;
1665
+ close(): void;
1666
+ }
1667
+ interface FeatureFlagModuleDeps<S extends SchemaStructure> {
1668
+ dataModule: DataModule<S>;
1669
+ sync: Sp00kySync<S>;
1670
+ auth: AuthService<S>;
1671
+ logger: Logger$1;
1672
+ }
1673
+ declare class FeatureFlagModule<S extends SchemaStructure> {
1674
+ private deps;
1675
+ private logger;
1676
+ private handles;
1677
+ private authUnsubscribe;
1678
+ private lastUserId;
1679
+ private querySubscription;
1680
+ private starting;
1681
+ private ttl;
1682
+ private snapshots;
1683
+ private loaded;
1684
+ private overrides;
1685
+ constructor(deps: FeatureFlagModuleDeps<S>);
1686
+ init(): void;
1687
+ feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
1688
+ closeAll(): Promise<void>;
1689
+ /** Auth changed: drop the old user's query/snapshots and re-observe. */
1690
+ private refresh;
1691
+ private teardownQuery;
1692
+ /** Start the single shared live query (idempotent; no-op with no handles). */
1693
+ private ensureStarted;
1694
+ /** Live query result → per-key snapshots → push to every active handle. */
1695
+ private applyRecords;
1696
+ /**
1697
+ * Force `key` to `variant` in THIS browser. Pass `null` to clear.
1698
+ *
1699
+ * Nothing is written to the server: the `_00_user_feature` assignment is
1700
+ * untouched, so clearing restores whatever the server says. Persisted to
1701
+ * localStorage on the page origin, so it survives a reload.
1702
+ */
1703
+ setLocalOverride(key: string, variant: string | null, payload?: unknown): void;
1704
+ clearLocalOverrides(): void;
1705
+ getLocalOverrides(): Record<string, FeatureFlagOverride>;
1706
+ /** The assignment for `key`, with any local override taking precedence. */
1707
+ private resolve;
1708
+ private pushAll;
1709
+ private loadOverrides;
1710
+ private persistOverrides;
1711
+ }
1712
+ //#endregion
1713
+ //#region src/modules/app-release/index.d.ts
1714
+ interface AppReleaseSnapshot {
1715
+ /** Latest announced version for the app, or undefined when no row exists. */
1716
+ version: string | undefined;
1717
+ /** Clients should clear SW/caches when reloading onto this version. */
1718
+ cacheBust: boolean;
1719
+ /** Clients should reload/update immediately instead of asking. */
1720
+ mandatory: boolean;
1721
+ releasedAt: string | undefined;
1722
+ }
1723
+ interface AppReleaseOptions {
1724
+ ttl?: QueryTimeToLive;
1725
+ }
1726
+ declare class AppReleaseHandle {
1727
+ readonly app: string;
1728
+ private latest;
1729
+ private listeners;
1730
+ private onCloseFn;
1731
+ private closed;
1732
+ constructor(app: string);
1733
+ set(snapshot: AppReleaseSnapshot): void;
1734
+ snapshot(): AppReleaseSnapshot;
1735
+ version(): string | undefined;
1736
+ /** True when the announced version is semver-newer than `currentVersion`. */
1737
+ updateAvailable(currentVersion: string): boolean;
1738
+ subscribe(cb: (s: AppReleaseSnapshot) => void): () => void;
1739
+ onClose(cb: () => void): void;
1740
+ close(): void;
1741
+ }
1742
+ interface AppReleaseModuleDeps<S extends SchemaStructure> {
1743
+ dataModule: DataModule<S>;
1744
+ sync: Sp00kySync<S>;
1745
+ auth: AuthService<S>;
1746
+ logger: Logger$1;
1747
+ }
1748
+ declare class AppReleaseModule<S extends SchemaStructure> {
1749
+ private deps;
1750
+ private logger;
1751
+ private handles;
1752
+ private authUnsubscribe;
1753
+ private lastUserId;
1754
+ private querySubscription;
1755
+ private starting;
1756
+ private ttl;
1757
+ private snapshots;
1758
+ private loaded;
1759
+ constructor(deps: AppReleaseModuleDeps<S>);
1760
+ init(): void;
1761
+ release(app: string, options?: AppReleaseOptions): AppReleaseHandle;
1762
+ closeAll(): Promise<void>;
1763
+ private refresh;
1764
+ private teardownQuery;
1765
+ private ensureStarted;
1766
+ private applyRecords;
1767
+ }
1768
+ //#endregion
1769
+ //#region src/services/blobs/blob-store.d.ts
1770
+ /**
1771
+ * Byte storage for cached bucket files.
1772
+ *
1773
+ * The default implementation is OPFS. Bucket files never arrive over HTTP in
1774
+ * this client — `BucketHandle.get()` is a SurrealQL RPC on the sync socket — so
1775
+ * neither the browser's HTTP cache nor the Cache API can hold them. We persist
1776
+ * the bytes ourselves, and OPFS is the cheapest place to put them: a read is
1777
+ * `getFile()` → a disk-backed lazy `File` that `URL.createObjectURL` can serve
1778
+ * without ever moving the bytes through the JS heap.
1779
+ *
1780
+ * Layout is real nested directories rather than one hashed filename:
1781
+ *
1782
+ * sp00ky-blobs/<namespace>/<bucket>/<...path segments>
1783
+ *
1784
+ * That costs a `getDirectoryHandle` per segment on write, and buys the property
1785
+ * the whole orphan story rests on: the full `(bucket, path)` key is recoverable
1786
+ * from a directory walk alone. The `_00_blob` manifest can therefore be wiped
1787
+ * (memory fallback, SQLite pool wipe, IndexedDB corruption recovery) and be
1788
+ * rebuilt from disk instead of taking the cached bytes down with it.
1789
+ */
1790
+ /** Identifies one cached file: the bucket it lives in and its path within. */
1791
+ interface BlobKey {
1792
+ bucket: string;
1793
+ path: string;
1794
+ }
1795
+ /** What a directory walk can tell us about a stored file, with no manifest. */
1796
+ interface BlobStat {
1797
+ key: BlobKey;
1798
+ size: number;
1799
+ /** File mtime. Seeds `lastAccess` when a manifest row has to be rebuilt. */
1800
+ mtime: number;
1801
+ }
1802
+ interface BlobStore {
1803
+ /** False for {@link MemoryBlobStore} and for OPFS-less environments: the
1804
+ * cache still dedupes and serves within a tab, but nothing survives reload. */
1805
+ readonly persistent: boolean;
1806
+ /** Namespace (the local bucketId) all keys are resolved under. */
1807
+ readonly namespace: string;
1808
+ read(key: BlobKey): Promise<Blob | null>;
1809
+ /** Returns the number of bytes written. Throws on quota exhaustion. */
1810
+ write(key: BlobKey, bytes: Blob): Promise<number>;
1811
+ remove(key: BlobKey): Promise<void>;
1812
+ /** Every committed file under the current namespace. Sweeps torn writes. */
1813
+ list(): Promise<BlobStat[]>;
1814
+ /** Drop the whole namespace (sign-out with `clearOnSignOut`, or a reset). */
1815
+ clear(): Promise<void>;
1816
+ /** Point at another namespace. Does not touch the bytes of the old one. */
1817
+ setNamespace(namespace: string): void;
1818
+ }
1819
+ //#endregion
1820
+ //#region src/services/blobs/blob-manifest.d.ts
1821
+ interface BlobEntry {
1822
+ /** `${bucket}/${path}` — also the `_00_blob` row id. */
1823
+ id: string;
1824
+ bucket: string;
1825
+ path: string;
1826
+ size: number;
1827
+ contentType: string;
1828
+ createdAt: number;
1829
+ lastAccess: number;
1830
+ hits: number;
1831
+ /** Exempt from pressure eviction. Never expires on its own. */
1832
+ pinned: boolean;
1833
+ }
1834
+ declare class BlobManifest {
1835
+ private local;
1836
+ private entries;
1837
+ /** Ids whose in-memory state has not been written back yet. */
1838
+ private dirty;
1839
+ private removed;
1840
+ private flushing;
1841
+ constructor(local: LocalStore);
1842
+ /**
1843
+ * Hydrate from the rows matching `keys`. Ids come from the OPFS listing, so
1844
+ * this never needs a full-table scan (and therefore never needs a QueryPlan).
1845
+ * Any read failure yields an empty manifest: reconcile then rebuilds every
1846
+ * row from disk, which is exactly the desired degradation.
1847
+ */
1848
+ load(ids: string[]): Promise<void>;
1849
+ get(key: BlobKey): BlobEntry | undefined;
1850
+ getById(id: string): BlobEntry | undefined;
1851
+ all(): BlobEntry[];
1852
+ totalBytes(): number;
1853
+ pinnedBytes(): number;
1854
+ put(entry: BlobEntry): void;
1855
+ touch(id: string, now: number): void;
1856
+ setPinned(id: string, pinned: boolean): boolean;
1857
+ remove(id: string): void;
1858
+ /** Forget everything without scheduling deletes — for a bucket switch, where
1859
+ * the rows belong to the store we are leaving and must stay put. */
1860
+ reset(): void;
1861
+ hasPendingWrites(): boolean;
1862
+ /**
1863
+ * Write back pending changes. Serialized: a second concurrent flush awaits
1864
+ * the first rather than racing it into the same rows. Failures are swallowed
1865
+ * on purpose — a lost metadata write costs an LRU timestamp, and the entry is
1866
+ * rebuilt from disk on the next reconcile.
1867
+ */
1868
+ flush(): Promise<void>;
1869
+ private doFlush;
1870
+ }
1871
+ //#endregion
1872
+ //#region src/services/blobs/blob-cache.d.ts
1873
+ interface BlobUrlLease {
1874
+ url: string;
1875
+ release(): void;
1876
+ }
1877
+ interface BlobReadOptions {
1878
+ /** Write through to L1 on a miss. Default true. */
1879
+ persist?: boolean;
1880
+ /** Mark the entry exempt from pressure eviction. */
1881
+ pin?: boolean;
1882
+ /**
1883
+ * Default `'never'`: a bucket path is treated as immutable, which is how the
1884
+ * client writes them (`crypto.randomUUID() + ext`). `'head'` spends a remote
1885
+ * `head()` to compare sizes before trusting L1.
1886
+ */
1887
+ revalidate?: 'never' | 'head';
1888
+ /** Skip L0/L1 entirely and refill from remote. Backs `refetch()`. */
1889
+ reload?: boolean;
1890
+ }
1891
+ interface BlobCacheStats {
1892
+ entries: number;
1893
+ totalBytes: number;
1894
+ budgetBytes: number;
1895
+ pinnedBytes: number;
1896
+ evictedEntries: number;
1897
+ evictedBytes: number;
1898
+ reconciledEntries: number;
1899
+ hits: number;
1900
+ misses: number;
1901
+ persistent: boolean;
1902
+ /** True when pinned bytes alone exceed the budget: new entries stop being
1903
+ * written rather than pinned ones being thrown away. */
1904
+ persistPaused: boolean;
1905
+ }
1906
+ interface BlobCacheOptions {
1907
+ store: BlobStore;
1908
+ manifest: BlobManifest;
1909
+ /** L2 read. Resolves to null when the file does not exist remotely. */
1910
+ fetchRemote(key: BlobKey): Promise<Blob | null>;
1911
+ /** L2 metadata, for `revalidate: 'head'`. */
1912
+ headRemote?(key: BlobKey): Promise<Record<string, unknown> | null>;
1913
+ logger: Logger$1;
1914
+ maxBytes: number;
1915
+ now?: () => number;
1916
+ /** Injected so the URL layer is exercisable off a DOM (node tests). */
1917
+ urls?: {
1918
+ create(blob: Blob): string;
1919
+ revoke(url: string): void;
1920
+ };
1921
+ }
1922
+ declare class BlobCache {
1923
+ private readonly store;
1924
+ private readonly manifest;
1925
+ private readonly fetchRemote;
1926
+ private readonly headRemote?;
1927
+ private readonly logger;
1928
+ private readonly now;
1929
+ private readonly urlFactory;
1930
+ private maxBytes;
1931
+ private persistPaused;
1932
+ /** Set after a quota failure survives one forced eviction. */
1933
+ private persistDisabled;
1934
+ private readonly urls;
1935
+ /** Ids at zero references, oldest first — the hot-URL window. */
1936
+ private idleUrls;
1937
+ private readonly inflight;
1938
+ private flushTimer;
1939
+ private readonly onPageHide;
1940
+ /**
1941
+ * Resolves once the manifest has been reconciled against disk. Reads await
1942
+ * it, so `start()` does NOT have to be awaited on the boot path — blocking
1943
+ * boot on an OPFS directory walk delayed the WebSocket connect (and with it
1944
+ * the connection supervisor) for no benefit.
1945
+ */
1946
+ private ready;
1947
+ private hits;
1948
+ private misses;
1949
+ private evictedEntries;
1950
+ private evictedBytes;
1951
+ private reconciledEntries;
1952
+ constructor(opts: BlobCacheOptions);
1953
+ /** Coalesce manifest write-back. Metadata only, so losing the tail costs an
1954
+ * LRU timestamp that reconcile reseeds from the file mtime. */
1955
+ private scheduleFlush;
1956
+ /**
1957
+ * Resolve the bytes for `key`, filling L1 on the way when `persist` is on.
1958
+ * Returns null when the file does not exist remotely and is not cached.
1959
+ */
1960
+ read(key: BlobKey, options?: BlobReadOptions): Promise<Blob | null>;
1961
+ /**
1962
+ * An object URL for `key`, refcounted. Callers MUST `release()`; the URL is
1963
+ * revoked once the last holder lets go and it falls out of the hot window.
1964
+ */
1965
+ acquireUrl(key: BlobKey, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
1966
+ private lease;
1967
+ private releaseUrl;
1968
+ private revokeUrl;
1969
+ /** L1 lookup with the size check that catches torn and cross-tab writes. */
1970
+ private readLocal;
1971
+ /** True when the remote agrees with the cached size, or cannot be reached. */
1972
+ private headMatches;
1973
+ private fetchDeduped;
1974
+ private persist;
1975
+ private writeThrough;
1976
+ /** Forget one path everywhere. Called on `bucket.put()`/`bucket.delete()`. */
1977
+ invalidate(key: BlobKey): Promise<void>;
1978
+ private dropLocal;
1979
+ setPinned(key: BlobKey, pinned: boolean): void;
1980
+ /**
1981
+ * Bring total bytes under budget by dropping the least recently used
1982
+ * entries. Pinned entries and anything with a live object URL are skipped —
1983
+ * evicting bytes that a mounted `<img>` is displaying would blank it.
1984
+ */
1985
+ private enforceBudget;
1986
+ /** Evict LRU-first until at or below `target`. Returns the resulting total. */
1987
+ private evictTo;
1988
+ /**
1989
+ * Rebuild the manifest from what is actually on disk. OPFS wins on existence
1990
+ * in both directions: files with no row get a row (seeded from mtime), rows
1991
+ * are only loaded for files that exist, and torn `.part-` writes are swept by
1992
+ * the walk itself.
1993
+ *
1994
+ * Rows whose file vanished outside our control (a browser origin eviction)
1995
+ * are left in `_00_blob`. They are inert — `load()` only ever asks for ids it
1996
+ * found on disk — and are overwritten if that path is cached again.
1997
+ */
1998
+ reconcile(): Promise<void>;
1999
+ /** Warm the cache for offline use. Skips anything already cached. */
2000
+ prefetch(keys: BlobKey[]): Promise<void>;
2001
+ /** Bind to the boot bucket and hydrate the manifest from disk. Separate from
2002
+ * {@link setNamespace} because boot must reconcile even when the namespace
2003
+ * it lands on is the one the store was constructed with. */
2004
+ start(namespace: string): Promise<void>;
2005
+ /** Repoint at another local bucket. The bytes of the old one stay on disk so
2006
+ * switching back (or signing back in) is still warm. */
2007
+ setNamespace(namespace: string): Promise<void>;
2008
+ setMaxBytes(maxBytes: number): void;
2009
+ /** Delete every cached byte in the current namespace. */
2010
+ clear(): Promise<void>;
2011
+ flush(): Promise<void>;
2012
+ /** Flush metadata and drop every object URL. Must run before the local store
2013
+ * closes — the flush writes through it. */
2014
+ close(): Promise<void>;
2015
+ stats(): BlobCacheStats;
2016
+ }
2017
+ //#endregion
2018
+ //#region src/sp00ky.d.ts
2019
+ /** Coerce whatever the `.get()` RPC hands back into a Blob. */
2020
+ declare function bucketContentToBlob(content: unknown): Blob | null;
547
2021
  declare class BucketHandle {
548
2022
  private bucketName;
549
2023
  private remote;
550
- constructor(bucketName: string, remote: RemoteDatabaseService);
2024
+ /** Absent on the raw handle the cache itself reads through. */
2025
+ private blobs?;
2026
+ constructor(bucketName: string, remote: RemoteDatabaseService, /** Absent on the raw handle the cache itself reads through. */
2027
+ blobs?: (BlobCache | null) | undefined);
551
2028
  put(path: string, content: string | Uint8Array | Blob): Promise<void>;
552
2029
  get(path: string): Promise<unknown>;
2030
+ /**
2031
+ * Read through the local blob cache: OPFS first, the bucket second. Unlike
2032
+ * {@link get} this survives a reload and works offline. Returns null when the
2033
+ * file exists in neither place.
2034
+ */
2035
+ read(path: string, options?: BlobReadOptions): Promise<Blob | null>;
2036
+ /**
2037
+ * A refcounted object URL for `path`, suitable for `<img src>`. The caller
2038
+ * MUST call `release()` when the URL goes off screen. Returns null when the
2039
+ * file does not exist, or when object URLs are unavailable (non-browser).
2040
+ */
2041
+ url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
2042
+ /** Exempt `path` from pressure eviction. Pinned bytes never expire. */
2043
+ pin(path: string): void;
2044
+ unpin(path: string): void;
2045
+ /** Drop `path` from the local cache without touching the remote file. */
2046
+ evict(path: string): Promise<void>;
2047
+ /** Warm the cache for offline use. Already-cached paths are skipped. */
2048
+ prefetch(paths: string[]): Promise<void>;
553
2049
  delete(path: string): Promise<void>;
554
2050
  exists(path: string): Promise<boolean>;
555
2051
  head(path: string): Promise<Record<string, unknown>>;
@@ -557,55 +2053,240 @@ declare class BucketHandle {
557
2053
  rename(sourcePath: string, targetPath: string): Promise<void>;
558
2054
  list(prefix?: string): Promise<string[]>;
559
2055
  }
560
- declare class SpookyClient<S extends SchemaStructure> {
2056
+ declare class Sp00kyClient<S extends SchemaStructure> {
561
2057
  private config;
562
2058
  private local;
563
2059
  private remote;
2060
+ private blobs;
2061
+ private connectionSupervisor;
564
2062
  private persistenceClient;
565
2063
  private migrator;
566
2064
  private cache;
567
2065
  private dataModule;
568
2066
  private sync;
569
2067
  private devTools;
2068
+ private crdtManager;
2069
+ private featureFlags;
2070
+ private appReleases;
2071
+ private preloadedHashes;
2072
+ private pendingQueryInits;
570
2073
  private logger;
571
2074
  auth: AuthService<S>;
572
2075
  streamProcessor: StreamProcessorService;
573
- get remoteClient(): Surreal;
574
- get localClient(): Surreal;
2076
+ private tabsCoordinator;
2077
+ private sharedActive;
2078
+ /** Current shared-tabs role, or null when the feature is off/fell back. */
2079
+ get tabRole(): TabRole | null;
2080
+ get remoteClient(): surrealdb0.Surreal;
2081
+ get localClient(): unknown;
575
2082
  get pendingMutationCount(): number;
2083
+ /** Number of times the initial list_ref LIVE subscription retried on
2084
+ * the most recent `setCurrentUserId` call. 0 when the SSP's
2085
+ * pre-emptive user-table creation got there first; >0 when LIVE
2086
+ * registration hit a "table not found" race. Exposed so the e2e
2087
+ * suite can guard the pre-emptive path against regression. */
2088
+ get liveRetryCount(): number;
576
2089
  subscribeToPendingMutations(cb: (count: number) => void): () => void;
577
- constructor(config: SpookyConfig<S>);
2090
+ /** Current sync-health snapshot. See {@link Sp00kyConfig.syncHealth}. */
2091
+ get syncHealth(): SyncHealth;
2092
+ /**
2093
+ * Observe sync health. Fires immediately with the current status and again
2094
+ * on every healthy↔degraded transition. Returns an unsubscribe.
2095
+ */
2096
+ subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void;
2097
+ /** Durability of the local cache. See {@link StorageHealth}. `'unknown'` for
2098
+ * engines that don't report it. */
2099
+ get storageHealth(): StorageHealth;
2100
+ /**
2101
+ * Observe local-store durability. Fires immediately with the current snapshot
2102
+ * and again on every change (at most once per bucket open in practice).
2103
+ * Returns an unsubscribe.
2104
+ */
2105
+ subscribeToStorageHealth(cb: (health: StorageHealth) => void): () => void;
2106
+ constructor(config: Sp00kyConfig<S>);
2107
+ /** The shared-tabs role machinery, wired to this client's modules. */
2108
+ private buildTabsCoordinator;
578
2109
  /**
579
2110
  * Setup direct callbacks instead of event subscriptions
580
2111
  */
581
2112
  private setupCallbacks;
582
2113
  init(): Promise<void>;
2114
+ private bucketSwitchChain;
2115
+ private pendingBucketTarget;
2116
+ /**
2117
+ * Ensure the local store is this user's bucket, switching if needed. Called
2118
+ * from the auth listener on every auth flip; concurrent calls are chained
2119
+ * and superseded intermediates are skipped (latest target wins).
2120
+ */
2121
+ private ensureLocalBucket;
2122
+ /**
2123
+ * The bucket-switch choreography: drain → swap → rebind.
2124
+ *
2125
+ * Drain: sync quiesced (poll/LIVE stopped, in-flight round awaited so its
2126
+ * outbox delete lands in the OLD bucket, debounce timers cancelled),
2127
+ * DataModule timers cleared, CRDT fields closed WITHOUT their final flush
2128
+ * (the remote session already belongs to the next user).
2129
+ *
2130
+ * Swap: gate closes so any local query issued mid-switch (sibling auth
2131
+ * subscribers, FeatureFlagModule) waits and then runs against the NEW
2132
+ * bucket; store swaps open-new-before-close-old; schema provisions
2133
+ * (no-op for a returning bucket); stale `_00_query` rows are wiped (dead
2134
+ * sessionId-salted hashes with stale arrays — record bodies stay warm);
2135
+ * SSP resets to a fresh circuit with re-seeded permissions.
2136
+ *
2137
+ * Rebind: auth token re-persisted (the surrealdb persistence client wrote it
2138
+ * into the OLD bucket's `_00_kv` before this listener ran), active queries
2139
+ * re-homed keeping their hashes, sync resumed on the new bucket's own
2140
+ * outbox, and every query re-registered remotely to refill from the server.
2141
+ */
2142
+ private doSwitchBucket;
583
2143
  close(): Promise<void>;
2144
+ /**
2145
+ * Subscribe to a feature flag for the current user. Returns a
2146
+ * `FeatureFlagHandle` whose `variant()`, `payload()` and `enabled()`
2147
+ * accessors reflect the latest assignment from `_00_user_feature`,
2148
+ * and whose `subscribe(cb)` fires whenever that assignment changes.
2149
+ *
2150
+ * Permissions are enforced by SurrealDB: a client can only ever see
2151
+ * its own row, and cannot create or modify assignments.
2152
+ */
2153
+ feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
2154
+ /**
2155
+ * Force a feature flag to `variant` in THIS browser only; `null` clears it.
2156
+ *
2157
+ * Nothing is sent to the server — the `_00_user_feature` assignment is
2158
+ * untouched, so clearing restores whatever the server says. Persisted to
2159
+ * localStorage, survives reloads, and applies while signed out. Backs the
2160
+ * DevTools Flags tab, and is a convenient hook for tests.
2161
+ *
2162
+ * To change a flag for OTHER users you need admin rights (`spky admin add`)
2163
+ * and the DevTools Flags tab, or `spky flag`.
2164
+ */
2165
+ setFeatureOverride(key: string, variant: string | null, payload?: unknown): void;
2166
+ /** Drop every local feature flag override set via `setFeatureOverride`. */
2167
+ clearFeatureOverrides(): void;
2168
+ /** The local feature flag overrides currently in effect, keyed by flag. */
2169
+ getFeatureOverrides(): Record<string, FeatureFlagOverride>;
2170
+ /**
2171
+ * Observe the announced release of an app (`_00_app_release:<app>`, written
2172
+ * by `spky deploy` / `spky release`). The handle's `snapshot()` carries the
2173
+ * announced version plus the cache-bust/mandatory flags, and
2174
+ * `updateAvailable(currentVersion)` compares it semver-wise against the
2175
+ * running build. World-readable; writes are root-only.
2176
+ */
2177
+ appRelease(app: string, options?: AppReleaseOptions): AppReleaseHandle;
584
2178
  authenticate(token: string): Promise<surrealdb0.Tokens>;
2179
+ /**
2180
+ * Open a CRDT field for collaborative editing.
2181
+ * Returns a CrdtField with a LoroDoc that can be bound to any editor.
2182
+ * Also starts a LIVE SELECT on the parent table for real-time sync;
2183
+ * incoming events trigger a subquery fetch of `_00_crdt` / `_00_cursor`.
2184
+ */
2185
+ openCrdtField(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
2186
+ /**
2187
+ * Close a CRDT field when editing is done.
2188
+ */
2189
+ closeCrdtField(table: string, recordId: string, field: string): void;
585
2190
  deauthenticate(): Promise<void>;
586
- query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, SpookyQueryResultPromise>;
2191
+ query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, Sp00kyQueryResultPromise>;
587
2192
  private initQuery;
2193
+ /**
2194
+ * Background tail of {@link initQuery}: instant-hydrate (opt-in via
2195
+ * `config.instantHydrate`, and only when the query is cold) followed by
2196
+ * enqueuing the `register` down-event. Never rejects — both halves catch and
2197
+ * log, so `void`-ing the returned promise can't produce an unhandled
2198
+ * rejection. By default (hydrate off) the register lifecycle is the single
2199
+ * freshness path; the one-shot fetch is an optimization apps enable
2200
+ * explicitly, and it runs regardless of preload state — cache-first delivery
2201
+ * never depends on WHY rows are cached.
2202
+ */
2203
+ private finishQueryInit;
2204
+ /**
2205
+ * Smart, awaitable preload/prewarm into the LOCAL cache — without registering a
2206
+ * live view (NO `_00_query`, NO subscription, NO TTL heartbeat).
2207
+ *
2208
+ * Cache-aware via a durable per-bucket freshness marker (`_00_preload`):
2209
+ * - COLD (never preloaded in this bucket): fetch the query one-shot from the
2210
+ * remote, persist the rows (+ embedded `.related()` children), stamp the
2211
+ * marker — and AWAIT it. This is the "smart waiting" first load: callers can
2212
+ * `await db.preload(...)` to hold the UI until the data is ready.
2213
+ * - WARM (marker present): return instantly — NEVER blocks. `refresh` decides
2214
+ * whether to also kick a one-time silent refetch (see {@link PreloadOptions}).
2215
+ * Default `onUse` does nothing; the data freshens when the real `useQuery`
2216
+ * mounts and registers its live view.
2217
+ *
2218
+ * Best-effort: any fetch failure (offline, etc.) is a no-op warn (no marker
2219
+ * written, so it's retried next load). Deduped per session by query hash.
2220
+ */
2221
+ preload(finalQuery: FinalQuery<S, any, any, any, any, any>, options?: PreloadOptions): Promise<void>;
2222
+ /**
2223
+ * One-shot remote fetch + local persist for a preload query. Returns the row
2224
+ * count on success, or -1 on failure (best-effort: logged, never thrown) so
2225
+ * the caller skips stamping the freshness marker and retries next load.
2226
+ */
2227
+ private fetchAndPersist;
588
2228
  queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<string>;
589
2229
  subscribe(queryHash: string, callback: (records: Record<string, any>[]) => void, options?: {
590
2230
  immediate?: boolean;
591
2231
  }): Promise<() => void>;
2232
+ /**
2233
+ * Opt-in eager teardown for a query whose last subscriber has gone away
2234
+ * (e.g. a viewport-windowed list cancelling an off-screen window). No-op
2235
+ * while any subscriber remains. Tears down the remote `_00_query` view +
2236
+ * local WASM view instead of waiting for the TTL sweep. Default behavior
2237
+ * (no call here) keeps the view resident for cheap re-subscription.
2238
+ */
2239
+ deregisterQuery(queryHash: string): void;
2240
+ /**
2241
+ * Subscribe to a query's fetch-status changes (idle/fetching). With
2242
+ * `{ immediate: true }` the callback fires synchronously with the current
2243
+ * status. Powers the `useQuery` hook's `isFetching()` accessor.
2244
+ */
2245
+ subscribeQueryStatus(queryHash: string, callback: QueryStatusCallback, options?: {
2246
+ immediate?: boolean;
2247
+ }): () => void;
2248
+ /**
2249
+ * Report the frontend processing time (ms) a client framework spent applying
2250
+ * an update for a query (e.g. `useQuery`'s `reconcile()`), so DevTools/MCP can
2251
+ * surface the "frontend" phase of the per-query timing breakdown.
2252
+ */
2253
+ reportFrontendTiming(queryHash: string, ms: number): void;
592
2254
  run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, payload: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
593
2255
  bucket<B extends BucketNames<S>>(name: B): BucketHandle;
2256
+ /** Cache-free handle. The blob cache reads the remote through this, so a
2257
+ * cache miss can't loop back into the cache. */
2258
+ private rawBucket;
2259
+ /** Blob cache counters for DevTools. */
2260
+ getBlobCacheStats(): BlobCacheStats;
594
2261
  create(id: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
595
2262
  update(table: string, id: string, data: Record<string, unknown>, options?: UpdateOptions): Promise<{
596
2263
  [x: string]: /*elided*/any;
597
2264
  }>;
598
2265
  delete(table: string, id: string): Promise<void>;
599
2266
  useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T>;
600
- private persistClientId;
601
- private loadOrGenerateClientId;
2267
+ /**
2268
+ * Fetch SurrealDB's `session::id()` as a string. Used as a salt for
2269
+ * query-id hashing so two sessions for the same user get distinct
2270
+ * `_00_query` rows. Returns empty string if the query fails (we still
2271
+ * boot, just without session scoping for IDs).
2272
+ */
2273
+ private fetchSessionId;
602
2274
  }
603
2275
  //#endregion
2276
+ //#region src/utils/semver.d.ts
2277
+ /** True when `a` is a valid version strictly greater than valid version `b`. */
2278
+ declare function semverGt(a: unknown, b: unknown): boolean;
2279
+ //#endregion
604
2280
  //#region src/utils/index.d.ts
605
2281
  declare function fileToUint8Array(file: File | Blob): Promise<Uint8Array>;
2282
+ /**
2283
+ * Convert plain text to simple HTML paragraphs.
2284
+ * Useful for seeding a rich-text editor (e.g. TipTap/ProseMirror) with fallback content.
2285
+ */
2286
+ declare function textToHtml(text: string): string;
606
2287
  /**
607
2288
  * Helper for retrying DB operations with exponential backoff
608
2289
  */
609
2290
 
610
2291
  //#endregion
611
- export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, DebounceOptions, EventSubscriptionOptions, type Level, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryTimeToLive, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RunOptions, SpookyClient, SpookyConfig, SpookyQueryResult, SpookyQueryResultPromise, StoreType, UpdateOptions, createAuthEventSystem, fileToUint8Array };
2292
+ export { AppReleaseHandle, AppReleaseModule, type AppReleaseOptions, type AppReleaseSnapshot, AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, type BlobCacheStats, type BlobEntry, type BlobKey, type BlobReadOptions, type BlobUrlLease, BucketHandle, CURSOR_COLORS, ConnectionState, CrdtField, CrdtManager, DebounceOptions, EventSubscriptionOptions, FeatureFlagHandle, FeatureFlagModule, type FeatureFlagOptions, type FeatureFlagOverride, type FeatureFlagSnapshot, Level, MATERIALIZATION_SAMPLE_WINDOW, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, PreloadOptions, PreloadRefresh, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, ReconnectConfig, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, Sp00kyClient, Sp00kyConfig, Sp00kyQueryResult, Sp00kyQueryResultPromise, StorageHealth, StorageHealthStatus, StoreType, SyncHealth, SyncHealthConfig, SyncHealthStatus, TimingPhase, UpdateOptions, bucketContentToBlob, createAuthEventSystem, cursorColorFromName, fileToUint8Array, semverGt, textToHtml };