create-pmos 0.422.0 → 0.424.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 (102) hide show
  1. package/package.json +25 -25
  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-programs-test.js +28 -0
  8. package/workspace/briefing-premium-adddoc-test.js +1 -1
  9. package/workspace/briefing-premium-board-test.js +12 -10
  10. package/workspace/briefing-premium-initiative-test.js +30 -38
  11. package/workspace/briefing-premium-products-test.js +16 -3
  12. package/workspace/changelog.json +24 -0
  13. package/workspace/guide/briefing-premium.md +1 -1
  14. package/workspace/guide/claude-proposes.md +37 -0
  15. package/workspace/guide/home.md +9 -2
  16. package/workspace/guide/index.md +1 -0
  17. package/workspace/guide/programs.md +1 -0
  18. package/workspace/guide/workflows.md +5 -1
  19. package/workspace/home-pill-test.js +138 -0
  20. package/workspace/ini-workflow-test.js +4 -4
  21. package/workspace/lib/board-premium.js +3 -2
  22. package/workspace/lib/custom-docs.js +65 -3
  23. package/workspace/lib/engine/boards.js +25 -2
  24. package/workspace/lib/headless.js +14 -1
  25. package/workspace/lib/initiative-writer.js +63 -1
  26. package/workspace/lib/plan-items.js +36 -5
  27. package/workspace/lib/pmos-verbs.js +3 -0
  28. package/workspace/lib/programs.js +11 -3
  29. package/workspace/lib/proposal-runs.js +148 -0
  30. package/workspace/lib/proposals.js +370 -0
  31. package/workspace/lib/propose-cli.js +96 -0
  32. package/workspace/lib/route-modules.js +2 -0
  33. package/workspace/lib/routes-board-premium.js +33 -0
  34. package/workspace/lib/routes-boards.js +50 -10
  35. package/workspace/lib/routes-initiatives.js +12 -0
  36. package/workspace/lib/routes-proposals.js +325 -0
  37. package/workspace/lib/routes-task-links.js +336 -0
  38. package/workspace/lib/split-merge-suggest.js +162 -0
  39. package/workspace/lib/split-merge.js +379 -0
  40. package/workspace/lib/task-cli.js +58 -5
  41. package/workspace/lib/task-items.js +14 -2
  42. package/workspace/lib/task-links.js +422 -0
  43. package/workspace/package-lock.json +2 -2
  44. package/workspace/package.json +1 -1
  45. package/workspace/premium-board-actions-test.js +4 -4
  46. package/workspace/premium-core-doc-issue-test.js +2 -2
  47. package/workspace/premium-vw-backlog-test.js +3 -3
  48. package/workspace/premium-vw-item-test.js +3 -3
  49. package/workspace/premium-vw-jira-test.js +6 -6
  50. package/workspace/product-lane-test.js +165 -0
  51. package/workspace/program-panel-test.js +1 -1
  52. package/workspace/public/pm/bd-asks.js +3 -0
  53. package/workspace/public/pm/bd-ini-panel.js +16 -6
  54. package/workspace/public/pm/bd-prog.js +6 -1
  55. package/workspace/public/pm/bd-span.js +69 -15
  56. package/workspace/public/pm/bd-views.js +3 -2
  57. package/workspace/public/pm/cozy-board.css +10 -0
  58. package/workspace/public/pm/cozy-today.css +23 -0
  59. package/workspace/public/pm/cozy-viewer.js +14 -3
  60. package/workspace/public/pm/cozy-vw-shell.css +8 -0
  61. package/workspace/public/pm/home-app.js +45 -14
  62. package/workspace/public/pm/home-ini-model.js +0 -34
  63. package/workspace/public/pm/home-ini.js +72 -204
  64. package/workspace/public/pm/home-task.js +2 -1
  65. package/workspace/public/pm/home-today.js +66 -16
  66. package/workspace/public/pm/ini-groups.js +253 -0
  67. package/workspace/public/pm/ini-links.js +519 -0
  68. package/workspace/public/pm/ini-props.js +218 -0
  69. package/workspace/public/pm/ini-tasks.js +58 -22
  70. package/workspace/public/pm/ini-versions.js +14 -9
  71. package/workspace/public/pm/ini-view-model.js +93 -4
  72. package/workspace/public/pm/ini-view.css +306 -18
  73. package/workspace/public/pm/ini-view.js +161 -36
  74. package/workspace/public/pm/premium-board.js +54 -6
  75. package/workspace/public/pm/premium-vw-item.js +2 -1
  76. package/workspace/public/pm/split-merge.css +77 -0
  77. package/workspace/public/pm/split-merge.js +265 -0
  78. package/workspace/public/pm/stage-line.js +414 -0
  79. package/workspace/public/pm/task-card.css +13 -0
  80. package/workspace/public/pm/task-card.js +42 -5
  81. package/workspace/public/pm/wf-page.css +2 -0
  82. package/workspace/public/pm/wf-page.js +2 -0
  83. package/workspace/public/pm/wf-stage.js +2 -1
  84. package/workspace/server.js +52 -3
  85. package/workspace/skills/initiative-propose.skill +45 -0
  86. package/workspace/split-merge-ui-test.js +175 -0
  87. package/workspace/stage-card-test.js +13 -12
  88. package/workspace/task-card-test.js +3 -2
  89. package/workspace/task-delete-test.js +126 -0
  90. package/workspace/task-links-test.js +239 -0
  91. package/workspace/task-milestones-test.js +1 -1
  92. package/workspace/task-plans-test.js +6 -0
  93. package/workspace/task-views-test.js +1 -1
  94. package/workspace/test-lists/bd-ini-panel.json +8 -2
  95. package/workspace/test-lists/claude-proposes.json +5 -0
  96. package/workspace/test-lists/home-sessions-pill.json +6 -0
  97. package/workspace/test-lists/product-lane.json +6 -0
  98. package/workspace/test-lists/split-merge.json +15 -0
  99. package/workspace/test-lists/task-delete.json +5 -0
  100. package/workspace/test-lists/task-links.json +6 -0
  101. package/workspace/tk-tasks-test.js +186 -57
  102. package/workspace/workflows-setup-test.js +4 -3
