@agentshouse/mdruntime 0.1.0-cd3486bd400ed → 0.1.0-cd35ccee27a19

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 (343) hide show
  1. package/README.md +22 -10
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +308 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +251 -0
  8. package/dist/adapter.d.ts +195 -91
  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/attempt.d.ts.map +1 -1
  14. package/dist/attempt.js +14 -1
  15. package/dist/binary.d.ts +18 -0
  16. package/dist/binary.d.ts.map +1 -0
  17. package/dist/binary.js +8 -0
  18. package/dist/bootstrap.d.ts +31 -11
  19. package/dist/bootstrap.d.ts.map +1 -1
  20. package/dist/bootstrap.js +311 -92
  21. package/dist/cell.d.ts +6 -0
  22. package/dist/cell.d.ts.map +1 -0
  23. package/dist/cell.js +9 -0
  24. package/dist/chain.d.ts +12 -0
  25. package/dist/chain.d.ts.map +1 -0
  26. package/dist/chain.js +64 -0
  27. package/dist/change-set.d.ts +13 -14
  28. package/dist/change-set.d.ts.map +1 -1
  29. package/dist/change-set.js +17 -62
  30. package/dist/change.d.ts +42 -0
  31. package/dist/change.d.ts.map +1 -1
  32. package/dist/change.js +36 -0
  33. package/dist/chat-types.d.ts +5 -0
  34. package/dist/chat-types.d.ts.map +1 -0
  35. package/dist/chat-types.js +13 -0
  36. package/dist/client.d.ts +49 -21
  37. package/dist/client.d.ts.map +1 -1
  38. package/dist/client.js +131 -17
  39. package/dist/collection.d.ts +31 -9
  40. package/dist/collection.d.ts.map +1 -1
  41. package/dist/collection.js +49 -80
  42. package/dist/compile.d.ts +16 -0
  43. package/dist/compile.d.ts.map +1 -0
  44. package/dist/compile.js +711 -0
  45. package/dist/connection.d.ts +8 -111
  46. package/dist/connection.d.ts.map +1 -1
  47. package/dist/connection.js +107 -2218
  48. package/dist/context.d.ts +2 -0
  49. package/dist/context.d.ts.map +1 -0
  50. package/dist/context.js +7 -0
  51. package/dist/conversation-live.d.ts +136 -0
  52. package/dist/conversation-live.d.ts.map +1 -0
  53. package/dist/conversation-live.js +249 -0
  54. package/dist/conversation-row.d.ts +33 -0
  55. package/dist/conversation-row.d.ts.map +1 -0
  56. package/dist/conversation-row.js +53 -0
  57. package/dist/conversation.d.ts +71 -42
  58. package/dist/conversation.d.ts.map +1 -1
  59. package/dist/conversation.js +1068 -586
  60. package/dist/declaration.d.ts +10 -0
  61. package/dist/declaration.d.ts.map +1 -0
  62. package/dist/declaration.js +24 -0
  63. package/dist/digest.d.ts +7 -0
  64. package/dist/digest.d.ts.map +1 -0
  65. package/dist/digest.js +113 -0
  66. package/dist/discovery.d.ts +10 -0
  67. package/dist/discovery.d.ts.map +1 -0
  68. package/dist/discovery.js +148 -0
  69. package/dist/durability.d.ts +19 -1
  70. package/dist/durability.d.ts.map +1 -1
  71. package/dist/durability.js +5 -0
  72. package/dist/entry.d.ts +20 -0
  73. package/dist/entry.d.ts.map +1 -0
  74. package/dist/entry.js +161 -0
  75. package/dist/expectation.d.ts +4 -2
  76. package/dist/expectation.d.ts.map +1 -1
  77. package/dist/expectation.js +33 -11
  78. package/dist/failure.d.ts +1 -0
  79. package/dist/failure.d.ts.map +1 -1
  80. package/dist/failure.js +3 -0
  81. package/dist/figures.d.ts +49 -0
  82. package/dist/figures.d.ts.map +1 -0
  83. package/dist/figures.js +197 -0
  84. package/dist/file.d.ts +2 -19
  85. package/dist/file.d.ts.map +1 -1
  86. package/dist/file.js +7 -122
  87. package/dist/holdings.d.ts +29 -0
  88. package/dist/holdings.d.ts.map +1 -0
  89. package/dist/holdings.js +95 -0
  90. package/dist/host.d.ts +28 -4
  91. package/dist/host.d.ts.map +1 -1
  92. package/dist/host.js +163 -19
  93. package/dist/identity.d.ts +1 -0
  94. package/dist/identity.d.ts.map +1 -1
  95. package/dist/identity.js +3 -0
  96. package/dist/index.d.ts +37 -10
  97. package/dist/index.d.ts.map +1 -1
  98. package/dist/index.js +15 -0
  99. package/dist/install.d.ts +54 -0
  100. package/dist/install.d.ts.map +1 -0
  101. package/dist/install.js +538 -0
  102. package/dist/lifecycle.d.ts +7 -0
  103. package/dist/lifecycle.d.ts.map +1 -0
  104. package/dist/lifecycle.js +272 -0
  105. package/dist/listing.d.ts +48 -0
  106. package/dist/listing.d.ts.map +1 -0
  107. package/dist/listing.js +139 -0
  108. package/dist/live.d.ts +23 -0
  109. package/dist/live.d.ts.map +1 -0
  110. package/dist/live.js +165 -0
  111. package/dist/locks.d.ts +7 -0
  112. package/dist/locks.d.ts.map +1 -0
  113. package/dist/locks.js +33 -0
  114. package/dist/log.d.ts +1 -0
  115. package/dist/log.d.ts.map +1 -1
  116. package/dist/lost.d.ts +5 -0
  117. package/dist/lost.d.ts.map +1 -0
  118. package/dist/lost.js +50 -0
  119. package/dist/media.d.ts +8 -0
  120. package/dist/media.d.ts.map +1 -0
  121. package/dist/media.js +218 -0
  122. package/dist/memory.d.ts +24 -8
  123. package/dist/memory.d.ts.map +1 -1
  124. package/dist/memory.js +289 -96
  125. package/dist/messages.d.ts +49 -0
  126. package/dist/messages.d.ts.map +1 -0
  127. package/dist/messages.js +150 -0
  128. package/dist/model.d.ts +11 -12
  129. package/dist/model.d.ts.map +1 -1
  130. package/dist/model.js +7 -15
  131. package/dist/models.d.ts +10 -0
  132. package/dist/models.d.ts.map +1 -0
  133. package/dist/models.js +156 -0
  134. package/dist/name.d.ts +4 -0
  135. package/dist/name.d.ts.map +1 -0
  136. package/dist/name.js +122 -0
  137. package/dist/observation.d.ts +22 -4
  138. package/dist/observation.d.ts.map +1 -1
  139. package/dist/observation.js +127 -26
  140. package/dist/opened.d.ts +22 -0
  141. package/dist/opened.d.ts.map +1 -0
  142. package/dist/opened.js +107 -0
  143. package/dist/opening.d.ts +4 -0
  144. package/dist/opening.d.ts.map +1 -0
  145. package/dist/opening.js +59 -0
  146. package/dist/outcome.d.ts +56 -5
  147. package/dist/outcome.d.ts.map +1 -1
  148. package/dist/outcome.js +49 -0
  149. package/dist/overlay.d.ts +17 -5
  150. package/dist/overlay.d.ts.map +1 -1
  151. package/dist/overlay.js +161 -187
  152. package/dist/page-metadata.d.ts +8 -0
  153. package/dist/page-metadata.d.ts.map +1 -0
  154. package/dist/page-metadata.js +19 -0
  155. package/dist/parts.d.ts +11 -0
  156. package/dist/parts.d.ts.map +1 -0
  157. package/dist/parts.js +21 -0
  158. package/dist/paths.d.ts +2 -0
  159. package/dist/paths.d.ts.map +1 -1
  160. package/dist/paths.js +26 -0
  161. package/dist/preparation.d.ts +20 -0
  162. package/dist/preparation.d.ts.map +1 -0
  163. package/dist/preparation.js +233 -0
  164. package/dist/presence.d.ts +52 -0
  165. package/dist/presence.d.ts.map +1 -0
  166. package/dist/presence.js +220 -0
  167. package/dist/queries.d.ts +16 -0
  168. package/dist/queries.d.ts.map +1 -0
  169. package/dist/queries.js +270 -0
  170. package/dist/query.d.ts +51 -183
  171. package/dist/query.d.ts.map +1 -1
  172. package/dist/query.js +3 -464
  173. package/dist/{chat/questions.d.ts → questions.d.ts} +5 -2
  174. package/dist/questions.d.ts.map +1 -0
  175. package/dist/{chat/questions.js → questions.js} +13 -2
  176. package/dist/queue.d.ts +40 -0
  177. package/dist/queue.d.ts.map +1 -0
  178. package/dist/queue.js +455 -0
  179. package/dist/reach.d.ts +21 -5
  180. package/dist/reach.d.ts.map +1 -1
  181. package/dist/reach.js +313 -72
  182. package/dist/react.d.ts +20 -6
  183. package/dist/react.d.ts.map +1 -1
  184. package/dist/react.js +17 -18
  185. package/dist/{chat/read.d.ts → read.d.ts} +2 -0
  186. package/dist/read.d.ts.map +1 -0
  187. package/dist/{chat/read.js → read.js} +4 -0
  188. package/dist/readiness.d.ts +49 -0
  189. package/dist/readiness.d.ts.map +1 -0
  190. package/dist/readiness.js +27 -0
  191. package/dist/reads.d.ts +37 -0
  192. package/dist/reads.d.ts.map +1 -0
  193. package/dist/reads.js +174 -0
  194. package/dist/recovery.d.ts +26 -0
  195. package/dist/recovery.d.ts.map +1 -0
  196. package/dist/recovery.js +232 -0
  197. package/dist/refresh.d.ts +17 -14
  198. package/dist/refresh.d.ts.map +1 -1
  199. package/dist/refresh.js +145 -58
  200. package/dist/refusal.d.ts +5 -0
  201. package/dist/refusal.d.ts.map +1 -0
  202. package/dist/refusal.js +57 -0
  203. package/dist/replica.d.ts +53 -33
  204. package/dist/replica.d.ts.map +1 -1
  205. package/dist/replica.js +348 -166
  206. package/dist/room-draft.d.ts +55 -9
  207. package/dist/room-draft.d.ts.map +1 -1
  208. package/dist/room-draft.js +262 -73
  209. package/dist/room-drafts.d.ts +24 -0
  210. package/dist/room-drafts.d.ts.map +1 -0
  211. package/dist/room-drafts.js +217 -0
  212. package/dist/rows.d.ts +30 -35
  213. package/dist/rows.d.ts.map +1 -1
  214. package/dist/rows.js +60 -126
  215. package/dist/runtime.d.ts +34 -0
  216. package/dist/runtime.d.ts.map +1 -0
  217. package/dist/runtime.js +1 -0
  218. package/dist/save.d.ts +44 -22
  219. package/dist/save.d.ts.map +1 -1
  220. package/dist/save.js +950 -169
  221. package/dist/saves.d.ts +32 -0
  222. package/dist/saves.d.ts.map +1 -0
  223. package/dist/saves.js +413 -0
  224. package/dist/search.d.ts +15 -55
  225. package/dist/search.d.ts.map +1 -1
  226. package/dist/search.js +10 -121
  227. package/dist/seed.d.ts +25 -0
  228. package/dist/seed.d.ts.map +1 -0
  229. package/dist/seed.js +145 -0
  230. package/dist/shared.d.ts +10 -0
  231. package/dist/shared.d.ts.map +1 -0
  232. package/dist/shared.js +339 -0
  233. package/dist/shown.d.ts +32 -0
  234. package/dist/shown.d.ts.map +1 -0
  235. package/dist/shown.js +290 -0
  236. package/dist/socket/admission.d.ts +7 -0
  237. package/dist/socket/admission.d.ts.map +1 -0
  238. package/dist/socket/admission.js +39 -0
  239. package/dist/socket/answers.d.ts +6 -0
  240. package/dist/socket/answers.d.ts.map +1 -0
  241. package/dist/socket/answers.js +20 -0
  242. package/dist/socket/lanes.d.ts +20 -0
  243. package/dist/socket/lanes.d.ts.map +1 -0
  244. package/dist/socket/lanes.js +77 -0
  245. package/dist/socket/reads.d.ts +10 -0
  246. package/dist/socket/reads.d.ts.map +1 -0
  247. package/dist/socket/reads.js +31 -0
  248. package/dist/socket/session.d.ts +38 -0
  249. package/dist/socket/session.d.ts.map +1 -0
  250. package/dist/socket/session.js +23 -0
  251. package/dist/socket.d.ts +67 -0
  252. package/dist/socket.d.ts.map +1 -0
  253. package/dist/socket.js +170 -0
  254. package/dist/source.d.ts +61 -0
  255. package/dist/source.d.ts.map +1 -0
  256. package/dist/source.js +82 -0
  257. package/dist/status.d.ts +5 -2
  258. package/dist/status.d.ts.map +1 -1
  259. package/dist/status.js +3 -7
  260. package/dist/store/installs.d.ts +30 -0
  261. package/dist/store/installs.d.ts.map +1 -0
  262. package/dist/store/installs.js +34 -0
  263. package/dist/store/readiness.d.ts +10 -0
  264. package/dist/store/readiness.d.ts.map +1 -0
  265. package/dist/store/readiness.js +52 -0
  266. package/dist/store/records.d.ts +212 -0
  267. package/dist/store/records.d.ts.map +1 -0
  268. package/dist/store/records.js +430 -0
  269. package/dist/store/room-held.d.ts +8 -0
  270. package/dist/store/room-held.d.ts.map +1 -0
  271. package/dist/store/room-held.js +17 -0
  272. package/dist/store/space.d.ts +11 -0
  273. package/dist/store/space.d.ts.map +1 -0
  274. package/dist/store/space.js +41 -0
  275. package/dist/store/watchers.d.ts +17 -0
  276. package/dist/store/watchers.d.ts.map +1 -0
  277. package/dist/store/watchers.js +56 -0
  278. package/dist/store/writes.d.ts +24 -0
  279. package/dist/store/writes.d.ts.map +1 -0
  280. package/dist/store/writes.js +125 -0
  281. package/dist/store.d.ts +109 -117
  282. package/dist/store.d.ts.map +1 -1
  283. package/dist/store.js +155 -494
  284. package/dist/stored.d.ts +156 -0
  285. package/dist/stored.d.ts.map +1 -0
  286. package/dist/stored.js +649 -0
  287. package/dist/surface.d.ts +88 -0
  288. package/dist/surface.d.ts.map +1 -0
  289. package/dist/surface.js +223 -0
  290. package/dist/sync.d.ts +19 -0
  291. package/dist/sync.d.ts.map +1 -0
  292. package/dist/sync.js +521 -0
  293. package/dist/tables.d.ts +412 -0
  294. package/dist/tables.d.ts.map +1 -0
  295. package/dist/tables.js +737 -0
  296. package/dist/tallies.d.ts +18 -0
  297. package/dist/tallies.d.ts.map +1 -0
  298. package/dist/tallies.js +129 -0
  299. package/dist/transfer.d.ts +0 -5
  300. package/dist/transfer.d.ts.map +1 -1
  301. package/dist/turn.d.ts +9 -0
  302. package/dist/turn.d.ts.map +1 -0
  303. package/dist/turn.js +75 -0
  304. package/dist/validation.d.ts +29 -0
  305. package/dist/validation.d.ts.map +1 -0
  306. package/dist/validation.js +104 -0
  307. package/dist/windows.d.ts +11 -0
  308. package/dist/windows.d.ts.map +1 -0
  309. package/dist/windows.js +16 -0
  310. package/dist/wire.d.ts +220 -34
  311. package/dist/wire.d.ts.map +1 -1
  312. package/dist/wire.js +73 -31
  313. package/package.json +27 -15
  314. package/AGENTS.md +0 -290
  315. package/dist/attachment.d.ts +0 -3
  316. package/dist/attachment.d.ts.map +0 -1
  317. package/dist/attachment.js +0 -8
  318. package/dist/chat/acts.d.ts +0 -34
  319. package/dist/chat/acts.d.ts.map +0 -1
  320. package/dist/chat/acts.js +0 -36
  321. package/dist/chat/messages.d.ts +0 -21
  322. package/dist/chat/messages.d.ts.map +0 -1
  323. package/dist/chat/messages.js +0 -151
  324. package/dist/chat/overlay.d.ts +0 -31
  325. package/dist/chat/overlay.d.ts.map +0 -1
  326. package/dist/chat/overlay.js +0 -97
  327. package/dist/chat/parts.d.ts +0 -57
  328. package/dist/chat/parts.d.ts.map +0 -1
  329. package/dist/chat/parts.js +0 -119
  330. package/dist/chat/process.d.ts +0 -20
  331. package/dist/chat/process.d.ts.map +0 -1
  332. package/dist/chat/process.js +0 -32
  333. package/dist/chat/provider.d.ts +0 -57
  334. package/dist/chat/provider.d.ts.map +0 -1
  335. package/dist/chat/provider.js +0 -637
  336. package/dist/chat/questions.d.ts.map +0 -1
  337. package/dist/chat/read.d.ts.map +0 -1
  338. package/dist/chat.d.ts +0 -7
  339. package/dist/chat.d.ts.map +0 -1
  340. package/dist/chat.js +0 -2
  341. package/dist/read-side.d.ts +0 -18
  342. package/dist/read-side.d.ts.map +0 -1
  343. package/dist/read-side.js +0 -433
