@agentshouse/mdruntime 0.1.0-ccbe92df1c4f6 → 0.1.0-ccc0e3407f107

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.
Files changed (219) hide show
  1. package/README.md +11 -9
  2. package/dist/adapter.d.ts +91 -58
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -11
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +36 -47
  7. package/dist/bootstrap.d.ts +11 -14
  8. package/dist/bootstrap.d.ts.map +1 -1
  9. package/dist/bootstrap.js +101 -89
  10. package/dist/chain.d.ts +2 -1
  11. package/dist/chain.d.ts.map +1 -1
  12. package/dist/client.d.ts +15 -10
  13. package/dist/client.d.ts.map +1 -1
  14. package/dist/client.js +56 -7
  15. package/dist/collection.d.ts +7 -4
  16. package/dist/collection.d.ts.map +1 -1
  17. package/dist/collection.js +24 -19
  18. package/dist/compile.d.ts +1 -6
  19. package/dist/compile.d.ts.map +1 -1
  20. package/dist/compile.js +108 -243
  21. package/dist/connection.d.ts +14 -11
  22. package/dist/connection.d.ts.map +1 -1
  23. package/dist/connection.js +1256 -1225
  24. package/dist/context.d.ts +2 -0
  25. package/dist/context.d.ts.map +1 -0
  26. package/dist/{chat/context.js → context.js} +1 -4
  27. package/dist/conversation.d.ts +16 -15
  28. package/dist/conversation.d.ts.map +1 -1
  29. package/dist/conversation.js +234 -167
  30. package/dist/durability.d.ts +6 -1
  31. package/dist/durability.d.ts.map +1 -1
  32. package/dist/entry.d.ts +3 -2
  33. package/dist/entry.d.ts.map +1 -1
  34. package/dist/entry.js +17 -8
  35. package/dist/file.d.ts +2 -11
  36. package/dist/file.d.ts.map +1 -1
  37. package/dist/file.js +6 -4
  38. package/dist/host.d.ts +3 -7
  39. package/dist/host.d.ts.map +1 -1
  40. package/dist/host.js +53 -36
  41. package/dist/identity.d.ts +0 -1
  42. package/dist/identity.d.ts.map +1 -1
  43. package/dist/identity.js +0 -3
  44. package/dist/index.d.ts +12 -9
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +4 -1
  47. package/dist/job.d.ts +14 -0
  48. package/dist/job.d.ts.map +1 -0
  49. package/dist/job.js +28 -0
  50. package/dist/live.d.ts +2 -1
  51. package/dist/live.d.ts.map +1 -1
  52. package/dist/live.js +9 -5
  53. package/dist/media.d.ts +8 -0
  54. package/dist/media.d.ts.map +1 -0
  55. package/dist/media.js +218 -0
  56. package/dist/memory.d.ts +12 -6
  57. package/dist/memory.d.ts.map +1 -1
  58. package/dist/memory.js +210 -91
  59. package/dist/messages.d.ts +2 -2
  60. package/dist/messages.d.ts.map +1 -1
  61. package/dist/messages.js +15 -16
  62. package/dist/model.d.ts +8 -8
  63. package/dist/model.d.ts.map +1 -1
  64. package/dist/model.js +7 -13
  65. package/dist/observation.d.ts +3 -2
  66. package/dist/observation.d.ts.map +1 -1
  67. package/dist/observation.js +8 -3
  68. package/dist/outcome.d.ts +13 -5
  69. package/dist/outcome.d.ts.map +1 -1
  70. package/dist/outcome.js +16 -2
  71. package/dist/paths.d.ts +1 -0
  72. package/dist/paths.d.ts.map +1 -1
  73. package/dist/paths.js +4 -0
  74. package/dist/query.d.ts +8 -207
  75. package/dist/query.d.ts.map +1 -1
  76. package/dist/query.js +3 -505
  77. package/dist/reach.d.ts +14 -7
  78. package/dist/reach.d.ts.map +1 -1
  79. package/dist/reach.js +83 -53
  80. package/dist/react.d.ts +3 -1
  81. package/dist/react.d.ts.map +1 -1
  82. package/dist/readiness.d.ts +3 -16
  83. package/dist/readiness.d.ts.map +1 -1
  84. package/dist/readiness.js +2 -8
  85. package/dist/recovery.d.ts +1 -0
  86. package/dist/recovery.d.ts.map +1 -1
  87. package/dist/recovery.js +26 -0
  88. package/dist/refresh.d.ts +7 -5
  89. package/dist/refresh.d.ts.map +1 -1
  90. package/dist/refresh.js +30 -10
  91. package/dist/replica.d.ts +10 -9
  92. package/dist/replica.d.ts.map +1 -1
  93. package/dist/replica.js +30 -15
  94. package/dist/room-draft.d.ts +22 -5
  95. package/dist/room-draft.d.ts.map +1 -1
  96. package/dist/room-draft.js +131 -51
  97. package/dist/rows.d.ts +18 -16
  98. package/dist/rows.d.ts.map +1 -1
  99. package/dist/rows.js +45 -153
  100. package/dist/save.d.ts +2 -0
  101. package/dist/save.d.ts.map +1 -1
  102. package/dist/save.js +20 -12
  103. package/dist/search.d.ts +12 -63
  104. package/dist/search.d.ts.map +1 -1
  105. package/dist/search.js +9 -52
  106. package/dist/shared.d.ts +0 -1
  107. package/dist/shared.d.ts.map +1 -1
  108. package/dist/shared.js +0 -10
  109. package/dist/status.d.ts +4 -3
  110. package/dist/status.d.ts.map +1 -1
  111. package/dist/status.js +3 -1
  112. package/dist/store.d.ts +21 -39
  113. package/dist/store.d.ts.map +1 -1
  114. package/dist/store.js +33 -73
  115. package/dist/stored.d.ts +11 -27
  116. package/dist/stored.d.ts.map +1 -1
  117. package/dist/stored.js +187 -230
  118. package/dist/tables.d.ts +34 -51
  119. package/dist/tables.d.ts.map +1 -1
  120. package/dist/tables.js +75 -68
  121. package/dist/transfer.d.ts +0 -5
  122. package/dist/transfer.d.ts.map +1 -1
  123. package/dist/validation.d.ts +3 -4
  124. package/dist/validation.d.ts.map +1 -1
  125. package/dist/validation.js +42 -100
  126. package/dist/windows.d.ts +1 -12
  127. package/dist/windows.d.ts.map +1 -1
  128. package/dist/windows.js +2 -75
  129. package/dist/wire.d.ts +17 -8
  130. package/dist/wire.d.ts.map +1 -1
  131. package/dist/wire.js +5 -2
  132. package/package.json +10 -17
  133. package/AGENTS.md +0 -317
  134. package/dist/chat/acts.d.ts +0 -33
  135. package/dist/chat/acts.d.ts.map +0 -1
  136. package/dist/chat/acts.js +0 -36
  137. package/dist/chat/context.d.ts +0 -3
  138. package/dist/chat/context.d.ts.map +0 -1
  139. package/dist/chat/earlier.d.ts +0 -10
  140. package/dist/chat/earlier.d.ts.map +0 -1
  141. package/dist/chat/earlier.js +0 -54
  142. package/dist/chat/house.d.ts +0 -25
  143. package/dist/chat/house.d.ts.map +0 -1
  144. package/dist/chat/house.js +0 -38
  145. package/dist/chat/input.d.ts +0 -11
  146. package/dist/chat/input.d.ts.map +0 -1
  147. package/dist/chat/input.js +0 -92
  148. package/dist/chat/kit.d.ts +0 -61
  149. package/dist/chat/kit.d.ts.map +0 -1
  150. package/dist/chat/kit.js +0 -123
  151. package/dist/chat/lasting.d.ts +0 -6
  152. package/dist/chat/lasting.d.ts.map +0 -1
  153. package/dist/chat/lasting.js +0 -19
  154. package/dist/chat/list.d.ts +0 -28
  155. package/dist/chat/list.d.ts.map +0 -1
  156. package/dist/chat/list.js +0 -103
  157. package/dist/chat/markdown.d.ts +0 -20
  158. package/dist/chat/markdown.d.ts.map +0 -1
  159. package/dist/chat/markdown.js +0 -229
  160. package/dist/chat/message.d.ts +0 -7
  161. package/dist/chat/message.d.ts.map +0 -1
  162. package/dist/chat/message.js +0 -254
  163. package/dist/chat/messages.d.ts +0 -35
  164. package/dist/chat/messages.d.ts.map +0 -1
  165. package/dist/chat/messages.js +0 -210
  166. package/dist/chat/name.d.ts +0 -7
  167. package/dist/chat/name.d.ts.map +0 -1
  168. package/dist/chat/name.js +0 -37
  169. package/dist/chat/opened.d.ts +0 -12
  170. package/dist/chat/opened.d.ts.map +0 -1
  171. package/dist/chat/opened.js +0 -19
  172. package/dist/chat/overlay.d.ts +0 -32
  173. package/dist/chat/overlay.d.ts.map +0 -1
  174. package/dist/chat/overlay.js +0 -103
  175. package/dist/chat/parts.d.ts +0 -58
  176. package/dist/chat/parts.d.ts.map +0 -1
  177. package/dist/chat/parts.js +0 -120
  178. package/dist/chat/pieces.d.ts +0 -62
  179. package/dist/chat/pieces.d.ts.map +0 -1
  180. package/dist/chat/pieces.js +0 -1110
  181. package/dist/chat/process.d.ts +0 -20
  182. package/dist/chat/process.d.ts.map +0 -1
  183. package/dist/chat/process.js +0 -32
  184. package/dist/chat/provider.d.ts +0 -70
  185. package/dist/chat/provider.d.ts.map +0 -1
  186. package/dist/chat/provider.js +0 -635
  187. package/dist/chat/question.d.ts +0 -9
  188. package/dist/chat/question.d.ts.map +0 -1
  189. package/dist/chat/question.js +0 -69
  190. package/dist/chat/questions.d.ts +0 -31
  191. package/dist/chat/questions.d.ts.map +0 -1
  192. package/dist/chat/questions.js +0 -111
  193. package/dist/chat/read.d.ts +0 -5
  194. package/dist/chat/read.d.ts.map +0 -1
  195. package/dist/chat/read.js +0 -13
  196. package/dist/chat/ready.d.ts +0 -37
  197. package/dist/chat/ready.d.ts.map +0 -1
  198. package/dist/chat/ready.js +0 -93
  199. package/dist/chat/room-file.d.ts +0 -8
  200. package/dist/chat/room-file.d.ts.map +0 -1
  201. package/dist/chat/room-file.js +0 -72
  202. package/dist/chat/steady.d.ts +0 -3
  203. package/dist/chat/steady.d.ts.map +0 -1
  204. package/dist/chat/steady.js +0 -26
  205. package/dist/chat/style.d.ts +0 -4
  206. package/dist/chat/style.d.ts.map +0 -1
  207. package/dist/chat/style.js +0 -307
  208. package/dist/chat.d.ts +0 -15
  209. package/dist/chat.d.ts.map +0 -1
  210. package/dist/chat.js +0 -8
  211. package/dist/fulltext.d.ts +0 -27
  212. package/dist/fulltext.d.ts.map +0 -1
  213. package/dist/fulltext.js +0 -368
  214. package/dist/reads.d.ts +0 -11
  215. package/dist/reads.d.ts.map +0 -1
  216. package/dist/reads.js +0 -100
  217. package/dist/words.d.ts +0 -19
  218. package/dist/words.d.ts.map +0 -1
  219. package/dist/words.js +0 -150
@@ -1,8 +1,8 @@
1
- import { escapePointer, parseFrontmatter, roomTarget, target } from '@agentshouse/mdmodel';
2
- import { attachedModel, NO_ROOM_TYPES, roomTypesOf } from './attached-model.js';
1
+ import { identityOf, parseFrontmatter, plain, queryKey, roomTarget, target, } from '@agentshouse/mdmodel';
2
+ import { attachedModel, builtInModel, roomTypesOf, schemaTypes } from './attached-model.js';
3
3
  import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
4
4
  import { observing, whole } from './observation.js';
5
- import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
5
+ import { planWithin, } 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';
@@ -12,28 +12,28 @@ import { bootstrapRoom } from './bootstrap.js';
12
12
  import { changeSet, mergeChangeSets } from './change-set.js';
13
13
  import { createConversationStore, } from './conversation.js';
14
14
  import { leaving as leavingIn, partnerOf, queuedFile, } from './change.js';
15
- import { available, collectionOf, unreadable as unreadableIn, validRoot, } from './collection.js';
15
+ import { available, collectionOf, composedOf, validRoot, } from './collection.js';
16
16
  import { runtimeError } from './failure.js';
17
17
  import { createLog } from './log.js';
18
18
  import { binaryRecord } from './binary.js';
19
+ import { attachedFile } from './file.js';
19
20
  import { openedFiles } from './opened.js';
