@agentshouse/mdruntime 0.1.0-c9b1e59e5baeb → 0.1.0-c9f3bd0af990c

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 (232) hide show
  1. package/README.md +18 -15
  2. package/dist/adapter.d.ts +58 -32
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +38 -4
  7. package/dist/binary.d.ts +0 -5
  8. package/dist/binary.d.ts.map +1 -1
  9. package/dist/binary.js +1 -1
  10. package/dist/bootstrap.d.ts +30 -8
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +187 -85
  13. package/dist/chain.d.ts +11 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +6 -16
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -94
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +9 -5
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +80 -6
  25. package/dist/collection.d.ts +11 -20
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +53 -354
  28. package/dist/compile.d.ts +16 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +765 -0
  31. package/dist/connection.d.ts +18 -10
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +2593 -993
  34. package/dist/conversation.d.ts +7 -3
  35. package/dist/conversation.d.ts.map +1 -1
  36. package/dist/conversation.js +152 -83
  37. package/dist/digest.d.ts +7 -0
  38. package/dist/digest.d.ts.map +1 -0
  39. package/dist/digest.js +113 -0
  40. package/dist/durability.d.ts +3 -5
  41. package/dist/durability.d.ts.map +1 -1
  42. package/dist/durability.js +5 -0
  43. package/dist/entry.d.ts +3 -1
  44. package/dist/entry.d.ts.map +1 -1
  45. package/dist/entry.js +18 -24
  46. package/dist/file.d.ts +2 -19
  47. package/dist/file.d.ts.map +1 -1
  48. package/dist/file.js +7 -122
  49. package/dist/fulltext.d.ts +27 -0
  50. package/dist/fulltext.d.ts.map +1 -0
  51. package/dist/fulltext.js +368 -0
  52. package/dist/host.d.ts +4 -4
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +45 -15
  55. package/dist/identity.d.ts +0 -1
  56. package/dist/identity.d.ts.map +1 -1
  57. package/dist/identity.js +0 -3
  58. package/dist/index.d.ts +11 -9
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +4 -1
  61. package/dist/live.d.ts +22 -0
  62. package/dist/live.d.ts.map +1 -0
  63. package/dist/live.js +124 -0
  64. package/dist/media.d.ts +8 -0
  65. package/dist/media.d.ts.map +1 -0
  66. package/dist/media.js +208 -0
  67. package/dist/memory.d.ts +2 -1
  68. package/dist/memory.d.ts.map +1 -1
  69. package/dist/memory.js +63 -22
  70. package/dist/messages.d.ts +28 -0
  71. package/dist/messages.d.ts.map +1 -0
  72. package/dist/messages.js +90 -0
  73. package/dist/model.d.ts +6 -0
  74. package/dist/model.d.ts.map +1 -1
  75. package/dist/model.js +7 -1
  76. package/dist/name.d.ts +4 -0
  77. package/dist/name.d.ts.map +1 -0
  78. package/dist/name.js +122 -0
  79. package/dist/observation.d.ts +13 -0
  80. package/dist/observation.d.ts.map +1 -1
  81. package/dist/observation.js +84 -0
  82. package/dist/opened.d.ts +22 -0
  83. package/dist/opened.d.ts.map +1 -0
  84. package/dist/opened.js +107 -0
  85. package/dist/outcome.d.ts +40 -5
  86. package/dist/outcome.d.ts.map +1 -1
  87. package/dist/outcome.js +38 -2
  88. package/dist/overlay.d.ts +1 -4
  89. package/dist/overlay.d.ts.map +1 -1
  90. package/dist/overlay.js +22 -148
  91. package/dist/paths.d.ts +1 -0
  92. package/dist/paths.d.ts.map +1 -1
  93. package/dist/paths.js +4 -0
  94. package/dist/query.d.ts +11 -1
  95. package/dist/query.d.ts.map +1 -1
  96. package/dist/query.js +13 -1
  97. package/dist/reach.d.ts +13 -4
  98. package/dist/reach.d.ts.map +1 -1
  99. package/dist/reach.js +201 -72
  100. package/dist/readiness.d.ts +20 -9
  101. package/dist/readiness.d.ts.map +1 -1
  102. package/dist/readiness.js +6 -6
  103. package/dist/reads.d.ts.map +1 -1
  104. package/dist/reads.js +4 -2
  105. package/dist/recovery.d.ts +2 -2
  106. package/dist/recovery.d.ts.map +1 -1
  107. package/dist/recovery.js +8 -7
  108. package/dist/refresh.d.ts +15 -14
  109. package/dist/refresh.d.ts.map +1 -1
  110. package/dist/refresh.js +113 -75
  111. package/dist/replica.d.ts +56 -39
  112. package/dist/replica.d.ts.map +1 -1
  113. package/dist/replica.js +278 -197
  114. package/dist/room-draft.d.ts +4 -1
  115. package/dist/room-draft.d.ts.map +1 -1
  116. package/dist/room-draft.js +54 -13
  117. package/dist/rows.d.ts +38 -31
  118. package/dist/rows.d.ts.map +1 -1
  119. package/dist/rows.js +173 -101
  120. package/dist/save.d.ts +51 -14
  121. package/dist/save.d.ts.map +1 -1
  122. package/dist/save.js +363 -103
  123. package/dist/search.d.ts +11 -43
  124. package/dist/search.d.ts.map +1 -1
  125. package/dist/search.js +0 -213
  126. package/dist/shared.d.ts +4 -3
  127. package/dist/shared.d.ts.map +1 -1
  128. package/dist/shared.js +67 -34
  129. package/dist/status.d.ts +5 -0
  130. package/dist/status.d.ts.map +1 -1
  131. package/dist/status.js +3 -1
  132. package/dist/store.d.ts +96 -96
  133. package/dist/store.d.ts.map +1 -1
  134. package/dist/store.js +334 -720
  135. package/dist/stored.d.ts +154 -0
  136. package/dist/stored.d.ts.map +1 -0
  137. package/dist/stored.js +624 -0
  138. package/dist/tables.d.ts +362 -0
  139. package/dist/tables.d.ts.map +1 -0
  140. package/dist/tables.js +627 -0
  141. package/dist/turn.d.ts +1 -1
  142. package/dist/turn.d.ts.map +1 -1
  143. package/dist/turn.js +31 -20
  144. package/dist/validation.d.ts +30 -0
  145. package/dist/validation.d.ts.map +1 -0
  146. package/dist/validation.js +162 -0
  147. package/dist/windows.d.ts +2 -6
  148. package/dist/windows.d.ts.map +1 -1
  149. package/dist/windows.js +30 -34
  150. package/dist/wire.d.ts +17 -1
  151. package/dist/wire.d.ts.map +1 -1
  152. package/dist/wire.js +5 -1
  153. package/dist/words.d.ts +19 -0
  154. package/dist/words.d.ts.map +1 -0
  155. package/dist/words.js +156 -0
  156. package/package.json +18 -19
  157. package/AGENTS.md +0 -299
  158. package/dist/chat/acts.d.ts +0 -33
  159. package/dist/chat/acts.d.ts.map +0 -1
  160. package/dist/chat/acts.js +0 -36
  161. package/dist/chat/context.d.ts +0 -3
  162. package/dist/chat/context.d.ts.map +0 -1
  163. package/dist/chat/context.js +0 -10
  164. package/dist/chat/earlier.d.ts +0 -10
  165. package/dist/chat/earlier.d.ts.map +0 -1
  166. package/dist/chat/earlier.js +0 -54
  167. package/dist/chat/input.d.ts +0 -9
  168. package/dist/chat/input.d.ts.map +0 -1
  169. package/dist/chat/input.js +0 -99
  170. package/dist/chat/kit.d.ts +0 -61
  171. package/dist/chat/kit.d.ts.map +0 -1
  172. package/dist/chat/kit.js +0 -123
  173. package/dist/chat/lasting.d.ts +0 -6
  174. package/dist/chat/lasting.d.ts.map +0 -1
  175. package/dist/chat/lasting.js +0 -19
  176. package/dist/chat/list.d.ts +0 -28
  177. package/dist/chat/list.d.ts.map +0 -1
  178. package/dist/chat/list.js +0 -99
  179. package/dist/chat/markdown.d.ts +0 -21
  180. package/dist/chat/markdown.d.ts.map +0 -1
  181. package/dist/chat/markdown.js +0 -246
  182. package/dist/chat/message.d.ts +0 -6
  183. package/dist/chat/message.d.ts.map +0 -1
  184. package/dist/chat/message.js +0 -199
  185. package/dist/chat/messages.d.ts +0 -22
  186. package/dist/chat/messages.d.ts.map +0 -1
  187. package/dist/chat/messages.js +0 -175
  188. package/dist/chat/name.d.ts +0 -6
  189. package/dist/chat/name.d.ts.map +0 -1
  190. package/dist/chat/name.js +0 -37
  191. package/dist/chat/opened.d.ts +0 -11
  192. package/dist/chat/opened.d.ts.map +0 -1
  193. package/dist/chat/opened.js +0 -17
  194. package/dist/chat/overlay.d.ts +0 -32
  195. package/dist/chat/overlay.d.ts.map +0 -1
  196. package/dist/chat/overlay.js +0 -107
  197. package/dist/chat/parts.d.ts +0 -57
  198. package/dist/chat/parts.d.ts.map +0 -1
  199. package/dist/chat/parts.js +0 -119
  200. package/dist/chat/pieces.d.ts +0 -62
  201. package/dist/chat/pieces.d.ts.map +0 -1
  202. package/dist/chat/pieces.js +0 -1110
  203. package/dist/chat/process.d.ts +0 -20
  204. package/dist/chat/process.d.ts.map +0 -1
  205. package/dist/chat/process.js +0 -32
  206. package/dist/chat/provider.d.ts +0 -64
  207. package/dist/chat/provider.d.ts.map +0 -1
  208. package/dist/chat/provider.js +0 -637
  209. package/dist/chat/question.d.ts +0 -9
  210. package/dist/chat/question.d.ts.map +0 -1
  211. package/dist/chat/question.js +0 -69
  212. package/dist/chat/questions.d.ts +0 -31
  213. package/dist/chat/questions.d.ts.map +0 -1
  214. package/dist/chat/questions.js +0 -111
  215. package/dist/chat/read.d.ts +0 -5
  216. package/dist/chat/read.d.ts.map +0 -1
  217. package/dist/chat/read.js +0 -13
  218. package/dist/chat/ready.d.ts +0 -34
  219. package/dist/chat/ready.d.ts.map +0 -1
  220. package/dist/chat/ready.js +0 -91
  221. package/dist/chat/steady.d.ts +0 -3
  222. package/dist/chat/steady.d.ts.map +0 -1
  223. package/dist/chat/steady.js +0 -26
  224. package/dist/chat/style.d.ts +0 -4
  225. package/dist/chat/style.d.ts.map +0 -1
  226. package/dist/chat/style.js +0 -302
  227. package/dist/chat.d.ts +0 -14
  228. package/dist/chat.d.ts.map +0 -1
  229. package/dist/chat.js +0 -8
  230. package/dist/read-side.d.ts +0 -27
  231. package/dist/read-side.d.ts.map +0 -1
  232. package/dist/read-side.js +0 -826
@@ -1,47 +1,42 @@
1
- import { attachedModel } from './attached-model.js';
1
+ import { escapePointer, parseFrontmatter, roomTarget, target } from '@agentshouse/mdmodel';
2
+ import { attachedModel, builtInModel, roomTypesOf } from './attached-model.js';
2
3
  import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
3
4
  import { observing, whole } from './observation.js';
4
5
  import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
5
- import { createReadSide } from './read-side.js';
6
+ import { createLiveQueries } from './live.js';
6
7
  import { seededResult } from './seed.js';
7
- import { markdownPath, roomOfKey } from './rows.js';
8
- import { createSearchIndex, searchPlanOf, } from './search.js';
8
+ import { documentKey, roomOfKey } from './rows.js';
9
+ import { searchPlanOf } from './search.js';
9
10
  import { capped, MAXIMUM_WINDOW } from './windows.js';
10
- import { bootstrapRoom } from './bootstrap.js';
11
- import { changeSet, changing, entering, mergeChangeSets } from './change-set.js';
11
+ import { bootstrapRoom, NOTHING_DONE } from './bootstrap.js';
12
+ import { changeSet, mergeChangeSets } from './change-set.js';
12
13
  import { createConversationStore, } from './conversation.js';
13
- import { queuedFile } from './change.js';
14
- import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
14
+ import { leaving as leavingIn, partnerOf, queuedFile, } from './change.js';
15
+ import { available, collectionOf, composedOf, unreadable as unreadableIn, validRoot, } from './collection.js';
15
16
  import { runtimeError } from './failure.js';
16
17
  import { createLog } from './log.js';
17
18
  import { binaryRecord } from './binary.js';
