@agentshouse/mdruntime 0.1.0-ce531b51ff088 → 0.1.0-ce5b3c1e6ea87

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 (322) hide show
  1. package/README.md +25 -12
  2. package/dist/acceptance.d.ts +6 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +389 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +251 -0
  8. package/dist/adapter.d.ts +221 -62
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/adapter.js +1 -9
  11. package/dist/attached-model.d.ts +8 -0
  12. package/dist/attached-model.d.ts.map +1 -0
  13. package/dist/attached-model.js +46 -0
  14. package/dist/attempt.d.ts +16 -0
  15. package/dist/attempt.d.ts.map +1 -0
  16. package/dist/attempt.js +70 -0
  17. package/dist/binary.d.ts +18 -0
  18. package/dist/binary.d.ts.map +1 -0
  19. package/dist/binary.js +8 -0
  20. package/dist/bootstrap.d.ts +40 -6
  21. package/dist/bootstrap.d.ts.map +1 -1
  22. package/dist/bootstrap.js +336 -51
  23. package/dist/cell.d.ts +6 -0
  24. package/dist/cell.d.ts.map +1 -0
  25. package/dist/cell.js +9 -0
  26. package/dist/chain.d.ts +12 -0
  27. package/dist/chain.d.ts.map +1 -0
  28. package/dist/chain.js +64 -0
  29. package/dist/change-set.d.ts +9 -14
  30. package/dist/change-set.d.ts.map +1 -1
  31. package/dist/change-set.js +11 -56
  32. package/dist/change.d.ts +46 -0
  33. package/dist/change.d.ts.map +1 -1
  34. package/dist/change.js +34 -0
  35. package/dist/chat-types.d.ts +5 -0
  36. package/dist/chat-types.d.ts.map +1 -0
  37. package/dist/chat-types.js +13 -0
  38. package/dist/client.d.ts +55 -18
  39. package/dist/client.d.ts.map +1 -1
  40. package/dist/client.js +162 -30
  41. package/dist/collection.d.ts +13 -14
  42. package/dist/collection.d.ts.map +1 -1
  43. package/dist/collection.js +52 -56
  44. package/dist/compile.d.ts +16 -0
  45. package/dist/compile.d.ts.map +1 -0
  46. package/dist/compile.js +717 -0
  47. package/dist/connection.d.ts +8 -106
  48. package/dist/connection.d.ts.map +1 -1
  49. package/dist/connection.js +105 -2094
  50. package/dist/context.d.ts +2 -0
  51. package/dist/context.d.ts.map +1 -0
  52. package/dist/context.js +7 -0
  53. package/dist/conversation-live.d.ts +136 -0
  54. package/dist/conversation-live.d.ts.map +1 -0
  55. package/dist/conversation-live.js +248 -0
  56. package/dist/conversation-row.d.ts +33 -0
  57. package/dist/conversation-row.d.ts.map +1 -0
  58. package/dist/conversation-row.js +53 -0
  59. package/dist/conversation.d.ts +77 -40
  60. package/dist/conversation.d.ts.map +1 -1
  61. package/dist/conversation.js +1028 -438
  62. package/dist/digest.d.ts +7 -0
  63. package/dist/digest.d.ts.map +1 -0
  64. package/dist/digest.js +113 -0
  65. package/dist/discovery.d.ts +10 -0
  66. package/dist/discovery.d.ts.map +1 -0
  67. package/dist/discovery.js +175 -0
  68. package/dist/durability.d.ts +19 -1
  69. package/dist/durability.d.ts.map +1 -1
  70. package/dist/durability.js +5 -0
  71. package/dist/entry.d.ts +20 -0
  72. package/dist/entry.d.ts.map +1 -0
  73. package/dist/entry.js +161 -0
  74. package/dist/expectation.d.ts +21 -0
  75. package/dist/expectation.d.ts.map +1 -0
  76. package/dist/expectation.js +70 -0
  77. package/dist/failure.d.ts +13 -0
  78. package/dist/failure.d.ts.map +1 -0
  79. package/dist/failure.js +19 -0
  80. package/dist/figures.d.ts +48 -0
  81. package/dist/figures.d.ts.map +1 -0
  82. package/dist/figures.js +197 -0
  83. package/dist/file.d.ts +2 -19
  84. package/dist/file.d.ts.map +1 -1
  85. package/dist/file.js +7 -122
  86. package/dist/holdings.d.ts +29 -0
  87. package/dist/holdings.d.ts.map +1 -0
  88. package/dist/holdings.js +95 -0
  89. package/dist/host.d.ts +18 -2
  90. package/dist/host.d.ts.map +1 -1
  91. package/dist/host.js +165 -51
  92. package/dist/identity.d.ts +3 -0
  93. package/dist/identity.d.ts.map +1 -1
  94. package/dist/identity.js +13 -1
  95. package/dist/index.d.ts +36 -10
  96. package/dist/index.d.ts.map +1 -1
  97. package/dist/index.js +13 -0
  98. package/dist/install.d.ts +56 -0
  99. package/dist/install.d.ts.map +1 -0
  100. package/dist/install.js +560 -0
  101. package/dist/job.d.ts +33 -0
  102. package/dist/job.d.ts.map +1 -0
  103. package/dist/job.js +54 -0
  104. package/dist/lifecycle.d.ts +7 -0
  105. package/dist/lifecycle.d.ts.map +1 -0
  106. package/dist/lifecycle.js +268 -0
  107. package/dist/listing.d.ts +53 -0
  108. package/dist/listing.d.ts.map +1 -0
  109. package/dist/listing.js +175 -0
  110. package/dist/live.d.ts +23 -0
  111. package/dist/live.d.ts.map +1 -0
  112. package/dist/live.js +165 -0
  113. package/dist/locks.d.ts +9 -0
  114. package/dist/locks.d.ts.map +1 -0
  115. package/dist/locks.js +61 -0
  116. package/dist/log.d.ts +1 -0
  117. package/dist/log.d.ts.map +1 -1
  118. package/dist/lost.d.ts +5 -0
  119. package/dist/lost.d.ts.map +1 -0
  120. package/dist/lost.js +61 -0
  121. package/dist/media.d.ts +8 -0
  122. package/dist/media.d.ts.map +1 -0
  123. package/dist/media.js +218 -0
  124. package/dist/memory.d.ts +30 -9
  125. package/dist/memory.d.ts.map +1 -1
  126. package/dist/memory.js +337 -86
  127. package/dist/messages.d.ts +52 -0
  128. package/dist/messages.d.ts.map +1 -0
  129. package/dist/messages.js +168 -0
  130. package/dist/model.d.ts +17 -16
  131. package/dist/model.d.ts.map +1 -1
  132. package/dist/model.js +9 -9
  133. package/dist/models.d.ts +14 -0
  134. package/dist/models.d.ts.map +1 -0
  135. package/dist/models.js +193 -0
  136. package/dist/name.d.ts +4 -0
  137. package/dist/name.d.ts.map +1 -0
  138. package/dist/name.js +122 -0
  139. package/dist/observation.d.ts +22 -2
  140. package/dist/observation.d.ts.map +1 -1
  141. package/dist/observation.js +133 -17
  142. package/dist/open.d.ts +2 -0
  143. package/dist/open.d.ts.map +1 -0
  144. package/dist/open.js +1 -0
  145. package/dist/opened.d.ts +22 -0
  146. package/dist/opened.d.ts.map +1 -0
  147. package/dist/opened.js +107 -0
  148. package/dist/opening.d.ts +4 -0
  149. package/dist/opening.d.ts.map +1 -0
  150. package/dist/opening.js +57 -0
  151. package/dist/outcome.d.ts +75 -5
  152. package/dist/outcome.d.ts.map +1 -1
  153. package/dist/outcome.js +84 -1
  154. package/dist/overlay.d.ts +16 -5
  155. package/dist/overlay.d.ts.map +1 -1
  156. package/dist/overlay.js +147 -180
  157. package/dist/parts.d.ts +59 -0
  158. package/dist/parts.d.ts.map +1 -0
  159. package/dist/parts.js +118 -0
  160. package/dist/paths.d.ts +1 -1
  161. package/dist/paths.d.ts.map +1 -1
  162. package/dist/paths.js +4 -16
  163. package/dist/preparation.d.ts +20 -0
  164. package/dist/preparation.d.ts.map +1 -0
  165. package/dist/preparation.js +226 -0
  166. package/dist/queries.d.ts +16 -0
  167. package/dist/queries.d.ts.map +1 -0
  168. package/dist/queries.js +272 -0
  169. package/dist/query.d.ts +56 -180
  170. package/dist/query.d.ts.map +1 -1
  171. package/dist/query.js +3 -422
  172. package/dist/questions.d.ts +34 -0
  173. package/dist/questions.d.ts.map +1 -0
  174. package/dist/questions.js +122 -0
  175. package/dist/reach.d.ts +56 -0
  176. package/dist/reach.d.ts.map +1 -0
  177. package/dist/reach.js +494 -0
  178. package/dist/react.d.ts +51 -0
  179. package/dist/react.d.ts.map +1 -0
  180. package/dist/react.js +54 -0
  181. package/dist/read.d.ts +7 -0
  182. package/dist/read.d.ts.map +1 -0
  183. package/dist/read.js +17 -0
  184. package/dist/readiness.d.ts +50 -0
  185. package/dist/readiness.d.ts.map +1 -0
  186. package/dist/readiness.js +27 -0
  187. package/dist/reads.d.ts +37 -0
  188. package/dist/reads.d.ts.map +1 -0
  189. package/dist/reads.js +171 -0
  190. package/dist/recovery.d.ts +26 -0
  191. package/dist/recovery.d.ts.map +1 -0
  192. package/dist/recovery.js +232 -0
  193. package/dist/refresh.d.ts +21 -7
  194. package/dist/refresh.d.ts.map +1 -1
  195. package/dist/refresh.js +155 -55
  196. package/dist/refusal.d.ts +5 -0
  197. package/dist/refusal.d.ts.map +1 -0
  198. package/dist/refusal.js +56 -0
  199. package/dist/replica.d.ts +58 -33
  200. package/dist/replica.d.ts.map +1 -1
  201. package/dist/replica.js +356 -158
  202. package/dist/room-draft.d.ts +62 -11
  203. package/dist/room-draft.d.ts.map +1 -1
  204. package/dist/room-draft.js +281 -83
  205. package/dist/room-drafts.d.ts +24 -0
  206. package/dist/room-drafts.d.ts.map +1 -0
  207. package/dist/room-drafts.js +216 -0
  208. package/dist/rows.d.ts +32 -34
  209. package/dist/rows.d.ts.map +1 -1
  210. package/dist/rows.js +65 -87
  211. package/dist/runtime.d.ts +43 -0
  212. package/dist/runtime.d.ts.map +1 -0
  213. package/dist/runtime.js +1 -0
  214. package/dist/save.d.ts +76 -6
  215. package/dist/save.d.ts.map +1 -1
  216. package/dist/save.js +904 -182
  217. package/dist/saves.d.ts +70 -0
  218. package/dist/saves.d.ts.map +1 -0
  219. package/dist/saves.js +1006 -0
  220. package/dist/search.d.ts +15 -54
  221. package/dist/search.d.ts.map +1 -1
  222. package/dist/search.js +10 -121
  223. package/dist/seed.d.ts +25 -0
  224. package/dist/seed.d.ts.map +1 -0
  225. package/dist/seed.js +146 -0
  226. package/dist/shared.d.ts +15 -0
  227. package/dist/shared.d.ts.map +1 -0
  228. package/dist/shared.js +425 -0
  229. package/dist/shown.d.ts +31 -0
  230. package/dist/shown.d.ts.map +1 -0
  231. package/dist/shown.js +295 -0
  232. package/dist/socket/admission.d.ts +7 -0
  233. package/dist/socket/admission.d.ts.map +1 -0
  234. package/dist/socket/admission.js +39 -0
  235. package/dist/socket/answers.d.ts +6 -0
  236. package/dist/socket/answers.d.ts.map +1 -0
  237. package/dist/socket/answers.js +20 -0
  238. package/dist/socket/lanes.d.ts +20 -0
  239. package/dist/socket/lanes.d.ts.map +1 -0
  240. package/dist/socket/lanes.js +77 -0
  241. package/dist/socket/reads.d.ts +10 -0
  242. package/dist/socket/reads.d.ts.map +1 -0
  243. package/dist/socket/reads.js +31 -0
  244. package/dist/socket/rooms.d.ts +43 -0
  245. package/dist/socket/rooms.d.ts.map +1 -0
  246. package/dist/socket/rooms.js +90 -0
  247. package/dist/socket/session.d.ts +39 -0
  248. package/dist/socket/session.d.ts.map +1 -0
  249. package/dist/socket/session.js +23 -0
  250. package/dist/socket.d.ts +70 -0
  251. package/dist/socket.d.ts.map +1 -0
  252. package/dist/socket.js +175 -0
  253. package/dist/source.d.ts +61 -0
  254. package/dist/source.d.ts.map +1 -0
  255. package/dist/source.js +81 -0
  256. package/dist/status.d.ts +7 -4
  257. package/dist/status.d.ts.map +1 -1
  258. package/dist/status.js +3 -15
  259. package/dist/store/fences.d.ts +16 -0
  260. package/dist/store/fences.d.ts.map +1 -0
  261. package/dist/store/fences.js +26 -0
  262. package/dist/store/installs.d.ts +30 -0
  263. package/dist/store/installs.d.ts.map +1 -0
  264. package/dist/store/installs.js +34 -0
  265. package/dist/store/readiness.d.ts +10 -0
  266. package/dist/store/readiness.d.ts.map +1 -0
  267. package/dist/store/readiness.js +52 -0
  268. package/dist/store/records.d.ts +212 -0
  269. package/dist/store/records.d.ts.map +1 -0
  270. package/dist/store/records.js +408 -0
  271. package/dist/store/room-held.d.ts +8 -0
  272. package/dist/store/room-held.d.ts.map +1 -0
  273. package/dist/store/room-held.js +17 -0
  274. package/dist/store/space.d.ts +11 -0
  275. package/dist/store/space.d.ts.map +1 -0
  276. package/dist/store/space.js +43 -0
  277. package/dist/store/watchers.d.ts +17 -0
  278. package/dist/store/watchers.d.ts.map +1 -0
  279. package/dist/store/watchers.js +57 -0
  280. package/dist/store/writes.d.ts +24 -0
  281. package/dist/store/writes.d.ts.map +1 -0
  282. package/dist/store/writes.js +125 -0
  283. package/dist/store.d.ts +106 -93
  284. package/dist/store.d.ts.map +1 -1
  285. package/dist/store.js +142 -385
  286. package/dist/stored.d.ts +156 -0
  287. package/dist/stored.d.ts.map +1 -0
  288. package/dist/stored.js +637 -0
  289. package/dist/surface.d.ts +88 -0
  290. package/dist/surface.d.ts.map +1 -0
  291. package/dist/surface.js +223 -0
  292. package/dist/sync.d.ts +20 -0
  293. package/dist/sync.d.ts.map +1 -0
  294. package/dist/sync.js +532 -0
  295. package/dist/tables.d.ts +415 -0
  296. package/dist/tables.d.ts.map +1 -0
  297. package/dist/tables.js +740 -0
  298. package/dist/tallies.d.ts +18 -0
  299. package/dist/tallies.d.ts.map +1 -0
  300. package/dist/tallies.js +129 -0
  301. package/dist/transfer.d.ts +0 -5
  302. package/dist/transfer.d.ts.map +1 -1
  303. package/dist/turn.d.ts +9 -0
  304. package/dist/turn.d.ts.map +1 -0
  305. package/dist/turn.js +75 -0
  306. package/dist/validation.d.ts +29 -0
  307. package/dist/validation.d.ts.map +1 -0
  308. package/dist/validation.js +104 -0
  309. package/dist/windows.d.ts +11 -0
  310. package/dist/windows.d.ts.map +1 -0
  311. package/dist/windows.js +16 -0
  312. package/dist/wire.d.ts +207 -20
  313. package/dist/wire.d.ts.map +1 -1
  314. package/dist/wire.js +96 -61
  315. package/package.json +33 -10
  316. package/AGENTS.md +0 -207
  317. package/dist/attachment.d.ts +0 -3
  318. package/dist/attachment.d.ts.map +0 -1
  319. package/dist/attachment.js +0 -8
  320. package/dist/read-side.d.ts +0 -14
  321. package/dist/read-side.d.ts.map +0 -1
  322. package/dist/read-side.js +0 -371
