@jam-overture/loom 0.1.0 → 0.2.0

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 (290) hide show
  1. package/README.md +3 -1
  2. package/dist/application/application.d.ts +109 -0
  3. package/dist/application/application.js +97 -0
  4. package/dist/application/memory.d.ts +20 -0
  5. package/dist/application/memory.js +79 -0
  6. package/dist/application/store.d.ts +101 -0
  7. package/dist/application/store.js +25 -0
  8. package/dist/audience/audience.d.ts +36 -0
  9. package/dist/audience/audience.js +41 -0
  10. package/dist/cli/templates.js +19 -0
  11. package/dist/data/plan.js +0 -0
  12. package/dist/data/resolution.d.ts +23 -1
  13. package/dist/data/resolve.d.ts +30 -3
  14. package/dist/data/resolve.js +11 -3
  15. package/dist/ids.d.ts +10 -0
  16. package/dist/ids.js +11 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.js +1 -0
  19. package/dist/patch.d.ts +54 -0
  20. package/dist/patch.js +44 -0
  21. package/dist/project/closing.d.ts +51 -0
  22. package/dist/project/closing.js +53 -0
  23. package/dist/project/memory.d.ts +20 -0
  24. package/dist/project/memory.js +97 -0
  25. package/dist/project/project.d.ts +90 -0
  26. package/dist/project/project.js +91 -0
  27. package/dist/project/store.d.ts +135 -0
  28. package/dist/project/store.js +41 -0
  29. package/dist/registered.d.ts +64 -0
  30. package/dist/registered.js +118 -0
  31. package/dist/render/behaviour-adjust.d.ts +11 -19
  32. package/dist/render/behaviour-adjust.js +75 -21
  33. package/dist/render/behaviour-present.d.ts +8 -0
  34. package/dist/render/behaviour-present.js +57 -6
  35. package/dist/render/behaviour.d.ts +98 -17
  36. package/dist/render/behaviour.js +91 -26
  37. package/dist/render/control-name.d.ts +68 -0
  38. package/dist/render/control-name.js +37 -0
  39. package/dist/render/diagnostics.d.ts +72 -0
  40. package/dist/render/diagnostics.js +7 -0
  41. package/dist/render/index.d.ts +2 -0
  42. package/dist/render/index.js +2 -0
  43. package/dist/render/reads.d.ts +11 -0
  44. package/dist/render/reads.js +12 -1
  45. package/dist/render/render.js +102 -7
  46. package/dist/render/slots.d.ts +52 -0
  47. package/dist/render/slots.js +24 -0
  48. package/dist/render/theme.d.ts +54 -1
  49. package/dist/render/theme.js +23 -1
  50. package/dist/render/unshown.d.ts +152 -0
  51. package/dist/render/unshown.js +80 -0
  52. package/dist/runtime/analysis.d.ts +94 -9
  53. package/dist/runtime/analysis.js +54 -9
  54. package/dist/runtime/assessment.d.ts +36 -2
  55. package/dist/runtime/assessment.js +27 -2
  56. package/dist/runtime/checks.d.ts +148 -0
  57. package/dist/runtime/checks.js +112 -0
  58. package/dist/runtime/disposition.d.ts +96 -0
  59. package/dist/runtime/disposition.js +46 -0
  60. package/dist/runtime/gate.js +22 -0
  61. package/dist/runtime/index.d.ts +3 -0
  62. package/dist/runtime/index.js +3 -0
  63. package/dist/runtime/irreversibility.d.ts +64 -0
  64. package/dist/runtime/irreversibility.js +45 -0
  65. package/dist/runtime/pipeline.d.ts +43 -0
  66. package/dist/runtime/pipeline.js +10 -2
  67. package/dist/runtime/policy-change.d.ts +93 -0
  68. package/dist/runtime/policy-change.js +299 -0
  69. package/dist/runtime/policy-log.d.ts +406 -0
  70. package/dist/runtime/policy-log.js +285 -0
  71. package/dist/runtime/reversibility.d.ts +11 -9
  72. package/dist/runtime/stakes.d.ts +129 -1
  73. package/dist/runtime/stakes.js +240 -26
  74. package/dist/runtime/vocabulary.d.ts +164 -7
  75. package/dist/runtime/vocabulary.js +181 -8
  76. package/dist/sdk/audit.d.ts +174 -3
  77. package/dist/sdk/audit.js +176 -17
  78. package/dist/sdk/catalogue.d.ts +9 -1
  79. package/dist/sdk/conformance.d.ts +37 -3
  80. package/dist/sdk/conformance.js +25 -4
  81. package/dist/sdk/definition.d.ts +65 -0
  82. package/dist/sdk/definition.js +22 -0
  83. package/dist/sdk/registry.d.ts +29 -2
  84. package/dist/sdk/registry.js +63 -1
  85. package/dist/signals/action-change.d.ts +507 -0
  86. package/dist/signals/action-change.js +265 -0
  87. package/dist/signals/action.d.ts +331 -0
  88. package/dist/signals/action.js +182 -0
  89. package/dist/signals/applied-window.contract.d.ts +23 -0
  90. package/dist/signals/applied-window.contract.js +160 -0
  91. package/dist/signals/arriving.d.ts +210 -0
  92. package/dist/signals/arriving.js +133 -0
  93. package/dist/signals/audiences.d.ts +528 -0
  94. package/dist/signals/audiences.js +255 -0
  95. package/dist/signals/broadcast.d.ts +8 -2
  96. package/dist/signals/broadcast.js +58 -8
  97. package/dist/signals/change.d.ts +378 -0
  98. package/dist/signals/change.js +327 -0
  99. package/dist/signals/collect.d.ts +41 -0
  100. package/dist/signals/collect.js +88 -7
  101. package/dist/signals/copy-change.d.ts +553 -0
  102. package/dist/signals/copy-change.js +377 -0
  103. package/dist/signals/copy.d.ts +337 -0
  104. package/dist/signals/copy.js +211 -0
  105. package/dist/signals/counters.d.ts +46 -0
  106. package/dist/signals/counters.js +102 -0
  107. package/dist/signals/deliver.d.ts +23 -4
  108. package/dist/signals/deliver.js +34 -6
  109. package/dist/signals/deployment-change.d.ts +371 -0
  110. package/dist/signals/deployment-change.js +268 -0
  111. package/dist/signals/deployment.d.ts +265 -0
  112. package/dist/signals/deployment.js +152 -0
  113. package/dist/signals/door.d.ts +92 -0
  114. package/dist/signals/door.js +53 -0
  115. package/dist/signals/fold.d.ts +10 -0
  116. package/dist/signals/fold.js +5 -1
  117. package/dist/signals/form-action.d.ts +302 -0
  118. package/dist/signals/form-action.js +169 -0
  119. package/dist/signals/form-copy.d.ts +523 -0
  120. package/dist/signals/form-copy.js +346 -0
  121. package/dist/signals/forms.d.ts +247 -0
  122. package/dist/signals/forms.js +147 -0
  123. package/dist/signals/funnel-change.d.ts +548 -0
  124. package/dist/signals/funnel-change.js +361 -0
  125. package/dist/signals/funnel.d.ts +532 -0
  126. package/dist/signals/funnel.js +216 -0
  127. package/dist/signals/health.d.ts +497 -0
  128. package/dist/signals/health.js +361 -0
  129. package/dist/signals/index.d.ts +41 -0
  130. package/dist/signals/index.js +41 -0
  131. package/dist/signals/ingest.d.ts +56 -1
  132. package/dist/signals/ingest.js +91 -1
  133. package/dist/signals/journal.d.ts +11 -0
  134. package/dist/signals/kinds.d.ts +8 -6
  135. package/dist/signals/kinds.js +14 -6
  136. package/dist/signals/library-action.d.ts +430 -0
  137. package/dist/signals/library-action.js +336 -0
  138. package/dist/signals/library-change.d.ts +477 -0
  139. package/dist/signals/library-change.js +335 -0
  140. package/dist/signals/library-copy-change.d.ts +498 -0
  141. package/dist/signals/library-copy-change.js +476 -0
  142. package/dist/signals/library-pace.d.ts +319 -0
  143. package/dist/signals/library-pace.js +305 -0
  144. package/dist/signals/library-reach-change.d.ts +636 -0
  145. package/dist/signals/library-reach-change.js +534 -0
  146. package/dist/signals/library-reach.d.ts +377 -0
  147. package/dist/signals/library-reach.js +227 -0
  148. package/dist/signals/library-word-reach-change.d.ts +567 -0
  149. package/dist/signals/library-word-reach-change.js +568 -0
  150. package/dist/signals/library.d.ts +306 -0
  151. package/dist/signals/library.js +178 -0
  152. package/dist/signals/memory.d.ts +9 -0
  153. package/dist/signals/memory.js +105 -14
  154. package/dist/signals/migrate.d.ts +17 -2
  155. package/dist/signals/migrate.js +122 -5
  156. package/dist/signals/notice.d.ts +334 -0
  157. package/dist/signals/notice.js +228 -0
  158. package/dist/signals/pace-change.d.ts +501 -0
  159. package/dist/signals/pace-change.js +310 -0
  160. package/dist/signals/pace.d.ts +296 -0
  161. package/dist/signals/pace.js +291 -0
  162. package/dist/signals/page-views.d.ts +365 -0
  163. package/dist/signals/page-views.js +218 -0
  164. package/dist/signals/parts.d.ts +344 -0
  165. package/dist/signals/parts.js +273 -0
  166. package/dist/signals/postgres.d.ts +16 -2
  167. package/dist/signals/postgres.js +390 -87
  168. package/dist/signals/progress.d.ts +212 -0
  169. package/dist/signals/progress.js +150 -0
  170. package/dist/signals/ratios.d.ts +108 -0
  171. package/dist/signals/ratios.js +40 -0
  172. package/dist/signals/reach.d.ts +309 -0
  173. package/dist/signals/reach.js +93 -0
  174. package/dist/signals/readable.d.ts +34 -0
  175. package/dist/signals/readable.js +34 -0
  176. package/dist/signals/readership.d.ts +381 -0
  177. package/dist/signals/readership.js +308 -0
  178. package/dist/signals/region-pages.d.ts +173 -0
  179. package/dist/signals/region-pages.js +62 -0
  180. package/dist/signals/region.d.ts +287 -0
  181. package/dist/signals/region.js +198 -0
  182. package/dist/signals/retention.d.ts +141 -0
  183. package/dist/signals/retention.js +58 -0
  184. package/dist/signals/rollup.d.ts +67 -3
  185. package/dist/signals/rollup.js +0 -0
  186. package/dist/signals/schema.d.ts +432 -1
  187. package/dist/signals/schema.js +157 -3
  188. package/dist/signals/signal.d.ts +237 -11
  189. package/dist/signals/signal.js +70 -10
  190. package/dist/signals/silence-census.d.ts +205 -0
  191. package/dist/signals/silence-census.js +87 -0
  192. package/dist/signals/silences.d.ts +848 -0
  193. package/dist/signals/silences.js +753 -0
  194. package/dist/signals/tally.d.ts +140 -11
  195. package/dist/signals/window-figures.d.ts +148 -0
  196. package/dist/signals/window-figures.js +42 -0
  197. package/dist/signals/window.d.ts +866 -0
  198. package/dist/signals/window.js +693 -0
  199. package/dist/signals/words.d.ts +24 -0
  200. package/dist/signals/words.js +24 -0
  201. package/dist/store/index.d.ts +19 -0
  202. package/dist/store/index.js +19 -0
  203. package/dist/store/memory.d.ts +2 -1
  204. package/dist/store/memory.js +48 -12
  205. package/dist/store/migrate.d.ts +80 -0
  206. package/dist/store/migrate.js +259 -7
  207. package/dist/store/postgres-applications.d.ts +4 -0
  208. package/dist/store/postgres-applications.js +197 -0
  209. package/dist/store/postgres-holds.d.ts +10 -1
  210. package/dist/store/postgres-holds.js +116 -101
  211. package/dist/store/postgres-policy-log.d.ts +14 -0
  212. package/dist/store/postgres-policy-log.js +240 -0
  213. package/dist/store/postgres-projects.d.ts +4 -0
  214. package/dist/store/postgres-projects.js +217 -0
  215. package/dist/store/postgres.d.ts +16 -1
  216. package/dist/store/postgres.js +59 -9
  217. package/dist/store/replay.d.ts +102 -1
  218. package/dist/store/replay.js +190 -19
  219. package/dist/store/reseed.d.ts +238 -0
  220. package/dist/store/reseed.js +308 -0
  221. package/dist/store/schema.d.ts +450 -2
  222. package/dist/store/schema.js +141 -5
  223. package/dist/store/store.d.ts +29 -6
  224. package/dist/submit/resolve.js +2 -1
  225. package/dist/telemetry/calibration.d.ts +25 -0
  226. package/dist/telemetry/calibration.js +27 -1
  227. package/dist/telemetry/event.d.ts +391 -33
  228. package/dist/telemetry/event.js +84 -20
  229. package/dist/telemetry/index.d.ts +1 -0
  230. package/dist/telemetry/index.js +1 -0
  231. package/dist/telemetry/journal.d.ts +72 -3
  232. package/dist/telemetry/journal.js +9 -0
  233. package/dist/telemetry/lookup.d.ts +27 -0
  234. package/dist/telemetry/lookup.js +18 -0
  235. package/dist/telemetry/memory.js +23 -0
  236. package/dist/telemetry/migrate.js +22 -1
  237. package/dist/telemetry/postgres.d.ts +10 -1
  238. package/dist/telemetry/postgres.js +172 -90
  239. package/dist/telemetry/remeasure.d.ts +78 -0
  240. package/dist/telemetry/remeasure.js +83 -0
  241. package/dist/telemetry/retention.d.ts +2 -2
  242. package/dist/telemetry/schema.d.ts +17 -0
  243. package/dist/telemetry/schema.js +27 -1
  244. package/dist/telemetry/sink.d.ts +2 -2
  245. package/dist/testing/application-contract.d.ts +14 -0
  246. package/dist/testing/application-contract.js +270 -0
  247. package/dist/testing/contracts.d.ts +12 -7
  248. package/dist/testing/contracts.js +12 -7
  249. package/dist/testing/definitions.d.ts +78 -0
  250. package/dist/testing/definitions.js +134 -9
  251. package/dist/testing/doubles.d.ts +103 -0
  252. package/dist/testing/doubles.js +143 -0
  253. package/dist/testing/index.d.ts +2 -2
  254. package/dist/testing/index.js +2 -2
  255. package/dist/testing/journal-contract.js +140 -2
  256. package/dist/testing/model-contract.d.ts +110 -0
  257. package/dist/testing/model-contract.js +150 -0
  258. package/dist/testing/policy-log-contract.d.ts +26 -0
  259. package/dist/testing/policy-log-contract.js +344 -0
  260. package/dist/testing/project-contract.d.ts +14 -0
  261. package/dist/testing/project-contract.js +266 -0
  262. package/dist/testing/reader-signal-contract.d.ts +27 -2
  263. package/dist/testing/reader-signal-contract.js +475 -5
  264. package/dist/testing/store-contract.d.ts +7 -1
  265. package/dist/testing/store-contract.js +84 -3
  266. package/dist/theme/apply.d.ts +20 -1
  267. package/dist/theme/apply.js +5 -3
  268. package/dist/theme/derive.d.ts +22 -0
  269. package/dist/theme/derive.js +54 -1
  270. package/dist/theme/library.js +1 -1
  271. package/dist/theme/measure.d.ts +34 -0
  272. package/dist/theme/measure.js +30 -1
  273. package/dist/theme/palettes.js +7 -7
  274. package/dist/theme/ramp.d.ts +64 -0
  275. package/dist/theme/ramp.js +114 -0
  276. package/dist/theme/registry.d.ts +9 -0
  277. package/dist/theme/registry.js +11 -1
  278. package/dist/theme/separation.d.ts +97 -3
  279. package/dist/theme/separation.js +75 -0
  280. package/dist/tree/compare.d.ts +9 -0
  281. package/dist/tree/compare.js +18 -0
  282. package/dist/write/computed.d.ts +60 -0
  283. package/dist/write/computed.js +61 -0
  284. package/dist/write/held.d.ts +59 -0
  285. package/dist/write/index.d.ts +5 -2
  286. package/dist/write/index.js +5 -2
  287. package/dist/write/reseed.d.ts +151 -0
  288. package/dist/write/reseed.js +184 -0
  289. package/dist/write/revert.js +2 -16
  290. package/package.json +98 -19
