paseo-plugin-genui 0.0.0-stage → 0.1.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 (375) hide show
  1. package/LICENSE +202 -0
  2. package/OVERVIEW.md +21 -0
  3. package/README.md +77 -2
  4. package/assets/fonts/Excalifont-OFL.txt +109 -0
  5. package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +1 -0
  8. package/assets/screenshot-chat.png +0 -0
  9. package/assets/screenshot-gallery.png +0 -0
  10. package/assets/screenshot-playground.png +0 -0
  11. package/client/components/content.tsx +468 -0
  12. package/client/components/data.tsx +967 -0
  13. package/client/components/dev.tsx +998 -0
  14. package/client/components/diagram.tsx +513 -0
  15. package/client/components/inputs.tsx +994 -0
  16. package/client/components/layout.tsx +239 -0
  17. package/client/components/media.tsx +428 -0
  18. package/client/components/project.tsx +641 -0
  19. package/client/components/widgets.tsx +836 -0
  20. package/client/gallery.tsx +449 -0
  21. package/client/live-ui.tsx +525 -0
  22. package/client/loaders.ts +342 -0
  23. package/client/markdown.tsx +172 -0
  24. package/client/node-renderer.tsx +149 -0
  25. package/client/render.tsx +186 -0
  26. package/client/runtime.ts +103 -0
  27. package/client/settings.tsx +57 -0
  28. package/client/web.ts +186 -0
  29. package/gallery/Acceptance-baseline.ui +1 -0
  30. package/gallery/Acceptance-checklist.ui +14 -0
  31. package/gallery/Acceptance-evidence.ui +9 -0
  32. package/gallery/Acceptance.ui +1 -0
  33. package/gallery/Badge.ui +3 -0
  34. package/gallery/Benchmark-context.ui +8 -0
  35. package/gallery/Benchmark-hyperfine.ui +1 -0
  36. package/gallery/Benchmark-runs-list.ui +9 -0
  37. package/gallery/Benchmark-runs.ui +1 -0
  38. package/gallery/Benchmark-vitest.ui +1 -0
  39. package/gallery/Benchmark.ui +9 -0
  40. package/gallery/Board-bb-tasks.ui +1 -0
  41. package/gallery/Board-gh-issues.ui +1 -0
  42. package/gallery/Board-gh-prs.ui +1 -0
  43. package/gallery/Board.ui +12 -0
  44. package/gallery/Button-destructive.ui +4 -0
  45. package/gallery/Button.ui +5 -0
  46. package/gallery/Calendar.ui +15 -0
  47. package/gallery/CalendarHeatmap-short.ui +11 -0
  48. package/gallery/CalendarHeatmap.ui +4 -0
  49. package/gallery/Callout.ui +4 -0
  50. package/gallery/Card.ui +4 -0
  51. package/gallery/Carousel.ui +8 -0
  52. package/gallery/Changes-paths.ui +1 -0
  53. package/gallery/Changes-uncommitted.ui +1 -0
  54. package/gallery/Changes.ui +1 -0
  55. package/gallery/Chart-area.ui +3 -0
  56. package/gallery/Chart-funnel.ui +3 -0
  57. package/gallery/Chart-heatmap.ui +11 -0
  58. package/gallery/Chart-pie.ui +3 -0
  59. package/gallery/Chart-radar.ui +3 -0
  60. package/gallery/Chart-scatter.ui +4 -0
  61. package/gallery/Chart-stacked.ui +5 -0
  62. package/gallery/Chart.ui +3 -0
  63. package/gallery/Checklist.ui +6 -0
  64. package/gallery/Choice.ui +8 -0
  65. package/gallery/Code.ui +3 -0
  66. package/gallery/Commits-limit.ui +1 -0
  67. package/gallery/Commits.ui +1 -0
  68. package/gallery/Compare-conditions.ui +4 -0
  69. package/gallery/Compare-three.ui +7 -0
  70. package/gallery/Compare.ui +4 -0
  71. package/gallery/Coverage-go.ui +1 -0
  72. package/gallery/Coverage-lcov.ui +1 -0
  73. package/gallery/Coverage-python.ui +1 -0
  74. package/gallery/Coverage.ui +1 -0
  75. package/gallery/DateInput.ui +5 -0
  76. package/gallery/Decision-percent.ui +10 -0
  77. package/gallery/Decision.ui +12 -0
  78. package/gallery/Dependencies-audit.ui +1 -0
  79. package/gallery/Dependencies-both.ui +1 -0
  80. package/gallery/Dependencies-list.ui +7 -0
  81. package/gallery/Dependencies-pip.ui +1 -0
  82. package/gallery/Dependencies-pnpm.ui +1 -0
  83. package/gallery/Dependencies.ui +1 -0
  84. package/gallery/Details.ui +4 -0
  85. package/gallery/Diagram-flow.ui +16 -0
  86. package/gallery/Diagram-sequence.ui +15 -0
  87. package/gallery/Diagram.ui +16 -0
  88. package/gallery/Diff.ui +3 -0
  89. package/gallery/Divider.ui +5 -0
  90. package/gallery/EditableTable.ui +9 -0
  91. package/gallery/Entity-list.ui +6 -0
  92. package/gallery/Entity.ui +8 -0
  93. package/gallery/Environments.ui +1 -0
  94. package/gallery/Estimate-points.ui +6 -0
  95. package/gallery/Estimate.ui +8 -0
  96. package/gallery/File.ui +4 -0
  97. package/gallery/Findings-biome.ui +1 -0
  98. package/gallery/Findings-eslint.ui +1 -0
  99. package/gallery/Findings-oxlint.ui +1 -0
  100. package/gallery/Findings-review.ui +1 -0
  101. package/gallery/Findings-ruff.ui +1 -0
  102. package/gallery/Findings-sarif.ui +1 -0
  103. package/gallery/Findings-ship.ui +11 -0
  104. package/gallery/Findings-tsc.ui +1 -0
  105. package/gallery/Findings.ui +7 -0
  106. package/gallery/Flashcards.ui +3 -0
  107. package/gallery/FollowUps.ui +4 -0
  108. package/gallery/Form.ui +8 -0
  109. package/gallery/Gallery-links.ui +8 -0
  110. package/gallery/Gallery-strip.ui +14 -0
  111. package/gallery/Gallery.ui +10 -0
  112. package/gallery/Gauge.ui +7 -0
  113. package/gallery/Grid.ui +8 -0
  114. package/gallery/Hotspots-photo.ui +8 -0
  115. package/gallery/Hotspots.ui +11 -0
  116. package/gallery/Icon.ui +5 -0
  117. package/gallery/Image.ui +4 -0
  118. package/gallery/ImageSearch-aspects.ui +6 -0
  119. package/gallery/ImageSearch.ui +4 -0
  120. package/gallery/Input.ui +5 -0
  121. package/gallery/Json-api.ui +1 -0
  122. package/gallery/Json.ui +8 -0
  123. package/gallery/KeyValue.ui +3 -0
  124. package/gallery/List-dots.ui +8 -0
  125. package/gallery/List.ui +8 -0
  126. package/gallery/ListEditor.ui +5 -0
  127. package/gallery/Logs-jsonl.ui +1 -0
  128. package/gallery/Logs-lines.ui +11 -0
  129. package/gallery/Logs-python.ui +1 -0
  130. package/gallery/Logs.ui +1 -0
  131. package/gallery/Map-city.ui +8 -0
  132. package/gallery/Map.ui +8 -0
  133. package/gallery/Metric.ui +3 -0
  134. package/gallery/OptionCards-multi.ui +10 -0
  135. package/gallery/OptionCards.ui +10 -0
  136. package/gallery/Pipeline-checks.ui +1 -0
  137. package/gallery/Pipeline-passed.ui +1 -0
  138. package/gallery/Pipeline-running.ui +1 -0
  139. package/gallery/Pipeline-stages.ui +13 -0
  140. package/gallery/Pipeline.ui +1 -0
  141. package/gallery/Plan.ui +9 -0
  142. package/gallery/Progress.ui +4 -0
  143. package/gallery/Provenance-deploy.ui +6 -0
  144. package/gallery/Provenance-no-receipt.ui +1 -0
  145. package/gallery/Provenance.ui +1 -0
  146. package/gallery/PullRequest.ui +1 -0
  147. package/gallery/Quiz.ui +6 -0
  148. package/gallery/Radio.ui +5 -0
  149. package/gallery/Rating.ui +5 -0
  150. package/gallery/Roadmap-sprint.ui +8 -0
  151. package/gallery/Roadmap-year.ui +9 -0
  152. package/gallery/Roadmap.ui +12 -0
  153. package/gallery/Row.ui +3 -0
  154. package/gallery/SegmentedBar-status.ui +8 -0
  155. package/gallery/SegmentedBar.ui +8 -0
  156. package/gallery/Select.ui +5 -0
  157. package/gallery/Slider.ui +7 -0
  158. package/gallery/Snippet.ui +3 -0
  159. package/gallery/Sources-chips.ui +4 -0
  160. package/gallery/Sources.ui +8 -0
  161. package/gallery/Sparkline.ui +6 -0
  162. package/gallery/Stack.ui +7 -0
  163. package/gallery/StackTrace-aggregate.ui +1 -0
  164. package/gallery/StackTrace-browser.ui +1 -0
  165. package/gallery/StackTrace-go.ui +1 -0
  166. package/gallery/StackTrace-java.ui +1 -0
  167. package/gallery/StackTrace-python.ui +1 -0
  168. package/gallery/StackTrace-rust.ui +1 -0
  169. package/gallery/StackTrace.ui +1 -0
  170. package/gallery/Status.ui +9 -0
  171. package/gallery/Stepper.ui +5 -0
  172. package/gallery/Steps.ui +8 -0
  173. package/gallery/Swatches-long-names.ui +4 -0
  174. package/gallery/Swatches-picker.ui +5 -0
  175. package/gallery/Swatches.ui +10 -0
  176. package/gallery/Table.ui +7 -0
  177. package/gallery/Tabs.ui +8 -0
  178. package/gallery/Tests-junit.ui +1 -0
  179. package/gallery/Tests-vitest-junit.ui +1 -0
  180. package/gallery/Tests-vitest.ui +1 -0
  181. package/gallery/Tests.ui +9 -0
  182. package/gallery/Text.ui +4 -0
  183. package/gallery/TextArea.ui +5 -0
  184. package/gallery/Threads-ids.ui +1 -0
  185. package/gallery/Threads.ui +1 -0
  186. package/gallery/Timeline.ui +8 -0
  187. package/gallery/Timer.ui +4 -0
  188. package/gallery/Toggle.ui +5 -0
  189. package/gallery/Transcript-text.ui +1 -0
  190. package/gallery/Transcript-untimed.ui +1 -0
  191. package/gallery/Transcript-whisper.ui +1 -0
  192. package/gallery/Transcript.ui +1 -0
  193. package/gallery/Tree.ui +3 -0
  194. package/gallery/Treemap-du.ui +1 -0
  195. package/gallery/Treemap-esbuild.ui +1 -0
  196. package/gallery/Treemap-tree.ui +7 -0
  197. package/gallery/Treemap.ui +18 -0
  198. package/gallery/Walkthrough.ui +9 -0
  199. package/gallery/Waterfall-har.ui +1 -0
  200. package/gallery/Waterfall-trace.ui +1 -0
  201. package/gallery/Waterfall.ui +11 -0
  202. package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
  203. package/gallery/data/acceptance/coil-gate.json +153 -0
  204. package/gallery/data/acceptance/orbit-baseline.json +163 -0
  205. package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
  206. package/gallery/data/acceptance/shots/gate-light.png +0 -0
  207. package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
  208. package/gallery/data/activity.json +1 -0
  209. package/gallery/data/bb-tasks-orb.json +420 -0
  210. package/gallery/data/biome.json +229 -0
  211. package/gallery/data/compare/status-after.png +0 -0
  212. package/gallery/data/compare/status-before.png +0 -0
  213. package/gallery/data/compare/status-dark-2x.png +0 -0
  214. package/gallery/data/compare/status-rerender.png +0 -0
  215. package/gallery/data/coverage-py.json +1 -0
  216. package/gallery/data/coverage-summary.json +1 -0
  217. package/gallery/data/coverage.out +23 -0
  218. package/gallery/data/deploys.json +1 -0
  219. package/gallery/data/device-runs.json +285 -0
  220. package/gallery/data/du-node-modules.txt +359 -0
  221. package/gallery/data/esbuild-meta.json +292 -0
  222. package/gallery/data/eslint.json +266 -0
  223. package/gallery/data/eslint.sarif +343 -0
  224. package/gallery/data/findings-review.json +76 -0
  225. package/gallery/data/gh-issues-nextjs.json +293 -0
  226. package/gallery/data/gh-pr-checks.json +272 -0
  227. package/gallery/data/gh-prs-nextjs.json +419 -0
  228. package/gallery/data/gh-run-failed.json +1 -0
  229. package/gallery/data/gh-run-passed.json +1 -0
  230. package/gallery/data/gh-run-running.json +1 -0
  231. package/gallery/data/github-release.json +57 -0
  232. package/gallery/data/hyperfine-startup.json +187 -0
  233. package/gallery/data/lcov.info +234 -0
  234. package/gallery/data/logs/media-sync.log +127 -0
  235. package/gallery/data/logs/thumbnailer.jsonl +17 -0
  236. package/gallery/data/logs/vitest-run.log +89 -0
  237. package/gallery/data/npm-audit.json +672 -0
  238. package/gallery/data/npm-outdated.json +179 -0
  239. package/gallery/data/oxlint.json +156 -0
  240. package/gallery/data/pip-outdated.json +68 -0
  241. package/gallery/data/pnpm-outdated.json +58 -0
  242. package/gallery/data/provenance/devicectl-install.json +35 -0
  243. package/gallery/data/provenance/install-timeout.log +4 -0
  244. package/gallery/data/provenance/iphone-install.json +72 -0
  245. package/gallery/data/provenance/iphone-no-receipt.json +57 -0
  246. package/gallery/data/provenance/live-home.jpg +0 -0
  247. package/gallery/data/provenance/live-probe.txt +10 -0
  248. package/gallery/data/provenance/xcodebuild-2811.log +19 -0
  249. package/gallery/data/provenance/xcodebuild-4712.log +19 -0
  250. package/gallery/data/pytest-junit.xml +64 -0
  251. package/gallery/data/python-org-trace.json +35 -0
  252. package/gallery/data/python-org.har +31 -0
  253. package/gallery/data/retry.ts +13 -0
  254. package/gallery/data/ruff.json +261 -0
  255. package/gallery/data/src/durations.py +63 -0
  256. package/gallery/data/src/json-logic.ts +156 -0
  257. package/gallery/data/src/mapMath.ts +135 -0
  258. package/gallery/data/src/semver/bump.go +73 -0
  259. package/gallery/data/traces/browser-error.json +5 -0
  260. package/gallery/data/traces/go/main.go +27 -0
  261. package/gallery/data/traces/go-panic.txt +16 -0
  262. package/gallery/data/traces/java-caused-by.txt +15 -0
  263. package/gallery/data/traces/node-aggregate.txt +30 -0
  264. package/gallery/data/traces/node-cause.txt +21 -0
  265. package/gallery/data/traces/py/deploy.json +5 -0
  266. package/gallery/data/traces/py/deploy.py +17 -0
  267. package/gallery/data/traces/py/settings.py +19 -0
  268. package/gallery/data/traces/python-chain.txt +30 -0
  269. package/gallery/data/traces/rust/main.rs +11 -0
  270. package/gallery/data/traces/rust-panic.txt +16 -0
  271. package/gallery/data/traces/shop/cart.mjs +19 -0
  272. package/gallery/data/traces/shop/pricing.mjs +12 -0
  273. package/gallery/data/traces/shop/status.mjs +9 -0
  274. package/gallery/data/traces/web/app.js +11 -0
  275. package/gallery/data/traces/web/index.html +1 -0
  276. package/gallery/data/traces/web/search.js +10 -0
  277. package/gallery/data/transcript/call-noisy.wav +0 -0
  278. package/gallery/data/transcript/call.json +362 -0
  279. package/gallery/data/transcript/call.wav +0 -0
  280. package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
  281. package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
  282. package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
  283. package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
  284. package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
  285. package/gallery/data/tsc.txt +4 -0
  286. package/gallery/data/vitest-bench.json +119 -0
  287. package/gallery/data/vitest-junit.xml +133 -0
  288. package/gallery/data/vitest-report.json +612 -0
  289. package/gallery/images/bell.svg +10 -0
  290. package/gallery/images/bike.svg +47 -0
  291. package/gallery/images/footer-planet.webp +0 -0
  292. package/gallery/images/front-light.svg +13 -0
  293. package/gallery/images/golden-hour.webp +0 -0
  294. package/gallery/images/option-flowers.webp +0 -0
  295. package/gallery/images/option-hilltop.webp +0 -0
  296. package/gallery/images/option-meadow.webp +0 -0
  297. package/gallery/images/option-sky.webp +0 -0
  298. package/gallery/images/u-lock.svg +10 -0
  299. package/index.client.tsx +164 -0
  300. package/index.server.ts +216 -0
  301. package/package.json +50 -4
  302. package/paseo-plugin.json +14 -0
  303. package/server/data-source.ts +407 -0
  304. package/server/project.ts +981 -0
  305. package/server/web/address.ts +57 -0
  306. package/server/web/cache.ts +85 -0
  307. package/server/web/commons.ts +126 -0
  308. package/server/web/geocode.ts +40 -0
  309. package/server/web/image.ts +35 -0
  310. package/server/web/lookups.ts +248 -0
  311. package/server/web/net.ts +210 -0
  312. package/shared/core/acceptance.ts +744 -0
  313. package/shared/core/ast.ts +76 -0
  314. package/shared/core/benchmark.ts +743 -0
  315. package/shared/core/catalog.ts +733 -0
  316. package/shared/core/compare.ts +225 -0
  317. package/shared/core/coverage.ts +456 -0
  318. package/shared/core/data.ts +334 -0
  319. package/shared/core/decisions.ts +527 -0
  320. package/shared/core/dependencies.ts +604 -0
  321. package/shared/core/diagnostics.ts +24 -0
  322. package/shared/core/environments.ts +212 -0
  323. package/shared/core/evaluate.ts +737 -0
  324. package/shared/core/fields.ts +66 -0
  325. package/shared/core/findings.ts +1172 -0
  326. package/shared/core/form.ts +69 -0
  327. package/shared/core/index.ts +44 -0
  328. package/shared/core/junit.ts +252 -0
  329. package/shared/core/lexer.ts +275 -0
  330. package/shared/core/logs.ts +496 -0
  331. package/shared/core/markdown.ts +2374 -0
  332. package/shared/core/parser.ts +355 -0
  333. package/shared/core/perf-units.ts +228 -0
  334. package/shared/core/pipeline.ts +679 -0
  335. package/shared/core/project.ts +472 -0
  336. package/shared/core/provenance.ts +725 -0
  337. package/shared/core/stack-trace.ts +718 -0
  338. package/shared/core/streaming.ts +136 -0
  339. package/shared/core/test-results.ts +562 -0
  340. package/shared/core/thread-activity.ts +203 -0
  341. package/shared/core/tracking.ts +887 -0
  342. package/shared/core/transcript.ts +1364 -0
  343. package/shared/core/treemap.ts +446 -0
  344. package/shared/core/types.ts +70 -0
  345. package/shared/core/validate.ts +574 -0
  346. package/shared/core/waterfall.ts +536 -0
  347. package/shared/diagram/flowchart.ts +205 -0
  348. package/shared/diagram/layout.ts +440 -0
  349. package/shared/diagram/markdown.ts +135 -0
  350. package/shared/diagram/sequence.ts +122 -0
  351. package/shared/diagram/zoom.ts +114 -0
  352. package/shared/fallback.ts +120 -0
  353. package/shared/genui.test.ts +122 -0
  354. package/shared/instructions.ts +144 -0
  355. package/shared/logic/calendarMath.ts +130 -0
  356. package/shared/logic/chartMath.ts +289 -0
  357. package/shared/logic/choose-logic.ts +196 -0
  358. package/shared/logic/compare-logic.ts +270 -0
  359. package/shared/logic/content-model.ts +227 -0
  360. package/shared/logic/environments-logic.ts +53 -0
  361. package/shared/logic/explain-logic.ts +315 -0
  362. package/shared/logic/fieldLogic.ts +138 -0
  363. package/shared/logic/gallery-search.ts +71 -0
  364. package/shared/logic/icons.ts +322 -0
  365. package/shared/logic/json-logic.ts +153 -0
  366. package/shared/logic/mapMath.ts +133 -0
  367. package/shared/logic/project-logic.ts +39 -0
  368. package/shared/logic/references-model.ts +201 -0
  369. package/shared/logic/sketch.ts +165 -0
  370. package/shared/logic/supersession.ts +77 -0
  371. package/shared/logic/values.ts +133 -0
  372. package/shared/logic/widget-logic.ts +163 -0
  373. package/shared/rpc.ts +252 -0
  374. package/shared/segment.ts +81 -0
  375. package/shared/settings.ts +12 -0
