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

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,40 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { fx } from './effects';
3
+
4
+ describe('effect constructors', () => {
5
+ it('build every effect kind as plain data', () => {
6
+ const sealed = { sql: 'x;', extract: (r: unknown[]) => r[0] };
7
+ const plan = { table: 't' } as any;
8
+ const cases: Array<[ReturnType<typeof fx.now>, string]> = [
9
+ [fx.local.query('SELECT 1', { a: 1 }, 3), 'local.query'],
10
+ [fx.local.select(plan, { b: 2 }), 'local.select'],
11
+ [fx.local.execute(sealed, {}, 1), 'local.execute'],
12
+ [fx.local.getById('t', 'x'), 'local.getById'],
13
+ [fx.local.upsert('t', 'x', { a: 1 }, 'merge'), 'local.upsert'],
14
+ [fx.local.delete('t', 'x'), 'local.delete'],
15
+ [fx.remote.query('RETURN true', undefined, 10), 'remote.query'],
16
+ [fx.remote.live('_00_list_ref'), 'remote.live'],
17
+ [fx.remote.kill('u'), 'remote.kill'],
18
+ [fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '10m', tableName: 't' }), 'ssp.register'],
19
+ [fx.ssp.unregister('h'), 'ssp.unregister'],
20
+ [fx.ssp.ingest([]), 'ssp.ingest'],
21
+ [fx.timer.set('k', 5, { type: 'Drain' }), 'timer.set'],
22
+ [fx.timer.clear('k'), 'timer.clear'],
23
+ [fx.state.read((s) => s.tabId), 'state.read'],
24
+ [fx.state.update((s) => s), 'state.update'],
25
+ [fx.state.wait((s) => s.primed), 'state.wait'],
26
+ [fx.now(), 'now'],
27
+ [fx.id('mutation'), 'id'],
28
+ [fx.hash('abc'), 'hash'],
29
+ [fx.emit({ type: 'tray:changed', count: 1 }), 'emit'],
30
+ [fx.dispatch({ type: 'FetchRows' }), 'dispatch'],
31
+ [fx.all([fx.now()]), 'all'],
32
+ [fx.service('local.connect', 'anon'), 'service'],
33
+ ];
34
+ for (const [effect, kind] of cases) expect(effect.kind).toBe(kind);
35
+ expect(fx.local.query('SELECT 1', { a: 1 }, 3)).toEqual({ kind: 'local.query', sql: 'SELECT 1', vars: { a: 1 }, epoch: 3 });
36
+ expect(fx.local.epoch()).toEqual({ kind: 'local.epoch' });
37
+ expect(fx.timer.set('k', 5, { type: 'Drain' })).toEqual({ kind: 'timer.set', key: 'k', ms: 5, event: { type: 'Drain' } });
38
+ expect(fx.service('hint.write', 'u1')).toEqual({ kind: 'service', name: 'hint.write', args: ['u1'] });
39
+ });
40
+ });
@@ -0,0 +1,161 @@
1
+ import type { QueryPlan } from '@spooky-sync/query-builder';
2
+ import type { SealedQuery } from '../utils/surql';
3
+ import type { ClientState } from '../state/client-state';
4
+ import type { IngestRecord } from '../services/stream-processor/index';
5
+ import type { RecordVersionArray, RegistrationTimings } from '../types';
6
+ import type { OutEvent, RuntimeEvent } from './events';
7
+
8
+ export type Vars = Record<string, unknown>;
9
+
10
+ /** What the in-browser SSP needs to build a local view. */
11
+ export interface RegisterPlan {
12
+ queryHash: string;
13
+ surql: string;
14
+ params: Vars;
15
+ ttl: string;
16
+ tableName: string;
17
+ involvedTables?: string[];
18
+ }
19
+ export interface RegisterResult {
20
+ localArray: RecordVersionArray;
21
+ timings: RegistrationTimings;
22
+ }
23
+
24
+ /** One statement's outcome of a multi-statement remote request. */
25
+ export type StatementResult =
26
+ | { status: 'OK'; result: unknown }
27
+ | { status: 'ERR'; error: string };
28
+
29
+ /** One entry of an `all` fan-out result (allSettled semantics). */
30
+ export type Settled<T = unknown> = { ok: true; value: T } | { ok: false; error: unknown };
31
+
32
+ /**
33
+ * Effects are data. A saga yields one and gets its result back on the next
34
+ * `next()`; the interpreter (client/runtime) is the only place they execute.
35
+ */
36
+ export type Effect =
37
+ | { kind: 'local.query'; sql: string; vars?: Vars; epoch?: number }
38
+ | { kind: 'local.select'; plan: QueryPlan; params?: Vars }
39
+ | { kind: 'local.execute'; query: SealedQuery<unknown>; vars?: Vars; epoch?: number }
40
+ | { kind: 'local.getById'; table: string; id: unknown }
41
+ | { kind: 'local.upsert'; table: string; id: unknown; data: Record<string, unknown>; mode: 'replace' | 'merge' }
42
+ | { kind: 'local.delete'; table: string; id: unknown }
43
+ | { kind: 'local.epoch' }
44
+ | { kind: 'remote.query'; sql: string; vars?: Vars; timeoutMs?: number }
45
+ | { kind: 'remote.live'; table: string }
46
+ | { kind: 'remote.kill'; uuid: string }
47
+ | { kind: 'ssp.register'; plan: RegisterPlan }
48
+ | { kind: 'ssp.unregister'; hash: string }
49
+ | { kind: 'ssp.ingest'; records: IngestRecord[] }
50
+ | { kind: 'timer.set'; key: string; ms: number; event: RuntimeEvent }
51
+ | { kind: 'timer.clear'; key: string }
52
+ | { kind: 'state.read'; select: (s: ClientState) => unknown }
53
+ | { kind: 'state.update'; fn: (s: ClientState) => ClientState }
54
+ | { kind: 'state.wait'; until: (s: ClientState) => boolean }
55
+ | { kind: 'now' }
56
+ | { kind: 'id'; scope: 'mutation' | 'salt' }
57
+ | { kind: 'hash'; input: string }
58
+ | { kind: 'emit'; event: OutEvent }
59
+ | { kind: 'dispatch'; event: RuntimeEvent }
60
+ | { kind: 'all'; effects: Effect[] }
61
+ | { kind: 'service'; name: ServiceName; args: unknown[] };
62
+
63
+ export type EffectKind = Effect['kind'];
64
+
65
+ /**
66
+ * Calls into the legacy services that are adapters, not state: auth, tabs,
67
+ * blobs, migrator, crdt, persistence, the SSP lifecycle, the remote socket.
68
+ * A saga names the call; the runtime binds it to the real service.
69
+ */
70
+ export interface ServiceCalls {
71
+ 'hint.read': () => string | null;
72
+ 'hint.write': (bucketId: string) => void;
73
+ 'local.connect': (bucketId: string) => Promise<void>;
74
+ 'local.switchStore': (bucketId: string) => Promise<void>;
75
+ 'local.beginSwitch': () => () => void;
76
+ 'local.currentBucketId': () => string;
77
+ 'local.usesSurqlSchema': () => boolean;
78
+ 'local.dropBucket': (bucketId: string) => Promise<void>;
79
+ 'migrator.provision': () => Promise<void>;
80
+ 'blobs.start': (bucketId: string) => Promise<void>;
81
+ 'blobs.setNamespace': (bucketId: string) => Promise<void>;
82
+ 'blobs.clear': () => Promise<void>;
83
+ 'ssp.init': () => Promise<void>;
84
+ 'ssp.setPermissions': () => void;
85
+ 'ssp.setSessionAuth': (authId: string | null, access: string | null) => void;
86
+ 'ssp.prime': (pendingIds: string[]) => Promise<void>;
87
+ 'ssp.reset': () => Promise<void>;
88
+ 'ssp.setPersistence': (enabled: boolean) => void;
89
+ 'auth.restoreSession': () => Promise<string | null>;
90
+ 'auth.init': () => Promise<void>;
91
+ 'auth.sessionAuthId': () => string | null;
92
+ 'auth.access': () => string | null;
93
+ 'auth.token': () => string | null;
94
+ 'auth.currentUser': () => Record<string, unknown> | null;
95
+ 'auth.consumeEndedImpersonation': () => string | null;
96
+ 'remote.connect': () => Promise<void>;
97
+ 'remote.releaseViews': (ids: unknown[]) => void;
98
+ 'supervisor.start': () => void;
99
+ 'tabs.start': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
100
+ 'tabs.moveToBucket': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
101
+ 'crdt.setSessionId': (sessionId: string) => void;
102
+ 'crdt.closeAll': (flush: boolean) => void;
103
+ 'persistence.set': (key: string, value: unknown) => Promise<void>;
104
+ 'features.init': () => void;
105
+ 'releases.init': () => void;
106
+ 'window.attach': () => void;
107
+ }
108
+ export type ServiceName = keyof ServiceCalls;
109
+
110
+ /** Typed constructors. Each is one line so the saga reads like the plan. */
111
+ export const fx = {
112
+ local: {
113
+ query: (sql: string, vars?: Vars, epoch?: number): Effect => ({ kind: 'local.query', sql, vars, epoch }),
114
+ select: (plan: QueryPlan, params?: Vars): Effect => ({ kind: 'local.select', plan, params }),
115
+ execute: (query: SealedQuery<unknown>, vars?: Vars, epoch?: number): Effect => ({
116
+ kind: 'local.execute',
117
+ query,
118
+ vars,
119
+ epoch,
120
+ }),
121
+ getById: (table: string, id: unknown): Effect => ({ kind: 'local.getById', table, id }),
122
+ upsert: (table: string, id: unknown, data: Record<string, unknown>, mode: 'replace' | 'merge'): Effect => ({
123
+ kind: 'local.upsert',
124
+ table,
125
+ id,
126
+ data,
127
+ mode,
128
+ }),
129
+ delete: (table: string, id: unknown): Effect => ({ kind: 'local.delete', table, id }),
130
+ /** The store's current epoch; a write fenced with it is dropped after a bucket switch or transport failover. */
131
+ epoch: (): Effect => ({ kind: 'local.epoch' }),
132
+ },
133
+ remote: {
134
+ query: (sql: string, vars?: Vars, timeoutMs?: number): Effect => ({ kind: 'remote.query', sql, vars, timeoutMs }),
135
+ /** Subscribe to `LIVE SELECT * FROM <table>`; resolves to the live uuid. */
136
+ live: (table: string): Effect => ({ kind: 'remote.live', table }),
137
+ kill: (uuid: string): Effect => ({ kind: 'remote.kill', uuid }),
138
+ },
139
+ ssp: {
140
+ register: (plan: RegisterPlan): Effect => ({ kind: 'ssp.register', plan }),
141
+ unregister: (hash: string): Effect => ({ kind: 'ssp.unregister', hash }),
142
+ ingest: (records: IngestRecord[]): Effect => ({ kind: 'ssp.ingest', records }),
143
+ },
144
+ timer: {
145
+ set: (key: string, ms: number, event: RuntimeEvent): Effect => ({ kind: 'timer.set', key, ms, event }),
146
+ clear: (key: string): Effect => ({ kind: 'timer.clear', key }),
147
+ },
148
+ state: {
149
+ read: <T>(select: (s: ClientState) => T): Effect => ({ kind: 'state.read', select }),
150
+ update: (fn: (s: ClientState) => ClientState): Effect => ({ kind: 'state.update', fn }),
151
+ /** Suspend until `until(state)` holds (checked after every state.update). */
152
+ wait: (until: (s: ClientState) => boolean): Effect => ({ kind: 'state.wait', until }),
153
+ },
154
+ now: (): Effect => ({ kind: 'now' }),
155
+ id: (scope: 'mutation' | 'salt'): Effect => ({ kind: 'id', scope }),
156
+ hash: (input: string): Effect => ({ kind: 'hash', input }),
157
+ emit: (event: OutEvent): Effect => ({ kind: 'emit', event }),
158
+ dispatch: (event: RuntimeEvent): Effect => ({ kind: 'dispatch', event }),
159
+ all: (effects: Effect[]): Effect => ({ kind: 'all', effects }),
160
+ service: <N extends ServiceName>(name: N, ...args: Parameters<ServiceCalls[N]>): Effect => ({ kind: 'service', name, args }),
161
+ } as const;
@@ -0,0 +1,56 @@
1
+ import type { ConnectionState, InlineRow, QueryHash, QueryStatus, SyncHealth } from '../types';
2
+ import type { StreamUpdate } from '../services/stream-processor/index';
3
+
4
+ /**
5
+ * Inbound events. Everything that can start a saga arrives as one of these
6
+ * through the runtime router: public API calls, adapter callbacks, timers,
7
+ * and `dispatch` effects from other sagas.
8
+ */
9
+ export type RuntimeEvent =
10
+ | { type: 'EnsureRegistered'; requireAuth?: boolean; attempt?: number }
11
+ | { type: 'RegisterRemote'; hash: QueryHash; retry?: boolean }
12
+ | { type: 'SyncOutcome'; ok: boolean; error?: unknown }
13
+ | { type: 'AckPrune' }
14
+ | { type: 'ReadDirtyMembership' }
15
+ | { type: 'RecoverLostView'; hash: QueryHash }
16
+ | { type: 'ReadMembership'; hashes: QueryHash[] }
17
+ | { type: 'FetchRows' }
18
+ | { type: 'Materialize'; hash: QueryHash }
19
+ | { type: 'MaterializeDirty' }
20
+ | { type: 'LifecycleTick' }
21
+ | { type: 'GcTick' }
22
+ | { type: 'Drain' }
23
+ | { type: 'FlushWrite'; key: string }
24
+ | { type: 'PollTick' }
25
+ | { type: 'SelfHealTick' }
26
+ | { type: 'HeartbeatNow' }
27
+ | { type: 'StartRemote' }
28
+ | { type: 'PrimeCircuit' }
29
+ | { type: 'VersionsPrimed'; entries: Array<readonly [string, number]> }
30
+ | { type: 'WarmBlobs' }
31
+ | { type: 'AuthFlip'; userId: string | null }
32
+ | { type: 'BucketSwitch'; target: string }
33
+ | { type: 'PageHide' }
34
+ | { type: 'ConnectionChanged'; state: ConnectionState }
35
+ | { type: 'StreamUpdate'; update: StreamUpdate }
36
+ | { type: 'LiveStart' }
37
+ | { type: 'LiveChange'; hashes: QueryHash[]; rows?: InlineRow[] }
38
+ | { type: 'TabRole'; role: 'solo' | 'leader' | 'follower' }
39
+ | { type: 'TabMessage'; message: unknown };
40
+
41
+ export type OutEvent =
42
+ | { type: 'query:records'; hash: QueryHash; records: ReadonlyArray<Record<string, unknown>> }
43
+ | { type: 'query:status'; hash: QueryHash; status: QueryStatus }
44
+ | { type: 'query:authority'; hash: QueryHash; known: boolean }
45
+ | { type: 'query:view-lost'; hash: QueryHash }
46
+ | { type: 'query:evicted'; hash: QueryHash }
47
+ | { type: 'mutation:event'; event: unknown }
48
+ | { type: 'mutation:settled'; mutationId: string; recordId: string; eventType: string }
49
+ | { type: 'mutation:rolled-back'; mutationId: string; recordId: string; eventType: string; error: string }
50
+ | { type: 'tray:changed'; count: number }
51
+ | { type: 'health:changed'; health: SyncHealth }
52
+ | { type: 'activity:changed'; fetching: number; pending: number }
53
+ | { type: 'tabs:broadcast'; message: unknown }
54
+ | { type: 'tabs:sendTo'; tabId: string; message: unknown }
55
+ | { type: 'devtools'; name: string; data?: unknown }
56
+ | { type: 'log'; level: 'debug' | 'info' | 'warn' | 'error'; message: string; data?: unknown };
@@ -0,0 +1,116 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { RecordId } from 'surrealdb';
3
+ import { fakeAdapters, fakeHost } from '../testing/adapters';
4
+ import { createInterpreter } from './interpreter';
5
+ import { fx } from './effects';
6
+ import { setFailedCount } from '../state/reducers';
7
+
8
+ describe('interpreter', () => {
9
+ it('routes store, remote, ssp and service effects to the adapters', async () => {
10
+ const a = fakeAdapters({
11
+ remote: { queryResponses: async () => [{ status: 'OK', result: 1 }] },
12
+ ssp: {
13
+ registerQueryPlan: (plan) => ({ queryHash: plan.queryHash, localArray: [['t:1', 1]], registration: { parseMs: 1, planMs: 2, snapshotMs: 3 } }),
14
+ },
15
+ services: { 'hint.read': () => 'u1' },
16
+ });
17
+ const h = fakeHost();
18
+ const run = createInterpreter(a.adapters, h.host);
19
+ const sealed = { sql: 'SELECT 1;', extract: (r: unknown[]) => r };
20
+ await run(fx.local.query('SELECT 1', { a: 1 }, 2));
21
+ await run(fx.local.query('SELECT 2'));
22
+ await run(fx.local.execute(sealed, {}, 1));
23
+ await run(fx.local.select({ table: 't' } as any, {}));
24
+ await run(fx.local.getById('t', 'x'));
25
+ await run(fx.local.upsert('t', 'x', { a: 1 }, 'merge'));
26
+ await run(fx.local.delete('t', 'x'));
27
+ expect(await run(fx.local.epoch())).toBe(0);
28
+ expect(await run(fx.remote.query('RETURN 1'))).toEqual([{ status: 'OK', result: 1 }]);
29
+ expect(await run(fx.remote.live('_00_list_ref'))).toBe('live-uuid');
30
+ await run(fx.remote.kill('live-uuid'));
31
+ const reg = await run(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '10m', tableName: 't' }));
32
+ expect(reg).toEqual({ localArray: [['t:1', 1]], timings: { parseMs: 1, planMs: 2, snapshotMs: 3, wallMs: null } });
33
+ await run(fx.ssp.unregister('h'));
34
+ await run(fx.ssp.ingest([]));
35
+ expect(await run(fx.service('hint.read'))).toBe('u1');
36
+ expect(a.names()).toEqual([
37
+ 'local.query', 'local.query', 'local.execute', 'local.select', 'local.getById', 'local.upsert', 'local.delete',
38
+ 'remote.live', 'remote.kill', 'ssp.unregister', 'ssp.ingest', 'service.hint.read',
39
+ ]);
40
+ expect(a.calls[0]).toEqual(['local.query', ['SELECT 1', { a: 1 }]]);
41
+ });
42
+
43
+ it('ssp.register without a result throws; ssp.register uses default timings when the SSP reports none', async () => {
44
+ const empty = fakeAdapters({ ssp: { registerQueryPlan: () => undefined } });
45
+ const h = fakeHost();
46
+ await expect(createInterpreter(empty.adapters, h.host)(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '1m', tableName: 't' }))).rejects.toThrow(/not initialized/);
47
+ const bare = fakeAdapters();
48
+ const reg = await createInterpreter(bare.adapters, h.host)(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '1m', tableName: 't' }));
49
+ expect(reg).toEqual({ localArray: [], timings: { parseMs: null, planMs: null, snapshotMs: null, wallMs: null } });
50
+ });
51
+
52
+ it('remote.query honours the per-effect timeout', async () => {
53
+ const slow = fakeAdapters({ remote: { queryResponses: () => new Promise(() => {}) } });
54
+ const h = fakeHost();
55
+ await expect(createInterpreter(slow.adapters, h.host)(fx.remote.query('SLOW', undefined, 5))).rejects.toThrow(/timed out/);
56
+ });
57
+
58
+ it('state, wait, clock, ids, hash, timers, emit, dispatch, all', async () => {
59
+ const a = fakeAdapters({ now: 42 });
60
+ const h = fakeHost();
61
+ const run = createInterpreter(a.adapters, h.host);
62
+ expect(await run(fx.state.read((s) => s.tabId))).toBe('tab-a');
63
+ await run(fx.state.update(setFailedCount(2)));
64
+ expect(h.state.failedCount).toBe(2);
65
+ await run(fx.state.wait((s) => s.failedCount === 2));
66
+ const waiting = run(fx.state.wait((s) => s.failedCount === 3));
67
+ h.host.setState(setFailedCount(3)(h.state));
68
+ await waiting;
69
+ const ctrl = new AbortController();
70
+ const aborted = run(fx.state.wait(() => false), { signal: ctrl.signal });
71
+ ctrl.abort();
72
+ await expect(aborted).rejects.toThrow('aborted');
73
+ expect(await run(fx.now())).toBe(42);
74
+ expect(String(await run(fx.id('mutation')))).toMatch(/^_00_pending_mutations:/);
75
+ expect(String(await run(fx.id('salt')))).toMatch(/^salt-/);
76
+ expect(await run(fx.hash('abc'))).toMatch(/^ba7816bf/);
77
+ await run(fx.timer.set('k', 10, { type: 'Drain' }));
78
+ expect(a.timers.pending.has('k')).toBe(true);
79
+ a.timers.fire('k');
80
+ expect(h.dispatched).toEqual([{ type: 'Drain' }]);
81
+ await run(fx.timer.set('j', 1, { type: 'FetchRows' }));
82
+ await run(fx.timer.clear('j'));
83
+ expect(a.timers.pending.has('j')).toBe(false);
84
+ await run(fx.emit({ type: 'tray:changed', count: 1 }));
85
+ expect(h.emitted).toEqual([{ type: 'tray:changed', count: 1 }]);
86
+ await run(fx.dispatch({ type: 'PollTick' }));
87
+ expect(h.dispatched.at(-1)).toEqual({ type: 'PollTick' });
88
+ const settled = await run(fx.all([fx.now(), fx.remote.query('x'), fx.service('hint.read')]));
89
+ expect(settled).toEqual([{ ok: true, value: 42 }, { ok: true, value: [] }, { ok: true, value: undefined }]);
90
+ const failing = fakeAdapters({
91
+ remote: {
92
+ queryResponses: async () => {
93
+ throw new Error('boom');
94
+ },
95
+ },
96
+ });
97
+ const mixed = await createInterpreter(failing.adapters, h.host)(fx.all([fx.remote.query('x'), fx.now()]));
98
+ expect(mixed).toEqual([{ ok: false, error: new Error('boom') }, { ok: true, value: 1_700_000_000_000 }]);
99
+ });
100
+
101
+ it('live events reach the host as LiveChange dispatches', async () => {
102
+ let onChange: ((h: string[]) => void) | null = null;
103
+ const a = fakeAdapters({
104
+ remote: {
105
+ live: async (_t, cb) => {
106
+ onChange = cb;
107
+ return 'u';
108
+ },
109
+ },
110
+ });
111
+ const h = fakeHost();
112
+ await createInterpreter(a.adapters, h.host)(fx.remote.live('_00_list_ref'));
113
+ onChange!(['a', 'b']);
114
+ expect(h.dispatched).toEqual([{ type: 'LiveChange', hashes: ['a', 'b'] }]);
115
+ });
116
+ });
@@ -0,0 +1,148 @@
1
+ import { Duration } from 'surrealdb';
2
+ import type { LocalStore } from '../services/database/cache-engine';
3
+ import type { IngestRecord, StreamUpdate } from '../services/stream-processor/index';
4
+ import type { ClientState } from '../state/client-state';
5
+ import type { InlineRow } from '../types';
6
+ import { withTimeout } from '../utils/index';
7
+ import type { Effect, RegisterResult, ServiceCalls, Settled, StatementResult } from './effects';
8
+ import type { OutEvent, RuntimeEvent } from './events';
9
+
10
+ /** The five adapters plus the pure ports, as the interpreter sees them. */
11
+ export interface Adapters {
12
+ local: Pick<LocalStore, 'query' | 'execute' | 'select' | 'getById' | 'upsert' | 'delete'> & { readonly epoch: number };
13
+ remote: {
14
+ queryResponses(sql: string, vars?: Record<string, unknown>): Promise<StatementResult[]>;
15
+ /**
16
+ * Subscribe to `LIVE SELECT * FROM <table>`; the callback receives the
17
+ * changed query hashes, plus the changed rows themselves when the
18
+ * subscription was opened with the body joined on (`liveInlineBodies`).
19
+ */
20
+ live(table: string, onChange: (hashes: string[], rows?: InlineRow[]) => void): Promise<string>;
21
+ kill(uuid: string): Promise<void>;
22
+ };
23
+ ssp: {
24
+ registerQueryPlan(plan: {
25
+ queryHash: string;
26
+ surql: string;
27
+ params: Record<string, unknown>;
28
+ ttl: Duration;
29
+ lastActiveAt: Date;
30
+ localArray: [];
31
+ remoteArray: [];
32
+ meta: { tableName: string; involvedTables?: string[] };
33
+ }): StreamUpdate | undefined;
34
+ unregisterQueryPlan(hash: string): void;
35
+ ingestMany(records: IngestRecord[]): unknown;
36
+ };
37
+ timers: { set(key: string, ms: number, fire: () => void): void; clear(key: string): void };
38
+ clock: { now(): number };
39
+ ids: { mutation(): string; salt(): string };
40
+ hash(input: string): Promise<string>;
41
+ services: ServiceCalls;
42
+ }
43
+
44
+ /** What the interpreter needs from the runtime: state, waiting, emitting, dispatching. */
45
+ export interface InterpreterHost {
46
+ getState(): ClientState;
47
+ setState(next: ClientState): void;
48
+ waitFor(until: (s: ClientState) => boolean, signal?: AbortSignal): Promise<void>;
49
+ emit(event: OutEvent): void;
50
+ dispatch(event: RuntimeEvent): void;
51
+ }
52
+
53
+ export interface RunContext {
54
+ signal?: AbortSignal;
55
+ }
56
+
57
+ export type Interpreter = (effect: Effect, ctx?: RunContext) => Promise<unknown>;
58
+
59
+ /** Effects become adapter calls here and nowhere else. */
60
+ export function createInterpreter(adapters: Adapters, host: InterpreterHost): Interpreter {
61
+ const interpret: Interpreter = async (effect, ctx) => {
62
+ switch (effect.kind) {
63
+ case 'local.query':
64
+ return adapters.local.query(effect.sql, effect.vars, effect.epoch === undefined ? undefined : { epoch: effect.epoch });
65
+ case 'local.select':
66
+ return adapters.local.select(effect.plan, effect.params);
67
+ case 'local.execute':
68
+ return adapters.local.execute(effect.query, effect.vars, effect.epoch === undefined ? undefined : { epoch: effect.epoch });
69
+ case 'local.getById':
70
+ return adapters.local.getById(effect.table, effect.id);
71
+ case 'local.upsert':
72
+ return adapters.local.upsert(effect.table, effect.id, effect.data, effect.mode);
73
+ case 'local.delete':
74
+ return adapters.local.delete(effect.table, effect.id);
75
+ case 'local.epoch':
76
+ return adapters.local.epoch;
77
+ case 'remote.query': {
78
+ const pending = adapters.remote.queryResponses(effect.sql, effect.vars);
79
+ return effect.timeoutMs
80
+ ? withTimeout(pending, effect.timeoutMs, () => new Error(`Remote request timed out after ${effect.timeoutMs}ms`))
81
+ : pending;
82
+ }
83
+ case 'remote.live':
84
+ return adapters.remote.live(effect.table, (hashes, rows) => host.dispatch({ type: 'LiveChange', hashes, rows }));
85
+ case 'remote.kill':
86
+ return adapters.remote.kill(effect.uuid);
87
+ case 'ssp.register': {
88
+ const update = adapters.ssp.registerQueryPlan({
89
+ queryHash: effect.plan.queryHash,
90
+ surql: effect.plan.surql,
91
+ params: effect.plan.params,
92
+ ttl: new Duration(effect.plan.ttl),
93
+ lastActiveAt: new Date(adapters.clock.now()),
94
+ localArray: [],
95
+ remoteArray: [],
96
+ meta: { tableName: effect.plan.tableName, involvedTables: effect.plan.involvedTables },
97
+ });
98
+ if (!update) throw new Error('Stream processor is not initialized');
99
+ const result: RegisterResult = {
100
+ localArray: update.localArray,
101
+ timings: {
102
+ parseMs: update.registration?.parseMs ?? null,
103
+ planMs: update.registration?.planMs ?? null,
104
+ snapshotMs: update.registration?.snapshotMs ?? null,
105
+ wallMs: null,
106
+ },
107
+ };
108
+ return result;
109
+ }
110
+ case 'ssp.unregister':
111
+ return adapters.ssp.unregisterQueryPlan(effect.hash);
112
+ case 'ssp.ingest':
113
+ return adapters.ssp.ingestMany(effect.records);
114
+ case 'timer.set':
115
+ return adapters.timers.set(effect.key, effect.ms, () => host.dispatch(effect.event));
116
+ case 'timer.clear':
117
+ return adapters.timers.clear(effect.key);
118
+ case 'state.read':
119
+ return effect.select(host.getState());
120
+ case 'state.update': {
121
+ const next = effect.fn(host.getState());
122
+ host.setState(next);
123
+ return next;
124
+ }
125
+ case 'state.wait':
126
+ return host.waitFor(effect.until, ctx?.signal);
127
+ case 'now':
128
+ return adapters.clock.now();
129
+ case 'id':
130
+ return effect.scope === 'mutation' ? adapters.ids.mutation() : adapters.ids.salt();
131
+ case 'hash':
132
+ return adapters.hash(effect.input);
133
+ case 'emit':
134
+ return host.emit(effect.event);
135
+ case 'dispatch':
136
+ return host.dispatch(effect.event);
137
+ case 'all': {
138
+ const settled = await Promise.allSettled(effect.effects.map((e) => interpret(e, ctx)));
139
+ return settled.map((r): Settled => (r.status === 'fulfilled' ? { ok: true, value: r.value } : { ok: false, error: r.reason }));
140
+ }
141
+ case 'service': {
142
+ const fn = adapters.services[effect.name] as (...args: unknown[]) => unknown;
143
+ return fn(...effect.args);
144
+ }
145
+ }
146
+ };
147
+ return interpret;
148
+ }
@@ -0,0 +1,89 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Effect } from './effects';
3
+ import { fx } from './effects';
4
+ import { acquire, emptyLanes, release, runSaga } from './saga';
5
+ import type { Saga } from './saga';
6
+
7
+ describe('runSaga', () => {
8
+ it('threads effect results back into the generator in order', async () => {
9
+ const seen: Effect[] = [];
10
+ function* saga(): Saga<number> {
11
+ const a = (yield fx.now()) as number;
12
+ const b = (yield fx.hash('x')) as number;
13
+ return a + b;
14
+ }
15
+ const result = await runSaga(saga(), async (e) => {
16
+ seen.push(e);
17
+ return e.kind === 'now' ? 1 : 2;
18
+ });
19
+ expect(result).toBe(3);
20
+ expect(seen.map((e) => e.kind)).toEqual(['now', 'hash']);
21
+ });
22
+
23
+ it('throws a rejected effect into the saga so it can catch it', async () => {
24
+ function* saga(): Saga<string> {
25
+ try {
26
+ yield fx.now();
27
+ return 'unreachable';
28
+ } catch (err) {
29
+ return `caught:${(err as Error).message}`;
30
+ }
31
+ }
32
+ await expect(runSaga(saga(), async () => Promise.reject(new Error('boom')))).resolves.toBe('caught:boom');
33
+ });
34
+
35
+ it('propagates an error the saga does not catch', async () => {
36
+ function* saga(): Saga<void> {
37
+ yield fx.now();
38
+ }
39
+ await expect(runSaga(saga(), async () => Promise.reject(new Error('boom')))).rejects.toThrow('boom');
40
+ });
41
+
42
+ it('returns immediately for a saga that yields nothing', async () => {
43
+ // oxlint-disable-next-line require-yield
44
+ function* saga(): Saga<number> {
45
+ return 7;
46
+ }
47
+ await expect(runSaga(saga(), async () => undefined)).resolves.toBe(7);
48
+ });
49
+ });
50
+
51
+ describe('lanes', () => {
52
+ it('serial: first acquires, second waits, release hands over, final release frees', () => {
53
+ const lane = { kind: 'serial', key: 'outbox' } as const;
54
+ const a = acquire(emptyLanes(), lane);
55
+ expect(a.decision).toBe('start');
56
+ const b = acquire(a.state, lane);
57
+ expect(b.decision).toBe('wait');
58
+ const c = acquire(b.state, lane);
59
+ expect(c.decision).toBe('wait');
60
+ expect(c.state.waiting.get('outbox')).toBe(2);
61
+ const r1 = release(c.state, 'outbox');
62
+ expect(r1.startNext).toBe(true);
63
+ expect(r1.state.waiting.get('outbox')).toBe(1);
64
+ expect(r1.state.running.has('outbox')).toBe(true);
65
+ const r2 = release(r1.state, 'outbox');
66
+ expect(r2.startNext).toBe(true);
67
+ expect(r2.state.waiting.has('outbox')).toBe(false);
68
+ const r3 = release(r2.state, 'outbox');
69
+ expect(r3.startNext).toBe(false);
70
+ expect(r3.state.running.has('outbox')).toBe(false);
71
+ });
72
+
73
+ it('dedupe: a request during a run joins it and does not queue', () => {
74
+ const lane = { kind: 'dedupe', key: 'mat:h' } as const;
75
+ const a = acquire(emptyLanes(), lane);
76
+ const b = acquire(a.state, lane);
77
+ expect(b.decision).toBe('join');
78
+ expect(b.state).toBe(a.state);
79
+ const r = release(b.state, 'mat:h');
80
+ expect(r.startNext).toBe(false);
81
+ expect(r.state.running.size).toBe(0);
82
+ });
83
+
84
+ it('keys are independent', () => {
85
+ const a = acquire(emptyLanes(), { kind: 'serial', key: 'x' });
86
+ const b = acquire(a.state, { kind: 'serial', key: 'y' });
87
+ expect(b.decision).toBe('start');
88
+ });
89
+ });