@intentius/behold 0.29.42 → 0.29.44

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 (68) hide show
  1. package/AGENTS.md +77 -0
  2. package/dist/cli.js +3969 -1918
  3. package/package.json +1 -1
  4. package/web/app.js +1463 -80
  5. package/web/carve-narration.js +33 -1
  6. package/web/chant-live-pace.js +138 -0
  7. package/web/chant-live-pace.test.js +116 -0
  8. package/web/chant-pr-marks.js +417 -39
  9. package/web/chant-pr-marks.test.js +208 -4
  10. package/web/collision-marks.js +124 -0
  11. package/web/collision-marks.test.js +89 -0
  12. package/web/cone.js +269 -0
  13. package/web/cone.test.js +213 -0
  14. package/web/dots-cards.js +362 -0
  15. package/web/dots-cards.test.js +303 -0
  16. package/web/dots-live.test.js +151 -1
  17. package/web/dots-lod.js +20 -0
  18. package/web/dots-lod.test.js +19 -1
  19. package/web/dots-scene.js +110 -10
  20. package/web/dots.js +489 -87
  21. package/web/embed.js +17 -1
  22. package/web/embed.test.js +19 -1
  23. package/web/event-help-modal.js +85 -3
  24. package/web/event-help-modal.test.js +85 -1
  25. package/web/feature-flags.js +143 -0
  26. package/web/feature-flags.test.js +96 -0
  27. package/web/index.html +33 -0
  28. package/web/kiosk-carve.js +59 -6
  29. package/web/kiosk-event-help-ui.js +150 -19
  30. package/web/kiosk-event-help.js +102 -15
  31. package/web/kiosk-event-help.test.js +58 -1
  32. package/web/kiosk-metrics.js +4 -3
  33. package/web/kiosk-runs.js +8 -5
  34. package/web/kiosk-shot.js +157 -3
  35. package/web/kiosk-shot.test.js +223 -1
  36. package/web/kiosk-source.js +43 -0
  37. package/web/kiosk-source.test.js +39 -0
  38. package/web/kiosk-story-gate.test.js +44 -0
  39. package/web/kiosk-track.js +312 -0
  40. package/web/kiosk-track.test.js +148 -0
  41. package/web/kiosk-unknown-step.test.js +58 -0
  42. package/web/kiosk-vendor.js +71 -4
  43. package/web/kiosk-vendor.test.js +103 -3
  44. package/web/kiosk-watch.js +154 -1
  45. package/web/kiosk-watch.test.js +53 -1
  46. package/web/kiosk.html +57 -22
  47. package/web/kiosk.js +566 -53
  48. package/web/kiosk.test.js +2 -1
  49. package/web/small-stage.test.js +150 -0
  50. package/web/terragucci-blocked.test.js +192 -0
  51. package/web/terragucci-change.js +197 -17
  52. package/web/terragucci-components-live.js +189 -13
  53. package/web/terragucci-live-pace.js +92 -0
  54. package/web/terragucci-wave-results.test.js +125 -0
  55. package/web/terralith.html +7 -7
  56. package/web/tg-outside.js +124 -0
  57. package/web/tg-outside.test.js +100 -0
  58. package/web/tg-read-pills.js +122 -0
  59. package/web/tg-read-pills.test.js +117 -0
  60. package/web/theme.js +13 -1
  61. package/web/theme.test.js +21 -1
  62. package/web/tooltip.js +19 -7
  63. package/web/tooltip.test.js +15 -0
  64. package/web/tours/terralith.json +4 -4
  65. package/web/trunk-lane.test.js +1 -1
  66. package/web/view-url.js +8 -2
  67. package/web/why.js +129 -1
  68. package/web/why.test.js +113 -1
@@ -10,19 +10,71 @@
10
10
  // - `waves[n]`: the wave's state (waiting while the gate waits on the plan's
11
11
  // digest), the run's job status while it runs, and the facts on its box.
12
12
  //
13
- // Only what chant recorded: while pr-apply runs, chant has printed nothing
14
- // per wave or component yet (chant#4007), so the boxes say the job runs and
15
- // the cards keep the plan they apply.
13
+ // Live, when the stage uploaded its run events (chant#4007, the answer's
14
+ // `live`, folded by src/chant-pr-events.ts): each wave box says applying or
15
+ // done, and each card applying, applied, failed with a short reason, or
16
+ // blocked by the component that failed. Without events, only what the
17
+ // stage's summary says: while pr-apply runs it has printed nothing per wave
18
+ // or component yet, so the boxes say the job runs and the cards keep the
19
+ // plan they apply.
16
20
 
17
21
  /** The issue that asks chant for progress per wave and component, and a report a reader can fetch. */
18
22
  export const PROGRESS_ISSUE = "chant#4007";
19
23
 
20
24
  /** A card's state in one or two words, when the full words don't fit. */
21
- export const CHANT_SHORT = { planned: "planned", applied: "applied", unchanged: "unchanged", failed: "failed", refused: "refused", "not-started": "held back" };
25
+ export const CHANT_SHORT = { "applied-earlier": "applied earlier", planned: "planned", queued: "waiting", applying: "applying", applied: "applied", "already-applied": "applied before", unchanged: "unchanged", failed: "failed", refused: "refused", blocked: "blocked", "not-started": "did not run" };
22
26
 
23
- const TONE = { planned: "plan", waiting: "wait", applying: "busy", applied: "ok", failed: "bad", refused: "bad" };
27
+ const TONE = { "applied-earlier": "ok", planned: "plan", queued: "plan", waiting: "wait", applying: "busy", applied: "ok", "already-applied": "ok", failed: "bad", refused: "bad", blocked: "wait" };
24
28
  const toneOf = (state) => TONE[state] || "idle";
