@agentshouse/mdruntime 0.1.0-c49444f2a8fc6 → 0.1.0-c4ef58fbdca7b

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