@@ -0,0 +1,265 @@
1
+ /* pm/split-merge.js — split one initiative in two, or merge two into one (feedback batch 11 item 14, option A grilled 2026-10-26; canvas
2
+ * In development, row "Split or merge initiatives": XsmA1Suggest, XsmA1Menu, XsmA2Split, XsmA3SplitDone, XsmA5MergeCheck, XsmA7Closed).
3
+ * Claude suggests and the PM approves: the suggestion holds the whole proposal, and one ✓ does it. Nothing is written without a page press.
4
+ *
5
+ * dotHtml(r) the purple dot after a row's name on the Board and Home lists while Claude suggests something for it. Not a "your turn".
6
+ * nameHtml(r, title?) the name and the dot, for a name that clips: the dot stays in view.
7
+ * cardHtml(sg, title) the card under the initiative's summary (pm/ini-view.js draws it): the spark title, a one-line reason, the proposal
8
+ * lines and three icon buttons, ✕ Not now, ✎ Change it first, ✓ Split as suggested / Merge as suggested (data-sm).
9
+ * menuItems(c) the ⋯ items above "Move out of <program>": Split into a new initiative (scissors) and Merge into › (its own program
10
+ * first, then the rest of its board). [] for a row with no record.
11
+ * press(c, act, btn) a card press: 'no' hides the suggestion until the tasks change (POST /api/initiatives/suggest), 'edit' opens the
12
+ * split editor or the merge check with Claude's picks set, 'yes' does it as suggested.
13
+ * c: { row the Board model's row, rows() the open rows of its board, ws, board, product the product's name, program the program it is in
14
+ * or null, pmPost(path, body), say(text, undo?) the page's toast, reload() a new read, openIni(id) select a row and open it,
15
+ * openList(btn, label, rows [{ label, sub, icon, go }]) the page's small menu }
16
+ *
17
+ * The split editor and the merge check are drill-ins of the Viewer (← and ✕ go back to the initiative). After a split the new row is
18
+ * selected and opened; after a merge the one that stays is. Both toasts carry Undo (POST /api/initiatives/sm-undo), which puts both back.
19
+ * On a phone the Viewer is the full-screen sheet, so the card and both views are the phone sheet too. CSS: pm/split-merge.css, loaded here. */
20
+ import * as VW from './cozy-viewer.js';
21
+
22
+ const CSS = new URL('./split-merge.css', import.meta.url).href;
23
+ if (typeof document !== 'undefined' && document.head && !document.querySelector(`link[href="${CSS}"]`)) {
24
+ const l = document.createElement('link'); l.rel = 'stylesheet'; l.href = CSS; document.head.appendChild(l);
25
+ }
26
+ const esc = (s) => String(s == null ? '' : s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
27
+ const SVG = (d, w = 16, sw = 1.8) => `<svg aria-hidden="true" width="${w}" height="${w}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${sw}" stroke-linecap="round" stroke-linejoin="round"><path d="${d}"/></svg>`;
28
+ export const ICON = {
29
+ spark: 'M12 3l1.8 4.7L18.5 9.5l-4.7 1.8L12 16l-1.8-4.7L5.5 9.5l4.7-1.8z',
30
+ scissors: 'M9 6.5a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0zM9 17.5a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0zM20 4L8.6 15.4M14.5 14.5L20 20M8.6 8.6L12 12',
31
+ merge: 'M5 4v3a5 5 0 0 0 5 5h9M5 20v-3a5 5 0 0 1 5-5M16 9l3 3-3 3',
32
+ stay: 'M5 12l5 5 9-10',
33
+ x: 'M6 6l12 12M18 6L6 18', edit: 'M4 20h4L19 9l-4-4L4 16zM14 6l4 4', ok: 'M5 12.5l4.5 4.5L19 7.5',
34
+ swap: 'M7 4L3 8l4 4M3 8h14M17 20l4-4-4-4M21 16H7', down: 'M7 10l5 5 5-5', jira: 'M12 3l9 9-9 9-9-9z', doc: 'M7 3h7l5 5v13H7zM14 3v5h5', task: 'M5 5h14v14H5z',
35
+ };
36
+ const plural = (n, one, many) => n + ' ' + (n === 1 ? one : many);
37
+ /** "3 tasks · 1 ticket · 1 doc": zero is silence. */
38
+ export function countText(k) {
39
+ return [k.tasks ? plural(k.tasks, 'task', 'tasks') : '', k.tickets ? plural(k.tickets, 'ticket', 'tickets') : '', k.docs ? plural(k.docs, 'doc', 'docs') : ''].filter(Boolean).join(' · ');
40
+ }
41
+ const MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
42
+ const md = (iso) => (/^\d{4}-\d{2}-\d{2}/.test(String(iso || '')) ? MON[+iso.slice(5, 7) - 1] + ' ' + +iso.slice(8, 10) : '');
43
+
44
+ /* ── the dot and the card ── */
45
+ export function dotHtml(r) {
46
+ const sg = r && r.suggest; if (!sg) return '';
47
+ return `<i class="sm-dot" role="img" aria-label="${esc('Claude suggests: ' + sg.title)}" title="${esc('Claude suggests: ' + sg.title)}"></i>`;
48
+ }
49
+ /** The row's name with the dot: the name clips and the dot stays in view. Without a suggestion, the name alone. */
50
+ export function nameHtml(r, title) {
51
+ const t = esc(title == null ? (r && r.title) || '' : title);
52
+ return r && r.suggest ? `<span class="sm-nm">${t}</span>${dotHtml(r)}` : t;
53
+ }
54
+ export function cardHtml(sg, title) {
55
+ if (!sg || !sg.kind) return '';
56
+ const split = sg.kind === 'split';
57
+ const lines = split
58
+ ? [[ICON.scissors, sg.name, countText(sg.counts || {})], [ICON.stay, 'Stays here', sg.stays || '']]
59
+ : [[ICON.stay, sg.intoTitle + ' stays', sg.reason ? 'Because ' + sg.reason : ''], [ICON.merge, (title || 'This one') + ' closes', 'Its work moves; its chats, run log and history stay']];
60
+ const ok = split ? 'Split as suggested' : 'Merge as suggested';
61
+ return `<section class="sm-card" role="region" aria-label="${esc('Claude suggests: ' + sg.title)}"><div class="sm-h">${SVG(ICON.spark, 15)}<b>${esc(sg.title)}</b></div>`
62
+ + (sg.why ? `<p class="sm-why">${esc(sg.why)}</p>` : '')
63
+ + lines.map(([d, b, t]) => `<div class="sm-pl">${SVG(d, 15)}<span><b>${esc(b)}</b>${t ? ' · ' + esc(t) : ''}</span></div>`).join('')
64
+ + `<div class="sm-acts"><button type="button" class="sm-ib" data-sm="no" aria-label="Not now" title="Not now">${SVG(ICON.x, 16)}</button>`
65
+ + `<button type="button" class="sm-ib" data-sm="edit" aria-label="Change it first" title="Change it first">${SVG(ICON.edit, 16)}</button>`
66
+ + `<button type="button" class="sm-ib ok" data-sm="yes" aria-label="${ok}" title="${ok}">${SVG(ICON.ok, 16, 2.2)}</button></div></section>`;
67
+ }
68
+ /** The summary of an initiative that was merged (XsmA7Closed): "Merged · Oct 26" and where its work went. */
69
+ export function mergedHtml(m) {
70
+ if (!m || !m.intoId) return '';
71
+ return `<section class="sm-gone" aria-label="Merged"><div class="sm-h q">${SVG(ICON.merge, 15)}<b>Merged${m.at ? ' · ' + esc(m.at) : ''}</b></div>`
72
+ + `<p>Its tasks, tickets and documents are now in <button type="button" class="sm-lk" data-sm-open="${esc(m.intoId)}">${esc(m.into || m.intoId)}</button>.</p></section>`;
73
+ }
74
+
75
+ /* ── the ⋯ items ── */
76
+ const along = (r) => (r && r.wfStage && Number.isFinite(r.wfStage.n) ? r.wfStage.n : 0);
77
+ const openOf = (r) => ((r && r.tasks) || []).filter((t) => t.status !== 'Done' && t.status !== "Won't do" && t.status !== 'Archived').length
78
+ + (((r && r.track && r.track.tickets) || []).filter((k) => k.status !== 'Done').length);
79
+ function targets(c) {
80
+ const me = c.row, mine = new Set(((c.program && c.program.members) || []).map((m) => m && m.initiative));
81
+ const L = (c.rows() || []).filter((x) => x && x.id && x.id !== me.id && !(x.acts && x.acts.archived));
82
+ return L.filter((x) => mine.has(x.id)).concat(L.filter((x) => !mine.has(x.id)));
83
+ }
84
+ export function menuItems(c) {
85
+ if (!c || !c.row || !c.row.id) return [];
86
+ const out = [{ label: 'Split into a new initiative', icon: ICON.scissors, on: () => openSplit(c) }];
87
+ const T = targets(c);
88
+ if (T.length) {
89
+ out.push({ label: 'Merge into', icon: ICON.merge, note: '›', on: (b) => c.openList(b, 'Merge into', T.map((x) => ({ label: x.title, sub: (x.wfStage && x.wfStage.name) || x.stage || '', icon: ICON.merge, go: () => openMerge(c, x.id) }))) });
90
+ }
91
+ return out;
92
+ }
93
+
94
+ /* ── the card's presses ── */
95
+ export async function press(c, act) {
96
+ const r = c.row, sg = r && r.suggest; if (!sg) return;
97
+ if (act === 'no') {
98
+ // the pressed button holds the focus inside the Viewer, and a host does not redraw a body that holds it
99
+ if (typeof document !== 'undefined' && document.activeElement && document.activeElement.blur) document.activeElement.blur();
100
+ const x = await c.pmPost('/api/initiatives/suggest', { ws: c.ws, id: r.id, of: sg.kind, fp: sg.fp }).catch(() => null);
101
+ if (!x || !x.ok) { c.say((x && x.error) || 'It is still shown. The server did not say why.'); return; }
102
+ c.say('Not now. Claude asks again when the tasks change.');
103
+ await c.reload();
104
+ return;
105
+ }
106
+ if (act === 'edit') { if (sg.kind === 'split') openSplit(c, sg); else openMerge(c, sg.into, sg); return; }
107
+ if (act === 'yes') {
108
+ if (sg.kind === 'split') await doSplit(c, { name: sg.name, tasks: (sg.tasks || []).filter((u) => !isItem(r, u)), tickets: sg.tickets || [], docs: sg.docs || [], items: (sg.tasks || []).filter((u) => isItem(r, u)) });
109
+ else await doMerge(c, r.id, sg.into, {});
110
+ }
111
+ }
112
+ const isItem = (r, uid) => !!((r.tasks || []).find((t) => t.uid === uid && t.item));
113
+
114
+ /* ── the writes, each with Undo ── */
115
+ async function doSplit(c, p) {
116
+ const r = c.row;
117
+ const body = { ws: c.ws, from: r.id, name: p.name, tasks: [...(p.tasks || []), ...(p.items || [])], tickets: p.tickets || [], docs: p.docs || [], ...(p.stage ? { stage: p.stage } : {}) };
118
+ const x = await c.pmPost('/api/initiatives/split', body).catch(() => null);
119
+ if (!x || !x.ok) { c.say((x && x.error) || 'Nothing was split. The server did not say why.'); return false; }
120
+ const k = countText(x.counts || {});
121
+ c.say('Split off ' + x.title + (k ? ' · ' + k : ''), () => undo(c, x.undo, r.id));
122
+ await c.reload();
123
+ c.openIni(x.id);
124
+ return true;
125
+ }
126
+ async function doMerge(c, from, into, p) {
127
+ const x = await c.pmPost('/api/initiatives/merge', { ws: c.ws, from, into, ...(p.anchor ? { anchor: p.anchor } : {}) }).catch(() => null);
128
+ if (!x || !x.ok) { c.say((x && x.error) || 'Nothing was merged. The server did not say why.'); return false; }
129
+ const k = countText(x.counts || {});
130
+ c.say('Merged into ' + (x.title || into) + (k ? ' · ' + k : ''), () => undo(c, x.undo, from));
131
+ await c.reload();
132
+ c.openIni(into);
133
+ return true;
134
+ }
135
+ async function undo(c, op, back) {
136
+ const u = await c.pmPost('/api/initiatives/sm-undo', { ws: c.ws, undo: op }).catch(() => null);
137
+ c.say(u && u.ok ? 'Undone. Both are as they were.' : (u && u.error) || 'The undo did not land.');
138
+ await c.reload();
139
+ if (u && u.ok && back) c.openIni(back);
140
+ }
141
+
142
+ /* ── the split editor (XsmA2Split): Claude's picks are ticked; the PM only changes them ── */
143
+ function stageKeyOf(t) { return t.section || t.itemStage || (t.from && t.from.stage) || ''; }
144
+ export function splitModel(c, preset) {
145
+ const r = c.row, stages = (r.wfStages || []).map((s) => ({ key: s.key, name: s.name }));
146
+ const tasks = (r.tasks || []).filter((t) => t.status !== 'Archived').map((t) => ({ id: t.uid, title: t.title, sub: [stages.find((s) => s.key === stageKeyOf(t)) ? stages.find((s) => s.key === stageKeyOf(t)).name : '', md(t.end)].filter(Boolean).join(' · '), stage: stageKeyOf(t), item: !!t.item }));
147
+ const tickets = ((r.track && r.track.tickets) || []).map((k) => ({ id: k.key, title: k.key + ' ' + (k.title || ''), sub: [k.status, k.assignee].filter(Boolean).join(' · ') }));
148
+ const pick = new Set([...((preset && preset.tasks) || []), ...((preset && preset.tickets) || []), ...((preset && preset.docs) || [])]);
149
+ return { from: r, stages, tasks, tickets, docs: null, pick, name: (preset && preset.name) || '', stage: '', claude: !!preset };
150
+ }
151
+ /** The stage the new one starts at: the PM's pick, else the earliest of the picked tasks', else the one it is in now. */
152
+ export function startOf(S) {
153
+ if (S.stage) return S.stages.find((s) => s.key === S.stage) || null;
154
+ let best = -1;
155
+ for (const t of S.tasks) if (S.pick.has(t.id)) { const i = S.stages.findIndex((s) => s.key === t.stage); if (i >= 0 && (best < 0 || i < best)) best = i; }
156
+ if (best < 0) { const n = S.from.wfStage && S.from.wfStage.key; best = Math.max(0, S.stages.findIndex((s) => s.key === n)); }
157
+ return S.stages[best] || null;
158
+ }
159
+ function pickRow(x, on, icon) {
160
+ return `<button type="button" class="sm-pk${on ? ' on' : ''}" data-sm-pick="${esc(x.id)}" role="checkbox" aria-checked="${on}"><span class="sm-cb${on ? ' on' : ''}" aria-hidden="true">${on ? SVG(ICON.ok, 13, 2.6) : ''}</span>`
161
+ + `${icon ? `<span class="sm-ki">${SVG(icon, 15)}</span>` : ''}<span class="sm-t"><b>${esc(x.title)}</b>${x.sub ? `<span>${esc(x.sub)}</span>` : ''}</span></button>`;
162
+ }
163
+ function groupHtml(label, list, S, icon) {
164
+ if (!list || !list.length) return '';
165
+ const n = list.filter((x) => S.pick.has(x.id)).length, all = n === list.length, some = n > 0 && !all;
166
+ return `<div class="sm-gh"><button type="button" class="sm-cb${all ? ' on' : some ? ' some' : ''}" data-sm-all="${esc(label)}" role="checkbox" aria-checked="${all ? 'true' : some ? 'mixed' : 'false'}" aria-label="${esc('Pick all ' + label.toLowerCase())}">${all ? SVG(ICON.ok, 13, 2.6) : some ? '<i></i>' : ''}</button><b>${esc(label)}</b>${n ? `<span class="n">${n}</span>` : ''}</div>`
167
+ + list.map((x) => pickRow(x, S.pick.has(x.id), icon)).join('');
168
+ }
169
+ export function splitHtml(c, S) {
170
+ const r = S.from, st = startOf(S);
171
+ const keep = [c.product, c.program && c.program.title, (r.kr && r.kr.label) || r.anchor].filter(Boolean);
172
+ const k = { tasks: S.tasks.filter((x) => S.pick.has(x.id)).length, tickets: S.tickets.filter((x) => S.pick.has(x.id)).length, docs: (S.docs || []).filter((x) => S.pick.has(x.id)).length };
173
+ const can = !!(S.name.trim() && (k.tasks || k.tickets || k.docs));
174
+ const stageSel = S.stages.length ? `<label class="sm-st">starts at <select data-sm-stage aria-label="The stage it starts at">${S.stages.map((s) => `<option value="${esc(s.key)}"${st && st.key === s.key ? ' selected' : ''}>${esc(s.name)}</option>`).join('')}</select>${SVG(ICON.down, 13)}</label>` : '';
175
+ return `<div class="sm-v" data-sm-v="split"><div class="sm-nf">${SVG(ICON.scissors, 17)}<input id="smName" type="text" maxlength="120" aria-label="Name of the new initiative" placeholder="Name the new initiative" value="${esc(S.name)}">`
176
+ + (S.claude && S.name ? `<span class="sm-sg" title="Claude named it from the picked tasks">${SVG(ICON.spark, 15)}</span>` : '') + '</div>'
177
+ + `<p class="sm-keep">${keep.length ? 'Keeps ' + keep.map(esc).join(' · ') + (stageSel ? ' · ' : '') : ''}${stageSel}</p>`
178
+ + `<div class="sm-list">${groupHtml('Tasks', S.tasks, S, null)}${groupHtml('Jira tickets', S.tickets, S, ICON.jira)}${S.docs == null ? '<p class="sm-q">Reading the documents…</p>' : groupHtml('Documents', S.docs, S, ICON.doc)}`
179
+ + (!S.tasks.length && !S.tickets.length && S.docs && !S.docs.length ? '<p class="sm-q">It has no tasks, tickets or documents to move yet.</p>' : '') + '</div>'
180
+ + `<div class="sm-foot"><span class="sm-cnt">${esc(countText(k) || 'Pick what moves')}</span><button type="button" class="sm-ib" data-sm-x aria-label="Cancel" title="Cancel">${SVG(ICON.x, 17)}</button>`
181
+ + `<button type="button" class="sm-ib ok" data-sm-go${can ? '' : ' disabled'} aria-label="Split into a new initiative" title="Split">${SVG(ICON.scissors, 17)}</button></div></div>`;
182
+ }
183
+ export function openSplit(c, preset) {
184
+ const S = splitModel(c, preset);
185
+ VW.drill();
186
+ VW.open({ kind: 'Split', title: 'Split ' + c.row.title, icon: ICON.scissors, id: 'sm-split:' + c.row.id, render: (body) => {
187
+ const draw = () => { const f = document.activeElement && document.activeElement.id === 'smName'; body.innerHTML = splitHtml(c, S); if (f) { const i = body.querySelector('#smName'); i.focus(); i.setSelectionRange(i.value.length, i.value.length); } };
188
+ draw();
189
+ fetch('/api/custom-docs?ws=' + encodeURIComponent(c.ws || '') + '&scope=' + encodeURIComponent('initiative:' + c.row.id), { credentials: 'same-origin' }).then((x) => x.json()).catch(() => null).then((x) => {
190
+ S.docs = x && x.ok ? (x.docs || []).map((d) => ({ id: d.id, title: d.title, sub: d.writer || '' })) : [];
191
+ draw();
192
+ });
193
+ body.oninput = (e) => { if (e.target.id === 'smName') { S.name = e.target.value; S.claude = false; const g = body.querySelector('[data-sm-go]'); if (g) g.disabled = !(S.name.trim() && S.pick.size); const s = body.querySelector('.sm-sg'); if (s) s.remove(); } };
194
+ body.onchange = (e) => { if (e.target.matches('[data-sm-stage]')) { S.stage = e.target.value; } };
195
+ body.onclick = async (e) => {
196
+ const b = e.target.closest('button'); if (!b) return;
197
+ const d = b.dataset;
198
+ if (d.smPick) { if (S.pick.has(d.smPick)) S.pick.delete(d.smPick); else S.pick.add(d.smPick); draw(); return; }
199
+ if (d.smAll) { const L = d.smAll === 'Tasks' ? S.tasks : d.smAll === 'Jira tickets' ? S.tickets : S.docs || []; const all = L.every((x) => S.pick.has(x.id)); for (const x of L) { if (all) S.pick.delete(x.id); else S.pick.add(x.id); } draw(); return; }
200
+ if ('smX' in d) { VW.back(); return; }
201
+ if ('smGo' in d) {
202
+ b.disabled = true;
203
+ const ok = await doSplit(c, { name: S.name.trim(), stage: S.stage || '', tasks: S.tasks.filter((x) => S.pick.has(x.id)).map((x) => x.id), tickets: S.tickets.filter((x) => S.pick.has(x.id)).map((x) => x.id), docs: (S.docs || []).filter((x) => S.pick.has(x.id)).map((x) => x.id) });
204
+ if (!ok) b.disabled = false;
205
+ }
206
+ };
207
+ } });
208
+ }
209
+
210
+ /* ── the merge check (XsmA5MergeCheck): the one that closes on top, the one that stays under it; only the real differences ── */
211
+ const hasPrd = (r) => !!((r && r.trail && r.trail.links && r.trail.links.prd) || (r && r.track && r.track.links && r.track.links.prd) || (r && r.prd));
212
+ export function stayReason(go, stay) {
213
+ if (along(stay) > along(go)) return openOf(stay) > openOf(go) ? 'further along, with more open work' : 'further along';
214
+ if (along(stay) === along(go) && openOf(stay) > openOf(go)) return 'more open work';
215
+ return 'the one you picked';
216
+ }
217
+ export function mergeModel(c, intoId, preset) {
218
+ const rows = c.rows() || [], into = rows.find((x) => x.id === intoId) || null;
219
+ const S = { a: c.row, b: into, anchor: '', claude: !!preset };
220
+ S.anchor = (preset && preset.anchor && into && preset.anchor === ((into.kr && into.kr.label) || into.anchor) ? into.anchor : '') || (into && into.anchor) || '';
221
+ return S;
222
+ }
223
+ function cardOf(r, gone) {
224
+ const k = { tasks: ((r.tasks || []).filter((t) => t.status !== 'Archived')).length, tickets: ((r.track && r.track.tickets) || []).length };
225
+ const sub = gone ? countText(k) || 'Nothing to move' : [(r.wfStage && r.wfStage.name) || r.stage, (r.kr && r.kr.label) || r.anchor].filter(Boolean).join(' · ');
226
+ return `<div class="sm-mc${gone ? ' gone' : ' stay'}"><span class="sm-vi">${SVG(gone ? ICON.merge : ICON.stay, 15)}</span><span class="sm-t"><b>${esc(r.title)}</b><span>${esc(sub)}</span></span></div>`;
227
+ }
228
+ export function mergeHtml(c, S) {
229
+ const a = S.a, b = S.b;
230
+ if (!b) return '<div class="sm-v"><p class="sm-q">That initiative is not on this board any more.</p></div>';
231
+ const lbl = (r) => (r.kr && r.kr.label) || r.anchor || 'No anchor';
232
+ const diffs = [];
233
+ if ((a.anchor || '') !== (b.anchor || '') && a.anchor) {
234
+ const opt = (r) => `<button type="button" role="radio" aria-checked="${S.anchor === (r.anchor || '')}" data-sm-anchor="${esc(r.anchor || '')}">${esc(lbl(r))}${r === b ? `<span class="sm-sg2" title="Claude’s pick">${SVG(ICON.spark, 12)}</span>` : ''}</button>`;
235
+ diffs.push(`<div class="sm-df"><span class="sm-dl">Anchor</span><div class="sm-seg" role="radiogroup" aria-label="Anchor">${opt(a)}${opt(b)}</div></div>`);
236
+ }
237
+ const prd = hasPrd(a) && hasPrd(b) ? `<p class="sm-dn">${SVG(ICON.doc, 15)}Both have a PRD. ${esc(b.title)}’s stays the PRD; the other joins its documents.</p>`
238
+ : hasPrd(a) ? `<p class="sm-dn">${SVG(ICON.doc, 15)}Its PRD joins the documents of ${esc(b.title)}.</p>` : '';
239
+ const k = { tasks: ((a.tasks || []).filter((t) => t.status !== 'Archived')).length, tickets: ((a.track && a.track.tickets) || []).length };
240
+ return `<div class="sm-v" data-sm-v="merge">${cardOf(a, true)}<div class="sm-mid">${SVG(ICON.down, 16)}<button type="button" class="sm-ib" data-sm-swap aria-label="${esc('Swap: keep ' + a.title + ' instead')}" title="Swap which one stays">${SVG(ICON.swap, 16)}</button></div>`
241
+ + `${cardOf(b, false)}<p class="sm-r">Stays: ${esc(stayReason(a, b))}.</p>`
242
+ + (diffs.length || prd ? `<div class="sm-dfs">${diffs.join('')}${prd}</div>` : '')
243
+ + `<div class="sm-foot"><span class="sm-cnt">${esc(countText(k) || 'Nothing moves but the record')}</span><button type="button" class="sm-ib" data-sm-x aria-label="Cancel" title="Cancel">${SVG(ICON.x, 17)}</button>`
244
+ + `<button type="button" class="sm-ib ok" data-sm-go aria-label="${esc('Merge into ' + b.title)}" title="Merge">${SVG(ICON.merge, 17)}</button></div></div>`;
245
+ }
246
+ export function openMerge(c, intoId, preset) {
247
+ const S = mergeModel(c, intoId, preset);
248
+ VW.drill();
249
+ VW.open({ kind: 'Merge', title: 'Merge', icon: ICON.merge, id: 'sm-merge:' + c.row.id, render: (body) => {
250
+ const draw = () => { body.innerHTML = mergeHtml(c, S); };
251
+ draw();
252
+ body.onclick = async (e) => {
253
+ const b = e.target.closest('button'); if (!b) return;
254
+ const d = b.dataset;
255
+ if ('smSwap' in d && S.b) { const t = S.a; S.a = S.b; S.b = t; if (!S.claude || !S.anchor) S.anchor = S.b.anchor || ''; draw(); return; }
256
+ if (d.smAnchor != null) { S.anchor = d.smAnchor; draw(); return; }
257
+ if ('smX' in d) { VW.back(); return; }
258
+ if ('smGo' in d && S.b) {
259
+ b.disabled = true;
260
+ const ok = await doMerge(c, S.a.id, S.b.id, { anchor: S.anchor && S.anchor !== (S.b.anchor || '') ? S.anchor : '' });
261
+ if (!ok) b.disabled = false;
262
+ }
263
+ };
264
+ } });
265
+ }