@@ -0,0 +1,308 @@
1
+ import { err, ok, reduceResult } from "../result.js";
2
+ import { applyOperation } from "../tree/apply.js";
3
+ import { compareTrees } from "../tree/compare.js";
4
+ import { configurationOf } from "../tree/configuration.js";
5
+ import { namedNodeIds } from "../tree/naming.js";
6
+ import { collectNodeIds, findNode } from "../tree/navigation.js";
7
+ import { isContainerNode } from "../tree/node.js";
8
+ import { withRoot } from "../tree/tree.js";
9
+ /**
10
+ * The facets a `configure` cannot reach, so no plan can close them.
11
+ *
12
+ * A node's `kind` and an element's primitive `type` are what a node *is*, and
13
+ * changing either at a stable id would be a different node wearing the address —
14
+ * which is the case 0038 exists to refuse, not one to work around by removing
15
+ * and re-inserting. `name` and `text` are absent on purpose: both are settable
16
+ * (`configurationOf` exposes one key for each), which is what keeps a reseed
17
+ * exact.
18
+ *
19
+ * **The comparison at the end of the plan is what enforces this**, not a filter
20
+ * over this list: a facet nothing can close survives every pass and is still
21
+ * there to be reported. The list is the sentence a reader needs, and
22
+ * `reseed.test.ts` checks it against `NODE_FACETS` so a sixth facet cannot be
23
+ * left out of it.
24
+ */
25
+ export const UNRESEEDABLE_FACETS = ["kind", "type"];
26
+ /**
27
+ * One of the two is refused before anything is planned, and only one.
28
+ *
29
+ * A changed `kind` is the case where attempting the repair reports the wrong
30
+ * thing: the two kinds expose different configuration keys, so the patch names a
31
+ * key the node has not got and the plan comes back `unplannable` with an
32
+ * `unconfigurable-key` — a sentence about a patch, where a deployment needs one
33
+ * about a node. A changed `type` needs no such help: nothing in any pass touches
34
+ * a primitive, so the difference is still standing at the end and the comparison
35
+ * names it exactly.
36
+ */
37
+ const differentKinds = (authored, stored) => compareTrees(authored, stored).filter((difference) => difference.code === "changed" && difference.facets.includes("kind"));
38
+ const isElement = (node) => node.kind === "element";
39
+ const advance = (working, operation) => {
40
+ const applied = applyOperation(working.root, operation);
41
+ if (!applied.ok)
42
+ return err({ operation, error: applied.error });
43
+ return isElement(applied.value)
44
+ ? ok({ root: applied.value, operations: [...working.operations, operation] })
45
+ : err({
46
+ operation,
47
+ error: {
48
+ code: "schema-violation",
49
+ path: "root",
50
+ detail: `root became a ${applied.value.kind} node`,
51
+ },
52
+ });
53
+ };
54
+ const advanceAll = (working, operations) => reduceResult(operations, working, advance);
55
+ const childrenOf = (node) => isContainerNode(node) ? node.children : [];
56
+ const childrenAt = (root, nodeId) => {
57
+ const found = findNode(root, nodeId);
58
+ return found === null ? [] : childrenOf(found);
59
+ };
60
+ /**
61
+ * The topmost nodes the authored tree does not name, which are the only ones
62
+ * worth removing: a `remove` takes the whole subtree, and naming a node under
63
+ * one already removed would be a second operation against a node that is gone.
64
+ */
65
+ const strangersIn = (node, authored) => authored.has(node.id)
66
+ ? childrenOf(node).flatMap((child) => strangersIn(child, authored))
67
+ : [node.id];
68
+ const removals = (stored, authored) => strangersIn(stored, new Set(collectNodeIds(authored.root))).map((nodeId) => ({
69
+ op: "remove",
70
+ nodeId,
71
+ }));
72
+ const withoutChildren = (node) => isContainerNode(node) ? { ...node, children: [] } : node;
73
+ /** Whether any node under this one — not counting itself — is already in the stored page. */
74
+ const holdsStored = (node, stored) => childrenOf(node).some((child) => stored.has(child.id) || holdsStored(child, stored));
75
+ /**
76
+ * Put back every node the authored tree names and the stored page lacks, parents
77
+ * before children.
78
+ *
79
+ * A subtree with nothing of the stored page under it goes in whole, which is one
80
+ * operation for a card somebody deleted and is what a reviewer wants to read. A
81
+ * subtree that holds a node the page still has somewhere else goes in as a shell
82
+ * and its children are brought home by the claiming pass — inserting it whole
83
+ * would name an id twice and be refused.
84
+ *
85
+ * The index is clamped to the live child list, which is usually shorter than the
86
+ * authored one at this point. Nothing is lost by that: the pass after this one
87
+ * puts every child of every container in its authored position.
88
+ */
89
+ const insertAbsent = (working, authored, parentId, index, stored) => {
90
+ const absent = parentId !== undefined && !stored.has(authored.id);
91
+ const whole = absent && !holdsStored(authored, stored);
92
+ const placed = parentId === undefined || !absent
93
+ ? ok(working)
94
+ : advance(working, {
95
+ op: "insert",
96
+ parentId,
97
+ index: Math.min(index, childrenAt(working.root, parentId).length),
98
+ node: whole ? authored : withoutChildren(authored),
99
+ });
100
+ if (!placed.ok || whole)
101
+ return placed;
102
+ return reduceResult(childrenOf(authored), placed.value, (carried, child, at) => insertAbsent(carried, child, authored.id, at, stored));
103
+ };
104
+ /**
105
+ * Walk the authored tree, parents before children, doing one thing at each node.
106
+ *
107
+ * Top-down is what makes the two passes below correct rather than merely
108
+ * convergent, and the argument is the same for both: when a container is
109
+ * reached, every authored ancestor of it has already been put where the authored
110
+ * tree has it, so the container is not sitting inside one of its own authored
111
+ * children. A `move` that would be refused as going into a descendant therefore
112
+ * cannot be planned.
113
+ */
114
+ const descending = (working, authored, at) => {
115
+ const here = at(working, authored);
116
+ if (!here.ok)
117
+ return here;
118
+ return reduceResult(childrenOf(authored), here.value, (carried, child) => descending(carried, child, at));
119
+ };
120
+ /**
121
+ * Move in every child the authored tree gives this container and the stored page
122
+ * put somewhere else.
123
+ *
124
+ * Position is not settled here, deliberately. A container still holding a child
125
+ * that belongs deeper in the tree has one more child than the authored list, and
126
+ * ordering against that list would move a node to make room for one that is
127
+ * about to leave — two operations where the second was never needed. So this
128
+ * pass only ever puts a node under the parent that should have it, and the pass
129
+ * after it, by which time every container holds exactly its authored children,
130
+ * is the only one that moves anything for position's sake.
131
+ */
132
+ const claiming = (working, authored) => {
133
+ const held = childrenAt(working.root, authored.id);
134
+ return reduceResult(childrenOf(authored), working, (carried, child, index) => held.some((live) => live.id === child.id)
135
+ ? ok(carried)
136
+ : advance(carried, {
137
+ op: "move",
138
+ nodeId: child.id,
139
+ parentId: authored.id,
140
+ index: Math.min(index, childrenAt(carried.root, authored.id).length),
141
+ }));
142
+ };
143
+ /**
144
+ * Put one container's children in the order the authored tree gives them.
145
+ *
146
+ * Left to right, moving whichever child belongs at a position that does not hold
147
+ * it. A `move` is a detach followed by an insert (0009), so by the time position
148
+ * `index` is settled the positions before it are correct and the list is at least
149
+ * `index` long — the index this names is always in range.
150
+ */
151
+ const ordering = (working, authored) => reduceResult(childrenOf(authored), working, (carried, child, index) => childrenAt(carried.root, authored.id)[index]?.id === child.id
152
+ ? ok(carried)
153
+ : advance(carried, { op: "move", nodeId: child.id, parentId: authored.id, index }));
154
+ const patchFor = (stored, authored) => {
155
+ const set = Object.fromEntries(Object.entries(authored).filter(([key, value]) => JSON.stringify(stored[key]) !== JSON.stringify(value)));
156
+ return {
157
+ set,
158
+ unset: Object.keys(stored).filter((key) => !(key in authored)),
159
+ };
160
+ };
161
+ /**
162
+ * Set every node's configuration back to the authored one — props for an
163
+ * element, the value for a text node, the name for a slot.
164
+ *
165
+ * One `configure` per node that differs, keyed by the id both trees share.
166
+ * Nothing inserted above needs one: a node that went in carried the authored
167
+ * configuration with it.
168
+ */
169
+ const configuring = (working, authored) => {
170
+ const found = findNode(working.root, authored.id);
171
+ if (found === null)
172
+ return ok(working);
173
+ const patch = patchFor(configurationOf(found), configurationOf(authored));
174
+ return Object.keys(patch.set).length === 0 && patch.unset.length === 0
175
+ ? ok(working)
176
+ : advance(working, { op: "configure", nodeId: authored.id, ...patch });
177
+ };
178
+ const revivalsIn = (authored, stored) => {
179
+ const held = new Set(collectNodeIds(stored.root));
180
+ return compareTrees(authored, stored)
181
+ .filter((difference) => difference.code === "missing" && !held.has(difference.nodeId))
182
+ .map(({ nodeId, label }) => ({ nodeId, label }));
183
+ };
184
+ const retirementsIn = (authored, stored) => compareTrees(authored, stored)
185
+ .filter((difference) => difference.code === "extra")
186
+ .map(({ nodeId, label }) => ({ nodeId, label }));
187
+ const planned = (stored, authored) => {
188
+ const start = { root: stored.root, operations: [] };
189
+ const held = new Set(collectNodeIds(stored.root));
190
+ const cleared = advanceAll(start, removals(stored.root, authored));
191
+ if (!cleared.ok)
192
+ return cleared;
193
+ const filled = insertAbsent(cleared.value, authored.root, undefined, 0, held);
194
+ if (!filled.ok)
195
+ return filled;
196
+ const claimed = descending(filled.value, authored.root, claiming);
197
+ if (!claimed.ok)
198
+ return claimed;
199
+ const ordered = descending(claimed.value, authored.root, ordering);
200
+ if (!ordered.ok)
201
+ return ordered;
202
+ return descending(ordered.value, authored.root, configuring);
203
+ };
204
+ /**
205
+ * What it would take to carry a stored page to the tree its builder produces now.
206
+ *
207
+ * `stored` is the head of the page in the store; `authored` is a fresh build. The
208
+ * two must be the same page — the same tree id and the same root — because a root
209
+ * can be neither removed nor moved (0009), so there is no operation that turns one
210
+ * root into another.
211
+ *
212
+ * Pure, and it reads no log: both trees are in the caller's hand already, and the
213
+ * one question that needs the log — whether an id this plan revives was retired as
214
+ * the node that is coming back — is `idReturnsIn`'s and travels on `revives` for
215
+ * the Gate rather than being answered here.
216
+ */
217
+ export const planReseed = (stored, authored) => {
218
+ if (stored.treeId !== authored.treeId) {
219
+ return {
220
+ outcome: "different-page",
221
+ mismatch: { facet: "treeId", stored: stored.treeId, authored: authored.treeId },
222
+ };
223
+ }
224
+ if (stored.root.id !== authored.root.id) {
225
+ return {
226
+ outcome: "different-page",
227
+ mismatch: { facet: "rootId", stored: stored.root.id, authored: authored.root.id },
228
+ };
229
+ }
230
+ const mixedKinds = differentKinds(authored, stored);
231
+ if (mixedKinds.length > 0)
232
+ return { outcome: "unexpressible", blocking: mixedKinds };
233
+ if (compareTrees(authored, stored).length === 0) {
234
+ return { outcome: "already-authored", headRevision: stored.revision };
235
+ }
236
+ const plan = planned(stored, authored);
237
+ if (!plan.ok) {
238
+ return { outcome: "unplannable", operation: plan.error.operation, error: plan.error.error };
239
+ }
240
+ const remaining = compareTrees(authored, withRoot(stored, plan.value.root));
241
+ if (remaining.length > 0)
242
+ return { outcome: "unexpressible", blocking: remaining };
243
+ return plan.value.operations.length === 0
244
+ ? { outcome: "already-authored", headRevision: stored.revision }
245
+ : {
246
+ outcome: "reseedable",
247
+ operations: plan.value.operations,
248
+ headRevision: stored.revision,
249
+ revives: revivalsIn(authored, stored),
250
+ retires: retirementsIn(authored, stored),
251
+ };
252
+ };
253
+ const countOf = (count, noun) => `${count} ${noun}${count === 1 ? "" : "s"}`;
254
+ export const describeReseedPlan = (plan) => {
255
+ switch (plan.outcome) {
256
+ case "already-authored":
257
+ return `revision ${plan.headRevision} is the page the builder produces`;
258
+ case "reseedable":
259
+ return `${countOf(plan.operations.length, "operation")} would put the page back at head ${plan.headRevision}${plan.retires.length === 0 ? "" : `, removing ${countOf(plan.retires.length, "node")}`}${plan.revives.length === 0 ? "" : `, reviving ${countOf(plan.revives.length, "id")}`}`;
260
+ case "different-page":
261
+ return `${plan.mismatch.facet} ${plan.mismatch.stored} is not ${plan.mismatch.authored}`;
262
+ case "unexpressible":
263
+ return `${countOf(plan.blocking.length, "difference")} no operation can close: ${plan.blocking
264
+ .map((difference) => `${difference.label} (${difference.nodeId})`)
265
+ .join(", ")}`;
266
+ case "unplannable":
267
+ return `a planned ${plan.operation.op} was refused: ${plan.error.code}`;
268
+ }
269
+ };
270
+ const overlapIn = (entry, reseeding) => {
271
+ const overlap = namedNodeIds(entry.delta.operations).filter((nodeId) => reseeding.has(nodeId));
272
+ return overlap.length === 0 ? null : { revision: entry.revision, nodeIds: overlap };
273
+ };
274
+ const walkForDiscards = async (reader, treeId, reseeding, found, cursor) => {
275
+ const page = await reader.revisions(treeId, cursor === undefined ? {} : { cursor });
276
+ if (!page.ok)
277
+ return page;
278
+ const walked = page.value.revisions.reduce((current, entry) => {
279
+ const discarded = overlapIn(entry, reseeding);
280
+ return discarded === null ? current : [...current, discarded];
281
+ }, found);
282
+ return page.value.newer === null
283
+ ? ok(walked)
284
+ : await walkForDiscards(reader, treeId, reseeding, walked, page.value.newer);
285
+ };
286
+ /**
287
+ * Reads the whole log once and reports the entries a reseed would write over.
288
+ *
289
+ * Ascending by revision, because `DiscardedWork` is read by a person answering a
290
+ * hold (0019) and *revisions 3, 7 and 8* is the sentence they can check against
291
+ * the history screen beside it.
292
+ *
293
+ * The walk is the whole log rather than a window, and that is the honest cost: a
294
+ * reseed's reach is every node it names, and a change from the page's first week
295
+ * is as discarded as one from this morning. It is the same walk `planRevert`
296
+ * takes and it is paid once per reseed, which is an operation a person presses.
297
+ *
298
+ * A `StoreError` means the log could not be read, so nothing is known about the
299
+ * cost — and a caller must not then propose the reseed with an empty
300
+ * declaration, which would read as *the log was clean*.
301
+ */
302
+ export const reseedDiscards = async (reader, request) => {
303
+ const reseeding = new Set(namedNodeIds(request.operations));
304
+ return reseeding.size === 0
305
+ ? ok([])
306
+ : await walkForDiscards(reader, request.treeId, reseeding, []);
307
+ };
308
+ //# sourceMappingURL=reseed.js.map