@agentshouse/mdruntime 0.1.0-cef5833f6c689 → 0.1.0-cef76ea952695

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 (264) hide show
  1. package/README.md +9 -9
  2. package/dist/acceptance.d.ts +6 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +385 -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 +28 -55
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/attached-model.d.ts +3 -10
  11. package/dist/attached-model.d.ts.map +1 -1
  12. package/dist/attached-model.js +21 -56
  13. package/dist/bootstrap.d.ts +8 -4
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +207 -76
  16. package/dist/cell.d.ts +6 -0
  17. package/dist/cell.d.ts.map +1 -0
  18. package/dist/cell.js +9 -0
  19. package/dist/change.d.ts +1 -0
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +4 -2
  22. package/dist/chat-types.d.ts +5 -0
  23. package/dist/chat-types.d.ts.map +1 -0
  24. package/dist/chat-types.js +13 -0
  25. package/dist/client.d.ts +18 -2
  26. package/dist/client.d.ts.map +1 -1
  27. package/dist/client.js +7 -2
  28. package/dist/compile.d.ts +5 -0
  29. package/dist/compile.d.ts.map +1 -1
  30. package/dist/compile.js +202 -80
  31. package/dist/connection.d.ts +5 -141
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +103 -5175
  34. package/dist/conversation-live.d.ts +135 -0
  35. package/dist/conversation-live.d.ts.map +1 -0
  36. package/dist/conversation-live.js +247 -0
  37. package/dist/conversation-row.d.ts +33 -0
  38. package/dist/conversation-row.d.ts.map +1 -0
  39. package/dist/conversation-row.js +53 -0
  40. package/dist/conversation.d.ts +30 -37
  41. package/dist/conversation.d.ts.map +1 -1
  42. package/dist/conversation.js +853 -864
  43. package/dist/discovery.d.ts +10 -0
  44. package/dist/discovery.d.ts.map +1 -0
  45. package/dist/discovery.js +175 -0
  46. package/dist/durability.d.ts +15 -0
  47. package/dist/durability.d.ts.map +1 -1
  48. package/dist/entry.d.ts.map +1 -1
  49. package/dist/entry.js +4 -3
  50. package/dist/expectation.d.ts +4 -2
  51. package/dist/expectation.d.ts.map +1 -1
  52. package/dist/expectation.js +33 -11
  53. package/dist/failure.d.ts +1 -0
  54. package/dist/failure.d.ts.map +1 -1
  55. package/dist/failure.js +3 -0
  56. package/dist/figures.d.ts +49 -0
  57. package/dist/figures.d.ts.map +1 -0
  58. package/dist/figures.js +197 -0
  59. package/dist/holdings.d.ts +29 -0
  60. package/dist/holdings.d.ts.map +1 -0
  61. package/dist/holdings.js +95 -0
  62. package/dist/host.d.ts +1 -0
  63. package/dist/host.d.ts.map +1 -1
  64. package/dist/host.js +11 -4
  65. package/dist/index.d.ts +20 -6
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +5 -1
  68. package/dist/install.d.ts +56 -0
  69. package/dist/install.d.ts.map +1 -0
  70. package/dist/install.js +556 -0
  71. package/dist/job.d.ts +1 -10
  72. package/dist/job.d.ts.map +1 -1
  73. package/dist/job.js +2 -23
  74. package/dist/lifecycle.d.ts +7 -0
  75. package/dist/lifecycle.d.ts.map +1 -0
  76. package/dist/lifecycle.js +270 -0
  77. package/dist/listing.d.ts +53 -0
  78. package/dist/listing.d.ts.map +1 -0
  79. package/dist/listing.js +176 -0
  80. package/dist/live.d.ts +4 -4
  81. package/dist/live.d.ts.map +1 -1
  82. package/dist/live.js +54 -16
  83. package/dist/locks.d.ts +9 -0
  84. package/dist/locks.d.ts.map +1 -0
  85. package/dist/locks.js +62 -0
  86. package/dist/log.d.ts +1 -0
  87. package/dist/log.d.ts.map +1 -1
  88. package/dist/lost.d.ts +5 -0
  89. package/dist/lost.d.ts.map +1 -0
  90. package/dist/lost.js +61 -0
  91. package/dist/media.d.ts +1 -1
  92. package/dist/media.d.ts.map +1 -1
  93. package/dist/media.js +3 -3
  94. package/dist/memory.d.ts +3 -1
  95. package/dist/memory.d.ts.map +1 -1
  96. package/dist/memory.js +45 -5
  97. package/dist/messages.d.ts +22 -1
  98. package/dist/messages.d.ts.map +1 -1
  99. package/dist/messages.js +75 -16
  100. package/dist/model.d.ts +1 -7
  101. package/dist/model.d.ts.map +1 -1
  102. package/dist/model.js +0 -6
  103. package/dist/models.d.ts +14 -0
  104. package/dist/models.d.ts.map +1 -0
  105. package/dist/models.js +197 -0
  106. package/dist/observation.d.ts.map +1 -1
  107. package/dist/observation.js +12 -17
  108. package/dist/opened.js +3 -3
  109. package/dist/opening.d.ts +4 -0
  110. package/dist/opening.d.ts.map +1 -0
  111. package/dist/opening.js +60 -0
  112. package/dist/outcome.d.ts +4 -1
  113. package/dist/outcome.d.ts.map +1 -1
  114. package/dist/outcome.js +3 -0
  115. package/dist/overlay.d.ts +1 -0
  116. package/dist/overlay.d.ts.map +1 -1
  117. package/dist/overlay.js +11 -0
  118. package/dist/parts.d.ts +11 -0
  119. package/dist/parts.d.ts.map +1 -0
  120. package/dist/parts.js +21 -0
  121. package/dist/paths.d.ts +1 -0
  122. package/dist/paths.d.ts.map +1 -1
  123. package/dist/paths.js +22 -0
  124. package/dist/preparation.d.ts +20 -0
  125. package/dist/preparation.d.ts.map +1 -0
  126. package/dist/preparation.js +233 -0
  127. package/dist/queries.d.ts +16 -0
  128. package/dist/queries.d.ts.map +1 -0
  129. package/dist/queries.js +272 -0
  130. package/dist/query.d.ts +10 -2
  131. package/dist/query.d.ts.map +1 -1
  132. package/dist/questions.d.ts +34 -0
  133. package/dist/questions.d.ts.map +1 -0
  134. package/dist/questions.js +122 -0
  135. package/dist/reach.d.ts +8 -6
  136. package/dist/reach.d.ts.map +1 -1
  137. package/dist/reach.js +160 -101
  138. package/dist/react.d.ts +11 -1
  139. package/dist/react.d.ts.map +1 -1
  140. package/dist/react.js +10 -1
  141. package/dist/read.d.ts +7 -0
  142. package/dist/read.d.ts.map +1 -0
  143. package/dist/read.js +17 -0
  144. package/dist/readiness.d.ts +0 -1
  145. package/dist/readiness.d.ts.map +1 -1
  146. package/dist/reads.d.ts +37 -0
  147. package/dist/reads.d.ts.map +1 -0
  148. package/dist/reads.js +174 -0
  149. package/dist/recovery.d.ts.map +1 -1
  150. package/dist/recovery.js +9 -1
  151. package/dist/refresh.d.ts +1 -1
  152. package/dist/refresh.d.ts.map +1 -1
  153. package/dist/refresh.js +8 -20
  154. package/dist/refusal.d.ts +5 -0
  155. package/dist/refusal.d.ts.map +1 -0
  156. package/dist/refusal.js +57 -0
  157. package/dist/replica.d.ts +7 -7
  158. package/dist/replica.d.ts.map +1 -1
  159. package/dist/replica.js +51 -19
  160. package/dist/room-draft.d.ts +1 -3
  161. package/dist/room-draft.d.ts.map +1 -1
  162. package/dist/room-draft.js +3 -3
  163. package/dist/room-drafts.d.ts +24 -0
  164. package/dist/room-drafts.d.ts.map +1 -0
  165. package/dist/room-drafts.js +217 -0
  166. package/dist/rows.d.ts +14 -5
  167. package/dist/rows.d.ts.map +1 -1
  168. package/dist/rows.js +50 -58
  169. package/dist/runtime.d.ts +43 -0
  170. package/dist/runtime.d.ts.map +1 -0
  171. package/dist/runtime.js +1 -0
  172. package/dist/save.d.ts +5 -1
  173. package/dist/save.d.ts.map +1 -1
  174. package/dist/save.js +34 -21
  175. package/dist/saves.d.ts +70 -0
  176. package/dist/saves.d.ts.map +1 -0
  177. package/dist/saves.js +1005 -0
  178. package/dist/seed.d.ts +11 -2
  179. package/dist/seed.d.ts.map +1 -1
  180. package/dist/seed.js +71 -1
  181. package/dist/shared.d.ts +1 -6
  182. package/dist/shared.d.ts.map +1 -1
  183. package/dist/shared.js +13 -95
  184. package/dist/shown.d.ts +31 -0
  185. package/dist/shown.d.ts.map +1 -0
  186. package/dist/shown.js +290 -0
  187. package/dist/socket/admission.d.ts +7 -0
  188. package/dist/socket/admission.d.ts.map +1 -0
  189. package/dist/socket/admission.js +39 -0
  190. package/dist/socket/answers.d.ts +6 -0
  191. package/dist/socket/answers.d.ts.map +1 -0
  192. package/dist/socket/answers.js +20 -0
  193. package/dist/socket/lanes.d.ts +20 -0
  194. package/dist/socket/lanes.d.ts.map +1 -0
  195. package/dist/socket/lanes.js +77 -0
  196. package/dist/socket/reads.d.ts +10 -0
  197. package/dist/socket/reads.d.ts.map +1 -0
  198. package/dist/socket/reads.js +31 -0
  199. package/dist/socket/rooms.d.ts +44 -0
  200. package/dist/socket/rooms.d.ts.map +1 -0
  201. package/dist/socket/rooms.js +90 -0
  202. package/dist/socket/session.d.ts +38 -0
  203. package/dist/socket/session.d.ts.map +1 -0
  204. package/dist/socket/session.js +23 -0
  205. package/dist/socket.d.ts +70 -0
  206. package/dist/socket.d.ts.map +1 -0
  207. package/dist/socket.js +175 -0
  208. package/dist/source.d.ts +61 -0
  209. package/dist/source.d.ts.map +1 -0
  210. package/dist/source.js +82 -0
  211. package/dist/status.d.ts +0 -2
  212. package/dist/status.d.ts.map +1 -1
  213. package/dist/status.js +0 -6
  214. package/dist/store/fences.d.ts +16 -0
  215. package/dist/store/fences.d.ts.map +1 -0
  216. package/dist/store/fences.js +26 -0
  217. package/dist/store/installs.d.ts +30 -0
  218. package/dist/store/installs.d.ts.map +1 -0
  219. package/dist/store/installs.js +34 -0
  220. package/dist/store/readiness.d.ts +10 -0
  221. package/dist/store/readiness.d.ts.map +1 -0
  222. package/dist/store/readiness.js +52 -0
  223. package/dist/store/records.d.ts +210 -0
  224. package/dist/store/records.d.ts.map +1 -0
  225. package/dist/store/records.js +428 -0
  226. package/dist/store/room-held.d.ts +8 -0
  227. package/dist/store/room-held.d.ts.map +1 -0
  228. package/dist/store/room-held.js +17 -0
  229. package/dist/store/space.d.ts +11 -0
  230. package/dist/store/space.d.ts.map +1 -0
  231. package/dist/store/space.js +41 -0
  232. package/dist/store/watchers.d.ts +17 -0
  233. package/dist/store/watchers.d.ts.map +1 -0
  234. package/dist/store/watchers.js +56 -0
  235. package/dist/store/writes.d.ts +24 -0
  236. package/dist/store/writes.d.ts.map +1 -0
  237. package/dist/store/writes.js +125 -0
  238. package/dist/store.d.ts +107 -152
  239. package/dist/store.d.ts.map +1 -1
  240. package/dist/store.js +143 -394
  241. package/dist/stored.d.ts +59 -65
  242. package/dist/stored.d.ts.map +1 -1
  243. package/dist/stored.js +255 -149
  244. package/dist/surface.d.ts +88 -0
  245. package/dist/surface.d.ts.map +1 -0
  246. package/dist/surface.js +224 -0
  247. package/dist/sync.d.ts +19 -0
  248. package/dist/sync.d.ts.map +1 -0
  249. package/dist/sync.js +532 -0
  250. package/dist/tables.d.ts +110 -50
  251. package/dist/tables.d.ts.map +1 -1
  252. package/dist/tables.js +205 -54
  253. package/dist/tallies.d.ts +18 -0
  254. package/dist/tallies.d.ts.map +1 -0
  255. package/dist/tallies.js +129 -0
  256. package/dist/validation.d.ts.map +1 -1
  257. package/dist/validation.js +4 -4
  258. package/dist/wire.d.ts +16 -1
  259. package/dist/wire.d.ts.map +1 -1
  260. package/dist/wire.js +6 -0
  261. package/package.json +3 -2
  262. package/dist/fence.d.ts +0 -6
  263. package/dist/fence.d.ts.map +0 -1
  264. package/dist/fence.js +0 -10