18
- import { absentBinary, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
19
+ import { attachedFile } from './file.js';
20
+ import { openedFiles } from './opened.js';
21
+ import { ABSENT_TARGET, absentBinary, attachAnswer, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, OFFLINE, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
19
22
  import { createRecovery } from './recovery.js';
20
23
  import { doorReads } from './reads.js';
21
- import { refreshRoom } from './refresh.js';
22
- import { buildBatch, joined, limitedIn, queuedDocuments, requeued, } from './save.js';
23
- import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, replicating, sameMembership, standing, } from './replica.js';
24
- import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
24
+ import { advancement } from './refresh.js';
25
+ import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
26
+ import { compositionOf, entryAt, landed, movedBy, overlayOf, replaying, returning, returnedPaths, sameMembership, slicing, slicingWith, standing, storedRecord, touchedBy, } from './replica.js';
27
+ import { binaryBase, changedParts, changesOf, gatheredParts, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
25
28
  import { same, saving, STARTING, transferring, } from './status.js';
26
- import { detectStorageLoss, nextGeneration } from './durability.js';
27
- import { readinessOf, SETTLED_ROOMS, } from './readiness.js';
29
+ import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
30
+ import { readinessOf, } from './readiness.js';
28
31
  import { FencedMap } from './fence.js';
29
- import { tally } from './shared.js';
30
- import { inChunks, ROW_WEIGHT, stepped, yielded } from './turn.js';
32
+ import { sameBinary } from './overlay.js';
33
+ import { paced, ROW_WEIGHT, stepped, yielded } from './turn.js';
31
34
  import { identityTime, roomDraftRevision } from './identity.js';
32
- import { beneath, normalize } from './paths.js';
35
+ import { ancestors, beneath, normalize } from './paths.js';
33
36
  import { openRuntimeStore, } from './store.js';
37
+ import { changedCleanly, sameComposition, validatedCollection, } from './validation.js';
34
38
  import { unconfirmed } from './transfer.js';
35
39
  import { governs, holds, narrowed, planReach, } from './reach.js';
36
- function roomsAlone(plan) {
37
- return (plan.reach === null &&
38
- plan.root.types === null &&
39
- plan.root.where === null &&
40
- plan.root.pathPrefix === null &&
41
- plan.paths === null &&
42
- plan.hop === null &&
43
- plan.backlinks === null);
44
- }
45
40
  function within(root, path) {
46
41
  return root === '' || beneath(root, path);
47
42
  }
@@ -59,24 +54,30 @@ const OPENING_LANE = 'opening';
59
54
  const CONVERSATION_LANE = 'conversations';
60
55
  const ROOM_READS = 256;
61
56
  const ROOM_ATTEMPTS = 16;
57
+ const ERASE_BATCH = 512;
58
+ const DERIVE_BYTES = 1 << 23;
59
+ const DERIVE_FLOOR = 1 << 18;
60
+ // An index build aims at this much database work, so a Save, a query or the next part's progress waits behind at most one short build.
61
+ const DERIVE_AIM_MS = 250;
62
+ // A build past its floor of text stops after a second and a half of work, so a Room whose text costs ten times the usual still reports progress within the 2 s bound.
63
+ const DERIVE_LONGEST_MS = 1_500;
64
+ const WALK_BYTES = 1 << 20;
65
+ const QUERY_BYTES = 1 << 20;
66
+ const STAGE_BYTES = 1 << 20;
67
+ const STAGE_FLOOR = 1 << 16;
68
+ // A batch aims at this much database work, so a Save or a query waits behind at most one short write.
69
+ const STAGE_AIM_MS = 50;
70
+ const PARTS_HELD = 4;
71
+ const QUEUED_BYTES = 1 << 23;
72
+ // 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.
73
+ const ARRIVAL_REPORT_MS = 250;
74
+ const CLEAN_CHANGES = 2_000;
75
+ // 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.
76
+ const LOCK_GRACE_MS = 120_000;
62
77
  function operationOf(input) {
63
78
  const operation = input?.operation_id;
64
79
  return typeof operation === 'string' ? operation : null;
65
80
  }
66
- const outside = new Map();
67
- function outsideOf(roots) {
68
- const key = JSON.stringify(roots);
69
- const held = outside.get(key);
70
- if (held !== undefined)
71
- return held;
72
- const counted = (path) => (roots.some((root) => beneath(root, path)) ? 0 : 1);
73
- const made = {
74
- documents: (path) => (markdownPath(path) ? counted(path) : 0),
75
- binaries: counted,
76
- };
77
- outside.set(key, made);
78
- return made;
79
- }
80
81
  function abandonedOn(signal) {
81
82
  return new Promise((resolve) => {
82
83
  if (signal.aborted)
@@ -88,11 +89,14 @@ function abandonedOn(signal) {
88
89
  function roomLane(locator) {
89
90
  return `room ${locator}`;
90
91
  }
92
+ function pendingKey(locator, id) {
93
+ return `${locator}\u0000${id}`;
94
+ }
91
95
  export function composeRuntime(options) {
92
96
  const localSaves = options.adapter.localSaves === true;
97
+ const roomTypes = roomTypesOf(options.adapter.roomTypes, options.adapter.nameableRoomTypes);
93
98
  let authorization = options.authorization;
94
99
  let opening;
95
- let store;
96
100
  const fences = new Map();
97
101
  const isFenced = (locator) => fences.has(locator);
98
102
  let fencings = 0;
@@ -100,20 +104,23 @@ export function composeRuntime(options) {
100
104
  const fencedSince = (locator, since) => fences.has(locator) || (lastFenced.get(locator) ?? 0) > since;
101
105
  let discoveries = 0;
102
106
  const replicas = new FencedMap(isFenced);
107
+ const reopened = new Set();
108
+ const usableWhenHeld = options.usableWhenHeld ?? [];
103
109
  const queuesWithoutReplica = new FencedMap(isFenced);
104
110
  const pendings = new Map();
105
111
  const seeds = new Map();
106
112
  const seedWatchers = new Map();
107
113
  const roomDrafts = new FencedMap(isFenced);
114
+ const attachedFiles = new FencedMap(isFenced);
108
115
  const presentations = new Map();
109
116
  let limits = [];
110
117
  let capabilities = [];
111
118
  const rooms = new FencedMap(isFenced);
112
119
  const counted = new Map();
113
120
  const unreadable = new Set();
114
- const isModelFenced = (key) => isFenced(key.slice(key.indexOf(' ') + 1));
115
- const models = new FencedMap(isModelFenced);
116
- const liveModels = new FencedMap(isModelFenced);
121
+ const builtIn = builtInModel(roomTypes);
122
+ const models = new FencedMap(isFenced);
123
+ const liveModels = new FencedMap(isFenced);
117
124
  const invalidModels = new Set();
118
125
  const discovered = new Map();
119
126
  const reads = doorReads(options.adapter, (locator) => discovered.get(locator), () => generation);
@@ -121,23 +128,19 @@ export function composeRuntime(options) {
121
128
  const scopedRefusals = new Map();
122
129
  const sourceRefusals = new Map();
123
130
  const declarationRefusals = new Set();
124
- function modelKey(locator, attached) {
125
- return `${attached} ${locator}`;
131
+ function modelOf(locator) {
132
+ return models.get(locator) ?? NO_MODEL;
126
133
  }
127
- function modelOf(locator, attached = 'collection') {
128
- return models.get(modelKey(locator, attached)) ?? NO_MODEL;
129
- }
130
- function savedModel(locator, attached) {
131
- return invalidModels.has(modelKey(locator, attached))
134
+ function savedModel(locator) {
135
+ return invalidModels.has(locator)
132
136
  ? { status: 'unavailable', reason: 'revoked' }
133
- : modelOf(locator, attached);
137
+ : modelOf(locator);
138
+ }
139
+ function composed(record) {
140
+ return compositionOf(record, builtIn);
134
141
  }
135
142
  function compositionFor(record) {
136
- return compositionOf({
137
- ...record,
138
- model: savedModel(record.locator, 'collection'),
139
- selectionModel: savedModel(record.locator, 'typed-selection'),
140
- });
143
+ return composed({ ...record, model: savedModel(record.locator) });
141
144
  }
142
145
  let refusal = null;
143
146
  let storageLoss = null;
@@ -169,61 +172,117 @@ export function composeRuntime(options) {
169
172
  let roomReads = { generation, free: ROOM_READS, waiting: [] };
170
173
  const working = new Set();
171
174
  let mutations = Promise.resolve();
175
+ let gathering = null;
172
176
  const roomListeners = new Set();
177
+ const unlistedRooms = new Set();
173
178
  const refusalListeners = new Set();
174
179
  const storageLossListeners = new Set();
175
180
  const statusListeners = new Set();
176
181
  const queueSubscriptions = new Set();
177
182
  const roomDraftListeners = new Set();
178
183
  const presentationListeners = new Set();
179
- const readSide = createReadSide();
184
+ const liveQueries = createLiveQueries((plan, cover) => answeredPlan(plan, cover));
180
185
  let withdrawConversations;
181
186
  let currencyQueued = null;
182
187
  let socketOpen = false;
188
+ const confirmed = new Set();
189
+ let lockGrace = null;
190
+ const lockListeners = new Set();
183
191
  let serviceMode = 'open';
184
192
  let haltedRefusal = false;
185
193
  let openSignals = 0;
186
194
  const serviceModeListeners = new Set();
187
195
  let member = null;
188
196
  const memberListeners = new Set();
189
- const prepared = new Set();
190
- const projecting = new Set();
191
197
  const unpreparedDemand = new Set();
192
- const unindexable = new Set();
198
+ const unpreparable = new Set();
193
199
  let preparing = false;
194
200
  let targets = {};
195
201
  let persistence = options.shell.persistence;
196
- let capacity = null;
197
- const paused = new Set();
198
- let rechecking = null;
202
+ let full = false;
203
+ let probing = null;
204
+ let testing = null;
205
+ const database = {
206
+ async ask(request) {
207
+ try {
208
+ return await options.database.ask(request);
209
+ }
210
+ catch (error) {
211
+ if (refusedForSpace(error))
212
+ filled();
213
+ throw error;
214
+ }
215
+ },
216
+ };
199
217
  const readinessWatches = new Set();
200
218
  let readinessQueued = false;
201
219
  const catching = new Set();
202
220
  const transfers = new FencedMap(isFenced);
221
+ const installs = new Map();
222
+ const keptParts = new Map();
223
+ const arrivals = new Map();
224
+ const underived = new Map();
225
+ const unindexed = new Map();
226
+ let arrivalReported = 0;
227
+ let onScreen = options.shell.onScreen;
228
+ const shownWaiters = [];
229
+ let partsFree = PARTS_HELD;
230
+ let queuedBytes = 0;
231
+ const partWaiters = [];
232
+ const stageBudget = {
233
+ bytes: STAGE_FLOOR * 4,
234
+ floor: STAGE_FLOOR,
235
+ ceiling: STAGE_BYTES,
236
+ aim: STAGE_AIM_MS,
237
+ };
238
+ const deriveBudget = {
239
+ bytes: DERIVE_FLOOR * 4,
240
+ floor: DERIVE_FLOOR,
241
+ ceiling: DERIVE_BYTES,
242
+ aim: DERIVE_AIM_MS,
243
+ };
244
+ let installWrites = Promise.resolve();
203
245
  const conversations = createConversationStore({
204
246
  agents: declaredAgents,
205
- documents: ({ locator }) => rowsQuery(planOf({ rooms: [locator], select: { fields: 'all' } }, roomFacts())),
247
+ documents: (declaration, after) => rowsQuery(planOf({
248
+ rooms: [declaration.locator],
249
+ types: [declaration.type],
250
+ select: {
251
+ fields: [
252
+ declaration.identity,
253
+ declaration.name,
254
+ ...(declaration.retired === undefined ? [] : [declaration.retired]),
255
+ ].map((field) => `/${escapePointer(field)}`),
256
+ },
257
+ limit: MAXIMUM_WINDOW,
258
+ ...(after === undefined ? {} : { after }),
259
+ }, roomFacts())),
206
260
  call: (name, input) => invokeAction(name, input),
207
261
  durably,
208
262
  stream: () => options.adapter.conversations,
209
263
  publish: publishChangeSet,
210
264
  online: () => connectionOf() !== 'unreachable' && refusal === null,
265
+ listed: () => roomsListed,
266
+ full: () => full,
211
267
  closed: () => closed,
212
268
  unanswered: resendLater,
213
269
  expired: (operation) => pastHorizon(operation),
214
270
  });
215
- const searchIndex = createSearchIndex({
216
- room: (room) => readSide.room(room),
217
- document: (room, path) => readSide.document(room, path),
218
- });
219
- const roomsToIndex = new Set();
220
- const conversationsToIndex = new Set();
221
- let indexing = false;
222
271
  const journal = createLog();
223
- let notifyReady = () => { };
272
+ const keptFiles = openedFiles(database, isFenced);
273
+ let readyFromStore = () => { };
224
274
  const ready = new Promise((resolve) => {
225
- notifyReady = resolve;
275
+ readyFromStore = resolve;
276
+ });
277
+ let notifyDiscovered = () => { };
278
+ const firstDiscovery = new Promise((resolve) => {
279
+ notifyDiscovered = resolve;
226
280
  });
281
+ let roomsNamed = false;
282
+ function notifyReady() {
283
+ notifyDiscovered();
284
+ readyFromStore();
285
+ }
227
286
  function rememberRoomDraft(record) {
228
287
  const room = roomDrafts.get(record.locator) ?? new Map();
229
288
  room.set(record.path, record);
@@ -237,16 +296,36 @@ export function composeRuntime(options) {
237
296
  if (room.size === 0)
238
297
  roomDrafts.delete(locator);
239
298
  }
240
- function roomDraftOf(record) {
299
+ async function visibleOf(locator, paths) {
300
+ const held = replicas.get(locator);
301
+ if (held === undefined)
302
+ return undefined;
303
+ const local = await openLocal();
304
+ const slice = await stepped(slicing(await local.entries(held.record.key, 'visible', paths), paths));
305
+ return { ...slice, collection: collectionOf(slice.documents, composed(held.record)) };
306
+ }
307
+ function roomDraftOf(record, visible) {
241
308
  return structuredClone({
242
309
  room: record.locator,
243
310
  path: record.path,
244
311
  revision: record.revision,
245
312
  parts: record.parts,
246
- changedUnderneath: underneath(record.parts, record.path, replicas.get(record.locator)?.visible),
313
+ changedUnderneath: underneath(record.parts, record.path, visible),
247
314
  returned: record.returned,
248
315
  });
249
316
  }
317
+ async function roomDraftsOf(records) {
318
+ const rooms = new Map();
319
+ for (const record of records) {
320
+ const paths = rooms.get(record.locator) ?? [];
321
+ paths.push(record.path);
322
+ rooms.set(record.locator, paths);
323
+ }
324
+ const layers = new Map();
325
+ for (const [locator, paths] of rooms)
326
+ layers.set(locator, await visibleOf(locator, paths));
327
+ return records.map((record) => roomDraftOf(record, layers.get(record.locator)));
328
+ }
250
329
  function snapshotRoomDrafts() {
251
330
  const records = [...roomDrafts.values()].flatMap((room) => [...room.values()]);
252
331
  records.sort((left, right) => {
@@ -256,31 +335,61 @@ export function composeRuntime(options) {
256
335
  return left.path < right.path ? -1 : 1;
257
336
  return 0;
258
337
  });
259
- return records.map(roomDraftOf);
338
+ return roomDraftsOf(records);
260
339
  }
340
+ let draftsShown = Promise.resolve();
261
341
  function publishRoomDrafts(waitingOnly) {
262
342
  if (closed)
263
343
  return;
264
- const value = snapshotRoomDrafts();
265
- for (const listener of roomDraftListeners) {
266
- if (waitingOnly && listener.ready)
267
- continue;
268
- listener.ready = true;
269
- listener.notify(value);
270
- }
344
+ const snapshot = snapshotRoomDrafts();
345
+ draftsShown = draftsShown.then(async () => {
346
+ const value = await snapshot.catch(() => null);
347
+ if (value === null || closed)
348
+ return;
349
+ for (const listener of roomDraftListeners) {
350
+ if (waitingOnly && listener.ready)
351
+ continue;
352
+ listener.ready = true;
353
+ listener.notify(value);
354
+ }
355
+ });
271
356
  }
272
357
  function presentationOf(app) {
273
358
  const value = presentations.get(app);
274
359
  return value === undefined ? null : structuredClone(value);
275
360
  }
276
- function keepPresentation(app, next) {
277
- return exclusive(async () => {
278
- const local = await openLocal();
279
- const stored = next(presentations.get(app));
280
- await local.present({ app, value: stored });
281
- presentations.set(app, stored);
282
- publishChangeSet(changeSet('', null, null, { presentation: app }));
283
- });
361
+ async function writtenRoomDraft(room, path, revision, input, next) {
362
+ const parts = readParts(input);
363
+ if (path === '' || normalize(path) !== path)
364
+ throw runtimeError('room-draft-malformed');
365
+ const local = await openLocal();
366
+ const current = roomDrafts.get(room)?.get(path);
367
+ if ((current?.revision ?? null) !== revision) {
368
+ return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
369
+ }
370
+ const file = parts.file === undefined
371
+ ? undefined
372
+ : await heldFile(parts.file.blob, binaryBase(await visibleOf(room, [path]), path));
373
+ const record = {
374
+ locator: room,
375
+ path,
376
+ revision: next,
377
+ parts: held(current?.parts, parts, file),
378
+ returned: current?.returned ?? null,
379
+ };
380
+ await local.keepRoomDraft(record, parts.file === undefined || file === undefined
381
+ ? []
382
+ : [{ key: file.localId, blob: parts.file.blob }]);
383
+ rememberRoomDraft(record);
384
+ publishChangeSet(changeSet(room, { drafts: [path] }));
385
+ return { outcome: 'written', roomDraft: (await roomDraftsOf([record]))[0] ?? null };
386
+ }
387
+ async function keptPresentation(app, next) {
388
+ const local = await openLocal();
389
+ const stored = next(presentations.get(app));
390
+ await local.present({ app, value: stored });
391
+ presentations.set(app, stored);
392
+ publishChangeSet(changeSet('', { presentation: app }));
284
393
  }
285
394
  function emitPresentation(app) {
286
395
  if (closed)
@@ -295,6 +404,20 @@ export function composeRuntime(options) {
295
404
  (queuesWithoutReplica.size > 0 ||
296
405
  [...replicas.values()].some((replica) => replica.queue.length > 0)));
297
406
  }
407
+ function unsent() {
408
+ const changed = new Map();
409
+ for (const room of [...replicas.keys(), ...queuesWithoutReplica.keys()]) {
410
+ for (const { batch, state } of entriesOf(room)) {
411
+ if (state === 'accepted' || state === 'local')
412
+ continue;
413
+ for (const { path } of batch.changes)
414
+ changed.set(`${room}\u0000${path}`, { room, path });
415
+ }
416
+ }
417
+ return [...changed]
418
+ .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
419
+ .map(([, change]) => change);
420
+ }
298
421
  function attention() {
299
422
  const attentionRequired = [];
300
423
  for (const record of [...roomDrafts.values()].flatMap((room) => [...room.values()])) {
@@ -327,24 +450,72 @@ export function composeRuntime(options) {
327
450
  if (reset === null || submission.reset < reset)
328
451
  reset = submission.reset;
329
452
  }
330
- return { attentionRequired, reset };
453
+ return { attentionRequired, unsent: unsent(), reset };
331
454
  }
332
455
  function documentsIn(locator) {
333
- const replica = replicas.get(locator);
334
- if (replica === undefined)
335
- return 0;
336
- const roots = replica.confirmed.record.typedSelection?.roots ?? [];
337
- const weighed = outsideOf(roots);
338
- return (tally(replica.visible.documents, weighed.documents) +
339
- tally(replica.visible.binaries, weighed.binaries));
456
+ return replicas.get(locator)?.count ?? 0;
340
457
  }
341
458
  function listed() {
342
459
  return structuredClone([...rooms.values()].map((room) => {
343
460
  const documents = documentsIn(room.locator);
344
461
  counted.set(room.locator, documents);
345
- return { ...room, documents };
462
+ return locked(room.locator)
463
+ ? {
464
+ ...room,
465
+ model: modelTypes(NO_MODEL),
466
+ typedSelection: null,
467
+ documents: 0,
468
+ locked: true,
469
+ }
470
+ : { ...room, documents, locked: false };
346
471
  }));
347
472
  }
473
+ function locked(locator) {
474
+ return rooms.get(locator)?.replica.offline === 'locked' && !confirmed.has(locator);
475
+ }
476
+ function lockedRooms() {
477
+ return [...rooms.keys()].filter(locked).sort();
478
+ }
479
+ function relocked(before) {
480
+ const after = lockedRooms();
481
+ if (JSON.stringify(after) === JSON.stringify(before))
482
+ return;
483
+ journal.record(`locked ${after.length}`);
484
+ emitRooms();
485
+ for (const notify of [...lockListeners])
486
+ notify();
487
+ }
488
+ function confirmLocks(listed) {
489
+ if (!socketOpen)
490
+ return;
491
+ for (const room of listed) {
492
+ if (room.replica.admission === 'admitted')
493
+ confirmed.add(room.locator);
494
+ }
495
+ }
496
+ function connectionLost() {
497
+ if (lockGrace !== null || closed)
498
+ return;
499
+ lockGrace = setTimeout(() => {
500
+ lockGrace = null;
501
+ if (socketOpen || closed)
502
+ return;
503
+ const before = lockedRooms();
504
+ confirmed.clear();
505
+ relocked(before);
506
+ }, LOCK_GRACE_MS);
507
+ }
508
+ function connectionLive() {
509
+ if (lockGrace !== null)
510
+ clearTimeout(lockGrace);
511
+ lockGrace = null;
512
+ }
513
+ function watchLocks(notify) {
514
+ lockListeners.add(notify);
515
+ return () => {
516
+ lockListeners.delete(notify);
517
+ };
518
+ }
348
519
  function roomFacts(reach = null) {
349
520
  const held = [...rooms.values()]
350
521
  .filter((room) => holds(reach, room.locator))
@@ -352,7 +523,7 @@ export function composeRuntime(options) {
352
523
  const governed = governs(reach, room);
353
524
  const attached = [
354
525
  ...(governed.collection ? [modelOf(room.locator)] : []),
355
- ...(governed.selection ? [modelOf(room.locator, 'typed-selection')] : []),
526
+ ...(governed.selection ? [builtIn] : []),
356
527
  ];
357
528
  return {
358
529
  locator: room.locator,
@@ -363,16 +534,31 @@ export function composeRuntime(options) {
363
534
  return { listed: roomsListed, rooms: held };
364
535
  }
365
536
  function emitRooms() {
366
- readSide.recover();
537
+ liveQueries.recover();
367
538
  queueReadiness();
368
539
  const value = listed();
369
540
  for (const listener of roomListeners)
370
541
  listener(value);
542
+ emitKnownRooms();
543
+ }
544
+ function emitKnownRooms() {
545
+ conversations.learned();
546
+ if (unlistedRooms.size === 0)
547
+ return;
548
+ if (!roomsListed && rooms.size === 0 && refusal === null && !status.providerUnreachable)
549
+ return;
550
+ const value = listed();
551
+ for (const listener of [...unlistedRooms]) {
552
+ unlistedRooms.delete(listener);
553
+ roomListeners.add(listener);
554
+ listener(value);
555
+ }
371
556
  }
372
557
  function emitRefusal() {
373
558
  queueReadiness();
374
559
  for (const listener of refusalListeners)
375
560
  listener(refusal);
561
+ emitKnownRooms();
376
562
  }
377
563
  function emitStorageLoss() {
378
564
  queueReadiness();
@@ -393,6 +579,7 @@ export function composeRuntime(options) {
393
579
  if (closed)
394
580
  return;
395
581
  queueReadiness();
582
+ emitKnownRooms();
396
583
  const next = status.state === 'synced' && !conversations.current()
397
584
  ? { ...status, state: 'syncing' }
398
585
  : status;
@@ -402,17 +589,17 @@ export function composeRuntime(options) {
402
589
  for (const listener of statusListeners)
403
590
  listener(shown);
404
591
  }
405
- function entryOf(batch) {
592
+ function entryOf(locator, batch) {
406
593
  if (localSaves)
407
594
  return Object.freeze({ batch, state: 'local' });
408
- const state = pendings.get(batch.id);
595
+ const state = pendings.get(pendingKey(locator, batch.id));
409
596
  if (state?.accepted !== undefined) {
410
597
  return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
411
598
  }
412
599
  return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
413
600
  }
414
601
  function entriesOf(locator) {
415
- return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map(entryOf);
602
+ return (replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? []).map((batch) => entryOf(locator, batch));
416
603
  }
417
604
  function emitQueue(locator) {
418
605
  const entries = entriesOf(locator);
@@ -422,66 +609,42 @@ export function composeRuntime(options) {
422
609
  }
423
610
  }
424
611
  function applyChangeSet(set) {
425
- const held = {
426
- ...set,
427
- documents: set.documents.filter(({ locator }) => prepared.has(locator) || projecting.has(locator)),
428
- };
429
- const applied = readSide.apply(held, () => searchIndex.changed(held));
430
- if (applied instanceof Promise)
431
- void applied.catch(unpublished);
612
+ liveQueries.touched(set);
432
613
  }
433
614
  function unpublished(error) {
434
615
  journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
435
616
  }
436
- async function referenced(locator) {
437
- for (;;) {
438
- const record = replicas.get(locator)?.confirmed.record;
439
- if (record === undefined || closed)
440
- return;
441
- const held = { text: record.documents, binaries: record.binaries };
442
- const composition = compositionOf(record);
443
- if (referencesIndexed(held, composition))
444
- return;
445
- await indexReferrers(held, composition);
446
- }
617
+ function indexed(locator) {
618
+ return replicas.get(locator)?.indexed === true;
447
619
  }
448
620
  async function prepareReplica(locator) {
449
- await referenced(locator);
450
- const replica = replicas.get(locator);
451
- if (replica === undefined || closed || prepared.has(locator))
452
- return;
453
- const visible = replica.visible;
454
- const paths = [
455
- ...new Set([...Object.keys(visible.documents), ...Object.keys(visible.binaries)]),
456
- ];
457
- const entered = [];
458
- projecting.add(locator);
459
- try {
460
- await inChunks(paths, (path) => ROW_WEIGHT + (visible.documents[path]?.length ?? 0), (chunk) => {
461
- const current = replicas.get(locator);
462
- if (current === undefined || closed)
463
- return;
464
- const set = entering(locator, current, chunk);
465
- entered.push(set);
466
- return readSide.apply(set);
467
- });
468
- if (!replicas.has(locator) || closed)
621
+ for (;;) {
622
+ const held = replicas.get(locator);
623
+ if (held === undefined || closed || held.indexed)
469
624
  return;
470
- prepared.add(locator);
471
- }
472
- finally {
473
- projecting.delete(locator);
625
+ const key = held.record.key;
626
+ const done = await exclusive(async () => {
627
+ const local = await openLocal();
628
+ return timed(deriveBudget, () => local.rederive(key, composed(held.record), deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR));
629
+ });
630
+ const now = replicas.get(locator);
631
+ if (now?.record.key !== key)
632
+ continue;
633
+ if (!done.done)
634
+ continue;
635
+ replicas.set(locator, { ...now, indexed: true });
636
+ publishChangeSet(changeSet(locator, { documents: null }));
637
+ liveQueries.recover();
638
+ queueReadiness();
639
+ return;
474
640
  }
475
- conversations.changed(mergeChangeSets(entered));
476
- readSide.recover();
477
- queueReadiness();
478
641
  }
479
642
  function unprepared() {
480
- return [...unpreparedDemand, ...replicas.keys()].find((locator) => !prepared.has(locator) && !unindexable.has(locator));
643
+ return [...unpreparedDemand, ...replicas.keys()].find((locator) => !indexed(locator) && !unpreparable.has(locator));
481
644
  }
482
- function unbuilt(locator, error) {
483
- unindexable.add(locator);
484
- journal.record(`index failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
645
+ function unpreparedBy(locator, error) {
646
+ unpreparable.add(locator);
647
+ journal.record(`preparation failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
485
648
  queueReadiness();
486
649
  }
487
650
  function prepare(locators = []) {
@@ -497,7 +660,7 @@ export function composeRuntime(options) {
497
660
  if (closed)
498
661
  return;
499
662
  unpreparedDemand.delete(waiting);
500
- await prepareReplica(waiting).catch((error) => unbuilt(waiting, error));
663
+ await prepareReplica(waiting).catch((error) => unpreparedBy(waiting, error));
501
664
  }
502
665
  }
503
666
  finally {
@@ -506,7 +669,11 @@ export function composeRuntime(options) {
506
669
  })();
507
670
  }
508
671
  function roomsOfReach(reach) {
509
- return new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
672
+ return new Set([
673
+ ...reach.rooms,
674
+ ...reach.documents.map(roomOfKey),
675
+ ...reach.areas.flatMap((area) => (area.types.length === 0 ? [] : [area.room])),
676
+ ]);
510
677
  }
511
678
  function reachedRooms(plan) {
512
679
  const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
@@ -514,7 +681,9 @@ export function composeRuntime(options) {
514
681
  (reached === null
515
682
  ? [...new Set([...rooms.keys(), ...replicas.keys(), ...queuesWithoutReplica.keys()])]
516
683
  : [...reached]);
517
- return [...new Set(named)].filter((locator) => reached === null || reached.has(locator)).sort();
684
+ return [...new Set(named)]
685
+ .filter((locator) => (reached === null || reached.has(locator)) && !plan.locked.includes(locator))
686
+ .sort();
518
687
  }
519
688
  function awaited(locator) {
520
689
  return (!roomsListed ||
@@ -528,6 +697,7 @@ export function composeRuntime(options) {
528
697
  const pending = [];
529
698
  const coverage = [];
530
699
  const overlay = [];
700
+ const read = [];
531
701
  for (const locator of locators) {
532
702
  const replica = replicas.get(locator);
533
703
  if (replica === undefined) {
@@ -535,21 +705,22 @@ export function composeRuntime(options) {
535
705
  pending.push({ room: locator, awaits: 'acquisition' });
536
706
  continue;
537
707
  }
538
- if (!prepared.has(locator)) {
708
+ if (!replica.indexed) {
539
709
  pending.push({ room: locator, awaits: 'preparation' });
540
710
  continue;
541
711
  }
542
712
  coverage.push({
543
713
  room: locator,
544
- generation: replica.confirmed.record.transfer,
714
+ generation: replica.record.transfer,
545
715
  current: !(catching.has(locator) || owed.has(locator) || transfers.has(locator)),
546
716
  });
717
+ read.push({ locator, key: replica.record.key });
547
718
  for (const batch of replica.queue)
548
719
  overlay.push(batch.id);
549
720
  }
550
721
  if (pending.length > 0)
551
722
  return { pending };
552
- return { coverage: { rooms: coverage, indexes: [], overlay } };
723
+ return { coverage: { rooms: coverage, indexes: [], overlay }, rooms: read };
553
724
  }
554
725
  function awaitPreparation(cover) {
555
726
  if (!('pending' in cover))
@@ -567,7 +738,7 @@ export function composeRuntime(options) {
567
738
  return { listed: true, rooms: named.filter((room) => rooms.has(room)) };
568
739
  return { listed: named.every((room) => replicas.has(room)), rooms: named };
569
740
  }
570
- function roomState(locator, search) {
741
+ function roomState(locator) {
571
742
  if (sourceRefusals.has(locator))
572
743
  return 'refused';
573
744
  const listedRoom = discovered.get(locator);
@@ -578,27 +749,17 @@ export function composeRuntime(options) {
578
749
  return 'unadmitted';
579
750
  if (!replicas.has(locator))
580
751
  return 'acquiring';
581
- if (!prepared.has(locator))
752
+ if (!indexed(locator))
582
753
  return 'preparing';
583
- if (search && !searchIndex.rooms().has(locator))
584
- return 'indexing';
585
754
  return 'complete';
586
755
  }
587
- function conversationCoverage(reach, search) {
756
+ function conversationCoverage(reach) {
588
757
  const declared = reach === null || reach.conversations;
589
758
  if (!declared || options.adapter.conversations === undefined || declaredAgents() === undefined)
590
759
  return 'undeclared';
591
760
  if (!conversations.known() || !conversations.current())
592
761
  return 'acquiring';
593
- if (!search)
594
- return 'complete';
595
- const unindexed = conversations
596
- .searchable()
597
- .filter((one) => conversations.acquired(one) && !searchIndex.conversations().has(one));
598
- if (unindexed.length === 0)
599
- return 'complete';
600
- index([], unindexed);
601
- return 'indexing';
762
+ return 'complete';
602
763
  }
603
764
  function connectionOf() {
604
765
  if (socketOpen)
@@ -618,6 +779,7 @@ export function composeRuntime(options) {
618
779
  return [
619
780
  {
620
781
  room,
782
+ name: discovered.get(room)?.name ?? room,
621
783
  file: typeof file === 'string' ? file : '',
622
784
  rule: held.code,
623
785
  location: typeof location === 'string' ? location : null,
@@ -625,43 +787,97 @@ export function composeRuntime(options) {
625
787
  ];
626
788
  });
627
789
  }
790
+ function installationOf(coverage) {
791
+ const rooms = { done: 0, total: 0 };
792
+ const parts = { done: 0, total: 0 };
793
+ const bytes = { done: 0, total: 0 };
794
+ const received = { done: 0, total: 0 };
795
+ let installing = 0;
796
+ let unlisted = false;
797
+ let listed = false;
798
+ let waiting = false;
799
+ for (const { room, state } of coverage) {
800
+ if (state === 'unadmitted')
801
+ continue;
802
+ rooms.total += 1;
803
+ if (state === 'complete')
804
+ rooms.done += 1;
805
+ const transfer = transfers.get(room);
806
+ const replica = replicas.get(room)?.record.inventory ?? null;
807
+ const inventory = transfer?.inventory ?? replica ?? installs.get(room) ?? null;
808
+ const underway = state === 'acquiring' || (installs.has(room) && state !== 'complete');
809
+ if (underway)
810
+ installing += 1;
811
+ if (underway && inventory === null)
812
+ unlisted = true;
813
+ if (inventory !== null) {
814
+ listed = true;
815
+ const done = transfer?.done ?? (replica === null ? NOTHING_DONE : inventory);
816
+ const owed = (unindexed.get(room) ?? []).reduce((sum, part) => sum + part.bytes, 0);
817
+ parts.total += inventory.parts;
818
+ bytes.total += inventory.bytes;
819
+ parts.done += done.parts;
820
+ bytes.done += done.bytes - Math.ceil(owed);
821
+ received.total += inventory.bytes;
822
+ received.done += done.bytes;
823
+ for (const held of [keptParts.get(room), arrivals.get(room)]) {
824
+ for (const arrived of held?.values() ?? [])
825
+ received.done += arrived;
826
+ }
827
+ }
828
+ waiting ||= underway;
829
+ }
830
+ const step = installing === 0
831
+ ? null
832
+ : !listed
833
+ ? 'listing'
834
+ : unlisted || received.done < received.total
835
+ ? 'downloading'
836
+ : bytes.done < bytes.total
837
+ ? 'writing'
838
+ : 'replaying';
839
+ return {
840
+ step,
841
+ paused: waiting && !onScreen ? 'hidden' : null,
842
+ rooms,
843
+ parts,
844
+ bytes,
845
+ received,
846
+ };
847
+ }
628
848
  function readinessFor(port, reach) {
629
849
  const required = requiredRooms(reach);
630
850
  const search = port.search === 'required';
631
- const coverage = required.rooms.map((room) => ({ room, state: roomState(room, search) }));
632
- const building = (state) => coverage
633
- .filter((room) => room.state === state && !unindexable.has(room.room))
851
+ const coverage = required.rooms.map((room) => ({ room, state: roomState(room) }));
852
+ const preparing = coverage
853
+ .filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
634
854
  .map(({ room }) => room);
635
- const preparing = building('preparing');
636
855
  if (preparing.length > 0)
637
856
  prepare(preparing);
638
- const indexing = building('indexing');
639
- if (indexing.length > 0)
640
- index(indexing, []);
641
857
  return readinessOf({
642
858
  code: port.code,
643
859
  coverage: {
644
860
  listed: required.listed,
645
861
  rooms: coverage,
646
- conversations: conversationCoverage(reach, search),
862
+ conversations: conversationCoverage(reach),
647
863
  search: search ? 'required' : 'unused',
648
864
  },
649
865
  persistence,
650
866
  connection: connectionOf(),
651
867
  house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
652
- capacity: capacity !== null &&
653
- coverage.some(({ room, state }) => paused.has(room) || !SETTLED_ROOMS.has(state))
654
- ? capacity
655
- : port.capacity,
868
+ space: full ? 'full' : port.space,
869
+ installation: installationOf(coverage),
656
870
  refused: refusedIn(required.rooms),
657
871
  lost: storageLoss !== null,
658
- established: targets[port.target]?.established === true,
872
+ established: targets[port.target]?.established === true ||
873
+ (usableWhenHeld.includes(port.target) && required.rooms.some((room) => reopened.has(room))),
659
874
  });
660
875
  }
661
876
  function establish(name) {
662
877
  targets = { ...targets, [name]: { established: true } };
663
878
  queueReadiness();
664
- void durably((local) => local.keepTargets(targets)).catch(() => {
879
+ probeEachMinute();
880
+ void durably((local) => local.fact('targets', targets)).catch(() => {
665
881
  journal.record(`target record failed ${name}`);
666
882
  });
667
883
  }
@@ -698,92 +914,52 @@ export function composeRuntime(options) {
698
914
  readinessWatches.delete(watch);
699
915
  };
700
916
  }
701
- function layerBytes(layer) {
702
- let bytes = 0;
703
- for (const [path, text] of Object.entries(layer.documents))
704
- bytes += (path.length + text.length) * 2;
705
- return bytes;
706
- }
707
- function grownBy(next, previous) {
708
- if (previous === undefined)
709
- return layerBytes(next);
710
- const documents = Object.fromEntries(Object.entries(next.documents).filter(([path, text]) => previous.documents[path] !== text));
711
- return layerBytes({ ...next, documents });
712
- }
713
- function exceeded(error) {
714
- return (typeof error === 'object' &&
715
- error !== null &&
716
- error.name === 'QuotaExceededError');
717
- }
718
- async function estimated() {
719
- const estimate = options.shell.estimate === undefined ? null : await options.shell.estimate();
720
- return estimate === null ? null : Math.max(0, estimate.quota - estimate.usage);
721
- }
722
- function pause(locator, cause) {
723
- paused.add(locator);
724
- capacity = cause;
725
- journal.record(`capacity ${cause.cause} ${locator} ${cause.needed} ${cause.available ?? '-'}`);
917
+ function filled() {
918
+ if (full || closed)
919
+ return;
920
+ full = true;
921
+ journal.record('space full');
726
922
  queueReadiness();
727
- if (rechecking !== null)
923
+ probeEachMinute();
924
+ }
925
+ function installedTarget() {
926
+ return ((usableWhenHeld.length > 0 && reopened.size > 0) ||
927
+ Object.values(targets).some((target) => target.established));
928
+ }
929
+ function probeEachMinute() {
930
+ if (!full || probing !== null || closed || !installedTarget())
728
931
  return;
729
- // A paused preparation measures the browser's space again each minute, so space freed elsewhere resumes it without a retry.
730
- rechecking = setInterval(() => {
731
- void recheck();
932
+ // A device whose storage filled after its install tries a write each minute, so space freed elsewhere ends its read-only mode without a Retry.
933
+ probing = setInterval(() => {
934
+ void probed();
732
935
  }, 60_000);
733
936
  }
734
- async function recheck() {
735
- const held = capacity;
736
- if (held === null || closed)
737
- return;
738
- const available = await estimated().catch(() => null);
739
- if (available === null || available < (held.needed ?? 0))
740
- return;
741
- if (held.cause === 'quota-exceeded' && (held.available === null || available <= held.available))
742
- return;
743
- resume();
937
+ function probed() {
938
+ testing ??= database.ask({ kind: 'space.probe' }).then(() => {
939
+ testing = null;
940
+ spaceFreed();
941
+ return true;
942
+ }, () => {
943
+ testing = null;
944
+ return false;
945
+ });
946
+ return testing;
744
947
  }
745
- function resume() {
746
- if (rechecking !== null)
747
- clearInterval(rechecking);
748
- rechecking = null;
749
- if (paused.size === 0 && capacity === null)
948
+ function spaceFreed() {
949
+ if (!full)
750
950
  return;
751
- paused.clear();
752
- capacity = null;
753
- journal.record('capacity resumed');
951
+ full = false;
952
+ if (probing !== null)
953
+ clearInterval(probing);
954
+ probing = null;
955
+ unpreparable.clear();
956
+ journal.record('space freed');
754
957
  queueReadiness();
755
958
  if (!closed && authorization !== undefined)
756
959
  void sync('unstreamed');
757
960
  }
758
- async function reclaimed(local) {
759
- const unreferenced = [...transfers.keys()].filter((locator) => !discovered.has(locator));
760
- for (const locator of unreferenced) {
761
- await local.abandon(locator);
762
- transfers.delete(locator);
763
- }
764
- if (unreferenced.length > 0)
765
- journal.record(`capacity reclaimed ${unreferenced.length}`);
766
- return unreferenced.length > 0;
767
- }
768
- async function admitted(local, locator, needed) {
769
- if (paused.has(locator))
770
- return false;
771
- let available = await estimated().catch(() => null);
772
- if (available === null || available >= needed)
773
- return true;
774
- if (await reclaimed(local))
775
- available = await estimated().catch(() => null);
776
- if (available === null || available >= needed)
777
- return true;
778
- pause(locator, { cause: 'insufficient-space', needed, available });
779
- return false;
780
- }
781
- async function exceededOn(locator, bytes) {
782
- const available = await estimated().catch(() => null);
783
- pause(locator, { cause: 'quota-exceeded', needed: bytes, available });
784
- }
785
961
  function seedRoomHeld(room, version) {
786
- return prepared.has(room) && reachedBy(room, version);
962
+ return indexed(room) && reachedBy(room, version);
787
963
  }
788
964
  function seedHeld(at) {
789
965
  return Object.entries(at).every(([room, version]) => (roomsListed && !rooms.has(room)) || seedRoomHeld(room, version));
@@ -817,6 +993,75 @@ export function composeRuntime(options) {
817
993
  shown();
818
994
  }
819
995
  }
996
+ function sameCover(left, right) {
997
+ return JSON.stringify(left) === JSON.stringify(right);
998
+ }
999
+ async function coveredRows(plan, cover) {
1000
+ for (;;) {
1001
+ const before = cover();
1002
+ if ('pending' in before)
1003
+ return { pending: before };
1004
+ const local = await openLocal();
1005
+ const keys = before.rooms.map(({ key }) => key);
1006
+ const changes = local.changes(keys);
1007
+ const current = () => local.changes(keys) === changes;
1008
+ const rows = await windowRows(local, plan, before.rooms, current);
1009
+ const after = cover();
1010
+ if (rows !== null && sameCover(before, after))
1011
+ return { rows, cover: before, current };
1012
+ }
1013
+ }
1014
+ async function windowRows(local, plan, rooms, unchanged) {
1015
+ const first = await local.query(plan, rooms, QUERY_BYTES);
1016
+ if (first.more === undefined || !('documents' in first)) {
1017
+ const { more: _more, ...rows } = first;
1018
+ return rows;
1019
+ }
1020
+ const documents = [...first.documents];
1021
+ for (let more = first.more; more !== undefined;) {
1022
+ if (!unchanged())
1023
+ return null;
1024
+ const page = await local.query({
1025
+ ...plan,
1026
+ after: { text: more, values: JSON.parse(more) },
1027
+ limit: plan.limit === null ? null : plan.limit - documents.length,
1028
+ total: false,
1029
+ }, rooms, QUERY_BYTES);
1030
+ if (!('documents' in page))
1031
+ return null;
1032
+ documents.push(...page.documents);
1033
+ more = page.more;
1034
+ if (more === undefined) {
1035
+ return {
1036
+ documents,
1037
+ ...(page.next === undefined ? {} : { next: page.next }),
1038
+ ...(first.total === undefined ? {} : { total: first.total }),
1039
+ };
1040
+ }
1041
+ }
1042
+ return null;
1043
+ }
1044
+ function keyedOf(rows, coverage) {
1045
+ if ('groups' in rows) {
1046
+ return {
1047
+ result: { ...rows, coverage },
1048
+ keys: rows.groups.map((group) => JSON.stringify(group.key)),
1049
+ };
1050
+ }
1051
+ return {
1052
+ result: { ...rows, coverage },
1053
+ keys: rows.documents.map((document) => documentKey(document.room, document.path)),
1054
+ };
1055
+ }
1056
+ async function answeredPlan(plan, cover) {
1057
+ const answered = await coveredRows(plan, cover);
1058
+ if ('pending' in answered) {
1059
+ const pending = answered.pending;
1060
+ return { result: pending, keys: [], current: () => true };
1061
+ }
1062
+ const held = answered.cover;
1063
+ return { ...keyedOf(answered.rows, held.coverage), current: answered.current };
1064
+ }
820
1065
  async function rowsQuery(plan) {
821
1066
  const before = covered(plan);
822
1067
  awaitPreparation(before);
@@ -825,69 +1070,32 @@ export function composeRuntime(options) {
825
1070
  return seededResult(seeded).result;
826
1071
  if ('pending' in before)
827
1072
  return before;
828
- const { rows, at: cover } = await readSide.query(plan, () => covered(plan));
829
- awaitPreparation(cover);
830
- if ('pending' in cover)
1073
+ const answered = await coveredRows(plan, () => {
1074
+ const cover = covered(plan);
1075
+ awaitPreparation(cover);
831
1076
  return cover;
832
- return { ...rows, coverage: cover.coverage };
833
- }
834
- async function indexConversation(conversation) {
835
- searchIndex.indexMessages(conversation, [], false);
836
- let after = null;
837
- for (;;) {
838
- const batch = await durably((local) => local.messagesAfter(conversation, after));
839
- if (closed)
840
- return;
841
- searchIndex.indexMessages(conversation, batch.map(({ identity, text }) => ({ conversation, identity, text })), batch.length === 0);
842
- if (batch.length === 0)
843
- return;
844
- after = batch[batch.length - 1].identity;
845
- }
846
- }
847
- function indexNext() {
848
- const room = [...roomsToIndex].find((locator) => !searchIndex.rooms().has(locator) && !unindexable.has(locator));
849
- if (room !== undefined) {
850
- roomsToIndex.delete(room);
851
- return () => prepared.has(room)
852
- ? searchIndex.indexRoom(room).catch((error) => unbuilt(room, error))
853
- : undefined;
854
- }
855
- const conversation = [...conversationsToIndex].find((one) => !searchIndex.conversations().has(one));
856
- if (conversation === undefined)
857
- return null;
858
- conversationsToIndex.delete(conversation);
859
- return () => indexConversation(conversation);
860
- }
861
- function index(rooms, named) {
862
- for (const room of rooms)
863
- roomsToIndex.add(room);
864
- for (const conversation of named)
865
- conversationsToIndex.add(conversation);
866
- if (indexing)
867
- return;
868
- indexing = true;
869
- void (async () => {
870
- try {
871
- for (let unit = indexNext(); unit !== null && !closed; unit = indexNext()) {
872
- await yielded();
873
- await unit();
874
- queueReadiness();
875
- }
876
- }
877
- finally {
878
- indexing = false;
879
- }
880
- })().catch(() => { });
1077
+ });
1078
+ if ('pending' in answered)
1079
+ return answered.pending;
1080
+ const held = answered.cover;
1081
+ return { ...answered.rows, coverage: held.coverage };
881
1082
  }
882
1083
  function publishChangeSet(set) {
883
1084
  applyChangeSet(set);
1085
+ for (const { locator, path } of set.documents) {
1086
+ if (path !== null)
1087
+ attachedFiles.get(locator)?.delete(path);
1088
+ }
884
1089
  if (set.rooms.length > 0 ||
885
1090
  [...new Set(set.documents.map(({ locator }) => locator))].some((locator) => rooms.has(locator) && counted.get(locator) !== documentsIn(locator)))
886
1091
  emitRooms();
887
1092
  for (const locator of set.queues)
888
1093
  emitQueue(locator);
889
1094
  if (set.drafts.length > 0 ||
890
- set.documents.some((change) => roomDrafts.get(change.locator)?.has(change.path) === true)) {
1095
+ set.documents.some((change) => {
1096
+ const held = roomDrafts.get(change.locator);
1097
+ return held !== undefined && (change.path === null || held.has(change.path));
1098
+ })) {
891
1099
  publishRoomDrafts(false);
892
1100
  }
893
1101
  for (const app of set.presentations)
@@ -900,34 +1108,43 @@ export function composeRuntime(options) {
900
1108
  if (reason === 'revoked-authorization') {
901
1109
  await exclusive(async () => {
902
1110
  const revoked = { status: 'unavailable', reason: 'revoked' };
1111
+ const records = [...replicas].map(([locator, held]) => ({
1112
+ locator,
1113
+ held,
1114
+ record: { ...held.record, model: revoked },
1115
+ }));
1116
+ let answer = null;
903
1117
  try {
904
1118
  const local = await openLocal();
905
- await local.revokeModels([...replicas.values()].map((replica) => replica.confirmed.record));
1119
+ answer = await local.commit({
1120
+ rooms: records.map(({ locator, record }) => ({
1121
+ locator,
1122
+ key: record.key,
1123
+ record: storedRecord(record),
1124
+ composed: composedOf(composed(record)),
1125
+ })),
1126
+ });
906
1127
  }
907
1128
  catch {
908
1129
  journal.record('model revocation persistence failed');
909
1130
  }
910
- const keys = new Set(models.keys());
911
- for (const locator of rooms.keys()) {
912
- keys.add(modelKey(locator, 'collection'));
913
- keys.add(modelKey(locator, 'typed-selection'));
914
- }
1131
+ const keys = new Set([...models.keys(), ...rooms.keys()]);
915
1132
  for (const key of keys) {
916
1133
  invalidModels.add(key);
917
1134
  models.set(key, revoked);
918
1135
  liveModels.delete(key);
919
1136
  }
920
1137
  const changed = [];
921
- for (const [locator, room] of rooms) {
922
- const before = replicas.get(locator) ?? null;
923
- if (before !== null) {
924
- const record = {
925
- ...before.confirmed.record,
926
- model: revoked,
927
- selectionModel: revoked,
928
- };
929
- replicas.set(locator, replicaOf(confirmedOf(record, before.confirmed), before.queue, before));
930
- }
1138
+ for (const { locator, held, record } of records) {
1139
+ if (replicas.get(locator) !== held)
1140
+ continue;
1141
+ const indexed = answer === null ? held.indexed : committedRoom(locator, answer).indexed;
1142
+ replicas.set(locator, { ...held, record, indexed });
1143
+ }
1144
+ for (const locator of [...rooms.keys()]) {
1145
+ const room = rooms.get(locator);
1146
+ if (room === undefined)
1147
+ continue;
931
1148
  rooms.set(locator, {
932
1149
  ...room,
933
1150
  model: modelTypes(revoked),
@@ -935,9 +1152,10 @@ export function composeRuntime(options) {
935
1152
  ? null
936
1153
  : { ...room.typedSelection, model: modelTypes(revoked) },
937
1154
  });
938
- changed.push(changeSet(locator, before, replicas.get(locator) ?? null, { room: true }));
1155
+ changed.push(changeSet(locator, { documents: null, room: true }));
939
1156
  }
940
1157
  publishChangeSet(mergeChangeSets(changed));
1158
+ prepare();
941
1159
  });
942
1160
  }
943
1161
  }
@@ -965,15 +1183,16 @@ export function composeRuntime(options) {
965
1183
  ? null
966
1184
  : {
967
1185
  roots: selection.roots,
968
- model: modelTypes(modelOf(room.locator, 'typed-selection')),
1186
+ model: modelTypes(builtIn),
969
1187
  },
970
1188
  providerOwnedPaths: room.providerOwnedPaths,
971
- roomVersion: replicas.get(room.locator)?.confirmed.record.roomVersion ?? null,
1189
+ roomVersion: replicas.get(room.locator)?.record.roomVersion ?? null,
972
1190
  synchronizationAvailable,
973
1191
  refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
974
1192
  });
975
1193
  }
976
1194
  async function applyDiscovery(found, started, covered) {
1195
+ const lockedBefore = lockedRooms();
977
1196
  for (const room of found) {
978
1197
  const fence = fences.get(room.locator);
979
1198
  if (fence !== undefined && fence.erased && fence.at < started)
@@ -999,15 +1218,17 @@ export function composeRuntime(options) {
999
1218
  else
1000
1219
  unlistedKept(unlisted);
1001
1220
  for (const room of listed) {
1002
- const held = replicas.get(room.locator)?.confirmed.record;
1221
+ const held = replicas.get(room.locator)?.record;
1003
1222
  if (held !== undefined && held.replica.selection !== room.replica.selection) {
1004
1223
  list(held, false);
1005
1224
  continue;
1006
1225
  }
1007
1226
  list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
1008
1227
  }
1228
+ confirmLocks(listed);
1009
1229
  roomsListed = true;
1010
1230
  emitRooms();
1231
+ relocked(lockedBefore);
1011
1232
  return listed;
1012
1233
  }
1013
1234
  function heldLocally(locator) {
@@ -1028,7 +1249,7 @@ export function composeRuntime(options) {
1028
1249
  }
1029
1250
  function unlistedKept(unlisted) {
1030
1251
  for (const locator of unlisted) {
1031
- const held = replicas.get(locator)?.confirmed.record;
1252
+ const held = replicas.get(locator)?.record;
1032
1253
  if (held !== undefined)
1033
1254
  list(held, false);
1034
1255
  else
@@ -1037,64 +1258,90 @@ export function composeRuntime(options) {
1037
1258
  }
1038
1259
  function fence(locators) {
1039
1260
  const fresh = [...new Set(locators)].filter((locator) => !fences.has(locator));
1040
- const removed = fresh.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
1041
- room: true,
1042
- queue: true,
1261
+ const removed = fresh.map((locator) => ({
1262
+ locator,
1043
1263
  drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
1044
1264
  }));
1045
1265
  for (const locator of fresh) {
1046
1266
  fences.set(locator, { at: discoveries, erased: false });
1047
1267
  lastFenced.set(locator, ++fencings);
1268
+ confirmed.delete(locator);
1048
1269
  for (const batch of replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [])
1049
- pendings.delete(batch.id);
1270
+ pendings.delete(pendingKey(locator, batch.id));
1050
1271
  replicas.delete(locator);
1051
1272
  queuesWithoutReplica.delete(locator);
1052
1273
  rooms.delete(locator);
1053
1274
  roomDrafts.delete(locator);
1054
- transfers.delete(locator);
1275
+ attachedFiles.delete(locator);
1276
+ untransferred(locator);
1055
1277
  catching.delete(locator);
1056
1278
  discovered.delete(locator);
1057
1279
  announced.delete(locator);
1058
1280
  unreadable.delete(locator);
1059
1281
  stale.delete(locator);
1060
1282
  repairable.delete(locator);
1061
- unindexable.delete(locator);
1062
- paused.delete(locator);
1283
+ unpreparable.delete(locator);
1063
1284
  scopedRefusals.delete(locator);
1064
1285
  sourceRefusals.delete(locator);
1065
1286
  declarationRefusals.delete(locator);
1066
- for (const attached of ['collection', 'typed-selection']) {
1067
- const key = modelKey(locator, attached);
1068
- models.delete(key);
1069
- liveModels.delete(key);
1070
- invalidModels.delete(key);
1071
- }
1287
+ models.delete(locator);
1288
+ liveModels.delete(locator);
1289
+ invalidModels.delete(locator);
1072
1290
  journal.record(`room lost ${locator}`);
1073
1291
  }
1074
1292
  if (fresh.length === 0)
1075
1293
  return fresh;
1076
- publishChangeSet(mergeChangeSets(removed));
1077
- for (const locator of fresh)
1078
- prepared.delete(locator);
1079
- searchIndex.forget(fresh);
1080
- readSide.recover();
1294
+ publishChangeSet(mergeChangeSets(removed.map(({ locator, drafts }) => changeSet(locator, { documents: null, room: true, queue: true, drafts }))));
1295
+ liveQueries.recover();
1081
1296
  moveTo({ ...status, ...attention() });
1082
1297
  return fresh;
1083
1298
  }
1084
- async function erased(local, locators) {
1085
- await local.erase(locators);
1299
+ async function eraseFenced(locators) {
1086
1300
  for (const locator of locators) {
1301
+ while (await durably((local) => local.erase(locator, ERASE_BATCH)))
1302
+ await yielded();
1087
1303
  const held = fences.get(locator);
1088
1304
  if (held !== undefined)
1089
1305
  held.erased = true;
1090
1306
  }
1091
1307
  }
1308
+ let sweeping = false;
1309
+ function sweep() {
1310
+ if (sweeping || closed)
1311
+ return;
1312
+ sweeping = true;
1313
+ void (async () => {
1314
+ try {
1315
+ while (!closed && (await durably((local) => local.erase(null, ERASE_BATCH)))) {
1316
+ await yielded();
1317
+ }
1318
+ }
1319
+ catch {
1320
+ journal.record('erasure failed');
1321
+ }
1322
+ finally {
1323
+ sweeping = false;
1324
+ }
1325
+ })();
1326
+ }
1092
1327
  async function loseRooms(locators) {
1093
1328
  const fresh = fence(locators);
1094
- if (fresh.length > 0)
1095
- await durably((local) => erased(local, fresh));
1329
+ if (fresh.length === 0)
1330
+ return;
1331
+ await durably(async (local) => {
1332
+ await local.fence(fresh);
1333
+ await unpair(local);
1334
+ });
1335
+ await eraseFenced(fresh);
1336
+ }
1337
+ function retyped(persisted) {
1338
+ if (persisted.status === 'unavailable')
1339
+ return persisted;
1340
+ const { state } = attachedModel({ kind: 'model', snapshot: persisted.snapshot }, NO_MODEL, roomTypes);
1341
+ return state.status === 'unavailable' ? state : { ...persisted, types: state.types };
1096
1342
  }
1097
- function restored(key, persisted) {
1343
+ function restored(key, held) {
1344
+ const persisted = retyped(held);
1098
1345
  const prior = models.get(key);
1099
1346
  if (prior !== undefined &&
1100
1347
  prior.status !== 'unavailable' &&
@@ -1113,54 +1360,64 @@ export function composeRuntime(options) {
1113
1360
  return model;
1114
1361
  }
1115
1362
  async function hydrate(opened) {
1116
- const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named, known, staged, heldTargets,] = await Promise.all([
1117
- opened.all(),
1363
+ const [held, queues, kept, heldConversations, chatDrafts] = await Promise.all([
1364
+ opened.opened(),
1118
1365
  opened.queues(),
1119
1366
  opened.roomDrafts(),
1120
- opened.presentation(),
1121
1367
  opened.conversations(),
1122
1368
  opened.chatDrafts(),
1123
- opened.lastCompleted(),
1124
- opened.providerLimits(),
1125
- opened.capabilities(),
1126
- opened.member(),
1127
- opened.transfers(),
1128
- opened.targets(),
1129
1369
  ]);
1130
- targets = heldTargets;
1370
+ const records = [...held.rooms.values()];
1371
+ const counts = await opened.counts(records.map(({ key, typedSelection }) => ({ key, roots: typedSelection?.roots ?? [] })));
1372
+ const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
1373
+ targets = held.targets;
1131
1374
  transfers.clear();
1132
- for (const record of staged)
1133
- transfers.set(record.locator, record);
1134
- limits = declared;
1135
- capabilities = named;
1136
- member = known;
1375
+ for (const [locator, record] of held.transfers) {
1376
+ transfers.set(locator, {
1377
+ ...record,
1378
+ models: { collection: retyped(record.models.collection) },
1379
+ });
1380
+ }
1381
+ arrivals.clear();
1382
+ keptParts.clear();
1383
+ for (const [locator, parts] of held.kept)
1384
+ keptParts.set(locator, new Map(parts));
1385
+ limits = held.providerLimits;
1386
+ capabilities = held.capabilities;
1387
+ member = held.member;
1137
1388
  roomDrafts.clear();
1138
1389
  for (const record of kept)
1139
1390
  rememberRoomDraft(record);
1140
1391
  presentations.clear();
1141
- for (const record of presented)
1392
+ for (const record of held.presentation)
1142
1393
  presentations.set(record.app, record.value);
1143
- conversations.hydrate(held, chatDrafts);
1144
- const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
1394
+ conversations.hydrate(heldConversations, chatDrafts);
1145
1395
  pendings.clear();
1146
1396
  for (const queue of queues) {
1147
- for (const { batch, state } of queue.pending)
1148
- pendings.set(batch.id, state);
1397
+ for (const { batch, state } of queue.pending) {
1398
+ pendings.set(pendingKey(queue.locator, batch.id), state);
1399
+ }
1149
1400
  }
1150
- const previousReplicas = new Map(replicas);
1151
1401
  replicas.clear();
1402
+ reopened.clear();
1152
1403
  queuesWithoutReplica.clear();
1153
- const replicated = new Set(records.map((record) => record.locator));
1154
1404
  for (const [locator, queue] of queuedBatches) {
1155
- if (!replicated.has(locator))
1405
+ if (!held.rooms.has(locator))
1156
1406
  queuesWithoutReplica.set(locator, queue);
1157
1407
  }
1158
- for (const record of records) {
1159
- const model = restored(modelKey(record.locator, 'collection'), record.model);
1160
- const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
1161
- const prior = previousReplicas.get(record.locator);
1162
- const confirmed = confirmedOf({ ...record, model, selectionModel }, prior?.confirmed);
1163
- replicas.set(record.locator, replicaOf(confirmed, queuedBatches.get(record.locator) ?? [], prior));
1408
+ records.forEach((stored, index) => {
1409
+ if (fences.has(stored.locator))
1410
+ return;
1411
+ const record = { ...stored, model: restored(stored.locator, stored.model) };
1412
+ reopened.add(record.locator);
1413
+ replicas.set(record.locator, {
1414
+ record,
1415
+ queue: queuedBatches.get(record.locator) ?? [],
1416
+ overlaid: held.overlaid.get(record.key) ?? new Set(),
1417
+ count: counts[index] ?? 0,
1418
+ confirmed: 0,
1419
+ indexed: !held.unindexed.has(record.key),
1420
+ });
1164
1421
  const existing = rooms.get(record.locator);
1165
1422
  if (existing === undefined)
1166
1423
  list(record, false);
@@ -1170,20 +1427,21 @@ export function composeRuntime(options) {
1170
1427
  roomVersion: record.roomVersion,
1171
1428
  operations: record.operations,
1172
1429
  replica: record.replica,
1173
- model: modelTypes(model),
1430
+ model: modelTypes(record.model),
1174
1431
  typedSelection: record.typedSelection === null
1175
1432
  ? null
1176
- : {
1177
- roots: record.typedSelection.roots,
1178
- model: modelTypes(selectionModel),
1179
- },
1433
+ : { roots: record.typedSelection.roots, model: modelTypes(builtIn) },
1180
1434
  });
1181
1435
  }
1182
- }
1436
+ });
1437
+ for (const key of held.orphans)
1438
+ await opened.release(key);
1439
+ if (held.erasing || held.orphans.length > 0)
1440
+ queueMicrotask(sweep);
1183
1441
  return {
1184
- lastCompleted,
1442
+ lastCompleted: held.lastCompleted,
1185
1443
  locators: new Set([...queuedBatches.keys(), ...replicas.keys()]),
1186
- read: mergeChangeSets([...new Set([...previousReplicas.keys(), ...replicas.keys()])].map((locator) => changeSet(locator, previousReplicas.get(locator) ?? null, replicas.get(locator) ?? null))),
1444
+ read: mergeChangeSets([...replicas.keys()].map((locator) => changeSet(locator, { documents: null }))),
1187
1445
  };
1188
1446
  }
1189
1447
  async function readState(opened) {
@@ -1202,8 +1460,7 @@ export function composeRuntime(options) {
1202
1460
  async function open() {
1203
1461
  if (closed)
1204
1462
  throw new Error('runtime closed');
1205
- const opened = await openRuntimeStore(isFenced);
1206
- store = opened;
1463
+ const opened = openRuntimeStore(isFenced, database);
1207
1464
  const interrupted = await opened.fences();
1208
1465
  for (const locator of interrupted) {
1209
1466
  fences.set(locator, { at: discoveries, erased: false });
@@ -1222,9 +1479,17 @@ export function composeRuntime(options) {
1222
1479
  emitStorageLoss();
1223
1480
  }
1224
1481
  const lastCompleted = await readState(opened);
1225
- if (interrupted.length > 0)
1226
- await erased(opened, interrupted);
1482
+ if (interrupted.length > 0) {
1483
+ queueMicrotask(() => {
1484
+ void eraseFenced(interrupted).catch(() => {
1485
+ journal.record('erasure failed');
1486
+ });
1487
+ });
1488
+ }
1227
1489
  moveTo({ ...status, lastCompleted, ...attention() });
1490
+ void exclusive(() => unpair(opened)).catch(() => {
1491
+ journal.record('unpair failed');
1492
+ });
1228
1493
  return opened;
1229
1494
  }
1230
1495
  function durably(work) {
@@ -1241,180 +1506,376 @@ export function composeRuntime(options) {
1241
1506
  return opening;
1242
1507
  }
1243
1508
  function exclusive(work) {
1509
+ gathering = null;
1244
1510
  const next = mutations.then(work, work);
1245
1511
  mutations = next.then(() => undefined, () => undefined);
1246
1512
  return next;
1247
1513
  }
1248
- function withdrawable(batch) {
1249
- const state = pendings.get(batch.id);
1250
- return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
1514
+ function gathered(key, input, merge, work) {
1515
+ const tail = gathering;
1516
+ if (tail !== null && tail.key === key) {
1517
+ const merged = merge(tail.input, input);
1518
+ if (merged !== null) {
1519
+ tail.input = merged;
1520
+ return tail.done;
1521
+ }
1522
+ }
1523
+ const entry = { key, input, done: Promise.resolve() };
1524
+ entry.done = exclusive(() => {
1525
+ if (gathering === entry)
1526
+ gathering = null;
1527
+ return work(entry.input);
1528
+ });
1529
+ gathering = entry;
1530
+ return entry.done;
1531
+ }
1532
+ function withdrawable(locator) {
1533
+ return (batch) => {
1534
+ const state = pendings.get(pendingKey(locator, batch.id));
1535
+ return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
1536
+ };
1537
+ }
1538
+ const REWRITE_LINKS = ['field', 'body'];
1539
+ const CHECKED_LINKS = ['field', 'section'];
1540
+ async function sliceAt(record, view, queue, extra = [], moved = []) {
1541
+ const local = await openLocal();
1542
+ const needed = new Set([...touchedBy(queue), ...extra]);
1543
+ const movedPaths = [...new Set([...movedBy(queue, record.locator), ...moved])];
1544
+ if (movedPaths.length > 0) {
1545
+ for (const { path } of await local.referrers(record.key, view, movedPaths, REWRITE_LINKS)) {
1546
+ needed.add(path);
1547
+ }
1548
+ }
1549
+ const paths = [...needed];
1550
+ return stepped(slicing(await local.entries(record.key, view, paths), paths));
1551
+ }
1552
+ async function landedWhole(locator, batch, confirmedLayer, current) {
1553
+ if (!landed(batch, confirmedLayer, current))
1554
+ return false;
1555
+ const partner = partnerOf(batch, locator);
1556
+ if (partner === null)
1557
+ return true;
1558
+ const other = replicas.get(partner);
1559
+ const half = other?.queue.find((one) => one.id === batch.id);
1560
+ if (other === undefined || half === undefined)
1561
+ return false;
1562
+ return landed(half, await sliceAt(other.record, 'confirmed', [half]));
1563
+ }
1564
+ function carriedBack(locator, unlanded, written) {
1565
+ const back = new Map();
1566
+ const taken = [];
1567
+ for (const batch of unlanded) {
1568
+ const between = batch.between;
1569
+ if (between === undefined)
1570
+ continue;
1571
+ const source = between.source.room;
1572
+ const destination = between.destination.room;
1573
+ for (const { path: to } of pendings.get(pendingKey(destination, batch.id))?.drafts ?? []) {
1574
+ const record = roomDrafts.get(destination)?.get(to);
1575
+ const path = between.moves.find((move) => move.to === to)?.path;
1576
+ if (record === undefined || path === undefined)
1577
+ continue;
1578
+ const [into, key] = source === locator ? [written, path] : [back, documentKey(source, path)];
1579
+ const existing = into.get(key) ?? roomDrafts.get(source)?.get(path);
1580
+ taken.push({ locator: destination, path: to });
1581
+ into.set(key, {
1582
+ locator: source,
1583
+ path,
1584
+ revision: roomDraftRevision(),
1585
+ parts: returnedInto(existing?.parts, record.parts),
1586
+ returned: existing?.returned ?? null,
1587
+ });
1588
+ }
1589
+ }
1590
+ const records = [...back.values()];
1591
+ const elsewhere = taken.filter((one) => one.locator !== locator);
1592
+ const rooms = new Set([...records, ...elsewhere].map((one) => one.locator));
1593
+ return {
1594
+ taken: taken.filter((one) => one.locator === locator).map(({ path }) => path),
1595
+ writes: [...rooms].map((room) => ({
1596
+ locator: room,
1597
+ roomDrafts: records.filter((record) => record.locator === room),
1598
+ consumedRoomDrafts: elsewhere.filter((one) => one.locator === room).map(({ path }) => path),
1599
+ })),
1600
+ changes: [...rooms].map((room) => changeSet(room, {
1601
+ drafts: [...records, ...elsewhere]
1602
+ .filter((one) => one.locator === room)
1603
+ .map(({ path }) => path),
1604
+ })),
1605
+ remember() {
1606
+ for (const one of taken)
1607
+ forgetRoomDraft(one.locator, one.path);
1608
+ for (const record of records)
1609
+ rememberRoomDraft(record);
1610
+ },
1611
+ };
1612
+ }
1613
+ function committedRoom(locator, answer) {
1614
+ const indexed = answer.indexed[locator] ?? replicas.get(locator)?.indexed ?? false;
1615
+ if (!indexed)
1616
+ prepare([locator]);
1617
+ return { count: answer.counts[locator] ?? replicas.get(locator)?.count ?? 0, indexed };
1251
1618
  }
1252
- async function settle(local, locator, before, confirmed, queue, settling = {}) {
1619
+ function replayed(record, confirmedLayer, queue) {
1620
+ return stepped(replaying(confirmedLayer, queue, record.locator, composed(record)));
1621
+ }
1622
+ async function overlayDiff(record, before, visible, touched) {
1623
+ const kept = before !== undefined && before.record.key === record.key
1624
+ ? before.overlaid
1625
+ : new Set();
1626
+ return {
1627
+ overlay: {
1628
+ set: await stepped(overlayOf(visible, touched)),
1629
+ cleared: [...kept].filter((path) => !touched.has(path)),
1630
+ },
1631
+ touched,
1632
+ };
1633
+ }
1634
+ async function settle(local, locator, before, record, queue, settling = {}) {
1635
+ return together(local, [await settlement(local, locator, before, record, queue, settling)]);
1636
+ }
1637
+ function unpairedIn(locator, batch) {
1638
+ if (!queueIn(locator).some((held) => held.id === batch.id))
1639
+ return false;
1640
+ if (pendings.get(pendingKey(locator, batch.id))?.accepted !== undefined)
1641
+ return false;
1642
+ return !leavingIn(batch, locator) || withdrawable(locator)(batch);
1643
+ }
1644
+ async function together(local, settlements) {
1645
+ const settled = new Set(settlements.map(({ locator }) => locator));
1646
+ const all = [...settlements];
1647
+ for (let wave = settlements; wave.length > 0;) {
1648
+ const partnered = new Map();
1649
+ for (const { locator, left } of wave) {
1650
+ for (const batch of left) {
1651
+ const partner = partnerOf(batch, locator);
1652
+ if (partner === null || settled.has(partner) || !unpairedIn(partner, batch))
1653
+ continue;
1654
+ partnered.set(partner, [...(partnered.get(partner) ?? []), batch]);
1655
+ }
1656
+ }
1657
+ const partners = [];
1658
+ for (const [partner, batches] of partnered) {
1659
+ settled.add(partner);
1660
+ partners.push(await unpairing(local, partner, batches));
1661
+ }
1662
+ all.push(...partners);
1663
+ wave = partners;
1664
+ }
1665
+ const answer = await local.commit({
1666
+ rooms: all.flatMap(({ commit }) => commit.rooms),
1667
+ writes: all.flatMap(({ commit }) => commit.writes),
1668
+ });
1669
+ return mergeChangeSets(all.map((one) => one.apply(answer)));
1670
+ }
1671
+ async function settlement(local, locator, before, record, queue, settling = {}) {
1672
+ const view = settling.view ?? 'confirmed';
1253
1673
  const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
1254
- const { withdrawn, kept } = await stepped(standing(confirmed, queue, withdrawable, localSaves));
1674
+ const overrides = settling.changes;
1675
+ const read = await sliceAt(record, view, queue, [
1676
+ ...(before !== undefined && before.record.key === record.key ? before.overlaid : []),
1677
+ ...(overrides?.set.map(({ path }) => path) ?? []),
1678
+ ...(overrides?.removed ?? []),
1679
+ ]);
1680
+ const confirmedLayer = overrides === undefined
1681
+ ? read
1682
+ : await stepped(slicingWith(read, overrides.set, overrides.removed));
1683
+ const composition = composed(record);
1684
+ const { withdrawn, kept } = await stepped(standing(confirmedLayer, queue, withdrawable(locator), localSaves, locator, composition));
1255
1685
  const founded = kept.filter((batch) => !queue.includes(batch));
1256
1686
  const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
1257
- const unqueued = withdrawn.map((batch) => ({
1258
- batch,
1259
- returned: localSaves || landed(batch, confirmed) ? null : returnedBy('withdrawn'),
1260
- }));
1687
+ const unqueued = [];
1688
+ for (const batch of withdrawn) {
1689
+ unqueued.push({ batch, landed: await landedWhole(locator, batch, confirmedLayer) });
1690
+ }
1261
1691
  const returning = [
1262
- ...[...unqueued].reverse(),
1263
- ...(settling.leaving === undefined ? [] : [settling.leaving]),
1692
+ ...[...unqueued].reverse().map(({ batch, landed: held }) => ({
1693
+ batch,
1694
+ returned: localSaves || held ? null : returnedBy('withdrawn'),
1695
+ })),
1696
+ ...(settling.leaving ?? []),
1264
1697
  ];
1265
1698
  const written = new Map();
1266
- for (const { batch, returned } of returning) {
1267
- if (returned === null)
1699
+ for (const { batch, returned: reason } of returning) {
1700
+ if (reason === null || batch.between?.destination.room === locator)
1268
1701
  continue;
1269
- for (const saved of pendings.get(batch.id)?.drafts ?? []) {
1702
+ for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
1270
1703
  const existing = written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path);
1271
1704
  written.set(saved.path, {
1272
1705
  locator,
1273
1706
  path: saved.path,
1274
1707
  revision: roomDraftRevision(),
1275
1708
  parts: returnedInto(existing?.parts, saved.parts),
1276
- returned,
1709
+ returned: reason,
1277
1710
  });
1278
1711
  }
1279
1712
  }
1280
- await local.keepAuthored({ locator, queue: kept, founded, roomDrafts: [...written.values()] });
1713
+ const carried = carriedBack(locator, [
1714
+ ...[...unqueued].reverse().flatMap(({ batch, landed: held }) => (held ? [] : [batch])),
1715
+ ...(settling.leaving ?? []).flatMap(({ batch, returned }) => returned === null ? [] : [batch]),
1716
+ ], written);
1717
+ const replayedLayer = await replayed(record, confirmedLayer, kept);
1718
+ const { overlay, touched } = await overlayDiff(record, before, replayedLayer.visible, replayedLayer.touched);
1719
+ const commit = {
1720
+ rooms: [
1721
+ {
1722
+ locator,
1723
+ key: record.key,
1724
+ record: storedRecord(record),
1725
+ roots: record.typedSelection?.roots ?? [],
1726
+ composed: composedOf(composition),
1727
+ ...settling.commit,
1728
+ ...(overrides === undefined
1729
+ ? {}
1730
+ : { confirmed: { set: overrides.set, removed: overrides.removed } }),
1731
+ overlay,
1732
+ },
1733
+ ],
1734
+ writes: [
1735
+ {
1736
+ locator,
1737
+ queue: kept,
1738
+ founded,
1739
+ roomDrafts: [...written.values()],
1740
+ consumedRoomDrafts: carried.taken,
1741
+ },
1742
+ ...carried.writes,
1743
+ ],
1744
+ };
1281
1745
  const staying = new Set(kept.map((batch) => batch.id));
1282
- for (const batch of previousQueue) {
1283
- if (!staying.has(batch.id))
1284
- pendings.delete(batch.id);
1285
- }
1286
- for (const record of written.values())
1287
- rememberRoomDraft(record);
1288
- try {
1289
- await local.write({
1290
- next: confirmed.record,
1291
- previous: before?.confirmed.record ?? null,
1292
- ...(settling.stored === undefined ? {} : { stored: settling.stored }),
1293
- });
1294
- }
1295
- catch (error) {
1296
- const held = before === undefined ? null : await stepped(replicating(before.confirmed, kept, before));
1297
- const set = await stepped(changing(locator, before ?? null, held, {
1298
- queue: queueChanged,
1299
- drafts: [...written.keys()],
1300
- }));
1301
- if (held !== null)
1302
- replicas.set(locator, held);
1303
- else if (kept.length > 0)
1304
- queuesWithoutReplica.set(locator, kept);
1305
- else
1746
+ return {
1747
+ locator,
1748
+ left: previousQueue.filter((batch) => !staying.has(batch.id)),
1749
+ commit,
1750
+ apply: (answer) => {
1751
+ for (const batch of previousQueue) {
1752
+ if (!staying.has(batch.id))
1753
+ pendings.delete(pendingKey(locator, batch.id));
1754
+ }
1755
+ for (const draft of written.values())
1756
+ rememberRoomDraft(draft);
1757
+ carried.remember();
1758
+ const drafts = [...written.keys(), ...carried.taken];
1306
1759
  queuesWithoutReplica.delete(locator);
1307
- publishChangeSet(set);
1308
- moveTo({ ...status, ...attention() });
1309
- throw error;
1310
- }
1311
- const changed = { room: settling.room, queue: queueChanged, drafts: [...written.keys()] };
1312
- const next = await stepped(replicating(confirmed, kept, before));
1313
- const set = await stepped(changing(locator, before ?? null, next, changed));
1314
- return () => {
1315
- queuesWithoutReplica.delete(locator);
1316
- for (const { batch, returned } of unqueued) {
1317
- const outcome = localSaves ? 'dropped' : returned === null ? 'landed' : 'withdrawn';
1318
- journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
1319
- }
1320
- replicas.set(locator, next);
1321
- return heldChange(locator, before ?? null, next, set, changed);
1760
+ for (const { batch, landed: held } of unqueued) {
1761
+ const outcome = localSaves ? 'dropped' : held ? 'landed' : 'withdrawn';
1762
+ journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
1763
+ }
1764
+ const changed = answer.changed[locator];
1765
+ const confirmedChanged = settling.commit?.published !== undefined ||
1766
+ settling.commit?.refreshed === true ||
1767
+ overrides !== undefined;
1768
+ const { count, indexed } = committedRoom(locator, answer);
1769
+ replicas.set(locator, {
1770
+ record,
1771
+ queue: kept,
1772
+ overlaid: touched,
1773
+ count,
1774
+ confirmed: (before?.confirmed ?? 0) + (confirmedChanged ? 1 : 0),
1775
+ indexed,
1776
+ });
1777
+ return mergeChangeSets([
1778
+ changeSet(locator, {
1779
+ documents: changed === undefined ? [] : changed,
1780
+ room: settling.room,
1781
+ queue: queueChanged,
1782
+ drafts,
1783
+ }),
1784
+ ...carried.changes,
1785
+ ]);
1786
+ },
1322
1787
  };
1323
1788
  }
1324
- function heldChange(locator, before, next, set, changed) {
1325
- const held = replicas.get(locator) ?? null;
1326
- return held === next ? set : changeSet(locator, before, held, changed);
1327
- }
1328
1789
  function adoptModels(locator, record) {
1329
- for (const [attached, model] of [
1330
- ['collection', record.model],
1331
- ['typed-selection', record.selectionModel],
1332
- ]) {
1333
- const key = modelKey(locator, attached);
1334
- models.set(key, model);
1335
- invalidModels.delete(key);
1336
- if (model.status === 'current')
1337
- liveModels.set(key, JSON.stringify(model.snapshot));
1338
- else if (model.status === 'unavailable')
1339
- liveModels.delete(key);
1340
- }
1790
+ const model = record.model;
1791
+ models.set(locator, model);
1792
+ invalidModels.delete(locator);
1793
+ if (model.status === 'current')
1794
+ liveModels.set(locator, JSON.stringify(model.snapshot));
1795
+ else if (model.status === 'unavailable')
1796
+ liveModels.delete(locator);
1341
1797
  }
1342
1798
  function remodel(locator, answers, held) {
1343
- const taken = (answer, previous, attached) => {
1344
- if (answer === undefined)
1345
- return previous;
1346
- const { state, refused } = attachedModel(answer, previous);
1347
- if (refused !== null) {
1348
- const where = refused.location === undefined ? '' : ` ${refused.location}`;
1349
- journal.record(`model invalid ${locator} ${attached} ${refused.identity} ${refused.rule}${where}`);
1350
- }
1351
- models.set(modelKey(locator, attached), state);
1352
- return state;
1353
- };
1354
- return {
1355
- collection: taken(answers?.collection, held.collection, 'collection'),
1356
- selection: taken(answers?.selection, held.selection, 'typed-selection'),
1357
- };
1358
- }
1359
- function publish(local, room, confirmed, landing, live) {
1360
- const { base, stored } = landing;
1361
- return exclusive(async () => {
1362
- if (stored !== undefined) {
1363
- await indexReferrers({ text: confirmed.record.documents, binaries: confirmed.record.binaries }, compositionOf(confirmed.record));
1364
- }
1799
+ const answer = answers?.collection;
1800
+ if (answer === undefined)
1801
+ return held;
1802
+ const { state, refused } = attachedModel(answer, held.collection, roomTypes);
1803
+ if (refused !== null) {
1804
+ const where = refused.location === undefined ? '' : ` ${refused.location}`;
1805
+ journal.record(`model invalid ${locator} collection ${refused.identity} ${refused.rule}${where}`);
1806
+ }
1807
+ models.set(locator, state);
1808
+ return { collection: state };
1809
+ }
1810
+ function publish(local, room, record, landing, live) {
1811
+ const landed = exclusive(async () => {
1365
1812
  if (!live()) {
1366
1813
  caughtUpTo(room.locator);
1367
1814
  return true;
1368
1815
  }
1369
1816
  const prior = replicas.get(room.locator);
1370
- if (base !== undefined &&
1371
- (prior?.confirmed.record.roomVersion !== base.record.roomVersion ||
1372
- prior.confirmed.record.revisions !== base.record.revisions)) {
1817
+ if (landing.kind === 'refreshed' &&
1818
+ (prior === undefined || prior.record.key !== record.key || prior.confirmed !== landing.base)) {
1819
+ await local.unrefresh(record.key);
1373
1820
  return false;
1374
1821
  }
1375
- adoptModels(room.locator, confirmed.record);
1822
+ adoptModels(room.locator, record);
1376
1823
  const queue = prior?.queue ?? queuesWithoutReplica.get(room.locator) ?? [];
1377
- const roomVersion = confirmed.record.roomVersion;
1824
+ const roomVersion = record.roomVersion;
1378
1825
  const accepted = queue.filter((batch) => {
1379
- const accepted = pendings.get(batch.id)?.accepted;
1380
- return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
1826
+ const at = pendings.get(pendingKey(room.locator, batch.id))?.accepted;
1827
+ return at !== undefined && options.adapter.reached(room.locator, roomVersion, at);
1381
1828
  });
1382
- let settled;
1829
+ let set;
1383
1830
  try {
1384
- settled = await settle(local, room.locator, prior, confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true, ...(stored === undefined ? {} : { stored }) });
1831
+ set = await settle(local, room.locator, prior, record, queue.filter((batch) => !accepted.includes(batch)), landing.kind === 'refreshed'
1832
+ ? {
1833
+ room: true,
1834
+ view: 'refreshed',
1835
+ commit: { refreshed: true },
1836
+ }
1837
+ : {
1838
+ room: true,
1839
+ view: landing.refreshed ? 'refreshed' : 'confirmed',
1840
+ commit: {
1841
+ published: { from: prior?.record.key ?? null },
1842
+ ...(landing.refreshed ? { refreshed: true } : {}),
1843
+ },
1844
+ });
1385
1845
  }
1386
1846
  catch (error) {
1387
- if (!exceeded(error))
1847
+ if (!refusedForSpace(error))
1388
1848
  throw error;
1389
- await exceededOn(room.locator, grownBy(confirmed.record, prior?.confirmed.record));
1390
1849
  caughtUpTo(room.locator);
1391
1850
  return true;
1392
1851
  }
1393
- const set = settled();
1394
- if (stored !== undefined) {
1395
- transfers.delete(room.locator);
1852
+ if (landing.kind === 'bootstrapped') {
1853
+ untransferred(room.locator);
1396
1854
  catching.delete(room.locator);
1855
+ sweep();
1397
1856
  }
1398
1857
  for (const batch of accepted)
1399
- pendings.delete(batch.id);
1858
+ pendings.delete(pendingKey(room.locator, batch.id));
1400
1859
  stale.delete(room.locator);
1401
- unindexable.delete(room.locator);
1860
+ unpreparable.delete(room.locator);
1402
1861
  if (unreadable.delete(room.locator))
1403
1862
  scopedRefusals.delete(room.locator);
1404
1863
  sourceRefusals.delete(room.locator);
1405
1864
  declarationRefusals.delete(room.locator);
1406
1865
  list(room, true);
1407
- if (prior === undefined)
1408
- prepared.add(room.locator);
1409
1866
  publishChangeSet(set);
1867
+ await unpair(local);
1410
1868
  moveTo({ ...status, ...attention() });
1411
1869
  options.adapter.roomStream?.published(room.locator, roomVersion);
1870
+ if (accepted.length > 0 && (replicas.get(room.locator)?.queue.length ?? 0) > 0) {
1871
+ submitLater(room.locator);
1872
+ }
1412
1873
  return true;
1413
1874
  });
1875
+ return landing.kind === 'refreshed' ? landed : landed.finally(() => installed(room.locator));
1414
1876
  }
1415
1877
  async function savedRoom(locator) {
1416
1878
  const local = await openLocal();
1417
- await referenced(locator);
1418
1879
  const replica = replicas.get(locator);
1419
1880
  const room = rooms.get(locator);
1420
1881
  if (replica === undefined || room === undefined) {
@@ -1444,13 +1905,19 @@ export function composeRuntime(options) {
1444
1905
  }
1445
1906
  const target = await savedRoom(locator);
1446
1907
  const saved = [];
1908
+ const files = [];
1909
+ const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
1910
+ const visible = filed.length === 0 ? undefined : await visibleOf(locator, filed);
1447
1911
  for (const { path, parts } of read) {
1448
1912
  const file = parts.file === undefined
1449
1913
  ? undefined
1450
- : await heldFile(parts.file.blob, binaryBase(target.replica.visible, path));
1914
+ : await heldFile(parts.file.blob, binaryBase(visible, path));
1915
+ if (parts.file !== undefined && file !== undefined) {
1916
+ files.push({ key: file.localId, blob: parts.file.blob });
1917
+ }
1451
1918
  saved.push({ path, parts: held(undefined, parts, file) });
1452
1919
  }
1453
- return committed(locator, target, saved, new Set(), writtenBy);
1920
+ return committed(locator, target, saved, new Set(), writtenBy, files);
1454
1921
  }
1455
1922
  function carriedDrafts(locator, saved, consumed) {
1456
1923
  const holding = roomDrafts.get(locator);
@@ -1464,13 +1931,358 @@ export function composeRuntime(options) {
1464
1931
  return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
1465
1932
  }));
1466
1933
  }
1467
- async function committed(locator, { local, replica, room }, saved, consumed, writtenBy) {
1468
- const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
1934
+ async function savingIn(replica, changes, extra, leaving = []) {
1935
+ const record = replica.record;
1936
+ const local = await openLocal();
1937
+ const paths = [
1938
+ ...extra,
1939
+ ...changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]),
1940
+ ];
1941
+ const moved = [
1942
+ ...leaving,
1943
+ ...changes.flatMap((change) => (change.kind === 'move' ? [change.path] : [])),
1944
+ ];
1945
+ const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
1946
+ const referring = removed.length === 0
1947
+ ? []
1948
+ : (await local.referrers(record.key, 'confirmed', removed, CHECKED_LINKS)).map(({ path }) => path);
1949
+ const confirmedLayer = await sliceAt(record, 'confirmed', replica.queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
1950
+ const { visible } = await replayed(record, confirmedLayer, replica.queue);
1951
+ const composition = composed(record);
1952
+ return {
1953
+ record,
1954
+ confirmed: confirmedLayer,
1955
+ visible,
1956
+ folders: new Set(await local.folders(record.key, 'visible', paths)),
1957
+ *visibleOver(queue) {
1958
+ return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
1959
+ },
1960
+ };
1961
+ }
1962
+ async function unmodeledIn(record) {
1963
+ const composition = compositionFor(record);
1964
+ if (available(composition))
1965
+ return false;
1966
+ const local = await openLocal();
1967
+ const roots = record.typedSelection?.roots ?? [];
1968
+ return local.typedWithin(record.key, 'visible', roots, false);
1969
+ }
1970
+ function textDiffers(left, right) {
1971
+ return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
1972
+ !sameBinary(left?.binary ?? undefined, right?.binary ?? undefined));
1973
+ }
1974
+ function textOf(entry) {
1975
+ if (entry === null)
1976
+ return undefined;
1977
+ return entry.binary !== null ? null : entry.content;
1978
+ }
1979
+ function walkOf(local, key, view) {
1980
+ return async (after) => {
1981
+ const page = await local.walk(key, view, after, WALK_BYTES);
1982
+ return {
1983
+ entries: page.entries.map(({ path, content, binary }) => ({
1984
+ path,
1985
+ content: binary !== null ? null : content,
1986
+ })),
1987
+ next: page.next,
1988
+ };
1989
+ };
1990
+ }
1991
+ function lookupOf(local, key, view) {
1992
+ return async (paths) => {
1993
+ if (paths.length === 0)
1994
+ return new Map();
1995
+ return new Map((await local.typed(key, view, paths)).map(({ path, type, binary }) => [
1996
+ path,
1997
+ { type: type ?? undefined, binary },
1998
+ ]));
1999
+ };
2000
+ }
2001
+ function declaredOf(entry) {
2002
+ if (entry.binary !== null)
2003
+ return { type: undefined, binary: true };
2004
+ const read = parseFrontmatter(entry.content ?? '');
2005
+ return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
2006
+ }
2007
+ function overriddenWalk(walk, overrides) {
2008
+ const sorted = [...overrides.keys()].sort();
2009
+ const beyond = (path) => {
2010
+ if (path === null)
2011
+ return 0;
2012
+ let low = 0;
2013
+ let high = sorted.length;
2014
+ while (low < high) {
2015
+ const middle = (low + high) >>> 1;
2016
+ if (sorted[middle] <= path)
2017
+ low = middle + 1;
2018
+ else
2019
+ high = middle;
2020
+ }
2021
+ return low;
2022
+ };
2023
+ return async (after) => {
2024
+ const page = await walk(after);
2025
+ const end = page.next;
2026
+ const added = sorted
2027
+ .slice(beyond(after), end === null ? sorted.length : beyond(end))
2028
+ .flatMap((path) => {
2029
+ const entry = overrides.get(path);
2030
+ return entry === null || entry === undefined
2031
+ ? []
2032
+ : [{ path, content: entry.binary !== null ? null : entry.content }];
2033
+ });
2034
+ const entries = [...page.entries.filter(({ path }) => !overrides.has(path)), ...added].sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
2035
+ return { entries, next: end };
2036
+ };
2037
+ }
2038
+ function overriddenLookup(lookup, overrides) {
2039
+ return async (paths) => {
2040
+ const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
2041
+ for (const path of paths) {
2042
+ const entry = overrides.get(path);
2043
+ if (entry === undefined)
2044
+ continue;
2045
+ if (entry === null)
2046
+ known.delete(path);
2047
+ else
2048
+ known.set(path, declaredOf(entry));
2049
+ }
2050
+ return known;
2051
+ };
2052
+ }
2053
+ function validatorOf(record, confirmedLayer) {
2054
+ return async (resulting, touched) => {
2055
+ const composition = compositionFor(record);
2056
+ const local = await openLocal();
2057
+ const changed = [];
2058
+ const overrides = new Map();
2059
+ const pace = paced();
2060
+ for (const path of new Set([...confirmedLayer.held, ...touched])) {
2061
+ await pace(ROW_WEIGHT);
2062
+ const was = entryAt(confirmedLayer, path);
2063
+ const now = entryAt(resulting, path);
2064
+ if (!textDiffers(was, now))
2065
+ continue;
2066
+ overrides.set(path, now);
2067
+ if (textOf(was) !== textOf(now))
2068
+ changed.push({ path, was: textOf(was), now: textOf(now) });
2069
+ }
2070
+ const lookup = overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides);
2071
+ const clean = available(composition) &&
2072
+ changed.length <= CLEAN_CHANGES &&
2073
+ (await changedCleanly(changed, composed(record), composition, {
2074
+ lookup,
2075
+ texts: textsOf(local, record.key, 'confirmed'),
2076
+ referrers: referrersOf(local, record.key, 'confirmed'),
2077
+ }));
2078
+ if (clean)
2079
+ return { verdict: 'accept' };
2080
+ return validatedCollection(overriddenWalk(walkOf(local, record.key, 'confirmed'), overrides), lookup, composition);
2081
+ };
2082
+ }
2083
+ function textsOf(local, key, view) {
2084
+ return async (paths) => new Map((await local.entries(key, view, paths)).map(({ path, content, binary }) => [
2085
+ path,
2086
+ binary !== null ? null : content,
2087
+ ]));
2088
+ }
2089
+ function referrersOf(local, key, view) {
2090
+ return async (targets) => [
2091
+ ...new Set((await local.referrers(key, view, targets, CHECKED_LINKS)).map(({ path }) => path)),
2092
+ ];
2093
+ }
2094
+ async function sideOf(locator, replica, room, saving) {
2095
+ const record = replica.record;
2096
+ return {
2097
+ room: locator,
2098
+ handle: room.handle,
2099
+ baseRoomVersion: record.roomVersion,
2100
+ composition: compositionFor(record),
2101
+ operations: room.operations,
2102
+ providerOwnedPaths: room.providerOwnedPaths,
2103
+ visible: saving.visible,
2104
+ folders: saving.folders,
2105
+ queued: queuedDocuments(replica.queue),
2106
+ unmodeled: await unmodeledIn(record),
2107
+ validate: validatorOf(record, saving.confirmed),
2108
+ };
2109
+ }
2110
+ function movedInto(locator, saved) {
2111
+ const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
2112
+ return into?.parts.move?.room ?? null;
2113
+ }
2114
+ async function saveCommit(local, files, rooms) {
2115
+ const touched = new Map();
2116
+ const commits = [];
2117
+ for (const { locator, saving, before, queue } of rooms) {
2118
+ const replayedLayer = await replayed(saving.record, saving.confirmed, queue);
2119
+ const diff = await overlayDiff(saving.record, before, replayedLayer.visible, replayedLayer.touched);
2120
+ touched.set(locator, diff.touched);
2121
+ commits.push({
2122
+ locator,
2123
+ key: saving.record.key,
2124
+ roots: saving.record.typedSelection?.roots ?? [],
2125
+ composed: composedOf(composed(saving.record)),
2126
+ overlay: diff.overlay,
2127
+ });
2128
+ }
2129
+ try {
2130
+ const answer = await local.commit({
2131
+ rooms: commits,
2132
+ writes: rooms.map(({ write }) => write),
2133
+ files,
2134
+ whole: true,
2135
+ });
2136
+ return { answer, touched };
2137
+ }
2138
+ catch {
2139
+ return null;
2140
+ }
2141
+ }
2142
+ function savedReplica(locator, before, queue, touched, answer) {
2143
+ replicas.set(locator, {
2144
+ ...before,
2145
+ queue,
2146
+ overlaid: touched,
2147
+ count: answer.counts[locator] ?? before.count,
2148
+ });
2149
+ }
2150
+ async function committedMove(locator, destination, { local, replica, room }, saved, consumed, writtenBy, files) {
2151
+ const mixed = saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
2152
+ if (mixed !== undefined) {
2153
+ return { outcome: 'refused', code: 'mixed-room-move', retryable: false, file: mixed.path };
2154
+ }
2155
+ const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
2156
+ const sourceSaving = await savingIn(replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
2157
+ const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, sourceSaving.visible)));
1469
2158
  if (changed.length > 0)
1470
2159
  return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
1471
- const parts = saved.map(({ path, parts }) => changesOf(path, parts));
1472
- const changes = parts.flatMap(({ changes }) => changes);
1473
- const requeue = requeued(replica.queue, changes, replica.visible, withdrawable, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
2160
+ const target = replicas.get(destination);
2161
+ const listedRoom = rooms.get(destination);
2162
+ if (target === undefined || listedRoom === undefined) {
2163
+ throw runtimeError('room-not-held', `No Local Replica for ${destination}`);
2164
+ }
2165
+ const arriving = await savingIn(target, [], moves.map(({ to }) => to));
2166
+ const built = await buildMove({
2167
+ source: await sideOf(locator, replica, room, sourceSaving),
2168
+ destination: await sideOf(destination, target, listedRoom, arriving),
2169
+ moves,
2170
+ providerLimits: limits,
2171
+ local: localSaves,
2172
+ ...(writtenBy === undefined ? {} : { writtenBy }),
2173
+ });
2174
+ if (built.kind === 'refused')
2175
+ return { outcome: 'refused', ...built.refusal };
2176
+ const id = built.leaving.id;
2177
+ const carrying = saved.flatMap(({ path, parts }) => {
2178
+ const record = roomDrafts.get(locator)?.get(path);
2179
+ const to = moveTarget(parts.move);
2180
+ if (record === undefined || consumed.has(path) || roomDrafts.get(destination)?.has(to)) {
2181
+ return [];
2182
+ }
2183
+ const moved = {
2184
+ ...record,
2185
+ locator: destination,
2186
+ path: to,
2187
+ revision: roomDraftRevision(),
2188
+ };
2189
+ return [{ from: path, record: moved }];
2190
+ });
2191
+ const carried = carrying.map(({ record }) => record);
2192
+ const marked = carried.map(({ path }) => ({ path, parts: {} }));
2193
+ const left = [...consumed, ...carrying.map(({ from }) => from)];
2194
+ const leavingQueue = [...replica.queue, built.leaving];
2195
+ const arrivingQueue = [...target.queue, built.arriving];
2196
+ const committedBoth = await saveCommit(local, files, [
2197
+ {
2198
+ locator,
2199
+ saving: sourceSaving,
2200
+ before: replica,
2201
+ queue: leavingQueue,
2202
+ write: {
2203
+ locator,
2204
+ queue: leavingQueue,
2205
+ saved: new Map([[id, saved]]),
2206
+ consumedRoomDrafts: left,
2207
+ },
2208
+ },
2209
+ {
2210
+ locator: destination,
2211
+ saving: arriving,
2212
+ before: target,
2213
+ queue: arrivingQueue,
2214
+ write: {
2215
+ locator: destination,
2216
+ queue: arrivingQueue,
2217
+ saved: new Map([[id, marked]]),
2218
+ roomDrafts: carried,
2219
+ },
2220
+ },
2221
+ ]);
2222
+ if (committedBoth === null)
2223
+ return { outcome: 'refused', code: 'durable-write', retryable: true };
2224
+ const { answer, touched } = committedBoth;
2225
+ if (answer.fenced !== null) {
2226
+ throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
2227
+ }
2228
+ if (fences.has(locator) || fences.has(destination)) {
2229
+ throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
2230
+ }
2231
+ pendings.set(pendingKey(locator, id), {
2232
+ drafts: saved,
2233
+ submission: { state: 'waiting' },
2234
+ lost: false,
2235
+ });
2236
+ pendings.set(pendingKey(destination, id), {
2237
+ drafts: marked,
2238
+ submission: { state: 'waiting' },
2239
+ lost: false,
2240
+ });
2241
+ for (const path of left)
2242
+ forgetRoomDraft(locator, path);
2243
+ for (const record of carried)
2244
+ rememberRoomDraft(record);
2245
+ savedReplica(locator, replica, leavingQueue, touched.get(locator), answer);
2246
+ savedReplica(destination, target, arrivingQueue, touched.get(destination), answer);
2247
+ publishChangeSet(mergeChangeSets([
2248
+ changeSet(locator, {
2249
+ documents: answer.changed[locator] ?? [],
2250
+ queue: true,
2251
+ drafts: left,
2252
+ }),
2253
+ changeSet(destination, {
2254
+ documents: answer.changed[destination] ?? [],
2255
+ queue: true,
2256
+ drafts: carried.map(({ path }) => path),
2257
+ }),
2258
+ ]));
2259
+ queuedSave(locator);
2260
+ return { outcome: 'saved', identifier: id };
2261
+ }
2262
+ function queuedSave(locator) {
2263
+ if (!localSaves)
2264
+ recorded = false;
2265
+ moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
2266
+ submitLater(locator);
2267
+ }
2268
+ async function committed(locator, target, saved, consumed, writtenBy, files = []) {
2269
+ if (full)
2270
+ return { outcome: 'refused', code: 'durable-write', retryable: true };
2271
+ const into = movedInto(locator, saved);
2272
+ if (into !== null) {
2273
+ return committedMove(locator, into, target, saved, consumed, writtenBy, files);
2274
+ }
2275
+ const { local, replica, room } = target;
2276
+ const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
2277
+ const changes = parts.flatMap(({ changes: made }) => made);
2278
+ const saving = await savingIn(replica, changes, [
2279
+ ...saved.map(({ path }) => path),
2280
+ ...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
2281
+ ]);
2282
+ const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, underneath(held, path, saving.visible)));
2283
+ if (changed.length > 0)
2284
+ return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
2285
+ const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(locator), saving.visibleOver));
1474
2286
  if (requeue.kind === 'refused')
1475
2287
  return { outcome: 'refused', ...requeue.refusal };
1476
2288
  const foreign = [...requeue.withdrawn.keys()].some((origin) => {
@@ -1488,22 +2300,20 @@ export function composeRuntime(options) {
1488
2300
  const withdrawing = requeue.withdrawn.size > 0;
1489
2301
  const input = {
1490
2302
  local: localSaves,
1491
- baseRoomVersion: replica.confirmed.record.roomVersion,
1492
- composition: compositionFor(replica.confirmed.record),
2303
+ baseRoomVersion: replica.record.roomVersion,
2304
+ composition: compositionFor(replica.record),
1493
2305
  operations: room.operations,
1494
2306
  providerOwnedPaths: room.providerOwnedPaths,
1495
2307
  providerLimits: limits,
1496
2308
  handle: room.handle,
1497
- visible: replica.visible,
2309
+ visible: saving.visible,
2310
+ folders: saving.folders,
1498
2311
  changes,
1499
2312
  bases: parts.flatMap(({ bases }) => bases),
1500
2313
  queued: queuedDocuments(replica.queue),
1501
2314
  writtenBy,
1502
- clean: {
1503
- text: replica.confirmed.record.documents,
1504
- binaries: replica.confirmed.record.binaries,
1505
- composition: compositionOf(replica.confirmed.record),
1506
- },
2315
+ unmodeled: await unmodeledIn(replica.record),
2316
+ validate: validatorOf(replica.record, saving.confirmed),
1507
2317
  };
1508
2318
  const checked = withdrawing
1509
2319
  ? await buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
@@ -1528,8 +2338,7 @@ export function composeRuntime(options) {
1528
2338
  ? null
1529
2339
  : await buildBatch({
1530
2340
  ...input,
1531
- visible: (await stepped(replicating(replica.confirmed, requeue.queue, replica)))
1532
- .visible,
2341
+ visible: await stepped(saving.visibleOver(requeue.queue)),
1533
2342
  changes: requeue.changes,
1534
2343
  queued: queuedDocuments(requeue.queue),
1535
2344
  });
@@ -1548,7 +2357,7 @@ export function composeRuntime(options) {
1548
2357
  for (const [origin, replaced] of withdrawn) {
1549
2358
  if (replaced === null)
1550
2359
  continue;
1551
- const carried = withdrawnDrafts(pendings.get(origin)?.drafts ?? [], changes);
2360
+ const carried = withdrawnDrafts(pendings.get(pendingKey(locator, origin))?.drafts ?? [], changes);
1552
2361
  drafted.set(replaced.id, replaced === join?.batch
1553
2362
  ? [
1554
2363
  ...carried.map((held) => joinedDraft(held, saved)),
@@ -1562,66 +2371,77 @@ export function composeRuntime(options) {
1562
2371
  for (const replaced of withdrawn.values()) {
1563
2372
  if (replaced === null || !batches.includes(replaced))
1564
2373
  continue;
1565
- const limited = limitedIn(batches, replaced, limits, (queue) => replicaOf(replica.confirmed, queue, replica).visible);
2374
+ const limited = await stepped(limiting(batches, replaced, limits, saving.visibleOver));
1566
2375
  if (limited !== null)
1567
2376
  return { outcome: 'refused', ...limited };
1568
2377
  }
1569
2378
  const carried = carriedDrafts(locator, saved, consumed);
1570
2379
  const left = [...consumed, ...carried.keys()];
1571
2380
  const arrived = [...carried.values()];
1572
- try {
1573
- await local.keepAuthored({
2381
+ const done = await saveCommit(local, files, [
2382
+ {
1574
2383
  locator,
2384
+ saving,
2385
+ before: replica,
1575
2386
  queue: batches,
1576
- saved: drafted,
1577
- consumedRoomDrafts: left,
1578
- roomDrafts: arrived,
1579
- });
1580
- }
1581
- catch {
2387
+ write: {
2388
+ locator,
2389
+ queue: batches,
2390
+ saved: drafted,
2391
+ consumedRoomDrafts: left,
2392
+ roomDrafts: arrived,
2393
+ },
2394
+ },
2395
+ ]);
2396
+ if (done === null)
1582
2397
  return { outcome: 'refused', code: 'durable-write', retryable: true };
2398
+ const { answer, touched } = done;
2399
+ if (answer.fenced !== null) {
2400
+ throw runtimeError('room-not-held', `No Local Replica for ${answer.fenced}`);
1583
2401
  }
1584
- const next = await stepped(replicating(replica.confirmed, batches, replica));
1585
- const set = await stepped(changing(locator, replica, next, {
1586
- queue: true,
1587
- drafts: [...left, ...arrived.map(({ path }) => path)],
1588
- }));
1589
2402
  if (fences.has(locator))
1590
2403
  throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1591
2404
  for (const origin of withdrawn.keys())
1592
- pendings.delete(origin);
2405
+ pendings.delete(pendingKey(locator, origin));
1593
2406
  for (const [id, drafts] of drafted) {
1594
- pendings.set(id, { drafts, submission: { state: 'waiting' }, lost: false });
2407
+ pendings.set(pendingKey(locator, id), {
2408
+ drafts,
2409
+ submission: { state: 'waiting' },
2410
+ lost: false,
2411
+ });
1595
2412
  }
1596
- if (!localSaves)
1597
- recorded = false;
1598
2413
  for (const path of left)
1599
2414
  forgetRoomDraft(locator, path);
1600
2415
  for (const record of arrived)
1601
2416
  rememberRoomDraft(record);
1602
- replicas.set(locator, next);
1603
- publishChangeSet(set);
1604
- moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
1605
- submitLater(locator);
2417
+ savedReplica(locator, replica, batches, touched.get(locator), answer);
2418
+ publishChangeSet(changeSet(locator, {
2419
+ documents: answer.changed[locator] ?? [],
2420
+ queue: true,
2421
+ drafts: [...left, ...arrived.map(({ path }) => path)],
2422
+ }));
2423
+ queuedSave(locator);
1606
2424
  const identifier = join?.batch.id ??
1607
2425
  built?.batch.id ??
1608
2426
  [...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
1609
2427
  return { outcome: 'saved', identifier };
1610
2428
  }
1611
- async function mark(local, locator, identifier, submission, lost) {
1612
- const state = pendings.get(identifier);
1613
- if (!continuing() ||
1614
- state === undefined ||
1615
- (JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
1616
- return;
2429
+ async function mark(local, locator, batch, submission, lost) {
2430
+ const partner = partnerOf(batch, locator);
2431
+ for (const held of partner === null ? [locator] : [locator, partner]) {
2432
+ const state = pendings.get(pendingKey(held, batch.id));
2433
+ if (!continuing() ||
2434
+ state === undefined ||
2435
+ (JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
2436
+ continue;
2437
+ }
2438
+ await local.submit(held, batch.id, submission, lost);
2439
+ if (fences.has(held))
2440
+ continue;
2441
+ pendings.set(pendingKey(held, batch.id), { ...state, submission, lost });
2442
+ if (replicas.has(held))
2443
+ publishChangeSet(changeSet(held, { queue: true }));
1617
2444
  }
1618
- await local.submit(locator, identifier, submission, lost);
1619
- if (fences.has(locator))
1620
- return;
1621
- pendings.set(identifier, { ...state, submission, lost });
1622
- const replica = replicas.get(locator);
1623
- if (replica !== undefined)
1624
- publishChangeSet(changeSet(locator, replica, replica, { queue: true }));
1625
2445
  moveTo({ ...status, ...attention() });
1626
2446
  }
1627
2447
  async function keepUpload(local, locator, identifier, upload) {
@@ -1630,13 +2450,17 @@ export function composeRuntime(options) {
1630
2450
  await local.keepUpload(locator, identifier, upload);
1631
2451
  if (fences.has(locator))
1632
2452
  throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1633
- pendings.set(identifier, { ...pendings.get(identifier), upload });
2453
+ const key = pendingKey(locator, identifier);
2454
+ pendings.set(key, { ...pendings.get(key), upload });
1634
2455
  if (!continuing())
1635
2456
  throw new Error('connection refused');
1636
2457
  }
1637
2458
  async function readHistory(locator, path, query = {}) {
1638
2459
  if (closed || refusal !== null)
1639
2460
  return UNCERTAIN;
2461
+ await firstDiscovery;
2462
+ if (!roomsNamed)
2463
+ return UNCERTAIN;
1640
2464
  try {
1641
2465
  await ensureAuthorization();
1642
2466
  }
@@ -1663,7 +2487,7 @@ export function composeRuntime(options) {
1663
2487
  if (fencedSince(locator, since))
1664
2488
  return failure({ kind: 'room-not-readable', locator });
1665
2489
  if (result.kind === 'scoped-refusal') {
1666
- const room = replicas.get(result.locator)?.confirmed.record;
2490
+ const room = replicas.get(result.locator)?.record;
1667
2491
  if (room !== undefined)
1668
2492
  await scoped(result, room, await openLocal());
1669
2493
  }
@@ -1676,23 +2500,62 @@ export function composeRuntime(options) {
1676
2500
  return UNCERTAIN;
1677
2501
  }
1678
2502
  }
1679
- function queuedBlob(locator, path, localId) {
2503
+ function located(room, path, destination) {
2504
+ const crossing = roomTarget(destination);
2505
+ if (crossing === null) {
2506
+ const named = target(path, destination);
2507
+ return named === null || !rooms.has(room) ? null : { room, path: named };
2508
+ }
2509
+ const held = [...rooms.values()].find((listed) => listed.handle === crossing.handle);
2510
+ return held === undefined ? null : { room: held.locator, path: crossing.path };
2511
+ }
2512
+ async function resolveReference(room, path, destination) {
2513
+ if (roomTarget(destination) === null && target(path, destination) === null) {
2514
+ return ABSENT_TARGET;
2515
+ }
2516
+ const at = located(room, path, destination);
2517
+ if (at === null)
2518
+ return UNREADABLE;
2519
+ if (locked(at.room))
2520
+ return LOCKED_UNREADABLE;
2521
+ const held = replicas.get(at.room);
2522
+ if (held !== undefined &&
2523
+ (await (await openLocal()).entries(held.record.key, 'visible', [at.path])).length > 0) {
2524
+ return { kind: 'document', ...at };
2525
+ }
2526
+ const history = await readHistory(at.room, at.path);
2527
+ if (history.kind === 'no-history' || history.kind === 'revision')
2528
+ return ABSENT_TARGET;
2529
+ if (history.kind !== 'history')
2530
+ return history;
2531
+ const [last] = history.revisions;
2532
+ if (last === undefined)
2533
+ return ABSENT_TARGET;
2534
+ return history.deleted === true
2535
+ ? { kind: 'deleted', ...at, revision: last }
2536
+ : { kind: 'document', ...at };
2537
+ }
2538
+ function queuedFacts(locator, path, localId) {
1680
2539
  for (const batch of replicas.get(locator)?.queue ?? []) {
1681
2540
  const change = batch.changes[queuedFile(batch, path, localId)];
1682
2541
  if (change?.kind === 'binary')
1683
- return change.blob;
2542
+ return change;
1684
2543
  }
1685
- return null;
2544
+ const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
2545
+ return drafted?.localId === localId ? drafted : null;
1686
2546
  }
1687
- function binaryRoom(request) {
1688
- if (request.kind === 'path')
1689
- return request.locator;
1690
- const scope = options.adapter.referrerScope(request.referrer);
1691
- return scope?.kind === 'room' ? scope.locator : null;
2547
+ function batchFile(batch) {
2548
+ return async (localId) => {
2549
+ const change = batch.changes.find((held) => held.kind === 'binary' && held.localId === localId);
2550
+ return (await openLocal()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
2551
+ };
1692
2552
  }
1693
2553
  async function transferBinary(request, transfer, answer) {
1694
2554
  if (closed || refusal !== null)
1695
2555
  return UNCERTAIN;
2556
+ await firstDiscovery;
2557
+ if (!roomsNamed)
2558
+ return UNCERTAIN;
1696
2559
  try {
1697
2560
  await ensureAuthorization();
1698
2561
  }
@@ -1702,18 +2565,15 @@ export function composeRuntime(options) {
1702
2565
  if (closed || authorization === undefined || refusal !== null)
1703
2566
  return UNCERTAIN;
1704
2567
  const since = fencings;
1705
- journal.record(request.kind === 'path'
1706
- ? `transfer binary ${request.locator} ${request.path}`
1707
- : `transfer binary ${request.fileRef}`);
2568
+ journal.record(`transfer binary ${request.locator} ${request.path}`);
1708
2569
  try {
1709
2570
  const result = await transfer(authorization);
1710
2571
  const settled = result;
1711
2572
  journal.record(`outcome binary ${settled.kind}`);
1712
- const locator = binaryRoom(request);
1713
- if (locator !== null && fencedSince(locator, since))
1714
- return failure({ kind: 'room-not-readable', locator });
2573
+ if (fencedSince(request.locator, since))
2574
+ return failure({ kind: 'room-not-readable', locator: request.locator });
1715
2575
  if (settled.kind === 'scoped-refusal') {
1716
- const room = replicas.get(settled.locator)?.confirmed.record;
2576
+ const room = replicas.get(settled.locator)?.record;
1717
2577
  if (room !== undefined)
1718
2578
  await scoped(settled, room, await openLocal());
1719
2579
  }
@@ -1726,68 +2586,142 @@ export function composeRuntime(options) {
1726
2586
  return UNCERTAIN;
1727
2587
  }
1728
2588
  }
1729
- function readBinary(request) {
1730
- return transferBinary(request, (held) => options.adapter.binary(held, request), binaryAnswer);
2589
+ function fetchBinary(request, name) {
2590
+ return transferBinary(request, (authorization) => keptFiles.fetch(name, () => options.adapter.binary(authorization, request)), binaryAnswer);
1731
2591
  }
1732
- function binaryPath(locator, path, revision) {
2592
+ async function readBinary(request, name) {
2593
+ if (closed)
2594
+ return UNCERTAIN;
2595
+ const held = await keptFiles.find(name);
2596
+ if (held !== null)
2597
+ return held;
2598
+ return fetchBinary(request, name);
2599
+ }
2600
+ async function binaryPath(locator, path, revision) {
1733
2601
  if (revision !== undefined)
1734
2602
  return { kind: 'path', locator, path, revision };
1735
- const visible = replicas.get(locator)?.visible;
1736
- const held = visible?.binaries[path];
1737
- if (visible === undefined || held === undefined)
2603
+ const replica = replicas.get(locator);
2604
+ const local = await openLocal();
2605
+ const [held] = replica === undefined ? [] : await local.entries(replica.record.key, 'visible', [path]);
2606
+ const attached = held === undefined ? attachedFiles.get(locator)?.get(path) : undefined;
2607
+ if (attached !== undefined)
2608
+ return { kind: 'path', locator, path, revision: attached.revision };
2609
+ if (replica === undefined || held?.binary === undefined || held.binary === null)
1738
2610
  return null;
1739
- if (held.state === 'pending')
1740
- return { kind: 'pending', localId: held.localId };
1741
- const record = binaryRecord(path, held, visible.revisions[path]);
2611
+ if (held.binary.state === 'pending')
2612
+ return { kind: 'pending', localId: held.binary.localId };
2613
+ const record = binaryRecord(path, held.binary, held.revision ?? undefined);
1742
2614
  if (record.state !== 'accepted')
1743
2615
  return null;
1744
- const replica = replicas.get(locator);
1745
- const queued = (replica?.queue ?? []).flatMap((batch) => batch.changes);
2616
+ const queued = replica.queue.flatMap((batch) => batch.changes);
2617
+ const sources = queued.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
2618
+ const confirmedLayer = await stepped(slicing(await local.entries(replica.record.key, 'confirmed', [path, ...sources]), [
2619
+ path,
2620
+ ...sources,
2621
+ ]));
1746
2622
  const holds = (at) => {
1747
- const kept = replica?.confirmed.record.binaries[at];
2623
+ const kept = confirmedLayer.binaries[at];
1748
2624
  return (kept?.state === 'accepted' &&
1749
- kept.fileRef === record.fileRef &&
1750
- replica?.confirmed.record.revisions[at] === record.revision);
2625
+ kept.sha256 === record.sha256 &&
2626
+ confirmedLayer.revisions[at] === record.revision);
1751
2627
  };
1752
- let confirmed = path;
1753
- for (let index = queued.length - 1; index >= 0 && !holds(confirmed); index -= 1) {
2628
+ let confirmedAt = path;
2629
+ for (let index = queued.length - 1; index >= 0 && !holds(confirmedAt); index -= 1) {
1754
2630
  const change = queued[index];
1755
- if (change.kind === 'move' && change.to === confirmed)
1756
- confirmed = change.path;
2631
+ if (change.kind === 'move' && change.to === confirmedAt)
2632
+ confirmedAt = change.path;
1757
2633
  }
1758
2634
  return {
1759
2635
  kind: 'path',
1760
2636
  locator,
1761
- path: confirmed,
2637
+ path: confirmedAt,
1762
2638
  revision: record.revision,
1763
- fileRef: record.fileRef,
1764
2639
  };
1765
2640
  }
1766
- async function readBinaryPath(locator, path, revision) {
2641
+ async function askedBinary(locator, path, revision) {
1767
2642
  await ready;
1768
- const asked = binaryPath(locator, path, revision);
2643
+ const drafted = roomDrafts.get(locator)?.get(path)?.parts.file;
2644
+ return drafted !== undefined && drafted.localId === revision
2645
+ ? { kind: 'pending', localId: revision }
2646
+ : binaryPath(locator, path, revision);
2647
+ }
2648
+ function pendingBinary(locator, path, localId) {
2649
+ const facts = queuedFacts(locator, path, localId);
2650
+ return facts === null
2651
+ ? absentBinary(path)
2652
+ : {
2653
+ kind: 'binary',
2654
+ file: localId,
2655
+ byteLength: facts.byteLength,
2656
+ mediaType: facts.mediaType,
2657
+ };
2658
+ }
2659
+ async function readBinaryPath(locator, path, revision) {
2660
+ const asked = await askedBinary(locator, path, revision);
1769
2661
  if (asked === null)
1770
2662
  return absentBinary(path);
1771
- if (asked.kind === 'pending') {
1772
- const blob = queuedBlob(locator, path, asked.localId);
1773
- return blob === null ? absentBinary(path) : { kind: 'binary', blob };
1774
- }
1775
- return readBinary(asked);
2663
+ if (asked.kind === 'pending')
2664
+ return pendingBinary(locator, path, asked.localId);
2665
+ return readBinary(asked, { locator, path: asked.path, revision: asked.revision });
2666
+ }
2667
+ async function heldMediaType(locator, path) {
2668
+ const replica = replicas.get(locator);
2669
+ const [held] = replica === undefined
2670
+ ? []
2671
+ : await (await openLocal()).entries(replica.record.key, 'visible', [path]);
2672
+ return held === undefined
2673
+ ? attachedFiles.get(locator)?.get(path)?.mediaType
2674
+ : held.binary?.mediaType;
2675
+ }
2676
+ function linkOf(asked, held) {
2677
+ return transferBinary(asked, (authorization) => options.adapter.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
1776
2678
  }
1777
2679
  async function linkBinaryPath(locator, path, revision) {
1778
2680
  await ready;
1779
- const asked = binaryPath(locator, path, revision);
2681
+ const asked = await binaryPath(locator, path, revision);
1780
2682
  if (asked === null || asked.kind === 'pending')
1781
2683
  return absentBinary(path);
1782
- return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
2684
+ return linkOf(asked, revision === undefined ? await heldMediaType(locator, path) : undefined);
1783
2685
  }
1784
- async function readBinaryReference(reference) {
2686
+ async function showBinaryPath(locator, path, revision) {
2687
+ const asked = await askedBinary(locator, path, revision);
2688
+ if (asked === null)
2689
+ return absentBinary(path);
2690
+ if (asked.kind === 'pending')
2691
+ return pendingBinary(locator, path, asked.localId);
2692
+ if (closed)
2693
+ return UNCERTAIN;
2694
+ const name = { locator, path: asked.path, revision: asked.revision };
2695
+ const kept = await keptFiles.find(name);
2696
+ if (kept !== null)
2697
+ return kept;
2698
+ const mediaType = await heldMediaType(locator, path);
2699
+ if (mediaType?.startsWith('image/') === true)
2700
+ return fetchBinary(asked, name);
2701
+ return linkOf(asked, revision === undefined ? mediaType : undefined);
2702
+ }
2703
+ async function attachBinary(locator, file) {
1785
2704
  await ready;
1786
- return readBinary({
1787
- kind: 'reference',
1788
- fileRef: reference.fileRef,
1789
- referrer: reference.referrer,
1790
- });
2705
+ await firstDiscovery;
2706
+ if (closed || !roomsNamed || connectionOf() === 'unreachable' || refusal !== null)
2707
+ return OFFLINE;
2708
+ const request = await attachedFile(locator, file);
2709
+ return transferBinary({ locator, path: file.name }, async (held) => {
2710
+ const result = await options.adapter.attach(held, request);
2711
+ if (result.kind === 'attached') {
2712
+ const landed = attachedFiles.get(locator) ?? new Map();
2713
+ landed.set(result.path, { revision: result.revision, mediaType: request.mediaType });
2714
+ attachedFiles.set(locator, landed);
2715
+ }
2716
+ return result;
2717
+ }, attachAnswer);
2718
+ }
2719
+ function readPart(file, part) {
2720
+ return database.ask({ kind: 'file.part', key: file, part });
2721
+ }
2722
+ async function cleanUp() {
2723
+ const freed = await database.ask({ kind: 'opened.clean' });
2724
+ return freed + ((await options.shell.cleanUp?.()) ?? 0);
1791
2725
  }
1792
2726
  function declaredAgents() {
1793
2727
  for (const capability of capabilities) {
@@ -1805,6 +2739,13 @@ export function composeRuntime(options) {
1805
2739
  }
1806
2740
  return undefined;
1807
2741
  }
2742
+ function calledRooms(name, input) {
2743
+ const member = declaredAction(name)?.room;
2744
+ if (member === undefined || input === null || typeof input !== 'object')
2745
+ return [];
2746
+ const named = input[member];
2747
+ return (Array.isArray(named) ? named : [named]).filter((room) => typeof room === 'string');
2748
+ }
1808
2749
  async function invokeAction(name, input, dispatching) {
1809
2750
  await ready;
1810
2751
  if (closed || refusal !== null)
@@ -1824,7 +2765,7 @@ export function composeRuntime(options) {
1824
2765
  try {
1825
2766
  const result = await options.adapter.action(authorization, { name, input });
1826
2767
  if (result.kind === 'scoped-refusal') {
1827
- const room = replicas.get(result.locator)?.confirmed.record;
2768
+ const room = replicas.get(result.locator)?.record;
1828
2769
  if (room !== undefined)
1829
2770
  await scoped(result, room, await openLocal());
1830
2771
  }
@@ -1882,110 +2823,484 @@ export function composeRuntime(options) {
1882
2823
  await local.dropCall(held.operation);
1883
2824
  continue;
1884
2825
  }
1885
- calling.add(held.operation);
1886
- try {
1887
- if (unsettled(await invokeAction(held.name, held.input)))
1888
- return 'again';
1889
- await local.dropCall(held.operation);
1890
- }
1891
- finally {
1892
- calling.delete(held.operation);
1893
- }
2826
+ calling.add(held.operation);
2827
+ try {
2828
+ if (unsettled(await invokeAction(held.name, held.input)))
2829
+ return 'again';
2830
+ await local.dropCall(held.operation);
2831
+ }
2832
+ finally {
2833
+ calling.delete(held.operation);
2834
+ }
2835
+ }
2836
+ return 'done';
2837
+ }
2838
+ function continuing() {
2839
+ return !closed && !discoveryPaused;
2840
+ }
2841
+ function reachedBy(locator, roomVersion) {
2842
+ const held = replicas.get(locator);
2843
+ return (held !== undefined && options.adapter.reached(locator, held.record.roomVersion, roomVersion));
2844
+ }
2845
+ async function writtenAfter(locator, roomVersion, path) {
2846
+ const held = replicas.get(locator);
2847
+ if (held === undefined)
2848
+ return false;
2849
+ const [entry] = await (await openLocal()).entries(held.record.key, 'confirmed', [path]);
2850
+ const written = entry?.written ?? undefined;
2851
+ return (written !== undefined &&
2852
+ options.adapter.reached(locator, written, roomVersion) &&
2853
+ !options.adapter.reached(locator, roomVersion, written));
2854
+ }
2855
+ function overriddenTexts(texts, overrides) {
2856
+ return async (paths) => {
2857
+ const known = new Map(await texts(paths.filter((path) => !overrides.has(path))));
2858
+ for (const path of paths) {
2859
+ const entry = overrides.get(path);
2860
+ if (entry === undefined)
2861
+ continue;
2862
+ if (entry === null)
2863
+ known.delete(path);
2864
+ else
2865
+ known.set(path, entry.binary !== null ? null : entry.content);
2866
+ }
2867
+ return known;
2868
+ };
2869
+ }
2870
+ async function unreadableRoom(texts, walk, paths) {
2871
+ if (paths !== null) {
2872
+ const text = {};
2873
+ for (const [path, content] of await texts(paths))
2874
+ if (content !== null)
2875
+ text[path] = content;
2876
+ return stepped(unreadableIn(text, Object.keys(text)));
2877
+ }
2878
+ for (let after = null;;) {
2879
+ const page = await walk(after);
2880
+ const text = {};
2881
+ for (const { path, content } of page.entries)
2882
+ if (content !== null)
2883
+ text[path] = content;
2884
+ const refused = await stepped(unreadableIn(text, Object.keys(text)));
2885
+ if (refused !== null)
2886
+ return refused;
2887
+ if (page.next === null)
2888
+ return null;
2889
+ after = page.next;
2890
+ }
2891
+ }
2892
+ async function validatedRoom(local, record, view, previous, overrides = null) {
2893
+ const composition = composed(record);
2894
+ const modeled = record.model.status !== 'unavailable' || record.typedSelection !== null;
2895
+ const lookup = overrides === null
2896
+ ? lookupOf(local, record.key, view)
2897
+ : overriddenLookup(lookupOf(local, record.key, view), overrides);
2898
+ const texts = overrides === null
2899
+ ? textsOf(local, record.key, view)
2900
+ : overriddenTexts(textsOf(local, record.key, view), overrides);
2901
+ const walk = overrides === null
2902
+ ? walkOf(local, record.key, view)
2903
+ : overriddenWalk(walkOf(local, record.key, view), overrides);
2904
+ let changes = null;
2905
+ let reshaped = false;
2906
+ if (previous !== null && previous.changed.length <= CLEAN_CHANGES) {
2907
+ const [was, now] = await Promise.all([
2908
+ local.entries(record.key, 'confirmed', previous.changed),
2909
+ overrides === null
2910
+ ? local.entries(record.key, view, previous.changed)
2911
+ : Promise.resolve([]),
2912
+ ]);
2913
+ const before = new Map(was.map((entry) => [entry.path, entry]));
2914
+ const after = overrides === null
2915
+ ? new Map(now.map((entry) => [entry.path, entry]))
2916
+ : overrides;
2917
+ changes = previous.changed.flatMap((path) => {
2918
+ const one = before.get(path) ?? null;
2919
+ const other = after.get(path) ?? null;
2920
+ reshaped ||= textDiffers(one, other);
2921
+ const held = textOf(one);
2922
+ const next = textOf(other);
2923
+ return held === next ? [] : [{ path, was: held, now: next }];
2924
+ });
2925
+ }
2926
+ const named = changes === null ? null : changes.map(({ path }) => path);
2927
+ const textChanged = changes === null || changes.length > 0 || reshaped;
2928
+ const modelsChanged = previous === null || !sameComposition(composed(previous.record), composition);
2929
+ if (modeled && (textChanged || modelsChanged)) {
2930
+ const clean = previous !== null &&
2931
+ changes !== null &&
2932
+ (await changedCleanly(changes, composed(previous.record), composition, {
2933
+ lookup,
2934
+ texts,
2935
+ referrers: referrersOf(local, record.key, 'confirmed'),
2936
+ }));
2937
+ if (clean)
2938
+ return null;
2939
+ const verdict = await validatedCollection(walk, lookup, composition);
2940
+ if (verdict.verdict === 'accept')
2941
+ return null;
2942
+ if (verdict.rule !== 'model-unavailable')
2943
+ return { kind: 'refused', refusal: verdict };
2944
+ return unreadableRoom(texts, walk, named);
2945
+ }
2946
+ if (textChanged)
2947
+ return unreadableRoom(texts, walk, named);
2948
+ return null;
2949
+ }
2950
+ function refreshingOf(local, key, composition, liveness, replaying = null) {
2951
+ const write = replaying === null ? exclusive : installWrite;
2952
+ return {
2953
+ async turn() {
2954
+ if (replaying === null)
2955
+ return;
2956
+ installing(replaying);
2957
+ await screened();
2958
+ },
2959
+ prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
2960
+ async stage(page) {
2961
+ for (let written = 0;;) {
2962
+ if (replaying !== null)
2963
+ await screened();
2964
+ const end = batchEnd(page.entries, written);
2965
+ const last = end === page.entries.length;
2966
+ const entries = page.entries.slice(written, end);
2967
+ const staged = await write(async () => {
2968
+ if (!liveness())
2969
+ return false;
2970
+ await timed(stageBudget, () => local.refresh(key, composition, last
2971
+ ? { ...page, entries }
2972
+ : { entries, removals: [], folders: [], folderRemovals: [] }));
2973
+ return true;
2974
+ });
2975
+ if (!staged)
2976
+ return false;
2977
+ if (last)
2978
+ return true;
2979
+ written = end;
2980
+ }
2981
+ },
2982
+ };
2983
+ }
2984
+ function batchEnd(entries, from) {
2985
+ let end = from;
2986
+ let spent = 0;
2987
+ while (end < entries.length) {
2988
+ const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
2989
+ if (spent + size > stageBudget.bytes && end > from)
2990
+ break;
2991
+ spent += size;
2992
+ end += 1;
2993
+ }
2994
+ return end;
2995
+ }
2996
+ async function timed(budget, work) {
2997
+ const started = performance.now();
2998
+ const done = await work();
2999
+ const spent = performance.now() - started;
3000
+ if (spent < budget.aim / 2)
3001
+ budget.bytes = Math.min(budget.ceiling, Math.round(budget.bytes * 1.5));
3002
+ else if (spent > budget.aim * 2) {
3003
+ budget.bytes = Math.max(budget.floor, Math.round(budget.bytes / 1.5));
3004
+ }
3005
+ return done;
3006
+ }
3007
+ function installWrite(work) {
3008
+ const next = installWrites.then(() => exclusive(work), () => exclusive(work));
3009
+ installWrites = next.then(() => undefined, () => undefined);
3010
+ return next;
3011
+ }
3012
+ function screened() {
3013
+ if (onScreen || closed)
3014
+ return Promise.resolve();
3015
+ queueReadiness();
3016
+ return new Promise((resolve) => shownWaiters.push(resolve));
3017
+ }
3018
+ async function heldPart() {
3019
+ while (partsFree === 0 || queuedBytes >= QUEUED_BYTES) {
3020
+ await new Promise((admit) => partWaiters.push(admit));
3021
+ }
3022
+ partsFree -= 1;
3023
+ return once(() => {
3024
+ partsFree += 1;
3025
+ });
3026
+ }
3027
+ function once(release) {
3028
+ let released = false;
3029
+ return () => {
3030
+ if (released)
3031
+ return;
3032
+ released = true;
3033
+ release();
3034
+ for (const admit of partWaiters.splice(0))
3035
+ admit();
3036
+ };
3037
+ }
3038
+ function installing(locator, inventory = null) {
3039
+ installs.set(locator, inventory ?? installs.get(locator) ?? null);
3040
+ queueReadiness();
3041
+ }
3042
+ function untransferred(locator) {
3043
+ transfers.delete(locator);
3044
+ keptParts.delete(locator);
3045
+ underived.delete(locator);
3046
+ unindexed.delete(locator);
3047
+ }
3048
+ function owedIndexes(locator, bytes, text) {
3049
+ if (bytes <= 0 || text <= 0)
3050
+ return;
3051
+ unindexed.set(locator, [...(unindexed.get(locator) ?? []), { bytes, text }]);
3052
+ }
3053
+ function built(locator, { done, text }) {
3054
+ const owed = unindexed.get(locator) ?? [];
3055
+ let left = text;
3056
+ while (!done && left > 0 && owed.length > 0) {
3057
+ const [first] = owed;
3058
+ if (left >= first.text) {
3059
+ left -= first.text;
3060
+ owed.shift();
3061
+ continue;
3062
+ }
3063
+ owed[0] = {
3064
+ bytes: (first.bytes * (first.text - left)) / first.text,
3065
+ text: first.text - left,
3066
+ };
3067
+ left = 0;
1894
3068
  }
1895
- return 'done';
1896
- }
1897
- function continuing() {
1898
- return !closed && !discoveryPaused;
3069
+ if (done || owed.length === 0)
3070
+ unindexed.delete(locator);
3071
+ queueReadiness();
1899
3072
  }
1900
- function reachedBy(locator, roomVersion) {
1901
- const held = replicas.get(locator);
1902
- return (held !== undefined &&
1903
- options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion));
3073
+ function installed(locator) {
3074
+ if (installs.delete(locator))
3075
+ queueReadiness();
1904
3076
  }
1905
- function writtenAfter(locator, roomVersion, path) {
1906
- const written = replicas.get(locator)?.confirmed.record.written[path];
1907
- return (written !== undefined &&
1908
- options.adapter.reached(locator, written, roomVersion) &&
1909
- !options.adapter.reached(locator, roomVersion, written));
3077
+ function screen(value) {
3078
+ if (value === onScreen)
3079
+ return;
3080
+ onScreen = value;
3081
+ journal.record(`installation ${value ? 'shown' : 'hidden'}`);
3082
+ queueReadiness();
3083
+ if (!value)
3084
+ return;
3085
+ for (const resolve of shownWaiters.splice(0))
3086
+ resolve();
1910
3087
  }
1911
- async function pullRefresh(authorized, confirmed, live) {
1912
- if (!live())
3088
+ async function pullRefresh(authorized, held, liveness) {
3089
+ if (!liveness())
1913
3090
  return unconfirmed;
1914
3091
  const turn = generation;
3092
+ const { record } = held;
3093
+ const base = held.confirmed;
3094
+ const local = await openLocal();
1915
3095
  try {
1916
- const result = await refreshRoom(reads, authorized, confirmed, (models, held) => (live() ? remodel(confirmed.record.locator, models, held) : held), live);
1917
- const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
1918
- journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
1919
- return result;
3096
+ await exclusive(() => local.unrefresh(record.key));
3097
+ const result = await advancement(reads, authorized, record, (models, kept) => (liveness() ? remodel(record.locator, models, kept) : kept), liveness, refreshingOf(local, record.key, composed(record), liveness));
3098
+ if (result.kind !== 'advanced') {
3099
+ journal.record(`transfer refresh ${record.locator} ${result.kind}`);
3100
+ return result;
3101
+ }
3102
+ const refused = await validatedRoom(local, result.record, 'refreshed', {
3103
+ record,
3104
+ changed: [...result.changed],
3105
+ });
3106
+ if (refused !== null) {
3107
+ journal.record(`transfer refresh ${record.locator} refused`);
3108
+ return refused;
3109
+ }
3110
+ journal.record(`transfer refresh ${record.locator} ${result.record.roomVersion}`);
3111
+ return { kind: 'advanced', record: result.record, base };
1920
3112
  }
1921
3113
  catch (error) {
1922
- journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
3114
+ if (refusedForSpace(error))
3115
+ return unconfirmed;
3116
+ journal.record(`transfer refresh ${record.locator} transport-uncertainty`);
1923
3117
  throw error;
1924
3118
  }
1925
3119
  finally {
1926
- reads.finished(confirmed.record.locator, turn);
3120
+ reads.finished(record.locator, turn);
3121
+ }
3122
+ }
3123
+ const heldContents = new Map();
3124
+ function heldContentOf(local, key) {
3125
+ const known = heldContents.get(key);
3126
+ if (known !== undefined)
3127
+ return known;
3128
+ if (heldContents.size > ROOM_READS)
3129
+ heldContents.clear();
3130
+ const made = {
3131
+ files: (after, limit) => local.files(key, 'confirmed', after, limit),
3132
+ async contents(paths) {
3133
+ return new Map((await local.entries(key, 'confirmed', paths)).flatMap(({ path, content, revision }) => content === null || revision === null ? [] : [[path, { content, revision }]]));
3134
+ },
3135
+ };
3136
+ heldContents.set(key, made);
3137
+ return made;
3138
+ }
3139
+ async function keptTogether({ local, parts }) {
3140
+ const live = parts.filter((one) => one.live() && transfers.has(one.locator));
3141
+ await local.keepParts(live.map(({ locator, token, part }) => ({ locator, token, part })));
3142
+ for (const { locator, token, part } of live) {
3143
+ const held = keptParts.get(locator) ?? new Map();
3144
+ keptParts.set(locator, held.set(token, part.byteLength));
3145
+ arrivals.get(locator)?.delete(token);
1927
3146
  }
3147
+ queueReadiness();
1928
3148
  }
1929
- function staging(local, live) {
1930
- return {
1931
- held: (locator) => transfers.get(locator) ?? null,
1932
- staged: (record) => local.staged(record),
1933
- stage: (record, page) => exclusive(async () => {
1934
- if (!live())
3149
+ function staging(local, liveness) {
3150
+ const derived = async (locator, key, composition) => {
3151
+ underived.delete(locator);
3152
+ for (;;) {
3153
+ await screened();
3154
+ if (!liveness())
1935
3155
  return false;
1936
- const bytes = layerBytes(page);
1937
- if (!(await admitted(local, record.locator, bytes)))
3156
+ const done = await installWrite(async () => liveness()
3157
+ ? timed(deriveBudget, () => local.rederive(key, composition, deriveBudget.bytes, DERIVE_LONGEST_MS, DERIVE_FLOOR))
3158
+ : null);
3159
+ if (done === null)
1938
3160
  return false;
1939
- try {
1940
- await local.stage(record, page);
3161
+ built(locator, done);
3162
+ if (done.done)
3163
+ return true;
3164
+ }
3165
+ };
3166
+ return {
3167
+ held: (locator) => transfers.get(locator) ?? null,
3168
+ async turn(locator, step) {
3169
+ installing(locator);
3170
+ for (;;) {
3171
+ await screened();
3172
+ const release = step === 'downloading' ? await heldPart() : () => { };
3173
+ if (onScreen || closed)
3174
+ return release;
3175
+ release();
1941
3176
  }
1942
- catch (error) {
1943
- if (!exceeded(error))
1944
- throw error;
1945
- await exceededOn(record.locator, bytes);
1946
- return false;
3177
+ },
3178
+ queued(bytes) {
3179
+ queuedBytes += bytes;
3180
+ return once(() => {
3181
+ queuedBytes -= bytes;
3182
+ });
3183
+ },
3184
+ arriving(locator) {
3185
+ let ended = false;
3186
+ return {
3187
+ at(token, bytes) {
3188
+ if (ended || keptParts.get(locator)?.has(token) === true)
3189
+ return;
3190
+ const held = arrivals.get(locator) ?? new Map();
3191
+ arrivals.set(locator, held.set(token, bytes));
3192
+ const now = performance.now();
3193
+ if (now - arrivalReported < ARRIVAL_REPORT_MS)
3194
+ return;
3195
+ arrivalReported = now;
3196
+ queueReadiness();
3197
+ },
3198
+ end() {
3199
+ ended = true;
3200
+ if (arrivals.delete(locator))
3201
+ queueReadiness();
3202
+ },
3203
+ };
3204
+ },
3205
+ received: (locator, token) => local.transferPart(locator, token),
3206
+ keep: (locator, token, received) => gathered('kept parts', { local, parts: [{ locator, token, part: received, live: liveness }] }, (earlier, later) => earlier.local === later.local
3207
+ ? { local: earlier.local, parts: [...earlier.parts, ...later.parts] }
3208
+ : null, keptTogether),
3209
+ announced(locator) {
3210
+ const later = announced.get(locator);
3211
+ return later === undefined ? [] : [later];
3212
+ },
3213
+ async stage(record, page, composition, written) {
3214
+ const { locator } = record;
3215
+ installing(locator, record.inventory);
3216
+ await screened();
3217
+ if (!liveness())
3218
+ return null;
3219
+ const key = await installWrite(async () => {
3220
+ if (!liveness())
3221
+ return null;
3222
+ return local.stage(record, composition, page, written ?? undefined);
3223
+ });
3224
+ if (key === null || !liveness())
3225
+ return null;
3226
+ const arrived = !transfers.has(locator);
3227
+ const prior = transfers.get(locator)?.done.bytes ?? 0;
3228
+ const held = { ...record, key };
3229
+ transfers.set(locator, held);
3230
+ if (written !== null) {
3231
+ keptParts.get(locator)?.delete(written);
3232
+ arrivals.get(locator)?.delete(written);
1947
3233
  }
1948
- transfers.set(record.locator, record);
1949
- readSide.recover();
1950
- return true;
1951
- }),
3234
+ if (arrived)
3235
+ liveQueries.recover();
3236
+ installing(locator);
3237
+ liveQueries.recover();
3238
+ const text = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), 0);
3239
+ owedIndexes(locator, record.done.bytes - prior, text);
3240
+ const owed = text + (underived.get(locator) ?? 0);
3241
+ underived.set(locator, owed);
3242
+ if (owed < deriveBudget.bytes)
3243
+ return held;
3244
+ return (await derived(locator, key, composition)) ? held : null;
3245
+ },
3246
+ derive: derived,
3247
+ async refreshing(locator, key, composition) {
3248
+ await exclusive(() => local.unrefresh(key));
3249
+ return refreshingOf(local, key, composition, liveness, locator);
3250
+ },
1952
3251
  abandon: (locator) => exclusive(async () => {
1953
- if (!live())
3252
+ if (!liveness())
1954
3253
  return;
1955
3254
  await local.abandon(locator);
1956
- transfers.delete(locator);
1957
- readSide.recover();
3255
+ untransferred(locator);
3256
+ liveQueries.recover();
3257
+ sweep();
3258
+ }),
3259
+ setAside: (locator) => exclusive(async () => {
3260
+ if (!liveness())
3261
+ return;
3262
+ await local.abandon(locator, true);
3263
+ untransferred(locator);
3264
+ liveQueries.recover();
3265
+ }),
3266
+ release: (key) => exclusive(async () => {
3267
+ await local.release(key);
3268
+ sweep();
1958
3269
  }),
3270
+ content: (key) => heldContentOf(local, key),
3271
+ validate: (record, view) => validatedRoom(local, record, view, null),
1959
3272
  };
1960
3273
  }
1961
- function reusable(room) {
1962
- const record = replicas.get(room.locator)?.confirmed.record;
3274
+ function reusable(room, local) {
3275
+ const record = replicas.get(room.locator)?.record;
1963
3276
  if (record === undefined || record.replica.selection !== room.replica.selection)
1964
3277
  return null;
1965
- return { documents: record.documents, revisions: record.revisions };
3278
+ return heldContentOf(local, record.key);
1966
3279
  }
1967
- async function pullBootstrap(authorized, room, local, live) {
1968
- if (!live())
3280
+ async function pullBootstrap(authorized, room, local, liveness) {
3281
+ if (!liveness())
1969
3282
  return unconfirmed;
1970
3283
  const turn = generation;
1971
3284
  if (replicas.has(room.locator)) {
1972
3285
  catching.add(room.locator);
1973
- readSide.recover();
3286
+ liveQueries.recover();
1974
3287
  }
1975
3288
  try {
1976
- const result = await bootstrapRoom(reads, authorized, room, {
1977
- collection: modelOf(room.locator),
1978
- selection: modelOf(room.locator, 'typed-selection'),
1979
- }, (models, held) => (live() ? remodel(room.locator, models, held) : held), live, staging(local, live), reusable(room));
1980
- const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
3289
+ const result = await bootstrapRoom(reads, authorized, room, { collection: modelOf(room.locator) }, builtIn, (models, held) => (liveness() ? remodel(room.locator, models, held) : held), liveness, staging(local, liveness), reusable(room, local));
3290
+ const detail = result.kind === 'staged' ? result.record.roomVersion : result.kind;
1981
3291
  journal.record(`transfer bootstrap ${room.locator} ${detail}`);
1982
- if (result.kind !== 'staged')
3292
+ if (result.kind !== 'staged') {
1983
3293
  caughtUpTo(room.locator);
3294
+ installed(room.locator);
3295
+ }
1984
3296
  return result;
1985
3297
  }
1986
3298
  catch (error) {
1987
- journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
1988
3299
  caughtUpTo(room.locator);
3300
+ installed(room.locator);
3301
+ if (refusedForSpace(error))
3302
+ return unconfirmed;
3303
+ journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
1989
3304
  throw error;
1990
3305
  }
1991
3306
  finally {
@@ -1994,10 +3309,10 @@ export function composeRuntime(options) {
1994
3309
  }
1995
3310
  function caughtUpTo(locator) {
1996
3311
  if (catching.delete(locator))
1997
- readSide.recover();
3312
+ liveQueries.recover();
1998
3313
  }
1999
3314
  function behind(room) {
2000
- const held = replicas.get(room.locator)?.confirmed.record.roomVersion;
3315
+ const held = replicas.get(room.locator)?.record.roomVersion;
2001
3316
  return (held !== undefined &&
2002
3317
  [announced.get(room.locator), room.roomVersion].some((later) => later !== undefined && !options.adapter.reached(room.locator, held, later)));
2003
3318
  }
@@ -2009,13 +3324,13 @@ export function composeRuntime(options) {
2009
3324
  }
2010
3325
  return advanceOnce(room, local, authorized, live);
2011
3326
  }
2012
- async function advanceOnce(room, local, authorized, live) {
2013
- await referenced(room.locator);
2014
- if (!live() || paused.has(room.locator))
3327
+ async function advanceOnce(room, local, authorized, running) {
3328
+ const live = () => running() && !full;
3329
+ if (!live())
2015
3330
  return unconfirmed;
2016
3331
  const held = replicas.get(room.locator);
2017
3332
  if (held !== undefined) {
2018
- const refreshed = await pullRefresh(authorized, held.confirmed, live);
3333
+ const refreshed = await pullRefresh(authorized, held, live);
2019
3334
  if (!live())
2020
3335
  return unconfirmed;
2021
3336
  if (refreshed.kind === 'still-current') {
@@ -2030,32 +3345,32 @@ export function composeRuntime(options) {
2030
3345
  return null;
2031
3346
  }
2032
3347
  if (refreshed.kind === 'advanced') {
2033
- if (!(await publish(local, room, refreshed.confirmed, { base: held.confirmed }, live))) {
2034
- const now = replicas.get(room.locator)?.confirmed.record.roomVersion;
3348
+ const landing = { kind: 'refreshed', base: refreshed.base };
3349
+ if (!(await publish(local, room, refreshed.record, landing, live))) {
3350
+ const now = replicas.get(room.locator)?.record.roomVersion;
2035
3351
  const reached = now !== undefined &&
2036
- options.adapter.reached(room.locator, now, refreshed.confirmed.record.roomVersion);
3352
+ options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
2037
3353
  return reached ? null : advance(room, local, authorized, live);
2038
3354
  }
2039
- await retainModel(local, room, 'collection', modelOf(room.locator));
3355
+ await retainModel(local, room, modelOf(room.locator));
2040
3356
  return null;
2041
3357
  }
2042
3358
  if (refreshed.kind !== 'beyond-horizon')
2043
3359
  return refreshed;
2044
3360
  if (opened !== null) {
2045
3361
  owed.add(room.locator);
2046
- readSide.recover();
3362
+ liveQueries.recover();
2047
3363
  return unconfirmed;
2048
3364
  }
2049
3365
  if (reloadsDue.get(room.locator) === generation)
2050
3366
  return unconfirmed;
2051
3367
  }
2052
3368
  const bootstrapped = await pullBootstrap(authorized, room, local, live);
2053
- if (!live())
2054
- return unconfirmed;
2055
- if (bootstrapped.kind !== 'staged')
2056
- return bootstrapped;
2057
- await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
2058
- return null;
3369
+ if (bootstrapped.kind === 'staged') {
3370
+ await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
3371
+ return live() ? null : unconfirmed;
3372
+ }
3373
+ return live() ? bootstrapped : unconfirmed;
2059
3374
  }
2060
3375
  async function withhold(withheld, room, local, progress) {
2061
3376
  if (withheld.kind === 'unconfirmed')
@@ -2127,57 +3442,254 @@ export function composeRuntime(options) {
2127
3442
  (accepted.author === undefined ? '' : ` author ${accepted.author}`) +
2128
3443
  (accepted.time === undefined ? '' : ` time ${accepted.time}`));
2129
3444
  }
2130
- async function adoptAccepted(room, local, submitted, accepted) {
2131
- await referenced(room.locator);
3445
+ function settling(acceptance) {
3446
+ return acceptance !== null && 'apply' in acceptance;
3447
+ }
3448
+ function refusalOf(acceptance) {
3449
+ return settling(acceptance) ? (acceptance.refused ?? null) : acceptance;
3450
+ }
3451
+ async function acceptance(room, local, submitted, accepted) {
2132
3452
  if (!continuing())
2133
3453
  return unconfirmed;
2134
3454
  const current = replicas.get(room.locator);
2135
3455
  if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
2136
3456
  return null;
2137
3457
  }
2138
- const held = current.confirmed.record.roomVersion;
3458
+ const held = current.record.roomVersion;
2139
3459
  logAccepted(room.locator, submitted, accepted);
2140
- const applied = options.adapter.reached(room.locator, held, accepted.roomVersion)
2141
- ? { kind: 'staged', confirmed: current.confirmed }
2142
- : await applyReturned(current.confirmed, submitted, accepted, options.adapter.follows(room.locator, held, accepted.roomVersion)
2143
- ? accepted.roomVersion
2144
- : held, accepted.roomVersion);
2145
- if (applied.kind !== 'staged') {
2146
- const state = pendings.get(submitted.id);
2147
- await local.accept(room.locator, submitted.id, accepted.roomVersion);
2148
- if (state !== undefined && !fences.has(room.locator))
2149
- pendings.set(submitted.id, { ...state, accepted: accepted.roomVersion });
2150
- emitQueue(room.locator);
2151
- return applied;
2152
- }
2153
- const settled = await settle(local, room.locator, current, applied.confirmed, current.queue.filter((batch) => batch.id !== submitted.id), { room: true });
2154
- pendings.delete(submitted.id);
2155
- unreadable.delete(room.locator);
2156
- sourceRefusals.delete(room.locator);
2157
- declarationRefusals.delete(room.locator);
2158
- const set = settled();
2159
- list(room, true);
2160
- publishChangeSet(set);
2161
- moveTo({ ...status, ...attention() });
2162
- return null;
3460
+ let record = current.record;
3461
+ let changes;
3462
+ if (!options.adapter.reached(room.locator, held, accepted.roomVersion)) {
3463
+ const paths = [...returnedPaths(submitted, accepted)];
3464
+ const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
3465
+ const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion));
3466
+ record = {
3467
+ ...record,
3468
+ roomVersion: options.adapter.follows(room.locator, held, accepted.roomVersion)
3469
+ ? accepted.roomVersion
3470
+ : held,
3471
+ };
3472
+ const overrides = new Map([
3473
+ ...back.set.map((entry) => [entry.path, entry]),
3474
+ ...back.removed.map((path) => [path, null]),
3475
+ ]);
3476
+ const refused = await validatedRoom(local, record, 'confirmed', { record: current.record, changed: [...overrides.keys()] }, overrides);
3477
+ if (refused !== null) {
3478
+ const key = pendingKey(room.locator, submitted.id);
3479
+ return {
3480
+ locator: room.locator,
3481
+ left: [],
3482
+ commit: {
3483
+ rooms: [],
3484
+ writes: [
3485
+ {
3486
+ locator: room.locator,
3487
+ accepted: [{ id: submitted.id, roomVersion: accepted.roomVersion }],
3488
+ },
3489
+ ],
3490
+ },
3491
+ refused,
3492
+ apply() {
3493
+ const state = pendings.get(key);
3494
+ if (state !== undefined && !fences.has(room.locator))
3495
+ pendings.set(key, { ...state, accepted: accepted.roomVersion });
3496
+ return changeSet(room.locator, { queue: true });
3497
+ },
3498
+ };
3499
+ }
3500
+ changes = back;
3501
+ }
3502
+ const settled = await settlement(local, room.locator, current, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
3503
+ return {
3504
+ ...settled,
3505
+ apply(answer) {
3506
+ const set = settled.apply(answer);
3507
+ pendings.delete(pendingKey(room.locator, submitted.id));
3508
+ unreadable.delete(room.locator);
3509
+ sourceRefusals.delete(room.locator);
3510
+ declarationRefusals.delete(room.locator);
3511
+ list(room, true);
3512
+ return set;
3513
+ },
3514
+ };
3515
+ }
3516
+ async function adoptedTogether(local, acceptances) {
3517
+ const settlements = acceptances.filter(settling);
3518
+ if (settlements.length > 0) {
3519
+ publishChangeSet(await together(local, settlements));
3520
+ moveTo({ ...status, ...attention() });
3521
+ }
3522
+ await unpair(local);
2163
3523
  }
2164
3524
  async function adopted(room, local, submitted, accepted) {
2165
- const failed = await exclusive(() => adoptAccepted(room, local, submitted, accepted));
3525
+ const failed = await exclusive(async () => {
3526
+ const adopting = await acceptance(room, local, submitted, accepted);
3527
+ await adoptedTogether(local, [adopting]);
3528
+ return refusalOf(adopting);
3529
+ });
3530
+ if (failed === null)
3531
+ return true;
3532
+ if (failed.kind === 'refused' && continuing())
3533
+ refusedSource(room, failed);
3534
+ return false;
3535
+ }
3536
+ async function adoptedMove(room, partner, local, submitted, accepted) {
3537
+ const other = discovered.get(partner);
3538
+ const failed = await exclusive(async () => {
3539
+ const left = await acceptance(room, local, submitted, accepted);
3540
+ const arriving = replicas.get(partner)?.queue.find((batch) => batch.id === submitted.id);
3541
+ const arrived = other === undefined || arriving === undefined || accepted.destination === undefined
3542
+ ? null
3543
+ : await acceptance(other, local, arriving, {
3544
+ kind: 'accepted',
3545
+ ...accepted.destination,
3546
+ });
3547
+ await adoptedTogether(local, [left, arrived]);
3548
+ const refusedArrival = refusalOf(arrived);
3549
+ if (other !== undefined && refusedArrival?.kind === 'refused' && continuing()) {
3550
+ refusedSource(other, refusedArrival);
3551
+ }
3552
+ return refusalOf(left);
3553
+ });
3554
+ submitLater(partner);
2166
3555
  if (failed === null)
2167
3556
  return true;
2168
3557
  if (failed.kind === 'refused' && continuing())
2169
3558
  refusedSource(room, failed);
2170
3559
  return false;
2171
3560
  }
3561
+ function queueIn(locator) {
3562
+ return replicas.get(locator)?.queue ?? queuesWithoutReplica.get(locator) ?? [];
3563
+ }
3564
+ function behindIn(locator, batch) {
3565
+ const held = replicas.get(locator)?.queue;
3566
+ if (held === undefined)
3567
+ return queuesWithoutReplica.get(locator) !== undefined;
3568
+ return held.findIndex((one) => one.id === batch.id) > 0;
3569
+ }
3570
+ function unpaired(skipped) {
3571
+ for (const locator of [...replicas.keys(), ...queuesWithoutReplica.keys()]) {
3572
+ for (const batch of queueIn(locator)) {
3573
+ const partner = partnerOf(batch, locator);
3574
+ if (partner === null || skipped.has(pendingKey(locator, batch.id)))
3575
+ continue;
3576
+ if (queueIn(partner).some((held) => held.id === batch.id))
3577
+ continue;
3578
+ if (unpairedIn(locator, batch))
3579
+ return { locator, batch };
3580
+ }
3581
+ }
3582
+ return null;
3583
+ }
3584
+ async function unpairing(local, locator, batches) {
3585
+ const ids = new Set(batches.map(({ id }) => id));
3586
+ const leaving = batches.map((batch) => ({
3587
+ batch,
3588
+ returned: !localSaves && leavingIn(batch, locator) ? returnedBy('withdrawn') : null,
3589
+ }));
3590
+ const outcome = localSaves ? 'dropped' : 'unpaired';
3591
+ const current = replicas.get(locator);
3592
+ if (current !== undefined) {
3593
+ const settled = await settlement(local, locator, current, current.record, current.queue.filter((one) => !ids.has(one.id)), { leaving });
3594
+ return {
3595
+ ...settled,
3596
+ apply(answer) {
3597
+ for (const batch of batches)
3598
+ journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
3599
+ const set = settled.apply(answer);
3600
+ submitLater(locator);
3601
+ return set;
3602
+ },
3603
+ };
3604
+ }
3605
+ const queue = queueIn(locator).filter((one) => !ids.has(one.id));
3606
+ const written = new Map();
3607
+ for (const { batch, returned } of leaving) {
3608
+ if (returned === null)
3609
+ continue;
3610
+ for (const saved of pendings.get(pendingKey(locator, batch.id))?.drafts ?? []) {
3611
+ written.set(saved.path, {
3612
+ locator,
3613
+ path: saved.path,
3614
+ revision: roomDraftRevision(),
3615
+ parts: returnedInto((written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path))?.parts, saved.parts),
3616
+ returned,
3617
+ });
3618
+ }
3619
+ }
3620
+ const carried = carriedBack(locator, leaving.flatMap(({ batch, returned }) => (returned === null ? [] : [batch])), written);
3621
+ return {
3622
+ locator,
3623
+ left: batches,
3624
+ commit: {
3625
+ rooms: [],
3626
+ writes: [
3627
+ {
3628
+ locator,
3629
+ queue,
3630
+ roomDrafts: [...written.values()],
3631
+ consumedRoomDrafts: carried.taken,
3632
+ },
3633
+ ...carried.writes,
3634
+ ],
3635
+ },
3636
+ apply() {
3637
+ for (const batch of batches) {
3638
+ journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
3639
+ pendings.delete(pendingKey(locator, batch.id));
3640
+ }
3641
+ if (queue.length > 0)
3642
+ queuesWithoutReplica.set(locator, queue);
3643
+ else
3644
+ queuesWithoutReplica.delete(locator);
3645
+ for (const record of written.values())
3646
+ rememberRoomDraft(record);
3647
+ carried.remember();
3648
+ submitLater(locator);
3649
+ return mergeChangeSets([
3650
+ changeSet(locator, {
3651
+ queue: true,
3652
+ drafts: [...written.keys(), ...carried.taken],
3653
+ }),
3654
+ ...carried.changes,
3655
+ ]);
3656
+ },
3657
+ };
3658
+ }
3659
+ async function unpair(local) {
3660
+ const handled = new Set();
3661
+ for (let held = unpaired(handled); held !== null && continuing(); held = unpaired(handled)) {
3662
+ const { locator, batch } = held;
3663
+ handled.add(pendingKey(locator, batch.id));
3664
+ publishChangeSet(await together(local, [await unpairing(local, locator, [batch])]));
3665
+ }
3666
+ if (handled.size === 0)
3667
+ return;
3668
+ moveTo({ ...status, ...attention() });
3669
+ }
3670
+ async function discardHeld(head, room, local, signal) {
3671
+ const partner = partnerOf(head, room.locator);
3672
+ const other = partner === null ? undefined : discovered.get(partner);
3673
+ await discardPending(other === undefined ? [room] : [room, other], local, signal);
3674
+ }
2172
3675
  async function submitRoom(room, local, authorized, progress, signal) {
2173
3676
  while (continuing() && authorization !== undefined && !signal.aborted) {
2174
3677
  const replica = replicas.get(room.locator);
2175
3678
  const head = replica?.queue[0];
2176
3679
  if (replica === undefined || head === undefined)
2177
3680
  return 'done';
2178
- if (pendings.get(head.id)?.accepted !== undefined)
3681
+ const state = pendings.get(pendingKey(room.locator, head.id));
3682
+ if (state?.accepted !== undefined)
3683
+ return 'done';
3684
+ const partner = partnerOf(head, room.locator);
3685
+ if (partner !== null && !leavingIn(head, room.locator)) {
3686
+ submitLater(partner);
3687
+ return 'done';
3688
+ }
3689
+ if (partner !== null && behindIn(partner, head)) {
3690
+ progress.complete = false;
2179
3691
  return 'done';
2180
- const state = pendings.get(head.id);
3692
+ }
2181
3693
  const { submission, lost: earlier } = state ?? {
2182
3694
  submission: { state: 'waiting' },
2183
3695
  lost: false,
@@ -2193,16 +3705,16 @@ export function composeRuntime(options) {
2193
3705
  }
2194
3706
  const lost = earlier || submission.state === 'submitted';
2195
3707
  if (lost && pastHorizon(head.id)) {
2196
- await discardPending(room, local, signal);
3708
+ await discardHeld(head, room, local, signal);
2197
3709
  return 'done';
2198
3710
  }
2199
3711
  let result;
2200
3712
  journal.record(`submission ${room.locator} ${head.id}`);
2201
- await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
3713
+ await exclusive(() => mark(local, room.locator, head, { state: 'submitted' }, lost));
2202
3714
  if (discoveryPaused || fences.has(room.locator))
2203
3715
  return 'done';
2204
3716
  if (signal.aborted || writesHeld()) {
2205
- await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
3717
+ await exclusive(() => mark(local, room.locator, head, submission, earlier));
2206
3718
  if (signal.aborted)
2207
3719
  return 'done';
2208
3720
  progress.complete = false;
@@ -2210,11 +3722,17 @@ export function composeRuntime(options) {
2210
3722
  return 'done';
2211
3723
  }
2212
3724
  const opensBefore = openSignals;
2213
- const answered = awaitingAnswer(room.locator);
3725
+ const answeredHere = awaitingAnswer(room.locator);
3726
+ const answeredThere = partner === null ? () => { } : awaitingAnswer(partner);
3727
+ const answered = () => {
3728
+ answeredHere();
3729
+ answeredThere();
3730
+ };
2214
3731
  try {
2215
3732
  result = await options.adapter.mutate(authorized, {
2216
3733
  locator: room.locator,
2217
3734
  changeBatch: head,
3735
+ file: batchFile(head),
2218
3736
  ...(state?.upload === undefined ? {} : { upload: state.upload }),
2219
3737
  }, (upload) => exclusive(() => keepUpload(local, room.locator, head.id, upload)));
2220
3738
  }
@@ -2239,9 +3757,10 @@ export function composeRuntime(options) {
2239
3757
  return 'again';
2240
3758
  }
2241
3759
  if (result.kind === 'room-not-readable') {
3760
+ const unread = discovered.get(result.locator) ?? room;
2242
3761
  progress.complete = false;
2243
- unreadable.add(room.locator);
2244
- list(room, false);
3762
+ unreadable.add(unread.locator);
3763
+ list(unread, false);
2245
3764
  emitRooms();
2246
3765
  return 'done';
2247
3766
  }
@@ -2256,24 +3775,30 @@ export function composeRuntime(options) {
2256
3775
  }
2257
3776
  if (result.kind === 'write-halted') {
2258
3777
  journal.record(`outcome ${room.locator} ${head.id} write-halted`);
2259
- await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, lost));
3778
+ await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, lost));
2260
3779
  if (serviceMode === 'open' && openSignals !== opensBefore)
2261
3780
  continue;
2262
3781
  haltedRefusal = true;
2263
3782
  progress.complete = false;
2264
3783
  return 'done';
2265
3784
  }
3785
+ const other = partner === null ? undefined : discovered.get(partner);
2266
3786
  if (result.kind === 'conflicted' ||
2267
3787
  result.kind === 'rejected' ||
2268
3788
  result.kind === 'expired' ||
2269
3789
  result.kind === 'retryable-refusal') {
2270
3790
  if (result.kind === 'expired' && lost && result.restored !== true) {
2271
3791
  journal.record(`outcome ${room.locator} ${head.id} expired`);
2272
- await discardPending(room, local, signal);
3792
+ await discardHeld(head, room, local, signal);
2273
3793
  return 'done';
2274
3794
  }
2275
3795
  if (result.kind === 'expired') {
2276
- await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, false));
3796
+ await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, partner !== null && lost));
3797
+ const there = other === undefined ? null : await advance(other, local, authorized, continuing);
3798
+ if (other !== undefined && there !== null) {
3799
+ await withhold(there, other, local, progress);
3800
+ return 'done';
3801
+ }
2277
3802
  const withheld = await advance(room, local, authorized, continuing);
2278
3803
  if (withheld !== null) {
2279
3804
  await withhold(withheld, room, local, progress);
@@ -2290,11 +3815,25 @@ export function composeRuntime(options) {
2290
3815
  if (result.kind === 'retryable-refusal') {
2291
3816
  const reset = result.reset;
2292
3817
  progress.complete = false;
2293
- await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
3818
+ await exclusive(() => mark(local, room.locator, head, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
2294
3819
  return reset === undefined ? 'again' : { at: Date.parse(reset) };
2295
3820
  }
2296
3821
  if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
2297
3822
  const left = leaving(head, result);
3823
+ const refreshed = async () => ({
3824
+ there: other === undefined ? null : await advance(other, local, authorized, continuing),
3825
+ here: await advance(room, local, authorized, continuing),
3826
+ });
3827
+ const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
3828
+ if (judged !== null && (judged.there !== null || judged.here !== null)) {
3829
+ await exclusive(() => mark(local, room.locator, head, { state: 'waiting' }, lost));
3830
+ if (other !== undefined && judged.there !== null) {
3831
+ await withhold(judged.there, other, local, progress);
3832
+ }
3833
+ if (judged.here !== null)
3834
+ await withhold(judged.here, room, local, progress);
3835
+ return 'done';
3836
+ }
2298
3837
  await exclusive(async () => {
2299
3838
  if (!continuing())
2300
3839
  return;
@@ -2302,25 +3841,29 @@ export function composeRuntime(options) {
2302
3841
  if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
2303
3842
  return;
2304
3843
  }
2305
- const settled = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
2306
- leaving: (result.kind === 'expired' && landed(head, current.confirmed)) ||
2307
- (result.kind === 'conflicted' && landed(head, current.confirmed, result.affected))
2308
- ? { ...left, returned: null }
2309
- : left,
2310
- });
2311
- publishChangeSet(settled());
3844
+ const confirmedLayer = await sliceAt(current.record, 'confirmed', [head]);
3845
+ const held = (result.kind === 'expired' &&
3846
+ (await landedWhole(room.locator, head, confirmedLayer))) ||
3847
+ (result.kind === 'conflicted' &&
3848
+ (await landedWhole(room.locator, head, confirmedLayer, result.affected)));
3849
+ publishChangeSet(await settle(local, room.locator, current, current.record, current.queue.filter((batch) => batch.id !== head.id), { leaving: [held ? { ...left, returned: null } : left] }));
3850
+ await unpair(local);
2312
3851
  moveTo({ ...status, ...attention() });
2313
3852
  });
2314
3853
  if (result.kind === 'expired')
2315
3854
  continue;
2316
- const withheld = await advance(room, local, authorized, continuing);
2317
- if (withheld !== null) {
2318
- await withhold(withheld, room, local, progress);
3855
+ const { there, here } = judged ?? (await refreshed());
3856
+ if (other !== undefined && there !== null)
3857
+ await withhold(there, other, local, progress);
3858
+ if (here !== null) {
3859
+ await withhold(here, room, local, progress);
2319
3860
  return 'done';
2320
3861
  }
2321
3862
  continue;
2322
3863
  }
2323
- const kept = await adopted(room, local, head, result).finally(answered);
3864
+ const kept = await (partner === null
3865
+ ? adopted(room, local, head, result)
3866
+ : adoptedMove(room, partner, local, head, result)).finally(answered);
2324
3867
  if (!kept) {
2325
3868
  progress.complete = false;
2326
3869
  return 'done';
@@ -2331,44 +3874,68 @@ export function composeRuntime(options) {
2331
3874
  function pastHorizon(identity) {
2332
3875
  return (outcomeRetention !== null && identityTime(identity) + outcomeRetention * 1000 <= Date.now());
2333
3876
  }
2334
- async function discardPending(room, local, signal) {
2335
- const { locator } = room;
3877
+ async function discardPending(rooms, local, signal) {
2336
3878
  const discarded = await exclusive(async () => {
2337
- if (fences.has(locator))
2338
- return false;
2339
- const before = replicas.get(locator);
2340
- const queue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
2341
- fences.set(locator, { at: discoveries, erased: false });
2342
- lastFenced.set(locator, ++fencings);
2343
- try {
2344
- await local.discard(locator);
2345
- }
2346
- finally {
2347
- fences.delete(locator);
3879
+ const held = rooms
3880
+ .filter(({ locator }) => !fences.has(locator))
3881
+ .map((room) => ({
3882
+ room,
3883
+ before: replicas.get(room.locator),
3884
+ queue: queueIn(room.locator),
3885
+ }));
3886
+ if (held.length === 0)
3887
+ return [];
3888
+ const answer = await local.commit({
3889
+ rooms: held.flatMap(({ room, before }) => before === undefined
3890
+ ? []
3891
+ : [
3892
+ {
3893
+ locator: room.locator,
3894
+ key: before.record.key,
3895
+ roots: before.record.typedSelection?.roots ?? [],
3896
+ composed: composedOf(composed(before.record)),
3897
+ overlay: { set: [], cleared: [...before.overlaid] },
3898
+ },
3899
+ ]),
3900
+ discards: held.map(({ room }) => room.locator),
3901
+ });
3902
+ for (const { room, before, queue } of held) {
3903
+ const { locator } = room;
3904
+ lastFenced.set(locator, ++fencings);
3905
+ for (const batch of queue)
3906
+ pendings.delete(pendingKey(locator, batch.id));
3907
+ queuesWithoutReplica.delete(locator);
3908
+ if (before !== undefined) {
3909
+ replicas.set(locator, {
3910
+ ...before,
3911
+ queue: [],
3912
+ overlaid: new Set(),
3913
+ count: answer.counts[locator] ?? before.count,
3914
+ });
3915
+ }
3916
+ journal.record(`room discarded ${locator}`);
2348
3917
  }
2349
- for (const batch of queue)
2350
- pendings.delete(batch.id);
2351
- queuesWithoutReplica.delete(locator);
2352
- const after = before === undefined ? null : replicaOf(before.confirmed, [], before);
2353
- if (after !== null)
2354
- replicas.set(locator, after);
2355
- journal.record(`room discarded ${locator}`);
2356
- publishChangeSet(changeSet(locator, before ?? null, after, { queue: true }));
3918
+ publishChangeSet(mergeChangeSets(held.map(({ room }) => changeSet(room.locator, {
3919
+ documents: answer.changed[room.locator] ?? [],
3920
+ queue: true,
3921
+ }))));
2357
3922
  moveTo({ ...status, ...attention() });
2358
- return true;
2359
- });
2360
- if (!discarded)
2361
- return;
2362
- const progress = { complete: true, reachable: true };
2363
- await roomTask(locator, async (live) => {
2364
- await Promise.race([
2365
- reload(room, local, progress, () => live() && !signal.aborted),
2366
- abandonedOn(signal),
2367
- ]);
3923
+ return held.map(({ room }) => room);
2368
3924
  });
2369
- if (signal.aborted)
2370
- return;
2371
- await rest(progress);
3925
+ for (const room of discarded) {
3926
+ if (signal.aborted)
3927
+ return;
3928
+ const progress = { complete: true, reachable: true };
3929
+ await roomTask(room.locator, async (live) => {
3930
+ await Promise.race([
3931
+ reload(room, local, progress, () => live() && !signal.aborted),
3932
+ abandonedOn(signal),
3933
+ ]);
3934
+ });
3935
+ if (signal.aborted)
3936
+ return;
3937
+ await rest(progress);
3938
+ }
2372
3939
  }
2373
3940
  function awaitingAnswer(locator) {
2374
3941
  let release = () => { };
@@ -2382,16 +3949,17 @@ export function composeRuntime(options) {
2382
3949
  release();
2383
3950
  };
2384
3951
  }
2385
- async function retainModel(local, room, attached, next) {
2386
- const key = modelKey(room.locator, attached);
2387
- const previous = modelOf(room.locator, attached);
2388
- const heldBefore = replicas.get(room.locator)?.confirmed.record;
2389
- if (JSON.stringify(previous) === JSON.stringify(next) &&
2390
- (heldBefore === undefined ||
2391
- (JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
2392
- JSON.stringify(heldBefore.replica) === JSON.stringify(room.replica) &&
2393
- JSON.stringify(heldBefore.typedSelection) ===
2394
- JSON.stringify(room.typedSelection ?? null))))
3952
+ function declaredAsHeld(room) {
3953
+ const held = replicas.get(room.locator)?.record;
3954
+ return (held === undefined ||
3955
+ (JSON.stringify(held.operations) === JSON.stringify(room.operations) &&
3956
+ JSON.stringify(held.replica) === JSON.stringify(room.replica) &&
3957
+ JSON.stringify(held.typedSelection) === JSON.stringify(room.typedSelection ?? null)));
3958
+ }
3959
+ async function retainModel(local, room, next) {
3960
+ const key = room.locator;
3961
+ const previous = modelOf(room.locator);
3962
+ if (JSON.stringify(previous) === JSON.stringify(next) && declaredAsHeld(room))
2395
3963
  return;
2396
3964
  if (next.status === 'unavailable') {
2397
3965
  invalidModels.add(key);
@@ -2404,32 +3972,47 @@ export function composeRuntime(options) {
2404
3972
  let changed = null;
2405
3973
  if (held !== undefined) {
2406
3974
  const declared = {
2407
- ...held.confirmed.record,
3975
+ ...held.record,
2408
3976
  operations: room.operations,
2409
3977
  replica: room.replica,
2410
3978
  typedSelection: room.typedSelection ?? null,
2411
- ...(attached === 'collection' ? { model: next } : { selectionModel: next }),
3979
+ model: next,
2412
3980
  };
2413
- const reclassified = sameMembership(declared, held.confirmed.record)
2414
- ? { kind: 'staged', confirmed: confirmedOf(declared, held.confirmed) }
2415
- : await readReclassified(declared, held.confirmed);
2416
- const record = reclassified.kind === 'staged'
3981
+ let read = 'staged';
3982
+ if (!sameMembership(declared, held.record)) {
3983
+ const verdict = await validatedCollection(walkOf(local, held.record.key, 'confirmed'), lookupOf(local, held.record.key, 'confirmed'), composed(declared));
3984
+ read =
3985
+ verdict.verdict !== 'refuse'
3986
+ ? 'staged'
3987
+ : verdict.rule === 'model-unavailable'
3988
+ ? 'deferred'
3989
+ : { kind: 'refused', refusal: verdict };
3990
+ }
3991
+ const record = read === 'staged'
2417
3992
  ? declared
2418
- : {
2419
- ...declared,
2420
- typedSelection: held.confirmed.record.typedSelection,
2421
- };
2422
- const read = reclassified.kind === 'deferred'
2423
- ? { kind: 'staged', confirmed: confirmedOf(record, held.confirmed) }
2424
- : reclassified;
2425
- await local.write({ next: record, previous: held.confirmed.record });
2426
- const replica = read.kind === 'staged'
2427
- ? await stepped(replicating(read.confirmed, held.queue, held))
2428
- : { ...held, confirmed: { ...held.confirmed, record } };
2429
- const set = await stepped(changing(room.locator, held, replica, { room: true }));
2430
- replicas.set(room.locator, replica);
2431
- changed = heldChange(room.locator, held, replica, set, { room: true });
2432
- if (read.kind === 'staged') {
3993
+ : { ...declared, typedSelection: held.record.typedSelection };
3994
+ const confirmedLayer = await sliceAt(record, 'confirmed', held.queue, held.overlaid);
3995
+ const replayedLayer = await replayed(record, confirmedLayer, held.queue);
3996
+ const diff = await overlayDiff(record, held, replayedLayer.visible, replayedLayer.touched);
3997
+ const answer = await local.commit({
3998
+ rooms: [
3999
+ {
4000
+ locator: room.locator,
4001
+ key: record.key,
4002
+ record: storedRecord(record),
4003
+ roots: record.typedSelection?.roots ?? [],
4004
+ composed: composedOf(composed(record)),
4005
+ overlay: diff.overlay,
4006
+ },
4007
+ ],
4008
+ });
4009
+ const { count, indexed } = committedRoom(room.locator, answer);
4010
+ replicas.set(room.locator, { ...held, record, overlaid: diff.touched, count, indexed });
4011
+ changed = changeSet(room.locator, {
4012
+ documents: answer.changed[room.locator] ?? [],
4013
+ room: true,
4014
+ });
4015
+ if (read === 'staged' || read === 'deferred') {
2433
4016
  if (declarationRefusals.delete(room.locator)) {
2434
4017
  sourceRefusals.delete(room.locator);
2435
4018
  unreadable.delete(room.locator);
@@ -2440,20 +4023,23 @@ export function composeRuntime(options) {
2440
4023
  declarationRefusals.add(room.locator);
2441
4024
  }
2442
4025
  }
2443
- models.set(key, next);
2444
- if (next.status === 'current') {
2445
- liveModels.set(key, JSON.stringify(next.snapshot));
2446
- invalidModels.delete(key);
2447
- }
2448
- list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
4026
+ heldModel(room, next);
2449
4027
  if (changed === null)
2450
4028
  emitRooms();
2451
4029
  else
2452
4030
  publishChangeSet(changed);
2453
4031
  });
2454
4032
  }
4033
+ function heldModel(room, next) {
4034
+ models.set(room.locator, next);
4035
+ if (next.status === 'current') {
4036
+ liveModels.set(room.locator, JSON.stringify(next.snapshot));
4037
+ invalidModels.delete(room.locator);
4038
+ }
4039
+ list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
4040
+ }
2455
4041
  async function scoped(result, room, local) {
2456
- if ((result.scope === 'content' || result.scope === 'sync') && !result.retryable) {
4042
+ if (result.scope === 'content' && !result.retryable) {
2457
4043
  journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
2458
4044
  await loseRooms([room.locator]);
2459
4045
  return;
@@ -2465,22 +4051,14 @@ export function composeRuntime(options) {
2465
4051
  });
2466
4052
  let scopedAway = room;
2467
4053
  if (result.scope === 'model')
2468
- await retainModel(local, room, 'collection', {
4054
+ await retainModel(local, room, {
2469
4055
  status: 'unavailable',
2470
4056
  reason: 'not-readable',
2471
4057
  });
2472
4058
  else if (result.scope !== 'operation') {
2473
4059
  unreadable.add(room.locator);
2474
- scopedAway = {
2475
- ...room,
2476
- replica: { ...room.replica, admission: 'unavailable' },
2477
- operations: {
2478
- ...room.operations,
2479
- ...(result.scope === 'content' ? { read: [] } : {}),
2480
- ...(result.scope === 'sync' ? { sync: [] } : {}),
2481
- },
2482
- };
2483
- await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
4060
+ scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
4061
+ await retainModel(local, scopedAway, modelOf(room.locator));
2484
4062
  }
2485
4063
  if (!continuing())
2486
4064
  return;
@@ -2490,17 +4068,10 @@ export function composeRuntime(options) {
2490
4068
  }
2491
4069
  function heldRooms(listed) {
2492
4070
  return listed.flatMap((room) => {
2493
- const record = replicas.get(room.locator)?.confirmed.record;
4071
+ const record = replicas.get(room.locator)?.record;
2494
4072
  if (record === undefined || record.replica.selection !== room.replica.selection)
2495
4073
  return [];
2496
- return [
2497
- {
2498
- locator: room.locator,
2499
- roomVersion: record.roomVersion,
2500
- documents: record.documents,
2501
- revisions: record.revisions,
2502
- },
2503
- ];
4074
+ return [{ locator: room.locator, roomVersion: record.roomVersion }];
2504
4075
  });
2505
4076
  }
2506
4077
  async function synchronize(listed, local, progress, attempt, live) {
@@ -2528,7 +4099,7 @@ export function composeRuntime(options) {
2528
4099
  progress.complete = false;
2529
4100
  return false;
2530
4101
  }
2531
- const prior = replicas.get(room.locator)?.confirmed.record;
4102
+ const prior = replicas.get(room.locator)?.record;
2532
4103
  if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
2533
4104
  list(prior, false);
2534
4105
  emitRooms();
@@ -2537,7 +4108,7 @@ export function composeRuntime(options) {
2537
4108
  return false;
2538
4109
  }
2539
4110
  if (prior !== undefined)
2540
- await retainModel(local, room, 'collection', modelOf(room.locator));
4111
+ await retainModel(local, room, modelOf(room.locator));
2541
4112
  if (discoveryPaused || authorization === undefined)
2542
4113
  return false;
2543
4114
  return room.replica.admission === 'admitted';
@@ -2550,10 +4121,8 @@ export function composeRuntime(options) {
2550
4121
  await withhold(withheld, room, local, progress);
2551
4122
  }
2552
4123
  function revoked(locator) {
2553
- return ['collection', 'typed-selection'].some((attached) => {
2554
- const model = modelOf(locator, attached);
2555
- return model.status === 'unavailable' && model.reason === 'revoked';
2556
- });
4124
+ const model = modelOf(locator);
4125
+ return model.status === 'unavailable' && model.reason === 'revoked';
2557
4126
  }
2558
4127
  async function loadRoom(room, local, progress, live) {
2559
4128
  if (!live() || authorization === undefined)
@@ -2576,13 +4145,12 @@ export function composeRuntime(options) {
2576
4145
  if (!live() || authorization === undefined)
2577
4146
  return;
2578
4147
  const bootstrapped = await pullBootstrap(authorization, room, local, live);
2579
- if (!live())
2580
- return;
2581
4148
  if (bootstrapped.kind !== 'staged') {
2582
- await withhold(bootstrapped, room, local, progress);
4149
+ if (live())
4150
+ await withhold(bootstrapped, room, local, progress);
2583
4151
  return;
2584
4152
  }
2585
- await publish(local, room, bootstrapped.confirmed, { stored: bootstrapped.stored }, live);
4153
+ await publish(local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed }, live);
2586
4154
  await caughtUp(room, local, progress, live);
2587
4155
  }
2588
4156
  finally {
@@ -2604,6 +4172,8 @@ export function composeRuntime(options) {
2604
4172
  catch {
2605
4173
  result = { kind: 'transport-uncertainty' };
2606
4174
  }
4175
+ if (result.kind === 'discovery')
4176
+ roomsNamed = true;
2607
4177
  if (!live()) {
2608
4178
  progress.complete = false;
2609
4179
  notifyReady();
@@ -2611,12 +4181,7 @@ export function composeRuntime(options) {
2611
4181
  }
2612
4182
  journal.record(`connection discover ${result.kind}`);
2613
4183
  if (result.kind === 'transport-uncertainty') {
2614
- for (const room of rooms.values()) {
2615
- await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
2616
- if (room.typedSelection !== null) {
2617
- await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
2618
- }
2619
- }
4184
+ await settleModels([...rooms.values()].map((room) => discovered.get(room.locator) ?? room), 'cached', local);
2620
4185
  progress.reachable = false;
2621
4186
  progress.complete = false;
2622
4187
  notifyReady();
@@ -2639,12 +4204,13 @@ export function composeRuntime(options) {
2639
4204
  }
2640
4205
  for (const room of result.rooms) {
2641
4206
  if (room.operations === undefined ||
2642
- !['read', 'sync', 'write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
4207
+ !['write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
2643
4208
  room.operations[operation].every((scope) => typeof scope === 'string')) ||
2644
4209
  room.replica === undefined ||
2645
4210
  typeof room.replica.selection !== 'string' ||
2646
4211
  room.replica.selection.length === 0 ||
2647
- !['admitted', 'unavailable'].includes(room.replica.admission)) {
4212
+ !['admitted', 'unavailable'].includes(room.replica.admission) ||
4213
+ !['locked', 'open'].includes(room.replica.offline)) {
2648
4214
  journal.record(`invalid model or replica declaration ${room.locator}`);
2649
4215
  await refuse('missing-required-guarantee');
2650
4216
  notifyReady();
@@ -2668,11 +4234,13 @@ export function composeRuntime(options) {
2668
4234
  if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
2669
4235
  JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
2670
4236
  limits = result.providerLimits;
2671
- await local.declare(limits, capabilities);
4237
+ await local.fact('providerLimits', limits);
4238
+ await local.fact('capabilities', capabilities);
4239
+ conversations.declared();
2672
4240
  }
2673
4241
  const signedIn = result.member ?? null;
2674
4242
  if (JSON.stringify(signedIn) !== JSON.stringify(member)) {
2675
- await local.keepMember(signedIn);
4243
+ await local.fact('member', signedIn);
2676
4244
  member = signedIn;
2677
4245
  emitMember();
2678
4246
  }
@@ -2726,7 +4294,7 @@ export function composeRuntime(options) {
2726
4294
  function sync(attempt) {
2727
4295
  return task(OPENING_LANE, async (live) => {
2728
4296
  if (!live()) {
2729
- notifyReady();
4297
+ readyFromStore();
2730
4298
  return;
2731
4299
  }
2732
4300
  const progress = { complete: true, reachable: true };
@@ -2804,8 +4372,10 @@ export function composeRuntime(options) {
2804
4372
  });
2805
4373
  }
2806
4374
  function closedSocket(revoked, lost) {
4375
+ notifyDiscovered();
2807
4376
  const opened = socketOpen;
2808
4377
  socketOpen = false;
4378
+ connectionLost();
2809
4379
  if (lost) {
2810
4380
  reads.lost();
2811
4381
  conversations.lost();
@@ -2846,13 +4416,9 @@ export function composeRuntime(options) {
2846
4416
  const local = await openLocal();
2847
4417
  const reloaded = await replacing;
2848
4418
  if (progress.reachable) {
2849
- for (const room of discovered.values()) {
2850
- if (replicas.has(room.locator) &&
2851
- !expired.includes(room.locator) &&
2852
- !reloading.includes(room.locator)) {
2853
- await confirmModels(room, local);
2854
- }
2855
- }
4419
+ await settleModels([...discovered.values()].filter((room) => replicas.has(room.locator) &&
4420
+ !expired.includes(room.locator) &&
4421
+ !reloading.includes(room.locator)), 'current', local);
2856
4422
  }
2857
4423
  const failed = reloaded.find((outcome) => outcome.status === 'rejected');
2858
4424
  if (failed !== undefined)
@@ -2875,7 +4441,7 @@ export function composeRuntime(options) {
2875
4441
  owed.clear();
2876
4442
  for (const locator of reloading)
2877
4443
  catching.add(locator);
2878
- readSide.recover();
4444
+ liveQueries.recover();
2879
4445
  return reloading;
2880
4446
  }
2881
4447
  async function reloadNow(replaced, progress) {
@@ -2893,12 +4459,30 @@ export function composeRuntime(options) {
2893
4459
  return [];
2894
4460
  }
2895
4461
  }
2896
- async function confirmModels(room, local) {
2897
- for (const attached of ['collection', 'typed-selection']) {
2898
- const model = modelOf(room.locator, attached);
2899
- if (model.status === 'cached') {
2900
- await retainModel(local, room, attached, { ...model, status: 'current' });
4462
+ async function settleModels(held, status, local) {
4463
+ const owed = (room) => {
4464
+ const model = modelOf(room.locator);
4465
+ return model.status !== 'unavailable' && model.status !== status;
4466
+ };
4467
+ const settled = await exclusive(async () => {
4468
+ if (!continuing())
4469
+ return [];
4470
+ const unchanged = held.filter((room) => owed(room) && !declarationRefusals.has(room.locator) && declaredAsHeld(room));
4471
+ for (const room of unchanged) {
4472
+ const next = { ...modelOf(room.locator), status };
4473
+ const replica = replicas.get(room.locator);
4474
+ if (replica !== undefined)
4475
+ replicas.set(room.locator, { ...replica, record: { ...replica.record, model: next } });
4476
+ heldModel(room, next);
2901
4477
  }
4478
+ if (unchanged.length > 0)
4479
+ emitRooms();
4480
+ return unchanged;
4481
+ });
4482
+ for (const room of held) {
4483
+ if (settled.includes(room) || !owed(room))
4484
+ continue;
4485
+ await retainModel(local, room, { ...modelOf(room.locator), status });
2902
4486
  }
2903
4487
  }
2904
4488
  function submitLater(locator) {
@@ -3042,7 +4626,7 @@ export function composeRuntime(options) {
3042
4626
  if (synced && !recorded) {
3043
4627
  const completedAt = new Date().toISOString();
3044
4628
  try {
3045
- await (await openLocal()).complete(completedAt);
4629
+ await (await openLocal()).fact('lastCompleted', completedAt);
3046
4630
  if (!live())
3047
4631
  return;
3048
4632
  recorded = true;
@@ -3082,8 +4666,8 @@ export function composeRuntime(options) {
3082
4666
  if (room === undefined || room.replica.admission !== 'admitted')
3083
4667
  return;
3084
4668
  if (held !== undefined &&
3085
- (held.confirmed.record.replica.selection !== room.replica.selection ||
3086
- options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion))) {
4669
+ (held.record.replica.selection !== room.replica.selection ||
4670
+ options.adapter.reached(locator, held.record.roomVersion, roomVersion))) {
3087
4671
  return;
3088
4672
  }
3089
4673
  const during = underway?.generation === generation ? underway.progress : null;
@@ -3130,6 +4714,7 @@ export function composeRuntime(options) {
3130
4714
  return;
3131
4715
  if (event.kind === 'opened') {
3132
4716
  socketOpen = true;
4717
+ connectionLive();
3133
4718
  generation += 1;
3134
4719
  reads.opened();
3135
4720
  conversations.reopened();
@@ -3144,7 +4729,14 @@ export function composeRuntime(options) {
3144
4729
  void rediscover(event.scope);
3145
4730
  else
3146
4731
  void applyRoom(event.locator, event.roomVersion);
3147
- }, receipts);
4732
+ }, receipts, heldRevisions);
4733
+ }
4734
+ async function heldRevisions(locator, paths) {
4735
+ const held = replicas.get(locator);
4736
+ if (held === undefined || paths.length === 0)
4737
+ return new Map();
4738
+ const entries = await (await openLocal()).entries(held.record.key, 'confirmed', paths);
4739
+ return new Map(entries.flatMap(({ path, revision }) => revision === null ? [] : [[path, revision]]));
3148
4740
  }
3149
4741
  function writesHeld() {
3150
4742
  return serviceMode === 'write-halted' || haltedRefusal;
@@ -3214,8 +4806,13 @@ export function composeRuntime(options) {
3214
4806
  if (closed)
3215
4807
  return;
3216
4808
  closed = true;
3217
- if (rechecking !== null)
3218
- clearInterval(rechecking);
4809
+ notifyDiscovered();
4810
+ if (probing !== null)
4811
+ clearInterval(probing);
4812
+ for (const resolve of shownWaiters.splice(0))
4813
+ resolve();
4814
+ connectionLive();
4815
+ lockListeners.clear();
3219
4816
  readinessWatches.clear();
3220
4817
  withdrawStream?.();
3221
4818
  withdrawStream = undefined;
@@ -3224,17 +4821,14 @@ export function composeRuntime(options) {
3224
4821
  withdrawServiceMode?.();
3225
4822
  conversations.close();
3226
4823
  void recovery.retire();
3227
- store?.close();
3228
- void opening?.then((opened) => {
3229
- opened.close();
3230
- }, () => { });
3231
4824
  roomListeners.clear();
4825
+ unlistedRooms.clear();
3232
4826
  refusalListeners.clear();
3233
4827
  storageLossListeners.clear();
3234
4828
  statusListeners.clear();
3235
4829
  serviceModeListeners.clear();
3236
4830
  memberListeners.clear();
3237
- readSide.close();
4831
+ liveQueries.close();
3238
4832
  queueSubscriptions.clear();
3239
4833
  roomDraftListeners.clear();
3240
4834
  presentationListeners.clear();
@@ -3254,9 +4848,9 @@ export function composeRuntime(options) {
3254
4848
  observeStream();
3255
4849
  void openLocal().then(() => {
3256
4850
  if (capabilities.length > 0)
3257
- notifyReady();
4851
+ readyFromStore();
3258
4852
  }, () => {
3259
- notifyReady();
4853
+ readyFromStore();
3260
4854
  });
3261
4855
  if (options.adapter.roomStream === undefined)
3262
4856
  void sync('unstreamed');
@@ -3280,7 +4874,7 @@ export function composeRuntime(options) {
3280
4874
  const watchers = seedWatchers.get(plan.key) ?? new Set();
3281
4875
  seedWatchers.set(plan.key, watchers);
3282
4876
  watchers.add(shown);
3283
- const stop = readSide.watch(plan, identityOf(plan), () => {
4877
+ const stop = liveQueries.watch(plan, identityOf(plan), () => {
3284
4878
  const cover = covered(plan);
3285
4879
  awaitPreparation(cover);
3286
4880
  return seedBehind(plan, cover);
@@ -3301,66 +4895,53 @@ export function composeRuntime(options) {
3301
4895
  stop();
3302
4896
  };
3303
4897
  }
3304
- async function searched(input, reach) {
4898
+ async function searched(input, reach, locked) {
3305
4899
  const asked = searchPlanOf(input);
3306
- const plan = { ...asked, limit: capped(asked.limit) };
3307
- const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
4900
+ const filter = {
4901
+ ...filterOf(asked.documents ?? {}, roomFacts(reach), planReach(reach)),
4902
+ locked,
4903
+ };
4904
+ const unlocking = (filter.rooms ?? [])
4905
+ .filter((room) => locked.includes(room))
4906
+ .map((room) => ({ room, awaits: 'unlock' }));
3308
4907
  for (;;) {
3309
4908
  const cover = covered(filter);
3310
4909
  awaitPreparation(cover);
3311
- const pending = 'pending' in cover ? [...cover.pending] : [];
3312
- const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
3313
- const held = covered({
3314
- ...filter,
3315
- rooms: reachedRooms(filter).filter((room) => !awaited.has(room)),
3316
- });
3317
- const covering = 'pending' in held ? [] : held.coverage.rooms;
3318
- const unindexed = covering
3319
- .filter(({ room }) => !searchIndex.rooms().has(room))
3320
- .map(({ room }) => room);
3321
- for (const room of unindexed)
3322
- pending.push({ room, awaits: 'index' });
3323
- const listed = conversations.searchable();
3324
- const named = reach !== null && !reach.conversations
3325
- ? []
3326
- : (plan.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
3327
- const waiting = named.filter((one) => !searchIndex.conversations().has(one));
3328
- for (const conversation of waiting)
3329
- pending.push({ conversation, awaits: 'index' });
3330
- if (unindexed.length > 0 || waiting.length > 0)
3331
- index(unindexed, waiting);
4910
+ const pending = [...unlocking, ...('pending' in cover ? cover.pending : [])];
3332
4911
  const complete = pending.length === 0 && !('pending' in cover);
4912
+ const awaited = new Set('pending' in cover ? cover.pending.map(({ room }) => room) : []);
3333
4913
  const answering = complete
3334
4914
  ? filter
3335
- : {
3336
- ...filter,
3337
- rooms: covering
3338
- .filter(({ room }) => searchIndex.rooms().has(room))
3339
- .map(({ room }) => room),
3340
- };
4915
+ : { ...filter, rooms: reachedRooms(filter).filter((room) => !awaited.has(room)) };
3341
4916
  const answered = covered(answering);
3342
4917
  if ('pending' in answered)
3343
4918
  continue;
3344
- const searchedConversations = named.filter((one) => searchIndex.conversations().has(one));
3345
- await readSide.settled();
3346
- const seen = searchIndex.changes();
3347
- await searchIndex.caughtUp();
3348
- const selected = roomsAlone(answering) ? null : await rowsQuery(answering);
3349
- if (selected !== null && 'pending' in selected)
3350
- continue;
3351
- if (searchIndex.changes() !== seen ||
3352
- JSON.stringify(covered(answering)) !== JSON.stringify(answered))
4919
+ const listed = conversations.searchable();
4920
+ const named = reach !== null && !reach.conversations
4921
+ ? []
4922
+ : (asked.conversations ?? listed).filter((one) => listed.includes(one) && conversations.acquired(one));
4923
+ const hits = await (await openLocal()).search({
4924
+ text: asked.text,
4925
+ limit: capped(asked.limit),
4926
+ after: asked.after,
4927
+ total: asked.total,
4928
+ documents: {
4929
+ plan: answering,
4930
+ rooms: answered.rooms,
4931
+ beneath: asked.collection
4932
+ ? answered.rooms.map(({ locator, key }) => ({
4933
+ key,
4934
+ roots: replicas.get(locator)?.record.typedSelection?.roots ?? [],
4935
+ }))
4936
+ : [],
4937
+ },
4938
+ conversations: named,
4939
+ });
4940
+ if (JSON.stringify(covered(answering)) !== JSON.stringify(answered))
3353
4941
  continue;
3354
- const hits = searchIndex.search(plan, selected === null ? null : selected.documents, searchedConversations, answering.rooms === null ? null : new Set(answering.rooms), plan.collection
3355
- ? new Map(answered.coverage.rooms.map(({ room }) => [
3356
- room,
3357
- replicas.get(room)?.confirmed.record.typedSelection?.roots ?? [],
3358
- ]))
3359
- : null);
3360
4942
  const coverage = {
3361
4943
  rooms: answered.coverage.rooms,
3362
- conversations: searchedConversations,
3363
- index: searchIndex.generation(),
4944
+ conversations: named,
3364
4945
  overlay: answered.coverage.overlay,
3365
4946
  };
3366
4947
  return complete
@@ -3374,13 +4955,17 @@ export function composeRuntime(options) {
3374
4955
  roomDraftListeners.add(listener);
3375
4956
  void exclusive(async () => {
3376
4957
  await openLocal();
3377
- if (closed || !roomDraftListeners.has(listener))
3378
- return;
3379
- listener.ready = true;
3380
4958
  }).then(() => {
3381
- if (closed || !listener.ready || !roomDraftListeners.has(listener))
4959
+ if (closed || !roomDraftListeners.has(listener))
3382
4960
  return;
3383
- listener.notify(snapshotRoomDrafts());
4961
+ const snapshot = snapshotRoomDrafts();
4962
+ draftsShown = draftsShown.then(async () => {
4963
+ const value = await snapshot.catch(() => null);
4964
+ if (value === null || closed || !roomDraftListeners.has(listener))
4965
+ return;
4966
+ listener.ready = true;
4967
+ listener.notify(value);
4968
+ });
3384
4969
  }, () => { });
3385
4970
  return () => {
3386
4971
  roomDraftListeners.delete(listener);
@@ -3413,12 +4998,13 @@ export function composeRuntime(options) {
3413
4998
  };
3414
4999
  }
3415
5000
  if (topic === 'readiness') {
3416
- return watchReadiness({ target: '', code: 'stored', capacity: null, search: 'unused' }, null, deliver);
5001
+ return watchReadiness({ target: '', code: 'stored', space: null, search: 'unused' }, null, deliver);
3417
5002
  }
3418
5003
  if (topic === 'rooms') {
3419
- roomListeners.add(deliver);
3420
- deliver(listed());
5004
+ unlistedRooms.add(deliver);
5005
+ emitKnownRooms();
3421
5006
  return () => {
5007
+ unlistedRooms.delete(deliver);
3422
5008
  roomListeners.delete(deliver);
3423
5009
  };
3424
5010
  }
@@ -3472,17 +5058,27 @@ export function composeRuntime(options) {
3472
5058
  return openLocal().then(() => undefined);
3473
5059
  },
3474
5060
  rooms() {
3475
- return listed();
5061
+ if (closed)
5062
+ return Promise.resolve([]);
5063
+ return new Promise((settle) => {
5064
+ const answer = (value) => {
5065
+ unlistedRooms.delete(answer);
5066
+ roomListeners.delete(answer);
5067
+ settle(value);
5068
+ };
5069
+ unlistedRooms.add(answer);
5070
+ emitKnownRooms();
5071
+ });
3476
5072
  },
3477
5073
  query: ((input) => rowsQuery(planned(input, null))),
3478
- search: (input) => searched(input, null),
5074
+ search: (input) => searched(input, null, []),
3479
5075
  types(locator) {
3480
5076
  return {
3481
5077
  collection: modelTypes(modelOf(locator)),
3482
5078
  typedSelection: rooms.get(locator)?.typedSelection === undefined ||
3483
5079
  rooms.get(locator)?.typedSelection === null
3484
5080
  ? null
3485
- : modelTypes(modelOf(locator, 'typed-selection')),
5081
+ : modelTypes(builtIn),
3486
5082
  };
3487
5083
  },
3488
5084
  roomDraft: {
@@ -3495,46 +5091,32 @@ export function composeRuntime(options) {
3495
5091
  return listed;
3496
5092
  });
3497
5093
  },
3498
- read(room, path) {
5094
+ async read(room, path) {
3499
5095
  const record = roomDrafts.get(room)?.get(path);
3500
- return record === undefined ? null : roomDraftOf(record);
5096
+ return record === undefined ? null : ((await roomDraftsOf([record]))[0] ?? null);
3501
5097
  },
3502
- write(room, path, revision, input) {
3503
- return exclusive(async () => {
3504
- const parts = readParts(input);
3505
- if (path === '' || normalize(path) !== path)
3506
- throw runtimeError('room-draft-malformed');
3507
- const local = await openLocal();
3508
- const current = roomDrafts.get(room)?.get(path);
3509
- if ((current?.revision ?? null) !== revision) {
3510
- return staleWrite(current === undefined ? null : roomDraftOf(current));
3511
- }
3512
- const file = parts.file === undefined
3513
- ? undefined
3514
- : await heldFile(parts.file.blob, binaryBase(replicas.get(room)?.visible, path));
3515
- const record = {
3516
- locator: room,
3517
- path,
3518
- revision: roomDraftRevision(),
3519
- parts: held(current?.parts, parts, file),
3520
- returned: current?.returned ?? null,
3521
- };
3522
- await local.keepRoomDraft(record);
3523
- rememberRoomDraft(record);
3524
- publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
3525
- return { outcome: 'written', roomDraft: roomDraftOf(record) };
3526
- });
5098
+ write(room, path, revision, input, next = roomDraftRevision()) {
5099
+ if (typeof next !== 'string' || next === '')
5100
+ return Promise.reject(runtimeError('room-draft-malformed'));
5101
+ return gathered(`room-draft\u0000${room}\u0000${path}`, { revision, input, next }, (earlier, later) => {
5102
+ if (later.revision !== earlier.next)
5103
+ return null;
5104
+ const parts = gatheredParts(earlier.input, later.input);
5105
+ return parts === null
5106
+ ? null
5107
+ : { revision: earlier.revision, input: parts, next: later.next };
5108
+ }, (written) => writtenRoomDraft(room, path, written.revision, written.input, written.next));
3527
5109
  },
3528
5110
  discard(room, path, revision) {
3529
5111
  return exclusive(async () => {
3530
5112
  const local = await openLocal();
3531
5113
  const current = roomDrafts.get(room)?.get(path);
3532
5114
  if (current === undefined || current.revision !== revision) {
3533
- return staleWrite(current === undefined ? null : roomDraftOf(current));
5115
+ return staleWrite(current === undefined ? null : ((await roomDraftsOf([current]))[0] ?? null));
3534
5116
  }
3535
5117
  await local.discardRoomDraft(room, path);
3536
5118
  forgetRoomDraft(room, path);
3537
- publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
5119
+ publishChangeSet(changeSet(room, { drafts: [path] }));
3538
5120
  moveTo({ ...status, ...attention() });
3539
5121
  return { outcome: 'written', roomDraft: null };
3540
5122
  });
@@ -3550,13 +5132,13 @@ export function composeRuntime(options) {
3550
5132
  });
3551
5133
  },
3552
5134
  write(app, value) {
3553
- return keepPresentation(app, () => structuredClone(value));
5135
+ return exclusive(() => keptPresentation(app, () => structuredClone(value)));
3554
5136
  },
3555
5137
  edit(app, members) {
3556
- return keepPresentation(app, (held) => Object.fromEntries([
3557
- ...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(members, member)),
3558
- ...Object.entries(structuredClone(members)).filter(([, value]) => value !== null),
3559
- ]));
5138
+ return gathered(`presentation\u0000${app}`, structuredClone(members), (earlier, later) => ({ ...earlier, ...later }), (edited) => keptPresentation(app, (held) => Object.fromEntries([
5139
+ ...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(edited, member)),
5140
+ ...Object.entries(edited).filter(([, value]) => value !== null),
5141
+ ])));
3560
5142
  },
3561
5143
  },
3562
5144
  save(room, named, writtenBy) {
@@ -3568,15 +5150,22 @@ export function composeRuntime(options) {
3568
5150
  history(locator, path, query) {
3569
5151
  return readHistory(locator, path, query);
3570
5152
  },
5153
+ resolve(room, path, destination) {
5154
+ return resolveReference(room, path, destination);
5155
+ },
3571
5156
  binaryFile: {
5157
+ part: readPart,
3572
5158
  read: readBinaryPath,
3573
5159
  link: linkBinaryPath,
3574
- readReference: readBinaryReference,
5160
+ show: showBinaryPath,
5161
+ attach: attachBinary,
3575
5162
  },
3576
- call(name, input) {
3577
- return recoveredAction(name, input);
5163
+ async call(name, input) {
5164
+ await ready;
5165
+ const room = calledRooms(name, input).find((one) => locked(one));
5166
+ return room === undefined ? recoveredAction(name, input) : lockedRoom(room);
3578
5167
  },
3579
- reached(locator, roomVersion, path) {
5168
+ async reached(locator, roomVersion, path) {
3580
5169
  return path === undefined
3581
5170
  ? reachedBy(locator, roomVersion)
3582
5171
  : writtenAfter(locator, roomVersion, path);
@@ -3584,14 +5173,16 @@ export function composeRuntime(options) {
3584
5173
  log() {
3585
5174
  return journal.export();
3586
5175
  },
5176
+ cleanUp,
3587
5177
  persistence() {
3588
5178
  return persistence === 'granted';
3589
5179
  },
3590
5180
  async retry() {
3591
5181
  journal.record('retry');
3592
- unindexable.clear();
5182
+ unpreparable.clear();
3593
5183
  queueReadiness();
3594
- resume();
5184
+ if (full)
5185
+ await probed();
3595
5186
  },
3596
5187
  observe: observing(watch),
3597
5188
  async reauthorize() {
@@ -3613,12 +5204,16 @@ export function composeRuntime(options) {
3613
5204
  const reachable = {
3614
5205
  md,
3615
5206
  watch,
3616
- referrerScope: (referrer) => options.adapter.referrerScope(referrer),
3617
5207
  plan: planned,
3618
5208
  query: (plan) => rowsQuery(plan),
3619
5209
  watchPlan,
3620
5210
  search: searched,
3621
5211
  watchReadiness,
5212
+ locked,
5213
+ lockedRooms,
5214
+ watchLocks,
5215
+ located,
5216
+ room: (locator) => listed().find((one) => one.locator === locator),
3622
5217
  };
3623
5218
  return {
3624
5219
  md,
@@ -3633,6 +5228,11 @@ export function composeRuntime(options) {
3633
5228
  journal.record(`persistence ${answer}`);
3634
5229
  queueReadiness();
3635
5230
  },
3636
- within: (reach, port) => narrowed(reachable, reach, port),
5231
+ shown: screen,
5232
+ within: (reach, port) => {
5233
+ if (full)
5234
+ void probed();
5235
+ return narrowed(reachable, reach, port);
5236
+ },
3637
5237
  };
3638
5238
  }