@spooky-sync/core 0.0.1-canary.21 → 0.0.1-canary.211

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 (163) hide show
  1. package/AGENTS.md +57 -0
  2. package/dist/index.d.ts +2514 -58
  3. package/dist/index.js +12561 -2449
  4. package/dist/otel/index.d.ts +2 -2
  5. package/dist/otel/index.js +6 -6
  6. package/dist/sqlite-open.js +303 -0
  7. package/dist/sqlite-worker.d.ts +1 -0
  8. package/dist/sqlite-worker.js +439 -0
  9. package/dist/tabs-broker-worker.d.ts +8 -0
  10. package/dist/tabs-broker-worker.js +472 -0
  11. package/dist/types.d.ts +751 -11
  12. package/package.json +11 -7
  13. package/scripts/check-broker-bundle.mjs +33 -0
  14. package/skills/{spooky-core → sp00ky-core}/SKILL.md +12 -12
  15. package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
  16. package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
  17. package/src/bucket-blurhash.test.ts +148 -0
  18. package/src/build-globals.d.ts +12 -0
  19. package/src/events/events.test.ts +2 -1
  20. package/src/events/index.ts +3 -0
  21. package/src/index.ts +36 -2
  22. package/src/modules/app-release/index.test.ts +125 -0
  23. package/src/modules/app-release/index.ts +201 -0
  24. package/src/modules/auth/auth.local-first.test.ts +101 -0
  25. package/src/modules/auth/events/index.ts +2 -1
  26. package/src/modules/auth/index.ts +127 -24
  27. package/src/modules/cache/cache.relay.test.ts +95 -0
  28. package/src/modules/cache/index.ts +163 -43
  29. package/src/modules/cache/types.ts +2 -2
  30. package/src/modules/crdt/crdt-field.ts +294 -0
  31. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  32. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  33. package/src/modules/crdt/index.ts +463 -0
  34. package/src/modules/crdt/loro-loader.ts +25 -0
  35. package/src/modules/data/data.hydration.test.ts +142 -0
  36. package/src/modules/data/data.membership.test.ts +523 -0
  37. package/src/modules/data/data.notify-table.test.ts +41 -0
  38. package/src/modules/data/data.pending-ids.test.ts +199 -0
  39. package/src/modules/data/data.rebind.test.ts +170 -0
  40. package/src/modules/data/data.rematerialize.test.ts +114 -0
  41. package/src/modules/data/data.run.test.ts +113 -0
  42. package/src/modules/data/data.settled-writes.test.ts +206 -0
  43. package/src/modules/data/data.status.test.ts +249 -0
  44. package/src/modules/data/id-set-plan.test.ts +122 -0
  45. package/src/modules/data/index.ts +1815 -151
  46. package/src/modules/data/mutation-id.test.ts +25 -0
  47. package/src/modules/data/mutation-id.ts +35 -0
  48. package/src/modules/data/window-query.test.ts +52 -0
  49. package/src/modules/data/window-query.ts +194 -0
  50. package/src/modules/devtools/flags.ts +349 -0
  51. package/src/modules/devtools/index.ts +450 -46
  52. package/src/modules/devtools/notify-throttle.test.ts +154 -0
  53. package/src/modules/devtools/state-shape.test.ts +146 -0
  54. package/src/modules/devtools/storage-info.test.ts +79 -0
  55. package/src/modules/devtools/storage-info.ts +168 -0
  56. package/src/modules/devtools/versions.test.ts +74 -0
  57. package/src/modules/devtools/versions.ts +110 -0
  58. package/src/modules/feature-flag/index.test.ts +251 -0
  59. package/src/modules/feature-flag/index.ts +308 -0
  60. package/src/modules/ref-tables.test.ts +91 -0
  61. package/src/modules/ref-tables.ts +88 -0
  62. package/src/modules/sync/engine.ts +164 -82
  63. package/src/modules/sync/events/index.ts +9 -2
  64. package/src/modules/sync/queue/queue-down.test.ts +180 -0
  65. package/src/modules/sync/queue/queue-down.ts +80 -13
  66. package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
  67. package/src/modules/sync/queue/queue-up.ts +241 -57
  68. package/src/modules/sync/scheduler.pause.test.ts +109 -0
  69. package/src/modules/sync/scheduler.retry.test.ts +237 -0
  70. package/src/modules/sync/scheduler.ts +215 -13
  71. package/src/modules/sync/sync.cleanup.test.ts +116 -0
  72. package/src/modules/sync/sync.health.test.ts +149 -0
  73. package/src/modules/sync/sync.heartbeat.test.ts +80 -0
  74. package/src/modules/sync/sync.live-removal.test.ts +175 -0
  75. package/src/modules/sync/sync.reconnect.test.ts +145 -0
  76. package/src/modules/sync/sync.subquery.test.ts +82 -0
  77. package/src/modules/sync/sync.tabs.test.ts +249 -0
  78. package/src/modules/sync/sync.ts +1726 -99
  79. package/src/modules/sync/utils.test.ts +269 -2
  80. package/src/modules/sync/utils.ts +201 -17
  81. package/src/otel/index.ts +13 -10
  82. package/src/services/blobs/blob-cache.test.ts +359 -0
  83. package/src/services/blobs/blob-cache.ts +603 -0
  84. package/src/services/blobs/blob-manifest.ts +227 -0
  85. package/src/services/blobs/blob-store.test.ts +77 -0
  86. package/src/services/blobs/blob-store.ts +359 -0
  87. package/src/services/blobs/blob.fixture.ts +90 -0
  88. package/src/services/blobs/index.ts +70 -0
  89. package/src/services/database/cache-engine.ts +193 -0
  90. package/src/services/database/connection-supervisor.test.ts +289 -0
  91. package/src/services/database/connection-supervisor.ts +415 -0
  92. package/src/services/database/database.query-timeout.test.ts +83 -0
  93. package/src/services/database/database.ts +41 -12
  94. package/src/services/database/engine-factory.ts +33 -0
  95. package/src/services/database/errors.ts +34 -0
  96. package/src/services/database/events/index.ts +2 -1
  97. package/src/services/database/index.ts +7 -0
  98. package/src/services/database/local-migrator.ts +30 -27
  99. package/src/services/database/local.test.ts +64 -0
  100. package/src/services/database/local.ts +484 -67
  101. package/src/services/database/plan-render.test.ts +159 -0
  102. package/src/services/database/plan-render.ts +108 -0
  103. package/src/services/database/relation-resolver.test.ts +413 -0
  104. package/src/services/database/relation-resolver.ts +0 -0
  105. package/src/services/database/remote.ts +110 -14
  106. package/src/services/database/sqlite-cache-engine.test.ts +616 -0
  107. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  108. package/src/services/database/sqlite-cache-engine.ts +1358 -0
  109. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  110. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  111. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  112. package/src/services/database/sqlite-lock-verify.ts +45 -0
  113. package/src/services/database/sqlite-open.test.ts +150 -0
  114. package/src/services/database/sqlite-open.ts +164 -0
  115. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  116. package/src/services/database/sqlite-plan-sql.ts +138 -0
  117. package/src/services/database/sqlite-projection.test.ts +99 -0
  118. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  119. package/src/services/database/sqlite-select.test.ts +246 -0
  120. package/src/services/database/sqlite-select.ts +131 -0
  121. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  122. package/src/services/database/sqlite-transport.ts +224 -0
  123. package/src/services/database/sqlite-worker.ts +437 -0
  124. package/src/services/database/surql-translate.ts +416 -0
  125. package/src/services/database/surreal-cache-engine.ts +161 -0
  126. package/src/services/logger/index.ts +3 -2
  127. package/src/services/persistence/localstorage.ts +2 -2
  128. package/src/services/persistence/resilient.ts +11 -4
  129. package/src/services/persistence/surrealdb.ts +10 -10
  130. package/src/services/stream-processor/index.ts +796 -84
  131. package/src/services/stream-processor/permissions.test.ts +47 -0
  132. package/src/services/stream-processor/permissions.ts +53 -0
  133. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  134. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  135. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  136. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  137. package/src/services/stream-processor/wasm-types.ts +59 -3
  138. package/src/services/tabs/broker-client.ts +283 -0
  139. package/src/services/tabs/broker.test.ts +327 -0
  140. package/src/services/tabs/coordinator.test.ts +365 -0
  141. package/src/services/tabs/coordinator.ts +633 -0
  142. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  143. package/src/services/tabs/leader-locks.ts +75 -0
  144. package/src/services/tabs/protocol.ts +258 -0
  145. package/src/services/tabs/support.ts +36 -0
  146. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  147. package/src/sp00ky.auth-order.test.ts +92 -0
  148. package/src/sp00ky.init-query.test.ts +183 -0
  149. package/src/sp00ky.local-first.test.ts +60 -0
  150. package/src/sp00ky.ts +1693 -0
  151. package/src/types.ts +528 -13
  152. package/src/utils/blurhash.ts +90 -0
  153. package/src/utils/error-classification.test.ts +44 -0
  154. package/src/utils/error-classification.ts +7 -0
  155. package/src/utils/index.ts +79 -13
  156. package/src/utils/parser.test.ts +49 -120
  157. package/src/utils/parser.ts +32 -2
  158. package/src/utils/semver.test.ts +32 -0
  159. package/src/utils/semver.ts +30 -0
  160. package/src/utils/surql.ts +30 -18
  161. package/src/utils/withRetry.test.ts +1 -1
  162. package/tsdown.config.ts +86 -1
  163. package/src/spooky.ts +0 -395
