@agentshouse/mdruntime 0.1.0-cd37f80239af0 → 0.1.0-cd4e5feb4f531

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 (366) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +328 -0
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +26 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +345 -0
  11. package/dist/adapter.d.ts +263 -98
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +3 -11
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +20 -60
  16. package/dist/{binary.d.ts → attachment.d.ts} +8 -4
  17. package/dist/attachment.d.ts.map +1 -0
  18. package/dist/attachment.js +15 -0
  19. package/dist/authored.d.ts +88 -0
  20. package/dist/authored.d.ts.map +1 -0
  21. package/dist/authored.js +345 -0
  22. package/dist/bootstrap.d.ts +9 -5
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +204 -85
  25. package/dist/cell.d.ts +6 -0
  26. package/dist/cell.d.ts.map +1 -0
  27. package/dist/cell.js +9 -0
  28. package/dist/chain.d.ts +3 -2
  29. package/dist/chain.d.ts.map +1 -1
  30. package/dist/chain.js +5 -2
  31. package/dist/change-set.d.ts +4 -0
  32. package/dist/change-set.d.ts.map +1 -1
  33. package/dist/change-set.js +6 -0
  34. package/dist/change.d.ts +7 -2
  35. package/dist/change.d.ts.map +1 -1
  36. package/dist/change.js +7 -2
  37. package/dist/chat-types.d.ts +3 -0
  38. package/dist/chat-types.d.ts.map +1 -0
  39. package/dist/chat-types.js +11 -0
  40. package/dist/client.d.ts +47 -25
  41. package/dist/client.d.ts.map +1 -1
  42. package/dist/client.js +58 -68
  43. package/dist/collection.d.ts +28 -4
  44. package/dist/collection.d.ts.map +1 -1
  45. package/dist/collection.js +34 -10
  46. package/dist/column.d.ts +5 -0
  47. package/dist/column.d.ts.map +1 -0
  48. package/dist/column.js +9 -0
  49. package/dist/compile.d.ts +10 -7
  50. package/dist/compile.d.ts.map +1 -1
  51. package/dist/compile.js +317 -348
  52. package/dist/connection.d.ts +6 -143
  53. package/dist/connection.d.ts.map +1 -1
  54. package/dist/connection.js +117 -5073
  55. package/dist/context.d.ts +2 -0
  56. package/dist/context.d.ts.map +1 -0
  57. package/dist/context.js +7 -0
  58. package/dist/conversation-live.d.ts +150 -0
  59. package/dist/conversation-live.d.ts.map +1 -0
  60. package/dist/conversation-live.js +260 -0
  61. package/dist/conversation-row.d.ts +55 -0
  62. package/dist/conversation-row.d.ts.map +1 -0
  63. package/dist/conversation-row.js +82 -0
  64. package/dist/conversation.d.ts +78 -50
  65. package/dist/conversation.d.ts.map +1 -1
  66. package/dist/conversation.js +1023 -911
  67. package/dist/declaration.d.ts +10 -0
  68. package/dist/declaration.d.ts.map +1 -0
  69. package/dist/declaration.js +24 -0
  70. package/dist/discovery.d.ts +12 -0
  71. package/dist/discovery.d.ts.map +1 -0
  72. package/dist/discovery.js +175 -0
  73. package/dist/download.d.ts +6 -0
  74. package/dist/download.d.ts.map +1 -0
  75. package/dist/download.js +42 -0
  76. package/dist/durability.d.ts +20 -0
  77. package/dist/durability.d.ts.map +1 -1
  78. package/dist/durability.js +7 -0
  79. package/dist/entry.d.ts +28 -3
  80. package/dist/entry.d.ts.map +1 -1
  81. package/dist/entry.js +134 -34
  82. package/dist/expectation.d.ts +26 -13
  83. package/dist/expectation.d.ts.map +1 -1
  84. package/dist/expectation.js +72 -19
  85. package/dist/failure.d.ts +3 -0
  86. package/dist/failure.d.ts.map +1 -1
  87. package/dist/failure.js +8 -0
  88. package/dist/figures.d.ts +49 -0
  89. package/dist/figures.d.ts.map +1 -0
  90. package/dist/figures.js +199 -0
  91. package/dist/file.d.ts +1 -1
  92. package/dist/file.d.ts.map +1 -1
  93. package/dist/file.js +3 -2
  94. package/dist/holdings.d.ts +29 -0
  95. package/dist/holdings.d.ts.map +1 -0
  96. package/dist/holdings.js +95 -0
  97. package/dist/host.d.ts +25 -6
  98. package/dist/host.d.ts.map +1 -1
  99. package/dist/host.js +128 -51
  100. package/dist/house-query.d.ts +24 -0
  101. package/dist/house-query.d.ts.map +1 -0
  102. package/dist/house-query.js +102 -0
  103. package/dist/identity.d.ts +1 -2
  104. package/dist/identity.d.ts.map +1 -1
  105. package/dist/identity.js +1 -4
  106. package/dist/index.d.ts +47 -17
  107. package/dist/index.d.ts.map +1 -1
  108. package/dist/index.js +23 -7
  109. package/dist/install.d.ts +60 -0
  110. package/dist/install.d.ts.map +1 -0
  111. package/dist/install.js +537 -0
  112. package/dist/lifecycle.d.ts +7 -0
  113. package/dist/lifecycle.d.ts.map +1 -0
  114. package/dist/lifecycle.js +292 -0
  115. package/dist/linked-file.d.ts +7 -0
  116. package/dist/linked-file.d.ts.map +1 -0
  117. package/dist/linked-file.js +16 -0
  118. package/dist/listing.d.ts +60 -0
  119. package/dist/listing.d.ts.map +1 -0
  120. package/dist/listing.js +168 -0
  121. package/dist/live.d.ts +23 -6
  122. package/dist/live.d.ts.map +1 -1
  123. package/dist/live.js +138 -38
  124. package/dist/loaded.d.ts +7 -0
  125. package/dist/loaded.d.ts.map +1 -0
  126. package/dist/loaded.js +46 -0
  127. package/dist/locks.d.ts +7 -0
  128. package/dist/locks.d.ts.map +1 -0
  129. package/dist/locks.js +34 -0
  130. package/dist/lost.d.ts +5 -0
  131. package/dist/lost.d.ts.map +1 -0
  132. package/dist/lost.js +49 -0
  133. package/dist/media.d.ts +20 -4
  134. package/dist/media.d.ts.map +1 -1
  135. package/dist/media.js +184 -37
  136. package/dist/memory.d.ts +14 -4
  137. package/dist/memory.d.ts.map +1 -1
  138. package/dist/memory.js +147 -45
  139. package/dist/messages.d.ts +44 -5
  140. package/dist/messages.d.ts.map +1 -1
  141. package/dist/messages.js +129 -39
  142. package/dist/modal.d.ts +2 -0
  143. package/dist/modal.d.ts.map +1 -0
  144. package/dist/modal.js +11 -0
  145. package/dist/model.d.ts +12 -17
  146. package/dist/model.d.ts.map +1 -1
  147. package/dist/model.js +6 -14
  148. package/dist/models.d.ts +10 -0
  149. package/dist/models.d.ts.map +1 -0
  150. package/dist/models.js +153 -0
  151. package/dist/name.d.ts +0 -1
  152. package/dist/name.d.ts.map +1 -1
  153. package/dist/name.js +3 -7
  154. package/dist/observation.d.ts +30 -13
  155. package/dist/observation.d.ts.map +1 -1
  156. package/dist/observation.js +218 -81
  157. package/dist/online.d.ts +7 -0
  158. package/dist/online.d.ts.map +1 -0
  159. package/dist/online.js +29 -0
  160. package/dist/opened.d.ts +6 -6
  161. package/dist/opened.d.ts.map +1 -1
  162. package/dist/opened.js +7 -7
  163. package/dist/opening.d.ts +4 -0
  164. package/dist/opening.d.ts.map +1 -0
  165. package/dist/opening.js +71 -0
  166. package/dist/outcome.d.ts +37 -14
  167. package/dist/outcome.d.ts.map +1 -1
  168. package/dist/outcome.js +39 -6
  169. package/dist/overlay.d.ts +24 -8
  170. package/dist/overlay.d.ts.map +1 -1
  171. package/dist/overlay.js +143 -48
  172. package/dist/page-metadata.d.ts +8 -0
  173. package/dist/page-metadata.d.ts.map +1 -0
  174. package/dist/page-metadata.js +19 -0
  175. package/dist/parts.d.ts +7 -0
  176. package/dist/parts.d.ts.map +1 -0
  177. package/dist/parts.js +36 -0
  178. package/dist/paths.d.ts +1 -0
  179. package/dist/paths.d.ts.map +1 -1
  180. package/dist/paths.js +22 -0
  181. package/dist/preparation.d.ts +46 -0
  182. package/dist/preparation.d.ts.map +1 -0
  183. package/dist/preparation.js +344 -0
  184. package/dist/presence.d.ts +58 -0
  185. package/dist/presence.d.ts.map +1 -0
  186. package/dist/presence.js +231 -0
  187. package/dist/queries.d.ts +16 -0
  188. package/dist/queries.d.ts.map +1 -0
  189. package/dist/queries.js +330 -0
  190. package/dist/query.d.ts +15 -211
  191. package/dist/query.d.ts.map +1 -1
  192. package/dist/query.js +3 -505
  193. package/dist/questions.d.ts +30 -0
  194. package/dist/questions.d.ts.map +1 -0
  195. package/dist/questions.js +117 -0
  196. package/dist/queue.d.ts +40 -0
  197. package/dist/queue.d.ts.map +1 -0
  198. package/dist/queue.js +482 -0
  199. package/dist/reach.d.ts +25 -14
  200. package/dist/reach.d.ts.map +1 -1
  201. package/dist/reach.js +351 -138
  202. package/dist/react.d.ts +26 -3
  203. package/dist/react.d.ts.map +1 -1
  204. package/dist/react.js +33 -1
  205. package/dist/read.d.ts +7 -0
  206. package/dist/read.d.ts.map +1 -0
  207. package/dist/read.js +17 -0
  208. package/dist/readiness.d.ts +23 -7
  209. package/dist/readiness.d.ts.map +1 -1
  210. package/dist/readiness.js +18 -10
  211. package/dist/reads.d.ts +39 -0
  212. package/dist/reads.d.ts.map +1 -0
  213. package/dist/reads.js +189 -0
  214. package/dist/recovery.d.ts +4 -1
  215. package/dist/recovery.d.ts.map +1 -1
  216. package/dist/recovery.js +40 -9
  217. package/dist/refresh.d.ts +1 -1
  218. package/dist/refresh.d.ts.map +1 -1
  219. package/dist/refresh.js +13 -6
  220. package/dist/refusal.d.ts +5 -0
  221. package/dist/refusal.d.ts.map +1 -0
  222. package/dist/refusal.js +55 -0
  223. package/dist/replica.d.ts +12 -22
  224. package/dist/replica.d.ts.map +1 -1
  225. package/dist/replica.js +69 -35
  226. package/dist/retried.d.ts +30 -0
  227. package/dist/retried.d.ts.map +1 -0
  228. package/dist/retried.js +72 -0
  229. package/dist/room-draft.d.ts +35 -11
  230. package/dist/room-draft.d.ts.map +1 -1
  231. package/dist/room-draft.js +339 -116
  232. package/dist/room-drafts.d.ts +24 -0
  233. package/dist/room-drafts.d.ts.map +1 -0
  234. package/dist/room-drafts.js +222 -0
  235. package/dist/room-list.d.ts +10 -0
  236. package/dist/room-list.d.ts.map +1 -0
  237. package/dist/room-list.js +47 -0
  238. package/dist/rows.d.ts +24 -23
  239. package/dist/rows.d.ts.map +1 -1
  240. package/dist/rows.js +64 -187
  241. package/dist/run-tables.d.ts +25 -0
  242. package/dist/run-tables.d.ts.map +1 -0
  243. package/dist/run-tables.js +98 -0
  244. package/dist/running.d.ts +5 -0
  245. package/dist/running.d.ts.map +1 -0
  246. package/dist/running.js +10 -0
  247. package/dist/runs.d.ts +27 -0
  248. package/dist/runs.d.ts.map +1 -0
  249. package/dist/runs.js +92 -0
  250. package/dist/runtime.d.ts +37 -0
  251. package/dist/runtime.d.ts.map +1 -0
  252. package/dist/runtime.js +1 -0
  253. package/dist/save.d.ts +43 -86
  254. package/dist/save.d.ts.map +1 -1
  255. package/dist/save.js +343 -235
  256. package/dist/saves.d.ts +34 -0
  257. package/dist/saves.d.ts.map +1 -0
  258. package/dist/saves.js +464 -0
  259. package/dist/search.d.ts +12 -63
  260. package/dist/search.d.ts.map +1 -1
  261. package/dist/search.js +9 -52
  262. package/dist/seed.d.ts +11 -2
  263. package/dist/seed.d.ts.map +1 -1
  264. package/dist/seed.js +71 -1
  265. package/dist/selector.d.ts +3 -0
  266. package/dist/selector.d.ts.map +1 -0
  267. package/dist/selector.js +22 -0
  268. package/dist/shared.d.ts +1 -6
  269. package/dist/shared.d.ts.map +1 -1
  270. package/dist/shared.js +13 -95
  271. package/dist/shown.d.ts +35 -0
  272. package/dist/shown.d.ts.map +1 -0
  273. package/dist/shown.js +327 -0
  274. package/dist/socket/admission.d.ts +7 -0
  275. package/dist/socket/admission.d.ts.map +1 -0
  276. package/dist/socket/admission.js +39 -0
  277. package/dist/socket/answers.d.ts +6 -0
  278. package/dist/socket/answers.d.ts.map +1 -0
  279. package/dist/socket/answers.js +20 -0
  280. package/dist/socket/lanes.d.ts +28 -0
  281. package/dist/socket/lanes.d.ts.map +1 -0
  282. package/dist/socket/lanes.js +103 -0
  283. package/dist/socket/reads.d.ts +10 -0
  284. package/dist/socket/reads.d.ts.map +1 -0
  285. package/dist/socket/reads.js +31 -0
  286. package/dist/socket/session.d.ts +41 -0
  287. package/dist/socket/session.d.ts.map +1 -0
  288. package/dist/socket/session.js +26 -0
  289. package/dist/socket.d.ts +70 -0
  290. package/dist/socket.d.ts.map +1 -0
  291. package/dist/socket.js +172 -0
  292. package/dist/source.d.ts +96 -0
  293. package/dist/source.d.ts.map +1 -0
  294. package/dist/source.js +149 -0
  295. package/dist/status.d.ts +0 -2
  296. package/dist/status.d.ts.map +1 -1
  297. package/dist/status.js +0 -6
  298. package/dist/store/installs.d.ts +30 -0
  299. package/dist/store/installs.d.ts.map +1 -0
  300. package/dist/store/installs.js +40 -0
  301. package/dist/store/readiness.d.ts +11 -0
  302. package/dist/store/readiness.d.ts.map +1 -0
  303. package/dist/store/readiness.js +68 -0
  304. package/dist/store/records.d.ts +234 -0
  305. package/dist/store/records.d.ts.map +1 -0
  306. package/dist/store/records.js +474 -0
  307. package/dist/store/room-held.d.ts +8 -0
  308. package/dist/store/room-held.d.ts.map +1 -0
  309. package/dist/store/room-held.js +17 -0
  310. package/dist/store/rooms-shown.d.ts +8 -0
  311. package/dist/store/rooms-shown.d.ts.map +1 -0
  312. package/dist/store/rooms-shown.js +13 -0
  313. package/dist/store/space.d.ts +11 -0
  314. package/dist/store/space.d.ts.map +1 -0
  315. package/dist/store/space.js +41 -0
  316. package/dist/store/watchers.d.ts +17 -0
  317. package/dist/store/watchers.d.ts.map +1 -0
  318. package/dist/store/watchers.js +56 -0
  319. package/dist/store/writes.d.ts +24 -0
  320. package/dist/store/writes.d.ts.map +1 -0
  321. package/dist/store/writes.js +125 -0
  322. package/dist/store.d.ts +116 -144
  323. package/dist/store.d.ts.map +1 -1
  324. package/dist/store.js +174 -365
  325. package/dist/stored.d.ts +89 -38
  326. package/dist/stored.d.ts.map +1 -1
  327. package/dist/stored.js +459 -212
  328. package/dist/surface.d.ts +100 -0
  329. package/dist/surface.d.ts.map +1 -0
  330. package/dist/surface.js +258 -0
  331. package/dist/sync.d.ts +21 -0
  332. package/dist/sync.d.ts.map +1 -0
  333. package/dist/sync.js +557 -0
  334. package/dist/tables.d.ts +140 -65
  335. package/dist/tables.d.ts.map +1 -1
  336. package/dist/tables.js +222 -280
  337. package/dist/tallies.d.ts +18 -0
  338. package/dist/tallies.d.ts.map +1 -0
  339. package/dist/tallies.js +128 -0
  340. package/dist/validation.d.ts +2 -0
  341. package/dist/validation.d.ts.map +1 -1
  342. package/dist/validation.js +56 -13
  343. package/dist/windows.d.ts +1 -12
  344. package/dist/windows.d.ts.map +1 -1
  345. package/dist/windows.js +2 -75
  346. package/dist/wire.d.ts +87 -12
  347. package/dist/wire.d.ts.map +1 -1
  348. package/dist/wire.js +37 -14
  349. package/package.json +4 -3
  350. package/dist/attempt.d.ts +0 -16
  351. package/dist/attempt.d.ts.map +0 -1
  352. package/dist/attempt.js +0 -70
  353. package/dist/binary.d.ts.map +0 -1
  354. package/dist/binary.js +0 -8
  355. package/dist/fence.d.ts +0 -6
  356. package/dist/fence.d.ts.map +0 -1
  357. package/dist/fence.js +0 -10
  358. package/dist/fulltext.d.ts +0 -27
  359. package/dist/fulltext.d.ts.map +0 -1
  360. package/dist/fulltext.js +0 -370
  361. package/dist/log.d.ts +0 -5
  362. package/dist/log.d.ts.map +0 -1
  363. package/dist/log.js +0 -14
  364. package/dist/words.d.ts +0 -19
  365. package/dist/words.d.ts.map +0 -1
  366. package/dist/words.js +0 -156
