@agentshouse/mdruntime 0.1.0-ce531b51ff088 → 0.1.0-ce5c5bb29be5a

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 (185) hide show
  1. package/README.md +24 -11
  2. package/dist/adapter.d.ts +172 -48
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapter.js +1 -9
  5. package/dist/attached-model.d.ts +15 -0
  6. package/dist/attached-model.d.ts.map +1 -0
  7. package/dist/attached-model.js +81 -0
  8. package/dist/attempt.d.ts +16 -0
  9. package/dist/attempt.d.ts.map +1 -0
  10. package/dist/attempt.js +70 -0
  11. package/dist/binary.d.ts +18 -0
  12. package/dist/binary.d.ts.map +1 -0
  13. package/dist/binary.js +8 -0
  14. package/dist/bootstrap.d.ts +36 -6
  15. package/dist/bootstrap.d.ts.map +1 -1
  16. package/dist/bootstrap.js +199 -48
  17. package/dist/chain.d.ts +11 -0
  18. package/dist/chain.d.ts.map +1 -0
  19. package/dist/chain.js +64 -0
  20. package/dist/change-set.d.ts +9 -14
  21. package/dist/change-set.d.ts.map +1 -1
  22. package/dist/change-set.js +11 -56
  23. package/dist/change.d.ts +45 -0
  24. package/dist/change.d.ts.map +1 -1
  25. package/dist/change.js +34 -0
  26. package/dist/client.d.ts +37 -15
  27. package/dist/client.d.ts.map +1 -1
  28. package/dist/client.js +159 -29
  29. package/dist/collection.d.ts +12 -14
  30. package/dist/collection.d.ts.map +1 -1
  31. package/dist/collection.js +52 -56
  32. package/dist/compile.d.ts +16 -0
  33. package/dist/compile.d.ts.map +1 -0
  34. package/dist/compile.js +773 -0
  35. package/dist/connection.d.ts +67 -28
  36. package/dist/connection.d.ts.map +1 -1
  37. package/dist/connection.js +3911 -1044
  38. package/dist/conversation.d.ts +59 -18
  39. package/dist/conversation.d.ts.map +1 -1
  40. package/dist/conversation.js +849 -248
  41. package/dist/digest.d.ts +7 -0
  42. package/dist/digest.d.ts.map +1 -0
  43. package/dist/digest.js +113 -0
  44. package/dist/durability.d.ts +4 -1
  45. package/dist/durability.d.ts.map +1 -1
  46. package/dist/durability.js +5 -0
  47. package/dist/entry.d.ts +20 -0
  48. package/dist/entry.d.ts.map +1 -0
  49. package/dist/entry.js +151 -0
  50. package/dist/expectation.d.ts +21 -0
  51. package/dist/expectation.d.ts.map +1 -0
  52. package/dist/expectation.js +70 -0
  53. package/dist/failure.d.ts +13 -0
  54. package/dist/failure.d.ts.map +1 -0
  55. package/dist/failure.js +19 -0
  56. package/dist/fence.d.ts +6 -0
  57. package/dist/fence.d.ts.map +1 -0
  58. package/dist/fence.js +10 -0
  59. package/dist/file.d.ts +2 -19
  60. package/dist/file.d.ts.map +1 -1
  61. package/dist/file.js +7 -122
  62. package/dist/fulltext.d.ts +27 -0
  63. package/dist/fulltext.d.ts.map +1 -0
  64. package/dist/fulltext.js +370 -0
  65. package/dist/host.d.ts +20 -2
  66. package/dist/host.d.ts.map +1 -1
  67. package/dist/host.js +166 -48
  68. package/dist/identity.d.ts +4 -0
  69. package/dist/identity.d.ts.map +1 -1
  70. package/dist/identity.js +16 -1
  71. package/dist/index.d.ts +20 -9
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +9 -0
  74. package/dist/live.d.ts +22 -0
  75. package/dist/live.d.ts.map +1 -0
  76. package/dist/live.js +124 -0
  77. package/dist/media.d.ts +8 -0
  78. package/dist/media.d.ts.map +1 -0
  79. package/dist/media.js +218 -0
  80. package/dist/memory.d.ts +22 -8
  81. package/dist/memory.d.ts.map +1 -1
  82. package/dist/memory.js +208 -47
  83. package/dist/messages.d.ts +28 -0
  84. package/dist/messages.d.ts.map +1 -0
  85. package/dist/messages.js +90 -0
  86. package/dist/model.d.ts +22 -14
  87. package/dist/model.d.ts.map +1 -1
  88. package/dist/model.js +13 -4
  89. package/dist/name.d.ts +4 -0
  90. package/dist/name.d.ts.map +1 -0
  91. package/dist/name.js +122 -0
  92. package/dist/observation.d.ts +21 -1
  93. package/dist/observation.d.ts.map +1 -1
  94. package/dist/observation.js +128 -17
  95. package/dist/open.d.ts +2 -0
  96. package/dist/open.d.ts.map +1 -0
  97. package/dist/open.js +1 -0
  98. package/dist/opened.d.ts +22 -0
  99. package/dist/opened.d.ts.map +1 -0
  100. package/dist/opened.js +107 -0
  101. package/dist/outcome.d.ts +72 -5
  102. package/dist/outcome.d.ts.map +1 -1
  103. package/dist/outcome.js +81 -1
  104. package/dist/overlay.d.ts +15 -5
  105. package/dist/overlay.d.ts.map +1 -1
  106. package/dist/overlay.js +136 -180
  107. package/dist/paths.d.ts +1 -1
  108. package/dist/paths.d.ts.map +1 -1
  109. package/dist/paths.js +4 -16
  110. package/dist/query.d.ts +78 -7
  111. package/dist/query.d.ts.map +1 -1
  112. package/dist/query.js +126 -43
  113. package/dist/reach.d.ts +54 -0
  114. package/dist/reach.d.ts.map +1 -0
  115. package/dist/reach.js +448 -0
  116. package/dist/react.d.ts +43 -0
  117. package/dist/react.d.ts.map +1 -0
  118. package/dist/react.js +54 -0
  119. package/dist/readiness.d.ts +52 -0
  120. package/dist/readiness.d.ts.map +1 -0
  121. package/dist/readiness.js +27 -0
  122. package/dist/recovery.d.ts +26 -0
  123. package/dist/recovery.d.ts.map +1 -0
  124. package/dist/recovery.js +224 -0
  125. package/dist/refresh.d.ts +19 -7
  126. package/dist/refresh.d.ts.map +1 -1
  127. package/dist/refresh.js +125 -53
  128. package/dist/replica.d.ts +58 -33
  129. package/dist/replica.d.ts.map +1 -1
  130. package/dist/replica.js +320 -158
  131. package/dist/room-draft.d.ts +60 -10
  132. package/dist/room-draft.d.ts.map +1 -1
  133. package/dist/room-draft.js +227 -42
  134. package/dist/rows.d.ts +39 -34
  135. package/dist/rows.d.ts.map +1 -1
  136. package/dist/rows.js +195 -81
  137. package/dist/save.d.ts +74 -6
  138. package/dist/save.d.ts.map +1 -1
  139. package/dist/save.js +898 -181
  140. package/dist/search.d.ts +41 -29
  141. package/dist/search.d.ts.map +1 -1
  142. package/dist/search.js +37 -105
  143. package/dist/seed.d.ts +16 -0
  144. package/dist/seed.d.ts.map +1 -0
  145. package/dist/seed.js +75 -0
  146. package/dist/shared.d.ts +15 -0
  147. package/dist/shared.d.ts.map +1 -0
  148. package/dist/shared.js +421 -0
  149. package/dist/status.d.ts +7 -2
  150. package/dist/status.d.ts.map +1 -1
  151. package/dist/status.js +3 -9
  152. package/dist/store.d.ts +109 -59
  153. package/dist/store.d.ts.map +1 -1
  154. package/dist/store.js +323 -344
  155. package/dist/stored.d.ts +130 -0
  156. package/dist/stored.d.ts.map +1 -0
  157. package/dist/stored.js +560 -0
  158. package/dist/tables.d.ts +325 -0
  159. package/dist/tables.d.ts.map +1 -0
  160. package/dist/tables.js +578 -0
  161. package/dist/transfer.d.ts +0 -5
  162. package/dist/transfer.d.ts.map +1 -1
  163. package/dist/turn.d.ts +9 -0
  164. package/dist/turn.d.ts.map +1 -0
  165. package/dist/turn.js +75 -0
  166. package/dist/validation.d.ts +29 -0
  167. package/dist/validation.d.ts.map +1 -0
  168. package/dist/validation.js +101 -0
  169. package/dist/windows.d.ts +22 -0
  170. package/dist/windows.d.ts.map +1 -0
  171. package/dist/windows.js +89 -0
  172. package/dist/wire.d.ts +194 -19
  173. package/dist/wire.d.ts.map +1 -1
  174. package/dist/wire.js +90 -61
  175. package/dist/words.d.ts +19 -0
  176. package/dist/words.d.ts.map +1 -0
  177. package/dist/words.js +156 -0
  178. package/package.json +33 -10
  179. package/AGENTS.md +0 -207
  180. package/dist/attachment.d.ts +0 -3
  181. package/dist/attachment.d.ts.map +0 -1
  182. package/dist/attachment.js +0 -8
  183. package/dist/read-side.d.ts +0 -14
  184. package/dist/read-side.d.ts.map +0 -1
  185. package/dist/read-side.js +0 -371
