@spooky-sync/core 0.0.1-canary.98 → 1.0.0-beta.10

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 (279) hide show
  1. package/AGENTS.md +58 -21
  2. package/README.md +59 -16
  3. package/dist/index.d.ts +2235 -908
  4. package/dist/index.js +20424 -5052
  5. package/dist/live.d.ts +149 -0
  6. package/dist/live.js +739 -0
  7. package/dist/otel/index.d.ts +1 -1
  8. package/dist/pure.d.ts +401 -0
  9. package/dist/pure.js +24 -0
  10. package/dist/rows-worker.d.ts +1 -0
  11. package/dist/rows-worker.js +2639 -0
  12. package/dist/sqlite-open.js +392 -0
  13. package/dist/sqlite-worker.d.ts +1 -0
  14. package/dist/sqlite-worker.js +439 -0
  15. package/dist/sw.d.ts +207 -0
  16. package/dist/sw.js +393 -0
  17. package/dist/tabs-broker-worker.d.ts +8 -0
  18. package/dist/tabs-broker-worker.js +477 -0
  19. package/dist/types.d.ts +2084 -82
  20. package/dist/worker-edges.js +498 -0
  21. package/dist/worker-impersonation.js +60 -0
  22. package/dist/worker-ref-tables.d.ts +48 -0
  23. package/dist/worker-types.d.ts +112 -0
  24. package/dist/worker-types.js +73 -0
  25. package/package.json +39 -8
  26. package/scripts/check-broker-bundle.mjs +33 -0
  27. package/scripts/check-worker-entries.mjs +186 -0
  28. package/skills/sp00ky-core/SKILL.md +44 -8
  29. package/skills/sp00ky-core/references/config.md +8 -2
  30. package/src/boot/auth-flip.saga.test.ts +174 -0
  31. package/src/boot/auth-flip.saga.ts +77 -0
  32. package/src/boot/boot.saga.test.ts +203 -0
  33. package/src/boot/boot.saga.ts +146 -0
  34. package/src/boot/bucket-switch.saga.test.ts +191 -0
  35. package/src/boot/bucket-switch.saga.ts +151 -0
  36. package/src/boot/preload.saga.test.ts +96 -0
  37. package/src/boot/preload.saga.ts +45 -0
  38. package/src/bucket-blurhash.test.ts +148 -0
  39. package/src/client/bucket-handle.test.ts +195 -0
  40. package/src/client/bucket-handle.ts +372 -0
  41. package/src/client/eager-paint.test.ts +82 -0
  42. package/src/client/router.test.ts +59 -0
  43. package/src/client/router.ts +104 -0
  44. package/src/client/runtime.test.ts +319 -0
  45. package/src/client/runtime.ts +307 -0
  46. package/src/client/services.test.ts +74 -0
  47. package/src/client/services.ts +440 -0
  48. package/src/client/sp00ky-client.test.ts +570 -0
  49. package/src/client/sp00ky-client.ts +875 -0
  50. package/src/index.ts +87 -0
  51. package/src/kernel/constants.test.ts +19 -0
  52. package/src/kernel/constants.ts +94 -0
  53. package/src/kernel/effects.test.ts +40 -0
  54. package/src/kernel/effects.ts +181 -0
  55. package/src/kernel/events.ts +60 -0
  56. package/src/kernel/interpreter.test.ts +133 -0
  57. package/src/kernel/interpreter.ts +151 -0
  58. package/src/kernel/saga.test.ts +89 -0
  59. package/src/kernel/saga.ts +73 -0
  60. package/src/live/index.test.ts +512 -0
  61. package/src/live/index.ts +1059 -0
  62. package/src/modules/app-release/index.test.ts +125 -0
  63. package/src/modules/app-release/index.ts +209 -0
  64. package/src/modules/auth/auth.local-first.test.ts +228 -0
  65. package/src/modules/auth/auth.session-recheck.test.ts +140 -0
  66. package/src/modules/auth/events/index.ts +7 -1
  67. package/src/modules/auth/impersonation-banner.ts +443 -0
  68. package/src/modules/auth/impersonation.integration.test.ts +209 -0
  69. package/src/modules/auth/impersonation.test.ts +377 -0
  70. package/src/modules/auth/impersonation.ts +179 -0
  71. package/src/modules/auth/index.ts +516 -34
  72. package/src/modules/channel/channel.test.ts +752 -0
  73. package/src/modules/channel/hub.test.ts +223 -0
  74. package/src/modules/channel/hub.ts +355 -0
  75. package/src/modules/channel/index.ts +1497 -0
  76. package/src/modules/channel/room.test.ts +340 -0
  77. package/src/modules/channel/room.ts +367 -0
  78. package/src/modules/channel/rooms.fixture.ts +383 -0
  79. package/src/modules/channel/share.test.ts +535 -0
  80. package/src/modules/channel/webrtc.ts +472 -0
  81. package/src/modules/crdt/acceptance.ts +75 -0
  82. package/src/modules/crdt/bytes.ts +50 -0
  83. package/src/modules/crdt/crdt-field.ts +82 -238
  84. package/src/modules/crdt/crdt.test.ts +546 -0
  85. package/src/modules/crdt/hash.ts +103 -0
  86. package/src/modules/crdt/index.ts +740 -271
  87. package/src/modules/crdt/loro-loader.ts +25 -0
  88. package/src/modules/crdt/mirror.ts +37 -0
  89. package/src/modules/devtools/flags.ts +349 -0
  90. package/src/modules/devtools/index.ts +661 -99
  91. package/src/modules/devtools/mutations.test.ts +92 -0
  92. package/src/modules/devtools/mutations.ts +160 -0
  93. package/src/modules/devtools/notify-throttle.test.ts +166 -0
  94. package/src/modules/devtools/state-shape.test.ts +263 -0
  95. package/src/modules/devtools/storage-info.test.ts +79 -0
  96. package/src/modules/devtools/storage-info.ts +169 -0
  97. package/src/modules/devtools/versions.ts +29 -0
  98. package/src/modules/feature-flag/index.test.ts +179 -1
  99. package/src/modules/feature-flag/index.ts +117 -10
  100. package/src/modules/ref-tables.test.ts +28 -1
  101. package/src/modules/ref-tables.ts +23 -1
  102. package/src/modules/web-push/index.test.ts +824 -0
  103. package/src/modules/web-push/index.ts +1027 -0
  104. package/src/mutation/ingest-outage.integration.test.ts +400 -0
  105. package/src/mutation/jobs.test.ts +25 -0
  106. package/src/mutation/jobs.ts +41 -0
  107. package/src/mutation/mutation-id.test.ts +37 -0
  108. package/src/mutation/mutation-id.ts +35 -0
  109. package/src/mutation/pending-writes.integration.test.ts +175 -0
  110. package/src/mutation/push.saga.test.ts +361 -0
  111. package/src/mutation/push.saga.ts +245 -0
  112. package/src/mutation/rows.test.ts +278 -0
  113. package/src/mutation/rows.ts +515 -0
  114. package/src/mutation/tray.saga.test.ts +63 -0
  115. package/src/mutation/tray.saga.ts +48 -0
  116. package/src/mutation/write.saga.test.ts +436 -0
  117. package/src/mutation/write.saga.ts +384 -0
  118. package/src/pure.ts +66 -0
  119. package/src/push/types.ts +174 -0
  120. package/src/query/anon-query-isolation.integration.test.ts +531 -0
  121. package/src/query/env.test.ts +23 -0
  122. package/src/query/env.ts +65 -0
  123. package/src/query/fetch.saga.test.ts +199 -0
  124. package/src/query/fetch.saga.ts +120 -0
  125. package/src/query/hash.test.ts +14 -0
  126. package/src/query/hash.ts +18 -0
  127. package/src/query/id-set-plan.test.ts +132 -0
  128. package/src/query/lifecycle.saga.test.ts +274 -0
  129. package/src/query/lifecycle.saga.ts +186 -0
  130. package/src/query/list-ref-live.integration.test.ts +203 -0
  131. package/src/query/materialize.saga.test.ts +216 -0
  132. package/src/query/materialize.saga.ts +83 -0
  133. package/src/query/materialize.test.ts +58 -0
  134. package/src/query/materialize.ts +133 -0
  135. package/src/query/membership.saga.test.ts +514 -0
  136. package/src/query/membership.saga.ts +339 -0
  137. package/src/query/membership.test.ts +151 -0
  138. package/src/query/membership.ts +234 -0
  139. package/src/query/register.saga.test.ts +430 -0
  140. package/src/query/register.saga.ts +330 -0
  141. package/src/query/render-set.test.ts +47 -0
  142. package/src/query/render-set.ts +71 -0
  143. package/src/query/session-guard.integration.test.ts +304 -0
  144. package/src/query/sql.test.ts +118 -0
  145. package/src/query/sql.ts +174 -0
  146. package/src/{modules/data → query}/window-query.test.ts +22 -4
  147. package/src/{modules/data → query}/window-query.ts +86 -8
  148. package/src/services/blobs/blob-cache.test.ts +359 -0
  149. package/src/services/blobs/blob-cache.ts +603 -0
  150. package/src/services/blobs/blob-manifest.ts +227 -0
  151. package/src/services/blobs/blob-store.test.ts +77 -0
  152. package/src/services/blobs/blob-store.ts +359 -0
  153. package/src/services/blobs/blob.fixture.ts +90 -0
  154. package/src/services/blobs/index.ts +70 -0
  155. package/src/services/database/cache-engine.ts +223 -0
  156. package/src/services/database/connection-supervisor.test.ts +318 -0
  157. package/src/services/database/connection-supervisor.ts +422 -0
  158. package/src/services/database/database.query-timeout.test.ts +140 -0
  159. package/src/services/database/database.responses.test.ts +43 -0
  160. package/src/services/database/database.ts +151 -56
  161. package/src/services/database/engine-factory.ts +40 -0
  162. package/src/services/database/errors.ts +34 -0
  163. package/src/services/database/index.ts +8 -0
  164. package/src/services/database/landed-body-clear.integration.test.ts +236 -0
  165. package/src/services/database/local-migrator.integration.test.ts +110 -0
  166. package/src/services/database/local-migrator.ts +54 -9
  167. package/src/services/database/local.test.ts +117 -0
  168. package/src/services/database/local.ts +364 -74
  169. package/src/services/database/plan-render.test.ts +159 -0
  170. package/src/services/database/plan-render.ts +108 -0
  171. package/src/services/database/relation-resolver.test.ts +484 -0
  172. package/src/services/database/relation-resolver.ts +0 -0
  173. package/src/services/database/remote.ts +283 -4
  174. package/src/services/database/rows-cache-engine.test.ts +1014 -0
  175. package/src/services/database/rows-cache-engine.ts +921 -0
  176. package/src/services/database/rows-exec.integration.test.ts +589 -0
  177. package/src/services/database/rows-exec.ts +1027 -0
  178. package/src/services/database/rows-prewarm.ts +225 -0
  179. package/src/services/database/rows-store.test.ts +635 -0
  180. package/src/services/database/rows-store.ts +1208 -0
  181. package/src/services/database/rows-transport.fixture.ts +151 -0
  182. package/src/services/database/rows-worker.ts +588 -0
  183. package/src/services/database/sqlite-cache-engine.test.ts +649 -0
  184. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  185. package/src/services/database/sqlite-cache-engine.ts +1422 -0
  186. package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
  187. package/src/services/database/sqlite-column-reset.ts +91 -0
  188. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  189. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  190. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  191. package/src/services/database/sqlite-lock-verify.ts +45 -0
  192. package/src/services/database/sqlite-open.test.ts +150 -0
  193. package/src/services/database/sqlite-open.ts +164 -0
  194. package/src/services/database/sqlite-plan-sql.test.ts +164 -0
  195. package/src/services/database/sqlite-plan-sql.ts +196 -0
  196. package/src/services/database/sqlite-projection.test.ts +121 -0
  197. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  198. package/src/services/database/sqlite-select.test.ts +261 -0
  199. package/src/services/database/sqlite-select.ts +131 -0
  200. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  201. package/src/services/database/sqlite-transport.ts +231 -0
  202. package/src/services/database/sqlite-worker.ts +437 -0
  203. package/src/services/database/surql-translate.ts +417 -0
  204. package/src/services/database/surreal-cache-engine.ts +163 -0
  205. package/src/services/logger/index.test.ts +132 -0
  206. package/src/services/logger/index.ts +187 -6
  207. package/src/services/persistence/surrealdb.ts +2 -2
  208. package/src/services/stream-processor/index.ts +621 -93
  209. package/src/services/stream-processor/stream-processor.batch.test.ts +57 -7
  210. package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
  211. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  212. package/src/services/stream-processor/wasm-types.ts +41 -1
  213. package/src/services/tabs/broker-client.ts +284 -0
  214. package/src/services/tabs/broker.test.ts +381 -0
  215. package/src/services/tabs/coordinator.test.ts +386 -0
  216. package/src/services/tabs/coordinator.ts +646 -0
  217. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  218. package/src/services/tabs/leader-locks.ts +75 -0
  219. package/src/services/tabs/protocol.ts +262 -0
  220. package/src/services/tabs/support.ts +38 -0
  221. package/src/services/tabs/tabs-broker-worker.ts +652 -0
  222. package/src/sp00ky.ts +5 -648
  223. package/src/state/client-state.ts +231 -0
  224. package/src/state/lifecycle.test.ts +184 -0
  225. package/src/state/lifecycle.ts +127 -0
  226. package/src/state/reducers.test.ts +337 -0
  227. package/src/state/reducers.ts +571 -0
  228. package/src/state/selectors.test.ts +203 -0
  229. package/src/state/selectors.ts +264 -0
  230. package/src/sw/index.test.ts +582 -0
  231. package/src/sw/index.ts +607 -0
  232. package/src/sw/store.ts +96 -0
  233. package/src/sync/connection.saga.test.ts +97 -0
  234. package/src/sync/connection.saga.ts +89 -0
  235. package/src/sync/live.saga.test.ts +229 -0
  236. package/src/sync/live.saga.ts +116 -0
  237. package/src/sync/policy.health.test.ts +56 -0
  238. package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +96 -11
  239. package/src/{modules/sync/utils.ts → sync/policy.ts} +186 -7
  240. package/src/sync/poll.saga.test.ts +51 -0
  241. package/src/sync/poll.saga.ts +36 -0
  242. package/src/sync/tabs.saga.test.ts +78 -0
  243. package/src/sync/tabs.saga.ts +96 -0
  244. package/src/testing/adapters.ts +137 -0
  245. package/src/testing/build.ts +63 -0
  246. package/src/testing/fake-services.ts +63 -0
  247. package/src/testing/run-pure.test.ts +92 -0
  248. package/src/testing/run-pure.ts +128 -0
  249. package/src/testing/services.ts +17 -0
  250. package/src/types.ts +601 -25
  251. package/src/utils/blurhash.ts +90 -0
  252. package/src/utils/edges.ts +57 -0
  253. package/src/utils/error-classification.test.ts +116 -0
  254. package/src/utils/error-classification.ts +74 -0
  255. package/src/utils/index.ts +58 -3
  256. package/src/utils/parser.test.ts +67 -102
  257. package/src/utils/parser.ts +62 -1
  258. package/src/utils/semver.test.ts +32 -0
  259. package/src/utils/semver.ts +30 -0
  260. package/src/utils/sha256.ts +15 -0
  261. package/src/utils/surql.ts +17 -15
  262. package/src/utils/utils.test.ts +3 -2
  263. package/tsdown.config.ts +105 -4
  264. package/vitest.config.ts +52 -0
  265. package/src/modules/cache/index.ts +0 -253
  266. package/src/modules/cache/types.ts +0 -19
  267. package/src/modules/crdt/crdt-hydration.test.ts +0 -206
  268. package/src/modules/data/data.status.test.ts +0 -108
  269. package/src/modules/data/data.test.ts +0 -58
  270. package/src/modules/data/index.ts +0 -1491
  271. package/src/modules/sync/engine.ts +0 -227
  272. package/src/modules/sync/events/index.ts +0 -84
  273. package/src/modules/sync/index.ts +0 -3
  274. package/src/modules/sync/queue/index.ts +0 -2
  275. package/src/modules/sync/queue/queue-down.ts +0 -90
  276. package/src/modules/sync/queue/queue-up.ts +0 -224
  277. package/src/modules/sync/scheduler.ts +0 -121
  278. package/src/modules/sync/sync.ts +0 -1244
  279. package/src/sp00ky.auth-order.test.ts +0 -65