@@ -0,0 +1,556 @@
1
+ import { attachedModel } from './attached-model.js';
2
+ import { bootstrapRoom } from './bootstrap.js';
3
+ import { refusedForSpace } from './durability.js';
4
+ import { uncaught } from './failure.js';
5
+ import { name, usable } from './listing.js';
6
+ import { NO_MODEL } from './model.js';
7
+ import { advancement } from './refresh.js';
8
+ import { recoverReads, statusShown } from './shown.js';
9
+ import { NO_INSTALL, } from './store.js';
10
+ import { unconfirmed } from './transfer.js';
11
+ import { ROW_WEIGHT, yielded } from './turn.js';
12
+ // Bytes still arriving are reported at most this often, so readiness is not rebuilt for every chunk the network hands over while a slow link still moves the figure.
13
+ const ARRIVAL_REPORT_MS = 250;
14
+ const NOTHING_HELD = { bytes: 0 };
15
+ export const ERASE_BATCH = 512;
16
+ function transferGone(held) {
17
+ return {
18
+ ...held,
19
+ transfer: null,
20
+ arrived: null,
21
+ streaming: false,
22
+ underived: 0,
23
+ unindexed: [],
24
+ };
25
+ }
26
+ function amountOf(held, kept) {
27
+ if (held.transfer !== null) {
28
+ const owed = held.unindexed.reduce((sum, part) => sum + part.bytes, 0);
29
+ return {
30
+ done: held.transfer.done.bytes - Math.ceil(owed),
31
+ total: Math.max(held.arrived ?? 0, held.transfer.done.bytes),
32
+ };
33
+ }
34
+ return { done: kept, total: held.arrived ?? kept };
35
+ }
36
+ function setBack(held) {
37
+ const now = amountOf(held, NOTHING_HELD.bytes);
38
+ const earlier = held.startedOver;
39
+ return {
40
+ ...held,
41
+ startedOver: {
42
+ done: Math.max(now.done, earlier?.done ?? 0),
43
+ total: Math.max(now.total, earlier?.total ?? 0),
44
+ },
45
+ };
46
+ }
47
+ function owedIndexes(held, bytes, text) {
48
+ if (bytes <= 0 || text <= 0)
49
+ return held;
50
+ return { ...held, unindexed: [...held.unindexed, { bytes, text }] };
51
+ }
52
+ function built(held, done, text) {
53
+ const owed = [...held.unindexed];
54
+ let left = text;
55
+ while (!done && left > 0 && owed.length > 0) {
56
+ const [first] = owed;
57
+ if (left >= first.text) {
58
+ left -= first.text;
59
+ owed.shift();
60
+ continue;
61
+ }
62
+ owed[0] = {
63
+ bytes: (first.bytes * (first.text - left)) / first.text,
64
+ text: first.text - left,
65
+ };
66
+ left = 0;
67
+ }
68
+ return { ...held, unindexed: done ? [] : owed };
69
+ }
70
+ function batchEnd(entries, from, bytes) {
71
+ let end = from;
72
+ let spent = 0;
73
+ while (end < entries.length) {
74
+ const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
75
+ if (spent + size > bytes && end > from)
76
+ break;
77
+ spent += size;
78
+ end += 1;
79
+ }
80
+ return end;
81
+ }
82
+ export function installationOf(store, coverage, held) {
83
+ const bytes = { done: 0, total: 0 };
84
+ let installing = 0;
85
+ let downloading = false;
86
+ for (const { room, state } of coverage) {
87
+ if (state === 'unadmitted')
88
+ continue;
89
+ const install = store.installs.of(room);
90
+ const underway = state === 'acquiring' || (install.installing && state !== 'complete');
91
+ if (underway)
92
+ installing += 1;
93
+ if (underway && (install.streaming || install.arrived === null))
94
+ downloading = true;
95
+ const kept = underway ? NOTHING_HELD.bytes : (held.get(room)?.bytes ?? NOTHING_HELD.bytes);
96
+ const now = amountOf(install, kept);
97
+ const earlier = underway ? install.startedOver : null;
98
+ bytes.done += Math.max(now.done, earlier?.done ?? 0);
99
+ bytes.total += Math.max(now.total, earlier?.total ?? 0);
100
+ }
101
+ const step = installing === 0
102
+ ? null
103
+ : downloading
104
+ ? 'downloading'
105
+ : bytes.done < bytes.total
106
+ ? 'writing'
107
+ : 'replaying';
108
+ return { step, bytes };
109
+ }
110
+ export function roomInFlight(store, locator) {
111
+ const { catching, transfer } = store.installs.of(locator);
112
+ return catching || transfer !== null;
113
+ }
114
+ function installing(runtime, locator) {
115
+ runtime.store.installs.change(locator, (held) => ({ ...held, installing: true }));
116
+ runtime.store.readiness.queue();
117
+ }
118
+ function installed(runtime, locator) {
119
+ if (!runtime.store.installs.of(locator).installing)
120
+ return;
121
+ runtime.store.installs.change(locator, (held) => ({ ...held, installing: false }));
122
+ runtime.store.readiness.queue();
123
+ }
124
+ export function sweep({ store, socket, journal }) {
125
+ if (store.device.now().sweeping || socket.closed())
126
+ return;
127
+ store.device.set({ sweeping: true });
128
+ void (async () => {
129
+ try {
130
+ while (!socket.closed() && (await store.durably((local) => local.erase(null, ERASE_BATCH)))) {
131
+ await yielded();
132
+ }
133
+ }
134
+ catch (error) {
135
+ journal.record('erasure failed');
136
+ uncaught(error);
137
+ }
138
+ finally {
139
+ store.device.set({ sweeping: false });
140
+ }
141
+ })();
142
+ }
143
+ export function forgetInstall(store, locator) {
144
+ store.installs.change(locator, ({ installing }) => ({ ...NO_INSTALL, installing }));
145
+ }
146
+ export function caughtUpTo(runtime, locator) {
147
+ if (!runtime.store.installs.of(locator).catching)
148
+ return;
149
+ runtime.store.installs.change(locator, (held) => ({ ...held, catching: false }));
150
+ recoverReads(runtime);
151
+ }
152
+ export function catching(store, locator) {
153
+ store.installs.change(locator, (held) => ({ ...held, catching: true }));
154
+ }
155
+ export function confirmedAgain(store, locator) {
156
+ store.installs.change(locator, (held) => ({
157
+ ...held,
158
+ confirmations: held.confirmations + 1,
159
+ }));
160
+ }
161
+ export function standingAt(store, locator, held) {
162
+ return { key: held?.key ?? null, confirmed: store.installs.of(locator).confirmations };
163
+ }
164
+ function standsAt(store, locator, held, from) {
165
+ const now = standingAt(store, locator, held);
166
+ return now.key === from.key && now.confirmed === from.confirmed;
167
+ }
168
+ function shownAt({ store, socket }, locator, held) {
169
+ const raised = store.installs.of(locator).highest ?? undefined;
170
+ if (held === undefined || raised === undefined)
171
+ return held ?? raised;
172
+ return socket.reads.reached(locator, held, raised) ? held : raised;
173
+ }
174
+ export function showing(system, locator, roomVersion, held) {
175
+ const at = shownAt(system, locator, held);
176
+ if (at === undefined || !system.socket.reads.reached(locator, at, roomVersion)) {
177
+ system.store.installs.change(locator, (room) => ({ ...room, highest: roomVersion }));
178
+ }
179
+ }
180
+ export function olderThan(system, locator, roomVersion, held) {
181
+ const { store, socket } = system;
182
+ if (store.installs.of(locator).beyondHorizon)
183
+ return false;
184
+ const at = shownAt(system, locator, held);
185
+ return (at !== undefined &&
186
+ socket.reads.reached(locator, at, roomVersion) &&
187
+ !socket.reads.reached(locator, roomVersion, at));
188
+ }
189
+ async function older(runtime, locator, roomVersion) {
190
+ const { store } = runtime;
191
+ if (store.installs.of(locator).beyondHorizon)
192
+ return false;
193
+ return olderThan(runtime, locator, roomVersion, (await store.record(locator))?.roomVersion);
194
+ }
195
+ export function replaced(store, locators) {
196
+ for (const locator of locators) {
197
+ store.installs.change(locator, (held) => ({ ...held, highest: null, beyondHorizon: true }));
198
+ }
199
+ }
200
+ export async function reachedBy({ store, socket }, locator, roomVersion) {
201
+ const held = await store.record(locator);
202
+ return held !== undefined && socket.reads.reached(locator, held.roomVersion, roomVersion);
203
+ }
204
+ export async function behind({ store, socket }, room) {
205
+ const held = (await store.record(room.locator))?.roomVersion;
206
+ return held !== undefined && !socket.reads.reached(room.locator, held, room.roomVersion);
207
+ }
208
+ export async function writtenAfter({ store, socket }, locator, roomVersion, path) {
209
+ const held = await store.record(locator);
210
+ if (held === undefined)
211
+ return false;
212
+ const [entry] = await (await store.open()).entries(held.key, 'confirmed', [path]);
213
+ const written = entry?.written ?? undefined;
214
+ return (written !== undefined &&
215
+ socket.reads.reached(locator, written, roomVersion) &&
216
+ !socket.reads.reached(locator, roomVersion, written));
217
+ }
218
+ async function writtenLater(runtime, local, locator, key, paths, roomVersion, heldAt) {
219
+ if (paths.length === 0)
220
+ return new Set();
221
+ const { reached } = runtime.socket.reads;
222
+ const behindShown = heldAt === undefined
223
+ ? await older(runtime, locator, roomVersion)
224
+ : olderThan(runtime, locator, roomVersion, heldAt);
225
+ if (!behindShown)
226
+ return new Set();
227
+ const entries = await local.entries(key, 'confirmed', paths);
228
+ return new Set(entries.flatMap(({ path, written }) => written !== null &&
229
+ reached(locator, written, roomVersion) &&
230
+ !reached(locator, roomVersion, written)
231
+ ? [path]
232
+ : []));
233
+ }
234
+ function remodel(runtime, answers, held) {
235
+ const answer = answers?.collection;
236
+ if (answer === undefined)
237
+ return held;
238
+ return { collection: attachedModel(answer, runtime.roomTypes) };
239
+ }
240
+ function refreshingOf(runtime, local, locator, key, composition, liveness, replaying = false, heldAt) {
241
+ const { store } = runtime;
242
+ const write = replaying ? store.finishingWrite : store.exclusive;
243
+ return {
244
+ turn() {
245
+ if (replaying)
246
+ installing(runtime, locator);
247
+ },
248
+ prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
249
+ later: (paths, roomVersion) => writtenLater(runtime, local, locator, key, paths, roomVersion, heldAt),
250
+ async stage(page) {
251
+ for (let written = 0;;) {
252
+ const end = batchEnd(page.entries, written, store.stageBytes());
253
+ const last = end === page.entries.length;
254
+ const entries = page.entries.slice(written, end);
255
+ const staged = await write(async () => {
256
+ if (!liveness())
257
+ return false;
258
+ await store.refreshPage(local, key, composition, last
259
+ ? { ...page, entries }
260
+ : { entries, removals: [], folders: [], folderRemovals: [] });
261
+ return true;
262
+ });
263
+ if (!staged)
264
+ return false;
265
+ if (last)
266
+ return true;
267
+ written = end;
268
+ }
269
+ },
270
+ };
271
+ }
272
+ export async function pullRefresh(runtime, authorized, record, liveness, hold, refresh) {
273
+ if (!liveness())
274
+ return unconfirmed;
275
+ const { store, journal } = runtime;
276
+ const from = standingAt(store, record.locator, record);
277
+ const local = await store.open();
278
+ try {
279
+ await store.exclusive(() => local.unrefresh(record.key));
280
+ const result = await advancement(async (authorization, request) => {
281
+ hold.release();
282
+ try {
283
+ return await refresh(authorization, request);
284
+ }
285
+ finally {
286
+ hold.take();
287
+ }
288
+ }, authorized, record, (models, kept) => (liveness() ? remodel(runtime, models, kept) : kept), liveness, refreshingOf(runtime, local, record.locator, record.key, store.composed(record), liveness, false, record.roomVersion));
289
+ if (result.kind !== 'advanced') {
290
+ journal.record(`transfer refresh ${record.locator} ${result.kind}`);
291
+ return result;
292
+ }
293
+ journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
294
+ return {
295
+ kind: 'advanced',
296
+ record: result.record,
297
+ from,
298
+ remodelled: result.record.model !== record.model,
299
+ };
300
+ }
301
+ catch (error) {
302
+ if (refusedForSpace(error))
303
+ return unconfirmed;
304
+ journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
305
+ throw error;
306
+ }
307
+ }
308
+ async function abandoned(runtime, local, locator) {
309
+ await local.abandon(locator);
310
+ runtime.store.installs.change(locator, (held) => transferGone(setBack(held)));
311
+ recoverReads(runtime);
312
+ sweep(runtime);
313
+ }
314
+ function staging(runtime, local, liveness, unheld) {
315
+ const { store, socket } = runtime;
316
+ const derived = async (locator, key, composition) => {
317
+ store.installs.change(locator, (held) => ({ ...held, underived: 0 }));
318
+ for (;;) {
319
+ if (!liveness())
320
+ return false;
321
+ const done = await store.finishingWrite(async () => liveness() ? store.rederive(local, key, composition) : null);
322
+ if (done === null)
323
+ return false;
324
+ store.installs.change(locator, (held) => built(held, done.done, done.text));
325
+ runtime.store.readiness.queue();
326
+ if (done.done)
327
+ return true;
328
+ }
329
+ };
330
+ return {
331
+ held: (locator) => store.installs.of(locator).transfer,
332
+ async turn(locator, step) {
333
+ installing(runtime, locator);
334
+ return step === 'downloading' ? socket.heldStream() : () => { };
335
+ },
336
+ unqueued: socket.unqueued,
337
+ queued: socket.queued,
338
+ arriving(locator) {
339
+ let ended = false;
340
+ return {
341
+ at(bytes) {
342
+ if (ended)
343
+ return;
344
+ store.installs.change(locator, (held) => ({
345
+ ...held,
346
+ arrived: bytes,
347
+ streaming: true,
348
+ }));
349
+ if (!store.installs.arrivalDue(performance.now(), ARRIVAL_REPORT_MS))
350
+ return;
351
+ runtime.store.readiness.queue();
352
+ },
353
+ end() {
354
+ if (ended)
355
+ return;
356
+ ended = true;
357
+ if (!store.installs.of(locator).streaming)
358
+ return;
359
+ store.installs.change(locator, (held) => ({ ...held, streaming: false }));
360
+ runtime.store.readiness.queue();
361
+ },
362
+ };
363
+ },
364
+ streamed: socket.streamed,
365
+ async stage(record, page, composition) {
366
+ const { locator } = record;
367
+ installing(runtime, locator);
368
+ if (!liveness())
369
+ return null;
370
+ const key = await store.startingWrite(async () => {
371
+ if (!liveness())
372
+ return null;
373
+ return local.stage(record, composition, page);
374
+ });
375
+ if (key === null || !liveness())
376
+ return null;
377
+ const before = store.installs.of(locator);
378
+ const prior = before.transfer?.done.bytes ?? 0;
379
+ const held = { ...record, key };
380
+ const text = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), 0);
381
+ const owed = text + before.underived;
382
+ store.installs.change(locator, (room) => ({
383
+ ...owedIndexes(room, record.done.bytes - prior, text),
384
+ transfer: held,
385
+ underived: owed,
386
+ }));
387
+ installing(runtime, locator);
388
+ recoverReads(runtime);
389
+ if (owed < store.deriveBytes())
390
+ return held;
391
+ return (await derived(locator, key, composition)) ? held : null;
392
+ },
393
+ async resume(record, parts, composition) {
394
+ const { locator } = record;
395
+ installing(runtime, locator);
396
+ if (!liveness())
397
+ return null;
398
+ const prior = unheld === undefined ? await store.record(locator) : undefined;
399
+ const published = prior !== undefined && prior.replica.selection === record.selection ? prior.key : null;
400
+ const kept = unheld?.staged ?? (await local.held()).staged.has(locator);
401
+ if (published === null && !kept)
402
+ return { record: null, staged: new Set() };
403
+ const resumed = await store.startingWrite(async () => {
404
+ if (!liveness())
405
+ return null;
406
+ return local.resume(record, composition, parts.map((part) => part.id), published);
407
+ });
408
+ if (resumed === null || !liveness())
409
+ return null;
410
+ const parted = new Set(resumed.staged);
411
+ if (resumed.key === null)
412
+ return { record: null, staged: parted };
413
+ const bytes = parts.reduce((sum, part) => sum + (parted.has(part.id) ? part.bytes : 0), 0);
414
+ const held = {
415
+ ...record,
416
+ transfer: resumed.transfer,
417
+ key: resumed.key,
418
+ done: { bytes },
419
+ };
420
+ store.installs.change(locator, (room) => ({ ...room, transfer: held }));
421
+ recoverReads(runtime);
422
+ return { record: held, staged: parted };
423
+ },
424
+ derive: derived,
425
+ async refreshing(locator, key, composition) {
426
+ await store.exclusive(() => local.unrefresh(key));
427
+ return refreshingOf(runtime, local, locator, key, composition, liveness, true);
428
+ },
429
+ abandon: (locator) => store.exclusive(async () => {
430
+ if (liveness())
431
+ await abandoned(runtime, local, locator);
432
+ }),
433
+ setAside: async (locator) => {
434
+ if (!liveness())
435
+ return;
436
+ store.installs.change(locator, (held) => transferGone(setBack(held)));
437
+ recoverReads(runtime);
438
+ },
439
+ release: (key) => store.exclusive(async () => {
440
+ await local.release(key);
441
+ sweep(runtime);
442
+ }),
443
+ };
444
+ }
445
+ export async function pullBootstrap(runtime, authorized, room, local, liveness, prior, from = runtime.socket.reads, unheld) {
446
+ if (!liveness())
447
+ return unconfirmed;
448
+ const { store, journal } = runtime;
449
+ if (prior !== undefined) {
450
+ catching(store, room.locator);
451
+ recoverReads(runtime);
452
+ }
453
+ try {
454
+ const result = await bootstrapRoom(from, authorized, room, {
455
+ collection: prior?.model ?? runtime.socket.rooms.standing(room.locator).unheldModel ?? NO_MODEL,
456
+ }, runtime.builtIn, runtime.typedPaths, (models, held) => (liveness() ? remodel(runtime, models, held) : held), liveness, staging(runtime, local, liveness, unheld));
457
+ const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
458
+ journal.record(`transfer bootstrap ${room.locator} ${detail}`);
459
+ if (result.kind === 'staged' &&
460
+ (await older(runtime, room.locator, result.record.roomVersion))) {
461
+ caughtUpTo(runtime, room.locator);
462
+ installed(runtime, room.locator);
463
+ await abandoned(runtime, local, room.locator);
464
+ journal.record(`transfer bootstrap ${room.locator} older than shown`);
465
+ return { kind: 'transport-uncertainty' };
466
+ }
467
+ if (result.kind !== 'staged') {
468
+ caughtUpTo(runtime, room.locator);
469
+ installed(runtime, room.locator);
470
+ }
471
+ return result;
472
+ }
473
+ catch (error) {
474
+ caughtUpTo(runtime, room.locator);
475
+ installed(runtime, room.locator);
476
+ if (refusedForSpace(error))
477
+ return unconfirmed;
478
+ journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
479
+ throw error;
480
+ }
481
+ }
482
+ export function publish(runtime, local, room, reached, landing, live) {
483
+ const { store } = runtime;
484
+ const landed = store.exclusive(async () => {
485
+ if (!live()) {
486
+ caughtUpTo(runtime, room.locator);
487
+ return true;
488
+ }
489
+ const work = await store.work(room.locator);
490
+ const prior = work.replica;
491
+ if (!standsAt(store, room.locator, prior?.record, landing.from)) {
492
+ if (landing.kind === 'refreshed') {
493
+ await local.unrefresh(reached.key);
494
+ return false;
495
+ }
496
+ caughtUpTo(runtime, room.locator);
497
+ await abandoned(runtime, local, room.locator);
498
+ return false;
499
+ }
500
+ const record = landing.kind === 'refreshed' && prior !== undefined
501
+ ? {
502
+ ...prior.record,
503
+ roomVersion: reached.roomVersion,
504
+ ...(landing.remodelled ? { model: reached.model } : {}),
505
+ }
506
+ : reached;
507
+ let set;
508
+ try {
509
+ set = await runtime.settle(local, room.locator, work, record, work.queue, landing.kind === 'refreshed'
510
+ ? {
511
+ room: true,
512
+ view: 'refreshed',
513
+ commit: { refreshed: true },
514
+ }
515
+ : {
516
+ room: true,
517
+ view: landing.refreshed ? 'refreshed' : 'confirmed',
518
+ commit: {
519
+ published: { from: prior?.record.key ?? null },
520
+ ...(landing.refreshed ? { refreshed: true } : {}),
521
+ },
522
+ });
523
+ }
524
+ catch (error) {
525
+ if (!refusedForSpace(error))
526
+ throw error;
527
+ caughtUpTo(runtime, room.locator);
528
+ return true;
529
+ }
530
+ if (landing.kind === 'bootstrapped') {
531
+ runtime.socket.rooms.readied(room.locator);
532
+ store.installs.change(room.locator, (held) => ({
533
+ ...transferGone(held),
534
+ highest: null,
535
+ beyondHorizon: false,
536
+ startedOver: null,
537
+ catching: false,
538
+ }));
539
+ runtime.socket.rooms.stand(room.locator, { unheldModel: null });
540
+ if (prior !== undefined && prior.record.key !== record.key)
541
+ sweep(runtime);
542
+ void name(runtime, room.locator);
543
+ }
544
+ usable(runtime, room);
545
+ void runtime.store.changed(set);
546
+ await runtime.unpair(local);
547
+ void statusShown(runtime);
548
+ if (landing.kind === 'bootstrapped' && work.queue.length > 0) {
549
+ runtime.submitLater(room.locator);
550
+ }
551
+ return true;
552
+ });
553
+ return landing.kind === 'refreshed'
554
+ ? landed
555
+ : landed.finally(() => installed(runtime, room.locator));
556
+ }
package/dist/job.d.ts CHANGED
@@ -1,14 +1,5 @@
1
- export type Job = {
2
- readonly label: string;
3
- readonly kind: 'started' | 'scheduled' | 'ended';
4
- readonly state: string;
5
- readonly next: string | null;
6
- };
7
1
  export type RunningJob = {
8
2
  readonly name: string;
9
- readonly running_since: string;
10
3
  };