@@ -5,8 +5,16 @@ import {
5
5
  applyRecordVersionDiff,
6
6
  createDiffFromDbOp,
7
7
  ArraySyncer,
8
+ resolveListRefPollInterval,
9
+ DEFAULT_LIST_REF_POLL_INTERVAL_MS,
10
+ buildListRefSelect,
11
+ buildSubqueryListRefSelect,
12
+ nextPollDelayMs,
13
+ listRefPollDelayMs,
14
+ LIST_REF_POLL_MAX_INTERVAL_MS,
15
+ recordVersionArraysEqual,
8
16
  } from './utils';
9
- import { RecordVersionArray, RecordVersionDiff } from '../../types';
17
+ import type { RecordVersionArray, RecordVersionDiff } from '../../types';
10
18
  import { encodeRecordId } from '../../utils/index';
11
19
 
12
20
  function rid(table: string, id: string): RecordId<string> {
@@ -305,7 +313,266 @@ describe('ArraySyncer', () => {
305
313
  expect(diff!.removed).toHaveLength(3);
306
314
  // Check they come in sorted order
307
315
  const removedIds = diff!.removed.map((r) => encodeRecordId(r));
308
- const sorted = [...removedIds].sort();
316
+ const sorted = [...removedIds].toSorted();
309
317
  expect(removedIds).toEqual(sorted);
310
318
  });
311
319
  });