25
29
 
30
+ /** An error as a few words for a card: its first line, without a tool's prefix, cut at a word near `max`. */
31
+ export function shortReason(error, max = 60) {
32
+ if (typeof error !== "string" || !error.trim()) return null;
33
+ let t = error.split("\n").map((l) => l.trim()).find(Boolean) || "";
34
+ t = t
35
+ .replace(/^(error|Error|ERROR)[:\s]+/, "")
36
+ .replace(/^\S+ failed:\s*/, "")
37
+ // A tool's `<step> "<name>":` prefix names what the card already names.
38
+ .replace(/^[\w.-]+ "[^"]+":\s*/, "")
39
+ // The clause before a dash is the specific part; what follows is advice ("Inspect the stack events…").
40
+ .replace(/\s+[—–]\s+.*$/, "")
41
+ // CloudFormation's wrapper around a resource's own message.
42
+ .replace(/^Resource handler returned message:\s*/i, "")
43
+ .replace(/\s*\((?:Service|RequestToken|HandlerErrorCode|Status Code)\b[^)]*\)/g, "")
44
+ .replace(/^"([^]*)"\s*$/, "$1")
45
+ .replace(/[.\s]+$/, "");
46
+ if (t.length <= max) return t;
47
+ const cut = t.slice(0, max);
48
+ const sp = cut.lastIndexOf(" ");
49
+ return `${(sp > max * 0.6 ? cut.slice(0, sp) : cut).replace(/[,;:\s]+$/, "")}…`;
50
+ }
51
+
52
+ /**
53
+ * The failed component `name`'s dependencies lead back to, from the graph's
54
+ * `needs` (component -> what it depends on), or null: a failed dependency
55
+ * itself, else a blocked one's blocker. `stateOf(c)` is `{ state, blockedBy }`.
56
+ */
57
+ export function blockerOf(name, needs, stateOf) {
58
+ if (!needs) return null;
59
+ const seen = new Set();
60
+ const walk = (n) => {
61
+ for (const d of needs[n] || []) {
62
+ if (seen.has(d)) continue;
63
+ seen.add(d);
64
+ const s = stateOf(d);
65
+ if (s && s.state === "failed") return d;
66
+ if (s && s.state === "blocked" && s.blockedBy) return s.blockedBy;
67
+ const up = walk(d);
68
+ if (up) return up;
69
+ }
70
+ return null;
71
+ };
72
+ return walk(name);
73
+ }
74
+
75
+ /** A live component state (src/chant-pr-events.ts) as a card's state. */
76
+ const LIVE_CARD = { waiting: "queued", planning: "queued", planned: "queued", running: "applying", applied: "applied", "already-applied": "already-applied", failed: "failed", blocked: "blocked", gated: "refused", "not-run": "not-started" };
77
+
26
78
  /** A plan's changes as Terraform sums them up, "+1 ~2 -1" (create, update, delete; a replace is both), or null for none. */
