create-pmos 0.421.0 → 0.423.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 (112) hide show
  1. package/package.json +25 -25
  2. package/payload/KIT-VERSION +1 -1
  3. package/payload/planning/evals/eval-calibration.md +1 -1
  4. package/payload/planning/okrs/health-budgets.md +1 -1
  5. package/payload/scripts/anchor-resolve.js +93 -15
  6. package/payload/state/discoveries.md +1 -1
  7. package/workspace/board-programs-test.js +87 -3
  8. package/workspace/briefing-premium-adddoc-test.js +1 -1
  9. package/workspace/briefing-premium-board-test.js +13 -11
  10. package/workspace/briefing-premium-initiative-test.js +30 -38
  11. package/workspace/changelog.json +24 -0
  12. package/workspace/fixtures/report-api/draft.json +2 -1
  13. package/workspace/guide/briefing-premium.md +2 -1
  14. package/workspace/guide/home.md +14 -4
  15. package/workspace/guide/programs.md +5 -3
  16. package/workspace/guide/workflows.md +5 -1
  17. package/workspace/home-other-product-test.js +55 -0
  18. package/workspace/ini-workflow-test.js +4 -4
  19. package/workspace/lib/board-premium.js +21 -6
  20. package/workspace/lib/custom-docs.js +65 -3
  21. package/workspace/lib/doc-live-root.js +37 -0
  22. package/workspace/lib/engine/anchor-resolve.js +93 -15
  23. package/workspace/lib/engine/boards.js +30 -2
  24. package/workspace/lib/engine/intake.js +1 -1
  25. package/workspace/lib/hold-spans.js +24 -10
  26. package/workspace/lib/initiative-writer.js +63 -1
  27. package/workspace/lib/kg.js +1 -1
  28. package/workspace/lib/plan-items.js +36 -5
  29. package/workspace/lib/programs.js +11 -3
  30. package/workspace/lib/report-draft.js +3 -0
  31. package/workspace/lib/route-modules.js +2 -0
  32. package/workspace/lib/routes-board-premium.js +28 -0
  33. package/workspace/lib/routes-boards.js +92 -13
  34. package/workspace/lib/routes-doc-sheet.js +29 -1
  35. package/workspace/lib/routes-initiatives.js +12 -0
  36. package/workspace/lib/routes-programs.js +44 -5
  37. package/workspace/lib/routes-task-links.js +329 -0
  38. package/workspace/lib/routes-task-plans.js +333 -0
  39. package/workspace/lib/split-merge-suggest.js +162 -0
  40. package/workspace/lib/split-merge.js +379 -0
  41. package/workspace/lib/task-cli.js +73 -13
  42. package/workspace/lib/task-items.js +15 -3
  43. package/workspace/lib/task-links.js +422 -0
  44. package/workspace/lib/task-plans.js +232 -0
  45. package/workspace/lib/vendor/scripts/anchor-resolve.js +93 -15
  46. package/workspace/lib/vendor/scripts/intake.js +1 -1
  47. package/workspace/package-lock.json +2 -2
  48. package/workspace/package.json +1 -1
  49. package/workspace/premium-board-actions-test.js +2 -2
  50. package/workspace/premium-core-doc-issue-test.js +2 -2
  51. package/workspace/premium-vw-backlog-test.js +3 -3
  52. package/workspace/premium-vw-doc-test.js +26 -3
  53. package/workspace/premium-vw-item-test.js +3 -3
  54. package/workspace/premium-vw-jira-test.js +6 -6
  55. package/workspace/product-lane-test.js +165 -0
  56. package/workspace/program-leave-test.js +203 -0
  57. package/workspace/program-panel-test.js +1 -1
  58. package/workspace/public/pm/bd-acts.js +5 -2
  59. package/workspace/public/pm/bd-ini-panel.js +32 -9
  60. package/workspace/public/pm/bd-prog-panel.js +22 -7
  61. package/workspace/public/pm/bd-prog.js +6 -1
  62. package/workspace/public/pm/bd-span.js +234 -30
  63. package/workspace/public/pm/bd-views.js +3 -2
  64. package/workspace/public/pm/cozy-board.css +90 -0
  65. package/workspace/public/pm/cozy-home.css +3 -0
  66. package/workspace/public/pm/doc-follow.js +36 -0
  67. package/workspace/public/pm/home-app.js +62 -14
  68. package/workspace/public/pm/home-ini-model.js +0 -34
  69. package/workspace/public/pm/home-ini.js +81 -204
  70. package/workspace/public/pm/home-list.js +2 -2
  71. package/workspace/public/pm/home-today.js +3 -1
  72. package/workspace/public/pm/ini-groups.js +228 -0
  73. package/workspace/public/pm/ini-links.js +561 -0
  74. package/workspace/public/pm/ini-tasks.js +131 -30
  75. package/workspace/public/pm/ini-versions.js +348 -0
  76. package/workspace/public/pm/ini-view-model.js +113 -43
  77. package/workspace/public/pm/ini-view.css +341 -18
  78. package/workspace/public/pm/ini-view.js +153 -38
  79. package/workspace/public/pm/premium-board.js +160 -21
  80. package/workspace/public/pm/premium-report-read.js +20 -1
  81. package/workspace/public/pm/premium-shell.js +29 -4
  82. package/workspace/public/pm/premium-vw-doc.js +46 -11
  83. package/workspace/public/pm/prog-move.js +69 -0
  84. package/workspace/public/pm/split-merge.css +77 -0
  85. package/workspace/public/pm/split-merge.js +265 -0
  86. package/workspace/public/pm/stage-line.js +414 -0
  87. package/workspace/public/pm/task-card.css +3 -0
  88. package/workspace/public/pm/task-card.js +15 -6
  89. package/workspace/public/pm/wf-page.css +2 -0
  90. package/workspace/public/pm/wf-page.js +2 -0
  91. package/workspace/public/pm/wf-stage.js +2 -1
  92. package/workspace/server.js +15 -1
  93. package/workspace/split-merge-ui-test.js +175 -0
  94. package/workspace/stage-card-test.js +12 -11
  95. package/workspace/task-card-test.js +3 -2
  96. package/workspace/task-delete-test.js +126 -0
  97. package/workspace/task-links-test.js +227 -0
  98. package/workspace/task-milestones-test.js +176 -0
  99. package/workspace/task-plans-test.js +181 -0
  100. package/workspace/task-views-test.js +3 -3
  101. package/workspace/test-lists/bd-ini-panel.json +8 -2
  102. package/workspace/test-lists/product-lane.json +6 -0
  103. package/workspace/test-lists/program-leave.json +6 -0
  104. package/workspace/test-lists/split-merge.json +15 -0
  105. package/workspace/test-lists/task-delete.json +5 -0
  106. package/workspace/test-lists/task-links.json +6 -0
  107. package/workspace/test-lists/task-milestones.json +6 -0
  108. package/workspace/test-lists/task-plans.json +6 -0
  109. package/workspace/test-lists/viewer-live-file.json +9 -0
  110. package/workspace/timeline-holds-test.js +61 -4
  111. package/workspace/tk-tasks-test.js +173 -57
  112. package/workspace/workflows-setup-test.js +4 -3