320
+
321
+ describe('resolveListRefPollInterval', () => {
322
+ it('returns the default when no option is provided', () => {
323
+ expect(resolveListRefPollInterval()).toBe(DEFAULT_LIST_REF_POLL_INTERVAL_MS);
324
+ expect(resolveListRefPollInterval(undefined)).toBe(
325
+ DEFAULT_LIST_REF_POLL_INTERVAL_MS
326
+ );
327
+ });
328
+
329
+ it('respects a positive override', () => {
330
+ expect(resolveListRefPollInterval(1000)).toBe(1000);
331
+ expect(resolveListRefPollInterval(5000)).toBe(5000);
332
+ });
333
+
334
+ it('falls back to default for zero, negative, NaN, or Infinity', () => {
335
+ // Accepting these would either silently disable polling or busy-loop
336
+ // the event loop, so they fall back to the default.
337
+ expect(resolveListRefPollInterval(0)).toBe(DEFAULT_LIST_REF_POLL_INTERVAL_MS);
338
+ expect(resolveListRefPollInterval(-500)).toBe(
339
+ DEFAULT_LIST_REF_POLL_INTERVAL_MS
340
+ );
341
+ expect(resolveListRefPollInterval(NaN)).toBe(
342
+ DEFAULT_LIST_REF_POLL_INTERVAL_MS
343
+ );
344
+ expect(resolveListRefPollInterval(Infinity)).toBe(
345
+ DEFAULT_LIST_REF_POLL_INTERVAL_MS
346
+ );
347
+ });
348
+
349
+ it('locks the default at 500ms so accidental tuning trips this test', () => {
350
+ expect(DEFAULT_LIST_REF_POLL_INTERVAL_MS).toBe(500);
351
+ });
352
+ });
353
+
354
+ describe('buildListRefSelect', () => {
355
+ it('substitutes the table name', () => {
356
+ expect(buildListRefSelect('_00_list_ref')).toContain('FROM _00_list_ref ');
357
+ expect(buildListRefSelect('_00_list_ref_user_abc')).toContain(
358
+ 'FROM _00_list_ref_user_abc '
359
+ );
360
+ });
361
+
362
+ it('filters by the bound query id', () => {
363
+ expect(buildListRefSelect('_00_list_ref')).toContain('WHERE in = $in');
364
+ });
365
+
366
+ it('excludes subquery rows via parent IS NONE', () => {
367
+ // Without this predicate the poll would surface child records (the
368
+ // subquery-projection rows the SSP also writes into list_ref) as
369
+ // spurious "added" diffs against `localArray` every tick.
370
+ expect(buildListRefSelect('_00_list_ref')).toContain('parent IS NONE');
371
+ });
372
+
373
+ it('selects only `out` and `version`', () => {
374
+ // The diff only needs the record id and its version; pulling more
375
+ // fields would bloat the per-tick query traffic.
376
+ const sql = buildListRefSelect('_00_list_ref');
377
+ expect(sql.startsWith('SELECT out, version FROM ')).toBe(true);
378
+ });
379
+ });
380
+
381
+ describe('buildSubqueryListRefSelect', () => {
382
+ it('substitutes the table name', () => {
383
+ expect(buildSubqueryListRefSelect('_00_list_ref')).toContain('FROM _00_list_ref ');
384
+ expect(buildSubqueryListRefSelect('_00_list_ref_user_abc')).toContain(
385
+ 'FROM _00_list_ref_user_abc '
386
+ );
387
+ });
388
+
389
+ it('filters by the bound query id', () => {
390
+ expect(buildSubqueryListRefSelect('_00_list_ref')).toContain('WHERE in = $in');
391
+ });
392
+
393
+ it('selects ONLY subquery child rows via parent IS NOT NONE', () => {
394
+ // The mirror of `buildListRefSelect`'s `parent IS NONE`: the SSP writes
395
+ // `.related()` child rows into list_ref tagged with `parent`/`parent_rel`;
396
+ // this select pulls exactly those so their bodies can be synced into the
397
+ // local cache (separately from the primary window) and the correlated
398
+ // surql re-materializes with related data on a cold reload.
399
+ const sql = buildSubqueryListRefSelect('_00_list_ref');
400
+ expect(sql).toContain('parent IS NOT NONE');
401
+ expect(sql).not.toContain('parent IS NONE ');
402
+ });
403
+
404
+ it('selects only `out` and `version`', () => {
405
+ const sql = buildSubqueryListRefSelect('_00_list_ref');
406
+ expect(sql.startsWith('SELECT out, version FROM ')).toBe(true);
407
+ });
408
+ });
409
+
410
+ describe('nextPollDelayMs', () => {
411
+ it('returns the base interval when LIVE has never delivered', () => {
412
+ expect(
413
+ nextPollDelayMs({
414
+ now: 10_000,
415
+ lastLiveEventAt: null,
416
+ baseIntervalMs: 500,
417
+ })
418
+ ).toBe(500);
419
+ });
420
+
421
+ it('returns the healthy interval when a LIVE event fired within the cooldown', () => {
422
+ expect(
423
+ nextPollDelayMs({
424
+ now: 10_000,
425
+ lastLiveEventAt: 8_000, // 2s ago, well within 5s cooldown
426
+ baseIntervalMs: 500,
427
+ })
428
+ ).toBe(5_000);
429
+ });
430
+
431
+ it('snaps back to the base interval after the cooldown elapses', () => {
432
+ expect(
433
+ nextPollDelayMs({
434
+ now: 20_000,
435
+ lastLiveEventAt: 10_000, // 10s ago, well past cooldown
436
+ baseIntervalMs: 500,
437
+ })
438
+ ).toBe(500);
439
+ });
440
+
441
+ it('treats the cooldown boundary as expired (>= cooldownMs returns base)', () => {
442
+ // Exactly at the boundary, the LIVE feed is considered cold so we
443
+ // resume the aggressive cadence. Picks the same direction as
444
+ // `if (sinceLive >= cooldownMs)`.
445
+ expect(
446
+ nextPollDelayMs({
447
+ now: 15_000,
448
+ lastLiveEventAt: 10_000, // exactly 5s ago
449
+ baseIntervalMs: 500,
450
+ cooldownMs: 5_000,
451
+ })
452
+ ).toBe(500);
453
+ });
454
+
455
+ it('clamps a clock-skew negative sinceLive to the base interval', () => {
456
+ // If a stale timestamp puts the LIVE event in the future, we
457
+ // should not interpret that as "LIVE healthy" — treat it as
458
+ // unknown and use the conservative aggressive cadence.
459
+ expect(
460
+ nextPollDelayMs({
461
+ now: 10_000,
462
+ lastLiveEventAt: 20_000, // future timestamp
463
+ baseIntervalMs: 500,
464
+ })
465
+ ).toBe(500);
466
+ });
467
+
468
+ it('never widens below an aggressively-configured base interval', () => {
469
+ // If the caller picked a faster base (e.g. 100ms) on purpose,
470
+ // the healthy path should not silently slow things back to 5s.
471
+ // The healthy interval is clamped to at least base.
472
+ expect(
473
+ nextPollDelayMs({
474
+ now: 10_000,
475
+ lastLiveEventAt: 9_500,
476
+ baseIntervalMs: 8_000,
477
+ })
478
+ ).toBe(8_000);
479
+ });
480
+
481
+ it('respects custom cooldown and healthy-interval values', () => {
482
+ expect(
483
+ nextPollDelayMs({
484
+ now: 10_000,
485
+ lastLiveEventAt: 9_500, // 0.5s ago
486
+ baseIntervalMs: 250,
487
+ cooldownMs: 1_000,
488
+ healthyIntervalMs: 2_000,
489
+ })
490
+ ).toBe(2_000);
491
+ expect(
492
+ nextPollDelayMs({
493
+ now: 10_000,
494
+ lastLiveEventAt: 8_500, // 1.5s ago > cooldown
495
+ baseIntervalMs: 250,
496
+ cooldownMs: 1_000,
497
+ healthyIntervalMs: 2_000,
498
+ })
499
+ ).toBe(250);
500
+ });
501
+ });
502
+
503
+ describe('listRefPollDelayMs', () => {
504
+ it('returns the base interval at idle streak 0 (something just happened)', () => {
505
+ expect(listRefPollDelayMs({ idleStreak: 0, baseIntervalMs: 500 })).toBe(500);
506
+ // Negative streak is defensively treated as "active" too.
507
+ expect(listRefPollDelayMs({ idleStreak: -3, baseIntervalMs: 500 })).toBe(500);
508
+ });
509
+
510
+ it('doubles per idle streak (exponential backoff)', () => {
511
+ expect(listRefPollDelayMs({ idleStreak: 1, baseIntervalMs: 500 })).toBe(1_000);
512
+ expect(listRefPollDelayMs({ idleStreak: 2, baseIntervalMs: 500 })).toBe(2_000);
513
+ expect(listRefPollDelayMs({ idleStreak: 3, baseIntervalMs: 500 })).toBe(4_000);
514
+ });
515
+
516
+ it('caps at LIST_REF_POLL_MAX_INTERVAL_MS (5s) once the doubling exceeds it', () => {
517
+ // streak 4 would be 8000ms uncapped → clamped to 5000.
518
+ expect(listRefPollDelayMs({ idleStreak: 4, baseIntervalMs: 500 })).toBe(5_000);
519
+ expect(listRefPollDelayMs({ idleStreak: 50, baseIntervalMs: 500 })).toBe(5_000);
520
+ expect(LIST_REF_POLL_MAX_INTERVAL_MS).toBe(5_000);
521
+ });
522
+
523
+ it('never returns below the configured base, even when base exceeds the cap', () => {
524
+ // An aggressively-large base must not be implicitly shrunk by the cap.
525
+ expect(listRefPollDelayMs({ idleStreak: 0, baseIntervalMs: 8_000 })).toBe(8_000);
526
+ expect(listRefPollDelayMs({ idleStreak: 5, baseIntervalMs: 8_000 })).toBe(8_000);
527
+ });
528
+
529
+ it('respects a custom max interval', () => {
530
+ expect(
531
+ listRefPollDelayMs({ idleStreak: 10, baseIntervalMs: 500, maxIntervalMs: 3_000 })
532
+ ).toBe(3_000);
533
+ });
534
+
535
+ it('does not overflow for a very long idle streak', () => {
536
+ // 2^1000 would be Infinity; the exponent clamp keeps it finite and capped.
537
+ expect(listRefPollDelayMs({ idleStreak: 1_000, baseIntervalMs: 500 })).toBe(5_000);
538
+ });
539
+ });
540
+
541
+ describe('recordVersionArraysEqual', () => {
542
+ it('treats the same reference and identical contents as equal', () => {
543
+ const a: RecordVersionArray = [['game:1', 1], ['game:2', 3]];
544
+ expect(recordVersionArraysEqual(a, a)).toBe(true);
545
+ expect(recordVersionArraysEqual(a, [['game:1', 1], ['game:2', 3]])).toBe(true);
546
+ });
547
+
548
+ it('is order-insensitive (the list_ref SELECT has no ORDER BY)', () => {
549
+ expect(
550
+ recordVersionArraysEqual(
551
+ [['game:1', 1], ['game:2', 3]],
552
+ [['game:2', 3], ['game:1', 1]]
553
+ )
554
+ ).toBe(true);
555
+ });
556
+
557
+ it('returns false when a version differs for the same id', () => {
558
+ expect(
559
+ recordVersionArraysEqual([['game:1', 1]], [['game:1', 2]])
560
+ ).toBe(false);
561
+ });
562
+
563
+ it('returns false on length mismatch', () => {
564
+ expect(
565
+ recordVersionArraysEqual([['game:1', 1]], [['game:1', 1], ['game:2', 1]])
566
+ ).toBe(false);
567
+ });
568
+
569
+ it('returns false when an id is present in one but not the other', () => {
570
+ expect(
571
+ recordVersionArraysEqual([['game:1', 1]], [['game:2', 1]])
572
+ ).toBe(false);
573
+ });
574
+
575
+ it('treats two empty arrays as equal', () => {
576
+ expect(recordVersionArraysEqual([], [])).toBe(true);
577
+ });
578
+ });
@@ -1,5 +1,5 @@
1
- import { RecordId } from 'surrealdb';
2
- import { RecordVersionArray, RecordVersionDiff } from '../../types';
1
+ import type { RecordId } from 'surrealdb';
2
+ import type { RecordVersionArray, RecordVersionDiff } from '../../types';
3
3
  import { parseRecordIdString, encodeRecordId } from '../../utils/index';
