@agentshouse/mdruntime 0.1.0-c97456d63baba → 0.1.0-c9873d788cfb6

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