@agentshouse/mdruntime 0.1.0-cd50a4607cb7d → 0.1.0-cd574a610e9c8

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 (401) hide show
  1. package/README.md +24 -19
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +299 -0
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +26 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +302 -0
  11. package/dist/adapter.d.ts +226 -117
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +6 -10
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +35 -41
  16. package/dist/authored.d.ts +88 -0
  17. package/dist/authored.d.ts.map +1 -0
  18. package/dist/authored.js +345 -0
  19. package/dist/binary.d.ts +0 -5
  20. package/dist/binary.d.ts.map +1 -1
  21. package/dist/binary.js +1 -1
  22. package/dist/bootstrap.d.ts +31 -11
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +309 -109
  25. package/dist/cell.d.ts +6 -0
  26. package/dist/cell.d.ts.map +1 -0
  27. package/dist/cell.js +9 -0
  28. package/dist/chain.d.ts +12 -0
  29. package/dist/chain.d.ts.map +1 -0
  30. package/dist/chain.js +64 -0
  31. package/dist/change-set.d.ts +13 -16
  32. package/dist/change-set.d.ts.map +1 -1
  33. package/dist/change-set.js +17 -88
  34. package/dist/change.d.ts +36 -1
  35. package/dist/change.d.ts.map +1 -1
  36. package/dist/change.js +17 -3
  37. package/dist/chat-types.d.ts +3 -0
  38. package/dist/chat-types.d.ts.map +1 -0
  39. package/dist/chat-types.js +11 -0
  40. package/dist/client.d.ts +45 -25
  41. package/dist/client.d.ts.map +1 -1
  42. package/dist/client.js +132 -27
  43. package/dist/collection.d.ts +32 -21
  44. package/dist/collection.d.ts.map +1 -1
  45. package/dist/collection.js +53 -288
  46. package/dist/column.d.ts +5 -0
  47. package/dist/column.d.ts.map +1 -0
  48. package/dist/column.js +9 -0
  49. package/dist/compile.d.ts +18 -0
  50. package/dist/compile.d.ts.map +1 -0
  51. package/dist/compile.js +723 -0
  52. package/dist/connection.d.ts +7 -118
  53. package/dist/connection.d.ts.map +1 -1
  54. package/dist/connection.js +111 -2975
  55. package/dist/context.d.ts +2 -0
  56. package/dist/context.d.ts.map +1 -0
  57. package/dist/{chat/context.js → context.js} +2 -5
  58. package/dist/conversation-live.d.ts +150 -0
  59. package/dist/conversation-live.d.ts.map +1 -0
  60. package/dist/conversation-live.js +259 -0
  61. package/dist/conversation-row.d.ts +40 -0
  62. package/dist/conversation-row.d.ts.map +1 -0
  63. package/dist/conversation-row.js +66 -0
  64. package/dist/conversation.d.ts +85 -48
  65. package/dist/conversation.d.ts.map +1 -1
  66. package/dist/conversation.js +1095 -688
  67. package/dist/declaration.d.ts +10 -0
  68. package/dist/declaration.d.ts.map +1 -0
  69. package/dist/declaration.js +24 -0
  70. package/dist/digest.d.ts +7 -0
  71. package/dist/digest.d.ts.map +1 -0
  72. package/dist/digest.js +113 -0
  73. package/dist/discovery.d.ts +10 -0
  74. package/dist/discovery.d.ts.map +1 -0
  75. package/dist/discovery.js +150 -0
  76. package/dist/durability.d.ts +23 -1
  77. package/dist/durability.d.ts.map +1 -1
  78. package/dist/durability.js +9 -0
  79. package/dist/entry.d.ts +5 -2
  80. package/dist/entry.d.ts.map +1 -1
  81. package/dist/entry.js +33 -28
  82. package/dist/expectation.d.ts +26 -13
  83. package/dist/expectation.d.ts.map +1 -1
  84. package/dist/expectation.js +72 -19
  85. package/dist/failure.d.ts +3 -0
  86. package/dist/failure.d.ts.map +1 -1
  87. package/dist/failure.js +6 -0
  88. package/dist/figures.d.ts +49 -0
  89. package/dist/figures.d.ts.map +1 -0
  90. package/dist/figures.js +199 -0
  91. package/dist/file.d.ts +2 -19
  92. package/dist/file.d.ts.map +1 -1
  93. package/dist/file.js +8 -122
  94. package/dist/holdings.d.ts +29 -0
  95. package/dist/holdings.d.ts.map +1 -0
  96. package/dist/holdings.js +95 -0
  97. package/dist/host.d.ts +27 -4
  98. package/dist/host.d.ts.map +1 -1
  99. package/dist/host.js +177 -27
  100. package/dist/identity.d.ts +0 -2
  101. package/dist/identity.d.ts.map +1 -1
  102. package/dist/identity.js +0 -6
  103. package/dist/index.d.ts +41 -14
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +19 -3
  106. package/dist/install.d.ts +60 -0
  107. package/dist/install.d.ts.map +1 -0
  108. package/dist/install.js +537 -0
  109. package/dist/lifecycle.d.ts +7 -0
  110. package/dist/lifecycle.d.ts.map +1 -0
  111. package/dist/lifecycle.js +282 -0
  112. package/dist/listing.d.ts +52 -0
  113. package/dist/listing.d.ts.map +1 -0
  114. package/dist/listing.js +152 -0
  115. package/dist/live.d.ts +25 -0
  116. package/dist/live.d.ts.map +1 -0
  117. package/dist/live.js +206 -0
  118. package/dist/loaded.d.ts +5 -0
  119. package/dist/loaded.d.ts.map +1 -0
  120. package/dist/loaded.js +40 -0
  121. package/dist/locks.d.ts +7 -0
  122. package/dist/locks.d.ts.map +1 -0
  123. package/dist/locks.js +32 -0
  124. package/dist/lost.d.ts +5 -0
  125. package/dist/lost.d.ts.map +1 -0
  126. package/dist/lost.js +49 -0
  127. package/dist/media.d.ts +13 -0
  128. package/dist/media.d.ts.map +1 -0
  129. package/dist/media.js +269 -0
  130. package/dist/memory.d.ts +25 -7
  131. package/dist/memory.d.ts.map +1 -1
  132. package/dist/memory.js +320 -110
  133. package/dist/messages.d.ts +58 -0
  134. package/dist/messages.d.ts.map +1 -0
  135. package/dist/messages.js +160 -0
  136. package/dist/model.d.ts +11 -13
  137. package/dist/model.d.ts.map +1 -1
  138. package/dist/model.js +7 -24
  139. package/dist/models.d.ts +10 -0
  140. package/dist/models.d.ts.map +1 -0
  141. package/dist/models.js +153 -0
  142. package/dist/name.d.ts +4 -0
  143. package/dist/name.d.ts.map +1 -0
  144. package/dist/name.js +122 -0
  145. package/dist/observation.d.ts +40 -10
  146. package/dist/observation.d.ts.map +1 -1
  147. package/dist/observation.js +233 -39
  148. package/dist/opened.d.ts +22 -0
  149. package/dist/opened.d.ts.map +1 -0
  150. package/dist/opened.js +107 -0
  151. package/dist/opening.d.ts +4 -0
  152. package/dist/opening.d.ts.map +1 -0
  153. package/dist/opening.js +53 -0
  154. package/dist/outcome.d.ts +62 -9
  155. package/dist/outcome.d.ts.map +1 -1
  156. package/dist/outcome.js +72 -0
  157. package/dist/overlay.d.ts +9 -6
  158. package/dist/overlay.d.ts.map +1 -1
  159. package/dist/overlay.js +129 -166
  160. package/dist/page-metadata.d.ts +8 -0
  161. package/dist/page-metadata.d.ts.map +1 -0
  162. package/dist/page-metadata.js +19 -0
  163. package/dist/parts.d.ts +4 -0
  164. package/dist/parts.d.ts.map +1 -0
  165. package/dist/parts.js +13 -0
  166. package/dist/paths.d.ts +2 -0
  167. package/dist/paths.d.ts.map +1 -1
  168. package/dist/paths.js +26 -0
  169. package/dist/preparation.d.ts +41 -0
  170. package/dist/preparation.d.ts.map +1 -0
  171. package/dist/preparation.js +314 -0
  172. package/dist/presence.d.ts +58 -0
  173. package/dist/presence.d.ts.map +1 -0
  174. package/dist/presence.js +231 -0
  175. package/dist/queries.d.ts +16 -0
  176. package/dist/queries.d.ts.map +1 -0
  177. package/dist/queries.js +308 -0
  178. package/dist/query.d.ts +27 -186
  179. package/dist/query.d.ts.map +1 -1
  180. package/dist/query.js +3 -464
  181. package/dist/{chat/questions.d.ts → questions.d.ts} +6 -7
  182. package/dist/questions.d.ts.map +1 -0
  183. package/dist/{chat/questions.js → questions.js} +12 -7
  184. package/dist/queue.d.ts +40 -0
  185. package/dist/queue.d.ts.map +1 -0
  186. package/dist/queue.js +480 -0
  187. package/dist/reach.d.ts +20 -7
  188. package/dist/reach.d.ts.map +1 -1
  189. package/dist/reach.js +360 -98
  190. package/dist/react.d.ts +22 -5
  191. package/dist/react.d.ts.map +1 -1
  192. package/dist/react.js +40 -18
  193. package/dist/{chat/read.d.ts → read.d.ts} +2 -0
  194. package/dist/read.d.ts.map +1 -0
  195. package/dist/{chat/read.js → read.js} +4 -0
  196. package/dist/readiness.d.ts +55 -0
  197. package/dist/readiness.d.ts.map +1 -0
  198. package/dist/readiness.js +25 -0
  199. package/dist/reads.d.ts +39 -0
  200. package/dist/reads.d.ts.map +1 -0
  201. package/dist/reads.js +178 -0
  202. package/dist/recovery.d.ts +5 -2
  203. package/dist/recovery.d.ts.map +1 -1
  204. package/dist/recovery.js +65 -8
  205. package/dist/refresh.d.ts +17 -14
  206. package/dist/refresh.d.ts.map +1 -1
  207. package/dist/refresh.js +145 -54
  208. package/dist/refusal.d.ts +5 -0
  209. package/dist/refusal.d.ts.map +1 -0
  210. package/dist/refusal.js +55 -0
  211. package/dist/replica.d.ts +50 -33
  212. package/dist/replica.d.ts.map +1 -1
  213. package/dist/replica.js +340 -191
  214. package/dist/retried.d.ts +30 -0
  215. package/dist/retried.d.ts.map +1 -0
  216. package/dist/retried.js +72 -0
  217. package/dist/room-draft.d.ts +66 -12
  218. package/dist/room-draft.d.ts.map +1 -1
  219. package/dist/room-draft.js +416 -122
  220. package/dist/room-drafts.d.ts +24 -0
  221. package/dist/room-drafts.d.ts.map +1 -0
  222. package/dist/room-drafts.js +220 -0
  223. package/dist/room-list.d.ts +10 -0
  224. package/dist/room-list.d.ts.map +1 -0
  225. package/dist/room-list.js +45 -0
  226. package/dist/rows.d.ts +32 -29
  227. package/dist/rows.d.ts.map +1 -1
  228. package/dist/rows.js +60 -118
  229. package/dist/runtime.d.ts +34 -0
  230. package/dist/runtime.d.ts.map +1 -0
  231. package/dist/runtime.js +1 -0
  232. package/dist/save.d.ts +43 -48
  233. package/dist/save.d.ts.map +1 -1
  234. package/dist/save.js +751 -303
  235. package/dist/saves.d.ts +34 -0
  236. package/dist/saves.d.ts.map +1 -0
  237. package/dist/saves.js +437 -0
  238. package/dist/search.d.ts +15 -77
  239. package/dist/search.d.ts.map +1 -1
  240. package/dist/search.js +10 -186
  241. package/dist/seed.d.ts +25 -0
  242. package/dist/seed.d.ts.map +1 -0
  243. package/dist/seed.js +145 -0
  244. package/dist/shared.d.ts +6 -7
  245. package/dist/shared.d.ts.map +1 -1
  246. package/dist/shared.js +75 -104
  247. package/dist/shown.d.ts +33 -0
  248. package/dist/shown.d.ts.map +1 -0
  249. package/dist/shown.js +309 -0
  250. package/dist/socket/admission.d.ts +7 -0
  251. package/dist/socket/admission.d.ts.map +1 -0
  252. package/dist/socket/admission.js +39 -0
  253. package/dist/socket/answers.d.ts +6 -0
  254. package/dist/socket/answers.d.ts.map +1 -0
  255. package/dist/socket/answers.js +20 -0
  256. package/dist/socket/lanes.d.ts +28 -0
  257. package/dist/socket/lanes.d.ts.map +1 -0
  258. package/dist/socket/lanes.js +103 -0
  259. package/dist/socket/reads.d.ts +10 -0
  260. package/dist/socket/reads.d.ts.map +1 -0
  261. package/dist/socket/reads.js +31 -0
  262. package/dist/socket/session.d.ts +38 -0
  263. package/dist/socket/session.d.ts.map +1 -0
  264. package/dist/socket/session.js +23 -0
  265. package/dist/socket.d.ts +67 -0
  266. package/dist/socket.d.ts.map +1 -0
  267. package/dist/socket.js +164 -0
  268. package/dist/source.d.ts +59 -0
  269. package/dist/source.d.ts.map +1 -0
  270. package/dist/source.js +93 -0
  271. package/dist/status.d.ts +4 -5
  272. package/dist/status.d.ts.map +1 -1
  273. package/dist/status.js +3 -7
  274. package/dist/store/installs.d.ts +30 -0
  275. package/dist/store/installs.d.ts.map +1 -0
  276. package/dist/store/installs.js +40 -0
  277. package/dist/store/readiness.d.ts +11 -0
  278. package/dist/store/readiness.d.ts.map +1 -0
  279. package/dist/store/readiness.js +68 -0
  280. package/dist/store/records.d.ts +223 -0
  281. package/dist/store/records.d.ts.map +1 -0
  282. package/dist/store/records.js +465 -0
  283. package/dist/store/room-held.d.ts +8 -0
  284. package/dist/store/room-held.d.ts.map +1 -0
  285. package/dist/store/room-held.js +17 -0
  286. package/dist/store/rooms-shown.d.ts +8 -0
  287. package/dist/store/rooms-shown.d.ts.map +1 -0
  288. package/dist/store/rooms-shown.js +13 -0
  289. package/dist/store/space.d.ts +11 -0
  290. package/dist/store/space.d.ts.map +1 -0
  291. package/dist/store/space.js +41 -0
  292. package/dist/store/watchers.d.ts +17 -0
  293. package/dist/store/watchers.d.ts.map +1 -0
  294. package/dist/store/watchers.js +56 -0
  295. package/dist/store/writes.d.ts +24 -0
  296. package/dist/store/writes.d.ts.map +1 -0
  297. package/dist/store/writes.js +125 -0
  298. package/dist/store.d.ts +114 -141
  299. package/dist/store.d.ts.map +1 -1
  300. package/dist/store.js +163 -687
  301. package/dist/stored.d.ts +167 -0
  302. package/dist/stored.d.ts.map +1 -0
  303. package/dist/stored.js +700 -0
  304. package/dist/surface.d.ts +90 -0
  305. package/dist/surface.d.ts.map +1 -0
  306. package/dist/surface.js +225 -0
  307. package/dist/sync.d.ts +19 -0
  308. package/dist/sync.d.ts.map +1 -0
  309. package/dist/sync.js +536 -0
  310. package/dist/tables.d.ts +376 -0
  311. package/dist/tables.d.ts.map +1 -0
  312. package/dist/tables.js +499 -0
  313. package/dist/tallies.d.ts +18 -0
  314. package/dist/tallies.d.ts.map +1 -0
  315. package/dist/tallies.js +128 -0
  316. package/dist/transfer.d.ts +0 -5
  317. package/dist/transfer.d.ts.map +1 -1
  318. package/dist/turn.d.ts +5 -3
  319. package/dist/turn.d.ts.map +1 -1
  320. package/dist/turn.js +51 -21
  321. package/dist/validation.d.ts +29 -0
  322. package/dist/validation.d.ts.map +1 -0
  323. package/dist/validation.js +98 -0
  324. package/dist/windows.d.ts +11 -0
  325. package/dist/windows.d.ts.map +1 -0
  326. package/dist/windows.js +16 -0
  327. package/dist/wire.d.ts +230 -36
  328. package/dist/wire.d.ts.map +1 -1
  329. package/dist/wire.js +77 -33
  330. package/package.json +19 -19
  331. package/AGENTS.md +0 -296
  332. package/dist/attempt.d.ts +0 -16
  333. package/dist/attempt.d.ts.map +0 -1
  334. package/dist/attempt.js +0 -70
  335. package/dist/chat/acts.d.ts +0 -33
  336. package/dist/chat/acts.d.ts.map +0 -1
  337. package/dist/chat/acts.js +0 -36
  338. package/dist/chat/context.d.ts +0 -3
  339. package/dist/chat/context.d.ts.map +0 -1
  340. package/dist/chat/input.d.ts +0 -9
  341. package/dist/chat/input.d.ts.map +0 -1
  342. package/dist/chat/input.js +0 -99
  343. package/dist/chat/kit.d.ts +0 -60
  344. package/dist/chat/kit.d.ts.map +0 -1
  345. package/dist/chat/kit.js +0 -123
  346. package/dist/chat/lasting.d.ts +0 -6
  347. package/dist/chat/lasting.d.ts.map +0 -1
  348. package/dist/chat/lasting.js +0 -19
  349. package/dist/chat/list.d.ts +0 -26
  350. package/dist/chat/list.d.ts.map +0 -1
  351. package/dist/chat/list.js +0 -62
  352. package/dist/chat/markdown.d.ts +0 -20
  353. package/dist/chat/markdown.d.ts.map +0 -1
  354. package/dist/chat/markdown.js +0 -188
  355. package/dist/chat/message.d.ts +0 -6
  356. package/dist/chat/message.d.ts.map +0 -1
  357. package/dist/chat/message.js +0 -192
  358. package/dist/chat/messages.d.ts +0 -21
  359. package/dist/chat/messages.d.ts.map +0 -1
  360. package/dist/chat/messages.js +0 -151
  361. package/dist/chat/name.d.ts +0 -6
  362. package/dist/chat/name.d.ts.map +0 -1
  363. package/dist/chat/name.js +0 -37
  364. package/dist/chat/opened.d.ts +0 -11
  365. package/dist/chat/opened.d.ts.map +0 -1
  366. package/dist/chat/opened.js +0 -17
  367. package/dist/chat/overlay.d.ts +0 -31
  368. package/dist/chat/overlay.d.ts.map +0 -1
  369. package/dist/chat/overlay.js +0 -97
  370. package/dist/chat/parts.d.ts +0 -57
  371. package/dist/chat/parts.d.ts.map +0 -1
  372. package/dist/chat/parts.js +0 -119
  373. package/dist/chat/process.d.ts +0 -20
  374. package/dist/chat/process.d.ts.map +0 -1
  375. package/dist/chat/process.js +0 -32
  376. package/dist/chat/provider.d.ts +0 -63
  377. package/dist/chat/provider.d.ts.map +0 -1
  378. package/dist/chat/provider.js +0 -620
  379. package/dist/chat/question.d.ts +0 -9
  380. package/dist/chat/question.d.ts.map +0 -1
  381. package/dist/chat/question.js +0 -80
  382. package/dist/chat/questions.d.ts.map +0 -1
  383. package/dist/chat/read.d.ts.map +0 -1
  384. package/dist/chat/ready.d.ts +0 -33
  385. package/dist/chat/ready.d.ts.map +0 -1
  386. package/dist/chat/ready.js +0 -91
  387. package/dist/chat/style.d.ts +0 -4
  388. package/dist/chat/style.d.ts.map +0 -1
  389. package/dist/chat/style.js +0 -302
  390. package/dist/chat.d.ts +0 -14
  391. package/dist/chat.d.ts.map +0 -1
  392. package/dist/chat.js +0 -8
  393. package/dist/fence.d.ts +0 -6
  394. package/dist/fence.d.ts.map +0 -1
  395. package/dist/fence.js +0 -10
  396. package/dist/log.d.ts +0 -5
  397. package/dist/log.d.ts.map +0 -1
  398. package/dist/log.js +0 -14
  399. package/dist/read-side.d.ts +0 -25
  400. package/dist/read-side.d.ts.map +0 -1
  401. package/dist/read-side.js +0 -639
