@abloatai/ablo 0.36.0 → 0.37.1

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 (597) hide show
  1. package/AGENTS.md +2 -2
  2. package/CHANGELOG.md +55 -2014
  3. package/NOTICE +2 -2
  4. package/README.md +45 -63
  5. package/assets/banner.png +0 -0
  6. package/dist/auth.d.ts +2 -0
  7. package/dist/auth.d.ts.map +1 -0
  8. package/dist/auth.js +2 -0
  9. package/dist/auth.js.map +1 -0
  10. package/dist/client.d.ts +3 -0
  11. package/dist/client.d.ts.map +1 -0
  12. package/dist/client.js +3 -0
  13. package/dist/client.js.map +1 -0
  14. package/dist/coordination.d.ts +2 -0
  15. package/dist/coordination.d.ts.map +1 -0
  16. package/dist/coordination.js +2 -0
  17. package/dist/coordination.js.map +1 -0
  18. package/dist/index.d.ts +3 -114
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +3 -163
  21. package/dist/index.js.map +1 -0
  22. package/dist/react.d.ts +4 -0
  23. package/dist/react.d.ts.map +1 -0
  24. package/dist/react.js +3 -0
  25. package/dist/react.js.map +1 -0
  26. package/dist/schema.d.ts +2 -0
  27. package/dist/schema.d.ts.map +1 -0
  28. package/dist/schema.js +2 -0
  29. package/dist/schema.js.map +1 -0
  30. package/dist/server.d.ts +2 -0
  31. package/dist/server.d.ts.map +1 -0
  32. package/dist/server.js +2 -0
  33. package/dist/server.js.map +1 -0
  34. package/dist/source-conformance.d.ts +2 -0
  35. package/dist/source-conformance.d.ts.map +1 -0
  36. package/dist/source-conformance.js +2 -0
  37. package/dist/source-conformance.js.map +1 -0
  38. package/dist/source-drizzle.d.ts +2 -0
  39. package/dist/source-drizzle.d.ts.map +1 -0
  40. package/dist/source-drizzle.js +2 -0
  41. package/dist/source-drizzle.js.map +1 -0
  42. package/dist/source-kysely.d.ts +2 -0
  43. package/dist/source-kysely.d.ts.map +1 -0
  44. package/dist/source-kysely.js +2 -0
  45. package/dist/source-kysely.js.map +1 -0
  46. package/dist/source-next.d.ts +2 -0
  47. package/dist/source-next.d.ts.map +1 -0
  48. package/dist/source-next.js +2 -0
  49. package/dist/source-next.js.map +1 -0
  50. package/dist/source.d.ts +2 -0
  51. package/dist/source.d.ts.map +1 -0
  52. package/dist/source.js +2 -0
  53. package/dist/source.js.map +1 -0
  54. package/dist/wire.d.ts +2 -0
  55. package/dist/wire.d.ts.map +1 -0
  56. package/dist/wire.js +2 -0
  57. package/dist/wire.js.map +1 -0
  58. package/docs/agents.md +1 -1
  59. package/docs/api-keys.md +7 -6
  60. package/docs/api.md +10 -10
  61. package/docs/client-behavior.md +5 -5
  62. package/docs/coordination.md +52 -62
  63. package/docs/data-sources.md +1 -1
  64. package/docs/examples/agent-human.md +4 -4
  65. package/docs/examples/ai-sdk-tool.md +1 -1
  66. package/docs/examples/existing-python-backend.md +15 -4
  67. package/docs/examples/nextjs.md +27 -6
  68. package/docs/examples/server-agent.md +2 -2
  69. package/docs/how-it-works.md +4 -4
  70. package/docs/identity.md +2 -1
  71. package/docs/integration-guide.md +24 -13
  72. package/docs/internal/README.md +18 -0
  73. package/docs/internal/agent-fleet-coordination-design.md +171 -0
  74. package/docs/internal/agent-orchestration.md +58 -0
  75. package/docs/internal/commit-identifiers.md +91 -0
  76. package/docs/internal/concurrency-open-decisions.md +37 -0
  77. package/docs/internal/data-source-reverse-channel.md +150 -0
  78. package/docs/internal/per-field-conflict-detection.md +165 -0
  79. package/docs/internal/postgres-replication.md +64 -0
  80. package/docs/internal/serializable-schema.md +119 -0
  81. package/docs/internal/structure.md +32 -0
  82. package/docs/mcp.md +2 -2
  83. package/docs/migration.md +3 -3
  84. package/docs/quickstart.md +2 -2
  85. package/docs/react.md +5 -5
  86. package/docs/schema-contract.md +3 -3
  87. package/docs/sessions.md +91 -37
  88. package/examples/README.md +2 -2
  89. package/examples/data-source/README.md +1 -1
  90. package/examples/data-source/ablo-driver.ts +1 -1
  91. package/examples/data-source/customer-server.ts +1 -1
  92. package/examples/data-source/run.ts +1 -1
  93. package/examples/data-source/schema.ts +1 -1
  94. package/examples/quickstart.ts +2 -2
  95. package/llms.txt +8 -8
  96. package/package.json +63 -166
  97. package/bin/ablo.cjs +0 -39
  98. package/dist/BaseSyncedStore.d.ts +0 -843
  99. package/dist/BaseSyncedStore.js +0 -1971
  100. package/dist/Database.d.ts +0 -323
  101. package/dist/Database.js +0 -1502
  102. package/dist/InstanceCache.d.ts +0 -237
  103. package/dist/InstanceCache.js +0 -1166
  104. package/dist/LazyReferenceCollection.d.ts +0 -177
  105. package/dist/LazyReferenceCollection.js +0 -461
  106. package/dist/Model.d.ts +0 -454
  107. package/dist/Model.js +0 -919
  108. package/dist/ModelRegistry.d.ts +0 -225
  109. package/dist/ModelRegistry.js +0 -539
  110. package/dist/NetworkMonitor.d.ts +0 -28
  111. package/dist/NetworkMonitor.js +0 -79
  112. package/dist/RuntimeContext.d.ts +0 -52
  113. package/dist/RuntimeContext.js +0 -80
  114. package/dist/SyncClient.d.ts +0 -541
  115. package/dist/SyncClient.js +0 -2202
  116. package/dist/adapters/alwaysOnline.d.ts +0 -14
  117. package/dist/adapters/alwaysOnline.js +0 -17
  118. package/dist/adapters/inMemoryStorage.d.ts +0 -31
  119. package/dist/adapters/inMemoryStorage.js +0 -110
  120. package/dist/ai-sdk/coordinatedTool.d.ts +0 -120
  121. package/dist/ai-sdk/coordinatedTool.js +0 -134
  122. package/dist/ai-sdk/coordinationContext.d.ts +0 -46
  123. package/dist/ai-sdk/coordinationContext.js +0 -106
  124. package/dist/ai-sdk/index.d.ts +0 -121
  125. package/dist/ai-sdk/index.js +0 -121
  126. package/dist/ai-sdk/wrap.d.ts +0 -65
  127. package/dist/ai-sdk/wrap.js +0 -39
  128. package/dist/auth/index.d.ts +0 -1
  129. package/dist/auth/index.js +0 -8
  130. package/dist/batching/index.d.ts +0 -55
  131. package/dist/batching/index.js +0 -147
  132. package/dist/client/Ablo.d.ts +0 -231
  133. package/dist/client/Ablo.js +0 -160
  134. package/dist/client/abloClient.d.ts +0 -309
  135. package/dist/client/abloClient.js +0 -13
  136. package/dist/client/clientPrelude.d.ts +0 -52
  137. package/dist/client/clientPrelude.js +0 -60
  138. package/dist/client/consoleLogger.d.ts +0 -35
  139. package/dist/client/consoleLogger.js +0 -44
  140. package/dist/client/coreClient.d.ts +0 -60
  141. package/dist/client/coreClient.js +0 -118
  142. package/dist/client/createInternalComponents.d.ts +0 -50
  143. package/dist/client/createInternalComponents.js +0 -98
  144. package/dist/client/createModelProxy.d.ts +0 -248
  145. package/dist/client/createModelProxy.js +0 -884
  146. package/dist/client/humans.d.ts +0 -69
  147. package/dist/client/humans.js +0 -78
  148. package/dist/client/modelRegistration.d.ts +0 -10
  149. package/dist/client/modelRegistration.js +0 -312
  150. package/dist/client/options.d.ts +0 -461
  151. package/dist/client/options.js +0 -7
  152. package/dist/client/reactiveEngine.d.ts +0 -53
  153. package/dist/client/reactiveEngine.js +0 -688
  154. package/dist/client/resourceTypes.d.ts +0 -12
  155. package/dist/client/resourceTypes.js +0 -10
  156. package/dist/client/schemaConfig.d.ts +0 -44
  157. package/dist/client/schemaConfig.js +0 -185
  158. package/dist/client/storeCluster.d.ts +0 -47
  159. package/dist/client/storeCluster.js +0 -118
  160. package/dist/client/storeLifecycle.d.ts +0 -61
  161. package/dist/client/storeLifecycle.js +0 -231
  162. package/dist/client/validateAbloOptions.d.ts +0 -42
  163. package/dist/client/validateAbloOptions.js +0 -43
  164. package/dist/client/wsMutationExecutor.d.ts +0 -27
  165. package/dist/client/wsMutationExecutor.js +0 -72
  166. package/dist/context.d.ts +0 -42
  167. package/dist/context.js +0 -81
  168. package/dist/coordination/ClaimLog.d.ts +0 -26
  169. package/dist/coordination/ClaimLog.js +0 -32
  170. package/dist/coordination/index.d.ts +0 -1
  171. package/dist/coordination/index.js +0 -8
  172. package/dist/core/index.d.ts +0 -33
  173. package/dist/core/index.js +0 -48
  174. package/dist/docs/catalog.d.ts +0 -72
  175. package/dist/docs/catalog.js +0 -230
  176. package/dist/docs/index.d.ts +0 -10
  177. package/dist/docs/index.js +0 -10
  178. package/dist/environment.d.ts +0 -1
  179. package/dist/environment.js +0 -8
  180. package/dist/interfaces/index.d.ts +0 -311
  181. package/dist/interfaces/index.js +0 -9
  182. package/dist/keys/index.d.ts +0 -1
  183. package/dist/keys/index.js +0 -8
  184. package/dist/mutators/RecordingMutation.d.ts +0 -36
  185. package/dist/mutators/RecordingMutation.js +0 -182
  186. package/dist/mutators/Transaction.d.ts +0 -40
  187. package/dist/mutators/Transaction.js +0 -58
  188. package/dist/mutators/UndoManager.d.ts +0 -258
  189. package/dist/mutators/UndoManager.js +0 -658
  190. package/dist/mutators/defineMutators.d.ts +0 -60
  191. package/dist/mutators/defineMutators.js +0 -18
  192. package/dist/mutators/inverseOp.d.ts +0 -126
  193. package/dist/mutators/inverseOp.js +0 -71
  194. package/dist/mutators/mutateActions.d.ts +0 -45
  195. package/dist/mutators/mutateActions.js +0 -105
  196. package/dist/mutators/readerActions.d.ts +0 -33
  197. package/dist/mutators/readerActions.js +0 -57
  198. package/dist/mutators/undoApply.d.ts +0 -51
  199. package/dist/mutators/undoApply.js +0 -117
  200. package/dist/policy/index.d.ts +0 -21
  201. package/dist/policy/index.js +0 -20
  202. package/dist/query/QueryProcessor.d.ts +0 -75
  203. package/dist/query/QueryProcessor.js +0 -255
  204. package/dist/query/client.d.ts +0 -64
  205. package/dist/query/client.js +0 -138
  206. package/dist/query/types.d.ts +0 -85
  207. package/dist/query/types.js +0 -16
  208. package/dist/react/AbloProvider.d.ts +0 -242
  209. package/dist/react/AbloProvider.js +0 -458
  210. package/dist/react/ClientSideSuspense.d.ts +0 -36
  211. package/dist/react/ClientSideSuspense.js +0 -17
  212. package/dist/react/DefaultFallback.d.ts +0 -24
  213. package/dist/react/DefaultFallback.js +0 -43
  214. package/dist/react/context.d.ts +0 -55
  215. package/dist/react/context.js +0 -29
  216. package/dist/react/createAbloReact.d.ts +0 -56
  217. package/dist/react/createAbloReact.js +0 -51
  218. package/dist/react/index.d.ts +0 -62
  219. package/dist/react/index.js +0 -69
  220. package/dist/react/internalContext.d.ts +0 -33
  221. package/dist/react/internalContext.js +0 -3
  222. package/dist/react/useAblo.d.ts +0 -82
  223. package/dist/react/useAblo.js +0 -120
  224. package/dist/react/useCurrentUserId.d.ts +0 -22
  225. package/dist/react/useCurrentUserId.js +0 -34
  226. package/dist/react/useErrorListener.d.ts +0 -20
  227. package/dist/react/useErrorListener.js +0 -38
  228. package/dist/react/useMutationFailureListener.d.ts +0 -26
  229. package/dist/react/useMutationFailureListener.js +0 -38
  230. package/dist/react/useMutators.d.ts +0 -56
  231. package/dist/react/useMutators.js +0 -84
  232. package/dist/react/useReactive.d.ts +0 -35
  233. package/dist/react/useReactive.js +0 -123
  234. package/dist/react/useSyncStatus.d.ts +0 -59
  235. package/dist/react/useSyncStatus.js +0 -76
  236. package/dist/react/useUndoScope.d.ts +0 -34
  237. package/dist/react/useUndoScope.js +0 -81
  238. package/dist/schema/coordination.d.ts +0 -112
  239. package/dist/schema/coordination.js +0 -133
  240. package/dist/schema/ddl.d.ts +0 -97
  241. package/dist/schema/ddl.js +0 -491
  242. package/dist/schema/ddlLock.d.ts +0 -35
  243. package/dist/schema/ddlLock.js +0 -46
  244. package/dist/schema/diff.d.ts +0 -225
  245. package/dist/schema/diff.js +0 -289
  246. package/dist/schema/generate.d.ts +0 -19
  247. package/dist/schema/generate.js +0 -86
  248. package/dist/schema/index.d.ts +0 -42
  249. package/dist/schema/index.js +0 -80
  250. package/dist/schema/queries.d.ts +0 -201
  251. package/dist/schema/queries.js +0 -144
  252. package/dist/schema/select.d.ts +0 -40
  253. package/dist/schema/select.js +0 -90
  254. package/dist/schema/serialize.d.ts +0 -115
  255. package/dist/schema/serialize.js +0 -265
  256. package/dist/schema/sugar.d.ts +0 -109
  257. package/dist/schema/sugar.js +0 -83
  258. package/dist/schema/syncDeltaRow.d.ts +0 -6
  259. package/dist/schema/syncDeltaRow.js +0 -6
  260. package/dist/server/adapter.d.ts +0 -173
  261. package/dist/server/adapter.js +0 -18
  262. package/dist/server/commit.d.ts +0 -107
  263. package/dist/server/commit.js +0 -1
  264. package/dist/server/index.d.ts +0 -14
  265. package/dist/server/index.js +0 -2
  266. package/dist/server/readConfig.d.ts +0 -80
  267. package/dist/server/readConfig.js +0 -8
  268. package/dist/server/storageMode.d.ts +0 -23
  269. package/dist/server/storageMode.js +0 -17
  270. package/dist/source/adapter.d.ts +0 -83
  271. package/dist/source/adapter.js +0 -24
  272. package/dist/source/adapters/drizzle.d.ts +0 -48
  273. package/dist/source/adapters/drizzle.js +0 -219
  274. package/dist/source/adapters/kysely.d.ts +0 -42
  275. package/dist/source/adapters/kysely.js +0 -205
  276. package/dist/source/adapters/kyselyMutationCore.d.ts +0 -76
  277. package/dist/source/adapters/kyselyMutationCore.js +0 -125
  278. package/dist/source/adapters/memory.d.ts +0 -13
  279. package/dist/source/adapters/memory.js +0 -130
  280. package/dist/source/adapters/prisma.d.ts +0 -63
  281. package/dist/source/adapters/prisma.js +0 -202
  282. package/dist/source/conformance.d.ts +0 -37
  283. package/dist/source/conformance.js +0 -215
  284. package/dist/source/connector.d.ts +0 -95
  285. package/dist/source/connector.js +0 -266
  286. package/dist/source/connectorProtocol.d.ts +0 -154
  287. package/dist/source/connectorProtocol.js +0 -163
  288. package/dist/source/contract.d.ts +0 -195
  289. package/dist/source/contract.js +0 -164
  290. package/dist/source/factory.d.ts +0 -92
  291. package/dist/source/factory.js +0 -286
  292. package/dist/source/idempotency.d.ts +0 -61
  293. package/dist/source/idempotency.js +0 -144
  294. package/dist/source/index.d.ts +0 -23
  295. package/dist/source/index.js +0 -30
  296. package/dist/source/migrations.d.ts +0 -21
  297. package/dist/source/migrations.js +0 -103
  298. package/dist/source/next.d.ts +0 -32
  299. package/dist/source/next.js +0 -25
  300. package/dist/source/pushQueue.d.ts +0 -134
  301. package/dist/source/pushQueue.js +0 -256
  302. package/dist/source/signing.d.ts +0 -92
  303. package/dist/source/signing.js +0 -162
  304. package/dist/source/types.d.ts +0 -401
  305. package/dist/source/types.js +0 -59
  306. package/dist/storeContract.d.ts +0 -145
  307. package/dist/storeContract.js +0 -12
  308. package/dist/stores/DatabaseManager.d.ts +0 -107
  309. package/dist/stores/DatabaseManager.js +0 -388
  310. package/dist/stores/ObjectStore.d.ts +0 -115
  311. package/dist/stores/ObjectStore.js +0 -393
  312. package/dist/stores/ObjectStoreContract.d.ts +0 -38
  313. package/dist/stores/ObjectStoreContract.js +0 -1
  314. package/dist/stores/StoreManager.d.ts +0 -114
  315. package/dist/stores/StoreManager.js +0 -304
  316. package/dist/stores/SyncActionStore.d.ts +0 -99
  317. package/dist/stores/SyncActionStore.js +0 -506
  318. package/dist/stores/openIDBWithTimeout.d.ts +0 -65
  319. package/dist/stores/openIDBWithTimeout.js +0 -153
  320. package/dist/stores/syncAction.d.ts +0 -26
  321. package/dist/stores/syncAction.js +0 -16
  322. package/dist/surface.d.ts +0 -36
  323. package/dist/surface.js +0 -75
  324. package/dist/sync/BootstrapFetcher.d.ts +0 -284
  325. package/dist/sync/BootstrapFetcher.js +0 -964
  326. package/dist/sync/ConnectionManager.d.ts +0 -8
  327. package/dist/sync/ConnectionManager.js +0 -8
  328. package/dist/sync/OnDemandLoader.d.ts +0 -231
  329. package/dist/sync/OnDemandLoader.js +0 -743
  330. package/dist/sync/SubscriptionManager.d.ts +0 -159
  331. package/dist/sync/SubscriptionManager.js +0 -243
  332. package/dist/sync/SyncWebSocket.d.ts +0 -173
  333. package/dist/sync/SyncWebSocket.js +0 -438
  334. package/dist/sync/awaitClaimGrant.d.ts +0 -6
  335. package/dist/sync/awaitClaimGrant.js +0 -6
  336. package/dist/sync/bootstrapApply.d.ts +0 -73
  337. package/dist/sync/bootstrapApply.js +0 -73
  338. package/dist/sync/commitFrames.d.ts +0 -8
  339. package/dist/sync/commitFrames.js +0 -8
  340. package/dist/sync/contextPorts.d.ts +0 -18
  341. package/dist/sync/contextPorts.js +0 -31
  342. package/dist/sync/createClaimStream.d.ts +0 -7
  343. package/dist/sync/createClaimStream.js +0 -7
  344. package/dist/sync/createPresenceStream.d.ts +0 -69
  345. package/dist/sync/createPresenceStream.js +0 -200
  346. package/dist/sync/createSnapshot.d.ts +0 -29
  347. package/dist/sync/createSnapshot.js +0 -118
  348. package/dist/sync/credentialLifecycle.d.ts +0 -7
  349. package/dist/sync/credentialLifecycle.js +0 -7
  350. package/dist/sync/deltaPipeline.d.ts +0 -114
  351. package/dist/sync/deltaPipeline.js +0 -278
  352. package/dist/sync/groupChange.d.ts +0 -116
  353. package/dist/sync/groupChange.js +0 -244
  354. package/dist/sync/participants.d.ts +0 -132
  355. package/dist/sync/participants.js +0 -346
  356. package/dist/sync/persistedPrefix.d.ts +0 -12
  357. package/dist/sync/persistedPrefix.js +0 -22
  358. package/dist/sync/schemaDrift.d.ts +0 -55
  359. package/dist/sync/schemaDrift.js +0 -53
  360. package/dist/sync/schemas.d.ts +0 -71
  361. package/dist/sync/schemas.js +0 -94
  362. package/dist/sync/syncCursor.d.ts +0 -40
  363. package/dist/sync/syncCursor.js +0 -55
  364. package/dist/sync/syncPlan.d.ts +0 -54
  365. package/dist/sync/syncPlan.js +0 -50
  366. package/dist/sync/wsFrameHandlers.d.ts +0 -8
  367. package/dist/sync/wsFrameHandlers.js +0 -8
  368. package/dist/syncLog/contract.d.ts +0 -20
  369. package/dist/syncLog/contract.js +0 -19
  370. package/dist/syncLog/index.d.ts +0 -1
  371. package/dist/syncLog/index.js +0 -1
  372. package/dist/transaction/ablo.d.ts +0 -88
  373. package/dist/transaction/ablo.js +0 -33
  374. package/dist/transaction/auth/apiKey.d.ts +0 -152
  375. package/dist/transaction/auth/apiKey.js +0 -419
  376. package/dist/transaction/auth/bootstrapScope.d.ts +0 -15
  377. package/dist/transaction/auth/bootstrapScope.js +0 -1
  378. package/dist/transaction/auth/capability.d.ts +0 -212
  379. package/dist/transaction/auth/capability.js +0 -224
  380. package/dist/transaction/auth/credentialEndpoint.d.ts +0 -61
  381. package/dist/transaction/auth/credentialEndpoint.js +0 -86
  382. package/dist/transaction/auth/credentialPolicy.d.ts +0 -148
  383. package/dist/transaction/auth/credentialPolicy.js +0 -125
  384. package/dist/transaction/auth/credentialSource.d.ts +0 -30
  385. package/dist/transaction/auth/credentialSource.js +0 -55
  386. package/dist/transaction/auth/hostedEndpoints.d.ts +0 -21
  387. package/dist/transaction/auth/hostedEndpoints.js +0 -21
  388. package/dist/transaction/auth/identity.d.ts +0 -55
  389. package/dist/transaction/auth/identity.js +0 -210
  390. package/dist/transaction/auth/index.d.ts +0 -162
  391. package/dist/transaction/auth/index.js +0 -304
  392. package/dist/transaction/auth/schemas.d.ts +0 -59
  393. package/dist/transaction/auth/schemas.js +0 -85
  394. package/dist/transaction/auth/sessionMint.d.ts +0 -28
  395. package/dist/transaction/auth/sessionMint.js +0 -85
  396. package/dist/transaction/coordination/awaitClaimGrant.d.ts +0 -56
  397. package/dist/transaction/coordination/awaitClaimGrant.js +0 -124
  398. package/dist/transaction/coordination/claimHeartbeatLoop.d.ts +0 -84
  399. package/dist/transaction/coordination/claimHeartbeatLoop.js +0 -108
  400. package/dist/transaction/coordination/claimMeta.d.ts +0 -49
  401. package/dist/transaction/coordination/claimMeta.js +0 -52
  402. package/dist/transaction/coordination/createClaimStream.d.ts +0 -64
  403. package/dist/transaction/coordination/createClaimStream.js +0 -475
  404. package/dist/transaction/coordination/events.d.ts +0 -74
  405. package/dist/transaction/coordination/events.js +0 -7
  406. package/dist/transaction/coordination/index.d.ts +0 -19
  407. package/dist/transaction/coordination/index.js +0 -45
  408. package/dist/transaction/coordination/locator.d.ts +0 -104
  409. package/dist/transaction/coordination/locator.js +0 -102
  410. package/dist/transaction/coordination/schema.d.ts +0 -1536
  411. package/dist/transaction/coordination/schema.js +0 -1177
  412. package/dist/transaction/coordination/targetConflict.d.ts +0 -2
  413. package/dist/transaction/coordination/targetConflict.js +0 -107
  414. package/dist/transaction/coordination/trace.d.ts +0 -78
  415. package/dist/transaction/coordination/trace.js +0 -138
  416. package/dist/transaction/durableWrites.d.ts +0 -62
  417. package/dist/transaction/durableWrites.js +0 -71
  418. package/dist/transaction/environment.d.ts +0 -105
  419. package/dist/transaction/environment.js +0 -108
  420. package/dist/transaction/errorCodes.d.ts +0 -403
  421. package/dist/transaction/errorCodes.js +0 -484
  422. package/dist/transaction/errors.d.ts +0 -428
  423. package/dist/transaction/errors.js +0 -686
  424. package/dist/transaction/footprint.d.ts +0 -111
  425. package/dist/transaction/footprint.js +0 -0
  426. package/dist/transaction/index.d.ts +0 -20
  427. package/dist/transaction/index.js +0 -20
  428. package/dist/transaction/keys/index.d.ts +0 -87
  429. package/dist/transaction/keys/index.js +0 -207
  430. package/dist/transaction/log/syncDeltaRow.d.ts +0 -158
  431. package/dist/transaction/log/syncDeltaRow.js +0 -95
  432. package/dist/transaction/logPosition.d.ts +0 -97
  433. package/dist/transaction/logPosition.js +0 -125
  434. package/dist/transaction/logger.d.ts +0 -16
  435. package/dist/transaction/logger.js +0 -7
  436. package/dist/transaction/observability.d.ts +0 -53
  437. package/dist/transaction/observability.js +0 -19
  438. package/dist/transaction/persistence.d.ts +0 -12
  439. package/dist/transaction/persistence.js +0 -11
  440. package/dist/transaction/plugin.d.ts +0 -285
  441. package/dist/transaction/plugin.js +0 -106
  442. package/dist/transaction/policy/types.d.ts +0 -217
  443. package/dist/transaction/policy/types.js +0 -126
  444. package/dist/transaction/resources/functionalUpdate.d.ts +0 -79
  445. package/dist/transaction/resources/functionalUpdate.js +0 -87
  446. package/dist/transaction/resources/httpResources.d.ts +0 -321
  447. package/dist/transaction/resources/httpResources.js +0 -7
  448. package/dist/transaction/resources/modelOperations.d.ts +0 -427
  449. package/dist/transaction/resources/modelOperations.js +0 -12
  450. package/dist/transaction/resources/mutationOptions.d.ts +0 -66
  451. package/dist/transaction/resources/mutationOptions.js +0 -9
  452. package/dist/transaction/resources/where.d.ts +0 -101
  453. package/dist/transaction/resources/where.js +0 -115
  454. package/dist/transaction/resources/writeOptionsSchema.d.ts +0 -47
  455. package/dist/transaction/resources/writeOptionsSchema.js +0 -73
  456. package/dist/transaction/schema/field.d.ts +0 -120
  457. package/dist/transaction/schema/field.js +0 -265
  458. package/dist/transaction/schema/fieldRef.d.ts +0 -38
  459. package/dist/transaction/schema/fieldRef.js +0 -11
  460. package/dist/transaction/schema/loadStrategy.d.ts +0 -45
  461. package/dist/transaction/schema/loadStrategy.js +0 -46
  462. package/dist/transaction/schema/model.d.ts +0 -379
  463. package/dist/transaction/schema/model.js +0 -123
  464. package/dist/transaction/schema/openapi.d.ts +0 -58
  465. package/dist/transaction/schema/openapi.js +0 -501
  466. package/dist/transaction/schema/relation.d.ts +0 -204
  467. package/dist/transaction/schema/relation.js +0 -104
  468. package/dist/transaction/schema/residency.d.ts +0 -29
  469. package/dist/transaction/schema/residency.js +0 -25
  470. package/dist/transaction/schema/roles.d.ts +0 -249
  471. package/dist/transaction/schema/roles.js +0 -230
  472. package/dist/transaction/schema/schema.d.ts +0 -351
  473. package/dist/transaction/schema/schema.js +0 -325
  474. package/dist/transaction/schema/tenancy.d.ts +0 -139
  475. package/dist/transaction/schema/tenancy.js +0 -190
  476. package/dist/transaction/transactionLayer.d.ts +0 -82
  477. package/dist/transaction/transactionLayer.js +0 -24
  478. package/dist/transaction/transactions/settlement/commitEnvelope.d.ts +0 -143
  479. package/dist/transaction/transactions/settlement/commitEnvelope.js +0 -161
  480. package/dist/transaction/transactions/settlement/httpCommitEnvelope.d.ts +0 -53
  481. package/dist/transaction/transactions/settlement/httpCommitEnvelope.js +0 -207
  482. package/dist/transaction/transactions/settlement/idempotencyKey.d.ts +0 -10
  483. package/dist/transaction/transactions/settlement/idempotencyKey.js +0 -9
  484. package/dist/transaction/transactions/settlement/pendingWrite.d.ts +0 -112
  485. package/dist/transaction/transactions/settlement/pendingWrite.js +0 -20
  486. package/dist/transaction/transport/commitFrames.d.ts +0 -90
  487. package/dist/transaction/transport/commitFrames.js +0 -134
  488. package/dist/transaction/transport/connectionManager.d.ts +0 -215
  489. package/dist/transaction/transport/connectionManager.js +0 -673
  490. package/dist/transaction/transport/credentialLifecycle.d.ts +0 -177
  491. package/dist/transaction/transport/credentialLifecycle.js +0 -324
  492. package/dist/transaction/transport/heartbeat.d.ts +0 -65
  493. package/dist/transaction/transport/heartbeat.js +0 -93
  494. package/dist/transaction/transport/httpClient.d.ts +0 -131
  495. package/dist/transaction/transport/httpClient.js +0 -146
  496. package/dist/transaction/transport/httpOptions.d.ts +0 -33
  497. package/dist/transaction/transport/httpOptions.js +0 -12
  498. package/dist/transaction/transport/httpTransport.d.ts +0 -8
  499. package/dist/transaction/transport/httpTransport.js +0 -1388
  500. package/dist/transaction/transport/networkProbe.d.ts +0 -84
  501. package/dist/transaction/transport/networkProbe.js +0 -207
  502. package/dist/transaction/transport/wsFrameHandlers.d.ts +0 -128
  503. package/dist/transaction/transport/wsFrameHandlers.js +0 -429
  504. package/dist/transaction/transport/wsTransport.d.ts +0 -574
  505. package/dist/transaction/transport/wsTransport.js +0 -1023
  506. package/dist/transaction/types/assertExact.d.ts +0 -17
  507. package/dist/transaction/types/assertExact.js +0 -1
  508. package/dist/transaction/types/global.d.ts +0 -107
  509. package/dist/transaction/types/global.js +0 -40
  510. package/dist/transaction/types/index.d.ts +0 -205
  511. package/dist/transaction/types/index.js +0 -56
  512. package/dist/transaction/types/modelData.d.ts +0 -10
  513. package/dist/transaction/types/modelData.js +0 -9
  514. package/dist/transaction/types/participant.d.ts +0 -20
  515. package/dist/transaction/types/participant.js +0 -10
  516. package/dist/transaction/types/streams.d.ts +0 -550
  517. package/dist/transaction/types/streams.js +0 -11
  518. package/dist/transaction/utils/asyncIterator.d.ts +0 -34
  519. package/dist/transaction/utils/asyncIterator.js +0 -135
  520. package/dist/transaction/utils/duration.d.ts +0 -50
  521. package/dist/transaction/utils/duration.js +0 -77
  522. package/dist/transaction/utils/json.d.ts +0 -57
  523. package/dist/transaction/utils/json.js +0 -276
  524. package/dist/transaction/wire/accountResponses.d.ts +0 -420
  525. package/dist/transaction/wire/accountResponses.js +0 -290
  526. package/dist/transaction/wire/auth.d.ts +0 -56
  527. package/dist/transaction/wire/auth.js +0 -63
  528. package/dist/transaction/wire/bootstrapReason.d.ts +0 -9
  529. package/dist/transaction/wire/bootstrapReason.js +0 -8
  530. package/dist/transaction/wire/claimEvent.d.ts +0 -76
  531. package/dist/transaction/wire/claimEvent.js +0 -73
  532. package/dist/transaction/wire/claims.d.ts +0 -530
  533. package/dist/transaction/wire/claims.js +0 -327
  534. package/dist/transaction/wire/commit.d.ts +0 -603
  535. package/dist/transaction/wire/commit.js +0 -321
  536. package/dist/transaction/wire/delta.d.ts +0 -250
  537. package/dist/transaction/wire/delta.js +0 -147
  538. package/dist/transaction/wire/errorEnvelope.d.ts +0 -72
  539. package/dist/transaction/wire/errorEnvelope.js +0 -123
  540. package/dist/transaction/wire/feedCursor.d.ts +0 -60
  541. package/dist/transaction/wire/feedCursor.js +0 -82
  542. package/dist/transaction/wire/feedEvent.d.ts +0 -204
  543. package/dist/transaction/wire/feedEvent.js +0 -65
  544. package/dist/transaction/wire/frames.d.ts +0 -194
  545. package/dist/transaction/wire/frames.js +0 -50
  546. package/dist/transaction/wire/inboundFrames.d.ts +0 -562
  547. package/dist/transaction/wire/inboundFrames.js +0 -116
  548. package/dist/transaction/wire/index.d.ts +0 -54
  549. package/dist/transaction/wire/index.js +0 -83
  550. package/dist/transaction/wire/listEnvelope.d.ts +0 -37
  551. package/dist/transaction/wire/listEnvelope.js +0 -42
  552. package/dist/transaction/wire/modelMutations.d.ts +0 -31
  553. package/dist/transaction/wire/modelMutations.js +0 -52
  554. package/dist/transaction/wire/modelResponses.d.ts +0 -85
  555. package/dist/transaction/wire/modelResponses.js +0 -43
  556. package/dist/transaction/wire/modelShape.d.ts +0 -78
  557. package/dist/transaction/wire/modelShape.js +0 -74
  558. package/dist/transaction/wire/protocol.d.ts +0 -38
  559. package/dist/transaction/wire/protocol.js +0 -38
  560. package/dist/transaction/wire/protocolVersion.d.ts +0 -73
  561. package/dist/transaction/wire/protocolVersion.js +0 -83
  562. package/dist/transactions/mutations/MutationQueue.d.ts +0 -661
  563. package/dist/transactions/mutations/MutationQueue.js +0 -2807
  564. package/dist/transactions/mutations/MutationStore.d.ts +0 -20
  565. package/dist/transactions/mutations/MutationStore.js +0 -53
  566. package/dist/transactions/mutations/UnconfirmedWrites.d.ts +0 -82
  567. package/dist/transactions/mutations/UnconfirmedWrites.js +0 -104
  568. package/dist/transactions/mutations/coalesceRules.d.ts +0 -58
  569. package/dist/transactions/mutations/coalesceRules.js +0 -140
  570. package/dist/transactions/mutations/commitLatency.d.ts +0 -52
  571. package/dist/transactions/mutations/commitLatency.js +0 -130
  572. package/dist/transactions/mutations/commitOutboxStore.d.ts +0 -28
  573. package/dist/transactions/mutations/commitOutboxStore.js +0 -26
  574. package/dist/transactions/mutations/commitPayload.d.ts +0 -165
  575. package/dist/transactions/mutations/commitPayload.js +0 -152
  576. package/dist/transactions/mutations/deltaConfirmation.d.ts +0 -63
  577. package/dist/transactions/mutations/deltaConfirmation.js +0 -235
  578. package/dist/transactions/mutations/durableWriteStore.d.ts +0 -14
  579. package/dist/transactions/mutations/durableWriteStore.js +0 -12
  580. package/dist/transactions/mutations/optimisticApply.d.ts +0 -49
  581. package/dist/transactions/mutations/optimisticApply.js +0 -65
  582. package/dist/transactions/mutations/replayValidation.d.ts +0 -187
  583. package/dist/transactions/mutations/replayValidation.js +0 -164
  584. package/dist/utils/mobxSetup.d.ts +0 -53
  585. package/dist/utils/mobxSetup.js +0 -330
  586. package/dist/views/QueryView.d.ts +0 -79
  587. package/dist/views/QueryView.js +0 -218
  588. package/dist/views/ViewRegistry.d.ts +0 -20
  589. package/dist/views/ViewRegistry.js +0 -55
  590. package/dist/views/incrementalView.d.ts +0 -45
  591. package/dist/views/incrementalView.js +0 -69
  592. package/dist/webhooks/events.d.ts +0 -43
  593. package/dist/webhooks/events.js +0 -42
  594. package/dist/webhooks/index.d.ts +0 -8
  595. package/dist/webhooks/index.js +0 -8
  596. package/dist/wire/index.d.ts +0 -1
  597. package/dist/wire/index.js +0 -8
