@agentshouse/mdruntime 0.1.0-cff8ba8fa2542 → 0.1.0-cffcba9cd9c2f

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