@@ -1,2987 +1,123 @@
1
- import { attachedModel } from './attached-model.js';
2
- import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
3
- import { observing } from './observation.js';
4
- import { filterOf, identityOf, planOf, } from './query.js';
5
- import { createReadSide } from './read-side.js';
6
- import { markdownPath, roomOfKey } from './rows.js';
7
- import { createSearchIndex, searchPlanOf, } from './search.js';
8
- import { bootstrapRoom } from './bootstrap.js';
9
- import { changeSet, entering, mergeChangeSets } from './change-set.js';
10
- import { createConversationStore, } from './conversation.js';
11
- import { queuedFile } from './change.js';
12
- import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
13
- import { runtimeError } from './failure.js';
14
- import { createLog } from './log.js';
15
- import { binaryRecord } from './binary.js';
16
- import { absentBinary, binaryAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
1
+ import { notReadyWaitMs } from '@agentshouse/mdmodel';
2
+ import { submitLater } from './acceptance.js';
3
+ import { invokeAction, called, readConversationHistory, resendLater } from './actions.js';
4
+ import { builtInModel, NO_ROOM_TYPES } from './attached-model.js';
5
+ import { createLiveConversations } from './conversation-live.js';
6
+ import { writeRule } from './declaration.js';
7
+ import { refusedForSpace } from './durability.js';
8
+ import { uncaught } from './failure.js';
9
+ import { createFigures } from './figures.js';
10
+ import { scheduleCurrency, spaceFreed, start } from './lifecycle.js';
11
+ import { createLiveQueries } from './live.js';
12
+ import { open } from './opening.js';
13
+ import { deliverOwedReadiness, filled, prepare } from './preparation.js';
14
+ import { createPresence } from './presence.js';
15
+ import { answeredPlan, figuresCover, roomTallies } from './queries.js';
16
+ import { readAnswers } from './reads.js';
17
17
  import { createRecovery } from './recovery.js';
18
- import { refreshRoom } from './refresh.js';
19
- import { buildBatch, joined, limitedIn, requeued } from './save.js';
20
- import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, withdrawals, } from './replica.js';
21
- import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
22
- import { same, saving, STARTING, transferring, } from './status.js';
23
- import { detectStorageLoss, nextGeneration } from './durability.js';
24
- import { FencedMap } from './fence.js';
25
- import { tally } from './shared.js';
26
- import { inChunks, ROW_WEIGHT, yielded } from './turn.js';
27
- import { identityTime, roomDraftRevision } from './identity.js';
28
- import { beneath, normalize } from './paths.js';
29
- import { openRuntimeStore, } from './store.js';
30
- import { unconfirmed } from './transfer.js';
31
- import { governs, holds, narrowed, planReach, } from './reach.js';
32
- function within(root, path) {
33
- return root === '' || beneath(root, path);
34
- }
35
- function declaredSelection(room) {
36
- const selection = room.typedSelection;
37
- if (selection === undefined)
38
- return true;
39
- if (!Array.isArray(selection.roots) || selection.roots.length === 0)
40
- return false;
41
- return selection.roots.every((root, index) => validRoot(root) &&
42
- root !== '' &&
43
- selection.roots.every((other, at) => at === index || !within(other, root)));
44
- }
45
- const OPENING_LANE = 'opening';
46
- const CONVERSATION_LANE = 'conversations';
47
- const ROOM_READS = 256;
18
+ import { createRetries } from './retried.js';
19
+ import { publishChangeSet, show } from './shown.js';
20
+ import { createSocket } from './socket.js';
21
+ import { createStore } from './store.js';
22
+ import { composedRuntime } from './surface.js';
48
23
  const ROOM_ATTEMPTS = 16;
