@spooky-sync/core 0.0.1-canary.11 → 0.0.1-canary.110

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 (86) hide show
  1. package/AGENTS.md +56 -0
  2. package/dist/index.d.ts +1033 -372
  3. package/dist/index.js +5490 -1331
  4. package/dist/otel/index.d.ts +21 -0
  5. package/dist/otel/index.js +86 -0
  6. package/dist/sqlite-worker.d.ts +1 -0
  7. package/dist/sqlite-worker.js +107 -0
  8. package/dist/types.d.ts +721 -0
  9. package/package.json +40 -9
  10. package/skills/sp00ky-core/SKILL.md +258 -0
  11. package/skills/sp00ky-core/references/auth.md +98 -0
  12. package/skills/sp00ky-core/references/config.md +76 -0
  13. package/src/build-globals.d.ts +12 -0
  14. package/src/events/events.test.ts +2 -1
  15. package/src/events/index.ts +3 -0
  16. package/src/index.ts +9 -2
  17. package/src/modules/auth/events/index.ts +2 -1
  18. package/src/modules/auth/index.ts +59 -20
  19. package/src/modules/cache/index.ts +77 -32
  20. package/src/modules/cache/types.ts +2 -2
  21. package/src/modules/crdt/crdt-field.ts +288 -0
  22. package/src/modules/crdt/crdt-hydration.test.ts +206 -0
  23. package/src/modules/crdt/index.ts +357 -0
  24. package/src/modules/data/data.rebind.test.ts +147 -0
  25. package/src/modules/data/data.status.test.ts +249 -0
  26. package/src/modules/data/index.ts +996 -125
  27. package/src/modules/data/window-query.test.ts +52 -0
  28. package/src/modules/data/window-query.ts +154 -0
  29. package/src/modules/devtools/index.ts +180 -28
  30. package/src/modules/devtools/versions.test.ts +74 -0
  31. package/src/modules/devtools/versions.ts +81 -0
  32. package/src/modules/feature-flag/index.test.ts +120 -0
  33. package/src/modules/feature-flag/index.ts +209 -0
  34. package/src/modules/ref-tables.test.ts +91 -0
  35. package/src/modules/ref-tables.ts +88 -0
  36. package/src/modules/sync/engine.ts +101 -37
  37. package/src/modules/sync/events/index.ts +9 -2
  38. package/src/modules/sync/queue/queue-down.ts +12 -5
  39. package/src/modules/sync/queue/queue-up.ts +29 -14
  40. package/src/modules/sync/scheduler.pause.test.ts +109 -0
  41. package/src/modules/sync/scheduler.ts +73 -7
  42. package/src/modules/sync/sync.health.test.ts +149 -0
  43. package/src/modules/sync/sync.ts +1017 -62
  44. package/src/modules/sync/utils.test.ts +269 -2
  45. package/src/modules/sync/utils.ts +182 -17
  46. package/src/otel/index.ts +127 -0
  47. package/src/services/database/cache-engine.ts +143 -0
  48. package/src/services/database/database.ts +11 -11
  49. package/src/services/database/engine-factory.ts +32 -0
  50. package/src/services/database/events/index.ts +2 -1
  51. package/src/services/database/index.ts +6 -0
  52. package/src/services/database/local-migrator.ts +27 -27
  53. package/src/services/database/local.test.ts +64 -0
  54. package/src/services/database/local.ts +452 -66
  55. package/src/services/database/plan-render.test.ts +85 -0
  56. package/src/services/database/plan-render.ts +86 -0
  57. package/src/services/database/relation-resolver.test.ts +413 -0
  58. package/src/services/database/relation-resolver.ts +0 -0
  59. package/src/services/database/remote.ts +13 -13
  60. package/src/services/database/sqlite-cache-engine.test.ts +85 -0
  61. package/src/services/database/sqlite-cache-engine.ts +693 -0
  62. package/src/services/database/sqlite-worker.ts +116 -0
  63. package/src/services/database/surql-translate.ts +291 -0
  64. package/src/services/database/surreal-cache-engine.ts +122 -0
  65. package/src/services/logger/index.ts +6 -101
  66. package/src/services/persistence/localstorage.ts +2 -2
  67. package/src/services/persistence/resilient.ts +41 -0
  68. package/src/services/persistence/surrealdb.ts +10 -10
  69. package/src/services/stream-processor/index.ts +295 -38
  70. package/src/services/stream-processor/permissions.test.ts +47 -0
  71. package/src/services/stream-processor/permissions.ts +53 -0
  72. package/src/services/stream-processor/stream-processor.batch.test.ts +136 -0
  73. package/src/services/stream-processor/stream-processor.reset.test.ts +104 -0
  74. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  75. package/src/services/stream-processor/wasm-types.ts +18 -2
  76. package/src/sp00ky.auth-order.test.ts +92 -0
  77. package/src/sp00ky.ts +795 -0
  78. package/src/types.ts +231 -15
  79. package/src/utils/error-classification.test.ts +44 -0
  80. package/src/utils/error-classification.ts +7 -0
  81. package/src/utils/index.ts +35 -13
  82. package/src/utils/parser.ts +3 -2
  83. package/src/utils/surql.ts +24 -15
  84. package/src/utils/withRetry.test.ts +1 -1
  85. package/tsdown.config.ts +64 -1
  86. package/src/spooky.ts +0 -392
