@agentshouse/mdruntime 0.1.0-ce531b51ff088 → 0.1.0-ce6bc9239a3ee

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 (253) hide show
  1. package/README.md +25 -12
  2. package/dist/acceptance.d.ts +26 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +384 -0
  5. package/dist/adapter.d.ts +218 -62
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +1 -9
  8. package/dist/attached-model.d.ts +8 -0
  9. package/dist/attached-model.d.ts.map +1 -0
  10. package/dist/attached-model.js +46 -0
  11. package/dist/attempt.d.ts +16 -0
  12. package/dist/attempt.d.ts.map +1 -0
  13. package/dist/attempt.js +70 -0
  14. package/dist/binary.d.ts +18 -0
  15. package/dist/binary.d.ts.map +1 -0
  16. package/dist/binary.js +8 -0
  17. package/dist/bootstrap.d.ts +40 -6
  18. package/dist/bootstrap.d.ts.map +1 -1
  19. package/dist/bootstrap.js +336 -51
  20. package/dist/chain.d.ts +12 -0
  21. package/dist/chain.d.ts.map +1 -0
  22. package/dist/chain.js +64 -0
  23. package/dist/change-set.d.ts +9 -14
  24. package/dist/change-set.d.ts.map +1 -1
  25. package/dist/change-set.js +11 -56
  26. package/dist/change.d.ts +46 -0
  27. package/dist/change.d.ts.map +1 -1
  28. package/dist/change.js +34 -0
  29. package/dist/chat-types.d.ts +3 -0
  30. package/dist/chat-types.d.ts.map +1 -0
  31. package/dist/chat-types.js +2 -0
  32. package/dist/client.d.ts +50 -18
  33. package/dist/client.d.ts.map +1 -1
  34. package/dist/client.js +162 -30
  35. package/dist/collection.d.ts +13 -14
  36. package/dist/collection.d.ts.map +1 -1
  37. package/dist/collection.js +52 -56
  38. package/dist/compile.d.ts +16 -0
  39. package/dist/compile.d.ts.map +1 -0
  40. package/dist/compile.js +717 -0
  41. package/dist/connection.d.ts +63 -48
  42. package/dist/connection.d.ts.map +1 -1
  43. package/dist/connection.js +2487 -1575
  44. package/dist/context.d.ts +2 -0
  45. package/dist/context.d.ts.map +1 -0
  46. package/dist/context.js +7 -0
  47. package/dist/conversation-row.d.ts +33 -0
  48. package/dist/conversation-row.d.ts.map +1 -0
  49. package/dist/conversation-row.js +47 -0
  50. package/dist/conversation.d.ts +72 -22
  51. package/dist/conversation.d.ts.map +1 -1
  52. package/dist/conversation.js +1008 -273
  53. package/dist/digest.d.ts +7 -0
  54. package/dist/digest.d.ts.map +1 -0
  55. package/dist/digest.js +113 -0
  56. package/dist/durability.d.ts +10 -1
  57. package/dist/durability.d.ts.map +1 -1
  58. package/dist/durability.js +5 -0
  59. package/dist/entry.d.ts +20 -0
  60. package/dist/entry.d.ts.map +1 -0
  61. package/dist/entry.js +161 -0
  62. package/dist/expectation.d.ts +21 -0
  63. package/dist/expectation.d.ts.map +1 -0
  64. package/dist/expectation.js +70 -0
  65. package/dist/failure.d.ts +13 -0
  66. package/dist/failure.d.ts.map +1 -0
  67. package/dist/failure.js +19 -0
  68. package/dist/figures.d.ts +48 -0
  69. package/dist/figures.d.ts.map +1 -0
  70. package/dist/figures.js +197 -0
  71. package/dist/file.d.ts +2 -19
  72. package/dist/file.d.ts.map +1 -1
  73. package/dist/file.js +7 -122
  74. package/dist/host.d.ts +18 -2
  75. package/dist/host.d.ts.map +1 -1
  76. package/dist/host.js +164 -50
  77. package/dist/identity.d.ts +3 -0
  78. package/dist/identity.d.ts.map +1 -1
  79. package/dist/identity.js +13 -1
  80. package/dist/index.d.ts +31 -10
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +13 -0
  83. package/dist/install.d.ts +86 -0
  84. package/dist/install.d.ts.map +1 -0
  85. package/dist/install.js +537 -0
  86. package/dist/job.d.ts +33 -0
  87. package/dist/job.d.ts.map +1 -0
  88. package/dist/job.js +54 -0
  89. package/dist/live.d.ts +23 -0
  90. package/dist/live.d.ts.map +1 -0
  91. package/dist/live.js +165 -0
  92. package/dist/log.d.ts +1 -0
  93. package/dist/log.d.ts.map +1 -1
  94. package/dist/media.d.ts +8 -0
  95. package/dist/media.d.ts.map +1 -0
  96. package/dist/media.js +218 -0
  97. package/dist/memory.d.ts +30 -9
  98. package/dist/memory.d.ts.map +1 -1
  99. package/dist/memory.js +337 -86
  100. package/dist/messages.d.ts +52 -0
  101. package/dist/messages.d.ts.map +1 -0
  102. package/dist/messages.js +168 -0
  103. package/dist/model.d.ts +17 -16
  104. package/dist/model.d.ts.map +1 -1
  105. package/dist/model.js +9 -9
  106. package/dist/name.d.ts +4 -0
  107. package/dist/name.d.ts.map +1 -0
  108. package/dist/name.js +122 -0
  109. package/dist/observation.d.ts +22 -2
  110. package/dist/observation.d.ts.map +1 -1
  111. package/dist/observation.js +133 -17
  112. package/dist/open.d.ts +2 -0
  113. package/dist/open.d.ts.map +1 -0
  114. package/dist/open.js +1 -0
  115. package/dist/opened.d.ts +22 -0
  116. package/dist/opened.d.ts.map +1 -0
  117. package/dist/opened.js +107 -0
  118. package/dist/outcome.d.ts +73 -5
  119. package/dist/outcome.d.ts.map +1 -1
  120. package/dist/outcome.js +84 -1
  121. package/dist/overlay.d.ts +16 -5
  122. package/dist/overlay.d.ts.map +1 -1
  123. package/dist/overlay.js +147 -180
  124. package/dist/parts.d.ts +59 -0
  125. package/dist/parts.d.ts.map +1 -0
  126. package/dist/parts.js +118 -0
  127. package/dist/paths.d.ts +1 -1
  128. package/dist/paths.d.ts.map +1 -1
  129. package/dist/paths.js +4 -16
  130. package/dist/query.d.ts +56 -180
  131. package/dist/query.d.ts.map +1 -1
  132. package/dist/query.js +3 -422
  133. package/dist/questions.d.ts +32 -0
  134. package/dist/questions.d.ts.map +1 -0
  135. package/dist/questions.js +118 -0
  136. package/dist/reach.d.ts +54 -0
  137. package/dist/reach.d.ts.map +1 -0
  138. package/dist/reach.js +494 -0
  139. package/dist/react.d.ts +46 -0
  140. package/dist/react.d.ts.map +1 -0
  141. package/dist/react.js +54 -0
  142. package/dist/read.d.ts +7 -0
  143. package/dist/read.d.ts.map +1 -0
  144. package/dist/read.js +17 -0
  145. package/dist/readiness.d.ts +50 -0
  146. package/dist/readiness.d.ts.map +1 -0
  147. package/dist/readiness.js +27 -0
  148. package/dist/reads.d.ts +36 -0
  149. package/dist/reads.d.ts.map +1 -0
  150. package/dist/reads.js +136 -0
  151. package/dist/recovery.d.ts +26 -0
  152. package/dist/recovery.d.ts.map +1 -0
  153. package/dist/recovery.js +232 -0
  154. package/dist/refresh.d.ts +21 -7
  155. package/dist/refresh.d.ts.map +1 -1
  156. package/dist/refresh.js +155 -55
  157. package/dist/replica.d.ts +58 -33
  158. package/dist/replica.d.ts.map +1 -1
  159. package/dist/replica.js +355 -158
  160. package/dist/room-draft.d.ts +62 -11
  161. package/dist/room-draft.d.ts.map +1 -1
  162. package/dist/room-draft.js +281 -83
  163. package/dist/room-drafts.d.ts +29 -0
  164. package/dist/room-drafts.d.ts.map +1 -0
  165. package/dist/room-drafts.js +216 -0
  166. package/dist/rows.d.ts +32 -34
  167. package/dist/rows.d.ts.map +1 -1
  168. package/dist/rows.js +65 -87
  169. package/dist/save.d.ts +74 -6
  170. package/dist/save.d.ts.map +1 -1
  171. package/dist/save.js +902 -182
  172. package/dist/saves.d.ts +96 -0
  173. package/dist/saves.d.ts.map +1 -0
  174. package/dist/saves.js +999 -0
  175. package/dist/search.d.ts +15 -54
  176. package/dist/search.d.ts.map +1 -1
  177. package/dist/search.js +10 -121
  178. package/dist/seed.d.ts +30 -0
  179. package/dist/seed.d.ts.map +1 -0
  180. package/dist/seed.js +141 -0
  181. package/dist/shared.d.ts +15 -0
  182. package/dist/shared.d.ts.map +1 -0
  183. package/dist/shared.js +421 -0
  184. package/dist/socket/admission.d.ts +7 -0
  185. package/dist/socket/admission.d.ts.map +1 -0
  186. package/dist/socket/admission.js +39 -0
  187. package/dist/socket/answers.d.ts +6 -0
  188. package/dist/socket/answers.d.ts.map +1 -0
  189. package/dist/socket/answers.js +20 -0
  190. package/dist/socket/reads.d.ts +10 -0
  191. package/dist/socket/reads.d.ts.map +1 -0
  192. package/dist/socket/reads.js +31 -0
  193. package/dist/socket.d.ts +43 -0
  194. package/dist/socket.d.ts.map +1 -0
  195. package/dist/socket.js +96 -0
  196. package/dist/source.d.ts +61 -0
  197. package/dist/source.d.ts.map +1 -0
  198. package/dist/source.js +81 -0
  199. package/dist/status.d.ts +7 -4
  200. package/dist/status.d.ts.map +1 -1
  201. package/dist/status.js +3 -15
  202. package/dist/store/fences.d.ts +16 -0
  203. package/dist/store/fences.d.ts.map +1 -0
  204. package/dist/store/fences.js +26 -0
  205. package/dist/store/installs.d.ts +30 -0
  206. package/dist/store/installs.d.ts.map +1 -0
  207. package/dist/store/installs.js +34 -0
  208. package/dist/store/records.d.ts +212 -0
  209. package/dist/store/records.d.ts.map +1 -0
  210. package/dist/store/records.js +408 -0
  211. package/dist/store/room-held.d.ts +8 -0
  212. package/dist/store/room-held.d.ts.map +1 -0
  213. package/dist/store/room-held.js +17 -0
  214. package/dist/store/watchers.d.ts +15 -0
  215. package/dist/store/watchers.d.ts.map +1 -0
  216. package/dist/store/watchers.js +36 -0
  217. package/dist/store/writes.d.ts +24 -0
  218. package/dist/store/writes.d.ts.map +1 -0
  219. package/dist/store/writes.js +125 -0
  220. package/dist/store.d.ts +77 -94
  221. package/dist/store.d.ts.map +1 -1
  222. package/dist/store.js +118 -389
  223. package/dist/stored.d.ts +156 -0
  224. package/dist/stored.d.ts.map +1 -0
  225. package/dist/stored.js +637 -0
  226. package/dist/tables.d.ts +407 -0
  227. package/dist/tables.d.ts.map +1 -0
  228. package/dist/tables.js +724 -0
  229. package/dist/tallies.d.ts +18 -0
  230. package/dist/tallies.d.ts.map +1 -0
  231. package/dist/tallies.js +129 -0
  232. package/dist/transfer.d.ts +0 -5
  233. package/dist/transfer.d.ts.map +1 -1
  234. package/dist/turn.d.ts +9 -0
  235. package/dist/turn.d.ts.map +1 -0
  236. package/dist/turn.js +75 -0
  237. package/dist/validation.d.ts +29 -0
  238. package/dist/validation.d.ts.map +1 -0
  239. package/dist/validation.js +104 -0
  240. package/dist/windows.d.ts +11 -0
  241. package/dist/windows.d.ts.map +1 -0
  242. package/dist/windows.js +16 -0
  243. package/dist/wire.d.ts +205 -19
  244. package/dist/wire.d.ts.map +1 -1
  245. package/dist/wire.js +95 -61
  246. package/package.json +33 -10
  247. package/AGENTS.md +0 -207
  248. package/dist/attachment.d.ts +0 -3
  249. package/dist/attachment.d.ts.map +0 -1
  250. package/dist/attachment.js +0 -8
  251. package/dist/read-side.d.ts +0 -14
  252. package/dist/read-side.d.ts.map +0 -1
  253. package/dist/read-side.js +0 -371
