@agentshouse/mdruntime 0.1.0-c24bc09d750de → 0.1.0-c2682496ef6d0
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 +262 -262
- 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 +38 -9
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attempt.d.ts.map +1 -1
- package/dist/attempt.js +14 -1
- package/dist/bootstrap.d.ts +3 -2
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +32 -6
- 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/acts.d.ts +3 -4
- package/dist/chat/acts.d.ts.map +1 -1
- package/dist/chat/acts.js +10 -10
- 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 +8 -1
- package/dist/chat/list.d.ts.map +1 -1
- package/dist/chat/list.js +10 -6
- 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 +51 -67
- package/dist/chat/ready.d.ts +1 -0
- package/dist/chat/ready.d.ts.map +1 -1
- package/dist/chat/ready.js +4 -4
- package/dist/chat/style.js +3 -3
- package/dist/client.d.ts +12 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +12 -3
- 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 +24 -7
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1385 -325
- package/dist/conversation.d.ts +15 -7
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +368 -142
- package/dist/durability.d.ts +7 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +17 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +156 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/host.d.ts +17 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +86 -9
- 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 +9 -6
- 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 +29 -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 +5 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +22 -11
- package/dist/outcome.d.ts +4 -0
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +1 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +58 -55
- package/dist/query.d.ts +35 -3
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +8 -2
- package/dist/reach.d.ts +6 -3
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +17 -5
- package/dist/react.d.ts +7 -4
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +6 -4
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +285 -83
- 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 +1 -1
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +57 -20
- 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 +5 -13
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +5 -13
- package/dist/save.d.ts +6 -2
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +42 -18
- package/dist/search.d.ts +36 -6
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +132 -46
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +61 -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 +42 -11
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +276 -83
- package/dist/turn.d.ts +7 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +45 -0
- package/dist/wire.d.ts +136 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +48 -31
- package/package.json +6 -2
package/dist/connection.js
CHANGED
|
@@ -1,30 +1,46 @@
|
|
|
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, 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';
|
|
8
9
|
import { bootstrapRoom } from './bootstrap.js';
|
|
9
|
-
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
10
|
+
import { changeSet, entering, mergeChangeSets } from './change-set.js';
|
|
10
11
|
import { createConversationStore, } from './conversation.js';
|
|
11
12
|
import { queuedFile } from './change.js';
|
|
12
|
-
import { validRoot } from './collection.js';
|
|
13
|
+
import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
|
|
13
14
|
import { runtimeError } from './failure.js';
|
|
14
15
|
import { createLog } from './log.js';
|
|
15
16
|
import { binaryRecord } from './binary.js';
|
|
16
|
-
import { absentBinary, binaryAnswer, calledResult, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
|
|
17
|
+
import { absentBinary, binaryAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
|
|
18
|
+
import { createRecovery } from './recovery.js';
|
|
19
|
+
import { doorReads } from './reads.js';
|
|
17
20
|
import { refreshRoom } from './refresh.js';
|
|
18
21
|
import { buildBatch, joined, limitedIn, requeued } from './save.js';
|
|
19
22
|
import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, withdrawals, } from './replica.js';
|
|
20
23
|
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
21
24
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
22
25
|
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
23
|
-
import {
|
|
26
|
+
import { demanded, narrower, WHOLE_DEMAND, wider } from './acquisition.js';
|
|
27
|
+
import { readinessOf, SETTLED_ROOMS, } from './readiness.js';
|
|
28
|
+
import { FencedMap } from './fence.js';
|
|
29
|
+
import { tally } from './shared.js';
|
|
30
|
+
import { inChunks, ROW_WEIGHT, yielded } from './turn.js';
|
|
31
|
+
import { identityTime, roomDraftRevision } from './identity.js';
|
|
24
32
|
import { beneath, normalize } from './paths.js';
|
|
25
33
|
import { openRuntimeStore, } from './store.js';
|
|
26
34
|
import { unconfirmed } from './transfer.js';
|
|
27
35
|
import { governs, holds, narrowed, planReach, } from './reach.js';
|
|
36
|
+
function roomsAlone(plan) {
|
|
37
|
+
return (plan.reach === null &&
|
|
38
|
+
plan.root.types === null &&
|
|
39
|
+
plan.root.where === null &&
|
|
40
|
+
plan.root.pathPrefix === null &&
|
|
41
|
+
plan.hop === null &&
|
|
42
|
+
plan.backlinks === null);
|
|
43
|
+
}
|
|
28
44
|
function within(root, path) {
|
|
29
45
|
return root === '' || beneath(root, path);
|
|
30
46
|
}
|
|
@@ -38,25 +54,64 @@ function declaredSelection(room) {
|
|
|
38
54
|
root !== '' &&
|
|
39
55
|
selection.roots.every((other, at) => at === index || !within(other, root)));
|
|
40
56
|
}
|
|
57
|
+
const OPENING_LANE = 'opening';
|
|
58
|
+
const CONVERSATION_LANE = 'conversations';
|
|
59
|
+
const ROOM_READS = 256;
|
|
60
|
+
const ROOM_ATTEMPTS = 16;
|
|
61
|
+
const outside = new Map();
|
|
62
|
+
function outsideOf(roots) {
|
|
63
|
+
const key = JSON.stringify(roots);
|
|
64
|
+
const held = outside.get(key);
|
|
65
|
+
if (held !== undefined)
|
|
66
|
+
return held;
|
|
67
|
+
const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
|
|
68
|
+
const made = {
|
|
69
|
+
documents: (path) => (markdownPath(path) ? counted(path) : 0),
|
|
70
|
+
binaries: counted,
|
|
71
|
+
};
|
|
72
|
+
outside.set(key, made);
|
|
73
|
+
return made;
|
|
74
|
+
}
|
|
75
|
+
function abandonedOn(signal) {
|
|
76
|
+
return new Promise((resolve) => {
|
|
77
|
+
if (signal.aborted)
|
|
78
|
+
resolve(null);
|
|
79
|
+
else
|
|
80
|
+
signal.addEventListener('abort', () => resolve(null), { once: true });
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function roomLane(locator) {
|
|
84
|
+
return `room ${locator}`;
|
|
85
|
+
}
|
|
41
86
|
export function composeRuntime(options) {
|
|
42
87
|
const localSaves = options.adapter.localSaves === true;
|
|
43
88
|
let authorization = options.authorization;
|
|
44
89
|
let opening;
|
|
45
90
|
let store;
|
|
46
|
-
const
|
|
47
|
-
const
|
|
91
|
+
const fences = new Map();
|
|
92
|
+
const isFenced = (locator) => fences.has(locator);
|
|
93
|
+
let fencings = 0;
|
|
94
|
+
const lastFenced = new Map();
|
|
95
|
+
const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
|
|
96
|
+
let discoveries = 0;
|
|
97
|
+
const replicas = new FencedMap(isFenced);
|
|
98
|
+
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
48
99
|
const pendings = new Map();
|
|
49
|
-
const
|
|
100
|
+
const seeds = new Map();
|
|
101
|
+
const seedWatchers = new Map();
|
|
102
|
+
const roomDrafts = new FencedMap(isFenced);
|
|
50
103
|
const presentations = new Map();
|
|
51
104
|
let limits = [];
|
|
52
105
|
let capabilities = [];
|
|
53
|
-
const rooms = new
|
|
106
|
+
const rooms = new FencedMap(isFenced);
|
|
54
107
|
const counted = new Map();
|
|
55
108
|
const unreadable = new Set();
|
|
56
|
-
const
|
|
57
|
-
const
|
|
109
|
+
const isModelFenced = (key) => isFenced(key.slice(key.indexOf(' ') + 1));
|
|
110
|
+
const models = new FencedMap(isModelFenced);
|
|
111
|
+
const liveModels = new FencedMap(isModelFenced);
|
|
58
112
|
const invalidModels = new Set();
|
|
59
113
|
const discovered = new Map();
|
|
114
|
+
const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
|
|
60
115
|
let roomsListed = false;
|
|
61
116
|
const scopedRefusals = new Map();
|
|
62
117
|
const sourceRefusals = new Map();
|
|
@@ -84,7 +139,6 @@ export function composeRuntime(options) {
|
|
|
84
139
|
let status = STARTING;
|
|
85
140
|
let shown = STARTING;
|
|
86
141
|
let discoveryPaused = false;
|
|
87
|
-
let resetTimer;
|
|
88
142
|
let closed = false;
|
|
89
143
|
let stopping = false;
|
|
90
144
|
let withdrawStream;
|
|
@@ -92,12 +146,21 @@ export function composeRuntime(options) {
|
|
|
92
146
|
let underway = null;
|
|
93
147
|
let streamed = null;
|
|
94
148
|
let handed = false;
|
|
95
|
-
const
|
|
149
|
+
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
150
|
+
const repairing = new Set();
|
|
151
|
+
const repairable = new Map();
|
|
152
|
+
let outcomeRetention = null;
|
|
96
153
|
const answering = new Map();
|
|
97
154
|
let complete = false;
|
|
98
155
|
let recorded = false;
|
|
99
156
|
const stale = new Set();
|
|
100
|
-
let
|
|
157
|
+
let generation = 0;
|
|
158
|
+
let discovering = null;
|
|
159
|
+
let opened = null;
|
|
160
|
+
const owed = new Set();
|
|
161
|
+
const lanes = new Map();
|
|
162
|
+
let roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
163
|
+
const working = new Set();
|
|
101
164
|
let mutations = Promise.resolve();
|
|
102
165
|
const roomListeners = new Set();
|
|
103
166
|
const refusalListeners = new Set();
|
|
@@ -108,7 +171,7 @@ export function composeRuntime(options) {
|
|
|
108
171
|
const presentationListeners = new Set();
|
|
109
172
|
const readSide = createReadSide();
|
|
110
173
|
let withdrawConversations;
|
|
111
|
-
let currencyQueued =
|
|
174
|
+
let currencyQueued = null;
|
|
112
175
|
let socketOpen = false;
|
|
113
176
|
let serviceMode = 'open';
|
|
114
177
|
let haltedRefusal = false;
|
|
@@ -116,24 +179,41 @@ export function composeRuntime(options) {
|
|
|
116
179
|
const serviceModeListeners = new Set();
|
|
117
180
|
let member = null;
|
|
118
181
|
const memberListeners = new Set();
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
const
|
|
182
|
+
const prepared = new Set();
|
|
183
|
+
const projecting = new Set();
|
|
184
|
+
const unpreparedDemand = new Set();
|
|
185
|
+
const unindexable = new Set();
|
|
186
|
+
let preparing = false;
|
|
187
|
+
let demand = WHOLE_DEMAND;
|
|
188
|
+
let narrowing = null;
|
|
189
|
+
let targets = {};
|
|
190
|
+
let persistence = options.shell.persistence;
|
|
191
|
+
let capacity = null;
|
|
192
|
+
const paused = new Set();
|
|
193
|
+
let rechecking = null;
|
|
194
|
+
const readinessWatches = new Set();
|
|
195
|
+
let readinessQueued = false;
|
|
196
|
+
const catching = new Set();
|
|
197
|
+
const transfers = new FencedMap(isFenced);
|
|
122
198
|
const conversations = createConversationStore({
|
|
123
199
|
agents: declaredAgents,
|
|
124
|
-
documents: (locator) => rowsQuery(planOf({ rooms: [locator], select: 'all' }, roomFacts())),
|
|
200
|
+
documents: ({ locator }) => rowsQuery(planOf({ rooms: [locator], select: { fields: 'all' } }, roomFacts())),
|
|
125
201
|
call: (name, input) => invokeAction(name, input),
|
|
126
202
|
durably,
|
|
127
203
|
stream: () => options.adapter.conversations,
|
|
128
204
|
publish: publishChangeSet,
|
|
205
|
+
online: () => !status.providerUnreachable && refusal === null,
|
|
129
206
|
closed: () => closed,
|
|
207
|
+
unanswered: resendLater,
|
|
208
|
+
expired: (operation) => pastHorizon(operation),
|
|
130
209
|
});
|
|
131
210
|
const searchIndex = createSearchIndex({
|
|
132
|
-
|
|
211
|
+
room: (room) => readSide.room(room),
|
|
133
212
|
document: (room, path) => readSide.document(room, path),
|
|
134
|
-
messages: () => conversations.messages(),
|
|
135
|
-
message: (conversation, identity) => conversations.message(conversation, identity),
|
|
136
213
|
});
|
|
214
|
+
const roomsToIndex = new Set();
|
|
215
|
+
const conversationsToIndex = new Set();
|
|
216
|
+
let indexing = false;
|
|
137
217
|
const journal = createLog();
|
|
138
218
|
let notifyReady = () => { };
|
|
139
219
|
const ready = new Promise((resolve) => {
|
|
@@ -212,9 +292,16 @@ export function composeRuntime(options) {
|
|
|
212
292
|
returned: structuredClone(record.returned),
|
|
213
293
|
});
|
|
214
294
|
}
|
|
295
|
+
for (const [room, refusal] of sourceRefusals)
|
|
296
|
+
attentionRequired.push({ room, refusal: structuredClone(refusal) });
|
|
215
297
|
attentionRequired.sort((left, right) => {
|
|
216
298
|
if (left.room !== right.room)
|
|
217
299
|
return left.room < right.room ? -1 : 1;
|
|
300
|
+
if ('refusal' in left || 'refusal' in right) {
|
|
301
|
+
if ('refusal' in left && 'refusal' in right)
|
|
302
|
+
return 0;
|
|
303
|
+
return 'refusal' in left ? -1 : 1;
|
|
304
|
+
}
|
|
218
305
|
if (left.path !== right.path)
|
|
219
306
|
return left.path < right.path ? -1 : 1;
|
|
220
307
|
return 0;
|
|
@@ -233,12 +320,9 @@ export function composeRuntime(options) {
|
|
|
233
320
|
if (replica === undefined)
|
|
234
321
|
return 0;
|
|
235
322
|
const roots = replica.confirmed.record.typedSelection?.roots ?? [];
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
count += 1;
|
|
240
|
-
}
|
|
241
|
-
return count;
|
|
323
|
+
const weighed = outsideOf(roots);
|
|
324
|
+
return (tally(replica.visible.documents, weighed.documents) +
|
|
325
|
+
tally(replica.visible.binaries, weighed.binaries));
|
|
242
326
|
}
|
|
243
327
|
function listed() {
|
|
244
328
|
return structuredClone([...rooms.values()].map((room) => {
|
|
@@ -265,15 +349,19 @@ export function composeRuntime(options) {
|
|
|
265
349
|
return { listed: roomsListed, rooms: held };
|
|
266
350
|
}
|
|
267
351
|
function emitRooms() {
|
|
352
|
+
readSide.recover();
|
|
353
|
+
queueReadiness();
|
|
268
354
|
const value = listed();
|
|
269
355
|
for (const listener of roomListeners)
|
|
270
356
|
listener(value);
|
|
271
357
|
}
|
|
272
358
|
function emitRefusal() {
|
|
359
|
+
queueReadiness();
|
|
273
360
|
for (const listener of refusalListeners)
|
|
274
361
|
listener(refusal);
|
|
275
362
|
}
|
|
276
363
|
function emitStorageLoss() {
|
|
364
|
+
queueReadiness();
|
|
277
365
|
for (const listener of storageLossListeners)
|
|
278
366
|
listener(storageLoss);
|
|
279
367
|
}
|
|
@@ -290,7 +378,8 @@ export function composeRuntime(options) {
|
|
|
290
378
|
function show() {
|
|
291
379
|
if (closed)
|
|
292
380
|
return;
|
|
293
|
-
|
|
381
|
+
queueReadiness();
|
|
382
|
+
const next = status.state === 'synced' && demand.conversations && !conversations.current()
|
|
294
383
|
? { ...status, state: 'syncing' }
|
|
295
384
|
: status;
|
|
296
385
|
if (same(shown, next))
|
|
@@ -319,42 +408,511 @@ export function composeRuntime(options) {
|
|
|
319
408
|
}
|
|
320
409
|
}
|
|
321
410
|
function applyChangeSet(set) {
|
|
322
|
-
const held =
|
|
323
|
-
|
|
324
|
-
|
|
411
|
+
const held = {
|
|
412
|
+
...set,
|
|
413
|
+
documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
|
|
414
|
+
};
|
|
325
415
|
readSide.apply(held);
|
|
326
416
|
searchIndex.changed(held);
|
|
327
417
|
}
|
|
328
|
-
function
|
|
329
|
-
|
|
418
|
+
async function referenced(locator) {
|
|
419
|
+
for (;;) {
|
|
420
|
+
const record = replicas.get(locator)?.confirmed.record;
|
|
421
|
+
if (record === undefined || closed)
|
|
422
|
+
return;
|
|
423
|
+
const held = { text: record.documents, binaries: record.binaries };
|
|
424
|
+
const composition = compositionOf(record);
|
|
425
|
+
if (referencesIndexed(held, composition))
|
|
426
|
+
return;
|
|
427
|
+
await indexReferrers(held, composition);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async function prepareReplica(locator) {
|
|
431
|
+
await referenced(locator);
|
|
432
|
+
const replica = replicas.get(locator);
|
|
433
|
+
if (replica === undefined || closed || prepared.has(locator))
|
|
330
434
|
return;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
435
|
+
const visible = replica.visible;
|
|
436
|
+
const paths = [
|
|
437
|
+
...new Set([...Object.keys(visible.documents), ...Object.keys(visible.binaries)]),
|
|
438
|
+
];
|
|
439
|
+
const entered = [];
|
|
440
|
+
projecting.add(locator);
|
|
441
|
+
try {
|
|
442
|
+
await inChunks(paths, (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
|
|
443
|
+
const current = replicas.get(locator);
|
|
444
|
+
if (current === undefined || closed)
|
|
445
|
+
return;
|
|
446
|
+
const set = entering(locator, current, chunk);
|
|
447
|
+
readSide.apply(set);
|
|
448
|
+
entered.push(set);
|
|
449
|
+
});
|
|
450
|
+
if (!replicas.has(locator) || closed)
|
|
451
|
+
return;
|
|
452
|
+
prepared.add(locator);
|
|
341
453
|
}
|
|
342
|
-
|
|
454
|
+
finally {
|
|
455
|
+
projecting.delete(locator);
|
|
456
|
+
}
|
|
457
|
+
conversations.changed(mergeChangeSets(entered));
|
|
458
|
+
readSide.recover();
|
|
459
|
+
queueReadiness();
|
|
460
|
+
}
|
|
461
|
+
function unprepared() {
|
|
462
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !prepared.has(locator) && !unindexable.has(locator));
|
|
463
|
+
}
|
|
464
|
+
function unbuilt(locator, error) {
|
|
465
|
+
unindexable.add(locator);
|
|
466
|
+
journal.record(`index failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
467
|
+
queueReadiness();
|
|
468
|
+
}
|
|
469
|
+
function prepare(locators = []) {
|
|
470
|
+
for (const locator of locators)
|
|
471
|
+
unpreparedDemand.add(locator);
|
|
472
|
+
if (preparing)
|
|
343
473
|
return;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
474
|
+
preparing = true;
|
|
475
|
+
void (async () => {
|
|
476
|
+
try {
|
|
477
|
+
for (let waiting = unprepared(); waiting !== undefined; waiting = unprepared()) {
|
|
478
|
+
await yielded();
|
|
479
|
+
if (closed)
|
|
480
|
+
return;
|
|
481
|
+
unpreparedDemand.delete(waiting);
|
|
482
|
+
await prepareReplica(waiting).catch((error) => unbuilt(waiting, error));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
finally {
|
|
486
|
+
preparing = false;
|
|
487
|
+
}
|
|
488
|
+
})();
|
|
347
489
|
}
|
|
348
490
|
function reachedRooms(plan) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
491
|
+
const reach = plan.reach;
|
|
492
|
+
const reached = reach === null ? null : new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
|
|
493
|
+
const named = plan.rooms ??
|
|
494
|
+
(reached === null
|
|
495
|
+
? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
|
|
496
|
+
: [...reached]);
|
|
497
|
+
return [...new Set(named)].filter((locator) => reached === null || reached.has(locator)).sort();
|
|
498
|
+
}
|
|
499
|
+
function awaited(locator) {
|
|
500
|
+
return (!roomsListed ||
|
|
501
|
+
(rooms.has(locator) && discovered.get(locator)?.replica.admission === 'admitted'));
|
|
502
|
+
}
|
|
503
|
+
function covered(plan) {
|
|
504
|
+
const locators = reachedRooms(plan);
|
|
505
|
+
if (locators.length === 0 && plan.rooms === null && plan.reach === null && !roomsListed) {
|
|
506
|
+
return { pending: [] };
|
|
507
|
+
}
|
|
508
|
+
const pending = [];
|
|
509
|
+
const coverage = [];
|
|
510
|
+
const overlay = [];
|
|
511
|
+
for (const locator of locators) {
|
|
512
|
+
const replica = replicas.get(locator);
|
|
513
|
+
if (replica === undefined) {
|
|
514
|
+
if (awaited(locator))
|
|
515
|
+
pending.push({ room: locator, awaits: 'acquisition' });
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (!prepared.has(locator)) {
|
|
519
|
+
pending.push({ room: locator, awaits: 'preparation' });
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
coverage.push({
|
|
523
|
+
room: locator,
|
|
524
|
+
generation: replica.confirmed.record.transfer,
|
|
525
|
+
current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
|
|
526
|
+
});
|
|
527
|
+
for (const batch of replica.queue)
|
|
528
|
+
overlay.push(batch.id);
|
|
529
|
+
}
|
|
530
|
+
if (pending.length > 0)
|
|
531
|
+
return { pending };
|
|
532
|
+
return { coverage: { rooms: coverage, indexes: [], overlay } };
|
|
533
|
+
}
|
|
534
|
+
function awaitPreparation(cover) {
|
|
535
|
+
if (!('pending' in cover))
|
|
536
|
+
return;
|
|
537
|
+
const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
|
|
538
|
+
if (waiting.length > 0)
|
|
539
|
+
prepare(waiting.map(({ room }) => room));
|
|
540
|
+
}
|
|
541
|
+
function acquires(room) {
|
|
542
|
+
return (demanded(demand, room) ||
|
|
543
|
+
(replicas.get(room.locator)?.queue.length ?? 0) > 0 ||
|
|
544
|
+
queuesWithoutReplica.has(room.locator));
|
|
545
|
+
}
|
|
546
|
+
function acquiredLocators() {
|
|
547
|
+
return [...discovered.values()]
|
|
548
|
+
.filter(acquires)
|
|
549
|
+
.map((room) => room.locator)
|
|
550
|
+
.sort();
|
|
551
|
+
}
|
|
552
|
+
function heldDemand(acquired) {
|
|
553
|
+
if (demand.whole)
|
|
352
554
|
return null;
|
|
353
|
-
return
|
|
555
|
+
return { rooms: acquired.map((room) => room.locator), conversations: demand.conversations };
|
|
556
|
+
}
|
|
557
|
+
function applyDemand(next) {
|
|
558
|
+
const before = acquiredLocators();
|
|
559
|
+
const conversed = demand.conversations;
|
|
560
|
+
demand = next;
|
|
561
|
+
const after = acquiredLocators();
|
|
562
|
+
queueReadiness();
|
|
563
|
+
if (!conversed && next.conversations)
|
|
564
|
+
scheduleCurrency();
|
|
565
|
+
if (JSON.stringify(before) === JSON.stringify(after) && conversed === next.conversations)
|
|
566
|
+
return;
|
|
567
|
+
journal.record(`acquisition ${next.whole ? 'whole' : after.length}`);
|
|
568
|
+
if (options.adapter.roomStream === undefined)
|
|
569
|
+
void sync('unstreamed');
|
|
570
|
+
else if (socketOpen)
|
|
571
|
+
options.adapter.roomStream.reopen();
|
|
572
|
+
}
|
|
573
|
+
function acquire(next) {
|
|
574
|
+
if (closed)
|
|
575
|
+
return;
|
|
576
|
+
if (narrowing !== null)
|
|
577
|
+
clearTimeout(narrowing);
|
|
578
|
+
narrowing = null;
|
|
579
|
+
const widening = wider(next, demand);
|
|
580
|
+
if (!widening && !narrower(next, demand))
|
|
581
|
+
return;
|
|
582
|
+
if (widening || !roomsListed) {
|
|
583
|
+
applyDemand(next);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
// A narrower demand waits five seconds, so a tab that reloads returns before the stream reopens twice.
|
|
587
|
+
narrowing = setTimeout(() => {
|
|
588
|
+
narrowing = null;
|
|
589
|
+
applyDemand(next);
|
|
590
|
+
}, 5_000);
|
|
591
|
+
}
|
|
592
|
+
function requiredRooms(reach) {
|
|
593
|
+
if (reach === null)
|
|
594
|
+
return { listed: roomsListed, rooms: [...rooms.keys()].sort() };
|
|
595
|
+
const named = [...new Set(reach.rooms)].sort();
|
|
596
|
+
if (roomsListed)
|
|
597
|
+
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
598
|
+
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
599
|
+
}
|
|
600
|
+
function roomState(locator, search) {
|
|
601
|
+
if (sourceRefusals.has(locator))
|
|
602
|
+
return 'refused';
|
|
603
|
+
const listedRoom = discovered.get(locator);
|
|
604
|
+
if (roomsListed &&
|
|
605
|
+
(listedRoom === undefined ||
|
|
606
|
+
listedRoom.replica.admission !== 'admitted' ||
|
|
607
|
+
unreadable.has(locator)))
|
|
608
|
+
return 'unadmitted';
|
|
609
|
+
if (!replicas.has(locator))
|
|
610
|
+
return 'acquiring';
|
|
611
|
+
if (!prepared.has(locator))
|
|
612
|
+
return 'preparing';
|
|
613
|
+
if (search && !searchIndex.rooms().has(locator))
|
|
614
|
+
return 'indexing';
|
|
615
|
+
return 'complete';
|
|
616
|
+
}
|
|
617
|
+
function conversationCoverage(reach, search) {
|
|
618
|
+
const declared = reach === null || reach.conversations;
|
|
619
|
+
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
620
|
+
return 'undeclared';
|
|
621
|
+
if (!conversations.known() || !conversations.current())
|
|
622
|
+
return 'acquiring';
|
|
623
|
+
if (!search)
|
|
624
|
+
return 'complete';
|
|
625
|
+
const unindexed = conversations
|
|
626
|
+
.searchable()
|
|
627
|
+
.filter((one) => conversations.acquired(one) && !searchIndex.conversations().has(one));
|
|
628
|
+
if (unindexed.length === 0)
|
|
629
|
+
return 'complete';
|
|
630
|
+
index([], unindexed);
|
|
631
|
+
return 'indexing';
|
|
632
|
+
}
|
|
633
|
+
function connectionOf() {
|
|
634
|
+
if (socketOpen)
|
|
635
|
+
return 'connected';
|
|
636
|
+
if (status.providerUnreachable)
|
|
637
|
+
return 'unreachable';
|
|
638
|
+
return options.adapter.roomStream === undefined && status.lastCompleted !== null
|
|
639
|
+
? 'connected'
|
|
640
|
+
: 'connecting';
|
|
641
|
+
}
|
|
642
|
+
function refusedIn(locators) {
|
|
643
|
+
return locators.flatMap((room) => {
|
|
644
|
+
const held = sourceRefusals.get(room);
|
|
645
|
+
if (held === undefined)
|
|
646
|
+
return [];
|
|
647
|
+
const { file, location } = held.fields;
|
|
648
|
+
return [
|
|
649
|
+
{
|
|
650
|
+
room,
|
|
651
|
+
file: typeof file === 'string' ? file : '',
|
|
652
|
+
rule: held.code,
|
|
653
|
+
location: typeof location === 'string' ? location : null,
|
|
654
|
+
},
|
|
655
|
+
];
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
function readinessFor(port, reach) {
|
|
659
|
+
const required = requiredRooms(reach);
|
|
660
|
+
const search = port.search === 'required';
|
|
661
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room, search) }));
|
|
662
|
+
const building = (state) => coverage
|
|
663
|
+
.filter((room) => room.state === state && !unindexable.has(room.room))
|
|
664
|
+
.map(({ room }) => room);
|
|
665
|
+
const preparing = building('preparing');
|
|
666
|
+
if (preparing.length > 0)
|
|
667
|
+
prepare(preparing);
|
|
668
|
+
const indexing = building('indexing');
|
|
669
|
+
if (indexing.length > 0)
|
|
670
|
+
index(indexing, []);
|
|
671
|
+
return readinessOf({
|
|
672
|
+
code: port.code,
|
|
673
|
+
coverage: {
|
|
674
|
+
listed: required.listed,
|
|
675
|
+
rooms: coverage,
|
|
676
|
+
conversations: conversationCoverage(reach, search),
|
|
677
|
+
search: search ? 'required' : 'unused',
|
|
678
|
+
},
|
|
679
|
+
persistence,
|
|
680
|
+
connection: connectionOf(),
|
|
681
|
+
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
682
|
+
capacity: capacity !== null &&
|
|
683
|
+
coverage.some(({ room, state }) => paused.has(room) || !SETTLED_ROOMS.has(state))
|
|
684
|
+
? capacity
|
|
685
|
+
: port.capacity,
|
|
686
|
+
refused: refusedIn(required.rooms),
|
|
687
|
+
lost: storageLoss !== null,
|
|
688
|
+
established: targets[port.target]?.established === true,
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
function establish(name) {
|
|
692
|
+
targets = { ...targets, [name]: { established: true } };
|
|
693
|
+
queueReadiness();
|
|
694
|
+
void durably((local) => local.keepTargets(targets)).catch(() => {
|
|
695
|
+
journal.record(`target record failed ${name}`);
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
function deliverReadiness(watch) {
|
|
699
|
+
if (!readinessWatches.has(watch))
|
|
700
|
+
return;
|
|
701
|
+
const readiness = readinessFor(watch.port, watch.reach);
|
|
702
|
+
if (readiness.ready && targets[watch.port.target]?.established !== true) {
|
|
703
|
+
establish(watch.port.target);
|
|
704
|
+
}
|
|
705
|
+
const shown = JSON.stringify(readiness);
|
|
706
|
+
if (shown === watch.last)
|
|
707
|
+
return;
|
|
708
|
+
watch.last = shown;
|
|
709
|
+
watch.deliver(structuredClone(readiness));
|
|
710
|
+
}
|
|
711
|
+
function queueReadiness() {
|
|
712
|
+
if (readinessQueued || closed || readinessWatches.size === 0)
|
|
713
|
+
return;
|
|
714
|
+
readinessQueued = true;
|
|
715
|
+
queueMicrotask(() => {
|
|
716
|
+
readinessQueued = false;
|
|
717
|
+
if (closed)
|
|
718
|
+
return;
|
|
719
|
+
for (const watch of [...readinessWatches])
|
|
720
|
+
deliverReadiness(watch);
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
function watchReadiness(port, reach, deliver) {
|
|
724
|
+
const watch = { port, reach, deliver, last: null };
|
|
725
|
+
readinessWatches.add(watch);
|
|
726
|
+
void openLocal().then(() => deliverReadiness(watch), () => deliverReadiness(watch));
|
|
727
|
+
return () => {
|
|
728
|
+
readinessWatches.delete(watch);
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
function layerBytes(layer) {
|
|
732
|
+
let bytes = 0;
|
|
733
|
+
for (const [path, text] of Object.entries(layer.documents))
|
|
734
|
+
bytes += (path.length + text.length) * 2;
|
|
735
|
+
return bytes;
|
|
736
|
+
}
|
|
737
|
+
function grownBy(next, previous) {
|
|
738
|
+
if (previous === undefined)
|
|
739
|
+
return layerBytes(next);
|
|
740
|
+
const documents = Object.fromEntries(Object.entries(next.documents).filter(([path, text]) => previous.documents[path] !== text));
|
|
741
|
+
return layerBytes({ ...next, documents });
|
|
742
|
+
}
|
|
743
|
+
function exceeded(error) {
|
|
744
|
+
return (typeof error === 'object' &&
|
|
745
|
+
error !== null &&
|
|
746
|
+
error.name === 'QuotaExceededError');
|
|
747
|
+
}
|
|
748
|
+
async function estimated() {
|
|
749
|
+
const estimate = options.shell.estimate === undefined ? null : await options.shell.estimate();
|
|
750
|
+
return estimate === null ? null : Math.max(0, estimate.quota - estimate.usage);
|
|
751
|
+
}
|
|
752
|
+
function pause(locator, cause) {
|
|
753
|
+
paused.add(locator);
|
|
754
|
+
capacity = cause;
|
|
755
|
+
journal.record(`capacity ${cause.cause} ${locator} ${cause.needed} ${cause.available ?? '-'}`);
|
|
756
|
+
queueReadiness();
|
|
757
|
+
if (rechecking !== null)
|
|
758
|
+
return;
|
|
759
|
+
// A paused preparation measures the browser's space again each minute, so space freed elsewhere resumes it without a retry.
|
|
760
|
+
rechecking = setInterval(() => {
|
|
761
|
+
void recheck();
|
|
762
|
+
}, 60_000);
|
|
763
|
+
}
|
|
764
|
+
async function recheck() {
|
|
765
|
+
const held = capacity;
|
|
766
|
+
if (held === null || closed)
|
|
767
|
+
return;
|
|
768
|
+
const available = await estimated().catch(() => null);
|
|
769
|
+
if (available === null || available < (held.needed ?? 0))
|
|
770
|
+
return;
|
|
771
|
+
if (held.cause === 'quota-exceeded' && (held.available === null || available <= held.available))
|
|
772
|
+
return;
|
|
773
|
+
resume();
|
|
774
|
+
}
|
|
775
|
+
function resume() {
|
|
776
|
+
if (rechecking !== null)
|
|
777
|
+
clearInterval(rechecking);
|
|
778
|
+
rechecking = null;
|
|
779
|
+
if (paused.size === 0 && capacity === null)
|
|
780
|
+
return;
|
|
781
|
+
paused.clear();
|
|
782
|
+
capacity = null;
|
|
783
|
+
journal.record('capacity resumed');
|
|
784
|
+
queueReadiness();
|
|
785
|
+
if (!closed && authorization !== undefined)
|
|
786
|
+
void sync('unstreamed');
|
|
787
|
+
}
|
|
788
|
+
async function reclaimed(local) {
|
|
789
|
+
const unreferenced = [...transfers.keys()].filter((locator) => {
|
|
790
|
+
const room = discovered.get(locator);
|
|
791
|
+
return room === undefined || !acquires(room);
|
|
792
|
+
});
|
|
793
|
+
for (const locator of unreferenced) {
|
|
794
|
+
await local.abandon(locator);
|
|
795
|
+
transfers.delete(locator);
|
|
796
|
+
}
|
|
797
|
+
if (unreferenced.length > 0)
|
|
798
|
+
journal.record(`capacity reclaimed ${unreferenced.length}`);
|
|
799
|
+
return unreferenced.length > 0;
|
|
800
|
+
}
|
|
801
|
+
async function admitted(local, locator, needed) {
|
|
802
|
+
if (paused.has(locator))
|
|
803
|
+
return false;
|
|
804
|
+
let available = await estimated().catch(() => null);
|
|
805
|
+
if (available === null || available >= needed)
|
|
806
|
+
return true;
|
|
807
|
+
if (await reclaimed(local))
|
|
808
|
+
available = await estimated().catch(() => null);
|
|
809
|
+
if (available === null || available >= needed)
|
|
810
|
+
return true;
|
|
811
|
+
pause(locator, { cause: 'insufficient-space', needed, available });
|
|
812
|
+
return false;
|
|
354
813
|
}
|
|
355
|
-
function
|
|
356
|
-
|
|
357
|
-
|
|
814
|
+
async function exceededOn(locator, bytes) {
|
|
815
|
+
const available = await estimated().catch(() => null);
|
|
816
|
+
pause(locator, { cause: 'quota-exceeded', needed: bytes, available });
|
|
817
|
+
}
|
|
818
|
+
function seedRoomHeld(room, version) {
|
|
819
|
+
return prepared.has(room) && reachedBy(room, version);
|
|
820
|
+
}
|
|
821
|
+
function seedHeld(at) {
|
|
822
|
+
return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
|
|
823
|
+
}
|
|
824
|
+
function seedApplies(plan) {
|
|
825
|
+
const seed = seeds.get(plan.key);
|
|
826
|
+
if (seed === undefined)
|
|
827
|
+
return null;
|
|
828
|
+
if (!seedHeld(seed.at))
|
|
829
|
+
return seed;
|
|
830
|
+
seeds.delete(plan.key);
|
|
831
|
+
return null;
|
|
832
|
+
}
|
|
833
|
+
function seedBehind(plan, cover) {
|
|
834
|
+
const seed = seedApplies(plan);
|
|
835
|
+
if (seed === null || 'pending' in cover)
|
|
836
|
+
return cover;
|
|
837
|
+
return {
|
|
838
|
+
pending: Object.entries(seed.at)
|
|
839
|
+
.filter(([room, version]) => !seedRoomHeld(room, version))
|
|
840
|
+
.map(([room]) => ({ room, awaits: 'acquisition' })),
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
function seed(entry) {
|
|
844
|
+
if (seedHeld(entry.at))
|
|
845
|
+
return;
|
|
846
|
+
for (const { query, rows } of entry.queries) {
|
|
847
|
+
const key = queryKey(query);
|
|
848
|
+
seeds.set(key, { rows, at: entry.at });
|
|
849
|
+
for (const shown of seedWatchers.get(key) ?? [])
|
|
850
|
+
shown();
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
async function rowsQuery(plan) {
|
|
854
|
+
const before = covered(plan);
|
|
855
|
+
awaitPreparation(before);
|
|
856
|
+
const seeded = seedApplies(plan);
|
|
857
|
+
if (seeded !== null)
|
|
858
|
+
return seededResult(seeded).result;
|
|
859
|
+
if ('pending' in before)
|
|
860
|
+
return before;
|
|
861
|
+
const { rows, at: cover } = await readSide.query(plan, () => covered(plan));
|
|
862
|
+
awaitPreparation(cover);
|
|
863
|
+
if ('pending' in cover)
|
|
864
|
+
return cover;
|
|
865
|
+
return 'groups' in rows
|
|
866
|
+
? { groups: rows.groups, coverage: cover.coverage }
|
|
867
|
+
: { documents: rows.documents, coverage: cover.coverage };
|
|
868
|
+
}
|
|
869
|
+
async function indexConversation(conversation) {
|
|
870
|
+
searchIndex.indexMessages(conversation, [], false);
|
|
871
|
+
let after = null;
|
|
872
|
+
for (;;) {
|
|
873
|
+
const batch = await durably((local) => local.messagesAfter(conversation, after));
|
|
874
|
+
if (closed)
|
|
875
|
+
return;
|
|
876
|
+
searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
|
|
877
|
+
if (batch.length === 0)
|
|
878
|
+
return;
|
|
879
|
+
after = batch[batch.length - 1].identity;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function indexNext() {
|
|
883
|
+
const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator) && !unindexable.has(locator));
|
|
884
|
+
if (room !== undefined) {
|
|
885
|
+
roomsToIndex.delete(room);
|
|
886
|
+
return () => prepared.has(room)
|
|
887
|
+
? searchIndex.indexRoom(room).catch((error) => unbuilt(room, error))
|
|
888
|
+
: undefined;
|
|
889
|
+
}
|
|
890
|
+
const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
|
|
891
|
+
if (conversation === undefined)
|
|
892
|
+
return null;
|
|
893
|
+
conversationsToIndex.delete(conversation);
|
|
894
|
+
return () => indexConversation(conversation);
|
|
895
|
+
}
|
|
896
|
+
function index(rooms, named) {
|
|
897
|
+
for (const room of rooms)
|
|
898
|
+
roomsToIndex.add(room);
|
|
899
|
+
for (const conversation of named)
|
|
900
|
+
conversationsToIndex.add(conversation);
|
|
901
|
+
if (indexing)
|
|
902
|
+
return;
|
|
903
|
+
indexing = true;
|
|
904
|
+
void (async () => {
|
|
905
|
+
try {
|
|
906
|
+
for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
|
|
907
|
+
await yielded();
|
|
908
|
+
await unit();
|
|
909
|
+
queueReadiness();
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
finally {
|
|
913
|
+
indexing = false;
|
|
914
|
+
}
|
|
915
|
+
})().catch(() => { });
|
|
358
916
|
}
|
|
359
917
|
function publishChangeSet(set) {
|
|
360
918
|
applyChangeSet(set);
|
|
@@ -371,34 +929,7 @@ export function composeRuntime(options) {
|
|
|
371
929
|
emitPresentation(app);
|
|
372
930
|
conversations.changed(set);
|
|
373
931
|
}
|
|
374
|
-
function stopTimer() {
|
|
375
|
-
if (resetTimer === undefined)
|
|
376
|
-
return;
|
|
377
|
-
clearTimeout(resetTimer);
|
|
378
|
-
resetTimer = undefined;
|
|
379
|
-
}
|
|
380
|
-
function armTimer() {
|
|
381
|
-
stopTimer();
|
|
382
|
-
if (closed || stopping)
|
|
383
|
-
return;
|
|
384
|
-
let earliest = null;
|
|
385
|
-
for (const { submission } of pendings.values()) {
|
|
386
|
-
if (submission.state !== 'reset')
|
|
387
|
-
continue;
|
|
388
|
-
const at = Date.parse(submission.reset);
|
|
389
|
-
if (earliest === null || at < earliest)
|
|
390
|
-
earliest = at;
|
|
391
|
-
}
|
|
392
|
-
if (earliest === null)
|
|
393
|
-
return;
|
|
394
|
-
// A Save House held back goes again only once the reset its answer named has passed.
|
|
395
|
-
resetTimer = setTimeout(() => {
|
|
396
|
-
resetTimer = undefined;
|
|
397
|
-
submitAll();
|
|
398
|
-
}, Math.max(0, earliest - Date.now()));
|
|
399
|
-
}
|
|
400
932
|
async function refuse(reason, provider = null) {
|
|
401
|
-
stopTimer();
|
|
402
933
|
discoveryPaused = true;
|
|
403
934
|
try {
|
|
404
935
|
if (reason === 'revoked-authorization') {
|
|
@@ -477,14 +1008,27 @@ export function composeRuntime(options) {
|
|
|
477
1008
|
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
478
1009
|
});
|
|
479
1010
|
}
|
|
480
|
-
async function applyDiscovery(
|
|
1011
|
+
async function applyDiscovery(found, started) {
|
|
1012
|
+
for (const room of found) {
|
|
1013
|
+
const fence = fences.get(room.locator);
|
|
1014
|
+
if (fence !== undefined && fence.erased && fence.at < started)
|
|
1015
|
+
fences.delete(room.locator);
|
|
1016
|
+
}
|
|
1017
|
+
await loseRooms(found
|
|
1018
|
+
.filter((room) => room.replica.admission !== 'admitted' && heldLocally(room.locator))
|
|
1019
|
+
.map((room) => room.locator));
|
|
1020
|
+
const listed = found.filter((room) => !fences.has(room.locator));
|
|
481
1021
|
const listedLocators = new Set(listed.map((room) => room.locator));
|
|
482
1022
|
for (const locator of listedLocators)
|
|
483
1023
|
scopedRefusals.delete(locator);
|
|
484
1024
|
discovered.clear();
|
|
485
1025
|
for (const room of listed)
|
|
486
1026
|
discovered.set(room.locator, room);
|
|
487
|
-
|
|
1027
|
+
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
|
|
1028
|
+
if (options.adapter.exhaustiveDiscovery === true)
|
|
1029
|
+
await loseRooms(unlisted);
|
|
1030
|
+
else
|
|
1031
|
+
unlistedKept(unlisted);
|
|
488
1032
|
for (const room of listed) {
|
|
489
1033
|
const held = replicas.get(room.locator)?.confirmed.record;
|
|
490
1034
|
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
@@ -495,6 +1039,13 @@ export function composeRuntime(options) {
|
|
|
495
1039
|
}
|
|
496
1040
|
roomsListed = true;
|
|
497
1041
|
emitRooms();
|
|
1042
|
+
return listed;
|
|
1043
|
+
}
|
|
1044
|
+
function heldLocally(locator) {
|
|
1045
|
+
return (replicas.has(locator) ||
|
|
1046
|
+
roomDrafts.has(locator) ||
|
|
1047
|
+
transfers.has(locator) ||
|
|
1048
|
+
queuesWithoutReplica.has(locator));
|
|
498
1049
|
}
|
|
499
1050
|
function unlistedHeld(listed) {
|
|
500
1051
|
return [
|
|
@@ -506,41 +1057,73 @@ export function composeRuntime(options) {
|
|
|
506
1057
|
]),
|
|
507
1058
|
].filter((locator) => !listed.has(locator));
|
|
508
1059
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
const removed = unlisted.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
|
|
516
|
-
room: true,
|
|
517
|
-
queue: true,
|
|
518
|
-
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
519
|
-
}));
|
|
520
|
-
for (const locator of unlisted) {
|
|
521
|
-
for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
|
|
522
|
-
pendings.delete(batch.id);
|
|
523
|
-
replicas.delete(locator);
|
|
524
|
-
queuesWithoutReplica.delete(locator);
|
|
1060
|
+
function unlistedKept(unlisted) {
|
|
1061
|
+
for (const locator of unlisted) {
|
|
1062
|
+
const held = replicas.get(locator)?.confirmed.record;
|
|
1063
|
+
if (held !== undefined)
|
|
1064
|
+
list(held, false);
|
|
1065
|
+
else
|
|
525
1066
|
rooms.delete(locator);
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
function fence(locators) {
|
|
1070
|
+
const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
|
|
1071
|
+
const removed = fresh.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
|
|
1072
|
+
room: true,
|
|
1073
|
+
queue: true,
|
|
1074
|
+
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
1075
|
+
}));
|
|
1076
|
+
for (const locator of fresh) {
|
|
1077
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
1078
|
+
lastFenced.set(locator, ++fencings);
|
|
1079
|
+
for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
|
|
1080
|
+
pendings.delete(batch.id);
|
|
1081
|
+
replicas.delete(locator);
|
|
1082
|
+
queuesWithoutReplica.delete(locator);
|
|
1083
|
+
rooms.delete(locator);
|
|
1084
|
+
roomDrafts.delete(locator);
|
|
1085
|
+
transfers.delete(locator);
|
|
1086
|
+
catching.delete(locator);
|
|
1087
|
+
discovered.delete(locator);
|
|
1088
|
+
announced.delete(locator);
|
|
1089
|
+
unreadable.delete(locator);
|
|
1090
|
+
stale.delete(locator);
|
|
1091
|
+
repairable.delete(locator);
|
|
1092
|
+
unindexable.delete(locator);
|
|
1093
|
+
paused.delete(locator);
|
|
1094
|
+
scopedRefusals.delete(locator);
|
|
1095
|
+
sourceRefusals.delete(locator);
|
|
1096
|
+
declarationRefusals.delete(locator);
|
|
1097
|
+
for (const attached of ['collection', 'typed-selection']) {
|
|
1098
|
+
const key = modelKey(locator, attached);
|
|
1099
|
+
models.delete(key);
|
|
1100
|
+
liveModels.delete(key);
|
|
1101
|
+
invalidModels.delete(key);
|
|
540
1102
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
1103
|
+
journal.record(`room lost ${locator}`);
|
|
1104
|
+
}
|
|
1105
|
+
if (fresh.length === 0)
|
|
1106
|
+
return fresh;
|
|
1107
|
+
publishChangeSet(mergeChangeSets(removed));
|
|
1108
|
+
for (const locator of fresh)
|
|
1109
|
+
prepared.delete(locator);
|
|
1110
|
+
searchIndex.forget(fresh);
|
|
1111
|
+
readSide.recover();
|
|
1112
|
+
moveTo({ ...status, ...attention() });
|
|
1113
|
+
return fresh;
|
|
1114
|
+
}
|
|
1115
|
+
async function erased(local, locators) {
|
|
1116
|
+
await local.erase(locators);
|
|
1117
|
+
for (const locator of locators) {
|
|
1118
|
+
const held = fences.get(locator);
|
|
1119
|
+
if (held !== undefined)
|
|
1120
|
+
held.erased = true;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
async function loseRooms(locators) {
|
|
1124
|
+
const fresh = fence(locators);
|
|
1125
|
+
if (fresh.length > 0)
|
|
1126
|
+
await durably((local) => erased(local, fresh));
|
|
544
1127
|
}
|
|
545
1128
|
function restored(key, persisted) {
|
|
546
1129
|
const prior = models.get(key);
|
|
@@ -561,7 +1144,7 @@ export function composeRuntime(options) {
|
|
|
561
1144
|
return model;
|
|
562
1145
|
}
|
|
563
1146
|
async function hydrate(opened) {
|
|
564
|
-
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged,] = await Promise.all([
|
|
1147
|
+
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged, heldTargets,] = await Promise.all([
|
|
565
1148
|
opened.all(),
|
|
566
1149
|
opened.queues(),
|
|
567
1150
|
opened.roomDrafts(),
|
|
@@ -573,7 +1156,9 @@ export function composeRuntime(options) {
|
|
|
573
1156
|
opened.capabilities(),
|
|
574
1157
|
opened.member(),
|
|
575
1158
|
opened.transfers(),
|
|
1159
|
+
opened.targets(),
|
|
576
1160
|
]);
|
|
1161
|
+
targets = heldTargets;
|
|
577
1162
|
transfers.clear();
|
|
578
1163
|
for (const record of staged)
|
|
579
1164
|
transfers.set(record.locator, record);
|
|
@@ -635,6 +1220,7 @@ export function composeRuntime(options) {
|
|
|
635
1220
|
async function readState(opened) {
|
|
636
1221
|
const { lastCompleted, locators, read } = await hydrate(opened);
|
|
637
1222
|
applyChangeSet(read);
|
|
1223
|
+
prepare();
|
|
638
1224
|
if (replicas.size > 0)
|
|
639
1225
|
emitRooms();
|
|
640
1226
|
if (member !== null)
|
|
@@ -645,8 +1231,15 @@ export function composeRuntime(options) {
|
|
|
645
1231
|
return lastCompleted;
|
|
646
1232
|
}
|
|
647
1233
|
async function open() {
|
|
648
|
-
|
|
1234
|
+
if (closed)
|
|
1235
|
+
throw new Error('runtime closed');
|
|
1236
|
+
const opened = await openRuntimeStore(isFenced);
|
|
649
1237
|
store = opened;
|
|
1238
|
+
const interrupted = await opened.fences();
|
|
1239
|
+
for (const locator of interrupted) {
|
|
1240
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
1241
|
+
lastFenced.set(locator, ++fencings);
|
|
1242
|
+
}
|
|
650
1243
|
const kept = options.shell.generation;
|
|
651
1244
|
const storedGeneration = await opened.generation();
|
|
652
1245
|
const loss = detectStorageLoss(storedGeneration, kept);
|
|
@@ -660,6 +1253,8 @@ export function composeRuntime(options) {
|
|
|
660
1253
|
emitStorageLoss();
|
|
661
1254
|
}
|
|
662
1255
|
const lastCompleted = await readState(opened);
|
|
1256
|
+
if (interrupted.length > 0)
|
|
1257
|
+
await erased(opened, interrupted);
|
|
663
1258
|
moveTo({ ...status, lastCompleted, ...attention() });
|
|
664
1259
|
return opened;
|
|
665
1260
|
}
|
|
@@ -741,17 +1336,19 @@ export function composeRuntime(options) {
|
|
|
741
1336
|
moveTo({ ...status, ...attention() });
|
|
742
1337
|
throw error;
|
|
743
1338
|
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
const
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
1339
|
+
return () => {
|
|
1340
|
+
queuesWithoutReplica.delete(locator);
|
|
1341
|
+
for (const { batch, returned } of unqueued) {
|
|
1342
|
+
const outcome = localSaves ? 'dropped' : returned === null ? 'landed' : 'withdrawn';
|
|
1343
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
1344
|
+
}
|
|
1345
|
+
replicas.set(locator, replicaOf(confirmed, kept, before));
|
|
1346
|
+
return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
|
|
1347
|
+
room: settling.room,
|
|
1348
|
+
queue: kept.length !== previousQueue.length,
|
|
1349
|
+
drafts: [...written.keys()],
|
|
1350
|
+
});
|
|
1351
|
+
};
|
|
755
1352
|
}
|
|
756
1353
|
function adoptModels(locator, record) {
|
|
757
1354
|
for (const [attached, model] of [
|
|
@@ -784,11 +1381,16 @@ export function composeRuntime(options) {
|
|
|
784
1381
|
selection: taken(answers?.selection, held.selection, 'typed-selection'),
|
|
785
1382
|
};
|
|
786
1383
|
}
|
|
787
|
-
function publish(local, room, confirmed, landing
|
|
1384
|
+
function publish(local, room, confirmed, landing, live) {
|
|
788
1385
|
const { base, stored } = landing;
|
|
789
1386
|
return exclusive(async () => {
|
|
790
|
-
if (
|
|
1387
|
+
if (stored !== undefined) {
|
|
1388
|
+
await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
|
|
1389
|
+
}
|
|
1390
|
+
if (!live()) {
|
|
1391
|
+
caughtUpTo(room.locator);
|
|
791
1392
|
return true;
|
|
1393
|
+
}
|
|
792
1394
|
const prior = replicas.get(room.locator);
|
|
793
1395
|
if (base !== undefined &&
|
|
794
1396
|
(prior?.confirmed.record.roomVersion !== base.record.roomVersion ||
|
|
@@ -802,24 +1404,42 @@ export function composeRuntime(options) {
|
|
|
802
1404
|
const accepted = pendings.get(batch.id)?.accepted;
|
|
803
1405
|
return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
|
|
804
1406
|
});
|
|
805
|
-
|
|
806
|
-
|
|
1407
|
+
let settled;
|
|
1408
|
+
try {
|
|
1409
|
+
settled = await settle(local, room.locator, prior, confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true, ...(stored === undefined ? {} : { stored }) });
|
|
1410
|
+
}
|
|
1411
|
+
catch (error) {
|
|
1412
|
+
if (!exceeded(error))
|
|
1413
|
+
throw error;
|
|
1414
|
+
await exceededOn(room.locator, grownBy(confirmed.record, prior?.confirmed.record));
|
|
1415
|
+
caughtUpTo(room.locator);
|
|
1416
|
+
return true;
|
|
1417
|
+
}
|
|
1418
|
+
const set = settled();
|
|
1419
|
+
if (stored !== undefined) {
|
|
807
1420
|
transfers.delete(room.locator);
|
|
1421
|
+
catching.delete(room.locator);
|
|
1422
|
+
}
|
|
808
1423
|
for (const batch of accepted)
|
|
809
1424
|
pendings.delete(batch.id);
|
|
810
1425
|
stale.delete(room.locator);
|
|
1426
|
+
unindexable.delete(room.locator);
|
|
811
1427
|
if (unreadable.delete(room.locator))
|
|
812
1428
|
scopedRefusals.delete(room.locator);
|
|
813
1429
|
sourceRefusals.delete(room.locator);
|
|
814
1430
|
declarationRefusals.delete(room.locator);
|
|
815
1431
|
list(room, true);
|
|
1432
|
+
if (prior === undefined)
|
|
1433
|
+
prepared.add(room.locator);
|
|
816
1434
|
publishChangeSet(set);
|
|
817
1435
|
moveTo({ ...status, ...attention() });
|
|
1436
|
+
options.adapter.roomStream?.published(room.locator, roomVersion);
|
|
818
1437
|
return true;
|
|
819
1438
|
});
|
|
820
1439
|
}
|
|
821
1440
|
async function saveRoomDrafts(locator, named, writtenBy) {
|
|
822
1441
|
const local = await openLocal();
|
|
1442
|
+
await referenced(locator);
|
|
823
1443
|
const replica = replicas.get(locator);
|
|
824
1444
|
const room = rooms.get(locator);
|
|
825
1445
|
if (replica === undefined || room === undefined) {
|
|
@@ -867,6 +1487,11 @@ export function composeRuntime(options) {
|
|
|
867
1487
|
changes,
|
|
868
1488
|
bases: parts.flatMap(({ bases }) => bases),
|
|
869
1489
|
writtenBy,
|
|
1490
|
+
clean: {
|
|
1491
|
+
text: replica.confirmed.record.documents,
|
|
1492
|
+
binaries: replica.confirmed.record.binaries,
|
|
1493
|
+
composition: compositionOf(replica.confirmed.record),
|
|
1494
|
+
},
|
|
870
1495
|
};
|
|
871
1496
|
const checked = withdrawing ? buildBatch({ ...input, queued: requeue.queued }) : null;
|
|
872
1497
|
if (checked?.kind === 'refused')
|
|
@@ -937,6 +1562,8 @@ export function composeRuntime(options) {
|
|
|
937
1562
|
catch {
|
|
938
1563
|
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
939
1564
|
}
|
|
1565
|
+
if (fences.has(locator))
|
|
1566
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
940
1567
|
for (const origin of withdrawn.keys())
|
|
941
1568
|
pendings.delete(origin);
|
|
942
1569
|
for (const [id, drafts] of drafted) {
|
|
@@ -966,6 +1593,8 @@ export function composeRuntime(options) {
|
|
|
966
1593
|
return;
|
|
967
1594
|
}
|
|
968
1595
|
await local.submit(locator, identifier, submission, lost);
|
|
1596
|
+
if (fences.has(locator))
|
|
1597
|
+
return;
|
|
969
1598
|
pendings.set(identifier, { ...state, submission, lost });
|
|
970
1599
|
const replica = replicas.get(locator);
|
|
971
1600
|
if (replica !== undefined)
|
|
@@ -976,6 +1605,8 @@ export function composeRuntime(options) {
|
|
|
976
1605
|
if (!continuing())
|
|
977
1606
|
throw new Error('connection refused');
|
|
978
1607
|
await local.keepUpload(locator, identifier, upload);
|
|
1608
|
+
if (fences.has(locator))
|
|
1609
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
979
1610
|
pendings.set(identifier, { ...pendings.get(identifier), upload });
|
|
980
1611
|
if (!continuing())
|
|
981
1612
|
throw new Error('connection refused');
|
|
@@ -991,6 +1622,7 @@ export function composeRuntime(options) {
|
|
|
991
1622
|
}
|
|
992
1623
|
if (closed || authorization === undefined || refusal !== null)
|
|
993
1624
|
return UNCERTAIN;
|
|
1625
|
+
const since = fencings;
|
|
994
1626
|
const transfer = ['transfer', 'history', locator, path];
|
|
995
1627
|
if (query.revision !== undefined)
|
|
996
1628
|
transfer.push(query.revision);
|
|
@@ -1005,6 +1637,8 @@ export function composeRuntime(options) {
|
|
|
1005
1637
|
...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
|
|
1006
1638
|
});
|
|
1007
1639
|
journal.record(`outcome history ${result.kind}`);
|
|
1640
|
+
if (fencedSince(locator, since))
|
|
1641
|
+
return failure({ kind: 'room-not-readable', locator });
|
|
1008
1642
|
if (result.kind === 'scoped-refusal') {
|
|
1009
1643
|
const room = replicas.get(result.locator)?.confirmed.record;
|
|
1010
1644
|
if (room !== undefined)
|
|
@@ -1027,6 +1661,12 @@ export function composeRuntime(options) {
|
|
|
1027
1661
|
}
|
|
1028
1662
|
return null;
|
|
1029
1663
|
}
|
|
1664
|
+
function binaryRoom(request) {
|
|
1665
|
+
if (request.kind === 'path')
|
|
1666
|
+
return request.locator;
|
|
1667
|
+
const scope = options.adapter.referrerScope(request.referrer);
|
|
1668
|
+
return scope?.kind === 'room' ? scope.locator : null;
|
|
1669
|
+
}
|
|
1030
1670
|
async function readBinary(request) {
|
|
1031
1671
|
if (closed || refusal !== null)
|
|
1032
1672
|
return UNCERTAIN;
|
|
@@ -1038,12 +1678,16 @@ export function composeRuntime(options) {
|
|
|
1038
1678
|
}
|
|
1039
1679
|
if (closed || authorization === undefined || refusal !== null)
|
|
1040
1680
|
return UNCERTAIN;
|
|
1681
|
+
const since = fencings;
|
|
1041
1682
|
journal.record(request.kind === 'path'
|
|
1042
1683
|
? `transfer binary ${request.locator} ${request.path}`
|
|
1043
1684
|
: `transfer binary ${request.fileRef}`);
|
|
1044
1685
|
try {
|
|
1045
1686
|
const result = await options.adapter.binary(authorization, request);
|
|
1046
1687
|
journal.record(`outcome binary ${result.kind}`);
|
|
1688
|
+
const locator = binaryRoom(request);
|
|
1689
|
+
if (locator !== null && fencedSince(locator, since))
|
|
1690
|
+
return failure({ kind: 'room-not-readable', locator });
|
|
1047
1691
|
if (result.kind === 'scoped-refusal') {
|
|
1048
1692
|
const room = replicas.get(result.locator)?.confirmed.record;
|
|
1049
1693
|
if (room !== undefined)
|
|
@@ -1167,11 +1811,12 @@ export function composeRuntime(options) {
|
|
|
1167
1811
|
options.adapter.reached(locator, written, roomVersion) &&
|
|
1168
1812
|
!options.adapter.reached(locator, roomVersion, written));
|
|
1169
1813
|
}
|
|
1170
|
-
async function pullRefresh(authorized, confirmed) {
|
|
1171
|
-
if (!
|
|
1814
|
+
async function pullRefresh(authorized, confirmed, live) {
|
|
1815
|
+
if (!live())
|
|
1172
1816
|
return unconfirmed;
|
|
1817
|
+
const turn = generation;
|
|
1173
1818
|
try {
|
|
1174
|
-
const result = await refreshRoom(
|
|
1819
|
+
const result = await refreshRoom(reads, authorized, confirmed, (models, held) => (live() ? remodel(confirmed.record.locator, models, held) : held), live);
|
|
1175
1820
|
const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
|
|
1176
1821
|
journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
|
|
1177
1822
|
return result;
|
|
@@ -1180,48 +1825,101 @@ export function composeRuntime(options) {
|
|
|
1180
1825
|
journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
|
|
1181
1826
|
throw error;
|
|
1182
1827
|
}
|
|
1828
|
+
finally {
|
|
1829
|
+
reads.finished(confirmed.record.locator, turn);
|
|
1830
|
+
}
|
|
1183
1831
|
}
|
|
1184
|
-
function staging(local) {
|
|
1832
|
+
function staging(local, live) {
|
|
1185
1833
|
return {
|
|
1186
1834
|
held: (locator) => transfers.get(locator) ?? null,
|
|
1187
1835
|
staged: (record) => local.staged(record),
|
|
1188
1836
|
stage: (record, page) => exclusive(async () => {
|
|
1189
|
-
if (!
|
|
1837
|
+
if (!live())
|
|
1190
1838
|
return false;
|
|
1191
|
-
|
|
1839
|
+
const bytes = layerBytes(page);
|
|
1840
|
+
if (!(await admitted(local, record.locator, bytes)))
|
|
1841
|
+
return false;
|
|
1842
|
+
try {
|
|
1843
|
+
await local.stage(record, page);
|
|
1844
|
+
}
|
|
1845
|
+
catch (error) {
|
|
1846
|
+
if (!exceeded(error))
|
|
1847
|
+
throw error;
|
|
1848
|
+
await exceededOn(record.locator, bytes);
|
|
1849
|
+
return false;
|
|
1850
|
+
}
|
|
1192
1851
|
transfers.set(record.locator, record);
|
|
1852
|
+
readSide.recover();
|
|
1193
1853
|
return true;
|
|
1194
1854
|
}),
|
|
1195
1855
|
abandon: (locator) => exclusive(async () => {
|
|
1196
|
-
if (!
|
|
1856
|
+
if (!live())
|
|
1197
1857
|
return;
|
|
1198
1858
|
await local.abandon(locator);
|
|
1199
1859
|
transfers.delete(locator);
|
|
1860
|
+
readSide.recover();
|
|
1200
1861
|
}),
|
|
1201
1862
|
};
|
|
1202
1863
|
}
|
|
1203
|
-
|
|
1204
|
-
|
|
1864
|
+
function reusable(room) {
|
|
1865
|
+
const record = replicas.get(room.locator)?.confirmed.record;
|
|
1866
|
+
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
1867
|
+
return null;
|
|
1868
|
+
return { documents: record.documents, revisions: record.revisions };
|
|
1869
|
+
}
|
|
1870
|
+
async function pullBootstrap(authorized, room, local, live) {
|
|
1871
|
+
if (!live())
|
|
1205
1872
|
return unconfirmed;
|
|
1873
|
+
const turn = generation;
|
|
1874
|
+
if (replicas.has(room.locator)) {
|
|
1875
|
+
catching.add(room.locator);
|
|
1876
|
+
readSide.recover();
|
|
1877
|
+
}
|
|
1206
1878
|
try {
|
|
1207
|
-
const result = await bootstrapRoom(
|
|
1879
|
+
const result = await bootstrapRoom(reads, authorized, room, {
|
|
1208
1880
|
collection: modelOf(room.locator),
|
|
1209
1881
|
selection: modelOf(room.locator, 'typed-selection'),
|
|
1210
|
-
}, (models, held) => remodel(room.locator, models, held),
|
|
1882
|
+
}, (models, held) => (live() ? remodel(room.locator, models, held) : held), live, staging(local, live), reusable(room));
|
|
1211
1883
|
const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
|
|
1212
1884
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
1885
|
+
if (result.kind !== 'staged')
|
|
1886
|
+
caughtUpTo(room.locator);
|
|
1213
1887
|
return result;
|
|
1214
1888
|
}
|
|
1215
1889
|
catch (error) {
|
|
1216
1890
|
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
1891
|
+
caughtUpTo(room.locator);
|
|
1217
1892
|
throw error;
|
|
1218
1893
|
}
|
|
1894
|
+
finally {
|
|
1895
|
+
reads.finished(room.locator, turn);
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
function caughtUpTo(locator) {
|
|
1899
|
+
if (catching.delete(locator))
|
|
1900
|
+
readSide.recover();
|
|
1901
|
+
}
|
|
1902
|
+
function behind(room) {
|
|
1903
|
+
const held = replicas.get(room.locator)?.confirmed.record.roomVersion;
|
|
1904
|
+
return (held !== undefined &&
|
|
1905
|
+
[announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
|
|
1906
|
+
}
|
|
1907
|
+
async function advance(room, local, authorized, live) {
|
|
1908
|
+
const carried = reads.carried(room.locator);
|
|
1909
|
+
const withheld = await advanceOnce(room, local, authorized, live);
|
|
1910
|
+
if (withheld !== null || reads.carried(room.locator) === carried || !behind(room)) {
|
|
1911
|
+
return withheld;
|
|
1912
|
+
}
|
|
1913
|
+
return advanceOnce(room, local, authorized, live);
|
|
1219
1914
|
}
|
|
1220
|
-
async function
|
|
1915
|
+
async function advanceOnce(room, local, authorized, live) {
|
|
1916
|
+
await referenced(room.locator);
|
|
1917
|
+
if (!live() || paused.has(room.locator))
|
|
1918
|
+
return unconfirmed;
|
|
1221
1919
|
const held = replicas.get(room.locator);
|
|
1222
1920
|
if (held !== undefined) {
|
|
1223
|
-
const refreshed = await pullRefresh(authorized, held.confirmed);
|
|
1224
|
-
if (!
|
|
1921
|
+
const refreshed = await pullRefresh(authorized, held.confirmed, live);
|
|
1922
|
+
if (!live())
|
|
1225
1923
|
return unconfirmed;
|
|
1226
1924
|
if (refreshed.kind === 'still-current') {
|
|
1227
1925
|
stale.delete(room.locator);
|
|
@@ -1235,31 +1933,45 @@ export function composeRuntime(options) {
|
|
|
1235
1933
|
return null;
|
|
1236
1934
|
}
|
|
1237
1935
|
if (refreshed.kind === 'advanced') {
|
|
1238
|
-
if (!(await publish(local, room, refreshed.confirmed, { base: held.confirmed }))) {
|
|
1936
|
+
if (!(await publish(local, room, refreshed.confirmed, { base: held.confirmed }, live))) {
|
|
1239
1937
|
const now = replicas.get(room.locator)?.confirmed.record.roomVersion;
|
|
1240
1938
|
const reached = now !== undefined &&
|
|
1241
1939
|
options.adapter.reached(room.locator, now, refreshed.confirmed.record.roomVersion);
|
|
1242
|
-
return reached ? null : advance(room, local, authorized);
|
|
1940
|
+
return reached ? null : advance(room, local, authorized, live);
|
|
1243
1941
|
}
|
|
1244
1942
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
1245
1943
|
return null;
|
|
1246
1944
|
}
|
|
1247
1945
|
if (refreshed.kind !== 'beyond-horizon')
|
|
1248
1946
|
return refreshed;
|
|
1947
|
+
if (opened !== null) {
|
|
1948
|
+
owed.add(room.locator);
|
|
1949
|
+
readSide.recover();
|
|
1950
|
+
return unconfirmed;
|
|
1951
|
+
}
|
|
1249
1952
|
}
|
|
1250
|
-
const bootstrapped = await pullBootstrap(authorized, room, local);
|
|
1251
|
-
if (!
|
|
1953
|
+
const bootstrapped = await pullBootstrap(authorized, room, local, live);
|
|
1954
|
+
if (!live())
|
|
1252
1955
|
return unconfirmed;
|
|
1253
1956
|
if (bootstrapped.kind !== 'staged')
|
|
1254
1957
|
return bootstrapped;
|
|
1255
|
-
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored });
|
|
1958
|
+
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
1256
1959
|
return null;
|
|
1257
1960
|
}
|
|
1258
1961
|
async function withhold(withheld, room, local, progress) {
|
|
1962
|
+
if (withheld.kind === 'unconfirmed')
|
|
1963
|
+
return;
|
|
1259
1964
|
if (withheld.kind === 'refused')
|
|
1260
1965
|
stale.delete(room.locator);
|
|
1261
1966
|
else
|
|
1262
1967
|
stale.add(room.locator);
|
|
1968
|
+
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
1969
|
+
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
1970
|
+
repairable.set(room.locator, reset);
|
|
1971
|
+
repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
|
|
1972
|
+
}
|
|
1973
|
+
else
|
|
1974
|
+
repairable.delete(room.locator);
|
|
1263
1975
|
if (withheld.kind === 'scoped-refusal') {
|
|
1264
1976
|
await scoped(withheld, room, local);
|
|
1265
1977
|
}
|
|
@@ -1295,6 +2007,7 @@ export function composeRuntime(options) {
|
|
|
1295
2007
|
unreadable.add(room.locator);
|
|
1296
2008
|
list(room, false);
|
|
1297
2009
|
emitRooms();
|
|
2010
|
+
moveTo({ ...status, ...attention() });
|
|
1298
2011
|
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
1299
2012
|
}
|
|
1300
2013
|
function leaving(batch, result) {
|
|
@@ -1316,6 +2029,7 @@ export function composeRuntime(options) {
|
|
|
1316
2029
|
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
1317
2030
|
}
|
|
1318
2031
|
async function adoptAccepted(room, local, submitted, accepted) {
|
|
2032
|
+
await referenced(room.locator);
|
|
1319
2033
|
if (!continuing())
|
|
1320
2034
|
return unconfirmed;
|
|
1321
2035
|
const current = replicas.get(room.locator);
|
|
@@ -1332,16 +2046,17 @@ export function composeRuntime(options) {
|
|
|
1332
2046
|
if (applied.kind !== 'staged') {
|
|
1333
2047
|
const state = pendings.get(submitted.id);
|
|
1334
2048
|
await local.accept(room.locator, submitted.id, accepted.roomVersion);
|
|
1335
|
-
if (state !== undefined)
|
|
2049
|
+
if (state !== undefined && !fences.has(room.locator))
|
|
1336
2050
|
pendings.set(submitted.id, { ...state, accepted: accepted.roomVersion });
|
|
1337
2051
|
emitQueue(room.locator);
|
|
1338
2052
|
return applied;
|
|
1339
2053
|
}
|
|
1340
|
-
const
|
|
2054
|
+
const settled = await settle(local, room.locator, current, applied.confirmed, current.queue.filter((batch) => batch.id !== submitted.id), { room: true });
|
|
1341
2055
|
pendings.delete(submitted.id);
|
|
1342
2056
|
unreadable.delete(room.locator);
|
|
1343
2057
|
sourceRefusals.delete(room.locator);
|
|
1344
2058
|
declarationRefusals.delete(room.locator);
|
|
2059
|
+
const set = settled();
|
|
1345
2060
|
list(room, true);
|
|
1346
2061
|
publishChangeSet(set);
|
|
1347
2062
|
moveTo({ ...status, ...attention() });
|
|
@@ -1355,14 +2070,14 @@ export function composeRuntime(options) {
|
|
|
1355
2070
|
refusedSource(room, failed);
|
|
1356
2071
|
return false;
|
|
1357
2072
|
}
|
|
1358
|
-
async function submitRoom(room, local, authorized, progress) {
|
|
1359
|
-
while (continuing() && authorization !== undefined) {
|
|
2073
|
+
async function submitRoom(room, local, authorized, progress, signal) {
|
|
2074
|
+
while (continuing() && authorization !== undefined && !signal.aborted) {
|
|
1360
2075
|
const replica = replicas.get(room.locator);
|
|
1361
2076
|
const head = replica?.queue[0];
|
|
1362
2077
|
if (replica === undefined || head === undefined)
|
|
1363
|
-
return;
|
|
2078
|
+
return 'done';
|
|
1364
2079
|
if (pendings.get(head.id)?.accepted !== undefined)
|
|
1365
|
-
return;
|
|
2080
|
+
return 'done';
|
|
1366
2081
|
const state = pendings.get(head.id);
|
|
1367
2082
|
const { submission, lost: earlier } = state ?? {
|
|
1368
2083
|
submission: { state: 'waiting' },
|
|
@@ -1370,24 +2085,30 @@ export function composeRuntime(options) {
|
|
|
1370
2085
|
};
|
|
1371
2086
|
if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
|
|
1372
2087
|
progress.complete = false;
|
|
1373
|
-
return;
|
|
2088
|
+
return { at: Date.parse(submission.reset) };
|
|
1374
2089
|
}
|
|
1375
2090
|
if (writesHeld()) {
|
|
1376
2091
|
progress.complete = false;
|
|
1377
2092
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1378
|
-
return;
|
|
2093
|
+
return 'done';
|
|
1379
2094
|
}
|
|
1380
2095
|
const lost = earlier || submission.state === 'submitted';
|
|
2096
|
+
if (lost && pastHorizon(head.id)) {
|
|
2097
|
+
await discardPending(room, local, signal);
|
|
2098
|
+
return 'done';
|
|
2099
|
+
}
|
|
1381
2100
|
let result;
|
|
1382
2101
|
journal.record(`submission ${room.locator} ${head.id}`);
|
|
1383
2102
|
await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
|
|
1384
|
-
if (discoveryPaused)
|
|
1385
|
-
return;
|
|
1386
|
-
if (writesHeld()) {
|
|
2103
|
+
if (discoveryPaused || fences.has(room.locator))
|
|
2104
|
+
return 'done';
|
|
2105
|
+
if (signal.aborted || writesHeld()) {
|
|
1387
2106
|
await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
|
|
2107
|
+
if (signal.aborted)
|
|
2108
|
+
return 'done';
|
|
1388
2109
|
progress.complete = false;
|
|
1389
2110
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1390
|
-
return;
|
|
2111
|
+
return 'done';
|
|
1391
2112
|
}
|
|
1392
2113
|
const opensBefore = openSignals;
|
|
1393
2114
|
const answered = awaitingAnswer(room.locator);
|
|
@@ -1403,32 +2124,36 @@ export function composeRuntime(options) {
|
|
|
1403
2124
|
progress.complete = false;
|
|
1404
2125
|
if (continuing())
|
|
1405
2126
|
progress.reachable = false;
|
|
1406
|
-
return;
|
|
2127
|
+
return 'again';
|
|
2128
|
+
}
|
|
2129
|
+
if (signal.aborted) {
|
|
2130
|
+
answered();
|
|
2131
|
+
return 'done';
|
|
1407
2132
|
}
|
|
1408
2133
|
if (result.kind !== 'accepted' || !continuing())
|
|
1409
2134
|
answered();
|
|
1410
2135
|
if (!continuing())
|
|
1411
|
-
return;
|
|
2136
|
+
return 'done';
|
|
1412
2137
|
if (result.kind === 'transport-uncertainty') {
|
|
1413
2138
|
progress.complete = false;
|
|
1414
2139
|
progress.reachable = false;
|
|
1415
|
-
return;
|
|
2140
|
+
return 'again';
|
|
1416
2141
|
}
|
|
1417
2142
|
if (result.kind === 'room-not-readable') {
|
|
1418
2143
|
progress.complete = false;
|
|
1419
2144
|
unreadable.add(room.locator);
|
|
1420
2145
|
list(room, false);
|
|
1421
2146
|
emitRooms();
|
|
1422
|
-
return;
|
|
2147
|
+
return 'done';
|
|
1423
2148
|
}
|
|
1424
2149
|
if (result.kind === 'revoked-authorization') {
|
|
1425
2150
|
await refuse('revoked-authorization');
|
|
1426
|
-
return;
|
|
2151
|
+
return 'done';
|
|
1427
2152
|
}
|
|
1428
2153
|
if (result.kind === 'scoped-refusal') {
|
|
1429
2154
|
await scoped(result, room, local);
|
|
1430
2155
|
progress.complete = false;
|
|
1431
|
-
return;
|
|
2156
|
+
return 'done';
|
|
1432
2157
|
}
|
|
1433
2158
|
if (result.kind === 'write-halted') {
|
|
1434
2159
|
journal.record(`outcome ${room.locator} ${head.id} write-halted`);
|
|
@@ -1437,16 +2162,24 @@ export function composeRuntime(options) {
|
|
|
1437
2162
|
continue;
|
|
1438
2163
|
haltedRefusal = true;
|
|
1439
2164
|
progress.complete = false;
|
|
1440
|
-
return;
|
|
2165
|
+
return 'done';
|
|
1441
2166
|
}
|
|
1442
2167
|
if (result.kind === 'conflicted' ||
|
|
1443
2168
|
result.kind === 'rejected' ||
|
|
1444
2169
|
result.kind === 'expired' ||
|
|
1445
2170
|
result.kind === 'retryable-refusal') {
|
|
2171
|
+
if (result.kind === 'expired' && lost && result.restored !== true) {
|
|
2172
|
+
journal.record(`outcome ${room.locator} ${head.id} expired`);
|
|
2173
|
+
await discardPending(room, local, signal);
|
|
2174
|
+
return 'done';
|
|
2175
|
+
}
|
|
1446
2176
|
if (result.kind === 'expired') {
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
2177
|
+
await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, false));
|
|
2178
|
+
const withheld = await advance(room, local, authorized, continuing);
|
|
2179
|
+
if (withheld !== null) {
|
|
2180
|
+
await withhold(withheld, room, local, progress);
|
|
2181
|
+
return 'done';
|
|
2182
|
+
}
|
|
1450
2183
|
}
|
|
1451
2184
|
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
|
|
1452
2185
|
(result.kind === 'conflicted' && result.roomVersion !== undefined
|
|
@@ -1459,8 +2192,7 @@ export function composeRuntime(options) {
|
|
|
1459
2192
|
const reset = result.reset;
|
|
1460
2193
|
progress.complete = false;
|
|
1461
2194
|
await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
|
|
1462
|
-
|
|
1463
|
-
return;
|
|
2195
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
1464
2196
|
}
|
|
1465
2197
|
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
1466
2198
|
const left = leaving(head, result);
|
|
@@ -1471,37 +2203,82 @@ export function composeRuntime(options) {
|
|
|
1471
2203
|
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
1472
2204
|
return;
|
|
1473
2205
|
}
|
|
1474
|
-
const
|
|
2206
|
+
const settled = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
|
|
1475
2207
|
leaving: (result.kind === 'expired' && landed(head, current.confirmed)) ||
|
|
1476
2208
|
(result.kind === 'conflicted' && landed(head, current.confirmed, result.affected))
|
|
1477
2209
|
? { ...left, returned: null }
|
|
1478
2210
|
: left,
|
|
1479
2211
|
});
|
|
1480
|
-
publishChangeSet(
|
|
2212
|
+
publishChangeSet(settled());
|
|
1481
2213
|
moveTo({ ...status, ...attention() });
|
|
1482
2214
|
});
|
|
1483
2215
|
if (result.kind === 'expired')
|
|
1484
2216
|
continue;
|
|
1485
|
-
const withheld = await advance(room, local, authorized);
|
|
1486
|
-
if (withheld !== null)
|
|
1487
|
-
|
|
2217
|
+
const withheld = await advance(room, local, authorized, continuing);
|
|
2218
|
+
if (withheld !== null) {
|
|
2219
|
+
await withhold(withheld, room, local, progress);
|
|
2220
|
+
return 'done';
|
|
2221
|
+
}
|
|
1488
2222
|
continue;
|
|
1489
2223
|
}
|
|
1490
2224
|
const kept = await adopted(room, local, head, result).finally(answered);
|
|
1491
2225
|
if (!kept) {
|
|
1492
2226
|
progress.complete = false;
|
|
1493
|
-
return;
|
|
2227
|
+
return 'done';
|
|
1494
2228
|
}
|
|
1495
2229
|
}
|
|
2230
|
+
return 'done';
|
|
2231
|
+
}
|
|
2232
|
+
function pastHorizon(identity) {
|
|
2233
|
+
return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
|
|
2234
|
+
}
|
|
2235
|
+
async function discardPending(room, local, signal) {
|
|
2236
|
+
const { locator } = room;
|
|
2237
|
+
const discarded = await exclusive(async () => {
|
|
2238
|
+
if (fences.has(locator))
|
|
2239
|
+
return false;
|
|
2240
|
+
const before = replicas.get(locator);
|
|
2241
|
+
const queue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
2242
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
2243
|
+
lastFenced.set(locator, ++fencings);
|
|
2244
|
+
try {
|
|
2245
|
+
await local.discard(locator);
|
|
2246
|
+
}
|
|
2247
|
+
finally {
|
|
2248
|
+
fences.delete(locator);
|
|
2249
|
+
}
|
|
2250
|
+
for (const batch of queue)
|
|
2251
|
+
pendings.delete(batch.id);
|
|
2252
|
+
queuesWithoutReplica.delete(locator);
|
|
2253
|
+
const after = before === undefined ? null : replicaOf(before.confirmed, [], before);
|
|
2254
|
+
if (after !== null)
|
|
2255
|
+
replicas.set(locator, after);
|
|
2256
|
+
journal.record(`room discarded ${locator}`);
|
|
2257
|
+
publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
|
|
2258
|
+
moveTo({ ...status, ...attention() });
|
|
2259
|
+
return true;
|
|
2260
|
+
});
|
|
2261
|
+
if (!discarded)
|
|
2262
|
+
return;
|
|
2263
|
+
const progress = { complete: true, reachable: true };
|
|
2264
|
+
await roomTask(locator, async (live) => {
|
|
2265
|
+
await Promise.race([
|
|
2266
|
+
reload(room, local, progress, () => live() && !signal.aborted),
|
|
2267
|
+
abandonedOn(signal),
|
|
2268
|
+
]);
|
|
2269
|
+
});
|
|
2270
|
+
if (signal.aborted)
|
|
2271
|
+
return;
|
|
2272
|
+
await rest(progress);
|
|
1496
2273
|
}
|
|
1497
2274
|
function awaitingAnswer(locator) {
|
|
1498
2275
|
let release = () => { };
|
|
1499
2276
|
const awaited = new Promise((resolve) => {
|
|
1500
2277
|
release = resolve;
|
|
1501
2278
|
});
|
|
1502
|
-
answering.set(locator, awaited);
|
|
2279
|
+
answering.set(locator, { generation, answered: awaited });
|
|
1503
2280
|
return () => {
|
|
1504
|
-
if (answering.get(locator) === awaited)
|
|
2281
|
+
if (answering.get(locator)?.answered === awaited)
|
|
1505
2282
|
answering.delete(locator);
|
|
1506
2283
|
release();
|
|
1507
2284
|
};
|
|
@@ -1572,6 +2349,11 @@ export function composeRuntime(options) {
|
|
|
1572
2349
|
});
|
|
1573
2350
|
}
|
|
1574
2351
|
async function scoped(result, room, local) {
|
|
2352
|
+
if ((result.scope === 'content' || result.scope === 'sync') && !result.retryable) {
|
|
2353
|
+
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
2354
|
+
await loseRooms([room.locator]);
|
|
2355
|
+
return;
|
|
2356
|
+
}
|
|
1575
2357
|
scopedRefusals.set(room.locator, {
|
|
1576
2358
|
code: result.code,
|
|
1577
2359
|
fields: result.fields,
|
|
@@ -1617,19 +2399,23 @@ export function composeRuntime(options) {
|
|
|
1617
2399
|
];
|
|
1618
2400
|
});
|
|
1619
2401
|
}
|
|
1620
|
-
async function synchronize(listed, local, progress, attempt) {
|
|
1621
|
-
const
|
|
1622
|
-
const
|
|
2402
|
+
async function synchronize(listed, local, progress, attempt, live) {
|
|
2403
|
+
const acquired = listed.filter(acquires);
|
|
2404
|
+
const prepared = await Promise.allSettled(acquired.map((room) => prepareRoom(room, local, progress)));
|
|
2405
|
+
if (!live())
|
|
2406
|
+
return;
|
|
2407
|
+
const ready = acquired.filter((_, index) => {
|
|
1623
2408
|
const outcome = prepared[index];
|
|
1624
2409
|
return outcome?.status === 'fulfilled' && outcome.value;
|
|
1625
2410
|
});
|
|
1626
2411
|
if (attempt === 'opened') {
|
|
1627
|
-
options.adapter.roomStream?.hold(heldRooms(ready));
|
|
2412
|
+
options.adapter.roomStream?.hold(heldRooms(ready), heldDemand(acquired));
|
|
1628
2413
|
handed = true;
|
|
1629
2414
|
}
|
|
2415
|
+
discovering?.release();
|
|
1630
2416
|
const loaded = await Promise.allSettled(ready
|
|
1631
2417
|
.filter((room) => attempt === 'unstreamed' || !replicas.has(room.locator))
|
|
1632
|
-
.map((room) => loadRoom(room, local, progress)));
|
|
2418
|
+
.map((room) => roomTask(room.locator, (held) => loadRoom(room, local, progress, held))));
|
|
1633
2419
|
const failed = [...prepared, ...loaded].find((room) => room.status === 'rejected');
|
|
1634
2420
|
if (failed !== undefined)
|
|
1635
2421
|
throw failed.reason;
|
|
@@ -1651,21 +2437,12 @@ export function composeRuntime(options) {
|
|
|
1651
2437
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
1652
2438
|
if (discoveryPaused || authorization === undefined)
|
|
1653
2439
|
return false;
|
|
1654
|
-
|
|
1655
|
-
if (replicas.has(room.locator))
|
|
1656
|
-
progress.complete = false;
|
|
1657
|
-
return false;
|
|
1658
|
-
}
|
|
1659
|
-
return true;
|
|
2440
|
+
return room.replica.admission === 'admitted';
|
|
1660
2441
|
}
|
|
1661
|
-
async function caughtUp(room, local, progress) {
|
|
1662
|
-
|
|
1663
|
-
if (held === undefined || authorization === undefined)
|
|
1664
|
-
return;
|
|
1665
|
-
const behind = [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later));
|
|
1666
|
-
if (!behind)
|
|
2442
|
+
async function caughtUp(room, local, progress, live) {
|
|
2443
|
+
if (authorization === undefined || !live() || !behind(room))
|
|
1667
2444
|
return;
|
|
1668
|
-
const withheld = await advance(room, local, authorization);
|
|
2445
|
+
const withheld = await advance(room, local, authorization, live);
|
|
1669
2446
|
if (withheld !== null)
|
|
1670
2447
|
await withhold(withheld, room, local, progress);
|
|
1671
2448
|
}
|
|
@@ -1675,39 +2452,46 @@ export function composeRuntime(options) {
|
|
|
1675
2452
|
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
1676
2453
|
});
|
|
1677
2454
|
}
|
|
1678
|
-
async function loadRoom(room, local, progress) {
|
|
1679
|
-
if (
|
|
2455
|
+
async function loadRoom(room, local, progress, live) {
|
|
2456
|
+
if (!live() || authorization === undefined)
|
|
1680
2457
|
return;
|
|
1681
2458
|
if (replicas.has(room.locator) && revoked(room.locator)) {
|
|
1682
|
-
await reload(room, local, progress);
|
|
2459
|
+
await reload(room, local, progress, live);
|
|
1683
2460
|
return;
|
|
1684
2461
|
}
|
|
1685
|
-
const withheld = await advance(room, local, authorization);
|
|
2462
|
+
const withheld = await advance(room, local, authorization, live);
|
|
1686
2463
|
if (withheld !== null) {
|
|
1687
2464
|
await withhold(withheld, room, local, progress);
|
|
1688
2465
|
return;
|
|
1689
2466
|
}
|
|
1690
|
-
await caughtUp(room, local, progress);
|
|
2467
|
+
await caughtUp(room, local, progress, live);
|
|
1691
2468
|
}
|
|
1692
|
-
async function reload(room, local, progress) {
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
2469
|
+
async function reload(room, local, progress, live) {
|
|
2470
|
+
try {
|
|
2471
|
+
if (!live() || authorization === undefined)
|
|
2472
|
+
return;
|
|
2473
|
+
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
2474
|
+
if (!live())
|
|
2475
|
+
return;
|
|
2476
|
+
if (bootstrapped.kind !== 'staged') {
|
|
2477
|
+
await withhold(bootstrapped, room, local, progress);
|
|
2478
|
+
return;
|
|
2479
|
+
}
|
|
2480
|
+
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
2481
|
+
await caughtUp(room, local, progress, live);
|
|
2482
|
+
}
|
|
2483
|
+
finally {
|
|
2484
|
+
caughtUpTo(room.locator);
|
|
1701
2485
|
}
|
|
1702
|
-
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored });
|
|
1703
|
-
await caughtUp(room, local, progress);
|
|
1704
2486
|
}
|
|
1705
|
-
async function discoverRooms(local, progress, attempt) {
|
|
2487
|
+
async function discoverRooms(local, progress, attempt, live) {
|
|
1706
2488
|
if (closed || authorization === undefined) {
|
|
1707
2489
|
notifyReady();
|
|
1708
2490
|
return;
|
|
1709
2491
|
}
|
|
1710
2492
|
moveTo(transferring(status));
|
|
2493
|
+
discoveries += 1;
|
|
2494
|
+
const started = discoveries;
|
|
1711
2495
|
let result;
|
|
1712
2496
|
try {
|
|
1713
2497
|
result = await options.adapter.discover(authorization);
|
|
@@ -1715,7 +2499,8 @@ export function composeRuntime(options) {
|
|
|
1715
2499
|
catch {
|
|
1716
2500
|
result = { kind: 'transport-uncertainty' };
|
|
1717
2501
|
}
|
|
1718
|
-
if (
|
|
2502
|
+
if (!live()) {
|
|
2503
|
+
progress.complete = false;
|
|
1719
2504
|
notifyReady();
|
|
1720
2505
|
return;
|
|
1721
2506
|
}
|
|
@@ -1773,6 +2558,7 @@ export function composeRuntime(options) {
|
|
|
1773
2558
|
}
|
|
1774
2559
|
const previousCapabilities = capabilities;
|
|
1775
2560
|
capabilities = result.capabilities;
|
|
2561
|
+
outcomeRetention = result.retention.outcome;
|
|
1776
2562
|
notifyReady();
|
|
1777
2563
|
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
1778
2564
|
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
@@ -1785,8 +2571,7 @@ export function composeRuntime(options) {
|
|
|
1785
2571
|
member = signedIn;
|
|
1786
2572
|
emitMember();
|
|
1787
2573
|
}
|
|
1788
|
-
await applyDiscovery(result.rooms, local);
|
|
1789
|
-
await synchronize(result.rooms, local, progress, attempt);
|
|
2574
|
+
await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
|
|
1790
2575
|
}
|
|
1791
2576
|
async function ensureAuthorization() {
|
|
1792
2577
|
if (authorization !== undefined)
|
|
@@ -1800,20 +2585,50 @@ export function composeRuntime(options) {
|
|
|
1800
2585
|
throw error;
|
|
1801
2586
|
}
|
|
1802
2587
|
}
|
|
1803
|
-
function
|
|
2588
|
+
async function admittedRead(work) {
|
|
2589
|
+
if (roomReads.generation !== generation) {
|
|
2590
|
+
roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
2591
|
+
}
|
|
2592
|
+
const reads = roomReads;
|
|
2593
|
+
while (reads.free === 0)
|
|
2594
|
+
await new Promise((admit) => reads.waiting.push(admit));
|
|
2595
|
+
reads.free -= 1;
|
|
2596
|
+
try {
|
|
2597
|
+
await work();
|
|
2598
|
+
}
|
|
2599
|
+
finally {
|
|
2600
|
+
reads.free += 1;
|
|
2601
|
+
reads.waiting.shift()?.();
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
function roomTask(locator, work) {
|
|
2605
|
+
return task(roomLane(locator), (live) => admittedRead(() => work(live)));
|
|
2606
|
+
}
|
|
2607
|
+
function task(lane, work) {
|
|
1804
2608
|
if (stopping)
|
|
1805
|
-
return
|
|
1806
|
-
|
|
1807
|
-
|
|
2609
|
+
return Promise.resolve();
|
|
2610
|
+
const held = lanes.get(lane);
|
|
2611
|
+
const current = held?.generation === generation ? held : { generation, chain: Promise.resolve() };
|
|
2612
|
+
const turn = current.generation;
|
|
2613
|
+
const live = () => turn === generation && continuing();
|
|
2614
|
+
const run = current.chain.then(() => work(live)).catch(() => undefined);
|
|
2615
|
+
current.chain = run;
|
|
2616
|
+
lanes.set(lane, current);
|
|
2617
|
+
working.add(run);
|
|
2618
|
+
void run.finally(() => working.delete(run));
|
|
2619
|
+
return run;
|
|
1808
2620
|
}
|
|
1809
2621
|
function sync(attempt) {
|
|
1810
|
-
return task(async () => {
|
|
1811
|
-
if (
|
|
2622
|
+
return task(OPENING_LANE, async (live) => {
|
|
2623
|
+
if (!live()) {
|
|
1812
2624
|
notifyReady();
|
|
1813
2625
|
return;
|
|
1814
2626
|
}
|
|
1815
2627
|
const progress = { complete: true, reachable: true };
|
|
1816
|
-
|
|
2628
|
+
const gate = discover();
|
|
2629
|
+
if (attempt === 'opened')
|
|
2630
|
+
opened = generation;
|
|
2631
|
+
underway = attempt === 'opened' ? { generation, progress } : null;
|
|
1817
2632
|
if (attempt === 'opened')
|
|
1818
2633
|
streamed = progress;
|
|
1819
2634
|
complete = false;
|
|
@@ -1822,12 +2637,12 @@ export function composeRuntime(options) {
|
|
|
1822
2637
|
handed = false;
|
|
1823
2638
|
try {
|
|
1824
2639
|
const local = await openLocal();
|
|
1825
|
-
if (
|
|
2640
|
+
if (!live())
|
|
1826
2641
|
return;
|
|
1827
2642
|
await ensureAuthorization();
|
|
1828
|
-
if (
|
|
2643
|
+
if (!live())
|
|
1829
2644
|
return;
|
|
1830
|
-
await discoverRooms(local, progress, attempt);
|
|
2645
|
+
await discoverRooms(local, progress, attempt, live);
|
|
1831
2646
|
}
|
|
1832
2647
|
catch {
|
|
1833
2648
|
progress.complete = false;
|
|
@@ -1835,23 +2650,31 @@ export function composeRuntime(options) {
|
|
|
1835
2650
|
progress.reachable = false;
|
|
1836
2651
|
}
|
|
1837
2652
|
finally {
|
|
2653
|
+
gate.release();
|
|
1838
2654
|
notifyReady();
|
|
1839
2655
|
}
|
|
2656
|
+
if (!live())
|
|
2657
|
+
return;
|
|
1840
2658
|
if (attempt !== 'opened') {
|
|
1841
|
-
await finishOpening(progress);
|
|
2659
|
+
await finishOpening(progress, live);
|
|
1842
2660
|
return;
|
|
1843
2661
|
}
|
|
1844
2662
|
if (handed || closed || progress.reachable)
|
|
1845
2663
|
return;
|
|
1846
2664
|
underway = null;
|
|
2665
|
+
opened = null;
|
|
1847
2666
|
journal.record('connection reopen');
|
|
1848
|
-
await rest(progress);
|
|
2667
|
+
await rest(progress, live);
|
|
1849
2668
|
options.adapter.roomStream?.reopen();
|
|
1850
2669
|
});
|
|
1851
2670
|
}
|
|
1852
|
-
function closedSocket(revoked) {
|
|
2671
|
+
function closedSocket(revoked, lost) {
|
|
1853
2672
|
const opened = socketOpen;
|
|
1854
2673
|
socketOpen = false;
|
|
2674
|
+
if (lost) {
|
|
2675
|
+
reads.lost();
|
|
2676
|
+
conversations.lost();
|
|
2677
|
+
}
|
|
1855
2678
|
if (revoked) {
|
|
1856
2679
|
void refuse('revoked-authorization');
|
|
1857
2680
|
return;
|
|
@@ -1866,24 +2689,41 @@ export function composeRuntime(options) {
|
|
|
1866
2689
|
});
|
|
1867
2690
|
}
|
|
1868
2691
|
function applyOpening(expired) {
|
|
1869
|
-
|
|
1870
|
-
|
|
2692
|
+
const progress = underway?.generation === generation ? underway.progress : null;
|
|
2693
|
+
const cycle = progress ?? { complete: true, reachable: true };
|
|
2694
|
+
const replacing = reloadNow((room) => expired.includes(room.locator), cycle);
|
|
2695
|
+
if (opened === generation)
|
|
2696
|
+
opened = null;
|
|
2697
|
+
return task(OPENING_LANE, async (live) => {
|
|
1871
2698
|
underway = null;
|
|
1872
|
-
if (
|
|
2699
|
+
if (!live())
|
|
1873
2700
|
return;
|
|
1874
2701
|
if (progress === null) {
|
|
1875
|
-
await
|
|
2702
|
+
await replacing;
|
|
2703
|
+
await rest(cycle, live);
|
|
1876
2704
|
return;
|
|
1877
2705
|
}
|
|
2706
|
+
await roomsCarried(expired);
|
|
2707
|
+
if (!live())
|
|
2708
|
+
return;
|
|
2709
|
+
const replaced = new Set(owed);
|
|
2710
|
+
owed.clear();
|
|
1878
2711
|
try {
|
|
1879
2712
|
const local = await openLocal();
|
|
1880
2713
|
const held = [...discovered.values()].filter((room) => replicas.has(room.locator));
|
|
1881
|
-
const reloading = held.filter((room) => expired.includes(room.locator)
|
|
1882
|
-
|
|
2714
|
+
const reloading = held.filter((room) => !expired.includes(room.locator) &&
|
|
2715
|
+
(replaced.has(room.locator) || revoked(room.locator)));
|
|
2716
|
+
for (const room of reloading)
|
|
2717
|
+
catching.add(room.locator);
|
|
2718
|
+
readSide.recover();
|
|
2719
|
+
const reloads = Promise.allSettled(reloading.map((room) => roomTask(room.locator, (current) => reload(room, local, progress, current))));
|
|
2720
|
+
const reloaded = [...(await reloads), ...(await replacing)];
|
|
1883
2721
|
if (progress.reachable) {
|
|
1884
|
-
for (const room of held)
|
|
1885
|
-
if (!reloading.includes(room))
|
|
2722
|
+
for (const room of held) {
|
|
2723
|
+
if (!expired.includes(room.locator) && !reloading.includes(room)) {
|
|
1886
2724
|
await confirmModels(room, local);
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
1887
2727
|
}
|
|
1888
2728
|
const failed = reloaded.find((outcome) => outcome.status === 'rejected');
|
|
1889
2729
|
if (failed !== undefined)
|
|
@@ -1893,21 +2733,21 @@ export function composeRuntime(options) {
|
|
|
1893
2733
|
progress.complete = false;
|
|
1894
2734
|
progress.reachable = false;
|
|
1895
2735
|
}
|
|
1896
|
-
|
|
2736
|
+
if (live())
|
|
2737
|
+
await finishOpening(progress, live);
|
|
1897
2738
|
});
|
|
1898
2739
|
}
|
|
1899
|
-
async function
|
|
1900
|
-
const progress = { complete: true, reachable: true };
|
|
2740
|
+
async function reloadNow(replaced, progress) {
|
|
1901
2741
|
try {
|
|
1902
2742
|
const local = await openLocal();
|
|
1903
|
-
await Promise.allSettled([...discovered.values()]
|
|
1904
|
-
.filter((room) => replicas.has(room.locator) &&
|
|
1905
|
-
.map((room) => reload(room, local, progress)));
|
|
2743
|
+
return await Promise.allSettled([...discovered.values()]
|
|
2744
|
+
.filter((room) => replicas.has(room.locator) && replaced(room))
|
|
2745
|
+
.map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
|
|
1906
2746
|
}
|
|
1907
2747
|
catch {
|
|
1908
2748
|
progress.reachable = false;
|
|
2749
|
+
return [];
|
|
1909
2750
|
}
|
|
1910
|
-
await rest(progress);
|
|
1911
2751
|
}
|
|
1912
2752
|
async function confirmModels(room, local) {
|
|
1913
2753
|
for (const attached of ['collection', 'typed-selection']) {
|
|
@@ -1920,38 +2760,94 @@ export function composeRuntime(options) {
|
|
|
1920
2760
|
function submitLater(locator) {
|
|
1921
2761
|
if (localSaves)
|
|
1922
2762
|
return;
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
2763
|
+
void recovery.attempt(`submit ${locator}`, (signal) => submitPass(locator, signal));
|
|
2764
|
+
}
|
|
2765
|
+
async function submitPass(locator, signal) {
|
|
2766
|
+
const room = discovered.get(locator);
|
|
2767
|
+
if (closed || stopping || discoveryPaused || room === undefined)
|
|
2768
|
+
return 'done';
|
|
2769
|
+
const progress = { complete: true, reachable: true };
|
|
2770
|
+
let next;
|
|
2771
|
+
try {
|
|
2772
|
+
await ensureAuthorization();
|
|
2773
|
+
if (authorization === undefined)
|
|
2774
|
+
return 'done';
|
|
2775
|
+
next = await submitRoom(room, await openLocal(), authorization, progress, signal);
|
|
2776
|
+
}
|
|
2777
|
+
catch {
|
|
2778
|
+
progress.reachable = false;
|
|
2779
|
+
next = 'again';
|
|
2780
|
+
}
|
|
2781
|
+
if (signal.aborted)
|
|
2782
|
+
return 'done';
|
|
2783
|
+
await rest(progress);
|
|
2784
|
+
return next === 'again' && !socketOpen ? 'done' : next;
|
|
2785
|
+
}
|
|
2786
|
+
function repairLater(locator, when) {
|
|
2787
|
+
if (!socketOpen || repairing.has(locator))
|
|
1926
2788
|
return;
|
|
2789
|
+
void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
|
|
2790
|
+
}
|
|
2791
|
+
async function repairPass(locator, signal) {
|
|
2792
|
+
const room = discovered.get(locator);
|
|
2793
|
+
if (!socketOpen || closed || stopping || discoveryPaused || !stale.has(locator))
|
|
2794
|
+
return 'done';
|
|
2795
|
+
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2796
|
+
return 'done';
|
|
2797
|
+
const progress = { complete: true, reachable: true };
|
|
2798
|
+
repairing.add(locator);
|
|
2799
|
+
try {
|
|
2800
|
+
const local = await openLocal();
|
|
2801
|
+
await roomTask(locator, async (live) => {
|
|
2802
|
+
const current = () => live() && !signal.aborted;
|
|
2803
|
+
if (!current() || authorization === undefined)
|
|
2804
|
+
return;
|
|
2805
|
+
const withheld = await Promise.race([
|
|
2806
|
+
advance(room, local, authorization, current),
|
|
2807
|
+
abandonedOn(signal),
|
|
2808
|
+
]);
|
|
2809
|
+
if (withheld !== null && current())
|
|
2810
|
+
await withhold(withheld, room, local, progress);
|
|
2811
|
+
});
|
|
1927
2812
|
}
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
2813
|
+
catch {
|
|
2814
|
+
stale.add(locator);
|
|
2815
|
+
progress.reachable = false;
|
|
2816
|
+
}
|
|
2817
|
+
finally {
|
|
2818
|
+
repairing.delete(locator);
|
|
2819
|
+
}
|
|
2820
|
+
if (signal.aborted)
|
|
2821
|
+
return 'done';
|
|
2822
|
+
await rest(progress);
|
|
2823
|
+
if (!stale.has(locator)) {
|
|
2824
|
+
if ((replicas.get(locator)?.queue.length ?? 0) > 0)
|
|
2825
|
+
submitLater(locator);
|
|
2826
|
+
return 'done';
|
|
2827
|
+
}
|
|
2828
|
+
if (!repairable.has(locator))
|
|
2829
|
+
return 'done';
|
|
2830
|
+
const reset = repairable.get(locator);
|
|
2831
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
2832
|
+
}
|
|
2833
|
+
function resendLater() {
|
|
2834
|
+
if (!socketOpen)
|
|
2835
|
+
return;
|
|
2836
|
+
void recovery.attempt('resend', resendPass, { again: true });
|
|
2837
|
+
}
|
|
2838
|
+
async function resendPass(signal) {
|
|
2839
|
+
if (!socketOpen || closed || stopping || discoveryPaused)
|
|
2840
|
+
return 'done';
|
|
2841
|
+
const reading = { complete: true, reachable: true };
|
|
2842
|
+
try {
|
|
2843
|
+
await conversations.resend(reading);
|
|
2844
|
+
}
|
|
2845
|
+
catch {
|
|
2846
|
+
reading.reachable = false;
|
|
2847
|
+
}
|
|
2848
|
+
if (signal.aborted)
|
|
2849
|
+
return 'done';
|
|
2850
|
+
return reading.reachable && reading.complete ? 'done' : 'again';
|
|
1955
2851
|
}
|
|
1956
2852
|
function submitAll() {
|
|
1957
2853
|
for (const [locator, replica] of replicas) {
|
|
@@ -1959,31 +2855,42 @@ export function composeRuntime(options) {
|
|
|
1959
2855
|
submitLater(locator);
|
|
1960
2856
|
}
|
|
1961
2857
|
}
|
|
1962
|
-
async function finishOpening(progress) {
|
|
2858
|
+
async function finishOpening(progress, live) {
|
|
1963
2859
|
try {
|
|
1964
|
-
if (
|
|
2860
|
+
if (!live()) {
|
|
1965
2861
|
progress.complete = false;
|
|
1966
2862
|
return;
|
|
1967
2863
|
}
|
|
2864
|
+
reads.applied();
|
|
1968
2865
|
submitAll();
|
|
2866
|
+
for (const locator of stale)
|
|
2867
|
+
if (repairable.has(locator))
|
|
2868
|
+
repairLater(locator, {});
|
|
1969
2869
|
await conversations.reconcile();
|
|
1970
2870
|
if (status.providerUnreachable)
|
|
1971
2871
|
conversations.reconnected();
|
|
1972
2872
|
const reading = { complete: true, reachable: true };
|
|
1973
|
-
|
|
1974
|
-
|
|
2873
|
+
if (demand.conversations)
|
|
2874
|
+
await conversations.catchUp(reading);
|
|
2875
|
+
if (progress.reachable && reading.reachable)
|
|
2876
|
+
await conversations.resend(reading);
|
|
2877
|
+
if (!reading.reachable) {
|
|
1975
2878
|
progress.reachable = false;
|
|
2879
|
+
resendLater();
|
|
2880
|
+
}
|
|
1976
2881
|
}
|
|
1977
2882
|
catch {
|
|
1978
2883
|
progress.complete = false;
|
|
1979
2884
|
}
|
|
1980
2885
|
finally {
|
|
1981
|
-
|
|
1982
|
-
|
|
2886
|
+
if (live()) {
|
|
2887
|
+
complete = progress.complete;
|
|
2888
|
+
await rest(progress, live);
|
|
2889
|
+
}
|
|
1983
2890
|
}
|
|
1984
2891
|
}
|
|
1985
|
-
async function rest(progress) {
|
|
1986
|
-
if (
|
|
2892
|
+
async function rest(progress, live = () => !closed) {
|
|
2893
|
+
if (!live())
|
|
1987
2894
|
return;
|
|
1988
2895
|
let lastCompleted = status.lastCompleted;
|
|
1989
2896
|
let synced = complete && stale.size === 0 && !pending();
|
|
@@ -1991,6 +2898,8 @@ export function composeRuntime(options) {
|
|
|
1991
2898
|
const completedAt = new Date().toISOString();
|
|
1992
2899
|
try {
|
|
1993
2900
|
await (await openLocal()).complete(completedAt);
|
|
2901
|
+
if (!live())
|
|
2902
|
+
return;
|
|
1994
2903
|
recorded = true;
|
|
1995
2904
|
lastCompleted = completedAt;
|
|
1996
2905
|
}
|
|
@@ -2005,40 +2914,68 @@ export function composeRuntime(options) {
|
|
|
2005
2914
|
...attention(),
|
|
2006
2915
|
});
|
|
2007
2916
|
}
|
|
2917
|
+
function discover() {
|
|
2918
|
+
let release = () => { };
|
|
2919
|
+
const done = new Promise((resolve) => {
|
|
2920
|
+
release = resolve;
|
|
2921
|
+
});
|
|
2922
|
+
discovering = { generation, done, release };
|
|
2923
|
+
return discovering;
|
|
2924
|
+
}
|
|
2008
2925
|
function applyRoom(locator, roomVersion) {
|
|
2009
2926
|
announced.set(locator, roomVersion);
|
|
2010
|
-
return task(async () => {
|
|
2011
|
-
|
|
2012
|
-
|
|
2927
|
+
return task(roomLane(locator), async (live) => {
|
|
2928
|
+
if (discovering?.generation === generation)
|
|
2929
|
+
await discovering.done;
|
|
2930
|
+
const saving = answering.get(locator);
|
|
2931
|
+
if (saving?.generation === generation)
|
|
2932
|
+
await saving.answered;
|
|
2933
|
+
if (!live() || authorization === undefined)
|
|
2013
2934
|
return;
|
|
2014
2935
|
const room = discovered.get(locator);
|
|
2015
2936
|
const held = replicas.get(locator);
|
|
2016
|
-
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2937
|
+
if (room === undefined || room.replica.admission !== 'admitted' || !acquires(room))
|
|
2017
2938
|
return;
|
|
2018
2939
|
if (held !== undefined &&
|
|
2019
2940
|
(held.confirmed.record.replica.selection !== room.replica.selection ||
|
|
2020
2941
|
options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion))) {
|
|
2021
2942
|
return;
|
|
2022
2943
|
}
|
|
2023
|
-
const during = underway;
|
|
2944
|
+
const during = underway?.generation === generation ? underway.progress : null;
|
|
2024
2945
|
const progress = during ?? { complete: true, reachable: true };
|
|
2025
2946
|
if (during === null)
|
|
2026
2947
|
moveTo(transferring(status));
|
|
2027
2948
|
try {
|
|
2028
2949
|
const local = await openLocal();
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2950
|
+
await admittedRead(async () => {
|
|
2951
|
+
if (!live() || authorization === undefined)
|
|
2952
|
+
return;
|
|
2953
|
+
const withheld = await advance(room, local, authorization, live);
|
|
2954
|
+
if (withheld !== null)
|
|
2955
|
+
await withhold(withheld, room, local, progress);
|
|
2956
|
+
});
|
|
2032
2957
|
}
|
|
2033
2958
|
catch {
|
|
2034
2959
|
stale.add(locator);
|
|
2035
2960
|
}
|
|
2036
2961
|
finally {
|
|
2037
2962
|
if (during === null)
|
|
2038
|
-
await rest(progress);
|
|
2963
|
+
await rest(progress, live);
|
|
2039
2964
|
}
|
|
2040
2965
|
});
|
|
2041
2966
|
}
|
|
2967
|
+
function roomsCarried(replacing) {
|
|
2968
|
+
const replaced = new Set(replacing.map(roomLane));
|
|
2969
|
+
return Promise.all([...lanes]
|
|
2970
|
+
.filter(([lane, held]) => lane !== OPENING_LANE && !replaced.has(lane) && held.generation === generation)
|
|
2971
|
+
.map(([, held]) => held.chain));
|
|
2972
|
+
}
|
|
2973
|
+
const receipts = {
|
|
2974
|
+
heads: async () => (await openLocal()).receivedHeads(),
|
|
2975
|
+
parts: async (key, count) => (await openLocal()).receivedParts(key, count),
|
|
2976
|
+
keep: async (key, head, part) => (await openLocal()).keepReceived(key, head, part),
|
|
2977
|
+
drop: async (keys) => (await openLocal()).dropReceived(keys),
|
|
2978
|
+
};
|
|
2042
2979
|
function observeStream() {
|
|
2043
2980
|
const stream = options.adapter.roomStream;
|
|
2044
2981
|
if (stream === undefined || withdrawStream !== undefined)
|
|
@@ -2048,15 +2985,19 @@ export function composeRuntime(options) {
|
|
|
2048
2985
|
return;
|
|
2049
2986
|
if (event.kind === 'opened') {
|
|
2050
2987
|
socketOpen = true;
|
|
2988
|
+
generation += 1;
|
|
2989
|
+
reads.opened();
|
|
2990
|
+
conversations.reopened();
|
|
2991
|
+
recovery.resume();
|
|
2051
2992
|
void sync('opened');
|
|
2052
2993
|
}
|
|
2053
2994
|
else if (event.kind === 'closed')
|
|
2054
|
-
closedSocket(event.revoked);
|
|
2995
|
+
closedSocket(event.revoked, event.lost);
|
|
2055
2996
|
else if (event.kind === 'opening')
|
|
2056
2997
|
void applyOpening(event.expired);
|
|
2057
2998
|
else
|
|
2058
2999
|
void applyRoom(event.locator, event.roomVersion);
|
|
2059
|
-
});
|
|
3000
|
+
}, receipts);
|
|
2060
3001
|
}
|
|
2061
3002
|
function writesHeld() {
|
|
2062
3003
|
return serviceMode === 'write-halted' || haltedRefusal;
|
|
@@ -2071,6 +3012,7 @@ export function composeRuntime(options) {
|
|
|
2071
3012
|
haltedRefusal = false;
|
|
2072
3013
|
if (mode !== serviceMode) {
|
|
2073
3014
|
serviceMode = mode;
|
|
3015
|
+
queueReadiness();
|
|
2074
3016
|
journal.record(`service mode ${mode}`);
|
|
2075
3017
|
for (const listener of serviceModeListeners)
|
|
2076
3018
|
listener(mode);
|
|
@@ -2083,6 +3025,8 @@ export function composeRuntime(options) {
|
|
|
2083
3025
|
if (stream === undefined || withdrawConversations !== undefined)
|
|
2084
3026
|
return;
|
|
2085
3027
|
withdrawConversations = stream.observe((event) => {
|
|
3028
|
+
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted')
|
|
3029
|
+
queueReadiness();
|
|
2086
3030
|
if (conversations.receive(event)) {
|
|
2087
3031
|
void ready.then(scheduleCurrency);
|
|
2088
3032
|
return;
|
|
@@ -2094,17 +3038,18 @@ export function composeRuntime(options) {
|
|
|
2094
3038
|
});
|
|
2095
3039
|
}
|
|
2096
3040
|
function scheduleCurrency() {
|
|
2097
|
-
if (currencyQueued || closed || stopping)
|
|
3041
|
+
if (currencyQueued === generation || closed || stopping)
|
|
2098
3042
|
return;
|
|
2099
|
-
currencyQueued =
|
|
2100
|
-
void task(runCurrency);
|
|
3043
|
+
currencyQueued = generation;
|
|
3044
|
+
void task(CONVERSATION_LANE, runCurrency);
|
|
2101
3045
|
}
|
|
2102
|
-
async function runCurrency() {
|
|
2103
|
-
currencyQueued
|
|
2104
|
-
|
|
3046
|
+
async function runCurrency(live) {
|
|
3047
|
+
if (currencyQueued === generation)
|
|
3048
|
+
currencyQueued = null;
|
|
3049
|
+
if (!live())
|
|
2105
3050
|
return;
|
|
2106
3051
|
await conversations.reconcile();
|
|
2107
|
-
if (conversations.current()) {
|
|
3052
|
+
if (conversations.current() || !demand.conversations) {
|
|
2108
3053
|
show();
|
|
2109
3054
|
return;
|
|
2110
3055
|
}
|
|
@@ -2116,19 +3061,24 @@ export function composeRuntime(options) {
|
|
|
2116
3061
|
catch {
|
|
2117
3062
|
progress.reachable = false;
|
|
2118
3063
|
}
|
|
2119
|
-
await rest(progress);
|
|
3064
|
+
await rest(progress, live);
|
|
2120
3065
|
}
|
|
2121
3066
|
function close() {
|
|
2122
3067
|
if (closed)
|
|
2123
3068
|
return;
|
|
2124
3069
|
closed = true;
|
|
3070
|
+
if (narrowing !== null)
|
|
3071
|
+
clearTimeout(narrowing);
|
|
3072
|
+
if (rechecking !== null)
|
|
3073
|
+
clearInterval(rechecking);
|
|
3074
|
+
readinessWatches.clear();
|
|
2125
3075
|
withdrawStream?.();
|
|
2126
3076
|
withdrawStream = undefined;
|
|
2127
3077
|
withdrawConversations?.();
|
|
2128
3078
|
withdrawConversations = undefined;
|
|
2129
3079
|
withdrawServiceMode?.();
|
|
2130
3080
|
conversations.close();
|
|
2131
|
-
|
|
3081
|
+
void recovery.retire();
|
|
2132
3082
|
store?.close();
|
|
2133
3083
|
void opening?.then((opened) => {
|
|
2134
3084
|
opened.close();
|
|
@@ -2146,9 +3096,8 @@ export function composeRuntime(options) {
|
|
|
2146
3096
|
}
|
|
2147
3097
|
async function stop() {
|
|
2148
3098
|
stopping = true;
|
|
2149
|
-
|
|
2150
|
-
await
|
|
2151
|
-
await Promise.all([...submitting.values()].map((run) => run.done));
|
|
3099
|
+
await recovery.retire();
|
|
3100
|
+
await Promise.all(working);
|
|
2152
3101
|
await mutations;
|
|
2153
3102
|
close();
|
|
2154
3103
|
}
|
|
@@ -2170,17 +3119,101 @@ export function composeRuntime(options) {
|
|
|
2170
3119
|
return planOf(input, roomFacts(reach), planReach(reach));
|
|
2171
3120
|
}
|
|
2172
3121
|
function watchPlan(plan, deliver) {
|
|
2173
|
-
|
|
2174
|
-
|
|
3122
|
+
let seeded = false;
|
|
3123
|
+
let pending = true;
|
|
3124
|
+
const shown = () => {
|
|
3125
|
+
const seed = seeds.get(plan.key);
|
|
3126
|
+
if (seed === undefined || seeded || !pending)
|
|
3127
|
+
return;
|
|
3128
|
+
seeded = true;
|
|
3129
|
+
deliver(seededResult(seed));
|
|
3130
|
+
};
|
|
3131
|
+
const watchers = seedWatchers.get(plan.key) ?? new Set();
|
|
3132
|
+
seedWatchers.set(plan.key, watchers);
|
|
3133
|
+
watchers.add(shown);
|
|
3134
|
+
const stop = readSide.watch(plan, identityOf(plan), () => {
|
|
3135
|
+
const cover = covered(plan);
|
|
3136
|
+
awaitPreparation(cover);
|
|
3137
|
+
return seedBehind(plan, cover);
|
|
3138
|
+
}, (message) => {
|
|
3139
|
+
const answered = message;
|
|
3140
|
+
pending = whole(answered) && 'pending' in answered.result;
|
|
3141
|
+
if (pending && seeds.has(plan.key)) {
|
|
3142
|
+
shown();
|
|
3143
|
+
return;
|
|
3144
|
+
}
|
|
3145
|
+
seeded = false;
|
|
3146
|
+
deliver(message);
|
|
3147
|
+
});
|
|
3148
|
+
return () => {
|
|
3149
|
+
watchers.delete(shown);
|
|
3150
|
+
if (watchers.size === 0)
|
|
3151
|
+
seedWatchers.delete(plan.key);
|
|
3152
|
+
stop();
|
|
3153
|
+
};
|
|
2175
3154
|
}
|
|
2176
3155
|
async function searched(input, reach) {
|
|
2177
3156
|
const plan = searchPlanOf(input);
|
|
2178
|
-
const
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
3157
|
+
const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
|
|
3158
|
+
for (;;) {
|
|
3159
|
+
const cover = covered(filter);
|
|
3160
|
+
awaitPreparation(cover);
|
|
3161
|
+
const pending = 'pending' in cover ? [...cover.pending] : [];
|
|
3162
|
+
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
3163
|
+
const held = covered({
|
|
3164
|
+
...filter,
|
|
3165
|
+
rooms: reachedRooms(filter).filter((room) => !awaited.has(room)),
|
|
3166
|
+
});
|
|
3167
|
+
const covering = 'pending' in held ? [] : held.coverage.rooms;
|
|
3168
|
+
const unindexed = covering
|
|
3169
|
+
.filter(({ room }) => !searchIndex.rooms().has(room))
|
|
3170
|
+
.map(({ room }) => room);
|
|
3171
|
+
for (const room of unindexed)
|
|
3172
|
+
pending.push({ room, awaits: 'index' });
|
|
3173
|
+
const listed = conversations.searchable();
|
|
3174
|
+
const named = reach !== null && !reach.conversations
|
|
3175
|
+
? []
|
|
3176
|
+
: (plan.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
3177
|
+
const waiting = named.filter((one) => !searchIndex.conversations().has(one));
|
|
3178
|
+
for (const conversation of waiting)
|
|
3179
|
+
pending.push({ conversation, awaits: 'index' });
|
|
3180
|
+
if (unindexed.length > 0 || waiting.length > 0)
|
|
3181
|
+
index(unindexed, waiting);
|
|
3182
|
+
const complete = pending.length === 0 && !('pending' in cover);
|
|
3183
|
+
const answering = complete
|
|
3184
|
+
? filter
|
|
3185
|
+
: {
|
|
3186
|
+
...filter,
|
|
3187
|
+
rooms: covering
|
|
3188
|
+
.filter(({ room }) => searchIndex.rooms().has(room))
|
|
3189
|
+
.map(({ room }) => room),
|
|
3190
|
+
};
|
|
3191
|
+
const answered = covered(answering);
|
|
3192
|
+
if ('pending' in answered)
|
|
3193
|
+
continue;
|
|
3194
|
+
const searchedConversations = named.filter((one) => searchIndex.conversations().has(one));
|
|
3195
|
+
const seen = searchIndex.changes();
|
|
3196
|
+
await searchIndex.caughtUp();
|
|
3197
|
+
const selected = roomsAlone(answering) ? null : await rowsQuery(answering);
|
|
3198
|
+
if (selected !== null && 'pending' in selected)
|
|
3199
|
+
continue;
|
|
3200
|
+
if (searchIndex.changes() !== seen ||
|
|
3201
|
+
JSON.stringify(covered(answering)) !== JSON.stringify(answered))
|
|
3202
|
+
continue;
|
|
3203
|
+
const hits = searchIndex.search(plan, selected === null ? null : selected.documents, searchedConversations, answering.rooms === null ? null : new Set(answering.rooms), plan.collection
|
|
3204
|
+
? new Map(answered.coverage.rooms.map(({ room }) => [
|
|
3205
|
+
room,
|
|
3206
|
+
replicas.get(room)?.confirmed.record.typedSelection?.roots ?? [],
|
|
3207
|
+
]))
|
|
3208
|
+
: null);
|
|
3209
|
+
const coverage = {
|
|
3210
|
+
rooms: answered.coverage.rooms,
|
|
3211
|
+
conversations: searchedConversations,
|
|
3212
|
+
index: searchIndex.generation(),
|
|
3213
|
+
overlay: answered.coverage.overlay,
|
|
3214
|
+
};
|
|
3215
|
+
return complete ? { ...hits, coverage } : { pending, ...hits, coverage };
|
|
3216
|
+
}
|
|
2184
3217
|
}
|
|
2185
3218
|
const watch = (topic, input, deliver) => {
|
|
2186
3219
|
if (topic === 'room-drafts') {
|
|
@@ -2226,6 +3259,9 @@ export function composeRuntime(options) {
|
|
|
2226
3259
|
presentationListeners.delete(listener);
|
|
2227
3260
|
};
|
|
2228
3261
|
}
|
|
3262
|
+
if (topic === 'readiness') {
|
|
3263
|
+
return watchReadiness({ target: '', code: 'stored', capacity: null, search: 'unused' }, null, deliver);
|
|
3264
|
+
}
|
|
2229
3265
|
if (topic === 'rooms') {
|
|
2230
3266
|
roomListeners.add(deliver);
|
|
2231
3267
|
deliver(listed());
|
|
@@ -2392,21 +3428,29 @@ export function composeRuntime(options) {
|
|
|
2392
3428
|
return journal.export();
|
|
2393
3429
|
},
|
|
2394
3430
|
persistence() {
|
|
2395
|
-
return
|
|
3431
|
+
return persistence === 'granted';
|
|
3432
|
+
},
|
|
3433
|
+
async retry() {
|
|
3434
|
+
journal.record('retry');
|
|
3435
|
+
unindexable.clear();
|
|
3436
|
+
queueReadiness();
|
|
3437
|
+
resume();
|
|
2396
3438
|
},
|
|
2397
3439
|
observe: observing(watch),
|
|
2398
3440
|
async reauthorize() {
|
|
2399
3441
|
if (refusal === null || !refusal.reauthorizationOffered)
|
|
2400
3442
|
return;
|
|
2401
3443
|
journal.record('connection reauthorize');
|
|
2402
|
-
await
|
|
3444
|
+
await Promise.all([...lanes.values()]
|
|
3445
|
+
.filter((held) => held.generation === generation)
|
|
3446
|
+
.map((held) => held.chain));
|
|
2403
3447
|
await mutations;
|
|
2404
3448
|
authorization = undefined;
|
|
2405
3449
|
discoveryPaused = false;
|
|
2406
3450
|
if (options.adapter.roomStream === undefined)
|
|
2407
3451
|
await sync('unstreamed');
|
|
2408
3452
|
else
|
|
2409
|
-
await task(ensureAuthorization);
|
|
3453
|
+
await task(OPENING_LANE, ensureAuthorization);
|
|
2410
3454
|
},
|
|
2411
3455
|
};
|
|
2412
3456
|
const reachable = {
|
|
@@ -2417,6 +3461,22 @@ export function composeRuntime(options) {
|
|
|
2417
3461
|
query: (plan) => rowsQuery(plan),
|
|
2418
3462
|
watchPlan,
|
|
2419
3463
|
search: searched,
|
|
3464
|
+
watchReadiness,
|
|
3465
|
+
};
|
|
3466
|
+
return {
|
|
3467
|
+
md,
|
|
3468
|
+
seed,
|
|
3469
|
+
stop,
|
|
3470
|
+
lose: loseRooms,
|
|
3471
|
+
watch,
|
|
3472
|
+
acquire,
|
|
3473
|
+
persisted(answer) {
|
|
3474
|
+
if (answer === persistence)
|
|
3475
|
+
return;
|
|
3476
|
+
persistence = answer;
|
|
3477
|
+
journal.record(`persistence ${answer}`);
|
|
3478
|
+
queueReadiness();
|
|
3479
|
+
},
|
|
3480
|
+
within: (reach, port) => narrowed(reachable, reach, port),
|
|
2420
3481
|
};
|
|
2421
|
-
return { md, stop, watch, within: (reach) => narrowed(reachable, reach) };
|
|
2422
3482
|
}
|