@@ -1,352 +1,821 @@
1
1
  import type { SchemaStructure } from '@spooky-sync/query-builder';
2
- import type { LocalDatabaseService, RemoteDatabaseService } from '../../services/database/index';
2
+ import type { LoroDoc } from 'loro-crdt';
3
+ import { RecordId } from 'surrealdb';
4
+ import type { LocalStore, RemoteDatabaseService } from '../../services/database/index';
3
5
  import type { Logger } from '../../services/logger/index';
4
- import type { Uuid } from 'surrealdb';
5
- import { CrdtField } from './crdt-field';
6
6
  import { parseRecordIdString } from '../../utils/index';
7
+ import { check } from './acceptance';
8
+ import { asBytes, randomPeer } from './bytes';
9
+ import { CrdtField } from './crdt-field';
10
+ import { stateHash } from './hash';
11
+ import { loadLoro } from './loro-loader';
12
+ import type { CrdtKind } from './mirror';
7
13
 
8
14
  export { CrdtField, cursorColorFromName, CURSOR_COLORS } from './crdt-field';
9
15
 
10
16
  /**
11
- * CrdtManager manages active CrdtField instances and their sync channels.
17
+ * Collaborative (`@crdt`) fields.
12
18
  *
13
- * Collaborative state lives in two dedicated tables (defined in
14
- * `apps/cli/src/meta_tables_remote.surql`):
15
- * - `_00_crdt` { record_id, field, state } — one row per (record, field)
16
- * - `_00_cursor` { record_id, session_id, field, state } — one row per
17
- * (record, session, field)
19
+ * Each open field is a Loro document. The client never sends the document,
20
+ * only its own edits: every batch of local edits becomes one row of the
21
+ * generated `<table>__crdt_log` table, written through the ordinary outbox
22
+ * (so it survives offline, reloads and retries), and every client receives
23
+ * the other rows through an ordinary query and merges them. Nobody writes the
24
+ * canonical state but the SSP, which folds the log into `_00_crdt_doc` and the
25
+ * parent field's plain text (see `packages/ssp-node/src/crdt`).
18
26
  *
19
- * Splitting them off the parent row is what makes offline edits mergeable:
20
- * each (record, field) gets its own row, so concurrent offline writes don't
21
- * collide on the parent's last-write-wins semantics.
27
+ * A row is imported only if it passes the acceptance rule the fold applies
28
+ * (`./acceptance`), so this tab and the server always hold the same ops.
22
29
  *
23
- * Cross-browser delivery still rides the parent table's existing LIVE feed
24
- * to avoid SurrealDB v3 LIVE bugs around dereference-based permission rules
25
- * (issues 3602, 4026). On every meta UPSERT the writer also bumps the
26
- * parent's `_00_rv` (a no-op assignment); that fires the parent's LIVE
27
- * feed, and the receiver pulls the matching `_00_crdt` / `_00_cursor` rows
28
- * via subquery. Permission inheritance happens server-side via
29
- * `record_id.id != NONE` (SELECT) and `fn::can_update_record` (UPDATE).
30
+ * Edits carry this tab's Loro peer id, claimed for the signed-in user in
31
+ * `_00_crdt_peer` before the first row is sent; the database refuses a row
32
+ * whose peer its author does not own. A tab picks a fresh peer per session.
33
+ * Offline, edits stay local (and in the local snapshot) until the claim and
34
+ * the outbox can go through.
30
35
  */