package/dist/save.js CHANGED
@@ -1,14 +1,14 @@
1
- import { definitionOf } from './model.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
- import { changeBatchIdentity } from './identity.js';
6
- import { overlaying } from './overlay.js';
7
- import { ancestors, covered, normalize } from './paths.js';
8
- import { sorting } from './shared.js';
1
+ import { apply, ARRAY_INDEX, ATTACHMENT, create, links, parse, pointerSegments, references, relative, relativePath, relativizable, relativize, retarget, roomReferences, serialize, target, writeRoomPath, } from '@agentshouse/mdmodel';
2
+ import { immutable, isPart, leaving, queuedFile, departure, } from './change.js';
3
+ import { collectionOf, definitionsAt, documentsOf, governing, typeAt, UNTYPED, } from './collection.js';
4
+ import { changeBatchIdentity, roomDraftRevision } from './identity.js';
5
+ import { composed, overlaying } from './overlay.js';
6
+ import { ancestors } from './paths.js';
7
+ import { entriesIn, sorting } from './shared.js';
8
+ import { applyParts, changedParts, changesOf, moveTarget, partsBetween, withdrawnDrafts, } from './room-draft.js';
9
9
  import { paced, ROW_WEIGHT, stepped } from './turn.js';
10
10
  import { validatedCollection } from './validation.js';
