@intentius/behold 0.30.2 → 0.31.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 (92) hide show
  1. package/AGENTS.md +444 -15
  2. package/README.md +14 -13
  3. package/demos.json +14 -9
  4. package/dist/cli.js +53928 -26475
  5. package/example-carve/README.md +19 -23
  6. package/example-carve/app/package.json +1 -1
  7. package/example-carve/app/src/carved.ts +3 -3
  8. package/example-carve/carve-report.json +1 -1
  9. package/example-carve/{legacy-tf → tf-root}/terraform.tfstate +1 -1
  10. package/example-choudoufu-estate/README.md +7 -0
  11. package/example-choudoufu-estate/chant.workspace.json +1 -1
  12. package/example-choudoufu-estate/scripts/choudoufu-down.sh +2 -1
  13. package/example-choudoufu-estate/scripts/choudoufu-up.sh +10 -3
  14. package/example-terraform-estate/README.md +32 -5
  15. package/example-terraform-estate/chant.workspace.json +1 -1
  16. package/example-terraform-estate/recorded/adoption/platform.json +72 -0
  17. package/example-terraform-estate/recorded/adoption/prod.json +224 -0
  18. package/example-three-cloud/README.md +111 -18
  19. package/example-three-cloud/aws/Pulumi.yaml +15 -0
  20. package/example-three-cloud/aws/index.ts +174 -0
  21. package/example-three-cloud/aws/package-lock.json +2725 -0
  22. package/example-three-cloud/aws/package.json +14 -0
  23. package/example-three-cloud/aws/tsconfig.json +17 -0
  24. package/example-three-cloud/azure/main.tf +48 -0
  25. package/example-three-cloud/azure/site.tf +104 -0
  26. package/example-three-cloud/chant.config.json +9 -0
  27. package/example-three-cloud/chant.workspace.json +30 -4
  28. package/example-three-cloud/dns/chant.config.ts +26 -1
  29. package/example-three-cloud/dns/src/records.component.ts +32 -6
  30. package/example-three-cloud/package-lock.json +48 -27
  31. package/example-three-cloud/package.json +8 -6
  32. package/example-three-cloud/scripts/emulators-up.sh +28 -1
  33. package/example-three-cloud/scripts/front-door.ts +679 -86
  34. package/package.json +13 -6
  35. package/web/adopt-pulumi.js +114 -0
  36. package/web/adopt-pulumi.test.js +28 -0
  37. package/web/adopt.js +203 -0
  38. package/web/adopt.test.js +27 -0
  39. package/web/app.js +358 -10
  40. package/web/carve-steps.js +1 -1
  41. package/web/carve-steps.test.js +2 -2
  42. package/web/change-set-overlay.js +12 -5
  43. package/web/change-set-overlay.test.js +22 -0
  44. package/web/drift-stream.js +241 -0
  45. package/web/drift-stream.test.js +146 -0
  46. package/web/edge-weight.d.ts +1 -1
  47. package/web/edge-weight.js +5 -1
  48. package/web/edge-weight.test.js +19 -0
  49. package/web/graph-query.js +180 -0
  50. package/web/graph-query.test.js +49 -0
  51. package/web/graph.html +107 -0
  52. package/web/index-diff.js +109 -0
  53. package/web/index-diff.test.js +39 -0
  54. package/web/index.html +37 -0
  55. package/web/kiosk-ask-tips.js +15 -5
  56. package/web/kiosk-ask-tips.test.js +14 -1
  57. package/web/kiosk-estate-scene.js +72 -8
  58. package/web/kiosk-light-frame.js +1 -0
  59. package/web/kiosk-light.js +41 -3
  60. package/web/kiosk-member-kind.test.js +140 -0
  61. package/web/kiosk-popover.js +4 -1
  62. package/web/kiosk-side.js +296 -0
  63. package/web/kiosk-side.test.js +249 -0
  64. package/web/kiosk-watch.js +12 -0
  65. package/web/kiosk-watch.test.js +15 -1
  66. package/web/kiosk.html +47 -1
  67. package/web/kiosk.js +106 -7
  68. package/web/member-kind.js +31 -0
  69. package/web/template-apps.js +245 -0
  70. package/web/template-apps.test.js +252 -0
  71. package/web/templates.js +285 -0
  72. package/web/templates.test.js +155 -0
  73. package/web/terragucci-lifecycle.js +112 -62
  74. package/web/terragucci-lifecycle.test.js +29 -1
  75. package/web/terragucci-timeline.js +39 -2
  76. package/web/terragucci-timeline.test.js +42 -1
  77. package/example-three-cloud/aws/chant.config.ts +0 -8
  78. package/example-three-cloud/aws/site.component.ts +0 -13
  79. package/example-three-cloud/aws/src/site.ts +0 -27
  80. package/example-three-cloud/azure/chant.config.ts +0 -1
  81. package/example-three-cloud/azure/src/site.component.ts +0 -13
  82. package/example-three-cloud/azure/src/site.ts +0 -18
  83. /package/example-carve/{legacy-tf → tf-root}/cdn.tf +0 -0
  84. /package/example-carve/{legacy-tf → tf-root}/compute.tf +0 -0
  85. /package/example-carve/{legacy-tf → tf-root}/floci-override.tf.disabled +0 -0
  86. /package/example-carve/{legacy-tf → tf-root}/modules/cdn/main.tf +0 -0
  87. /package/example-carve/{legacy-tf → tf-root}/naming.tf +0 -0
  88. /package/example-carve/{legacy-tf → tf-root}/network.tf +0 -0
  89. /package/example-carve/{legacy-tf → tf-root}/observability.tf +0 -0
  90. /package/example-carve/{legacy-tf → tf-root}/outputs.tf +0 -0
  91. /package/example-carve/{legacy-tf → tf-root}/storage.tf +0 -0
  92. /package/example-carve/{legacy-tf → tf-root}/versions.tf +0 -0
