@agentshouse/mdruntime 0.1.0-c52f87bb83a70 → 0.1.0-c53207a379315
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 +28 -16
- 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 +10 -3
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +19 -4
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +138 -65
- 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 -0
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +23 -2
- package/dist/compile.d.ts +6 -1
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +78 -25
- package/dist/connection.d.ts +8 -6
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +746 -603
- package/dist/conversation.d.ts +2 -2
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +49 -33
- package/dist/durability.d.ts +2 -5
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- 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 +368 -0
- package/dist/host.d.ts +4 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +39 -17
- 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 +8 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- 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 +208 -0
- package/dist/memory.d.ts +1 -1
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +43 -18
- 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 -0
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +7 -1
- 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 -3
- 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 +6 -2
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +65 -42
- package/dist/readiness.d.ts +20 -9
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +6 -6
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +3 -1
- 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 -4
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +4 -3
- package/dist/room-draft.d.ts +1 -0
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +33 -0
- 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/store.d.ts +28 -43
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +50 -231
- package/dist/stored.d.ts +21 -11
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +159 -146
- package/dist/tables.d.ts +56 -17
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +98 -8
- 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 +150 -0
- package/package.json +14 -18
- package/AGENTS.md +0 -310
- 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 -237
- package/dist/chat/messages.d.ts +0 -29
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -190
- 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 -68
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -619
- 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 -306
- package/dist/chat.d.ts +0 -15
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
package/dist/connection.js
CHANGED
|
@@ -1,51 +1,42 @@
|
|
|
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
|
-
import { bootstrapRoom } from './bootstrap.js';
|
|
11
|
+
import { bootstrapRoom, NOTHING_DONE } 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, unreadable as unreadableIn, validRoot, } from './collection.js';
|
|
15
|
+
import { available, collectionOf, composedOf, unreadable as unreadableIn, 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
23
|
import { doorReads } from './reads.js';
|
|
23
24
|
import { advancement } from './refresh.js';
|
|
24
25
|
import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
|
|
25
26
|
import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, sameMembership, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
|
|
26
|
-
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
27
|
+
import { binaryBase, changedParts, changesOf, gatheredParts, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
|
|
27
28
|
import { same, saving, STARTING, transferring, } from './status.js';
|
|
28
|
-
import { detectStorageLoss, nextGeneration } from './durability.js';
|
|
29
|
-
import { readinessOf,
|
|
29
|
+
import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
|
|
30
|
+
import { readinessOf, } from './readiness.js';
|
|
30
31
|
import { FencedMap } from './fence.js';
|
|
31
|
-
import { fingerprint } from './stored.js';
|
|
32
32
|
import { sameBinary } from './overlay.js';
|
|
33
33
|
import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
|
|
34
34
|
import { identityTime, roomDraftRevision } from './identity.js';
|
|
35
|
-
import { beneath, normalize } from './paths.js';
|
|
35
|
+
import { ancestors, beneath, normalize } from './paths.js';
|
|
36
36
|
import { openRuntimeStore, } from './store.js';
|
|
37
37
|
import { changedCleanly, sameComposition, validatedCollection, } from './validation.js';
|
|
38
38
|
import { unconfirmed } from './transfer.js';
|
|
39
39
|
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
40
|
function within(root, path) {
|
|
50
41
|
return root === '' || beneath(root, path);
|
|
51
42
|
}
|
|
@@ -63,12 +54,21 @@ const OPENING_LANE = 'opening';
|
|
|
63
54
|
const CONVERSATION_LANE = 'conversations';
|
|
64
55
|
const ROOM_READS = 256;
|
|
65
56
|
const ROOM_ATTEMPTS = 16;
|
|
66
|
-
const SEARCH_BATCH = 256;
|
|
67
57
|
const ERASE_BATCH = 512;
|
|
68
|
-
const
|
|
58
|
+
const DERIVE_BYTES = 1 << 23;
|
|
59
|
+
const DERIVE_FLOOR = 1 << 18;
|
|
60
|
+
// 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.
|
|
61
|
+
const DERIVE_AIM_MS = 250;
|
|
69
62
|
const WALK_BYTES = 1 << 20;
|
|
70
63
|
const QUERY_BYTES = 1 << 20;
|
|
71
64
|
const STAGE_BYTES = 1 << 20;
|
|
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 PARTS_HELD = 4;
|
|
69
|
+
const QUEUED_BYTES = 1 << 23;
|
|
70
|
+
// Bytes still arriving are reported at most this often, well inside the 2 s progress bound, so readiness is not rebuilt for every chunk the network hands over.
|
|
71
|
+
const ARRIVAL_REPORT_MS = 250;
|
|
72
72
|
const CLEAN_CHANGES = 2_000;
|
|
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;
|
|
@@ -92,10 +92,9 @@ function pendingKey(locator, id) {
|
|
|
92
92
|
}
|
|
93
93
|
export function composeRuntime(options) {
|
|
94
94
|
const localSaves = options.adapter.localSaves === true;
|
|
95
|
-
const roomTypes = roomTypesOf(options.adapter.roomTypes);
|
|
95
|
+
const roomTypes = roomTypesOf(options.adapter.roomTypes, options.adapter.nameableRoomTypes);
|
|
96
96
|
let authorization = options.authorization;
|
|
97
97
|
let opening;
|
|
98
|
-
let store;
|
|
99
98
|
const fences = new Map();
|
|
100
99
|
const isFenced = (locator) => fences.has(locator);
|
|
101
100
|
let fencings = 0;
|
|
@@ -103,20 +102,23 @@ export function composeRuntime(options) {
|
|
|
103
102
|
const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
|
|
104
103
|
let discoveries = 0;
|
|
105
104
|
const replicas = new FencedMap(isFenced);
|
|
105
|
+
const reopened = new Set();
|
|
106
|
+
const usableWhenHeld = options.usableWhenHeld ?? [];
|
|
106
107
|
const queuesWithoutReplica = new FencedMap(isFenced);
|
|
107
108
|
const pendings = new Map();
|
|
108
109
|
const seeds = new Map();
|
|
109
110
|
const seedWatchers = new Map();
|
|
110
111
|
const roomDrafts = new FencedMap(isFenced);
|
|
112
|
+
const attachedFiles = new FencedMap(isFenced);
|
|
111
113
|
const presentations = new Map();
|
|
112
114
|
let limits = [];
|
|
113
115
|
let capabilities = [];
|
|
114
116
|
const rooms = new FencedMap(isFenced);
|
|
115
117
|
const counted = new Map();
|
|
116
118
|
const unreadable = new Set();
|
|
117
|
-
const
|
|
118
|
-
const models = new FencedMap(
|
|
119
|
-
const liveModels = new FencedMap(
|
|
119
|
+
const builtIn = builtInModel(roomTypes);
|
|
120
|
+
const models = new FencedMap(isFenced);
|
|
121
|
+
const liveModels = new FencedMap(isFenced);
|
|
120
122
|
const invalidModels = new Set();
|
|
121
123
|
const discovered = new Map();
|
|
122
124
|
const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
|
|
@@ -124,23 +126,19 @@ export function composeRuntime(options) {
|
|
|
124
126
|
const scopedRefusals = new Map();
|
|
125
127
|
const sourceRefusals = new Map();
|
|
126
128
|
const declarationRefusals = new Set();
|
|
127
|
-
function
|
|
128
|
-
return
|
|
129
|
-
}
|
|
130
|
-
function modelOf(locator, attached = 'collection') {
|
|
131
|
-
return models.get(modelKey(locator, attached)) ?? NO_MODEL;
|
|
129
|
+
function modelOf(locator) {
|
|
130
|
+
return models.get(locator) ?? NO_MODEL;
|
|
132
131
|
}
|
|
133
|
-
function savedModel(locator
|
|
134
|
-
return invalidModels.has(
|
|
132
|
+
function savedModel(locator) {
|
|
133
|
+
return invalidModels.has(locator)
|
|
135
134
|
? { status: 'unavailable', reason: 'revoked' }
|
|
136
|
-
: modelOf(locator
|
|
135
|
+
: modelOf(locator);
|
|
136
|
+
}
|
|
137
|
+
function composed(record) {
|
|
138
|
+
return compositionOf(record, builtIn);
|
|
137
139
|
}
|
|
138
140
|
function compositionFor(record) {
|
|
139
|
-
return
|
|
140
|
-
...record,
|
|
141
|
-
model: savedModel(record.locator, 'collection'),
|
|
142
|
-
selectionModel: savedModel(record.locator, 'typed-selection'),
|
|
143
|
-
});
|
|
141
|
+
return composed({ ...record, model: savedModel(record.locator) });
|
|
144
142
|
}
|
|
145
143
|
let refusal = null;
|
|
146
144
|
let storageLoss = null;
|
|
@@ -172,6 +170,7 @@ export function composeRuntime(options) {
|
|
|
172
170
|
let roomReads = { generation, free: ROOM_READS, waiting: [] };
|
|
173
171
|
const working = new Set();
|
|
174
172
|
let mutations = Promise.resolve();
|
|
173
|
+
let gathering = null;
|
|
175
174
|
const roomListeners = new Set();
|
|
176
175
|
const refusalListeners = new Set();
|
|
177
176
|
const storageLossListeners = new Set();
|
|
@@ -192,19 +191,53 @@ export function composeRuntime(options) {
|
|
|
192
191
|
const serviceModeListeners = new Set();
|
|
193
192
|
let member = null;
|
|
194
193
|
const memberListeners = new Set();
|
|
195
|
-
const prepared = new Set();
|
|
196
194
|
const unpreparedDemand = new Set();
|
|
197
|
-
const
|
|
195
|
+
const unpreparable = new Set();
|
|
198
196
|
let preparing = false;
|
|
199
197
|
let targets = {};
|
|
200
198
|
let persistence = options.shell.persistence;
|
|
201
|
-
let
|
|
202
|
-
|
|
203
|
-
let
|
|
199
|
+
let full = false;
|
|
200
|
+
let probing = null;
|
|
201
|
+
let testing = null;
|
|
202
|
+
const database = {
|
|
203
|
+
async ask(request) {
|
|
204
|
+
try {
|
|
205
|
+
return await options.database.ask(request);
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
if (refusedForSpace(error))
|
|
209
|
+
filled();
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
};
|
|
204
214
|
const readinessWatches = new Set();
|
|
205
215
|
let readinessQueued = false;
|
|
206
216
|
const catching = new Set();
|
|
207
217
|
const transfers = new FencedMap(isFenced);
|
|
218
|
+
const installs = new Map();
|
|
219
|
+
const keptParts = new Map();
|
|
220
|
+
const arrivals = new Map();
|
|
221
|
+
const underived = new Map();
|
|
222
|
+
let arrivalReported = 0;
|
|
223
|
+
let onScreen = options.shell.onScreen;
|
|
224
|
+
const shownWaiters = [];
|
|
225
|
+
let partsFree = PARTS_HELD;
|
|
226
|
+
let queuedBytes = 0;
|
|
227
|
+
const partWaiters = [];
|
|
228
|
+
const stageBudget = {
|
|
229
|
+
bytes: STAGE_FLOOR * 4,
|
|
230
|
+
floor: STAGE_FLOOR,
|
|
231
|
+
ceiling: STAGE_BYTES,
|
|
232
|
+
aim: STAGE_AIM_MS,
|
|
233
|
+
};
|
|
234
|
+
const deriveBudget = {
|
|
235
|
+
bytes: DERIVE_FLOOR * 4,
|
|
236
|
+
floor: DERIVE_FLOOR,
|
|
237
|
+
ceiling: DERIVE_BYTES,
|
|
238
|
+
aim: DERIVE_AIM_MS,
|
|
239
|
+
};
|
|
240
|
+
let installWrites = Promise.resolve();
|
|
208
241
|
const conversations = createConversationStore({
|
|
209
242
|
agents: declaredAgents,
|
|
210
243
|
documents: (declaration, after) => rowsQuery(planOf({
|
|
@@ -225,40 +258,26 @@ export function composeRuntime(options) {
|
|
|
225
258
|
stream: () => options.adapter.conversations,
|
|
226
259
|
publish: publishChangeSet,
|
|
227
260
|
online: () => connectionOf() !== 'unreachable' && refusal === null,
|
|
261
|
+
full: () => full,
|
|
228
262
|
closed: () => closed,
|
|
229
263
|
unanswered: resendLater,
|
|
230
264
|
expired: (operation) => pastHorizon(operation),
|
|
231
265
|
});
|
|
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
266
|
const journal = createLog();
|
|
257
|
-
const keptFiles = openedFiles(
|
|
258
|
-
let
|
|
267
|
+
const keptFiles = openedFiles(database, isFenced);
|
|
268
|
+
let readyFromStore = () => { };
|
|
259
269
|
const ready = new Promise((resolve) => {
|
|
260
|
-
|
|
270
|
+
readyFromStore = resolve;
|
|
271
|
+
});
|
|
272
|
+
let notifyDiscovered = () => { };
|
|
273
|
+
const firstDiscovery = new Promise((resolve) => {
|
|
274
|
+
notifyDiscovered = resolve;
|
|
261
275
|
});
|
|
276
|
+
let roomsNamed = false;
|
|
277
|
+
function notifyReady() {
|
|
278
|
+
notifyDiscovered();
|
|
279
|
+
readyFromStore();
|
|
280
|
+
}
|
|
262
281
|
function rememberRoomDraft(record) {
|
|
263
282
|
const room = roomDrafts.get(record.locator) ?? new Map();
|
|
264
283
|
room.set(record.path, record);
|
|
@@ -278,7 +297,7 @@ export function composeRuntime(options) {
|
|
|
278
297
|
return undefined;
|
|
279
298
|
const local = await openLocal();
|
|
280
299
|
const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
|
|
281
|
-
return { ...slice, collection: collectionOf(slice.documents,
|
|
300
|
+
return { ...slice, collection: collectionOf(slice.documents, composed(held.record)) };
|
|
282
301
|
}
|
|
283
302
|
function roomDraftOf(record, visible) {
|
|
284
303
|
return structuredClone({
|
|
@@ -334,14 +353,38 @@ export function composeRuntime(options) {
|
|
|
334
353
|
const value = presentations.get(app);
|
|
335
354
|
return value === undefined ? null : structuredClone(value);
|
|
336
355
|
}
|
|
337
|
-
function
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
356
|
+
async function writtenRoomDraft(room, path, revision, input, next) {
|
|
357
|
+
const parts = readParts(input);
|
|
358
|
+
if (path === '' || normalize(path) !== path)
|
|
359
|
+
throw runtimeError('room-draft-malformed');
|
|
360
|
+
const local = await openLocal();
|
|
361
|
+
const current = roomDrafts.get(room)?.get(path);
|
|
362
|
+
if ((current?.revision ?? null) !== revision) {
|
|
363
|
+
return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
|
|
364
|
+
}
|
|
365
|
+
const file = parts.file === undefined
|
|
366
|
+
? undefined
|
|
367
|
+
: await heldFile(parts.file.blob, binaryBase(await visibleOf(room, [path]), path));
|
|
368
|
+
const record = {
|
|
369
|
+
locator: room,
|
|
370
|
+
path,
|
|
371
|
+
revision: next,
|
|
372
|
+
parts: held(current?.parts, parts, file),
|
|
373
|
+
returned: current?.returned ?? null,
|
|
374
|
+
};
|
|
375
|
+
await local.keepRoomDraft(record, parts.file === undefined || file === undefined
|
|
376
|
+
? []
|
|
377
|
+
: [{ key: file.localId, blob: parts.file.blob }]);
|
|
378
|
+
rememberRoomDraft(record);
|
|
379
|
+
publishChangeSet(changeSet(room, { drafts: [path] }));
|
|
380
|
+
return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
|
|
381
|
+
}
|
|
382
|
+
async function keptPresentation(app, next) {
|
|
383
|
+
const local = await openLocal();
|
|
384
|
+
const stored = next(presentations.get(app));
|
|
385
|
+
await local.present({ app, value: stored });
|
|
386
|
+
presentations.set(app, stored);
|
|
387
|
+
publishChangeSet(changeSet('', { presentation: app }));
|
|
345
388
|
}
|
|
346
389
|
function emitPresentation(app) {
|
|
347
390
|
if (closed)
|
|
@@ -461,7 +504,7 @@ export function composeRuntime(options) {
|
|
|
461
504
|
const governed = governs(reach, room);
|
|
462
505
|
const attached = [
|
|
463
506
|
...(governed.collection ? [modelOf(room.locator)] : []),
|
|
464
|
-
...(governed.selection ? [
|
|
507
|
+
...(governed.selection ? [builtIn] : []),
|
|
465
508
|
];
|
|
466
509
|
return {
|
|
467
510
|
locator: room.locator,
|
|
@@ -532,23 +575,29 @@ export function composeRuntime(options) {
|
|
|
532
575
|
}
|
|
533
576
|
function applyChangeSet(set) {
|
|
534
577
|
liveQueries.touched(set);
|
|
535
|
-
searchIndex.changed(set);
|
|
536
578
|
}
|
|
537
579
|
function unpublished(error) {
|
|
538
580
|
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
539
581
|
}
|
|
582
|
+
function indexed(locator) {
|
|
583
|
+
return replicas.get(locator)?.indexed === true;
|
|
584
|
+
}
|
|
540
585
|
async function prepareReplica(locator) {
|
|
541
586
|
for (;;) {
|
|
542
587
|
const held = replicas.get(locator);
|
|
543
|
-
if (held === undefined || closed ||
|
|
588
|
+
if (held === undefined || closed || held.indexed)
|
|
544
589
|
return;
|
|
545
590
|
const key = held.record.key;
|
|
546
|
-
const done = await exclusive(async () =>
|
|
547
|
-
|
|
591
|
+
const done = await exclusive(async () => {
|
|
592
|
+
const local = await openLocal();
|
|
593
|
+
return timed(deriveBudget, () => local.rederive(key, composed(held.record), deriveBudget.bytes));
|
|
594
|
+
});
|
|
595
|
+
const now = replicas.get(locator);
|
|
596
|
+
if (now?.record.key !== key)
|
|
548
597
|
continue;
|
|
549
598
|
if (!done)
|
|
550
599
|
continue;
|
|
551
|
-
|
|
600
|
+
replicas.set(locator, { ...now, indexed: true });
|
|
552
601
|
publishChangeSet(changeSet(locator, { documents: null }));
|
|
553
602
|
liveQueries.recover();
|
|
554
603
|
queueReadiness();
|
|
@@ -556,11 +605,11 @@ export function composeRuntime(options) {
|
|
|
556
605
|
}
|
|
557
606
|
}
|
|
558
607
|
function unprepared() {
|
|
559
|
-
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !
|
|
608
|
+
return [...unpreparedDemand, ...replicas.keys()].find((locator) => !indexed(locator) && !unpreparable.has(locator));
|
|
560
609
|
}
|
|
561
|
-
function
|
|
562
|
-
|
|
563
|
-
journal.record(`
|
|
610
|
+
function unpreparedBy(locator, error) {
|
|
611
|
+
unpreparable.add(locator);
|
|
612
|
+
journal.record(`preparation failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
564
613
|
queueReadiness();
|
|
565
614
|
}
|
|
566
615
|
function prepare(locators = []) {
|
|
@@ -576,7 +625,7 @@ export function composeRuntime(options) {
|
|
|
576
625
|
if (closed)
|
|
577
626
|
return;
|
|
578
627
|
unpreparedDemand.delete(waiting);
|
|
579
|
-
await prepareReplica(waiting).catch((error) =>
|
|
628
|
+
await prepareReplica(waiting).catch((error) => unpreparedBy(waiting, error));
|
|
580
629
|
}
|
|
581
630
|
}
|
|
582
631
|
finally {
|
|
@@ -585,7 +634,11 @@ export function composeRuntime(options) {
|
|
|
585
634
|
})();
|
|
586
635
|
}
|
|
587
636
|
function roomsOfReach(reach) {
|
|
588
|
-
return new Set([
|
|
637
|
+
return new Set([
|
|
638
|
+
...reach.rooms,
|
|
639
|
+
...reach.documents.map(roomOfKey),
|
|
640
|
+
...reach.areas.flatMap((area) => (area.types.length === 0 ? [] : [area.room])),
|
|
641
|
+
]);
|
|
589
642
|
}
|
|
590
643
|
function reachedRooms(plan) {
|
|
591
644
|
const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
|
|
@@ -617,7 +670,7 @@ export function composeRuntime(options) {
|
|
|
617
670
|
pending.push({ room: locator, awaits: 'acquisition' });
|
|
618
671
|
continue;
|
|
619
672
|
}
|
|
620
|
-
if (!
|
|
673
|
+
if (!replica.indexed) {
|
|
621
674
|
pending.push({ room: locator, awaits: 'preparation' });
|
|
622
675
|
continue;
|
|
623
676
|
}
|
|
@@ -650,7 +703,7 @@ export function composeRuntime(options) {
|
|
|
650
703
|
return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
|
|
651
704
|
return { listed: named.every((room) => replicas.has(room)), rooms: named };
|
|
652
705
|
}
|
|
653
|
-
function roomState(locator
|
|
706
|
+
function roomState(locator) {
|
|
654
707
|
if (sourceRefusals.has(locator))
|
|
655
708
|
return 'refused';
|
|
656
709
|
const listedRoom = discovered.get(locator);
|
|
@@ -661,27 +714,17 @@ export function composeRuntime(options) {
|
|
|
661
714
|
return 'unadmitted';
|
|
662
715
|
if (!replicas.has(locator))
|
|
663
716
|
return 'acquiring';
|
|
664
|
-
if (!
|
|
717
|
+
if (!indexed(locator))
|
|
665
718
|
return 'preparing';
|
|
666
|
-
if (search && !searchIndex.rooms().has(locator))
|
|
667
|
-
return 'indexing';
|
|
668
719
|
return 'complete';
|
|
669
720
|
}
|
|
670
|
-
function conversationCoverage(reach
|
|
721
|
+
function conversationCoverage(reach) {
|
|
671
722
|
const declared = reach === null || reach.conversations;
|
|
672
723
|
if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
|
|
673
724
|
return 'undeclared';
|
|
674
725
|
if (!conversations.known() || !conversations.current())
|
|
675
726
|
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';
|
|
727
|
+
return 'complete';
|
|
685
728
|
}
|
|
686
729
|
function connectionOf() {
|
|
687
730
|
if (socketOpen)
|
|
@@ -701,6 +744,7 @@ export function composeRuntime(options) {
|
|
|
701
744
|
return [
|
|
702
745
|
{
|
|
703
746
|
room,
|
|
747
|
+
name: discovered.get(room)?.name ?? room,
|
|
704
748
|
file: typeof file === 'string' ? file : '',
|
|
705
749
|
rule: held.code,
|
|
706
750
|
location: typeof location === 'string' ? location : null,
|
|
@@ -708,42 +752,95 @@ export function composeRuntime(options) {
|
|
|
708
752
|
];
|
|
709
753
|
});
|
|
710
754
|
}
|
|
755
|
+
function installationOf(coverage) {
|
|
756
|
+
const rooms = { done: 0, total: 0 };
|
|
757
|
+
const parts = { done: 0, total: 0 };
|
|
758
|
+
const bytes = { done: 0, total: 0 };
|
|
759
|
+
const received = { done: 0, total: 0 };
|
|
760
|
+
let installing = 0;
|
|
761
|
+
let unlisted = false;
|
|
762
|
+
let listed = false;
|
|
763
|
+
let waiting = false;
|
|
764
|
+
for (const { room, state } of coverage) {
|
|
765
|
+
if (state === 'unadmitted')
|
|
766
|
+
continue;
|
|
767
|
+
rooms.total += 1;
|
|
768
|
+
if (state === 'complete')
|
|
769
|
+
rooms.done += 1;
|
|
770
|
+
const transfer = transfers.get(room);
|
|
771
|
+
const replica = replicas.get(room)?.record.inventory ?? null;
|
|
772
|
+
const inventory = transfer?.inventory ?? replica ?? installs.get(room) ?? null;
|
|
773
|
+
const underway = state === 'acquiring' || (installs.has(room) && state !== 'complete');
|
|
774
|
+
if (underway)
|
|
775
|
+
installing += 1;
|
|
776
|
+
if (underway && inventory === null)
|
|
777
|
+
unlisted = true;
|
|
778
|
+
if (inventory !== null) {
|
|
779
|
+
listed = true;
|
|
780
|
+
const done = transfer?.done ?? (replica === null ? NOTHING_DONE : inventory);
|
|
781
|
+
parts.total += inventory.parts;
|
|
782
|
+
bytes.total += inventory.bytes;
|
|
783
|
+
parts.done += done.parts;
|
|
784
|
+
bytes.done += done.bytes;
|
|
785
|
+
received.total += inventory.bytes;
|
|
786
|
+
received.done += done.bytes;
|
|
787
|
+
for (const held of [keptParts.get(room), arrivals.get(room)]) {
|
|
788
|
+
for (const arrived of held?.values() ?? [])
|
|
789
|
+
received.done += arrived;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
waiting ||= underway;
|
|
793
|
+
}
|
|
794
|
+
const step = installing === 0
|
|
795
|
+
? null
|
|
796
|
+
: !listed
|
|
797
|
+
? 'listing'
|
|
798
|
+
: unlisted || received.done < received.total
|
|
799
|
+
? 'downloading'
|
|
800
|
+
: bytes.done < bytes.total
|
|
801
|
+
? 'writing'
|
|
802
|
+
: 'replaying';
|
|
803
|
+
return {
|
|
804
|
+
step,
|
|
805
|
+
paused: waiting && !onScreen ? 'hidden' : null,
|
|
806
|
+
rooms,
|
|
807
|
+
parts,
|
|
808
|
+
bytes,
|
|
809
|
+
received,
|
|
810
|
+
};
|
|
811
|
+
}
|
|
711
812
|
function readinessFor(port, reach) {
|
|
712
813
|
const required = requiredRooms(reach);
|
|
713
814
|
const search = port.search === 'required';
|
|
714
|
-
const coverage = required.rooms.map((room) => ({ room, state: roomState(room
|
|
715
|
-
const
|
|
716
|
-
.filter((room) => room.state ===
|
|
815
|
+
const coverage = required.rooms.map((room) => ({ room, state: roomState(room) }));
|
|
816
|
+
const preparing = coverage
|
|
817
|
+
.filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
|
|
717
818
|
.map(({ room }) => room);
|
|
718
|
-
const preparing = building('preparing');
|
|
719
819
|
if (preparing.length > 0)
|
|
720
820
|
prepare(preparing);
|
|
721
|
-
const indexing = building('indexing');
|
|
722
|
-
if (indexing.length > 0)
|
|
723
|
-
index(indexing, []);
|
|
724
821
|
return readinessOf({
|
|
725
822
|
code: port.code,
|
|
726
823
|
coverage: {
|
|
727
824
|
listed: required.listed,
|
|
728
825
|
rooms: coverage,
|
|
729
|
-
conversations: conversationCoverage(reach
|
|
826
|
+
conversations: conversationCoverage(reach),
|
|
730
827
|
search: search ? 'required' : 'unused',
|
|
731
828
|
},
|
|
732
829
|
persistence,
|
|
733
830
|
connection: connectionOf(),
|
|
734
831
|
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
? capacity
|
|
738
|
-
: port.capacity,
|
|
832
|
+
space: full ? 'full' : port.space,
|
|
833
|
+
installation: installationOf(coverage),
|
|
739
834
|
refused: refusedIn(required.rooms),
|
|
740
835
|
lost: storageLoss !== null,
|
|
741
|
-
established: targets[port.target]?.established === true
|
|
836
|
+
established: targets[port.target]?.established === true ||
|
|
837
|
+
(usableWhenHeld.includes(port.target) && required.rooms.some((room) => reopened.has(room))),
|
|
742
838
|
});
|
|
743
839
|
}
|
|
744
840
|
function establish(name) {
|
|
745
841
|
targets = { ...targets, [name]: { established: true } };
|
|
746
842
|
queueReadiness();
|
|
843
|
+
probeEachMinute();
|
|
747
844
|
void durably((local) => local.fact('targets', targets)).catch(() => {
|
|
748
845
|
journal.record(`target record failed ${name}`);
|
|
749
846
|
});
|
|
@@ -781,87 +878,52 @@ export function composeRuntime(options) {
|
|
|
781
878
|
readinessWatches.delete(watch);
|
|
782
879
|
};
|
|
783
880
|
}
|
|
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 ?? '-'}`);
|
|
881
|
+
function filled() {
|
|
882
|
+
if (full || closed)
|
|
883
|
+
return;
|
|
884
|
+
full = true;
|
|
885
|
+
journal.record('space full');
|
|
803
886
|
queueReadiness();
|
|
804
|
-
|
|
887
|
+
probeEachMinute();
|
|
888
|
+
}
|
|
889
|
+
function installedTarget() {
|
|
890
|
+
return ((usableWhenHeld.length > 0 && reopened.size > 0) ||
|
|
891
|
+
Object.values(targets).some((target) => target.established));
|
|
892
|
+
}
|
|
893
|
+
function probeEachMinute() {
|
|
894
|
+
if (!full || probing !== null || closed || !installedTarget())
|
|
805
895
|
return;
|
|
806
|
-
// A
|
|
807
|
-
|
|
808
|
-
void
|
|
896
|
+
// 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.
|
|
897
|
+
probing = setInterval(() => {
|
|
898
|
+
void probed();
|
|
809
899
|
}, 60_000);
|
|
810
900
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
resume();
|
|
901
|
+
function probed() {
|
|
902
|
+
testing ??= database.ask({ kind: 'space.probe' }).then(() => {
|
|
903
|
+
testing = null;
|
|
904
|
+
spaceFreed();
|
|
905
|
+
return true;
|
|
906
|
+
}, () => {
|
|
907
|
+
testing = null;
|
|
908
|
+
return false;
|
|
909
|
+
});
|
|
910
|
+
return testing;
|
|
822
911
|
}
|
|
823
|
-
function
|
|
824
|
-
if (
|
|
825
|
-
clearInterval(rechecking);
|
|
826
|
-
rechecking = null;
|
|
827
|
-
if (paused.size === 0 && capacity === null)
|
|
912
|
+
function spaceFreed() {
|
|
913
|
+
if (!full)
|
|
828
914
|
return;
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
915
|
+
full = false;
|
|
916
|
+
if (probing !== null)
|
|
917
|
+
clearInterval(probing);
|
|
918
|
+
probing = null;
|
|
919
|
+
unpreparable.clear();
|
|
920
|
+
journal.record('space freed');
|
|
832
921
|
queueReadiness();
|
|
833
922
|
if (!closed && authorization !== undefined)
|
|
834
923
|
void sync('unstreamed');
|
|
835
924
|
}
|
|
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
925
|
function seedRoomHeld(room, version) {
|
|
864
|
-
return
|
|
926
|
+
return indexed(room) && reachedBy(room, version);
|
|
865
927
|
}
|
|
866
928
|
function seedHeld(at) {
|
|
867
929
|
return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
|
|
@@ -982,83 +1044,12 @@ export function composeRuntime(options) {
|
|
|
982
1044
|
const held = answered.cover;
|
|
983
1045
|
return { ...answered.rows, coverage: held.coverage };
|
|
984
1046
|
}
|
|
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
1047
|
function publishChangeSet(set) {
|
|
1061
1048
|
applyChangeSet(set);
|
|
1049
|
+
for (const { locator, path } of set.documents) {
|
|
1050
|
+
if (path !== null)
|
|
1051
|
+
attachedFiles.get(locator)?.delete(path);
|
|
1052
|
+
}
|
|
1062
1053
|
if (set.rooms.length > 0 ||
|
|
1063
1054
|
[...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
|
|
1064
1055
|
emitRooms();
|
|
@@ -1084,27 +1075,24 @@ export function composeRuntime(options) {
|
|
|
1084
1075
|
const records = [...replicas].map(([locator, held]) => ({
|
|
1085
1076
|
locator,
|
|
1086
1077
|
held,
|
|
1087
|
-
record: { ...held.record, model: revoked
|
|
1078
|
+
record: { ...held.record, model: revoked },
|
|
1088
1079
|
}));
|
|
1080
|
+
let answer = null;
|
|
1089
1081
|
try {
|
|
1090
1082
|
const local = await openLocal();
|
|
1091
|
-
await local.commit({
|
|
1083
|
+
answer = await local.commit({
|
|
1092
1084
|
rooms: records.map(({ locator, record }) => ({
|
|
1093
1085
|
locator,
|
|
1094
1086
|
key: record.key,
|
|
1095
1087
|
record: storedRecord(record),
|
|
1096
|
-
|
|
1088
|
+
composed: composedOf(composed(record)),
|
|
1097
1089
|
})),
|
|
1098
1090
|
});
|
|
1099
1091
|
}
|
|
1100
1092
|
catch {
|
|
1101
1093
|
journal.record('model revocation persistence failed');
|
|
1102
1094
|
}
|
|
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
|
-
}
|
|
1095
|
+
const keys = new Set([...models.keys(), ...rooms.keys()]);
|
|
1108
1096
|
for (const key of keys) {
|
|
1109
1097
|
invalidModels.add(key);
|
|
1110
1098
|
models.set(key, revoked);
|
|
@@ -1114,8 +1102,8 @@ export function composeRuntime(options) {
|
|
|
1114
1102
|
for (const { locator, held, record } of records) {
|
|
1115
1103
|
if (replicas.get(locator) !== held)
|
|
1116
1104
|
continue;
|
|
1117
|
-
|
|
1118
|
-
replicas.set(locator, { ...held, record });
|
|
1105
|
+
const indexed = answer === null ? held.indexed : committedRoom(locator, answer).indexed;
|
|
1106
|
+
replicas.set(locator, { ...held, record, indexed });
|
|
1119
1107
|
}
|
|
1120
1108
|
for (const locator of [...rooms.keys()]) {
|
|
1121
1109
|
const room = rooms.get(locator);
|
|
@@ -1159,7 +1147,7 @@ export function composeRuntime(options) {
|
|
|
1159
1147
|
? null
|
|
1160
1148
|
: {
|
|
1161
1149
|
roots: selection.roots,
|
|
1162
|
-
model: modelTypes(
|
|
1150
|
+
model: modelTypes(builtIn),
|
|
1163
1151
|
},
|
|
1164
1152
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
1165
1153
|
roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
|
|
@@ -1248,30 +1236,25 @@ export function composeRuntime(options) {
|
|
|
1248
1236
|
queuesWithoutReplica.delete(locator);
|
|
1249
1237
|
rooms.delete(locator);
|
|
1250
1238
|
roomDrafts.delete(locator);
|
|
1251
|
-
|
|
1239
|
+
attachedFiles.delete(locator);
|
|
1240
|
+
untransferred(locator);
|
|
1252
1241
|
catching.delete(locator);
|
|
1253
1242
|
discovered.delete(locator);
|
|
1254
1243
|
announced.delete(locator);
|
|
1255
1244
|
unreadable.delete(locator);
|
|
1256
1245
|
stale.delete(locator);
|
|
1257
1246
|
repairable.delete(locator);
|
|
1258
|
-
|
|
1259
|
-
paused.delete(locator);
|
|
1260
|
-
prepared.delete(locator);
|
|
1247
|
+
unpreparable.delete(locator);
|
|
1261
1248
|
scopedRefusals.delete(locator);
|
|
1262
1249
|
sourceRefusals.delete(locator);
|
|
1263
1250
|
declarationRefusals.delete(locator);
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
liveModels.delete(key);
|
|
1268
|
-
invalidModels.delete(key);
|
|
1269
|
-
}
|
|
1251
|
+
models.delete(locator);
|
|
1252
|
+
liveModels.delete(locator);
|
|
1253
|
+
invalidModels.delete(locator);
|
|
1270
1254
|
journal.record(`room lost ${locator}`);
|
|
1271
1255
|
}
|
|
1272
1256
|
if (fresh.length === 0)
|
|
1273
1257
|
return fresh;
|
|
1274
|
-
searchIndex.forget(fresh);
|
|
1275
1258
|
publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, { documents: null, room: true, queue: true, drafts }))));
|
|
1276
1259
|
liveQueries.recover();
|
|
1277
1260
|
moveTo({ ...status, ...attention() });
|
|
@@ -1315,7 +1298,14 @@ export function composeRuntime(options) {
|
|
|
1315
1298
|
});
|
|
1316
1299
|
await eraseFenced(fresh);
|
|
1317
1300
|
}
|
|
1318
|
-
function
|
|
1301
|
+
function retyped(persisted) {
|
|
1302
|
+
if (persisted.status === 'unavailable')
|
|
1303
|
+
return persisted;
|
|
1304
|
+
const { state } = attachedModel({ kind: 'model', snapshot: persisted.snapshot }, NO_MODEL, roomTypes);
|
|
1305
|
+
return state.status === 'unavailable' ? state : { ...persisted, types: state.types };
|
|
1306
|
+
}
|
|
1307
|
+
function restored(key, held) {
|
|
1308
|
+
const persisted = retyped(held);
|
|
1319
1309
|
const prior = models.get(key);
|
|
1320
1310
|
if (prior !== undefined &&
|
|
1321
1311
|
prior.status !== 'unavailable' &&
|
|
@@ -1346,8 +1336,16 @@ export function composeRuntime(options) {
|
|
|
1346
1336
|
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
1347
1337
|
targets = held.targets;
|
|
1348
1338
|
transfers.clear();
|
|
1349
|
-
for (const [locator, record] of held.transfers)
|
|
1350
|
-
transfers.set(locator,
|
|
1339
|
+
for (const [locator, record] of held.transfers) {
|
|
1340
|
+
transfers.set(locator, {
|
|
1341
|
+
...record,
|
|
1342
|
+
models: { collection: retyped(record.models.collection) },
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
arrivals.clear();
|
|
1346
|
+
keptParts.clear();
|
|
1347
|
+
for (const [locator, parts] of held.kept)
|
|
1348
|
+
keptParts.set(locator, new Map(parts));
|
|
1351
1349
|
limits = held.providerLimits;
|
|
1352
1350
|
capabilities = held.capabilities;
|
|
1353
1351
|
member = held.member;
|
|
@@ -1365,7 +1363,7 @@ export function composeRuntime(options) {
|
|
|
1365
1363
|
}
|
|
1366
1364
|
}
|
|
1367
1365
|
replicas.clear();
|
|
1368
|
-
|
|
1366
|
+
reopened.clear();
|
|
1369
1367
|
queuesWithoutReplica.clear();
|
|
1370
1368
|
for (const [locator, queue] of queuedBatches) {
|
|
1371
1369
|
if (!held.rooms.has(locator))
|
|
@@ -1374,18 +1372,16 @@ export function composeRuntime(options) {
|
|
|
1374
1372
|
records.forEach((stored, index) => {
|
|
1375
1373
|
if (fences.has(stored.locator))
|
|
1376
1374
|
return;
|
|
1377
|
-
const
|
|
1378
|
-
|
|
1379
|
-
const record = { ...stored, model, selectionModel };
|
|
1375
|
+
const record = { ...stored, model: restored(stored.locator, stored.model) };
|
|
1376
|
+
reopened.add(record.locator);
|
|
1380
1377
|
replicas.set(record.locator, {
|
|
1381
1378
|
record,
|
|
1382
1379
|
queue: queuedBatches.get(record.locator) ?? [],
|
|
1383
1380
|
overlaid: held.overlaid.get(record.key) ?? new Set(),
|
|
1384
1381
|
count: counts[index] ?? 0,
|
|
1385
1382
|
confirmed: 0,
|
|
1383
|
+
indexed: !held.unindexed.has(record.key),
|
|
1386
1384
|
});
|
|
1387
|
-
if (!held.unprepared.has(record.key))
|
|
1388
|
-
prepared.add(record.locator);
|
|
1389
1385
|
const existing = rooms.get(record.locator);
|
|
1390
1386
|
if (existing === undefined)
|
|
1391
1387
|
list(record, false);
|
|
@@ -1395,13 +1391,10 @@ export function composeRuntime(options) {
|
|
|
1395
1391
|
roomVersion: record.roomVersion,
|
|
1396
1392
|
operations: record.operations,
|
|
1397
1393
|
replica: record.replica,
|
|
1398
|
-
model: modelTypes(model),
|
|
1394
|
+
model: modelTypes(record.model),
|
|
1399
1395
|
typedSelection: record.typedSelection === null
|
|
1400
1396
|
? null
|
|
1401
|
-
: {
|
|
1402
|
-
roots: record.typedSelection.roots,
|
|
1403
|
-
model: modelTypes(selectionModel),
|
|
1404
|
-
},
|
|
1397
|
+
: { roots: record.typedSelection.roots, model: modelTypes(builtIn) },
|
|
1405
1398
|
});
|
|
1406
1399
|
}
|
|
1407
1400
|
});
|
|
@@ -1431,8 +1424,7 @@ export function composeRuntime(options) {
|
|
|
1431
1424
|
async function open() {
|
|
1432
1425
|
if (closed)
|
|
1433
1426
|
throw new Error('runtime closed');
|
|
1434
|
-
const opened =
|
|
1435
|
-
store = opened;
|
|
1427
|
+
const opened = openRuntimeStore(isFenced, database);
|
|
1436
1428
|
const interrupted = await opened.fences();
|
|
1437
1429
|
for (const locator of interrupted) {
|
|
1438
1430
|
fences.set(locator, { at: discoveries, erased: false });
|
|
@@ -1478,10 +1470,29 @@ export function composeRuntime(options) {
|
|
|
1478
1470
|
return opening;
|
|
1479
1471
|
}
|
|
1480
1472
|
function exclusive(work) {
|
|
1473
|
+
gathering = null;
|
|
1481
1474
|
const next = mutations.then(work, work);
|
|
1482
1475
|
mutations = next.then(() => undefined, () => undefined);
|
|
1483
1476
|
return next;
|
|
1484
1477
|
}
|
|
1478
|
+
function gathered(key, input, merge, work) {
|
|
1479
|
+
const tail = gathering;
|
|
1480
|
+
if (tail !== null && tail.key === key) {
|
|
1481
|
+
const merged = merge(tail.input, input);
|
|
1482
|
+
if (merged !== null) {
|
|
1483
|
+
tail.input = merged;
|
|
1484
|
+
return tail.done;
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
const entry = { key, input, done: Promise.resolve() };
|
|
1488
|
+
entry.done = exclusive(() => {
|
|
1489
|
+
if (gathering === entry)
|
|
1490
|
+
gathering = null;
|
|
1491
|
+
return work(entry.input);
|
|
1492
|
+
});
|
|
1493
|
+
gathering = entry;
|
|
1494
|
+
return entry.done;
|
|
1495
|
+
}
|
|
1485
1496
|
function withdrawable(locator) {
|
|
1486
1497
|
return (batch) => {
|
|
1487
1498
|
const state = pendings.get(pendingKey(locator, batch.id));
|
|
@@ -1563,26 +1574,14 @@ export function composeRuntime(options) {
|
|
|
1563
1574
|
},
|
|
1564
1575
|
};
|
|
1565
1576
|
}
|
|
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))
|
|
1577
|
+
function committedRoom(locator, answer) {
|
|
1578
|
+
const indexed = answer.indexed[locator] ?? replicas.get(locator)?.indexed ?? false;
|
|
1579
|
+
if (!indexed)
|
|
1581
1580
|
prepare([locator]);
|
|
1582
|
-
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0 };
|
|
1581
|
+
return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0, indexed };
|
|
1583
1582
|
}
|
|
1584
1583
|
function replayed(record, confirmedLayer, queue) {
|
|
1585
|
-
return stepped(replaying(confirmedLayer, queue, record.locator,
|
|
1584
|
+
return stepped(replaying(confirmedLayer, queue, record.locator, composed(record)));
|
|
1586
1585
|
}
|
|
1587
1586
|
async function overlayDiff(record, before, visible, touched) {
|
|
1588
1587
|
const kept = before !== undefined && before.record.key === record.key
|
|
@@ -1645,7 +1644,7 @@ export function composeRuntime(options) {
|
|
|
1645
1644
|
const confirmedLayer = overrides === undefined
|
|
1646
1645
|
? read
|
|
1647
1646
|
: await stepped(slicingWith(read, overrides.set, overrides.removed));
|
|
1648
|
-
const composition =
|
|
1647
|
+
const composition = composed(record);
|
|
1649
1648
|
const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
|
|
1650
1649
|
const founded = kept.filter((batch) => !queue.includes(batch));
|
|
1651
1650
|
const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
|
|
@@ -1688,6 +1687,7 @@ export function composeRuntime(options) {
|
|
|
1688
1687
|
key: record.key,
|
|
1689
1688
|
record: storedRecord(record),
|
|
1690
1689
|
roots: record.typedSelection?.roots ?? [],
|
|
1690
|
+
composed: composedOf(composition),
|
|
1691
1691
|
...settling.commit,
|
|
1692
1692
|
...(overrides === undefined
|
|
1693
1693
|
? {}
|
|
@@ -1729,14 +1729,14 @@ export function composeRuntime(options) {
|
|
|
1729
1729
|
const confirmedChanged = settling.commit?.published !== undefined ||
|
|
1730
1730
|
settling.commit?.refreshed === true ||
|
|
1731
1731
|
overrides !== undefined;
|
|
1732
|
-
|
|
1733
|
-
const { count } = committedRoom(locator, record, answer);
|
|
1732
|
+
const { count, indexed } = committedRoom(locator, answer);
|
|
1734
1733
|
replicas.set(locator, {
|
|
1735
1734
|
record,
|
|
1736
1735
|
queue: kept,
|
|
1737
1736
|
overlaid: touched,
|
|
1738
1737
|
count,
|
|
1739
1738
|
confirmed: (before?.confirmed ?? 0) + (confirmedChanged ? 1 : 0),
|
|
1739
|
+
indexed,
|
|
1740
1740
|
});
|
|
1741
1741
|
return mergeChangeSets([
|
|
1742
1742
|
changeSet(locator, {
|
|
@@ -1751,38 +1751,28 @@ export function composeRuntime(options) {
|
|
|
1751
1751
|
};
|
|
1752
1752
|
}
|
|
1753
1753
|
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
|
-
}
|
|
1754
|
+
const model = record.model;
|
|
1755
|
+
models.set(locator, model);
|
|
1756
|
+
invalidModels.delete(locator);
|
|
1757
|
+
if (model.status === 'current')
|
|
1758
|
+
liveModels.set(locator, JSON.stringify(model.snapshot));
|
|
1759
|
+
else if (model.status === 'unavailable')
|
|
1760
|
+
liveModels.delete(locator);
|
|
1766
1761
|
}
|
|
1767
1762
|
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
|
-
};
|
|
1763
|
+
const answer = answers?.collection;
|
|
1764
|
+
if (answer === undefined)
|
|
1765
|
+
return held;
|
|
1766
|
+
const { state, refused } = attachedModel(answer, held.collection, roomTypes);
|
|
1767
|
+
if (refused !== null) {
|
|
1768
|
+
const where = refused.location === undefined ? '' : ` ${refused.location}`;
|
|
1769
|
+
journal.record(`model invalid ${locator} collection ${refused.identity} ${refused.rule}${where}`);
|
|
1770
|
+
}
|
|
1771
|
+
models.set(locator, state);
|
|
1772
|
+
return { collection: state };
|
|
1783
1773
|
}
|
|
1784
1774
|
function publish(local, room, record, landing, live) {
|
|
1785
|
-
|
|
1775
|
+
const landed = exclusive(async () => {
|
|
1786
1776
|
if (!live()) {
|
|
1787
1777
|
caughtUpTo(room.locator);
|
|
1788
1778
|
return true;
|
|
@@ -1806,34 +1796,32 @@ export function composeRuntime(options) {
|
|
|
1806
1796
|
? {
|
|
1807
1797
|
room: true,
|
|
1808
1798
|
view: 'refreshed',
|
|
1809
|
-
commit: { refreshed: true
|
|
1799
|
+
commit: { refreshed: true },
|
|
1810
1800
|
}
|
|
1811
1801
|
: {
|
|
1812
1802
|
room: true,
|
|
1813
1803
|
view: landing.refreshed ? 'refreshed' : 'confirmed',
|
|
1814
1804
|
commit: {
|
|
1815
1805
|
published: { from: prior?.record.key ?? null },
|
|
1816
|
-
composition: compositionOf(record),
|
|
1817
1806
|
...(landing.refreshed ? { refreshed: true } : {}),
|
|
1818
1807
|
},
|
|
1819
1808
|
});
|
|
1820
1809
|
}
|
|
1821
1810
|
catch (error) {
|
|
1822
|
-
if (!
|
|
1811
|
+
if (!refusedForSpace(error))
|
|
1823
1812
|
throw error;
|
|
1824
|
-
await exceededOn(room.locator, null);
|
|
1825
1813
|
caughtUpTo(room.locator);
|
|
1826
1814
|
return true;
|
|
1827
1815
|
}
|
|
1828
1816
|
if (landing.kind === 'bootstrapped') {
|
|
1829
|
-
|
|
1817
|
+
untransferred(room.locator);
|
|
1830
1818
|
catching.delete(room.locator);
|
|
1831
1819
|
sweep();
|
|
1832
1820
|
}
|
|
1833
1821
|
for (const batch of accepted)
|
|
1834
1822
|
pendings.delete(pendingKey(room.locator, batch.id));
|
|
1835
1823
|
stale.delete(room.locator);
|
|
1836
|
-
|
|
1824
|
+
unpreparable.delete(room.locator);
|
|
1837
1825
|
if (unreadable.delete(room.locator))
|
|
1838
1826
|
scopedRefusals.delete(room.locator);
|
|
1839
1827
|
sourceRefusals.delete(room.locator);
|
|
@@ -1848,6 +1836,7 @@ export function composeRuntime(options) {
|
|
|
1848
1836
|
}
|
|
1849
1837
|
return true;
|
|
1850
1838
|
});
|
|
1839
|
+
return landing.kind === 'refreshed' ? landed : landed.finally(() => installed(room.locator));
|
|
1851
1840
|
}
|
|
1852
1841
|
async function savedRoom(locator) {
|
|
1853
1842
|
const local = await openLocal();
|
|
@@ -1921,13 +1910,14 @@ export function composeRuntime(options) {
|
|
|
1921
1910
|
const referring = removed.length === 0
|
|
1922
1911
|
? []
|
|
1923
1912
|
: (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
|
|
1924
|
-
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...referring], moved);
|
|
1913
|
+
const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
|
|
1925
1914
|
const { visible } = await replayed(record, confirmedLayer, replica.queue);
|
|
1926
|
-
const composition =
|
|
1915
|
+
const composition = composed(record);
|
|
1927
1916
|
return {
|
|
1928
1917
|
record,
|
|
1929
1918
|
confirmed: confirmedLayer,
|
|
1930
1919
|
visible,
|
|
1920
|
+
folders: new Set(await local.folders(record.key, 'visible', paths)),
|
|
1931
1921
|
*visibleOver(queue) {
|
|
1932
1922
|
return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
|
|
1933
1923
|
},
|
|
@@ -1939,13 +1929,7 @@ export function composeRuntime(options) {
|
|
|
1939
1929
|
return false;
|
|
1940
1930
|
const local = await openLocal();
|
|
1941
1931
|
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)));
|
|
1932
|
+
return local.typedWithin(record.key, 'visible', roots, false);
|
|
1949
1933
|
}
|
|
1950
1934
|
function textDiffers(left, right) {
|
|
1951
1935
|
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
@@ -2050,7 +2034,7 @@ export function composeRuntime(options) {
|
|
|
2050
2034
|
const lookup = overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides);
|
|
2051
2035
|
const clean = available(composition) &&
|
|
2052
2036
|
changed.length <= CLEAN_CHANGES &&
|
|
2053
|
-
(await changedCleanly(changed,
|
|
2037
|
+
(await changedCleanly(changed, composed(record), composition, {
|
|
2054
2038
|
lookup,
|
|
2055
2039
|
texts: textsOf(local, record.key, 'confirmed'),
|
|
2056
2040
|
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
@@ -2081,6 +2065,7 @@ export function composeRuntime(options) {
|
|
|
2081
2065
|
operations: room.operations,
|
|
2082
2066
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
2083
2067
|
visible: saving.visible,
|
|
2068
|
+
folders: saving.folders,
|
|
2084
2069
|
queued: queuedDocuments(replica.queue),
|
|
2085
2070
|
unmodeled: await unmodeledIn(record),
|
|
2086
2071
|
validate: validatorOf(record, saving.confirmed),
|
|
@@ -2101,6 +2086,7 @@ export function composeRuntime(options) {
|
|
|
2101
2086
|
locator,
|
|
2102
2087
|
key: saving.record.key,
|
|
2103
2088
|
roots: saving.record.typedSelection?.roots ?? [],
|
|
2089
|
+
composed: composedOf(composed(saving.record)),
|
|
2104
2090
|
overlay: diff.overlay,
|
|
2105
2091
|
});
|
|
2106
2092
|
}
|
|
@@ -2244,6 +2230,8 @@ export function composeRuntime(options) {
|
|
|
2244
2230
|
submitLater(locator);
|
|
2245
2231
|
}
|
|
2246
2232
|
async function committed(locator, target, saved, consumed, writtenBy, files = []) {
|
|
2233
|
+
if (full)
|
|
2234
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
2247
2235
|
const into = movedInto(locator, saved);
|
|
2248
2236
|
if (into !== null) {
|
|
2249
2237
|
return committedMove(locator, into, target, saved, consumed, writtenBy, files);
|
|
@@ -2283,6 +2271,7 @@ export function composeRuntime(options) {
|
|
|
2283
2271
|
providerLimits: limits,
|
|
2284
2272
|
handle: room.handle,
|
|
2285
2273
|
visible: saving.visible,
|
|
2274
|
+
folders: saving.folders,
|
|
2286
2275
|
changes,
|
|
2287
2276
|
bases: parts.flatMap(({ bases }) => bases),
|
|
2288
2277
|
queued: queuedDocuments(replica.queue),
|
|
@@ -2433,6 +2422,9 @@ export function composeRuntime(options) {
|
|
|
2433
2422
|
async function readHistory(locator, path, query = {}) {
|
|
2434
2423
|
if (closed || refusal !== null)
|
|
2435
2424
|
return UNCERTAIN;
|
|
2425
|
+
await firstDiscovery;
|
|
2426
|
+
if (!roomsNamed)
|
|
2427
|
+
return UNCERTAIN;
|
|
2436
2428
|
try {
|
|
2437
2429
|
await ensureAuthorization();
|
|
2438
2430
|
}
|
|
@@ -2522,14 +2514,12 @@ export function composeRuntime(options) {
|
|
|
2522
2514
|
return (await openLocal()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
2523
2515
|
};
|
|
2524
2516
|
}
|
|
2525
|
-
function binaryRoom(request) {
|
|
2526
|
-
if (request.kind === 'path')
|
|
2527
|
-
return request.locator;
|
|
2528
|
-
return options.adapter.referrerScope(request.referrer)?.locator ?? null;
|
|
2529
|
-
}
|
|
2530
2517
|
async function transferBinary(request, transfer, answer) {
|
|
2531
2518
|
if (closed || refusal !== null)
|
|
2532
2519
|
return UNCERTAIN;
|
|
2520
|
+
await firstDiscovery;
|
|
2521
|
+
if (!roomsNamed)
|
|
2522
|
+
return UNCERTAIN;
|
|
2533
2523
|
try {
|
|
2534
2524
|
await ensureAuthorization();
|
|
2535
2525
|
}
|
|
@@ -2539,16 +2529,13 @@ export function composeRuntime(options) {
|
|
|
2539
2529
|
if (closed || authorization === undefined || refusal !== null)
|
|
2540
2530
|
return UNCERTAIN;
|
|
2541
2531
|
const since = fencings;
|
|
2542
|
-
journal.record(request.
|
|
2543
|
-
? `transfer binary ${request.locator} ${request.path}`
|
|
2544
|
-
: `transfer binary ${request.fileRef}`);
|
|
2532
|
+
journal.record(`transfer binary ${request.locator} ${request.path}`);
|
|
2545
2533
|
try {
|
|
2546
2534
|
const result = await transfer(authorization);
|
|
2547
2535
|
const settled = result;
|
|
2548
2536
|
journal.record(`outcome binary ${settled.kind}`);
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
return failure({ kind: 'room-not-readable', locator });
|
|
2537
|
+
if (fencedSince(request.locator, since))
|
|
2538
|
+
return failure({ kind: 'room-not-readable', locator: request.locator });
|
|
2552
2539
|
if (settled.kind === 'scoped-refusal') {
|
|
2553
2540
|
const room = replicas.get(settled.locator)?.record;
|
|
2554
2541
|
if (room !== undefined)
|
|
@@ -2563,23 +2550,27 @@ export function composeRuntime(options) {
|
|
|
2563
2550
|
return UNCERTAIN;
|
|
2564
2551
|
}
|
|
2565
2552
|
}
|
|
2553
|
+
function fetchBinary(request, name) {
|
|
2554
|
+
return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
|
|
2555
|
+
}
|
|
2566
2556
|
async function readBinary(request, name) {
|
|
2567
2557
|
if (closed)
|
|
2568
2558
|
return UNCERTAIN;
|
|
2569
2559
|
const held = await keptFiles.find(name);
|
|
2570
2560
|
if (held !== null)
|
|
2571
2561
|
return held;
|
|
2572
|
-
return
|
|
2562
|
+
return fetchBinary(request, name);
|
|
2573
2563
|
}
|
|
2574
2564
|
async function binaryPath(locator, path, revision) {
|
|
2575
2565
|
if (revision !== undefined)
|
|
2576
2566
|
return { kind: 'path', locator, path, revision };
|
|
2577
2567
|
const replica = replicas.get(locator);
|
|
2578
|
-
if (replica === undefined)
|
|
2579
|
-
return null;
|
|
2580
2568
|
const local = await openLocal();
|
|
2581
|
-
const [held] = await local.entries(replica.record.key, 'visible', [path]);
|
|
2582
|
-
|
|
2569
|
+
const [held] = replica === undefined ? [] : await local.entries(replica.record.key, 'visible', [path]);
|
|
2570
|
+
const attached = held === undefined ? attachedFiles.get(locator)?.get(path) : undefined;
|
|
2571
|
+
if (attached !== undefined)
|
|
2572
|
+
return { kind: 'path', locator, path, revision: attached.revision };
|
|
2573
|
+
if (replica === undefined || held?.binary === undefined || held.binary === null)
|
|
2583
2574
|
return null;
|
|
2584
2575
|
if (held.binary.state === 'pending')
|
|
2585
2576
|
return { kind: 'pending', localId: held.binary.localId };
|
|
@@ -2595,7 +2586,7 @@ export function composeRuntime(options) {
|
|
|
2595
2586
|
const holds = (at) => {
|
|
2596
2587
|
const kept = confirmedLayer.binaries[at];
|
|
2597
2588
|
return (kept?.state === 'accepted' &&
|
|
2598
|
-
kept.
|
|
2589
|
+
kept.sha256 === record.sha256 &&
|
|
2599
2590
|
confirmedLayer.revisions[at] === record.revision);
|
|
2600
2591
|
};
|
|
2601
2592
|
let confirmedAt = path;
|
|
@@ -2609,49 +2600,91 @@ export function composeRuntime(options) {
|
|
|
2609
2600
|
locator,
|
|
2610
2601
|
path: confirmedAt,
|
|
2611
2602
|
revision: record.revision,
|
|
2612
|
-
fileRef: record.fileRef,
|
|
2613
2603
|
};
|
|
2614
2604
|
}
|
|
2615
|
-
async function
|
|
2605
|
+
async function askedBinary(locator, path, revision) {
|
|
2616
2606
|
await ready;
|
|
2617
2607
|
const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
|
|
2618
|
-
|
|
2608
|
+
return drafted !== undefined && drafted.localId === revision
|
|
2619
2609
|
? { kind: 'pending', localId: revision }
|
|
2620
|
-
:
|
|
2610
|
+
: binaryPath(locator, path, revision);
|
|
2611
|
+
}
|
|
2612
|
+
function pendingBinary(locator, path, localId) {
|
|
2613
|
+
const facts = queuedFacts(locator, path, localId);
|
|
2614
|
+
return facts === null
|
|
2615
|
+
? absentBinary(path)
|
|
2616
|
+
: {
|
|
2617
|
+
kind: 'binary',
|
|
2618
|
+
file: localId,
|
|
2619
|
+
byteLength: facts.byteLength,
|
|
2620
|
+
mediaType: facts.mediaType,
|
|
2621
|
+
};
|
|
2622
|
+
}
|
|
2623
|
+
async function readBinaryPath(locator, path, revision) {
|
|
2624
|
+
const asked = await askedBinary(locator, path, revision);
|
|
2621
2625
|
if (asked === null)
|
|
2622
2626
|
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
|
-
}
|
|
2627
|
+
if (asked.kind === 'pending')
|
|
2628
|
+
return pendingBinary(locator, path, asked.localId);
|
|
2634
2629
|
return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
|
|
2635
2630
|
}
|
|
2631
|
+
async function heldMediaType(locator, path) {
|
|
2632
|
+
const replica = replicas.get(locator);
|
|
2633
|
+
const [held] = replica === undefined
|
|
2634
|
+
? []
|
|
2635
|
+
: await (await openLocal()).entries(replica.record.key, 'visible', [path]);
|
|
2636
|
+
return held === undefined
|
|
2637
|
+
? attachedFiles.get(locator)?.get(path)?.mediaType
|
|
2638
|
+
: held.binary?.mediaType;
|
|
2639
|
+
}
|
|
2640
|
+
function linkOf(asked, held) {
|
|
2641
|
+
return transferBinary(asked, (authorization) => options.adapter.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
|
|
2642
|
+
}
|
|
2636
2643
|
async function linkBinaryPath(locator, path, revision) {
|
|
2637
2644
|
await ready;
|
|
2638
2645
|
const asked = await binaryPath(locator, path, revision);
|
|
2639
2646
|
if (asked === null || asked.kind === 'pending')
|
|
2640
2647
|
return absentBinary(path);
|
|
2641
|
-
return
|
|
2648
|
+
return linkOf(asked, revision === undefined ? await heldMediaType(locator, path) : undefined);
|
|
2642
2649
|
}
|
|
2643
|
-
async function
|
|
2650
|
+
async function showBinaryPath(locator, path, revision) {
|
|
2651
|
+
const asked = await askedBinary(locator, path, revision);
|
|
2652
|
+
if (asked === null)
|
|
2653
|
+
return absentBinary(path);
|
|
2654
|
+
if (asked.kind === 'pending')
|
|
2655
|
+
return pendingBinary(locator, path, asked.localId);
|
|
2656
|
+
if (closed)
|
|
2657
|
+
return UNCERTAIN;
|
|
2658
|
+
const name = { locator, path: asked.path, revision: asked.revision };
|
|
2659
|
+
const kept = await keptFiles.find(name);
|
|
2660
|
+
if (kept !== null)
|
|
2661
|
+
return kept;
|
|
2662
|
+
const mediaType = await heldMediaType(locator, path);
|
|
2663
|
+
if (mediaType?.startsWith('image/') === true)
|
|
2664
|
+
return fetchBinary(asked, name);
|
|
2665
|
+
return linkOf(asked, revision === undefined ? mediaType : undefined);
|
|
2666
|
+
}
|
|
2667
|
+
async function attachBinary(locator, file) {
|
|
2644
2668
|
await ready;
|
|
2645
|
-
|
|
2646
|
-
if (
|
|
2647
|
-
return
|
|
2648
|
-
|
|
2669
|
+
await firstDiscovery;
|
|
2670
|
+
if (closed || !roomsNamed || connectionOf() === 'unreachable' || refusal !== null)
|
|
2671
|
+
return OFFLINE;
|
|
2672
|
+
const request = await attachedFile(locator, file);
|
|
2673
|
+
return transferBinary({ locator, path: file.name }, async (held) => {
|
|
2674
|
+
const result = await options.adapter.attach(held, request);
|
|
2675
|
+
if (result.kind === 'attached') {
|
|
2676
|
+
const landed = attachedFiles.get(locator) ?? new Map();
|
|
2677
|
+
landed.set(result.path, { revision: result.revision, mediaType: request.mediaType });
|
|
2678
|
+
attachedFiles.set(locator, landed);
|
|
2679
|
+
}
|
|
2680
|
+
return result;
|
|
2681
|
+
}, attachAnswer);
|
|
2649
2682
|
}
|
|
2650
2683
|
function readPart(file, part) {
|
|
2651
|
-
return
|
|
2684
|
+
return database.ask({ kind: 'file.part', key: file, part });
|
|
2652
2685
|
}
|
|
2653
2686
|
async function cleanUp() {
|
|
2654
|
-
const freed = await
|
|
2687
|
+
const freed = await database.ask({ kind: 'opened.clean' });
|
|
2655
2688
|
return freed + ((await options.shell.cleanUp?.()) ?? 0);
|
|
2656
2689
|
}
|
|
2657
2690
|
function declaredAgents() {
|
|
@@ -2821,8 +2854,8 @@ export function composeRuntime(options) {
|
|
|
2821
2854
|
}
|
|
2822
2855
|
}
|
|
2823
2856
|
async function validatedRoom(local, record, view, previous, overrides = null) {
|
|
2824
|
-
const composition =
|
|
2825
|
-
const modeled = record.model.status !== 'unavailable' || record.
|
|
2857
|
+
const composition = composed(record);
|
|
2858
|
+
const modeled = record.model.status !== 'unavailable' || record.typedSelection !== null;
|
|
2826
2859
|
const lookup = overrides === null
|
|
2827
2860
|
? lookupOf(local, record.key, view)
|
|
2828
2861
|
: overriddenLookup(lookupOf(local, record.key, view), overrides);
|
|
@@ -2856,11 +2889,11 @@ export function composeRuntime(options) {
|
|
|
2856
2889
|
}
|
|
2857
2890
|
const named = changes === null ? null : changes.map(({ path }) => path);
|
|
2858
2891
|
const textChanged = changes === null || changes.length > 0 || reshaped;
|
|
2859
|
-
const modelsChanged = previous === null || !sameComposition(
|
|
2892
|
+
const modelsChanged = previous === null || !sameComposition(composed(previous.record), composition);
|
|
2860
2893
|
if (modeled && (textChanged || modelsChanged)) {
|
|
2861
2894
|
const clean = previous !== null &&
|
|
2862
2895
|
changes !== null &&
|
|
2863
|
-
(await changedCleanly(changes,
|
|
2896
|
+
(await changedCleanly(changes, composed(previous.record), composition, {
|
|
2864
2897
|
lookup,
|
|
2865
2898
|
texts,
|
|
2866
2899
|
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
@@ -2878,41 +2911,117 @@ export function composeRuntime(options) {
|
|
|
2878
2911
|
return unreadableRoom(texts, walk, named);
|
|
2879
2912
|
return null;
|
|
2880
2913
|
}
|
|
2881
|
-
function refreshingOf(local, key, liveness) {
|
|
2914
|
+
function refreshingOf(local, key, composition, liveness, replaying = null) {
|
|
2915
|
+
const write = replaying === null ? exclusive : installWrite;
|
|
2882
2916
|
return {
|
|
2917
|
+
async turn() {
|
|
2918
|
+
if (replaying === null)
|
|
2919
|
+
return;
|
|
2920
|
+
installing(replaying);
|
|
2921
|
+
await screened();
|
|
2922
|
+
},
|
|
2883
2923
|
prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
|
|
2884
2924
|
async stage(page) {
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
const
|
|
2925
|
+
for (let written = 0;;) {
|
|
2926
|
+
if (replaying !== null)
|
|
2927
|
+
await screened();
|
|
2928
|
+
const end = batchEnd(page.entries, written);
|
|
2929
|
+
const last = end === page.entries.length;
|
|
2930
|
+
const entries = page.entries.slice(written, end);
|
|
2931
|
+
const staged = await write(async () => {
|
|
2889
2932
|
if (!liveness())
|
|
2890
2933
|
return false;
|
|
2891
|
-
await local.refresh(key, last
|
|
2934
|
+
await timed(stageBudget, () => local.refresh(key, composition, last
|
|
2892
2935
|
? { ...page, entries }
|
|
2893
|
-
: { entries, removals: [], folders: [], folderRemovals: [] });
|
|
2936
|
+
: { entries, removals: [], folders: [], folderRemovals: [] }));
|
|
2894
2937
|
return true;
|
|
2895
2938
|
});
|
|
2896
2939
|
if (!staged)
|
|
2897
2940
|
return false;
|
|
2941
|
+
if (last)
|
|
2942
|
+
return true;
|
|
2943
|
+
written = end;
|
|
2898
2944
|
}
|
|
2899
|
-
return true;
|
|
2900
2945
|
},
|
|
2901
2946
|
};
|
|
2902
2947
|
}
|
|
2903
|
-
function
|
|
2904
|
-
|
|
2948
|
+
function batchEnd(entries, from) {
|
|
2949
|
+
let end = from;
|
|
2905
2950
|
let spent = 0;
|
|
2906
|
-
|
|
2907
|
-
const size = (
|
|
2908
|
-
if (spent + size >
|
|
2909
|
-
|
|
2910
|
-
spent = 0;
|
|
2911
|
-
}
|
|
2912
|
-
batches.at(-1).push(entry);
|
|
2951
|
+
while (end < entries.length) {
|
|
2952
|
+
const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
|
|
2953
|
+
if (spent + size > stageBudget.bytes && end > from)
|
|
2954
|
+
break;
|
|
2913
2955
|
spent += size;
|
|
2956
|
+
end += 1;
|
|
2914
2957
|
}
|
|
2915
|
-
return
|
|
2958
|
+
return end;
|
|
2959
|
+
}
|
|
2960
|
+
async function timed(budget, work) {
|
|
2961
|
+
const started = performance.now();
|
|
2962
|
+
const done = await work();
|
|
2963
|
+
const spent = performance.now() - started;
|
|
2964
|
+
if (spent < budget.aim / 2)
|
|
2965
|
+
budget.bytes = Math.min(budget.ceiling, Math.round(budget.bytes * 1.5));
|
|
2966
|
+
else if (spent > budget.aim * 2) {
|
|
2967
|
+
budget.bytes = Math.max(budget.floor, Math.round(budget.bytes / 1.5));
|
|
2968
|
+
}
|
|
2969
|
+
return done;
|
|
2970
|
+
}
|
|
2971
|
+
function installWrite(work) {
|
|
2972
|
+
const next = installWrites.then(() => exclusive(work), () => exclusive(work));
|
|
2973
|
+
installWrites = next.then(() => undefined, () => undefined);
|
|
2974
|
+
return next;
|
|
2975
|
+
}
|
|
2976
|
+
function screened() {
|
|
2977
|
+
if (onScreen || closed)
|
|
2978
|
+
return Promise.resolve();
|
|
2979
|
+
queueReadiness();
|
|
2980
|
+
return new Promise((resolve) => shownWaiters.push(resolve));
|
|
2981
|
+
}
|
|
2982
|
+
async function heldPart() {
|
|
2983
|
+
while (partsFree === 0 || queuedBytes >= QUEUED_BYTES) {
|
|
2984
|
+
await new Promise((admit) => partWaiters.push(admit));
|
|
2985
|
+
}
|
|
2986
|
+
partsFree -= 1;
|
|
2987
|
+
return once(() => {
|
|
2988
|
+
partsFree += 1;
|
|
2989
|
+
});
|
|
2990
|
+
}
|
|
2991
|
+
function once(release) {
|
|
2992
|
+
let released = false;
|
|
2993
|
+
return () => {
|
|
2994
|
+
if (released)
|
|
2995
|
+
return;
|
|
2996
|
+
released = true;
|
|
2997
|
+
release();
|
|
2998
|
+
for (const admit of partWaiters.splice(0))
|
|
2999
|
+
admit();
|
|
3000
|
+
};
|
|
3001
|
+
}
|
|
3002
|
+
function installing(locator, inventory = null) {
|
|
3003
|
+
installs.set(locator, inventory ?? installs.get(locator) ?? null);
|
|
3004
|
+
queueReadiness();
|
|
3005
|
+
}
|
|
3006
|
+
function untransferred(locator) {
|
|
3007
|
+
transfers.delete(locator);
|
|
3008
|
+
keptParts.delete(locator);
|
|
3009
|
+
underived.delete(locator);
|
|
3010
|
+
}
|
|
3011
|
+
function installed(locator) {
|
|
3012
|
+
if (installs.delete(locator))
|
|
3013
|
+
queueReadiness();
|
|
3014
|
+
}
|
|
3015
|
+
function screen(value) {
|
|
3016
|
+
if (value === onScreen)
|
|
3017
|
+
return;
|
|
3018
|
+
onScreen = value;
|
|
3019
|
+
journal.record(`installation ${value ? 'shown' : 'hidden'}`);
|
|
3020
|
+
queueReadiness();
|
|
3021
|
+
if (!value)
|
|
3022
|
+
return;
|
|
3023
|
+
for (const resolve of shownWaiters.splice(0))
|
|
3024
|
+
resolve();
|
|
2916
3025
|
}
|
|
2917
3026
|
async function pullRefresh(authorized, held, liveness) {
|
|
2918
3027
|
if (!liveness())
|
|
@@ -2923,7 +3032,7 @@ export function composeRuntime(options) {
|
|
|
2923
3032
|
const local = await openLocal();
|
|
2924
3033
|
try {
|
|
2925
3034
|
await exclusive(() => local.unrefresh(record.key));
|
|
2926
|
-
const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, liveness));
|
|
3035
|
+
const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, composed(record), liveness));
|
|
2927
3036
|
if (result.kind !== 'advanced') {
|
|
2928
3037
|
journal.record(`transfer refresh ${record.locator} ${result.kind}`);
|
|
2929
3038
|
return result;
|
|
@@ -2940,6 +3049,8 @@ export function composeRuntime(options) {
|
|
|
2940
3049
|
return { kind: 'advanced', record: result.record, base };
|
|
2941
3050
|
}
|
|
2942
3051
|
catch (error) {
|
|
3052
|
+
if (refusedForSpace(error))
|
|
3053
|
+
return unconfirmed;
|
|
2943
3054
|
journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
|
|
2944
3055
|
throw error;
|
|
2945
3056
|
}
|
|
@@ -2963,47 +3074,119 @@ export function composeRuntime(options) {
|
|
|
2963
3074
|
heldContents.set(key, made);
|
|
2964
3075
|
return made;
|
|
2965
3076
|
}
|
|
3077
|
+
async function keptTogether({ local, parts }) {
|
|
3078
|
+
const live = parts.filter((one) => one.live() && transfers.has(one.locator));
|
|
3079
|
+
await local.keepParts(live.map(({ locator, token, part }) => ({ locator, token, part })));
|
|
3080
|
+
for (const { locator, token, part } of live) {
|
|
3081
|
+
const held = keptParts.get(locator) ?? new Map();
|
|
3082
|
+
keptParts.set(locator, held.set(token, part.byteLength));
|
|
3083
|
+
arrivals.get(locator)?.delete(token);
|
|
3084
|
+
}
|
|
3085
|
+
queueReadiness();
|
|
3086
|
+
}
|
|
2966
3087
|
function staging(local, liveness) {
|
|
3088
|
+
const derived = async (locator, key, composition) => {
|
|
3089
|
+
underived.delete(locator);
|
|
3090
|
+
for (;;) {
|
|
3091
|
+
await screened();
|
|
3092
|
+
if (!liveness())
|
|
3093
|
+
return false;
|
|
3094
|
+
const done = await installWrite(async () => liveness()
|
|
3095
|
+
? timed(deriveBudget, () => local.rederive(key, composition, deriveBudget.bytes))
|
|
3096
|
+
: null);
|
|
3097
|
+
if (done === null)
|
|
3098
|
+
return false;
|
|
3099
|
+
if (done)
|
|
3100
|
+
return true;
|
|
3101
|
+
}
|
|
3102
|
+
};
|
|
2967
3103
|
return {
|
|
2968
3104
|
held: (locator) => transfers.get(locator) ?? null,
|
|
2969
|
-
async
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
3105
|
+
async turn(locator, step) {
|
|
3106
|
+
installing(locator);
|
|
3107
|
+
for (;;) {
|
|
3108
|
+
await screened();
|
|
3109
|
+
const release = step === 'downloading' ? await heldPart() : () => { };
|
|
3110
|
+
if (onScreen || closed)
|
|
3111
|
+
return release;
|
|
3112
|
+
release();
|
|
3113
|
+
}
|
|
3114
|
+
},
|
|
3115
|
+
queued(bytes) {
|
|
3116
|
+
queuedBytes += bytes;
|
|
3117
|
+
return once(() => {
|
|
3118
|
+
queuedBytes -= bytes;
|
|
3119
|
+
});
|
|
3120
|
+
},
|
|
3121
|
+
arriving(locator) {
|
|
3122
|
+
let ended = false;
|
|
3123
|
+
return {
|
|
3124
|
+
at(token, bytes) {
|
|
3125
|
+
if (ended || keptParts.get(locator)?.has(token) === true)
|
|
3126
|
+
return;
|
|
3127
|
+
const held = arrivals.get(locator) ?? new Map();
|
|
3128
|
+
arrivals.set(locator, held.set(token, bytes));
|
|
3129
|
+
const now = performance.now();
|
|
3130
|
+
if (now - arrivalReported < ARRIVAL_REPORT_MS)
|
|
3131
|
+
return;
|
|
3132
|
+
arrivalReported = now;
|
|
3133
|
+
queueReadiness();
|
|
3134
|
+
},
|
|
3135
|
+
end() {
|
|
3136
|
+
ended = true;
|
|
3137
|
+
if (arrivals.delete(locator))
|
|
3138
|
+
queueReadiness();
|
|
3139
|
+
},
|
|
3140
|
+
};
|
|
3141
|
+
},
|
|
3142
|
+
received: (locator, token) => local.transferPart(locator, token),
|
|
3143
|
+
keep: (locator, token, received) => gathered('kept parts', { local, parts: [{ locator, token, part: received, live: liveness }] }, (earlier, later) => earlier.local === later.local
|
|
3144
|
+
? { local: earlier.local, parts: [...earlier.parts, ...later.parts] }
|
|
3145
|
+
: null, keptTogether),
|
|
3146
|
+
announced(locator) {
|
|
3147
|
+
const later = announced.get(locator);
|
|
3148
|
+
return later === undefined ? [] : [later];
|
|
3149
|
+
},
|
|
3150
|
+
async stage(record, page, composition, written) {
|
|
3151
|
+
const { locator } = record;
|
|
3152
|
+
installing(locator, record.inventory);
|
|
3153
|
+
await screened();
|
|
3154
|
+
if (!liveness())
|
|
3155
|
+
return null;
|
|
3156
|
+
const key = await installWrite(async () => {
|
|
3157
|
+
if (!liveness())
|
|
2991
3158
|
return null;
|
|
2992
|
-
|
|
3159
|
+
return local.stage(record, composition, page, written ?? undefined);
|
|
3160
|
+
});
|
|
3161
|
+
if (key === null || !liveness())
|
|
3162
|
+
return null;
|
|
3163
|
+
const arrived = !transfers.has(locator);
|
|
3164
|
+
const held = { ...record, key };
|
|
3165
|
+
transfers.set(locator, held);
|
|
3166
|
+
if (written !== null) {
|
|
3167
|
+
keptParts.get(locator)?.delete(written);
|
|
3168
|
+
arrivals.get(locator)?.delete(written);
|
|
2993
3169
|
}
|
|
2994
|
-
|
|
3170
|
+
if (arrived)
|
|
3171
|
+
liveQueries.recover();
|
|
3172
|
+
installing(locator);
|
|
2995
3173
|
liveQueries.recover();
|
|
2996
|
-
|
|
3174
|
+
const owed = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), underived.get(locator) ?? 0);
|
|
3175
|
+
underived.set(locator, owed);
|
|
3176
|
+
if (owed < deriveBudget.bytes)
|
|
3177
|
+
return held;
|
|
3178
|
+
return (await derived(locator, key, composition)) ? held : null;
|
|
2997
3179
|
},
|
|
2998
|
-
|
|
3180
|
+
derive: derived,
|
|
3181
|
+
async refreshing(locator, key, composition) {
|
|
2999
3182
|
await exclusive(() => local.unrefresh(key));
|
|
3000
|
-
return refreshingOf(local, key, liveness);
|
|
3183
|
+
return refreshingOf(local, key, composition, liveness, locator);
|
|
3001
3184
|
},
|
|
3002
3185
|
abandon: (locator) => exclusive(async () => {
|
|
3003
3186
|
if (!liveness())
|
|
3004
3187
|
return;
|
|
3005
3188
|
await local.abandon(locator);
|
|
3006
|
-
|
|
3189
|
+
untransferred(locator);
|
|
3007
3190
|
liveQueries.recover();
|
|
3008
3191
|
sweep();
|
|
3009
3192
|
}),
|
|
@@ -3011,7 +3194,7 @@ export function composeRuntime(options) {
|
|
|
3011
3194
|
if (!liveness())
|
|
3012
3195
|
return;
|
|
3013
3196
|
await local.abandon(locator, true);
|
|
3014
|
-
|
|
3197
|
+
untransferred(locator);
|
|
3015
3198
|
liveQueries.recover();
|
|
3016
3199
|
}),
|
|
3017
3200
|
release: (key) => exclusive(async () => {
|
|
@@ -3037,19 +3220,21 @@ export function composeRuntime(options) {
|
|
|
3037
3220
|
liveQueries.recover();
|
|
3038
3221
|
}
|
|
3039
3222
|
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));
|
|
3223
|
+
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
3224
|
const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
|
|
3045
3225
|
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
3046
|
-
if (result.kind !== 'staged')
|
|
3226
|
+
if (result.kind !== 'staged') {
|
|
3047
3227
|
caughtUpTo(room.locator);
|
|
3228
|
+
installed(room.locator);
|
|
3229
|
+
}
|
|
3048
3230
|
return result;
|
|
3049
3231
|
}
|
|
3050
3232
|
catch (error) {
|
|
3051
|
-
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
3052
3233
|
caughtUpTo(room.locator);
|
|
3234
|
+
installed(room.locator);
|
|
3235
|
+
if (refusedForSpace(error))
|
|
3236
|
+
return unconfirmed;
|
|
3237
|
+
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
3053
3238
|
throw error;
|
|
3054
3239
|
}
|
|
3055
3240
|
finally {
|
|
@@ -3073,8 +3258,9 @@ export function composeRuntime(options) {
|
|
|
3073
3258
|
}
|
|
3074
3259
|
return advanceOnce(room, local, authorized, live);
|
|
3075
3260
|
}
|
|
3076
|
-
async function advanceOnce(room, local, authorized,
|
|
3077
|
-
|
|
3261
|
+
async function advanceOnce(room, local, authorized, running) {
|
|
3262
|
+
const live = () => running() && !full;
|
|
3263
|
+
if (!live())
|
|
3078
3264
|
return unconfirmed;
|
|
3079
3265
|
const held = replicas.get(room.locator);
|
|
3080
3266
|
if (held !== undefined) {
|
|
@@ -3100,7 +3286,7 @@ export function composeRuntime(options) {
|
|
|
3100
3286
|
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
3101
3287
|
return reached ? null : advance(room, local, authorized, live);
|
|
3102
3288
|
}
|
|
3103
|
-
await retainModel(local, room,
|
|
3289
|
+
await retainModel(local, room, modelOf(room.locator));
|
|
3104
3290
|
return null;
|
|
3105
3291
|
}
|
|
3106
3292
|
if (refreshed.kind !== 'beyond-horizon')
|
|
@@ -3114,12 +3300,11 @@ export function composeRuntime(options) {
|
|
|
3114
3300
|
return unconfirmed;
|
|
3115
3301
|
}
|
|
3116
3302
|
const bootstrapped = await pullBootstrap(authorized, room, local, live);
|
|
3117
|
-
if (
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
return null;
|
|
3303
|
+
if (bootstrapped.kind === 'staged') {
|
|
3304
|
+
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
3305
|
+
return live() ? null : unconfirmed;
|
|
3306
|
+
}
|
|
3307
|
+
return live() ? bootstrapped : unconfirmed;
|
|
3123
3308
|
}
|
|
3124
3309
|
async function withhold(withheld, room, local, progress) {
|
|
3125
3310
|
if (withheld.kind === 'unconfirmed')
|
|
@@ -3642,6 +3827,7 @@ export function composeRuntime(options) {
|
|
|
3642
3827
|
locator: room.locator,
|
|
3643
3828
|
key: before.record.key,
|
|
3644
3829
|
roots: before.record.typedSelection?.roots ?? [],
|
|
3830
|
+
composed: composedOf(composed(before.record)),
|
|
3645
3831
|
overlay: { set: [], cleared: [...before.overlaid] },
|
|
3646
3832
|
},
|
|
3647
3833
|
]),
|
|
@@ -3697,9 +3883,9 @@ export function composeRuntime(options) {
|
|
|
3697
3883
|
release();
|
|
3698
3884
|
};
|
|
3699
3885
|
}
|
|
3700
|
-
async function retainModel(local, room,
|
|
3701
|
-
const key =
|
|
3702
|
-
const previous = modelOf(room.locator
|
|
3886
|
+
async function retainModel(local, room, next) {
|
|
3887
|
+
const key = room.locator;
|
|
3888
|
+
const previous = modelOf(room.locator);
|
|
3703
3889
|
const heldBefore = replicas.get(room.locator)?.record;
|
|
3704
3890
|
if (JSON.stringify(previous) === JSON.stringify(next) &&
|
|
3705
3891
|
(heldBefore === undefined ||
|
|
@@ -3723,11 +3909,11 @@ export function composeRuntime(options) {
|
|
|
3723
3909
|
operations: room.operations,
|
|
3724
3910
|
replica: room.replica,
|
|
3725
3911
|
typedSelection: room.typedSelection ?? null,
|
|
3726
|
-
|
|
3912
|
+
model: next,
|
|
3727
3913
|
};
|
|
3728
3914
|
let read = 'staged';
|
|
3729
3915
|
if (!sameMembership(declared, held.record)) {
|
|
3730
|
-
const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'),
|
|
3916
|
+
const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), composed(declared));
|
|
3731
3917
|
read =
|
|
3732
3918
|
verdict.verdict !== 'refuse'
|
|
3733
3919
|
? 'staged'
|
|
@@ -3748,14 +3934,13 @@ export function composeRuntime(options) {
|
|
|
3748
3934
|
key: record.key,
|
|
3749
3935
|
record: storedRecord(record),
|
|
3750
3936
|
roots: record.typedSelection?.roots ?? [],
|
|
3751
|
-
|
|
3937
|
+
composed: composedOf(composed(record)),
|
|
3752
3938
|
overlay: diff.overlay,
|
|
3753
3939
|
},
|
|
3754
3940
|
],
|
|
3755
3941
|
});
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count });
|
|
3942
|
+
const { count, indexed } = committedRoom(room.locator, answer);
|
|
3943
|
+
replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count, indexed });
|
|
3759
3944
|
changed = changeSet(room.locator, {
|
|
3760
3945
|
documents: answer.changed[room.locator] ?? [],
|
|
3761
3946
|
room: true,
|
|
@@ -3796,14 +3981,14 @@ export function composeRuntime(options) {
|
|
|
3796
3981
|
});
|
|
3797
3982
|
let scopedAway = room;
|
|
3798
3983
|
if (result.scope === 'model')
|
|
3799
|
-
await retainModel(local, room,
|
|
3984
|
+
await retainModel(local, room, {
|
|
3800
3985
|
status: 'unavailable',
|
|
3801
3986
|
reason: 'not-readable',
|
|
3802
3987
|
});
|
|
3803
3988
|
else if (result.scope !== 'operation') {
|
|
3804
3989
|
unreadable.add(room.locator);
|
|
3805
3990
|
scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
|
|
3806
|
-
await retainModel(local, scopedAway,
|
|
3991
|
+
await retainModel(local, scopedAway, modelOf(room.locator));
|
|
3807
3992
|
}
|
|
3808
3993
|
if (!continuing())
|
|
3809
3994
|
return;
|
|
@@ -3853,7 +4038,7 @@ export function composeRuntime(options) {
|
|
|
3853
4038
|
return false;
|
|
3854
4039
|
}
|
|
3855
4040
|
if (prior !== undefined)
|
|
3856
|
-
await retainModel(local, room,
|
|
4041
|
+
await retainModel(local, room, modelOf(room.locator));
|
|
3857
4042
|
if (discoveryPaused || authorization === undefined)
|
|
3858
4043
|
return false;
|
|
3859
4044
|
return room.replica.admission === 'admitted';
|
|
@@ -3866,10 +4051,8 @@ export function composeRuntime(options) {
|
|
|
3866
4051
|
await withhold(withheld, room, local, progress);
|
|
3867
4052
|
}
|
|
3868
4053
|
function revoked(locator) {
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
3872
|
-
});
|
|
4054
|
+
const model = modelOf(locator);
|
|
4055
|
+
return model.status === 'unavailable' && model.reason === 'revoked';
|
|
3873
4056
|
}
|
|
3874
4057
|
async function loadRoom(room, local, progress, live) {
|
|
3875
4058
|
if (!live() || authorization === undefined)
|
|
@@ -3892,10 +4075,9 @@ export function composeRuntime(options) {
|
|
|
3892
4075
|
if (!live() || authorization === undefined)
|
|
3893
4076
|
return;
|
|
3894
4077
|
const bootstrapped = await pullBootstrap(authorization, room, local, live);
|
|
3895
|
-
if (!live())
|
|
3896
|
-
return;
|
|
3897
4078
|
if (bootstrapped.kind !== 'staged') {
|
|
3898
|
-
|
|
4079
|
+
if (live())
|
|
4080
|
+
await withhold(bootstrapped, room, local, progress);
|
|
3899
4081
|
return;
|
|
3900
4082
|
}
|
|
3901
4083
|
await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
|
|
@@ -3920,6 +4102,8 @@ export function composeRuntime(options) {
|
|
|
3920
4102
|
catch {
|
|
3921
4103
|
result = { kind: 'transport-uncertainty' };
|
|
3922
4104
|
}
|
|
4105
|
+
if (result.kind === 'discovery')
|
|
4106
|
+
roomsNamed = true;
|
|
3923
4107
|
if (!live()) {
|
|
3924
4108
|
progress.complete = false;
|
|
3925
4109
|
notifyReady();
|
|
@@ -3928,10 +4112,7 @@ export function composeRuntime(options) {
|
|
|
3928
4112
|
journal.record(`connection discover ${result.kind}`);
|
|
3929
4113
|
if (result.kind === 'transport-uncertainty') {
|
|
3930
4114
|
for (const room of rooms.values()) {
|
|
3931
|
-
await retainModel(local, room,
|
|
3932
|
-
if (room.typedSelection !== null) {
|
|
3933
|
-
await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
|
|
3934
|
-
}
|
|
4115
|
+
await retainModel(local, room, cached(modelOf(room.locator)));
|
|
3935
4116
|
}
|
|
3936
4117
|
progress.reachable = false;
|
|
3937
4118
|
progress.complete = false;
|
|
@@ -4044,7 +4225,7 @@ export function composeRuntime(options) {
|
|
|
4044
4225
|
function sync(attempt) {
|
|
4045
4226
|
return task(OPENING_LANE, async (live) => {
|
|
4046
4227
|
if (!live()) {
|
|
4047
|
-
|
|
4228
|
+
readyFromStore();
|
|
4048
4229
|
return;
|
|
4049
4230
|
}
|
|
4050
4231
|
const progress = { complete: true, reachable: true };
|
|
@@ -4122,6 +4303,7 @@ export function composeRuntime(options) {
|
|
|
4122
4303
|
});
|
|
4123
4304
|
}
|
|
4124
4305
|
function closedSocket(revoked, lost) {
|
|
4306
|
+
notifyDiscovered();
|
|
4125
4307
|
const opened = socketOpen;
|
|
4126
4308
|
socketOpen = false;
|
|
4127
4309
|
connectionLost();
|
|
@@ -4213,12 +4395,9 @@ export function composeRuntime(options) {
|
|
|
4213
4395
|
}
|
|
4214
4396
|
}
|
|
4215
4397
|
async function confirmModels(room, local) {
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
await retainModel(local, room, attached, { ...model, status: 'current' });
|
|
4220
|
-
}
|
|
4221
|
-
}
|
|
4398
|
+
const model = modelOf(room.locator);
|
|
4399
|
+
if (model.status === 'cached')
|
|
4400
|
+
await retainModel(local, room, { ...model, status: 'current' });
|
|
4222
4401
|
}
|
|
4223
4402
|
function submitLater(locator) {
|
|
4224
4403
|
if (localSaves)
|
|
@@ -4541,8 +4720,11 @@ export function composeRuntime(options) {
|
|
|
4541
4720
|
if (closed)
|
|
4542
4721
|
return;
|
|
4543
4722
|
closed = true;
|
|
4544
|
-
|
|
4545
|
-
|
|
4723
|
+
notifyDiscovered();
|
|
4724
|
+
if (probing !== null)
|
|
4725
|
+
clearInterval(probing);
|
|
4726
|
+
for (const resolve of shownWaiters.splice(0))
|
|
4727
|
+
resolve();
|
|
4546
4728
|
connectionLive();
|
|
4547
4729
|
lockListeners.clear();
|
|
4548
4730
|
readinessWatches.clear();
|
|
@@ -4553,10 +4735,6 @@ export function composeRuntime(options) {
|
|
|
4553
4735
|
withdrawServiceMode?.();
|
|
4554
4736
|
conversations.close();
|
|
4555
4737
|
void recovery.retire();
|
|
4556
|
-
store?.close();
|
|
4557
|
-
void opening?.then((opened) => {
|
|
4558
|
-
opened.close();
|
|
4559
|
-
}, () => { });
|
|
4560
4738
|
roomListeners.clear();
|
|
4561
4739
|
refusalListeners.clear();
|
|
4562
4740
|
storageLossListeners.clear();
|
|
@@ -4583,9 +4761,9 @@ export function composeRuntime(options) {
|
|
|
4583
4761
|
observeStream();
|
|
4584
4762
|
void openLocal().then(() => {
|
|
4585
4763
|
if (capabilities.length > 0)
|
|
4586
|
-
|
|
4764
|
+
readyFromStore();
|
|
4587
4765
|
}, () => {
|
|
4588
|
-
|
|
4766
|
+
readyFromStore();
|
|
4589
4767
|
});
|
|
4590
4768
|
if (options.adapter.roomStream === undefined)
|
|
4591
4769
|
void sync('unstreamed');
|
|
@@ -4632,9 +4810,8 @@ export function composeRuntime(options) {
|
|
|
4632
4810
|
}
|
|
4633
4811
|
async function searched(input, reach, locked) {
|
|
4634
4812
|
const asked = searchPlanOf(input);
|
|
4635
|
-
const plan = { ...asked, limit: capped(asked.limit) };
|
|
4636
4813
|
const filter = {
|
|
4637
|
-
...filterOf(
|
|
4814
|
+
...filterOf(asked.documents ?? {}, roomFacts(reach), planReach(reach)),
|
|
4638
4815
|
locked,
|
|
4639
4816
|
};
|
|
4640
4817
|
const unlocking = (filter.rooms ?? [])
|
|
@@ -4643,65 +4820,41 @@ export function composeRuntime(options) {
|
|
|
4643
4820
|
for (;;) {
|
|
4644
4821
|
const cover = covered(filter);
|
|
4645
4822
|
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);
|
|
4823
|
+
const pending = [...unlocking, ...('pending' in cover ? cover.pending : [])];
|
|
4670
4824
|
const complete = pending.length === 0 && !('pending' in cover);
|
|
4825
|
+
const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
|
|
4671
4826
|
const answering = complete
|
|
4672
4827
|
? filter
|
|
4673
|
-
: {
|
|
4674
|
-
...filter,
|
|
4675
|
-
rooms: covering
|
|
4676
|
-
.filter(({ room }) => searchIndex.rooms().has(room))
|
|
4677
|
-
.map(({ room }) => room),
|
|
4678
|
-
};
|
|
4828
|
+
: { ...filter, rooms: reachedRooms(filter).filter((room) => !awaited.has(room)) };
|
|
4679
4829
|
const answered = covered(answering);
|
|
4680
4830
|
if ('pending' in answered)
|
|
4681
4831
|
continue;
|
|
4682
|
-
const
|
|
4683
|
-
const
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4832
|
+
const listed = conversations.searchable();
|
|
4833
|
+
const named = reach !== null && !reach.conversations
|
|
4834
|
+
? []
|
|
4835
|
+
: (asked.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
|
|
4836
|
+
const hits = await (await openLocal()).search({
|
|
4837
|
+
text: asked.text,
|
|
4838
|
+
limit: capped(asked.limit),
|
|
4839
|
+
after: asked.after,
|
|
4840
|
+
total: asked.total,
|
|
4841
|
+
documents: {
|
|
4842
|
+
plan: answering,
|
|
4843
|
+
rooms: answered.rooms,
|
|
4844
|
+
beneath: asked.collection
|
|
4845
|
+
? answered.rooms.map(({ locator, key }) => ({
|
|
4846
|
+
key,
|
|
4847
|
+
roots: replicas.get(locator)?.record.typedSelection?.roots ?? [],
|
|
4848
|
+
}))
|
|
4849
|
+
: [],
|
|
4850
|
+
},
|
|
4851
|
+
conversations: named,
|
|
4852
|
+
});
|
|
4853
|
+
if (JSON.stringify(covered(answering)) !== JSON.stringify(answered))
|
|
4690
4854
|
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
4855
|
const coverage = {
|
|
4702
4856
|
rooms: answered.coverage.rooms,
|
|
4703
|
-
conversations:
|
|
4704
|
-
index: searchIndex.generation(),
|
|
4857
|
+
conversations: named,
|
|
4705
4858
|
overlay: answered.coverage.overlay,
|
|
4706
4859
|
};
|
|
4707
4860
|
return complete
|
|
@@ -4758,7 +4911,7 @@ export function composeRuntime(options) {
|
|
|
4758
4911
|
};
|
|
4759
4912
|
}
|
|
4760
4913
|
if (topic === 'readiness') {
|
|
4761
|
-
return watchReadiness({ target: '', code: 'stored',
|
|
4914
|
+
return watchReadiness({ target: '', code: 'stored', space: null, search: 'unused' }, null, deliver);
|
|
4762
4915
|
}
|
|
4763
4916
|
if (topic === 'rooms') {
|
|
4764
4917
|
roomListeners.add(deliver);
|
|
@@ -4827,7 +4980,7 @@ export function composeRuntime(options) {
|
|
|
4827
4980
|
typedSelection: rooms.get(locator)?.typedSelection === undefined ||
|
|
4828
4981
|
rooms.get(locator)?.typedSelection === null
|
|
4829
4982
|
? null
|
|
4830
|
-
: modelTypes(
|
|
4983
|
+
: modelTypes(builtIn),
|
|
4831
4984
|
};
|
|
4832
4985
|
},
|
|
4833
4986
|
roomDraft: {
|
|
@@ -4844,33 +4997,17 @@ export function composeRuntime(options) {
|
|
|
4844
4997
|
const record = roomDrafts.get(room)?.get(path);
|
|
4845
4998
|
return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
|
|
4846
4999
|
},
|
|
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
|
-
});
|
|
5000
|
+
write(room, path, revision, input, next = roomDraftRevision()) {
|
|
5001
|
+
if (typeof next !== 'string' || next === '')
|
|
5002
|
+
return Promise.reject(runtimeError('room-draft-malformed'));
|
|
5003
|
+
return gathered(`room-draft\u0000${room}\u0000${path}`, { revision, input, next }, (earlier, later) => {
|
|
5004
|
+
if (later.revision !== earlier.next)
|
|
5005
|
+
return null;
|
|
5006
|
+
const parts = gatheredParts(earlier.input, later.input);
|
|
5007
|
+
return parts === null
|
|
5008
|
+
? null
|
|
5009
|
+
: { revision: earlier.revision, input: parts, next: later.next };
|
|
5010
|
+
}, (written) => writtenRoomDraft(room, path, written.revision, written.input, written.next));
|
|
4874
5011
|
},
|
|
4875
5012
|
discard(room, path, revision) {
|
|
4876
5013
|
return exclusive(async () => {
|
|
@@ -4897,13 +5034,13 @@ export function composeRuntime(options) {
|
|
|
4897
5034
|
});
|
|
4898
5035
|
},
|
|
4899
5036
|
write(app, value) {
|
|
4900
|
-
return
|
|
5037
|
+
return exclusive(() => keptPresentation(app, () => structuredClone(value)));
|
|
4901
5038
|
},
|
|
4902
5039
|
edit(app, members) {
|
|
4903
|
-
return
|
|
4904
|
-
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(
|
|
4905
|
-
...Object.entries(
|
|
4906
|
-
]));
|
|
5040
|
+
return gathered(`presentation\u0000${app}`, structuredClone(members), (earlier, later) => ({ ...earlier, ...later }), (edited) => keptPresentation(app, (held) => Object.fromEntries([
|
|
5041
|
+
...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(edited, member)),
|
|
5042
|
+
...Object.entries(edited).filter(([, value]) => value !== null),
|
|
5043
|
+
])));
|
|
4907
5044
|
},
|
|
4908
5045
|
},
|
|
4909
5046
|
save(room, named, writtenBy) {
|
|
@@ -4922,7 +5059,8 @@ export function composeRuntime(options) {
|
|
|
4922
5059
|
part: readPart,
|
|
4923
5060
|
read: readBinaryPath,
|
|
4924
5061
|
link: linkBinaryPath,
|
|
4925
|
-
|
|
5062
|
+
show: showBinaryPath,
|
|
5063
|
+
attach: attachBinary,
|
|
4926
5064
|
},
|
|
4927
5065
|
async call(name, input) {
|
|
4928
5066
|
await ready;
|
|
@@ -4943,9 +5081,10 @@ export function composeRuntime(options) {
|
|
|
4943
5081
|
},
|
|
4944
5082
|
async retry() {
|
|
4945
5083
|
journal.record('retry');
|
|
4946
|
-
|
|
5084
|
+
unpreparable.clear();
|
|
4947
5085
|
queueReadiness();
|
|
4948
|
-
|
|
5086
|
+
if (full)
|
|
5087
|
+
await probed();
|
|
4949
5088
|
},
|
|
4950
5089
|
observe: observing(watch),
|
|
4951
5090
|
async reauthorize() {
|
|
@@ -4967,7 +5106,6 @@ export function composeRuntime(options) {
|
|
|
4967
5106
|
const reachable = {
|
|
4968
5107
|
md,
|
|
4969
5108
|
watch,
|
|
4970
|
-
referrerScope: (referrer) => options.adapter.referrerScope(referrer),
|
|
4971
5109
|
plan: planned,
|
|
4972
5110
|
query: (plan) => rowsQuery(plan),
|
|
4973
5111
|
watchPlan,
|
|
@@ -4991,6 +5129,11 @@ export function composeRuntime(options) {
|
|
|
4991
5129
|
journal.record(`persistence ${answer}`);
|
|
4992
5130
|
queueReadiness();
|
|
4993
5131
|
},
|
|
4994
|
-
|
|
5132
|
+
shown: screen,
|
|
5133
|
+
within: (reach, port) => {
|
|
5134
|
+
if (full)
|
|
5135
|
+
void probed();
|
|
5136
|
+
return narrowed(reachable, reach, port);
|
|
5137
|
+
},
|
|
4995
5138
|
};
|
|
4996
5139
|
}
|