4
4
 
5
5
  export class ArraySyncer {
@@ -8,8 +8,8 @@ export class ArraySyncer {
8
8
  private needsSort = false;
9
9
 
10
10
  constructor(localArray: RecordVersionArray, remoteArray: RecordVersionArray) {
11
- this.remoteArray = remoteArray.sort((a, b) => a[0].localeCompare(b[0]));
12
- this.localArray = localArray.sort((a, b) => a[0].localeCompare(b[0]));
11
+ this.remoteArray = remoteArray.toSorted((a, b) => a[0].localeCompare(b[0]));
12
+ this.localArray = localArray.toSorted((a, b) => a[0].localeCompare(b[0]));
13
13
  }
14
14
 
15
15
  /**
@@ -49,7 +49,6 @@ export class ArraySyncer {
49
49
  this.localArray.sort((a, b) => a[0].localeCompare(b[0]));
50
50
  this.needsSort = false;
51
51
  }
52
- console.log('xxxx555', this.localArray, this.remoteArray);
53
52
  const diff = diffRecordVersionArray(this.localArray, this.remoteArray);
54
53
  return diff;
55
54
  }
@@ -93,10 +92,12 @@ export function diffRecordVersionArray(
93
92
  return {
94
93
  added: added.map((id) => ({
95
94
  id: parseRecordIdString(id),
95
+ // oxlint-disable-next-line no-non-null-assertion
96
96
  version: remoteMap.get(id)!,
97
97
  })),
98
98
  updated: updated.map((id) => ({
99
99
  id: parseRecordIdString(id),
100
+ // oxlint-disable-next-line no-non-null-assertion
100
101
  version: remoteMap.get(id)!,
101
102
  })),
102
103
  removed: removed.map(parseRecordIdString),
@@ -127,7 +128,7 @@ export function applyRecordVersionDiff(
127
128
  currentMap.set(encodeRecordId(item.id), item.version);
128
129
  }
129
130
 
130
- return Array.from(currentMap).sort((a, b) => a[0].localeCompare(b[0]));
131
+ return Array.from(currentMap).toSorted((a, b) => a[0].localeCompare(b[0]));
131
132
  }
132
133
 
133
134
  export function createDiffFromDbOp(
@@ -136,17 +137,14 @@ export function createDiffFromDbOp(
136
137
  version: number,
137
138
  versions?: RecordVersionArray
138
139
  ): RecordVersionDiff {
139
- // Version guard: skip stale CREATE/UPDATE, but always process DELETE
140
- if (op !== 'DELETE') {
141
- const old = versions?.find((record) => record[0] === encodeRecordId(recordId));
142
-
143
- if (old && old[1] >= version) {
144
- return {
145
- added: [],
146
- updated: [],
147
- removed: [],
148
- };
149
- }
140
+ const old = versions?.find((record) => record[0] === encodeRecordId(recordId));
141
+
142
+ if (old && old[1] >= version) {
143
+ return {
144
+ added: [],
145
+ updated: [],
146
+ removed: [],
147
+ };
150
148
  }
151
149
 
152
150
  if (op === 'CREATE') {
@@ -169,3 +167,189 @@ export function createDiffFromDbOp(
169
167
  };
170
168
  }
171
169
  }
170
+
171
+ /**
172
+ * Default cadence for the `_00_list_ref_user_<id>` poll fallback. The
173
+ * poll is the safety net for SurrealDB v3's occasionally-dropped LIVE
174
+ * deliveries; 500ms is aggressive enough to feel real-time on the
175
+ * happy path while keeping the per-session query load bounded.
176
+ */
177
+ export const DEFAULT_LIST_REF_POLL_INTERVAL_MS = 500;
178
+
179
+ /**
180
+ * Build the SurrealQL select that powers both the initial-fetch and
181
+ * the periodic poll of `_00_list_ref[_user_<id>]`. The `parent IS NONE`
182
+ * predicate excludes subquery entries (rows with `parent_rel` set)
183
+ * because the client's `RecordVersionArray` only tracks primary rows;
184
+ * including subquery rows would surface them as spurious "added"
185
+ * diffs every tick.
186
+ */
187
+ export function buildListRefSelect(table: string): string {
188
+ return `SELECT out, version FROM ${table} WHERE in = $in AND parent IS NONE`;
189
+ }
190
+
191
+ /**
192
+ * Build the select that says whether an EMPTY id-set means "this query has no
193
+ * rows" or "the server has not published them yet".
194
+ *
195
+ * The SSP writes `rowCount` onto the `_00_query` row in the same statement that
196
+ * registers the view — synchronously, and BEFORE it hands the view's initial
197
+ * edges to the coalescing edge flusher. So the two are not interchangeable:
198
+ * `rowCount > 0` with no edges is the flush window, and only `rowCount === 0`
199
+ * is a genuinely empty query. Polling the edges alone cannot tell those apart
200
+ * no matter how long it waits, which is why this is read alongside them rather
201
+ * than a retry counter.
202
+ *
203
+ * Returns `NONE` (→ null) when the row is not readable or does not exist yet;
204
+ * callers must treat that as "unknown", not as zero.
205
+ */
206
+ export function buildQueryRowCountSelect(): string {
207
+ return 'SELECT VALUE rowCount FROM ONLY $in';
208
+ }
209
+
210
+ /**
211
+ * Build the SurrealQL select for a query's SUBQUERY child edges — the
212
+ * mirror of {@link buildListRefSelect}. `.related()` queries register a
213
+ * correlated subquery; the SSP materializes each matched child as a
214
+ * `_00_list_ref` edge tagged with `parent`/`parent_rel` (see
215
+ * `apps/ssp` edge writer). `parent IS NONE` (the primary select) drops
216
+ * these, so their bodies never reach the local cache and a cold-reload
217
+ * re-materialization of the correlated surql yields empty related
218
+ * fields. This `parent IS NOT NONE` variant pulls the child `out`+`version`
219
+ * pairs (any nesting depth) so we can sync their bodies into the local
220
+ * store SEPARATELY from the primary window array.
221
+ */
222
+ export function buildSubqueryListRefSelect(table: string): string {
223
+ return `SELECT out, version FROM ${table} WHERE in = $in AND parent IS NOT NONE`;
224
+ }
225
+
226
+ /**
227
+ * Resolve the effective list-ref poll interval. Negative or zero
228
+ * values fall back to the default — accepting them would either
229
+ * disable polling silently or busy-loop the event loop.
230
+ */
231
+ export function resolveListRefPollInterval(opt?: number): number {
232
+ if (typeof opt !== 'number' || !Number.isFinite(opt) || opt <= 0) {
233
+ return DEFAULT_LIST_REF_POLL_INTERVAL_MS;
234
+ }
235
+ return opt;
236
+ }
237
+
238
+ /**
239
+ * When the LIVE feed has delivered an event within this window, treat
240
+ * the LIVE subscription as healthy and back the poll off to
241
+ * {@link LIVE_HEALTHY_POLL_INTERVAL_MS}. As soon as LIVE quiets for
242
+ * longer than this, the poll snaps back to the aggressive default.
243
+ */
244
+ export const LIVE_HEALTHY_COOLDOWN_MS = 5_000;
245
+
246
+ /**
247
+ * Poll interval while LIVE is delivering events. The aggressive
248
+ * default 500ms costs ~120 queries / minute / session — wasted work
249
+ * when LIVE is already covering us. 5s keeps the safety net in place
250
+ * at 1/10th the load.
251
+ */
252
+ export const LIVE_HEALTHY_POLL_INTERVAL_MS = 5_000;
253
+
254
+ /**
255
+ * Pick the next poll delay based on whether LIVE has been healthy
256
+ * recently. If a LIVE event fired within `cooldownMs`, use the slow
257
+ * (`healthyIntervalMs`) cadence; otherwise the fast (`baseIntervalMs`)
258
+ * cadence. Pure so it's unit-testable; `Sp00kySync.startListRefPoll`
259
+ * calls it from a self-rescheduling timer.
260
+ *
261
+ * The healthy interval is clamped to at least `baseIntervalMs` so
262
+ * configuring an aggressive base (e.g. 100ms) never gets implicitly
263
+ * widened by this helper.
264
+ *
265
+ * @deprecated Superseded by {@link listRefPollDelayMs}, which backs the
266
+ * poll off based on observed change activity (LIVE *or* poll-detected)
267
+ * rather than LIVE liveness alone — the cross-session LIVE-permission gap
268
+ * means LIVE often never fires, so this helper would keep the poll pinned
269
+ * at the aggressive base forever even on a fully idle page. Kept (and
270
+ * tested) for reference.
271
+ */
272
+ export function nextPollDelayMs(args: {
273
+ now: number;
274
+ lastLiveEventAt: number | null;
275
+ baseIntervalMs: number;
276
+ cooldownMs?: number;
277
+ healthyIntervalMs?: number;
278
+ }): number {
279
+ const {
280
+ now,
281
+ lastLiveEventAt,
282
+ baseIntervalMs,
283
+ cooldownMs = LIVE_HEALTHY_COOLDOWN_MS,
284
+ healthyIntervalMs = LIVE_HEALTHY_POLL_INTERVAL_MS,
285
+ } = args;
286
+ if (lastLiveEventAt === null) return baseIntervalMs;
287
+ const sinceLive = now - lastLiveEventAt;
288
+ if (sinceLive < 0 || sinceLive >= cooldownMs) return baseIntervalMs;
289
+ return Math.max(healthyIntervalMs, baseIntervalMs);
290
+ }
291
+
292
+ /**
293
+ * Ceiling for the adaptive list_ref poll backoff. An idle page (no LIVE
294
+ * events, no poll-detected list_ref changes) coasts up to this cadence;
295
+ * the existing healthy-LIVE safety net runs at the same 5s, so this keeps
296
+ * the worst-case catch-up latency for a missed cross-session change at the
297
+ * cadence the codebase already treats as acceptable.
298
+ */
299
+ export const LIST_REF_POLL_MAX_INTERVAL_MS = 5_000;
300
+
301
+ /**
302
+ * Adaptive poll delay: stay at the responsive `baseIntervalMs` while
303
+ * changes are arriving, and exponentially back off toward `maxIntervalMs`
304
+ * while the `_00_list_ref` is quiet.
305
+ *
306
+ * `idleStreak` is the count of consecutive poll cycles that observed *no*
307
+ * change. `Sp00kySync` resets it to 0 whenever a poll detects a real
308
+ * remoteArray change OR a LIVE event lands, so any activity snaps the poll
309
+ * straight back to `baseIntervalMs`. A streak of 0 (something just
310
+ * happened) → base; otherwise `base * 2^streak` capped at `maxIntervalMs`.
311
+ *
312
+ * This replaces {@link nextPollDelayMs}: the old helper slowed the poll
313
+ * only while LIVE was *delivering*, but the cross-session LIVE-permission
314
+ * gap means LIVE frequently never fires here, so it left a fully idle page
315
+ * polling every `base` ms forever (the "continuous queries while idle"
316
+ * symptom). Backing off on observed idleness instead covers the
317
+ * LIVE-healthy case for free (LIVE applies the change → the next poll sees
318
+ * nothing new → the streak grows → it backs off).
319
+ */
320
+ export function listRefPollDelayMs(args: {
321
+ idleStreak: number;
322
+ baseIntervalMs: number;
323
+ maxIntervalMs?: number;
324
+ }): number {
325
+ const { idleStreak, baseIntervalMs, maxIntervalMs = LIST_REF_POLL_MAX_INTERVAL_MS } = args;
326
+ const cap = Math.max(baseIntervalMs, maxIntervalMs);
327
+ if (idleStreak <= 0) return baseIntervalMs;
328
+ // 2^streak grows fast; clamp the exponent so it can't overflow on a
329
+ // page left idle for a very long time.
330
+ const exponent = Math.min(idleStreak, 30);
331
+ return Math.min(baseIntervalMs * 2 ** exponent, cap);
332
+ }
333
+
334
+ /**
335
+ * Order-insensitive equality for two `RecordVersionArray`s (each a list of
336
+ * `[recordIdString, version]`). The `_00_list_ref` SELECT has no `ORDER
337
+ * BY`, so row order can differ between polls without anything having
338
+ * actually changed — comparing as an id→version map avoids false
339
+ * "changed" verdicts that would defeat the idle backoff. Record ids are
340
+ * unique within a query's list_ref, so a map is a faithful representation.
341
+ */
342
+ export function recordVersionArraysEqual(
343
+ a: RecordVersionArray,
344
+ b: RecordVersionArray
345
+ ): boolean {
346
+ if (a === b) return true;
347
+ if (a.length !== b.length) return false;
348
+ const byId = new Map<string, number>();
349
+ for (const [id, version] of a) byId.set(id, version);
350
+ for (const [id, version] of b) {
351
+ // `get` returns undefined for a missing id, which never === a number.
352
+ if (byId.get(id) !== version) return false;
353
+ }
354
+ return true;
355
+ }
package/src/otel/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Level } from 'pino';
2
- import { PinoTransmit } from '../types';
1
+ import type { Level } from 'pino';
2
+ import type { PinoTransmit } from '../types';
3
3
 
4
4
  // Map pino levels to OTEL severity numbers
5
5
  // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
@@ -32,7 +32,7 @@ async function loadOtelModules(otelEndpoint: string) {
32
32
  ]);