49
- const outside = new Map();
50
- function outsideOf(roots) {
51
- const key = JSON.stringify(roots);
52
- const held = outside.get(key);
53
- if (held !== undefined)
54
- return held;
55
- const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
56
- const made = {
57
- documents: (path) => (markdownPath(path) ? counted(path) : 0),
58
- binaries: counted,
59
- };
60
- outside.set(key, made);
61
- return made;
62
- }
63
- function abandonedOn(signal) {
64
- return new Promise((resolve) => {
65
- if (signal.aborted)
66
- resolve(null);
67
- else
68
- signal.addEventListener('abort', () => resolve(null), { once: true });
69
- });
70
- }
71
- function roomLane(locator) {
72
- return `room ${locator}`;
73
- }
74
24
  export function composeRuntime(options) {
75
- const localSaves = options.adapter.localSaves === true;
76
- let authorization = options.authorization;
77
- let opening;
78
- let store;
79
- const fences = new Map();
80
- const isFenced = (locator) => fences.has(locator);
81
- let fencings = 0;
82
- const lastFenced = new Map();
83
- const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
84
- let discoveries = 0;
85
- const replicas = new FencedMap(isFenced);
86
- const queuesWithoutReplica = new FencedMap(isFenced);
87
- const pendings = new Map();
88
- const roomDrafts = new FencedMap(isFenced);
89
- const presentations = new Map();
90
- let limits = [];
91
- let capabilities = [];
92
- const rooms = new FencedMap(isFenced);
93
- const counted = new Map();
94
- const unreadable = new Set();
95
- const isModelFenced = (key) => isFenced(key.slice(key.indexOf(' ') + 1));
96
- const models = new FencedMap(isModelFenced);
97
- const liveModels = new FencedMap(isModelFenced);
98
- const invalidModels = new Set();
99
- const discovered = new Map();
100
- let roomsListed = false;
101
- const scopedRefusals = new Map();
102
- const sourceRefusals = new Map();
103
- const declarationRefusals = new Set();
104
- function modelKey(locator, attached) {
105
- return `${attached} ${locator}`;
106
- }
107
- function modelOf(locator, attached = 'collection') {
108
- return models.get(modelKey(locator, attached)) ?? NO_MODEL;
109
- }
110
- function savedModel(locator, attached) {
111
- return invalidModels.has(modelKey(locator, attached))
112
- ? { status: 'unavailable', reason: 'revoked' }
113
- : modelOf(locator, attached);
114
- }
115
- function compositionFor(record) {
116
- return compositionOf({
117
- ...record,
118
- model: savedModel(record.locator, 'collection'),
119
- selectionModel: savedModel(record.locator, 'typed-selection'),
120
- });
121
- }
122
- let refusal = null;
123
- let storageLoss = null;
124
- let status = STARTING;
125
- let shown = STARTING;
126
- let discoveryPaused = false;
127
- let closed = false;
128
- let stopping = false;
129
- let withdrawStream;
130
- const announced = new Map();
131
- let underway = null;
132
- let streamed = null;
133
- let handed = false;
134
- const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
135
- const repairing = new Set();
136
- const repairable = new Map();
137
- let outcomeRetention = null;
138
- const answering = new Map();
139
- let complete = false;
140
- let recorded = false;
141
- const stale = new Set();
142
- let generation = 0;
143
- let discovering = null;
144
- let opened = null;
145
- const owed = new Set();
146
- const lanes = new Map();
147
- let roomReads = { generation, free: ROOM_READS, waiting: [] };
148
- const working = new Set();
149
- let mutations = Promise.resolve();
150
- const roomListeners = new Set();
151
- const refusalListeners = new Set();
152
- const storageLossListeners = new Set();
153
- const statusListeners = new Set();
154
- const queueSubscriptions = new Set();
155
- const roomDraftListeners = new Set();
156
- const presentationListeners = new Set();
157
- const readSide = createReadSide();
158
- let withdrawConversations;
159
- let currencyQueued = null;
160
- let socketOpen = false;
161
- let serviceMode = 'open';
162
- let haltedRefusal = false;
163
- let openSignals = 0;
164
- const serviceModeListeners = new Set();
165
- let member = null;
166
- const memberListeners = new Set();
167
- const prepared = new Set();
168
- const projecting = new Set();
169
- const demanded = new Set();
170
- let preparing = false;
171
- const catching = new Set();
172
- const transfers = new FencedMap(isFenced);
173
- const conversations = createConversationStore({
174
- agents: declaredAgents,
175
- documents: ({ locator }) => rowsQuery(planOf({ rooms: [locator], select: { fields: 'all' } }, roomFacts())),
176
- call: (name, input) => invokeAction(name, input),
177
- durably,
178
- stream: () => options.adapter.conversations,
179
- publish: publishChangeSet,
180
- online: () => !status.providerUnreachable && refusal === null,
181
- closed: () => closed,
182
- unanswered: resendLater,
183
- expired: (operation) => pastHorizon(operation),
184
- });
185
- const searchIndex = createSearchIndex({
186
- room: (room) => readSide.room(room),
187
- document: (room, path) => readSide.document(room, path),
188
- });
189
- const roomsToIndex = new Set();
190
- const conversationsToIndex = new Set();
191
- let indexing = false;
192
- const journal = createLog();
193
- let notifyReady = () => { };
194
- const ready = new Promise((resolve) => {
195
- notifyReady = resolve;
196
- });
197
- function rememberRoomDraft(record) {
198
- const room = roomDrafts.get(record.locator) ?? new Map();
199
- room.set(record.path, record);
200
- roomDrafts.set(record.locator, room);
201
- }
202
- function forgetRoomDraft(locator, path) {
203
- const room = roomDrafts.get(locator);
204
- if (room === undefined)
205
- return;
206
- room.delete(path);
207
- if (room.size === 0)
208
- roomDrafts.delete(locator);
209
- }
210
- function roomDraftOf(record) {
211
- return structuredClone({
212
- room: record.locator,
213
- path: record.path,
214
- revision: record.revision,
215
- parts: record.parts,
216
- changedUnderneath: underneath(record.parts, record.path, replicas.get(record.locator)?.visible),
217
- returned: record.returned,
218
- });
219
- }
220
- function snapshotRoomDrafts() {
221
- const records = [...roomDrafts.values()].flatMap((room) => [...room.values()]);
222
- records.sort((left, right) => {
223
- if (left.locator !== right.locator)
224
- return left.locator < right.locator ? -1 : 1;
225
- if (left.path !== right.path)
226
- return left.path < right.path ? -1 : 1;
227
- return 0;
228
- });
229
- return records.map(roomDraftOf);
230
- }
231
- function publishRoomDrafts(waitingOnly) {
232
- if (closed)
233
- return;
234
- const value = snapshotRoomDrafts();
235
- for (const listener of roomDraftListeners) {
236
- if (waitingOnly && listener.ready)
237
- continue;
238
- listener.ready = true;
239
- listener.notify(value);
240
- }
241
- }
242
- function presentationOf(app) {
243
- const value = presentations.get(app);
244
- return value === undefined ? null : structuredClone(value);
245
- }
246
- function emitPresentation(app) {
247
- if (closed)
248
- return;
249
- for (const listener of presentationListeners) {
250
- if (listener.app === app && listener.ready)
251
- listener.notify(presentationOf(app));
252
- }
253
- }
254
- function pending() {
255
- return (!localSaves &&
256
- (queuesWithoutReplica.size > 0 ||
257
- [...replicas.values()].some((replica) => replica.queue.length > 0)));
258
- }
259
- function attention() {
260
- const attentionRequired = [];
261
- for (const record of [...roomDrafts.values()].flatMap((room) => [...room.values()])) {
262
- if (record.returned === null)
263
- continue;
264
- attentionRequired.push({
265
- room: record.locator,
266
- path: record.path,
267
- returned: structuredClone(record.returned),
268
- });
269
- }
270
- for (const [room, refusal] of sourceRefusals)
271
- attentionRequired.push({ room, refusal: structuredClone(refusal) });
272
- attentionRequired.sort((left, right) => {
273
- if (left.room !== right.room)
274
- return left.room < right.room ? -1 : 1;
275
- if ('refusal' in left || 'refusal' in right) {
276
- if ('refusal' in left && 'refusal' in right)
277
- return 0;
278
- return 'refusal' in left ? -1 : 1;
279
- }
280
- if (left.path !== right.path)
281
- return left.path < right.path ? -1 : 1;
282
- return 0;
283
- });
284
- let reset = null;
285
- for (const { submission } of pendings.values()) {
286
- if (submission.state !== 'reset')
287
- continue;
288
- if (reset === null || submission.reset < reset)
289
- reset = submission.reset;
290
- }
291
- return { attentionRequired, reset };
292
- }
293
- function documentsIn(locator) {
294
- const replica = replicas.get(locator);
295
- if (replica === undefined)
296
- return 0;
297
- const roots = replica.confirmed.record.typedSelection?.roots ?? [];
298
- const weighed = outsideOf(roots);
299
- return (tally(replica.visible.documents, weighed.documents) +
300
- tally(replica.visible.binaries, weighed.binaries));
301
- }
302
- function listed() {
303
- return structuredClone([...rooms.values()].map((room) => {
304
- const documents = documentsIn(room.locator);
305
- counted.set(room.locator, documents);
306
- return { ...room, documents };
307
- }));
308
- }
309
- function roomFacts(reach = null) {
310
- const held = [...rooms.values()]
311
- .filter((room) => holds(reach, room.locator))
312
- .map((room) => {
313
- const governed = governs(reach, room);
314
- const attached = [
315
- ...(governed.collection ? [modelOf(room.locator)] : []),
316
- ...(governed.selection ? [modelOf(room.locator, 'typed-selection')] : []),
317
- ];
318
- return {
319
- locator: room.locator,
320
- available: attached.every((model) => model.status !== 'unavailable'),
321
- types: attached.flatMap((model) => model.status === 'unavailable' ? [] : Object.values(model.types)),
322
- };
323
- });
324
- return { listed: roomsListed, rooms: held };
325
- }
326
- function emitRooms() {
327
- readSide.recover();
328
- const value = listed();
329
- for (const listener of roomListeners)
330
- listener(value);
331
- }
332
- function emitRefusal() {
333
- for (const listener of refusalListeners)
334
- listener(refusal);
335
- }
336
- function emitStorageLoss() {
337
- for (const listener of storageLossListeners)
338
- listener(storageLoss);
339
- }
340
- function emitMember() {
341
- for (const listener of memberListeners)
342
- listener(member);
343
- }
344
- function moveTo(next) {
345
- if (closed)
346
- return;
347
- status = next;
348
- show();
349
- }
350
- function show() {
351
- if (closed)
352
- return;
353
- const next = status.state === 'synced' && !conversations.current()
354
- ? { ...status, state: 'syncing' }
355
- : status;
356
- if (same(shown, next))
357
- return;
358
- shown = next;
359
- for (const listener of statusListeners)
360
- listener(shown);
361
- }
362
- function entryOf(batch) {
363
- if (localSaves)
364
- return Object.freeze({ batch, state: 'local' });
365
- const state = pendings.get(batch.id);
366
- if (state?.accepted !== undefined) {
367
- return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
368
- }
369
- return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
370
- }
371
- function entriesOf(locator) {
372
- return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map(entryOf);
373
- }
374
- function emitQueue(locator) {
375
- const entries = entriesOf(locator);
376
- for (const subscription of queueSubscriptions) {
377
- if (subscription.locator === locator)
378
- subscription.notify(entries);
379
- }
380
- }
381
- function applyChangeSet(set) {
382
- const held = {
383
- ...set,
384
- documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
385
- };
386
- readSide.apply(held);
387
- searchIndex.changed(held);
388
- }
389
- async function referenced(locator) {
390
- for (;;) {
391
- const record = replicas.get(locator)?.confirmed.record;
392
- if (record === undefined || closed)
393
- return;
394
- const held = { text: record.documents, binaries: record.binaries };
395
- const composition = compositionOf(record);
396
- if (referencesIndexed(held, composition))
397
- return;
398
- await indexReferrers(held, composition);
399
- }
400
- }
401
- async function prepareReplica(locator) {
402
- await referenced(locator);
403
- const replica = replicas.get(locator);
404
- if (replica === undefined || closed || prepared.has(locator))
405
- return;
406
- const visible = replica.visible;
407
- const paths = [
408
- ...new Set([...Object.keys(visible.documents), ...Object.keys(visible.binaries)]),
409
- ];
410
- const entered = [];
411
- projecting.add(locator);
412
- try {
413
- await inChunks(paths, (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
414
- const current = replicas.get(locator);
415
- if (current === undefined || closed)
416
- return;
417
- const set = entering(locator, current, chunk);
418
- readSide.apply(set);
419
- entered.push(set);
420
- });
421
- if (!replicas.has(locator) || closed)
422
- return;
423
- prepared.add(locator);
424
- }
425
- finally {
426
- projecting.delete(locator);
427
- }
428
- conversations.changed(mergeChangeSets(entered));
429
- readSide.recover();
430
- }
431
- function unprepared() {
432
- return [...demanded, ...replicas.keys()].find((locator) => !prepared.has(locator));
433
- }
434
- function prepare(locators = []) {
435
- for (const locator of locators)
436
- demanded.add(locator);
437
- if (preparing)
438
- return;
439
- preparing = true;
440
- void (async () => {
441
- try {
442
- for (let waiting = unprepared(); waiting !== undefined; waiting = unprepared()) {
443
- await yielded();
444
- if (closed)
445
- return;
446
- demanded.delete(waiting);
447
- await prepareReplica(waiting);
25
+ const { adapter, shell } = options;
26
+ const roomTypes = adapter.roomTypes ?? NO_ROOM_TYPES;
27
+ const builtIn = builtInModel(roomTypes);
28
+ const typedPaths = adapter.typedPaths ?? [];
29
+ const retries = createRetries(shell);
30
+ const socket = createSocket(adapter, retries, options.authorization);
31
+ const presence = createPresence(notReadyWaitMs);
32
+ const store = createStore({
33
+ retries,
34
+ database: {
35
+ async ask(request) {
36
+ try {
37
+ return await options.database.ask(request);
448
38
  }
449
- }
450
- finally {
451
- preparing = false;
452
- }
453
- })();
454
- }
455
- function reachedRooms(plan) {
456
- const reach = plan.reach;
457
- const reached = reach === null ? null : new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
458
- const named = plan.rooms ??
459
- (reached === null
460
- ? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
461
- : [...reached]);
462
- return [...new Set(named)].filter((locator) => reached === null || reached.has(locator)).sort();
463
- }
464
- function awaited(locator) {
465
- return rooms.has(locator) || !roomsListed;
466
- }
467
- function covered(plan) {
468
- const locators = reachedRooms(plan);
469
- if (locators.length === 0 && plan.rooms === null && plan.reach === null && !roomsListed) {
470
- return { pending: [] };
471
- }
472
- const pending = [];
473
- const coverage = [];
474
- const overlay = [];
475
- for (const locator of locators) {
476
- const replica = replicas.get(locator);
477
- if (replica === undefined) {
478
- if (awaited(locator))
479
- pending.push({ room: locator, awaits: 'acquisition' });
480
- continue;
481
- }
482
- if (!prepared.has(locator)) {
483
- pending.push({ room: locator, awaits: 'preparation' });
484
- continue;
485
- }
486
- coverage.push({
487
- room: locator,
488
- generation: replica.confirmed.record.transfer,
489
- current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
490
- });
491
- for (const batch of replica.queue)
492
- overlay.push(batch.id);
493
- }
494
- if (pending.length > 0)
495
- return { pending };
496
- return { coverage: { rooms: coverage, indexes: [], overlay } };
497
- }
498
- function awaitPreparation(cover) {
499
- if (!('pending' in cover))
500
- return;
501
- const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
502
- if (waiting.length > 0)
503
- prepare(waiting.map(({ room }) => room));
504
- }
505
- async function rowsQuery(plan) {
506
- const before = covered(plan);
507
- awaitPreparation(before);
508
- if ('pending' in before)
509
- return before;
510
- const { rows, at: cover } = await readSide.query(plan, () => covered(plan));
511
- awaitPreparation(cover);
512
- if ('pending' in cover)
513
- return cover;
514
- return 'groups' in rows
515
- ? { groups: rows.groups, coverage: cover.coverage }
516
- : { documents: rows.documents, coverage: cover.coverage };
517
- }
518
- async function indexConversation(conversation) {
519
- searchIndex.indexMessages(conversation, [], false);
520
- let after = null;
521
- for (;;) {
522
- const batch = await durably((local) => local.messagesAfter(conversation, after));
523
- if (closed)
524
- return;
525
- searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
526
- if (batch.length === 0)
527
- return;
528
- after = batch[batch.length - 1].identity;
529
- }
530
- }
531
- function indexNext() {
532
- const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator));
533
- if (room !== undefined) {
534
- roomsToIndex.delete(room);
535
- return () => (prepared.has(room) ? searchIndex.indexRoom(room) : undefined);
536
- }
537
- const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
538
- if (conversation === undefined)
539
- return null;
540
- conversationsToIndex.delete(conversation);
541
- return () => indexConversation(conversation);
542
- }
543
- function index(rooms, named) {
544
- for (const room of rooms)
545
- roomsToIndex.add(room);
546
- for (const conversation of named)
547
- conversationsToIndex.add(conversation);
548
- if (indexing)
549
- return;
550
- indexing = true;
551
- void (async () => {
552
- try {
553
- for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
554
- await yielded();
555
- await unit();
39
+ catch (error) {
40
+ if (refusedForSpace(error))
41
+ filled(runtime);
42
+ throw error;
556
43
  }
557
- }
558
- finally {
559
- indexing = false;
560
- }
561
- })().catch(() => { });
562
- }
563
- function publishChangeSet(set) {
564
- applyChangeSet(set);
565
- if (set.rooms.length > 0 ||
566
- [...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
567
- emitRooms();
568
- for (const locator of set.queues)
569
- emitQueue(locator);
570
- if (set.drafts.length > 0 ||
571
- set.documents.some((change) => roomDrafts.get(change.locator)?.has(change.path) === true)) {
572
- publishRoomDrafts(false);
573
- }
574
- for (const app of set.presentations)
575
- emitPresentation(app);
576
- conversations.changed(set);
577
- }
578
- async function refuse(reason, provider = null) {
579
- discoveryPaused = true;
580
- try {
581
- if (reason === 'revoked-authorization') {
582
- await exclusive(async () => {
583
- const revoked = { status: 'unavailable', reason: 'revoked' };
584
- try {
585
- const local = await openLocal();
586
- await local.revokeModels([...replicas.values()].map((replica) => replica.confirmed.record));
587
- }
588
- catch {
589
- journal.record('model revocation persistence failed');
590
- }
591
- const keys = new Set(models.keys());
592
- for (const locator of rooms.keys()) {
593
- keys.add(modelKey(locator, 'collection'));
594
- keys.add(modelKey(locator, 'typed-selection'));
595
- }
596
- for (const key of keys) {
597
- invalidModels.add(key);
598
- models.set(key, revoked);
599
- liveModels.delete(key);
600
- }
601
- const changed = [];
602
- for (const [locator, room] of rooms) {
603
- const before = replicas.get(locator) ?? null;
604
- if (before !== null) {
605
- const record = {
606
- ...before.confirmed.record,
607
- model: revoked,
608
- selectionModel: revoked,
609
- };
610
- replicas.set(locator, replicaOf(confirmedOf(record, before.confirmed), before.queue, before));
611
- }
612
- rooms.set(locator, {
613
- ...room,
614
- model: modelTypes(revoked),
615
- typedSelection: room.typedSelection === null
616
- ? null
617
- : { ...room.typedSelection, model: modelTypes(revoked) },
618
- });
619
- changed.push(changeSet(locator, before, replicas.get(locator) ?? null, { room: true }));
620
- }
621
- publishChangeSet(mergeChangeSets(changed));
622
- });
623
- }
624
- }
625
- finally {
626
- refusal = {
627
- code: reason,
628
- retryable: false,
629
- reauthorizationOffered: reason === 'revoked-authorization',
630
- refusal: provider,
631
- };
632
- journal.record(`connection refused ${reason}`);
633
- emitRefusal();
634
- }
635
- }
636
- function list(room, synchronizationAvailable) {
637
- const selection = room.typedSelection ?? null;
638
- rooms.set(room.locator, {
639
- name: room.name,
640
- locator: room.locator,
641
- handle: room.handle,
642
- operations: room.operations,
643
- replica: room.replica,
644
- model: modelTypes(modelOf(room.locator)),
645
- typedSelection: selection === null
646
- ? null
647
- : {
648
- roots: selection.roots,
649
- model: modelTypes(modelOf(room.locator, 'typed-selection')),
650
- },
651
- providerOwnedPaths: room.providerOwnedPaths,
652
- roomVersion: replicas.get(room.locator)?.confirmed.record.roomVersion ?? null,
653
- synchronizationAvailable,
654
- refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
655
- });
656
- }
657
- async function applyDiscovery(found, started) {
658
- for (const room of found) {
659
- const fence = fences.get(room.locator);
660
- if (fence !== undefined && fence.erased && fence.at < started)
661
- fences.delete(room.locator);
662
- }
663
- const listed = found.filter((room) => !fences.has(room.locator));
664
- const listedLocators = new Set(listed.map((room) => room.locator));
665
- for (const locator of listedLocators)
666
- scopedRefusals.delete(locator);
667
- discovered.clear();
668
- for (const room of listed)
669
- discovered.set(room.locator, room);
670
- const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
671
- if (options.adapter.exhaustiveDiscovery === true)
672
- await loseRooms(unlisted);
673
- else
674
- unlistedKept(unlisted);
675
- for (const room of listed) {
676
- const held = replicas.get(room.locator)?.confirmed.record;
677
- if (held !== undefined && held.replica.selection !== room.replica.selection) {
678
- list(held, false);
679
- continue;
680
- }
681
- list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
682
- }
683
- roomsListed = true;
684
- emitRooms();
685
- return listed;
686
- }
687
- function unlistedHeld(listed) {
688
- return [
689
- ...new Set([
690
- ...rooms.keys(),
691
- ...roomDrafts.keys(),
692
- ...transfers.keys(),
693
- ...queuesWithoutReplica.keys(),
694
- ]),
695
- ].filter((locator) => !listed.has(locator));
696
- }
697
- function unlistedKept(unlisted) {
698
- for (const locator of unlisted) {
699
- const held = replicas.get(locator)?.confirmed.record;
700
- if (held !== undefined)
701
- list(held, false);
702
- else
703
- rooms.delete(locator);
704
- }
705
- }
706
- function fence(locators) {
707
- const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
708
- const removed = fresh.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
709
- room: true,
710
- queue: true,
711
- drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
712
- }));
713
- for (const locator of fresh) {
714
- fences.set(locator, { at: discoveries, erased: false });
715
- lastFenced.set(locator, ++fencings);
716
- for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
717
- pendings.delete(batch.id);
718
- replicas.delete(locator);
719
- queuesWithoutReplica.delete(locator);
720
- rooms.delete(locator);
721
- roomDrafts.delete(locator);
722
- transfers.delete(locator);
723
- catching.delete(locator);
724
- discovered.delete(locator);
725
- announced.delete(locator);
726
- unreadable.delete(locator);
727
- stale.delete(locator);
728
- repairable.delete(locator);
729
- scopedRefusals.delete(locator);
730
- sourceRefusals.delete(locator);
731
- declarationRefusals.delete(locator);
732
- for (const attached of ['collection', 'typed-selection']) {
733
- const key = modelKey(locator, attached);
734
- models.delete(key);
735
- liveModels.delete(key);
736
- invalidModels.delete(key);
737
- }
738
- journal.record(`room lost ${locator}`);
739
- }
740
- if (fresh.length === 0)
741
- return fresh;
742
- publishChangeSet(mergeChangeSets(removed));
743
- for (const locator of fresh)
744
- prepared.delete(locator);
745
- searchIndex.forget(fresh);
746
- readSide.recover();
747
- moveTo({ ...status, ...attention() });
748
- return fresh;
749
- }
750
- async function erased(local, locators) {
751
- await local.erase(locators);
752
- for (const locator of locators) {
753
- const held = fences.get(locator);
754
- if (held !== undefined)
755
- held.erased = true;
756
- }
757
- }
758
- async function loseRooms(locators) {
759
- const fresh = fence(locators);
760
- if (fresh.length > 0)
761
- await durably((local) => erased(local, fresh));
762
- }
763
- function restored(key, persisted) {
764
- const prior = models.get(key);
765
- if (prior !== undefined &&
766
- prior.status !== 'unavailable' &&
767
- (persisted.status === 'unavailable' || !sameModel(prior.snapshot, persisted.snapshot))) {
768
- invalidModels.add(key);
769
- }
770
- const model = persisted.status === 'unavailable' ||
771
- liveModels.get(key) === JSON.stringify(persisted.snapshot)
772
- ? persisted
773
- : cached(persisted);
774
- models.set(key, model);
775
- if (model.status === 'unavailable') {
776
- liveModels.delete(key);
777
- invalidModels.delete(key);
778
- }
779
- return model;
780
- }
781
- async function hydrate(opened) {
782
- const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged,] = await Promise.all([
783
- opened.all(),
784
- opened.queues(),
785
- opened.roomDrafts(),
786
- opened.presentation(),
787
- opened.conversations(),
788
- opened.chatDrafts(),
789
- opened.lastCompleted(),
790
- opened.providerLimits(),
791
- opened.capabilities(),
792
- opened.member(),
793
- opened.transfers(),
794
- ]);
795
- transfers.clear();
796
- for (const record of staged)
797
- transfers.set(record.locator, record);
798
- limits = declared;
799
- capabilities = named;
800
- member = known;
801
- roomDrafts.clear();
802
- for (const record of kept)
803
- rememberRoomDraft(record);
804
- presentations.clear();
805
- for (const record of presented)
806
- presentations.set(record.app, record.value);
807
- conversations.hydrate(held, chatDrafts);
808
- const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
809
- pendings.clear();
810
- for (const queue of queues) {
811
- for (const { batch, state } of queue.pending)
812
- pendings.set(batch.id, state);
813
- }
814
- const previousReplicas = new Map(replicas);
815
- replicas.clear();
816
- queuesWithoutReplica.clear();
817
- const replicated = new Set(records.map((record) => record.locator));
818
- for (const [locator, queue] of queuedBatches) {
819
- if (!replicated.has(locator))
820
- queuesWithoutReplica.set(locator, queue);
821
- }
822
- for (const record of records) {
823
- const model = restored(modelKey(record.locator, 'collection'), record.model);
824
- const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
825
- const prior = previousReplicas.get(record.locator);
826
- const confirmed = confirmedOf({ ...record, model, selectionModel }, prior?.confirmed);
827
- replicas.set(record.locator, replicaOf(confirmed, queuedBatches.get(record.locator) ?? [], prior));
828
- const existing = rooms.get(record.locator);
829
- if (existing === undefined)
830
- list(record, false);
831
- else {
832
- rooms.set(record.locator, {
833
- ...existing,
834
- roomVersion: record.roomVersion,
835
- operations: record.operations,
836
- replica: record.replica,
837
- model: modelTypes(model),
838
- typedSelection: record.typedSelection === null
839
- ? null
840
- : {
841
- roots: record.typedSelection.roots,
842
- model: modelTypes(selectionModel),
843
- },
844
- });
845
- }
846
- }
847
- return {
848
- lastCompleted,
849
- locators: new Set([...queuedBatches.keys(), ...replicas.keys()]),
850
- read: mergeChangeSets([...new Set([...previousReplicas.keys(), ...replicas.keys()])].map((locator) => changeSet(locator, previousReplicas.get(locator) ?? null, replicas.get(locator) ?? null))),
851
- };
852
- }
853
- async function readState(opened) {
854
- const { lastCompleted, locators, read } = await hydrate(opened);
855
- applyChangeSet(read);
856
- prepare();
857
- if (replicas.size > 0)
858
- emitRooms();
859
- if (member !== null)
860
- emitMember();
861
- for (const locator of locators)
862
- emitQueue(locator);
863
- publishRoomDrafts(false);
864
- return lastCompleted;
865
- }
866
- async function open() {
867
- if (closed)
868
- throw new Error('runtime closed');
869
- const opened = await openRuntimeStore(isFenced);
870
- store = opened;
871
- const interrupted = await opened.fences();
872
- for (const locator of interrupted) {
873
- fences.set(locator, { at: discoveries, erased: false });
874
- lastFenced.set(locator, ++fencings);
875
- }
876
- const kept = options.shell.generation;
877
- const storedGeneration = await opened.generation();
878
- const loss = detectStorageLoss(storedGeneration, kept);
879
- const generation = nextGeneration(storedGeneration, kept);
880
- if (generation !== storedGeneration)
881
- await opened.rememberGeneration(generation);
882
- if (generation !== kept)
883
- await options.shell.keepGeneration(generation);
884
- if (loss !== null) {
885
- storageLoss = loss;
886
- emitStorageLoss();
887
- }
888
- const lastCompleted = await readState(opened);
889
- if (interrupted.length > 0)
890
- await erased(opened, interrupted);
891
- moveTo({ ...status, lastCompleted, ...attention() });
892
- return opened;
893
- }
894
- function durably(work) {
895
- return exclusive(async () => {
896
- const local = await openLocal();
897
- return work(local);
898
- });
899
- }
900
- function openLocal() {
901
- opening ??= open().catch((error) => {
902
- opening = undefined;
903
- throw error;
904
- });
905
- return opening;
906
- }
907
- function exclusive(work) {
908
- const next = mutations.then(work, work);
909
- mutations = next.then(() => undefined, () => undefined);
910
- return next;
911
- }
912
- function withdrawable(batch) {
913
- const state = pendings.get(batch.id);
914
- return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
915
- }
916
- async function settle(local, locator, before, confirmed, queue, settling = {}) {
917
- const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
918
- const withdrawn = withdrawals(confirmed, queue, withdrawable, localSaves);
919
- const kept = queue.filter((batch) => !withdrawn.includes(batch));
920
- const unqueued = withdrawn.map((batch) => ({
921
- batch,
922
- returned: localSaves || landed(batch, confirmed) ? null : returnedBy('withdrawn'),
923
- }));
924
- const returning = [
925
- ...[...unqueued].reverse(),
926
- ...(settling.leaving === undefined ? [] : [settling.leaving]),
927
- ];
928
- const written = new Map();
929
- for (const { batch, returned } of returning) {
930
- if (returned === null)
931
- continue;
932
- for (const saved of pendings.get(batch.id)?.drafts ?? []) {
933
- const existing = written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path);
934
- written.set(saved.path, {
935
- locator,
936
- path: saved.path,
937
- revision: roomDraftRevision(),
938
- parts: returnedInto(existing?.parts, saved.parts),
939
- returned,
940
- });
941
- }
942
- }
943
- await local.keepAuthored({ locator, queue: kept, roomDrafts: [...written.values()] });
944
- for (const batch of previousQueue) {
945
- if (!kept.includes(batch))
946
- pendings.delete(batch.id);
947
- }
948
- for (const record of written.values())
949
- rememberRoomDraft(record);
950
- try {
951
- await local.write({
952
- next: confirmed.record,
953
- previous: before?.confirmed.record ?? null,
954
- ...(settling.stored === undefined ? {} : { stored: settling.stored }),
955
- });
956
- }
957
- catch (error) {
958
- const held = before === undefined ? null : replicaOf(before.confirmed, kept, before);
959
- if (held !== null)
960
- replicas.set(locator, held);
961
- else if (kept.length > 0)
962
- queuesWithoutReplica.set(locator, kept);
963
- else
964
- queuesWithoutReplica.delete(locator);
965
- publishChangeSet(changeSet(locator, before ?? null, held, {
966
- queue: kept.length !== previousQueue.length,
967
- drafts: [...written.keys()],
968
- }));
969
- moveTo({ ...status, ...attention() });
970
- throw error;
971
- }
972
- return () => {
973
- queuesWithoutReplica.delete(locator);
974
- for (const { batch, returned } of unqueued) {
975
- const outcome = localSaves ? 'dropped' : returned === null ? 'landed' : 'withdrawn';
976
- journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
977
- }
978
- replicas.set(locator, replicaOf(confirmed, kept, before));
979
- return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
980
- room: settling.room,
981
- queue: kept.length !== previousQueue.length,
982
- drafts: [...written.keys()],
983
- });
984
- };
985
- }
986
- function adoptModels(locator, record) {
987
- for (const [attached, model] of [
988
- ['collection', record.model],
989
- ['typed-selection', record.selectionModel],
990
- ]) {
991
- const key = modelKey(locator, attached);
992
- models.set(key, model);
993
- invalidModels.delete(key);
994
- if (model.status === 'current')
995
- liveModels.set(key, JSON.stringify(model.snapshot));
996
- else if (model.status === 'unavailable')
997
- liveModels.delete(key);
998
- }
999
- }
1000
- function remodel(locator, answers, held) {
1001
- const taken = (answer, previous, attached) => {
1002
- if (answer === undefined)
1003
- return previous;
1004
- const { state, refused } = attachedModel(answer, previous);
1005
- if (refused !== null) {
1006
- const where = refused.location === undefined ? '' : ` ${refused.location}`;
1007
- journal.record(`model invalid ${locator} ${attached} ${refused.identity} ${refused.rule}${where}`);
1008
- }
1009
- models.set(modelKey(locator, attached), state);
1010
- return state;
1011
- };
1012
- return {
1013
- collection: taken(answers?.collection, held.collection, 'collection'),
1014
- selection: taken(answers?.selection, held.selection, 'typed-selection'),
1015
- };
1016
- }
1017
- function publish(local, room, confirmed, landing, live) {
1018
- const { base, stored } = landing;
1019
- return exclusive(async () => {
1020
- if (stored !== undefined) {
1021
- await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
1022
- }
1023
- if (!live()) {
1024
- caughtUpTo(room.locator);
1025
- return true;
1026
- }
1027
- const prior = replicas.get(room.locator);
1028
- if (base !== undefined &&
1029
- (prior?.confirmed.record.roomVersion !== base.record.roomVersion ||
1030
- prior.confirmed.record.revisions !== base.record.revisions)) {
1031
- return false;
1032
- }
1033
- adoptModels(room.locator, confirmed.record);
1034
- const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
1035
- const roomVersion = confirmed.record.roomVersion;
1036
- const accepted = queue.filter((batch) => {
1037
- const accepted = pendings.get(batch.id)?.accepted;
1038
- return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
1039
- });
1040
- const set = (await settle(local, room.locator, prior, confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true, ...(stored === undefined ? {} : { stored }) }))();
1041
- if (stored !== undefined) {
1042
- transfers.delete(room.locator);
1043
- catching.delete(room.locator);
1044
- }
1045
- for (const batch of accepted)
1046
- pendings.delete(batch.id);
1047
- stale.delete(room.locator);
1048
- if (unreadable.delete(room.locator))
1049
- scopedRefusals.delete(room.locator);
1050
- sourceRefusals.delete(room.locator);
1051
- declarationRefusals.delete(room.locator);
1052
- list(room, true);
1053
- if (prior === undefined)
1054
- prepared.add(room.locator);
1055
- publishChangeSet(set);
1056
- moveTo({ ...status, ...attention() });
1057
- options.adapter.roomStream?.published(room.locator, roomVersion);
1058
- return true;
1059
- });
1060
- }
1061
- async function saveRoomDrafts(locator, named, writtenBy) {
1062
- const local = await openLocal();
1063
- await referenced(locator);
1064
- const replica = replicas.get(locator);
1065
- const room = rooms.get(locator);
1066
- if (replica === undefined || room === undefined) {
1067
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1068
- }
1069
- const saved = [];
1070
- for (const { path, revision } of named) {
1071
- const record = roomDrafts.get(locator)?.get(path);
1072
- if (record === undefined || record.revision !== revision) {
1073
- return { outcome: 'refused', code: 'stale', retryable: false, file: path };
1074
- }
1075
- if (!saved.some((draft) => draft.path === path))
1076
- saved.push({ path, parts: record.parts });
1077
- }
1078
- const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
1079
- if (changed.length > 0)
1080
- return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
1081
- const parts = saved.map(({ path, parts }) => changesOf(path, parts));
1082
- const changes = parts.flatMap(({ changes }) => changes);
1083
- const requeue = requeued(replica.queue, changes, replica.visible, withdrawable, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
1084
- if (requeue.kind === 'refused')
1085
- return { outcome: 'refused', ...requeue.refusal };
1086
- const foreign = [...requeue.withdrawn.keys()].some((origin) => {
1087
- const writer = replica.queue.find((batch) => batch.id === origin)?.writtenBy;
1088
- return writer?.app !== writtenBy?.app || writer?.release !== writtenBy?.release;
1089
- });
1090
- if (foreign) {
1091
- return {
1092
- outcome: 'refused',
1093
- code: 'unknown-revision',
1094
- retryable: false,
1095
- file: [...requeue.queued].at(-1),
1096
- };
1097
- }
1098
- const withdrawing = requeue.withdrawn.size > 0;
1099
- const input = {
1100
- local: localSaves,
1101
- baseRoomVersion: replica.confirmed.record.roomVersion,
1102
- composition: compositionFor(replica.confirmed.record),
1103
- operations: room.operations,
1104
- providerOwnedPaths: room.providerOwnedPaths,
1105
- providerLimits: limits,
1106
- handle: room.handle,
1107
- visible: replica.visible,
1108
- changes,
1109
- bases: parts.flatMap(({ bases }) => bases),
1110
- writtenBy,
1111
- clean: {
1112
- text: replica.confirmed.record.documents,
1113
- binaries: replica.confirmed.record.binaries,
1114
- composition: compositionOf(replica.confirmed.record),
1115
44
  },
1116
- };
1117
- const checked = withdrawing ? buildBatch({ ...input, queued: requeue.queued }) : null;
1118
- if (checked?.kind === 'refused')
1119
- return { outcome: 'refused', ...checked.refusal };
1120
- const owners = [...requeue.owners].flatMap((origin) => {
1121
- const batch = requeue.withdrawn.get(origin);
1122
- return batch === undefined || batch === null ? [] : [{ origin, batch }];
1123
- });
1124
- if (owners.length > 1) {
1125
- return {
1126
- outcome: 'refused',
1127
- code: 'unknown-revision',
1128
- retryable: false,
1129
- file: [...requeue.queued].at(-1),
1130
- };
1131
- }
1132
- const built = !withdrawing
1133
- ? buildBatch(input)
1134
- : requeue.changes.length === 0
1135
- ? null
1136
- : buildBatch({
1137
- ...input,
1138
- visible: replicaOf(replica.confirmed, requeue.queue, replica).visible,
1139
- changes: requeue.changes,
1140
- });
1141
- if (built?.kind === 'refused')
1142
- return { outcome: 'refused', ...built.refusal };
1143
- const [owner] = owners;
1144
- const join = built === null || owner === undefined
1145
- ? null
1146
- : joined(requeue.queue, owner.batch, built.batch);
1147
- if (join?.kind === 'refused')
1148
- return { outcome: 'refused', ...join.refusal };
1149
- const withdrawn = new Map(requeue.withdrawn);
1150
- if (join !== null && owner !== undefined)
1151
- withdrawn.set(owner.origin, join.batch);
1152
- const drafted = new Map();
1153
- for (const [origin, replaced] of withdrawn) {
1154
- if (replaced === null)
1155
- continue;
1156
- const carried = withdrawnDrafts(pendings.get(origin)?.drafts ?? [], changes);
1157
- drafted.set(replaced.id, replaced === join?.batch
1158
- ? [
1159
- ...carried.map((held) => joinedDraft(held, saved)),
1160
- ...saved.filter((draft) => !carried.some((held) => held.path === draft.path)),
1161
- ]
1162
- : carried);
1163
- }
1164
- if (built !== null && join === null)
1165
- drafted.set(built.batch.id, saved);
1166
- const batches = join !== null ? join.queue : built === null ? requeue.queue : [...requeue.queue, built.batch];
1167
- for (const replaced of withdrawn.values()) {
1168
- if (replaced === null || !batches.includes(replaced))
1169
- continue;
1170
- const limited = limitedIn(batches, replaced, limits, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
1171
- if (limited !== null)
1172
- return { outcome: 'refused', ...limited };
1173
- }
1174
- const consumed = saved.map(({ path }) => path);
1175
- try {
1176
- await local.keepAuthored({
1177
- locator,
1178
- queue: batches,
1179
- saved: drafted,
1180
- consumedRoomDrafts: consumed,
1181
- });
1182
- }
1183
- catch {
1184
- return { outcome: 'refused', code: 'durable-write', retryable: true };
1185
- }
1186
- if (fences.has(locator))
1187
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1188
- for (const origin of withdrawn.keys())
1189
- pendings.delete(origin);
1190
- for (const [id, drafts] of drafted) {
1191
- pendings.set(id, { drafts, submission: { state: 'waiting' }, lost: false });
1192
- }
1193
- if (!localSaves)
1194
- recorded = false;
1195
- for (const path of consumed)
1196
- forgetRoomDraft(locator, path);
1197
- replicas.set(locator, replicaOf(replica.confirmed, batches, replica));
1198
- publishChangeSet(changeSet(locator, replica, replicas.get(locator) ?? null, {
1199
- queue: true,
1200
- drafts: consumed,
1201
- }));
1202
- moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
1203
- submitLater(locator);
1204
- const identifier = join?.batch.id ??
1205
- built?.batch.id ??
1206
- [...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
1207
- return { outcome: 'saved', identifier };
1208
- }
1209
- async function mark(local, locator, identifier, submission, lost) {
1210
- const state = pendings.get(identifier);
1211
- if (!continuing() ||
1212
- state === undefined ||
1213
- (JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
1214
- return;
1215
- }
1216
- await local.submit(locator, identifier, submission, lost);
1217
- if (fences.has(locator))
1218
- return;
1219
- pendings.set(identifier, { ...state, submission, lost });
1220
- const replica = replicas.get(locator);
1221
- if (replica !== undefined)
1222
- publishChangeSet(changeSet(locator, replica, replica, { queue: true }));
1223
- moveTo({ ...status, ...attention() });
1224
- }
1225
- async function keepUpload(local, locator, identifier, upload) {
1226
- if (!continuing())
1227
- throw new Error('connection refused');
1228
- await local.keepUpload(locator, identifier, upload);
1229
- if (fences.has(locator))
1230
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1231
- pendings.set(identifier, { ...pendings.get(identifier), upload });
1232
- if (!continuing())
1233
- throw new Error('connection refused');
1234
- }
1235
- async function readHistory(locator, path, query = {}) {
1236
- if (closed || refusal !== null)
1237
- return UNCERTAIN;
1238
- try {
1239
- await ensureAuthorization();
1240
- }
1241
- catch {
1242
- return UNCERTAIN;
1243
- }
1244
- if (closed || authorization === undefined || refusal !== null)
1245
- return UNCERTAIN;
1246
- const since = fencings;
1247
- const transfer = ['transfer', 'history', locator, path];
1248
- if (query.revision !== undefined)
1249
- transfer.push(query.revision);
1250
- if (query.pageToken !== undefined)
1251
- transfer.push(query.pageToken);
1252
- journal.record(transfer.join(' '));
1253
- try {
1254
- const result = await options.adapter.history(authorization, {
1255
- locator,
1256
- path,
1257
- ...(query.revision === undefined ? {} : { revision: query.revision }),
1258
- ...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
1259
- });
1260
- journal.record(`outcome history ${result.kind}`);
1261
- if (fencedSince(locator, since))
1262
- return failure({ kind: 'room-not-readable', locator });
1263
- if (result.kind === 'scoped-refusal') {
1264
- const room = replicas.get(result.locator)?.confirmed.record;
1265
- if (room !== undefined)
1266
- await scoped(result, room, await openLocal());
1267
- }
1268
- if (result.kind === 'revoked-authorization')
1269
- await refuse('revoked-authorization');
1270
- return historyAnswer(result);
1271
- }
1272
- catch {
1273
- journal.record('outcome history transport-uncertainty');
1274
- return UNCERTAIN;
1275
- }
1276
- }
1277
- function queuedBlob(locator, path, localId) {
1278
- for (const batch of replicas.get(locator)?.queue ?? []) {
1279
- const change = batch.changes[queuedFile(batch, path, localId)];
1280
- if (change?.kind === 'binary')
1281
- return change.blob;
1282
- }
1283
- return null;
1284
- }
1285
- function binaryRoom(request) {
1286
- if (request.kind === 'path')
1287
- return request.locator;
1288
- const scope = options.adapter.referrerScope(request.referrer);
1289
- return scope?.kind === 'room' ? scope.locator : null;
1290
- }
1291
- async function readBinary(request) {
1292
- if (closed || refusal !== null)
1293
- return UNCERTAIN;
1294
- try {
1295
- await ensureAuthorization();
1296
- }
1297
- catch {
1298
- return UNCERTAIN;
1299
- }
1300
- if (closed || authorization === undefined || refusal !== null)
1301
- return UNCERTAIN;
1302
- const since = fencings;
1303
- journal.record(request.kind === 'path'
1304
- ? `transfer binary ${request.locator} ${request.path}`
1305
- : `transfer binary ${request.fileRef}`);
1306
- try {
1307
- const result = await options.adapter.binary(authorization, request);
1308
- journal.record(`outcome binary ${result.kind}`);
1309
- const locator = binaryRoom(request);
1310
- if (locator !== null && fencedSince(locator, since))
1311
- return failure({ kind: 'room-not-readable', locator });
1312
- if (result.kind === 'scoped-refusal') {
1313
- const room = replicas.get(result.locator)?.confirmed.record;
1314
- if (room !== undefined)
1315
- await scoped(result, room, await openLocal());
1316
- }
1317
- if (result.kind === 'revoked-authorization')
1318
- await refuse('revoked-authorization');
1319
- return binaryAnswer(result);
1320
- }
1321
- catch {
1322
- journal.record('outcome binary transport-uncertainty');
1323
- return UNCERTAIN;
1324
- }
1325
- }
1326
- async function readBinaryPath(locator, path, revision) {
1327
- await ready;
1328
- if (revision !== undefined)
1329
- return readBinary({ kind: 'path', locator, path, revision });
1330
- const visible = replicas.get(locator)?.visible;
1331
- const held = visible?.binaries[path];
1332
- if (visible === undefined || held === undefined)
1333
- return absentBinary(path);
1334
- if (held.state === 'pending') {
1335
- const blob = queuedBlob(locator, path, held.localId);
1336
- return blob === null ? absentBinary(path) : { kind: 'binary', blob };
1337
- }
1338
- const record = binaryRecord(path, held, visible.revisions[path]);
1339
- if (record.state !== 'accepted')
1340
- return absentBinary(path);
1341
- const replica = replicas.get(locator);
1342
- const queued = (replica?.queue ?? []).flatMap((batch) => batch.changes);
1343
- const holds = (at) => {
1344
- const kept = replica?.confirmed.record.binaries[at];
1345
- return (kept?.state === 'accepted' &&
1346
- kept.fileRef === record.fileRef &&
1347
- replica?.confirmed.record.revisions[at] === record.revision);
1348
- };
1349
- let confirmed = path;
1350
- for (let index = queued.length - 1; index >= 0 && !holds(confirmed); index -= 1) {
1351
- const change = queued[index];
1352
- if (change.kind === 'move' && change.to === confirmed)
1353
- confirmed = change.path;
1354
- }
1355
- return readBinary({
1356
- kind: 'path',
1357
- locator,
1358
- path: confirmed,
1359
- revision: record.revision,
1360
- fileRef: record.fileRef,
1361
- });
1362
- }
1363
- async function readBinaryReference(reference) {
1364
- await ready;
1365
- return readBinary({
1366
- kind: 'reference',
1367
- fileRef: reference.fileRef,
1368
- referrer: reference.referrer,
1369
- });
1370
- }
1371
- function declaredAgents() {
1372
- for (const capability of capabilities) {
1373
- if (capability.conversationAgents !== undefined)
1374
- return capability.conversationAgents;
1375
- }
1376
- return undefined;
1377
- }
1378
- function declaredAction(name) {
1379
- for (const capability of capabilities) {
1380
- for (const action of capability.actions ?? []) {
1381
- if (action.name === name)
1382
- return action;
1383
- }
1384
- }
1385
- return undefined;
1386
- }
1387
- async function invokeAction(name, input) {
1388
- await ready;
1389
- if (closed || refusal !== null)
1390
- return UNCERTAIN;
1391
- if (declaredAction(name) === undefined)
1392
- return undeclared(name);
1393
- try {
1394
- await ensureAuthorization();
1395
- }
1396
- catch {
1397
- return UNCERTAIN;
1398
- }
1399
- if (closed || authorization === undefined || refusal !== null)
1400
- return UNCERTAIN;
1401
- journal.record(`transfer action ${name}`);
1402
- try {
1403
- const result = await options.adapter.action(authorization, { name, input });
1404
- if (result.kind === 'scoped-refusal') {
1405
- const room = replicas.get(result.locator)?.confirmed.record;
1406
- if (room !== undefined)
1407
- await scoped(result, room, await openLocal());
1408
- }
1409
- if (result.kind === 'revoked-authorization')
1410
- await refuse('revoked-authorization');
1411
- journal.record(`outcome action ${result.kind}`);
1412
- if (result.kind === 'rejected')
1413
- journal.record(`refusal rejected ${result.code}`);
1414
- return calledResult(result);
1415
- }
1416
- catch {
1417
- journal.record('outcome action transport-uncertainty');
1418
- return UNCERTAIN;
1419
- }
1420
- }
1421
- function continuing() {
1422
- return !closed && !discoveryPaused;
1423
- }
1424
- function reachedBy(locator, roomVersion) {
1425
- const held = replicas.get(locator);
1426
- return (held !== undefined &&
1427
- options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion));
1428
- }
1429
- function writtenAfter(locator, roomVersion, path) {
1430
- const written = replicas.get(locator)?.confirmed.record.written[path];
1431
- return (written !== undefined &&
1432
- options.adapter.reached(locator, written, roomVersion) &&
1433
- !options.adapter.reached(locator, roomVersion, written));
1434
- }
1435
- async function pullRefresh(authorized, confirmed, live) {
1436
- if (!live())
1437
- return unconfirmed;
1438
- try {
1439
- const result = await refreshRoom(options.adapter, authorized, confirmed, (models, held) => (live() ? remodel(confirmed.record.locator, models, held) : held), live);
1440
- const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
1441
- journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
1442
- return result;
1443
- }
1444
- catch (error) {
1445
- journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
1446
- throw error;
1447
- }
1448
- }
1449
- function staging(local, live) {
1450
- return {
1451
- held: (locator) => transfers.get(locator) ?? null,
1452
- staged: (record) => local.staged(record),
1453
- stage: (record, page) => exclusive(async () => {
1454
- if (!live())
1455
- return false;
1456
- await local.stage(record, page);
1457
- transfers.set(record.locator, record);
1458
- readSide.recover();
1459
- return true;
1460
- }),
1461
- abandon: (locator) => exclusive(async () => {
1462
- if (!live())
1463
- return;
1464
- await local.abandon(locator);
1465
- transfers.delete(locator);
1466
- readSide.recover();
1467
- }),
1468
- };
1469
- }
1470
- function reusable(room) {
1471
- const record = replicas.get(room.locator)?.confirmed.record;
1472
- if (record === undefined || record.replica.selection !== room.replica.selection)
1473
- return null;
1474
- return { documents: record.documents, revisions: record.revisions };
1475
- }
1476
- async function pullBootstrap(authorized, room, local, live) {
1477
- if (!live())
1478
- return unconfirmed;
1479
- if (replicas.has(room.locator)) {
1480
- catching.add(room.locator);
1481
- readSide.recover();
1482
- }
1483
- try {
1484
- const result = await bootstrapRoom(options.adapter, authorized, room, {
1485
- collection: modelOf(room.locator),
1486
- selection: modelOf(room.locator, 'typed-selection'),
1487
- }, (models, held) => (live() ? remodel(room.locator, models, held) : held), live, staging(local, live), reusable(room));
1488
- const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
1489
- journal.record(`transfer bootstrap ${room.locator} ${detail}`);
1490
- if (result.kind !== 'staged')
1491
- caughtUpTo(room.locator);
1492
- return result;
1493
- }
1494
- catch (error) {
1495
- journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
1496
- caughtUpTo(room.locator);
1497
- throw error;
1498
- }
1499
- }
1500
- function caughtUpTo(locator) {
1501
- if (catching.delete(locator))
1502
- readSide.recover();
1503
- }
1504
- async function advance(room, local, authorized, live) {
1505
- await referenced(room.locator);
1506
- if (!live())
1507
- return unconfirmed;
1508
- const held = replicas.get(room.locator);
1509
- if (held !== undefined) {
1510
- const refreshed = await pullRefresh(authorized, held.confirmed, live);
1511
- if (!live())
1512
- return unconfirmed;
1513
- if (refreshed.kind === 'still-current') {
1514
- stale.delete(room.locator);
1515
- if (sourceRefusals.has(room.locator))
1516
- return null;
1517
- if (unreadable.delete(room.locator)) {
1518
- scopedRefusals.delete(room.locator);
1519
- list(room, true);
1520
- emitRooms();
1521
- }
1522
- return null;
1523
- }
1524
- if (refreshed.kind === 'advanced') {
1525
- if (!(await publish(local, room, refreshed.confirmed, { base: held.confirmed }, live))) {
1526
- const now = replicas.get(room.locator)?.confirmed.record.roomVersion;
1527
- const reached = now !== undefined &&
1528
- options.adapter.reached(room.locator, now, refreshed.confirmed.record.roomVersion);
1529
- return reached ? null : advance(room, local, authorized, live);
1530
- }
1531
- await retainModel(local, room, 'collection', modelOf(room.locator));
1532
- return null;
1533
- }
1534
- if (refreshed.kind !== 'beyond-horizon')
1535
- return refreshed;
1536
- if (opened !== null) {
1537
- owed.add(room.locator);
1538
- readSide.recover();
1539
- return unconfirmed;
1540
- }
1541
- }
1542
- const bootstrapped = await pullBootstrap(authorized, room, local, live);
1543
- if (!live())
1544
- return unconfirmed;
1545
- if (bootstrapped.kind !== 'staged')
1546
- return bootstrapped;
1547
- await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
1548
- return null;
1549
- }
1550
- async function withhold(withheld, room, local, progress) {
1551
- if (withheld.kind === 'unconfirmed')
1552
- return;
1553
- if (withheld.kind === 'refused')
1554
- stale.delete(room.locator);
1555
- else
1556
- stale.add(room.locator);
1557
- if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
1558
- const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
1559
- repairable.set(room.locator, reset);
1560
- repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
1561
- }
1562
- else
1563
- repairable.delete(room.locator);
1564
- if (withheld.kind === 'scoped-refusal') {
1565
- await scoped(withheld, room, local);
1566
- }
1567
- else if (withheld.kind === 'transport-uncertainty') {
1568
- progress.reachable = false;
1569
- }
1570
- else if (withheld.kind === 'retryable-refusal') {
1571
- journal.record(`refusal ${withheld.kind} ${withheld.code}`);
1572
- }
1573
- else if (withheld.kind === 'room-not-readable') {
1574
- unreadable.add(room.locator);
1575
- list(room, false);
1576
- emitRooms();
1577
- }
1578
- else if (withheld.kind === 'refused') {
1579
- refusedSource(room, withheld);
1580
- }
1581
- else if (withheld.kind === 'revoked-authorization') {
1582
- await refuse('revoked-authorization');
1583
- }
1584
- }
1585
- function refusedSource(room, { refusal }) {
1586
- sourceRefusals.set(room.locator, {
1587
- code: refusal.rule,
1588
- retryable: false,
1589
- fields: {
1590
- file: refusal.file,
1591
- ...(refusal.location === undefined ? {} : { location: refusal.location }),
1592
- ...(refusal.referrers === undefined ? {} : { referrers: refusal.referrers }),
1593
- },
1594
- });
1595
- declarationRefusals.delete(room.locator);
1596
- unreadable.add(room.locator);
1597
- list(room, false);
1598
- emitRooms();
1599
- moveTo({ ...status, ...attention() });
1600
- journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
1601
- }
1602
- function leaving(batch, result) {
1603
- if (result.kind === 'expired')
1604
- return { batch, returned: returnedBy('expired') };
1605
- return {
1606
- batch,
1607
- returned: {
1608
- kind: result.kind,
1609
- code: result.code,
1610
- fields: result.fields,
1611
- retryable: result.retryable,
1612
- },
1613
- };
1614
- }
1615
- function logAccepted(locator, submitted, accepted) {
1616
- journal.record(`outcome ${locator} ${submitted.id} accepted ${accepted.roomVersion}` +
1617
- (accepted.author === undefined ? '' : ` author ${accepted.author}`) +
1618
- (accepted.time === undefined ? '' : ` time ${accepted.time}`));
1619
- }
1620
- async function adoptAccepted(room, local, submitted, accepted) {
1621
- await referenced(room.locator);
1622
- if (!continuing())
1623
- return unconfirmed;
1624
- const current = replicas.get(room.locator);
1625
- if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
1626
- return null;
1627
- }
1628
- const held = current.confirmed.record.roomVersion;
1629
- logAccepted(room.locator, submitted, accepted);
1630
- const applied = options.adapter.reached(room.locator, held, accepted.roomVersion)
1631
- ? { kind: 'staged', confirmed: current.confirmed }
1632
- : applyReturned(current.confirmed, submitted, accepted, options.adapter.follows(room.locator, held, accepted.roomVersion)
1633
- ? accepted.roomVersion
1634
- : held, accepted.roomVersion);
1635
- if (applied.kind !== 'staged') {
1636
- const state = pendings.get(submitted.id);
1637
- await local.accept(room.locator, submitted.id, accepted.roomVersion);
1638
- if (state !== undefined && !fences.has(room.locator))
1639
- pendings.set(submitted.id, { ...state, accepted: accepted.roomVersion });
1640
- emitQueue(room.locator);
1641
- return applied;
1642
- }
1643
- const settled = await settle(local, room.locator, current, applied.confirmed, current.queue.filter((batch) => batch.id !== submitted.id), { room: true });
1644
- pendings.delete(submitted.id);
1645
- unreadable.delete(room.locator);
1646
- sourceRefusals.delete(room.locator);
1647
- declarationRefusals.delete(room.locator);
1648
- const set = settled();
1649
- list(room, true);
1650
- publishChangeSet(set);
1651
- moveTo({ ...status, ...attention() });
1652
- return null;
1653
- }
1654
- async function adopted(room, local, submitted, accepted) {
1655
- const failed = await exclusive(() => adoptAccepted(room, local, submitted, accepted));
1656
- if (failed === null)
1657
- return true;
1658
- if (failed.kind === 'refused' && continuing())
1659
- refusedSource(room, failed);
1660
- return false;
1661
- }
1662
- async function submitRoom(room, local, authorized, progress, signal) {
1663
- while (continuing() && authorization !== undefined && !signal.aborted) {
1664
- const replica = replicas.get(room.locator);
1665
- const head = replica?.queue[0];
1666
- if (replica === undefined || head === undefined)
1667
- return 'done';
1668
- if (pendings.get(head.id)?.accepted !== undefined)
1669
- return 'done';
1670
- const state = pendings.get(head.id);
1671
- const { submission, lost: earlier } = state ?? {
1672
- submission: { state: 'waiting' },
1673
- lost: false,
1674
- };
1675
- if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
1676
- progress.complete = false;
1677
- return { at: Date.parse(submission.reset) };
1678
- }
1679
- if (writesHeld()) {
1680
- progress.complete = false;
1681
- journal.record(`submission ${room.locator} ${head.id} held`);
1682
- return 'done';
1683
- }
1684
- const lost = earlier || submission.state === 'submitted';
1685
- if (lost && pastHorizon(head.id)) {
1686
- await discardPending(room, local, signal);
1687
- return 'done';
1688
- }
1689
- let result;
1690
- journal.record(`submission ${room.locator} ${head.id}`);
1691
- await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
1692
- if (discoveryPaused || fences.has(room.locator))
1693
- return 'done';
1694
- if (signal.aborted || writesHeld()) {
1695
- await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
1696
- if (signal.aborted)
1697
- return 'done';
1698
- progress.complete = false;
1699
- journal.record(`submission ${room.locator} ${head.id} held`);
1700
- return 'done';
1701
- }
1702
- const opensBefore = openSignals;
1703
- const answered = awaitingAnswer(room.locator);
1704
- try {
1705
- result = await options.adapter.mutate(authorized, {
1706
- locator: room.locator,
1707
- changeBatch: head,
1708
- ...(state?.upload === undefined ? {} : { upload: state.upload }),
1709
- }, (upload) => exclusive(() => keepUpload(local, room.locator, head.id, upload)));
1710
- }
1711
- catch {
1712
- answered();
1713
- progress.complete = false;
1714
- if (continuing())
1715
- progress.reachable = false;
1716
- return 'again';
1717
- }
1718
- if (signal.aborted) {
1719
- answered();
1720
- return 'done';
1721
- }
1722
- if (result.kind !== 'accepted' || !continuing())
1723
- answered();
1724
- if (!continuing())
1725
- return 'done';
1726
- if (result.kind === 'transport-uncertainty') {
1727
- progress.complete = false;
1728
- progress.reachable = false;
1729
- return 'again';
1730
- }
1731
- if (result.kind === 'room-not-readable') {
1732
- progress.complete = false;
1733
- unreadable.add(room.locator);
1734
- list(room, false);
1735
- emitRooms();
1736
- return 'done';
1737
- }
1738
- if (result.kind === 'revoked-authorization') {
1739
- await refuse('revoked-authorization');
1740
- return 'done';
1741
- }
1742
- if (result.kind === 'scoped-refusal') {
1743
- await scoped(result, room, local);
1744
- progress.complete = false;
1745
- return 'done';
1746
- }
1747
- if (result.kind === 'write-halted') {
1748
- journal.record(`outcome ${room.locator} ${head.id} write-halted`);
1749
- await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, lost));
1750
- if (serviceMode === 'open' && openSignals !== opensBefore)
1751
- continue;
1752
- haltedRefusal = true;
1753
- progress.complete = false;
1754
- return 'done';
1755
- }
1756
- if (result.kind === 'conflicted' ||
1757
- result.kind === 'rejected' ||
1758
- result.kind === 'expired' ||
1759
- result.kind === 'retryable-refusal') {
1760
- if (result.kind === 'expired' && lost && result.restored !== true) {
1761
- journal.record(`outcome ${room.locator} ${head.id} expired`);
1762
- await discardPending(room, local, signal);
1763
- return 'done';
1764
- }
1765
- if (result.kind === 'expired') {
1766
- await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, false));
1767
- const withheld = await advance(room, local, authorized, continuing);
1768
- if (withheld !== null) {
1769
- await withhold(withheld, room, local, progress);
1770
- return 'done';
1771
- }
1772
- }
1773
- journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
1774
- (result.kind === 'conflicted' && result.roomVersion !== undefined
1775
- ? ` ${result.roomVersion}`
1776
- : ''));
1777
- if (result.kind !== 'expired')
1778
- journal.record(`refusal ${result.kind} ${result.code}`);
1779
- }
1780
- if (result.kind === 'retryable-refusal') {
1781
- const reset = result.reset;
1782
- progress.complete = false;
1783
- await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
1784
- return reset === undefined ? 'again' : { at: Date.parse(reset) };
1785
- }
1786
- if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
1787
- const left = leaving(head, result);
1788
- await exclusive(async () => {
1789
- if (!continuing())
1790
- return;
1791
- const current = replicas.get(room.locator);
1792
- if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
1793
- return;
1794
- }
1795
- const settled = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
1796
- leaving: (result.kind === 'expired' && landed(head, current.confirmed)) ||
1797
- (result.kind === 'conflicted' && landed(head, current.confirmed, result.affected))
1798
- ? { ...left, returned: null }
1799
- : left,
1800
- });
1801
- publishChangeSet(settled());
1802
- moveTo({ ...status, ...attention() });
1803
- });
1804
- if (result.kind === 'expired')
1805
- continue;
1806
- const withheld = await advance(room, local, authorized, continuing);
1807
- if (withheld !== null) {
1808
- await withhold(withheld, room, local, progress);
1809
- return 'done';
1810
- }
1811
- continue;
1812
- }
1813
- const kept = await adopted(room, local, head, result).finally(answered);
1814
- if (!kept) {
1815
- progress.complete = false;
1816
- return 'done';
1817
- }
1818
- }
1819
- return 'done';
1820
- }
1821
- function pastHorizon(identity) {
1822
- return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
1823
- }
1824
- async function discardPending(room, local, signal) {
1825
- const { locator } = room;
1826
- const discarded = await exclusive(async () => {
1827
- if (fences.has(locator))
1828
- return false;
1829
- const before = replicas.get(locator);
1830
- const queue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
1831
- fences.set(locator, { at: discoveries, erased: false });
1832
- lastFenced.set(locator, ++fencings);
1833
- try {
1834
- await local.discard(locator);
1835
- }
1836
- finally {
1837
- fences.delete(locator);
1838
- }
1839
- for (const batch of queue)
1840
- pendings.delete(batch.id);
1841
- queuesWithoutReplica.delete(locator);
1842
- const after = before === undefined ? null : replicaOf(before.confirmed, [], before);
1843
- if (after !== null)
1844
- replicas.set(locator, after);
1845
- journal.record(`room discarded ${locator}`);
1846
- publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
1847
- moveTo({ ...status, ...attention() });
1848
- return true;
1849
- });
1850
- if (!discarded)
1851
- return;
1852
- const progress = { complete: true, reachable: true };
1853
- await roomTask(locator, async (live) => {
1854
- await Promise.race([
1855
- reload(room, local, progress, () => live() && !signal.aborted),
1856
- abandonedOn(signal),
1857
- ]);
1858
- });
1859
- if (signal.aborted)
1860
- return;
1861
- await rest(progress);
1862
- }
1863
- function awaitingAnswer(locator) {
1864
- let release = () => { };
1865
- const awaited = new Promise((resolve) => {
1866
- release = resolve;
1867
- });
1868
- answering.set(locator, { generation, answered: awaited });
1869
- return () => {
1870
- if (answering.get(locator)?.answered === awaited)
1871
- answering.delete(locator);
1872
- release();
1873
- };
1874
- }
1875
- async function retainModel(local, room, attached, next) {
1876
- const key = modelKey(room.locator, attached);
1877
- const previous = modelOf(room.locator, attached);
1878
- const heldBefore = replicas.get(room.locator)?.confirmed.record;
1879
- if (JSON.stringify(previous) === JSON.stringify(next) &&
1880
- (heldBefore === undefined ||
1881
- (JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
1882
- JSON.stringify(heldBefore.replica) === JSON.stringify(room.replica) &&
1883
- JSON.stringify(heldBefore.typedSelection) ===
1884
- JSON.stringify(room.typedSelection ?? null))))
1885
- return;
1886
- if (next.status === 'unavailable') {
1887
- invalidModels.add(key);
1888
- liveModels.delete(key);
1889
- }
1890
- await exclusive(async () => {
1891
- if (!continuing())
1892
- return;
1893
- const held = replicas.get(room.locator);
1894
- if (held !== undefined) {
1895
- const declared = {
1896
- ...held.confirmed.record,
1897
- operations: room.operations,
1898
- replica: room.replica,
1899
- typedSelection: room.typedSelection ?? null,
1900
- ...(attached === 'collection' ? { model: next } : { selectionModel: next }),
1901
- };
1902
- const reclassified = sameMembership(declared, held.confirmed.record)
1903
- ? { kind: 'staged', confirmed: confirmedOf(declared, held.confirmed) }
1904
- : readReclassified(declared, held.confirmed);
1905
- const record = reclassified.kind === 'staged'
1906
- ? declared
1907
- : {
1908
- ...declared,
1909
- typedSelection: held.confirmed.record.typedSelection,
1910
- };
1911
- const read = reclassified.kind === 'deferred'
1912
- ? { kind: 'staged', confirmed: confirmedOf(record, held.confirmed) }
1913
- : reclassified;
1914
- await local.write({ next: record, previous: held.confirmed.record });
1915
- if (read.kind === 'staged') {
1916
- replicas.set(room.locator, replicaOf(read.confirmed, held.queue, held));
1917
- if (declarationRefusals.delete(room.locator)) {
1918
- sourceRefusals.delete(room.locator);
1919
- unreadable.delete(room.locator);
1920
- }
1921
- }
1922
- else if (read.kind === 'refused') {
1923
- replicas.set(room.locator, { ...held, confirmed: { ...held.confirmed, record } });
1924
- refusedSource(room, read);
1925
- declarationRefusals.add(room.locator);
1926
- }
1927
- }
1928
- models.set(key, next);
1929
- if (next.status === 'current') {
1930
- liveModels.set(key, JSON.stringify(next.snapshot));
1931
- invalidModels.delete(key);
1932
- }
1933
- list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
1934
- if (held === undefined)
1935
- emitRooms();
1936
- else
1937
- publishChangeSet(changeSet(room.locator, held, replicas.get(room.locator) ?? null, { room: true }));
1938
- });
1939
- }
1940
- async function scoped(result, room, local) {
1941
- scopedRefusals.set(room.locator, {
1942
- code: result.code,
1943
- fields: result.fields,
1944
- retryable: result.retryable,
1945
- });
1946
- let scopedAway = room;
1947
- if (result.scope === 'model')
1948
- await retainModel(local, room, 'collection', {
1949
- status: 'unavailable',
1950
- reason: 'not-readable',
1951
- });
1952
- else if (result.scope !== 'operation') {
1953
- unreadable.add(room.locator);
1954
- scopedAway = {
1955
- ...room,
1956
- replica: { ...room.replica, admission: 'unavailable' },
1957
- operations: {
1958
- ...room.operations,
1959
- ...(result.scope === 'content' ? { read: [] } : {}),
1960
- ...(result.scope === 'sync' ? { sync: [] } : {}),
1961
- },
1962
- };
1963
- await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
1964
- }
1965
- if (!continuing())
1966
- return;
1967
- list(scopedAway, scopedAway.replica.admission === 'admitted' && !unreadable.has(room.locator));
1968
- emitRooms();
1969
- journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
1970
- }
1971
- function heldRooms(listed) {
1972
- return listed.flatMap((room) => {
1973
- const record = replicas.get(room.locator)?.confirmed.record;
1974
- if (record === undefined || record.replica.selection !== room.replica.selection)
1975
- return [];
1976
- return [
1977
- {
1978
- locator: room.locator,
1979
- roomVersion: record.roomVersion,
1980
- documents: record.documents,
1981
- revisions: record.revisions,
1982
- },
1983
- ];
1984
- });
1985
- }
1986
- async function synchronize(listed, local, progress, attempt, live) {
1987
- const prepared = await Promise.allSettled(listed.map((room) => prepareRoom(room, local, progress)));
1988
- if (!live())
1989
- return;
1990
- const ready = listed.filter((_, index) => {
1991
- const outcome = prepared[index];
1992
- return outcome?.status === 'fulfilled' && outcome.value;
1993
- });
1994
- if (attempt === 'opened') {
1995
- options.adapter.roomStream?.hold(heldRooms(ready));
1996
- handed = true;
1997
- }
1998
- discovering?.release();
1999
- const loaded = await Promise.allSettled(ready
2000
- .filter((room) => attempt === 'unstreamed' || !replicas.has(room.locator))
2001
- .map((room) => roomTask(room.locator, (held) => loadRoom(room, local, progress, held))));
2002
- const failed = [...prepared, ...loaded].find((room) => room.status === 'rejected');
2003
- if (failed !== undefined)
2004
- throw failed.reason;
2005
- }
2006
- async function prepareRoom(room, local, progress) {
2007
- if (closed || authorization === undefined) {
2008
- progress.complete = false;
2009
- return false;
2010
- }
2011
- const prior = replicas.get(room.locator)?.confirmed.record;
2012
- if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
2013
- list(prior, false);
2014
- emitRooms();
2015
- journal.record(`selection changed ${room.locator}`);
2016
- progress.complete = false;
2017
- return false;
2018
- }
2019
- if (prior !== undefined)
2020
- await retainModel(local, room, 'collection', modelOf(room.locator));
2021
- if (discoveryPaused || authorization === undefined)
2022
- return false;
2023
- if (room.replica.admission !== 'admitted') {
2024
- if (replicas.has(room.locator))
2025
- progress.complete = false;
2026
- return false;
2027
- }
2028
- return true;
2029
- }
2030
- async function caughtUp(room, local, progress, live) {
2031
- const held = replicas.get(room.locator)?.confirmed.record.roomVersion;
2032
- if (held === undefined || authorization === undefined || !live())
2033
- return;
2034
- const behind = [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later));
2035
- if (!behind)
2036
- return;
2037
- const withheld = await advance(room, local, authorization, live);
2038
- if (withheld !== null)
2039
- await withhold(withheld, room, local, progress);
2040
- }
2041
- function revoked(locator) {
2042
- return ['collection', 'typed-selection'].some((attached) => {
2043
- const model = modelOf(locator, attached);
2044
- return model.status === 'unavailable' && model.reason === 'revoked';
2045
- });
2046
- }
2047
- async function loadRoom(room, local, progress, live) {
2048
- if (!live() || authorization === undefined)
2049
- return;
2050
- if (replicas.has(room.locator) && revoked(room.locator)) {
2051
- await reload(room, local, progress, live);
2052
- return;
2053
- }
2054
- const withheld = await advance(room, local, authorization, live);
2055
- if (withheld !== null) {
2056
- await withhold(withheld, room, local, progress);
2057
- return;
2058
- }
2059
- await caughtUp(room, local, progress, live);
2060
- }
2061
- async function reload(room, local, progress, live) {
2062
- try {
2063
- if (!live() || authorization === undefined)
2064
- return;
2065
- const bootstrapped = await pullBootstrap(authorization, room, local, live);
2066
- if (!live())
2067
- return;
2068
- if (bootstrapped.kind !== 'staged') {
2069
- await withhold(bootstrapped, room, local, progress);
2070
- return;
2071
- }
2072
- await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
2073
- await caughtUp(room, local, progress, live);
2074
- }
2075
- finally {
2076
- caughtUpTo(room.locator);
2077
- }
2078
- }
2079
- async function discoverRooms(local, progress, attempt, live) {
2080
- if (closed || authorization === undefined) {
2081
- notifyReady();
2082
- return;
2083
- }
2084
- moveTo(transferring(status));
2085
- discoveries += 1;
2086
- const started = discoveries;
2087
- let result;
2088
- try {
2089
- result = await options.adapter.discover(authorization);
2090
- }
2091
- catch {
2092
- result = { kind: 'transport-uncertainty' };
2093
- }
2094
- if (!live()) {
2095
- progress.complete = false;
2096
- notifyReady();
2097
- return;
2098
- }
2099
- journal.record(`connection discover ${result.kind}`);
2100
- if (result.kind === 'transport-uncertainty') {
2101
- for (const room of rooms.values()) {
2102
- await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
2103
- if (room.typedSelection !== null) {
2104
- await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
2105
- }
2106
- }
2107
- progress.reachable = false;
2108
- progress.complete = false;
2109
- notifyReady();
2110
- return;
2111
- }
2112
- if (result.kind === 'contract-unavailable') {
2113
- journal.record(`connection contract unavailable ${result.code}`);
2114
- await refuse('missing-required-guarantee', {
2115
- code: result.code,
2116
- fields: result.fields,
2117
- retryable: result.retryable,
2118
- });
2119
- notifyReady();
2120
- return;
2121
- }
2122
- if (result.kind === 'revoked-authorization') {
2123
- await refuse('revoked-authorization');
2124
- notifyReady();
2125
- return;
2126
- }
2127
- for (const room of result.rooms) {
2128
- if (room.operations === undefined ||
2129
- !['read', 'sync', 'write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
2130
- room.operations[operation].every((scope) => typeof scope === 'string')) ||
2131
- room.replica === undefined ||
2132
- typeof room.replica.selection !== 'string' ||
2133
- room.replica.selection.length === 0 ||
2134
- !['admitted', 'unavailable'].includes(room.replica.admission)) {
2135
- journal.record(`invalid model or replica declaration ${room.locator}`);
2136
- await refuse('missing-required-guarantee');
2137
- notifyReady();
2138
- return;
2139
- }
2140
- if (!declaredSelection(room)) {
2141
- journal.record(`invalid typed selection declaration ${room.locator}`);
2142
- await refuse('missing-required-guarantee');
2143
- notifyReady();
2144
- return;
2145
- }
2146
- }
2147
- if (refusal !== null) {
2148
- refusal = null;
2149
- emitRefusal();
2150
- }
2151
- const previousCapabilities = capabilities;
2152
- capabilities = result.capabilities;
2153
- outcomeRetention = result.retention.outcome;
2154
- notifyReady();
2155
- if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
2156
- JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
2157
- limits = result.providerLimits;
2158
- await local.declare(limits, capabilities);
2159
- }
2160
- const signedIn = result.member ?? null;
2161
- if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
2162
- await local.keepMember(signedIn);
2163
- member = signedIn;
2164
- emitMember();
2165
- }
2166
- await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
2167
- }
2168
- async function ensureAuthorization() {
2169
- if (authorization !== undefined)
2170
- return;
2171
- try {
2172
- authorization = await options.adapter.authorize();
2173
- journal.record('connection authorize');
2174
- }
2175
- catch (error) {
2176
- journal.record('connection authorize transport-uncertainty');
2177
- throw error;
2178
- }
2179
- }
2180
- async function admittedRead(work) {
2181
- if (roomReads.generation !== generation) {
2182
- roomReads = { generation, free: ROOM_READS, waiting: [] };
2183
- }
2184
- const reads = roomReads;
2185
- while (reads.free === 0)
2186
- await new Promise((admit) => reads.waiting.push(admit));
2187
- reads.free -= 1;
2188
- try {
2189
- await work();
2190
- }
2191
- finally {
2192
- reads.free += 1;
2193
- reads.waiting.shift()?.();
2194
- }
2195
- }
2196
- function roomTask(locator, work) {
2197
- return task(roomLane(locator), (live) => admittedRead(() => work(live)));
2198
- }
2199
- function task(lane, work) {
2200
- if (stopping)
2201
- return Promise.resolve();
2202
- const held = lanes.get(lane);
2203
- const current = held?.generation === generation ? held : { generation, chain: Promise.resolve() };
2204
- const turn = current.generation;
2205
- const live = () => turn === generation && continuing();
2206
- const run = current.chain.then(() => work(live)).catch(() => undefined);
2207
- current.chain = run;
2208
- lanes.set(lane, current);
2209
- working.add(run);
2210
- void run.finally(() => working.delete(run));
2211
- return run;
2212
- }
2213
- function sync(attempt) {
2214
- return task(OPENING_LANE, async (live) => {
2215
- if (!live()) {
2216
- notifyReady();
2217
- return;
2218
- }
2219
- const progress = { complete: true, reachable: true };
2220
- const gate = discover();
2221
- if (attempt === 'opened')
2222
- opened = generation;
2223
- underway = attempt === 'opened' ? { generation, progress } : null;
2224
- if (attempt === 'opened')
2225
- streamed = progress;
2226
- complete = false;
2227
- recorded = false;
2228
- moveTo(transferring(status));
2229
- handed = false;
2230
- try {
2231
- const local = await openLocal();
2232
- if (!live())
2233
- return;
2234
- await ensureAuthorization();
2235
- if (!live())
2236
- return;
2237
- await discoverRooms(local, progress, attempt, live);
2238
- }
2239
- catch {
2240
- progress.complete = false;
2241
- if (attempt === 'opened' && !handed)
2242
- progress.reachable = false;
2243
- }
2244
- finally {
2245
- gate.release();
2246
- notifyReady();
2247
- }
2248
- if (!live())
2249
- return;
2250
- if (attempt !== 'opened') {
2251
- await finishOpening(progress, live);
2252
- return;
2253
- }
2254
- if (handed || closed || progress.reachable)
2255
- return;
2256
- underway = null;
2257
- opened = null;
2258
- journal.record('connection reopen');
2259
- await rest(progress, live);
2260
- options.adapter.roomStream?.reopen();
2261
- });
2262
- }
2263
- function closedSocket(revoked) {
2264
- const opened = socketOpen;
2265
- socketOpen = false;
2266
- if (revoked) {
2267
- void refuse('revoked-authorization');
2268
- return;
2269
- }
2270
- complete = false;
2271
- if (streamed !== null)
2272
- streamed.complete = false;
2273
- moveTo({
2274
- ...status,
2275
- state: status.state === 'synced' ? 'syncing' : status.state,
2276
- providerUnreachable: status.providerUnreachable || !opened,
2277
- });
2278
- }
2279
- function applyOpening(expired) {
2280
- const progress = underway?.generation === generation ? underway.progress : null;
2281
- const cycle = progress ?? { complete: true, reachable: true };
2282
- const replacing = reloadNow((room) => expired.includes(room.locator), cycle);
2283
- if (opened === generation)
2284
- opened = null;
2285
- return task(OPENING_LANE, async (live) => {
2286
- underway = null;
2287
- if (!live())
2288
- return;
2289
- if (progress === null) {
2290
- await replacing;
2291
- await rest(cycle, live);
2292
- return;
2293
- }
2294
- await roomsCarried(expired);
2295
- if (!live())
2296
- return;
2297
- const replaced = new Set(owed);
2298
- owed.clear();
2299
- try {
2300
- const local = await openLocal();
2301
- const held = [...discovered.values()].filter((room) => replicas.has(room.locator));
2302
- const reloading = held.filter((room) => !expired.includes(room.locator) &&
2303
- (replaced.has(room.locator) || revoked(room.locator)));
2304
- for (const room of reloading)
2305
- catching.add(room.locator);
2306
- readSide.recover();
2307
- const reloads = Promise.allSettled(reloading.map((room) => roomTask(room.locator, (current) => reload(room, local, progress, current))));
2308
- const reloaded = [...(await reloads), ...(await replacing)];
2309
- if (progress.reachable) {
2310
- for (const room of held) {
2311
- if (!expired.includes(room.locator) && !reloading.includes(room)) {
2312
- await confirmModels(room, local);
2313
- }
2314
- }
2315
- }
2316
- const failed = reloaded.find((outcome) => outcome.status === 'rejected');
2317
- if (failed !== undefined)
2318
- throw failed.reason;
2319
- }
2320
- catch {
2321
- progress.complete = false;
2322
- progress.reachable = false;
2323
- }
2324
- if (live())
2325
- await finishOpening(progress, live);
2326
- });
2327
- }
2328
- async function reloadNow(replaced, progress) {
2329
- try {
2330
- const local = await openLocal();
2331
- return await Promise.allSettled([...discovered.values()]
2332
- .filter((room) => replicas.has(room.locator) && replaced(room))
2333
- .map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
2334
- }
2335
- catch {
2336
- progress.reachable = false;
2337
- return [];
2338
- }
2339
- }
2340
- async function confirmModels(room, local) {
2341
- for (const attached of ['collection', 'typed-selection']) {
2342
- const model = modelOf(room.locator, attached);
2343
- if (model.status === 'cached') {
2344
- await retainModel(local, room, attached, { ...model, status: 'current' });
2345
- }
2346
- }
2347
- }
2348
- function submitLater(locator) {
2349
- if (localSaves)
2350
- return;
2351
- void recovery.attempt(`submit ${locator}`, (signal) => submitPass(locator, signal));
2352
- }
2353
- async function submitPass(locator, signal) {
2354
- const room = discovered.get(locator);
2355
- if (closed || stopping || discoveryPaused || room === undefined)
2356
- return 'done';
2357
- const progress = { complete: true, reachable: true };
2358
- let next;
2359
- try {
2360
- await ensureAuthorization();
2361
- if (authorization === undefined)
2362
- return 'done';
2363
- next = await submitRoom(room, await openLocal(), authorization, progress, signal);
2364
- }
2365
- catch {
2366
- progress.reachable = false;
2367
- next = 'again';
2368
- }
2369
- if (signal.aborted)
2370
- return 'done';
2371
- await rest(progress);
2372
- return next === 'again' && !socketOpen ? 'done' : next;
2373
- }
2374
- function repairLater(locator, when) {
2375
- if (!socketOpen || repairing.has(locator))
2376
- return;
2377
- void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
2378
- }
2379
- async function repairPass(locator, signal) {
2380
- const room = discovered.get(locator);
2381
- if (!socketOpen || closed || stopping || discoveryPaused || !stale.has(locator))
2382
- return 'done';
2383
- if (room === undefined || room.replica.admission !== 'admitted')
2384
- return 'done';
2385
- const progress = { complete: true, reachable: true };
2386
- repairing.add(locator);
2387
- try {
2388
- const local = await openLocal();
2389
- await roomTask(locator, async (live) => {
2390
- const current = () => live() && !signal.aborted;
2391
- if (!current() || authorization === undefined)
2392
- return;
2393
- const withheld = await Promise.race([
2394
- advance(room, local, authorization, current),
2395
- abandonedOn(signal),
2396
- ]);
2397
- if (withheld !== null && current())
2398
- await withhold(withheld, room, local, progress);
2399
- });
2400
- }
2401
- catch {
2402
- stale.add(locator);
2403
- progress.reachable = false;
2404
- }
2405
- finally {
2406
- repairing.delete(locator);
2407
- }
2408
- if (signal.aborted)
2409
- return 'done';
2410
- await rest(progress);
2411
- if (!stale.has(locator)) {
2412
- if ((replicas.get(locator)?.queue.length ?? 0) > 0)
2413
- submitLater(locator);
2414
- return 'done';
2415
- }
2416
- if (!repairable.has(locator))
2417
- return 'done';
2418
- const reset = repairable.get(locator);
2419
- return reset === undefined ? 'again' : { at: Date.parse(reset) };
2420
- }
2421
- function resendLater() {
2422
- if (!socketOpen)
2423
- return;
2424
- void recovery.attempt('resend', resendPass, { again: true });
2425
- }
2426
- async function resendPass(signal) {
2427
- if (!socketOpen || closed || stopping || discoveryPaused)
2428
- return 'done';
2429
- const reading = { complete: true, reachable: true };
2430
- try {
2431
- await conversations.resend(reading);
2432
- }
2433
- catch {
2434
- reading.reachable = false;
2435
- }
2436
- if (signal.aborted)
2437
- return 'done';
2438
- return reading.reachable && reading.complete ? 'done' : 'again';
2439
- }
2440
- function submitAll() {
2441
- for (const [locator, replica] of replicas) {
2442
- if (replica.queue.length > 0)
2443
- submitLater(locator);
2444
- }
2445
- }
2446
- async function finishOpening(progress, live) {
2447
- try {
2448
- if (!live()) {
2449
- progress.complete = false;
2450
- return;
2451
- }
2452
- submitAll();
2453
- for (const locator of stale)
2454
- if (repairable.has(locator))
2455
- repairLater(locator, {});
2456
- await conversations.reconcile();
2457
- if (status.providerUnreachable)
2458
- conversations.reconnected();
2459
- const reading = { complete: true, reachable: true };
2460
- await conversations.catchUp(reading);
2461
- if (progress.reachable && reading.reachable)
2462
- await conversations.resend(reading);
2463
- if (!reading.reachable) {
2464
- progress.reachable = false;
2465
- resendLater();
2466
- }
2467
- }
2468
- catch {
2469
- progress.complete = false;
2470
- }
2471
- finally {
2472
- if (live()) {
2473
- complete = progress.complete;
2474
- await rest(progress, live);
2475
- }
2476
- }
2477
- }
2478
- async function rest(progress, live = () => !closed) {
2479
- if (!live())
2480
- return;
2481
- let lastCompleted = status.lastCompleted;
2482
- let synced = complete && stale.size === 0 && !pending();
2483
- if (synced && !recorded) {
2484
- const completedAt = new Date().toISOString();
2485
- try {
2486
- await (await openLocal()).complete(completedAt);
2487
- if (!live())
2488
- return;
2489
- recorded = true;
2490
- lastCompleted = completedAt;
2491
- }
2492
- catch {
2493
- synced = false;
2494
- }
2495
- }
2496
- moveTo({
2497
- state: pending() ? 'pending' : synced && complete ? 'synced' : 'syncing',
2498
- lastCompleted,
2499
- providerUnreachable: !progress.reachable,
2500
- ...attention(),
2501
- });
2502
- }
2503
- function discover() {
2504
- let release = () => { };
2505
- const done = new Promise((resolve) => {
2506
- release = resolve;
2507
- });
2508
- discovering = { generation, done, release };
2509
- return discovering;
2510
- }
2511
- function applyRoom(locator, roomVersion) {
2512
- announced.set(locator, roomVersion);
2513
- return task(roomLane(locator), async (live) => {
2514
- if (discovering?.generation === generation)
2515
- await discovering.done;
2516
- const saving = answering.get(locator);
2517
- if (saving?.generation === generation)
2518
- await saving.answered;
2519
- if (!live() || authorization === undefined)
2520
- return;
2521
- const room = discovered.get(locator);
2522
- const held = replicas.get(locator);
2523
- if (room === undefined || room.replica.admission !== 'admitted')
2524
- return;
2525
- if (held !== undefined &&
2526
- (held.confirmed.record.replica.selection !== room.replica.selection ||
2527
- options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion))) {
2528
- return;
2529
- }
2530
- const during = underway?.generation === generation ? underway.progress : null;
2531
- const progress = during ?? { complete: true, reachable: true };
2532
- if (during === null)
2533
- moveTo(transferring(status));
2534
- try {
2535
- const local = await openLocal();
2536
- await admittedRead(async () => {
2537
- if (!live() || authorization === undefined)
2538
- return;
2539
- const withheld = await advance(room, local, authorization, live);
2540
- if (withheld !== null)
2541
- await withhold(withheld, room, local, progress);
2542
- });
2543
- }
2544
- catch {
2545
- stale.add(locator);
2546
- }
2547
- finally {
2548
- if (during === null)
2549
- await rest(progress, live);
2550
- }
2551
- });
2552
- }
2553
- function roomsCarried(replacing) {
2554
- const replaced = new Set(replacing.map(roomLane));
2555
- return Promise.all([...lanes]
2556
- .filter(([lane, held]) => lane !== OPENING_LANE && !replaced.has(lane) && held.generation === generation)
2557
- .map(([, held]) => held.chain));
2558
- }
2559
- const receipts = {
2560
- heads: async () => (await openLocal()).receivedHeads(),
2561
- parts: async (key, count) => (await openLocal()).receivedParts(key, count),
2562
- keep: async (key, head, part) => (await openLocal()).keepReceived(key, head, part),
2563
- drop: async (keys) => (await openLocal()).dropReceived(keys),
2564
- };
2565
- function observeStream() {
2566
- const stream = options.adapter.roomStream;
2567
- if (stream === undefined || withdrawStream !== undefined)
2568
- return;
2569
- withdrawStream = stream.observe((event) => {
2570
- if (closed || stopping)
2571
- return;
2572
- if (event.kind === 'opened') {
2573
- socketOpen = true;
2574
- generation += 1;
2575
- conversations.reopened();
2576
- recovery.resume();
2577
- void sync('opened');
2578
- }
2579
- else if (event.kind === 'closed')
2580
- closedSocket(event.revoked);
2581
- else if (event.kind === 'opening')
2582
- void applyOpening(event.expired);
2583
- else
2584
- void applyRoom(event.locator, event.roomVersion);
2585
- }, receipts);
2586
- }
2587
- function writesHeld() {
2588
- return serviceMode === 'write-halted' || haltedRefusal;
2589
- }
2590
- function signalled(mode) {
2591
- if (closed)
2592
- return;
2593
- if (mode === 'open')
2594
- openSignals += 1;
2595
- const releasing = mode === 'open' && (haltedRefusal || serviceMode === 'write-halted');
2596
- if (mode === 'open')
2597
- haltedRefusal = false;
2598
- if (mode !== serviceMode) {
2599
- serviceMode = mode;
2600
- journal.record(`service mode ${mode}`);
2601
- for (const listener of serviceModeListeners)
2602
- listener(mode);
2603
- }
2604
- if (releasing)
2605
- submitAll();
2606
- }
2607
- function observeConversations() {
2608
- const stream = options.adapter.conversations;
2609
- if (stream === undefined || withdrawConversations !== undefined)
2610
- return;
2611
- withdrawConversations = stream.observe((event) => {
2612
- if (conversations.receive(event)) {
2613
- void ready.then(scheduleCurrency);
2614
- return;
2615
- }
2616
- if (event.kind !== 'gap' || conversations.current())
2617
- return;
2618
- show();
2619
- scheduleCurrency();
2620
- });
2621
- }
2622
- function scheduleCurrency() {
2623
- if (currencyQueued === generation || closed || stopping)
2624
- return;
2625
- currencyQueued = generation;
2626
- void task(CONVERSATION_LANE, runCurrency);
2627
- }
2628
- async function runCurrency(live) {
2629
- if (currencyQueued === generation)
2630
- currencyQueued = null;
2631
- if (!live())
2632
- return;
2633
- await conversations.reconcile();
2634
- if (conversations.current()) {
2635
- show();
2636
- return;
2637
- }
2638
- const progress = { complete: false, reachable: !status.providerUnreachable };
2639
- moveTo(transferring(status));
2640
- try {
2641
- await conversations.catchUp(progress);
2642
- }
2643
- catch {
2644
- progress.reachable = false;
2645
- }
2646
- await rest(progress, live);
2647
- }
2648
- function close() {
2649
- if (closed)
2650
- return;
2651
- closed = true;
2652
- withdrawStream?.();
2653
- withdrawStream = undefined;
2654
- withdrawConversations?.();
2655
- withdrawConversations = undefined;
2656
- withdrawServiceMode?.();
2657
- conversations.close();
2658
- void recovery.retire();
2659
- store?.close();
2660
- void opening?.then((opened) => {
2661
- opened.close();
2662
- }, () => { });
2663
- roomListeners.clear();
2664
- refusalListeners.clear();
2665
- storageLossListeners.clear();
2666
- statusListeners.clear();
2667
- serviceModeListeners.clear();
2668
- memberListeners.clear();
2669
- readSide.close();
2670
- queueSubscriptions.clear();
2671
- roomDraftListeners.clear();
2672
- presentationListeners.clear();
2673
- }
2674
- async function stop() {
2675
- stopping = true;
2676
- await recovery.retire();
2677
- await Promise.all(working);
2678
- await mutations;
2679
- close();
2680
- }
2681
- const withdrawServiceMode = options.adapter.serviceMode?.observe(signalled);
2682
- observeConversations();
2683
- options.signal?.addEventListener('abort', close, { once: true });
2684
- if (options.signal?.aborted)
2685
- close();
2686
- observeStream();
2687
- void openLocal().then(() => {
2688
- if (capabilities.length > 0)
2689
- notifyReady();
2690
- }, () => {
2691
- notifyReady();
2692
- });
2693
- if (options.adapter.roomStream === undefined)
2694
- void sync('unstreamed');
2695
- function planned(input, reach) {
2696
- return planOf(input, roomFacts(reach), planReach(reach));
2697
- }
2698
- function watchPlan(plan, deliver) {
2699
- return readSide.watch(plan, identityOf(plan), () => {
2700
- const cover = covered(plan);
2701
- awaitPreparation(cover);
2702
- return cover;
2703
- }, deliver);
2704
- }
2705
- async function searched(input, reach) {
2706
- const plan = searchPlanOf(input);
2707
- const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
2708
- for (;;) {
2709
- const cover = covered(filter);
2710
- awaitPreparation(cover);
2711
- const pending = 'pending' in cover ? [...cover.pending] : [];
2712
- const covering = 'pending' in cover ? [] : cover.coverage.rooms;
2713
- const unindexed = covering
2714
- .filter(({ room }) => !searchIndex.rooms().has(room))
2715
- .map(({ room }) => room);
2716
- for (const room of unindexed)
2717
- pending.push({ room, awaits: 'index' });
2718
- const held = conversations.searchable();
2719
- const named = reach !== null && !reach.conversations
2720
- ? []
2721
- : (plan.conversations ?? held).filter((one) => held.includes(one) && conversations.acquired(one));
2722
- const waiting = named.filter((one) => !searchIndex.conversations().has(one));
2723
- for (const conversation of waiting)
2724
- pending.push({ conversation, awaits: 'index' });
2725
- if (unindexed.length > 0 || waiting.length > 0)
2726
- index(unindexed, waiting);
2727
- if (pending.length > 0 || 'pending' in cover)
2728
- return { pending };
2729
- const seen = searchIndex.changes();
2730
- await searchIndex.caughtUp();
2731
- const selected = plan.documents === undefined && reach === null ? null : await rowsQuery(filter);
2732
- if (selected !== null && 'pending' in selected)
2733
- return selected;
2734
- if (searchIndex.changes() !== seen ||
2735
- JSON.stringify(covered(filter)) !== JSON.stringify(cover))
2736
- continue;
2737
- return {
2738
- ...searchIndex.search(plan, selected === null ? null : selected.documents, named),
2739
- coverage: {
2740
- rooms: covering,
2741
- conversations: named,
2742
- index: searchIndex.generation(),
2743
- overlay: cover.coverage.overlay,
2744
- },
2745
- };
2746
- }
2747
- }
2748
- const watch = (topic, input, deliver) => {
2749
- if (topic === 'room-drafts') {
2750
- const listener = { notify: deliver, ready: false };
2751
- roomDraftListeners.add(listener);
2752
- void exclusive(async () => {
2753
- await openLocal();
2754
- if (closed || !roomDraftListeners.has(listener))
2755
- return;
2756
- listener.ready = true;
2757
- }).then(() => {
2758
- if (closed || !listener.ready || !roomDraftListeners.has(listener))
2759
- return;
2760
- listener.notify(snapshotRoomDrafts());
2761
- }, () => { });
2762
- return () => {
2763
- roomDraftListeners.delete(listener);
2764
- };
2765
- }
2766
- if (topic === 'conversation') {
2767
- return conversations.watch(input[0], input[1], deliver);
2768
- }
2769
- if (topic === 'conversations')
2770
- return conversations.watchList(deliver);
2771
- if (topic === 'agents')
2772
- return conversations.watchAgents(deliver);
2773
- if (topic === 'presentation') {
2774
- const listener = {
2775
- app: input[0],
2776
- notify: deliver,
2777
- ready: false,
2778
- };
2779
- presentationListeners.add(listener);
2780
- void exclusive(async () => {
2781
- await openLocal();
2782
- }).then(() => {
2783
- if (closed || !presentationListeners.has(listener))
2784
- return;
2785
- listener.ready = true;
2786
- listener.notify(presentationOf(listener.app));
2787
- }, () => { });
2788
- return () => {
2789
- presentationListeners.delete(listener);
2790
- };
2791
- }
2792
- if (topic === 'rooms') {
2793
- roomListeners.add(deliver);
2794
- deliver(listed());
2795
- return () => {
2796
- roomListeners.delete(deliver);
2797
- };
2798
- }
2799
- if (topic === 'connection-refusal') {
2800
- refusalListeners.add(deliver);
2801
- deliver(refusal);
2802
- return () => {
2803
- refusalListeners.delete(deliver);
2804
- };
2805
- }
2806
- if (topic === 'storage-loss') {
2807
- storageLossListeners.add(deliver);
2808
- deliver(storageLoss);
2809
- return () => {
2810
- storageLossListeners.delete(deliver);
2811
- };
2812
- }
2813
- if (topic === 'sync-status') {
2814
- statusListeners.add(deliver);
2815
- deliver(shown);
2816
- return () => {
2817
- statusListeners.delete(deliver);
2818
- };
2819
- }
2820
- if (topic === 'service-mode') {
2821
- serviceModeListeners.add(deliver);
2822
- deliver(serviceMode);
2823
- return () => {
2824
- serviceModeListeners.delete(deliver);
2825
- };
2826
- }
2827
- if (topic === 'member') {
2828
- memberListeners.add(deliver);
2829
- deliver(member);
2830
- return () => {
2831
- memberListeners.delete(deliver);
2832
- };
2833
- }
2834
- if (topic === 'queue') {
2835
- const queue = { locator: input[0], notify: deliver };
2836
- queueSubscriptions.add(queue);
2837
- queue.notify(entriesOf(queue.locator));
2838
- return () => {
2839
- queueSubscriptions.delete(queue);
2840
- };
2841
- }
2842
- return watchPlan(planned(input[0], null), deliver);
2843
- };
2844
- const md = {
2845
- loaded() {
2846
- return openLocal().then(() => undefined);
2847
45
  },
2848
- rooms() {
2849
- return listed();
46
+ roomTypes,
47
+ builtIn,
48
+ typedPaths,
49
+ persistence: shell.persistence,
50
+ lost: presence.lost,
51
+ losing: presence.losing,
52
+ opening: (local) => open(runtime, local),
53
+ unpublished: uncaught,
54
+ changed(set) {
55
+ if (set.unindexed.length > 0)
56
+ prepare(runtime, set.unindexed);
57
+ for (const locator of set.due)
58
+ submitLater(runtime, locator);
59
+ return publishChangeSet(runtime, set);
2850
60
  },
2851
- query: ((input) => rowsQuery(planned(input, null))),
2852
- search: (input) => searched(input, null),
2853
- types(locator) {
2854
- return {
2855
- collection: modelTypes(modelOf(locator)),
2856
- typedSelection: rooms.get(locator)?.typedSelection === undefined ||
2857
- rooms.get(locator)?.typedSelection === null
2858
- ? null
2859
- : modelTypes(modelOf(locator, 'typed-selection')),
2860
- };
2861
- },
2862
- roomDraft: {
2863
- list() {
2864
- return exclusive(async () => {
2865
- await openLocal();
2866
- return snapshotRoomDrafts();
2867
- }).then((listed) => {
2868
- publishRoomDrafts(true);
2869
- return listed;
2870
- });
2871
- },
2872
- read(room, path) {
2873
- const record = roomDrafts.get(room)?.get(path);
2874
- return record === undefined ? null : roomDraftOf(record);
2875
- },
2876
- write(room, path, revision, input) {
2877
- return exclusive(async () => {
2878
- const parts = readParts(input);
2879
- if (path === '' || normalize(path) !== path)
2880
- throw runtimeError('room-draft-malformed');
2881
- const local = await openLocal();
2882
- const current = roomDrafts.get(room)?.get(path);
2883
- if ((current?.revision ?? null) !== revision) {
2884
- return staleWrite(current === undefined ? null : roomDraftOf(current));
2885
- }
2886
- const file = parts.file === undefined
2887
- ? undefined
2888
- : await heldFile(parts.file.blob, binaryBase(replicas.get(room)?.visible, path));
2889
- const record = {
2890
- locator: room,
2891
- path,
2892
- revision: roomDraftRevision(),
2893
- parts: held(current?.parts, parts, file),
2894
- returned: current?.returned ?? null,
2895
- };
2896
- await local.keepRoomDraft(record);
2897
- rememberRoomDraft(record);
2898
- publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
2899
- return { outcome: 'written', roomDraft: roomDraftOf(record) };
2900
- });
2901
- },
2902
- discard(room, path, revision) {
2903
- return exclusive(async () => {
2904
- const local = await openLocal();
2905
- const current = roomDrafts.get(room)?.get(path);
2906
- if (current === undefined || current.revision !== revision) {
2907
- return staleWrite(current === undefined ? null : roomDraftOf(current));
2908
- }
2909
- await local.discardRoomDraft(room, path);
2910
- forgetRoomDraft(room, path);
2911
- publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
2912
- moveTo({ ...status, ...attention() });
2913
- return { outcome: 'written', roomDraft: null };
2914
- });
2915
- },
2916
- },
2917
- conversation: conversations.surface,
2918
- chatDraft: conversations.chatDraft,
2919
- presentation: {
2920
- read(app) {
2921
- return exclusive(async () => {
2922
- await openLocal();
2923
- return presentationOf(app);
2924
- });
61
+ deliverReadiness: (owed, rooms) => deliverOwedReadiness(runtime, owed, rooms),
62
+ spaceFreed: () => spaceFreed(runtime),
63
+ });
64
+ const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
65
+ const runtime = {
66
+ store,
67
+ socket,
68
+ presence,
69
+ recovery,
70
+ retries,
71
+ shell,
72
+ localSaves: adapter.localSaves === true,
73
+ roomTypes,
74
+ builtIn,
75
+ typedPaths,
76
+ writeRule: (declaration) => writeRule(declaration, adapter.pathRule ?? (() => true), adapter.localSaves === true),
77
+ usableWhenHeld: options.usableWhenHeld ?? [],
78
+ build: options.build ?? '',
79
+ conversations: createLiveConversations({
80
+ call: (name, input, attempt) => invokeAction(runtime, name, input, { attempt }),
81
+ history: (request) => readConversationHistory(runtime, request),
82
+ durably: store.durably,
83
+ local: store.open,
84
+ again: () => {
85
+ void socket.ready.then(() => scheduleCurrency(runtime));
2925
86
  },
2926
- write(app, value) {
2927
- return exclusive(async () => {
2928
- const local = await openLocal();
2929
- const stored = structuredClone(value);
2930
- await local.present({ app, value: stored });
2931
- presentations.set(app, stored);
2932
- publishChangeSet(changeSet('', null, null, { presentation: app }));
2933
- });
87
+ settled: () => {
88
+ show(runtime);
2934
89
  },
2935
- },
2936
- save(room, named, writtenBy) {
2937
- return exclusive(() => saveRoomDrafts(room, named, writtenBy));
2938
- },
2939
- history(locator, path, query) {
2940
- return readHistory(locator, path, query);
2941
- },
2942
- binaryFile: {
2943
- read: readBinaryPath,
2944
- readReference: readBinaryReference,
2945
- },
2946
- call(name, input) {
2947
- return invokeAction(name, input);
2948
- },
2949
- reached(locator, roomVersion, path) {
2950
- return path === undefined
2951
- ? reachedBy(locator, roomVersion)
2952
- : writtenAfter(locator, roomVersion, path);
2953
- },
2954
- log() {
2955
- return journal.export();
2956
- },
2957
- persistence() {
2958
- return options.shell.persistentStorage;
2959
- },
2960
- observe: observing(watch),
2961
- async reauthorize() {
2962
- if (refusal === null || !refusal.reauthorizationOffered)
2963
- return;
2964
- journal.record('connection reauthorize');
2965
- await Promise.all([...lanes.values()]
2966
- .filter((held) => held.generation === generation)
2967
- .map((held) => held.chain));
2968
- await mutations;
2969
- authorization = undefined;
2970
- discoveryPaused = false;
2971
- if (options.adapter.roomStream === undefined)
2972
- await sync('unstreamed');
2973
- else
2974
- await task(OPENING_LANE, ensureAuthorization);
2975
- },
2976
- };
2977
- const reachable = {
2978
- md,
2979
- watch,
2980
- referrerScope: (referrer) => options.adapter.referrerScope(referrer),
2981
- plan: planned,
2982
- query: (plan) => rowsQuery(plan),
2983
- watchPlan,
2984
- search: searched,
90
+ stream: socket.conversations,
91
+ publish: store.changed,
92
+ online: socket.reached,
93
+ full: store.space.full,
94
+ closed: socket.closed,
95
+ unanswered: () => resendLater(runtime),
96
+ heard: (operation, lost) => socket.lanes.settle(operation, lost),
97
+ recovery: recovery.scope(),
98
+ retries,
99
+ }),
100
+ liveQueries: createLiveQueries((plan, cover) => answeredPlan(runtime, plan, cover), store.covering),
101
+ figures: createFigures({
102
+ cover: () => figuresCover(runtime),
103
+ rooms: (rooms, since) => roomTallies(runtime, rooms, since),
104
+ conversations: (conversations, since) => store.ask({ kind: 'conversation.tallies', conversations, since }),
105
+ }),
106
+ lastAnswers: readAnswers({
107
+ database: store,
108
+ build: options.build ?? '',
109
+ unkept: adapter.unkeptReads ?? [],
110
+ leading: adapter.leadingReads ?? [],
111
+ recovery: recovery.scope(),
112
+ retries,
113
+ call: (name, input, attempt) => called(runtime, name, input, attempt),
114
+ online: socket.reached,
115
+ streaming: socket.isOpen,
116
+ closed: socket.closed,
117
+ full: store.space.full,
118
+ filled: () => filled(runtime),
119
+ }),
2985
120
  };
2986
- return { md, stop, lose: loseRooms, watch, within: (reach) => narrowed(reachable, reach) };
121
+ start(runtime, options.signal);
122
+ return composedRuntime(runtime);
2987
123
  }