@@ -0,0 +1,725 @@
1
+ // Provenance: which build or revision really reached a phone or a server.
2
+ // A chain of stages from source to live (checkout → build → transfer →
3
+ // install → launch; commit → build → deploy → live probe), each with the
4
+ // facts it reported (branch, sha, build, device, url…). Reads the chain,
5
+ // flags a fact that differs from what was requested (`expected`) or that
6
+ // changes between stages (the sha built is not the sha installed), finds the
7
+ // last stage with a receipt, and says in one line what is true. Unknown is
8
+ // its own state: a stage without a reported outcome is "no receipt", never
9
+ // a pass. Pure: no React, no DOM.
10
+ import { formatElapsed, parseDuration, timestamp } from "./pipeline";
11
+ import type { Value } from "./types";
12
+
13
+ export type StageStatus = "done" | "failed" | "running" | "pending" | "skipped" | "unknown";
14
+
15
+ export const STAGE_STATUS_LABEL: Readonly<Record<StageStatus, string>> = {
16
+ done: "Done",
17
+ failed: "Failed",
18
+ running: "Running",
19
+ pending: "Not started",
20
+ skipped: "Skipped",
21
+ unknown: "Unknown",
22
+ };
23
+
24
+ /** What a fact's value is checked against, when it is wrong. */
25
+ export type FactCheck =
26
+ /** Differs from what the user asked for. */
27
+ | { readonly kind: "expected"; readonly expected: string }
28
+ /** Differs from the stage before that reported the same fact. */
29
+ | { readonly kind: "changed"; readonly stage: number; readonly previous: string };
30
+
31
+ export interface Fact {
32
+ /** Canonical key: `sha` for commit/revision, `build` for CFBundleVersion… */
33
+ readonly key: string;
34
+ /** The key as shown. */
35
+ readonly label: string;
36
+ /** The value in full, as copied. */
37
+ readonly value: string;
38
+ /** The value as shown: a sha's first 7 characters, a digest's first 12. */
39
+ readonly short: string;
40
+ /** An identity fact (branch, sha, build, version…), compared across stages. */
41
+ readonly identity: boolean;
42
+ readonly check: FactCheck | null;
43
+ /** Equal to what was requested, or to the previous stage that reported it; only ever on a stage with a receipt. */
44
+ readonly confirmed: boolean;
45
+ }
46
+
47
+ export type Evidence =
48
+ | { readonly kind: "url"; readonly url: string; readonly label: string | null }
49
+ | { readonly kind: "image"; readonly src: string; readonly label: string | null }
50
+ | { readonly kind: "file"; readonly path: string; readonly line: number | null; readonly label: string | null }
51
+ | { readonly kind: "text"; readonly text: string; readonly label: string | null };
52
+
53
+ /**
54
+ * Whether a stage reported its outcome: `receipt` (done, failed or
55
+ * running), `none` (no receipt: unknown, or never reported), `waiting` (after
56
+ * a running stage), `not-reached` (after a failure), `skipped`.
57
+ */
58
+ export type Receipt = "receipt" | "none" | "waiting" | "not-reached" | "skipped";
59
+
60
+ export interface Stage {
61
+ readonly name: string;
62
+ readonly status: StageStatus;
63
+ /** A status word worth keeping beside the status: "timed out". */
64
+ readonly note: string | null;
65
+ readonly facts: readonly Fact[];
66
+ /** Epoch ms, when `time` is a date. */
67
+ readonly at: number | null;
68
+ /** `time` as written, when it is no date ("after 6 min"). */
69
+ readonly timeText: string | null;
70
+ readonly ms: number | null;
71
+ readonly detail: string | null;
72
+ readonly evidence: readonly Evidence[];
73
+ readonly receipt: Receipt;
74
+ /** The last stage with a receipt while the chain stops after it: later stages have no receipt and nothing failed or runs. */
75
+ readonly last: boolean;
76
+ /** The past-tense verb for what this stage did ("installed"), from its name; null when its name says none. */
77
+ readonly verb: string | null;
78
+ }
79
+
80
+ export interface Mismatch {
81
+ readonly stage: number;
82
+ readonly key: string;
83
+ readonly label: string;
84
+ readonly actual: string;
85
+ readonly check: FactCheck;
86
+ }
87
+
88
+ export type VerdictTone = "good" | "bad" | "warn" | "info" | "neutral";
89
+
90
+ export interface Verdict {
91
+ readonly tone: VerdictTone;
92
+ readonly title: string;
93
+ /** Mismatches first, then what is unconfirmed or missing. */
94
+ readonly lines: readonly { readonly text: string; readonly tone: VerdictTone }[];
95
+ }
96
+
97
+ export interface Provenance {
98
+ readonly stages: readonly Stage[];
99
+ readonly expected: readonly Fact[];
100
+ readonly mismatches: readonly Mismatch[];
101
+ /** The index of the last stage with a receipt; -1 when none has one. */
102
+ readonly lastReceipt: number;
103
+ /** Summed durations, and how many stages had one. */
104
+ readonly totalMs: number | null;
105
+ readonly timed: number;
106
+ readonly verdict: Verdict;
107
+ }
108
+
109
+ export type ProvenanceResult = { readonly ok: true; readonly provenance: Provenance } | { readonly ok: false; readonly error: string };
110
+
111
+ const SHAPE_HINT = "Provenance needs a list of stages [{name, status, facts?: {branch, sha, build, device…}}], source to live";
112
+
113
+ /** Stages read at most; a real chain has a handful. */
114
+ export const MAX_STAGES = 40;
115
+ /** Facts read per stage at most. */
116
+ const MAX_FACTS = 24;
117
+ /** Evidence items read per stage at most. */
118
+ const MAX_EVIDENCE = 12;
119
+
120
+ type Fields = Readonly<Record<string, Value | undefined>>;
121
+
122
+ function fieldsOf(value: Value | undefined): Fields | null {
123
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
124
+ if ("kind" in value && (value.kind === "node" || value.kind === "action" || value.kind === "binding")) return null;
125
+ return value as Fields;
126
+ }
127
+
128
+ function str(value: Value | undefined): string | null {
129
+ if (typeof value === "string") return value.trim() === "" ? null : value.trim();
130
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
131
+ return null;
132
+ }
133
+
134
+ // ---------------------------------------------------------------------------
135
+ // Status
136
+
137
+ const STATUS_WORDS: Readonly<Record<string, readonly [StageStatus, string | null]>> = {
138
+ done: ["done", null],
139
+ success: ["done", null],
140
+ succeeded: ["done", null],
141
+ successful: ["done", null],
142
+ ok: ["done", null],
143
+ pass: ["done", null],
144
+ passed: ["done", null],
145
+ completed: ["done", null],
146
+ complete: ["done", null],
147
+ finished: ["done", null],
148
+ built: ["done", null],
149
+ installed: ["done", null],
150
+ deployed: ["done", null],
151
+ launched: ["done", null],
152
+ live: ["done", null],
153
+ verified: ["done", null],
154
+ failed: ["failed", null],
155
+ failure: ["failed", null],
156
+ fail: ["failed", null],
157
+ error: ["failed", "error"],
158
+ errored: ["failed", "error"],
159
+ broken: ["failed", null],
160
+ crashed: ["failed", "crashed"],
161
+ rejected: ["failed", "rejected"],
162
+ running: ["running", null],
163
+ inprogress: ["running", null],
164
+ active: ["running", null],
165
+ started: ["running", null],
166
+ building: ["running", null],
167
+ installing: ["running", null],
168
+ deploying: ["running", null],
169
+ uploading: ["running", null],
170
+ transferring: ["running", null],
171
+ pending: ["pending", null],
172
+ queued: ["pending", "queued"],
173
+ waiting: ["pending", null],
174
+ scheduled: ["pending", null],
175
+ notstarted: ["pending", null],
176
+ todo: ["pending", null],
177
+ skipped: ["skipped", null],
178
+ skip: ["skipped", null],
179
+ notneeded: ["skipped", null],
180
+ na: ["skipped", null],
181
+ unknown: ["unknown", null],
182
+ // A request that timed out says nothing about what happened on the other end.
183
+ timeout: ["unknown", "timed out"],
184
+ timedout: ["unknown", "timed out"],
185
+ noreceipt: ["unknown", null],
186
+ uncertain: ["unknown", null],
187
+ unverified: ["unknown", "unverified"],
188
+ claimed: ["unknown", "claimed, not checked"],
189
+ missing: ["unknown", null],
190
+ };
191
+
192
+ /** A status word (any case; spaces, `_`, `-`, `/` ignored); anything else is unknown, keeping the word as its note. */
193
+ export function stageStatus(value: Value | undefined): { readonly status: StageStatus; readonly note: string | null } {
194
+ if (typeof value === "boolean") return { status: value ? "done" : "failed", note: null };
195
+ const raw = str(value);
196
+ if (raw === null) return { status: "unknown", note: null };
197
+ const word = raw.toLowerCase().replace(/[\s_/-]+/g, "");
198
+ if (Object.hasOwn(STATUS_WORDS, word)) {
199
+ const [status, note] = STATUS_WORDS[word];
200
+ return { status, note };
201
+ }
202
+ return { status: "unknown", note: raw };
203
+ }
204
+
205
+ // ---------------------------------------------------------------------------
206
+ // Facts
207
+
208
+ /** Spellings of the keys tools and agents use, lowercased without separators, to the canonical key. */
209
+ const KEY_ALIASES: Readonly<Record<string, string>> = {
210
+ sha: "sha",
211
+ commit: "sha",
212
+ commitsha: "sha",
213
+ revision: "sha",
214
+ rev: "sha",
215
+ gitsha: "sha",
216
+ githash: "sha",
217
+ gitcommit: "sha",
218
+ headsha: "sha",
219
+ head: "sha",
220
+ branch: "branch",
221
+ ref: "branch",
222
+ gitbranch: "branch",
223
+ headbranch: "branch",
224
+ build: "build",
225
+ buildnumber: "build",
226
+ buildno: "build",
227
+ bundleversion: "build",
228
+ cfbundleversion: "build",
229
+ version: "version",
230
+ versionname: "version",
231
+ appversion: "version",
232
+ marketingversion: "version",
233
+ cfbundleshortversionstring: "version",
234
+ digest: "digest",
235
+ checksum: "digest",
236
+ sha256: "digest",
237
+ hash: "digest",
238
+ artifactdigest: "digest",
239
+ imagedigest: "digest",
240
+ bundle: "bundle",
241
+ bundleid: "bundle",
242
+ bundleidentifier: "bundle",
243
+ artifact: "artifact",
244
+ ipa: "artifact",
245
+ archive: "artifact",
246
+ package: "artifact",
247
+ model: "model",
248
+ host: "host",
249
+ hostname: "host",
250
+ machine: "host",
251
+ device: "device",
252
+ devicename: "device",
253
+ phone: "device",
254
+ udid: "udid",
255
+ deviceid: "udid",
256
+ url: "url",
257
+ link: "url",
258
+ };
259
+
260
+ /** Facts that name what was built and shipped: compared across stages. Hosts, devices and URLs change by design. */
261
+ const IDENTITY_KEYS: Readonly<Record<string, true>> = { branch: true, sha: true, build: true, version: true, bundle: true, digest: true, model: true };
262
+
263
+ /** The order facts read in, most telling first; other keys follow in the order given. */
264
+ const KEY_ORDER = ["branch", "sha", "build", "version", "bundle", "digest", "artifact", "model", "host", "device", "udid", "url"];
265
+
266
+ const KEY_LABEL: Readonly<Record<string, string>> = { udid: "UDID", url: "URL" };
267
+
268
+ /** `abc1234` for a sha, a digest's first 12 hex digits (no `sha256:`), a URL without its scheme; everything else as written. */
269
+ export function shortValue(key: string, value: string): string {
270
+ if (key === "sha" && /^[0-9a-f]{8,}$/i.test(value)) return value.slice(0, 7);
271
+ if (key === "digest") {
272
+ const match = /^([a-z0-9]+:)?([0-9a-f]{16,})$/i.exec(value);
273
+ if (match !== null) return match[2].slice(0, 12);
274
+ }
275
+ if (key === "url") return value.replace(/^https?:\/\//i, "").replace(/\/$/, "");
276
+ return value;
277
+ }
278
+
279
+ function factText(value: Value | undefined): string | null {
280
+ if (typeof value === "boolean") return value ? "yes" : "no";
281
+ const text = str(value);
282
+ if (text !== null) return text;
283
+ if (value === undefined || value === null || typeof value !== "object") return null;
284
+ try {
285
+ return JSON.stringify(value);
286
+ } catch {
287
+ return null;
288
+ }
289
+ }
290
+
291
+ /** Whether two values of `key` name the same thing: a short sha matches its full sha; case and quotes are ignored for words. */
292
+ export function sameValue(key: string, a: string, b: string): boolean {
293
+ if (key === "sha" || key === "digest") {
294
+ const clean = (value: string) => value.toLowerCase().replace(/^[a-z0-9]+:(?=[0-9a-f]{16,}$)/, "");
295
+ const x = clean(a);
296
+ const y = clean(b);
297
+ if (x === y) return true;
298
+ const [shorter, longer] = x.length <= y.length ? [x, y] : [y, x];
299
+ return /^[0-9a-f]{7,}$/.test(shorter) && /^[0-9a-f]+$/.test(longer) && longer.startsWith(shorter);
300
+ }
301
+ // A branch without `refs/heads/`, `refs/remotes/<remote>/` or `origin/`.
302
+ const plain = (value: string) => value.replace(/^refs\/heads\//, "").replace(/^refs\/remotes\/[^/]+\//, "").replace(/^origin\//, "");
303
+ if (key === "branch") return plain(a) === plain(b);
304
+ const loose = (value: string) => value.normalize("NFC").replace(/[\u2018\u2019]/g, "'").replace(/\s+/g, " ").trim().toLowerCase();
305
+ return loose(a) === loose(b);
306
+ }
307
+
308
+ interface RawFact {
309
+ readonly key: string;
310
+ readonly label: string;
311
+ readonly value: string;
312
+ }
313
+
314
+ /** Facts as `{key: value}`, or as `[{key|name|label, value}]` as some tools write them; known spellings to their canonical key, first one wins. */
315
+ function rawFacts(value: Value | undefined): RawFact[] {
316
+ const fields = fieldsOf(value);
317
+ const pairs: [string, Value | undefined][] =
318
+ fields !== null
319
+ ? Object.entries(fields)
320
+ : Array.isArray(value)
321
+ ? value.flatMap((item): [string, Value | undefined][] => {
322
+ const entry = fieldsOf(item);
323
+ const given = entry === null ? null : str(entry.key ?? entry.name ?? entry.label);
324
+ return entry === null || given === null ? [] : [[given, entry.value]];
325
+ })
326
+ : [];
327
+ const out: RawFact[] = [];
328
+ const seen = new Set<string>();
329
+ for (const [given, raw] of pairs) {
330
+ if (out.length >= MAX_FACTS) break;
331
+ const text = factText(raw);
332
+ if (text === null) continue;
333
+ const alias = KEY_ALIASES[given.toLowerCase().replace(/[\s_.-]+/g, "")];
334
+ const key = alias ?? given.trim();
335
+ if (key === "" || seen.has(key)) continue;
336
+ seen.add(key);
337
+ out.push({ key, label: alias === undefined ? key : (KEY_LABEL[key] ?? key), value: text });
338
+ }
339
+ const rank = (key: string) => {
340
+ const index = KEY_ORDER.indexOf(key);
341
+ return index < 0 ? KEY_ORDER.length : index;
342
+ };
343
+ return out.map((fact, index) => ({ fact, index })).sort((a, b) => rank(a.fact.key) - rank(b.fact.key) || a.index - b.index).map(({ fact }) => fact);
344
+ }
345
+
346
+ // ---------------------------------------------------------------------------
347
+ // Evidence
348
+
349
+ const IMAGE_EXTENSION = /\.(png|jpe?g|gif|webp|avif|heic)(\?.*)?$/i;
350
+
351
+ /** A string as evidence: an https link, an image, a file (with `:line`), or a note. */
352
+ export function evidenceOf(raw: string, label: string | null = null): Evidence {
353
+ const text = raw.trim();
354
+ if (/^https?:\/\/\S+$/i.test(text)) {
355
+ return IMAGE_EXTENSION.test(text) && /^https:/i.test(text) ? { kind: "image", src: text, label } : { kind: "url", url: text, label };
356
+ }
357
+ const pathLike = !/\s/.test(text) && (/^(\/|\.{1,2}\/|~\/|\$[A-Z_]+)/.test(text) || text.includes("/") || /\.[a-z0-9]{1,6}(:\d+)?$/i.test(text));
358
+ if (pathLike) {
359
+ const located = /^(.*?):(\d+)(?::\d+)?$/.exec(text);
360
+ const path = located === null ? text : located[1];
361
+ const line = located === null ? null : Number(located[2]);
362
+ if (IMAGE_EXTENSION.test(path)) return { kind: "image", src: path, label };
363
+ return { kind: "file", path, line, label };
364
+ }
365
+ return { kind: "text", text, label };
366
+ }
367
+
368
+ function evidenceItem(value: Value | undefined): Evidence | null {
369
+ if (typeof value === "string") return value.trim() === "" ? null : evidenceOf(value);
370
+ const fields = fieldsOf(value);
371
+ if (fields === null) return null;
372
+ const label = str(fields.label ?? fields.title ?? fields.name);
373
+ const url = str(fields.url ?? fields.href ?? fields.link);
374
+ if (url !== null && /^https?:\/\//i.test(url)) return { ...evidenceOf(url, label), label } as Evidence;
375
+ const image = str(fields.image ?? fields.src ?? fields.screenshot);
376
+ if (image !== null) {
377
+ const read = evidenceOf(image, label);
378
+ return read.kind === "file" ? { kind: "image", src: read.path, label } : read.kind === "url" ? { kind: "image", src: read.url, label } : read;
379
+ }
380
+ const path = str(fields.path ?? fields.file ?? fields.log);
381
+ if (path !== null) {
382
+ const line = typeof fields.line === "number" && Number.isInteger(fields.line) && fields.line > 0 ? fields.line : null;
383
+ const read = evidenceOf(path, label);
384
+ if (read.kind === "file") return { ...read, line: line ?? read.line };
385
+ return read;
386
+ }
387
+ const text = str(fields.text ?? fields.note ?? fields.output ?? fields.message);
388
+ return text === null ? null : { kind: "text", text, label };
389
+ }
390
+
391
+ function evidenceList(fields: Fields): Evidence[] {
392
+ const out: Evidence[] = [];
393
+ const add = (value: Value | undefined) => {
394
+ if (value === undefined || value === null) return;
395
+ for (const item of Array.isArray(value) ? value : [value]) {
396
+ if (out.length >= MAX_EVIDENCE) return;
397
+ const evidence = evidenceItem(item);
398
+ if (evidence !== null) out.push(evidence);
399
+ }
400
+ };
401
+ add(fields.evidence);
402
+ add(fields.receipt);
403
+ add(fields.log);
404
+ add(fields.screenshot);
405
+ add(fields.artifacts);
406
+ return out;
407
+ }
408
+
409
+ // ---------------------------------------------------------------------------
410
+ // Stage names
411
+
412
+ /** The verb a stage's name implies, in the past tense, first match wins: "Install on iPhone" → installed. */
413
+ const VERBS: readonly (readonly [RegExp, string])[] = [
414
+ [/\b(re-?)?install/i, "installed"],
415
+ [/\blaunch|\bopen(ed)? on\b|\brun on\b|\bstart(ed)? on\b/i, "launched"],
416
+ [/\bprobe|\blive\b|\bserv(e|ed|ing)\b|\bsmoke\b|\bhealth/i, "live"],
417
+ [/\bdeploy|\bpromot/i, "deployed"],
418
+ [/\bpublish/i, "published"],
419
+ [/\brelease/i, "released"],
420
+ [/\bupload|\bsubmit/i, "uploaded"],
421
+ [/\btransfer|\bcop(y|ied)\b|\bsync|\bscp\b|\bdownload/i, "transferred"],
422
+ [/\bbuil[dt]|\bcompil|\barchiv|\bbundl/i, "built"],
423
+ [/\bcheck(ed)?[\s-]?out|\bsource\b|\bclone|\bfetch|\bworktree/i, "checked out"],
424
+ [/\bmerge/i, "merged"],
425
+ [/\bpush/i, "pushed"],
426
+ [/\bcommit/i, "committed"],
427
+ ];
428
+
429
+ export function stageVerb(name: string): string | null {
430
+ for (const [pattern, verb] of VERBS) if (pattern.test(name)) return verb;
431
+ return null;
432
+ }
433
+
434
+ /** What the verdict calls the thing a stage holds: "Installed build", "Deployed revision". */
435
+ const SUBJECT: Readonly<Record<string, string>> = {
436
+ installed: "Installed build",
437
+ launched: "Launched build",
438
+ live: "Deployed revision",
439
+ deployed: "Deployed revision",
440
+ published: "Published revision",
441
+ released: "Released build",
442
+ uploaded: "Uploaded build",
443
+ transferred: "Transferred build",
444
+ built: "Built artifact",
445
+ "checked out": "Checked-out source",
446
+ merged: "Merged revision",
447
+ pushed: "Pushed revision",
448
+ committed: "Commit",
449
+ };
450
+
451
+ function subjectOf(stage: Stage): string {
452
+ return stage.verb === null ? stage.name : SUBJECT[stage.verb];
453
+ }
454
+
455
+ /** "installed main", or "main at Smoke test" where the name says no verb. */
456
+ function valueAt(stage: Stage, value: string): string {
457
+ return stage.verb === null ? `${value} at ${stage.name}` : `${stage.verb} ${value}`;
458
+ }
459
+
460
+ // ---------------------------------------------------------------------------
461
+ // Reading
462
+
463
+ interface RawStage {
464
+ readonly name: string;
465
+ readonly status: StageStatus;
466
+ readonly note: string | null;
467
+ readonly facts: RawFact[];
468
+ readonly at: number | null;
469
+ readonly timeText: string | null;
470
+ readonly ms: number | null;
471
+ readonly detail: string | null;
472
+ readonly evidence: Evidence[];
473
+ }
474
+
475
+ function rawStage(value: Value, index: number): RawStage | null {
476
+ if (typeof value === "string") {
477
+ const name = value.trim();
478
+ return name === "" ? null : { name, status: "unknown", note: null, facts: [], at: null, timeText: null, ms: null, detail: null, evidence: [] };
479
+ }
480
+ const fields = fieldsOf(value);
481
+ if (fields === null) return null;
482
+ const name = str(fields.name ?? fields.stage ?? fields.title ?? fields.step) ?? `Stage ${index + 1}`;
483
+ const { status, note } = stageStatus(fields.status ?? fields.state ?? fields.result ?? fields.conclusion ?? undefined);
484
+ const timeValue = fields.time ?? fields.at ?? fields.completedAt ?? fields.finishedAt ?? fields.startedAt ?? fields.timestamp ?? undefined;
485
+ const at = timestamp(timeValue);
486
+ const timeText = at === null ? str(timeValue) : null;
487
+ let ms = parseDuration(fields.duration ?? fields.elapsed ?? undefined);
488
+ if (ms === null) {
489
+ const start = timestamp(fields.startedAt ?? fields.start ?? undefined);
490
+ const end = timestamp(fields.completedAt ?? fields.finishedAt ?? fields.end ?? undefined);
491
+ if (start !== null && end !== null && end >= start) ms = end - start;
492
+ }
493
+ const detail = str(fields.detail ?? fields.note ?? fields.message ?? fields.error ?? undefined);
494
+ return { name, status, note, facts: rawFacts(fields.facts ?? fields.identity ?? undefined), at, timeText, ms, detail, evidence: evidenceList(fields) };
495
+ }
496
+
497
+ /** The chain `stages` describes, checked against `expected`, or why it can't be read. */
498
+ export function readProvenance(stages: Value | undefined, expected?: Value): ProvenanceResult {
499
+ if (!Array.isArray(stages)) return { ok: false, error: SHAPE_HINT };
500
+ if (stages.length > MAX_STAGES) return { ok: false, error: `Provenance shows up to ${MAX_STAGES} stages; this chain has ${stages.length}` };
501
+ const raws = stages.map(rawStage).filter((stage): stage is RawStage => stage !== null);
502
+ if (stages.length > 0 && raws.length === 0) return { ok: false, error: SHAPE_HINT };
503
+ const wanted = rawFacts(expected);
504
+ const wantedBy = new Map(wanted.map((fact) => [fact.key, fact.value]));
505
+
506
+ // A receipt: the stage reported its outcome (done, failed or running). Evidence on a stage without an outcome is shown, but is no receipt.
507
+ const reported = raws.map((stage) => stage.status === "done" || stage.status === "failed" || stage.status === "running");
508
+ const lastReceipt = reported.lastIndexOf(true);
509
+
510
+ // Checks: against what was asked for where it was asked, else against the previous stage reporting the same identity fact.
511
+ // A stage without a receipt is checked (it may be set up wrong) but confirms nothing: its facts are intentions.
512
+ const mismatches: Mismatch[] = [];
513
+ const checks = raws.map(() => new Map<string, { check: FactCheck | null; confirmed: boolean }>());
514
+ const previous = new Map<string, { stage: number; value: string }>();
515
+ raws.forEach((stage, index) => {
516
+ for (const fact of stage.facts) {
517
+ const want = wantedBy.get(fact.key);
518
+ if (want !== undefined) {
519
+ const same = sameValue(fact.key, want, fact.value);
520
+ const check: FactCheck | null = same ? null : { kind: "expected", expected: want };
521
+ checks[index].set(fact.key, { check, confirmed: same && reported[index] });
522
+ if (check !== null) mismatches.push({ stage: index, key: fact.key, label: fact.label, actual: fact.value, check });
523
+ continue;
524
+ }
525
+ if (IDENTITY_KEYS[fact.key] !== true) continue;
526
+ const before = previous.get(fact.key);
527
+ previous.set(fact.key, { stage: index, value: fact.value });
528
+ if (before === undefined) continue;
529
+ const same = sameValue(fact.key, before.value, fact.value);
530
+ const check: FactCheck | null = same ? null : { kind: "changed", stage: before.stage, previous: before.value };
531
+ const confirmed = same && reported[index] && reported[before.stage];
532
+ checks[index].set(fact.key, { check, confirmed });
533
+ // The first stage of an agreeing pair is confirmed too.
534
+ if (confirmed && !checks[before.stage].has(fact.key)) checks[before.stage].set(fact.key, { check: null, confirmed: true });
535
+ if (check !== null) mismatches.push({ stage: index, key: fact.key, label: fact.label, actual: fact.value, check });
536
+ }
537
+ });
538
+
539
+ const failedAt = raws.findIndex((stage) => stage.status === "failed");
540
+ const runningAt = raws.findIndex((stage) => stage.status === "running");
541
+ const receiptOf = (stage: RawStage, index: number): Receipt => {
542
+ if (stage.status === "skipped") return "skipped";
543
+ if (reported[index]) return "receipt";
544
+ if (index > lastReceipt && failedAt >= 0 && failedAt <= lastReceipt) return "not-reached";
545
+ if (index > lastReceipt && runningAt >= 0) return "waiting";
546
+ return "none";
547
+ };
548
+ const receipts = raws.map(receiptOf);
549
+ const gapAfter = receipts.some((receipt, index) => index > lastReceipt && receipt === "none");
550
+
551
+ const built: Stage[] = raws.map((stage, index) => ({
552
+ name: stage.name,
553
+ status: stage.status,
554
+ note: stage.note,
555
+ facts: stage.facts.map((fact) => {
556
+ const checked = checks[index].get(fact.key);
557
+ return {
558
+ key: fact.key,
559
+ label: fact.label,
560
+ value: fact.value,
561
+ short: shortValue(fact.key, fact.value),
562
+ identity: IDENTITY_KEYS[fact.key] === true,
563
+ check: checked?.check ?? null,
564
+ confirmed: checked?.confirmed ?? false,
565
+ };
566
+ }),
567
+ at: stage.at,
568
+ timeText: stage.timeText,
569
+ ms: stage.ms,
570
+ detail: stage.detail,
571
+ evidence: stage.evidence,
572
+ receipt: receipts[index],
573
+ last: index === lastReceipt && gapAfter,
574
+ verb: stageVerb(stage.name),
575
+ }));
576
+
577
+ const timed = built.filter((stage) => stage.ms !== null);
578
+ const expectedFacts: Fact[] = wanted.map((fact) => ({
579
+ key: fact.key,
580
+ label: fact.label,
581
+ value: fact.value,
582
+ short: shortValue(fact.key, fact.value),
583
+ identity: IDENTITY_KEYS[fact.key] === true,
584
+ check: null,
585
+ confirmed: built.some((stage) => stage.receipt === "receipt" && stage.facts.some((candidate) => candidate.key === fact.key && candidate.confirmed)),
586
+ }));
587
+ return {
588
+ ok: true,
589
+ provenance: {
590
+ stages: built,
591
+ expected: expectedFacts,
592
+ mismatches,
593
+ lastReceipt,
594
+ totalMs: timed.length === 0 ? null : timed.reduce((sum, stage) => sum + (stage.ms ?? 0), 0),
595
+ timed: timed.length,
596
+ verdict: verdictOf(built, expectedFacts, mismatches, lastReceipt),
597
+ },
598
+ };
599
+ }
600
+
601
+ // ---------------------------------------------------------------------------
602
+ // Verdict
603
+
604
+ /** "expected branch feat/399 · installed main", the first wrong stage named when it is earlier. */
605
+ export function mismatchText(stages: readonly Stage[], mismatch: Mismatch, firstWrong: number = mismatch.stage): string {
606
+ const stage = stages[mismatch.stage];
607
+ const actual = shortValue(mismatch.key, mismatch.actual);
608
+ if (mismatch.check.kind === "expected") {
609
+ const since = firstWrong < mismatch.stage ? ` (wrong from ${stages[firstWrong].name} on)` : "";
610
+ return `expected ${mismatch.label} ${shortValue(mismatch.key, mismatch.check.expected)} · ${valueAt(stage, actual)}${since}`;
611
+ }
612
+ const from = stages[mismatch.check.stage];
613
+ return `${mismatch.label} changed: ${valueAt(from, shortValue(mismatch.key, mismatch.check.previous))} · ${valueAt(stage, actual)}`;
614
+ }
615
+
616
+ function joinNames(names: readonly string[]): string {
617
+ return names.length <= 1 ? (names[0] ?? "") : `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
618
+ }
619
+
620
+ function verdictOf(stages: readonly Stage[], expected: readonly Fact[], mismatches: readonly Mismatch[], lastReceipt: number): Verdict {
621
+ const lines: { text: string; tone: VerdictTone }[] = [];
622
+
623
+ // One line per wrong fact: what was asked for against the furthest stage that has it wrong.
624
+ const expectedWrong = mismatches.filter((mismatch) => mismatch.check.kind === "expected");
625
+ for (const key of new Set(expectedWrong.map((mismatch) => mismatch.key))) {
626
+ const ofKey = expectedWrong.filter((mismatch) => mismatch.key === key);
627
+ lines.push({ text: mismatchText(stages, ofKey[ofKey.length - 1], ofKey[0].stage), tone: "bad" });
628
+ }
629
+ const changed = mismatches.filter((mismatch) => mismatch.check.kind === "changed");
630
+ for (const mismatch of changed) lines.push({ text: mismatchText(stages, mismatch), tone: "bad" });
631
+
632
+ // What was asked for and no stage with a receipt confirms.
633
+ for (const fact of expected) {
634
+ if (fact.confirmed || expectedWrong.some((mismatch) => mismatch.key === fact.key)) continue;
635
+ const named = stages.filter((stage) => stage.facts.some((candidate) => candidate.key === fact.key));
636
+ lines.push({
637
+ text:
638
+ named.length === 0
639
+ ? `${fact.label} not confirmed: no stage reports it (expected ${fact.short})`
640
+ : `${fact.label} ${fact.short} not confirmed: only ${joinNames(named.map((stage) => stage.name))} names it, without a receipt`,
641
+ tone: "warn",
642
+ });
643
+ }
644
+
645
+ // Stages without a receipt, with why where the data says ("timed out").
646
+ const missing = stages.filter((stage) => stage.receipt === "none");
647
+ if (missing.length > 0) {
648
+ lines.push({ text: `No receipt: ${missing.map((stage) => (stage.note === null ? stage.name : `${stage.name} (${stage.note})`)).join(" · ")}`, tone: "warn" });
649
+ }
650
+
651
+ const pick = (title: string, tone: VerdictTone): Verdict => ({ tone, title, lines });
652
+
653
+ if (stages.length === 0) return pick("No stages", "neutral");
654
+ const failed = stages.find((stage) => stage.status === "failed");
655
+ if (failed !== undefined) return pick(`${failed.name} failed${failed.note === null ? "" : `: ${failed.note}`}`, "bad");
656
+
657
+ if (expectedWrong.length > 0) {
658
+ const furthest = expectedWrong[expectedWrong.length - 1];
659
+ const stage = stages[furthest.stage];
660
+ return pick(
661
+ stage.receipt === "receipt" ? `${subjectOf(stage)} is not the requested one` : `${stage.name}: ${furthest.label} isn't the requested one`,
662
+ "bad",
663
+ );
664
+ }
665
+ if (changed.length > 0) {
666
+ const last = changed[changed.length - 1];
667
+ if (last.check.kind === "changed") {
668
+ const from = last.check.stage;
669
+ return pick(`${subjectOf(stages[last.stage])} doesn't match ${from === 0 ? "the source" : stages[from].name}`, "bad");
670
+ }
671
+ }
672
+
673
+ const running = stages.find((stage) => stage.status === "running");
674
+ if (running !== undefined) return pick(`${running.name}: in progress`, "info");
675
+
676
+ if (lastReceipt < 0) return pick(`No receipts: none of the ${stages.length === 1 ? "stage" : `${stages.length} stages`} reported an outcome`, "warn");
677
+ if (stages.some((stage, index) => index > lastReceipt && stage.receipt !== "skipped")) {
678
+ return pick(`Unknown after ${stages[lastReceipt].name}: no receipt`, "warn");
679
+ }
680
+
681
+ // Every stage that ran has a receipt. Say what was compared, and against what.
682
+ const compared = stages
683
+ .map((stage, index) => ({ stage, index }))
684
+ .filter(({ stage }) => stage.receipt === "receipt" && stage.facts.some((fact) => fact.confirmed));
685
+ const subject = compared.length === 0 ? null : compared[compared.length - 1].stage;
686
+ const unconfirmed = expected.filter((fact) => !fact.confirmed);
687
+ if (subject === null) {
688
+ if (expected.length > 0) return pick(`Done, but no stage confirms the requested ${joinNames(expected.map((fact) => fact.label))}`, "warn");
689
+ lines.push({ text: "No identity fact (branch, sha, build, version, digest) appears in two stages, so nothing was compared", tone: "neutral" });
690
+ return pick(stages.length === 1 ? `${stages[0].name} done` : `All ${stages.length} stages done`, "good");
691
+ }
692
+ if (unconfirmed.length > 0) return pick(`${subjectOf(subject)}: ${joinNames(unconfirmed.map((fact) => fact.label))} not confirmed`, "warn");
693
+ // What was compared, so a match is not taken on faith: "sha 44f1d74 at all 4 stages".
694
+ const agreed = [...new Set(compared.flatMap(({ stage }) => stage.facts.filter((fact) => fact.confirmed).map((fact) => fact.key)))].map((key) => {
695
+ const holding = compared.filter(({ stage }) => stage.facts.some((fact) => fact.key === key && fact.confirmed));
696
+ const fact = holding[0].stage.facts.find((candidate) => candidate.key === key);
697
+ const where = holding.length === stages.length && stages.length > 2 ? `all ${stages.length} stages` : joinNames(holding.map(({ stage }) => stage.name));
698
+ return { text: `${fact?.label ?? key} ${fact?.short ?? ""} at ${where}`, tone: "good" as const };
699
+ });
700
+ lines.unshift(...agreed);
701
+ if (expected.length > 0) return pick(`${subjectOf(subject)} is the requested one`, missing.length > 0 ? "warn" : "good");
702
+ return pick(`${subjectOf(subject)} matches ${compared[0].index === 0 ? "source" : compared[0].stage.name}`, missing.length > 0 ? "warn" : "good");
703
+ }
704
+
705
+ // ---------------------------------------------------------------------------
706
+ // Summaries
707
+
708
+ /** "5 stages · 3 with a receipt · 6m 11s (4 of 5 timed)". */
709
+ export function chainSummary(provenance: Provenance): string {
710
+ const { stages, totalMs, timed } = provenance;
711
+ const counted = stages.filter((stage) => stage.receipt !== "skipped");
712
+ const receipts = counted.filter((stage) => stage.receipt === "receipt").length;
713
+ const parts = [`${stages.length} ${stages.length === 1 ? "stage" : "stages"}`];
714
+ if (receipts < counted.length) parts.push(`${receipts} with a receipt`);
715
+ if (totalMs !== null) parts.push(`${formatElapsed(totalMs)}${timed < counted.length ? ` (${timed} of ${counted.length} timed)` : ""}`);
716
+ return parts.join(" · ");
717
+ }
718
+
719
+ /** What a stage without a receipt says instead of its status. */
720
+ export const RECEIPT_LABEL: Readonly<Record<Exclude<Receipt, "receipt">, string>> = {
721
+ none: "no receipt",
722
+ waiting: "waiting",
723
+ "not-reached": "not reached",
724
+ skipped: "skipped",
725
+ };