27
79
  export function countWords(counts) {
28
80
  if (!counts) return null;
@@ -59,28 +111,43 @@ function memberState(m, stage) {
59
111
  if (!m) return "unchanged";
60
112
  if (m.status === "applied") return "applied";
61
113
  if (m.status === "failed" || m.status === "plan-failed") return "failed";
62
- if (m.status === "blocked") return "not-started";
114
+ if (m.status === "blocked") return "blocked";
63
115
  if (stage.stage === "apply" && stage.status === "refused") return "refused";
64
116
  return "planned";
65
117
  }
66
118
 
67
- /** The words on a card for its state. */
68
- function cardWords(state, m, gate) {
119
+ /** The words on a card for its state; `x` carries the live facts (reason, blocker, wave). */
120
+ function cardWords(state, m, gate, x = {}) {
69
121
  if (state === "unchanged") return "unchanged";
70
- if (state === "failed") return m && m.status === "plan-failed" ? "plan failed" : "apply failed";
71
- if (state === "not-started") return "held back by an earlier wave";
122
+ if (state === "failed") {
123
+ const what = (m && m.status === "plan-failed") || x.failedIn === "plan" ? "plan failed" : "failed";
124
+ return x.reason ? `${what}: ${x.reason}` : what === "failed" ? (x.kiosk ? "failed" : "apply failed") : what;
125
+ }
126
+ if (state === "blocked") return x.blockedBy ? `blocked by ${x.blockedBy}` : "blocked by an earlier failure";
127
+ if (state === "not-started") return "did not run";
72
128
  if (state === "refused") return gate && gate.refusal ? `refused (${gate.refusal})` : "refused";
129
+ if (state === "queued") return x.wave ? `waiting for wave ${x.wave}` : "waiting";
130
+ if (state === "applying") return "applying";
131
+ if (state === "already-applied") return "applied before";
132
+ if (state === "applied-earlier") return "applied earlier";
73
133
  const c = countWords(m && m.counts);
74
134
  if (state === "applied") return c ? `applied ${c}` : "applied";
75
135
  return c ? `planned ${c}` : "planned, no changes";
76
136
  }
77
137
 
138
+ /** The middle form, when the full words don't fit: the state with its blocker, or the failure's first words. */
139
+ function cardMid(state, x) {
140
+ if (state === "failed" && x.reason) return `failed: ${shortReason(x.reason, 16)}`;
141
+ if (state === "blocked" && x.blockedBy) return `blocked by ${x.blockedBy}`;
142
+ return null;
143
+ }
144
+
78
145
  /**
79
146
  * The marks for /api/chant/pr's answer and the components graph's
80
147
  * `meta.chantPr` facets. Empty (no cards, no waves) before the pull
81
148
  * request's first plan: a card with no state draws as the view's neutral.
82
149
  */
83
- export function chantComponentMarks(answer, facets) {
150
+ export function chantComponentMarks(answer, facets, opts = {}) {
84
151
  const cards = {};
85
152
  const waves = {};
86
153
  const empty = { stage: null, cards, waves, source: null };
@@ -88,59 +155,261 @@ export function chantComponentMarks(answer, facets) {
88
155
  const stage = stageOf(answer);
89
156
  const gate = answer.gate || null;
90
157
  const running = answer.running || null;
158
+ // The apply's run events, when it uploaded them; else the plan's while it plans.
159
+ const liveRun = answer.live ? (answer.live.apply || (!answer.apply ? answer.live.plan : null)) : null;
160
+ const live = liveRun && liveRun.stage === "apply" ? liveRun : null;
161
+ const livePlan = liveRun && liveRun.stage === "plan" ? liveRun : null;
91
162
  const componentOf = (m) => m.component || (facets.members && facets.members[m.member]) || m.member;
163
+ const earlier = new Set(answer.live && answer.live.earlier && Array.isArray(answer.live.earlier.applied) ? answer.live.earlier.applied : []);
92
164
  const waveNumbers = [...new Set(Object.values(facets.waves))].filter(Number.isInteger).sort((a, b) => a - b);
93
- if (!stage && !running) return empty;
165
+ // A fix just opened has no plan yet, and still its cards say what they were: applied earlier, or their last state.
166
+ const carried = !!answer.lastStates || earlier.size > 0;
167
+ if (!stage && !running && !liveRun && !carried) return empty;
168
+ // Without the apply's events, its job running is all a box knows.
169
+ const progressUnknown = !!(running && running.stage === "apply" && !live);
170
+ // Events are coming (the stage uploads them) but none landed yet: the boxes say it is starting.
171
+ const starting = progressUnknown && !!answer.eventsExpected;
172
+ // The issue number is for Inspect: never on a kiosk, never while events are on their way.
173
+ const issueNote = progressUnknown && !starting && !opts.kiosk;
174
+ // The wave the apply starts with: the first one this change touches.
175
+ const startingWave = starting ? waveNumbers.find((n) => Object.entries(facets.waves).some(([c, w]) => w === n && (!stage || memberState(stage.members.find((x) => componentOf(x) === c) || null, stage) !== "unchanged"))) ?? null : null;
94
176
 
95
- const facts = (state, m) => {
177
+ const facts = (state, m, x) => {
96
178
  const out = [];
97
179
  if (m && countSentence(m.counts)) out.push(countSentence(m.counts));
98
180
  if (stage && stage.digest) out.push(`plan ${short8(stage.digest)}`);
99
- if (m && m.error) out.push(m.error);
181
+ if (x.error) out.push(x.error);
182
+ else if (m && m.error) out.push(m.error);
183
+ if (state === "blocked") out.push(x.blockedBy ? `did not run: ${x.blockedBy} failed, and this depends on it` : "did not run: something it depends on failed");
100
184
  if (m && m.inputsMoved && m.inputsMoved.length) out.push(`inputs from ${m.inputsMoved.join(", ")} moved; the next run plans it again`);
101
- if (state === "planned" && running && running.stage === "apply") out.push(`per-component progress: ${PROGRESS_ISSUE}`);
185
+ if (state === "planned" && issueNote) out.push(`per-component progress: ${PROGRESS_ISSUE}`);
102
186
  return out;
103
187
  };
104
- if (stage) {
105
- for (const [c, n] of Object.entries(facets.waves)) {
106
- const m = stage.members.find((x) => componentOf(x) === c) || null;
107
- const state = memberState(m, stage);
108
- cards[c] = { root: c, wave: n, state, tone: toneOf(state), words: cardWords(state, m, gate), short: CHANT_SHORT[state] || state, facts: facts(state, m) };
109
- }
188
+ const liveOf = (c) => (live && live.components ? live.components[c] : null);
189
+ const memberOf = (c) => (stage ? stage.members.find((x) => componentOf(x) === c) || null : null);
190
+ // A component's state first, then its blocker: a blocker is read from the others' states.
191
+ const states = {};
192
+ for (const [c, n] of Object.entries(facets.waves)) {
193
+ const m = memberOf(c);
194
+ const l = liveOf(c);
195
+ const lp = livePlan && livePlan.components ? livePlan.components[c] : null;
196
+ let state;
197
+ // A run that never reached its component (a refusal at the gate) says so through its summary, once it is printed.
198
+ if (l && l.state === "not-run" && m && stage && stage.stage === "apply") state = memberState(m, stage);
199
+ else if (l) state = LIVE_CARD[l.state] || "queued";
200
+ else if (lp && !stage) state = lp.state === "failed" ? "failed" : lp.state === "planned" ? "planned" : "queued";
201
+ else if (live && !m) state = "unchanged";
202
+ else if (!stage) state = "unchanged";
203
+ else state = memberState(m, stage);
204
+ // A finished apply's summary over a live read that has not caught up (the last upload is after the summary).
205
+ if (l && m && answer.apply && (m.status === "applied" || m.status === "failed" || m.status === "blocked") && (state === "queued" || state === "applying")) state = memberState(m, stage);
206
+ // Not in this run, and an earlier apply of the cycle applied it: it landed then.
207
+ if (state === "unchanged" && earlier.has(c)) state = "applied-earlier";
208
+ // A new pull request with no plan yet (the fix just opened): what it will touch keeps its last state.
209
+ if ((state === "unchanged" || state === "queued") && answer.lastStates && answer.lastStates[c]) state = answer.lastStates[c];
210
+ states[c] = { state, wave: n, m, l, blockedBy: l && l.blockedBy ? l.blockedBy : null };
211
+ }
212
+ const stateOf = (c) => states[c] || null;
213
+ for (const [c, s] of Object.entries(states)) {
214
+ if (s.state === "blocked" && !s.blockedBy) s.blockedBy = blockerOf(c, facets.needs, stateOf);
215
+ }
216
+ for (const [c, s] of Object.entries(states)) {
217
+ if (!stage && !liveRun && !carried) continue;
218
+ const error = (s.l && s.l.error) || (s.m && s.m.error) || (livePlan && livePlan.components && livePlan.components[c] && livePlan.components[c].error) || null;
219
+ const x = { reason: shortReason(error), error, blockedBy: s.blockedBy, failedIn: s.l ? s.l.failedIn : null, wave: s.wave, kiosk: !!opts.kiosk };
220
+ const words = cardWords(s.state, s.m, gate, x);
221
+ const mid = cardMid(s.state, x);
222
+ cards[c] = { root: c, wave: s.wave, state: s.state, tone: toneOf(s.state), words, ...(mid && mid !== words ? { mid } : {}), short: CHANT_SHORT[s.state] || s.state, facts: facts(s.state, s.m, x), ...(s.blockedBy ? { blockedBy: s.blockedBy } : {}), ...(x.reason ? { reason: x.reason } : {}) };
110
223
  }
224
+ // behold#631: the cards the pipeline is about to work on while it is between events: all of a pull request's
225
+ // cards before its plan, wave 1's before the apply's first wave. They pulse so the stage never looks frozen.
226
+ if (running && running.stage === "plan" && !stage) {
227
+ for (const card of Object.values(cards)) if (card.state !== "applied-earlier" && card.state !== "unchanged") card.working = true;
228
+ if (!Object.values(cards).some((c) => c.working)) for (const [c, n] of Object.entries(facets.waves)) if (!cards[c]) cards[c] = { root: c, wave: n, state: "unchanged", tone: "idle", words: "", short: "", facts: [], working: true };
229
+ }
230
+ const firstWave = waveNumbers.find((n) => Object.values(cards).some((c) => c.wave === n && c.state !== "unchanged" && c.state !== "applied-earlier"));
231
+ if (running && running.stage === "apply" && !live && firstWave !== undefined) for (const card of Object.values(cards)) if (card.wave === firstWave && card.state !== "applied-earlier") card.working = true;
111
232
  for (const n of waveNumbers) {
112
233
  const members = Object.entries(facets.waves).filter(([, w]) => w === n).map(([c]) => cards[c]).filter(Boolean);
113
234
  const touched = members.filter((m) => m.state !== "unchanged");
235
+ const earlierOnly = touched.length > 0 && touched.every((m) => m.state === "applied-earlier");
236
+ // Live: the graph's wave by its components' names, since run-start's
237
+ // wave numbers are re-layered on a resume and are not the boxes'.
238
+ const inRun = live ? members.filter((m) => states[m.root] && states[m.root].l) : [];
114
239
  let state = null;
115
- if (touched.length) {
240
+ let status = null;
241
+ if (inRun.length) {
242
+ const count = (...st) => inRun.filter((m) => st.includes(m.state)).length;
243
+ const cnt = { applied: count("applied", "already-applied"), failed: count("failed"), blocked: count("blocked"), applying: count("applying"), queued: count("queued"), gated: count("refused"), notRun: count("not-started") };
244
+ const blockedBy = [...new Set(inRun.filter((m) => m.state === "blocked" && m.blockedBy).map((m) => m.blockedBy))];
245
+ const blockedWords = cnt.blocked ? `${cnt.blocked} blocked${blockedBy.length === 1 ? ` by ${blockedBy[0]}` : ""}` : null;
246
+ // Settled in this run (a resume's already-applied did not run in it).
247
+ const fresh = count("applied", "failed", "blocked");
248
+ if (cnt.applying || (cnt.queued && fresh > 0 && !live.settled)) {
249
+ state = "applying";
250
+ status = { status: "running", words: "applying", tone: "busy" };
251
+ } else if (cnt.queued === inRun.length || (cnt.queued && !live.settled)) {
252
+ state = "planned";
253
+ // "queued", not "waiting": a box's short form of waiting is "held", which reads as a failure word.
254
+ status = { status: "queued", words: "waiting", tone: "idle" };
255
+ } else if (cnt.failed) {
256
+ state = "failed";
257
+ status = { status: "failed", words: `${cnt.failed} failed${cnt.applied ? `, ${cnt.applied} applied` : ""}`, tone: "bad" };
258
+ } else if (cnt.gated) {
259
+ state = "waiting";
260
+ status = { status: "waiting", words: "held at a gate", tone: "wait" };
261
+ } else if (cnt.blocked === inRun.length) {
262
+ state = "blocked";
263
+ status = { status: "blocked", words: blockedBy.length === 1 ? `blocked by ${blockedBy[0]}` : "blocked", tone: "wait" };
264
+ } else if (cnt.applied) {
265
+ state = blockedWords ? "blocked" : "applied";
266
+ status = { status: "done", words: blockedWords ? `${cnt.applied} applied, ${blockedWords}` : "done", tone: "ok" };
267
+ } else {
268
+ state = "not-started";
269
+ status = { status: "not-run", words: "did not run", tone: "idle" };
270
+ }
271
+ } else if (touched.length) {
116
272
  if (touched.some((m) => m.state === "failed")) state = "failed";
117
273
  else if (touched.some((m) => m.state === "refused")) state = "refused";
118
- else if (touched.every((m) => m.state === "applied")) state = "applied";
119
- else if (touched.some((m) => m.state === "not-started")) state = "not-started";
274
+ else if (touched.every((m) => m.state === "applied" || m.state === "already-applied" || m.state === "applied-earlier")) state = "applied";
275
+ else if (touched.every((m) => m.state === "blocked" || m.state === "not-started")) state = "not-started";
276
+ else if (touched.some((m) => m.state === "blocked")) state = "blocked";
120
277
  else state = gate && gate.state === "waiting" && !answer.apply ? "waiting" : "planned";
121
278
  }
122
- const status = running ? { status: running.status, words: `${running.job} ${running.status}`, tone: running.status === "running" ? "busy" : "idle", run: running.run } : null;
279
+ if (!status && earlierOnly) status = { status: "done", words: "applied earlier", tone: "ok" };
280
+ // A kiosk says the plan job in words a passer-by reads ("planning", not "plan queued").
281
+ const jobWords = opts.kiosk && running && running.stage === "plan" ? (running.status === "running" ? "planning" : "waiting to plan") : running ? `${running.job} ${running.status}` : null;
282
+ if (!status && running && !(live && live.settled)) status = { status: running.status, words: jobWords, tone: running.status === "running" ? "busy" : "idle", run: running.run };
283
+ if (status && running && status.run === undefined) status.run = running.run;
123
284
  const f = [];
124
- if (state === "waiting" && gate && gate.digest) f.push(`approve plan ${hexOf(gate.digest)}`);
125
- else if (state === "planned" && gate && gate.state === "approved") f.push(gate.by ? `approved by ${gate.by.replace(/^.*:/, "")}` : "approved");
126
- if (state === "applied" && stage && stage.digest) f.push(`applied plan ${short8(stage.digest)}`);
127
- // While pr-apply runs, chant has said nothing per wave yet: that first, with the issue that asks for it.
128
- if (running && running.stage === "apply" && touched.length && state !== "applied") f.unshift(`per-wave progress: ${PROGRESS_ISSUE}`);
129
- waves[n] = { number: n, job: running ? running.job : null, state, tone: state ? toneOf(state) : status ? status.tone : "idle", status, title: status ? `wave-${n} · ${status.words}` : `wave-${n}`, facts: f };
285
+ // A kiosk names the plan by its short id; Inspect keeps the whole digest.
286
+ // The kiosk's approval card names the plan; its boxes don't repeat it. Inspect keeps the whole digest on them.
287
+ if (state === "waiting" && gate && gate.digest && !inRun.length && !opts.kiosk) f.push(`approve plan ${hexOf(gate.digest)}`);
288
+ // On a kiosk the approval card says who approved which plan; the boxes don't repeat it. Inspect keeps both.
289
+ else if (state === "planned" && gate && gate.state === "approved" && !opts.kiosk) f.push(gate.by ? `approved by ${gate.by.replace(/^.*:/, "")}` : "approved");
290
+ // A box whose cards all landed in an earlier apply did not apply this run's plan.
291
+ if (state === "applied" && stage && stage.digest && !earlierOnly && !opts.kiosk) f.push(`applied plan ${short8(stage.digest)}`);
292
+ if (state === "failed") for (const m of members.filter((x) => x.state === "failed")) f.push(m.reason ? `${m.root} failed: ${m.reason}` : `${m.root} failed`);
293
+ // While pr-apply runs without events, chant has said nothing per wave yet: that first, with the issue that asks for it.
294
+ if (issueNote && touched.length && state !== "applied") f.unshift(`per-wave progress: ${PROGRESS_ISSUE}`);
295
+ // Only the wave the apply is starting says so; the ones after it wait their turn.
296
+ if (starting && touched.length && state !== "applied") {
297
+ status = n === startingWave ? { status: "running", words: "starting…", tone: "busy", ...(running ? { run: running.run } : {}) } : { status: "queued", words: "waiting", tone: "idle", ...(running ? { run: running.run } : {}) };
298
+ }
299
+ const job = running ? running.job : null;
300
+ // A failed wave's first fact, shorter, for a narrow box: the reason cut, then the component alone.
301
+ const firstFailed = state === "failed" ? members.find((x) => x.state === "failed") : null;
302
+ const factShort = firstFailed && firstFailed.reason ? [`${firstFailed.root} failed: ${shortReason(firstFailed.reason, 36)}`, `${firstFailed.root} failed`] : [];
303
+ // On a kiosk a wave waiting on the approval is outlined in the plan's tone: the warning tone is "blocked"'s alone.
304
+ const waveTone = state === "waiting" && opts.kiosk ? "plan" : state ? toneOf(state) : status ? status.tone : "idle";
305
+ waves[n] = { number: n, job, state, tone: waveTone, status, title: status ? `wave-${n} · ${status.words}` : `wave-${n}`, facts: f, ...(factShort.length ? { factShort } : {}) };
130
306
  }
131
- return { stage: stage ? stage.stage : null, cards, waves, source: sourceWords(answer) };
307
+ const reach = reachOf(cards, facets, { stage, live, answer, earlier });
308
+ return { stage: live ? "apply" : stage ? stage.stage : livePlan ? "plan" : null, cards, waves, source: sourceWords(answer), ...(reach ? { reach } : {}), ...(opts.kiosk ? { statusRight: true, cardFont: 15, waveFont: 18 } : {}) };
132
309
  }
133
310
 
134
311
  /** Where the marks came from, for each tooltip. */
135
312
  export function sourceWords(answer) {
136
313
  const from = (s) => (s.from === "report" ? `chant's pr-${s.stage} report` : `chant pr-${s.stage}'s summary in run #${s.run}`);
137
314
  const parts = [answer.plan, answer.apply].filter(Boolean).map(from);
315
+ for (const l of answer.live ? [answer.live.plan, answer.live.apply] : []) if (l) parts.push(`chant pr-${l.stage}'s run events`);
138
316
  if (answer.gate && answer.gate.from === "ledger") parts.push("the gate's ledger");
139
317
  return parts.length ? `(from ${parts.join(", ")})` : null;
140
318
  }
141
319
 
142
320
  /** The steps chant's gate shows its panel on: the plan waits, it is approved, the merge applied it. */
143
- export const CHANT_PANEL_STEPS = ["waiting", "approved", "merged", "applying", "applied"];
321
+ export const CHANT_PANEL_STEPS = ["waiting", "approved", "merged", "applying", "applied", "failed"];
322
+
323
+ /**
324
+ * The apply's waves as its run events tell them, for the kiosk's tips: each
325
+ * wave's state, what failed in it (with the reason) and what it left
326
+ * blocked. Null without the apply's events.
327
+ */
328
+ export function chantLiveWaves(answer) {
329
+ const live = answer && answer.live && answer.live.apply;
330
+ if (!live || !Array.isArray(live.waves)) return null;
331
+ const comps = live.components || {};
332
+ return graphWaves(live).map((w) => {
333
+ const of = (st) => w.components.map((c) => comps[c]).filter((c) => c && c.state === st);
334
+ const blocked = of("blocked");
335
+ return {
336
+ wave: w.wave,
337
+ state: w.state,
338
+ components: [...w.components],
339
+ applied: of("applied").length + of("already-applied").length,
340
+ appliedNames: [...of("applied"), ...of("already-applied")].map((c) => c.component),
341
+ failed: of("failed").map((c) => ({
342
+ component: c.component,
343
+ reason: shortReason(c.error),
344
+ ...(c.error ? { error: c.error } : {}),
345
+ // What its failure blocked, wherever in the run: the cards a viewer must still see.
346
+ blocks: Object.values(comps).filter((x) => x.state === "blocked" && x.blockedBy === c.component).map((x) => x.component),
347
+ })),
348
+ blocked: blocked.length,
349
+ blockedBy: [...new Set(blocked.map((c) => c.blockedBy).filter(Boolean))],
350
+ };
351
+ });
352
+ }
353
+
354
+ /**
355
+ * The apply's waves numbered as the components graph numbers them, when
356
+ * every component carries its `graphWave`: a fix's or a resume's run-start
357
+ * re-layers what is left (its wave 1 may be the graph's wave 2), and a
358
+ * viewer reads the boxes' numbers. Else run-start's waves as they are.
359
+ */
360
+ export function graphWaves(live) {
361
+ const comps = live.components || {};
362
+ const runWaves = Array.isArray(live.waves) ? live.waves : [];
363
+ const names = runWaves.flatMap((w) => w.components);
364
+ if (!names.length || !names.every((c) => comps[c] && Number.isInteger(comps[c].graphWave))) return runWaves;
365
+ const by = new Map();
366
+ for (const w of runWaves) {
367
+ for (const c of w.components) {
368
+ const g = comps[c].graphWave;
369
+ if (!by.has(g)) by.set(g, { wave: g, components: [], states: [] });
370
+ by.get(g).components.push(c);
371
+ by.get(g).states.push(w.state);
372
+ }
373
+ }
374
+ return [...by.values()]
375
+ .sort((a, b) => a.wave - b.wave)
376
+ .map((g) => {
377
+ const counts = { applied: 0, failed: 0, blocked: 0, alreadyApplied: 0, running: 0, waiting: 0 };
378
+ for (const c of g.components) {
379
+ const s = comps[c].state;
380
+ if (s === "applied") counts.applied++;
381
+ else if (s === "failed") counts.failed++;
382
+ else if (s === "blocked") counts.blocked++;
383
+ else if (s === "already-applied") counts.alreadyApplied++;
384
+ else if (s === "running") counts.running++;
385
+ else if (s !== "gated" && s !== "not-run") counts.waiting++;
386
+ }
387
+ // One run wave maps to one graph wave in a fan-out; where they mix, the busiest state leads.
388
+ const rank = ["running", "failed", "applied", "gated", "blocked", "not-run", "waiting"];
389
+ const state = [...g.states].sort((a, b) => rank.indexOf(a) - rank.indexOf(b))[0] || "waiting";
390
+ return { wave: g.wave, state: counts.running ? "running" : state, components: g.components, counts };
391
+ });
392
+ }
393
+
394
+ /** "3 applied, 1 failed, 6 blocked" for a partial apply, from the events or the summary. */
395
+ function partialWords(answer) {
396
+ const live = answer.live && answer.live.apply;
397
+ const n = { applied: 0, failed: 0, blocked: 0 };
398
+ if (live && live.components) {
399
+ for (const c of Object.values(live.components)) {
400
+ if (c.state === "applied" || c.state === "already-applied") n.applied++;
401
+ else if (c.state === "failed") n.failed++;
402
+ else if (c.state === "blocked") n.blocked++;
403
+ }
404
+ } else if (answer.apply) {
405
+ for (const m of answer.apply.members) {
406
+ if (m.status === "applied") n.applied++;
407
+ else if (m.status === "failed") n.failed++;
408
+ else if (m.status === "blocked") n.blocked++;
409
+ }
410
+ }
411
+ return [n.applied ? `${n.applied} applied` : null, n.failed ? `${n.failed} failed` : null, n.blocked ? `${n.blocked} blocked` : null].filter(Boolean).join(", ");
412
+ }
144
413
 
145
414
  /**
146
415
  * "How this hold ends" for chant's gate: one gate per pull request (op
@@ -149,13 +418,15 @@ export const CHANT_PANEL_STEPS = ["waiting", "approved", "merged", "applying", "
149
418
  * approve line pr-plan printed), the ledger's approval of that digest, and
150
419
  * pr-apply applying under it. Null when the answer has no gate.
151
420
  */
152
- export function chantApprovalModel({ demo, answer, sealCheck = null }) {
421
+ export function chantApprovalModel({ demo, answer, sealCheck = null, kiosk = false }) {
153
422
  if (!demo || !CHANT_PANEL_STEPS.includes(demo.step) || !answer || !answer.gate || !answer.plan) return null;
423
+ // An answer read for another pull request of the cycle (the first, while the fix's is still to come) is not this one's.
424
+ if (Number.isInteger(demo.pull) && Number.isInteger(answer.pull) && demo.pull !== answer.pull) return null;
154
425
  const g = answer.gate;
155
426
  const plan = answer.plan;
156
427
  const apply = answer.apply;
157
428
  const rows = [];
158
- rows.push({ id: "requested", label: "Requested", state: "lit", text: `Pull request #${answer.pull} asks a reviewer to approve its plan.`, runId: plan.run || null, ...(g.approveCommand && g.state === "waiting" ? { command: g.approveCommand } : {}) });
429
+ rows.push({ id: "requested", label: "Requested", state: "lit", text: `Pull request #${answer.pull} asks a reviewer to approve its plan.`, runId: plan.run || null, ...(g.approveCommand && g.state === "waiting" && !kiosk ? { command: g.approveCommand } : {}) });
159
430
  if (g.state === "approved") {
160
431
  const signed = g.seal === "sealed";
161
432
  rows.push({
@@ -169,10 +440,117 @@ export function chantApprovalModel({ demo, answer, sealCheck = null }) {
169
440
  });
170
441
  } else if (g.state === "changed" || g.state === "refused") {
171
442
  rows.push({ id: "approved", label: "Approved", state: "dim", text: g.state === "changed" ? "The plan moved after review and needs a fresh approval." : `The apply refused (${g.refusal || "no approval"}).` });
172
- } else rows.push({ id: "approved", label: "Approved", state: "dim", text: "Next, a reviewer approves the digest." });
443
+ } else rows.push({ id: "approved", label: "Approved", state: "dim", text: kiosk ? "A reviewer approves this exact plan." : "Next, a reviewer approves the digest." });
444
+ const live = answer.live && answer.live.apply;
445
+ const failedApply = (apply && apply.status === "failed") || (live && live.settled && live.status === "failed");
173
446
  if (apply && apply.status === "applied") rows.push({ id: "spent", label: "Applied", state: "lit", text: "The merge planned again and applied this digest.", runId: apply.run || null });
174
- else if (answer.running && answer.running.stage === "apply") rows.push({ id: "spent", label: "Applied", state: "going", text: "chant is applying this digest on merge now.", runId: answer.running.run });
175
- else rows.push({ id: "spent", label: "Applied", state: "dim", text: "On merge, chant applies the plan when it matches." });
447
+ else if (failedApply) {
448
+ const words = partialWords(answer);
449
+ rows.push({ id: "spent", label: "Applied", state: "dim", text: `The merge applied part of this digest${words ? `: ${words}` : ""}. The rest waits for a fix.`, runId: (apply && apply.run) || (answer.running && answer.running.run) || null });
450
+ } else if (answer.running && answer.running.stage === "apply") {
451
+ const gw = live ? graphWaves(live) : [];
452
+ const at = gw.filter((w) => w.state === "running").map((w) => w.wave);
453
+ const of = gw.length;
454
+ rows.push({ id: "spent", label: "Applied", state: "going", text: at.length && of ? `chant is applying this digest on merge now: wave ${at.join(", ")}.` : "chant is applying this digest on merge now.", runId: answer.running.run });
455
+ } else rows.push({ id: "spent", label: "Applied", state: "dim", text: kiosk ? "The merge applies it only if it still matches." : "On merge, chant applies the plan when it matches." });
176
456
  const run = apply ? apply.run : answer.running ? answer.running.run : plan.run;
177
- return { wave: null, digest: g.digest, run: run || null, rows };
457
+ const chantWaves = chantLiveWaves(answer);
458
+ // A kiosk names no run numbers: a passer-by can't place them. Inspect keeps them.
459
+ if (kiosk) for (const r of rows) r.runId = null;
460
+ return { wave: null, digest: g.digest, run: kiosk ? null : run || null, rows, ...(kiosk ? { kiosk: true } : {}), ...(chantWaves ? { chantWaves } : {}) };
461
+ }
462
+
463
+ /**
464
+ * behold#631: the result card at a chant repo's applied step, the one
465
+ * terragucci's scenarios get (web/kiosk-watch.js resultModel), from the
466
+ * apply's run events: what this run applied, what earlier applies of the
467
+ * cycle did, and each wave's count. Null before the applied step or without
468
+ * the apply's events.
469
+ */
470
+ export function chantResultModel(demo, answer) {
471
+ if (!demo || demo.step !== "applied") return null;
472
+ const live = answer && answer.live && answer.live.apply;
473
+ if (!live || !live.components) return null;
474
+ const comps = Object.values(live.components);
475
+ const now = comps.filter((c) => c.state === "applied").length;
476
+ const earlierList = answer.live.earlier && Array.isArray(answer.live.earlier.applied) ? answer.live.earlier.applied : [];
477
+ const before = new Set([...earlierList, ...comps.filter((c) => c.state === "already-applied").map((c) => c.component)]);
478
+ for (const c of comps) if (c.state === "applied") before.delete(c.component);
479
+ const waves = graphWaves(live);
480
+ const counts = [`${now} applied this run`];
481
+ if (before.size) counts.push(`${before.size} applied earlier`);
482
+ // A later run of the cycle (the fix) ran only what was left: say so, or "2 waves" reads as a miscount of the three.
483
+ const wavesWords = `${waves.length} ${waves.length === 1 ? "wave" : "waves"}`;
484
+ counts.push(earlierList.length ? `the fix ran ${wavesWords}` : wavesWords);
485
+ const failed = comps.filter((c) => c.state === "failed").length;
486
+ const blocked = comps.filter((c) => c.state === "blocked").length;
487
+ if (failed) counts.push(`${failed} failed`);
488
+ if (blocked) counts.push(`${blocked} blocked`);
489
+ return {
490
+ head: "Applied",
491
+ line: typeof demo.label === "string" ? demo.label : "",
492
+ counts,
493
+ waves: waves.map((w) => {
494
+ const n = w.counts || {};
495
+ const parts = [n.applied ? `${n.applied} applied` : null, n.alreadyApplied ? `${n.alreadyApplied} applied before` : null, n.failed ? `${n.failed} failed` : null, n.blocked ? `${n.blocked} blocked` : null].filter(Boolean);
496
+ return { wave: w.wave, words: parts.length ? parts.join(", ") : null };
497
+ }),
498
+ };
499
+ }
500
+
501
+ /** Everything downstream of `roots` along the graph's `needs` (consumer -> producer), transitively, within `allowed` when given. */
502
+ export function downstreamOf(roots, needs, allowed = null) {
503
+ const users = new Map();
504
+ for (const [c, deps] of Object.entries(needs || {})) for (const d of deps) (users.get(d) || users.set(d, []).get(d)).push(c);
505
+ const out = new Set();
506
+ const walk = (n) => {
507
+ for (const u of users.get(n) || []) {
508
+ if (out.has(u) || (allowed && !allowed.has(u))) continue;
509
+ out.add(u);
510
+ walk(u);
511
+ }
512
+ };
513
+ for (const r of roots) walk(r);
514
+ return out;
515
+ }
516
+
517
+ /**
518
+ * behold#631: what a change reaches on the components graph, drawn as a soft
519
+ * wedge from its root(s) up over everything downstream (app.js
520
+ * drawReach). `{ key, roots, covered, tone, label }`, or null:
521
+ *
522
+ * - while a plan stands and its apply has not started a wave: the plan's
523
+ * roots (members with no dependency inside the plan), covering the plan;
524
+ * "reaches 15 stacks", or for a later pull request of the cycle (the fix)
525
+ * "the fix reaches 7";
526
+ * - once a component failed (as the paced graph shows it): the failed
527
+ * component(s), covering what they block, "6 apps blocked";
528
+ * - nothing once the apply runs, has applied, or at the reset.
529
+ *
530
+ * The viewer's words say "reaches" and "blocked", the cards' own word.
531
+ */
532
+ export function reachOf(cards, facets, { stage, live, answer, earlier = new Set() } = {}) {
533
+ const needs = facets && facets.needs ? facets.needs : {};
534
+ const failed = Object.values(cards).filter((c) => c.state === "failed").map((c) => c.root);
535
+ // The failure's reach lasts until the fix opens (the fix's cards then carry their last state, `lastStates`).
536
+ if (failed.length && !(answer && answer.lastStates)) {
537
+ const blocked = Object.values(cards).filter((c) => c.state === "blocked");
538
+ const held = new Set(blocked.filter((c) => !c.blockedBy || failed.includes(c.blockedBy)).map((c) => c.root));
539
+ for (const d of downstreamOf(failed, needs)) if (cards[d] && cards[d].state === "blocked") held.add(d);
540
+ if (!held.size) return null;
541
+ const names = [...held];
542
+ const noun = names.every((n) => /^app-/.test(n)) ? (names.length === 1 ? "app" : "apps") : names.length === 1 ? "stack" : "stacks";
543
+ // One word for one state: the cards say "blocked", so does this.
544
+ return { key: `failed|${failed.sort().join(",")}|${names.length}`, roots: failed, covered: names.sort(), tone: "bad", label: `${names.length} ${noun} blocked` };
545
+ }
546
+ if (!stage || stage.stage !== "plan" || !answer || answer.apply) return null;
547
+ // Once the apply has started a wave, the applying cards carry the story.
548
+ if (live && Object.values(live.components || {}).some((c) => c.state !== "waiting" && c.state !== "planned" && c.state !== "planning")) return null;
549
+ const members = new Set(Object.values(cards).filter((c) => c.state === "planned" || c.state === "queued").map((c) => c.root));
550
+ if (!members.size) return null;
551
+ const roots = [...members].filter((c) => !(needs[c] || []).some((d) => members.has(d))).sort();
552
+ if (!roots.length) return null;
553
+ const covered = [...new Set([...roots, ...downstreamOf(roots, needs, members)])].sort();
554
+ const fix = earlier.size > 0;
555
+ return { key: `plan|${roots.join(",")}|${covered.length}|${fix ? "fix" : "change"}`, roots, covered, tone: "plan", label: fix ? `the fix reaches ${covered.length}` : `reaches ${covered.length} ${covered.length === 1 ? "stack" : "stacks"}` };
178
556
  }