@spooky-sync/core 0.0.1-canary.15 → 0.0.1-canary.151

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 (101) hide show
  1. package/AGENTS.md +56 -0
  2. package/dist/index.d.ts +1192 -372
  3. package/dist/index.js +5772 -1270
  4. package/dist/otel/index.d.ts +21 -0
  5. package/dist/otel/index.js +86 -0
  6. package/dist/sqlite-plan-sql.js +194 -0
  7. package/dist/sqlite-worker.d.ts +1 -0
  8. package/dist/sqlite-worker.js +202 -0
  9. package/dist/types.d.ts +755 -0
  10. package/package.json +40 -8
  11. package/skills/sp00ky-core/SKILL.md +258 -0
  12. package/skills/sp00ky-core/references/auth.md +98 -0
  13. package/skills/sp00ky-core/references/config.md +76 -0
  14. package/src/build-globals.d.ts +12 -0
  15. package/src/events/events.test.ts +2 -1
  16. package/src/events/index.ts +3 -0
  17. package/src/index.ts +16 -2
  18. package/src/modules/app-release/index.test.ts +125 -0
  19. package/src/modules/app-release/index.ts +201 -0
  20. package/src/modules/auth/events/index.ts +2 -1
  21. package/src/modules/auth/index.ts +59 -20
  22. package/src/modules/cache/index.ts +77 -32
  23. package/src/modules/cache/types.ts +2 -2
  24. package/src/modules/crdt/crdt-field.ts +294 -0
  25. package/src/modules/crdt/crdt-hydration.test.ts +206 -0
  26. package/src/modules/crdt/index.ts +361 -0
  27. package/src/modules/crdt/loro-loader.ts +25 -0
  28. package/src/modules/data/data.hydration.test.ts +142 -0
  29. package/src/modules/data/data.rebind.test.ts +147 -0
  30. package/src/modules/data/data.run.test.ts +113 -0
  31. package/src/modules/data/data.status.test.ts +249 -0
  32. package/src/modules/data/index.ts +1134 -128
  33. package/src/modules/data/window-query.test.ts +52 -0
  34. package/src/modules/data/window-query.ts +154 -0
  35. package/src/modules/devtools/index.ts +191 -30
  36. package/src/modules/devtools/versions.test.ts +74 -0
  37. package/src/modules/devtools/versions.ts +81 -0
  38. package/src/modules/feature-flag/index.test.ts +120 -0
  39. package/src/modules/feature-flag/index.ts +209 -0
  40. package/src/modules/ref-tables.test.ts +91 -0
  41. package/src/modules/ref-tables.ts +88 -0
  42. package/src/modules/sync/engine.ts +101 -37
  43. package/src/modules/sync/events/index.ts +9 -2
  44. package/src/modules/sync/queue/queue-down.ts +12 -5
  45. package/src/modules/sync/queue/queue-up.ts +29 -14
  46. package/src/modules/sync/scheduler.pause.test.ts +109 -0
  47. package/src/modules/sync/scheduler.ts +73 -7
  48. package/src/modules/sync/sync.health.test.ts +149 -0
  49. package/src/modules/sync/sync.subquery.test.ts +82 -0
  50. package/src/modules/sync/sync.ts +1017 -62
  51. package/src/modules/sync/utils.test.ts +269 -2
  52. package/src/modules/sync/utils.ts +182 -17
  53. package/src/otel/index.ts +127 -0
  54. package/src/services/database/cache-engine.ts +143 -0
  55. package/src/services/database/database.ts +11 -11
  56. package/src/services/database/engine-factory.ts +32 -0
  57. package/src/services/database/events/index.ts +2 -1
  58. package/src/services/database/index.ts +6 -0
  59. package/src/services/database/local-migrator.ts +28 -27
  60. package/src/services/database/local.test.ts +64 -0
  61. package/src/services/database/local.ts +478 -67
  62. package/src/services/database/plan-render.test.ts +159 -0
  63. package/src/services/database/plan-render.ts +108 -0
  64. package/src/services/database/relation-resolver.test.ts +413 -0
  65. package/src/services/database/relation-resolver.ts +0 -0
  66. package/src/services/database/remote.ts +13 -13
  67. package/src/services/database/sqlite-cache-engine.test.ts +215 -0
  68. package/src/services/database/sqlite-cache-engine.ts +829 -0
  69. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  70. package/src/services/database/sqlite-plan-sql.ts +106 -0
  71. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  72. package/src/services/database/sqlite-select.test.ts +279 -0
  73. package/src/services/database/sqlite-select.ts +113 -0
  74. package/src/services/database/sqlite-worker.ts +160 -0
  75. package/src/services/database/surql-translate.ts +291 -0
  76. package/src/services/database/surreal-cache-engine.ts +139 -0
  77. package/src/services/logger/index.ts +6 -101
  78. package/src/services/persistence/localstorage.ts +2 -2
  79. package/src/services/persistence/resilient.ts +11 -4
  80. package/src/services/persistence/surrealdb.ts +10 -10
  81. package/src/services/stream-processor/index.ts +295 -38
  82. package/src/services/stream-processor/permissions.test.ts +47 -0
  83. package/src/services/stream-processor/permissions.ts +53 -0
  84. package/src/services/stream-processor/stream-processor.batch.test.ts +136 -0
  85. package/src/services/stream-processor/stream-processor.reset.test.ts +104 -0
  86. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  87. package/src/services/stream-processor/wasm-types.ts +18 -2
  88. package/src/sp00ky.auth-order.test.ts +92 -0
  89. package/src/sp00ky.init-query.test.ts +183 -0
  90. package/src/sp00ky.ts +1000 -0
  91. package/src/types.ts +267 -15
  92. package/src/utils/error-classification.test.ts +44 -0
  93. package/src/utils/error-classification.ts +7 -0
  94. package/src/utils/index.ts +35 -13
  95. package/src/utils/parser.ts +3 -2
  96. package/src/utils/semver.test.ts +32 -0
  97. package/src/utils/semver.ts +30 -0
  98. package/src/utils/surql.ts +24 -15
  99. package/src/utils/withRetry.test.ts +1 -1
  100. package/tsdown.config.ts +77 -1
  101. package/src/spooky.ts +0 -395
@@ -1,28 +1,34 @@
1
1
  import { RecordId, Duration } from 'surrealdb';
2
- import {
2
+ import type {
3
3
  SchemaStructure,
4
4
  TableNames,
5
5
  BackendNames,
6
6
  BackendRoutes,
7
7
  RoutePayload,
8
+ QueryPlan,
8
9
  } from '@spooky-sync/query-builder';
9
- import { LocalDatabaseService } from '../../services/database/index';
10
- import { CacheModule, RecordWithId } from '../cache/index';
11
- import { Logger } from '../../services/logger/index';
12
- import { StreamUpdate } from '../../services/stream-processor/index';
13
- import {
14
- MutationEvent,
10
+ import type { LocalStore } from '../../services/database/index';
11
+ import { StaleEpochError } from '../../services/database/index';
12
+ import type { CacheModule, RecordWithId, CacheRecord } from '../cache/index';
13
+ import type { Logger } from '../../services/logger/index';
14
+ import type { StreamUpdate } from '../../services/stream-processor/index';
15
+ import type {
15
16
  QueryConfig,
16
17
  QueryHash,
17
18
  QueryState,
19
+ QueryStatus,
20
+ QueryStatusCallback,
18
21
  QueryTimeToLive,
19
22
  QueryUpdateCallback,
20
23
  MutationCallback,
21
24
  RecordVersionArray,
22
25
  QueryConfigRecord,
23
26
  UpdateOptions,
24
- RunOptions,
25
- } from '../../types';
27
+ QueryTimings,
28
+ PhaseStat,
29
+ RegistrationTimings,
30
+ RunOptions} from '../../types';
31
+ import { MATERIALIZATION_SAMPLE_WINDOW } from '../../types';
26
32
  import {
27
33
  parseRecordIdString,
28
34
  extractIdPart,
@@ -31,11 +37,29 @@ import {
31
37
  withRetry,
32
38
  surql,
33
39
  parseParams,
40
+ cleanRecord,
34
41
  extractTablePart,
35
42
  generateId,
36
43
  } from '../../utils/index';
37
- import { CreateEvent, DeleteEvent, UpdateEvent } from '../sync/index';
38
- import { PushEventOptions } from '../../events/index';
44
+ import type { CreateEvent, DeleteEvent, UpdateEvent } from '../sync/index';
45
+ import type { PushEventOptions } from '../../events/index';
46
+ import { buildWindowMaterialization, buildWindowMaterializationPlan } from './window-query';
47
+
48
+ /** Push a timing sample (ms) into a rolling window, capped at the sample window. */
49
+ function pushSample(samples: number[], ms: number): void {
50
+ samples.push(ms);
51
+ if (samples.length > MATERIALIZATION_SAMPLE_WINDOW) samples.shift();
52
+ }
53
+
54
+ /** Build a {lastMs,p50,p90,p99,count} summary from a rolling sample window. */
55
+ function phaseStatOf(samples: number[], lastMs: number | null): PhaseStat {
56
+ if (samples.length === 0) {
57
+ return { lastMs, p50: null, p90: null, p99: null, count: 0 };
58
+ }
59
+ const sorted = [...samples].sort((a, b) => a - b);
60
+ const pick = (q: number) => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))]!;
61
+ return { lastMs, p50: pick(0.5), p90: pick(0.9), p99: pick(0.99), count: samples.length };
62
+ }
39
63
 
