@agentshouse/mdruntime 0.1.0-c8f3163f32f93 → 0.1.0-c907ac1fbed44
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 +289 -276
- package/README.md +16 -14
- package/dist/adapter.d.ts +76 -35
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +4 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +13 -3
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +26 -7
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +148 -78
- package/dist/chain.d.ts +11 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -63
- package/dist/change.d.ts +31 -1
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +10 -1
- package/dist/client.d.ts +23 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +48 -8
- package/dist/collection.d.ts +7 -6
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +31 -77
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +721 -0
- package/dist/connection.d.ts +41 -15
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +3058 -638
- package/dist/conversation.d.ts +26 -7
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +441 -161
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +5 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +5 -3
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -23
- package/dist/file.d.ts +2 -10
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -120
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +368 -0
- package/dist/host.d.ts +13 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +120 -13
- package/dist/identity.d.ts +1 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +3 -0
- package/dist/index.d.ts +15 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +123 -0
- package/dist/memory.d.ts +8 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +43 -20
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +90 -0
- package/dist/model.d.ts +4 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +10 -1
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +19 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +113 -12
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +34 -4
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +25 -0
- package/dist/overlay.d.ts +3 -4
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +99 -189
- package/dist/query.d.ts +60 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +48 -7
- package/dist/reach.d.ts +11 -6
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +181 -61
- package/dist/react.d.ts +8 -6
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/readiness.d.ts +64 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +100 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +198 -0
- package/dist/refresh.d.ts +16 -15
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +112 -44
- package/dist/replica.d.ts +57 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +295 -149
- package/dist/room-draft.d.ts +17 -2
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +65 -25
- package/dist/rows.d.ts +28 -37
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +113 -72
- package/dist/save.d.ts +51 -9
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +526 -165
- package/dist/search.d.ts +39 -28
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +37 -105
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +16 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +431 -0
- package/dist/status.d.ts +4 -0
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +103 -78
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +336 -556
- package/dist/stored.d.ts +145 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +567 -0
- package/dist/tables.d.ts +354 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +624 -0
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +162 -0
- package/dist/windows.d.ts +22 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +89 -0
- package/dist/wire.d.ts +164 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +55 -31
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +150 -0
- package/package.json +14 -15
- package/dist/chat/acts.d.ts +0 -33
- package/dist/chat/acts.d.ts.map +0 -1
- package/dist/chat/acts.js +0 -36
- package/dist/chat/context.d.ts +0 -3
- package/dist/chat/context.d.ts.map +0 -1
- package/dist/chat/context.js +0 -10
- package/dist/chat/input.d.ts +0 -9
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -99
- package/dist/chat/kit.d.ts +0 -60
- package/dist/chat/kit.d.ts.map +0 -1
- package/dist/chat/kit.js +0 -123
- package/dist/chat/lasting.d.ts +0 -6
- package/dist/chat/lasting.d.ts.map +0 -1
- package/dist/chat/lasting.js +0 -19
- package/dist/chat/list.d.ts +0 -26
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -62
- package/dist/chat/markdown.d.ts +0 -20
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -188
- package/dist/chat/message.d.ts +0 -6
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -192
- package/dist/chat/messages.d.ts +0 -21
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -151
- package/dist/chat/name.d.ts +0 -6
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -11
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -17
- package/dist/chat/overlay.d.ts +0 -31
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -97
- package/dist/chat/parts.d.ts +0 -57
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -119
- package/dist/chat/process.d.ts +0 -20
- package/dist/chat/process.d.ts.map +0 -1
- package/dist/chat/process.js +0 -32
- package/dist/chat/provider.d.ts +0 -63
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -620
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -80
- package/dist/chat/questions.d.ts +0 -31
- package/dist/chat/questions.d.ts.map +0 -1
- package/dist/chat/questions.js +0 -111
- package/dist/chat/read.d.ts +0 -5
- package/dist/chat/read.d.ts.map +0 -1
- package/dist/chat/read.js +0 -13
- package/dist/chat/ready.d.ts +0 -33
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -91
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -302
- package/dist/chat.d.ts +0 -14
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -440
package/dist/connection.js
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { escapePointer, parseFrontmatter, roomTarget, target } from '@agentshouse/mdmodel';
|
|
2
|
+
import { attachedModel, NO_ROOM_TYPES, roomTypesOf } from './attached-model.js';
|
|
2
3
|
import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
|
|
3
|
-
import { observing } from './observation.js';
|
|
4
|
-
import { filterOf, identityOf, planOf, } from './query.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { observing, whole } from './observation.js';
|
|
5
|
+
import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
|
|
6
|
+
import { createLiveQueries } from './live.js';
|
|
7
|
+
import { seededResult } from './seed.js';
|
|
8
|
+
import { documentKey, roomOfKey } from './rows.js';
|
|
9
|
+
import { searchPlanOf } from './search.js';
|
|
10
|
+
import { capped, MAXIMUM_WINDOW } from './windows.js';
|
|
8
11
|
import { bootstrapRoom } from './bootstrap.js';
|
|
9
12
|
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
10
13
|
import { createConversationStore, } from './conversation.js';
|
|
11
|
-
import { queuedFile } from './change.js';
|
|
12
|
-
import { validRoot } from './collection.js';
|
|
14
|
+
import { leaving as leavingIn, partnerOf, queuedFile, } from './change.js';
|
|
15
|
+
import { available, collectionOf, unreadable as unreadableIn, validRoot, } from './collection.js';
|
|
13
16
|
import { runtimeError } from './failure.js';
|
|
14
17
|
import { createLog } from './log.js';
|
|
15
18
|
import { binaryRecord } from './binary.js';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
19
|
+
import { openedFiles } from './opened.js';
|
|
20
|
+
import { ABSENT_TARGET, absentBinary, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
|
|
21
|
+
import { createRecovery } from './recovery.js';
|
|
22
|
+
import { doorReads } from './reads.js';
|
|
23
|
+
import { advancement } from './refresh.js';
|
|
24
|
+
import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
|
|
25
|
+
import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, sameMembership, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
|
|
26
|
+
import { binaryBase, changedParts, changesOf, gatheredParts, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
21
27
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
22
|
-
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
28
|
+
import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
|
|
29
|
+
import { readinessOf, } from './readiness.js';
|
|
23
30
|
import { FencedMap } from './fence.js';
|
|
24
|
-
import {
|
|
31
|
+
import { sameBinary } from './overlay.js';
|
|
32
|
+
import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
|
|
33
|
+
import { identityTime, roomDraftRevision } from './identity.js';
|
|
25
34
|
import { beneath, normalize } from './paths.js';
|
|
26
35
|
import { openRuntimeStore, } from './store.js';
|
|
36
|
+
import { changedCleanly, sameComposition, validatedCollection, } from './validation.js';
|
|
27
37
|
import { unconfirmed } from './transfer.js';
|
|
28
38
|
import { governs, holds, narrowed, planReach, } from './reach.js';
|
|
29
39
|
function within(root, path) {
|
|
@@ -42,14 +52,50 @@ function declaredSelection(room) {
|
|
|
42
52
|
const OPENING_LANE = 'opening';
|
|
43
53
|
const CONVERSATION_LANE = 'conversations';
|
|
44
54
|
const ROOM_READS = 256;
|
|
55
|
+
const ROOM_ATTEMPTS = 16;
|
|
56
|
+
const ERASE_BATCH = 512;
|
|
57
|
+
const REDERIVE_BATCH = 256;
|
|
58
|
+
const WALK_BYTES = 1 << 20;
|
|
59
|
+
const QUERY_BYTES = 1 << 20;
|
|
60
|
+
const STAGE_BYTES = 1 << 20;
|
|
61
|
+
const STAGE_FLOOR = 1 << 16;
|
|
62
|
+
// A batch aims at this much database work, so a Save or a query waits behind at most one short write.
|
|
63
|
+
const STAGE_AIM_MS = 50;
|
|
64
|
+
const PARTS_HELD = 4;
|
|
65
|
+
// Bytes still arriving are reported at most this often, well inside the 2 s progress bound, so readiness is not rebuilt for every chunk the network hands over.
|
|
66
|
+
const ARRIVAL_REPORT_MS = 250;
|
|
67
|
+
const INSTALL_STEPS = [
|
|
68
|
+
'writing',
|
|
69
|
+
'downloading',
|
|
70
|
+
'replaying',
|
|
71
|
+
'listing',
|
|
72
|
+
];
|
|
73
|
+
const CLEAN_CHANGES = 2_000;
|
|
74
|
+
// A Room that locks offline stays readable this long after the connection drops, so a laptop waking or changing network keeps working while a disconnected device locks soon after.
|
|
75
|
+
const LOCK_GRACE_MS = 120_000;
|
|
76
|
+
function operationOf(input) {
|
|
77
|
+
const operation = input?.operation_id;
|
|
78
|
+
return typeof operation === 'string' ? operation : null;
|
|
79
|
+
}
|
|
80
|
+
function abandonedOn(signal) {
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
if (signal.aborted)
|
|
83
|
+
resolve(null);
|
|
84
|
+
else
|
|
85
|
+
signal.addEventListener('abort', () => resolve(null), { once: true });
|
|
86
|
+
});
|
|
87
|
+
}
|
|
45
88
|
function roomLane(locator) {
|
|
46
89
|
return `room ${locator}`;
|
|
47
90
|
}
|
|
91
|
+
function pendingKey(locator, id) {
|
|
92
|
+
return `${locator}\u0000${id}`;
|
|
93
|
+
}
|
|
48
94
|
export function composeRuntime(options) {
|
|
49
95
|
const localSaves = options.adapter.localSaves === true;
|
|
96
|
+
const roomTypes = roomTypesOf(options.adapter.roomTypes);
|
|
50
97
|
let authorization = options.authorization;
|
|
51
98
|
let opening;
|
|
52
|
-
let store;
|
|
53
99
|
const fences = new Map();
|
|
54
100
|
const isFenced = (locator) => fences.has(locator);
|
|
55
101
|
let fencings = 0;
|
|
@@ -59,6 +105,8 @@ export function composeRuntime(options) {
|
|
|
59
105
|
const replicas = new FencedMap(isFenced);
|
|
60
106
|
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
61
107
|
const pendings = new Map();
|
|
108
|
+
const seeds = new Map();
|
|
109
|
+
const seedWatchers = new Map();
|
|
62
110
|
const roomDrafts = new FencedMap(isFenced);
|
|
63
111
|
const presentations = new Map();
|
|
64
112
|
let limits = [];
|
|
@@ -71,6 +119,7 @@ export function composeRuntime(options) {
|
|
|
71
119
|
const liveModels = new FencedMap(isModelFenced);
|
|
72
120
|
const invalidModels = new Set();
|
|
73
121
|
const discovered = new Map();
|
|
122
|
+
const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
|
|
74
123
|
let roomsListed = false;
|
|
75
124
|
const scopedRefusals = new Map();
|
|
76
125
|
const sourceRefusals = new Map();
|
|
@@ -98,7 +147,6 @@ export function composeRuntime(options) {
|
|
|
98
147
|
let status = STARTING;
|
|
99
148
|
let shown = STARTING;
|
|
100
149
|
let discoveryPaused = false;
|
|
101
|
-
let resetTimer;
|
|
102
150
|
let closed = false;
|
|
103
151
|
let stopping = false;
|
|
104
152
|
let withdrawStream;
|
|
@@ -106,7 +154,11 @@ export function composeRuntime(options) {
|
|
|
106
154
|
let underway = null;
|
|
107
155
|
let streamed = null;
|
|
108
156
|
let handed = false;
|
|
109
|
-
const
|
|
157
|
+
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
158
|
+
const repairing = new Set();
|
|
159
|
+
const calling = new Set();
|
|
160
|
+
const repairable = new Map();
|
|
161
|
+
let outcomeRetention = null;
|
|
110
162
|
const answering = new Map();
|
|
111
163
|
let complete = false;
|
|
112
164
|
let recorded = false;
|
|
@@ -115,10 +167,12 @@ export function composeRuntime(options) {
|
|
|
115
167
|
let discovering = null;
|
|
116
168
|
let opened = null;
|
|
117
169
|
const owed = new Set();
|
|
170
|
+
const reloadsDue = new Map();
|
|
118
171
|
const lanes = new Map();
|
|
119
172
|
let roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
120
173
|
const working = new Set();
|
|
121
174
|
let mutations = Promise.resolve();
|
|
175
|
+
let gathering = null;
|
|
122
176
|
const roomListeners = new Set();
|
|
123
177
|
const refusalListeners = new Set();
|
|
124
178
|
const storageLossListeners = new Set();
|
|
@@ -126,36 +180,79 @@ export function composeRuntime(options) {
|
|
|
126
180
|
const queueSubscriptions = new Set();
|
|
127
181
|
const roomDraftListeners = new Set();
|
|
128
182
|
const presentationListeners = new Set();
|
|
129
|
-
const
|
|
183
|
+
const liveQueries = createLiveQueries((plan, cover) => answeredPlan(plan, cover));
|
|
130
184
|
let withdrawConversations;
|
|
131
185
|
let currencyQueued = null;
|
|
132
186
|
let socketOpen = false;
|
|
187
|
+
const confirmed = new Set();
|
|
188
|
+
let lockGrace = null;
|
|
189
|
+
const lockListeners = new Set();
|
|
133
190
|
let serviceMode = 'open';
|
|
134
191
|
let haltedRefusal = false;
|
|
135
192
|
let openSignals = 0;
|
|
136
193
|
const serviceModeListeners = new Set();
|
|
137
194
|
let member = null;
|
|
138
195
|
const memberListeners = new Set();
|
|
139
|
-
const
|
|
140
|
-
|
|
196
|
+
const unpreparedDemand = new Set();
|
|
197
|
+
const unpreparable = new Set();
|
|
198
|
+
let preparing = false;
|
|
199
|
+
let targets = {};
|
|
200
|
+
let persistence = options.shell.persistence;
|
|
201
|
+
let full = false;
|
|
202
|
+
let probing = null;
|
|
203
|
+
let testing = null;
|
|
204
|
+
const database = {
|
|
205
|
+
async ask(request) {
|
|
206
|
+
try {
|
|
207
|
+
return await options.database.ask(request);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
if (refusedForSpace(error))
|
|
211
|
+
filled();
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
const readinessWatches = new Set();
|
|
217
|
+
let readinessQueued = false;
|
|
218
|
+
const catching = new Set();
|
|
141
219
|
const transfers = new FencedMap(isFenced);
|
|
220
|
+
const installs = new Map();
|
|
221
|
+
const arrivals = new Map();
|
|
222
|
+
let arrivalReported = 0;
|
|
223
|
+
let onScreen = options.shell.onScreen;
|
|
224
|
+
const shownWaiters = [];
|
|
225
|
+
let partsFree = PARTS_HELD;
|
|
226
|
+
const partWaiters = [];
|
|
227
|
+
let stageBytes = STAGE_FLOOR * 4;
|
|
228
|
+
let installWrites = Promise.resolve();
|
|
142
229
|
const conversations = createConversationStore({
|
|
143
230
|
agents: declaredAgents,
|
|
144
|
-
documents: (
|
|
231
|
+
documents: (declaration, after) => rowsQuery(planOf({
|
|
232
|
+
rooms: [declaration.locator],
|
|
233
|
+
types: [declaration.type],
|
|
234
|
+
select: {
|
|
235
|
+
fields: [
|
|
236
|
+
declaration.identity,
|
|
237
|
+
declaration.name,
|
|
238
|
+
...(declaration.retired === undefined ? [] : [declaration.retired]),
|
|
239
|
+
].map((field) => `/${escapePointer(field)}`),
|
|
240
|
+
},
|
|
241
|
+
limit: MAXIMUM_WINDOW,
|
|
242
|
+
...(after === undefined ? {} : { after }),
|
|
243
|
+
}, roomFacts())),
|
|
145
244
|
call: (name, input) => invokeAction(name, input),
|
|
146
245
|
durably,
|
|
147
246
|
stream: () => options.adapter.conversations,
|
|
148
247
|
publish: publishChangeSet,
|
|
149
|
-
online: () =>
|
|
248
|
+
online: () => connectionOf() !== 'unreachable' && refusal === null,
|
|
249
|
+
full: () => full,
|
|
150
250
|
closed: () => closed,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
documents: () => readSide.documents(),
|
|
154
|
-
document: (room, path) => readSide.document(room, path),
|
|
155
|
-
messages: () => conversations.messages(),
|
|
156
|
-
message: (conversation, identity) => conversations.message(conversation, identity),
|
|
251
|
+
unanswered: resendLater,
|
|
252
|
+
expired: (operation) => pastHorizon(operation),
|
|
157
253
|
});
|
|
158
254
|
const journal = createLog();
|
|
255
|
+
const keptFiles = openedFiles(database, isFenced);
|
|
159
256
|
let notifyReady = () => { };
|
|
160
257
|
const ready = new Promise((resolve) => {
|
|
161
258
|
notifyReady = resolve;
|
|
@@ -173,16 +270,36 @@ export function composeRuntime(options) {
|
|
|
173
270
|
if (room.size === 0)
|
|
174
271
|
roomDrafts.delete(locator);
|
|
175
272
|
}
|
|
176
|
-
function
|
|
273
|
+
async function visibleOf(locator, paths) {
|
|
274
|
+
const held = replicas.get(locator);
|
|
275
|
+
if (held === undefined)
|
|
276
|
+
return undefined;
|
|
277
|
+
const local = await openLocal();
|
|
278
|
+
const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
|
|
279
|
+
return { ...slice, collection: collectionOf(slice.documents, compositionOf(held.record)) };
|
|
280
|
+
}
|
|
281
|
+
function roomDraftOf(record, visible) {
|
|
177
282
|
return structuredClone({
|
|
178
283
|
room: record.locator,
|
|
179
284
|
path: record.path,
|
|
180
285
|
revision: record.revision,
|
|
181
286
|
parts: record.parts,
|
|
182
|
-
changedUnderneath: underneath(record.parts, record.path,
|
|
287
|
+
changedUnderneath: underneath(record.parts, record.path, visible),
|
|
183
288
|
returned: record.returned,
|
|
184
289
|
});
|
|
185
290
|
}
|
|
291
|
+
async function roomDraftsOf(records) {
|
|
292
|
+
const rooms = new Map();
|
|
293
|
+
for (const record of records) {
|
|
294
|
+
const paths = rooms.get(record.locator) ?? [];
|
|
295
|
+
paths.push(record.path);
|
|
296
|
+
rooms.set(record.locator, paths);
|
|
297
|
+
}
|
|
298
|
+
const layers = new Map();
|
|
299
|
+
for (const [locator, paths] of rooms)
|
|
300
|
+
layers.set(locator, await visibleOf(locator, paths));
|
|
301
|
+
return records.map((record) => roomDraftOf(record, layers.get(record.locator)));
|
|
302
|
+
}
|
|
186
303
|
function snapshotRoomDrafts() {
|
|
187
304
|
const records = [...roomDrafts.values()].flatMap((room) => [...room.values()]);
|
|
188
305
|
records.sort((left, right) => {
|
|
@@ -192,23 +309,62 @@ export function composeRuntime(options) {
|
|
|
192
309
|
return left.path < right.path ? -1 : 1;
|
|
193
310
|
return 0;
|
|
194
311
|
});
|
|
195
|
-
return records
|
|
312
|
+
return roomDraftsOf(records);
|
|
196
313
|
}
|
|
314
|
+
let draftsShown = Promise.resolve();
|
|
197
315
|
function publishRoomDrafts(waitingOnly) {
|
|
198
316
|
if (closed)
|
|
199
317
|
return;
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
listener
|
|
206
|
-
|
|
318
|
+
const snapshot = snapshotRoomDrafts();
|
|
319
|
+
draftsShown = draftsShown.then(async () => {
|
|
320
|
+
const value = await snapshot.catch(() => null);
|
|
321
|
+
if (value === null || closed)
|
|
322
|
+
return;
|
|
323
|
+
for (const listener of roomDraftListeners) {
|
|
324
|
+
if (waitingOnly && listener.ready)
|
|
325
|
+
continue;
|
|
326
|
+
listener.ready = true;
|
|
327
|
+
listener.notify(value);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
207
330
|
}
|
|
208
331
|
function presentationOf(app) {
|
|
209
332
|
const value = presentations.get(app);
|
|
210
333
|
return value === undefined ? null : structuredClone(value);
|
|
211
334
|
}
|
|
335
|
+
async function writtenRoomDraft(room, path, revision, input, next) {
|
|
336
|
+
const parts = readParts(input);
|
|
337
|
+
if (path === '' || normalize(path) !== path)
|
|
338
|
+
throw runtimeError('room-draft-malformed');
|
|
339
|
+
const local = await openLocal();
|
|
340
|
+
const current = roomDrafts.get(room)?.get(path);
|
|
341
|
+
if ((current?.revision ?? null) !== revision) {
|
|
342
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
343
|
+
}
|
|
344
|
+
const file = parts.file === undefined
|
|
345
|
+
? undefined
|
|
346
|
+
: await heldFile(parts.file.blob, binaryBase(await visibleOf(room, [path]), path));
|
|
347
|
+
const record = {
|
|
348
|
+
locator: room,
|
|
349
|
+
path,
|
|
350
|
+
revision: next,
|
|
351
|
+
parts: held(current?.parts, parts, file),
|
|
352
|
+
returned: current?.returned ?? null,
|
|
353
|
+
};
|
|
354
|
+
await local.keepRoomDraft(record, parts.file === undefined || file === undefined
|
|
355
|
+
? []
|
|
356
|
+
: [{ key: file.localId, blob: parts.file.blob }]);
|
|
357
|
+
rememberRoomDraft(record);
|
|
358
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
359
|
+
return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
|
|
360
|
+
}
|
|
361
|
+
async function keptPresentation(app, next) {
|
|
362
|
+
const local = await openLocal();
|
|
363
|
+
const stored = next(presentations.get(app));
|
|
364
|
+
await local.present({ app, value: stored });
|
|
365
|
+
presentations.set(app, stored);
|
|
366
|
+
publishChangeSet(changeSet('', { presentation: app }));
|
|
367
|
+
}
|
|
212
368
|
function emitPresentation(app) {
|
|
213
369
|
if (closed)
|
|
214
370
|
return;
|
|
@@ -233,9 +389,16 @@ export function composeRuntime(options) {
|
|
|
233
389
|
returned: structuredClone(record.returned),
|
|
234
390
|
});
|
|
235
391
|
}
|
|
392
|
+
for (const [room, refusal] of sourceRefusals)
|
|
393
|
+
attentionRequired.push({ room, refusal: structuredClone(refusal) });
|
|
236
394
|
attentionRequired.sort((left, right) => {
|
|
237
395
|
if (left.room !== right.room)
|
|
238
396
|
return left.room < right.room ? -1 : 1;
|
|
397
|
+
if ('refusal' in left || 'refusal' in right) {
|
|
398
|
+
if ('refusal' in left && 'refusal' in right)
|
|
399
|
+
return 0;
|
|
400
|
+
return 'refusal' in left ? -1 : 1;
|
|
401
|
+
}
|
|
239
402
|
if (left.path !== right.path)
|
|
240
403
|
return left.path < right.path ? -1 : 1;
|
|
241
404
|
return 0;
|
|
@@ -250,23 +413,69 @@ export function composeRuntime(options) {
|
|
|
250
413
|
return { attentionRequired, reset };
|
|
251
414
|
}
|
|
252
415
|
function documentsIn(locator) {
|
|
253
|
-
|
|
254
|
-
if (replica === undefined)
|
|
255
|
-
return 0;
|
|
256
|
-
const roots = replica.confirmed.record.typedSelection?.roots ?? [];
|
|
257
|
-
const paths = [
|
|
258
|
-
...Object.keys(replica.visible.documents).filter(markdownPath),
|
|
259
|
-
...Object.keys(replica.visible.binaries),
|
|
260
|
-
];
|
|
261
|
-
return paths.filter((path) => !roots.some((root) => beneath(root, path))).length;
|
|
416
|
+
return replicas.get(locator)?.count ?? 0;
|
|
262
417
|
}
|
|
263
418
|
function listed() {
|
|
264
419
|
return structuredClone([...rooms.values()].map((room) => {
|
|
265
420
|
const documents = documentsIn(room.locator);
|
|
266
421
|
counted.set(room.locator, documents);
|
|
267
|
-
return
|
|
422
|
+
return locked(room.locator)
|
|
423
|
+
? {
|
|
424
|
+
...room,
|
|
425
|
+
model: modelTypes(NO_MODEL),
|
|
426
|
+
typedSelection: null,
|
|
427
|
+
documents: 0,
|
|
428
|
+
locked: true,
|
|
429
|
+
}
|
|
430
|
+
: { ...room, documents, locked: false };
|
|
268
431
|
}));
|
|
269
432
|
}
|
|
433
|
+
function locked(locator) {
|
|
434
|
+
return rooms.get(locator)?.replica.offline === 'locked' && !confirmed.has(locator);
|
|
435
|
+
}
|
|
436
|
+
function lockedRooms() {
|
|
437
|
+
return [...rooms.keys()].filter(locked).sort();
|
|
438
|
+
}
|
|
439
|
+
function relocked(before) {
|
|
440
|
+
const after = lockedRooms();
|
|
441
|
+
if (JSON.stringify(after) === JSON.stringify(before))
|
|
442
|
+
return;
|
|
443
|
+
journal.record(`locked ${after.length}`);
|
|
444
|
+
emitRooms();
|
|
445
|
+
for (const notify of [...lockListeners])
|
|
446
|
+
notify();
|
|
447
|
+
}
|
|
448
|
+
function confirmLocks(listed) {
|
|
449
|
+
if (!socketOpen)
|
|
450
|
+
return;
|
|
451
|
+
for (const room of listed) {
|
|
452
|
+
if (room.replica.admission === 'admitted')
|
|
453
|
+
confirmed.add(room.locator);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function connectionLost() {
|
|
457
|
+
if (lockGrace !== null || closed)
|
|
458
|
+
return;
|
|
459
|
+
lockGrace = setTimeout(() => {
|
|
460
|
+
lockGrace = null;
|
|
461
|
+
if (socketOpen || closed)
|
|
462
|
+
return;
|
|
463
|
+
const before = lockedRooms();
|
|
464
|
+
confirmed.clear();
|
|
465
|
+
relocked(before);
|
|
466
|
+
}, LOCK_GRACE_MS);
|
|
467
|
+
}
|
|
468
|
+
function connectionLive() {
|
|
469
|
+
if (lockGrace !== null)
|
|
470
|
+
clearTimeout(lockGrace);
|
|
471
|
+
lockGrace = null;
|
|
472
|
+
}
|
|
473
|
+
function watchLocks(notify) {
|
|
474
|
+
lockListeners.add(notify);
|
|
475
|
+
return () => {
|
|
476
|
+
lockListeners.delete(notify);
|
|
477
|
+
};
|
|
478
|
+
}
|
|
270
479
|
function roomFacts(reach = null) {
|
|
271
480
|
const held = [...rooms.values()]
|
|
272
481
|
.filter((room) => holds(reach, room.locator))
|
|
@@ -285,15 +494,19 @@ export function composeRuntime(options) {
|
|
|
285
494
|
return { listed: roomsListed, rooms: held };
|
|
286
495
|
}
|
|
287
496
|
function emitRooms() {
|
|
497
|
+
liveQueries.recover();
|
|
498
|
+
queueReadiness();
|
|
288
499
|
const value = listed();
|
|
289
500
|
for (const listener of roomListeners)
|
|
290
501
|
listener(value);
|
|
291
502
|
}
|
|
292
503
|
function emitRefusal() {
|
|
504
|
+
queueReadiness();
|
|
293
505
|
for (const listener of refusalListeners)
|
|
294
506
|
listener(refusal);
|
|
295
507
|
}
|
|
296
508
|
function emitStorageLoss() {
|
|
509
|
+
queueReadiness();
|
|
297
510
|
for (const listener of storageLossListeners)
|
|
298
511
|
listener(storageLoss);
|
|
299
512
|
}
|
|
@@ -310,6 +523,7 @@ export function composeRuntime(options) {
|
|
|
310
523
|
function show() {
|
|
311
524
|
if (closed)
|
|
312
525
|
return;
|
|
526
|
+
queueReadiness();
|
|
313
527
|
const next = status.state === 'synced' && !conversations.current()
|
|
314
528
|
? { ...status, state: 'syncing' }
|
|
315
529
|
: status;
|
|
@@ -319,17 +533,17 @@ export function composeRuntime(options) {
|
|
|
319
533
|
for (const listener of statusListeners)
|
|
320
534
|
listener(shown);
|
|
321
535
|
}
|
|
322
|
-
function entryOf(batch) {
|
|
536
|
+
function entryOf(locator, batch) {
|
|
323
537
|
if (localSaves)
|
|
324
538
|
return Object.freeze({ batch, state: 'local' });
|
|
325
|
-
const state = pendings.get(batch.id);
|
|
539
|
+
const state = pendings.get(pendingKey(locator, batch.id));
|
|
326
540
|
if (state?.accepted !== undefined) {
|
|
327
541
|
return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
|
|
328
542
|
}
|
|
329
543
|
return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
|
|
330
544
|
}
|
|
331
545
|
function entriesOf(locator) {
|
|
332
|
-
return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map(entryOf);
|
|
546
|
+
return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map((batch) => entryOf(locator, batch));
|
|
333
547
|
}
|
|
334
548
|
function emitQueue(locator) {
|
|
335
549
|
const entries = entriesOf(locator);
|
|
@@ -339,42 +553,454 @@ export function composeRuntime(options) {
|
|
|
339
553
|
}
|
|
340
554
|
}
|
|
341
555
|
function applyChangeSet(set) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
556
|
+
liveQueries.touched(set);
|
|
557
|
+
}
|
|
558
|
+
function unpublished(error) {
|
|
559
|
+
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
560
|
+
}
|
|
561
|
+
function indexed(locator) {
|
|
562
|
+
return replicas.get(locator)?.indexed === true;
|
|
563
|
+
}
|
|
564
|
+
async function prepareReplica(locator) {
|
|
565
|
+
for (;;) {
|
|
566
|
+
const held = replicas.get(locator);
|
|
567
|
+
if (held === undefined || closed || held.indexed)
|
|
568
|
+
return;
|
|
569
|
+
const key = held.record.key;
|
|
570
|
+
const done = await exclusive(async () => (await openLocal()).rederive(key, REDERIVE_BATCH));
|
|
571
|
+
const now = replicas.get(locator);
|
|
572
|
+
if (now?.record.key !== key)
|
|
356
573
|
continue;
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
574
|
+
if (!done)
|
|
575
|
+
continue;
|
|
576
|
+
replicas.set(locator, { ...now, indexed: true });
|
|
577
|
+
publishChangeSet(changeSet(locator, { documents: null }));
|
|
578
|
+
liveQueries.recover();
|
|
579
|
+
queueReadiness();
|
|
580
|
+
return;
|
|
361
581
|
}
|
|
362
|
-
|
|
582
|
+
}
|
|
583
|
+
function unprepared() {
|
|
584
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !indexed(locator) && !unpreparable.has(locator));
|
|
585
|
+
}
|
|
586
|
+
function unpreparedBy(locator, error) {
|
|
587
|
+
unpreparable.add(locator);
|
|
588
|
+
journal.record(`preparation failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
589
|
+
queueReadiness();
|
|
590
|
+
}
|
|
591
|
+
function prepare(locators = []) {
|
|
592
|
+
for (const locator of locators)
|
|
593
|
+
unpreparedDemand.add(locator);
|
|
594
|
+
if (preparing)
|
|
363
595
|
return;
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
596
|
+
preparing = true;
|
|
597
|
+
void (async () => {
|
|
598
|
+
try {
|
|
599
|
+
for (let waiting = unprepared(); waiting !== undefined; waiting = unprepared()) {
|
|
600
|
+
await yielded();
|
|
601
|
+
if (closed)
|
|
602
|
+
return;
|
|
603
|
+
unpreparedDemand.delete(waiting);
|
|
604
|
+
await prepareReplica(waiting).catch((error) => unpreparedBy(waiting, error));
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
finally {
|
|
608
|
+
preparing = false;
|
|
609
|
+
}
|
|
610
|
+
})();
|
|
611
|
+
}
|
|
612
|
+
function roomsOfReach(reach) {
|
|
613
|
+
return new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
|
|
367
614
|
}
|
|
368
615
|
function reachedRooms(plan) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
616
|
+
const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
|
|
617
|
+
const named = plan.rooms ??
|
|
618
|
+
(reached === null
|
|
619
|
+
? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
|
|
620
|
+
: [...reached]);
|
|
621
|
+
return [...new Set(named)]
|
|
622
|
+
.filter((locator) => (reached === null || reached.has(locator)) && !plan.locked.includes(locator))
|
|
623
|
+
.sort();
|
|
624
|
+
}
|
|
625
|
+
function awaited(locator) {
|
|
626
|
+
return (!roomsListed ||
|
|
627
|
+
(rooms.has(locator) && discovered.get(locator)?.replica.admission === 'admitted'));
|
|
628
|
+
}
|
|
629
|
+
function covered(plan) {
|
|
630
|
+
const locators = reachedRooms(plan);
|
|
631
|
+
if (locators.length === 0 && plan.rooms === null && plan.reach === null && !roomsListed) {
|
|
632
|
+
return { pending: [] };
|
|
633
|
+
}
|
|
634
|
+
const pending = [];
|
|
635
|
+
const coverage = [];
|
|
636
|
+
const overlay = [];
|
|
637
|
+
const read = [];
|
|
638
|
+
for (const locator of locators) {
|
|
639
|
+
const replica = replicas.get(locator);
|
|
640
|
+
if (replica === undefined) {
|
|
641
|
+
if (awaited(locator))
|
|
642
|
+
pending.push({ room: locator, awaits: 'acquisition' });
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (!replica.indexed) {
|
|
646
|
+
pending.push({ room: locator, awaits: 'preparation' });
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
coverage.push({
|
|
650
|
+
room: locator,
|
|
651
|
+
generation: replica.record.transfer,
|
|
652
|
+
current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
|
|
653
|
+
});
|
|
654
|
+
read.push({ locator, key: replica.record.key });
|
|
655
|
+
for (const batch of replica.queue)
|
|
656
|
+
overlay.push(batch.id);
|
|
657
|
+
}
|
|
658
|
+
if (pending.length > 0)
|
|
659
|
+
return { pending };
|
|
660
|
+
return { coverage: { rooms: coverage, indexes: [], overlay }, rooms: read };
|
|
661
|
+
}
|
|
662
|
+
function awaitPreparation(cover) {
|
|
663
|
+
if (!('pending' in cover))
|
|
664
|
+
return;
|
|
665
|
+
const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
|
|
666
|
+
if (waiting.length > 0)
|
|
667
|
+
prepare(waiting.map(({ room }) => room));
|
|
668
|
+
}
|
|
669
|
+
function requiredRooms(given) {
|
|
670
|
+
const reach = planReach(given);
|
|
671
|
+
if (reach === null)
|
|
672
|
+
return { listed: roomsListed, rooms: [...rooms.keys()].sort() };
|
|
673
|
+
const named = [...roomsOfReach(reach)].sort();
|
|
674
|
+
if (roomsListed)
|
|
675
|
+
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
676
|
+
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
677
|
+
}
|
|
678
|
+
function roomState(locator) {
|
|
679
|
+
if (sourceRefusals.has(locator))
|
|
680
|
+
return 'refused';
|
|
681
|
+
const listedRoom = discovered.get(locator);
|
|
682
|
+
if (roomsListed &&
|
|
683
|
+
(listedRoom === undefined ||
|
|
684
|
+
listedRoom.replica.admission !== 'admitted' ||
|
|
685
|
+
unreadable.has(locator)))
|
|
686
|
+
return 'unadmitted';
|
|
687
|
+
if (!replicas.has(locator))
|
|
688
|
+
return 'acquiring';
|
|
689
|
+
if (!indexed(locator))
|
|
690
|
+
return 'preparing';
|
|
691
|
+
return 'complete';
|
|
692
|
+
}
|
|
693
|
+
function conversationCoverage(reach) {
|
|
694
|
+
const declared = reach === null || reach.conversations;
|
|
695
|
+
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
696
|
+
return 'undeclared';
|
|
697
|
+
if (!conversations.known() || !conversations.current())
|
|
698
|
+
return 'acquiring';
|
|
699
|
+
return 'complete';
|
|
700
|
+
}
|
|
701
|
+
function connectionOf() {
|
|
702
|
+
if (socketOpen)
|
|
703
|
+
return 'connected';
|
|
704
|
+
if (status.providerUnreachable)
|
|
705
|
+
return 'unreachable';
|
|
706
|
+
return options.adapter.roomStream === undefined && status.lastCompleted !== null
|
|
707
|
+
? 'connected'
|
|
708
|
+
: 'connecting';
|
|
709
|
+
}
|
|
710
|
+
function refusedIn(locators) {
|
|
711
|
+
return locators.flatMap((room) => {
|
|
712
|
+
const held = sourceRefusals.get(room);
|
|
713
|
+
if (held === undefined)
|
|
714
|
+
return [];
|
|
715
|
+
const { file, location } = held.fields;
|
|
716
|
+
return [
|
|
717
|
+
{
|
|
718
|
+
room,
|
|
719
|
+
name: discovered.get(room)?.name ?? room,
|
|
720
|
+
file: typeof file === 'string' ? file : '',
|
|
721
|
+
rule: held.code,
|
|
722
|
+
location: typeof location === 'string' ? location : null,
|
|
723
|
+
},
|
|
724
|
+
];
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
function installationOf(coverage) {
|
|
728
|
+
const rooms = { done: 0, total: 0 };
|
|
729
|
+
const parts = { done: 0, total: 0 };
|
|
730
|
+
const bytes = { done: 0, total: 0 };
|
|
731
|
+
const received = { done: 0, total: 0 };
|
|
732
|
+
const steps = new Set();
|
|
733
|
+
let waiting = false;
|
|
734
|
+
for (const { room, state } of coverage) {
|
|
735
|
+
if (state === 'unadmitted')
|
|
736
|
+
continue;
|
|
737
|
+
rooms.total += 1;
|
|
738
|
+
if (state === 'complete')
|
|
739
|
+
rooms.done += 1;
|
|
740
|
+
const transfer = transfers.get(room);
|
|
741
|
+
const held = installs.get(room);
|
|
742
|
+
const inventory = transfer?.inventory ?? replicas.get(room)?.record.inventory ?? null;
|
|
743
|
+
if (inventory !== null) {
|
|
744
|
+
const done = transfer?.done ?? inventory;
|
|
745
|
+
const share = held?.share ?? null;
|
|
746
|
+
parts.total += inventory.parts;
|
|
747
|
+
bytes.total += inventory.bytes;
|
|
748
|
+
parts.done += done.parts;
|
|
749
|
+
bytes.done += done.bytes;
|
|
750
|
+
if (share !== null && share.entries > 0) {
|
|
751
|
+
bytes.done += Math.floor((share.bytes * share.written) / share.entries);
|
|
752
|
+
}
|
|
753
|
+
received.total += inventory.bytes;
|
|
754
|
+
received.done += done.bytes + (share?.bytes ?? 0);
|
|
755
|
+
for (const arrived of arrivals.get(room)?.values() ?? [])
|
|
756
|
+
received.done += arrived;
|
|
757
|
+
}
|
|
758
|
+
const installing = held !== undefined && state !== 'complete';
|
|
759
|
+
if (installing)
|
|
760
|
+
steps.add(held.step);
|
|
761
|
+
waiting ||= installing || state === 'acquiring';
|
|
762
|
+
}
|
|
763
|
+
return {
|
|
764
|
+
step: INSTALL_STEPS.find((step) => steps.has(step)) ?? null,
|
|
765
|
+
paused: waiting && !onScreen ? 'hidden' : null,
|
|
766
|
+
rooms,
|
|
767
|
+
parts,
|
|
768
|
+
bytes,
|
|
769
|
+
received,
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
function readinessFor(port, reach) {
|
|
773
|
+
const required = requiredRooms(reach);
|
|
774
|
+
const search = port.search === 'required';
|
|
775
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room) }));
|
|
776
|
+
const preparing = coverage
|
|
777
|
+
.filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
|
|
778
|
+
.map(({ room }) => room);
|
|
779
|
+
if (preparing.length > 0)
|
|
780
|
+
prepare(preparing);
|
|
781
|
+
return readinessOf({
|
|
782
|
+
code: port.code,
|
|
783
|
+
coverage: {
|
|
784
|
+
listed: required.listed,
|
|
785
|
+
rooms: coverage,
|
|
786
|
+
conversations: conversationCoverage(reach),
|
|
787
|
+
search: search ? 'required' : 'unused',
|
|
788
|
+
},
|
|
789
|
+
persistence,
|
|
790
|
+
connection: connectionOf(),
|
|
791
|
+
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
792
|
+
space: full ? 'full' : port.space,
|
|
793
|
+
installation: installationOf(coverage),
|
|
794
|
+
refused: refusedIn(required.rooms),
|
|
795
|
+
lost: storageLoss !== null,
|
|
796
|
+
established: targets[port.target]?.established === true,
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
function establish(name) {
|
|
800
|
+
targets = { ...targets, [name]: { established: true } };
|
|
801
|
+
queueReadiness();
|
|
802
|
+
probeEachMinute();
|
|
803
|
+
void durably((local) => local.fact('targets', targets)).catch(() => {
|
|
804
|
+
journal.record(`target record failed ${name}`);
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
function deliverReadiness(watch) {
|
|
808
|
+
if (!readinessWatches.has(watch))
|
|
809
|
+
return;
|
|
810
|
+
const readiness = readinessFor(watch.port, watch.reach);
|
|
811
|
+
if (readiness.ready && targets[watch.port.target]?.established !== true) {
|
|
812
|
+
establish(watch.port.target);
|
|
813
|
+
}
|
|
814
|
+
const shown = JSON.stringify(readiness);
|
|
815
|
+
if (shown === watch.last)
|
|
816
|
+
return;
|
|
817
|
+
watch.last = shown;
|
|
818
|
+
watch.deliver(structuredClone(readiness));
|
|
819
|
+
}
|
|
820
|
+
function queueReadiness() {
|
|
821
|
+
if (readinessQueued || closed || readinessWatches.size === 0)
|
|
822
|
+
return;
|
|
823
|
+
readinessQueued = true;
|
|
824
|
+
queueMicrotask(() => {
|
|
825
|
+
readinessQueued = false;
|
|
826
|
+
if (closed)
|
|
827
|
+
return;
|
|
828
|
+
for (const watch of [...readinessWatches])
|
|
829
|
+
deliverReadiness(watch);
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
function watchReadiness(port, reach, deliver) {
|
|
833
|
+
const watch = { port, reach, deliver, last: null };
|
|
834
|
+
readinessWatches.add(watch);
|
|
835
|
+
void openLocal().then(() => deliverReadiness(watch), () => deliverReadiness(watch));
|
|
836
|
+
return () => {
|
|
837
|
+
readinessWatches.delete(watch);
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
function filled() {
|
|
841
|
+
if (full || closed)
|
|
842
|
+
return;
|
|
843
|
+
full = true;
|
|
844
|
+
journal.record('space full');
|
|
845
|
+
queueReadiness();
|
|
846
|
+
probeEachMinute();
|
|
847
|
+
}
|
|
848
|
+
function installedTarget() {
|
|
849
|
+
return Object.values(targets).some((target) => target.established);
|
|
850
|
+
}
|
|
851
|
+
function probeEachMinute() {
|
|
852
|
+
if (!full || probing !== null || closed || !installedTarget())
|
|
853
|
+
return;
|
|
854
|
+
// A device whose storage filled after its install tries a write each minute, so space freed elsewhere ends its read-only mode without a Retry.
|
|
855
|
+
probing = setInterval(() => {
|
|
856
|
+
void probed();
|
|
857
|
+
}, 60_000);
|
|
858
|
+
}
|
|
859
|
+
function probed() {
|
|
860
|
+
testing ??= database.ask({ kind: 'space.probe' }).then(() => {
|
|
861
|
+
testing = null;
|
|
862
|
+
spaceFreed();
|
|
863
|
+
return true;
|
|
864
|
+
}, () => {
|
|
865
|
+
testing = null;
|
|
866
|
+
return false;
|
|
867
|
+
});
|
|
868
|
+
return testing;
|
|
869
|
+
}
|
|
870
|
+
function spaceFreed() {
|
|
871
|
+
if (!full)
|
|
872
|
+
return;
|
|
873
|
+
full = false;
|
|
874
|
+
if (probing !== null)
|
|
875
|
+
clearInterval(probing);
|
|
876
|
+
probing = null;
|
|
877
|
+
unpreparable.clear();
|
|
878
|
+
journal.record('space freed');
|
|
879
|
+
queueReadiness();
|
|
880
|
+
if (!closed && authorization !== undefined)
|
|
881
|
+
void sync('unstreamed');
|
|
882
|
+
}
|
|
883
|
+
function seedRoomHeld(room, version) {
|
|
884
|
+
return indexed(room) && reachedBy(room, version);
|
|
885
|
+
}
|
|
886
|
+
function seedHeld(at) {
|
|
887
|
+
return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
|
|
888
|
+
}
|
|
889
|
+
function seedApplies(plan) {
|
|
890
|
+
const seed = seeds.get(plan.key);
|
|
891
|
+
if (seed === undefined)
|
|
372
892
|
return null;
|
|
373
|
-
|
|
893
|
+
if (!seedHeld(seed.at))
|
|
894
|
+
return seed;
|
|
895
|
+
seeds.delete(plan.key);
|
|
896
|
+
return null;
|
|
897
|
+
}
|
|
898
|
+
function seedBehind(plan, cover) {
|
|
899
|
+
const seed = seedApplies(plan);
|
|
900
|
+
if (seed === null || 'pending' in cover)
|
|
901
|
+
return cover;
|
|
902
|
+
return {
|
|
903
|
+
pending: Object.entries(seed.at)
|
|
904
|
+
.filter(([room, version]) => !seedRoomHeld(room, version))
|
|
905
|
+
.map(([room]) => ({ room, awaits: 'acquisition' })),
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
function seed(entry) {
|
|
909
|
+
if (seedHeld(entry.at))
|
|
910
|
+
return;
|
|
911
|
+
for (const { query, rows } of entry.queries) {
|
|
912
|
+
const key = queryKey(query);
|
|
913
|
+
seeds.set(key, { rows, at: entry.at });
|
|
914
|
+
for (const shown of seedWatchers.get(key) ?? [])
|
|
915
|
+
shown();
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
function sameCover(left, right) {
|
|
919
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
920
|
+
}
|
|
921
|
+
async function coveredRows(plan, cover) {
|
|
922
|
+
for (;;) {
|
|
923
|
+
const before = cover();
|
|
924
|
+
if ('pending' in before)
|
|
925
|
+
return { pending: before };
|
|
926
|
+
const local = await openLocal();
|
|
927
|
+
const keys = before.rooms.map(({ key }) => key);
|
|
928
|
+
const changes = local.changes(keys);
|
|
929
|
+
const current = () => local.changes(keys) === changes;
|
|
930
|
+
const rows = await windowRows(local, plan, before.rooms, current);
|
|
931
|
+
const after = cover();
|
|
932
|
+
if (rows !== null && sameCover(before, after))
|
|
933
|
+
return { rows, cover: before, current };
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
async function windowRows(local, plan, rooms, unchanged) {
|
|
937
|
+
const first = await local.query(plan, rooms, QUERY_BYTES);
|
|
938
|
+
if (first.more === undefined || !('documents' in first)) {
|
|
939
|
+
const { more: _more, ...rows } = first;
|
|
940
|
+
return rows;
|
|
941
|
+
}
|
|
942
|
+
const documents = [...first.documents];
|
|
943
|
+
for (let more = first.more; more !== undefined;) {
|
|
944
|
+
if (!unchanged())
|
|
945
|
+
return null;
|
|
946
|
+
const page = await local.query({
|
|
947
|
+
...plan,
|
|
948
|
+
after: { text: more, values: JSON.parse(more) },
|
|
949
|
+
limit: plan.limit === null ? null : plan.limit - documents.length,
|
|
950
|
+
total: false,
|
|
951
|
+
}, rooms, QUERY_BYTES);
|
|
952
|
+
if (!('documents' in page))
|
|
953
|
+
return null;
|
|
954
|
+
documents.push(...page.documents);
|
|
955
|
+
more = page.more;
|
|
956
|
+
if (more === undefined) {
|
|
957
|
+
return {
|
|
958
|
+
documents,
|
|
959
|
+
...(page.next === undefined ? {} : { next: page.next }),
|
|
960
|
+
...(first.total === undefined ? {} : { total: first.total }),
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return null;
|
|
965
|
+
}
|
|
966
|
+
function keyedOf(rows, coverage) {
|
|
967
|
+
if ('groups' in rows) {
|
|
968
|
+
return {
|
|
969
|
+
result: { ...rows, coverage },
|
|
970
|
+
keys: rows.groups.map((group) => JSON.stringify(group.key)),
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
return {
|
|
974
|
+
result: { ...rows, coverage },
|
|
975
|
+
keys: rows.documents.map((document) => documentKey(document.room, document.path)),
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
async function answeredPlan(plan, cover) {
|
|
979
|
+
const answered = await coveredRows(plan, cover);
|
|
980
|
+
if ('pending' in answered) {
|
|
981
|
+
const pending = answered.pending;
|
|
982
|
+
return { result: pending, keys: [], current: () => true };
|
|
983
|
+
}
|
|
984
|
+
const held = answered.cover;
|
|
985
|
+
return { ...keyedOf(answered.rows, held.coverage), current: answered.current };
|
|
374
986
|
}
|
|
375
|
-
function rowsQuery(plan) {
|
|
376
|
-
|
|
377
|
-
|
|
987
|
+
async function rowsQuery(plan) {
|
|
988
|
+
const before = covered(plan);
|
|
989
|
+
awaitPreparation(before);
|
|
990
|
+
const seeded = seedApplies(plan);
|
|
991
|
+
if (seeded !== null)
|
|
992
|
+
return seededResult(seeded).result;
|
|
993
|
+
if ('pending' in before)
|
|
994
|
+
return before;
|
|
995
|
+
const answered = await coveredRows(plan, () => {
|
|
996
|
+
const cover = covered(plan);
|
|
997
|
+
awaitPreparation(cover);
|
|
998
|
+
return cover;
|
|
999
|
+
});
|
|
1000
|
+
if ('pending' in answered)
|
|
1001
|
+
return answered.pending;
|
|
1002
|
+
const held = answered.cover;
|
|
1003
|
+
return { ...answered.rows, coverage: held.coverage };
|
|
378
1004
|
}
|
|
379
1005
|
function publishChangeSet(set) {
|
|
380
1006
|
applyChangeSet(set);
|
|
@@ -384,49 +1010,38 @@ export function composeRuntime(options) {
|
|
|
384
1010
|
for (const locator of set.queues)
|
|
385
1011
|
emitQueue(locator);
|
|
386
1012
|
if (set.drafts.length > 0 ||
|
|
387
|
-
set.documents.some((change) =>
|
|
1013
|
+
set.documents.some((change) => {
|
|
1014
|
+
const held = roomDrafts.get(change.locator);
|
|
1015
|
+
return held !== undefined && (change.path === null || held.has(change.path));
|
|
1016
|
+
})) {
|
|
388
1017
|
publishRoomDrafts(false);
|
|
389
1018
|
}
|
|
390
1019
|
for (const app of set.presentations)
|
|
391
1020
|
emitPresentation(app);
|
|
392
1021
|
conversations.changed(set);
|
|
393
1022
|
}
|
|
394
|
-
function stopTimer() {
|
|
395
|
-
if (resetTimer === undefined)
|
|
396
|
-
return;
|
|
397
|
-
clearTimeout(resetTimer);
|
|
398
|
-
resetTimer = undefined;
|
|
399
|
-
}
|
|
400
|
-
function armTimer() {
|
|
401
|
-
stopTimer();
|
|
402
|
-
if (closed || stopping)
|
|
403
|
-
return;
|
|
404
|
-
let earliest = null;
|
|
405
|
-
for (const { submission } of pendings.values()) {
|
|
406
|
-
if (submission.state !== 'reset')
|
|
407
|
-
continue;
|
|
408
|
-
const at = Date.parse(submission.reset);
|
|
409
|
-
if (earliest === null || at < earliest)
|
|
410
|
-
earliest = at;
|
|
411
|
-
}
|
|
412
|
-
if (earliest === null)
|
|
413
|
-
return;
|
|
414
|
-
// A Save House held back goes again only once the reset its answer named has passed.
|
|
415
|
-
resetTimer = setTimeout(() => {
|
|
416
|
-
resetTimer = undefined;
|
|
417
|
-
submitAll();
|
|
418
|
-
}, Math.max(0, earliest - Date.now()));
|
|
419
|
-
}
|
|
420
1023
|
async function refuse(reason, provider = null) {
|
|
421
|
-
stopTimer();
|
|
422
1024
|
discoveryPaused = true;
|
|
423
1025
|
try {
|
|
424
1026
|
if (reason === 'revoked-authorization') {
|
|
425
1027
|
await exclusive(async () => {
|
|
426
1028
|
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
1029
|
+
const records = [...replicas].map(([locator, held]) => ({
|
|
1030
|
+
locator,
|
|
1031
|
+
held,
|
|
1032
|
+
record: { ...held.record, model: revoked, selectionModel: revoked },
|
|
1033
|
+
}));
|
|
1034
|
+
let answer = null;
|
|
427
1035
|
try {
|
|
428
1036
|
const local = await openLocal();
|
|
429
|
-
await local.
|
|
1037
|
+
answer = await local.commit({
|
|
1038
|
+
rooms: records.map(({ locator, record }) => ({
|
|
1039
|
+
locator,
|
|
1040
|
+
key: record.key,
|
|
1041
|
+
record: storedRecord(record),
|
|
1042
|
+
composition: compositionOf(record),
|
|
1043
|
+
})),
|
|
1044
|
+
});
|
|
430
1045
|
}
|
|
431
1046
|
catch {
|
|
432
1047
|
journal.record('model revocation persistence failed');
|
|
@@ -442,16 +1057,16 @@ export function composeRuntime(options) {
|
|
|
442
1057
|
liveModels.delete(key);
|
|
443
1058
|
}
|
|
444
1059
|
const changed = [];
|
|
445
|
-
for (const
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
1060
|
+
for (const { locator, held, record } of records) {
|
|
1061
|
+
if (replicas.get(locator) !== held)
|
|
1062
|
+
continue;
|
|
1063
|
+
const indexed = answer === null ? held.indexed : committedRoom(locator, answer).indexed;
|
|
1064
|
+
replicas.set(locator, { ...held, record, indexed });
|
|
1065
|
+
}
|
|
1066
|
+
for (const locator of [...rooms.keys()]) {
|
|
1067
|
+
const room = rooms.get(locator);
|
|
1068
|
+
if (room === undefined)
|
|
1069
|
+
continue;
|
|
455
1070
|
rooms.set(locator, {
|
|
456
1071
|
...room,
|
|
457
1072
|
model: modelTypes(revoked),
|
|
@@ -459,9 +1074,10 @@ export function composeRuntime(options) {
|
|
|
459
1074
|
? null
|
|
460
1075
|
: { ...room.typedSelection, model: modelTypes(revoked) },
|
|
461
1076
|
});
|
|
462
|
-
changed.push(changeSet(locator,
|
|
1077
|
+
changed.push(changeSet(locator, { documents: null, room: true }));
|
|
463
1078
|
}
|
|
464
1079
|
publishChangeSet(mergeChangeSets(changed));
|
|
1080
|
+
prepare();
|
|
465
1081
|
});
|
|
466
1082
|
}
|
|
467
1083
|
}
|
|
@@ -492,41 +1108,57 @@ export function composeRuntime(options) {
|
|
|
492
1108
|
model: modelTypes(modelOf(room.locator, 'typed-selection')),
|
|
493
1109
|
},
|
|
494
1110
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
495
|
-
roomVersion: replicas.get(room.locator)?.
|
|
1111
|
+
roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
|
|
496
1112
|
synchronizationAvailable,
|
|
497
1113
|
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
498
1114
|
});
|
|
499
1115
|
}
|
|
500
|
-
async function applyDiscovery(found, started) {
|
|
1116
|
+
async function applyDiscovery(found, started, covered) {
|
|
1117
|
+
const lockedBefore = lockedRooms();
|
|
501
1118
|
for (const room of found) {
|
|
502
1119
|
const fence = fences.get(room.locator);
|
|
503
1120
|
if (fence !== undefined && fence.erased && fence.at < started)
|
|
504
1121
|
fences.delete(room.locator);
|
|
505
1122
|
}
|
|
1123
|
+
await loseRooms(found
|
|
1124
|
+
.filter((room) => room.replica.admission !== 'admitted' && heldLocally(room.locator))
|
|
1125
|
+
.map((room) => room.locator));
|
|
506
1126
|
const listed = found.filter((room) => !fences.has(room.locator));
|
|
507
1127
|
const listedLocators = new Set(listed.map((room) => room.locator));
|
|
508
1128
|
for (const locator of listedLocators)
|
|
509
1129
|
scopedRefusals.delete(locator);
|
|
510
|
-
|
|
1130
|
+
if (covered === undefined)
|
|
1131
|
+
discovered.clear();
|
|
1132
|
+
else
|
|
1133
|
+
for (const locator of covered)
|
|
1134
|
+
discovered.delete(locator);
|
|
511
1135
|
for (const room of listed)
|
|
512
1136
|
discovered.set(room.locator, room);
|
|
513
|
-
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
|
|
1137
|
+
const unlisted = unlistedHeld(new Set(found.map((room) => room.locator))).filter((locator) => covered === undefined || covered.includes(locator));
|
|
514
1138
|
if (options.adapter.exhaustiveDiscovery === true)
|
|
515
1139
|
await loseRooms(unlisted);
|
|
516
1140
|
else
|
|
517
1141
|
unlistedKept(unlisted);
|
|
518
1142
|
for (const room of listed) {
|
|
519
|
-
const held = replicas.get(room.locator)?.
|
|
1143
|
+
const held = replicas.get(room.locator)?.record;
|
|
520
1144
|
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
521
1145
|
list(held, false);
|
|
522
1146
|
continue;
|
|
523
1147
|
}
|
|
524
1148
|
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
525
1149
|
}
|
|
1150
|
+
confirmLocks(listed);
|
|
526
1151
|
roomsListed = true;
|
|
527
1152
|
emitRooms();
|
|
1153
|
+
relocked(lockedBefore);
|
|
528
1154
|
return listed;
|
|
529
1155
|
}
|
|
1156
|
+
function heldLocally(locator) {
|
|
1157
|
+
return (replicas.has(locator) ||
|
|
1158
|
+
roomDrafts.has(locator) ||
|
|
1159
|
+
transfers.has(locator) ||
|
|
1160
|
+
queuesWithoutReplica.has(locator));
|
|
1161
|
+
}
|
|
530
1162
|
function unlistedHeld(listed) {
|
|
531
1163
|
return [
|
|
532
1164
|
...new Set([
|
|
@@ -539,7 +1171,7 @@ export function composeRuntime(options) {
|
|
|
539
1171
|
}
|
|
540
1172
|
function unlistedKept(unlisted) {
|
|
541
1173
|
for (const locator of unlisted) {
|
|
542
|
-
const held = replicas.get(locator)?.
|
|
1174
|
+
const held = replicas.get(locator)?.record;
|
|
543
1175
|
if (held !== undefined)
|
|
544
1176
|
list(held, false);
|
|
545
1177
|
else
|
|
@@ -548,25 +1180,28 @@ export function composeRuntime(options) {
|
|
|
548
1180
|
}
|
|
549
1181
|
function fence(locators) {
|
|
550
1182
|
const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
|
|
551
|
-
const removed = fresh.map((locator) =>
|
|
552
|
-
|
|
553
|
-
queue: true,
|
|
1183
|
+
const removed = fresh.map((locator) => ({
|
|
1184
|
+
locator,
|
|
554
1185
|
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
555
1186
|
}));
|
|
556
1187
|
for (const locator of fresh) {
|
|
557
1188
|
fences.set(locator, { at: discoveries, erased: false });
|
|
558
1189
|
lastFenced.set(locator, ++fencings);
|
|
1190
|
+
confirmed.delete(locator);
|
|
559
1191
|
for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
|
|
560
|
-
pendings.delete(batch.id);
|
|
1192
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
561
1193
|
replicas.delete(locator);
|
|
562
1194
|
queuesWithoutReplica.delete(locator);
|
|
563
1195
|
rooms.delete(locator);
|
|
564
1196
|
roomDrafts.delete(locator);
|
|
565
1197
|
transfers.delete(locator);
|
|
1198
|
+
catching.delete(locator);
|
|
566
1199
|
discovered.delete(locator);
|
|
567
1200
|
announced.delete(locator);
|
|
568
1201
|
unreadable.delete(locator);
|
|
569
1202
|
stale.delete(locator);
|
|
1203
|
+
repairable.delete(locator);
|
|
1204
|
+
unpreparable.delete(locator);
|
|
570
1205
|
scopedRefusals.delete(locator);
|
|
571
1206
|
sourceRefusals.delete(locator);
|
|
572
1207
|
declarationRefusals.delete(locator);
|
|
@@ -580,24 +1215,48 @@ export function composeRuntime(options) {
|
|
|
580
1215
|
}
|
|
581
1216
|
if (fresh.length === 0)
|
|
582
1217
|
return fresh;
|
|
583
|
-
publishChangeSet(mergeChangeSets(removed));
|
|
584
|
-
|
|
585
|
-
materialized.delete(locator);
|
|
1218
|
+
publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, { documents: null, room: true, queue: true, drafts }))));
|
|
1219
|
+
liveQueries.recover();
|
|
586
1220
|
moveTo({ ...status, ...attention() });
|
|
587
1221
|
return fresh;
|
|
588
1222
|
}
|
|
589
|
-
async function
|
|
590
|
-
await local.erase(locators);
|
|
1223
|
+
async function eraseFenced(locators) {
|
|
591
1224
|
for (const locator of locators) {
|
|
1225
|
+
while (await durably((local) => local.erase(locator, ERASE_BATCH)))
|
|
1226
|
+
await yielded();
|
|
592
1227
|
const held = fences.get(locator);
|
|
593
1228
|
if (held !== undefined)
|
|
594
1229
|
held.erased = true;
|
|
595
1230
|
}
|
|
596
1231
|
}
|
|
1232
|
+
let sweeping = false;
|
|
1233
|
+
function sweep() {
|
|
1234
|
+
if (sweeping || closed)
|
|
1235
|
+
return;
|
|
1236
|
+
sweeping = true;
|
|
1237
|
+
void (async () => {
|
|
1238
|
+
try {
|
|
1239
|
+
while (!closed && (await durably((local) => local.erase(null, ERASE_BATCH)))) {
|
|
1240
|
+
await yielded();
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
catch {
|
|
1244
|
+
journal.record('erasure failed');
|
|
1245
|
+
}
|
|
1246
|
+
finally {
|
|
1247
|
+
sweeping = false;
|
|
1248
|
+
}
|
|
1249
|
+
})();
|
|
1250
|
+
}
|
|
597
1251
|
async function loseRooms(locators) {
|
|
598
1252
|
const fresh = fence(locators);
|
|
599
|
-
if (fresh.length
|
|
600
|
-
|
|
1253
|
+
if (fresh.length === 0)
|
|
1254
|
+
return;
|
|
1255
|
+
await durably(async (local) => {
|
|
1256
|
+
await local.fence(fresh);
|
|
1257
|
+
await unpair(local);
|
|
1258
|
+
});
|
|
1259
|
+
await eraseFenced(fresh);
|
|
601
1260
|
}
|
|
602
1261
|
function restored(key, persisted) {
|
|
603
1262
|
const prior = models.get(key);
|
|
@@ -618,52 +1277,56 @@ export function composeRuntime(options) {
|
|
|
618
1277
|
return model;
|
|
619
1278
|
}
|
|
620
1279
|
async function hydrate(opened) {
|
|
621
|
-
const [
|
|
622
|
-
opened.
|
|
1280
|
+
const [held, queues, kept, heldConversations, chatDrafts] = await Promise.all([
|
|
1281
|
+
opened.opened(),
|
|
623
1282
|
opened.queues(),
|
|
624
1283
|
opened.roomDrafts(),
|
|
625
|
-
opened.presentation(),
|
|
626
1284
|
opened.conversations(),
|
|
627
1285
|
opened.chatDrafts(),
|
|
628
|
-
opened.lastCompleted(),
|
|
629
|
-
opened.providerLimits(),
|
|
630
|
-
opened.capabilities(),
|
|
631
|
-
opened.member(),
|
|
632
|
-
opened.transfers(),
|
|
633
1286
|
]);
|
|
1287
|
+
const records = [...held.rooms.values()];
|
|
1288
|
+
const counts = await opened.counts(records.map(({ key, typedSelection }) => ({ key, roots: typedSelection?.roots ?? [] })));
|
|
1289
|
+
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
1290
|
+
targets = held.targets;
|
|
634
1291
|
transfers.clear();
|
|
635
|
-
for (const record of
|
|
636
|
-
transfers.set(
|
|
637
|
-
limits =
|
|
638
|
-
capabilities =
|
|
639
|
-
member =
|
|
1292
|
+
for (const [locator, record] of held.transfers)
|
|
1293
|
+
transfers.set(locator, record);
|
|
1294
|
+
limits = held.providerLimits;
|
|
1295
|
+
capabilities = held.capabilities;
|
|
1296
|
+
member = held.member;
|
|
640
1297
|
roomDrafts.clear();
|
|
641
1298
|
for (const record of kept)
|
|
642
1299
|
rememberRoomDraft(record);
|
|
643
1300
|
presentations.clear();
|
|
644
|
-
for (const record of
|
|
1301
|
+
for (const record of held.presentation)
|
|
645
1302
|
presentations.set(record.app, record.value);
|
|
646
|
-
conversations.hydrate(
|
|
647
|
-
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
1303
|
+
conversations.hydrate(heldConversations, chatDrafts);
|
|
648
1304
|
pendings.clear();
|
|
649
1305
|
for (const queue of queues) {
|
|
650
|
-
for (const { batch, state } of queue.pending)
|
|
651
|
-
pendings.set(batch.id, state);
|
|
1306
|
+
for (const { batch, state } of queue.pending) {
|
|
1307
|
+
pendings.set(pendingKey(queue.locator, batch.id), state);
|
|
1308
|
+
}
|
|
652
1309
|
}
|
|
653
|
-
const previousReplicas = new Map(replicas);
|
|
654
1310
|
replicas.clear();
|
|
655
1311
|
queuesWithoutReplica.clear();
|
|
656
|
-
const replicated = new Set(records.map((record) => record.locator));
|
|
657
1312
|
for (const [locator, queue] of queuedBatches) {
|
|
658
|
-
if (!
|
|
1313
|
+
if (!held.rooms.has(locator))
|
|
659
1314
|
queuesWithoutReplica.set(locator, queue);
|
|
660
1315
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const
|
|
665
|
-
const
|
|
666
|
-
|
|
1316
|
+
records.forEach((stored, index) => {
|
|
1317
|
+
if (fences.has(stored.locator))
|
|
1318
|
+
return;
|
|
1319
|
+
const model = restored(modelKey(stored.locator, 'collection'), stored.model);
|
|
1320
|
+
const selectionModel = restored(modelKey(stored.locator, 'typed-selection'), stored.selectionModel);
|
|
1321
|
+
const record = { ...stored, model, selectionModel };
|
|
1322
|
+
replicas.set(record.locator, {
|
|
1323
|
+
record,
|
|
1324
|
+
queue: queuedBatches.get(record.locator) ?? [],
|
|
1325
|
+
overlaid: held.overlaid.get(record.key) ?? new Set(),
|
|
1326
|
+
count: counts[index] ?? 0,
|
|
1327
|
+
confirmed: 0,
|
|
1328
|
+
indexed: !held.unindexed.has(record.key),
|
|
1329
|
+
});
|
|
667
1330
|
const existing = rooms.get(record.locator);
|
|
668
1331
|
if (existing === undefined)
|
|
669
1332
|
list(record, false);
|
|
@@ -682,16 +1345,21 @@ export function composeRuntime(options) {
|
|
|
682
1345
|
},
|
|
683
1346
|
});
|
|
684
1347
|
}
|
|
685
|
-
}
|
|
1348
|
+
});
|
|
1349
|
+
for (const key of held.orphans)
|
|
1350
|
+
await opened.release(key);
|
|
1351
|
+
if (held.erasing || held.orphans.length > 0)
|
|
1352
|
+
queueMicrotask(sweep);
|
|
686
1353
|
return {
|
|
687
|
-
lastCompleted,
|
|
1354
|
+
lastCompleted: held.lastCompleted,
|
|
688
1355
|
locators: new Set([...queuedBatches.keys(), ...replicas.keys()]),
|
|
689
|
-
read: mergeChangeSets([...
|
|
1356
|
+
read: mergeChangeSets([...replicas.keys()].map((locator) => changeSet(locator, { documents: null }))),
|
|
690
1357
|
};
|
|
691
1358
|
}
|
|
692
1359
|
async function readState(opened) {
|
|
693
1360
|
const { lastCompleted, locators, read } = await hydrate(opened);
|
|
694
1361
|
applyChangeSet(read);
|
|
1362
|
+
prepare();
|
|
695
1363
|
if (replicas.size > 0)
|
|
696
1364
|
emitRooms();
|
|
697
1365
|
if (member !== null)
|
|
@@ -704,8 +1372,7 @@ export function composeRuntime(options) {
|
|
|
704
1372
|
async function open() {
|
|
705
1373
|
if (closed)
|
|
706
1374
|
throw new Error('runtime closed');
|
|
707
|
-
const opened =
|
|
708
|
-
store = opened;
|
|
1375
|
+
const opened = openRuntimeStore(isFenced, database);
|
|
709
1376
|
const interrupted = await opened.fences();
|
|
710
1377
|
for (const locator of interrupted) {
|
|
711
1378
|
fences.set(locator, { at: discoveries, erased: false });
|
|
@@ -724,9 +1391,17 @@ export function composeRuntime(options) {
|
|
|
724
1391
|
emitStorageLoss();
|
|
725
1392
|
}
|
|
726
1393
|
const lastCompleted = await readState(opened);
|
|
727
|
-
if (interrupted.length > 0)
|
|
728
|
-
|
|
1394
|
+
if (interrupted.length > 0) {
|
|
1395
|
+
queueMicrotask(() => {
|
|
1396
|
+
void eraseFenced(interrupted).catch(() => {
|
|
1397
|
+
journal.record('erasure failed');
|
|
1398
|
+
});
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
729
1401
|
moveTo({ ...status, lastCompleted, ...attention() });
|
|
1402
|
+
void exclusive(() => unpair(opened)).catch(() => {
|
|
1403
|
+
journal.record('unpair failed');
|
|
1404
|
+
});
|
|
730
1405
|
return opened;
|
|
731
1406
|
}
|
|
732
1407
|
function durably(work) {
|
|
@@ -743,81 +1418,284 @@ export function composeRuntime(options) {
|
|
|
743
1418
|
return opening;
|
|
744
1419
|
}
|
|
745
1420
|
function exclusive(work) {
|
|
1421
|
+
gathering = null;
|
|
746
1422
|
const next = mutations.then(work, work);
|
|
747
1423
|
mutations = next.then(() => undefined, () => undefined);
|
|
748
1424
|
return next;
|
|
749
1425
|
}
|
|
750
|
-
function
|
|
751
|
-
const
|
|
752
|
-
|
|
1426
|
+
function gathered(key, input, merge, work) {
|
|
1427
|
+
const tail = gathering;
|
|
1428
|
+
if (tail !== null && tail.key === key) {
|
|
1429
|
+
const merged = merge(tail.input, input);
|
|
1430
|
+
if (merged !== null) {
|
|
1431
|
+
tail.input = merged;
|
|
1432
|
+
return tail.done;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
const entry = { key, input, done: Promise.resolve() };
|
|
1436
|
+
entry.done = exclusive(() => {
|
|
1437
|
+
if (gathering === entry)
|
|
1438
|
+
gathering = null;
|
|
1439
|
+
return work(entry.input);
|
|
1440
|
+
});
|
|
1441
|
+
gathering = entry;
|
|
1442
|
+
return entry.done;
|
|
1443
|
+
}
|
|
1444
|
+
function withdrawable(locator) {
|
|
1445
|
+
return (batch) => {
|
|
1446
|
+
const state = pendings.get(pendingKey(locator, batch.id));
|
|
1447
|
+
return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
const REWRITE_LINKS = ['field', 'body'];
|
|
1451
|
+
const CHECKED_LINKS = ['field', 'section'];
|
|
1452
|
+
async function sliceAt(record, view, queue, extra = [], moved = []) {
|
|
1453
|
+
const local = await openLocal();
|
|
1454
|
+
const needed = new Set([...touchedBy(queue), ...extra]);
|
|
1455
|
+
const movedPaths = [...new Set([...movedBy(queue, record.locator), ...moved])];
|
|
1456
|
+
if (movedPaths.length > 0) {
|
|
1457
|
+
for (const { path } of await local.referrers(record.key, view, movedPaths, REWRITE_LINKS)) {
|
|
1458
|
+
needed.add(path);
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
const paths = [...needed];
|
|
1462
|
+
return stepped(slicing(await local.entries(record.key, view, paths), paths));
|
|
1463
|
+
}
|
|
1464
|
+
async function landedWhole(locator, batch, confirmedLayer, current) {
|
|
1465
|
+
if (!landed(batch, confirmedLayer, current))
|
|
1466
|
+
return false;
|
|
1467
|
+
const partner = partnerOf(batch, locator);
|
|
1468
|
+
if (partner === null)
|
|
1469
|
+
return true;
|
|
1470
|
+
const other = replicas.get(partner);
|
|
1471
|
+
const half = other?.queue.find((one) => one.id === batch.id);
|
|
1472
|
+
if (other === undefined || half === undefined)
|
|
1473
|
+
return false;
|
|
1474
|
+
return landed(half, await sliceAt(other.record, 'confirmed', [half]));
|
|
1475
|
+
}
|
|
1476
|
+
function carriedBack(locator, unlanded, written) {
|
|
1477
|
+
const back = new Map();
|
|
1478
|
+
const taken = [];
|
|
1479
|
+
for (const batch of unlanded) {
|
|
1480
|
+
const between = batch.between;
|
|
1481
|
+
if (between === undefined)
|
|
1482
|
+
continue;
|
|
1483
|
+
const source = between.source.room;
|
|
1484
|
+
const destination = between.destination.room;
|
|
1485
|
+
for (const { path: to } of pendings.get(pendingKey(destination, batch.id))?.drafts ?? []) {
|
|
1486
|
+
const record = roomDrafts.get(destination)?.get(to);
|
|
1487
|
+
const path = between.moves.find((move) => move.to === to)?.path;
|
|
1488
|
+
if (record === undefined || path === undefined)
|
|
1489
|
+
continue;
|
|
1490
|
+
const [into, key] = source === locator ? [written, path] : [back, documentKey(source, path)];
|
|
1491
|
+
const existing = into.get(key) ?? roomDrafts.get(source)?.get(path);
|
|
1492
|
+
taken.push({ locator: destination, path: to });
|
|
1493
|
+
into.set(key, {
|
|
1494
|
+
locator: source,
|
|
1495
|
+
path,
|
|
1496
|
+
revision: roomDraftRevision(),
|
|
1497
|
+
parts: returnedInto(existing?.parts, record.parts),
|
|
1498
|
+
returned: existing?.returned ?? null,
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
const records = [...back.values()];
|
|
1503
|
+
const elsewhere = taken.filter((one) => one.locator !== locator);
|
|
1504
|
+
const rooms = new Set([...records, ...elsewhere].map((one) => one.locator));
|
|
1505
|
+
return {
|
|
1506
|
+
taken: taken.filter((one) => one.locator === locator).map(({ path }) => path),
|
|
1507
|
+
writes: [...rooms].map((room) => ({
|
|
1508
|
+
locator: room,
|
|
1509
|
+
roomDrafts: records.filter((record) => record.locator === room),
|
|
1510
|
+
consumedRoomDrafts: elsewhere.filter((one) => one.locator === room).map(({ path }) => path),
|
|
1511
|
+
})),
|
|
1512
|
+
changes: [...rooms].map((room) => changeSet(room, {
|
|
1513
|
+
drafts: [...records, ...elsewhere]
|
|
1514
|
+
.filter((one) => one.locator === room)
|
|
1515
|
+
.map(({ path }) => path),
|
|
1516
|
+
})),
|
|
1517
|
+
remember() {
|
|
1518
|
+
for (const one of taken)
|
|
1519
|
+
forgetRoomDraft(one.locator, one.path);
|
|
1520
|
+
for (const record of records)
|
|
1521
|
+
rememberRoomDraft(record);
|
|
1522
|
+
},
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
function committedRoom(locator, answer) {
|
|
1526
|
+
const indexed = answer.indexed[locator] ?? replicas.get(locator)?.indexed ?? false;
|
|
1527
|
+
if (!indexed)
|
|
1528
|
+
prepare([locator]);
|
|
1529
|
+
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0, indexed };
|
|
1530
|
+
}
|
|
1531
|
+
function replayed(record, confirmedLayer, queue) {
|
|
1532
|
+
return stepped(replaying(confirmedLayer, queue, record.locator, compositionOf(record)));
|
|
1533
|
+
}
|
|
1534
|
+
async function overlayDiff(record, before, visible, touched) {
|
|
1535
|
+
const kept = before !== undefined && before.record.key === record.key
|
|
1536
|
+
? before.overlaid
|
|
1537
|
+
: new Set();
|
|
1538
|
+
return {
|
|
1539
|
+
overlay: {
|
|
1540
|
+
set: await stepped(overlayOf(visible, touched)),
|
|
1541
|
+
cleared: [...kept].filter((path) => !touched.has(path)),
|
|
1542
|
+
},
|
|
1543
|
+
touched,
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
async function settle(local, locator, before, record, queue, settling = {}) {
|
|
1547
|
+
return together(local, [await settlement(local, locator, before, record, queue, settling)]);
|
|
1548
|
+
}
|
|
1549
|
+
function unpairedIn(locator, batch) {
|
|
1550
|
+
if (!queueIn(locator).some((held) => held.id === batch.id))
|
|
1551
|
+
return false;
|
|
1552
|
+
if (pendings.get(pendingKey(locator, batch.id))?.accepted !== undefined)
|
|
1553
|
+
return false;
|
|
1554
|
+
return !leavingIn(batch, locator) || withdrawable(locator)(batch);
|
|
1555
|
+
}
|
|
1556
|
+
async function together(local, settlements) {
|
|
1557
|
+
const settled = new Set(settlements.map(({ locator }) => locator));
|
|
1558
|
+
const all = [...settlements];
|
|
1559
|
+
for (let wave = settlements; wave.length > 0;) {
|
|
1560
|
+
const partnered = new Map();
|
|
1561
|
+
for (const { locator, left } of wave) {
|
|
1562
|
+
for (const batch of left) {
|
|
1563
|
+
const partner = partnerOf(batch, locator);
|
|
1564
|
+
if (partner === null || settled.has(partner) || !unpairedIn(partner, batch))
|
|
1565
|
+
continue;
|
|
1566
|
+
partnered.set(partner, [...(partnered.get(partner) ?? []), batch]);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
const partners = [];
|
|
1570
|
+
for (const [partner, batches] of partnered) {
|
|
1571
|
+
settled.add(partner);
|
|
1572
|
+
partners.push(await unpairing(local, partner, batches));
|
|
1573
|
+
}
|
|
1574
|
+
all.push(...partners);
|
|
1575
|
+
wave = partners;
|
|
1576
|
+
}
|
|
1577
|
+
const answer = await local.commit({
|
|
1578
|
+
rooms: all.flatMap(({ commit }) => commit.rooms),
|
|
1579
|
+
writes: all.flatMap(({ commit }) => commit.writes),
|
|
1580
|
+
});
|
|
1581
|
+
return mergeChangeSets(all.map((one) => one.apply(answer)));
|
|
753
1582
|
}
|
|
754
|
-
async function
|
|
1583
|
+
async function settlement(local, locator, before, record, queue, settling = {}) {
|
|
1584
|
+
const view = settling.view ?? 'confirmed';
|
|
755
1585
|
const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
756
|
-
const
|
|
757
|
-
const
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
1586
|
+
const overrides = settling.changes;
|
|
1587
|
+
const read = await sliceAt(record, view, queue, [
|
|
1588
|
+
...(before !== undefined && before.record.key === record.key ? before.overlaid : []),
|
|
1589
|
+
...(overrides?.set.map(({ path }) => path) ?? []),
|
|
1590
|
+
...(overrides?.removed ?? []),
|
|
1591
|
+
]);
|
|
1592
|
+
const confirmedLayer = overrides === undefined
|
|
1593
|
+
? read
|
|
1594
|
+
: await stepped(slicingWith(read, overrides.set, overrides.removed));
|
|
1595
|
+
const composition = compositionOf(record);
|
|
1596
|
+
const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
|
|
1597
|
+
const founded = kept.filter((batch) => !queue.includes(batch));
|
|
1598
|
+
const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
|
|
1599
|
+
const unqueued = [];
|
|
1600
|
+
for (const batch of withdrawn) {
|
|
1601
|
+
unqueued.push({ batch, landed: await landedWhole(locator, batch, confirmedLayer) });
|
|
1602
|
+
}
|
|
762
1603
|
const returning = [
|
|
763
|
-
...[...unqueued].reverse(),
|
|
764
|
-
|
|
1604
|
+
...[...unqueued].reverse().map(({ batch, landed: held }) => ({
|
|
1605
|
+
batch,
|
|
1606
|
+
returned: localSaves || held ? null : returnedBy('withdrawn'),
|
|
1607
|
+
})),
|
|
1608
|
+
...(settling.leaving ?? []),
|
|
765
1609
|
];
|
|
766
1610
|
const written = new Map();
|
|
767
|
-
for (const { batch, returned } of returning) {
|
|
768
|
-
if (
|
|
1611
|
+
for (const { batch, returned: reason } of returning) {
|
|
1612
|
+
if (reason === null || batch.between?.destination.room === locator)
|
|
769
1613
|
continue;
|
|
770
|
-
for (const saved of pendings.get(batch.id)?.drafts ?? []) {
|
|
1614
|
+
for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
|
|
771
1615
|
const existing = written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path);
|
|
772
1616
|
written.set(saved.path, {
|
|
773
1617
|
locator,
|
|
774
1618
|
path: saved.path,
|
|
775
1619
|
revision: roomDraftRevision(),
|
|
776
1620
|
parts: returnedInto(existing?.parts, saved.parts),
|
|
777
|
-
returned,
|
|
1621
|
+
returned: reason,
|
|
778
1622
|
});
|
|
779
1623
|
}
|
|
780
1624
|
}
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
1625
|
+
const carried = carriedBack(locator, [
|
|
1626
|
+
...[...unqueued].reverse().flatMap(({ batch, landed: held }) => (held ? [] : [batch])),
|
|
1627
|
+
...(settling.leaving ?? []).flatMap(({ batch, returned }) => returned === null ? [] : [batch]),
|
|
1628
|
+
], written);
|
|
1629
|
+
const replayedLayer = await replayed(record, confirmedLayer, kept);
|
|
1630
|
+
const { overlay, touched } = await overlayDiff(record, before, replayedLayer.visible, replayedLayer.touched);
|
|
1631
|
+
const commit = {
|
|
1632
|
+
rooms: [
|
|
1633
|
+
{
|
|
1634
|
+
locator,
|
|
1635
|
+
key: record.key,
|
|
1636
|
+
record: storedRecord(record),
|
|
1637
|
+
roots: record.typedSelection?.roots ?? [],
|
|
1638
|
+
...settling.commit,
|
|
1639
|
+
...(overrides === undefined
|
|
1640
|
+
? {}
|
|
1641
|
+
: { confirmed: { set: overrides.set, removed: overrides.removed } }),
|
|
1642
|
+
overlay,
|
|
1643
|
+
},
|
|
1644
|
+
],
|
|
1645
|
+
writes: [
|
|
1646
|
+
{
|
|
1647
|
+
locator,
|
|
1648
|
+
queue: kept,
|
|
1649
|
+
founded,
|
|
1650
|
+
roomDrafts: [...written.values()],
|
|
1651
|
+
consumedRoomDrafts: carried.taken,
|
|
1652
|
+
},
|
|
1653
|
+
...carried.writes,
|
|
1654
|
+
],
|
|
1655
|
+
};
|
|
1656
|
+
const staying = new Set(kept.map((batch) => batch.id));
|
|
1657
|
+
return {
|
|
1658
|
+
locator,
|
|
1659
|
+
left: previousQueue.filter((batch) => !staying.has(batch.id)),
|
|
1660
|
+
commit,
|
|
1661
|
+
apply: (answer) => {
|
|
1662
|
+
for (const batch of previousQueue) {
|
|
1663
|
+
if (!staying.has(batch.id))
|
|
1664
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
1665
|
+
}
|
|
1666
|
+
for (const draft of written.values())
|
|
1667
|
+
rememberRoomDraft(draft);
|
|
1668
|
+
carried.remember();
|
|
1669
|
+
const drafts = [...written.keys(), ...carried.taken];
|
|
802
1670
|
queuesWithoutReplica.delete(locator);
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
1671
|
+
for (const { batch, landed: held } of unqueued) {
|
|
1672
|
+
const outcome = localSaves ? 'dropped' : held ? 'landed' : 'withdrawn';
|
|
1673
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
1674
|
+
}
|
|
1675
|
+
const changed = answer.changed[locator];
|
|
1676
|
+
const confirmedChanged = settling.commit?.published !== undefined ||
|
|
1677
|
+
settling.commit?.refreshed === true ||
|
|
1678
|
+
overrides !== undefined;
|
|
1679
|
+
const { count, indexed } = committedRoom(locator, answer);
|
|
1680
|
+
replicas.set(locator, {
|
|
1681
|
+
record,
|
|
1682
|
+
queue: kept,
|
|
1683
|
+
overlaid: touched,
|
|
1684
|
+
count,
|
|
1685
|
+
confirmed: (before?.confirmed ?? 0) + (confirmedChanged ? 1 : 0),
|
|
1686
|
+
indexed,
|
|
1687
|
+
});
|
|
1688
|
+
return mergeChangeSets([
|
|
1689
|
+
changeSet(locator, {
|
|
1690
|
+
documents: changed === undefined ? [] : changed,
|
|
1691
|
+
room: settling.room,
|
|
1692
|
+
queue: queueChanged,
|
|
1693
|
+
drafts,
|
|
1694
|
+
}),
|
|
1695
|
+
...carried.changes,
|
|
1696
|
+
]);
|
|
1697
|
+
},
|
|
1698
|
+
};
|
|
821
1699
|
}
|
|
822
1700
|
function adoptModels(locator, record) {
|
|
823
1701
|
for (const [attached, model] of [
|
|
@@ -837,7 +1715,7 @@ export function composeRuntime(options) {
|
|
|
837
1715
|
const taken = (answer, previous, attached) => {
|
|
838
1716
|
if (answer === undefined)
|
|
839
1717
|
return previous;
|
|
840
|
-
const { state, refused } = attachedModel(answer, previous);
|
|
1718
|
+
const { state, refused } = attachedModel(answer, previous, attached === 'collection' ? roomTypes : NO_ROOM_TYPES);
|
|
841
1719
|
if (refused !== null) {
|
|
842
1720
|
const where = refused.location === undefined ? '' : ` ${refused.location}`;
|
|
843
1721
|
journal.record(`model invalid ${locator} ${attached} ${refused.identity} ${refused.rule}${where}`);
|
|
@@ -850,48 +1728,85 @@ export function composeRuntime(options) {
|
|
|
850
1728
|
selection: taken(answers?.selection, held.selection, 'typed-selection'),
|
|
851
1729
|
};
|
|
852
1730
|
}
|
|
853
|
-
function publish(local, room,
|
|
854
|
-
const
|
|
855
|
-
|
|
856
|
-
|
|
1731
|
+
function publish(local, room, record, landing, live) {
|
|
1732
|
+
const landed = exclusive(async () => {
|
|
1733
|
+
if (!live()) {
|
|
1734
|
+
caughtUpTo(room.locator);
|
|
857
1735
|
return true;
|
|
1736
|
+
}
|
|
858
1737
|
const prior = replicas.get(room.locator);
|
|
859
|
-
if (
|
|
860
|
-
(prior
|
|
861
|
-
|
|
1738
|
+
if (landing.kind === 'refreshed' &&
|
|
1739
|
+
(prior === undefined || prior.record.key !== record.key || prior.confirmed !== landing.base)) {
|
|
1740
|
+
await local.unrefresh(record.key);
|
|
862
1741
|
return false;
|
|
863
1742
|
}
|
|
864
|
-
adoptModels(room.locator,
|
|
1743
|
+
adoptModels(room.locator, record);
|
|
865
1744
|
const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
|
|
866
|
-
const roomVersion =
|
|
1745
|
+
const roomVersion = record.roomVersion;
|
|
867
1746
|
const accepted = queue.filter((batch) => {
|
|
868
|
-
const
|
|
869
|
-
return
|
|
1747
|
+
const at = pendings.get(pendingKey(room.locator, batch.id))?.accepted;
|
|
1748
|
+
return at !== undefined && options.adapter.reached(room.locator, roomVersion, at);
|
|
870
1749
|
});
|
|
871
|
-
|
|
872
|
-
|
|
1750
|
+
let set;
|
|
1751
|
+
try {
|
|
1752
|
+
set = await settle(local, room.locator, prior, record, queue.filter((batch) => !accepted.includes(batch)), landing.kind === 'refreshed'
|
|
1753
|
+
? {
|
|
1754
|
+
room: true,
|
|
1755
|
+
view: 'refreshed',
|
|
1756
|
+
commit: { refreshed: true, composition: compositionOf(record) },
|
|
1757
|
+
}
|
|
1758
|
+
: {
|
|
1759
|
+
room: true,
|
|
1760
|
+
view: landing.refreshed ? 'refreshed' : 'confirmed',
|
|
1761
|
+
commit: {
|
|
1762
|
+
published: { from: prior?.record.key ?? null },
|
|
1763
|
+
composition: compositionOf(record),
|
|
1764
|
+
...(landing.refreshed ? { refreshed: true } : {}),
|
|
1765
|
+
},
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
catch (error) {
|
|
1769
|
+
if (!refusedForSpace(error))
|
|
1770
|
+
throw error;
|
|
1771
|
+
caughtUpTo(room.locator);
|
|
1772
|
+
return true;
|
|
1773
|
+
}
|
|
1774
|
+
if (landing.kind === 'bootstrapped') {
|
|
873
1775
|
transfers.delete(room.locator);
|
|
1776
|
+
catching.delete(room.locator);
|
|
1777
|
+
sweep();
|
|
1778
|
+
}
|
|
874
1779
|
for (const batch of accepted)
|
|
875
|
-
pendings.delete(batch.id);
|
|
1780
|
+
pendings.delete(pendingKey(room.locator, batch.id));
|
|
876
1781
|
stale.delete(room.locator);
|
|
1782
|
+
unpreparable.delete(room.locator);
|
|
877
1783
|
if (unreadable.delete(room.locator))
|
|
878
1784
|
scopedRefusals.delete(room.locator);
|
|
879
1785
|
sourceRefusals.delete(room.locator);
|
|
880
1786
|
declarationRefusals.delete(room.locator);
|
|
881
1787
|
list(room, true);
|
|
882
1788
|
publishChangeSet(set);
|
|
1789
|
+
await unpair(local);
|
|
883
1790
|
moveTo({ ...status, ...attention() });
|
|
884
1791
|
options.adapter.roomStream?.published(room.locator, roomVersion);
|
|
1792
|
+
if (accepted.length > 0 && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
|
|
1793
|
+
submitLater(room.locator);
|
|
1794
|
+
}
|
|
885
1795
|
return true;
|
|
886
1796
|
});
|
|
1797
|
+
return landing.kind === 'refreshed' ? landed : landed.finally(() => installed(room.locator));
|
|
887
1798
|
}
|
|
888
|
-
async function
|
|
1799
|
+
async function savedRoom(locator) {
|
|
889
1800
|
const local = await openLocal();
|
|
890
1801
|
const replica = replicas.get(locator);
|
|
891
1802
|
const room = rooms.get(locator);
|
|
892
1803
|
if (replica === undefined || room === undefined) {
|
|
893
1804
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
894
1805
|
}
|
|
1806
|
+
return { local, replica, room };
|
|
1807
|
+
}
|
|
1808
|
+
async function saveRoomDrafts(locator, named, writtenBy) {
|
|
1809
|
+
const target = await savedRoom(locator);
|
|
895
1810
|
const saved = [];
|
|
896
1811
|
for (const { path, revision } of named) {
|
|
897
1812
|
const record = roomDrafts.get(locator)?.get(path);
|
|
@@ -901,12 +1816,398 @@ export function composeRuntime(options) {
|
|
|
901
1816
|
if (!saved.some((draft) => draft.path === path))
|
|
902
1817
|
saved.push({ path, parts: record.parts });
|
|
903
1818
|
}
|
|
904
|
-
|
|
1819
|
+
return committed(locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
|
|
1820
|
+
}
|
|
1821
|
+
async function saveParts(locator, named, writtenBy) {
|
|
1822
|
+
const read = named.map(({ path, parts }) => ({ path, parts: readParts(parts) }));
|
|
1823
|
+
if (read.some(({ path }, at) => path === '' ||
|
|
1824
|
+
normalize(path) !== path ||
|
|
1825
|
+
read.findIndex((one) => one.path === path) !== at)) {
|
|
1826
|
+
throw runtimeError('room-draft-malformed');
|
|
1827
|
+
}
|
|
1828
|
+
const target = await savedRoom(locator);
|
|
1829
|
+
const saved = [];
|
|
1830
|
+
const files = [];
|
|
1831
|
+
const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
|
|
1832
|
+
const visible = filed.length === 0 ? undefined : await visibleOf(locator, filed);
|
|
1833
|
+
for (const { path, parts } of read) {
|
|
1834
|
+
const file = parts.file === undefined
|
|
1835
|
+
? undefined
|
|
1836
|
+
: await heldFile(parts.file.blob, binaryBase(visible, path));
|
|
1837
|
+
if (parts.file !== undefined && file !== undefined) {
|
|
1838
|
+
files.push({ key: file.localId, blob: parts.file.blob });
|
|
1839
|
+
}
|
|
1840
|
+
saved.push({ path, parts: held(undefined, parts, file) });
|
|
1841
|
+
}
|
|
1842
|
+
return committed(locator, target, saved, new Set(), writtenBy, files);
|
|
1843
|
+
}
|
|
1844
|
+
function carriedDrafts(locator, saved, consumed) {
|
|
1845
|
+
const holding = roomDrafts.get(locator);
|
|
1846
|
+
return new Map(saved.flatMap(({ path, parts }) => {
|
|
1847
|
+
const record = holding?.get(path);
|
|
1848
|
+
if (parts.move === undefined || record === undefined || consumed.has(path))
|
|
1849
|
+
return [];
|
|
1850
|
+
const to = moveTarget(parts.move);
|
|
1851
|
+
if (to === path || holding?.has(to) === true)
|
|
1852
|
+
return [];
|
|
1853
|
+
return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
|
|
1854
|
+
}));
|
|
1855
|
+
}
|
|
1856
|
+
async function savingIn(replica, changes, extra, leaving = []) {
|
|
1857
|
+
const record = replica.record;
|
|
1858
|
+
const local = await openLocal();
|
|
1859
|
+
const paths = [
|
|
1860
|
+
...extra,
|
|
1861
|
+
...changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]),
|
|
1862
|
+
];
|
|
1863
|
+
const moved = [
|
|
1864
|
+
...leaving,
|
|
1865
|
+
...changes.flatMap((change) => (change.kind === 'move' ? [change.path] : [])),
|
|
1866
|
+
];
|
|
1867
|
+
const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
|
|
1868
|
+
const referring = removed.length === 0
|
|
1869
|
+
? []
|
|
1870
|
+
: (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
|
|
1871
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...referring], moved);
|
|
1872
|
+
const { visible } = await replayed(record, confirmedLayer, replica.queue);
|
|
1873
|
+
const composition = compositionOf(record);
|
|
1874
|
+
return {
|
|
1875
|
+
record,
|
|
1876
|
+
confirmed: confirmedLayer,
|
|
1877
|
+
visible,
|
|
1878
|
+
*visibleOver(queue) {
|
|
1879
|
+
return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
|
|
1880
|
+
},
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
async function unmodeledIn(record) {
|
|
1884
|
+
const composition = compositionFor(record);
|
|
1885
|
+
if (available(composition))
|
|
1886
|
+
return false;
|
|
1887
|
+
const local = await openLocal();
|
|
1888
|
+
const roots = record.typedSelection?.roots ?? [];
|
|
1889
|
+
if (composition.model.status === 'unavailable' &&
|
|
1890
|
+
(await local.typedWithin(record.key, 'visible', roots, false))) {
|
|
1891
|
+
return true;
|
|
1892
|
+
}
|
|
1893
|
+
return (composition.selection !== null &&
|
|
1894
|
+
composition.selection.model.status === 'unavailable' &&
|
|
1895
|
+
(await local.typedWithin(record.key, 'visible', roots, true)));
|
|
1896
|
+
}
|
|
1897
|
+
function textDiffers(left, right) {
|
|
1898
|
+
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
1899
|
+
!sameBinary(left?.binary ?? undefined, right?.binary ?? undefined));
|
|
1900
|
+
}
|
|
1901
|
+
function textOf(entry) {
|
|
1902
|
+
if (entry === null)
|
|
1903
|
+
return undefined;
|
|
1904
|
+
return entry.binary !== null ? null : entry.content;
|
|
1905
|
+
}
|
|
1906
|
+
function walkOf(local, key, view) {
|
|
1907
|
+
return async (after) => {
|
|
1908
|
+
const page = await local.walk(key, view, after, WALK_BYTES);
|
|
1909
|
+
return {
|
|
1910
|
+
entries: page.entries.map(({ path, content, binary }) => ({
|
|
1911
|
+
path,
|
|
1912
|
+
content: binary !== null ? null : content,
|
|
1913
|
+
})),
|
|
1914
|
+
next: page.next,
|
|
1915
|
+
};
|
|
1916
|
+
};
|
|
1917
|
+
}
|
|
1918
|
+
function lookupOf(local, key, view) {
|
|
1919
|
+
return async (paths) => {
|
|
1920
|
+
if (paths.length === 0)
|
|
1921
|
+
return new Map();
|
|
1922
|
+
return new Map((await local.typed(key, view, paths)).map(({ path, type, binary }) => [
|
|
1923
|
+
path,
|
|
1924
|
+
{ type: type ?? undefined, binary },
|
|
1925
|
+
]));
|
|
1926
|
+
};
|
|
1927
|
+
}
|
|
1928
|
+
function declaredOf(entry) {
|
|
1929
|
+
if (entry.binary !== null)
|
|
1930
|
+
return { type: undefined, binary: true };
|
|
1931
|
+
const read = parseFrontmatter(entry.content ?? '');
|
|
1932
|
+
return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
|
|
1933
|
+
}
|
|
1934
|
+
function overriddenWalk(walk, overrides) {
|
|
1935
|
+
const sorted = [...overrides.keys()].sort();
|
|
1936
|
+
const beyond = (path) => {
|
|
1937
|
+
if (path === null)
|
|
1938
|
+
return 0;
|
|
1939
|
+
let low = 0;
|
|
1940
|
+
let high = sorted.length;
|
|
1941
|
+
while (low < high) {
|
|
1942
|
+
const middle = (low + high) >>> 1;
|
|
1943
|
+
if (sorted[middle] <= path)
|
|
1944
|
+
low = middle + 1;
|
|
1945
|
+
else
|
|
1946
|
+
high = middle;
|
|
1947
|
+
}
|
|
1948
|
+
return low;
|
|
1949
|
+
};
|
|
1950
|
+
return async (after) => {
|
|
1951
|
+
const page = await walk(after);
|
|
1952
|
+
const end = page.next;
|
|
1953
|
+
const added = sorted
|
|
1954
|
+
.slice(beyond(after), end === null ? sorted.length : beyond(end))
|
|
1955
|
+
.flatMap((path) => {
|
|
1956
|
+
const entry = overrides.get(path);
|
|
1957
|
+
return entry === null || entry === undefined
|
|
1958
|
+
? []
|
|
1959
|
+
: [{ path, content: entry.binary !== null ? null : entry.content }];
|
|
1960
|
+
});
|
|
1961
|
+
const entries = [...page.entries.filter(({ path }) => !overrides.has(path)), ...added].sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
1962
|
+
return { entries, next: end };
|
|
1963
|
+
};
|
|
1964
|
+
}
|
|
1965
|
+
function overriddenLookup(lookup, overrides) {
|
|
1966
|
+
return async (paths) => {
|
|
1967
|
+
const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
|
|
1968
|
+
for (const path of paths) {
|
|
1969
|
+
const entry = overrides.get(path);
|
|
1970
|
+
if (entry === undefined)
|
|
1971
|
+
continue;
|
|
1972
|
+
if (entry === null)
|
|
1973
|
+
known.delete(path);
|
|
1974
|
+
else
|
|
1975
|
+
known.set(path, declaredOf(entry));
|
|
1976
|
+
}
|
|
1977
|
+
return known;
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
function validatorOf(record, confirmedLayer) {
|
|
1981
|
+
return async (resulting, touched) => {
|
|
1982
|
+
const composition = compositionFor(record);
|
|
1983
|
+
const local = await openLocal();
|
|
1984
|
+
const changed = [];
|
|
1985
|
+
const overrides = new Map();
|
|
1986
|
+
const pace = paced();
|
|
1987
|
+
for (const path of new Set([...confirmedLayer.held, ...touched])) {
|
|
1988
|
+
await pace(ROW_WEIGHT);
|
|
1989
|
+
const was = entryAt(confirmedLayer, path);
|
|
1990
|
+
const now = entryAt(resulting, path);
|
|
1991
|
+
if (!textDiffers(was, now))
|
|
1992
|
+
continue;
|
|
1993
|
+
overrides.set(path, now);
|
|
1994
|
+
if (textOf(was) !== textOf(now))
|
|
1995
|
+
changed.push({ path, was: textOf(was), now: textOf(now) });
|
|
1996
|
+
}
|
|
1997
|
+
const lookup = overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides);
|
|
1998
|
+
const clean = available(composition) &&
|
|
1999
|
+
changed.length <= CLEAN_CHANGES &&
|
|
2000
|
+
(await changedCleanly(changed, compositionOf(record), composition, {
|
|
2001
|
+
lookup,
|
|
2002
|
+
texts: textsOf(local, record.key, 'confirmed'),
|
|
2003
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2004
|
+
}));
|
|
2005
|
+
if (clean)
|
|
2006
|
+
return { verdict: 'accept' };
|
|
2007
|
+
return validatedCollection(overriddenWalk(walkOf(local, record.key, 'confirmed'), overrides), lookup, composition);
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
function textsOf(local, key, view) {
|
|
2011
|
+
return async (paths) => new Map((await local.entries(key, view, paths)).map(({ path, content, binary }) => [
|
|
2012
|
+
path,
|
|
2013
|
+
binary !== null ? null : content,
|
|
2014
|
+
]));
|
|
2015
|
+
}
|
|
2016
|
+
function referrersOf(local, key, view) {
|
|
2017
|
+
return async (targets) => [
|
|
2018
|
+
...new Set((await local.referrers(key, view, targets, CHECKED_LINKS)).map(({ path }) => path)),
|
|
2019
|
+
];
|
|
2020
|
+
}
|
|
2021
|
+
async function sideOf(locator, replica, room, saving) {
|
|
2022
|
+
const record = replica.record;
|
|
2023
|
+
return {
|
|
2024
|
+
room: locator,
|
|
2025
|
+
handle: room.handle,
|
|
2026
|
+
baseRoomVersion: record.roomVersion,
|
|
2027
|
+
composition: compositionFor(record),
|
|
2028
|
+
operations: room.operations,
|
|
2029
|
+
providerOwnedPaths: room.providerOwnedPaths,
|
|
2030
|
+
visible: saving.visible,
|
|
2031
|
+
queued: queuedDocuments(replica.queue),
|
|
2032
|
+
unmodeled: await unmodeledIn(record),
|
|
2033
|
+
validate: validatorOf(record, saving.confirmed),
|
|
2034
|
+
};
|
|
2035
|
+
}
|
|
2036
|
+
function movedInto(locator, saved) {
|
|
2037
|
+
const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
|
|
2038
|
+
return into?.parts.move?.room ?? null;
|
|
2039
|
+
}
|
|
2040
|
+
async function saveCommit(local, files, rooms) {
|
|
2041
|
+
const touched = new Map();
|
|
2042
|
+
const commits = [];
|
|
2043
|
+
for (const { locator, saving, before, queue } of rooms) {
|
|
2044
|
+
const replayedLayer = await replayed(saving.record, saving.confirmed, queue);
|
|
2045
|
+
const diff = await overlayDiff(saving.record, before, replayedLayer.visible, replayedLayer.touched);
|
|
2046
|
+
touched.set(locator, diff.touched);
|
|
2047
|
+
commits.push({
|
|
2048
|
+
locator,
|
|
2049
|
+
key: saving.record.key,
|
|
2050
|
+
roots: saving.record.typedSelection?.roots ?? [],
|
|
2051
|
+
overlay: diff.overlay,
|
|
2052
|
+
});
|
|
2053
|
+
}
|
|
2054
|
+
try {
|
|
2055
|
+
const answer = await local.commit({
|
|
2056
|
+
rooms: commits,
|
|
2057
|
+
writes: rooms.map(({ write }) => write),
|
|
2058
|
+
files,
|
|
2059
|
+
whole: true,
|
|
2060
|
+
});
|
|
2061
|
+
return { answer, touched };
|
|
2062
|
+
}
|
|
2063
|
+
catch {
|
|
2064
|
+
return null;
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
function savedReplica(locator, before, queue, touched, answer) {
|
|
2068
|
+
replicas.set(locator, {
|
|
2069
|
+
...before,
|
|
2070
|
+
queue,
|
|
2071
|
+
overlaid: touched,
|
|
2072
|
+
count: answer.counts[locator] ?? before.count,
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
async function committedMove(locator, destination, { local, replica, room }, saved, consumed, writtenBy, files) {
|
|
2076
|
+
const mixed = saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
|
|
2077
|
+
if (mixed !== undefined) {
|
|
2078
|
+
return { outcome: 'refused', code: 'mixed-room-move', retryable: false, file: mixed.path };
|
|
2079
|
+
}
|
|
2080
|
+
const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
2081
|
+
const sourceSaving = await savingIn(replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
|
|
2082
|
+
const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, sourceSaving.visible)));
|
|
2083
|
+
if (changed.length > 0)
|
|
2084
|
+
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
2085
|
+
const target = replicas.get(destination);
|
|
2086
|
+
const listedRoom = rooms.get(destination);
|
|
2087
|
+
if (target === undefined || listedRoom === undefined) {
|
|
2088
|
+
throw runtimeError('room-not-held', `No Local Replica for ${destination}`);
|
|
2089
|
+
}
|
|
2090
|
+
const arriving = await savingIn(target, [], moves.map(({ to }) => to));
|
|
2091
|
+
const built = await buildMove({
|
|
2092
|
+
source: await sideOf(locator, replica, room, sourceSaving),
|
|
2093
|
+
destination: await sideOf(destination, target, listedRoom, arriving),
|
|
2094
|
+
moves,
|
|
2095
|
+
providerLimits: limits,
|
|
2096
|
+
local: localSaves,
|
|
2097
|
+
...(writtenBy === undefined ? {} : { writtenBy }),
|
|
2098
|
+
});
|
|
2099
|
+
if (built.kind === 'refused')
|
|
2100
|
+
return { outcome: 'refused', ...built.refusal };
|
|
2101
|
+
const id = built.leaving.id;
|
|
2102
|
+
const carrying = saved.flatMap(({ path, parts }) => {
|
|
2103
|
+
const record = roomDrafts.get(locator)?.get(path);
|
|
2104
|
+
const to = moveTarget(parts.move);
|
|
2105
|
+
if (record === undefined || consumed.has(path) || roomDrafts.get(destination)?.has(to)) {
|
|
2106
|
+
return [];
|
|
2107
|
+
}
|
|
2108
|
+
const moved = {
|
|
2109
|
+
...record,
|
|
2110
|
+
locator: destination,
|
|
2111
|
+
path: to,
|
|
2112
|
+
revision: roomDraftRevision(),
|
|
2113
|
+
};
|
|
2114
|
+
return [{ from: path, record: moved }];
|
|
2115
|
+
});
|
|
2116
|
+
const carried = carrying.map(({ record }) => record);
|
|
2117
|
+
const marked = carried.map(({ path }) => ({ path, parts: {} }));
|
|
2118
|
+
const left = [...consumed, ...carrying.map(({ from }) => from)];
|
|
2119
|
+
const leavingQueue = [...replica.queue, built.leaving];
|
|
2120
|
+
const arrivingQueue = [...target.queue, built.arriving];
|
|
2121
|
+
const committedBoth = await saveCommit(local, files, [
|
|
2122
|
+
{
|
|
2123
|
+
locator,
|
|
2124
|
+
saving: sourceSaving,
|
|
2125
|
+
before: replica,
|
|
2126
|
+
queue: leavingQueue,
|
|
2127
|
+
write: {
|
|
2128
|
+
locator,
|
|
2129
|
+
queue: leavingQueue,
|
|
2130
|
+
saved: new Map([[id, saved]]),
|
|
2131
|
+
consumedRoomDrafts: left,
|
|
2132
|
+
},
|
|
2133
|
+
},
|
|
2134
|
+
{
|
|
2135
|
+
locator: destination,
|
|
2136
|
+
saving: arriving,
|
|
2137
|
+
before: target,
|
|
2138
|
+
queue: arrivingQueue,
|
|
2139
|
+
write: {
|
|
2140
|
+
locator: destination,
|
|
2141
|
+
queue: arrivingQueue,
|
|
2142
|
+
saved: new Map([[id, marked]]),
|
|
2143
|
+
roomDrafts: carried,
|
|
2144
|
+
},
|
|
2145
|
+
},
|
|
2146
|
+
]);
|
|
2147
|
+
if (committedBoth === null)
|
|
2148
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2149
|
+
const { answer, touched } = committedBoth;
|
|
2150
|
+
if (answer.fenced !== null) {
|
|
2151
|
+
throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
|
|
2152
|
+
}
|
|
2153
|
+
if (fences.has(locator) || fences.has(destination)) {
|
|
2154
|
+
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
2155
|
+
}
|
|
2156
|
+
pendings.set(pendingKey(locator, id), {
|
|
2157
|
+
drafts: saved,
|
|
2158
|
+
submission: { state: 'waiting' },
|
|
2159
|
+
lost: false,
|
|
2160
|
+
});
|
|
2161
|
+
pendings.set(pendingKey(destination, id), {
|
|
2162
|
+
drafts: marked,
|
|
2163
|
+
submission: { state: 'waiting' },
|
|
2164
|
+
lost: false,
|
|
2165
|
+
});
|
|
2166
|
+
for (const path of left)
|
|
2167
|
+
forgetRoomDraft(locator, path);
|
|
2168
|
+
for (const record of carried)
|
|
2169
|
+
rememberRoomDraft(record);
|
|
2170
|
+
savedReplica(locator, replica, leavingQueue, touched.get(locator), answer);
|
|
2171
|
+
savedReplica(destination, target, arrivingQueue, touched.get(destination), answer);
|
|
2172
|
+
publishChangeSet(mergeChangeSets([
|
|
2173
|
+
changeSet(locator, {
|
|
2174
|
+
documents: answer.changed[locator] ?? [],
|
|
2175
|
+
queue: true,
|
|
2176
|
+
drafts: left,
|
|
2177
|
+
}),
|
|
2178
|
+
changeSet(destination, {
|
|
2179
|
+
documents: answer.changed[destination] ?? [],
|
|
2180
|
+
queue: true,
|
|
2181
|
+
drafts: carried.map(({ path }) => path),
|
|
2182
|
+
}),
|
|
2183
|
+
]));
|
|
2184
|
+
queuedSave(locator);
|
|
2185
|
+
return { outcome: 'saved', identifier: id };
|
|
2186
|
+
}
|
|
2187
|
+
function queuedSave(locator) {
|
|
2188
|
+
if (!localSaves)
|
|
2189
|
+
recorded = false;
|
|
2190
|
+
moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
|
|
2191
|
+
submitLater(locator);
|
|
2192
|
+
}
|
|
2193
|
+
async function committed(locator, target, saved, consumed, writtenBy, files = []) {
|
|
2194
|
+
if (full)
|
|
2195
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2196
|
+
const into = movedInto(locator, saved);
|
|
2197
|
+
if (into !== null) {
|
|
2198
|
+
return committedMove(locator, into, target, saved, consumed, writtenBy, files);
|
|
2199
|
+
}
|
|
2200
|
+
const { local, replica, room } = target;
|
|
2201
|
+
const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
2202
|
+
const changes = parts.flatMap(({ changes: made }) => made);
|
|
2203
|
+
const saving = await savingIn(replica, changes, [
|
|
2204
|
+
...saved.map(({ path }) => path),
|
|
2205
|
+
...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
|
|
2206
|
+
]);
|
|
2207
|
+
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, underneath(held, path, saving.visible)));
|
|
905
2208
|
if (changed.length > 0)
|
|
906
2209
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
907
|
-
const
|
|
908
|
-
const changes = parts.flatMap(({ changes }) => changes);
|
|
909
|
-
const requeue = requeued(replica.queue, changes, replica.visible, withdrawable, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
|
|
2210
|
+
const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(locator), saving.visibleOver));
|
|
910
2211
|
if (requeue.kind === 'refused')
|
|
911
2212
|
return { outcome: 'refused', ...requeue.refusal };
|
|
912
2213
|
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
@@ -924,18 +2225,23 @@ export function composeRuntime(options) {
|
|
|
924
2225
|
const withdrawing = requeue.withdrawn.size > 0;
|
|
925
2226
|
const input = {
|
|
926
2227
|
local: localSaves,
|
|
927
|
-
baseRoomVersion: replica.
|
|
928
|
-
composition: compositionFor(replica.
|
|
2228
|
+
baseRoomVersion: replica.record.roomVersion,
|
|
2229
|
+
composition: compositionFor(replica.record),
|
|
929
2230
|
operations: room.operations,
|
|
930
2231
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
931
2232
|
providerLimits: limits,
|
|
932
2233
|
handle: room.handle,
|
|
933
|
-
visible:
|
|
2234
|
+
visible: saving.visible,
|
|
934
2235
|
changes,
|
|
935
2236
|
bases: parts.flatMap(({ bases }) => bases),
|
|
2237
|
+
queued: queuedDocuments(replica.queue),
|
|
936
2238
|
writtenBy,
|
|
2239
|
+
unmodeled: await unmodeledIn(replica.record),
|
|
2240
|
+
validate: validatorOf(replica.record, saving.confirmed),
|
|
937
2241
|
};
|
|
938
|
-
const checked = withdrawing
|
|
2242
|
+
const checked = withdrawing
|
|
2243
|
+
? await buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
|
|
2244
|
+
: null;
|
|
939
2245
|
if (checked?.kind === 'refused')
|
|
940
2246
|
return { outcome: 'refused', ...checked.refusal };
|
|
941
2247
|
const owners = [...requeue.owners].flatMap((origin) => {
|
|
@@ -951,13 +2257,14 @@ export function composeRuntime(options) {
|
|
|
951
2257
|
};
|
|
952
2258
|
}
|
|
953
2259
|
const built = !withdrawing
|
|
954
|
-
? buildBatch(input)
|
|
2260
|
+
? await buildBatch(input)
|
|
955
2261
|
: requeue.changes.length === 0
|
|
956
2262
|
? null
|
|
957
|
-
: buildBatch({
|
|
2263
|
+
: await buildBatch({
|
|
958
2264
|
...input,
|
|
959
|
-
visible:
|
|
2265
|
+
visible: await stepped(saving.visibleOver(requeue.queue)),
|
|
960
2266
|
changes: requeue.changes,
|
|
2267
|
+
queued: queuedDocuments(requeue.queue),
|
|
961
2268
|
});
|
|
962
2269
|
if (built?.kind === 'refused')
|
|
963
2270
|
return { outcome: 'refused', ...built.refusal };
|
|
@@ -974,7 +2281,7 @@ export function composeRuntime(options) {
|
|
|
974
2281
|
for (const [origin, replaced] of withdrawn) {
|
|
975
2282
|
if (replaced === null)
|
|
976
2283
|
continue;
|
|
977
|
-
const carried = withdrawnDrafts(pendings.get(origin)?.drafts ?? [], changes);
|
|
2284
|
+
const carried = withdrawnDrafts(pendings.get(pendingKey(locator, origin))?.drafts ?? [], changes);
|
|
978
2285
|
drafted.set(replaced.id, replaced === join?.batch
|
|
979
2286
|
? [
|
|
980
2287
|
...carried.map((held) => joinedDraft(held, saved)),
|
|
@@ -988,59 +2295,77 @@ export function composeRuntime(options) {
|
|
|
988
2295
|
for (const replaced of withdrawn.values()) {
|
|
989
2296
|
if (replaced === null || !batches.includes(replaced))
|
|
990
2297
|
continue;
|
|
991
|
-
const limited =
|
|
2298
|
+
const limited = await stepped(limiting(batches, replaced, limits, saving.visibleOver));
|
|
992
2299
|
if (limited !== null)
|
|
993
2300
|
return { outcome: 'refused', ...limited };
|
|
994
2301
|
}
|
|
995
|
-
const
|
|
996
|
-
|
|
997
|
-
|
|
2302
|
+
const carried = carriedDrafts(locator, saved, consumed);
|
|
2303
|
+
const left = [...consumed, ...carried.keys()];
|
|
2304
|
+
const arrived = [...carried.values()];
|
|
2305
|
+
const done = await saveCommit(local, files, [
|
|
2306
|
+
{
|
|
998
2307
|
locator,
|
|
2308
|
+
saving,
|
|
2309
|
+
before: replica,
|
|
999
2310
|
queue: batches,
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
2311
|
+
write: {
|
|
2312
|
+
locator,
|
|
2313
|
+
queue: batches,
|
|
2314
|
+
saved: drafted,
|
|
2315
|
+
consumedRoomDrafts: left,
|
|
2316
|
+
roomDrafts: arrived,
|
|
2317
|
+
},
|
|
2318
|
+
},
|
|
2319
|
+
]);
|
|
2320
|
+
if (done === null)
|
|
1005
2321
|
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2322
|
+
const { answer, touched } = done;
|
|
2323
|
+
if (answer.fenced !== null) {
|
|
2324
|
+
throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
|
|
1006
2325
|
}
|
|
1007
2326
|
if (fences.has(locator))
|
|
1008
2327
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
1009
2328
|
for (const origin of withdrawn.keys())
|
|
1010
|
-
pendings.delete(origin);
|
|
2329
|
+
pendings.delete(pendingKey(locator, origin));
|
|
1011
2330
|
for (const [id, drafts] of drafted) {
|
|
1012
|
-
pendings.set(
|
|
2331
|
+
pendings.set(pendingKey(locator, id), {
|
|
2332
|
+
drafts,
|
|
2333
|
+
submission: { state: 'waiting' },
|
|
2334
|
+
lost: false,
|
|
2335
|
+
});
|
|
1013
2336
|
}
|
|
1014
|
-
|
|
1015
|
-
recorded = false;
|
|
1016
|
-
for (const path of consumed)
|
|
2337
|
+
for (const path of left)
|
|
1017
2338
|
forgetRoomDraft(locator, path);
|
|
1018
|
-
|
|
1019
|
-
|
|
2339
|
+
for (const record of arrived)
|
|
2340
|
+
rememberRoomDraft(record);
|
|
2341
|
+
savedReplica(locator, replica, batches, touched.get(locator), answer);
|
|
2342
|
+
publishChangeSet(changeSet(locator, {
|
|
2343
|
+
documents: answer.changed[locator] ?? [],
|
|
1020
2344
|
queue: true,
|
|
1021
|
-
drafts:
|
|
2345
|
+
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
1022
2346
|
}));
|
|
1023
|
-
|
|
1024
|
-
submitLater(locator);
|
|
2347
|
+
queuedSave(locator);
|
|
1025
2348
|
const identifier = join?.batch.id ??
|
|
1026
2349
|
built?.batch.id ??
|
|
1027
2350
|
[...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
|
|
1028
2351
|
return { outcome: 'saved', identifier };
|
|
1029
2352
|
}
|
|
1030
|
-
async function mark(local, locator,
|
|
1031
|
-
const
|
|
1032
|
-
|
|
1033
|
-
state
|
|
1034
|
-
(
|
|
1035
|
-
|
|
2353
|
+
async function mark(local, locator, batch, submission, lost) {
|
|
2354
|
+
const partner = partnerOf(batch, locator);
|
|
2355
|
+
for (const held of partner === null ? [locator] : [locator, partner]) {
|
|
2356
|
+
const state = pendings.get(pendingKey(held, batch.id));
|
|
2357
|
+
if (!continuing() ||
|
|
2358
|
+
state === undefined ||
|
|
2359
|
+
(JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
|
|
2360
|
+
continue;
|
|
2361
|
+
}
|
|
2362
|
+
await local.submit(held, batch.id, submission, lost);
|
|
2363
|
+
if (fences.has(held))
|
|
2364
|
+
continue;
|
|
2365
|
+
pendings.set(pendingKey(held, batch.id), { ...state, submission, lost });
|
|
2366
|
+
if (replicas.has(held))
|
|
2367
|
+
publishChangeSet(changeSet(held, { queue: true }));
|
|
1036
2368
|
}
|
|
1037
|
-
await local.submit(locator, identifier, submission, lost);
|
|
1038
|
-
if (fences.has(locator))
|
|
1039
|
-
return;
|
|
1040
|
-
pendings.set(identifier, { ...state, submission, lost });
|
|
1041
|
-
const replica = replicas.get(locator);
|
|
1042
|
-
if (replica !== undefined)
|
|
1043
|
-
publishChangeSet(changeSet(locator, replica, replica, { queue: true }));
|
|
1044
2369
|
moveTo({ ...status, ...attention() });
|
|
1045
2370
|
}
|
|
1046
2371
|
async function keepUpload(local, locator, identifier, upload) {
|
|
@@ -1049,7 +2374,8 @@ export function composeRuntime(options) {
|
|
|
1049
2374
|
await local.keepUpload(locator, identifier, upload);
|
|
1050
2375
|
if (fences.has(locator))
|
|
1051
2376
|
throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
|
|
1052
|
-
|
|
2377
|
+
const key = pendingKey(locator, identifier);
|
|
2378
|
+
pendings.set(key, { ...pendings.get(key), upload });
|
|
1053
2379
|
if (!continuing())
|
|
1054
2380
|
throw new Error('connection refused');
|
|
1055
2381
|
}
|
|
@@ -1082,7 +2408,7 @@ export function composeRuntime(options) {
|
|
|
1082
2408
|
if (fencedSince(locator, since))
|
|
1083
2409
|
return failure({ kind: 'room-not-readable', locator });
|
|
1084
2410
|
if (result.kind === 'scoped-refusal') {
|
|
1085
|
-
const room = replicas.get(result.locator)?.
|
|
2411
|
+
const room = replicas.get(result.locator)?.record;
|
|
1086
2412
|
if (room !== undefined)
|
|
1087
2413
|
await scoped(result, room, await openLocal());
|
|
1088
2414
|
}
|
|
@@ -1095,21 +2421,57 @@ export function composeRuntime(options) {
|
|
|
1095
2421
|
return UNCERTAIN;
|
|
1096
2422
|
}
|
|
1097
2423
|
}
|
|
1098
|
-
function
|
|
2424
|
+
function located(room, path, destination) {
|
|
2425
|
+
const crossing = roomTarget(destination);
|
|
2426
|
+
if (crossing === null) {
|
|
2427
|
+
const named = target(path, destination);
|
|
2428
|
+
return named === null || !rooms.has(room) ? null : { room, path: named };
|
|
2429
|
+
}
|
|
2430
|
+
const held = [...rooms.values()].find((listed) => listed.handle === crossing.handle);
|
|
2431
|
+
return held === undefined ? null : { room: held.locator, path: crossing.path };
|
|
2432
|
+
}
|
|
2433
|
+
async function resolveReference(room, path, destination) {
|
|
2434
|
+
if (roomTarget(destination) === null && target(path, destination) === null) {
|
|
2435
|
+
return ABSENT_TARGET;
|
|
2436
|
+
}
|
|
2437
|
+
const at = located(room, path, destination);
|
|
2438
|
+
if (at === null)
|
|
2439
|
+
return UNREADABLE;
|
|
2440
|
+
if (locked(at.room))
|
|
2441
|
+
return LOCKED_UNREADABLE;
|
|
2442
|
+
const held = replicas.get(at.room);
|
|
2443
|
+
if (held !== undefined &&
|
|
2444
|
+
(await (await openLocal()).entries(held.record.key, 'visible', [at.path])).length > 0) {
|
|
2445
|
+
return { kind: 'document', ...at };
|
|
2446
|
+
}
|
|
2447
|
+
const history = await readHistory(at.room, at.path);
|
|
2448
|
+
if (history.kind === 'no-history' || history.kind === 'revision')
|
|
2449
|
+
return ABSENT_TARGET;
|
|
2450
|
+
if (history.kind !== 'history')
|
|
2451
|
+
return history;
|
|
2452
|
+
const [last] = history.revisions;
|
|
2453
|
+
if (last === undefined)
|
|
2454
|
+
return ABSENT_TARGET;
|
|
2455
|
+
return history.deleted === true
|
|
2456
|
+
? { kind: 'deleted', ...at, revision: last }
|
|
2457
|
+
: { kind: 'document', ...at };
|
|
2458
|
+
}
|
|
2459
|
+
function queuedFacts(locator, path, localId) {
|
|
1099
2460
|
for (const batch of replicas.get(locator)?.queue ?? []) {
|
|
1100
2461
|
const change = batch.changes[queuedFile(batch, path, localId)];
|
|
1101
2462
|
if (change?.kind === 'binary')
|
|
1102
|
-
return change
|
|
2463
|
+
return change;
|
|
1103
2464
|
}
|
|
1104
|
-
|
|
2465
|
+
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2466
|
+
return drafted?.localId === localId ? drafted : null;
|
|
1105
2467
|
}
|
|
1106
|
-
function
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
2468
|
+
function batchFile(batch) {
|
|
2469
|
+
return async (localId) => {
|
|
2470
|
+
const change = batch.changes.find((held) => held.kind === 'binary' && held.localId === localId);
|
|
2471
|
+
return (await openLocal()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
2472
|
+
};
|
|
1111
2473
|
}
|
|
1112
|
-
async function
|
|
2474
|
+
async function transferBinary(request, transfer, answer) {
|
|
1113
2475
|
if (closed || refusal !== null)
|
|
1114
2476
|
return UNCERTAIN;
|
|
1115
2477
|
try {
|
|
@@ -1121,73 +2483,109 @@ export function composeRuntime(options) {
|
|
|
1121
2483
|
if (closed || authorization === undefined || refusal !== null)
|
|
1122
2484
|
return UNCERTAIN;
|
|
1123
2485
|
const since = fencings;
|
|
1124
|
-
journal.record(request.
|
|
1125
|
-
? `transfer binary ${request.locator} ${request.path}`
|
|
1126
|
-
: `transfer binary ${request.fileRef}`);
|
|
2486
|
+
journal.record(`transfer binary ${request.locator} ${request.path}`);
|
|
1127
2487
|
try {
|
|
1128
|
-
const result = await
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
if (
|
|
1132
|
-
return failure({ kind: 'room-not-readable', locator });
|
|
1133
|
-
if (
|
|
1134
|
-
const room = replicas.get(
|
|
2488
|
+
const result = await transfer(authorization);
|
|
2489
|
+
const settled = result;
|
|
2490
|
+
journal.record(`outcome binary ${settled.kind}`);
|
|
2491
|
+
if (fencedSince(request.locator, since))
|
|
2492
|
+
return failure({ kind: 'room-not-readable', locator: request.locator });
|
|
2493
|
+
if (settled.kind === 'scoped-refusal') {
|
|
2494
|
+
const room = replicas.get(settled.locator)?.record;
|
|
1135
2495
|
if (room !== undefined)
|
|
1136
|
-
await scoped(
|
|
2496
|
+
await scoped(settled, room, await openLocal());
|
|
1137
2497
|
}
|
|
1138
|
-
if (
|
|
2498
|
+
if (settled.kind === 'revoked-authorization')
|
|
1139
2499
|
await refuse('revoked-authorization');
|
|
1140
|
-
return
|
|
2500
|
+
return answer(result);
|
|
1141
2501
|
}
|
|
1142
2502
|
catch {
|
|
1143
2503
|
journal.record('outcome binary transport-uncertainty');
|
|
1144
2504
|
return UNCERTAIN;
|
|
1145
2505
|
}
|
|
1146
2506
|
}
|
|
1147
|
-
async function
|
|
1148
|
-
|
|
2507
|
+
async function readBinary(request, name) {
|
|
2508
|
+
if (closed)
|
|
2509
|
+
return UNCERTAIN;
|
|
2510
|
+
const held = await keptFiles.find(name);
|
|
2511
|
+
if (held !== null)
|
|
2512
|
+
return held;
|
|
2513
|
+
return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
|
|
2514
|
+
}
|
|
2515
|
+
async function binaryPath(locator, path, revision) {
|
|
1149
2516
|
if (revision !== undefined)
|
|
1150
|
-
return
|
|
1151
|
-
const visible = replicas.get(locator)?.visible;
|
|
1152
|
-
const held = visible?.binaries[path];
|
|
1153
|
-
if (visible === undefined || held === undefined)
|
|
1154
|
-
return absentBinary(path);
|
|
1155
|
-
if (held.state === 'pending') {
|
|
1156
|
-
const blob = queuedBlob(locator, path, held.localId);
|
|
1157
|
-
return blob === null ? absentBinary(path) : { kind: 'binary', blob };
|
|
1158
|
-
}
|
|
1159
|
-
const record = binaryRecord(path, held, visible.revisions[path]);
|
|
1160
|
-
if (record.state !== 'accepted')
|
|
1161
|
-
return absentBinary(path);
|
|
2517
|
+
return { kind: 'path', locator, path, revision };
|
|
1162
2518
|
const replica = replicas.get(locator);
|
|
1163
|
-
|
|
2519
|
+
if (replica === undefined)
|
|
2520
|
+
return null;
|
|
2521
|
+
const local = await openLocal();
|
|
2522
|
+
const [held] = await local.entries(replica.record.key, 'visible', [path]);
|
|
2523
|
+
if (held?.binary === undefined || held.binary === null)
|
|
2524
|
+
return null;
|
|
2525
|
+
if (held.binary.state === 'pending')
|
|
2526
|
+
return { kind: 'pending', localId: held.binary.localId };
|
|
2527
|
+
const record = binaryRecord(path, held.binary, held.revision ?? undefined);
|
|
2528
|
+
if (record.state !== 'accepted')
|
|
2529
|
+
return null;
|
|
2530
|
+
const queued = replica.queue.flatMap((batch) => batch.changes);
|
|
2531
|
+
const sources = queued.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
|
|
2532
|
+
const confirmedLayer = await stepped(slicing(await local.entries(replica.record.key, 'confirmed', [path, ...sources]), [
|
|
2533
|
+
path,
|
|
2534
|
+
...sources,
|
|
2535
|
+
]));
|
|
1164
2536
|
const holds = (at) => {
|
|
1165
|
-
const kept =
|
|
2537
|
+
const kept = confirmedLayer.binaries[at];
|
|
1166
2538
|
return (kept?.state === 'accepted' &&
|
|
1167
|
-
kept.
|
|
1168
|
-
|
|
2539
|
+
kept.sha256 === record.sha256 &&
|
|
2540
|
+
confirmedLayer.revisions[at] === record.revision);
|
|
1169
2541
|
};
|
|
1170
|
-
let
|
|
1171
|
-
for (let index = queued.length - 1; index >= 0 && !holds(
|
|
2542
|
+
let confirmedAt = path;
|
|
2543
|
+
for (let index = queued.length - 1; index >= 0 && !holds(confirmedAt); index -= 1) {
|
|
1172
2544
|
const change = queued[index];
|
|
1173
|
-
if (change.kind === 'move' && change.to ===
|
|
1174
|
-
|
|
2545
|
+
if (change.kind === 'move' && change.to === confirmedAt)
|
|
2546
|
+
confirmedAt = change.path;
|
|
1175
2547
|
}
|
|
1176
|
-
return
|
|
2548
|
+
return {
|
|
1177
2549
|
kind: 'path',
|
|
1178
2550
|
locator,
|
|
1179
|
-
path:
|
|
2551
|
+
path: confirmedAt,
|
|
1180
2552
|
revision: record.revision,
|
|
1181
|
-
|
|
1182
|
-
});
|
|
2553
|
+
};
|
|
1183
2554
|
}
|
|
1184
|
-
async function
|
|
2555
|
+
async function readBinaryPath(locator, path, revision) {
|
|
1185
2556
|
await ready;
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
2557
|
+
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2558
|
+
const asked = drafted !== undefined && drafted.localId === revision
|
|
2559
|
+
? { kind: 'pending', localId: revision }
|
|
2560
|
+
: await binaryPath(locator, path, revision);
|
|
2561
|
+
if (asked === null)
|
|
2562
|
+
return absentBinary(path);
|
|
2563
|
+
if (asked.kind === 'pending') {
|
|
2564
|
+
const facts = queuedFacts(locator, path, asked.localId);
|
|
2565
|
+
return facts === null
|
|
2566
|
+
? absentBinary(path)
|
|
2567
|
+
: {
|
|
2568
|
+
kind: 'binary',
|
|
2569
|
+
file: asked.localId,
|
|
2570
|
+
byteLength: facts.byteLength,
|
|
2571
|
+
mediaType: facts.mediaType,
|
|
2572
|
+
};
|
|
2573
|
+
}
|
|
2574
|
+
return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
|
|
2575
|
+
}
|
|
2576
|
+
async function linkBinaryPath(locator, path, revision) {
|
|
2577
|
+
await ready;
|
|
2578
|
+
const asked = await binaryPath(locator, path, revision);
|
|
2579
|
+
if (asked === null || asked.kind === 'pending')
|
|
2580
|
+
return absentBinary(path);
|
|
2581
|
+
return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
|
|
2582
|
+
}
|
|
2583
|
+
function readPart(file, part) {
|
|
2584
|
+
return database.ask({ kind: 'file.part', key: file, part });
|
|
2585
|
+
}
|
|
2586
|
+
async function cleanUp() {
|
|
2587
|
+
const freed = await database.ask({ kind: 'opened.clean' });
|
|
2588
|
+
return freed + ((await options.shell.cleanUp?.()) ?? 0);
|
|
1191
2589
|
}
|
|
1192
2590
|
function declaredAgents() {
|
|
1193
2591
|
for (const capability of capabilities) {
|
|
@@ -1205,7 +2603,14 @@ export function composeRuntime(options) {
|
|
|
1205
2603
|
}
|
|
1206
2604
|
return undefined;
|
|
1207
2605
|
}
|
|
1208
|
-
|
|
2606
|
+
function calledRooms(name, input) {
|
|
2607
|
+
const member = declaredAction(name)?.room;
|
|
2608
|
+
if (member === undefined || input === null || typeof input !== 'object')
|
|
2609
|
+
return [];
|
|
2610
|
+
const named = input[member];
|
|
2611
|
+
return (Array.isArray(named) ? named : [named]).filter((room) => typeof room === 'string');
|
|
2612
|
+
}
|
|
2613
|
+
async function invokeAction(name, input, dispatching) {
|
|
1209
2614
|
await ready;
|
|
1210
2615
|
if (closed || refusal !== null)
|
|
1211
2616
|
return UNCERTAIN;
|
|
@@ -1219,11 +2624,12 @@ export function composeRuntime(options) {
|
|
|
1219
2624
|
}
|
|
1220
2625
|
if (closed || authorization === undefined || refusal !== null)
|
|
1221
2626
|
return UNCERTAIN;
|
|
2627
|
+
await dispatching?.();
|
|
1222
2628
|
journal.record(`transfer action ${name}`);
|
|
1223
2629
|
try {
|
|
1224
2630
|
const result = await options.adapter.action(authorization, { name, input });
|
|
1225
2631
|
if (result.kind === 'scoped-refusal') {
|
|
1226
|
-
const room = replicas.get(result.locator)?.
|
|
2632
|
+
const room = replicas.get(result.locator)?.record;
|
|
1227
2633
|
if (room !== undefined)
|
|
1228
2634
|
await scoped(result, room, await openLocal());
|
|
1229
2635
|
}
|
|
@@ -1239,80 +2645,515 @@ export function composeRuntime(options) {
|
|
|
1239
2645
|
return UNCERTAIN;
|
|
1240
2646
|
}
|
|
1241
2647
|
}
|
|
2648
|
+
function unsettled(result) {
|
|
2649
|
+
return result.kind !== 'action' && result.retryable;
|
|
2650
|
+
}
|
|
2651
|
+
async function recoveredAction(name, input) {
|
|
2652
|
+
const operation = operationOf(input);
|
|
2653
|
+
if (operation === null)
|
|
2654
|
+
return invokeAction(name, input);
|
|
2655
|
+
const kept = async () => (await openLocal()).keepCall({ operation, name, input });
|
|
2656
|
+
calling.add(operation);
|
|
2657
|
+
try {
|
|
2658
|
+
let result = await invokeAction(name, input, kept);
|
|
2659
|
+
if (unsettled(result)) {
|
|
2660
|
+
await recovery.attempt(`action ${operation}`, async () => {
|
|
2661
|
+
if (closed || refusal !== null)
|
|
2662
|
+
return 'done';
|
|
2663
|
+
if (!socketOpen)
|
|
2664
|
+
return 'again';
|
|
2665
|
+
result = await invokeAction(name, input, kept);
|
|
2666
|
+
return unsettled(result) ? 'again' : 'done';
|
|
2667
|
+
}, { again: true });
|
|
2668
|
+
}
|
|
2669
|
+
if (!unsettled(result))
|
|
2670
|
+
await (await openLocal()).dropCall(operation);
|
|
2671
|
+
return result;
|
|
2672
|
+
}
|
|
2673
|
+
finally {
|
|
2674
|
+
calling.delete(operation);
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
async function resendCalls(signal) {
|
|
2678
|
+
if (!socketOpen || closed || stopping)
|
|
2679
|
+
return 'done';
|
|
2680
|
+
const local = await openLocal();
|
|
2681
|
+
for (const held of await local.calls()) {
|
|
2682
|
+
if (calling.has(held.operation))
|
|
2683
|
+
continue;
|
|
2684
|
+
if (closed || signal.aborted)
|
|
2685
|
+
return 'done';
|
|
2686
|
+
if (pastHorizon(held.operation)) {
|
|
2687
|
+
await local.dropCall(held.operation);
|
|
2688
|
+
continue;
|
|
2689
|
+
}
|
|
2690
|
+
calling.add(held.operation);
|
|
2691
|
+
try {
|
|
2692
|
+
if (unsettled(await invokeAction(held.name, held.input)))
|
|
2693
|
+
return 'again';
|
|
2694
|
+
await local.dropCall(held.operation);
|
|
2695
|
+
}
|
|
2696
|
+
finally {
|
|
2697
|
+
calling.delete(held.operation);
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
return 'done';
|
|
2701
|
+
}
|
|
1242
2702
|
function continuing() {
|
|
1243
2703
|
return !closed && !discoveryPaused;
|
|
1244
2704
|
}
|
|
1245
2705
|
function reachedBy(locator, roomVersion) {
|
|
1246
2706
|
const held = replicas.get(locator);
|
|
1247
|
-
return (held !== undefined &&
|
|
1248
|
-
options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion));
|
|
2707
|
+
return (held !== undefined && options.adapter.reached(locator, held.record.roomVersion, roomVersion));
|
|
1249
2708
|
}
|
|
1250
|
-
function writtenAfter(locator, roomVersion, path) {
|
|
1251
|
-
const
|
|
2709
|
+
async function writtenAfter(locator, roomVersion, path) {
|
|
2710
|
+
const held = replicas.get(locator);
|
|
2711
|
+
if (held === undefined)
|
|
2712
|
+
return false;
|
|
2713
|
+
const [entry] = await (await openLocal()).entries(held.record.key, 'confirmed', [path]);
|
|
2714
|
+
const written = entry?.written ?? undefined;
|
|
1252
2715
|
return (written !== undefined &&
|
|
1253
2716
|
options.adapter.reached(locator, written, roomVersion) &&
|
|
1254
2717
|
!options.adapter.reached(locator, roomVersion, written));
|
|
1255
2718
|
}
|
|
1256
|
-
|
|
1257
|
-
|
|
2719
|
+
function overriddenTexts(texts, overrides) {
|
|
2720
|
+
return async (paths) => {
|
|
2721
|
+
const known = new Map(await texts(paths.filter((path) => !overrides.has(path))));
|
|
2722
|
+
for (const path of paths) {
|
|
2723
|
+
const entry = overrides.get(path);
|
|
2724
|
+
if (entry === undefined)
|
|
2725
|
+
continue;
|
|
2726
|
+
if (entry === null)
|
|
2727
|
+
known.delete(path);
|
|
2728
|
+
else
|
|
2729
|
+
known.set(path, entry.binary !== null ? null : entry.content);
|
|
2730
|
+
}
|
|
2731
|
+
return known;
|
|
2732
|
+
};
|
|
2733
|
+
}
|
|
2734
|
+
async function unreadableRoom(texts, walk, paths) {
|
|
2735
|
+
if (paths !== null) {
|
|
2736
|
+
const text = {};
|
|
2737
|
+
for (const [path, content] of await texts(paths))
|
|
2738
|
+
if (content !== null)
|
|
2739
|
+
text[path] = content;
|
|
2740
|
+
return stepped(unreadableIn(text, Object.keys(text)));
|
|
2741
|
+
}
|
|
2742
|
+
for (let after = null;;) {
|
|
2743
|
+
const page = await walk(after);
|
|
2744
|
+
const text = {};
|
|
2745
|
+
for (const { path, content } of page.entries)
|
|
2746
|
+
if (content !== null)
|
|
2747
|
+
text[path] = content;
|
|
2748
|
+
const refused = await stepped(unreadableIn(text, Object.keys(text)));
|
|
2749
|
+
if (refused !== null)
|
|
2750
|
+
return refused;
|
|
2751
|
+
if (page.next === null)
|
|
2752
|
+
return null;
|
|
2753
|
+
after = page.next;
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
async function validatedRoom(local, record, view, previous, overrides = null) {
|
|
2757
|
+
const composition = compositionOf(record);
|
|
2758
|
+
const modeled = record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable';
|
|
2759
|
+
const lookup = overrides === null
|
|
2760
|
+
? lookupOf(local, record.key, view)
|
|
2761
|
+
: overriddenLookup(lookupOf(local, record.key, view), overrides);
|
|
2762
|
+
const texts = overrides === null
|
|
2763
|
+
? textsOf(local, record.key, view)
|
|
2764
|
+
: overriddenTexts(textsOf(local, record.key, view), overrides);
|
|
2765
|
+
const walk = overrides === null
|
|
2766
|
+
? walkOf(local, record.key, view)
|
|
2767
|
+
: overriddenWalk(walkOf(local, record.key, view), overrides);
|
|
2768
|
+
let changes = null;
|
|
2769
|
+
let reshaped = false;
|
|
2770
|
+
if (previous !== null && previous.changed.length <= CLEAN_CHANGES) {
|
|
2771
|
+
const [was, now] = await Promise.all([
|
|
2772
|
+
local.entries(record.key, 'confirmed', previous.changed),
|
|
2773
|
+
overrides === null
|
|
2774
|
+
? local.entries(record.key, view, previous.changed)
|
|
2775
|
+
: Promise.resolve([]),
|
|
2776
|
+
]);
|
|
2777
|
+
const before = new Map(was.map((entry) => [entry.path, entry]));
|
|
2778
|
+
const after = overrides === null
|
|
2779
|
+
? new Map(now.map((entry) => [entry.path, entry]))
|
|
2780
|
+
: overrides;
|
|
2781
|
+
changes = previous.changed.flatMap((path) => {
|
|
2782
|
+
const one = before.get(path) ?? null;
|
|
2783
|
+
const other = after.get(path) ?? null;
|
|
2784
|
+
reshaped ||= textDiffers(one, other);
|
|
2785
|
+
const held = textOf(one);
|
|
2786
|
+
const next = textOf(other);
|
|
2787
|
+
return held === next ? [] : [{ path, was: held, now: next }];
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
const named = changes === null ? null : changes.map(({ path }) => path);
|
|
2791
|
+
const textChanged = changes === null || changes.length > 0 || reshaped;
|
|
2792
|
+
const modelsChanged = previous === null || !sameComposition(compositionOf(previous.record), composition);
|
|
2793
|
+
if (modeled && (textChanged || modelsChanged)) {
|
|
2794
|
+
const clean = previous !== null &&
|
|
2795
|
+
changes !== null &&
|
|
2796
|
+
(await changedCleanly(changes, compositionOf(previous.record), composition, {
|
|
2797
|
+
lookup,
|
|
2798
|
+
texts,
|
|
2799
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2800
|
+
}));
|
|
2801
|
+
if (clean)
|
|
2802
|
+
return null;
|
|
2803
|
+
const verdict = await validatedCollection(walk, lookup, composition);
|
|
2804
|
+
if (verdict.verdict === 'accept')
|
|
2805
|
+
return null;
|
|
2806
|
+
if (verdict.rule !== 'model-unavailable')
|
|
2807
|
+
return { kind: 'refused', refusal: verdict };
|
|
2808
|
+
return unreadableRoom(texts, walk, named);
|
|
2809
|
+
}
|
|
2810
|
+
if (textChanged)
|
|
2811
|
+
return unreadableRoom(texts, walk, named);
|
|
2812
|
+
return null;
|
|
2813
|
+
}
|
|
2814
|
+
function refreshingOf(local, key, liveness, replaying = null) {
|
|
2815
|
+
const write = replaying === null ? exclusive : installWrite;
|
|
2816
|
+
return {
|
|
2817
|
+
async turn() {
|
|
2818
|
+
if (replaying === null)
|
|
2819
|
+
return;
|
|
2820
|
+
installing(replaying, 'replaying');
|
|
2821
|
+
await screened();
|
|
2822
|
+
},
|
|
2823
|
+
prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
|
|
2824
|
+
async stage(page) {
|
|
2825
|
+
for (let written = 0;;) {
|
|
2826
|
+
if (replaying !== null)
|
|
2827
|
+
await screened();
|
|
2828
|
+
const end = batchEnd(page.entries, written);
|
|
2829
|
+
const last = end === page.entries.length;
|
|
2830
|
+
const entries = page.entries.slice(written, end);
|
|
2831
|
+
const staged = await write(async () => {
|
|
2832
|
+
if (!liveness())
|
|
2833
|
+
return false;
|
|
2834
|
+
await timed(() => local.refresh(key, last
|
|
2835
|
+
? { ...page, entries }
|
|
2836
|
+
: { entries, removals: [], folders: [], folderRemovals: [] }));
|
|
2837
|
+
return true;
|
|
2838
|
+
});
|
|
2839
|
+
if (!staged)
|
|
2840
|
+
return false;
|
|
2841
|
+
if (last)
|
|
2842
|
+
return true;
|
|
2843
|
+
written = end;
|
|
2844
|
+
}
|
|
2845
|
+
},
|
|
2846
|
+
};
|
|
2847
|
+
}
|
|
2848
|
+
function batchEnd(entries, from) {
|
|
2849
|
+
let end = from;
|
|
2850
|
+
let spent = 0;
|
|
2851
|
+
while (end < entries.length) {
|
|
2852
|
+
const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
|
|
2853
|
+
if (spent + size > stageBytes && end > from)
|
|
2854
|
+
break;
|
|
2855
|
+
spent += size;
|
|
2856
|
+
end += 1;
|
|
2857
|
+
}
|
|
2858
|
+
return end;
|
|
2859
|
+
}
|
|
2860
|
+
async function timed(work) {
|
|
2861
|
+
const started = performance.now();
|
|
2862
|
+
const done = await work();
|
|
2863
|
+
const spent = performance.now() - started;
|
|
2864
|
+
if (spent < STAGE_AIM_MS / 2)
|
|
2865
|
+
stageBytes = Math.min(STAGE_BYTES, Math.round(stageBytes * 1.5));
|
|
2866
|
+
else if (spent > STAGE_AIM_MS * 2) {
|
|
2867
|
+
stageBytes = Math.max(STAGE_FLOOR, Math.round(stageBytes / 1.5));
|
|
2868
|
+
}
|
|
2869
|
+
return done;
|
|
2870
|
+
}
|
|
2871
|
+
function installWrite(work) {
|
|
2872
|
+
const next = installWrites.then(() => exclusive(work), () => exclusive(work));
|
|
2873
|
+
installWrites = next.then(() => undefined, () => undefined);
|
|
2874
|
+
return next;
|
|
2875
|
+
}
|
|
2876
|
+
function screened() {
|
|
2877
|
+
if (onScreen || closed)
|
|
2878
|
+
return Promise.resolve();
|
|
2879
|
+
queueReadiness();
|
|
2880
|
+
return new Promise((resolve) => shownWaiters.push(resolve));
|
|
2881
|
+
}
|
|
2882
|
+
async function heldPart() {
|
|
2883
|
+
while (partsFree === 0)
|
|
2884
|
+
await new Promise((admit) => partWaiters.push(admit));
|
|
2885
|
+
partsFree -= 1;
|
|
2886
|
+
return freed();
|
|
2887
|
+
}
|
|
2888
|
+
function freed() {
|
|
2889
|
+
let released = false;
|
|
2890
|
+
return () => {
|
|
2891
|
+
if (released)
|
|
2892
|
+
return;
|
|
2893
|
+
released = true;
|
|
2894
|
+
partsFree += 1;
|
|
2895
|
+
partWaiters.shift()?.();
|
|
2896
|
+
};
|
|
2897
|
+
}
|
|
2898
|
+
function installing(locator, step, share = null) {
|
|
2899
|
+
installs.set(locator, { step, share });
|
|
2900
|
+
queueReadiness();
|
|
2901
|
+
}
|
|
2902
|
+
function installed(locator) {
|
|
2903
|
+
if (installs.delete(locator))
|
|
2904
|
+
queueReadiness();
|
|
2905
|
+
}
|
|
2906
|
+
function screen(value) {
|
|
2907
|
+
if (value === onScreen)
|
|
2908
|
+
return;
|
|
2909
|
+
onScreen = value;
|
|
2910
|
+
journal.record(`installation ${value ? 'shown' : 'hidden'}`);
|
|
2911
|
+
queueReadiness();
|
|
2912
|
+
if (!value)
|
|
2913
|
+
return;
|
|
2914
|
+
for (const resolve of shownWaiters.splice(0))
|
|
2915
|
+
resolve();
|
|
2916
|
+
}
|
|
2917
|
+
async function pullRefresh(authorized, held, liveness) {
|
|
2918
|
+
if (!liveness())
|
|
1258
2919
|
return unconfirmed;
|
|
2920
|
+
const turn = generation;
|
|
2921
|
+
const { record } = held;
|
|
2922
|
+
const base = held.confirmed;
|
|
2923
|
+
const local = await openLocal();
|
|
1259
2924
|
try {
|
|
1260
|
-
|
|
1261
|
-
const
|
|
1262
|
-
|
|
1263
|
-
|
|
2925
|
+
await exclusive(() => local.unrefresh(record.key));
|
|
2926
|
+
const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, liveness));
|
|
2927
|
+
if (result.kind !== 'advanced') {
|
|
2928
|
+
journal.record(`transfer refresh ${record.locator} ${result.kind}`);
|
|
2929
|
+
return result;
|
|
2930
|
+
}
|
|
2931
|
+
const refused = await validatedRoom(local, result.record, 'refreshed', {
|
|
2932
|
+
record,
|
|
2933
|
+
changed: [...result.changed],
|
|
2934
|
+
});
|
|
2935
|
+
if (refused !== null) {
|
|
2936
|
+
journal.record(`transfer refresh ${record.locator} refused`);
|
|
2937
|
+
return refused;
|
|
2938
|
+
}
|
|
2939
|
+
journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
|
|
2940
|
+
return { kind: 'advanced', record: result.record, base };
|
|
1264
2941
|
}
|
|
1265
2942
|
catch (error) {
|
|
1266
|
-
|
|
2943
|
+
if (refusedForSpace(error))
|
|
2944
|
+
return unconfirmed;
|
|
2945
|
+
journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
|
|
1267
2946
|
throw error;
|
|
1268
2947
|
}
|
|
2948
|
+
finally {
|
|
2949
|
+
reads.finished(record.locator, turn);
|
|
2950
|
+
}
|
|
1269
2951
|
}
|
|
1270
|
-
|
|
2952
|
+
const heldContents = new Map();
|
|
2953
|
+
function heldContentOf(local, key) {
|
|
2954
|
+
const known = heldContents.get(key);
|
|
2955
|
+
if (known !== undefined)
|
|
2956
|
+
return known;
|
|
2957
|
+
if (heldContents.size > ROOM_READS)
|
|
2958
|
+
heldContents.clear();
|
|
2959
|
+
const made = {
|
|
2960
|
+
files: (after, limit) => local.files(key, 'confirmed', after, limit),
|
|
2961
|
+
async contents(paths) {
|
|
2962
|
+
return new Map((await local.entries(key, 'confirmed', paths)).flatMap(({ path, content, revision }) => content === null || revision === null ? [] : [[path, { content, revision }]]));
|
|
2963
|
+
},
|
|
2964
|
+
};
|
|
2965
|
+
heldContents.set(key, made);
|
|
2966
|
+
return made;
|
|
2967
|
+
}
|
|
2968
|
+
function staging(local, liveness) {
|
|
1271
2969
|
return {
|
|
1272
2970
|
held: (locator) => transfers.get(locator) ?? null,
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
2971
|
+
async turn(locator, step) {
|
|
2972
|
+
installing(locator, step);
|
|
2973
|
+
for (;;) {
|
|
2974
|
+
await screened();
|
|
2975
|
+
const release = step === 'downloading' ? await heldPart() : () => { };
|
|
2976
|
+
if (onScreen || closed)
|
|
2977
|
+
return release;
|
|
2978
|
+
release();
|
|
2979
|
+
}
|
|
2980
|
+
},
|
|
2981
|
+
spare() {
|
|
2982
|
+
if (!onScreen || closed || partsFree === 0)
|
|
2983
|
+
return null;
|
|
2984
|
+
partsFree -= 1;
|
|
2985
|
+
return freed();
|
|
2986
|
+
},
|
|
2987
|
+
arriving(locator) {
|
|
2988
|
+
let ended = false;
|
|
2989
|
+
return {
|
|
2990
|
+
at(token, bytes) {
|
|
2991
|
+
if (ended)
|
|
2992
|
+
return;
|
|
2993
|
+
const held = arrivals.get(locator) ?? new Map();
|
|
2994
|
+
arrivals.set(locator, held.set(token, bytes));
|
|
2995
|
+
const now = performance.now();
|
|
2996
|
+
if (now - arrivalReported < ARRIVAL_REPORT_MS)
|
|
2997
|
+
return;
|
|
2998
|
+
arrivalReported = now;
|
|
2999
|
+
queueReadiness();
|
|
3000
|
+
},
|
|
3001
|
+
end() {
|
|
3002
|
+
ended = true;
|
|
3003
|
+
if (arrivals.delete(locator))
|
|
3004
|
+
queueReadiness();
|
|
3005
|
+
},
|
|
3006
|
+
};
|
|
3007
|
+
},
|
|
3008
|
+
received: (locator, token) => local.transferPart(locator, token),
|
|
3009
|
+
keep: (locator, token, received) => installWrite(async () => {
|
|
3010
|
+
if (liveness())
|
|
3011
|
+
await local.keepPart(locator, token, received);
|
|
1280
3012
|
}),
|
|
3013
|
+
announced(locator) {
|
|
3014
|
+
const later = announced.get(locator);
|
|
3015
|
+
return later === undefined ? [] : [later];
|
|
3016
|
+
},
|
|
3017
|
+
async stage(record, page, composition, received) {
|
|
3018
|
+
const { locator } = record;
|
|
3019
|
+
const whole = received?.byteLength ?? 0;
|
|
3020
|
+
const from = record.writing?.written ?? 0;
|
|
3021
|
+
const token = record.writing?.token ?? null;
|
|
3022
|
+
const entries = page.entries.length;
|
|
3023
|
+
let held = record;
|
|
3024
|
+
if (token !== null)
|
|
3025
|
+
arrivals.get(locator)?.delete(token);
|
|
3026
|
+
installing(locator, 'writing', { written: from, entries, bytes: whole });
|
|
3027
|
+
for (let written = from;;) {
|
|
3028
|
+
await screened();
|
|
3029
|
+
if (!liveness())
|
|
3030
|
+
return null;
|
|
3031
|
+
const end = batchEnd(page.entries, written);
|
|
3032
|
+
const last = end === entries;
|
|
3033
|
+
const next = last
|
|
3034
|
+
? {
|
|
3035
|
+
...held,
|
|
3036
|
+
writing: null,
|
|
3037
|
+
done: {
|
|
3038
|
+
parts: held.done.parts + (received === null ? 0 : 1),
|
|
3039
|
+
bytes: held.done.bytes + whole,
|
|
3040
|
+
},
|
|
3041
|
+
}
|
|
3042
|
+
: { ...held, writing: { token, written: end } };
|
|
3043
|
+
const key = await installWrite(async () => {
|
|
3044
|
+
if (!liveness())
|
|
3045
|
+
return null;
|
|
3046
|
+
return timed(() => local.stage(next, composition, {
|
|
3047
|
+
entries: page.entries.slice(written, end),
|
|
3048
|
+
folders: last ? page.folders : [],
|
|
3049
|
+
}, last && received !== null && token !== null ? token : undefined));
|
|
3050
|
+
});
|
|
3051
|
+
if (key === null)
|
|
3052
|
+
return null;
|
|
3053
|
+
const arrived = !transfers.has(locator);
|
|
3054
|
+
held = { ...next, key };
|
|
3055
|
+
transfers.set(locator, held);
|
|
3056
|
+
if (arrived)
|
|
3057
|
+
liveQueries.recover();
|
|
3058
|
+
if (last) {
|
|
3059
|
+
installing(locator, held.pageToken === null ? 'replaying' : 'downloading');
|
|
3060
|
+
liveQueries.recover();
|
|
3061
|
+
return held;
|
|
3062
|
+
}
|
|
3063
|
+
installing(locator, 'writing', { written: end, entries, bytes: whole });
|
|
3064
|
+
written = end;
|
|
3065
|
+
}
|
|
3066
|
+
},
|
|
3067
|
+
async refreshing(locator, key) {
|
|
3068
|
+
await exclusive(() => local.unrefresh(key));
|
|
3069
|
+
return refreshingOf(local, key, liveness, locator);
|
|
3070
|
+
},
|
|
1281
3071
|
abandon: (locator) => exclusive(async () => {
|
|
1282
|
-
if (!
|
|
3072
|
+
if (!liveness())
|
|
1283
3073
|
return;
|
|
1284
3074
|
await local.abandon(locator);
|
|
1285
3075
|
transfers.delete(locator);
|
|
3076
|
+
liveQueries.recover();
|
|
3077
|
+
sweep();
|
|
3078
|
+
}),
|
|
3079
|
+
setAside: (locator) => exclusive(async () => {
|
|
3080
|
+
if (!liveness())
|
|
3081
|
+
return;
|
|
3082
|
+
await local.abandon(locator, true);
|
|
3083
|
+
transfers.delete(locator);
|
|
3084
|
+
liveQueries.recover();
|
|
3085
|
+
}),
|
|
3086
|
+
release: (key) => exclusive(async () => {
|
|
3087
|
+
await local.release(key);
|
|
3088
|
+
sweep();
|
|
1286
3089
|
}),
|
|
3090
|
+
content: (key) => heldContentOf(local, key),
|
|
3091
|
+
validate: (record, view) => validatedRoom(local, record, view, null),
|
|
1287
3092
|
};
|
|
1288
3093
|
}
|
|
1289
|
-
function reusable(room) {
|
|
1290
|
-
const record = replicas.get(room.locator)?.
|
|
3094
|
+
function reusable(room, local) {
|
|
3095
|
+
const record = replicas.get(room.locator)?.record;
|
|
1291
3096
|
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
1292
3097
|
return null;
|
|
1293
|
-
return
|
|
3098
|
+
return heldContentOf(local, record.key);
|
|
1294
3099
|
}
|
|
1295
|
-
async function pullBootstrap(authorized, room, local,
|
|
1296
|
-
if (!
|
|
3100
|
+
async function pullBootstrap(authorized, room, local, liveness) {
|
|
3101
|
+
if (!liveness())
|
|
1297
3102
|
return unconfirmed;
|
|
3103
|
+
const turn = generation;
|
|
3104
|
+
if (replicas.has(room.locator)) {
|
|
3105
|
+
catching.add(room.locator);
|
|
3106
|
+
liveQueries.recover();
|
|
3107
|
+
}
|
|
1298
3108
|
try {
|
|
1299
|
-
const result = await bootstrapRoom(
|
|
3109
|
+
const result = await bootstrapRoom(reads, authorized, room, {
|
|
1300
3110
|
collection: modelOf(room.locator),
|
|
1301
3111
|
selection: modelOf(room.locator, 'typed-selection'),
|
|
1302
|
-
}, (models, held) => (
|
|
1303
|
-
const detail = result.kind === 'staged' ? result.
|
|
3112
|
+
}, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
|
|
3113
|
+
const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
|
|
1304
3114
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
3115
|
+
if (result.kind !== 'staged') {
|
|
3116
|
+
caughtUpTo(room.locator);
|
|
3117
|
+
installed(room.locator);
|
|
3118
|
+
}
|
|
1305
3119
|
return result;
|
|
1306
3120
|
}
|
|
1307
3121
|
catch (error) {
|
|
3122
|
+
caughtUpTo(room.locator);
|
|
3123
|
+
installed(room.locator);
|
|
3124
|
+
if (refusedForSpace(error))
|
|
3125
|
+
return unconfirmed;
|
|
1308
3126
|
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
1309
3127
|
throw error;
|
|
1310
3128
|
}
|
|
3129
|
+
finally {
|
|
3130
|
+
reads.finished(room.locator, turn);
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
function caughtUpTo(locator) {
|
|
3134
|
+
if (catching.delete(locator))
|
|
3135
|
+
liveQueries.recover();
|
|
3136
|
+
}
|
|
3137
|
+
function behind(room) {
|
|
3138
|
+
const held = replicas.get(room.locator)?.record.roomVersion;
|
|
3139
|
+
return (held !== undefined &&
|
|
3140
|
+
[announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
|
|
1311
3141
|
}
|
|
1312
3142
|
async function advance(room, local, authorized, live) {
|
|
3143
|
+
const carried = reads.carried(room.locator);
|
|
3144
|
+
const withheld = await advanceOnce(room, local, authorized, live);
|
|
3145
|
+
if (withheld !== null || reads.carried(room.locator) === carried || !behind(room)) {
|
|
3146
|
+
return withheld;
|
|
3147
|
+
}
|
|
3148
|
+
return advanceOnce(room, local, authorized, live);
|
|
3149
|
+
}
|
|
3150
|
+
async function advanceOnce(room, local, authorized, running) {
|
|
3151
|
+
const live = () => running() && !full;
|
|
3152
|
+
if (!live())
|
|
3153
|
+
return unconfirmed;
|
|
1313
3154
|
const held = replicas.get(room.locator);
|
|
1314
3155
|
if (held !== undefined) {
|
|
1315
|
-
const refreshed = await pullRefresh(authorized, held
|
|
3156
|
+
const refreshed = await pullRefresh(authorized, held, live);
|
|
1316
3157
|
if (!live())
|
|
1317
3158
|
return unconfirmed;
|
|
1318
3159
|
if (refreshed.kind === 'still-current') {
|
|
@@ -1327,10 +3168,11 @@ export function composeRuntime(options) {
|
|
|
1327
3168
|
return null;
|
|
1328
3169
|
}
|
|
1329
3170
|
if (refreshed.kind === 'advanced') {
|
|
1330
|
-
|
|
1331
|
-
|
|
3171
|
+
const landing = { kind: 'refreshed', base: refreshed.base };
|
|
3172
|
+
if (!(await publish(local, room, refreshed.record, landing, live))) {
|
|
3173
|
+
const now = replicas.get(room.locator)?.record.roomVersion;
|
|
1332
3174
|
const reached = now !== undefined &&
|
|
1333
|
-
options.adapter.reached(room.locator, now, refreshed.
|
|
3175
|
+
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
1334
3176
|
return reached ? null : advance(room, local, authorized, live);
|
|
1335
3177
|
}
|
|
1336
3178
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
@@ -1340,22 +3182,33 @@ export function composeRuntime(options) {
|
|
|
1340
3182
|
return refreshed;
|
|
1341
3183
|
if (opened !== null) {
|
|
1342
3184
|
owed.add(room.locator);
|
|
3185
|
+
liveQueries.recover();
|
|
1343
3186
|
return unconfirmed;
|
|
1344
3187
|
}
|
|
3188
|
+
if (reloadsDue.get(room.locator) === generation)
|
|
3189
|
+
return unconfirmed;
|
|
1345
3190
|
}
|
|
1346
3191
|
const bootstrapped = await pullBootstrap(authorized, room, local, live);
|
|
1347
|
-
if (
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
return null;
|
|
3192
|
+
if (bootstrapped.kind === 'staged') {
|
|
3193
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
3194
|
+
return live() ? null : unconfirmed;
|
|
3195
|
+
}
|
|
3196
|
+
return live() ? bootstrapped : unconfirmed;
|
|
1353
3197
|
}
|
|
1354
3198
|
async function withhold(withheld, room, local, progress) {
|
|
3199
|
+
if (withheld.kind === 'unconfirmed')
|
|
3200
|
+
return;
|
|
1355
3201
|
if (withheld.kind === 'refused')
|
|
1356
3202
|
stale.delete(room.locator);
|
|
1357
3203
|
else
|
|
1358
3204
|
stale.add(room.locator);
|
|
3205
|
+
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
3206
|
+
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
3207
|
+
repairable.set(room.locator, reset);
|
|
3208
|
+
repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
|
|
3209
|
+
}
|
|
3210
|
+
else
|
|
3211
|
+
repairable.delete(room.locator);
|
|
1359
3212
|
if (withheld.kind === 'scoped-refusal') {
|
|
1360
3213
|
await scoped(withheld, room, local);
|
|
1361
3214
|
}
|
|
@@ -1391,6 +3244,7 @@ export function composeRuntime(options) {
|
|
|
1391
3244
|
unreadable.add(room.locator);
|
|
1392
3245
|
list(room, false);
|
|
1393
3246
|
emitRooms();
|
|
3247
|
+
moveTo({ ...status, ...attention() });
|
|
1394
3248
|
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
1395
3249
|
}
|
|
1396
3250
|
function leaving(batch, result) {
|
|
@@ -1411,86 +3265,297 @@ export function composeRuntime(options) {
|
|
|
1411
3265
|
(accepted.author === undefined ? '' : ` author ${accepted.author}`) +
|
|
1412
3266
|
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
1413
3267
|
}
|
|
1414
|
-
|
|
3268
|
+
function settling(acceptance) {
|
|
3269
|
+
return acceptance !== null && 'apply' in acceptance;
|
|
3270
|
+
}
|
|
3271
|
+
function refusalOf(acceptance) {
|
|
3272
|
+
return settling(acceptance) ? (acceptance.refused ?? null) : acceptance;
|
|
3273
|
+
}
|
|
3274
|
+
async function acceptance(room, local, submitted, accepted) {
|
|
1415
3275
|
if (!continuing())
|
|
1416
3276
|
return unconfirmed;
|
|
1417
3277
|
const current = replicas.get(room.locator);
|
|
1418
3278
|
if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
|
|
1419
3279
|
return null;
|
|
1420
3280
|
}
|
|
1421
|
-
const held = current.
|
|
1422
|
-
logAccepted(room.locator, submitted, accepted);
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
3281
|
+
const held = current.record.roomVersion;
|
|
3282
|
+
logAccepted(room.locator, submitted, accepted);
|
|
3283
|
+
let record = current.record;
|
|
3284
|
+
let changes;
|
|
3285
|
+
if (!options.adapter.reached(room.locator, held, accepted.roomVersion)) {
|
|
3286
|
+
const paths = [...returnedPaths(submitted, accepted)];
|
|
3287
|
+
const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
|
|
3288
|
+
const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion));
|
|
3289
|
+
record = {
|
|
3290
|
+
...record,
|
|
3291
|
+
roomVersion: options.adapter.follows(room.locator, held, accepted.roomVersion)
|
|
3292
|
+
? accepted.roomVersion
|
|
3293
|
+
: held,
|
|
3294
|
+
};
|
|
3295
|
+
const overrides = new Map([
|
|
3296
|
+
...back.set.map((entry) => [entry.path, entry]),
|
|
3297
|
+
...back.removed.map((path) => [path, null]),
|
|
3298
|
+
]);
|
|
3299
|
+
const refused = await validatedRoom(local, record, 'confirmed', { record: current.record, changed: [...overrides.keys()] }, overrides);
|
|
3300
|
+
if (refused !== null) {
|
|
3301
|
+
const key = pendingKey(room.locator, submitted.id);
|
|
3302
|
+
return {
|
|
3303
|
+
locator: room.locator,
|
|
3304
|
+
left: [],
|
|
3305
|
+
commit: {
|
|
3306
|
+
rooms: [],
|
|
3307
|
+
writes: [
|
|
3308
|
+
{
|
|
3309
|
+
locator: room.locator,
|
|
3310
|
+
accepted: [{ id: submitted.id, roomVersion: accepted.roomVersion }],
|
|
3311
|
+
},
|
|
3312
|
+
],
|
|
3313
|
+
},
|
|
3314
|
+
refused,
|
|
3315
|
+
apply() {
|
|
3316
|
+
const state = pendings.get(key);
|
|
3317
|
+
if (state !== undefined && !fences.has(room.locator))
|
|
3318
|
+
pendings.set(key, { ...state, accepted: accepted.roomVersion });
|
|
3319
|
+
return changeSet(room.locator, { queue: true });
|
|
3320
|
+
},
|
|
3321
|
+
};
|
|
3322
|
+
}
|
|
3323
|
+
changes = back;
|
|
3324
|
+
}
|
|
3325
|
+
const settled = await settlement(local, room.locator, current, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
|
|
3326
|
+
return {
|
|
3327
|
+
...settled,
|
|
3328
|
+
apply(answer) {
|
|
3329
|
+
const set = settled.apply(answer);
|
|
3330
|
+
pendings.delete(pendingKey(room.locator, submitted.id));
|
|
3331
|
+
unreadable.delete(room.locator);
|
|
3332
|
+
sourceRefusals.delete(room.locator);
|
|
3333
|
+
declarationRefusals.delete(room.locator);
|
|
3334
|
+
list(room, true);
|
|
3335
|
+
return set;
|
|
3336
|
+
},
|
|
3337
|
+
};
|
|
3338
|
+
}
|
|
3339
|
+
async function adoptedTogether(local, acceptances) {
|
|
3340
|
+
const settlements = acceptances.filter(settling);
|
|
3341
|
+
if (settlements.length > 0) {
|
|
3342
|
+
publishChangeSet(await together(local, settlements));
|
|
3343
|
+
moveTo({ ...status, ...attention() });
|
|
3344
|
+
}
|
|
3345
|
+
await unpair(local);
|
|
1445
3346
|
}
|
|
1446
3347
|
async function adopted(room, local, submitted, accepted) {
|
|
1447
|
-
const failed = await exclusive(() =>
|
|
3348
|
+
const failed = await exclusive(async () => {
|
|
3349
|
+
const adopting = await acceptance(room, local, submitted, accepted);
|
|
3350
|
+
await adoptedTogether(local, [adopting]);
|
|
3351
|
+
return refusalOf(adopting);
|
|
3352
|
+
});
|
|
3353
|
+
if (failed === null)
|
|
3354
|
+
return true;
|
|
3355
|
+
if (failed.kind === 'refused' && continuing())
|
|
3356
|
+
refusedSource(room, failed);
|
|
3357
|
+
return false;
|
|
3358
|
+
}
|
|
3359
|
+
async function adoptedMove(room, partner, local, submitted, accepted) {
|
|
3360
|
+
const other = discovered.get(partner);
|
|
3361
|
+
const failed = await exclusive(async () => {
|
|
3362
|
+
const left = await acceptance(room, local, submitted, accepted);
|
|
3363
|
+
const arriving = replicas.get(partner)?.queue.find((batch) => batch.id === submitted.id);
|
|
3364
|
+
const arrived = other === undefined || arriving === undefined || accepted.destination === undefined
|
|
3365
|
+
? null
|
|
3366
|
+
: await acceptance(other, local, arriving, {
|
|
3367
|
+
kind: 'accepted',
|
|
3368
|
+
...accepted.destination,
|
|
3369
|
+
});
|
|
3370
|
+
await adoptedTogether(local, [left, arrived]);
|
|
3371
|
+
const refusedArrival = refusalOf(arrived);
|
|
3372
|
+
if (other !== undefined && refusedArrival?.kind === 'refused' && continuing()) {
|
|
3373
|
+
refusedSource(other, refusedArrival);
|
|
3374
|
+
}
|
|
3375
|
+
return refusalOf(left);
|
|
3376
|
+
});
|
|
3377
|
+
submitLater(partner);
|
|
1448
3378
|
if (failed === null)
|
|
1449
3379
|
return true;
|
|
1450
3380
|
if (failed.kind === 'refused' && continuing())
|
|
1451
3381
|
refusedSource(room, failed);
|
|
1452
3382
|
return false;
|
|
1453
3383
|
}
|
|
1454
|
-
|
|
1455
|
-
|
|
3384
|
+
function queueIn(locator) {
|
|
3385
|
+
return replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
3386
|
+
}
|
|
3387
|
+
function behindIn(locator, batch) {
|
|
3388
|
+
const held = replicas.get(locator)?.queue;
|
|
3389
|
+
if (held === undefined)
|
|
3390
|
+
return queuesWithoutReplica.get(locator) !== undefined;
|
|
3391
|
+
return held.findIndex((one) => one.id === batch.id) > 0;
|
|
3392
|
+
}
|
|
3393
|
+
function unpaired(skipped) {
|
|
3394
|
+
for (const locator of [...replicas.keys(), ...queuesWithoutReplica.keys()]) {
|
|
3395
|
+
for (const batch of queueIn(locator)) {
|
|
3396
|
+
const partner = partnerOf(batch, locator);
|
|
3397
|
+
if (partner === null || skipped.has(pendingKey(locator, batch.id)))
|
|
3398
|
+
continue;
|
|
3399
|
+
if (queueIn(partner).some((held) => held.id === batch.id))
|
|
3400
|
+
continue;
|
|
3401
|
+
if (unpairedIn(locator, batch))
|
|
3402
|
+
return { locator, batch };
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
return null;
|
|
3406
|
+
}
|
|
3407
|
+
async function unpairing(local, locator, batches) {
|
|
3408
|
+
const ids = new Set(batches.map(({ id }) => id));
|
|
3409
|
+
const leaving = batches.map((batch) => ({
|
|
3410
|
+
batch,
|
|
3411
|
+
returned: !localSaves && leavingIn(batch, locator) ? returnedBy('withdrawn') : null,
|
|
3412
|
+
}));
|
|
3413
|
+
const outcome = localSaves ? 'dropped' : 'unpaired';
|
|
3414
|
+
const current = replicas.get(locator);
|
|
3415
|
+
if (current !== undefined) {
|
|
3416
|
+
const settled = await settlement(local, locator, current, current.record, current.queue.filter((one) => !ids.has(one.id)), { leaving });
|
|
3417
|
+
return {
|
|
3418
|
+
...settled,
|
|
3419
|
+
apply(answer) {
|
|
3420
|
+
for (const batch of batches)
|
|
3421
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
3422
|
+
const set = settled.apply(answer);
|
|
3423
|
+
submitLater(locator);
|
|
3424
|
+
return set;
|
|
3425
|
+
},
|
|
3426
|
+
};
|
|
3427
|
+
}
|
|
3428
|
+
const queue = queueIn(locator).filter((one) => !ids.has(one.id));
|
|
3429
|
+
const written = new Map();
|
|
3430
|
+
for (const { batch, returned } of leaving) {
|
|
3431
|
+
if (returned === null)
|
|
3432
|
+
continue;
|
|
3433
|
+
for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
|
|
3434
|
+
written.set(saved.path, {
|
|
3435
|
+
locator,
|
|
3436
|
+
path: saved.path,
|
|
3437
|
+
revision: roomDraftRevision(),
|
|
3438
|
+
parts: returnedInto((written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path))?.parts, saved.parts),
|
|
3439
|
+
returned,
|
|
3440
|
+
});
|
|
3441
|
+
}
|
|
3442
|
+
}
|
|
3443
|
+
const carried = carriedBack(locator, leaving.flatMap(({ batch, returned }) => (returned === null ? [] : [batch])), written);
|
|
3444
|
+
return {
|
|
3445
|
+
locator,
|
|
3446
|
+
left: batches,
|
|
3447
|
+
commit: {
|
|
3448
|
+
rooms: [],
|
|
3449
|
+
writes: [
|
|
3450
|
+
{
|
|
3451
|
+
locator,
|
|
3452
|
+
queue,
|
|
3453
|
+
roomDrafts: [...written.values()],
|
|
3454
|
+
consumedRoomDrafts: carried.taken,
|
|
3455
|
+
},
|
|
3456
|
+
...carried.writes,
|
|
3457
|
+
],
|
|
3458
|
+
},
|
|
3459
|
+
apply() {
|
|
3460
|
+
for (const batch of batches) {
|
|
3461
|
+
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
3462
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
3463
|
+
}
|
|
3464
|
+
if (queue.length > 0)
|
|
3465
|
+
queuesWithoutReplica.set(locator, queue);
|
|
3466
|
+
else
|
|
3467
|
+
queuesWithoutReplica.delete(locator);
|
|
3468
|
+
for (const record of written.values())
|
|
3469
|
+
rememberRoomDraft(record);
|
|
3470
|
+
carried.remember();
|
|
3471
|
+
submitLater(locator);
|
|
3472
|
+
return mergeChangeSets([
|
|
3473
|
+
changeSet(locator, {
|
|
3474
|
+
queue: true,
|
|
3475
|
+
drafts: [...written.keys(), ...carried.taken],
|
|
3476
|
+
}),
|
|
3477
|
+
...carried.changes,
|
|
3478
|
+
]);
|
|
3479
|
+
},
|
|
3480
|
+
};
|
|
3481
|
+
}
|
|
3482
|
+
async function unpair(local) {
|
|
3483
|
+
const handled = new Set();
|
|
3484
|
+
for (let held = unpaired(handled); held !== null && continuing(); held = unpaired(handled)) {
|
|
3485
|
+
const { locator, batch } = held;
|
|
3486
|
+
handled.add(pendingKey(locator, batch.id));
|
|
3487
|
+
publishChangeSet(await together(local, [await unpairing(local, locator, [batch])]));
|
|
3488
|
+
}
|
|
3489
|
+
if (handled.size === 0)
|
|
3490
|
+
return;
|
|
3491
|
+
moveTo({ ...status, ...attention() });
|
|
3492
|
+
}
|
|
3493
|
+
async function discardHeld(head, room, local, signal) {
|
|
3494
|
+
const partner = partnerOf(head, room.locator);
|
|
3495
|
+
const other = partner === null ? undefined : discovered.get(partner);
|
|
3496
|
+
await discardPending(other === undefined ? [room] : [room, other], local, signal);
|
|
3497
|
+
}
|
|
3498
|
+
async function submitRoom(room, local, authorized, progress, signal) {
|
|
3499
|
+
while (continuing() && authorization !== undefined && !signal.aborted) {
|
|
1456
3500
|
const replica = replicas.get(room.locator);
|
|
1457
3501
|
const head = replica?.queue[0];
|
|
1458
3502
|
if (replica === undefined || head === undefined)
|
|
1459
|
-
return;
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
3503
|
+
return 'done';
|
|
3504
|
+
const state = pendings.get(pendingKey(room.locator, head.id));
|
|
3505
|
+
if (state?.accepted !== undefined)
|
|
3506
|
+
return 'done';
|
|
3507
|
+
const partner = partnerOf(head, room.locator);
|
|
3508
|
+
if (partner !== null && !leavingIn(head, room.locator)) {
|
|
3509
|
+
submitLater(partner);
|
|
3510
|
+
return 'done';
|
|
3511
|
+
}
|
|
3512
|
+
if (partner !== null && behindIn(partner, head)) {
|
|
3513
|
+
progress.complete = false;
|
|
3514
|
+
return 'done';
|
|
3515
|
+
}
|
|
1463
3516
|
const { submission, lost: earlier } = state ?? {
|
|
1464
3517
|
submission: { state: 'waiting' },
|
|
1465
3518
|
lost: false,
|
|
1466
3519
|
};
|
|
1467
3520
|
if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
|
|
1468
3521
|
progress.complete = false;
|
|
1469
|
-
return;
|
|
3522
|
+
return { at: Date.parse(submission.reset) };
|
|
1470
3523
|
}
|
|
1471
3524
|
if (writesHeld()) {
|
|
1472
3525
|
progress.complete = false;
|
|
1473
3526
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1474
|
-
return;
|
|
3527
|
+
return 'done';
|
|
1475
3528
|
}
|
|
1476
3529
|
const lost = earlier || submission.state === 'submitted';
|
|
3530
|
+
if (lost && pastHorizon(head.id)) {
|
|
3531
|
+
await discardHeld(head, room, local, signal);
|
|
3532
|
+
return 'done';
|
|
3533
|
+
}
|
|
1477
3534
|
let result;
|
|
1478
3535
|
journal.record(`submission ${room.locator} ${head.id}`);
|
|
1479
|
-
await exclusive(() => mark(local, room.locator, head
|
|
3536
|
+
await exclusive(() => mark(local, room.locator, head, { state: 'submitted' }, lost));
|
|
1480
3537
|
if (discoveryPaused || fences.has(room.locator))
|
|
1481
|
-
return;
|
|
1482
|
-
if (writesHeld()) {
|
|
1483
|
-
await exclusive(() => mark(local, room.locator, head
|
|
3538
|
+
return 'done';
|
|
3539
|
+
if (signal.aborted || writesHeld()) {
|
|
3540
|
+
await exclusive(() => mark(local, room.locator, head, submission, earlier));
|
|
3541
|
+
if (signal.aborted)
|
|
3542
|
+
return 'done';
|
|
1484
3543
|
progress.complete = false;
|
|
1485
3544
|
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1486
|
-
return;
|
|
3545
|
+
return 'done';
|
|
1487
3546
|
}
|
|
1488
3547
|
const opensBefore = openSignals;
|
|
1489
|
-
const
|
|
3548
|
+
const answeredHere = awaitingAnswer(room.locator);
|
|
3549
|
+
const answeredThere = partner === null ? () => { } : awaitingAnswer(partner);
|
|
3550
|
+
const answered = () => {
|
|
3551
|
+
answeredHere();
|
|
3552
|
+
answeredThere();
|
|
3553
|
+
};
|
|
1490
3554
|
try {
|
|
1491
3555
|
result = await options.adapter.mutate(authorized, {
|
|
1492
3556
|
locator: room.locator,
|
|
1493
3557
|
changeBatch: head,
|
|
3558
|
+
file: batchFile(head),
|
|
1494
3559
|
...(state?.upload === undefined ? {} : { upload: state.upload }),
|
|
1495
3560
|
}, (upload) => exclusive(() => keepUpload(local, room.locator, head.id, upload)));
|
|
1496
3561
|
}
|
|
@@ -1499,50 +3564,69 @@ export function composeRuntime(options) {
|
|
|
1499
3564
|
progress.complete = false;
|
|
1500
3565
|
if (continuing())
|
|
1501
3566
|
progress.reachable = false;
|
|
1502
|
-
return;
|
|
3567
|
+
return 'again';
|
|
3568
|
+
}
|
|
3569
|
+
if (signal.aborted) {
|
|
3570
|
+
answered();
|
|
3571
|
+
return 'done';
|
|
1503
3572
|
}
|
|
1504
3573
|
if (result.kind !== 'accepted' || !continuing())
|
|
1505
3574
|
answered();
|
|
1506
3575
|
if (!continuing())
|
|
1507
|
-
return;
|
|
3576
|
+
return 'done';
|
|
1508
3577
|
if (result.kind === 'transport-uncertainty') {
|
|
1509
3578
|
progress.complete = false;
|
|
1510
3579
|
progress.reachable = false;
|
|
1511
|
-
return;
|
|
3580
|
+
return 'again';
|
|
1512
3581
|
}
|
|
1513
3582
|
if (result.kind === 'room-not-readable') {
|
|
3583
|
+
const unread = discovered.get(result.locator) ?? room;
|
|
1514
3584
|
progress.complete = false;
|
|
1515
|
-
unreadable.add(
|
|
1516
|
-
list(
|
|
3585
|
+
unreadable.add(unread.locator);
|
|
3586
|
+
list(unread, false);
|
|
1517
3587
|
emitRooms();
|
|
1518
|
-
return;
|
|
3588
|
+
return 'done';
|
|
1519
3589
|
}
|
|
1520
3590
|
if (result.kind === 'revoked-authorization') {
|
|
1521
3591
|
await refuse('revoked-authorization');
|
|
1522
|
-
return;
|
|
3592
|
+
return 'done';
|
|
1523
3593
|
}
|
|
1524
3594
|
if (result.kind === 'scoped-refusal') {
|
|
1525
3595
|
await scoped(result, room, local);
|
|
1526
3596
|
progress.complete = false;
|
|
1527
|
-
return;
|
|
3597
|
+
return 'done';
|
|
1528
3598
|
}
|
|
1529
3599
|
if (result.kind === 'write-halted') {
|
|
1530
3600
|
journal.record(`outcome ${room.locator} ${head.id} write-halted`);
|
|
1531
|
-
await exclusive(() => mark(local, room.locator, head
|
|
3601
|
+
await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, lost));
|
|
1532
3602
|
if (serviceMode === 'open' && openSignals !== opensBefore)
|
|
1533
3603
|
continue;
|
|
1534
3604
|
haltedRefusal = true;
|
|
1535
3605
|
progress.complete = false;
|
|
1536
|
-
return;
|
|
3606
|
+
return 'done';
|
|
1537
3607
|
}
|
|
3608
|
+
const other = partner === null ? undefined : discovered.get(partner);
|
|
1538
3609
|
if (result.kind === 'conflicted' ||
|
|
1539
3610
|
result.kind === 'rejected' ||
|
|
1540
3611
|
result.kind === 'expired' ||
|
|
1541
3612
|
result.kind === 'retryable-refusal') {
|
|
3613
|
+
if (result.kind === 'expired' && lost && result.restored !== true) {
|
|
3614
|
+
journal.record(`outcome ${room.locator} ${head.id} expired`);
|
|
3615
|
+
await discardHeld(head, room, local, signal);
|
|
3616
|
+
return 'done';
|
|
3617
|
+
}
|
|
1542
3618
|
if (result.kind === 'expired') {
|
|
3619
|
+
await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, partner !== null && lost));
|
|
3620
|
+
const there = other === undefined ? null : await advance(other, local, authorized, continuing);
|
|
3621
|
+
if (other !== undefined && there !== null) {
|
|
3622
|
+
await withhold(there, other, local, progress);
|
|
3623
|
+
return 'done';
|
|
3624
|
+
}
|
|
1543
3625
|
const withheld = await advance(room, local, authorized, continuing);
|
|
1544
|
-
if (withheld !== null)
|
|
1545
|
-
|
|
3626
|
+
if (withheld !== null) {
|
|
3627
|
+
await withhold(withheld, room, local, progress);
|
|
3628
|
+
return 'done';
|
|
3629
|
+
}
|
|
1546
3630
|
}
|
|
1547
3631
|
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
|
|
1548
3632
|
(result.kind === 'conflicted' && result.roomVersion !== undefined
|
|
@@ -1554,12 +3638,25 @@ export function composeRuntime(options) {
|
|
|
1554
3638
|
if (result.kind === 'retryable-refusal') {
|
|
1555
3639
|
const reset = result.reset;
|
|
1556
3640
|
progress.complete = false;
|
|
1557
|
-
await exclusive(() => mark(local, room.locator, head
|
|
1558
|
-
|
|
1559
|
-
return;
|
|
3641
|
+
await exclusive(() => mark(local, room.locator, head, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
|
|
3642
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
1560
3643
|
}
|
|
1561
3644
|
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
1562
3645
|
const left = leaving(head, result);
|
|
3646
|
+
const refreshed = async () => ({
|
|
3647
|
+
there: other === undefined ? null : await advance(other, local, authorized, continuing),
|
|
3648
|
+
here: await advance(room, local, authorized, continuing),
|
|
3649
|
+
});
|
|
3650
|
+
const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
|
|
3651
|
+
if (judged !== null && (judged.there !== null || judged.here !== null)) {
|
|
3652
|
+
await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, lost));
|
|
3653
|
+
if (other !== undefined && judged.there !== null) {
|
|
3654
|
+
await withhold(judged.there, other, local, progress);
|
|
3655
|
+
}
|
|
3656
|
+
if (judged.here !== null)
|
|
3657
|
+
await withhold(judged.here, room, local, progress);
|
|
3658
|
+
return 'done';
|
|
3659
|
+
}
|
|
1563
3660
|
await exclusive(async () => {
|
|
1564
3661
|
if (!continuing())
|
|
1565
3662
|
return;
|
|
@@ -1567,27 +3664,99 @@ export function composeRuntime(options) {
|
|
|
1567
3664
|
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
1568
3665
|
return;
|
|
1569
3666
|
}
|
|
1570
|
-
const
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
});
|
|
1576
|
-
|
|
3667
|
+
const confirmedLayer = await sliceAt(current.record, 'confirmed', [head]);
|
|
3668
|
+
const held = (result.kind === 'expired' &&
|
|
3669
|
+
(await landedWhole(room.locator, head, confirmedLayer))) ||
|
|
3670
|
+
(result.kind === 'conflicted' &&
|
|
3671
|
+
(await landedWhole(room.locator, head, confirmedLayer, result.affected)));
|
|
3672
|
+
publishChangeSet(await settle(local, room.locator, current, current.record, current.queue.filter((batch) => batch.id !== head.id), { leaving: [held ? { ...left, returned: null } : left] }));
|
|
3673
|
+
await unpair(local);
|
|
1577
3674
|
moveTo({ ...status, ...attention() });
|
|
1578
3675
|
});
|
|
1579
3676
|
if (result.kind === 'expired')
|
|
1580
3677
|
continue;
|
|
1581
|
-
const
|
|
1582
|
-
if (
|
|
1583
|
-
|
|
3678
|
+
const { there, here } = judged ?? (await refreshed());
|
|
3679
|
+
if (other !== undefined && there !== null)
|
|
3680
|
+
await withhold(there, other, local, progress);
|
|
3681
|
+
if (here !== null) {
|
|
3682
|
+
await withhold(here, room, local, progress);
|
|
3683
|
+
return 'done';
|
|
3684
|
+
}
|
|
1584
3685
|
continue;
|
|
1585
3686
|
}
|
|
1586
|
-
const kept = await
|
|
3687
|
+
const kept = await (partner === null
|
|
3688
|
+
? adopted(room, local, head, result)
|
|
3689
|
+
: adoptedMove(room, partner, local, head, result)).finally(answered);
|
|
1587
3690
|
if (!kept) {
|
|
1588
3691
|
progress.complete = false;
|
|
1589
|
-
return;
|
|
3692
|
+
return 'done';
|
|
3693
|
+
}
|
|
3694
|
+
}
|
|
3695
|
+
return 'done';
|
|
3696
|
+
}
|
|
3697
|
+
function pastHorizon(identity) {
|
|
3698
|
+
return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
|
|
3699
|
+
}
|
|
3700
|
+
async function discardPending(rooms, local, signal) {
|
|
3701
|
+
const discarded = await exclusive(async () => {
|
|
3702
|
+
const held = rooms
|
|
3703
|
+
.filter(({ locator }) => !fences.has(locator))
|
|
3704
|
+
.map((room) => ({
|
|
3705
|
+
room,
|
|
3706
|
+
before: replicas.get(room.locator),
|
|
3707
|
+
queue: queueIn(room.locator),
|
|
3708
|
+
}));
|
|
3709
|
+
if (held.length === 0)
|
|
3710
|
+
return [];
|
|
3711
|
+
const answer = await local.commit({
|
|
3712
|
+
rooms: held.flatMap(({ room, before }) => before === undefined
|
|
3713
|
+
? []
|
|
3714
|
+
: [
|
|
3715
|
+
{
|
|
3716
|
+
locator: room.locator,
|
|
3717
|
+
key: before.record.key,
|
|
3718
|
+
roots: before.record.typedSelection?.roots ?? [],
|
|
3719
|
+
overlay: { set: [], cleared: [...before.overlaid] },
|
|
3720
|
+
},
|
|
3721
|
+
]),
|
|
3722
|
+
discards: held.map(({ room }) => room.locator),
|
|
3723
|
+
});
|
|
3724
|
+
for (const { room, before, queue } of held) {
|
|
3725
|
+
const { locator } = room;
|
|
3726
|
+
lastFenced.set(locator, ++fencings);
|
|
3727
|
+
for (const batch of queue)
|
|
3728
|
+
pendings.delete(pendingKey(locator, batch.id));
|
|
3729
|
+
queuesWithoutReplica.delete(locator);
|
|
3730
|
+
if (before !== undefined) {
|
|
3731
|
+
replicas.set(locator, {
|
|
3732
|
+
...before,
|
|
3733
|
+
queue: [],
|
|
3734
|
+
overlaid: new Set(),
|
|
3735
|
+
count: answer.counts[locator] ?? before.count,
|
|
3736
|
+
});
|
|
3737
|
+
}
|
|
3738
|
+
journal.record(`room discarded ${locator}`);
|
|
1590
3739
|
}
|
|
3740
|
+
publishChangeSet(mergeChangeSets(held.map(({ room }) => changeSet(room.locator, {
|
|
3741
|
+
documents: answer.changed[room.locator] ?? [],
|
|
3742
|
+
queue: true,
|
|
3743
|
+
}))));
|
|
3744
|
+
moveTo({ ...status, ...attention() });
|
|
3745
|
+
return held.map(({ room }) => room);
|
|
3746
|
+
});
|
|
3747
|
+
for (const room of discarded) {
|
|
3748
|
+
if (signal.aborted)
|
|
3749
|
+
return;
|
|
3750
|
+
const progress = { complete: true, reachable: true };
|
|
3751
|
+
await roomTask(room.locator, async (live) => {
|
|
3752
|
+
await Promise.race([
|
|
3753
|
+
reload(room, local, progress, () => live() && !signal.aborted),
|
|
3754
|
+
abandonedOn(signal),
|
|
3755
|
+
]);
|
|
3756
|
+
});
|
|
3757
|
+
if (signal.aborted)
|
|
3758
|
+
return;
|
|
3759
|
+
await rest(progress);
|
|
1591
3760
|
}
|
|
1592
3761
|
}
|
|
1593
3762
|
function awaitingAnswer(locator) {
|
|
@@ -1605,7 +3774,7 @@ export function composeRuntime(options) {
|
|
|
1605
3774
|
async function retainModel(local, room, attached, next) {
|
|
1606
3775
|
const key = modelKey(room.locator, attached);
|
|
1607
3776
|
const previous = modelOf(room.locator, attached);
|
|
1608
|
-
const heldBefore = replicas.get(room.locator)?.
|
|
3777
|
+
const heldBefore = replicas.get(room.locator)?.record;
|
|
1609
3778
|
if (JSON.stringify(previous) === JSON.stringify(next) &&
|
|
1610
3779
|
(heldBefore === undefined ||
|
|
1611
3780
|
(JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
|
|
@@ -1621,36 +3790,56 @@ export function composeRuntime(options) {
|
|
|
1621
3790
|
if (!continuing())
|
|
1622
3791
|
return;
|
|
1623
3792
|
const held = replicas.get(room.locator);
|
|
3793
|
+
let changed = null;
|
|
1624
3794
|
if (held !== undefined) {
|
|
1625
3795
|
const declared = {
|
|
1626
|
-
...held.
|
|
3796
|
+
...held.record,
|
|
1627
3797
|
operations: room.operations,
|
|
1628
3798
|
replica: room.replica,
|
|
1629
3799
|
typedSelection: room.typedSelection ?? null,
|
|
1630
3800
|
...(attached === 'collection' ? { model: next } : { selectionModel: next }),
|
|
1631
3801
|
};
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
3802
|
+
let read = 'staged';
|
|
3803
|
+
if (!sameMembership(declared, held.record)) {
|
|
3804
|
+
const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), compositionOf(declared));
|
|
3805
|
+
read =
|
|
3806
|
+
verdict.verdict !== 'refuse'
|
|
3807
|
+
? 'staged'
|
|
3808
|
+
: verdict.rule === 'model-unavailable'
|
|
3809
|
+
? 'deferred'
|
|
3810
|
+
: { kind: 'refused', refusal: verdict };
|
|
3811
|
+
}
|
|
3812
|
+
const record = read === 'staged'
|
|
1636
3813
|
? declared
|
|
1637
|
-
: {
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
const
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
3814
|
+
: { ...declared, typedSelection: held.record.typedSelection };
|
|
3815
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', held.queue, held.overlaid);
|
|
3816
|
+
const replayedLayer = await replayed(record, confirmedLayer, held.queue);
|
|
3817
|
+
const diff = await overlayDiff(record, held, replayedLayer.visible, replayedLayer.touched);
|
|
3818
|
+
const answer = await local.commit({
|
|
3819
|
+
rooms: [
|
|
3820
|
+
{
|
|
3821
|
+
locator: room.locator,
|
|
3822
|
+
key: record.key,
|
|
3823
|
+
record: storedRecord(record),
|
|
3824
|
+
roots: record.typedSelection?.roots ?? [],
|
|
3825
|
+
composition: compositionOf(record),
|
|
3826
|
+
overlay: diff.overlay,
|
|
3827
|
+
},
|
|
3828
|
+
],
|
|
3829
|
+
});
|
|
3830
|
+
const { count, indexed } = committedRoom(room.locator, answer);
|
|
3831
|
+
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count, indexed });
|
|
3832
|
+
changed = changeSet(room.locator, {
|
|
3833
|
+
documents: answer.changed[room.locator] ?? [],
|
|
3834
|
+
room: true,
|
|
3835
|
+
});
|
|
3836
|
+
if (read === 'staged' || read === 'deferred') {
|
|
1647
3837
|
if (declarationRefusals.delete(room.locator)) {
|
|
1648
3838
|
sourceRefusals.delete(room.locator);
|
|
1649
3839
|
unreadable.delete(room.locator);
|
|
1650
3840
|
}
|
|
1651
3841
|
}
|
|
1652
|
-
else
|
|
1653
|
-
replicas.set(room.locator, { ...held, confirmed: { ...held.confirmed, record } });
|
|
3842
|
+
else {
|
|
1654
3843
|
refusedSource(room, read);
|
|
1655
3844
|
declarationRefusals.add(room.locator);
|
|
1656
3845
|
}
|
|
@@ -1661,13 +3850,18 @@ export function composeRuntime(options) {
|
|
|
1661
3850
|
invalidModels.delete(key);
|
|
1662
3851
|
}
|
|
1663
3852
|
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1664
|
-
if (
|
|
3853
|
+
if (changed === null)
|
|
1665
3854
|
emitRooms();
|
|
1666
3855
|
else
|
|
1667
|
-
publishChangeSet(
|
|
3856
|
+
publishChangeSet(changed);
|
|
1668
3857
|
});
|
|
1669
3858
|
}
|
|
1670
3859
|
async function scoped(result, room, local) {
|
|
3860
|
+
if (result.scope === 'content' && !result.retryable) {
|
|
3861
|
+
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
3862
|
+
await loseRooms([room.locator]);
|
|
3863
|
+
return;
|
|
3864
|
+
}
|
|
1671
3865
|
scopedRefusals.set(room.locator, {
|
|
1672
3866
|
code: result.code,
|
|
1673
3867
|
fields: result.fields,
|
|
@@ -1681,15 +3875,7 @@ export function composeRuntime(options) {
|
|
|
1681
3875
|
});
|
|
1682
3876
|
else if (result.scope !== 'operation') {
|
|
1683
3877
|
unreadable.add(room.locator);
|
|
1684
|
-
scopedAway = {
|
|
1685
|
-
...room,
|
|
1686
|
-
replica: { ...room.replica, admission: 'unavailable' },
|
|
1687
|
-
operations: {
|
|
1688
|
-
...room.operations,
|
|
1689
|
-
...(result.scope === 'content' ? { read: [] } : {}),
|
|
1690
|
-
...(result.scope === 'sync' ? { sync: [] } : {}),
|
|
1691
|
-
},
|
|
1692
|
-
};
|
|
3878
|
+
scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
|
|
1693
3879
|
await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
|
|
1694
3880
|
}
|
|
1695
3881
|
if (!continuing())
|
|
@@ -1700,17 +3886,10 @@ export function composeRuntime(options) {
|
|
|
1700
3886
|
}
|
|
1701
3887
|
function heldRooms(listed) {
|
|
1702
3888
|
return listed.flatMap((room) => {
|
|
1703
|
-
const record = replicas.get(room.locator)?.
|
|
3889
|
+
const record = replicas.get(room.locator)?.record;
|
|
1704
3890
|
if (record === undefined || record.replica.selection !== room.replica.selection)
|
|
1705
3891
|
return [];
|
|
1706
|
-
return [
|
|
1707
|
-
{
|
|
1708
|
-
locator: room.locator,
|
|
1709
|
-
roomVersion: record.roomVersion,
|
|
1710
|
-
documents: record.documents,
|
|
1711
|
-
revisions: record.revisions,
|
|
1712
|
-
},
|
|
1713
|
-
];
|
|
3892
|
+
return [{ locator: room.locator, roomVersion: record.roomVersion }];
|
|
1714
3893
|
});
|
|
1715
3894
|
}
|
|
1716
3895
|
async function synchronize(listed, local, progress, attempt, live) {
|
|
@@ -1738,7 +3917,7 @@ export function composeRuntime(options) {
|
|
|
1738
3917
|
progress.complete = false;
|
|
1739
3918
|
return false;
|
|
1740
3919
|
}
|
|
1741
|
-
const prior = replicas.get(room.locator)?.
|
|
3920
|
+
const prior = replicas.get(room.locator)?.record;
|
|
1742
3921
|
if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
|
|
1743
3922
|
list(prior, false);
|
|
1744
3923
|
emitRooms();
|
|
@@ -1750,19 +3929,10 @@ export function composeRuntime(options) {
|
|
|
1750
3929
|
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
1751
3930
|
if (discoveryPaused || authorization === undefined)
|
|
1752
3931
|
return false;
|
|
1753
|
-
|
|
1754
|
-
if (replicas.has(room.locator))
|
|
1755
|
-
progress.complete = false;
|
|
1756
|
-
return false;
|
|
1757
|
-
}
|
|
1758
|
-
return true;
|
|
3932
|
+
return room.replica.admission === 'admitted';
|
|
1759
3933
|
}
|
|
1760
3934
|
async function caughtUp(room, local, progress, live) {
|
|
1761
|
-
|
|
1762
|
-
if (held === undefined || authorization === undefined || !live())
|
|
1763
|
-
return;
|
|
1764
|
-
const behind = [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later));
|
|
1765
|
-
if (!behind)
|
|
3935
|
+
if (authorization === undefined || !live() || !behind(room))
|
|
1766
3936
|
return;
|
|
1767
3937
|
const withheld = await advance(room, local, authorization, live);
|
|
1768
3938
|
if (withheld !== null)
|
|
@@ -1789,19 +3959,25 @@ export function composeRuntime(options) {
|
|
|
1789
3959
|
await caughtUp(room, local, progress, live);
|
|
1790
3960
|
}
|
|
1791
3961
|
async function reload(room, local, progress, live) {
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
3962
|
+
try {
|
|
3963
|
+
if (live())
|
|
3964
|
+
reloadsDue.delete(room.locator);
|
|
3965
|
+
if (!live() || authorization === undefined)
|
|
3966
|
+
return;
|
|
3967
|
+
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
3968
|
+
if (bootstrapped.kind !== 'staged') {
|
|
3969
|
+
if (live())
|
|
3970
|
+
await withhold(bootstrapped, room, local, progress);
|
|
3971
|
+
return;
|
|
3972
|
+
}
|
|
3973
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
3974
|
+
await caughtUp(room, local, progress, live);
|
|
3975
|
+
}
|
|
3976
|
+
finally {
|
|
3977
|
+
caughtUpTo(room.locator);
|
|
1800
3978
|
}
|
|
1801
|
-
await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
|
|
1802
|
-
await caughtUp(room, local, progress, live);
|
|
1803
3979
|
}
|
|
1804
|
-
async function discoverRooms(local, progress, attempt, live) {
|
|
3980
|
+
async function discoverRooms(local, progress, attempt, live, scope) {
|
|
1805
3981
|
if (closed || authorization === undefined) {
|
|
1806
3982
|
notifyReady();
|
|
1807
3983
|
return;
|
|
@@ -1811,7 +3987,7 @@ export function composeRuntime(options) {
|
|
|
1811
3987
|
const started = discoveries;
|
|
1812
3988
|
let result;
|
|
1813
3989
|
try {
|
|
1814
|
-
result = await options.adapter.discover(authorization);
|
|
3990
|
+
result = await options.adapter.discover(authorization, scope);
|
|
1815
3991
|
}
|
|
1816
3992
|
catch {
|
|
1817
3993
|
result = { kind: 'transport-uncertainty' };
|
|
@@ -1851,12 +4027,13 @@ export function composeRuntime(options) {
|
|
|
1851
4027
|
}
|
|
1852
4028
|
for (const room of result.rooms) {
|
|
1853
4029
|
if (room.operations === undefined ||
|
|
1854
|
-
!['
|
|
4030
|
+
!['write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
|
|
1855
4031
|
room.operations[operation].every((scope) => typeof scope === 'string')) ||
|
|
1856
4032
|
room.replica === undefined ||
|
|
1857
4033
|
typeof room.replica.selection !== 'string' ||
|
|
1858
4034
|
room.replica.selection.length === 0 ||
|
|
1859
|
-
!['admitted', 'unavailable'].includes(room.replica.admission)
|
|
4035
|
+
!['admitted', 'unavailable'].includes(room.replica.admission) ||
|
|
4036
|
+
!['locked', 'open'].includes(room.replica.offline)) {
|
|
1860
4037
|
journal.record(`invalid model or replica declaration ${room.locator}`);
|
|
1861
4038
|
await refuse('missing-required-guarantee');
|
|
1862
4039
|
notifyReady();
|
|
@@ -1875,19 +4052,21 @@ export function composeRuntime(options) {
|
|
|
1875
4052
|
}
|
|
1876
4053
|
const previousCapabilities = capabilities;
|
|
1877
4054
|
capabilities = result.capabilities;
|
|
4055
|
+
outcomeRetention = result.retention.outcome;
|
|
1878
4056
|
notifyReady();
|
|
1879
4057
|
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
1880
4058
|
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
1881
4059
|
limits = result.providerLimits;
|
|
1882
|
-
await local.
|
|
4060
|
+
await local.fact('providerLimits', limits);
|
|
4061
|
+
await local.fact('capabilities', capabilities);
|
|
1883
4062
|
}
|
|
1884
4063
|
const signedIn = result.member ?? null;
|
|
1885
4064
|
if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
|
|
1886
|
-
await local.
|
|
4065
|
+
await local.fact('member', signedIn);
|
|
1887
4066
|
member = signedIn;
|
|
1888
4067
|
emitMember();
|
|
1889
4068
|
}
|
|
1890
|
-
await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
|
|
4069
|
+
await synchronize(await applyDiscovery(result.rooms, started, result.covered), local, progress, attempt, live);
|
|
1891
4070
|
}
|
|
1892
4071
|
async function ensureAuthorization() {
|
|
1893
4072
|
if (authorization !== undefined)
|
|
@@ -1984,9 +4163,44 @@ export function composeRuntime(options) {
|
|
|
1984
4163
|
options.adapter.roomStream?.reopen();
|
|
1985
4164
|
});
|
|
1986
4165
|
}
|
|
1987
|
-
function
|
|
4166
|
+
function rediscover(scope) {
|
|
4167
|
+
return task(OPENING_LANE, async (live) => {
|
|
4168
|
+
if (!live() || !socketOpen)
|
|
4169
|
+
return;
|
|
4170
|
+
const progress = { complete: true, reachable: true };
|
|
4171
|
+
const gate = discover();
|
|
4172
|
+
moveTo(transferring(status));
|
|
4173
|
+
try {
|
|
4174
|
+
const local = await openLocal();
|
|
4175
|
+
if (!live())
|
|
4176
|
+
return;
|
|
4177
|
+
await ensureAuthorization();
|
|
4178
|
+
if (!live())
|
|
4179
|
+
return;
|
|
4180
|
+
await discoverRooms(local, progress, 'authorized', live, scope);
|
|
4181
|
+
}
|
|
4182
|
+
catch {
|
|
4183
|
+
progress.complete = false;
|
|
4184
|
+
}
|
|
4185
|
+
finally {
|
|
4186
|
+
gate.release();
|
|
4187
|
+
notifyReady();
|
|
4188
|
+
}
|
|
4189
|
+
if (!live())
|
|
4190
|
+
return;
|
|
4191
|
+
if (!progress.complete)
|
|
4192
|
+
complete = false;
|
|
4193
|
+
await rest(progress, live);
|
|
4194
|
+
});
|
|
4195
|
+
}
|
|
4196
|
+
function closedSocket(revoked, lost) {
|
|
1988
4197
|
const opened = socketOpen;
|
|
1989
4198
|
socketOpen = false;
|
|
4199
|
+
connectionLost();
|
|
4200
|
+
if (lost) {
|
|
4201
|
+
reads.lost();
|
|
4202
|
+
conversations.lost();
|
|
4203
|
+
}
|
|
1990
4204
|
if (revoked) {
|
|
1991
4205
|
void refuse('revoked-authorization');
|
|
1992
4206
|
return;
|
|
@@ -2003,7 +4217,10 @@ export function composeRuntime(options) {
|
|
|
2003
4217
|
function applyOpening(expired) {
|
|
2004
4218
|
const progress = underway?.generation === generation ? underway.progress : null;
|
|
2005
4219
|
const cycle = progress ?? { complete: true, reachable: true };
|
|
2006
|
-
|
|
4220
|
+
if (opened === generation)
|
|
4221
|
+
opened = null;
|
|
4222
|
+
const reloading = progress === null ? [] : owedReloads(expired);
|
|
4223
|
+
const replacing = reloadNow((room) => expired.includes(room.locator) || reloading.includes(room.locator), cycle);
|
|
2007
4224
|
return task(OPENING_LANE, async (live) => {
|
|
2008
4225
|
underway = null;
|
|
2009
4226
|
if (!live())
|
|
@@ -2013,23 +4230,17 @@ export function composeRuntime(options) {
|
|
|
2013
4230
|
await rest(cycle, live);
|
|
2014
4231
|
return;
|
|
2015
4232
|
}
|
|
2016
|
-
await roomsCarried(expired);
|
|
4233
|
+
await roomsCarried([...expired, ...reloading]);
|
|
2017
4234
|
if (!live())
|
|
2018
4235
|
return;
|
|
2019
|
-
if (opened === generation)
|
|
2020
|
-
opened = null;
|
|
2021
|
-
const replaced = new Set(owed);
|
|
2022
|
-
owed.clear();
|
|
2023
4236
|
try {
|
|
2024
4237
|
const local = await openLocal();
|
|
2025
|
-
const
|
|
2026
|
-
const reloading = held.filter((room) => !expired.includes(room.locator) &&
|
|
2027
|
-
(replaced.has(room.locator) || revoked(room.locator)));
|
|
2028
|
-
const reloads = Promise.allSettled(reloading.map((room) => roomTask(room.locator, (current) => reload(room, local, progress, current))));
|
|
2029
|
-
const reloaded = [...(await reloads), ...(await replacing)];
|
|
4238
|
+
const reloaded = await replacing;
|
|
2030
4239
|
if (progress.reachable) {
|
|
2031
|
-
for (const room of
|
|
2032
|
-
if (
|
|
4240
|
+
for (const room of discovered.values()) {
|
|
4241
|
+
if (replicas.has(room.locator) &&
|
|
4242
|
+
!expired.includes(room.locator) &&
|
|
4243
|
+
!reloading.includes(room.locator)) {
|
|
2033
4244
|
await confirmModels(room, local);
|
|
2034
4245
|
}
|
|
2035
4246
|
}
|
|
@@ -2046,14 +4257,29 @@ export function composeRuntime(options) {
|
|
|
2046
4257
|
await finishOpening(progress, live);
|
|
2047
4258
|
});
|
|
2048
4259
|
}
|
|
4260
|
+
function owedReloads(expired) {
|
|
4261
|
+
const reloading = [...discovered.values()]
|
|
4262
|
+
.filter((room) => replicas.has(room.locator) &&
|
|
4263
|
+
!expired.includes(room.locator) &&
|
|
4264
|
+
(owed.has(room.locator) || revoked(room.locator)))
|
|
4265
|
+
.map((room) => room.locator);
|
|
4266
|
+
owed.clear();
|
|
4267
|
+
for (const locator of reloading)
|
|
4268
|
+
catching.add(locator);
|
|
4269
|
+
liveQueries.recover();
|
|
4270
|
+
return reloading;
|
|
4271
|
+
}
|
|
2049
4272
|
async function reloadNow(replaced, progress) {
|
|
4273
|
+
const reloaded = [...discovered.values()].filter((room) => replicas.has(room.locator) && replaced(room));
|
|
4274
|
+
for (const room of reloaded)
|
|
4275
|
+
reloadsDue.set(room.locator, generation);
|
|
2050
4276
|
try {
|
|
2051
4277
|
const local = await openLocal();
|
|
2052
|
-
return await Promise.allSettled(
|
|
2053
|
-
.filter((room) => replicas.has(room.locator) && replaced(room))
|
|
2054
|
-
.map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
|
|
4278
|
+
return await Promise.allSettled(reloaded.map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
|
|
2055
4279
|
}
|
|
2056
4280
|
catch {
|
|
4281
|
+
for (const room of reloaded)
|
|
4282
|
+
reloadsDue.delete(room.locator);
|
|
2057
4283
|
progress.reachable = false;
|
|
2058
4284
|
return [];
|
|
2059
4285
|
}
|
|
@@ -2069,38 +4295,94 @@ export function composeRuntime(options) {
|
|
|
2069
4295
|
function submitLater(locator) {
|
|
2070
4296
|
if (localSaves)
|
|
2071
4297
|
return;
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
4298
|
+
void recovery.attempt(`submit ${locator}`, (signal) => submitPass(locator, signal));
|
|
4299
|
+
}
|
|
4300
|
+
async function submitPass(locator, signal) {
|
|
4301
|
+
const room = discovered.get(locator);
|
|
4302
|
+
if (closed || stopping || discoveryPaused || room === undefined)
|
|
4303
|
+
return 'done';
|
|
4304
|
+
const progress = { complete: true, reachable: true };
|
|
4305
|
+
let next;
|
|
4306
|
+
try {
|
|
4307
|
+
await ensureAuthorization();
|
|
4308
|
+
if (authorization === undefined)
|
|
4309
|
+
return 'done';
|
|
4310
|
+
next = await submitRoom(room, await openLocal(), authorization, progress, signal);
|
|
4311
|
+
}
|
|
4312
|
+
catch {
|
|
4313
|
+
progress.reachable = false;
|
|
4314
|
+
next = 'again';
|
|
4315
|
+
}
|
|
4316
|
+
if (signal.aborted)
|
|
4317
|
+
return 'done';
|
|
4318
|
+
await rest(progress);
|
|
4319
|
+
return next === 'again' && !socketOpen ? 'done' : next;
|
|
4320
|
+
}
|
|
4321
|
+
function repairLater(locator, when) {
|
|
4322
|
+
if (!socketOpen || repairing.has(locator))
|
|
2075
4323
|
return;
|
|
4324
|
+
void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
|
|
4325
|
+
}
|
|
4326
|
+
async function repairPass(locator, signal) {
|
|
4327
|
+
const room = discovered.get(locator);
|
|
4328
|
+
if (!socketOpen || closed || stopping || discoveryPaused || !stale.has(locator))
|
|
4329
|
+
return 'done';
|
|
4330
|
+
if (room === undefined || room.replica.admission !== 'admitted')
|
|
4331
|
+
return 'done';
|
|
4332
|
+
const progress = { complete: true, reachable: true };
|
|
4333
|
+
repairing.add(locator);
|
|
4334
|
+
try {
|
|
4335
|
+
const local = await openLocal();
|
|
4336
|
+
await roomTask(locator, async (live) => {
|
|
4337
|
+
const current = () => live() && !signal.aborted;
|
|
4338
|
+
if (!current() || authorization === undefined)
|
|
4339
|
+
return;
|
|
4340
|
+
const withheld = await Promise.race([
|
|
4341
|
+
advance(room, local, authorization, current),
|
|
4342
|
+
abandonedOn(signal),
|
|
4343
|
+
]);
|
|
4344
|
+
if (withheld !== null && current())
|
|
4345
|
+
await withhold(withheld, room, local, progress);
|
|
4346
|
+
});
|
|
2076
4347
|
}
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
4348
|
+
catch {
|
|
4349
|
+
stale.add(locator);
|
|
4350
|
+
progress.reachable = false;
|
|
4351
|
+
}
|
|
4352
|
+
finally {
|
|
4353
|
+
repairing.delete(locator);
|
|
4354
|
+
}
|
|
4355
|
+
if (signal.aborted)
|
|
4356
|
+
return 'done';
|
|
4357
|
+
await rest(progress);
|
|
4358
|
+
if (!stale.has(locator)) {
|
|
4359
|
+
if ((replicas.get(locator)?.queue.length ?? 0) > 0)
|
|
4360
|
+
submitLater(locator);
|
|
4361
|
+
return 'done';
|
|
4362
|
+
}
|
|
4363
|
+
if (!repairable.has(locator))
|
|
4364
|
+
return 'done';
|
|
4365
|
+
const reset = repairable.get(locator);
|
|
4366
|
+
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
4367
|
+
}
|
|
4368
|
+
function resendLater() {
|
|
4369
|
+
if (!socketOpen)
|
|
4370
|
+
return;
|
|
4371
|
+
void recovery.attempt('resend', resendPass, { again: true });
|
|
4372
|
+
}
|
|
4373
|
+
async function resendPass(signal) {
|
|
4374
|
+
if (!socketOpen || closed || stopping || discoveryPaused)
|
|
4375
|
+
return 'done';
|
|
4376
|
+
const reading = { complete: true, reachable: true };
|
|
4377
|
+
try {
|
|
4378
|
+
await conversations.resend(reading);
|
|
4379
|
+
}
|
|
4380
|
+
catch {
|
|
4381
|
+
reading.reachable = false;
|
|
4382
|
+
}
|
|
4383
|
+
if (signal.aborted)
|
|
4384
|
+
return 'done';
|
|
4385
|
+
return reading.reachable && reading.complete ? 'done' : 'again';
|
|
2104
4386
|
}
|
|
2105
4387
|
function submitAll() {
|
|
2106
4388
|
for (const [locator, replica] of replicas) {
|
|
@@ -2114,7 +4396,11 @@ export function composeRuntime(options) {
|
|
|
2114
4396
|
progress.complete = false;
|
|
2115
4397
|
return;
|
|
2116
4398
|
}
|
|
4399
|
+
reads.applied();
|
|
2117
4400
|
submitAll();
|
|
4401
|
+
for (const locator of stale)
|
|
4402
|
+
if (repairable.has(locator))
|
|
4403
|
+
repairLater(locator, {});
|
|
2118
4404
|
await conversations.reconcile();
|
|
2119
4405
|
if (status.providerUnreachable)
|
|
2120
4406
|
conversations.reconnected();
|
|
@@ -2122,8 +4408,12 @@ export function composeRuntime(options) {
|
|
|
2122
4408
|
await conversations.catchUp(reading);
|
|
2123
4409
|
if (progress.reachable && reading.reachable)
|
|
2124
4410
|
await conversations.resend(reading);
|
|
2125
|
-
if (
|
|
4411
|
+
if (progress.reachable)
|
|
4412
|
+
void recovery.attempt('calls', resendCalls);
|
|
4413
|
+
if (!reading.reachable) {
|
|
2126
4414
|
progress.reachable = false;
|
|
4415
|
+
resendLater();
|
|
4416
|
+
}
|
|
2127
4417
|
}
|
|
2128
4418
|
catch {
|
|
2129
4419
|
progress.complete = false;
|
|
@@ -2143,7 +4433,7 @@ export function composeRuntime(options) {
|
|
|
2143
4433
|
if (synced && !recorded) {
|
|
2144
4434
|
const completedAt = new Date().toISOString();
|
|
2145
4435
|
try {
|
|
2146
|
-
await (await openLocal()).
|
|
4436
|
+
await (await openLocal()).fact('lastCompleted', completedAt);
|
|
2147
4437
|
if (!live())
|
|
2148
4438
|
return;
|
|
2149
4439
|
recorded = true;
|
|
@@ -2183,8 +4473,8 @@ export function composeRuntime(options) {
|
|
|
2183
4473
|
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2184
4474
|
return;
|
|
2185
4475
|
if (held !== undefined &&
|
|
2186
|
-
(held.
|
|
2187
|
-
options.adapter.reached(locator, held.
|
|
4476
|
+
(held.record.replica.selection !== room.replica.selection ||
|
|
4477
|
+
options.adapter.reached(locator, held.record.roomVersion, roomVersion))) {
|
|
2188
4478
|
return;
|
|
2189
4479
|
}
|
|
2190
4480
|
const during = underway?.generation === generation ? underway.progress : null;
|
|
@@ -2231,16 +4521,29 @@ export function composeRuntime(options) {
|
|
|
2231
4521
|
return;
|
|
2232
4522
|
if (event.kind === 'opened') {
|
|
2233
4523
|
socketOpen = true;
|
|
4524
|
+
connectionLive();
|
|
2234
4525
|
generation += 1;
|
|
4526
|
+
reads.opened();
|
|
4527
|
+
conversations.reopened();
|
|
4528
|
+
recovery.resume();
|
|
2235
4529
|
void sync('opened');
|
|
2236
4530
|
}
|
|
2237
4531
|
else if (event.kind === 'closed')
|
|
2238
|
-
closedSocket(event.revoked);
|
|
4532
|
+
closedSocket(event.revoked, event.lost);
|
|
2239
4533
|
else if (event.kind === 'opening')
|
|
2240
4534
|
void applyOpening(event.expired);
|
|
4535
|
+
else if (event.kind === 'authority-changed')
|
|
4536
|
+
void rediscover(event.scope);
|
|
2241
4537
|
else
|
|
2242
4538
|
void applyRoom(event.locator, event.roomVersion);
|
|
2243
|
-
}, receipts);
|
|
4539
|
+
}, receipts, heldRevisions);
|
|
4540
|
+
}
|
|
4541
|
+
async function heldRevisions(locator, paths) {
|
|
4542
|
+
const held = replicas.get(locator);
|
|
4543
|
+
if (held === undefined || paths.length === 0)
|
|
4544
|
+
return new Map();
|
|
4545
|
+
const entries = await (await openLocal()).entries(held.record.key, 'confirmed', paths);
|
|
4546
|
+
return new Map(entries.flatMap(({ path, revision }) => revision === null ? [] : [[path, revision]]));
|
|
2244
4547
|
}
|
|
2245
4548
|
function writesHeld() {
|
|
2246
4549
|
return serviceMode === 'write-halted' || haltedRefusal;
|
|
@@ -2255,6 +4558,7 @@ export function composeRuntime(options) {
|
|
|
2255
4558
|
haltedRefusal = false;
|
|
2256
4559
|
if (mode !== serviceMode) {
|
|
2257
4560
|
serviceMode = mode;
|
|
4561
|
+
queueReadiness();
|
|
2258
4562
|
journal.record(`service mode ${mode}`);
|
|
2259
4563
|
for (const listener of serviceModeListeners)
|
|
2260
4564
|
listener(mode);
|
|
@@ -2267,6 +4571,8 @@ export function composeRuntime(options) {
|
|
|
2267
4571
|
if (stream === undefined || withdrawConversations !== undefined)
|
|
2268
4572
|
return;
|
|
2269
4573
|
withdrawConversations = stream.observe((event) => {
|
|
4574
|
+
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted')
|
|
4575
|
+
queueReadiness();
|
|
2270
4576
|
if (conversations.receive(event)) {
|
|
2271
4577
|
void ready.then(scheduleCurrency);
|
|
2272
4578
|
return;
|
|
@@ -2307,33 +4613,35 @@ export function composeRuntime(options) {
|
|
|
2307
4613
|
if (closed)
|
|
2308
4614
|
return;
|
|
2309
4615
|
closed = true;
|
|
4616
|
+
if (probing !== null)
|
|
4617
|
+
clearInterval(probing);
|
|
4618
|
+
for (const resolve of shownWaiters.splice(0))
|
|
4619
|
+
resolve();
|
|
4620
|
+
connectionLive();
|
|
4621
|
+
lockListeners.clear();
|
|
4622
|
+
readinessWatches.clear();
|
|
2310
4623
|
withdrawStream?.();
|
|
2311
4624
|
withdrawStream = undefined;
|
|
2312
4625
|
withdrawConversations?.();
|
|
2313
4626
|
withdrawConversations = undefined;
|
|
2314
4627
|
withdrawServiceMode?.();
|
|
2315
4628
|
conversations.close();
|
|
2316
|
-
|
|
2317
|
-
store?.close();
|
|
2318
|
-
void opening?.then((opened) => {
|
|
2319
|
-
opened.close();
|
|
2320
|
-
}, () => { });
|
|
4629
|
+
void recovery.retire();
|
|
2321
4630
|
roomListeners.clear();
|
|
2322
4631
|
refusalListeners.clear();
|
|
2323
4632
|
storageLossListeners.clear();
|
|
2324
4633
|
statusListeners.clear();
|
|
2325
4634
|
serviceModeListeners.clear();
|
|
2326
4635
|
memberListeners.clear();
|
|
2327
|
-
|
|
4636
|
+
liveQueries.close();
|
|
2328
4637
|
queueSubscriptions.clear();
|
|
2329
4638
|
roomDraftListeners.clear();
|
|
2330
4639
|
presentationListeners.clear();
|
|
2331
4640
|
}
|
|
2332
4641
|
async function stop() {
|
|
2333
4642
|
stopping = true;
|
|
2334
|
-
|
|
4643
|
+
await recovery.retire();
|
|
2335
4644
|
await Promise.all(working);
|
|
2336
|
-
await Promise.all([...submitting.values()].map((run) => run.done));
|
|
2337
4645
|
await mutations;
|
|
2338
4646
|
close();
|
|
2339
4647
|
}
|
|
@@ -2352,20 +4660,99 @@ export function composeRuntime(options) {
|
|
|
2352
4660
|
if (options.adapter.roomStream === undefined)
|
|
2353
4661
|
void sync('unstreamed');
|
|
2354
4662
|
function planned(input, reach) {
|
|
2355
|
-
|
|
4663
|
+
const plan = planOf(input, roomFacts(reach), planReach(reach));
|
|
4664
|
+
const named = plan.paths !== null &&
|
|
4665
|
+
plan.rooms !== null &&
|
|
4666
|
+
plan.paths.length * plan.rooms.length <= MAXIMUM_WINDOW;
|
|
4667
|
+
return plan.limit === null && named ? plan : { ...plan, limit: capped(plan.limit) };
|
|
2356
4668
|
}
|
|
2357
4669
|
function watchPlan(plan, deliver) {
|
|
2358
|
-
|
|
2359
|
-
|
|
4670
|
+
let seeded = false;
|
|
4671
|
+
let pending = true;
|
|
4672
|
+
const shown = () => {
|
|
4673
|
+
const seed = seeds.get(plan.key);
|
|
4674
|
+
if (seed === undefined || seeded || !pending)
|
|
4675
|
+
return;
|
|
4676
|
+
seeded = true;
|
|
4677
|
+
deliver(seededResult(seed));
|
|
4678
|
+
};
|
|
4679
|
+
const watchers = seedWatchers.get(plan.key) ?? new Set();
|
|
4680
|
+
seedWatchers.set(plan.key, watchers);
|
|
4681
|
+
watchers.add(shown);
|
|
4682
|
+
const stop = liveQueries.watch(plan, identityOf(plan), () => {
|
|
4683
|
+
const cover = covered(plan);
|
|
4684
|
+
awaitPreparation(cover);
|
|
4685
|
+
return seedBehind(plan, cover);
|
|
4686
|
+
}, (message) => {
|
|
4687
|
+
const answered = message;
|
|
4688
|
+
pending = whole(answered) && 'pending' in answered.result;
|
|
4689
|
+
if (pending && seeds.has(plan.key)) {
|
|
4690
|
+
shown();
|
|
4691
|
+
return;
|
|
4692
|
+
}
|
|
4693
|
+
seeded = false;
|
|
4694
|
+
deliver(message);
|
|
4695
|
+
});
|
|
4696
|
+
return () => {
|
|
4697
|
+
watchers.delete(shown);
|
|
4698
|
+
if (watchers.size === 0)
|
|
4699
|
+
seedWatchers.delete(plan.key);
|
|
4700
|
+
stop();
|
|
4701
|
+
};
|
|
2360
4702
|
}
|
|
2361
|
-
async function searched(input, reach) {
|
|
2362
|
-
const
|
|
2363
|
-
const
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
4703
|
+
async function searched(input, reach, locked) {
|
|
4704
|
+
const asked = searchPlanOf(input);
|
|
4705
|
+
const filter = {
|
|
4706
|
+
...filterOf(asked.documents ?? {}, roomFacts(reach), planReach(reach)),
|
|
4707
|
+
locked,
|
|
4708
|
+
};
|
|
4709
|
+
const unlocking = (filter.rooms ?? [])
|
|
4710
|
+
.filter((room) => locked.includes(room))
|
|
4711
|
+
.map((room) => ({ room, awaits: 'unlock' }));
|
|
4712
|
+
for (;;) {
|
|
4713
|
+
const cover = covered(filter);
|
|
4714
|
+
awaitPreparation(cover);
|
|
4715
|
+
const pending = [...unlocking, ...('pending' in cover ? cover.pending : [])];
|
|
4716
|
+
const complete = pending.length === 0 && !('pending' in cover);
|
|
4717
|
+
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
4718
|
+
const answering = complete
|
|
4719
|
+
? filter
|
|
4720
|
+
: { ...filter, rooms: reachedRooms(filter).filter((room) => !awaited.has(room)) };
|
|
4721
|
+
const answered = covered(answering);
|
|
4722
|
+
if ('pending' in answered)
|
|
4723
|
+
continue;
|
|
4724
|
+
const listed = conversations.searchable();
|
|
4725
|
+
const named = reach !== null && !reach.conversations
|
|
4726
|
+
? []
|
|
4727
|
+
: (asked.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
4728
|
+
const hits = await (await openLocal()).search({
|
|
4729
|
+
text: asked.text,
|
|
4730
|
+
limit: capped(asked.limit),
|
|
4731
|
+
after: asked.after,
|
|
4732
|
+
total: asked.total,
|
|
4733
|
+
documents: {
|
|
4734
|
+
plan: answering,
|
|
4735
|
+
rooms: answered.rooms,
|
|
4736
|
+
beneath: asked.collection
|
|
4737
|
+
? answered.rooms.map(({ locator, key }) => ({
|
|
4738
|
+
key,
|
|
4739
|
+
roots: replicas.get(locator)?.record.typedSelection?.roots ?? [],
|
|
4740
|
+
}))
|
|
4741
|
+
: [],
|
|
4742
|
+
},
|
|
4743
|
+
conversations: named,
|
|
4744
|
+
});
|
|
4745
|
+
if (JSON.stringify(covered(answering)) !== JSON.stringify(answered))
|
|
4746
|
+
continue;
|
|
4747
|
+
const coverage = {
|
|
4748
|
+
rooms: answered.coverage.rooms,
|
|
4749
|
+
conversations: named,
|
|
4750
|
+
overlay: answered.coverage.overlay,
|
|
4751
|
+
};
|
|
4752
|
+
return complete
|
|
4753
|
+
? { ...hits, coverage }
|
|
4754
|
+
: { pending, documents: hits.documents, messages: hits.messages, coverage };
|
|
4755
|
+
}
|
|
2369
4756
|
}
|
|
2370
4757
|
const watch = (topic, input, deliver) => {
|
|
2371
4758
|
if (topic === 'room-drafts') {
|
|
@@ -2373,13 +4760,17 @@ export function composeRuntime(options) {
|
|
|
2373
4760
|
roomDraftListeners.add(listener);
|
|
2374
4761
|
void exclusive(async () => {
|
|
2375
4762
|
await openLocal();
|
|
2376
|
-
if (closed || !roomDraftListeners.has(listener))
|
|
2377
|
-
return;
|
|
2378
|
-
listener.ready = true;
|
|
2379
4763
|
}).then(() => {
|
|
2380
|
-
if (closed || !
|
|
4764
|
+
if (closed || !roomDraftListeners.has(listener))
|
|
2381
4765
|
return;
|
|
2382
|
-
|
|
4766
|
+
const snapshot = snapshotRoomDrafts();
|
|
4767
|
+
draftsShown = draftsShown.then(async () => {
|
|
4768
|
+
const value = await snapshot.catch(() => null);
|
|
4769
|
+
if (value === null || closed || !roomDraftListeners.has(listener))
|
|
4770
|
+
return;
|
|
4771
|
+
listener.ready = true;
|
|
4772
|
+
listener.notify(value);
|
|
4773
|
+
});
|
|
2383
4774
|
}, () => { });
|
|
2384
4775
|
return () => {
|
|
2385
4776
|
roomDraftListeners.delete(listener);
|
|
@@ -2411,6 +4802,9 @@ export function composeRuntime(options) {
|
|
|
2411
4802
|
presentationListeners.delete(listener);
|
|
2412
4803
|
};
|
|
2413
4804
|
}
|
|
4805
|
+
if (topic === 'readiness') {
|
|
4806
|
+
return watchReadiness({ target: '', code: 'stored', space: null, search: 'unused' }, null, deliver);
|
|
4807
|
+
}
|
|
2414
4808
|
if (topic === 'rooms') {
|
|
2415
4809
|
roomListeners.add(deliver);
|
|
2416
4810
|
deliver(listed());
|
|
@@ -2471,7 +4865,7 @@ export function composeRuntime(options) {
|
|
|
2471
4865
|
return listed();
|
|
2472
4866
|
},
|
|
2473
4867
|
query: ((input) => rowsQuery(planned(input, null))),
|
|
2474
|
-
search: (input) => searched(input, null),
|
|
4868
|
+
search: (input) => searched(input, null, []),
|
|
2475
4869
|
types(locator) {
|
|
2476
4870
|
return {
|
|
2477
4871
|
collection: modelTypes(modelOf(locator)),
|
|
@@ -2491,46 +4885,32 @@ export function composeRuntime(options) {
|
|
|
2491
4885
|
return listed;
|
|
2492
4886
|
});
|
|
2493
4887
|
},
|
|
2494
|
-
read(room, path) {
|
|
4888
|
+
async read(room, path) {
|
|
2495
4889
|
const record = roomDrafts.get(room)?.get(path);
|
|
2496
|
-
return record === undefined ? null :
|
|
4890
|
+
return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
|
|
2497
4891
|
},
|
|
2498
|
-
write(room, path, revision, input) {
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
const
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
? undefined
|
|
2510
|
-
: await heldFile(parts.file.blob, binaryBase(replicas.get(room)?.visible, path));
|
|
2511
|
-
const record = {
|
|
2512
|
-
locator: room,
|
|
2513
|
-
path,
|
|
2514
|
-
revision: roomDraftRevision(),
|
|
2515
|
-
parts: held(current?.parts, parts, file),
|
|
2516
|
-
returned: current?.returned ?? null,
|
|
2517
|
-
};
|
|
2518
|
-
await local.keepRoomDraft(record);
|
|
2519
|
-
rememberRoomDraft(record);
|
|
2520
|
-
publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
|
|
2521
|
-
return { outcome: 'written', roomDraft: roomDraftOf(record) };
|
|
2522
|
-
});
|
|
4892
|
+
write(room, path, revision, input, next = roomDraftRevision()) {
|
|
4893
|
+
if (typeof next !== 'string' || next === '')
|
|
4894
|
+
return Promise.reject(runtimeError('room-draft-malformed'));
|
|
4895
|
+
return gathered(`room-draft\u0000${room}\u0000${path}`, { revision, input, next }, (earlier, later) => {
|
|
4896
|
+
if (later.revision !== earlier.next)
|
|
4897
|
+
return null;
|
|
4898
|
+
const parts = gatheredParts(earlier.input, later.input);
|
|
4899
|
+
return parts === null
|
|
4900
|
+
? null
|
|
4901
|
+
: { revision: earlier.revision, input: parts, next: later.next };
|
|
4902
|
+
}, (written) => writtenRoomDraft(room, path, written.revision, written.input, written.next));
|
|
2523
4903
|
},
|
|
2524
4904
|
discard(room, path, revision) {
|
|
2525
4905
|
return exclusive(async () => {
|
|
2526
4906
|
const local = await openLocal();
|
|
2527
4907
|
const current = roomDrafts.get(room)?.get(path);
|
|
2528
4908
|
if (current === undefined || current.revision !== revision) {
|
|
2529
|
-
return staleWrite(current === undefined ? null :
|
|
4909
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
2530
4910
|
}
|
|
2531
4911
|
await local.discardRoomDraft(room, path);
|
|
2532
4912
|
forgetRoomDraft(room, path);
|
|
2533
|
-
publishChangeSet(changeSet(room,
|
|
4913
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
2534
4914
|
moveTo({ ...status, ...attention() });
|
|
2535
4915
|
return { outcome: 'written', roomDraft: null };
|
|
2536
4916
|
});
|
|
@@ -2546,29 +4926,38 @@ export function composeRuntime(options) {
|
|
|
2546
4926
|
});
|
|
2547
4927
|
},
|
|
2548
4928
|
write(app, value) {
|
|
2549
|
-
return exclusive(
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
4929
|
+
return exclusive(() => keptPresentation(app, () => structuredClone(value)));
|
|
4930
|
+
},
|
|
4931
|
+
edit(app, members) {
|
|
4932
|
+
return gathered(`presentation\u0000${app}`, structuredClone(members), (earlier, later) => ({ ...earlier, ...later }), (edited) => keptPresentation(app, (held) => Object.fromEntries([
|
|
4933
|
+
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(edited, member)),
|
|
4934
|
+
...Object.entries(edited).filter(([, value]) => value !== null),
|
|
4935
|
+
])));
|
|
2556
4936
|
},
|
|
2557
4937
|
},
|
|
2558
4938
|
save(room, named, writtenBy) {
|
|
2559
4939
|
return exclusive(() => saveRoomDrafts(room, named, writtenBy));
|
|
2560
4940
|
},
|
|
4941
|
+
saveParts(room, parts, writtenBy) {
|
|
4942
|
+
return exclusive(() => saveParts(room, parts, writtenBy));
|
|
4943
|
+
},
|
|
2561
4944
|
history(locator, path, query) {
|
|
2562
4945
|
return readHistory(locator, path, query);
|
|
2563
4946
|
},
|
|
4947
|
+
resolve(room, path, destination) {
|
|
4948
|
+
return resolveReference(room, path, destination);
|
|
4949
|
+
},
|
|
2564
4950
|
binaryFile: {
|
|
4951
|
+
part: readPart,
|
|
2565
4952
|
read: readBinaryPath,
|
|
2566
|
-
|
|
4953
|
+
link: linkBinaryPath,
|
|
2567
4954
|
},
|
|
2568
|
-
call(name, input) {
|
|
2569
|
-
|
|
4955
|
+
async call(name, input) {
|
|
4956
|
+
await ready;
|
|
4957
|
+
const room = calledRooms(name, input).find((one) => locked(one));
|
|
4958
|
+
return room === undefined ? recoveredAction(name, input) : lockedRoom(room);
|
|
2570
4959
|
},
|
|
2571
|
-
reached(locator, roomVersion, path) {
|
|
4960
|
+
async reached(locator, roomVersion, path) {
|
|
2572
4961
|
return path === undefined
|
|
2573
4962
|
? reachedBy(locator, roomVersion)
|
|
2574
4963
|
: writtenAfter(locator, roomVersion, path);
|
|
@@ -2576,8 +4965,16 @@ export function composeRuntime(options) {
|
|
|
2576
4965
|
log() {
|
|
2577
4966
|
return journal.export();
|
|
2578
4967
|
},
|
|
4968
|
+
cleanUp,
|
|
2579
4969
|
persistence() {
|
|
2580
|
-
return
|
|
4970
|
+
return persistence === 'granted';
|
|
4971
|
+
},
|
|
4972
|
+
async retry() {
|
|
4973
|
+
journal.record('retry');
|
|
4974
|
+
unpreparable.clear();
|
|
4975
|
+
queueReadiness();
|
|
4976
|
+
if (full)
|
|
4977
|
+
await probed();
|
|
2581
4978
|
},
|
|
2582
4979
|
observe: observing(watch),
|
|
2583
4980
|
async reauthorize() {
|
|
@@ -2599,11 +4996,34 @@ export function composeRuntime(options) {
|
|
|
2599
4996
|
const reachable = {
|
|
2600
4997
|
md,
|
|
2601
4998
|
watch,
|
|
2602
|
-
referrerScope: (referrer) => options.adapter.referrerScope(referrer),
|
|
2603
4999
|
plan: planned,
|
|
2604
5000
|
query: (plan) => rowsQuery(plan),
|
|
2605
5001
|
watchPlan,
|
|
2606
5002
|
search: searched,
|
|
5003
|
+
watchReadiness,
|
|
5004
|
+
locked,
|
|
5005
|
+
lockedRooms,
|
|
5006
|
+
watchLocks,
|
|
5007
|
+
located,
|
|
5008
|
+
};
|
|
5009
|
+
return {
|
|
5010
|
+
md,
|
|
5011
|
+
seed,
|
|
5012
|
+
stop,
|
|
5013
|
+
lose: loseRooms,
|
|
5014
|
+
watch,
|
|
5015
|
+
persisted(answer) {
|
|
5016
|
+
if (answer === persistence)
|
|
5017
|
+
return;
|
|
5018
|
+
persistence = answer;
|
|
5019
|
+
journal.record(`persistence ${answer}`);
|
|
5020
|
+
queueReadiness();
|
|
5021
|
+
},
|
|
5022
|
+
shown: screen,
|
|
5023
|
+
within: (reach, port) => {
|
|
5024
|
+
if (full)
|
|
5025
|
+
void probed();
|
|
5026
|
+
return narrowed(reachable, reach, port);
|
|
5027
|
+
},
|
|
2607
5028
|
};
|
|
2608
|
-
return { md, stop, lose: loseRooms, watch, within: (reach) => narrowed(reachable, reach) };
|
|
2609
5029
|
}
|