create-pmos 0.610.0 → 0.612.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 (164) hide show
  1. package/package.json +135 -27
  2. package/payload/KIT-VERSION +2 -2
  3. package/payload/planning/evals/eval-calibration.md +1 -1
  4. package/payload/planning/okrs/health-budgets.md +1 -1
  5. package/payload/state/discoveries.md +1 -1
  6. package/payload/state/log.md +1 -1
  7. package/workspace/board-columns-test.js +2 -1
  8. package/workspace/board-deliveries-test.js +2 -1
  9. package/workspace/board-programs-test.js +7 -3
  10. package/workspace/board-publish-ui-test.js +2 -1
  11. package/workspace/board-shared-ui-test.js +2 -1
  12. package/workspace/board-workflows-test.js +2 -1
  13. package/workspace/briefing-premium-board-test.js +3 -2
  14. package/workspace/briefing-premium-calendar-2-test.js +3 -2
  15. package/workspace/briefing-premium-calendar-3-test.js +1 -1
  16. package/workspace/briefing-premium-calendar-test.js +1 -1
  17. package/workspace/briefing-premium-doc-edit-test.js +2 -1
  18. package/workspace/briefing-premium-insights-test.js +4 -3
  19. package/workspace/briefing-premium-test.js +3 -2
  20. package/workspace/calm-timeline-test.js +2 -1
  21. package/workspace/changelog.json +134 -0
  22. package/workspace/core-docs-workflow-test.js +2 -1
  23. package/workspace/ev-everywhere-test.js +3 -2
  24. package/workspace/guide/home.md +2 -2
  25. package/workspace/guide/index.md +4 -0
  26. package/workspace/guide/layout.md +97 -3
  27. package/workspace/guide/programs.md +16 -2
  28. package/workspace/guide/routine.md +65 -0
  29. package/workspace/guide/scheduled-tasks-and-cli.md +1 -1
  30. package/workspace/guide/settings-and-switches.md +2 -2
  31. package/workspace/impact-screens-test.js +3 -1
  32. package/workspace/ini-workflow-test.js +2 -1
  33. package/workspace/lib/board-premium.js +12 -4
  34. package/workspace/lib/classic-board.js +15 -0
  35. package/workspace/lib/cos-chat.js +1 -0
  36. package/workspace/lib/doc-library.js +43 -12
  37. package/workspace/lib/doc-path.js +30 -0
  38. package/workspace/lib/engine/boards.js +4 -2
  39. package/workspace/lib/events-place.js +368 -0
  40. package/workspace/lib/events-stated.js +129 -0
  41. package/workspace/lib/initiative-chat.js +1 -0
  42. package/workspace/lib/insights-premium.js +13 -1
  43. package/workspace/lib/library-find.js +162 -0
  44. package/workspace/lib/library-items.js +250 -0
  45. package/workspace/lib/notes.js +9 -1
  46. package/workspace/lib/plan-items.js +17 -3
  47. package/workspace/lib/route-modules.js +4 -0
  48. package/workspace/lib/routes-artefacts.js +5 -2
  49. package/workspace/lib/routes-boards.js +6 -2
  50. package/workspace/lib/routes-doc-sheet.js +1 -0
  51. package/workspace/lib/routes-events-place.js +203 -0
  52. package/workspace/lib/routes-library.js +155 -0
  53. package/workspace/lib/routes-pop-work.js +39 -0
  54. package/workspace/lib/routes-report-setups.js +32 -1
  55. package/workspace/lib/routes-today-log.js +48 -0
  56. package/workspace/lib/routine-reports.js +84 -0
  57. package/workspace/lib/status-when.js +36 -0
  58. package/workspace/lib/task-items.js +5 -3
  59. package/workspace/lib/today-log.js +131 -0
  60. package/workspace/live-pill-round-test.js +10 -7
  61. package/workspace/metric-drafts-ui-test.js +3 -1
  62. package/workspace/milestone-pins-test.js +2 -1
  63. package/workspace/milestone-suggest-test.js +3 -1
  64. package/workspace/package-lock.json +2 -2
  65. package/workspace/package.json +1 -1
  66. package/workspace/panels-test.js +2 -1
  67. package/workspace/premium-board-actions-test.js +3 -1
  68. package/workspace/premium-rec-tab-test.js +2 -1
  69. package/workspace/premium-vw-backlog-test.js +3 -1
  70. package/workspace/premium-vw-item-test.js +3 -1
  71. package/workspace/premium-vw-jira-test.js +3 -1
  72. package/workspace/premium-vw-meet-test.js +2 -1
  73. package/workspace/premium-vw-notes-test.js +3 -2
  74. package/workspace/product-lane-test.js +2 -1
  75. package/workspace/program-charter-test.js +3 -1
  76. package/workspace/program-leave-test.js +2 -1
  77. package/workspace/program-order-test.js +2 -1
  78. package/workspace/program-panel-test.js +3 -1
  79. package/workspace/public/pm/artefact-kind.js +87 -0
  80. package/workspace/public/pm/bd-ini-panel.js +77 -12
  81. package/workspace/public/pm/bd-prog-panel.js +55 -8
  82. package/workspace/public/pm/bd-span.js +5 -0
  83. package/workspace/public/pm/bd-work.css +192 -0
  84. package/workspace/public/pm/bd-work.js +483 -0
  85. package/workspace/public/pm/cozy-bar.css +11 -0
  86. package/workspace/public/pm/cozy-bar.js +99 -0
  87. package/workspace/public/pm/cozy-day-pick.css +27 -0
  88. package/workspace/public/pm/cozy-day-pick.js +97 -0
  89. package/workspace/public/pm/cozy-ev.css +2 -5
  90. package/workspace/public/pm/cozy-events-view.js +274 -0
  91. package/workspace/public/pm/cozy-events.css +179 -0
  92. package/workspace/public/pm/cozy-events.js +278 -0
  93. package/workspace/public/pm/cozy-evmodel.js +1 -1
  94. package/workspace/public/pm/cozy-goals.css +172 -0
  95. package/workspace/public/pm/cozy-library.css +125 -0
  96. package/workspace/public/pm/cozy-live-pill.js +1 -0
  97. package/workspace/public/pm/cozy-nav.js +6 -7
  98. package/workspace/public/pm/cozy-pick.css +3 -0
  99. package/workspace/public/pm/cozy-pop-work.css +224 -0
  100. package/workspace/public/pm/cozy-pop-work.js +668 -0
  101. package/workspace/public/pm/cozy-popup.css +11 -2
  102. package/workspace/public/pm/cozy-popup.js +67 -9
  103. package/workspace/public/pm/cozy-routine-job.js +203 -0
  104. package/workspace/public/pm/cozy-routine-model.js +219 -0
  105. package/workspace/public/pm/cozy-routine-pop.js +318 -0
  106. package/workspace/public/pm/cozy-routine.css +155 -0
  107. package/workspace/public/pm/cozy-routine.js +335 -0
  108. package/workspace/public/pm/cozy-schedwork.js +6 -5
  109. package/workspace/public/pm/cozy-settings.js +1 -5
  110. package/workspace/public/pm/cozy-status-when.js +48 -0
  111. package/workspace/public/pm/cozy-today-app.js +236 -0
  112. package/workspace/public/pm/cozy-today-model.js +176 -0
  113. package/workspace/public/pm/cozy-today-page.css +100 -0
  114. package/workspace/public/pm/cozy-today-view.js +76 -0
  115. package/workspace/public/pm/cozy-viewer.js +19 -8
  116. package/workspace/public/pm/cozy-vw-mtg.css +39 -0
  117. package/workspace/public/pm/goals-model.js +205 -0
  118. package/workspace/public/pm/goals-open.js +124 -0
  119. package/workspace/public/pm/home-app.js +18 -3
  120. package/workspace/public/pm/home-chat.js +4 -2
  121. package/workspace/public/pm/home-ini.js +43 -10
  122. package/workspace/public/pm/ini-edit.js +3 -1
  123. package/workspace/public/pm/ini-view-model.js +1 -1
  124. package/workspace/public/pm/library-model.js +197 -0
  125. package/workspace/public/pm/library-open.js +37 -0
  126. package/workspace/public/pm/page-chat.js +12 -2
  127. package/workspace/public/pm/pop-work-model.js +493 -0
  128. package/workspace/public/pm/premium-board.js +204 -17
  129. package/workspace/public/pm/premium-goals.js +213 -0
  130. package/workspace/public/pm/premium-library.js +436 -0
  131. package/workspace/public/pm/premium-shell.js +1 -1
  132. package/workspace/public/pm/premium-vw-mtg.js +79 -1
  133. package/workspace/public/pm/scope-chip.js +204 -0
  134. package/workspace/public/premium-board.html +1 -0
  135. package/workspace/public/premium-events.html +44 -0
  136. package/workspace/public/premium-goals.html +37 -0
  137. package/workspace/public/premium-library.html +39 -0
  138. package/workspace/public/premium-routine.html +33 -0
  139. package/workspace/public/today.html +50 -0
  140. package/workspace/ready-dash-ui-test.js +3 -1
  141. package/workspace/rename-test.js +2 -1
  142. package/workspace/run-tests.js +4 -1
  143. package/workspace/server.js +4 -2
  144. package/workspace/sort-order-test.js +2 -1
  145. package/workspace/split-merge-ui-test.js +3 -1
  146. package/workspace/stage-card-tasks-test.js +3 -1
  147. package/workspace/stage-card-test.js +2 -1
  148. package/workspace/task-delete-test.js +2 -1
  149. package/workspace/task-links-test.js +2 -1
  150. package/workspace/task-milestones-test.js +4 -2
  151. package/workspace/task-plans-test.js +3 -1
  152. package/workspace/task-press-test.js +3 -1
  153. package/workspace/task-views-test.js +3 -1
  154. package/workspace/test-lists/doc-path.json +9 -0
  155. package/workspace/test-lists/events-place.json +11 -0
  156. package/workspace/test-lists/goals-place.json +9 -0
  157. package/workspace/test-lists/library-place.json +9 -0
  158. package/workspace/test-lists/pop-work.json +9 -0
  159. package/workspace/test-lists/routine-place.json +9 -0
  160. package/workspace/test-lists/status-when.json +9 -0
  161. package/workspace/test-lists/today-log.json +9 -0
  162. package/workspace/test-lists/work-view.json +9 -0
  163. package/workspace/timeline-holds-test.js +3 -1
  164. package/workspace/tk-tasks-test.js +2 -1