package/dist/replica.js CHANGED
@@ -1,159 +1,216 @@
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 { writtenFacts } from './rows.js';
7
+ import { effectiveIn, rewriting } from './save.js';
8
+ import { guarding } from './shared.js';
9
+ import { ROW_WEIGHT } from './turn.js';
10
+ export function compositionOf(record, builtIn, typedPaths) {
8
11
  return {
9
12
  model: record.model,
10
13
  selection: record.typedSelection === null
11
14
  ? null
12
- : { roots: record.typedSelection.roots, model: record.selectionModel },
15
+ : { roots: record.typedSelection.roots, model: builtIn },
16
+ typedPaths,
13
17
  };
14
18
  }
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;
19
+ export function storedRecord(record) {
20
+ const { key: _key, ...stored } = record;
21
+ return JSON.stringify({ ...stored, model: storedModel(stored.model) });
22
22
  }
23
- export function sameMembership(record, other) {
24
- return (JSON.stringify(record.typedSelection?.roots ?? null) ===
25
- JSON.stringify(other.typedSelection?.roots ?? null));
23
+ const PROBES = new Set([
24
+ 'then',
25
+ 'toJSON',
26
+ 'constructor',
27
+ 'valueOf',
28
+ 'toString',
29
+ ]);
30
+ class UnreadPath extends Error {
31
+ constructor(path) {
32
+ super(`the replica slice holds no row for ${path}`);
33
+ }
26
34
  }
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 };
35
+ function strict(values, held) {
36
+ const guard = (property) => {
37
+ if (typeof property === 'string' && !held.has(property) && !PROBES.has(property)) {
38
+ throw new UnreadPath(property);
36
39
  }
37
- }
38
- return readConfirmed(record, previous);
40
+ };
41
+ return guarding(values, {
42
+ get(target, property, receiver) {
43
+ guard(property);
44
+ return Reflect.get(target, property, receiver);
45
+ },
46
+ has(target, property) {
47
+ guard(property);
48
+ return Reflect.has(target, property);
49
+ },
50
+ getOwnPropertyDescriptor(target, property) {
51
+ guard(property);
52
+ return Reflect.getOwnPropertyDescriptor(target, property);
53
+ },
54
+ });
39
55
  }
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 };
56
+ export function* slicing(entries, paths) {
57
+ const held = new Set(paths);
58
+ for (const entry of entries)
59
+ held.add(entry.path);
60
+ const documents = {};
61
+ const binaries = {};
62
+ const revisions = {};
63
+ const fileIds = {};
64
+ const created = {};
65
+ const changes = {};
66
+ const written = {};
67
+ const facts = {};
68
+ for (const entry of entries) {
69
+ if (entry.facts !== null)
70
+ facts[entry.path] = entry.facts;
71
+ if (entry.binary !== null)
72
+ binaries[entry.path] = entry.binary;
73
+ else if (entry.content !== null)
74
+ documents[entry.path] = entry.content;
75
+ if (entry.revision !== null)
76
+ revisions[entry.path] = entry.revision;
77
+ if (entry.fileId !== null)
78
+ fileIds[entry.path] = entry.fileId;
79
+ if (entry.created !== null)
80
+ created[entry.path] = entry.created;
81
+ if (entry.change !== null)
82
+ changes[entry.path] = entry.change;
83
+ if (entry.written !== null)
84
+ written[entry.path] = entry.written;
85
+ yield ROW_WEIGHT;
86
+ }
87
+ return {
88
+ documents: strict(documents, held),
89
+ binaries: strict(binaries, held),
90
+ revisions: strict(revisions, held),
91
+ fileIds: strict(fileIds, held),
92
+ created: strict(created, held),
93
+ changes: strict(changes, held),
94
+ written: strict(written, held),
95
+ facts: strict(facts, held),
96
+ held,
97
+ };
47
98
  }
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])),
99
+ export function entryAt(layer, path) {
100
+ const content = layer.documents[path];
101
+ const binary = layer.binaries[path];
102
+ if (content === undefined && binary === undefined)
103
+ return null;
104
+ return {
105
+ path,
106
+ content: content ?? null,
107
+ binary: binary ?? null,
108
+ revision: layer.revisions[path] ?? null,
109
+ fileId: layer.fileIds[path] ?? null,
110
+ created: layer.created[path] ?? null,
111
+ change: layer.changes[path] ?? null,
112
+ written: layer.written?.[path] ?? null,
55
113
  };
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];
114
+ }
115
+ export function* slicingWith(slice, set, removed) {
116
+ const entries = new Map();
117
+ for (const path of slice.held) {
118
+ const entry = entryAt(slice, path);
119
+ if (entry !== null) {
120
+ entries.set(path, { ...entry, type: null, facts: slice.facts[path] ?? null });
121
+ }
122
+ yield ROW_WEIGHT;
60
123
  }
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);
124
+ for (const path of removed)
125
+ entries.delete(path);
126
+ for (const entry of set)
127
+ entries.set(entry.path, { ...entry, type: null });
128
+ return yield* slicing([...entries.values()], [...slice.held, ...set.map(({ path }) => path), ...removed]);
129
+ }
130
+ function changePaths(change) {
131
+ return change.kind === 'move' ? [change.path, change.to] : [change.path];
132
+ }
133
+ export function touchedBy(queue) {
134
+ const paths = new Set();
135
+ for (const batch of queue) {
136
+ for (const change of batch.changes)
137
+ for (const path of changePaths(change))
138
+ paths.add(path);
139
+ for (const precondition of batch.preconditions)
140
+ paths.add(precondition.path);
141
+ for (const move of batch.between?.moves ?? [])
142
+ paths.add(move.path);
67
143
  }
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);
144
+ return paths;
77
145
  }
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
- });
146
+ export function movedBy(queue, locator) {
147
+ const paths = new Set();
148
+ for (const batch of queue) {
149
+ if (!rewriting(batch, locator))
150
+ continue;
151
+ for (const change of batch.changes)
152
+ if (change.kind === 'move')
153
+ paths.add(change.path);
154
+ if (batch.between?.source.room === locator) {
155
+ for (const move of batch.between.moves)
156
+ paths.add(move.path);
157
+ }
116
158
  }
117
- return { layer, replay };
159
+ return paths;
118
160
  }
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
- };
161
+ export function* replaying(confirmed, queue, locator, composition) {
162
+ let layer = confirmed;
163
+ const touched = new Set();
164
+ for (const batch of queue) {
165
+ const effective = rewriting(batch, locator)
166
+ ? yield* effectiveIn(batch, locator, {
167
+ ...layer,
168
+ collection: collectionOf(layer.documents, composition),
169
+ })
170
+ : batch;
171
+ for (const change of effective.changes)
172
+ for (const path of changePaths(change))
173
+ touched.add(path);
174
+ layer = yield* overlaying(layer, [effective]);
175
+ yield ROW_WEIGHT;
134
176
  }
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
177
  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
- },
178
+ visible: { ...layer, collection: collectionOf(layer.documents, composition) },
179
+ touched,
151
180
  };
