@agentshouse/mdruntime 0.1.0-c94cfdbdb6f68 → 0.1.0-c95f0f04f2113

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