@agentshouse/mdruntime 0.1.0-cff8ba8fa2542 → 0.1.0-cffcba9cd9c2f

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