11
- export declare function jobOf(said: string): Job;
12
- export declare function jobsRunning(jobs: readonly RunningJob[], now: number): string[];
13
- export declare function sessionEnded(jobs: readonly RunningJob[], now: number): string;
4
+ export declare function sessionEnded(jobs: readonly RunningJob[]): string;
14
5
  //# sourceMappingURL=job.d.ts.map
package/dist/job.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,GAAG;IAChB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,WAAW,GAAG,OAAO,CAAC;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAChC,CAAC;AAQF,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CASvC;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,UAAU,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAM9E;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,UAAU,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAI7E"}
1
+ {"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,CAAC;AAIF,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,CAIhE"}
package/dist/job.js CHANGED
@@ -1,28 +1,7 @@
1
- const NEXT_RUN = 'next run ';
2
- const MINUTE = 60_000;
3
1
  const ENDED = 'Session ended.';
4
- export function jobOf(said) {
5
- const at = said.lastIndexOf(': ');
6
- const label = at === -1 ? said : said.slice(0, at);
7
- const state = at === -1 ? '' : said.slice(at + 2);
8
- if (state === 'running')
9
- return { label, kind: 'started', state, next: null };
10
- if (state === 'scheduled')
11
- return { label, kind: 'scheduled', state, next: null };
12
- return state.startsWith(NEXT_RUN)
13
- ? { label, kind: 'scheduled', state, next: state.slice(NEXT_RUN.length) }
14
- : { label, kind: 'ended', state, next: null };
15
- }
16
- export function jobsRunning(jobs, now) {
17
- return jobs.map((job) => {
18
- const since = Date.parse(job.running_since);
19
- const ran = Number.isNaN(since) ? 0 : Math.floor((now - since) / MINUTE);
20
- return ran >= 1 ? `${job.name} (${ran} min)` : job.name;
21
- });
22
- }
23
- export function sessionEnded(jobs, now) {
2
+ export function sessionEnded(jobs) {
24
3
  if (jobs.length === 0)
25
4
  return ENDED;
26
5
  const counted = jobs.length === 1 ? '1 background job' : `${jobs.length} background jobs`;
27
- return `${ENDED} ${counted} stopped: ${jobsRunning(jobs, now).join(', ')}`;
6
+ return `${ENDED} ${counted} stopped: ${jobs.map(({ name }) => name).join(', ')}`;
28
7
  }
@@ -0,0 +1,7 @@
1
+ import type { Runtime } from './runtime.js';
2
+ export declare function scheduleCurrency(runtime: Runtime): void;
3
+ export declare function spaceFreed(runtime: Runtime): void;
4
+ export declare function close({ store, socket, conversations, recovery, liveQueries, figures }: Runtime): void;
5
+ export declare function stop(runtime: Runtime): Promise<void>;
6
+ export declare function start(runtime: Runtime, signal: AbortSignal | undefined): void;
7
+ //# sourceMappingURL=lifecycle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA8M5C,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,QAOhD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,QAS1C;AAED,wBAAgB,KAAK,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE,OAAO,QAQ9F;AAED,wBAAsB,IAAI,CAAC,OAAO,EAAE,OAAO,iBAS1C;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,GAAG,SAAS,QAyCtE"}