@@ -1,2202 +0,0 @@
1
- /**
2
- * Applies model mutations and manages the offline write queue. The
3
- * SyncClient turns local create, update, delete, and archive calls into
4
- * optimistic changes, holds them while the client is offline, sends them to
5
- * the server when connectivity returns, and resolves conflicts when the
6
- * server's version of a row disagrees with the local one. It sits between the
7
- * reactive object pool and the {@link MutationQueue} that delivers writes
8
- * over the network.
9
- */
10
- import { runInAction } from 'mobx';
11
- import { v4 as uuid } from 'uuid';
12
- import { InstanceCache, ModelScope } from './InstanceCache.js';
13
- import { Model } from './Model.js';
14
- import { snapshotJsonValue } from './transaction/utils/json.js';
15
- // ModelRegistry instance accessed via this.objectPool.registry
16
- import { LoadStrategy } from './transaction/types/index.js';
17
- import { globalRuntime } from './context.js';
18
- import { AbloAuthenticationError, AbloError, AbloValidationError } from './transaction/errors.js';
19
- import { EventEmitter } from 'events';
20
- import { NetworkMonitor } from './NetworkMonitor.js';
21
- import { MutationQueue } from './transactions/mutations/MutationQueue.js';
22
- import { observeCommitLatency, } from './transactions/mutations/commitLatency.js';
23
- import { legacyPendingMutationRecordSchema, PENDING_MUTATION_REPLAY_WINDOW_MS, pendingMutationRecordId, pendingMutationRecordSchema, persistedMutationSchema, } from './transactions/mutations/replayValidation.js';
24
- import { UnconfirmedWrites, } from './transactions/mutations/UnconfirmedWrites.js';
25
- import { LogPosition } from './transaction/logPosition.js';
26
- import { DatabaseCommitOutboxStore, } from './transactions/mutations/commitOutboxStore.js';
27
- /**
28
- * Reports whether an incoming snapshot record is strictly newer than the
29
- * model already in the pool. The comparison uses the server-stamped
30
- * `updatedAt` timestamp, since rows carry no numeric version and the delta
31
- * pipeline resolves order by arrival (last write wins). An undefined incoming
32
- * timestamp counts as not newer, so a known row is never clobbered; an
33
- * undefined existing timestamp means the pooled row is unversioned, so the
34
- * incoming record wins. The scoped hydrate-on-enter path uses this to drop
35
- * snapshot rows that a live delta has already advanced past.
36
- */
37
- function rawRecordIsNewer(data, existing) {
38
- const raw = data.updatedAt;
39
- const inMs = raw instanceof Date
40
- ? raw.getTime()
41
- : typeof raw === 'string'
42
- ? (Number.isNaN(Date.parse(raw)) ? undefined : Date.parse(raw))
43
- : typeof raw === 'number'
44
- ? raw
45
- : undefined;
46
- const exMs = existing.updatedAt instanceof Date ? existing.updatedAt.getTime() : undefined;
47
- if (inMs === undefined)
48
- return false;
49
- if (exMs === undefined)
50
- return true;
51
- return inMs > exMs;
52
- }
53
- /**
54
- * Converts an untyped server `updatedAt` value — an ISO string, epoch number,
55
- * or Date read off an untyped row — into epoch milliseconds for
56
- * last-write-wins comparison. Falsy or non-date values become 0, matching the
57
- * conflict resolver's rule that a missing timestamp sorts as the epoch.
58
- */
59
- function toEpochMs(value) {
60
- if (!value)
61
- return 0;
62
- if (value instanceof Date)
63
- return value.getTime();
64
- if (typeof value === 'string' || typeof value === 'number') {
65
- return new Date(value).getTime();
66
- }
67
- return 0;
68
- }
69
- export class SyncClient extends EventEmitter {
70
- runtime;
71
- objectPool;
72
- database;
73
- get mutationExecutor() { return this.runtime.mutationExecutor; }
74
- networkMonitor;
75
- /**
76
- * @internal — test seam, stripped from the published declarations by
77
- * `stripInternal`. Unit suites deliver queue lifecycle events directly.
78
- */
79
- mutationQueue;
80
- observers = new Set();
81
- // Authentication context
82
- userId = null;
83
- organizationId = null;
84
- // Pending mutations queue
85
- pendingMutations = [];
86
- stagedMutationIds = new Set();
87
- pendingJournalBatch = [];
88
- journalFlushScheduled = false;
89
- commitOutboxNamespace;
90
- /**
91
- * Tracks the ids of transactions the client has applied optimistically but
92
- * the server has not yet confirmed. When a delta arrives, the receive path
93
- * consults this set to recognize the echo of the client's own mutation and
94
- * skip the now-redundant pool update; the IndexedDB write still runs,
95
- * because the delta is the authoritative version of the row. Without this
96
- * discriminator, an optimistically applied delete followed by a
97
- * server-confirmed create echo would resurrect the row for the window
98
- * between the two confirmations.
99
- *
100
- * The set is bounded with first-in-first-out eviction, and
101
- * {@link SyncClient.getEchoMetrics} exposes its counters.
102
- */
103
- echoTracker = new UnconfirmedWrites();
104
- // Connection state
105
- connectionState = 'disconnected';
106
- // Configuration
107
- isDisposed = false;
108
- /**
109
- * The client's position in the global delta order, held as the single
110
- * canonical {@link LogPosition} instance. The store advances `applied` and
111
- * `persisted` as deltas land, the queue advances `acked` on commit
112
- * responses, and snapshots and claims read `readFloor`.
113
- */
114
- position = new LogPosition();
115
- constructor(objectPool, database, commitOutbox = new DatabaseCommitOutboxStore(database), commitOutboxNamespace = 'default', runtime = globalRuntime) {
116
- super();
117
- this.runtime = runtime;
118
- this.objectPool = objectPool;
119
- this.database = database;
120
- this.commitOutboxNamespace = commitOutboxNamespace;
121
- this.networkMonitor = new NetworkMonitor(this.runtime);
122
- // Initialize MutationQueue with proper configuration
123
- this.mutationQueue = new MutationQueue({
124
- position: this.position,
125
- runtime: this.runtime,
126
- maxBatchSize: 50, // Larger batches keep the batch count low for bulk operations
127
- // A short delay keeps writes responsive; coalescing still groups them
128
- batchDelay: 150,
129
- maxRetries: 3,
130
- enableOptimistic: true,
131
- enablePersistence: true,
132
- conflictResolution: {
133
- strategy: 'last-write-wins',
134
- },
135
- });
136
- this.mutationQueue.setCommitOutbox(commitOutbox);
137
- this.mutationQueue.on('commit:envelope_persisted', (event) => {
138
- if (event.sourceMutationIds.length === 0)
139
- return;
140
- const consumed = new Set(event.sourceMutationIds);
141
- this.pendingMutations = this.pendingMutations.filter((mutation) => !consumed.has(mutation.mutationId));
142
- for (const mutationId of consumed)
143
- this.stagedMutationIds.delete(mutationId);
144
- if (this.stagedMutationIds.size === 0 && this.pendingMutations.length > 0) {
145
- this.scheduleSync();
146
- }
147
- });
148
- this.mutationQueue.on('transaction:completed', (transaction) => {
149
- const completed = new Set(transaction.sourceMutationIds ?? []);
150
- if (completed.size > 0) {
151
- this.pendingMutations = this.pendingMutations.filter((mutation) => !completed.has(mutation.mutationId));
152
- }
153
- for (const mutationId of transaction.sourceMutationIds ?? []) {
154
- this.stagedMutationIds.delete(mutationId);
155
- void this.database
156
- .removeTransaction(pendingMutationRecordId(mutationId))
157
- .catch(() => undefined);
158
- }
159
- if (this.stagedMutationIds.size === 0 && this.pendingMutations.length > 0) {
160
- this.scheduleSync();
161
- }
162
- });
163
- this.mutationQueue.on('transaction:failed', ({ transaction }) => {
164
- const failed = transaction.sourceMutationIds ?? [];
165
- if (failed.length === 0)
166
- return;
167
- const failedSet = new Set(failed);
168
- this.pendingMutations = this.pendingMutations.filter((mutation) => !failedSet.has(mutation.mutationId));
169
- for (const mutationId of failed) {
170
- this.stagedMutationIds.delete(mutationId);
171
- // The queue has already rolled the model back, so replaying the
172
- // journal row on the next boot would resurrect a rejected write.
173
- void this.database
174
- .removeTransaction(pendingMutationRecordId(mutationId))
175
- .catch(() => undefined);
176
- }
177
- // Without this drain, terminally failed ids stayed claimed forever and
178
- // the size guard in processPendingMutations stalled every later write.
179
- if (this.stagedMutationIds.size === 0 && this.pendingMutations.length > 0) {
180
- this.scheduleSync();
181
- }
182
- });
183
- // Provide connection state to MutationQueue - prevents rollbacks during disconnection
184
- this.mutationQueue.setConnectionChecker(() => this.connectionState === 'connected');
185
- // Restore object-pool state when a transaction is rolled back. If the
186
- // server rejects a write or it times out, the model's previous state is
187
- // put back. Because writes are no longer applied to IndexedDB
188
- // optimistically, that store already holds the correct state.
189
- this.setupTransactionRollbackHandling();
190
- // Forward reconciliation requests from the transaction queue. When delta
191
- // confirmation times out, the client cycles the WebSocket connection to
192
- // trigger a catch-up from the server rather than rolling the write back.
193
- this.setupReconciliationForwarding();
194
- // Persist unconfirmed transactions to IndexedDB. When delta retries are
195
- // exhausted, the write is cached so it survives a tab close.
196
- this.setupAwaitingTransactionPersistence();
197
- // Setup network monitoring
198
- this.setupNetworkMonitoring();
199
- }
200
- /**
201
- * Setup network monitoring handlers
202
- */
203
- setupNetworkMonitoring() {
204
- // Both handlers emit to external listeners (which can throw) before/around
205
- // their own try/catch — route rejections into observability rather than
206
- // losing a failed reconnect flush silently.
207
- this.networkMonitor.on('online', () => {
208
- void this.handleReconnection().catch((error) => {
209
- this.runtime.observability.captureMutationFailure({
210
- context: 'network-online-reconnection',
211
- error: error instanceof Error ? error : new Error(String(error)),
212
- });
213
- });
214
- });
215
- this.networkMonitor.on('offline', () => {
216
- void this.handleDisconnection().catch((error) => {
217
- this.runtime.observability.captureMutationFailure({
218
- context: 'network-offline-handler',
219
- error: error instanceof Error ? error : new Error(String(error)),
220
- });
221
- });
222
- });
223
- }
224
- /**
225
- * Handle transaction rollback. Two distinct shapes flow through this
226
- * event:
227
- *
228
- * 1. **Server-rejected rollback** (`reason === 'permanent_error'`,
229
- * `'max_retries_exhausted'`, `'conflict_server_wins'`) — the
230
- * optimistic state is wrong, the row exists, restore previous
231
- * state and notify the UI.
232
- *
233
- * 2. **Local-cancellation cleanup** (`reason === 'model_cancelled'`,
234
- * `'cascade_parent_deleted'`) — the user deleted this model (or
235
- * its parent), so a pending UPDATE on it gets cancelled. There's
236
- * nothing to restore (the model is doomed) and no UI notification
237
- * needed (the delete itself already triggered re-renders). Just
238
- * discard the optimistic state silently.
239
- *
240
- * Treating both paths the same caused the deletion-flicker bug: every
241
- * cancelled update on a multi-child record fired a per-model observer
242
- * event and a `[SyncClient.rollback]` warn, producing N renders and N
243
- * spam log lines for one user-initiated delete.
244
- */
245
- setupTransactionRollbackHandling() {
246
- this.mutationQueue.on('optimistic:rollback', (event) => {
247
- const { model, previousState, transaction, reason, error } = event;
248
- // Local cleanup path — discard quietly. The optimistic state was
249
- // applied to a model that's already disposed by the cascading
250
- // delete, and emitting per-model observer events here would
251
- // re-render N times for one user-initiated cascade.
252
- if (reason === 'model_cancelled' || reason === 'cascade_parent_deleted') {
253
- return;
254
- }
255
- // Surface the typed AbloError fields directly — `type`/`code`/
256
- // `httpStatus`/`requestId` are what tell us the rollback cause
257
- // (e.g. `AbloValidationError` with `code: 'schema_...'`,
258
- // `AbloServerError` with `httpStatus: 500`). Falling back to
259
- // generic message lets us still see unstructured errors.
260
- // Mechanic-level breadcrumb only. The authoritative, user-facing
261
- // reason is logged once at `warn` by `MutationQueue.handleFailure`
262
- // (`Permanent error - rolling back`). Logging the same typed cause
263
- // again here at `warn` is what produced three identical dumps per
264
- // rejected write — keep it at `debug` so the rollback mechanics are
265
- // available when debugging but don't double the console noise.
266
- const abloErr = error instanceof AbloError ? error : undefined;
267
- this.runtime.logger.debug('[SyncClient.rollback]', {
268
- txType: transaction.type,
269
- modelName: transaction.modelName,
270
- modelId: transaction.modelId.slice(0, 12),
271
- reason: reason ?? 'unknown',
272
- errorType: abloErr?.type ?? error?.name,
273
- errorCode: abloErr?.code,
274
- httpStatus: abloErr?.httpStatus,
275
- requestId: abloErr?.requestId,
276
- message: error?.message,
277
- });
278
- this.runtime.observability.captureRollback({
279
- transactionType: transaction.type,
280
- modelName: transaction.modelName,
281
- modelId: transaction.modelId,
282
- reason: reason ?? 'unknown',
283
- error: error?.message,
284
- connectionState: this.connectionState,
285
- });
286
- try {
287
- if (transaction.type === 'create') {
288
- // CREATE rollback: remove the optimistically created entity
289
- this.objectPool.remove(transaction.modelId);
290
- }
291
- else if (transaction.type === 'delete' &&
292
- reason === 'permanent_error' &&
293
- error?.message?.includes('not found')) {
294
- // DELETE "not found" rollback: the entity doesn't exist on the server.
295
- // Instead of restoring a ghost entity, remove it locally too.
296
- // Both sides agree: this entity should not exist.
297
- this.runtime.observability.breadcrumb('DELETE rolled back with "not found" - removing ghost entity', 'sync.conflict', 'info', {
298
- modelId: transaction.modelId,
299
- modelName: transaction.modelName,
300
- });
301
- this.objectPool.remove(transaction.modelId);
302
- }
303
- else if (model) {
304
- // For update/delete/archive: restore model (with previousState if available)
305
- // Guard: if the model was disposed (e.g. by a concurrent DELETE rollback or
306
- // cascade), don't re-add it — Object.assign cannot restore the private
307
- // isDisposed flag, so the model would be added in a broken state.
308
- if (model.disposed) {
309
- // Follow-on of an already-logged permanent error, not its own
310
- // problem: the tx that failed has already surfaced the cause in
311
- // MutationQueue. Restoring a disposed model is a no-op by
312
- // design (can't revive the private isDisposed flag), so keep this
313
- // at `debug` instead of emitting a second `warn` that reads as a
314
- // distinct failure in the console.
315
- this.runtime.logger.debug('[SyncClient] Rollback skipped restore (model already disposed)', {
316
- modelId: transaction.modelId,
317
- modelName: transaction.modelName,
318
- reason,
319
- });
320
- }
321
- else {
322
- if (previousState)
323
- Object.assign(model, previousState);
324
- this.objectPool.add(model, ModelScope.live);
325
- }
326
- }
327
- this.notifyObservers({
328
- type: 'rollback',
329
- modelType: transaction.modelName,
330
- modelId: transaction.modelId,
331
- transactionType: transaction.type,
332
- });
333
- // Emit event so SyncedStore can clear pendingDeletes on delete rollback
334
- this.emit('sync:rollback', {
335
- modelId: transaction.modelId,
336
- modelName: transaction.modelName,
337
- transactionType: transaction.type,
338
- reason,
339
- });
340
- }
341
- catch (error) {
342
- this.runtime.observability.captureMutationFailure({
343
- context: 'rollback-failed',
344
- transactionId: transaction.id,
345
- modelName: transaction.modelName,
346
- modelId: transaction.modelId,
347
- error: error instanceof Error ? error : new Error(String(error)),
348
- });
349
- }
350
- });
351
- }
352
- /**
353
- * Forward reconciliation requests from the {@link MutationQueue} to the
354
- * sync layer. When delta confirmation times out, the queue emits
355
- * `reconciliation:needed` instead of rolling back, so optimistic state the
356
- * server may already have committed is never destroyed.
357
- */
358
- setupReconciliationForwarding() {
359
- this.mutationQueue.on('reconciliation:needed', (event) => {
360
- this.runtime.observability.captureReconciliation({
361
- reason: event.reason,
362
- model: event.model,
363
- modelId: event.modelId,
364
- syncIdNeeded: event.syncIdNeeded,
365
- lastSeenSyncId: event.lastSeenSyncId,
366
- retryCount: event.retryCount,
367
- connectionState: this.connectionState,
368
- });
369
- // Forward to SyncedStore via event — it has access to the WebSocket
370
- this.emit('reconciliation:needed', event);
371
- });
372
- }
373
- /**
374
- * Persist unconfirmed transactions to IndexedDB. When delta-confirmation
375
- * retries are exhausted, the transaction is cached so it survives a tab
376
- * close. On the next session, a WebSocket reconnect and delta catch-up
377
- * deliver the missing deltas and confirm the transaction.
378
- */
379
- setupAwaitingTransactionPersistence() {
380
- this.mutationQueue.on('transaction:persist_awaiting', (event) => {
381
- // void is safe: the handler's body is fully try/catch'd.
382
- void this.persistAwaitingTransaction(event);
383
- });
384
- // Clean up persisted awaiting transactions when they're finally confirmed
385
- this.mutationQueue.on('transaction:completed', (tx) => {
386
- // void is safe: the handler's body is fully try/catch'd.
387
- void this.removeAwaitingTransaction(tx.id);
388
- });
389
- // Echo detection bridge. When the queue stages a transaction, the
390
- // client has already optimistically applied the change to the
391
- // pool — record the tx id so the matching server delta echo gets
392
- // recognized in `applyDeltaBatchToPool`. The set is drained when
393
- // the echo lands; if a transaction is rolled back before the
394
- // server processes it, we drain on rollback too so a stale id
395
- // doesn't permanently silence a foreign delta sharing the same id
396
- // (vanishingly unlikely for UUIDs, but cheap insurance).
397
- this.mutationQueue.on('transaction:created', (tx) => {
398
- if (!tx.localOnly)
399
- this.echoTracker.markPending(tx.id);
400
- });
401
- this.mutationQueue.on('optimistic:rollback', (event) => {
402
- this.echoTracker.drainOnRollback(event.transaction.id);
403
- });
404
- }
405
- /** Persist an unconfirmed transaction to IndexedDB (never rejects — failures are captured). */
406
- async persistAwaitingTransaction(event) {
407
- if (!this.database)
408
- return;
409
- try {
410
- await this.database.saveTransaction({
411
- id: `awaiting_${event.txId}`,
412
- type: 'awaiting_delta',
413
- timestamp: Date.now(),
414
- awaitingDelta: {
415
- syncIdNeeded: event.syncIdNeeded ?? 0,
416
- modelName: event.model,
417
- modelId: event.modelId,
418
- operationType: event.operationType,
419
- },
420
- });
421
- this.runtime.observability.breadcrumb('Persisted unconfirmed transaction to IDB', 'sync.transaction', 'info', {
422
- txId: event.txId,
423
- model: event.model,
424
- modelId: event.modelId,
425
- });
426
- }
427
- catch (error) {
428
- this.runtime.observability.captureMutationFailure({
429
- context: 'persist-awaiting-transaction',
430
- modelName: event.model,
431
- modelId: event.modelId,
432
- error: error instanceof Error ? error : new Error(String(error)),
433
- });
434
- }
435
- }
436
- /** Drop the persisted awaiting-row once confirmed (never rejects). */
437
- async removeAwaitingTransaction(txId) {
438
- if (!this.database)
439
- return;
440
- try {
441
- await this.database.removeTransaction(`awaiting_${txId}`);
442
- }
443
- catch {
444
- // Ignore — might not have been persisted
445
- }
446
- }
447
- /**
448
- * Initialize sync client with authentication
449
- */
450
- async initialize(userId, organizationId) {
451
- this.userId = userId;
452
- this.organizationId = organizationId;
453
- this.runtime.observability.setContext(userId, organizationId);
454
- this.mutationQueue.setCommitOutboxScope({
455
- organizationId,
456
- participantId: userId,
457
- namespace: this.commitOutboxNamespace,
458
- });
459
- // Calls made during startup are allowed to queue before identity arrives,
460
- // but they cannot be serialized with a trustworthy scope until now. Flush
461
- // those already-created journal promises before any write can be staged.
462
- if (this.pendingJournalBatch.length > 0) {
463
- this.scheduleJournalFlush();
464
- await Promise.all(this.pendingMutations.map((mutation) => mutation.journaled));
465
- }
466
- // Restore exact, already-sealed requests first. The returned source ids
467
- // suppress any legacy queue entry left behind by an older non-atomic
468
- // handoff.
469
- const sealedMutationIds = await this.mutationQueue.restoreDurableCommits();
470
- await this.restoreMutationQueue(sealedMutationIds);
471
- // Read the initial network status from the injected OnlineStatusProvider.
472
- // In the browser this reflects the host's connectivity signal; in Node it
473
- // reports online by default. NetworkMonitor drives the ongoing
474
- // online/offline transitions below — this read is only the initial
475
- // snapshot taken when identity is set.
476
- if (this.runtime.onlineStatus.isOnline()) {
477
- this.setConnectionState('connected');
478
- }
479
- else {
480
- // Offline - start in offline mode
481
- this.setConnectionState('disconnected');
482
- this.emit('sync:offline');
483
- }
484
- if (this.pendingMutations.length > 0)
485
- this.scheduleSync();
486
- }
487
- /**
488
- * The organization this client writes under (set by `initialize`).
489
- * Read by the model proxy so `create()` defaults `organizationId` the
490
- * same way the mutator path does — `null` until identity is wired.
491
- */
492
- getOrganizationId() {
493
- return this.organizationId;
494
- }
495
- /**
496
- * Self-healing helper for individual model records.
497
- *
498
- * Two registry-driven repair passes run on every row hydrated from
499
- * IndexedDB or merged from a delta:
500
- *
501
- * 1. **Auto-fill** — for each `autoFill` rule the consumer's schema
502
- * declares on this model, copy the corresponding identity value
503
- * (`organizationId` / `userId`) onto the row when it's missing.
504
- * Repairs rows from a past version that didn't write the field.
505
- *
506
- * 2. **Required-field gate** — if the row is missing any field listed
507
- * in the model's `requiredFields`, return `null` so the caller
508
- * skips this record. Used for FK columns whose absence renders the
509
- * row unrecoverable (e.g. a Block with no sectionId).
510
- *
511
- * The engine itself is product-neutral: model identity (which fields
512
- * to back-fill, which absences are fatal) lives entirely in the
513
- * consumer schema.
514
- */
515
- healModelRecord(modelType, data) {
516
- const meta = this.objectPool.registry.getMetadata(modelType);
517
- if (!meta)
518
- return { data, healed: false };
519
- const idPrefix = data.id?.slice(0, 8) ?? 'unknown';
520
- let result = data;
521
- let healed = false;
522
- if (meta.autoFill) {
523
- for (const rule of meta.autoFill) {
524
- if (result[rule.field])
525
- continue;
526
- const replacement = rule.from === 'organizationId' ? this.organizationId : this.userId;
527
- if (!replacement)
528
- continue;
529
- this.runtime.observability.captureSelfHealing({
530
- modelName: modelType,
531
- modelId: idPrefix,
532
- field: rule.field,
533
- action: `added missing ${rule.field}`,
534
- });
535
- result = { ...result, [rule.field]: replacement };
536
- healed = true;
537
- }
538
- }
539
- if (meta.requiredFields) {
540
- for (const field of meta.requiredFields) {
541
- if (result[field])
542
- continue;
543
- this.runtime.observability.captureSelfHealing({
544
- modelName: modelType,
545
- modelId: idPrefix,
546
- field,
547
- action: `skipped corrupted ${modelType} - missing ${field}`,
548
- });
549
- return null;
550
- }
551
- }
552
- return { data: result, healed };
553
- }
554
- /**
555
- * Hydrate InstanceCache with data from Database
556
- * Called after bootstrap is complete
557
- */
558
- async hydrateFromDatabase() {
559
- if (!this.database) {
560
- throw new AbloValidationError('Database not available for hydration', {
561
- code: 'sync_client_db_missing',
562
- });
563
- }
564
- // Get model types that should be hydrated on startup (skip lazy per LSE)
565
- const modelTypes = this.objectPool.registry.getRegisteredModelNames().filter((name) => {
566
- const meta = this.objectPool.registry.getMetadata(name);
567
- return meta?.loadStrategy === LoadStrategy.instant;
568
- });
569
- const totalStart = typeof performance !== 'undefined' ? performance.now() : Date.now();
570
- // Phase 1: Fetch all data from IndexedDB and create model instances (async I/O).
571
- // We collect all models across ALL types before touching MobX, so that Phase 2
572
- // can add them in a single addBatch() call → ONE MobX action → ONE re-render.
573
- const allModelsToAdd = [];
574
- const perTypePerfLogs = [];
575
- for (const modelType of modelTypes) {
576
- const typeStart = typeof performance !== 'undefined' ? performance.now() : Date.now();
577
- try {
578
- // Get raw data from Database (via StoreManager)
579
- const rawData = await this.database.hydrateModels(modelType);
580
- const afterFetch = typeof performance !== 'undefined' ? performance.now() : Date.now();
581
- // Create models in batch first, collect for deferred addBatch
582
- const modelsForType = [];
583
- const recordsToHeal = [];
584
- for (const data of rawData) {
585
- let withType = data && typeof data === 'object' && !data.__typename
586
- ? { __typename: modelType, ...data }
587
- : data;
588
- // Self-healing: Fix corrupted IndexedDB records missing essential fields
589
- const healResult = this.healModelRecord(modelType, withType);
590
- if (healResult === null) {
591
- continue; // Record is corrupted beyond repair — skip
592
- }
593
- withType = healResult.data;
594
- if (healResult.healed) {
595
- recordsToHeal.push({ id: healResult.data.id, data: healResult.data });
596
- }
597
- const model = this.objectPool.createFromData(withType);
598
- if (model) {
599
- modelsForType.push(model);
600
- }
601
- }
602
- // Collect models for the single batched addBatch call in Phase 2
603
- allModelsToAdd.push(...modelsForType);
604
- // Persist healed records back to IndexedDB (fire-and-forget, non-blocking)
605
- if (recordsToHeal.length > 0 && this.database) {
606
- this.runtime.logger.info(`[SyncClient.hydrate] Persisting ${recordsToHeal.length} healed ${modelType} records to IndexedDB`);
607
- // Use fire-and-forget to not block hydration.
608
- // void is safe: the handler's body is fully try/catch'd.
609
- void Promise.resolve().then(async () => {
610
- try {
611
- for (const { id, data } of recordsToHeal) {
612
- await this.database.putRecord(modelType, id, data);
613
- }
614
- this.runtime.logger.info(`[SyncClient.hydrate] Successfully healed ${recordsToHeal.length} ${modelType} records`);
615
- }
616
- catch (err) {
617
- this.runtime.observability.captureMutationFailure({
618
- context: 'persist-healed-records',
619
- modelName: modelType,
620
- error: err instanceof Error ? err : new Error(String(err)),
621
- });
622
- }
623
- });
624
- }
625
- const typeEnd = typeof performance !== 'undefined' ? performance.now() : Date.now();
626
- perTypePerfLogs.push({
627
- type: modelType,
628
- fetched: rawData.length,
629
- added: modelsForType.length,
630
- fetchMs: (afterFetch - typeStart).toFixed(2),
631
- createMs: (typeEnd - afterFetch).toFixed(2),
632
- });
633
- }
634
- catch (error) {
635
- this.runtime.observability.captureBootstrapFailure(error, { type: `hydrate-${modelType}` });
636
- }
637
- }
638
- // Phase 2: Single MobX action — add ALL models across all types at once.
639
- const addStart = typeof performance !== 'undefined' ? performance.now() : Date.now();
640
- const totalAdded = this.objectPool.addBatch(allModelsToAdd, ModelScope.live);
641
- const addEnd = typeof performance !== 'undefined' ? performance.now() : Date.now();
642
- // Log per-type perf after the batched add (so logs still show per-type breakdown)
643
- for (const entry of perTypePerfLogs) {
644
- this.runtime.logger.debug('hydrate:type', parseFloat(entry.fetchMs) + parseFloat(entry.createMs), {
645
- type: entry.type,
646
- fetched: entry.fetched,
647
- added: entry.added,
648
- fetchMs: entry.fetchMs,
649
- createMs: entry.createMs,
650
- });
651
- }
652
- const totalEnd = typeof performance !== 'undefined' ? performance.now() : Date.now();
653
- this.runtime.logger.debug('hydrate:total', totalEnd - totalStart, {
654
- totalModels: totalAdded,
655
- addBatchMs: (addEnd - addStart).toFixed(2),
656
- });
657
- // One-line startup summary: types pre-seeded and items per type
658
- try {
659
- const preseededTypes = this.objectPool.registry.getRegisteredModelNames();
660
- const stats = this.objectPool.getStats();
661
- this.runtime.logger.info('startup_summary', {
662
- typesPreseeded: preseededTypes.length,
663
- poolSize: stats.size,
664
- typeCounts: stats.typeCounts,
665
- });
666
- }
667
- catch { }
668
- }
669
- /**
670
- * Re-hydrate InstanceCache from IndexedDB when the pool already has data.
671
- *
672
- * Unlike hydrateFromDatabase() (which uses addBatch and skips existing IDs),
673
- * this method properly:
674
- * 1. Upserts models — updates existing models in-place, adds new ones
675
- * 2. Removes ghosts — deletes models from the pool that no longer exist in IndexedDB
676
- *
677
- * Used by background bootstrap, network recovery, and server-triggered re-bootstrap.
678
- */
679
- async rehydrateFromDatabase() {
680
- if (!this.database) {
681
- throw new AbloValidationError('Database not available for rehydration', {
682
- code: 'sync_client_db_missing',
683
- });
684
- }
685
- const totalStart = typeof performance !== 'undefined' ? performance.now() : Date.now();
686
- // Model types to rehydrate (same filter as hydrateFromDatabase)
687
- const modelTypes = this.objectPool.registry.getRegisteredModelNames().filter((name) => {
688
- const meta = this.objectPool.registry.getMetadata(name);
689
- return meta?.loadStrategy === LoadStrategy.instant;
690
- });
691
- // ── Phase 1: Read from IndexedDB & create model instances (async I/O) ──
692
- const allModels = [];
693
- const idbIdsByType = new Map();
694
- let healedCount = 0;
695
- let skippedCount = 0;
696
- for (const modelType of modelTypes) {
697
- try {
698
- const rawData = await this.database.hydrateModels(modelType);
699
- const idsForType = new Set();
700
- idbIdsByType.set(modelType, idsForType);
701
- for (const data of rawData) {
702
- let withType = data && typeof data === 'object' && !data.__typename
703
- ? { __typename: modelType, ...data }
704
- : data;
705
- // Self-healing
706
- const healResult = this.healModelRecord(modelType, withType);
707
- if (healResult === null) {
708
- skippedCount++;
709
- continue;
710
- }
711
- withType = healResult.data;
712
- if (healResult.healed) {
713
- healedCount++;
714
- // Persist heal back to IndexedDB (fire-and-forget)
715
- if (this.database) {
716
- const id = healResult.data.id;
717
- const healedData = healResult.data;
718
- // void is safe: the handler's body is fully try/catch'd.
719
- void Promise.resolve().then(async () => {
720
- try {
721
- await this.database.putRecord(modelType, id, healedData);
722
- }
723
- catch {
724
- // Non-critical — will heal again next time
725
- }
726
- });
727
- }
728
- }
729
- // Register ID before createFromData — prevents ghost removal
730
- // if createFromData fails for a record that exists in IDB
731
- const recordId = withType.id;
732
- if (recordId) {
733
- idsForType.add(recordId);
734
- }
735
- try {
736
- const model = this.objectPool.createFromData(withType);
737
- if (model) {
738
- allModels.push(model);
739
- }
740
- }
741
- catch (error) {
742
- this.runtime.observability.breadcrumb('Model creation failed during rehydration', 'sync.bootstrap', 'warning', {
743
- modelType,
744
- modelId: recordId?.slice(0, 8) ?? 'unknown',
745
- error: error instanceof Error ? error.message : String(error),
746
- });
747
- skippedCount++;
748
- }
749
- }
750
- }
751
- catch (error) {
752
- this.runtime.observability.captureBootstrapFailure(error, { type: `rehydrate-${modelType}` });
753
- }
754
- }
755
- // ── Phase 2: Upsert batch (single MobX action) ──
756
- // createFromData already calls updateFromData() on existing models,
757
- // so existing models are up-to-date. Upsert adds the new ones and
758
- // updates scope for any that changed.
759
- const beforeSize = this.objectPool.size;
760
- this.objectPool.upsertBatch(allModels, ModelScope.live);
761
- const addedCount = this.objectPool.size - beforeSize;
762
- const updatedCount = allModels.length - addedCount;
763
- // ── Phase 3: Reconcile ghost deletions (single MobX action) ──
764
- // Only reconcile types that were rehydrated — never touch lazy-loaded types.
765
- const ghostIds = [];
766
- for (const modelType of modelTypes) {
767
- const idbIds = idbIdsByType.get(modelType);
768
- if (!idbIds)
769
- continue; // Type had an error during fetch — don't reconcile
770
- const poolIds = this.objectPool.getIdsByModelType(modelType);
771
- if (!poolIds)
772
- continue;
773
- for (const poolId of poolIds) {
774
- if (!idbIds.has(poolId)) {
775
- ghostIds.push(poolId);
776
- }
777
- }
778
- }
779
- const removedCount = this.objectPool.removeBatch(ghostIds);
780
- // ── Phase 4: Stats & logging ──
781
- const totalEnd = typeof performance !== 'undefined' ? performance.now() : Date.now();
782
- const elapsedMs = Math.round(totalEnd - totalStart);
783
- const stats = {
784
- added: addedCount,
785
- updated: updatedCount,
786
- removed: removedCount,
787
- skipped: skippedCount,
788
- healed: healedCount,
789
- elapsedMs,
790
- };
791
- this.runtime.logger.info('[SyncClient.rehydrate] Complete', {
792
- ...stats,
793
- poolSize: this.objectPool.size,
794
- ghostIds: ghostIds.length > 0 ? ghostIds.slice(0, 5).map((id) => id.slice(0, 8)) : [],
795
- });
796
- this.runtime.observability.breadcrumb('Rehydration complete', 'sync.bootstrap', 'info', {
797
- added: stats.added,
798
- updated: stats.updated,
799
- removed: stats.removed,
800
- elapsedMs: stats.elapsedMs,
801
- });
802
- return stats;
803
- }
804
- /**
805
- * Apply a mutation to a model optimistically and queue it for server sync.
806
- * IndexedDB is updated only once the server confirms the change with a delta
807
- * packet.
808
- *
809
- * A model's changes are captured before the pool action runs, because a pool
810
- * operation such as an upsert can clear the model's local change set;
811
- * capturing first ensures those changes are never lost. The captured set is
812
- * frozen and handed to {@link queueMutation}.
813
- */
814
- mutate(type, model, poolAction, writeOptions) {
815
- // No-op UPDATE guard (O(1)). An update with no dirty fields would travel
816
- // to the server, get dropped by `coalesceOperations` Rule 4 (empty input),
817
- // and — if it was the only op — come back as `lastSyncId: 0`. That trips
818
- // `captureCommitZeroSyncId` (false-positive Sentry anomaly) AND parks the
819
- // tx in `awaiting_delta` for a 30s reconciliation timeout on a write that
820
- // changed nothing. `Model.hasChanges` reads `modifiedProperties.size`, so
821
- // this costs O(1) with no allocation (vs. O(N) materializing getChanges()).
822
- //
823
- // Strict `=== false` is deliberate: `rowAsModel` only casts, so a non-Model
824
- // object can reach here with `hasChanges === undefined`. `undefined === false`
825
- // is false → we fall through to the normal path rather than risk dropping a
826
- // real write. Only a genuine Model with an empty dirty-set is skipped.
827
- if (type === 'update' && model.hasChanges === false) {
828
- return;
829
- }
830
- // Capture changes before the pool action runs. Pool operations —
831
- // upsert in particular — can clear the model's local changes, so
832
- // capturing first ensures they are never lost.
833
- const capturedChanges = type === 'update' || type === 'create' ? this.captureModelChanges(model) : undefined;
834
- poolAction();
835
- this.queueMutation({ type, model, timestamp: new Date(), capturedChanges, writeOptions });
836
- this.notifyObservers({
837
- type,
838
- modelType: model.getModelName(),
839
- model: type !== 'delete' ? model : undefined,
840
- modelId: model.id,
841
- });
842
- // QueryProcessor uses `models:changed` to invalidate caches. Coalesce
843
- // to one event per microtask: a paste of 100 rows should re-run
844
- // affected queries ONCE, not 100×.
845
- this.markModelChanged(model.getModelName());
846
- }
847
- pendingChangedTypes = null;
848
- markModelChanged(modelType) {
849
- if (!this.pendingChangedTypes) {
850
- this.pendingChangedTypes = new Set();
851
- const schedule = typeof queueMicrotask === 'function'
852
- ? queueMicrotask
853
- : (cb) => Promise.resolve().then(cb);
854
- schedule(() => {
855
- const types = this.pendingChangedTypes;
856
- this.pendingChangedTypes = null;
857
- if (types && types.size > 0)
858
- this.emit('models:changed', types);
859
- });
860
- }
861
- this.pendingChangedTypes.add(modelType);
862
- }
863
- /**
864
- * Capture model changes immutably BEFORE any pool operations
865
- * This prevents the fragile pattern of reading changes after state modification
866
- */
867
- captureModelChanges(model) {
868
- if (typeof model.getChanges !== 'function')
869
- return undefined;
870
- const changes = model.getChanges();
871
- // Return a frozen copy to prevent accidental modification
872
- return Object.keys(changes).length > 0 ? Object.freeze({ ...changes }) : undefined;
873
- }
874
- /** Add new model (CREATE) - works offline */
875
- add(model, options) {
876
- this.mutate('create', model, () => { this.objectPool.add(model, ModelScope.live); }, options);
877
- }
878
- /** Update existing model (UPDATE) - works offline */
879
- update(model, options) {
880
- this.mutate('update', model, () => { this.objectPool.upsert(model, ModelScope.live); }, options);
881
- }
882
- /**
883
- * Update existing model with pre-computed changes.
884
- * Used by saveManyOptimized when incoming models have empty change-tracking
885
- * (e.g. freshly constructed cell models from a bulk document decomposition).
886
- */
887
- updateWithChanges(model, changes) {
888
- this.runtime.logger.debug(`SyncClient.updateWithChanges`, {
889
- modelId: model.id,
890
- modelType: model.getModelName(),
891
- });
892
- // Use pre-computed changes if provided, otherwise fall back to model.getChanges()
893
- const capturedChanges = changes && Object.keys(changes).length > 0
894
- ? Object.freeze({ ...changes })
895
- : this.captureModelChanges(model);
896
- // No-op UPDATE guard: neither an explicit change set nor model dirty-fields.
897
- // `captureModelChanges` already returns undefined for an empty dirty-set, so
898
- // an undefined here means there is genuinely nothing to send — skip rather
899
- // than emit an empty-input update that the server coalesces to lastSyncId 0
900
- // (see the same guard in `mutate`).
901
- if (capturedChanges === undefined)
902
- return;
903
- this.objectPool.upsert(model, ModelScope.live);
904
- this.queueMutation({ type: 'update', model, timestamp: new Date(), capturedChanges });
905
- this.notifyObservers({
906
- type: 'update',
907
- modelType: model.getModelName(),
908
- model,
909
- modelId: model.id,
910
- });
911
- }
912
- /** Expose the GraphQL client for atomic mutations (e.g., createSectionWithBlocks).
913
- * Used by SyncedStore for operations that bypass the transaction queue
914
- * but still need optimistic pool updates at the sync layer. */
915
- get gql() {
916
- return this.mutationExecutor;
917
- }
918
- /** Delete model (DELETE) - works offline */
919
- delete(model, options) {
920
- // Clear pending mutations first to prevent "not found" errors on fast delete
921
- this.clearPendingMutationsForModel(model.id);
922
- this.mutate('delete', model, () => this.objectPool.remove(model.id), options);
923
- }
924
- /**
925
- * Clear all pending mutations for a specific model
926
- * Called before deletion to prevent "record not found" errors on the server
927
- */
928
- clearPendingMutationsForModel(modelId) {
929
- const beforeCount = this.pendingMutations.length;
930
- const removed = this.pendingMutations.filter((mutation) => mutation.model.id === modelId);
931
- this.pendingMutations = this.pendingMutations.filter((m) => m.model.id !== modelId);
932
- const afterCount = this.pendingMutations.length;
933
- if (beforeCount !== afterCount) {
934
- this.runtime.logger.debug('[SyncClient.clearPendingMutationsForModel] Cleared pending mutations', {
935
- modelId,
936
- clearedCount: beforeCount - afterCount,
937
- remainingCount: afterCount,
938
- });
939
- for (const mutation of removed) {
940
- // Once staged, MutationQueue owns cancellation and transfers this
941
- // source id into the superseding delete envelope. Deleting the journal
942
- // row here would make that valid atomic promotion look like a
943
- // multi-tab loser. Truly unstaged work can be canceled locally.
944
- if (this.stagedMutationIds.has(mutation.mutationId))
945
- continue;
946
- this.stagedMutationIds.delete(mutation.mutationId);
947
- void mutation.journaled
948
- .then(() => this.database.removeTransaction(pendingMutationRecordId(mutation.mutationId)))
949
- .catch(() => undefined);
950
- }
951
- }
952
- }
953
- /**
954
- * Upload a file and create its attachment record. The upload runs through
955
- * the {@link MutationQueue}, and a model is built from the server's
956
- * response and added to the pool.
957
- */
958
- async uploadFile(file, options) {
959
- if (!this.userId || !this.organizationId) {
960
- throw new AbloAuthenticationError('Authentication required for file uploads', {
961
- code: 'file_upload_auth_required',
962
- });
963
- }
964
- try {
965
- // Use MutationQueue to handle the upload mutation
966
- const result = await this.mutationQueue.uploadAttachment(file, {
967
- id: options.id,
968
- attachableType: options.attachableType,
969
- attachableId: options.attachableId,
970
- metadata: options.metadata,
971
- }, {
972
- userId: this.userId,
973
- organizationId: this.organizationId,
974
- });
975
- if (result) {
976
- // Create model from response using ModelRegistry (generic — no concrete class import)
977
- const model = this.objectPool.createFromData({
978
- id: options.id,
979
- ...result,
980
- });
981
- if (model) {
982
- this.objectPool.add(model, ModelScope.live);
983
- this.notifyObservers({
984
- type: 'create',
985
- modelType: model.getModelName(),
986
- model,
987
- });
988
- return model;
989
- }
990
- }
991
- return null;
992
- }
993
- catch (error) {
994
- this.runtime.observability.captureMutationFailure({
995
- context: 'file-upload',
996
- error: error instanceof Error ? error : new Error(String(error)),
997
- });
998
- throw error;
999
- }
1000
- }
1001
- /**
1002
- * Batch upload files — single GraphQL call + parallel S3 PUTs.
1003
- *
1004
- * Returns the raw `Model[]` built by the object pool (typename is
1005
- * determined by the payload the server returns — currently always
1006
- * `Attachment`). The SDK has no knowledge of app-specific model classes,
1007
- * so it cannot honestly claim a narrower return type; consumers that
1008
- * need an `Attachment[]` project through their own typed accessor
1009
- * (e.g. `store.query.attachments.findMany({ where: { id: IN ids } })`)
1010
- * after the upload resolves.
1011
- */
1012
- async batchUploadFiles(files, options) {
1013
- if (!this.userId || !this.organizationId) {
1014
- throw new AbloAuthenticationError('Authentication required for file uploads', {
1015
- code: 'file_upload_auth_required',
1016
- });
1017
- }
1018
- const items = options.ids.map((id) => ({
1019
- id,
1020
- attachableType: options.attachableType,
1021
- attachableId: options.attachableId,
1022
- metadata: options.metadata,
1023
- }));
1024
- const results = await this.mutationQueue.batchUploadAttachments(files, items, {
1025
- userId: this.userId,
1026
- organizationId: this.organizationId,
1027
- });
1028
- const models = [];
1029
- for (const result of results) {
1030
- const model = this.objectPool.createFromData({ ...result });
1031
- if (model) {
1032
- this.objectPool.add(model, ModelScope.live);
1033
- this.notifyObservers({
1034
- type: 'create',
1035
- modelType: model.getModelName(),
1036
- model,
1037
- });
1038
- models.push(model);
1039
- }
1040
- }
1041
- return models;
1042
- }
1043
- /** Archive model (ARCHIVE) - works offline */
1044
- archive(model) {
1045
- this.mutate('archive', model, () => { this.objectPool.updateScope(model.id, ModelScope.archived); });
1046
- }
1047
- /**
1048
- * Append a mutation to the pending queue and schedule its sync work.
1049
- *
1050
- * IndexedDB persistence and the server push are deferred to a microtask, so
1051
- * many pushes within the same tick collapse into a single serialization and
1052
- * a single process call. Without the deferral, queueing a hundred mutations
1053
- * at once — a large paste, a document import, bulk row creation — would
1054
- * reserialize the whole growing queue a hundred times, an O(N²) cost in
1055
- * `model.toJSON()`.
1056
- *
1057
- * @param mutation.capturedChanges - Pre-captured, frozen changes, used to
1058
- * avoid re-reading a model after pool operations that might clear them.
1059
- */
1060
- queueMutation(mutation) {
1061
- const mutationId = `mutation_${uuid()}`;
1062
- const modelData = mutation.model.toJSON
1063
- ? mutation.model.toJSON()
1064
- : { ...mutation.model };
1065
- let resolveJournal;
1066
- let rejectJournal;
1067
- const journaled = new Promise((resolve, reject) => {
1068
- resolveJournal = resolve;
1069
- rejectJournal = reject;
1070
- });
1071
- let resolveStaged;
1072
- let rejectStaged;
1073
- const staged = new Promise((resolve, reject) => {
1074
- resolveStaged = resolve;
1075
- rejectStaged = reject;
1076
- });
1077
- const pending = {
1078
- ...mutation,
1079
- mutationId,
1080
- modelData,
1081
- journaled,
1082
- resolveJournal,
1083
- rejectJournal,
1084
- staged,
1085
- resolveStaged,
1086
- rejectStaged,
1087
- };
1088
- this.pendingJournalBatch.push(pending);
1089
- this.scheduleJournalFlush();
1090
- // Offline drains may not await this until much later. Observe rejection
1091
- // immediately to avoid an unhandled-promise report while retaining the
1092
- // original rejecting promise for fail-closed dispatch.
1093
- void pending.journaled.catch(() => undefined);
1094
- void pending.staged.catch(() => undefined);
1095
- this.pendingMutations.push(pending);
1096
- this.scheduleSync();
1097
- }
1098
- scheduleJournalFlush() {
1099
- if (this.journalFlushScheduled)
1100
- return;
1101
- this.journalFlushScheduled = true;
1102
- const schedule = typeof queueMicrotask === 'function'
1103
- ? queueMicrotask
1104
- : (callback) => { void Promise.resolve().then(callback); };
1105
- schedule(() => {
1106
- this.journalFlushScheduled = false;
1107
- const batch = this.pendingJournalBatch;
1108
- this.pendingJournalBatch = [];
1109
- void this.flushPendingMutationJournal(batch);
1110
- });
1111
- }
1112
- async flushPendingMutationJournal(batch) {
1113
- if (batch.length === 0)
1114
- return;
1115
- if (!this.userId || !this.organizationId) {
1116
- // Startup writes remain behind their unresolved journal promise. Identity
1117
- // initialization re-kicks this batch once its durable scope is known.
1118
- this.pendingJournalBatch = [...batch, ...this.pendingJournalBatch];
1119
- return;
1120
- }
1121
- try {
1122
- // Build each durable row independently. A malformed mutation must not
1123
- // prevent unrelated writes from the same event-loop burst from being
1124
- // journaled and dispatched.
1125
- const entries = [];
1126
- for (const mutation of batch) {
1127
- try {
1128
- entries.push({
1129
- mutation,
1130
- record: this.pendingMutationRecord(mutation),
1131
- });
1132
- }
1133
- catch (error) {
1134
- mutation.rejectJournal?.(error);
1135
- }
1136
- }
1137
- if (entries.length === 0)
1138
- return;
1139
- const database = this.database;
1140
- const saveOne = async (record) => {
1141
- // Some adapters (including IndexedDB's put path) can throw before
1142
- // returning a Promise. The async wrapper turns that into an individual
1143
- // rejected outcome instead of aborting construction of the whole list.
1144
- await database.saveTransaction(record);
1145
- };
1146
- let outcomes;
1147
- if (database.saveTransactions) {
1148
- try {
1149
- await database.saveTransactions(entries.map(({ record }) => record));
1150
- outcomes = entries.map(() => ({
1151
- status: 'fulfilled',
1152
- value: undefined,
1153
- }));
1154
- }
1155
- catch {
1156
- // IndexedDB aborts a multi-row transaction when any value cannot be
1157
- // cloned. Retry one row per transaction to identify the offender and
1158
- // preserve every valid sibling. Re-putting a row is idempotent by id.
1159
- outcomes = await Promise.allSettled(entries.map(({ record }) => saveOne(record)));
1160
- }
1161
- }
1162
- else {
1163
- outcomes = await Promise.allSettled(entries.map(({ record }) => saveOne(record)));
1164
- }
1165
- for (const [index, entry] of entries.entries()) {
1166
- const outcome = outcomes[index];
1167
- if (outcome?.status === 'fulfilled') {
1168
- entry.mutation.resolveJournal?.();
1169
- }
1170
- else {
1171
- entry.mutation.rejectJournal?.(outcome?.reason ??
1172
- new Error('Mutation journal persistence failed'));
1173
- }
1174
- }
1175
- }
1176
- finally {
1177
- for (const mutation of batch) {
1178
- mutation.resolveJournal = undefined;
1179
- mutation.rejectJournal = undefined;
1180
- }
1181
- }
1182
- }
1183
- syncScheduled = false;
1184
- scheduleSync() {
1185
- if (this.syncScheduled)
1186
- return;
1187
- this.syncScheduled = true;
1188
- const schedule = typeof queueMicrotask === 'function'
1189
- ? queueMicrotask
1190
- : (cb) => Promise.resolve().then(cb);
1191
- schedule(() => {
1192
- this.syncScheduled = false;
1193
- if (this.runtime.onlineStatus.isOnline()) {
1194
- this.processPendingMutations().catch((err) => {
1195
- this.runtime.observability.breadcrumb('Background sync failed', 'sync.transaction', 'warning', { error: err instanceof Error ? err.message : String(err) });
1196
- });
1197
- }
1198
- });
1199
- }
1200
- pendingMutationRecord(mutation) {
1201
- if (!this.userId || !this.organizationId) {
1202
- throw new AbloValidationError('Cannot persist a mutation before participant scope is initialized', { code: 'write_options_invalid' });
1203
- }
1204
- const candidate = {
1205
- id: pendingMutationRecordId(mutation.mutationId),
1206
- type: 'pending_mutation',
1207
- storageVersion: 2,
1208
- mutation: {
1209
- mutationId: mutation.mutationId,
1210
- type: mutation.type,
1211
- modelData: mutation.modelData,
1212
- modelName: mutation.model.getModelName(),
1213
- timestamp: mutation.timestamp.toISOString(),
1214
- ...(mutation.capturedChanges !== undefined
1215
- ? { capturedChanges: mutation.capturedChanges }
1216
- : {}),
1217
- ...(mutation.writeOptions !== undefined
1218
- ? { writeOptions: mutation.writeOptions }
1219
- : {}),
1220
- },
1221
- scope: {
1222
- organizationId: this.organizationId,
1223
- participantId: this.userId,
1224
- namespace: this.commitOutboxNamespace,
1225
- },
1226
- timestamp: mutation.timestamp.getTime(),
1227
- };
1228
- // One framework-neutral boundary contract owns both Proxy unwrapping and
1229
- // rejection of values JSON would silently corrupt before IndexedDB sees it.
1230
- return pendingMutationRecordSchema.parse(snapshotJsonValue(candidate, '$.pendingMutation'));
1231
- }
1232
- async persistPendingMutation(mutation) {
1233
- await this.database.saveTransaction(this.pendingMutationRecord(mutation));
1234
- }
1235
- /**
1236
- * Restore the mutation queue from IndexedDB.
1237
- *
1238
- * The persisted record was written by an earlier session, possibly by an
1239
- * older build of the SDK, so each entry is validated as it is replayed:
1240
- * corrupt entries are dropped and logged at debug level, and a failure is
1241
- * never swallowed silently, because the survival of offline writes must be
1242
- * observable.
1243
- */
1244
- async restoreMutationQueue(sealedMutationIds = new Set()) {
1245
- if (!this.database || !this.userId)
1246
- return;
1247
- try {
1248
- const stored = await this.database.getPersistedTransactions();
1249
- const restoredMutationIds = new Set();
1250
- let heldForReview = 0;
1251
- const restore = async (mutation, migrateLegacy, legacyMutationId) => {
1252
- const parsed = persistedMutationSchema.safeParse(mutation);
1253
- if (!parsed.success) {
1254
- this.runtime.logger.debug('[SyncClient] Dropping malformed persisted mutation', {
1255
- issues: parsed.error.issues.map((i) => i.path.join('.')).join(', '),
1256
- });
1257
- return;
1258
- }
1259
- // The window is anchored to when the write was made, because a
1260
- // record re-sealed on restore would otherwise reset its own expiry
1261
- // clock. An unparseable timestamp is held rather than replayed.
1262
- const writtenAt = Date.parse(parsed.data.timestamp);
1263
- const age = Date.now() - writtenAt;
1264
- if (!(age < PENDING_MUTATION_REPLAY_WINDOW_MS)) {
1265
- heldForReview += 1;
1266
- this.runtime.logger.warn('A saved local write is older than the server idempotency window and was held for review.');
1267
- return;
1268
- }
1269
- const mutationId = parsed.data.mutationId ?? legacyMutationId ?? `mutation_${uuid()}`;
1270
- if (sealedMutationIds.has(mutationId) ||
1271
- restoredMutationIds.has(mutationId))
1272
- return;
1273
- const model = this.objectPool.createFromData(parsed.data.modelData);
1274
- if (model) {
1275
- const pending = {
1276
- mutationId,
1277
- type: parsed.data.type,
1278
- model,
1279
- modelData: parsed.data.modelData,
1280
- timestamp: new Date(parsed.data.timestamp),
1281
- ...(parsed.data.capturedChanges !== undefined
1282
- ? { capturedChanges: parsed.data.capturedChanges }
1283
- : {}),
1284
- ...(parsed.data.writeOptions !== undefined
1285
- ? { writeOptions: parsed.data.writeOptions }
1286
- : {}),
1287
- journaled: Promise.resolve(),
1288
- // Restored mutations have no live `wait: 'confirmed'` caller, so
1289
- // their staging needs no waiter handshake.
1290
- staged: Promise.resolve(),
1291
- };
1292
- if (migrateLegacy) {
1293
- pending.journaled = this.persistPendingMutation(pending);
1294
- await pending.journaled;
1295
- }
1296
- this.pendingMutations.push(pending);
1297
- restoredMutationIds.add(mutationId);
1298
- }
1299
- };
1300
- for (const row of stored) {
1301
- if (row.type !== 'pending_mutation')
1302
- continue;
1303
- const parsed = pendingMutationRecordSchema.safeParse(row);
1304
- if (!parsed.success) {
1305
- const legacy = legacyPendingMutationRecordSchema.safeParse(row);
1306
- if (legacy.success) {
1307
- await restore(legacy.data.mutation, true);
1308
- continue;
1309
- }
1310
- this.runtime.logger.debug('[SyncClient] Dropping malformed pending mutation record', {
1311
- rowId: row.id,
1312
- });
1313
- continue;
1314
- }
1315
- if (parsed.data.scope.organizationId !== this.organizationId ||
1316
- parsed.data.scope.participantId !== this.userId ||
1317
- parsed.data.scope.namespace !== this.commitOutboxNamespace) {
1318
- this.runtime.logger.warn('A saved local write belongs to a different account or server and was held for review.');
1319
- continue;
1320
- }
1321
- await restore(parsed.data.mutation, false);
1322
- }
1323
- const legacyQueue = stored.find((row) => row.id === 'mutation-queue');
1324
- if (legacyQueue?.mutations) {
1325
- const heldBefore = heldForReview;
1326
- for (const [index, mutation] of legacyQueue.mutations.entries()) {
1327
- await restore(mutation, true, `legacy_mutation_${index}`);
1328
- }
1329
- // Deleting the legacy row would discard any entry held for review, so
1330
- // it is only removed once every entry has migrated.
1331
- if (heldForReview === heldBefore) {
1332
- await this.database.removeTransaction('mutation-queue');
1333
- }
1334
- }
1335
- }
1336
- catch (error) {
1337
- // A restore failure means queued offline writes did NOT rehydrate.
1338
- // Self-healing is impossible here (the record may be unreadable), but
1339
- // the failure must be visible for diagnosis instead of silent loss.
1340
- this.runtime.logger.debug('[SyncClient] Failed to restore offline mutation queue', {
1341
- error: error instanceof Error ? error.message : String(error),
1342
- });
1343
- this.runtime.observability.captureMutationFailure({
1344
- context: 'restore-mutation-queue',
1345
- error: error instanceof Error ? error : String(error),
1346
- });
1347
- }
1348
- }
1349
- /**
1350
- * Process pending mutations - can be called by SyncedStore when online
1351
- *
1352
- * Best Practice: Only sync models that still exist locally (local-first principle)
1353
- * - If a model was deleted locally → skip any pending updates/creates for it
1354
- * - This prevents "record not found" errors from fast copy-paste-delete workflows
1355
- */
1356
- async processPendingMutations() {
1357
- if (this.pendingMutations.length === 0)
1358
- return;
1359
- // Identity guard. The early returns here used to be silent — the bug
1360
- // pattern was "every mutation from a logged-in user evaporates" when
1361
- // `SyncClient.initialize()` wasn't called (e.g., missing wiring in
1362
- // the consumer's `BaseSyncedStore.initialize` generator). Warn so
1363
- // this class of misconfiguration surfaces in dev instead of
1364
- // manifesting as "my drag doesn't save."
1365
- if (!this.userId || !this.organizationId) {
1366
- // Internal invariant, not a consumer-actionable error: identity (user +
1367
- // org) hasn't arrived yet. The mutations stay queued and retry once it
1368
- // does, so this is `debug` — a transient startup race is normal. If it
1369
- // never clears it means the host app finished sign-in without seeding
1370
- // identity, which surfaces downstream as "writes never confirm"; we do
1371
- // NOT name internal wiring (`SyncClient.initialize`) here because that
1372
- // method isn't part of the @abloatai/ablo surface a reader could act on.
1373
- this.runtime.logger.debug('[sync] writes waiting for identity (user/org not set yet) — queued, will retry', {
1374
- pending: this.pendingMutations.length,
1375
- userId: this.userId,
1376
- organizationId: this.organizationId,
1377
- });
1378
- return;
1379
- }
1380
- if (!this.runtime.onlineStatus.isOnline())
1381
- return; // Skip if offline
1382
- if (this.isDisposed)
1383
- return; // Skip if disposed
1384
- if (this.stagedMutationIds.size > 0)
1385
- return;
1386
- const mutations = this.pendingMutations.filter((mutation) => !this.stagedMutationIds.has(mutation.mutationId)).slice(0, 500);
1387
- if (mutations.length === 0)
1388
- return;
1389
- // Claim the batch BEFORE awaiting the journal. This method runs
1390
- // concurrently — the scheduleSync microtask and a direct syncNow() caller
1391
- // land in the same tick — and both would otherwise capture this same
1392
- // batch, suspend on the identical `journaled` promises, and stage every
1393
- // mutation twice (two transactions on the wire for one write). Claiming
1394
- // synchronously makes the second caller hit the guard above and return.
1395
- for (const mutation of mutations) {
1396
- this.stagedMutationIds.add(mutation.mutationId);
1397
- }
1398
- // A journal rejection is permanent for that mutation (fail-closed: it can
1399
- // never dispatch without its durable record), so drop it rather than
1400
- // leaving it queued to poison every later pass. Healthy batch members
1401
- // still stage.
1402
- // Settle each mutation into an object that keeps the mutation bound to its
1403
- // own outcome. Correlating by array index would force a `mutations[index]!`
1404
- // non-null assertion (the lookup is `T | undefined` under
1405
- // noUncheckedIndexedAccess); carrying the reference makes it unnecessary.
1406
- const journalOutcomes = await Promise.all(mutations.map(async (mutation) => {
1407
- try {
1408
- await mutation.journaled;
1409
- return { mutation, ok: true };
1410
- }
1411
- catch (reason) {
1412
- return { mutation, ok: false, reason };
1413
- }
1414
- }));
1415
- const journaledMutations = [];
1416
- for (const outcome of journalOutcomes) {
1417
- const { mutation } = outcome;
1418
- if (outcome.ok) {
1419
- journaledMutations.push(mutation);
1420
- continue;
1421
- }
1422
- this.stagedMutationIds.delete(mutation.mutationId);
1423
- this.pendingMutations = this.pendingMutations.filter((pending) => pending.mutationId !== mutation.mutationId);
1424
- mutation.rejectStaged?.(outcome.reason);
1425
- this.runtime.observability.captureMutationFailure({
1426
- context: 'persist-pending-mutation',
1427
- error: outcome.reason instanceof Error
1428
- ? outcome.reason
1429
- : new Error(String(outcome.reason)),
1430
- });
1431
- }
1432
- // Stage every mutation synchronously within the same event-loop tick;
1433
- // the transaction queue's microtask batches and sends them together.
1434
- for (const mutation of journaledMutations) {
1435
- // Stage synchronously - MutationQueue handles batching, retry, and errors
1436
- this.stageMutation(mutation);
1437
- }
1438
- }
1439
- /**
1440
- * Stage mutation to MutationQueue - mutations in same tick are batched via microtask
1441
- *
1442
- * @param mutation.capturedChanges - Pre-captured changes to use instead of re-reading from model
1443
- */
1444
- stageMutation(mutation) {
1445
- if (!this.userId || !this.organizationId) {
1446
- // Nothing will stage this call; settle the waiter with the legacy
1447
- // "silently dropped" semantics rather than hanging a `wait: 'confirmed'`.
1448
- mutation.resolveStaged?.();
1449
- return;
1450
- }
1451
- const ctx = { userId: this.userId, organizationId: this.organizationId };
1452
- // Settlement is delivered via transaction.confirmation, not this promise —
1453
- // it only rejects when staging itself throws (change extraction, optimistic
1454
- // apply, store add). That means the write never entered the queue, so
1455
- // capture it instead of dropping it silently.
1456
- const captureStagingFailure = (error) => {
1457
- this.runtime.observability.captureMutationFailure({
1458
- context: `stage-mutation-${mutation.type}`,
1459
- modelName: mutation.model.getModelName(),
1460
- modelId: mutation.model.id,
1461
- error: error instanceof Error ? error : new Error(String(error)),
1462
- });
1463
- this.stagedMutationIds.delete(mutation.mutationId);
1464
- };
1465
- const staging = mutation.type === 'update'
1466
- ? this.mutationQueue.update(mutation.model, ctx, mutation.capturedChanges, mutation.writeOptions, mutation.mutationId)
1467
- : this.mutationQueue[mutation.type].bind(this.mutationQueue)(mutation.model, ctx, mutation.writeOptions, mutation.mutationId);
1468
- staging
1469
- .then(() => mutation.resolveStaged?.())
1470
- .catch((error) => {
1471
- captureStagingFailure(error);
1472
- mutation.rejectStaged?.(error);
1473
- });
1474
- }
1475
- /**
1476
- * Resolve a conflict between the local model and incoming server data,
1477
- * called while processing deltas from the WebSocket. Certain server states,
1478
- * such as deletions and deactivations, always take precedence even when the
1479
- * local model has unsynced changes, so the two sides stay consistent.
1480
- */
1481
- resolveConflicts(localModel, serverData) {
1482
- const hasLocalChanges = localModel.hasChanges;
1483
- // Safely get timestamp, handling both Date objects and strings
1484
- const localUpdatedAt = localModel.updatedAt
1485
- ? localModel.updatedAt instanceof Date
1486
- ? localModel.updatedAt.getTime()
1487
- : new Date(localModel.updatedAt).getTime()
1488
- : 0;
1489
- const serverUpdatedAt = toEpochMs(serverData.updatedAt);
1490
- this.runtime.logger.debug('Conflict resolution', {
1491
- modelId: localModel.id,
1492
- modelType: localModel.getModelName(),
1493
- hasLocalChanges,
1494
- localUpdatedAt: localModel.updatedAt?.toString(),
1495
- serverUpdatedAt: serverData.updatedAt,
1496
- localChanges: localModel.getChanges(),
1497
- serverState: this.extractCriticalState(serverData),
1498
- });
1499
- // PRIORITY 1: Check for critical server states that must be respected
1500
- // These states override any local changes to maintain data consistency
1501
- const criticalServerStates = this.extractCriticalState(serverData);
1502
- const shouldForceAcceptServer = this.hasCriticalStateChange(criticalServerStates);
1503
- if (shouldForceAcceptServer) {
1504
- this.runtime.logger.debug('Accepting server update - critical state change detected', {
1505
- modelId: localModel.id,
1506
- criticalStates: criticalServerStates,
1507
- });
1508
- // Force accept server state for critical changes
1509
- localModel.updateFromData(serverData);
1510
- localModel.clearChanges();
1511
- localModel.markAsSynced();
1512
- return localModel;
1513
- }
1514
- // Local-first: if we have local dirty fields, merge by field.
1515
- // Keep locally changed fields; apply server for the rest.
1516
- if (hasLocalChanges) {
1517
- const localChanges = localModel.getChanges();
1518
- this.runtime.logger.debug('Merging server update with local dirty fields', {
1519
- modelId: localModel.id,
1520
- keptFields: Object.keys(localChanges || {}),
1521
- });
1522
- // Merge: server baseline + local dirty fields win
1523
- const merged = { ...serverData, ...(localChanges || {}) };
1524
- // Preserve the most recent updatedAt without clearing dirty flags
1525
- if (serverData.updatedAt || localModel.updatedAt) {
1526
- const mergedUpdatedAt = new Date(Math.max(localUpdatedAt, serverUpdatedAt));
1527
- // updateFromData accepts Date or ISO string for dates
1528
- merged.updatedAt = mergedUpdatedAt;
1529
- }
1530
- localModel.updateFromData(merged);
1531
- // Intentionally DO NOT clearChanges here; pending tx will confirm and clear
1532
- return localModel;
1533
- }
1534
- // No local changes: fall back to LWW to converge
1535
- // Accept server regardless of timestamp equality to stay in sync
1536
- const acceptReason = serverUpdatedAt > localUpdatedAt ? 'server is newer' : 'no local changes';
1537
- this.runtime.logger.debug(`Accepting server update - ${acceptReason}`);
1538
- localModel.updateFromData(serverData);
1539
- localModel.clearChanges();
1540
- localModel.markAsSynced();
1541
- return localModel;
1542
- }
1543
- /**
1544
- * Extract the critical state fields from server data. These are the states
1545
- * that must be honored even when the local model has unsynced changes. The
1546
- * conflict resolver reads exactly these fields and no others.
1547
- */
1548
- extractCriticalState(serverData) {
1549
- const critical = {};
1550
- if (!serverData || typeof serverData !== 'object') {
1551
- return critical;
1552
- }
1553
- // Deletion/archival states - always critical
1554
- if (serverData.deletedAt !== undefined) {
1555
- critical.deletedAt = serverData.deletedAt;
1556
- }
1557
- if (serverData.archivedAt !== undefined) {
1558
- critical.archivedAt = serverData.archivedAt;
1559
- }
1560
- // Deactivation states - critical for assignments and similar entities
1561
- if (serverData.isActive !== undefined && serverData.isActive === false) {
1562
- critical.isActive = false;
1563
- }
1564
- if (serverData.unassignedAt !== undefined) {
1565
- critical.unassignedAt = serverData.unassignedAt;
1566
- }
1567
- return critical;
1568
- }
1569
- /**
1570
- * Check if critical state changes exist that require forcing server state
1571
- */
1572
- hasCriticalStateChange(criticalStates) {
1573
- // Any critical state present means we should force accept server
1574
- return (Object.keys(criticalStates).length > 0 &&
1575
- Object.values(criticalStates).some((v) => v !== null && v !== undefined));
1576
- }
1577
- /**
1578
- * Handle network reconnection
1579
- */
1580
- async handleReconnection() {
1581
- this.runtime.observability.breadcrumb('Network reconnected', 'sync.offline');
1582
- this.emit('sync:reconnecting');
1583
- try {
1584
- // Prefer a single batch flush for pending mutations (fast path)
1585
- try {
1586
- await this.mutationQueue.flushOfflineQueue();
1587
- }
1588
- catch { }
1589
- // Process all queued mutations
1590
- await this.processPendingMutations();
1591
- this.setConnectionState('connected');
1592
- this.emit('sync:reconnected');
1593
- }
1594
- catch (error) {
1595
- this.runtime.observability.captureMutationFailure({
1596
- context: 'reconnection-sync',
1597
- error: error instanceof Error ? error : new Error(String(error)),
1598
- });
1599
- this.emit('sync:error', error);
1600
- }
1601
- }
1602
- /**
1603
- * Handle network disconnection
1604
- */
1605
- async handleDisconnection() {
1606
- this.runtime.observability.breadcrumb('Network disconnected', 'sync.offline');
1607
- this.setConnectionState('disconnected');
1608
- this.emit('sync:offline');
1609
- }
1610
- /**
1611
- * Get current sync state
1612
- */
1613
- getState() {
1614
- return {
1615
- connectionState: this.connectionState,
1616
- pendingMutations: this.pendingMutations.length,
1617
- lastSyncAt: new Date(),
1618
- error: undefined,
1619
- };
1620
- }
1621
- /**
1622
- * Set connection state
1623
- */
1624
- setConnectionState(state) {
1625
- const oldState = this.connectionState;
1626
- this.connectionState = state;
1627
- if (oldState !== state) {
1628
- this.runtime.observability.setConnectionState(state);
1629
- this.runtime.observability.breadcrumb(`Connection: ${oldState} → ${state}`, 'sync.websocket');
1630
- if (state === 'connected') {
1631
- this.emit('connection:established');
1632
- this.mutationQueue.setConnectionState('connected');
1633
- }
1634
- else if (state === 'disconnected') {
1635
- this.emit('connection:disconnected');
1636
- this.mutationQueue.setConnectionState('disconnected');
1637
- }
1638
- }
1639
- }
1640
- /**
1641
- * Subscribe to events with disposer pattern
1642
- */
1643
- subscribe(event, handler) {
1644
- super.on(event, handler);
1645
- // Return disposer function
1646
- return () => {
1647
- this.off(event, handler);
1648
- };
1649
- }
1650
- /**
1651
- * Add observer for sync events
1652
- */
1653
- addObserver(observer) {
1654
- this.observers.add(observer);
1655
- }
1656
- /**
1657
- * Remove observer
1658
- */
1659
- removeObserver(observer) {
1660
- this.observers.delete(observer);
1661
- }
1662
- /**
1663
- * Notify all observers
1664
- */
1665
- notifyObservers(event) {
1666
- for (const observer of this.observers) {
1667
- if (observer.onSync) {
1668
- try {
1669
- observer.onSync(event);
1670
- }
1671
- catch (error) {
1672
- this.runtime.observability.breadcrumb('Observer error', 'sync.transaction', 'error', {
1673
- error: error instanceof Error ? error.message : String(error),
1674
- });
1675
- }
1676
- }
1677
- }
1678
- }
1679
- /**
1680
- * Disconnect from sync
1681
- */
1682
- disconnect() {
1683
- this.setConnectionState('disconnected');
1684
- }
1685
- /**
1686
- * Mark the sync client as connected
1687
- * Called when WebSocket successfully connects (can happen independently of browser online/offline)
1688
- */
1689
- markConnected() {
1690
- this.setConnectionState('connected');
1691
- // Browser online state may have marked the client connected before the
1692
- // WebSocket itself was ready. Always kick both durable lanes on the real
1693
- // socket event, even when the high-level state did not change.
1694
- void this.mutationQueue.flushOfflineQueue().catch((error) => {
1695
- this.runtime.observability.captureMutationFailure({
1696
- context: 'restore-commit-outbox',
1697
- error: error instanceof Error ? error : new Error(String(error)),
1698
- });
1699
- });
1700
- void this.processPendingMutations();
1701
- }
1702
- /**
1703
- * Dispose and cleanup
1704
- */
1705
- dispose() {
1706
- this.isDisposed = true;
1707
- this.disconnect();
1708
- this.networkMonitor.dispose();
1709
- this.observers.clear();
1710
- this.pendingMutations = [];
1711
- this.removeAllListeners();
1712
- }
1713
- /**
1714
- * Notify the {@link MutationQueue} of an incoming delta so it can confirm
1715
- * hosted writes by sync-id threshold and queued forwards by their echoed
1716
- * source-batch correlation id.
1717
- * @param syncId - The sync id of the received delta.
1718
- * @param transactionId - Optional server echo of the originating local write.
1719
- * @param correlationId - Opaque batch identity decoded from a source WAL echo.
1720
- */
1721
- onDeltaReceived(syncId, transactionId, correlationId) {
1722
- try {
1723
- this.mutationQueue.onDeltaReceived(syncId, transactionId, correlationId);
1724
- }
1725
- catch (e) {
1726
- this.runtime.observability.breadcrumb('Failed to notify delta received', 'sync.transaction', 'warning', {
1727
- syncId,
1728
- transactionId,
1729
- correlationId,
1730
- });
1731
- }
1732
- }
1733
- /**
1734
- * Cancel pending transactions for child entities orphaned by a parent's
1735
- * deletion. The store calls this when a delete delta arrives for a parent,
1736
- * cancelling any queued writes on children that reference it.
1737
- *
1738
- * @param childModelName - The child model type (for example, `Block`).
1739
- * @param foreignKey - The foreign-key property name (for example, `sectionId`).
1740
- * @param parentId - The id of the deleted parent.
1741
- * @returns The number of transactions cancelled.
1742
- */
1743
- cancelTransactionsByForeignKey(childModelName, foreignKey, parentId) {
1744
- return this.mutationQueue.cancelTransactionsByForeignKey(childModelName, foreignKey, parentId);
1745
- }
1746
- /**
1747
- * Wait for a transaction to be confirmed by its delta echo. Delegates to the
1748
- * {@link MutationQueue}, which handles the confirmation timeout.
1749
- */
1750
- waitForDeltaConfirmation(transactionId) {
1751
- return this.mutationQueue.waitForConfirmation(transactionId);
1752
- }
1753
- /**
1754
- * Force sync now - process pending mutations
1755
- */
1756
- async syncNow() {
1757
- // Snapshot before draining: a concurrent drain may already have claimed
1758
- // this caller's write, in which case processPendingMutations returns
1759
- // without staging anything. `wait: 'confirmed'` resolves on finding no
1760
- // in-flight work, so it must not run until every write queued before this
1761
- // call has a real transaction in the queue or was definitively dropped.
1762
- const queuedBeforeCall = this.pendingMutations.map((mutation) => mutation.staged);
1763
- await this.processPendingMutations();
1764
- await Promise.allSettled(queuedBeforeCall);
1765
- }
1766
- /**
1767
- * Get sync statistics. Return type is inferred from the literal so
1768
- * the call site sees the actual shape — `connectionState` narrowed
1769
- * to its three states, `objectPoolStats` typed by `InstanceCache.getStats`.
1770
- */
1771
- getSyncStats() {
1772
- return {
1773
- connectionState: this.connectionState,
1774
- pendingMutations: this.pendingMutations.length,
1775
- objectPoolStats: this.objectPool.getStats(),
1776
- };
1777
- }
1778
- /**
1779
- * Get pending transaction count from MutationQueue
1780
- * Used by SyncedStore to compute hasUnsyncedChanges
1781
- */
1782
- getPendingTransactionCount() {
1783
- const stats = this.mutationQueue.getStats();
1784
- // Include pending and executing as "unsynced"
1785
- // awaiting_delta transactions are included in 'executing' until confirmed
1786
- // Completed and failed are "synced" (either done or gave up)
1787
- return stats.pending + stats.executing;
1788
- }
1789
- /**
1790
- * Subscribe to transaction events for sync status tracking
1791
- * Returns unsubscribe function
1792
- */
1793
- onTransactionEvent(event, callback) {
1794
- const eventName = `transaction:${event}`;
1795
- this.mutationQueue.on(eventName, callback);
1796
- return () => this.mutationQueue.off(eventName, callback);
1797
- }
1798
- /**
1799
- * Subscribe to mutation failures with the full payload. Mirrors the
1800
- * underlying MutationQueue 'transaction:failed' shape so consumers
1801
- * can render typed UI (toast keyed by `AbloError.type`, route-level
1802
- * "this entity reverted" boundaries, telemetry).
1803
- *
1804
- * Distinct from `onTransactionEvent('failed', cb)`, which serves the
1805
- * parameterless `pendingChanges` counter and intentionally drops the
1806
- * payload. The two coexist: the counter callback stays lightweight, while
1807
- * this typed listener drives user-visible surfaces.
1808
- */
1809
- onMutationFailure(listener) {
1810
- this.mutationQueue.on('transaction:failed', listener);
1811
- return () => this.mutationQueue.off('transaction:failed', listener);
1812
- }
1813
- /**
1814
- * Subscribe to commit round-trip latency, split into the local seal and the
1815
- * remote acknowledgement. Fires once per completed commit.
1816
- *
1817
- * Taps the {@link MutationQueue} emitter for the same reason
1818
- * {@link onMutationFailure} does: the commit lifecycle events originate
1819
- * there and the SyncClient's own emitter never rebroadcasts them.
1820
- */
1821
- onCommitLatency(listener) {
1822
- return observeCommitLatency(this.mutationQueue, listener);
1823
- }
1824
- /**
1825
- * Subscribe to local transaction creation with the full {@link QueuedMutation}
1826
- * payload (`type`, `modelName`, `modelId`, `data`, `previousData`). This is
1827
- * the feed the store's local-mutation subscription taps for undo recording.
1828
- *
1829
- * It subscribes to the {@link MutationQueue}'s emitter directly, since
1830
- * that is the only emitter that fires `transaction:created`. The SyncClient's
1831
- * own emitter (reached through {@link subscribe}) never rebroadcasts that
1832
- * event, so routing undo through `subscribe('transaction:created')` would
1833
- * record nothing. {@link onMutationFailure} taps the queue for the same
1834
- * reason.
1835
- */
1836
- onLocalTransaction(listener) {
1837
- this.mutationQueue.on('transaction:created', listener);
1838
- const snapshotsByCommit = new Map();
1839
- const onCommitStaging = (payload) => {
1840
- snapshotsByCommit.set(payload.clientTxId, payload.operations.map((operation) => {
1841
- if (operation.type === 'CREATE')
1842
- return undefined;
1843
- const resident = this.objectPool.get(operation.id);
1844
- return resident?.toJSON();
1845
- }));
1846
- };
1847
- const onCommitSealFailed = (payload) => {
1848
- snapshotsByCommit.delete(payload.clientTxId);
1849
- };
1850
- // Commit-lane writes (`ablo.commits.create` — the agent/atomic door) ride
1851
- // their own `commit:created` event: they have no optimistic pool apply,
1852
- // so they must not feed the echo tracker's `transaction:created` path.
1853
- // Enrich each operation with previous state captured from the pool HERE
1854
- // (the queue is pool-free) and hand the synthesized transaction to the
1855
- // same listener, so undo observes every write door — one stream.
1856
- const onCommitCreated = (payload) => {
1857
- const stagedSnapshots = snapshotsByCommit.get(payload.clientTxId);
1858
- snapshotsByCommit.delete(payload.clientTxId);
1859
- const TYPE_BY_WIRE = {
1860
- CREATE: 'create',
1861
- UPDATE: 'update',
1862
- DELETE: 'delete',
1863
- ARCHIVE: 'archive',
1864
- UNARCHIVE: 'unarchive',
1865
- };
1866
- payload.operations.forEach((op, index) => {
1867
- const type = TYPE_BY_WIRE[op.type];
1868
- if (!type || !op.id)
1869
- return;
1870
- const snapshot = type === 'create'
1871
- ? undefined
1872
- : stagedSnapshots
1873
- ? stagedSnapshots[index]
1874
- : this.objectPool.get(op.id)?.toJSON();
1875
- // A DELETE of a row the local graph never saw is not invertible —
1876
- // recording it would make undo "restore" an empty husk. Skip it.
1877
- if (type === 'delete' && !snapshot)
1878
- return;
1879
- // UPDATE inverse must only revert the fields this op actually wrote;
1880
- // handing undo the FULL row would clobber concurrent edits to
1881
- // unrelated fields on revert.
1882
- const previousData = type === 'update' && snapshot && op.input
1883
- ? Object.fromEntries(Object.keys(op.input).map((key) => [key, snapshot[key]]))
1884
- : snapshot ?? null;
1885
- listener({
1886
- id: `${payload.clientTxId}_op${index}`,
1887
- type,
1888
- modelName: op.model,
1889
- modelId: op.id,
1890
- modelKey: op.model,
1891
- data: op.input ?? undefined,
1892
- previousData,
1893
- context: {
1894
- userId: this.userId ?? '',
1895
- organizationId: this.organizationId ?? '',
1896
- },
1897
- status: 'pending',
1898
- createdAt: Date.now(),
1899
- attempts: 0,
1900
- priority: 'normal',
1901
- priorityScore: 0,
1902
- });
1903
- });
1904
- };
1905
- this.mutationQueue.on('commit:staging', onCommitStaging);
1906
- this.mutationQueue.on('commit:seal_failed', onCommitSealFailed);
1907
- this.mutationQueue.on('commit:created', onCommitCreated);
1908
- return () => {
1909
- this.mutationQueue.off('transaction:created', listener);
1910
- this.mutationQueue.off('commit:staging', onCommitStaging);
1911
- this.mutationQueue.off('commit:seal_failed', onCommitSealFailed);
1912
- this.mutationQueue.off('commit:created', onCommitCreated);
1913
- snapshotsByCommit.clear();
1914
- };
1915
- }
1916
- /**
1917
- * Wait for the latest in-flight transaction for (modelName, modelId)
1918
- * to be confirmed by the server, or reject if it's rolled back.
1919
- * Resolves immediately when no transaction is in flight — see
1920
- * `MutationQueue.confirmationFor` for the lookup contract.
1921
- *
1922
- * Distinct from `waitForDeltaConfirmation(transactionId)` which keys
1923
- * off a known tx id; this variant is for call sites that hold a
1924
- * Model reference but never see the underlying transaction.
1925
- */
1926
- waitForConfirmation(modelName, modelId) {
1927
- return this.mutationQueue.confirmationFor(modelName, modelId);
1928
- }
1929
- /**
1930
- * Get detailed debug info for the sync debug page
1931
- */
1932
- getDebugInfo() {
1933
- return {
1934
- connectionState: this.connectionState,
1935
- pendingMutationsCount: this.pendingMutations.length,
1936
- mutationQueue: this.mutationQueue.getDebugInfo(),
1937
- };
1938
- }
1939
- // --- Best-practice assignment ops ---
1940
- async unassignEntity(entityType, entityId) {
1941
- // Call server-side unassign to avoid per-id races
1942
- await this.mutationExecutor.executeDelete('Assignment', entityId);
1943
- }
1944
- async reassignEntity(entityType, entityId, assigneeType, assigneeId, id) {
1945
- await this.mutationExecutor.executeCreate('Assignment', id || '', {
1946
- entityType,
1947
- entityId,
1948
- assigneeType,
1949
- assigneeId,
1950
- });
1951
- }
1952
- // ── Delta + Bootstrap application (owns InstanceCache writes) ──────────────
1953
- /**
1954
- * Apply a batch of delta results from Database to the InstanceCache.
1955
- * Owns: model creation, upsert, remove, archive, conflict resolution.
1956
- * Returns: nothing — InstanceCache is updated in place.
1957
- */
1958
- /**
1959
- * Mark a local transaction as optimistically applied. The matching
1960
- * server delta (when it arrives with the same `transactionId`) will
1961
- * be recognized as an echo and skip the pool mutation. Called
1962
- * automatically by `MutationQueue` when a transaction is staged;
1963
- * exposed publicly so tests can drive the API directly.
1964
- */
1965
- markTransactionPending(transactionId) {
1966
- this.echoTracker.markPending(transactionId);
1967
- }
1968
- /**
1969
- * Read echo-detection counters: hits, rollbacks, evictions, and the
1970
- * current pending-set size. Surfaced for production observability
1971
- * — a sustained `evictions > 0` rate or `rollbacks` spike is a
1972
- * health signal worth alerting on.
1973
- */
1974
- getEchoMetrics() {
1975
- return this.echoTracker.getMetrics();
1976
- }
1977
- /**
1978
- * Package-internal accessor for the {@link MutationQueue}. Used by
1979
- * `Ablo.commits.create()` to route raw multi-operation envelopes through the
1980
- * same retry-on-reconnect lane as the model proxy path, and by tests to
1981
- * exercise the queue's interaction with {@link markTransactionPending} on the
1982
- * real instance the SyncClient subscribes to. It is not re-exported to SDK
1983
- * consumers; `Ablo` is the public surface.
1984
- */
1985
- getMutationQueue() {
1986
- return this.mutationQueue;
1987
- }
1988
- applyDeltaBatchToPool(dbResults, enrichRelations) {
1989
- const modelsToAdd = [];
1990
- const modelsToUpsert = [];
1991
- const idsToRemove = [];
1992
- const idsToArchive = [];
1993
- // Pre-pass: collect every id slated for `remove` in this batch. The
1994
- // parent-delete flicker came from this exact pattern: a peer (or the
1995
- // user themself) deletes a parent with N children; the commit produces
1996
- // BOTH residual `update` deltas (from the optimistic edits that
1997
- // happened just before the delete) AND `remove` deltas. The
1998
- // `update` branch below would `createFromData` the row back into
1999
- // the pool when `existing` was already gone (optimistic remove
2000
- // happened), and the next loop iteration's `remove` would strip
2001
- // it again — net effect: pool transitions live → gone → live →
2002
- // gone in one tick, which the renderer catches mid-frame as a
2003
- // flicker. Filter ops on doomed ids before they touch the pool.
2004
- const idsBeingRemoved = new Set();
2005
- for (const r of dbResults) {
2006
- if (r.action === 'remove')
2007
- idsBeingRemoved.add(r.modelId);
2008
- }
2009
- for (const result of dbResults) {
2010
- const { modelName, modelId, action, transactionId } = result;
2011
- // Echo detection: if this delta carries a transaction id that matches
2012
- // one already applied optimistically, the pool already reflects the
2013
- // mutation, so the pool operation is skipped. The IndexedDB write in
2014
- // Database.processDeltaBatch still runs; only the in-memory pool update
2015
- // is suppressed. This prevents a resurrection flicker: a server-confirmed
2016
- // create arriving after the user has optimistically deleted the row would
2017
- // otherwise re-add it for the brief window before the matching delete
2018
- // confirmation lands.
2019
- if (this.echoTracker.consumeEcho(transactionId)) {
2020
- continue;
2021
- }
2022
- // If a later op in this batch will remove this id, skip earlier
2023
- // add/update ops on it. Server FK ordering can produce
2024
- // U(child)+D(child) when an optimistic edit and a delete both
2025
- // commit in the same window; only the final state matters.
2026
- if ((action === 'add' || action === 'update') && idsBeingRemoved.has(modelId)) {
2027
- continue;
2028
- }
2029
- switch (action) {
2030
- case 'add': {
2031
- const existing = this.objectPool.get(modelId);
2032
- if (existing) {
2033
- existing.markAsSynced();
2034
- }
2035
- else if (result.data) {
2036
- const data = enrichRelations(modelName, { ...result.data, __typename: modelName });
2037
- const model = this.objectPool.createFromData(data);
2038
- if (model)
2039
- modelsToAdd.push(model);
2040
- }
2041
- break;
2042
- }
2043
- case 'update': {
2044
- const existing = this.objectPool.get(modelId);
2045
- if (existing && !existing.disposed && result.data) {
2046
- enrichRelations(modelName, result.data);
2047
- const resolved = this.resolveConflicts(existing, result.data);
2048
- modelsToUpsert.push(resolved);
2049
- }
2050
- // Resurrection drop: if `existing` is gone (optimistic delete
2051
- // discarded it; the matching D delta is in-flight) we used
2052
- // to call `createFromData` here, which reintroduced the row
2053
- // for a frame before the D delta stripped it again — the
2054
- // chart-delete flicker. Trust the local state. If the server
2055
- // still considers the row alive, a subsequent bootstrap or
2056
- // resync will reconcile.
2057
- break;
2058
- }
2059
- case 'remove':
2060
- idsToRemove.push(modelId);
2061
- break;
2062
- case 'archive':
2063
- idsToArchive.push(modelId);
2064
- break;
2065
- case 'verify':
2066
- // `verify` is `Database.processDeltaBatch`'s signal for a delta
2067
- // whose IDB store transaction FAILED. Pool isn't updated for
2068
- // this delta — by design, since the persisted view doesn't
2069
- // reflect it either — and the persistence-gated cursor in
2070
- // `BaseSyncedStore.flushPendingDeltas` will NOT ack past it,
2071
- // so the next 30s catch-up poll (or reconnect handshake) will
2072
- // re-fetch and re-apply. Logged here so silent IDB failures
2073
- // are observable instead of disappearing into a default switch
2074
- // fall-through.
2075
- // Self-healing: the next catch-up poll / reconnect re-fetches and
2076
- // re-applies this delta, so it's forensic, not consumer-actionable → debug.
2077
- this.runtime.logger.debug('[SyncClient.applyDeltaBatchToPool] skipping pool op for unpersisted delta', {
2078
- modelName,
2079
- modelId: modelId.slice(0, 12),
2080
- });
2081
- break;
2082
- }
2083
- }
2084
- // Reveal the whole frame in a single MobX action. `addBatch`,
2085
- // `upsertBatch`, `removeBatch`, and `updateScope` are each individually
2086
- // wrapped in an action, so calling them in sequence flushes reactions at
2087
- // every action boundary — a catch-up frame that adds, updates, and removes
2088
- // would fire every dependent reaction several times in a row, re-rendering
2089
- // and re-sorting on each. Wrapping them in one outer `runInAction` defers
2090
- // all reaction flushes to a single boundary, so dependents recompute
2091
- // exactly once regardless of how many models or operation kinds the frame
2092
- // touched. The app therefore never observes a partially applied frame.
2093
- runInAction(() => {
2094
- if (modelsToAdd.length > 0)
2095
- this.objectPool.addBatch(modelsToAdd, ModelScope.live);
2096
- if (modelsToUpsert.length > 0)
2097
- this.objectPool.upsertBatch(modelsToUpsert, ModelScope.live);
2098
- if (idsToRemove.length > 0)
2099
- this.objectPool.removeBatch(idsToRemove);
2100
- for (const id of idsToArchive)
2101
- this.objectPool.updateScope(id, ModelScope.archived);
2102
- // Emit changed model types so QueryProcessor can auto-invalidate.
2103
- // Kept inside the action so any observable query-cache state it
2104
- // flips is part of the same atomic reveal.
2105
- const changedTypes = new Set(dbResults.map(r => r.modelName));
2106
- if (changedTypes.size > 0)
2107
- this.emit('models:changed', changedTypes);
2108
- });
2109
- }
2110
- /**
2111
- * Apply bootstrap data to the InstanceCache with ghost removal.
2112
- * Owns: model creation, batch upsert, ghost detection + removal.
2113
- */
2114
- applyBootstrapDataToPool(bootstrapData, protectedIds, options) {
2115
- if (!bootstrapData.models) {
2116
- return { added: 0, updated: 0, removed: 0, skipped: 0, healed: 0 };
2117
- }
2118
- const allModels = [];
2119
- const serverIdsByType = new Map();
2120
- let healedCount = 0;
2121
- let skippedCount = 0;
2122
- const failedTypes = new Set(bootstrapData.failedModels ?? []);
2123
- for (const [modelType, records] of Object.entries(bootstrapData.models)) {
2124
- if (failedTypes.has(modelType))
2125
- continue;
2126
- const idsForType = new Set();
2127
- serverIdsByType.set(modelType, idsForType);
2128
- if (!Array.isArray(records) || records.length === 0)
2129
- continue;
2130
- for (const rawRecord of records) {
2131
- if (!rawRecord || typeof rawRecord !== 'object') {
2132
- skippedCount++;
2133
- continue;
2134
- }
2135
- let data = rawRecord;
2136
- if (!data.__typename)
2137
- data = { __typename: modelType, ...data };
2138
- const healResult = this.healModelRecord(modelType, data);
2139
- if (healResult === null) {
2140
- skippedCount++;
2141
- continue;
2142
- }
2143
- data = healResult.data;
2144
- if (healResult.healed)
2145
- healedCount++;
2146
- const recordId = data.id;
2147
- if (recordId)
2148
- idsForType.add(recordId);
2149
- // Scoped backfill for the hydrate-on-enter path: a subset snapshot is
2150
- // taken at a server watermark. If a concurrent live delta already
2151
- // advanced this row past the snapshot, skip it. `createFromData`
2152
- // mutates the pooled model in place to keep instances alive, so this
2153
- // version guard has to run before it; a guard at the upsert layer would
2154
- // be too late, because the row would already be clobbered.
2155
- if (options?.scoped && recordId) {
2156
- const existing = this.objectPool.get(recordId);
2157
- if (existing && !rawRecordIsNewer(data, existing)) {
2158
- skippedCount++;
2159
- continue;
2160
- }
2161
- }
2162
- try {
2163
- const model = this.objectPool.createFromData(data);
2164
- if (model)
2165
- allModels.push(model);
2166
- }
2167
- catch {
2168
- skippedCount++;
2169
- }
2170
- }
2171
- }
2172
- // Upsert. The scoped stale-skip above already guarded the version, so a
2173
- // plain upsert is correct here for both paths.
2174
- const beforeSize = this.objectPool.size;
2175
- this.objectPool.upsertBatch(allModels, ModelScope.live);
2176
- const addedCount = this.objectPool.size - beforeSize;
2177
- const updatedCount = allModels.length - addedCount;
2178
- // Ghost removal: drop pool entities absent from the server snapshot. This
2179
- // is valid only for a full bootstrap, where the snapshot is authoritative
2180
- // for each returned type. A scoped subset snapshot must not remove rows of
2181
- // the same type that belong to other, unhydrated groups.
2182
- let removedCount = 0;
2183
- if (!options?.scoped) {
2184
- const ghostIds = [];
2185
- for (const [modelType, serverIds] of serverIdsByType) {
2186
- const poolIds = this.objectPool.getIdsByModelType(modelType);
2187
- if (!poolIds)
2188
- continue;
2189
- for (const poolId of poolIds) {
2190
- if (!serverIds.has(poolId) && !protectedIds?.has(poolId))
2191
- ghostIds.push(poolId);
2192
- }
2193
- }
2194
- removedCount = this.objectPool.removeBatch(ghostIds);
2195
- }
2196
- // Emit changed model types so QueryProcessor can auto-invalidate
2197
- const changedTypes = new Set(Object.keys(bootstrapData.models));
2198
- if (changedTypes.size > 0)
2199
- this.emit('models:changed', changedTypes);
2200
- return { added: addedCount, updated: updatedCount, removed: removedCount, skipped: skippedCount, healed: healedCount };
2201
- }
2202
- }