@spooky-sync/core 0.0.1-canary.25 → 0.0.1-canary.255

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 (228) hide show
  1. package/AGENTS.md +78 -0
  2. package/README.md +56 -16
  3. package/dist/index.d.ts +2187 -80
  4. package/dist/index.js +12653 -2581
  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 +787 -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 +158 -0
  23. package/src/boot/bucket-switch.saga.ts +115 -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 +94 -0
  32. package/src/client/runtime.test.ts +192 -0
  33. package/src/client/runtime.ts +296 -0
  34. package/src/client/services.ts +382 -0
  35. package/src/client/sp00ky-client.test.ts +264 -0
  36. package/src/client/sp00ky-client.ts +499 -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 +54 -0
  42. package/src/kernel/effects.test.ts +40 -0
  43. package/src/kernel/effects.ts +159 -0
  44. package/src/kernel/events.ts +55 -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 +2 -1
  53. package/src/modules/auth/index.ts +138 -25
  54. package/src/modules/crdt/crdt-field.ts +294 -0
  55. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  56. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  57. package/src/modules/crdt/index.ts +463 -0
  58. package/src/modules/crdt/loro-loader.ts +25 -0
  59. package/src/modules/devtools/flags.ts +349 -0
  60. package/src/modules/devtools/index.ts +459 -47
  61. package/src/modules/devtools/notify-throttle.test.ts +154 -0
  62. package/src/modules/devtools/state-shape.test.ts +149 -0
  63. package/src/modules/devtools/storage-info.test.ts +79 -0
  64. package/src/modules/devtools/storage-info.ts +168 -0
  65. package/src/modules/devtools/versions.test.ts +74 -0
  66. package/src/modules/devtools/versions.ts +110 -0
  67. package/src/modules/feature-flag/index.test.ts +251 -0
  68. package/src/modules/feature-flag/index.ts +316 -0
  69. package/src/modules/ref-tables.test.ts +91 -0
  70. package/src/modules/ref-tables.ts +88 -0
  71. package/src/mutation/jobs.test.ts +25 -0
  72. package/src/mutation/jobs.ts +41 -0
  73. package/src/mutation/mutation-id.test.ts +37 -0
  74. package/src/mutation/mutation-id.ts +35 -0
  75. package/src/mutation/push.saga.test.ts +285 -0
  76. package/src/mutation/push.saga.ts +215 -0
  77. package/src/mutation/rows.test.ts +201 -0
  78. package/src/mutation/rows.ts +377 -0
  79. package/src/mutation/tray.saga.test.ts +63 -0
  80. package/src/mutation/tray.saga.ts +48 -0
  81. package/src/mutation/write.saga.test.ts +163 -0
  82. package/src/mutation/write.saga.ts +166 -0
  83. package/src/otel/index.ts +13 -10
  84. package/src/query/env.test.ts +23 -0
  85. package/src/query/env.ts +56 -0
  86. package/src/query/fetch.saga.test.ts +156 -0
  87. package/src/query/fetch.saga.ts +115 -0
  88. package/src/query/hash.test.ts +14 -0
  89. package/src/query/hash.ts +18 -0
  90. package/src/query/id-set-plan.test.ts +132 -0
  91. package/src/query/lifecycle.saga.test.ts +122 -0
  92. package/src/query/lifecycle.saga.ts +101 -0
  93. package/src/query/materialize.saga.test.ts +82 -0
  94. package/src/query/materialize.saga.ts +75 -0
  95. package/src/query/materialize.test.ts +50 -0
  96. package/src/query/materialize.ts +44 -0
  97. package/src/query/membership.saga.test.ts +246 -0
  98. package/src/query/membership.saga.ts +208 -0
  99. package/src/query/membership.test.ts +118 -0
  100. package/src/query/membership.ts +158 -0
  101. package/src/query/register.saga.test.ts +285 -0
  102. package/src/query/register.saga.ts +216 -0
  103. package/src/query/render-set.test.ts +36 -0
  104. package/src/query/render-set.ts +60 -0
  105. package/src/query/sql.test.ts +62 -0
  106. package/src/query/sql.ts +79 -0
  107. package/src/query/window-query.test.ts +70 -0
  108. package/src/query/window-query.ts +195 -0
  109. package/src/services/blobs/blob-cache.test.ts +359 -0
  110. package/src/services/blobs/blob-cache.ts +603 -0
  111. package/src/services/blobs/blob-manifest.ts +227 -0
  112. package/src/services/blobs/blob-store.test.ts +77 -0
  113. package/src/services/blobs/blob-store.ts +359 -0
  114. package/src/services/blobs/blob.fixture.ts +90 -0
  115. package/src/services/blobs/index.ts +70 -0
  116. package/src/services/database/cache-engine.ts +193 -0
  117. package/src/services/database/connection-supervisor.test.ts +318 -0
  118. package/src/services/database/connection-supervisor.ts +422 -0
  119. package/src/services/database/database.query-timeout.test.ts +140 -0
  120. package/src/services/database/database.responses.test.ts +43 -0
  121. package/src/services/database/database.ts +145 -62
  122. package/src/services/database/engine-factory.ts +33 -0
  123. package/src/services/database/errors.ts +34 -0
  124. package/src/services/database/events/index.ts +2 -1
  125. package/src/services/database/index.ts +7 -0
  126. package/src/services/database/local-migrator.ts +30 -27
  127. package/src/services/database/local.test.ts +64 -0
  128. package/src/services/database/local.ts +484 -67
  129. package/src/services/database/plan-render.test.ts +159 -0
  130. package/src/services/database/plan-render.ts +108 -0
  131. package/src/services/database/relation-resolver.test.ts +467 -0
  132. package/src/services/database/relation-resolver.ts +0 -0
  133. package/src/services/database/remote.ts +222 -15
  134. package/src/services/database/sqlite-cache-engine.test.ts +617 -0
  135. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  136. package/src/services/database/sqlite-cache-engine.ts +1349 -0
  137. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  138. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  139. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  140. package/src/services/database/sqlite-lock-verify.ts +45 -0
  141. package/src/services/database/sqlite-open.test.ts +150 -0
  142. package/src/services/database/sqlite-open.ts +164 -0
  143. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  144. package/src/services/database/sqlite-plan-sql.ts +153 -0
  145. package/src/services/database/sqlite-projection.test.ts +121 -0
  146. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  147. package/src/services/database/sqlite-select.test.ts +246 -0
  148. package/src/services/database/sqlite-select.ts +131 -0
  149. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  150. package/src/services/database/sqlite-transport.ts +224 -0
  151. package/src/services/database/sqlite-worker.ts +437 -0
  152. package/src/services/database/surql-translate.ts +416 -0
  153. package/src/services/database/surreal-cache-engine.ts +161 -0
  154. package/src/services/logger/index.ts +3 -2
  155. package/src/services/persistence/localstorage.ts +2 -2
  156. package/src/services/persistence/resilient.ts +11 -4
  157. package/src/services/persistence/surrealdb.ts +10 -10
  158. package/src/services/stream-processor/index.ts +796 -84
  159. package/src/services/stream-processor/permissions.test.ts +47 -0
  160. package/src/services/stream-processor/permissions.ts +53 -0
  161. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  162. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  163. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  164. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  165. package/src/services/stream-processor/wasm-types.ts +59 -3
  166. package/src/services/tabs/broker-client.ts +283 -0
  167. package/src/services/tabs/broker.test.ts +327 -0
  168. package/src/services/tabs/coordinator.test.ts +365 -0
  169. package/src/services/tabs/coordinator.ts +633 -0
  170. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  171. package/src/services/tabs/leader-locks.ts +75 -0
  172. package/src/services/tabs/protocol.ts +262 -0
  173. package/src/services/tabs/support.ts +36 -0
  174. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  175. package/src/sp00ky.ts +5 -0
  176. package/src/state/client-state.ts +184 -0
  177. package/src/state/lifecycle.test.ts +118 -0
  178. package/src/state/lifecycle.ts +86 -0
  179. package/src/state/reducers.test.ts +280 -0
  180. package/src/state/reducers.ts +476 -0
  181. package/src/state/selectors.test.ts +149 -0
  182. package/src/state/selectors.ts +184 -0
  183. package/src/sync/connection.saga.test.ts +86 -0
  184. package/src/sync/connection.saga.ts +85 -0
  185. package/src/sync/live.saga.test.ts +178 -0
  186. package/src/sync/live.saga.ts +107 -0
  187. package/src/sync/policy.health.test.ts +56 -0
  188. package/src/sync/policy.test.ts +659 -0
  189. package/src/sync/policy.ts +502 -0
  190. package/src/sync/poll.saga.test.ts +51 -0
  191. package/src/sync/poll.saga.ts +36 -0
  192. package/src/sync/tabs.saga.test.ts +74 -0
  193. package/src/sync/tabs.saga.ts +93 -0
  194. package/src/testing/adapters.ts +133 -0
  195. package/src/testing/build.ts +61 -0
  196. package/src/testing/fake-services.ts +34 -0
  197. package/src/testing/run-pure.test.ts +92 -0
  198. package/src/testing/run-pure.ts +128 -0
  199. package/src/testing/services.ts +17 -0
  200. package/src/types.ts +591 -14
  201. package/src/utils/blurhash.ts +90 -0
  202. package/src/utils/error-classification.test.ts +54 -0
  203. package/src/utils/error-classification.ts +25 -0
  204. package/src/utils/index.ts +79 -13
  205. package/src/utils/parser.test.ts +49 -120
  206. package/src/utils/parser.ts +32 -2
  207. package/src/utils/semver.test.ts +32 -0
  208. package/src/utils/semver.ts +30 -0
  209. package/src/utils/surql.ts +30 -18
  210. package/src/utils/utils.test.ts +2 -1
  211. package/src/utils/withRetry.test.ts +1 -1
  212. package/tsdown.config.ts +86 -1
  213. package/vitest.config.ts +48 -0
  214. package/src/modules/cache/index.ts +0 -241
  215. package/src/modules/cache/types.ts +0 -19
  216. package/src/modules/data/data.test.ts +0 -58
  217. package/src/modules/data/index.ts +0 -864
  218. package/src/modules/sync/engine.ts +0 -166
  219. package/src/modules/sync/events/index.ts +0 -77
  220. package/src/modules/sync/index.ts +0 -3
  221. package/src/modules/sync/queue/index.ts +0 -2
  222. package/src/modules/sync/queue/queue-down.ts +0 -89
  223. package/src/modules/sync/queue/queue-up.ts +0 -223
  224. package/src/modules/sync/scheduler.ts +0 -84
  225. package/src/modules/sync/sync.ts +0 -421
  226. package/src/modules/sync/utils.test.ts +0 -311
  227. package/src/modules/sync/utils.ts +0 -167
  228. package/src/spooky.ts +0 -395