40
64
  /**
41
65
  * DataModule - Unified query and mutation management
@@ -47,22 +71,105 @@ export class DataModule<S extends SchemaStructure> {
47
71
  private activeQueries: Map<QueryHash, QueryState> = new Map();
48
72
  private pendingQueries: Map<QueryHash, Promise<QueryHash>> = new Map();
49
73
  private subscriptions: Map<QueryHash, Set<QueryUpdateCallback>> = new Map();
74
+ private statusSubscriptions: Map<QueryHash, Set<QueryStatusCallback>> = new Map();
50
75
  private mutationCallbacks: Set<MutationCallback> = new Set();
51
76
  private debounceTimers: Map<QueryHash, NodeJS.Timeout> = new Map();
77
+ // The update each debounce timer would process on its trailing edge. Kept in a
78
+ // map (not just the timer closure) so `flushPendingStreamUpdate` can process
79
+ // it early — the sync engine flushes before flipping a query to `idle`, so
80
+ // subscribers never observe idle status with stale (partial-window) rows.
81
+ private pendingStreamUpdates: Map<QueryHash, StreamUpdate> = new Map();
82
+ // Refcount of in-flight fetch cycles per query (registration + concurrent
83
+ // poll/LIVE sync rounds can overlap). Status flips to `fetching` on 0→1 and
84
+ // back to `idle` only on 1→0, so an inner cycle finishing can't emit a
85
+ // premature idle mid-registration.
86
+ private fetchDepth: Map<QueryHash, number> = new Map();
52
87
  private logger: Logger;
88
+ /**
89
+ * Optional observer notified whenever a query's fetch status changes.
90
+ * Wired by Sp00kyClient to push status changes into DevTools. Kept as a
91
+ * settable field (rather than a constructor arg) because DevTools is
92
+ * constructed after DataModule.
93
+ */
94
+ public onQueryStatusChange?: (hash: QueryHash, status: QueryStatus) => void;
95
+ /**
96
+ * Optional observer invoked when a still-subscribed query's TTL heartbeat
97
+ * fires (~90% of the TTL). Wired by Sp00kyClient to
98
+ * `Sp00kySync.heartbeatQuery`, which refreshes the remote `_00_query`
99
+ * row's `lastActiveAt` so an actively-watched query never expires. Settable
100
+ * field (not a constructor arg) because the sync engine is wired after
101
+ * DataModule is constructed — mirrors `onQueryStatusChange`.
102
+ */
103
+ public onHeartbeat?: (hash: QueryHash) => void;
104
+ /**
105
+ * Optional hook fired by {@link deregisterQuery} when an opt-in query (e.g. a
106
+ * viewport-windowed list cancelling an off-screen window) loses its last
107
+ * subscriber. Wired by Sp00kyClient to enqueue a `cleanup` down-event, which
108
+ * tears the remote `_00_query` view down (releasing its `_00_list_ref` edges)
109
+ * instead of leaving it for the TTL sweep. The local view + state are freed in
110
+ * {@link finalizeDeregister} only after that remote delete, so a fast
111
+ * re-subscribe (scroll back) can abort/heal the teardown — see `cleanupQuery`.
112
+ */
113
+ public onDeregister?: (hash: QueryHash) => void;
114
+ // Salt for query-id hashing. Set from SurrealDB's session::id() so two
115
+ // browser sessions registering the same logical query (same surql + params)
116
+ // don't collide on the same `_00_query` row — each session gets its own.
117
+ // Empty string until init(sessionId) is called.
118
+ private sessionId: string = '';
119
+ // Authenticated user record id (e.g. `"user:abc"`). Updated by
120
+ // `setCurrentUserId` from the auth subscription. null when
121
+ // unauthenticated. Consulted by `Sp00kySync.listRefTable()` so the
122
+ // poll and LIVE subscription target the same per-user
123
+ // `_00_list_ref_user_<id>` table the sync engine writes to.
124
+ private currentUserId: string | null = null;
53
125
 
54
126
  constructor(
55
127
  private cache: CacheModule,
56
- private local: LocalDatabaseService,
128
+ private local: LocalStore,
57
129
  private schema: S,
58
130
  logger: Logger,
59
- private streamDebounceTime: number = 100
131
+ // Client-side SSP aggregation throttle: coalesces the in-browser
132
+ // StreamProcessor's per-record stream updates per query before notifying
133
+ // readers, so a burst of synced records repaints the UI once per window
134
+ // rather than row-by-row. 50ms keeps it responsive while still batching the
135
+ // initial-sync stream. Override via `SyncedDbConfig.streamDebounceTime`.
136
+ private streamDebounceTime: number = 50
60
137
  ) {
61
138
  this.logger = logger.child({ service: 'DataModule' });
62
139
  }
63
140
 