@@ -0,0 +1,721 @@
1
+ import { RecordId } from "surrealdb";
2
+ import { QueryPlan, QueryPlan as QueryPlan$1, RecordId as RecordId$1, SchemaStructure, WhereNode } from "@spooky-sync/query-builder";
3
+ import { Level, Level as Level$1, Logger, LoggerOptions } from "pino";
4
+
5
+ //#region src/events/index.d.ts
6
+ /**
7
+ * Utility type to define the payload structure of an event.
8
+ * If the payload type P is never, it defines payload as undefined.
9
+ */
10
+ type EventPayloadDefinition<P> = [P] extends [never] ? {
11
+ payload: undefined;
12
+ } : {
13
+ payload: P;
14
+ };
15
+ /**
16
+ * Defines the structure of an event with a specific type and payload.
17
+ * @template T The string literal type of the event.
18
+ * @template P The type of the event payload.
19
+ */
20
+ type EventDefinition<T extends string, P> = {
21
+ type: T;
22
+ } & EventPayloadDefinition<P>;
23
+ /**
24
+ * A map of event types to their definitions.
25
+ * Keys are event names, values are EventDefinitions.
26
+ */
27
+ type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
28
+ /**
29
+ * Options for pushing/emitting events.
30
+ */
31
+ interface PushEventOptions {
32
+ /** Configuration for debouncing the event. */
33
+ debounced?: {
34
+ key: string;
35
+ delay: number;
36
+ };
37
+ }
38
+ /**
39
+ * Extracts the full Event object type from the map for a given key.
40
+ */
41
+ type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
42
+ /**
43
+ * Extracts the payload type from the map for a given key.
44
+ */
45
+ type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
46
+ /**
47
+ * Array of available event type keys.
48
+ */
49
+ type EventTypes<E extends EventTypeMap> = (keyof E)[];
50
+ /**
51
+ * Represents a valid key (event name) from the EventTypeMap.
52
+ */
53
+ type EventType<E extends EventTypeMap> = keyof E;
54
+ /**
55
+ * Function signature for an event handler.
56
+ */
57
+ type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
58
+ /**
59
+ * Options when subscribing to an event.
60
+ */
61
+ type EventSubscriptionOptions$1 = {
62
+ /** If true, the handler will be called immediately with the last emitted event of this type (if any). */
63
+ immediately?: boolean;
64
+ /** If true, the subscription will be automatically removed after the first event is handled. */
65
+ once?: boolean;
66
+ };
67
+ /**
68
+ * A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
69
+ * @template E The EventTypeMap defining all supported events.
70
+ */
71
+ declare class EventSystem<E extends EventTypeMap> {
72
+ private _eventTypes;
73
+ private subscriberId;
74
+ private isProcessing;
75
+ private buffer;
76
+ private subscribers;
77
+ private subscribersTypeMap;
78
+ private lastEvents;
79
+ private debouncedEvents;
80
+ constructor(_eventTypes: EventTypes<E>);
81
+ get eventTypes(): EventTypes<E>;
82
+ /**
83
+ * Subscribes a handler to a specific event type.
84
+ * @param type The event type to subscribe to.
85
+ * @param handler The function to call when the event occurs.
86
+ * @param options Subscription options (once, immediately).
87
+ * @returns A subscription ID that can be used to unsubscribe.
88
+ */
89
+ subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
90
+ /**
91
+ * Subscribes a handler to multiple event types.
92
+ * @param types An array of event types to subscribe to.
93
+ * @param handler The function to call when any of the events occur.
94
+ * @param options Subscription options.
95
+ * @returns An array of subscription IDs.
96
+ */
97
+ subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
98
+ /**
99
+ * Unsubscribes a specific subscription by ID.
100
+ * @param id The subscription ID returned by subscribe().
101
+ * @returns True if the subscription was found and removed, false otherwise.
102
+ */
103
+ unsubscribe(id: number): boolean;
104
+ /**
105
+ * Emits an event with the given type and payload.
106
+ * @param type The type of event to emit.
107
+ * @param payload The data associated with the event.
108
+ */
109
+ emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
110
+ /**
111
+ * Adds a fully constructed event object to the system.
112
+ * Similar to emit, but takes the full event object directly.
113
+ * Supports debouncing if options are provided.
114
+ * @param event The event object.
115
+ * @param options Options for the event push (e.g., debouncing).
116
+ */
117
+ addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
118
+ private handleDebouncedEvent;
119
+ private scheduleProcessing;
120
+ private processEvents;
121
+ private dequeue;
122
+ private setLastEvent;
123
+ private broadcastEvent;
124
+ }
125
+ //#endregion
126
+ //#region src/modules/sync/events/index.d.ts
127
+ declare const SyncEventTypes: {
128
+ readonly QueryUpdated: "SYNC_QUERY_UPDATED";
129
+ readonly RemoteDataIngested: "SYNC_REMOTE_DATA_INGESTED";
130
+ readonly MutationRolledBack: "SYNC_MUTATION_ROLLED_BACK";
131
+ readonly SyncHealthChanged: "SYNC_HEALTH_CHANGED";
132
+ };
133
+ type SyncEventTypeMap = {
134
+ [SyncEventTypes.QueryUpdated]: EventDefinition<typeof SyncEventTypes.QueryUpdated, {
135
+ queryId: any;
136
+ localHash?: string;
137
+ localArray?: RecordVersionArray;
138
+ remoteHash?: string;
139
+ remoteArray?: RecordVersionArray;
140
+ records: Record<string, any>[];
141
+ }>;
142
+ [SyncEventTypes.RemoteDataIngested]: EventDefinition<typeof SyncEventTypes.RemoteDataIngested, {
143
+ records: Record<string, any>[];
144
+ }>;
145
+ [SyncEventTypes.MutationRolledBack]: EventDefinition<typeof SyncEventTypes.MutationRolledBack, {
146
+ eventType: string;
147
+ recordId: string;
148
+ error: string;
149
+ }>;
150
+ [SyncEventTypes.SyncHealthChanged]: EventDefinition<typeof SyncEventTypes.SyncHealthChanged, SyncHealth>;
151
+ };
152
+ type SyncEventSystem = EventSystem<SyncEventTypeMap>;
153
+ //#endregion
154
+ //#region src/services/logger/index.d.ts
155
+ type Logger$1 = Logger;
156
+ //#endregion
157
+ //#region src/services/database/events/index.d.ts
158
+ declare const DatabaseEventTypes: {
159
+ readonly LocalQuery: "DATABASE_LOCAL_QUERY";
160
+ readonly RemoteQuery: "DATABASE_REMOTE_QUERY";
161
+ };
162
+ interface DatabaseQueryEventPayload {
163
+ query: string;
164
+ vars?: Record<string, unknown>;
165
+ duration: number;
166
+ success: boolean;
167
+ error?: string;
168
+ timestamp: number;
169
+ }
170
+ type DatabaseEventTypeMap = {
171
+ [DatabaseEventTypes.LocalQuery]: EventDefinition<typeof DatabaseEventTypes.LocalQuery, DatabaseQueryEventPayload>;
172
+ [DatabaseEventTypes.RemoteQuery]: EventDefinition<typeof DatabaseEventTypes.RemoteQuery, DatabaseQueryEventPayload>;
173
+ };
174
+ type DatabaseEventSystem = EventSystem<DatabaseEventTypeMap>;
175
+ //#endregion
176
+ //#region src/utils/surql.d.ts
177
+ interface SealedQuery<T = void> {
178
+ readonly sql: string;
179
+ readonly extract: (results: unknown[]) => T;
180
+ }
181
+ //#endregion
182
+ //#region src/services/database/cache-engine.d.ts
183
+ /**
184
+ * A materialized row. Keys are field names; values are already decoded to the
185
+ * client's runtime shapes (RecordId stays a RecordId, bytes a Uint8Array, …) so
186
+ * every backend hands `DataModule` the same shape SurrealDB does today.
187
+ */
188
+ type Row = Record<string, unknown>;
189
+ /** A record identifier — a `RecordId` or its stable string form (`table:id`). */
190
+ type Id = unknown;
191
+ /** How an order clause is expressed everywhere in the engine layer. */
192
+ type OrderBy = [field: string, direction: 'asc' | 'desc'][];
193
+ /**
194
+ * Batched relation fetch: "give me every row of `table` whose `matchField` is
195
+ * one of `keys`, filtered by `where`, ordered by `orderBy`". This is the single
196
+ * primitive relation decomposition (§3) leans on — implemented as
197
+ * `SELECT … WHERE <matchField> IN (…)` on SQLite, `SELECT … FROM $keys` /
198
+ * `WHERE <matchField> IN $keys` on SurrealDB, or an index scan elsewhere. Order
199
+ * here is a hint; the resolver re-applies order+limit PER PARENT after grouping.
200
+ */
201
+ interface RelationFetch {
202
+ table: string;
203
+ matchField: string;
204
+ keys: Id[];
205
+ where?: WhereNode[];
206
+ orderBy?: OrderBy;
207
+ select?: string[];
208
+ }
209
+ /**
210
+ * The read side of an engine, minus relation resolution — the surface a
211
+ * {@link RelationResolver} needs. Kept separate so the resolver can be unit
212
+ * tested against an in-memory fake without a full engine.
213
+ */
214
+ interface RowFetcher {
215
+ /** Batched fan-out fetch. See {@link RelationFetch}. */
216
+ fetchRelation(req: RelationFetch): Promise<Row[]>;
217
+ }
218
+ /** A transaction handle — the same verbs as the engine, but atomic. */
219
+ interface EngineTx {
220
+ upsert(table: string, id: Id, data: Row, mode: 'replace' | 'merge'): Promise<void>;
221
+ patch(table: string, id: Id, patches: unknown[]): Promise<void>;
222
+ delete(table: string, id: Id): Promise<void>;
223
+ }
224
+ /**
225
+ * A pluggable local cache backend. SurrealDB (the default) and SQLite both
226
+ * implement this; the rest of the client talks verbs, never SurrealQL.
227
+ *
228
+ * Reactivity is NOT part of this contract: the local cache is passive. The SSP
229
+ * (remote) drives change; `DataModule` writes rows here and re-reads them. The
230
+ * `epoch` field preserves the existing bucket-switch fencing (see
231
+ * `LocalDatabaseService.epoch`): an async chain captures it at start and its
232
+ * write is dropped if the epoch moved (a bucket switch) in between.
233
+ */
234
+ interface LocalCacheEngine extends RowFetcher {
235
+ /** Monotonic store generation; bumped on every bucket switch. */
236
+ readonly epoch: number;
237
+ connect(bucketId: string): Promise<void>;
238
+ switchBucket(bucketId: string): Promise<void>;
239
+ close(): Promise<void>;
240
+ /** Run `fn` inside a single atomic transaction. */
241
+ transaction<T>(fn: (tx: EngineTx) => Promise<T>): Promise<T>;
242
+ /**
243
+ * Materialize a query, including its `.related()` tree (via §3
244
+ * decomposition). Params bind `where` `paramRef`s and any windowing id-set.
245
+ */
246
+ select(plan: QueryPlan$1, params?: Record<string, unknown>): Promise<Row[]>;
247
+ /** Fetch rows by primary id, preserving `ids` order; missing ids are skipped. */
248
+ selectByIds(table: string, ids: Id[], opts?: {
249
+ select?: string[];
250
+ orderBy?: OrderBy;
251
+ }): Promise<Row[]>;
252
+ /** Single-record read by primary id, or `null`. */
253
+ getById(table: string, id: Id): Promise<Row | null>;
254
+ upsert(table: string, id: Id, data: Row, mode: 'replace' | 'merge'): Promise<void>;
255
+ patch(table: string, id: Id, patches: unknown[]): Promise<void>;
256
+ delete(table: string, id: Id): Promise<void>;
257
+ }
258
+ /**
259
+ * The full surface the client's `this.local` field depends on: the
260
+ * engine-neutral {@link LocalCacheEngine} verbs PLUS the legacy
261
+ * SurrealQL/lifecycle methods the not-yet-migrated call sites still use.
262
+ * `SurrealCacheEngine` (subclass of `LocalDatabaseService`) and
263
+ * `SqliteCacheEngine` (via a SurrealQL-vocabulary shim) both satisfy this, so
264
+ * either can back `this.local`.
265
+ *
266
+ * `getClient()` returns the underlying SurrealDB `Surreal` handle where one
267
+ * exists (SurrealDB backend); backends without one (SQLite) throw — it is only
268
+ * used by advanced/DevTools paths, never on the hot path.
269
+ */
270
+ interface LocalStore extends LocalCacheEngine {
271
+ /**
272
+ * Whether this engine needs SurrealQL schema provisioning (`DEFINE TABLE`,
273
+ * `DEFINE FIELD`, …) run against it at init / bucket switch. SurrealDB → true;
274
+ * schemaless engines (SQLite creates tables lazily) → false, so the client
275
+ * skips the `LocalMigrator` entirely for them.
276
+ */
277
+ readonly usesSurqlSchema: boolean;
278
+ query<T extends unknown[]>(query: string, vars?: Record<string, unknown>, opts?: {
279
+ epoch?: number;
280
+ }): Promise<T>;
281
+ execute<T>(query: SealedQuery<T>, vars?: Record<string, unknown>, opts?: {
282
+ epoch?: number;
283
+ }): Promise<T>;
284
+ queryUngated<T extends unknown[]>(query: string, vars?: Record<string, unknown>): Promise<T>;
285
+ switchStore(bucketId: string): Promise<void>;
286
+ beginSwitch(): () => void;
287
+ getEvents(): DatabaseEventSystem;
288
+ getClient(): unknown;
289
+ getConfig(): Sp00kyConfig<any>['database'];
290
+ readonly currentBucketId: string;
291
+ }
292
+ /** Selected local cache backend. Mirrors the `persistenceClient` config pattern. */
293
+ type LocalEngineChoice = 'surrealdb' | 'sqlite' | LocalStore;
294
+ /** Thrown when relation decomposition nests past {@link MAX_RELATION_DEPTH} —
295
+ * a guard against a cyclic schema producing unbounded fan-out. */
296
+ //#endregion
297
+ //#region src/modules/sync/queue/queue-up.d.ts
298
+ type CreateEvent = {
299
+ type: 'create';
300
+ mutation_id: RecordId;
301
+ record_id: RecordId;
302
+ data: Record<string, unknown>;
303
+ record?: Record<string, unknown>;
304
+ tableName?: string;
305
+ options?: PushEventOptions;
306
+ };
307
+ type UpdateEvent = {
308
+ type: 'update';
309
+ mutation_id: RecordId;
310
+ record_id: RecordId;
311
+ data: Record<string, unknown>;
312
+ record?: Record<string, unknown>;
313
+ beforeRecord?: Record<string, unknown>;
314
+ options?: PushEventOptions;
315
+ };
316
+ type DeleteEvent = {
317
+ type: 'delete';
318
+ mutation_id: RecordId;
319
+ record_id: RecordId;
320
+ options?: PushEventOptions;
321
+ };
322
+ type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
323
+ //#endregion
324
+ //#region src/types.d.ts
325
+ /**
326
+ * A pino browser transmit object for forwarding logs to an external sink (e.g. OpenTelemetry).
327
+ */
328
+ type PinoTransmit = NonNullable<NonNullable<LoggerOptions['browser']>['transmit']>;
329
+ /**
330
+ * The type of storage backend to use for the local database.
331
+ * - 'memory': In-memory storage (transient).
332
+ * - 'indexeddb': IndexedDB storage (persistent).
333
+ */
334
+ type StoreType = 'memory' | 'indexeddb';
335
+ /**
336
+ * Interface for a custom persistence client.
337
+ * Allows providing a custom storage mechanism for the local database.
338
+ */
339
+ interface PersistenceClient {
340
+ /**
341
+ * Sets a value in the storage.
342
+ * @param key The key to set.
343
+ * @param value The value to store.
344
+ */
345
+ set<T>(key: string, value: T): Promise<void>;
346
+ /**
347
+ * Gets a value from the storage.
348
+ * @param key The key to retrieve.
349
+ * @returns The stored value or null if not found.
350
+ */
351
+ get<T>(key: string): Promise<T | null>;
352
+ /**
353
+ * Removes a value from the storage.
354
+ * @param key The key to remove.
355
+ */
356
+ remove(key: string): Promise<void>;
357
+ }
358
+ /**
359
+ * Supported Time-To-Live (TTL) values for cached queries.
360
+ * Format: number + unit (m=minutes, h=hours, d=days).
361
+ */
362
+ type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
363
+ /**
364
+ * Result object returned when a query is registered or executed.
365
+ */
366
+ interface Sp00kyQueryResult {
367
+ /** The unique hash identifier for the query. */
368
+ hash: string;
369
+ }
370
+ type Sp00kyQueryResultPromise = Promise<Sp00kyQueryResult>;
371
+ interface EventSubscriptionOptions {
372
+ priority?: number;
373
+ }
374
+ /**
375
+ * Configuration options for the Sp00ky client.
376
+ * @template S The schema structure type.
377
+ */
378
+ interface Sp00kyConfig<S extends SchemaStructure> {
379
+ /** Database connection configuration. */
380
+ database: {
381
+ /** The SurrealDB endpoint URL. */
382
+ endpoint?: string;
383
+ /** The namespace to use. */
384
+ namespace: string;
385
+ /** The database name. */
386
+ database: string;
387
+ /** The local store type implementation. */
388
+ store?: StoreType;
389
+ /** Authentication token. */
390
+ token?: string;
391
+ };
392
+ /** The schema definition. */
393
+ schema: S;
394
+ /** The compiled SURQL schema string. */
395
+ schemaSurql: string;
396
+ /** Logging level. */
397
+ logLevel: Level$1;
398
+ /**
399
+ * Persistence client to use.
400
+ * Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
401
+ */
402
+ persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
403
+ /**
404
+ * Local cache engine backend. `'surrealdb'` (default) uses the in-browser
405
+ * SurrealDB-WASM store; `'sqlite'` uses official SQLite-WASM in a Worker with
406
+ * OPFS persistence; or pass a custom {@link LocalCacheEngine}. The local cache
407
+ * is a passive queryable store — reactivity is driven by the remote SSP, not
408
+ * this engine. See `services/database/cache-engine.ts`.
409
+ */
410
+ localEngine?: LocalEngineChoice;
411
+ /** A pino browser transmit object for forwarding logs (e.g. via @spooky-sync/core/otel). */
412
+ otelTransmit?: PinoTransmit;
413
+ /**
414
+ * Debounce time in milliseconds for stream updates (the client-side SSP
415
+ * aggregation throttle — coalesces the in-browser StreamProcessor's
416
+ * per-record updates per query before notifying readers).
417
+ * Defaults to 50ms.
418
+ */
419
+ streamDebounceTime?: number;
420
+ /**
421
+ * Debounce time in milliseconds for syncing collaborative (CRDT) field
422
+ * changes to the remote database. Local writes happen immediately on
423
+ * every keystroke (so reload/offline works), but the remote UPSERT is
424
+ * coalesced over this window. Lower = snappier remote propagation +
425
+ * more network traffic; higher = less traffic + more lag for other
426
+ * collaborators. Defaults to 500ms.
427
+ */
428
+ crdtDebounceMs?: number;
429
+ /**
430
+ * Cadence (ms) for the `_00_list_ref` poll that catches cross-session
431
+ * UPDATEs the SurrealDB v3 LIVE-permission gap drops. Lower = faster
432
+ * convergence + more query load; higher = the inverse. Non-positive
433
+ * values fall back to the default (500ms).
434
+ */
435
+ refSyncIntervalMs?: number;
436
+ /**
437
+ * Instant-hydrate cold queries: when a query is registered with no local
438
+ * data yet, first run its surql directly on the remote (one-shot) and display
439
+ * the result immediately, THEN do the full realtime registration in the
440
+ * background. The hydrated rows are ingested with their versions so the
441
+ * registration's `syncRecords` skips re-pulling unchanged bodies. Cuts cold
442
+ * first-paint from ~one full registration round-trip to ~one query.
443
+ * Defaults to `true`; set `false` to keep the old wait-for-registration path.
444
+ */
445
+ instantHydrate?: boolean;
446
+ /**
447
+ * Enable realtime sync while signed out. When `true`, the client starts its
448
+ * `_00_list_ref` poll (and a LIVE subscription) against the shared
449
+ * `_00_list_ref_anon` table even with no authenticated user, so a logged-out
450
+ * page gets live `useQuery` updates over world-readable tables. Requires the
451
+ * server to be deployed with `anonymousLiveQueries: true` in `sp00ky.yml`
452
+ * (this flag must match it). Defaults to `false`: anonymous clients can read
453
+ * one-shot but never sync live.
454
+ */
455
+ enableAnonymousLiveQueries?: boolean;
456
+ /**
457
+ * Surface sustained sync failures as a "degraded" health status that the app
458
+ * can observe via `subscribeToSyncHealth` (or the client-solid
459
+ * `useSyncStatus` hook) to render a "can't reach the server" banner.
460
+ *
461
+ * Individual failures — a transient remote 500 on query registration, a
462
+ * dropped WebSocket, etc. — are always swallowed and retried; they never
463
+ * throw at the app. This only controls when a *run* of consecutive failures
464
+ * is reported. Status flips back to `healthy` on the next successful sync
465
+ * round. Defaults to `{ degradeAfterConsecutiveFailures: 3 }`; pass `false`
466
+ * (or `degradeAfterConsecutiveFailures: 0`) to never report degraded.
467
+ */
468
+ syncHealth?: SyncHealthConfig | false;
469
+ }
470
+ /** Tunables for sync-health reporting. See {@link Sp00kyConfig.syncHealth}. */
471
+ interface SyncHealthConfig {
472
+ /**
473
+ * Number of consecutive failed sync rounds (up or down) before the status
474
+ * flips from `healthy` to `degraded`. A single transient failure is absorbed
475
+ * by the retry; only a sustained run trips the banner. Defaults to `3`. `0`
476
+ * disables degraded reporting entirely.
477
+ */
478
+ degradeAfterConsecutiveFailures?: number;
479
+ }
480
+ type SyncHealthStatus = 'healthy' | 'degraded';
481
+ /** Snapshot of sync health delivered to `subscribeToSyncHealth` subscribers. */
482
+ interface SyncHealth {
483
+ /** `'degraded'` once consecutive failures cross the configured threshold. */
484
+ status: SyncHealthStatus;
485
+ /** Consecutive failed sync rounds at the moment of this report. */
486
+ consecutiveFailures: number;
487
+ /** Classification of the most recent failure (only set while `degraded`). */
488
+ kind?: 'network' | 'application';
489
+ /** Message of the most recent failure (only set while `degraded`). */
490
+ error?: string;
491
+ /**
492
+ * `true` once at least one sync round has succeeded this session. Lets a UI
493
+ * distinguish a first-time "connecting" phase (never reached the server yet,
494
+ * so a cold-start failure run is expected) from a real lost connection after
495
+ * a working session. Never resets back to `false` once set.
496
+ */
497
+ everConnected: boolean;
498
+ }
499
+ type QueryHash = string;
500
+ type RecordVersionArray = Array<[string, number]>;
501
+ /**
502
+ * Represents the difference between two record version sets.
503
+ * Used for synchronizing local and remote states.
504
+ */
505
+ interface RecordVersionDiff {
506
+ /** List of records added. */
507
+ added: Array<{
508
+ id: RecordId$1<string>;
509
+ version: number;
510
+ }>;
511
+ /** List of records updated. */
512
+ updated: Array<{
513
+ id: RecordId$1<string>;
514
+ version: number;
515
+ }>;
516
+ /** List of record IDs removed. */
517
+ removed: RecordId$1<string>[];
518
+ }
519
+ /**
520
+ * Configuration for a specific query instance.
521
+ * Stores metadata about the query's state, parameters, and versioning.
522
+ */
523
+ interface QueryConfig {
524
+ /** The unique ID of the query config record. */
525
+ id: RecordId$1<string>;
526
+ /** The SURQL query string. */
527
+ surql: string;
528
+ /**
529
+ * Engine-neutral plan for `surql` (in-memory only; not persisted to
530
+ * `_00_query`). Present when the query came from the query-builder. Non-
531
+ * SurrealQL local engines (SQLite) materialize via `engine.select(plan)`
532
+ * instead of re-running `surql`, which they cannot parse.
533
+ */
534
+ plan?: QueryPlan;
535
+ /** Parameters used in the query. */
536
+ params: Record<string, any>;
537
+ /** The version array representing the local state of results. */
538
+ localArray: RecordVersionArray;
539
+ /** The version array representing the remote (server) state of results. */
540
+ remoteArray: RecordVersionArray;
541
+ /**
542
+ * In-memory only (never persisted to `_00_query`): version array of the
543
+ * subquery CHILD rows pulled via `parent IS NOT NONE` edges, so the
544
+ * child-body sync is idempotent across polls. Kept separate from
545
+ * `remoteArray` so related child rows never enter the primary window /
546
+ * `rowCount` / `localArray`.
547
+ */
548
+ subqueryRemoteArray?: RecordVersionArray;
549
+ /** Time-To-Live for this query. */
550
+ ttl: QueryTimeToLive;
551
+ /** Timestamp when the query was last accessed/active. */
552
+ lastActiveAt: Date;
553
+ /** The name of the table this query targets (if applicable). */
554
+ tableName: string;
555
+ }
556
+ type QueryConfigRecord = QueryConfig & {
557
+ id: string;
558
+ };
559
+ /**
560
+ * Runtime fetch status of a live query.
561
+ * - `idle`: registered, initial sync completed, and not currently fetching
562
+ * missing records — the materialized rows are authoritative (a windowed
563
+ * query's short result really is the end of the list).
564
+ * - `fetching`: the query is registering (a query is born `fetching` until its
565
+ * initial remote sync completes) or the sync engine is fetching/ingesting
566
+ * missing records for it. Any pending debounced result is flushed BEFORE the
567
+ * flip back to `idle`, so idle status never races ahead of the rows.
568
+ */
569
+ type QueryStatus = 'idle' | 'fetching';
570
+ /**
571
+ * Internal state of a live query.
572
+ */
573
+ interface QueryState {
574
+ /** The configuration for this query. */
575
+ config: QueryConfig;
576
+ /** The current cached records for this query. */
577
+ records: Record<string, any>[];
578
+ /** Set once `applyHydration` has run for this query, so the cold instant-hydrate
579
+ * path fires at most once per query (see DataModule.isCold/applyHydration). */
580
+ hydrated?: boolean;
581
+ /** Set once `notifyQuerySynced` has emitted for this registration lifetime.
582
+ * Ephemeral (unlike the persisted `updateCount`), so a re-registered query
583
+ * always emits at least once even when its records are unchanged — otherwise
584
+ * an empty re-registered window would never notify and stay "loading". */
585
+ syncNotified?: boolean;
586
+ /** Timer for TTL expiration. */
587
+ ttlTimer: NodeJS.Timeout | null;
588
+ /** TTL duration in milliseconds. */
589
+ ttlDurationMs: number;
590
+ /** Number of times the query has been updated. */
591
+ updateCount: number;
592
+ /** Timestamp (ms) of the last user-visible update, or null before the first
593
+ * one. Surfaced to DevTools as `lastUpdate` — must NOT be stamped on read. */
594
+ lastUpdatedAt: number | null;
595
+ /**
596
+ * Rolling window of the most recent materialization-step latencies (ms).
597
+ * Capped at MATERIALIZATION_SAMPLE_WINDOW; used to recompute p55/p90/p99
598
+ * before each persist to `_00_query`. Samples themselves are not persisted.
599
+ */
600
+ materializationSamples: number[];
601
+ /** Most recent end-to-end ingest latency in ms, or null until the first ingest. */
602
+ lastIngestLatencyMs: number | null;
603
+ /** Cumulative count of ingest/materialization errors observed for this query. */
604
+ errorCount: number;
605
+ /**
606
+ * Ephemeral runtime fetch status. Not persisted to `_00_query`; observable
607
+ * via DevTools and the `useQuery` hook. `fetching` while the sync engine is
608
+ * pulling missing records for this query, otherwise `idle`.
609
+ */
610
+ status: QueryStatus;
611
+ /**
612
+ * Rolling per-phase timing samples (ms), in addition to `materializationSamples`
613
+ * (which holds the SSP whole-ingest wall time). Keyed by `TimingPhase` minus
614
+ * `ssp`. Not persisted — surfaced live to DevTools + MCP via `phaseTimings`.
615
+ */
616
+ phaseSamples: Record<string, number[]>;
617
+ /** Most recent sample (ms) per phase, or null. */
618
+ phaseLast: Record<string, number | null>;
619
+ /** One-shot SSP registration timings (ms). */
620
+ registrationTimings: RegistrationTimings;
621
+ }
622
+ /** Cap on the rolling materialization-sample window kept per query in memory. */
623
+ declare const MATERIALIZATION_SAMPLE_WINDOW = 100;
624
+ /** Timed processing phases surfaced per query. `ssp` is the WASM-ingest wall
625
+ * time; the `ssp*` phases are its internal breakdown from the SSP binding. */
626
+ type TimingPhase = 'ssp' | 'sspStoreApply' | 'sspCircuitStep' | 'sspTransform' | 'localFetch' | 'remoteFetch' | 'frontend';
627
+ /** One-shot registration timings (ms), captured once when a query registers. */
628
+ interface RegistrationTimings {
629
+ /** SSP surql→plan parse + permission injection. */
630
+ parseMs: number | null;
631
+ /** SSP operator-DAG build. */
632
+ planMs: number | null;
633
+ /** SSP initial snapshot evaluation. */
634
+ snapshotMs: number | null;
635
+ /** Wall time of `cache.registerQuery` (register_view round-trip). */
636
+ wallMs: number | null;
637
+ }
638
+ /** Percentile summary for one timed phase, surfaced to DevTools + MCP. */
639
+ interface PhaseStat {
640
+ lastMs: number | null;
641
+ p50: number | null;
642
+ p90: number | null;
643
+ p99: number | null;
644
+ count: number;
645
+ }
646
+ /** Per-query processing-time breakdown surfaced via DevTools panel + MCP. */
647
+ interface QueryTimings {
648
+ ssp: PhaseStat;
649
+ sspStoreApply: PhaseStat;
650
+ sspCircuitStep: PhaseStat;
651
+ sspTransform: PhaseStat;
652
+ localFetch: PhaseStat;
653
+ remoteFetch: PhaseStat;
654
+ frontend: PhaseStat;
655
+ registration: RegistrationTimings;
656
+ updateCount: number;
657
+ errorCount: number;
658
+ }
659
+ type QueryUpdateCallback = (records: Record<string, any>[]) => void;
660
+ type QueryStatusCallback = (status: QueryStatus) => void;
661
+ type MutationCallback = (mutations: UpEvent[]) => void;
662
+ type MutationEventType = 'create' | 'update' | 'delete';
663
+ /**
664
+ * Represents a mutation event (create, update, delete) to be synchronized.
665
+ */
666
+ interface MutationEvent {
667
+ /** Example: 'create', 'update', or 'delete'. */
668
+ type: MutationEventType;
669
+ /** unique id of the mutation */
670
+ mutation_id: RecordId$1<string>;
671
+ /** The ID of the record being mutated. */
672
+ record_id: RecordId$1<string>;
673
+ /** The data payload for create/update operations. */
674
+ data?: any;
675
+ /** The full record data (optional context). */
676
+ record?: any;
677
+ /** Options for the mutation event (e.g., debounce settings). */
678
+ options?: PushEventOptions;
679
+ /** Timestamp when the event was created. */
680
+ createdAt: Date;
681
+ }
682
+ /**
683
+ * Options for run operations.
684
+ */
685
+ interface RunOptions {
686
+ assignedTo?: string;
687
+ max_retries?: number;
688
+ retry_strategy?: 'linear' | 'exponential';
689
+ /** Timeout in seconds for the backend HTTP call. Only used if the backend allows timeout override. */
690
+ timeout?: number;
691
+ /**
692
+ * Minimum delay in milliseconds before the job is eligible to run. While
693
+ * delayed the job stays pending (enqueued) and can still be killed.
694
+ */
695
+ delay?: number;
696
+ }
697
+ /**
698
+ * Options for update operations.
699
+ */
700
+ interface UpdateOptions {
701
+ /**
702
+ * Debounce configuration for the update.
703
+ * If boolean, enables default debounce behavior.
704
+ */
705
+ debounced?: boolean | DebounceOptions;
706
+ }
707
+ /**
708
+ * Configuration options for debouncing updates.
709
+ */
710
+ interface DebounceOptions {
711
+ /**
712
+ * The key to use for debouncing.
713
+ * - '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'.
714
+ * - 'recordId_x_fields': Debounce based on record ID and specific fields.
715
+ */
716
+ key?: 'recordId' | 'recordId_x_fields';
717
+ /** The debounce delay in milliseconds. */
718
+ delay?: number;
719
+ }
720
+ //#endregion
721
+ export { SyncHealthStatus as A, EventDefinition as B, RunOptions as C, StoreType as D, Sp00kyQueryResultPromise as E, SealedQuery as F, DatabaseEventSystem as I, DatabaseEventTypes as L, UpdateOptions as M, UpEvent as N, SyncHealth as O, LocalStore as P, Logger$1 as R, RegistrationTimings as S, Sp00kyQueryResult as T, EventSystem as V, QueryTimeToLive as _, MutationCallback as a, RecordVersionArray as b, PersistenceClient as c, QueryConfig as d, QueryConfigRecord as f, QueryStatusCallback as g, QueryStatus as h, MATERIALIZATION_SAMPLE_WINDOW as i, TimingPhase as j, SyncHealthConfig as k, PhaseStat as l, QueryState as m, EventSubscriptionOptions as n, MutationEvent as o, QueryHash as p, Level$1 as r, MutationEventType as s, DebounceOptions as t, PinoTransmit as u, QueryTimings as v, Sp00kyConfig as w, RecordVersionDiff as x, QueryUpdateCallback as y, SyncEventSystem as z };