@agentshouse/mdruntime 0.1.0-c52c32704e1a1 → 0.1.0-c52f87bb83a70
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.
- package/AGENTS.md +24 -18
- package/README.md +9 -8
- package/dist/adapter.d.ts +21 -8
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +4 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +13 -3
- package/dist/bootstrap.d.ts +13 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +86 -88
- package/dist/chain.d.ts +11 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +5 -9
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +10 -86
- package/dist/change.d.ts +0 -1
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +1 -1
- package/dist/chat/room-file.d.ts.map +1 -1
- package/dist/chat/room-file.js +8 -3
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +27 -2
- package/dist/collection.d.ts +5 -19
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +22 -360
- package/dist/compile.d.ts +11 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +712 -0
- package/dist/connection.d.ts +9 -5
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1460 -639
- package/dist/conversation.d.ts +1 -1
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +22 -15
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +1 -0
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +3 -1
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -24
- package/dist/file.d.ts +0 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -6
- package/dist/host.d.ts +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +7 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +123 -0
- package/dist/memory.d.ts +1 -0
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +13 -1
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +12 -0
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +84 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +10 -4
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +3 -0
- package/dist/overlay.d.ts +0 -3
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +1 -151
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +43 -42
- package/dist/reads.js +1 -1
- package/dist/refresh.d.ts +10 -13
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +74 -70
- package/dist/replica.d.ts +55 -38
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +273 -214
- package/dist/room-draft.d.ts +1 -1
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +5 -6
- package/dist/rows.d.ts +29 -31
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +113 -69
- package/dist/save.d.ts +9 -10
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +59 -57
- package/dist/search.d.ts +2 -2
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +60 -28
- package/dist/shared.d.ts +0 -3
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +1 -31
- package/dist/store.d.ts +83 -68
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +321 -535
- package/dist/stored.d.ts +143 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +561 -0
- package/dist/tables.d.ts +320 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +537 -0
- package/dist/turn.d.ts +1 -1
- package/dist/turn.d.ts.map +1 -1
- package/dist/turn.js +28 -20
- package/dist/validation.d.ts +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +162 -0
- package/dist/windows.d.ts +2 -1
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +30 -25
- package/dist/wire.d.ts +8 -0
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +2 -0
- package/package.json +6 -3
- package/dist/read-side.d.ts +0 -27
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -1179
package/dist/connection.js
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
1
|
-
import { roomTarget, target } from '@agentshouse/mdmodel';
|
|
2
|
-
import { attachedModel } from './attached-model.js';
|
|
1
|
+
import { escapePointer, parseFrontmatter, roomTarget, target } from '@agentshouse/mdmodel';
|
|
2
|
+
import { attachedModel, NO_ROOM_TYPES, roomTypesOf } from './attached-model.js';
|
|
3
3
|
import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
|
|
4
4
|
import { observing, whole } from './observation.js';
|
|
5
5
|
import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
|
|
6
|
-
import {
|
|
6
|
+
import { createLiveQueries } from './live.js';
|
|
7
7
|
import { seededResult } from './seed.js';
|
|
8
|
-
import { documentKey,
|
|
8
|
+
import { documentKey, roomOfKey } from './rows.js';
|
|
9
9
|
import { createSearchIndex, searchPlanOf, } from './search.js';
|
|
10
10
|
import { capped, MAXIMUM_WINDOW } from './windows.js';
|
|
11
11
|
import { bootstrapRoom } from './bootstrap.js';
|
|
12
|
-
import { changeSet,
|
|
12
|
+
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
13
13
|
import { createConversationStore, } from './conversation.js';
|
|
14
14
|
import { leaving as leavingIn, partnerOf, queuedFile, } from './change.js';
|
|
15
|
-
import {
|
|
15
|
+
import { available, collectionOf, unreadable as unreadableIn, validRoot, } from './collection.js';
|
|
16
16
|
import { runtimeError } from './failure.js';
|
|
17
17
|
import { createLog } from './log.js';
|
|
18
18
|
import { binaryRecord } from './binary.js';
|
|
19
|
-
import {
|
|
19
|
+
import { openedFiles } from './opened.js';
|
|
20
|
+
import { ABSENT_TARGET, absentBinary, absentReference, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
|
|
20
21
|
import { createRecovery } from './recovery.js';
|
|
21
22
|
import { doorReads } from './reads.js';
|
|
22
|
-
import {
|
|
23
|
+
import { advancement } from './refresh.js';
|
|
23
24
|
import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
|
|
24
|
-
import {
|
|
25
|
+
import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, sameMembership, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
|
|
25
26
|
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
26
27
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
27
28
|
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
28
29
|
import { readinessOf, SETTLED_ROOMS, } from './readiness.js';
|
|
29
30
|
import { FencedMap } from './fence.js';
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
31
|
+
import { fingerprint } from './stored.js';
|
|
32
|
+
import { sameBinary } from './overlay.js';
|
|
33
|
+
import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
|
|
32
34
|
import { identityTime, roomDraftRevision } from './identity.js';
|
|
33
35
|
import { beneath, normalize } from './paths.js';
|
|
34
36
|
import { openRuntimeStore, } from './store.js';
|
|
37
|
+
import { changedCleanly, sameComposition, validatedCollection, } from './validation.js';
|
|
35
38
|
import { unconfirmed } from './transfer.js';
|
|
36
39
|
import { governs, holds, narrowed, planReach, } from './reach.js';
|
|
37
40
|
function roomsAlone(plan) {
|
|
@@ -60,26 +63,19 @@ const OPENING_LANE = 'opening';
|
|
|
60
63
|
const CONVERSATION_LANE = 'conversations';
|
|
61
64
|
const ROOM_READS = 256;
|
|
62
65
|
const ROOM_ATTEMPTS = 16;
|
|
66
|
+
const SEARCH_BATCH = 256;
|
|
67
|
+
const ERASE_BATCH = 512;
|
|
68
|
+
const REDERIVE_BATCH = 256;
|
|
69
|
+
const WALK_BYTES = 1 << 20;
|
|
70
|
+
const QUERY_BYTES = 1 << 20;
|
|
71
|
+
const STAGE_BYTES = 1 << 20;
|
|
72
|
+
const CLEAN_CHANGES = 2_000;
|
|
63
73
|
// A Room that locks offline stays readable this long after the connection drops, so a laptop waking or changing network keeps working while a disconnected device locks soon after.
|
|
64
74
|
const LOCK_GRACE_MS = 120_000;
|
|
65
75
|
function operationOf(input) {
|
|
66
76
|
const operation = input?.operation_id;
|
|
67
77
|
return typeof operation === 'string' ? operation : null;
|
|
68
78
|
}
|
|
69
|
-
const outside = new Map();
|
|
70
|
-
function outsideOf(roots) {
|
|
71
|
-
const key = JSON.stringify(roots);
|
|
72
|
-
const held = outside.get(key);
|
|
73
|
-
if (held !== undefined)
|
|
74
|
-
return held;
|
|
75
|
-
const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
|
|
76
|
-
const made = {
|
|
77
|
-
documents: (path) => (markdownPath(path) ? counted(path) : 0),
|
|
78
|
-
binaries: counted,
|
|
79
|
-
};
|
|
80
|
-
outside.set(key, made);
|
|
81
|
-
return made;
|
|
82
|
-
}
|
|
83
79
|
function abandonedOn(signal) {
|
|
84
80
|
return new Promise((resolve) => {
|
|
85
81
|
if (signal.aborted)
|
|
@@ -96,6 +92,7 @@ function pendingKey(locator, id) {
|
|
|
96
92
|
}
|
|
97
93
|
export function composeRuntime(options) {
|
|
98
94
|
const localSaves = options.adapter.localSaves === true;
|
|
95
|
+
const roomTypes = roomTypesOf(options.adapter.roomTypes);
|
|
99
96
|
let authorization = options.authorization;
|
|
100
97
|
let opening;
|
|
101
98
|
let store;
|
|
@@ -182,7 +179,7 @@ export function composeRuntime(options) {
|
|
|
182
179
|
const queueSubscriptions = new Set();
|
|
183
180
|
const roomDraftListeners = new Set();
|
|
184
181
|
const presentationListeners = new Set();
|
|
185
|
-
const
|
|
182
|
+
const liveQueries = createLiveQueries((plan, cover) => answeredPlan(plan, cover));
|
|
186
183
|
let withdrawConversations;
|
|
187
184
|
let currencyQueued = null;
|
|
188
185
|
let socketOpen = false;
|
|
@@ -196,7 +193,6 @@ export function composeRuntime(options) {
|
|
|
196
193
|
let member = null;
|
|
197
194
|
const memberListeners = new Set();
|
|
198
195
|
const prepared = new Set();
|
|
199
|
-
const projecting = new Set();
|
|
200
196
|
const unpreparedDemand = new Set();
|
|
201
197
|
const unindexable = new Set();
|
|
202
198
|
let preparing = false;
|
|
@@ -211,7 +207,19 @@ export function composeRuntime(options) {
|
|
|
211
207
|
const transfers = new FencedMap(isFenced);
|
|
212
208
|
const conversations = createConversationStore({
|
|
213
209
|
agents: declaredAgents,
|
|
214
|
-
documents: (
|
|
210
|
+
documents: (declaration, after) => rowsQuery(planOf({
|
|
211
|
+
rooms: [declaration.locator],
|
|
212
|
+
types: [declaration.type],
|
|
213
|
+
select: {
|
|
214
|
+
fields: [
|
|
215
|
+
declaration.identity,
|
|
216
|
+
declaration.name,
|
|
217
|
+
...(declaration.retired === undefined ? [] : [declaration.retired]),
|
|
218
|
+
].map((field) => `/${escapePointer(field)}`),
|
|
219
|
+
},
|
|
220
|
+
limit: MAXIMUM_WINDOW,
|
|
221
|
+
...(after === undefined ? {} : { after }),
|
|
222
|
+
}, roomFacts())),
|
|
215
223
|
call: (name, input) => invokeAction(name, input),
|
|
216
224
|
durably,
|
|
217
225
|
stream: () => options.adapter.conversations,
|
|
@@ -222,13 +230,31 @@ export function composeRuntime(options) {
|
|
|
222
230
|
expired: (operation) => pastHorizon(operation),
|
|
223
231
|
});
|
|
224
232
|
const searchIndex = createSearchIndex({
|
|
225
|
-
room
|
|
226
|
-
|
|
233
|
+
async *room(room) {
|
|
234
|
+
const local = await openLocal();
|
|
235
|
+
for (let after = null;;) {
|
|
236
|
+
const key = replicas.get(room)?.record.key;
|
|
237
|
+
if (key === undefined)
|
|
238
|
+
return;
|
|
239
|
+
const page = await local.searched(key, room, after, SEARCH_BATCH, null);
|
|
240
|
+
yield page.documents;
|
|
241
|
+
if (page.next === null)
|
|
242
|
+
return;
|
|
243
|
+
after = page.next;
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
async documents(room, paths) {
|
|
247
|
+
const key = replicas.get(room)?.record.key;
|
|
248
|
+
if (key === undefined || paths.length === 0)
|
|
249
|
+
return [];
|
|
250
|
+
return (await (await openLocal()).searched(key, room, null, paths.length, paths)).documents;
|
|
251
|
+
},
|
|
227
252
|
});
|
|
228
253
|
const roomsToIndex = new Set();
|
|
229
254
|
const conversationsToIndex = new Set();
|
|
230
255
|
let indexing = false;
|
|
231
256
|
const journal = createLog();
|
|
257
|
+
const keptFiles = openedFiles(options.database, isFenced);
|
|
232
258
|
let notifyReady = () => { };
|
|
233
259
|
const ready = new Promise((resolve) => {
|
|
234
260
|
notifyReady = resolve;
|
|
@@ -246,16 +272,36 @@ export function composeRuntime(options) {
|
|
|
246
272
|
if (room.size === 0)
|
|
247
273
|
roomDrafts.delete(locator);
|
|
248
274
|
}
|
|
249
|
-
function
|
|
275
|
+
async function visibleOf(locator, paths) {
|
|
276
|
+
const held = replicas.get(locator);
|
|
277
|
+
if (held === undefined)
|
|
278
|
+
return undefined;
|
|
279
|
+
const local = await openLocal();
|
|
280
|
+
const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
|
|
281
|
+
return { ...slice, collection: collectionOf(slice.documents, compositionOf(held.record)) };
|
|
282
|
+
}
|
|
283
|
+
function roomDraftOf(record, visible) {
|
|
250
284
|
return structuredClone({
|
|
251
285
|
room: record.locator,
|
|
252
286
|
path: record.path,
|
|
253
287
|
revision: record.revision,
|
|
254
288
|
parts: record.parts,
|
|
255
|
-
changedUnderneath: underneath(record.parts, record.path,
|
|
289
|
+
changedUnderneath: underneath(record.parts, record.path, visible),
|
|
256
290
|
returned: record.returned,
|
|
257
291
|
});
|
|
258
292
|
}
|
|
293
|
+
async function roomDraftsOf(records) {
|
|
294
|
+
const rooms = new Map();
|
|
295
|
+
for (const record of records) {
|
|
296
|
+
const paths = rooms.get(record.locator) ?? [];
|
|
297
|
+
paths.push(record.path);
|
|
298
|
+
rooms.set(record.locator, paths);
|
|
299
|
+
}
|
|
300
|
+
const layers = new Map();
|
|
301
|
+
for (const [locator, paths] of rooms)
|
|
302
|
+
layers.set(locator, await visibleOf(locator, paths));
|
|
303
|
+
return records.map((record) => roomDraftOf(record, layers.get(record.locator)));
|
|
304
|
+
}
|
|
259
305
|
function snapshotRoomDrafts() {
|
|
260
306
|
const records = [...roomDrafts.values()].flatMap((room) => [...room.values()]);
|
|
261
307
|
records.sort((left, right) => {
|
|
@@ -265,18 +311,24 @@ export function composeRuntime(options) {
|
|
|
265
311
|
return left.path < right.path ? -1 : 1;
|
|
266
312
|
return 0;
|
|
267
313
|
});
|
|
268
|
-
return records
|
|
314
|
+
return roomDraftsOf(records);
|
|
269
315
|
}
|
|
316
|
+
let draftsShown = Promise.resolve();
|
|
270
317
|
function publishRoomDrafts(waitingOnly) {
|
|
271
318
|
if (closed)
|
|
272
319
|
return;
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
listener
|
|
279
|
-
|
|
320
|
+
const snapshot = snapshotRoomDrafts();
|
|
321
|
+
draftsShown = draftsShown.then(async () => {
|
|
322
|
+
const value = await snapshot.catch(() => null);
|
|
323
|
+
if (value === null || closed)
|
|
324
|
+
return;
|
|
325
|
+
for (const listener of roomDraftListeners) {
|
|
326
|
+
if (waitingOnly && listener.ready)
|
|
327
|
+
continue;
|
|
328
|
+
listener.ready = true;
|
|
329
|
+
listener.notify(value);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
280
332
|
}
|
|
281
333
|
function presentationOf(app) {
|
|
282
334
|
const value = presentations.get(app);
|
|
@@ -288,7 +340,7 @@ export function composeRuntime(options) {
|
|
|
288
340
|
const stored = next(presentations.get(app));
|
|
289
341
|
await local.present({ app, value: stored });
|
|
290
342
|
presentations.set(app, stored);
|
|
291
|
-
publishChangeSet(changeSet('',
|
|
343
|
+
publishChangeSet(changeSet('', { presentation: app }));
|
|
292
344
|
});
|
|
293
345
|
}
|
|
294
346
|
function emitPresentation(app) {
|
|
@@ -339,13 +391,7 @@ export function composeRuntime(options) {
|
|
|
339
391
|
return { attentionRequired, reset };
|
|
340
392
|
}
|
|
341
393
|
function documentsIn(locator) {
|
|
342
|
-
|
|
343
|
-
return replica === undefined ? 0 : finished(counting(replica));
|
|
344
|
-
}
|
|
345
|
-
function* counting(replica) {
|
|
346
|
-
const weighed = outsideOf(replica.confirmed.record.typedSelection?.roots ?? []);
|
|
347
|
-
return ((yield* tallying(replica.visible.documents, weighed.documents)) +
|
|
348
|
-
(yield* tallying(replica.visible.binaries, weighed.binaries)));
|
|
394
|
+
return replicas.get(locator)?.count ?? 0;
|
|
349
395
|
}
|
|
350
396
|
function listed() {
|
|
351
397
|
return structuredClone([...rooms.values()].map((room) => {
|
|
@@ -426,7 +472,7 @@ export function composeRuntime(options) {
|
|
|
426
472
|
return { listed: roomsListed, rooms: held };
|
|
427
473
|
}
|
|
428
474
|
function emitRooms() {
|
|
429
|
-
|
|
475
|
+
liveQueries.recover();
|
|
430
476
|
queueReadiness();
|
|
431
477
|
const value = listed();
|
|
432
478
|
for (const listener of roomListeners)
|
|
@@ -485,56 +531,29 @@ export function composeRuntime(options) {
|
|
|
485
531
|
}
|
|
486
532
|
}
|
|
487
533
|
function applyChangeSet(set) {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
|
|
491
|
-
};
|
|
492
|
-
const applied = readSide.apply(held, () => searchIndex.changed(held));
|
|
493
|
-
if (applied instanceof Promise)
|
|
494
|
-
void applied.catch(unpublished);
|
|
534
|
+
liveQueries.touched(set);
|
|
535
|
+
searchIndex.changed(set);
|
|
495
536
|
}
|
|
496
537
|
function unpublished(error) {
|
|
497
538
|
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
498
539
|
}
|
|
499
|
-
async function referenced(locator) {
|
|
500
|
-
for (;;) {
|
|
501
|
-
const record = replicas.get(locator)?.confirmed.record;
|
|
502
|
-
if (record === undefined || closed)
|
|
503
|
-
return;
|
|
504
|
-
const held = { text: record.documents, binaries: record.binaries };
|
|
505
|
-
const composition = compositionOf(record);
|
|
506
|
-
if (referencesIndexed(held, composition))
|
|
507
|
-
return;
|
|
508
|
-
await indexReferrers(held, composition);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
540
|
async function prepareReplica(locator) {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
return;
|
|
516
|
-
const visible = replica.visible;
|
|
517
|
-
const entered = [];
|
|
518
|
-
projecting.add(locator);
|
|
519
|
-
try {
|
|
520
|
-
await inChunks(await stepped(keysIn(visible.documents, visible.binaries)), (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
|
|
521
|
-
const current = replicas.get(locator);
|
|
522
|
-
if (current === undefined || closed)
|
|
523
|
-
return;
|
|
524
|
-
const set = entering(locator, current, chunk);
|
|
525
|
-
entered.push(set);
|
|
526
|
-
return readSide.apply(set);
|
|
527
|
-
});
|
|
528
|
-
if (!replicas.has(locator) || closed)
|
|
541
|
+
for (;;) {
|
|
542
|
+
const held = replicas.get(locator);
|
|
543
|
+
if (held === undefined || closed || prepared.has(locator))
|
|
529
544
|
return;
|
|
545
|
+
const key = held.record.key;
|
|
546
|
+
const done = await exclusive(async () => (await openLocal()).rederive(key, REDERIVE_BATCH));
|
|
547
|
+
if (replicas.get(locator)?.record.key !== key)
|
|
548
|
+
continue;
|
|
549
|
+
if (!done)
|
|
550
|
+
continue;
|
|
530
551
|
prepared.add(locator);
|
|
552
|
+
publishChangeSet(changeSet(locator, { documents: null }));
|
|
553
|
+
liveQueries.recover();
|
|
554
|
+
queueReadiness();
|
|
555
|
+
return;
|
|
531
556
|
}
|
|
532
|
-
finally {
|
|
533
|
-
projecting.delete(locator);
|
|
534
|
-
}
|
|
535
|
-
conversations.changed(mergeChangeSets(entered));
|
|
536
|
-
readSide.recover();
|
|
537
|
-
queueReadiness();
|
|
538
557
|
}
|
|
539
558
|
function unprepared() {
|
|
540
559
|
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !prepared.has(locator) && !unindexable.has(locator));
|
|
@@ -590,6 +609,7 @@ export function composeRuntime(options) {
|
|
|
590
609
|
const pending = [];
|
|
591
610
|
const coverage = [];
|
|
592
611
|
const overlay = [];
|
|
612
|
+
const read = [];
|
|
593
613
|
for (const locator of locators) {
|
|
594
614
|
const replica = replicas.get(locator);
|
|
595
615
|
if (replica === undefined) {
|
|
@@ -603,15 +623,16 @@ export function composeRuntime(options) {
|
|
|
603
623
|
}
|
|
604
624
|
coverage.push({
|
|
605
625
|
room: locator,
|
|
606
|
-
generation: replica.
|
|
626
|
+
generation: replica.record.transfer,
|
|
607
627
|
current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
|
|
608
628
|
});
|
|
629
|
+
read.push({ locator, key: replica.record.key });
|
|
609
630
|
for (const batch of replica.queue)
|
|
610
631
|
overlay.push(batch.id);
|
|
611
632
|
}
|
|
612
633
|
if (pending.length > 0)
|
|
613
634
|
return { pending };
|
|
614
|
-
return { coverage: { rooms: coverage, indexes: [], overlay } };
|
|
635
|
+
return { coverage: { rooms: coverage, indexes: [], overlay }, rooms: read };
|
|
615
636
|
}
|
|
616
637
|
function awaitPreparation(cover) {
|
|
617
638
|
if (!('pending' in cover))
|
|
@@ -723,7 +744,7 @@ export function composeRuntime(options) {
|
|
|
723
744
|
function establish(name) {
|
|
724
745
|
targets = { ...targets, [name]: { established: true } };
|
|
725
746
|
queueReadiness();
|
|
726
|
-
void durably((local) => local.
|
|
747
|
+
void durably((local) => local.fact('targets', targets)).catch(() => {
|
|
727
748
|
journal.record(`target record failed ${name}`);
|
|
728
749
|
});
|
|
729
750
|
}
|
|
@@ -760,18 +781,12 @@ export function composeRuntime(options) {
|
|
|
760
781
|
readinessWatches.delete(watch);
|
|
761
782
|
};
|
|
762
783
|
}
|
|
763
|
-
function
|
|
784
|
+
function pageBytes(page) {
|
|
764
785
|
let bytes = 0;
|
|
765
|
-
for (const
|
|
766
|
-
bytes += (path.length +
|
|
786
|
+
for (const { path, content } of page.entries)
|
|
787
|
+
bytes += (path.length + (content?.length ?? 0)) * 2;
|
|
767
788
|
return bytes;
|
|
768
789
|
}
|
|
769
|
-
function grownBy(next, previous) {
|
|
770
|
-
if (previous === undefined)
|
|
771
|
-
return layerBytes(next);
|
|
772
|
-
const documents = Object.fromEntries(Object.entries(next.documents).filter(([path, text]) => previous.documents[path] !== text));
|
|
773
|
-
return layerBytes({ ...next, documents });
|
|
774
|
-
}
|
|
775
790
|
function exceeded(error) {
|
|
776
791
|
return (typeof error === 'object' &&
|
|
777
792
|
error !== null &&
|
|
@@ -880,6 +895,75 @@ export function composeRuntime(options) {
|
|
|
880
895
|
shown();
|
|
881
896
|
}
|
|
882
897
|
}
|
|
898
|
+
function sameCover(left, right) {
|
|
899
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
900
|
+
}
|
|
901
|
+
async function coveredRows(plan, cover) {
|
|
902
|
+
for (;;) {
|
|
903
|
+
const before = cover();
|
|
904
|
+
if ('pending' in before)
|
|
905
|
+
return { pending: before };
|
|
906
|
+
const local = await openLocal();
|
|
907
|
+
const keys = before.rooms.map(({ key }) => key);
|
|
908
|
+
const changes = local.changes(keys);
|
|
909
|
+
const current = () => local.changes(keys) === changes;
|
|
910
|
+
const rows = await windowRows(local, plan, before.rooms, current);
|
|
911
|
+
const after = cover();
|
|
912
|
+
if (rows !== null && sameCover(before, after))
|
|
913
|
+
return { rows, cover: before, current };
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
async function windowRows(local, plan, rooms, unchanged) {
|
|
917
|
+
const first = await local.query(plan, rooms, QUERY_BYTES);
|
|
918
|
+
if (first.more === undefined || !('documents' in first)) {
|
|
919
|
+
const { more: _more, ...rows } = first;
|
|
920
|
+
return rows;
|
|
921
|
+
}
|
|
922
|
+
const documents = [...first.documents];
|
|
923
|
+
for (let more = first.more; more !== undefined;) {
|
|
924
|
+
if (!unchanged())
|
|
925
|
+
return null;
|
|
926
|
+
const page = await local.query({
|
|
927
|
+
...plan,
|
|
928
|
+
after: { text: more, values: JSON.parse(more) },
|
|
929
|
+
limit: plan.limit === null ? null : plan.limit - documents.length,
|
|
930
|
+
total: false,
|
|
931
|
+
}, rooms, QUERY_BYTES);
|
|
932
|
+
if (!('documents' in page))
|
|
933
|
+
return null;
|
|
934
|
+
documents.push(...page.documents);
|
|
935
|
+
more = page.more;
|
|
936
|
+
if (more === undefined) {
|
|
937
|
+
return {
|
|
938
|
+
documents,
|
|
939
|
+
...(page.next === undefined ? {} : { next: page.next }),
|
|
940
|
+
...(first.total === undefined ? {} : { total: first.total }),
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return null;
|
|
945
|
+
}
|
|
946
|
+
function keyedOf(rows, coverage) {
|
|
947
|
+
if ('groups' in rows) {
|
|
948
|
+
return {
|
|
949
|
+
result: { ...rows, coverage },
|
|
950
|
+
keys: rows.groups.map((group) => JSON.stringify(group.key)),
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
return {
|
|
954
|
+
result: { ...rows, coverage },
|
|
955
|
+
keys: rows.documents.map((document) => documentKey(document.room, document.path)),
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
async function answeredPlan(plan, cover) {
|
|
959
|
+
const answered = await coveredRows(plan, cover);
|
|
960
|
+
if ('pending' in answered) {
|
|
961
|
+
const pending = answered.pending;
|
|
962
|
+
return { result: pending, keys: [], current: () => true };
|
|
963
|
+
}
|
|
964
|
+
const held = answered.cover;
|
|
965
|
+
return { ...keyedOf(answered.rows, held.coverage), current: answered.current };
|
|
966
|
+
}
|
|
883
967
|
async function rowsQuery(plan) {
|
|
884
968
|
const before = covered(plan);
|
|
885
969
|
awaitPreparation(before);
|
|
@@ -888,11 +972,42 @@ export function composeRuntime(options) {
|
|
|
888
972
|
return seededResult(seeded).result;
|
|
889
973
|
if ('pending' in before)
|
|
890
974
|
return before;
|
|
891
|
-
const
|
|
892
|
-
|
|
893
|
-
|
|
975
|
+
const answered = await coveredRows(plan, () => {
|
|
976
|
+
const cover = covered(plan);
|
|
977
|
+
awaitPreparation(cover);
|
|
894
978
|
return cover;
|
|
895
|
-
|
|
979
|
+
});
|
|
980
|
+
if ('pending' in answered)
|
|
981
|
+
return answered.pending;
|
|
982
|
+
const held = answered.cover;
|
|
983
|
+
return { ...answered.rows, coverage: held.coverage };
|
|
984
|
+
}
|
|
985
|
+
async function everyRow(plan) {
|
|
986
|
+
const window = { ...plan, limit: MAXIMUM_WINDOW, after: null, total: false };
|
|
987
|
+
const first = await rowsQuery(window);
|
|
988
|
+
if ('pending' in first || first.next === undefined)
|
|
989
|
+
return first;
|
|
990
|
+
const documents = 'documents' in first ? [...first.documents] : [];
|
|
991
|
+
const groups = 'groups' in first ? [...first.groups] : [];
|
|
992
|
+
for (let next = first.next; next !== undefined;) {
|
|
993
|
+
const values = JSON.parse(next);
|
|
994
|
+
const page = await rowsQuery({
|
|
995
|
+
...window,
|
|
996
|
+
after: { text: next, values: values.length === 0 ? null : values },
|
|
997
|
+
});
|
|
998
|
+
if ('pending' in page)
|
|
999
|
+
return page;
|
|
1000
|
+
if ('documents' in page)
|
|
1001
|
+
documents.push(...page.documents);
|
|
1002
|
+
else
|
|
1003
|
+
groups.push(...page.groups);
|
|
1004
|
+
if (JSON.stringify(page.coverage) !== JSON.stringify(first.coverage))
|
|
1005
|
+
return everyRow(plan);
|
|
1006
|
+
next = page.next;
|
|
1007
|
+
}
|
|
1008
|
+
return 'documents' in first
|
|
1009
|
+
? { documents, coverage: first.coverage }
|
|
1010
|
+
: { groups, coverage: first.coverage };
|
|
896
1011
|
}
|
|
897
1012
|
async function indexConversation(conversation) {
|
|
898
1013
|
searchIndex.indexMessages(conversation, [], false);
|
|
@@ -950,7 +1065,10 @@ export function composeRuntime(options) {
|
|
|
950
1065
|
for (const locator of set.queues)
|
|
951
1066
|
emitQueue(locator);
|
|
952
1067
|
if (set.drafts.length > 0 ||
|
|
953
|
-
set.documents.some((change) =>
|
|
1068
|
+
set.documents.some((change) => {
|
|
1069
|
+
const held = roomDrafts.get(change.locator);
|
|
1070
|
+
return held !== undefined && (change.path === null || held.has(change.path));
|
|
1071
|
+
})) {
|
|
954
1072
|
publishRoomDrafts(false);
|
|
955
1073
|
}
|
|
956
1074
|
for (const app of set.presentations)
|
|
@@ -963,9 +1081,21 @@ export function composeRuntime(options) {
|
|
|
963
1081
|
if (reason === 'revoked-authorization') {
|
|
964
1082
|
await exclusive(async () => {
|
|
965
1083
|
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
1084
|
+
const records = [...replicas].map(([locator, held]) => ({
|
|
1085
|
+
locator,
|
|
1086
|
+
held,
|
|
1087
|
+
record: { ...held.record, model: revoked, selectionModel: revoked },
|
|
1088
|
+
}));
|
|
966
1089
|
try {
|
|
967
1090
|
const local = await openLocal();
|
|
968
|
-
await local.
|
|
1091
|
+
await local.commit({
|
|
1092
|
+
rooms: records.map(({ locator, record }) => ({
|
|
1093
|
+
locator,
|
|
1094
|
+
key: record.key,
|
|
1095
|
+
record: storedRecord(record),
|
|
1096
|
+
composition: compositionOf(record),
|
|
1097
|
+
})),
|
|
1098
|
+
});
|
|
969
1099
|
}
|
|
970
1100
|
catch {
|
|
971
1101
|
journal.record('model revocation persistence failed');
|
|
@@ -980,22 +1110,17 @@ export function composeRuntime(options) {
|
|
|
980
1110
|
models.set(key, revoked);
|
|
981
1111
|
liveModels.delete(key);
|
|
982
1112
|
}
|
|
983
|
-
const revocations = [];
|
|
984
|
-
for (const locator of [...rooms.keys()]) {
|
|
985
|
-
const before = replicas.get(locator) ?? null;
|
|
986
|
-
const after = before === null
|
|
987
|
-
? null
|
|
988
|
-
: await stepped(replicating(await confirmedOf({ ...before.confirmed.record, model: revoked, selectionModel: revoked }, before.confirmed), before.queue, before));
|
|
989
|
-
const set = await stepped(changing(locator, before, after, { room: true }));
|
|
990
|
-
revocations.push([locator, before, after, set]);
|
|
991
|
-
}
|
|
992
1113
|
const changed = [];
|
|
993
|
-
for (const
|
|
1114
|
+
for (const { locator, held, record } of records) {
|
|
1115
|
+
if (replicas.get(locator) !== held)
|
|
1116
|
+
continue;
|
|
1117
|
+
recomposed(locator, held.record, record);
|
|
1118
|
+
replicas.set(locator, { ...held, record });
|
|
1119
|
+
}
|
|
1120
|
+
for (const locator of [...rooms.keys()]) {
|
|
994
1121
|
const room = rooms.get(locator);
|
|
995
|
-
if (room === undefined
|
|
1122
|
+
if (room === undefined)
|
|
996
1123
|
continue;
|
|
997
|
-
if (after !== null)
|
|
998
|
-
replicas.set(locator, after);
|
|
999
1124
|
rooms.set(locator, {
|
|
1000
1125
|
...room,
|
|
1001
1126
|
model: modelTypes(revoked),
|
|
@@ -1003,9 +1128,10 @@ export function composeRuntime(options) {
|
|
|
1003
1128
|
? null
|
|
1004
1129
|
: { ...room.typedSelection, model: modelTypes(revoked) },
|
|
1005
1130
|
});
|
|
1006
|
-
changed.push(
|
|
1131
|
+
changed.push(changeSet(locator, { documents: null, room: true }));
|
|
1007
1132
|
}
|
|
1008
1133
|
publishChangeSet(mergeChangeSets(changed));
|
|
1134
|
+
prepare();
|
|
1009
1135
|
});
|
|
1010
1136
|
}
|
|
1011
1137
|
}
|
|
@@ -1036,7 +1162,7 @@ export function composeRuntime(options) {
|
|
|
1036
1162
|
model: modelTypes(modelOf(room.locator, 'typed-selection')),
|
|
1037
1163
|
},
|
|
1038
1164
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
1039
|
-
roomVersion: replicas.get(room.locator)?.
|
|
1165
|
+
roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
|
|
1040
1166
|
synchronizationAvailable,
|
|
1041
1167
|
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
1042
1168
|
});
|
|
@@ -1068,7 +1194,7 @@ export function composeRuntime(options) {
|
|
|
1068
1194
|
else
|
|
1069
1195
|
unlistedKept(unlisted);
|
|
1070
1196
|
for (const room of listed) {
|
|
1071
|
-
const held = replicas.get(room.locator)?.
|
|
1197
|
+
const held = replicas.get(room.locator)?.record;
|
|
1072
1198
|
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
1073
1199
|
list(held, false);
|
|
1074
1200
|
continue;
|
|
@@ -1099,7 +1225,7 @@ export function composeRuntime(options) {
|
|
|
1099
1225
|
}
|
|
1100
1226
|
function unlistedKept(unlisted) {
|
|
1101
1227
|
for (const locator of unlisted) {
|
|
1102
|
-
const held = replicas.get(locator)?.
|
|
1228
|
+
const held = replicas.get(locator)?.record;
|
|
1103
1229
|
if (held !== undefined)
|
|
1104
1230
|
list(held, false);
|
|
1105
1231
|
else
|
|
@@ -1110,7 +1236,6 @@ export function composeRuntime(options) {
|
|
|
1110
1236
|
const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
|
|
1111
1237
|
const removed = fresh.map((locator) => ({
|
|
1112
1238
|
locator,
|
|
1113
|
-
shown: prepared.has(locator) || projecting.has(locator) ? (replicas.get(locator) ?? null) : null,
|
|
1114
1239
|
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
1115
1240
|
}));
|
|
1116
1241
|
for (const locator of fresh) {
|
|
@@ -1147,36 +1272,48 @@ export function composeRuntime(options) {
|
|
|
1147
1272
|
if (fresh.length === 0)
|
|
1148
1273
|
return fresh;
|
|
1149
1274
|
searchIndex.forget(fresh);
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
void withdrawn.catch(unpublished);
|
|
1153
|
-
publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, null, null, { room: true, queue: true, drafts }))));
|
|
1154
|
-
readSide.recover();
|
|
1275
|
+
publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, { documents: null, room: true, queue: true, drafts }))));
|
|
1276
|
+
liveQueries.recover();
|
|
1155
1277
|
moveTo({ ...status, ...attention() });
|
|
1156
1278
|
return fresh;
|
|
1157
1279
|
}
|
|
1158
|
-
function
|
|
1159
|
-
const sets = [];
|
|
1160
|
-
for (const { locator, shown } of removed)
|
|
1161
|
-
sets.push(yield* changing(locator, shown, null));
|
|
1162
|
-
return mergeChangeSets(sets);
|
|
1163
|
-
}
|
|
1164
|
-
async function erased(local, locators) {
|
|
1165
|
-
await local.erase(locators);
|
|
1280
|
+
async function eraseFenced(locators) {
|
|
1166
1281
|
for (const locator of locators) {
|
|
1282
|
+
while (await durably((local) => local.erase(locator, ERASE_BATCH)))
|
|
1283
|
+
await yielded();
|
|
1167
1284
|
const held = fences.get(locator);
|
|
1168
1285
|
if (held !== undefined)
|
|
1169
1286
|
held.erased = true;
|
|
1170
1287
|
}
|
|
1171
1288
|
}
|
|
1289
|
+
let sweeping = false;
|
|
1290
|
+
function sweep() {
|
|
1291
|
+
if (sweeping || closed)
|
|
1292
|
+
return;
|
|
1293
|
+
sweeping = true;
|
|
1294
|
+
void (async () => {
|
|
1295
|
+
try {
|
|
1296
|
+
while (!closed && (await durably((local) => local.erase(null, ERASE_BATCH)))) {
|
|
1297
|
+
await yielded();
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
catch {
|
|
1301
|
+
journal.record('erasure failed');
|
|
1302
|
+
}
|
|
1303
|
+
finally {
|
|
1304
|
+
sweeping = false;
|
|
1305
|
+
}
|
|
1306
|
+
})();
|
|
1307
|
+
}
|
|
1172
1308
|
async function loseRooms(locators) {
|
|
1173
1309
|
const fresh = fence(locators);
|
|
1174
1310
|
if (fresh.length === 0)
|
|
1175
1311
|
return;
|
|
1176
1312
|
await durably(async (local) => {
|
|
1177
|
-
await
|
|
1313
|
+
await local.fence(fresh);
|
|
1178
1314
|
await unpair(local);
|
|
1179
1315
|
});
|
|
1316
|
+
await eraseFenced(fresh);
|
|
1180
1317
|
}
|
|
1181
1318
|
function restored(key, persisted) {
|
|
1182
1319
|
const prior = models.get(key);
|
|
@@ -1197,50 +1334,30 @@ export function composeRuntime(options) {
|
|
|
1197
1334
|
return model;
|
|
1198
1335
|
}
|
|
1199
1336
|
async function hydrate(opened) {
|
|
1200
|
-
const [
|
|
1201
|
-
opened.
|
|
1337
|
+
const [held, queues, kept, heldConversations, chatDrafts] = await Promise.all([
|
|
1338
|
+
opened.opened(),
|
|
1202
1339
|
opened.queues(),
|
|
1203
1340
|
opened.roomDrafts(),
|
|
1204
|
-
opened.presentation(),
|
|
1205
1341
|
opened.conversations(),
|
|
1206
1342
|
opened.chatDrafts(),
|
|
1207
|
-
opened.lastCompleted(),
|
|
1208
|
-
opened.providerLimits(),
|
|
1209
|
-
opened.capabilities(),
|
|
1210
|
-
opened.member(),
|
|
1211
|
-
opened.transfers(),
|
|
1212
|
-
opened.targets(),
|
|
1213
1343
|
]);
|
|
1344
|
+
const records = [...held.rooms.values()];
|
|
1345
|
+
const counts = await opened.counts(records.map(({ key, typedSelection }) => ({ key, roots: typedSelection?.roots ?? [] })));
|
|
1214
1346
|
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
1215
|
-
|
|
1216
|
-
const read = new Map();
|
|
1217
|
-
for (const record of records) {
|
|
1218
|
-
const model = restored(modelKey(record.locator, 'collection'), record.model);
|
|
1219
|
-
const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
|
|
1220
|
-
const prior = previousReplicas.get(record.locator);
|
|
1221
|
-
const confirmed = await confirmedOf({ ...record, model, selectionModel }, prior?.confirmed);
|
|
1222
|
-
const replica = await stepped(replicating(confirmed, queuedBatches.get(record.locator) ?? [], prior));
|
|
1223
|
-
await stepped(counting(replica));
|
|
1224
|
-
read.set(record.locator, replica);
|
|
1225
|
-
}
|
|
1226
|
-
const sets = new Map();
|
|
1227
|
-
for (const locator of new Set([...previousReplicas.keys(), ...read.keys()])) {
|
|
1228
|
-
sets.set(locator, await stepped(changing(locator, previousReplicas.get(locator) ?? null, read.get(locator) ?? null)));
|
|
1229
|
-
}
|
|
1230
|
-
targets = heldTargets;
|
|
1347
|
+
targets = held.targets;
|
|
1231
1348
|
transfers.clear();
|
|
1232
|
-
for (const record of
|
|
1233
|
-
transfers.set(
|
|
1234
|
-
limits =
|
|
1235
|
-
capabilities =
|
|
1236
|
-
member =
|
|
1349
|
+
for (const [locator, record] of held.transfers)
|
|
1350
|
+
transfers.set(locator, record);
|
|
1351
|
+
limits = held.providerLimits;
|
|
1352
|
+
capabilities = held.capabilities;
|
|
1353
|
+
member = held.member;
|
|
1237
1354
|
roomDrafts.clear();
|
|
1238
1355
|
for (const record of kept)
|
|
1239
1356
|
rememberRoomDraft(record);
|
|
1240
1357
|
presentations.clear();
|
|
1241
|
-
for (const record of
|
|
1358
|
+
for (const record of held.presentation)
|
|
1242
1359
|
presentations.set(record.app, record.value);
|
|
1243
|
-
conversations.hydrate(
|
|
1360
|
+
conversations.hydrate(heldConversations, chatDrafts);
|
|
1244
1361
|
pendings.clear();
|
|
1245
1362
|
for (const queue of queues) {
|
|
1246
1363
|
for (const { batch, state } of queue.pending) {
|
|
@@ -1248,17 +1365,27 @@ export function composeRuntime(options) {
|
|
|
1248
1365
|
}
|
|
1249
1366
|
}
|
|
1250
1367
|
replicas.clear();
|
|
1368
|
+
prepared.clear();
|
|
1251
1369
|
queuesWithoutReplica.clear();
|
|
1252
1370
|
for (const [locator, queue] of queuedBatches) {
|
|
1253
|
-
if (!
|
|
1371
|
+
if (!held.rooms.has(locator))
|
|
1254
1372
|
queuesWithoutReplica.set(locator, queue);
|
|
1255
1373
|
}
|
|
1256
|
-
|
|
1257
|
-
if (fences.has(
|
|
1258
|
-
|
|
1259
|
-
const
|
|
1260
|
-
const
|
|
1261
|
-
|
|
1374
|
+
records.forEach((stored, index) => {
|
|
1375
|
+
if (fences.has(stored.locator))
|
|
1376
|
+
return;
|
|
1377
|
+
const model = restored(modelKey(stored.locator, 'collection'), stored.model);
|
|
1378
|
+
const selectionModel = restored(modelKey(stored.locator, 'typed-selection'), stored.selectionModel);
|
|
1379
|
+
const record = { ...stored, model, selectionModel };
|
|
1380
|
+
replicas.set(record.locator, {
|
|
1381
|
+
record,
|
|
1382
|
+
queue: queuedBatches.get(record.locator) ?? [],
|
|
1383
|
+
overlaid: held.overlaid.get(record.key) ?? new Set(),
|
|
1384
|
+
count: counts[index] ?? 0,
|
|
1385
|
+
confirmed: 0,
|
|
1386
|
+
});
|
|
1387
|
+
if (!held.unprepared.has(record.key))
|
|
1388
|
+
prepared.add(record.locator);
|
|
1262
1389
|
const existing = rooms.get(record.locator);
|
|
1263
1390
|
if (existing === undefined)
|
|
1264
1391
|
list(record, false);
|
|
@@ -1277,11 +1404,15 @@ export function composeRuntime(options) {
|
|
|
1277
1404
|
},
|
|
1278
1405
|
});
|
|
1279
1406
|
}
|
|
1280
|
-
}
|
|
1407
|
+
});
|
|
1408
|
+
for (const key of held.orphans)
|
|
1409
|
+
await opened.release(key);
|
|
1410
|
+
if (held.erasing || held.orphans.length > 0)
|
|
1411
|
+
queueMicrotask(sweep);
|
|
1281
1412
|
return {
|
|
1282
|
-
lastCompleted,
|
|
1413
|
+
lastCompleted: held.lastCompleted,
|
|
1283
1414
|
locators: new Set([...queuedBatches.keys(), ...replicas.keys()]),
|
|
1284
|
-
read: mergeChangeSets([...
|
|
1415
|
+
read: mergeChangeSets([...replicas.keys()].map((locator) => changeSet(locator, { documents: null }))),
|
|
1285
1416
|
};
|
|
1286
1417
|
}
|
|
1287
1418
|
async function readState(opened) {
|
|
@@ -1300,7 +1431,7 @@ export function composeRuntime(options) {
|
|
|
1300
1431
|
async function open() {
|
|
1301
1432
|
if (closed)
|
|
1302
1433
|
throw new Error('runtime closed');
|
|
1303
|
-
const opened = await openRuntimeStore(isFenced);
|
|
1434
|
+
const opened = await openRuntimeStore(isFenced, options.database);
|
|
1304
1435
|
store = opened;
|
|
1305
1436
|
const interrupted = await opened.fences();
|
|
1306
1437
|
for (const locator of interrupted) {
|
|
@@ -1320,8 +1451,13 @@ export function composeRuntime(options) {
|
|
|
1320
1451
|
emitStorageLoss();
|
|
1321
1452
|
}
|
|
1322
1453
|
const lastCompleted = await readState(opened);
|
|
1323
|
-
if (interrupted.length > 0)
|
|
1324
|
-
|
|
1454
|
+
if (interrupted.length > 0) {
|
|
1455
|
+
queueMicrotask(() => {
|
|
1456
|
+
void eraseFenced(interrupted).catch(() => {
|
|
1457
|
+
journal.record('erasure failed');
|
|
1458
|
+
});
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1325
1461
|
moveTo({ ...status, lastCompleted, ...attention() });
|
|
1326
1462
|
void exclusive(() => unpair(opened)).catch(() => {
|
|
1327
1463
|
journal.record('unpair failed');
|
|
@@ -1352,15 +1488,31 @@ export function composeRuntime(options) {
|
|
|
1352
1488
|
return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
|
|
1353
1489
|
};
|
|
1354
1490
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1491
|
+
const REWRITE_LINKS = ['field', 'body'];
|
|
1492
|
+
const CHECKED_LINKS = ['field', 'section'];
|
|
1493
|
+
async function sliceAt(record, view, queue, extra = [], moved = []) {
|
|
1494
|
+
const local = await openLocal();
|
|
1495
|
+
const needed = new Set([...touchedBy(queue), ...extra]);
|
|
1496
|
+
const movedPaths = [...new Set([...movedBy(queue, record.locator), ...moved])];
|
|
1497
|
+
if (movedPaths.length > 0) {
|
|
1498
|
+
for (const { path } of await local.referrers(record.key, view, movedPaths, REWRITE_LINKS)) {
|
|
1499
|
+
needed.add(path);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
const paths = [...needed];
|
|
1503
|
+
return stepped(slicing(await local.entries(record.key, view, paths), paths));
|
|
1504
|
+
}
|
|
1505
|
+
async function landedWhole(locator, batch, confirmedLayer, current) {
|
|
1506
|
+
if (!landed(batch, confirmedLayer, current))
|
|
1357
1507
|
return false;
|
|
1358
1508
|
const partner = partnerOf(batch, locator);
|
|
1359
1509
|
if (partner === null)
|
|
1360
1510
|
return true;
|
|
1361
1511
|
const other = replicas.get(partner);
|
|
1362
1512
|
const half = other?.queue.find((one) => one.id === batch.id);
|
|
1363
|
-
|
|
1513
|
+
if (other === undefined || half === undefined)
|
|
1514
|
+
return false;
|
|
1515
|
+
return landed(half, await sliceAt(other.record, 'confirmed', [half]));
|
|
1364
1516
|
}
|
|
1365
1517
|
function carriedBack(locator, unlanded, written) {
|
|
1366
1518
|
const back = new Map();
|
|
@@ -1398,7 +1550,7 @@ export function composeRuntime(options) {
|
|
|
1398
1550
|
roomDrafts: records.filter((record) => record.locator === room),
|
|
1399
1551
|
consumedRoomDrafts: elsewhere.filter((one) => one.locator === room).map(({ path }) => path),
|
|
1400
1552
|
})),
|
|
1401
|
-
changes: [...rooms].map((room) => changeSet(room,
|
|
1553
|
+
changes: [...rooms].map((room) => changeSet(room, {
|
|
1402
1554
|
drafts: [...records, ...elsewhere]
|
|
1403
1555
|
.filter((one) => one.locator === room)
|
|
1404
1556
|
.map(({ path }) => path),
|
|
@@ -1411,25 +1563,106 @@ export function composeRuntime(options) {
|
|
|
1411
1563
|
},
|
|
1412
1564
|
};
|
|
1413
1565
|
}
|
|
1414
|
-
|
|
1566
|
+
function recomposed(locator, before, after) {
|
|
1567
|
+
if (before !== undefined &&
|
|
1568
|
+
before.key === after.key &&
|
|
1569
|
+
fingerprint(compositionOf(before)) === fingerprint(compositionOf(after))) {
|
|
1570
|
+
return;
|
|
1571
|
+
}
|
|
1572
|
+
if (before !== undefined && before.key === after.key)
|
|
1573
|
+
prepared.delete(locator);
|
|
1574
|
+
}
|
|
1575
|
+
function committedRoom(locator, record, answer) {
|
|
1576
|
+
if (answer.prepared[locator] === false)
|
|
1577
|
+
prepared.delete(locator);
|
|
1578
|
+
else if (answer.prepared[locator] === true)
|
|
1579
|
+
prepared.add(locator);
|
|
1580
|
+
if (!prepared.has(locator))
|
|
1581
|
+
prepare([locator]);
|
|
1582
|
+
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0 };
|
|
1583
|
+
}
|
|
1584
|
+
function replayed(record, confirmedLayer, queue) {
|
|
1585
|
+
return stepped(replaying(confirmedLayer, queue, record.locator, compositionOf(record)));
|
|
1586
|
+
}
|
|
1587
|
+
async function overlayDiff(record, before, visible, touched) {
|
|
1588
|
+
const kept = before !== undefined && before.record.key === record.key
|
|
1589
|
+
? before.overlaid
|
|
1590
|
+
: new Set();
|
|
1591
|
+
return {
|
|
1592
|
+
overlay: {
|
|
1593
|
+
set: await stepped(overlayOf(visible, touched)),
|
|
1594
|
+
cleared: [...kept].filter((path) => !touched.has(path)),
|
|
1595
|
+
},
|
|
1596
|
+
touched,
|
|
1597
|
+
};
|
|
1598
|
+
}
|
|
1599
|
+
async function settle(local, locator, before, record, queue, settling = {}) {
|
|
1600
|
+
return together(local, [await settlement(local, locator, before, record, queue, settling)]);
|
|
1601
|
+
}
|
|
1602
|
+
function unpairedIn(locator, batch) {
|
|
1603
|
+
if (!queueIn(locator).some((held) => held.id === batch.id))
|
|
1604
|
+
return false;
|
|
1605
|
+
if (pendings.get(pendingKey(locator, batch.id))?.accepted !== undefined)
|
|
1606
|
+
return false;
|
|
1607
|
+
return !leavingIn(batch, locator) || withdrawable(locator)(batch);
|
|
1608
|
+
}
|
|
1609
|
+
async function together(local, settlements) {
|
|
1610
|
+
const settled = new Set(settlements.map(({ locator }) => locator));
|
|
1611
|
+
const all = [...settlements];
|
|
1612
|
+
for (let wave = settlements; wave.length > 0;) {
|
|
1613
|
+
const partnered = new Map();
|
|
1614
|
+
for (const { locator, left } of wave) {
|
|
1615
|
+
for (const batch of left) {
|
|
1616
|
+
const partner = partnerOf(batch, locator);
|
|
1617
|
+
if (partner === null || settled.has(partner) || !unpairedIn(partner, batch))
|
|
1618
|
+
continue;
|
|
1619
|
+
partnered.set(partner, [...(partnered.get(partner) ?? []), batch]);
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
const partners = [];
|
|
1623
|
+
for (const [partner, batches] of partnered) {
|
|
1624
|
+
settled.add(partner);
|
|
1625
|
+
partners.push(await unpairing(local, partner, batches));
|
|
1626
|
+
}
|
|
1627
|
+
all.push(...partners);
|
|
1628
|
+
wave = partners;
|
|
1629
|
+
}
|
|
1630
|
+
const answer = await local.commit({
|
|
1631
|
+
rooms: all.flatMap(({ commit }) => commit.rooms),
|
|
1632
|
+
writes: all.flatMap(({ commit }) => commit.writes),
|
|
1633
|
+
});
|
|
1634
|
+
return mergeChangeSets(all.map((one) => one.apply(answer)));
|
|
1635
|
+
}
|
|
1636
|
+
async function settlement(local, locator, before, record, queue, settling = {}) {
|
|
1637
|
+
const view = settling.view ?? 'confirmed';
|
|
1415
1638
|
const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
1416
|
-
const
|
|
1639
|
+
const overrides = settling.changes;
|
|
1640
|
+
const read = await sliceAt(record, view, queue, [
|
|
1641
|
+
...(before !== undefined && before.record.key === record.key ? before.overlaid : []),
|
|
1642
|
+
...(overrides?.set.map(({ path }) => path) ?? []),
|
|
1643
|
+
...(overrides?.removed ?? []),
|
|
1644
|
+
]);
|
|
1645
|
+
const confirmedLayer = overrides === undefined
|
|
1646
|
+
? read
|
|
1647
|
+
: await stepped(slicingWith(read, overrides.set, overrides.removed));
|
|
1648
|
+
const composition = compositionOf(record);
|
|
1649
|
+
const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
|
|
1417
1650
|
const founded = kept.filter((batch) => !queue.includes(batch));
|
|
1418
1651
|
const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
|
|
1419
|
-
const unqueued =
|
|
1420
|
-
|
|
1421
|
-
landed: landedWhole(locator, batch,
|
|
1422
|
-
}
|
|
1652
|
+
const unqueued = [];
|
|
1653
|
+
for (const batch of withdrawn) {
|
|
1654
|
+
unqueued.push({ batch, landed: await landedWhole(locator, batch, confirmedLayer) });
|
|
1655
|
+
}
|
|
1423
1656
|
const returning = [
|
|
1424
|
-
...[...unqueued].reverse().map(({ batch, landed }) => ({
|
|
1657
|
+
...[...unqueued].reverse().map(({ batch, landed: held }) => ({
|
|
1425
1658
|
batch,
|
|
1426
|
-
returned: localSaves ||
|
|
1659
|
+
returned: localSaves || held ? null : returnedBy('withdrawn'),
|
|
1427
1660
|
})),
|
|
1428
|
-
...(settling.leaving
|
|
1661
|
+
...(settling.leaving ?? []),
|
|
1429
1662
|
];
|
|
1430
1663
|
const written = new Map();
|
|
1431
|
-
for (const { batch, returned } of returning) {
|
|
1432
|
-
if (
|
|
1664
|
+
for (const { batch, returned: reason } of returning) {
|
|
1665
|
+
if (reason === null || batch.between?.destination.room === locator)
|
|
1433
1666
|
continue;
|
|
1434
1667
|
for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
|
|
1435
1668
|
const existing = written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path);
|
|
@@ -1438,73 +1671,85 @@ export function composeRuntime(options) {
|
|
|
1438
1671
|
path: saved.path,
|
|
1439
1672
|
revision: roomDraftRevision(),
|
|
1440
1673
|
parts: returnedInto(existing?.parts, saved.parts),
|
|
1441
|
-
returned,
|
|
1674
|
+
returned: reason,
|
|
1442
1675
|
});
|
|
1443
1676
|
}
|
|
1444
1677
|
}
|
|
1445
1678
|
const carried = carriedBack(locator, [
|
|
1446
|
-
...[...unqueued].reverse().flatMap(({ batch, landed }) => (
|
|
1447
|
-
...(settling.leaving
|
|
1448
|
-
? []
|
|
1449
|
-
: [settling.leaving.batch]),
|
|
1679
|
+
...[...unqueued].reverse().flatMap(({ batch, landed: held }) => (held ? [] : [batch])),
|
|
1680
|
+
...(settling.leaving ?? []).flatMap(({ batch, returned }) => returned === null ? [] : [batch]),
|
|
1450
1681
|
], written);
|
|
1451
|
-
await
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1682
|
+
const replayedLayer = await replayed(record, confirmedLayer, kept);
|
|
1683
|
+
const { overlay, touched } = await overlayDiff(record, before, replayedLayer.visible, replayedLayer.touched);
|
|
1684
|
+
const commit = {
|
|
1685
|
+
rooms: [
|
|
1686
|
+
{
|
|
1687
|
+
locator,
|
|
1688
|
+
key: record.key,
|
|
1689
|
+
record: storedRecord(record),
|
|
1690
|
+
roots: record.typedSelection?.roots ?? [],
|
|
1691
|
+
...settling.commit,
|
|
1692
|
+
...(overrides === undefined
|
|
1693
|
+
? {}
|
|
1694
|
+
: { confirmed: { set: overrides.set, removed: overrides.removed } }),
|
|
1695
|
+
overlay,
|
|
1696
|
+
},
|
|
1697
|
+
],
|
|
1698
|
+
writes: [
|
|
1699
|
+
{
|
|
1700
|
+
locator,
|
|
1701
|
+
queue: kept,
|
|
1702
|
+
founded,
|
|
1703
|
+
roomDrafts: [...written.values()],
|
|
1704
|
+
consumedRoomDrafts: carried.taken,
|
|
1705
|
+
},
|
|
1706
|
+
...carried.writes,
|
|
1707
|
+
],
|
|
1708
|
+
};
|
|
1458
1709
|
const staying = new Set(kept.map((batch) => batch.id));
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
});
|
|
1473
|
-
}
|
|
1474
|
-
catch (error) {
|
|
1475
|
-
const held = before === undefined ? null : await stepped(replicating(before.confirmed, kept, before));
|
|
1476
|
-
const set = await stepped(changing(locator, before ?? null, held, { queue: queueChanged, drafts }));
|
|
1477
|
-
if (held !== null)
|
|
1478
|
-
replicas.set(locator, held);
|
|
1479
|
-
else if (kept.length > 0)
|
|
1480
|
-
queuesWithoutReplica.set(locator, kept);
|
|
1481
|
-
else
|
|
1710
|
+
return {
|
|
1711
|
+
locator,
|
|
1712
|
+
left: previousQueue.filter((batch) => !staying.has(batch.id)),
|
|
1713
|
+
commit,
|
|
1714
|
+
apply: (answer) => {
|
|
1715
|
+
for (const batch of previousQueue) {
|
|
1716
|
+
if (!staying.has(batch.id))
|
|
1717
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
1718
|
+
}
|
|
1719
|
+
for (const draft of written.values())
|
|
1720
|
+
rememberRoomDraft(draft);
|
|
1721
|
+
carried.remember();
|
|
1722
|
+
const drafts = [...written.keys(), ...carried.taken];
|
|
1482
1723
|
queuesWithoutReplica.delete(locator);
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1724
|
+
for (const { batch, landed: held } of unqueued) {
|
|
1725
|
+
const outcome = localSaves ? 'dropped' : held ? 'landed' : 'withdrawn';
|
|
1726
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
1727
|
+
}
|
|
1728
|
+
const changed = answer.changed[locator];
|
|
1729
|
+
const confirmedChanged = settling.commit?.published !== undefined ||
|
|
1730
|
+
settling.commit?.refreshed === true ||
|
|
1731
|
+
overrides !== undefined;
|
|
1732
|
+
recomposed(locator, before?.record, record);
|
|
1733
|
+
const { count } = committedRoom(locator, record, answer);
|
|
1734
|
+
replicas.set(locator, {
|
|
1735
|
+
record,
|
|
1736
|
+
queue: kept,
|
|
1737
|
+
overlaid: touched,
|
|
1738
|
+
count,
|
|
1739
|
+
confirmed: (before?.confirmed ?? 0) + (confirmedChanged ? 1 : 0),
|
|
1740
|
+
});
|
|
1741
|
+
return mergeChangeSets([
|
|
1742
|
+
changeSet(locator, {
|
|
1743
|
+
documents: changed === undefined ? [] : changed,
|
|
1744
|
+
room: settling.room,
|
|
1745
|
+
queue: queueChanged,
|
|
1746
|
+
drafts,
|
|
1747
|
+
}),
|
|
1748
|
+
...carried.changes,
|
|
1749
|
+
]);
|
|
1750
|
+
},
|
|
1502
1751
|
};
|
|
1503
1752
|
}
|
|
1504
|
-
function heldChange(locator, before, next, set, changed) {
|
|
1505
|
-
const held = replicas.get(locator) ?? null;
|
|
1506
|
-
return held === next ? set : changeSet(locator, before, held, changed);
|
|
1507
|
-
}
|
|
1508
1753
|
function adoptModels(locator, record) {
|
|
1509
1754
|
for (const [attached, model] of [
|
|
1510
1755
|
['collection', record.model],
|
|
@@ -1523,7 +1768,7 @@ export function composeRuntime(options) {
|
|
|
1523
1768
|
const taken = (answer, previous, attached) => {
|
|
1524
1769
|
if (answer === undefined)
|
|
1525
1770
|
return previous;
|
|
1526
|
-
const { state, refused } = attachedModel(answer, previous);
|
|
1771
|
+
const { state, refused } = attachedModel(answer, previous, attached === 'collection' ? roomTypes : NO_ROOM_TYPES);
|
|
1527
1772
|
if (refused !== null) {
|
|
1528
1773
|
const where = refused.location === undefined ? '' : ` ${refused.location}`;
|
|
1529
1774
|
journal.record(`model invalid ${locator} ${attached} ${refused.identity} ${refused.rule}${where}`);
|
|
@@ -1536,44 +1781,54 @@ export function composeRuntime(options) {
|
|
|
1536
1781
|
selection: taken(answers?.selection, held.selection, 'typed-selection'),
|
|
1537
1782
|
};
|
|
1538
1783
|
}
|
|
1539
|
-
function publish(local, room,
|
|
1540
|
-
const { base, stored } = landing;
|
|
1784
|
+
function publish(local, room, record, landing, live) {
|
|
1541
1785
|
return exclusive(async () => {
|
|
1542
|
-
if (stored !== undefined) {
|
|
1543
|
-
await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
|
|
1544
|
-
}
|
|
1545
1786
|
if (!live()) {
|
|
1546
1787
|
caughtUpTo(room.locator);
|
|
1547
1788
|
return true;
|
|
1548
1789
|
}
|
|
1549
1790
|
const prior = replicas.get(room.locator);
|
|
1550
|
-
if (
|
|
1551
|
-
(prior
|
|
1552
|
-
|
|
1791
|
+
if (landing.kind === 'refreshed' &&
|
|
1792
|
+
(prior === undefined || prior.record.key !== record.key || prior.confirmed !== landing.base)) {
|
|
1793
|
+
await local.unrefresh(record.key);
|
|
1553
1794
|
return false;
|
|
1554
1795
|
}
|
|
1555
|
-
adoptModels(room.locator,
|
|
1796
|
+
adoptModels(room.locator, record);
|
|
1556
1797
|
const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
|
|
1557
|
-
const roomVersion =
|
|
1798
|
+
const roomVersion = record.roomVersion;
|
|
1558
1799
|
const accepted = queue.filter((batch) => {
|
|
1559
|
-
const
|
|
1560
|
-
return
|
|
1800
|
+
const at = pendings.get(pendingKey(room.locator, batch.id))?.accepted;
|
|
1801
|
+
return at !== undefined && options.adapter.reached(room.locator, roomVersion, at);
|
|
1561
1802
|
});
|
|
1562
|
-
let
|
|
1803
|
+
let set;
|
|
1563
1804
|
try {
|
|
1564
|
-
|
|
1805
|
+
set = await settle(local, room.locator, prior, record, queue.filter((batch) => !accepted.includes(batch)), landing.kind === 'refreshed'
|
|
1806
|
+
? {
|
|
1807
|
+
room: true,
|
|
1808
|
+
view: 'refreshed',
|
|
1809
|
+
commit: { refreshed: true, composition: compositionOf(record) },
|
|
1810
|
+
}
|
|
1811
|
+
: {
|
|
1812
|
+
room: true,
|
|
1813
|
+
view: landing.refreshed ? 'refreshed' : 'confirmed',
|
|
1814
|
+
commit: {
|
|
1815
|
+
published: { from: prior?.record.key ?? null },
|
|
1816
|
+
composition: compositionOf(record),
|
|
1817
|
+
...(landing.refreshed ? { refreshed: true } : {}),
|
|
1818
|
+
},
|
|
1819
|
+
});
|
|
1565
1820
|
}
|
|
1566
1821
|
catch (error) {
|
|
1567
1822
|
if (!exceeded(error))
|
|
1568
1823
|
throw error;
|
|
1569
|
-
await exceededOn(room.locator,
|
|
1824
|
+
await exceededOn(room.locator, null);
|
|
1570
1825
|
caughtUpTo(room.locator);
|
|
1571
1826
|
return true;
|
|
1572
1827
|
}
|
|
1573
|
-
|
|
1574
|
-
if (stored !== undefined) {
|
|
1828
|
+
if (landing.kind === 'bootstrapped') {
|
|
1575
1829
|
transfers.delete(room.locator);
|
|
1576
1830
|
catching.delete(room.locator);
|
|
1831
|
+
sweep();
|
|
1577
1832
|
}
|
|
1578
1833
|
for (const batch of accepted)
|
|
1579
1834
|
pendings.delete(pendingKey(room.locator, batch.id));
|
|
@@ -1584,8 +1839,6 @@ export function composeRuntime(options) {
|
|
|
1584
1839
|
sourceRefusals.delete(room.locator);
|
|
1585
1840
|
declarationRefusals.delete(room.locator);
|
|
1586
1841
|
list(room, true);
|
|
1587
|
-
if (prior === undefined)
|
|
1588
|
-
prepared.add(room.locator);
|
|
1589
1842
|
publishChangeSet(set);
|
|
1590
1843
|
await unpair(local);
|
|
1591
1844
|
moveTo({ ...status, ...attention() });
|
|
@@ -1598,7 +1851,6 @@ export function composeRuntime(options) {
|
|
|
1598
1851
|
}
|
|
1599
1852
|
async function savedRoom(locator) {
|
|
1600
1853
|
const local = await openLocal();
|
|
1601
|
-
await referenced(locator);
|
|
1602
1854
|
const replica = replicas.get(locator);
|
|
1603
1855
|
const room = rooms.get(locator);
|
|
1604
1856
|
if (replica === undefined || room === undefined) {
|
|
@@ -1628,13 +1880,19 @@ export function composeRuntime(options) {
|
|
|
1628
1880
|
}
|
|
1629
1881
|
const target = await savedRoom(locator);
|
|
1630
1882
|
const saved = [];
|
|
1883
|
+
const files = [];
|
|
1884
|
+
const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
|
|
1885
|
+
const visible = filed.length === 0 ? undefined : await visibleOf(locator, filed);
|
|
1631
1886
|
for (const { path, parts } of read) {
|
|
1632
1887
|
const file = parts.file === undefined
|
|
1633
1888
|
? undefined
|
|
1634
|
-
: await heldFile(parts.file.blob, binaryBase(
|
|
1889
|
+
: await heldFile(parts.file.blob, binaryBase(visible, path));
|
|
1890
|
+
if (parts.file !== undefined && file !== undefined) {
|
|
1891
|
+
files.push({ key: file.localId, blob: parts.file.blob });
|
|
1892
|
+
}
|
|
1635
1893
|
saved.push({ path, parts: held(undefined, parts, file) });
|
|
1636
1894
|
}
|
|
1637
|
-
return committed(locator, target, saved, new Set(), writtenBy);
|
|
1895
|
+
return committed(locator, target, saved, new Set(), writtenBy, files);
|
|
1638
1896
|
}
|
|
1639
1897
|
function carriedDrafts(locator, saved, consumed) {
|
|
1640
1898
|
const holding = roomDrafts.get(locator);
|
|
@@ -1648,8 +1906,173 @@ export function composeRuntime(options) {
|
|
|
1648
1906
|
return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
|
|
1649
1907
|
}));
|
|
1650
1908
|
}
|
|
1651
|
-
function
|
|
1652
|
-
const record = replica.
|
|
1909
|
+
async function savingIn(replica, changes, extra, leaving = []) {
|
|
1910
|
+
const record = replica.record;
|
|
1911
|
+
const local = await openLocal();
|
|
1912
|
+
const paths = [
|
|
1913
|
+
...extra,
|
|
1914
|
+
...changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]),
|
|
1915
|
+
];
|
|
1916
|
+
const moved = [
|
|
1917
|
+
...leaving,
|
|
1918
|
+
...changes.flatMap((change) => (change.kind === 'move' ? [change.path] : [])),
|
|
1919
|
+
];
|
|
1920
|
+
const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
|
|
1921
|
+
const referring = removed.length === 0
|
|
1922
|
+
? []
|
|
1923
|
+
: (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
|
|
1924
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...referring], moved);
|
|
1925
|
+
const { visible } = await replayed(record, confirmedLayer, replica.queue);
|
|
1926
|
+
const composition = compositionOf(record);
|
|
1927
|
+
return {
|
|
1928
|
+
record,
|
|
1929
|
+
confirmed: confirmedLayer,
|
|
1930
|
+
visible,
|
|
1931
|
+
*visibleOver(queue) {
|
|
1932
|
+
return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
|
|
1933
|
+
},
|
|
1934
|
+
};
|
|
1935
|
+
}
|
|
1936
|
+
async function unmodeledIn(record) {
|
|
1937
|
+
const composition = compositionFor(record);
|
|
1938
|
+
if (available(composition))
|
|
1939
|
+
return false;
|
|
1940
|
+
const local = await openLocal();
|
|
1941
|
+
const roots = record.typedSelection?.roots ?? [];
|
|
1942
|
+
if (composition.model.status === 'unavailable' &&
|
|
1943
|
+
(await local.typedWithin(record.key, 'visible', roots, false))) {
|
|
1944
|
+
return true;
|
|
1945
|
+
}
|
|
1946
|
+
return (composition.selection !== null &&
|
|
1947
|
+
composition.selection.model.status === 'unavailable' &&
|
|
1948
|
+
(await local.typedWithin(record.key, 'visible', roots, true)));
|
|
1949
|
+
}
|
|
1950
|
+
function textDiffers(left, right) {
|
|
1951
|
+
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
1952
|
+
!sameBinary(left?.binary ?? undefined, right?.binary ?? undefined));
|
|
1953
|
+
}
|
|
1954
|
+
function textOf(entry) {
|
|
1955
|
+
if (entry === null)
|
|
1956
|
+
return undefined;
|
|
1957
|
+
return entry.binary !== null ? null : entry.content;
|
|
1958
|
+
}
|
|
1959
|
+
function walkOf(local, key, view) {
|
|
1960
|
+
return async (after) => {
|
|
1961
|
+
const page = await local.walk(key, view, after, WALK_BYTES);
|
|
1962
|
+
return {
|
|
1963
|
+
entries: page.entries.map(({ path, content, binary }) => ({
|
|
1964
|
+
path,
|
|
1965
|
+
content: binary !== null ? null : content,
|
|
1966
|
+
})),
|
|
1967
|
+
next: page.next,
|
|
1968
|
+
};
|
|
1969
|
+
};
|
|
1970
|
+
}
|
|
1971
|
+
function lookupOf(local, key, view) {
|
|
1972
|
+
return async (paths) => {
|
|
1973
|
+
if (paths.length === 0)
|
|
1974
|
+
return new Map();
|
|
1975
|
+
return new Map((await local.typed(key, view, paths)).map(({ path, type, binary }) => [
|
|
1976
|
+
path,
|
|
1977
|
+
{ type: type ?? undefined, binary },
|
|
1978
|
+
]));
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
function declaredOf(entry) {
|
|
1982
|
+
if (entry.binary !== null)
|
|
1983
|
+
return { type: undefined, binary: true };
|
|
1984
|
+
const read = parseFrontmatter(entry.content ?? '');
|
|
1985
|
+
return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
|
|
1986
|
+
}
|
|
1987
|
+
function overriddenWalk(walk, overrides) {
|
|
1988
|
+
const sorted = [...overrides.keys()].sort();
|
|
1989
|
+
const beyond = (path) => {
|
|
1990
|
+
if (path === null)
|
|
1991
|
+
return 0;
|
|
1992
|
+
let low = 0;
|
|
1993
|
+
let high = sorted.length;
|
|
1994
|
+
while (low < high) {
|
|
1995
|
+
const middle = (low + high) >>> 1;
|
|
1996
|
+
if (sorted[middle] <= path)
|
|
1997
|
+
low = middle + 1;
|
|
1998
|
+
else
|
|
1999
|
+
high = middle;
|
|
2000
|
+
}
|
|
2001
|
+
return low;
|
|
2002
|
+
};
|
|
2003
|
+
return async (after) => {
|
|
2004
|
+
const page = await walk(after);
|
|
2005
|
+
const end = page.next;
|
|
2006
|
+
const added = sorted
|
|
2007
|
+
.slice(beyond(after), end === null ? sorted.length : beyond(end))
|
|
2008
|
+
.flatMap((path) => {
|
|
2009
|
+
const entry = overrides.get(path);
|
|
2010
|
+
return entry === null || entry === undefined
|
|
2011
|
+
? []
|
|
2012
|
+
: [{ path, content: entry.binary !== null ? null : entry.content }];
|
|
2013
|
+
});
|
|
2014
|
+
const entries = [...page.entries.filter(({ path }) => !overrides.has(path)), ...added].sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
2015
|
+
return { entries, next: end };
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
function overriddenLookup(lookup, overrides) {
|
|
2019
|
+
return async (paths) => {
|
|
2020
|
+
const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
|
|
2021
|
+
for (const path of paths) {
|
|
2022
|
+
const entry = overrides.get(path);
|
|
2023
|
+
if (entry === undefined)
|
|
2024
|
+
continue;
|
|
2025
|
+
if (entry === null)
|
|
2026
|
+
known.delete(path);
|
|
2027
|
+
else
|
|
2028
|
+
known.set(path, declaredOf(entry));
|
|
2029
|
+
}
|
|
2030
|
+
return known;
|
|
2031
|
+
};
|
|
2032
|
+
}
|
|
2033
|
+
function validatorOf(record, confirmedLayer) {
|
|
2034
|
+
return async (resulting, touched) => {
|
|
2035
|
+
const composition = compositionFor(record);
|
|
2036
|
+
const local = await openLocal();
|
|
2037
|
+
const changed = [];
|
|
2038
|
+
const overrides = new Map();
|
|
2039
|
+
const pace = paced();
|
|
2040
|
+
for (const path of new Set([...confirmedLayer.held, ...touched])) {
|
|
2041
|
+
await pace(ROW_WEIGHT);
|
|
2042
|
+
const was = entryAt(confirmedLayer, path);
|
|
2043
|
+
const now = entryAt(resulting, path);
|
|
2044
|
+
if (!textDiffers(was, now))
|
|
2045
|
+
continue;
|
|
2046
|
+
overrides.set(path, now);
|
|
2047
|
+
if (textOf(was) !== textOf(now))
|
|
2048
|
+
changed.push({ path, was: textOf(was), now: textOf(now) });
|
|
2049
|
+
}
|
|
2050
|
+
const lookup = overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides);
|
|
2051
|
+
const clean = available(composition) &&
|
|
2052
|
+
changed.length <= CLEAN_CHANGES &&
|
|
2053
|
+
(await changedCleanly(changed, compositionOf(record), composition, {
|
|
2054
|
+
lookup,
|
|
2055
|
+
texts: textsOf(local, record.key, 'confirmed'),
|
|
2056
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2057
|
+
}));
|
|
2058
|
+
if (clean)
|
|
2059
|
+
return { verdict: 'accept' };
|
|
2060
|
+
return validatedCollection(overriddenWalk(walkOf(local, record.key, 'confirmed'), overrides), lookup, composition);
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
2063
|
+
function textsOf(local, key, view) {
|
|
2064
|
+
return async (paths) => new Map((await local.entries(key, view, paths)).map(({ path, content, binary }) => [
|
|
2065
|
+
path,
|
|
2066
|
+
binary !== null ? null : content,
|
|
2067
|
+
]));
|
|
2068
|
+
}
|
|
2069
|
+
function referrersOf(local, key, view) {
|
|
2070
|
+
return async (targets) => [
|
|
2071
|
+
...new Set((await local.referrers(key, view, targets, CHECKED_LINKS)).map(({ path }) => path)),
|
|
2072
|
+
];
|
|
2073
|
+
}
|
|
2074
|
+
async function sideOf(locator, replica, room, saving) {
|
|
2075
|
+
const record = replica.record;
|
|
1653
2076
|
return {
|
|
1654
2077
|
room: locator,
|
|
1655
2078
|
handle: room.handle,
|
|
@@ -1657,37 +2080,71 @@ export function composeRuntime(options) {
|
|
|
1657
2080
|
composition: compositionFor(record),
|
|
1658
2081
|
operations: room.operations,
|
|
1659
2082
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
1660
|
-
visible:
|
|
2083
|
+
visible: saving.visible,
|
|
1661
2084
|
queued: queuedDocuments(replica.queue),
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
binaries: record.binaries,
|
|
1665
|
-
composition: compositionOf(record),
|
|
1666
|
-
},
|
|
2085
|
+
unmodeled: await unmodeledIn(record),
|
|
2086
|
+
validate: validatorOf(record, saving.confirmed),
|
|
1667
2087
|
};
|
|
1668
2088
|
}
|
|
1669
2089
|
function movedInto(locator, saved) {
|
|
1670
2090
|
const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
|
|
1671
2091
|
return into?.parts.move?.room ?? null;
|
|
1672
2092
|
}
|
|
1673
|
-
async function
|
|
2093
|
+
async function saveCommit(local, files, rooms) {
|
|
2094
|
+
const touched = new Map();
|
|
2095
|
+
const commits = [];
|
|
2096
|
+
for (const { locator, saving, before, queue } of rooms) {
|
|
2097
|
+
const replayedLayer = await replayed(saving.record, saving.confirmed, queue);
|
|
2098
|
+
const diff = await overlayDiff(saving.record, before, replayedLayer.visible, replayedLayer.touched);
|
|
2099
|
+
touched.set(locator, diff.touched);
|
|
2100
|
+
commits.push({
|
|
2101
|
+
locator,
|
|
2102
|
+
key: saving.record.key,
|
|
2103
|
+
roots: saving.record.typedSelection?.roots ?? [],
|
|
2104
|
+
overlay: diff.overlay,
|
|
2105
|
+
});
|
|
2106
|
+
}
|
|
2107
|
+
try {
|
|
2108
|
+
const answer = await local.commit({
|
|
2109
|
+
rooms: commits,
|
|
2110
|
+
writes: rooms.map(({ write }) => write),
|
|
2111
|
+
files,
|
|
2112
|
+
whole: true,
|
|
2113
|
+
});
|
|
2114
|
+
return { answer, touched };
|
|
2115
|
+
}
|
|
2116
|
+
catch {
|
|
2117
|
+
return null;
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
function savedReplica(locator, before, queue, touched, answer) {
|
|
2121
|
+
replicas.set(locator, {
|
|
2122
|
+
...before,
|
|
2123
|
+
queue,
|
|
2124
|
+
overlaid: touched,
|
|
2125
|
+
count: answer.counts[locator] ?? before.count,
|
|
2126
|
+
});
|
|
2127
|
+
}
|
|
2128
|
+
async function committedMove(locator, destination, { local, replica, room }, saved, consumed, writtenBy, files) {
|
|
1674
2129
|
const mixed = saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
|
|
1675
2130
|
if (mixed !== undefined) {
|
|
1676
2131
|
return { outcome: 'refused', code: 'mixed-room-move', retryable: false, file: mixed.path };
|
|
1677
2132
|
}
|
|
1678
|
-
const
|
|
2133
|
+
const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
2134
|
+
const sourceSaving = await savingIn(replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
|
|
2135
|
+
const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, sourceSaving.visible)));
|
|
1679
2136
|
if (changed.length > 0)
|
|
1680
2137
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
1681
|
-
await referenced(destination);
|
|
1682
2138
|
const target = replicas.get(destination);
|
|
1683
2139
|
const listedRoom = rooms.get(destination);
|
|
1684
2140
|
if (target === undefined || listedRoom === undefined) {
|
|
1685
2141
|
throw runtimeError('room-not-held', `No Local Replica for ${destination}`);
|
|
1686
2142
|
}
|
|
2143
|
+
const arriving = await savingIn(target, [], moves.map(({ to }) => to));
|
|
1687
2144
|
const built = await buildMove({
|
|
1688
|
-
source: sideOf(locator, replica, room),
|
|
1689
|
-
destination: sideOf(destination, target, listedRoom),
|
|
1690
|
-
moves
|
|
2145
|
+
source: await sideOf(locator, replica, room, sourceSaving),
|
|
2146
|
+
destination: await sideOf(destination, target, listedRoom, arriving),
|
|
2147
|
+
moves,
|
|
1691
2148
|
providerLimits: limits,
|
|
1692
2149
|
local: localSaves,
|
|
1693
2150
|
...(writtenBy === undefined ? {} : { writtenBy }),
|
|
@@ -1714,31 +2171,38 @@ export function composeRuntime(options) {
|
|
|
1714
2171
|
const left = [...consumed, ...carrying.map(({ from }) => from)];
|
|
1715
2172
|
const leavingQueue = [...replica.queue, built.leaving];
|
|
1716
2173
|
const arrivingQueue = [...target.queue, built.arriving];
|
|
1717
|
-
|
|
1718
|
-
|
|
2174
|
+
const committedBoth = await saveCommit(local, files, [
|
|
2175
|
+
{
|
|
1719
2176
|
locator,
|
|
2177
|
+
saving: sourceSaving,
|
|
2178
|
+
before: replica,
|
|
1720
2179
|
queue: leavingQueue,
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
2180
|
+
write: {
|
|
2181
|
+
locator,
|
|
2182
|
+
queue: leavingQueue,
|
|
2183
|
+
saved: new Map([[id, saved]]),
|
|
2184
|
+
consumedRoomDrafts: left,
|
|
2185
|
+
},
|
|
2186
|
+
},
|
|
2187
|
+
{
|
|
1724
2188
|
locator: destination,
|
|
2189
|
+
saving: arriving,
|
|
2190
|
+
before: target,
|
|
1725
2191
|
queue: arrivingQueue,
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
2192
|
+
write: {
|
|
2193
|
+
locator: destination,
|
|
2194
|
+
queue: arrivingQueue,
|
|
2195
|
+
saved: new Map([[id, marked]]),
|
|
2196
|
+
roomDrafts: carried,
|
|
2197
|
+
},
|
|
2198
|
+
},
|
|
2199
|
+
]);
|
|
2200
|
+
if (committedBoth === null)
|
|
1731
2201
|
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2202
|
+
const { answer, touched } = committedBoth;
|
|
2203
|
+
if (answer.fenced !== null) {
|
|
2204
|
+
throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
|
|
1732
2205
|
}
|
|
1733
|
-
const nextSource = await stepped(replicating(replica.confirmed, leavingQueue, replica));
|
|
1734
|
-
const nextDestination = await stepped(replicating(target.confirmed, arrivingQueue, target));
|
|
1735
|
-
const sets = [
|
|
1736
|
-
await stepped(changing(locator, replica, nextSource, { queue: true, drafts: left })),
|
|
1737
|
-
await stepped(changing(destination, target, nextDestination, {
|
|
1738
|
-
queue: true,
|
|
1739
|
-
drafts: carried.map(({ path }) => path),
|
|
1740
|
-
})),
|
|
1741
|
-
];
|
|
1742
2206
|
if (fences.has(locator) || fences.has(destination)) {
|
|
1743
2207
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
1744
2208
|
}
|
|
@@ -1756,9 +2220,20 @@ export function composeRuntime(options) {
|
|
|
1756
2220
|
forgetRoomDraft(locator, path);
|
|
1757
2221
|
for (const record of carried)
|
|
1758
2222
|
rememberRoomDraft(record);
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
publishChangeSet(mergeChangeSets(
|
|
2223
|
+
savedReplica(locator, replica, leavingQueue, touched.get(locator), answer);
|
|
2224
|
+
savedReplica(destination, target, arrivingQueue, touched.get(destination), answer);
|
|
2225
|
+
publishChangeSet(mergeChangeSets([
|
|
2226
|
+
changeSet(locator, {
|
|
2227
|
+
documents: answer.changed[locator] ?? [],
|
|
2228
|
+
queue: true,
|
|
2229
|
+
drafts: left,
|
|
2230
|
+
}),
|
|
2231
|
+
changeSet(destination, {
|
|
2232
|
+
documents: answer.changed[destination] ?? [],
|
|
2233
|
+
queue: true,
|
|
2234
|
+
drafts: carried.map(({ path }) => path),
|
|
2235
|
+
}),
|
|
2236
|
+
]));
|
|
1762
2237
|
queuedSave(locator);
|
|
1763
2238
|
return { outcome: 'saved', identifier: id };
|
|
1764
2239
|
}
|
|
@@ -1768,20 +2243,22 @@ export function composeRuntime(options) {
|
|
|
1768
2243
|
moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
|
|
1769
2244
|
submitLater(locator);
|
|
1770
2245
|
}
|
|
1771
|
-
async function committed(locator, target, saved, consumed, writtenBy) {
|
|
2246
|
+
async function committed(locator, target, saved, consumed, writtenBy, files = []) {
|
|
1772
2247
|
const into = movedInto(locator, saved);
|
|
1773
|
-
if (into !== null)
|
|
1774
|
-
return committedMove(locator, into, target, saved, consumed, writtenBy);
|
|
2248
|
+
if (into !== null) {
|
|
2249
|
+
return committedMove(locator, into, target, saved, consumed, writtenBy, files);
|
|
2250
|
+
}
|
|
1775
2251
|
const { local, replica, room } = target;
|
|
1776
|
-
const
|
|
2252
|
+
const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
2253
|
+
const changes = parts.flatMap(({ changes: made }) => made);
|
|
2254
|
+
const saving = await savingIn(replica, changes, [
|
|
2255
|
+
...saved.map(({ path }) => path),
|
|
2256
|
+
...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
|
|
2257
|
+
]);
|
|
2258
|
+
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, underneath(held, path, saving.visible)));
|
|
1777
2259
|
if (changed.length > 0)
|
|
1778
2260
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
1779
|
-
const
|
|
1780
|
-
const changes = parts.flatMap(({ changes }) => changes);
|
|
1781
|
-
const visibleOver = function* (queue) {
|
|
1782
|
-
return (yield* replicating(replica.confirmed, queue, replica)).visible;
|
|
1783
|
-
};
|
|
1784
|
-
const requeue = await stepped(requeuing(replica.queue, changes, replica.visible, withdrawable(locator), visibleOver));
|
|
2261
|
+
const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(locator), saving.visibleOver));
|
|
1785
2262
|
if (requeue.kind === 'refused')
|
|
1786
2263
|
return { outcome: 'refused', ...requeue.refusal };
|
|
1787
2264
|
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
@@ -1799,22 +2276,19 @@ export function composeRuntime(options) {
|
|
|
1799
2276
|
const withdrawing = requeue.withdrawn.size > 0;
|
|
1800
2277
|
const input = {
|
|
1801
2278
|
local: localSaves,
|
|
1802
|
-
baseRoomVersion: replica.
|
|
1803
|
-
composition: compositionFor(replica.
|
|
2279
|
+
baseRoomVersion: replica.record.roomVersion,
|
|
2280
|
+
composition: compositionFor(replica.record),
|
|
1804
2281
|
operations: room.operations,
|
|
1805
2282
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
1806
2283
|
providerLimits: limits,
|
|
1807
2284
|
handle: room.handle,
|
|
1808
|
-
visible:
|
|
2285
|
+
visible: saving.visible,
|
|
1809
2286
|
changes,
|
|
1810
2287
|
bases: parts.flatMap(({ bases }) => bases),
|
|
1811
2288
|
queued: queuedDocuments(replica.queue),
|
|
1812
2289
|
writtenBy,
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
binaries: replica.confirmed.record.binaries,
|
|
1816
|
-
composition: compositionOf(replica.confirmed.record),
|
|
1817
|
-
},
|
|
2290
|
+
unmodeled: await unmodeledIn(replica.record),
|
|
2291
|
+
validate: validatorOf(replica.record, saving.confirmed),
|
|
1818
2292
|
};
|
|
1819
2293
|
const checked = withdrawing
|
|
1820
2294
|
? await buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
|
|
@@ -1839,8 +2313,7 @@ export function composeRuntime(options) {
|
|
|
1839
2313
|
? null
|
|
1840
2314
|
: await buildBatch({
|
|
1841
2315
|
...input,
|
|
1842
|
-
visible:
|
|
1843
|
-
.visible,
|
|
2316
|
+
visible: await stepped(saving.visibleOver(requeue.queue)),
|
|
1844
2317
|
changes: requeue.changes,
|
|
1845
2318
|
queued: queuedDocuments(requeue.queue),
|
|
1846
2319
|
});
|
|
@@ -1873,30 +2346,34 @@ export function composeRuntime(options) {
|
|
|
1873
2346
|
for (const replaced of withdrawn.values()) {
|
|
1874
2347
|
if (replaced === null || !batches.includes(replaced))
|
|
1875
2348
|
continue;
|
|
1876
|
-
const limited = await stepped(limiting(batches, replaced, limits, visibleOver));
|
|
2349
|
+
const limited = await stepped(limiting(batches, replaced, limits, saving.visibleOver));
|
|
1877
2350
|
if (limited !== null)
|
|
1878
2351
|
return { outcome: 'refused', ...limited };
|
|
1879
2352
|
}
|
|
1880
2353
|
const carried = carriedDrafts(locator, saved, consumed);
|
|
1881
2354
|
const left = [...consumed, ...carried.keys()];
|
|
1882
2355
|
const arrived = [...carried.values()];
|
|
1883
|
-
|
|
1884
|
-
|
|
2356
|
+
const done = await saveCommit(local, files, [
|
|
2357
|
+
{
|
|
1885
2358
|
locator,
|
|
2359
|
+
saving,
|
|
2360
|
+
before: replica,
|
|
1886
2361
|
queue: batches,
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2362
|
+
write: {
|
|
2363
|
+
locator,
|
|
2364
|
+
queue: batches,
|
|
2365
|
+
saved: drafted,
|
|
2366
|
+
consumedRoomDrafts: left,
|
|
2367
|
+
roomDrafts: arrived,
|
|
2368
|
+
},
|
|
2369
|
+
},
|
|
2370
|
+
]);
|
|
2371
|
+
if (done === null)
|
|
1893
2372
|
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2373
|
+
const { answer, touched } = done;
|
|
2374
|
+
if (answer.fenced !== null) {
|
|
2375
|
+
throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
|
|
1894
2376
|
}
|
|
1895
|
-
const next = await stepped(replicating(replica.confirmed, batches, replica));
|
|
1896
|
-
const set = await stepped(changing(locator, replica, next, {
|
|
1897
|
-
queue: true,
|
|
1898
|
-
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
1899
|
-
}));
|
|
1900
2377
|
if (fences.has(locator))
|
|
1901
2378
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
1902
2379
|
for (const origin of withdrawn.keys())
|
|
@@ -1912,8 +2389,12 @@ export function composeRuntime(options) {
|
|
|
1912
2389
|
forgetRoomDraft(locator, path);
|
|
1913
2390
|
for (const record of arrived)
|
|
1914
2391
|
rememberRoomDraft(record);
|
|
1915
|
-
|
|
1916
|
-
publishChangeSet(
|
|
2392
|
+
savedReplica(locator, replica, batches, touched.get(locator), answer);
|
|
2393
|
+
publishChangeSet(changeSet(locator, {
|
|
2394
|
+
documents: answer.changed[locator] ?? [],
|
|
2395
|
+
queue: true,
|
|
2396
|
+
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
2397
|
+
}));
|
|
1917
2398
|
queuedSave(locator);
|
|
1918
2399
|
const identifier = join?.batch.id ??
|
|
1919
2400
|
built?.batch.id ??
|
|
@@ -1933,9 +2414,8 @@ export function composeRuntime(options) {
|
|
|
1933
2414
|
if (fences.has(held))
|
|
1934
2415
|
continue;
|
|
1935
2416
|
pendings.set(pendingKey(held, batch.id), { ...state, submission, lost });
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
publishChangeSet(changeSet(held, replica, replica, { queue: true }));
|
|
2417
|
+
if (replicas.has(held))
|
|
2418
|
+
publishChangeSet(changeSet(held, { queue: true }));
|
|
1939
2419
|
}
|
|
1940
2420
|
moveTo({ ...status, ...attention() });
|
|
1941
2421
|
}
|
|
@@ -1979,7 +2459,7 @@ export function composeRuntime(options) {
|
|
|
1979
2459
|
if (fencedSince(locator, since))
|
|
1980
2460
|
return failure({ kind: 'room-not-readable', locator });
|
|
1981
2461
|
if (result.kind === 'scoped-refusal') {
|
|
1982
|
-
const room = replicas.get(result.locator)?.
|
|
2462
|
+
const room = replicas.get(result.locator)?.record;
|
|
1983
2463
|
if (room !== undefined)
|
|
1984
2464
|
await scoped(result, room, await openLocal());
|
|
1985
2465
|
}
|
|
@@ -2010,8 +2490,9 @@ export function composeRuntime(options) {
|
|
|
2010
2490
|
return UNREADABLE;
|
|
2011
2491
|
if (locked(at.room))
|
|
2012
2492
|
return LOCKED_UNREADABLE;
|
|
2013
|
-
const
|
|
2014
|
-
if (
|
|
2493
|
+
const held = replicas.get(at.room);
|
|
2494
|
+
if (held !== undefined &&
|
|
2495
|
+
(await (await openLocal()).entries(held.record.key, 'visible', [at.path])).length > 0) {
|
|
2015
2496
|
return { kind: 'document', ...at };
|
|
2016
2497
|
}
|
|
2017
2498
|
const history = await readHistory(at.room, at.path);
|
|
@@ -2026,13 +2507,20 @@ export function composeRuntime(options) {
|
|
|
2026
2507
|
? { kind: 'deleted', ...at, revision: last }
|
|
2027
2508
|
: { kind: 'document', ...at };
|
|
2028
2509
|
}
|
|
2029
|
-
function
|
|
2510
|
+
function queuedFacts(locator, path, localId) {
|
|
2030
2511
|
for (const batch of replicas.get(locator)?.queue ?? []) {
|
|
2031
2512
|
const change = batch.changes[queuedFile(batch, path, localId)];
|
|
2032
2513
|
if (change?.kind === 'binary')
|
|
2033
|
-
return change
|
|
2514
|
+
return change;
|
|
2034
2515
|
}
|
|
2035
|
-
|
|
2516
|
+
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2517
|
+
return drafted?.localId === localId ? drafted : null;
|
|
2518
|
+
}
|
|
2519
|
+
function batchFile(batch) {
|
|
2520
|
+
return async (localId) => {
|
|
2521
|
+
const change = batch.changes.find((held) => held.kind === 'binary' && held.localId === localId);
|
|
2522
|
+
return (await openLocal()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
2523
|
+
};
|
|
2036
2524
|
}
|
|
2037
2525
|
function binaryRoom(request) {
|
|
2038
2526
|
if (request.kind === 'path')
|
|
@@ -2062,7 +2550,7 @@ export function composeRuntime(options) {
|
|
|
2062
2550
|
if (locator !== null && fencedSince(locator, since))
|
|
2063
2551
|
return failure({ kind: 'room-not-readable', locator });
|
|
2064
2552
|
if (settled.kind === 'scoped-refusal') {
|
|
2065
|
-
const room = replicas.get(settled.locator)?.
|
|
2553
|
+
const room = replicas.get(settled.locator)?.record;
|
|
2066
2554
|
if (room !== undefined)
|
|
2067
2555
|
await scoped(settled, room, await openLocal());
|
|
2068
2556
|
}
|
|
@@ -2075,68 +2563,96 @@ export function composeRuntime(options) {
|
|
|
2075
2563
|
return UNCERTAIN;
|
|
2076
2564
|
}
|
|
2077
2565
|
}
|
|
2078
|
-
function readBinary(request) {
|
|
2079
|
-
|
|
2566
|
+
async function readBinary(request, name) {
|
|
2567
|
+
if (closed)
|
|
2568
|
+
return UNCERTAIN;
|
|
2569
|
+
const held = await keptFiles.find(name);
|
|
2570
|
+
if (held !== null)
|
|
2571
|
+
return held;
|
|
2572
|
+
return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
|
|
2080
2573
|
}
|
|
2081
|
-
function binaryPath(locator, path, revision) {
|
|
2574
|
+
async function binaryPath(locator, path, revision) {
|
|
2082
2575
|
if (revision !== undefined)
|
|
2083
2576
|
return { kind: 'path', locator, path, revision };
|
|
2084
|
-
const
|
|
2085
|
-
|
|
2086
|
-
if (visible === undefined || held === undefined)
|
|
2577
|
+
const replica = replicas.get(locator);
|
|
2578
|
+
if (replica === undefined)
|
|
2087
2579
|
return null;
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2580
|
+
const local = await openLocal();
|
|
2581
|
+
const [held] = await local.entries(replica.record.key, 'visible', [path]);
|
|
2582
|
+
if (held?.binary === undefined || held.binary === null)
|
|
2583
|
+
return null;
|
|
2584
|
+
if (held.binary.state === 'pending')
|
|
2585
|
+
return { kind: 'pending', localId: held.binary.localId };
|
|
2586
|
+
const record = binaryRecord(path, held.binary, held.revision ?? undefined);
|
|
2091
2587
|
if (record.state !== 'accepted')
|
|
2092
2588
|
return null;
|
|
2093
|
-
const
|
|
2094
|
-
const
|
|
2589
|
+
const queued = replica.queue.flatMap((batch) => batch.changes);
|
|
2590
|
+
const sources = queued.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
|
|
2591
|
+
const confirmedLayer = await stepped(slicing(await local.entries(replica.record.key, 'confirmed', [path, ...sources]), [
|
|
2592
|
+
path,
|
|
2593
|
+
...sources,
|
|
2594
|
+
]));
|
|
2095
2595
|
const holds = (at) => {
|
|
2096
|
-
const kept =
|
|
2596
|
+
const kept = confirmedLayer.binaries[at];
|
|
2097
2597
|
return (kept?.state === 'accepted' &&
|
|
2098
2598
|
kept.fileRef === record.fileRef &&
|
|
2099
|
-
|
|
2599
|
+
confirmedLayer.revisions[at] === record.revision);
|
|
2100
2600
|
};
|
|
2101
|
-
let
|
|
2102
|
-
for (let index = queued.length - 1; index >= 0 && !holds(
|
|
2601
|
+
let confirmedAt = path;
|
|
2602
|
+
for (let index = queued.length - 1; index >= 0 && !holds(confirmedAt); index -= 1) {
|
|
2103
2603
|
const change = queued[index];
|
|
2104
|
-
if (change.kind === 'move' && change.to ===
|
|
2105
|
-
|
|
2604
|
+
if (change.kind === 'move' && change.to === confirmedAt)
|
|
2605
|
+
confirmedAt = change.path;
|
|
2106
2606
|
}
|
|
2107
2607
|
return {
|
|
2108
2608
|
kind: 'path',
|
|
2109
2609
|
locator,
|
|
2110
|
-
path:
|
|
2610
|
+
path: confirmedAt,
|
|
2111
2611
|
revision: record.revision,
|
|
2112
2612
|
fileRef: record.fileRef,
|
|
2113
2613
|
};
|
|
2114
2614
|
}
|
|
2115
2615
|
async function readBinaryPath(locator, path, revision) {
|
|
2116
2616
|
await ready;
|
|
2117
|
-
const
|
|
2617
|
+
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2618
|
+
const asked = drafted !== undefined && drafted.localId === revision
|
|
2619
|
+
? { kind: 'pending', localId: revision }
|
|
2620
|
+
: await binaryPath(locator, path, revision);
|
|
2118
2621
|
if (asked === null)
|
|
2119
2622
|
return absentBinary(path);
|
|
2120
2623
|
if (asked.kind === 'pending') {
|
|
2121
|
-
const
|
|
2122
|
-
return
|
|
2624
|
+
const facts = queuedFacts(locator, path, asked.localId);
|
|
2625
|
+
return facts === null
|
|
2626
|
+
? absentBinary(path)
|
|
2627
|
+
: {
|
|
2628
|
+
kind: 'binary',
|
|
2629
|
+
file: asked.localId,
|
|
2630
|
+
byteLength: facts.byteLength,
|
|
2631
|
+
mediaType: facts.mediaType,
|
|
2632
|
+
};
|
|
2123
2633
|
}
|
|
2124
|
-
return readBinary(asked);
|
|
2634
|
+
return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
|
|
2125
2635
|
}
|
|
2126
2636
|
async function linkBinaryPath(locator, path, revision) {
|
|
2127
2637
|
await ready;
|
|
2128
|
-
const asked = binaryPath(locator, path, revision);
|
|
2638
|
+
const asked = await binaryPath(locator, path, revision);
|
|
2129
2639
|
if (asked === null || asked.kind === 'pending')
|
|
2130
2640
|
return absentBinary(path);
|
|
2131
2641
|
return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
|
|
2132
2642
|
}
|
|
2133
2643
|
async function readBinaryReference(reference) {
|
|
2134
2644
|
await ready;
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2645
|
+
const scope = options.adapter.referrerScope(reference.referrer);
|
|
2646
|
+
if (scope === null)
|
|
2647
|
+
return absentReference(reference.fileRef);
|
|
2648
|
+
return readBinary({ kind: 'reference', fileRef: reference.fileRef, referrer: reference.referrer }, { locator: scope.locator, path: scope.path ?? '', revision: reference.fileRef });
|
|
2649
|
+
}
|
|
2650
|
+
function readPart(file, part) {
|
|
2651
|
+
return options.database.ask({ kind: 'file.part', key: file, part });
|
|
2652
|
+
}
|
|
2653
|
+
async function cleanUp() {
|
|
2654
|
+
const freed = await options.database.ask({ kind: 'opened.clean' });
|
|
2655
|
+
return freed + ((await options.shell.cleanUp?.()) ?? 0);
|
|
2140
2656
|
}
|
|
2141
2657
|
function declaredAgents() {
|
|
2142
2658
|
for (const capability of capabilities) {
|
|
@@ -2180,7 +2696,7 @@ export function composeRuntime(options) {
|
|
|
2180
2696
|
try {
|
|
2181
2697
|
const result = await options.adapter.action(authorization, { name, input });
|
|
2182
2698
|
if (result.kind === 'scoped-refusal') {
|
|
2183
|
-
const room = replicas.get(result.locator)?.
|
|
2699
|
+
const room = replicas.get(result.locator)?.record;
|
|
2184
2700
|
if (room !== undefined)
|
|
2185
2701
|
await scoped(result, room, await openLocal());
|
|
2186
2702
|
}
|
|
@@ -2255,85 +2771,277 @@ export function composeRuntime(options) {
|
|
|
2255
2771
|
}
|
|
2256
2772
|
function reachedBy(locator, roomVersion) {
|
|
2257
2773
|
const held = replicas.get(locator);
|
|
2258
|
-
return (held !== undefined &&
|
|
2259
|
-
options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion));
|
|
2774
|
+
return (held !== undefined && options.adapter.reached(locator, held.record.roomVersion, roomVersion));
|
|
2260
2775
|
}
|
|
2261
|
-
function writtenAfter(locator, roomVersion, path) {
|
|
2262
|
-
const
|
|
2776
|
+
async function writtenAfter(locator, roomVersion, path) {
|
|
2777
|
+
const held = replicas.get(locator);
|
|
2778
|
+
if (held === undefined)
|
|
2779
|
+
return false;
|
|
2780
|
+
const [entry] = await (await openLocal()).entries(held.record.key, 'confirmed', [path]);
|
|
2781
|
+
const written = entry?.written ?? undefined;
|
|
2263
2782
|
return (written !== undefined &&
|
|
2264
2783
|
options.adapter.reached(locator, written, roomVersion) &&
|
|
2265
2784
|
!options.adapter.reached(locator, roomVersion, written));
|
|
2266
2785
|
}
|
|
2267
|
-
|
|
2268
|
-
|
|
2786
|
+
function overriddenTexts(texts, overrides) {
|
|
2787
|
+
return async (paths) => {
|
|
2788
|
+
const known = new Map(await texts(paths.filter((path) => !overrides.has(path))));
|
|
2789
|
+
for (const path of paths) {
|
|
2790
|
+
const entry = overrides.get(path);
|
|
2791
|
+
if (entry === undefined)
|
|
2792
|
+
continue;
|
|
2793
|
+
if (entry === null)
|
|
2794
|
+
known.delete(path);
|
|
2795
|
+
else
|
|
2796
|
+
known.set(path, entry.binary !== null ? null : entry.content);
|
|
2797
|
+
}
|
|
2798
|
+
return known;
|
|
2799
|
+
};
|
|
2800
|
+
}
|
|
2801
|
+
async function unreadableRoom(texts, walk, paths) {
|
|
2802
|
+
if (paths !== null) {
|
|
2803
|
+
const text = {};
|
|
2804
|
+
for (const [path, content] of await texts(paths))
|
|
2805
|
+
if (content !== null)
|
|
2806
|
+
text[path] = content;
|
|
2807
|
+
return stepped(unreadableIn(text, Object.keys(text)));
|
|
2808
|
+
}
|
|
2809
|
+
for (let after = null;;) {
|
|
2810
|
+
const page = await walk(after);
|
|
2811
|
+
const text = {};
|
|
2812
|
+
for (const { path, content } of page.entries)
|
|
2813
|
+
if (content !== null)
|
|
2814
|
+
text[path] = content;
|
|
2815
|
+
const refused = await stepped(unreadableIn(text, Object.keys(text)));
|
|
2816
|
+
if (refused !== null)
|
|
2817
|
+
return refused;
|
|
2818
|
+
if (page.next === null)
|
|
2819
|
+
return null;
|
|
2820
|
+
after = page.next;
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
async function validatedRoom(local, record, view, previous, overrides = null) {
|
|
2824
|
+
const composition = compositionOf(record);
|
|
2825
|
+
const modeled = record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable';
|
|
2826
|
+
const lookup = overrides === null
|
|
2827
|
+
? lookupOf(local, record.key, view)
|
|
2828
|
+
: overriddenLookup(lookupOf(local, record.key, view), overrides);
|
|
2829
|
+
const texts = overrides === null
|
|
2830
|
+
? textsOf(local, record.key, view)
|
|
2831
|
+
: overriddenTexts(textsOf(local, record.key, view), overrides);
|
|
2832
|
+
const walk = overrides === null
|
|
2833
|
+
? walkOf(local, record.key, view)
|
|
2834
|
+
: overriddenWalk(walkOf(local, record.key, view), overrides);
|
|
2835
|
+
let changes = null;
|
|
2836
|
+
let reshaped = false;
|
|
2837
|
+
if (previous !== null && previous.changed.length <= CLEAN_CHANGES) {
|
|
2838
|
+
const [was, now] = await Promise.all([
|
|
2839
|
+
local.entries(record.key, 'confirmed', previous.changed),
|
|
2840
|
+
overrides === null
|
|
2841
|
+
? local.entries(record.key, view, previous.changed)
|
|
2842
|
+
: Promise.resolve([]),
|
|
2843
|
+
]);
|
|
2844
|
+
const before = new Map(was.map((entry) => [entry.path, entry]));
|
|
2845
|
+
const after = overrides === null
|
|
2846
|
+
? new Map(now.map((entry) => [entry.path, entry]))
|
|
2847
|
+
: overrides;
|
|
2848
|
+
changes = previous.changed.flatMap((path) => {
|
|
2849
|
+
const one = before.get(path) ?? null;
|
|
2850
|
+
const other = after.get(path) ?? null;
|
|
2851
|
+
reshaped ||= textDiffers(one, other);
|
|
2852
|
+
const held = textOf(one);
|
|
2853
|
+
const next = textOf(other);
|
|
2854
|
+
return held === next ? [] : [{ path, was: held, now: next }];
|
|
2855
|
+
});
|
|
2856
|
+
}
|
|
2857
|
+
const named = changes === null ? null : changes.map(({ path }) => path);
|
|
2858
|
+
const textChanged = changes === null || changes.length > 0 || reshaped;
|
|
2859
|
+
const modelsChanged = previous === null || !sameComposition(compositionOf(previous.record), composition);
|
|
2860
|
+
if (modeled && (textChanged || modelsChanged)) {
|
|
2861
|
+
const clean = previous !== null &&
|
|
2862
|
+
changes !== null &&
|
|
2863
|
+
(await changedCleanly(changes, compositionOf(previous.record), composition, {
|
|
2864
|
+
lookup,
|
|
2865
|
+
texts,
|
|
2866
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2867
|
+
}));
|
|
2868
|
+
if (clean)
|
|
2869
|
+
return null;
|
|
2870
|
+
const verdict = await validatedCollection(walk, lookup, composition);
|
|
2871
|
+
if (verdict.verdict === 'accept')
|
|
2872
|
+
return null;
|
|
2873
|
+
if (verdict.rule !== 'model-unavailable')
|
|
2874
|
+
return { kind: 'refused', refusal: verdict };
|
|
2875
|
+
return unreadableRoom(texts, walk, named);
|
|
2876
|
+
}
|
|
2877
|
+
if (textChanged)
|
|
2878
|
+
return unreadableRoom(texts, walk, named);
|
|
2879
|
+
return null;
|
|
2880
|
+
}
|
|
2881
|
+
function refreshingOf(local, key, liveness) {
|
|
2882
|
+
return {
|
|
2883
|
+
prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
|
|
2884
|
+
async stage(page) {
|
|
2885
|
+
const batches = batched(page.entries);
|
|
2886
|
+
for (const [index, entries] of batches.entries()) {
|
|
2887
|
+
const last = index === batches.length - 1;
|
|
2888
|
+
const staged = await exclusive(async () => {
|
|
2889
|
+
if (!liveness())
|
|
2890
|
+
return false;
|
|
2891
|
+
await local.refresh(key, last
|
|
2892
|
+
? { ...page, entries }
|
|
2893
|
+
: { entries, removals: [], folders: [], folderRemovals: [] });
|
|
2894
|
+
return true;
|
|
2895
|
+
});
|
|
2896
|
+
if (!staged)
|
|
2897
|
+
return false;
|
|
2898
|
+
}
|
|
2899
|
+
return true;
|
|
2900
|
+
},
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
function batched(entries) {
|
|
2904
|
+
const batches = [[]];
|
|
2905
|
+
let spent = 0;
|
|
2906
|
+
for (const entry of entries) {
|
|
2907
|
+
const size = (entry.content?.length ?? 0) + ROW_WEIGHT;
|
|
2908
|
+
if (spent + size > STAGE_BYTES && batches.at(-1).length > 0) {
|
|
2909
|
+
batches.push([]);
|
|
2910
|
+
spent = 0;
|
|
2911
|
+
}
|
|
2912
|
+
batches.at(-1).push(entry);
|
|
2913
|
+
spent += size;
|
|
2914
|
+
}
|
|
2915
|
+
return batches;
|
|
2916
|
+
}
|
|
2917
|
+
async function pullRefresh(authorized, held, liveness) {
|
|
2918
|
+
if (!liveness())
|
|
2269
2919
|
return unconfirmed;
|
|
2270
2920
|
const turn = generation;
|
|
2921
|
+
const { record } = held;
|
|
2922
|
+
const base = held.confirmed;
|
|
2923
|
+
const local = await openLocal();
|
|
2271
2924
|
try {
|
|
2272
|
-
|
|
2273
|
-
const
|
|
2274
|
-
|
|
2275
|
-
|
|
2925
|
+
await exclusive(() => local.unrefresh(record.key));
|
|
2926
|
+
const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, liveness));
|
|
2927
|
+
if (result.kind !== 'advanced') {
|
|
2928
|
+
journal.record(`transfer refresh ${record.locator} ${result.kind}`);
|
|
2929
|
+
return result;
|
|
2930
|
+
}
|
|
2931
|
+
const refused = await validatedRoom(local, result.record, 'refreshed', {
|
|
2932
|
+
record,
|
|
2933
|
+
changed: [...result.changed],
|
|
2934
|
+
});
|
|
2935
|
+
if (refused !== null) {
|
|
2936
|
+
journal.record(`transfer refresh ${record.locator} refused`);
|
|
2937
|
+
return refused;
|
|
2938
|
+
}
|
|
2939
|
+
journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
|
|
2940
|
+
return { kind: 'advanced', record: result.record, base };
|
|
2276
2941
|
}
|
|
2277
2942
|
catch (error) {
|
|
2278
|
-
journal.record(`transfer refresh ${
|
|
2943
|
+
journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
|
|
2279
2944
|
throw error;
|
|
2280
2945
|
}
|
|
2281
2946
|
finally {
|
|
2282
|
-
reads.finished(
|
|
2283
|
-
}
|
|
2947
|
+
reads.finished(record.locator, turn);
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
const heldContents = new Map();
|
|
2951
|
+
function heldContentOf(local, key) {
|
|
2952
|
+
const known = heldContents.get(key);
|
|
2953
|
+
if (known !== undefined)
|
|
2954
|
+
return known;
|
|
2955
|
+
if (heldContents.size > ROOM_READS)
|
|
2956
|
+
heldContents.clear();
|
|
2957
|
+
const made = {
|
|
2958
|
+
files: (after, limit) => local.files(key, 'confirmed', after, limit),
|
|
2959
|
+
async contents(paths) {
|
|
2960
|
+
return new Map((await local.entries(key, 'confirmed', paths)).flatMap(({ path, content, revision }) => content === null || revision === null ? [] : [[path, { content, revision }]]));
|
|
2961
|
+
},
|
|
2962
|
+
};
|
|
2963
|
+
heldContents.set(key, made);
|
|
2964
|
+
return made;
|
|
2284
2965
|
}
|
|
2285
|
-
function staging(local,
|
|
2966
|
+
function staging(local, liveness) {
|
|
2286
2967
|
return {
|
|
2287
2968
|
held: (locator) => transfers.get(locator) ?? null,
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
const
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2969
|
+
async stage(record, page, composition) {
|
|
2970
|
+
const bytes = pageBytes(page);
|
|
2971
|
+
const batches = batched(page.entries);
|
|
2972
|
+
let key = record.key;
|
|
2973
|
+
for (const [index, entries] of batches.entries()) {
|
|
2974
|
+
const last = index === batches.length - 1;
|
|
2975
|
+
const staged = await exclusive(async () => {
|
|
2976
|
+
if (!liveness())
|
|
2977
|
+
return null;
|
|
2978
|
+
if (index === 0 && !(await admitted(local, record.locator, bytes)))
|
|
2979
|
+
return null;
|
|
2980
|
+
try {
|
|
2981
|
+
return await local.stage(record, composition, { entries, folders: last ? page.folders : [] }, key, last);
|
|
2982
|
+
}
|
|
2983
|
+
catch (error) {
|
|
2984
|
+
if (!exceeded(error))
|
|
2985
|
+
throw error;
|
|
2986
|
+
await exceededOn(record.locator, bytes);
|
|
2987
|
+
return null;
|
|
2988
|
+
}
|
|
2989
|
+
});
|
|
2990
|
+
if (staged === null)
|
|
2991
|
+
return null;
|
|
2992
|
+
key = staged;
|
|
2303
2993
|
}
|
|
2304
|
-
transfers.set(record.locator, record);
|
|
2305
|
-
|
|
2306
|
-
return
|
|
2307
|
-
}
|
|
2994
|
+
transfers.set(record.locator, { ...record, key });
|
|
2995
|
+
liveQueries.recover();
|
|
2996
|
+
return key;
|
|
2997
|
+
},
|
|
2998
|
+
async refreshing(key) {
|
|
2999
|
+
await exclusive(() => local.unrefresh(key));
|
|
3000
|
+
return refreshingOf(local, key, liveness);
|
|
3001
|
+
},
|
|
2308
3002
|
abandon: (locator) => exclusive(async () => {
|
|
2309
|
-
if (!
|
|
3003
|
+
if (!liveness())
|
|
2310
3004
|
return;
|
|
2311
3005
|
await local.abandon(locator);
|
|
2312
3006
|
transfers.delete(locator);
|
|
2313
|
-
|
|
3007
|
+
liveQueries.recover();
|
|
3008
|
+
sweep();
|
|
2314
3009
|
}),
|
|
3010
|
+
setAside: (locator) => exclusive(async () => {
|
|
3011
|
+
if (!liveness())
|
|
3012
|
+
return;
|
|
3013
|
+
await local.abandon(locator, true);
|
|
3014
|
+
transfers.delete(locator);
|
|
3015
|
+
liveQueries.recover();
|
|
3016
|
+
}),
|
|
3017
|
+
release: (key) => exclusive(async () => {
|
|
3018
|
+
await local.release(key);
|
|
3019
|
+
sweep();
|
|
3020
|
+
}),
|
|
3021
|
+
content: (key) => heldContentOf(local, key),
|
|
3022
|
+
validate: (record, view) => validatedRoom(local, record, view, null),
|
|
2315
3023
|
};
|
|
2316
3024
|
}
|
|
2317
|
-
function reusable(room) {
|
|
2318
|
-
const record = replicas.get(room.locator)?.
|
|
3025
|
+
function reusable(room, local) {
|
|
3026
|
+
const record = replicas.get(room.locator)?.record;
|
|
2319
3027
|
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
2320
3028
|
return null;
|
|
2321
|
-
return
|
|
3029
|
+
return heldContentOf(local, record.key);
|
|
2322
3030
|
}
|
|
2323
|
-
async function pullBootstrap(authorized, room, local,
|
|
2324
|
-
if (!
|
|
3031
|
+
async function pullBootstrap(authorized, room, local, liveness) {
|
|
3032
|
+
if (!liveness())
|
|
2325
3033
|
return unconfirmed;
|
|
2326
3034
|
const turn = generation;
|
|
2327
3035
|
if (replicas.has(room.locator)) {
|
|
2328
3036
|
catching.add(room.locator);
|
|
2329
|
-
|
|
3037
|
+
liveQueries.recover();
|
|
2330
3038
|
}
|
|
2331
3039
|
try {
|
|
2332
3040
|
const result = await bootstrapRoom(reads, authorized, room, {
|
|
2333
3041
|
collection: modelOf(room.locator),
|
|
2334
3042
|
selection: modelOf(room.locator, 'typed-selection'),
|
|
2335
|
-
}, (models, held) => (
|
|
2336
|
-
const detail = result.kind === 'staged' ? result.
|
|
3043
|
+
}, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
|
|
3044
|
+
const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
|
|
2337
3045
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
2338
3046
|
if (result.kind !== 'staged')
|
|
2339
3047
|
caughtUpTo(room.locator);
|
|
@@ -2350,10 +3058,10 @@ export function composeRuntime(options) {
|
|
|
2350
3058
|
}
|
|
2351
3059
|
function caughtUpTo(locator) {
|
|
2352
3060
|
if (catching.delete(locator))
|
|
2353
|
-
|
|
3061
|
+
liveQueries.recover();
|
|
2354
3062
|
}
|
|
2355
3063
|
function behind(room) {
|
|
2356
|
-
const held = replicas.get(room.locator)?.
|
|
3064
|
+
const held = replicas.get(room.locator)?.record.roomVersion;
|
|
2357
3065
|
return (held !== undefined &&
|
|
2358
3066
|
[announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
|
|
2359
3067
|
}
|
|
@@ -2366,12 +3074,11 @@ export function composeRuntime(options) {
|
|
|
2366
3074
|
return advanceOnce(room, local, authorized, live);
|
|
2367
3075
|
}
|
|
2368
3076
|
async function advanceOnce(room, local, authorized, live) {
|
|
2369
|
-
await referenced(room.locator);
|
|
2370
3077
|
if (!live() || paused.has(room.locator))
|
|
2371
3078
|
return unconfirmed;
|
|
2372
3079
|
const held = replicas.get(room.locator);
|
|
2373
3080
|
if (held !== undefined) {
|
|
2374
|
-
const refreshed = await pullRefresh(authorized, held
|
|
3081
|
+
const refreshed = await pullRefresh(authorized, held, live);
|
|
2375
3082
|
if (!live())
|
|
2376
3083
|
return unconfirmed;
|
|
2377
3084
|
if (refreshed.kind === 'still-current') {
|
|
@@ -2386,10 +3093,11 @@ export function composeRuntime(options) {
|
|
|
2386
3093
|
return null;
|
|
2387
3094
|
}
|
|
2388
3095
|
if (refreshed.kind === 'advanced') {
|
|
2389
|
-
|
|
2390
|
-
|
|
3096
|
+
const landing = { kind: 'refreshed', base: refreshed.base };
|
|
3097
|
+
if (!(await publish(local, room, refreshed.record, landing, live))) {
|
|
3098
|
+
const now = replicas.get(room.locator)?.record.roomVersion;
|
|
2391
3099
|
const reached = now !== undefined &&
|
|
2392
|
-
options.adapter.reached(room.locator, now, refreshed.
|
|
3100
|
+
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
2393
3101
|
return reached ? null : advance(room, local, authorized, live);
|
|
2394
3102
|
}
|
|
2395
3103
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
@@ -2399,7 +3107,7 @@ export function composeRuntime(options) {
|
|
|
2399
3107
|
return refreshed;
|
|
2400
3108
|
if (opened !== null) {
|
|
2401
3109
|
owed.add(room.locator);
|
|
2402
|
-
|
|
3110
|
+
liveQueries.recover();
|
|
2403
3111
|
return unconfirmed;
|
|
2404
3112
|
}
|
|
2405
3113
|
if (reloadsDue.get(room.locator) === generation)
|
|
@@ -2410,7 +3118,7 @@ export function composeRuntime(options) {
|
|
|
2410
3118
|
return unconfirmed;
|
|
2411
3119
|
if (bootstrapped.kind !== 'staged')
|
|
2412
3120
|
return bootstrapped;
|
|
2413
|
-
await publish(local, room, bootstrapped.
|
|
3121
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
2414
3122
|
return null;
|
|
2415
3123
|
}
|
|
2416
3124
|
async function withhold(withheld, room, local, progress) {
|
|
@@ -2483,46 +3191,90 @@ export function composeRuntime(options) {
|
|
|
2483
3191
|
(accepted.author === undefined ? '' : ` author ${accepted.author}`) +
|
|
2484
3192
|
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
2485
3193
|
}
|
|
2486
|
-
|
|
2487
|
-
|
|
3194
|
+
function settling(acceptance) {
|
|
3195
|
+
return acceptance !== null && 'apply' in acceptance;
|
|
3196
|
+
}
|
|
3197
|
+
function refusalOf(acceptance) {
|
|
3198
|
+
return settling(acceptance) ? (acceptance.refused ?? null) : acceptance;
|
|
3199
|
+
}
|
|
3200
|
+
async function acceptance(room, local, submitted, accepted) {
|
|
2488
3201
|
if (!continuing())
|
|
2489
3202
|
return unconfirmed;
|
|
2490
3203
|
const current = replicas.get(room.locator);
|
|
2491
3204
|
if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
|
|
2492
3205
|
return null;
|
|
2493
3206
|
}
|
|
2494
|
-
const held = current.
|
|
3207
|
+
const held = current.record.roomVersion;
|
|
2495
3208
|
logAccepted(room.locator, submitted, accepted);
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
3209
|
+
let record = current.record;
|
|
3210
|
+
let changes;
|
|
3211
|
+
if (!options.adapter.reached(room.locator, held, accepted.roomVersion)) {
|
|
3212
|
+
const paths = [...returnedPaths(submitted, accepted)];
|
|
3213
|
+
const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
|
|
3214
|
+
const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion));
|
|
3215
|
+
record = {
|
|
3216
|
+
...record,
|
|
3217
|
+
roomVersion: options.adapter.follows(room.locator, held, accepted.roomVersion)
|
|
3218
|
+
? accepted.roomVersion
|
|
3219
|
+
: held,
|
|
3220
|
+
};
|
|
3221
|
+
const overrides = new Map([
|
|
3222
|
+
...back.set.map((entry) => [entry.path, entry]),
|
|
3223
|
+
...back.removed.map((path) => [path, null]),
|
|
3224
|
+
]);
|
|
3225
|
+
const refused = await validatedRoom(local, record, 'confirmed', { record: current.record, changed: [...overrides.keys()] }, overrides);
|
|
3226
|
+
if (refused !== null) {
|
|
3227
|
+
const key = pendingKey(room.locator, submitted.id);
|
|
3228
|
+
return {
|
|
3229
|
+
locator: room.locator,
|
|
3230
|
+
left: [],
|
|
3231
|
+
commit: {
|
|
3232
|
+
rooms: [],
|
|
3233
|
+
writes: [
|
|
3234
|
+
{
|
|
3235
|
+
locator: room.locator,
|
|
3236
|
+
accepted: [{ id: submitted.id, roomVersion: accepted.roomVersion }],
|
|
3237
|
+
},
|
|
3238
|
+
],
|
|
3239
|
+
},
|
|
3240
|
+
refused,
|
|
3241
|
+
apply() {
|
|
3242
|
+
const state = pendings.get(key);
|
|
3243
|
+
if (state !== undefined && !fences.has(room.locator))
|
|
3244
|
+
pendings.set(key, { ...state, accepted: accepted.roomVersion });
|
|
3245
|
+
return changeSet(room.locator, { queue: true });
|
|
3246
|
+
},
|
|
3247
|
+
};
|
|
3248
|
+
}
|
|
3249
|
+
changes = back;
|
|
3250
|
+
}
|
|
3251
|
+
const settled = await settlement(local, room.locator, current, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
|
|
3252
|
+
return {
|
|
3253
|
+
...settled,
|
|
3254
|
+
apply(answer) {
|
|
3255
|
+
const set = settled.apply(answer);
|
|
3256
|
+
pendings.delete(pendingKey(room.locator, submitted.id));
|
|
3257
|
+
unreadable.delete(room.locator);
|
|
3258
|
+
sourceRefusals.delete(room.locator);
|
|
3259
|
+
declarationRefusals.delete(room.locator);
|
|
3260
|
+
list(room, true);
|
|
3261
|
+
return set;
|
|
3262
|
+
},
|
|
3263
|
+
};
|
|
3264
|
+
}
|
|
3265
|
+
async function adoptedTogether(local, acceptances) {
|
|
3266
|
+
const settlements = acceptances.filter(settling);
|
|
3267
|
+
if (settlements.length > 0) {
|
|
3268
|
+
publishChangeSet(await together(local, settlements));
|
|
3269
|
+
moveTo({ ...status, ...attention() });
|
|
3270
|
+
}
|
|
3271
|
+
await unpair(local);
|
|
2520
3272
|
}
|
|
2521
3273
|
async function adopted(room, local, submitted, accepted) {
|
|
2522
3274
|
const failed = await exclusive(async () => {
|
|
2523
|
-
const adopting = await
|
|
2524
|
-
await
|
|
2525
|
-
return adopting;
|
|
3275
|
+
const adopting = await acceptance(room, local, submitted, accepted);
|
|
3276
|
+
await adoptedTogether(local, [adopting]);
|
|
3277
|
+
return refusalOf(adopting);
|
|
2526
3278
|
});
|
|
2527
3279
|
if (failed === null)
|
|
2528
3280
|
return true;
|
|
@@ -2533,19 +3285,20 @@ export function composeRuntime(options) {
|
|
|
2533
3285
|
async function adoptedMove(room, partner, local, submitted, accepted) {
|
|
2534
3286
|
const other = discovered.get(partner);
|
|
2535
3287
|
const failed = await exclusive(async () => {
|
|
2536
|
-
const left = await
|
|
3288
|
+
const left = await acceptance(room, local, submitted, accepted);
|
|
2537
3289
|
const arriving = replicas.get(partner)?.queue.find((batch) => batch.id === submitted.id);
|
|
2538
3290
|
const arrived = other === undefined || arriving === undefined || accepted.destination === undefined
|
|
2539
3291
|
? null
|
|
2540
|
-
: await
|
|
3292
|
+
: await acceptance(other, local, arriving, {
|
|
2541
3293
|
kind: 'accepted',
|
|
2542
3294
|
...accepted.destination,
|
|
2543
3295
|
});
|
|
2544
|
-
|
|
2545
|
-
|
|
3296
|
+
await adoptedTogether(local, [left, arrived]);
|
|
3297
|
+
const refusedArrival = refusalOf(arrived);
|
|
3298
|
+
if (other !== undefined && refusedArrival?.kind === 'refused' && continuing()) {
|
|
3299
|
+
refusedSource(other, refusedArrival);
|
|
2546
3300
|
}
|
|
2547
|
-
|
|
2548
|
-
return left;
|
|
3301
|
+
return refusalOf(left);
|
|
2549
3302
|
});
|
|
2550
3303
|
submitLater(partner);
|
|
2551
3304
|
if (failed === null)
|
|
@@ -2571,79 +3324,102 @@ export function composeRuntime(options) {
|
|
|
2571
3324
|
continue;
|
|
2572
3325
|
if (queueIn(partner).some((held) => held.id === batch.id))
|
|
2573
3326
|
continue;
|
|
2574
|
-
if (
|
|
2575
|
-
|
|
2576
|
-
if (leavingIn(batch, locator) && !withdrawable(locator)(batch))
|
|
2577
|
-
continue;
|
|
2578
|
-
return { locator, batch };
|
|
3327
|
+
if (unpairedIn(locator, batch))
|
|
3328
|
+
return { locator, batch };
|
|
2579
3329
|
}
|
|
2580
3330
|
}
|
|
2581
3331
|
return null;
|
|
2582
3332
|
}
|
|
2583
|
-
async function
|
|
2584
|
-
const
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
3333
|
+
async function unpairing(local, locator, batches) {
|
|
3334
|
+
const ids = new Set(batches.map(({ id }) => id));
|
|
3335
|
+
const leaving = batches.map((batch) => ({
|
|
3336
|
+
batch,
|
|
3337
|
+
returned: !localSaves && leavingIn(batch, locator) ? returnedBy('withdrawn') : null,
|
|
3338
|
+
}));
|
|
3339
|
+
const outcome = localSaves ? 'dropped' : 'unpaired';
|
|
3340
|
+
const current = replicas.get(locator);
|
|
3341
|
+
if (current !== undefined) {
|
|
3342
|
+
const settled = await settlement(local, locator, current, current.record, current.queue.filter((one) => !ids.has(one.id)), { leaving });
|
|
3343
|
+
return {
|
|
3344
|
+
...settled,
|
|
3345
|
+
apply(answer) {
|
|
3346
|
+
for (const batch of batches)
|
|
3347
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
3348
|
+
const set = settled.apply(answer);
|
|
3349
|
+
submitLater(locator);
|
|
3350
|
+
return set;
|
|
3351
|
+
},
|
|
2592
3352
|
};
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
3353
|
+
}
|
|
3354
|
+
const queue = queueIn(locator).filter((one) => !ids.has(one.id));
|
|
3355
|
+
const written = new Map();
|
|
3356
|
+
for (const { batch, returned } of leaving) {
|
|
3357
|
+
if (returned === null)
|
|
2597
3358
|
continue;
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
const written = new Map((leaving.returned === null
|
|
2601
|
-
? []
|
|
2602
|
-
: (pendings.get(pendingKey(locator, batch.id))?.drafts ?? [])).map((saved) => [
|
|
2603
|
-
saved.path,
|
|
2604
|
-
{
|
|
3359
|
+
for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
|
|
3360
|
+
written.set(saved.path, {
|
|
2605
3361
|
locator,
|
|
2606
3362
|
path: saved.path,
|
|
2607
3363
|
revision: roomDraftRevision(),
|
|
2608
|
-
parts: returnedInto(roomDrafts.get(locator)?.get(saved.path)?.parts, saved.parts),
|
|
2609
|
-
returned
|
|
2610
|
-
}
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
3364
|
+
parts: returnedInto((written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path))?.parts, saved.parts),
|
|
3365
|
+
returned,
|
|
3366
|
+
});
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
const carried = carriedBack(locator, leaving.flatMap(({ batch, returned }) => (returned === null ? [] : [batch])), written);
|
|
3370
|
+
return {
|
|
3371
|
+
locator,
|
|
3372
|
+
left: batches,
|
|
3373
|
+
commit: {
|
|
3374
|
+
rooms: [],
|
|
3375
|
+
writes: [
|
|
3376
|
+
{
|
|
3377
|
+
locator,
|
|
3378
|
+
queue,
|
|
3379
|
+
roomDrafts: [...written.values()],
|
|
3380
|
+
consumedRoomDrafts: carried.taken,
|
|
3381
|
+
},
|
|
3382
|
+
...carried.writes,
|
|
3383
|
+
],
|
|
3384
|
+
},
|
|
3385
|
+
apply() {
|
|
3386
|
+
for (const batch of batches) {
|
|
3387
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
3388
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
3389
|
+
}
|
|
3390
|
+
if (queue.length > 0)
|
|
3391
|
+
queuesWithoutReplica.set(locator, queue);
|
|
3392
|
+
else
|
|
3393
|
+
queuesWithoutReplica.delete(locator);
|
|
3394
|
+
for (const record of written.values())
|
|
3395
|
+
rememberRoomDraft(record);
|
|
3396
|
+
carried.remember();
|
|
3397
|
+
submitLater(locator);
|
|
3398
|
+
return mergeChangeSets([
|
|
3399
|
+
changeSet(locator, {
|
|
3400
|
+
queue: true,
|
|
3401
|
+
drafts: [...written.keys(), ...carried.taken],
|
|
3402
|
+
}),
|
|
3403
|
+
...carried.changes,
|
|
3404
|
+
]);
|
|
3405
|
+
},
|
|
3406
|
+
};
|
|
3407
|
+
}
|
|
3408
|
+
async function unpair(local) {
|
|
3409
|
+
const handled = new Set();
|
|
3410
|
+
for (let held = unpaired(handled); held !== null && continuing(); held = unpaired(handled)) {
|
|
3411
|
+
const { locator, batch } = held;
|
|
3412
|
+
handled.add(pendingKey(locator, batch.id));
|
|
3413
|
+
publishChangeSet(await together(local, [await unpairing(local, locator, [batch])]));
|
|
2634
3414
|
}
|
|
2635
3415
|
if (handled.size === 0)
|
|
2636
3416
|
return;
|
|
2637
3417
|
moveTo({ ...status, ...attention() });
|
|
2638
|
-
for (const key of handled)
|
|
2639
|
-
submitLater(key.slice(0, key.indexOf('\u0000')));
|
|
2640
3418
|
}
|
|
2641
3419
|
async function discardHeld(head, room, local, signal) {
|
|
2642
3420
|
const partner = partnerOf(head, room.locator);
|
|
2643
3421
|
const other = partner === null ? undefined : discovered.get(partner);
|
|
2644
|
-
await discardPending(room, local, signal);
|
|
2645
|
-
if (other !== undefined && !signal.aborted)
|
|
2646
|
-
await discardPending(other, local, signal);
|
|
3422
|
+
await discardPending(other === undefined ? [room] : [room, other], local, signal);
|
|
2647
3423
|
}
|
|
2648
3424
|
async function submitRoom(room, local, authorized, progress, signal) {
|
|
2649
3425
|
while (continuing() && authorization !== undefined && !signal.aborted) {
|
|
@@ -2705,6 +3481,7 @@ export function composeRuntime(options) {
|
|
|
2705
3481
|
result = await options.adapter.mutate(authorized, {
|
|
2706
3482
|
locator: room.locator,
|
|
2707
3483
|
changeBatch: head,
|
|
3484
|
+
file: batchFile(head),
|
|
2708
3485
|
...(state?.upload === undefined ? {} : { upload: state.upload }),
|
|
2709
3486
|
}, (upload) => exclusive(() => keepUpload(local, room.locator, head.id, upload)));
|
|
2710
3487
|
}
|
|
@@ -2813,14 +3590,12 @@ export function composeRuntime(options) {
|
|
|
2813
3590
|
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
2814
3591
|
return;
|
|
2815
3592
|
}
|
|
2816
|
-
const
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
});
|
|
2823
|
-
publishChangeSet(settled());
|
|
3593
|
+
const confirmedLayer = await sliceAt(current.record, 'confirmed', [head]);
|
|
3594
|
+
const held = (result.kind === 'expired' &&
|
|
3595
|
+
(await landedWhole(room.locator, head, confirmedLayer))) ||
|
|
3596
|
+
(result.kind === 'conflicted' &&
|
|
3597
|
+
(await landedWhole(room.locator, head, confirmedLayer, result.affected)));
|
|
3598
|
+
publishChangeSet(await settle(local, room.locator, current, current.record, current.queue.filter((batch) => batch.id !== head.id), { leaving: [held ? { ...left, returned: null } : left] }));
|
|
2824
3599
|
await unpair(local);
|
|
2825
3600
|
moveTo({ ...status, ...attention() });
|
|
2826
3601
|
});
|
|
@@ -2848,44 +3623,67 @@ export function composeRuntime(options) {
|
|
|
2848
3623
|
function pastHorizon(identity) {
|
|
2849
3624
|
return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
|
|
2850
3625
|
}
|
|
2851
|
-
async function discardPending(
|
|
2852
|
-
const { locator } = room;
|
|
3626
|
+
async function discardPending(rooms, local, signal) {
|
|
2853
3627
|
const discarded = await exclusive(async () => {
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
3628
|
+
const held = rooms
|
|
3629
|
+
.filter(({ locator }) => !fences.has(locator))
|
|
3630
|
+
.map((room) => ({
|
|
3631
|
+
room,
|
|
3632
|
+
before: replicas.get(room.locator),
|
|
3633
|
+
queue: queueIn(room.locator),
|
|
3634
|
+
}));
|
|
3635
|
+
if (held.length === 0)
|
|
3636
|
+
return [];
|
|
3637
|
+
const answer = await local.commit({
|
|
3638
|
+
rooms: held.flatMap(({ room, before }) => before === undefined
|
|
3639
|
+
? []
|
|
3640
|
+
: [
|
|
3641
|
+
{
|
|
3642
|
+
locator: room.locator,
|
|
3643
|
+
key: before.record.key,
|
|
3644
|
+
roots: before.record.typedSelection?.roots ?? [],
|
|
3645
|
+
overlay: { set: [], cleared: [...before.overlaid] },
|
|
3646
|
+
},
|
|
3647
|
+
]),
|
|
3648
|
+
discards: held.map(({ room }) => room.locator),
|
|
3649
|
+
});
|
|
3650
|
+
for (const { room, before, queue } of held) {
|
|
3651
|
+
const { locator } = room;
|
|
3652
|
+
lastFenced.set(locator, ++fencings);
|
|
3653
|
+
for (const batch of queue)
|
|
3654
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
3655
|
+
queuesWithoutReplica.delete(locator);
|
|
3656
|
+
if (before !== undefined) {
|
|
3657
|
+
replicas.set(locator, {
|
|
3658
|
+
...before,
|
|
3659
|
+
queue: [],
|
|
3660
|
+
overlaid: new Set(),
|
|
3661
|
+
count: answer.counts[locator] ?? before.count,
|
|
3662
|
+
});
|
|
3663
|
+
}
|
|
3664
|
+
journal.record(`room discarded ${locator}`);
|
|
2865
3665
|
}
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
if (after !== null)
|
|
2871
|
-
replicas.set(locator, after);
|
|
2872
|
-
journal.record(`room discarded ${locator}`);
|
|
2873
|
-
publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
|
|
3666
|
+
publishChangeSet(mergeChangeSets(held.map(({ room }) => changeSet(room.locator, {
|
|
3667
|
+
documents: answer.changed[room.locator] ?? [],
|
|
3668
|
+
queue: true,
|
|
3669
|
+
}))));
|
|
2874
3670
|
moveTo({ ...status, ...attention() });
|
|
2875
|
-
return
|
|
3671
|
+
return held.map(({ room }) => room);
|
|
2876
3672
|
});
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
await
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
3673
|
+
for (const room of discarded) {
|
|
3674
|
+
if (signal.aborted)
|
|
3675
|
+
return;
|
|
3676
|
+
const progress = { complete: true, reachable: true };
|
|
3677
|
+
await roomTask(room.locator, async (live) => {
|
|
3678
|
+
await Promise.race([
|
|
3679
|
+
reload(room, local, progress, () => live() && !signal.aborted),
|
|
3680
|
+
abandonedOn(signal),
|
|
3681
|
+
]);
|
|
3682
|
+
});
|
|
3683
|
+
if (signal.aborted)
|
|
3684
|
+
return;
|
|
3685
|
+
await rest(progress);
|
|
3686
|
+
}
|
|
2889
3687
|
}
|
|
2890
3688
|
function awaitingAnswer(locator) {
|
|
2891
3689
|
let release = () => { };
|
|
@@ -2902,7 +3700,7 @@ export function composeRuntime(options) {
|
|
|
2902
3700
|
async function retainModel(local, room, attached, next) {
|
|
2903
3701
|
const key = modelKey(room.locator, attached);
|
|
2904
3702
|
const previous = modelOf(room.locator, attached);
|
|
2905
|
-
const heldBefore = replicas.get(room.locator)?.
|
|
3703
|
+
const heldBefore = replicas.get(room.locator)?.record;
|
|
2906
3704
|
if (JSON.stringify(previous) === JSON.stringify(next) &&
|
|
2907
3705
|
(heldBefore === undefined ||
|
|
2908
3706
|
(JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
|
|
@@ -2921,33 +3719,48 @@ export function composeRuntime(options) {
|
|
|
2921
3719
|
let changed = null;
|
|
2922
3720
|
if (held !== undefined) {
|
|
2923
3721
|
const declared = {
|
|
2924
|
-
...held.
|
|
3722
|
+
...held.record,
|
|
2925
3723
|
operations: room.operations,
|
|
2926
3724
|
replica: room.replica,
|
|
2927
3725
|
typedSelection: room.typedSelection ?? null,
|
|
2928
3726
|
...(attached === 'collection' ? { model: next } : { selectionModel: next }),
|
|
2929
3727
|
};
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
3728
|
+
let read = 'staged';
|
|
3729
|
+
if (!sameMembership(declared, held.record)) {
|
|
3730
|
+
const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), compositionOf(declared));
|
|
3731
|
+
read =
|
|
3732
|
+
verdict.verdict !== 'refuse'
|
|
3733
|
+
? 'staged'
|
|
3734
|
+
: verdict.rule === 'model-unavailable'
|
|
3735
|
+
? 'deferred'
|
|
3736
|
+
: { kind: 'refused', refusal: verdict };
|
|
3737
|
+
}
|
|
3738
|
+
const record = read === 'staged'
|
|
2934
3739
|
? declared
|
|
2935
|
-
: {
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
const
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
3740
|
+
: { ...declared, typedSelection: held.record.typedSelection };
|
|
3741
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', held.queue, held.overlaid);
|
|
3742
|
+
const replayedLayer = await replayed(record, confirmedLayer, held.queue);
|
|
3743
|
+
const diff = await overlayDiff(record, held, replayedLayer.visible, replayedLayer.touched);
|
|
3744
|
+
const answer = await local.commit({
|
|
3745
|
+
rooms: [
|
|
3746
|
+
{
|
|
3747
|
+
locator: room.locator,
|
|
3748
|
+
key: record.key,
|
|
3749
|
+
record: storedRecord(record),
|
|
3750
|
+
roots: record.typedSelection?.roots ?? [],
|
|
3751
|
+
composition: compositionOf(record),
|
|
3752
|
+
overlay: diff.overlay,
|
|
3753
|
+
},
|
|
3754
|
+
],
|
|
3755
|
+
});
|
|
3756
|
+
recomposed(room.locator, held.record, record);
|
|
3757
|
+
const { count } = committedRoom(room.locator, record, answer);
|
|
3758
|
+
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count });
|
|
3759
|
+
changed = changeSet(room.locator, {
|
|
3760
|
+
documents: answer.changed[room.locator] ?? [],
|
|
3761
|
+
room: true,
|
|
3762
|
+
});
|
|
3763
|
+
if (read === 'staged' || read === 'deferred') {
|
|
2951
3764
|
if (declarationRefusals.delete(room.locator)) {
|
|
2952
3765
|
sourceRefusals.delete(room.locator);
|
|
2953
3766
|
unreadable.delete(room.locator);
|
|
@@ -3000,17 +3813,10 @@ export function composeRuntime(options) {
|
|
|
3000
3813
|
}
|
|
3001
3814
|
function heldRooms(listed) {
|
|
3002
3815
|
return listed.flatMap((room) => {
|
|
3003
|
-
const record = replicas.get(room.locator)?.
|
|
3816
|
+
const record = replicas.get(room.locator)?.record;
|
|
3004
3817
|
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
3005
3818
|
return [];
|
|
3006
|
-
return [
|
|
3007
|
-
{
|
|
3008
|
-
locator: room.locator,
|
|
3009
|
-
roomVersion: record.roomVersion,
|
|
3010
|
-
documents: record.documents,
|
|
3011
|
-
revisions: record.revisions,
|
|
3012
|
-
},
|
|
3013
|
-
];
|
|
3819
|
+
return [{ locator: room.locator, roomVersion: record.roomVersion }];
|
|
3014
3820
|
});
|
|
3015
3821
|
}
|
|
3016
3822
|
async function synchronize(listed, local, progress, attempt, live) {
|
|
@@ -3038,7 +3844,7 @@ export function composeRuntime(options) {
|
|
|
3038
3844
|
progress.complete = false;
|
|
3039
3845
|
return false;
|
|
3040
3846
|
}
|
|
3041
|
-
const prior = replicas.get(room.locator)?.
|
|
3847
|
+
const prior = replicas.get(room.locator)?.record;
|
|
3042
3848
|
if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
|
|
3043
3849
|
list(prior, false);
|
|
3044
3850
|
emitRooms();
|
|
@@ -3092,7 +3898,7 @@ export function composeRuntime(options) {
|
|
|
3092
3898
|
await withhold(bootstrapped, room, local, progress);
|
|
3093
3899
|
return;
|
|
3094
3900
|
}
|
|
3095
|
-
await publish(local, room, bootstrapped.
|
|
3901
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
3096
3902
|
await caughtUp(room, local, progress, live);
|
|
3097
3903
|
}
|
|
3098
3904
|
finally {
|
|
@@ -3179,11 +3985,12 @@ export function composeRuntime(options) {
|
|
|
3179
3985
|
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
3180
3986
|
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
3181
3987
|
limits = result.providerLimits;
|
|
3182
|
-
await local.
|
|
3988
|
+
await local.fact('providerLimits', limits);
|
|
3989
|
+
await local.fact('capabilities', capabilities);
|
|
3183
3990
|
}
|
|
3184
3991
|
const signedIn = result.member ?? null;
|
|
3185
3992
|
if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
|
|
3186
|
-
await local.
|
|
3993
|
+
await local.fact('member', signedIn);
|
|
3187
3994
|
member = signedIn;
|
|
3188
3995
|
emitMember();
|
|
3189
3996
|
}
|
|
@@ -3387,7 +4194,7 @@ export function composeRuntime(options) {
|
|
|
3387
4194
|
owed.clear();
|
|
3388
4195
|
for (const locator of reloading)
|
|
3389
4196
|
catching.add(locator);
|
|
3390
|
-
|
|
4197
|
+
liveQueries.recover();
|
|
3391
4198
|
return reloading;
|
|
3392
4199
|
}
|
|
3393
4200
|
async function reloadNow(replaced, progress) {
|
|
@@ -3554,7 +4361,7 @@ export function composeRuntime(options) {
|
|
|
3554
4361
|
if (synced && !recorded) {
|
|
3555
4362
|
const completedAt = new Date().toISOString();
|
|
3556
4363
|
try {
|
|
3557
|
-
await (await openLocal()).
|
|
4364
|
+
await (await openLocal()).fact('lastCompleted', completedAt);
|
|
3558
4365
|
if (!live())
|
|
3559
4366
|
return;
|
|
3560
4367
|
recorded = true;
|
|
@@ -3594,8 +4401,8 @@ export function composeRuntime(options) {
|
|
|
3594
4401
|
if (room === undefined || room.replica.admission !== 'admitted')
|
|
3595
4402
|
return;
|
|
3596
4403
|
if (held !== undefined &&
|
|
3597
|
-
(held.
|
|
3598
|
-
options.adapter.reached(locator, held.
|
|
4404
|
+
(held.record.replica.selection !== room.replica.selection ||
|
|
4405
|
+
options.adapter.reached(locator, held.record.roomVersion, roomVersion))) {
|
|
3599
4406
|
return;
|
|
3600
4407
|
}
|
|
3601
4408
|
const during = underway?.generation === generation ? underway.progress : null;
|
|
@@ -3657,7 +4464,14 @@ export function composeRuntime(options) {
|
|
|
3657
4464
|
void rediscover(event.scope);
|
|
3658
4465
|
else
|
|
3659
4466
|
void applyRoom(event.locator, event.roomVersion);
|
|
3660
|
-
}, receipts);
|
|
4467
|
+
}, receipts, heldRevisions);
|
|
4468
|
+
}
|
|
4469
|
+
async function heldRevisions(locator, paths) {
|
|
4470
|
+
const held = replicas.get(locator);
|
|
4471
|
+
if (held === undefined || paths.length === 0)
|
|
4472
|
+
return new Map();
|
|
4473
|
+
const entries = await (await openLocal()).entries(held.record.key, 'confirmed', paths);
|
|
4474
|
+
return new Map(entries.flatMap(({ path, revision }) => revision === null ? [] : [[path, revision]]));
|
|
3661
4475
|
}
|
|
3662
4476
|
function writesHeld() {
|
|
3663
4477
|
return serviceMode === 'write-halted' || haltedRefusal;
|
|
@@ -3749,7 +4563,7 @@ export function composeRuntime(options) {
|
|
|
3749
4563
|
statusListeners.clear();
|
|
3750
4564
|
serviceModeListeners.clear();
|
|
3751
4565
|
memberListeners.clear();
|
|
3752
|
-
|
|
4566
|
+
liveQueries.close();
|
|
3753
4567
|
queueSubscriptions.clear();
|
|
3754
4568
|
roomDraftListeners.clear();
|
|
3755
4569
|
presentationListeners.clear();
|
|
@@ -3795,7 +4609,7 @@ export function composeRuntime(options) {
|
|
|
3795
4609
|
const watchers = seedWatchers.get(plan.key) ?? new Set();
|
|
3796
4610
|
seedWatchers.set(plan.key, watchers);
|
|
3797
4611
|
watchers.add(shown);
|
|
3798
|
-
const stop =
|
|
4612
|
+
const stop = liveQueries.watch(plan, identityOf(plan), () => {
|
|
3799
4613
|
const cover = covered(plan);
|
|
3800
4614
|
awaitPreparation(cover);
|
|
3801
4615
|
return seedBehind(plan, cover);
|
|
@@ -3866,10 +4680,9 @@ export function composeRuntime(options) {
|
|
|
3866
4680
|
if ('pending' in answered)
|
|
3867
4681
|
continue;
|
|
3868
4682
|
const searchedConversations = named.filter((one) => searchIndex.conversations().has(one));
|
|
3869
|
-
await readSide.settled();
|
|
3870
4683
|
const seen = searchIndex.changes();
|
|
3871
4684
|
await searchIndex.caughtUp();
|
|
3872
|
-
const selected = roomsAlone(answering) ? null : await
|
|
4685
|
+
const selected = roomsAlone(answering) ? null : await everyRow(answering);
|
|
3873
4686
|
if (selected !== null && 'pending' in selected)
|
|
3874
4687
|
continue;
|
|
3875
4688
|
if (searchIndex.changes() !== seen ||
|
|
@@ -3882,7 +4695,7 @@ export function composeRuntime(options) {
|
|
|
3882
4695
|
: new Set(reachedRooms(answering)), plan.collection
|
|
3883
4696
|
? new Map(answered.coverage.rooms.map(({ room }) => [
|
|
3884
4697
|
room,
|
|
3885
|
-
replicas.get(room)?.
|
|
4698
|
+
replicas.get(room)?.record.typedSelection?.roots ?? [],
|
|
3886
4699
|
]))
|
|
3887
4700
|
: null);
|
|
3888
4701
|
const coverage = {
|
|
@@ -3902,13 +4715,17 @@ export function composeRuntime(options) {
|
|
|
3902
4715
|
roomDraftListeners.add(listener);
|
|
3903
4716
|
void exclusive(async () => {
|
|
3904
4717
|
await openLocal();
|
|
3905
|
-
if (closed || !roomDraftListeners.has(listener))
|
|
3906
|
-
return;
|
|
3907
|
-
listener.ready = true;
|
|
3908
4718
|
}).then(() => {
|
|
3909
|
-
if (closed || !
|
|
4719
|
+
if (closed || !roomDraftListeners.has(listener))
|
|
3910
4720
|
return;
|
|
3911
|
-
|
|
4721
|
+
const snapshot = snapshotRoomDrafts();
|
|
4722
|
+
draftsShown = draftsShown.then(async () => {
|
|
4723
|
+
const value = await snapshot.catch(() => null);
|
|
4724
|
+
if (value === null || closed || !roomDraftListeners.has(listener))
|
|
4725
|
+
return;
|
|
4726
|
+
listener.ready = true;
|
|
4727
|
+
listener.notify(value);
|
|
4728
|
+
});
|
|
3912
4729
|
}, () => { });
|
|
3913
4730
|
return () => {
|
|
3914
4731
|
roomDraftListeners.delete(listener);
|
|
@@ -4023,9 +4840,9 @@ export function composeRuntime(options) {
|
|
|
4023
4840
|
return listed;
|
|
4024
4841
|
});
|
|
4025
4842
|
},
|
|
4026
|
-
read(room, path) {
|
|
4843
|
+
async read(room, path) {
|
|
4027
4844
|
const record = roomDrafts.get(room)?.get(path);
|
|
4028
|
-
return record === undefined ? null :
|
|
4845
|
+
return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
|
|
4029
4846
|
},
|
|
4030
4847
|
write(room, path, revision, input) {
|
|
4031
4848
|
return exclusive(async () => {
|
|
@@ -4035,11 +4852,11 @@ export function composeRuntime(options) {
|
|
|
4035
4852
|
const local = await openLocal();
|
|
4036
4853
|
const current = roomDrafts.get(room)?.get(path);
|
|
4037
4854
|
if ((current?.revision ?? null) !== revision) {
|
|
4038
|
-
return staleWrite(current === undefined ? null :
|
|
4855
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
4039
4856
|
}
|
|
4040
4857
|
const file = parts.file === undefined
|
|
4041
4858
|
? undefined
|
|
4042
|
-
: await heldFile(parts.file.blob, binaryBase(
|
|
4859
|
+
: await heldFile(parts.file.blob, binaryBase(await visibleOf(room, [path]), path));
|
|
4043
4860
|
const record = {
|
|
4044
4861
|
locator: room,
|
|
4045
4862
|
path,
|
|
@@ -4047,10 +4864,12 @@ export function composeRuntime(options) {
|
|
|
4047
4864
|
parts: held(current?.parts, parts, file),
|
|
4048
4865
|
returned: current?.returned ?? null,
|
|
4049
4866
|
};
|
|
4050
|
-
await local.keepRoomDraft(record
|
|
4867
|
+
await local.keepRoomDraft(record, parts.file === undefined || file === undefined
|
|
4868
|
+
? []
|
|
4869
|
+
: [{ key: file.localId, blob: parts.file.blob }]);
|
|
4051
4870
|
rememberRoomDraft(record);
|
|
4052
|
-
publishChangeSet(changeSet(room,
|
|
4053
|
-
return { outcome: 'written', roomDraft:
|
|
4871
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
4872
|
+
return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
|
|
4054
4873
|
});
|
|
4055
4874
|
},
|
|
4056
4875
|
discard(room, path, revision) {
|
|
@@ -4058,11 +4877,11 @@ export function composeRuntime(options) {
|
|
|
4058
4877
|
const local = await openLocal();
|
|
4059
4878
|
const current = roomDrafts.get(room)?.get(path);
|
|
4060
4879
|
if (current === undefined || current.revision !== revision) {
|
|
4061
|
-
return staleWrite(current === undefined ? null :
|
|
4880
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
4062
4881
|
}
|
|
4063
4882
|
await local.discardRoomDraft(room, path);
|
|
4064
4883
|
forgetRoomDraft(room, path);
|
|
4065
|
-
publishChangeSet(changeSet(room,
|
|
4884
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
4066
4885
|
moveTo({ ...status, ...attention() });
|
|
4067
4886
|
return { outcome: 'written', roomDraft: null };
|
|
4068
4887
|
});
|
|
@@ -4100,6 +4919,7 @@ export function composeRuntime(options) {
|
|
|
4100
4919
|
return resolveReference(room, path, destination);
|
|
4101
4920
|
},
|
|
4102
4921
|
binaryFile: {
|
|
4922
|
+
part: readPart,
|
|
4103
4923
|
read: readBinaryPath,
|
|
4104
4924
|
link: linkBinaryPath,
|
|
4105
4925
|
readReference: readBinaryReference,
|
|
@@ -4109,7 +4929,7 @@ export function composeRuntime(options) {
|
|
|
4109
4929
|
const room = calledRooms(name, input).find((one) => locked(one));
|
|
4110
4930
|
return room === undefined ? recoveredAction(name, input) : lockedRoom(room);
|
|
4111
4931
|
},
|
|
4112
|
-
reached(locator, roomVersion, path) {
|
|
4932
|
+
async reached(locator, roomVersion, path) {
|
|
4113
4933
|
return path === undefined
|
|
4114
4934
|
? reachedBy(locator, roomVersion)
|
|
4115
4935
|
: writtenAfter(locator, roomVersion, path);
|
|
@@ -4117,6 +4937,7 @@ export function composeRuntime(options) {
|
|
|
4117
4937
|
log() {
|
|
4118
4938
|
return journal.export();
|
|
4119
4939
|
},
|
|
4940
|
+
cleanUp,
|
|
4120
4941
|
persistence() {
|
|
4121
4942
|
return persistence === 'granted';
|
|
4122
4943
|
},
|