@@ -0,0 +1,493 @@
1
+ /* pm/pop-work-model.js — what the popup shows for work, as data (slice 3 of the six-place navigation; boards 3, 20, 21, 22, 25 on Exploring 2,
2
+ * page "Navigation: Today, Work, Goals, Events, Library, Routine"; build plan /mnt/project-files/nav-structure/build-plan.md). Pure: no DOM, no
3
+ * reads. pm/cozy-pop-work.js draws it; pop-work.test.js checks it.
4
+ *
5
+ * workOf(r, v, o) → { title, overview, suggests, rowSuggests, more, rows, span, artefacts, many } one initiative in the popup
6
+ * r the initiative row as GET /api/board-premium sends it (Board and Home both read it)
7
+ * v pm/ini-view-model.js iniView(r, ctx): the stage, its dates, the tasks by stage, what PMOS proposes (v.props)
8
+ * o { steps (Product feature's eight), today ('YYYY-MM-DD'), docs (home-ini-model.js docRows of the initiative's read), items (the
9
+ * Library's rows for this initiative, GET /api/library-items, when that route is there), product (its name), program ({ title } | null),
10
+ * goal (the goal's name, else the anchor), mode ('full' | 'quarter' | 'half' | 'year' | '2y' | '5y': the timeline's span), open (Set of
11
+ * stage keys the PM unfolded), shut (Set of stage keys the PM folded), doneOpen (Set of stage keys whose done tasks show), farOpen (bool) }
12
+ * levelOf(L, o) → the same parts one level up: a program or a product (boards 26, 27). L: { kind, title, path, summary, facts, rows, docs, props }
13
+ * spanOf(dates, mode, today) → { a, b } ISO the window of the timeline: Full runs from the earliest start to the latest end (no bar is cut)
14
+ * barOf(x, span, today) → { l, w, pass, late, ms, dot } in % of the window one bar: the plan, the part that has passed, the missed date
15
+ * dayText(iso, today) → "4 Nov", "13 Jan 2027" dateLate(iso, today) → "4 Nov · 14 days late" | "4 Nov"
16
+ * summaryOf(r, v, o) → { text, own } own: the PM's words (the record's description); else PMOS wrote it (✦)
17
+ *
18
+ * THE RULES (design notes st2, boards 3, 20, 21, 22, 25):
19
+ * 1 Overview: the summary, then "PMOS suggests" (at most three; absent when there is none), then the facts. With many tasks the Overview
20
+ * folds to its summary and "Details" opens the facts (many: more than 6 rows of tasks in the open stage).
21
+ * 2 A suggestion about one task sits under that task (rowSuggests by uid); the same suggestion never shows twice.
22
+ * 3 The breakdown: a stage is a row that folds ("3 of 9 done"). Only the stage in progress opens by itself. In the open stage late tasks
23
+ * come first, then by date; done tasks fold to one line ("3 tasks done"). The stages after the next one fold into one row
24
+ * ("Prioritize to Evaluate, 5 stages").
25
+ * 4 Bars: light grey the plan, dark grey the part that has passed, the missed date a diamond, a red line from it to today. A row with no
26
+ * date has no bar: PMOS does not invent a date.
27
+ * 5 Artefacts: one table: Artefact, Kind, Updated. A core document a gate needs that is not written is a grey row that says which gate. */
28
+
29
+ import { gapsOf } from './cozy-bar.js';
30
+ import { provenStatus, NO_SIGNAL } from './status-proof.js';
31
+ const MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
32
+ const DAY = 864e5;
33
+ const day = (v) => { const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(v || '')); return m ? new Date(+m[1], +m[2] - 1, +m[3]) : null; };
34
+ const iso = (d) => d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
35
+ const between = (a, b) => Math.round((b - a) / DAY);
36
+ const plural = (n, a, b) => n + ' ' + (n === 1 ? a : b);
37
+ const todayOf = (o) => day(o && o.today) || (() => { const d = new Date(); return new Date(d.getFullYear(), d.getMonth(), d.getDate()); })();
38
+ const norm = (s) => String(s || '').trim().toLowerCase().replace(/\s+/g, ' ');
39
+ export const MAX_SUGGEST = 3;
40
+ export const MANY = 6;
41
+ export const NEXT = 5; // a long stage shows its late and in-progress tasks and the next five
42
+ export const MODES = [['quarter', 'Quarter'], ['half', 'Half'], ['year', 'Year'], ['2y', '2 yr'], ['5y', '5 yr'], ['full', 'Full']];
43
+
44
+ /** "4 Nov" this year, "13 Jan 2027" in any other. */
45
+ export function dayText(v, today) {
46
+ const d = day(v); if (!d) return '';
47
+ const t = today instanceof Date ? today : day(today) || new Date();
48
+ return d.getDate() + ' ' + MON[d.getMonth()] + (d.getFullYear() !== t.getFullYear() ? ' ' + d.getFullYear() : '');
49
+ }
50
+ /** A date and, when it has passed and the thing is open, how late: "4 Nov · 14 days late". */
51
+ export function dateLate(v, today, open = true) {
52
+ const d = day(v), t = today instanceof Date ? today : day(today); if (!d || !t) return { text: dayText(v, t), late: false };
53
+ const late = open && d < t;
54
+ return { text: dayText(v, t) + (late ? ' · ' + plural(between(d, t), 'day', 'days') + ' late' : ''), late };
55
+ }
56
+
57
+ /* ── the timeline ── */
58
+ /** The window: Full from the earliest date to the latest (and today), so no bar is cut; the others start at the period that holds today. */
59
+ export function spanOf(dates, mode, today) {
60
+ const t = today instanceof Date ? today : day(today) || new Date();
61
+ const ds = (dates || []).map(day).filter(Boolean).concat([t]);
62
+ if (!mode || mode === 'full') {
63
+ let a = new Date(Math.min(...ds)), b = new Date(Math.max(...ds));
64
+ if (between(a, b) < 28) b = new Date(a.getFullYear(), a.getMonth(), a.getDate() + 28);
65
+ // Today sits inside the span, never at its right edge (board 20): the span runs on to at least a fifth past today
66
+ if (between(t, b) < between(a, b) * 0.2) b = new Date(t.getFullYear(), t.getMonth(), t.getDate() + Math.max(14, Math.round(between(a, t) * 0.25)));
67
+ const pad = Math.max(2, Math.round(between(a, b) * 0.03));
68
+ a = new Date(a.getFullYear(), a.getMonth(), a.getDate() - pad); b = new Date(b.getFullYear(), b.getMonth(), b.getDate() + pad);
69
+ return { a: iso(a), b: iso(b) };
70
+ }
71
+ const y = t.getFullYear(), m = t.getMonth();
72
+ const start = mode === 'quarter' ? new Date(y, m - (m % 3), 1) : mode === 'half' ? new Date(y, m < 6 ? 0 : 6, 1) : new Date(y, 0, 1);
73
+ const months = { quarter: 3, half: 6, year: 12, '2y': 24, '5y': 60 }[mode] || 12;
74
+ return { a: iso(start), b: iso(new Date(start.getFullYear(), start.getMonth() + months, 0)) };
75
+ }
76
+ const pct = (d, S) => { const a = day(S.a), b = day(S.b); return ((d - a) / Math.max(DAY, b - a)) * 100; };
77
+ const clamp = (n) => Math.max(0, Math.min(100, n));
78
+ /** One bar in the window. x: { start, end, done, open }. A row with neither date has no bar (null). */
79
+ export function barOf(x, S, today) {
80
+ const t = today instanceof Date ? today : day(today) || new Date();
81
+ const a0 = day(x.start), b0 = day(x.end);
82
+ if (!a0 && !b0) return null;
83
+ const a = a0 || b0, b = b0 || a0;
84
+ const l = clamp(pct(a, S)), r = clamp(pct(new Date(b.getFullYear(), b.getMonth(), b.getDate() + 1), S));
85
+ if (r <= 0 || l >= 100) return { off: true };
86
+ const tx = clamp(pct(t, S));
87
+ const pass = x.done ? r - l : Math.max(0, Math.min(r, tx) - l);
88
+ const missed = !x.done && x.open !== false && !!b0 && b0 < t;
89
+ return { l, w: Math.max(0.6, r - l), pass: Math.max(0, pass), dot: !a0 || !b0,
90
+ late: missed ? { l: clamp(pct(b0, S)), w: Math.max(0.3, tx - clamp(pct(b0, S))) } : null, ms: missed ? clamp(pct(b0, S)) : null };
91
+ }
92
+ /** The month marks on the header: the first of each month that fits, thinned to about six. */
93
+ export function marksOf(S, today) {
94
+ const a = day(S.a), b = day(S.b), t = today instanceof Date ? today : day(today) || new Date(), out = [];
95
+ const n = (b.getFullYear() - a.getFullYear()) * 12 + b.getMonth() - a.getMonth();
96
+ const step = n <= 6 ? 1 : n <= 14 ? 3 : n <= 30 ? 6 : 12;
97
+ for (let d = new Date(a.getFullYear(), a.getMonth() + 1, 1); d < b; d = new Date(d.getFullYear(), d.getMonth() + 1, 1)) {
98
+ if (d.getMonth() % step) continue;
99
+ out.push({ x: pct(d, S), label: d.getMonth() === 0 && step < 12 ? String(d.getFullYear()) : step >= 12 ? String(d.getFullYear()) : MON[d.getMonth()] });
100
+ }
101
+ const tx = pct(t, S);
102
+ return { months: out.filter((m) => m.x > 2 && m.x < 98), today: tx >= 0 && tx <= 100 ? tx : null };
103
+ }
104
+
105
+ /* ── the stages and the dates they ran (the record only) ── */
106
+ function ranOf(r, list) {
107
+ const tr = r.trail || {}, moves = Array.isArray(tr.moves) ? tr.moves : [];
108
+ const idx = (to) => list.findIndex((s) => s.key === to || norm(s.name) === norm(to));
109
+ const ran = list.map(() => ({ from: null, to: null }));
110
+ moves.forEach((m, i) => { const j = idx(m.to); if (j < 0) return; ran[j].from = m.at; const nx = moves[i + 1]; ran[j].to = nx ? nx.at : null; });
111
+ const cur = list.findIndex((s) => s.status === 'now');
112
+ if (cur >= 0 && tr.since) ran[cur].from = tr.since;
113
+ if (list.length && !ran[0].from && r.start) ran[0].from = String(r.start).slice(0, 10);
114
+ const fr = (r.found && r.found.ran) || {};
115
+ list.forEach((s, i) => { const f = fr[s.key] || fr[s.name]; if (f && f.from) ran[i].from = f.from; if (f && f.to) ran[i].to = f.to; });
116
+ return ran;
117
+ }
118
+ const isoOf = (v) => (v ? String(v).slice(0, 10) : '');
119
+ const taskSpan = (ts) => {
120
+ const a = ts.map((x) => x.start || x.end).filter(Boolean).sort(), b = ts.map((x) => x.end || x.start).filter(Boolean).sort();
121
+ return [a[0] || '', b[b.length - 1] || ''];
122
+ };
123
+ const urgent = (a, b) => (b.late - a.late) || (a.end || '9999').localeCompare(b.end || '9999');
124
+ const STATUS = { done: 'Done', now: 'In progress', next: 'To do', skip: 'Skipped' };
125
+ const taskStatus = (x) => (x.wont ? "Won't do" : x.status || 'To do');
126
+
127
+ /* ── the suggestions: what PMOS proposes (r.props, lib/proposals.js), and its split or merge ── */
128
+ const PROP_TITLE = {
129
+ status: (p) => plural(p.items.length, 'task looks', 'tasks look') + ' done. Mark ' + (p.items.length === 1 ? 'it' : 'them') + ' done?',
130
+ names: (p) => 'Tidy ' + plural(p.items.length, 'name', 'names') + '?',
131
+ description: () => 'Keep this description?',
132
+ build: () => 'Open a draft pull request?',
133
+ outcome: (p) => (p.type === 'date' ? 'Set a date to check the outcome?' : 'Say what should change once it ships?'),
134
+ };
135
+ /** One line of a proposal, in plain words. */
136
+ export function sayOf(p) {
137
+ if (!p) return '';
138
+ if (p.title) return String(p.title);
139
+ const f = PROP_TITLE[p.group];
140
+ if (f) return f(p);
141
+ if (p.group === 'dates') return 'New dates for ' + plural(p.items.length, 'task', 'tasks') + '?';
142
+ if (p.group === 'found') return 'Add what PMOS found elsewhere?';
143
+ return 'A change PMOS suggests';
144
+ }
145
+ /** The task a proposal is about, when it is about one task only (it then sits under that row). */
146
+ const aboutTask = (p) => { const it = p.items || []; return it.length === 1 && (it[0].task || it[0].uid) ? String(it[0].task || it[0].uid) : ''; };
147
+ export function suggestsOf(v) {
148
+ const P = (((v && v.props) || {}).groups || []).flatMap((g) => g.props || []).filter((p) => p && p.id && p.section !== 'unplaced' && p.group !== 'grill' && p.group !== 'description');
149
+ const seen = new Set(), rows = new Map(), top = [];
150
+ for (const p of P) {
151
+ const say = sayOf(p), k = norm(say);
152
+ if (seen.has(k)) continue; // the same suggestion never shows twice
153
+ seen.add(k);
154
+ const s = { id: p.id, sig: p.sig || '', say, why: p.why || '', group: p.group, type: p.type || '', sources: p.sources || [], items: p.items || [], data: p.data || {}, sure: p.section === 'sure' };
155
+ const t = aboutTask(p);
156
+ if (t && !rows.has(t)) rows.set(t, s); else top.push(s);
157
+ }
158
+ // split or merge (batch 11 item 14) is a suggestion about the whole initiative
159
+ if (v && v.suggest && v.suggest.kind && v.suggest.title) top.unshift({ id: 'sm', sm: true, say: String(v.suggest.title), why: v.suggest.why || v.suggest.reason || '', group: v.suggest.kind, sources: [], items: [] });
160
+ return { top: top.slice(0, MAX_SUGGEST), more: Math.max(0, top.length - MAX_SUGGEST), rows };
161
+ }
162
+ /** The description PMOS drafted for this initiative, waiting under the ✦ (lib/description-props.js), or null. */
163
+ export function draftDesc(v) {
164
+ const p = (((v && v.props) || {}).groups || []).flatMap((g) => g.props || []).find((x) => x && x.group === 'description' && x.data && x.data.text);
165
+ return p ? { id: p.id, sig: p.sig || '', text: String(p.data.text) } : null;
166
+ }
167
+
168
+ /* ── the Overview ── */
169
+ const firstSentences = (s, n = 2) => { const parts = String(s || '').replace(/\s+/g, ' ').trim().match(/[^.!?]+[.!?]+(\s|$)|[^.!?]+$/g) || []; return parts.slice(0, n).join('').trim(); };
170
+ /** Text that is only an import note ("…162 issues, 35 open. tsv. It is in Closed."): never the Overview's words (round 3 point 14). */
171
+ export const importNote = (s) => { const t = String(s || ''); return /\b(tsv|csv|xlsx)\b/i.test(t) || /\b\d+ issues?\b.*\b\d+ open\b/i.test(t) || /^\s*(imported|import)\b/i.test(t); };
172
+ /** The summary: what it is (the record's description, else PMOS's draft), then where it stands, in PMOS's words. own: the PM's words lead. */
173
+ export function summaryOf(r, v, o = {}) {
174
+ const t = todayOf(o), st = v.stage || {}, d = st.dates || {}, said = [];
175
+ const desc = v.desc && !importNote(v.desc) ? String(v.desc) : '', draft = !desc ? draftDesc(v) : null;
176
+ const lead = desc || (draft && draft.text) || '';
177
+ if (st.name && st.n) said.push('It is in ' + st.name + (d.left ? (/over$/.test(d.left) ? ' and ' + d.left + ' its plan' : ', ' + d.left) : '') + '.');
178
+ const late = ((v.tasks || {}).sections || []).flatMap((s) => (s.top || []).concat(...(s.groups || []).map((g) => g.rows || []))).filter((x) => x.late);
179
+ if (late.length === 1) said.push('One task holds it: ' + late[0].title + ' is ' + late[0].lateText + '.');
180
+ else if (late.length > 1) said.push(plural(late.length, 'task is', 'tasks are') + ' late.');
181
+ const gate = st.gate && (st.gate.parts || []).filter((x) => !x.ok).map((x) => x.text);
182
+ if (gate && gate.length) said.push(gate.join(' and ').replace(/^./, (c) => c.toUpperCase()) + ', so it cannot pass the ' + String(st.gate.label || 'gate').replace(/^Gate/, 'gate') + '.');
183
+ // folded (board 20): the first sentence, where it stands, and how many tasks of the stage in progress are open; the gate waits for Details
184
+ const cur = ((v.tasks || {}).sections || []).find((s) => s.kind === 'stage' && s.status === 'now');
185
+ let openLine = '';
186
+ if (cur) {
187
+ const ts = (cur.top || []).concat(...(cur.groups || []).map((g) => g.rows || [])), n = ts.length + (cur.done || []).length, op = ts.filter((x) => !x.closed).length;
188
+ if (n && op) openLine = op + ' of ' + n + ' tasks in ' + cur.name + ' ' + (op === 1 ? 'is' : 'are') + ' open.';
189
+ }
190
+ const gateLine = said.length && gate && gate.length ? said[said.length - 1] : '';
191
+ const where = gateLine ? said.slice(0, -1) : said;
192
+ return { lead: firstSentences(lead, 3), own: !!desc, draft, status: said.join(' '), text: [firstSentences(lead, 3), said.join(' ')].filter(Boolean).join(' '),
193
+ short: firstSentences(lead, 1), folded: where.concat(openLine ? [openLine] : []).join(' ') };
194
+ }
195
+ /** A value shaped like an id, never a name: a hash ("8f24f28c35"), a KR key ("KR-C4", "O2.KR1", "q4-2026:o2:kr1"), a health budget key. The
196
+ * grid never shows one (round 3 points 4 and 12). */
197
+ export function idShaped(v) {
198
+ const s = String(v == null ? '' : v).trim(); if (!s) return false;
199
+ if (/\s/.test(s)) return false;
200
+ return /^[0-9a-f]{7,}$/i.test(s) || /^(kr|o)\d*[-.:]?[a-z]?\d/i.test(s) || /^[a-z0-9-]+(:[a-z0-9-]+)+$/i.test(s) || /^(o\d+|[a-z]+-[a-z]?\d+)(\.[a-z0-9-]+)+$/i.test(s) || /^health:/i.test(s);
201
+ }
202
+ const STATUS_WORD = { 'on track': 'On track', 'at risk': 'At risk', late: 'Late', done: 'Done', 'on hold': 'On hold' };
203
+ /** The Details grid (round 3 point 12): Stage / Status, Start / Target end, Next milestone / Goal, Product / Program, Workflow / Open. One
204
+ * plain value each; a field with no value, or only an id for a value, is not there. act: what a press on the value does (the host has it). */
205
+ export function factsOf(r, v, o = {}) {
206
+ const t = todayOf(o), st = v.stage || {}, F = [];
207
+ const add = (f) => { if (f && f.v && !idShaped(f.v)) F.push(f); };
208
+ const closed = !!(r.closed || r.ended), live = !closed && r.status !== 'done';
209
+ // a cell is pressable only when the press opens something (round 3 point 15): Next milestone, Goal, Product, Program. The rest is plain text
210
+ add(st.name ? { k: 'Stage', v: st.name, read: true } : null);
211
+ const end = day(r.end), over = live && end && end < t ? between(end, t) : 0;
212
+ // Status through the provenance rule (status-proof.js): a claim with no source and read time says No signal
213
+ const ps = closed ? '' : provenStatus(r, o.now);
214
+ const sw = closed ? 'Closed' : ps === NO_SIGNAL ? 'No signal' : STATUS_WORD[ps] || '';
215
+ add(sw ? { k: 'Status', v: sw + (sw === 'Late' && over ? ', ' + plural(over, 'day', 'days') + ' over' : ''), warn: sw === 'Late' || sw === 'At risk', read: true } : null);
216
+ add(r.start ? { k: 'Start', v: dayText(r.start, t), read: true } : null);
217
+ add(r.end ? { k: 'Target end', v: dayText(r.end, t), note: over ? plural(over, 'day', 'days') + ' over' : '', warn: !!over, read: true } : null);
218
+ const nm = nextMilestone(v, t);
219
+ add(nm ? { k: 'Next milestone', v: nm.name, note: nm.date, warn: nm.late, act: nm.uid ? 'task:' + nm.uid : '', read: !nm.uid } : null);
220
+ add(o.goal ? { k: 'Goal', v: o.goal, act: o.canGoal ? 'goal' : '', read: !o.canGoal } : null);
221
+ add(o.product ? { k: 'Product', v: o.product, act: o.canProduct ? 'product' : '', read: !o.canProduct } : null);
222
+ add(o.program && o.program.title ? { k: 'Program', v: o.program.title, act: o.program.id ? 'program' : '', read: !o.program.id } : null);
223
+ const wf = v.summary && v.summary.wf;
224
+ add(wf && wf.name ? { k: 'Workflow', v: wf.name + (st.of ? ', ' + plural(st.of, 'stage', 'stages') : ''), read: true } : null);
225
+ const tk = v.tasks || {}, docs = (o.docs || []).filter((x) => x.path).length + (o.items || []).length;
226
+ add({ k: 'Open', v: [tk.open ? plural(tk.open, 'task', 'tasks') : '', docs ? plural(docs, 'document', 'documents') : ''].filter(Boolean).join(' · '), read: true });
227
+ return F;
228
+ }
229
+ /** The next open milestone task (◇), else null. */
230
+ function nextMilestone(v, t) {
231
+ const all = ((v.tasks || {}).sections || []).flatMap((s) => (s.top || []).concat(...(s.groups || []).map((g) => g.rows || [])));
232
+ const ms = all.filter((x) => x.ms && !x.closed && x.end).sort((a, b) => a.end.localeCompare(b.end))[0];
233
+ if (!ms) {
234
+ const g = (((v.stage || {}).gates || {}).open || [])[0];
235
+ return g ? { name: g.name, date: g.iso ? dateLate(g.iso, t).text : '', late: !!g.late, uid: g.id } : null;
236
+ }
237
+ const dl = dateLate(ms.end, t);
238
+ return { name: ms.title, date: dl.text, late: dl.late, uid: ms.uid };
239
+ }
240
+
241
+ /* ── the Overview explains the initiative (round 3 point 10): Problem, Goal, Done when, from its own records; never invented ── */
242
+ const textOf = (f) => (f && typeof f === 'object' && !Array.isArray(f) ? String(f.text || '') : '');
243
+ /** overviewOf(r, v, o) → { lines: [{ k, text, src, path?, edit, miss?, ask?, id }], status }. o.art: GET /api/artefacts?scope=experiment:<id>
244
+ * .artefacts (null while it reads), o.docs, o.goal (the goal's name). A line with no source says so and carries the words ✓ sends to Ask PMOS.
245
+ * edit: { to: 'artefact', artefact, field } | { to: 'desc' } | null (not edited here). */
246
+ export function overviewOf(r, v, o = {}) {
247
+ const A = new Map(((o.art || [])).map((a) => [a.key, a])), ex = A.get('experiment') || { fields: {}, carried: [] }, dt = A.get('done-test') || { fields: {}, carried: [] };
248
+ const title = v.title || r.title || 'this initiative';
249
+ const docs = (o.docs || []).filter((d) => d && (d.path || d.link) && d.title);
250
+ const from = docs.find((d) => /brief/i.test(d.title)) || docs.find((d) => /\bprd\b/i.test(d.title)) || docs[0] || null;
251
+ const fromW = from ? (/brief/i.test(from.title) ? 'the brief' : /\bprd\b/i.test(from.title) ? 'the PRD' : from.title) : '';
252
+ const offer = (k, what) => ({ miss: k + ' is not written.' + (fromW ? ' Draft it from ' + fromW + '?' : ' Draft it with you?'),
253
+ ask: 'Draft ' + what + ' for ' + title + (fromW ? ' from ' + fromW : '') + ', in two short sentences, for me to read. Say what you read. Do not invent anything the records do not say.' });
254
+ const carriedPath = (a, f) => ((a.carried || []).find((c) => c.field === f && (c.path || c.text)) || null);
255
+ const lines = [];
256
+ // Problem: the Experiment's problem line, else the problem statement it carries
257
+ const pb = textOf(ex.fields.why), pc = carriedPath(ex, 'why');
258
+ if (pb) lines.push({ id: 'problem', k: 'Problem', text: pb, src: 'the Experiment', edit: { to: 'artefact', artefact: 'experiment', field: 'why' } });
259
+ else if (pc && pc.text) lines.push({ id: 'problem', k: 'Problem', text: String(pc.text), src: pc.title || 'the problem statement', path: pc.path || '', edit: { to: 'artefact', artefact: 'experiment', field: 'why' } });
260
+ else lines.push({ id: 'problem', k: 'Problem', text: '', edit: { to: 'artefact', artefact: 'experiment', field: 'why' }, ...offer('Problem', 'the problem (who has it and what it costs)') });
261
+ // Goal: the hypothesis, else the record's description; with the goal or key result it serves, by name
262
+ const hy = textOf(ex.fields.hypothesis), ds = v.desc && !importNote(v.desc) ? firstSentences(v.desc, 2) : '';
263
+ const serves = o.goal ? 'It serves ' + o.goal.replace(/\.$/, '') + '.' : '';
264
+ if (hy) lines.push({ id: 'goal', k: 'Goal', text: hy, serves, src: 'the Experiment', edit: { to: 'artefact', artefact: 'experiment', field: 'hypothesis' } });
265
+ else if (ds) lines.push({ id: 'goal', k: 'Goal', text: ds, serves, src: 'the record', edit: { to: 'desc' } });
266
+ else lines.push({ id: 'goal', k: 'Goal', text: '', serves, edit: { to: 'artefact', artefact: 'experiment', field: 'hypothesis' }, ...offer('Goal', 'what we are trying to achieve') });
267
+ // Done when: the done test's checks
268
+ const ck = Array.isArray(dt.fields.check) ? dt.fields.check.filter((x) => x && x.text) : [];
269
+ if (ck.length) lines.push({ id: 'done', k: 'Done when', text: ck[0].text + (ck.length > 1 ? ' (and ' + plural(ck.length - 1, 'more check', 'more checks') + ')' : ''), src: 'the Done test', edit: null });
270
+ else lines.push({ id: 'done', k: 'Done when', text: '', edit: { to: 'artefact', artefact: 'done-test', field: 'check' }, ...offer('Done when', 'the done test (how we will know it worked, and when we check)') });
271
+ const sm = summaryOf(r, v, o);
272
+ const status = sm.status.replace(/^It is in /, 'In ');
273
+ return { lines, status, reading: o.art == null };
274
+ }
275
+
276
+ /** The goal's name for an anchor (GET /api/boards/anchors rows { value, kind, label }): "KR-C4", "O-C:KR-C4" or "O-C.KR-C4" finds the KR
277
+ * whose value ends in it, and its label without the token ("KR-C4 De-risk Okta dependency" → "De-risk Okta dependency"). '' when none. */
278
+ export function goalName(list, anchor) {
279
+ const a = String(anchor || '').trim().toUpperCase().replace(/^KR:/, ''); if (!a) return '';
280
+ const L = list || [], dotted = a.replace(/:/g, '.'), last = dotted.split('.').pop(), two = dotted.split('.').slice(-2).join('.');
281
+ // "q4-2026:o2:kr1" (a period first), "O2.KR1", "health:ux": the whole value, else the objective and the KR, else a KR id found once
282
+ let hit = L.find((x) => { const u = String(x.value || '').toUpperCase(); return u === dotted || u === a || u === two; });
283
+ if (!hit) { const by = L.filter((x) => x.kind === 'kr' && String(x.value || '').toUpperCase().split('.').pop() === last); if (by.length === 1) hit = by[0]; }
284
+ if (!hit) return '';
285
+ const lb = String(hit.label || '');
286
+ if (hit.kind === 'kr') return lb.replace(/^\S+\s+(?:[—–:-]\s*)?/, '').replace(/\.$/, '');
287
+ const m = /\((.+)\)$/.exec(lb); return m ? m[1] : lb;
288
+ }
289
+
290
+ /* ── the work breakdown ── */
291
+ function taskRow(x, t) {
292
+ const dl = x.end ? dateLate(x.end, t, !x.closed) : { text: x.start ? 'from ' + dayText(x.start, t) : '', late: false };
293
+ const st = taskStatus(x);
294
+ return { kind: 'task', uid: x.uid, title: x.title, status: st, date: dl.text, late: dl.late, start: x.start || '', end: x.end || '', since: x.since || '', done: !!x.closed, ms: !!x.ms, jira: !!x.jkey, canCheck: !!x.canCheck,
295
+ prog: /progress|review/i.test(st), wont: !!x.wont || st === "Won't do" };
296
+ }
297
+ export function breakdownOf(r, v, o = {}) {
298
+ const t = todayOf(o), list = ((v.stage || {}).ladder || []).map((s, i) => ({ name: s.name, status: s.cls, ms: !!s.ms, i }));
299
+ const secs = (v.tasks || {}).sections || [];
300
+ const ran = ranOf(r, secs.filter((s) => s.kind === 'stage').map((s) => ({ key: s.key, name: s.name, status: s.status })));
301
+ const open = o.open || new Set(), shut = o.shut || new Set(), dOpen = o.doneOpen || new Set();
302
+ const stages = secs.filter((s) => s.kind === 'stage');
303
+ const cur = stages.findIndex((s) => s.status === 'now');
304
+ const rows = [];
305
+ let many = false;
306
+ const looseSec = secs.find((s) => s.kind === 'none');
307
+ const looseTs = looseSec ? (looseSec.top || []).concat(...(looseSec.groups || []).map((g) => g.rows || [])) : [];
308
+ // the tasks in no stage are one quiet folded row at the end (round 3 point 21): no task rows and no bars until it is opened; its one ✦
309
+ // offers to put them in the stage in progress, the one stage PMOS can name for each
310
+ const looseUnder = null;
311
+ const loose = [];
312
+ const stageRow = (s, i) => {
313
+ const ts = (s.top || []).concat(...(s.groups || []).map((g) => g.rows || []));
314
+ const lz = looseUnder === s ? looseTs : [];
315
+ const all = ts.concat(s.done || []);
316
+ const nDone = (s.done || []).length, n = all.length;
317
+ const [ta, tb] = taskSpan(all);
318
+ const rn = ran[i] || {};
319
+ let a = '', b = '';
320
+ if (s.status === 'done') { a = isoOf(rn.from) || ta; b = isoOf(rn.to) || tb; }
321
+ else if (s.status === 'now') { const pv = ran.slice(0, i).reverse().find((q) => q.to || q.from) || {}; a = isoOf(rn.from) || isoOf(pv.to) || ta || isoOf(pv.from); b = isoOf(r.end) || tb; } // no recorded start: since the last date the record holds before it // the stage in progress runs to the plan end: late is said against the plan
322
+ else if (s.status !== 'skip') { a = ta; b = tb; }
323
+ const live = s.status === 'now' && r.status !== 'done' && !r.ended;
324
+ const dl = b && (s.status === 'now' || s.status === 'next') ? dateLate(b, t, live || ts.some((x) => !x.closed)) : { text: b && s.status === 'done' ? '' : '', late: false };
325
+ const isOpen = shut.has(s.key) ? false : open.has(s.key) ? true : s.status === 'now';
326
+ const count = n ? (s.status === 'next' && !nDone ? plural(n, 'task', 'tasks') : nDone + ' of ' + n + ' done') : '';
327
+ const has = n + lz.length > 0;
328
+ // the bar's gap line and its ◇s (pm/cozy-bar.js stageBar): the stage's dated tasks, and its milestone tasks
329
+ const gaps = a && b ? gapsOf(all.filter((x) => !x.wont).map((x) => [x.start || x.end, x.end || x.start]).filter(([p]) => p), a, b) : [];
330
+ const marks = all.filter((x) => x.ms && !x.wont && x.end).map((x) => ({ uid: x.uid, title: x.title, end: x.end, done: !!x.closed, late: !!x.late && !x.closed }));
331
+ rows.push({ kind: 'stage', key: s.key, n: s.n, name: s.name, ms: !!s.ms, status: STATUS[s.status] || '', count, date: dl.text, late: dl.late, start: a, end: b, gaps, marks, tone: s.status === 'done' ? 'dn' : s.status === 'now' ? 'now' : '',
332
+ done: s.status === 'done', open: (isOpen || lz.length > 0) && has, can: has, now: s.status === 'now', skip: s.status === 'skip' });
333
+ if (!((isOpen || lz.length > 0) && has)) return;
334
+ const sorted = ts.slice().sort(urgent);
335
+ if (s.status === 'now' && sorted.length > MANY) many = true;
336
+ // a long stage (round 3 point 7): the late and in-progress tasks, then the next NEXT, then "N more"
337
+ const all2 = sorted.map((x) => taskRow(x, t));
338
+ let shown = all2;
339
+ if (all2.length > NEXT + 3 && !(o.moreOpen && o.moreOpen.has(s.key))) {
340
+ const hot = all2.filter((x) => x.late || x.prog), rest = all2.filter((x) => !x.late && !x.prog);
341
+ const keep = new Set(hot.concat(rest.slice(0, NEXT)));
342
+ shown = all2.filter((x) => keep.has(x));
343
+ }
344
+ for (const x of shown) rows.push({ ...x, sec: s.key });
345
+ if (shown.length < all2.length) rows.push({ kind: 'more', key: s.key, n: all2.length - shown.length, label: plural(all2.length - shown.length, 'more task', 'more tasks') });
346
+ for (const x of lz.slice().sort(urgent)) { rows.push({ ...taskRow(x, t), sec: '', loose: s.key }); loose.push({ uid: x.uid, title: x.title, to: s.key, name: s.name }); }
347
+ if (nDone) {
348
+ const dd = s.done.map((x) => x.end).filter(Boolean).sort();
349
+ rows.push({ kind: 'done', key: s.key, n: nDone, label: plural(nDone, 'task', 'tasks') + ' done', open: dOpen.has(s.key), start: (s.done.map((x) => x.start).filter(Boolean).sort()[0]) || dd[0] || '', end: dd[dd.length - 1] || '' });
350
+ if (dOpen.has(s.key)) for (const x of s.done) rows.push({ ...taskRow(x, t), sec: s.key });
351
+ }
352
+ };
353
+ // the stages after the next one fold into one row (board 20), unless the PM opened it
354
+ const farFrom = cur >= 0 ? cur + 2 : -1;
355
+ stages.forEach((s, i) => {
356
+ if (farFrom > 0 && i >= farFrom && stages.length - farFrom >= 2 && !o.farOpen) {
357
+ if (i === farFrom) {
358
+ const far = stages.slice(farFrom), n = far.reduce((k, x) => k + (x.top || []).length + (x.done || []).length + (x.groups || []).reduce((m, g) => m + g.n, 0), 0);
359
+ rows.push({ kind: 'far', n: s.n, name: far[0].name + ' to ' + far[far.length - 1].name, count: plural(far.length, 'stage', 'stages'), status: 'To do', tasks: n });
360
+ }
361
+ return;
362
+ }
363
+ stageRow(s, i);
364
+ });
365
+ const ts = looseTs.filter((x) => !x.closed);
366
+ if (ts.length) {
367
+ const isOpen = open.has('') && !shut.has('');
368
+ rows.push({ kind: 'stage', key: '', n: '', name: 'Not in a stage', status: '', count: plural(ts.length, 'open task', 'open tasks'), date: '', start: '', end: '', open: isOpen, can: true, loose: true });
369
+ if (isOpen) for (const x of ts.slice().sort(urgent)) rows.push({ ...taskRow(x, t), sec: '' });
370
+ if (cur >= 0) for (const x of ts) loose.push({ uid: x.uid, title: x.title, to: stages[cur].key, name: stages[cur].name });
371
+ }
372
+ return { rows, many, list, loose };
373
+ }
374
+
375
+ /* ── the artefacts ── */
376
+ const KIND_OF_ITEM = { doc: 'Document', link: 'Link', meeting: 'Meeting notes', proto: 'Prototype', design: 'Design' };
377
+ /** One table: the Library's rows when that route answers, else the core documents the page already reads; then the missing ones a gate needs. */
378
+ export function artefactsOf(v, o = {}) {
379
+ const t = todayOf(o), out = [], seen = new Set();
380
+ const key = (s) => norm(s).replace(/[^a-z0-9]+/g, ' ').trim();
381
+ for (const it of o.items || []) {
382
+ if (!it || !it.name || seen.has(key(it.name))) continue;
383
+ seen.add(key(it.name));
384
+ out.push({ id: it.id, title: it.name, kind: it.kindText || KIND_OF_ITEM[it.kind] || 'Document', when: it.updated ? dayText(it.updated, t) || it.updated : '', item: it, link: it.kind === 'link',
385
+ type: it.type || '', path: (it.open && it.open.doc && it.open.doc.path) || '', akind: it.kind, at: Date.parse(it.updated || '') || 0,
386
+ // the Library's own Kind (lib/library-items.js kindOf, PR #1376) when it is a specific one; the popup's resolver only fills in for older servers
387
+ kindName: it.kindText && !/^(Document|Meeting notes|Link|File)$/.test(it.kindText) ? (it.kindText === 'Rubric' ? 'Eval rubric' : it.kindText) : '' });
388
+ }
389
+ for (const d of o.docs || []) {
390
+ if (!d || !d.title || seen.has(key(d.title))) continue;
391
+ if (d.path || d.link) {
392
+ seen.add(key(d.title));
393
+ const lk = d.link && d.link.url;
394
+ out.push({ id: d.key, title: d.title, kind: lk ? 'Link · ' + (d.link.kind || 'page') : d.kind === 'Slides' ? 'Slides' : 'Document', when: whenOf(d.when, t), path: d.path || '', url: lk || '', link: !!lk,
395
+ type: d.type || '', akind: lk ? 'link' : 'doc', at: Date.parse(d.at || d.date || d.updated || d.when || '') || 0 });
396
+ }
397
+ }
398
+ // what a gate needs and is not written: grey rows that say which gate (board 21)
399
+ const st = (v && v.stage) || {}, gate = st.gate, next = (st.next && st.next.name) || '';
400
+ const gateName = gate && gate.label ? gate.label.replace(/^Gate to /, '') : next;
401
+ if (gate && (gate.parts || []).some((x) => !x.ok)) {
402
+ for (const d of o.docs || []) {
403
+ if (!d || d.path || d.link || !d.core || seen.has(key(d.title))) continue;
404
+ if (!/\b(prd|rubric|done test|brief|spec)\b/i.test(d.title) && !gate.parts.some((x) => !x.ok && norm(x.text).includes(norm(d.title).split(' ')[0]))) continue;
405
+ seen.add(key(d.title));
406
+ out.push({ id: d.key, title: d.title, kind: 'Document', missing: 'Not written · needed for the gate to ' + (gateName || 'the next stage'), key: d.key, act: d.act || null });
407
+ }
408
+ // the gate names a document the page has not read a row for ("PRD and rubric not written"): its grey row all the same (board 21)
409
+ const NAMES = { prd: 'PRD', rubric: 'Rubric', 'done test': 'Done test', spec: 'Spec', brief: 'Brief' };
410
+ for (const part of gate.parts.filter((x) => !x.ok)) {
411
+ for (const m of String(part.text || '').matchAll(/\b(prd|rubric|done test|spec|brief)\b/gi)) {
412
+ const title = NAMES[m[1].toLowerCase()];
413
+ if (seen.has(key(title)) || out.some((a) => key(a.title).split(' ').includes(key(title)))) continue;
414
+ seen.add(key(title));
415
+ out.push({ id: 'gate:' + key(title), title, kind: 'Document', missing: 'Not written · needed for the gate to ' + (gateName || 'the next stage') });
416
+ }
417
+ }
418
+ }
419
+ return out;
420
+ }
421
+ const whenOf = (w, t) => { const s = String(w || ''); return day(s) ? dayText(s, t) : s.replace(/^added\s+/, ''); };
422
+
423
+ /* ── what PMOS suggests from the record itself (board 3, 25), next to what lib/proposals.js proposes ── */
424
+ const andList = (a) => (a.length <= 1 ? a.join('') : a.slice(0, -1).join(', ') + ' and ' + a[a.length - 1]);
425
+ /** derivedOf(r, v, o, bd, arts) → { top: [s], rows: Map(uid → s) }. Each s: { id, derived: true, say, why, ask?, move? }. ask: the words ✓ sends
426
+ * to Ask PMOS, which does it in a real session; move: { uid, to, name } ✓ puts a task in a stage. o.dropped: the ids the PM said No to. */
427
+ export function derivedOf(r, v, o, bd, arts) {
428
+ const t = todayOf(o), drop = o.dropped || new Set(), top = [], rows = new Map(), title = v.title || r.title || 'this initiative';
429
+ const live = r.status !== 'done' && !r.ended && !r.closed;
430
+ // the plan end has passed and an open task runs later: move the plan end to that task's date
431
+ const end = day(r.end);
432
+ if (live && end && end < t) {
433
+ const all = ((v.tasks || {}).sections || []).flatMap((s) => (s.top || []).concat(...(s.groups || []).map((g) => g.rows || []))).filter((x) => !x.closed && day(x.end) && day(x.end) >= t);
434
+ const last = all.sort((a, b) => b.end.localeCompare(a.end))[0];
435
+ if (last) top.push({ id: 'd:plan-end', derived: true, say: 'Move the plan end to ' + dayText(last.end, t) + '. It follows ' + last.title + '.',
436
+ why: 'The plan ended on ' + dayText(r.end, t) + ', and ' + last.title + ' is due on ' + dayText(last.end, t) + '.',
437
+ ask: 'Move the plan end of ' + title + ' to ' + last.end + ': it follows the task ' + last.title + '. Tell me what else moves.' });
438
+ }
439
+ // a document the gate needs is not written: PMOS drafts it from what is written, for the PM to read
440
+ const miss = (arts || []).filter((a) => a.missing).map((a) => a.title);
441
+ if (live && miss.length) {
442
+ const n = (arts || []).filter((a) => !a.missing).length, what = andList(miss.map((m) => 'the ' + m));
443
+ top.push({ id: 'd:draft', derived: true, say: 'Let me draft ' + what + (n ? ' from the ' + plural(n, 'document', 'documents') : '') + ', for you to read.',
444
+ why: 'The gate to the next stage needs ' + what + ', and ' + (miss.length === 1 ? 'it is' : 'they are') + ' not written.',
445
+ ask: 'Draft ' + what + ' for ' + title + (n ? ' from its ' + plural(n, 'document', 'documents') : '') + ', for me to read.' });
446
+ }
447
+ // honest state (round 3 point 13): closed or done, with open tasks, is a contradiction PMOS asks about
448
+ const nOpen = (v.tasks || {}).open || 0;
449
+ if ((r.closed || r.ended || r.status === 'done') && nOpen > 0) {
450
+ const w = r.status === 'done' && !r.closed && !r.ended ? 'done' : 'closed';
451
+ top.unshift({ id: 'd:closed-open', derived: true, say: 'It is ' + w + ', but ' + plural(nOpen, 'task is', 'tasks are') + ' open. Close them, or reopen the initiative?',
452
+ why: '', choices: [{ label: 'Close them', ask: 'Close the ' + plural(nOpen, 'open task', 'open tasks') + ' of ' + title + '. It is ' + w + '. Tell me what you closed.' },
453
+ { label: 'Reopen', ask: 'Reopen ' + title + ': it has ' + plural(nOpen, 'open task', 'open tasks') + '. Put it back in the stage it was in before, and tell me where.' }] });
454
+ }
455
+ // one ✦ on the Not in a stage row (round 3 point 21), keyed 'stage:' (the row of no stage)
456
+ const lz = (bd && bd.loose) || [];
457
+ if (lz.length) rows.set('stage:', { id: 'd:loose', derived: true, say: (lz.length === 1 ? 'Put it in ' : 'Put the ' + lz.length + ' tasks in ') + lz[0].name + '?', why: '', moveAll: { uids: lz.map((x) => x.uid), to: lz[0].to, name: lz[0].name } });
458
+ return { top: top.filter((s) => !drop.has(s.id)), rows: new Map([...rows].filter(([, s]) => !drop.has(s.id))) };
459
+ }
460
+
461
+ /* ── one initiative ── */
462
+ export function workOf(r, v, o = {}) {
463
+ const t = todayOf(o);
464
+ const sum = summaryOf(r, v, o), sug = suggestsOf(v), bd = breakdownOf(r, v, o), arts = artefactsOf(v, o);
465
+ const dv = derivedOf(r, v, o, bd, arts);
466
+ const all = sug.top.concat(dv.top), rowS = new Map(sug.rows);
467
+ for (const [k, s] of dv.rows) if (!rowS.has(k)) rowS.set(k, s);
468
+ const dates = [r.start, r.end].concat(bd.rows.flatMap((x) => [x.start, x.end])).filter(Boolean);
469
+ const span = spanOf(dates, o.mode || 'full', t);
470
+ return { title: v.title || r.title || '', summary: sum, overview: overviewOf(r, v, o), suggests: all.slice(0, MAX_SUGGEST), more: sug.more + Math.max(0, all.length - MAX_SUGGEST), rowSuggests: rowS, facts: factsOf(r, v, o),
471
+ rows: bd.rows, many: bd.many, span, today: iso(t), artefacts: arts };
472
+ }
473
+
474
+ /* ── one level up: a program or a product (boards 26, 27) ── */
475
+ /** L: { kind: 'program' | 'product', title, summary, facts: [{ k, v, warn?, note? }], rows: [{ id, kind, title, stage, start, end, done, sub? }],
476
+ * docs: [{ title, kind, when, path?, url? }], props? }. The rows one level down keep their own dates; a row with none has no bar. */
477
+ export function levelOf(L, o = {}) {
478
+ const t = todayOf(o);
479
+ const rows = (L.rows || []).map((x) => {
480
+ const dl = x.end ? dateLate(x.end, t, !x.done) : { text: '', late: false };
481
+ return { ...x, date: dl.text, late: dl.late };
482
+ });
483
+ const span = spanOf(rows.flatMap((x) => [x.start, x.end]).filter(Boolean), o.mode || 'full', t);
484
+ const late = rows.filter((x) => x.late).length;
485
+ // the same Overview shape as an initiative (round 3 point 10): what it is for, its goal, done when; a line with no source offers a draft
486
+ const nm = L.kind === 'product' ? 'product' : 'program', who = L.title || 'this ' + nm;
487
+ const line = (id, k, text, what) => (text ? { id, k, text: String(text), edit: null } : { id, k, text: '', edit: null, miss: k + ' is not written. Draft it with you?',
488
+ ask: 'Draft ' + what + ' for the ' + nm + ' ' + who + ', in two short sentences, for me to read. Say what you read. Do not invent anything the records do not say.' });
489
+ const sum = L.summary && !importNote(L.summary) ? firstSentences(L.summary, 2) : '';
490
+ const overview = { lines: [line('for', 'For', sum, 'what the ' + nm + ' is for'), line('goal', 'Goal', L.goal || '', 'the goal of the ' + nm), line('done', 'Done when', L.done || '', 'how we will know the ' + nm + ' worked')], status: L.status || '' };
491
+ return { kind: L.kind, title: L.title, summary: L.summary || '', overview, facts: L.facts || [], rows, span, today: iso(t), late, artefacts: (L.docs || []).map((d) => ({ ...d, when: whenOf(d.when, t) })),
492
+ suggests: suggestsOf({ props: L.props || null }).top };
493
+ }