@agentshouse/mdruntime 0.1.0-alpha.1 → 0.1.0-alpha.10
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/dist/adapter.d.ts +142 -29
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js.map +1 -1
- package/dist/bootstrap.d.ts +14 -0
- package/dist/bootstrap.d.ts.map +1 -0
- package/dist/bootstrap.js +55 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/change.d.ts +65 -0
- package/dist/change.d.ts.map +1 -0
- package/dist/change.js +12 -0
- package/dist/change.js.map +1 -0
- package/dist/client.d.ts +63 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +227 -0
- package/dist/client.js.map +1 -0
- package/dist/collection.d.ts +33 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +115 -0
- package/dist/collection.js.map +1 -0
- package/dist/connection.d.ts +85 -7
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1573 -46
- package/dist/connection.js.map +1 -1
- package/dist/conversation.d.ts +57 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +404 -0
- package/dist/conversation.js.map +1 -0
- package/dist/durability.d.ts +9 -0
- package/dist/durability.d.ts.map +1 -0
- package/dist/durability.js +11 -0
- package/dist/durability.js.map +1 -0
- package/dist/file.d.ts +20 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/file.js +149 -0
- package/dist/file.js.map +1 -0
- package/dist/host.d.ts +9 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +200 -0
- package/dist/host.js.map +1 -0
- package/dist/identity.d.ts +2 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +16 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +15 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/log.d.ts +5 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +15 -0
- package/dist/log.js.map +1 -0
- package/dist/memory.d.ts +24 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +300 -37
- package/dist/memory.js.map +1 -1
- package/dist/model.d.ts +70 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +28 -0
- package/dist/model.js.map +1 -0
- package/dist/outcome.d.ts +37 -0
- package/dist/outcome.d.ts.map +1 -0
- package/dist/outcome.js +23 -0
- package/dist/outcome.js.map +1 -0
- package/dist/overlay.d.ts +8 -0
- package/dist/overlay.d.ts.map +1 -0
- package/dist/overlay.js +139 -0
- package/dist/overlay.js.map +1 -0
- package/dist/paths.d.ts +6 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +47 -0
- package/dist/paths.js.map +1 -0
- package/dist/query.d.ts +13 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +18 -0
- package/dist/query.js.map +1 -0
- package/dist/refresh.d.ts +14 -0
- package/dist/refresh.d.ts.map +1 -0
- package/dist/refresh.js +65 -0
- package/dist/refresh.js.map +1 -0
- package/dist/replica.d.ts +23 -0
- package/dist/replica.d.ts.map +1 -0
- package/dist/replica.js +78 -0
- package/dist/replica.js.map +1 -0
- package/dist/save.d.ts +31 -0
- package/dist/save.d.ts.map +1 -0
- package/dist/save.js +537 -0
- package/dist/save.js.map +1 -0
- package/dist/status.d.ts +25 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +32 -0
- package/dist/status.js.map +1 -0
- package/dist/store.d.ts +76 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +191 -0
- package/dist/store.js.map +1 -0
- package/dist/transfer.d.ts +5 -0
- package/dist/transfer.d.ts.map +1 -0
- package/dist/transfer.js +5 -0
- package/dist/transfer.js.map +1 -0
- package/dist/wire.d.ts +63 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +139 -0
- package/dist/wire.js.map +1 -0
- package/package.json +14 -2
package/dist/connection.js
CHANGED
|
@@ -1,43 +1,294 @@
|
|
|
1
|
+
import { rawQuery } from './query.js';
|
|
2
|
+
import { cached, modelTypes, NO_MODEL, sameModel } from './model.js';
|
|
1
3
|
import { ROOM_CONTRACT_VERSION, VERSION_1_CAPABILITIES } from './adapter.js';
|
|
4
|
+
import { bootstrapRoom } from './bootstrap.js';
|
|
5
|
+
import { createConversationStore } from './conversation.js';
|
|
6
|
+
import { MD_MODEL, PRESENTED, validRoot } from './collection.js';
|
|
7
|
+
import { createLog } from './log.js';
|
|
8
|
+
import { queuedOutcome, refusalOf, refused, withheldUntilReset } from './outcome.js';
|
|
9
|
+
import { refreshRoom } from './refresh.js';
|
|
10
|
+
import { buildBatch } from './save.js';
|
|
11
|
+
import { applyReturned, compositionOf, readConfirmed, replicaOf } from './replica.js';
|
|
12
|
+
import { settled, same, saving, STARTING, transferring } from './status.js';
|
|
13
|
+
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
14
|
+
import { beneath } from './paths.js';
|
|
15
|
+
import { openRuntimeStore } from './store.js';
|
|
16
|
+
import { unconfirmed } from './transfer.js';
|
|
2
17
|
const DEFAULT_DISCOVERY_INTERVAL_MS = 60_000;
|
|
3
18
|
const knownCapabilities = new Set(VERSION_1_CAPABILITIES);
|
|
4
|
-
function
|
|
5
|
-
return
|
|
6
|
-
name: room.name,
|
|
7
|
-
locator: room.locator,
|
|
8
|
-
writeGrants: room.writeGrants,
|
|
9
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
10
|
-
synchronizationAvailable: true
|
|
11
|
-
};
|
|
19
|
+
function within(root, path) {
|
|
20
|
+
return root === '' || beneath(root, path);
|
|
12
21
|
}
|
|
13
|
-
function
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
function declaredIdentity(model) {
|
|
23
|
+
if (model === undefined) return false;
|
|
24
|
+
if (model === null) return true;
|
|
25
|
+
return typeof model.identity === 'string' && model.identity.length > 0 && typeof model.version === 'string' && model.version.length > 0;
|
|
26
|
+
}
|
|
27
|
+
function declaredSelection(room) {
|
|
28
|
+
const selection = room.typedSelection;
|
|
29
|
+
if (selection === undefined) return true;
|
|
30
|
+
if (!Array.isArray(selection.roots) || selection.roots.length === 0) return false;
|
|
31
|
+
if (!declaredIdentity(selection.attachedModel)) return false;
|
|
32
|
+
return selection.roots.every((root, index)=>validRoot(root) && root !== '' && !within(room.collectionRoot, root) && selection.roots.every((other, at)=>at === index || !within(other, root)));
|
|
21
33
|
}
|
|
22
34
|
export function createMdRuntime(options) {
|
|
23
35
|
const period = options.discoveryIntervalMs ?? DEFAULT_DISCOVERY_INTERVAL_MS;
|
|
24
36
|
let authorization = options.authorization;
|
|
25
|
-
let
|
|
37
|
+
let opening;
|
|
38
|
+
let store;
|
|
39
|
+
const replicas = new Map();
|
|
40
|
+
const drafts = new Map();
|
|
41
|
+
const presentations = new Map();
|
|
42
|
+
const outcomes = new Map();
|
|
43
|
+
let limits = [];
|
|
44
|
+
let capabilities = [];
|
|
45
|
+
const rooms = new Map();
|
|
46
|
+
const unreadable = new Set();
|
|
47
|
+
const models = new Map();
|
|
48
|
+
const liveModels = new Map();
|
|
49
|
+
const invalidModels = new Set();
|
|
50
|
+
const modelEpoch = new Map();
|
|
51
|
+
const discovered = new Map();
|
|
52
|
+
const scopedRefusals = new Map();
|
|
53
|
+
function modelKey(locator, attached) {
|
|
54
|
+
return `${attached} ${locator}`;
|
|
55
|
+
}
|
|
56
|
+
function modelOf(locator, attached = 'collection') {
|
|
57
|
+
return models.get(modelKey(locator, attached)) ?? NO_MODEL;
|
|
58
|
+
}
|
|
59
|
+
function savedModel(locator, attached) {
|
|
60
|
+
return invalidModels.has(modelKey(locator, attached)) ? {
|
|
61
|
+
status: 'unavailable',
|
|
62
|
+
reason: 'revoked'
|
|
63
|
+
} : modelOf(locator, attached);
|
|
64
|
+
}
|
|
65
|
+
function compositionFor(record) {
|
|
66
|
+
return compositionOf({
|
|
67
|
+
...record,
|
|
68
|
+
model: savedModel(record.locator, 'collection'),
|
|
69
|
+
selectionModel: savedModel(record.locator, 'typed-selection')
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const mismatchedRoots = new Set();
|
|
26
73
|
let refusal = null;
|
|
74
|
+
let storageLoss = null;
|
|
75
|
+
let status = STARTING;
|
|
27
76
|
let discoveryPaused = false;
|
|
28
77
|
let timer;
|
|
29
78
|
let closed = false;
|
|
79
|
+
let withdrawNotifications;
|
|
80
|
+
let handed;
|
|
81
|
+
const notices = new Map();
|
|
30
82
|
let chain = Promise.resolve();
|
|
83
|
+
let mutations = Promise.resolve();
|
|
31
84
|
let inflight = false;
|
|
32
85
|
let queued = false;
|
|
33
86
|
const roomListeners = new Set();
|
|
34
87
|
const refusalListeners = new Set();
|
|
88
|
+
const storageLossListeners = new Set();
|
|
89
|
+
const statusListeners = new Set();
|
|
90
|
+
const querySubscriptions = new Set();
|
|
91
|
+
const queueSubscriptions = new Set();
|
|
92
|
+
const draftListeners = new Set();
|
|
93
|
+
const presentationListeners = new Set();
|
|
94
|
+
let withdrawConversations;
|
|
95
|
+
const conversations = createConversationStore({
|
|
96
|
+
cards: declaredCards,
|
|
97
|
+
documents: ()=>visible({}),
|
|
98
|
+
call: (name, input)=>invokeAction(name, input),
|
|
99
|
+
durably,
|
|
100
|
+
path: ()=>options.adapter.conversations,
|
|
101
|
+
closed: ()=>closed
|
|
102
|
+
});
|
|
103
|
+
const journal = createLog();
|
|
104
|
+
let notifyReady = ()=>{};
|
|
105
|
+
const ready = new Promise((resolve)=>{
|
|
106
|
+
notifyReady = resolve;
|
|
107
|
+
});
|
|
108
|
+
function draftContent(locator, path) {
|
|
109
|
+
return drafts.get(locator)?.get(path);
|
|
110
|
+
}
|
|
111
|
+
function rememberDraft(locator, path, content) {
|
|
112
|
+
const room = drafts.get(locator) ?? new Map();
|
|
113
|
+
room.set(path, content);
|
|
114
|
+
drafts.set(locator, room);
|
|
115
|
+
}
|
|
116
|
+
function forgetDraft(locator, path) {
|
|
117
|
+
const room = drafts.get(locator);
|
|
118
|
+
if (room === undefined) return;
|
|
119
|
+
room.delete(path);
|
|
120
|
+
if (room.size === 0) drafts.delete(locator);
|
|
121
|
+
}
|
|
122
|
+
function snapshotDrafts() {
|
|
123
|
+
const entries = [];
|
|
124
|
+
for (const [locator, room] of drafts){
|
|
125
|
+
for (const [path, content] of room){
|
|
126
|
+
entries.push({
|
|
127
|
+
locator,
|
|
128
|
+
path,
|
|
129
|
+
content: structuredClone(content)
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
entries.sort((left, right)=>{
|
|
134
|
+
if (left.locator !== right.locator) return left.locator < right.locator ? -1 : 1;
|
|
135
|
+
if (left.path !== right.path) return left.path < right.path ? -1 : 1;
|
|
136
|
+
return 0;
|
|
137
|
+
});
|
|
138
|
+
return entries;
|
|
139
|
+
}
|
|
140
|
+
async function readDrafts(opened) {
|
|
141
|
+
await hydrate(opened);
|
|
142
|
+
return snapshotDrafts();
|
|
143
|
+
}
|
|
144
|
+
function publishDrafts(waitingOnly) {
|
|
145
|
+
if (closed) return;
|
|
146
|
+
const value = snapshotDrafts();
|
|
147
|
+
for (const listener of draftListeners){
|
|
148
|
+
if (waitingOnly && listener.ready) continue;
|
|
149
|
+
listener.ready = true;
|
|
150
|
+
listener.notify(value);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function emitDrafts() {
|
|
154
|
+
publishDrafts(false);
|
|
155
|
+
}
|
|
156
|
+
function releaseWaitingDrafts() {
|
|
157
|
+
publishDrafts(true);
|
|
158
|
+
}
|
|
159
|
+
function presentationOf(app) {
|
|
160
|
+
const value = presentations.get(app);
|
|
161
|
+
return value === undefined ? null : structuredClone(value);
|
|
162
|
+
}
|
|
163
|
+
function emitPresentation(app) {
|
|
164
|
+
if (closed) return;
|
|
165
|
+
for (const listener of presentationListeners){
|
|
166
|
+
if (listener.app === app && listener.ready) listener.notify(presentationOf(app));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function commitDraft(work) {
|
|
170
|
+
return exclusive(async ()=>{
|
|
171
|
+
const local = await openLocal();
|
|
172
|
+
await hydrate(local);
|
|
173
|
+
await work(local);
|
|
174
|
+
}).then(()=>{
|
|
175
|
+
emitDrafts();
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function pending() {
|
|
179
|
+
return [
|
|
180
|
+
...replicas.values()
|
|
181
|
+
].some((replica)=>replica.queue.length > 0);
|
|
182
|
+
}
|
|
183
|
+
function attention() {
|
|
184
|
+
const attentionRequired = [];
|
|
185
|
+
let reset = null;
|
|
186
|
+
for (const [locator, replica] of replicas){
|
|
187
|
+
const head = replica.queue[0];
|
|
188
|
+
if (head === undefined) continue;
|
|
189
|
+
const outcome = outcomes.get(head.id);
|
|
190
|
+
if (outcome?.kind === 'retryable-refusal') {
|
|
191
|
+
if (outcome.reset !== undefined && (reset === null || outcome.reset < reset)) {
|
|
192
|
+
reset = outcome.reset;
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (!refused(outcome)) continue;
|
|
197
|
+
attentionRequired.push({
|
|
198
|
+
locator,
|
|
199
|
+
batch: head.id,
|
|
200
|
+
outcome: outcome.kind,
|
|
201
|
+
refusal: refusalOf(outcome),
|
|
202
|
+
...outcome.kind === 'conflicted' && outcome.roomVersion !== undefined ? {
|
|
203
|
+
roomVersion: outcome.roomVersion
|
|
204
|
+
} : {}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
attentionRequired,
|
|
209
|
+
reset
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function storedOutcomes(batches) {
|
|
213
|
+
const stored = {};
|
|
214
|
+
for (const batch of batches){
|
|
215
|
+
const outcome = queuedOutcome(outcomes.get(batch.id));
|
|
216
|
+
if (outcome.kind !== 'pending') stored[batch.id] = outcome;
|
|
217
|
+
}
|
|
218
|
+
return stored;
|
|
219
|
+
}
|
|
220
|
+
async function persistQueue(local, locator, batches) {
|
|
221
|
+
await local.writeQueue({
|
|
222
|
+
locator,
|
|
223
|
+
batches,
|
|
224
|
+
outcomes: storedOutcomes(batches)
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function listed() {
|
|
228
|
+
return structuredClone([
|
|
229
|
+
...rooms.values()
|
|
230
|
+
]);
|
|
231
|
+
}
|
|
232
|
+
function queryRooms() {
|
|
233
|
+
return [
|
|
234
|
+
...replicas.values()
|
|
235
|
+
].map((replica)=>({
|
|
236
|
+
room: replica.confirmed.record.locator,
|
|
237
|
+
roomVersion: replica.confirmed.record.roomVersion,
|
|
238
|
+
documents: replica.visible.collection.documents,
|
|
239
|
+
model: modelTypes(modelOf(replica.confirmed.record.locator)),
|
|
240
|
+
typedSelection: replica.confirmed.record.typedSelection === null ? null : modelTypes(modelOf(replica.confirmed.record.locator, 'typed-selection'))
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
35
243
|
function emitRooms() {
|
|
36
|
-
|
|
244
|
+
const value = listed();
|
|
245
|
+
for (const listener of roomListeners)listener(value);
|
|
37
246
|
}
|
|
38
247
|
function emitRefusal() {
|
|
39
248
|
for (const listener of refusalListeners)listener(refusal);
|
|
40
249
|
}
|
|
250
|
+
function emitStorageLoss() {
|
|
251
|
+
for (const listener of storageLossListeners)listener(storageLoss);
|
|
252
|
+
}
|
|
253
|
+
function moveTo(next) {
|
|
254
|
+
if (closed || same(status, next)) return;
|
|
255
|
+
status = next;
|
|
256
|
+
for (const listener of statusListeners)listener(status);
|
|
257
|
+
}
|
|
258
|
+
function visible(request) {
|
|
259
|
+
if (options.queryBinding === null) {
|
|
260
|
+
if (Object.keys(request).length > 0) throw new Error('query-binding-unavailable');
|
|
261
|
+
return rawQuery(queryRooms());
|
|
262
|
+
}
|
|
263
|
+
return options.queryBinding(queryRooms(), {
|
|
264
|
+
...request,
|
|
265
|
+
selector: {
|
|
266
|
+
$and: [
|
|
267
|
+
PRESENTED,
|
|
268
|
+
request.selector ?? {}
|
|
269
|
+
]
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function emitVisible() {
|
|
274
|
+
for (const subscription of querySubscriptions){
|
|
275
|
+
subscription.notify(visible(subscription.query));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function entriesOf(locator) {
|
|
279
|
+
const replica = replicas.get(locator);
|
|
280
|
+
if (replica === undefined) return [];
|
|
281
|
+
return replica.queue.map((batch)=>Object.freeze({
|
|
282
|
+
batch,
|
|
283
|
+
outcome: queuedOutcome(outcomes.get(batch.id))
|
|
284
|
+
}));
|
|
285
|
+
}
|
|
286
|
+
function emitQueue(locator) {
|
|
287
|
+
const entries = entriesOf(locator);
|
|
288
|
+
for (const subscription of queueSubscriptions){
|
|
289
|
+
if (subscription.locator === locator) subscription.notify(entries);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
41
292
|
function stopTimer() {
|
|
42
293
|
if (timer === undefined) return;
|
|
43
294
|
clearInterval(timer);
|
|
@@ -49,46 +300,1012 @@ export function createMdRuntime(options) {
|
|
|
49
300
|
void enqueue();
|
|
50
301
|
}, period);
|
|
51
302
|
}
|
|
52
|
-
function refuse(reason) {
|
|
303
|
+
function refuse(reason, provider = null) {
|
|
304
|
+
if (reason === 'revoked-authorization') {
|
|
305
|
+
const revoked = {
|
|
306
|
+
status: 'unavailable',
|
|
307
|
+
reason: 'revoked'
|
|
308
|
+
};
|
|
309
|
+
const attachments = [
|
|
310
|
+
'collection',
|
|
311
|
+
'typed-selection'
|
|
312
|
+
];
|
|
313
|
+
const keys = new Set(models.keys());
|
|
314
|
+
for (const locator of rooms.keys()){
|
|
315
|
+
for (const attached of attachments)keys.add(modelKey(locator, attached));
|
|
316
|
+
}
|
|
317
|
+
for (const key of keys){
|
|
318
|
+
invalidModels.add(key);
|
|
319
|
+
modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
320
|
+
models.set(key, revoked);
|
|
321
|
+
liveModels.delete(key);
|
|
322
|
+
}
|
|
323
|
+
for (const [locator, room] of rooms)rooms.set(locator, {
|
|
324
|
+
...room,
|
|
325
|
+
model: modelTypes(revoked),
|
|
326
|
+
typedSelection: room.typedSelection === null ? null : {
|
|
327
|
+
...room.typedSelection,
|
|
328
|
+
model: modelTypes(revoked)
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
emitRooms();
|
|
332
|
+
void exclusive(async ()=>{
|
|
333
|
+
const local = await openLocal();
|
|
334
|
+
for (const record of (await local.all()))await local.publish({
|
|
335
|
+
...record,
|
|
336
|
+
model: revoked,
|
|
337
|
+
selectionModel: revoked
|
|
338
|
+
});
|
|
339
|
+
}).catch(()=>{
|
|
340
|
+
journal.record('model invalidation durable-write failure');
|
|
341
|
+
});
|
|
342
|
+
}
|
|
53
343
|
stopTimer();
|
|
54
344
|
discoveryPaused = true;
|
|
55
345
|
refusal = {
|
|
56
346
|
reason,
|
|
57
|
-
reauthorizationOffered: reason === 'revoked-authorization'
|
|
347
|
+
reauthorizationOffered: reason === 'revoked-authorization',
|
|
348
|
+
refusal: provider
|
|
58
349
|
};
|
|
350
|
+
journal.record(`connection refused ${reason}`);
|
|
59
351
|
emitRefusal();
|
|
60
352
|
}
|
|
61
|
-
function
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
353
|
+
function list(room, synchronizationAvailable) {
|
|
354
|
+
const selection = room.typedSelection ?? null;
|
|
355
|
+
rooms.set(room.locator, {
|
|
356
|
+
name: room.name,
|
|
357
|
+
collectionRoot: room.collectionRoot,
|
|
358
|
+
locator: room.locator,
|
|
359
|
+
operations: room.operations,
|
|
360
|
+
replica: room.replica,
|
|
361
|
+
attachedModel: room.attachedModel,
|
|
362
|
+
model: modelTypes(modelOf(room.locator)),
|
|
363
|
+
typedSelection: selection === null ? null : {
|
|
364
|
+
roots: selection.roots,
|
|
365
|
+
attachedModel: selection.attachedModel,
|
|
366
|
+
model: modelTypes(modelOf(room.locator, 'typed-selection'))
|
|
367
|
+
},
|
|
368
|
+
providerOwnedPaths: room.providerOwnedPaths,
|
|
369
|
+
roomVersion: replicas.get(room.locator)?.confirmed.record.roomVersion ?? null,
|
|
370
|
+
synchronizationAvailable,
|
|
371
|
+
refusal: scopedRefusals.get(room.locator) ?? null
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function applyDiscovery(listed) {
|
|
375
|
+
const listedLocators = new Set(listed.map((room)=>room.locator));
|
|
376
|
+
for (const locator of listedLocators)scopedRefusals.delete(locator);
|
|
377
|
+
discovered.clear();
|
|
378
|
+
for (const room of listed)discovered.set(room.locator, room);
|
|
379
|
+
for (const room of listed){
|
|
380
|
+
const held = replicas.get(room.locator)?.confirmed.record;
|
|
381
|
+
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
382
|
+
list(held, false);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
386
|
+
}
|
|
387
|
+
for (const [locator, room] of rooms){
|
|
388
|
+
if (listedLocators.has(locator)) continue;
|
|
389
|
+
rooms.set(locator, {
|
|
390
|
+
...room,
|
|
391
|
+
synchronizationAvailable: false
|
|
392
|
+
});
|
|
67
393
|
}
|
|
68
|
-
rooms = next;
|
|
69
394
|
emitRooms();
|
|
395
|
+
return listedLocators;
|
|
70
396
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
397
|
+
function restored(key, persisted) {
|
|
398
|
+
const prior = models.get(key);
|
|
399
|
+
if (prior !== undefined && prior.status !== 'unavailable' && (persisted.status === 'unavailable' || !sameModel(prior.snapshot, persisted.snapshot))) {
|
|
400
|
+
modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
401
|
+
invalidModels.add(key);
|
|
402
|
+
}
|
|
403
|
+
const model = persisted.status === 'unavailable' || liveModels.get(key) === JSON.stringify(persisted.snapshot) ? persisted : cached(persisted);
|
|
404
|
+
models.set(key, model);
|
|
405
|
+
if (model.status === 'unavailable') {
|
|
406
|
+
liveModels.delete(key);
|
|
407
|
+
invalidModels.delete(key);
|
|
408
|
+
}
|
|
409
|
+
return model;
|
|
410
|
+
}
|
|
411
|
+
async function hydrate(opened) {
|
|
412
|
+
const [records, queues, kept, presented, cached, unsent, lastCompleted, declared] = await Promise.all([
|
|
413
|
+
opened.all(),
|
|
414
|
+
opened.queues(),
|
|
415
|
+
opened.drafts(),
|
|
416
|
+
opened.presentation(),
|
|
417
|
+
opened.conversations(),
|
|
418
|
+
opened.instructions(),
|
|
419
|
+
opened.lastCompleted(),
|
|
420
|
+
opened.providerLimits()
|
|
421
|
+
]);
|
|
422
|
+
limits = declared;
|
|
423
|
+
drafts.clear();
|
|
424
|
+
for (const draft of kept)rememberDraft(draft.locator, draft.path, draft.content);
|
|
425
|
+
presentations.clear();
|
|
426
|
+
for (const record of presented)presentations.set(record.app, record.value);
|
|
427
|
+
conversations.hydrate(cached, unsent);
|
|
428
|
+
const queuedBatches = new Map(queues.map((queue)=>[
|
|
429
|
+
queue.locator,
|
|
430
|
+
queue.batches
|
|
431
|
+
]));
|
|
432
|
+
for (const queue of queues){
|
|
433
|
+
for (const [id, outcome] of Object.entries(queue.outcomes)){
|
|
434
|
+
outcomes.set(id, outcome);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
replicas.clear();
|
|
438
|
+
for (const record of records){
|
|
439
|
+
const model = restored(modelKey(record.locator, 'collection'), record.model);
|
|
440
|
+
const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
|
|
441
|
+
const confirmed = readConfirmed({
|
|
442
|
+
...record,
|
|
443
|
+
model,
|
|
444
|
+
selectionModel
|
|
445
|
+
});
|
|
446
|
+
if (confirmed === null) continue;
|
|
447
|
+
replicas.set(record.locator, replicaOf(confirmed, queuedBatches.get(record.locator) ?? []));
|
|
448
|
+
const existing = rooms.get(record.locator);
|
|
449
|
+
if (existing === undefined) list(record, false);
|
|
450
|
+
else {
|
|
451
|
+
rooms.set(record.locator, {
|
|
452
|
+
...existing,
|
|
453
|
+
roomVersion: record.roomVersion,
|
|
454
|
+
operations: record.operations,
|
|
455
|
+
replica: record.replica,
|
|
456
|
+
attachedModel: record.attachedModel,
|
|
457
|
+
model: modelTypes(model),
|
|
458
|
+
typedSelection: record.typedSelection === null ? null : {
|
|
459
|
+
roots: record.typedSelection.roots,
|
|
460
|
+
attachedModel: record.typedSelection.attachedModel,
|
|
461
|
+
model: modelTypes(selectionModel)
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
lastCompleted,
|
|
468
|
+
locators: new Set([
|
|
469
|
+
...queuedBatches.keys(),
|
|
470
|
+
...replicas.keys()
|
|
471
|
+
])
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
async function readState(opened) {
|
|
475
|
+
const { lastCompleted, locators } = await hydrate(opened);
|
|
476
|
+
if (replicas.size > 0) {
|
|
477
|
+
emitRooms();
|
|
478
|
+
emitVisible();
|
|
479
|
+
}
|
|
480
|
+
for (const locator of locators)emitQueue(locator);
|
|
481
|
+
emitDrafts();
|
|
482
|
+
return lastCompleted;
|
|
483
|
+
}
|
|
484
|
+
async function open() {
|
|
485
|
+
const opened = await openRuntimeStore();
|
|
486
|
+
store = opened;
|
|
487
|
+
const kept = options.shell.generation;
|
|
488
|
+
const storedGeneration = await opened.generation();
|
|
489
|
+
const loss = detectStorageLoss(storedGeneration, kept);
|
|
490
|
+
const generation = nextGeneration(storedGeneration, kept);
|
|
491
|
+
if (generation !== storedGeneration) await opened.rememberGeneration(generation);
|
|
492
|
+
if (generation !== kept) await options.shell.keepGeneration(generation);
|
|
493
|
+
if (loss !== null) {
|
|
494
|
+
storageLoss = loss;
|
|
495
|
+
emitStorageLoss();
|
|
496
|
+
}
|
|
497
|
+
const lastCompleted = await readState(opened);
|
|
498
|
+
moveTo({
|
|
499
|
+
...status,
|
|
500
|
+
lastCompleted,
|
|
501
|
+
...attention()
|
|
502
|
+
});
|
|
503
|
+
return opened;
|
|
504
|
+
}
|
|
505
|
+
function durably(work) {
|
|
506
|
+
return exclusive(async ()=>{
|
|
507
|
+
const local = await openLocal();
|
|
508
|
+
await hydrate(local);
|
|
509
|
+
return work(local);
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
function openLocal() {
|
|
513
|
+
opening ??= open().catch((error)=>{
|
|
514
|
+
opening = undefined;
|
|
515
|
+
throw error;
|
|
516
|
+
});
|
|
517
|
+
return opening;
|
|
518
|
+
}
|
|
519
|
+
function exclusive(work) {
|
|
520
|
+
const next = mutations.then(work, work);
|
|
521
|
+
mutations = next.then(()=>undefined, ()=>undefined);
|
|
522
|
+
return next;
|
|
523
|
+
}
|
|
524
|
+
function publish(local, room, confirmed) {
|
|
525
|
+
return exclusive(async ()=>{
|
|
526
|
+
await hydrate(local);
|
|
527
|
+
const retained = readConfirmed({
|
|
528
|
+
...confirmed.record,
|
|
529
|
+
model: modelOf(room.locator),
|
|
530
|
+
selectionModel: modelOf(room.locator, 'typed-selection')
|
|
531
|
+
});
|
|
532
|
+
if (retained !== null) {
|
|
533
|
+
await local.publish(retained.record);
|
|
534
|
+
unreadable.delete(room.locator);
|
|
535
|
+
replicas.set(room.locator, replicaOf(retained, replicas.get(room.locator)?.queue ?? []));
|
|
536
|
+
}
|
|
537
|
+
list(room, true);
|
|
538
|
+
emitRooms();
|
|
539
|
+
emitVisible();
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
async function write(locator, changes, replacing) {
|
|
543
|
+
const local = await openLocal();
|
|
544
|
+
await hydrate(local);
|
|
545
|
+
const replica = replicas.get(locator);
|
|
546
|
+
const room = rooms.get(locator);
|
|
547
|
+
if (replica === undefined || room === undefined) {
|
|
548
|
+
throw new Error(`No Local Replica for ${locator}`);
|
|
549
|
+
}
|
|
550
|
+
if (mismatchedRoots.has(locator)) return {
|
|
551
|
+
outcome: 'refused',
|
|
552
|
+
rule: 'collection-root-mismatch'
|
|
553
|
+
};
|
|
554
|
+
const built = buildBatch({
|
|
555
|
+
baseRoomVersion: replica.confirmed.record.roomVersion,
|
|
556
|
+
composition: compositionFor(replica.confirmed.record),
|
|
557
|
+
operations: room.operations,
|
|
558
|
+
binding: options.modelBinding,
|
|
559
|
+
providerOwnedPaths: room.providerOwnedPaths,
|
|
560
|
+
providerLimits: limits,
|
|
561
|
+
visible: replica.visible,
|
|
562
|
+
changes
|
|
563
|
+
});
|
|
564
|
+
if (built.kind === 'refused') return {
|
|
565
|
+
outcome: 'refused',
|
|
566
|
+
...built.refusal
|
|
567
|
+
};
|
|
568
|
+
const head = replica.queue[0];
|
|
569
|
+
if (replacing !== undefined) {
|
|
570
|
+
if (head === undefined || head.id !== replacing || !refused(outcomes.get(head.id))) {
|
|
571
|
+
return {
|
|
572
|
+
outcome: 'refused',
|
|
573
|
+
rule: 'no-refused-head'
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
const batches = replacing === undefined ? [
|
|
578
|
+
...replica.queue,
|
|
579
|
+
built.batch
|
|
580
|
+
] : [
|
|
581
|
+
built.batch,
|
|
582
|
+
...replica.queue.slice(1)
|
|
583
|
+
];
|
|
584
|
+
const consumed = [
|
|
585
|
+
...new Set(changes.map((change)=>change.path))
|
|
586
|
+
];
|
|
587
|
+
try {
|
|
588
|
+
await local.append({
|
|
589
|
+
locator,
|
|
590
|
+
batches,
|
|
591
|
+
outcomes: storedOutcomes(batches)
|
|
592
|
+
}, consumed);
|
|
593
|
+
} catch {
|
|
594
|
+
return {
|
|
595
|
+
outcome: 'refused',
|
|
596
|
+
rule: 'durable-write'
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
if (replacing !== undefined) outcomes.delete(replacing);
|
|
600
|
+
for (const path of consumed)forgetDraft(locator, path);
|
|
601
|
+
replicas.set(locator, replicaOf(replica.confirmed, batches));
|
|
602
|
+
emitQueue(locator);
|
|
603
|
+
emitVisible();
|
|
604
|
+
emitDrafts();
|
|
605
|
+
moveTo({
|
|
606
|
+
...saving(status),
|
|
607
|
+
...attention()
|
|
608
|
+
});
|
|
609
|
+
void enqueue();
|
|
610
|
+
return {
|
|
611
|
+
outcome: 'saved',
|
|
612
|
+
id: built.batch.id
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
async function discardHead(locator, identifier) {
|
|
616
|
+
const local = await openLocal();
|
|
617
|
+
await hydrate(local);
|
|
618
|
+
const replica = replicas.get(locator);
|
|
619
|
+
const head = replica?.queue[0];
|
|
620
|
+
if (replica === undefined || head === undefined || head.id !== identifier || !refused(outcomes.get(head.id))) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const batches = replica.queue.slice(1);
|
|
624
|
+
outcomes.delete(identifier);
|
|
625
|
+
await persistQueue(local, locator, batches);
|
|
626
|
+
replicas.set(locator, replicaOf(replica.confirmed, batches));
|
|
627
|
+
emitQueue(locator);
|
|
628
|
+
emitVisible();
|
|
629
|
+
moveTo({
|
|
630
|
+
...status,
|
|
631
|
+
state: pending() ? 'pending' : status.state,
|
|
632
|
+
...attention()
|
|
633
|
+
});
|
|
634
|
+
void enqueue();
|
|
635
|
+
}
|
|
636
|
+
async function readHistory(locator, path, query = {}) {
|
|
637
|
+
if (closed || refusal !== null) return {
|
|
638
|
+
kind: 'transport-uncertainty'
|
|
639
|
+
};
|
|
640
|
+
try {
|
|
641
|
+
await ensureAuthorization();
|
|
642
|
+
} catch {
|
|
643
|
+
return {
|
|
644
|
+
kind: 'transport-uncertainty'
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
if (closed || authorization === undefined || refusal !== null) {
|
|
648
|
+
return {
|
|
649
|
+
kind: 'transport-uncertainty'
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
const transfer = [
|
|
653
|
+
'transfer',
|
|
654
|
+
'history',
|
|
655
|
+
locator,
|
|
656
|
+
path
|
|
657
|
+
];
|
|
658
|
+
if (query.revision !== undefined) transfer.push(query.revision);
|
|
659
|
+
if (query.pageToken !== undefined) transfer.push(query.pageToken);
|
|
660
|
+
journal.record(transfer.join(' '));
|
|
661
|
+
try {
|
|
662
|
+
const result = await options.adapter.history(authorization, {
|
|
663
|
+
locator,
|
|
664
|
+
path,
|
|
665
|
+
...query.revision === undefined ? {} : {
|
|
666
|
+
revision: query.revision
|
|
667
|
+
},
|
|
668
|
+
...query.pageToken === undefined ? {} : {
|
|
669
|
+
pageToken: query.pageToken
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
journal.record(`outcome history ${result.kind}`);
|
|
673
|
+
if (result.kind === 'scoped-refusal') {
|
|
674
|
+
const room = replicas.get(result.locator)?.confirmed.record;
|
|
675
|
+
if (room !== undefined) await scoped(result, room, await openLocal());
|
|
676
|
+
}
|
|
677
|
+
if (result.kind === 'revoked-authorization') refuse('revoked-authorization');
|
|
678
|
+
return result;
|
|
679
|
+
} catch {
|
|
680
|
+
journal.record('outcome history transport-uncertainty');
|
|
681
|
+
return {
|
|
682
|
+
kind: 'transport-uncertainty'
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
function declaredCards() {
|
|
687
|
+
for (const capability of capabilities){
|
|
688
|
+
if (capability.conversationCards !== undefined) return capability.conversationCards;
|
|
689
|
+
}
|
|
690
|
+
return undefined;
|
|
691
|
+
}
|
|
692
|
+
function declaredAction(name) {
|
|
693
|
+
for (const capability of capabilities){
|
|
694
|
+
for (const action of capability.actions ?? []){
|
|
695
|
+
if (action.name === name) return action;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return undefined;
|
|
699
|
+
}
|
|
700
|
+
async function invokeAction(name, input) {
|
|
701
|
+
await ready;
|
|
702
|
+
if (closed || refusal !== null) return {
|
|
703
|
+
kind: 'transport-uncertainty'
|
|
704
|
+
};
|
|
705
|
+
if (declaredAction(name) === undefined) return {
|
|
706
|
+
kind: 'undeclared'
|
|
707
|
+
};
|
|
708
|
+
try {
|
|
709
|
+
await ensureAuthorization();
|
|
710
|
+
} catch {
|
|
711
|
+
return {
|
|
712
|
+
kind: 'transport-uncertainty'
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
if (closed || authorization === undefined || refusal !== null) {
|
|
716
|
+
return {
|
|
717
|
+
kind: 'transport-uncertainty'
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
journal.record(`transfer action ${name}`);
|
|
721
|
+
try {
|
|
722
|
+
const result = await options.adapter.action(authorization, {
|
|
723
|
+
name,
|
|
724
|
+
input
|
|
725
|
+
});
|
|
726
|
+
if (result.kind === 'scoped-refusal') {
|
|
727
|
+
const room = replicas.get(result.locator)?.confirmed.record;
|
|
728
|
+
if (room !== undefined) await scoped(result, room, await openLocal());
|
|
729
|
+
}
|
|
730
|
+
if (result.kind === 'revoked-authorization') refuse('revoked-authorization');
|
|
731
|
+
journal.record(`outcome action ${result.kind}`);
|
|
732
|
+
if (result.kind === 'rejected') journal.record(`refusal rejected ${result.code}`);
|
|
733
|
+
return result;
|
|
734
|
+
} catch {
|
|
735
|
+
journal.record('outcome action transport-uncertainty');
|
|
736
|
+
return {
|
|
737
|
+
kind: 'transport-uncertainty'
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
async function pullRefresh(authorized, confirmed) {
|
|
742
|
+
try {
|
|
743
|
+
const result = await refreshRoom(options.adapter, authorized, confirmed, options.modelBinding);
|
|
744
|
+
const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
|
|
745
|
+
journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
|
|
746
|
+
return result;
|
|
747
|
+
} catch (error) {
|
|
748
|
+
journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
|
|
749
|
+
throw error;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
async function pullBootstrap(authorized, room) {
|
|
753
|
+
try {
|
|
754
|
+
const result = await bootstrapRoom(options.adapter, authorized, room, {
|
|
755
|
+
collection: modelOf(room.locator),
|
|
756
|
+
selection: modelOf(room.locator, 'typed-selection')
|
|
757
|
+
}, options.modelBinding);
|
|
758
|
+
const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
|
|
759
|
+
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
760
|
+
return result;
|
|
761
|
+
} catch (error) {
|
|
762
|
+
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
763
|
+
throw error;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
async function advance(room, local, authorized) {
|
|
767
|
+
const held = replicas.get(room.locator);
|
|
768
|
+
if (held !== undefined) {
|
|
769
|
+
const refreshed = await pullRefresh(authorized, held.confirmed);
|
|
770
|
+
if (refreshed.kind === 'still-current') {
|
|
771
|
+
if (unreadable.delete(room.locator)) {
|
|
772
|
+
list(room, true);
|
|
773
|
+
emitRooms();
|
|
774
|
+
}
|
|
775
|
+
return null;
|
|
776
|
+
}
|
|
777
|
+
if (refreshed.kind === 'advanced') {
|
|
778
|
+
if (closed) return unconfirmed;
|
|
779
|
+
await publish(local, room, refreshed.confirmed);
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
if (refreshed.kind !== 'beyond-horizon') return refreshed;
|
|
783
|
+
}
|
|
784
|
+
const bootstrapped = await pullBootstrap(authorized, room);
|
|
785
|
+
if (bootstrapped.kind !== 'staged') return bootstrapped;
|
|
786
|
+
if (closed) return unconfirmed;
|
|
787
|
+
await publish(local, room, bootstrapped.confirmed);
|
|
788
|
+
return null;
|
|
789
|
+
}
|
|
790
|
+
function recordRefusal(result) {
|
|
791
|
+
if (result.kind === 'expired') return {
|
|
792
|
+
kind: 'expired'
|
|
793
|
+
};
|
|
794
|
+
if (result.kind === 'retryable-refusal') {
|
|
795
|
+
return {
|
|
796
|
+
kind: 'retryable-refusal',
|
|
797
|
+
code: result.code,
|
|
798
|
+
fields: result.fields,
|
|
799
|
+
...result.reset === undefined ? {} : {
|
|
800
|
+
reset: result.reset
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
if (result.kind === 'conflicted') {
|
|
805
|
+
return {
|
|
806
|
+
kind: 'conflicted',
|
|
807
|
+
...result.roomVersion === undefined ? {} : {
|
|
808
|
+
roomVersion: result.roomVersion
|
|
809
|
+
},
|
|
810
|
+
affected: result.affected,
|
|
811
|
+
code: result.code,
|
|
812
|
+
fields: result.fields
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
return {
|
|
816
|
+
kind: 'rejected',
|
|
817
|
+
affected: result.affected,
|
|
818
|
+
code: result.code,
|
|
819
|
+
fields: result.fields
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
async function commitAccept(room, local, submitted, accepted, base, overlay) {
|
|
823
|
+
await hydrate(local);
|
|
824
|
+
const current = replicas.get(room.locator);
|
|
825
|
+
if (current === undefined || !current.queue.some((batch)=>batch.id === submitted.id)) {
|
|
826
|
+
return true;
|
|
827
|
+
}
|
|
828
|
+
const remaining = current.queue.filter((batch)=>batch.id !== submitted.id);
|
|
829
|
+
const retainedBase = readConfirmed({
|
|
830
|
+
...base.record,
|
|
831
|
+
model: modelOf(room.locator),
|
|
832
|
+
selectionModel: modelOf(room.locator, 'typed-selection')
|
|
833
|
+
});
|
|
834
|
+
if (retainedBase === null) return false;
|
|
835
|
+
const applied = overlay ? applyReturned(retainedBase, accepted.documents, accepted.revisions, accepted.removals, accepted.roomVersion, options.modelBinding) : retainedBase;
|
|
836
|
+
if (applied === null) return false;
|
|
837
|
+
await local.publish(applied.record);
|
|
838
|
+
await persistQueue(local, room.locator, remaining);
|
|
839
|
+
outcomes.set(submitted.id, {
|
|
840
|
+
kind: 'accepted',
|
|
841
|
+
roomVersion: accepted.roomVersion,
|
|
842
|
+
...accepted.author === undefined ? {} : {
|
|
843
|
+
author: accepted.author
|
|
844
|
+
},
|
|
845
|
+
...accepted.time === undefined ? {} : {
|
|
846
|
+
time: accepted.time
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
unreadable.delete(room.locator);
|
|
850
|
+
replicas.set(room.locator, replicaOf(applied, remaining));
|
|
851
|
+
list(room, true);
|
|
852
|
+
emitRooms();
|
|
853
|
+
emitQueue(room.locator);
|
|
854
|
+
emitVisible();
|
|
855
|
+
journal.record(`outcome ${room.locator} ${submitted.id} accepted ${accepted.roomVersion}` + (accepted.author === undefined ? '' : ` author ${accepted.author}`) + (accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
async function submitRoom(room, local, authorized, progress) {
|
|
859
|
+
while(!closed && authorization !== undefined){
|
|
860
|
+
const replica = replicas.get(room.locator);
|
|
861
|
+
const head = replica?.queue[0];
|
|
862
|
+
if (replica === undefined || head === undefined) return null;
|
|
863
|
+
const held = outcomes.get(head.id);
|
|
864
|
+
if (refused(held)) return null;
|
|
865
|
+
if (held?.kind === 'retryable-refusal') {
|
|
866
|
+
if (withheldUntilReset(held, Date.now())) {
|
|
867
|
+
progress.complete = false;
|
|
868
|
+
return null;
|
|
869
|
+
}
|
|
870
|
+
await exclusive(async ()=>{
|
|
871
|
+
await hydrate(local);
|
|
872
|
+
const current = replicas.get(room.locator);
|
|
873
|
+
if (current === undefined || current.queue[0]?.id !== head.id) return;
|
|
874
|
+
outcomes.delete(head.id);
|
|
875
|
+
await persistQueue(local, room.locator, current.queue);
|
|
876
|
+
emitQueue(room.locator);
|
|
877
|
+
moveTo({
|
|
878
|
+
...status,
|
|
879
|
+
...attention()
|
|
880
|
+
});
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
let result;
|
|
884
|
+
journal.record(`submission ${room.locator} ${head.id}`);
|
|
885
|
+
try {
|
|
886
|
+
result = await options.adapter.mutate(authorized, {
|
|
887
|
+
locator: room.locator,
|
|
888
|
+
changeBatch: head
|
|
889
|
+
});
|
|
890
|
+
} catch {
|
|
891
|
+
progress.complete = false;
|
|
892
|
+
progress.reachable = false;
|
|
893
|
+
return null;
|
|
894
|
+
}
|
|
895
|
+
if (closed) return null;
|
|
896
|
+
if (result.kind === 'transport-uncertainty') {
|
|
897
|
+
progress.complete = false;
|
|
898
|
+
progress.reachable = false;
|
|
899
|
+
return null;
|
|
900
|
+
}
|
|
901
|
+
if (result.kind === 'room-not-readable') {
|
|
902
|
+
progress.complete = false;
|
|
903
|
+
unreadable.add(room.locator);
|
|
904
|
+
list(room, false);
|
|
905
|
+
emitRooms();
|
|
906
|
+
return null;
|
|
907
|
+
}
|
|
908
|
+
if (result.kind === 'revoked-authorization') {
|
|
909
|
+
refuse('revoked-authorization');
|
|
910
|
+
return 'stop';
|
|
911
|
+
}
|
|
912
|
+
if (result.kind === 'scoped-refusal') {
|
|
913
|
+
await scoped(result, room, local);
|
|
914
|
+
progress.complete = false;
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired' || result.kind === 'retryable-refusal') {
|
|
918
|
+
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` + (result.kind === 'conflicted' && result.roomVersion !== undefined ? ` ${result.roomVersion}` : ''));
|
|
919
|
+
if (result.kind !== 'expired') journal.record(`refusal ${result.kind} ${result.code}`);
|
|
920
|
+
if (result.kind === 'retryable-refusal') progress.complete = false;
|
|
921
|
+
await exclusive(async ()=>{
|
|
922
|
+
await hydrate(local);
|
|
923
|
+
const current = replicas.get(room.locator);
|
|
924
|
+
if (current === undefined || !current.queue.some((batch)=>batch.id === head.id)) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
outcomes.set(head.id, recordRefusal(result));
|
|
928
|
+
await persistQueue(local, room.locator, current.queue);
|
|
929
|
+
emitQueue(room.locator);
|
|
930
|
+
moveTo({
|
|
931
|
+
...status,
|
|
932
|
+
...attention()
|
|
933
|
+
});
|
|
934
|
+
});
|
|
935
|
+
return null;
|
|
936
|
+
}
|
|
937
|
+
const from = replicas.get(room.locator)?.confirmed;
|
|
938
|
+
if (from === undefined) return null;
|
|
939
|
+
const pulled = await pullRefresh(authorized, from);
|
|
940
|
+
if (pulled.kind === 'revoked-authorization') {
|
|
941
|
+
refuse('revoked-authorization');
|
|
942
|
+
return 'stop';
|
|
943
|
+
}
|
|
944
|
+
if (pulled.kind === 'scoped-refusal') {
|
|
945
|
+
await scoped(pulled, room, local);
|
|
946
|
+
progress.complete = false;
|
|
947
|
+
return null;
|
|
948
|
+
}
|
|
949
|
+
if (pulled.kind === 'still-current') {
|
|
950
|
+
const committed = await exclusive(()=>commitAccept(room, local, head, result, from, true));
|
|
951
|
+
if (!committed) {
|
|
952
|
+
progress.complete = false;
|
|
953
|
+
return null;
|
|
954
|
+
}
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
if (pulled.kind === 'advanced') {
|
|
958
|
+
const overlay = pulled.confirmed.record.roomVersion === result.roomVersion;
|
|
959
|
+
const committed = await exclusive(()=>commitAccept(room, local, head, result, pulled.confirmed, overlay));
|
|
960
|
+
if (!committed) {
|
|
961
|
+
progress.complete = false;
|
|
962
|
+
return null;
|
|
963
|
+
}
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (pulled.kind === 'beyond-horizon') {
|
|
967
|
+
const bootstrapped = await pullBootstrap(authorized, room);
|
|
968
|
+
if (bootstrapped.kind === 'revoked-authorization') {
|
|
969
|
+
refuse('revoked-authorization');
|
|
970
|
+
return 'stop';
|
|
971
|
+
}
|
|
972
|
+
if (bootstrapped.kind === 'scoped-refusal') {
|
|
973
|
+
await scoped(bootstrapped, room, local);
|
|
974
|
+
progress.complete = false;
|
|
975
|
+
return null;
|
|
976
|
+
}
|
|
977
|
+
if (bootstrapped.kind === 'staged') {
|
|
978
|
+
const overlay = bootstrapped.confirmed.record.roomVersion === result.roomVersion;
|
|
979
|
+
const committed = await exclusive(()=>commitAccept(room, local, head, result, bootstrapped.confirmed, overlay));
|
|
980
|
+
if (!committed) {
|
|
981
|
+
progress.complete = false;
|
|
982
|
+
return null;
|
|
983
|
+
}
|
|
984
|
+
continue;
|
|
985
|
+
}
|
|
986
|
+
progress.complete = false;
|
|
987
|
+
if (bootstrapped.kind === 'transport-uncertainty') {
|
|
988
|
+
progress.reachable = false;
|
|
989
|
+
} else if (bootstrapped.kind === 'room-not-readable') {
|
|
990
|
+
unreadable.add(room.locator);
|
|
991
|
+
list(room, false);
|
|
992
|
+
emitRooms();
|
|
993
|
+
}
|
|
994
|
+
return null;
|
|
995
|
+
}
|
|
996
|
+
progress.complete = false;
|
|
997
|
+
if (pulled.kind === 'transport-uncertainty') {
|
|
998
|
+
progress.reachable = false;
|
|
999
|
+
} else if (pulled.kind === 'room-not-readable') {
|
|
1000
|
+
unreadable.add(room.locator);
|
|
1001
|
+
list(room, false);
|
|
1002
|
+
emitRooms();
|
|
1003
|
+
}
|
|
1004
|
+
return null;
|
|
1005
|
+
}
|
|
1006
|
+
return null;
|
|
1007
|
+
}
|
|
1008
|
+
async function retainModel(local, room, attached, next) {
|
|
1009
|
+
const key = modelKey(room.locator, attached);
|
|
1010
|
+
if (next.status === 'unavailable') modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
1011
|
+
const epoch = modelEpoch.get(key) ?? 0;
|
|
1012
|
+
const previous = modelOf(room.locator, attached);
|
|
1013
|
+
const heldBefore = replicas.get(room.locator)?.confirmed.record;
|
|
1014
|
+
if (JSON.stringify(previous) === JSON.stringify(next) && (heldBefore === undefined || JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) && JSON.stringify(heldBefore.replica) === JSON.stringify(room.replica) && JSON.stringify(heldBefore.attachedModel) === JSON.stringify(room.attachedModel) && JSON.stringify(heldBefore.typedSelection) === JSON.stringify(room.typedSelection ?? null))) return;
|
|
1015
|
+
const queryChanged = previous.status === 'unavailable' || next.status === 'unavailable' ? previous.status !== next.status : JSON.stringify(previous.types) !== JSON.stringify(next.types);
|
|
1016
|
+
if (next.status === 'unavailable') {
|
|
1017
|
+
invalidModels.add(key);
|
|
1018
|
+
liveModels.delete(key);
|
|
1019
|
+
}
|
|
1020
|
+
await exclusive(async ()=>{
|
|
1021
|
+
await hydrate(local);
|
|
1022
|
+
if (next.status !== 'unavailable' && epoch !== (modelEpoch.get(key) ?? 0)) return;
|
|
1023
|
+
const held = replicas.get(room.locator);
|
|
1024
|
+
if (held !== undefined) {
|
|
1025
|
+
const record = {
|
|
1026
|
+
...held.confirmed.record,
|
|
1027
|
+
operations: room.operations,
|
|
1028
|
+
replica: room.replica,
|
|
1029
|
+
attachedModel: room.attachedModel,
|
|
1030
|
+
typedSelection: room.typedSelection ?? null,
|
|
1031
|
+
...attached === 'collection' ? {
|
|
1032
|
+
model: next
|
|
1033
|
+
} : {
|
|
1034
|
+
selectionModel: next
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
await local.publish(record);
|
|
1038
|
+
const confirmed = readConfirmed(record);
|
|
1039
|
+
if (confirmed !== null) replicas.set(room.locator, replicaOf(confirmed, held.queue));
|
|
1040
|
+
}
|
|
1041
|
+
models.set(key, next);
|
|
1042
|
+
if (next.status === 'current') {
|
|
1043
|
+
liveModels.set(key, JSON.stringify(next.snapshot));
|
|
1044
|
+
invalidModels.delete(key);
|
|
1045
|
+
}
|
|
1046
|
+
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1047
|
+
emitRooms();
|
|
1048
|
+
if (queryChanged && held !== undefined) emitVisible();
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
async function acquireModel(local, room, authorized, selection) {
|
|
1052
|
+
const attached = selection === null ? 'collection' : 'typed-selection';
|
|
1053
|
+
const key = modelKey(room.locator, attached);
|
|
1054
|
+
const declared = selection === null ? room.attachedModel : selection.attachedModel;
|
|
1055
|
+
let previous = modelOf(room.locator, attached);
|
|
1056
|
+
if (previous.status !== 'unavailable' && (declared === null || !sameModel(previous.snapshot, declared))) {
|
|
1057
|
+
previous = {
|
|
1058
|
+
status: 'unavailable',
|
|
1059
|
+
reason: 'version-changed'
|
|
1060
|
+
};
|
|
1061
|
+
await retainModel(local, room, attached, previous);
|
|
1062
|
+
}
|
|
1063
|
+
const epoch = modelEpoch.get(key) ?? 0;
|
|
1064
|
+
let answer;
|
|
1065
|
+
try {
|
|
1066
|
+
answer = await options.adapter.model(authorized, selection === null ? {
|
|
1067
|
+
locator: room.locator,
|
|
1068
|
+
attached: 'collection',
|
|
1069
|
+
collectionRoot: room.collectionRoot,
|
|
1070
|
+
model: declared
|
|
1071
|
+
} : {
|
|
1072
|
+
locator: room.locator,
|
|
1073
|
+
attached: 'typed-selection',
|
|
1074
|
+
roots: selection.roots,
|
|
1075
|
+
model: declared
|
|
1076
|
+
});
|
|
1077
|
+
} catch {
|
|
1078
|
+
answer = {
|
|
1079
|
+
kind: 'transport-uncertainty'
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
if (epoch !== (modelEpoch.get(key) ?? 0)) return;
|
|
1083
|
+
if (answer.kind === 'revoked-authorization') {
|
|
1084
|
+
await retainModel(local, room, attached, {
|
|
1085
|
+
status: 'unavailable',
|
|
1086
|
+
reason: 'revoked'
|
|
1087
|
+
});
|
|
1088
|
+
refuse('revoked-authorization');
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
if (answer.kind === 'transport-uncertainty') {
|
|
1092
|
+
await retainModel(local, room, attached, cached(previous));
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
if (answer.kind === 'model-unavailable') {
|
|
1096
|
+
await retainModel(local, room, attached, {
|
|
1097
|
+
status: 'unavailable',
|
|
1098
|
+
reason: answer.reason
|
|
1099
|
+
});
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
if (declared === null || !sameModel(answer.snapshot, declared)) {
|
|
1103
|
+
await retainModel(local, room, attached, {
|
|
1104
|
+
status: 'unavailable',
|
|
1105
|
+
reason: 'version-changed'
|
|
1106
|
+
});
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
if (previous.status !== 'unavailable' && sameModel(previous.snapshot, answer.snapshot) && JSON.stringify(Object.entries(previous.snapshot.definitions).sort()) !== JSON.stringify(Object.entries(answer.snapshot.definitions).sort())) {
|
|
1110
|
+
await retainModel(local, room, attached, {
|
|
1111
|
+
status: 'unavailable',
|
|
1112
|
+
reason: 'invalid'
|
|
1113
|
+
});
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
const verified = options.modelBinding?.verify(answer.snapshot);
|
|
1117
|
+
await retainModel(local, room, attached, verified?.kind === 'verified' ? {
|
|
1118
|
+
status: 'current',
|
|
1119
|
+
snapshot: structuredClone(answer.snapshot),
|
|
1120
|
+
verifiedWith: MD_MODEL,
|
|
1121
|
+
types: verified.types
|
|
1122
|
+
} : {
|
|
1123
|
+
status: 'unavailable',
|
|
1124
|
+
reason: verified === undefined ? 'binding-unavailable' : 'invalid'
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
async function scoped(result, room, local) {
|
|
1128
|
+
scopedRefusals.set(room.locator, {
|
|
1129
|
+
code: result.code,
|
|
1130
|
+
fields: result.fields
|
|
1131
|
+
});
|
|
1132
|
+
let scopedAway = room;
|
|
1133
|
+
if (result.scope === 'model') await retainModel(local, room, 'collection', {
|
|
1134
|
+
status: 'unavailable',
|
|
1135
|
+
reason: 'not-readable'
|
|
1136
|
+
});
|
|
1137
|
+
else if (result.scope !== 'operation') {
|
|
1138
|
+
unreadable.add(room.locator);
|
|
1139
|
+
scopedAway = {
|
|
1140
|
+
...room,
|
|
1141
|
+
replica: {
|
|
1142
|
+
...room.replica,
|
|
1143
|
+
admission: 'unavailable'
|
|
1144
|
+
},
|
|
1145
|
+
operations: {
|
|
1146
|
+
...room.operations,
|
|
1147
|
+
...result.scope === 'content' ? {
|
|
1148
|
+
read: []
|
|
1149
|
+
} : {},
|
|
1150
|
+
...result.scope === 'sync' ? {
|
|
1151
|
+
sync: []
|
|
1152
|
+
} : {}
|
|
1153
|
+
}
|
|
1154
|
+
};
|
|
1155
|
+
await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
|
|
1156
|
+
}
|
|
1157
|
+
list(scopedAway, scopedAway.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1158
|
+
emitRooms();
|
|
1159
|
+
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
1160
|
+
}
|
|
1161
|
+
async function synchronize(discovered, local, progress) {
|
|
1162
|
+
for (const room of discovered){
|
|
1163
|
+
if (closed || authorization === undefined) {
|
|
1164
|
+
progress.complete = false;
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
const prior = replicas.get(room.locator)?.confirmed.record;
|
|
1168
|
+
if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
|
|
1169
|
+
list(prior, false);
|
|
1170
|
+
emitRooms();
|
|
1171
|
+
journal.record(`selection changed ${room.locator}`);
|
|
1172
|
+
progress.complete = false;
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
await acquireModel(local, room, authorization, null);
|
|
1176
|
+
if (discoveryPaused) return;
|
|
1177
|
+
if (room.typedSelection !== undefined) {
|
|
1178
|
+
await acquireModel(local, room, authorization, room.typedSelection);
|
|
1179
|
+
if (discoveryPaused) return;
|
|
1180
|
+
}
|
|
1181
|
+
if (room.replica.admission !== 'admitted') {
|
|
1182
|
+
progress.complete = false;
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
const held = replicas.get(room.locator);
|
|
1186
|
+
if (held !== undefined && held.confirmed.record.replica.selection !== room.replica.selection) {
|
|
1187
|
+
progress.complete = false;
|
|
1188
|
+
continue;
|
|
1189
|
+
}
|
|
1190
|
+
const withheld = await advance(room, local, authorization);
|
|
1191
|
+
if (withheld !== null) {
|
|
1192
|
+
progress.complete = false;
|
|
1193
|
+
if (withheld.kind === 'scoped-refusal') {
|
|
1194
|
+
await scoped(withheld, room, local);
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
if (withheld.kind === 'transport-uncertainty') {
|
|
1198
|
+
progress.reachable = false;
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
if (withheld.kind === 'room-not-readable') {
|
|
1202
|
+
unreadable.add(room.locator);
|
|
1203
|
+
list(room, false);
|
|
1204
|
+
emitRooms();
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
if (withheld.kind === 'revoked-authorization') {
|
|
1208
|
+
refuse('revoked-authorization');
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
continue;
|
|
1212
|
+
}
|
|
1213
|
+
if (await submitRoom(room, local, authorization, progress) === 'stop') return;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
async function cycle(local, progress) {
|
|
1217
|
+
if (closed || authorization === undefined) {
|
|
1218
|
+
notifyReady();
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
moveTo(transferring(status));
|
|
1222
|
+
let result;
|
|
1223
|
+
try {
|
|
1224
|
+
result = await options.adapter.discover(authorization);
|
|
1225
|
+
} catch {
|
|
1226
|
+
result = {
|
|
1227
|
+
kind: 'transport-uncertainty'
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
if (closed) {
|
|
1231
|
+
notifyReady();
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
journal.record(`connection discover ${result.kind}`);
|
|
1235
|
+
if (result.kind === 'transport-uncertainty') {
|
|
1236
|
+
for (const room of rooms.values()){
|
|
1237
|
+
await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
|
|
1238
|
+
if (room.typedSelection !== null) {
|
|
1239
|
+
await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
progress.reachable = false;
|
|
1243
|
+
notifyReady();
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
if (result.kind === 'contract-unavailable') {
|
|
1247
|
+
journal.record(`connection contract unavailable ${result.code}`);
|
|
1248
|
+
refuse('missing-required-guarantee', {
|
|
1249
|
+
code: result.code,
|
|
1250
|
+
fields: result.fields
|
|
1251
|
+
});
|
|
1252
|
+
notifyReady();
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
76
1255
|
if (result.kind === 'revoked-authorization') {
|
|
77
1256
|
refuse('revoked-authorization');
|
|
1257
|
+
notifyReady();
|
|
78
1258
|
return;
|
|
79
1259
|
}
|
|
80
1260
|
if (!result.supportedVersions.includes(ROOM_CONTRACT_VERSION)) {
|
|
81
1261
|
refuse('no-matching-version');
|
|
1262
|
+
notifyReady();
|
|
82
1263
|
return;
|
|
83
1264
|
}
|
|
84
|
-
if (result.mdModel.identity !==
|
|
1265
|
+
if (result.mdModel.identity !== MD_MODEL.identity || result.mdModel.version !== MD_MODEL.version) {
|
|
85
1266
|
refuse('md-model-version-mismatch');
|
|
1267
|
+
notifyReady();
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1270
|
+
mismatchedRoots.clear();
|
|
1271
|
+
for (const room of result.rooms){
|
|
1272
|
+
const held = replicas.get(room.locator)?.confirmed.record;
|
|
1273
|
+
const malformed = !validRoot(room.collectionRoot);
|
|
1274
|
+
if (room.operations === undefined || ![
|
|
1275
|
+
'read',
|
|
1276
|
+
'sync',
|
|
1277
|
+
'write',
|
|
1278
|
+
'delete'
|
|
1279
|
+
].every((operation)=>Array.isArray(room.operations[operation]) && room.operations[operation].every((scope)=>typeof scope === 'string')) || room.replica === undefined || typeof room.replica.selection !== 'string' || room.replica.selection.length === 0 || ![
|
|
1280
|
+
'admitted',
|
|
1281
|
+
'unavailable'
|
|
1282
|
+
].includes(room.replica.admission) || !declaredIdentity(room.attachedModel)) {
|
|
1283
|
+
journal.record(`invalid model or replica declaration ${room.locator}`);
|
|
1284
|
+
refuse('missing-required-guarantee');
|
|
1285
|
+
notifyReady();
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
if (!declaredSelection(room)) {
|
|
1289
|
+
journal.record(`invalid typed selection declaration ${room.locator}`);
|
|
1290
|
+
refuse('missing-required-guarantee');
|
|
1291
|
+
notifyReady();
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
if (malformed || held !== undefined && held.collectionRoot !== room.collectionRoot) {
|
|
1295
|
+
mismatchedRoots.add(room.locator);
|
|
1296
|
+
journal.record(`collection root ${malformed ? 'invalid' : 'changed'} ${room.locator}`);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
if (mismatchedRoots.size > 0) {
|
|
1300
|
+
refuse('missing-required-guarantee');
|
|
1301
|
+
notifyReady();
|
|
86
1302
|
return;
|
|
87
1303
|
}
|
|
88
1304
|
const declared = new Set(result.capabilities.map((capability)=>capability.name));
|
|
89
1305
|
for (const required of options.requiredGuarantees){
|
|
90
1306
|
if (!knownCapabilities.has(required) || !declared.has(required)) {
|
|
91
1307
|
refuse('missing-required-guarantee');
|
|
1308
|
+
notifyReady();
|
|
92
1309
|
return;
|
|
93
1310
|
}
|
|
94
1311
|
}
|
|
@@ -96,22 +1313,60 @@ export function createMdRuntime(options) {
|
|
|
96
1313
|
refusal = null;
|
|
97
1314
|
emitRefusal();
|
|
98
1315
|
}
|
|
99
|
-
|
|
1316
|
+
capabilities = result.capabilities;
|
|
1317
|
+
notifyReady();
|
|
1318
|
+
if (declared.has('room-version-notifications')) observeRoomVersions();
|
|
1319
|
+
if (declared.has('provider-actions')) observeConversations();
|
|
1320
|
+
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits)) {
|
|
1321
|
+
limits = result.providerLimits;
|
|
1322
|
+
await local.declare(limits);
|
|
1323
|
+
}
|
|
1324
|
+
const listedLocators = applyDiscovery(result.rooms);
|
|
1325
|
+
handSelected(result.rooms.filter((room)=>room.replica.admission === 'admitted').map((room)=>room.locator).sort());
|
|
1326
|
+
progress.complete = [
|
|
1327
|
+
...replicas.keys()
|
|
1328
|
+
].every((locator)=>listedLocators.has(locator));
|
|
1329
|
+
await synchronize(result.rooms, local, progress);
|
|
100
1330
|
}
|
|
101
1331
|
async function ensureAuthorization() {
|
|
102
1332
|
if (authorization !== undefined) return;
|
|
103
|
-
|
|
1333
|
+
try {
|
|
1334
|
+
authorization = await options.adapter.authorize();
|
|
1335
|
+
journal.record('connection authorize');
|
|
1336
|
+
} catch (error) {
|
|
1337
|
+
journal.record('connection authorize transport-uncertainty');
|
|
1338
|
+
throw error;
|
|
1339
|
+
}
|
|
104
1340
|
}
|
|
105
1341
|
async function connect() {
|
|
106
|
-
if (closed || discoveryPaused)
|
|
1342
|
+
if (closed || discoveryPaused) {
|
|
1343
|
+
notifyReady();
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
startTimer();
|
|
1347
|
+
const progress = {
|
|
1348
|
+
complete: false,
|
|
1349
|
+
reachable: true
|
|
1350
|
+
};
|
|
1351
|
+
let completedAt = new Date().toISOString();
|
|
107
1352
|
try {
|
|
1353
|
+
const local = await openLocal();
|
|
1354
|
+
if (closed) return;
|
|
108
1355
|
await ensureAuthorization();
|
|
109
1356
|
if (closed) return;
|
|
110
|
-
await cycle();
|
|
1357
|
+
await cycle(local, progress);
|
|
1358
|
+
if (closed) return;
|
|
1359
|
+
if (progress.complete) {
|
|
1360
|
+
completedAt = new Date().toISOString();
|
|
1361
|
+
await local.complete(completedAt);
|
|
1362
|
+
}
|
|
111
1363
|
} catch {
|
|
1364
|
+
progress.complete = false;
|
|
1365
|
+
notifyReady();
|
|
112
1366
|
return;
|
|
113
1367
|
} finally{
|
|
114
|
-
|
|
1368
|
+
notifyReady();
|
|
1369
|
+
moveTo(settled(status, progress, completedAt, pending(), attention()));
|
|
115
1370
|
}
|
|
116
1371
|
}
|
|
117
1372
|
async function run() {
|
|
@@ -125,6 +1380,79 @@ export function createMdRuntime(options) {
|
|
|
125
1380
|
}
|
|
126
1381
|
}
|
|
127
1382
|
}
|
|
1383
|
+
function observeRoomVersions() {
|
|
1384
|
+
const path = options.adapter.roomVersions;
|
|
1385
|
+
if (path === undefined || withdrawNotifications !== undefined) return;
|
|
1386
|
+
withdrawNotifications = path.observe((notification)=>{
|
|
1387
|
+
if (closed || discoveryPaused) return;
|
|
1388
|
+
notices.set(notification.locator, notification.roomVersion);
|
|
1389
|
+
chain = chain.then(runNotified, runNotified);
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
function observeConversations() {
|
|
1393
|
+
const path = options.adapter.conversations;
|
|
1394
|
+
if (path === undefined || withdrawConversations !== undefined) return;
|
|
1395
|
+
withdrawConversations = path.observe((event)=>{
|
|
1396
|
+
conversations.receive(event);
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
function handSelected(locators) {
|
|
1400
|
+
const path = options.adapter.roomVersions;
|
|
1401
|
+
if (path === undefined || withdrawNotifications === undefined) return;
|
|
1402
|
+
if (handed !== undefined && JSON.stringify(locators) === JSON.stringify(handed)) return;
|
|
1403
|
+
handed = locators;
|
|
1404
|
+
path.select(locators);
|
|
1405
|
+
}
|
|
1406
|
+
async function runNotified() {
|
|
1407
|
+
const announced = [
|
|
1408
|
+
...notices
|
|
1409
|
+
];
|
|
1410
|
+
notices.clear();
|
|
1411
|
+
if (announced.length === 0 || closed || discoveryPaused) return;
|
|
1412
|
+
const resting = status;
|
|
1413
|
+
const progress = {
|
|
1414
|
+
complete: false,
|
|
1415
|
+
reachable: !resting.providerUnreachable
|
|
1416
|
+
};
|
|
1417
|
+
moveTo(transferring(resting));
|
|
1418
|
+
try {
|
|
1419
|
+
const local = await openLocal();
|
|
1420
|
+
for (const [locator, roomVersion] of announced){
|
|
1421
|
+
if (closed || discoveryPaused || authorization === undefined) return;
|
|
1422
|
+
if (handed?.includes(locator) !== true) continue;
|
|
1423
|
+
const room = discovered.get(locator);
|
|
1424
|
+
const held = replicas.get(locator);
|
|
1425
|
+
if (room === undefined || held === undefined) continue;
|
|
1426
|
+
if (held.confirmed.record.roomVersion === roomVersion) continue;
|
|
1427
|
+
const withheld = await advance(room, local, authorization);
|
|
1428
|
+
if (withheld === null) continue;
|
|
1429
|
+
if (withheld.kind === 'scoped-refusal') {
|
|
1430
|
+
await scoped(withheld, room, local);
|
|
1431
|
+
continue;
|
|
1432
|
+
}
|
|
1433
|
+
if (withheld.kind === 'transport-uncertainty') {
|
|
1434
|
+
progress.reachable = false;
|
|
1435
|
+
continue;
|
|
1436
|
+
}
|
|
1437
|
+
if (withheld.kind === 'room-not-readable') {
|
|
1438
|
+
unreadable.add(locator);
|
|
1439
|
+
list(room, false);
|
|
1440
|
+
emitRooms();
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
if (withheld.kind === 'revoked-authorization') {
|
|
1444
|
+
refuse('revoked-authorization');
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
} catch {
|
|
1449
|
+
progress.reachable = false;
|
|
1450
|
+
} finally{
|
|
1451
|
+
if (!closed) {
|
|
1452
|
+
moveTo(settled(resting, progress, resting.lastCompleted, pending(), attention()));
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
128
1456
|
function enqueue() {
|
|
129
1457
|
if (inflight) {
|
|
130
1458
|
queued = true;
|
|
@@ -136,37 +1464,236 @@ export function createMdRuntime(options) {
|
|
|
136
1464
|
}
|
|
137
1465
|
function close() {
|
|
138
1466
|
closed = true;
|
|
1467
|
+
withdrawNotifications?.();
|
|
1468
|
+
withdrawNotifications = undefined;
|
|
1469
|
+
withdrawConversations?.();
|
|
1470
|
+
withdrawConversations = undefined;
|
|
1471
|
+
conversations.close();
|
|
139
1472
|
stopTimer();
|
|
1473
|
+
store?.close();
|
|
1474
|
+
void opening?.then((opened)=>{
|
|
1475
|
+
opened.close();
|
|
1476
|
+
}, ()=>{});
|
|
140
1477
|
roomListeners.clear();
|
|
141
1478
|
refusalListeners.clear();
|
|
1479
|
+
storageLossListeners.clear();
|
|
1480
|
+
statusListeners.clear();
|
|
1481
|
+
querySubscriptions.clear();
|
|
1482
|
+
queueSubscriptions.clear();
|
|
1483
|
+
draftListeners.clear();
|
|
1484
|
+
presentationListeners.clear();
|
|
142
1485
|
}
|
|
143
1486
|
options.signal?.addEventListener('abort', close, {
|
|
144
1487
|
once: true
|
|
145
1488
|
});
|
|
146
1489
|
if (options.signal?.aborted) close();
|
|
1490
|
+
void openLocal().catch(()=>{
|
|
1491
|
+
notifyReady();
|
|
1492
|
+
});
|
|
147
1493
|
void enqueue();
|
|
148
1494
|
return {
|
|
149
1495
|
rooms () {
|
|
150
|
-
return
|
|
1496
|
+
return listed();
|
|
1497
|
+
},
|
|
1498
|
+
query (request = {}) {
|
|
1499
|
+
return visible(request);
|
|
1500
|
+
},
|
|
1501
|
+
types (locator) {
|
|
1502
|
+
return {
|
|
1503
|
+
collection: modelTypes(modelOf(locator)),
|
|
1504
|
+
typedSelection: rooms.get(locator)?.typedSelection === undefined || rooms.get(locator)?.typedSelection === null ? null : modelTypes(modelOf(locator, 'typed-selection'))
|
|
1505
|
+
};
|
|
1506
|
+
},
|
|
1507
|
+
syncStatus () {
|
|
1508
|
+
return status;
|
|
1509
|
+
},
|
|
1510
|
+
draft: {
|
|
1511
|
+
list () {
|
|
1512
|
+
return exclusive(async ()=>{
|
|
1513
|
+
const local = await openLocal();
|
|
1514
|
+
return readDrafts(local);
|
|
1515
|
+
}).then((entries)=>{
|
|
1516
|
+
releaseWaitingDrafts();
|
|
1517
|
+
return entries;
|
|
1518
|
+
});
|
|
1519
|
+
},
|
|
1520
|
+
read (locator, path) {
|
|
1521
|
+
const content = draftContent(locator, path);
|
|
1522
|
+
return content === undefined ? null : structuredClone(content);
|
|
1523
|
+
},
|
|
1524
|
+
replace (locator, path, content) {
|
|
1525
|
+
return commitDraft(async (local)=>{
|
|
1526
|
+
const stored = structuredClone(content);
|
|
1527
|
+
await local.keep({
|
|
1528
|
+
locator,
|
|
1529
|
+
path,
|
|
1530
|
+
content: stored
|
|
1531
|
+
});
|
|
1532
|
+
rememberDraft(locator, path, stored);
|
|
1533
|
+
});
|
|
1534
|
+
},
|
|
1535
|
+
discard (locator, path) {
|
|
1536
|
+
return commitDraft(async (local)=>{
|
|
1537
|
+
await local.discard(locator, path);
|
|
1538
|
+
forgetDraft(locator, path);
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
},
|
|
1542
|
+
conversation: conversations.surface,
|
|
1543
|
+
presentation: {
|
|
1544
|
+
read (app) {
|
|
1545
|
+
return exclusive(async ()=>{
|
|
1546
|
+
await hydrate(await openLocal());
|
|
1547
|
+
return presentationOf(app);
|
|
1548
|
+
});
|
|
1549
|
+
},
|
|
1550
|
+
write (app, value) {
|
|
1551
|
+
return exclusive(async ()=>{
|
|
1552
|
+
const local = await openLocal();
|
|
1553
|
+
await hydrate(local);
|
|
1554
|
+
const stored = structuredClone(value);
|
|
1555
|
+
await local.present({
|
|
1556
|
+
app,
|
|
1557
|
+
value: stored
|
|
1558
|
+
});
|
|
1559
|
+
presentations.set(app, stored);
|
|
1560
|
+
}).then(()=>{
|
|
1561
|
+
emitPresentation(app);
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
},
|
|
1565
|
+
save (locator, changes, replacing) {
|
|
1566
|
+
return exclusive(()=>write(locator, changes, replacing));
|
|
1567
|
+
},
|
|
1568
|
+
outcome (identifier) {
|
|
1569
|
+
const recorded = outcomes.get(identifier);
|
|
1570
|
+
if (recorded !== undefined) return recorded;
|
|
1571
|
+
for (const replica of replicas.values()){
|
|
1572
|
+
if (replica.queue.some((batch)=>batch.id === identifier)) return {
|
|
1573
|
+
kind: 'pending'
|
|
1574
|
+
};
|
|
1575
|
+
}
|
|
1576
|
+
return null;
|
|
1577
|
+
},
|
|
1578
|
+
queue: {
|
|
1579
|
+
discard (locator, identifier) {
|
|
1580
|
+
return exclusive(()=>discardHead(locator, identifier));
|
|
1581
|
+
}
|
|
1582
|
+
},
|
|
1583
|
+
history (locator, path, query) {
|
|
1584
|
+
return readHistory(locator, path, query);
|
|
1585
|
+
},
|
|
1586
|
+
call (name, input) {
|
|
1587
|
+
return invokeAction(name, input);
|
|
151
1588
|
},
|
|
152
|
-
|
|
1589
|
+
log () {
|
|
1590
|
+
return journal.export();
|
|
1591
|
+
},
|
|
1592
|
+
storageLoss () {
|
|
1593
|
+
return storageLoss;
|
|
1594
|
+
},
|
|
1595
|
+
persistence () {
|
|
1596
|
+
return options.shell.persistentStorage;
|
|
1597
|
+
},
|
|
1598
|
+
observe (topic, second, third) {
|
|
1599
|
+
if (topic === 'drafts') {
|
|
1600
|
+
const notify = second;
|
|
1601
|
+
const listener = {
|
|
1602
|
+
notify,
|
|
1603
|
+
ready: false
|
|
1604
|
+
};
|
|
1605
|
+
draftListeners.add(listener);
|
|
1606
|
+
void exclusive(async ()=>{
|
|
1607
|
+
const local = await openLocal();
|
|
1608
|
+
await readDrafts(local);
|
|
1609
|
+
if (closed || !draftListeners.has(listener)) return;
|
|
1610
|
+
listener.ready = true;
|
|
1611
|
+
}).then(()=>{
|
|
1612
|
+
if (closed || !listener.ready || !draftListeners.has(listener)) return;
|
|
1613
|
+
listener.notify(snapshotDrafts());
|
|
1614
|
+
}, ()=>{});
|
|
1615
|
+
return ()=>{
|
|
1616
|
+
draftListeners.delete(listener);
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
if (topic === 'conversation') {
|
|
1620
|
+
return conversations.observe(second, third);
|
|
1621
|
+
}
|
|
1622
|
+
if (topic === 'presentation') {
|
|
1623
|
+
const listener = {
|
|
1624
|
+
app: second,
|
|
1625
|
+
notify: third,
|
|
1626
|
+
ready: false
|
|
1627
|
+
};
|
|
1628
|
+
presentationListeners.add(listener);
|
|
1629
|
+
void exclusive(async ()=>{
|
|
1630
|
+
await hydrate(await openLocal());
|
|
1631
|
+
}).then(()=>{
|
|
1632
|
+
if (closed || !presentationListeners.has(listener)) return;
|
|
1633
|
+
listener.ready = true;
|
|
1634
|
+
listener.notify(presentationOf(listener.app));
|
|
1635
|
+
}, ()=>{});
|
|
1636
|
+
return ()=>{
|
|
1637
|
+
presentationListeners.delete(listener);
|
|
1638
|
+
};
|
|
1639
|
+
}
|
|
153
1640
|
if (topic === 'rooms') {
|
|
154
|
-
const notify =
|
|
1641
|
+
const notify = second;
|
|
155
1642
|
roomListeners.add(notify);
|
|
156
|
-
notify(
|
|
1643
|
+
notify(listed());
|
|
157
1644
|
return ()=>{
|
|
158
1645
|
roomListeners.delete(notify);
|
|
159
1646
|
};
|
|
160
1647
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
1648
|
+
if (topic === 'connection-refusal') {
|
|
1649
|
+
const notify = second;
|
|
1650
|
+
refusalListeners.add(notify);
|
|
1651
|
+
notify(refusal);
|
|
1652
|
+
return ()=>{
|
|
1653
|
+
refusalListeners.delete(notify);
|
|
1654
|
+
};
|
|
1655
|
+
}
|
|
1656
|
+
if (topic === 'storage-loss') {
|
|
1657
|
+
const notify = second;
|
|
1658
|
+
storageLossListeners.add(notify);
|
|
1659
|
+
notify(storageLoss);
|
|
1660
|
+
return ()=>{
|
|
1661
|
+
storageLossListeners.delete(notify);
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
if (topic === 'sync-status') {
|
|
1665
|
+
const notify = second;
|
|
1666
|
+
statusListeners.add(notify);
|
|
1667
|
+
notify(status);
|
|
1668
|
+
return ()=>{
|
|
1669
|
+
statusListeners.delete(notify);
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
if (topic === 'queue') {
|
|
1673
|
+
const queue = {
|
|
1674
|
+
locator: second,
|
|
1675
|
+
notify: third
|
|
1676
|
+
};
|
|
1677
|
+
queueSubscriptions.add(queue);
|
|
1678
|
+
queue.notify(entriesOf(queue.locator));
|
|
1679
|
+
return ()=>{
|
|
1680
|
+
queueSubscriptions.delete(queue);
|
|
1681
|
+
};
|
|
1682
|
+
}
|
|
1683
|
+
const subscription = {
|
|
1684
|
+
query: second,
|
|
1685
|
+
notify: third
|
|
1686
|
+
};
|
|
1687
|
+
const first = visible(subscription.query);
|
|
1688
|
+
querySubscriptions.add(subscription);
|
|
1689
|
+
subscription.notify(first);
|
|
164
1690
|
return ()=>{
|
|
165
|
-
|
|
1691
|
+
querySubscriptions.delete(subscription);
|
|
166
1692
|
};
|
|
167
1693
|
},
|
|
168
1694
|
async reauthorize () {
|
|
169
1695
|
if (refusal === null || !refusal.reauthorizationOffered) return;
|
|
1696
|
+
journal.record('connection reauthorize');
|
|
170
1697
|
authorization = undefined;
|
|
171
1698
|
discoveryPaused = false;
|
|
172
1699
|
await enqueue();
|