@@ -0,0 +1,140 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
4
+ import { composedReportOf, composedView } from "../src/chant-compose.ts";
5
+ import { drawScene, sceneModel } from "./kiosk-estate-scene.js";
6
+ import { readingRows } from "./kiosk.js";
7
+ import { planOf } from "./kiosk-light.js";
8
+ import { HOLE_WORDS, KIND_ROWS, KIND_TAGS, memberKinds } from "./member-kind.js";
9
+
10
+ // behold#1008: a composed chant run draws a hole member's box and a previewed member's box differently, and the
11
+ // kiosk's "How to read this" says what each mark means. The run is behold#969's create plan
12
+ // (src/__fixtures__/chant-compose/create-plan.json): network creates subnetB, app reads its id unknown (previewed),
13
+ // edge reads app's known values (plain). As src/chant-compose.test.ts does, edge is made a hole.
14
+ const FIX = join(import.meta.dirname, "..", "src", "__fixtures__", "chant-compose");
15
+ const plan = () => JSON.parse(readFileSync(join(FIX, "create-plan.json"), "utf8"));
16
+ const REASON = "no preview: CloudFormation refused the stand-in";
17
+ const OPEN = { width: 1900, height: 740 };
18
+
19
+ function withHole(entriesToo) {
20
+ const doc = plan();
21
+ doc.changeSet.members.find((m) => m.member === "edge").holes = [{ address: "allowlist", reason: REASON }];
22
+ if (!entriesToo) doc.changeSet.entries = doc.changeSet.entries.filter((e) => e.member !== "edge");
23
+ return composedView(composedReportOf(doc), undefined);
24
+ }
25
+
26
+ // A stand-in for the svg drawScene writes into: elements with attributes, children and text, enough to walk.
27
+ function fakeNode(tag) {
28
+ const n = {
29
+ tagName: tag,
30
+ attrs: {},
31
+ children: [],
32
+ parent: null,
33
+ textContent: "",
34
+ setAttribute: (k, v) => void (n.attrs[k] = v),
35
+ getAttribute: (k) => (k in n.attrs ? n.attrs[k] : null),
36
+ hasAttribute: (k) => k in n.attrs,
37
+ appendChild: (c) => {
38
+ if (c.parent) c.remove();
39
+ c.parent = n;
40
+ n.children.push(c);
41
+ return c;
42
+ },
43
+ remove: () => {
44
+ if (!n.parent) return;
45
+ n.parent.children = n.parent.children.filter((x) => x !== n);
46
+ n.parent = null;
47
+ },
48
+ querySelectorAll: (sel) => {
49
+ const m = /^:scope > \[([\w-]+)\]$/.exec(sel);
50
+ return m ? n.children.filter((c) => c.hasAttribute(m[1])) : [];
51
+ },
52
+ querySelector: (sel) => n.querySelectorAll(sel)[0] || null,
53
+ };
54
+ return n;
55
+ }
56
+ const walk = (n) => [n, ...n.children.flatMap(walk)];
57
+ const drawn = (model) => {
58
+ const svg = fakeNode("svg");
59
+ drawScene(svg, model);
60
+ return walk(svg);
61
+ };
62
+ const withAttr = (nodes, k, v) => nodes.filter((n) => n.attrs[k] === v);
63
+
64
+ let was;
65
+ beforeEach(() => {
66
+ was = globalThis.document;
67
+ globalThis.document = { createElementNS: (_ns, tag) => fakeNode(tag) };
68
+ });
69
+ afterEach(() => {
70
+ globalThis.document = was;
71
+ });
72
+
73
+ describe("a hole member and a previewed member, drawn apart (behold#1008)", () => {
74
+ it("reads each member's kind from the composed answer", () => {
75
+ const v = withHole(true);
76
+ expect([...memberKinds(v.answer)].map(([m, k]) => [m, k.kind])).toEqual([["app", "previewed"], ["edge", "hole"], ["network", "plain"]]);
77
+ expect(memberKinds(v.answer).get("edge").reasons).toEqual([REASON]);
78
+ expect(memberKinds(null).size).toBe(0);
79
+ });
80
+
81
+ it("puts one hole mark on edge's box and the previewed mark on app's, and none on network's", () => {
82
+ const v = withHole(true);
83
+ const nodes = drawn(sceneModel(v.answer, planOf(v.answer), null, OPEN));
84
+ const boxes = nodes.filter((n) => n.hasAttribute("data-layout-box"));
85
+ expect(boxes.map((b) => [b.attrs["data-layout-box"], b.attrs["data-member-kind"]])).toEqual([
86
+ ["box:network", "plain"],
87
+ ["box:app", "previewed"],
88
+ ["box:edge", "hole"],
89
+ ]);
90
+ const holes = withAttr(nodes, "data-kl-kind-mark", "hole");
91
+ const previewed = withAttr(nodes, "data-kl-kind-mark", "previewed");
92
+ expect(holes).toHaveLength(1);
93
+ expect(previewed).toHaveLength(1);
94
+ expect(holes[0].parent.attrs["data-layout-box"]).toBe("box:edge");
95
+ expect(previewed[0].parent.attrs["data-layout-box"]).toBe("box:app");
96
+ expect(walk(holes[0]).some((n) => n.textContent === KIND_TAGS.hole)).toBe(true);
97
+ expect(walk(previewed[0]).some((n) => n.textContent === KIND_TAGS.previewed)).toBe(true);
98
+ // The hole's box is dashed; the others are not.
99
+ const outline = (name) => nodes.find((n) => n.attrs["data-group-id"] === name);
100
+ expect(outline("edge").attrs["stroke-dasharray"]).toBeTruthy();
101
+ expect(outline("app").attrs["stroke-dasharray"]).toBeUndefined();
102
+ expect(outline("network").attrs["stroke-dasharray"]).toBeUndefined();
103
+ // Every member's cards are still there: network's subnet, app's interface, edge's allowlist.
104
+ expect(nodes.filter((n) => n.hasAttribute("data-node-id")).map((n) => n.attrs["data-node-id"])).toEqual(["chant/network/subnetB", "chant/app/workerInterface", "chant/edge/allowlist"]);
105
+ });
106
+
107
+ it("keeps a hole's box when nothing was previewed in it, with an empty slot saying so and why", () => {
108
+ const v = withHole(false);
109
+ const m = sceneModel(v.answer, planOf(v.answer), null, OPEN);
110
+ expect(m.roots.map((r) => [r.path, r.kind, r.cards.length])).toEqual([["network", "plain", 1], ["app", "previewed", 1], ["edge", "hole", 0]]);
111
+ const edge = m.roots[2];
112
+ expect(edge.hole).toMatchObject({ words: HOLE_WORDS, reasons: [REASON] });
113
+ expect(edge.hole.y).toBeGreaterThanOrEqual(edge.y);
114
+ expect(edge.hole.y + edge.hole.h).toBeLessThanOrEqual(edge.y + edge.h);
115
+ const nodes = drawn(m);
116
+ const slot = nodes.filter((n) => n.attrs["data-kl-hole"] === "edge");
117
+ expect(slot).toHaveLength(1);
118
+ expect(walk(slot[0]).map((n) => n.textContent)).toContain(HOLE_WORDS);
119
+ expect(walk(slot[0]).map((n) => n.textContent)).toContain(REASON);
120
+ expect(withAttr(nodes, "data-kl-kind-mark", "hole")).toHaveLength(1);
121
+ });
122
+
123
+ it("draws no mark on a run with no kinds (a composed choudoufu run)", () => {
124
+ const v = withHole(true);
125
+ const answer = { ...v.answer, chant: undefined };
126
+ const nodes = drawn(sceneModel(answer, planOf(answer), null, OPEN));
127
+ expect(nodes.filter((n) => n.hasAttribute("data-member-kind"))).toEqual([]);
128
+ expect(nodes.filter((n) => n.hasAttribute("data-kl-kind-mark"))).toEqual([]);
129
+ });
130
+
131
+ it("says in the legend what the hole mark and the previewed mark mean, on chant's composed page alone", () => {
132
+ const rows = readingRows("resources", "chant-compose");
133
+ expect(rows.filter((r) => r.mark === "hole")).toEqual([KIND_ROWS[0]]);
134
+ expect(rows.filter((r) => r.mark === "previewed")).toEqual([KIND_ROWS[1]]);
135
+ expect(KIND_ROWS[0].words).toMatch(/^Dashed box: a stack no preview could be made of\.$/);
136
+ expect(KIND_ROWS[1].words).toMatch(/^Tag previewed: some values known after apply\.$/);
137
+ for (const r of KIND_ROWS) expect(r.words.length, r.words).toBeLessThanOrEqual(50);
138
+ for (const tool of [null, "chant", "terragrunt", "choudoufu-compose"]) for (const view of ["components", "resources"]) expect(readingRows(view, tool).some((r) => r.mark === "hole" || r.mark === "previewed")).toBe(false);
139
+ });
140
+ });
@@ -59,8 +59,11 @@ export function valueWords(v, max = 40) {
59
59
  return s.length > max ? `${s.slice(0, max - 3)}...` : s;
60
60
  }