64
- async init(): Promise<void> {
65
- this.logger.info({ Category: 'spooky-client::DataModule::init' }, 'DataModule initialized');
141
+ async init(sessionId: string): Promise<void> {
142
+ this.sessionId = sessionId;
143
+ this.logger.info(
144
+ { sessionId, Category: 'sp00ky-client::DataModule::init' },
145
+ 'DataModule initialized'
146
+ );
147
+ }
148
+
149
+ /**
150
+ * Update the session salt used in query-id hashing. Call this when the
151
+ * SurrealDB session changes (sign-in, sign-out, reconnect). Subsequently
152
+ * registered queries will get fresh, session-scoped IDs.
153
+ */
154
+ setSessionId(sessionId: string): void {
155
+ this.sessionId = sessionId;
156
+ }
157
+
158
+ /**
159
+ * Update the authenticated user record id. Pass `null` on sign-out.
160
+ * Read by `Sp00kySync.listRefTable()` so the LIVE subscription and
161
+ * the poll route to the same per-user `_00_list_ref_user_<id>` the
162
+ * SSP writes to.
163
+ */
164
+ setCurrentUserId(userId: string | null): void {
165
+ this.currentUserId = userId;
166
+ }
167
+
168
+ /** Read-only view of the authenticated user id used for per-user
169
+ * `_00_list_ref` routing. Other modules consult this so they pick the
170
+ * same table name DataModule does. */
171
+ getCurrentUserId(): string | null {
172
+ return this.currentUserId;
66
173
  }
67
174
 
68
175
  // ==================== QUERY MANAGEMENT ====================
@@ -74,19 +181,22 @@ export class DataModule<S extends SchemaStructure> {
74
181
  tableName: T,
75
182
  surqlString: string,
76
183
  params: Record<string, any>,
77
- ttl: QueryTimeToLive
184
+ ttl: QueryTimeToLive,
185
+ plan?: QueryPlan
78
186
  ): Promise<QueryHash> {
79
187
  const hash = await this.calculateHash({ surql: surqlString, params });
80
188
  this.logger.debug(
81
- { hash, Category: 'spooky-client::DataModule::query' },
189
+ { hash, Category: 'sp00ky-client::DataModule::query' },
82
190
  'Query Initialization: started'
83
191
  );
84
192
 
85
- const recordId = new RecordId('_spooky_query', hash);
193
+ // `_00_query` stays the single shared registration table in both
194
+ // ref-modes; the per-user split happens only on `_00_list_ref`.
195
+ const recordId = new RecordId('_00_query', hash);
86
196
 
87
197
  if (this.activeQueries.has(hash)) {
88
198
  this.logger.debug(
89
- { hash, Category: 'spooky-client::DataModule::query' },
199
+ { hash, Category: 'sp00ky-client::DataModule::query' },
90
200
  'Query Initialization: exists, returning'
91
201
  );
92
202
  return hash;
@@ -95,7 +205,7 @@ export class DataModule<S extends SchemaStructure> {
95
205
  // Another call is already creating this query — wait for it
96
206
  if (this.pendingQueries.has(hash)) {
97
207
  this.logger.debug(
98
- { hash, Category: 'spooky-client::DataModule::query' },
208
+ { hash, Category: 'sp00ky-client::DataModule::query' },
99
209
  'Query Initialization: pending, waiting for existing creation'
100
210
  );
101
211
  await this.pendingQueries.get(hash);
@@ -103,12 +213,12 @@ export class DataModule<S extends SchemaStructure> {
103
213
  }
104
214
 
105
215
  this.logger.debug(
106
- { hash, Category: 'spooky-client::DataModule::query' },
216
+ { hash, Category: 'sp00ky-client::DataModule::query' },
107
217
  'Query Initialization: not found, creating new query'
108
218
  );
109
219
 
110
220
  // Create the query and track the pending promise
111
- const promise = this.createAndRegisterQuery<T>(hash, recordId, surqlString, params, ttl, tableName);
221
+ const promise = this.createAndRegisterQuery<T>(hash, recordId, surqlString, params, ttl, tableName, plan);
112
222
  this.pendingQueries.set(hash, promise);
113
223
  try {
114
224
  await promise;
@@ -147,11 +257,95 @@ export class DataModule<S extends SchemaStructure> {
147
257
  subs.delete(callback);
148
258
  if (subs.size === 0) {
149
259
  this.subscriptions.delete(queryHash);
260
+ // NOTE: intentionally do NOT tear down the query / free its in-browser
261
+ // SSP view here. The subscriber-gated heartbeat (startTTLHeartbeat)
262
+ // already self-stops once subscribers hit 0, so an abandoned query
263
+ // stops being kept alive and the server's TTL sweep removes it. Freeing
264
+ // the local view on every last-unsubscribe caused re-registration churn
265
+ // on navigation (open A → leave → open A again re-registers), which is
266
+ // a flakiness risk for no real benefit — keep the local view resident.
150
267
  }
151
268
  }
152
269
  };
153
270
  }
154
271
 
272
+ /**
273
+ * Subscribe to a query's fetch-status changes (idle/fetching).
274
+ * With `{ immediate: true }` the callback fires synchronously with the
275
+ * current status (defaults to `idle` if the query isn't registered yet).
276
+ */
277
+ subscribeStatus(
278
+ queryHash: string,
279
+ callback: QueryStatusCallback,
280
+ options: { immediate?: boolean } = {}
281
+ ): () => void {
282
+ if (!this.statusSubscriptions.has(queryHash)) {
283
+ this.statusSubscriptions.set(queryHash, new Set());
284
+ }
285
+ this.statusSubscriptions.get(queryHash)?.add(callback);
286
+
287
+ if (options.immediate) {
288
+ callback(this.activeQueries.get(queryHash)?.status ?? 'idle');
289
+ }
290
+
291
+ return () => {
292
+ const subs = this.statusSubscriptions.get(queryHash);
293
+ if (subs) {
294
+ subs.delete(callback);
295
+ if (subs.size === 0) {
296
+ this.statusSubscriptions.delete(queryHash);
297
+ }
298
+ }
299
+ };
300
+ }
301
+
302
+ /**
303
+ * Set a query's fetch status and notify status observers (DevTools +
304
+ * `subscribeStatus` listeners). No-op when the status is unchanged or the
305
+ * query is unknown.
306
+ */
307
+ setQueryStatus(queryHash: string, status: QueryStatus): void {
308
+ const queryState = this.activeQueries.get(queryHash);
309
+ if (!queryState || queryState.status === status) {
310
+ return;
311
+ }
312
+ queryState.status = status;
313
+
314
+ this.onQueryStatusChange?.(queryHash, status);
315
+
316
+ const subs = this.statusSubscriptions.get(queryHash);
317
+ if (subs) {
318
+ for (const callback of subs) {
319
+ callback(status);
320
+ }
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Enter a fetch cycle for a query. Refcounted: registration and concurrent
326
+ * poll/LIVE sync rounds can overlap on the same hash, and only the OUTERMOST
327
+ * cycle may flip the status — 0→1 emits `fetching`, and `endFetching`'s 1→0
328
+ * emits `idle`. Always pair with `endFetching` in a `finally`.
329
+ */
330
+ beginFetching(queryHash: string): void {
331
+ const depth = this.fetchDepth.get(queryHash) ?? 0;
332
+ this.fetchDepth.set(queryHash, depth + 1);
333
+ if (depth === 0) {
334
+ this.setQueryStatus(queryHash, 'fetching');
335
+ }
336
+ }
337
+
338
+ /** Leave a fetch cycle started with {@link beginFetching}; emits `idle` on the last exit. */
339
+ endFetching(queryHash: string): void {
340
+ const depth = this.fetchDepth.get(queryHash) ?? 0;
341
+ if (depth <= 1) {
342
+ this.fetchDepth.delete(queryHash);
343
+ this.setQueryStatus(queryHash, 'idle');
344
+ return;
345
+ }
346
+ this.fetchDepth.set(queryHash, depth - 1);
347
+ }
348
+
155
349
  /**
156
350
  * Subscribe to mutations (for sync)
157
351
  */
@@ -168,67 +362,208 @@ export class DataModule<S extends SchemaStructure> {
168
362
  async onStreamUpdate(update: StreamUpdate): Promise<void> {
169
363
  const { queryHash, op } = update;
170
364
 
171
- // Only debounce UPDATE operations
172
- // CREATE and DELETE should propagate immediately
173
- if (op === 'UPDATE') {
174
- // Clear existing timer if any
175
- if (this.debounceTimers.has(queryHash)) {
176
- clearTimeout(this.debounceTimers.get(queryHash)!);
365
+ // DELETE propagates immediately — a removed row should disappear without
366
+ // waiting on a debounce.
367
+ //
368
+ // CREATE and UPDATE are coalesced per query on a trailing timer. A list's
369
+ // rows stream in from sync as many small `_00_list_ref` diffs, each its own
370
+ // `cache.saveBatch` → one stream update per chunk. `ingestMany` only
371
+ // coalesces records ingested synchronously together, so chunks spread over
372
+ // time each re-materialize and re-notify — a 50-row window can fire 30+
373
+ // updates as it fills. Each StreamUpdate carries the full materialized
374
+ // `localArray`, so the latest one already reflects every prior chunk: keep
375
+ // only it and fire once on the trailing edge, settling the query in a couple
376
+ // of notifications instead of one per chunk.
377
+ if (op === 'DELETE') {
378
+ const existing = this.debounceTimers.get(queryHash);
379
+ if (existing) {
380
+ clearTimeout(existing);
381
+ this.debounceTimers.delete(queryHash);
177
382
  }
383
+ // The DELETE update carries the full latest localArray, so the coalesced
384
+ // CREATE/UPDATE it supersedes is already reflected — drop it.
385
+ this.pendingStreamUpdates.delete(queryHash);
386
+ await this.processStreamUpdate(update);
387
+ return;
388
+ }
178
389
 
179
- // Set new timer
180
- const timer = setTimeout(async () => {
181
- this.debounceTimers.delete(queryHash);
182
- await this.processStreamUpdate(update);
183
- }, this.streamDebounceTime);
390
+ // Clear existing timer if any
391
+ if (this.debounceTimers.has(queryHash)) {
392
+ // oxlint-disable-next-line no-non-null-assertion -- guarded by .has() check above
393
+ clearTimeout(this.debounceTimers.get(queryHash)!);
394
+ }
184
395
 
185
- this.debounceTimers.set(queryHash, timer);
186
- } else {
187
- // CREATE and DELETE - process immediately
396
+ // Set new timer
397
+ this.pendingStreamUpdates.set(queryHash, update);
398
+ const timer = setTimeout(async () => {
399
+ this.debounceTimers.delete(queryHash);
400
+ this.pendingStreamUpdates.delete(queryHash);
188
401
  await this.processStreamUpdate(update);
402
+ }, this.streamDebounceTime);
403
+
404
+ this.debounceTimers.set(queryHash, timer);
405
+ }
406
+
407
+ /**
408
+ * Process a query's pending (debounced) stream update NOW instead of on the
409
+ * trailing edge. Called by the sync engine before it flips a query back to
410
+ * `idle`, so the status change never races ahead of the rows it fetched.
411
+ * No-op when nothing is pending. The pending entry is removed before the
412
+ * await so a concurrently-firing timer can't process it twice.
413
+ */
414
+ async flushPendingStreamUpdate(queryHash: string): Promise<void> {
415
+ const timer = this.debounceTimers.get(queryHash);
416
+ if (timer) {
417
+ clearTimeout(timer);
418
+ this.debounceTimers.delete(queryHash);
419
+ }
420
+ const pending = this.pendingStreamUpdates.get(queryHash);
421
+ if (!pending) return;
422
+ this.pendingStreamUpdates.delete(queryHash);
423
+ await this.processStreamUpdate(pending);
424
+ }
425
+
426
+ // Materialize a query's result rows from the local DB. For a windowed query
427
+ // (`LIMIT n START m`, m>0) the original surql is NOT re-run — re-applying
428
+ // `START m` against the shared local store would skip the window's own rows
429
+ // (sparse windowing) and return nothing. Instead we select the window's
430
+ // record id-set directly, preferring the server's `list_ref` (`remoteArray`,
431
+ // authoritative) over the in-browser SSP's view (`sspArray`), and re-apply
432
+ // the original ORDER BY for stable order.
433
+ private async materializeRecords(
434
+ queryState: QueryState,
435
+ sspArray?: Array<[string, number]>
436
+ ): Promise<Record<string, any>[]> {
437
+ const t0 = performance.now();
438
+ const plan = queryState.config.plan;
439
+ const windowMat = buildWindowMaterialization(queryState.config.surql);
440
+ let records: Record<string, any>[];
441
+ if (windowMat) {
442
+ const win =
443
+ (queryState.config.remoteArray?.length && queryState.config.remoteArray) ||
444
+ (sspArray?.length && sspArray) ||
445
+ queryState.config.localArray ||
446
+ [];
447
+ const winIds = win.map(([id]) => parseRecordIdString(id));
448
+ if (plan) {
449
+ // Engine-neutral window materialization: select exactly the id-set,
450
+ // keeping ORDER BY + relations. Works on any local engine.
451
+ const winPlan = buildWindowMaterializationPlan(plan, winIds) ?? { ...plan, ids: winIds };
452
+ records = await this.local.select(winPlan, queryState.config.params);
453
+ } else {
454
+ const [rows] = await this.local.query<[Record<string, any>[]]>(windowMat.query, {
455
+ ...queryState.config.params,
456
+ __win: winIds,
457
+ });
458
+ records = rows || [];
459
+ }
460
+ } else if (plan) {
461
+ records = await this.local.select(plan, queryState.config.params);
462
+ } else {
463
+ const [rows] = await this.local.query<[Record<string, any>[]]>(
464
+ queryState.config.surql,
465
+ queryState.config.params
466
+ );
467
+ records = rows || [];
189
468
  }
469
+ // Local SurrealDB record-fetch time → DevTools "localFetch" phase.
470
+ this.recordPhase(queryState, 'localFetch', performance.now() - t0);
471
+ return records;
190
472
  }
191
473
 
192
474
  private async processStreamUpdate(update: StreamUpdate): Promise<void> {
193
- const { queryHash, localArray } = update;
475
+ const { queryHash, localArray, materializationTimeMs } = update;
194
476
  const queryState = this.activeQueries.get(queryHash);
195
477
  if (!queryState) {
196
478
  this.logger.warn(
197
- { queryHash, Category: 'spooky-client::DataModule::onStreamUpdate' },
479
+ { queryHash, Category: 'sp00ky-client::DataModule::onStreamUpdate' },
198
480
  'Received update for unknown query. Skipping...'
199
481
  );
200
482
  return;
201
483
  }
202
484
 
203
- try {
204
- // Fetch updated records
205
- const [records] = await this.local.query<[Record<string, any>[]]>(
206
- queryState.config.surql,
207
- queryState.config.params
208
- );
485
+ // Update the rolling materialization-sample window before the work that
486
+ // could throw, so the percentiles still move when the downstream local
487
+ // query fails (the materialization step itself ran).
488
+ if (typeof materializationTimeMs === 'number') {
489
+ queryState.materializationSamples.push(materializationTimeMs);
490
+ if (queryState.materializationSamples.length > MATERIALIZATION_SAMPLE_WINDOW) {
491
+ queryState.materializationSamples.shift();
492
+ }
493
+ queryState.lastIngestLatencyMs = materializationTimeMs;
494
+ }
495
+ // Record the SSP internal sub-phase timings (from the WASM binding) so
496
+ // DevTools can attribute ingest cost to store-apply vs circuit-step vs transform.
497
+ if (typeof update.storeApplyMs === 'number')
498
+ this.recordPhase(queryState, 'sspStoreApply', update.storeApplyMs);
499
+ if (typeof update.circuitStepMs === 'number')
500
+ this.recordPhase(queryState, 'sspCircuitStep', update.circuitStepMs);
501
+ if (typeof update.transformMs === 'number')
502
+ this.recordPhase(queryState, 'sspTransform', update.transformMs);
503
+ const percentiles = this.computeMaterializationPercentiles(queryState.materializationSamples);
504
+
505
+ // Fence against bucket switches: this update's `localArray` came from the
506
+ // pre-switch SSP circuit; applying it after a switch would show (and
507
+ // persist) the previous user's ids in the new bucket.
508
+ const epoch = this.local.epoch;
209
509
 
210
- // Update state
211
- const newRecords = records || [];
510
+ try {
511
+ // Materialize the query's rows. For a windowed (offset) query, re-running
512
+ // the original surql would re-apply `START n` against the shared local DB
513
+ // and skip the window's rows entirely; instead select the SSP's
514
+ // materialized window id-set (`localArray`) directly, re-applying the
515
+ // original ORDER BY for stable display order. Non-offset queries keep the
516
+ // normal re-query path.
517
+ const newRecords = await this.materializeRecords(queryState, localArray);
518
+ if (epoch !== this.local.epoch) return;
212
519
  queryState.config.localArray = localArray;
213
- await this.local.query(surql.seal(surql.updateSet('id', ['localArray'])), {
214
- id: queryState.config.id,
215
- localArray,
216
- });
217
520
 
218
- // Skip notification if records haven't changed
219
521
  const prevJson = JSON.stringify(queryState.records);
220
522
  const newJson = JSON.stringify(newRecords);
221
523
  queryState.records = newRecords;
222
- if (prevJson === newJson) {
524
+ const recordsChanged = prevJson !== newJson;
525
+
526
+ // updateCount counts user-visible updates (matches the prior semantic),
527
+ // while the materialization sample/percentiles already moved above for
528
+ // every observed engine step.
529
+ if (recordsChanged) {
530
+ queryState.updateCount++;
531
+ queryState.lastUpdatedAt = Date.now();
532
+ }
533
+
534
+ await this.local.query(
535
+ surql.seal(
536
+ surql.updateSet('id', [
537
+ 'localArray',
538
+ 'rowCount',
539
+ 'updateCount',
540
+ 'lastIngestLatency',
541
+ 'materializationP55',
542
+ 'materializationP90',
543
+ 'materializationP99',
544
+ ])
545
+ ),
546
+ {
547
+ id: queryState.config.id,
548
+ localArray,
549
+ rowCount: localArray.length,
550
+ updateCount: queryState.updateCount,
551
+ lastIngestLatency: queryState.lastIngestLatencyMs,
552
+ materializationP55: percentiles.p55,
553
+ materializationP90: percentiles.p90,
554
+ materializationP99: percentiles.p99,
555
+ },
556
+ { epoch }
557
+ );
558
+
559
+ if (!recordsChanged) {
223
560
  this.logger.debug(
224
- { queryHash, Category: 'spooky-client::DataModule::onStreamUpdate' },
561
+ { queryHash, Category: 'sp00ky-client::DataModule::onStreamUpdate' },
225
562
  'Query records unchanged, skipping notification'
226
563
  );
227
564
  return;
228
565
  }
229
566
 
230
- queryState.updateCount++;
231
-
232
567
  // Notify subscribers
233
568
  const subscribers = this.subscriptions.get(queryHash);
234
569
  if (subscribers) {
@@ -240,19 +575,110 @@ export class DataModule<S extends SchemaStructure> {
240
575
  this.logger.debug(
241
576
  {
242
577
  queryHash,
243
- recordCount: records?.length,
244
- Category: 'spooky-client::DataModule::onStreamUpdate',
578
+ recordCount: newRecords?.length,
579
+ Category: 'sp00ky-client::DataModule::onStreamUpdate',
245
580
  },
246
581
  'Query updated from stream'
247
582
  );
248
583
  } catch (err) {
584
+ if (err instanceof StaleEpochError) {
585
+ this.logger.debug(
586
+ { queryHash, Category: 'sp00ky-client::DataModule::onStreamUpdate' },
587
+ 'Dropped stream update from before a bucket switch'
588
+ );
589
+ return;
590
+ }
591
+ queryState.errorCount++;
249
592
  this.logger.error(
250
- { err, queryHash, Category: 'spooky-client::DataModule::onStreamUpdate' },
593
+ { err, queryHash, Category: 'sp00ky-client::DataModule::onStreamUpdate' },
251
594
  'Failed to fetch records for stream update'
252
595
  );
596
+ // Best-effort persist of the bumped errorCount; swallow secondary
597
+ // failures to avoid masking the original error in logs.
598
+ try {
599
+ await this.local.query(surql.seal(surql.updateSet('id', ['errorCount'])), {
600
+ id: queryState.config.id,
601
+ errorCount: queryState.errorCount,
602
+ });
603
+ } catch (persistErr) {
604
+ this.logger.warn(
605
+ {
606
+ err: persistErr,
607
+ queryHash,
608
+ Category: 'sp00ky-client::DataModule::onStreamUpdate',
609
+ },
610
+ 'Failed to persist incremented errorCount'
611
+ );
612
+ }
253
613
  }
254
614
  }
255
615
 
616
+ /**
617
+ * Compute p55/p90/p99 from a rolling window of materialization samples.
618
+ * Returns nulls for any percentile that has no samples yet so SurrealDB
619
+ * `option<float>` columns stay NONE rather than 0 before the first ingest.
620
+ */
621
+ private computeMaterializationPercentiles(samples: number[]): {
622
+ p55: number | null;
623
+ p90: number | null;
624
+ p99: number | null;
625
+ } {
626
+ if (samples.length === 0) {
627
+ return { p55: null, p90: null, p99: null };
628
+ }
629
+ const sorted = [...samples].sort((a, b) => a - b);
630
+ const pick = (q: number) => {
631
+ const idx = Math.min(sorted.length - 1, Math.floor(q * sorted.length));
632
+ return sorted[idx]!;
633
+ };
634
+ return { p55: pick(0.55), p90: pick(0.90), p99: pick(0.99) };
635
+ }
636
+
637
+ /** Record a per-phase timing sample (ms) on a query's rolling window. */
638
+ private recordPhase(qs: QueryState, phase: string, ms: number): void {
639
+ if (!Number.isFinite(ms)) return;
640
+ const arr = qs.phaseSamples[phase] ?? (qs.phaseSamples[phase] = []);
641
+ pushSample(arr, ms);
642
+ qs.phaseLast[phase] = ms;
643
+ }
644
+
645
+ /** Record the remote record-fetch time (ms) for a query. Called by the sync engine. */
646
+ recordRemoteFetch(hash: string, ms: number): void {
647
+ const qs = this.activeQueries.get(hash);
648
+ if (qs) this.recordPhase(qs, 'remoteFetch', ms);
649
+ }
650
+
651
+ /**
652
+ * Record the frontend reconcile time (ms) for a query. Called from `useQuery`
653
+ * via `Sp00kyClient.reportFrontendTiming` after it applies an update to its store.
654
+ */
655
+ recordFrontendTiming(hash: string, ms: number): void {
656
+ const qs = this.activeQueries.get(hash);
657
+ if (qs) this.recordPhase(qs, 'frontend', ms);
658
+ }
659
+
660
+ /**
661
+ * Build the per-query processing-time breakdown surfaced to the DevTools panel
662
+ * and the MCP. `ssp` is the WASM-ingest wall time (from `materializationSamples`);
663
+ * the rest come from the per-phase rolling windows + one-shot registration timings.
664
+ */
665
+ phaseTimings(q: QueryState): QueryTimings {
666
+ const stat = (phase: string) =>
667
+ phaseStatOf(q.phaseSamples[phase] ?? [], q.phaseLast[phase] ?? null);
668
+ return {
669
+ ssp: phaseStatOf(q.materializationSamples, q.lastIngestLatencyMs),
670
+ sspStoreApply: stat('sspStoreApply'),
671
+ sspCircuitStep: stat('sspCircuitStep'),
672
+ sspTransform: stat('sspTransform'),
673
+ localFetch: stat('localFetch'),
674
+ remoteFetch: stat('remoteFetch'),
675
+ frontend: stat('frontend'),
676
+ registration: q.registrationTimings,
677
+ updateCount: q.updateCount,
678
+ errorCount: q.errorCount,
679
+ };
680
+ }
681
+
256
682
  /**
257
683
  * Get query state (for sync and devtools)
258
684
  */
@@ -260,6 +686,268 @@ export class DataModule<S extends SchemaStructure> {
260
686
  return this.activeQueries.get(hash);
261
687
  }
262
688
 
689
+ /**
690
+ * Cold-query guard for instant-hydrate: true when the query exists, hasn't been
691
+ * hydrated, and has NOT yet fetched its server result (`remoteArray` empty).
692
+ * We gate on `remoteArray`, not local `records`: a windowed query is often
693
+ * partially pre-seeded from the circuit (e.g. the dashboard's 5-row preview),
694
+ * but it still hasn't loaded its own full window from the server — so it should
695
+ * still hydrate. A warm re-subscribe (remoteArray already populated) is skipped.
696
+ */
697
+ isCold(hash: string): boolean {
698
+ const qs = this.activeQueries.get(hash);
699
+ return !!qs && !qs.hydrated && (qs.config.remoteArray?.length ?? 0) === 0;
700
+ }
701
+
702
+ /**
703
+ * Walk a hydrated record's fields and append any EMBEDDED child records to
704
+ * `batch` (recursing for nested related fields). An embedded child is a
705
+ * value that is itself a record — a non-null object whose `id` is a
706
+ * `RecordId` — or an array of such records (one-to-many vs one-to-one). A
707
+ * bare `RecordId` (a foreign-key reference) or any other value is skipped,
708
+ * so this never mistakes a FK column for an embedded body. Children are
709
+ * keyed by their own `record.id.table`, versioned by `_00_rv`, and cleaned
710
+ * to their table's real columns (which strips the alias/related fields).
711
+ * `seen` dedupes within the batch.
712
+ */
713
+ private collectEmbeddedChildren(
714
+ record: Record<string, any>,
715
+ batch: CacheRecord[],
716
+ seen: Set<string>
717
+ ): void {
718
+ const isEmbeddedRecord = (v: unknown): v is RecordWithId =>
719
+ !!v &&
720
+ typeof v === 'object' &&
721
+ !(v instanceof RecordId) &&
722
+ (v as { id?: unknown }).id instanceof RecordId;
723
+
724
+ for (const value of Object.values(record)) {
725
+ const children = Array.isArray(value)
726
+ ? value.filter(isEmbeddedRecord)
727
+ : isEmbeddedRecord(value)
728
+ ? [value]
729
+ : [];
730
+ for (const child of children) {
731
+ const key = encodeRecordId(child.id);
732
+ if (seen.has(key)) continue;
733
+ seen.add(key);
734
+ // Recurse FIRST so nested grandchildren are captured before `cleanRecord`
735
+ // strips this child's alias fields.
736
+ this.collectEmbeddedChildren(child, batch, seen);
737
+ const table = child.id.table.toString();
738
+ const tableSchema = this.schema.tables.find((t) => t.name === table);
739
+ batch.push({
740
+ table,
741
+ op: 'CREATE',
742
+ // Flatten the child too: a nested comment carries its own embedded
743
+ // `author` object which the schemafull `record<user>` field would
744
+ // reject. (Its author is captured as a separate row by the recursion
745
+ // above.)
746
+ record: this.flattenRelationsForStorage(child, tableSchema),
747
+ version: (child._00_rv as number) || 1,
748
+ });
749
+ }
750
+ }
751
+ }
752
+
753
+ /**
754
+ * Prepare a subquery-bearing row (preload / hydration) for the schemafull
755
+ * local store: replace an embedded FORWARD-relation object (`author = { id, … }`)
756
+ * with its RecordId so a `record<…>` field coerces, and DROP reverse-subquery
757
+ * ARRAYS (`comments = [ … ]`) since their rows are cached separately as their
758
+ * own bodies. A flat record — as the live `SELECT * FROM $ids` sync returns,
759
+ * with relations already RecordIds — passes through unchanged.
760
+ */
761
+ private flattenRelationsForStorage(
762
+ record: Record<string, any>,
763
+ tableSchema?: { columns: Record<string, any> }
764
+ ): RecordWithId {
765
+ const isEmbeddedRecord = (v: unknown): boolean =>
766
+ !!v &&
767
+ typeof v === 'object' &&
768
+ !(v instanceof RecordId) &&
769
+ (v as { id?: unknown }).id instanceof RecordId;
770
+ const flat: Record<string, any> = {};
771
+ for (const [k, v] of Object.entries(record)) {
772
+ if (isEmbeddedRecord(v)) {
773
+ flat[k] = (v as { id: unknown }).id;
774
+ } else if (Array.isArray(v) && v.some(isEmbeddedRecord)) {
775
+ continue;
776
+ } else {
777
+ flat[k] = v;
778
+ }
779
+ }
780
+ return (tableSchema ? cleanRecord(tableSchema.columns, flat) : flat) as RecordWithId;
781
+ }
782
+
783
+ /**
784
+ * Instant-hydrate: ingest rows fetched one-shot from the remote (the query's own
785
+ * surql run directly) so the query DISPLAYS immediately, while the full realtime
786
+ * registration proceeds in the background. Ingests with versions (`_00_rv`) so the
787
+ * later `syncRecords` dedup skips re-pulling unchanged bodies, and seeds
788
+ * `remoteArray` so windowed queries materialize the correct window (no sparse
789
+ * local-circuit issue). Runs at most once per query (the `hydrated` flag).
790
+ */
791
+ async applyHydration(hash: string, rows: RecordWithId[]): Promise<void> {
792
+ const queryState = this.activeQueries.get(hash);
793
+ if (!queryState) return;
794
+ queryState.hydrated = true; // run-once, even when the remote returns nothing
795
+ if (rows.length === 0) return;
796
+
797
+ const epoch = this.local.epoch;
798
+ const tableName = queryState.config.tableName;
799
+ await this.buildAndSaveCacheBatch(tableName, rows);
800
+ // Bucket switched while we persisted: these rows were fetched under the
801
+ // previous auth context — don't let them prime the new bucket's query
802
+ // state; the rebind's re-registration refills it. (saveBatch's own epoch
803
+ // fence usually catches this, but the switch can land between it and here.)
804
+ if (epoch !== this.local.epoch) return;
805
+
806
+ // Prime remoteArray from the hydrated id+version pairs: `materializeRecords`
807
+ // prefers it for windowed queries (correct window) and it feeds the version
808
+ // dedup. Registration later overwrites it with the authoritative `_00_list_ref`.
809
+ queryState.config.remoteArray = rows.map(
810
+ (r) => [encodeRecordId(r.id), (r._00_rv as number) || 1] as [string, number]
811
+ );
812
+
813
+ queryState.records = await this.materializeRecords(queryState);
814
+ const subscribers = this.subscriptions.get(hash);
815
+ if (subscribers) {
816
+ for (const cb of subscribers) cb(queryState.records);
817
+ }
818
+ }
819
+
820
+ /**
821
+ * Build the cache batch for a set of one-shot rows and persist it to the
822
+ * local DB + in-browser SSP. Maps each row to a `CREATE` op on its own table
823
+ * and extracts EMBEDDED related children (any nesting depth) as their own
824
+ * records — a `.related()` query returns its children embedded, and a later
825
+ * correlated re-materialization needs them present as standalone rows.
826
+ * Shared by `applyHydration` (live registration) and `persistSnapshot`
827
+ * (preload).
828
+ */
829
+ private async buildAndSaveCacheBatch(
830
+ tableName: string,
831
+ rows: RecordWithId[]
832
+ ): Promise<void> {
833
+ const tableSchema = this.schema.tables.find((t) => t.name === tableName);
834
+ const batch: CacheRecord[] = rows.map((record) => ({
835
+ table: tableName,
836
+ op: 'CREATE' as const,
837
+ // Flatten embedded relations first: a preload/hydration row can carry a
838
+ // forward relation as a full nested OBJECT (`author = { id, … }`) and a
839
+ // reverse subquery as an array of objects (`comments = [ … ]`). The
840
+ // schemafull local field is `record<user>`, which rejects an object
841
+ // (`Couldn't coerce … found { id: …, username: … }`) and would throw the
842
+ // WHOLE batch. Store the parent with `author` as its RecordId and drop the
843
+ // subquery arrays — the children are cached as their own rows below.
844
+ record: this.flattenRelationsForStorage(record, tableSchema),
845
+ version: (record._00_rv as number) || 1,
846
+ }));
847
+
848
+ const seen = new Set<string>(rows.map((r) => encodeRecordId(r.id)));
849
+ for (const record of rows) {
850
+ this.collectEmbeddedChildren(record, batch, seen);
851
+ }
852
+
853
+ await this.cache.saveBatch(batch);
854
+ }
855
+
856
+ /**
857
+ * Preload/prewarm: persist one-shot rows (and their embedded related children)
858
+ * into the local cache WITHOUT registering a query — no `activeQueries` entry,
859
+ * no `_00_query` view, no TTL heartbeat. The rows live in the local DB as
860
+ * ordinary bodies (never GC'd on their own) so a later `useQuery` seeds its
861
+ * first paint from them instantly, then registers a live view to freshen.
862
+ */
863
+ async persistSnapshot(tableName: string, rows: RecordWithId[]): Promise<void> {
864
+ if (rows.length === 0) return;
865
+ await this.buildAndSaveCacheBatch(tableName, rows);
866
+ }
867
+
868
+ /**
869
+ * Read the durable preload freshness marker for a query hash, or null if this
870
+ * query was never preloaded in the current bucket. Co-located with the cached
871
+ * rows (per-bucket `_00_preload` table) so a bucket switch that clears the
872
+ * data also clears the marker — a stale marker can't claim "warm" when the
873
+ * rows are gone. Any read error is treated as cold.
874
+ */
875
+ async getPreloadMarker(
876
+ hash: string
877
+ ): Promise<{ fetchedAt: number; rowCount: number } | null> {
878
+ try {
879
+ // Pass a real RecordId: a bare string id hits the SurrealDB engine's
880
+ // `FROM ONLY $__id` as a plain string, which "selects" the string itself
881
+ // (a truthy non-row) instead of the record — misread as a warm marker.
882
+ const row = await this.local.getById('_00_preload', new RecordId('_00_preload', hash));
883
+ if (!row || typeof row !== 'object') return null;
884
+ return {
885
+ fetchedAt: Number((row as any).fetchedAt) || 0,
886
+ rowCount: Number((row as any).rowCount) || 0,
887
+ };
888
+ } catch {
889
+ return null;
890
+ }
891
+ }
892
+
893
+ /** Stamp the preload freshness marker after a successful snapshot fetch. */
894
+ async writePreloadMarker(hash: string, rowCount: number): Promise<void> {
895
+ // RecordId, not a bare string: the SurrealDB engine binds the id verbatim,
896
+ // and `UPSERT <string>` is an InternalError — the marker silently never
897
+ // landed (the write is awaited inside preload's best-effort catch).
898
+ await this.local.upsert(
899
+ '_00_preload',
900
+ new RecordId('_00_preload', hash),
901
+ { fetchedAt: Date.now(), rowCount },
902
+ 'replace'
903
+ );
904
+ }
905
+
906
+ /** True while ≥1 live subscriber is watching this query (refcount guard). */
907
+ hasSubscribers(hash: string): boolean {
908
+ return (this.subscriptions.get(hash)?.size ?? 0) > 0;
909
+ }
910
+
911
+ /**
912
+ * Opt-in eager teardown for a query whose LAST subscriber just left — used by
913
+ * viewport-windowed lists to cancel off-screen windows instead of leaving
914
+ * their remote views to expire on the TTL sweep. No-op while any subscriber
915
+ * remains (refcount). Only enqueues the remote cleanup here; the local WASM
916
+ * view + in-memory state are freed in {@link finalizeDeregister} after the
917
+ * remote delete completes, so a re-subscribe in between aborts/heals it.
918
+ *
919
+ * NOTE: most queries should NOT use this — the default keep-alive on
920
+ * unsubscribe avoids re-registration churn on navigation.
921
+ */
922
+ deregisterQuery(hash: string): void {
923
+ if (this.hasSubscribers(hash)) return;
924
+ if (!this.activeQueries.has(hash)) return;
925
+ this.onDeregister?.(hash);
926
+ }
927
+
928
+ /**
929
+ * Final local teardown after the remote `_00_query` row was deleted: free the
930
+ * WASM view, heartbeat timer, debounce timer, and in-memory state. Caller
931
+ * (`cleanupQuery`) guarantees no subscriber remains.
932
+ */
933
+ finalizeDeregister(hash: string): void {
934
+ const qs = this.activeQueries.get(hash);
935
+ if (qs?.ttlTimer) {
936
+ clearTimeout(qs.ttlTimer);
937
+ qs.ttlTimer = null;
938
+ }
939
+ const debounce = this.debounceTimers.get(hash);
940
+ if (debounce) {
941
+ clearTimeout(debounce);
942
+ this.debounceTimers.delete(hash);
943
+ }
944
+ this.pendingStreamUpdates.delete(hash);
945
+ this.fetchDepth.delete(hash);
946
+ this.cache.unregisterQuery(hash);
947
+ this.activeQueries.delete(hash);
948
+ this.subscriptions.delete(hash);
949
+ }
950
+
263
951
  /**
264
952
  * Get query state by id (for sync and devtools)
265
953
  */
@@ -274,36 +962,163 @@ export class DataModule<S extends SchemaStructure> {
274
962
  return Array.from(this.activeQueries.values());
275
963
  }
276
964
 
965
+ getActiveQueryHashes(): QueryHash[] {
966
+ return Array.from(this.activeQueries.keys());
967
+ }
968
+
277
969
  async updateQueryLocalArray(id: string, localArray: RecordVersionArray): Promise<void> {
278
970
  const queryState = this.activeQueries.get(id);
279
971
  if (!queryState) {
280
972
  this.logger.warn(
281
- { id, Category: 'spooky-client::DataModule::updateQueryLocalArray' },
973
+ { id, Category: 'sp00ky-client::DataModule::updateQueryLocalArray' },
282
974
  'Query to update local array not found'
283
975
  );
284
976
  return;
285
977
  }
978
+ const epoch = this.local.epoch;
286
979
  queryState.config.localArray = localArray;
287
- await this.local.query(surql.seal(surql.updateSet('id', ['localArray'])), {
288
- id: queryState.config.id,
289
- localArray,
290
- });
980
+ try {
981
+ await this.local.query(
982
+ surql.seal(surql.updateSet('id', ['localArray'])),
983
+ {
984
+ id: queryState.config.id,
985
+ localArray,
986
+ },
987
+ { epoch }
988
+ );
989
+ } catch (err) {
990
+ if (err instanceof StaleEpochError) return;
991
+ throw err;
992
+ }
291
993
  }
292
994
 
293
995
  async updateQueryRemoteArray(hash: string, remoteArray: RecordVersionArray): Promise<void> {
294
996
  const queryState = this.getQueryByHash(hash);
295
997
  if (!queryState) {
296
998
  this.logger.warn(
297
- { hash, Category: 'spooky-client::DataModule::updateQueryRemoteArray' },
999
+ { hash, Category: 'sp00ky-client::DataModule::updateQueryRemoteArray' },
298
1000
  'Query to update remote array not found'
299
1001
  );
300
1002
  return;
301
1003
  }
1004
+ const epoch = this.local.epoch;
302
1005
  queryState.config.remoteArray = remoteArray;
303
- await this.local.query(surql.seal(surql.updateSet('id', ['remoteArray'])), {
304
- id: queryState.config.id,
305
- remoteArray,
306
- });
1006
+ try {
1007
+ await this.local.query(
1008
+ surql.seal(surql.updateSet('id', ['remoteArray'])),
1009
+ {
1010
+ id: queryState.config.id,
1011
+ remoteArray,
1012
+ },
1013
+ { epoch }
1014
+ );
1015
+ } catch (err) {
1016
+ if (err instanceof StaleEpochError) return;
1017
+ throw err;
1018
+ }
1019
+ }
1020
+
1021
+ /**
1022
+ * Cancel every armed timer ahead of a local-bucket switch: stream-update
1023
+ * debounce timers (their pending updates carry the OLD bucket's id-sets) and
1024
+ * per-query TTL heartbeats (they'd refresh the previous user's remote
1025
+ * `_00_query` rows under the new session). The rebind re-arms heartbeats.
1026
+ */
1027
+ quiesce(): void {
1028
+ for (const timer of this.debounceTimers.values()) {
1029
+ clearTimeout(timer);
1030
+ }
1031
+ this.debounceTimers.clear();
1032
+ this.pendingStreamUpdates.clear();
1033
+ this.fetchDepth.clear();
1034
+ for (const queryState of this.activeQueries.values()) {
1035
+ if (queryState.ttlTimer) {
1036
+ clearTimeout(queryState.ttlTimer);
1037
+ queryState.ttlTimer = null;
1038
+ }
1039
+ }
1040
+ }
1041
+
1042
+ /**
1043
+ * Re-home every active query in a freshly-opened bucket, KEEPING its hash —
1044
+ * `useQuery` subscriptions are keyed by hash and don't re-register on auth
1045
+ * changes, so the hooks must stay attached. Per query:
1046
+ * 1. reset the sync arrays + hydration flag and drop the previous user's
1047
+ * records, notifying subscribers with the new-bucket materialization
1048
+ * (usually empty) so their rows leave the UI immediately;
1049
+ * 2. recreate the `_00_query` row in the new bucket;
1050
+ * 3. re-register the SSP view on the (fresh, post-reset) processor — this
1051
+ * also rebinds the view to the NEW `$auth` context;
1052
+ * 4. restart the TTL heartbeat.
1053
+ * Returns the hashes so the caller can enqueue remote re-registration, which
1054
+ * refills records from the server via the normal register→sync→notify path.
1055
+ */
1056
+ async rebindAfterBucketSwitch(): Promise<QueryHash[]> {
1057
+ const hashes: QueryHash[] = [];
1058
+ for (const [hash, queryState] of this.activeQueries.entries()) {
1059
+ const config = queryState.config;
1060
+ config.localArray = [];
1061
+ config.remoteArray = [];
1062
+ config.subqueryRemoteArray = undefined;
1063
+ queryState.hydrated = false;
1064
+ queryState.syncNotified = false;
1065
+ queryState.records = [];
1066
+ // Via setQueryStatus (not a bare assignment) so status observers see the
1067
+ // flip back to a loading state.
1068
+ this.setQueryStatus(hash, 'fetching');
1069
+
1070
+ try {
1071
+ await withRetry(this.logger, () =>
1072
+ this.local.query<[QueryConfigRecord]>(surql.seal(surql.create('id', 'data')), {
1073
+ id: config.id,
1074
+ data: {
1075
+ surql: config.surql,
1076
+ params: config.params,
1077
+ localArray: [],
1078
+ remoteArray: [],
1079
+ lastActiveAt: new Date(),
1080
+ createdAt: new Date(),
1081
+ ttl: config.ttl,
1082
+ tableName: config.tableName,
1083
+ updateCount: queryState.updateCount,
1084
+ rowCount: 0,
1085
+ errorCount: queryState.errorCount,
1086
+ },
1087
+ })
1088
+ );
1089
+
1090
+ const { localArray } = this.cache.registerQuery({
1091
+ queryHash: hash,
1092
+ surql: config.surql,
1093
+ params: config.params,
1094
+ ttl: new Duration(config.ttl),
1095
+ lastActiveAt: new Date(),
1096
+ });
1097
+ config.localArray = localArray;
1098
+ await this.local.query(surql.seal(surql.updateSet('id', ['localArray', 'rowCount'])), {
1099
+ id: config.id,
1100
+ localArray,
1101
+ rowCount: localArray.length,
1102
+ });
1103
+ } catch (err) {
1104
+ this.logger.error(
1105
+ { err, hash, Category: 'sp00ky-client::DataModule::rebindAfterBucketSwitch' },
1106
+ 'Failed to rebind query after bucket switch; remote re-registration will retry'
1107
+ );
1108
+ }
1109
+
1110
+ // Notify AFTER the SSP re-registration so a subscriber that re-reads
1111
+ // synchronously sees consistent (empty) state.
1112
+ const subscribers = this.subscriptions.get(hash);
1113
+ if (subscribers) {
1114
+ for (const callback of subscribers) {
1115
+ callback(queryState.records);
1116
+ }
1117
+ }
1118
+ this.startTTLHeartbeat(queryState, hash);
1119
+ hashes.push(hash);
1120
+ }
1121
+ return hashes;
307
1122
  }
308
1123
 
309
1124
  /**
@@ -313,20 +1128,29 @@ export class DataModule<S extends SchemaStructure> {
313
1128
  async notifyQuerySynced(queryHash: string): Promise<void> {
314
1129
  const queryState = this.activeQueries.get(queryHash);
315
1130
  if (!queryState) return;
316
-
317
- // Re-query local DB for latest data
318
- const [records] = await this.local.query<[Record<string, any>[]]>(
319
- queryState.config.surql,
320
- queryState.config.params
321
- );
322
- const newRecords = records || [];
1131
+ const epoch = this.local.epoch;
1132
+
1133
+ // Re-query local DB for latest data (windowed queries materialize from the
1134
+ // list_ref window so they resolve even if the in-browser SSP never emits —
1135
+ // it can't compute a high offset whose preceding rows aren't resident).
1136
+ const newRecords = await this.materializeRecords(queryState);
1137
+ // Bucket switched while we materialized: these rows mix old-bucket reads
1138
+ // with new-bucket state — drop them; the rebind/re-registration re-emits.
1139
+ if (epoch !== this.local.epoch) return;
323
1140
  const changed = JSON.stringify(queryState.records) !== JSON.stringify(newRecords);
324
1141
  queryState.records = newRecords;
325
1142
 
326
- // Notify if data changed OR if this is the first sync (updateCount === 0)
327
- // The latter handles "query truly has no results" so UI can stop loading
328
- if (changed || queryState.updateCount === 0) {
1143
+ // Notify if data changed OR if this registration lifetime hasn't emitted a
1144
+ // post-sync notification yet. The latter handles "query truly has no
1145
+ // results" so the UI can stop loading — gated on the in-memory
1146
+ // `syncNotified` flag rather than `updateCount === 0`, because updateCount
1147
+ // is PERSISTED across deregister/re-register: a re-registered empty window
1148
+ // (updateCount > 0, records unchanged) would otherwise never emit and its
1149
+ // subscribers would show a loading state forever.
1150
+ if (changed || !queryState.syncNotified) {
1151
+ queryState.syncNotified = true;
329
1152
  queryState.updateCount++;
1153
+ queryState.lastUpdatedAt = Date.now();
330
1154
  const subscribers = this.subscriptions.get(queryHash);
331
1155
  if (subscribers) {
332
1156
  for (const callback of subscribers) {
@@ -344,6 +1168,24 @@ export class DataModule<S extends SchemaStructure> {
344
1168
  data: RoutePayload<S, B, R>,
345
1169
  options?: RunOptions
346
1170
  ): Promise<void> {
1171
+ const { tableName, record } = this.buildJobRecord(backend, path, data, options);
1172
+ const recordId = `${tableName}:${generateId()}`;
1173
+ await this.create(recordId, record);
1174
+ }
1175
+
1176
+ /**
1177
+ * Build the outbox job record + resolve its table for a backend route.
1178
+ *
1179
+ * Every job is a single execution. Recurring work is declared server-side
1180
+ * (`schedules:` in sp00ky.yml) and the scheduler creates a fresh row per cycle,
1181
+ * so nothing here needs to know about schedules.
1182
+ */
1183
+ private buildJobRecord<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(
1184
+ backend: B,
1185
+ path: R,
1186
+ data: RoutePayload<S, B, R>,
1187
+ options?: RunOptions
1188
+ ): { tableName: string; record: Record<string, unknown> } {
347
1189
  const route = this.schema.backends?.[backend]?.routes?.[path];
348
1190
  if (!route) {
349
1191
  throw new Error(`Route ${backend}.${path} not found`);
@@ -366,16 +1208,28 @@ export class DataModule<S extends SchemaStructure> {
366
1208
  const record: Record<string, unknown> = {
367
1209
  path,
368
1210
  payload: JSON.stringify(payload),
1211
+ // Set explicitly: the schema's DEFAULT ALWAYS "pending" is server-side
1212
+ // only. An optimistic local create without it surfaces with status
1213
+ // undefined, so in-flight indicators keyed on pending/processing stay
1214
+ // off until the first server echo (seconds on a delayed job).
1215
+ status: 'pending',
369
1216
  max_retries: options?.max_retries ?? 3,
370
1217
  retry_strategy: options?.retry_strategy ?? 'linear',
371
1218
  };
372
1219
 
1220
+ if (options?.timeout != null) {
1221
+ record.timeout = options.timeout;
1222
+ }
1223
+
1224
+ if (options?.delay != null) {
1225
+ record.delay = options.delay;
1226
+ }
1227
+
373
1228
  if (options?.assignedTo) {
374
1229
  record.assigned_to = options.assignedTo;
375
1230
  }
376
1231
 
377
- const recordId = `${tableName}:${generateId()}`;
378
- await this.create(recordId, record);
1232
+ return { tableName, record };
379
1233
  }
380
1234
 
381
1235
  // ==================== MUTATION MANAGEMENT ====================
@@ -392,7 +1246,7 @@ export class DataModule<S extends SchemaStructure> {
392
1246
 
393
1247
  const rid = parseRecordIdString(id);
394
1248
  const params = parseParams(tableSchema.columns, data);
395
- const mutationId = parseRecordIdString(`_spooky_pending_mutations:${Date.now()}`);
1249
+ const mutationId = parseRecordIdString(`_00_pending_mutations:${Date.now()}`);
396
1250
 
397
1251
  const dataKeys = Object.keys(params).map((key) => ({ key, variable: `data_${key}` }));
398
1252
  const prefixedParams = Object.fromEntries(
@@ -441,7 +1295,7 @@ export class DataModule<S extends SchemaStructure> {
441
1295
  callback([mutationEvent]);
442
1296
  }
443
1297
 
444
- this.logger.debug({ id, Category: 'spooky-client::DataModule::create' }, 'Record created');
1298
+ this.logger.debug({ id, Category: 'sp00ky-client::DataModule::create' }, 'Record created');
445
1299
 
446
1300
  return target;
447
1301
  }
@@ -463,7 +1317,10 @@ export class DataModule<S extends SchemaStructure> {
463
1317
 
464
1318
  const rid = parseRecordIdString(id);
465
1319
  const params = parseParams(tableSchema.columns, data);
466
- const mutationId = parseRecordIdString(`_spooky_pending_mutations:${Date.now()}`);
1320
+ const mutationId = parseRecordIdString(`_00_pending_mutations:${Date.now()}`);
1321
+
1322
+ // Note: CRDT state is pushed directly to the _00_crdt table by CrdtField.pushToRemote(),
1323
+ // NOT through the record update pipeline. This keeps the record data clean.
467
1324
 
468
1325
  // Capture current record state before mutation for rollback support
469
1326
  const [beforeRecord] = await withRetry(this.logger, () =>
@@ -472,7 +1329,7 @@ export class DataModule<S extends SchemaStructure> {
472
1329
 
473
1330
  const query = surql.seal<{ target: T }>(
474
1331
  surql.tx([
475
- surql.updateSet('id', [{ statement: 'spooky_rv += 1' }]),
1332
+ surql.updateSet('id', [{ statement: '_00_rv += 1' }]),
476
1333
  surql.let('updated', surql.updateMerge('id', 'data')),
477
1334
  surql.createMutation('update', 'mid', 'id', 'data'),
478
1335
  surql.returnObject([{ key: 'target', variable: 'updated' }]),
@@ -496,8 +1353,8 @@ export class DataModule<S extends SchemaStructure> {
496
1353
  updatedFields[key] = (target as Record<string, any>)[key];
497
1354
  }
498
1355
  }
499
- if ('spooky_rv' in (target as Record<string, any>)) {
500
- updatedFields.spooky_rv = (target as Record<string, any>).spooky_rv;
1356
+ if ('_00_rv' in (target as Record<string, any>)) {
1357
+ updatedFields._00_rv = (target as Record<string, any>)._00_rv;
501
1358
  }
502
1359
  this.replaceRecordInQueries(updatedFields);
503
1360
 
@@ -509,7 +1366,7 @@ export class DataModule<S extends SchemaStructure> {
509
1366
  table: table,
510
1367
  op: 'UPDATE',
511
1368
  record: parsedRecord,
512
- version: target.spooky_rv as number,
1369
+ version: target._00_rv as number,
513
1370
  },
514
1371
  true
515
1372
  );
@@ -531,7 +1388,7 @@ export class DataModule<S extends SchemaStructure> {
531
1388
  callback([mutationEvent]);
532
1389
  }
533
1390
 
534
- this.logger.debug({ id, Category: 'spooky-client::DataModule::update' }, 'Record updated');
1391
+ this.logger.debug({ id, Category: 'sp00ky-client::DataModule::update' }, 'Record updated');
535
1392
 
536
1393
  return target;
537
1394
  }
@@ -547,14 +1404,53 @@ export class DataModule<S extends SchemaStructure> {
547
1404
  }
548
1405
 
549
1406
  const rid = parseRecordIdString(id);
550
- const mutationId = parseRecordIdString(`_spooky_pending_mutations:${Date.now()}`);
1407
+ const mutationId = parseRecordIdString(`_00_pending_mutations:${Date.now()}`);
1408
+
1409
+ // Fetch the record before deleting so DBSP can match it against query predicates
1410
+ const [beforeRecords] = await this.local.query<[Record<string, any>[]]>(
1411
+ 'SELECT * FROM ONLY $id',
1412
+ { id: rid }
1413
+ );
1414
+ const beforeRecord = beforeRecords ?? {};
551
1415
 
552
1416
  const query = surql.seal<void>(
553
1417
  surql.tx([surql.delete('id'), surql.createMutation('delete', 'mid', 'id')])
554
1418
  );
555
1419
 
556
1420
  await withRetry(this.logger, () => this.local.execute(query, { id: rid, mid: mutationId }));
557
- await this.cache.delete(table, id, true);
1421
+
1422
+ // The local DELETE has now committed. Everything below must reflect that in
1423
+ // active live queries — so the deleted row disappears optimistically without
1424
+ // a reload — even if the optimistic SSP-view ingest below fails. Previously a
1425
+ // throw from `cache.delete` (the WASM ingest) aborted `delete()` after the
1426
+ // commit, so the manual notify loop never ran and the row lingered on screen
1427
+ // until reload. Ingesting the delete into the in-browser SSP view is
1428
+ // best-effort: the manual re-materialize reads the local DB (which already
1429
+ // excludes the row), so the result is correct regardless.
1430
+ try {
1431
+ await this.cache.delete(table, id, true, beforeRecord);
1432
+ } catch (err) {
1433
+ this.logger.error(
1434
+ { err, id, Category: 'sp00ky-client::DataModule::delete' },
1435
+ 'SSP delete-ingest failed; relying on query re-materialize to reflect the delete'
1436
+ );
1437
+ }
1438
+
1439
+ // DBSP may not emit view updates for DELETE ops — manually notify all queries
1440
+ // that reference this table. Each is isolated so one failing re-materialize
1441
+ // can't stop the others (or the sync emit below) from running.
1442
+ for (const [queryHash, queryState] of this.activeQueries) {
1443
+ if (queryState.config.tableName === tableName) {
1444
+ try {
1445
+ await this.notifyQuerySynced(queryHash);
1446
+ } catch (err) {
1447
+ this.logger.error(
1448
+ { err, queryHash, Category: 'sp00ky-client::DataModule::delete' },
1449
+ 'notifyQuerySynced failed after delete'
1450
+ );
1451
+ }
1452
+ }
1453
+ }
558
1454
 
559
1455
  // Emit mutation event
560
1456
  const mutationEvent: DeleteEvent = {
@@ -567,7 +1463,7 @@ export class DataModule<S extends SchemaStructure> {
567
1463
  callback([mutationEvent]);
568
1464
  }
569
1465
 
570
- this.logger.debug({ id, Category: 'spooky-client::DataModule::delete' }, 'Record deleted');
1466
+ this.logger.debug({ id, Category: 'sp00ky-client::DataModule::delete' }, 'Record deleted');
571
1467
  }
572
1468
 
573
1469
  // ==================== ROLLBACK METHODS ====================
@@ -586,12 +1482,12 @@ export class DataModule<S extends SchemaStructure> {
586
1482
  this.removeRecordFromQueries(recordId);
587
1483
 
588
1484
  this.logger.info(
589
- { id, tableName, Category: 'spooky-client::DataModule::rollbackCreate' },
1485
+ { id, tableName, Category: 'sp00ky-client::DataModule::rollbackCreate' },
590
1486
  'Rolled back optimistic create'
591
1487
  );
592
1488
  } catch (err) {
593
1489
  this.logger.error(
594
- { err, id, tableName, Category: 'spooky-client::DataModule::rollbackCreate' },
1490
+ { err, id, tableName, Category: 'sp00ky-client::DataModule::rollbackCreate' },
595
1491
  'Failed to rollback create'
596
1492
  );
597
1493
  }
@@ -626,7 +1522,7 @@ export class DataModule<S extends SchemaStructure> {
626
1522
  table: tableName,
627
1523
  op: 'UPDATE',
628
1524
  record: parsedRecord,
629
- version: (beforeRecord.spooky_rv as number) || 1,
1525
+ version: (beforeRecord._00_rv as number) || 1,
630
1526
  },
631
1527
  true
632
1528
  );
@@ -635,12 +1531,12 @@ export class DataModule<S extends SchemaStructure> {
635
1531
  await this.replaceRecordInQueries(beforeRecord);
636
1532
 
637
1533
  this.logger.info(
638
- { id, tableName, Category: 'spooky-client::DataModule::rollbackUpdate' },
1534
+ { id, tableName, Category: 'sp00ky-client::DataModule::rollbackUpdate' },
639
1535
  'Rolled back optimistic update'
640
1536
  );
641
1537
  } catch (err) {
642
1538
  this.logger.error(
643
- { err, id, tableName, Category: 'spooky-client::DataModule::rollbackUpdate' },
1539
+ { err, id, tableName, Category: 'sp00ky-client::DataModule::rollbackUpdate' },
644
1540
  'Failed to rollback update'
645
1541
  );
646
1542
  }
@@ -678,7 +1574,8 @@ export class DataModule<S extends SchemaStructure> {
678
1574
  surqlString: string,
679
1575
  params: Record<string, any>,
680
1576
  ttl: QueryTimeToLive,
681
- tableName: T
1577
+ tableName: T,
1578
+ plan?: QueryPlan
682
1579
  ): Promise<QueryHash> {
683
1580
  const queryState = await this.createNewQuery<T>({
684
1581
  recordId,
@@ -686,31 +1583,76 @@ export class DataModule<S extends SchemaStructure> {
686
1583
  params,
687
1584
  ttl,
688
1585
  tableName,
1586
+ plan,
689
1587
  });
690
1588
 
691
- const { localArray } = this.cache.registerQuery({
1589
+ const t0 = performance.now();
1590
+ const { localArray, registrationTimings } = this.cache.registerQuery({
692
1591
  queryHash: hash,
693
1592
  surql: surqlString,
694
1593
  params,
695
1594
  ttl: new Duration(ttl),
696
1595
  lastActiveAt: new Date(),
697
1596
  });
1597
+ const registrationTime = performance.now() - t0;
1598
+
1599
+ // Record the one-shot SSP registration timings (parse/plan/snapshot from the
1600
+ // WASM binding) + the register_view wall time for DevTools.
1601
+ queryState.registrationTimings = {
1602
+ parseMs: registrationTimings?.parseMs ?? null,
1603
+ planMs: registrationTimings?.planMs ?? null,
1604
+ snapshotMs: registrationTimings?.snapshotMs ?? null,
1605
+ wallMs: registrationTime,
1606
+ };
698
1607
 
699
1608
  await withRetry(this.logger, () =>
700
- this.local.query(surql.seal(surql.updateSet('id', ['localArray'])), {
701
- id: recordId,
702
- localArray,
703
- })
1609
+ this.local.query(
1610
+ surql.seal(surql.updateSet('id', ['localArray', 'registrationTime', 'rowCount'])),
1611
+ {
1612
+ id: recordId,
1613
+ localArray,
1614
+ registrationTime,
1615
+ rowCount: localArray.length,
1616
+ }
1617
+ )
704
1618
  );
705
1619
 
1620
+ // Windowed (`START n`) queries skipped the raw initial load in
1621
+ // createNewQuery (O(offset) + wrong rows for sparse windows). Seed the
1622
+ // initial rows now from the SSP's window id-set (`localArray`) via the same
1623
+ // window-materialization path the stream updates use — O(window), and the
1624
+ // ids are already the correct window — so the first paint isn't empty while
1625
+ // the remote `_00_list_ref` syncs in.
1626
+ const windowMat = buildWindowMaterialization(surqlString);
1627
+ if (windowMat && localArray.length > 0) {
1628
+ try {
1629
+ const winIds = localArray.map(([id]) => parseRecordIdString(id));
1630
+ if (plan) {
1631
+ const winPlan = buildWindowMaterializationPlan(plan, winIds) ?? { ...plan, ids: winIds };
1632
+ queryState.records = await this.local.select(winPlan, params);
1633
+ } else {
1634
+ const [seeded] = await this.local.query<[Record<string, any>[]]>(windowMat.query, {
1635
+ ...params,
1636
+ __win: winIds,
1637
+ });
1638
+ queryState.records = seeded || [];
1639
+ }
1640
+ } catch (err) {
1641
+ this.logger.warn(
1642
+ { err, hash, Category: 'sp00ky-client::DataModule::createAndRegisterQuery' },
1643
+ 'Failed to seed windowed initial records from localArray'
1644
+ );
1645
+ }
1646
+ }
1647
+
706
1648
  this.activeQueries.set(hash, queryState);
707
- this.startTTLHeartbeat(queryState);
1649
+ this.startTTLHeartbeat(queryState, hash);
708
1650
  this.logger.debug(
709
1651
  {
710
1652
  hash,
711
1653
  tableName,
712
1654
  recordCount: queryState.records.length,
713
- Category: 'spooky-client::DataModule::query',
1655
+ Category: 'sp00ky-client::DataModule::query',
714
1656
  },
715
1657
  'Query registered'
716
1658
  );
@@ -724,14 +1666,25 @@ export class DataModule<S extends SchemaStructure> {
724
1666
  params,
725
1667
  ttl,
726
1668
  tableName,
1669
+ plan,
727
1670
  }: {
728
1671
  recordId: RecordId;
729
1672
  surql: string;
730
1673
  params: Record<string, any>;
731
1674
  ttl: QueryTimeToLive;
732
1675
  tableName: T;
1676
+ plan?: QueryPlan;
733
1677
  }): Promise<QueryState> {
734
- const tableSchema = this.schema.tables.find((t) => t.name === tableName);
1678
+ // `_00_*` meta tables (feature flags, app releases) are framework-owned:
1679
+ // they exist in the client db schema by construction but are never part of
1680
+ // the app's generated `schema.tables`, so give them an empty column map
1681
+ // instead of the not-found error (which silently broke every meta-table
1682
+ // live query, e.g. feature flags never updating on this path).
1683
+ const tableSchema =
1684
+ this.schema.tables.find((t) => t.name === tableName) ??
1685
+ (String(tableName).startsWith('_00_')
1686
+ ? ({ name: tableName, columns: {} } as any)
1687
+ : undefined);
735
1688
  if (!tableSchema) {
736
1689
  throw new Error(`Table ${tableName} not found`);
737
1690
  }
@@ -752,8 +1705,12 @@ export class DataModule<S extends SchemaStructure> {
752
1705
  localArray: [],
753
1706
  remoteArray: [],
754
1707
  lastActiveAt: new Date(),
1708
+ createdAt: new Date(),
755
1709
  ttl,
756
1710
  tableName,
1711
+ updateCount: 0,
1712
+ rowCount: 0,
1713
+ errorCount: 0,
757
1714
  },
758
1715
  })
759
1716
  );
@@ -763,62 +1720,111 @@ export class DataModule<S extends SchemaStructure> {
763
1720
  const config: QueryConfig = {
764
1721
  ...configRecord,
765
1722
  id: recordId,
1723
+ // In-memory only — carries the engine-neutral plan so non-SurrealQL local
1724
+ // engines materialize via `select(plan)` instead of parsing `surql`.
1725
+ plan,
766
1726
  params: parseParams(tableSchema.columns, configRecord.params),
767
1727
  };
768
1728
 
769
1729
  let records: Record<string, any>[] = [];
770
- try {
771
- const [result] = await this.local.query<[Record<string, any>[]]>(surqlString, params);
772
- records = result || [];
773
- } catch (err) {
774
- this.logger.warn(
775
- { err, Category: 'spooky-client::DataModule::createNewQuery' },
776
- 'Failed to load initial cached records'
777
- );
1730
+ // Windowed (`START n`) queries: do NOT seed from the raw surql here. Running
1731
+ // `… LIMIT n START m` against the shared local store is O(m) — it sorts and
1732
+ // skips m rows on every window open — AND returns the wrong rows for sparse
1733
+ // windows (the reason `buildWindowMaterialization` exists). Those windows are
1734
+ // seeded from the SSP `localArray` in `createAndRegisterQuery` instead.
1735
+ if (buildWindowMaterialization(surqlString) === null) {
1736
+ try {
1737
+ // Prefer the engine-neutral plan (required for non-SurrealQL engines);
1738
+ // fall back to running the raw surql on the SurrealDB engine.
1739
+ const result = plan
1740
+ ? await this.local.select(plan, params)
1741
+ : (await this.local.query<[Record<string, any>[]]>(surqlString, params))[0];
1742
+ records = result || [];
1743
+ } catch (err) {
1744
+ this.logger.warn(
1745
+ { err, Category: 'sp00ky-client::DataModule::createNewQuery' },
1746
+ 'Failed to load initial cached records'
1747
+ );
1748
+ }
778
1749
  }
779
1750
 
1751
+ // Persisted counters survive a restart even though the rolling
1752
+ // sample window is rebuilt from scratch in memory.
1753
+ const persistedUpdateCount =
1754
+ typeof (configRecord as any)?.updateCount === 'number'
1755
+ ? (configRecord as any).updateCount
1756
+ : 0;
1757
+ const persistedErrorCount =
1758
+ typeof (configRecord as any)?.errorCount === 'number'
1759
+ ? (configRecord as any).errorCount
1760
+ : 0;
1761
+
780
1762
  return {
781
1763
  config,
782
1764
  records,
783
1765
  ttlTimer: null,
784
1766
  ttlDurationMs: parseDuration(ttl),
785
- updateCount: 0,
1767
+ updateCount: persistedUpdateCount,
1768
+ lastUpdatedAt: null,
1769
+ materializationSamples: [],
1770
+ lastIngestLatencyMs: null,
1771
+ errorCount: persistedErrorCount,
1772
+ // Born `fetching`, not `idle`: every cold registration is followed by a
1773
+ // `register` down-event whose lifecycle (Sp00kySync.registerQuery) resolves
1774
+ // the status to `idle` once the initial sync completed. Starting idle left
1775
+ // a gap where a fresh windowed query looked settled while still empty.
1776
+ status: 'fetching',
1777
+ phaseSamples: {},
1778
+ phaseLast: {},
1779
+ registrationTimings: { parseMs: null, planMs: null, snapshotMs: null, wallMs: null },
786
1780
  };
787
1781
  }
788
1782
 
789
1783
  private async calculateHash(data: any): Promise<string> {
790
- const content = JSON.stringify(data);
1784
+ // sessionId is part of the hash so the same logical query from two
1785
+ // sessions (e.g. two browser tabs of the same user) lands on different
1786
+ // `_00_query` rows and doesn't fight over a shared one.
1787
+ const content = JSON.stringify({ ...data, sessionId: this.sessionId });
791
1788
  const msgBuffer = new TextEncoder().encode(content);
792
1789
  const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
793
1790
  const hashArray = Array.from(new Uint8Array(hashBuffer));
794
1791
  return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
795
1792
  }
796
1793
 
797
- private startTTLHeartbeat(queryState: QueryState): void {
1794
+ private startTTLHeartbeat(queryState: QueryState, hash: QueryHash): void {
798
1795
  if (queryState.ttlTimer) return;
799
1796
 
800
1797
  const heartbeatTime = Math.floor(queryState.ttlDurationMs * 0.9);
801
1798
 
802
1799
  queryState.ttlTimer = setTimeout(() => {
803
- // TODO: Emit heartbeat event for sync
1800
+ queryState.ttlTimer = null;
1801
+ // Only keep the remote query alive while something is actually watching
1802
+ // it. With the server now sweeping ALL expired views (not just in-circuit
1803
+ // ones), an un-refreshed query WOULD be swept after its TTL — so a live
1804
+ // subscriber must heartbeat. An abandoned query (no subscribers) is left
1805
+ // to expire and get swept; its local view was already torn down at the
1806
+ // last unsubscribe, so we just stop the timer here.
1807
+ const subscriberCount = this.subscriptions.get(hash)?.size ?? 0;
1808
+ if (subscriberCount === 0) {
1809
+ this.logger.debug(
1810
+ { hash, Category: 'sp00ky-client::DataModule::startTTLHeartbeat' },
1811
+ 'TTL heartbeat: no subscribers, stopping'
1812
+ );
1813
+ return;
1814
+ }
1815
+ this.onHeartbeat?.(hash);
804
1816
  this.logger.debug(
805
1817
  {
1818
+ hash,
806
1819
  id: encodeRecordId(queryState.config.id),
807
- Category: 'spooky-client::DataModule::startTTLHeartbeat',
1820
+ Category: 'sp00ky-client::DataModule::startTTLHeartbeat',
808
1821
  },
809
- 'TTL heartbeat'
1822
+ 'TTL heartbeat sent'
810
1823
  );
811
- this.startTTLHeartbeat(queryState);
1824
+ this.startTTLHeartbeat(queryState, hash);
812
1825
  }, heartbeatTime);
813
1826
  }
814
1827
 
815
- private stopTTLHeartbeat(queryState: QueryState): void {
816
- if (queryState.ttlTimer) {
817
- clearTimeout(queryState.ttlTimer);
818
- queryState.ttlTimer = null;
819
- }
820
- }
821
-
822
1828
  private async replaceRecordInQueries(record: Record<string, any>): Promise<void> {
823
1829
  for (const [queryHash, queryState] of this.activeQueries.entries()) {
824
1830
  const index = queryState.records.findIndex((r) => r.id === record.id);
@@ -851,7 +1857,7 @@ export function parseUpdateOptions(
851
1857
  const delay = options.debounced !== true ? (options.debounced?.delay ?? 200) : 200;
852
1858
  const keyType = options.debounced !== true ? (options.debounced?.key ?? id) : id;
853
1859
  const key =
854
- keyType === 'recordId_x_fields' ? `${id}::${Object.keys(data).sort().join('#')}` : id;
1860
+ keyType === 'recordId_x_fields' ? `${id}::${Object.keys(data).toSorted().join('#')}` : id;
855
1861
 
856
1862
  pushEventOptions = {
857
1863
  debounced: {