@@ -0,0 +1,329 @@
1
+ 'use strict';
2
+ /* routes-task-links.js — links between tasks that move together (feedback batch 11 item 15; canvas In development, row "Task
3
+ * dependencies"). The fold, the engine and the writer are lib/task-links.js; a task's dates change only through the board's own task edit
4
+ * (routes-boards taskEdit: boards.jsonl, or the plan-items ledger for a breakdown item). A Jira ticket is never written.
5
+ *
6
+ * GET /api/task-links?ws&board&initiative → { ok, links } links: lib/task-links.js viewOf (null: nothing to show)
7
+ * POST /api/task-links/link { ws, board, from, to, type?, lag?, source? } → { ok, from?, moved, cant, batch?, links }
8
+ * a link the PM made (drag a dot onto a bar's end, the search, the joint card's type and lag: the same
9
+ * pair again). Refused when it would loop back. What waits moves just enough at once (the PM's own act);
10
+ * `from` is the link as it was (Undo sends it back, or unlinks a new one)
11
+ * POST /api/task-links/unlink { ws, board, from, to } → { ok, from } `from`: the link as it was, for Undo
12
+ * POST /api/task-links/move { ws, board, task, start?, due? } → { ok, item, moved, cant, asks, batch?, pulled? }
13
+ * the PM's own move (a drag, the date picker) of a task with links: its dates, then what waits on it moves
14
+ * just enough; moving earlier moves nothing and files "Pull N tasks in" (pulled: the count)
15
+ * POST /api/task-links/accept { ws, board, initiative, ids: [id] } → { ok, linked, moved, batch?, links } ✓ or Add all
16
+ * POST /api/task-links/drop { ws, board, initiative, id } → { ok } ✕: a proposal or a recommendation, for good (same source)
17
+ * POST /api/task-links/ask { ws, board, initiative, id, answer: 'yes' | 'no' } → { ok, moved?, outbox?, batch? } Needs you
18
+ * POST /api/task-links/undo { ws, board, batch } → { ok, back, kept } Undo / Undo all (kept: a task changed since, left as it is)
19
+ * POST /api/task-links/sweep { ws, board, initiative } → { ok, moved, links } the quiet moves: when the Tasks section opens
20
+ * POST /api/task-links/quiet { ws, board, initiative, on } → { ok } · /seen { ws, board, initiative } → { ok }
21
+ *
22
+ * sweepFor(ctx, w, boardId, ini, g) the quiet pass, outside HTTP (`pmos task set` after a date change: a date changed outside a drag)
23
+ *
24
+ * Every POST passes pm-guard requirePagePress. With no state folder the read answers no links and every write answers 409. */
25
+ const fs = require('fs');
26
+ const path = require('path');
27
+ const guard = require('./pm-guard.js');
28
+ const TL = require('./task-links.js');
29
+ const RB = require('./routes-boards.js');
30
+
31
+ const isObj = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
32
+ const fail = (ctx, res, status, error, extra = {}) => { ctx.json(res, { ok: false, error, ...extra }, status); return true; };
33
+ const NO_STATE = 'This repository has no PMOS state folder (pmos/state, state or .pmos). Nothing is saved until the folder exists.';
34
+ const LOOP = 'That would loop back to this task';
35
+ const dirOf = (ctx, w) => (ctx.stateDirFor ? ctx.stateDirFor(w) : null);
36
+ const todayIso = () => { const d = new Date(); return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0'); };
37
+ const plural = (n, a, b) => n + ' ' + (n === 1 ? a : b);
38
+
39
+ /* ── what the engine reads: every work item on the board, and the sources Claude proposes from ── */
40
+ function frame(ctx, w, board, o = {}) {
41
+ let rows = RB.baseRows(w);
42
+ // outside the server (`pmos task`), rows still being read are derived now, as the CLI's other verbs do
43
+ if (rows.pending && o.derive) { try { require('./initiative-rows.js').deriveNow(w.root); } catch { /* answered below */ } rows = RB.baseRows(w); }
44
+ if (!rows.ok) return null;
45
+ const fr = RB.frameFor(ctx, w);
46
+ const mine = rows.rows.filter((r) => RB.boardIdOf(r, fr.boards, fr.folded, w) === board.id);
47
+ const items = RB.itemsOf(w, fr, board, mine);
48
+ const T = new Map(), byUid = new Map();
49
+ for (const x of items) { if (!x || x.kind === 'Epic' || !x.uid) continue; const t = TL.taskOf(x); T.set(t.uid, t); byUid.set(t.uid, x); }
50
+ const ends = new Map(); for (const r of mine) if (r.end) ends.set(r.id, TL.dn(r.end));
51
+ return { rows: mine, items, T, byUid, limit: (ini) => (ends.has(ini) ? ends.get(ini) : null) };
52
+ }
53
+ const PRD_CACHE = new Map(); // file → { m, text }
54
+ function prdText(root, ini) {
55
+ try {
56
+ const JT = require('./jira-tickets.js');
57
+ const rel = JT.prdOf(JT.recordFm(root, ini));
58
+ if (!rel) return '';
59
+ const file = path.resolve(root, rel.replace(/^\/+/, ''));
60
+ if (!file.startsWith(path.resolve(root))) return '';
61
+ const st = fs.statSync(file), c = PRD_CACHE.get(file);
62
+ if (c && c.m === st.mtimeMs) return c.text;
63
+ const text = st.size < 400000 ? fs.readFileSync(file, 'utf8') : '';
64
+ PRD_CACHE.set(file, { m: st.mtimeMs, text });
65
+ return text;
66
+ } catch { return ''; }
67
+ }
68
+ /** The sources Claude proposes links from, for one initiative: the PRD's words, the drafted Jira tickets' "depends", the plan's order. */
69
+ function sourcesOf(root, ini, items) {
70
+ let jira = [];
71
+ try { const d = require('./jira-tickets.js').readDraft(root, ini); jira = d ? (d.tickets || []).map((t) => ({ id: t.id, key: t.jira || '', depends: t.depends || [] })) : []; } catch { jira = []; }
72
+ const order = (items || []).filter((x) => x && x.ini === ini && x.store === 'item' && Number.isFinite(x.order)).map((x) => ({ uid: x.uid, order: x.order, section: x.section || x.itemStage || '' }));
73
+ return { prd: prdText(root, ini), jira, order };
74
+ }
75
+ function view(ctx, w, board, ini, f) {
76
+ const F = TL.read(dirOf(ctx, w));
77
+ f = f || frame(ctx, w, board);
78
+ if (!f) return null;
79
+ return TL.viewOf(F, ini, { T: f.T, today: todayIso(), limit: f.limit, src: sourcesOf(w.root, ini, f.items) });
80
+ }
81
+
82
+ /* ── the writes: one task's dates through the board's own edit; a batch the Undo can take back ── */
83
+ function setDates(ctx, p, g, uid, start, due) {
84
+ const body = { ws: p.w.id, board: p.board.id, task: uid };
85
+ if (start !== undefined) body.start = start || '';
86
+ if (due !== undefined) body.due = due || '';
87
+ return RB.taskEdit(ctx, body, g);
88
+ }
89
+ /** Write the engine's moves, then one `moved` line. → [{ task, title, from, to }] the moves that landed. */
90
+ function apply(ctx, p, g, f, ini, moves, via, why) {
91
+ const done = [];
92
+ for (const m of moves) {
93
+ const t = f.T.get(m.uid), to = { start: TL.isoN(m.to.s), due: TL.isoN(m.to.e) }, from = { start: TL.isoN(m.from.s), due: TL.isoN(m.from.e) };
94
+ const x = f.byUid.get(m.uid) || {};
95
+ const r = setDates(ctx, p, g, m.uid, x.start ? to.start : undefined, to.due);
96
+ if (r.body && r.body.ok) done.push({ task: m.uid, title: t ? t.title : m.uid, from, to, why: why || ('waits on ' + ((f.T.get(m.after) || {}).title || m.after)) });
97
+ }
98
+ if (!done.length) return { done, batch: null };
99
+ const batch = TL.newBatch();
100
+ TL.append(dirOf(ctx, p.w), g.actor, { kind: 'moved', board: p.board.id, ini, batch, via, moves: done });
101
+ return { done, batch };
102
+ }
103
+ const cantOf = (S, f) => S.clashes.map((c) => ({ task: c.uid, kind: c.kind, title: (f.T.get(c.uid) || {}).title || c.uid }))
104
+ .concat(S.asks.map((a) => ({ task: a.uid, kind: a.why === 'milestone' ? 'milestone' : a.why, title: (f.T.get(a.uid) || {}).title || a.uid })));
105
+
106
+ /** The quiet pass for one initiative: keep the approved links after a date changed outside a drag. → { moved: n, batch } */
107
+ function sweep(ctx, p, g, ini, o = {}) {
108
+ const dir = dirOf(ctx, p.w), F = TL.read(dir);
109
+ if (!F.links.size || F.quiet.has(ini)) return { moved: 0, batch: null };
110
+ const f = frame(ctx, p.w, p.board, o); if (!f) return { moved: 0, batch: null };
111
+ const links = TL.linksOf(F).filter((l) => f.T.has(l.from) && f.T.has(l.to) && (f.T.get(l.from).ini === ini || f.T.get(l.to).ini === ini));
112
+ if (!links.length) return { moved: 0, batch: null };
113
+ const S = TL.settle(f.T, links, { today: TL.dn(todayIso()), ini, mode: 'quiet', limit: f.limit });
114
+ const mv = S.moves.filter((m) => f.T.get(m.uid).ini === ini);
115
+ if (!mv.length) return { moved: 0, batch: null };
116
+ const r = apply(ctx, p, g, f, ini, mv, 'quiet');
117
+ return { moved: r.done.length, batch: r.batch };
118
+ }
119
+ function sweepFor(ctx, w, boardId, ini, g) {
120
+ try {
121
+ if (!dirOf(ctx, w)) return { moved: 0 };
122
+ const fr = RB.frameFor(ctx, w), board = fr.boards.find((b) => b.id === boardId);
123
+ if (!board) return { moved: 0 };
124
+ return sweep(ctx, { w, fr, board, file: RB.ledgerFile(ctx, w) }, g, ini, { derive: true });
125
+ } catch { return { moved: 0 }; }
126
+ }
127
+
128
+ /** The PM's own act, then what waits on `seed` moves just enough (promises, Jira and Done stay; they come back as asks and clashes). */
129
+ function ownMove(ctx, p, g, seed, ini) {
130
+ const F = TL.read(dirOf(ctx, p.w)), f = frame(ctx, p.w, p.board);
131
+ if (!f || !f.T.has(seed)) return { moved: [], cant: [], batch: null };
132
+ const S = TL.settle(f.T, TL.linksOf(F).filter((l) => f.T.has(l.from) && f.T.has(l.to)), { seeds: [seed], today: TL.dn(todayIso()), ini, mode: 'own', limit: f.limit });
133
+ const r = apply(ctx, p, g, f, ini, S.moves, 'own');
134
+ return { moved: r.done, cant: cantOf(S, f), batch: r.batch };
135
+ }
136
+
137
+ const READS = {
138
+ '/api/task-links': (ctx, res, url) => {
139
+ const p = RB.postFrame(ctx, res, { ws: url.searchParams.get('ws') || null, board: url.searchParams.get('board') || '' });
140
+ if (!p) return;
141
+ const ini = url.searchParams.get('initiative') || '';
142
+ if (!ini) return fail(ctx, res, 400, 'initiative is required');
143
+ ctx.json(res, { ok: true, links: dirOf(ctx, p.w) ? view(ctx, p.w, p.board, ini) : null });
144
+ },
145
+ };
146
+
147
+ const WRITES = {
148
+ '/api/task-links/link': (ctx, res, body, g, p) => {
149
+ const c = TL.cleanLink(body);
150
+ if (c.error) return fail(ctx, res, 400, c.error);
151
+ const f = frame(ctx, p.w, p.board);
152
+ if (!f) return fail(ctx, res, 202, 'The board is still being read. Try again in a moment.');
153
+ const a = f.T.get(c.link.from), b = f.T.get(c.link.to);
154
+ if (!a || !b) return fail(ctx, res, 404, `no task ${!a ? c.link.from : c.link.to} on this board`);
155
+ const dir = dirOf(ctx, p.w), F = TL.read(dir), k = TL.keyOf(c.link.from, c.link.to);
156
+ const others = TL.linksOf(F).filter((l) => TL.keyOf(l.from, l.to) !== k && TL.keyOf(l.to, l.from) !== k);
157
+ if (TL.loops(others, c.link.from, c.link.to)) return fail(ctx, res, 409, LOOP);
158
+ const was = F.links.get(k) || null, back = F.links.get(TL.keyOf(c.link.to, c.link.from)) || null;
159
+ if (back) TL.append(dir, g.actor, { kind: 'unlink', from: back.from, to: back.to });
160
+ const ini = b.ini || a.ini;
161
+ TL.append(dir, g.actor, { kind: 'link', board: p.board.id, ini, ...c.link, source: typeof body.source === 'string' && body.source ? body.source.slice(0, 20) : 'you' });
162
+ const m = ownMove(ctx, p, g, c.link.from, ini);
163
+ ctx.json(res, { ok: true, from: was ? { type: was.type, lag: was.lag } : null, swapped: !!back, moved: m.moved, cant: m.cant, batch: m.batch, links: view(ctx, p.w, p.board, ini) });
164
+ },
165
+ '/api/task-links/unlink': (ctx, res, body, g, p) => {
166
+ const from = String(body.from || ''), to = String(body.to || ''), dir = dirOf(ctx, p.w), F = TL.read(dir);
167
+ const was = F.links.get(TL.keyOf(from, to));
168
+ if (!was) return fail(ctx, res, 404, 'no such link');
169
+ TL.append(dir, g.actor, { kind: 'unlink', from, to });
170
+ ctx.json(res, { ok: true, from: { from, to, type: was.type, lag: was.lag, source: was.source } });
171
+ },
172
+ '/api/task-links/move': (ctx, res, body, g, p) => {
173
+ const uid = String(body.task || '');
174
+ const f = frame(ctx, p.w, p.board);
175
+ if (!f) return fail(ctx, res, 202, 'The board is still being read. Try again in a moment.');
176
+ const t0 = f.T.get(uid);
177
+ if (!t0) return fail(ctx, res, 404, `no task ${uid || '(none named)'} on this board`);
178
+ if (t0.jira) return fail(ctx, res, 409, 'Jira owns this ticket: its dates change in Jira');
179
+ const x0 = f.byUid.get(uid) || {};
180
+ const e = setDates(ctx, p, g, uid, body.start, body.due);
181
+ if (!e.body || e.body.ok !== true) return ctx.json(res, e.body || { ok: false, error: 'the write was refused' }, e.status || 500);
182
+ const from = { start: x0.start || '', due: x0.end || '' };
183
+ // later: what waits follows; earlier: nothing moves, Claude files "Pull N tasks in"
184
+ const s1 = body.start != null && body.start !== '' ? TL.dn(body.start) : t0.s, e1 = body.due != null && body.due !== '' ? TL.dn(body.due) : t0.e;
185
+ const shift = Math.max(e1 != null && t0.e != null ? e1 - t0.e : 0, s1 != null && t0.s != null && body.due == null ? s1 - t0.s : 0);
186
+ const early = Math.min(e1 != null && t0.e != null ? e1 - t0.e : 0, s1 != null && t0.s != null ? s1 - t0.s : 0);
187
+ let m = { moved: [], cant: [], batch: null }, pulled = 0;
188
+ if (shift > 0 || (s1 != null && t0.s != null && s1 > t0.s)) m = ownMove(ctx, p, g, uid, t0.ini);
189
+ else if (early < 0) {
190
+ const F = TL.read(dirOf(ctx, p.w)), T = new Map(f.T); T.set(uid, { ...t0, s: s1, e: e1 });
191
+ const P = TL.pullOf(T, TL.linksOf(F), uid, -early, TL.dn(todayIso()));
192
+ if (P.length) { TL.append(dirOf(ctx, p.w), g.actor, { kind: 'pull', board: p.board.id, ini: t0.ini, id: TL.newBatch().replace('mv-', 'pl-'), after: uid, days: -early, tasks: P.map((x) => x.uid) }); pulled = P.length; }
193
+ }
194
+ // one batch for Undo: the task's own change first, then what moved with it
195
+ const batch = TL.newBatch();
196
+ TL.append(dirOf(ctx, p.w), g.actor, { kind: 'moved', board: p.board.id, ini: t0.ini, batch, via: 'own', moves: [{ task: uid, title: t0.title, from, to: { start: body.start != null ? body.start : from.start, due: body.due != null ? body.due : from.due }, why: 'you moved it' }, ...m.moved] });
197
+ if (m.batch) TL.append(dirOf(ctx, p.w), g.actor, { kind: 'undo', batch: m.batch }); // folded into the one batch above
198
+ ctx.json(res, { ok: true, item: e.body.item || null, moved: m.moved, cant: m.cant, batch, pulled });
199
+ },
200
+ '/api/task-links/accept': (ctx, res, body, g, p) => {
201
+ const ini = String(body.initiative || ''), ids = Array.isArray(body.ids) ? body.ids.filter((x) => typeof x === 'string').slice(0, 40) : [];
202
+ if (!ini || !ids.length) return fail(ctx, res, 400, 'initiative and ids are required');
203
+ const f = frame(ctx, p.w, p.board);
204
+ if (!f) return fail(ctx, res, 202, 'The board is still being read. Try again in a moment.');
205
+ const v = view(ctx, p.w, p.board, ini, f), dir = dirOf(ctx, p.w);
206
+ const want = (v ? v.proposals : []).filter((x) => ids.includes(x.id));
207
+ if (!want.length) return fail(ctx, res, 409, 'Those proposals are no longer open');
208
+ const linked = [], moved = [];
209
+ let F = TL.read(dir);
210
+ for (const x of want) {
211
+ if (x.kind === 'link') {
212
+ if (TL.loops(TL.linksOf(F), x.from, x.to)) continue;
213
+ TL.append(dir, g.actor, { kind: 'link', board: p.board.id, ini, from: x.from, to: x.to, type: x.type, lag: x.lag || 0, source: x.source });
214
+ linked.push({ from: x.from, to: x.to }); F = TL.read(dir);
215
+ } else if (x.kind === 'pull') {
216
+ const f2 = frame(ctx, p.w, p.board);
217
+ const mv = x.tasks.map((u) => { const t = f2.T.get(u); const P = { uid: u, from: { s: t.s, e: t.e }, to: { s: t.s == null ? null : t.s - x.days, e: t.e == null ? null : t.e - x.days }, after: x.after }; return P; });
218
+ const r = apply(ctx, p, g, f2, ini, mv, 'ask', 'pulled in after ' + ((f2.T.get(x.after) || {}).title || x.after));
219
+ moved.push(...r.done); TL.append(dir, g.actor, { kind: 'drop', sig: x.id });
220
+ } else if (x.kind === 'move') {
221
+ const f2 = frame(ctx, p.w, p.board);
222
+ const mv = x.tasks.filter((m) => f2.T.has(m.task)).map((m) => ({ uid: m.task, from: { s: TL.dn(m.from.start), e: TL.dn(m.from.due) }, to: { s: TL.dn(m.to.start), e: TL.dn(m.to.due) } }));
223
+ const r = apply(ctx, p, g, f2, ini, mv, 'ask', 'to keep your links');
224
+ moved.push(...r.done);
225
+ }
226
+ }
227
+ // what the new links ask for now moves just enough (the PM's ✓ is the approval)
228
+ for (const l of linked) { const m = ownMove(ctx, p, g, l.from, ini); moved.push(...m.moved); }
229
+ ctx.json(res, { ok: true, linked, moved, links: view(ctx, p.w, p.board, ini) });
230
+ },
231
+ '/api/task-links/drop': (ctx, res, body, g, p) => {
232
+ const id = String(body.id || '');
233
+ if (!/^[pamju]:/.test(id) || id.length > 300) return fail(ctx, res, 400, 'id names a proposal or a recommendation');
234
+ TL.append(dirOf(ctx, p.w), g.actor, { kind: 'drop', sig: id });
235
+ ctx.json(res, { ok: true });
236
+ },
237
+ '/api/task-links/ask': (ctx, res, body, g, p) => {
238
+ const ini = String(body.initiative || ''), id = String(body.id || ''), yes = body.answer === 'yes';
239
+ if (!ini || !id || (body.answer !== 'yes' && body.answer !== 'no')) return fail(ctx, res, 400, 'initiative, id and answer (yes or no) are required');
240
+ const f = frame(ctx, p.w, p.board);
241
+ if (!f) return fail(ctx, res, 202, 'The board is still being read. Try again in a moment.');
242
+ const v = view(ctx, p.w, p.board, ini, f), a = v ? v.asks.find((x) => x.id === id) : null, dir = dirOf(ctx, p.w);
243
+ if (!a) return fail(ctx, res, 409, 'That is no longer waiting on you');
244
+ if (!yes) { TL.append(dir, g.actor, { kind: 'drop', sig: id }); return ctx.json(res, { ok: true }); }
245
+ if (a.kind === 'jira') {
246
+ let entry = null;
247
+ try {
248
+ const { repoScript } = require('./repo-script.js');
249
+ const outbox = require(repoScript('outbox.js')), store = require('./outbox-store.js');
250
+ const e = outbox.draftOutbox({ channel: 'jira-write', recipient: a.task, subject: 'Start date of ' + a.task, body: a.comment, origin: 'pm' });
251
+ entry = { id: 'out-' + require('crypto').randomBytes(6).toString('hex'), ...e };
252
+ store.appendEvent(ctx.runtimeDir, { event: 'queued', ...entry }, new Date());
253
+ } catch (err) { return fail(ctx, res, 500, 'The comment could not go to the outbox: ' + ((err && err.message) || err)); }
254
+ TL.append(dir, g.actor, { kind: 'outbox', sig: id, entry: entry.id });
255
+ TL.append(dir, g.actor, { kind: 'drop', sig: id });
256
+ return ctx.json(res, { ok: true, outbox: entry.id });
257
+ }
258
+ // a promise: the move Claude recommends, then what waits on it (the ✓ lets this one task move)
259
+ const x = f.byUid.get(a.task) || {};
260
+ const e = setDates(ctx, p, g, a.task, x.start ? a.to.start : undefined, a.to.due);
261
+ if (!e.body || e.body.ok !== true) return ctx.json(res, e.body || { ok: false, error: 'the write was refused' }, e.status || 500);
262
+ const t = f.T.get(a.task), m = ownMove(ctx, p, g, a.task, ini);
263
+ const batch = TL.newBatch();
264
+ TL.append(dir, g.actor, { kind: 'moved', board: p.board.id, ini, batch, via: 'ask', moves: [{ task: a.task, title: t.title, from: { start: x.start || '', due: x.end || '' }, to: a.to, why: 'you approved it' }, ...m.moved] });
265
+ if (m.batch) TL.append(dir, g.actor, { kind: 'undo', batch: m.batch });
266
+ ctx.json(res, { ok: true, moved: m.moved.length + 1, batch });
267
+ },
268
+ '/api/task-links/undo': (ctx, res, body, g, p) => {
269
+ const dir = dirOf(ctx, p.w), F = TL.read(dir), id = String(body.batch || '');
270
+ const b = F.moved.find((x) => x.batch === id);
271
+ if (!b || F.undone.has(id)) return fail(ctx, res, 409, 'Nothing to undo');
272
+ const f = frame(ctx, p.w, p.board);
273
+ if (!f) return fail(ctx, res, 202, 'The board is still being read. Try again in a moment.');
274
+ let back = 0, kept = 0;
275
+ for (const m of b.moves.slice().reverse()) {
276
+ const x = f.byUid.get(m.task);
277
+ // a task changed since keeps its dates: Undo never overwrites a later change
278
+ if (!x || (x.start || '') !== (m.to.start || x.start || '') || (x.end || '') !== (m.to.due || '')) { kept++; continue; }
279
+ const r = setDates(ctx, p, g, m.task, m.from.start !== undefined ? m.from.start : undefined, m.from.due);
280
+ if (r.body && r.body.ok) back++; else kept++;
281
+ }
282
+ TL.append(dir, g.actor, { kind: 'undo', batch: id });
283
+ ctx.json(res, { ok: true, back, kept });
284
+ },
285
+ '/api/task-links/sweep': (ctx, res, body, g, p) => {
286
+ const ini = String(body.initiative || '');
287
+ if (!ini) return fail(ctx, res, 400, 'initiative is required');
288
+ const r = sweep(ctx, p, g, ini);
289
+ ctx.json(res, { ok: true, moved: r.moved, links: r.moved ? view(ctx, p.w, p.board, ini) : undefined });
290
+ },
291
+ '/api/task-links/quiet': (ctx, res, body, g, p) => {
292
+ const ini = String(body.initiative || '');
293
+ if (!ini || typeof body.on !== 'boolean') return fail(ctx, res, 400, 'initiative and on (true or false) are required');
294
+ TL.append(dirOf(ctx, p.w), g.actor, { kind: 'quiet', ini, on: body.on });
295
+ ctx.json(res, { ok: true });
296
+ },
297
+ '/api/task-links/seen': (ctx, res, body, g, p) => {
298
+ const ini = String(body.initiative || '');
299
+ if (!ini) return fail(ctx, res, 400, 'initiative is required');
300
+ TL.append(dirOf(ctx, p.w), g.actor, { kind: 'seen', ini });
301
+ ctx.json(res, { ok: true });
302
+ },
303
+ };
304
+
305
+ module.exports = {
306
+ prefixes: ['/api/task-links'],
307
+ sweepFor, sourcesOf, frame, LOOP,
308
+ handle: async (req, res, url, ctx) => {
309
+ const p0 = url.pathname;
310
+ if (req.method === 'GET') {
311
+ const fn = READS[p0];
312
+ if (!fn) return fail(ctx, res, 404, 'not found');
313
+ fn(ctx, res, url);
314
+ return true;
315
+ }
316
+ if (req.method !== 'POST') return fail(ctx, res, 405, 'GET or POST only');
317
+ const fn = WRITES[p0];
318
+ if (!fn) return fail(ctx, res, 404, 'not found');
319
+ const got = await ctx.readJson(req);
320
+ if (!got.ok || !isObj(got.body)) return fail(ctx, res, 400, 'the body must be a JSON object');
321
+ const body = got.body, g = guard.requirePagePress(req, body);
322
+ if (!g.ok) return fail(ctx, res, g.status, g.error);
323
+ const p = RB.postFrame(ctx, res, body);
324
+ if (!p) return true;
325
+ if (!p.file || !dirOf(ctx, p.w)) return fail(ctx, res, 409, NO_STATE);
326
+ fn(ctx, res, body, g, p);
327
+ return true;
328
+ },
329
+ };