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,887 @@
1
+ // Tracking data for Board, Roadmap and Status: reads what models write and
2
+ // what `bb tasks list --json`, `gh issue list --json` and `gh pr list --json`
3
+ // print into one card/item shape, and the calendar maths they share. Dates are
4
+ // whole days (days since 1970-01-01, UTC), so no timezone ever moves a due
5
+ // date; "today" is always passed in, never read from the clock here.
6
+ import { isPlainObject } from "./evaluate";
7
+ import type { Value } from "./types";
8
+
9
+ export type Read<T> = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: string };
10
+
11
+ const DAY_MS = 86_400_000;
12
+
13
+ /** A field of a loaded or written object: absent fields are undefined. */
14
+ type Field = Value | undefined;
15
+
16
+ /** A plain object's fields by key, or null for anything else (nodes, actions, lists, scalars). */
17
+ function record(value: Field): { readonly [key: string]: Value } | null {
18
+ // Narrowing keeps structurally compatible Action shapes; widen to read by key.
19
+ return value !== undefined && isPlainObject(value) ? value : null;
20
+ }
21
+
22
+ /** A trimmed string from a string or number; null for anything else or blank. */
23
+ function str(value: Field): string | null {
24
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
25
+ if (typeof value !== "string") return null;
26
+ const trimmed = value.trim();
27
+ return trimmed === "" ? null : trimmed;
28
+ }
29
+
30
+ function plural(count: number, noun: string): string {
31
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
32
+ }
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // Days
36
+
37
+ /** `yyyy-mm-dd` (or an ISO date-time, read by its date part) as a day number; null for anything else or an impossible date. */
38
+ export function parseDay(value: Field): number | null {
39
+ if (typeof value !== "string") return null;
40
+ const match = /^\s*(\d{4})-(\d{1,2})-(\d{1,2})(?:$|[T\s])/.exec(value);
41
+ if (match === null) return null;
42
+ const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
43
+ const date = new Date(Date.UTC(year, month - 1, day));
44
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return null;
45
+ return date.getTime() / DAY_MS;
46
+ }
47
+
48
+ /** The user's calendar day at `now` (their wall clock), as a day number. */
49
+ export function localDay(now: Date): number {
50
+ return Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()) / DAY_MS;
51
+ }
52
+
53
+ function utc(day: number): Date {
54
+ return new Date(day * DAY_MS);
55
+ }
56
+
57
+ /** `2026-10-10`. */
58
+ export function isoDay(day: number): string {
59
+ return utc(day).toISOString().slice(0, 10);
60
+ }
61
+
62
+ const SHORT = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", timeZone: "UTC" });
63
+ const LONG = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", year: "numeric", timeZone: "UTC" });
64
+ const MONTH = new Intl.DateTimeFormat("en-US", { month: "short", timeZone: "UTC" });
65
+ const MONTH_YEAR = new Intl.DateTimeFormat("en-US", { month: "short", year: "numeric", timeZone: "UTC" });
66
+ const WEEKDAY = new Intl.DateTimeFormat("en-US", { weekday: "short", month: "short", day: "numeric", year: "numeric", timeZone: "UTC" });
67
+
68
+ /** "Oct 10", with the year when it isn't `today`'s year. */
69
+ export function formatDay(day: number, today: number): string {
70
+ return (utc(day).getUTCFullYear() === utc(today).getUTCFullYear() ? SHORT : LONG).format(utc(day));
71
+ }
72
+
73
+ /** "Sat, Oct 10, 2026": the exact date, for titles. */
74
+ export function fullDay(day: number): string {
75
+ return WEEKDAY.format(utc(day));
76
+ }
77
+
78
+ /** "Oct 1 – Oct 20" (or one date when both are the same day). */
79
+ export function formatRange(start: number, end: number, today: number): string {
80
+ return start === end ? formatDay(start, today) : `${formatDay(start, today)} – ${formatDay(end, today)}`;
81
+ }
82
+
83
+ /** Days in a duration as people say it: "1 day", "12 days", "3 weeks" (whole weeks only). */
84
+ export function formatDays(days: number): string {
85
+ if (days >= 14 && days % 7 === 0) return plural(days / 7, "week");
86
+ return plural(days, "day");
87
+ }
88
+
89
+ /**
90
+ * A due date relative to `today`: "due today", "due tomorrow", "in 5 days",
91
+ * "in 3 weeks", "in 4 months"; past it "1 day late", "3 days late", "2
92
+ * weeks late". `late` is true for any past date (the caller decides whether
93
+ * a finished item is late).
94
+ */
95
+ export function relativeDue(due: number, today: number): { readonly text: string; readonly late: boolean } {
96
+ const delta = due - today;
97
+ if (delta === 0) return { text: "due today", late: false };
98
+ if (delta === 1) return { text: "due tomorrow", late: false };
99
+ const span = Math.abs(delta);
100
+ const amount =
101
+ span < 14 ? plural(span, "day") : span < 60 ? plural(Math.round(span / 7), "week") : plural(Math.round(span / 30.44), "month");
102
+ return delta > 0 ? { text: `in ${amount}`, late: false } : { text: `${amount} late`, late: true };
103
+ }
104
+
105
+ /** Monday on or before `day`. */
106
+ function mondayOf(day: number): number {
107
+ // Day 0 (1970-01-01) was a Thursday.
108
+ return day - ((utc(day).getUTCDay() + 6) % 7);
109
+ }
110
+
111
+ function monthStart(day: number, add = 0): number {
112
+ const date = utc(day);
113
+ return Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + add, 1) / DAY_MS;
114
+ }
115
+
116
+ // ---------------------------------------------------------------------------
117
+ // People
118
+
119
+ /** A person from a name, a login, or `{name?, login?}` (GitHub's assignees and authors). */
120
+ function person(value: Field): string | null {
121
+ const fields = record(value);
122
+ if (fields !== null) return str(fields.name) ?? str(fields.login) ?? str(fields.displayName) ?? str(fields.email);
123
+ return str(value)?.replace(/^@/, "") ?? null;
124
+ }
125
+
126
+ function people(value: Field): string[] {
127
+ const all = Array.isArray(value) ? value.map(person) : [person(value)];
128
+ return [...new Set(all.filter((name): name is string => name !== null))];
129
+ }
130
+
131
+ /** "Ana Lima" → "AL", "lukesandberg" → "L", "ana.lima@x.com" → "AL". */
132
+ export function initials(name: string): string {
133
+ const words = name
134
+ .replace(/@.*$/, "")
135
+ .split(/[\s._-]+/)
136
+ .filter((word) => /\p{L}|\p{N}/u.test(word));
137
+ if (words.length === 0) return "?";
138
+ const first = [...words[0]!][0]!;
139
+ const last = words.length > 1 ? [...words[words.length - 1]!][0]! : "";
140
+ return (first + last).toUpperCase();
141
+ }
142
+
143
+ // ---------------------------------------------------------------------------
144
+ // Board
145
+
146
+ export type Priority = "urgent" | "high" | "medium" | "low" | "none";
147
+
148
+ export interface BoardLabel {
149
+ readonly name: string;
150
+ /** `#rrggbb` when the source gives a colour (GitHub does). */
151
+ readonly color: string | null;
152
+ }
153
+
154
+ export interface BoardCard {
155
+ /** Unique within the board, for React keys. */
156
+ readonly id: string;
157
+ /** What the card shows as its handle: `ORB-12`, `#99954`, an `id` given. */
158
+ readonly key: string | null;
159
+ readonly title: string;
160
+ readonly status: string;
161
+ readonly detail: string | null;
162
+ readonly priority: Priority | null;
163
+ readonly labels: readonly BoardLabel[];
164
+ readonly owners: readonly string[];
165
+ readonly due: number | null;
166
+ readonly url: string | null;
167
+ /** Agents working on a BB task now. */
168
+ readonly agents: number;
169
+ /** The parent task's key, for BB sub-tasks. */
170
+ readonly parent: string | null;
171
+ /** When GitHub last saw a change (ms), for issues and pull requests. */
172
+ readonly updated: number | null;
173
+ /** Finished one way or another (done, merged, closed, canceled): never late. */
174
+ readonly closed: boolean;
175
+ }
176
+
177
+ export interface BoardColumn {
178
+ readonly status: string;
179
+ readonly label: string;
180
+ readonly cards: readonly BoardCard[];
181
+ }
182
+
183
+ export type BoardSource = "list" | "tasks" | "issues" | "pulls";
184
+
185
+ export interface Board {
186
+ readonly source: BoardSource;
187
+ readonly columns: readonly BoardColumn[];
188
+ /** Cards on the board (in the columns shown). */
189
+ readonly total: number;
190
+ /** Cards whose status is not among the `columns` asked for. */
191
+ readonly hidden: number;
192
+ /** Items left out for having no title. */
193
+ readonly skipped: number;
194
+ /** `bb tasks list` stopped at its limit: there are more tasks than these. */
195
+ readonly truncated: boolean;
196
+ }
197
+
198
+ /** The known workflow, in order, with the label each column shows. */
199
+ const WORKFLOW: readonly (readonly [string, string])[] = [
200
+ ["triage", "Triage"],
201
+ ["backlog", "Backlog"],
202
+ ["todo", "Todo"],
203
+ ["draft", "Draft"],
204
+ ["open", "Open"],
205
+ ["in_progress", "In progress"],
206
+ ["in_review", "In review"],
207
+ ["blocked", "Blocked"],
208
+ ["done", "Done"],
209
+ ["merged", "Merged"],
210
+ ["closed", "Closed"],
211
+ ["canceled", "Canceled"],
212
+ ];
213
+ const WORKFLOW_LABEL: Readonly<Record<string, string>> = Object.fromEntries(WORKFLOW);
214
+ const WORKFLOW_ORDER: Readonly<Record<string, number>> = Object.fromEntries(WORKFLOW.map(([status], index) => [status, index]));
215
+ const NO_STATUS = "no_status";
216
+
217
+ const STATUS_ALIASES: Readonly<Record<string, string>> = {
218
+ to_do: "todo",
219
+ not_started: "todo",
220
+ doing: "in_progress",
221
+ started: "in_progress",
222
+ active: "in_progress",
223
+ wip: "in_progress",
224
+ progress: "in_progress",
225
+ review: "in_review",
226
+ in_code_review: "in_review",
227
+ code_review: "in_review",
228
+ complete: "done",
229
+ completed: "done",
230
+ finished: "done",
231
+ shipped: "done",
232
+ resolved: "done",
233
+ cancelled: "canceled",
234
+ wontfix: "canceled",
235
+ won_t_fix: "canceled",
236
+ duplicate: "canceled",
237
+ };
238
+
239
+ /** Columns that hold finished work: their cards are never late. */
240
+ const CLOSED_STATUSES: Readonly<Record<string, true>> = { done: true, merged: true, closed: true, canceled: true };
241
+
242
+ /** A status as a column id: `In Progress`, `in-progress` and `doing` are all `in_progress`; unknown ones keep their own slug. */
243
+ export function boardStatus(value: Field): string {
244
+ const raw = str(value);
245
+ if (raw === null) return NO_STATUS;
246
+ const slug = raw
247
+ .toLowerCase()
248
+ .replace(/[^\p{L}\p{N}]+/gu, "_")
249
+ .replace(/^_+|_+$/g, "");
250
+ if (slug === "") return NO_STATUS;
251
+ return STATUS_ALIASES[slug] ?? slug;
252
+ }
253
+
254
+ function statusLabel(status: string, given: ReadonlyMap<string, string>): string {
255
+ if (status === NO_STATUS) return "No status";
256
+ const known = Object.hasOwn(WORKFLOW_LABEL, status) ? WORKFLOW_LABEL[status] : undefined;
257
+ if (known !== undefined) return known;
258
+ const raw = given.get(status) ?? status.replace(/_/g, " ");
259
+ return raw.charAt(0).toUpperCase() + raw.slice(1);
260
+ }
261
+
262
+ const PRIORITY_ALIASES: Readonly<Record<string, Priority>> = {
263
+ urgent: "urgent",
264
+ critical: "urgent",
265
+ p0: "urgent",
266
+ high: "high",
267
+ p1: "high",
268
+ medium: "medium",
269
+ normal: "medium",
270
+ p2: "medium",
271
+ low: "low",
272
+ p3: "low",
273
+ p4: "low",
274
+ none: "none",
275
+ no_priority: "none",
276
+ };
277
+
278
+ /** Linear's numbers too: 1 urgent, 2 high, 3 medium, 4 low, 0 none. */
279
+ export function priority(value: Field): Priority | null {
280
+ if (typeof value === "number") return (["none", "urgent", "high", "medium", "low"] as const)[value] ?? null;
281
+ const raw = str(value);
282
+ if (raw === null) return null;
283
+ return PRIORITY_ALIASES[raw.toLowerCase().replace(/[^a-z0-9]+/g, "_")] ?? null;
284
+ }
285
+
286
+ function hexColor(value: Field): string | null {
287
+ const raw = str(value);
288
+ if (raw === null) return null;
289
+ const match = /^#?([0-9a-f]{6}|[0-9a-f]{3})$/i.exec(raw);
290
+ return match === null ? null : `#${match[1]!.toLowerCase()}`;
291
+ }
292
+
293
+ function labels(value: Field): BoardLabel[] {
294
+ const all = Array.isArray(value) ? value : value === undefined || value === null ? [] : [value];
295
+ const out: BoardLabel[] = [];
296
+ for (const item of all) {
297
+ const fields = record(item);
298
+ const name = fields === null ? str(item) : str(fields.name);
299
+ if (name === null || out.some((label) => label.name === name)) continue;
300
+ out.push({ name, color: fields === null ? null : hexColor(fields.color) });
301
+ }
302
+ return out;
303
+ }
304
+
305
+ function webUrl(value: Field): string | null {
306
+ const raw = str(value);
307
+ return raw !== null && /^https?:\/\/\S+$/i.test(raw) ? raw : null;
308
+ }
309
+
310
+ function updatedAt(value: Field): number | null {
311
+ const raw = str(value);
312
+ if (raw === null) return null;
313
+ const ms = Date.parse(raw);
314
+ return Number.isFinite(ms) ? ms : null;
315
+ }
316
+
317
+ type RawCard = Omit<BoardCard, "id" | "closed"> & { readonly given: string | null };
318
+
319
+ function listCard(value: Value): RawCard | null {
320
+ if (typeof value === "string") {
321
+ const title = str(value);
322
+ return title === null ? null : { ...EMPTY_CARD, title };
323
+ }
324
+ const item = record(value);
325
+ if (item === null) return null;
326
+ const title = str(item.title) ?? str(item.name);
327
+ if (title === null) return null;
328
+ return {
329
+ key: str(item.key) ?? str(item.id),
330
+ title,
331
+ status: boardStatus(item.status ?? item.state),
332
+ given: str(item.status ?? item.state),
333
+ detail: str(item.detail) ?? str(item.description) ?? str(item.note),
334
+ priority: priority(item.priority),
335
+ labels: labels(item.labels ?? item.tags),
336
+ owners: people(item.owner ?? item.assignee ?? item.assignees),
337
+ due: parseDay(item.due ?? item.dueDate),
338
+ url: webUrl(item.url),
339
+ agents: typeof item.agentsWorking === "number" && item.agentsWorking > 0 ? item.agentsWorking : 0,
340
+ parent: null,
341
+ updated: null,
342
+ };
343
+ }
344
+
345
+ const EMPTY_CARD: RawCard = {
346
+ key: null,
347
+ title: "",
348
+ status: NO_STATUS,
349
+ given: null,
350
+ detail: null,
351
+ priority: null,
352
+ labels: [],
353
+ owners: [],
354
+ due: null,
355
+ url: null,
356
+ agents: 0,
357
+ parent: null,
358
+ updated: null,
359
+ };
360
+
361
+ function taskCard(value: Value, parents: ReadonlyMap<string, string>): RawCard | null {
362
+ const card = listCard(value);
363
+ const item = record(value);
364
+ if (card === null || item === null) return card;
365
+ const parentId = str(item.parentTaskId);
366
+ return { ...card, key: str(item.key) ?? card.key, parent: parentId === null ? null : (parents.get(parentId) ?? null) };
367
+ }
368
+
369
+ function githubCard(value: Value, pulls: boolean): RawCard | null {
370
+ const item = record(value);
371
+ if (item === null) return null;
372
+ const title = str(item.title);
373
+ if (title === null) return null;
374
+ const state = str(item.state)?.toLowerCase() ?? null;
375
+ const status = pulls && state === "open" && item.isDraft === true ? "draft" : boardStatus(state);
376
+ const assignees = people(item.assignees);
377
+ return {
378
+ ...EMPTY_CARD,
379
+ key: typeof item.number === "number" ? `#${item.number}` : null,
380
+ title,
381
+ status,
382
+ given: state,
383
+ detail: str(item.body),
384
+ labels: labels(item.labels),
385
+ // A pull request without assignees is its author's.
386
+ owners: assignees.length === 0 && pulls ? people(item.author) : assignees,
387
+ url: webUrl(item.url),
388
+ updated: updatedAt(item.updatedAt),
389
+ };
390
+ }
391
+
392
+ function isGithubItem(value: Value): boolean {
393
+ const item = record(value);
394
+ return item !== null && typeof item.number === "number" && typeof item.state === "string" && typeof item.title === "string";
395
+ }
396
+
397
+ function isPull(value: Value): boolean {
398
+ const item = record(value);
399
+ return (
400
+ item !== null &&
401
+ ("isDraft" in item || "mergedAt" in item || "headRefName" in item || item.state === "MERGED" || /\/pull\/\d+/.test(String(item.url ?? "")))
402
+ );
403
+ }
404
+
405
+ /**
406
+ * A board from a list of cards (`{title, status?, key?|id?, …}` or bare
407
+ * titles), `bb tasks list --json` (`{tasks}`), or `gh issue list --json` /
408
+ * `gh pr list --json`. `columns` names the statuses to show, in order (empty
409
+ * ones too); without it the known workflow order applies, unknown statuses
410
+ * follow in first-seen order, and empty columns are left out.
411
+ */
412
+ export function readBoard(items: Field, columns?: Field): Read<Board> {
413
+ let source: BoardSource = "list";
414
+ let raw: (RawCard | null)[];
415
+ let truncated = false;
416
+ const report = record(items);
417
+ const tasks = report?.tasks;
418
+ if (report !== null && Array.isArray(tasks)) {
419
+ source = "tasks";
420
+ const parents = new Map<string, string>();
421
+ for (const value of tasks as readonly Value[]) {
422
+ const task = record(value);
423
+ if (task === null) continue;
424
+ const id = str(task.id);
425
+ const key = str(task.key);
426
+ if (id !== null && key !== null) parents.set(id, key);
427
+ }
428
+ raw = (tasks as readonly Value[]).map((task) => taskCard(task, parents));
429
+ truncated = str(report.nextCursor) !== null;
430
+ } else if (Array.isArray(items)) {
431
+ const github = items.length > 0 && items.every(isGithubItem);
432
+ const pulls = github && items.some(isPull);
433
+ if (github) source = pulls ? "pulls" : "issues";
434
+ raw = items.map((item) => (github ? githubCard(item, pulls) : listCard(item)));
435
+ } else if (items === undefined || items === null) {
436
+ return { ok: false, error: "Board needs a list of items, or Load a bb tasks or gh issue/pr list --json file" };
437
+ } else {
438
+ return {
439
+ ok: false,
440
+ error: "Board needs a list of {title, status?} items, bb tasks list --json ({tasks: […]}) or gh issue/pr list --json",
441
+ };
442
+ }
443
+
444
+ const given = new Map<string, string>();
445
+ const cards: BoardCard[] = [];
446
+ const seen = new Map<string, number>();
447
+ for (const card of raw) {
448
+ if (card === null) continue;
449
+ if (card.given !== null && !given.has(card.status)) given.set(card.status, card.given);
450
+ const base = card.key ?? card.title;
451
+ const repeat = seen.get(base) ?? 0;
452
+ seen.set(base, repeat + 1);
453
+ const { given: _given, ...rest } = card;
454
+ cards.push({ ...rest, id: repeat === 0 ? base : `${base}~${repeat}`, closed: Object.hasOwn(CLOSED_STATUSES, card.status) });
455
+ }
456
+ const skipped = raw.length - cards.length;
457
+
458
+ const named = Array.isArray(columns) ? [...new Set(columns.map(boardStatus).filter((status) => status !== NO_STATUS))] : [];
459
+ for (const column of Array.isArray(columns) ? columns : []) {
460
+ const status = boardStatus(column);
461
+ const text = str(column);
462
+ if (text !== null && !given.has(status)) given.set(status, text);
463
+ }
464
+ const byStatus = new Map<string, BoardCard[]>();
465
+ for (const card of cards) {
466
+ const bucket = byStatus.get(card.status);
467
+ if (bucket === undefined) byStatus.set(card.status, [card]);
468
+ else bucket.push(card);
469
+ }
470
+ let order: string[];
471
+ if (named.length > 0) {
472
+ order = named;
473
+ } else {
474
+ const firstSeen = [...byStatus.keys()];
475
+ order = firstSeen.sort((a, b) => rank(a, firstSeen) - rank(b, firstSeen));
476
+ }
477
+ const shown = order.map((status) => ({ status, label: statusLabel(status, given), cards: byStatus.get(status) ?? [] }));
478
+ const total = shown.reduce((sum, column) => sum + column.cards.length, 0);
479
+ return { ok: true, value: { source, columns: shown, total, hidden: cards.length - total, skipped, truncated } };
480
+ }
481
+
482
+ /** Workflow statuses in their order, then unknown ones as first seen, then cards without a status. */
483
+ function rank(status: string, firstSeen: readonly string[]): number {
484
+ if (status === NO_STATUS) return 10_000;
485
+ return Object.hasOwn(WORKFLOW_ORDER, status) ? WORKFLOW_ORDER[status]! : 100 + firstSeen.indexOf(status);
486
+ }
487
+
488
+ /** A card is late when its due day has passed and it isn't finished. */
489
+ export function cardLate(card: BoardCard, today: number): boolean {
490
+ return card.due !== null && !card.closed && card.due < today;
491
+ }
492
+
493
+ /** The column shown first on a narrow screen: where the work is happening, else the first one with cards. */
494
+ export function firstColumn(board: Board): number {
495
+ const busy = ["in_progress", "in_review", "open", "todo"]
496
+ .map((status) => board.columns.findIndex((column) => column.status === status && column.cards.length > 0))
497
+ .find((index) => index >= 0);
498
+ if (busy !== undefined) return busy;
499
+ const filled = board.columns.findIndex((column) => column.cards.length > 0);
500
+ return filled < 0 ? 0 : filled;
501
+ }
502
+
503
+ // ---------------------------------------------------------------------------
504
+ // Roadmap
505
+
506
+ export type RoadmapStatus = "done" | "active" | "planned" | "blocked" | "at-risk";
507
+
508
+ const ROADMAP_STATUS: Readonly<Record<string, RoadmapStatus>> = {
509
+ done: "done",
510
+ complete: "done",
511
+ completed: "done",
512
+ shipped: "done",
513
+ active: "active",
514
+ in_progress: "active",
515
+ doing: "active",
516
+ started: "active",
517
+ on_track: "active",
518
+ planned: "planned",
519
+ todo: "planned",
520
+ not_started: "planned",
521
+ upcoming: "planned",
522
+ blocked: "blocked",
523
+ at_risk: "at-risk",
524
+ risk: "at-risk",
525
+ off_track: "at-risk",
526
+ late: "at-risk",
527
+ };
528
+
529
+ export interface RoadmapItem {
530
+ /** Position in the given list. */
531
+ readonly index: number;
532
+ readonly title: string;
533
+ readonly lane: string;
534
+ /** First day. */
535
+ readonly start: number;
536
+ /** Last day (inclusive); the same as `start` for a milestone. */
537
+ readonly end: number;
538
+ readonly milestone: boolean;
539
+ readonly status: RoadmapStatus;
540
+ readonly owner: string | null;
541
+ /** 0–100; null when not given (and the item isn't done). */
542
+ readonly progress: number | null;
543
+ readonly detail: string | null;
544
+ /** The title named in `after`, as written. */
545
+ readonly after: string | null;
546
+ /** The item `after` names, by `index`; null when it names nothing on the roadmap. */
547
+ readonly afterIndex: number | null;
548
+ /** Starts before the item it comes after has ended. */
549
+ readonly overlap: boolean;
550
+ /** Ended (or its milestone passed) before today without being done. */
551
+ readonly late: boolean;
552
+ }
553
+
554
+ export interface RoadmapLane {
555
+ readonly name: string;
556
+ readonly items: readonly RoadmapItem[];
557
+ }
558
+
559
+ export type RoadmapScale = "days" | "weeks" | "months";
560
+
561
+ export interface RoadmapTick {
562
+ readonly day: number;
563
+ readonly label: string;
564
+ /** A month or year boundary, drawn stronger. */
565
+ readonly major: boolean;
566
+ }
567
+
568
+ export interface Roadmap {
569
+ readonly lanes: readonly RoadmapLane[];
570
+ readonly items: readonly RoadmapItem[];
571
+ /** The axis: first day shown, and the day after the last. */
572
+ readonly from: number;
573
+ readonly to: number;
574
+ readonly scale: RoadmapScale;
575
+ readonly ticks: readonly RoadmapTick[];
576
+ readonly today: number;
577
+ readonly skipped: readonly { readonly title: string; readonly reason: string }[];
578
+ }
579
+
580
+ function roadmapStatus(value: Field): RoadmapStatus | null {
581
+ const raw = str(value);
582
+ if (raw === null) return null;
583
+ return ROADMAP_STATUS[raw.toLowerCase().replace(/[^a-z0-9]+/g, "_")] ?? null;
584
+ }
585
+
586
+ /** 0–100 from a number or "60%"; fractions below 1 are read as shares (0.6 → 60). */
587
+ function percent(value: Field): number | null {
588
+ let number: number | null = null;
589
+ if (typeof value === "number" && Number.isFinite(value)) number = value;
590
+ else if (typeof value === "string" && /^\s*\d+(\.\d+)?\s*%?\s*$/.test(value)) number = Number.parseFloat(value);
591
+ if (number === null) return null;
592
+ if (number > 0 && number < 1 && !(typeof value === "string" && value.includes("%"))) number *= 100;
593
+ return Math.min(100, Math.max(0, number));
594
+ }
595
+
596
+ /** A share of the axis, 0–1, for a day (the start of that day). */
597
+ export function axisShare(day: number, roadmap: Pick<Roadmap, "from" | "to">): number {
598
+ return (day - roadmap.from) / (roadmap.to - roadmap.from);
599
+ }
600
+
601
+ /** The scale an axis of `span` days reads best in. */
602
+ export function roadmapScale(span: number): RoadmapScale {
603
+ return span <= 31 ? "days" : span <= 182 ? "weeks" : "months";
604
+ }
605
+
606
+ function axis(lo: number, hi: number, today: number): Pick<Roadmap, "from" | "to" | "scale" | "ticks"> {
607
+ // Today joins the axis when it is near the plan, so the line has a place.
608
+ const span = Math.max(1, hi - lo);
609
+ if (today >= lo - span * 0.3 && today < lo) lo = today;
610
+ if (today >= hi && today < hi + span * 0.3) hi = today + 1;
611
+ const scale = roadmapScale(hi - lo);
612
+ const ticks: RoadmapTick[] = [];
613
+ let from: number;
614
+ let to: number;
615
+ if (scale === "days") {
616
+ from = lo - 1;
617
+ to = hi + 1;
618
+ for (let day = from; day < to; day += 1) {
619
+ const first = utc(day).getUTCDate() === 1 || day === from;
620
+ ticks.push({ day, label: first ? SHORT.format(utc(day)) : String(utc(day).getUTCDate()), major: utc(day).getUTCDate() === 1 });
621
+ }
622
+ } else if (scale === "weeks") {
623
+ from = mondayOf(lo);
624
+ to = mondayOf(hi - 1) + 7;
625
+ let month = -1;
626
+ for (let day = from; day < to; day += 7) {
627
+ const date = utc(day);
628
+ const newMonth = date.getUTCMonth() !== month;
629
+ month = date.getUTCMonth();
630
+ ticks.push({ day, label: newMonth ? SHORT.format(date) : String(date.getUTCDate()), major: newMonth && day !== from });
631
+ }
632
+ } else {
633
+ from = monthStart(lo);
634
+ to = monthStart(hi - 1, 1);
635
+ for (let day = from; day < to; day = monthStart(day, 1)) {
636
+ const january = utc(day).getUTCMonth() === 0;
637
+ ticks.push({ day, label: (january || day === from ? MONTH_YEAR : MONTH).format(utc(day)), major: january });
638
+ }
639
+ }
640
+ return { from, to, scale, ticks };
641
+ }
642
+
643
+ /**
644
+ * A roadmap from `{title, start, end | days, lane? | group?, status?,
645
+ * owner?, milestone?, after?, progress?}` items (dates `yyyy-mm-dd`). A
646
+ * milestone is `milestone: "yyyy-mm-dd"` (or `milestone: true` with `start`).
647
+ * `days` counts the start day (start + 1 day ends the same day). Without a
648
+ * status an item is done at 100 %, active when today is inside it, else
649
+ * planned. Items with no usable dates are left out and listed in `skipped`.
650
+ */
651
+ export function readRoadmap(items: Field, today: number): Read<Roadmap> {
652
+ if (!Array.isArray(items)) return { ok: false, error: "Roadmap needs a list of {title, start, end} items" };
653
+ const skipped: { title: string; reason: string }[] = [];
654
+ type Draft = Omit<RoadmapItem, "afterIndex" | "overlap" | "late">;
655
+ const drafts: Draft[] = [];
656
+ items.forEach((value, index) => {
657
+ const item = record(value);
658
+ if (item === null) {
659
+ skipped.push({ title: str(value) ?? `Item ${index + 1}`, reason: "not an item" });
660
+ return;
661
+ }
662
+ const title = str(item.title) ?? str(item.name);
663
+ if (title === null) {
664
+ skipped.push({ title: `Item ${index + 1}`, reason: "no title" });
665
+ return;
666
+ }
667
+ const milestoneDay = parseDay(item.milestone);
668
+ const milestone = milestoneDay !== null || item.milestone === true;
669
+ let start = milestoneDay ?? parseDay(item.start ?? item.from ?? item.date);
670
+ let end: number | null;
671
+ if (milestone) {
672
+ end = start;
673
+ } else {
674
+ end = parseDay(item.end ?? item.to ?? item.due);
675
+ const days = typeof item.days === "number" && Number.isFinite(item.days) && item.days >= 1 ? Math.round(item.days) : null;
676
+ if (end === null && start !== null && days !== null) end = start + days - 1;
677
+ if (start === null && end !== null && days !== null) start = end - days + 1;
678
+ }
679
+ if (start === null || end === null) {
680
+ skipped.push({ title, reason: start === null ? "no start date (yyyy-mm-dd)" : "no end date or days" });
681
+ return;
682
+ }
683
+ if (end < start) {
684
+ skipped.push({ title, reason: `ends (${isoDay(end)}) before it starts (${isoDay(start)})` });
685
+ return;
686
+ }
687
+ const given = roadmapStatus(item.status);
688
+ const progressGiven = percent(item.progress);
689
+ const status: RoadmapStatus =
690
+ given ?? (progressGiven === 100 ? "done" : today >= start && today <= end && !milestone ? "active" : "planned");
691
+ drafts.push({
692
+ index,
693
+ title,
694
+ lane: str(item.lane) ?? str(item.group) ?? "",
695
+ start,
696
+ end,
697
+ milestone,
698
+ status,
699
+ owner: person(item.owner ?? item.assignee),
700
+ progress: progressGiven ?? (status === "done" && !milestone ? 100 : null),
701
+ detail: str(item.detail) ?? str(item.note) ?? str(item.description),
702
+ after: str(item.after) ?? str(item.dependsOn) ?? str(item.depends),
703
+ });
704
+ });
705
+ if (drafts.length === 0) {
706
+ return items.length === 0
707
+ ? { ok: true, value: { lanes: [], items: [], from: today, to: today + 1, scale: "days", ticks: [], today, skipped } }
708
+ : { ok: false, error: `No roadmap item has usable dates: ${skipped[0]!.title} has ${skipped[0]!.reason}` };
709
+ }
710
+
711
+ const byTitle = new Map<string, Draft>();
712
+ for (const draft of drafts) {
713
+ const key = draft.title.toLowerCase();
714
+ if (!byTitle.has(key)) byTitle.set(key, draft);
715
+ }
716
+ const resolved: RoadmapItem[] = drafts.map((draft) => {
717
+ const before = draft.after === null ? undefined : byTitle.get(draft.after.toLowerCase());
718
+ const predecessor = before === undefined || before === draft ? null : before;
719
+ return {
720
+ ...draft,
721
+ afterIndex: predecessor?.index ?? null,
722
+ overlap: predecessor !== null && (predecessor.milestone ? draft.start < predecessor.end : draft.start <= predecessor.end),
723
+ late: draft.status !== "done" && draft.end < today,
724
+ };
725
+ });
726
+
727
+ const laneOrder: string[] = [];
728
+ const byLane = new Map<string, RoadmapItem[]>();
729
+ for (const item of resolved) {
730
+ const bucket = byLane.get(item.lane);
731
+ if (bucket === undefined) {
732
+ laneOrder.push(item.lane);
733
+ byLane.set(item.lane, [item]);
734
+ } else bucket.push(item);
735
+ }
736
+ const lo = Math.min(...resolved.map((item) => item.start));
737
+ const hi = Math.max(...resolved.map((item) => item.end)) + 1;
738
+ return {
739
+ ok: true,
740
+ value: {
741
+ lanes: laneOrder.map((name) => ({ name, items: byLane.get(name)! })),
742
+ items: resolved,
743
+ ...axis(lo, hi, today),
744
+ today,
745
+ skipped,
746
+ },
747
+ };
748
+ }
749
+
750
+ export const ROADMAP_STATUS_LABEL: Readonly<Record<RoadmapStatus, string>> = {
751
+ done: "Done",
752
+ active: "Active",
753
+ planned: "Planned",
754
+ blocked: "Blocked",
755
+ "at-risk": "At risk",
756
+ };
757
+
758
+ /** Days an item spans, counting both ends. */
759
+ export function itemDays(item: Pick<RoadmapItem, "start" | "end">): number {
760
+ return item.end - item.start + 1;
761
+ }
762
+
763
+ // ---------------------------------------------------------------------------
764
+ // Status
765
+
766
+ export type Health = "off-track" | "blocked" | "at-risk" | "on-track" | "not-started" | "done";
767
+
768
+ const HEALTH_ALIASES: Readonly<Record<string, Health>> = {
769
+ on_track: "on-track",
770
+ ontrack: "on-track",
771
+ green: "on-track",
772
+ good: "on-track",
773
+ healthy: "on-track",
774
+ active: "on-track",
775
+ in_progress: "on-track",
776
+ at_risk: "at-risk",
777
+ atrisk: "at-risk",
778
+ risk: "at-risk",
779
+ yellow: "at-risk",
780
+ amber: "at-risk",
781
+ warn: "at-risk",
782
+ warning: "at-risk",
783
+ off_track: "off-track",
784
+ offtrack: "off-track",
785
+ red: "off-track",
786
+ behind: "off-track",
787
+ late: "off-track",
788
+ bad: "off-track",
789
+ done: "done",
790
+ complete: "done",
791
+ completed: "done",
792
+ shipped: "done",
793
+ blocked: "blocked",
794
+ not_started: "not-started",
795
+ notstarted: "not-started",
796
+ planned: "not-started",
797
+ todo: "not-started",
798
+ upcoming: "not-started",
799
+ };
800
+
801
+ /** Worst first: what needs attention, then what is fine, then what is finished. */
802
+ export const HEALTH_ORDER: readonly Health[] = ["off-track", "blocked", "at-risk", "on-track", "not-started", "done"];
803
+
804
+ export const HEALTH_LABEL: Readonly<Record<Health, string>> = {
805
+ "off-track": "Off track",
806
+ blocked: "Blocked",
807
+ "at-risk": "At risk",
808
+ "on-track": "On track",
809
+ "not-started": "Not started",
810
+ done: "Done",
811
+ };
812
+
813
+ export function health(value: Field): Health | null {
814
+ const raw = str(value);
815
+ if (raw === null) return null;
816
+ return HEALTH_ALIASES[raw.toLowerCase().replace(/[^a-z0-9]+/g, "_")] ?? null;
817
+ }
818
+
819
+ export interface HealthItem {
820
+ readonly index: number;
821
+ readonly name: string;
822
+ /** Null for a status the reader doesn't know; `given` keeps its text. */
823
+ readonly status: Health | null;
824
+ readonly given: string | null;
825
+ readonly progress: number | null;
826
+ readonly owner: string | null;
827
+ readonly due: number | null;
828
+ readonly note: string | null;
829
+ /** Past due and not done. */
830
+ readonly late: boolean;
831
+ }
832
+
833
+ export interface HealthReport {
834
+ /** Worst first (see HEALTH_ORDER); unknown statuses after at-risk; the given order within each status. */
835
+ readonly items: readonly HealthItem[];
836
+ /** Counts per status in the strip's reading order, non-zero only. */
837
+ readonly counts: readonly { readonly status: Health; readonly count: number }[];
838
+ readonly unknown: number;
839
+ readonly skipped: number;
840
+ }
841
+
842
+ /** The summary strip's reading order. */
843
+ const STRIP_ORDER: readonly Health[] = ["on-track", "at-risk", "off-track", "blocked", "not-started", "done"];
844
+
845
+ function healthRank(status: Health | null): number {
846
+ return status === null ? HEALTH_ORDER.indexOf("at-risk") + 0.5 : HEALTH_ORDER.indexOf(status);
847
+ }
848
+
849
+ /** A health report from `{name, status, progress?, owner?, due?, note?}` items, worst first. */
850
+ export function readStatus(items: Field, today: number): Read<HealthReport> {
851
+ if (!Array.isArray(items)) return { ok: false, error: "Status needs a list of {name, status} items" };
852
+ const read: HealthItem[] = [];
853
+ items.forEach((value, index) => {
854
+ const item = record(value);
855
+ if (item === null) return;
856
+ const name = str(item.name) ?? str(item.title);
857
+ if (name === null) return;
858
+ const status = health(item.status);
859
+ const due = parseDay(item.due ?? item.dueDate);
860
+ read.push({
861
+ index,
862
+ name,
863
+ status,
864
+ given: str(item.status),
865
+ progress: percent(item.progress),
866
+ owner: person(item.owner ?? item.assignee),
867
+ due,
868
+ note: str(item.note) ?? str(item.detail),
869
+ late: due !== null && due < today && status !== "done",
870
+ });
871
+ });
872
+ const sorted = [...read].sort((a, b) => healthRank(a.status) - healthRank(b.status) || a.index - b.index);
873
+ const counts = STRIP_ORDER.map((status) => ({ status, count: read.filter((item) => item.status === status).length })).filter(
874
+ (entry) => entry.count > 0,
875
+ );
876
+ return {
877
+ ok: true,
878
+ value: { items: sorted, counts, unknown: read.filter((item) => item.status === null).length, skipped: items.length - read.length },
879
+ };
880
+ }
881
+
882
+ /** "4 on track · 1 at risk · 1 off track". */
883
+ export function healthSummary(report: HealthReport): string {
884
+ const parts = report.counts.map(({ status, count }) => `${count} ${HEALTH_LABEL[status].toLowerCase()}`);
885
+ if (report.unknown > 0) parts.push(`${report.unknown} other`);
886
+ return parts.join(" · ");
887
+ }