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,422 @@
1
+ 'use strict';
2
+ /* task-links.js — links between tasks that move together (feedback batch 11 item 15; canvas In development, row "Task dependencies":
3
+ * DepAGrill, DepA8Suggest, DepA9Adjust, DepA1Timeline–DepA7Phone, DepA0Rules). THE ONE WRITER and THE ONE FOLD of <state>/task-links.jsonl,
4
+ * beside boards.jsonl, and the pure engine that keeps dates in line. lib/routes-task-links.js does the I/O; pm/ini-links.js draws it.
5
+ *
6
+ * A LINK says one task waits on another: { from, to, type, lag }. `from` is the task waited on, `to` the task that waits; each is a task
7
+ * uid (a Board task, a breakdown item) or a Jira key. The four types name the two ends joined, `to`'s bound on the left:
8
+ * fs finish → start `to` starts after `from` finishes (start ≥ from.due + 1 + lag) the default
9
+ * ss start → start `to` starts when `from` starts (start ≥ from.start + lag)
10
+ * ff finish → finish `to` finishes when `from` finishes (due ≥ from.due + lag)
11
+ * sf start → finish `to` finishes when `from` starts (due ≥ from.start + lag)
12
+ * A link that would loop back is refused. Claude never adds a link the PM did not approve: a proposal is only a proposal until ✓.
13
+ *
14
+ * THE RULES (DepAGrill, 16 questions):
15
+ * - Claude proposes links (the PRD's "once", "after", "with"; a Jira ticket's "depends"; the order of the plan; dates that already butt
16
+ * end to start); ✓ each or Add all; ✕ drops one for good, unless its source changes (the signature holds the source).
17
+ * - The PM's own move (a drag, the date picker): the tasks after it move just enough (slack first), and the ones after those; the toast
18
+ * says "Moved N tasks with it" with Undo. Moving earlier moves nothing: Claude proposes "Pull N tasks in".
19
+ * - A date that changes outside a drag (chat, a Jira read, a late task): inside approved links Claude moves the tasks after it quietly,
20
+ * no toast; the grey ✦ chip counts the moves since last seen, Undo all. Per initiative the quiet moves can be turned off: then every
21
+ * such move is a proposal. Claude adjusts Live only, never a draft (a draft's tasks are not here).
22
+ * - Where Claude stops: a promise would move (a ◇ milestone task, the committed end, another initiative's task). It recommends one move:
23
+ * ✓ applies it, ✕ keeps the date and the link turns orange. A Jira ticket never moves: the link turns orange with "!", and Claude
24
+ * drafts a comment to the assignee; ✓ puts it in the outbox. A Done task stays where it is.
25
+ * - A late task keeps its own date (it is a promise); the tasks after it start no earlier than tomorrow, with an orange start edge.
26
+ *
27
+ * THE LEDGER. One line per change, { at, by, kind, ... }:
28
+ * link { board, ini, from, to, type, lag, source } a link the PM approved (✓, Add all, a drag, the joint card, `pmos task link`);
29
+ * the last line for a pair wins, so the joint card's type and lag are a new line
30
+ * unlink { from, to }
31
+ * propose { board, ini, from, to, type, lag, source, why } a link an agent filed for the PM to approve (`pmos task link --propose`)
32
+ * drop { sig } a proposal or a recommendation the PM dropped (✕); a new source is a new signature
33
+ * quiet { ini, on } the PM's switch for the quiet moves of one initiative (on by default)
34
+ * moved { board, ini, batch, via: 'quiet' | 'own' | 'ask', moves: [{ task, title, from: { start, due }, to: { start, due }, why }] }
35
+ * undo { batch } Undo or Undo all of a batch
36
+ * seen { ini } the PM opened the grey chip: the moves before it are seen
37
+ * pull { board, ini, id, after, days, tasks: [task] } "Pull N tasks in": the PM moved `after` earlier by `days`
38
+ * outbox { sig, entry } the drafted Jira comment went to the outbox
39
+ *
40
+ * ledgerFile(dir) · read(dir) → fold · append(dir, actor, line)
41
+ * fold(lines) PURE → { links, proposed, drops, quiet, moved, undone, seen, pulls, sent, t }
42
+ * linksOf(F) · loops(links, from, to) · keyOf(from, to) · TYPES
43
+ * needOf(link, a, today) → { start?, due? } the earliest day numbers `to` may take, given `from` (a)
44
+ * settle(T, links, o) PURE → { moves, asks, clashes } the engine (see below)
45
+ * proposalsOf(ini, T, links, F, src) PURE → [proposal]
46
+ * viewOf(F, ini, T, o) → what r.links carries for the one view (null: nothing to show)
47
+ * decorate(rows, F, items, o) puts r.links on each Board row that has something to show
48
+ */
49
+ const fs = require('fs');
50
+ const path = require('path');
51
+ const crypto = require('crypto');
52
+
53
+ const LEDGER = 'task-links.jsonl';
54
+ const KINDS = new Set(['link', 'unlink', 'propose', 'drop', 'quiet', 'moved', 'undo', 'seen', 'pull', 'outbox']);
55
+ const TYPES = ['fs', 'ss', 'ff', 'sf'];
56
+ const TYPE_WORDS = { fs: 'Finish → start', ss: 'Start → start', ff: 'Finish → finish', sf: 'Start → finish' };
57
+ const CLOSED = new Set(['Done', "Won't do", 'Archived']);
58
+ const isObj = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
59
+ const str = (v) => (typeof v === 'string' ? v : '');
60
+ const UID_RE = /^[A-Za-z0-9][A-Za-z0-9:._-]{0,120}$/;
61
+ const MAX_LAG = 365;
62
+ const MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
63
+
64
+ /* ── days as whole numbers (UTC) ── */
65
+ const DAY = /^(\d{4})-(\d{2})-(\d{2})/;
66
+ const dn = (iso) => { const m = DAY.exec(String(iso || '')); return m ? Math.round(Date.UTC(+m[1], +m[2] - 1, +m[3]) / 864e5) : null; };
67
+ const isoN = (n) => (n == null ? '' : new Date(n * 864e5).toISOString().slice(0, 10));
68
+ const md = (n) => { if (n == null) return ''; const d = new Date(n * 864e5); return MON[d.getUTCMonth()] + ' ' + d.getUTCDate(); };
69
+ const keyOf = (from, to) => from + '>' + to;
70
+ const hash = (s) => crypto.createHash('sha1').update(String(s)).digest('hex').slice(0, 10);
71
+ const newBatch = () => 'mv-' + crypto.randomBytes(5).toString('hex');
72
+
73
+ /* ── the ledger ── */
74
+ function ledgerFile(dir) { return dir ? path.join(dir, LEDGER) : null; }
75
+ function readLines(file) {
76
+ if (!file) return [];
77
+ let text = '';
78
+ try { text = fs.readFileSync(file, 'utf8'); } catch { return []; }
79
+ const out = [];
80
+ for (const raw of text.split('\n')) { if (!raw.trim()) continue; try { const l = JSON.parse(raw); if (isObj(l) && KINDS.has(l.kind)) out.push(l); } catch { /* a torn line is skipped */ } }
81
+ return out;
82
+ }
83
+ function read(dir) { return fold(readLines(ledgerFile(dir))); }
84
+ function append(dir, actor, ev) {
85
+ const line = { at: new Date().toISOString(), by: (actor && actor.by) || null, ...ev };
86
+ fs.appendFileSync(ledgerFile(dir), JSON.stringify(line) + '\n');
87
+ return line;
88
+ }
89
+
90
+ /** A link's fields, cleaned: → { link } | { error }. */
91
+ function cleanLink(o) {
92
+ if (!isObj(o)) return { error: 'a link is an object' };
93
+ const from = str(o.from), to = str(o.to);
94
+ if (!UID_RE.test(from) || !UID_RE.test(to)) return { error: 'from and to are task ids or Jira keys' };
95
+ if (from === to) return { error: 'A task cannot wait on itself' };
96
+ const type = o.type == null ? 'fs' : str(o.type).toLowerCase();
97
+ if (!TYPES.includes(type)) return { error: 'type is one of ' + TYPES.join(', ') };
98
+ const lag = o.lag == null || o.lag === '' ? 0 : Number(o.lag);
99
+ if (!Number.isInteger(lag) || Math.abs(lag) > MAX_LAG) return { error: `lag is whole days, between -${MAX_LAG} and ${MAX_LAG}` };
100
+ return { link: { from, to, type, lag } };
101
+ }
102
+
103
+ /** PURE. The ledger's lines, in order, into what holds now. */
104
+ function fold(lines) {
105
+ const links = new Map(); // key(from, to) → { from, to, type, lag, source, ini, board, at, by }
106
+ const proposed = new Map(); // key → the agent-filed proposal (until linked, unlinked or dropped)
107
+ const drops = new Set(); // signatures the PM dropped
108
+ const quiet = new Map(); // ini → false when the PM turned the quiet moves off
109
+ const moved = []; // the batches, oldest first
110
+ const undone = new Set(); // batch ids
111
+ const seen = new Map(); // ini → iso of the last time the PM opened the grey chip
112
+ const pulls = new Map(); // id → pull
113
+ const sent = new Map(); // sig → outbox entry id
114
+ let t = '';
115
+ for (const l of lines || []) {
116
+ if (!isObj(l) || !KINDS.has(l.kind)) continue;
117
+ if (l.at && String(l.at) > t) t = String(l.at);
118
+ if (l.kind === 'link' || l.kind === 'propose') {
119
+ const c = cleanLink(l); if (c.error) continue;
120
+ const k = keyOf(c.link.from, c.link.to);
121
+ const x = { ...c.link, source: str(l.source) || 'you', why: str(l.why), ini: str(l.ini), board: str(l.board), at: l.at || null, by: l.by || null };
122
+ if (l.kind === 'link') { links.set(k, x); proposed.delete(k); } else if (!links.has(k)) proposed.set(k, x);
123
+ } else if (l.kind === 'unlink') {
124
+ const k = keyOf(str(l.from), str(l.to)); links.delete(k); proposed.delete(k);
125
+ } else if (l.kind === 'drop' && str(l.sig)) drops.add(l.sig);
126
+ else if (l.kind === 'quiet' && str(l.ini) && typeof l.on === 'boolean') { if (l.on) quiet.delete(l.ini); else quiet.set(l.ini, false); }
127
+ else if (l.kind === 'moved' && str(l.batch) && Array.isArray(l.moves)) moved.push({ batch: l.batch, ini: str(l.ini), board: str(l.board), via: str(l.via) || 'quiet', at: l.at || null, moves: l.moves.filter(isObj) });
128
+ else if (l.kind === 'undo' && str(l.batch)) undone.add(l.batch);
129
+ else if (l.kind === 'seen' && str(l.ini)) seen.set(l.ini, l.at || '');
130
+ else if (l.kind === 'pull' && str(l.id)) pulls.set(l.id, { id: l.id, ini: str(l.ini), board: str(l.board), after: str(l.after), days: Number(l.days) || 0, tasks: Array.isArray(l.tasks) ? l.tasks.filter((x) => typeof x === 'string') : [], at: l.at || null });
131
+ else if (l.kind === 'outbox' && str(l.sig)) sent.set(l.sig, str(l.entry));
132
+ }
133
+ for (const k of proposed.keys()) { const p = proposed.get(k); if (drops.has(sigOf(p))) proposed.delete(k); }
134
+ return { links, proposed, drops, quiet, moved, undone, seen, pulls, sent, t };
135
+ }
136
+ const linksOf = (F) => [...F.links.values()];
137
+
138
+ /** Would from → to close a loop? (to already reaches from along the links.) */
139
+ function loops(links, from, to) {
140
+ const next = new Map();
141
+ for (const l of links) { if (!next.has(l.from)) next.set(l.from, []); next.get(l.from).push(l.to); }
142
+ const seen = new Set([to]), q = [to];
143
+ while (q.length) { const a = q.shift(); if (a === from) return true; for (const b of next.get(a) || []) if (!seen.has(b)) { seen.add(b); q.push(b); } }
144
+ return false;
145
+ }
146
+
147
+ /* ── the engine ── */
148
+ /** A task as the engine reads it, from a work item (routes-boards itemsOf) or a Board row's task: day numbers and what it may do. */
149
+ function taskOf(x) {
150
+ const s = dn(x.start), e = dn(x.end != null ? x.end : x.due);
151
+ return { uid: x.uid || x.key, title: str(x.title), ini: str(x.ini), status: str(x.status) || 'To do', s: s == null ? e : s, e: e == null ? s : e,
152
+ jira: x.src === 'jira' || !!x.jira, ms: !!x.milestone, closed: CLOSED.has(x.status), item: !!x.item || x.store === 'item' };
153
+ }
154
+ /** The dates the engine reads `a` at: a late open task keeps its own date, but what waits on it cannot start before tomorrow. */
155
+ function effOf(a, today) {
156
+ if (a.closed || today == null) return { s: a.s, e: a.e };
157
+ const late = a.e != null && a.e < today, unstarted = a.status === 'To do' && a.s != null && a.s < today;
158
+ return { s: unstarted ? today : a.s, e: late ? today : a.e };
159
+ }
160
+ /** The earliest day numbers `to` may take, given `from` (a, as the engine reads it). */
161
+ function needOf(l, a, today) {
162
+ const e = effOf(a, today), lag = l.lag || 0;
163
+ if (l.type === 'fs') return e.e == null ? {} : { start: e.e + 1 + lag };
164
+ if (l.type === 'ss') return e.s == null ? {} : { start: e.s + lag };
165
+ if (l.type === 'ff') return e.e == null ? {} : { due: e.e + lag };
166
+ return e.s == null ? {} : { due: e.s + lag };
167
+ }
168
+ /** How many days `b` must move later to meet the need (0: it meets it). */
169
+ function shortOf(need, b) {
170
+ let d = 0;
171
+ if (need.start != null && b.s != null) d = Math.max(d, need.start - b.s);
172
+ if (need.due != null && b.e != null) d = Math.max(d, need.due - b.e);
173
+ return d;
174
+ }
175
+ /** Is moving `b` a promise only the PM's ✓ moves? o: { ini, limit (day number: the committed end) } → the reason, or ''. */
176
+ function promiseOf(b, to, o) {
177
+ if (b.ms) return 'milestone';
178
+ if (o.ini && b.ini && b.ini !== o.ini) return 'other';
179
+ if (o.limit != null && to.e != null && to.e > o.limit && (b.e == null || b.e <= o.limit)) return 'end';
180
+ return '';
181
+ }
182
+
183
+ /**
184
+ * PURE. Keep the links: move what waits, just enough, from the seeds outward (all the links' tasks when no seed is given).
185
+ * T Map uid → task (taskOf), the tasks the links name; a uid not in T is not known here and is left alone
186
+ * links [link] the approved links
187
+ * o { seeds?: [uid], today (day number), ini (the initiative the move started in), limit?: (ini) → day number | null,
188
+ * mode: 'own' (the PM's drag: no toast here) | 'quiet', allow?: Set uid (a promise the PM's ✓ lets move) }
189
+ * → { moves: [{ uid, from: { s, e }, to: { s, e }, after, link }], asks: [{ uid, kind: 'promise', why, to: { s, e }, after, link }],
190
+ * clashes: [{ uid, kind: 'jira' | 'done', need, days, after, link }] }
191
+ * A move keeps the task's length. A Done task, a Jira ticket and a promise are not moved; what waits on them is not moved by this pass.
192
+ */
193
+ function settle(T, links, o = {}) {
194
+ const out = { moves: [], asks: [], clashes: [] };
195
+ const cur = new Map(); for (const [k, v] of T) cur.set(k, { ...v });
196
+ const by = new Map(); for (const l of links) { if (!by.has(l.from)) by.set(l.from, []); by.get(l.from).push(l); }
197
+ const allow = o.allow || new Set(), today = o.today == null ? null : o.today;
198
+ const q = (o.seeds && o.seeds.length ? o.seeds : [...new Set(links.map((l) => l.from))]).filter((u) => cur.has(u));
199
+ const asked = new Set(), clashed = new Set();
200
+ let guard = 0;
201
+ while (q.length && guard++ < 5000) {
202
+ const a = cur.get(q.shift());
203
+ for (const l of by.get(a.uid) || []) {
204
+ const b = cur.get(l.to); if (!b) continue;
205
+ const need = needOf(l, a, today), d = shortOf(need, b);
206
+ if (d <= 0) continue;
207
+ const to = { s: b.s == null ? null : b.s + d, e: b.e == null ? null : b.e + d };
208
+ if (b.closed || b.jira) { const k = l.from + '>' + l.to; if (!clashed.has(k)) { clashed.add(k); out.clashes.push({ uid: b.uid, kind: b.jira ? 'jira' : 'done', need, days: d, after: a.uid, link: l }); } continue; }
209
+ const p = allow.has(b.uid) ? '' : promiseOf(b, to, { ini: o.ini, limit: o.limit ? o.limit(b.ini) : null });
210
+ if (p) { if (!asked.has(b.uid)) { asked.add(b.uid); out.asks.push({ uid: b.uid, kind: 'promise', why: p, from: { s: b.s, e: b.e }, to, days: d, after: a.uid, link: l }); } continue; }
211
+ const prev = out.moves.find((m) => m.uid === b.uid);
212
+ if (prev) { prev.to = to; prev.after = a.uid; prev.link = l; } else out.moves.push({ uid: b.uid, from: { s: b.s, e: b.e }, to, after: a.uid, link: l });
213
+ cur.set(b.uid, { ...b, s: to.s, e: to.e });
214
+ q.push(b.uid);
215
+ }
216
+ }
217
+ return out;
218
+ }
219
+ /** The tasks a move earlier leaves room for: what waited tight on `uid` (and tight on those) and can come in by up to `days`. PURE. */
220
+ function pullOf(T, links, uid, days, today) {
221
+ const by = new Map(); for (const l of links) { if (!by.has(l.from)) by.set(l.from, []); by.get(l.from).push(l); }
222
+ const out = [], seen = new Set([uid]), q = [{ u: uid, n: days }];
223
+ while (q.length) {
224
+ const { u, n } = q.shift(), a = T.get(u); if (!a) continue;
225
+ for (const l of by.get(u) || []) {
226
+ const b = T.get(l.to); if (!b || seen.has(b.uid) || b.closed || b.jira || b.ms || b.ini !== a.ini || b.status !== 'To do') continue;
227
+ // the room: how far b may come in now that a moved earlier (a is read at its new dates)
228
+ const need = needOf(l, a, today);
229
+ let room = 0;
230
+ if (need.start != null && b.s != null) room = b.s - need.start;
231
+ else if (need.due != null && b.e != null) room = b.e - need.due;
232
+ const m = Math.min(n, room);
233
+ if (m <= 0 || (today != null && b.s != null && b.s - m <= today)) continue;
234
+ seen.add(b.uid); out.push({ uid: b.uid, days: m });
235
+ T.set(b.uid, { ...b, s: b.s == null ? null : b.s - m, e: b.e == null ? null : b.e - m });
236
+ q.push({ u: b.uid, n: m });
237
+ }
238
+ }
239
+ return out;
240
+ }
241
+
242
+ /* ── what Claude proposes ── */
243
+ const STOP = new Set(['with', 'from', 'that', 'this', 'into', 'once', 'after', 'when', 'then', 'their', 'there', 'have', 'been', 'will', 'each', 'every', 'plan', 'task', 'tasks', 'about', 'before', 'until', 'which', 'what', 'them', 'they', 'also', 'only', 'more', 'than', 'over', 'under', 'whether']);
244
+ const words = (s) => String(s || '').toLowerCase().replace(/[^a-z0-9]+/g, ' ').split(' ').filter((w) => w.length >= 4 && !STOP.has(w));
245
+ /** The task a piece of text names: the one whose title shares the most words with it — 2 at least, or 1 that no other title has. */
246
+ function namedIn(text, tasks) {
247
+ const ws = new Set(words(text)), seen = new Map();
248
+ for (const t of tasks) for (const w of new Set(words(t.title))) seen.set(w, (seen.get(w) || 0) + 1);
249
+ let best = null, score = 0;
250
+ for (const t of tasks) {
251
+ const tw = [...new Set(words(t.title))]; if (!tw.length) continue;
252
+ const hit = tw.filter((w) => ws.has(w));
253
+ const ok = hit.length >= 2 || (hit.length === 1 && hit[0].length >= 5 && seen.get(hit[0]) === 1);
254
+ if (ok && hit.length > score) { best = t; score = hit.length; }
255
+ }
256
+ return best;
257
+ }
258
+ /** The PRD's sentences that say an order: "B once A", "B after A", "after A, B", "B with A". → [{ from, to, type, text }] */
259
+ function prdOrders(text, tasks) {
260
+ const out = [];
261
+ const body = String(text || '').replace(/^---\n[\s\S]*?\n---\n?/, '').replace(/[`*_>#|]/g, ' ');
262
+ for (const raw of body.split(/(?<=[.!?;])\s+|\n+/)) {
263
+ const s = raw.replace(/\s+/g, ' ').trim(); if (s.length < 12 || s.length > 400) continue;
264
+ let m = /^(?:only\s+)?(?:once|after|when)\s+(.+?),\s*(.+)$/i.exec(s);
265
+ let A = null, Bt = null, type = 'fs';
266
+ if (m) { A = m[1]; Bt = m[2]; }
267
+ else if ((m = /^(.+?)\s+(?:only\s+)?(?:once|after)\s+(.+)$/i.exec(s))) { Bt = m[1]; A = m[2]; }
268
+ else if ((m = /^(.+?)\s+(?:together\s+)?with\s+(.+)$/i.exec(s))) { Bt = m[1]; A = m[2]; type = 'ff'; }
269
+ if (!m) continue;
270
+ const a = namedIn(A, tasks), b = namedIn(Bt, tasks);
271
+ if (a && b && a.uid !== b.uid) out.push({ from: a.uid, to: b.uid, type, text: s.length > 110 ? s.slice(0, 107) + '…' : s });
272
+ }
273
+ return out;
274
+ }
275
+ /** A proposal's signature: the pair, the type and its source, so a new source (a new PRD line, new dates) is proposed again. */
276
+ function sigOf(p) { return 'p:' + keyOf(p.from, p.to) + ':' + p.type + ':' + hash(p.source + '|' + (p.why || '')); }
277
+
278
+ /**
279
+ * PURE. What Claude proposes for one initiative, newest knowledge first; never a pair already linked (either way), a loop, a closed task,
280
+ * or a proposal the PM dropped (same source).
281
+ * T Map uid → task (taskOf) — this initiative's open tasks and tickets
282
+ * src { prd: text | '', jira: [{ id, key, depends: [id] }], order: [{ uid, order, section }] }
283
+ */
284
+ function proposalsOf(ini, T, links, F, src = {}) {
285
+ const out = [], have = new Set();
286
+ for (const l of links) { have.add(keyOf(l.from, l.to)); have.add(keyOf(l.to, l.from)); }
287
+ const mine = [...T.values()].filter((t) => t.ini === ini && !t.closed);
288
+ const push = (p) => {
289
+ const k = keyOf(p.from, p.to);
290
+ if (have.has(k) || out.some((x) => keyOf(x.from, x.to) === k || keyOf(x.to, x.from) === k)) return;
291
+ const a = T.get(p.from), b = T.get(p.to); if (!a || !b || a.closed || b.closed) return;
292
+ if (loops(links, p.from, p.to)) return;
293
+ const sig = sigOf(p); if (F.drops.has(sig)) return;
294
+ out.push({ ...p, lag: p.lag || 0, id: sig });
295
+ };
296
+ // filed by an agent (the PM asked in chat for a proposal)
297
+ for (const p of F.proposed.values()) if (p.ini === ini || T.has(p.to)) push({ from: p.from, to: p.to, type: p.type, lag: p.lag, source: 'agent', why: p.why || 'Proposed in chat' });
298
+ // the PRD's words
299
+ for (const p of prdOrders(src.prd, mine)) push({ from: p.from, to: p.to, type: p.type, source: 'prd', why: 'PRD: “' + p.text + '”' });
300
+ // Jira: a drafted ticket that depends on another
301
+ const keyOfId = new Map((src.jira || []).filter((t) => t.key).map((t) => [t.id, t.key]));
302
+ for (const t of src.jira || []) for (const d of t.depends || []) { const a = keyOfId.get(d); if (a && t.key) push({ from: a, to: t.key, type: 'fs', source: 'jira', why: 'Jira: ' + t.key + ' depends on ' + a }); }
303
+ // the order of the plan: one breakdown item after the one before it, in one section, where the dates agree
304
+ const ord = (src.order || []).filter((x) => T.has(x.uid)).sort((x, y) => String(x.section).localeCompare(String(y.section)) || x.order - y.order);
305
+ for (let i = 1; i < ord.length; i++) {
306
+ if (ord[i].section !== ord[i - 1].section) continue;
307
+ const a = T.get(ord[i - 1].uid), b = T.get(ord[i].uid);
308
+ if (a.e != null && b.s != null && a.e < b.s) push({ from: a.uid, to: b.uid, type: 'fs', source: 'plan', why: 'The order of the plan' });
309
+ }
310
+ // dates that already butt end to start (the next day, or over a weekend)
311
+ // (one per task each way: a task that already waits on one, or is already waited on, is left out — a guess, not a web)
312
+ const dated = mine.filter((t) => t.s != null && t.e != null && !t.jira), took = new Set();
313
+ for (const l of links) { took.add('to:' + l.to); took.add('from:' + l.from); }
314
+ for (const p of out) { took.add('to:' + p.to); took.add('from:' + p.from); }
315
+ for (const b of dated.slice().sort((x, y) => x.s - y.s)) {
316
+ if (took.has('to:' + b.uid)) continue;
317
+ const a = dated.find((x) => x !== b && !took.has('from:' + x.uid) && (b.s - x.e === 1 || (new Date(x.e * 864e5).getUTCDay() === 5 && b.s - x.e === 3)));
318
+ if (!a) continue;
319
+ const n = out.length;
320
+ push({ from: a.uid, to: b.uid, type: 'fs', source: 'dates', why: 'The dates meet: ' + clip(a.title, 40) + ' ends ' + md(a.e) + ', ' + clip(b.title, 40) + ' starts ' + md(b.s) });
321
+ if (out.length > n) { took.add('to:' + b.uid); took.add('from:' + a.uid); }
322
+ }
323
+ return out.slice(0, 30);
324
+ }
325
+ const clip = (s, n) => { s = String(s || ''); return s.length > n ? s.slice(0, n - 1) + '…' : s; };
326
+
327
+ /**
328
+ * What r.links carries for one initiative, or null when nothing is linked, proposed or waiting:
329
+ * { links: [{ from, to, type, lag, source, other?: { ini, title }, state: 'ok' | 'late' | 'jira' | 'promise' | 'done' }],
330
+ * proposals: [{ id, from, to, type, lag, source, why, kind: 'link' | 'pull' | 'move', ... }],
331
+ * moved: { n, batches: [{ batch, at, moves }], tasks: [uid] } the quiet moves since the PM last opened the grey chip
332
+ * asks: [{ id, kind: 'promise' | 'jira', task, why, to: { start, due }, after, text }] "Needs you"
333
+ * late: [uid] the tasks that wait on a late task (orange start edge)
334
+ * quiet: true | false }
335
+ * o: { today (iso), T (Map of every task the links name), limit(ini), titles: Map uid → { ini, title }, src }
336
+ */
337
+ function viewOf(F, ini, o) {
338
+ const T = o.T, today = dn(o.today);
339
+ const mineL = linksOf(F).filter((l) => (T.get(l.from) && T.get(l.from).ini === ini) || (T.get(l.to) && T.get(l.to).ini === ini));
340
+ const all = linksOf(F).filter((l) => T.has(l.from) && T.has(l.to));
341
+ // the state of each link now: met, or why not
342
+ const late = new Set();
343
+ const links = mineL.map((l) => {
344
+ const a = T.get(l.from), b = T.get(l.to);
345
+ const x = { from: l.from, to: l.to, type: l.type, lag: l.lag, source: l.source, state: 'ok' };
346
+ const oth = a && a.ini !== ini ? a : b && b.ini !== ini ? b : null;
347
+ if (oth) x.other = { ini: oth.ini, title: oth.title, tile: (oth.ini || '').slice(0, 2).toUpperCase() };
348
+ else if (!a || !b) x.other = { ini: '', title: (o.titles && o.titles.get(!a ? l.from : l.to)) || (!a ? l.from : l.to), tile: '' };
349
+ if (!a || !b) return x;
350
+ const d = shortOf(needOf(l, a, today), b);
351
+ const aLate = !a.closed && a.e != null && today != null && a.e < today;
352
+ if (aLate && !b.closed) { x.state = 'late'; late.add(b.uid); }
353
+ if (d > 0) x.state = b.jira ? 'jira' : b.closed ? 'done' : promiseOf(b, { e: b.e + d }, { ini, limit: o.limit ? o.limit(b.ini) : null }) || b.ms ? 'promise' : aLate ? 'late' : 'short';
354
+ return x;
355
+ });
356
+ // the late chain: everything downstream of a late task
357
+ const by = new Map(); for (const l of all) { if (!by.has(l.from)) by.set(l.from, []); by.get(l.from).push(l.to); }
358
+ const q = [...late]; while (q.length) { const u = q.shift(); for (const v of by.get(u) || []) { const t = T.get(v); if (t && !t.closed && !late.has(v)) { late.add(v); q.push(v); } } }
359
+ // Needs you: what keeping a link would move that only the PM moves, and a Jira ticket a link says starts too early
360
+ const asks = [];
361
+ const S = settle(T, all.filter((l) => T.get(l.from).ini === ini || T.get(l.to).ini === ini), { today, ini, mode: 'quiet', limit: o.limit });
362
+ for (const a of S.asks) {
363
+ const b = T.get(a.uid), id = 'a:' + a.uid + ':' + isoN(a.to.s) + ':' + isoN(a.to.e);
364
+ if (F.drops.has(id)) continue;
365
+ const word = a.why === 'milestone' ? 'It is a milestone (' + md(b.e) + ')' : a.why === 'other' ? 'It is in another initiative' : 'It would end after the committed date';
366
+ asks.push({ id, kind: 'promise', why: a.why, task: a.uid, title: b.title, ms: b.ms, days: a.days, to: { start: isoN(a.to.s), due: isoN(a.to.e) }, after: a.after,
367
+ text: word + '. Claude recommends moving it ' + (a.days === 1 ? 'a day' : a.days + ' days') + '; ' + clip((T.get(a.after) || {}).title, 40) + ' needs it.' });
368
+ }
369
+ for (const c of S.clashes) {
370
+ if (c.kind !== 'jira') continue;
371
+ const b = T.get(c.uid), a = T.get(c.after), id = 'j:' + c.uid + ':' + c.link.from + ':' + isoN(b.s);
372
+ if (F.drops.has(id)) continue;
373
+ const want = c.need.start != null ? c.need.start : b.s + c.days;
374
+ asks.push({ id, kind: 'jira', task: c.uid, title: b.title, days: c.days, after: c.after, sent: F.sent.has(id),
375
+ head: c.uid + ' starts ' + md(b.s) + ', ' + (c.days === 1 ? 'a day' : c.days + ' days') + ' too early',
376
+ text: 'Jira, so PMOS can’t move it. Claude drafted a comment asking to start ' + md(want) + '.',
377
+ comment: 'Could ' + c.uid + ' start on ' + md(want) + ' or later? It waits on "' + clip(a ? a.title : c.after, 80) + '", which finishes ' + md(effOf(a, today).e) + '.' });
378
+ }
379
+ // the proposals: Claude's links, Pull N tasks in, and (with the quiet moves off) the moves Claude would have made
380
+ const proposals = o.withProposals === false ? [] : proposalsOf(ini, T, all, F, o.src || {}).map((p) => ({ ...p, kind: 'link' }));
381
+ for (const p of F.pulls.values()) {
382
+ if (p.ini !== ini || F.drops.has('u:' + p.id)) continue;
383
+ const ok = p.tasks.filter((u) => T.has(u) && !T.get(u).closed);
384
+ if (!ok.length) continue;
385
+ proposals.unshift({ id: 'u:' + p.id, kind: 'pull', days: p.days, tasks: ok, after: p.after, why: 'You moved ' + clip((T.get(p.after) || {}).title || p.after, 40) + ' earlier' });
386
+ }
387
+ const quiet = !F.quiet.has(ini);
388
+ if (!quiet) {
389
+ const mv = S.moves;
390
+ if (mv.length) {
391
+ const id = 'm:' + hash(mv.map((m) => m.uid + isoN(m.to.s) + isoN(m.to.e)).join('|'));
392
+ if (!F.drops.has(id)) proposals.unshift({ id, kind: 'move', tasks: mv.map((m) => ({ task: m.uid, title: (T.get(m.uid) || {}).title, to: { start: isoN(m.to.s), due: isoN(m.to.e) }, from: { start: isoN(m.from.s), due: isoN(m.from.e) } })), why: 'To keep your links' });
393
+ }
394
+ }
395
+ // the quiet moves since the PM last looked
396
+ const since = F.seen.get(ini) || '';
397
+ const batches = F.moved.filter((b) => b.ini === ini && b.via === 'quiet' && !F.undone.has(b.batch) && String(b.at || '') > since).reverse();
398
+ const movedTasks = [...new Set(batches.flatMap((b) => b.moves.map((m) => m.task)))];
399
+ const moved = { n: batches.reduce((n, b) => n + b.moves.length, 0), batches, tasks: movedTasks };
400
+ if (!links.length && !proposals.length && !moved.n && !asks.length) return quiet ? null : { links, proposals, moved, asks, late: [], quiet, t: F.t };
401
+ return { links, proposals, moved, asks, late: [...late], quiet, t: F.t };
402
+ }
403
+
404
+ /** Each Board row's r.links (null rows are left as they are). items: the board's work items (Jira tickets too). o: { today, src(ini) } */
405
+ function decorate(rows, F, items, o = {}) {
406
+ if (!F || (!F.links.size && !F.proposed.size && !F.moved.length && !F.pulls.size && !o.src)) return rows;
407
+ const T = new Map();
408
+ for (const w of items || []) { if (!w || w.kind === 'Epic' || !(w.uid || w.key)) continue; const t = taskOf(w); T.set(t.uid, t); }
409
+ const ends = new Map(); for (const r of rows || []) if (r && r.id && r.end) ends.set(r.id, dn(r.end));
410
+ for (const r of rows || []) {
411
+ if (!r || !r.id || r.links !== undefined) continue;
412
+ let v = null;
413
+ try { v = viewOf(F, r.id, { T, today: o.today, limit: (ini) => (ends.has(ini) ? ends.get(ini) : null), src: o.src ? o.src(r.id, T) : {} }); } catch { v = null; }
414
+ if (v) r.links = v;
415
+ }
416
+ return rows;
417
+ }
418
+
419
+ module.exports = {
420
+ LEDGER, TYPES, TYPE_WORDS, ledgerFile, read, readLines, append, fold, cleanLink, linksOf, loops, keyOf, sigOf, taskOf, effOf, needOf, shortOf,
421
+ settle, pullOf, proposalsOf, prdOrders, viewOf, decorate, newBatch, dn, isoN, md, hash,
422
+ };