@agentshouse/mdruntime 0.1.0-c104334047dfe → 0.1.0-c112fd663ab59

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