package/dist/replica.js CHANGED
@@ -1,159 +1,195 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { immutable } from './change.js';
3
- import { parseCollection, readCollection, validateCollection, } from './collection.js';
4
- import { overlay, overlayChanged, sameLayerText } from './overlay.js';
5
- import { providerRewritten } from './save.js';
6
- import { equalValue } from './room-draft.js';
7
- export function compositionOf(record) {
1
+ import { equalValue, parse } from '@agentshouse/mdmodel';
2
+ import { immutable, landing } from './change.js';
3
+ import { collectionOf } from './collection.js';
4
+ import { storedModel } from './model.js';
5
+ import { acceptedBinaries, overlaying } from './overlay.js';
6
+ import { effectiveIn, rewriting } from './save.js';
7
+ import { ROW_WEIGHT } from './turn.js';
8
+ export function compositionOf(record, builtIn) {
8
9
  return {
9
10
  model: record.model,
10
11
  selection: record.typedSelection === null
11
12
  ? null
12
- : { roots: record.typedSelection.roots, model: record.selectionModel },
13
+ : { roots: record.typedSelection.roots, model: builtIn },
13
14
  };
14
15
  }
15
- export function readConfirmed(record, previous) {
16
- const read = readCollection(record.documents, compositionOf(record), previous === undefined
17
- ? undefined
18
- : { text: previous.record.documents, collection: previous.collection });
19
- return read.kind === 'read'
20
- ? { kind: 'staged', confirmed: { record, collection: read.collection } }
21
- : read;
16
+ export function storedRecord(record) {
17
+ const { key: _key, ...stored } = record;
18
+ return JSON.stringify({ ...stored, model: storedModel(stored.model) });
22
19
  }
23
- export function sameMembership(record, other) {
24
- return (JSON.stringify(record.typedSelection?.roots ?? null) ===
25
- JSON.stringify(other.typedSelection?.roots ?? null));
20
+ const PROBES = new Set([
21
+ 'then',
22
+ 'toJSON',
23
+ 'constructor',
24
+ 'valueOf',
25
+ 'toString',
26
+ ]);
27
+ class UnreadPath extends Error {
28
+ constructor(path) {
29
+ super(`the replica slice holds no row for ${path}`);
30
+ }
26
31
  }
27
- export function readStaged(record, previous) {
28
- if ((record.model.status === 'current' || record.selectionModel.status === 'current') &&
29
- (previous === undefined ||
30
- !sameLayerText(record, previous.record) ||
31
- record.model !== previous.record.model ||
32
- record.selectionModel !== previous.record.selectionModel)) {
33
- const verdict = validateCollection(record.documents, record.binaryTargets, compositionOf(record));
34
- if (verdict.verdict === 'refuse' && verdict.rule !== 'model-unavailable') {
35
- return { kind: 'refused', refusal: verdict };
32
+ function strict(values, held) {
33
+ const guard = (property) => {
34
+ if (typeof property === 'string' && !held.has(property) && !PROBES.has(property)) {
35
+ throw new UnreadPath(property);
36
36
  }
37
- }
38
- return readConfirmed(record, previous);
37
+ };
38
+ return new Proxy(values, {
39
+ get(target, property, receiver) {
40
+ guard(property);
41
+ return Reflect.get(target, property, receiver);
42
+ },
43
+ has(target, property) {
44
+ guard(property);
45
+ return Reflect.has(target, property);
46
+ },
47
+ getOwnPropertyDescriptor(target, property) {
48
+ guard(property);
49
+ return Reflect.getOwnPropertyDescriptor(target, property);
50
+ },
51
+ });
39
52
  }
40
- export function readReclassified(record, previous) {
41
- const verdict = validateCollection(record.documents, record.binaryTargets, compositionOf(record));
42
- if (verdict.verdict !== 'refuse')
43
- return readConfirmed(record, previous);
44
- return verdict.rule === 'model-unavailable'
45
- ? { kind: 'deferred' }
46
- : { kind: 'refused', refusal: verdict };
53
+ export function* slicing(entries, paths) {
54
+ const held = new Set(paths);
55
+ for (const entry of entries)
56
+ held.add(entry.path);
57
+ const documents = {};
58
+ const binaries = {};
59
+ const revisions = {};
60
+ const fileIds = {};
61
+ const changes = {};
62
+ const written = {};
63
+ for (const entry of entries) {
64
+ if (entry.binary !== null)
65
+ binaries[entry.path] = entry.binary;
66
+ else if (entry.content !== null)
67
+ documents[entry.path] = entry.content;
68
+ if (entry.revision !== null)
69
+ revisions[entry.path] = entry.revision;
70
+ if (entry.fileId !== null)
71
+ fileIds[entry.path] = entry.fileId;
72
+ if (entry.change !== null)
73
+ changes[entry.path] = entry.change;
74
+ if (entry.written !== null)
75
+ written[entry.path] = entry.written;
76
+ yield ROW_WEIGHT;
77
+ }
78
+ return {
79
+ documents: strict(documents, held),
80
+ binaries: strict(binaries, held),
81
+ revisions: strict(revisions, held),
82
+ fileIds: strict(fileIds, held),
83
+ changes: strict(changes, held),
84
+ written: strict(written, held),
85
+ held,
86
+ };
47
87
  }
48
- export function applyReturned(confirmed, documents, revisions, removals, roomVersion) {
49
- const nextDocuments = { ...confirmed.record.documents, ...documents };
50
- const nextRevisions = { ...confirmed.record.revisions, ...revisions };
51
- const nextChanges = { ...confirmed.record.changes };
52
- const nextWritten = {
53
- ...confirmed.record.written,
54
- ...Object.fromEntries(Object.keys(documents).map((path) => [path, roomVersion])),
88
+ export function entryAt(layer, path) {
89
+ const content = layer.documents[path];
90
+ const binary = layer.binaries[path];
91
+ if (content === undefined && binary === undefined)
92
+ return null;
93
+ return {
94
+ path,
95
+ content: content ?? null,
96
+ binary: binary ?? null,
97
+ revision: layer.revisions[path] ?? null,
98
+ fileId: layer.fileIds[path] ?? null,
99
+ change: layer.changes[path] ?? null,
100
+ written: layer.written?.[path] ?? null,
55
101
  };
56
- const binaryTargets = new Set(confirmed.record.binaryTargets);
57
- for (const [path, revision] of Object.entries(revisions)) {
58
- if (confirmed.record.revisions[path] !== revision)
59
- delete nextChanges[path];
102
+ }
103
+ export function* slicingWith(slice, set, removed) {
104
+ const entries = new Map();
105
+ for (const path of slice.held) {
106
+ const entry = entryAt(slice, path);
107
+ if (entry !== null)
108
+ entries.set(path, { ...entry, type: null });
109
+ yield ROW_WEIGHT;
60
110
  }
61
- for (const path of removals) {
62
- delete nextDocuments[path];
63
- delete nextRevisions[path];
64
- delete nextChanges[path];
65
- delete nextWritten[path];
66
- binaryTargets.delete(path);
111
+ for (const path of removed)
112
+ entries.delete(path);
113
+ for (const entry of set)
114
+ entries.set(entry.path, { ...entry, type: null });
115
+ return yield* slicing([...entries.values()], [...slice.held, ...set.map(({ path }) => path), ...removed]);
116
+ }
117
+ function changePaths(change) {
118
+ return change.kind === 'move' ? [change.path, change.to] : [change.path];
119
+ }
120
+ export function touchedBy(queue) {
121
+ const paths = new Set();
122
+ for (const batch of queue) {
123
+ for (const change of batch.changes)
124
+ for (const path of changePaths(change))
125
+ paths.add(path);
126
+ for (const precondition of batch.preconditions)
127
+ paths.add(precondition.path);
128
+ for (const move of batch.between?.moves ?? [])
129
+ paths.add(move.path);
67
130
  }
68
- return readStaged({
69
- ...confirmed.record,
70
- roomVersion,
71
- documents: nextDocuments,
72
- revisions: nextRevisions,
73
- changes: nextChanges,
74
- written: nextWritten,
75
- binaryTargets: [...binaryTargets].sort(),
76
- }, confirmed);
131
+ return paths;
77
132
  }
78
- function predicted(confirmed, batches, previous) {
79
- const composition = compositionOf(confirmed.record);
80
- let layer = confirmed.record;
81
- const replay = [];
82
- const prior = new Map(previous?.replay.map((step) => [step.id, step]) ?? []);
83
- for (const batch of batches) {
84
- const before = layer;
85
- const old = prior.get(batch.id);
86
- const hasMove = batch.changes.some((change) => change.kind === 'move');
87
- const reuseRewrite = old?.collection !== undefined &&
88
- sameLayerText(before, old.before) &&
89
- old.collection.composition.model === composition.model &&
90
- old.collection.composition.selection?.model === composition.selection?.model &&
91
- JSON.stringify(old.collection.composition.selection?.roots) ===
92
- JSON.stringify(composition.selection?.roots);
93
- const collection = hasMove
94
- ? reuseRewrite
95
- ? old.collection
96
- : parseCollection(before.documents, composition, old?.collection === undefined
97
- ? { text: confirmed.record.documents, collection: confirmed.collection }
98
- : { text: old.before.documents, collection: old.collection })
99
- : undefined;
100
- const effective = collection === undefined
101
- ? batch
102
- : reuseRewrite
103
- ? old.effective
104
- : providerRewritten(batch, { ...before, collection });
105
- layer =
106
- old === undefined
107
- ? overlay(before, [effective])
108
- : overlayChanged(before, [effective], old.before, [old.effective], old.after);
109
- replay.push({
110
- id: batch.id,
111
- before,
112
- after: layer,
113
- effective,
114
- ...(collection === undefined ? {} : { collection }),
115
- });
133
+ export function movedBy(queue, locator) {
134
+ const paths = new Set();
135
+ for (const batch of queue) {
136
+ if (!rewriting(batch, locator))
137
+ continue;
138
+ for (const change of batch.changes)
139
+ if (change.kind === 'move')
140
+ paths.add(change.path);
141
+ if (batch.between?.source.room === locator) {
142
+ for (const move of batch.between.moves)
143
+ paths.add(move.path);
144
+ }
116
145
  }
117
- return { layer, replay };
146
+ return paths;
118
147
  }
119
- export function replicaOf(confirmed, queue, rewriting, previous) {
120
- const batches = Object.freeze(queue.map(immutable));
121
- if (batches.length === 0) {
122
- return {
123
- confirmed,
124
- queue: batches,
125
- visible: {
126
- documents: confirmed.record.documents,
127
- binaryTargets: confirmed.record.binaryTargets,
128
- revisions: confirmed.record.revisions,
129
- changes: confirmed.record.changes,
130
- collection: confirmed.collection,
131
- },
132
- replay: [],
133
- };
148
+ export function* replaying(confirmed, queue, locator, composition) {
149
+ let layer = confirmed;
150
+ const touched = new Set();
151
+ for (const batch of queue) {
152
+ const effective = rewriting(batch, locator)
153
+ ? yield* effectiveIn(batch, locator, {
154
+ ...layer,
155
+ collection: collectionOf(layer.documents, composition),
156
+ })
157
+ : batch;
158
+ for (const change of effective.changes)
159
+ for (const path of changePaths(change))
160
+ touched.add(path);
161
+ layer = yield* overlaying(layer, [effective]);
162
+ yield ROW_WEIGHT;
134
163
  }
135
- const predictedState = rewriting === 'provider' ? predicted(confirmed, batches, previous) : null;
136
- const layer = rewriting === 'provider'
137
- ? predictedState.layer
138
- : previous === undefined
139
- ? overlay(confirmed.record, batches)
140
- : overlayChanged(confirmed.record, batches, previous.confirmed.record, previous.queue, previous.visible);
141
164
  return {
142
- confirmed,
143
- queue: batches,
144
- replay: predictedState?.replay ?? [],
145
- visible: {
146
- ...layer,
147
- collection: parseCollection(layer.documents, compositionOf(confirmed.record), previous === undefined
148
- ? undefined
149
- : { text: previous.visible.documents, collection: previous.visible.collection }),
150
- },
165
+ visible: { ...layer, collection: collectionOf(layer.documents, composition) },
166
+ touched,
151
167
  };
152
168
  }
169
+ export function* overlayOf(visible, touched) {
170
+ const rows = [];
171
+ for (const path of touched) {
172
+ yield ROW_WEIGHT;
173
+ const entry = entryAt(visible, path);
174
+ rows.push(entry === null
175
+ ? {
176
+ path,
177
+ present: false,
178
+ content: null,
179
+ binary: null,
180
+ revision: null,
181
+ fileId: null,
182
+ change: null,
183
+ written: null,
184
+ }
185
+ : { ...entry, written: null, present: true });
186
+ }
187
+ return rows;
188
+ }
153
189
  function present(layer, path) {
154
- return layer.documents[path] !== undefined || layer.binaryTargets.includes(path);
190
+ return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
155
191
  }
156
- function holds(layer, preconditions) {
192
+ export function holds(layer, preconditions) {
157
193
  const parsed = new Map();
158
194
  const documentAt = (path) => {
159
195
  if (!parsed.has(path)) {
@@ -172,6 +208,8 @@ function holds(layer, preconditions) {
172
208
  if (precondition.kind === 'revision') {
173
209
  return present(layer, path) && layer.revisions[path] === precondition.revision;
174
210
  }
211
+ if (precondition.kind === 'content')
212
+ return layer.documents[path] === precondition.content;
175
213
  const document = documentAt(path);
176
214
  if (document === null)
177
215
  return false;
@@ -216,53 +254,177 @@ function effects(change) {
216
254
  { kind: 'present', path: change.to },
217
255
  ];
218
256
  }
257
+ if (change.kind === 'binary' || change.kind === 'arrival')
258
+ return [{ kind: 'present', path }];
219
259
  return [{ kind: 'absent', path }];
220
260
  }
221
- function after(changes, index, path) {
222
- let at = path;
223
- for (const change of changes.slice(index + 1)) {
224
- if (change.path !== at)
225
- continue;
226
- if (change.kind === 'remove')
227
- return null;
228
- if (change.kind === 'move')
229
- at = change.to;
230
- }
231
- return at;
232
- }
233
- export function landed(batch, confirmed) {
234
- const layer = confirmed.record;
261
+ export function landed(batch, confirmed, current = {}) {
262
+ const documents = new Proxy(confirmed.documents, {
263
+ get(target, property, receiver) {
264
+ if (typeof property === 'string' && Object.hasOwn(current, property))
265
+ return current[property];
266
+ return Reflect.get(target, property, receiver);
267
+ },
268
+ });
269
+ const layer = { ...confirmed, documents };
235
270
  return batch.changes.every((change, index) => {
236
271
  if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
237
- const at = after(batch.changes, index, change.path);
272
+ const at = landing(batch.changes, index, change.path);
238
273
  return at === null || layer.documents[at] === change.content;
239
274
  }
275
+ if (change.kind === 'binary') {
276
+ const at = landing(batch.changes, index, change.path);
277
+ return at === null || layer.binaries[at]?.sha256 === change.sha256;
278
+ }
240
279
  return effects(change).every((effect) => {
241
280
  if (effect.kind === 'absent')
242
281
  return holds(layer, [effect]);
243
- const at = after(batch.changes, index, effect.path);
282
+ const at = landing(batch.changes, index, effect.path);
244
283
  return at === null || holds(layer, [{ ...effect, path: at }]);
245
284
  });
246
285
  });
247
286
  }
248
- export function withdrawals(confirmed, queue, withdrawable, rewriting) {
287
+ function founded(batch, layer) {
288
+ let changed = false;
289
+ const preconditions = batch.preconditions.map((precondition) => {
290
+ if (precondition.kind !== 'content')
291
+ return precondition;
292
+ const revision = layer.revisions[precondition.path];
293
+ if (revision === undefined || layer.documents[precondition.path] !== precondition.content) {
294
+ return precondition;
295
+ }
296
+ changed = true;
297
+ return { kind: 'revision', path: precondition.path, revision };
298
+ });
299
+ return changed ? immutable({ ...batch, preconditions }) : batch;
300
+ }
301
+ export function* standing(confirmed, queue, withdrawable, local, locator, composition) {
249
302
  if (!queue.some(withdrawable))
250
- return [];
251
- const composition = compositionOf(confirmed.record);
252
- let layer = confirmed.record;
303
+ return { withdrawn: [], kept: queue };
304
+ let layer = confirmed;
253
305
  const withdrawn = [];
306
+ const kept = [];
254
307
  for (const batch of queue) {
255
308
  if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
256
309
  withdrawn.push(batch);
257
310
  continue;
258
311
  }
259
- const effective = rewriting === 'provider' && batch.changes.some((change) => change.kind === 'move')
260
- ? providerRewritten(batch, {
312
+ const effective = rewriting(batch, locator)
313
+ ? yield* effectiveIn(batch, locator, {
261
314
  ...layer,
262
- collection: parseCollection(layer.documents, composition),
315
+ collection: collectionOf(layer.documents, composition),
263
316
  })
264
317
  : batch;
265
- layer = overlay(layer, [effective]);
318
+ let unapplied = false;
319
+ const next = yield* overlaying(layer, [effective], () => {
320
+ unapplied = true;
321
+ });
322
+ if (local && unapplied) {
323
+ withdrawn.push(batch);
324
+ continue;
325
+ }
326
+ kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
327
+ layer = next;
328
+ }
329
+ return { withdrawn, kept };
330
+ }
331
+ export function returnedPaths(submitted, accepted) {
332
+ const paths = new Set([
333
+ ...Object.keys(accepted.documents),
334
+ ...Object.keys(accepted.revisions),
335
+ ...Object.keys(accepted.fileIds ?? {}),
336
+ ...Object.keys(accepted.changes),
337
+ ...Object.keys(accepted.binaryFiles ?? {}),
338
+ ...accepted.removals,
339
+ ]);
340
+ for (const change of submitted.changes) {
341
+ if (change.kind === 'move') {
342
+ paths.add(change.path);
343
+ paths.add(change.to);
344
+ }
345
+ if (change.kind === 'arrival')
346
+ paths.add(change.path);
347
+ }
348
+ return paths;
349
+ }
350
+ export function* returning(confirmed, submitted, accepted, writtenAt) {
351
+ const documents = new Map();
352
+ const binaries = new Map();
353
+ const revisions = new Map();
354
+ const fileIds = new Map();
355
+ const changes = new Map();
356
+ const written = new Map();
357
+ const read = (changed, held, path) => (changed.has(path) ? changed.get(path) : held[path]);
358
+ for (const [path, content] of Object.entries(accepted.documents)) {
359
+ yield ROW_WEIGHT;
360
+ documents.set(path, content);
361
+ fileIds.set(path, undefined);
362
+ written.set(path, writtenAt);
363
+ }
364
+ for (const [path, revision] of Object.entries(accepted.revisions)) {
365
+ yield ROW_WEIGHT;
366
+ revisions.set(path, revision);
367
+ fileIds.set(path, undefined);
368
+ if (confirmed.revisions[path] !== revision)
369
+ changes.set(path, undefined);
370
+ }
371
+ for (const [path, fileId] of Object.entries(accepted.fileIds ?? {}))
372
+ fileIds.set(path, fileId);
373
+ for (const [path, change] of Object.entries(accepted.changes))
374
+ changes.set(path, change);
375
+ for (const path of accepted.removals) {
376
+ documents.set(path, undefined);
377
+ revisions.set(path, undefined);
378
+ fileIds.set(path, undefined);
379
+ changes.set(path, undefined);
380
+ written.set(path, undefined);
381
+ }
382
+ for (const change of submitted.changes) {
383
+ if (change.kind === 'arrival' && 'binary' in change) {
384
+ if (accepted.revisions[change.path] !== undefined) {
385
+ binaries.set(change.path, { state: 'accepted', ...change.binary });
386
+ }
387
+ continue;
388
+ }
389
+ if (change.kind !== 'move')
390
+ continue;
391
+ const moved = read(binaries, confirmed.binaries, change.path);
392
+ if (moved === undefined || accepted.revisions[change.to] === undefined)
393
+ continue;
394
+ binaries.set(change.to, moved);
395
+ binaries.set(change.path, undefined);
396
+ }
397
+ for (const [path, binary] of acceptedBinaries(accepted.binaryFiles ?? {}))
398
+ binaries.set(path, binary);
399
+ for (const path of accepted.removals)
400
+ binaries.set(path, undefined);
401
+ const paths = new Set([
402
+ ...documents.keys(),
403
+ ...binaries.keys(),
404
+ ...revisions.keys(),
405
+ ...fileIds.keys(),
406
+ ...changes.keys(),
407
+ ...written.keys(),
408
+ ]);
409
+ const set = [];
410
+ const removed = [];
411
+ for (const path of paths) {
412
+ yield ROW_WEIGHT;
413
+ const content = read(documents, confirmed.documents, path);
414
+ const binary = read(binaries, confirmed.binaries, path);
415
+ if (content === undefined && binary === undefined) {
416
+ removed.push(path);
417
+ continue;
418
+ }
419
+ set.push({
420
+ path,
421
+ content: binary === undefined ? (content ?? null) : null,
422
+ binary: binary ?? null,
423
+ revision: read(revisions, confirmed.revisions, path) ?? null,
424
+ fileId: read(fileIds, confirmed.fileIds, path) ?? null,
425
+ change: read(changes, confirmed.changes, path) ?? null,
426
+ written: read(written, confirmed.written, path) ?? null,
427
+ });
266
428
  }
267
- return withdrawn;
429
+ return { set, removed };
268
430
  }
@@ -1,4 +1,5 @@
1
1
  import type { ProviderRefusal } from './adapter.js';
2
+ import type { Open } from './open.js';
2
3
  import type { Change, Precondition } from './change.js';
3
4
  import type { VisibleLayer } from './replica.js';
4
5
  export type FieldValue = {
@@ -31,11 +32,24 @@ export type RoomDraftParts = {
31
32
  readonly creation?: RoomDraftCreation;
32
33
  readonly move?: {
33
34
  readonly to: string;
35
+ readonly room?: string;
34
36
  readonly base?: string | null;
35
37
  };
36
38
  readonly removal?: {
37
39
  readonly base?: string | null;
38
40
  };
41
+ readonly file?: {
42
+ readonly blob: Blob;
43
+ readonly base?: string | null;
44
+ };
45
+ };
46
+ export type HeldFile = {
47
+ readonly localId: string;
48
+ readonly name?: string;
49
+ readonly byteLength: number;
50
+ readonly mediaType: string;
51
+ readonly sha256: string;
52
+ readonly base: string | null;
39
53
  };
40
54
  type HeldParts = {
41
55
  readonly fields?: Readonly<Record<string, {
@@ -57,11 +71,13 @@ type HeldParts = {
57
71
  readonly creation?: RoomDraftCreation;
58
72
  readonly move?: {
59
73
  readonly to: string;
74
+ readonly room?: string;
60
75
  readonly base: string | null;
61
76
  };
62
77
  readonly removal?: {
63
78
  readonly base: string | null;
64
79
  };
80
+ readonly file?: HeldFile;
65
81
  };
66
82
  type ChangedUnderneath = {
67
83
  readonly fields?: Readonly<Record<string, FieldValue>>;
@@ -71,14 +87,12 @@ type ChangedUnderneath = {
71
87
  readonly creation?: string;
72
88
  readonly move?: string | null;
73
89
  readonly removal?: string | null;
90
+ readonly file?: string | null;
74
91
  };
75
- export type ReturnReason = (ProviderRefusal & {
76
- readonly kind: 'conflicted' | 'rejected';
77
- }) | {
78
- readonly kind: 'expired';
79
- } | {
80
- readonly kind: 'withdrawn';
92
+ export type ReturnReason = ProviderRefusal & {
93
+ readonly kind: Open<'conflicted' | 'rejected' | 'expired' | 'withdrawn'>;
81
94
  };
95
+ export declare function returnedBy(kind: 'expired' | 'withdrawn'): ReturnReason;
82
96
  export type RoomDraft = {
83
97
  readonly room: string;
84
98
  readonly path: string;
@@ -92,11 +106,14 @@ export type RoomDraftWrite = {
92
106
  readonly roomDraft: RoomDraft | null;
93
107
  } | {
94
108
  readonly outcome: 'stale';
109
+ readonly code: string;
110
+ readonly retryable: boolean;
95
111
  readonly current: RoomDraft | null;
96
112
  };
113
+ export declare function staleWrite(current: RoomDraft | null): RoomDraftWrite;
97
114
  export type ChangedPart = {
98
115
  readonly path: string;
99
- readonly part: 'field' | 'section' | 'preamble' | 'markdown' | 'creation' | 'move' | 'removal';
116
+ readonly part: Open<'field' | 'section' | 'preamble' | 'markdown' | 'move' | 'removal' | 'file'>;
100
117
  readonly name?: string;
101
118
  };
102
119
  export type RoomDraftRecord = {
@@ -111,14 +128,47 @@ export type SavedDraft = {
111
128
  readonly parts: HeldParts;
112
129
  };
113
130
  export declare function readParts(value: unknown): RoomDraftParts;
114
- export declare function held(current: HeldParts | undefined, parts: RoomDraftParts): HeldParts;
115
- export declare function equalValue(left: unknown, right: unknown): boolean;
131
+ export declare function gatheredParts(earlier: unknown, later: unknown): unknown;
132
+ export declare function held(current: HeldParts | undefined, parts: RoomDraftParts, file?: HeldFile): HeldParts;
116
133
  export declare function underneath(parts: HeldParts, path: string, visible: VisibleLayer | undefined): ChangedUnderneath;
117
- export declare function changedParts(path: string, report: ChangedUnderneath): readonly ChangedPart[];
134
+ export declare function heldFile(blob: Blob, base: string | null): Promise<HeldFile>;
135
+ export declare function binaryBase(visible: VisibleLayer | undefined, path: string): string | null;
136
+ export declare function changedParts(path: string, parts: HeldParts, visible: VisibleLayer | undefined): readonly ChangedPart[];
137
+ export type TextParts = {
138
+ readonly fields?: Readonly<Record<string, {
139
+ readonly value: unknown;
140
+ }>>;
141
+ readonly sections?: Readonly<Record<string, {
142
+ readonly value: string;
143
+ }>>;
144
+ readonly preamble?: {
145
+ readonly value: string;
146
+ };
147
+ };
148
+ export declare function partsAfter(markdown: string, parts: TextParts): string;
118
149
  export declare function returnedInto(existing: HeldParts | undefined, returned: HeldParts): HeldParts;
150
+ export declare function moveTarget(move: {
151
+ readonly to: string;
152
+ }): string;
153
+ export declare function readSavedParts(named: readonly {
154
+ readonly path: string;
155
+ readonly parts: unknown;
156
+ }[]): readonly {
157
+ readonly path: string;
158
+ readonly parts: RoomDraftParts;
159
+ }[];
160
+ export declare function partsChanges(named: readonly {
161
+ readonly path: string;
162
+ readonly parts: unknown;
163
+ }[]): {
164
+ readonly changes: readonly Change[];
165
+ readonly preconditions: readonly Precondition[];
166
+ };
119
167
  export declare function changesOf(path: string, parts: HeldParts): {
120
168
  changes: readonly Change[];
121
169
  bases: readonly Precondition[];
122
170
  };
171
+ export declare function joinedDraft(carried: SavedDraft, saved: readonly SavedDraft[]): SavedDraft;
172
+ export declare function withdrawnDrafts(drafts: readonly SavedDraft[], changes: readonly Change[]): readonly SavedDraft[];
123
173
  export {};
124
174
  //# sourceMappingURL=room-draft.d.ts.map