152
181
  }
182
+ export function* overlayOf(visible, touched) {
183
+ const rows = [];
184
+ for (const path of touched) {
185
+ const entry = entryAt(visible, path);
186
+ yield ROW_WEIGHT + (entry?.content?.length ?? 0);
187
+ const text = entry?.binary === null ? entry.content : null;
188
+ rows.push(entry === null
189
+ ? {
190
+ path,
191
+ present: false,
192
+ content: null,
193
+ binary: null,
194
+ revision: null,
195
+ fileId: null,
196
+ created: null,
197
+ change: null,
198
+ written: null,
199
+ facts: null,
200
+ }
201
+ : {
202
+ ...entry,
203
+ written: null,
204
+ present: true,
205
+ facts: text === null ? null : writtenFacts(path, text, visible.collection.composition),
206
+ });
207
+ }
208
+ return rows;
209
+ }
153
210
  function present(layer, path) {
154
- return layer.documents[path] !== undefined || layer.binaryTargets.includes(path);
211
+ return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
155
212
  }
156
- function holds(layer, preconditions) {
213
+ export function holds(layer, preconditions) {
157
214
  const parsed = new Map();
158
215
  const documentAt = (path) => {
159
216
  if (!parsed.has(path)) {
@@ -172,6 +229,8 @@ function holds(layer, preconditions) {
172
229
  if (precondition.kind === 'revision') {
173
230
  return present(layer, path) && layer.revisions[path] === precondition.revision;
174
231
  }
232
+ if (precondition.kind === 'content')
233
+ return layer.documents[path] === precondition.content;
175
234
  const document = documentAt(path);
176
235
  if (document === null)
177
236
  return false;
@@ -216,53 +275,192 @@ function effects(change) {
216
275
  { kind: 'present', path: change.to },
217
276
  ];
218
277
  }
278
+ if (change.kind === 'binary' || change.kind === 'arrival')
279
+ return [{ kind: 'present', path }];
219
280
  return [{ kind: 'absent', path }];
220
281
  }
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;
282
+ export function landed(batch, confirmed, current = {}) {
283
+ const documents = new Proxy(confirmed.documents, {
284
+ get(target, property, receiver) {
285
+ if (typeof property === 'string' && Object.hasOwn(current, property))
286
+ return current[property];
287
+ return Reflect.get(target, property, receiver);
288
+ },
289
+ });
290
+ const layer = { ...confirmed, documents };
235
291
  return batch.changes.every((change, index) => {
236
292
  if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
237
- const at = after(batch.changes, index, change.path);
293
+ const at = landing(batch.changes, index, change.path);
238
294
  return at === null || layer.documents[at] === change.content;
239
295
  }
296
+ if (change.kind === 'binary') {
297
+ const at = landing(batch.changes, index, change.path);
298
+ return at === null || layer.binaries[at]?.sha256 === change.sha256;
299
+ }
240
300
  return effects(change).every((effect) => {
241
301
  if (effect.kind === 'absent')
242
302
  return holds(layer, [effect]);
243
- const at = after(batch.changes, index, effect.path);
303
+ const at = landing(batch.changes, index, effect.path);
244
304
  return at === null || holds(layer, [{ ...effect, path: at }]);
245
305
  });
246
306
  });
247
307
  }
248
- export function withdrawals(confirmed, queue, withdrawable, rewriting) {
308
+ function founded(batch, layer) {
309
+ let changed = false;
310
+ const preconditions = batch.preconditions.map((precondition) => {
311
+ if (precondition.kind !== 'content')
312
+ return precondition;
313
+ const revision = layer.revisions[precondition.path];
314
+ if (revision === undefined || layer.documents[precondition.path] !== precondition.content) {
315
+ return precondition;
316
+ }
317
+ changed = true;
318
+ return { kind: 'revision', path: precondition.path, revision };
319
+ });
320
+ return changed ? immutable({ ...batch, preconditions }) : batch;
321
+ }
322
+ export function* standing(confirmed, queue, withdrawable, local, locator, composition) {
249
323
  if (!queue.some(withdrawable))
250
- return [];
251
- const composition = compositionOf(confirmed.record);
252
- let layer = confirmed.record;
324
+ return { withdrawn: [], kept: queue };
325
+ let layer = confirmed;
253
326
  const withdrawn = [];
327
+ const kept = [];
254
328
  for (const batch of queue) {
255
329
  if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
256
330
  withdrawn.push(batch);
257
331
  continue;
258
332
  }
259
- const effective = rewriting === 'provider' && batch.changes.some((change) => change.kind === 'move')
260
- ? providerRewritten(batch, {
333
+ const effective = rewriting(batch, locator)
334
+ ? yield* effectiveIn(batch, locator, {
261
335
  ...layer,
262
- collection: parseCollection(layer.documents, composition),
336
+ collection: collectionOf(layer.documents, composition),
263
337
  })
264
338
  : batch;
265
- layer = overlay(layer, [effective]);
339
+ let unapplied = false;
340
+ const next = yield* overlaying(layer, [effective], () => {
341
+ unapplied = true;
342
+ });
343
+ if (local && unapplied) {
344
+ withdrawn.push(batch);
345
+ continue;
346
+ }
347
+ kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
348
+ layer = next;
349
+ }
350
+ return { withdrawn, kept };
351
+ }
352
+ export function returnedPaths(submitted, accepted) {
353
+ const paths = new Set([
354
+ ...Object.keys(accepted.documents),
355
+ ...Object.keys(accepted.revisions),
356
+ ...Object.keys(accepted.fileIds ?? {}),
357
+ ...Object.keys(accepted.changes),
358
+ ...Object.keys(accepted.binaryFiles ?? {}),
359
+ ...accepted.removals,
360
+ ]);
361
+ for (const change of submitted.changes) {
362
+ if (change.kind === 'move') {
363
+ paths.add(change.path);
364
+ paths.add(change.to);
365
+ }
366
+ if (change.kind === 'arrival')
367
+ paths.add(change.path);
368
+ }
369
+ return paths;
370
+ }
371
+ export function* returning(confirmed, submitted, accepted, writtenAt, composition) {
372
+ const documents = new Map();
373
+ const binaries = new Map();
374
+ const revisions = new Map();
375
+ const fileIds = new Map();
376
+ const created = new Map();
377
+ const changes = new Map();
378
+ const written = new Map();
379
+ const facts = new Map();
380
+ const read = (changed, held, path) => (changed.has(path) ? changed.get(path) : held[path]);
381
+ for (const [path, content] of Object.entries(accepted.documents)) {
382
+ yield ROW_WEIGHT + content.length;
383
+ documents.set(path, content);
384
+ facts.set(path, writtenFacts(path, content, composition));
385
+ fileIds.set(path, undefined);
386
+ written.set(path, writtenAt);
387
+ }
388
+ for (const [path, revision] of Object.entries(accepted.revisions)) {
389
+ yield ROW_WEIGHT;
390
+ revisions.set(path, revision);
391
+ fileIds.set(path, undefined);
392
+ if (confirmed.revisions[path] !== revision)
393
+ changes.set(path, undefined);
394
+ }
395
+ for (const [path, fileId] of Object.entries(accepted.fileIds ?? {}))
396
+ fileIds.set(path, fileId);
397
+ for (const [path, change] of Object.entries(accepted.changes))
398
+ changes.set(path, change);
399
+ for (const path of accepted.removals) {
400
+ documents.set(path, undefined);
401
+ revisions.set(path, undefined);
402
+ fileIds.set(path, undefined);
403
+ changes.set(path, undefined);
404
+ written.set(path, undefined);
405
+ }
406
+ for (const change of submitted.changes) {
407
+ if (change.kind === 'arrival') {
408
+ if (accepted.revisions[change.path] === undefined)
409
+ continue;
410
+ created.set(change.path, change.created);
411
+ if ('binary' in change)
412
+ binaries.set(change.path, { state: 'accepted', ...change.binary });
413
+ continue;
414
+ }
415
+ if (change.kind !== 'move' || accepted.revisions[change.to] === undefined)
416
+ continue;
417
+ created.set(change.to, read(created, confirmed.created, change.path));
418
+ created.set(change.path, undefined);
419
+ const moved = read(binaries, confirmed.binaries, change.path);
420
+ if (moved === undefined)
421
+ continue;
422
+ binaries.set(change.to, moved);
423
+ binaries.set(change.path, undefined);
424
+ }
425
+ for (const [path, binary] of acceptedBinaries(accepted.binaryFiles ?? {}))
426
+ binaries.set(path, binary);
427
+ for (const path of accepted.removals) {
428
+ binaries.set(path, undefined);
429
+ created.set(path, undefined);
430
+ }
431
+ const paths = new Set([
432
+ ...documents.keys(),
433
+ ...binaries.keys(),
434
+ ...revisions.keys(),
435
+ ...fileIds.keys(),
436
+ ...created.keys(),
437
+ ...changes.keys(),
438
+ ...written.keys(),
439
+ ]);
440
+ const set = [];
441
+ const removed = [];
442
+ for (const path of paths) {
443
+ const content = read(documents, confirmed.documents, path);
444
+ const binary = read(binaries, confirmed.binaries, path);
445
+ yield ROW_WEIGHT;
446
+ if (content === undefined && binary === undefined) {
447
+ removed.push(path);
448
+ continue;
449
+ }
450
+ const text = binary === undefined ? (content ?? null) : null;
451
+ const fresh = confirmed.documents[path] === undefined && confirmed.binaries[path] === undefined;
452
+ set.push({
453
+ path,
454
+ content: text,
455
+ facts: text === null ? null : (read(facts, confirmed.facts, path) ?? null),
456
+ binary: binary ?? null,
457
+ revision: read(revisions, confirmed.revisions, path) ?? null,
458
+ fileId: read(fileIds, confirmed.fileIds, path) ?? null,
459
+ created: read(created, confirmed.created, path) ??
460
+ (fresh ? (read(changes, confirmed.changes, path)?.time ?? null) : null),
461
+ change: read(changes, confirmed.changes, path) ?? null,
462
+ written: read(written, confirmed.written, path) ?? null,
463
+ });
266
464
  }
267
- return withdrawn;
465
+ return { set, removed };
268
466
  }