33
33
 
34
34
  const resource = resourceFromAttributes({
35
- [ATTR_SERVICE_NAME]: 'spooky-client',
35
+ [ATTR_SERVICE_NAME]: 'sp00ky-client',
36
36
  });
37
37
 
38
38
  const exporter = new OTLPLogExporter({
@@ -61,7 +61,7 @@ async function loadOtelModules(otelEndpoint: string) {
61
61
  * ```ts
62
62
  * import { createOtelTransmit } from '@spooky-sync/core/otel';
63
63
  *
64
- * new SpookyClient({
64
+ * new Sp00kyClient({
65
65
  * // ...
66
66
  * otelTransmit: createOtelTransmit('http://localhost:4318/v1/logs'),
67
67
  * });
@@ -74,6 +74,7 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
74
74
  return {
75
75
  level: level,
76
76
  send: (levelLabel: string, logEvent: any) => {
77
+ // oxlint-disable-next-line promise/always-return
77
78
  otelReady.then((getOtelLogger) => {
78
79
  try {
79
80
  const messages = [...logEvent.messages];
@@ -81,15 +82,15 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
81
82
 
82
83
  // Construct the message body
83
84
  let body = '';
84
- const msg = messages.pop();
85
+ const lastMsg = messages.pop();
85
86
 
86
- if (typeof msg === 'string') {
87
- body = msg;
88
- } else if (msg) {
89
- body = JSON.stringify(msg);
87
+ if (typeof lastMsg === 'string') {
88
+ body = lastMsg;
89
+ } else if (lastMsg) {
90
+ body = JSON.stringify(lastMsg);
90
91
  }
91
92
 
92
- let category = 'spooky-client::unknown';
93
+ let category = 'sp00ky-client::unknown';
93
94
 
94
95
  const attributes = {};
95
96
  for (const msg of messages) {
@@ -114,9 +115,11 @@ export function createOtelTransmit(endpoint: string, level: Level = 'info'): Pin
114
115
  timestamp: new Date(logEvent.ts),
115
116
  });
116
117
  } catch (e) {
118
+ // oxlint-disable-next-line no-console
117
119
  console.warn('Failed to transmit log to OTEL endpoint', e);
118
120
  }
119
121
  }).catch((e) => {
122
+ // oxlint-disable-next-line no-console
120
123
  console.warn('Failed to load OpenTelemetry modules', e);
121
124
  });
122
125
  },