61
61
 
62
- /** The instance part of an address beyond its block (`[0]`, `["a"]`), or "". */
62
+ /** The instance part of an address beyond its block (`[0]`, `["a"]`), or "". An address
63
+ * under an expanded module call (`module.app[0].aws_eip.pool[1]`, chant#4486) gives "",
64
+ * so the line shows the whole address: `[1]` alone would not say which module instance. */
63
65
  function instanceKey(address) {
66
+ if (/\]\./.test(address || "")) return "";
64
67
  const m = /(\[[^\]]*\])$/.exec(address || "");
65
68
  return m ? m[1] : "";
66
69
  }
@@ -0,0 +1,296 @@
1
+ // The Watch side pane of the prompt demo: a column at the right of the graph, above the prompt pane, on a wide
2
+ // screen. At its top the act's run as a trace (the steps with their timings: the plan, the approval, the apply, and
3
+ // each estate's part of the apply), drawn with the rows Inspect's trace box uses; under it one small card about what
4
+ // the terminal's current line names (the boxes its `ask.light` and `ask.paths` light): the resource, its before and
5
+ // after, and which estate's record was written. It says nothing the terminal does not: it shows the run's facts.
6
+ //
7
+ // The models are pure (data in, data out, tested next door); renderSide draws them. web/kiosk.js wires it.
8
+
9
+ import { applyOf, cardLines, cardTitle, lineState, planOf, selectLight, stateMarks, stateWords } from "./kiosk-light.js";
10
+ import { msShort } from "./kiosk-wave-traces.js";
11
+
12
+ /** Where a viewer's choice to fold the pane is kept, in their own browser. */
13
+ export const SIDE_STORE = "behold.kiosk.side";
14
+
15
+ /** Phones (up to this width) keep the graph and the prompt only. */
16
+ export const SIDE_PHONE = "(max-width: 700px)";
17
+
18
+ /** Whether the pane is open: open unless this viewer folded it. A storage that throws counts as open. */
19
+ export function sideStored(store) {
20
+ try {
21
+ return !(store && store.getItem(SIDE_STORE) === "closed");
22
+ } catch {
23
+ return true;
24
+ }
25
+ }
26
+
27
+ /** Keep the viewer's choice; a storage that throws keeps it for this visit only. */
28
+ export function keepSide(store, open) {
29
+ try {
30
+ if (store) store.setItem(SIDE_STORE, open ? "open" : "closed");
31
+ } catch {
32
+ /* private mode: the choice lasts this visit */
33
+ }
34
+ }
35
+
36
+ /**
37
+ * How the pane shows: "open", "folded" (a tab at the right edge), or "none". Only in Watch with the prompt pane up
38
+ * (ask mode), and never on a phone.
39
+ */
40
+ export function sideState({ ask, phone, open }) {
41
+ if (!ask || phone) return "none";
42
+ return open ? "open" : "folded";
43
+ }
44
+
45
+ const ms = (v) => {
46
+ const t = typeof v === "string" ? Date.parse(v) : NaN;
47
+ return Number.isFinite(t) ? t : null;
48
+ };
49
+ const lastSegment = (path) => String(path || "").split("/").filter(Boolean).pop() || String(path || "");
50
+
51
+ /** A job's or a step's tone: done, running, waiting, failed, or skipped. */
52
+ export function jobTone(status) {
53
+ if (status === "success" || status === "applied" || status === "approved") return "done";
54
+ if (status === "running" || status === "applying") return "running";
55
+ if (status === "failure" || status === "failed" || status === "cancelled" || status === "partial") return "failed";
56
+ if (status === "waiting" || status === "queued" || status === "blocked") return "waiting";
57
+ return "skipped";
58
+ }
59
+
60
+ /**
61
+ * The step the trace is drawn at: the terminal's current turn (`turn`, `{ scenario, step }` from the prompt pane's
62
+ * events), on the cycle its scenario was last seen on (`cycles`, scenario to cycle), since the terminal plays its
63
+ * steps in turn and may be behind demo.json (`demo`). demo.json's step when no turn has played, or the turn's
64
+ * scenario was never seen.
65
+ */
66
+ export function sideStep(demo, turn, cycles) {
67
+ if (!turn || !cycles || !cycles.has(turn.scenario)) return demo || null;
68
+ const cycle = cycles.get(turn.scenario);
69
+ if (demo && demo.cycle === cycle) return { ...demo, step: turn.step };
70
+ return { ...(demo || {}), cycle, scenario: turn.scenario, step: turn.step };
71
+ }
72
+
73
+ /** Whether the step is at or past its act's merge, so the run view the kiosk holds is this act's apply. */
74
+ export function sideApplied(demo) {
75
+ return !!demo && ["merged", "applying", "waiting", "resumed", "applied", "failed", "reset"].includes(demo.step);
76
+ }
77
+
78
+ /**
79
+ * The run view the pane may read for the act at `demo`: only once the act has merged, and only the run view of the
80
+ * act's own merge commit (`progress.run.commit`, which the reads name). The kiosk's read can be the last merge's run
81
+ * view (an earlier act's, or another demo's), and before this act's apply nothing of its own has been written: the
82
+ * pane then shows plan-time facts alone. Null when there is none to read.
83
+ */
84
+ export function actRunView(demo, progress) {
85
+ if (!sideApplied(demo) || !progress || progress.error || !progress.run || progress.run.present === false) return null;
86
+ const have = typeof progress.run.commit === "string" ? progress.run.commit : "";
87
+ const want = typeof demo.commit === "string" ? demo.commit : "";
88
+ if (have.length < 7 || want.length < 7 || !(have.startsWith(want) || want.startsWith(have))) return null;
89
+ return progress;
90
+ }
91
+
92
+ const APPROVED_STEPS = new Set(["approved", "merged", "applying", "waiting", "resumed", "applied", "failed", "reset"]);
93
+
94
+ /** A pipeline job's place in the trace: "plan", "approval", "apply" (with its wave, for an apply-wave-N job), or null for the bookkeeping jobs. */
95
+ export function jobKind(name) {
96
+ const n = String(name || "");
97
+ if (n === "plan" || n === "replan") return { kind: "plan", label: n === "replan" ? "re-plan" : "plan" };
98
+ if (n === "approval") return { kind: "approval" };
99
+ const w = /^apply-wave-(\d+)$/.exec(n);
100
+ if (w) return { kind: "apply", wave: Number(w[1]) };
101
+ if (n === "apply") return { kind: "apply", wave: null };
102
+ return null;
103
+ }
104
+
105
+ /**
106
+ * The act's run as trace rows, in the order they ran: `{ rows, from, to }`. Each row is `{ key, label, tone, at, end,
107
+ * ms, depth }`, times in epoch ms (`end` null while it runs), `depth` 1 for an estate's part of the apply. The rows,
108
+ * from the jobs of the act's runs (the bookkeeping jobs, checks, notes and tips, left out):
109
+ * - the plan (and a re-plan);
110
+ * - the approval: the wait from the plan's end to the approval (its job's end, else the first apply's run), or to now
111
+ * while the step still waits on it;
112
+ * - the apply: one row per apply job, named for the estates its wave applies when the run view says (else "wave N"),
113
+ * and, for a single apply job, each estate's part of it from the run view (no time of its own: the run view states
114
+ * each part's outcome, not when it ran).
115
+ * `runs` is the runs index (web/kiosk-runs.js), `demo` the step, `answer` and `progress` the kiosk's terragucci reads.
116
+ */
117
+ export function runTrace(runs, demo, { answer = null, progress = null, now = Date.now() } = {}) {
118
+ if (!demo || !Number.isInteger(demo.cycle)) return { rows: [], from: null, to: null };
119
+ const mine = (Array.isArray(runs) ? runs : [])
120
+ .filter((r) => r && r.cycle === demo.cycle)
121
+ .sort((a, b) => (ms(a.created_at) || 0) - (ms(b.created_at) || 0) || (a.id || 0) - (b.id || 0));
122
+ const jobs = [];
123
+ for (const run of mine) {
124
+ for (const j of run.jobs || []) {
125
+ const k = jobKind(j.name);
126
+ const at = ms(j.started_at);
127
+ if (!k || (at === null && jobTone(j.status) === "skipped")) continue;
128
+ jobs.push({ run, job: j, ...k, at, end: ms(j.ended_at), created: ms(run.created_at) });
129
+ }
130
+ }
131
+ const row = (x, label) => ({ key: `${x.run.id}/${x.job.id ?? x.job.name}`, label, tone: jobTone(x.job.status), at: x.at, end: x.end, ms: x.at !== null ? (x.end ?? now) - x.at : null, depth: 0, run: x.run.id });
132
+ const rows = [];
133
+ const plans = jobs.filter((x) => x.kind === "plan");
134
+ for (const x of plans) rows.push(row(x, x.label));
135
+ const applies = jobs.filter((x) => x.kind === "apply");
136
+ const planEnd = plans.length ? plans[plans.length - 1].end : null;
137
+ if (planEnd !== null) {
138
+ const job = jobs.find((x) => x.kind === "approval" && x.end !== null);
139
+ const firstApply = applies.length ? applies[0].created ?? applies[0].at : null;
140
+ const done = job ? job.end : firstApply;
141
+ const approved = done !== null || APPROVED_STEPS.has(demo.step);
142
+ rows.push({ key: "approval", label: "approval", tone: approved ? "done" : "waiting", at: planEnd, end: done, ms: done !== null ? Math.max(0, done - planEnd) : approved ? null : Math.max(0, now - planEnd), depth: 0 });
143
+ }
144
+ const waveRoots = new Map();
145
+ for (const w of (progress && Array.isArray(progress.states) ? progress.states : [])) {
146
+ const roots = w.composed && Array.isArray(w.composed.members) ? w.composed.members.map((m) => m.root) : Array.isArray(w.roots) ? w.roots : [];
147
+ if (Number.isInteger(w.number) && roots.length) waveRoots.set(w.number, roots.map(lastSegment).join(", "));
148
+ }
149
+ for (const x of applies) {
150
+ const label = x.wave === null ? "apply" : waveRoots.get(x.wave) || `wave ${x.wave}`;
151
+ const r = row(x, label);
152
+ rows.push(r);
153
+ if (applies.length === 1) for (const part of applyParts(answer, progress)) rows.push({ ...part, key: `${r.key}/${part.root}`, at: r.at, end: r.end, ms: null, depth: 1 });
154
+ }
155
+ const times = rows.flatMap((r) => [r.at, r.end]).filter((t) => t !== null);
156
+ const from = times.length ? Math.min(...times) : null;
157
+ const to = rows.some((r) => r.at !== null && r.end === null) ? now : times.length ? Math.max(...times) : null;
158
+ return { rows, from, to: to !== null && from !== null && to <= from ? from + 1 : to };
159
+ }
160
+
161
+ const PART_WORDS = { applied: "applied", "no-changes": "applied", partial: "partly applied", failed: "partly applied", "not-started": "not applied" };
162
+
163
+ /** Each estate's part of the apply, from the run view: `{ root, label, tone, words }`, in the plan's order. */
164
+ export function applyParts(answer, progress) {
165
+ const plan = planOf(answer, progress);
166
+ const roots = plan.roots.length ? plan.roots.map((r) => r.path) : progress && Array.isArray(progress.reads) ? progress.reads.map((r) => r.root) : [];
167
+ const out = [];
168
+ for (const root of roots) {
169
+ const a = applyOf(root, progress);
170
+ if (!a) continue;
171
+ const words = a.apply ? PART_WORDS[a.apply] || a.apply : a.waveState === "applying" || a.waveState === "running" ? "applying" : "waiting";
172
+ const tone = a.apply ? jobTone(a.apply === "no-changes" ? "applied" : a.apply === "not-started" ? "skipped" : a.apply) : jobTone(words);
173
+ out.push({ root, label: lastSegment(root), tone, words });
174
+ }
175
+ return out;
176
+ }
177
+
178
+ /**
179
+ * The card about the terminal's current line: what its `ask.light` names (and `ask.paths` reaches), from the plan.
180
+ * `{ items, estates }` or null when the line names nothing the plan has. Each item is `{ key, name, type, action,
181
+ * estate, lines }`, `lines` the attribute lines the graph's card shows (`{ state, text }`: the before and after, or
182
+ * the value known after apply); each estate is `{ name, words }`, its records written or not. At most `max` items.
183
+ */
184
+ export function detailsModel(answer, progress, ask, { max = 3 } = {}) {
185
+ if (!ask || !answer || answer.error) return null;
186
+ const plan = planOf(answer, progress);
187
+ const sel = selectLight(plan, ask);
188
+ if (!sel) return null;
189
+ const light = Array.isArray(ask.light) ? ask.light : [];
190
+ const lines = cardLines(plan, light);
191
+ const items = sel.lit.slice(0, max).map((key) => {
192
+ const res = plan.resources.get(key);
193
+ const t = cardTitle(res.address);
194
+ return {
195
+ key,
196
+ name: t.name,
197
+ type: t.type,
198
+ action: res.action || null,
199
+ estate: lastSegment(res.root),
200
+ // An id line only for a resource the apply creates: an update keeps its id, and its card shows the value that moves.
201
+ lines: lines.filter((l) => l.key === key && !(l.path === "id" && res.action !== "create")).map((l) => lineState(l, answer, progress)),
202
+ };
203
+ });
204
+ const marks = stateMarks(answer, progress);
205
+ // An estate is named only once there is something to say of it: its apply's outcome, its records written.
206
+ const estates = sel.roots
207
+ .map((root) => {
208
+ const m = marks[root];
209
+ return { name: lastSegment(root), words: m ? stateWords(m) || null : null, written: !!(m && m.written) };
210
+ })
211
+ .filter((e) => e.words);
212
+ return { items, more: Math.max(0, sel.lit.length - items.length), estates };
213
+ }
214
+
215
+ // ---------------------------------------------------------------------------
216
+ // The DOM.
217
+
218
+ const el = (doc, tag, cls, text) => {
219
+ const n = doc.createElement(tag);
220
+ if (cls) n.className = cls;
221
+ if (text !== undefined && text !== null) n.textContent = text;
222
+ return n;
223
+ };
224
+
225
+ /** The head over the trace and the card. */
226
+ export const SIDE_WORDS = { run: "This act's run", details: "On this line", fold: "Hide", unfold: "Run details" };
227
+
228
+ /**
229
+ * Draw the pane into `host`: the trace (`trace`, from runTrace) as rows of Inspect's trace box (`.k-wf`), then
230
+ * `traces(node)` (the waves' Tempo traces, as Inspect draws them, when it has them), then the card (`details`). The
231
+ * DOM is drawn again only when what it shows changes (`key`).
232
+ */
233
+ export function renderSide(host, { trace, details }, { doc = typeof document !== "undefined" ? document : null, traces = null, onFold = null } = {}) {
234
+ if (!host || !doc) return;
235
+ const key = JSON.stringify([trace.rows.map((r) => [r.key, r.tone, r.words, r.ms === null ? null : Math.floor(r.ms / 1000)]), details]);
236
+ if (host.dataset && host.dataset.key === key) return;
237
+ if (host.dataset) host.dataset.key = key;
238
+ host.replaceChildren();
239
+ const head = el(doc, "div", "k-side-head");
240
+ head.appendChild(el(doc, "h2", "k-side-title", SIDE_WORDS.run));
241
+ if (onFold) {
242
+ const fold = el(doc, "button", "k-side-fold", SIDE_WORDS.fold);
243
+ fold.type = "button";
244
+ fold.setAttribute("aria-label", "Hide the run details");
245
+ fold.addEventListener("click", onFold);
246
+ head.appendChild(fold);
247
+ }
248
+ host.appendChild(head);
249
+ const wf = el(doc, "div", "k-wf k-side-wf");
250
+ const span = trace.from !== null && trace.to !== null ? trace.to - trace.from : 0;
251
+ for (const r of trace.rows) {
252
+ const row = el(doc, "div", `k-wf-row k-wf-plain k-side-row k-tone-${r.tone}${r.depth ? " k-side-part" : ""}`);
253
+ row.appendChild(el(doc, "span", "k-wf-wave", r.label));
254
+ const track = el(doc, "span", "k-wf-track");
255
+ if (r.depth) track.appendChild(el(doc, "span", "k-side-words", r.words));
256
+ else if (r.at !== null && span > 0) {
257
+ const seg = el(doc, "span", `k-wf-seg k-side-seg k-tone-${r.tone}`);
258
+ const a = (r.at - trace.from) / span;
259
+ const b = ((r.end ?? trace.to) - trace.from) / span;
260
+ seg.style.left = `${(a * 100).toFixed(2)}%`;
261
+ seg.style.width = `${Math.max(0.5, (b - a) * 100).toFixed(2)}%`;
262
+ track.appendChild(seg);
263
+ }
264
+ row.appendChild(track);
265
+ row.appendChild(el(doc, "span", "k-wf-words", r.depth ? "" : r.ms !== null ? msShort(r.ms) : ""));
266
+ wf.appendChild(row);
267
+ }
268
+ if (!trace.rows.length) wf.appendChild(el(doc, "p", "k-side-quiet", "No run yet in this act."));
269
+ host.appendChild(wf);
270
+ if (traces) {
271
+ const t = el(doc, "section", "k-i-traces k-side-traces");
272
+ traces(t);
273
+ if (!t.hidden) host.appendChild(t);
274
+ }
275
+ if (details && (details.items.length || details.estates.length)) {
276
+ const card = el(doc, "section", "k-side-card");
277
+ card.appendChild(el(doc, "h2", "k-side-title", SIDE_WORDS.details));
278
+ for (const it of details.items) {
279
+ const item = el(doc, "div", "k-side-item");
280
+ const name = el(doc, "p", "k-side-name");
281
+ name.appendChild(el(doc, "b", null, it.name));
282
+ name.appendChild(el(doc, "span", "k-side-type", [it.type, it.action, it.estate].filter(Boolean).join(" · ")));
283
+ item.appendChild(name);
284
+ for (const l of it.lines) item.appendChild(el(doc, "p", `k-side-line k-line-${l.state}`, l.text));
285
+ card.appendChild(item);
286
+ }
287
+ if (details.more) card.appendChild(el(doc, "p", "k-side-quiet", `and ${details.more} more`));
288
+ for (const e of details.estates) {
289
+ const p = el(doc, "p", `k-side-estate${e.written ? " k-side-written" : ""}`);
290
+ p.appendChild(el(doc, "b", null, e.name));
291
+ if (e.words) p.appendChild(el(doc, "span", "k-side-type", e.words));
292
+ card.appendChild(p);
293
+ }
294
+ host.appendChild(card);
295
+ }
296
+ }