package/dist/save.js CHANGED
@@ -1,55 +1,73 @@
1
1
  import { definitionOf } from './model.js';
2
- import { apply, links, parse, retarget, serialize, target, } from '@agentshouse/mdmodel';
3
- import { immutable, } from './change.js';
4
- import { governing, parseCollection, validateCollection, } from './collection.js';
2
+ import { apply, ARRAY_INDEX, ATTACHMENT, links, parse, pointerSegments, references, relative, relativize, retarget, roomReferences, serialize, target, } from '@agentshouse/mdmodel';
3
+ import { immutable, leaving, queuedFile, departure, } from './change.js';
4
+ import { collectionOf, documentsOf, governing, } from './collection.js';
5
5
  import { changeBatchIdentity } from './identity.js';
6
- import { overlay } from './overlay.js';
7
- import { covered, normalize, relative } from './paths.js';
8
- function refuse(rule, file, location) {
6
+ import { overlaying } from './overlay.js';
7
+ import { ancestors, covered, normalize } from './paths.js';
8
+ import { sorting } from './shared.js';
9
+ import { paced, ROW_WEIGHT, stepped } from './turn.js';
10
+ import { validatedCollection } from './validation.js';
11
+ function refuse(code, file, location) {
9
12
  return {
10
13
  kind: 'refused',
11
14
  refusal: {
12
- rule,
15
+ code,
16
+ retryable: false,
13
17
  ...(file === undefined ? {} : { file }),
14
18
  ...(location === undefined ? {} : { location }),
15
19
  },
16
20
  };
17
21
  }
18
- function referenceField(spec) {
19
- return spec.kind === 'reference' || (spec.kind === 'list' && spec.of.kind === 'reference');
20
- }
21
- function repointed(value, from, to, moves) {
22
- if (Array.isArray(value)) {
23
- const next = value.map((item) => repointed(item, from, to, moves) ?? item);
24
- return next.some((item, index) => item !== value[index]) ? next : undefined;
22
+ function repointed(document, definition, from, to, moves, settled) {
23
+ const parsed = parse(document.content, from);
24
+ if (!parsed.ok)
25
+ return [];
26
+ const changes = [];
27
+ const names = new Set();
28
+ for (const { pointer, value } of definition === undefined
29
+ ? []
30
+ : references(document, definition)) {
31
+ const [name = ''] = pointerSegments(pointer);
32
+ const named = target(from, value);
33
+ if (named === null || settled.has(`${from} ${name}`))
34
+ continue;
35
+ const destination = moves.get(named) ?? named;
36
+ if (target(to, value) === destination)
37
+ continue;
38
+ changes.push({
39
+ kind: 'reference',
40
+ pointer,
41
+ value: retarget(value, relative(to, destination)),
42
+ prior: value,
43
+ });
44
+ names.add(name);
25
45
  }
26
- if (typeof value !== 'string')
27
- return undefined;
28
- const named = target(from, value);
29
- if (named === null)
30
- return undefined;
31
- const destination = moves.get(named) ?? named;
32
- return target(to, value) === destination ? undefined : retarget(value, relative(to, destination));
46
+ if (changes.length === 0)
47
+ return [];
48
+ const applied = apply(parsed.document, changes);
49
+ if (applied.outcome !== 'applied')
50
+ return [];
51
+ const next = applied.document;
52
+ return [...names].map((name) => ({ kind: 'field', path: from, name, value: next.fields[name] }));
33
53
  }
34
- function edited(document, path, changes) {
35
- let next = document;
54
+ function edited(document, changes) {
55
+ const parts = new Map();
36
56
  for (const change of changes) {
37
- if (change.path !== path)
38
- continue;
39
- if (change.kind !== 'section' && change.kind !== 'preamble')
40
- continue;
41
- const applied = apply(next, change.kind === 'preamble'
42
- ? { kind: 'preamble', value: change.value, prior: next.preamble }
43
- : {
57
+ if (change.kind === 'preamble')
58
+ parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
59
+ if (change.kind === 'section')
60
+ parts.set(`section ${change.name}`, {
44
61
  kind: 'section',
45
62
  name: change.name,
46
63
  value: change.value,
47
- prior: next.sections[change.name],
64
+ prior: document.sections[change.name] ?? '',
48
65
  });
49
- if (applied.outcome === 'applied')
50
- next = applied.document;
51
66
  }
52
- return next;
67
+ if (parts.size === 0)
68
+ return document;
69
+ const applied = apply(document, [...parts.values()]);
70
+ return applied.outcome === 'applied' ? applied.document : document;
53
71
  }
54
72
  function bodies(path, before, after) {
55
73
  const changes = [];
@@ -64,7 +82,7 @@ function bodies(path, before, after) {
64
82
  return changes;
65
83
  }
66
84
  function relinks(path, moved, before, moves) {
67
- let after = before;
85
+ const changes = [];
68
86
  for (const link of links(before, path)) {
69
87
  const destination = moves.get(link.target) ?? link.target;
70
88
  if (destination === link.target && moved === path)
@@ -72,19 +90,23 @@ function relinks(path, moved, before, moves) {
72
90
  const to = relative(moved, destination);
73
91
  if (to === relative(path, link.target))
74
92
  continue;
75
- const applied = apply(after, { kind: 'link', href: link.href, to });
76
- if (applied.outcome === 'refuse') {
77
- return {
78
- kind: 'refused',
79
- refusal: {
80
- rule: applied.refusal.rule,
81
- file: path,
82
- ...(applied.refusal.location === undefined ? {} : { location: applied.refusal.location }),
83
- },
84
- };
85
- }
86
- after = applied.document;
93
+ changes.push({ kind: 'link', href: link.href, to });
94
+ }
95
+ if (changes.length === 0)
96
+ return { kind: 'changes', changes: [] };
97
+ const applied = apply(before, changes);
98
+ if (applied.outcome === 'refuse') {
99
+ return {
100
+ kind: 'refused',
101
+ refusal: {
102
+ code: applied.refusal.rule,
103
+ retryable: false,
104
+ file: path,
105
+ ...(applied.refusal.location === undefined ? {} : { location: applied.refusal.location }),
106
+ },
107
+ };
87
108
  }
109
+ const after = applied.document;
88
110
  if (after === before)
89
111
  return { kind: 'changes', changes: [] };
90
112
  if (before.type === undefined) {
@@ -92,93 +114,161 @@ function relinks(path, moved, before, moves) {
92
114
  }
93
115
  return { kind: 'changes', changes: bodies(path, before, after) };
94
116
  }
95
- function subsumed(merged, path) {
96
- for (let index = merged.length - 1; index >= 0; index -= 1) {
97
- const item = merged[index];
98
- if (item.path !== path)
99
- continue;
100
- if (item.kind === 'section' || item.kind === 'preamble')
101
- merged.splice(index, 1);
102
- }
117
+ function partKey(change) {
118
+ return change.kind === 'section' ? `section ${change.name}` : change.kind;
103
119
  }
104
- function rewrites(changes, visible) {
120
+ function* rewrites(changes, visible) {
105
121
  const moves = new Map();
106
122
  const settled = new Set();
107
- for (const change of changes) {
123
+ const parts = new Map();
124
+ for (const [index, change] of changes.entries()) {
108
125
  if (change.kind === 'move')
109
126
  moves.set(change.path, change.to);
110
- if (change.kind === 'remove' || change.kind === 'replace' || change.kind === 'create') {
127
+ if (change.kind === 'remove' ||
128
+ change.kind === 'replace' ||
129
+ change.kind === 'create' ||
130
+ change.kind === 'binary') {
111
131
  settled.add(change.path);
112
132
  }
113
133
  if (change.kind === 'field')
114
134
  settled.add(`${change.path} ${change.name}`);
135
+ if (isPart(change)) {
136
+ const held = parts.get(change.path);
137
+ if (held === undefined)
138
+ parts.set(change.path, [index]);
139
+ else
140
+ held.push(index);
141
+ }
115
142
  }
116
143
  if (moves.size === 0)
117
144
  return { kind: 'changes', changes };
118
145
  const merged = [...changes];
119
146
  const added = [];
120
- for (const [path, document] of Object.entries(visible.collection.documents)) {
147
+ for (const [path, document] of yield* documentsOf(visible.collection)) {
121
148
  if (settled.has(path))
122
149
  continue;
123
150
  const moved = moves.get(path) ?? path;
124
151
  const definition = definitionOf(governing(visible.collection.composition, path), document.type);
125
- for (const [name, spec] of Object.entries(definition?.fields ?? {})) {
126
- if (!referenceField(spec) || settled.has(`${path} ${name}`))
127
- continue;
128
- const value = repointed(document.fields[name], path, moved, moves);
129
- if (value === undefined)
130
- continue;
131
- added.push({ kind: 'field', path, name, value });
132
- }
133
- const relinked = relinks(path, moved, edited(document, path, merged), moves);
152
+ const repointing = repointed(document, definition, path, moved, moves, settled);
153
+ added.push(...repointing);
154
+ const own = parts.get(path) ?? [];
155
+ const relinked = relinks(path, moved, edited(document, own.map((index) => merged[index])), moves);
134
156
  if (relinked.kind === 'refused')
135
157
  return relinked;
158
+ const keyed = new Map();
159
+ for (const index of own) {
160
+ const key = partKey(merged[index]);
161
+ if (!keyed.has(key))
162
+ keyed.set(key, index);
163
+ }
136
164
  for (const change of relinked.changes) {
137
165
  if (change.kind === 'replace') {
138
- subsumed(merged, path);
166
+ for (const index of own) {
167
+ const kind = merged[index]?.kind;
168
+ if (kind === 'section' || kind === 'preamble')
169
+ merged[index] = null;
170
+ }
139
171
  added.push(change);
140
172
  continue;
141
173
  }
142
- const index = merged.findIndex((item) => item.kind === change.kind &&
143
- item.path === path &&
144
- (change.kind !== 'section' || (item.kind === 'section' && item.name === change.name)));
145
- if (index === -1)
174
+ const index = keyed.get(partKey(change));
175
+ if (index === undefined)
146
176
  added.push(change);
147
177
  else
148
178
  merged[index] = change;
149
179
  }
180
+ const text = visible.collection.text[path].length;
181
+ const rewritten = repointing.length > 0 || relinked.changes.length > 0;
182
+ yield ROW_WEIGHT + text + (rewritten ? text : 0);
150
183
  }
151
- return { kind: 'changes', changes: [...added, ...merged] };
184
+ return {
185
+ kind: 'changes',
186
+ changes: [...added, ...merged.filter((change) => change !== null)],
187
+ };
152
188
  }
153
- function anticipated(changes, preconditions, rewritten, visible) {
154
- const derived = rewritten.filter((change) => !changes.includes(change));
189
+ function* anticipated(changes, preconditions, rewritten, visible) {
190
+ const given = new Set(changes);
191
+ const derived = rewritten.filter((change) => !given.has(change));
155
192
  return {
156
193
  changes: rewritten,
157
- preconditions: [...preconditionsOf(derived, visible, []), ...preconditions],
194
+ preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...preconditions],
158
195
  };
159
196
  }
160
- export function providerRewritten(batch, visible) {
161
- const rewritten = rewrites(batch.changes, visible);
197
+ export function* providerRewritten(batch, visible) {
198
+ const rewritten = yield* rewrites(batch.changes, visible);
162
199
  if (rewritten.kind === 'refused')
163
200
  return batch;
164
201
  return {
165
202
  ...batch,
166
- ...anticipated(batch.changes, batch.preconditions, rewritten.changes, visible),
203
+ ...(yield* anticipated(batch.changes, batch.preconditions, rewritten.changes, visible)),
167
204
  };
168
205
  }
169
- function targets(value, from) {
170
- const values = Array.isArray(value) ? value : [value];
171
- const resolved = [];
172
- for (const item of values) {
173
- if (typeof item !== 'string')
206
+ function crossed(document, definition, path, to) {
207
+ const changes = [];
208
+ for (const link of links(document, path)) {
209
+ const written = to(link.target, link.href);
210
+ if (written !== undefined)
211
+ changes.push({ kind: 'link', href: link.href, to: written });
212
+ }
213
+ for (const { pointer, value } of definition === undefined
214
+ ? []
215
+ : references(document, definition)) {
216
+ const named = target(path, value);
217
+ const written = named === null ? undefined : to(named, value);
218
+ if (written !== undefined) {
219
+ changes.push({ kind: 'reference', pointer, value: retarget(value, written), prior: value });
220
+ }
221
+ }
222
+ return changes;
223
+ }
224
+ function rewritten(path, document, changes) {
225
+ if (changes.length === 0)
226
+ return [];
227
+ const applied = apply(document, changes);
228
+ if (applied.outcome !== 'applied')
229
+ return [];
230
+ const after = applied.document;
231
+ if (document.type === undefined)
232
+ return [{ kind: 'replace', path, content: serialize(after) }];
233
+ const names = new Set(changes.flatMap((change) => change.kind === 'reference' ? [pointerSegments(change.pointer)[0] ?? ''] : []));
234
+ return [
235
+ ...[...names].map((name) => ({ kind: 'field', path, name, value: after.fields[name] })),
236
+ ...bodies(path, document, after),
237
+ ];
238
+ }
239
+ function roomPathOf(handle, path) {
240
+ return `/rooms/${handle}/${path}`;
241
+ }
242
+ export function* leavingRewritten(batch, visible) {
243
+ const between = batch.between;
244
+ if (between === undefined)
245
+ return batch;
246
+ const moved = new Map(between.moves.map(({ path, to }) => [path, roomPathOf(between.destination.handle, to)]));
247
+ const derived = [];
248
+ for (const [path, document] of yield* documentsOf(visible.collection)) {
249
+ if (moved.has(path))
174
250
  continue;
175
- const named = target(from, item);
176
- if (named !== null)
177
- resolved.push(named);
251
+ const definition = definitionOf(governing(visible.collection.composition, path), document.type);
252
+ derived.push(...rewritten(path, document, crossed(document, definition, path, (named) => moved.get(named))));
253
+ yield ROW_WEIGHT + visible.collection.text[path].length;
178
254
  }
179
- return resolved;
255
+ if (derived.length === 0)
256
+ return batch;
257
+ return {
258
+ ...batch,
259
+ changes: [...derived, ...batch.changes],
260
+ preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...batch.preconditions],
261
+ };
180
262
  }
181
- function incoming(collection, removed) {
263
+ export function rewriting(batch, locator) {
264
+ return leaving(batch, locator) || batch.changes.some((change) => change.kind === 'move');
265
+ }
266
+ export function* effectiveIn(batch, locator, visible) {
267
+ return leaving(batch, locator)
268
+ ? yield* leavingRewritten(batch, visible)
269
+ : yield* providerRewritten(batch, visible);
270
+ }
271
+ function* incoming(collection, removed, candidates) {
182
272
  const found = new Map();
183
273
  const note = (target, file) => {
184
274
  if (!removed.has(target))
@@ -187,18 +277,31 @@ function incoming(collection, removed) {
187
277
  referrers.add(file);
188
278
  found.set(target, referrers);
189
279
  };
190
- for (const [path, document] of Object.entries(collection.documents)) {
280
+ const read = function* (paths) {
281
+ const seen = new Set();
282
+ for (const path of paths) {
283
+ if (seen.has(path))
284
+ continue;
285
+ seen.add(path);
286
+ const document = collection.document(path);
287
+ if (document !== undefined)
288
+ yield [path, document];
289
+ }
290
+ };
291
+ for (const [path, document] of candidates === null
292
+ ? yield* documentsOf(collection)
293
+ : read(candidates)) {
191
294
  const definition = definitionOf(governing(collection.composition, path), document.type);
192
- for (const [name, spec] of Object.entries(definition?.fields ?? {})) {
193
- if (!referenceField(spec))
295
+ for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
296
+ if (to.includes(ATTACHMENT))
194
297
  continue;
195
- for (const named of targets(document.fields[name], path))
298
+ const named = target(path, value);
299
+ if (named !== null)
196
300
  note(named, path);
197
301
  }
198
- for (const link of links(document, path))
199
- note(link.target, path);
302
+ yield ROW_WEIGHT + collection.text[path].length;
200
303
  }
201
- const [first] = [...found.keys()].sort();
304
+ const [first] = yield* sorting(found.keys());
202
305
  if (first === undefined)
203
306
  return null;
204
307
  const every = new Set();
@@ -207,18 +310,29 @@ function incoming(collection, removed) {
207
310
  every.add(file);
208
311
  return {
209
312
  kind: 'refused',
210
- refusal: { rule: 'referenced-missing-file', file: first, referrers: [...every].sort() },
313
+ refusal: {
314
+ code: 'referenced-missing-file',
315
+ retryable: false,
316
+ file: first,
317
+ referrers: yield* sorting(every),
318
+ },
211
319
  };
212
320
  }
213
321
  function affected(change) {
214
322
  return change.kind === 'move' ? [change.path, change.to] : [change.path];
215
323
  }
216
- function guard(changes, input) {
217
- const existing = new Set([
218
- ...Object.keys(input.visible.documents),
219
- ...input.visible.binaryTargets,
220
- ]);
221
- for (const change of changes) {
324
+ const WRITE = ['write'];
325
+ const MOVE_OUT = ['write', 'erase'];
326
+ function guard(changes, input, removing) {
327
+ const overridden = new Map();
328
+ const existing = {
329
+ has: (path) => overridden.get(path) ??
330
+ (Object.hasOwn(input.visible.documents, path) || Object.hasOwn(input.visible.binaries, path)),
331
+ add: (path) => void overridden.set(path, true),
332
+ delete: (path) => void overridden.set(path, false),
333
+ };
334
+ const occupied = (path) => input.folders.has(path) ? path : (ancestors(path).find((one) => existing.has(one)) ?? null);
335
+ for (const [index, change] of changes.entries()) {
222
336
  for (const path of affected(change)) {
223
337
  const normalized = normalize(path);
224
338
  if (normalized === null || normalized === '')
@@ -226,22 +340,41 @@ function guard(changes, input) {
226
340
  if (covered(input.providerOwnedPaths, normalized)) {
227
341
  return refuse('provider-owned-path', normalized);
228
342
  }
229
- const operation = change.kind === 'remove' ? 'delete' : 'write';
230
- if (!covered(input.operations[operation], normalized))
231
- return refuse(`${operation}-grant`, normalized);
343
+ for (const operation of change.kind === 'remove' ? removing : WRITE)
344
+ if (!input.local && !covered(input.operations[operation], normalized))
345
+ return refuse(`${operation}-grant`, normalized);
232
346
  }
233
- if (change.kind === 'create') {
234
- if (existing.has(change.path))
347
+ if (change.kind === 'create' || change.kind === 'arrival') {
348
+ const taken = existing.has(change.path) ? change.path : occupied(change.path);
349
+ if (taken !== null)
350
+ return refuse('path-collision', taken);
351
+ existing.add(change.path);
352
+ continue;
353
+ }
354
+ if (change.kind === 'binary') {
355
+ if (input.visible.documents[change.path] !== undefined ||
356
+ (existing.has(change.path) &&
357
+ input.bases.some((base) => base.kind === 'absent' && base.path === change.path))) {
235
358
  return refuse('path-collision', change.path);
359
+ }
360
+ const taken = occupied(change.path);
361
+ if (taken !== null)
362
+ return refuse('path-collision', taken);
363
+ if (!input.local &&
364
+ existing.has(change.path) &&
365
+ input.visible.revisions[departure(changes, index)] === undefined) {
366
+ return refuse('unknown-revision', change.path);
367
+ }
236
368
  existing.add(change.path);
237
369
  continue;
238
370
  }
239
371
  if (change.kind === 'move') {
240
372
  if (!existing.has(change.path))
241
373
  return refuse('path-absent', change.path);
242
- if (existing.has(change.to))
243
- return refuse('path-collision', change.to);
244
- if (unrevisionedDocument(input.visible, change.path)) {
374
+ const taken = existing.has(change.to) ? change.to : occupied(change.to);
375
+ if (taken !== null)
376
+ return refuse('path-collision', taken);
377
+ if (unrevised(input, change.path)) {
245
378
  return refuse('unknown-revision', change.path);
246
379
  }
247
380
  existing.delete(change.path);
@@ -251,30 +384,53 @@ function guard(changes, input) {
251
384
  if (!existing.has(change.path))
252
385
  return refuse('path-absent', change.path);
253
386
  if (change.kind === 'remove') {
254
- if (unrevisionedDocument(input.visible, change.path)) {
387
+ if (unrevised(input, change.path)) {
255
388
  return refuse('unknown-revision', change.path);
256
389
  }
257
390
  existing.delete(change.path);
258
391
  continue;
259
392
  }
260
393
  if (change.kind === 'replace') {
261
- if (input.visible.revisions[change.path] === undefined) {
394
+ if (unrevised(input, change.path)) {
262
395
  return refuse('unknown-revision', change.path);
263
396
  }
264
397
  continue;
265
398
  }
266
- if (input.visible.collection.documents[change.path] === undefined) {
399
+ if (input.visible.collection.document(change.path) === undefined) {
267
400
  return refuse('path-absent', change.path);
268
401
  }
269
402
  }
270
403
  return null;
271
404
  }
272
- function unrevisionedDocument(visible, path) {
273
- return visible.revisions[path] === undefined && !visible.binaryTargets.includes(path);
405
+ function unrevised(input, path) {
406
+ return (!input.local && input.queued?.has(path) !== true && input.visible.revisions[path] === undefined);
407
+ }
408
+ export function queuedDocuments(queue) {
409
+ const paths = new Set();
410
+ for (const batch of queue) {
411
+ for (const change of batch.changes) {
412
+ if (change.kind === 'binary' || (change.kind === 'arrival' && !('content' in change)))
413
+ continue;
414
+ if (change.kind === 'remove')
415
+ paths.delete(change.path);
416
+ else if (change.kind !== 'move')
417
+ paths.add(change.path);
418
+ else if (paths.delete(change.path))
419
+ paths.add(change.to);
420
+ }
421
+ }
422
+ return paths;
423
+ }
424
+ function identity(precondition) {
425
+ const part = precondition.kind === 'field' || precondition.kind === 'section' ? precondition.name : '';
426
+ return `${precondition.kind} ${precondition.path} ${part}`;
274
427
  }
275
428
  function existence(visible, path) {
276
429
  const revision = visible.revisions[path];
277
- return revision === undefined ? { kind: 'present', path } : { kind: 'revision', path, revision };
430
+ if (revision !== undefined)
431
+ return { kind: 'revision', path, revision };
432
+ const content = visible.documents[path];
433
+ return content === undefined ? { kind: 'present', path } : { kind: 'content', path, content };
278
434
  }
279
435
  function sameTarget(precondition, change) {
280
436
  if (precondition.kind !== change.kind || precondition.path !== change.path)
@@ -282,25 +438,25 @@ function sameTarget(precondition, change) {
282
438
  return ((precondition.kind !== 'field' && precondition.kind !== 'section') ||
283
439
  ((change.kind === 'field' || change.kind === 'section') && precondition.name === change.name));
284
440
  }
285
- function preconditionsOf(changes, visible, bases) {
441
+ function* preconditionsOf(changes, visible, bases) {
286
442
  const preconditions = [];
287
443
  const seen = new Set();
288
444
  const add = (precondition) => {
289
- const part = precondition.kind === 'field' || precondition.kind === 'section' ? precondition.name : '';
290
- const identity = `${precondition.kind} ${precondition.path} ${part}`;
291
- if (seen.has(identity))
445
+ const held = identity(precondition);
446
+ if (seen.has(held))
292
447
  return;
293
- seen.add(identity);
448
+ seen.add(held);
294
449
  preconditions.push(precondition);
295
450
  };
296
451
  for (const change of changes) {
452
+ yield ROW_WEIGHT;
297
453
  if (change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble') {
298
454
  const based = bases.find((base) => sameTarget(base, change));
299
455
  if (based !== undefined) {
300
456
  add(based);
301
457
  continue;
302
458
  }
303
- const document = visible.collection.documents[change.path];
459
+ const document = visible.collection.document(change.path);
304
460
  if (change.kind === 'field') {
305
461
  add({
306
462
  kind: 'field',
@@ -325,6 +481,13 @@ function preconditionsOf(changes, visible, bases) {
325
481
  add({ kind: 'absent', path: change.path });
326
482
  continue;
327
483
  }
484
+ if (change.kind === 'binary') {
485
+ const revision = visible.revisions[change.path];
486
+ add(visible.binaries[change.path] === undefined || revision === undefined
487
+ ? { kind: 'absent', path: change.path }
488
+ : { kind: 'revision', path: change.path, revision });
489
+ continue;
490
+ }
328
491
  add(existence(visible, change.path));
329
492
  if (change.kind === 'move')
330
493
  add({ kind: 'absent', path: change.to });
@@ -334,10 +497,13 @@ function preconditionsOf(changes, visible, bases) {
334
497
  function depth(path) {
335
498
  return path.split('/').length;
336
499
  }
337
- function exceeded(limits, changes, resulting) {
338
- const written = changes.map((change) => (change.kind === 'move' ? change.to : change.path));
500
+ function* exceeding(limits, changes, effective, resulting) {
501
+ const written = effective.map((change) => (change.kind === 'move' ? change.to : change.path));
339
502
  const created = changes
340
- .filter((change) => change.kind === 'create' || change.kind === 'move')
503
+ .filter((change) => change.kind === 'create' ||
504
+ change.kind === 'move' ||
505
+ change.kind === 'binary' ||
506
+ change.kind === 'arrival')
341
507
  .map((change) => (change.kind === 'move' ? change.to : change.path));
342
508
  for (const limit of limits) {
343
509
  const value = limit.value;
@@ -351,9 +517,13 @@ function exceeded(limits, changes, resulting) {
351
517
  return refuse('provider-limit', undefined, limit.name);
352
518
  }
353
519
  if (limit.name === 'document-size') {
354
- const large = written.find((path) => (resulting.documents[path] ?? '').length > value);
355
- if (large !== undefined)
356
- return refuse('provider-limit', large, limit.name);
520
+ for (const path of written) {
521
+ const text = resulting.documents[path] ?? '';
522
+ if (new TextEncoder().encode(text).byteLength > value) {
523
+ return refuse('provider-limit', path, limit.name);
524
+ }
525
+ yield ROW_WEIGHT + text.length;
526
+ }
357
527
  }
358
528
  if (limit.name === 'path-length') {
359
529
  const long = created.find((path) => path.length > value);
@@ -368,67 +538,505 @@ function exceeded(limits, changes, resulting) {
368
538
  }
369
539
  return null;
370
540
  }
371
- export function buildBatch(input) {
372
- const structural = input.changes.find((change) => change.kind === 'move' || change.kind === 'remove');
373
- if (structural !== undefined &&
374
- Object.entries(input.visible.collection.documents).some(([path, document]) => document.type !== undefined &&
375
- document.type !== 'any' &&
376
- governing(input.composition, path).status === 'unavailable'))
377
- return refuse('model-unavailable', structural.path);
378
- if (input.changes.length === 0)
379
- return refuse('no-change');
380
- const rewritten = rewrites(input.changes, input.visible);
381
- if (rewritten.kind === 'refused')
382
- return rewritten;
383
- const changes = input.rewriting === 'provider' ? input.changes : rewritten.changes;
384
- const refusal = guard(changes, input);
385
- if (refusal !== null)
386
- return refusal;
387
- const preconditions = preconditionsOf(changes, input.visible, input.bases);
388
- const predicted = input.rewriting === 'provider'
389
- ? anticipated(changes, preconditions, rewritten.changes, input.visible)
390
- : { changes, preconditions };
391
- const resulting = overlay(input.visible, [
392
- { id: '', baseRoomVersion: input.baseRoomVersion, ...predicted },
393
- ]);
394
- const removed = new Set(changes.filter((change) => change.kind === 'remove').map((change) => change.path));
541
+ function shellRelative(text, path, handle, definition) {
542
+ if (!text.includes(`/rooms/${handle}/`))
543
+ return null;
544
+ const parsed = parse(text, path);
545
+ if (!parsed.ok)
546
+ return null;
547
+ const result = relativize(parsed.document, path, handle, definition(parsed.document.type));
548
+ return result.outcome === 'applied' && result.document.content !== text ? result.document : null;
549
+ }
550
+ function relativeField(path, name, value, handle, definition) {
551
+ const opened = parse('---\n---\n');
552
+ if (definition === undefined ||
553
+ !opened.ok ||
554
+ !JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
555
+ return value;
556
+ const held = apply(opened.document, [{ kind: 'field', name, value, prior: undefined }]);
557
+ if (held.outcome !== 'applied')
558
+ return value;
559
+ const result = relativize(held.document, path, handle, definition);
560
+ return result.outcome === 'applied' ? result.document.fields[name] : value;
561
+ }
562
+ function relativeChanges(input) {
563
+ const { handle, composition, visible } = input;
564
+ const typed = (path) => (type) => definitionOf(governing(composition, path), type);
565
+ const body = (path, text) => shellRelative(text, path, handle, () => undefined)?.content ?? text;
566
+ return input.changes.map((change) => {
567
+ const definition = typed(change.path);
568
+ if (change.kind === 'field') {
569
+ const type = visible.collection.document(change.path)?.type;
570
+ const value = relativeField(change.path, change.name, change.value, handle, definition(type));
571
+ return value === change.value ? change : { ...change, value };
572
+ }
573
+ if (change.kind === 'section' || change.kind === 'preamble') {
574
+ const value = body(change.path, change.value);
575
+ return value === change.value ? change : { ...change, value };
576
+ }
577
+ if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
578
+ const content = shellRelative(change.content, change.path, handle, definition)?.content ?? change.content;
579
+ return content === change.content ? change : { ...change, content };
580
+ }
581
+ if (change.kind !== 'create')
582
+ return change;
583
+ const type = typeof change.fields.type === 'string' ? change.fields.type : undefined;
584
+ const fields = Object.fromEntries(Object.entries(change.fields).map(([name, value]) => [
585
+ name,
586
+ relativeField(change.path, name, value, handle, definition(type)),
587
+ ]));
588
+ const sections = Object.fromEntries(Object.entries(change.sections).map(([name, value]) => [name, body(change.path, value)]));
589
+ return { ...change, fields, sections };
590
+ });
591
+ }
592
+ function unqueueable(file) {
593
+ return { kind: 'refused', refusal: { code: 'unknown-revision', retryable: false, file } };
594
+ }
595
+ function isPart(change) {
596
+ return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
597
+ }
598
+ function touched(change) {
599
+ return change.kind === 'move' ? [change.path, change.to] : [change.path];
600
+ }
601
+ function partsApplied(content, path, changes) {
602
+ const parsed = parse(content, path);
603
+ if (!parsed.ok)
604
+ return null;
605
+ const document = parsed.document;
606
+ const parts = new Map();
607
+ for (const change of changes) {
608
+ if (change.kind === 'field')
609
+ parts.set(`field ${change.name}`, {
610
+ kind: 'field',
611
+ name: change.name,
612
+ value: change.value,
613
+ prior: document.fields[change.name],
614
+ });
615
+ else if (change.kind === 'section')
616
+ parts.set(`section ${change.name}`, {
617
+ kind: 'section',
618
+ name: change.name,
619
+ value: change.value,
620
+ prior: document.sections[change.name] ?? '',
621
+ });
622
+ else
623
+ parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
624
+ }
625
+ const applied = apply(document, [...parts.values()]);
626
+ return applied.outcome === 'applied' ? serialize(applied.document) : null;
627
+ }
628
+ function settledParts(changes, deferred) {
629
+ for (const [held, parts] of deferred) {
630
+ const content = partsApplied(held.content, held.path, parts);
631
+ if (content === null)
632
+ return held.path;
633
+ changes[changes.indexOf(held)] = { ...held, content };
634
+ }
635
+ deferred.clear();
636
+ return null;
637
+ }
638
+ function folded(changes, change, appending, deferred) {
639
+ const held = changes.find((item) => item.path === change.path && (item.kind === 'create' || item.kind === 'replace'));
640
+ const whole = held === undefined ? -1 : changes.indexOf(held);
641
+ const occupied = changes.some((item) => !isPart(item) && item !== held && touched(item).includes(change.path));
642
+ if (isPart(change)) {
643
+ const same = changes.findIndex((item) => item.kind === change.kind &&
644
+ item.path === change.path &&
645
+ (change.kind === 'preamble' || ('name' in item && item.name === change.name)));
646
+ if (same !== -1) {
647
+ changes[same] = change;
648
+ return 'folded';
649
+ }
650
+ if (held === undefined) {
651
+ if (!appending)
652
+ return 'skipped';
653
+ if (occupied)
654
+ return 'refused';
655
+ changes.push(change);
656
+ return 'appended';
657
+ }
658
+ if ('content' in held) {
659
+ const parts = deferred.get(held);
660
+ if (parts === undefined)
661
+ deferred.set(held, [change]);
662
+ else
663
+ parts.push(change);
664
+ return 'folded';
665
+ }
666
+ if (change.kind === 'preamble')
667
+ return 'refused';
668
+ changes[whole] =
669
+ change.kind === 'field'
670
+ ? { ...held, fields: { ...held.fields, [change.name]: change.value } }
671
+ : { ...held, sections: { ...held.sections, [change.name]: change.value } };
672
+ return 'folded';
673
+ }
674
+ if (change.kind === 'replace') {
675
+ if (held !== undefined) {
676
+ if (!('content' in held))
677
+ return 'refused';
678
+ changes[whole] = { ...held, content: change.content };
679
+ return 'folded';
680
+ }
681
+ const parts = changes.filter((item) => isPart(item) && item.path === change.path);
682
+ if (parts.length === 0 && !appending)
683
+ return 'skipped';
684
+ if (occupied)
685
+ return 'refused';
686
+ changes.splice(0, changes.length, ...changes.filter((item) => !parts.includes(item)), change);
687
+ return 'appended';
688
+ }
689
+ if (changes.some((item) => touched(change).some((path) => touched(item).includes(path))))
690
+ return 'refused';
691
+ changes.push(change);
692
+ return 'appended';
693
+ }
694
+ function* relinked(batch, move, before, after, appending) {
695
+ const derived = yield* rewrites([move], after);
696
+ if (derived.kind === 'refused')
697
+ return derived;
698
+ const changes = [...batch.changes];
699
+ const appended = [];
700
+ let changed = false;
701
+ const deferred = new Map();
702
+ for (const change of derived.changes) {
703
+ if (change === move)
704
+ continue;
705
+ const unsettled = isPart(change) ? null : settledParts(changes, deferred);
706
+ if (unsettled !== null)
707
+ return unqueueable(unsettled);
708
+ const outcome = folded(changes, change, appending, deferred);
709
+ if (outcome === 'refused')
710
+ return unqueueable(change.path);
711
+ if (outcome === 'appended')
712
+ appended.push(change);
713
+ if (outcome !== 'skipped')
714
+ changed = true;
715
+ }
716
+ const unsettled = settledParts(changes, deferred);
717
+ if (unsettled !== null)
718
+ return unqueueable(unsettled);
719
+ if (!changed)
720
+ return { kind: 'relinked', batch };
721
+ const held = new Set(batch.preconditions.map(identity));
722
+ const added = yield* preconditionsOf(appended, before, []);
723
+ return {
724
+ kind: 'relinked',
725
+ batch: immutable({
726
+ ...batch,
727
+ id: changeBatchIdentity(),
728
+ changes,
729
+ preconditions: [
730
+ ...batch.preconditions,
731
+ ...added.filter((precondition) => !held.has(identity(precondition))),
732
+ ],
733
+ }),
734
+ };
735
+ }
736
+ function leading(changes, at) {
737
+ const from = departure(changes, at);
738
+ return new Set(changes.filter((change, index) => index < at && change.kind === 'move' && departure(changes, index) === from));
739
+ }
740
+ function created(batch, at) {
741
+ const file = batch.changes[at];
742
+ return (leading(batch.changes, at).size === 0 &&
743
+ batch.preconditions.some((precondition) => precondition.kind === 'absent' && precondition.path === file.path));
744
+ }
745
+ function withdrawnFile(batch, change, at) {
746
+ const file = batch.changes[at];
747
+ const from = departure(batch.changes, at);
748
+ const moves = leading(batch.changes, at);
749
+ const fresh = created(batch, at);
750
+ const vacated = new Set([...moves].map((move) => move.to));
751
+ if (fresh)
752
+ vacated.add(file.path);
753
+ const replaced = fresh
754
+ ? change.kind === 'remove'
755
+ ? []
756
+ : [{ ...file, path: change.to }]
757
+ : change.kind === 'remove'
758
+ ? [{ kind: 'remove', path: from }]
759
+ : change.to === from
760
+ ? [{ ...file, path: from }]
761
+ : [
762
+ { kind: 'move', path: from, to: change.to },
763
+ { ...file, path: change.to },
764
+ ];
765
+ const added = change.kind === 'move' && (fresh || change.to !== from)
766
+ ? [{ kind: 'absent', path: change.to }]
767
+ : [];
768
+ const changes = batch.changes.flatMap((item) => item === file ? replaced : item.kind === 'move' && moves.has(item) ? [] : [item]);
769
+ if (changes.length === 0)
770
+ return null;
771
+ const preconditions = [
772
+ ...added,
773
+ ...batch.preconditions.filter((precondition) => precondition.kind !== 'absent' || !vacated.has(precondition.path)),
774
+ ];
775
+ return immutable({ ...batch, id: changeBatchIdentity(), changes, preconditions });
776
+ }
777
+ export function* requeuing(queue, changes, visible, changeable, state) {
778
+ const next = [...queue];
779
+ const origins = new Map();
780
+ const withdrawn = new Map();
781
+ const owners = new Set();
782
+ const queued = new Set();
783
+ const kept = [];
784
+ const originOf = (batch) => origins.get(batch) ?? batch.id;
785
+ const open = (batch) => origins.has(batch) || changeable(batch);
786
+ for (const change of changes) {
787
+ const held = visible.binaries[change.path];
788
+ if ((change.kind !== 'move' && change.kind !== 'remove') || held?.state !== 'pending') {
789
+ kept.push(change);
790
+ continue;
791
+ }
792
+ let index = next.length - 1;
793
+ while (index >= 0 && queuedFile(next[index], change.path, held.localId) === -1)
794
+ index -= 1;
795
+ const owner = next[index];
796
+ if (owner === undefined || !open(owner))
797
+ return unqueueable(change.path);
798
+ const following = next.slice(index);
799
+ const rebuilt = [];
800
+ let before = change.kind === 'move' ? yield* state(next.slice(0, index)) : visible;
801
+ for (const [offset, batch] of following.entries()) {
802
+ let current = batch;
803
+ if (change.kind === 'move') {
804
+ const after = yield* state(next.slice(0, index + offset + 1));
805
+ const appending = offset === 0 && created(batch, queuedFile(batch, change.path, held.localId));
806
+ const relinking = yield* relinked(batch, change, before, after, appending);
807
+ if (relinking.kind === 'refused')
808
+ return relinking;
809
+ current = relinking.batch;
810
+ before = after;
811
+ }
812
+ if (current !== batch && (!open(batch) || batch.between !== undefined))
813
+ return unqueueable(change.path);
814
+ rebuilt.push(offset === 0
815
+ ? withdrawnFile(current, change, queuedFile(current, change.path, held.localId))
816
+ : current);
817
+ }
818
+ for (const [offset, batch] of following.entries()) {
819
+ const replaced = rebuilt[offset];
820
+ if (replaced === batch)
821
+ continue;
822
+ const origin = originOf(batch);
823
+ withdrawn.set(origin, replaced);
824
+ if (replaced !== null)
825
+ origins.set(replaced, origin);
826
+ }
827
+ owners.add(originOf(owner));
828
+ next.splice(index, following.length, ...rebuilt.filter((batch) => batch !== null));
829
+ queued.add(change.path);
830
+ }
831
+ return {
832
+ kind: 'requeued',
833
+ queue: withdrawn.size === 0 ? queue : next,
834
+ changes: kept,
835
+ queued,
836
+ withdrawn,
837
+ owners,
838
+ };
839
+ }
840
+ function covering(precondition) {
841
+ return precondition.kind === 'field' ||
842
+ precondition.kind === 'section' ||
843
+ precondition.kind === 'preamble'
844
+ ? identity(precondition)
845
+ : `path ${precondition.path}`;
846
+ }
847
+ export function joined(queue, owner, extra) {
848
+ const later = new Set(queue.slice(queue.indexOf(owner) + 1).flatMap((batch) => batch.changes.flatMap(touched)));
849
+ const clash = extra.changes.flatMap(touched).find((path) => later.has(path));
850
+ if (clash !== undefined)
851
+ return unqueueable(clash);
852
+ const changes = [...owner.changes];
853
+ const deferred = new Map();
854
+ for (const change of extra.changes) {
855
+ const unsettled = isPart(change) ? null : settledParts(changes, deferred);
856
+ if (unsettled !== null)
857
+ return unqueueable(unsettled);
858
+ if (folded(changes, change, true, deferred) === 'refused')
859
+ return unqueueable(change.path);
860
+ }
861
+ const unsettled = settledParts(changes, deferred);
862
+ if (unsettled !== null)
863
+ return unqueueable(unsettled);
864
+ const held = new Set(owner.preconditions.map(covering));
865
+ const written = new Set(owner.changes.flatMap((change) => change.kind === 'create' || change.kind === 'replace' ? [change.path] : []));
866
+ const preconditions = [
867
+ ...owner.preconditions,
868
+ ...extra.preconditions.filter((precondition) => {
869
+ const key = covering(precondition);
870
+ return (!held.has(key) && (key === `path ${precondition.path}` || !written.has(precondition.path)));
871
+ }),
872
+ ];
873
+ const batch = immutable({
874
+ ...owner,
875
+ ...(extra.validation === undefined ? {} : { validation: extra.validation }),
876
+ id: changeBatchIdentity(),
877
+ changes,
878
+ preconditions,
879
+ });
880
+ return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
881
+ }
882
+ export function* limiting(queue, batch, limits, state) {
883
+ const at = queue.indexOf(batch);
884
+ const rewritten = yield* providerRewritten(batch, yield* state(queue.slice(0, at)));
885
+ const limited = yield* exceeding(limits, batch.changes, rewritten.changes, yield* state(queue.slice(0, at + 1)));
886
+ return limited?.kind === 'refused' ? limited.refusal : null;
887
+ }
888
+ function typedText(content) {
889
+ if (content === undefined)
890
+ return false;
891
+ const parsed = parse(content);
892
+ return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
893
+ }
894
+ function touchedLibrary(resulting, touched, composition) {
895
+ const entries = touched
896
+ .flatMap((path) => {
897
+ const content = resulting.documents[path];
898
+ const binary = resulting.binaries[path] !== undefined;
899
+ return content === undefined && !binary ? [] : [{ path, content: content ?? null }];
900
+ })
901
+ .sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
902
+ const contents = new Map(entries.map(({ path, content }) => [path, content]));
903
+ const binaries = new Set(Object.keys(resulting.binaries).filter((path) => resulting.binaries[path] !== undefined));
904
+ return validatedCollection(() => Promise.resolve({ entries, next: null }), async (paths) => {
905
+ const known = new Map();
906
+ const pace = paced();
907
+ for (const path of paths) {
908
+ const content = contents.get(path);
909
+ if (binaries.has(path))
910
+ known.set(path, { type: undefined, binary: true });
911
+ else if (content !== undefined && content !== null) {
912
+ const read = parse(content, path);
913
+ if (read.ok)
914
+ known.set(path, { type: read.document.type ?? 'any', binary: false });
915
+ await pace(content.length + ROW_WEIGHT);
916
+ }
917
+ }
918
+ return known;
919
+ }, composition);
920
+ }
921
+ async function judging(visible, resulting, composition, validate, removed, touched) {
395
922
  if (removed.size > 0) {
396
- const referenced = incoming(parseCollection(resulting.documents, input.composition), removed);
923
+ const referenced = await stepped(incoming(collectionOf(resulting.documents, composition), removed, null));
397
924
  if (referenced !== null)
398
925
  return referenced;
399
926
  }
400
- const typed = (content) => {
401
- if (content === undefined)
402
- return false;
403
- const parsed = parse(content);
404
- return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
405
- };
406
- const typedPaths = [...new Set(predicted.changes.flatMap(affected))].filter((path) => typed(input.visible.documents[path]) || typed(resulting.documents[path]));
407
- const needsModel = typedPaths.length > 0;
408
- const withoutModel = typedPaths.find((path) => governing(input.composition, path).status === 'unavailable');
927
+ const typedPaths = [];
928
+ const pace = paced();
929
+ for (const path of touched) {
930
+ const before = visible.documents[path];
931
+ const after = resulting.documents[path];
932
+ if (typedText(before) || typedText(after))
933
+ typedPaths.push(path);
934
+ await pace((before?.length ?? 0) + (after?.length ?? 0) + ROW_WEIGHT);
935
+ }
936
+ const withoutModel = typedPaths.find((path) => governing(composition, path).status === 'unavailable');
409
937
  if (withoutModel !== undefined)
410
938
  return refuse('model-unavailable', withoutModel);
411
- const validationText = needsModel
412
- ? resulting.documents
413
- : Object.fromEntries(Object.entries(resulting.documents).filter(([path]) => predicted.changes.some((change) => affected(change).includes(path))));
414
- const verdict = validateCollection(validationText, resulting.binaryTargets, input.composition);
939
+ const verdict = typedPaths.length > 0
940
+ ? await validate(resulting, touched)
941
+ : await touchedLibrary(resulting, touched, composition);
415
942
  if (verdict.verdict === 'refuse') {
416
943
  return {
417
944
  kind: 'refused',
418
945
  refusal: {
419
- rule: verdict.rule,
946
+ code: verdict.rule,
947
+ retryable: false,
420
948
  file: verdict.file,
421
949
  ...(verdict.location === undefined ? {} : { location: verdict.location }),
422
950
  ...(verdict.referrers === undefined ? {} : { referrers: verdict.referrers }),
423
951
  },
424
952
  };
425
953
  }
426
- const limited = exceeded(input.providerLimits, changes, resulting);
954
+ return {
955
+ kind: 'judged',
956
+ validated: typedPaths
957
+ .map((path) => governing(composition, path))
958
+ .find((model) => model.status !== 'unavailable'),
959
+ };
960
+ }
961
+ function placed(reference) {
962
+ const key = roomPathOf(reference.handle, reference.path);
963
+ if (reference.kind === 'link')
964
+ return `link ${key}`;
965
+ if (reference.kind === 'item')
966
+ return `section ${reference.section} ${key}`;
967
+ const pointer = reference.pointer
968
+ .split('/')
969
+ .map((segment) => (ARRAY_INDEX.test(segment) ? '-' : segment))
970
+ .join('/');
971
+ return `field ${pointer} ${key}`;
972
+ }
973
+ function roomReferencesIn(path, content, composition) {
974
+ if (content === undefined)
975
+ return [];
976
+ const parsed = parse(content, path);
977
+ if (!parsed.ok)
978
+ return [];
979
+ return roomReferences(parsed.document, definitionOf(governing(composition, path), parsed.document.type));
980
+ }
981
+ function* admittedRooms(changes, visible, resulting, composition) {
982
+ const from = new Map();
983
+ for (const [index, change] of changes.entries()) {
984
+ if (change.kind === 'move')
985
+ from.set(change.to, departure(changes, index));
986
+ }
987
+ for (const path of [...new Set(changes.flatMap(affected))].sort()) {
988
+ const now = resulting.documents[path];
989
+ if (now === undefined)
990
+ continue;
991
+ const held = new Set(roomReferencesIn(path, visible.documents[from.get(path) ?? path], composition).map(placed));
992
+ for (const reference of roomReferencesIn(path, now, composition)) {
993
+ if (reference.kind === 'link' || reference.crossRoom === true)
994
+ continue;
995
+ if (held.has(placed(reference)))
996
+ continue;
997
+ return refuse('unresolved-reference', path, reference.kind === 'field' ? reference.pointer : reference.section);
998
+ }
999
+ yield ROW_WEIGHT + now.length;
1000
+ }
1001
+ return null;
1002
+ }
1003
+ function validationOf(validated) {
1004
+ return validated === undefined
1005
+ ? {}
1006
+ : {
1007
+ validation: {
1008
+ identity: validated.snapshot.identity,
1009
+ version: validated.snapshot.version,
1010
+ source: validated.status,
1011
+ },
1012
+ };
1013
+ }
1014
+ async function batching(input) {
1015
+ const changes = relativeChanges(input);
1016
+ const structural = changes.find((change) => change.kind === 'move' || change.kind === 'remove');
1017
+ if (structural !== undefined && input.unmodeled) {
1018
+ return refuse('model-unavailable', structural.path);
1019
+ }
1020
+ if (changes.length === 0)
1021
+ return refuse('no-change');
1022
+ const rewritten = await stepped(rewrites(changes, input.visible));
1023
+ if (rewritten.kind === 'refused')
1024
+ return rewritten;
1025
+ const refusal = guard(changes, input, WRITE);
1026
+ if (refusal !== null)
1027
+ return refusal;
1028
+ const preconditions = await stepped(preconditionsOf(changes, input.visible, input.bases));
1029
+ const predicted = await stepped(anticipated(changes, preconditions, rewritten.changes, input.visible));
1030
+ const resulting = await stepped(overlaying(input.visible, [{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted }]));
1031
+ const judged = await judging(input.visible, resulting, input.composition, input.validate, new Set(changes.filter((change) => change.kind === 'remove').map((change) => change.path)), [...new Set(predicted.changes.flatMap(affected))]);
1032
+ if (judged.kind === 'refused')
1033
+ return judged;
1034
+ const crossing = await stepped(admittedRooms(changes, input.visible, resulting, input.composition));
1035
+ if (crossing !== null)
1036
+ return crossing;
1037
+ const limited = await stepped(exceeding(input.providerLimits, changes, predicted.changes, resulting));
427
1038
  if (limited !== null)
428
1039
  return limited;
429
- const validated = typedPaths
430
- .map((path) => governing(input.composition, path))
431
- .find((model) => model.status !== 'unavailable');
432
1040
  return {
433
1041
  kind: 'batch',
434
1042
  batch: immutable({
@@ -437,15 +1045,127 @@ export function buildBatch(input) {
437
1045
  changes,
438
1046
  preconditions,
439
1047
  ...(input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy }),
440
- ...(validated === undefined
441
- ? {}
442
- : {
443
- validation: {
444
- identity: validated.snapshot.identity,
445
- version: validated.snapshot.version,
446
- source: validated.status,
447
- },
448
- }),
1048
+ ...validationOf(judged.validated),
449
1049
  }),
450
1050
  };
451
1051
  }
1052
+ export const MOVE_DOCUMENTS = 'move-documents';
1053
+ function heldAt(visible, path) {
1054
+ return visible.documents[path] !== undefined || visible.binaries[path] !== undefined;
1055
+ }
1056
+ function arrivingText(path, to, source, moved) {
1057
+ const content = source.visible.documents[path];
1058
+ const document = source.visible.collection.document(path);
1059
+ if (document === undefined)
1060
+ return content;
1061
+ const definition = definitionOf(governing(source.composition, path), document.type);
1062
+ const changes = crossed(document, definition, path, (named, href) => {
1063
+ const at = moved.get(named);
1064
+ if (at !== undefined)
1065
+ return target(to, href) === at ? undefined : relative(to, at);
1066
+ return heldAt(source.visible, named) ? roomPathOf(source.handle, named) : undefined;
1067
+ });
1068
+ if (changes.length === 0)
1069
+ return content;
1070
+ const applied = apply(document, changes);
1071
+ return applied.outcome === 'applied' ? serialize(applied.document) : content;
1072
+ }
1073
+ function arrivalOf({ path, to }, source, moved) {
1074
+ const { documents, revisions, fileIds, created, changes, binaries } = source.visible;
1075
+ const kept = {
1076
+ ...(fileIds[path] === undefined ? {} : { fileId: fileIds[path] }),
1077
+ ...(created[path] === undefined ? {} : { created: created[path] }),
1078
+ ...(changes[path] === undefined ? {} : { change: changes[path] }),
1079
+ };
1080
+ const heads = {
1081
+ ...(revisions[path] === undefined ? {} : { revision: revisions[path] }),
1082
+ ...kept,
1083
+ };
1084
+ const binary = binaries[path];
1085
+ if (binary?.state === 'accepted') {
1086
+ const { byteLength, mediaType, sha256 } = binary;
1087
+ return {
1088
+ kind: 'arrival',
1089
+ path: to,
1090
+ binary: { byteLength, mediaType, sha256 },
1091
+ ...heads,
1092
+ };
1093
+ }
1094
+ const content = arrivingText(path, to, source, moved);
1095
+ return {
1096
+ kind: 'arrival',
1097
+ path: to,
1098
+ content,
1099
+ ...(content === documents[path] ? heads : kept),
1100
+ };
1101
+ }
1102
+ async function moving(input) {
1103
+ const { source, destination, moves } = input;
1104
+ if (moves.length === 0)
1105
+ return refuse('no-change');
1106
+ const removals = moves.map(({ path }) => ({ kind: 'remove', path }));
1107
+ const leaves = guard(removals, { ...source, local: input.local, bases: [] }, MOVE_OUT);
1108
+ if (leaves !== null)
1109
+ return leaves;
1110
+ const uploading = moves.find(({ path }) => source.visible.binaries[path]?.state === 'pending');
1111
+ if (uploading !== undefined)
1112
+ return refuse('unknown-revision', uploading.path);
1113
+ const moved = new Map(moves.map(({ path, to }) => [path, to]));
1114
+ const arrivals = moves.map((move) => arrivalOf(move, source, moved));
1115
+ const arrives = guard(arrivals, { ...destination, local: input.local, bases: [] }, WRITE);
1116
+ if (arrives !== null)
1117
+ return arrives;
1118
+ const bound = input.providerLimits.find((limit) => limit.name === MOVE_DOCUMENTS)?.value;
1119
+ if (typeof bound === 'number' && moves.length > bound) {
1120
+ return refuse('provider-limit', undefined, MOVE_DOCUMENTS);
1121
+ }
1122
+ if (source.unmodeled)
1123
+ return refuse('model-unavailable', moves[0].path);
1124
+ const id = changeBatchIdentity();
1125
+ const between = {
1126
+ source: { room: source.room, baseRoomVersion: source.baseRoomVersion },
1127
+ destination: {
1128
+ room: destination.room,
1129
+ baseRoomVersion: destination.baseRoomVersion,
1130
+ handle: destination.handle,
1131
+ },
1132
+ moves,
1133
+ };
1134
+ const written = input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy };
1135
+ const leavingBatch = {
1136
+ id,
1137
+ baseRoomVersion: source.baseRoomVersion,
1138
+ changes: removals,
1139
+ preconditions: moves.map(({ path }) => existence(source.visible, path)),
1140
+ ...written,
1141
+ between,
1142
+ };
1143
+ const effective = await stepped(leavingRewritten(leavingBatch, source.visible));
1144
+ const left = await stepped(overlaying(source.visible, [effective]));
1145
+ const sourceJudged = await judging(source.visible, left, source.composition, source.validate, new Set(moves.map(({ path }) => path)), [...new Set(effective.changes.map((change) => change.path))]);
1146
+ if (sourceJudged.kind === 'refused')
1147
+ return sourceJudged;
1148
+ const arrivingBatch = {
1149
+ id,
1150
+ baseRoomVersion: destination.baseRoomVersion,
1151
+ changes: arrivals,
1152
+ preconditions: moves.map(({ to }) => ({ kind: 'absent', path: to })),
1153
+ ...written,
1154
+ between,
1155
+ };
1156
+ const arrived = await stepped(overlaying(destination.visible, [arrivingBatch]));
1157
+ const destinationJudged = await judging(destination.visible, arrived, destination.composition, destination.validate, new Set(), moves.map(({ to }) => to));
1158
+ if (destinationJudged.kind === 'refused')
1159
+ return destinationJudged;
1160
+ const limited = (await stepped(exceeding(input.providerLimits, removals, effective.changes, left))) ??
1161
+ (await stepped(exceeding(input.providerLimits, arrivals, arrivals, arrived)));
1162
+ if (limited !== null)
1163
+ return limited;
1164
+ return { kind: 'moved', leaving: immutable(leavingBatch), arriving: immutable(arrivingBatch) };
1165
+ }
1166
+ export function buildMove(input) {
1167
+ return moving(input);
1168
+ }
1169
+ export function buildBatch(input) {
1170
+ return batching(input);
1171
+ }