@agentshouse/mdruntime 0.1.0-c8f3163f32f93 → 0.1.0-c91a238f9703e
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 +281 -275
- package/README.md +8 -8
- package/dist/acquisition.d.ts +14 -0
- package/dist/acquisition.d.ts.map +1 -0
- package/dist/acquisition.js +24 -0
- package/dist/adapter.d.ts +32 -8
- package/dist/adapter.d.ts.map +1 -1
- package/dist/bootstrap.d.ts +2 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +4 -0
- package/dist/change-set.d.ts +2 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +36 -10
- package/dist/chat/earlier.d.ts +10 -0
- package/dist/chat/earlier.d.ts.map +1 -0
- package/dist/chat/earlier.js +54 -0
- package/dist/chat/kit.d.ts +2 -1
- package/dist/chat/kit.d.ts.map +1 -1
- package/dist/chat/kit.js +3 -3
- package/dist/chat/list.d.ts +2 -1
- package/dist/chat/list.d.ts.map +1 -1
- package/dist/chat/list.js +42 -5
- package/dist/chat/message.d.ts.map +1 -1
- package/dist/chat/message.js +2 -2
- package/dist/chat/messages.d.ts +1 -0
- package/dist/chat/messages.d.ts.map +1 -1
- package/dist/chat/messages.js +4 -0
- package/dist/chat/provider.d.ts +1 -1
- package/dist/chat/provider.d.ts.map +1 -1
- package/dist/chat/provider.js +37 -29
- package/dist/chat/question.d.ts +1 -1
- package/dist/chat/question.d.ts.map +1 -1
- package/dist/chat/question.js +7 -18
- package/dist/chat/ready.d.ts +1 -0
- package/dist/chat/ready.d.ts.map +1 -1
- package/dist/chat/ready.js +2 -2
- package/dist/chat/style.js +1 -1
- package/dist/client.d.ts +16 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +18 -5
- package/dist/collection.d.ts +17 -3
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +230 -19
- package/dist/connection.d.ts +32 -9
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1326 -255
- package/dist/conversation.d.ts +16 -5
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +323 -119
- package/dist/durability.d.ts +7 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +2 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +3 -3
- package/dist/host.d.ts +16 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +90 -8
- package/dist/identity.d.ts +2 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +6 -0
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/memory.d.ts +8 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +30 -7
- package/dist/model.d.ts +4 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +10 -1
- package/dist/observation.d.ts +7 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +29 -12
- package/dist/outcome.d.ts +8 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +3 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +50 -55
- package/dist/query.d.ts +56 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +36 -7
- package/dist/reach.d.ts +6 -3
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +29 -4
- package/dist/react.d.ts +8 -6
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +419 -92
- package/dist/readiness.d.ts +53 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +98 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +197 -0
- package/dist/refresh.d.ts +4 -4
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +17 -10
- package/dist/replica.d.ts +6 -2
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +76 -23
- package/dist/room-draft.d.ts +13 -1
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +2 -2
- package/dist/rows.d.ts +6 -13
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +10 -13
- package/dist/save.d.ts +6 -0
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +53 -14
- package/dist/search.d.ts +49 -6
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +192 -47
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +11 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +368 -0
- package/dist/status.d.ts +4 -0
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +36 -6
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +193 -55
- package/dist/turn.d.ts +7 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +45 -0
- package/dist/windows.d.ts +26 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +93 -0
- package/dist/wire.d.ts +152 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +52 -31
- package/package.json +2 -2
package/dist/connection.js
CHANGED
|
@@ -1,31 +1,48 @@
|
|
|
1
1
|
import { attachedModel } from './attached-model.js';
|
|
2
2
|
import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
|
|
3
|
-
import { observing } from './observation.js';
|
|
4
|
-
import { filterOf, identityOf, planOf, } from './query.js';
|
|
3
|
+
import { observing, whole } from './observation.js';
|
|
4
|
+
import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
|
|
5
5
|
import { createReadSide } from './read-side.js';
|
|
6
|
+
import { seededResult } from './seed.js';
|
|
6
7
|
import { markdownPath, roomOfKey } from './rows.js';
|
|
7
8
|
import { createSearchIndex, searchPlanOf, } from './search.js';
|
|
9
|
+
import { capped, MAXIMUM_WINDOW } from './windows.js';
|
|
8
10
|
import { bootstrapRoom } from './bootstrap.js';
|
|
9
|
-
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
11
|
+
import { changeSet, entering, mergeChangeSets } from './change-set.js';
|
|
10
12
|
import { createConversationStore, } from './conversation.js';
|
|
11
13
|
import { queuedFile } from './change.js';
|
|
12
|
-
import { validRoot } from './collection.js';
|
|
14
|
+
import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
|
|
13
15
|
import { runtimeError } from './failure.js';
|
|
14
16
|
import { createLog } from './log.js';
|
|
15
17
|
import { binaryRecord } from './binary.js';
|
|
16
|
-
import { absentBinary, binaryAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
|
|
18
|
+
import { absentBinary, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
|
|
19
|
+
import { createRecovery } from './recovery.js';
|
|
20
|
+
import { doorReads } from './reads.js';
|
|
17
21
|
import { refreshRoom } from './refresh.js';
|
|
18
|
-
import { buildBatch, joined, limitedIn, requeued } from './save.js';
|
|
19
|
-
import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership,
|
|
20
|
-
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
22
|
+
import { buildBatch, joined, limitedIn, queuedDocuments, requeued, } from './save.js';
|
|
23
|
+
import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, standing, } from './replica.js';
|
|
24
|
+
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
21
25
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
22
26
|
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
27
|
+
import { demanded, narrower, WHOLE_DEMAND, wider } from './acquisition.js';
|
|
28
|
+
import { readinessOf, SETTLED_ROOMS, } from './readiness.js';
|
|
23
29
|
import { FencedMap } from './fence.js';
|
|
24
|
-
import {
|
|
30
|
+
import { tally } from './shared.js';
|
|
31
|
+
import { inChunks, ROW_WEIGHT, yielded } from './turn.js';
|
|
32
|
+
import { identityTime, roomDraftRevision } from './identity.js';
|
|
25
33
|
import { beneath, normalize } from './paths.js';
|
|
26
34
|
import { openRuntimeStore, } from './store.js';
|
|
27
35
|
import { unconfirmed } from './transfer.js';
|
|
28
36
|
import { governs, holds, narrowed, planReach, } from './reach.js';
|
|
37
|
+
function roomsAlone(plan) {
|
|
38
|
+
return (plan.reach === null &&
|
|
39
|
+
plan.root.types === null &&
|
|
40
|
+
plan.root.where === null &&
|
|
41
|
+
plan.root.pathPrefix === null &&
|
|
42
|
+
plan.paths === null &&
|
|
43
|
+
plan.hop === null &&
|
|
44
|
+
plan.backlinks === null);
|
|
45
|
+
}
|
|
29
46
|
function within(root, path) {
|
|
30
47
|
return root === '' || beneath(root, path);
|
|
31
48
|
}
|
|
@@ -42,6 +59,33 @@ function declaredSelection(room) {
|
|
|
42
59
|
const OPENING_LANE = 'opening';
|
|
43
60
|
const CONVERSATION_LANE = 'conversations';
|
|
44
61
|
const ROOM_READS = 256;
|
|
62
|
+
const ROOM_ATTEMPTS = 16;
|
|
63
|
+
function operationOf(input) {
|
|
64
|
+
const operation = input?.operation_id;
|
|
65
|
+
return typeof operation === 'string' ? operation : null;
|
|
66
|
+
}
|
|
67
|
+
const outside = new Map();
|
|
68
|
+
function outsideOf(roots) {
|
|
69
|
+
const key = JSON.stringify(roots);
|
|
70
|
+
const held = outside.get(key);
|
|
71
|
+
if (held !== undefined)
|
|
72
|
+
return held;
|
|
73
|
+
const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
|
|
74
|
+
const made = {
|
|
75
|
+
documents: (path) => (markdownPath(path) ? counted(path) : 0),
|
|
76
|
+
binaries: counted,
|
|
77
|
+
};
|
|
78
|
+
outside.set(key, made);
|
|
79
|
+
return made;
|
|
80
|
+
}
|
|
81
|
+
function abandonedOn(signal) {
|
|
82
|
+
return new Promise((resolve) => {
|
|
83
|
+
if (signal.aborted)
|
|
84
|
+
resolve(null);
|
|
85
|
+
else
|
|
86
|
+
signal.addEventListener('abort', () => resolve(null), { once: true });
|
|
87
|
+
});
|
|
88
|
+
}
|
|
45
89
|
function roomLane(locator) {
|
|
46
90
|
return `room ${locator}`;
|
|
47
91
|
}
|
|
@@ -59,6 +103,8 @@ export function composeRuntime(options) {
|
|
|
59
103
|
const replicas = new FencedMap(isFenced);
|
|
60
104
|
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
61
105
|
const pendings = new Map();
|
|
106
|
+
const seeds = new Map();
|
|
107
|
+
const seedWatchers = new Map();
|
|
62
108
|
const roomDrafts = new FencedMap(isFenced);
|
|
63
109
|
const presentations = new Map();
|
|
64
110
|
let limits = [];
|
|
@@ -71,6 +117,7 @@ export function composeRuntime(options) {
|
|
|
71
117
|
const liveModels = new FencedMap(isModelFenced);
|
|
72
118
|
const invalidModels = new Set();
|
|
73
119
|
const discovered = new Map();
|
|
120
|
+
const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
|
|
74
121
|
let roomsListed = false;
|
|
75
122
|
const scopedRefusals = new Map();
|
|
76
123
|
const sourceRefusals = new Map();
|
|
@@ -98,7 +145,6 @@ export function composeRuntime(options) {
|
|
|
98
145
|
let status = STARTING;
|
|
99
146
|
let shown = STARTING;
|
|
100
147
|
let discoveryPaused = false;
|
|
101
|
-
let resetTimer;
|
|
102
148
|
let closed = false;
|
|
103
149
|
let stopping = false;
|
|
104
150
|
let withdrawStream;
|
|
@@ -106,7 +152,11 @@ export function composeRuntime(options) {
|
|
|
106
152
|
let underway = null;
|
|
107
153
|
let streamed = null;
|
|
108
154
|
let handed = false;
|
|
109
|
-
const
|
|
155
|
+
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
156
|
+
const repairing = new Set();
|
|
157
|
+
const calling = new Set();
|
|
158
|
+
const repairable = new Map();
|
|
159
|
+
let outcomeRetention = null;
|
|
110
160
|
const answering = new Map();
|
|
111
161
|
let complete = false;
|
|
112
162
|
let recorded = false;
|
|
@@ -136,25 +186,41 @@ export function composeRuntime(options) {
|
|
|
136
186
|
const serviceModeListeners = new Set();
|
|
137
187
|
let member = null;
|
|
138
188
|
const memberListeners = new Set();
|
|
139
|
-
const
|
|
140
|
-
|
|
189
|
+
const prepared = new Set();
|
|
190
|
+
const projecting = new Set();
|
|
191
|
+
const unpreparedDemand = new Set();
|
|
192
|
+
const unindexable = new Set();
|
|
193
|
+
let preparing = false;
|
|
194
|
+
let demand = WHOLE_DEMAND;
|
|
195
|
+
let narrowing = null;
|
|
196
|
+
let targets = {};
|
|
197
|
+
let persistence = options.shell.persistence;
|
|
198
|
+
let capacity = null;
|
|
199
|
+
const paused = new Set();
|
|
200
|
+
let rechecking = null;
|
|
201
|
+
const readinessWatches = new Set();
|
|
202
|
+
let readinessQueued = false;
|
|
203
|
+
const catching = new Set();
|
|
141
204
|
const transfers = new FencedMap(isFenced);
|
|
142
205
|
const conversations = createConversationStore({
|
|
143
206
|
agents: declaredAgents,
|
|
144
|
-
documents: (locator) => rowsQuery(planOf({ rooms: [locator], select: 'all' }, roomFacts())),
|
|
207
|
+
documents: ({ locator }) => rowsQuery(planOf({ rooms: [locator], select: { fields: 'all' } }, roomFacts())),
|
|
145
208
|
call: (name, input) => invokeAction(name, input),
|
|
146
209
|
durably,
|
|
147
210
|
stream: () => options.adapter.conversations,
|
|
148
211
|
publish: publishChangeSet,
|
|
149
|
-
online: () =>
|
|
212
|
+
online: () => connectionOf() !== 'unreachable' && refusal === null,
|
|
150
213
|
closed: () => closed,
|
|
214
|
+
unanswered: resendLater,
|
|
215
|
+
expired: (operation) => pastHorizon(operation),
|
|
151
216
|
});
|
|
152
217
|
const searchIndex = createSearchIndex({
|
|
153
|
-
|
|
218
|
+
room: (room) => readSide.room(room),
|
|
154
219
|
document: (room, path) => readSide.document(room, path),
|
|
155
|
-
messages: () => conversations.messages(),
|
|
156
|
-
message: (conversation, identity) => conversations.message(conversation, identity),
|
|
157
220
|
});
|
|
221
|
+
const roomsToIndex = new Set();
|
|
222
|
+
const conversationsToIndex = new Set();
|
|
223
|
+
let indexing = false;
|
|
158
224
|
const journal = createLog();
|
|
159
225
|
let notifyReady = () => { };
|
|
160
226
|
const ready = new Promise((resolve) => {
|
|
@@ -209,6 +275,15 @@ export function composeRuntime(options) {
|
|
|
209
275
|
const value = presentations.get(app);
|
|
210
276
|
return value === undefined ? null : structuredClone(value);
|
|
211
277
|
}
|
|
278
|
+
function keepPresentation(app, next) {
|
|
279
|
+
return exclusive(async () => {
|
|
280
|
+
const local = await openLocal();
|
|
281
|
+
const stored = next(presentations.get(app));
|
|
282
|
+
await local.present({ app, value: stored });
|
|
283
|
+
presentations.set(app, stored);
|
|
284
|
+
publishChangeSet(changeSet('', null, null, { presentation: app }));
|
|
285
|
+
});
|
|
286
|
+
}
|
|
212
287
|
function emitPresentation(app) {
|
|
213
288
|
if (closed)
|
|
214
289
|
return;
|
|
@@ -233,9 +308,16 @@ export function composeRuntime(options) {
|
|
|
233
308
|
returned: structuredClone(record.returned),
|
|
234
309
|
});
|
|
235
310
|
}
|
|
311
|
+
for (const [room, refusal] of sourceRefusals)
|
|
312
|
+
attentionRequired.push({ room, refusal: structuredClone(refusal) });
|
|
236
313
|
attentionRequired.sort((left, right) => {
|
|
237
314
|
if (left.room !== right.room)
|
|
238
315
|
return left.room < right.room ? -1 : 1;
|
|
316
|
+
if ('refusal' in left || 'refusal' in right) {
|
|
317
|
+
if ('refusal' in left && 'refusal' in right)
|
|
318
|
+
return 0;
|
|
319
|
+
return 'refusal' in left ? -1 : 1;
|
|
320
|
+
}
|
|
239
321
|
if (left.path !== right.path)
|
|
240
322
|
return left.path < right.path ? -1 : 1;
|
|
241
323
|
return 0;
|
|
@@ -254,11 +336,9 @@ export function composeRuntime(options) {
|
|
|
254
336
|
if (replica === undefined)
|
|
255
337
|
return 0;
|
|
256
338
|
const roots = replica.confirmed.record.typedSelection?.roots ?? [];
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
];
|
|
261
|
-
return paths.filter((path) => !roots.some((root) => beneath(root, path))).length;
|
|
339
|
+
const weighed = outsideOf(roots);
|
|
340
|
+
return (tally(replica.visible.documents, weighed.documents) +
|
|
341
|
+
tally(replica.visible.binaries, weighed.binaries));
|
|
262
342
|
}
|
|
263
343
|
function listed() {
|
|
264
344
|
return structuredClone([...rooms.values()].map((room) => {
|
|
@@ -285,15 +365,19 @@ export function composeRuntime(options) {
|
|
|
285
365
|
return { listed: roomsListed, rooms: held };
|
|
286
366
|
}
|
|
287
367
|
function emitRooms() {
|
|
368
|
+
readSide.recover();
|
|
369
|
+
queueReadiness();
|
|
288
370
|
const value = listed();
|
|
289
371
|
for (const listener of roomListeners)
|
|
290
372
|
listener(value);
|
|
291
373
|
}
|
|
292
374
|
function emitRefusal() {
|
|
375
|
+
queueReadiness();
|
|
293
376
|
for (const listener of refusalListeners)
|
|
294
377
|
listener(refusal);
|
|
295
378
|
}
|
|
296
379
|
function emitStorageLoss() {
|
|
380
|
+
queueReadiness();
|
|
297
381
|
for (const listener of storageLossListeners)
|
|
298
382
|
listener(storageLoss);
|
|
299
383
|
}
|
|
@@ -310,7 +394,8 @@ export function composeRuntime(options) {
|
|
|
310
394
|
function show() {
|
|
311
395
|
if (closed)
|
|
312
396
|
return;
|
|
313
|
-
|
|
397
|
+
queueReadiness();
|
|
398
|
+
const next = status.state === 'synced' && demand.conversations && !conversations.current()
|
|
314
399
|
? { ...status, state: 'syncing' }
|
|
315
400
|
: status;
|
|
316
401
|
if (same(shown, next))
|
|
@@ -339,42 +424,517 @@ export function composeRuntime(options) {
|
|
|
339
424
|
}
|
|
340
425
|
}
|
|
341
426
|
function applyChangeSet(set) {
|
|
342
|
-
const held =
|
|
343
|
-
|
|
344
|
-
|
|
427
|
+
const held = {
|
|
428
|
+
...set,
|
|
429
|
+
documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
|
|
430
|
+
};
|
|
345
431
|
readSide.apply(held);
|
|
346
432
|
searchIndex.changed(held);
|
|
347
433
|
}
|
|
348
|
-
function
|
|
349
|
-
|
|
434
|
+
async function referenced(locator) {
|
|
435
|
+
for (;;) {
|
|
436
|
+
const record = replicas.get(locator)?.confirmed.record;
|
|
437
|
+
if (record === undefined || closed)
|
|
438
|
+
return;
|
|
439
|
+
const held = { text: record.documents, binaries: record.binaries };
|
|
440
|
+
const composition = compositionOf(record);
|
|
441
|
+
if (referencesIndexed(held, composition))
|
|
442
|
+
return;
|
|
443
|
+
await indexReferrers(held, composition);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
async function prepareReplica(locator) {
|
|
447
|
+
await referenced(locator);
|
|
448
|
+
const replica = replicas.get(locator);
|
|
449
|
+
if (replica === undefined || closed || prepared.has(locator))
|
|
350
450
|
return;
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
451
|
+
const visible = replica.visible;
|
|
452
|
+
const paths = [
|
|
453
|
+
...new Set([...Object.keys(visible.documents), ...Object.keys(visible.binaries)]),
|
|
454
|
+
];
|
|
455
|
+
const entered = [];
|
|
456
|
+
projecting.add(locator);
|
|
457
|
+
try {
|
|
458
|
+
await inChunks(paths, (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
|
|
459
|
+
const current = replicas.get(locator);
|
|
460
|
+
if (current === undefined || closed)
|
|
461
|
+
return;
|
|
462
|
+
const set = entering(locator, current, chunk);
|
|
463
|
+
readSide.apply(set);
|
|
464
|
+
entered.push(set);
|
|
465
|
+
});
|
|
466
|
+
if (!replicas.has(locator) || closed)
|
|
467
|
+
return;
|
|
468
|
+
prepared.add(locator);
|
|
361
469
|
}
|
|
362
|
-
|
|
470
|
+
finally {
|
|
471
|
+
projecting.delete(locator);
|
|
472
|
+
}
|
|
473
|
+
conversations.changed(mergeChangeSets(entered));
|
|
474
|
+
readSide.recover();
|
|
475
|
+
queueReadiness();
|
|
476
|
+
}
|
|
477
|
+
function unprepared() {
|
|
478
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !prepared.has(locator) && !unindexable.has(locator));
|
|
479
|
+
}
|
|
480
|
+
function unbuilt(locator, error) {
|
|
481
|
+
unindexable.add(locator);
|
|
482
|
+
journal.record(`index failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
483
|
+
queueReadiness();
|
|
484
|
+
}
|
|
485
|
+
function prepare(locators = []) {
|
|
486
|
+
for (const locator of locators)
|
|
487
|
+
unpreparedDemand.add(locator);
|
|
488
|
+
if (preparing)
|
|
363
489
|
return;
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
490
|
+
preparing = true;
|
|
491
|
+
void (async () => {
|
|
492
|
+
try {
|
|
493
|
+
for (let waiting = unprepared(); waiting !== undefined; waiting = unprepared()) {
|
|
494
|
+
await yielded();
|
|
495
|
+
if (closed)
|
|
496
|
+
return;
|
|
497
|
+
unpreparedDemand.delete(waiting);
|
|
498
|
+
await prepareReplica(waiting).catch((error) => unbuilt(waiting, error));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
finally {
|
|
502
|
+
preparing = false;
|
|
503
|
+
}
|
|
504
|
+
})();
|
|
367
505
|
}
|
|
368
506
|
function reachedRooms(plan) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
507
|
+
const reach = plan.reach;
|
|
508
|
+
const reached = reach === null ? null : new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
|
|
509
|
+
const named = plan.rooms ??
|
|
510
|
+
(reached === null
|
|
511
|
+
? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
|
|
512
|
+
: [...reached]);
|
|
513
|
+
return [...new Set(named)].filter((locator) => reached === null || reached.has(locator)).sort();
|
|
514
|
+
}
|
|
515
|
+
function awaited(locator) {
|
|
516
|
+
return (!roomsListed ||
|
|
517
|
+
(rooms.has(locator) && discovered.get(locator)?.replica.admission === 'admitted'));
|
|
518
|
+
}
|
|
519
|
+
function covered(plan) {
|
|
520
|
+
const locators = reachedRooms(plan);
|
|
521
|
+
if (locators.length === 0 && plan.rooms === null && plan.reach === null && !roomsListed) {
|
|
522
|
+
return { pending: [] };
|
|
523
|
+
}
|
|
524
|
+
const pending = [];
|
|
525
|
+
const coverage = [];
|
|
526
|
+
const overlay = [];
|
|
527
|
+
for (const locator of locators) {
|
|
528
|
+
const replica = replicas.get(locator);
|
|
529
|
+
if (replica === undefined) {
|
|
530
|
+
if (awaited(locator))
|
|
531
|
+
pending.push({ room: locator, awaits: 'acquisition' });
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (!prepared.has(locator)) {
|
|
535
|
+
pending.push({ room: locator, awaits: 'preparation' });
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
coverage.push({
|
|
539
|
+
room: locator,
|
|
540
|
+
generation: replica.confirmed.record.transfer,
|
|
541
|
+
current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
|
|
542
|
+
});
|
|
543
|
+
for (const batch of replica.queue)
|
|
544
|
+
overlay.push(batch.id);
|
|
545
|
+
}
|
|
546
|
+
if (pending.length > 0)
|
|
547
|
+
return { pending };
|
|
548
|
+
return { coverage: { rooms: coverage, indexes: [], overlay } };
|
|
549
|
+
}
|
|
550
|
+
function awaitPreparation(cover) {
|
|
551
|
+
if (!('pending' in cover))
|
|
552
|
+
return;
|
|
553
|
+
const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
|
|
554
|
+
if (waiting.length > 0)
|
|
555
|
+
prepare(waiting.map(({ room }) => room));
|
|
556
|
+
}
|
|
557
|
+
function acquires(room) {
|
|
558
|
+
return (demanded(demand, room) ||
|
|
559
|
+
(replicas.get(room.locator)?.queue.length ?? 0) > 0 ||
|
|
560
|
+
queuesWithoutReplica.has(room.locator));
|
|
561
|
+
}
|
|
562
|
+
function acquiredLocators() {
|
|
563
|
+
return [...discovered.values()]
|
|
564
|
+
.filter(acquires)
|
|
565
|
+
.map((room) => room.locator)
|
|
566
|
+
.sort();
|
|
567
|
+
}
|
|
568
|
+
function heldLocators(acquired) {
|
|
569
|
+
return demand.whole ? acquired : [...new Set([...acquired, ...demand.rooms])].sort();
|
|
570
|
+
}
|
|
571
|
+
function heldDemand(acquired) {
|
|
572
|
+
if (demand.whole)
|
|
573
|
+
return null;
|
|
574
|
+
return {
|
|
575
|
+
rooms: heldLocators(acquired.map((room) => room.locator)),
|
|
576
|
+
conversations: demand.conversations,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
function applyDemand(next) {
|
|
580
|
+
const before = heldLocators(acquiredLocators());
|
|
581
|
+
const { whole, conversations: conversed } = demand;
|
|
582
|
+
demand = next;
|
|
583
|
+
const after = heldLocators(acquiredLocators());
|
|
584
|
+
queueReadiness();
|
|
585
|
+
if (!conversed && next.conversations)
|
|
586
|
+
scheduleCurrency();
|
|
587
|
+
if (JSON.stringify(before) === JSON.stringify(after) &&
|
|
588
|
+
whole === next.whole &&
|
|
589
|
+
conversed === next.conversations)
|
|
590
|
+
return;
|
|
591
|
+
journal.record(`acquisition ${next.whole ? 'whole' : after.length}`);
|
|
592
|
+
if (options.adapter.roomStream === undefined)
|
|
593
|
+
void sync('unstreamed');
|
|
594
|
+
else if (socketOpen)
|
|
595
|
+
options.adapter.roomStream.reopen();
|
|
596
|
+
}
|
|
597
|
+
function acquire(next) {
|
|
598
|
+
if (closed)
|
|
599
|
+
return;
|
|
600
|
+
if (narrowing !== null)
|
|
601
|
+
clearTimeout(narrowing);
|
|
602
|
+
narrowing = null;
|
|
603
|
+
const widening = wider(next, demand);
|
|
604
|
+
if (!widening && !narrower(next, demand))
|
|
605
|
+
return;
|
|
606
|
+
if (widening || !roomsListed) {
|
|
607
|
+
applyDemand(next);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
// A narrower demand waits five seconds, so a tab that reloads returns before the stream reopens twice.
|
|
611
|
+
narrowing = setTimeout(() => {
|
|
612
|
+
narrowing = null;
|
|
613
|
+
applyDemand(next);
|
|
614
|
+
}, 5_000);
|
|
615
|
+
}
|
|
616
|
+
function requiredRooms(reach) {
|
|
617
|
+
if (reach === null)
|
|
618
|
+
return { listed: roomsListed, rooms: [...rooms.keys()].sort() };
|
|
619
|
+
const named = [...new Set(reach.rooms)].sort();
|
|
620
|
+
if (roomsListed)
|
|
621
|
+
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
622
|
+
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
623
|
+
}
|
|
624
|
+
function roomState(locator, search) {
|
|
625
|
+
if (sourceRefusals.has(locator))
|
|
626
|
+
return 'refused';
|
|
627
|
+
const listedRoom = discovered.get(locator);
|
|
628
|
+
if (roomsListed &&
|
|
629
|
+
(listedRoom === undefined ||
|
|
630
|
+
listedRoom.replica.admission !== 'admitted' ||
|
|
631
|
+
unreadable.has(locator)))
|
|
632
|
+
return 'unadmitted';
|
|
633
|
+
if (!replicas.has(locator))
|
|
634
|
+
return 'acquiring';
|
|
635
|
+
if (!prepared.has(locator))
|
|
636
|
+
return 'preparing';
|
|
637
|
+
if (search && !searchIndex.rooms().has(locator))
|
|
638
|
+
return 'indexing';
|
|
639
|
+
return 'complete';
|
|
640
|
+
}
|
|
641
|
+
function conversationCoverage(reach, search) {
|
|
642
|
+
const declared = reach === null || reach.conversations;
|
|
643
|
+
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
644
|
+
return 'undeclared';
|
|
645
|
+
if (!conversations.known() || !conversations.current())
|
|
646
|
+
return 'acquiring';
|
|
647
|
+
if (!search)
|
|
648
|
+
return 'complete';
|
|
649
|
+
const unindexed = conversations
|
|
650
|
+
.searchable()
|
|
651
|
+
.filter((one) => conversations.acquired(one) && !searchIndex.conversations().has(one));
|
|
652
|
+
if (unindexed.length === 0)
|
|
653
|
+
return 'complete';
|
|
654
|
+
index([], unindexed);
|
|
655
|
+
return 'indexing';
|
|
656
|
+
}
|
|
657
|
+
function connectionOf() {
|
|
658
|
+
if (socketOpen)
|
|
659
|
+
return 'connected';
|
|
660
|
+
if (status.providerUnreachable)
|
|
661
|
+
return 'unreachable';
|
|
662
|
+
return options.adapter.roomStream === undefined && status.lastCompleted !== null
|
|
663
|
+
? 'connected'
|
|
664
|
+
: 'connecting';
|
|
665
|
+
}
|
|
666
|
+
function refusedIn(locators) {
|
|
667
|
+
return locators.flatMap((room) => {
|
|
668
|
+
const held = sourceRefusals.get(room);
|
|
669
|
+
if (held === undefined)
|
|
670
|
+
return [];
|
|
671
|
+
const { file, location } = held.fields;
|
|
672
|
+
return [
|
|
673
|
+
{
|
|
674
|
+
room,
|
|
675
|
+
file: typeof file === 'string' ? file : '',
|
|
676
|
+
rule: held.code,
|
|
677
|
+
location: typeof location === 'string' ? location : null,
|
|
678
|
+
},
|
|
679
|
+
];
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
function readinessFor(port, reach) {
|
|
683
|
+
const required = requiredRooms(reach);
|
|
684
|
+
const search = port.search === 'required';
|
|
685
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room, search) }));
|
|
686
|
+
const building = (state) => coverage
|
|
687
|
+
.filter((room) => room.state === state && !unindexable.has(room.room))
|
|
688
|
+
.map(({ room }) => room);
|
|
689
|
+
const preparing = building('preparing');
|
|
690
|
+
if (preparing.length > 0)
|
|
691
|
+
prepare(preparing);
|
|
692
|
+
const indexing = building('indexing');
|
|
693
|
+
if (indexing.length > 0)
|
|
694
|
+
index(indexing, []);
|
|
695
|
+
return readinessOf({
|
|
696
|
+
code: port.code,
|
|
697
|
+
coverage: {
|
|
698
|
+
listed: required.listed,
|
|
699
|
+
rooms: coverage,
|
|
700
|
+
conversations: conversationCoverage(reach, search),
|
|
701
|
+
search: search ? 'required' : 'unused',
|
|
702
|
+
},
|
|
703
|
+
persistence,
|
|
704
|
+
connection: connectionOf(),
|
|
705
|
+
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
706
|
+
capacity: capacity !== null &&
|
|
707
|
+
coverage.some(({ room, state }) => paused.has(room) || !SETTLED_ROOMS.has(state))
|
|
708
|
+
? capacity
|
|
709
|
+
: port.capacity,
|
|
710
|
+
refused: refusedIn(required.rooms),
|
|
711
|
+
lost: storageLoss !== null,
|
|
712
|
+
established: targets[port.target]?.established === true,
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
function establish(name) {
|
|
716
|
+
targets = { ...targets, [name]: { established: true } };
|
|
717
|
+
queueReadiness();
|
|
718
|
+
void durably((local) => local.keepTargets(targets)).catch(() => {
|
|
719
|
+
journal.record(`target record failed ${name}`);
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
function deliverReadiness(watch) {
|
|
723
|
+
if (!readinessWatches.has(watch))
|
|
724
|
+
return;
|
|
725
|
+
const readiness = readinessFor(watch.port, watch.reach);
|
|
726
|
+
if (readiness.ready && targets[watch.port.target]?.established !== true) {
|
|
727
|
+
establish(watch.port.target);
|
|
728
|
+
}
|
|
729
|
+
const shown = JSON.stringify(readiness);
|
|
730
|
+
if (shown === watch.last)
|
|
731
|
+
return;
|
|
732
|
+
watch.last = shown;
|
|
733
|
+
watch.deliver(structuredClone(readiness));
|
|
734
|
+
}
|
|
735
|
+
function queueReadiness() {
|
|
736
|
+
if (readinessQueued || closed || readinessWatches.size === 0)
|
|
737
|
+
return;
|
|
738
|
+
readinessQueued = true;
|
|
739
|
+
queueMicrotask(() => {
|
|
740
|
+
readinessQueued = false;
|
|
741
|
+
if (closed)
|
|
742
|
+
return;
|
|
743
|
+
for (const watch of [...readinessWatches])
|
|
744
|
+
deliverReadiness(watch);
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
function watchReadiness(port, reach, deliver) {
|
|
748
|
+
const watch = { port, reach, deliver, last: null };
|
|
749
|
+
readinessWatches.add(watch);
|
|
750
|
+
void openLocal().then(() => deliverReadiness(watch), () => deliverReadiness(watch));
|
|
751
|
+
return () => {
|
|
752
|
+
readinessWatches.delete(watch);
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function layerBytes(layer) {
|
|
756
|
+
let bytes = 0;
|
|
757
|
+
for (const [path, text] of Object.entries(layer.documents))
|
|
758
|
+
bytes += (path.length + text.length) * 2;
|
|
759
|
+
return bytes;
|
|
760
|
+
}
|
|
761
|
+
function grownBy(next, previous) {
|
|
762
|
+
if (previous === undefined)
|
|
763
|
+
return layerBytes(next);
|
|
764
|
+
const documents = Object.fromEntries(Object.entries(next.documents).filter(([path, text]) => previous.documents[path] !== text));
|
|
765
|
+
return layerBytes({ ...next, documents });
|
|
766
|
+
}
|
|
767
|
+
function exceeded(error) {
|
|
768
|
+
return (typeof error === 'object' &&
|
|
769
|
+
error !== null &&
|
|
770
|
+
error.name === 'QuotaExceededError');
|
|
771
|
+
}
|
|
772
|
+
async function estimated() {
|
|
773
|
+
const estimate = options.shell.estimate === undefined ? null : await options.shell.estimate();
|
|
774
|
+
return estimate === null ? null : Math.max(0, estimate.quota - estimate.usage);
|
|
775
|
+
}
|
|
776
|
+
function pause(locator, cause) {
|
|
777
|
+
paused.add(locator);
|
|
778
|
+
capacity = cause;
|
|
779
|
+
journal.record(`capacity ${cause.cause} ${locator} ${cause.needed} ${cause.available ?? '-'}`);
|
|
780
|
+
queueReadiness();
|
|
781
|
+
if (rechecking !== null)
|
|
782
|
+
return;
|
|
783
|
+
// A paused preparation measures the browser's space again each minute, so space freed elsewhere resumes it without a retry.
|
|
784
|
+
rechecking = setInterval(() => {
|
|
785
|
+
void recheck();
|
|
786
|
+
}, 60_000);
|
|
787
|
+
}
|
|
788
|
+
async function recheck() {
|
|
789
|
+
const held = capacity;
|
|
790
|
+
if (held === null || closed)
|
|
791
|
+
return;
|
|
792
|
+
const available = await estimated().catch(() => null);
|
|
793
|
+
if (available === null || available < (held.needed ?? 0))
|
|
794
|
+
return;
|
|
795
|
+
if (held.cause === 'quota-exceeded' && (held.available === null || available <= held.available))
|
|
796
|
+
return;
|
|
797
|
+
resume();
|
|
798
|
+
}
|
|
799
|
+
function resume() {
|
|
800
|
+
if (rechecking !== null)
|
|
801
|
+
clearInterval(rechecking);
|
|
802
|
+
rechecking = null;
|
|
803
|
+
if (paused.size === 0 && capacity === null)
|
|
804
|
+
return;
|
|
805
|
+
paused.clear();
|
|
806
|
+
capacity = null;
|
|
807
|
+
journal.record('capacity resumed');
|
|
808
|
+
queueReadiness();
|
|
809
|
+
if (!closed && authorization !== undefined)
|
|
810
|
+
void sync('unstreamed');
|
|
811
|
+
}
|
|
812
|
+
async function reclaimed(local) {
|
|
813
|
+
const unreferenced = [...transfers.keys()].filter((locator) => {
|
|
814
|
+
const room = discovered.get(locator);
|
|
815
|
+
return room === undefined || !acquires(room);
|
|
816
|
+
});
|
|
817
|
+
for (const locator of unreferenced) {
|
|
818
|
+
await local.abandon(locator);
|
|
819
|
+
transfers.delete(locator);
|
|
820
|
+
}
|
|
821
|
+
if (unreferenced.length > 0)
|
|
822
|
+
journal.record(`capacity reclaimed ${unreferenced.length}`);
|
|
823
|
+
return unreferenced.length > 0;
|
|
824
|
+
}
|
|
825
|
+
async function admitted(local, locator, needed) {
|
|
826
|
+
if (paused.has(locator))
|
|
827
|
+
return false;
|
|
828
|
+
let available = await estimated().catch(() => null);
|
|
829
|
+
if (available === null || available >= needed)
|
|
830
|
+
return true;
|
|
831
|
+
if (await reclaimed(local))
|
|
832
|
+
available = await estimated().catch(() => null);
|
|
833
|
+
if (available === null || available >= needed)
|
|
834
|
+
return true;
|
|
835
|
+
pause(locator, { cause: 'insufficient-space', needed, available });
|
|
836
|
+
return false;
|
|
837
|
+
}
|
|
838
|
+
async function exceededOn(locator, bytes) {
|
|
839
|
+
const available = await estimated().catch(() => null);
|
|
840
|
+
pause(locator, { cause: 'quota-exceeded', needed: bytes, available });
|
|
841
|
+
}
|
|
842
|
+
function seedRoomHeld(room, version) {
|
|
843
|
+
return prepared.has(room) && reachedBy(room, version);
|
|
844
|
+
}
|
|
845
|
+
function seedHeld(at) {
|
|
846
|
+
return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
|
|
847
|
+
}
|
|
848
|
+
function seedApplies(plan) {
|
|
849
|
+
const seed = seeds.get(plan.key);
|
|
850
|
+
if (seed === undefined)
|
|
372
851
|
return null;
|
|
373
|
-
|
|
852
|
+
if (!seedHeld(seed.at))
|
|
853
|
+
return seed;
|
|
854
|
+
seeds.delete(plan.key);
|
|
855
|
+
return null;
|
|
374
856
|
}
|
|
375
|
-
function
|
|
376
|
-
|
|
377
|
-
|
|
857
|
+
function seedBehind(plan, cover) {
|
|
858
|
+
const seed = seedApplies(plan);
|
|
859
|
+
if (seed === null || 'pending' in cover)
|
|
860
|
+
return cover;
|
|
861
|
+
return {
|
|
862
|
+
pending: Object.entries(seed.at)
|
|
863
|
+
.filter(([room, version]) => !seedRoomHeld(room, version))
|
|
864
|
+
.map(([room]) => ({ room, awaits: 'acquisition' })),
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
function seed(entry) {
|
|
868
|
+
if (seedHeld(entry.at))
|
|
869
|
+
return;
|
|
870
|
+
for (const { query, rows } of entry.queries) {
|
|
871
|
+
const key = queryKey(query);
|
|
872
|
+
seeds.set(key, { rows, at: entry.at });
|
|
873
|
+
for (const shown of seedWatchers.get(key) ?? [])
|
|
874
|
+
shown();
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
async function rowsQuery(plan) {
|
|
878
|
+
const before = covered(plan);
|
|
879
|
+
awaitPreparation(before);
|
|
880
|
+
const seeded = seedApplies(plan);
|
|
881
|
+
if (seeded !== null)
|
|
882
|
+
return seededResult(seeded).result;
|
|
883
|
+
if ('pending' in before)
|
|
884
|
+
return before;
|
|
885
|
+
const { rows, at: cover } = await readSide.query(plan, () => covered(plan));
|
|
886
|
+
awaitPreparation(cover);
|
|
887
|
+
if ('pending' in cover)
|
|
888
|
+
return cover;
|
|
889
|
+
return { ...rows, coverage: cover.coverage };
|
|
890
|
+
}
|
|
891
|
+
async function indexConversation(conversation) {
|
|
892
|
+
searchIndex.indexMessages(conversation, [], false);
|
|
893
|
+
let after = null;
|
|
894
|
+
for (;;) {
|
|
895
|
+
const batch = await durably((local) => local.messagesAfter(conversation, after));
|
|
896
|
+
if (closed)
|
|
897
|
+
return;
|
|
898
|
+
searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
|
|
899
|
+
if (batch.length === 0)
|
|
900
|
+
return;
|
|
901
|
+
after = batch[batch.length - 1].identity;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
function indexNext() {
|
|
905
|
+
const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator) && !unindexable.has(locator));
|
|
906
|
+
if (room !== undefined) {
|
|
907
|
+
roomsToIndex.delete(room);
|
|
908
|
+
return () => prepared.has(room)
|
|
909
|
+
? searchIndex.indexRoom(room).catch((error) => unbuilt(room, error))
|
|
910
|
+
: undefined;
|
|
911
|
+
}
|
|
912
|
+
const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
|
|
913
|
+
if (conversation === undefined)
|
|
914
|
+
return null;
|
|
915
|
+
conversationsToIndex.delete(conversation);
|
|
916
|
+
return () => indexConversation(conversation);
|
|
917
|
+
}
|
|
918
|
+
function index(rooms, named) {
|
|
919
|
+
for (const room of rooms)
|
|
920
|
+
roomsToIndex.add(room);
|
|
921
|
+
for (const conversation of named)
|
|
922
|
+
conversationsToIndex.add(conversation);
|
|
923
|
+
if (indexing)
|
|
924
|
+
return;
|
|
925
|
+
indexing = true;
|
|
926
|
+
void (async () => {
|
|
927
|
+
try {
|
|
928
|
+
for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
|
|
929
|
+
await yielded();
|
|
930
|
+
await unit();
|
|
931
|
+
queueReadiness();
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
finally {
|
|
935
|
+
indexing = false;
|
|
936
|
+
}
|
|
937
|
+
})().catch(() => { });
|
|
378
938
|
}
|
|
379
939
|
function publishChangeSet(set) {
|
|
380
940
|
applyChangeSet(set);
|
|
@@ -391,34 +951,7 @@ export function composeRuntime(options) {
|
|
|
391
951
|
emitPresentation(app);
|
|
392
952
|
conversations.changed(set);
|
|
393
953
|
}
|
|
394
|
-
function stopTimer() {
|
|
395
|
-
if (resetTimer === undefined)
|
|
396
|
-
return;
|
|
397
|
-
clearTimeout(resetTimer);
|
|
398
|
-
resetTimer = undefined;
|
|
399
|
-
}
|
|
400
|
-
function armTimer() {
|
|
401
|
-
stopTimer();
|
|
402
|
-
if (closed || stopping)
|
|
403
|
-
return;
|
|
404
|
-
let earliest = null;
|
|
405
|
-
for (const { submission } of pendings.values()) {
|
|
406
|
-
if (submission.state !== 'reset')
|
|
407
|
-
continue;
|
|
408
|
-
const at = Date.parse(submission.reset);
|
|
409
|
-
if (earliest === null || at < earliest)
|
|
410
|
-
earliest = at;
|
|
411
|
-
}
|
|
412
|
-
if (earliest === null)
|
|
413
|
-
return;
|
|
414
|
-
// A Save House held back goes again only once the reset its answer named has passed.
|
|
415
|
-
resetTimer = setTimeout(() => {
|
|
416
|
-
resetTimer = undefined;
|
|
417
|
-
submitAll();
|
|
418
|
-
}, Math.max(0, earliest - Date.now()));
|
|
419
|
-
}
|
|
420
954
|
async function refuse(reason, provider = null) {
|
|
421
|
-
stopTimer();
|
|
422
955
|
discoveryPaused = true;
|
|
423
956
|
try {
|
|
424
957
|
if (reason === 'revoked-authorization') {
|
|
@@ -497,20 +1030,27 @@ export function composeRuntime(options) {
|
|
|
497
1030
|
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
498
1031
|
});
|
|
499
1032
|
}
|
|
500
|
-
async function applyDiscovery(found, started) {
|
|
1033
|
+
async function applyDiscovery(found, started, covered) {
|
|
501
1034
|
for (const room of found) {
|
|
502
1035
|
const fence = fences.get(room.locator);
|
|
503
1036
|
if (fence !== undefined && fence.erased && fence.at < started)
|
|
504
1037
|
fences.delete(room.locator);
|
|
505
1038
|
}
|
|
1039
|
+
await loseRooms(found
|
|
1040
|
+
.filter((room) => room.replica.admission !== 'admitted' && heldLocally(room.locator))
|
|
1041
|
+
.map((room) => room.locator));
|
|
506
1042
|
const listed = found.filter((room) => !fences.has(room.locator));
|
|
507
1043
|
const listedLocators = new Set(listed.map((room) => room.locator));
|
|
508
1044
|
for (const locator of listedLocators)
|
|
509
1045
|
scopedRefusals.delete(locator);
|
|
510
|
-
|
|
1046
|
+
if (covered === undefined)
|
|
1047
|
+
discovered.clear();
|
|
1048
|
+
else
|
|
1049
|
+
for (const locator of covered)
|
|
1050
|
+
discovered.delete(locator);
|
|
511
1051
|
for (const room of listed)
|
|
512
1052
|
discovered.set(room.locator, room);
|
|
513
|
-
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
|
|
1053
|
+
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator))).filter((locator) => covered === undefined || covered.includes(locator));
|
|
514
1054
|
if (options.adapter.exhaustiveDiscovery === true)
|
|
515
1055
|
await loseRooms(unlisted);
|
|
516
1056
|
else
|
|
@@ -527,6 +1067,12 @@ export function composeRuntime(options) {
|
|
|
527
1067
|
emitRooms();
|
|
528
1068
|
return listed;
|
|
529
1069
|
}
|
|
1070
|
+
function heldLocally(locator) {
|
|
1071
|
+
return (replicas.has(locator) ||
|
|
1072
|
+
roomDrafts.has(locator) ||
|
|
1073
|
+
transfers.has(locator) ||
|
|
1074
|
+
queuesWithoutReplica.has(locator));
|
|
1075
|
+
}
|
|
530
1076
|
function unlistedHeld(listed) {
|
|
531
1077
|
return [
|
|
532
1078
|
...new Set([
|
|
@@ -563,10 +1109,14 @@ export function composeRuntime(options) {
|
|
|
563
1109
|
rooms.delete(locator);
|
|
564
1110
|
roomDrafts.delete(locator);
|
|
565
1111
|
transfers.delete(locator);
|
|
1112
|
+
catching.delete(locator);
|
|
566
1113
|
discovered.delete(locator);
|
|
567
1114
|
announced.delete(locator);
|
|
568
1115
|
unreadable.delete(locator);
|
|
569
1116
|
stale.delete(locator);
|
|
1117
|
+
repairable.delete(locator);
|
|
1118
|
+
unindexable.delete(locator);
|
|
1119
|
+
paused.delete(locator);
|
|
570
1120
|
scopedRefusals.delete(locator);
|
|
571
1121
|
sourceRefusals.delete(locator);
|
|
572
1122
|
declarationRefusals.delete(locator);
|
|
@@ -582,7 +1132,9 @@ export function composeRuntime(options) {
|
|
|
582
1132
|
return fresh;
|
|
583
1133
|
publishChangeSet(mergeChangeSets(removed));
|
|
584
1134
|
for (const locator of fresh)
|
|
585
|
-
|
|
1135
|
+
prepared.delete(locator);
|
|
1136
|
+
searchIndex.forget(fresh);
|
|
1137
|
+
readSide.recover();
|
|
586
1138
|
moveTo({ ...status, ...attention() });
|
|
587
1139
|
return fresh;
|
|
588
1140
|
}
|
|
@@ -618,7 +1170,7 @@ export function composeRuntime(options) {
|
|
|
618
1170
|
return model;
|
|
619
1171
|
}
|
|
620
1172
|
async function hydrate(opened) {
|
|
621
|
-
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged,] = await Promise.all([
|
|
1173
|
+
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged, heldTargets,] = await Promise.all([
|
|
622
1174
|
opened.all(),
|
|
623
1175
|
opened.queues(),
|
|
624
1176
|
opened.roomDrafts(),
|
|
@@ -630,7 +1182,9 @@ export function composeRuntime(options) {
|
|
|
630
1182
|
opened.capabilities(),
|
|
631
1183
|
opened.member(),
|
|
632
1184
|
opened.transfers(),
|
|
1185
|
+
opened.targets(),
|
|
633
1186
|
]);
|
|
1187
|
+
targets = heldTargets;
|
|
634
1188
|
transfers.clear();
|
|
635
1189
|
for (const record of staged)
|
|
636
1190
|
transfers.set(record.locator, record);
|
|
@@ -692,6 +1246,7 @@ export function composeRuntime(options) {
|
|
|
692
1246
|
async function readState(opened) {
|
|
693
1247
|
const { lastCompleted, locators, read } = await hydrate(opened);
|
|
694
1248
|
applyChangeSet(read);
|
|
1249
|
+
prepare();
|
|
695
1250
|
if (replicas.size > 0)
|
|
696
1251
|
emitRooms();
|
|
697
1252
|
if (member !== null)
|
|
@@ -753,8 +1308,9 @@ export function composeRuntime(options) {
|
|
|
753
1308
|
}
|
|
754
1309
|
async function settle(local, locator, before, confirmed, queue, settling = {}) {
|
|
755
1310
|
const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
756
|
-
const withdrawn =
|
|
757
|
-
const
|
|
1311
|
+
const { withdrawn, kept } = standing(confirmed, queue, withdrawable, localSaves);
|
|
1312
|
+
const founded = kept.filter((batch) => !queue.includes(batch));
|
|
1313
|
+
const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
|
|
758
1314
|
const unqueued = withdrawn.map((batch) => ({
|
|
759
1315
|
batch,
|
|
760
1316
|
returned: localSaves || landed(batch, confirmed) ? null : returnedBy('withdrawn'),
|
|
@@ -778,9 +1334,10 @@ export function composeRuntime(options) {
|
|
|
778
1334
|
});
|
|
779
1335
|
}
|
|
780
1336
|
}
|
|
781
|
-
await local.keepAuthored({ locator, queue: kept, roomDrafts: [...written.values()] });
|
|
1337
|
+
await local.keepAuthored({ locator, queue: kept, founded, roomDrafts: [...written.values()] });
|
|
1338
|
+
const staying = new Set(kept.map((batch) => batch.id));
|
|
782
1339
|
for (const batch of previousQueue) {
|
|
783
|
-
if (!
|
|
1340
|
+
if (!staying.has(batch.id))
|
|
784
1341
|
pendings.delete(batch.id);
|
|
785
1342
|
}
|
|
786
1343
|
for (const record of written.values())
|
|
@@ -801,23 +1358,25 @@ export function composeRuntime(options) {
|
|
|
801
1358
|
else
|
|
802
1359
|
queuesWithoutReplica.delete(locator);
|
|
803
1360
|
publishChangeSet(changeSet(locator, before ?? null, held, {
|
|
804
|
-
queue:
|
|
1361
|
+
queue: queueChanged,
|
|
805
1362
|
drafts: [...written.keys()],
|
|
806
1363
|
}));
|
|
807
1364
|
moveTo({ ...status, ...attention() });
|
|
808
1365
|
throw error;
|
|
809
1366
|
}
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
const
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
1367
|
+
return () => {
|
|
1368
|
+
queuesWithoutReplica.delete(locator);
|
|
1369
|
+
for (const { batch, returned } of unqueued) {
|
|
1370
|
+
const outcome = localSaves ? 'dropped' : returned === null ? 'landed' : 'withdrawn';
|
|
1371
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
1372
|
+
}
|
|
1373
|
+
replicas.set(locator, replicaOf(confirmed, kept, before));
|
|
1374
|
+
return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
|
|
1375
|
+
room: settling.room,
|
|
1376
|
+
queue: queueChanged,
|
|
1377
|
+
drafts: [...written.keys()],
|
|
1378
|
+
});
|
|
1379
|
+
};
|
|
821
1380
|
}
|
|
822
1381
|
function adoptModels(locator, record) {
|
|
823
1382
|
for (const [attached, model] of [
|
|
@@ -853,8 +1412,13 @@ export function composeRuntime(options) {
|
|
|
853
1412
|
function publish(local, room, confirmed, landing, live) {
|
|
854
1413
|
const { base, stored } = landing;
|
|
855
1414
|
return exclusive(async () => {
|
|
856
|
-
if (
|
|
1415
|
+
if (stored !== undefined) {
|
|
1416
|
+
await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
|
|
1417
|
+
}
|
|
1418
|
+
if (!live()) {
|
|
1419
|
+
caughtUpTo(room.locator);
|
|
857
1420
|
return true;
|
|
1421
|
+
}
|
|
858
1422
|
const prior = replicas.get(room.locator);
|
|
859
1423
|
if (base !== undefined &&
|
|
860
1424
|
(prior?.confirmed.record.roomVersion !== base.record.roomVersion ||
|
|
@@ -868,30 +1432,51 @@ export function composeRuntime(options) {
|
|
|
868
1432
|
const accepted = pendings.get(batch.id)?.accepted;
|
|
869
1433
|
return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
|
|
870
1434
|
});
|
|
871
|
-
|
|
872
|
-
|
|
1435
|
+
let settled;
|
|
1436
|
+
try {
|
|
1437
|
+
settled = await settle(local, room.locator, prior, confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true, ...(stored === undefined ? {} : { stored }) });
|
|
1438
|
+
}
|
|
1439
|
+
catch (error) {
|
|
1440
|
+
if (!exceeded(error))
|
|
1441
|
+
throw error;
|
|
1442
|
+
await exceededOn(room.locator, grownBy(confirmed.record, prior?.confirmed.record));
|
|
1443
|
+
caughtUpTo(room.locator);
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
const set = settled();
|
|
1447
|
+
if (stored !== undefined) {
|
|
873
1448
|
transfers.delete(room.locator);
|
|
1449
|
+
catching.delete(room.locator);
|
|
1450
|
+
}
|
|
874
1451
|
for (const batch of accepted)
|
|
875
1452
|
pendings.delete(batch.id);
|
|
876
1453
|
stale.delete(room.locator);
|
|
1454
|
+
unindexable.delete(room.locator);
|
|
877
1455
|
if (unreadable.delete(room.locator))
|
|
878
1456
|
scopedRefusals.delete(room.locator);
|
|
879
1457
|
sourceRefusals.delete(room.locator);
|
|
880
1458
|
declarationRefusals.delete(room.locator);
|
|
881
1459
|
list(room, true);
|
|
1460
|
+
if (prior === undefined)
|
|
1461
|
+
prepared.add(room.locator);
|
|
882
1462
|
publishChangeSet(set);
|
|
883
1463
|
moveTo({ ...status, ...attention() });
|
|
884
1464
|
options.adapter.roomStream?.published(room.locator, roomVersion);
|
|
885
1465
|
return true;
|
|
886
1466
|
});
|
|
887
1467
|
}
|
|
888
|
-
async function
|
|
1468
|
+
async function savedRoom(locator) {
|
|
889
1469
|
const local = await openLocal();
|
|
1470
|
+
await referenced(locator);
|
|
890
1471
|
const replica = replicas.get(locator);
|
|
891
1472
|
const room = rooms.get(locator);
|
|
892
1473
|
if (replica === undefined || room === undefined) {
|
|
893
1474
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
894
1475
|
}
|
|
1476
|
+
return { local, replica, room };
|
|
1477
|
+
}
|
|
1478
|
+
async function saveRoomDrafts(locator, named, writtenBy) {
|
|
1479
|
+
const target = await savedRoom(locator);
|
|
895
1480
|
const saved = [];
|
|
896
1481
|
for (const { path, revision } of named) {
|
|
897
1482
|
const record = roomDrafts.get(locator)?.get(path);
|
|
@@ -901,6 +1486,38 @@ export function composeRuntime(options) {
|
|
|
901
1486
|
if (!saved.some((draft) => draft.path === path))
|
|
902
1487
|
saved.push({ path, parts: record.parts });
|
|
903
1488
|
}
|
|
1489
|
+
return committed(locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
|
|
1490
|
+
}
|
|
1491
|
+
async function saveParts(locator, named, writtenBy) {
|
|
1492
|
+
const read = named.map(({ path, parts }) => ({ path, parts: readParts(parts) }));
|
|
1493
|
+
if (read.some(({ path }, at) => path === '' ||
|
|
1494
|
+
normalize(path) !== path ||
|
|
1495
|
+
read.findIndex((one) => one.path === path) !== at)) {
|
|
1496
|
+
throw runtimeError('room-draft-malformed');
|
|
1497
|
+
}
|
|
1498
|
+
const target = await savedRoom(locator);
|
|
1499
|
+
const saved = [];
|
|
1500
|
+
for (const { path, parts } of read) {
|
|
1501
|
+
const file = parts.file === undefined
|
|
1502
|
+
? undefined
|
|
1503
|
+
: await heldFile(parts.file.blob, binaryBase(target.replica.visible, path));
|
|
1504
|
+
saved.push({ path, parts: held(undefined, parts, file) });
|
|
1505
|
+
}
|
|
1506
|
+
return committed(locator, target, saved, new Set(), writtenBy);
|
|
1507
|
+
}
|
|
1508
|
+
function carriedDrafts(locator, saved, consumed) {
|
|
1509
|
+
const holding = roomDrafts.get(locator);
|
|
1510
|
+
return new Map(saved.flatMap(({ path, parts }) => {
|
|
1511
|
+
const record = holding?.get(path);
|
|
1512
|
+
if (parts.move === undefined || record === undefined || consumed.has(path))
|
|
1513
|
+
return [];
|
|
1514
|
+
const to = moveTarget(parts.move);
|
|
1515
|
+
if (to === path || holding?.has(to) === true)
|
|
1516
|
+
return [];
|
|
1517
|
+
return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
|
|
1518
|
+
}));
|
|
1519
|
+
}
|
|
1520
|
+
async function committed(locator, { local, replica, room }, saved, consumed, writtenBy) {
|
|
904
1521
|
const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
|
|
905
1522
|
if (changed.length > 0)
|
|
906
1523
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
@@ -933,9 +1550,17 @@ export function composeRuntime(options) {
|
|
|
933
1550
|
visible: replica.visible,
|
|
934
1551
|
changes,
|
|
935
1552
|
bases: parts.flatMap(({ bases }) => bases),
|
|
1553
|
+
queued: queuedDocuments(replica.queue),
|
|
936
1554
|
writtenBy,
|
|
1555
|
+
clean: {
|
|
1556
|
+
text: replica.confirmed.record.documents,
|
|
1557
|
+
binaries: replica.confirmed.record.binaries,
|
|
1558
|
+
composition: compositionOf(replica.confirmed.record),
|
|
1559
|
+
},
|
|
937
1560
|
};
|
|
938
|
-
const checked = withdrawing
|
|
1561
|
+
const checked = withdrawing
|
|
1562
|
+
? buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
|
|
1563
|
+
: null;
|
|
939
1564
|
if (checked?.kind === 'refused')
|
|
940
1565
|
return { outcome: 'refused', ...checked.refusal };
|
|
941
1566
|
const owners = [...requeue.owners].flatMap((origin) => {
|
|
@@ -958,6 +1583,7 @@ export function composeRuntime(options) {
|
|
|
958
1583
|
...input,
|
|
959
1584
|
visible: replicaOf(replica.confirmed, requeue.queue, replica).visible,
|
|
960
1585
|
changes: requeue.changes,
|
|
1586
|
+
queued: queuedDocuments(requeue.queue),
|
|
961
1587
|
});
|
|
962
1588
|
if (built?.kind === 'refused')
|
|
963
1589
|
return { outcome: 'refused', ...built.refusal };
|
|
@@ -992,13 +1618,16 @@ export function composeRuntime(options) {
|
|
|
992
1618
|
if (limited !== null)
|
|
993
1619
|
return { outcome: 'refused', ...limited };
|
|
994
1620
|
}
|
|
995
|
-
const
|
|
1621
|
+
const carried = carriedDrafts(locator, saved, consumed);
|
|
1622
|
+
const left = [...consumed, ...carried.keys()];
|
|
1623
|
+
const arrived = [...carried.values()];
|
|
996
1624
|
try {
|
|
997
1625
|
await local.keepAuthored({
|
|
998
1626
|
locator,
|
|
999
1627
|
queue: batches,
|
|
1000
1628
|
saved: drafted,
|
|
1001
|
-
consumedRoomDrafts:
|
|
1629
|
+
consumedRoomDrafts: left,
|
|
1630
|
+
roomDrafts: arrived,
|
|
1002
1631
|
});
|
|
1003
1632
|
}
|
|
1004
1633
|
catch {
|
|
@@ -1013,12 +1642,14 @@ export function composeRuntime(options) {
|
|
|
1013
1642
|
}
|
|
1014
1643
|
if (!localSaves)
|
|
1015
1644
|
recorded = false;
|
|
1016
|
-
for (const path of
|
|
1645
|
+
for (const path of left)
|
|
1017
1646
|
forgetRoomDraft(locator, path);
|
|
1647
|
+
for (const record of arrived)
|
|
1648
|
+
rememberRoomDraft(record);
|
|
1018
1649
|
replicas.set(locator, replicaOf(replica.confirmed, batches, replica));
|
|
1019
1650
|
publishChangeSet(changeSet(locator, replica, replicas.get(locator) ?? null, {
|
|
1020
1651
|
queue: true,
|
|
1021
|
-
drafts:
|
|
1652
|
+
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
1022
1653
|
}));
|
|
1023
1654
|
moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
|
|
1024
1655
|
submitLater(locator);
|
|
@@ -1109,7 +1740,7 @@ export function composeRuntime(options) {
|
|
|
1109
1740
|
const scope = options.adapter.referrerScope(request.referrer);
|
|
1110
1741
|
return scope?.kind === 'room' ? scope.locator : null;
|
|
1111
1742
|
}
|
|
1112
|
-
async function
|
|
1743
|
+
async function transferBinary(request, transfer, answer) {
|
|
1113
1744
|
if (closed || refusal !== null)
|
|
1114
1745
|
return UNCERTAIN;
|
|
1115
1746
|
try {
|
|
@@ -1125,40 +1756,41 @@ export function composeRuntime(options) {
|
|
|
1125
1756
|
? `transfer binary ${request.locator} ${request.path}`
|
|
1126
1757
|
: `transfer binary ${request.fileRef}`);
|
|
1127
1758
|
try {
|
|
1128
|
-
const result = await
|
|
1129
|
-
|
|
1759
|
+
const result = await transfer(authorization);
|
|
1760
|
+
const settled = result;
|
|
1761
|
+
journal.record(`outcome binary ${settled.kind}`);
|
|
1130
1762
|
const locator = binaryRoom(request);
|
|
1131
1763
|
if (locator !== null && fencedSince(locator, since))
|
|
1132
1764
|
return failure({ kind: 'room-not-readable', locator });
|
|
1133
|
-
if (
|
|
1134
|
-
const room = replicas.get(
|
|
1765
|
+
if (settled.kind === 'scoped-refusal') {
|
|
1766
|
+
const room = replicas.get(settled.locator)?.confirmed.record;
|
|
1135
1767
|
if (room !== undefined)
|
|
1136
|
-
await scoped(
|
|
1768
|
+
await scoped(settled, room, await openLocal());
|
|
1137
1769
|
}
|
|
1138
|
-
if (
|
|
1770
|
+
if (settled.kind === 'revoked-authorization')
|
|
1139
1771
|
await refuse('revoked-authorization');
|
|
1140
|
-
return
|
|
1772
|
+
return answer(result);
|
|
1141
1773
|
}
|
|
1142
1774
|
catch {
|
|
1143
1775
|
journal.record('outcome binary transport-uncertainty');
|
|
1144
1776
|
return UNCERTAIN;
|
|
1145
1777
|
}
|
|
1146
1778
|
}
|
|
1147
|
-
|
|
1148
|
-
|
|
1779
|
+
function readBinary(request) {
|
|
1780
|
+
return transferBinary(request, (held) => options.adapter.binary(held, request), binaryAnswer);
|
|
1781
|
+
}
|
|
1782
|
+
function binaryPath(locator, path, revision) {
|
|
1149
1783
|
if (revision !== undefined)
|
|
1150
|
-
return
|
|
1784
|
+
return { kind: 'path', locator, path, revision };
|
|
1151
1785
|
const visible = replicas.get(locator)?.visible;
|
|
1152
1786
|
const held = visible?.binaries[path];
|
|
1153
1787
|
if (visible === undefined || held === undefined)
|
|
1154
|
-
return
|
|
1155
|
-
if (held.state === 'pending')
|
|
1156
|
-
|
|
1157
|
-
return blob === null ? absentBinary(path) : { kind: 'binary', blob };
|
|
1158
|
-
}
|
|
1788
|
+
return null;
|
|
1789
|
+
if (held.state === 'pending')
|
|
1790
|
+
return { kind: 'pending', localId: held.localId };
|
|
1159
1791
|
const record = binaryRecord(path, held, visible.revisions[path]);
|
|
1160
1792
|
if (record.state !== 'accepted')
|
|
1161
|
-
return
|
|
1793
|
+
return null;
|
|
1162
1794
|
const replica = replicas.get(locator);
|
|
1163
1795
|
const queued = (replica?.queue ?? []).flatMap((batch) => batch.changes);
|
|
1164
1796
|
const holds = (at) => {
|
|
@@ -1173,13 +1805,31 @@ export function composeRuntime(options) {
|
|
|
1173
1805
|
if (change.kind === 'move' && change.to === confirmed)
|
|
1174
1806
|
confirmed = change.path;
|
|
1175
1807
|
}
|
|
1176
|
-
return
|
|
1808
|
+
return {
|
|
1177
1809
|
kind: 'path',
|
|
1178
1810
|
locator,
|
|
1179
1811
|
path: confirmed,
|
|
1180
1812
|
revision: record.revision,
|
|
1181
1813
|
fileRef: record.fileRef,
|
|
1182
|
-
}
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
async function readBinaryPath(locator, path, revision) {
|
|
1817
|
+
await ready;
|
|
1818
|
+
const asked = binaryPath(locator, path, revision);
|
|
1819
|
+
if (asked === null)
|
|
1820
|
+
return absentBinary(path);
|
|
1821
|
+
if (asked.kind === 'pending') {
|
|
1822
|
+
const blob = queuedBlob(locator, path, asked.localId);
|
|
1823
|
+
return blob === null ? absentBinary(path) : { kind: 'binary', blob };
|
|
1824
|
+
}
|
|
1825
|
+
return readBinary(asked);
|
|
1826
|
+
}
|
|
1827
|
+
async function linkBinaryPath(locator, path, revision) {
|
|
1828
|
+
await ready;
|
|
1829
|
+
const asked = binaryPath(locator, path, revision);
|
|
1830
|
+
if (asked === null || asked.kind === 'pending')
|
|
1831
|
+
return absentBinary(path);
|
|
1832
|
+
return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
|
|
1183
1833
|
}
|
|
1184
1834
|
async function readBinaryReference(reference) {
|
|
1185
1835
|
await ready;
|
|
@@ -1205,7 +1855,7 @@ export function composeRuntime(options) {
|
|
|
1205
1855
|
}
|
|
1206
1856
|
return undefined;
|
|
1207
1857
|
}
|
|
1208
|
-
async function invokeAction(name, input) {
|
|
1858
|
+
async function invokeAction(name, input, dispatching) {
|
|
1209
1859
|
await ready;
|
|
1210
1860
|
if (closed || refusal !== null)
|
|
1211
1861
|
return UNCERTAIN;
|
|
@@ -1219,6 +1869,7 @@ export function composeRuntime(options) {
|
|
|
1219
1869
|
}
|
|
1220
1870
|
if (closed || authorization === undefined || refusal !== null)
|
|
1221
1871
|
return UNCERTAIN;
|
|
1872
|
+
await dispatching?.();
|
|
1222
1873
|
journal.record(`transfer action ${name}`);
|
|
1223
1874
|
try {
|
|
1224
1875
|
const result = await options.adapter.action(authorization, { name, input });
|
|
@@ -1239,6 +1890,60 @@ export function composeRuntime(options) {
|
|
|
1239
1890
|
return UNCERTAIN;
|
|
1240
1891
|
}
|
|
1241
1892
|
}
|
|
1893
|
+
function unsettled(result) {
|
|
1894
|
+
return result.kind !== 'action' && result.retryable;
|
|
1895
|
+
}
|
|
1896
|
+
async function recoveredAction(name, input) {
|
|
1897
|
+
const operation = operationOf(input);
|
|
1898
|
+
if (operation === null)
|
|
1899
|
+
return invokeAction(name, input);
|
|
1900
|
+
const kept = async () => (await openLocal()).keepCall({ operation, name, input });
|
|
1901
|
+
calling.add(operation);
|
|
1902
|
+
try {
|
|
1903
|
+
let result = await invokeAction(name, input, kept);
|
|
1904
|
+
if (unsettled(result)) {
|
|
1905
|
+
await recovery.attempt(`action ${operation}`, async () => {
|
|
1906
|
+
if (closed || refusal !== null)
|
|
1907
|
+
return 'done';
|
|
1908
|
+
if (!socketOpen)
|
|
1909
|
+
return 'again';
|
|
1910
|
+
result = await invokeAction(name, input, kept);
|
|
1911
|
+
return unsettled(result) ? 'again' : 'done';
|
|
1912
|
+
}, { again: true });
|
|
1913
|
+
}
|
|
1914
|
+
if (!unsettled(result))
|
|
1915
|
+
await (await openLocal()).dropCall(operation);
|
|
1916
|
+
return result;
|
|
1917
|
+
}
|
|
1918
|
+
finally {
|
|
1919
|
+
calling.delete(operation);
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
async function resendCalls(signal) {
|
|
1923
|
+
if (!socketOpen || closed || stopping)
|
|
1924
|
+
return 'done';
|
|
1925
|
+
const local = await openLocal();
|
|
1926
|
+
for (const held of await local.calls()) {
|
|
1927
|
+
if (calling.has(held.operation))
|
|
1928
|
+
continue;
|
|
1929
|
+
if (closed || signal.aborted)
|
|
1930
|
+
return 'done';
|
|
1931
|
+
if (pastHorizon(held.operation)) {
|
|
1932
|
+
await local.dropCall(held.operation);
|
|
1933
|
+
continue;
|
|
1934
|
+
}
|
|
1935
|
+
calling.add(held.operation);
|
|
1936
|
+
try {
|
|
1937
|
+
if (unsettled(await invokeAction(held.name, held.input)))
|
|
1938
|
+
return 'again';
|
|
1939
|
+
await local.dropCall(held.operation);
|
|
1940
|
+
}
|
|
1941
|
+
finally {
|
|
1942
|
+
calling.delete(held.operation);
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
return 'done';
|
|
1946
|
+
}
|
|
1242
1947
|
function continuing() {
|
|
1243
1948
|
return !closed && !discoveryPaused;
|
|
1244
1949
|
}
|
|
@@ -1256,8 +1961,9 @@ export function composeRuntime(options) {
|
|
|
1256
1961
|
async function pullRefresh(authorized, confirmed, live) {
|
|
1257
1962
|
if (!live())
|
|
1258
1963
|
return unconfirmed;
|
|
1964
|
+
const turn = generation;
|
|
1259
1965
|
try {
|
|
1260
|
-
const result = await refreshRoom(
|
|
1966
|
+
const result = await refreshRoom(reads, authorized, confirmed, (models, held) => (live() ? remodel(confirmed.record.locator, models, held) : held), live);
|
|
1261
1967
|
const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
|
|
1262
1968
|
journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
|
|
1263
1969
|
return result;
|
|
@@ -1266,6 +1972,9 @@ export function composeRuntime(options) {
|
|
|
1266
1972
|
journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
|
|
1267
1973
|
throw error;
|
|
1268
1974
|
}
|
|
1975
|
+
finally {
|
|
1976
|
+
reads.finished(confirmed.record.locator, turn);
|
|
1977
|
+
}
|
|
1269
1978
|
}
|
|
1270
1979
|
function staging(local, live) {
|
|
1271
1980
|
return {
|
|
@@ -1274,8 +1983,20 @@ export function composeRuntime(options) {
|
|
|
1274
1983
|
stage: (record, page) => exclusive(async () => {
|
|
1275
1984
|
if (!live())
|
|
1276
1985
|
return false;
|
|
1277
|
-
|
|
1986
|
+
const bytes = layerBytes(page);
|
|
1987
|
+
if (!(await admitted(local, record.locator, bytes)))
|
|
1988
|
+
return false;
|
|
1989
|
+
try {
|
|
1990
|
+
await local.stage(record, page);
|
|
1991
|
+
}
|
|
1992
|
+
catch (error) {
|
|
1993
|
+
if (!exceeded(error))
|
|
1994
|
+
throw error;
|
|
1995
|
+
await exceededOn(record.locator, bytes);
|
|
1996
|
+
return false;
|
|
1997
|
+
}
|
|
1278
1998
|
transfers.set(record.locator, record);
|
|
1999
|
+
readSide.recover();
|
|
1279
2000
|
return true;
|
|
1280
2001
|
}),
|
|
1281
2002
|
abandon: (locator) => exclusive(async () => {
|
|
@@ -1283,6 +2004,7 @@ export function composeRuntime(options) {
|
|
|
1283
2004
|
return;
|
|
1284
2005
|
await local.abandon(locator);
|
|
1285
2006
|
transfers.delete(locator);
|
|
2007
|
+
readSide.recover();
|
|
1286
2008
|
}),
|
|
1287
2009
|
};
|
|
1288
2010
|
}
|
|
@@ -1295,21 +2017,52 @@ export function composeRuntime(options) {
|
|
|
1295
2017
|
async function pullBootstrap(authorized, room, local, live) {
|
|
1296
2018
|
if (!live())
|
|
1297
2019
|
return unconfirmed;
|
|
2020
|
+
const turn = generation;
|
|
2021
|
+
if (replicas.has(room.locator)) {
|
|
2022
|
+
catching.add(room.locator);
|
|
2023
|
+
readSide.recover();
|
|
2024
|
+
}
|
|
1298
2025
|
try {
|
|
1299
|
-
const result = await bootstrapRoom(
|
|
2026
|
+
const result = await bootstrapRoom(reads, authorized, room, {
|
|
1300
2027
|
collection: modelOf(room.locator),
|
|
1301
2028
|
selection: modelOf(room.locator, 'typed-selection'),
|
|
1302
2029
|
}, (models, held) => (live() ? remodel(room.locator, models, held) : held), live, staging(local, live), reusable(room));
|
|
1303
2030
|
const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
|
|
1304
2031
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
2032
|
+
if (result.kind !== 'staged')
|
|
2033
|
+
caughtUpTo(room.locator);
|
|
1305
2034
|
return result;
|
|
1306
2035
|
}
|
|
1307
2036
|
catch (error) {
|
|
1308
2037
|
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
2038
|
+
caughtUpTo(room.locator);
|
|
1309
2039
|
throw error;
|
|
1310
2040
|
}
|
|
2041
|
+
finally {
|
|
2042
|
+
reads.finished(room.locator, turn);
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
function caughtUpTo(locator) {
|
|
2046
|
+
if (catching.delete(locator))
|
|
2047
|
+
readSide.recover();
|
|
2048
|
+
}
|
|
2049
|
+
function behind(room) {
|
|
2050
|
+
const held = replicas.get(room.locator)?.confirmed.record.roomVersion;
|
|
2051
|
+
return (held !== undefined &&
|
|
2052
|
+
[announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
|
|
1311
2053
|
}
|
|
1312
2054
|
async function advance(room, local, authorized, live) {
|
|
2055
|
+
const carried = reads.carried(room.locator);
|
|
2056
|
+
const withheld = await advanceOnce(room, local, authorized, live);
|
|
2057
|
+
if (withheld !== null || reads.carried(room.locator) === carried || !behind(room)) {
|
|
2058
|
+
return withheld;
|
|
2059
|
+
}
|
|
2060
|
+
return advanceOnce(room, local, authorized, live);
|
|
2061
|
+
}
|
|
2062
|
+
async function advanceOnce(room, local, authorized, live) {
|
|
2063
|
+
await referenced(room.locator);
|
|
2064
|
+
if (!live() || paused.has(room.locator))
|
|
2065
|
+
return unconfirmed;
|
|
1313
2066
|
const held = replicas.get(room.locator);
|
|
1314
2067
|
if (held !== undefined) {
|
|
1315
2068
|
const refreshed = await pullRefresh(authorized, held.confirmed, live);
|
|
@@ -1340,6 +2093,7 @@ export function composeRuntime(options) {
|
|
|
1340
2093
|
return refreshed;
|
|
1341
2094
|
if (opened !== null) {
|
|
1342
2095
|
owed.add(room.locator);
|
|
2096
|
+
readSide.recover();
|
|
1343
2097
|
return unconfirmed;
|
|
1344
2098
|
}
|
|
1345
2099
|
}
|
|
@@ -1352,10 +2106,19 @@ export function composeRuntime(options) {
|
|
|
1352
2106
|
return null;
|
|
1353
2107
|
}
|
|
1354
2108
|
async function withhold(withheld, room, local, progress) {
|
|
2109
|
+
if (withheld.kind === 'unconfirmed')
|
|
2110
|
+
return;
|
|
1355
2111
|
if (withheld.kind === 'refused')
|
|
1356
2112
|
stale.delete(room.locator);
|
|
1357
2113
|
else
|
|
1358
2114
|
stale.add(room.locator);
|
|
2115
|
+
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
2116
|
+
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
2117
|
+
repairable.set(room.locator, reset);
|
|
2118
|
+
repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
|
|
2119
|
+
}
|
|
2120
|
+
else
|
|
2121
|
+
repairable.delete(room.locator);
|
|
1359
2122
|
if (withheld.kind === 'scoped-refusal') {
|
|
1360
2123
|
await scoped(withheld, room, local);
|
|
1361
2124
|
}
|
|
@@ -1391,6 +2154,7 @@ export function composeRuntime(options) {
|
|
|
1391
2154
|
unreadable.add(room.locator);
|
|
1392
2155
|
list(room, false);
|
|
1393
2156
|
emitRooms();
|
|
2157
|
+
moveTo({ ...status, ...attention() });
|
|
1394
2158
|
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
1395
2159
|
}
|
|
1396
2160
|
function leaving(batch, result) {
|
|
@@ -1412,6 +2176,7 @@ export function composeRuntime(options) {
|
|
|
1412
2176
|
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
1413
2177
|
}
|
|
1414
2178
|
async function adoptAccepted(room, local, submitted, accepted) {
|
|
2179
|
+
await referenced(room.locator);
|
|
1415
2180
|
if (!continuing())
|
|
1416
2181
|
return unconfirmed;
|
|
1417
2182
|
const current = replicas.get(room.locator);
|
|
@@ -1433,11 +2198,12 @@ export function composeRuntime(options) {
|
|
|
1433
2198
|
emitQueue(room.locator);
|
|
1434
2199
|
return applied;
|
|
1435
2200
|
}
|
|
1436
|
-
const
|
|
2201
|
+
const settled = await settle(local, room.locator, current, applied.confirmed, current.queue.filter((batch) => batch.id !== submitted.id), { room: true });
|
|
1437
2202
|
pendings.delete(submitted.id);
|
|
1438
2203
|
unreadable.delete(room.locator);
|
|
1439
2204
|
sourceRefusals.delete(room.locator);
|
|
1440
2205
|
declarationRefusals.delete(room.locator);
|
|
2206
|
+
const set = settled();
|
|
1441
2207
|
list(room, true);
|
|
1442
2208
|
publishChangeSet(set);
|
|
1443
2209
|
moveTo({ ...status, ...attention() });
|
|
@@ -1451,14 +2217,14 @@ export function composeRuntime(options) {
|
|
|
1451
2217
|
refusedSource(room, failed);
|
|
1452
2218
|
return false;
|
|
1453
2219
|
}
|
|
1454
|
-
async function submitRoom(room, local, authorized, progress) {
|
|
1455
|
-
while (continuing() && authorization !== undefined) {
|
|
2220
|
+
async function submitRoom(room, local, authorized, progress, signal) {
|
|
2221
|
+
while (continuing() && authorization !== undefined && !signal.aborted) {
|
|
1456
2222
|
const replica = replicas.get(room.locator);
|
|
1457
2223
|
const head = replica?.queue[0];
|
|
1458
2224
|
if (replica === undefined || head === undefined)
|
|
1459
|
-
return;
|
|
2225
|
+
return 'done';
|
|
1460
2226
|
if (pendings.get(head.id)?.accepted !== undefined)
|
|
1461
|
-
return;
|
|
2227
|
+
return 'done';
|
|
1462
2228
|
const state = pendings.get(head.id);
|
|
1463
2229
|
const { submission, lost: earlier } = state ?? {
|
|
1464
2230
|
submission: { state: 'waiting' },
|
|
@@ -1466,24 +2232,30 @@ export function composeRuntime(options) {
|
|
|
1466
2232
|
};
|
|
1467
2233
|
if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
|
|
1468
2234
|
progress.complete = false;
|
|
1469
|
-
return;
|
|
2235
|
+
return { at: Date.parse(submission.reset) };
|
|
1470
2236
|
}
|
|
1471
2237
|
if (writesHeld()) {
|
|
1472
2238
|
progress.complete = false;
|
|
1473
2239
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1474
|
-
return;
|
|
2240
|
+
return 'done';
|
|
1475
2241
|
}
|
|
1476
2242
|
const lost = earlier || submission.state === 'submitted';
|
|
2243
|
+
if (lost && pastHorizon(head.id)) {
|
|
2244
|
+
await discardPending(room, local, signal);
|
|
2245
|
+
return 'done';
|
|
2246
|
+
}
|
|
1477
2247
|
let result;
|
|
1478
2248
|
journal.record(`submission ${room.locator} ${head.id}`);
|
|
1479
2249
|
await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
|
|
1480
2250
|
if (discoveryPaused || fences.has(room.locator))
|
|
1481
|
-
return;
|
|
1482
|
-
if (writesHeld()) {
|
|
2251
|
+
return 'done';
|
|
2252
|
+
if (signal.aborted || writesHeld()) {
|
|
1483
2253
|
await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
|
|
2254
|
+
if (signal.aborted)
|
|
2255
|
+
return 'done';
|
|
1484
2256
|
progress.complete = false;
|
|
1485
2257
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1486
|
-
return;
|
|
2258
|
+
return 'done';
|
|
1487
2259
|
}
|
|
1488
2260
|
const opensBefore = openSignals;
|
|
1489
2261
|
const answered = awaitingAnswer(room.locator);
|
|
@@ -1499,32 +2271,36 @@ export function composeRuntime(options) {
|
|
|
1499
2271
|
progress.complete = false;
|
|
1500
2272
|
if (continuing())
|
|
1501
2273
|
progress.reachable = false;
|
|
1502
|
-
return;
|
|
2274
|
+
return 'again';
|
|
2275
|
+
}
|
|
2276
|
+
if (signal.aborted) {
|
|
2277
|
+
answered();
|
|
2278
|
+
return 'done';
|
|
1503
2279
|
}
|
|
1504
2280
|
if (result.kind !== 'accepted' || !continuing())
|
|
1505
2281
|
answered();
|
|
1506
2282
|
if (!continuing())
|
|
1507
|
-
return;
|
|
2283
|
+
return 'done';
|
|
1508
2284
|
if (result.kind === 'transport-uncertainty') {
|
|
1509
2285
|
progress.complete = false;
|
|
1510
2286
|
progress.reachable = false;
|
|
1511
|
-
return;
|
|
2287
|
+
return 'again';
|
|
1512
2288
|
}
|
|
1513
2289
|
if (result.kind === 'room-not-readable') {
|
|
1514
2290
|
progress.complete = false;
|
|
1515
2291
|
unreadable.add(room.locator);
|
|
1516
2292
|
list(room, false);
|
|
1517
2293
|
emitRooms();
|
|
1518
|
-
return;
|
|
2294
|
+
return 'done';
|
|
1519
2295
|
}
|
|
1520
2296
|
if (result.kind === 'revoked-authorization') {
|
|
1521
2297
|
await refuse('revoked-authorization');
|
|
1522
|
-
return;
|
|
2298
|
+
return 'done';
|
|
1523
2299
|
}
|
|
1524
2300
|
if (result.kind === 'scoped-refusal') {
|
|
1525
2301
|
await scoped(result, room, local);
|
|
1526
2302
|
progress.complete = false;
|
|
1527
|
-
return;
|
|
2303
|
+
return 'done';
|
|
1528
2304
|
}
|
|
1529
2305
|
if (result.kind === 'write-halted') {
|
|
1530
2306
|
journal.record(`outcome ${room.locator} ${head.id} write-halted`);
|
|
@@ -1533,16 +2309,24 @@ export function composeRuntime(options) {
|
|
|
1533
2309
|
continue;
|
|
1534
2310
|
haltedRefusal = true;
|
|
1535
2311
|
progress.complete = false;
|
|
1536
|
-
return;
|
|
2312
|
+
return 'done';
|
|
1537
2313
|
}
|
|
1538
2314
|
if (result.kind === 'conflicted' ||
|
|
1539
2315
|
result.kind === 'rejected' ||
|
|
1540
2316
|
result.kind === 'expired' ||
|
|
1541
2317
|
result.kind === 'retryable-refusal') {
|
|
2318
|
+
if (result.kind === 'expired' && lost && result.restored !== true) {
|
|
2319
|
+
journal.record(`outcome ${room.locator} ${head.id} expired`);
|
|
2320
|
+
await discardPending(room, local, signal);
|
|
2321
|
+
return 'done';
|
|
2322
|
+
}
|
|
1542
2323
|
if (result.kind === 'expired') {
|
|
2324
|
+
await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, false));
|
|
1543
2325
|
const withheld = await advance(room, local, authorized, continuing);
|
|
1544
|
-
if (withheld !== null)
|
|
1545
|
-
|
|
2326
|
+
if (withheld !== null) {
|
|
2327
|
+
await withhold(withheld, room, local, progress);
|
|
2328
|
+
return 'done';
|
|
2329
|
+
}
|
|
1546
2330
|
}
|
|
1547
2331
|
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
|
|
1548
2332
|
(result.kind === 'conflicted' && result.roomVersion !== undefined
|
|
@@ -1555,8 +2339,7 @@ export function composeRuntime(options) {
|
|
|
1555
2339
|
const reset = result.reset;
|
|
1556
2340
|
progress.complete = false;
|
|
1557
2341
|
await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
|
|
1558
|
-
|
|
1559
|
-
return;
|
|
2342
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
1560
2343
|
}
|
|
1561
2344
|
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
1562
2345
|
const left = leaving(head, result);
|
|
@@ -1567,28 +2350,73 @@ export function composeRuntime(options) {
|
|
|
1567
2350
|
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
1568
2351
|
return;
|
|
1569
2352
|
}
|
|
1570
|
-
const
|
|
2353
|
+
const settled = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
|
|
1571
2354
|
leaving: (result.kind === 'expired' && landed(head, current.confirmed)) ||
|
|
1572
2355
|
(result.kind === 'conflicted' && landed(head, current.confirmed, result.affected))
|
|
1573
2356
|
? { ...left, returned: null }
|
|
1574
2357
|
: left,
|
|
1575
2358
|
});
|
|
1576
|
-
publishChangeSet(
|
|
2359
|
+
publishChangeSet(settled());
|
|
1577
2360
|
moveTo({ ...status, ...attention() });
|
|
1578
2361
|
});
|
|
1579
2362
|
if (result.kind === 'expired')
|
|
1580
2363
|
continue;
|
|
1581
2364
|
const withheld = await advance(room, local, authorized, continuing);
|
|
1582
|
-
if (withheld !== null)
|
|
1583
|
-
|
|
2365
|
+
if (withheld !== null) {
|
|
2366
|
+
await withhold(withheld, room, local, progress);
|
|
2367
|
+
return 'done';
|
|
2368
|
+
}
|
|
1584
2369
|
continue;
|
|
1585
2370
|
}
|
|
1586
2371
|
const kept = await adopted(room, local, head, result).finally(answered);
|
|
1587
2372
|
if (!kept) {
|
|
1588
2373
|
progress.complete = false;
|
|
1589
|
-
return;
|
|
2374
|
+
return 'done';
|
|
1590
2375
|
}
|
|
1591
2376
|
}
|
|
2377
|
+
return 'done';
|
|
2378
|
+
}
|
|
2379
|
+
function pastHorizon(identity) {
|
|
2380
|
+
return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
|
|
2381
|
+
}
|
|
2382
|
+
async function discardPending(room, local, signal) {
|
|
2383
|
+
const { locator } = room;
|
|
2384
|
+
const discarded = await exclusive(async () => {
|
|
2385
|
+
if (fences.has(locator))
|
|
2386
|
+
return false;
|
|
2387
|
+
const before = replicas.get(locator);
|
|
2388
|
+
const queue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
2389
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
2390
|
+
lastFenced.set(locator, ++fencings);
|
|
2391
|
+
try {
|
|
2392
|
+
await local.discard(locator);
|
|
2393
|
+
}
|
|
2394
|
+
finally {
|
|
2395
|
+
fences.delete(locator);
|
|
2396
|
+
}
|
|
2397
|
+
for (const batch of queue)
|
|
2398
|
+
pendings.delete(batch.id);
|
|
2399
|
+
queuesWithoutReplica.delete(locator);
|
|
2400
|
+
const after = before === undefined ? null : replicaOf(before.confirmed, [], before);
|
|
2401
|
+
if (after !== null)
|
|
2402
|
+
replicas.set(locator, after);
|
|
2403
|
+
journal.record(`room discarded ${locator}`);
|
|
2404
|
+
publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
|
|
2405
|
+
moveTo({ ...status, ...attention() });
|
|
2406
|
+
return true;
|
|
2407
|
+
});
|
|
2408
|
+
if (!discarded)
|
|
2409
|
+
return;
|
|
2410
|
+
const progress = { complete: true, reachable: true };
|
|
2411
|
+
await roomTask(locator, async (live) => {
|
|
2412
|
+
await Promise.race([
|
|
2413
|
+
reload(room, local, progress, () => live() && !signal.aborted),
|
|
2414
|
+
abandonedOn(signal),
|
|
2415
|
+
]);
|
|
2416
|
+
});
|
|
2417
|
+
if (signal.aborted)
|
|
2418
|
+
return;
|
|
2419
|
+
await rest(progress);
|
|
1592
2420
|
}
|
|
1593
2421
|
function awaitingAnswer(locator) {
|
|
1594
2422
|
let release = () => { };
|
|
@@ -1668,6 +2496,11 @@ export function composeRuntime(options) {
|
|
|
1668
2496
|
});
|
|
1669
2497
|
}
|
|
1670
2498
|
async function scoped(result, room, local) {
|
|
2499
|
+
if ((result.scope === 'content' || result.scope === 'sync') && !result.retryable) {
|
|
2500
|
+
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
2501
|
+
await loseRooms([room.locator]);
|
|
2502
|
+
return;
|
|
2503
|
+
}
|
|
1671
2504
|
scopedRefusals.set(room.locator, {
|
|
1672
2505
|
code: result.code,
|
|
1673
2506
|
fields: result.fields,
|
|
@@ -1714,15 +2547,16 @@ export function composeRuntime(options) {
|
|
|
1714
2547
|
});
|
|
1715
2548
|
}
|
|
1716
2549
|
async function synchronize(listed, local, progress, attempt, live) {
|
|
1717
|
-
const
|
|
2550
|
+
const acquired = listed.filter(acquires);
|
|
2551
|
+
const prepared = await Promise.allSettled(acquired.map((room) => prepareRoom(room, local, progress)));
|
|
1718
2552
|
if (!live())
|
|
1719
2553
|
return;
|
|
1720
|
-
const ready =
|
|
2554
|
+
const ready = acquired.filter((_, index) => {
|
|
1721
2555
|
const outcome = prepared[index];
|
|
1722
2556
|
return outcome?.status === 'fulfilled' && outcome.value;
|
|
1723
2557
|
});
|
|
1724
2558
|
if (attempt === 'opened') {
|
|
1725
|
-
options.adapter.roomStream?.hold(heldRooms(ready));
|
|
2559
|
+
options.adapter.roomStream?.hold(heldRooms(ready), heldDemand(acquired));
|
|
1726
2560
|
handed = true;
|
|
1727
2561
|
}
|
|
1728
2562
|
discovering?.release();
|
|
@@ -1750,19 +2584,10 @@ export function composeRuntime(options) {
|
|
|
1750
2584
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
1751
2585
|
if (discoveryPaused || authorization === undefined)
|
|
1752
2586
|
return false;
|
|
1753
|
-
|
|
1754
|
-
if (replicas.has(room.locator))
|
|
1755
|
-
progress.complete = false;
|
|
1756
|
-
return false;
|
|
1757
|
-
}
|
|
1758
|
-
return true;
|
|
2587
|
+
return room.replica.admission === 'admitted';
|
|
1759
2588
|
}
|
|
1760
2589
|
async function caughtUp(room, local, progress, live) {
|
|
1761
|
-
|
|
1762
|
-
if (held === undefined || authorization === undefined || !live())
|
|
1763
|
-
return;
|
|
1764
|
-
const behind = [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later));
|
|
1765
|
-
if (!behind)
|
|
2590
|
+
if (authorization === undefined || !live() || !behind(room))
|
|
1766
2591
|
return;
|
|
1767
2592
|
const withheld = await advance(room, local, authorization, live);
|
|
1768
2593
|
if (withheld !== null)
|
|
@@ -1789,19 +2614,24 @@ export function composeRuntime(options) {
|
|
|
1789
2614
|
await caughtUp(room, local, progress, live);
|
|
1790
2615
|
}
|
|
1791
2616
|
async function reload(room, local, progress, live) {
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
2617
|
+
try {
|
|
2618
|
+
if (!live() || authorization === undefined)
|
|
2619
|
+
return;
|
|
2620
|
+
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
2621
|
+
if (!live())
|
|
2622
|
+
return;
|
|
2623
|
+
if (bootstrapped.kind !== 'staged') {
|
|
2624
|
+
await withhold(bootstrapped, room, local, progress);
|
|
2625
|
+
return;
|
|
2626
|
+
}
|
|
2627
|
+
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
2628
|
+
await caughtUp(room, local, progress, live);
|
|
2629
|
+
}
|
|
2630
|
+
finally {
|
|
2631
|
+
caughtUpTo(room.locator);
|
|
1800
2632
|
}
|
|
1801
|
-
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
1802
|
-
await caughtUp(room, local, progress, live);
|
|
1803
2633
|
}
|
|
1804
|
-
async function discoverRooms(local, progress, attempt, live) {
|
|
2634
|
+
async function discoverRooms(local, progress, attempt, live, scope) {
|
|
1805
2635
|
if (closed || authorization === undefined) {
|
|
1806
2636
|
notifyReady();
|
|
1807
2637
|
return;
|
|
@@ -1811,7 +2641,7 @@ export function composeRuntime(options) {
|
|
|
1811
2641
|
const started = discoveries;
|
|
1812
2642
|
let result;
|
|
1813
2643
|
try {
|
|
1814
|
-
result = await options.adapter.discover(authorization);
|
|
2644
|
+
result = await options.adapter.discover(authorization, scope);
|
|
1815
2645
|
}
|
|
1816
2646
|
catch {
|
|
1817
2647
|
result = { kind: 'transport-uncertainty' };
|
|
@@ -1875,6 +2705,7 @@ export function composeRuntime(options) {
|
|
|
1875
2705
|
}
|
|
1876
2706
|
const previousCapabilities = capabilities;
|
|
1877
2707
|
capabilities = result.capabilities;
|
|
2708
|
+
outcomeRetention = result.retention.outcome;
|
|
1878
2709
|
notifyReady();
|
|
1879
2710
|
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
1880
2711
|
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
@@ -1887,7 +2718,7 @@ export function composeRuntime(options) {
|
|
|
1887
2718
|
member = signedIn;
|
|
1888
2719
|
emitMember();
|
|
1889
2720
|
}
|
|
1890
|
-
await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
|
|
2721
|
+
await synchronize(await applyDiscovery(result.rooms, started, result.covered), local, progress, attempt, live);
|
|
1891
2722
|
}
|
|
1892
2723
|
async function ensureAuthorization() {
|
|
1893
2724
|
if (authorization !== undefined)
|
|
@@ -1984,9 +2815,43 @@ export function composeRuntime(options) {
|
|
|
1984
2815
|
options.adapter.roomStream?.reopen();
|
|
1985
2816
|
});
|
|
1986
2817
|
}
|
|
1987
|
-
function
|
|
2818
|
+
function rediscover(scope) {
|
|
2819
|
+
return task(OPENING_LANE, async (live) => {
|
|
2820
|
+
if (!live() || !socketOpen)
|
|
2821
|
+
return;
|
|
2822
|
+
const progress = { complete: true, reachable: true };
|
|
2823
|
+
const gate = discover();
|
|
2824
|
+
moveTo(transferring(status));
|
|
2825
|
+
try {
|
|
2826
|
+
const local = await openLocal();
|
|
2827
|
+
if (!live())
|
|
2828
|
+
return;
|
|
2829
|
+
await ensureAuthorization();
|
|
2830
|
+
if (!live())
|
|
2831
|
+
return;
|
|
2832
|
+
await discoverRooms(local, progress, 'authorized', live, scope);
|
|
2833
|
+
}
|
|
2834
|
+
catch {
|
|
2835
|
+
progress.complete = false;
|
|
2836
|
+
}
|
|
2837
|
+
finally {
|
|
2838
|
+
gate.release();
|
|
2839
|
+
notifyReady();
|
|
2840
|
+
}
|
|
2841
|
+
if (!live())
|
|
2842
|
+
return;
|
|
2843
|
+
if (!progress.complete)
|
|
2844
|
+
complete = false;
|
|
2845
|
+
await rest(progress, live);
|
|
2846
|
+
});
|
|
2847
|
+
}
|
|
2848
|
+
function closedSocket(revoked, lost) {
|
|
1988
2849
|
const opened = socketOpen;
|
|
1989
2850
|
socketOpen = false;
|
|
2851
|
+
if (lost) {
|
|
2852
|
+
reads.lost();
|
|
2853
|
+
conversations.lost();
|
|
2854
|
+
}
|
|
1990
2855
|
if (revoked) {
|
|
1991
2856
|
void refuse('revoked-authorization');
|
|
1992
2857
|
return;
|
|
@@ -2003,7 +2868,10 @@ export function composeRuntime(options) {
|
|
|
2003
2868
|
function applyOpening(expired) {
|
|
2004
2869
|
const progress = underway?.generation === generation ? underway.progress : null;
|
|
2005
2870
|
const cycle = progress ?? { complete: true, reachable: true };
|
|
2006
|
-
|
|
2871
|
+
if (opened === generation)
|
|
2872
|
+
opened = null;
|
|
2873
|
+
const reloading = progress === null ? [] : owedReloads(expired);
|
|
2874
|
+
const replacing = reloadNow((room) => expired.includes(room.locator) || reloading.includes(room.locator), cycle);
|
|
2007
2875
|
return task(OPENING_LANE, async (live) => {
|
|
2008
2876
|
underway = null;
|
|
2009
2877
|
if (!live())
|
|
@@ -2013,23 +2881,17 @@ export function composeRuntime(options) {
|
|
|
2013
2881
|
await rest(cycle, live);
|
|
2014
2882
|
return;
|
|
2015
2883
|
}
|
|
2016
|
-
await roomsCarried(expired);
|
|
2884
|
+
await roomsCarried([...expired, ...reloading]);
|
|
2017
2885
|
if (!live())
|
|
2018
2886
|
return;
|
|
2019
|
-
if (opened === generation)
|
|
2020
|
-
opened = null;
|
|
2021
|
-
const replaced = new Set(owed);
|
|
2022
|
-
owed.clear();
|
|
2023
2887
|
try {
|
|
2024
2888
|
const local = await openLocal();
|
|
2025
|
-
const
|
|
2026
|
-
const reloading = held.filter((room) => !expired.includes(room.locator) &&
|
|
2027
|
-
(replaced.has(room.locator) || revoked(room.locator)));
|
|
2028
|
-
const reloads = Promise.allSettled(reloading.map((room) => roomTask(room.locator, (current) => reload(room, local, progress, current))));
|
|
2029
|
-
const reloaded = [...(await reloads), ...(await replacing)];
|
|
2889
|
+
const reloaded = await replacing;
|
|
2030
2890
|
if (progress.reachable) {
|
|
2031
|
-
for (const room of
|
|
2032
|
-
if (
|
|
2891
|
+
for (const room of discovered.values()) {
|
|
2892
|
+
if (replicas.has(room.locator) &&
|
|
2893
|
+
!expired.includes(room.locator) &&
|
|
2894
|
+
!reloading.includes(room.locator)) {
|
|
2033
2895
|
await confirmModels(room, local);
|
|
2034
2896
|
}
|
|
2035
2897
|
}
|
|
@@ -2046,6 +2908,18 @@ export function composeRuntime(options) {
|
|
|
2046
2908
|
await finishOpening(progress, live);
|
|
2047
2909
|
});
|
|
2048
2910
|
}
|
|
2911
|
+
function owedReloads(expired) {
|
|
2912
|
+
const reloading = [...discovered.values()]
|
|
2913
|
+
.filter((room) => replicas.has(room.locator) &&
|
|
2914
|
+
!expired.includes(room.locator) &&
|
|
2915
|
+
(owed.has(room.locator) || revoked(room.locator)))
|
|
2916
|
+
.map((room) => room.locator);
|
|
2917
|
+
owed.clear();
|
|
2918
|
+
for (const locator of reloading)
|
|
2919
|
+
catching.add(locator);
|
|
2920
|
+
readSide.recover();
|
|
2921
|
+
return reloading;
|
|
2922
|
+
}
|
|
2049
2923
|
async function reloadNow(replaced, progress) {
|
|
2050
2924
|
try {
|
|
2051
2925
|
const local = await openLocal();
|
|
@@ -2069,38 +2943,94 @@ export function composeRuntime(options) {
|
|
|
2069
2943
|
function submitLater(locator) {
|
|
2070
2944
|
if (localSaves)
|
|
2071
2945
|
return;
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2946
|
+
void recovery.attempt(`submit ${locator}`, (signal) => submitPass(locator, signal));
|
|
2947
|
+
}
|
|
2948
|
+
async function submitPass(locator, signal) {
|
|
2949
|
+
const room = discovered.get(locator);
|
|
2950
|
+
if (closed || stopping || discoveryPaused || room === undefined)
|
|
2951
|
+
return 'done';
|
|
2952
|
+
const progress = { complete: true, reachable: true };
|
|
2953
|
+
let next;
|
|
2954
|
+
try {
|
|
2955
|
+
await ensureAuthorization();
|
|
2956
|
+
if (authorization === undefined)
|
|
2957
|
+
return 'done';
|
|
2958
|
+
next = await submitRoom(room, await openLocal(), authorization, progress, signal);
|
|
2959
|
+
}
|
|
2960
|
+
catch {
|
|
2961
|
+
progress.reachable = false;
|
|
2962
|
+
next = 'again';
|
|
2963
|
+
}
|
|
2964
|
+
if (signal.aborted)
|
|
2965
|
+
return 'done';
|
|
2966
|
+
await rest(progress);
|
|
2967
|
+
return next === 'again' && !socketOpen ? 'done' : next;
|
|
2968
|
+
}
|
|
2969
|
+
function repairLater(locator, when) {
|
|
2970
|
+
if (!socketOpen || repairing.has(locator))
|
|
2075
2971
|
return;
|
|
2972
|
+
void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
|
|
2973
|
+
}
|
|
2974
|
+
async function repairPass(locator, signal) {
|
|
2975
|
+
const room = discovered.get(locator);
|
|
2976
|
+
if (!socketOpen || closed || stopping || discoveryPaused || !stale.has(locator))
|
|
2977
|
+
return 'done';
|
|
2978
|
+
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2979
|
+
return 'done';
|
|
2980
|
+
const progress = { complete: true, reachable: true };
|
|
2981
|
+
repairing.add(locator);
|
|
2982
|
+
try {
|
|
2983
|
+
const local = await openLocal();
|
|
2984
|
+
await roomTask(locator, async (live) => {
|
|
2985
|
+
const current = () => live() && !signal.aborted;
|
|
2986
|
+
if (!current() || authorization === undefined)
|
|
2987
|
+
return;
|
|
2988
|
+
const withheld = await Promise.race([
|
|
2989
|
+
advance(room, local, authorization, current),
|
|
2990
|
+
abandonedOn(signal),
|
|
2991
|
+
]);
|
|
2992
|
+
if (withheld !== null && current())
|
|
2993
|
+
await withhold(withheld, room, local, progress);
|
|
2994
|
+
});
|
|
2076
2995
|
}
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2996
|
+
catch {
|
|
2997
|
+
stale.add(locator);
|
|
2998
|
+
progress.reachable = false;
|
|
2999
|
+
}
|
|
3000
|
+
finally {
|
|
3001
|
+
repairing.delete(locator);
|
|
3002
|
+
}
|
|
3003
|
+
if (signal.aborted)
|
|
3004
|
+
return 'done';
|
|
3005
|
+
await rest(progress);
|
|
3006
|
+
if (!stale.has(locator)) {
|
|
3007
|
+
if ((replicas.get(locator)?.queue.length ?? 0) > 0)
|
|
3008
|
+
submitLater(locator);
|
|
3009
|
+
return 'done';
|
|
3010
|
+
}
|
|
3011
|
+
if (!repairable.has(locator))
|
|
3012
|
+
return 'done';
|
|
3013
|
+
const reset = repairable.get(locator);
|
|
3014
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
3015
|
+
}
|
|
3016
|
+
function resendLater() {
|
|
3017
|
+
if (!socketOpen)
|
|
3018
|
+
return;
|
|
3019
|
+
void recovery.attempt('resend', resendPass, { again: true });
|
|
3020
|
+
}
|
|
3021
|
+
async function resendPass(signal) {
|
|
3022
|
+
if (!socketOpen || closed || stopping || discoveryPaused)
|
|
3023
|
+
return 'done';
|
|
3024
|
+
const reading = { complete: true, reachable: true };
|
|
3025
|
+
try {
|
|
3026
|
+
await conversations.resend(reading);
|
|
3027
|
+
}
|
|
3028
|
+
catch {
|
|
3029
|
+
reading.reachable = false;
|
|
3030
|
+
}
|
|
3031
|
+
if (signal.aborted)
|
|
3032
|
+
return 'done';
|
|
3033
|
+
return reading.reachable && reading.complete ? 'done' : 'again';
|
|
2104
3034
|
}
|
|
2105
3035
|
function submitAll() {
|
|
2106
3036
|
for (const [locator, replica] of replicas) {
|
|
@@ -2114,16 +3044,25 @@ export function composeRuntime(options) {
|
|
|
2114
3044
|
progress.complete = false;
|
|
2115
3045
|
return;
|
|
2116
3046
|
}
|
|
3047
|
+
reads.applied();
|
|
2117
3048
|
submitAll();
|
|
3049
|
+
for (const locator of stale)
|
|
3050
|
+
if (repairable.has(locator))
|
|
3051
|
+
repairLater(locator, {});
|
|
2118
3052
|
await conversations.reconcile();
|
|
2119
3053
|
if (status.providerUnreachable)
|
|
2120
3054
|
conversations.reconnected();
|
|
2121
3055
|
const reading = { complete: true, reachable: true };
|
|
2122
|
-
|
|
3056
|
+
if (demand.conversations)
|
|
3057
|
+
await conversations.catchUp(reading);
|
|
2123
3058
|
if (progress.reachable && reading.reachable)
|
|
2124
3059
|
await conversations.resend(reading);
|
|
2125
|
-
if (
|
|
3060
|
+
if (progress.reachable)
|
|
3061
|
+
void recovery.attempt('calls', resendCalls);
|
|
3062
|
+
if (!reading.reachable) {
|
|
2126
3063
|
progress.reachable = false;
|
|
3064
|
+
resendLater();
|
|
3065
|
+
}
|
|
2127
3066
|
}
|
|
2128
3067
|
catch {
|
|
2129
3068
|
progress.complete = false;
|
|
@@ -2180,7 +3119,7 @@ export function composeRuntime(options) {
|
|
|
2180
3119
|
return;
|
|
2181
3120
|
const room = discovered.get(locator);
|
|
2182
3121
|
const held = replicas.get(locator);
|
|
2183
|
-
if (room === undefined || room.replica.admission !== 'admitted')
|
|
3122
|
+
if (room === undefined || room.replica.admission !== 'admitted' || !acquires(room))
|
|
2184
3123
|
return;
|
|
2185
3124
|
if (held !== undefined &&
|
|
2186
3125
|
(held.confirmed.record.replica.selection !== room.replica.selection ||
|
|
@@ -2232,12 +3171,17 @@ export function composeRuntime(options) {
|
|
|
2232
3171
|
if (event.kind === 'opened') {
|
|
2233
3172
|
socketOpen = true;
|
|
2234
3173
|
generation += 1;
|
|
3174
|
+
reads.opened();
|
|
3175
|
+
conversations.reopened();
|
|
3176
|
+
recovery.resume();
|
|
2235
3177
|
void sync('opened');
|
|
2236
3178
|
}
|
|
2237
3179
|
else if (event.kind === 'closed')
|
|
2238
|
-
closedSocket(event.revoked);
|
|
3180
|
+
closedSocket(event.revoked, event.lost);
|
|
2239
3181
|
else if (event.kind === 'opening')
|
|
2240
3182
|
void applyOpening(event.expired);
|
|
3183
|
+
else if (event.kind === 'authority-changed')
|
|
3184
|
+
void rediscover(event.scope);
|
|
2241
3185
|
else
|
|
2242
3186
|
void applyRoom(event.locator, event.roomVersion);
|
|
2243
3187
|
}, receipts);
|
|
@@ -2255,6 +3199,7 @@ export function composeRuntime(options) {
|
|
|
2255
3199
|
haltedRefusal = false;
|
|
2256
3200
|
if (mode !== serviceMode) {
|
|
2257
3201
|
serviceMode = mode;
|
|
3202
|
+
queueReadiness();
|
|
2258
3203
|
journal.record(`service mode ${mode}`);
|
|
2259
3204
|
for (const listener of serviceModeListeners)
|
|
2260
3205
|
listener(mode);
|
|
@@ -2267,6 +3212,8 @@ export function composeRuntime(options) {
|
|
|
2267
3212
|
if (stream === undefined || withdrawConversations !== undefined)
|
|
2268
3213
|
return;
|
|
2269
3214
|
withdrawConversations = stream.observe((event) => {
|
|
3215
|
+
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted')
|
|
3216
|
+
queueReadiness();
|
|
2270
3217
|
if (conversations.receive(event)) {
|
|
2271
3218
|
void ready.then(scheduleCurrency);
|
|
2272
3219
|
return;
|
|
@@ -2289,7 +3236,7 @@ export function composeRuntime(options) {
|
|
|
2289
3236
|
if (!live())
|
|
2290
3237
|
return;
|
|
2291
3238
|
await conversations.reconcile();
|
|
2292
|
-
if (conversations.current()) {
|
|
3239
|
+
if (conversations.current() || !demand.conversations) {
|
|
2293
3240
|
show();
|
|
2294
3241
|
return;
|
|
2295
3242
|
}
|
|
@@ -2307,13 +3254,18 @@ export function composeRuntime(options) {
|
|
|
2307
3254
|
if (closed)
|
|
2308
3255
|
return;
|
|
2309
3256
|
closed = true;
|
|
3257
|
+
if (narrowing !== null)
|
|
3258
|
+
clearTimeout(narrowing);
|
|
3259
|
+
if (rechecking !== null)
|
|
3260
|
+
clearInterval(rechecking);
|
|
3261
|
+
readinessWatches.clear();
|
|
2310
3262
|
withdrawStream?.();
|
|
2311
3263
|
withdrawStream = undefined;
|
|
2312
3264
|
withdrawConversations?.();
|
|
2313
3265
|
withdrawConversations = undefined;
|
|
2314
3266
|
withdrawServiceMode?.();
|
|
2315
3267
|
conversations.close();
|
|
2316
|
-
|
|
3268
|
+
void recovery.retire();
|
|
2317
3269
|
store?.close();
|
|
2318
3270
|
void opening?.then((opened) => {
|
|
2319
3271
|
opened.close();
|
|
@@ -2331,9 +3283,8 @@ export function composeRuntime(options) {
|
|
|
2331
3283
|
}
|
|
2332
3284
|
async function stop() {
|
|
2333
3285
|
stopping = true;
|
|
2334
|
-
|
|
3286
|
+
await recovery.retire();
|
|
2335
3287
|
await Promise.all(working);
|
|
2336
|
-
await Promise.all([...submitting.values()].map((run) => run.done));
|
|
2337
3288
|
await mutations;
|
|
2338
3289
|
close();
|
|
2339
3290
|
}
|
|
@@ -2352,20 +3303,111 @@ export function composeRuntime(options) {
|
|
|
2352
3303
|
if (options.adapter.roomStream === undefined)
|
|
2353
3304
|
void sync('unstreamed');
|
|
2354
3305
|
function planned(input, reach) {
|
|
2355
|
-
|
|
3306
|
+
const plan = planOf(input, roomFacts(reach), planReach(reach));
|
|
3307
|
+
const named = plan.paths !== null &&
|
|
3308
|
+
plan.rooms !== null &&
|
|
3309
|
+
plan.paths.length * plan.rooms.length <= MAXIMUM_WINDOW;
|
|
3310
|
+
return plan.limit === null && named ? plan : { ...plan, limit: capped(plan.limit) };
|
|
2356
3311
|
}
|
|
2357
3312
|
function watchPlan(plan, deliver) {
|
|
2358
|
-
|
|
2359
|
-
|
|
3313
|
+
let seeded = false;
|
|
3314
|
+
let pending = true;
|
|
3315
|
+
const shown = () => {
|
|
3316
|
+
const seed = seeds.get(plan.key);
|
|
3317
|
+
if (seed === undefined || seeded || !pending)
|
|
3318
|
+
return;
|
|
3319
|
+
seeded = true;
|
|
3320
|
+
deliver(seededResult(seed));
|
|
3321
|
+
};
|
|
3322
|
+
const watchers = seedWatchers.get(plan.key) ?? new Set();
|
|
3323
|
+
seedWatchers.set(plan.key, watchers);
|
|
3324
|
+
watchers.add(shown);
|
|
3325
|
+
const stop = readSide.watch(plan, identityOf(plan), () => {
|
|
3326
|
+
const cover = covered(plan);
|
|
3327
|
+
awaitPreparation(cover);
|
|
3328
|
+
return seedBehind(plan, cover);
|
|
3329
|
+
}, (message) => {
|
|
3330
|
+
const answered = message;
|
|
3331
|
+
pending = whole(answered) && 'pending' in answered.result;
|
|
3332
|
+
if (pending && seeds.has(plan.key)) {
|
|
3333
|
+
shown();
|
|
3334
|
+
return;
|
|
3335
|
+
}
|
|
3336
|
+
seeded = false;
|
|
3337
|
+
deliver(message);
|
|
3338
|
+
});
|
|
3339
|
+
return () => {
|
|
3340
|
+
watchers.delete(shown);
|
|
3341
|
+
if (watchers.size === 0)
|
|
3342
|
+
seedWatchers.delete(plan.key);
|
|
3343
|
+
stop();
|
|
3344
|
+
};
|
|
2360
3345
|
}
|
|
2361
3346
|
async function searched(input, reach) {
|
|
2362
|
-
const
|
|
2363
|
-
const
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
3347
|
+
const asked = searchPlanOf(input);
|
|
3348
|
+
const plan = { ...asked, limit: capped(asked.limit) };
|
|
3349
|
+
const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
|
|
3350
|
+
for (;;) {
|
|
3351
|
+
const cover = covered(filter);
|
|
3352
|
+
awaitPreparation(cover);
|
|
3353
|
+
const pending = 'pending' in cover ? [...cover.pending] : [];
|
|
3354
|
+
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
3355
|
+
const held = covered({
|
|
3356
|
+
...filter,
|
|
3357
|
+
rooms: reachedRooms(filter).filter((room) => !awaited.has(room)),
|
|
3358
|
+
});
|
|
3359
|
+
const covering = 'pending' in held ? [] : held.coverage.rooms;
|
|
3360
|
+
const unindexed = covering
|
|
3361
|
+
.filter(({ room }) => !searchIndex.rooms().has(room))
|
|
3362
|
+
.map(({ room }) => room);
|
|
3363
|
+
for (const room of unindexed)
|
|
3364
|
+
pending.push({ room, awaits: 'index' });
|
|
3365
|
+
const listed = conversations.searchable();
|
|
3366
|
+
const named = reach !== null && !reach.conversations
|
|
3367
|
+
? []
|
|
3368
|
+
: (plan.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
3369
|
+
const waiting = named.filter((one) => !searchIndex.conversations().has(one));
|
|
3370
|
+
for (const conversation of waiting)
|
|
3371
|
+
pending.push({ conversation, awaits: 'index' });
|
|
3372
|
+
if (unindexed.length > 0 || waiting.length > 0)
|
|
3373
|
+
index(unindexed, waiting);
|
|
3374
|
+
const complete = pending.length === 0 && !('pending' in cover);
|
|
3375
|
+
const answering = complete
|
|
3376
|
+
? filter
|
|
3377
|
+
: {
|
|
3378
|
+
...filter,
|
|
3379
|
+
rooms: covering
|
|
3380
|
+
.filter(({ room }) => searchIndex.rooms().has(room))
|
|
3381
|
+
.map(({ room }) => room),
|
|
3382
|
+
};
|
|
3383
|
+
const answered = covered(answering);
|
|
3384
|
+
if ('pending' in answered)
|
|
3385
|
+
continue;
|
|
3386
|
+
const searchedConversations = named.filter((one) => searchIndex.conversations().has(one));
|
|
3387
|
+
const seen = searchIndex.changes();
|
|
3388
|
+
await searchIndex.caughtUp();
|
|
3389
|
+
const selected = roomsAlone(answering) ? null : await rowsQuery(answering);
|
|
3390
|
+
if (selected !== null && 'pending' in selected)
|
|
3391
|
+
continue;
|
|
3392
|
+
if (searchIndex.changes() !== seen ||
|
|
3393
|
+
JSON.stringify(covered(answering)) !== JSON.stringify(answered))
|
|
3394
|
+
continue;
|
|
3395
|
+
const hits = searchIndex.search(plan, selected === null ? null : selected.documents, searchedConversations, answering.rooms === null ? null : new Set(answering.rooms), plan.collection
|
|
3396
|
+
? new Map(answered.coverage.rooms.map(({ room }) => [
|
|
3397
|
+
room,
|
|
3398
|
+
replicas.get(room)?.confirmed.record.typedSelection?.roots ?? [],
|
|
3399
|
+
]))
|
|
3400
|
+
: null);
|
|
3401
|
+
const coverage = {
|
|
3402
|
+
rooms: answered.coverage.rooms,
|
|
3403
|
+
conversations: searchedConversations,
|
|
3404
|
+
index: searchIndex.generation(),
|
|
3405
|
+
overlay: answered.coverage.overlay,
|
|
3406
|
+
};
|
|
3407
|
+
return complete
|
|
3408
|
+
? { ...hits, coverage }
|
|
3409
|
+
: { pending, documents: hits.documents, messages: hits.messages, coverage };
|
|
3410
|
+
}
|
|
2369
3411
|
}
|
|
2370
3412
|
const watch = (topic, input, deliver) => {
|
|
2371
3413
|
if (topic === 'room-drafts') {
|
|
@@ -2411,6 +3453,9 @@ export function composeRuntime(options) {
|
|
|
2411
3453
|
presentationListeners.delete(listener);
|
|
2412
3454
|
};
|
|
2413
3455
|
}
|
|
3456
|
+
if (topic === 'readiness') {
|
|
3457
|
+
return watchReadiness({ target: '', code: 'stored', capacity: null, search: 'unused' }, null, deliver);
|
|
3458
|
+
}
|
|
2414
3459
|
if (topic === 'rooms') {
|
|
2415
3460
|
roomListeners.add(deliver);
|
|
2416
3461
|
deliver(listed());
|
|
@@ -2546,27 +3591,31 @@ export function composeRuntime(options) {
|
|
|
2546
3591
|
});
|
|
2547
3592
|
},
|
|
2548
3593
|
write(app, value) {
|
|
2549
|
-
return
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
3594
|
+
return keepPresentation(app, () => structuredClone(value));
|
|
3595
|
+
},
|
|
3596
|
+
edit(app, members) {
|
|
3597
|
+
return keepPresentation(app, (held) => Object.fromEntries([
|
|
3598
|
+
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(members, member)),
|
|
3599
|
+
...Object.entries(structuredClone(members)).filter(([, value]) => value !== null),
|
|
3600
|
+
]));
|
|
2556
3601
|
},
|
|
2557
3602
|
},
|
|
2558
3603
|
save(room, named, writtenBy) {
|
|
2559
3604
|
return exclusive(() => saveRoomDrafts(room, named, writtenBy));
|
|
2560
3605
|
},
|
|
3606
|
+
saveParts(room, parts, writtenBy) {
|
|
3607
|
+
return exclusive(() => saveParts(room, parts, writtenBy));
|
|
3608
|
+
},
|
|
2561
3609
|
history(locator, path, query) {
|
|
2562
3610
|
return readHistory(locator, path, query);
|
|
2563
3611
|
},
|
|
2564
3612
|
binaryFile: {
|
|
2565
3613
|
read: readBinaryPath,
|
|
3614
|
+
link: linkBinaryPath,
|
|
2566
3615
|
readReference: readBinaryReference,
|
|
2567
3616
|
},
|
|
2568
3617
|
call(name, input) {
|
|
2569
|
-
return
|
|
3618
|
+
return recoveredAction(name, input);
|
|
2570
3619
|
},
|
|
2571
3620
|
reached(locator, roomVersion, path) {
|
|
2572
3621
|
return path === undefined
|
|
@@ -2577,7 +3626,13 @@ export function composeRuntime(options) {
|
|
|
2577
3626
|
return journal.export();
|
|
2578
3627
|
},
|
|
2579
3628
|
persistence() {
|
|
2580
|
-
return
|
|
3629
|
+
return persistence === 'granted';
|
|
3630
|
+
},
|
|
3631
|
+
async retry() {
|
|
3632
|
+
journal.record('retry');
|
|
3633
|
+
unindexable.clear();
|
|
3634
|
+
queueReadiness();
|
|
3635
|
+
resume();
|
|
2581
3636
|
},
|
|
2582
3637
|
observe: observing(watch),
|
|
2583
3638
|
async reauthorize() {
|
|
@@ -2604,6 +3659,22 @@ export function composeRuntime(options) {
|
|
|
2604
3659
|
query: (plan) => rowsQuery(plan),
|
|
2605
3660
|
watchPlan,
|
|
2606
3661
|
search: searched,
|
|
3662
|
+
watchReadiness,
|
|
3663
|
+
};
|
|
3664
|
+
return {
|
|
3665
|
+
md,
|
|
3666
|
+
seed,
|
|
3667
|
+
stop,
|
|
3668
|
+
lose: loseRooms,
|
|
3669
|
+
watch,
|
|
3670
|
+
acquire,
|
|
3671
|
+
persisted(answer) {
|
|
3672
|
+
if (answer === persistence)
|
|
3673
|
+
return;
|
|
3674
|
+
persistence = answer;
|
|
3675
|
+
journal.record(`persistence ${answer}`);
|
|
3676
|
+
queueReadiness();
|
|
3677
|
+
},
|
|
3678
|
+
within: (reach, port) => narrowed(reachable, reach, port),
|
|
2607
3679
|
};
|
|
2608
|
-
return { md, stop, lose: loseRooms, watch, within: (reach) => narrowed(reachable, reach) };
|
|
2609
3680
|
}
|