@spooky-sync/core 0.0.1-canary.27 → 0.0.1-canary.271

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 (231) hide show
  1. package/AGENTS.md +78 -0
  2. package/README.md +56 -16
  3. package/dist/index.d.ts +2279 -81
  4. package/dist/index.js +13490 -2585
  5. package/dist/otel/index.d.ts +2 -2
  6. package/dist/otel/index.js +6 -6
  7. package/dist/sqlite-open.js +331 -0
  8. package/dist/sqlite-worker.d.ts +1 -0
  9. package/dist/sqlite-worker.js +439 -0
  10. package/dist/tabs-broker-worker.d.ts +8 -0
  11. package/dist/tabs-broker-worker.js +472 -0
  12. package/dist/types.d.ts +939 -46
  13. package/package.json +16 -8
  14. package/scripts/check-broker-bundle.mjs +33 -0
  15. package/skills/{spooky-core → sp00ky-core}/SKILL.md +30 -12
  16. package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
  17. package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
  18. package/src/boot/auth-flip.saga.test.ts +82 -0
  19. package/src/boot/auth-flip.saga.ts +62 -0
  20. package/src/boot/boot.saga.test.ts +151 -0
  21. package/src/boot/boot.saga.ts +128 -0
  22. package/src/boot/bucket-switch.saga.test.ts +187 -0
  23. package/src/boot/bucket-switch.saga.ts +131 -0
  24. package/src/boot/preload.saga.test.ts +51 -0
  25. package/src/boot/preload.saga.ts +31 -0
  26. package/src/bucket-blurhash.test.ts +148 -0
  27. package/src/build-globals.d.ts +12 -0
  28. package/src/client/bucket-handle.test.ts +117 -0
  29. package/src/client/bucket-handle.ts +227 -0
  30. package/src/client/router.test.ts +56 -0
  31. package/src/client/router.ts +96 -0
  32. package/src/client/runtime.test.ts +192 -0
  33. package/src/client/runtime.ts +296 -0
  34. package/src/client/services.ts +386 -0
  35. package/src/client/sp00ky-client.test.ts +264 -0
  36. package/src/client/sp00ky-client.ts +528 -0
  37. package/src/events/events.test.ts +2 -1
  38. package/src/events/index.ts +3 -0
  39. package/src/index.ts +37 -2
  40. package/src/kernel/constants.test.ts +19 -0
  41. package/src/kernel/constants.ts +61 -0
  42. package/src/kernel/effects.test.ts +40 -0
  43. package/src/kernel/effects.ts +161 -0
  44. package/src/kernel/events.ts +56 -0
  45. package/src/kernel/interpreter.test.ts +116 -0
  46. package/src/kernel/interpreter.ts +148 -0
  47. package/src/kernel/saga.test.ts +89 -0
  48. package/src/kernel/saga.ts +73 -0
  49. package/src/modules/app-release/index.test.ts +125 -0
  50. package/src/modules/app-release/index.ts +209 -0
  51. package/src/modules/auth/auth.local-first.test.ts +141 -0
  52. package/src/modules/auth/events/index.ts +9 -2
  53. package/src/modules/auth/impersonation-banner.ts +443 -0
  54. package/src/modules/auth/impersonation.test.ts +355 -0
  55. package/src/modules/auth/impersonation.ts +179 -0
  56. package/src/modules/auth/index.ts +344 -27
  57. package/src/modules/crdt/crdt-field.ts +294 -0
  58. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  59. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  60. package/src/modules/crdt/index.ts +463 -0
  61. package/src/modules/crdt/loro-loader.ts +25 -0
  62. package/src/modules/devtools/flags.ts +349 -0
  63. package/src/modules/devtools/index.ts +529 -47
  64. package/src/modules/devtools/notify-throttle.test.ts +154 -0
  65. package/src/modules/devtools/state-shape.test.ts +149 -0
  66. package/src/modules/devtools/storage-info.test.ts +79 -0
  67. package/src/modules/devtools/storage-info.ts +168 -0
  68. package/src/modules/devtools/versions.test.ts +74 -0
  69. package/src/modules/devtools/versions.ts +110 -0
  70. package/src/modules/feature-flag/index.test.ts +251 -0
  71. package/src/modules/feature-flag/index.ts +316 -0
  72. package/src/modules/ref-tables.test.ts +91 -0
  73. package/src/modules/ref-tables.ts +88 -0
  74. package/src/mutation/jobs.test.ts +25 -0
  75. package/src/mutation/jobs.ts +41 -0
  76. package/src/mutation/mutation-id.test.ts +37 -0
  77. package/src/mutation/mutation-id.ts +35 -0
  78. package/src/mutation/push.saga.test.ts +285 -0
  79. package/src/mutation/push.saga.ts +215 -0
  80. package/src/mutation/rows.test.ts +201 -0
  81. package/src/mutation/rows.ts +377 -0
  82. package/src/mutation/tray.saga.test.ts +63 -0
  83. package/src/mutation/tray.saga.ts +48 -0
  84. package/src/mutation/write.saga.test.ts +163 -0
  85. package/src/mutation/write.saga.ts +166 -0
  86. package/src/otel/index.ts +13 -10
  87. package/src/query/env.test.ts +23 -0
  88. package/src/query/env.ts +56 -0
  89. package/src/query/fetch.saga.test.ts +156 -0
  90. package/src/query/fetch.saga.ts +115 -0
  91. package/src/query/hash.test.ts +14 -0
  92. package/src/query/hash.ts +18 -0
  93. package/src/query/id-set-plan.test.ts +132 -0
  94. package/src/query/lifecycle.saga.test.ts +122 -0
  95. package/src/query/lifecycle.saga.ts +101 -0
  96. package/src/query/materialize.saga.test.ts +82 -0
  97. package/src/query/materialize.saga.ts +75 -0
  98. package/src/query/materialize.test.ts +50 -0
  99. package/src/query/materialize.ts +44 -0
  100. package/src/query/membership.saga.test.ts +322 -0
  101. package/src/query/membership.saga.ts +262 -0
  102. package/src/query/membership.test.ts +118 -0
  103. package/src/query/membership.ts +171 -0
  104. package/src/query/register.saga.test.ts +293 -0
  105. package/src/query/register.saga.ts +226 -0
  106. package/src/query/render-set.test.ts +36 -0
  107. package/src/query/render-set.ts +60 -0
  108. package/src/query/sql.test.ts +62 -0
  109. package/src/query/sql.ts +79 -0
  110. package/src/query/window-query.test.ts +70 -0
  111. package/src/query/window-query.ts +195 -0
  112. package/src/services/blobs/blob-cache.test.ts +359 -0
  113. package/src/services/blobs/blob-cache.ts +603 -0
  114. package/src/services/blobs/blob-manifest.ts +227 -0
  115. package/src/services/blobs/blob-store.test.ts +77 -0
  116. package/src/services/blobs/blob-store.ts +359 -0
  117. package/src/services/blobs/blob.fixture.ts +90 -0
  118. package/src/services/blobs/index.ts +70 -0
  119. package/src/services/database/cache-engine.ts +199 -0
  120. package/src/services/database/connection-supervisor.test.ts +318 -0
  121. package/src/services/database/connection-supervisor.ts +422 -0
  122. package/src/services/database/database.query-timeout.test.ts +140 -0
  123. package/src/services/database/database.responses.test.ts +43 -0
  124. package/src/services/database/database.ts +145 -62
  125. package/src/services/database/engine-factory.ts +33 -0
  126. package/src/services/database/errors.ts +34 -0
  127. package/src/services/database/events/index.ts +2 -1
  128. package/src/services/database/index.ts +7 -0
  129. package/src/services/database/local-migrator.ts +30 -27
  130. package/src/services/database/local.test.ts +64 -0
  131. package/src/services/database/local.ts +491 -67
  132. package/src/services/database/plan-render.test.ts +159 -0
  133. package/src/services/database/plan-render.ts +108 -0
  134. package/src/services/database/relation-resolver.test.ts +467 -0
  135. package/src/services/database/relation-resolver.ts +0 -0
  136. package/src/services/database/remote.ts +222 -15
  137. package/src/services/database/sqlite-cache-engine.test.ts +617 -0
  138. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  139. package/src/services/database/sqlite-cache-engine.ts +1366 -0
  140. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  141. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  142. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  143. package/src/services/database/sqlite-lock-verify.ts +45 -0
  144. package/src/services/database/sqlite-open.test.ts +150 -0
  145. package/src/services/database/sqlite-open.ts +164 -0
  146. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  147. package/src/services/database/sqlite-plan-sql.ts +153 -0
  148. package/src/services/database/sqlite-projection.test.ts +121 -0
  149. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  150. package/src/services/database/sqlite-select.test.ts +246 -0
  151. package/src/services/database/sqlite-select.ts +131 -0
  152. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  153. package/src/services/database/sqlite-transport.ts +224 -0
  154. package/src/services/database/sqlite-worker.ts +437 -0
  155. package/src/services/database/surql-translate.ts +416 -0
  156. package/src/services/database/surreal-cache-engine.ts +161 -0
  157. package/src/services/logger/index.ts +3 -2
  158. package/src/services/persistence/localstorage.ts +2 -2
  159. package/src/services/persistence/resilient.ts +11 -4
  160. package/src/services/persistence/surrealdb.ts +10 -10
  161. package/src/services/stream-processor/index.ts +796 -84
  162. package/src/services/stream-processor/permissions.test.ts +47 -0
  163. package/src/services/stream-processor/permissions.ts +53 -0
  164. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  165. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  166. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  167. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  168. package/src/services/stream-processor/wasm-types.ts +59 -3
  169. package/src/services/tabs/broker-client.ts +283 -0
  170. package/src/services/tabs/broker.test.ts +327 -0
  171. package/src/services/tabs/coordinator.test.ts +365 -0
  172. package/src/services/tabs/coordinator.ts +633 -0
  173. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  174. package/src/services/tabs/leader-locks.ts +75 -0
  175. package/src/services/tabs/protocol.ts +262 -0
  176. package/src/services/tabs/support.ts +36 -0
  177. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  178. package/src/sp00ky.ts +5 -0
  179. package/src/state/client-state.ts +191 -0
  180. package/src/state/lifecycle.test.ts +118 -0
  181. package/src/state/lifecycle.ts +86 -0
  182. package/src/state/reducers.test.ts +283 -0
  183. package/src/state/reducers.ts +488 -0
  184. package/src/state/selectors.test.ts +149 -0
  185. package/src/state/selectors.ts +184 -0
  186. package/src/sync/connection.saga.test.ts +86 -0
  187. package/src/sync/connection.saga.ts +85 -0
  188. package/src/sync/live.saga.test.ts +178 -0
  189. package/src/sync/live.saga.ts +107 -0
  190. package/src/sync/policy.health.test.ts +56 -0
  191. package/src/sync/policy.test.ts +659 -0
  192. package/src/sync/policy.ts +502 -0
  193. package/src/sync/poll.saga.test.ts +51 -0
  194. package/src/sync/poll.saga.ts +36 -0
  195. package/src/sync/tabs.saga.test.ts +74 -0
  196. package/src/sync/tabs.saga.ts +93 -0
  197. package/src/testing/adapters.ts +133 -0
  198. package/src/testing/build.ts +63 -0
  199. package/src/testing/fake-services.ts +34 -0
  200. package/src/testing/run-pure.test.ts +92 -0
  201. package/src/testing/run-pure.ts +128 -0
  202. package/src/testing/services.ts +17 -0
  203. package/src/types.ts +603 -14
  204. package/src/utils/blurhash.ts +90 -0
  205. package/src/utils/error-classification.test.ts +54 -0
  206. package/src/utils/error-classification.ts +25 -0
  207. package/src/utils/index.ts +79 -13
  208. package/src/utils/parser.test.ts +49 -120
  209. package/src/utils/parser.ts +32 -2
  210. package/src/utils/semver.test.ts +32 -0
  211. package/src/utils/semver.ts +30 -0
  212. package/src/utils/surql.ts +30 -18
  213. package/src/utils/utils.test.ts +2 -1
  214. package/src/utils/withRetry.test.ts +1 -1
  215. package/tsdown.config.ts +86 -1
  216. package/vitest.config.ts +48 -0
  217. package/src/modules/cache/index.ts +0 -241
  218. package/src/modules/cache/types.ts +0 -19
  219. package/src/modules/data/data.test.ts +0 -58
  220. package/src/modules/data/index.ts +0 -879
  221. package/src/modules/sync/engine.ts +0 -173
  222. package/src/modules/sync/events/index.ts +0 -77
  223. package/src/modules/sync/index.ts +0 -3
  224. package/src/modules/sync/queue/index.ts +0 -2
  225. package/src/modules/sync/queue/queue-down.ts +0 -89
  226. package/src/modules/sync/queue/queue-up.ts +0 -223
  227. package/src/modules/sync/scheduler.ts +0 -84
  228. package/src/modules/sync/sync.ts +0 -421
  229. package/src/modules/sync/utils.test.ts +0 -311
  230. package/src/modules/sync/utils.ts +0 -167
  231. package/src/spooky.ts +0 -395
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Backend versions of the stack components, derived from the entity list the
3
+ * backend `/info` endpoint exposes (read via the `fn::spooky::info()` SurrealQL
4
+ * function). Any component that isn't reported degrades to `'unavailable'`.
5
+ */
6
+ export interface BackendVersions {
7
+ ssp: string;
8
+ scheduler: string;
9
+ surrealdb: string;
10
+ }
11
+
12
+ export const UNAVAILABLE = 'unavailable';
13
+
14
+ /**
15
+ * A single stack entity as reported by `/info` (one per ssp / scheduler /
16
+ * backend). Carries far more than versions — status, uptime, ip, views — so the
17
+ * DevTools can render the whole stack. Extra fields are preserved verbatim.
18
+ */
19
+ /** One end-to-end sync-pipeline probe cycle recorded by the scheduler. */
20
+ export interface HeartbeatSample {
21
+ ts: number;
22
+ /** `null` for a failed cycle. */
23
+ ms: number | null;
24
+ ok: boolean;
25
+ }
26
+
27
+ /**
28
+ * E2E heartbeat state, reported on the scheduler entity only. The scheduler
29
+ * times a probe row's full round trip (DB event → ingest → broadcast → SSP
30
+ * circuit step); `samples` is its rolling window of recent cycles, which the
31
+ * DevTools sparkline renders.
32
+ */
33
+ export interface HeartbeatInfo {
34
+ enabled: boolean;
35
+ stale: boolean;
36
+ /** Nothing to measure right now (e.g. no ready SSPs mid-bootstrap). */
37
+ blocked?: boolean;
38
+ blocked_reason?: string | null;
39
+ last_e2e_ms: number | null;
40
+ last_ok_epoch_ms: number | null;
41
+ consecutive_failures: number;
42
+ interval_secs: number;
43
+ samples?: HeartbeatSample[];
44
+ }
45
+
46
+ export interface BackendEntity {
47
+ entity: string;
48
+ id?: string;
49
+ ip?: string | null;
50
+ status?: string;
51
+ version?: string;
52
+ surrealdb_version?: string;
53
+ uptime_seconds?: number;
54
+ views?: number;
55
+ /** Scheduler entity only. */
56
+ heartbeat?: HeartbeatInfo;
57
+ [key: string]: unknown;
58
+ }
59
+
60
+ export interface BackendInfo {
61
+ versions: BackendVersions;
62
+ entities: BackendEntity[];
63
+ }
64
+
65
+ export function emptyBackendVersions(): BackendVersions {
66
+ return { ssp: UNAVAILABLE, scheduler: UNAVAILABLE, surrealdb: UNAVAILABLE };
67
+ }
68
+
69
+ export function emptyBackendInfo(): BackendInfo {
70
+ return { versions: emptyBackendVersions(), entities: [] };
71
+ }
72
+
73
+ /** Strip a leading `surrealdb-` so versions read as bare semver (e.g. `2.0.3`). */
74
+ function normalizeServerVersion(v: string): string {
75
+ return String(v).replace(/^surrealdb-/i, '').trim();
76
+ }
77
+
78
+ /**
79
+ * Normalize whatever `RETURN fn::spooky::info()` resolves to into the entity
80
+ * array. The SurrealQL function returns the parsed `/info` array; depending on
81
+ * how the result is unwrapped it may arrive as the array itself, a single
82
+ * object, or `null`. Tolerant of all three.
83
+ */
84
+ export function toEntityArray(raw: unknown): BackendEntity[] {
85
+ if (Array.isArray(raw)) return raw.filter((e): e is BackendEntity => !!e && typeof e === 'object');
86
+ if (raw && typeof raw === 'object') return [raw as BackendEntity];
87
+ return [];
88
+ }
89
+
90
+ /**
91
+ * Derive component versions + the full entity list from a `/info` entity array.
92
+ * `surrealdb` is taken from whichever entity reports `surrealdb_version` (ssp or
93
+ * scheduler). Never throws; missing pieces stay `'unavailable'`.
94
+ */
95
+ export function parseBackendInfo(raw: unknown): BackendInfo {
96
+ const entities = toEntityArray(raw);
97
+ const versions = emptyBackendVersions();
98
+
99
+ for (const entity of entities) {
100
+ const version = entity.version ? String(entity.version) : undefined;
101
+ if (entity.entity === 'ssp' && version) versions.ssp = version;
102
+ else if (entity.entity === 'scheduler' && version) versions.scheduler = version;
103
+
104
+ if (versions.surrealdb === UNAVAILABLE && entity.surrealdb_version) {
105
+ versions.surrealdb = normalizeServerVersion(String(entity.surrealdb_version));
106
+ }
107
+ }
108
+
109
+ return { versions, entities };
110
+ }
@@ -0,0 +1,251 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import { FeatureFlagModule } from './index';
3
+
4
+ // vitest runs in the `node` environment here, so there is no localStorage.
5
+ // The module guards every access with `globalThis.localStorage?.`, which keeps
6
+ // overrides in-memory-only — this shim lets the persistence path be tested.
7
+ function installLocalStorage() {
8
+ const store = new Map<string, string>();
9
+ const shim = {
10
+ getItem: (k: string) => store.get(k) ?? null,
11
+ setItem: (k: string, v: string) => void store.set(k, v),
12
+ removeItem: (k: string) => void store.delete(k),
13
+ };
14
+ (globalThis as any).localStorage = shim;
15
+ return { store, uninstall: () => delete (globalThis as any).localStorage };
16
+ }
17
+
18
+ // Minimal mocks for the three deps the module touches. The DataModule mock
19
+ // captures the single subscribe callback so a test can push live results, and
20
+ // counts query() calls to assert the query is SHARED (one registration for all
21
+ // flags), not per-key.
22
+ function makeDeps() {
23
+ let subCb: ((records: unknown[]) => void) | null = null;
24
+ const calls: Array<{ sql: string; params: unknown }> = [];
25
+ let authCb: ((userId: string | null) => void) | null = null;
26
+
27
+ const dataModule = {
28
+ query: async (_table: string, sql: string, params: unknown) => {
29
+ calls.push({ sql, params });
30
+ return `hash:${calls.length}`;
31
+ },
32
+ subscribe: (_hash: string, cb: (records: unknown[]) => void) => {
33
+ subCb = cb;
34
+ return () => {
35
+ subCb = null;
36
+ };
37
+ },
38
+ };
39
+ const sync = { enqueueDownEvent: () => {} };
40
+ const auth = {
41
+ subscribe: (cb: (userId: string | null) => void) => {
42
+ authCb = cb;
43
+ return () => {
44
+ authCb = null;
45
+ };
46
+ },
47
+ };
48
+ const logger = { child: () => ({ warn: () => {} }) };
49
+
50
+ const deps = { dataModule, sync, auth, logger } as any;
51
+ return {
52
+ deps,
53
+ calls,
54
+ push: (records: unknown[]) => subCb?.(records),
55
+ setUser: (id: string | null) => authCb?.(id),
56
+ hasSub: () => subCb !== null,
57
+ };
58
+ }
59
+
60
+ const tick = () => new Promise((r) => setTimeout(r, 0));
61
+
62
+ describe('FeatureFlagModule', () => {
63
+ let env: ReturnType<typeof makeDeps>;
64
+ let mod: FeatureFlagModule<any>;
65
+
66
+ beforeEach(() => {
67
+ env = makeDeps();
68
+ mod = new FeatureFlagModule(env.deps);
69
+ });
70
+
71
+ it('registers ONE shared, unfiltered query for many flags', async () => {
72
+ mod.feature('alpha');
73
+ mod.feature('beta');
74
+ mod.feature('gamma');
75
+ await tick();
76
+
77
+ expect(env.calls.length).toBe(1);
78
+ expect(env.calls[0].sql).not.toContain('WHERE');
79
+ expect(env.calls[0].sql).toContain('FROM _00_user_feature');
80
+ expect(env.calls[0].params).toEqual({});
81
+ });
82
+
83
+ it('fans the shared result out to each handle by key', async () => {
84
+ const alpha = mod.feature('alpha', { fallback: 'off' });
85
+ const beta = mod.feature('beta', { fallback: 'off' });
86
+ const missing = mod.feature('missing', { fallback: 'off' });
87
+ await tick();
88
+
89
+ env.push([
90
+ { key: 'alpha', variant: 'on' },
91
+ { key: 'beta', variant: 'off' },
92
+ ]);
93
+
94
+ expect(alpha.enabled()).toBe(true);
95
+ expect(alpha.variant()).toBe('on');
96
+ expect(beta.enabled()).toBe(false); // assigned 'off'
97
+ expect(missing.enabled()).toBe(false); // no row → fallback 'off'
98
+ });
99
+
100
+ it('observes NEW assignments live without re-registering', async () => {
101
+ const flag = mod.feature('live', { fallback: 'off' });
102
+ await tick();
103
+
104
+ env.push([]); // user starts with no assignment
105
+ expect(flag.enabled()).toBe(false);
106
+
107
+ env.push([{ key: 'live', variant: 'on' }]); // assigned while observing
108
+ expect(flag.enabled()).toBe(true);
109
+
110
+ expect(env.calls.length).toBe(1); // still the same single query
111
+ });
112
+
113
+ it('seeds a late-created handle from the already-loaded snapshot', async () => {
114
+ mod.feature('alpha', { fallback: 'off' });
115
+ await tick();
116
+ env.push([{ key: 'alpha', variant: 'on' }]);
117
+
118
+ const late = mod.feature('alpha', { fallback: 'off' });
119
+ expect(late.enabled()).toBe(true); // no fallback flash
120
+ });
121
+
122
+ it('clears flags and re-observes on user change', async () => {
123
+ mod.init();
124
+ const flag = mod.feature('alpha', { fallback: 'off' });
125
+ await tick();
126
+ env.push([{ key: 'alpha', variant: 'on' }]);
127
+ expect(flag.enabled()).toBe(true);
128
+
129
+ env.setUser('user:other'); // sign-in as a different user
130
+ await tick();
131
+ expect(flag.enabled()).toBe(false); // cleared until the new query resolves
132
+ expect(env.hasSub()).toBe(true); // re-registered for the new user
133
+ });
134
+ });
135
+
136
+ // Local overrides force a variant in THIS browser only. They must win over the
137
+ // server assignment on every read path — `variant()`, `payload()`, `enabled()`
138
+ // and `subscribe()` all funnel through the same resolver, so a gap in any one
139
+ // of them is a gap in all of them.
140
+ describe('FeatureFlagModule local overrides', () => {
141
+ let env: ReturnType<typeof makeDeps>;
142
+ let mod: FeatureFlagModule<any>;
143
+ let ls: ReturnType<typeof installLocalStorage>;
144
+
145
+ beforeEach(() => {
146
+ ls = installLocalStorage();
147
+ env = makeDeps();
148
+ mod = new FeatureFlagModule(env.deps);
149
+ });
150
+
151
+ afterEach(() => ls.uninstall());
152
+
153
+ it('wins over the server assignment', async () => {
154
+ const flag = mod.feature('alpha', { fallback: 'off' });
155
+ await tick();
156
+ env.push([{ key: 'alpha', variant: 'off' }]);
157
+ expect(flag.enabled()).toBe(false);
158
+
159
+ mod.setLocalOverride('alpha', 'on');
160
+ expect(flag.variant()).toBe('on');
161
+ expect(flag.enabled()).toBe(true);
162
+ });
163
+
164
+ it('survives a later live result for the same key', async () => {
165
+ const flag = mod.feature('alpha', { fallback: 'off' });
166
+ await tick();
167
+ mod.setLocalOverride('alpha', 'on');
168
+
169
+ env.push([{ key: 'alpha', variant: 'off' }]); // server disagrees
170
+ expect(flag.variant()).toBe('on');
171
+ });
172
+
173
+ it('applies before the first result, without a fallback flash', () => {
174
+ mod.setLocalOverride('alpha', 'on');
175
+ const flag = mod.feature('alpha', { fallback: 'off' });
176
+ expect(flag.variant()).toBe('on'); // seeded even though nothing loaded yet
177
+ });
178
+
179
+ it('carries its own payload', async () => {
180
+ const flag = mod.feature('alpha', { fallback: 'off' });
181
+ await tick();
182
+ env.push([{ key: 'alpha', variant: 'off', payload: { copy: 'server' } }]);
183
+
184
+ mod.setLocalOverride('alpha', 'on', { copy: 'local' });
185
+ expect(flag.payload()).toEqual({ copy: 'local' });
186
+ });
187
+
188
+ it('notifies subscribers when set and cleared', async () => {
189
+ const flag = mod.feature('alpha', { fallback: 'off' });
190
+ await tick();
191
+ env.push([{ key: 'alpha', variant: 'off' }]);
192
+
193
+ const seen: (string | undefined)[] = [];
194
+ flag.subscribe((s) => seen.push(s.variant));
195
+ expect(seen).toEqual(['off']); // immediate call
196
+
197
+ mod.setLocalOverride('alpha', 'on');
198
+ mod.setLocalOverride('alpha', null);
199
+ expect(seen).toEqual(['off', 'on', 'off']);
200
+ });
201
+
202
+ it('restores the server assignment when cleared', async () => {
203
+ const flag = mod.feature('alpha', { fallback: 'off' });
204
+ await tick();
205
+ env.push([{ key: 'alpha', variant: 'treatment', payload: { copy: 'server' } }]);
206
+
207
+ mod.setLocalOverride('alpha', 'off');
208
+ expect(flag.variant()).toBe('off');
209
+
210
+ mod.clearLocalOverrides();
211
+ expect(flag.variant()).toBe('treatment');
212
+ expect(flag.payload()).toEqual({ copy: 'server' });
213
+ });
214
+
215
+ it('survives a user change — it is a browser setting, not a session one', async () => {
216
+ mod.init();
217
+ const flag = mod.feature('alpha', { fallback: 'off' });
218
+ await tick();
219
+ mod.setLocalOverride('alpha', 'on');
220
+
221
+ env.setUser('user:other');
222
+ await tick();
223
+ expect(flag.variant()).toBe('on');
224
+ });
225
+
226
+ it('persists to localStorage and reloads into a fresh module', () => {
227
+ mod.setLocalOverride('alpha', 'on', { copy: 'local' });
228
+ expect(mod.getLocalOverrides()).toEqual({ alpha: { variant: 'on', payload: { copy: 'local' } } });
229
+
230
+ // A new module reads the same page-origin store, as after a reload.
231
+ const reloaded = new FeatureFlagModule(makeDeps().deps);
232
+ expect(reloaded.getLocalOverrides()).toEqual({
233
+ alpha: { variant: 'on', payload: { copy: 'local' } },
234
+ });
235
+ expect(reloaded.feature('alpha', { fallback: 'off' }).variant()).toBe('on');
236
+ });
237
+
238
+ it('drops the storage key entirely once the last override is cleared', () => {
239
+ mod.setLocalOverride('alpha', 'on');
240
+ expect(ls.store.size).toBe(1);
241
+
242
+ mod.setLocalOverride('alpha', null);
243
+ expect(ls.store.size).toBe(0);
244
+ expect(new FeatureFlagModule(makeDeps().deps).getLocalOverrides()).toEqual({});
245
+ });
246
+
247
+ it('ignores a corrupt store rather than failing to construct', () => {
248
+ ls.store.set('sp00ky:feature-overrides', '{not json');
249
+ expect(() => new FeatureFlagModule(makeDeps().deps)).not.toThrow();
250
+ });
251
+ });
@@ -0,0 +1,316 @@
1
+ import type { SchemaStructure } from '@spooky-sync/query-builder';
2
+ import type { AuthService } from '../auth/index';
3
+ import type { Logger } from '../../services/logger/index';
4
+ import type { QueryTimeToLive } from '../../types';
5
+
6
+ // One shared LIVE query over ALL of the signed-in user's assignments — the
7
+ // `_00_user_feature` select permission scopes it to `user = $auth.id`, so no
8
+ // per-key `WHERE key = $key` param is needed. A single registration means every
9
+ // flag the user is (or becomes) assigned is observed at once: new assignments
10
+ // stream in live, and a handle for an unassigned key simply resolves to its
11
+ // fallback. Avoids one-registration-per-flag and the param-filtered live query.
12
+ const FEATURE_QUERY = 'SELECT key, variant, payload FROM _00_user_feature';
13
+
14
+ // Local overrides live on the PAGE origin, so they survive reloads and are
15
+ // shared across tabs of the app. Deliberately not the DevTools panel's own
16
+ // storage — the panel runs on a different origin and would get a separate
17
+ // bucket.
18
+ const OVERRIDE_STORAGE_KEY = 'sp00ky:feature-overrides';
19
+
20
+ interface FeatureRow {
21
+ key?: string;
22
+ variant?: string;
23
+ payload?: unknown;
24
+ }
25
+
26
+ export interface FeatureFlagSnapshot {
27
+ variant: string | undefined;
28
+ payload: unknown | undefined;
29
+ }
30
+
31
+ export interface FeatureFlagOptions {
32
+ fallback?: string;
33
+ ttl?: QueryTimeToLive;
34
+ }
35
+
36
+ /**
37
+ * A locally forced variant. Applies to THIS browser only and is never sent to
38
+ * the server — the assignment in `_00_user_feature` is untouched, so clearing
39
+ * the override restores whatever the server says.
40
+ */
41
+ export interface FeatureFlagOverride {
42
+ variant: string;
43
+ payload?: unknown;
44
+ }
45
+
46
+ export class FeatureFlagHandle {
47
+ private latest: FeatureFlagSnapshot = { variant: undefined, payload: undefined };
48
+ private listeners = new Set<(s: FeatureFlagSnapshot) => void>();
49
+ private unsubscribeFn: (() => void) | null = null;
50
+ private onCloseFn: (() => void) | null = null;
51
+ private closed = false;
52
+
53
+ constructor(
54
+ public readonly key: string,
55
+ public readonly fallback: string | undefined,
56
+ ) {}
57
+
58
+ attach(unsubscribe: () => void): void {
59
+ this.unsubscribeFn?.();
60
+ this.unsubscribeFn = unsubscribe;
61
+ }
62
+
63
+ detach(): void {
64
+ this.unsubscribeFn?.();
65
+ this.unsubscribeFn = null;
66
+ }
67
+
68
+ set(snapshot: FeatureFlagSnapshot): void {
69
+ if (this.closed) return;
70
+ this.latest = snapshot;
71
+ for (const cb of this.listeners) cb(snapshot);
72
+ }
73
+
74
+ variant(): string | undefined {
75
+ return this.latest.variant ?? this.fallback;
76
+ }
77
+
78
+ payload<T = unknown>(): T | undefined {
79
+ return this.latest.payload as T | undefined;
80
+ }
81
+
82
+ enabled(): boolean {
83
+ const v = this.variant();
84
+ return v !== undefined && v !== 'off';
85
+ }
86
+
87
+ subscribe(cb: (s: FeatureFlagSnapshot) => void): () => void {
88
+ this.listeners.add(cb);
89
+ cb({ variant: this.variant(), payload: this.latest.payload });
90
+ return () => {
91
+ this.listeners.delete(cb);
92
+ };
93
+ }
94
+
95
+ onClose(cb: () => void): void {
96
+ this.onCloseFn = cb;
97
+ }
98
+
99
+ close(): void {
100
+ if (this.closed) return;
101
+ this.closed = true;
102
+ this.listeners.clear();
103
+ this.detach();
104
+ this.onCloseFn?.();
105
+ }
106
+ }
107
+
108
+ /** The slice of the query engine this module drives: register a live query
109
+ * locally, ask for its remote registration, subscribe to its rows. */
110
+ export interface LiveQueryPort {
111
+ query(table: string, surql: string, params: Record<string, unknown>, ttl: QueryTimeToLive): Promise<string>;
112
+ subscribe(hash: string, cb: (records: Record<string, any>[]) => void, options?: { immediate?: boolean }): () => void;
113
+ }
114
+ export interface RemoteRegisterPort {
115
+ enqueueDownEvent(event: { type: 'register'; payload: { hash: string } }): void;
116
+ }
117
+
118
+ export interface FeatureFlagModuleDeps<S extends SchemaStructure> {
119
+ dataModule: LiveQueryPort;
120
+ sync: RemoteRegisterPort;
121
+ auth: Pick<AuthService<S>, 'subscribe'>;
122
+ logger: Logger;
123
+ }
124
+
125
+ export class FeatureFlagModule<S extends SchemaStructure> {
126
+ private logger: Logger;
127
+ private handles = new Set<FeatureFlagHandle>();
128
+ private authUnsubscribe: (() => void) | null = null;
129
+ private lastUserId: string | null = null;
130
+
131
+ // The single shared live query over the user's assignments.
132
+ private querySubscription: (() => void) | null = null;
133
+ private starting = false;
134
+ // Longest TTL any caller asked for (the query is shared across all flags).
135
+ private ttl: QueryTimeToLive = '10m';
136
+ // Latest assignment per key, plus whether the query has resolved at least
137
+ // once (so a handle created before the first result knows to wait vs. fall
138
+ // back). `snapshots` only holds ASSIGNED keys; an absent key → fallback.
139
+ private snapshots = new Map<string, FeatureFlagSnapshot>();
140
+ private loaded = false;
141
+ // Developer-forced variants, this browser only. Take precedence over the
142
+ // server assignment for every read path.
143
+ private overrides = new Map<string, FeatureFlagOverride>();
144
+
145
+ constructor(private deps: FeatureFlagModuleDeps<S>) {
146
+ this.logger = deps.logger.child({ service: 'FeatureFlagModule' });
147
+ // Loaded in the constructor rather than `init()` so an override applies
148
+ // even when `client.feature()` is called before auth resolves.
149
+ this.loadOverrides();
150
+ }
151
+
152
+ init(): void {
153
+ if (this.authUnsubscribe) return;
154
+ this.authUnsubscribe = this.deps.auth.subscribe((userId) => {
155
+ if (userId === this.lastUserId) return;
156
+ this.lastUserId = userId;
157
+ void this.refresh();
158
+ });
159
+ }
160
+
161
+ feature(key: string, options: FeatureFlagOptions = {}): FeatureFlagHandle {
162
+ const handle = new FeatureFlagHandle(key, options.fallback);
163
+ this.handles.add(handle);
164
+ handle.onClose(() => this.handles.delete(handle));
165
+ if (options.ttl) this.ttl = options.ttl;
166
+ // If the shared query already resolved, seed this handle immediately so a
167
+ // late `feature()` call doesn't flash the fallback for an assigned key.
168
+ // An override seeds it too, even before the first result: forcing a
169
+ // variant should take effect instantly, not one round trip later.
170
+ if (this.loaded || this.overrides.has(key)) {
171
+ handle.set(this.resolve(key));
172
+ }
173
+ void this.ensureStarted();
174
+ return handle;
175
+ }
176
+
177
+ async closeAll(): Promise<void> {
178
+ this.authUnsubscribe?.();
179
+ this.authUnsubscribe = null;
180
+ this.teardownQuery();
181
+ for (const handle of [...this.handles]) handle.close();
182
+ }
183
+
184
+ /** Auth changed: drop the old user's query/snapshots and re-observe. */
185
+ private async refresh(): Promise<void> {
186
+ this.teardownQuery();
187
+ this.loaded = false;
188
+ this.snapshots.clear();
189
+ // Clear handles immediately so a sign-out hides flag-gated UI without lag.
190
+ // `resolve` keeps any local override in place across the switch — it is a
191
+ // developer setting for this browser, not part of the session.
192
+ for (const handle of this.handles) {
193
+ handle.set(this.resolve(handle.key));
194
+ }
195
+ await this.ensureStarted();
196
+ }
197
+
198
+ private teardownQuery(): void {
199
+ this.querySubscription?.();
200
+ this.querySubscription = null;
201
+ }
202
+
203
+ /** Start the single shared live query (idempotent; no-op with no handles). */
204
+ private async ensureStarted(): Promise<void> {
205
+ if (this.querySubscription || this.starting || this.handles.size === 0) return;
206
+ this.starting = true;
207
+ try {
208
+ const hash = await this.deps.dataModule.query(
209
+ '_00_user_feature' as any,
210
+ FEATURE_QUERY,
211
+ {},
212
+ this.ttl,
213
+ );
214
+ this.deps.sync.enqueueDownEvent({ type: 'register', payload: { hash } });
215
+ this.querySubscription = this.deps.dataModule.subscribe(
216
+ hash,
217
+ (records) => this.applyRecords(records as FeatureRow[]),
218
+ { immediate: true },
219
+ );
220
+ } catch (err) {
221
+ this.logger.warn(
222
+ { err, Category: 'sp00ky-client::FeatureFlagModule::register' },
223
+ 'Failed to register feature flag query',
224
+ );
225
+ } finally {
226
+ this.starting = false;
227
+ }
228
+ }
229
+
230
+ /** Live query result → per-key snapshots → push to every active handle. */
231
+ private applyRecords(records: FeatureRow[]): void {
232
+ this.snapshots.clear();
233
+ for (const row of records ?? []) {
234
+ if (row && typeof row.key === 'string') {
235
+ this.snapshots.set(row.key, { variant: row.variant, payload: row.payload });
236
+ }
237
+ }
238
+ this.loaded = true;
239
+ this.pushAll();
240
+ }
241
+
242
+ // ===========================================================
243
+ // Local overrides (this browser only)
244
+ // ===========================================================
245
+
246
+ /**
247
+ * Force `key` to `variant` in THIS browser. Pass `null` to clear.
248
+ *
249
+ * Nothing is written to the server: the `_00_user_feature` assignment is
250
+ * untouched, so clearing restores whatever the server says. Persisted to
251
+ * localStorage on the page origin, so it survives a reload.
252
+ */
253
+ setLocalOverride(key: string, variant: string | null, payload?: unknown): void {
254
+ if (variant === null) this.overrides.delete(key);
255
+ else this.overrides.set(key, { variant, payload });
256
+ this.persistOverrides();
257
+ this.pushAll();
258
+ }
259
+
260
+ clearLocalOverrides(): void {
261
+ this.overrides.clear();
262
+ this.persistOverrides();
263
+ this.pushAll();
264
+ }
265
+
266
+ getLocalOverrides(): Record<string, FeatureFlagOverride> {
267
+ return Object.fromEntries(this.overrides);
268
+ }
269
+
270
+ /** The assignment for `key`, with any local override taking precedence. */
271
+ private resolve(key: string): FeatureFlagSnapshot {
272
+ const override = this.overrides.get(key);
273
+ if (override) return { variant: override.variant, payload: override.payload };
274
+ return this.snapshots.get(key) ?? { variant: undefined, payload: undefined };
275
+ }
276
+
277
+ private pushAll(): void {
278
+ for (const handle of this.handles) handle.set(this.resolve(handle.key));
279
+ }
280
+
281
+ private loadOverrides(): void {
282
+ try {
283
+ const raw = globalThis.localStorage?.getItem(OVERRIDE_STORAGE_KEY);
284
+ if (!raw) return;
285
+ const parsed = JSON.parse(raw) as Record<string, FeatureFlagOverride>;
286
+ for (const [key, value] of Object.entries(parsed ?? {})) {
287
+ if (value && typeof value.variant === 'string') this.overrides.set(key, value);
288
+ }
289
+ } catch (err) {
290
+ // Best-effort: a corrupt or unavailable store must never stop the
291
+ // client from booting. Same posture as the DevTools prefs helper.
292
+ this.logger.warn(
293
+ { err, Category: 'sp00ky-client::FeatureFlagModule::loadOverrides' },
294
+ 'Failed to read local feature flag overrides',
295
+ );
296
+ }
297
+ }
298
+
299
+ private persistOverrides(): void {
300
+ try {
301
+ if (this.overrides.size === 0) {
302
+ globalThis.localStorage?.removeItem(OVERRIDE_STORAGE_KEY);
303
+ return;
304
+ }
305
+ globalThis.localStorage?.setItem(
306
+ OVERRIDE_STORAGE_KEY,
307
+ JSON.stringify(this.getLocalOverrides()),
308
+ );
309
+ } catch (err) {
310
+ this.logger.warn(
311
+ { err, Category: 'sp00ky-client::FeatureFlagModule::persistOverrides' },
312
+ 'Failed to persist local feature flag overrides',
313
+ );
314
+ }
315
+ }
316
+ }