@@ -0,0 +1,159 @@
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
+ 'migrator.provision': () => Promise<void>;
79
+ 'blobs.start': (bucketId: string) => Promise<void>;
80
+ 'blobs.setNamespace': (bucketId: string) => Promise<void>;
81
+ 'blobs.clear': () => Promise<void>;
82
+ 'ssp.init': () => Promise<void>;
83
+ 'ssp.setPermissions': () => void;
84
+ 'ssp.setSessionAuth': (authId: string | null, access: string | null) => void;
85
+ 'ssp.prime': (pendingIds: string[]) => Promise<void>;
86
+ 'ssp.reset': () => Promise<void>;
87
+ 'ssp.setPersistence': (enabled: boolean) => void;
88
+ 'auth.restoreSession': () => Promise<string | null>;
89
+ 'auth.init': () => Promise<void>;
90
+ 'auth.sessionAuthId': () => string | null;
91
+ 'auth.access': () => string | null;
92
+ 'auth.token': () => string | null;
93
+ 'auth.currentUser': () => Record<string, unknown> | null;
94
+ 'remote.connect': () => Promise<void>;
95
+ 'remote.releaseViews': (ids: unknown[]) => void;
96
+ 'supervisor.start': () => void;
97
+ 'tabs.start': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
98
+ 'tabs.moveToBucket': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
99
+ 'crdt.setSessionId': (sessionId: string) => void;
100
+ 'crdt.closeAll': (flush: boolean) => void;
101
+ 'persistence.set': (key: string, value: unknown) => Promise<void>;
102
+ 'features.init': () => void;
103
+ 'releases.init': () => void;
104
+ 'window.attach': () => void;
105
+ }
106
+ export type ServiceName = keyof ServiceCalls;
107
+
108
+ /** Typed constructors. Each is one line so the saga reads like the plan. */
109
+ export const fx = {
110
+ local: {
111
+ query: (sql: string, vars?: Vars, epoch?: number): Effect => ({ kind: 'local.query', sql, vars, epoch }),
112
+ select: (plan: QueryPlan, params?: Vars): Effect => ({ kind: 'local.select', plan, params }),
113
+ execute: (query: SealedQuery<unknown>, vars?: Vars, epoch?: number): Effect => ({
114
+ kind: 'local.execute',
115
+ query,
116
+ vars,
117
+ epoch,
118
+ }),
119
+ getById: (table: string, id: unknown): Effect => ({ kind: 'local.getById', table, id }),
120
+ upsert: (table: string, id: unknown, data: Record<string, unknown>, mode: 'replace' | 'merge'): Effect => ({
121
+ kind: 'local.upsert',
122
+ table,
123
+ id,
124
+ data,
125
+ mode,
126
+ }),
127
+ delete: (table: string, id: unknown): Effect => ({ kind: 'local.delete', table, id }),
128
+ /** The store's current epoch; a write fenced with it is dropped after a bucket switch or transport failover. */
129
+ epoch: (): Effect => ({ kind: 'local.epoch' }),
130
+ },
131
+ remote: {
132
+ query: (sql: string, vars?: Vars, timeoutMs?: number): Effect => ({ kind: 'remote.query', sql, vars, timeoutMs }),
133
+ /** Subscribe to `LIVE SELECT * FROM <table>`; resolves to the live uuid. */
134
+ live: (table: string): Effect => ({ kind: 'remote.live', table }),
135
+ kill: (uuid: string): Effect => ({ kind: 'remote.kill', uuid }),
136
+ },
137
+ ssp: {
138
+ register: (plan: RegisterPlan): Effect => ({ kind: 'ssp.register', plan }),
139
+ unregister: (hash: string): Effect => ({ kind: 'ssp.unregister', hash }),
140
+ ingest: (records: IngestRecord[]): Effect => ({ kind: 'ssp.ingest', records }),
141
+ },
142
+ timer: {
143
+ set: (key: string, ms: number, event: RuntimeEvent): Effect => ({ kind: 'timer.set', key, ms, event }),
144
+ clear: (key: string): Effect => ({ kind: 'timer.clear', key }),
145
+ },
146
+ state: {
147
+ read: <T>(select: (s: ClientState) => T): Effect => ({ kind: 'state.read', select }),
148
+ update: (fn: (s: ClientState) => ClientState): Effect => ({ kind: 'state.update', fn }),
149
+ /** Suspend until `until(state)` holds (checked after every state.update). */
150
+ wait: (until: (s: ClientState) => boolean): Effect => ({ kind: 'state.wait', until }),
151
+ },
152
+ now: (): Effect => ({ kind: 'now' }),
153
+ id: (scope: 'mutation' | 'salt'): Effect => ({ kind: 'id', scope }),
154
+ hash: (input: string): Effect => ({ kind: 'hash', input }),
155
+ emit: (event: OutEvent): Effect => ({ kind: 'emit', event }),
156
+ dispatch: (event: RuntimeEvent): Effect => ({ kind: 'dispatch', event }),
157
+ all: (effects: Effect[]): Effect => ({ kind: 'all', effects }),
158
+ service: <N extends ServiceName>(name: N, ...args: Parameters<ServiceCalls[N]>): Effect => ({ kind: 'service', name, args }),
159
+ } as const;
@@ -0,0 +1,55 @@
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: 'ReadMembership'; hashes: QueryHash[] }
16
+ | { type: 'FetchRows' }
17
+ | { type: 'Materialize'; hash: QueryHash }
18
+ | { type: 'MaterializeDirty' }
19
+ | { type: 'LifecycleTick' }
20
+ | { type: 'GcTick' }
21
+ | { type: 'Drain' }
22
+ | { type: 'FlushWrite'; key: string }
23
+ | { type: 'PollTick' }
24
+ | { type: 'SelfHealTick' }
25
+ | { type: 'HeartbeatNow' }
26
+ | { type: 'StartRemote' }
27
+ | { type: 'PrimeCircuit' }
28
+ | { type: 'VersionsPrimed'; entries: Array<readonly [string, number]> }
29
+ | { type: 'WarmBlobs' }
30
+ | { type: 'AuthFlip'; userId: string | null }
31
+ | { type: 'BucketSwitch'; target: string }
32
+ | { type: 'PageHide' }
33
+ | { type: 'ConnectionChanged'; state: ConnectionState }
34
+ | { type: 'StreamUpdate'; update: StreamUpdate }
35
+ | { type: 'LiveStart' }
36
+ | { type: 'LiveChange'; hashes: QueryHash[]; rows?: InlineRow[] }
37
+ | { type: 'TabRole'; role: 'solo' | 'leader' | 'follower' }
38
+ | { type: 'TabMessage'; message: unknown };
39
+
40
+ export type OutEvent =
41
+ | { type: 'query:records'; hash: QueryHash; records: ReadonlyArray<Record<string, unknown>> }
42
+ | { type: 'query:status'; hash: QueryHash; status: QueryStatus }
43
+ | { type: 'query:authority'; hash: QueryHash; known: boolean }
44
+ | { type: 'query:view-lost'; hash: QueryHash }
45
+ | { type: 'query:evicted'; hash: QueryHash }
46
+ | { type: 'mutation:event'; event: unknown }
47
+ | { type: 'mutation:settled'; mutationId: string; recordId: string; eventType: string }
48
+ | { type: 'mutation:rolled-back'; mutationId: string; recordId: string; eventType: string; error: string }
49
+ | { type: 'tray:changed'; count: number }
50
+ | { type: 'health:changed'; health: SyncHealth }
51
+ | { type: 'activity:changed'; fetching: number; pending: number }
52
+ | { type: 'tabs:broadcast'; message: unknown }
53
+ | { type: 'tabs:sendTo'; tabId: string; message: unknown }
54
+ | { type: 'devtools'; name: string; data?: unknown }
55
+ | { 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
+ });
@@ -0,0 +1,73 @@
1
+ import type { Effect } from './effects';
2
+
3
+ /**
4
+ * A saga is a generator that yields effects and receives their results. It
5
+ * holds no references to adapters or state, so it can be driven by the real
6
+ * interpreter or by `testing/run-pure` with canned results.
7
+ */
8
+ export type Saga<R> = Generator<Effect, R, any>;
9
+ export type Interpret = (effect: Effect) => Promise<unknown>;
10
+
11
+ /**
12
+ * Drive a saga to completion. A rejected effect is thrown back into the saga
13
+ * at the `yield`, so sagas use ordinary `try/catch`; an error the saga does
14
+ * not catch propagates to the caller.
15
+ */
16
+ export async function runSaga<R>(saga: Saga<R>, interpret: Interpret): Promise<R> {
17
+ let step = saga.next();
18
+ while (!step.done) {
19
+ let result: unknown;
20
+ try {
21
+ result = await interpret(step.value);
22
+ } catch (error) {
23
+ step = saga.throw(error);
24
+ continue;
25
+ }
26
+ step = saga.next(result);
27
+ }
28
+ return step.value;
29
+ }
30
+
31
+ /**
32
+ * Lanes bound concurrency without a queue object per lane:
33
+ * - `serial`: runs strictly one at a time per key, in arrival order.
34
+ * - `dedupe`: a request while one run is active joins that run instead of
35
+ * starting another.
36
+ * The bookkeeping is a pure value so the policy is unit-testable; the runtime
37
+ * owns the promises.
38
+ */
39
+ export type Lane = { kind: 'serial'; key: string } | { kind: 'dedupe'; key: string };
40
+
41
+ export interface LaneState {
42
+ readonly running: ReadonlySet<string>;
43
+ readonly waiting: ReadonlyMap<string, number>;
44
+ }
45
+
46
+ export const emptyLanes = (): LaneState => ({ running: new Set(), waiting: new Map() });
47
+
48
+ export type LaneDecision = 'start' | 'wait' | 'join';
49
+
50
+ export function acquire(state: LaneState, lane: Lane): { decision: LaneDecision; state: LaneState } {
51
+ if (!state.running.has(lane.key)) {
52
+ const running = new Set(state.running);
53
+ running.add(lane.key);
54
+ return { decision: 'start', state: { running, waiting: state.waiting } };
55
+ }
56
+ if (lane.kind === 'dedupe') return { decision: 'join', state };
57
+ const waiting = new Map(state.waiting);
58
+ waiting.set(lane.key, (waiting.get(lane.key) ?? 0) + 1);
59
+ return { decision: 'wait', state: { running: state.running, waiting } };
60
+ }
61
+
62
+ export function release(state: LaneState, key: string): { startNext: boolean; state: LaneState } {
63
+ const pending = state.waiting.get(key) ?? 0;
64
+ if (pending > 0) {
65
+ const waiting = new Map(state.waiting);
66
+ if (pending === 1) waiting.delete(key);
67
+ else waiting.set(key, pending - 1);
68
+ return { startNext: true, state: { running: state.running, waiting } };
69
+ }
70
+ const running = new Set(state.running);
71
+ running.delete(key);
72
+ return { startNext: false, state: { running, waiting: state.waiting } };
73
+ }