@spooky-sync/core 0.0.1-canary.74 → 0.0.1-canary.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as Logger$1, C as RunOptions, D as StoreType, E as Sp00kyQueryResultPromise, M as EventSystem, O as TimingPhase, S as RegistrationTimings, T as Sp00kyQueryResult, _ as QueryTimeToLive, a as MutationCallback, b as RecordVersionArray, c as PersistenceClient, d as QueryConfig, f as QueryConfigRecord, g as QueryStatusCallback, h as QueryStatus, i as MATERIALIZATION_SAMPLE_WINDOW, j as EventDefinition, k as UpdateOptions, l as PhaseStat, m as QueryState, n as EventSubscriptionOptions, o as MutationEvent, p as QueryHash, r as Level, s as MutationEventType, t as DebounceOptions, u as PinoTransmit, v as QueryTimings, w as Sp00kyConfig, x as RecordVersionDiff, y as QueryUpdateCallback } from "./types.js";
1
+ import { A as UpEvent, C as RunOptions, D as StoreType, E as Sp00kyQueryResultPromise, M as SyncEventSystem, N as EventDefinition, O as TimingPhase, P as EventSystem, S as RegistrationTimings, T as Sp00kyQueryResult, _ as QueryTimeToLive, a as MutationCallback, b as RecordVersionArray, c as PersistenceClient, d as QueryConfig, f as QueryConfigRecord, g as QueryStatusCallback, h as QueryStatus, i as MATERIALIZATION_SAMPLE_WINDOW, j as Logger$1, k as UpdateOptions, l as PhaseStat, m as QueryState, n as EventSubscriptionOptions, o as MutationEvent, p as QueryHash, r as Level, s as MutationEventType, t as DebounceOptions, u as PinoTransmit, v as QueryTimings, w as Sp00kyConfig, x as RecordVersionDiff, y as QueryUpdateCallback } from "./types.js";
2
2
  import * as surrealdb0 from "surrealdb";
3
3
  import { Duration, RecordId, Surreal as Surreal$1, SurrealTransaction } from "surrealdb";
4
4
  import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
@@ -88,6 +88,33 @@ declare class RemoteDatabaseService extends AbstractDatabaseService {
88
88
  invalidate(): Promise<void>;
89
89
  }
90
90
  //#endregion
91
+ //#region src/modules/sync/queue/queue-down.d.ts
92
+ type RegisterEvent = {
93
+ type: 'register';
94
+ payload: {
95
+ hash: string;
96
+ };
97
+ };
98
+ type SyncEvent = {
99
+ type: 'sync';
100
+ payload: {
101
+ hash: string;
102
+ };
103
+ };
104
+ type HeartbeatEvent = {
105
+ type: 'heartbeat';
106
+ payload: {
107
+ hash: string;
108
+ };
109
+ };
110
+ type CleanupEvent = {
111
+ type: 'cleanup';
112
+ payload: {
113
+ hash: string;
114
+ };
115
+ };
116
+ type DownEvent = RegisterEvent | SyncEvent | HeartbeatEvent | CleanupEvent;
117
+ //#endregion
91
118
  //#region src/services/stream-processor/wasm-types.d.ts