package/dist/sync.js ADDED
@@ -0,0 +1,521 @@
1
+ import { resendCalls, resendLater } from './actions.js';
2
+ import { catchUpConversations, reconcileConversations, resendConversations, } from './conversation.js';
3
+ import { declared, sameDeclaration } from './declaration.js';
4
+ import { applyDiscovery, discoverRooms, malformed, signIn } from './discovery.js';
5
+ import { behind, caughtUpTo, publish, pullBootstrap, pullRefresh, reachedBy, replaced, standingAt, } from './install.js';
6
+ import { heldRooms, name } from './listing.js';
7
+ import { retainModel, revoked, scoped } from './models.js';
8
+ import { refuse } from './refusal.js';
9
+ import { emitRooms, moveTo, rest, roomUnreadable, submissionDue, transferring, AUTHORITY_TOPIC, } from './shown.js';
10
+ import { unconfirmed } from './transfer.js';
11
+ export const OPENING_LANE = 'opening';
12
+ async function advance(runtime, room, local, authorized, running, unheld) {
13
+ const { store, socket, presence } = runtime;
14
+ const live = () => running() && !store.space.full();
15
+ if (!live())
16
+ return unconfirmed;
17
+ const held = unheld === undefined ? await store.record(room.locator) : undefined;
18
+ if (socket.streamed && (held !== undefined || !store.installs.of(room.locator).beyondHorizon)) {
19
+ presence.stood(room.locator, false);
20
+ await name(runtime, room.locator);
21
+ return null;
22
+ }
23
+ const refresh = socket.reads.refresh;
24
+ if (held !== undefined && refresh !== undefined) {
25
+ const refreshed = await store.ahead((hold) => refreshedRoom(runtime, room, local, authorized, held, live, hold, refresh));
26
+ if (refreshed === 'behind')
27
+ return advance(runtime, room, local, authorized, live);
28
+ if (refreshed !== 'beyond-horizon')
29
+ return refreshed;
30
+ if (presence.standing(room.locator).reloadDue)
31
+ return unconfirmed;
32
+ replaced(store, [room.locator]);
33
+ }
34
+ const prior = held === undefined ? undefined : await store.record(room.locator);
35
+ const from = standingAt(store, room.locator, prior);
36
+ const bootstrapped = await pullBootstrap(runtime, authorized, room, local, live, prior, socket.reads, unheld);
37
+ if (bootstrapped.kind !== 'staged')
38
+ return live() ? bootstrapped : unconfirmed;
39
+ const landing = { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from };
40
+ if (await publish(runtime, local, room, bootstrapped.record, landing, live)) {
41
+ return live() ? null : unconfirmed;
42
+ }
43
+ return (await reachedBy(runtime, room.locator, bootstrapped.record.roomVersion))
44
+ ? null
45
+ : advance(runtime, room, local, authorized, live);
46
+ }
47
+ async function refreshedRoom(runtime, room, local, authorized, held, live, hold, refresh) {
48
+ const { store, socket, presence } = runtime;
49
+ const refreshed = await pullRefresh(runtime, authorized, held, live, hold, refresh);
50
+ if (!live())
51
+ return unconfirmed;
52
+ if (refreshed.kind === 'still-current') {
53
+ presence.stood(room.locator, false);
54
+ if (presence.readable(room.locator))
55
+ emitRooms(runtime);
56
+ return null;
57
+ }
58
+ if (refreshed.kind === 'advanced') {
59
+ const landing = {
60
+ kind: 'refreshed',
61
+ from: refreshed.from,
62
+ remodelled: refreshed.remodelled,
63
+ };
64
+ if (!(await publish(runtime, local, room, refreshed.record, landing, live))) {
65
+ const now = (await store.record(room.locator))?.roomVersion;
66
+ const reached = now !== undefined && socket.reads.reached(room.locator, now, refreshed.record.roomVersion);
67
+ return reached ? null : 'behind';
68
+ }
69
+ await retainModel(runtime, room.locator, declared(room), null);
70
+ return null;
71
+ }
72
+ return refreshed.kind === 'beyond-horizon' ? 'beyond-horizon' : refreshed;
73
+ }
74
+ export function advanced(runtime, room, local, authorized) {
75
+ return runtime.socket.lanes.inRoom(room.locator, (live) => advance(runtime, room, local, authorized, live));
76
+ }
77
+ export async function withhold(runtime, withheld, room, local, progress) {
78
+ const { presence, journal } = runtime;
79
+ if (withheld.kind === 'unconfirmed' || presence.lost(room.locator))
80
+ return;
81
+ if (withheld.kind === 'not-ready') {
82
+ progress.complete = false;
83
+ askAgain(runtime, room.locator);
84
+ return;
85
+ }
86
+ if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
87
+ const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
88
+ presence.stood(room.locator, true, { reset });
89
+ repairLater(runtime, room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
90
+ }
91
+ else
92
+ presence.stood(room.locator, true, null);
93
+ if (withheld.kind === 'scoped-refusal') {
94
+ await scoped(runtime, withheld, room.locator, declared(room));
95
+ }
96
+ else if (withheld.kind === 'transport-uncertainty') {
97
+ progress.reachable = false;
98
+ }
99
+ else if (withheld.kind === 'retryable-refusal') {
100
+ journal.record(`refusal ${withheld.kind} ${withheld.code}`);
101
+ }
102
+ else if (withheld.kind === 'room-not-readable') {
103
+ roomUnreadable(runtime, room.locator);
104
+ }
105
+ else if (withheld.kind === 'revoked-authorization') {
106
+ await refuse(runtime, 'revoked-authorization');
107
+ }
108
+ }
109
+ function askAgain(runtime, locator) {
110
+ const { store, socket, presence } = runtime;
111
+ presence.askAgain(locator, () => {
112
+ const room = presence.room(locator);
113
+ if (socket.closed() ||
114
+ socket.stopping() ||
115
+ (socket.streamed && !socket.isOpen()) ||
116
+ room === undefined)
117
+ return;
118
+ const progress = { complete: true, reachable: true };
119
+ void store
120
+ .open()
121
+ .then((local) => socket.lanes.roomTask(locator, (live) => loadRoom(runtime, room, local, progress, live)))
122
+ .then(() => rest(runtime, progress), () => undefined);
123
+ });
124
+ }
125
+ async function prepareRoom(runtime, room, progress, prior) {
126
+ const { socket, journal } = runtime;
127
+ const { session } = socket;
128
+ if (socket.closed() || session.now().authorization === undefined) {
129
+ progress.complete = false;
130
+ return false;
131
+ }
132
+ if (prior !== undefined && prior.declaration.replica.selection !== room.replica.selection) {
133
+ emitRooms(runtime);
134
+ journal.record(`selection changed ${room.locator}`);
135
+ progress.complete = false;
136
+ return false;
137
+ }
138
+ const declaration = declared(room);
139
+ if (prior !== undefined && !sameDeclaration(prior.declaration, declaration)) {
140
+ await retainModel(runtime, room.locator, declaration, null);
141
+ }
142
+ const { paused, authorization } = session.now();
143
+ if (paused || authorization === undefined)
144
+ return false;
145
+ return room.replica.admission === 'admitted';
146
+ }
147
+ async function synchronize(runtime, listed, local, progress, attempt, live) {
148
+ const { store, socket } = runtime;
149
+ const heldNow = await store.rooms(listed.map(({ locator }) => locator));
150
+ const prepared = await Promise.allSettled(listed.map((room) => prepareRoom(runtime, room, progress, heldNow.get(room.locator)?.record)));
151
+ if (!live())
152
+ return;
153
+ const ready = listed.filter((_, index) => {
154
+ const outcome = prepared[index];
155
+ return outcome.status === 'fulfilled' && outcome.value;
156
+ });
157
+ if (attempt === 'opened') {
158
+ const held = await heldRooms(runtime, ready);
159
+ if (!live())
160
+ return;
161
+ socket.hand(held);
162
+ }
163
+ const kept = attempt === 'authorized'
164
+ ? new Set((await store.rooms(ready.map(({ locator }) => locator))).keys())
165
+ : new Set();
166
+ for (const room of ready) {
167
+ if (attempt === 'opened' || kept.has(room.locator)) {
168
+ if (attempt === 'authorized')
169
+ await name(runtime, room.locator);
170
+ continue;
171
+ }
172
+ void socket.lanes
173
+ .roomTask(room.locator, (live) => loadRoom(runtime, room, local, progress, live))
174
+ .then(() => rest(runtime, progress));
175
+ }
176
+ const failed = prepared.find((room) => room.status === 'rejected');
177
+ if (failed !== undefined)
178
+ throw failed.reason;
179
+ }
180
+ async function caughtUp(runtime, room, local, progress, live) {
181
+ const { socket } = runtime;
182
+ const { authorization } = socket.session.now();
183
+ if (socket.streamed || authorization === undefined)
184
+ return;
185
+ if (!live() || !(await behind(runtime, room)))
186
+ return;
187
+ const withheld = await advance(runtime, room, local, authorization, live);
188
+ if (withheld !== null)
189
+ await withhold(runtime, withheld, room, local, progress);
190
+ }
191
+ export async function loadRoom(runtime, room, local, progress, live, unheld) {
192
+ const { store, socket } = runtime;
193
+ const { authorization } = socket.session.now();
194
+ if (!live() || authorization === undefined)
195
+ return;
196
+ const prior = unheld === undefined ? await store.record(room.locator) : undefined;
197
+ if (prior !== undefined) {
198
+ if (!(await prepareRoom(runtime, room, progress, prior)))
199
+ return;
200
+ if (revoked(prior.model)) {
201
+ await reload(runtime, room, local, progress, live);
202
+ return;
203
+ }
204
+ }
205
+ const withheld = await advance(runtime, room, local, authorization, live, unheld);
206
+ if (withheld !== null) {
207
+ await withhold(runtime, withheld, room, local, progress);
208
+ return;
209
+ }
210
+ await caughtUp(runtime, room, local, progress, live);
211
+ }
212
+ export async function reload(runtime, room, local, progress, live) {
213
+ const { store, socket, presence } = runtime;
214
+ try {
215
+ if (live())
216
+ presence.reload(room.locator, false);
217
+ const { authorization } = socket.session.now();
218
+ if (!live() || authorization === undefined)
219
+ return;
220
+ const prior = await store.record(room.locator);
221
+ const from = standingAt(store, room.locator, prior);
222
+ const bootstrapped = await pullBootstrap(runtime, authorization, room, local, live, prior);
223
+ if (bootstrapped.kind !== 'staged') {
224
+ if (live())
225
+ await withhold(runtime, bootstrapped, room, local, progress);
226
+ return;
227
+ }
228
+ await publish(runtime, local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
229
+ await caughtUp(runtime, room, local, progress, live);
230
+ }
231
+ finally {
232
+ caughtUpTo(runtime, room.locator);
233
+ }
234
+ }
235
+ function repairLater(runtime, locator, when) {
236
+ const { socket, presence, recovery } = runtime;
237
+ if (!socket.isOpen() || presence.standing(locator).repairing)
238
+ return;
239
+ void recovery.attempt(`repair ${locator}`, (signal) => repairPass(runtime, locator, signal), when);
240
+ }
241
+ async function repairPass(runtime, locator, signal) {
242
+ const { store, socket, presence } = runtime;
243
+ const room = presence.room(locator);
244
+ if (!socket.isOpen() ||
245
+ !socket.continuing() ||
246
+ socket.stopping() ||
247
+ !presence.standing(locator).stale)
248
+ return 'done';
249
+ if (room === undefined || room.replica.admission !== 'admitted')
250
+ return 'done';
251
+ const progress = { complete: true, reachable: true };
252
+ presence.repairing(locator, true);
253
+ try {
254
+ const local = await store.open();
255
+ await socket.lanes.roomTask(locator, async (live) => {
256
+ const { authorization } = socket.session.now();
257
+ if (!live() || authorization === undefined)
258
+ return;
259
+ const withheld = await advance(runtime, room, local, authorization, live);
260
+ if (withheld !== null && live())
261
+ await withhold(runtime, withheld, room, local, progress);
262
+ });
263
+ }
264
+ catch {
265
+ presence.stood(locator, true);
266
+ progress.reachable = false;
267
+ }
268
+ finally {
269
+ presence.repairing(locator, false);
270
+ }
271
+ if (signal.aborted)
272
+ return 'done';
273
+ await rest(runtime, progress);
274
+ if (!presence.standing(locator).stale) {
275
+ await submissionDue(runtime, [locator]);
276
+ return 'done';
277
+ }
278
+ const { repairable } = presence.standing(locator);
279
+ if (repairable === null)
280
+ return 'done';
281
+ return repairable.reset === undefined ? 'again' : { at: Date.parse(repairable.reset) };
282
+ }
283
+ export async function finishOpening(runtime, progress, live) {
284
+ const { socket, presence, conversations, recovery } = runtime;
285
+ try {
286
+ if (!live()) {
287
+ progress.complete = false;
288
+ return;
289
+ }
290
+ await submissionDue(runtime);
291
+ for (const locator of presence.repairsOwed())
292
+ repairLater(runtime, locator, {});
293
+ await reconcileConversations(conversations);
294
+ const reading = { complete: true, reachable: true };
295
+ await catchUpConversations(conversations, reading);
296
+ if (progress.reachable && reading.reachable)
297
+ await resendConversations(conversations, reading);
298
+ if (progress.reachable) {
299
+ void recovery.attempt('calls', (signal) => resendCalls(runtime, signal));
300
+ }
301
+ if (!reading.reachable) {
302
+ progress.reachable = false;
303
+ resendLater(runtime);
304
+ }
305
+ }
306
+ catch {
307
+ progress.complete = false;
308
+ }
309
+ finally {
310
+ if (live()) {
311
+ socket.session.set({ complete: progress.complete });
312
+ await rest(runtime, progress, live);
313
+ }
314
+ }
315
+ }
316
+ export function sync(runtime, attempt, found) {
317
+ const { store, socket } = runtime;
318
+ const { session } = socket;
319
+ return socket.lanes.task(OPENING_LANE, async (live) => {
320
+ if (!live()) {
321
+ socket.readyFromStore();
322
+ return;
323
+ }
324
+ const progress = { complete: true, reachable: true };
325
+ session.set({
326
+ underway: attempt === 'opened' ? progress : null,
327
+ ...(attempt === 'opened' ? { streamed: progress } : {}),
328
+ complete: false,
329
+ recorded: false,
330
+ });
331
+ moveTo(runtime, transferring(runtime));
332
+ try {
333
+ const local = await store.open();
334
+ if (!live())
335
+ return;
336
+ await socket.authorize();
337
+ if (!live())
338
+ return;
339
+ const listed = await discoverRooms(runtime, local, progress, live, found);
340
+ if (listed !== null)
341
+ await synchronize(runtime, listed, local, progress, attempt, live);
342
+ }
343
+ catch {
344
+ progress.complete = false;
345
+ if (attempt === 'opened' && !socket.isHanded())
346
+ progress.reachable = false;
347
+ }
348
+ finally {
349
+ socket.notifyReady();
350
+ }
351
+ if (!live())
352
+ return;
353
+ if (attempt !== 'opened') {
354
+ await finishOpening(runtime, progress, live);
355
+ return;
356
+ }
357
+ if (socket.isHanded() || socket.closed() || progress.reachable)
358
+ return;
359
+ session.set({ underway: null });
360
+ await rest(runtime, progress, live);
361
+ });
362
+ }
363
+ export function resynchronize(runtime) {
364
+ const { store, socket, presence } = runtime;
365
+ return socket.lanes.task(OPENING_LANE, async (live) => {
366
+ if (!live() || !socket.isAnnounced())
367
+ return;
368
+ const progress = { complete: true, reachable: true };
369
+ moveTo(runtime, transferring(runtime));
370
+ try {
371
+ const local = await store.open();
372
+ if (!live())
373
+ return;
374
+ await synchronize(runtime, presence.rooms(), local, progress, 'unstreamed', live);
375
+ }
376
+ catch {
377
+ progress.complete = false;
378
+ }
379
+ if (live())
380
+ await finishOpening(runtime, progress, live);
381
+ });
382
+ }
383
+ export function countAuthorityChange({ store, socket, lastAnswers }) {
384
+ const authorityChanges = socket.session.now().authorityChanges + 1;
385
+ socket.session.set({ authorityChanges });
386
+ store.tell(AUTHORITY_TOPIC, authorityChanges);
387
+ lastAnswers.again();
388
+ }
389
+ export function changeAuthority(runtime, change) {
390
+ const { store, socket, journal } = runtime;
391
+ const { session } = socket;
392
+ return socket.lanes.task(OPENING_LANE, async (live) => {
393
+ if (!live() || !socket.isAnnounced())
394
+ return;
395
+ countAuthorityChange(runtime);
396
+ const progress = { complete: true, reachable: true };
397
+ moveTo(runtime, transferring(runtime));
398
+ try {
399
+ const local = await store.open();
400
+ if (!live())
401
+ return;
402
+ const invalid = malformed(change.rooms);
403
+ if (invalid !== null) {
404
+ journal.record(invalid);
405
+ await refuse(runtime, 'missing-required-guarantee');
406
+ return;
407
+ }
408
+ if (change.member !== undefined)
409
+ await signIn(runtime, local, change.member);
410
+ const discoveries = session.now().discoveries + 1;
411
+ session.set({ discoveries });
412
+ const listed = await applyDiscovery(runtime, change.rooms, discoveries, change.left);
413
+ await synchronize(runtime, listed, local, progress, 'authorized', live);
414
+ }
415
+ catch {
416
+ progress.complete = false;
417
+ }
418
+ if (!live())
419
+ return;
420
+ if (!progress.complete)
421
+ session.set({ complete: false });
422
+ await rest(runtime, progress, live);
423
+ });
424
+ }
425
+ async function installCarried(runtime, room, local, change, live, progress) {
426
+ const { store, socket } = runtime;
427
+ const { authorization, underway } = socket.session.now();
428
+ if (authorization === undefined)
429
+ return;
430
+ if (underway === null)
431
+ moveTo(runtime, transferring(runtime));
432
+ const prior = await store.record(room.locator);
433
+ const from = standingAt(store, room.locator, prior);
434
+ const page = await change.read();
435
+ const carried = {
436
+ bootstrap: () => Promise.resolve({
437
+ kind: 'bootstrap',
438
+ roomVersion: page.roomVersion,
439
+ ...(page.models === undefined ? {} : { models: page.models }),
440
+ documents: page.documents,
441
+ facts: page.facts,
442
+ revisions: page.revisions,
443
+ ...(page.fileIds === undefined ? {} : { fileIds: page.fileIds }),
444
+ ...(page.created === undefined ? {} : { created: page.created }),
445
+ changes: page.changes,
446
+ binaryFiles: page.binaryFiles,
447
+ ...(page.folders === undefined ? {} : { folders: page.folders }),
448
+ }),
449
+ reached: socket.reads.reached,
450
+ };
451
+ const bootstrapped = await pullBootstrap(runtime, authorization, room, local, live, prior, carried);
452
+ if (bootstrapped.kind !== 'staged') {
453
+ if (live())
454
+ await withhold(runtime, bootstrapped, room, local, progress);
455
+ return;
456
+ }
457
+ await publish(runtime, local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
458
+ }
459
+ export function applyRoom(runtime, change) {
460
+ const { store, socket, presence } = runtime;
461
+ const { session } = socket;
462
+ const { locator } = change;
463
+ if (socket.stopping())
464
+ return Promise.resolve();
465
+ return socket.lanes
466
+ .inRoom(locator, async (live) => {
467
+ await socket.answered(locator);
468
+ const progress = session.now().underway ?? { complete: true, reachable: true };
469
+ try {
470
+ const local = await store.open();
471
+ for (;;) {
472
+ if (!live())
473
+ return null;
474
+ const room = presence.room(locator);
475
+ if (room === undefined || room.replica.admission !== 'admitted')
476
+ return null;
477
+ const held = await store.record(locator);
478
+ if (held === undefined) {
479
+ if (change.from !== null)
480
+ return null;
481
+ await installCarried(runtime, room, local, change, live, progress);
482
+ return progress;
483
+ }
484
+ if (held.declaration.replica.selection !== room.replica.selection)
485
+ return null;
486
+ const at = held.roomVersion;
487
+ if (socket.reads.reached(locator, at, change.roomVersion))
488
+ return progress;
489
+ if (change.from === null || !socket.reads.reached(locator, at, change.from)) {
490
+ await name(runtime, locator);
491
+ return progress;
492
+ }
493
+ if (session.now().underway === null)
494
+ moveTo(runtime, transferring(runtime));
495
+ const writing = () => live() && !store.space.full();
496
+ const refreshed = await store.ahead((hold) => pullRefresh(runtime, session.now().authorization ?? {}, held, writing, hold, () => change.read()));
497
+ if (refreshed.kind !== 'advanced')
498
+ return progress;
499
+ const landing = {
500
+ kind: 'refreshed',
501
+ from: refreshed.from,
502
+ remodelled: refreshed.remodelled,
503
+ };
504
+ if (await publish(runtime, local, room, refreshed.record, landing, writing)) {
505
+ presence.stood(locator, false);
506
+ const declaration = declared(room);
507
+ if (!sameDeclaration(held.declaration, declaration))
508
+ await retainModel(runtime, locator, declaration, null);
509
+ return progress;
510
+ }
511
+ }
512
+ }
513
+ catch {
514
+ if (!presence.standing(locator).stale)
515
+ void name(runtime, locator);
516
+ presence.stood(locator, true);
517
+ return progress;
518
+ }
519
+ })
520
+ .then((progress) => rest(runtime, progress), () => undefined);
521
+ }