36
+
37
+ /** A followed query: stop following it, and know when the server answered. */
38
+ export interface Watch {
39
+ stop: () => void;
40
+ /** Resolves once the server's membership for the query is known and its
41
+ * rows have landed locally (never rejects; may never resolve offline). */
42
+ settled: Promise<void>;
43
+ }
44
+
45
+ /** The runtime pieces the manager needs from the client facade. */
46
+ export interface CrdtPorts {
47
+ /** Register a query on `table` filtered by `where` and follow its rows. */
48
+ watch(table: string, where: Record<string, unknown>, onRows: (rows: Record<string, unknown>[]) => void): Promise<Watch>;
49
+ /** Optimistic create through the outbox. */
50
+ create(id: string, data: Record<string, unknown>): Promise<unknown>;
51
+ }
52
+
53
+ /** Local-only state of one field: the merged document and own unsent edits. */
54
+ interface LocalState {
55
+ snapshot?: Uint8Array;
56
+ /** Server fold this snapshot includes (for the canonical refetch). */
57
+ rev: number;
58
+ /** Every peer this device edited the field under, and how far its ops
59
+ * are already in the outbox. */
60
+ peers: Record<string, { enqueued: number }>;
61
+ }
62
+
63
+ interface Session {
64
+ key: string;
65
+ table: string;
66
+ recordId: string;
67
+ field: string;
68
+ kind: CrdtKind;
69
+ cursor: boolean;
70
+ doc: LoroDoc;
71
+ handle: CrdtField;
72
+ refs: number;
73
+ local: LocalState;
74
+ /** Log rows already looked at (imported, refused or own). */
75
+ seen: Set<string>;
76
+ /** Log rows in the latest delivery: when some vanish, the server folded. */
77
+ present: Set<string>;
78
+ /** Highest published rev this device's document was checked against. */
79
+ verifiedRev: number;
80
+ verifyTimer: ReturnType<typeof setTimeout> | null;
81
+ disposers: Array<() => void>;
82
+ flushTimer: ReturnType<typeof setTimeout> | null;
83
+ /** When the armed flush timer fires. */
84
+ flushDueAt: number;
85
+ /** When this session last sent edits (0: not yet). */
86
+ lastSentAt: number;
87
+ persistTimer: ReturnType<typeof setTimeout> | null;
88
+ flushing: Promise<void> | null;
89
+ /** A flush asked for while one was running: it runs once that one is done. */
90
+ flushQueued: Promise<void> | null;
91
+ refetching: boolean;
92
+ presenceTimer: ReturnType<typeof setTimeout> | null;
93
+ presencePending: Uint8Array | null;
94
+ /** This session wrote a presence row (removed again on close). */
95
+ presenceSent: boolean;
96
+ /** The latest import of delivered rows (chained, so awaiting it awaits all). */
97
+ ingesting: Promise<void>;
98
+ /** The server answered the published-document read (with a row or none). */
99
+ canonicalAnswered: boolean;
100
+ /** The log watch delivered the server's first answer. */
101
+ logSettled: boolean;
102
+ /** The second published-document read of a first open (see `recheckCanonical`). */
103
+ recheck: Promise<void> | null;
104
+ }
105
+
106
+ const LOCAL_TABLE = '_00_crdt_local';
107
+ const LOG_SUFFIX = '__crdt_log';
108
+ const PRESENCE_SUFFIX = '__crdt_presence';
109
+ const PERSIST_DEBOUNCE_MS = 400;
110
+ const PRESENCE_THROTTLE_MS = 250;
111
+ const CLAIM_RETRY_MS = 5000;
112
+ /** Longest an open or close waits on the server (the first answer for a
113
+ * field with nothing local, the last edits going out) before going on
114
+ * without it. Offline it does not wait at all. */
115
+ const SERVER_WAIT_MS = 4000;
116
+ /** Pause after a fold is noticed before checking the published document. */
117
+ const VERIFY_DELAY_MS = 1000;
118
+
31
119
  export class CrdtManager {
32
- private fields = new Map<string, CrdtField>();
33
- // One LIVE subscription per parent table (e.g. "thread" → uuid).
34
- private liveByTable = new Map<string, Uuid>();
35
- // Coalesces concurrent first-time subscribes for the same table.
36
- private pendingLive = new Map<string, Promise<void>>();
120
+ private sessions = new Map<string, Session>();
121
+ private opening = new Map<string, Promise<Session>>();
122
+ /** Closes still saving their field's local state, by session key. */
123
+ private saving = new Map<string, Promise<void>>();
124
+ private ports: CrdtPorts | null = null;
37
125
  private logger: Logger;
38
- // SurrealDB session id, used as the per-session key inside `_00_cursor`.
39
- private sessionId: string = '';
126
+ private peer: `${number}` = randomPeer();
127
+ /** The current peer's claim: settled once the database holds it for us. */
128
+ private claim: Promise<boolean> | null = null;
129
+ private claimRetry: ReturnType<typeof setTimeout> | null = null;
130
+ private connectionUnsubscribes: Array<() => void> = [];
40
131
 
41
132
  constructor(
42
133
  private schema: SchemaStructure,
43
- private local: LocalDatabaseService,
134
+ private local: LocalStore,
44
135
  private remote: RemoteDatabaseService,
45
136
  logger: Logger,
46
- private debounceMs: number = 500,
137
+ /** First edit after a quiet spell: wait this long to batch the burst. */
138
+ private debounceMs: number = 50,
139
+ /** Shortest gap between two sends of one field, while edits keep coming. */
140
+ private sendIntervalMs: number = 200
47
141
  ) {
48
142
  this.logger = logger.child({ service: 'CrdtManager' });
143
+ this.connectionUnsubscribes.push(
144
+ this.remote.subscribeConnection('connected', () => {
145
+ for (const s of this.sessions.values()) {
146
+ this.scheduleFlush(s, 0);
147
+ // Opened while the server could not answer: read it now.
148
+ if (!s.handle.isLoaded() && !s.canonicalAnswered) void this.refetchCanonical(s);
149
+ }
150
+ })
151
+ );
49
152
  }
50
153
 
51
- /** Set the session id that scopes this client's cursor entries. Must be
52
- * called before `open()` for cursors to be pushed under a stable key.
53
- * Passed in from `sp00ky.ts` at boot (it already fetches `session::id()`
54
- * for the data-module salt). */
55
- setSessionId(sessionId: string): void {
56
- this.sessionId = sessionId;
154
+ /** Hand over the runtime ports. Called once by the client facade. */
155
+ attach(ports: CrdtPorts): void {
156
+ this.ports = ports;
57
157
  }
58
158
 
59
159
  /**
60
- * Open a CRDT field for collaborative editing.
61
- *
62
- * @param table - Table name
63
- * @param recordId - Full record ID (e.g., "thread:abc")
64
- * @param field - Field name (e.g., "title", "content")
65
- * @param fallbackText - Current plain text from the record, used to seed the
66
- * LoroDoc if no CRDT state exists yet (migration path)
160
+ * A new signed-in identity (boot, sign-in, sign-out): edits from here on
161
+ * belong to it, under a peer it claims. Edits already made stay with the
162
+ * peer they were made under.
67
163
  */
68
- async open(
69
- table: string,
70
- recordId: string,
71
- field: string,
72
- fallbackText?: string,
73
- ): Promise<CrdtField> {
74
- this.assertCrdtField(table, field);
75
- const cursorsEnabled = this.fieldHasCursor(table, field);
76
- const key = this.makeKey(table, recordId, field);
77
- let crdtField = this.fields.get(key);
78
-
79
- if (crdtField) {
80
- return crdtField;
164
+ setSessionId(_salt: string): void {
165
+ this.peer = randomPeer();
166
+ this.claim = null;
167
+ for (const s of this.sessions.values()) s.doc.setPeerId(this.peer);
168
+ }
169
+
170
+ dispose(): void {
171
+ for (const off of this.connectionUnsubscribes.splice(0)) {
172
+ try {
173
+ off();
174
+ } catch {
175
+ /* ignore */
176
+ }
81
177
  }
178
+ if (this.claimRetry) clearTimeout(this.claimRetry);
179
+ }
82
180
 
83
- // Read the snapshot directly off the parent row. `@crdt`-only fields
84
- // hold the base64 snapshot inline; `@crdt @cursor` fields hold a
85
- // `{ state, cursors }` object so we drill into `.state`. The query
86
- // always selects the local row — sync-down already populated it
87
- // (the snapshot is a column on the parent, not a sidecar table) so
88
- // there is no separate fetch on the happy path.
89
- let initialCrdtState: Uint8Array | undefined;
90
- try {
91
- const [result] = await this.local.query<[unknown]>(
92
- `SELECT VALUE ${field} FROM ONLY $id`,
93
- { id: parseRecordIdString(recordId) },
94
- );
95
- const snapshot = this.extractSnapshot(result, cursorsEnabled);
96
- if (snapshot) initialCrdtState = snapshot;
97
- } catch (e) {
98
- this.logger.info(
99
- { error: String(e), recordId, field, Category: 'sp00ky-client::CrdtManager::open' },
100
- 'No existing CRDT state found in local cache (continuing with empty doc)'
101
- );
181
+ /**
182
+ * Open a collaborative field. Every open of the same field shares one
183
+ * document; close each open with {@link close}.
184
+ *
185
+ * `draft` (from {@link createDraft}) hands over edits made before the record
186
+ * existed: they are merged in and sent like this tab's own.
187
+ */
188
+ async open(table: string, recordId: string, field: string, draft?: CrdtField): Promise<CrdtField> {
189
+ const { kind, cursor } = this.fieldSpec(table, field);
190
+ const key = `${table}|${recordId}|${field}`;
191
+ let session = this.sessions.get(key);
192
+ if (session) {
193
+ session.refs++;
194
+ } else {
195
+ const inFlight = this.opening.get(key);
196
+ if (inFlight) {
197
+ session = await inFlight;
198
+ session.refs++;
199
+ } else {
200
+ const pending = this.start(key, table, recordId, field, kind, cursor, !!draft).finally(() => this.opening.delete(key));
201
+ this.opening.set(key, pending);
202
+ session = await pending;
203
+ }
102
204
  }
205
+ if (draft) this.adopt(session, draft.getDoc());
206
+ return session.handle;
207
+ }
103
208
 
104
- crdtField = new CrdtField(field, cursorsEnabled, initialCrdtState, this.logger);
105
- crdtField.startSync(this.local, this.remote, recordId, this.sessionId, this.debounceMs);
106
- this.fields.set(key, crdtField);
209
+ /**
210
+ * A detached document for a field of a record that does not exist yet (a
211
+ * "new thread" form). Nothing is sent; pass it to {@link open} once the
212
+ * record is created and its edits become the record's first ones.
213
+ */
214
+ async createDraft(table: string, field: string): Promise<CrdtField> {
215
+ const { kind, cursor } = this.fieldSpec(table, field);
216
+ const loro = await loadLoro();
217
+ return new CrdtField(table, '', field, kind, cursor, new loro.LoroDoc(), { pushPresence: () => {} });
218
+ }
107
219
 
108
- this.logger.info(
109
- { key, hasInitialState: !!initialCrdtState, hasFallback: !!fallbackText, Category: 'sp00ky-client::CrdtManager::open' },
110
- 'CrdtField opened'
111
- );
220
+ private adopt(s: Session, draft: LoroDoc): void {
221
+ const peers = [...draft.oplogVersion().toJSON().keys()];
222
+ if (peers.length === 0) return;
223
+ this.tryImport(s.doc, draft.export({ mode: 'update' }), s.key);
224
+ // The draft's ops carry its own peer: claim and send them under it.
225
+ for (const peer of peers) s.local.peers[String(peer)] ??= { enqueued: 0 };
226
+ this.schedulePersist(s);
227
+ this.scheduleFlush(s, 0);
228
+ }
112
229
 
113
- // Fire-and-forget: the LIVE subscription receives *future* updates;
114
- // the initial snapshot is already in hand. `ensureTableSubscription`
115
- // coalesces concurrent calls via `pendingLive`, so this is safe.
116
- void this.ensureTableSubscription(table);
117
-
118
- // Local was empty — a fresh device, a memory-backed local DB after
119
- // reload, or a record that hasn't been sync'd locally yet. Pull the
120
- // parent row from remote and dispatch its CRDT field; otherwise the
121
- // editor sits empty until the parent's LIVE feed happens to fire.
122
- if (!initialCrdtState) {
123
- void this.fetchAndDispatchRow(table, recordId);
124
- }
230
+ /** Close one open. The last close of a field resolves once its unsent
231
+ * edits are in the outbox, or once they are saved on the device when the
232
+ * server does not answer in time (offline: at once). */
233
+ close(table: string, recordId: string, field: string): Promise<void> {
234
+ const s = this.sessions.get(`${table}|${recordId}|${field}`);
235
+ if (!s || --s.refs > 0) return Promise.resolve();
236
+ return this.stop(s, true);
237
+ }
125
238
 
126
- return crdtField;
239
+ /** Close every field. `flush: false` (a bucket switch) drops unsent edits
240
+ * from this session's memory; they stay in the local snapshot of the
241
+ * bucket they belong to. */
242
+ closeAll(options: { flush?: boolean } = {}): void {
243
+ for (const s of [...this.sessions.values()]) void this.stop(s, options.flush ?? true);
127
244
  }
128
245
 
129
- close(table: string, recordId: string, field: string): void {
130
- const key = this.makeKey(table, recordId, field);
131
- const crdtField = this.fields.get(key);
132
- if (crdtField) {
133
- crdtField.stopSync();
134
- this.fields.delete(key);
135
- }
246
+ // ------------------------------------------------------------------
136
247
 
137
- // If no fields on this table remain open, tear down the table-wide LIVE.
138
- const tablePrefix = `${table}:`;
139
- const stillOpen = Array.from(this.fields.keys()).some((k) => k.startsWith(tablePrefix));
140
- if (!stillOpen) {
141
- this.killTableSubscription(table);
248
+ private fieldSpec(table: string, field: string): { kind: CrdtKind; cursor: boolean } {
249
+ const t = this.schema.tables.find((x) => x.name === table);
250
+ if (!t) throw new Error(`CrdtManager: unknown table '${table}'`);
251
+ const col = (t.columns as Record<string, any>)[field];
252
+ if (!col) throw new Error(`CrdtManager: unknown field '${table}.${field}'`);
253
+ if (!col.crdt) {
254
+ throw new Error(
255
+ `CrdtManager: '${table}.${field}' is not a CRDT field. Add \`-- @crdt text\` or \`-- @crdt prosemirror\` above its DEFINE FIELD and re-run \`spky generate\`.`
256
+ );
142
257
  }
258
+ const kind: CrdtKind = col.crdt === 'prosemirror' ? 'prosemirror' : 'text';
259
+ return { kind, cursor: !!col.cursor };
260
+ }
143
261
 
144
- this.logger.debug(
145
- { key, Category: 'sp00ky-client::CrdtManager::close' },
146
- 'CrdtField closed'
262
+ private async start(
263
+ key: string,
264
+ table: string,
265
+ recordId: string,
266
+ field: string,
267
+ kind: CrdtKind,
268
+ cursor: boolean,
269
+ /** Opened with a draft: the record was just created here, so the server
270
+ * has nothing for it yet and the open does not wait on it. */
271
+ fresh = false
272
+ ): Promise<Session> {
273
+ if (!this.ports) throw new Error('CrdtManager: not attached to a client');
274
+ const loro = await loadLoro();
275
+ const doc = new loro.LoroDoc();
276
+ doc.setPeerId(this.peer);
277
+
278
+ // A close of this field may still be saving: read what it saves.
279
+ await this.saving.get(key);
280
+ const local = await this.readLocal(recordId, field);
281
+ if (local.snapshot) this.tryImport(doc, local.snapshot, key);
282
+
283
+ const session: Session = {
284
+ key,
285
+ table,
286
+ recordId,
287
+ field,
288
+ kind,
289
+ cursor,
290
+ doc,
291
+ handle: undefined as unknown as CrdtField,
292
+ refs: 1,
293
+ local,
294
+ seen: new Set(),
295
+ present: new Set(),
296
+ verifiedRev: 0,
297
+ verifyTimer: null,
298
+ disposers: [],
299
+ flushTimer: null,
300
+ flushDueAt: 0,
301
+ lastSentAt: 0,
302
+ persistTimer: null,
303
+ flushing: null,
304
+ flushQueued: null,
305
+ refetching: false,
306
+ presenceTimer: null,
307
+ presencePending: null,
308
+ presenceSent: false,
309
+ ingesting: Promise.resolve(),
310
+ canonicalAnswered: false,
311
+ logSettled: false,
312
+ recheck: null,
313
+ };
314
+ session.handle = new CrdtField(table, recordId, field, kind, cursor, doc, {
315
+ pushPresence: (state) => this.pushPresence(session, state),
316
+ });
317
+ // Known from the start: this device's copy, or a record just created here.
318
+ if (local.snapshot || fresh) session.handle.markLoaded();
319
+ this.sessions.set(key, session);
320
+
321
+ // Claimed now, so the first edits do not wait on the round trip.
322
+ void this.ensureClaimed();
323
+ // First open on this device: start from the published document. Read at
324
+ // the same time as the log query below registers, not before it: each is
325
+ // a round trip, and the import order does not matter (ops whose
326
+ // predecessors are still missing wait inside Loro until they arrive).
327
+ const canonical = !local.snapshot && !fresh ? this.refetchCanonical(session) : null;
328
+
329
+ session.disposers.push(
330
+ doc.subscribeLocalUpdates(() => {
331
+ // Remember every peer that made edits here: a later identity change
332
+ // swaps the peer, and these ops must still go out under this one.
333
+ session.local.peers[this.peer] ??= { enqueued: 0 };
334
+ this.scheduleFlush(session);
335
+ this.schedulePersist(session);
336
+ })
147
337
  );
338
+ const ports = this.ports;
339
+ const logWatch = ports.watch(`${table}${LOG_SUFFIX}`, { doc: recordId, field }, (rows) => {
340
+ session.ingesting = session.ingesting.then(() => this.ingest(session, rows));
341
+ });
342
+ const presenceWatch = cursor
343
+ ? ports.watch(`${table}${PRESENCE_SUFFIX}`, { doc: recordId, field }, (rows) => this.ingestPresence(session, rows))
344
+ : null;
345
+ const [log, presence] = await Promise.all([logWatch, presenceWatch]);
346
+ session.disposers.push(log.stop);
347
+ if (presence) session.disposers.push(presence.stop);
348
+ void log.settled.then(async () => {
349
+ session.logSettled = true;
350
+ await session.ingesting;
351
+ await this.recheckCanonical(session, canonical);
352
+ this.checkLoaded(session);
353
+ });
354
+ // Offline the published document is read once the connection is back
355
+ // instead of holding the open.
356
+ if (canonical) await this.serverWait(canonical);
357
+ // An editor binding an empty document creates its root structure, and
358
+ // two clients doing that at once keep only one of them (with whatever was
359
+ // typed into the other). With nothing local, hand the document out only
360
+ // once the server's rows for it are in.
361
+ if (!session.handle.hasContent() && !fresh) await this.serverWait(log.settled);
362
+ await session.ingesting;
363
+ if (session.logSettled) await this.serverWait(this.recheckCanonical(session, canonical));
364
+ // Edits a previous session made but never sent (closed tab, offline).
365
+ this.scheduleFlush(session, 0);
366
+ this.logger.debug({ key, peer: this.peer, Category: 'sp00ky-client::CrdtManager::open' }, 'CRDT field opened');
367
+ return session;
148
368
  }
149
369
 
150
- closeAll(): void {
151
- for (const [_, field] of this.fields) {
152
- field.stopSync();
370
+ private async stop(s: Session, flush: boolean): Promise<void> {
371
+ this.sessions.delete(s.key);
372
+ for (const off of s.disposers.splice(0)) {
373
+ try {
374
+ off();
375
+ } catch {
376
+ /* ignore */
377
+ }
153
378
  }
154
- this.fields.clear();
155
- for (const table of Array.from(this.liveByTable.keys())) {
156
- this.killTableSubscription(table);
379
+ if (s.flushTimer) clearTimeout(s.flushTimer);
380
+ if (s.persistTimer) clearTimeout(s.persistTimer);
381
+ if (s.presenceTimer) clearTimeout(s.presenceTimer);
382
+ if (s.verifyTimer) clearTimeout(s.verifyTimer);
383
+ if (s.presenceSent) void this.dropPresence(s);
384
+ if (flush) {
385
+ const saved = this.persist(s);
386
+ this.saving.set(s.key, saved);
387
+ await saved;
388
+ if (this.saving.get(s.key) === saved) this.saving.delete(s.key);
389
+ await this.serverWait(this.flush(s));
157
390
  }
158
391
  }
159
392
 
160
- /** Ensure a single `LIVE SELECT * FROM <table>` is running, shared across
161
- * every open CrdtField on `table`. */
162
- private async ensureTableSubscription(table: string): Promise<void> {
163
- if (this.liveByTable.has(table)) return;
393
+ /**
394
+ * Wait for server-bound `work`, at most {@link SERVER_WAIT_MS}, and not at
395
+ * all while disconnected: a remote call made then queues behind the
396
+ * reconnect for up to the query timeout, and an offline "new thread" form
397
+ * must not hang on it. The work itself goes on either way.
398
+ */
399
+ private serverWait(work: Promise<unknown>): Promise<unknown> {
400
+ if (this.remote.getStatus() !== 'connected') return Promise.resolve();
401
+ return Promise.race([work, delay(SERVER_WAIT_MS)]);
402
+ }
164
403
 
165
- const pending = this.pendingLive.get(table);
166
- if (pending) return pending;
404
+ // --- receiving ---------------------------------------------------
167
405
 
168
- const start = (async () => {
169
- try {
170
- const [uuid] = await this.remote.query<[Uuid]>(
171
- `LIVE SELECT * FROM ${table}`,
172
- );
406
+ private async ingest(s: Session, rows: Record<string, unknown>[]): Promise<void> {
407
+ const loro = await loadLoro();
408
+ const ids = new Set(rows.map((row) => String(row.id ?? '')));
409
+ for (const id of s.present) {
410
+ if (!ids.has(id)) {
411
+ this.scheduleVerify(s);
412
+ break;
413
+ }
414
+ }
415
+ s.present = ids;
416
+ const blobs: Uint8Array[] = [];
417
+ for (const row of rows) {
418
+ const id = String(row.id ?? '');
419
+ if (!id || s.seen.has(id)) continue;
420
+ s.seen.add(id);
421
+ const blob = asBytes(row.data);
422
+ const verdict = check(loro, blob, row.peer == null ? undefined : String(row.peer));
423
+ if (!verdict.ok) {
424
+ this.logger.debug({ row: id, reason: verdict.reason, Category: 'sp00ky-client::CrdtManager::ingest' }, 'CRDT log row refused');
425
+ continue;
426
+ }
427
+ blobs.push(blob!);
428
+ }
429
+ if (blobs.length === 0) return;
430
+ let pending = false;
431
+ for (const blob of blobs) {
432
+ const status = this.tryImport(s.doc, blob, s.key);
433
+ if (status?.pending) pending = true;
434
+ }
435
+ this.checkLoaded(s);
436
+ this.schedulePersist(s);
437
+ // Ops depending on rows that were folded away before this device saw
438
+ // them: the published snapshot holds those predecessors.
439
+ if (pending) void this.refetchCanonical(s);
440
+ }
173
441
 
174
- const subscription = await this.remote.getClient().liveOf(uuid);
175
- subscription.subscribe((message) => {
176
- if (message.action === 'KILLED') return;
177
- if (message.action !== 'CREATE' && message.action !== 'UPDATE') return;
178
- this.dispatchRow(table, message.value as Record<string, unknown>);
179
- });
442
+ /** Mark the field loaded once its content arrived, or the server answered
443
+ * both reads (the published document and the log) with nothing. */
444
+ private checkLoaded(s: Session): void {
445
+ if (s.handle.hasContent() || (s.canonicalAnswered && s.logSettled)) s.handle.markLoaded();
446
+ }
180
447
 
181
- this.liveByTable.set(table, uuid);
182
- this.logger.info(
183
- { table, Category: 'sp00ky-client::CrdtManager::ensureTableSubscription' },
184
- 'LIVE SELECT started'
185
- );
186
- } catch (e) {
187
- this.logger.warn(
188
- { error: e, table, Category: 'sp00ky-client::CrdtManager::ensureTableSubscription' },
189
- 'Failed to start LIVE SELECT'
190
- );
448
+ private tryImport(doc: LoroDoc, blob: Uint8Array, key: string): { pending: boolean } | null {
449
+ try {
450
+ const status = doc.import(blob);
451
+ return { pending: !!status.pending && status.pending.size > 0 };
452
+ } catch (e) {
453
+ this.logger.warn({ key, error: String(e), Category: 'sp00ky-client::CrdtManager::import' }, 'CRDT import failed');
454
+ return null;
455
+ }
456
+ }
457
+
458
+ private async refetchCanonical(s: Session): Promise<void> {
459
+ if (s.refetching) return;
460
+ s.refetching = true;
461
+ try {
462
+ const [row] = await this.remote.query<[{ rev?: number; snapshot?: unknown } | null]>(
463
+ "SELECT rev, snapshot FROM ONLY type::record('_00_crdt_doc', [type::record($doc), $field])",
464
+ { doc: s.recordId, field: s.field }
465
+ );
466
+ const snapshot = asBytes(row?.snapshot);
467
+ if (snapshot) {
468
+ this.tryImport(s.doc, snapshot, s.key);
469
+ s.local.rev = Math.max(s.local.rev, row?.rev ?? 0);
470
+ this.schedulePersist(s);
191
471
  }
472
+ s.canonicalAnswered = true;
473
+ this.checkLoaded(s);
474
+ } catch (e) {
475
+ // Offline or no published document yet: the log rows still arrive.
476
+ this.logger.debug({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::refetch' }, 'No canonical CRDT document fetched');
477
+ } finally {
478
+ s.refetching = false;
479
+ }
480
+ }
481
+
482
+ /**
483
+ * A first open reads the published document and the log at the same time,
484
+ * and a fold landing between the two hides the field from both: it
485
+ * publishes its snapshot, then deletes the rows the log query was about to
486
+ * return, after the published-document read came back empty. Nothing seen
487
+ * means nothing to verify, so the editor stayed empty until the next edit
488
+ * (a second device opening a just-created thread, under e2e load). Once the
489
+ * log has answered, a field that is still empty reads the published
490
+ * document again: the fold that took its rows committed before deleting
491
+ * them. Once per open; `first` is the first read (null with a local copy or
492
+ * a draft, which need none).
493
+ */
494
+ private recheckCanonical(s: Session, first: Promise<void> | null): Promise<void> {
495
+ if (!first) return Promise.resolve();
496
+ s.recheck ??= (async () => {
497
+ await first;
498
+ await s.ingesting;
499
+ if (!s.handle.hasContent()) await this.refetchCanonical(s);
192
500
  })();
501
+ return s.recheck;
502
+ }
193
503
 
194
- this.pendingLive.set(table, start);
504
+ // --- checking against the published document ---------------------
505
+
506
+ private scheduleVerify(s: Session): void {
507
+ if (s.verifyTimer) clearTimeout(s.verifyTimer);
508
+ s.verifyTimer = setTimeout(() => {
509
+ s.verifyTimer = null;
510
+ void this.verify(s);
511
+ }, VERIFY_DELAY_MS);
512
+ }
513
+
514
+ /**
515
+ * After a fold: compare this device's document with the one the server
516
+ * published, at the version the server published. Missing ops (rows this
517
+ * device never got before they were folded away) are repaired by merging
518
+ * the published snapshot in. The same ops giving a different state cannot
519
+ * be merged away; that is logged.
520
+ */
521
+ private async verify(s: Session): Promise<void> {
522
+ type Published = {
523
+ rev?: number;
524
+ state_hash?: string;
525
+ upto?: { vv?: Record<string, number>; frontiers?: { peer: `${number}`; counter: number }[] };
526
+ };
527
+ let row: Published | null;
195
528
  try {
196
- await start;
529
+ [row] = await this.remote.query<[Published | null]>(
530
+ "SELECT rev, upto, state_hash FROM ONLY type::record('_00_crdt_doc', [type::record($doc), $field])",
531
+ { doc: s.recordId, field: s.field }
532
+ );
533
+ } catch {
534
+ return; // offline: the next fold checks again
535
+ }
536
+ const rev = row?.rev ?? 0;
537
+ const frontiers = row?.upto?.frontiers;
538
+ if (!row?.state_hash || !frontiers || !row.upto?.vv || rev <= s.verifiedRev || !this.sessions.has(s.key)) return;
539
+
540
+ const have = s.doc.oplogVersion().toJSON();
541
+ const behind = Object.entries(row.upto.vv).some(([peer, end]) => (have.get(peer as `${number}`) ?? 0) < end);
542
+ if (behind) {
543
+ this.logger.info({ key: s.key, rev, Category: 'sp00ky-client::CrdtManager::verify' }, 'CRDT document behind the published one; merging it in');
544
+ await this.refetchCanonical(s);
545
+ return;
546
+ }
547
+ s.verifiedRev = rev;
548
+ // WebCrypto is missing on insecure origins: nothing to check with there.
549
+ if (!globalThis.crypto?.subtle) return;
550
+ const loro = await loadLoro();
551
+ let fork: LoroDoc | undefined;
552
+ try {
553
+ fork = s.doc.forkAt(frontiers);
554
+ const hash = await stateHash(loro, fork);
555
+ if (hash !== row.state_hash) {
556
+ this.logger.warn(
557
+ { key: s.key, rev, hash, published: row.state_hash, Category: 'sp00ky-client::CrdtManager::verify' },
558
+ 'CRDT document differs from the published one at the same version'
559
+ );
560
+ }
561
+ } catch (e) {
562
+ this.logger.debug({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::verify' }, 'CRDT document check failed');
197
563
  } finally {
198
- this.pendingLive.delete(table);
564
+ fork?.free();
199
565
  }
200
566
  }
201
567
 
202
- /** Apply a parent-row payload from a non-LIVE source (e.g. the
203
- * list_ref-driven sync engine, when the cross-user LIVE on the
204
- * parent table is filtered out by the SurrealDB cross-session
205
- * permission gap). Same semantics as the internal `dispatchRow`. */
206
- applyRow(table: string, row: Record<string, unknown>): void {
207
- this.dispatchRow(table, row);
208
- }
209
-
210
- /** Dispatch a parent-row LIVE event to every open CrdtField on that
211
- * record. Each open field reads its slice of the row directly — the
212
- * CRDT snapshot is a column on the parent now, so there is no
213
- * follow-up subquery. */
214
- private dispatchRow(table: string, row: Record<string, unknown>): void {
215
- const id = row.id != null ? String(row.id) : '';
216
- if (!id) return;
217
-
218
- const rowKeyPrefix = `${table}:${id}:`;
219
- for (const [key, crdtField] of this.fields) {
220
- if (!key.startsWith(rowKeyPrefix)) continue;
221
- const fieldName = key.slice(rowKeyPrefix.length);
222
- const cursorsEnabled = this.fieldHasCursor(table, fieldName);
223
- const slice = row[fieldName];
224
- const snapshot = this.extractSnapshot(slice, cursorsEnabled);
225
- if (snapshot) crdtField.importRemote(snapshot);
226
-
227
- if (cursorsEnabled && slice && typeof slice === 'object') {
228
- const cursors = (slice as { cursors?: unknown }).cursors;
229
- if (cursors && typeof cursors === 'object') {
230
- for (const [sid, blob] of Object.entries(cursors as Record<string, unknown>)) {
231
- if (sid === this.sessionId) continue;
232
- if (typeof blob === 'string' && blob.length > 0) {
233
- crdtField.importRemoteCursor(blob);
234
- }
235
- }
236
- }
568
+ // --- sending -----------------------------------------------------
569
+
570
+ /**
571
+ * Arm the send. Edits go out on a steady cadence while they keep coming:
572
+ * the first one after a quiet spell waits `debounceMs` (one row for the
573
+ * burst), later ones at least `sendIntervalMs` after the previous send. An
574
+ * armed timer is never pushed back, so continuous typing cannot hold the
575
+ * edits; it is only brought forward (`delay`, e.g. 0 on reconnect).
576
+ */
577
+ private scheduleFlush(s: Session, delay?: number): void {
578
+ const now = Date.now();
579
+ const due = delay !== undefined ? now + delay : Math.max(now + this.debounceMs, s.lastSentAt + this.sendIntervalMs);
580
+ if (s.flushTimer) {
581
+ if (s.flushDueAt <= due) return;
582
+ clearTimeout(s.flushTimer);
583
+ }
584
+ s.flushDueAt = due;
585
+ s.flushTimer = setTimeout(() => {
586
+ s.flushTimer = null;
587
+ void this.flush(s);
588
+ }, due - now);
589
+ }
590
+
591
+ /** Send what is unsent. A call while a send runs queues one more after it,
592
+ * so edits made during that send are not left waiting for the next edit. */
593
+ private flush(s: Session): Promise<void> {
594
+ if (s.flushing) {
595
+ s.flushQueued ??= s.flushing.then(() => {
596
+ s.flushQueued = null;
597
+ return this.flush(s);
598
+ });
599
+ return s.flushQueued;
600
+ }
601
+ s.flushing = this.sendOwnEdits(s).finally(() => {
602
+ s.flushing = null;
603
+ });
604
+ return s.flushing;
605
+ }
606
+
607
+ /** Send every own peer's ops that are not in the outbox yet, one row per
608
+ * peer. Only the peer's own ops travel (`updates-in-range`): a plain
609
+ * `from: version` export would carry ops imported from others too, and
610
+ * the acceptance rule refuses such a row. */
611
+ private async sendOwnEdits(s: Session): Promise<void> {
612
+ const vv = s.doc.oplogVersion();
613
+ const own = new Set([...Object.keys(s.local.peers), this.peer]);
614
+ const due = [...own]
615
+ .map((peer) => ({ peer, end: vv.get(peer as `${number}`) ?? 0, from: s.local.peers[peer]?.enqueued ?? 0 }))
616
+ .filter((p) => p.end > p.from);
617
+ if (due.length === 0 || !this.ports) return;
618
+ s.lastSentAt = Date.now();
619
+ if (!(await this.ensureClaimed())) {
620
+ this.retryLater(s);
621
+ return;
622
+ }
623
+ for (const { peer, end, from } of due) {
624
+ if (peer !== this.peer && !(await this.claimPeer(peer as `${number}`))) continue;
625
+ const data = s.doc.export({
626
+ mode: 'updates-in-range',
627
+ spans: [{ id: { peer: peer as `${number}`, counter: from }, len: end - from }],
628
+ });
629
+ try {
630
+ await this.ports.create(`${s.table}${LOG_SUFFIX}:${rowKey()}`, {
631
+ doc: s.recordId,
632
+ field: s.field,
633
+ peer,
634
+ data,
635
+ });
636
+ } catch (e) {
637
+ this.logger.warn({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::flush' }, 'Queueing CRDT edits failed');
638
+ this.retryLater(s);
639
+ return;
237
640
  }
641
+ s.local.peers[peer] = { enqueued: end };
642
+ }
643
+ // The outbox marks reach the device with the next persist. An open field
644
+ // sends every few hundred ms while typing, and a persist exports the whole
645
+ // document, so it rides the persist debounce; a crash in between only
646
+ // re-sends rows, which import as no-ops. A closing field saves now.
647
+ if (this.sessions.get(s.key) === s) this.schedulePersist(s);
648
+ else await this.persist(s);
649
+ }
650
+
651
+ private retryLater(s: Session): void {
652
+ if (this.sessions.get(s.key) === s) this.scheduleFlush(s, CLAIM_RETRY_MS);
653
+ }
654
+
655
+ /** Claim this session's peer once; a failure (offline, signed out) is retried. */
656
+ private ensureClaimed(): Promise<boolean> {
657
+ if (!this.claim) {
658
+ const peer = this.peer;
659
+ this.claim = this.claimPeer(peer).then((ok) => {
660
+ if (!ok && this.peer === peer) this.claim = null;
661
+ return ok;
662
+ });
238
663
  }
664
+ return this.claim;
239
665
  }
240
666
 
241
- /** One-shot remote fetch for a row whose CRDT field hasn't synced
242
- * locally yet (fresh device, memory-backed local DB after reload, …).
243
- * Used by `open()` when the local read came up empty. Subsequent
244
- * cross-browser updates ride `dispatchRow` via the parent LIVE feed. */
245
- private async fetchAndDispatchRow(table: string, id: string): Promise<void> {
667
+ private async claimPeer(peer: `${number}`): Promise<boolean> {
246
668
  try {
247
- const recordId = parseRecordIdString(id);
248
- const [row] = await this.remote.query<[Record<string, unknown> | null]>(
249
- `SELECT * FROM ONLY $id`,
250
- { id: recordId },
251
- );
252
- if (!row || typeof row !== 'object') return;
253
- this.dispatchRow(table, row as Record<string, unknown>);
669
+ // A refused CREATE (signed out, impersonating) answers with no row.
670
+ const [created] = await this.remote.query<[unknown[]]>("CREATE type::record('_00_crdt_peer', $peer) RETURN id", { peer });
671
+ return Array.isArray(created) && created.length > 0;
254
672
  } catch (e) {
255
- this.logger.warn(
256
- { error: e, table, id, Category: 'sp00ky-client::CrdtManager::fetchAndDispatchRow' },
257
- 'Failed to fetch parent row for CRDT hydration'
258
- );
673
+ if (!/already exists/i.test(String(e))) return false;
674
+ // Ours from an earlier claim (only its owner can read it), or taken.
675
+ try {
676
+ const [owner] = await this.remote.query<[unknown]>(
677
+ "SELECT VALUE author FROM ONLY type::record('_00_crdt_peer', $peer)",
678
+ { peer }
679
+ );
680
+ if (owner != null) return true;
681
+ } catch {
682
+ return false;
683
+ }
684
+ this.logger.error({ peer, Category: 'sp00ky-client::CrdtManager::claim' }, 'CRDT peer id taken by another user');
685
+ if (peer === this.peer) this.setSessionId('');
686
+ return false;
259
687
  }
260
688
  }
261
689
 
262
- /** Schema lookup: does `<table>.<field>` carry a `@cursor` annotation?
263
- * Determines the on-disk shape (plain snapshot vs. `{ state, cursors }`). */
264
- private fieldHasCursor(table: string, field: string): boolean {
265
- const tableSchema = this.schema.tables.find((t) => t.name === table);
266
- return !!tableSchema?.columns[field]?.cursor;
267
- }
268
-
269
- /** Pull the LoroDoc snapshot bytes out of a row slice. For `@crdt`-only
270
- * the slice IS the snapshot (Uint8Array); for `@crdt @cursor` it's
271
- * `{ state, cursors }` where `state` carries the snapshot bytes. */
272
- private extractSnapshot(value: unknown, cursorsEnabled: boolean): Uint8Array | undefined {
273
- const asBytes = (v: unknown): Uint8Array | undefined => {
274
- if (v instanceof Uint8Array) return v.length > 0 ? v : undefined;
275
- // SurrealDB ferries bytes through several shapes depending on
276
- // transport and on whether the field is a top-level `bytes` column
277
- // or bytes nested inside a FLEXIBLE object. Round-tripping bytes
278
- // through `option<object> FLEXIBLE` (the `@crdt @cursor` shape) in
279
- // particular comes back as a plain `number[]` from the local WASM
280
- // DB and as `Uint8Array` from the remote WS engine. Normalize all
281
- // recognized variants here so the receiving CrdtField doesn't care.
282
- if (v instanceof ArrayBuffer) return new Uint8Array(v);
283
- if (ArrayBuffer.isView(v)) {
284
- const view = v as ArrayBufferView;
285
- return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
286
- }
287
- if (Array.isArray(v) && v.length > 0 && v.every((n) => typeof n === 'number')) {
288
- return Uint8Array.from(v as number[]);
289
- }
290
- return undefined;
291
- };
690
+ // --- local state -------------------------------------------------
691
+
692
+ private localId(recordId: string, field: string): RecordId {
693
+ return new RecordId(LOCAL_TABLE, `${recordId}|${field}`);
694
+ }
292
695
 
293
- if (cursorsEnabled) {
294
- if (value && typeof value === 'object' && !(value instanceof Uint8Array)) {
295
- return asBytes((value as { state?: unknown }).state);
696
+ private async readLocal(recordId: string, field: string): Promise<LocalState> {
697
+ try {
698
+ const row = await this.local.getById(LOCAL_TABLE, this.localId(recordId, field));
699
+ if (row) {
700
+ return {
701
+ snapshot: asBytes(row.snapshot),
702
+ rev: Number(row.rev ?? 0),
703
+ peers: (row.peers as LocalState['peers']) ?? {},
704
+ };
296
705
  }
297
- return undefined;
298
- }
299
- return asBytes(value);
300
- }
301
-
302
- private killTableSubscription(table: string): void {
303
- const uuid = this.liveByTable.get(table);
304
- if (uuid) {
305
- // We're tearing down: KILL on an already-dead/closed LIVE throws, and the
306
- // subscription may have ended on its own, so the failure is expected and
307
- // safe to drop.
308
- this.remote.query('KILL $uuid', { uuid }).catch((err) => {
309
- this.logger.debug(
310
- { err, table, Category: 'sp00ky-client::CrdtManager::killTableSubscription' },
311
- 'KILL of table LIVE failed (already closed?)'
312
- );
313
- });
314
- this.liveByTable.delete(table);
706
+ } catch (e) {
707
+ this.logger.debug({ error: String(e), Category: 'sp00ky-client::CrdtManager::readLocal' }, 'No local CRDT state');
315
708
  }
709
+ return { rev: 0, peers: {} };
316
710
  }
317
711
 
318
- private makeKey(table: string, recordId: string, field: string): string {
319
- return `${table}:${recordId}:${field}`;
712
+ private schedulePersist(s: Session): void {
713
+ if (s.persistTimer) return;
714
+ s.persistTimer = setTimeout(() => {
715
+ s.persistTimer = null;
716
+ void this.persist(s);
717
+ }, PERSIST_DEBOUNCE_MS);
320
718
  }
321
719
 
322
- /**
323
- * Throws if `<table>.<field>` is not annotated `@crdt` in the schema. Catches
324
- * typos, removed annotations, and stale schema codegen at the call site instead
325
- * of silently producing a non-CRDT writer.
326
- */
327
- private assertCrdtField(table: string, field: string): void {
328
- if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(field)) {
329
- throw new Error(
330
- `openCrdtField: refusing unsafe field identifier '${field}' — must match [a-zA-Z_][a-zA-Z0-9_]*`
720
+ /** Save the merged document. Another tab may have saved too: merge with
721
+ * its snapshot rather than overwrite it, and keep the further of the two
722
+ * outbox marks per peer. */
723
+ private async persist(s: Session): Promise<void> {
724
+ try {
725
+ const stored = await this.readLocal(s.recordId, s.field);
726
+ if (stored.snapshot) this.tryImport(s.doc, stored.snapshot, s.key);
727
+ for (const [peer, mark] of Object.entries(stored.peers)) {
728
+ const mine = s.local.peers[peer]?.enqueued ?? 0;
729
+ if (mark.enqueued > mine) s.local.peers[peer] = { enqueued: mark.enqueued };
730
+ }
731
+ s.local.rev = Math.max(s.local.rev, stored.rev);
732
+ s.local.snapshot = s.doc.export({ mode: 'snapshot' });
733
+ await this.local.upsert(
734
+ LOCAL_TABLE,
735
+ this.localId(s.recordId, s.field),
736
+ { doc: s.recordId, field: s.field, snapshot: s.local.snapshot, rev: s.local.rev, peers: s.local.peers },
737
+ 'replace'
331
738
  );
739
+ } catch (e) {
740
+ this.logger.debug({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::persist' }, 'Saving local CRDT state failed');
332
741
  }
333
- const tableSchema = this.schema.tables.find((t) => t.name === table);
334
- if (!tableSchema) {
335
- throw new Error(
336
- `openCrdtField: unknown table '${table}'. Available: ${this.schema.tables.map((t) => t.name).join(', ')}`
742
+ }
743
+
744
+ // --- presence ----------------------------------------------------
745
+
746
+ private pushPresence(s: Session, state: Uint8Array): void {
747
+ s.presencePending = state;
748
+ if (s.presenceTimer) return;
749
+ s.presenceTimer = setTimeout(() => {
750
+ s.presenceTimer = null;
751
+ const data = s.presencePending;
752
+ s.presencePending = null;
753
+ if (data) void this.sendPresence(s, data);
754
+ }, PRESENCE_THROTTLE_MS);
755
+ }
756
+
757
+ /** Presence is the latest state only: no outbox, no offline queue. */
758
+ private async sendPresence(s: Session, state: Uint8Array): Promise<void> {
759
+ if (!(await this.ensureClaimed())) return;
760
+ try {
761
+ await this.remote.query(
762
+ // One row per session and field, keyed by a plain identifier (an array
763
+ // key does not survive the client's record-id handling). `$session`
764
+ // is a protected SurrealDB variable: the session id binds as `$sid`.
765
+ `UPSERT type::record($table, $key) SET doc = type::record($doc), field = $field, session = $sid, state = $state RETURN NONE`,
766
+ {
767
+ table: `${s.table}${PRESENCE_SUFFIX}`,
768
+ key: presenceKey(this.peer, s.field, s.recordId),
769
+ doc: s.recordId,
770
+ field: s.field,
771
+ sid: this.peer,
772
+ state,
773
+ }
337
774
  );
775
+ s.presenceSent = true;
776
+ } catch (e) {
777
+ this.logger.debug({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::presence' }, 'Presence update failed');
338
778
  }
339
- const column = tableSchema.columns[field];
340
- if (!column) {
341
- throw new Error(
342
- `openCrdtField: '${table}.${field}' is not in the schema. Available fields: ${Object.keys(tableSchema.columns).join(', ')}`
343
- );
779
+ }
780
+
781
+ /** Remove this session's cursor when the field closes; the server's TTL
782
+ * sweep covers a tab that never gets here. */
783
+ private async dropPresence(s: Session): Promise<void> {
784
+ try {
785
+ await this.remote.query(`DELETE type::record($table, $key) RETURN NONE`, {
786
+ table: `${s.table}${PRESENCE_SUFFIX}`,
787
+ key: presenceKey(this.peer, s.field, s.recordId),
788
+ });
789
+ } catch (e) {
790
+ this.logger.debug({ key: s.key, error: String(e), Category: 'sp00ky-client::CrdtManager::presence' }, 'Presence removal failed');
344
791
  }
345
- if (!column.crdt) {
346
- throw new Error(
347
- `openCrdtField: '${table}.${field}' is not annotated '@crdt' in the schema. ` +
348
- `Add '-- @crdt text' above the field's DEFINE FIELD and regenerate the client schema.`
349
- );
792
+ }
793
+
794
+
795
+ private ingestPresence(s: Session, rows: Record<string, unknown>[]): void {
796
+ for (const row of rows) {
797
+ if (String(row.session ?? '') === this.peer) continue;
798
+ const state = asBytes(row.state);
799
+ if (state) s.handle.deliverPresence(state);
350
800
  }
351
801
  }
352
802
  }
803
+
804
+ /** `p<session>_<field>_<record key>`, letters, digits and `_` only. */
805
+ function presenceKey(session: string, field: string, recordId: string): string {
806
+ const key = recordId.slice(recordId.indexOf(':') + 1).replace(/[^A-Za-z0-9_]/g, '_');
807
+ return `p${session}_${field}_${key}`;
808
+ }
809
+
810
+ function delay(ms: number): Promise<void> {
811
+ return new Promise((resolve) => setTimeout(resolve, ms));
812
+ }
813
+
814
+ function rowKey(): string {
815
+ const bytes = new Uint8Array(12);
816
+ crypto.getRandomValues(bytes);
817
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
818
+ }
819
+
820
+ // Kept for callers that parse record ids next to CRDT fields.
821
+ export { parseRecordIdString };