92
119
  interface WasmStreamUpdate {
93
120
  query_id: string;
@@ -159,6 +186,7 @@ declare class StreamProcessorService {
159
186
  private receivers;
160
187
  private batching;
161
188
  private batchBuffer;
189
+ private sessionAuth;
162
190
  constructor(events: EventSystem<StreamProcessorEvents>, db: LocalDatabaseService, persistenceClient: PersistenceClient, logger: Logger);
163
191
  /**
164
192
  * Add a receiver for stream updates.
@@ -213,6 +241,17 @@ declare class StreamProcessorService {
213
241
  * non-`_00_` tables are default-denied and registration fails.
214
242
  */
215
243
  setPermissions(permissions: Record<string, string>): void;
244
+ /**
245
+ * Set the current session's auth identity for permission injection,
246
+ * mirroring the server's `fn::query::register`
247
+ * (`object::extend(params, { auth: { id: $auth.id }, access: $access })`).
248
+ * Stored as strings (empty when logged out) and applied to every
249
+ * `register_view` in {@link registerQueryPlan}. Must be set before a
250
+ * `$auth`-gated query registers (and re-set on auth state changes), or the
251
+ * in-browser SSP's `permission_inject` rejects it with
252
+ * "requires $auth but registration params lack it".
253
+ */
254
+ setSessionAuth(authId: string | null, access: string | null): void;
216
255
  saveState(): Promise<void>;
217
256
  /**
218
257
  * Ingest a record change into the processor.
@@ -232,6 +271,483 @@ declare class StreamProcessorService {
232
271
  private normalizeValue;
233
272
  }
234
273
  //#endregion
274
+ //#region src/modules/cache/types.d.ts
275
+ type RecordWithId = Record<string, any> & {
276
+ id: RecordId<string>;
277
+ };
278
+ interface QueryConfig$1 {
279
+ queryHash: string;
280
+ surql: string;
281
+ params: Record<string, any>;
282
+ ttl: QueryTimeToLive | Duration;
283
+ lastActiveAt: Date;
284
+ }
285
+ interface CacheRecord {
286
+ table: string;
287
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
288
+ record: RecordWithId;
289
+ version: number;
290
+ }
291
+ //#endregion
292
+ //#region src/modules/cache/index.d.ts
293
+ /**
294
+ * CacheModule - Centralized storage and DBSP ingestion
295
+ *
296
+ * Single responsibility: Handle all local storage operations and DBSP ingestion.
297
+ * This module acts as the bridge between data operations and persistence.
298
+ */
299
+ declare class CacheModule implements StreamUpdateReceiver {
300
+ private local;
301
+ private streamProcessor;
302
+ private logger;
303
+ private streamUpdateCallback;
304
+ private versionLookups;
305
+ constructor(local: LocalDatabaseService, streamProcessor: StreamProcessorService, streamUpdateCallback: (update: StreamUpdate) => void, logger: Logger$1);
306
+ /**
307
+ * Implements StreamUpdateReceiver interface
308
+ * Called directly by StreamProcessor when views change
309
+ */
310
+ onStreamUpdate(update: StreamUpdate): void;
311
+ lookup(recordId: string): number;
312
+ /**
313
+ * Save a single record to local DB and ingest into DBSP
314
+ * Used by mutations (create/update)
315
+ */
316
+ save(cacheRecord: CacheRecord, skipDbInsert?: boolean): Promise<void>;
317
+ /**
318
+ * Save multiple records in a batch
319
+ * More efficient than calling save() multiple times
320
+ * Used by sync operations
321
+ */
322
+ saveBatch(records: CacheRecord[], skipDbInsert?: boolean): Promise<void>;
323
+ /**
324
+ * Delete a record from local DB and ingest deletion into DBSP
325
+ */
326
+ delete(table: string, id: string, skipDbDelete?: boolean, recordData?: Record<string, any>): Promise<void>;
327
+ /**
328
+ * Register a query with DBSP to create a materialized view
329
+ * Returns the initial result array
330
+ */
331
+ registerQuery(config: QueryConfig$1): {
332
+ localArray: RecordVersionArray;
333
+ registrationTimings?: {
334
+ parseMs: number;
335
+ planMs: number;
336
+ snapshotMs: number;
337
+ };
338
+ };
339
+ /**
340
+ * Unregister a query from DBSP
341
+ */
342
+ unregisterQuery(queryHash: string): void;
343
+ }
344
+ //#endregion
345
+ //#region src/modules/data/index.d.ts
346
+ /**
347
+ * DataModule - Unified query and mutation management
348
+ *
349
+ * Merges the functionality of QueryManager and MutationManager.
350
+ * Uses CacheModule for all storage operations.
351
+ */
352
+ declare class DataModule<S extends SchemaStructure> {
353
+ private cache;
354
+ private local;
355
+ private schema;
356
+ private streamDebounceTime;
357
+ private activeQueries;
358
+ private pendingQueries;
359
+ private subscriptions;
360
+ private statusSubscriptions;
361
+ private mutationCallbacks;
362
+ private debounceTimers;
363
+ private logger;
364
+ /**
365
+ * Optional observer notified whenever a query's fetch status changes.
366
+ * Wired by Sp00kyClient to push status changes into DevTools. Kept as a
367
+ * settable field (rather than a constructor arg) because DevTools is
368
+ * constructed after DataModule.
369
+ */
370
+ onQueryStatusChange?: (hash: QueryHash, status: QueryStatus) => void;
371
+ /**
372
+ * Optional observer invoked when a still-subscribed query's TTL heartbeat
373
+ * fires (~90% of the TTL). Wired by Sp00kyClient to
374
+ * `Sp00kySync.heartbeatQuery`, which refreshes the remote `_00_query`
375
+ * row's `lastActiveAt` so an actively-watched query never expires. Settable
376
+ * field (not a constructor arg) because the sync engine is wired after
377
+ * DataModule is constructed — mirrors `onQueryStatusChange`.
378
+ */
379
+ onHeartbeat?: (hash: QueryHash) => void;
380
+ /**
381
+ * Optional hook fired by {@link deregisterQuery} when an opt-in query (e.g. a
382
+ * viewport-windowed list cancelling an off-screen window) loses its last
383
+ * subscriber. Wired by Sp00kyClient to enqueue a `cleanup` down-event, which
384
+ * tears the remote `_00_query` view down (releasing its `_00_list_ref` edges)
385
+ * instead of leaving it for the TTL sweep. The local view + state are freed in
386
+ * {@link finalizeDeregister} only after that remote delete, so a fast
387
+ * re-subscribe (scroll back) can abort/heal the teardown — see `cleanupQuery`.
388
+ */
389
+ onDeregister?: (hash: QueryHash) => void;
390
+ private sessionId;
391
+ private currentUserId;
392
+ constructor(cache: CacheModule, local: LocalDatabaseService, schema: S, logger: Logger$1, streamDebounceTime?: number);
393
+ init(sessionId: string): Promise<void>;
394
+ /**
395
+ * Update the session salt used in query-id hashing. Call this when the
396
+ * SurrealDB session changes (sign-in, sign-out, reconnect). Subsequently
397
+ * registered queries will get fresh, session-scoped IDs.
398
+ */
399
+ setSessionId(sessionId: string): void;
400
+ /**
401
+ * Update the authenticated user record id. Pass `null` on sign-out.
402
+ * Read by `Sp00kySync.listRefTable()` so the LIVE subscription and
403
+ * the poll route to the same per-user `_00_list_ref_user_<id>` the
404
+ * SSP writes to.
405
+ */
406
+ setCurrentUserId(userId: string | null): void;
407
+ /** Read-only view of the authenticated user id used for per-user
408
+ * `_00_list_ref` routing. Other modules consult this so they pick the
409
+ * same table name DataModule does. */
410
+ getCurrentUserId(): string | null;
411
+ /**
412
+ * Register a query and return its hash for subscriptions
413
+ */
414
+ query<T extends TableNames<S>>(tableName: T, surqlString: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<QueryHash>;
415
+ /**
416
+ * Subscribe to query updates
417
+ */
418
+ subscribe(queryHash: string, callback: QueryUpdateCallback, options?: {
419
+ immediate?: boolean;
420
+ }): () => void;
421
+ /**
422
+ * Subscribe to a query's fetch-status changes (idle/fetching).
423
+ * With `{ immediate: true }` the callback fires synchronously with the
424
+ * current status (defaults to `idle` if the query isn't registered yet).
425
+ */
426
+ subscribeStatus(queryHash: string, callback: QueryStatusCallback, options?: {
427
+ immediate?: boolean;
428
+ }): () => void;
429
+ /**
430
+ * Set a query's fetch status and notify status observers (DevTools +
431
+ * `subscribeStatus` listeners). No-op when the status is unchanged or the
432
+ * query is unknown.
433
+ */
434
+ setQueryStatus(queryHash: string, status: QueryStatus): void;
435
+ /**
436
+ * Subscribe to mutations (for sync)
437
+ */
438
+ onMutation(callback: MutationCallback): () => void;
439
+ /**
440
+ * Handle stream updates from DBSP (via CacheModule)
441
+ */
442
+ onStreamUpdate(update: StreamUpdate): Promise<void>;
443
+ private materializeRecords;
444
+ private processStreamUpdate;
445
+ /**
446
+ * Compute p55/p90/p99 from a rolling window of materialization samples.
447
+ * Returns nulls for any percentile that has no samples yet so SurrealDB
448
+ * `option<float>` columns stay NONE rather than 0 before the first ingest.
449
+ */
450
+ private computeMaterializationPercentiles;
451
+ /** Record a per-phase timing sample (ms) on a query's rolling window. */
452
+ private recordPhase;
453
+ /** Record the remote record-fetch time (ms) for a query. Called by the sync engine. */
454
+ recordRemoteFetch(hash: string, ms: number): void;
455
+ /**
456
+ * Record the frontend reconcile time (ms) for a query. Called from `useQuery`
457
+ * via `Sp00kyClient.reportFrontendTiming` after it applies an update to its store.
458
+ */
459
+ recordFrontendTiming(hash: string, ms: number): void;
460
+ /**
461
+ * Build the per-query processing-time breakdown surfaced to the DevTools panel
462
+ * and the MCP. `ssp` is the WASM-ingest wall time (from `materializationSamples`);
463
+ * the rest come from the per-phase rolling windows + one-shot registration timings.
464
+ */
465
+ phaseTimings(q: QueryState): QueryTimings;
466
+ /**
467
+ * Get query state (for sync and devtools)
468
+ */
469
+ getQueryByHash(hash: string): QueryState | undefined;
470
+ /**
471
+ * Cold-query guard for instant-hydrate: true when the query exists, hasn't been
472
+ * hydrated, and has NOT yet fetched its server result (`remoteArray` empty).
473
+ * We gate on `remoteArray`, not local `records`: a windowed query is often
474
+ * partially pre-seeded from the circuit (e.g. the dashboard's 5-row preview),
475
+ * but it still hasn't loaded its own full window from the server — so it should
476
+ * still hydrate. A warm re-subscribe (remoteArray already populated) is skipped.
477
+ */
478
+ isCold(hash: string): boolean;
479
+ /**
480
+ * Walk a hydrated record's fields and append any EMBEDDED child records to
481
+ * `batch` (recursing for nested related fields). An embedded child is a
482
+ * value that is itself a record — a non-null object whose `id` is a
483
+ * `RecordId` — or an array of such records (one-to-many vs one-to-one). A
484
+ * bare `RecordId` (a foreign-key reference) or any other value is skipped,
485
+ * so this never mistakes a FK column for an embedded body. Children are
486
+ * keyed by their own `record.id.table`, versioned by `_00_rv`, and cleaned
487
+ * to their table's real columns (which strips the alias/related fields).
488
+ * `seen` dedupes within the batch.
489
+ */
490
+ private collectEmbeddedChildren;
491
+ /**
492
+ * Instant-hydrate: ingest rows fetched one-shot from the remote (the query's own
493
+ * surql run directly) so the query DISPLAYS immediately, while the full realtime
494
+ * registration proceeds in the background. Ingests with versions (`_00_rv`) so the
495
+ * later `syncRecords` dedup skips re-pulling unchanged bodies, and seeds
496
+ * `remoteArray` so windowed queries materialize the correct window (no sparse
497
+ * local-circuit issue). Runs at most once per query (the `hydrated` flag).
498
+ */
499
+ applyHydration(hash: string, rows: RecordWithId[]): Promise<void>;
500
+ /** True while ≥1 live subscriber is watching this query (refcount guard). */
501
+ hasSubscribers(hash: string): boolean;
502
+ /**
503
+ * Opt-in eager teardown for a query whose LAST subscriber just left — used by
504
+ * viewport-windowed lists to cancel off-screen windows instead of leaving
505
+ * their remote views to expire on the TTL sweep. No-op while any subscriber
506
+ * remains (refcount). Only enqueues the remote cleanup here; the local WASM
507
+ * view + in-memory state are freed in {@link finalizeDeregister} after the
508
+ * remote delete completes, so a re-subscribe in between aborts/heals it.
509
+ *
510
+ * NOTE: most queries should NOT use this — the default keep-alive on
511
+ * unsubscribe avoids re-registration churn on navigation.
512
+ */
513
+ deregisterQuery(hash: string): void;
514
+ /**
515
+ * Final local teardown after the remote `_00_query` row was deleted: free the
516
+ * WASM view, heartbeat timer, debounce timer, and in-memory state. Caller
517
+ * (`cleanupQuery`) guarantees no subscriber remains.
518
+ */
519
+ finalizeDeregister(hash: string): void;
520
+ /**
521
+ * Get query state by id (for sync and devtools)
522
+ */
523
+ getQueryById(id: RecordId<string>): QueryState | undefined;
524
+ /**
525
+ * Get all active queries (for devtools)
526
+ */
527
+ getActiveQueries(): QueryState[];
528
+ getActiveQueryHashes(): QueryHash[];
529
+ updateQueryLocalArray(id: string, localArray: RecordVersionArray): Promise<void>;
530
+ updateQueryRemoteArray(hash: string, remoteArray: RecordVersionArray): Promise<void>;
531
+ /**
532
+ * Called after a query's initial sync completes.
533
+ * Ensures subscribers are notified even if no stream updates fired (e.g. empty result set).
534
+ */
535
+ notifyQuerySynced(queryHash: string): Promise<void>;
536
+ run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, data: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
537
+ /**
538
+ * Create a new record
539
+ */
540
+ create<T extends Record<string, unknown>>(id: string, data: T): Promise<T>;
541
+ /**
542
+ * Update an existing record
543
+ */
544
+ update<T extends Record<string, unknown>>(table: string, id: string, data: Partial<T>, options?: UpdateOptions): Promise<T>;
545
+ /**
546
+ * Delete a record
547
+ */
548
+ delete(table: string, id: string): Promise<void>;
549
+ /**
550
+ * Rollback a failed optimistic create by deleting the record locally
551
+ */
552
+ rollbackCreate(recordId: RecordId, tableName: string): Promise<void>;
553
+ /**
554
+ * Rollback a failed optimistic update by restoring the previous record state
555
+ */
556
+ rollbackUpdate(recordId: RecordId, tableName: string, beforeRecord: Record<string, unknown>): Promise<void>;
557
+ /**
558
+ * Remove a record from all active query states and notify subscribers
559
+ */
560
+ private removeRecordFromQueries;
561
+ private createAndRegisterQuery;
562
+ private createNewQuery;
563
+ private calculateHash;
564
+ private startTTLHeartbeat;
565
+ private replaceRecordInQueries;
566
+ }
567
+ /**
568
+ * Parse update options to generate push event options
569
+ */
570
+ //#endregion
571
+ //#region src/modules/sync/sync.d.ts
572
+ /**
573
+ * Tunables for `Sp00kySync` construction.
574
+ */
575
+ interface Sp00kySyncOptions {
576
+ /**
577
+ * Cadence (ms) for the `_00_list_ref` poll fallback that catches
578
+ * cross-session UPDATEs the LIVE-permission gap drops. Non-positive
579
+ * values fall back to the default; see
580
+ * {@link resolveListRefPollInterval}.
581
+ */
582
+ refSyncIntervalMs?: number;
583
+ }
584
+ /**
585
+ * The main synchronization engine for Sp00ky.
586
+ * Handles the bidirectional synchronization between the local database and the remote backend.
587
+ * Uses a queue-based architecture with 'up' (local to remote) and 'down' (remote to local) queues.
588
+ * @template S The schema structure type.
589
+ */
590
+ declare class Sp00kySync<S extends SchemaStructure> {
591
+ private local;
592
+ private remote;
593
+ private cache;
594
+ private dataModule;
595
+ private schema;
596
+ private upQueue;
597
+ private downQueue;
598
+ private isInit;
599
+ private logger;
600
+ private syncEngine;
601
+ /** Engine-level events (e.g. `SYNC_REMOTE_DATA_INGESTED`). Distinct
602
+ * from `this.events`, which carries Sp00kySync-level events like
603
+ * `SYNC_QUERY_UPDATED` and `SYNC_MUTATION_ROLLED_BACK`. */
604
+ get engineEvents(): SyncEventSystem;
605
+ private scheduler;
606
+ private wasDisconnected;
607
+ events: SyncEventSystem;
608
+ private currentUserId;
609
+ private refMode;
610
+ private currentLiveQueryUuid;
611
+ private liveQueryUnsubscribe;
612
+ private listRefPollTimer;
613
+ private listRefPollRunning;
614
+ readonly refSyncIntervalMs: number;
615
+ private listRefIdleStreak;
616
+ private stillRemoteStreaks;
617
+ private lastLiveEventAt;
618
+ private _liveRetryCount;
619
+ get liveRetryCount(): number;
620
+ get isSyncing(): boolean;
621
+ get pendingMutationCount(): number;
622
+ subscribeToPendingMutations(cb: (count: number) => void): () => void;
623
+ constructor(local: LocalDatabaseService, remote: RemoteDatabaseService, cache: CacheModule, dataModule: DataModule<S>, schema: S, logger: Logger$1, options?: Sp00kySyncOptions);
624
+ /**
625
+ * Initializes the synchronization system.
626
+ * Starts the scheduler and initiates the initial sync cycles.
627
+ * @throws Error if already initialized.
628
+ */
629
+ init(): Promise<void>;
630
+ /**
631
+ * Push the authenticated user's record id from the parent client's
632
+ * auth subscription. Tears down the existing `_00_list_ref` LIVE (if
633
+ * any) and re-registers it under the new user's dedicated table so
634
+ * SurrealDB binds the permission rule under the post-flip auth
635
+ * context. Pass `null` on sign-out.
636
+ *
637
+ * The dedicated `_00_list_ref_user_<id>` table is created lazily by
638
+ * the SSP when the first query registration arrives, which may be
639
+ * concurrent with this call. We retry the LIVE registration with a
640
+ * short backoff so a "table not found" race resolves without
641
+ * surfacing as a permanent auth-loading hang.
642
+ */
643
+ setCurrentUserId(userId: string | null): Promise<void>;
644
+ private startListRefPoll;
645
+ private stopListRefPoll;
646
+ /**
647
+ * One poll cycle: refetch `_00_list_ref` for every active query. Returns
648
+ * whether ANY query's remoteArray actually changed — the scheduler uses this
649
+ * to drive the adaptive idle backoff.
650
+ */
651
+ private pollListRefForActiveQueries;
652
+ /**
653
+ * Pull the upstream list_ref entries for `queryHash`, diff them
654
+ * against the local `remoteArray` cache, sync any added/updated rows
655
+ * through the SyncEngine, then persist the new remoteArray. This is
656
+ * the same shape `createRemoteQuery` does for its initial fetch and
657
+ * what `handleRemoteListRefChange` does per-LIVE-event — we reuse
658
+ * it on a timer as a fallback for missed LIVE notifications.
659
+ */
660
+ private refetchListRefForQuery;
661
+ /**
662
+ * Resolve the current `_00_list_ref` table name for the active auth
663
+ * context. Public so the `createRemoteQuery` initial-fetch path can
664
+ * read from the right per-user table.
665
+ *
666
+ * Reads the user id from `DataModule` rather than the local mirror,
667
+ * because `DataModule.setCurrentUserId` runs synchronously from the
668
+ * auth callback (before any `await`), whereas `sync.setCurrentUserId`
669
+ * is async — the userQuery's initial fetch can fire between those
670
+ * two points and we need the correct table name immediately.
671
+ */
672
+ listRefTable(): string;
673
+ private killRefLiveQuery;
674
+ private restartRefLiveQuery;
675
+ private subscribeToReconnect;
676
+ private startRefLiveQueries;
677
+ private handleRemoteListRefChange;
678
+ /**
679
+ * Handle a LIVE change to a SUBQUERY child edge (a `_00_list_ref` row with
680
+ * `parent` set) for a `.related()` query. Unlike primary rows, child rows
681
+ * must NOT touch the query's `localArray`/`remoteArray`/`rowCount`; we only
682
+ * keep the child BODY fresh in the local cache so the in-browser SSP's
683
+ * subquery-table dependency re-materializes the parent view.
684
+ *
685
+ * CREATE/UPDATE fetch+upsert the child body. DELETE is intentionally a
686
+ * no-op: a child leaving this query's set must not delete a body another
687
+ * query may still show (see `syncSubqueryChildren` deletion-safety note);
688
+ * a genuine record delete propagates via the normal delete path.
689
+ */
690
+ private handleRemoteSubqueryChange;
691
+ /**
692
+ * Enqueues a 'down' event (from remote to local) for processing.
693
+ * @param event The DownEvent to enqueue.
694
+ */
695
+ enqueueDownEvent(event: DownEvent): void;
696
+ private processUpEvent;
697
+ private handleRollback;
698
+ private processDownEvent;
699
+ /**
700
+ * Synchronizes a specific query by hash.
701
+ * Compares local and remote version arrays and fetches differences.
702
+ * @param hash The hash of the query to sync.
703
+ */
704
+ syncQuery(hash: string): Promise<void>;
705
+ /**
706
+ * Run a sync for a single query while reflecting its fetch status. Marks the
707
+ * query `fetching` for the duration when the diff actually pulls records
708
+ * (added/updated), then resets to `idle` in a `finally` so a failed sync
709
+ * never leaves a query stuck `fetching`. Part A's notification coalescing
710
+ * means the single resulting UI update lands after this completes.
711
+ */
712
+ private runSyncForQuery;
713
+ /**
714
+ * Record ids with a pending local DELETE in the outbox (`_00_pending_mutations`).
715
+ * Sync must not re-fetch/re-insert these — the remote delete is async, so the
716
+ * server's `_00_list_ref` still lists them until it's processed, and the diff
717
+ * would otherwise resurrect a just-deleted record.
718
+ */
719
+ private getPendingDeleteIds;
720
+ /**
721
+ * Enqueues a list of mutations (up events) to be sent to the remote.
722
+ * @param mutations Array of UpEvents (create/update/delete) to enqueue.
723
+ */
724
+ enqueueMutation(mutations: UpEvent[]): Promise<void>;
725
+ private registerQuery;
726
+ private createRemoteQuery;
727
+ /**
728
+ * Sync the BODIES of a `.related()` query's subquery child rows into the
729
+ * local cache, separately from the primary window array. The SSP writes
730
+ * each matched child as a `_00_list_ref` edge tagged `parent`/`parent_rel`;
731
+ * `buildSubqueryListRefSelect` pulls those `out`+`version` pairs (any
732
+ * nesting depth). We diff against the in-memory `subqueryRemoteArray` and
733
+ * fetch added/updated bodies through the SyncEngine — which `saveBatch`s
734
+ * them into the local DB AND the in-browser SSP, whose subquery-table
735
+ * dependency then re-materializes the parent view (no explicit notify).
736
+ *
737
+ * Deletion safety: we pass `removed: []` deliberately. A child body can be
738
+ * shared by other queries; letting `handleRemovedRecords` delete one that
739
+ * merely left THIS query's child set would clobber data another query still
740
+ * shows. Genuine record deletes flow through the normal delete path; a
741
+ * lingering orphan body is invisible (the correlated WHERE stops matching).
742
+ *
743
+ * Kept off `runSyncForQuery` on purpose so child fetches never flip the
744
+ * query to `fetching` or skew its DevTools timings.
745
+ */
746
+ private syncSubqueryChildren;
747
+ heartbeatQuery(queryHash: string): Promise<void>;
748
+ private cleanupQuery;
749
+ }
750
+ //#endregion
235
751
  //#region src/modules/auth/events/index.d.ts
236
752
  declare const AuthEventTypes: {
237
753
  readonly AuthStateChanged: "AUTH_STATE_CHANGED";
@@ -254,6 +770,13 @@ declare class AuthService<S extends SchemaStructure> {
254
770
  token: string | null;
255
771
  currentUser: any | null;
256
772
  isAuthenticated: boolean;
773
+ /**
774
+ * The record-access method name for the current session (e.g. `"account"`),
775
+ * derived from the token's `AC` claim. Consumed by the in-browser SSP's
776
+ * permission injection so `$access`-gated table predicates resolve locally,
777
+ * mirroring the server's `$access`. Null when logged out.
778
+ */
779
+ access: string | null;
257
780
  isLoading: boolean;
258
781
  private events;
259
782
  get eventSystem(): AuthEventSystem;
@@ -275,6 +798,9 @@ declare class AuthService<S extends SchemaStructure> {
275
798
  */
276
799
  signOut(): Promise<void>;
277
800
  private setSession;
801
+ /** Fallback when the token carries no `AC` claim: if the schema defines
802
+ * exactly one record-access method, assume the session used it. */
803
+ private defaultAccessName;
278
804
  signUp<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signup'>): Promise<void>;
279
805
  signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
280
806
  }
@@ -423,6 +949,54 @@ declare class CrdtManager {
423
949
  private assertCrdtField;
424
950
  }
425
951
  //#endregion
952
+ //#region src/modules/feature-flag/index.d.ts
953
+ interface FeatureFlagSnapshot {
954
+ variant: string | undefined;
955
+ payload: unknown | undefined;
956
+ }
957
+ interface FeatureFlagOptions {
958
+ fallback?: string;
959
+ ttl?: QueryTimeToLive;
960
+ }
961
+ declare class FeatureFlagHandle {
962
+ readonly key: string;
963
+ readonly fallback: string | undefined;
964
+ private latest;
965
+ private listeners;
966
+ private unsubscribeFn;
967
+ private onCloseFn;
968
+ private closed;
969
+ constructor(key: string, fallback: string | undefined);
970
+ attach(unsubscribe: () => void): void;
971
+ detach(): void;
972
+ set(snapshot: FeatureFlagSnapshot): void;
973
+ variant(): string | undefined;
974
+ payload<T = unknown>(): T | undefined;
975
+ enabled(): boolean;
976
+ subscribe(cb: (s: FeatureFlagSnapshot) => void): () => void;
977
+ onClose(cb: () => void): void;
978
+ close(): void;
979
+ }
980
+ interface FeatureFlagModuleDeps<S extends SchemaStructure> {
981
+ dataModule: DataModule<S>;
982
+ sync: Sp00kySync<S>;
983
+ auth: AuthService<S>;
984
+ logger: Logger$1;
985
+ }
986
+ declare class FeatureFlagModule<S extends SchemaStructure> {
987
+ private deps;
988
+ private logger;
989
+ private active;
990
+ private authUnsubscribe;
991
+ private lastUserId;
992
+ constructor(deps: FeatureFlagModuleDeps<S>);
993
+ init(): void;
994
+ feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
995
+ closeAll(): Promise<void>;
996
+ private refreshAll;
997
+ private register;
998
+ }
999
+ //#endregion
426
1000
  //#region src/sp00ky.d.ts
427
1001
  declare class BucketHandle {
428
1002
  private bucketName;
@@ -448,6 +1022,7 @@ declare class Sp00kyClient<S extends SchemaStructure> {
448
1022
  private sync;
449
1023
  private devTools;
450
1024
  private crdtManager;
1025
+ private featureFlags;
451
1026
  private logger;
452
1027
  auth: AuthService<S>;
453
1028
  streamProcessor: StreamProcessorService;
@@ -468,6 +1043,16 @@ declare class Sp00kyClient<S extends SchemaStructure> {
468
1043
  private setupCallbacks;
469
1044
  init(): Promise<void>;
470
1045
  close(): Promise<void>;
1046
+ /**
1047
+ * Subscribe to a feature flag for the current user. Returns a
1048
+ * `FeatureFlagHandle` whose `variant()`, `payload()` and `enabled()`
1049
+ * accessors reflect the latest assignment from `_00_user_feature`,
1050
+ * and whose `subscribe(cb)` fires whenever that assignment changes.
1051
+ *
1052
+ * Permissions are enforced by SurrealDB: a client can only ever see
1053
+ * its own row, and cannot create or modify assignments.
1054
+ */
1055
+ feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
471
1056
  authenticate(token: string): Promise<surrealdb0.Tokens>;
472
1057
  /**
473
1058
  * Open a CRDT field for collaborative editing.
@@ -538,4 +1123,4 @@ declare function textToHtml(text: string): string;
538
1123
  */
539
1124
 
540
1125
  //#endregion
541
- export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, CURSOR_COLORS, CrdtField, CrdtManager, DebounceOptions, EventSubscriptionOptions, Level, MATERIALIZATION_SAMPLE_WINDOW, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, Sp00kyClient, Sp00kyConfig, Sp00kyQueryResult, Sp00kyQueryResultPromise, StoreType, TimingPhase, UpdateOptions, createAuthEventSystem, cursorColorFromName, fileToUint8Array, textToHtml };
1126
+ export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, CURSOR_COLORS, CrdtField, CrdtManager, DebounceOptions, EventSubscriptionOptions, FeatureFlagHandle, FeatureFlagModule, type FeatureFlagOptions, type FeatureFlagSnapshot, Level, MATERIALIZATION_SAMPLE_WINDOW, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, Sp00kyClient, Sp00kyConfig, Sp00kyQueryResult, Sp00kyQueryResultPromise, StoreType, TimingPhase, UpdateOptions, createAuthEventSystem, cursorColorFromName, fileToUint8Array, textToHtml };