20
- import { ABSENT_TARGET, absentBinary, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
21
+ import { ABSENT_TARGET, absentBinary, attachAnswer, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, OFFLINE, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
21
22
  import { createRecovery } from './recovery.js';
22
- import { doorReads } from './reads.js';
23
23
  import { advancement } from './refresh.js';
24
24
  import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
25
- import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, 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';
25
+ import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
26
+ import { binaryBase, changedParts, changesOf, gatheredParts, held, heldFile, joinedDraft, moveTarget, readParts, readSavedParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
27
27
  import { same, saving, STARTING, transferring, } from './status.js';
28
28
  import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
29
29
  import { readinessOf, } from './readiness.js';
30
30
  import { FencedMap } from './fence.js';
31
31
  import { sameBinary } from './overlay.js';
32
32
  import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
33
- import { identityTime, roomDraftRevision } from './identity.js';
34
- import { beneath, normalize } from './paths.js';
33
+ import { roomDraftRevision } from './identity.js';
34
+ import { ancestors, beneath, normalize } from './paths.js';
35
35
  import { openRuntimeStore, } from './store.js';
36
- import { changedCleanly, sameComposition, validatedCollection, } from './validation.js';
36
+ import { validatedNeighbourhood } from './validation.js';
37
37
  import { unconfirmed } from './transfer.js';
38
38
  import { governs, holds, narrowed, planReach, } from './reach.js';
39
39
  function within(root, path) {
@@ -54,46 +54,43 @@ const CONVERSATION_LANE = 'conversations';
54
54
  const ROOM_READS = 256;
55
55
  const ROOM_ATTEMPTS = 16;
56
56
  const ERASE_BATCH = 512;
57
- const REDERIVE_BATCH = 256;
58
- const WALK_BYTES = 1 << 20;
57
+ const DERIVE_BYTES = 1 << 23;
58
+ const DERIVE_FLOOR = 1 << 18;
59
+ // An index build aims at this much database work, so a Save, a query or the next part's progress waits behind at most one short build.
60
+ const DERIVE_AIM_MS = 250;
61
+ // A build past its floor of text stops after a second and a half of work, its full-text writes included, so a Room whose text costs ten times the usual holds a Save, a query or the next part's progress behind no longer build.
62
+ const DERIVE_LONGEST_MS = 1_500;
59
63
  const QUERY_BYTES = 1 << 20;
60
64
  const STAGE_BYTES = 1 << 20;
61
65
  const STAGE_FLOOR = 1 << 16;
62
66
  // A batch aims at this much database work, so a Save or a query waits behind at most one short write.
63
67
  const STAGE_AIM_MS = 50;
64
- const PARTS_HELD = 4;
65
- // Bytes still arriving are reported at most this often, well inside the 2 s progress bound, so readiness is not rebuilt for every chunk the network hands over.
68
+ const STREAMS_HELD = 4;
69
+ const NOTHING_HELD = { bytes: 0 };
70
+ const QUEUED_TEXT = 1 << 23;
71
+ // Bytes still arriving are reported at most this often, so readiness is not rebuilt for every chunk the network hands over while a slow link still moves the figure.
66
72
  const ARRIVAL_REPORT_MS = 250;
67
- const INSTALL_STEPS = [
68
- 'writing',
69
- 'downloading',
70
- 'replaying',
71
- 'listing',
72
- ];
73
- const CLEAN_CHANGES = 2_000;
74
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.
75
74
  const LOCK_GRACE_MS = 120_000;
76
75
  function operationOf(input) {
77
76
  const operation = input?.operation_id;
78
77
  return typeof operation === 'string' ? operation : null;
79
78
  }
80
- function abandonedOn(signal) {
81
- return new Promise((resolve) => {
82
- if (signal.aborted)
83
- resolve(null);
84
- else
85
- signal.addEventListener('abort', () => resolve(null), { once: true });
79
+ function readUntil(read, signal) {
80
+ if (signal.aborted)
81
+ return Promise.resolve({ kind: 'transport-uncertainty' });
82
+ return new Promise((resolve, reject) => {
83
+ const stop = () => resolve({ kind: 'transport-uncertainty' });
84
+ signal.addEventListener('abort', stop, { once: true });
85
+ read.then(resolve, reject).finally(() => signal.removeEventListener('abort', stop));
86
86
  });
87
87
  }
88
- function roomLane(locator) {
89
- return `room ${locator}`;
90
- }
91
88
  function pendingKey(locator, id) {
92
89
  return `${locator}\u0000${id}`;
93
90
  }
94
91
  export function composeRuntime(options) {
95
92
  const localSaves = options.adapter.localSaves === true;
96
- const roomTypes = roomTypesOf(options.adapter.roomTypes);
93
+ const roomTypes = roomTypesOf(options.adapter.roomTypes, options.adapter.nameableRoomTypes);
97
94
  let authorization = options.authorization;
98
95
  let opening;
99
96
  const fences = new Map();
@@ -103,44 +100,53 @@ export function composeRuntime(options) {
103
100
  const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
104
101
  let discoveries = 0;
105
102
  const replicas = new FencedMap(isFenced);
103
+ const reopened = new Set();
104
+ const usableWhenHeld = options.usableWhenHeld ?? [];
106
105
  const queuesWithoutReplica = new FencedMap(isFenced);
107
106
  const pendings = new Map();
107
+ const landedAhead = new Map();
108
108
  const seeds = new Map();
109
109
  const seedWatchers = new Map();
110
110
  const roomDrafts = new FencedMap(isFenced);
111
+ const attachedFiles = new FencedMap(isFenced);
111
112
  const presentations = new Map();
112
113
  let limits = [];
113
114
  let capabilities = [];
114
115
  const rooms = new FencedMap(isFenced);
115
116
  const counted = new Map();
116
117
  const unreadable = new Set();
117
- const isModelFenced = (key) => isFenced(key.slice(key.indexOf(' ') + 1));
118
- const models = new FencedMap(isModelFenced);
119
- const liveModels = new FencedMap(isModelFenced);
118
+ const builtIn = builtInModel(roomTypes);
119
+ const typedPaths = options.adapter.typedPaths ?? [];
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
- const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
124
+ let inFlight = new AbortController();
125
+ const refreshed = options.adapter.refresh?.bind(options.adapter);
126
+ const reads = {
127
+ bootstrap: (authorized, request) => readUntil(options.adapter.bootstrap(authorized, request), inFlight.signal),
128
+ ...(refreshed === undefined
129
+ ? {}
130
+ : {
131
+ refresh: (authorized, request) => readUntil(refreshed(authorized, request), inFlight.signal),
132
+ }),
133
+ reached: (locator, roomVersion, target) => options.adapter.reached(locator, roomVersion, target),
134
+ };
123
135
  let roomsListed = false;
124
136
  const scopedRefusals = new Map();
125
- const sourceRefusals = new Map();
126
- const declarationRefusals = new Set();
127
- function modelKey(locator, attached) {
128
- return `${attached} ${locator}`;
129
- }
130
- function modelOf(locator, attached = 'collection') {
131
- return models.get(modelKey(locator, attached)) ?? NO_MODEL;
137
+ function modelOf(locator) {
138
+ return models.get(locator) ?? NO_MODEL;
132
139
  }
133
- function savedModel(locator, attached) {
134
- return invalidModels.has(modelKey(locator, attached))
140
+ function savedModel(locator) {
141
+ return invalidModels.has(locator)
135
142
  ? { status: 'unavailable', reason: 'revoked' }
136
- : modelOf(locator, attached);
143
+ : modelOf(locator);
144
+ }
145
+ function composed(record) {
146
+ return compositionOf(record, builtIn, typedPaths);
137
147
  }
138
148
  function compositionFor(record) {
139
- return compositionOf({
140
- ...record,
141
- model: savedModel(record.locator, 'collection'),
142
- selectionModel: savedModel(record.locator, 'typed-selection'),
143
- });
149
+ return composed({ ...record, model: savedModel(record.locator) });
144
150
  }
145
151
  let refusal = null;
146
152
  let storageLoss = null;
@@ -150,29 +156,31 @@ export function composeRuntime(options) {
150
156
  let closed = false;
151
157
  let stopping = false;
152
158
  let withdrawStream;
153
- const announced = new Map();
154
159
  let underway = null;
155
160
  let streamed = null;
156
161
  let handed = false;
162
+ let naming = 0;
157
163
  const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
158
164
  const repairing = new Set();
159
165
  const calling = new Set();
160
166
  const repairable = new Map();
161
- let outcomeRetention = null;
162
167
  const answering = new Map();
163
168
  let complete = false;
164
169
  let recorded = false;
165
170
  const stale = new Set();
166
171
  let generation = 0;
167
- let discovering = null;
168
- let opened = null;
169
- const owed = new Set();
170
- const reloadsDue = new Map();
172
+ const reloadsDue = new Set();
173
+ const highest = new Map();
174
+ const beyondHorizon = new Set();
171
175
  const lanes = new Map();
172
- let roomReads = { generation, free: ROOM_READS, waiting: [] };
176
+ const roomLanes = new Map();
177
+ let roomWork = 0;
178
+ const roomReads = { free: ROOM_READS, waiting: [] };
173
179
  const working = new Set();
174
180
  let mutations = Promise.resolve();
181
+ let gathering = null;
175
182
  const roomListeners = new Set();
183
+ const unlistedRooms = new Set();
176
184
  const refusalListeners = new Set();
177
185
  const storageLossListeners = new Set();
178
186
  const statusListeners = new Set();
@@ -183,6 +191,8 @@ export function composeRuntime(options) {
183
191
  let withdrawConversations;
184
192
  let currencyQueued = null;
185
193
  let socketOpen = false;
194
+ let announced = false;
195
+ let missedChange = false;
186
196
  const confirmed = new Set();
187
197
  let lockGrace = null;
188
198
  const lockListeners = new Set();
@@ -192,6 +202,8 @@ export function composeRuntime(options) {
192
202
  const serviceModeListeners = new Set();
193
203
  let member = null;
194
204
  const memberListeners = new Set();
205
+ let authorityChanges = 0;
206
+ const authorityChangeListeners = new Set();
195
207
  const unpreparedDemand = new Set();
196
208
  const unpreparable = new Set();
197
209
  let preparing = false;
@@ -216,31 +228,32 @@ export function composeRuntime(options) {
216
228
  let readinessQueued = false;
217
229
  const catching = new Set();
218
230
  const transfers = new FencedMap(isFenced);
219
- const installs = new Map();
231
+ const installs = new Set();
220
232
  const arrivals = new Map();
233
+ const streaming = new Set();
234
+ const underived = new Map();
235
+ const unindexed = new Map();
236
+ const startedOver = new Map();
221
237
  let arrivalReported = 0;
222
- let onScreen = options.shell.onScreen;
223
- const shownWaiters = [];
224
- let partsFree = PARTS_HELD;
225
- const partWaiters = [];
226
- let stageBytes = STAGE_FLOOR * 4;
227
- let installWrites = Promise.resolve();
238
+ let streamsFree = STREAMS_HELD;
239
+ let queuedText = 0;
240
+ const installWaiters = [];
241
+ const stageBudget = {
242
+ bytes: STAGE_FLOOR * 4,
243
+ floor: STAGE_FLOOR,
244
+ ceiling: STAGE_BYTES,
245
+ aim: STAGE_AIM_MS,
246
+ };
247
+ const deriveBudget = {
248
+ bytes: DERIVE_FLOOR * 4,
249
+ floor: DERIVE_FLOOR,
250
+ ceiling: DERIVE_BYTES,
251
+ aim: DERIVE_AIM_MS,
252
+ };
253
+ const installTurns = { busy: false, ahead: 0, finishing: [], starting: [] };
228
254
  const conversations = createConversationStore({
229
- agents: declaredAgents,
230
- documents: (declaration, after) => rowsQuery(planOf({
231
- rooms: [declaration.locator],
232
- types: [declaration.type],
233
- select: {
234
- fields: [
235
- declaration.identity,
236
- declaration.name,
237
- ...(declaration.retired === undefined ? [] : [declaration.retired]),
238
- ].map((field) => `/${escapePointer(field)}`),
239
- },
240
- limit: MAXIMUM_WINDOW,
241
- ...(after === undefined ? {} : { after }),
242
- }, roomFacts())),
243
255
  call: (name, input) => invokeAction(name, input),
256
+ history: (request) => readConversationHistory(request),
244
257
  durably,
245
258
  stream: () => options.adapter.conversations,
246
259
  publish: publishChangeSet,
@@ -248,14 +261,22 @@ export function composeRuntime(options) {
248
261
  full: () => full,
249
262
  closed: () => closed,
250
263
  unanswered: resendLater,
251
- expired: (operation) => pastHorizon(operation),
252
264
  });
253
265
  const journal = createLog();
254
266
  const keptFiles = openedFiles(database, isFenced);
255
- let notifyReady = () => { };
267
+ let readyFromStore = () => { };
256
268
  const ready = new Promise((resolve) => {
257
- notifyReady = resolve;
269
+ readyFromStore = resolve;
270
+ });
271
+ let notifyDiscovered = () => { };
272
+ const firstDiscovery = new Promise((resolve) => {
273
+ notifyDiscovered = resolve;
258
274
  });
275
+ let roomsNamed = false;
276
+ function notifyReady() {
277
+ notifyDiscovered();
278
+ readyFromStore();
279
+ }
259
280
  function rememberRoomDraft(record) {
260
281
  const room = roomDrafts.get(record.locator) ?? new Map();
261
282
  room.set(record.path, record);
@@ -275,7 +296,7 @@ export function composeRuntime(options) {
275
296
  return undefined;
276
297
  const local = await openLocal();
277
298
  const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
278
- return { ...slice, collection: collectionOf(slice.documents, compositionOf(held.record)) };
299
+ return { ...slice, collection: collectionOf(slice.documents, composed(held.record)) };
279
300
  }
280
301
  function roomDraftOf(record, visible) {
281
302
  return structuredClone({
@@ -331,14 +352,40 @@ export function composeRuntime(options) {
331
352
  const value = presentations.get(app);
332
353
  return value === undefined ? null : structuredClone(value);
333
354
  }
334
- function keepPresentation(app, next) {
335
- return exclusive(async () => {
336
- const local = await openLocal();
337
- const stored = next(presentations.get(app));
338
- await local.present({ app, value: stored });
339
- presentations.set(app, stored);
340
- publishChangeSet(changeSet('', { presentation: app }));
341
- });
355
+ async function writtenRoomDraft(room, path, revision, input, next) {
356
+ const parts = readParts(input, path);
357
+ if (path === '' || normalize(path) !== path)
358
+ throw runtimeError('room-draft-malformed', `room-draft-malformed at ${path}: a path inside the Room, such as notes/a.md`);
359
+ const local = await openLocal();
360
+ const current = roomDrafts.get(room)?.get(path);
361
+ if ((current?.revision ?? null) !== revision) {
362
+ return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
363
+ }
364
+ const file = parts.file === undefined
365
+ ? undefined
366
+ : await heldFile(parts.file.blob, parts.file.base === undefined
367
+ ? binaryBase(await visibleOf(room, [path]), path)
368
+ : parts.file.base);
369
+ const record = {
370
+ locator: room,
371
+ path,
372
+ revision: next,
373
+ parts: held(current?.parts, parts, file),
374
+ returned: current?.returned ?? null,
375
+ };
376
+ await local.keepRoomDraft(record, parts.file === undefined || file === undefined
377
+ ? []
378
+ : [{ key: file.localId, blob: parts.file.blob }]);
379
+ rememberRoomDraft(record);
380
+ publishChangeSet(changeSet(room, { drafts: [path] }));
381
+ return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
382
+ }
383
+ async function keptPresentation(app, next) {
384
+ const local = await openLocal();
385
+ const stored = next(presentations.get(app));
386
+ await local.present({ app, value: stored });
387
+ presentations.set(app, stored);
388
+ publishChangeSet(changeSet('', { presentation: app }));
342
389
  }
343
390
  function emitPresentation(app) {
344
391
  if (closed)
@@ -353,6 +400,20 @@ export function composeRuntime(options) {
353
400
  (queuesWithoutReplica.size > 0 ||
354
401
  [...replicas.values()].some((replica) => replica.queue.length > 0)));
355
402
  }
403
+ function unsent() {
404
+ const changed = new Map();
405
+ for (const room of [...replicas.keys(), ...queuesWithoutReplica.keys()]) {
406
+ for (const { batch, state } of entriesOf(room)) {
407
+ if (state === 'local')
408
+ continue;
409
+ for (const { path } of batch.changes)
410
+ changed.set(`${room}\u0000${path}`, { room, path });
411
+ }
412
+ }
413
+ return [...changed]
414
+ .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
415
+ .map(([, change]) => change);
416
+ }
356
417
  function attention() {
357
418
  const attentionRequired = [];
358
419
  for (const record of [...roomDrafts.values()].flatMap((room) => [...room.values()])) {
@@ -364,16 +425,9 @@ export function composeRuntime(options) {
364
425
  returned: structuredClone(record.returned),
365
426
  });
366
427
  }
367
- for (const [room, refusal] of sourceRefusals)
368
- attentionRequired.push({ room, refusal: structuredClone(refusal) });
369
428
  attentionRequired.sort((left, right) => {
370
429
  if (left.room !== right.room)
371
430
  return left.room < right.room ? -1 : 1;
372
- if ('refusal' in left || 'refusal' in right) {
373
- if ('refusal' in left && 'refusal' in right)
374
- return 0;
375
- return 'refusal' in left ? -1 : 1;
376
- }
377
431
  if (left.path !== right.path)
378
432
  return left.path < right.path ? -1 : 1;
379
433
  return 0;
@@ -385,7 +439,7 @@ export function composeRuntime(options) {
385
439
  if (reset === null || submission.reset < reset)
386
440
  reset = submission.reset;
387
441
  }
388
- return { attentionRequired, reset };
442
+ return { attentionRequired, unsent: unsent(), reset };
389
443
  }
390
444
  function documentsIn(locator) {
391
445
  return replicas.get(locator)?.count ?? 0;
@@ -458,7 +512,7 @@ export function composeRuntime(options) {
458
512
  const governed = governs(reach, room);
459
513
  const attached = [
460
514
  ...(governed.collection ? [modelOf(room.locator)] : []),
461
- ...(governed.selection ? [modelOf(room.locator, 'typed-selection')] : []),
515
+ ...(governed.selection ? [builtIn] : []),
462
516
  ];
463
517
  return {
464
518
  locator: room.locator,
@@ -474,11 +528,26 @@ export function composeRuntime(options) {
474
528
  const value = listed();
475
529
  for (const listener of roomListeners)
476
530
  listener(value);
531
+ emitKnownRooms();
532
+ }
533
+ function emitKnownRooms() {
534
+ conversations.learned();
535
+ if (unlistedRooms.size === 0)
536
+ return;
537
+ if (!roomsListed && rooms.size === 0 && refusal === null && !status.providerUnreachable)
538
+ return;
539
+ const value = listed();
540
+ for (const listener of [...unlistedRooms]) {
541
+ unlistedRooms.delete(listener);
542
+ roomListeners.add(listener);
543
+ listener(value);
544
+ }
477
545
  }
478
546
  function emitRefusal() {
479
547
  queueReadiness();
480
548
  for (const listener of refusalListeners)
481
549
  listener(refusal);
550
+ emitKnownRooms();
482
551
  }
483
552
  function emitStorageLoss() {
484
553
  queueReadiness();
@@ -499,6 +568,7 @@ export function composeRuntime(options) {
499
568
  if (closed)
500
569
  return;
501
570
  queueReadiness();
571
+ emitKnownRooms();
502
572
  const next = status.state === 'synced' && !conversations.current()
503
573
  ? { ...status, state: 'syncing' }
504
574
  : status;
@@ -512,9 +582,6 @@ export function composeRuntime(options) {
512
582
  if (localSaves)
513
583
  return Object.freeze({ batch, state: 'local' });
514
584
  const state = pendings.get(pendingKey(locator, batch.id));
515
- if (state?.accepted !== undefined) {
516
- return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
517
- }
518
585
  return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
519
586
  }
520
587
  function entriesOf(locator) {
@@ -542,11 +609,14 @@ export function composeRuntime(options) {
542
609
  if (held === undefined || closed || held.indexed)
543
610
  return;
544
611
  const key = held.record.key;
545
- const done = await exclusive(async () => (await openLocal()).rederive(key, REDERIVE_BATCH));
612
+ const done = await exclusive(async () => {
613
+ const local = await openLocal();
614
+ return timed(deriveBudget, () => local.rederive(key, composed(held.record), deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR));
615
+ });
546
616
  const now = replicas.get(locator);
547
617
  if (now?.record.key !== key)
548
618
  continue;
549
- if (!done)
619
+ if (!done.done)
550
620
  continue;
551
621
  replicas.set(locator, { ...now, indexed: true });
552
622
  publishChangeSet(changeSet(locator, { documents: null }));
@@ -624,7 +694,7 @@ export function composeRuntime(options) {
624
694
  coverage.push({
625
695
  room: locator,
626
696
  generation: replica.record.transfer,
627
- current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
697
+ current: !(catching.has(locator) || transfers.has(locator)),
628
698
  });
629
699
  read.push({ locator, key: replica.record.key });
630
700
  for (const batch of replica.queue)
@@ -651,8 +721,6 @@ export function composeRuntime(options) {
651
721
  return { listed: named.every((room) => replicas.has(room)), rooms: named };
652
722
  }
653
723
  function roomState(locator) {
654
- if (sourceRefusals.has(locator))
655
- return 'refused';
656
724
  const listedRoom = discovered.get(locator);
657
725
  if (roomsListed &&
658
726
  (listedRoom === undefined ||
@@ -667,7 +735,7 @@ export function composeRuntime(options) {
667
735
  }
668
736
  function conversationCoverage(reach) {
669
737
  const declared = reach === null || reach.conversations;
670
- if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
738
+ if (!declared || options.adapter.conversations === undefined)
671
739
  return 'undeclared';
672
740
  if (!conversations.known() || !conversations.current())
673
741
  return 'acquiring';
@@ -682,70 +750,50 @@ export function composeRuntime(options) {
682
750
  ? 'connected'
683
751
  : 'connecting';
684
752
  }
685
- function refusedIn(locators) {
686
- return locators.flatMap((room) => {
687
- const held = sourceRefusals.get(room);
688
- if (held === undefined)
689
- return [];
690
- const { file, location } = held.fields;
691
- return [
692
- {
693
- room,
694
- file: typeof file === 'string' ? file : '',
695
- rule: held.code,
696
- location: typeof location === 'string' ? location : null,
697
- },
698
- ];
699
- });
753
+ function bytesOf(room, underway) {
754
+ const transfer = transfers.get(room);
755
+ if (transfer !== undefined) {
756
+ const owed = (unindexed.get(room) ?? []).reduce((sum, part) => sum + part.bytes, 0);
757
+ return {
758
+ done: transfer.done.bytes - Math.ceil(owed),
759
+ total: Math.max(arrivals.get(room) ?? 0, transfer.done.bytes),
760
+ };
761
+ }
762
+ const held = underway ? NOTHING_HELD : (replicas.get(room)?.record.inventory ?? NOTHING_HELD);
763
+ return { done: held.bytes, total: arrivals.get(room) ?? held.bytes };
700
764
  }
701
765
  function installationOf(coverage) {
702
766
  const rooms = { done: 0, total: 0 };
703
- const parts = { done: 0, total: 0 };
704
767
  const bytes = { done: 0, total: 0 };
705
- const received = { done: 0, total: 0 };
706
- const steps = new Set();
707
- let waiting = false;
768
+ let installing = 0;
769
+ let downloading = false;
708
770
  for (const { room, state } of coverage) {
709
771
  if (state === 'unadmitted')
710
772
  continue;
711
773
  rooms.total += 1;
712
774
  if (state === 'complete')
713
775
  rooms.done += 1;
714
- const transfer = transfers.get(room);
715
- const held = installs.get(room);
716
- const inventory = transfer?.inventory ?? replicas.get(room)?.record.inventory ?? null;
717
- if (inventory !== null) {
718
- const done = transfer?.done ?? inventory;
719
- const share = held?.share ?? null;
720
- parts.total += inventory.parts;
721
- bytes.total += inventory.bytes;
722
- parts.done += done.parts;
723
- bytes.done += done.bytes;
724
- if (share !== null && share.entries > 0) {
725
- bytes.done += Math.floor((share.bytes * share.written) / share.entries);
726
- }
727
- received.total += inventory.bytes;
728
- received.done += done.bytes + (share?.bytes ?? 0);
729
- for (const arrived of arrivals.get(room)?.values() ?? [])
730
- received.done += arrived;
731
- }
732
- const installing = held !== undefined && state !== 'complete';
733
- if (installing)
734
- steps.add(held.step);
735
- waiting ||= installing || state === 'acquiring';
736
- }
737
- return {
738
- step: INSTALL_STEPS.find((step) => steps.has(step)) ?? null,
739
- paused: waiting && !onScreen ? 'hidden' : null,
740
- rooms,
741
- parts,
742
- bytes,
743
- received,
744
- };
776
+ const underway = state === 'acquiring' || (installs.has(room) && state !== 'complete');
777
+ if (underway)
778
+ installing += 1;
779
+ if (underway && (streaming.has(room) || !arrivals.has(room)))
780
+ downloading = true;
781
+ const now = bytesOf(room, underway);
782
+ const earlier = underway ? startedOver.get(room) : undefined;
783
+ bytes.done += Math.max(now.done, earlier?.done ?? 0);
784
+ bytes.total += Math.max(now.total, earlier?.total ?? 0);
785
+ }
786
+ const step = installing === 0
787
+ ? null
788
+ : downloading
789
+ ? 'downloading'
790
+ : bytes.done < bytes.total
791
+ ? 'writing'
792
+ : 'replaying';
793
+ return { step, rooms, bytes };
745
794
  }
746
795
  function readinessFor(port, reach) {
747
796
  const required = requiredRooms(reach);
748
- const search = port.search === 'required';
749
797
  const coverage = required.rooms.map((room) => ({ room, state: roomState(room) }));
750
798
  const preparing = coverage
751
799
  .filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
@@ -758,16 +806,15 @@ export function composeRuntime(options) {
758
806
  listed: required.listed,
759
807
  rooms: coverage,
760
808
  conversations: conversationCoverage(reach),
761
- search: search ? 'required' : 'unused',
762
809
  },
763
810
  persistence,
764
811
  connection: connectionOf(),
765
812
  house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
766
- space: full ? 'full' : port.space,
813
+ space: full ? 'full' : null,
767
814
  installation: installationOf(coverage),
768
- refused: refusedIn(required.rooms),
769
815
  lost: storageLoss !== null,
770
- established: targets[port.target]?.established === true,
816
+ established: targets[port.target]?.established === true ||
817
+ (usableWhenHeld.includes(port.target) && required.rooms.some((room) => reopened.has(room))),
771
818
  });
772
819
  }
773
820
  function establish(name) {
@@ -820,7 +867,8 @@ export function composeRuntime(options) {
820
867
  probeEachMinute();
821
868
  }
822
869
  function installedTarget() {
823
- return Object.values(targets).some((target) => target.established);
870
+ return ((usableWhenHeld.length > 0 && reopened.size > 0) ||
871
+ Object.values(targets).some((target) => target.established));
824
872
  }
825
873
  function probeEachMinute() {
826
874
  if (!full || probing !== null || closed || !installedTarget())
@@ -851,8 +899,12 @@ export function composeRuntime(options) {
851
899
  unpreparable.clear();
852
900
  journal.record('space freed');
853
901
  queueReadiness();
854
- if (!closed && authorization !== undefined)
855
- void sync('unstreamed');
902
+ if (closed || authorization === undefined)
903
+ return;
904
+ if (options.adapter.roomStream === undefined)
905
+ void sync('unstreamed', null);
906
+ else
907
+ void resynchronize();
856
908
  }
857
909
  function seedRoomHeld(room, version) {
858
910
  return indexed(room) && reachedBy(room, version);
@@ -978,6 +1030,10 @@ export function composeRuntime(options) {
978
1030
  }
979
1031
  function publishChangeSet(set) {
980
1032
  applyChangeSet(set);
1033
+ for (const { locator, path } of set.documents) {
1034
+ if (path !== null)
1035
+ attachedFiles.get(locator)?.delete(path);
1036
+ }
981
1037
  if (set.rooms.length > 0 ||
982
1038
  [...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
983
1039
  emitRooms();
@@ -996,6 +1052,8 @@ export function composeRuntime(options) {
996
1052
  }
997
1053
  async function refuse(reason, provider = null) {
998
1054
  discoveryPaused = true;
1055
+ inFlight.abort();
1056
+ inFlight = new AbortController();
999
1057
  try {
1000
1058
  if (reason === 'revoked-authorization') {
1001
1059
  await exclusive(async () => {
@@ -1003,7 +1061,7 @@ export function composeRuntime(options) {
1003
1061
  const records = [...replicas].map(([locator, held]) => ({
1004
1062
  locator,
1005
1063
  held,
1006
- record: { ...held.record, model: revoked, selectionModel: revoked },
1064
+ record: { ...held.record, model: revoked },
1007
1065
  }));
1008
1066
  let answer = null;
1009
1067
  try {
@@ -1013,18 +1071,14 @@ export function composeRuntime(options) {
1013
1071
  locator,
1014
1072
  key: record.key,
1015
1073
  record: storedRecord(record),
1016
- composition: compositionOf(record),
1074
+ composed: composedOf(composed(record)),
1017
1075
  })),
1018
1076
  });
1019
1077
  }
1020
1078
  catch {
1021
1079
  journal.record('model revocation persistence failed');
1022
1080
  }
1023
- const keys = new Set(models.keys());
1024
- for (const locator of rooms.keys()) {
1025
- keys.add(modelKey(locator, 'collection'));
1026
- keys.add(modelKey(locator, 'typed-selection'));
1027
- }
1081
+ const keys = new Set([...models.keys(), ...rooms.keys()]);
1028
1082
  for (const key of keys) {
1029
1083
  invalidModels.add(key);
1030
1084
  models.set(key, revoked);
@@ -1079,15 +1133,15 @@ export function composeRuntime(options) {
1079
1133
  ? null
1080
1134
  : {
1081
1135
  roots: selection.roots,
1082
- model: modelTypes(modelOf(room.locator, 'typed-selection')),
1136
+ model: modelTypes(builtIn),
1083
1137
  },
1084
1138
  providerOwnedPaths: room.providerOwnedPaths,
1085
1139
  roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
1086
1140
  synchronizationAvailable,
1087
- refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
1141
+ refusal: scopedRefusals.get(room.locator) ?? null,
1088
1142
  });
1089
1143
  }
1090
- async function applyDiscovery(found, started, covered) {
1144
+ async function applyDiscovery(found, started, left) {
1091
1145
  const lockedBefore = lockedRooms();
1092
1146
  for (const room of found) {
1093
1147
  const fence = fences.get(room.locator);
@@ -1101,18 +1155,11 @@ export function composeRuntime(options) {
1101
1155
  const listedLocators = new Set(listed.map((room) => room.locator));
1102
1156
  for (const locator of listedLocators)
1103
1157
  scopedRefusals.delete(locator);
1104
- if (covered === undefined)
1158
+ if (left === undefined)
1105
1159
  discovered.clear();
1106
- else
1107
- for (const locator of covered)
1108
- discovered.delete(locator);
1109
1160
  for (const room of listed)
1110
1161
  discovered.set(room.locator, room);
1111
- const unlisted = unlistedHeld(new Set(found.map((room) => room.locator))).filter((locator) => covered === undefined || covered.includes(locator));
1112
- if (options.adapter.exhaustiveDiscovery === true)
1113
- await loseRooms(unlisted);
1114
- else
1115
- unlistedKept(unlisted);
1162
+ await loseRooms(left ?? unlistedHeld(new Set(found.map((room) => room.locator))));
1116
1163
  for (const room of listed) {
1117
1164
  const held = replicas.get(room.locator)?.record;
1118
1165
  if (held !== undefined && held.replica.selection !== room.replica.selection) {
@@ -1143,15 +1190,6 @@ export function composeRuntime(options) {
1143
1190
  ]),
1144
1191
  ].filter((locator) => !listed.has(locator));
1145
1192
  }
1146
- function unlistedKept(unlisted) {
1147
- for (const locator of unlisted) {
1148
- const held = replicas.get(locator)?.record;
1149
- if (held !== undefined)
1150
- list(held, false);
1151
- else
1152
- rooms.delete(locator);
1153
- }
1154
- }
1155
1193
  function fence(locators) {
1156
1194
  const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
1157
1195
  const removed = fresh.map((locator) => ({
@@ -1162,29 +1200,29 @@ export function composeRuntime(options) {
1162
1200
  fences.set(locator, { at: discoveries, erased: false });
1163
1201
  lastFenced.set(locator, ++fencings);
1164
1202
  confirmed.delete(locator);
1165
- for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
1203
+ for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []) {
1166
1204
  pendings.delete(pendingKey(locator, batch.id));
1205
+ landedAhead.delete(pendingKey(locator, batch.id));
1206
+ }
1167
1207
  replicas.delete(locator);
1208
+ highest.delete(locator);
1209
+ beyondHorizon.delete(locator);
1168
1210
  queuesWithoutReplica.delete(locator);
1169
1211
  rooms.delete(locator);
1170
1212
  roomDrafts.delete(locator);
1171
- transfers.delete(locator);
1213
+ attachedFiles.delete(locator);
1214
+ untransferred(locator);
1215
+ startedOver.delete(locator);
1172
1216
  catching.delete(locator);
1173
1217
  discovered.delete(locator);
1174
- announced.delete(locator);
1175
1218
  unreadable.delete(locator);
1176
1219
  stale.delete(locator);
1177
1220
  repairable.delete(locator);
1178
1221
  unpreparable.delete(locator);
1179
1222
  scopedRefusals.delete(locator);
1180
- sourceRefusals.delete(locator);
1181
- declarationRefusals.delete(locator);
1182
- for (const attached of ['collection', 'typed-selection']) {
1183
- const key = modelKey(locator, attached);
1184
- models.delete(key);
1185
- liveModels.delete(key);
1186
- invalidModels.delete(key);
1187
- }
1223
+ models.delete(locator);
1224
+ liveModels.delete(locator);
1225
+ invalidModels.delete(locator);
1188
1226
  journal.record(`room lost ${locator}`);
1189
1227
  }
1190
1228
  if (fresh.length === 0)
@@ -1232,7 +1270,13 @@ export function composeRuntime(options) {
1232
1270
  });
1233
1271
  await eraseFenced(fresh);
1234
1272
  }
1235
- function restored(key, persisted) {
1273
+ function retyped(persisted) {
1274
+ if (persisted.status === 'unavailable')
1275
+ return persisted;
1276
+ return { ...persisted, types: schemaTypes(persisted.snapshot.schema, roomTypes) };
1277
+ }
1278
+ function restored(key, held) {
1279
+ const persisted = retyped(held);
1236
1280
  const prior = models.get(key);
1237
1281
  if (prior !== undefined &&
1238
1282
  prior.status !== 'unavailable' &&
@@ -1263,8 +1307,9 @@ export function composeRuntime(options) {
1263
1307
  const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
1264
1308
  targets = held.targets;
1265
1309
  transfers.clear();
1266
- for (const [locator, record] of held.transfers)
1267
- transfers.set(locator, record);
1310
+ arrivals.clear();
1311
+ streaming.clear();
1312
+ startedOver.clear();
1268
1313
  limits = held.providerLimits;
1269
1314
  capabilities = held.capabilities;
1270
1315
  member = held.member;
@@ -1274,14 +1319,16 @@ export function composeRuntime(options) {
1274
1319
  presentations.clear();
1275
1320
  for (const record of held.presentation)
1276
1321
  presentations.set(record.app, record.value);
1277
- conversations.hydrate(heldConversations, chatDrafts);
1322
+ conversations.hydrate(heldConversations, chatDrafts, held.conversationList, held.conversationCursor);
1278
1323
  pendings.clear();
1324
+ landedAhead.clear();
1279
1325
  for (const queue of queues) {
1280
1326
  for (const { batch, state } of queue.pending) {
1281
1327
  pendings.set(pendingKey(queue.locator, batch.id), state);
1282
1328
  }
1283
1329
  }
1284
1330
  replicas.clear();
1331
+ reopened.clear();
1285
1332
  queuesWithoutReplica.clear();
1286
1333
  for (const [locator, queue] of queuedBatches) {
1287
1334
  if (!held.rooms.has(locator))
@@ -1290,9 +1337,8 @@ export function composeRuntime(options) {
1290
1337
  records.forEach((stored, index) => {
1291
1338
  if (fences.has(stored.locator))
1292
1339
  return;
1293
- const model = restored(modelKey(stored.locator, 'collection'), stored.model);
1294
- const selectionModel = restored(modelKey(stored.locator, 'typed-selection'), stored.selectionModel);
1295
- const record = { ...stored, model, selectionModel };
1340
+ const record = { ...stored, model: restored(stored.locator, stored.model) };
1341
+ reopened.add(record.locator);
1296
1342
  replicas.set(record.locator, {
1297
1343
  record,
1298
1344
  queue: queuedBatches.get(record.locator) ?? [],
@@ -1303,25 +1349,28 @@ export function composeRuntime(options) {
1303
1349
  });
1304
1350
  const existing = rooms.get(record.locator);
1305
1351
  if (existing === undefined)
1306
- list(record, false);
1352
+ list(record, record.roomVersion !== null);
1307
1353
  else {
1308
1354
  rooms.set(record.locator, {
1309
1355
  ...existing,
1310
1356
  roomVersion: record.roomVersion,
1311
1357
  operations: record.operations,
1312
1358
  replica: record.replica,
1313
- model: modelTypes(model),
1359
+ model: modelTypes(record.model),
1314
1360
  typedSelection: record.typedSelection === null
1315
1361
  ? null
1316
- : {
1317
- roots: record.typedSelection.roots,
1318
- model: modelTypes(selectionModel),
1319
- },
1362
+ : { roots: record.typedSelection.roots, model: modelTypes(builtIn) },
1320
1363
  });
1321
1364
  }
1322
1365
  });
1323
- for (const key of held.orphans)
1324
- await opened.release(key);
1366
+ try {
1367
+ for (const key of held.orphans)
1368
+ await opened.release(key);
1369
+ }
1370
+ catch (error) {
1371
+ if (!refusedForSpace(error))
1372
+ throw error;
1373
+ }
1325
1374
  if (held.erasing || held.orphans.length > 0)
1326
1375
  queueMicrotask(sweep);
1327
1376
  return {
@@ -1392,24 +1441,41 @@ export function composeRuntime(options) {
1392
1441
  return opening;
1393
1442
  }
1394
1443
  function exclusive(work) {
1444
+ gathering = null;
1395
1445
  const next = mutations.then(work, work);
1396
1446
  mutations = next.then(() => undefined, () => undefined);
1397
1447
  return next;
1398
1448
  }
1449
+ function gathered(key, input, merge, work) {
1450
+ const tail = gathering;
1451
+ if (tail !== null && tail.key === key) {
1452
+ const merged = merge(tail.input, input);
1453
+ if (merged !== null) {
1454
+ tail.input = merged;
1455
+ return tail.done;
1456
+ }
1457
+ }
1458
+ const entry = { key, input, done: Promise.resolve() };
1459
+ entry.done = exclusive(() => {
1460
+ if (gathering === entry)
1461
+ gathering = null;
1462
+ return work(entry.input);
1463
+ });
1464
+ gathering = entry;
1465
+ return entry.done;
1466
+ }
1399
1467
  function withdrawable(locator) {
1400
1468
  return (batch) => {
1401
1469
  const state = pendings.get(pendingKey(locator, batch.id));
1402
1470
  return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
1403
1471
  };
1404
1472
  }
1405
- const REWRITE_LINKS = ['field', 'body'];
1406
- const CHECKED_LINKS = ['field', 'section'];
1407
1473
  async function sliceAt(record, view, queue, extra = [], moved = []) {
1408
1474
  const local = await openLocal();
1409
1475
  const needed = new Set([...touchedBy(queue), ...extra]);
1410
1476
  const movedPaths = [...new Set([...movedBy(queue, record.locator), ...moved])];
1411
1477
  if (movedPaths.length > 0) {
1412
- for (const { path } of await local.referrers(record.key, view, movedPaths, REWRITE_LINKS)) {
1478
+ for (const { path } of await local.referrers(record.key, view, movedPaths)) {
1413
1479
  needed.add(path);
1414
1480
  }
1415
1481
  }
@@ -1484,7 +1550,7 @@ export function composeRuntime(options) {
1484
1550
  return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0, indexed };
1485
1551
  }
1486
1552
  function replayed(record, confirmedLayer, queue) {
1487
- return stepped(replaying(confirmedLayer, queue, record.locator, compositionOf(record)));
1553
+ return stepped(replaying(confirmedLayer, queue, record.locator, composed(record)));
1488
1554
  }
1489
1555
  async function overlayDiff(record, before, visible, touched) {
1490
1556
  const kept = before !== undefined && before.record.key === record.key
@@ -1504,8 +1570,6 @@ export function composeRuntime(options) {
1504
1570
  function unpairedIn(locator, batch) {
1505
1571
  if (!queueIn(locator).some((held) => held.id === batch.id))
1506
1572
  return false;
1507
- if (pendings.get(pendingKey(locator, batch.id))?.accepted !== undefined)
1508
- return false;
1509
1573
  return !leavingIn(batch, locator) || withdrawable(locator)(batch);
1510
1574
  }
1511
1575
  async function together(local, settlements) {
@@ -1547,11 +1611,17 @@ export function composeRuntime(options) {
1547
1611
  const confirmedLayer = overrides === undefined
1548
1612
  ? read
1549
1613
  : await stepped(slicingWith(read, overrides.set, overrides.removed));
1550
- const composition = compositionOf(record);
1551
- const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
1552
- const founded = kept.filter((batch) => !queue.includes(batch));
1614
+ const composition = composed(record);
1615
+ const shown = queue.filter((batch) => {
1616
+ const held = landedAhead.get(pendingKey(locator, batch.id));
1617
+ return (held !== undefined &&
1618
+ Object.entries(held).some(([path, revision]) => confirmedLayer.revisions[path] !== revision));
1619
+ });
1620
+ const unshown = queue.filter((batch) => !shown.includes(batch));
1621
+ const { withdrawn, kept } = await stepped(standing(confirmedLayer, unshown, withdrawable(locator), localSaves, locator, composition));
1622
+ const founded = kept.filter((batch) => !unshown.includes(batch));
1553
1623
  const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
1554
- const unqueued = [];
1624
+ const unqueued = shown.map((batch) => ({ batch, landed: true }));
1555
1625
  for (const batch of withdrawn) {
1556
1626
  unqueued.push({ batch, landed: await landedWhole(locator, batch, confirmedLayer) });
1557
1627
  }
@@ -1590,6 +1660,7 @@ export function composeRuntime(options) {
1590
1660
  key: record.key,
1591
1661
  record: storedRecord(record),
1592
1662
  roots: record.typedSelection?.roots ?? [],
1663
+ composed: composedOf(composition),
1593
1664
  ...settling.commit,
1594
1665
  ...(overrides === undefined
1595
1666
  ? {}
@@ -1615,8 +1686,10 @@ export function composeRuntime(options) {
1615
1686
  commit,
1616
1687
  apply: (answer) => {
1617
1688
  for (const batch of previousQueue) {
1618
- if (!staying.has(batch.id))
1619
- pendings.delete(pendingKey(locator, batch.id));
1689
+ if (staying.has(batch.id))
1690
+ continue;
1691
+ pendings.delete(pendingKey(locator, batch.id));
1692
+ landedAhead.delete(pendingKey(locator, batch.id));
1620
1693
  }
1621
1694
  for (const draft of written.values())
1622
1695
  rememberRoomDraft(draft);
@@ -1653,69 +1726,60 @@ export function composeRuntime(options) {
1653
1726
  };
1654
1727
  }
1655
1728
  function adoptModels(locator, record) {
1656
- for (const [attached, model] of [
1657
- ['collection', record.model],
1658
- ['typed-selection', record.selectionModel],
1659
- ]) {
1660
- const key = modelKey(locator, attached);
1661
- models.set(key, model);
1662
- invalidModels.delete(key);
1663
- if (model.status === 'current')
1664
- liveModels.set(key, JSON.stringify(model.snapshot));
1665
- else if (model.status === 'unavailable')
1666
- liveModels.delete(key);
1667
- }
1729
+ const model = record.model;
1730
+ models.set(locator, model);
1731
+ invalidModels.delete(locator);
1732
+ if (model.status === 'current')
1733
+ liveModels.set(locator, JSON.stringify(model.snapshot));
1734
+ else if (model.status === 'unavailable')
1735
+ liveModels.delete(locator);
1668
1736
  }
1669
1737
  function remodel(locator, answers, held) {
1670
- const taken = (answer, previous, attached) => {
1671
- if (answer === undefined)
1672
- return previous;
1673
- const { state, refused } = attachedModel(answer, previous, attached === 'collection' ? roomTypes : NO_ROOM_TYPES);
1674
- if (refused !== null) {
1675
- const where = refused.location === undefined ? '' : ` ${refused.location}`;
1676
- journal.record(`model invalid ${locator} ${attached} ${refused.identity} ${refused.rule}${where}`);
1677
- }
1678
- models.set(modelKey(locator, attached), state);
1679
- return state;
1680
- };
1681
- return {
1682
- collection: taken(answers?.collection, held.collection, 'collection'),
1683
- selection: taken(answers?.selection, held.selection, 'typed-selection'),
1684
- };
1738
+ const answer = answers?.collection;
1739
+ if (answer === undefined)
1740
+ return held;
1741
+ const state = attachedModel(answer, roomTypes);
1742
+ models.set(locator, state);
1743
+ return { collection: state };
1685
1744
  }
1686
- function publish(local, room, record, landing, live) {
1745
+ function publish(local, room, reached, landing, live) {
1687
1746
  const landed = exclusive(async () => {
1688
1747
  if (!live()) {
1689
1748
  caughtUpTo(room.locator);
1690
1749
  return true;
1691
1750
  }
1692
1751
  const prior = replicas.get(room.locator);
1693
- if (landing.kind === 'refreshed' &&
1694
- (prior === undefined || prior.record.key !== record.key || prior.confirmed !== landing.base)) {
1695
- await local.unrefresh(record.key);
1752
+ if (!stands(room.locator, landing.from)) {
1753
+ if (landing.kind === 'refreshed') {
1754
+ await local.unrefresh(reached.key);
1755
+ return false;
1756
+ }
1757
+ caughtUpTo(room.locator);
1758
+ await abandoned(local, room.locator);
1696
1759
  return false;
1697
1760
  }
1761
+ const record = landing.kind === 'refreshed' && prior !== undefined
1762
+ ? {
1763
+ ...prior.record,
1764
+ roomVersion: reached.roomVersion,
1765
+ ...(landing.remodelled ? { model: reached.model } : {}),
1766
+ }
1767
+ : reached;
1698
1768
  adoptModels(room.locator, record);
1699
1769
  const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
1700
- const roomVersion = record.roomVersion;
1701
- const accepted = queue.filter((batch) => {
1702
- const at = pendings.get(pendingKey(room.locator, batch.id))?.accepted;
1703
- return at !== undefined && options.adapter.reached(room.locator, roomVersion, at);
1704
- });
1705
1770
  let set;
1706
1771
  try {
1707
- set = await settle(local, room.locator, prior, record, queue.filter((batch) => !accepted.includes(batch)), landing.kind === 'refreshed'
1772
+ set = await settle(local, room.locator, prior, record, queue, landing.kind === 'refreshed'
1708
1773
  ? {
1709
1774
  room: true,
1710
1775
  view: 'refreshed',
1711
- commit: { refreshed: true, composition: compositionOf(record) },
1776
+ commit: { refreshed: true },
1712
1777
  }
1713
1778
  : {
1714
1779
  room: true,
1715
1780
  view: landing.refreshed ? 'refreshed' : 'confirmed',
1716
1781
  commit: {
1717
1782
  published: { from: prior?.record.key ?? null },
1718
- composition: compositionOf(record),
1719
1783
  ...(landing.refreshed ? { refreshed: true } : {}),
1720
1784
  },
1721
1785
  });
@@ -1727,24 +1791,24 @@ export function composeRuntime(options) {
1727
1791
  return true;
1728
1792
  }
1729
1793
  if (landing.kind === 'bootstrapped') {
1730
- transfers.delete(room.locator);
1794
+ highest.delete(room.locator);
1795
+ beyondHorizon.delete(room.locator);
1796
+ untransferred(room.locator);
1797
+ startedOver.delete(room.locator);
1731
1798
  catching.delete(room.locator);
1732
- sweep();
1799
+ if (prior !== undefined && prior.record.key !== record.key)
1800
+ sweep();
1801
+ name(room.locator);
1733
1802
  }
1734
- for (const batch of accepted)
1735
- pendings.delete(pendingKey(room.locator, batch.id));
1736
1803
  stale.delete(room.locator);
1737
1804
  unpreparable.delete(room.locator);
1738
1805
  if (unreadable.delete(room.locator))
1739
1806
  scopedRefusals.delete(room.locator);
1740
- sourceRefusals.delete(room.locator);
1741
- declarationRefusals.delete(room.locator);
1742
1807
  list(room, true);
1743
1808
  publishChangeSet(set);
1744
1809
  await unpair(local);
1745
1810
  moveTo({ ...status, ...attention() });
1746
- options.adapter.roomStream?.published(room.locator, roomVersion);
1747
- if (accepted.length > 0 && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
1811
+ if (landing.kind === 'bootstrapped' && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
1748
1812
  submitLater(room.locator);
1749
1813
  }
1750
1814
  return true;
@@ -1756,7 +1820,7 @@ export function composeRuntime(options) {
1756
1820
  const replica = replicas.get(locator);
1757
1821
  const room = rooms.get(locator);
1758
1822
  if (replica === undefined || room === undefined) {
1759
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1823
+ throw runtimeError('unknown-room', `Unknown Room ${locator}`);
1760
1824
  }
1761
1825
  return { local, replica, room };
1762
1826
  }
@@ -1774,12 +1838,7 @@ export function composeRuntime(options) {
1774
1838
  return committed(locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
1775
1839
  }
1776
1840
  async function saveParts(locator, named, writtenBy) {
1777
- const read = named.map(({ path, parts }) => ({ path, parts: readParts(parts) }));
1778
- if (read.some(({ path }, at) => path === '' ||
1779
- normalize(path) !== path ||
1780
- read.findIndex((one) => one.path === path) !== at)) {
1781
- throw runtimeError('room-draft-malformed');
1782
- }
1841
+ const read = readSavedParts(named);
1783
1842
  const target = await savedRoom(locator);
1784
1843
  const saved = [];
1785
1844
  const files = [];
@@ -1788,7 +1847,7 @@ export function composeRuntime(options) {
1788
1847
  for (const { path, parts } of read) {
1789
1848
  const file = parts.file === undefined
1790
1849
  ? undefined
1791
- : await heldFile(parts.file.blob, binaryBase(visible, path));
1850
+ : await heldFile(parts.file.blob, parts.file.base === undefined ? binaryBase(visible, path) : parts.file.base);
1792
1851
  if (parts.file !== undefined && file !== undefined) {
1793
1852
  files.push({ key: file.localId, blob: parts.file.blob });
1794
1853
  }
@@ -1822,14 +1881,15 @@ export function composeRuntime(options) {
1822
1881
  const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
1823
1882
  const referring = removed.length === 0
1824
1883
  ? []
1825
- : (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
1826
- const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...referring], moved);
1884
+ : (await local.referrers(record.key, 'confirmed', removed)).map(({ path }) => path);
1885
+ const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
1827
1886
  const { visible } = await replayed(record, confirmedLayer, replica.queue);
1828
- const composition = compositionOf(record);
1887
+ const composition = composed(record);
1829
1888
  return {
1830
1889
  record,
1831
1890
  confirmed: confirmedLayer,
1832
1891
  visible,
1892
+ folders: new Set(await local.folders(record.key, 'visible', paths)),
1833
1893
  *visibleOver(queue) {
1834
1894
  return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
1835
1895
  },
@@ -1841,13 +1901,7 @@ export function composeRuntime(options) {
1841
1901
  return false;
1842
1902
  const local = await openLocal();
1843
1903
  const roots = record.typedSelection?.roots ?? [];
1844
- if (composition.model.status === 'unavailable' &&
1845
- (await local.typedWithin(record.key, 'visible', roots, false))) {
1846
- return true;
1847
- }
1848
- return (composition.selection !== null &&
1849
- composition.selection.model.status === 'unavailable' &&
1850
- (await local.typedWithin(record.key, 'visible', roots, true)));
1904
+ return local.typedWithin(record.key, 'visible', roots, false);
1851
1905
  }
1852
1906
  function textDiffers(left, right) {
1853
1907
  return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
@@ -1858,18 +1912,6 @@ export function composeRuntime(options) {
1858
1912
  return undefined;
1859
1913
  return entry.binary !== null ? null : entry.content;
1860
1914
  }
1861
- function walkOf(local, key, view) {
1862
- return async (after) => {
1863
- const page = await local.walk(key, view, after, WALK_BYTES);
1864
- return {
1865
- entries: page.entries.map(({ path, content, binary }) => ({
1866
- path,
1867
- content: binary !== null ? null : content,
1868
- })),
1869
- next: page.next,
1870
- };
1871
- };
1872
- }
1873
1915
  function lookupOf(local, key, view) {
1874
1916
  return async (paths) => {
1875
1917
  if (paths.length === 0)
@@ -1886,37 +1928,6 @@ export function composeRuntime(options) {
1886
1928
  const read = parseFrontmatter(entry.content ?? '');
1887
1929
  return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
1888
1930
  }
1889
- function overriddenWalk(walk, overrides) {
1890
- const sorted = [...overrides.keys()].sort();
1891
- const beyond = (path) => {
1892
- if (path === null)
1893
- return 0;
1894
- let low = 0;
1895
- let high = sorted.length;
1896
- while (low < high) {
1897
- const middle = (low + high) >>> 1;
1898
- if (sorted[middle] <= path)
1899
- low = middle + 1;
1900
- else
1901
- high = middle;
1902
- }
1903
- return low;
1904
- };
1905
- return async (after) => {
1906
- const page = await walk(after);
1907
- const end = page.next;
1908
- const added = sorted
1909
- .slice(beyond(after), end === null ? sorted.length : beyond(end))
1910
- .flatMap((path) => {
1911
- const entry = overrides.get(path);
1912
- return entry === null || entry === undefined
1913
- ? []
1914
- : [{ path, content: entry.binary !== null ? null : entry.content }];
1915
- });
1916
- const entries = [...page.entries.filter(({ path }) => !overrides.has(path)), ...added].sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
1917
- return { entries, next: end };
1918
- };
1919
- }
1920
1931
  function overriddenLookup(lookup, overrides) {
1921
1932
  return async (paths) => {
1922
1933
  const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
@@ -1934,9 +1945,8 @@ export function composeRuntime(options) {
1934
1945
  }
1935
1946
  function validatorOf(record, confirmedLayer) {
1936
1947
  return async (resulting, touched) => {
1937
- const composition = compositionFor(record);
1938
1948
  const local = await openLocal();
1939
- const changed = [];
1949
+ const changes = [];
1940
1950
  const overrides = new Map();
1941
1951
  const pace = paced();
1942
1952
  for (const path of new Set([...confirmedLayer.held, ...touched])) {
@@ -1947,19 +1957,13 @@ export function composeRuntime(options) {
1947
1957
  continue;
1948
1958
  overrides.set(path, now);
1949
1959
  if (textOf(was) !== textOf(now))
1950
- changed.push({ path, was: textOf(was), now: textOf(now) });
1960
+ changes.push({ path, was: textOf(was), now: textOf(now) });
1951
1961
  }
1952
- const lookup = overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides);
1953
- const clean = available(composition) &&
1954
- changed.length <= CLEAN_CHANGES &&
1955
- (await changedCleanly(changed, compositionOf(record), composition, {
1956
- lookup,
1957
- texts: textsOf(local, record.key, 'confirmed'),
1958
- referrers: referrersOf(local, record.key, 'confirmed'),
1959
- }));
1960
- if (clean)
1961
- return { verdict: 'accept' };
1962
- return validatedCollection(overriddenWalk(walkOf(local, record.key, 'confirmed'), overrides), lookup, composition);
1962
+ return validatedNeighbourhood(changes, compositionFor(record), {
1963
+ lookup: overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides),
1964
+ texts: textsOf(local, record.key, 'confirmed'),
1965
+ referrers: referrersOf(local, record.key, 'confirmed'),
1966
+ });
1963
1967
  };
1964
1968
  }
1965
1969
  function textsOf(local, key, view) {
@@ -1970,7 +1974,7 @@ export function composeRuntime(options) {
1970
1974
  }
1971
1975
  function referrersOf(local, key, view) {
1972
1976
  return async (targets) => [
1973
- ...new Set((await local.referrers(key, view, targets, CHECKED_LINKS)).map(({ path }) => path)),
1977
+ ...new Set((await local.referrers(key, view, targets)).map(({ path }) => path)),
1974
1978
  ];
1975
1979
  }
1976
1980
  async function sideOf(locator, replica, room, saving) {
@@ -1983,6 +1987,7 @@ export function composeRuntime(options) {
1983
1987
  operations: room.operations,
1984
1988
  providerOwnedPaths: room.providerOwnedPaths,
1985
1989
  visible: saving.visible,
1990
+ folders: saving.folders,
1986
1991
  queued: queuedDocuments(replica.queue),
1987
1992
  unmodeled: await unmodeledIn(record),
1988
1993
  validate: validatorOf(record, saving.confirmed),
@@ -2003,6 +2008,7 @@ export function composeRuntime(options) {
2003
2008
  locator,
2004
2009
  key: saving.record.key,
2005
2010
  roots: saving.record.typedSelection?.roots ?? [],
2011
+ composed: composedOf(composed(saving.record)),
2006
2012
  overlay: diff.overlay,
2007
2013
  });
2008
2014
  }
@@ -2034,13 +2040,13 @@ export function composeRuntime(options) {
2034
2040
  }
2035
2041
  const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
2036
2042
  const sourceSaving = await savingIn(replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
2037
- const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, sourceSaving.visible)));
2043
+ const changed = saved.flatMap(({ path, parts }) => changedParts(path, parts, sourceSaving.visible));
2038
2044
  if (changed.length > 0)
2039
2045
  return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
2040
2046
  const target = replicas.get(destination);
2041
2047
  const listedRoom = rooms.get(destination);
2042
2048
  if (target === undefined || listedRoom === undefined) {
2043
- throw runtimeError('room-not-held', `No Local Replica for ${destination}`);
2049
+ throw runtimeError('unknown-room', `Unknown Room ${destination}`);
2044
2050
  }
2045
2051
  const arriving = await savingIn(target, [], moves.map(({ to }) => to));
2046
2052
  const built = await buildMove({
@@ -2103,10 +2109,10 @@ export function composeRuntime(options) {
2103
2109
  return { outcome: 'refused', code: 'durable-write', retryable: true };
2104
2110
  const { answer, touched } = committedBoth;
2105
2111
  if (answer.fenced !== null) {
2106
- throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
2112
+ throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
2107
2113
  }
2108
2114
  if (fences.has(locator) || fences.has(destination)) {
2109
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
2115
+ throw runtimeError('unknown-room', `Unknown Room ${locator}`);
2110
2116
  }
2111
2117
  pendings.set(pendingKey(locator, id), {
2112
2118
  drafts: saved,
@@ -2159,7 +2165,7 @@ export function composeRuntime(options) {
2159
2165
  ...saved.map(({ path }) => path),
2160
2166
  ...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
2161
2167
  ]);
2162
- const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, underneath(held, path, saving.visible)));
2168
+ const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, held, saving.visible));
2163
2169
  if (changed.length > 0)
2164
2170
  return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
2165
2171
  const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(locator), saving.visibleOver));
@@ -2187,6 +2193,7 @@ export function composeRuntime(options) {
2187
2193
  providerLimits: limits,
2188
2194
  handle: room.handle,
2189
2195
  visible: saving.visible,
2196
+ folders: saving.folders,
2190
2197
  changes,
2191
2198
  bases: parts.flatMap(({ bases }) => bases),
2192
2199
  queued: queuedDocuments(replica.queue),
@@ -2276,10 +2283,10 @@ export function composeRuntime(options) {
2276
2283
  return { outcome: 'refused', code: 'durable-write', retryable: true };
2277
2284
  const { answer, touched } = done;
2278
2285
  if (answer.fenced !== null) {
2279
- throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
2286
+ throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
2280
2287
  }
2281
2288
  if (fences.has(locator))
2282
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
2289
+ throw runtimeError('unknown-room', `Unknown Room ${locator}`);
2283
2290
  for (const origin of withdrawn.keys())
2284
2291
  pendings.delete(pendingKey(locator, origin));
2285
2292
  for (const [id, drafts] of drafted) {
@@ -2328,7 +2335,7 @@ export function composeRuntime(options) {
2328
2335
  throw new Error('connection refused');
2329
2336
  await local.keepUpload(locator, identifier, upload);
2330
2337
  if (fences.has(locator))
2331
- throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
2338
+ throw runtimeError('unknown-room', `Unknown Room ${locator}`);
2332
2339
  const key = pendingKey(locator, identifier);
2333
2340
  pendings.set(key, { ...pendings.get(key), upload });
2334
2341
  if (!continuing())
@@ -2337,6 +2344,9 @@ export function composeRuntime(options) {
2337
2344
  async function readHistory(locator, path, query = {}) {
2338
2345
  if (closed || refusal !== null)
2339
2346
  return UNCERTAIN;
2347
+ await firstDiscovery;
2348
+ if (!roomsNamed)
2349
+ return UNCERTAIN;
2340
2350
  try {
2341
2351
  await ensureAuthorization();
2342
2352
  }
@@ -2429,6 +2439,9 @@ export function composeRuntime(options) {
2429
2439
  async function transferBinary(request, transfer, answer) {
2430
2440
  if (closed || refusal !== null)
2431
2441
  return UNCERTAIN;
2442
+ await firstDiscovery;
2443
+ if (!roomsNamed)
2444
+ return UNCERTAIN;
2432
2445
  try {
2433
2446
  await ensureAuthorization();
2434
2447
  }
@@ -2459,23 +2472,27 @@ export function composeRuntime(options) {
2459
2472
  return UNCERTAIN;
2460
2473
  }
2461
2474
  }
2475
+ function fetchBinary(request, name) {
2476
+ return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
2477
+ }
2462
2478
  async function readBinary(request, name) {
2463
2479
  if (closed)
2464
2480
  return UNCERTAIN;
2465
2481
  const held = await keptFiles.find(name);
2466
2482
  if (held !== null)
2467
2483
  return held;
2468
- return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
2484
+ return fetchBinary(request, name);
2469
2485
  }
2470
2486
  async function binaryPath(locator, path, revision) {
2471
2487
  if (revision !== undefined)
2472
2488
  return { kind: 'path', locator, path, revision };
2473
2489
  const replica = replicas.get(locator);
2474
- if (replica === undefined)
2475
- return null;
2476
2490
  const local = await openLocal();
2477
- const [held] = await local.entries(replica.record.key, 'visible', [path]);
2478
- if (held?.binary === undefined || held.binary === null)
2491
+ const [held] = replica === undefined ? [] : await local.entries(replica.record.key, 'visible', [path]);
2492
+ const attached = held === undefined ? attachedFiles.get(locator)?.get(path) : undefined;
2493
+ if (attached !== undefined)
2494
+ return { kind: 'path', locator, path, revision: attached.revision };
2495
+ if (replica === undefined || held?.binary === undefined || held.binary === null)
2479
2496
  return null;
2480
2497
  if (held.binary.state === 'pending')
2481
2498
  return { kind: 'pending', localId: held.binary.localId };
@@ -2507,33 +2524,83 @@ export function composeRuntime(options) {
2507
2524
  revision: record.revision,
2508
2525
  };
2509
2526
  }
2510
- async function readBinaryPath(locator, path, revision) {
2527
+ async function askedBinary(locator, path, revision) {
2511
2528
  await ready;
2512
2529
  const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
2513
- const asked = drafted !== undefined && drafted.localId === revision
2530
+ return drafted !== undefined && drafted.localId === revision
2514
2531
  ? { kind: 'pending', localId: revision }
2515
- : await binaryPath(locator, path, revision);
2532
+ : binaryPath(locator, path, revision);
2533
+ }
2534
+ function pendingBinary(locator, path, localId) {
2535
+ const facts = queuedFacts(locator, path, localId);
2536
+ return facts === null
2537
+ ? absentBinary(path)
2538
+ : {
2539
+ kind: 'binary',
2540
+ file: localId,
2541
+ byteLength: facts.byteLength,
2542
+ mediaType: facts.mediaType,
2543
+ };
2544
+ }
2545
+ async function readBinaryPath(locator, path, revision) {
2546
+ const asked = await askedBinary(locator, path, revision);
2516
2547
  if (asked === null)
2517
2548
  return absentBinary(path);
2518
- if (asked.kind === 'pending') {
2519
- const facts = queuedFacts(locator, path, asked.localId);
2520
- return facts === null
2521
- ? absentBinary(path)
2522
- : {
2523
- kind: 'binary',
2524
- file: asked.localId,
2525
- byteLength: facts.byteLength,
2526
- mediaType: facts.mediaType,
2527
- };
2528
- }
2549
+ if (asked.kind === 'pending')
2550
+ return pendingBinary(locator, path, asked.localId);
2529
2551
  return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
2530
2552
  }
2553
+ async function heldMediaType(locator, path) {
2554
+ const replica = replicas.get(locator);
2555
+ const [held] = replica === undefined
2556
+ ? []
2557
+ : await (await openLocal()).entries(replica.record.key, 'visible', [path]);
2558
+ return held === undefined
2559
+ ? attachedFiles.get(locator)?.get(path)?.mediaType
2560
+ : held.binary?.mediaType;
2561
+ }
2562
+ function linkOf(asked, held) {
2563
+ return transferBinary(asked, (authorization) => options.adapter.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
2564
+ }
2531
2565
  async function linkBinaryPath(locator, path, revision) {
2532
2566
  await ready;
2533
2567
  const asked = await binaryPath(locator, path, revision);
2534
2568
  if (asked === null || asked.kind === 'pending')
2535
2569
  return absentBinary(path);
2536
- return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
2570
+ return linkOf(asked, revision === undefined ? await heldMediaType(locator, path) : undefined);
2571
+ }
2572
+ async function showBinaryPath(locator, path, revision) {
2573
+ const asked = await askedBinary(locator, path, revision);
2574
+ if (asked === null)
2575
+ return absentBinary(path);
2576
+ if (asked.kind === 'pending')
2577
+ return pendingBinary(locator, path, asked.localId);
2578
+ if (closed)
2579
+ return UNCERTAIN;
2580
+ const name = { locator, path: asked.path, revision: asked.revision };
2581
+ const kept = await keptFiles.find(name);
2582
+ if (kept !== null)
2583
+ return kept;
2584
+ const mediaType = await heldMediaType(locator, path);
2585
+ if (mediaType?.startsWith('image/') === true)
2586
+ return fetchBinary(asked, name);
2587
+ return linkOf(asked, revision === undefined ? mediaType : undefined);
2588
+ }
2589
+ async function attachBinary(locator, file) {
2590
+ await ready;
2591
+ await firstDiscovery;
2592
+ if (closed || !roomsNamed || connectionOf() === 'unreachable' || refusal !== null)
2593
+ return OFFLINE;
2594
+ const request = await attachedFile(locator, file);
2595
+ return transferBinary({ locator, path: file.name }, async (held) => {
2596
+ const result = await options.adapter.attach(held, request);
2597
+ if (result.kind === 'attached') {
2598
+ const landed = attachedFiles.get(locator) ?? new Map();
2599
+ landed.set(result.path, { revision: result.revision, mediaType: request.mediaType });
2600
+ attachedFiles.set(locator, landed);
2601
+ }
2602
+ return result;
2603
+ }, attachAnswer);
2537
2604
  }
2538
2605
  function readPart(file, part) {
2539
2606
  return database.ask({ kind: 'file.part', key: file, part });
@@ -2542,13 +2609,6 @@ export function composeRuntime(options) {
2542
2609
  const freed = await database.ask({ kind: 'opened.clean' });
2543
2610
  return freed + ((await options.shell.cleanUp?.()) ?? 0);
2544
2611
  }
2545
- function declaredAgents() {
2546
- for (const capability of capabilities) {
2547
- if (capability.conversationAgents !== undefined)
2548
- return capability.conversationAgents;
2549
- }
2550
- return undefined;
2551
- }
2552
2612
  function declaredAction(name) {
2553
2613
  for (const capability of capabilities) {
2554
2614
  for (const action of capability.actions ?? []) {
@@ -2600,6 +2660,24 @@ export function composeRuntime(options) {
2600
2660
  return UNCERTAIN;
2601
2661
  }
2602
2662
  }
2663
+ async function readConversationHistory(request) {
2664
+ await ready;
2665
+ const stream = options.adapter.conversations;
2666
+ if (closed || refusal !== null || stream?.history === undefined)
2667
+ return UNCERTAIN;
2668
+ journal.record('transfer conversation history');
2669
+ try {
2670
+ const result = await stream.history(request);
2671
+ if (result.kind === 'revoked-authorization')
2672
+ await refuse('revoked-authorization');
2673
+ journal.record(`outcome conversation history ${result.kind}`);
2674
+ return calledResult(result);
2675
+ }
2676
+ catch {
2677
+ journal.record('outcome conversation history transport-uncertainty');
2678
+ return UNCERTAIN;
2679
+ }
2680
+ }
2603
2681
  function unsettled(result) {
2604
2682
  return result.kind !== 'action' && result.retryable;
2605
2683
  }
@@ -2638,10 +2716,6 @@ export function composeRuntime(options) {
2638
2716
  continue;
2639
2717
  if (closed || signal.aborted)
2640
2718
  return 'done';
2641
- if (pastHorizon(held.operation)) {
2642
- await local.dropCall(held.operation);
2643
- continue;
2644
- }
2645
2719
  calling.add(held.operation);
2646
2720
  try {
2647
2721
  if (unsettled(await invokeAction(held.name, held.input)))
@@ -2661,6 +2735,33 @@ export function composeRuntime(options) {
2661
2735
  const held = replicas.get(locator);
2662
2736
  return (held !== undefined && options.adapter.reached(locator, held.record.roomVersion, roomVersion));
2663
2737
  }
2738
+ function shownAt(locator) {
2739
+ const held = replicas.get(locator)?.record.roomVersion;
2740
+ const raised = highest.get(locator);
2741
+ if (held === undefined || raised === undefined)
2742
+ return held ?? raised;
2743
+ return options.adapter.reached(locator, held, raised) ? held : raised;
2744
+ }
2745
+ function showing(locator, roomVersion) {
2746
+ const at = shownAt(locator);
2747
+ if (at === undefined || !options.adapter.reached(locator, at, roomVersion)) {
2748
+ highest.set(locator, roomVersion);
2749
+ }
2750
+ }
2751
+ function older(locator, roomVersion) {
2752
+ if (beyondHorizon.has(locator))
2753
+ return false;
2754
+ const at = shownAt(locator);
2755
+ return (at !== undefined &&
2756
+ options.adapter.reached(locator, at, roomVersion) &&
2757
+ !options.adapter.reached(locator, roomVersion, at));
2758
+ }
2759
+ function replaced(locators) {
2760
+ for (const locator of locators) {
2761
+ highest.delete(locator);
2762
+ beyondHorizon.add(locator);
2763
+ }
2764
+ }
2664
2765
  async function writtenAfter(locator, roomVersion, path) {
2665
2766
  const held = replicas.get(locator);
2666
2767
  if (held === undefined)
@@ -2671,122 +2772,34 @@ export function composeRuntime(options) {
2671
2772
  options.adapter.reached(locator, written, roomVersion) &&
2672
2773
  !options.adapter.reached(locator, roomVersion, written));
2673
2774
  }
2674
- function overriddenTexts(texts, overrides) {
2675
- return async (paths) => {
2676
- const known = new Map(await texts(paths.filter((path) => !overrides.has(path))));
2677
- for (const path of paths) {
2678
- const entry = overrides.get(path);
2679
- if (entry === undefined)
2680
- continue;
2681
- if (entry === null)
2682
- known.delete(path);
2683
- else
2684
- known.set(path, entry.binary !== null ? null : entry.content);
2685
- }
2686
- return known;
2687
- };
2688
- }
2689
- async function unreadableRoom(texts, walk, paths) {
2690
- if (paths !== null) {
2691
- const text = {};
2692
- for (const [path, content] of await texts(paths))
2693
- if (content !== null)
2694
- text[path] = content;
2695
- return stepped(unreadableIn(text, Object.keys(text)));
2696
- }
2697
- for (let after = null;;) {
2698
- const page = await walk(after);
2699
- const text = {};
2700
- for (const { path, content } of page.entries)
2701
- if (content !== null)
2702
- text[path] = content;
2703
- const refused = await stepped(unreadableIn(text, Object.keys(text)));
2704
- if (refused !== null)
2705
- return refused;
2706
- if (page.next === null)
2707
- return null;
2708
- after = page.next;
2709
- }
2710
- }
2711
- async function validatedRoom(local, record, view, previous, overrides = null) {
2712
- const composition = compositionOf(record);
2713
- const modeled = record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable';
2714
- const lookup = overrides === null
2715
- ? lookupOf(local, record.key, view)
2716
- : overriddenLookup(lookupOf(local, record.key, view), overrides);
2717
- const texts = overrides === null
2718
- ? textsOf(local, record.key, view)
2719
- : overriddenTexts(textsOf(local, record.key, view), overrides);
2720
- const walk = overrides === null
2721
- ? walkOf(local, record.key, view)
2722
- : overriddenWalk(walkOf(local, record.key, view), overrides);
2723
- let changes = null;
2724
- let reshaped = false;
2725
- if (previous !== null && previous.changed.length <= CLEAN_CHANGES) {
2726
- const [was, now] = await Promise.all([
2727
- local.entries(record.key, 'confirmed', previous.changed),
2728
- overrides === null
2729
- ? local.entries(record.key, view, previous.changed)
2730
- : Promise.resolve([]),
2731
- ]);
2732
- const before = new Map(was.map((entry) => [entry.path, entry]));
2733
- const after = overrides === null
2734
- ? new Map(now.map((entry) => [entry.path, entry]))
2735
- : overrides;
2736
- changes = previous.changed.flatMap((path) => {
2737
- const one = before.get(path) ?? null;
2738
- const other = after.get(path) ?? null;
2739
- reshaped ||= textDiffers(one, other);
2740
- const held = textOf(one);
2741
- const next = textOf(other);
2742
- return held === next ? [] : [{ path, was: held, now: next }];
2743
- });
2744
- }
2745
- const named = changes === null ? null : changes.map(({ path }) => path);
2746
- const textChanged = changes === null || changes.length > 0 || reshaped;
2747
- const modelsChanged = previous === null || !sameComposition(compositionOf(previous.record), composition);
2748
- if (modeled && (textChanged || modelsChanged)) {
2749
- const clean = previous !== null &&
2750
- changes !== null &&
2751
- (await changedCleanly(changes, compositionOf(previous.record), composition, {
2752
- lookup,
2753
- texts,
2754
- referrers: referrersOf(local, record.key, 'confirmed'),
2755
- }));
2756
- if (clean)
2757
- return null;
2758
- const verdict = await validatedCollection(walk, lookup, composition);
2759
- if (verdict.verdict === 'accept')
2760
- return null;
2761
- if (verdict.rule !== 'model-unavailable')
2762
- return { kind: 'refused', refusal: verdict };
2763
- return unreadableRoom(texts, walk, named);
2764
- }
2765
- if (textChanged)
2766
- return unreadableRoom(texts, walk, named);
2767
- return null;
2775
+ async function writtenLater(local, locator, key, paths, roomVersion) {
2776
+ if (paths.length === 0 || !older(locator, roomVersion))
2777
+ return new Set();
2778
+ const entries = await local.entries(key, 'confirmed', paths);
2779
+ return new Set(entries.flatMap(({ path, written }) => written !== null &&
2780
+ options.adapter.reached(locator, written, roomVersion) &&
2781
+ !options.adapter.reached(locator, roomVersion, written)
2782
+ ? [path]
2783
+ : []));
2768
2784
  }
2769
- function refreshingOf(local, key, liveness, replaying = null) {
2770
- const write = replaying === null ? exclusive : installWrite;
2785
+ function refreshingOf(local, locator, key, composition, liveness, replaying = false) {
2786
+ const write = replaying ? finishingWrite : exclusive;
2771
2787
  return {
2772
- async turn() {
2773
- if (replaying === null)
2774
- return;
2775
- installing(replaying, 'replaying');
2776
- await screened();
2788
+ turn() {
2789
+ if (replaying)
2790
+ installing(locator);
2777
2791
  },
2778
2792
  prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
2793
+ later: (paths, roomVersion) => writtenLater(local, locator, key, paths, roomVersion),
2779
2794
  async stage(page) {
2780
2795
  for (let written = 0;;) {
2781
- if (replaying !== null)
2782
- await screened();
2783
2796
  const end = batchEnd(page.entries, written);
2784
2797
  const last = end === page.entries.length;
2785
2798
  const entries = page.entries.slice(written, end);
2786
2799
  const staged = await write(async () => {
2787
2800
  if (!liveness())
2788
2801
  return false;
2789
- await timed(() => local.refresh(key, last
2802
+ await timed(stageBudget, () => local.refresh(key, composition, last
2790
2803
  ? { ...page, entries }
2791
2804
  : { entries, removals: [], folders: [], folderRemovals: [] }));
2792
2805
  return true;
@@ -2805,94 +2818,180 @@ export function composeRuntime(options) {
2805
2818
  let spent = 0;
2806
2819
  while (end < entries.length) {
2807
2820
  const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
2808
- if (spent + size > stageBytes && end > from)
2821
+ if (spent + size > stageBudget.bytes && end > from)
2809
2822
  break;
2810
2823
  spent += size;
2811
2824
  end += 1;
2812
2825
  }
2813
2826
  return end;
2814
2827
  }
2815
- async function timed(work) {
2828
+ async function timed(budget, work) {
2816
2829
  const started = performance.now();
2817
2830
  const done = await work();
2818
2831
  const spent = performance.now() - started;
2819
- if (spent < STAGE_AIM_MS / 2)
2820
- stageBytes = Math.min(STAGE_BYTES, Math.round(stageBytes * 1.5));
2821
- else if (spent > STAGE_AIM_MS * 2) {
2822
- stageBytes = Math.max(STAGE_FLOOR, Math.round(stageBytes / 1.5));
2832
+ if (spent < budget.aim / 2)
2833
+ budget.bytes = Math.min(budget.ceiling, Math.round(budget.bytes * 1.5));
2834
+ else if (spent > budget.aim * 2) {
2835
+ budget.bytes = Math.max(budget.floor, Math.round(budget.bytes / 1.5));
2823
2836
  }
2824
2837
  return done;
2825
2838
  }
2826
2839
  function installWrite(work) {
2827
- const next = installWrites.then(() => exclusive(work), () => exclusive(work));
2828
- installWrites = next.then(() => undefined, () => undefined);
2829
- return next;
2840
+ return installTurn(installTurns.starting, work);
2841
+ }
2842
+ function finishingWrite(work) {
2843
+ return installTurn(installTurns.finishing, work);
2844
+ }
2845
+ function installTurn(queue, work) {
2846
+ return new Promise((resolve, reject) => {
2847
+ queue.push(() => void exclusive(work)
2848
+ .then(resolve, reject)
2849
+ .finally(() => {
2850
+ installTurns.busy = false;
2851
+ nextInstallTurn();
2852
+ }));
2853
+ nextInstallTurn();
2854
+ });
2830
2855
  }
2831
- function screened() {
2832
- if (onScreen || closed)
2833
- return Promise.resolve();
2834
- queueReadiness();
2835
- return new Promise((resolve) => shownWaiters.push(resolve));
2856
+ function nextInstallTurn() {
2857
+ if (installTurns.busy || installTurns.ahead > 0)
2858
+ return;
2859
+ const turn = installTurns.finishing.shift() ?? installTurns.starting.shift();
2860
+ if (turn === undefined)
2861
+ return;
2862
+ installTurns.busy = true;
2863
+ turn();
2864
+ }
2865
+ async function ahead(work) {
2866
+ let taken = false;
2867
+ const hold = {
2868
+ take() {
2869
+ if (taken)
2870
+ return;
2871
+ taken = true;
2872
+ installTurns.ahead += 1;
2873
+ },
2874
+ release() {
2875
+ if (!taken)
2876
+ return;
2877
+ taken = false;
2878
+ installTurns.ahead -= 1;
2879
+ resumeInstall();
2880
+ },
2881
+ };
2882
+ hold.take();
2883
+ try {
2884
+ return await work(hold);
2885
+ }
2886
+ finally {
2887
+ hold.release();
2888
+ }
2836
2889
  }
2837
- async function heldPart() {
2838
- while (partsFree === 0)
2839
- await new Promise((admit) => partWaiters.push(admit));
2840
- partsFree -= 1;
2841
- return freed();
2890
+ function resumeInstall() {
2891
+ if (installTurns.ahead > 0)
2892
+ return;
2893
+ void yielded().then(nextInstallTurn);
2894
+ }
2895
+ async function heldStream() {
2896
+ while (streamsFree === 0)
2897
+ await new Promise((admit) => installWaiters.push(admit));
2898
+ streamsFree -= 1;
2899
+ return once(() => {
2900
+ streamsFree += 1;
2901
+ });
2902
+ }
2903
+ async function unqueued() {
2904
+ while (queuedText >= QUEUED_TEXT) {
2905
+ await new Promise((admit) => installWaiters.push(admit));
2906
+ }
2842
2907
  }
2843
- function freed() {
2908
+ function once(release) {
2844
2909
  let released = false;
2845
2910
  return () => {
2846
2911
  if (released)
2847
2912
  return;
2848
2913
  released = true;
2849
- partsFree += 1;
2850
- partWaiters.shift()?.();
2914
+ release();
2915
+ for (const admit of installWaiters.splice(0))
2916
+ admit();
2851
2917
  };
2852
2918
  }
2853
- function installing(locator, step, share = null) {
2854
- installs.set(locator, { step, share });
2919
+ function installing(locator) {
2920
+ installs.add(locator);
2855
2921
  queueReadiness();
2856
2922
  }
2857
- function installed(locator) {
2858
- if (installs.delete(locator))
2859
- queueReadiness();
2923
+ function untransferred(locator) {
2924
+ transfers.delete(locator);
2925
+ arrivals.delete(locator);
2926
+ streaming.delete(locator);
2927
+ underived.delete(locator);
2928
+ unindexed.delete(locator);
2929
+ }
2930
+ function setBack(locator) {
2931
+ const now = bytesOf(locator, true);
2932
+ const earlier = startedOver.get(locator);
2933
+ startedOver.set(locator, {
2934
+ done: Math.max(now.done, earlier?.done ?? 0),
2935
+ total: Math.max(now.total, earlier?.total ?? 0),
2936
+ });
2860
2937
  }
2861
- function screen(value) {
2862
- if (value === onScreen)
2938
+ function owedIndexes(locator, bytes, text) {
2939
+ if (bytes <= 0 || text <= 0)
2863
2940
  return;
2864
- onScreen = value;
2865
- journal.record(`installation ${value ? 'shown' : 'hidden'}`);
2941
+ unindexed.set(locator, [...(unindexed.get(locator) ?? []), { bytes, text }]);
2942
+ }
2943
+ function built(locator, { done, text }) {
2944
+ const owed = unindexed.get(locator) ?? [];
2945
+ let left = text;
2946
+ while (!done && left > 0 && owed.length > 0) {
2947
+ const [first] = owed;
2948
+ if (left >= first.text) {
2949
+ left -= first.text;
2950
+ owed.shift();
2951
+ continue;
2952
+ }
2953
+ owed[0] = {
2954
+ bytes: (first.bytes * (first.text - left)) / first.text,
2955
+ text: first.text - left,
2956
+ };
2957
+ left = 0;
2958
+ }
2959
+ if (done || owed.length === 0)
2960
+ unindexed.delete(locator);
2866
2961
  queueReadiness();
2867
- if (!value)
2868
- return;
2869
- for (const resolve of shownWaiters.splice(0))
2870
- resolve();
2871
2962
  }
2872
- async function pullRefresh(authorized, held, liveness) {
2963
+ function installed(locator) {
2964
+ if (installs.delete(locator))
2965
+ queueReadiness();
2966
+ }
2967
+ async function pullRefresh(authorized, held, liveness, hold, refresh) {
2873
2968
  if (!liveness())
2874
2969
  return unconfirmed;
2875
- const turn = generation;
2876
2970
  const { record } = held;
2877
- const base = held.confirmed;
2971
+ const from = standingOf(record.locator);
2878
2972
  const local = await openLocal();
2879
2973
  try {
2880
2974
  await exclusive(() => local.unrefresh(record.key));
2881
- const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, liveness));
2975
+ const result = await advancement(async (authorization, request) => {
2976
+ hold.release();
2977
+ try {
2978
+ return await refresh(authorization, request);
2979
+ }
2980
+ finally {
2981
+ hold.take();
2982
+ }
2983
+ }, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.locator, record.key, composed(record), liveness));
2882
2984
  if (result.kind !== 'advanced') {
2883
2985
  journal.record(`transfer refresh ${record.locator} ${result.kind}`);
2884
2986
  return result;
2885
2987
  }
2886
- const refused = await validatedRoom(local, result.record, 'refreshed', {
2887
- record,
2888
- changed: [...result.changed],
2889
- });
2890
- if (refused !== null) {
2891
- journal.record(`transfer refresh ${record.locator} refused`);
2892
- return refused;
2893
- }
2894
2988
  journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
2895
- return { kind: 'advanced', record: result.record, base };
2989
+ return {
2990
+ kind: 'advanced',
2991
+ record: result.record,
2992
+ from,
2993
+ remodelled: result.record.model !== record.model,
2994
+ };
2896
2995
  }
2897
2996
  catch (error) {
2898
2997
  if (refusedForSpace(error))
@@ -2900,9 +2999,6 @@ export function composeRuntime(options) {
2900
2999
  journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
2901
3000
  throw error;
2902
3001
  }
2903
- finally {
2904
- reads.finished(record.locator, turn);
2905
- }
2906
3002
  }
2907
3003
  const heldContents = new Map();
2908
3004
  function heldContentOf(local, key) {
@@ -2920,33 +3016,50 @@ export function composeRuntime(options) {
2920
3016
  heldContents.set(key, made);
2921
3017
  return made;
2922
3018
  }
3019
+ async function abandoned(local, locator) {
3020
+ await local.abandon(locator);
3021
+ setBack(locator);
3022
+ untransferred(locator);
3023
+ liveQueries.recover();
3024
+ sweep();
3025
+ }
2923
3026
  function staging(local, liveness) {
3027
+ const derived = async (locator, key, composition) => {
3028
+ underived.delete(locator);
3029
+ for (;;) {
3030
+ if (!liveness())
3031
+ return false;
3032
+ const done = await finishingWrite(async () => liveness()
3033
+ ? timed(deriveBudget, () => local.rederive(key, composition, deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR))
3034
+ : null);
3035
+ if (done === null)
3036
+ return false;
3037
+ built(locator, done);
3038
+ if (done.done)
3039
+ return true;
3040
+ }
3041
+ };
2924
3042
  return {
2925
3043
  held: (locator) => transfers.get(locator) ?? null,
2926
3044
  async turn(locator, step) {
2927
- installing(locator, step);
2928
- for (;;) {
2929
- await screened();
2930
- const release = step === 'downloading' ? await heldPart() : () => { };
2931
- if (onScreen || closed)
2932
- return release;
2933
- release();
2934
- }
3045
+ installing(locator);
3046
+ return step === 'downloading' ? heldStream() : () => { };
2935
3047
  },
2936
- spare() {
2937
- if (!onScreen || closed || partsFree === 0)
2938
- return null;
2939
- partsFree -= 1;
2940
- return freed();
3048
+ unqueued,
3049
+ queued(text) {
3050
+ queuedText += text;
3051
+ return once(() => {
3052
+ queuedText -= text;
3053
+ });
2941
3054
  },
2942
3055
  arriving(locator) {
2943
3056
  let ended = false;
2944
3057
  return {
2945
- at(token, bytes) {
3058
+ at(bytes) {
2946
3059
  if (ended)
2947
3060
  return;
2948
- const held = arrivals.get(locator) ?? new Map();
2949
- arrivals.set(locator, held.set(token, bytes));
3061
+ arrivals.set(locator, bytes);
3062
+ streaming.add(locator);
2950
3063
  const now = performance.now();
2951
3064
  if (now - arrivalReported < ARRIVAL_REPORT_MS)
2952
3065
  return;
@@ -2954,96 +3067,64 @@ export function composeRuntime(options) {
2954
3067
  queueReadiness();
2955
3068
  },
2956
3069
  end() {
3070
+ if (ended)
3071
+ return;
2957
3072
  ended = true;
2958
- if (arrivals.delete(locator))
3073
+ if (streaming.delete(locator))
2959
3074
  queueReadiness();
2960
3075
  },
2961
3076
  };
2962
3077
  },
2963
- received: (locator, token) => local.transferPart(locator, token),
2964
- keep: (locator, token, received) => installWrite(async () => {
2965
- if (liveness())
2966
- await local.keepPart(locator, token, received);
2967
- }),
2968
- announced(locator) {
2969
- const later = announced.get(locator);
2970
- return later === undefined ? [] : [later];
2971
- },
2972
- async stage(record, page, composition, received) {
3078
+ streamed: options.adapter.roomStream !== undefined,
3079
+ async stage(record, page, composition) {
2973
3080
  const { locator } = record;
2974
- const whole = received?.byteLength ?? 0;
2975
- const from = record.writing?.written ?? 0;
2976
- const token = record.writing?.token ?? null;
2977
- const entries = page.entries.length;
2978
- let held = record;
2979
- if (token !== null)
2980
- arrivals.get(locator)?.delete(token);
2981
- installing(locator, 'writing', { written: from, entries, bytes: whole });
2982
- for (let written = from;;) {
2983
- await screened();
3081
+ installing(locator);
3082
+ if (!liveness())
3083
+ return null;
3084
+ const key = await installWrite(async () => {
2984
3085
  if (!liveness())
2985
3086
  return null;
2986
- const end = batchEnd(page.entries, written);
2987
- const last = end === entries;
2988
- const next = last
2989
- ? {
2990
- ...held,
2991
- writing: null,
2992
- done: {
2993
- parts: held.done.parts + (received === null ? 0 : 1),
2994
- bytes: held.done.bytes + whole,
2995
- },
2996
- }
2997
- : { ...held, writing: { token, written: end } };
2998
- const key = await installWrite(async () => {
2999
- if (!liveness())
3000
- return null;
3001
- return timed(() => local.stage(next, composition, {
3002
- entries: page.entries.slice(written, end),
3003
- folders: last ? page.folders : [],
3004
- }, last && received !== null && token !== null ? token : undefined));
3005
- });
3006
- if (key === null)
3007
- return null;
3008
- const arrived = !transfers.has(locator);
3009
- held = { ...next, key };
3010
- transfers.set(locator, held);
3011
- if (arrived)
3012
- liveQueries.recover();
3013
- if (last) {
3014
- installing(locator, held.pageToken === null ? 'replaying' : 'downloading');
3015
- liveQueries.recover();
3016
- return held;
3017
- }
3018
- installing(locator, 'writing', { written: end, entries, bytes: whole });
3019
- written = end;
3020
- }
3087
+ return local.stage(record, composition, page);
3088
+ });
3089
+ if (key === null || !liveness())
3090
+ return null;
3091
+ const arrived = !transfers.has(locator);
3092
+ const prior = transfers.get(locator)?.done.bytes ?? 0;
3093
+ const held = { ...record, key };
3094
+ transfers.set(locator, held);
3095
+ if (arrived)
3096
+ liveQueries.recover();
3097
+ installing(locator);
3098
+ liveQueries.recover();
3099
+ const text = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), 0);
3100
+ owedIndexes(locator, record.done.bytes - prior, text);
3101
+ const owed = text + (underived.get(locator) ?? 0);
3102
+ underived.set(locator, owed);
3103
+ if (owed < deriveBudget.bytes)
3104
+ return held;
3105
+ return (await derived(locator, key, composition)) ? held : null;
3021
3106
  },
3022
- async refreshing(locator, key) {
3107
+ derive: derived,
3108
+ async refreshing(locator, key, composition) {
3023
3109
  await exclusive(() => local.unrefresh(key));
3024
- return refreshingOf(local, key, liveness, locator);
3110
+ return refreshingOf(local, locator, key, composition, liveness, true);
3025
3111
  },
3026
3112
  abandon: (locator) => exclusive(async () => {
3027
- if (!liveness())
3028
- return;
3029
- await local.abandon(locator);
3030
- transfers.delete(locator);
3031
- liveQueries.recover();
3032
- sweep();
3113
+ if (liveness())
3114
+ await abandoned(local, locator);
3033
3115
  }),
3034
- setAside: (locator) => exclusive(async () => {
3116
+ setAside: async (locator) => {
3035
3117
  if (!liveness())
3036
3118
  return;
3037
- await local.abandon(locator, true);
3038
- transfers.delete(locator);
3119
+ setBack(locator);
3120
+ untransferred(locator);
3039
3121
  liveQueries.recover();
3040
- }),
3122
+ },
3041
3123
  release: (key) => exclusive(async () => {
3042
3124
  await local.release(key);
3043
3125
  sweep();
3044
3126
  }),
3045
3127
  content: (key) => heldContentOf(local, key),
3046
- validate: (record, view) => validatedRoom(local, record, view, null),
3047
3128
  };
3048
3129
  }
3049
3130
  function reusable(room, local) {
@@ -3055,18 +3136,21 @@ export function composeRuntime(options) {
3055
3136
  async function pullBootstrap(authorized, room, local, liveness) {
3056
3137
  if (!liveness())
3057
3138
  return unconfirmed;
3058
- const turn = generation;
3059
3139
  if (replicas.has(room.locator)) {
3060
3140
  catching.add(room.locator);
3061
3141
  liveQueries.recover();
3062
3142
  }
3063
3143
  try {
3064
- const result = await bootstrapRoom(reads, authorized, room, {
3065
- collection: modelOf(room.locator),
3066
- selection: modelOf(room.locator, 'typed-selection'),
3067
- }, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
3144
+ const result = await bootstrapRoom(reads, authorized, room, { collection: modelOf(room.locator) }, builtIn, typedPaths, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
3068
3145
  const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
3069
3146
  journal.record(`transfer bootstrap ${room.locator} ${detail}`);
3147
+ if (result.kind === 'staged' && older(room.locator, result.record.roomVersion)) {
3148
+ caughtUpTo(room.locator);
3149
+ installed(room.locator);
3150
+ await abandoned(local, room.locator);
3151
+ journal.record(`transfer bootstrap ${room.locator} older than shown`);
3152
+ return { kind: 'transport-uncertainty' };
3153
+ }
3070
3154
  if (result.kind !== 'staged') {
3071
3155
  caughtUpTo(room.locator);
3072
3156
  installed(room.locator);
@@ -3081,9 +3165,6 @@ export function composeRuntime(options) {
3081
3165
  journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
3082
3166
  throw error;
3083
3167
  }
3084
- finally {
3085
- reads.finished(room.locator, turn);
3086
- }
3087
3168
  }
3088
3169
  function caughtUpTo(locator) {
3089
3170
  if (catching.delete(locator))
@@ -3091,72 +3172,83 @@ export function composeRuntime(options) {
3091
3172
  }
3092
3173
  function behind(room) {
3093
3174
  const held = replicas.get(room.locator)?.record.roomVersion;
3094
- return (held !== undefined &&
3095
- [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
3175
+ return held !== undefined && !options.adapter.reached(room.locator, held, room.roomVersion);
3096
3176
  }
3097
- async function advance(room, local, authorized, live) {
3098
- const carried = reads.carried(room.locator);
3099
- const withheld = await advanceOnce(room, local, authorized, live);
3100
- if (withheld !== null || reads.carried(room.locator) === carried || !behind(room)) {
3101
- return withheld;
3102
- }
3103
- return advanceOnce(room, local, authorized, live);
3177
+ function standingOf(locator) {
3178
+ const held = replicas.get(locator);
3179
+ return { key: held?.record.key ?? null, confirmed: held?.confirmed ?? 0 };
3180
+ }
3181
+ function stands(locator, from) {
3182
+ const now = standingOf(locator);
3183
+ return now.key === from.key && now.confirmed === from.confirmed;
3104
3184
  }
3105
- async function advanceOnce(room, local, authorized, running) {
3185
+ async function advance(room, local, authorized, running) {
3106
3186
  const live = () => running() && !full;
3107
3187
  if (!live())
3108
3188
  return unconfirmed;
3109
3189
  const held = replicas.get(room.locator);
3110
- if (held !== undefined) {
3111
- const refreshed = await pullRefresh(authorized, held, live);
3112
- if (!live())
3113
- return unconfirmed;
3114
- if (refreshed.kind === 'still-current') {
3115
- stale.delete(room.locator);
3116
- if (sourceRefusals.has(room.locator))
3117
- return null;
3118
- if (unreadable.delete(room.locator)) {
3119
- scopedRefusals.delete(room.locator);
3120
- list(room, true);
3121
- emitRooms();
3122
- }
3123
- return null;
3124
- }
3125
- if (refreshed.kind === 'advanced') {
3126
- const landing = { kind: 'refreshed', base: refreshed.base };
3127
- if (!(await publish(local, room, refreshed.record, landing, live))) {
3128
- const now = replicas.get(room.locator)?.record.roomVersion;
3129
- const reached = now !== undefined &&
3130
- options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
3131
- return reached ? null : advance(room, local, authorized, live);
3132
- }
3133
- await retainModel(local, room, 'collection', modelOf(room.locator));
3134
- return null;
3135
- }
3136
- if (refreshed.kind !== 'beyond-horizon')
3190
+ if (held !== undefined && options.adapter.roomStream !== undefined) {
3191
+ stale.delete(room.locator);
3192
+ name(room.locator);
3193
+ return null;
3194
+ }
3195
+ const refresh = reads.refresh;
3196
+ if (held !== undefined && refresh !== undefined) {
3197
+ const refreshed = await ahead((hold) => refreshedRoom(room, local, authorized, held, live, hold, refresh));
3198
+ if (refreshed === 'behind')
3199
+ return advance(room, local, authorized, live);
3200
+ if (refreshed !== 'beyond-horizon')
3137
3201
  return refreshed;
3138
- if (opened !== null) {
3139
- owed.add(room.locator);
3140
- liveQueries.recover();
3141
- return unconfirmed;
3142
- }
3143
- if (reloadsDue.get(room.locator) === generation)
3202
+ if (reloadsDue.has(room.locator))
3144
3203
  return unconfirmed;
3204
+ replaced([room.locator]);
3145
3205
  }
3206
+ const from = standingOf(room.locator);
3146
3207
  const bootstrapped = await pullBootstrap(authorized, room, local, live);
3147
- if (bootstrapped.kind === 'staged') {
3148
- await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
3208
+ if (bootstrapped.kind !== 'staged')
3209
+ return live() ? bootstrapped : unconfirmed;
3210
+ const landing = { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from };
3211
+ if (await publish(local, room, bootstrapped.record, landing, live)) {
3149
3212
  return live() ? null : unconfirmed;
3150
3213
  }
3151
- return live() ? bootstrapped : unconfirmed;
3214
+ return reachedBy(room.locator, bootstrapped.record.roomVersion)
3215
+ ? null
3216
+ : advance(room, local, authorized, live);
3217
+ }
3218
+ async function refreshedRoom(room, local, authorized, held, live, hold, refresh) {
3219
+ const refreshed = await pullRefresh(authorized, held, live, hold, refresh);
3220
+ if (!live())
3221
+ return unconfirmed;
3222
+ if (refreshed.kind === 'still-current') {
3223
+ stale.delete(room.locator);
3224
+ if (unreadable.delete(room.locator)) {
3225
+ scopedRefusals.delete(room.locator);
3226
+ list(room, true);
3227
+ emitRooms();
3228
+ }
3229
+ return null;
3230
+ }
3231
+ if (refreshed.kind === 'advanced') {
3232
+ const landing = {
3233
+ kind: 'refreshed',
3234
+ from: refreshed.from,
3235
+ remodelled: refreshed.remodelled,
3236
+ };
3237
+ if (!(await publish(local, room, refreshed.record, landing, live))) {
3238
+ const now = replicas.get(room.locator)?.record.roomVersion;
3239
+ const reached = now !== undefined &&
3240
+ options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
3241
+ return reached ? null : 'behind';
3242
+ }
3243
+ await retainModel(local, room, modelOf(room.locator));
3244
+ return null;
3245
+ }
3246
+ return refreshed.kind === 'beyond-horizon' ? 'beyond-horizon' : refreshed;
3152
3247
  }
3153
3248
  async function withhold(withheld, room, local, progress) {
3154
- if (withheld.kind === 'unconfirmed')
3249
+ if (withheld.kind === 'unconfirmed' || isFenced(room.locator))
3155
3250
  return;
3156
- if (withheld.kind === 'refused')
3157
- stale.delete(room.locator);
3158
- else
3159
- stale.add(room.locator);
3251
+ stale.add(room.locator);
3160
3252
  if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
3161
3253
  const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
3162
3254
  repairable.set(room.locator, reset);
@@ -3178,33 +3270,11 @@ export function composeRuntime(options) {
3178
3270
  list(room, false);
3179
3271
  emitRooms();
3180
3272
  }
3181
- else if (withheld.kind === 'refused') {
3182
- refusedSource(room, withheld);
3183
- }
3184
3273
  else if (withheld.kind === 'revoked-authorization') {
3185
3274
  await refuse('revoked-authorization');
3186
3275
  }
3187
3276
  }
3188
- function refusedSource(room, { refusal }) {
3189
- sourceRefusals.set(room.locator, {
3190
- code: refusal.rule,
3191
- retryable: false,
3192
- fields: {
3193
- file: refusal.file,
3194
- ...(refusal.location === undefined ? {} : { location: refusal.location }),
3195
- ...(refusal.referrers === undefined ? {} : { referrers: refusal.referrers }),
3196
- },
3197
- });
3198
- declarationRefusals.delete(room.locator);
3199
- unreadable.add(room.locator);
3200
- list(room, false);
3201
- emitRooms();
3202
- moveTo({ ...status, ...attention() });
3203
- journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
3204
- }
3205
3277
  function leaving(batch, result) {
3206
- if (result.kind === 'expired')
3207
- return { batch, returned: returnedBy('expired') };
3208
3278
  return {
3209
3279
  batch,
3210
3280
  returned: {
@@ -3223,8 +3293,8 @@ export function composeRuntime(options) {
3223
3293
  function settling(acceptance) {
3224
3294
  return acceptance !== null && 'apply' in acceptance;
3225
3295
  }
3226
- function refusalOf(acceptance) {
3227
- return settling(acceptance) ? (acceptance.refused ?? null) : acceptance;
3296
+ function failureOf(acceptance) {
3297
+ return settling(acceptance) ? null : acceptance;
3228
3298
  }
3229
3299
  async function acceptance(room, local, submitted, accepted) {
3230
3300
  if (!continuing())
@@ -3237,44 +3307,17 @@ export function composeRuntime(options) {
3237
3307
  logAccepted(room.locator, submitted, accepted);
3238
3308
  let record = current.record;
3239
3309
  let changes;
3240
- if (!options.adapter.reached(room.locator, held, accepted.roomVersion)) {
3310
+ if (!options.adapter.reached(room.locator, held, accepted.roomVersion) &&
3311
+ !older(room.locator, accepted.roomVersion)) {
3241
3312
  const paths = [...returnedPaths(submitted, accepted)];
3242
3313
  const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
3243
- const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion));
3314
+ const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion, composed(record)));
3244
3315
  record = {
3245
3316
  ...record,
3246
3317
  roomVersion: options.adapter.follows(room.locator, held, accepted.roomVersion)
3247
3318
  ? accepted.roomVersion
3248
3319
  : held,
3249
3320
  };
3250
- const overrides = new Map([
3251
- ...back.set.map((entry) => [entry.path, entry]),
3252
- ...back.removed.map((path) => [path, null]),
3253
- ]);
3254
- const refused = await validatedRoom(local, record, 'confirmed', { record: current.record, changed: [...overrides.keys()] }, overrides);
3255
- if (refused !== null) {
3256
- const key = pendingKey(room.locator, submitted.id);
3257
- return {
3258
- locator: room.locator,
3259
- left: [],
3260
- commit: {
3261
- rooms: [],
3262
- writes: [
3263
- {
3264
- locator: room.locator,
3265
- accepted: [{ id: submitted.id, roomVersion: accepted.roomVersion }],
3266
- },
3267
- ],
3268
- },
3269
- refused,
3270
- apply() {
3271
- const state = pendings.get(key);
3272
- if (state !== undefined && !fences.has(room.locator))
3273
- pendings.set(key, { ...state, accepted: accepted.roomVersion });
3274
- return changeSet(room.locator, { queue: true });
3275
- },
3276
- };
3277
- }
3278
3321
  changes = back;
3279
3322
  }
3280
3323
  const settled = await settlement(local, room.locator, current, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
@@ -3282,10 +3325,9 @@ export function composeRuntime(options) {
3282
3325
  ...settled,
3283
3326
  apply(answer) {
3284
3327
  const set = settled.apply(answer);
3328
+ showing(room.locator, accepted.roomVersion);
3285
3329
  pendings.delete(pendingKey(room.locator, submitted.id));
3286
3330
  unreadable.delete(room.locator);
3287
- sourceRefusals.delete(room.locator);
3288
- declarationRefusals.delete(room.locator);
3289
3331
  list(room, true);
3290
3332
  return set;
3291
3333
  },
@@ -3303,13 +3345,9 @@ export function composeRuntime(options) {
3303
3345
  const failed = await exclusive(async () => {
3304
3346
  const adopting = await acceptance(room, local, submitted, accepted);
3305
3347
  await adoptedTogether(local, [adopting]);
3306
- return refusalOf(adopting);
3348
+ return failureOf(adopting);
3307
3349
  });
3308
- if (failed === null)
3309
- return true;
3310
- if (failed.kind === 'refused' && continuing())
3311
- refusedSource(room, failed);
3312
- return false;
3350
+ return failed === null;
3313
3351
  }
3314
3352
  async function adoptedMove(room, partner, local, submitted, accepted) {
3315
3353
  const other = discovered.get(partner);
@@ -3323,18 +3361,10 @@ export function composeRuntime(options) {
3323
3361
  ...accepted.destination,
3324
3362
  });
3325
3363
  await adoptedTogether(local, [left, arrived]);
3326
- const refusedArrival = refusalOf(arrived);
3327
- if (other !== undefined && refusedArrival?.kind === 'refused' && continuing()) {
3328
- refusedSource(other, refusedArrival);
3329
- }
3330
- return refusalOf(left);
3364
+ return failureOf(left);
3331
3365
  });
3332
3366
  submitLater(partner);
3333
- if (failed === null)
3334
- return true;
3335
- if (failed.kind === 'refused' && continuing())
3336
- refusedSource(room, failed);
3337
- return false;
3367
+ return failed === null;
3338
3368
  }
3339
3369
  function queueIn(locator) {
3340
3370
  return replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [];
@@ -3445,20 +3475,13 @@ export function composeRuntime(options) {
3445
3475
  return;
3446
3476
  moveTo({ ...status, ...attention() });
3447
3477
  }
3448
- async function discardHeld(head, room, local, signal) {
3449
- const partner = partnerOf(head, room.locator);
3450
- const other = partner === null ? undefined : discovered.get(partner);
3451
- await discardPending(other === undefined ? [room] : [room, other], local, signal);
3452
- }
3453
3478
  async function submitRoom(room, local, authorized, progress, signal) {
3454
3479
  while (continuing() && authorization !== undefined && !signal.aborted) {
3455
3480
  const replica = replicas.get(room.locator);
3456
- const head = replica?.queue[0];
3481
+ const head = replica?.queue.find((batch) => !landedAhead.has(pendingKey(room.locator, batch.id)));
3457
3482
  if (replica === undefined || head === undefined)
3458
3483
  return 'done';
3459
3484
  const state = pendings.get(pendingKey(room.locator, head.id));
3460
- if (state?.accepted !== undefined)
3461
- return 'done';
3462
3485
  const partner = partnerOf(head, room.locator);
3463
3486
  if (partner !== null && !leavingIn(head, room.locator)) {
3464
3487
  submitLater(partner);
@@ -3482,10 +3505,6 @@ export function composeRuntime(options) {
3482
3505
  return 'done';
3483
3506
  }
3484
3507
  const lost = earlier || submission.state === 'submitted';
3485
- if (lost && pastHorizon(head.id)) {
3486
- await discardHeld(head, room, local, signal);
3487
- return 'done';
3488
- }
3489
3508
  let result;
3490
3509
  journal.record(`submission ${room.locator} ${head.id}`);
3491
3510
  await exclusive(() => mark(local, room.locator, head, { state: 'submitted' }, lost));
@@ -3563,32 +3582,12 @@ export function composeRuntime(options) {
3563
3582
  const other = partner === null ? undefined : discovered.get(partner);
3564
3583
  if (result.kind === 'conflicted' ||
3565
3584
  result.kind === 'rejected' ||
3566
- result.kind === 'expired' ||
3567
3585
  result.kind === 'retryable-refusal') {
3568
- if (result.kind === 'expired' && lost && result.restored !== true) {
3569
- journal.record(`outcome ${room.locator} ${head.id} expired`);
3570
- await discardHeld(head, room, local, signal);
3571
- return 'done';
3572
- }
3573
- if (result.kind === 'expired') {
3574
- await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, partner !== null && lost));
3575
- const there = other === undefined ? null : await advance(other, local, authorized, continuing);
3576
- if (other !== undefined && there !== null) {
3577
- await withhold(there, other, local, progress);
3578
- return 'done';
3579
- }
3580
- const withheld = await advance(room, local, authorized, continuing);
3581
- if (withheld !== null) {
3582
- await withhold(withheld, room, local, progress);
3583
- return 'done';
3584
- }
3585
- }
3586
3586
  journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
3587
3587
  (result.kind === 'conflicted' && result.roomVersion !== undefined
3588
3588
  ? ` ${result.roomVersion}`
3589
3589
  : ''));
3590
- if (result.kind !== 'expired')
3591
- journal.record(`refusal ${result.kind} ${result.code}`);
3590
+ journal.record(`refusal ${result.kind} ${result.code}`);
3592
3591
  }
3593
3592
  if (result.kind === 'retryable-refusal') {
3594
3593
  const reset = result.reset;
@@ -3596,11 +3595,11 @@ export function composeRuntime(options) {
3596
3595
  await exclusive(() => mark(local, room.locator, head, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
3597
3596
  return reset === undefined ? 'again' : { at: Date.parse(reset) };
3598
3597
  }
3599
- if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
3598
+ if (result.kind === 'conflicted' || result.kind === 'rejected') {
3600
3599
  const left = leaving(head, result);
3601
3600
  const refreshed = async () => ({
3602
- there: other === undefined ? null : await advance(other, local, authorized, continuing),
3603
- here: await advance(room, local, authorized, continuing),
3601
+ there: other === undefined ? null : await advanced(other, local, authorized),
3602
+ here: await advanced(room, local, authorized),
3604
3603
  });
3605
3604
  const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
3606
3605
  if (judged !== null && (judged.there !== null || judged.here !== null)) {
@@ -3620,16 +3619,19 @@ export function composeRuntime(options) {
3620
3619
  return;
3621
3620
  }
3622
3621
  const confirmedLayer = await sliceAt(current.record, 'confirmed', [head]);
3623
- const held = (result.kind === 'expired' &&
3624
- (await landedWhole(room.locator, head, confirmedLayer))) ||
3625
- (result.kind === 'conflicted' &&
3626
- (await landedWhole(room.locator, head, confirmedLayer, result.affected)));
3622
+ const held = result.kind === 'conflicted' &&
3623
+ (await landedWhole(room.locator, head, confirmedLayer, result.affected));
3624
+ if (held &&
3625
+ result.kind === 'conflicted' &&
3626
+ partner === null &&
3627
+ !landed(head, confirmedLayer)) {
3628
+ landedAhead.set(pendingKey(room.locator, head.id), Object.fromEntries([...touchedBy([head])].map((path) => [path, confirmedLayer.revisions[path]])));
3629
+ return;
3630
+ }
3627
3631
  publishChangeSet(await settle(local, room.locator, current, current.record, current.queue.filter((batch) => batch.id !== head.id), { leaving: [held ? { ...left, returned: null } : left] }));
3628
3632
  await unpair(local);
3629
3633
  moveTo({ ...status, ...attention() });
3630
3634
  });
3631
- if (result.kind === 'expired')
3632
- continue;
3633
3635
  const { there, here } = judged ?? (await refreshed());
3634
3636
  if (other !== undefined && there !== null)
3635
3637
  await withhold(there, other, local, progress);
@@ -3649,93 +3651,55 @@ export function composeRuntime(options) {
3649
3651
  }
3650
3652
  return 'done';
3651
3653
  }
3652
- function pastHorizon(identity) {
3653
- return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
3654
- }
3655
- async function discardPending(rooms, local, signal) {
3656
- const discarded = await exclusive(async () => {
3657
- const held = rooms
3658
- .filter(({ locator }) => !fences.has(locator))
3659
- .map((room) => ({
3660
- room,
3661
- before: replicas.get(room.locator),
3662
- queue: queueIn(room.locator),
3663
- }));
3664
- if (held.length === 0)
3665
- return [];
3666
- const answer = await local.commit({
3667
- rooms: held.flatMap(({ room, before }) => before === undefined
3668
- ? []
3669
- : [
3670
- {
3671
- locator: room.locator,
3672
- key: before.record.key,
3673
- roots: before.record.typedSelection?.roots ?? [],
3674
- overlay: { set: [], cleared: [...before.overlaid] },
3675
- },
3676
- ]),
3677
- discards: held.map(({ room }) => room.locator),
3678
- });
3679
- for (const { room, before, queue } of held) {
3680
- const { locator } = room;
3681
- lastFenced.set(locator, ++fencings);
3682
- for (const batch of queue)
3683
- pendings.delete(pendingKey(locator, batch.id));
3684
- queuesWithoutReplica.delete(locator);
3685
- if (before !== undefined) {
3686
- replicas.set(locator, {
3687
- ...before,
3688
- queue: [],
3689
- overlaid: new Set(),
3690
- count: answer.counts[locator] ?? before.count,
3691
- });
3692
- }
3693
- journal.record(`room discarded ${locator}`);
3694
- }
3695
- publishChangeSet(mergeChangeSets(held.map(({ room }) => changeSet(room.locator, {
3696
- documents: answer.changed[room.locator] ?? [],
3697
- queue: true,
3698
- }))));
3699
- moveTo({ ...status, ...attention() });
3700
- return held.map(({ room }) => room);
3701
- });
3702
- for (const room of discarded) {
3703
- if (signal.aborted)
3704
- return;
3705
- const progress = { complete: true, reachable: true };
3706
- await roomTask(room.locator, async (live) => {
3707
- await Promise.race([
3708
- reload(room, local, progress, () => live() && !signal.aborted),
3709
- abandonedOn(signal),
3710
- ]);
3711
- });
3712
- if (signal.aborted)
3713
- return;
3714
- await rest(progress);
3715
- }
3716
- }
3717
3654
  function awaitingAnswer(locator) {
3655
+ answering.get(locator)?.release();
3718
3656
  let release = () => { };
3719
- const awaited = new Promise((resolve) => {
3657
+ const answered = new Promise((resolve) => {
3720
3658
  release = resolve;
3721
3659
  });
3722
- answering.set(locator, { generation, answered: awaited });
3660
+ const awaited = { answered, release };
3661
+ answering.set(locator, awaited);
3723
3662
  return () => {
3724
- if (answering.get(locator)?.answered === awaited)
3663
+ if (answering.get(locator) === awaited)
3725
3664
  answering.delete(locator);
3726
3665
  release();
3727
3666
  };
3728
3667
  }
3729
- async function retainModel(local, room, attached, next) {
3730
- const key = modelKey(room.locator, attached);
3731
- const previous = modelOf(room.locator, attached);
3732
- const heldBefore = replicas.get(room.locator)?.record;
3733
- if (JSON.stringify(previous) === JSON.stringify(next) &&
3734
- (heldBefore === undefined ||
3735
- (JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
3736
- JSON.stringify(heldBefore.replica) === JSON.stringify(room.replica) &&
3737
- JSON.stringify(heldBefore.typedSelection) ===
3738
- JSON.stringify(room.typedSelection ?? null))))
3668
+ function declaredAsHeld(room) {
3669
+ const held = replicas.get(room.locator)?.record;
3670
+ return (held === undefined ||
3671
+ (JSON.stringify(held.operations) === JSON.stringify(room.operations) &&
3672
+ JSON.stringify(held.replica) === JSON.stringify(room.replica) &&
3673
+ JSON.stringify(held.typedSelection) === JSON.stringify(room.typedSelection ?? null)));
3674
+ }
3675
+ async function keepNaming(local, room) {
3676
+ const held = replicas.get(room.locator)?.record;
3677
+ if (held === undefined || (held.name === room.name && held.handle === room.handle))
3678
+ return;
3679
+ await exclusive(async () => {
3680
+ if (!continuing())
3681
+ return;
3682
+ const replica = replicas.get(room.locator);
3683
+ if (replica === undefined)
3684
+ return;
3685
+ const record = { ...replica.record, name: room.name, handle: room.handle };
3686
+ await local.commit({
3687
+ rooms: [
3688
+ {
3689
+ locator: room.locator,
3690
+ key: record.key,
3691
+ record: storedRecord(record),
3692
+ composed: composedOf(composed(record)),
3693
+ },
3694
+ ],
3695
+ });
3696
+ replicas.set(room.locator, { ...replica, record });
3697
+ });
3698
+ }
3699
+ async function retainModel(local, room, next) {
3700
+ const key = room.locator;
3701
+ const previous = modelOf(room.locator);
3702
+ if (JSON.stringify(previous) === JSON.stringify(next) && declaredAsHeld(room))
3739
3703
  return;
3740
3704
  if (next.status === 'unavailable') {
3741
3705
  invalidModels.add(key);
@@ -3747,26 +3711,15 @@ export function composeRuntime(options) {
3747
3711
  const held = replicas.get(room.locator);
3748
3712
  let changed = null;
3749
3713
  if (held !== undefined) {
3750
- const declared = {
3714
+ const record = {
3751
3715
  ...held.record,
3716
+ name: room.name,
3717
+ handle: room.handle,
3752
3718
  operations: room.operations,
3753
3719
  replica: room.replica,
3754
3720
  typedSelection: room.typedSelection ?? null,
3755
- ...(attached === 'collection' ? { model: next } : { selectionModel: next }),
3721
+ model: next,
3756
3722
  };
3757
- let read = 'staged';
3758
- if (!sameMembership(declared, held.record)) {
3759
- const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), compositionOf(declared));
3760
- read =
3761
- verdict.verdict !== 'refuse'
3762
- ? 'staged'
3763
- : verdict.rule === 'model-unavailable'
3764
- ? 'deferred'
3765
- : { kind: 'refused', refusal: verdict };
3766
- }
3767
- const record = read === 'staged'
3768
- ? declared
3769
- : { ...declared, typedSelection: held.record.typedSelection };
3770
3723
  const confirmedLayer = await sliceAt(record, 'confirmed', held.queue, held.overlaid);
3771
3724
  const replayedLayer = await replayed(record, confirmedLayer, held.queue);
3772
3725
  const diff = await overlayDiff(record, held, replayedLayer.visible, replayedLayer.touched);
@@ -3777,7 +3730,7 @@ export function composeRuntime(options) {
3777
3730
  key: record.key,
3778
3731
  record: storedRecord(record),
3779
3732
  roots: record.typedSelection?.roots ?? [],
3780
- composition: compositionOf(record),
3733
+ composed: composedOf(composed(record)),
3781
3734
  overlay: diff.overlay,
3782
3735
  },
3783
3736
  ],
@@ -3788,29 +3741,22 @@ export function composeRuntime(options) {
3788
3741
  documents: answer.changed[room.locator] ?? [],
3789
3742
  room: true,
3790
3743
  });
3791
- if (read === 'staged' || read === 'deferred') {
3792
- if (declarationRefusals.delete(room.locator)) {
3793
- sourceRefusals.delete(room.locator);
3794
- unreadable.delete(room.locator);
3795
- }
3796
- }
3797
- else {
3798
- refusedSource(room, read);
3799
- declarationRefusals.add(room.locator);
3800
- }
3801
- }
3802
- models.set(key, next);
3803
- if (next.status === 'current') {
3804
- liveModels.set(key, JSON.stringify(next.snapshot));
3805
- invalidModels.delete(key);
3806
3744
  }
3807
- list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
3745
+ heldModel(room, next);
3808
3746
  if (changed === null)
3809
3747
  emitRooms();
3810
3748
  else
3811
3749
  publishChangeSet(changed);
3812
3750
  });
3813
3751
  }
3752
+ function heldModel(room, next) {
3753
+ models.set(room.locator, next);
3754
+ if (next.status === 'current') {
3755
+ liveModels.set(room.locator, JSON.stringify(next.snapshot));
3756
+ invalidModels.delete(room.locator);
3757
+ }
3758
+ list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
3759
+ }
3814
3760
  async function scoped(result, room, local) {
3815
3761
  if (result.scope === 'content' && !result.retryable) {
3816
3762
  journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
@@ -3824,14 +3770,14 @@ export function composeRuntime(options) {
3824
3770
  });
3825
3771
  let scopedAway = room;
3826
3772
  if (result.scope === 'model')
3827
- await retainModel(local, room, 'collection', {
3773
+ await retainModel(local, room, {
3828
3774
  status: 'unavailable',
3829
3775
  reason: 'not-readable',
3830
3776
  });
3831
3777
  else if (result.scope !== 'operation') {
3832
3778
  unreadable.add(room.locator);
3833
3779
  scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
3834
- await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
3780
+ await retainModel(local, scopedAway, modelOf(room.locator));
3835
3781
  }
3836
3782
  if (!continuing())
3837
3783
  return;
@@ -3839,6 +3785,17 @@ export function composeRuntime(options) {
3839
3785
  emitRooms();
3840
3786
  journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
3841
3787
  }
3788
+ function name(locator) {
3789
+ const stream = options.adapter.roomStream;
3790
+ const record = replicas.get(locator)?.record;
3791
+ const room = discovered.get(locator);
3792
+ if (stream === undefined || record === undefined || room === undefined)
3793
+ return;
3794
+ if (!socketOpen || !handed || record.replica.selection !== room.replica.selection)
3795
+ return;
3796
+ naming += 1;
3797
+ stream.hold([{ locator, roomVersion: record.roomVersion }]);
3798
+ }
3842
3799
  function heldRooms(listed) {
3843
3800
  return listed.flatMap((room) => {
3844
3801
  const record = replicas.get(room.locator)?.record;
@@ -3856,14 +3813,19 @@ export function composeRuntime(options) {
3856
3813
  return outcome?.status === 'fulfilled' && outcome.value;
3857
3814
  });
3858
3815
  if (attempt === 'opened') {
3816
+ naming += 1;
3859
3817
  options.adapter.roomStream?.hold(heldRooms(ready));
3860
3818
  handed = true;
3861
3819
  }
3862
- discovering?.release();
3863
- const loaded = await Promise.allSettled(ready
3864
- .filter((room) => attempt === 'unstreamed' || !replicas.has(room.locator))
3865
- .map((room) => roomTask(room.locator, (held) => loadRoom(room, local, progress, held))));
3866
- const failed = [...prepared, ...loaded].find((room) => room.status === 'rejected');
3820
+ for (const room of ready) {
3821
+ if (attempt !== 'unstreamed' && replicas.has(room.locator)) {
3822
+ if (attempt === 'authorized')
3823
+ name(room.locator);
3824
+ continue;
3825
+ }
3826
+ void roomTask(room.locator, (live) => loadRoom(room, local, progress, live)).then(() => rest(progress));
3827
+ }
3828
+ const failed = prepared.find((room) => room.status === 'rejected');
3867
3829
  if (failed !== undefined)
3868
3830
  throw failed.reason;
3869
3831
  }
@@ -3881,27 +3843,31 @@ export function composeRuntime(options) {
3881
3843
  return false;
3882
3844
  }
3883
3845
  if (prior !== undefined)
3884
- await retainModel(local, room, 'collection', modelOf(room.locator));
3846
+ await retainModel(local, room, modelOf(room.locator));
3847
+ if (prior !== undefined)
3848
+ await keepNaming(local, room);
3885
3849
  if (discoveryPaused || authorization === undefined)
3886
3850
  return false;
3887
3851
  return room.replica.admission === 'admitted';
3888
3852
  }
3889
3853
  async function caughtUp(room, local, progress, live) {
3890
- if (authorization === undefined || !live() || !behind(room))
3854
+ if (options.adapter.roomStream !== undefined || authorization === undefined)
3855
+ return;
3856
+ if (!live() || !behind(room))
3891
3857
  return;
3892
3858
  const withheld = await advance(room, local, authorization, live);
3893
3859
  if (withheld !== null)
3894
3860
  await withhold(withheld, room, local, progress);
3895
3861
  }
3896
3862
  function revoked(locator) {
3897
- return ['collection', 'typed-selection'].some((attached) => {
3898
- const model = modelOf(locator, attached);
3899
- return model.status === 'unavailable' && model.reason === 'revoked';
3900
- });
3863
+ const model = modelOf(locator);
3864
+ return model.status === 'unavailable' && model.reason === 'revoked';
3901
3865
  }
3902
3866
  async function loadRoom(room, local, progress, live) {
3903
3867
  if (!live() || authorization === undefined)
3904
3868
  return;
3869
+ if (replicas.has(room.locator) && !(await prepareRoom(room, local, progress)))
3870
+ return;
3905
3871
  if (replicas.has(room.locator) && revoked(room.locator)) {
3906
3872
  await reload(room, local, progress, live);
3907
3873
  return;
@@ -3919,20 +3885,53 @@ export function composeRuntime(options) {
3919
3885
  reloadsDue.delete(room.locator);
3920
3886
  if (!live() || authorization === undefined)
3921
3887
  return;
3888
+ const from = standingOf(room.locator);
3922
3889
  const bootstrapped = await pullBootstrap(authorization, room, local, live);
3923
3890
  if (bootstrapped.kind !== 'staged') {
3924
3891
  if (live())
3925
3892
  await withhold(bootstrapped, room, local, progress);
3926
3893
  return;
3927
3894
  }
3928
- await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
3895
+ await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
3929
3896
  await caughtUp(room, local, progress, live);
3930
3897
  }
3931
3898
  finally {
3932
3899
  caughtUpTo(room.locator);
3933
3900
  }
3934
3901
  }
3935
- async function discoverRooms(local, progress, attempt, live, scope) {
3902
+ async function asked(authorized) {
3903
+ try {
3904
+ return (await options.adapter.discover?.(authorized)) ?? { kind: 'transport-uncertainty' };
3905
+ }
3906
+ catch {
3907
+ return { kind: 'transport-uncertainty' };
3908
+ }
3909
+ }
3910
+ function malformed(listed) {
3911
+ for (const room of listed) {
3912
+ if (room.operations === undefined ||
3913
+ !['write', 'erase'].every((operation) => Array.isArray(room.operations[operation]) &&
3914
+ room.operations[operation].every((scope) => typeof scope === 'string')) ||
3915
+ room.replica === undefined ||
3916
+ typeof room.replica.selection !== 'string' ||
3917
+ room.replica.selection.length === 0 ||
3918
+ !['admitted', 'unavailable'].includes(room.replica.admission) ||
3919
+ !['locked', 'open'].includes(room.replica.offline)) {
3920
+ return `invalid model or replica declaration ${room.locator}`;
3921
+ }
3922
+ if (!declaredSelection(room))
3923
+ return `invalid typed selection declaration ${room.locator}`;
3924
+ }
3925
+ return null;
3926
+ }
3927
+ async function signIn(local, signedIn) {
3928
+ if (JSON.stringify(signedIn) === JSON.stringify(member))
3929
+ return;
3930
+ await local.fact('member', signedIn);
3931
+ member = signedIn;
3932
+ emitMember();
3933
+ }
3934
+ async function discoverRooms(local, progress, attempt, live, found) {
3936
3935
  if (closed || authorization === undefined) {
3937
3936
  notifyReady();
3938
3937
  return;
@@ -3940,13 +3939,9 @@ export function composeRuntime(options) {
3940
3939
  moveTo(transferring(status));
3941
3940
  discoveries += 1;
3942
3941
  const started = discoveries;
3943
- let result;
3944
- try {
3945
- result = await options.adapter.discover(authorization, scope);
3946
- }
3947
- catch {
3948
- result = { kind: 'transport-uncertainty' };
3949
- }
3942
+ const result = found ?? (await asked(authorization));
3943
+ if (result.kind === 'discovery')
3944
+ roomsNamed = true;
3950
3945
  if (!live()) {
3951
3946
  progress.complete = false;
3952
3947
  notifyReady();
@@ -3954,12 +3949,7 @@ export function composeRuntime(options) {
3954
3949
  }
3955
3950
  journal.record(`connection discover ${result.kind}`);
3956
3951
  if (result.kind === 'transport-uncertainty') {
3957
- for (const room of rooms.values()) {
3958
- await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
3959
- if (room.typedSelection !== null) {
3960
- await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
3961
- }
3962
- }
3952
+ await cachedModels(local);
3963
3953
  progress.reachable = false;
3964
3954
  progress.complete = false;
3965
3955
  notifyReady();
@@ -3980,26 +3970,12 @@ export function composeRuntime(options) {
3980
3970
  notifyReady();
3981
3971
  return;
3982
3972
  }
3983
- for (const room of result.rooms) {
3984
- if (room.operations === undefined ||
3985
- !['write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
3986
- room.operations[operation].every((scope) => typeof scope === 'string')) ||
3987
- room.replica === undefined ||
3988
- typeof room.replica.selection !== 'string' ||
3989
- room.replica.selection.length === 0 ||
3990
- !['admitted', 'unavailable'].includes(room.replica.admission) ||
3991
- !['locked', 'open'].includes(room.replica.offline)) {
3992
- journal.record(`invalid model or replica declaration ${room.locator}`);
3993
- await refuse('missing-required-guarantee');
3994
- notifyReady();
3995
- return;
3996
- }
3997
- if (!declaredSelection(room)) {
3998
- journal.record(`invalid typed selection declaration ${room.locator}`);
3999
- await refuse('missing-required-guarantee');
4000
- notifyReady();
4001
- return;
4002
- }
3973
+ const invalid = malformed(result.rooms);
3974
+ if (invalid !== null) {
3975
+ journal.record(invalid);
3976
+ await refuse('missing-required-guarantee');
3977
+ notifyReady();
3978
+ return;
4003
3979
  }
4004
3980
  if (refusal !== null) {
4005
3981
  refusal = null;
@@ -4007,7 +3983,6 @@ export function composeRuntime(options) {
4007
3983
  }
4008
3984
  const previousCapabilities = capabilities;
4009
3985
  capabilities = result.capabilities;
4010
- outcomeRetention = result.retention.outcome;
4011
3986
  notifyReady();
4012
3987
  if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
4013
3988
  JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
@@ -4015,13 +3990,8 @@ export function composeRuntime(options) {
4015
3990
  await local.fact('providerLimits', limits);
4016
3991
  await local.fact('capabilities', capabilities);
4017
3992
  }
4018
- const signedIn = result.member ?? null;
4019
- if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
4020
- await local.fact('member', signedIn);
4021
- member = signedIn;
4022
- emitMember();
4023
- }
4024
- await synchronize(await applyDiscovery(result.rooms, started, result.covered), local, progress, attempt, live);
3993
+ await signIn(local, result.member ?? null);
3994
+ await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
4025
3995
  }
4026
3996
  async function ensureAuthorization() {
4027
3997
  if (authorization !== undefined)
@@ -4036,23 +4006,45 @@ export function composeRuntime(options) {
4036
4006
  }
4037
4007
  }
4038
4008
  async function admittedRead(work) {
4039
- if (roomReads.generation !== generation) {
4040
- roomReads = { generation, free: ROOM_READS, waiting: [] };
4009
+ while (roomReads.free === 0) {
4010
+ await new Promise((admit) => roomReads.waiting.push(admit));
4041
4011
  }
4042
- const reads = roomReads;
4043
- while (reads.free === 0)
4044
- await new Promise((admit) => reads.waiting.push(admit));
4045
- reads.free -= 1;
4012
+ roomReads.free -= 1;
4046
4013
  try {
4047
4014
  await work();
4048
4015
  }
4049
4016
  finally {
4050
- reads.free += 1;
4051
- reads.waiting.shift()?.();
4017
+ roomReads.free += 1;
4018
+ roomReads.waiting.shift()?.();
4052
4019
  }
4053
4020
  }
4021
+ function inRoom(locator, work) {
4022
+ roomWork += 1;
4023
+ const ran = (roomLanes.get(locator) ?? Promise.resolve()).then(async () => {
4024
+ try {
4025
+ return await work(continuing);
4026
+ }
4027
+ finally {
4028
+ roomWork -= 1;
4029
+ }
4030
+ });
4031
+ const chain = ran.then(() => undefined, () => undefined);
4032
+ roomLanes.set(locator, chain);
4033
+ working.add(chain);
4034
+ void chain.finally(() => {
4035
+ working.delete(chain);
4036
+ if (roomLanes.get(locator) === chain)
4037
+ roomLanes.delete(locator);
4038
+ });
4039
+ return ran;
4040
+ }
4054
4041
  function roomTask(locator, work) {
4055
- return task(roomLane(locator), (live) => admittedRead(() => work(live)));
4042
+ if (stopping)
4043
+ return Promise.resolve();
4044
+ return inRoom(locator, (live) => admittedRead(() => work(live))).catch(() => undefined);
4045
+ }
4046
+ function advanced(room, local, authorized) {
4047
+ return inRoom(room.locator, (live) => advance(room, local, authorized, live));
4056
4048
  }
4057
4049
  function task(lane, work) {
4058
4050
  if (stopping)
@@ -4068,23 +4060,19 @@ export function composeRuntime(options) {
4068
4060
  void run.finally(() => working.delete(run));
4069
4061
  return run;
4070
4062
  }
4071
- function sync(attempt) {
4063
+ function sync(attempt, found) {
4072
4064
  return task(OPENING_LANE, async (live) => {
4073
4065
  if (!live()) {
4074
- notifyReady();
4066
+ readyFromStore();
4075
4067
  return;
4076
4068
  }
4077
4069
  const progress = { complete: true, reachable: true };
4078
- const gate = discover();
4079
- if (attempt === 'opened')
4080
- opened = generation;
4081
- underway = attempt === 'opened' ? { generation, progress } : null;
4070
+ underway = attempt === 'opened' ? progress : null;
4082
4071
  if (attempt === 'opened')
4083
4072
  streamed = progress;
4084
4073
  complete = false;
4085
4074
  recorded = false;
4086
4075
  moveTo(transferring(status));
4087
- handed = false;
4088
4076
  try {
4089
4077
  const local = await openLocal();
4090
4078
  if (!live())
@@ -4092,7 +4080,7 @@ export function composeRuntime(options) {
4092
4080
  await ensureAuthorization();
4093
4081
  if (!live())
4094
4082
  return;
4095
- await discoverRooms(local, progress, attempt, live);
4083
+ await discoverRooms(local, progress, attempt, live, found);
4096
4084
  }
4097
4085
  catch {
4098
4086
  progress.complete = false;
@@ -4100,7 +4088,6 @@ export function composeRuntime(options) {
4100
4088
  progress.reachable = false;
4101
4089
  }
4102
4090
  finally {
4103
- gate.release();
4104
4091
  notifyReady();
4105
4092
  }
4106
4093
  if (!live())
@@ -4112,35 +4099,59 @@ export function composeRuntime(options) {
4112
4099
  if (handed || closed || progress.reachable)
4113
4100
  return;
4114
4101
  underway = null;
4115
- opened = null;
4116
- journal.record('connection reopen');
4117
4102
  await rest(progress, live);
4118
- options.adapter.roomStream?.reopen();
4119
4103
  });
4120
4104
  }
4121
- function rediscover(scope) {
4105
+ function resynchronize() {
4122
4106
  return task(OPENING_LANE, async (live) => {
4123
- if (!live() || !socketOpen)
4107
+ if (!live() || !announced)
4124
4108
  return;
4125
4109
  const progress = { complete: true, reachable: true };
4126
- const gate = discover();
4127
4110
  moveTo(transferring(status));
4128
4111
  try {
4129
4112
  const local = await openLocal();
4130
4113
  if (!live())
4131
4114
  return;
4132
- await ensureAuthorization();
4115
+ await synchronize([...discovered.values()], local, progress, 'unstreamed', live);
4116
+ }
4117
+ catch {
4118
+ progress.complete = false;
4119
+ }
4120
+ if (live())
4121
+ await finishOpening(progress, live);
4122
+ });
4123
+ }
4124
+ function countAuthorityChange() {
4125
+ authorityChanges += 1;
4126
+ for (const listener of authorityChangeListeners)
4127
+ listener(authorityChanges);
4128
+ }
4129
+ function changeAuthority(change) {
4130
+ return task(OPENING_LANE, async (live) => {
4131
+ if (!live() || !announced)
4132
+ return;
4133
+ countAuthorityChange();
4134
+ const progress = { complete: true, reachable: true };
4135
+ moveTo(transferring(status));
4136
+ try {
4137
+ const local = await openLocal();
4133
4138
  if (!live())
4134
4139
  return;
4135
- await discoverRooms(local, progress, 'authorized', live, scope);
4140
+ const invalid = malformed(change.rooms);
4141
+ if (invalid !== null) {
4142
+ journal.record(invalid);
4143
+ await refuse('missing-required-guarantee');
4144
+ return;
4145
+ }
4146
+ if (change.member !== undefined)
4147
+ await signIn(local, change.member);
4148
+ discoveries += 1;
4149
+ const listed = await applyDiscovery(change.rooms, discoveries, change.left);
4150
+ await synchronize(listed, local, progress, 'authorized', live);
4136
4151
  }
4137
4152
  catch {
4138
4153
  progress.complete = false;
4139
4154
  }
4140
- finally {
4141
- gate.release();
4142
- notifyReady();
4143
- }
4144
4155
  if (!live())
4145
4156
  return;
4146
4157
  if (!progress.complete)
@@ -4148,14 +4159,17 @@ export function composeRuntime(options) {
4148
4159
  await rest(progress, live);
4149
4160
  });
4150
4161
  }
4151
- function closedSocket(revoked, lost) {
4152
- const opened = socketOpen;
4162
+ function closedSocket(revoked, lost, authorityChanged) {
4163
+ notifyReady();
4164
+ const opened = announced;
4153
4165
  socketOpen = false;
4166
+ announced = false;
4167
+ missedChange ||= authorityChanged;
4168
+ handed = false;
4169
+ naming = 0;
4154
4170
  connectionLost();
4155
- if (lost) {
4156
- reads.lost();
4171
+ if (lost)
4157
4172
  conversations.lost();
4158
- }
4159
4173
  if (revoked) {
4160
4174
  void refuse('revoked-authorization');
4161
4175
  return;
@@ -4166,18 +4180,32 @@ export function composeRuntime(options) {
4166
4180
  moveTo({
4167
4181
  ...status,
4168
4182
  state: status.state === 'synced' ? 'syncing' : status.state,
4169
- providerUnreachable: status.providerUnreachable || !opened,
4183
+ providerUnreachable: status.providerUnreachable || (!opened && lost),
4170
4184
  });
4185
+ void closing(opened);
4186
+ }
4187
+ function closing(opened) {
4188
+ return task(OPENING_LANE, async (live) => {
4189
+ if (!live())
4190
+ return;
4191
+ const local = await openLocal();
4192
+ if (!opened && !socketOpen)
4193
+ await cachedModels(local);
4194
+ await rest(null, live);
4195
+ });
4196
+ }
4197
+ function cachedModels(local) {
4198
+ return settleModels([...rooms.values()].map((room) => discovered.get(room.locator) ?? room), 'cached', local);
4171
4199
  }
4172
- function applyOpening(expired) {
4173
- const progress = underway?.generation === generation ? underway.progress : null;
4200
+ function caughtUpRooms(expired) {
4201
+ const progress = underway;
4202
+ underway = null;
4203
+ naming = Math.max(0, naming - 1);
4174
4204
  const cycle = progress ?? { complete: true, reachable: true };
4175
- if (opened === generation)
4176
- opened = null;
4205
+ replaced(expired);
4177
4206
  const reloading = progress === null ? [] : owedReloads(expired);
4178
4207
  const replacing = reloadNow((room) => expired.includes(room.locator) || reloading.includes(room.locator), cycle);
4179
4208
  return task(OPENING_LANE, async (live) => {
4180
- underway = null;
4181
4209
  if (!live())
4182
4210
  return;
4183
4211
  if (progress === null) {
@@ -4185,24 +4213,14 @@ export function composeRuntime(options) {
4185
4213
  await rest(cycle, live);
4186
4214
  return;
4187
4215
  }
4188
- await roomsCarried([...expired, ...reloading]);
4189
- if (!live())
4190
- return;
4191
4216
  try {
4192
4217
  const local = await openLocal();
4193
- const reloaded = await replacing;
4218
+ await replacing;
4194
4219
  if (progress.reachable) {
4195
- for (const room of discovered.values()) {
4196
- if (replicas.has(room.locator) &&
4197
- !expired.includes(room.locator) &&
4198
- !reloading.includes(room.locator)) {
4199
- await confirmModels(room, local);
4200
- }
4201
- }
4220
+ await settleModels([...discovered.values()].filter((room) => replicas.has(room.locator) &&
4221
+ !expired.includes(room.locator) &&
4222
+ !reloading.includes(room.locator)), 'current', local);
4202
4223
  }
4203
- const failed = reloaded.find((outcome) => outcome.status === 'rejected');
4204
- if (failed !== undefined)
4205
- throw failed.reason;
4206
4224
  }
4207
4225
  catch {
4208
4226
  progress.complete = false;
@@ -4214,11 +4232,8 @@ export function composeRuntime(options) {
4214
4232
  }
4215
4233
  function owedReloads(expired) {
4216
4234
  const reloading = [...discovered.values()]
4217
- .filter((room) => replicas.has(room.locator) &&
4218
- !expired.includes(room.locator) &&
4219
- (owed.has(room.locator) || revoked(room.locator)))
4235
+ .filter((room) => replicas.has(room.locator) && !expired.includes(room.locator) && revoked(room.locator))
4220
4236
  .map((room) => room.locator);
4221
- owed.clear();
4222
4237
  for (const locator of reloading)
4223
4238
  catching.add(locator);
4224
4239
  liveQueries.recover();
@@ -4227,24 +4242,43 @@ export function composeRuntime(options) {
4227
4242
  async function reloadNow(replaced, progress) {
4228
4243
  const reloaded = [...discovered.values()].filter((room) => replicas.has(room.locator) && replaced(room));
4229
4244
  for (const room of reloaded)
4230
- reloadsDue.set(room.locator, generation);
4245
+ reloadsDue.add(room.locator);
4231
4246
  try {
4232
4247
  const local = await openLocal();
4233
- return await Promise.allSettled(reloaded.map((room) => roomTask(room.locator, (live) => reload(room, local, progress, live))));
4248
+ for (const room of reloaded) {
4249
+ void roomTask(room.locator, (live) => reload(room, local, progress, live)).then(() => rest(progress));
4250
+ }
4234
4251
  }
4235
4252
  catch {
4236
4253
  for (const room of reloaded)
4237
4254
  reloadsDue.delete(room.locator);
4238
4255
  progress.reachable = false;
4239
- return [];
4240
4256
  }
4241
4257
  }
4242
- async function confirmModels(room, local) {
4243
- for (const attached of ['collection', 'typed-selection']) {
4244
- const model = modelOf(room.locator, attached);
4245
- if (model.status === 'cached') {
4246
- await retainModel(local, room, attached, { ...model, status: 'current' });
4258
+ async function settleModels(held, status, local) {
4259
+ const owed = (room) => {
4260
+ const model = modelOf(room.locator);
4261
+ return model.status !== 'unavailable' && model.status !== status;
4262
+ };
4263
+ const settled = await exclusive(async () => {
4264
+ if (!continuing())
4265
+ return [];
4266
+ const unchanged = held.filter((room) => owed(room) && declaredAsHeld(room));
4267
+ for (const room of unchanged) {
4268
+ const next = { ...modelOf(room.locator), status };
4269
+ const replica = replicas.get(room.locator);
4270
+ if (replica !== undefined)
4271
+ replicas.set(room.locator, { ...replica, record: { ...replica.record, model: next } });
4272
+ heldModel(room, next);
4247
4273
  }
4274
+ if (unchanged.length > 0)
4275
+ emitRooms();
4276
+ return unchanged;
4277
+ });
4278
+ for (const room of held) {
4279
+ if (settled.includes(room) || !owed(room))
4280
+ continue;
4281
+ await retainModel(local, room, { ...modelOf(room.locator), status });
4248
4282
  }
4249
4283
  }
4250
4284
  function submitLater(locator) {
@@ -4289,14 +4323,10 @@ export function composeRuntime(options) {
4289
4323
  try {
4290
4324
  const local = await openLocal();
4291
4325
  await roomTask(locator, async (live) => {
4292
- const current = () => live() && !signal.aborted;
4293
- if (!current() || authorization === undefined)
4326
+ if (!live() || authorization === undefined)
4294
4327
  return;
4295
- const withheld = await Promise.race([
4296
- advance(room, local, authorization, current),
4297
- abandonedOn(signal),
4298
- ]);
4299
- if (withheld !== null && current())
4328
+ const withheld = await advance(room, local, authorization, live);
4329
+ if (withheld !== null && live())
4300
4330
  await withhold(withheld, room, local, progress);
4301
4331
  });
4302
4332
  }
@@ -4351,14 +4381,11 @@ export function composeRuntime(options) {
4351
4381
  progress.complete = false;
4352
4382
  return;
4353
4383
  }
4354
- reads.applied();
4355
4384
  submitAll();
4356
4385
  for (const locator of stale)
4357
4386
  if (repairable.has(locator))
4358
4387
  repairLater(locator, {});
4359
4388
  await conversations.reconcile();
4360
- if (status.providerUnreachable)
4361
- conversations.reconnected();
4362
4389
  const reading = { complete: true, reachable: true };
4363
4390
  await conversations.catchUp(reading);
4364
4391
  if (progress.reachable && reading.reachable)
@@ -4384,7 +4411,7 @@ export function composeRuntime(options) {
4384
4411
  if (!live())
4385
4412
  return;
4386
4413
  let lastCompleted = status.lastCompleted;
4387
- let synced = complete && stale.size === 0 && !pending();
4414
+ let synced = complete && roomWork === 0 && naming === 0 && stale.size === 0 && !pending();
4388
4415
  if (synced && !recorded) {
4389
4416
  const completedAt = new Date().toISOString();
4390
4417
  try {
@@ -4399,74 +4426,69 @@ export function composeRuntime(options) {
4399
4426
  }
4400
4427
  }
4401
4428
  moveTo({
4402
- state: pending() ? 'pending' : synced && complete ? 'synced' : 'syncing',
4429
+ state: pending()
4430
+ ? 'pending'
4431
+ : synced && complete && roomWork === 0 && naming === 0
4432
+ ? 'synced'
4433
+ : 'syncing',
4403
4434
  lastCompleted,
4404
- providerUnreachable: !progress.reachable,
4435
+ providerUnreachable: progress === null ? status.providerUnreachable : !progress.reachable,
4405
4436
  ...attention(),
4406
4437
  });
4407
4438
  }
4408
- function discover() {
4409
- let release = () => { };
4410
- const done = new Promise((resolve) => {
4411
- release = resolve;
4412
- });
4413
- discovering = { generation, done, release };
4414
- return discovering;
4415
- }
4416
- function applyRoom(locator, roomVersion) {
4417
- announced.set(locator, roomVersion);
4418
- return task(roomLane(locator), async (live) => {
4419
- if (discovering?.generation === generation)
4420
- await discovering.done;
4421
- const saving = answering.get(locator);
4422
- if (saving?.generation === generation)
4423
- await saving.answered;
4424
- if (!live() || authorization === undefined)
4425
- return;
4426
- const room = discovered.get(locator);
4427
- const held = replicas.get(locator);
4428
- if (room === undefined || room.replica.admission !== 'admitted')
4429
- return;
4430
- if (held !== undefined &&
4431
- (held.record.replica.selection !== room.replica.selection ||
4432
- options.adapter.reached(locator, held.record.roomVersion, roomVersion))) {
4433
- return;
4434
- }
4435
- const during = underway?.generation === generation ? underway.progress : null;
4436
- const progress = during ?? { complete: true, reachable: true };
4437
- if (during === null)
4438
- moveTo(transferring(status));
4439
+ function applyRoom(change) {
4440
+ const { locator } = change;
4441
+ if (stopping)
4442
+ return Promise.resolve();
4443
+ return inRoom(locator, async (live) => {
4444
+ await answering.get(locator)?.answered;
4445
+ const progress = underway ?? { complete: true, reachable: true };
4439
4446
  try {
4440
4447
  const local = await openLocal();
4441
- await admittedRead(async () => {
4442
- if (!live() || authorization === undefined)
4443
- return;
4444
- const withheld = await advance(room, local, authorization, live);
4445
- if (withheld !== null)
4446
- await withhold(withheld, room, local, progress);
4447
- });
4448
+ for (;;) {
4449
+ if (!live())
4450
+ return null;
4451
+ const room = discovered.get(locator);
4452
+ const held = replicas.get(locator);
4453
+ if (room === undefined ||
4454
+ held === undefined ||
4455
+ room.replica.admission !== 'admitted' ||
4456
+ held.record.replica.selection !== room.replica.selection) {
4457
+ return null;
4458
+ }
4459
+ const at = held.record.roomVersion;
4460
+ if (options.adapter.reached(locator, at, change.roomVersion))
4461
+ return progress;
4462
+ if (!options.adapter.reached(locator, at, change.from)) {
4463
+ name(locator);
4464
+ return progress;
4465
+ }
4466
+ if (underway === null)
4467
+ moveTo(transferring(status));
4468
+ const writing = () => live() && !full;
4469
+ const refreshed = await ahead((hold) => pullRefresh(authorization ?? {}, held, writing, hold, () => change.read()));
4470
+ if (refreshed.kind !== 'advanced')
4471
+ return progress;
4472
+ const landing = {
4473
+ kind: 'refreshed',
4474
+ from: refreshed.from,
4475
+ remodelled: refreshed.remodelled,
4476
+ };
4477
+ if (await publish(local, room, refreshed.record, landing, writing)) {
4478
+ stale.delete(locator);
4479
+ await retainModel(local, room, modelOf(locator));
4480
+ return progress;
4481
+ }
4482
+ }
4448
4483
  }
4449
4484
  catch {
4485
+ if (!stale.has(locator))
4486
+ name(locator);
4450
4487
  stale.add(locator);
4488
+ return progress;
4451
4489
  }
4452
- finally {
4453
- if (during === null)
4454
- await rest(progress, live);
4455
- }
4456
- });
4490
+ }).then((progress) => rest(progress), () => undefined);
4457
4491
  }
4458
- function roomsCarried(replacing) {
4459
- const replaced = new Set(replacing.map(roomLane));
4460
- return Promise.all([...lanes]
4461
- .filter(([lane, held]) => lane !== OPENING_LANE && !replaced.has(lane) && held.generation === generation)
4462
- .map(([, held]) => held.chain));
4463
- }
4464
- const receipts = {
4465
- heads: async () => (await openLocal()).receivedHeads(),
4466
- parts: async (key, count) => (await openLocal()).receivedParts(key, count),
4467
- keep: async (key, head, part) => (await openLocal()).keepReceived(key, head, part),
4468
- drop: async (keys) => (await openLocal()).dropReceived(keys),
4469
- };
4470
4492
  function observeStream() {
4471
4493
  const stream = options.adapter.roomStream;
4472
4494
  if (stream === undefined || withdrawStream !== undefined)
@@ -4476,29 +4498,33 @@ export function composeRuntime(options) {
4476
4498
  return;
4477
4499
  if (event.kind === 'opened') {
4478
4500
  socketOpen = true;
4501
+ announced = false;
4502
+ handed = false;
4503
+ naming = 0;
4479
4504
  connectionLive();
4480
4505
  generation += 1;
4481
- reads.opened();
4482
4506
  conversations.reopened();
4483
4507
  recovery.resume();
4484
- void sync('opened');
4508
+ }
4509
+ else if (event.kind === 'discovered') {
4510
+ if (!socketOpen)
4511
+ return;
4512
+ announced = true;
4513
+ if (missedChange) {
4514
+ missedChange = false;
4515
+ countAuthorityChange();
4516
+ }
4517
+ void sync('opened', event.discovery);
4485
4518
  }
4486
4519
  else if (event.kind === 'closed')
4487
- closedSocket(event.revoked, event.lost);
4488
- else if (event.kind === 'opening')
4489
- void applyOpening(event.expired);
4520
+ closedSocket(event.revoked, event.lost, event.authorityChanged);
4521
+ else if (event.kind === 'caught-up')
4522
+ void caughtUpRooms(event.expired);
4490
4523
  else if (event.kind === 'authority-changed')
4491
- void rediscover(event.scope);
4524
+ void changeAuthority(event);
4492
4525
  else
4493
- void applyRoom(event.locator, event.roomVersion);
4494
- }, receipts, heldRevisions);
4495
- }
4496
- async function heldRevisions(locator, paths) {
4497
- const held = replicas.get(locator);
4498
- if (held === undefined || paths.length === 0)
4499
- return new Map();
4500
- const entries = await (await openLocal()).entries(held.record.key, 'confirmed', paths);
4501
- return new Map(entries.flatMap(({ path, revision }) => revision === null ? [] : [[path, revision]]));
4526
+ void applyRoom(event);
4527
+ });
4502
4528
  }
4503
4529
  function writesHeld() {
4504
4530
  return serviceMode === 'write-halted' || haltedRefusal;
@@ -4568,10 +4594,9 @@ export function composeRuntime(options) {
4568
4594
  if (closed)
4569
4595
  return;
4570
4596
  closed = true;
4597
+ notifyDiscovered();
4571
4598
  if (probing !== null)
4572
4599
  clearInterval(probing);
4573
- for (const resolve of shownWaiters.splice(0))
4574
- resolve();
4575
4600
  connectionLive();
4576
4601
  lockListeners.clear();
4577
4602
  readinessWatches.clear();
@@ -4583,11 +4608,13 @@ export function composeRuntime(options) {
4583
4608
  conversations.close();
4584
4609
  void recovery.retire();
4585
4610
  roomListeners.clear();
4611
+ unlistedRooms.clear();
4586
4612
  refusalListeners.clear();
4587
4613
  storageLossListeners.clear();
4588
4614
  statusListeners.clear();
4589
4615
  serviceModeListeners.clear();
4590
4616
  memberListeners.clear();
4617
+ authorityChangeListeners.clear();
4591
4618
  liveQueries.close();
4592
4619
  queueSubscriptions.clear();
4593
4620
  roomDraftListeners.clear();
@@ -4595,6 +4622,7 @@ export function composeRuntime(options) {
4595
4622
  }
4596
4623
  async function stop() {
4597
4624
  stopping = true;
4625
+ inFlight.abort();
4598
4626
  await recovery.retire();
4599
4627
  await Promise.all(working);
4600
4628
  await mutations;
@@ -4608,20 +4636,20 @@ export function composeRuntime(options) {
4608
4636
  observeStream();
4609
4637
  void openLocal().then(() => {
4610
4638
  if (capabilities.length > 0)
4611
- notifyReady();
4639
+ readyFromStore();
4612
4640
  }, () => {
4613
- notifyReady();
4641
+ readyFromStore();
4614
4642
  });
4615
4643
  if (options.adapter.roomStream === undefined)
4616
- void sync('unstreamed');
4644
+ void sync('unstreamed', null);
4617
4645
  function planned(input, reach) {
4618
- const plan = planOf(input, roomFacts(reach), planReach(reach));
4646
+ const plan = planWithin(input, roomFacts(reach), planReach(reach));
4619
4647
  const named = plan.paths !== null &&
4620
4648
  plan.rooms !== null &&
4621
4649
  plan.paths.length * plan.rooms.length <= MAXIMUM_WINDOW;
4622
4650
  return plan.limit === null && named ? plan : { ...plan, limit: capped(plan.limit) };
4623
4651
  }
4624
- function watchPlan(plan, deliver) {
4652
+ function watchPlan(plan, deliver, fail) {
4625
4653
  let seeded = false;
4626
4654
  let pending = true;
4627
4655
  const shown = () => {
@@ -4647,7 +4675,7 @@ export function composeRuntime(options) {
4647
4675
  }
4648
4676
  seeded = false;
4649
4677
  deliver(message);
4650
- });
4678
+ }, fail);
4651
4679
  return () => {
4652
4680
  watchers.delete(shown);
4653
4681
  if (watchers.size === 0)
@@ -4655,61 +4683,59 @@ export function composeRuntime(options) {
4655
4683
  stop();
4656
4684
  };
4657
4685
  }
4658
- async function searched(input, reach, locked) {
4686
+ async function searched(input, reach) {
4659
4687
  const asked = searchPlanOf(input);
4660
- const filter = {
4661
- ...filterOf(asked.documents ?? {}, roomFacts(reach), planReach(reach)),
4662
- locked,
4663
- };
4664
- const unlocking = (filter.rooms ?? [])
4665
- .filter((room) => locked.includes(room))
4666
- .map((room) => ({ room, awaits: 'unlock' }));
4667
- for (;;) {
4668
- const cover = covered(filter);
4669
- awaitPreparation(cover);
4670
- const pending = [...unlocking, ...('pending' in cover ? cover.pending : [])];
4671
- const complete = pending.length === 0 && !('pending' in cover);
4672
- const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
4673
- const answering = complete
4674
- ? filter
4675
- : { ...filter, rooms: reachedRooms(filter).filter((room) => !awaited.has(room)) };
4676
- const answered = covered(answering);
4677
- if ('pending' in answered)
4678
- continue;
4679
- const listed = conversations.searchable();
4680
- const named = reach !== null && !reach.conversations
4681
- ? []
4682
- : (asked.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
4683
- const hits = await (await openLocal()).search({
4684
- text: asked.text,
4685
- limit: capped(asked.limit),
4686
- after: asked.after,
4687
- total: asked.total,
4688
- documents: {
4689
- plan: answering,
4690
- rooms: answered.rooms,
4691
- beneath: asked.collection
4692
- ? answered.rooms.map(({ locator, key }) => ({
4693
- key,
4694
- roots: replicas.get(locator)?.record.typedSelection?.roots ?? [],
4695
- }))
4696
- : [],
4697
- },
4698
- conversations: named,
4699
- });
4700
- if (JSON.stringify(covered(answering)) !== JSON.stringify(answered))
4701
- continue;
4702
- const coverage = {
4703
- rooms: answered.coverage.rooms,
4704
- conversations: named,
4705
- overlay: answered.coverage.overlay,
4688
+ await ready;
4689
+ const search = options.adapter.search;
4690
+ if (search === undefined) {
4691
+ return {
4692
+ kind: 'undeclared',
4693
+ code: 'action-undeclared',
4694
+ fields: { name: 'search' },
4695
+ retryable: false,
4706
4696
  };
4707
- return complete
4708
- ? { ...hits, coverage }
4709
- : { pending, documents: hits.documents, messages: hits.messages, coverage };
4697
+ }
4698
+ if (closed || refusal !== null || connectionOf() !== 'connected')
4699
+ return OFFLINE;
4700
+ const reached = reach === null ? null : new Set(reach.rooms);
4701
+ const named = asked.rooms === null
4702
+ ? reached === null
4703
+ ? null
4704
+ : [...reached]
4705
+ : asked.rooms.filter((room) => reached === null || reached.has(room));
4706
+ const rooms = named === null ? null : named.filter((room) => replicas.has(room)).sort();
4707
+ if (rooms !== null && rooms.length === 0)
4708
+ return { kind: 'found', documents: [] };
4709
+ try {
4710
+ await ensureAuthorization();
4711
+ }
4712
+ catch {
4713
+ return UNCERTAIN;
4714
+ }
4715
+ if (closed || authorization === undefined || refusal !== null)
4716
+ return UNCERTAIN;
4717
+ journal.record('transfer search');
4718
+ try {
4719
+ const result = await search(authorization, { text: asked.text, rooms });
4720
+ if (result.kind === 'revoked-authorization')
4721
+ await refuse('revoked-authorization');
4722
+ journal.record(`outcome search ${result.kind}`);
4723
+ if (result.kind === 'rejected')
4724
+ return result;
4725
+ if (result.kind !== 'found')
4726
+ return failure(result);
4727
+ const kept = rooms === null ? null : new Set(rooms);
4728
+ return {
4729
+ kind: 'found',
4730
+ documents: result.documents.filter((hit) => kept === null || kept.has(hit.room)),
4731
+ };
4732
+ }
4733
+ catch {
4734
+ journal.record('outcome search transport-uncertainty');
4735
+ return UNCERTAIN;
4710
4736
  }
4711
4737
  }
4712
- const watch = (topic, input, deliver) => {
4738
+ const watch = (topic, input, deliver, fail) => {
4713
4739
  if (topic === 'room-drafts') {
4714
4740
  const listener = { notify: deliver, ready: false };
4715
4741
  roomDraftListeners.add(listener);
@@ -4758,12 +4784,13 @@ export function composeRuntime(options) {
4758
4784
  };
4759
4785
  }
4760
4786
  if (topic === 'readiness') {
4761
- return watchReadiness({ target: '', code: 'stored', space: null, search: 'unused' }, null, deliver);
4787
+ return watchReadiness({ target: '', code: 'stored' }, null, deliver);
4762
4788
  }
4763
4789
  if (topic === 'rooms') {
4764
- roomListeners.add(deliver);
4765
- deliver(listed());
4790
+ unlistedRooms.add(deliver);
4791
+ emitKnownRooms();
4766
4792
  return () => {
4793
+ unlistedRooms.delete(deliver);
4767
4794
  roomListeners.delete(deliver);
4768
4795
  };
4769
4796
  }
@@ -4802,6 +4829,13 @@ export function composeRuntime(options) {
4802
4829
  memberListeners.delete(deliver);
4803
4830
  };
4804
4831
  }
4832
+ if (topic === 'authority-changes') {
4833
+ authorityChangeListeners.add(deliver);
4834
+ deliver(authorityChanges);
4835
+ return () => {
4836
+ authorityChangeListeners.delete(deliver);
4837
+ };
4838
+ }
4805
4839
  if (topic === 'queue') {
4806
4840
  const queue = { locator: input[0], notify: deliver };
4807
4841
  queueSubscriptions.add(queue);
@@ -4810,24 +4844,34 @@ export function composeRuntime(options) {
4810
4844
  queueSubscriptions.delete(queue);
4811
4845
  };
4812
4846
  }
4813
- return watchPlan(planned(input[0], null), deliver);
4847
+ return watchPlan(planned(input[0], null), deliver, fail);
4814
4848
  };
4815
4849
  const md = {
4816
4850
  loaded() {
4817
4851
  return openLocal().then(() => undefined);
4818
4852
  },
4819
4853
  rooms() {
4820
- return listed();
4854
+ if (closed)
4855
+ return Promise.resolve([]);
4856
+ return new Promise((settle) => {
4857
+ const answer = (value) => {
4858
+ unlistedRooms.delete(answer);
4859
+ roomListeners.delete(answer);
4860
+ settle(value);
4861
+ };
4862
+ unlistedRooms.add(answer);
4863
+ emitKnownRooms();
4864
+ });
4821
4865
  },
4822
4866
  query: ((input) => rowsQuery(planned(input, null))),
4823
- search: (input) => searched(input, null, []),
4867
+ search: (input) => searched(input, null),
4824
4868
  types(locator) {
4825
4869
  return {
4826
4870
  collection: modelTypes(modelOf(locator)),
4827
4871
  typedSelection: rooms.get(locator)?.typedSelection === undefined ||
4828
4872
  rooms.get(locator)?.typedSelection === null
4829
4873
  ? null
4830
- : modelTypes(modelOf(locator, 'typed-selection')),
4874
+ : modelTypes(builtIn),
4831
4875
  };
4832
4876
  },
4833
4877
  roomDraft: {
@@ -4844,33 +4888,17 @@ export function composeRuntime(options) {
4844
4888
  const record = roomDrafts.get(room)?.get(path);
4845
4889
  return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
4846
4890
  },
4847
- write(room, path, revision, input) {
4848
- return exclusive(async () => {
4849
- const parts = readParts(input);
4850
- if (path === '' || normalize(path) !== path)
4851
- throw runtimeError('room-draft-malformed');
4852
- const local = await openLocal();
4853
- const current = roomDrafts.get(room)?.get(path);
4854
- if ((current?.revision ?? null) !== revision) {
4855
- return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
4856
- }
4857
- const file = parts.file === undefined
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
- });
4891
+ write(room, path, revision, input, next = roomDraftRevision()) {
4892
+ if (typeof next !== 'string' || next === '')
4893
+ return Promise.reject(runtimeError('room-draft-malformed'));
4894
+ return gathered(`room-draft\u0000${room}\u0000${path}`, { revision, input, next }, (earlier, later) => {
4895
+ if (later.revision !== earlier.next)
4896
+ return null;
4897
+ const parts = gatheredParts(earlier.input, later.input);
4898
+ return parts === null
4899
+ ? null
4900
+ : { revision: earlier.revision, input: parts, next: later.next };
4901
+ }, (written) => writtenRoomDraft(room, path, written.revision, written.input, written.next));
4874
4902
  },
4875
4903
  discard(room, path, revision) {
4876
4904
  return exclusive(async () => {
@@ -4897,13 +4925,13 @@ export function composeRuntime(options) {
4897
4925
  });
4898
4926
  },
4899
4927
  write(app, value) {
4900
- return keepPresentation(app, () => structuredClone(value));
4928
+ return exclusive(() => keptPresentation(app, () => structuredClone(value)));
4901
4929
  },
4902
4930
  edit(app, members) {
4903
- return keepPresentation(app, (held) => Object.fromEntries([
4904
- ...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(members, member)),
4905
- ...Object.entries(structuredClone(members)).filter(([, value]) => value !== null),
4906
- ]));
4931
+ return gathered(`presentation\u0000${app}`, structuredClone(members), (earlier, later) => ({ ...earlier, ...later }), (edited) => keptPresentation(app, (held) => Object.fromEntries([
4932
+ ...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(edited, member)),
4933
+ ...Object.entries(edited).filter(([, value]) => value !== null),
4934
+ ])));
4907
4935
  },
4908
4936
  },
4909
4937
  save(room, named, writtenBy) {
@@ -4922,6 +4950,8 @@ export function composeRuntime(options) {
4922
4950
  part: readPart,
4923
4951
  read: readBinaryPath,
4924
4952
  link: linkBinaryPath,
4953
+ show: showBinaryPath,
4954
+ attach: attachBinary,
4925
4955
  },
4926
4956
  async call(name, input) {
4927
4957
  await ready;
@@ -4937,6 +4967,8 @@ export function composeRuntime(options) {
4937
4967
  return journal.export();
4938
4968
  },
4939
4969
  cleanUp,
4970
+ unsent: () => database.ask({ kind: 'authored.unsent' }),
4971
+ reset: () => options.shell.reset(),
4940
4972
  persistence() {
4941
4973
  return persistence === 'granted';
4942
4974
  },
@@ -4959,7 +4991,7 @@ export function composeRuntime(options) {
4959
4991
  authorization = undefined;
4960
4992
  discoveryPaused = false;
4961
4993
  if (options.adapter.roomStream === undefined)
4962
- await sync('unstreamed');
4994
+ await sync('unstreamed', null);
4963
4995
  else
4964
4996
  await task(OPENING_LANE, ensureAuthorization);
4965
4997
  },
@@ -4976,12 +5008,12 @@ export function composeRuntime(options) {
4976
5008
  lockedRooms,
4977
5009
  watchLocks,
4978
5010
  located,
5011
+ room: (locator) => listed().find((one) => one.locator === locator),
4979
5012
  };
4980
5013
  return {
4981
5014
  md,
4982
5015
  seed,
4983
5016
  stop,
4984
- lose: loseRooms,
4985
5017
  watch,
4986
5018
  persisted(answer) {
4987
5019
  if (answer === persistence)
@@ -4990,7 +5022,6 @@ export function composeRuntime(options) {
4990
5022
  journal.record(`persistence ${answer}`);
4991
5023
  queueReadiness();
4992
5024
  },
4993
- shown: screen,
4994
5025
  within: (reach, port) => {
4995
5026
  if (full)
4996
5027
  void probed();