@agentshouse/mdruntime 0.1.0-c5f333dfa0583 → 0.1.0-c6477d7203b3e
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/README.md +10 -8
- package/dist/adapter.d.ts +30 -34
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +2 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +28 -4
- 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 +16 -8
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +139 -94
- package/dist/change-set.d.ts +1 -7
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +1 -4
- package/dist/client.d.ts +6 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +54 -7
- package/dist/collection.d.ts +5 -3
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +24 -19
- package/dist/compile.d.ts +6 -1
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +94 -33
- package/dist/connection.d.ts +8 -7
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1090 -1114
- package/dist/conversation.d.ts +7 -5
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +171 -55
- package/dist/durability.d.ts +1 -5
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +1 -0
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +1 -1
- package/dist/file.d.ts +2 -11
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +6 -4
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +370 -0
- package/dist/host.d.ts +3 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +42 -18
- package/dist/identity.d.ts +0 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -3
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +2 -1
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +2 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +46 -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 +6 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +7 -10
- package/dist/observation.d.ts +1 -0
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +1 -1
- package/dist/outcome.d.ts +13 -6
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +16 -5
- package/dist/overlay.js +1 -1
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -0
- package/dist/query.d.ts +10 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +12 -1
- package/dist/reach.d.ts +8 -3
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +69 -46
- package/dist/readiness.d.ts +14 -15
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +2 -8
- package/dist/recovery.d.ts +1 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +26 -0
- package/dist/refresh.d.ts +6 -2
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +61 -27
- package/dist/replica.d.ts +5 -5
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +4 -7
- package/dist/room-draft.d.ts +18 -2
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +73 -6
- package/dist/rows.d.ts +15 -6
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +102 -74
- package/dist/save.d.ts +2 -0
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +13 -7
- package/dist/search.d.ts +11 -43
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +0 -296
- package/dist/shared.d.ts +0 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +0 -10
- package/dist/status.d.ts +4 -3
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -1
- package/dist/store.d.ts +23 -58
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +37 -272
- package/dist/stored.d.ts +10 -23
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +217 -218
- package/dist/tables.d.ts +51 -46
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +94 -53
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/validation.d.ts +3 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +35 -96
- package/dist/wire.d.ts +5 -5
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +2 -2
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +156 -0
- package/package.json +11 -19
- package/AGENTS.md +0 -312
- 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/earlier.d.ts +0 -10
- package/dist/chat/earlier.d.ts.map +0 -1
- package/dist/chat/earlier.js +0 -54
- package/dist/chat/house.d.ts +0 -25
- package/dist/chat/house.d.ts.map +0 -1
- package/dist/chat/house.js +0 -38
- package/dist/chat/input.d.ts +0 -11
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -92
- package/dist/chat/kit.d.ts +0 -61
- 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 -28
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -103
- package/dist/chat/markdown.d.ts +0 -21
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -246
- package/dist/chat/message.d.ts +0 -7
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -254
- package/dist/chat/messages.d.ts +0 -35
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -210
- package/dist/chat/name.d.ts +0 -7
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -12
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -19
- package/dist/chat/overlay.d.ts +0 -32
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -103
- package/dist/chat/parts.d.ts +0 -58
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -120
- package/dist/chat/pieces.d.ts +0 -62
- package/dist/chat/pieces.d.ts.map +0 -1
- package/dist/chat/pieces.js +0 -1110
- 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 -70
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -635
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -69
- 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 -37
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -93
- package/dist/chat/room-file.d.ts +0 -8
- package/dist/chat/room-file.d.ts.map +0 -1
- package/dist/chat/room-file.js +0 -72
- package/dist/chat/steady.d.ts +0 -3
- package/dist/chat/steady.d.ts.map +0 -1
- package/dist/chat/steady.js +0 -26
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -307
- package/dist/chat.d.ts +0 -15
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/reads.d.ts +0 -11
- package/dist/reads.d.ts.map +0 -1
- package/dist/reads.js +0 -98
package/dist/connection.js
CHANGED
|
@@ -1,51 +1,41 @@
|
|
|
1
1
|
import { escapePointer, parseFrontmatter, roomTarget, target } from '@agentshouse/mdmodel';
|
|
2
|
-
import { attachedModel,
|
|
2
|
+
import { attachedModel, builtInModel, roomTypesOf } from './attached-model.js';
|
|
3
3
|
import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
|
|
4
4
|
import { observing, whole } from './observation.js';
|
|
5
5
|
import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
|
|
6
6
|
import { createLiveQueries } from './live.js';
|
|
7
7
|
import { seededResult } from './seed.js';
|
|
8
8
|
import { documentKey, roomOfKey } from './rows.js';
|
|
9
|
-
import {
|
|
9
|
+
import { searchPlanOf } from './search.js';
|
|
10
10
|
import { capped, MAXIMUM_WINDOW } from './windows.js';
|
|
11
11
|
import { bootstrapRoom } from './bootstrap.js';
|
|
12
12
|
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
13
13
|
import { createConversationStore, } from './conversation.js';
|
|
14
14
|
import { leaving as leavingIn, partnerOf, queuedFile, } from './change.js';
|
|
15
|
-
import { available, collectionOf,
|
|
15
|
+
import { available, collectionOf, composedOf, validRoot, } from './collection.js';
|
|
16
16
|
import { runtimeError } from './failure.js';
|
|
17
17
|
import { createLog } from './log.js';
|
|
18
18
|
import { binaryRecord } from './binary.js';
|
|
19
|
+
import { attachedFile } from './file.js';
|
|
19
20
|
import { openedFiles } from './opened.js';
|
|
20
|
-
import { ABSENT_TARGET, absentBinary,
|
|
21
|
+
import { ABSENT_TARGET, absentBinary, attachAnswer, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, OFFLINE, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
|
|
21
22
|
import { createRecovery } from './recovery.js';
|
|
22
|
-
import { doorReads } from './reads.js';
|
|
23
23
|
import { advancement } from './refresh.js';
|
|
24
24
|
import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
|
|
25
|
-
import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths,
|
|
26
|
-
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
25
|
+
import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
|
|
26
|
+
import { binaryBase, changedParts, changesOf, gatheredParts, held, heldFile, joinedDraft, moveTarget, readParts, readSavedParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
27
27
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
28
|
-
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
29
|
-
import { readinessOf,
|
|
28
|
+
import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
|
|
29
|
+
import { readinessOf, } from './readiness.js';
|
|
30
30
|
import { FencedMap } from './fence.js';
|
|
31
|
-
import { fingerprint } from './stored.js';
|
|
32
31
|
import { sameBinary } from './overlay.js';
|
|
33
32
|
import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
|
|
34
33
|
import { identityTime, roomDraftRevision } from './identity.js';
|
|
35
|
-
import { beneath, normalize } from './paths.js';
|
|
34
|
+
import { ancestors, beneath, normalize } from './paths.js';
|
|
36
35
|
import { openRuntimeStore, } from './store.js';
|
|
37
|
-
import {
|
|
36
|
+
import { validatedNeighbourhood } from './validation.js';
|
|
38
37
|
import { unconfirmed } from './transfer.js';
|
|
39
38
|
import { governs, holds, narrowed, planReach, } from './reach.js';
|
|
40
|
-
function roomsAlone(plan) {
|
|
41
|
-
return (plan.reach === null &&
|
|
42
|
-
plan.root.types === null &&
|
|
43
|
-
plan.root.where === null &&
|
|
44
|
-
plan.root.pathPrefix === null &&
|
|
45
|
-
plan.paths === null &&
|
|
46
|
-
plan.hop === null &&
|
|
47
|
-
plan.backlinks === null);
|
|
48
|
-
}
|
|
49
39
|
function within(root, path) {
|
|
50
40
|
return root === '' || beneath(root, path);
|
|
51
41
|
}
|
|
@@ -63,39 +53,46 @@ const OPENING_LANE = 'opening';
|
|
|
63
53
|
const CONVERSATION_LANE = 'conversations';
|
|
64
54
|
const ROOM_READS = 256;
|
|
65
55
|
const ROOM_ATTEMPTS = 16;
|
|
66
|
-
const SEARCH_BATCH = 256;
|
|
67
56
|
const ERASE_BATCH = 512;
|
|
68
|
-
const
|
|
69
|
-
const
|
|
57
|
+
const DERIVE_BYTES = 1 << 23;
|
|
58
|
+
const DERIVE_FLOOR = 1 << 18;
|
|
59
|
+
// An index build aims at this much database work, so a Save, a query or the next part's progress waits behind at most one short build.
|
|
60
|
+
const DERIVE_AIM_MS = 250;
|
|
61
|
+
// A build past its floor of text stops after a second and a half of work, its full-text writes included, so a Room whose text costs ten times the usual holds a Save, a query or the next part's progress behind no longer build.
|
|
62
|
+
const DERIVE_LONGEST_MS = 1_500;
|
|
70
63
|
const QUERY_BYTES = 1 << 20;
|
|
71
64
|
const STAGE_BYTES = 1 << 20;
|
|
72
|
-
const
|
|
65
|
+
const STAGE_FLOOR = 1 << 16;
|
|
66
|
+
// A batch aims at this much database work, so a Save or a query waits behind at most one short write.
|
|
67
|
+
const STAGE_AIM_MS = 50;
|
|
68
|
+
const STREAMS_HELD = 4;
|
|
69
|
+
const NOTHING_HELD = { bytes: 0 };
|
|
70
|
+
const QUEUED_TEXT = 1 << 23;
|
|
71
|
+
// Bytes still arriving are reported at most this often, so readiness is not rebuilt for every chunk the network hands over while a slow link still moves the figure.
|
|
72
|
+
const ARRIVAL_REPORT_MS = 250;
|
|
73
73
|
// 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.
|
|
74
74
|
const LOCK_GRACE_MS = 120_000;
|
|
75
75
|
function operationOf(input) {
|
|
76
76
|
const operation = input?.operation_id;
|
|
77
77
|
return typeof operation === 'string' ? operation : null;
|
|
78
78
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
function readUntil(read, signal) {
|
|
80
|
+
if (signal.aborted)
|
|
81
|
+
return Promise.resolve({ kind: 'transport-uncertainty' });
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
const stop = () => resolve({ kind: 'transport-uncertainty' });
|
|
84
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
85
|
+
read.then(resolve, reject).finally(() => signal.removeEventListener('abort', stop));
|
|
85
86
|
});
|
|
86
87
|
}
|
|
87
|
-
function roomLane(locator) {
|
|
88
|
-
return `room ${locator}`;
|
|
89
|
-
}
|
|
90
88
|
function pendingKey(locator, id) {
|
|
91
89
|
return `${locator}\u0000${id}`;
|
|
92
90
|
}
|
|
93
91
|
export function composeRuntime(options) {
|
|
94
92
|
const localSaves = options.adapter.localSaves === true;
|
|
95
|
-
const roomTypes = roomTypesOf(options.adapter.roomTypes);
|
|
93
|
+
const roomTypes = roomTypesOf(options.adapter.roomTypes, options.adapter.nameableRoomTypes);
|
|
96
94
|
let authorization = options.authorization;
|
|
97
95
|
let opening;
|
|
98
|
-
let store;
|
|
99
96
|
const fences = new Map();
|
|
100
97
|
const isFenced = (locator) => fences.has(locator);
|
|
101
98
|
let fencings = 0;
|
|
@@ -103,44 +100,46 @@ export function composeRuntime(options) {
|
|
|
103
100
|
const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
|
|
104
101
|
let discoveries = 0;
|
|
105
102
|
const replicas = new FencedMap(isFenced);
|
|
103
|
+
const reopened = new Set();
|
|
104
|
+
const usableWhenHeld = options.usableWhenHeld ?? [];
|
|
106
105
|
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
107
106
|
const pendings = new Map();
|
|
108
107
|
const seeds = new Map();
|
|
109
108
|
const seedWatchers = new Map();
|
|
110
109
|
const roomDrafts = new FencedMap(isFenced);
|
|
110
|
+
const attachedFiles = new FencedMap(isFenced);
|
|
111
111
|
const presentations = new Map();
|
|
112
112
|
let limits = [];
|
|
113
113
|
let capabilities = [];
|
|
114
114
|
const rooms = new FencedMap(isFenced);
|
|
115
115
|
const counted = new Map();
|
|
116
116
|
const unreadable = new Set();
|
|
117
|
-
const
|
|
118
|
-
const models = new FencedMap(
|
|
119
|
-
const liveModels = new FencedMap(
|
|
117
|
+
const builtIn = builtInModel(roomTypes);
|
|
118
|
+
const models = new FencedMap(isFenced);
|
|
119
|
+
const liveModels = new FencedMap(isFenced);
|
|
120
120
|
const invalidModels = new Set();
|
|
121
121
|
const discovered = new Map();
|
|
122
|
-
|
|
122
|
+
let inFlight = new AbortController();
|
|
123
|
+
const reads = {
|
|
124
|
+
bootstrap: (authorized, request) => readUntil(options.adapter.bootstrap(authorized, request), inFlight.signal),
|
|
125
|
+
refresh: (authorized, request) => readUntil(options.adapter.refresh(authorized, request), inFlight.signal),
|
|
126
|
+
reached: (locator, roomVersion, target) => options.adapter.reached(locator, roomVersion, target),
|
|
127
|
+
};
|
|
123
128
|
let roomsListed = false;
|
|
124
129
|
const scopedRefusals = new Map();
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
function modelKey(locator, attached) {
|
|
128
|
-
return `${attached} ${locator}`;
|
|
129
|
-
}
|
|
130
|
-
function modelOf(locator, attached = 'collection') {
|
|
131
|
-
return models.get(modelKey(locator, attached)) ?? NO_MODEL;
|
|
130
|
+
function modelOf(locator) {
|
|
131
|
+
return models.get(locator) ?? NO_MODEL;
|
|
132
132
|
}
|
|
133
|
-
function savedModel(locator
|
|
134
|
-
return invalidModels.has(
|
|
133
|
+
function savedModel(locator) {
|
|
134
|
+
return invalidModels.has(locator)
|
|
135
135
|
? { status: 'unavailable', reason: 'revoked' }
|
|
136
|
-
: modelOf(locator
|
|
136
|
+
: modelOf(locator);
|
|
137
|
+
}
|
|
138
|
+
function composed(record) {
|
|
139
|
+
return compositionOf(record, builtIn);
|
|
137
140
|
}
|
|
138
141
|
function compositionFor(record) {
|
|
139
|
-
return
|
|
140
|
-
...record,
|
|
141
|
-
model: savedModel(record.locator, 'collection'),
|
|
142
|
-
selectionModel: savedModel(record.locator, 'typed-selection'),
|
|
143
|
-
});
|
|
142
|
+
return composed({ ...record, model: savedModel(record.locator) });
|
|
144
143
|
}
|
|
145
144
|
let refusal = null;
|
|
146
145
|
let storageLoss = null;
|
|
@@ -165,14 +164,18 @@ export function composeRuntime(options) {
|
|
|
165
164
|
const stale = new Set();
|
|
166
165
|
let generation = 0;
|
|
167
166
|
let discovering = null;
|
|
168
|
-
let opened =
|
|
167
|
+
let opened = false;
|
|
169
168
|
const owed = new Set();
|
|
170
|
-
const reloadsDue = new
|
|
169
|
+
const reloadsDue = new Set();
|
|
171
170
|
const lanes = new Map();
|
|
172
|
-
|
|
171
|
+
const roomLanes = new Map();
|
|
172
|
+
let roomWork = 0;
|
|
173
|
+
const roomReads = { free: ROOM_READS, waiting: [] };
|
|
173
174
|
const working = new Set();
|
|
174
175
|
let mutations = Promise.resolve();
|
|
176
|
+
let gathering = null;
|
|
175
177
|
const roomListeners = new Set();
|
|
178
|
+
const unlistedRooms = new Set();
|
|
176
179
|
const refusalListeners = new Set();
|
|
177
180
|
const storageLossListeners = new Set();
|
|
178
181
|
const statusListeners = new Set();
|
|
@@ -192,19 +195,53 @@ export function composeRuntime(options) {
|
|
|
192
195
|
const serviceModeListeners = new Set();
|
|
193
196
|
let member = null;
|
|
194
197
|
const memberListeners = new Set();
|
|
195
|
-
const prepared = new Set();
|
|
196
198
|
const unpreparedDemand = new Set();
|
|
197
|
-
const
|
|
199
|
+
const unpreparable = new Set();
|
|
198
200
|
let preparing = false;
|
|
199
201
|
let targets = {};
|
|
200
202
|
let persistence = options.shell.persistence;
|
|
201
|
-
let
|
|
202
|
-
|
|
203
|
-
let
|
|
203
|
+
let full = false;
|
|
204
|
+
let probing = null;
|
|
205
|
+
let testing = null;
|
|
206
|
+
const database = {
|
|
207
|
+
async ask(request) {
|
|
208
|
+
try {
|
|
209
|
+
return await options.database.ask(request);
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (refusedForSpace(error))
|
|
213
|
+
filled();
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
};
|
|
204
218
|
const readinessWatches = new Set();
|
|
205
219
|
let readinessQueued = false;
|
|
206
220
|
const catching = new Set();
|
|
207
221
|
const transfers = new FencedMap(isFenced);
|
|
222
|
+
const installs = new Set();
|
|
223
|
+
const arrivals = new Map();
|
|
224
|
+
const streaming = new Set();
|
|
225
|
+
const underived = new Map();
|
|
226
|
+
const unindexed = new Map();
|
|
227
|
+
const startedOver = new Map();
|
|
228
|
+
let arrivalReported = 0;
|
|
229
|
+
let streamsFree = STREAMS_HELD;
|
|
230
|
+
let queuedText = 0;
|
|
231
|
+
const installWaiters = [];
|
|
232
|
+
const stageBudget = {
|
|
233
|
+
bytes: STAGE_FLOOR * 4,
|
|
234
|
+
floor: STAGE_FLOOR,
|
|
235
|
+
ceiling: STAGE_BYTES,
|
|
236
|
+
aim: STAGE_AIM_MS,
|
|
237
|
+
};
|
|
238
|
+
const deriveBudget = {
|
|
239
|
+
bytes: DERIVE_FLOOR * 4,
|
|
240
|
+
floor: DERIVE_FLOOR,
|
|
241
|
+
ceiling: DERIVE_BYTES,
|
|
242
|
+
aim: DERIVE_AIM_MS,
|
|
243
|
+
};
|
|
244
|
+
const installTurns = { busy: false, ahead: 0, finishing: [], starting: [] };
|
|
208
245
|
const conversations = createConversationStore({
|
|
209
246
|
agents: declaredAgents,
|
|
210
247
|
documents: (declaration, after) => rowsQuery(planOf({
|
|
@@ -225,40 +262,27 @@ export function composeRuntime(options) {
|
|
|
225
262
|
stream: () => options.adapter.conversations,
|
|
226
263
|
publish: publishChangeSet,
|
|
227
264
|
online: () => connectionOf() !== 'unreachable' && refusal === null,
|
|
265
|
+
listed: () => roomsListed,
|
|
266
|
+
full: () => full,
|
|
228
267
|
closed: () => closed,
|
|
229
268
|
unanswered: resendLater,
|
|
230
269
|
expired: (operation) => pastHorizon(operation),
|
|
231
270
|
});
|
|
232
|
-
const searchIndex = createSearchIndex({
|
|
233
|
-
async *room(room) {
|
|
234
|
-
const local = await openLocal();
|
|
235
|
-
for (let after = null;;) {
|
|
236
|
-
const key = replicas.get(room)?.record.key;
|
|
237
|
-
if (key === undefined)
|
|
238
|
-
return;
|
|
239
|
-
const page = await local.searched(key, room, after, SEARCH_BATCH, null);
|
|
240
|
-
yield page.documents;
|
|
241
|
-
if (page.next === null)
|
|
242
|
-
return;
|
|
243
|
-
after = page.next;
|
|
244
|
-
}
|
|
245
|
-
},
|
|
246
|
-
async documents(room, paths) {
|
|
247
|
-
const key = replicas.get(room)?.record.key;
|
|
248
|
-
if (key === undefined || paths.length === 0)
|
|
249
|
-
return [];
|
|
250
|
-
return (await (await openLocal()).searched(key, room, null, paths.length, paths)).documents;
|
|
251
|
-
},
|
|
252
|
-
});
|
|
253
|
-
const roomsToIndex = new Set();
|
|
254
|
-
const conversationsToIndex = new Set();
|
|
255
|
-
let indexing = false;
|
|
256
271
|
const journal = createLog();
|
|
257
|
-
const keptFiles = openedFiles(
|
|
258
|
-
let
|
|
272
|
+
const keptFiles = openedFiles(database, isFenced);
|
|
273
|
+
let readyFromStore = () => { };
|
|
259
274
|
const ready = new Promise((resolve) => {
|
|
260
|
-
|
|
275
|
+
readyFromStore = resolve;
|
|
261
276
|
});
|
|
277
|
+
let notifyDiscovered = () => { };
|
|
278
|
+
const firstDiscovery = new Promise((resolve) => {
|
|
279
|
+
notifyDiscovered = resolve;
|
|
280
|
+
});
|
|
281
|
+
let roomsNamed = false;
|
|
282
|
+
function notifyReady() {
|
|
283
|
+
notifyDiscovered();
|
|
284
|
+
readyFromStore();
|
|
285
|
+
}
|
|
262
286
|
function rememberRoomDraft(record) {
|
|
263
287
|
const room = roomDrafts.get(record.locator) ?? new Map();
|
|
264
288
|
room.set(record.path, record);
|
|
@@ -278,7 +302,7 @@ export function composeRuntime(options) {
|
|
|
278
302
|
return undefined;
|
|
279
303
|
const local = await openLocal();
|
|
280
304
|
const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
|
|
281
|
-
return { ...slice, collection: collectionOf(slice.documents,
|
|
305
|
+
return { ...slice, collection: collectionOf(slice.documents, composed(held.record)) };
|
|
282
306
|
}
|
|
283
307
|
function roomDraftOf(record, visible) {
|
|
284
308
|
return structuredClone({
|
|
@@ -334,14 +358,40 @@ export function composeRuntime(options) {
|
|
|
334
358
|
const value = presentations.get(app);
|
|
335
359
|
return value === undefined ? null : structuredClone(value);
|
|
336
360
|
}
|
|
337
|
-
function
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
361
|
+
async function writtenRoomDraft(room, path, revision, input, next) {
|
|
362
|
+
const parts = readParts(input);
|
|
363
|
+
if (path === '' || normalize(path) !== path)
|
|
364
|
+
throw runtimeError('room-draft-malformed');
|
|
365
|
+
const local = await openLocal();
|
|
366
|
+
const current = roomDrafts.get(room)?.get(path);
|
|
367
|
+
if ((current?.revision ?? null) !== revision) {
|
|
368
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
369
|
+
}
|
|
370
|
+
const file = parts.file === undefined
|
|
371
|
+
? undefined
|
|
372
|
+
: await heldFile(parts.file.blob, parts.file.base === undefined
|
|
373
|
+
? binaryBase(await visibleOf(room, [path]), path)
|
|
374
|
+
: parts.file.base);
|
|
375
|
+
const record = {
|
|
376
|
+
locator: room,
|
|
377
|
+
path,
|
|
378
|
+
revision: next,
|
|
379
|
+
parts: held(current?.parts, parts, file),
|
|
380
|
+
returned: current?.returned ?? null,
|
|
381
|
+
};
|
|
382
|
+
await local.keepRoomDraft(record, parts.file === undefined || file === undefined
|
|
383
|
+
? []
|
|
384
|
+
: [{ key: file.localId, blob: parts.file.blob }]);
|
|
385
|
+
rememberRoomDraft(record);
|
|
386
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
387
|
+
return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
|
|
388
|
+
}
|
|
389
|
+
async function keptPresentation(app, next) {
|
|
390
|
+
const local = await openLocal();
|
|
391
|
+
const stored = next(presentations.get(app));
|
|
392
|
+
await local.present({ app, value: stored });
|
|
393
|
+
presentations.set(app, stored);
|
|
394
|
+
publishChangeSet(changeSet('', { presentation: app }));
|
|
345
395
|
}
|
|
346
396
|
function emitPresentation(app) {
|
|
347
397
|
if (closed)
|
|
@@ -356,6 +406,20 @@ export function composeRuntime(options) {
|
|
|
356
406
|
(queuesWithoutReplica.size > 0 ||
|
|
357
407
|
[...replicas.values()].some((replica) => replica.queue.length > 0)));
|
|
358
408
|
}
|
|
409
|
+
function unsent() {
|
|
410
|
+
const changed = new Map();
|
|
411
|
+
for (const room of [...replicas.keys(), ...queuesWithoutReplica.keys()]) {
|
|
412
|
+
for (const { batch, state } of entriesOf(room)) {
|
|
413
|
+
if (state === 'local')
|
|
414
|
+
continue;
|
|
415
|
+
for (const { path } of batch.changes)
|
|
416
|
+
changed.set(`${room}\u0000${path}`, { room, path });
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return [...changed]
|
|
420
|
+
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
421
|
+
.map(([, change]) => change);
|
|
422
|
+
}
|
|
359
423
|
function attention() {
|
|
360
424
|
const attentionRequired = [];
|
|
361
425
|
for (const record of [...roomDrafts.values()].flatMap((room) => [...room.values()])) {
|
|
@@ -367,16 +431,9 @@ export function composeRuntime(options) {
|
|
|
367
431
|
returned: structuredClone(record.returned),
|
|
368
432
|
});
|
|
369
433
|
}
|
|
370
|
-
for (const [room, refusal] of sourceRefusals)
|
|
371
|
-
attentionRequired.push({ room, refusal: structuredClone(refusal) });
|
|
372
434
|
attentionRequired.sort((left, right) => {
|
|
373
435
|
if (left.room !== right.room)
|
|
374
436
|
return left.room < right.room ? -1 : 1;
|
|
375
|
-
if ('refusal' in left || 'refusal' in right) {
|
|
376
|
-
if ('refusal' in left && 'refusal' in right)
|
|
377
|
-
return 0;
|
|
378
|
-
return 'refusal' in left ? -1 : 1;
|
|
379
|
-
}
|
|
380
437
|
if (left.path !== right.path)
|
|
381
438
|
return left.path < right.path ? -1 : 1;
|
|
382
439
|
return 0;
|
|
@@ -388,7 +445,7 @@ export function composeRuntime(options) {
|
|
|
388
445
|
if (reset === null || submission.reset < reset)
|
|
389
446
|
reset = submission.reset;
|
|
390
447
|
}
|
|
391
|
-
return { attentionRequired, reset };
|
|
448
|
+
return { attentionRequired, unsent: unsent(), reset };
|
|
392
449
|
}
|
|
393
450
|
function documentsIn(locator) {
|
|
394
451
|
return replicas.get(locator)?.count ?? 0;
|
|
@@ -461,7 +518,7 @@ export function composeRuntime(options) {
|
|
|
461
518
|
const governed = governs(reach, room);
|
|
462
519
|
const attached = [
|
|
463
520
|
...(governed.collection ? [modelOf(room.locator)] : []),
|
|
464
|
-
...(governed.selection ? [
|
|
521
|
+
...(governed.selection ? [builtIn] : []),
|
|
465
522
|
];
|
|
466
523
|
return {
|
|
467
524
|
locator: room.locator,
|
|
@@ -477,11 +534,26 @@ export function composeRuntime(options) {
|
|
|
477
534
|
const value = listed();
|
|
478
535
|
for (const listener of roomListeners)
|
|
479
536
|
listener(value);
|
|
537
|
+
emitKnownRooms();
|
|
538
|
+
}
|
|
539
|
+
function emitKnownRooms() {
|
|
540
|
+
conversations.learned();
|
|
541
|
+
if (unlistedRooms.size === 0)
|
|
542
|
+
return;
|
|
543
|
+
if (!roomsListed && rooms.size === 0 && refusal === null && !status.providerUnreachable)
|
|
544
|
+
return;
|
|
545
|
+
const value = listed();
|
|
546
|
+
for (const listener of [...unlistedRooms]) {
|
|
547
|
+
unlistedRooms.delete(listener);
|
|
548
|
+
roomListeners.add(listener);
|
|
549
|
+
listener(value);
|
|
550
|
+
}
|
|
480
551
|
}
|
|
481
552
|
function emitRefusal() {
|
|
482
553
|
queueReadiness();
|
|
483
554
|
for (const listener of refusalListeners)
|
|
484
555
|
listener(refusal);
|
|
556
|
+
emitKnownRooms();
|
|
485
557
|
}
|
|
486
558
|
function emitStorageLoss() {
|
|
487
559
|
queueReadiness();
|
|
@@ -502,6 +574,7 @@ export function composeRuntime(options) {
|
|
|
502
574
|
if (closed)
|
|
503
575
|
return;
|
|
504
576
|
queueReadiness();
|
|
577
|
+
emitKnownRooms();
|
|
505
578
|
const next = status.state === 'synced' && !conversations.current()
|
|
506
579
|
? { ...status, state: 'syncing' }
|
|
507
580
|
: status;
|
|
@@ -515,9 +588,6 @@ export function composeRuntime(options) {
|
|
|
515
588
|
if (localSaves)
|
|
516
589
|
return Object.freeze({ batch, state: 'local' });
|
|
517
590
|
const state = pendings.get(pendingKey(locator, batch.id));
|
|
518
|
-
if (state?.accepted !== undefined) {
|
|
519
|
-
return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
|
|
520
|
-
}
|
|
521
591
|
return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
|
|
522
592
|
}
|
|
523
593
|
function entriesOf(locator) {
|
|
@@ -532,23 +602,29 @@ export function composeRuntime(options) {
|
|
|
532
602
|
}
|
|
533
603
|
function applyChangeSet(set) {
|
|
534
604
|
liveQueries.touched(set);
|
|
535
|
-
searchIndex.changed(set);
|
|
536
605
|
}
|
|
537
606
|
function unpublished(error) {
|
|
538
607
|
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
539
608
|
}
|
|
609
|
+
function indexed(locator) {
|
|
610
|
+
return replicas.get(locator)?.indexed === true;
|
|
611
|
+
}
|
|
540
612
|
async function prepareReplica(locator) {
|
|
541
613
|
for (;;) {
|
|
542
614
|
const held = replicas.get(locator);
|
|
543
|
-
if (held === undefined || closed ||
|
|
615
|
+
if (held === undefined || closed || held.indexed)
|
|
544
616
|
return;
|
|
545
617
|
const key = held.record.key;
|
|
546
|
-
const done = await exclusive(async () =>
|
|
547
|
-
|
|
618
|
+
const done = await exclusive(async () => {
|
|
619
|
+
const local = await openLocal();
|
|
620
|
+
return timed(deriveBudget, () => local.rederive(key, composed(held.record), deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR));
|
|
621
|
+
});
|
|
622
|
+
const now = replicas.get(locator);
|
|
623
|
+
if (now?.record.key !== key)
|
|
548
624
|
continue;
|
|
549
|
-
if (!done)
|
|
625
|
+
if (!done.done)
|
|
550
626
|
continue;
|
|
551
|
-
|
|
627
|
+
replicas.set(locator, { ...now, indexed: true });
|
|
552
628
|
publishChangeSet(changeSet(locator, { documents: null }));
|
|
553
629
|
liveQueries.recover();
|
|
554
630
|
queueReadiness();
|
|
@@ -556,11 +632,11 @@ export function composeRuntime(options) {
|
|
|
556
632
|
}
|
|
557
633
|
}
|
|
558
634
|
function unprepared() {
|
|
559
|
-
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !
|
|
635
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !indexed(locator) && !unpreparable.has(locator));
|
|
560
636
|
}
|
|
561
|
-
function
|
|
562
|
-
|
|
563
|
-
journal.record(`
|
|
637
|
+
function unpreparedBy(locator, error) {
|
|
638
|
+
unpreparable.add(locator);
|
|
639
|
+
journal.record(`preparation failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
564
640
|
queueReadiness();
|
|
565
641
|
}
|
|
566
642
|
function prepare(locators = []) {
|
|
@@ -576,7 +652,7 @@ export function composeRuntime(options) {
|
|
|
576
652
|
if (closed)
|
|
577
653
|
return;
|
|
578
654
|
unpreparedDemand.delete(waiting);
|
|
579
|
-
await prepareReplica(waiting).catch((error) =>
|
|
655
|
+
await prepareReplica(waiting).catch((error) => unpreparedBy(waiting, error));
|
|
580
656
|
}
|
|
581
657
|
}
|
|
582
658
|
finally {
|
|
@@ -585,7 +661,11 @@ export function composeRuntime(options) {
|
|
|
585
661
|
})();
|
|
586
662
|
}
|
|
587
663
|
function roomsOfReach(reach) {
|
|
588
|
-
return new Set([
|
|
664
|
+
return new Set([
|
|
665
|
+
...reach.rooms,
|
|
666
|
+
...reach.documents.map(roomOfKey),
|
|
667
|
+
...reach.areas.flatMap((area) => (area.types.length === 0 ? [] : [area.room])),
|
|
668
|
+
]);
|
|
589
669
|
}
|
|
590
670
|
function reachedRooms(plan) {
|
|
591
671
|
const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
|
|
@@ -617,7 +697,7 @@ export function composeRuntime(options) {
|
|
|
617
697
|
pending.push({ room: locator, awaits: 'acquisition' });
|
|
618
698
|
continue;
|
|
619
699
|
}
|
|
620
|
-
if (!
|
|
700
|
+
if (!replica.indexed) {
|
|
621
701
|
pending.push({ room: locator, awaits: 'preparation' });
|
|
622
702
|
continue;
|
|
623
703
|
}
|
|
@@ -650,9 +730,7 @@ export function composeRuntime(options) {
|
|
|
650
730
|
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
651
731
|
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
652
732
|
}
|
|
653
|
-
function roomState(locator
|
|
654
|
-
if (sourceRefusals.has(locator))
|
|
655
|
-
return 'refused';
|
|
733
|
+
function roomState(locator) {
|
|
656
734
|
const listedRoom = discovered.get(locator);
|
|
657
735
|
if (roomsListed &&
|
|
658
736
|
(listedRoom === undefined ||
|
|
@@ -661,27 +739,17 @@ export function composeRuntime(options) {
|
|
|
661
739
|
return 'unadmitted';
|
|
662
740
|
if (!replicas.has(locator))
|
|
663
741
|
return 'acquiring';
|
|
664
|
-
if (!
|
|
742
|
+
if (!indexed(locator))
|
|
665
743
|
return 'preparing';
|
|
666
|
-
if (search && !searchIndex.rooms().has(locator))
|
|
667
|
-
return 'indexing';
|
|
668
744
|
return 'complete';
|
|
669
745
|
}
|
|
670
|
-
function conversationCoverage(reach
|
|
746
|
+
function conversationCoverage(reach) {
|
|
671
747
|
const declared = reach === null || reach.conversations;
|
|
672
748
|
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
673
749
|
return 'undeclared';
|
|
674
750
|
if (!conversations.known() || !conversations.current())
|
|
675
751
|
return 'acquiring';
|
|
676
|
-
|
|
677
|
-
return 'complete';
|
|
678
|
-
const unindexed = conversations
|
|
679
|
-
.searchable()
|
|
680
|
-
.filter((one) => conversations.acquired(one) && !searchIndex.conversations().has(one));
|
|
681
|
-
if (unindexed.length === 0)
|
|
682
|
-
return 'complete';
|
|
683
|
-
index([], unindexed);
|
|
684
|
-
return 'indexing';
|
|
752
|
+
return 'complete';
|
|
685
753
|
}
|
|
686
754
|
function connectionOf() {
|
|
687
755
|
if (socketOpen)
|
|
@@ -692,58 +760,79 @@ export function composeRuntime(options) {
|
|
|
692
760
|
? 'connected'
|
|
693
761
|
: 'connecting';
|
|
694
762
|
}
|
|
695
|
-
function
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
}
|
|
763
|
+
function bytesOf(room, underway) {
|
|
764
|
+
const transfer = transfers.get(room);
|
|
765
|
+
if (transfer !== undefined) {
|
|
766
|
+
const owed = (unindexed.get(room) ?? []).reduce((sum, part) => sum + part.bytes, 0);
|
|
767
|
+
return {
|
|
768
|
+
done: transfer.done.bytes - Math.ceil(owed),
|
|
769
|
+
total: Math.max(arrivals.get(room) ?? 0, transfer.done.bytes),
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
const held = underway ? NOTHING_HELD : (replicas.get(room)?.record.inventory ?? NOTHING_HELD);
|
|
773
|
+
return { done: held.bytes, total: arrivals.get(room) ?? held.bytes };
|
|
774
|
+
}
|
|
775
|
+
function installationOf(coverage) {
|
|
776
|
+
const rooms = { done: 0, total: 0 };
|
|
777
|
+
const bytes = { done: 0, total: 0 };
|
|
778
|
+
let installing = 0;
|
|
779
|
+
let downloading = false;
|
|
780
|
+
for (const { room, state } of coverage) {
|
|
781
|
+
if (state === 'unadmitted')
|
|
782
|
+
continue;
|
|
783
|
+
rooms.total += 1;
|
|
784
|
+
if (state === 'complete')
|
|
785
|
+
rooms.done += 1;
|
|
786
|
+
const underway = state === 'acquiring' || (installs.has(room) && state !== 'complete');
|
|
787
|
+
if (underway)
|
|
788
|
+
installing += 1;
|
|
789
|
+
if (underway && (streaming.has(room) || !arrivals.has(room)))
|
|
790
|
+
downloading = true;
|
|
791
|
+
const now = bytesOf(room, underway);
|
|
792
|
+
const earlier = underway ? startedOver.get(room) : undefined;
|
|
793
|
+
bytes.done += Math.max(now.done, earlier?.done ?? 0);
|
|
794
|
+
bytes.total += Math.max(now.total, earlier?.total ?? 0);
|
|
795
|
+
}
|
|
796
|
+
const step = installing === 0
|
|
797
|
+
? null
|
|
798
|
+
: downloading
|
|
799
|
+
? 'downloading'
|
|
800
|
+
: bytes.done < bytes.total
|
|
801
|
+
? 'writing'
|
|
802
|
+
: 'replaying';
|
|
803
|
+
return { step, rooms, bytes };
|
|
710
804
|
}
|
|
711
805
|
function readinessFor(port, reach) {
|
|
712
806
|
const required = requiredRooms(reach);
|
|
713
807
|
const search = port.search === 'required';
|
|
714
|
-
const coverage = required.rooms.map((room) => ({ room, state: roomState(room
|
|
715
|
-
const
|
|
716
|
-
.filter((room) => room.state ===
|
|
808
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room) }));
|
|
809
|
+
const preparing = coverage
|
|
810
|
+
.filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
|
|
717
811
|
.map(({ room }) => room);
|
|
718
|
-
const preparing = building('preparing');
|
|
719
812
|
if (preparing.length > 0)
|
|
720
813
|
prepare(preparing);
|
|
721
|
-
const indexing = building('indexing');
|
|
722
|
-
if (indexing.length > 0)
|
|
723
|
-
index(indexing, []);
|
|
724
814
|
return readinessOf({
|
|
725
815
|
code: port.code,
|
|
726
816
|
coverage: {
|
|
727
817
|
listed: required.listed,
|
|
728
818
|
rooms: coverage,
|
|
729
|
-
conversations: conversationCoverage(reach
|
|
819
|
+
conversations: conversationCoverage(reach),
|
|
730
820
|
search: search ? 'required' : 'unused',
|
|
731
821
|
},
|
|
732
822
|
persistence,
|
|
733
823
|
connection: connectionOf(),
|
|
734
824
|
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
? capacity
|
|
738
|
-
: port.capacity,
|
|
739
|
-
refused: refusedIn(required.rooms),
|
|
825
|
+
space: full ? 'full' : port.space,
|
|
826
|
+
installation: installationOf(coverage),
|
|
740
827
|
lost: storageLoss !== null,
|
|
741
|
-
established: targets[port.target]?.established === true
|
|
828
|
+
established: targets[port.target]?.established === true ||
|
|
829
|
+
(usableWhenHeld.includes(port.target) && required.rooms.some((room) => reopened.has(room))),
|
|
742
830
|
});
|
|
743
831
|
}
|
|
744
832
|
function establish(name) {
|
|
745
833
|
targets = { ...targets, [name]: { established: true } };
|
|
746
834
|
queueReadiness();
|
|
835
|
+
probeEachMinute();
|
|
747
836
|
void durably((local) => local.fact('targets', targets)).catch(() => {
|
|
748
837
|
journal.record(`target record failed ${name}`);
|
|
749
838
|
});
|
|
@@ -781,87 +870,52 @@ export function composeRuntime(options) {
|
|
|
781
870
|
readinessWatches.delete(watch);
|
|
782
871
|
};
|
|
783
872
|
}
|
|
784
|
-
function
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
}
|
|
790
|
-
function exceeded(error) {
|
|
791
|
-
return (typeof error === 'object' &&
|
|
792
|
-
error !== null &&
|
|
793
|
-
error.name === 'QuotaExceededError');
|
|
794
|
-
}
|
|
795
|
-
async function estimated() {
|
|
796
|
-
const estimate = options.shell.estimate === undefined ? null : await options.shell.estimate();
|
|
797
|
-
return estimate === null ? null : Math.max(0, estimate.total - estimate.usage);
|
|
798
|
-
}
|
|
799
|
-
function pause(locator, cause) {
|
|
800
|
-
paused.add(locator);
|
|
801
|
-
capacity = cause;
|
|
802
|
-
journal.record(`capacity ${cause.cause} ${locator} ${cause.needed} ${cause.available ?? '-'}`);
|
|
873
|
+
function filled() {
|
|
874
|
+
if (full || closed)
|
|
875
|
+
return;
|
|
876
|
+
full = true;
|
|
877
|
+
journal.record('space full');
|
|
803
878
|
queueReadiness();
|
|
804
|
-
|
|
879
|
+
probeEachMinute();
|
|
880
|
+
}
|
|
881
|
+
function installedTarget() {
|
|
882
|
+
return ((usableWhenHeld.length > 0 && reopened.size > 0) ||
|
|
883
|
+
Object.values(targets).some((target) => target.established));
|
|
884
|
+
}
|
|
885
|
+
function probeEachMinute() {
|
|
886
|
+
if (!full || probing !== null || closed || !installedTarget())
|
|
805
887
|
return;
|
|
806
|
-
// A
|
|
807
|
-
|
|
808
|
-
void
|
|
888
|
+
// 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.
|
|
889
|
+
probing = setInterval(() => {
|
|
890
|
+
void probed();
|
|
809
891
|
}, 60_000);
|
|
810
892
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
resume();
|
|
893
|
+
function probed() {
|
|
894
|
+
testing ??= database.ask({ kind: 'space.probe' }).then(() => {
|
|
895
|
+
testing = null;
|
|
896
|
+
spaceFreed();
|
|
897
|
+
return true;
|
|
898
|
+
}, () => {
|
|
899
|
+
testing = null;
|
|
900
|
+
return false;
|
|
901
|
+
});
|
|
902
|
+
return testing;
|
|
822
903
|
}
|
|
823
|
-
function
|
|
824
|
-
if (
|
|
825
|
-
clearInterval(rechecking);
|
|
826
|
-
rechecking = null;
|
|
827
|
-
if (paused.size === 0 && capacity === null)
|
|
904
|
+
function spaceFreed() {
|
|
905
|
+
if (!full)
|
|
828
906
|
return;
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
907
|
+
full = false;
|
|
908
|
+
if (probing !== null)
|
|
909
|
+
clearInterval(probing);
|
|
910
|
+
probing = null;
|
|
911
|
+
unpreparable.clear();
|
|
912
|
+
journal.record('space freed');
|
|
832
913
|
queueReadiness();
|
|
833
914
|
if (!closed && authorization !== undefined)
|
|
834
915
|
void sync('unstreamed');
|
|
835
916
|
}
|
|
836
|
-
async function reclaimed(local) {
|
|
837
|
-
const unreferenced = [...transfers.keys()].filter((locator) => !discovered.has(locator));
|
|
838
|
-
for (const locator of unreferenced) {
|
|
839
|
-
await local.abandon(locator);
|
|
840
|
-
transfers.delete(locator);
|
|
841
|
-
}
|
|
842
|
-
if (unreferenced.length > 0)
|
|
843
|
-
journal.record(`capacity reclaimed ${unreferenced.length}`);
|
|
844
|
-
return unreferenced.length > 0;
|
|
845
|
-
}
|
|
846
|
-
async function admitted(local, locator, needed) {
|
|
847
|
-
if (paused.has(locator))
|
|
848
|
-
return false;
|
|
849
|
-
let available = await estimated().catch(() => null);
|
|
850
|
-
if (available === null || available >= needed)
|
|
851
|
-
return true;
|
|
852
|
-
if (await reclaimed(local))
|
|
853
|
-
available = await estimated().catch(() => null);
|
|
854
|
-
if (available === null || available >= needed)
|
|
855
|
-
return true;
|
|
856
|
-
pause(locator, { cause: 'insufficient-space', needed, available });
|
|
857
|
-
return false;
|
|
858
|
-
}
|
|
859
|
-
async function exceededOn(locator, bytes) {
|
|
860
|
-
const available = await estimated().catch(() => null);
|
|
861
|
-
pause(locator, { cause: 'storage-exceeded', needed: bytes, available });
|
|
862
|
-
}
|
|
863
917
|
function seedRoomHeld(room, version) {
|
|
864
|
-
return
|
|
918
|
+
return indexed(room) && reachedBy(room, version);
|
|
865
919
|
}
|
|
866
920
|
function seedHeld(at) {
|
|
867
921
|
return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
|
|
@@ -982,83 +1036,12 @@ export function composeRuntime(options) {
|
|
|
982
1036
|
const held = answered.cover;
|
|
983
1037
|
return { ...answered.rows, coverage: held.coverage };
|
|
984
1038
|
}
|
|
985
|
-
async function everyRow(plan) {
|
|
986
|
-
const window = { ...plan, limit: MAXIMUM_WINDOW, after: null, total: false };
|
|
987
|
-
const first = await rowsQuery(window);
|
|
988
|
-
if ('pending' in first || first.next === undefined)
|
|
989
|
-
return first;
|
|
990
|
-
const documents = 'documents' in first ? [...first.documents] : [];
|
|
991
|
-
const groups = 'groups' in first ? [...first.groups] : [];
|
|
992
|
-
for (let next = first.next; next !== undefined;) {
|
|
993
|
-
const values = JSON.parse(next);
|
|
994
|
-
const page = await rowsQuery({
|
|
995
|
-
...window,
|
|
996
|
-
after: { text: next, values: values.length === 0 ? null : values },
|
|
997
|
-
});
|
|
998
|
-
if ('pending' in page)
|
|
999
|
-
return page;
|
|
1000
|
-
if ('documents' in page)
|
|
1001
|
-
documents.push(...page.documents);
|
|
1002
|
-
else
|
|
1003
|
-
groups.push(...page.groups);
|
|
1004
|
-
if (JSON.stringify(page.coverage) !== JSON.stringify(first.coverage))
|
|
1005
|
-
return everyRow(plan);
|
|
1006
|
-
next = page.next;
|
|
1007
|
-
}
|
|
1008
|
-
return 'documents' in first
|
|
1009
|
-
? { documents, coverage: first.coverage }
|
|
1010
|
-
: { groups, coverage: first.coverage };
|
|
1011
|
-
}
|
|
1012
|
-
async function indexConversation(conversation) {
|
|
1013
|
-
searchIndex.indexMessages(conversation, [], false);
|
|
1014
|
-
let after = null;
|
|
1015
|
-
for (;;) {
|
|
1016
|
-
const batch = await durably((local) => local.messagesAfter(conversation, after));
|
|
1017
|
-
if (closed)
|
|
1018
|
-
return;
|
|
1019
|
-
searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
|
|
1020
|
-
if (batch.length === 0)
|
|
1021
|
-
return;
|
|
1022
|
-
after = batch[batch.length - 1].identity;
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
function indexNext() {
|
|
1026
|
-
const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator) && !unindexable.has(locator));
|
|
1027
|
-
if (room !== undefined) {
|
|
1028
|
-
roomsToIndex.delete(room);
|
|
1029
|
-
return () => prepared.has(room)
|
|
1030
|
-
? searchIndex.indexRoom(room).catch((error) => unbuilt(room, error))
|
|
1031
|
-
: undefined;
|
|
1032
|
-
}
|
|
1033
|
-
const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
|
|
1034
|
-
if (conversation === undefined)
|
|
1035
|
-
return null;
|
|
1036
|
-
conversationsToIndex.delete(conversation);
|
|
1037
|
-
return () => indexConversation(conversation);
|
|
1038
|
-
}
|
|
1039
|
-
function index(rooms, named) {
|
|
1040
|
-
for (const room of rooms)
|
|
1041
|
-
roomsToIndex.add(room);
|
|
1042
|
-
for (const conversation of named)
|
|
1043
|
-
conversationsToIndex.add(conversation);
|
|
1044
|
-
if (indexing)
|
|
1045
|
-
return;
|
|
1046
|
-
indexing = true;
|
|
1047
|
-
void (async () => {
|
|
1048
|
-
try {
|
|
1049
|
-
for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
|
|
1050
|
-
await yielded();
|
|
1051
|
-
await unit();
|
|
1052
|
-
queueReadiness();
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
finally {
|
|
1056
|
-
indexing = false;
|
|
1057
|
-
}
|
|
1058
|
-
})().catch(() => { });
|
|
1059
|
-
}
|
|
1060
1039
|
function publishChangeSet(set) {
|
|
1061
1040
|
applyChangeSet(set);
|
|
1041
|
+
for (const { locator, path } of set.documents) {
|
|
1042
|
+
if (path !== null)
|
|
1043
|
+
attachedFiles.get(locator)?.delete(path);
|
|
1044
|
+
}
|
|
1062
1045
|
if (set.rooms.length > 0 ||
|
|
1063
1046
|
[...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
|
|
1064
1047
|
emitRooms();
|
|
@@ -1077,6 +1060,8 @@ export function composeRuntime(options) {
|
|
|
1077
1060
|
}
|
|
1078
1061
|
async function refuse(reason, provider = null) {
|
|
1079
1062
|
discoveryPaused = true;
|
|
1063
|
+
inFlight.abort();
|
|
1064
|
+
inFlight = new AbortController();
|
|
1080
1065
|
try {
|
|
1081
1066
|
if (reason === 'revoked-authorization') {
|
|
1082
1067
|
await exclusive(async () => {
|
|
@@ -1084,27 +1069,24 @@ export function composeRuntime(options) {
|
|
|
1084
1069
|
const records = [...replicas].map(([locator, held]) => ({
|
|
1085
1070
|
locator,
|
|
1086
1071
|
held,
|
|
1087
|
-
record: { ...held.record, model: revoked
|
|
1072
|
+
record: { ...held.record, model: revoked },
|
|
1088
1073
|
}));
|
|
1074
|
+
let answer = null;
|
|
1089
1075
|
try {
|
|
1090
1076
|
const local = await openLocal();
|
|
1091
|
-
await local.commit({
|
|
1077
|
+
answer = await local.commit({
|
|
1092
1078
|
rooms: records.map(({ locator, record }) => ({
|
|
1093
1079
|
locator,
|
|
1094
1080
|
key: record.key,
|
|
1095
1081
|
record: storedRecord(record),
|
|
1096
|
-
|
|
1082
|
+
composed: composedOf(composed(record)),
|
|
1097
1083
|
})),
|
|
1098
1084
|
});
|
|
1099
1085
|
}
|
|
1100
1086
|
catch {
|
|
1101
1087
|
journal.record('model revocation persistence failed');
|
|
1102
1088
|
}
|
|
1103
|
-
const keys = new Set(models.keys());
|
|
1104
|
-
for (const locator of rooms.keys()) {
|
|
1105
|
-
keys.add(modelKey(locator, 'collection'));
|
|
1106
|
-
keys.add(modelKey(locator, 'typed-selection'));
|
|
1107
|
-
}
|
|
1089
|
+
const keys = new Set([...models.keys(), ...rooms.keys()]);
|
|
1108
1090
|
for (const key of keys) {
|
|
1109
1091
|
invalidModels.add(key);
|
|
1110
1092
|
models.set(key, revoked);
|
|
@@ -1114,8 +1096,8 @@ export function composeRuntime(options) {
|
|
|
1114
1096
|
for (const { locator, held, record } of records) {
|
|
1115
1097
|
if (replicas.get(locator) !== held)
|
|
1116
1098
|
continue;
|
|
1117
|
-
|
|
1118
|
-
replicas.set(locator, { ...held, record });
|
|
1099
|
+
const indexed = answer === null ? held.indexed : committedRoom(locator, answer).indexed;
|
|
1100
|
+
replicas.set(locator, { ...held, record, indexed });
|
|
1119
1101
|
}
|
|
1120
1102
|
for (const locator of [...rooms.keys()]) {
|
|
1121
1103
|
const room = rooms.get(locator);
|
|
@@ -1159,12 +1141,12 @@ export function composeRuntime(options) {
|
|
|
1159
1141
|
? null
|
|
1160
1142
|
: {
|
|
1161
1143
|
roots: selection.roots,
|
|
1162
|
-
model: modelTypes(
|
|
1144
|
+
model: modelTypes(builtIn),
|
|
1163
1145
|
},
|
|
1164
1146
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
1165
1147
|
roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
|
|
1166
1148
|
synchronizationAvailable,
|
|
1167
|
-
refusal: scopedRefusals.get(room.locator) ??
|
|
1149
|
+
refusal: scopedRefusals.get(room.locator) ?? null,
|
|
1168
1150
|
});
|
|
1169
1151
|
}
|
|
1170
1152
|
async function applyDiscovery(found, started, covered) {
|
|
@@ -1248,30 +1230,24 @@ export function composeRuntime(options) {
|
|
|
1248
1230
|
queuesWithoutReplica.delete(locator);
|
|
1249
1231
|
rooms.delete(locator);
|
|
1250
1232
|
roomDrafts.delete(locator);
|
|
1251
|
-
|
|
1233
|
+
attachedFiles.delete(locator);
|
|
1234
|
+
untransferred(locator);
|
|
1235
|
+
startedOver.delete(locator);
|
|
1252
1236
|
catching.delete(locator);
|
|
1253
1237
|
discovered.delete(locator);
|
|
1254
1238
|
announced.delete(locator);
|
|
1255
1239
|
unreadable.delete(locator);
|
|
1256
1240
|
stale.delete(locator);
|
|
1257
1241
|
repairable.delete(locator);
|
|
1258
|
-
|
|
1259
|
-
paused.delete(locator);
|
|
1260
|
-
prepared.delete(locator);
|
|
1242
|
+
unpreparable.delete(locator);
|
|
1261
1243
|
scopedRefusals.delete(locator);
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
const key = modelKey(locator, attached);
|
|
1266
|
-
models.delete(key);
|
|
1267
|
-
liveModels.delete(key);
|
|
1268
|
-
invalidModels.delete(key);
|
|
1269
|
-
}
|
|
1244
|
+
models.delete(locator);
|
|
1245
|
+
liveModels.delete(locator);
|
|
1246
|
+
invalidModels.delete(locator);
|
|
1270
1247
|
journal.record(`room lost ${locator}`);
|
|
1271
1248
|
}
|
|
1272
1249
|
if (fresh.length === 0)
|
|
1273
1250
|
return fresh;
|
|
1274
|
-
searchIndex.forget(fresh);
|
|
1275
1251
|
publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, { documents: null, room: true, queue: true, drafts }))));
|
|
1276
1252
|
liveQueries.recover();
|
|
1277
1253
|
moveTo({ ...status, ...attention() });
|
|
@@ -1315,7 +1291,14 @@ export function composeRuntime(options) {
|
|
|
1315
1291
|
});
|
|
1316
1292
|
await eraseFenced(fresh);
|
|
1317
1293
|
}
|
|
1318
|
-
function
|
|
1294
|
+
function retyped(persisted) {
|
|
1295
|
+
if (persisted.status === 'unavailable')
|
|
1296
|
+
return persisted;
|
|
1297
|
+
const { state } = attachedModel({ kind: 'model', snapshot: persisted.snapshot }, NO_MODEL, roomTypes);
|
|
1298
|
+
return state.status === 'unavailable' ? state : { ...persisted, types: state.types };
|
|
1299
|
+
}
|
|
1300
|
+
function restored(key, held) {
|
|
1301
|
+
const persisted = retyped(held);
|
|
1319
1302
|
const prior = models.get(key);
|
|
1320
1303
|
if (prior !== undefined &&
|
|
1321
1304
|
prior.status !== 'unavailable' &&
|
|
@@ -1346,8 +1329,9 @@ export function composeRuntime(options) {
|
|
|
1346
1329
|
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
1347
1330
|
targets = held.targets;
|
|
1348
1331
|
transfers.clear();
|
|
1349
|
-
|
|
1350
|
-
|
|
1332
|
+
arrivals.clear();
|
|
1333
|
+
streaming.clear();
|
|
1334
|
+
startedOver.clear();
|
|
1351
1335
|
limits = held.providerLimits;
|
|
1352
1336
|
capabilities = held.capabilities;
|
|
1353
1337
|
member = held.member;
|
|
@@ -1357,7 +1341,7 @@ export function composeRuntime(options) {
|
|
|
1357
1341
|
presentations.clear();
|
|
1358
1342
|
for (const record of held.presentation)
|
|
1359
1343
|
presentations.set(record.app, record.value);
|
|
1360
|
-
conversations.hydrate(heldConversations, chatDrafts);
|
|
1344
|
+
conversations.hydrate(heldConversations, chatDrafts, held.conversationList);
|
|
1361
1345
|
pendings.clear();
|
|
1362
1346
|
for (const queue of queues) {
|
|
1363
1347
|
for (const { batch, state } of queue.pending) {
|
|
@@ -1365,7 +1349,7 @@ export function composeRuntime(options) {
|
|
|
1365
1349
|
}
|
|
1366
1350
|
}
|
|
1367
1351
|
replicas.clear();
|
|
1368
|
-
|
|
1352
|
+
reopened.clear();
|
|
1369
1353
|
queuesWithoutReplica.clear();
|
|
1370
1354
|
for (const [locator, queue] of queuedBatches) {
|
|
1371
1355
|
if (!held.rooms.has(locator))
|
|
@@ -1374,39 +1358,40 @@ export function composeRuntime(options) {
|
|
|
1374
1358
|
records.forEach((stored, index) => {
|
|
1375
1359
|
if (fences.has(stored.locator))
|
|
1376
1360
|
return;
|
|
1377
|
-
const
|
|
1378
|
-
|
|
1379
|
-
const record = { ...stored, model, selectionModel };
|
|
1361
|
+
const record = { ...stored, model: restored(stored.locator, stored.model) };
|
|
1362
|
+
reopened.add(record.locator);
|
|
1380
1363
|
replicas.set(record.locator, {
|
|
1381
1364
|
record,
|
|
1382
1365
|
queue: queuedBatches.get(record.locator) ?? [],
|
|
1383
1366
|
overlaid: held.overlaid.get(record.key) ?? new Set(),
|
|
1384
1367
|
count: counts[index] ?? 0,
|
|
1385
1368
|
confirmed: 0,
|
|
1369
|
+
indexed: !held.unindexed.has(record.key),
|
|
1386
1370
|
});
|
|
1387
|
-
if (!held.unprepared.has(record.key))
|
|
1388
|
-
prepared.add(record.locator);
|
|
1389
1371
|
const existing = rooms.get(record.locator);
|
|
1390
1372
|
if (existing === undefined)
|
|
1391
|
-
list(record,
|
|
1373
|
+
list(record, record.roomVersion !== null);
|
|
1392
1374
|
else {
|
|
1393
1375
|
rooms.set(record.locator, {
|
|
1394
1376
|
...existing,
|
|
1395
1377
|
roomVersion: record.roomVersion,
|
|
1396
1378
|
operations: record.operations,
|
|
1397
1379
|
replica: record.replica,
|
|
1398
|
-
model: modelTypes(model),
|
|
1380
|
+
model: modelTypes(record.model),
|
|
1399
1381
|
typedSelection: record.typedSelection === null
|
|
1400
1382
|
? null
|
|
1401
|
-
: {
|
|
1402
|
-
roots: record.typedSelection.roots,
|
|
1403
|
-
model: modelTypes(selectionModel),
|
|
1404
|
-
},
|
|
1383
|
+
: { roots: record.typedSelection.roots, model: modelTypes(builtIn) },
|
|
1405
1384
|
});
|
|
1406
1385
|
}
|
|
1407
1386
|
});
|
|
1408
|
-
|
|
1409
|
-
|
|
1387
|
+
try {
|
|
1388
|
+
for (const key of held.orphans)
|
|
1389
|
+
await opened.release(key);
|
|
1390
|
+
}
|
|
1391
|
+
catch (error) {
|
|
1392
|
+
if (!refusedForSpace(error))
|
|
1393
|
+
throw error;
|
|
1394
|
+
}
|
|
1410
1395
|
if (held.erasing || held.orphans.length > 0)
|
|
1411
1396
|
queueMicrotask(sweep);
|
|
1412
1397
|
return {
|
|
@@ -1431,8 +1416,7 @@ export function composeRuntime(options) {
|
|
|
1431
1416
|
async function open() {
|
|
1432
1417
|
if (closed)
|
|
1433
1418
|
throw new Error('runtime closed');
|
|
1434
|
-
const opened =
|
|
1435
|
-
store = opened;
|
|
1419
|
+
const opened = openRuntimeStore(isFenced, database);
|
|
1436
1420
|
const interrupted = await opened.fences();
|
|
1437
1421
|
for (const locator of interrupted) {
|
|
1438
1422
|
fences.set(locator, { at: discoveries, erased: false });
|
|
@@ -1478,10 +1462,29 @@ export function composeRuntime(options) {
|
|
|
1478
1462
|
return opening;
|
|
1479
1463
|
}
|
|
1480
1464
|
function exclusive(work) {
|
|
1465
|
+
gathering = null;
|
|
1481
1466
|
const next = mutations.then(work, work);
|
|
1482
1467
|
mutations = next.then(() => undefined, () => undefined);
|
|
1483
1468
|
return next;
|
|
1484
1469
|
}
|
|
1470
|
+
function gathered(key, input, merge, work) {
|
|
1471
|
+
const tail = gathering;
|
|
1472
|
+
if (tail !== null && tail.key === key) {
|
|
1473
|
+
const merged = merge(tail.input, input);
|
|
1474
|
+
if (merged !== null) {
|
|
1475
|
+
tail.input = merged;
|
|
1476
|
+
return tail.done;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
const entry = { key, input, done: Promise.resolve() };
|
|
1480
|
+
entry.done = exclusive(() => {
|
|
1481
|
+
if (gathering === entry)
|
|
1482
|
+
gathering = null;
|
|
1483
|
+
return work(entry.input);
|
|
1484
|
+
});
|
|
1485
|
+
gathering = entry;
|
|
1486
|
+
return entry.done;
|
|
1487
|
+
}
|
|
1485
1488
|
function withdrawable(locator) {
|
|
1486
1489
|
return (batch) => {
|
|
1487
1490
|
const state = pendings.get(pendingKey(locator, batch.id));
|
|
@@ -1563,26 +1566,14 @@ export function composeRuntime(options) {
|
|
|
1563
1566
|
},
|
|
1564
1567
|
};
|
|
1565
1568
|
}
|
|
1566
|
-
function
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
fingerprint(compositionOf(before)) === fingerprint(compositionOf(after))) {
|
|
1570
|
-
return;
|
|
1571
|
-
}
|
|
1572
|
-
if (before !== undefined && before.key === after.key)
|
|
1573
|
-
prepared.delete(locator);
|
|
1574
|
-
}
|
|
1575
|
-
function committedRoom(locator, record, answer) {
|
|
1576
|
-
if (answer.prepared[locator] === false)
|
|
1577
|
-
prepared.delete(locator);
|
|
1578
|
-
else if (answer.prepared[locator] === true)
|
|
1579
|
-
prepared.add(locator);
|
|
1580
|
-
if (!prepared.has(locator))
|
|
1569
|
+
function committedRoom(locator, answer) {
|
|
1570
|
+
const indexed = answer.indexed[locator] ?? replicas.get(locator)?.indexed ?? false;
|
|
1571
|
+
if (!indexed)
|
|
1581
1572
|
prepare([locator]);
|
|
1582
|
-
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0 };
|
|
1573
|
+
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0, indexed };
|
|
1583
1574
|
}
|
|
1584
1575
|
function replayed(record, confirmedLayer, queue) {
|
|
1585
|
-
return stepped(replaying(confirmedLayer, queue, record.locator,
|
|
1576
|
+
return stepped(replaying(confirmedLayer, queue, record.locator, composed(record)));
|
|
1586
1577
|
}
|
|
1587
1578
|
async function overlayDiff(record, before, visible, touched) {
|
|
1588
1579
|
const kept = before !== undefined && before.record.key === record.key
|
|
@@ -1602,8 +1593,6 @@ export function composeRuntime(options) {
|
|
|
1602
1593
|
function unpairedIn(locator, batch) {
|
|
1603
1594
|
if (!queueIn(locator).some((held) => held.id === batch.id))
|
|
1604
1595
|
return false;
|
|
1605
|
-
if (pendings.get(pendingKey(locator, batch.id))?.accepted !== undefined)
|
|
1606
|
-
return false;
|
|
1607
1596
|
return !leavingIn(batch, locator) || withdrawable(locator)(batch);
|
|
1608
1597
|
}
|
|
1609
1598
|
async function together(local, settlements) {
|
|
@@ -1645,7 +1634,7 @@ export function composeRuntime(options) {
|
|
|
1645
1634
|
const confirmedLayer = overrides === undefined
|
|
1646
1635
|
? read
|
|
1647
1636
|
: await stepped(slicingWith(read, overrides.set, overrides.removed));
|
|
1648
|
-
const composition =
|
|
1637
|
+
const composition = composed(record);
|
|
1649
1638
|
const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
|
|
1650
1639
|
const founded = kept.filter((batch) => !queue.includes(batch));
|
|
1651
1640
|
const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
|
|
@@ -1688,6 +1677,7 @@ export function composeRuntime(options) {
|
|
|
1688
1677
|
key: record.key,
|
|
1689
1678
|
record: storedRecord(record),
|
|
1690
1679
|
roots: record.typedSelection?.roots ?? [],
|
|
1680
|
+
composed: composedOf(composition),
|
|
1691
1681
|
...settling.commit,
|
|
1692
1682
|
...(overrides === undefined
|
|
1693
1683
|
? {}
|
|
@@ -1729,14 +1719,14 @@ export function composeRuntime(options) {
|
|
|
1729
1719
|
const confirmedChanged = settling.commit?.published !== undefined ||
|
|
1730
1720
|
settling.commit?.refreshed === true ||
|
|
1731
1721
|
overrides !== undefined;
|
|
1732
|
-
|
|
1733
|
-
const { count } = committedRoom(locator, record, answer);
|
|
1722
|
+
const { count, indexed } = committedRoom(locator, answer);
|
|
1734
1723
|
replicas.set(locator, {
|
|
1735
1724
|
record,
|
|
1736
1725
|
queue: kept,
|
|
1737
1726
|
overlaid: touched,
|
|
1738
1727
|
count,
|
|
1739
1728
|
confirmed: (before?.confirmed ?? 0) + (confirmedChanged ? 1 : 0),
|
|
1729
|
+
indexed,
|
|
1740
1730
|
});
|
|
1741
1731
|
return mergeChangeSets([
|
|
1742
1732
|
changeSet(locator, {
|
|
@@ -1751,103 +1741,95 @@ export function composeRuntime(options) {
|
|
|
1751
1741
|
};
|
|
1752
1742
|
}
|
|
1753
1743
|
function adoptModels(locator, record) {
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
if (model.status === 'current')
|
|
1762
|
-
liveModels.set(key, JSON.stringify(model.snapshot));
|
|
1763
|
-
else if (model.status === 'unavailable')
|
|
1764
|
-
liveModels.delete(key);
|
|
1765
|
-
}
|
|
1744
|
+
const model = record.model;
|
|
1745
|
+
models.set(locator, model);
|
|
1746
|
+
invalidModels.delete(locator);
|
|
1747
|
+
if (model.status === 'current')
|
|
1748
|
+
liveModels.set(locator, JSON.stringify(model.snapshot));
|
|
1749
|
+
else if (model.status === 'unavailable')
|
|
1750
|
+
liveModels.delete(locator);
|
|
1766
1751
|
}
|
|
1767
1752
|
function remodel(locator, answers, held) {
|
|
1768
|
-
const
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
};
|
|
1779
|
-
return {
|
|
1780
|
-
collection: taken(answers?.collection, held.collection, 'collection'),
|
|
1781
|
-
selection: taken(answers?.selection, held.selection, 'typed-selection'),
|
|
1782
|
-
};
|
|
1753
|
+
const answer = answers?.collection;
|
|
1754
|
+
if (answer === undefined)
|
|
1755
|
+
return held;
|
|
1756
|
+
const { state, refused } = attachedModel(answer, held.collection, roomTypes);
|
|
1757
|
+
if (refused !== null) {
|
|
1758
|
+
const where = refused.location === undefined ? '' : ` ${refused.location}`;
|
|
1759
|
+
journal.record(`model invalid ${locator} collection ${refused.identity} ${refused.rule}${where}`);
|
|
1760
|
+
}
|
|
1761
|
+
models.set(locator, state);
|
|
1762
|
+
return { collection: state };
|
|
1783
1763
|
}
|
|
1784
|
-
function publish(local, room,
|
|
1785
|
-
|
|
1764
|
+
function publish(local, room, reached, landing, live) {
|
|
1765
|
+
const landed = exclusive(async () => {
|
|
1786
1766
|
if (!live()) {
|
|
1787
1767
|
caughtUpTo(room.locator);
|
|
1788
1768
|
return true;
|
|
1789
1769
|
}
|
|
1790
1770
|
const prior = replicas.get(room.locator);
|
|
1791
|
-
if (landing.
|
|
1792
|
-
(
|
|
1793
|
-
|
|
1771
|
+
if (!stands(room.locator, landing.from)) {
|
|
1772
|
+
if (landing.kind === 'refreshed') {
|
|
1773
|
+
await local.unrefresh(reached.key);
|
|
1774
|
+
return false;
|
|
1775
|
+
}
|
|
1776
|
+
caughtUpTo(room.locator);
|
|
1777
|
+
await abandoned(local, room.locator);
|
|
1794
1778
|
return false;
|
|
1795
1779
|
}
|
|
1780
|
+
const record = landing.kind === 'refreshed' && prior !== undefined
|
|
1781
|
+
? {
|
|
1782
|
+
...prior.record,
|
|
1783
|
+
roomVersion: reached.roomVersion,
|
|
1784
|
+
...(landing.remodelled ? { model: reached.model } : {}),
|
|
1785
|
+
}
|
|
1786
|
+
: reached;
|
|
1796
1787
|
adoptModels(room.locator, record);
|
|
1797
1788
|
const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
|
|
1798
|
-
const roomVersion = record.roomVersion;
|
|
1799
|
-
const accepted = queue.filter((batch) => {
|
|
1800
|
-
const at = pendings.get(pendingKey(room.locator, batch.id))?.accepted;
|
|
1801
|
-
return at !== undefined && options.adapter.reached(room.locator, roomVersion, at);
|
|
1802
|
-
});
|
|
1803
1789
|
let set;
|
|
1804
1790
|
try {
|
|
1805
|
-
set = await settle(local, room.locator, prior, record, queue
|
|
1791
|
+
set = await settle(local, room.locator, prior, record, queue, landing.kind === 'refreshed'
|
|
1806
1792
|
? {
|
|
1807
1793
|
room: true,
|
|
1808
1794
|
view: 'refreshed',
|
|
1809
|
-
commit: { refreshed: true
|
|
1795
|
+
commit: { refreshed: true },
|
|
1810
1796
|
}
|
|
1811
1797
|
: {
|
|
1812
1798
|
room: true,
|
|
1813
1799
|
view: landing.refreshed ? 'refreshed' : 'confirmed',
|
|
1814
1800
|
commit: {
|
|
1815
1801
|
published: { from: prior?.record.key ?? null },
|
|
1816
|
-
composition: compositionOf(record),
|
|
1817
1802
|
...(landing.refreshed ? { refreshed: true } : {}),
|
|
1818
1803
|
},
|
|
1819
1804
|
});
|
|
1820
1805
|
}
|
|
1821
1806
|
catch (error) {
|
|
1822
|
-
if (!
|
|
1807
|
+
if (!refusedForSpace(error))
|
|
1823
1808
|
throw error;
|
|
1824
|
-
await exceededOn(room.locator, null);
|
|
1825
1809
|
caughtUpTo(room.locator);
|
|
1826
1810
|
return true;
|
|
1827
1811
|
}
|
|
1828
1812
|
if (landing.kind === 'bootstrapped') {
|
|
1829
|
-
|
|
1813
|
+
untransferred(room.locator);
|
|
1814
|
+
startedOver.delete(room.locator);
|
|
1830
1815
|
catching.delete(room.locator);
|
|
1831
|
-
|
|
1816
|
+
if (prior !== undefined && prior.record.key !== record.key)
|
|
1817
|
+
sweep();
|
|
1832
1818
|
}
|
|
1833
|
-
for (const batch of accepted)
|
|
1834
|
-
pendings.delete(pendingKey(room.locator, batch.id));
|
|
1835
1819
|
stale.delete(room.locator);
|
|
1836
|
-
|
|
1820
|
+
unpreparable.delete(room.locator);
|
|
1837
1821
|
if (unreadable.delete(room.locator))
|
|
1838
1822
|
scopedRefusals.delete(room.locator);
|
|
1839
|
-
sourceRefusals.delete(room.locator);
|
|
1840
|
-
declarationRefusals.delete(room.locator);
|
|
1841
1823
|
list(room, true);
|
|
1842
1824
|
publishChangeSet(set);
|
|
1843
1825
|
await unpair(local);
|
|
1844
1826
|
moveTo({ ...status, ...attention() });
|
|
1845
|
-
|
|
1846
|
-
if (accepted.length > 0 && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
|
|
1827
|
+
if (landing.kind === 'bootstrapped' && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
|
|
1847
1828
|
submitLater(room.locator);
|
|
1848
1829
|
}
|
|
1849
1830
|
return true;
|
|
1850
1831
|
});
|
|
1832
|
+
return landing.kind === 'refreshed' ? landed : landed.finally(() => installed(room.locator));
|
|
1851
1833
|
}
|
|
1852
1834
|
async function savedRoom(locator) {
|
|
1853
1835
|
const local = await openLocal();
|
|
@@ -1872,12 +1854,7 @@ export function composeRuntime(options) {
|
|
|
1872
1854
|
return committed(locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
|
|
1873
1855
|
}
|
|
1874
1856
|
async function saveParts(locator, named, writtenBy) {
|
|
1875
|
-
const read = named
|
|
1876
|
-
if (read.some(({ path }, at) => path === '' ||
|
|
1877
|
-
normalize(path) !== path ||
|
|
1878
|
-
read.findIndex((one) => one.path === path) !== at)) {
|
|
1879
|
-
throw runtimeError('room-draft-malformed');
|
|
1880
|
-
}
|
|
1857
|
+
const read = readSavedParts(named);
|
|
1881
1858
|
const target = await savedRoom(locator);
|
|
1882
1859
|
const saved = [];
|
|
1883
1860
|
const files = [];
|
|
@@ -1886,7 +1863,7 @@ export function composeRuntime(options) {
|
|
|
1886
1863
|
for (const { path, parts } of read) {
|
|
1887
1864
|
const file = parts.file === undefined
|
|
1888
1865
|
? undefined
|
|
1889
|
-
: await heldFile(parts.file.blob, binaryBase(visible, path));
|
|
1866
|
+
: await heldFile(parts.file.blob, parts.file.base === undefined ? binaryBase(visible, path) : parts.file.base);
|
|
1890
1867
|
if (parts.file !== undefined && file !== undefined) {
|
|
1891
1868
|
files.push({ key: file.localId, blob: parts.file.blob });
|
|
1892
1869
|
}
|
|
@@ -1921,13 +1898,14 @@ export function composeRuntime(options) {
|
|
|
1921
1898
|
const referring = removed.length === 0
|
|
1922
1899
|
? []
|
|
1923
1900
|
: (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
|
|
1924
|
-
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...referring], moved);
|
|
1901
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
|
|
1925
1902
|
const { visible } = await replayed(record, confirmedLayer, replica.queue);
|
|
1926
|
-
const composition =
|
|
1903
|
+
const composition = composed(record);
|
|
1927
1904
|
return {
|
|
1928
1905
|
record,
|
|
1929
1906
|
confirmed: confirmedLayer,
|
|
1930
1907
|
visible,
|
|
1908
|
+
folders: new Set(await local.folders(record.key, 'visible', paths)),
|
|
1931
1909
|
*visibleOver(queue) {
|
|
1932
1910
|
return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
|
|
1933
1911
|
},
|
|
@@ -1939,13 +1917,7 @@ export function composeRuntime(options) {
|
|
|
1939
1917
|
return false;
|
|
1940
1918
|
const local = await openLocal();
|
|
1941
1919
|
const roots = record.typedSelection?.roots ?? [];
|
|
1942
|
-
|
|
1943
|
-
(await local.typedWithin(record.key, 'visible', roots, false))) {
|
|
1944
|
-
return true;
|
|
1945
|
-
}
|
|
1946
|
-
return (composition.selection !== null &&
|
|
1947
|
-
composition.selection.model.status === 'unavailable' &&
|
|
1948
|
-
(await local.typedWithin(record.key, 'visible', roots, true)));
|
|
1920
|
+
return local.typedWithin(record.key, 'visible', roots, false);
|
|
1949
1921
|
}
|
|
1950
1922
|
function textDiffers(left, right) {
|
|
1951
1923
|
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
@@ -1956,18 +1928,6 @@ export function composeRuntime(options) {
|
|
|
1956
1928
|
return undefined;
|
|
1957
1929
|
return entry.binary !== null ? null : entry.content;
|
|
1958
1930
|
}
|
|
1959
|
-
function walkOf(local, key, view) {
|
|
1960
|
-
return async (after) => {
|
|
1961
|
-
const page = await local.walk(key, view, after, WALK_BYTES);
|
|
1962
|
-
return {
|
|
1963
|
-
entries: page.entries.map(({ path, content, binary }) => ({
|
|
1964
|
-
path,
|
|
1965
|
-
content: binary !== null ? null : content,
|
|
1966
|
-
})),
|
|
1967
|
-
next: page.next,
|
|
1968
|
-
};
|
|
1969
|
-
};
|
|
1970
|
-
}
|
|
1971
1931
|
function lookupOf(local, key, view) {
|
|
1972
1932
|
return async (paths) => {
|
|
1973
1933
|
if (paths.length === 0)
|
|
@@ -1984,37 +1944,6 @@ export function composeRuntime(options) {
|
|
|
1984
1944
|
const read = parseFrontmatter(entry.content ?? '');
|
|
1985
1945
|
return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
|
|
1986
1946
|
}
|
|
1987
|
-
function overriddenWalk(walk, overrides) {
|
|
1988
|
-
const sorted = [...overrides.keys()].sort();
|
|
1989
|
-
const beyond = (path) => {
|
|
1990
|
-
if (path === null)
|
|
1991
|
-
return 0;
|
|
1992
|
-
let low = 0;
|
|
1993
|
-
let high = sorted.length;
|
|
1994
|
-
while (low < high) {
|
|
1995
|
-
const middle = (low + high) >>> 1;
|
|
1996
|
-
if (sorted[middle] <= path)
|
|
1997
|
-
low = middle + 1;
|
|
1998
|
-
else
|
|
1999
|
-
high = middle;
|
|
2000
|
-
}
|
|
2001
|
-
return low;
|
|
2002
|
-
};
|
|
2003
|
-
return async (after) => {
|
|
2004
|
-
const page = await walk(after);
|
|
2005
|
-
const end = page.next;
|
|
2006
|
-
const added = sorted
|
|
2007
|
-
.slice(beyond(after), end === null ? sorted.length : beyond(end))
|
|
2008
|
-
.flatMap((path) => {
|
|
2009
|
-
const entry = overrides.get(path);
|
|
2010
|
-
return entry === null || entry === undefined
|
|
2011
|
-
? []
|
|
2012
|
-
: [{ path, content: entry.binary !== null ? null : entry.content }];
|
|
2013
|
-
});
|
|
2014
|
-
const entries = [...page.entries.filter(({ path }) => !overrides.has(path)), ...added].sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
2015
|
-
return { entries, next: end };
|
|
2016
|
-
};
|
|
2017
|
-
}
|
|
2018
1947
|
function overriddenLookup(lookup, overrides) {
|
|
2019
1948
|
return async (paths) => {
|
|
2020
1949
|
const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
|
|
@@ -2032,9 +1961,8 @@ export function composeRuntime(options) {
|
|
|
2032
1961
|
}
|
|
2033
1962
|
function validatorOf(record, confirmedLayer) {
|
|
2034
1963
|
return async (resulting, touched) => {
|
|
2035
|
-
const composition = compositionFor(record);
|
|
2036
1964
|
const local = await openLocal();
|
|
2037
|
-
const
|
|
1965
|
+
const changes = [];
|
|
2038
1966
|
const overrides = new Map();
|
|
2039
1967
|
const pace = paced();
|
|
2040
1968
|
for (const path of new Set([...confirmedLayer.held, ...touched])) {
|
|
@@ -2045,19 +1973,13 @@ export function composeRuntime(options) {
|
|
|
2045
1973
|
continue;
|
|
2046
1974
|
overrides.set(path, now);
|
|
2047
1975
|
if (textOf(was) !== textOf(now))
|
|
2048
|
-
|
|
1976
|
+
changes.push({ path, was: textOf(was), now: textOf(now) });
|
|
2049
1977
|
}
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
texts: textsOf(local, record.key, 'confirmed'),
|
|
2056
|
-
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2057
|
-
}));
|
|
2058
|
-
if (clean)
|
|
2059
|
-
return { verdict: 'accept' };
|
|
2060
|
-
return validatedCollection(overriddenWalk(walkOf(local, record.key, 'confirmed'), overrides), lookup, composition);
|
|
1978
|
+
return validatedNeighbourhood(changes, compositionFor(record), {
|
|
1979
|
+
lookup: overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides),
|
|
1980
|
+
texts: textsOf(local, record.key, 'confirmed'),
|
|
1981
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
1982
|
+
});
|
|
2061
1983
|
};
|
|
2062
1984
|
}
|
|
2063
1985
|
function textsOf(local, key, view) {
|
|
@@ -2081,6 +2003,7 @@ export function composeRuntime(options) {
|
|
|
2081
2003
|
operations: room.operations,
|
|
2082
2004
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
2083
2005
|
visible: saving.visible,
|
|
2006
|
+
folders: saving.folders,
|
|
2084
2007
|
queued: queuedDocuments(replica.queue),
|
|
2085
2008
|
unmodeled: await unmodeledIn(record),
|
|
2086
2009
|
validate: validatorOf(record, saving.confirmed),
|
|
@@ -2101,6 +2024,7 @@ export function composeRuntime(options) {
|
|
|
2101
2024
|
locator,
|
|
2102
2025
|
key: saving.record.key,
|
|
2103
2026
|
roots: saving.record.typedSelection?.roots ?? [],
|
|
2027
|
+
composed: composedOf(composed(saving.record)),
|
|
2104
2028
|
overlay: diff.overlay,
|
|
2105
2029
|
});
|
|
2106
2030
|
}
|
|
@@ -2132,7 +2056,7 @@ export function composeRuntime(options) {
|
|
|
2132
2056
|
}
|
|
2133
2057
|
const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
2134
2058
|
const sourceSaving = await savingIn(replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
|
|
2135
|
-
const changed = saved.flatMap(({ path, parts }) => changedParts(path,
|
|
2059
|
+
const changed = saved.flatMap(({ path, parts }) => changedParts(path, parts, sourceSaving.visible));
|
|
2136
2060
|
if (changed.length > 0)
|
|
2137
2061
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
2138
2062
|
const target = replicas.get(destination);
|
|
@@ -2244,6 +2168,8 @@ export function composeRuntime(options) {
|
|
|
2244
2168
|
submitLater(locator);
|
|
2245
2169
|
}
|
|
2246
2170
|
async function committed(locator, target, saved, consumed, writtenBy, files = []) {
|
|
2171
|
+
if (full)
|
|
2172
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2247
2173
|
const into = movedInto(locator, saved);
|
|
2248
2174
|
if (into !== null) {
|
|
2249
2175
|
return committedMove(locator, into, target, saved, consumed, writtenBy, files);
|
|
@@ -2255,7 +2181,7 @@ export function composeRuntime(options) {
|
|
|
2255
2181
|
...saved.map(({ path }) => path),
|
|
2256
2182
|
...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
|
|
2257
2183
|
]);
|
|
2258
|
-
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path,
|
|
2184
|
+
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, held, saving.visible));
|
|
2259
2185
|
if (changed.length > 0)
|
|
2260
2186
|
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
2261
2187
|
const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(locator), saving.visibleOver));
|
|
@@ -2283,6 +2209,7 @@ export function composeRuntime(options) {
|
|
|
2283
2209
|
providerLimits: limits,
|
|
2284
2210
|
handle: room.handle,
|
|
2285
2211
|
visible: saving.visible,
|
|
2212
|
+
folders: saving.folders,
|
|
2286
2213
|
changes,
|
|
2287
2214
|
bases: parts.flatMap(({ bases }) => bases),
|
|
2288
2215
|
queued: queuedDocuments(replica.queue),
|
|
@@ -2433,6 +2360,9 @@ export function composeRuntime(options) {
|
|
|
2433
2360
|
async function readHistory(locator, path, query = {}) {
|
|
2434
2361
|
if (closed || refusal !== null)
|
|
2435
2362
|
return UNCERTAIN;
|
|
2363
|
+
await firstDiscovery;
|
|
2364
|
+
if (!roomsNamed)
|
|
2365
|
+
return UNCERTAIN;
|
|
2436
2366
|
try {
|
|
2437
2367
|
await ensureAuthorization();
|
|
2438
2368
|
}
|
|
@@ -2522,14 +2452,12 @@ export function composeRuntime(options) {
|
|
|
2522
2452
|
return (await openLocal()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
2523
2453
|
};
|
|
2524
2454
|
}
|
|
2525
|
-
function binaryRoom(request) {
|
|
2526
|
-
if (request.kind === 'path')
|
|
2527
|
-
return request.locator;
|
|
2528
|
-
return options.adapter.referrerScope(request.referrer)?.locator ?? null;
|
|
2529
|
-
}
|
|
2530
2455
|
async function transferBinary(request, transfer, answer) {
|
|
2531
2456
|
if (closed || refusal !== null)
|
|
2532
2457
|
return UNCERTAIN;
|
|
2458
|
+
await firstDiscovery;
|
|
2459
|
+
if (!roomsNamed)
|
|
2460
|
+
return UNCERTAIN;
|
|
2533
2461
|
try {
|
|
2534
2462
|
await ensureAuthorization();
|
|
2535
2463
|
}
|
|
@@ -2539,16 +2467,13 @@ export function composeRuntime(options) {
|
|
|
2539
2467
|
if (closed || authorization === undefined || refusal !== null)
|
|
2540
2468
|
return UNCERTAIN;
|
|
2541
2469
|
const since = fencings;
|
|
2542
|
-
journal.record(request.
|
|
2543
|
-
? `transfer binary ${request.locator} ${request.path}`
|
|
2544
|
-
: `transfer binary ${request.fileRef}`);
|
|
2470
|
+
journal.record(`transfer binary ${request.locator} ${request.path}`);
|
|
2545
2471
|
try {
|
|
2546
2472
|
const result = await transfer(authorization);
|
|
2547
2473
|
const settled = result;
|
|
2548
2474
|
journal.record(`outcome binary ${settled.kind}`);
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
return failure({ kind: 'room-not-readable', locator });
|
|
2475
|
+
if (fencedSince(request.locator, since))
|
|
2476
|
+
return failure({ kind: 'room-not-readable', locator: request.locator });
|
|
2552
2477
|
if (settled.kind === 'scoped-refusal') {
|
|
2553
2478
|
const room = replicas.get(settled.locator)?.record;
|
|
2554
2479
|
if (room !== undefined)
|
|
@@ -2563,23 +2488,27 @@ export function composeRuntime(options) {
|
|
|
2563
2488
|
return UNCERTAIN;
|
|
2564
2489
|
}
|
|
2565
2490
|
}
|
|
2491
|
+
function fetchBinary(request, name) {
|
|
2492
|
+
return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
|
|
2493
|
+
}
|
|
2566
2494
|
async function readBinary(request, name) {
|
|
2567
2495
|
if (closed)
|
|
2568
2496
|
return UNCERTAIN;
|
|
2569
2497
|
const held = await keptFiles.find(name);
|
|
2570
2498
|
if (held !== null)
|
|
2571
2499
|
return held;
|
|
2572
|
-
return
|
|
2500
|
+
return fetchBinary(request, name);
|
|
2573
2501
|
}
|
|
2574
2502
|
async function binaryPath(locator, path, revision) {
|
|
2575
2503
|
if (revision !== undefined)
|
|
2576
2504
|
return { kind: 'path', locator, path, revision };
|
|
2577
2505
|
const replica = replicas.get(locator);
|
|
2578
|
-
if (replica === undefined)
|
|
2579
|
-
return null;
|
|
2580
2506
|
const local = await openLocal();
|
|
2581
|
-
const [held] = await local.entries(replica.record.key, 'visible', [path]);
|
|
2582
|
-
|
|
2507
|
+
const [held] = replica === undefined ? [] : await local.entries(replica.record.key, 'visible', [path]);
|
|
2508
|
+
const attached = held === undefined ? attachedFiles.get(locator)?.get(path) : undefined;
|
|
2509
|
+
if (attached !== undefined)
|
|
2510
|
+
return { kind: 'path', locator, path, revision: attached.revision };
|
|
2511
|
+
if (replica === undefined || held?.binary === undefined || held.binary === null)
|
|
2583
2512
|
return null;
|
|
2584
2513
|
if (held.binary.state === 'pending')
|
|
2585
2514
|
return { kind: 'pending', localId: held.binary.localId };
|
|
@@ -2595,7 +2524,7 @@ export function composeRuntime(options) {
|
|
|
2595
2524
|
const holds = (at) => {
|
|
2596
2525
|
const kept = confirmedLayer.binaries[at];
|
|
2597
2526
|
return (kept?.state === 'accepted' &&
|
|
2598
|
-
kept.
|
|
2527
|
+
kept.sha256 === record.sha256 &&
|
|
2599
2528
|
confirmedLayer.revisions[at] === record.revision);
|
|
2600
2529
|
};
|
|
2601
2530
|
let confirmedAt = path;
|
|
@@ -2609,49 +2538,91 @@ export function composeRuntime(options) {
|
|
|
2609
2538
|
locator,
|
|
2610
2539
|
path: confirmedAt,
|
|
2611
2540
|
revision: record.revision,
|
|
2612
|
-
fileRef: record.fileRef,
|
|
2613
2541
|
};
|
|
2614
2542
|
}
|
|
2615
|
-
async function
|
|
2543
|
+
async function askedBinary(locator, path, revision) {
|
|
2616
2544
|
await ready;
|
|
2617
2545
|
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2618
|
-
|
|
2546
|
+
return drafted !== undefined && drafted.localId === revision
|
|
2619
2547
|
? { kind: 'pending', localId: revision }
|
|
2620
|
-
:
|
|
2548
|
+
: binaryPath(locator, path, revision);
|
|
2549
|
+
}
|
|
2550
|
+
function pendingBinary(locator, path, localId) {
|
|
2551
|
+
const facts = queuedFacts(locator, path, localId);
|
|
2552
|
+
return facts === null
|
|
2553
|
+
? absentBinary(path)
|
|
2554
|
+
: {
|
|
2555
|
+
kind: 'binary',
|
|
2556
|
+
file: localId,
|
|
2557
|
+
byteLength: facts.byteLength,
|
|
2558
|
+
mediaType: facts.mediaType,
|
|
2559
|
+
};
|
|
2560
|
+
}
|
|
2561
|
+
async function readBinaryPath(locator, path, revision) {
|
|
2562
|
+
const asked = await askedBinary(locator, path, revision);
|
|
2621
2563
|
if (asked === null)
|
|
2622
2564
|
return absentBinary(path);
|
|
2623
|
-
if (asked.kind === 'pending')
|
|
2624
|
-
|
|
2625
|
-
return facts === null
|
|
2626
|
-
? absentBinary(path)
|
|
2627
|
-
: {
|
|
2628
|
-
kind: 'binary',
|
|
2629
|
-
file: asked.localId,
|
|
2630
|
-
byteLength: facts.byteLength,
|
|
2631
|
-
mediaType: facts.mediaType,
|
|
2632
|
-
};
|
|
2633
|
-
}
|
|
2565
|
+
if (asked.kind === 'pending')
|
|
2566
|
+
return pendingBinary(locator, path, asked.localId);
|
|
2634
2567
|
return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
|
|
2635
2568
|
}
|
|
2569
|
+
async function heldMediaType(locator, path) {
|
|
2570
|
+
const replica = replicas.get(locator);
|
|
2571
|
+
const [held] = replica === undefined
|
|
2572
|
+
? []
|
|
2573
|
+
: await (await openLocal()).entries(replica.record.key, 'visible', [path]);
|
|
2574
|
+
return held === undefined
|
|
2575
|
+
? attachedFiles.get(locator)?.get(path)?.mediaType
|
|
2576
|
+
: held.binary?.mediaType;
|
|
2577
|
+
}
|
|
2578
|
+
function linkOf(asked, held) {
|
|
2579
|
+
return transferBinary(asked, (authorization) => options.adapter.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
|
|
2580
|
+
}
|
|
2636
2581
|
async function linkBinaryPath(locator, path, revision) {
|
|
2637
2582
|
await ready;
|
|
2638
2583
|
const asked = await binaryPath(locator, path, revision);
|
|
2639
2584
|
if (asked === null || asked.kind === 'pending')
|
|
2640
2585
|
return absentBinary(path);
|
|
2641
|
-
return
|
|
2586
|
+
return linkOf(asked, revision === undefined ? await heldMediaType(locator, path) : undefined);
|
|
2642
2587
|
}
|
|
2643
|
-
async function
|
|
2588
|
+
async function showBinaryPath(locator, path, revision) {
|
|
2589
|
+
const asked = await askedBinary(locator, path, revision);
|
|
2590
|
+
if (asked === null)
|
|
2591
|
+
return absentBinary(path);
|
|
2592
|
+
if (asked.kind === 'pending')
|
|
2593
|
+
return pendingBinary(locator, path, asked.localId);
|
|
2594
|
+
if (closed)
|
|
2595
|
+
return UNCERTAIN;
|
|
2596
|
+
const name = { locator, path: asked.path, revision: asked.revision };
|
|
2597
|
+
const kept = await keptFiles.find(name);
|
|
2598
|
+
if (kept !== null)
|
|
2599
|
+
return kept;
|
|
2600
|
+
const mediaType = await heldMediaType(locator, path);
|
|
2601
|
+
if (mediaType?.startsWith('image/') === true)
|
|
2602
|
+
return fetchBinary(asked, name);
|
|
2603
|
+
return linkOf(asked, revision === undefined ? mediaType : undefined);
|
|
2604
|
+
}
|
|
2605
|
+
async function attachBinary(locator, file) {
|
|
2644
2606
|
await ready;
|
|
2645
|
-
|
|
2646
|
-
if (
|
|
2647
|
-
return
|
|
2648
|
-
|
|
2607
|
+
await firstDiscovery;
|
|
2608
|
+
if (closed || !roomsNamed || connectionOf() === 'unreachable' || refusal !== null)
|
|
2609
|
+
return OFFLINE;
|
|
2610
|
+
const request = await attachedFile(locator, file);
|
|
2611
|
+
return transferBinary({ locator, path: file.name }, async (held) => {
|
|
2612
|
+
const result = await options.adapter.attach(held, request);
|
|
2613
|
+
if (result.kind === 'attached') {
|
|
2614
|
+
const landed = attachedFiles.get(locator) ?? new Map();
|
|
2615
|
+
landed.set(result.path, { revision: result.revision, mediaType: request.mediaType });
|
|
2616
|
+
attachedFiles.set(locator, landed);
|
|
2617
|
+
}
|
|
2618
|
+
return result;
|
|
2619
|
+
}, attachAnswer);
|
|
2649
2620
|
}
|
|
2650
2621
|
function readPart(file, part) {
|
|
2651
|
-
return
|
|
2622
|
+
return database.ask({ kind: 'file.part', key: file, part });
|
|
2652
2623
|
}
|
|
2653
2624
|
async function cleanUp() {
|
|
2654
|
-
const freed = await
|
|
2625
|
+
const freed = await database.ask({ kind: 'opened.clean' });
|
|
2655
2626
|
return freed + ((await options.shell.cleanUp?.()) ?? 0);
|
|
2656
2627
|
}
|
|
2657
2628
|
function declaredAgents() {
|
|
@@ -2783,169 +2754,225 @@ export function composeRuntime(options) {
|
|
|
2783
2754
|
options.adapter.reached(locator, written, roomVersion) &&
|
|
2784
2755
|
!options.adapter.reached(locator, roomVersion, written));
|
|
2785
2756
|
}
|
|
2786
|
-
function
|
|
2787
|
-
|
|
2788
|
-
const known = new Map(await texts(paths.filter((path) => !overrides.has(path))));
|
|
2789
|
-
for (const path of paths) {
|
|
2790
|
-
const entry = overrides.get(path);
|
|
2791
|
-
if (entry === undefined)
|
|
2792
|
-
continue;
|
|
2793
|
-
if (entry === null)
|
|
2794
|
-
known.delete(path);
|
|
2795
|
-
else
|
|
2796
|
-
known.set(path, entry.binary !== null ? null : entry.content);
|
|
2797
|
-
}
|
|
2798
|
-
return known;
|
|
2799
|
-
};
|
|
2800
|
-
}
|
|
2801
|
-
async function unreadableRoom(texts, walk, paths) {
|
|
2802
|
-
if (paths !== null) {
|
|
2803
|
-
const text = {};
|
|
2804
|
-
for (const [path, content] of await texts(paths))
|
|
2805
|
-
if (content !== null)
|
|
2806
|
-
text[path] = content;
|
|
2807
|
-
return stepped(unreadableIn(text, Object.keys(text)));
|
|
2808
|
-
}
|
|
2809
|
-
for (let after = null;;) {
|
|
2810
|
-
const page = await walk(after);
|
|
2811
|
-
const text = {};
|
|
2812
|
-
for (const { path, content } of page.entries)
|
|
2813
|
-
if (content !== null)
|
|
2814
|
-
text[path] = content;
|
|
2815
|
-
const refused = await stepped(unreadableIn(text, Object.keys(text)));
|
|
2816
|
-
if (refused !== null)
|
|
2817
|
-
return refused;
|
|
2818
|
-
if (page.next === null)
|
|
2819
|
-
return null;
|
|
2820
|
-
after = page.next;
|
|
2821
|
-
}
|
|
2822
|
-
}
|
|
2823
|
-
async function validatedRoom(local, record, view, previous, overrides = null) {
|
|
2824
|
-
const composition = compositionOf(record);
|
|
2825
|
-
const modeled = record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable';
|
|
2826
|
-
const lookup = overrides === null
|
|
2827
|
-
? lookupOf(local, record.key, view)
|
|
2828
|
-
: overriddenLookup(lookupOf(local, record.key, view), overrides);
|
|
2829
|
-
const texts = overrides === null
|
|
2830
|
-
? textsOf(local, record.key, view)
|
|
2831
|
-
: overriddenTexts(textsOf(local, record.key, view), overrides);
|
|
2832
|
-
const walk = overrides === null
|
|
2833
|
-
? walkOf(local, record.key, view)
|
|
2834
|
-
: overriddenWalk(walkOf(local, record.key, view), overrides);
|
|
2835
|
-
let changes = null;
|
|
2836
|
-
let reshaped = false;
|
|
2837
|
-
if (previous !== null && previous.changed.length <= CLEAN_CHANGES) {
|
|
2838
|
-
const [was, now] = await Promise.all([
|
|
2839
|
-
local.entries(record.key, 'confirmed', previous.changed),
|
|
2840
|
-
overrides === null
|
|
2841
|
-
? local.entries(record.key, view, previous.changed)
|
|
2842
|
-
: Promise.resolve([]),
|
|
2843
|
-
]);
|
|
2844
|
-
const before = new Map(was.map((entry) => [entry.path, entry]));
|
|
2845
|
-
const after = overrides === null
|
|
2846
|
-
? new Map(now.map((entry) => [entry.path, entry]))
|
|
2847
|
-
: overrides;
|
|
2848
|
-
changes = previous.changed.flatMap((path) => {
|
|
2849
|
-
const one = before.get(path) ?? null;
|
|
2850
|
-
const other = after.get(path) ?? null;
|
|
2851
|
-
reshaped ||= textDiffers(one, other);
|
|
2852
|
-
const held = textOf(one);
|
|
2853
|
-
const next = textOf(other);
|
|
2854
|
-
return held === next ? [] : [{ path, was: held, now: next }];
|
|
2855
|
-
});
|
|
2856
|
-
}
|
|
2857
|
-
const named = changes === null ? null : changes.map(({ path }) => path);
|
|
2858
|
-
const textChanged = changes === null || changes.length > 0 || reshaped;
|
|
2859
|
-
const modelsChanged = previous === null || !sameComposition(compositionOf(previous.record), composition);
|
|
2860
|
-
if (modeled && (textChanged || modelsChanged)) {
|
|
2861
|
-
const clean = previous !== null &&
|
|
2862
|
-
changes !== null &&
|
|
2863
|
-
(await changedCleanly(changes, compositionOf(previous.record), composition, {
|
|
2864
|
-
lookup,
|
|
2865
|
-
texts,
|
|
2866
|
-
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
2867
|
-
}));
|
|
2868
|
-
if (clean)
|
|
2869
|
-
return null;
|
|
2870
|
-
const verdict = await validatedCollection(walk, lookup, composition);
|
|
2871
|
-
if (verdict.verdict === 'accept')
|
|
2872
|
-
return null;
|
|
2873
|
-
if (verdict.rule !== 'model-unavailable')
|
|
2874
|
-
return { kind: 'refused', refusal: verdict };
|
|
2875
|
-
return unreadableRoom(texts, walk, named);
|
|
2876
|
-
}
|
|
2877
|
-
if (textChanged)
|
|
2878
|
-
return unreadableRoom(texts, walk, named);
|
|
2879
|
-
return null;
|
|
2880
|
-
}
|
|
2881
|
-
function refreshingOf(local, key, liveness) {
|
|
2757
|
+
function refreshingOf(local, key, composition, liveness, replaying = null) {
|
|
2758
|
+
const write = replaying === null ? exclusive : finishingWrite;
|
|
2882
2759
|
return {
|
|
2760
|
+
turn() {
|
|
2761
|
+
if (replaying !== null)
|
|
2762
|
+
installing(replaying);
|
|
2763
|
+
},
|
|
2883
2764
|
prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
|
|
2884
2765
|
async stage(page) {
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
const last =
|
|
2888
|
-
const
|
|
2766
|
+
for (let written = 0;;) {
|
|
2767
|
+
const end = batchEnd(page.entries, written);
|
|
2768
|
+
const last = end === page.entries.length;
|
|
2769
|
+
const entries = page.entries.slice(written, end);
|
|
2770
|
+
const staged = await write(async () => {
|
|
2889
2771
|
if (!liveness())
|
|
2890
2772
|
return false;
|
|
2891
|
-
await local.refresh(key, last
|
|
2773
|
+
await timed(stageBudget, () => local.refresh(key, composition, last
|
|
2892
2774
|
? { ...page, entries }
|
|
2893
|
-
: { entries, removals: [], folders: [], folderRemovals: [] });
|
|
2775
|
+
: { entries, removals: [], folders: [], folderRemovals: [] }));
|
|
2894
2776
|
return true;
|
|
2895
2777
|
});
|
|
2896
2778
|
if (!staged)
|
|
2897
2779
|
return false;
|
|
2780
|
+
if (last)
|
|
2781
|
+
return true;
|
|
2782
|
+
written = end;
|
|
2898
2783
|
}
|
|
2899
|
-
return true;
|
|
2900
2784
|
},
|
|
2901
2785
|
};
|
|
2902
2786
|
}
|
|
2903
|
-
function
|
|
2904
|
-
|
|
2787
|
+
function batchEnd(entries, from) {
|
|
2788
|
+
let end = from;
|
|
2905
2789
|
let spent = 0;
|
|
2906
|
-
|
|
2907
|
-
const size = (
|
|
2908
|
-
if (spent + size >
|
|
2909
|
-
|
|
2910
|
-
spent = 0;
|
|
2911
|
-
}
|
|
2912
|
-
batches.at(-1).push(entry);
|
|
2790
|
+
while (end < entries.length) {
|
|
2791
|
+
const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
|
|
2792
|
+
if (spent + size > stageBudget.bytes && end > from)
|
|
2793
|
+
break;
|
|
2913
2794
|
spent += size;
|
|
2795
|
+
end += 1;
|
|
2796
|
+
}
|
|
2797
|
+
return end;
|
|
2798
|
+
}
|
|
2799
|
+
async function timed(budget, work) {
|
|
2800
|
+
const started = performance.now();
|
|
2801
|
+
const done = await work();
|
|
2802
|
+
const spent = performance.now() - started;
|
|
2803
|
+
if (spent < budget.aim / 2)
|
|
2804
|
+
budget.bytes = Math.min(budget.ceiling, Math.round(budget.bytes * 1.5));
|
|
2805
|
+
else if (spent > budget.aim * 2) {
|
|
2806
|
+
budget.bytes = Math.max(budget.floor, Math.round(budget.bytes / 1.5));
|
|
2807
|
+
}
|
|
2808
|
+
return done;
|
|
2809
|
+
}
|
|
2810
|
+
function installWrite(work) {
|
|
2811
|
+
return installTurn(installTurns.starting, work);
|
|
2812
|
+
}
|
|
2813
|
+
function finishingWrite(work) {
|
|
2814
|
+
return installTurn(installTurns.finishing, work);
|
|
2815
|
+
}
|
|
2816
|
+
function installTurn(queue, work) {
|
|
2817
|
+
return new Promise((resolve, reject) => {
|
|
2818
|
+
queue.push(() => void exclusive(work)
|
|
2819
|
+
.then(resolve, reject)
|
|
2820
|
+
.finally(() => {
|
|
2821
|
+
installTurns.busy = false;
|
|
2822
|
+
nextInstallTurn();
|
|
2823
|
+
}));
|
|
2824
|
+
nextInstallTurn();
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
function nextInstallTurn() {
|
|
2828
|
+
if (installTurns.busy || installTurns.ahead > 0)
|
|
2829
|
+
return;
|
|
2830
|
+
const turn = installTurns.finishing.shift() ?? installTurns.starting.shift();
|
|
2831
|
+
if (turn === undefined)
|
|
2832
|
+
return;
|
|
2833
|
+
installTurns.busy = true;
|
|
2834
|
+
turn();
|
|
2835
|
+
}
|
|
2836
|
+
async function ahead(work) {
|
|
2837
|
+
let taken = false;
|
|
2838
|
+
const hold = {
|
|
2839
|
+
take() {
|
|
2840
|
+
if (taken)
|
|
2841
|
+
return;
|
|
2842
|
+
taken = true;
|
|
2843
|
+
installTurns.ahead += 1;
|
|
2844
|
+
},
|
|
2845
|
+
release() {
|
|
2846
|
+
if (!taken)
|
|
2847
|
+
return;
|
|
2848
|
+
taken = false;
|
|
2849
|
+
installTurns.ahead -= 1;
|
|
2850
|
+
resumeInstall();
|
|
2851
|
+
},
|
|
2852
|
+
};
|
|
2853
|
+
hold.take();
|
|
2854
|
+
try {
|
|
2855
|
+
return await work(hold);
|
|
2914
2856
|
}
|
|
2915
|
-
|
|
2857
|
+
finally {
|
|
2858
|
+
hold.release();
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
function resumeInstall() {
|
|
2862
|
+
if (installTurns.ahead > 0)
|
|
2863
|
+
return;
|
|
2864
|
+
void yielded().then(nextInstallTurn);
|
|
2865
|
+
}
|
|
2866
|
+
async function heldStream() {
|
|
2867
|
+
while (streamsFree === 0)
|
|
2868
|
+
await new Promise((admit) => installWaiters.push(admit));
|
|
2869
|
+
streamsFree -= 1;
|
|
2870
|
+
return once(() => {
|
|
2871
|
+
streamsFree += 1;
|
|
2872
|
+
});
|
|
2873
|
+
}
|
|
2874
|
+
async function unqueued() {
|
|
2875
|
+
while (queuedText >= QUEUED_TEXT) {
|
|
2876
|
+
await new Promise((admit) => installWaiters.push(admit));
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
function once(release) {
|
|
2880
|
+
let released = false;
|
|
2881
|
+
return () => {
|
|
2882
|
+
if (released)
|
|
2883
|
+
return;
|
|
2884
|
+
released = true;
|
|
2885
|
+
release();
|
|
2886
|
+
for (const admit of installWaiters.splice(0))
|
|
2887
|
+
admit();
|
|
2888
|
+
};
|
|
2916
2889
|
}
|
|
2917
|
-
|
|
2890
|
+
function installing(locator) {
|
|
2891
|
+
installs.add(locator);
|
|
2892
|
+
queueReadiness();
|
|
2893
|
+
}
|
|
2894
|
+
function untransferred(locator) {
|
|
2895
|
+
transfers.delete(locator);
|
|
2896
|
+
arrivals.delete(locator);
|
|
2897
|
+
streaming.delete(locator);
|
|
2898
|
+
underived.delete(locator);
|
|
2899
|
+
unindexed.delete(locator);
|
|
2900
|
+
}
|
|
2901
|
+
function setBack(locator) {
|
|
2902
|
+
const now = bytesOf(locator, true);
|
|
2903
|
+
const earlier = startedOver.get(locator);
|
|
2904
|
+
startedOver.set(locator, {
|
|
2905
|
+
done: Math.max(now.done, earlier?.done ?? 0),
|
|
2906
|
+
total: Math.max(now.total, earlier?.total ?? 0),
|
|
2907
|
+
});
|
|
2908
|
+
}
|
|
2909
|
+
function owedIndexes(locator, bytes, text) {
|
|
2910
|
+
if (bytes <= 0 || text <= 0)
|
|
2911
|
+
return;
|
|
2912
|
+
unindexed.set(locator, [...(unindexed.get(locator) ?? []), { bytes, text }]);
|
|
2913
|
+
}
|
|
2914
|
+
function built(locator, { done, text }) {
|
|
2915
|
+
const owed = unindexed.get(locator) ?? [];
|
|
2916
|
+
let left = text;
|
|
2917
|
+
while (!done && left > 0 && owed.length > 0) {
|
|
2918
|
+
const [first] = owed;
|
|
2919
|
+
if (left >= first.text) {
|
|
2920
|
+
left -= first.text;
|
|
2921
|
+
owed.shift();
|
|
2922
|
+
continue;
|
|
2923
|
+
}
|
|
2924
|
+
owed[0] = {
|
|
2925
|
+
bytes: (first.bytes * (first.text - left)) / first.text,
|
|
2926
|
+
text: first.text - left,
|
|
2927
|
+
};
|
|
2928
|
+
left = 0;
|
|
2929
|
+
}
|
|
2930
|
+
if (done || owed.length === 0)
|
|
2931
|
+
unindexed.delete(locator);
|
|
2932
|
+
queueReadiness();
|
|
2933
|
+
}
|
|
2934
|
+
function installed(locator) {
|
|
2935
|
+
if (installs.delete(locator))
|
|
2936
|
+
queueReadiness();
|
|
2937
|
+
}
|
|
2938
|
+
async function pullRefresh(authorized, held, liveness, hold) {
|
|
2918
2939
|
if (!liveness())
|
|
2919
2940
|
return unconfirmed;
|
|
2920
|
-
const turn = generation;
|
|
2921
2941
|
const { record } = held;
|
|
2922
|
-
const
|
|
2942
|
+
const from = standingOf(record.locator);
|
|
2923
2943
|
const local = await openLocal();
|
|
2924
2944
|
try {
|
|
2925
2945
|
await exclusive(() => local.unrefresh(record.key));
|
|
2926
|
-
const result = await advancement(
|
|
2946
|
+
const result = await advancement({
|
|
2947
|
+
...reads,
|
|
2948
|
+
async refresh(authorization, request) {
|
|
2949
|
+
hold.release();
|
|
2950
|
+
try {
|
|
2951
|
+
return await reads.refresh(authorization, request);
|
|
2952
|
+
}
|
|
2953
|
+
finally {
|
|
2954
|
+
hold.take();
|
|
2955
|
+
}
|
|
2956
|
+
},
|
|
2957
|
+
}, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, composed(record), liveness));
|
|
2927
2958
|
if (result.kind !== 'advanced') {
|
|
2928
2959
|
journal.record(`transfer refresh ${record.locator} ${result.kind}`);
|
|
2929
2960
|
return result;
|
|
2930
2961
|
}
|
|
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
2962
|
journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
|
|
2940
|
-
return {
|
|
2963
|
+
return {
|
|
2964
|
+
kind: 'advanced',
|
|
2965
|
+
record: result.record,
|
|
2966
|
+
from,
|
|
2967
|
+
remodelled: result.record.model !== record.model,
|
|
2968
|
+
};
|
|
2941
2969
|
}
|
|
2942
2970
|
catch (error) {
|
|
2971
|
+
if (refusedForSpace(error))
|
|
2972
|
+
return unconfirmed;
|
|
2943
2973
|
journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
|
|
2944
2974
|
throw error;
|
|
2945
2975
|
}
|
|
2946
|
-
finally {
|
|
2947
|
-
reads.finished(record.locator, turn);
|
|
2948
|
-
}
|
|
2949
2976
|
}
|
|
2950
2977
|
const heldContents = new Map();
|
|
2951
2978
|
function heldContentOf(local, key) {
|
|
@@ -2963,63 +2990,118 @@ export function composeRuntime(options) {
|
|
|
2963
2990
|
heldContents.set(key, made);
|
|
2964
2991
|
return made;
|
|
2965
2992
|
}
|
|
2993
|
+
async function abandoned(local, locator) {
|
|
2994
|
+
await local.abandon(locator);
|
|
2995
|
+
setBack(locator);
|
|
2996
|
+
untransferred(locator);
|
|
2997
|
+
liveQueries.recover();
|
|
2998
|
+
sweep();
|
|
2999
|
+
}
|
|
2966
3000
|
function staging(local, liveness) {
|
|
3001
|
+
const derived = async (locator, key, composition) => {
|
|
3002
|
+
underived.delete(locator);
|
|
3003
|
+
for (;;) {
|
|
3004
|
+
if (!liveness())
|
|
3005
|
+
return false;
|
|
3006
|
+
const done = await finishingWrite(async () => liveness()
|
|
3007
|
+
? timed(deriveBudget, () => local.rederive(key, composition, deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR))
|
|
3008
|
+
: null);
|
|
3009
|
+
if (done === null)
|
|
3010
|
+
return false;
|
|
3011
|
+
built(locator, done);
|
|
3012
|
+
if (done.done)
|
|
3013
|
+
return true;
|
|
3014
|
+
}
|
|
3015
|
+
};
|
|
2967
3016
|
return {
|
|
2968
3017
|
held: (locator) => transfers.get(locator) ?? null,
|
|
3018
|
+
async turn(locator, step) {
|
|
3019
|
+
installing(locator);
|
|
3020
|
+
return step === 'downloading' ? heldStream() : () => { };
|
|
3021
|
+
},
|
|
3022
|
+
unqueued,
|
|
3023
|
+
queued(text) {
|
|
3024
|
+
queuedText += text;
|
|
3025
|
+
return once(() => {
|
|
3026
|
+
queuedText -= text;
|
|
3027
|
+
});
|
|
3028
|
+
},
|
|
3029
|
+
arriving(locator) {
|
|
3030
|
+
let ended = false;
|
|
3031
|
+
return {
|
|
3032
|
+
at(bytes) {
|
|
3033
|
+
if (ended)
|
|
3034
|
+
return;
|
|
3035
|
+
arrivals.set(locator, bytes);
|
|
3036
|
+
streaming.add(locator);
|
|
3037
|
+
const now = performance.now();
|
|
3038
|
+
if (now - arrivalReported < ARRIVAL_REPORT_MS)
|
|
3039
|
+
return;
|
|
3040
|
+
arrivalReported = now;
|
|
3041
|
+
queueReadiness();
|
|
3042
|
+
},
|
|
3043
|
+
end() {
|
|
3044
|
+
if (ended)
|
|
3045
|
+
return;
|
|
3046
|
+
ended = true;
|
|
3047
|
+
if (streaming.delete(locator))
|
|
3048
|
+
queueReadiness();
|
|
3049
|
+
},
|
|
3050
|
+
};
|
|
3051
|
+
},
|
|
3052
|
+
announced(locator) {
|
|
3053
|
+
const later = announced.get(locator);
|
|
3054
|
+
return later === undefined ? [] : [later];
|
|
3055
|
+
},
|
|
2969
3056
|
async stage(record, page, composition) {
|
|
2970
|
-
const
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
if (!liveness())
|
|
2977
|
-
return null;
|
|
2978
|
-
if (index === 0 && !(await admitted(local, record.locator, bytes)))
|
|
2979
|
-
return null;
|
|
2980
|
-
try {
|
|
2981
|
-
return await local.stage(record, composition, { entries, folders: last ? page.folders : [] }, key, last);
|
|
2982
|
-
}
|
|
2983
|
-
catch (error) {
|
|
2984
|
-
if (!exceeded(error))
|
|
2985
|
-
throw error;
|
|
2986
|
-
await exceededOn(record.locator, bytes);
|
|
2987
|
-
return null;
|
|
2988
|
-
}
|
|
2989
|
-
});
|
|
2990
|
-
if (staged === null)
|
|
3057
|
+
const { locator } = record;
|
|
3058
|
+
installing(locator);
|
|
3059
|
+
if (!liveness())
|
|
3060
|
+
return null;
|
|
3061
|
+
const key = await installWrite(async () => {
|
|
3062
|
+
if (!liveness())
|
|
2991
3063
|
return null;
|
|
2992
|
-
|
|
2993
|
-
}
|
|
2994
|
-
|
|
3064
|
+
return local.stage(record, composition, page);
|
|
3065
|
+
});
|
|
3066
|
+
if (key === null || !liveness())
|
|
3067
|
+
return null;
|
|
3068
|
+
const arrived = !transfers.has(locator);
|
|
3069
|
+
const prior = transfers.get(locator)?.done.bytes ?? 0;
|
|
3070
|
+
const held = { ...record, key };
|
|
3071
|
+
transfers.set(locator, held);
|
|
3072
|
+
if (arrived)
|
|
3073
|
+
liveQueries.recover();
|
|
3074
|
+
installing(locator);
|
|
2995
3075
|
liveQueries.recover();
|
|
2996
|
-
|
|
3076
|
+
const text = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), 0);
|
|
3077
|
+
owedIndexes(locator, record.done.bytes - prior, text);
|
|
3078
|
+
const owed = text + (underived.get(locator) ?? 0);
|
|
3079
|
+
underived.set(locator, owed);
|
|
3080
|
+
if (owed < deriveBudget.bytes)
|
|
3081
|
+
return held;
|
|
3082
|
+
return (await derived(locator, key, composition)) ? held : null;
|
|
2997
3083
|
},
|
|
2998
|
-
|
|
3084
|
+
derive: derived,
|
|
3085
|
+
async refreshing(locator, key, composition) {
|
|
2999
3086
|
await exclusive(() => local.unrefresh(key));
|
|
3000
|
-
return refreshingOf(local, key, liveness);
|
|
3087
|
+
return refreshingOf(local, key, composition, liveness, locator);
|
|
3001
3088
|
},
|
|
3002
3089
|
abandon: (locator) => exclusive(async () => {
|
|
3003
|
-
if (
|
|
3004
|
-
|
|
3005
|
-
await local.abandon(locator);
|
|
3006
|
-
transfers.delete(locator);
|
|
3007
|
-
liveQueries.recover();
|
|
3008
|
-
sweep();
|
|
3090
|
+
if (liveness())
|
|
3091
|
+
await abandoned(local, locator);
|
|
3009
3092
|
}),
|
|
3010
|
-
setAside:
|
|
3093
|
+
setAside: async (locator) => {
|
|
3011
3094
|
if (!liveness())
|
|
3012
3095
|
return;
|
|
3013
|
-
|
|
3014
|
-
|
|
3096
|
+
setBack(locator);
|
|
3097
|
+
untransferred(locator);
|
|
3015
3098
|
liveQueries.recover();
|
|
3016
|
-
}
|
|
3099
|
+
},
|
|
3017
3100
|
release: (key) => exclusive(async () => {
|
|
3018
3101
|
await local.release(key);
|
|
3019
3102
|
sweep();
|
|
3020
3103
|
}),
|
|
3021
3104
|
content: (key) => heldContentOf(local, key),
|
|
3022
|
-
validate: (record, view) => validatedRoom(local, record, view, null),
|
|
3023
3105
|
};
|
|
3024
3106
|
}
|
|
3025
3107
|
function reusable(room, local) {
|
|
@@ -3031,30 +3113,28 @@ export function composeRuntime(options) {
|
|
|
3031
3113
|
async function pullBootstrap(authorized, room, local, liveness) {
|
|
3032
3114
|
if (!liveness())
|
|
3033
3115
|
return unconfirmed;
|
|
3034
|
-
const turn = generation;
|
|
3035
3116
|
if (replicas.has(room.locator)) {
|
|
3036
3117
|
catching.add(room.locator);
|
|
3037
3118
|
liveQueries.recover();
|
|
3038
3119
|
}
|
|
3039
3120
|
try {
|
|
3040
|
-
const result = await bootstrapRoom(reads, authorized, room, {
|
|
3041
|
-
collection: modelOf(room.locator),
|
|
3042
|
-
selection: modelOf(room.locator, 'typed-selection'),
|
|
3043
|
-
}, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
|
|
3121
|
+
const result = await bootstrapRoom(reads, authorized, room, { collection: modelOf(room.locator) }, builtIn, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
|
|
3044
3122
|
const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
|
|
3045
3123
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
3046
|
-
if (result.kind !== 'staged')
|
|
3124
|
+
if (result.kind !== 'staged') {
|
|
3047
3125
|
caughtUpTo(room.locator);
|
|
3126
|
+
installed(room.locator);
|
|
3127
|
+
}
|
|
3048
3128
|
return result;
|
|
3049
3129
|
}
|
|
3050
3130
|
catch (error) {
|
|
3051
|
-
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
3052
3131
|
caughtUpTo(room.locator);
|
|
3132
|
+
installed(room.locator);
|
|
3133
|
+
if (refusedForSpace(error))
|
|
3134
|
+
return unconfirmed;
|
|
3135
|
+
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
3053
3136
|
throw error;
|
|
3054
3137
|
}
|
|
3055
|
-
finally {
|
|
3056
|
-
reads.finished(room.locator, turn);
|
|
3057
|
-
}
|
|
3058
3138
|
}
|
|
3059
3139
|
function caughtUpTo(locator) {
|
|
3060
3140
|
if (catching.delete(locator))
|
|
@@ -3065,69 +3145,79 @@ export function composeRuntime(options) {
|
|
|
3065
3145
|
return (held !== undefined &&
|
|
3066
3146
|
[announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
|
|
3067
3147
|
}
|
|
3068
|
-
|
|
3069
|
-
const
|
|
3070
|
-
|
|
3071
|
-
if (withheld !== null || reads.carried(room.locator) === carried || !behind(room)) {
|
|
3072
|
-
return withheld;
|
|
3073
|
-
}
|
|
3074
|
-
return advanceOnce(room, local, authorized, live);
|
|
3148
|
+
function standingOf(locator) {
|
|
3149
|
+
const held = replicas.get(locator);
|
|
3150
|
+
return { key: held?.record.key ?? null, confirmed: held?.confirmed ?? 0 };
|
|
3075
3151
|
}
|
|
3076
|
-
|
|
3077
|
-
|
|
3152
|
+
function stands(locator, from) {
|
|
3153
|
+
const now = standingOf(locator);
|
|
3154
|
+
return now.key === from.key && now.confirmed === from.confirmed;
|
|
3155
|
+
}
|
|
3156
|
+
async function advance(room, local, authorized, running) {
|
|
3157
|
+
const live = () => running() && !full;
|
|
3158
|
+
if (!live())
|
|
3078
3159
|
return unconfirmed;
|
|
3079
3160
|
const held = replicas.get(room.locator);
|
|
3080
3161
|
if (held !== undefined) {
|
|
3081
|
-
const refreshed = await
|
|
3082
|
-
if (
|
|
3083
|
-
return
|
|
3084
|
-
if (refreshed
|
|
3085
|
-
stale.delete(room.locator);
|
|
3086
|
-
if (sourceRefusals.has(room.locator))
|
|
3087
|
-
return null;
|
|
3088
|
-
if (unreadable.delete(room.locator)) {
|
|
3089
|
-
scopedRefusals.delete(room.locator);
|
|
3090
|
-
list(room, true);
|
|
3091
|
-
emitRooms();
|
|
3092
|
-
}
|
|
3093
|
-
return null;
|
|
3094
|
-
}
|
|
3095
|
-
if (refreshed.kind === 'advanced') {
|
|
3096
|
-
const landing = { kind: 'refreshed', base: refreshed.base };
|
|
3097
|
-
if (!(await publish(local, room, refreshed.record, landing, live))) {
|
|
3098
|
-
const now = replicas.get(room.locator)?.record.roomVersion;
|
|
3099
|
-
const reached = now !== undefined &&
|
|
3100
|
-
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
3101
|
-
return reached ? null : advance(room, local, authorized, live);
|
|
3102
|
-
}
|
|
3103
|
-
await retainModel(local, room, 'collection', modelOf(room.locator));
|
|
3104
|
-
return null;
|
|
3105
|
-
}
|
|
3106
|
-
if (refreshed.kind !== 'beyond-horizon')
|
|
3162
|
+
const refreshed = await ahead((hold) => refreshedRoom(room, local, authorized, held, live, hold));
|
|
3163
|
+
if (refreshed === 'behind')
|
|
3164
|
+
return advance(room, local, authorized, live);
|
|
3165
|
+
if (refreshed !== 'beyond-horizon')
|
|
3107
3166
|
return refreshed;
|
|
3108
|
-
if (opened
|
|
3167
|
+
if (opened) {
|
|
3109
3168
|
owed.add(room.locator);
|
|
3110
3169
|
liveQueries.recover();
|
|
3111
3170
|
return unconfirmed;
|
|
3112
3171
|
}
|
|
3113
|
-
if (reloadsDue.
|
|
3172
|
+
if (reloadsDue.has(room.locator))
|
|
3114
3173
|
return unconfirmed;
|
|
3115
3174
|
}
|
|
3175
|
+
const from = standingOf(room.locator);
|
|
3116
3176
|
const bootstrapped = await pullBootstrap(authorized, room, local, live);
|
|
3177
|
+
if (bootstrapped.kind !== 'staged')
|
|
3178
|
+
return live() ? bootstrapped : unconfirmed;
|
|
3179
|
+
const landing = { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from };
|
|
3180
|
+
if (await publish(local, room, bootstrapped.record, landing, live)) {
|
|
3181
|
+
return live() ? null : unconfirmed;
|
|
3182
|
+
}
|
|
3183
|
+
return reachedBy(room.locator, bootstrapped.record.roomVersion)
|
|
3184
|
+
? null
|
|
3185
|
+
: advance(room, local, authorized, live);
|
|
3186
|
+
}
|
|
3187
|
+
async function refreshedRoom(room, local, authorized, held, live, hold) {
|
|
3188
|
+
const refreshed = await pullRefresh(authorized, held, live, hold);
|
|
3117
3189
|
if (!live())
|
|
3118
3190
|
return unconfirmed;
|
|
3119
|
-
if (
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3191
|
+
if (refreshed.kind === 'still-current') {
|
|
3192
|
+
stale.delete(room.locator);
|
|
3193
|
+
if (unreadable.delete(room.locator)) {
|
|
3194
|
+
scopedRefusals.delete(room.locator);
|
|
3195
|
+
list(room, true);
|
|
3196
|
+
emitRooms();
|
|
3197
|
+
}
|
|
3198
|
+
return null;
|
|
3199
|
+
}
|
|
3200
|
+
if (refreshed.kind === 'advanced') {
|
|
3201
|
+
const landing = {
|
|
3202
|
+
kind: 'refreshed',
|
|
3203
|
+
from: refreshed.from,
|
|
3204
|
+
remodelled: refreshed.remodelled,
|
|
3205
|
+
};
|
|
3206
|
+
if (!(await publish(local, room, refreshed.record, landing, live))) {
|
|
3207
|
+
const now = replicas.get(room.locator)?.record.roomVersion;
|
|
3208
|
+
const reached = now !== undefined &&
|
|
3209
|
+
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
3210
|
+
return reached ? null : 'behind';
|
|
3211
|
+
}
|
|
3212
|
+
await retainModel(local, room, modelOf(room.locator));
|
|
3213
|
+
return null;
|
|
3214
|
+
}
|
|
3215
|
+
return refreshed.kind === 'beyond-horizon' ? 'beyond-horizon' : refreshed;
|
|
3123
3216
|
}
|
|
3124
3217
|
async function withhold(withheld, room, local, progress) {
|
|
3125
3218
|
if (withheld.kind === 'unconfirmed')
|
|
3126
3219
|
return;
|
|
3127
|
-
|
|
3128
|
-
stale.delete(room.locator);
|
|
3129
|
-
else
|
|
3130
|
-
stale.add(room.locator);
|
|
3220
|
+
stale.add(room.locator);
|
|
3131
3221
|
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
3132
3222
|
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
3133
3223
|
repairable.set(room.locator, reset);
|
|
@@ -3149,30 +3239,10 @@ export function composeRuntime(options) {
|
|
|
3149
3239
|
list(room, false);
|
|
3150
3240
|
emitRooms();
|
|
3151
3241
|
}
|
|
3152
|
-
else if (withheld.kind === 'refused') {
|
|
3153
|
-
refusedSource(room, withheld);
|
|
3154
|
-
}
|
|
3155
3242
|
else if (withheld.kind === 'revoked-authorization') {
|
|
3156
3243
|
await refuse('revoked-authorization');
|
|
3157
3244
|
}
|
|
3158
3245
|
}
|
|
3159
|
-
function refusedSource(room, { refusal }) {
|
|
3160
|
-
sourceRefusals.set(room.locator, {
|
|
3161
|
-
code: refusal.rule,
|
|
3162
|
-
retryable: false,
|
|
3163
|
-
fields: {
|
|
3164
|
-
file: refusal.file,
|
|
3165
|
-
...(refusal.location === undefined ? {} : { location: refusal.location }),
|
|
3166
|
-
...(refusal.referrers === undefined ? {} : { referrers: refusal.referrers }),
|
|
3167
|
-
},
|
|
3168
|
-
});
|
|
3169
|
-
declarationRefusals.delete(room.locator);
|
|
3170
|
-
unreadable.add(room.locator);
|
|
3171
|
-
list(room, false);
|
|
3172
|
-
emitRooms();
|
|
3173
|
-
moveTo({ ...status, ...attention() });
|
|
3174
|
-
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
3175
|
-
}
|
|
3176
3246
|
function leaving(batch, result) {
|
|
3177
3247
|
if (result.kind === 'expired')
|
|
3178
3248
|
return { batch, returned: returnedBy('expired') };
|
|
@@ -3194,8 +3264,8 @@ export function composeRuntime(options) {
|
|
|
3194
3264
|
function settling(acceptance) {
|
|
3195
3265
|
return acceptance !== null && 'apply' in acceptance;
|
|
3196
3266
|
}
|
|
3197
|
-
function
|
|
3198
|
-
return settling(acceptance) ?
|
|
3267
|
+
function failureOf(acceptance) {
|
|
3268
|
+
return settling(acceptance) ? null : acceptance;
|
|
3199
3269
|
}
|
|
3200
3270
|
async function acceptance(room, local, submitted, accepted) {
|
|
3201
3271
|
if (!continuing())
|
|
@@ -3218,34 +3288,6 @@ export function composeRuntime(options) {
|
|
|
3218
3288
|
? accepted.roomVersion
|
|
3219
3289
|
: held,
|
|
3220
3290
|
};
|
|
3221
|
-
const overrides = new Map([
|
|
3222
|
-
...back.set.map((entry) => [entry.path, entry]),
|
|
3223
|
-
...back.removed.map((path) => [path, null]),
|
|
3224
|
-
]);
|
|
3225
|
-
const refused = await validatedRoom(local, record, 'confirmed', { record: current.record, changed: [...overrides.keys()] }, overrides);
|
|
3226
|
-
if (refused !== null) {
|
|
3227
|
-
const key = pendingKey(room.locator, submitted.id);
|
|
3228
|
-
return {
|
|
3229
|
-
locator: room.locator,
|
|
3230
|
-
left: [],
|
|
3231
|
-
commit: {
|
|
3232
|
-
rooms: [],
|
|
3233
|
-
writes: [
|
|
3234
|
-
{
|
|
3235
|
-
locator: room.locator,
|
|
3236
|
-
accepted: [{ id: submitted.id, roomVersion: accepted.roomVersion }],
|
|
3237
|
-
},
|
|
3238
|
-
],
|
|
3239
|
-
},
|
|
3240
|
-
refused,
|
|
3241
|
-
apply() {
|
|
3242
|
-
const state = pendings.get(key);
|
|
3243
|
-
if (state !== undefined && !fences.has(room.locator))
|
|
3244
|
-
pendings.set(key, { ...state, accepted: accepted.roomVersion });
|
|
3245
|
-
return changeSet(room.locator, { queue: true });
|
|
3246
|
-
},
|
|
3247
|
-
};
|
|
3248
|
-
}
|
|
3249
3291
|
changes = back;
|
|
3250
3292
|
}
|
|
3251
3293
|
const settled = await settlement(local, room.locator, current, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
|
|
@@ -3255,8 +3297,6 @@ export function composeRuntime(options) {
|
|
|
3255
3297
|
const set = settled.apply(answer);
|
|
3256
3298
|
pendings.delete(pendingKey(room.locator, submitted.id));
|
|
3257
3299
|
unreadable.delete(room.locator);
|
|
3258
|
-
sourceRefusals.delete(room.locator);
|
|
3259
|
-
declarationRefusals.delete(room.locator);
|
|
3260
3300
|
list(room, true);
|
|
3261
3301
|
return set;
|
|
3262
3302
|
},
|
|
@@ -3274,13 +3314,9 @@ export function composeRuntime(options) {
|
|
|
3274
3314
|
const failed = await exclusive(async () => {
|
|
3275
3315
|
const adopting = await acceptance(room, local, submitted, accepted);
|
|
3276
3316
|
await adoptedTogether(local, [adopting]);
|
|
3277
|
-
return
|
|
3317
|
+
return failureOf(adopting);
|
|
3278
3318
|
});
|
|
3279
|
-
|
|
3280
|
-
return true;
|
|
3281
|
-
if (failed.kind === 'refused' && continuing())
|
|
3282
|
-
refusedSource(room, failed);
|
|
3283
|
-
return false;
|
|
3319
|
+
return failed === null;
|
|
3284
3320
|
}
|
|
3285
3321
|
async function adoptedMove(room, partner, local, submitted, accepted) {
|
|
3286
3322
|
const other = discovered.get(partner);
|
|
@@ -3294,18 +3330,10 @@ export function composeRuntime(options) {
|
|
|
3294
3330
|
...accepted.destination,
|
|
3295
3331
|
});
|
|
3296
3332
|
await adoptedTogether(local, [left, arrived]);
|
|
3297
|
-
|
|
3298
|
-
if (other !== undefined && refusedArrival?.kind === 'refused' && continuing()) {
|
|
3299
|
-
refusedSource(other, refusedArrival);
|
|
3300
|
-
}
|
|
3301
|
-
return refusalOf(left);
|
|
3333
|
+
return failureOf(left);
|
|
3302
3334
|
});
|
|
3303
3335
|
submitLater(partner);
|
|
3304
|
-
|
|
3305
|
-
return true;
|
|
3306
|
-
if (failed.kind === 'refused' && continuing())
|
|
3307
|
-
refusedSource(room, failed);
|
|
3308
|
-
return false;
|
|
3336
|
+
return failed === null;
|
|
3309
3337
|
}
|
|
3310
3338
|
function queueIn(locator) {
|
|
3311
3339
|
return replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [];
|
|
@@ -3428,8 +3456,6 @@ export function composeRuntime(options) {
|
|
|
3428
3456
|
if (replica === undefined || head === undefined)
|
|
3429
3457
|
return 'done';
|
|
3430
3458
|
const state = pendings.get(pendingKey(room.locator, head.id));
|
|
3431
|
-
if (state?.accepted !== undefined)
|
|
3432
|
-
return 'done';
|
|
3433
3459
|
const partner = partnerOf(head, room.locator);
|
|
3434
3460
|
if (partner !== null && !leavingIn(head, room.locator)) {
|
|
3435
3461
|
submitLater(partner);
|
|
@@ -3543,12 +3569,12 @@ export function composeRuntime(options) {
|
|
|
3543
3569
|
}
|
|
3544
3570
|
if (result.kind === 'expired') {
|
|
3545
3571
|
await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, partner !== null && lost));
|
|
3546
|
-
const there = other === undefined ? null : await
|
|
3572
|
+
const there = other === undefined ? null : await advanced(other, local, authorized);
|
|
3547
3573
|
if (other !== undefined && there !== null) {
|
|
3548
3574
|
await withhold(there, other, local, progress);
|
|
3549
3575
|
return 'done';
|
|
3550
3576
|
}
|
|
3551
|
-
const withheld = await
|
|
3577
|
+
const withheld = await advanced(room, local, authorized);
|
|
3552
3578
|
if (withheld !== null) {
|
|
3553
3579
|
await withhold(withheld, room, local, progress);
|
|
3554
3580
|
return 'done';
|
|
@@ -3570,8 +3596,8 @@ export function composeRuntime(options) {
|
|
|
3570
3596
|
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
3571
3597
|
const left = leaving(head, result);
|
|
3572
3598
|
const refreshed = async () => ({
|
|
3573
|
-
there: other === undefined ? null : await
|
|
3574
|
-
here: await
|
|
3599
|
+
there: other === undefined ? null : await advanced(other, local, authorized),
|
|
3600
|
+
here: await advanced(room, local, authorized),
|
|
3575
3601
|
});
|
|
3576
3602
|
const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
|
|
3577
3603
|
if (judged !== null && (judged.there !== null || judged.here !== null)) {
|
|
@@ -3642,6 +3668,7 @@ export function composeRuntime(options) {
|
|
|
3642
3668
|
locator: room.locator,
|
|
3643
3669
|
key: before.record.key,
|
|
3644
3670
|
roots: before.record.typedSelection?.roots ?? [],
|
|
3671
|
+
composed: composedOf(composed(before.record)),
|
|
3645
3672
|
overlay: { set: [], cleared: [...before.overlaid] },
|
|
3646
3673
|
},
|
|
3647
3674
|
]),
|
|
@@ -3674,39 +3701,37 @@ export function composeRuntime(options) {
|
|
|
3674
3701
|
if (signal.aborted)
|
|
3675
3702
|
return;
|
|
3676
3703
|
const progress = { complete: true, reachable: true };
|
|
3677
|
-
await roomTask(room.locator,
|
|
3678
|
-
await Promise.race([
|
|
3679
|
-
reload(room, local, progress, () => live() && !signal.aborted),
|
|
3680
|
-
abandonedOn(signal),
|
|
3681
|
-
]);
|
|
3682
|
-
});
|
|
3704
|
+
await roomTask(room.locator, (live) => reload(room, local, progress, live));
|
|
3683
3705
|
if (signal.aborted)
|
|
3684
3706
|
return;
|
|
3685
3707
|
await rest(progress);
|
|
3686
3708
|
}
|
|
3687
3709
|
}
|
|
3688
3710
|
function awaitingAnswer(locator) {
|
|
3711
|
+
answering.get(locator)?.release();
|
|
3689
3712
|
let release = () => { };
|
|
3690
|
-
const
|
|
3713
|
+
const answered = new Promise((resolve) => {
|
|
3691
3714
|
release = resolve;
|
|
3692
3715
|
});
|
|
3693
|
-
|
|
3716
|
+
const awaited = { answered, release };
|
|
3717
|
+
answering.set(locator, awaited);
|
|
3694
3718
|
return () => {
|
|
3695
|
-
if (answering.get(locator)
|
|
3719
|
+
if (answering.get(locator) === awaited)
|
|
3696
3720
|
answering.delete(locator);
|
|
3697
3721
|
release();
|
|
3698
3722
|
};
|
|
3699
3723
|
}
|
|
3700
|
-
|
|
3701
|
-
const
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3724
|
+
function declaredAsHeld(room) {
|
|
3725
|
+
const held = replicas.get(room.locator)?.record;
|
|
3726
|
+
return (held === undefined ||
|
|
3727
|
+
(JSON.stringify(held.operations) === JSON.stringify(room.operations) &&
|
|
3728
|
+
JSON.stringify(held.replica) === JSON.stringify(room.replica) &&
|
|
3729
|
+
JSON.stringify(held.typedSelection) === JSON.stringify(room.typedSelection ?? null)));
|
|
3730
|
+
}
|
|
3731
|
+
async function retainModel(local, room, next) {
|
|
3732
|
+
const key = room.locator;
|
|
3733
|
+
const previous = modelOf(room.locator);
|
|
3734
|
+
if (JSON.stringify(previous) === JSON.stringify(next) && declaredAsHeld(room))
|
|
3710
3735
|
return;
|
|
3711
3736
|
if (next.status === 'unavailable') {
|
|
3712
3737
|
invalidModels.add(key);
|
|
@@ -3718,26 +3743,13 @@ export function composeRuntime(options) {
|
|
|
3718
3743
|
const held = replicas.get(room.locator);
|
|
3719
3744
|
let changed = null;
|
|
3720
3745
|
if (held !== undefined) {
|
|
3721
|
-
const
|
|
3746
|
+
const record = {
|
|
3722
3747
|
...held.record,
|
|
3723
3748
|
operations: room.operations,
|
|
3724
3749
|
replica: room.replica,
|
|
3725
3750
|
typedSelection: room.typedSelection ?? null,
|
|
3726
|
-
|
|
3751
|
+
model: next,
|
|
3727
3752
|
};
|
|
3728
|
-
let read = 'staged';
|
|
3729
|
-
if (!sameMembership(declared, held.record)) {
|
|
3730
|
-
const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), compositionOf(declared));
|
|
3731
|
-
read =
|
|
3732
|
-
verdict.verdict !== 'refuse'
|
|
3733
|
-
? 'staged'
|
|
3734
|
-
: verdict.rule === 'model-unavailable'
|
|
3735
|
-
? 'deferred'
|
|
3736
|
-
: { kind: 'refused', refusal: verdict };
|
|
3737
|
-
}
|
|
3738
|
-
const record = read === 'staged'
|
|
3739
|
-
? declared
|
|
3740
|
-
: { ...declared, typedSelection: held.record.typedSelection };
|
|
3741
3753
|
const confirmedLayer = await sliceAt(record, 'confirmed', held.queue, held.overlaid);
|
|
3742
3754
|
const replayedLayer = await replayed(record, confirmedLayer, held.queue);
|
|
3743
3755
|
const diff = await overlayDiff(record, held, replayedLayer.visible, replayedLayer.touched);
|
|
@@ -3748,41 +3760,33 @@ export function composeRuntime(options) {
|
|
|
3748
3760
|
key: record.key,
|
|
3749
3761
|
record: storedRecord(record),
|
|
3750
3762
|
roots: record.typedSelection?.roots ?? [],
|
|
3751
|
-
|
|
3763
|
+
composed: composedOf(composed(record)),
|
|
3752
3764
|
overlay: diff.overlay,
|
|
3753
3765
|
},
|
|
3754
3766
|
],
|
|
3755
3767
|
});
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count });
|
|
3768
|
+
const { count, indexed } = committedRoom(room.locator, answer);
|
|
3769
|
+
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count, indexed });
|
|
3759
3770
|
changed = changeSet(room.locator, {
|
|
3760
3771
|
documents: answer.changed[room.locator] ?? [],
|
|
3761
3772
|
room: true,
|
|
3762
3773
|
});
|
|
3763
|
-
if (read === 'staged' || read === 'deferred') {
|
|
3764
|
-
if (declarationRefusals.delete(room.locator)) {
|
|
3765
|
-
sourceRefusals.delete(room.locator);
|
|
3766
|
-
unreadable.delete(room.locator);
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
else {
|
|
3770
|
-
refusedSource(room, read);
|
|
3771
|
-
declarationRefusals.add(room.locator);
|
|
3772
|
-
}
|
|
3773
|
-
}
|
|
3774
|
-
models.set(key, next);
|
|
3775
|
-
if (next.status === 'current') {
|
|
3776
|
-
liveModels.set(key, JSON.stringify(next.snapshot));
|
|
3777
|
-
invalidModels.delete(key);
|
|
3778
3774
|
}
|
|
3779
|
-
|
|
3775
|
+
heldModel(room, next);
|
|
3780
3776
|
if (changed === null)
|
|
3781
3777
|
emitRooms();
|
|
3782
3778
|
else
|
|
3783
3779
|
publishChangeSet(changed);
|
|
3784
3780
|
});
|
|
3785
3781
|
}
|
|
3782
|
+
function heldModel(room, next) {
|
|
3783
|
+
models.set(room.locator, next);
|
|
3784
|
+
if (next.status === 'current') {
|
|
3785
|
+
liveModels.set(room.locator, JSON.stringify(next.snapshot));
|
|
3786
|
+
invalidModels.delete(room.locator);
|
|
3787
|
+
}
|
|
3788
|
+
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
3789
|
+
}
|
|
3786
3790
|
async function scoped(result, room, local) {
|
|
3787
3791
|
if (result.scope === 'content' && !result.retryable) {
|
|
3788
3792
|
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
@@ -3796,14 +3800,14 @@ export function composeRuntime(options) {
|
|
|
3796
3800
|
});
|
|
3797
3801
|
let scopedAway = room;
|
|
3798
3802
|
if (result.scope === 'model')
|
|
3799
|
-
await retainModel(local, room,
|
|
3803
|
+
await retainModel(local, room, {
|
|
3800
3804
|
status: 'unavailable',
|
|
3801
3805
|
reason: 'not-readable',
|
|
3802
3806
|
});
|
|
3803
3807
|
else if (result.scope !== 'operation') {
|
|
3804
3808
|
unreadable.add(room.locator);
|
|
3805
3809
|
scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
|
|
3806
|
-
await retainModel(local, scopedAway,
|
|
3810
|
+
await retainModel(local, scopedAway, modelOf(room.locator));
|
|
3807
3811
|
}
|
|
3808
3812
|
if (!continuing())
|
|
3809
3813
|
return;
|
|
@@ -3832,10 +3836,12 @@ export function composeRuntime(options) {
|
|
|
3832
3836
|
handed = true;
|
|
3833
3837
|
}
|
|
3834
3838
|
discovering?.release();
|
|
3835
|
-
const
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
+
for (const room of ready) {
|
|
3840
|
+
if (attempt !== 'unstreamed' && replicas.has(room.locator))
|
|
3841
|
+
continue;
|
|
3842
|
+
void roomTask(room.locator, (live) => loadRoom(room, local, progress, live)).then(() => rest(progress));
|
|
3843
|
+
}
|
|
3844
|
+
const failed = prepared.find((room) => room.status === 'rejected');
|
|
3839
3845
|
if (failed !== undefined)
|
|
3840
3846
|
throw failed.reason;
|
|
3841
3847
|
}
|
|
@@ -3853,7 +3859,7 @@ export function composeRuntime(options) {
|
|
|
3853
3859
|
return false;
|
|
3854
3860
|
}
|
|
3855
3861
|
if (prior !== undefined)
|
|
3856
|
-
await retainModel(local, room,
|
|
3862
|
+
await retainModel(local, room, modelOf(room.locator));
|
|
3857
3863
|
if (discoveryPaused || authorization === undefined)
|
|
3858
3864
|
return false;
|
|
3859
3865
|
return room.replica.admission === 'admitted';
|
|
@@ -3866,14 +3872,14 @@ export function composeRuntime(options) {
|
|
|
3866
3872
|
await withhold(withheld, room, local, progress);
|
|
3867
3873
|
}
|
|
3868
3874
|
function revoked(locator) {
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
3872
|
-
});
|
|
3875
|
+
const model = modelOf(locator);
|
|
3876
|
+
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
3873
3877
|
}
|
|
3874
3878
|
async function loadRoom(room, local, progress, live) {
|
|
3875
3879
|
if (!live() || authorization === undefined)
|
|
3876
3880
|
return;
|
|
3881
|
+
if (replicas.has(room.locator) && !(await prepareRoom(room, local, progress)))
|
|
3882
|
+
return;
|
|
3877
3883
|
if (replicas.has(room.locator) && revoked(room.locator)) {
|
|
3878
3884
|
await reload(room, local, progress, live);
|
|
3879
3885
|
return;
|
|
@@ -3891,14 +3897,14 @@ export function composeRuntime(options) {
|
|
|
3891
3897
|
reloadsDue.delete(room.locator);
|
|
3892
3898
|
if (!live() || authorization === undefined)
|
|
3893
3899
|
return;
|
|
3900
|
+
const from = standingOf(room.locator);
|
|
3894
3901
|
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
3895
|
-
if (!live())
|
|
3896
|
-
return;
|
|
3897
3902
|
if (bootstrapped.kind !== 'staged') {
|
|
3898
|
-
|
|
3903
|
+
if (live())
|
|
3904
|
+
await withhold(bootstrapped, room, local, progress);
|
|
3899
3905
|
return;
|
|
3900
3906
|
}
|
|
3901
|
-
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
3907
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
|
|
3902
3908
|
await caughtUp(room, local, progress, live);
|
|
3903
3909
|
}
|
|
3904
3910
|
finally {
|
|
@@ -3920,6 +3926,8 @@ export function composeRuntime(options) {
|
|
|
3920
3926
|
catch {
|
|
3921
3927
|
result = { kind: 'transport-uncertainty' };
|
|
3922
3928
|
}
|
|
3929
|
+
if (result.kind === 'discovery')
|
|
3930
|
+
roomsNamed = true;
|
|
3923
3931
|
if (!live()) {
|
|
3924
3932
|
progress.complete = false;
|
|
3925
3933
|
notifyReady();
|
|
@@ -3927,12 +3935,7 @@ export function composeRuntime(options) {
|
|
|
3927
3935
|
}
|
|
3928
3936
|
journal.record(`connection discover ${result.kind}`);
|
|
3929
3937
|
if (result.kind === 'transport-uncertainty') {
|
|
3930
|
-
|
|
3931
|
-
await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
|
|
3932
|
-
if (room.typedSelection !== null) {
|
|
3933
|
-
await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
|
|
3934
|
-
}
|
|
3935
|
-
}
|
|
3938
|
+
await settleModels([...rooms.values()].map((room) => discovered.get(room.locator) ?? room), 'cached', local);
|
|
3936
3939
|
progress.reachable = false;
|
|
3937
3940
|
progress.complete = false;
|
|
3938
3941
|
notifyReady();
|
|
@@ -3987,6 +3990,7 @@ export function composeRuntime(options) {
|
|
|
3987
3990
|
limits = result.providerLimits;
|
|
3988
3991
|
await local.fact('providerLimits', limits);
|
|
3989
3992
|
await local.fact('capabilities', capabilities);
|
|
3993
|
+
conversations.declared();
|
|
3990
3994
|
}
|
|
3991
3995
|
const signedIn = result.member ?? null;
|
|
3992
3996
|
if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
|
|
@@ -4009,23 +4013,45 @@ export function composeRuntime(options) {
|
|
|
4009
4013
|
}
|
|
4010
4014
|
}
|
|
4011
4015
|
async function admittedRead(work) {
|
|
4012
|
-
|
|
4013
|
-
|
|
4016
|
+
while (roomReads.free === 0) {
|
|
4017
|
+
await new Promise((admit) => roomReads.waiting.push(admit));
|
|
4014
4018
|
}
|
|
4015
|
-
|
|
4016
|
-
while (reads.free === 0)
|
|
4017
|
-
await new Promise((admit) => reads.waiting.push(admit));
|
|
4018
|
-
reads.free -= 1;
|
|
4019
|
+
roomReads.free -= 1;
|
|
4019
4020
|
try {
|
|
4020
4021
|
await work();
|
|
4021
4022
|
}
|
|
4022
4023
|
finally {
|
|
4023
|
-
|
|
4024
|
-
|
|
4024
|
+
roomReads.free += 1;
|
|
4025
|
+
roomReads.waiting.shift()?.();
|
|
4025
4026
|
}
|
|
4026
4027
|
}
|
|
4028
|
+
function inRoom(locator, work) {
|
|
4029
|
+
roomWork += 1;
|
|
4030
|
+
const ran = (roomLanes.get(locator) ?? Promise.resolve()).then(async () => {
|
|
4031
|
+
try {
|
|
4032
|
+
return await work(continuing);
|
|
4033
|
+
}
|
|
4034
|
+
finally {
|
|
4035
|
+
roomWork -= 1;
|
|
4036
|
+
}
|
|
4037
|
+
});
|
|
4038
|
+
const chain = ran.then(() => undefined, () => undefined);
|
|
4039
|
+
roomLanes.set(locator, chain);
|
|
4040
|
+
working.add(chain);
|
|
4041
|
+
void chain.finally(() => {
|
|
4042
|
+
working.delete(chain);
|
|
4043
|
+
if (roomLanes.get(locator) === chain)
|
|
4044
|
+
roomLanes.delete(locator);
|
|
4045
|
+
});
|
|
4046
|
+
return ran;
|
|
4047
|
+
}
|
|
4027
4048
|
function roomTask(locator, work) {
|
|
4028
|
-
|
|
4049
|
+
if (stopping)
|
|
4050
|
+
return Promise.resolve();
|
|
4051
|
+
return inRoom(locator, (live) => admittedRead(() => work(live))).catch(() => undefined);
|
|
4052
|
+
}
|
|
4053
|
+
function advanced(room, local, authorized) {
|
|
4054
|
+
return inRoom(room.locator, (live) => advance(room, local, authorized, live));
|
|
4029
4055
|
}
|
|
4030
4056
|
function task(lane, work) {
|
|
4031
4057
|
if (stopping)
|
|
@@ -4044,14 +4070,13 @@ export function composeRuntime(options) {
|
|
|
4044
4070
|
function sync(attempt) {
|
|
4045
4071
|
return task(OPENING_LANE, async (live) => {
|
|
4046
4072
|
if (!live()) {
|
|
4047
|
-
|
|
4073
|
+
readyFromStore();
|
|
4048
4074
|
return;
|
|
4049
4075
|
}
|
|
4050
4076
|
const progress = { complete: true, reachable: true };
|
|
4051
4077
|
const gate = discover();
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
underway = attempt === 'opened' ? { generation, progress } : null;
|
|
4078
|
+
opened = attempt === 'opened';
|
|
4079
|
+
underway = attempt === 'opened' ? progress : null;
|
|
4055
4080
|
if (attempt === 'opened')
|
|
4056
4081
|
streamed = progress;
|
|
4057
4082
|
complete = false;
|
|
@@ -4085,7 +4110,7 @@ export function composeRuntime(options) {
|
|
|
4085
4110
|
if (handed || closed || progress.reachable)
|
|
4086
4111
|
return;
|
|
4087
4112
|
underway = null;
|
|
4088
|
-
opened =
|
|
4113
|
+
opened = false;
|
|
4089
4114
|
journal.record('connection reopen');
|
|
4090
4115
|
await rest(progress, live);
|
|
4091
4116
|
options.adapter.roomStream?.reopen();
|
|
@@ -4122,13 +4147,12 @@ export function composeRuntime(options) {
|
|
|
4122
4147
|
});
|
|
4123
4148
|
}
|
|
4124
4149
|
function closedSocket(revoked, lost) {
|
|
4150
|
+
notifyDiscovered();
|
|
4125
4151
|
const opened = socketOpen;
|
|
4126
4152
|
socketOpen = false;
|
|
4127
4153
|
connectionLost();
|
|
4128
|
-
if (lost)
|
|
4129
|
-
reads.lost();
|
|
4154
|
+
if (lost)
|
|
4130
4155
|
conversations.lost();
|
|
4131
|
-
}
|
|
4132
4156
|
if (revoked) {
|
|
4133
4157
|
void refuse('revoked-authorization');
|
|
4134
4158
|
return;
|
|
@@ -4143,10 +4167,9 @@ export function composeRuntime(options) {
|
|
|
4143
4167
|
});
|
|
4144
4168
|
}
|
|
4145
4169
|
function applyOpening(expired) {
|
|
4146
|
-
const progress = underway
|
|
4170
|
+
const progress = underway;
|
|
4147
4171
|
const cycle = progress ?? { complete: true, reachable: true };
|
|
4148
|
-
|
|
4149
|
-
opened = null;
|
|
4172
|
+
opened = false;
|
|
4150
4173
|
const reloading = progress === null ? [] : owedReloads(expired);
|
|
4151
4174
|
const replacing = reloadNow((room) => expired.includes(room.locator) || reloading.includes(room.locator), cycle);
|
|
4152
4175
|
return task(OPENING_LANE, async (live) => {
|
|
@@ -4158,24 +4181,14 @@ export function composeRuntime(options) {
|
|
|
4158
4181
|
await rest(cycle, live);
|
|
4159
4182
|
return;
|
|
4160
4183
|
}
|
|
4161
|
-
await roomsCarried([...expired, ...reloading]);
|
|
4162
|
-
if (!live())
|
|
4163
|
-
return;
|
|
4164
4184
|
try {
|
|
4165
4185
|
const local = await openLocal();
|
|
4166
|
-
|
|
4186
|
+
await replacing;
|
|
4167
4187
|
if (progress.reachable) {
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
!reloading.includes(room.locator)) {
|
|
4172
|
-
await confirmModels(room, local);
|
|
4173
|
-
}
|
|
4174
|
-
}
|
|
4188
|
+
await settleModels([...discovered.values()].filter((room) => replicas.has(room.locator) &&
|
|
4189
|
+
!expired.includes(room.locator) &&
|
|
4190
|
+
!reloading.includes(room.locator)), 'current', local);
|
|
4175
4191
|
}
|
|
4176
|
-
const failed = reloaded.find((outcome) => outcome.status === 'rejected');
|
|
4177
|
-
if (failed !== undefined)
|
|
4178
|
-
throw failed.reason;
|
|
4179
4192
|
}
|
|
4180
4193
|
catch {
|
|
4181
4194
|
progress.complete = false;
|
|
@@ -4200,24 +4213,43 @@ export function composeRuntime(options) {
|
|
|
4200
4213
|
async function reloadNow(replaced, progress) {
|
|
4201
4214
|
const reloaded = [...discovered.values()].filter((room) => replicas.has(room.locator) && replaced(room));
|
|
4202
4215
|
for (const room of reloaded)
|
|
4203
|
-
reloadsDue.
|
|
4216
|
+
reloadsDue.add(room.locator);
|
|
4204
4217
|
try {
|
|
4205
4218
|
const local = await openLocal();
|
|
4206
|
-
|
|
4219
|
+
for (const room of reloaded) {
|
|
4220
|
+
void roomTask(room.locator, (live) => reload(room, local, progress, live)).then(() => rest(progress));
|
|
4221
|
+
}
|
|
4207
4222
|
}
|
|
4208
4223
|
catch {
|
|
4209
4224
|
for (const room of reloaded)
|
|
4210
4225
|
reloadsDue.delete(room.locator);
|
|
4211
4226
|
progress.reachable = false;
|
|
4212
|
-
return [];
|
|
4213
4227
|
}
|
|
4214
4228
|
}
|
|
4215
|
-
async function
|
|
4216
|
-
|
|
4217
|
-
const model = modelOf(room.locator
|
|
4218
|
-
|
|
4219
|
-
|
|
4229
|
+
async function settleModels(held, status, local) {
|
|
4230
|
+
const owed = (room) => {
|
|
4231
|
+
const model = modelOf(room.locator);
|
|
4232
|
+
return model.status !== 'unavailable' && model.status !== status;
|
|
4233
|
+
};
|
|
4234
|
+
const settled = await exclusive(async () => {
|
|
4235
|
+
if (!continuing())
|
|
4236
|
+
return [];
|
|
4237
|
+
const unchanged = held.filter((room) => owed(room) && declaredAsHeld(room));
|
|
4238
|
+
for (const room of unchanged) {
|
|
4239
|
+
const next = { ...modelOf(room.locator), status };
|
|
4240
|
+
const replica = replicas.get(room.locator);
|
|
4241
|
+
if (replica !== undefined)
|
|
4242
|
+
replicas.set(room.locator, { ...replica, record: { ...replica.record, model: next } });
|
|
4243
|
+
heldModel(room, next);
|
|
4220
4244
|
}
|
|
4245
|
+
if (unchanged.length > 0)
|
|
4246
|
+
emitRooms();
|
|
4247
|
+
return unchanged;
|
|
4248
|
+
});
|
|
4249
|
+
for (const room of held) {
|
|
4250
|
+
if (settled.includes(room) || !owed(room))
|
|
4251
|
+
continue;
|
|
4252
|
+
await retainModel(local, room, { ...modelOf(room.locator), status });
|
|
4221
4253
|
}
|
|
4222
4254
|
}
|
|
4223
4255
|
function submitLater(locator) {
|
|
@@ -4262,14 +4294,10 @@ export function composeRuntime(options) {
|
|
|
4262
4294
|
try {
|
|
4263
4295
|
const local = await openLocal();
|
|
4264
4296
|
await roomTask(locator, async (live) => {
|
|
4265
|
-
|
|
4266
|
-
if (!current() || authorization === undefined)
|
|
4297
|
+
if (!live() || authorization === undefined)
|
|
4267
4298
|
return;
|
|
4268
|
-
const withheld = await
|
|
4269
|
-
|
|
4270
|
-
abandonedOn(signal),
|
|
4271
|
-
]);
|
|
4272
|
-
if (withheld !== null && current())
|
|
4299
|
+
const withheld = await advance(room, local, authorization, live);
|
|
4300
|
+
if (withheld !== null && live())
|
|
4273
4301
|
await withhold(withheld, room, local, progress);
|
|
4274
4302
|
});
|
|
4275
4303
|
}
|
|
@@ -4324,14 +4352,11 @@ export function composeRuntime(options) {
|
|
|
4324
4352
|
progress.complete = false;
|
|
4325
4353
|
return;
|
|
4326
4354
|
}
|
|
4327
|
-
reads.applied();
|
|
4328
4355
|
submitAll();
|
|
4329
4356
|
for (const locator of stale)
|
|
4330
4357
|
if (repairable.has(locator))
|
|
4331
4358
|
repairLater(locator, {});
|
|
4332
4359
|
await conversations.reconcile();
|
|
4333
|
-
if (status.providerUnreachable)
|
|
4334
|
-
conversations.reconnected();
|
|
4335
4360
|
const reading = { complete: true, reachable: true };
|
|
4336
4361
|
await conversations.catchUp(reading);
|
|
4337
4362
|
if (progress.reachable && reading.reachable)
|
|
@@ -4357,7 +4382,7 @@ export function composeRuntime(options) {
|
|
|
4357
4382
|
if (!live())
|
|
4358
4383
|
return;
|
|
4359
4384
|
let lastCompleted = status.lastCompleted;
|
|
4360
|
-
let synced = complete && stale.size === 0 && !pending();
|
|
4385
|
+
let synced = complete && roomWork === 0 && stale.size === 0 && !pending();
|
|
4361
4386
|
if (synced && !recorded) {
|
|
4362
4387
|
const completedAt = new Date().toISOString();
|
|
4363
4388
|
try {
|
|
@@ -4372,42 +4397,41 @@ export function composeRuntime(options) {
|
|
|
4372
4397
|
}
|
|
4373
4398
|
}
|
|
4374
4399
|
moveTo({
|
|
4375
|
-
state: pending() ? 'pending' : synced && complete ? 'synced' : 'syncing',
|
|
4400
|
+
state: pending() ? 'pending' : synced && complete && roomWork === 0 ? 'synced' : 'syncing',
|
|
4376
4401
|
lastCompleted,
|
|
4377
|
-
providerUnreachable: !progress.reachable,
|
|
4402
|
+
providerUnreachable: progress === null ? status.providerUnreachable : !progress.reachable,
|
|
4378
4403
|
...attention(),
|
|
4379
4404
|
});
|
|
4380
4405
|
}
|
|
4381
4406
|
function discover() {
|
|
4407
|
+
discovering?.release();
|
|
4382
4408
|
let release = () => { };
|
|
4383
4409
|
const done = new Promise((resolve) => {
|
|
4384
4410
|
release = resolve;
|
|
4385
4411
|
});
|
|
4386
|
-
discovering = {
|
|
4412
|
+
discovering = { done, release };
|
|
4387
4413
|
return discovering;
|
|
4388
4414
|
}
|
|
4389
4415
|
function applyRoom(locator, roomVersion) {
|
|
4390
4416
|
announced.set(locator, roomVersion);
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
await saving.answered;
|
|
4417
|
+
if (stopping)
|
|
4418
|
+
return Promise.resolve();
|
|
4419
|
+
return inRoom(locator, async (live) => {
|
|
4420
|
+
await discovering?.done;
|
|
4421
|
+
await answering.get(locator)?.answered;
|
|
4397
4422
|
if (!live() || authorization === undefined)
|
|
4398
|
-
return;
|
|
4423
|
+
return null;
|
|
4399
4424
|
const room = discovered.get(locator);
|
|
4400
4425
|
const held = replicas.get(locator);
|
|
4401
4426
|
if (room === undefined || room.replica.admission !== 'admitted')
|
|
4402
|
-
return;
|
|
4427
|
+
return null;
|
|
4403
4428
|
if (held !== undefined &&
|
|
4404
4429
|
(held.record.replica.selection !== room.replica.selection ||
|
|
4405
4430
|
options.adapter.reached(locator, held.record.roomVersion, roomVersion))) {
|
|
4406
|
-
return;
|
|
4431
|
+
return null;
|
|
4407
4432
|
}
|
|
4408
|
-
const
|
|
4409
|
-
|
|
4410
|
-
if (during === null)
|
|
4433
|
+
const progress = underway ?? { complete: true, reachable: true };
|
|
4434
|
+
if (underway === null)
|
|
4411
4435
|
moveTo(transferring(status));
|
|
4412
4436
|
try {
|
|
4413
4437
|
const local = await openLocal();
|
|
@@ -4422,24 +4446,9 @@ export function composeRuntime(options) {
|
|
|
4422
4446
|
catch {
|
|
4423
4447
|
stale.add(locator);
|
|
4424
4448
|
}
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
await rest(progress, live);
|
|
4428
|
-
}
|
|
4429
|
-
});
|
|
4449
|
+
return progress;
|
|
4450
|
+
}).then((progress) => rest(progress), () => undefined);
|
|
4430
4451
|
}
|
|
4431
|
-
function roomsCarried(replacing) {
|
|
4432
|
-
const replaced = new Set(replacing.map(roomLane));
|
|
4433
|
-
return Promise.all([...lanes]
|
|
4434
|
-
.filter(([lane, held]) => lane !== OPENING_LANE && !replaced.has(lane) && held.generation === generation)
|
|
4435
|
-
.map(([, held]) => held.chain));
|
|
4436
|
-
}
|
|
4437
|
-
const receipts = {
|
|
4438
|
-
heads: async () => (await openLocal()).receivedHeads(),
|
|
4439
|
-
parts: async (key, count) => (await openLocal()).receivedParts(key, count),
|
|
4440
|
-
keep: async (key, head, part) => (await openLocal()).keepReceived(key, head, part),
|
|
4441
|
-
drop: async (keys) => (await openLocal()).dropReceived(keys),
|
|
4442
|
-
};
|
|
4443
4452
|
function observeStream() {
|
|
4444
4453
|
const stream = options.adapter.roomStream;
|
|
4445
4454
|
if (stream === undefined || withdrawStream !== undefined)
|
|
@@ -4451,7 +4460,6 @@ export function composeRuntime(options) {
|
|
|
4451
4460
|
socketOpen = true;
|
|
4452
4461
|
connectionLive();
|
|
4453
4462
|
generation += 1;
|
|
4454
|
-
reads.opened();
|
|
4455
4463
|
conversations.reopened();
|
|
4456
4464
|
recovery.resume();
|
|
4457
4465
|
void sync('opened');
|
|
@@ -4464,14 +4472,7 @@ export function composeRuntime(options) {
|
|
|
4464
4472
|
void rediscover(event.scope);
|
|
4465
4473
|
else
|
|
4466
4474
|
void applyRoom(event.locator, event.roomVersion);
|
|
4467
|
-
}
|
|
4468
|
-
}
|
|
4469
|
-
async function heldRevisions(locator, paths) {
|
|
4470
|
-
const held = replicas.get(locator);
|
|
4471
|
-
if (held === undefined || paths.length === 0)
|
|
4472
|
-
return new Map();
|
|
4473
|
-
const entries = await (await openLocal()).entries(held.record.key, 'confirmed', paths);
|
|
4474
|
-
return new Map(entries.flatMap(({ path, revision }) => revision === null ? [] : [[path, revision]]));
|
|
4475
|
+
});
|
|
4475
4476
|
}
|
|
4476
4477
|
function writesHeld() {
|
|
4477
4478
|
return serviceMode === 'write-halted' || haltedRefusal;
|
|
@@ -4541,8 +4542,9 @@ export function composeRuntime(options) {
|
|
|
4541
4542
|
if (closed)
|
|
4542
4543
|
return;
|
|
4543
4544
|
closed = true;
|
|
4544
|
-
|
|
4545
|
-
|
|
4545
|
+
notifyDiscovered();
|
|
4546
|
+
if (probing !== null)
|
|
4547
|
+
clearInterval(probing);
|
|
4546
4548
|
connectionLive();
|
|
4547
4549
|
lockListeners.clear();
|
|
4548
4550
|
readinessWatches.clear();
|
|
@@ -4553,11 +4555,8 @@ export function composeRuntime(options) {
|
|
|
4553
4555
|
withdrawServiceMode?.();
|
|
4554
4556
|
conversations.close();
|
|
4555
4557
|
void recovery.retire();
|
|
4556
|
-
store?.close();
|
|
4557
|
-
void opening?.then((opened) => {
|
|
4558
|
-
opened.close();
|
|
4559
|
-
}, () => { });
|
|
4560
4558
|
roomListeners.clear();
|
|
4559
|
+
unlistedRooms.clear();
|
|
4561
4560
|
refusalListeners.clear();
|
|
4562
4561
|
storageLossListeners.clear();
|
|
4563
4562
|
statusListeners.clear();
|
|
@@ -4570,6 +4569,7 @@ export function composeRuntime(options) {
|
|
|
4570
4569
|
}
|
|
4571
4570
|
async function stop() {
|
|
4572
4571
|
stopping = true;
|
|
4572
|
+
inFlight.abort();
|
|
4573
4573
|
await recovery.retire();
|
|
4574
4574
|
await Promise.all(working);
|
|
4575
4575
|
await mutations;
|
|
@@ -4583,9 +4583,9 @@ export function composeRuntime(options) {
|
|
|
4583
4583
|
observeStream();
|
|
4584
4584
|
void openLocal().then(() => {
|
|
4585
4585
|
if (capabilities.length > 0)
|
|
4586
|
-
|
|
4586
|
+
readyFromStore();
|
|
4587
4587
|
}, () => {
|
|
4588
|
-
|
|
4588
|
+
readyFromStore();
|
|
4589
4589
|
});
|
|
4590
4590
|
if (options.adapter.roomStream === undefined)
|
|
4591
4591
|
void sync('unstreamed');
|
|
@@ -4632,9 +4632,8 @@ export function composeRuntime(options) {
|
|
|
4632
4632
|
}
|
|
4633
4633
|
async function searched(input, reach, locked) {
|
|
4634
4634
|
const asked = searchPlanOf(input);
|
|
4635
|
-
const plan = { ...asked, limit: capped(asked.limit) };
|
|
4636
4635
|
const filter = {
|
|
4637
|
-
...filterOf(
|
|
4636
|
+
...filterOf(asked.documents ?? {}, roomFacts(reach), planReach(reach)),
|
|
4638
4637
|
locked,
|
|
4639
4638
|
};
|
|
4640
4639
|
const unlocking = (filter.rooms ?? [])
|
|
@@ -4643,65 +4642,41 @@ export function composeRuntime(options) {
|
|
|
4643
4642
|
for (;;) {
|
|
4644
4643
|
const cover = covered(filter);
|
|
4645
4644
|
awaitPreparation(cover);
|
|
4646
|
-
const pending = [
|
|
4647
|
-
...unlocking,
|
|
4648
|
-
...('pending' in cover ? cover.pending : []),
|
|
4649
|
-
];
|
|
4650
|
-
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
4651
|
-
const held = covered({
|
|
4652
|
-
...filter,
|
|
4653
|
-
rooms: reachedRooms(filter).filter((room) => !awaited.has(room)),
|
|
4654
|
-
});
|
|
4655
|
-
const covering = 'pending' in held ? [] : held.coverage.rooms;
|
|
4656
|
-
const unindexed = covering
|
|
4657
|
-
.filter(({ room }) => !searchIndex.rooms().has(room))
|
|
4658
|
-
.map(({ room }) => room);
|
|
4659
|
-
for (const room of unindexed)
|
|
4660
|
-
pending.push({ room, awaits: 'index' });
|
|
4661
|
-
const listed = conversations.searchable();
|
|
4662
|
-
const named = reach !== null && !reach.conversations
|
|
4663
|
-
? []
|
|
4664
|
-
: (plan.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
4665
|
-
const waiting = named.filter((one) => !searchIndex.conversations().has(one));
|
|
4666
|
-
for (const conversation of waiting)
|
|
4667
|
-
pending.push({ conversation, awaits: 'index' });
|
|
4668
|
-
if (unindexed.length > 0 || waiting.length > 0)
|
|
4669
|
-
index(unindexed, waiting);
|
|
4645
|
+
const pending = [...unlocking, ...('pending' in cover ? cover.pending : [])];
|
|
4670
4646
|
const complete = pending.length === 0 && !('pending' in cover);
|
|
4647
|
+
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
4671
4648
|
const answering = complete
|
|
4672
4649
|
? filter
|
|
4673
|
-
: {
|
|
4674
|
-
...filter,
|
|
4675
|
-
rooms: covering
|
|
4676
|
-
.filter(({ room }) => searchIndex.rooms().has(room))
|
|
4677
|
-
.map(({ room }) => room),
|
|
4678
|
-
};
|
|
4650
|
+
: { ...filter, rooms: reachedRooms(filter).filter((room) => !awaited.has(room)) };
|
|
4679
4651
|
const answered = covered(answering);
|
|
4680
4652
|
if ('pending' in answered)
|
|
4681
4653
|
continue;
|
|
4682
|
-
const
|
|
4683
|
-
const
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4654
|
+
const listed = conversations.searchable();
|
|
4655
|
+
const named = reach !== null && !reach.conversations
|
|
4656
|
+
? []
|
|
4657
|
+
: (asked.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
4658
|
+
const hits = await (await openLocal()).search({
|
|
4659
|
+
text: asked.text,
|
|
4660
|
+
limit: capped(asked.limit),
|
|
4661
|
+
after: asked.after,
|
|
4662
|
+
total: asked.total,
|
|
4663
|
+
documents: {
|
|
4664
|
+
plan: answering,
|
|
4665
|
+
rooms: answered.rooms,
|
|
4666
|
+
beneath: asked.collection
|
|
4667
|
+
? answered.rooms.map(({ locator, key }) => ({
|
|
4668
|
+
key,
|
|
4669
|
+
roots: replicas.get(locator)?.record.typedSelection?.roots ?? [],
|
|
4670
|
+
}))
|
|
4671
|
+
: [],
|
|
4672
|
+
},
|
|
4673
|
+
conversations: named,
|
|
4674
|
+
});
|
|
4675
|
+
if (JSON.stringify(covered(answering)) !== JSON.stringify(answered))
|
|
4690
4676
|
continue;
|
|
4691
|
-
const hits = searchIndex.search(plan, selected === null ? null : selected.documents, searchedConversations, answering.rooms !== null
|
|
4692
|
-
? new Set(answering.rooms)
|
|
4693
|
-
: locked.length === 0
|
|
4694
|
-
? null
|
|
4695
|
-
: new Set(reachedRooms(answering)), plan.collection
|
|
4696
|
-
? new Map(answered.coverage.rooms.map(({ room }) => [
|
|
4697
|
-
room,
|
|
4698
|
-
replicas.get(room)?.record.typedSelection?.roots ?? [],
|
|
4699
|
-
]))
|
|
4700
|
-
: null);
|
|
4701
4677
|
const coverage = {
|
|
4702
4678
|
rooms: answered.coverage.rooms,
|
|
4703
|
-
conversations:
|
|
4704
|
-
index: searchIndex.generation(),
|
|
4679
|
+
conversations: named,
|
|
4705
4680
|
overlay: answered.coverage.overlay,
|
|
4706
4681
|
};
|
|
4707
4682
|
return complete
|
|
@@ -4758,12 +4733,13 @@ export function composeRuntime(options) {
|
|
|
4758
4733
|
};
|
|
4759
4734
|
}
|
|
4760
4735
|
if (topic === 'readiness') {
|
|
4761
|
-
return watchReadiness({ target: '', code: 'stored',
|
|
4736
|
+
return watchReadiness({ target: '', code: 'stored', space: null, search: 'unused' }, null, deliver);
|
|
4762
4737
|
}
|
|
4763
4738
|
if (topic === 'rooms') {
|
|
4764
|
-
|
|
4765
|
-
|
|
4739
|
+
unlistedRooms.add(deliver);
|
|
4740
|
+
emitKnownRooms();
|
|
4766
4741
|
return () => {
|
|
4742
|
+
unlistedRooms.delete(deliver);
|
|
4767
4743
|
roomListeners.delete(deliver);
|
|
4768
4744
|
};
|
|
4769
4745
|
}
|
|
@@ -4817,7 +4793,17 @@ export function composeRuntime(options) {
|
|
|
4817
4793
|
return openLocal().then(() => undefined);
|
|
4818
4794
|
},
|
|
4819
4795
|
rooms() {
|
|
4820
|
-
|
|
4796
|
+
if (closed)
|
|
4797
|
+
return Promise.resolve([]);
|
|
4798
|
+
return new Promise((settle) => {
|
|
4799
|
+
const answer = (value) => {
|
|
4800
|
+
unlistedRooms.delete(answer);
|
|
4801
|
+
roomListeners.delete(answer);
|
|
4802
|
+
settle(value);
|
|
4803
|
+
};
|
|
4804
|
+
unlistedRooms.add(answer);
|
|
4805
|
+
emitKnownRooms();
|
|
4806
|
+
});
|
|
4821
4807
|
},
|
|
4822
4808
|
query: ((input) => rowsQuery(planned(input, null))),
|
|
4823
4809
|
search: (input) => searched(input, null, []),
|
|
@@ -4827,7 +4813,7 @@ export function composeRuntime(options) {
|
|
|
4827
4813
|
typedSelection: rooms.get(locator)?.typedSelection === undefined ||
|
|
4828
4814
|
rooms.get(locator)?.typedSelection === null
|
|
4829
4815
|
? null
|
|
4830
|
-
: modelTypes(
|
|
4816
|
+
: modelTypes(builtIn),
|
|
4831
4817
|
};
|
|
4832
4818
|
},
|
|
4833
4819
|
roomDraft: {
|
|
@@ -4844,33 +4830,17 @@ export function composeRuntime(options) {
|
|
|
4844
4830
|
const record = roomDrafts.get(room)?.get(path);
|
|
4845
4831
|
return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
|
|
4846
4832
|
},
|
|
4847
|
-
write(room, path, revision, input) {
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
const
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
? undefined
|
|
4859
|
-
: await heldFile(parts.file.blob, binaryBase(await visibleOf(room, [path]), path));
|
|
4860
|
-
const record = {
|
|
4861
|
-
locator: room,
|
|
4862
|
-
path,
|
|
4863
|
-
revision: roomDraftRevision(),
|
|
4864
|
-
parts: held(current?.parts, parts, file),
|
|
4865
|
-
returned: current?.returned ?? null,
|
|
4866
|
-
};
|
|
4867
|
-
await local.keepRoomDraft(record, parts.file === undefined || file === undefined
|
|
4868
|
-
? []
|
|
4869
|
-
: [{ key: file.localId, blob: parts.file.blob }]);
|
|
4870
|
-
rememberRoomDraft(record);
|
|
4871
|
-
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
4872
|
-
return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
|
|
4873
|
-
});
|
|
4833
|
+
write(room, path, revision, input, next = roomDraftRevision()) {
|
|
4834
|
+
if (typeof next !== 'string' || next === '')
|
|
4835
|
+
return Promise.reject(runtimeError('room-draft-malformed'));
|
|
4836
|
+
return gathered(`room-draft\u0000${room}\u0000${path}`, { revision, input, next }, (earlier, later) => {
|
|
4837
|
+
if (later.revision !== earlier.next)
|
|
4838
|
+
return null;
|
|
4839
|
+
const parts = gatheredParts(earlier.input, later.input);
|
|
4840
|
+
return parts === null
|
|
4841
|
+
? null
|
|
4842
|
+
: { revision: earlier.revision, input: parts, next: later.next };
|
|
4843
|
+
}, (written) => writtenRoomDraft(room, path, written.revision, written.input, written.next));
|
|
4874
4844
|
},
|
|
4875
4845
|
discard(room, path, revision) {
|
|
4876
4846
|
return exclusive(async () => {
|
|
@@ -4897,13 +4867,13 @@ export function composeRuntime(options) {
|
|
|
4897
4867
|
});
|
|
4898
4868
|
},
|
|
4899
4869
|
write(app, value) {
|
|
4900
|
-
return
|
|
4870
|
+
return exclusive(() => keptPresentation(app, () => structuredClone(value)));
|
|
4901
4871
|
},
|
|
4902
4872
|
edit(app, members) {
|
|
4903
|
-
return
|
|
4904
|
-
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(
|
|
4905
|
-
...Object.entries(
|
|
4906
|
-
]));
|
|
4873
|
+
return gathered(`presentation\u0000${app}`, structuredClone(members), (earlier, later) => ({ ...earlier, ...later }), (edited) => keptPresentation(app, (held) => Object.fromEntries([
|
|
4874
|
+
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(edited, member)),
|
|
4875
|
+
...Object.entries(edited).filter(([, value]) => value !== null),
|
|
4876
|
+
])));
|
|
4907
4877
|
},
|
|
4908
4878
|
},
|
|
4909
4879
|
save(room, named, writtenBy) {
|
|
@@ -4922,7 +4892,8 @@ export function composeRuntime(options) {
|
|
|
4922
4892
|
part: readPart,
|
|
4923
4893
|
read: readBinaryPath,
|
|
4924
4894
|
link: linkBinaryPath,
|
|
4925
|
-
|
|
4895
|
+
show: showBinaryPath,
|
|
4896
|
+
attach: attachBinary,
|
|
4926
4897
|
},
|
|
4927
4898
|
async call(name, input) {
|
|
4928
4899
|
await ready;
|
|
@@ -4943,9 +4914,10 @@ export function composeRuntime(options) {
|
|
|
4943
4914
|
},
|
|
4944
4915
|
async retry() {
|
|
4945
4916
|
journal.record('retry');
|
|
4946
|
-
|
|
4917
|
+
unpreparable.clear();
|
|
4947
4918
|
queueReadiness();
|
|
4948
|
-
|
|
4919
|
+
if (full)
|
|
4920
|
+
await probed();
|
|
4949
4921
|
},
|
|
4950
4922
|
observe: observing(watch),
|
|
4951
4923
|
async reauthorize() {
|
|
@@ -4967,7 +4939,6 @@ export function composeRuntime(options) {
|
|
|
4967
4939
|
const reachable = {
|
|
4968
4940
|
md,
|
|
4969
4941
|
watch,
|
|
4970
|
-
referrerScope: (referrer) => options.adapter.referrerScope(referrer),
|
|
4971
4942
|
plan: planned,
|
|
4972
4943
|
query: (plan) => rowsQuery(plan),
|
|
4973
4944
|
watchPlan,
|
|
@@ -4977,6 +4948,7 @@ export function composeRuntime(options) {
|
|
|
4977
4948
|
lockedRooms,
|
|
4978
4949
|
watchLocks,
|
|
4979
4950
|
located,
|
|
4951
|
+
room: (locator) => listed().find((one) => one.locator === locator),
|
|
4980
4952
|
};
|
|
4981
4953
|
return {
|
|
4982
4954
|
md,
|
|
@@ -4991,6 +4963,10 @@ export function composeRuntime(options) {
|
|
|
4991
4963
|
journal.record(`persistence ${answer}`);
|
|
4992
4964
|
queueReadiness();
|
|
4993
4965
|
},
|
|
4994
|
-
within: (reach, port) =>
|
|
4966
|
+
within: (reach, port) => {
|
|
4967
|
+
if (full)
|
|
4968
|
+
void probed();
|
|
4969
|
+
return narrowed(reachable, reach, port);
|
|
4970
|
+
},
|
|
4995
4971
|
};
|
|
4996
4972
|
}
|