11
- function refuse(code, file, location) {
11
+ function refuse(code, file, location, line) {
12
12
  return {
13
13
  kind: 'refused',
14
14
  refusal: {
@@ -16,13 +16,14 @@ function refuse(code, file, location) {
16
16
  retryable: false,
17
17
  ...(file === undefined ? {} : { file }),
18
18
  ...(location === undefined ? {} : { location }),
19
+ ...(line === undefined ? {} : { line }),
19
20
  },
20
21
  };
21
22
  }
23
+ function refusedBy(refusal, file) {
24
+ return refuse(refusal.rule, file, refusal.location, refusal.line);
25
+ }
22
26
  function repointed(document, definition, from, to, moves, settled) {
23
- const parsed = parse(document.content, from);
24
- if (!parsed.ok)
25
- return [];
26
27
  const changes = [];
27
28
  const names = new Set();
28
29
  for (const { pointer, value } of definition === undefined
@@ -45,43 +46,13 @@ function repointed(document, definition, from, to, moves, settled) {
45
46
  }
46
47
  if (changes.length === 0)
47
48
  return [];
48
- const applied = apply(parsed.document, changes);
49
+ const applied = apply(document, changes, definition);
49
50
  if (applied.outcome !== 'applied')
50
51
  return [];
51
52
  const next = applied.document;
52
53
  return [...names].map((name) => ({ kind: 'field', path: from, name, value: next.fields[name] }));
53
54
  }
54
- function edited(document, changes) {
55
- const parts = new Map();
56
- for (const change of changes) {
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}`, {
61
- kind: 'section',
62
- name: change.name,
63
- value: change.value,
64
- prior: document.sections[change.name] ?? '',
65
- });
66
- }
67
- if (parts.size === 0)
68
- return document;
69
- const applied = apply(document, [...parts.values()]);
70
- return applied.outcome === 'applied' ? applied.document : document;
71
- }
72
- function bodies(path, before, after) {
73
- const changes = [];
74
- if (after.preamble !== before.preamble) {
75
- changes.push({ kind: 'preamble', path, value: after.preamble });
76
- }
77
- for (const [name, value] of Object.entries(after.sections)) {
78
- if (before.sections[name] === value)
79
- continue;
80
- changes.push({ kind: 'section', path, name, value });
81
- }
82
- return changes;
83
- }
84
- function relinks(path, moved, before, moves) {
55
+ function relinks(path, moved, before, moves, definition) {
85
56
  const changes = [];
86
57
  for (const link of links(before, path)) {
87
58
  const destination = moves.get(link.target) ?? link.target;
@@ -94,28 +65,30 @@ function relinks(path, moved, before, moves) {
94
65
  }
95
66
  if (changes.length === 0)
96
67
  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
- };
108
- }
68
+ const applied = apply(before, changes, definition);
69
+ if (applied.outcome === 'refuse')
70
+ return refusedBy(applied.refusal, path);
109
71
  const after = applied.document;
110
72
  if (after === before)
111
73
  return { kind: 'changes', changes: [] };
112
74
  if (before.type === undefined) {
113
75
  return { kind: 'changes', changes: [{ kind: 'replace', path, content: serialize(after) }] };
114
76
  }
115
- return { kind: 'changes', changes: bodies(path, before, after) };
77
+ return { kind: 'changes', changes: partsBetween(path, before, after) };
116
78
  }
117
79
  function partKey(change) {
118
- return change.kind === 'section' ? `section ${change.name}` : change.kind;
80
+ return change.kind === 'section' || change.kind === 'field'
81
+ ? `${change.kind} ${change.name}`
82
+ : change.kind;
83
+ }
84
+ const NAMES_A_SKIP_TESTS = 64;
85
+ const ENCODED = /[%&\\]/u;
86
+ function movedNames(moves) {
87
+ const names = new Set([...moves.keys()].map((path) => path.slice(path.lastIndexOf('/') + 1)));
88
+ return names.size > NAMES_A_SKIP_TESTS ? null : [...names];
89
+ }
90
+ function cannotLink(text, names) {
91
+ return names !== null && !ENCODED.test(text) && !names.some((name) => text.includes(name));
119
92
  }
120
93
  function* rewrites(changes, visible) {
121
94
  const moves = new Map();
@@ -142,17 +115,21 @@ function* rewrites(changes, visible) {
142
115
  }
143
116
  if (moves.size === 0)
144
117
  return { kind: 'changes', changes };
118
+ const names = movedNames(moves);
145
119
  const merged = [...changes];
146
120
  const added = [];
147
121
  for (const [path, document] of yield* documentsOf(visible.collection)) {
148
122
  if (settled.has(path))
149
123
  continue;
150
124
  const moved = moves.get(path) ?? path;
151
- const definition = definitionOf(governing(visible.collection.composition, path), document.type);
125
+ const definition = definitionsAt(visible.collection.composition, path)(document.type);
152
126
  const repointing = repointed(document, definition, path, moved, moves, settled);
153
127
  added.push(...repointing);
154
128
  const own = parts.get(path) ?? [];
155
- const relinked = relinks(path, moved, edited(document, own.map((index) => merged[index])), moves);
129
+ const edited = applyParts(document, own.map((index) => merged[index]), definition);
130
+ const relinked = moved === path && own.length === 0 && cannotLink(document.content, names)
131
+ ? { kind: 'changes', changes: [] }
132
+ : relinks(path, moved, edited.outcome === 'applied' ? edited.document : document, moves, definition);
156
133
  if (relinked.kind === 'refused')
157
134
  return relinked;
158
135
  const keyed = new Map();
@@ -165,7 +142,7 @@ function* rewrites(changes, visible) {
165
142
  if (change.kind === 'replace') {
166
143
  for (const index of own) {
167
144
  const kind = merged[index]?.kind;
168
- if (kind === 'section' || kind === 'preamble')
145
+ if (kind === 'field' || kind === 'section' || kind === 'preamble')
169
146
  merged[index] = null;
170
147
  }
171
148
  added.push(change);
@@ -221,35 +198,33 @@ function crossed(document, definition, path, to) {
221
198
  }
222
199
  return changes;
223
200
  }
224
- function rewritten(path, document, changes) {
201
+ function rewritten(path, document, changes, definition) {
225
202
  if (changes.length === 0)
226
203
  return [];
227
- const applied = apply(document, changes);
204
+ const applied = apply(document, changes, definition);
228
205
  if (applied.outcome !== 'applied')
229
206
  return [];
230
207
  const after = applied.document;
231
208
  if (document.type === undefined)
232
209
  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}`;
210
+ return partsBetween(path, document, after);
241
211
  }
242
- export function* leavingRewritten(batch, visible) {
212
+ function* leavingRewritten(batch, visible) {
243
213
  const between = batch.between;
244
214
  if (between === undefined)
245
215
  return batch;
246
- const moved = new Map(between.moves.map(({ path, to }) => [path, roomPathOf(between.destination.handle, to)]));
216
+ const { handle } = between.destination;
217
+ const moved = new Map(between.moves.map(({ path, to }) => [path, to]));
247
218
  const derived = [];
248
219
  for (const [path, document] of yield* documentsOf(visible.collection)) {
249
220
  if (moved.has(path))
250
221
  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))));
222
+ const definition = definitionsAt(visible.collection.composition, path)(document.type);
223
+ const into = (named) => {
224
+ const to = moved.get(named);
225
+ return to === undefined ? undefined : relativePath({ path }, { handle, path: to });
226
+ };
227
+ derived.push(...rewritten(path, document, crossed(document, definition, path, into), definition));
253
228
  yield ROW_WEIGHT + visible.collection.text[path].length;
254
229
  }
255
230
  if (derived.length === 0)
@@ -291,7 +266,7 @@ function* incoming(collection, removed, candidates) {
291
266
  for (const [path, document] of candidates === null
292
267
  ? yield* documentsOf(collection)
293
268
  : read(candidates)) {
294
- const definition = definitionOf(governing(collection.composition, path), document.type);
269
+ const definition = definitionsAt(collection.composition, path)(document.type);
295
270
  for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
296
271
  if (to.includes(ATTACHMENT))
297
272
  continue;
@@ -321,6 +296,8 @@ function* incoming(collection, removed, candidates) {
321
296
  function affected(change) {
322
297
  return change.kind === 'move' ? [change.path, change.to] : [change.path];
323
298
  }
299
+ const WRITE = ['write'];
300
+ const MOVE_OUT = ['write', 'erase'];
324
301
  function guard(changes, input, removing) {
325
302
  const overridden = new Map();
326
303
  const existing = {
@@ -332,15 +309,9 @@ function guard(changes, input, removing) {
332
309
  const occupied = (path) => input.folders.has(path) ? path : (ancestors(path).find((one) => existing.has(one)) ?? null);
333
310
  for (const [index, change] of changes.entries()) {
334
311
  for (const path of affected(change)) {
335
- const normalized = normalize(path);
336
- if (normalized === null || normalized === '')
337
- return refuse('room-root', path);
338
- if (covered(input.providerOwnedPaths, normalized)) {
339
- return refuse('provider-owned-path', normalized);
340
- }
341
- const operation = change.kind === 'remove' ? removing : 'write';
342
- if (!input.local && !covered(input.operations[operation], normalized))
343
- return refuse(`${operation}-grant`, normalized);
312
+ const refused = input.rule(path, change.kind === 'remove' ? removing : WRITE);
313
+ if (refused !== null)
314
+ return refuse(refused.code, refused.path);
344
315
  }
345
316
  if (change.kind === 'create' || change.kind === 'arrival') {
346
317
  const taken = existing.has(change.path) ? change.path : occupied(change.path);
@@ -401,9 +372,9 @@ function guard(changes, input, removing) {
401
372
  return null;
402
373
  }
403
374
  function unrevised(input, path) {
404
- return (!input.local && input.queued?.has(path) !== true && input.visible.revisions[path] === undefined);
375
+ return !input.local && !input.queued.has(path) && input.visible.revisions[path] === undefined;
405
376
  }
406
- export function queuedDocuments(queue) {
377
+ function queuedDocuments(queue) {
407
378
  const paths = new Set();
408
379
  for (const batch of queue) {
409
380
  for (const change of batch.changes) {
@@ -448,7 +419,7 @@ function* preconditionsOf(changes, visible, bases) {
448
419
  };
449
420
  for (const change of changes) {
450
421
  yield ROW_WEIGHT;
451
- if (change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble') {
422
+ if (isPart(change)) {
452
423
  const based = bases.find((base) => sameTarget(base, change));
453
424
  if (based !== undefined) {
454
425
  add(based);
@@ -537,95 +508,103 @@ function* exceeding(limits, changes, effective, resulting) {
537
508
  return null;
538
509
  }
539
510
  function shellRelative(text, path, handle, definition) {
540
- if (!text.includes(`/rooms/${handle}/`))
511
+ if (!relativizable(text, handle))
541
512
  return null;
542
- const parsed = parse(text, path);
513
+ const parsed = parse(text, path, definition);
543
514
  if (!parsed.ok)
544
515
  return null;
545
516
  const result = relativize(parsed.document, path, handle, definition(parsed.document.type));
546
517
  return result.outcome === 'applied' && result.document.content !== text ? result.document : null;
547
518
  }
548
519
  function relativeField(path, name, value, handle, definition) {
549
- const opened = parse('---\n---\n');
550
- if (definition === undefined ||
551
- !opened.ok ||
552
- !JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
520
+ if (definition === undefined || !relativizable(JSON.stringify(value ?? null), handle))
553
521
  return value;
554
- const held = apply(opened.document, [{ kind: 'field', name, value, prior: undefined }]);
522
+ const held = apply(create(definition.name, definition), [{ kind: 'field', name, value, prior: undefined }], definition);
555
523
  if (held.outcome !== 'applied')
556
524
  return value;
557
525
  const result = relativize(held.document, path, handle, definition);
558
526
  return result.outcome === 'applied' ? result.document.fields[name] : value;
559
527
  }
560
528
  function relativeChanges(input) {
529
+ const changes = [];
530
+ for (const change of input.changes) {
531
+ const next = relativeChange(change, input);
532
+ if ('refusal' in next)
533
+ return next;
534
+ changes.push(next.change);
535
+ }
536
+ return { kind: 'changes', changes };
537
+ }
538
+ function relativeChange(change, input) {
561
539
  const { handle, composition, visible } = input;
562
- const typed = (path) => (type) => definitionOf(governing(composition, path), type);
563
540
  const body = (path, text) => shellRelative(text, path, handle, () => undefined)?.content ?? text;
564
- return input.changes.map((change) => {
565
- const definition = typed(change.path);
566
- if (change.kind === 'field') {
567
- const type = visible.collection.document(change.path)?.type;
568
- const value = relativeField(change.path, change.name, change.value, handle, definition(type));
569
- return value === change.value ? change : { ...change, value };
570
- }
571
- if (change.kind === 'section' || change.kind === 'preamble') {
572
- const value = body(change.path, change.value);
573
- return value === change.value ? change : { ...change, value };
574
- }
575
- if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
576
- const content = shellRelative(change.content, change.path, handle, definition)?.content ?? change.content;
577
- return content === change.content ? change : { ...change, content };
578
- }
579
- if (change.kind !== 'create')
580
- return change;
581
- const type = typeof change.fields.type === 'string' ? change.fields.type : undefined;
582
- const fields = Object.fromEntries(Object.entries(change.fields).map(([name, value]) => [
583
- name,
584
- relativeField(change.path, name, value, handle, definition(type)),
585
- ]));
586
- const sections = Object.fromEntries(Object.entries(change.sections).map(([name, value]) => [name, body(change.path, value)]));
587
- return { ...change, fields, sections };
588
- });
541
+ const definition = definitionsAt(composition, change.path);
542
+ if (change.kind === 'field') {
543
+ const type = visible.collection.document(change.path)?.type;
544
+ const value = relativeField(change.path, change.name, change.value, handle, definition(type));
545
+ return { change: value === change.value ? change : { ...change, value } };
546
+ }
547
+ if (change.kind === 'section' || change.kind === 'preamble') {
548
+ const value = body(change.path, change.value);
549
+ return { change: value === change.value ? change : { ...change, value } };
550
+ }
551
+ if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
552
+ const content = shellRelative(change.content, change.path, handle, definition)?.content ?? change.content;
553
+ return { change: content === change.content ? change : { ...change, content } };
554
+ }
555
+ if (change.kind !== 'create')
556
+ return { change };
557
+ const type = typeof change.fields.type === 'string' ? change.fields.type : undefined;
558
+ const fields = Object.fromEntries(Object.entries(change.fields).map(([name, value]) => [
559
+ name,
560
+ relativeField(change.path, name, value, handle, definition(type)),
561
+ ]));
562
+ const sections = Object.fromEntries(Object.entries(change.sections).map(([name, value]) => [name, body(change.path, value)]));
563
+ const written = composed(change.path, fields, sections, composition);
564
+ if (written.outcome === 'refuse')
565
+ return refusedBy(written.refusal, change.path);
566
+ if (written.outcome === 'stale')
567
+ throw new Error(`the creation of ${change.path} is stale`);
568
+ if (Object.keys(sections).length === 0)
569
+ return { change: { ...change, fields, sections } };
570
+ return {
571
+ change: { kind: 'create', path: change.path, content: serialize(written.document) },
572
+ };
589
573
  }
590
574
  function unqueueable(file) {
591
- return { kind: 'refused', refusal: { code: 'unknown-revision', retryable: false, file } };
592
- }
593
- function isPart(change) {
594
- return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
575
+ return refuse('unknown-revision', file);
595
576
  }
596
577
  function touched(change) {
597
578
  return change.kind === 'move' ? [change.path, change.to] : [change.path];
598
579
  }
599
- function partsApplied(content, path, changes) {
600
- const parsed = parse(content, path);
601
- if (!parsed.ok)
602
- return null;
603
- const document = parsed.document;
604
- const parts = new Map();
580
+ function partsApplied(content, path, changes, composition) {
581
+ const applied = partsResult(content, path, changes, composition);
582
+ return applied?.outcome === 'applied' ? serialize(applied.document) : null;
583
+ }
584
+ function unapplied(changes, visible, composition) {
585
+ const parted = new Map();
605
586
  for (const change of changes) {
606
- if (change.kind === 'field')
607
- parts.set(`field ${change.name}`, {
608
- kind: 'field',
609
- name: change.name,
610
- value: change.value,
611
- prior: document.fields[change.name],
612
- });
613
- else if (change.kind === 'section')
614
- parts.set(`section ${change.name}`, {
615
- kind: 'section',
616
- name: change.name,
617
- value: change.value,
618
- prior: document.sections[change.name] ?? '',
619
- });
620
- else
621
- parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
587
+ if (isPart(change))
588
+ parted.set(change.path, [...(parted.get(change.path) ?? []), change]);
589
+ }
590
+ for (const [path, parts] of parted) {
591
+ const content = visible.documents[path];
592
+ if (content === undefined)
593
+ continue;
594
+ const applied = partsResult(content, path, parts, composition);
595
+ if (applied?.outcome === 'refuse')
596
+ return refusedBy(applied.refusal, path);
622
597
  }
623
- const applied = apply(document, [...parts.values()]);
624
- return applied.outcome === 'applied' ? serialize(applied.document) : null;
598
+ return null;
599
+ }
600
+ function partsResult(content, path, changes, composition) {
601
+ const definitions = definitionsAt(composition, path);
602
+ const parsed = parse(content, path, definitions);
603
+ return parsed.ok ? applyParts(parsed.document, changes, definitions(parsed.document.type)) : null;
625
604
  }
626
- function settledParts(changes, deferred) {
605
+ function settledParts(changes, deferred, composition) {
627
606
  for (const [held, parts] of deferred) {
628
- const content = partsApplied(held.content, held.path, parts);
607
+ const content = partsApplied(held.content, held.path, parts, composition);
629
608
  if (content === null)
630
609
  return held.path;
631
610
  changes[changes.indexOf(held)] = { ...held, content };
@@ -700,7 +679,9 @@ function* relinked(batch, move, before, after, appending) {
700
679
  for (const change of derived.changes) {
701
680
  if (change === move)
702
681
  continue;
703
- const unsettled = isPart(change) ? null : settledParts(changes, deferred);
682
+ const unsettled = isPart(change)
683
+ ? null
684
+ : settledParts(changes, deferred, after.collection.composition);
704
685
  if (unsettled !== null)
705
686
  return unqueueable(unsettled);
706
687
  const outcome = folded(changes, change, appending, deferred);
@@ -711,7 +692,7 @@ function* relinked(batch, move, before, after, appending) {
711
692
  if (outcome !== 'skipped')
712
693
  changed = true;
713
694
  }
714
- const unsettled = settledParts(changes, deferred);
695
+ const unsettled = settledParts(changes, deferred, after.collection.composition);
715
696
  if (unsettled !== null)
716
697
  return unqueueable(unsettled);
717
698
  if (!changed)
@@ -772,11 +753,10 @@ function withdrawnFile(batch, change, at) {
772
753
  ];
773
754
  return immutable({ ...batch, id: changeBatchIdentity(), changes, preconditions });
774
755
  }
775
- export function* requeuing(queue, changes, visible, changeable, state) {
756
+ function* requeuing(queue, changes, visible, changeable, state) {
776
757
  const next = [...queue];
777
758
  const origins = new Map();
778
759
  const withdrawn = new Map();
779
- const owners = new Set();
780
760
  const queued = new Set();
781
761
  const kept = [];
782
762
  const originOf = (batch) => origins.get(batch) ?? batch.id;
@@ -822,7 +802,6 @@ export function* requeuing(queue, changes, visible, changeable, state) {
822
802
  if (replaced !== null)
823
803
  origins.set(replaced, origin);
824
804
  }
825
- owners.add(originOf(owner));
826
805
  next.splice(index, following.length, ...rebuilt.filter((batch) => batch !== null));
827
806
  queued.add(change.path);
828
807
  }
@@ -832,62 +811,16 @@ export function* requeuing(queue, changes, visible, changeable, state) {
832
811
  changes: kept,
833
812
  queued,
834
813
  withdrawn,
835
- owners,
836
814
  };
837
815
  }
838
- function covering(precondition) {
839
- return precondition.kind === 'field' ||
840
- precondition.kind === 'section' ||
841
- precondition.kind === 'preamble'
842
- ? identity(precondition)
843
- : `path ${precondition.path}`;
844
- }
845
- export function joined(queue, owner, extra) {
846
- const later = new Set(queue.slice(queue.indexOf(owner) + 1).flatMap((batch) => batch.changes.flatMap(touched)));
847
- const clash = extra.changes.flatMap(touched).find((path) => later.has(path));
848
- if (clash !== undefined)
849
- return unqueueable(clash);
850
- const changes = [...owner.changes];
851
- const deferred = new Map();
852
- for (const change of extra.changes) {
853
- const unsettled = isPart(change) ? null : settledParts(changes, deferred);
854
- if (unsettled !== null)
855
- return unqueueable(unsettled);
856
- if (folded(changes, change, true, deferred) === 'refused')
857
- return unqueueable(change.path);
858
- }
859
- const unsettled = settledParts(changes, deferred);
860
- if (unsettled !== null)
861
- return unqueueable(unsettled);
862
- const held = new Set(owner.preconditions.map(covering));
863
- const written = new Set(owner.changes.flatMap((change) => change.kind === 'create' || change.kind === 'replace' ? [change.path] : []));
864
- const preconditions = [
865
- ...owner.preconditions,
866
- ...extra.preconditions.filter((precondition) => {
867
- const key = covering(precondition);
868
- return (!held.has(key) && (key === `path ${precondition.path}` || !written.has(precondition.path)));
869
- }),
870
- ];
871
- const batch = immutable({
872
- ...owner,
873
- ...(extra.validation === undefined ? {} : { validation: extra.validation }),
874
- id: changeBatchIdentity(),
875
- changes,
876
- preconditions,
877
- });
878
- return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
879
- }
880
- export function* limiting(queue, batch, limits, state) {
816
+ function* limiting(queue, batch, limits, state) {
881
817
  const at = queue.indexOf(batch);
882
818
  const rewritten = yield* providerRewritten(batch, yield* state(queue.slice(0, at)));
883
819
  const limited = yield* exceeding(limits, batch.changes, rewritten.changes, yield* state(queue.slice(0, at + 1)));
884
- return limited?.kind === 'refused' ? limited.refusal : null;
820
+ return limited?.refusal ?? null;
885
821
  }
886
- function typedText(content) {
887
- if (content === undefined)
888
- return false;
889
- const parsed = parse(content);
890
- return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
822
+ function typedContent(composition, path, content) {
823
+ return (content !== undefined && (typeAt(composition, path, parse(content)) ?? UNTYPED) !== UNTYPED);
891
824
  }
892
825
  function touchedLibrary(resulting, touched, composition) {
893
826
  const entries = touched
@@ -898,7 +831,7 @@ function touchedLibrary(resulting, touched, composition) {
898
831
  })
899
832
  .sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
900
833
  const contents = new Map(entries.map(({ path, content }) => [path, content]));
901
- const binaries = new Set(Object.keys(resulting.binaries).filter((path) => resulting.binaries[path] !== undefined));
834
+ const binaries = new Set(Array.from(entriesIn(resulting.binaries), ([path]) => path));
902
835
  return validatedCollection(() => Promise.resolve({ entries, next: null }), async (paths) => {
903
836
  const known = new Map();
904
837
  const pace = paced();
@@ -907,9 +840,9 @@ function touchedLibrary(resulting, touched, composition) {
907
840
  if (binaries.has(path))
908
841
  known.set(path, { type: undefined, binary: true });
909
842
  else if (content !== undefined && content !== null) {
910
- const read = parse(content, path);
911
- if (read.ok)
912
- known.set(path, { type: read.document.type ?? 'any', binary: false });
843
+ const type = typeAt(composition, path, parse(content, path, definitionsAt(composition, path)));
844
+ if (type !== undefined)
845
+ known.set(path, { type, binary: false });
913
846
  await pace(content.length + ROW_WEIGHT);
914
847
  }
915
848
  }
@@ -927,7 +860,7 @@ async function judging(visible, resulting, composition, validate, removed, touch
927
860
  for (const path of touched) {
928
861
  const before = visible.documents[path];
929
862
  const after = resulting.documents[path];
930
- if (typedText(before) || typedText(after))
863
+ if (typedContent(composition, path, before) || typedContent(composition, path, after))
931
864
  typedPaths.push(path);
932
865
  await pace((before?.length ?? 0) + (after?.length ?? 0) + ROW_WEIGHT);
933
866
  }
@@ -945,6 +878,7 @@ async function judging(visible, resulting, composition, validate, removed, touch
945
878
  retryable: false,
946
879
  file: verdict.file,
947
880
  ...(verdict.location === undefined ? {} : { location: verdict.location }),
881
+ ...(verdict.line === undefined ? {} : { line: verdict.line }),
948
882
  ...(verdict.referrers === undefined ? {} : { referrers: verdict.referrers }),
949
883
  },
950
884
  };
@@ -957,11 +891,9 @@ async function judging(visible, resulting, composition, validate, removed, touch
957
891
  };
958
892
  }
959
893
  function placed(reference) {
960
- const key = roomPathOf(reference.handle, reference.path);
894
+ const key = writeRoomPath(reference);
961
895
  if (reference.kind === 'link')
962
896
  return `link ${key}`;
963
- if (reference.kind === 'item')
964
- return `section ${reference.section} ${key}`;
965
897
  const pointer = reference.pointer
966
898
  .split('/')
967
899
  .map((segment) => (ARRAY_INDEX.test(segment) ? '-' : segment))
@@ -971,10 +903,11 @@ function placed(reference) {
971
903
  function roomReferencesIn(path, content, composition) {
972
904
  if (content === undefined)
973
905
  return [];
974
- const parsed = parse(content, path);
906
+ const definitions = definitionsAt(composition, path);
907
+ const parsed = parse(content, path, definitions);
975
908
  if (!parsed.ok)
976
909
  return [];
977
- return roomReferences(path, content, definitionOf(governing(composition, path), parsed.document.type));
910
+ return roomReferences(parsed.document, { path }, definitions(parsed.document.type));
978
911
  }
979
912
  function* admittedRooms(changes, visible, resulting, composition) {
980
913
  const from = new Map();
@@ -992,7 +925,7 @@ function* admittedRooms(changes, visible, resulting, composition) {
992
925
  continue;
993
926
  if (held.has(placed(reference)))
994
927
  continue;
995
- return refuse('unresolved-reference', path, reference.kind === 'field' ? reference.pointer : reference.section);
928
+ return refuse('unresolved-reference', path, reference.pointer);
996
929
  }
997
930
  yield ROW_WEIGHT + now.length;
998
931
  }
@@ -1010,7 +943,10 @@ function validationOf(validated) {
1010
943
  };
1011
944
  }
1012
945
  async function batching(input) {
1013
- const changes = relativeChanges(input);
946
+ const relative = relativeChanges(input);
947
+ if (relative.kind === 'refused')
948
+ return relative;
949
+ const { changes } = relative;
1014
950
  const structural = changes.find((change) => change.kind === 'move' || change.kind === 'remove');
1015
951
  if (structural !== undefined && input.unmodeled) {
1016
952
  return refuse('model-unavailable', structural.path);
@@ -1020,12 +956,12 @@ async function batching(input) {
1020
956
  const rewritten = await stepped(rewrites(changes, input.visible));
1021
957
  if (rewritten.kind === 'refused')
1022
958
  return rewritten;
1023
- const refusal = guard(changes, input, 'delete');
959
+ const refusal = guard(changes, input, WRITE) ?? unapplied(changes, input.visible, input.composition);
1024
960
  if (refusal !== null)
1025
961
  return refusal;
1026
962
  const preconditions = await stepped(preconditionsOf(changes, input.visible, input.bases));
1027
963
  const predicted = await stepped(anticipated(changes, preconditions, rewritten.changes, input.visible));
1028
- const resulting = await stepped(overlaying(input.visible, [{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted }]));
964
+ const resulting = await stepped(overlaying(input.visible, [{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted }], input.composition));
1029
965
  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))]);
1030
966
  if (judged.kind === 'refused')
1031
967
  return judged;
@@ -1047,7 +983,7 @@ async function batching(input) {
1047
983
  }),
1048
984
  };
1049
985
  }
1050
- export const MOVE_DOCUMENTS = 'move-documents';
986
+ const MOVE_DOCUMENTS = 'move-documents';
1051
987
  function heldAt(visible, path) {
1052
988
  return visible.documents[path] !== undefined || visible.binaries[path] !== undefined;
1053
989
  }
@@ -1056,22 +992,25 @@ function arrivingText(path, to, source, moved) {
1056
992
  const document = source.visible.collection.document(path);
1057
993
  if (document === undefined)
1058
994
  return content;
1059
- const definition = definitionOf(governing(source.composition, path), document.type);
995
+ const definition = definitionsAt(source.composition, path)(document.type);
1060
996
  const changes = crossed(document, definition, path, (named, href) => {
1061
997
  const at = moved.get(named);
1062
998
  if (at !== undefined)
1063
999
  return target(to, href) === at ? undefined : relative(to, at);
1064
- return heldAt(source.visible, named) ? roomPathOf(source.handle, named) : undefined;
1000
+ return heldAt(source.visible, named)
1001
+ ? relativePath({ path: to }, { handle: source.handle, path: named })
1002
+ : undefined;
1065
1003
  });
1066
1004
  if (changes.length === 0)
1067
1005
  return content;
1068
- const applied = apply(document, changes);
1006
+ const applied = apply(document, changes, definition);
1069
1007
  return applied.outcome === 'applied' ? serialize(applied.document) : content;
1070
1008
  }
1071
1009
  function arrivalOf({ path, to }, source, moved) {
1072
- const { documents, revisions, fileIds, changes, binaries } = source.visible;
1010
+ const { documents, revisions, fileIds, created, changes, binaries } = source.visible;
1073
1011
  const kept = {
1074
1012
  ...(fileIds[path] === undefined ? {} : { fileId: fileIds[path] }),
1013
+ ...(created[path] === undefined ? {} : { created: created[path] }),
1075
1014
  ...(changes[path] === undefined ? {} : { change: changes[path] }),
1076
1015
  };
1077
1016
  const heads = {
@@ -1098,10 +1037,8 @@ function arrivalOf({ path, to }, source, moved) {
1098
1037
  }
1099
1038
  async function moving(input) {
1100
1039
  const { source, destination, moves } = input;
1101
- if (moves.length === 0)
1102
- return refuse('no-change');
1103
1040
  const removals = moves.map(({ path }) => ({ kind: 'remove', path }));
1104
- const leaves = guard(removals, { ...source, local: input.local, bases: [] }, 'write');
1041
+ const leaves = guard(removals, { ...source, local: input.local, bases: [] }, MOVE_OUT);
1105
1042
  if (leaves !== null)
1106
1043
  return leaves;
1107
1044
  const uploading = moves.find(({ path }) => source.visible.binaries[path]?.state === 'pending');
@@ -1109,7 +1046,7 @@ async function moving(input) {
1109
1046
  return refuse('unknown-revision', uploading.path);
1110
1047
  const moved = new Map(moves.map(({ path, to }) => [path, to]));
1111
1048
  const arrivals = moves.map((move) => arrivalOf(move, source, moved));
1112
- const arrives = guard(arrivals, { ...destination, local: input.local, bases: [] }, 'write');
1049
+ const arrives = guard(arrivals, { ...destination, local: input.local, bases: [] }, WRITE);
1113
1050
  if (arrives !== null)
1114
1051
  return arrives;
1115
1052
  const bound = input.providerLimits.find((limit) => limit.name === MOVE_DOCUMENTS)?.value;
@@ -1138,7 +1075,7 @@ async function moving(input) {
1138
1075
  between,
1139
1076
  };
1140
1077
  const effective = await stepped(leavingRewritten(leavingBatch, source.visible));
1141
- const left = await stepped(overlaying(source.visible, [effective]));
1078
+ const left = await stepped(overlaying(source.visible, [effective], source.composition));
1142
1079
  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))]);
1143
1080
  if (sourceJudged.kind === 'refused')
1144
1081
  return sourceJudged;
@@ -1150,7 +1087,7 @@ async function moving(input) {
1150
1087
  ...written,
1151
1088
  between,
1152
1089
  };
1153
- const arrived = await stepped(overlaying(destination.visible, [arrivingBatch]));
1090
+ const arrived = await stepped(overlaying(destination.visible, [arrivingBatch], destination.composition));
1154
1091
  const destinationJudged = await judging(destination.visible, arrived, destination.composition, destination.validate, new Set(), moves.map(({ to }) => to));
1155
1092
  if (destinationJudged.kind === 'refused')
1156
1093
  return destinationJudged;
@@ -1160,9 +1097,180 @@ async function moving(input) {
1160
1097
  return limited;
1161
1098
  return { kind: 'moved', leaving: immutable(leavingBatch), arriving: immutable(arrivingBatch) };
1162
1099
  }
1163
- export function buildMove(input) {
1164
- return moving(input);
1100
+ function unreadable(saved, visible, composition) {
1101
+ const path = saved.find(({ path: at, parts }) => (parts.fields !== undefined ||
1102
+ parts.sections !== undefined ||
1103
+ parts.preamble !== undefined) &&
1104
+ typedContent(composition, at, visible.documents[at]) &&
1105
+ governing(composition, at).status === 'unavailable')?.path;
1106
+ return path === undefined ? null : refuse('model-unavailable', path);
1107
+ }
1108
+ function changedUnderneath(saved, visible) {
1109
+ const parts = saved.flatMap(({ path, parts: held }) => changedParts(path, held, visible));
1110
+ return parts.length === 0
1111
+ ? null
1112
+ : { kind: 'refused', refusal: { code: 'changed-underneath', retryable: false, parts } };
1113
+ }
1114
+ function sideOf(room, visible) {
1115
+ const { record } = room;
1116
+ return {
1117
+ room: record.locator,
1118
+ handle: record.declaration.handle,
1119
+ baseRoomVersion: record.roomVersion,
1120
+ composition: room.composition,
1121
+ rule: room.rule,
1122
+ visible,
1123
+ folders: room.folders,
1124
+ queued: queuedDocuments(room.queue),
1125
+ unmodeled: !room.modelled,
1126
+ validate: room.validate,
1127
+ };
1128
+ }
1129
+ function carriedDrafts(ask, from, into) {
1130
+ return new Map(ask.saved.flatMap(({ path, parts }) => {
1131
+ const record = from.drafts.get(path);
1132
+ if (parts.move === undefined || record === undefined || ask.consumed.has(path))
1133
+ return [];
1134
+ const to = moveTarget(parts.move);
1135
+ if (into.drafts.has(to))
1136
+ return [];
1137
+ const locator = into.record.locator;
1138
+ return [[path, { ...record, locator, path: to, revision: roomDraftRevision() }]];
1139
+ }));
1140
+ }
1141
+ async function plannedMove(ask, source, into) {
1142
+ const destination = into.record.locator;
1143
+ const mixed = ask.saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
1144
+ if (mixed !== undefined)
1145
+ return refuse('mixed-room-move', mixed.path);
1146
+ const leavingVisible = await stepped(source.replay(source.queue));
1147
+ const changed = changedUnderneath(ask.saved, leavingVisible);
1148
+ if (changed !== null)
1149
+ return changed;
1150
+ const moves = ask.saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
1151
+ const built = await moving({
1152
+ source: sideOf(source, leavingVisible),
1153
+ destination: sideOf(into, await stepped(into.replay(into.queue))),
1154
+ moves,
1155
+ providerLimits: ask.providerLimits,
1156
+ local: ask.local,
1157
+ ...(ask.writtenBy === undefined ? {} : { writtenBy: ask.writtenBy }),
1158
+ });
1159
+ if (built.kind === 'refused')
1160
+ return built;
1161
+ const { id } = built.leaving;
1162
+ const carried = carriedDrafts(ask, source, into);
1163
+ return {
1164
+ kind: 'planned',
1165
+ identifier: id,
1166
+ rooms: [
1167
+ {
1168
+ locator: source.record.locator,
1169
+ queue: [...source.queue, built.leaving],
1170
+ saved: new Map([[id, ask.saved]]),
1171
+ consumed: [...ask.consumed, ...carried.keys()],
1172
+ carried: [],
1173
+ },
1174
+ {
1175
+ locator: destination,
1176
+ queue: [...into.queue, built.arriving],
1177
+ saved: new Map([
1178
+ [id, [...carried.values()].map(({ path }) => ({ path, parts: {} }))],
1179
+ ]),
1180
+ consumed: [],
1181
+ carried: [...carried.values()],
1182
+ },
1183
+ ],
1184
+ };
1185
+ }
1186
+ async function plannedWithin(ask, room) {
1187
+ const { record } = room;
1188
+ const visible = await stepped(room.replay(room.queue));
1189
+ const changed = unreadable(ask.saved, visible, room.composition) ?? changedUnderneath(ask.saved, visible);
1190
+ if (changed !== null)
1191
+ return changed;
1192
+ const parts = ask.saved.map(({ path, parts: held }) => changesOf(path, held));
1193
+ const changes = parts.flatMap(({ changes: made }) => made);
1194
+ const requeue = await stepped(requeuing(room.queue, changes, visible, (batch) => room.changeable.has(batch.id), room.replay));
1195
+ if (requeue.kind === 'refused')
1196
+ return requeue;
1197
+ const foreign = [...requeue.withdrawn.keys()].some((origin) => {
1198
+ const writer = room.queue.find((batch) => batch.id === origin)?.writtenBy;
1199
+ return writer?.app !== ask.writtenBy?.app || writer?.release !== ask.writtenBy?.release;
1200
+ });
1201
+ if (foreign)
1202
+ return unqueueable([...requeue.queued].at(-1));
1203
+ const input = {
1204
+ local: ask.local,
1205
+ baseRoomVersion: record.roomVersion,
1206
+ composition: room.composition,
1207
+ rule: room.rule,
1208
+ providerLimits: ask.providerLimits,
1209
+ handle: record.declaration.handle,
1210
+ visible,
1211
+ folders: room.folders,
1212
+ changes,
1213
+ bases: parts.flatMap(({ bases }) => bases),
1214
+ queued: queuedDocuments(room.queue),
1215
+ writtenBy: ask.writtenBy,
1216
+ unmodeled: !room.modelled,
1217
+ validate: room.validate,
1218
+ };
1219
+ const rewritten = requeue.withdrawn.size > 0;
1220
+ if (rewritten) {
1221
+ const whole = await batching({
1222
+ ...input,
1223
+ providerLimits: [],
1224
+ queued: new Set([...input.queued, ...requeue.queued]),
1225
+ });
1226
+ if (whole.kind === 'refused')
1227
+ return whole;
1228
+ }
1229
+ const own = !rewritten
1230
+ ? await batching(input)
1231
+ : requeue.changes.length === 0
1232
+ ? null
1233
+ : await batching({
1234
+ ...input,
1235
+ visible: await stepped(room.replay(requeue.queue)),
1236
+ changes: requeue.changes,
1237
+ queued: queuedDocuments(requeue.queue),
1238
+ });
1239
+ if (own?.kind === 'refused')
1240
+ return own;
1241
+ const queue = own === null ? requeue.queue : [...requeue.queue, own.batch];
1242
+ const saved = new Map();
1243
+ for (const [origin, rebuilt] of requeue.withdrawn) {
1244
+ if (rebuilt === null)
1245
+ continue;
1246
+ const limited = await stepped(limiting(queue, rebuilt, ask.providerLimits, room.replay));
1247
+ if (limited !== null)
1248
+ return { kind: 'refused', refusal: limited };
1249
+ saved.set(rebuilt.id, withdrawnDrafts(room.saved.get(origin) ?? [], changes));
1250
+ }
1251
+ if (own !== null) {
1252
+ saved.set(own.batch.id, ask.saved.filter(({ path }) => !requeue.queued.has(path)));
1253
+ }
1254
+ const carried = carriedDrafts(ask, room, room);
1255
+ return {
1256
+ kind: 'planned',
1257
+ identifier: own?.batch.id ??
1258
+ [...requeue.withdrawn].map(([origin, rebuilt]) => rebuilt?.id ?? origin).at(-1),
1259
+ rooms: [
1260
+ {
1261
+ locator: record.locator,
1262
+ queue,
1263
+ saved,
1264
+ consumed: [...ask.consumed, ...carried.keys()],
1265
+ carried: [...carried.values()],
1266
+ },
1267
+ ],
1268
+ };
1269
+ }
1270
+ export function destination(locator, saved) {
1271
+ const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
1272
+ return into?.parts.move?.room ?? null;
1165
1273
  }
1166
- export function buildBatch(input) {
1167
- return batching(input);
1274
+ export function planSave(ask, room, into) {
1275
+ return into === undefined ? plannedWithin(ask, room) : plannedMove(ask, room, into);
1168
1276
  }