@agentshouse/mdruntime 0.1.0-c30ed2b8e04d5 → 0.1.0-c36e0a098a0e3
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 +266 -183
- package/README.md +20 -9
- 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 +101 -19
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +15 -2
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +23 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +17 -5
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +110 -37
- package/dist/change-set.d.ts +2 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +48 -16
- package/dist/change.d.ts +15 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +26 -1
- package/dist/chat/acts.d.ts +33 -0
- package/dist/chat/acts.d.ts.map +1 -0
- package/dist/chat/acts.js +36 -0
- package/dist/chat/context.d.ts +3 -0
- package/dist/chat/context.d.ts.map +1 -0
- package/dist/chat/context.js +10 -0
- package/dist/chat/input.d.ts +9 -0
- package/dist/chat/input.d.ts.map +1 -0
- package/dist/chat/input.js +99 -0
- package/dist/chat/kit.d.ts +60 -0
- package/dist/chat/kit.d.ts.map +1 -0
- package/dist/chat/kit.js +123 -0
- package/dist/chat/lasting.d.ts +6 -0
- package/dist/chat/lasting.d.ts.map +1 -0
- package/dist/chat/lasting.js +19 -0
- package/dist/chat/list.d.ts +26 -0
- package/dist/chat/list.d.ts.map +1 -0
- package/dist/chat/list.js +62 -0
- package/dist/chat/markdown.d.ts +20 -0
- package/dist/chat/markdown.d.ts.map +1 -0
- package/dist/chat/markdown.js +188 -0
- package/dist/chat/message.d.ts +6 -0
- package/dist/chat/message.d.ts.map +1 -0
- package/dist/chat/message.js +192 -0
- package/dist/chat/messages.d.ts +21 -0
- package/dist/chat/messages.d.ts.map +1 -0
- package/dist/chat/messages.js +151 -0
- package/dist/chat/name.d.ts +6 -0
- package/dist/chat/name.d.ts.map +1 -0
- package/dist/chat/name.js +37 -0
- package/dist/chat/opened.d.ts +11 -0
- package/dist/chat/opened.d.ts.map +1 -0
- package/dist/chat/opened.js +17 -0
- package/dist/chat/overlay.d.ts +31 -0
- package/dist/chat/overlay.d.ts.map +1 -0
- package/dist/chat/overlay.js +97 -0
- package/dist/chat/parts.d.ts +57 -0
- package/dist/chat/parts.d.ts.map +1 -0
- package/dist/chat/parts.js +119 -0
- package/dist/chat/process.d.ts +20 -0
- package/dist/chat/process.d.ts.map +1 -0
- package/dist/chat/process.js +32 -0
- package/dist/chat/provider.d.ts +63 -0
- package/dist/chat/provider.d.ts.map +1 -0
- package/dist/chat/provider.js +620 -0
- package/dist/chat/question.d.ts +9 -0
- package/dist/chat/question.d.ts.map +1 -0
- package/dist/chat/question.js +80 -0
- package/dist/chat/questions.d.ts +31 -0
- package/dist/chat/questions.d.ts.map +1 -0
- package/dist/chat/questions.js +111 -0
- package/dist/chat/read.d.ts +5 -0
- package/dist/chat/read.d.ts.map +1 -0
- package/dist/chat/read.js +13 -0
- package/dist/chat/ready.d.ts +33 -0
- package/dist/chat/ready.d.ts.map +1 -0
- package/dist/chat/ready.js +91 -0
- package/dist/chat/style.d.ts +4 -0
- package/dist/chat/style.d.ts.map +1 -0
- package/dist/chat/style.js +302 -0
- package/dist/chat.d.ts +14 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/chat.js +8 -0
- package/dist/client.d.ts +27 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +68 -16
- package/dist/collection.d.ts +21 -12
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +271 -23
- package/dist/connection.d.ts +45 -17
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1669 -397
- package/dist/conversation.d.ts +39 -14
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +468 -208
- 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/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -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 +13 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +51 -17
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +19 -1
- package/dist/index.d.ts +14 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/memory.d.ts +7 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +89 -23
- package/dist/model.d.ts +14 -8
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +15 -3
- package/dist/observation.d.ts +5 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +22 -11
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/outcome.d.ts +32 -3
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +42 -1
- package/dist/overlay.d.ts +15 -3
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +94 -77
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +0 -16
- package/dist/query.d.ts +33 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +74 -33
- package/dist/reach.d.ts +8 -2
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +68 -39
- package/dist/react.d.ts +43 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +64 -0
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +391 -129
- package/dist/readiness.d.ts +53 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -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 +10 -0
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +43 -38
- package/dist/replica.d.ts +9 -6
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +116 -68
- package/dist/room-draft.d.ts +26 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +132 -23
- package/dist/rows.d.ts +10 -14
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +85 -50
- package/dist/save.d.ts +32 -1
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +464 -89
- package/dist/search.d.ts +31 -8
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +108 -45
- 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 +6 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -8
- package/dist/store.d.ts +74 -17
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +531 -198
- 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 +142 -33
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +62 -31
- package/package.json +31 -7
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
package/dist/connection.js
CHANGED
|
@@ -3,19 +3,30 @@ import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
|
|
|
3
3
|
import { observing } from './observation.js';
|
|
4
4
|
import { filterOf, identityOf, planOf, } from './query.js';
|
|
5
5
|
import { createReadSide } from './read-side.js';
|
|
6
|
+
import { markdownPath, roomOfKey } from './rows.js';
|
|
6
7
|
import { createSearchIndex, searchPlanOf, } from './search.js';
|
|
7
8
|
import { bootstrapRoom } from './bootstrap.js';
|
|
8
|
-
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
9
|
+
import { changeSet, entering, mergeChangeSets } from './change-set.js';
|
|
9
10
|
import { createConversationStore, } from './conversation.js';
|
|
10
|
-
import {
|
|
11
|
+
import { queuedFile } from './change.js';
|
|
12
|
+
import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
|
|
13
|
+
import { runtimeError } from './failure.js';
|
|
11
14
|
import { createLog } from './log.js';
|
|
15
|
+
import { binaryRecord } from './binary.js';
|
|
16
|
+
import { absentBinary, binaryAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
|
|
17
|
+
import { createRecovery } from './recovery.js';
|
|
12
18
|
import { refreshRoom } from './refresh.js';
|
|
13
|
-
import { buildBatch } from './save.js';
|
|
14
|
-
import { applyReturned, compositionOf,
|
|
15
|
-
import { changedParts, changesOf, held, readParts, returnedInto, underneath, } from './room-draft.js';
|
|
16
|
-
import {
|
|
19
|
+
import { buildBatch, joined, limitedIn, requeued } from './save.js';
|
|
20
|
+
import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, withdrawals, } from './replica.js';
|
|
21
|
+
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
22
|
+
import { same, saving, STARTING, transferring, } from './status.js';
|
|
17
23
|
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
18
|
-
import {
|
|
24
|
+
import { demanded, narrower, WHOLE_DEMAND, wider } from './acquisition.js';
|
|
25
|
+
import { readinessOf, SETTLED_ROOMS, } from './readiness.js';
|
|
26
|
+
import { FencedMap } from './fence.js';
|
|
27
|
+
import { tally } from './shared.js';
|
|
28
|
+
import { inChunks, ROW_WEIGHT, yielded } from './turn.js';
|
|
29
|
+
import { identityTime, roomDraftRevision } from './identity.js';
|
|
19
30
|
import { beneath, normalize } from './paths.js';
|
|
20
31
|
import { openRuntimeStore, } from './store.js';
|
|
21
32
|
import { unconfirmed } from './transfer.js';
|
|
@@ -33,20 +44,59 @@ function declaredSelection(room) {
|
|
|
33
44
|
root !== '' &&
|
|
34
45
|
selection.roots.every((other, at) => at === index || !within(other, root)));
|
|
35
46
|
}
|
|
47
|
+
const OPENING_LANE = 'opening';
|
|
48
|
+
const CONVERSATION_LANE = 'conversations';
|
|
49
|
+
const ROOM_READS = 256;
|
|
50
|
+
const ROOM_ATTEMPTS = 16;
|
|
51
|
+
const outside = new Map();
|
|
52
|
+
function outsideOf(roots) {
|
|
53
|
+
const key = JSON.stringify(roots);
|
|
54
|
+
const held = outside.get(key);
|
|
55
|
+
if (held !== undefined)
|
|
56
|
+
return held;
|
|
57
|
+
const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
|
|
58
|
+
const made = {
|
|
59
|
+
documents: (path) => (markdownPath(path) ? counted(path) : 0),
|
|
60
|
+
binaries: counted,
|
|
61
|
+
};
|
|
62
|
+
outside.set(key, made);
|
|
63
|
+
return made;
|
|
64
|
+
}
|
|
65
|
+
function abandonedOn(signal) {
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
if (signal.aborted)
|
|
68
|
+
resolve(null);
|
|
69
|
+
else
|
|
70
|
+
signal.addEventListener('abort', () => resolve(null), { once: true });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function roomLane(locator) {
|
|
74
|
+
return `room ${locator}`;
|
|
75
|
+
}
|
|
36
76
|
export function composeRuntime(options) {
|
|
77
|
+
const localSaves = options.adapter.localSaves === true;
|
|
37
78
|
let authorization = options.authorization;
|
|
38
79
|
let opening;
|
|
39
80
|
let store;
|
|
40
|
-
const
|
|
81
|
+
const fences = new Map();
|
|
82
|
+
const isFenced = (locator) => fences.has(locator);
|
|
83
|
+
let fencings = 0;
|
|
84
|
+
const lastFenced = new Map();
|
|
85
|
+
const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
|
|
86
|
+
let discoveries = 0;
|
|
87
|
+
const replicas = new FencedMap(isFenced);
|
|
88
|
+
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
41
89
|
const pendings = new Map();
|
|
42
|
-
const roomDrafts = new
|
|
90
|
+
const roomDrafts = new FencedMap(isFenced);
|
|
43
91
|
const presentations = new Map();
|
|
44
92
|
let limits = [];
|
|
45
93
|
let capabilities = [];
|
|
46
|
-
const rooms = new
|
|
94
|
+
const rooms = new FencedMap(isFenced);
|
|
95
|
+
const counted = new Map();
|
|
47
96
|
const unreadable = new Set();
|
|
48
|
-
const
|
|
49
|
-
const
|
|
97
|
+
const isModelFenced = (key) => isFenced(key.slice(key.indexOf(' ') + 1));
|
|
98
|
+
const models = new FencedMap(isModelFenced);
|
|
99
|
+
const liveModels = new FencedMap(isModelFenced);
|
|
50
100
|
const invalidModels = new Set();
|
|
51
101
|
const discovered = new Map();
|
|
52
102
|
let roomsListed = false;
|
|
@@ -76,18 +126,28 @@ export function composeRuntime(options) {
|
|
|
76
126
|
let status = STARTING;
|
|
77
127
|
let shown = STARTING;
|
|
78
128
|
let discoveryPaused = false;
|
|
79
|
-
let resetTimer;
|
|
80
129
|
let closed = false;
|
|
81
130
|
let stopping = false;
|
|
82
131
|
let withdrawStream;
|
|
83
132
|
const announced = new Map();
|
|
84
133
|
let underway = null;
|
|
85
|
-
|
|
134
|
+
let streamed = null;
|
|
135
|
+
let handed = false;
|
|
136
|
+
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
137
|
+
const repairing = new Set();
|
|
138
|
+
const repairable = new Map();
|
|
139
|
+
let outcomeRetention = null;
|
|
86
140
|
const answering = new Map();
|
|
87
141
|
let complete = false;
|
|
88
142
|
let recorded = false;
|
|
89
143
|
const stale = new Set();
|
|
90
|
-
let
|
|
144
|
+
let generation = 0;
|
|
145
|
+
let discovering = null;
|
|
146
|
+
let opened = null;
|
|
147
|
+
const owed = new Set();
|
|
148
|
+
const lanes = new Map();
|
|
149
|
+
let roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
150
|
+
const working = new Set();
|
|
91
151
|
let mutations = Promise.resolve();
|
|
92
152
|
const roomListeners = new Set();
|
|
93
153
|
const refusalListeners = new Set();
|
|
@@ -98,27 +158,49 @@ export function composeRuntime(options) {
|
|
|
98
158
|
const presentationListeners = new Set();
|
|
99
159
|
const readSide = createReadSide();
|
|
100
160
|
let withdrawConversations;
|
|
101
|
-
let currencyQueued =
|
|
161
|
+
let currencyQueued = null;
|
|
102
162
|
let socketOpen = false;
|
|
103
163
|
let serviceMode = 'open';
|
|
104
164
|
let haltedRefusal = false;
|
|
105
165
|
let openSignals = 0;
|
|
106
166
|
const serviceModeListeners = new Set();
|
|
167
|
+
let member = null;
|
|
168
|
+
const memberListeners = new Set();
|
|
169
|
+
const prepared = new Set();
|
|
170
|
+
const projecting = new Set();
|
|
171
|
+
const unpreparedDemand = new Set();
|
|
172
|
+
const unindexable = new Set();
|
|
173
|
+
let preparing = false;
|
|
174
|
+
let demand = WHOLE_DEMAND;
|
|
175
|
+
let narrowing = null;
|
|
176
|
+
let targets = {};
|
|
177
|
+
let persistence = options.shell.persistence;
|
|
178
|
+
let capacity = null;
|
|
179
|
+
const paused = new Set();
|
|
180
|
+
let rechecking = null;
|
|
181
|
+
const readinessWatches = new Set();
|
|
182
|
+
let readinessQueued = false;
|
|
183
|
+
const catching = new Set();
|
|
184
|
+
const transfers = new FencedMap(isFenced);
|
|
107
185
|
const conversations = createConversationStore({
|
|
108
|
-
|
|
109
|
-
documents: (locator) =>
|
|
186
|
+
agents: declaredAgents,
|
|
187
|
+
documents: ({ locator }) => rowsQuery(planOf({ rooms: [locator], select: { fields: 'all' } }, roomFacts())),
|
|
110
188
|
call: (name, input) => invokeAction(name, input),
|
|
111
189
|
durably,
|
|
112
190
|
stream: () => options.adapter.conversations,
|
|
113
191
|
publish: publishChangeSet,
|
|
192
|
+
online: () => !status.providerUnreachable && refusal === null,
|
|
114
193
|
closed: () => closed,
|
|
194
|
+
unanswered: resendLater,
|
|
195
|
+
expired: (operation) => pastHorizon(operation),
|
|
115
196
|
});
|
|
116
197
|
const searchIndex = createSearchIndex({
|
|
117
|
-
|
|
198
|
+
room: (room) => readSide.room(room),
|
|
118
199
|
document: (room, path) => readSide.document(room, path),
|
|
119
|
-
messages: () => conversations.messages(),
|
|
120
|
-
message: (conversation, identity) => conversations.message(conversation, identity),
|
|
121
200
|
});
|
|
201
|
+
const roomsToIndex = new Set();
|
|
202
|
+
const conversationsToIndex = new Set();
|
|
203
|
+
let indexing = false;
|
|
122
204
|
const journal = createLog();
|
|
123
205
|
let notifyReady = () => { };
|
|
124
206
|
const ready = new Promise((resolve) => {
|
|
@@ -182,7 +264,9 @@ export function composeRuntime(options) {
|
|
|
182
264
|
}
|
|
183
265
|
}
|
|
184
266
|
function pending() {
|
|
185
|
-
return
|
|
267
|
+
return (!localSaves &&
|
|
268
|
+
(queuesWithoutReplica.size > 0 ||
|
|
269
|
+
[...replicas.values()].some((replica) => replica.queue.length > 0)));
|
|
186
270
|
}
|
|
187
271
|
function attention() {
|
|
188
272
|
const attentionRequired = [];
|
|
@@ -195,9 +279,16 @@ export function composeRuntime(options) {
|
|
|
195
279
|
returned: structuredClone(record.returned),
|
|
196
280
|
});
|
|
197
281
|
}
|
|
282
|
+
for (const [room, refusal] of sourceRefusals)
|
|
283
|
+
attentionRequired.push({ room, refusal: structuredClone(refusal) });
|
|
198
284
|
attentionRequired.sort((left, right) => {
|
|
199
285
|
if (left.room !== right.room)
|
|
200
286
|
return left.room < right.room ? -1 : 1;
|
|
287
|
+
if ('refusal' in left || 'refusal' in right) {
|
|
288
|
+
if ('refusal' in left && 'refusal' in right)
|
|
289
|
+
return 0;
|
|
290
|
+
return 'refusal' in left ? -1 : 1;
|
|
291
|
+
}
|
|
201
292
|
if (left.path !== right.path)
|
|
202
293
|
return left.path < right.path ? -1 : 1;
|
|
203
294
|
return 0;
|
|
@@ -211,8 +302,21 @@ export function composeRuntime(options) {
|
|
|
211
302
|
}
|
|
212
303
|
return { attentionRequired, reset };
|
|
213
304
|
}
|
|
305
|
+
function documentsIn(locator) {
|
|
306
|
+
const replica = replicas.get(locator);
|
|
307
|
+
if (replica === undefined)
|
|
308
|
+
return 0;
|
|
309
|
+
const roots = replica.confirmed.record.typedSelection?.roots ?? [];
|
|
310
|
+
const weighed = outsideOf(roots);
|
|
311
|
+
return (tally(replica.visible.documents, weighed.documents) +
|
|
312
|
+
tally(replica.visible.binaries, weighed.binaries));
|
|
313
|
+
}
|
|
214
314
|
function listed() {
|
|
215
|
-
return structuredClone([...rooms.values()])
|
|
315
|
+
return structuredClone([...rooms.values()].map((room) => {
|
|
316
|
+
const documents = documentsIn(room.locator);
|
|
317
|
+
counted.set(room.locator, documents);
|
|
318
|
+
return { ...room, documents };
|
|
319
|
+
}));
|
|
216
320
|
}
|
|
217
321
|
function roomFacts(reach = null) {
|
|
218
322
|
const held = [...rooms.values()]
|
|
@@ -232,18 +336,26 @@ export function composeRuntime(options) {
|
|
|
232
336
|
return { listed: roomsListed, rooms: held };
|
|
233
337
|
}
|
|
234
338
|
function emitRooms() {
|
|
339
|
+
readSide.recover();
|
|
340
|
+
queueReadiness();
|
|
235
341
|
const value = listed();
|
|
236
342
|
for (const listener of roomListeners)
|
|
237
343
|
listener(value);
|
|
238
344
|
}
|
|
239
345
|
function emitRefusal() {
|
|
346
|
+
queueReadiness();
|
|
240
347
|
for (const listener of refusalListeners)
|
|
241
348
|
listener(refusal);
|
|
242
349
|
}
|
|
243
350
|
function emitStorageLoss() {
|
|
351
|
+
queueReadiness();
|
|
244
352
|
for (const listener of storageLossListeners)
|
|
245
353
|
listener(storageLoss);
|
|
246
354
|
}
|
|
355
|
+
function emitMember() {
|
|
356
|
+
for (const listener of memberListeners)
|
|
357
|
+
listener(member);
|
|
358
|
+
}
|
|
247
359
|
function moveTo(next) {
|
|
248
360
|
if (closed)
|
|
249
361
|
return;
|
|
@@ -253,7 +365,8 @@ export function composeRuntime(options) {
|
|
|
253
365
|
function show() {
|
|
254
366
|
if (closed)
|
|
255
367
|
return;
|
|
256
|
-
|
|
368
|
+
queueReadiness();
|
|
369
|
+
const next = status.state === 'synced' && demand.conversations && !conversations.current()
|
|
257
370
|
? { ...status, state: 'syncing' }
|
|
258
371
|
: status;
|
|
259
372
|
if (same(shown, next))
|
|
@@ -263,6 +376,8 @@ export function composeRuntime(options) {
|
|
|
263
376
|
listener(shown);
|
|
264
377
|
}
|
|
265
378
|
function entryOf(batch) {
|
|
379
|
+
if (localSaves)
|
|
380
|
+
return Object.freeze({ batch, state: 'local' });
|
|
266
381
|
const state = pendings.get(batch.id);
|
|
267
382
|
if (state?.accepted !== undefined) {
|
|
268
383
|
return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
|
|
@@ -270,10 +385,7 @@ export function composeRuntime(options) {
|
|
|
270
385
|
return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
|
|
271
386
|
}
|
|
272
387
|
function entriesOf(locator) {
|
|
273
|
-
|
|
274
|
-
if (replica === undefined)
|
|
275
|
-
return [];
|
|
276
|
-
return replica.queue.map(entryOf);
|
|
388
|
+
return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map(entryOf);
|
|
277
389
|
}
|
|
278
390
|
function emitQueue(locator) {
|
|
279
391
|
const entries = entriesOf(locator);
|
|
@@ -283,12 +395,486 @@ export function composeRuntime(options) {
|
|
|
283
395
|
}
|
|
284
396
|
}
|
|
285
397
|
function applyChangeSet(set) {
|
|
286
|
-
|
|
287
|
-
|
|
398
|
+
const held = {
|
|
399
|
+
...set,
|
|
400
|
+
documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
|
|
401
|
+
};
|
|
402
|
+
readSide.apply(held);
|
|
403
|
+
searchIndex.changed(held);
|
|
404
|
+
}
|
|
405
|
+
async function referenced(locator) {
|
|
406
|
+
for (;;) {
|
|
407
|
+
const record = replicas.get(locator)?.confirmed.record;
|
|
408
|
+
if (record === undefined || closed)
|
|
409
|
+
return;
|
|
410
|
+
const held = { text: record.documents, binaries: record.binaries };
|
|
411
|
+
const composition = compositionOf(record);
|
|
412
|
+
if (referencesIndexed(held, composition))
|
|
413
|
+
return;
|
|
414
|
+
await indexReferrers(held, composition);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
async function prepareReplica(locator) {
|
|
418
|
+
await referenced(locator);
|
|
419
|
+
const replica = replicas.get(locator);
|
|
420
|
+
if (replica === undefined || closed || prepared.has(locator))
|
|
421
|
+
return;
|
|
422
|
+
const visible = replica.visible;
|
|
423
|
+
const paths = [
|
|
424
|
+
...new Set([...Object.keys(visible.documents), ...Object.keys(visible.binaries)]),
|
|
425
|
+
];
|
|
426
|
+
const entered = [];
|
|
427
|
+
projecting.add(locator);
|
|
428
|
+
try {
|
|
429
|
+
await inChunks(paths, (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
|
|
430
|
+
const current = replicas.get(locator);
|
|
431
|
+
if (current === undefined || closed)
|
|
432
|
+
return;
|
|
433
|
+
const set = entering(locator, current, chunk);
|
|
434
|
+
readSide.apply(set);
|
|
435
|
+
entered.push(set);
|
|
436
|
+
});
|
|
437
|
+
if (!replicas.has(locator) || closed)
|
|
438
|
+
return;
|
|
439
|
+
prepared.add(locator);
|
|
440
|
+
}
|
|
441
|
+
finally {
|
|
442
|
+
projecting.delete(locator);
|
|
443
|
+
}
|
|
444
|
+
conversations.changed(mergeChangeSets(entered));
|
|
445
|
+
readSide.recover();
|
|
446
|
+
queueReadiness();
|
|
447
|
+
}
|
|
448
|
+
function unprepared() {
|
|
449
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !prepared.has(locator) && !unindexable.has(locator));
|
|
450
|
+
}
|
|
451
|
+
function unbuilt(locator, error) {
|
|
452
|
+
unindexable.add(locator);
|
|
453
|
+
journal.record(`index failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
454
|
+
queueReadiness();
|
|
455
|
+
}
|
|
456
|
+
function prepare(locators = []) {
|
|
457
|
+
for (const locator of locators)
|
|
458
|
+
unpreparedDemand.add(locator);
|
|
459
|
+
if (preparing)
|
|
460
|
+
return;
|
|
461
|
+
preparing = true;
|
|
462
|
+
void (async () => {
|
|
463
|
+
try {
|
|
464
|
+
for (let waiting = unprepared(); waiting !== undefined; waiting = unprepared()) {
|
|
465
|
+
await yielded();
|
|
466
|
+
if (closed)
|
|
467
|
+
return;
|
|
468
|
+
unpreparedDemand.delete(waiting);
|
|
469
|
+
await prepareReplica(waiting).catch((error) => unbuilt(waiting, error));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
finally {
|
|
473
|
+
preparing = false;
|
|
474
|
+
}
|
|
475
|
+
})();
|
|
476
|
+
}
|
|
477
|
+
function reachedRooms(plan) {
|
|
478
|
+
const reach = plan.reach;
|
|
479
|
+
const reached = reach === null ? null : new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
|
|
480
|
+
const named = plan.rooms ??
|
|
481
|
+
(reached === null
|
|
482
|
+
? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
|
|
483
|
+
: [...reached]);
|
|
484
|
+
return [...new Set(named)].filter((locator) => reached === null || reached.has(locator)).sort();
|
|
485
|
+
}
|
|
486
|
+
function awaited(locator) {
|
|
487
|
+
return rooms.has(locator) || !roomsListed;
|
|
488
|
+
}
|
|
489
|
+
function covered(plan) {
|
|
490
|
+
const locators = reachedRooms(plan);
|
|
491
|
+
if (locators.length === 0 && plan.rooms === null && plan.reach === null && !roomsListed) {
|
|
492
|
+
return { pending: [] };
|
|
493
|
+
}
|
|
494
|
+
const pending = [];
|
|
495
|
+
const coverage = [];
|
|
496
|
+
const overlay = [];
|
|
497
|
+
for (const locator of locators) {
|
|
498
|
+
const replica = replicas.get(locator);
|
|
499
|
+
if (replica === undefined) {
|
|
500
|
+
if (awaited(locator))
|
|
501
|
+
pending.push({ room: locator, awaits: 'acquisition' });
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
if (!prepared.has(locator)) {
|
|
505
|
+
pending.push({ room: locator, awaits: 'preparation' });
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
coverage.push({
|
|
509
|
+
room: locator,
|
|
510
|
+
generation: replica.confirmed.record.transfer,
|
|
511
|
+
current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
|
|
512
|
+
});
|
|
513
|
+
for (const batch of replica.queue)
|
|
514
|
+
overlay.push(batch.id);
|
|
515
|
+
}
|
|
516
|
+
if (pending.length > 0)
|
|
517
|
+
return { pending };
|
|
518
|
+
return { coverage: { rooms: coverage, indexes: [], overlay } };
|
|
519
|
+
}
|
|
520
|
+
function awaitPreparation(cover) {
|
|
521
|
+
if (!('pending' in cover))
|
|
522
|
+
return;
|
|
523
|
+
const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
|
|
524
|
+
if (waiting.length > 0)
|
|
525
|
+
prepare(waiting.map(({ room }) => room));
|
|
526
|
+
}
|
|
527
|
+
function acquires(room) {
|
|
528
|
+
return (demanded(demand, room) ||
|
|
529
|
+
(replicas.get(room.locator)?.queue.length ?? 0) > 0 ||
|
|
530
|
+
queuesWithoutReplica.has(room.locator));
|
|
531
|
+
}
|
|
532
|
+
function acquiredLocators() {
|
|
533
|
+
return [...discovered.values()]
|
|
534
|
+
.filter(acquires)
|
|
535
|
+
.map((room) => room.locator)
|
|
536
|
+
.sort();
|
|
537
|
+
}
|
|
538
|
+
function heldDemand(acquired) {
|
|
539
|
+
if (demand.whole)
|
|
540
|
+
return null;
|
|
541
|
+
return { rooms: acquired.map((room) => room.locator), conversations: demand.conversations };
|
|
542
|
+
}
|
|
543
|
+
function applyDemand(next) {
|
|
544
|
+
const before = acquiredLocators();
|
|
545
|
+
const conversed = demand.conversations;
|
|
546
|
+
demand = next;
|
|
547
|
+
const after = acquiredLocators();
|
|
548
|
+
queueReadiness();
|
|
549
|
+
if (!conversed && next.conversations)
|
|
550
|
+
scheduleCurrency();
|
|
551
|
+
if (JSON.stringify(before) === JSON.stringify(after) && conversed === next.conversations)
|
|
552
|
+
return;
|
|
553
|
+
journal.record(`acquisition ${next.whole ? 'whole' : after.length}`);
|
|
554
|
+
if (options.adapter.roomStream === undefined)
|
|
555
|
+
void sync('unstreamed');
|
|
556
|
+
else if (socketOpen)
|
|
557
|
+
options.adapter.roomStream.reopen();
|
|
558
|
+
}
|
|
559
|
+
function acquire(next) {
|
|
560
|
+
if (closed)
|
|
561
|
+
return;
|
|
562
|
+
if (narrowing !== null)
|
|
563
|
+
clearTimeout(narrowing);
|
|
564
|
+
narrowing = null;
|
|
565
|
+
const widening = wider(next, demand);
|
|
566
|
+
if (!widening && !narrower(next, demand))
|
|
567
|
+
return;
|
|
568
|
+
if (widening || !roomsListed) {
|
|
569
|
+
applyDemand(next);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
// A narrower demand waits five seconds, so a tab that reloads returns before the stream reopens twice.
|
|
573
|
+
narrowing = setTimeout(() => {
|
|
574
|
+
narrowing = null;
|
|
575
|
+
applyDemand(next);
|
|
576
|
+
}, 5_000);
|
|
577
|
+
}
|
|
578
|
+
function requiredRooms(reach) {
|
|
579
|
+
if (reach === null)
|
|
580
|
+
return { listed: roomsListed, rooms: [...rooms.keys()].sort() };
|
|
581
|
+
const named = [...new Set(reach.rooms)].sort();
|
|
582
|
+
if (roomsListed)
|
|
583
|
+
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
584
|
+
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
585
|
+
}
|
|
586
|
+
function roomState(locator, search) {
|
|
587
|
+
if (sourceRefusals.has(locator))
|
|
588
|
+
return 'refused';
|
|
589
|
+
const listedRoom = discovered.get(locator);
|
|
590
|
+
if (roomsListed &&
|
|
591
|
+
(listedRoom === undefined ||
|
|
592
|
+
listedRoom.replica.admission !== 'admitted' ||
|
|
593
|
+
unreadable.has(locator)))
|
|
594
|
+
return 'unadmitted';
|
|
595
|
+
if (!replicas.has(locator))
|
|
596
|
+
return 'acquiring';
|
|
597
|
+
if (!prepared.has(locator))
|
|
598
|
+
return 'preparing';
|
|
599
|
+
if (search && !searchIndex.rooms().has(locator))
|
|
600
|
+
return 'indexing';
|
|
601
|
+
return 'complete';
|
|
602
|
+
}
|
|
603
|
+
function conversationCoverage(reach, search) {
|
|
604
|
+
const declared = reach === null || reach.conversations;
|
|
605
|
+
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
606
|
+
return 'undeclared';
|
|
607
|
+
if (!conversations.known() || !conversations.current())
|
|
608
|
+
return 'acquiring';
|
|
609
|
+
if (!search)
|
|
610
|
+
return 'complete';
|
|
611
|
+
const unindexed = conversations
|
|
612
|
+
.searchable()
|
|
613
|
+
.filter((one) => conversations.acquired(one) && !searchIndex.conversations().has(one));
|
|
614
|
+
if (unindexed.length === 0)
|
|
615
|
+
return 'complete';
|
|
616
|
+
index([], unindexed);
|
|
617
|
+
return 'indexing';
|
|
618
|
+
}
|
|
619
|
+
function connectionOf() {
|
|
620
|
+
if (socketOpen)
|
|
621
|
+
return 'connected';
|
|
622
|
+
if (status.providerUnreachable)
|
|
623
|
+
return 'unreachable';
|
|
624
|
+
return options.adapter.roomStream === undefined && status.lastCompleted !== null
|
|
625
|
+
? 'connected'
|
|
626
|
+
: 'connecting';
|
|
627
|
+
}
|
|
628
|
+
function refusedIn(locators) {
|
|
629
|
+
return locators.flatMap((room) => {
|
|
630
|
+
const held = sourceRefusals.get(room);
|
|
631
|
+
if (held === undefined)
|
|
632
|
+
return [];
|
|
633
|
+
const { file, location } = held.fields;
|
|
634
|
+
return [
|
|
635
|
+
{
|
|
636
|
+
room,
|
|
637
|
+
file: typeof file === 'string' ? file : '',
|
|
638
|
+
rule: held.code,
|
|
639
|
+
location: typeof location === 'string' ? location : null,
|
|
640
|
+
},
|
|
641
|
+
];
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
function readinessFor(port, reach) {
|
|
645
|
+
const required = requiredRooms(reach);
|
|
646
|
+
const target = targets[port.target];
|
|
647
|
+
const search = target?.search === true;
|
|
648
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room, search) }));
|
|
649
|
+
const building = (state) => coverage
|
|
650
|
+
.filter((room) => room.state === state && !unindexable.has(room.room))
|
|
651
|
+
.map(({ room }) => room);
|
|
652
|
+
const preparing = building('preparing');
|
|
653
|
+
if (preparing.length > 0)
|
|
654
|
+
prepare(preparing);
|
|
655
|
+
const indexing = building('indexing');
|
|
656
|
+
if (indexing.length > 0)
|
|
657
|
+
index(indexing, []);
|
|
658
|
+
return readinessOf({
|
|
659
|
+
code: port.code,
|
|
660
|
+
coverage: {
|
|
661
|
+
listed: required.listed,
|
|
662
|
+
rooms: coverage,
|
|
663
|
+
conversations: conversationCoverage(reach, search),
|
|
664
|
+
search: search ? 'required' : 'unused',
|
|
665
|
+
},
|
|
666
|
+
persistence,
|
|
667
|
+
connection: connectionOf(),
|
|
668
|
+
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
669
|
+
capacity: capacity !== null &&
|
|
670
|
+
coverage.some(({ room, state }) => paused.has(room) || !SETTLED_ROOMS.has(state))
|
|
671
|
+
? capacity
|
|
672
|
+
: port.capacity,
|
|
673
|
+
refused: refusedIn(required.rooms),
|
|
674
|
+
lost: storageLoss !== null,
|
|
675
|
+
established: target?.established === true,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
function keepTarget(name, change) {
|
|
679
|
+
const held = targets[name] ?? { established: false, search: false };
|
|
680
|
+
const next = { ...held, ...change };
|
|
681
|
+
if (next.established === held.established && next.search === held.search)
|
|
682
|
+
return;
|
|
683
|
+
targets = { ...targets, [name]: next };
|
|
684
|
+
queueReadiness();
|
|
685
|
+
void durably((local) => local.keepTargets(targets)).catch(() => {
|
|
686
|
+
journal.record(`target record failed ${name}`);
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
function deliverReadiness(watch) {
|
|
690
|
+
if (!readinessWatches.has(watch))
|
|
691
|
+
return;
|
|
692
|
+
const readiness = readinessFor(watch.port, watch.reach);
|
|
693
|
+
if (readiness.ready && targets[watch.port.target]?.established !== true) {
|
|
694
|
+
keepTarget(watch.port.target, { established: true });
|
|
695
|
+
}
|
|
696
|
+
const shown = JSON.stringify(readiness);
|
|
697
|
+
if (shown === watch.last)
|
|
698
|
+
return;
|
|
699
|
+
watch.last = shown;
|
|
700
|
+
watch.deliver(structuredClone(readiness));
|
|
701
|
+
}
|
|
702
|
+
function queueReadiness() {
|
|
703
|
+
if (readinessQueued || closed || readinessWatches.size === 0)
|
|
704
|
+
return;
|
|
705
|
+
readinessQueued = true;
|
|
706
|
+
queueMicrotask(() => {
|
|
707
|
+
readinessQueued = false;
|
|
708
|
+
if (closed)
|
|
709
|
+
return;
|
|
710
|
+
for (const watch of [...readinessWatches])
|
|
711
|
+
deliverReadiness(watch);
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
function watchReadiness(port, reach, deliver) {
|
|
715
|
+
const watch = { port, reach, deliver, last: null };
|
|
716
|
+
readinessWatches.add(watch);
|
|
717
|
+
void openLocal().then(() => deliverReadiness(watch), () => deliverReadiness(watch));
|
|
718
|
+
return () => {
|
|
719
|
+
readinessWatches.delete(watch);
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
function searching(target) {
|
|
723
|
+
if (targets[target]?.search !== true)
|
|
724
|
+
keepTarget(target, { search: true });
|
|
725
|
+
}
|
|
726
|
+
function layerBytes(layer) {
|
|
727
|
+
let bytes = 0;
|
|
728
|
+
for (const [path, text] of Object.entries(layer.documents))
|
|
729
|
+
bytes += (path.length + text.length) * 2;
|
|
730
|
+
return bytes;
|
|
731
|
+
}
|
|
732
|
+
function grownBy(next, previous) {
|
|
733
|
+
if (previous === undefined)
|
|
734
|
+
return layerBytes(next);
|
|
735
|
+
const documents = Object.fromEntries(Object.entries(next.documents).filter(([path, text]) => previous.documents[path] !== text));
|
|
736
|
+
return layerBytes({ ...next, documents });
|
|
737
|
+
}
|
|
738
|
+
function exceeded(error) {
|
|
739
|
+
return (typeof error === 'object' &&
|
|
740
|
+
error !== null &&
|
|
741
|
+
error.name === 'QuotaExceededError');
|
|
742
|
+
}
|
|
743
|
+
async function estimated() {
|
|
744
|
+
const estimate = options.shell.estimate === undefined ? null : await options.shell.estimate();
|
|
745
|
+
return estimate === null ? null : Math.max(0, estimate.quota - estimate.usage);
|
|
746
|
+
}
|
|
747
|
+
function pause(locator, cause) {
|
|
748
|
+
paused.add(locator);
|
|
749
|
+
capacity = cause;
|
|
750
|
+
journal.record(`capacity ${cause.cause} ${locator} ${cause.needed} ${cause.available ?? '-'}`);
|
|
751
|
+
queueReadiness();
|
|
752
|
+
if (rechecking !== null)
|
|
753
|
+
return;
|
|
754
|
+
// A paused preparation measures the browser's space again each minute, so space freed elsewhere resumes it without a retry.
|
|
755
|
+
rechecking = setInterval(() => {
|
|
756
|
+
void recheck();
|
|
757
|
+
}, 60_000);
|
|
758
|
+
}
|
|
759
|
+
async function recheck() {
|
|
760
|
+
const held = capacity;
|
|
761
|
+
if (held === null || closed)
|
|
762
|
+
return;
|
|
763
|
+
const available = await estimated().catch(() => null);
|
|
764
|
+
if (available === null || available < (held.needed ?? 0))
|
|
765
|
+
return;
|
|
766
|
+
if (held.cause === 'quota-exceeded' && (held.available === null || available <= held.available))
|
|
767
|
+
return;
|
|
768
|
+
resume();
|
|
769
|
+
}
|
|
770
|
+
function resume() {
|
|
771
|
+
if (rechecking !== null)
|
|
772
|
+
clearInterval(rechecking);
|
|
773
|
+
rechecking = null;
|
|
774
|
+
if (paused.size === 0 && capacity === null)
|
|
775
|
+
return;
|
|
776
|
+
paused.clear();
|
|
777
|
+
capacity = null;
|
|
778
|
+
journal.record('capacity resumed');
|
|
779
|
+
queueReadiness();
|
|
780
|
+
if (!closed && authorization !== undefined)
|
|
781
|
+
void sync('unstreamed');
|
|
782
|
+
}
|
|
783
|
+
async function reclaimed(local) {
|
|
784
|
+
const unreferenced = [...transfers.keys()].filter((locator) => {
|
|
785
|
+
const room = discovered.get(locator);
|
|
786
|
+
return room === undefined || !acquires(room);
|
|
787
|
+
});
|
|
788
|
+
for (const locator of unreferenced) {
|
|
789
|
+
await local.abandon(locator);
|
|
790
|
+
transfers.delete(locator);
|
|
791
|
+
}
|
|
792
|
+
if (unreferenced.length > 0)
|
|
793
|
+
journal.record(`capacity reclaimed ${unreferenced.length}`);
|
|
794
|
+
return unreferenced.length > 0;
|
|
795
|
+
}
|
|
796
|
+
async function admitted(local, locator, needed) {
|
|
797
|
+
if (paused.has(locator))
|
|
798
|
+
return false;
|
|
799
|
+
let available = await estimated().catch(() => null);
|
|
800
|
+
if (available === null || available >= needed)
|
|
801
|
+
return true;
|
|
802
|
+
if (await reclaimed(local))
|
|
803
|
+
available = await estimated().catch(() => null);
|
|
804
|
+
if (available === null || available >= needed)
|
|
805
|
+
return true;
|
|
806
|
+
pause(locator, { cause: 'insufficient-space', needed, available });
|
|
807
|
+
return false;
|
|
808
|
+
}
|
|
809
|
+
async function exceededOn(locator, bytes) {
|
|
810
|
+
const available = await estimated().catch(() => null);
|
|
811
|
+
pause(locator, { cause: 'quota-exceeded', needed: bytes, available });
|
|
812
|
+
}
|
|
813
|
+
async function rowsQuery(plan) {
|
|
814
|
+
const before = covered(plan);
|
|
815
|
+
awaitPreparation(before);
|
|
816
|
+
if ('pending' in before)
|
|
817
|
+
return before;
|
|
818
|
+
const { rows, at: cover } = await readSide.query(plan, () => covered(plan));
|
|
819
|
+
awaitPreparation(cover);
|
|
820
|
+
if ('pending' in cover)
|
|
821
|
+
return cover;
|
|
822
|
+
return 'groups' in rows
|
|
823
|
+
? { groups: rows.groups, coverage: cover.coverage }
|
|
824
|
+
: { documents: rows.documents, coverage: cover.coverage };
|
|
825
|
+
}
|
|
826
|
+
async function indexConversation(conversation) {
|
|
827
|
+
searchIndex.indexMessages(conversation, [], false);
|
|
828
|
+
let after = null;
|
|
829
|
+
for (;;) {
|
|
830
|
+
const batch = await durably((local) => local.messagesAfter(conversation, after));
|
|
831
|
+
if (closed)
|
|
832
|
+
return;
|
|
833
|
+
searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
|
|
834
|
+
if (batch.length === 0)
|
|
835
|
+
return;
|
|
836
|
+
after = batch[batch.length - 1].identity;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
function indexNext() {
|
|
840
|
+
const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator) && !unindexable.has(locator));
|
|
841
|
+
if (room !== undefined) {
|
|
842
|
+
roomsToIndex.delete(room);
|
|
843
|
+
return () => prepared.has(room)
|
|
844
|
+
? searchIndex.indexRoom(room).catch((error) => unbuilt(room, error))
|
|
845
|
+
: undefined;
|
|
846
|
+
}
|
|
847
|
+
const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
|
|
848
|
+
if (conversation === undefined)
|
|
849
|
+
return null;
|
|
850
|
+
conversationsToIndex.delete(conversation);
|
|
851
|
+
return () => indexConversation(conversation);
|
|
852
|
+
}
|
|
853
|
+
function index(rooms, named) {
|
|
854
|
+
for (const room of rooms)
|
|
855
|
+
roomsToIndex.add(room);
|
|
856
|
+
for (const conversation of named)
|
|
857
|
+
conversationsToIndex.add(conversation);
|
|
858
|
+
if (indexing)
|
|
859
|
+
return;
|
|
860
|
+
indexing = true;
|
|
861
|
+
void (async () => {
|
|
862
|
+
try {
|
|
863
|
+
for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
|
|
864
|
+
await yielded();
|
|
865
|
+
await unit();
|
|
866
|
+
queueReadiness();
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
finally {
|
|
870
|
+
indexing = false;
|
|
871
|
+
}
|
|
872
|
+
})().catch(() => { });
|
|
288
873
|
}
|
|
289
874
|
function publishChangeSet(set) {
|
|
290
875
|
applyChangeSet(set);
|
|
291
|
-
if (set.rooms.length > 0
|
|
876
|
+
if (set.rooms.length > 0 ||
|
|
877
|
+
[...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
|
|
292
878
|
emitRooms();
|
|
293
879
|
for (const locator of set.queues)
|
|
294
880
|
emitQueue(locator);
|
|
@@ -299,39 +885,8 @@ export function composeRuntime(options) {
|
|
|
299
885
|
for (const app of set.presentations)
|
|
300
886
|
emitPresentation(app);
|
|
301
887
|
conversations.changed(set);
|
|
302
|
-
const cards = declaredCards();
|
|
303
|
-
if (cards !== undefined && set.documents.some((change) => change.locator === cards.locator)) {
|
|
304
|
-
scheduleCurrency();
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
function stopTimer() {
|
|
308
|
-
if (resetTimer === undefined)
|
|
309
|
-
return;
|
|
310
|
-
clearTimeout(resetTimer);
|
|
311
|
-
resetTimer = undefined;
|
|
312
|
-
}
|
|
313
|
-
function armTimer() {
|
|
314
|
-
stopTimer();
|
|
315
|
-
if (closed || stopping)
|
|
316
|
-
return;
|
|
317
|
-
let earliest = null;
|
|
318
|
-
for (const { submission } of pendings.values()) {
|
|
319
|
-
if (submission.state !== 'reset')
|
|
320
|
-
continue;
|
|
321
|
-
const at = Date.parse(submission.reset);
|
|
322
|
-
if (earliest === null || at < earliest)
|
|
323
|
-
earliest = at;
|
|
324
|
-
}
|
|
325
|
-
if (earliest === null)
|
|
326
|
-
return;
|
|
327
|
-
// A Save House held back goes again only once the reset its answer named has passed.
|
|
328
|
-
resetTimer = setTimeout(() => {
|
|
329
|
-
resetTimer = undefined;
|
|
330
|
-
submitAll();
|
|
331
|
-
}, Math.max(0, earliest - Date.now()));
|
|
332
888
|
}
|
|
333
889
|
async function refuse(reason, provider = null) {
|
|
334
|
-
stopTimer();
|
|
335
890
|
discoveryPaused = true;
|
|
336
891
|
try {
|
|
337
892
|
if (reason === 'revoked-authorization') {
|
|
@@ -339,10 +894,7 @@ export function composeRuntime(options) {
|
|
|
339
894
|
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
340
895
|
try {
|
|
341
896
|
const local = await openLocal();
|
|
342
|
-
await local.revokeModels([...replicas.values()].map((replica) =>
|
|
343
|
-
record: replica.confirmed.record,
|
|
344
|
-
queue: replica.queue,
|
|
345
|
-
})));
|
|
897
|
+
await local.revokeModels([...replicas.values()].map((replica) => replica.confirmed.record));
|
|
346
898
|
}
|
|
347
899
|
catch {
|
|
348
900
|
journal.record('model revocation persistence failed');
|
|
@@ -366,10 +918,7 @@ export function composeRuntime(options) {
|
|
|
366
918
|
model: revoked,
|
|
367
919
|
selectionModel: revoked,
|
|
368
920
|
};
|
|
369
|
-
|
|
370
|
-
replicas.set(locator, read.kind === 'staged'
|
|
371
|
-
? replicaOf(read.confirmed, before.queue, before)
|
|
372
|
-
: { ...before, confirmed: { ...before.confirmed, record } });
|
|
921
|
+
replicas.set(locator, replicaOf(confirmedOf(record, before.confirmed), before.queue, before));
|
|
373
922
|
}
|
|
374
923
|
rooms.set(locator, {
|
|
375
924
|
...room,
|
|
@@ -386,7 +935,8 @@ export function composeRuntime(options) {
|
|
|
386
935
|
}
|
|
387
936
|
finally {
|
|
388
937
|
refusal = {
|
|
389
|
-
reason,
|
|
938
|
+
code: reason,
|
|
939
|
+
retryable: false,
|
|
390
940
|
reauthorizationOffered: reason === 'revoked-authorization',
|
|
391
941
|
refusal: provider,
|
|
392
942
|
};
|
|
@@ -399,6 +949,7 @@ export function composeRuntime(options) {
|
|
|
399
949
|
rooms.set(room.locator, {
|
|
400
950
|
name: room.name,
|
|
401
951
|
locator: room.locator,
|
|
952
|
+
handle: room.handle,
|
|
402
953
|
operations: room.operations,
|
|
403
954
|
replica: room.replica,
|
|
404
955
|
model: modelTypes(modelOf(room.locator)),
|
|
@@ -410,19 +961,28 @@ export function composeRuntime(options) {
|
|
|
410
961
|
},
|
|
411
962
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
412
963
|
roomVersion: replicas.get(room.locator)?.confirmed.record.roomVersion ?? null,
|
|
413
|
-
attachments: replicas.get(room.locator)?.confirmed.record.attachments ?? [],
|
|
414
964
|
synchronizationAvailable,
|
|
415
965
|
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
416
966
|
});
|
|
417
967
|
}
|
|
418
|
-
async function applyDiscovery(
|
|
968
|
+
async function applyDiscovery(found, started) {
|
|
969
|
+
for (const room of found) {
|
|
970
|
+
const fence = fences.get(room.locator);
|
|
971
|
+
if (fence !== undefined && fence.erased && fence.at < started)
|
|
972
|
+
fences.delete(room.locator);
|
|
973
|
+
}
|
|
974
|
+
const listed = found.filter((room) => !fences.has(room.locator));
|
|
419
975
|
const listedLocators = new Set(listed.map((room) => room.locator));
|
|
420
976
|
for (const locator of listedLocators)
|
|
421
977
|
scopedRefusals.delete(locator);
|
|
422
978
|
discovered.clear();
|
|
423
979
|
for (const room of listed)
|
|
424
980
|
discovered.set(room.locator, room);
|
|
425
|
-
|
|
981
|
+
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
|
|
982
|
+
if (options.adapter.exhaustiveDiscovery === true)
|
|
983
|
+
await loseRooms(unlisted);
|
|
984
|
+
else
|
|
985
|
+
unlistedKept(unlisted);
|
|
426
986
|
for (const room of listed) {
|
|
427
987
|
const held = replicas.get(room.locator)?.confirmed.record;
|
|
428
988
|
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
@@ -433,43 +993,85 @@ export function composeRuntime(options) {
|
|
|
433
993
|
}
|
|
434
994
|
roomsListed = true;
|
|
435
995
|
emitRooms();
|
|
996
|
+
return listed;
|
|
436
997
|
}
|
|
437
998
|
function unlistedHeld(listed) {
|
|
438
|
-
return [
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
pendings.delete(batch.id);
|
|
454
|
-
replicas.delete(locator);
|
|
999
|
+
return [
|
|
1000
|
+
...new Set([
|
|
1001
|
+
...rooms.keys(),
|
|
1002
|
+
...roomDrafts.keys(),
|
|
1003
|
+
...transfers.keys(),
|
|
1004
|
+
...queuesWithoutReplica.keys(),
|
|
1005
|
+
]),
|
|
1006
|
+
].filter((locator) => !listed.has(locator));
|
|
1007
|
+
}
|
|
1008
|
+
function unlistedKept(unlisted) {
|
|
1009
|
+
for (const locator of unlisted) {
|
|
1010
|
+
const held = replicas.get(locator)?.confirmed.record;
|
|
1011
|
+
if (held !== undefined)
|
|
1012
|
+
list(held, false);
|
|
1013
|
+
else
|
|
455
1014
|
rooms.delete(locator);
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
function fence(locators) {
|
|
1018
|
+
const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
|
|
1019
|
+
const removed = fresh.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
|
|
1020
|
+
room: true,
|
|
1021
|
+
queue: true,
|
|
1022
|
+
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
1023
|
+
}));
|
|
1024
|
+
for (const locator of fresh) {
|
|
1025
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
1026
|
+
lastFenced.set(locator, ++fencings);
|
|
1027
|
+
for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
|
|
1028
|
+
pendings.delete(batch.id);
|
|
1029
|
+
replicas.delete(locator);
|
|
1030
|
+
queuesWithoutReplica.delete(locator);
|
|
1031
|
+
rooms.delete(locator);
|
|
1032
|
+
roomDrafts.delete(locator);
|
|
1033
|
+
transfers.delete(locator);
|
|
1034
|
+
catching.delete(locator);
|
|
1035
|
+
discovered.delete(locator);
|
|
1036
|
+
announced.delete(locator);
|
|
1037
|
+
unreadable.delete(locator);
|
|
1038
|
+
stale.delete(locator);
|
|
1039
|
+
repairable.delete(locator);
|
|
1040
|
+
unindexable.delete(locator);
|
|
1041
|
+
paused.delete(locator);
|
|
1042
|
+
scopedRefusals.delete(locator);
|
|
1043
|
+
sourceRefusals.delete(locator);
|
|
1044
|
+
declarationRefusals.delete(locator);
|
|
1045
|
+
for (const attached of ['collection', 'typed-selection']) {
|
|
1046
|
+
const key = modelKey(locator, attached);
|
|
1047
|
+
models.delete(key);
|
|
1048
|
+
liveModels.delete(key);
|
|
1049
|
+
invalidModels.delete(key);
|
|
469
1050
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
1051
|
+
journal.record(`room lost ${locator}`);
|
|
1052
|
+
}
|
|
1053
|
+
if (fresh.length === 0)
|
|
1054
|
+
return fresh;
|
|
1055
|
+
publishChangeSet(mergeChangeSets(removed));
|
|
1056
|
+
for (const locator of fresh)
|
|
1057
|
+
prepared.delete(locator);
|
|
1058
|
+
searchIndex.forget(fresh);
|
|
1059
|
+
readSide.recover();
|
|
1060
|
+
moveTo({ ...status, ...attention() });
|
|
1061
|
+
return fresh;
|
|
1062
|
+
}
|
|
1063
|
+
async function erased(local, locators) {
|
|
1064
|
+
await local.erase(locators);
|
|
1065
|
+
for (const locator of locators) {
|
|
1066
|
+
const held = fences.get(locator);
|
|
1067
|
+
if (held !== undefined)
|
|
1068
|
+
held.erased = true;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
async function loseRooms(locators) {
|
|
1072
|
+
const fresh = fence(locators);
|
|
1073
|
+
if (fresh.length > 0)
|
|
1074
|
+
await durably((local) => erased(local, fresh));
|
|
473
1075
|
}
|
|
474
1076
|
function restored(key, persisted) {
|
|
475
1077
|
const prior = models.get(key);
|
|
@@ -490,7 +1092,7 @@ export function composeRuntime(options) {
|
|
|
490
1092
|
return model;
|
|
491
1093
|
}
|
|
492
1094
|
async function hydrate(opened) {
|
|
493
|
-
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named] = await Promise.all([
|
|
1095
|
+
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged, heldTargets,] = await Promise.all([
|
|
494
1096
|
opened.all(),
|
|
495
1097
|
opened.queues(),
|
|
496
1098
|
opened.roomDrafts(),
|
|
@@ -500,9 +1102,17 @@ export function composeRuntime(options) {
|
|
|
500
1102
|
opened.lastCompleted(),
|
|
501
1103
|
opened.providerLimits(),
|
|
502
1104
|
opened.capabilities(),
|
|
1105
|
+
opened.member(),
|
|
1106
|
+
opened.transfers(),
|
|
1107
|
+
opened.targets(),
|
|
503
1108
|
]);
|
|
1109
|
+
targets = heldTargets;
|
|
1110
|
+
transfers.clear();
|
|
1111
|
+
for (const record of staged)
|
|
1112
|
+
transfers.set(record.locator, record);
|
|
504
1113
|
limits = declared;
|
|
505
1114
|
capabilities = named;
|
|
1115
|
+
member = known;
|
|
506
1116
|
roomDrafts.clear();
|
|
507
1117
|
for (const record of kept)
|
|
508
1118
|
rememberRoomDraft(record);
|
|
@@ -518,18 +1128,17 @@ export function composeRuntime(options) {
|
|
|
518
1128
|
}
|
|
519
1129
|
const previousReplicas = new Map(replicas);
|
|
520
1130
|
replicas.clear();
|
|
1131
|
+
queuesWithoutReplica.clear();
|
|
1132
|
+
const replicated = new Set(records.map((record) => record.locator));
|
|
1133
|
+
for (const [locator, queue] of queuedBatches) {
|
|
1134
|
+
if (!replicated.has(locator))
|
|
1135
|
+
queuesWithoutReplica.set(locator, queue);
|
|
1136
|
+
}
|
|
521
1137
|
for (const record of records) {
|
|
522
1138
|
const model = restored(modelKey(record.locator, 'collection'), record.model);
|
|
523
1139
|
const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
|
|
524
1140
|
const prior = previousReplicas.get(record.locator);
|
|
525
|
-
const
|
|
526
|
-
const read = readConfirmed(retyped, prior?.confirmed);
|
|
527
|
-
const confirmed = read.kind === 'staged'
|
|
528
|
-
? read.confirmed
|
|
529
|
-
: {
|
|
530
|
-
record: retyped,
|
|
531
|
-
collection: parseCollection(retyped.documents, compositionOf(retyped)),
|
|
532
|
-
};
|
|
1141
|
+
const confirmed = confirmedOf({ ...record, model, selectionModel }, prior?.confirmed);
|
|
533
1142
|
replicas.set(record.locator, replicaOf(confirmed, queuedBatches.get(record.locator) ?? [], prior));
|
|
534
1143
|
const existing = rooms.get(record.locator);
|
|
535
1144
|
if (existing === undefined)
|
|
@@ -538,7 +1147,6 @@ export function composeRuntime(options) {
|
|
|
538
1147
|
rooms.set(record.locator, {
|
|
539
1148
|
...existing,
|
|
540
1149
|
roomVersion: record.roomVersion,
|
|
541
|
-
attachments: record.attachments,
|
|
542
1150
|
operations: record.operations,
|
|
543
1151
|
replica: record.replica,
|
|
544
1152
|
model: modelTypes(model),
|
|
@@ -550,10 +1158,6 @@ export function composeRuntime(options) {
|
|
|
550
1158
|
},
|
|
551
1159
|
});
|
|
552
1160
|
}
|
|
553
|
-
if (read.kind === 'refused') {
|
|
554
|
-
refusedSource(record, read);
|
|
555
|
-
declarationRefusals.add(record.locator);
|
|
556
|
-
}
|
|
557
1161
|
}
|
|
558
1162
|
return {
|
|
559
1163
|
lastCompleted,
|
|
@@ -564,16 +1168,26 @@ export function composeRuntime(options) {
|
|
|
564
1168
|
async function readState(opened) {
|
|
565
1169
|
const { lastCompleted, locators, read } = await hydrate(opened);
|
|
566
1170
|
applyChangeSet(read);
|
|
1171
|
+
prepare();
|
|
567
1172
|
if (replicas.size > 0)
|
|
568
1173
|
emitRooms();
|
|
1174
|
+
if (member !== null)
|
|
1175
|
+
emitMember();
|
|
569
1176
|
for (const locator of locators)
|
|
570
1177
|
emitQueue(locator);
|
|
571
1178
|
publishRoomDrafts(false);
|
|
572
1179
|
return lastCompleted;
|
|
573
1180
|
}
|
|
574
1181
|
async function open() {
|
|
575
|
-
|
|
1182
|
+
if (closed)
|
|
1183
|
+
throw new Error('runtime closed');
|
|
1184
|
+
const opened = await openRuntimeStore(isFenced);
|
|
576
1185
|
store = opened;
|
|
1186
|
+
const interrupted = await opened.fences();
|
|
1187
|
+
for (const locator of interrupted) {
|
|
1188
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
1189
|
+
lastFenced.set(locator, ++fencings);
|
|
1190
|
+
}
|
|
577
1191
|
const kept = options.shell.generation;
|
|
578
1192
|
const storedGeneration = await opened.generation();
|
|
579
1193
|
const loss = detectStorageLoss(storedGeneration, kept);
|
|
@@ -587,6 +1201,8 @@ export function composeRuntime(options) {
|
|
|
587
1201
|
emitStorageLoss();
|
|
588
1202
|
}
|
|
589
1203
|
const lastCompleted = await readState(opened);
|
|
1204
|
+
if (interrupted.length > 0)
|
|
1205
|
+
await erased(opened, interrupted);
|
|
590
1206
|
moveTo({ ...status, lastCompleted, ...attention() });
|
|
591
1207
|
return opened;
|
|
592
1208
|
}
|
|
@@ -613,10 +1229,15 @@ export function composeRuntime(options) {
|
|
|
613
1229
|
return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
|
|
614
1230
|
}
|
|
615
1231
|
async function settle(local, locator, before, confirmed, queue, settling = {}) {
|
|
616
|
-
const
|
|
1232
|
+
const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
1233
|
+
const withdrawn = withdrawals(confirmed, queue, withdrawable, localSaves);
|
|
617
1234
|
const kept = queue.filter((batch) => !withdrawn.includes(batch));
|
|
1235
|
+
const unqueued = withdrawn.map((batch) => ({
|
|
1236
|
+
batch,
|
|
1237
|
+
returned: localSaves || landed(batch, confirmed) ? null : returnedBy('withdrawn'),
|
|
1238
|
+
}));
|
|
618
1239
|
const returning = [
|
|
619
|
-
...
|
|
1240
|
+
...[...unqueued].reverse(),
|
|
620
1241
|
...(settling.leaving === undefined ? [] : [settling.leaving]),
|
|
621
1242
|
];
|
|
622
1243
|
const written = new Map();
|
|
@@ -634,27 +1255,48 @@ export function composeRuntime(options) {
|
|
|
634
1255
|
});
|
|
635
1256
|
}
|
|
636
1257
|
}
|
|
637
|
-
await local.
|
|
638
|
-
|
|
639
|
-
previous: before?.confirmed.record ?? null,
|
|
640
|
-
queue: kept,
|
|
641
|
-
previousQueue: before?.queue ?? [],
|
|
642
|
-
roomDrafts: [...written.values()],
|
|
643
|
-
});
|
|
644
|
-
for (const batch of before?.queue ?? []) {
|
|
1258
|
+
await local.keepAuthored({ locator, queue: kept, roomDrafts: [...written.values()] });
|
|
1259
|
+
for (const batch of previousQueue) {
|
|
645
1260
|
if (!kept.includes(batch))
|
|
646
1261
|
pendings.delete(batch.id);
|
|
647
1262
|
}
|
|
648
1263
|
for (const record of written.values())
|
|
649
1264
|
rememberRoomDraft(record);
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
1265
|
+
try {
|
|
1266
|
+
await local.write({
|
|
1267
|
+
next: confirmed.record,
|
|
1268
|
+
previous: before?.confirmed.record ?? null,
|
|
1269
|
+
...(settling.stored === undefined ? {} : { stored: settling.stored }),
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
catch (error) {
|
|
1273
|
+
const held = before === undefined ? null : replicaOf(before.confirmed, kept, before);
|
|
1274
|
+
if (held !== null)
|
|
1275
|
+
replicas.set(locator, held);
|
|
1276
|
+
else if (kept.length > 0)
|
|
1277
|
+
queuesWithoutReplica.set(locator, kept);
|
|
1278
|
+
else
|
|
1279
|
+
queuesWithoutReplica.delete(locator);
|
|
1280
|
+
publishChangeSet(changeSet(locator, before ?? null, held, {
|
|
1281
|
+
queue: kept.length !== previousQueue.length,
|
|
1282
|
+
drafts: [...written.keys()],
|
|
1283
|
+
}));
|
|
1284
|
+
moveTo({ ...status, ...attention() });
|
|
1285
|
+
throw error;
|
|
1286
|
+
}
|
|
1287
|
+
return () => {
|
|
1288
|
+
queuesWithoutReplica.delete(locator);
|
|
1289
|
+
for (const { batch, returned } of unqueued) {
|
|
1290
|
+
const outcome = localSaves ? 'dropped' : returned === null ? 'landed' : 'withdrawn';
|
|
1291
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
1292
|
+
}
|
|
1293
|
+
replicas.set(locator, replicaOf(confirmed, kept, before));
|
|
1294
|
+
return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
|
|
1295
|
+
room: settling.room,
|
|
1296
|
+
queue: kept.length !== previousQueue.length,
|
|
1297
|
+
drafts: [...written.keys()],
|
|
1298
|
+
});
|
|
1299
|
+
};
|
|
658
1300
|
}
|
|
659
1301
|
function adoptModels(locator, record) {
|
|
660
1302
|
for (const [attached, model] of [
|
|
@@ -687,10 +1329,16 @@ export function composeRuntime(options) {
|
|
|
687
1329
|
selection: taken(answers?.selection, held.selection, 'typed-selection'),
|
|
688
1330
|
};
|
|
689
1331
|
}
|
|
690
|
-
function publish(local, room, confirmed,
|
|
1332
|
+
function publish(local, room, confirmed, landing, live) {
|
|
1333
|
+
const { base, stored } = landing;
|
|
691
1334
|
return exclusive(async () => {
|
|
692
|
-
if (
|
|
1335
|
+
if (stored !== undefined) {
|
|
1336
|
+
await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
|
|
1337
|
+
}
|
|
1338
|
+
if (!live()) {
|
|
1339
|
+
caughtUpTo(room.locator);
|
|
693
1340
|
return true;
|
|
1341
|
+
}
|
|
694
1342
|
const prior = replicas.get(room.locator);
|
|
695
1343
|
if (base !== undefined &&
|
|
696
1344
|
(prior?.confirmed.record.roomVersion !== base.record.roomVersion ||
|
|
@@ -698,76 +1346,179 @@ export function composeRuntime(options) {
|
|
|
698
1346
|
return false;
|
|
699
1347
|
}
|
|
700
1348
|
adoptModels(room.locator, confirmed.record);
|
|
701
|
-
const queue = prior?.queue ?? [];
|
|
1349
|
+
const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
|
|
702
1350
|
const roomVersion = confirmed.record.roomVersion;
|
|
703
1351
|
const accepted = queue.filter((batch) => {
|
|
704
1352
|
const accepted = pendings.get(batch.id)?.accepted;
|
|
705
1353
|
return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
|
|
706
1354
|
});
|
|
707
|
-
|
|
1355
|
+
let settled;
|
|
1356
|
+
try {
|
|
1357
|
+
settled = await settle(local, room.locator, prior, confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true, ...(stored === undefined ? {} : { stored }) });
|
|
1358
|
+
}
|
|
1359
|
+
catch (error) {
|
|
1360
|
+
if (!exceeded(error))
|
|
1361
|
+
throw error;
|
|
1362
|
+
await exceededOn(room.locator, grownBy(confirmed.record, prior?.confirmed.record));
|
|
1363
|
+
caughtUpTo(room.locator);
|
|
1364
|
+
return true;
|
|
1365
|
+
}
|
|
1366
|
+
const set = settled();
|
|
1367
|
+
if (stored !== undefined) {
|
|
1368
|
+
transfers.delete(room.locator);
|
|
1369
|
+
catching.delete(room.locator);
|
|
1370
|
+
}
|
|
708
1371
|
for (const batch of accepted)
|
|
709
1372
|
pendings.delete(batch.id);
|
|
710
1373
|
stale.delete(room.locator);
|
|
1374
|
+
unindexable.delete(room.locator);
|
|
711
1375
|
if (unreadable.delete(room.locator))
|
|
712
1376
|
scopedRefusals.delete(room.locator);
|
|
713
1377
|
sourceRefusals.delete(room.locator);
|
|
714
1378
|
declarationRefusals.delete(room.locator);
|
|
715
1379
|
list(room, true);
|
|
1380
|
+
if (prior === undefined)
|
|
1381
|
+
prepared.add(room.locator);
|
|
716
1382
|
publishChangeSet(set);
|
|
717
1383
|
moveTo({ ...status, ...attention() });
|
|
1384
|
+
options.adapter.roomStream?.published(room.locator, roomVersion);
|
|
718
1385
|
return true;
|
|
719
1386
|
});
|
|
720
1387
|
}
|
|
721
1388
|
async function saveRoomDrafts(locator, named, writtenBy) {
|
|
722
1389
|
const local = await openLocal();
|
|
1390
|
+
await referenced(locator);
|
|
723
1391
|
const replica = replicas.get(locator);
|
|
724
1392
|
const room = rooms.get(locator);
|
|
725
1393
|
if (replica === undefined || room === undefined) {
|
|
726
|
-
throw
|
|
1394
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
727
1395
|
}
|
|
728
1396
|
const saved = [];
|
|
729
1397
|
for (const { path, revision } of named) {
|
|
730
1398
|
const record = roomDrafts.get(locator)?.get(path);
|
|
731
1399
|
if (record === undefined || record.revision !== revision) {
|
|
732
|
-
return { outcome: 'refused',
|
|
1400
|
+
return { outcome: 'refused', code: 'stale', retryable: false, file: path };
|
|
733
1401
|
}
|
|
734
1402
|
if (!saved.some((draft) => draft.path === path))
|
|
735
1403
|
saved.push({ path, parts: record.parts });
|
|
736
1404
|
}
|
|
737
1405
|
const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
|
|
738
1406
|
if (changed.length > 0)
|
|
739
|
-
return { outcome: 'refused',
|
|
1407
|
+
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
740
1408
|
const parts = saved.map(({ path, parts }) => changesOf(path, parts));
|
|
741
|
-
const
|
|
1409
|
+
const changes = parts.flatMap(({ changes }) => changes);
|
|
1410
|
+
const requeue = requeued(replica.queue, changes, replica.visible, withdrawable, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
|
|
1411
|
+
if (requeue.kind === 'refused')
|
|
1412
|
+
return { outcome: 'refused', ...requeue.refusal };
|
|
1413
|
+
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
1414
|
+
const writer = replica.queue.find((batch) => batch.id === origin)?.writtenBy;
|
|
1415
|
+
return writer?.app !== writtenBy?.app || writer?.release !== writtenBy?.release;
|
|
1416
|
+
});
|
|
1417
|
+
if (foreign) {
|
|
1418
|
+
return {
|
|
1419
|
+
outcome: 'refused',
|
|
1420
|
+
code: 'unknown-revision',
|
|
1421
|
+
retryable: false,
|
|
1422
|
+
file: [...requeue.queued].at(-1),
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
const withdrawing = requeue.withdrawn.size > 0;
|
|
1426
|
+
const input = {
|
|
1427
|
+
local: localSaves,
|
|
742
1428
|
baseRoomVersion: replica.confirmed.record.roomVersion,
|
|
743
1429
|
composition: compositionFor(replica.confirmed.record),
|
|
744
1430
|
operations: room.operations,
|
|
745
1431
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
746
1432
|
providerLimits: limits,
|
|
1433
|
+
handle: room.handle,
|
|
747
1434
|
visible: replica.visible,
|
|
748
|
-
changes
|
|
1435
|
+
changes,
|
|
749
1436
|
bases: parts.flatMap(({ bases }) => bases),
|
|
750
1437
|
writtenBy,
|
|
1438
|
+
clean: {
|
|
1439
|
+
text: replica.confirmed.record.documents,
|
|
1440
|
+
binaries: replica.confirmed.record.binaries,
|
|
1441
|
+
composition: compositionOf(replica.confirmed.record),
|
|
1442
|
+
},
|
|
1443
|
+
};
|
|
1444
|
+
const checked = withdrawing ? buildBatch({ ...input, queued: requeue.queued }) : null;
|
|
1445
|
+
if (checked?.kind === 'refused')
|
|
1446
|
+
return { outcome: 'refused', ...checked.refusal };
|
|
1447
|
+
const owners = [...requeue.owners].flatMap((origin) => {
|
|
1448
|
+
const batch = requeue.withdrawn.get(origin);
|
|
1449
|
+
return batch === undefined || batch === null ? [] : [{ origin, batch }];
|
|
751
1450
|
});
|
|
752
|
-
if (
|
|
1451
|
+
if (owners.length > 1) {
|
|
1452
|
+
return {
|
|
1453
|
+
outcome: 'refused',
|
|
1454
|
+
code: 'unknown-revision',
|
|
1455
|
+
retryable: false,
|
|
1456
|
+
file: [...requeue.queued].at(-1),
|
|
1457
|
+
};
|
|
1458
|
+
}
|
|
1459
|
+
const built = !withdrawing
|
|
1460
|
+
? buildBatch(input)
|
|
1461
|
+
: requeue.changes.length === 0
|
|
1462
|
+
? null
|
|
1463
|
+
: buildBatch({
|
|
1464
|
+
...input,
|
|
1465
|
+
visible: replicaOf(replica.confirmed, requeue.queue, replica).visible,
|
|
1466
|
+
changes: requeue.changes,
|
|
1467
|
+
});
|
|
1468
|
+
if (built?.kind === 'refused')
|
|
753
1469
|
return { outcome: 'refused', ...built.refusal };
|
|
754
|
-
const
|
|
1470
|
+
const [owner] = owners;
|
|
1471
|
+
const join = built === null || owner === undefined
|
|
1472
|
+
? null
|
|
1473
|
+
: joined(requeue.queue, owner.batch, built.batch);
|
|
1474
|
+
if (join?.kind === 'refused')
|
|
1475
|
+
return { outcome: 'refused', ...join.refusal };
|
|
1476
|
+
const withdrawn = new Map(requeue.withdrawn);
|
|
1477
|
+
if (join !== null && owner !== undefined)
|
|
1478
|
+
withdrawn.set(owner.origin, join.batch);
|
|
1479
|
+
const drafted = new Map();
|
|
1480
|
+
for (const [origin, replaced] of withdrawn) {
|
|
1481
|
+
if (replaced === null)
|
|
1482
|
+
continue;
|
|
1483
|
+
const carried = withdrawnDrafts(pendings.get(origin)?.drafts ?? [], changes);
|
|
1484
|
+
drafted.set(replaced.id, replaced === join?.batch
|
|
1485
|
+
? [
|
|
1486
|
+
...carried.map((held) => joinedDraft(held, saved)),
|
|
1487
|
+
...saved.filter((draft) => !carried.some((held) => held.path === draft.path)),
|
|
1488
|
+
]
|
|
1489
|
+
: carried);
|
|
1490
|
+
}
|
|
1491
|
+
if (built !== null && join === null)
|
|
1492
|
+
drafted.set(built.batch.id, saved);
|
|
1493
|
+
const batches = join !== null ? join.queue : built === null ? requeue.queue : [...requeue.queue, built.batch];
|
|
1494
|
+
for (const replaced of withdrawn.values()) {
|
|
1495
|
+
if (replaced === null || !batches.includes(replaced))
|
|
1496
|
+
continue;
|
|
1497
|
+
const limited = limitedIn(batches, replaced, limits, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
|
|
1498
|
+
if (limited !== null)
|
|
1499
|
+
return { outcome: 'refused', ...limited };
|
|
1500
|
+
}
|
|
755
1501
|
const consumed = saved.map(({ path }) => path);
|
|
756
1502
|
try {
|
|
757
|
-
await local.
|
|
758
|
-
|
|
759
|
-
previous: replica.confirmed.record,
|
|
1503
|
+
await local.keepAuthored({
|
|
1504
|
+
locator,
|
|
760
1505
|
queue: batches,
|
|
761
|
-
|
|
762
|
-
saved,
|
|
1506
|
+
saved: drafted,
|
|
763
1507
|
consumedRoomDrafts: consumed,
|
|
764
1508
|
});
|
|
765
1509
|
}
|
|
766
1510
|
catch {
|
|
767
|
-
return { outcome: 'refused',
|
|
1511
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
1512
|
+
}
|
|
1513
|
+
if (fences.has(locator))
|
|
1514
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
1515
|
+
for (const origin of withdrawn.keys())
|
|
1516
|
+
pendings.delete(origin);
|
|
1517
|
+
for (const [id, drafts] of drafted) {
|
|
1518
|
+
pendings.set(id, { drafts, submission: { state: 'waiting' }, lost: false });
|
|
768
1519
|
}
|
|
769
|
-
|
|
770
|
-
|
|
1520
|
+
if (!localSaves)
|
|
1521
|
+
recorded = false;
|
|
771
1522
|
for (const path of consumed)
|
|
772
1523
|
forgetRoomDraft(locator, path);
|
|
773
1524
|
replicas.set(locator, replicaOf(replica.confirmed, batches, replica));
|
|
@@ -775,9 +1526,12 @@ export function composeRuntime(options) {
|
|
|
775
1526
|
queue: true,
|
|
776
1527
|
drafts: consumed,
|
|
777
1528
|
}));
|
|
778
|
-
moveTo({ ...saving(status), ...attention() });
|
|
1529
|
+
moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
|
|
779
1530
|
submitLater(locator);
|
|
780
|
-
|
|
1531
|
+
const identifier = join?.batch.id ??
|
|
1532
|
+
built?.batch.id ??
|
|
1533
|
+
[...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
|
|
1534
|
+
return { outcome: 'saved', identifier };
|
|
781
1535
|
}
|
|
782
1536
|
async function mark(local, locator, identifier, submission, lost) {
|
|
783
1537
|
const state = pendings.get(identifier);
|
|
@@ -787,6 +1541,8 @@ export function composeRuntime(options) {
|
|
|
787
1541
|
return;
|
|
788
1542
|
}
|
|
789
1543
|
await local.submit(locator, identifier, submission, lost);
|
|
1544
|
+
if (fences.has(locator))
|
|
1545
|
+
return;
|
|
790
1546
|
pendings.set(identifier, { ...state, submission, lost });
|
|
791
1547
|
const replica = replicas.get(locator);
|
|
792
1548
|
if (replica !== undefined)
|
|
@@ -797,22 +1553,24 @@ export function composeRuntime(options) {
|
|
|
797
1553
|
if (!continuing())
|
|
798
1554
|
throw new Error('connection refused');
|
|
799
1555
|
await local.keepUpload(locator, identifier, upload);
|
|
1556
|
+
if (fences.has(locator))
|
|
1557
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
800
1558
|
pendings.set(identifier, { ...pendings.get(identifier), upload });
|
|
801
1559
|
if (!continuing())
|
|
802
1560
|
throw new Error('connection refused');
|
|
803
1561
|
}
|
|
804
1562
|
async function readHistory(locator, path, query = {}) {
|
|
805
1563
|
if (closed || refusal !== null)
|
|
806
|
-
return
|
|
1564
|
+
return UNCERTAIN;
|
|
807
1565
|
try {
|
|
808
1566
|
await ensureAuthorization();
|
|
809
1567
|
}
|
|
810
1568
|
catch {
|
|
811
|
-
return
|
|
812
|
-
}
|
|
813
|
-
if (closed || authorization === undefined || refusal !== null) {
|
|
814
|
-
return { kind: 'transport-uncertainty' };
|
|
1569
|
+
return UNCERTAIN;
|
|
815
1570
|
}
|
|
1571
|
+
if (closed || authorization === undefined || refusal !== null)
|
|
1572
|
+
return UNCERTAIN;
|
|
1573
|
+
const since = fencings;
|
|
816
1574
|
const transfer = ['transfer', 'history', locator, path];
|
|
817
1575
|
if (query.revision !== undefined)
|
|
818
1576
|
transfer.push(query.revision);
|
|
@@ -827,6 +1585,8 @@ export function composeRuntime(options) {
|
|
|
827
1585
|
...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
|
|
828
1586
|
});
|
|
829
1587
|
journal.record(`outcome history ${result.kind}`);
|
|
1588
|
+
if (fencedSince(locator, since))
|
|
1589
|
+
return failure({ kind: 'room-not-readable', locator });
|
|
830
1590
|
if (result.kind === 'scoped-refusal') {
|
|
831
1591
|
const room = replicas.get(result.locator)?.confirmed.record;
|
|
832
1592
|
if (room !== undefined)
|
|
@@ -834,17 +1594,111 @@ export function composeRuntime(options) {
|
|
|
834
1594
|
}
|
|
835
1595
|
if (result.kind === 'revoked-authorization')
|
|
836
1596
|
await refuse('revoked-authorization');
|
|
837
|
-
return result;
|
|
1597
|
+
return historyAnswer(result);
|
|
838
1598
|
}
|
|
839
1599
|
catch {
|
|
840
1600
|
journal.record('outcome history transport-uncertainty');
|
|
841
|
-
return
|
|
1601
|
+
return UNCERTAIN;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
function queuedBlob(locator, path, localId) {
|
|
1605
|
+
for (const batch of replicas.get(locator)?.queue ?? []) {
|
|
1606
|
+
const change = batch.changes[queuedFile(batch, path, localId)];
|
|
1607
|
+
if (change?.kind === 'binary')
|
|
1608
|
+
return change.blob;
|
|
842
1609
|
}
|
|
1610
|
+
return null;
|
|
843
1611
|
}
|
|
844
|
-
function
|
|
1612
|
+
function binaryRoom(request) {
|
|
1613
|
+
if (request.kind === 'path')
|
|
1614
|
+
return request.locator;
|
|
1615
|
+
const scope = options.adapter.referrerScope(request.referrer);
|
|
1616
|
+
return scope?.kind === 'room' ? scope.locator : null;
|
|
1617
|
+
}
|
|
1618
|
+
async function readBinary(request) {
|
|
1619
|
+
if (closed || refusal !== null)
|
|
1620
|
+
return UNCERTAIN;
|
|
1621
|
+
try {
|
|
1622
|
+
await ensureAuthorization();
|
|
1623
|
+
}
|
|
1624
|
+
catch {
|
|
1625
|
+
return UNCERTAIN;
|
|
1626
|
+
}
|
|
1627
|
+
if (closed || authorization === undefined || refusal !== null)
|
|
1628
|
+
return UNCERTAIN;
|
|
1629
|
+
const since = fencings;
|
|
1630
|
+
journal.record(request.kind === 'path'
|
|
1631
|
+
? `transfer binary ${request.locator} ${request.path}`
|
|
1632
|
+
: `transfer binary ${request.fileRef}`);
|
|
1633
|
+
try {
|
|
1634
|
+
const result = await options.adapter.binary(authorization, request);
|
|
1635
|
+
journal.record(`outcome binary ${result.kind}`);
|
|
1636
|
+
const locator = binaryRoom(request);
|
|
1637
|
+
if (locator !== null && fencedSince(locator, since))
|
|
1638
|
+
return failure({ kind: 'room-not-readable', locator });
|
|
1639
|
+
if (result.kind === 'scoped-refusal') {
|
|
1640
|
+
const room = replicas.get(result.locator)?.confirmed.record;
|
|
1641
|
+
if (room !== undefined)
|
|
1642
|
+
await scoped(result, room, await openLocal());
|
|
1643
|
+
}
|
|
1644
|
+
if (result.kind === 'revoked-authorization')
|
|
1645
|
+
await refuse('revoked-authorization');
|
|
1646
|
+
return binaryAnswer(result);
|
|
1647
|
+
}
|
|
1648
|
+
catch {
|
|
1649
|
+
journal.record('outcome binary transport-uncertainty');
|
|
1650
|
+
return UNCERTAIN;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
async function readBinaryPath(locator, path, revision) {
|
|
1654
|
+
await ready;
|
|
1655
|
+
if (revision !== undefined)
|
|
1656
|
+
return readBinary({ kind: 'path', locator, path, revision });
|
|
1657
|
+
const visible = replicas.get(locator)?.visible;
|
|
1658
|
+
const held = visible?.binaries[path];
|
|
1659
|
+
if (visible === undefined || held === undefined)
|
|
1660
|
+
return absentBinary(path);
|
|
1661
|
+
if (held.state === 'pending') {
|
|
1662
|
+
const blob = queuedBlob(locator, path, held.localId);
|
|
1663
|
+
return blob === null ? absentBinary(path) : { kind: 'binary', blob };
|
|
1664
|
+
}
|
|
1665
|
+
const record = binaryRecord(path, held, visible.revisions[path]);
|
|
1666
|
+
if (record.state !== 'accepted')
|
|
1667
|
+
return absentBinary(path);
|
|
1668
|
+
const replica = replicas.get(locator);
|
|
1669
|
+
const queued = (replica?.queue ?? []).flatMap((batch) => batch.changes);
|
|
1670
|
+
const holds = (at) => {
|
|
1671
|
+
const kept = replica?.confirmed.record.binaries[at];
|
|
1672
|
+
return (kept?.state === 'accepted' &&
|
|
1673
|
+
kept.fileRef === record.fileRef &&
|
|
1674
|
+
replica?.confirmed.record.revisions[at] === record.revision);
|
|
1675
|
+
};
|
|
1676
|
+
let confirmed = path;
|
|
1677
|
+
for (let index = queued.length - 1; index >= 0 && !holds(confirmed); index -= 1) {
|
|
1678
|
+
const change = queued[index];
|
|
1679
|
+
if (change.kind === 'move' && change.to === confirmed)
|
|
1680
|
+
confirmed = change.path;
|
|
1681
|
+
}
|
|
1682
|
+
return readBinary({
|
|
1683
|
+
kind: 'path',
|
|
1684
|
+
locator,
|
|
1685
|
+
path: confirmed,
|
|
1686
|
+
revision: record.revision,
|
|
1687
|
+
fileRef: record.fileRef,
|
|
1688
|
+
});
|
|
1689
|
+
}
|
|
1690
|
+
async function readBinaryReference(reference) {
|
|
1691
|
+
await ready;
|
|
1692
|
+
return readBinary({
|
|
1693
|
+
kind: 'reference',
|
|
1694
|
+
fileRef: reference.fileRef,
|
|
1695
|
+
referrer: reference.referrer,
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
function declaredAgents() {
|
|
845
1699
|
for (const capability of capabilities) {
|
|
846
|
-
if (capability.
|
|
847
|
-
return capability.
|
|
1700
|
+
if (capability.conversationAgents !== undefined)
|
|
1701
|
+
return capability.conversationAgents;
|
|
848
1702
|
}
|
|
849
1703
|
return undefined;
|
|
850
1704
|
}
|
|
@@ -860,18 +1714,17 @@ export function composeRuntime(options) {
|
|
|
860
1714
|
async function invokeAction(name, input) {
|
|
861
1715
|
await ready;
|
|
862
1716
|
if (closed || refusal !== null)
|
|
863
|
-
return
|
|
1717
|
+
return UNCERTAIN;
|
|
864
1718
|
if (declaredAction(name) === undefined)
|
|
865
|
-
return
|
|
1719
|
+
return undeclared(name);
|
|
866
1720
|
try {
|
|
867
1721
|
await ensureAuthorization();
|
|
868
1722
|
}
|
|
869
1723
|
catch {
|
|
870
|
-
return
|
|
871
|
-
}
|
|
872
|
-
if (closed || authorization === undefined || refusal !== null) {
|
|
873
|
-
return { kind: 'transport-uncertainty' };
|
|
1724
|
+
return UNCERTAIN;
|
|
874
1725
|
}
|
|
1726
|
+
if (closed || authorization === undefined || refusal !== null)
|
|
1727
|
+
return UNCERTAIN;
|
|
875
1728
|
journal.record(`transfer action ${name}`);
|
|
876
1729
|
try {
|
|
877
1730
|
const result = await options.adapter.action(authorization, { name, input });
|
|
@@ -885,11 +1738,11 @@ export function composeRuntime(options) {
|
|
|
885
1738
|
journal.record(`outcome action ${result.kind}`);
|
|
886
1739
|
if (result.kind === 'rejected')
|
|
887
1740
|
journal.record(`refusal rejected ${result.code}`);
|
|
888
|
-
return result;
|
|
1741
|
+
return calledResult(result);
|
|
889
1742
|
}
|
|
890
1743
|
catch {
|
|
891
1744
|
journal.record('outcome action transport-uncertainty');
|
|
892
|
-
return
|
|
1745
|
+
return UNCERTAIN;
|
|
893
1746
|
}
|
|
894
1747
|
}
|
|
895
1748
|
function continuing() {
|
|
@@ -906,11 +1759,11 @@ export function composeRuntime(options) {
|
|
|
906
1759
|
options.adapter.reached(locator, written, roomVersion) &&
|
|
907
1760
|
!options.adapter.reached(locator, roomVersion, written));
|
|
908
1761
|
}
|
|
909
|
-
async function pullRefresh(authorized, confirmed) {
|
|
910
|
-
if (!
|
|
1762
|
+
async function pullRefresh(authorized, confirmed, live) {
|
|
1763
|
+
if (!live())
|
|
911
1764
|
return unconfirmed;
|
|
912
1765
|
try {
|
|
913
|
-
const result = await refreshRoom(options.adapter, authorized, confirmed, (models, held) => remodel(confirmed.record.locator, models, held),
|
|
1766
|
+
const result = await refreshRoom(options.adapter, authorized, confirmed, (models, held) => (live() ? remodel(confirmed.record.locator, models, held) : held), live);
|
|
914
1767
|
const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
|
|
915
1768
|
journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
|
|
916
1769
|
return result;
|
|
@@ -920,33 +1773,85 @@ export function composeRuntime(options) {
|
|
|
920
1773
|
throw error;
|
|
921
1774
|
}
|
|
922
1775
|
}
|
|
923
|
-
|
|
924
|
-
|
|
1776
|
+
function staging(local, live) {
|
|
1777
|
+
return {
|
|
1778
|
+
held: (locator) => transfers.get(locator) ?? null,
|
|
1779
|
+
staged: (record) => local.staged(record),
|
|
1780
|
+
stage: (record, page) => exclusive(async () => {
|
|
1781
|
+
if (!live())
|
|
1782
|
+
return false;
|
|
1783
|
+
const bytes = layerBytes(page);
|
|
1784
|
+
if (!(await admitted(local, record.locator, bytes)))
|
|
1785
|
+
return false;
|
|
1786
|
+
try {
|
|
1787
|
+
await local.stage(record, page);
|
|
1788
|
+
}
|
|
1789
|
+
catch (error) {
|
|
1790
|
+
if (!exceeded(error))
|
|
1791
|
+
throw error;
|
|
1792
|
+
await exceededOn(record.locator, bytes);
|
|
1793
|
+
return false;
|
|
1794
|
+
}
|
|
1795
|
+
transfers.set(record.locator, record);
|
|
1796
|
+
readSide.recover();
|
|
1797
|
+
return true;
|
|
1798
|
+
}),
|
|
1799
|
+
abandon: (locator) => exclusive(async () => {
|
|
1800
|
+
if (!live())
|
|
1801
|
+
return;
|
|
1802
|
+
await local.abandon(locator);
|
|
1803
|
+
transfers.delete(locator);
|
|
1804
|
+
readSide.recover();
|
|
1805
|
+
}),
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1808
|
+
function reusable(room) {
|
|
1809
|
+
const record = replicas.get(room.locator)?.confirmed.record;
|
|
1810
|
+
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
1811
|
+
return null;
|
|
1812
|
+
return { documents: record.documents, revisions: record.revisions };
|
|
1813
|
+
}
|
|
1814
|
+
async function pullBootstrap(authorized, room, local, live) {
|
|
1815
|
+
if (!live())
|
|
925
1816
|
return unconfirmed;
|
|
1817
|
+
if (replicas.has(room.locator)) {
|
|
1818
|
+
catching.add(room.locator);
|
|
1819
|
+
readSide.recover();
|
|
1820
|
+
}
|
|
926
1821
|
try {
|
|
927
1822
|
const result = await bootstrapRoom(options.adapter, authorized, room, {
|
|
928
1823
|
collection: modelOf(room.locator),
|
|
929
1824
|
selection: modelOf(room.locator, 'typed-selection'),
|
|
930
|
-
}, (models, held) => remodel(room.locator, models, held),
|
|
1825
|
+
}, (models, held) => (live() ? remodel(room.locator, models, held) : held), live, staging(local, live), reusable(room));
|
|
931
1826
|
const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
|
|
932
1827
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
1828
|
+
if (result.kind !== 'staged')
|
|
1829
|
+
caughtUpTo(room.locator);
|
|
933
1830
|
return result;
|
|
934
1831
|
}
|
|
935
1832
|
catch (error) {
|
|
936
1833
|
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
1834
|
+
caughtUpTo(room.locator);
|
|
937
1835
|
throw error;
|
|
938
1836
|
}
|
|
939
1837
|
}
|
|
940
|
-
|
|
1838
|
+
function caughtUpTo(locator) {
|
|
1839
|
+
if (catching.delete(locator))
|
|
1840
|
+
readSide.recover();
|
|
1841
|
+
}
|
|
1842
|
+
async function advance(room, local, authorized, live) {
|
|
1843
|
+
await referenced(room.locator);
|
|
1844
|
+
if (!live() || paused.has(room.locator))
|
|
1845
|
+
return unconfirmed;
|
|
941
1846
|
const held = replicas.get(room.locator);
|
|
942
1847
|
if (held !== undefined) {
|
|
943
|
-
const refreshed = await pullRefresh(authorized, held.confirmed);
|
|
944
|
-
if (!
|
|
1848
|
+
const refreshed = await pullRefresh(authorized, held.confirmed, live);
|
|
1849
|
+
if (!live())
|
|
945
1850
|
return unconfirmed;
|
|
946
1851
|
if (refreshed.kind === 'still-current') {
|
|
947
|
-
if (sourceRefusals.has(room.locator))
|
|
948
|
-
return unconfirmed;
|
|
949
1852
|
stale.delete(room.locator);
|
|
1853
|
+
if (sourceRefusals.has(room.locator))
|
|
1854
|
+
return null;
|
|
950
1855
|
if (unreadable.delete(room.locator)) {
|
|
951
1856
|
scopedRefusals.delete(room.locator);
|
|
952
1857
|
list(room, true);
|
|
@@ -955,28 +1860,45 @@ export function composeRuntime(options) {
|
|
|
955
1860
|
return null;
|
|
956
1861
|
}
|
|
957
1862
|
if (refreshed.kind === 'advanced') {
|
|
958
|
-
if (!(await publish(local, room, refreshed.confirmed, held.confirmed))) {
|
|
1863
|
+
if (!(await publish(local, room, refreshed.confirmed, { base: held.confirmed }, live))) {
|
|
959
1864
|
const now = replicas.get(room.locator)?.confirmed.record.roomVersion;
|
|
960
1865
|
const reached = now !== undefined &&
|
|
961
1866
|
options.adapter.reached(room.locator, now, refreshed.confirmed.record.roomVersion);
|
|
962
|
-
return reached ? null : advance(room, local, authorized);
|
|
1867
|
+
return reached ? null : advance(room, local, authorized, live);
|
|
963
1868
|
}
|
|
964
1869
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
965
1870
|
return null;
|
|
966
1871
|
}
|
|
967
1872
|
if (refreshed.kind !== 'beyond-horizon')
|
|
968
1873
|
return refreshed;
|
|
1874
|
+
if (opened !== null) {
|
|
1875
|
+
owed.add(room.locator);
|
|
1876
|
+
readSide.recover();
|
|
1877
|
+
return unconfirmed;
|
|
1878
|
+
}
|
|
969
1879
|
}
|
|
970
|
-
const bootstrapped = await pullBootstrap(authorized, room);
|
|
971
|
-
if (!
|
|
1880
|
+
const bootstrapped = await pullBootstrap(authorized, room, local, live);
|
|
1881
|
+
if (!live())
|
|
972
1882
|
return unconfirmed;
|
|
973
1883
|
if (bootstrapped.kind !== 'staged')
|
|
974
1884
|
return bootstrapped;
|
|
975
|
-
await publish(local, room, bootstrapped.confirmed);
|
|
1885
|
+
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
976
1886
|
return null;
|
|
977
1887
|
}
|
|
978
1888
|
async function withhold(withheld, room, local, progress) {
|
|
979
|
-
|
|
1889
|
+
if (withheld.kind === 'unconfirmed')
|
|
1890
|
+
return;
|
|
1891
|
+
if (withheld.kind === 'refused')
|
|
1892
|
+
stale.delete(room.locator);
|
|
1893
|
+
else
|
|
1894
|
+
stale.add(room.locator);
|
|
1895
|
+
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
1896
|
+
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
1897
|
+
repairable.set(room.locator, reset);
|
|
1898
|
+
repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
|
|
1899
|
+
}
|
|
1900
|
+
else
|
|
1901
|
+
repairable.delete(room.locator);
|
|
980
1902
|
if (withheld.kind === 'scoped-refusal') {
|
|
981
1903
|
await scoped(withheld, room, local);
|
|
982
1904
|
}
|
|
@@ -1001,6 +1923,7 @@ export function composeRuntime(options) {
|
|
|
1001
1923
|
function refusedSource(room, { refusal }) {
|
|
1002
1924
|
sourceRefusals.set(room.locator, {
|
|
1003
1925
|
code: refusal.rule,
|
|
1926
|
+
retryable: false,
|
|
1004
1927
|
fields: {
|
|
1005
1928
|
file: refusal.file,
|
|
1006
1929
|
...(refusal.location === undefined ? {} : { location: refusal.location }),
|
|
@@ -1011,12 +1934,21 @@ export function composeRuntime(options) {
|
|
|
1011
1934
|
unreadable.add(room.locator);
|
|
1012
1935
|
list(room, false);
|
|
1013
1936
|
emitRooms();
|
|
1937
|
+
moveTo({ ...status, ...attention() });
|
|
1014
1938
|
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
1015
1939
|
}
|
|
1016
1940
|
function leaving(batch, result) {
|
|
1017
1941
|
if (result.kind === 'expired')
|
|
1018
|
-
return { batch, returned:
|
|
1019
|
-
return {
|
|
1942
|
+
return { batch, returned: returnedBy('expired') };
|
|
1943
|
+
return {
|
|
1944
|
+
batch,
|
|
1945
|
+
returned: {
|
|
1946
|
+
kind: result.kind,
|
|
1947
|
+
code: result.code,
|
|
1948
|
+
fields: result.fields,
|
|
1949
|
+
retryable: result.retryable,
|
|
1950
|
+
},
|
|
1951
|
+
};
|
|
1020
1952
|
}
|
|
1021
1953
|
function logAccepted(locator, submitted, accepted) {
|
|
1022
1954
|
journal.record(`outcome ${locator} ${submitted.id} accepted ${accepted.roomVersion}` +
|
|
@@ -1024,6 +1956,7 @@ export function composeRuntime(options) {
|
|
|
1024
1956
|
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
1025
1957
|
}
|
|
1026
1958
|
async function adoptAccepted(room, local, submitted, accepted) {
|
|
1959
|
+
await referenced(room.locator);
|
|
1027
1960
|
if (!continuing())
|
|
1028
1961
|
return unconfirmed;
|
|
1029
1962
|
const current = replicas.get(room.locator);
|
|
@@ -1034,22 +1967,23 @@ export function composeRuntime(options) {
|
|
|
1034
1967
|
logAccepted(room.locator, submitted, accepted);
|
|
1035
1968
|
const applied = options.adapter.reached(room.locator, held, accepted.roomVersion)
|
|
1036
1969
|
? { kind: 'staged', confirmed: current.confirmed }
|
|
1037
|
-
: applyReturned(current.confirmed,
|
|
1970
|
+
: applyReturned(current.confirmed, submitted, accepted, options.adapter.follows(room.locator, held, accepted.roomVersion)
|
|
1038
1971
|
? accepted.roomVersion
|
|
1039
1972
|
: held, accepted.roomVersion);
|
|
1040
1973
|
if (applied.kind !== 'staged') {
|
|
1041
1974
|
const state = pendings.get(submitted.id);
|
|
1042
1975
|
await local.accept(room.locator, submitted.id, accepted.roomVersion);
|
|
1043
|
-
if (state !== undefined)
|
|
1976
|
+
if (state !== undefined && !fences.has(room.locator))
|
|
1044
1977
|
pendings.set(submitted.id, { ...state, accepted: accepted.roomVersion });
|
|
1045
1978
|
emitQueue(room.locator);
|
|
1046
1979
|
return applied;
|
|
1047
1980
|
}
|
|
1048
|
-
const
|
|
1981
|
+
const settled = await settle(local, room.locator, current, applied.confirmed, current.queue.filter((batch) => batch.id !== submitted.id), { room: true });
|
|
1049
1982
|
pendings.delete(submitted.id);
|
|
1050
1983
|
unreadable.delete(room.locator);
|
|
1051
1984
|
sourceRefusals.delete(room.locator);
|
|
1052
1985
|
declarationRefusals.delete(room.locator);
|
|
1986
|
+
const set = settled();
|
|
1053
1987
|
list(room, true);
|
|
1054
1988
|
publishChangeSet(set);
|
|
1055
1989
|
moveTo({ ...status, ...attention() });
|
|
@@ -1063,14 +1997,14 @@ export function composeRuntime(options) {
|
|
|
1063
1997
|
refusedSource(room, failed);
|
|
1064
1998
|
return false;
|
|
1065
1999
|
}
|
|
1066
|
-
async function submitRoom(room, local, authorized, progress) {
|
|
1067
|
-
while (continuing() && authorization !== undefined) {
|
|
2000
|
+
async function submitRoom(room, local, authorized, progress, signal) {
|
|
2001
|
+
while (continuing() && authorization !== undefined && !signal.aborted) {
|
|
1068
2002
|
const replica = replicas.get(room.locator);
|
|
1069
2003
|
const head = replica?.queue[0];
|
|
1070
2004
|
if (replica === undefined || head === undefined)
|
|
1071
|
-
return;
|
|
2005
|
+
return 'done';
|
|
1072
2006
|
if (pendings.get(head.id)?.accepted !== undefined)
|
|
1073
|
-
return;
|
|
2007
|
+
return 'done';
|
|
1074
2008
|
const state = pendings.get(head.id);
|
|
1075
2009
|
const { submission, lost: earlier } = state ?? {
|
|
1076
2010
|
submission: { state: 'waiting' },
|
|
@@ -1078,24 +2012,30 @@ export function composeRuntime(options) {
|
|
|
1078
2012
|
};
|
|
1079
2013
|
if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
|
|
1080
2014
|
progress.complete = false;
|
|
1081
|
-
return;
|
|
2015
|
+
return { at: Date.parse(submission.reset) };
|
|
1082
2016
|
}
|
|
1083
2017
|
if (writesHeld()) {
|
|
1084
2018
|
progress.complete = false;
|
|
1085
2019
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1086
|
-
return;
|
|
2020
|
+
return 'done';
|
|
1087
2021
|
}
|
|
1088
2022
|
const lost = earlier || submission.state === 'submitted';
|
|
2023
|
+
if (lost && pastHorizon(head.id)) {
|
|
2024
|
+
await discardPending(room, local, signal);
|
|
2025
|
+
return 'done';
|
|
2026
|
+
}
|
|
1089
2027
|
let result;
|
|
1090
2028
|
journal.record(`submission ${room.locator} ${head.id}`);
|
|
1091
2029
|
await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
|
|
1092
|
-
if (discoveryPaused)
|
|
1093
|
-
return;
|
|
1094
|
-
if (writesHeld()) {
|
|
2030
|
+
if (discoveryPaused || fences.has(room.locator))
|
|
2031
|
+
return 'done';
|
|
2032
|
+
if (signal.aborted || writesHeld()) {
|
|
1095
2033
|
await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
|
|
2034
|
+
if (signal.aborted)
|
|
2035
|
+
return 'done';
|
|
1096
2036
|
progress.complete = false;
|
|
1097
2037
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1098
|
-
return;
|
|
2038
|
+
return 'done';
|
|
1099
2039
|
}
|
|
1100
2040
|
const opensBefore = openSignals;
|
|
1101
2041
|
const answered = awaitingAnswer(room.locator);
|
|
@@ -1111,32 +2051,36 @@ export function composeRuntime(options) {
|
|
|
1111
2051
|
progress.complete = false;
|
|
1112
2052
|
if (continuing())
|
|
1113
2053
|
progress.reachable = false;
|
|
1114
|
-
return;
|
|
2054
|
+
return 'again';
|
|
2055
|
+
}
|
|
2056
|
+
if (signal.aborted) {
|
|
2057
|
+
answered();
|
|
2058
|
+
return 'done';
|
|
1115
2059
|
}
|
|
1116
2060
|
if (result.kind !== 'accepted' || !continuing())
|
|
1117
2061
|
answered();
|
|
1118
2062
|
if (!continuing())
|
|
1119
|
-
return;
|
|
2063
|
+
return 'done';
|
|
1120
2064
|
if (result.kind === 'transport-uncertainty') {
|
|
1121
2065
|
progress.complete = false;
|
|
1122
2066
|
progress.reachable = false;
|
|
1123
|
-
return;
|
|
2067
|
+
return 'again';
|
|
1124
2068
|
}
|
|
1125
2069
|
if (result.kind === 'room-not-readable') {
|
|
1126
2070
|
progress.complete = false;
|
|
1127
2071
|
unreadable.add(room.locator);
|
|
1128
2072
|
list(room, false);
|
|
1129
2073
|
emitRooms();
|
|
1130
|
-
return;
|
|
2074
|
+
return 'done';
|
|
1131
2075
|
}
|
|
1132
2076
|
if (result.kind === 'revoked-authorization') {
|
|
1133
2077
|
await refuse('revoked-authorization');
|
|
1134
|
-
return;
|
|
2078
|
+
return 'done';
|
|
1135
2079
|
}
|
|
1136
2080
|
if (result.kind === 'scoped-refusal') {
|
|
1137
2081
|
await scoped(result, room, local);
|
|
1138
2082
|
progress.complete = false;
|
|
1139
|
-
return;
|
|
2083
|
+
return 'done';
|
|
1140
2084
|
}
|
|
1141
2085
|
if (result.kind === 'write-halted') {
|
|
1142
2086
|
journal.record(`outcome ${room.locator} ${head.id} write-halted`);
|
|
@@ -1145,16 +2089,24 @@ export function composeRuntime(options) {
|
|
|
1145
2089
|
continue;
|
|
1146
2090
|
haltedRefusal = true;
|
|
1147
2091
|
progress.complete = false;
|
|
1148
|
-
return;
|
|
2092
|
+
return 'done';
|
|
1149
2093
|
}
|
|
1150
2094
|
if (result.kind === 'conflicted' ||
|
|
1151
2095
|
result.kind === 'rejected' ||
|
|
1152
2096
|
result.kind === 'expired' ||
|
|
1153
2097
|
result.kind === 'retryable-refusal') {
|
|
2098
|
+
if (result.kind === 'expired' && lost && result.restored !== true) {
|
|
2099
|
+
journal.record(`outcome ${room.locator} ${head.id} expired`);
|
|
2100
|
+
await discardPending(room, local, signal);
|
|
2101
|
+
return 'done';
|
|
2102
|
+
}
|
|
1154
2103
|
if (result.kind === 'expired') {
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
2104
|
+
await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, false));
|
|
2105
|
+
const withheld = await advance(room, local, authorized, continuing);
|
|
2106
|
+
if (withheld !== null) {
|
|
2107
|
+
await withhold(withheld, room, local, progress);
|
|
2108
|
+
return 'done';
|
|
2109
|
+
}
|
|
1158
2110
|
}
|
|
1159
2111
|
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
|
|
1160
2112
|
(result.kind === 'conflicted' && result.roomVersion !== undefined
|
|
@@ -1167,8 +2119,7 @@ export function composeRuntime(options) {
|
|
|
1167
2119
|
const reset = result.reset;
|
|
1168
2120
|
progress.complete = false;
|
|
1169
2121
|
await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
|
|
1170
|
-
|
|
1171
|
-
return;
|
|
2122
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
1172
2123
|
}
|
|
1173
2124
|
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
1174
2125
|
const left = leaving(head, result);
|
|
@@ -1179,36 +2130,82 @@ export function composeRuntime(options) {
|
|
|
1179
2130
|
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
1180
2131
|
return;
|
|
1181
2132
|
}
|
|
1182
|
-
const
|
|
1183
|
-
leaving: result.kind === 'expired' && landed(head, current.confirmed)
|
|
2133
|
+
const settled = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
|
|
2134
|
+
leaving: (result.kind === 'expired' && landed(head, current.confirmed)) ||
|
|
2135
|
+
(result.kind === 'conflicted' && landed(head, current.confirmed, result.affected))
|
|
1184
2136
|
? { ...left, returned: null }
|
|
1185
2137
|
: left,
|
|
1186
2138
|
});
|
|
1187
|
-
publishChangeSet(
|
|
2139
|
+
publishChangeSet(settled());
|
|
1188
2140
|
moveTo({ ...status, ...attention() });
|
|
1189
2141
|
});
|
|
1190
2142
|
if (result.kind === 'expired')
|
|
1191
2143
|
continue;
|
|
1192
|
-
const withheld = await advance(room, local, authorized);
|
|
1193
|
-
if (withheld !== null)
|
|
1194
|
-
|
|
2144
|
+
const withheld = await advance(room, local, authorized, continuing);
|
|
2145
|
+
if (withheld !== null) {
|
|
2146
|
+
await withhold(withheld, room, local, progress);
|
|
2147
|
+
return 'done';
|
|
2148
|
+
}
|
|
1195
2149
|
continue;
|
|
1196
2150
|
}
|
|
1197
2151
|
const kept = await adopted(room, local, head, result).finally(answered);
|
|
1198
2152
|
if (!kept) {
|
|
1199
2153
|
progress.complete = false;
|
|
1200
|
-
return;
|
|
2154
|
+
return 'done';
|
|
1201
2155
|
}
|
|
1202
2156
|
}
|
|
2157
|
+
return 'done';
|
|
2158
|
+
}
|
|
2159
|
+
function pastHorizon(identity) {
|
|
2160
|
+
return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
|
|
2161
|
+
}
|
|
2162
|
+
async function discardPending(room, local, signal) {
|
|
2163
|
+
const { locator } = room;
|
|
2164
|
+
const discarded = await exclusive(async () => {
|
|
2165
|
+
if (fences.has(locator))
|
|
2166
|
+
return false;
|
|
2167
|
+
const before = replicas.get(locator);
|
|
2168
|
+
const queue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
2169
|
+
fences.set(locator, { at: discoveries, erased: false });
|
|
2170
|
+
lastFenced.set(locator, ++fencings);
|
|
2171
|
+
try {
|
|
2172
|
+
await local.discard(locator);
|
|
2173
|
+
}
|
|
2174
|
+
finally {
|
|
2175
|
+
fences.delete(locator);
|
|
2176
|
+
}
|
|
2177
|
+
for (const batch of queue)
|
|
2178
|
+
pendings.delete(batch.id);
|
|
2179
|
+
queuesWithoutReplica.delete(locator);
|
|
2180
|
+
const after = before === undefined ? null : replicaOf(before.confirmed, [], before);
|
|
2181
|
+
if (after !== null)
|
|
2182
|
+
replicas.set(locator, after);
|
|
2183
|
+
journal.record(`room discarded ${locator}`);
|
|
2184
|
+
publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
|
|
2185
|
+
moveTo({ ...status, ...attention() });
|
|
2186
|
+
return true;
|
|
2187
|
+
});
|
|
2188
|
+
if (!discarded)
|
|
2189
|
+
return;
|
|
2190
|
+
const progress = { complete: true, reachable: true };
|
|
2191
|
+
await roomTask(locator, async (live) => {
|
|
2192
|
+
await Promise.race([
|
|
2193
|
+
reload(room, local, progress, () => live() && !signal.aborted),
|
|
2194
|
+
abandonedOn(signal),
|
|
2195
|
+
]);
|
|
2196
|
+
});
|
|
2197
|
+
if (signal.aborted)
|
|
2198
|
+
return;
|
|
2199
|
+
await rest(progress);
|
|
1203
2200
|
}
|
|
1204
2201
|
function awaitingAnswer(locator) {
|
|
1205
2202
|
let release = () => { };
|
|
1206
2203
|
const awaited = new Promise((resolve) => {
|
|
1207
2204
|
release = resolve;
|
|
1208
2205
|
});
|
|
1209
|
-
answering.set(locator, awaited);
|
|
2206
|
+
answering.set(locator, { generation, answered: awaited });
|
|
1210
2207
|
return () => {
|
|
1211
|
-
if (answering.get(locator) === awaited)
|
|
2208
|
+
if (answering.get(locator)?.answered === awaited)
|
|
1212
2209
|
answering.delete(locator);
|
|
1213
2210
|
release();
|
|
1214
2211
|
};
|
|
@@ -1241,7 +2238,7 @@ export function composeRuntime(options) {
|
|
|
1241
2238
|
...(attached === 'collection' ? { model: next } : { selectionModel: next }),
|
|
1242
2239
|
};
|
|
1243
2240
|
const reclassified = sameMembership(declared, held.confirmed.record)
|
|
1244
|
-
?
|
|
2241
|
+
? { kind: 'staged', confirmed: confirmedOf(declared, held.confirmed) }
|
|
1245
2242
|
: readReclassified(declared, held.confirmed);
|
|
1246
2243
|
const record = reclassified.kind === 'staged'
|
|
1247
2244
|
? declared
|
|
@@ -1249,12 +2246,10 @@ export function composeRuntime(options) {
|
|
|
1249
2246
|
...declared,
|
|
1250
2247
|
typedSelection: held.confirmed.record.typedSelection,
|
|
1251
2248
|
};
|
|
1252
|
-
const read = reclassified.kind === 'deferred'
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
queue: held.queue,
|
|
1257
|
-
});
|
|
2249
|
+
const read = reclassified.kind === 'deferred'
|
|
2250
|
+
? { kind: 'staged', confirmed: confirmedOf(record, held.confirmed) }
|
|
2251
|
+
: reclassified;
|
|
2252
|
+
await local.write({ next: record, previous: held.confirmed.record });
|
|
1258
2253
|
if (read.kind === 'staged') {
|
|
1259
2254
|
replicas.set(room.locator, replicaOf(read.confirmed, held.queue, held));
|
|
1260
2255
|
if (declarationRefusals.delete(room.locator)) {
|
|
@@ -1281,7 +2276,11 @@ export function composeRuntime(options) {
|
|
|
1281
2276
|
});
|
|
1282
2277
|
}
|
|
1283
2278
|
async function scoped(result, room, local) {
|
|
1284
|
-
scopedRefusals.set(room.locator, {
|
|
2279
|
+
scopedRefusals.set(room.locator, {
|
|
2280
|
+
code: result.code,
|
|
2281
|
+
fields: result.fields,
|
|
2282
|
+
retryable: result.retryable,
|
|
2283
|
+
});
|
|
1285
2284
|
let scopedAway = room;
|
|
1286
2285
|
if (result.scope === 'model')
|
|
1287
2286
|
await retainModel(local, room, 'collection', {
|
|
@@ -1318,22 +2317,27 @@ export function composeRuntime(options) {
|
|
|
1318
2317
|
roomVersion: record.roomVersion,
|
|
1319
2318
|
documents: record.documents,
|
|
1320
2319
|
revisions: record.revisions,
|
|
1321
|
-
attachments: record.attachments,
|
|
1322
2320
|
},
|
|
1323
2321
|
];
|
|
1324
2322
|
});
|
|
1325
2323
|
}
|
|
1326
|
-
async function synchronize(listed, local, progress, attempt) {
|
|
1327
|
-
const
|
|
1328
|
-
const
|
|
2324
|
+
async function synchronize(listed, local, progress, attempt, live) {
|
|
2325
|
+
const acquired = listed.filter(acquires);
|
|
2326
|
+
const prepared = await Promise.allSettled(acquired.map((room) => prepareRoom(room, local, progress)));
|
|
2327
|
+
if (!live())
|
|
2328
|
+
return;
|
|
2329
|
+
const ready = acquired.filter((_, index) => {
|
|
1329
2330
|
const outcome = prepared[index];
|
|
1330
2331
|
return outcome?.status === 'fulfilled' && outcome.value;
|
|
1331
2332
|
});
|
|
1332
|
-
if (attempt === 'opened')
|
|
1333
|
-
options.adapter.roomStream?.hold(heldRooms(ready));
|
|
2333
|
+
if (attempt === 'opened') {
|
|
2334
|
+
options.adapter.roomStream?.hold(heldRooms(ready), heldDemand(acquired));
|
|
2335
|
+
handed = true;
|
|
2336
|
+
}
|
|
2337
|
+
discovering?.release();
|
|
1334
2338
|
const loaded = await Promise.allSettled(ready
|
|
1335
2339
|
.filter((room) => attempt === 'unstreamed' || !replicas.has(room.locator))
|
|
1336
|
-
.map((room) => loadRoom(room, local, progress)));
|
|
2340
|
+
.map((room) => roomTask(room.locator, (held) => loadRoom(room, local, progress, held))));
|
|
1337
2341
|
const failed = [...prepared, ...loaded].find((room) => room.status === 'rejected');
|
|
1338
2342
|
if (failed !== undefined)
|
|
1339
2343
|
throw failed.reason;
|
|
@@ -1362,14 +2366,14 @@ export function composeRuntime(options) {
|
|
|
1362
2366
|
}
|
|
1363
2367
|
return true;
|
|
1364
2368
|
}
|
|
1365
|
-
async function caughtUp(room, local, progress) {
|
|
2369
|
+
async function caughtUp(room, local, progress, live) {
|
|
1366
2370
|
const held = replicas.get(room.locator)?.confirmed.record.roomVersion;
|
|
1367
|
-
if (held === undefined || authorization === undefined)
|
|
2371
|
+
if (held === undefined || authorization === undefined || !live())
|
|
1368
2372
|
return;
|
|
1369
2373
|
const behind = [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later));
|
|
1370
2374
|
if (!behind)
|
|
1371
2375
|
return;
|
|
1372
|
-
const withheld = await advance(room, local, authorization);
|
|
2376
|
+
const withheld = await advance(room, local, authorization, live);
|
|
1373
2377
|
if (withheld !== null)
|
|
1374
2378
|
await withhold(withheld, room, local, progress);
|
|
1375
2379
|
}
|
|
@@ -1379,39 +2383,46 @@ export function composeRuntime(options) {
|
|
|
1379
2383
|
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
1380
2384
|
});
|
|
1381
2385
|
}
|
|
1382
|
-
async function loadRoom(room, local, progress) {
|
|
1383
|
-
if (
|
|
2386
|
+
async function loadRoom(room, local, progress, live) {
|
|
2387
|
+
if (!live() || authorization === undefined)
|
|
1384
2388
|
return;
|
|
1385
2389
|
if (replicas.has(room.locator) && revoked(room.locator)) {
|
|
1386
|
-
await reload(room, local, progress);
|
|
2390
|
+
await reload(room, local, progress, live);
|
|
1387
2391
|
return;
|
|
1388
2392
|
}
|
|
1389
|
-
const withheld = await advance(room, local, authorization);
|
|
2393
|
+
const withheld = await advance(room, local, authorization, live);
|
|
1390
2394
|
if (withheld !== null) {
|
|
1391
2395
|
await withhold(withheld, room, local, progress);
|
|
1392
2396
|
return;
|
|
1393
2397
|
}
|
|
1394
|
-
await caughtUp(room, local, progress);
|
|
2398
|
+
await caughtUp(room, local, progress, live);
|
|
1395
2399
|
}
|
|
1396
|
-
async function reload(room, local, progress) {
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
2400
|
+
async function reload(room, local, progress, live) {
|
|
2401
|
+
try {
|
|
2402
|
+
if (!live() || authorization === undefined)
|
|
2403
|
+
return;
|
|
2404
|
+
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
2405
|
+
if (!live())
|
|
2406
|
+
return;
|
|
2407
|
+
if (bootstrapped.kind !== 'staged') {
|
|
2408
|
+
await withhold(bootstrapped, room, local, progress);
|
|
2409
|
+
return;
|
|
2410
|
+
}
|
|
2411
|
+
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
2412
|
+
await caughtUp(room, local, progress, live);
|
|
2413
|
+
}
|
|
2414
|
+
finally {
|
|
2415
|
+
caughtUpTo(room.locator);
|
|
1405
2416
|
}
|
|
1406
|
-
await publish(local, room, bootstrapped.confirmed);
|
|
1407
|
-
await caughtUp(room, local, progress);
|
|
1408
2417
|
}
|
|
1409
|
-
async function discoverRooms(local, progress, attempt) {
|
|
2418
|
+
async function discoverRooms(local, progress, attempt, live) {
|
|
1410
2419
|
if (closed || authorization === undefined) {
|
|
1411
2420
|
notifyReady();
|
|
1412
2421
|
return;
|
|
1413
2422
|
}
|
|
1414
2423
|
moveTo(transferring(status));
|
|
2424
|
+
discoveries += 1;
|
|
2425
|
+
const started = discoveries;
|
|
1415
2426
|
let result;
|
|
1416
2427
|
try {
|
|
1417
2428
|
result = await options.adapter.discover(authorization);
|
|
@@ -1419,7 +2430,8 @@ export function composeRuntime(options) {
|
|
|
1419
2430
|
catch {
|
|
1420
2431
|
result = { kind: 'transport-uncertainty' };
|
|
1421
2432
|
}
|
|
1422
|
-
if (
|
|
2433
|
+
if (!live()) {
|
|
2434
|
+
progress.complete = false;
|
|
1423
2435
|
notifyReady();
|
|
1424
2436
|
return;
|
|
1425
2437
|
}
|
|
@@ -1433,15 +2445,16 @@ export function composeRuntime(options) {
|
|
|
1433
2445
|
}
|
|
1434
2446
|
progress.reachable = false;
|
|
1435
2447
|
progress.complete = false;
|
|
1436
|
-
if (attempt === 'opened') {
|
|
1437
|
-
options.adapter.roomStream?.hold(heldRooms([...discovered.values()]));
|
|
1438
|
-
}
|
|
1439
2448
|
notifyReady();
|
|
1440
2449
|
return;
|
|
1441
2450
|
}
|
|
1442
2451
|
if (result.kind === 'contract-unavailable') {
|
|
1443
2452
|
journal.record(`connection contract unavailable ${result.code}`);
|
|
1444
|
-
await refuse('missing-required-guarantee', {
|
|
2453
|
+
await refuse('missing-required-guarantee', {
|
|
2454
|
+
code: result.code,
|
|
2455
|
+
fields: result.fields,
|
|
2456
|
+
retryable: result.retryable,
|
|
2457
|
+
});
|
|
1445
2458
|
notifyReady();
|
|
1446
2459
|
return;
|
|
1447
2460
|
}
|
|
@@ -1476,14 +2489,20 @@ export function composeRuntime(options) {
|
|
|
1476
2489
|
}
|
|
1477
2490
|
const previousCapabilities = capabilities;
|
|
1478
2491
|
capabilities = result.capabilities;
|
|
2492
|
+
outcomeRetention = result.retention.outcome;
|
|
1479
2493
|
notifyReady();
|
|
1480
2494
|
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
1481
2495
|
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
1482
2496
|
limits = result.providerLimits;
|
|
1483
2497
|
await local.declare(limits, capabilities);
|
|
1484
2498
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
2499
|
+
const signedIn = result.member ?? null;
|
|
2500
|
+
if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
|
|
2501
|
+
await local.keepMember(signedIn);
|
|
2502
|
+
member = signedIn;
|
|
2503
|
+
emitMember();
|
|
2504
|
+
}
|
|
2505
|
+
await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
|
|
1487
2506
|
}
|
|
1488
2507
|
async function ensureAuthorization() {
|
|
1489
2508
|
if (authorization !== undefined)
|
|
@@ -1497,44 +2516,87 @@ export function composeRuntime(options) {
|
|
|
1497
2516
|
throw error;
|
|
1498
2517
|
}
|
|
1499
2518
|
}
|
|
1500
|
-
function
|
|
2519
|
+
async function admittedRead(work) {
|
|
2520
|
+
if (roomReads.generation !== generation) {
|
|
2521
|
+
roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
2522
|
+
}
|
|
2523
|
+
const reads = roomReads;
|
|
2524
|
+
while (reads.free === 0)
|
|
2525
|
+
await new Promise((admit) => reads.waiting.push(admit));
|
|
2526
|
+
reads.free -= 1;
|
|
2527
|
+
try {
|
|
2528
|
+
await work();
|
|
2529
|
+
}
|
|
2530
|
+
finally {
|
|
2531
|
+
reads.free += 1;
|
|
2532
|
+
reads.waiting.shift()?.();
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
function roomTask(locator, work) {
|
|
2536
|
+
return task(roomLane(locator), (live) => admittedRead(() => work(live)));
|
|
2537
|
+
}
|
|
2538
|
+
function task(lane, work) {
|
|
1501
2539
|
if (stopping)
|
|
1502
|
-
return
|
|
1503
|
-
|
|
1504
|
-
|
|
2540
|
+
return Promise.resolve();
|
|
2541
|
+
const held = lanes.get(lane);
|
|
2542
|
+
const current = held?.generation === generation ? held : { generation, chain: Promise.resolve() };
|
|
2543
|
+
const turn = current.generation;
|
|
2544
|
+
const live = () => turn === generation && continuing();
|
|
2545
|
+
const run = current.chain.then(() => work(live)).catch(() => undefined);
|
|
2546
|
+
current.chain = run;
|
|
2547
|
+
lanes.set(lane, current);
|
|
2548
|
+
working.add(run);
|
|
2549
|
+
void run.finally(() => working.delete(run));
|
|
2550
|
+
return run;
|
|
1505
2551
|
}
|
|
1506
2552
|
function sync(attempt) {
|
|
1507
|
-
return task(async () => {
|
|
1508
|
-
if (
|
|
2553
|
+
return task(OPENING_LANE, async (live) => {
|
|
2554
|
+
if (!live()) {
|
|
1509
2555
|
notifyReady();
|
|
1510
2556
|
return;
|
|
1511
2557
|
}
|
|
1512
2558
|
const progress = { complete: true, reachable: true };
|
|
1513
|
-
|
|
2559
|
+
const gate = discover();
|
|
2560
|
+
if (attempt === 'opened')
|
|
2561
|
+
opened = generation;
|
|
2562
|
+
underway = attempt === 'opened' ? { generation, progress } : null;
|
|
2563
|
+
if (attempt === 'opened')
|
|
2564
|
+
streamed = progress;
|
|
1514
2565
|
complete = false;
|
|
1515
2566
|
recorded = false;
|
|
1516
2567
|
moveTo(transferring(status));
|
|
2568
|
+
handed = false;
|
|
1517
2569
|
try {
|
|
1518
2570
|
const local = await openLocal();
|
|
1519
|
-
if (
|
|
2571
|
+
if (!live())
|
|
1520
2572
|
return;
|
|
1521
2573
|
await ensureAuthorization();
|
|
1522
|
-
if (
|
|
2574
|
+
if (!live())
|
|
1523
2575
|
return;
|
|
1524
|
-
await discoverRooms(local, progress, attempt);
|
|
2576
|
+
await discoverRooms(local, progress, attempt, live);
|
|
1525
2577
|
}
|
|
1526
2578
|
catch {
|
|
1527
2579
|
progress.complete = false;
|
|
1528
|
-
if (attempt === 'opened')
|
|
1529
|
-
|
|
1530
|
-
}
|
|
2580
|
+
if (attempt === 'opened' && !handed)
|
|
2581
|
+
progress.reachable = false;
|
|
1531
2582
|
}
|
|
1532
2583
|
finally {
|
|
2584
|
+
gate.release();
|
|
1533
2585
|
notifyReady();
|
|
1534
2586
|
}
|
|
1535
|
-
if (
|
|
2587
|
+
if (!live())
|
|
2588
|
+
return;
|
|
2589
|
+
if (attempt !== 'opened') {
|
|
2590
|
+
await finishOpening(progress, live);
|
|
1536
2591
|
return;
|
|
1537
|
-
|
|
2592
|
+
}
|
|
2593
|
+
if (handed || closed || progress.reachable)
|
|
2594
|
+
return;
|
|
2595
|
+
underway = null;
|
|
2596
|
+
opened = null;
|
|
2597
|
+
journal.record('connection reopen');
|
|
2598
|
+
await rest(progress, live);
|
|
2599
|
+
options.adapter.roomStream?.reopen();
|
|
1538
2600
|
});
|
|
1539
2601
|
}
|
|
1540
2602
|
function closedSocket(revoked) {
|
|
@@ -1544,29 +2606,51 @@ export function composeRuntime(options) {
|
|
|
1544
2606
|
void refuse('revoked-authorization');
|
|
1545
2607
|
return;
|
|
1546
2608
|
}
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
2609
|
+
complete = false;
|
|
2610
|
+
if (streamed !== null)
|
|
2611
|
+
streamed.complete = false;
|
|
2612
|
+
moveTo({
|
|
2613
|
+
...status,
|
|
2614
|
+
state: status.state === 'synced' ? 'syncing' : status.state,
|
|
2615
|
+
providerUnreachable: status.providerUnreachable || !opened,
|
|
2616
|
+
});
|
|
1550
2617
|
}
|
|
1551
2618
|
function applyOpening(expired) {
|
|
1552
|
-
|
|
1553
|
-
|
|
2619
|
+
const progress = underway?.generation === generation ? underway.progress : null;
|
|
2620
|
+
const cycle = progress ?? { complete: true, reachable: true };
|
|
2621
|
+
const replacing = reloadNow((room) => expired.includes(room.locator), cycle);
|
|
2622
|
+
if (opened === generation)
|
|
2623
|
+
opened = null;
|
|
2624
|
+
return task(OPENING_LANE, async (live) => {
|
|
1554
2625
|
underway = null;
|
|
1555
|
-
if (
|
|
2626
|
+
if (!live())
|
|
1556
2627
|
return;
|
|
1557
2628
|
if (progress === null) {
|
|
1558
|
-
await
|
|
2629
|
+
await replacing;
|
|
2630
|
+
await rest(cycle, live);
|
|
1559
2631
|
return;
|
|
1560
2632
|
}
|
|
2633
|
+
await roomsCarried(expired);
|
|
2634
|
+
if (!live())
|
|
2635
|
+
return;
|
|
2636
|
+
const replaced = new Set(owed);
|
|
2637
|
+
owed.clear();
|
|
1561
2638
|
try {
|
|
1562
2639
|
const local = await openLocal();
|
|
1563
2640
|
const held = [...discovered.values()].filter((room) => replicas.has(room.locator));
|
|
1564
|
-
const reloading = held.filter((room) => expired.includes(room.locator)
|
|
1565
|
-
|
|
2641
|
+
const reloading = held.filter((room) => !expired.includes(room.locator) &&
|
|
2642
|
+
(replaced.has(room.locator) || revoked(room.locator)));
|
|
2643
|
+
for (const room of reloading)
|
|
2644
|
+
catching.add(room.locator);
|
|
2645
|
+
readSide.recover();
|
|
2646
|
+
const reloads = Promise.allSettled(reloading.map((room) => roomTask(room.locator, (current) => reload(room, local, progress, current))));
|
|
2647
|
+
const reloaded = [...(await reloads), ...(await replacing)];
|
|
1566
2648
|
if (progress.reachable) {
|
|
1567
|
-
for (const room of held)
|
|
1568
|
-
if (!reloading.includes(room))
|
|
2649
|
+
for (const room of held) {
|
|
2650
|
+
if (!expired.includes(room.locator) && !reloading.includes(room)) {
|
|
1569
2651
|
await confirmModels(room, local);
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
1570
2654
|
}
|
|
1571
2655
|
const failed = reloaded.find((outcome) => outcome.status === 'rejected');
|
|
1572
2656
|
if (failed !== undefined)
|
|
@@ -1576,21 +2660,21 @@ export function composeRuntime(options) {
|
|
|
1576
2660
|
progress.complete = false;
|
|
1577
2661
|
progress.reachable = false;
|
|
1578
2662
|
}
|
|
1579
|
-
|
|
2663
|
+
if (live())
|
|
2664
|
+
await finishOpening(progress, live);
|
|
1580
2665
|
});
|
|
1581
2666
|
}
|
|
1582
|
-
async function
|
|
1583
|
-
const progress = { complete: true, reachable: true };
|
|
2667
|
+
async function reloadNow(replaced, progress) {
|
|
1584
2668
|
try {
|
|
1585
2669
|
const local = await openLocal();
|
|
1586
|
-
await Promise.allSettled([...discovered.values()]
|
|
1587
|
-
.filter((room) => replicas.has(room.locator) &&
|
|
1588
|
-
.map((room) => reload(room, local, progress)));
|
|
2670
|
+
return await Promise.allSettled([...discovered.values()]
|
|
2671
|
+
.filter((room) => replicas.has(room.locator) && replaced(room))
|
|
2672
|
+
.map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
|
|
1589
2673
|
}
|
|
1590
2674
|
catch {
|
|
1591
2675
|
progress.reachable = false;
|
|
2676
|
+
return [];
|
|
1592
2677
|
}
|
|
1593
|
-
await rest(progress);
|
|
1594
2678
|
}
|
|
1595
2679
|
async function confirmModels(room, local) {
|
|
1596
2680
|
for (const attached of ['collection', 'typed-selection']) {
|
|
@@ -1601,38 +2685,96 @@ export function composeRuntime(options) {
|
|
|
1601
2685
|
}
|
|
1602
2686
|
}
|
|
1603
2687
|
function submitLater(locator) {
|
|
1604
|
-
|
|
1605
|
-
if (running !== undefined) {
|
|
1606
|
-
running.again = true;
|
|
2688
|
+
if (localSaves)
|
|
1607
2689
|
return;
|
|
2690
|
+
void recovery.attempt(`submit ${locator}`, (signal) => submitPass(locator, signal));
|
|
2691
|
+
}
|
|
2692
|
+
async function submitPass(locator, signal) {
|
|
2693
|
+
const room = discovered.get(locator);
|
|
2694
|
+
if (closed || stopping || discoveryPaused || room === undefined)
|
|
2695
|
+
return 'done';
|
|
2696
|
+
const progress = { complete: true, reachable: true };
|
|
2697
|
+
let next;
|
|
2698
|
+
try {
|
|
2699
|
+
await ensureAuthorization();
|
|
2700
|
+
if (authorization === undefined)
|
|
2701
|
+
return 'done';
|
|
2702
|
+
next = await submitRoom(room, await openLocal(), authorization, progress, signal);
|
|
1608
2703
|
}
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
2704
|
+
catch {
|
|
2705
|
+
progress.reachable = false;
|
|
2706
|
+
next = 'again';
|
|
2707
|
+
}
|
|
2708
|
+
if (signal.aborted)
|
|
2709
|
+
return 'done';
|
|
2710
|
+
await rest(progress);
|
|
2711
|
+
return next === 'again' && !socketOpen ? 'done' : next;
|
|
2712
|
+
}
|
|
2713
|
+
function repairLater(locator, when) {
|
|
2714
|
+
if (!socketOpen || repairing.has(locator))
|
|
2715
|
+
return;
|
|
2716
|
+
void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
|
|
2717
|
+
}
|
|
2718
|
+
async function repairPass(locator, signal) {
|
|
2719
|
+
const room = discovered.get(locator);
|
|
2720
|
+
if (!socketOpen || closed || stopping || discoveryPaused || !stale.has(locator))
|
|
2721
|
+
return 'done';
|
|
2722
|
+
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2723
|
+
return 'done';
|
|
2724
|
+
const progress = { complete: true, reachable: true };
|
|
2725
|
+
repairing.add(locator);
|
|
2726
|
+
try {
|
|
2727
|
+
const local = await openLocal();
|
|
2728
|
+
await roomTask(locator, async (live) => {
|
|
2729
|
+
const current = () => live() && !signal.aborted;
|
|
2730
|
+
if (!current() || authorization === undefined)
|
|
2731
|
+
return;
|
|
2732
|
+
const withheld = await Promise.race([
|
|
2733
|
+
advance(room, local, authorization, current),
|
|
2734
|
+
abandonedOn(signal),
|
|
2735
|
+
]);
|
|
2736
|
+
if (withheld !== null && current())
|
|
2737
|
+
await withhold(withheld, room, local, progress);
|
|
2738
|
+
});
|
|
2739
|
+
}
|
|
2740
|
+
catch {
|
|
2741
|
+
stale.add(locator);
|
|
2742
|
+
progress.reachable = false;
|
|
2743
|
+
}
|
|
2744
|
+
finally {
|
|
2745
|
+
repairing.delete(locator);
|
|
2746
|
+
}
|
|
2747
|
+
if (signal.aborted)
|
|
2748
|
+
return 'done';
|
|
2749
|
+
await rest(progress);
|
|
2750
|
+
if (!stale.has(locator)) {
|
|
2751
|
+
if ((replicas.get(locator)?.queue.length ?? 0) > 0)
|
|
2752
|
+
submitLater(locator);
|
|
2753
|
+
return 'done';
|
|
2754
|
+
}
|
|
2755
|
+
if (!repairable.has(locator))
|
|
2756
|
+
return 'done';
|
|
2757
|
+
const reset = repairable.get(locator);
|
|
2758
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
2759
|
+
}
|
|
2760
|
+
function resendLater() {
|
|
2761
|
+
if (!socketOpen)
|
|
2762
|
+
return;
|
|
2763
|
+
void recovery.attempt('resend', resendPass, { again: true });
|
|
2764
|
+
}
|
|
2765
|
+
async function resendPass(signal) {
|
|
2766
|
+
if (!socketOpen || closed || stopping || discoveryPaused)
|
|
2767
|
+
return 'done';
|
|
2768
|
+
const reading = { complete: true, reachable: true };
|
|
2769
|
+
try {
|
|
2770
|
+
await conversations.resend(reading);
|
|
2771
|
+
}
|
|
2772
|
+
catch {
|
|
2773
|
+
reading.reachable = false;
|
|
2774
|
+
}
|
|
2775
|
+
if (signal.aborted)
|
|
2776
|
+
return 'done';
|
|
2777
|
+
return reading.reachable && reading.complete ? 'done' : 'again';
|
|
1636
2778
|
}
|
|
1637
2779
|
function submitAll() {
|
|
1638
2780
|
for (const [locator, replica] of replicas) {
|
|
@@ -1640,31 +2782,41 @@ export function composeRuntime(options) {
|
|
|
1640
2782
|
submitLater(locator);
|
|
1641
2783
|
}
|
|
1642
2784
|
}
|
|
1643
|
-
async function finishOpening(progress) {
|
|
2785
|
+
async function finishOpening(progress, live) {
|
|
1644
2786
|
try {
|
|
1645
|
-
if (
|
|
2787
|
+
if (!live()) {
|
|
1646
2788
|
progress.complete = false;
|
|
1647
2789
|
return;
|
|
1648
2790
|
}
|
|
1649
2791
|
submitAll();
|
|
2792
|
+
for (const locator of stale)
|
|
2793
|
+
if (repairable.has(locator))
|
|
2794
|
+
repairLater(locator, {});
|
|
1650
2795
|
await conversations.reconcile();
|
|
1651
2796
|
if (status.providerUnreachable)
|
|
1652
2797
|
conversations.reconnected();
|
|
1653
2798
|
const reading = { complete: true, reachable: true };
|
|
1654
|
-
|
|
1655
|
-
|
|
2799
|
+
if (demand.conversations)
|
|
2800
|
+
await conversations.catchUp(reading);
|
|
2801
|
+
if (progress.reachable && reading.reachable)
|
|
2802
|
+
await conversations.resend(reading);
|
|
2803
|
+
if (!reading.reachable) {
|
|
1656
2804
|
progress.reachable = false;
|
|
2805
|
+
resendLater();
|
|
2806
|
+
}
|
|
1657
2807
|
}
|
|
1658
2808
|
catch {
|
|
1659
2809
|
progress.complete = false;
|
|
1660
2810
|
}
|
|
1661
2811
|
finally {
|
|
1662
|
-
|
|
1663
|
-
|
|
2812
|
+
if (live()) {
|
|
2813
|
+
complete = progress.complete;
|
|
2814
|
+
await rest(progress, live);
|
|
2815
|
+
}
|
|
1664
2816
|
}
|
|
1665
2817
|
}
|
|
1666
|
-
async function rest(progress) {
|
|
1667
|
-
if (
|
|
2818
|
+
async function rest(progress, live = () => !closed) {
|
|
2819
|
+
if (!live())
|
|
1668
2820
|
return;
|
|
1669
2821
|
let lastCompleted = status.lastCompleted;
|
|
1670
2822
|
let synced = complete && stale.size === 0 && !pending();
|
|
@@ -1672,6 +2824,8 @@ export function composeRuntime(options) {
|
|
|
1672
2824
|
const completedAt = new Date().toISOString();
|
|
1673
2825
|
try {
|
|
1674
2826
|
await (await openLocal()).complete(completedAt);
|
|
2827
|
+
if (!live())
|
|
2828
|
+
return;
|
|
1675
2829
|
recorded = true;
|
|
1676
2830
|
lastCompleted = completedAt;
|
|
1677
2831
|
}
|
|
@@ -1680,46 +2834,74 @@ export function composeRuntime(options) {
|
|
|
1680
2834
|
}
|
|
1681
2835
|
}
|
|
1682
2836
|
moveTo({
|
|
1683
|
-
state: pending() ? 'pending' : synced ? 'synced' : 'syncing',
|
|
2837
|
+
state: pending() ? 'pending' : synced && complete ? 'synced' : 'syncing',
|
|
1684
2838
|
lastCompleted,
|
|
1685
2839
|
providerUnreachable: !progress.reachable,
|
|
1686
2840
|
...attention(),
|
|
1687
2841
|
});
|
|
1688
2842
|
}
|
|
2843
|
+
function discover() {
|
|
2844
|
+
let release = () => { };
|
|
2845
|
+
const done = new Promise((resolve) => {
|
|
2846
|
+
release = resolve;
|
|
2847
|
+
});
|
|
2848
|
+
discovering = { generation, done, release };
|
|
2849
|
+
return discovering;
|
|
2850
|
+
}
|
|
1689
2851
|
function applyRoom(locator, roomVersion) {
|
|
1690
2852
|
announced.set(locator, roomVersion);
|
|
1691
|
-
return task(async () => {
|
|
1692
|
-
|
|
1693
|
-
|
|
2853
|
+
return task(roomLane(locator), async (live) => {
|
|
2854
|
+
if (discovering?.generation === generation)
|
|
2855
|
+
await discovering.done;
|
|
2856
|
+
const saving = answering.get(locator);
|
|
2857
|
+
if (saving?.generation === generation)
|
|
2858
|
+
await saving.answered;
|
|
2859
|
+
if (!live() || authorization === undefined)
|
|
1694
2860
|
return;
|
|
1695
2861
|
const room = discovered.get(locator);
|
|
1696
2862
|
const held = replicas.get(locator);
|
|
1697
|
-
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2863
|
+
if (room === undefined || room.replica.admission !== 'admitted' || !acquires(room))
|
|
1698
2864
|
return;
|
|
1699
2865
|
if (held !== undefined &&
|
|
1700
2866
|
(held.confirmed.record.replica.selection !== room.replica.selection ||
|
|
1701
2867
|
options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion))) {
|
|
1702
2868
|
return;
|
|
1703
2869
|
}
|
|
1704
|
-
const during = underway;
|
|
2870
|
+
const during = underway?.generation === generation ? underway.progress : null;
|
|
1705
2871
|
const progress = during ?? { complete: true, reachable: true };
|
|
1706
2872
|
if (during === null)
|
|
1707
2873
|
moveTo(transferring(status));
|
|
1708
2874
|
try {
|
|
1709
2875
|
const local = await openLocal();
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
2876
|
+
await admittedRead(async () => {
|
|
2877
|
+
if (!live() || authorization === undefined)
|
|
2878
|
+
return;
|
|
2879
|
+
const withheld = await advance(room, local, authorization, live);
|
|
2880
|
+
if (withheld !== null)
|
|
2881
|
+
await withhold(withheld, room, local, progress);
|
|
2882
|
+
});
|
|
1713
2883
|
}
|
|
1714
2884
|
catch {
|
|
1715
2885
|
stale.add(locator);
|
|
1716
2886
|
}
|
|
1717
2887
|
finally {
|
|
1718
2888
|
if (during === null)
|
|
1719
|
-
await rest(progress);
|
|
2889
|
+
await rest(progress, live);
|
|
1720
2890
|
}
|
|
1721
2891
|
});
|
|
1722
2892
|
}
|
|
2893
|
+
function roomsCarried(replacing) {
|
|
2894
|
+
const replaced = new Set(replacing.map(roomLane));
|
|
2895
|
+
return Promise.all([...lanes]
|
|
2896
|
+
.filter(([lane, held]) => lane !== OPENING_LANE && !replaced.has(lane) && held.generation === generation)
|
|
2897
|
+
.map(([, held]) => held.chain));
|
|
2898
|
+
}
|
|
2899
|
+
const receipts = {
|
|
2900
|
+
heads: async () => (await openLocal()).receivedHeads(),
|
|
2901
|
+
parts: async (key, count) => (await openLocal()).receivedParts(key, count),
|
|
2902
|
+
keep: async (key, head, part) => (await openLocal()).keepReceived(key, head, part),
|
|
2903
|
+
drop: async (keys) => (await openLocal()).dropReceived(keys),
|
|
2904
|
+
};
|
|
1723
2905
|
function observeStream() {
|
|
1724
2906
|
const stream = options.adapter.roomStream;
|
|
1725
2907
|
if (stream === undefined || withdrawStream !== undefined)
|
|
@@ -1729,6 +2911,9 @@ export function composeRuntime(options) {
|
|
|
1729
2911
|
return;
|
|
1730
2912
|
if (event.kind === 'opened') {
|
|
1731
2913
|
socketOpen = true;
|
|
2914
|
+
generation += 1;
|
|
2915
|
+
conversations.reopened();
|
|
2916
|
+
recovery.resume();
|
|
1732
2917
|
void sync('opened');
|
|
1733
2918
|
}
|
|
1734
2919
|
else if (event.kind === 'closed')
|
|
@@ -1737,7 +2922,7 @@ export function composeRuntime(options) {
|
|
|
1737
2922
|
void applyOpening(event.expired);
|
|
1738
2923
|
else
|
|
1739
2924
|
void applyRoom(event.locator, event.roomVersion);
|
|
1740
|
-
});
|
|
2925
|
+
}, receipts);
|
|
1741
2926
|
}
|
|
1742
2927
|
function writesHeld() {
|
|
1743
2928
|
return serviceMode === 'write-halted' || haltedRefusal;
|
|
@@ -1752,6 +2937,7 @@ export function composeRuntime(options) {
|
|
|
1752
2937
|
haltedRefusal = false;
|
|
1753
2938
|
if (mode !== serviceMode) {
|
|
1754
2939
|
serviceMode = mode;
|
|
2940
|
+
queueReadiness();
|
|
1755
2941
|
journal.record(`service mode ${mode}`);
|
|
1756
2942
|
for (const listener of serviceModeListeners)
|
|
1757
2943
|
listener(mode);
|
|
@@ -1764,7 +2950,12 @@ export function composeRuntime(options) {
|
|
|
1764
2950
|
if (stream === undefined || withdrawConversations !== undefined)
|
|
1765
2951
|
return;
|
|
1766
2952
|
withdrawConversations = stream.observe((event) => {
|
|
1767
|
-
|
|
2953
|
+
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted')
|
|
2954
|
+
queueReadiness();
|
|
2955
|
+
if (conversations.receive(event)) {
|
|
2956
|
+
void ready.then(scheduleCurrency);
|
|
2957
|
+
return;
|
|
2958
|
+
}
|
|
1768
2959
|
if (event.kind !== 'gap' || conversations.current())
|
|
1769
2960
|
return;
|
|
1770
2961
|
show();
|
|
@@ -1772,46 +2963,47 @@ export function composeRuntime(options) {
|
|
|
1772
2963
|
});
|
|
1773
2964
|
}
|
|
1774
2965
|
function scheduleCurrency() {
|
|
1775
|
-
if (currencyQueued || closed || stopping)
|
|
2966
|
+
if (currencyQueued === generation || closed || stopping)
|
|
1776
2967
|
return;
|
|
1777
|
-
currencyQueued =
|
|
1778
|
-
void task(runCurrency);
|
|
2968
|
+
currencyQueued = generation;
|
|
2969
|
+
void task(CONVERSATION_LANE, runCurrency);
|
|
1779
2970
|
}
|
|
1780
|
-
async function runCurrency() {
|
|
1781
|
-
currencyQueued
|
|
1782
|
-
|
|
2971
|
+
async function runCurrency(live) {
|
|
2972
|
+
if (currencyQueued === generation)
|
|
2973
|
+
currencyQueued = null;
|
|
2974
|
+
if (!live())
|
|
1783
2975
|
return;
|
|
1784
2976
|
await conversations.reconcile();
|
|
1785
|
-
if (conversations.current()) {
|
|
2977
|
+
if (conversations.current() || !demand.conversations) {
|
|
1786
2978
|
show();
|
|
1787
2979
|
return;
|
|
1788
2980
|
}
|
|
1789
|
-
const
|
|
1790
|
-
|
|
1791
|
-
moveTo(transferring(resting));
|
|
2981
|
+
const progress = { complete: false, reachable: !status.providerUnreachable };
|
|
2982
|
+
moveTo(transferring(status));
|
|
1792
2983
|
try {
|
|
1793
2984
|
await conversations.catchUp(progress);
|
|
1794
2985
|
}
|
|
1795
2986
|
catch {
|
|
1796
2987
|
progress.reachable = false;
|
|
1797
2988
|
}
|
|
1798
|
-
|
|
1799
|
-
if (!closed) {
|
|
1800
|
-
moveTo(settled(resting, progress, resting.lastCompleted, pending(), attention()));
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
2989
|
+
await rest(progress, live);
|
|
1803
2990
|
}
|
|
1804
2991
|
function close() {
|
|
1805
2992
|
if (closed)
|
|
1806
2993
|
return;
|
|
1807
2994
|
closed = true;
|
|
2995
|
+
if (narrowing !== null)
|
|
2996
|
+
clearTimeout(narrowing);
|
|
2997
|
+
if (rechecking !== null)
|
|
2998
|
+
clearInterval(rechecking);
|
|
2999
|
+
readinessWatches.clear();
|
|
1808
3000
|
withdrawStream?.();
|
|
1809
3001
|
withdrawStream = undefined;
|
|
1810
3002
|
withdrawConversations?.();
|
|
1811
3003
|
withdrawConversations = undefined;
|
|
1812
3004
|
withdrawServiceMode?.();
|
|
1813
3005
|
conversations.close();
|
|
1814
|
-
|
|
3006
|
+
void recovery.retire();
|
|
1815
3007
|
store?.close();
|
|
1816
3008
|
void opening?.then((opened) => {
|
|
1817
3009
|
opened.close();
|
|
@@ -1821,6 +3013,7 @@ export function composeRuntime(options) {
|
|
|
1821
3013
|
storageLossListeners.clear();
|
|
1822
3014
|
statusListeners.clear();
|
|
1823
3015
|
serviceModeListeners.clear();
|
|
3016
|
+
memberListeners.clear();
|
|
1824
3017
|
readSide.close();
|
|
1825
3018
|
queueSubscriptions.clear();
|
|
1826
3019
|
roomDraftListeners.clear();
|
|
@@ -1828,9 +3021,8 @@ export function composeRuntime(options) {
|
|
|
1828
3021
|
}
|
|
1829
3022
|
async function stop() {
|
|
1830
3023
|
stopping = true;
|
|
1831
|
-
|
|
1832
|
-
await
|
|
1833
|
-
await Promise.all([...submitting.values()].map((run) => run.done));
|
|
3024
|
+
await recovery.retire();
|
|
3025
|
+
await Promise.all(working);
|
|
1834
3026
|
await mutations;
|
|
1835
3027
|
close();
|
|
1836
3028
|
}
|
|
@@ -1852,14 +3044,54 @@ export function composeRuntime(options) {
|
|
|
1852
3044
|
return planOf(input, roomFacts(reach), planReach(reach));
|
|
1853
3045
|
}
|
|
1854
3046
|
function watchPlan(plan, deliver) {
|
|
1855
|
-
return readSide.watch(plan, identityOf(plan),
|
|
3047
|
+
return readSide.watch(plan, identityOf(plan), () => {
|
|
3048
|
+
const cover = covered(plan);
|
|
3049
|
+
awaitPreparation(cover);
|
|
3050
|
+
return cover;
|
|
3051
|
+
}, deliver);
|
|
1856
3052
|
}
|
|
1857
3053
|
async function searched(input, reach) {
|
|
1858
3054
|
const plan = searchPlanOf(input);
|
|
1859
|
-
const
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
3055
|
+
const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
|
|
3056
|
+
for (;;) {
|
|
3057
|
+
const cover = covered(filter);
|
|
3058
|
+
awaitPreparation(cover);
|
|
3059
|
+
const pending = 'pending' in cover ? [...cover.pending] : [];
|
|
3060
|
+
const covering = 'pending' in cover ? [] : cover.coverage.rooms;
|
|
3061
|
+
const unindexed = covering
|
|
3062
|
+
.filter(({ room }) => !searchIndex.rooms().has(room))
|
|
3063
|
+
.map(({ room }) => room);
|
|
3064
|
+
for (const room of unindexed)
|
|
3065
|
+
pending.push({ room, awaits: 'index' });
|
|
3066
|
+
const held = conversations.searchable();
|
|
3067
|
+
const named = reach !== null && !reach.conversations
|
|
3068
|
+
? []
|
|
3069
|
+
: (plan.conversations ?? held).filter((one) => held.includes(one) && conversations.acquired(one));
|
|
3070
|
+
const waiting = named.filter((one) => !searchIndex.conversations().has(one));
|
|
3071
|
+
for (const conversation of waiting)
|
|
3072
|
+
pending.push({ conversation, awaits: 'index' });
|
|
3073
|
+
if (unindexed.length > 0 || waiting.length > 0)
|
|
3074
|
+
index(unindexed, waiting);
|
|
3075
|
+
if (pending.length > 0 || 'pending' in cover)
|
|
3076
|
+
return { pending };
|
|
3077
|
+
const seen = searchIndex.changes();
|
|
3078
|
+
await searchIndex.caughtUp();
|
|
3079
|
+
const selected = plan.documents === undefined && reach === null ? null : await rowsQuery(filter);
|
|
3080
|
+
if (selected !== null && 'pending' in selected)
|
|
3081
|
+
return selected;
|
|
3082
|
+
if (searchIndex.changes() !== seen ||
|
|
3083
|
+
JSON.stringify(covered(filter)) !== JSON.stringify(cover))
|
|
3084
|
+
continue;
|
|
3085
|
+
return {
|
|
3086
|
+
...searchIndex.search(plan, selected === null ? null : selected.documents, named),
|
|
3087
|
+
coverage: {
|
|
3088
|
+
rooms: covering,
|
|
3089
|
+
conversations: named,
|
|
3090
|
+
index: searchIndex.generation(),
|
|
3091
|
+
overlay: cover.coverage.overlay,
|
|
3092
|
+
},
|
|
3093
|
+
};
|
|
3094
|
+
}
|
|
1863
3095
|
}
|
|
1864
3096
|
const watch = (topic, input, deliver) => {
|
|
1865
3097
|
if (topic === 'room-drafts') {
|
|
@@ -1882,6 +3114,10 @@ export function composeRuntime(options) {
|
|
|
1882
3114
|
if (topic === 'conversation') {
|
|
1883
3115
|
return conversations.watch(input[0], input[1], deliver);
|
|
1884
3116
|
}
|
|
3117
|
+
if (topic === 'conversations')
|
|
3118
|
+
return conversations.watchList(deliver);
|
|
3119
|
+
if (topic === 'agents')
|
|
3120
|
+
return conversations.watchAgents(deliver);
|
|
1885
3121
|
if (topic === 'presentation') {
|
|
1886
3122
|
const listener = {
|
|
1887
3123
|
app: input[0],
|
|
@@ -1901,6 +3137,9 @@ export function composeRuntime(options) {
|
|
|
1901
3137
|
presentationListeners.delete(listener);
|
|
1902
3138
|
};
|
|
1903
3139
|
}
|
|
3140
|
+
if (topic === 'readiness') {
|
|
3141
|
+
return watchReadiness({ target: '', code: 'stored', capacity: null }, null, deliver);
|
|
3142
|
+
}
|
|
1904
3143
|
if (topic === 'rooms') {
|
|
1905
3144
|
roomListeners.add(deliver);
|
|
1906
3145
|
deliver(listed());
|
|
@@ -1936,6 +3175,13 @@ export function composeRuntime(options) {
|
|
|
1936
3175
|
serviceModeListeners.delete(deliver);
|
|
1937
3176
|
};
|
|
1938
3177
|
}
|
|
3178
|
+
if (topic === 'member') {
|
|
3179
|
+
memberListeners.add(deliver);
|
|
3180
|
+
deliver(member);
|
|
3181
|
+
return () => {
|
|
3182
|
+
memberListeners.delete(deliver);
|
|
3183
|
+
};
|
|
3184
|
+
}
|
|
1939
3185
|
if (topic === 'queue') {
|
|
1940
3186
|
const queue = { locator: input[0], notify: deliver };
|
|
1941
3187
|
queueSubscriptions.add(queue);
|
|
@@ -1953,7 +3199,7 @@ export function composeRuntime(options) {
|
|
|
1953
3199
|
rooms() {
|
|
1954
3200
|
return listed();
|
|
1955
3201
|
},
|
|
1956
|
-
query: ((input) =>
|
|
3202
|
+
query: ((input) => rowsQuery(planned(input, null))),
|
|
1957
3203
|
search: (input) => searched(input, null),
|
|
1958
3204
|
types(locator) {
|
|
1959
3205
|
return {
|
|
@@ -1982,20 +3228,20 @@ export function composeRuntime(options) {
|
|
|
1982
3228
|
return exclusive(async () => {
|
|
1983
3229
|
const parts = readParts(input);
|
|
1984
3230
|
if (path === '' || normalize(path) !== path)
|
|
1985
|
-
throw
|
|
3231
|
+
throw runtimeError('room-draft-malformed');
|
|
1986
3232
|
const local = await openLocal();
|
|
1987
3233
|
const current = roomDrafts.get(room)?.get(path);
|
|
1988
3234
|
if ((current?.revision ?? null) !== revision) {
|
|
1989
|
-
return
|
|
1990
|
-
outcome: 'stale',
|
|
1991
|
-
current: current === undefined ? null : roomDraftOf(current),
|
|
1992
|
-
};
|
|
3235
|
+
return staleWrite(current === undefined ? null : roomDraftOf(current));
|
|
1993
3236
|
}
|
|
3237
|
+
const file = parts.file === undefined
|
|
3238
|
+
? undefined
|
|
3239
|
+
: await heldFile(parts.file.blob, binaryBase(replicas.get(room)?.visible, path));
|
|
1994
3240
|
const record = {
|
|
1995
3241
|
locator: room,
|
|
1996
3242
|
path,
|
|
1997
3243
|
revision: roomDraftRevision(),
|
|
1998
|
-
parts: held(current?.parts, parts),
|
|
3244
|
+
parts: held(current?.parts, parts, file),
|
|
1999
3245
|
returned: current?.returned ?? null,
|
|
2000
3246
|
};
|
|
2001
3247
|
await local.keepRoomDraft(record);
|
|
@@ -2009,10 +3255,7 @@ export function composeRuntime(options) {
|
|
|
2009
3255
|
const local = await openLocal();
|
|
2010
3256
|
const current = roomDrafts.get(room)?.get(path);
|
|
2011
3257
|
if (current === undefined || current.revision !== revision) {
|
|
2012
|
-
return
|
|
2013
|
-
outcome: 'stale',
|
|
2014
|
-
current: current === undefined ? null : roomDraftOf(current),
|
|
2015
|
-
};
|
|
3258
|
+
return staleWrite(current === undefined ? null : roomDraftOf(current));
|
|
2016
3259
|
}
|
|
2017
3260
|
await local.discardRoomDraft(room, path);
|
|
2018
3261
|
forgetRoomDraft(room, path);
|
|
@@ -2047,6 +3290,10 @@ export function composeRuntime(options) {
|
|
|
2047
3290
|
history(locator, path, query) {
|
|
2048
3291
|
return readHistory(locator, path, query);
|
|
2049
3292
|
},
|
|
3293
|
+
binaryFile: {
|
|
3294
|
+
read: readBinaryPath,
|
|
3295
|
+
readReference: readBinaryReference,
|
|
3296
|
+
},
|
|
2050
3297
|
call(name, input) {
|
|
2051
3298
|
return invokeAction(name, input);
|
|
2052
3299
|
},
|
|
@@ -2059,30 +3306,55 @@ export function composeRuntime(options) {
|
|
|
2059
3306
|
return journal.export();
|
|
2060
3307
|
},
|
|
2061
3308
|
persistence() {
|
|
2062
|
-
return
|
|
3309
|
+
return persistence === 'granted';
|
|
3310
|
+
},
|
|
3311
|
+
async retry() {
|
|
3312
|
+
journal.record('retry');
|
|
3313
|
+
unindexable.clear();
|
|
3314
|
+
queueReadiness();
|
|
3315
|
+
resume();
|
|
2063
3316
|
},
|
|
2064
3317
|
observe: observing(watch),
|
|
2065
3318
|
async reauthorize() {
|
|
2066
3319
|
if (refusal === null || !refusal.reauthorizationOffered)
|
|
2067
3320
|
return;
|
|
2068
3321
|
journal.record('connection reauthorize');
|
|
2069
|
-
await
|
|
3322
|
+
await Promise.all([...lanes.values()]
|
|
3323
|
+
.filter((held) => held.generation === generation)
|
|
3324
|
+
.map((held) => held.chain));
|
|
2070
3325
|
await mutations;
|
|
2071
3326
|
authorization = undefined;
|
|
2072
3327
|
discoveryPaused = false;
|
|
2073
3328
|
if (options.adapter.roomStream === undefined)
|
|
2074
3329
|
await sync('unstreamed');
|
|
2075
3330
|
else
|
|
2076
|
-
await task(ensureAuthorization);
|
|
3331
|
+
await task(OPENING_LANE, ensureAuthorization);
|
|
2077
3332
|
},
|
|
2078
3333
|
};
|
|
2079
3334
|
const reachable = {
|
|
2080
3335
|
md,
|
|
2081
3336
|
watch,
|
|
3337
|
+
referrerScope: (referrer) => options.adapter.referrerScope(referrer),
|
|
2082
3338
|
plan: planned,
|
|
2083
|
-
query: (plan) =>
|
|
3339
|
+
query: (plan) => rowsQuery(plan),
|
|
2084
3340
|
watchPlan,
|
|
2085
3341
|
search: searched,
|
|
3342
|
+
searching,
|
|
3343
|
+
watchReadiness,
|
|
3344
|
+
};
|
|
3345
|
+
return {
|
|
3346
|
+
md,
|
|
3347
|
+
stop,
|
|
3348
|
+
lose: loseRooms,
|
|
3349
|
+
watch,
|
|
3350
|
+
acquire,
|
|
3351
|
+
persisted(answer) {
|
|
3352
|
+
if (answer === persistence)
|
|
3353
|
+
return;
|
|
3354
|
+
persistence = answer;
|
|
3355
|
+
journal.record(`persistence ${answer}`);
|
|
3356
|
+
queueReadiness();
|
|
3357
|
+
},
|
|
3358
|
+
within: (reach, port) => narrowed(reachable, reach, port),
|
|
2086
3359
|
};
|
|
2087
|
-
return { md, stop, watch, within: (reach) => narrowed(reachable, reach) };
|
|
2088
3360
|
}
|