create-pmos 0.827.0 → 0.829.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.
- package/package.json +217 -45
- package/payload/KIT-VERSION +1 -1
- package/payload/planning/evals/eval-calibration.md +1 -1
- package/payload/planning/okrs/health-budgets.md +1 -1
- package/payload/state/discoveries.md +1 -1
- package/workspace/bin/pmos.js +19 -9
- package/workspace/board-deliveries-test.js +2 -2
- package/workspace/changelog.json +216 -12
- package/workspace/guide/cli.md +62 -0
- package/workspace/guide/home.md +1 -0
- package/workspace/guide/settings-and-switches.md +1 -0
- package/workspace/lib/board-cli.js +5 -1
- package/workspace/lib/board-premium.js +1 -0
- package/workspace/lib/critical-needs.js +162 -0
- package/workspace/lib/deps-cli.js +102 -0
- package/workspace/lib/engine/boards.js +13 -2
- package/workspace/lib/features.json +5 -0
- package/workspace/lib/five-action-cap.js +84 -0
- package/workspace/lib/needs-providers.js +1 -0
- package/workspace/lib/page-one-answer.js +86 -0
- package/workspace/lib/pages-cli.js +6 -2
- package/workspace/lib/people-cli.js +43 -2
- package/workspace/lib/pmos-verbs.js +15 -1
- package/workspace/lib/portfolio-cli.js +64 -0
- package/workspace/lib/portfolio.js +168 -0
- package/workspace/lib/prototype-cli.js +86 -0
- package/workspace/lib/report-cli.js +12 -2
- package/workspace/lib/report-readers.js +5 -3
- package/workspace/lib/review-pack.js +348 -0
- package/workspace/lib/review-reasons.js +357 -0
- package/workspace/lib/route-modules.js +6 -0
- package/workspace/lib/routes-boards.js +25 -2
- package/workspace/lib/routes-portfolio.js +50 -0
- package/workspace/lib/routes-review-pack.js +42 -0
- package/workspace/lib/routes-roadmap-ops.js +53 -1
- package/workspace/lib/routes-sim-users.js +66 -0
- package/workspace/lib/routes-stakeholders.js +42 -0
- package/workspace/lib/routes-strategy-card.js +29 -1
- package/workspace/lib/routes-team-waits.js +24 -0
- package/workspace/lib/routes-use-log.js +6 -1
- package/workspace/lib/sim-users.js +329 -0
- package/workspace/lib/stakeholder-map.js +232 -0
- package/workspace/lib/task-store.js +1 -1
- package/workspace/lib/team-waits.js +167 -0
- package/workspace/lib/tradeoff-cards.js +258 -0
- package/workspace/lib/tradeoff-cli.js +83 -0
- package/workspace/lib/voice-cli.js +7 -0
- package/workspace/package-lock.json +2 -2
- package/workspace/package.json +1 -1
- package/workspace/premium-vw-jira-test.js +6 -2
- package/workspace/public/pm/bd-backlog.js +44 -6
- package/workspace/public/pm/bd-principles.js +12 -1
- package/workspace/public/pm/cozy-library.css +5 -0
- package/workspace/public/pm/cozy-pick.css +1 -0
- package/workspace/public/pm/cozy-pop-work.js +8 -2
- package/workspace/public/pm/cozy-proto.css +5 -0
- package/workspace/public/pm/cozy-proto.js +11 -3
- package/workspace/public/pm/cozy-today-app.js +53 -3
- package/workspace/public/pm/cozy-today-view.js +12 -4
- package/workspace/public/pm/day-time.js +8 -0
- package/workspace/public/pm/five-action-cap.js +104 -0
- package/workspace/public/pm/home-ini.js +3 -2
- package/workspace/public/pm/ini-links.js +2 -1
- package/workspace/public/pm/ini-tasks.js +5 -2
- package/workspace/public/pm/ini-view-model.js +14 -6
- package/workspace/public/pm/ini-view.js +12 -2
- package/workspace/public/pm/library-model.js +1 -1
- package/workspace/public/pm/page-one-answer.css +18 -0
- package/workspace/public/pm/page-one-answer.js +134 -0
- package/workspace/public/pm/pop-work-model.js +8 -6
- package/workspace/public/pm/portfolio.js +67 -0
- package/workspace/public/pm/premium-board.js +19 -6
- package/workspace/public/pm/premium-library.js +27 -3
- package/workspace/public/pm/product-switcher.js +7 -1
- package/workspace/public/pm/range-pick.css +12 -0
- package/workspace/public/pm/range-pick.js +30 -6
- package/workspace/public/pm/review-pack.js +47 -0
- package/workspace/public/pm/review-reasons.js +75 -0
- package/workspace/public/pm/sim-users.js +34 -0
- package/workspace/public/pm/sort-order.js +3 -0
- package/workspace/public/pm/stakeholder-map.js +125 -0
- package/workspace/public/pm/task-card.js +10 -7
- package/workspace/public/pm/team-waits.js +56 -0
- package/workspace/public/pm/tradeoff-cards.css +27 -0
- package/workspace/public/pm/tradeoff-cards.js +183 -0
- package/workspace/public/premium-board.html +1 -0
- package/workspace/public/premium-portfolio.html +48 -0
- package/workspace/public/today.html +1 -0
- package/workspace/run-tests.js +9 -1
- package/workspace/server.js +1 -1
- package/workspace/stage-card-tasks-test.js +11 -5
- package/workspace/test-lists/critical-needs.json +9 -0
- package/workspace/test-lists/five-action-cap.json +9 -0
- package/workspace/test-lists/page-one-answer.json +9 -0
- package/workspace/test-lists/portfolio.json +9 -0
- package/workspace/test-lists/review-pack.json +9 -0
- package/workspace/test-lists/review-reasons.json +9 -0
- package/workspace/test-lists/sim-users.json +9 -0
- package/workspace/test-lists/stakeholder-map.json +9 -0
- package/workspace/test-lists/task-time-optional.json +9 -0
- package/workspace/test-lists/team-waits.json +9 -0
- package/workspace/test-lists/tradeoff-cards.json +9 -0
- package/workspace/test-timings.json +25 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/* review-pack.js — the monthly product review pack (initiative review-pack, job #1910, idea A343; board BatchMoreADS panel 25, agreed 2026-11-25).
|
|
3
|
+
*
|
|
4
|
+
* Before a monthly review with leadership the PM writes the same page from zero. PMOS already keeps each part of it as a record, so it drafts
|
|
5
|
+
* the page and the PM edits it. One pack per product per month. No model call: every line is read from a record, and every line names that
|
|
6
|
+
* record. A part with nothing to cite is left out (no source, no line; zero is silence).
|
|
7
|
+
*
|
|
8
|
+
* gather(root, { today, stateDir }) → { goals, failed, decision, asks } the product's records, each item with `source` { label, ref } or none
|
|
9
|
+
* goals the key results of the OKR files and the loose KR metric records (lib/goal-source.js), with the state of their newest reading
|
|
10
|
+
* (lib/goal-status.js readingOf). Source: the reading's own source, else the key result's line in the OKR file, else the record.
|
|
11
|
+
* failed each refuted outcome check of the last WINDOW_DAYS days (outcome_checks.jsonl, the live checks only), with the newest Lessons
|
|
12
|
+
* line of that experiment (artefacts.jsonl). Source: the check's path:line.
|
|
13
|
+
* decision a key result of the OKR files that is a decision (okr-goals `decision`: go/no-go, decided by) with a date today or later, the
|
|
14
|
+
* nearest first; else the decision journal's next look-again (state open or due), the nearest first. Source: its line.
|
|
15
|
+
* asks the Narrative artefact's `asks` lines of the product (scope portfolio or board:<id>), newest first. Source: the set's own sources.
|
|
16
|
+
* packOf({ product, today, goals, failed, decision, asks }) PURE → { title, month, slot, sections: [{ key, head, lines | bets }], dropped } | null
|
|
17
|
+
* null when no part has a line. `dropped` counts the items left out for having no source.
|
|
18
|
+
* markdown(pack) PURE → the file. readPack(text) PURE → { title, sections } read back from a file (the PM's edits show in Library)
|
|
19
|
+
* write(root, pack, { redraft }) → { ok, path, sha, written } | { ok: false, status, error }
|
|
20
|
+
* <state>/reports/review-pack/<YYYY-MM>-01.md (lib/report-draft.js draftPath, so the Issue press and the report readers know it).
|
|
21
|
+
* A pack that exists is left byte for byte as it is (the PM may have edited it) unless `redraft`; an issued pack is frozen (409).
|
|
22
|
+
* After a write, the report readers are told (onWritten: lib/report-readers.js attach listens here as it does to report drafts).
|
|
23
|
+
* onWritten(fn) → unsubscribe
|
|
24
|
+
* itemsOf({ ws, name, packs }) PURE → Library rows (the lib/library-items.js ITEM shape) in the Reports family, kind 'Review pack', with
|
|
25
|
+
* `pack` { sections, cadence, slot, sha, issued } for the preview and the share (Issue) press. lib/routes-review-pack.js serves them.
|
|
26
|
+
* due(today) PURE → true in the last DUE_DAYS days of the month: the pack is drafted before the review.
|
|
27
|
+
* tick(ctx, now) → [{ ws, ok, path? , why? }] drafts the month's pack of each product that has none, when due. Never shares.
|
|
28
|
+
* autoOn(env) → whether Library's open drafts due packs here (PMOS_REVIEW_PACK; off by default on a scratch server)
|
|
29
|
+
* list(root) → [{ path, slot, title, sections, sha, cadence }] the packs of one product, newest first (Library › Reports)
|
|
30
|
+
* cli(args, { root, now, say }) → exit code `pmos report pack [--redraft] [--json] [--now <time>]`
|
|
31
|
+
* The copy names PMOS only. Sharing is the PM's Issue press (POST /api/report/issue); nothing here issues or sends. */
|
|
32
|
+
const fs = require('fs');
|
|
33
|
+
const path = require('path');
|
|
34
|
+
|
|
35
|
+
const CADENCE = 'review-pack';
|
|
36
|
+
const WINDOW_DAYS = 30;
|
|
37
|
+
const DUE_DAYS = 7;
|
|
38
|
+
const MAX = { goals: 6, failed: 4, asks: 4 };
|
|
39
|
+
const DAY = 86400000;
|
|
40
|
+
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
41
|
+
const MON = MONTHS.map((m) => m.slice(0, 3));
|
|
42
|
+
const DAY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
43
|
+
const one = (v, n = 240) => String(v == null ? '' : v).replace(/\s+/g, ' ').trim().slice(0, n);
|
|
44
|
+
const short = (d) => (DAY_RE.test(String(d || '')) ? (+d.slice(8, 10)) + ' ' + MON[+d.slice(5, 7) - 1] : String(d || ''));
|
|
45
|
+
const firstSentence = (s) => { const t = one(s, 600); const m = /^(.+?[.;])(\s|$)/.exec(t); return one(m ? m[1].replace(/[.;]$/, '') : t, 160); };
|
|
46
|
+
const relOf = (root, f) => path.relative(root, f).split(path.sep).join('/');
|
|
47
|
+
const SEVERITY = { broken: 0, behind: 1, close: 2, none: 3, on: 4 };
|
|
48
|
+
const STATE_WORD = { on: 'on track', behind: 'behind', close: 'near its limit', broken: 'over its limit', none: 'no reading yet' };
|
|
49
|
+
const HEAD = { goals: 'Goals', failed: 'Bets that failed', decision: 'Next big decision', asks: 'Asks' };
|
|
50
|
+
const SAID = 'Drafted by PMOS from goals, experiments and decisions. Edit, then share.';
|
|
51
|
+
|
|
52
|
+
/* ── reading the records ─────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
53
|
+
function linesOf(file) { try { return fs.readFileSync(file, 'utf8').split('\n'); } catch { return null; } }
|
|
54
|
+
function stateDirOf(root) {
|
|
55
|
+
for (const rel of [['pmos', 'state'], ['state'], ['.pmos']]) { const d = path.join(root, ...rel); try { if (fs.statSync(d).isDirectory()) return d; } catch { /* next */ } }
|
|
56
|
+
try { return require('./driver.js').stateDirOf(root); } catch { return null; }
|
|
57
|
+
}
|
|
58
|
+
function okrDirOf(root) {
|
|
59
|
+
for (const d of [path.join(root, 'pmos', 'planning', 'okrs'), path.join(root, 'planning', 'okrs')]) { try { if (fs.statSync(d).isDirectory()) return d; } catch { /* next */ } }
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/** The path:line of the first line of the OKR files that names a key result's token as a heading or a list item. */
|
|
63
|
+
const tokRe = (t) => new RegExp('(^|[^A-Za-z0-9.-])' + String(t).replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '([^A-Za-z0-9-]|$)');
|
|
64
|
+
const listy = (l) => /^\s*(#{1,6}\s|[-*]\s|\|)/.test(l);
|
|
65
|
+
/** The path:line of a key result in the OKR files: the first heading or list line naming its token after its objective's line (KR1 repeats per objective). */
|
|
66
|
+
function okrRef(root, files, token, objective) {
|
|
67
|
+
const dir = okrDirOf(root);
|
|
68
|
+
if (!dir || !token) return null;
|
|
69
|
+
const re = tokRe(token), ro = objective ? tokRe(objective) : null;
|
|
70
|
+
for (const f of files || []) {
|
|
71
|
+
const L = linesOf(path.join(dir, f));
|
|
72
|
+
if (!L) continue;
|
|
73
|
+
const o = ro ? L.findIndex((l) => listy(l) && ro.test(l)) : 0;
|
|
74
|
+
if (o < 0) continue;
|
|
75
|
+
const i = L.findIndex((l, n) => n >= o && listy(l) && re.test(l));
|
|
76
|
+
if (i >= 0) return relOf(root, path.join(dir, f)) + ':' + (i + 1);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
function titleOf(root, id) {
|
|
81
|
+
for (const d of [path.join(root, 'pmos', 'state', 'initiatives'), path.join(root, 'planning', 'initiatives')]) {
|
|
82
|
+
const L = linesOf(path.join(d, id + '.md'));
|
|
83
|
+
if (!L) continue;
|
|
84
|
+
const t = L.slice(0, 40).find((l) => /^title:\s*/.test(l));
|
|
85
|
+
if (t) return one(t.replace(/^title:\s*/, '').replace(/^["']|["']$/g, ''), 120);
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
function goalsOf(root, today) {
|
|
90
|
+
const GS = require('./goal-status.js');
|
|
91
|
+
let src; try { src = require('./goal-source.js').source({ root }); } catch { return []; }
|
|
92
|
+
const files = (src.okr && src.okr.files) || [];
|
|
93
|
+
let recRel = ''; try { recRel = relOf(root, require('./goal-source.js').recordsDirOf(root)) + '/kr-metrics.jsonl'; } catch { recRel = ''; }
|
|
94
|
+
const out = [];
|
|
95
|
+
const add = (title, rec, token, objective) => {
|
|
96
|
+
const r = rec ? GS.readingOf(rec, today) : { dot: 'none', txt: '', source: '' };
|
|
97
|
+
let ref = r.source ? one(r.source, 200) : '';
|
|
98
|
+
let label = ref ? 'reading' : '';
|
|
99
|
+
if (!ref && token) { ref = okrRef(root, files, token, objective) || ''; label = ref ? 'OKR file' : ''; }
|
|
100
|
+
if (!ref && rec && rec.rid && recRel) { ref = recRel + '#' + rec.rid; label = 'goal record'; }
|
|
101
|
+
const state = STATE_WORD[r.dot] || 'no reading yet';
|
|
102
|
+
const text = r.dot === 'none' ? `${title}: no reading yet` : `${title}: ${r.txt}${r.txt ? ' · ' : ''}${state}${r.stale && r.ageDays != null ? `, reading ${r.ageDays} days old` : ''}`;
|
|
103
|
+
out.push({ title, dot: r.dot, text: one(text, 200), source: ref ? { label, ref } : null });
|
|
104
|
+
};
|
|
105
|
+
for (const g of src.goals || []) if (!(g.k && g.k.decision && !g.record)) add(one(g.title, 110), g.record, g.token, g.o && g.o.token); // a decision key result is the Next big decision, not a goal
|
|
106
|
+
for (const rec of src.loose || []) add(one(rec.title || rec.rid, 110), rec, null);
|
|
107
|
+
return out.sort((a, b) => SEVERITY[a.dot] - SEVERITY[b.dot]);
|
|
108
|
+
}
|
|
109
|
+
function failedOf(root, today, stateDir) {
|
|
110
|
+
const O = require('./outcomes.js');
|
|
111
|
+
const files = [...new Set([stateDir && path.join(stateDir, 'outcome_checks.jsonl'), path.join(root, 'pmos', 'state', 'outcome_checks.jsonl'), path.join(root, 'planning', 'runs', 'outcome_checks.jsonl')].filter(Boolean))];
|
|
112
|
+
const rows = [], at = new Map();
|
|
113
|
+
for (const f of files) (linesOf(f) || []).forEach((l, i) => { let o = null; try { o = JSON.parse(l); } catch { return; } if (o && o.initiative_id) { rows.push(o); at.set(o, relOf(root, f) + ':' + (i + 1)); } });
|
|
114
|
+
const live = new Set(O.liveChecks(rows));
|
|
115
|
+
const from = Date.parse(today + 'T00:00:00Z') - WINDOW_DAYS * DAY, to = Date.parse(today + 'T23:59:59Z');
|
|
116
|
+
let lessons = new Map();
|
|
117
|
+
try {
|
|
118
|
+
const A = require('./artefacts.js');
|
|
119
|
+
const F = A.fold(A.readLines(A.file(stateDir)));
|
|
120
|
+
lessons = new Map([...F].map(([scope, m]) => [scope, ((m.get('lessons') || {}).sets || []).filter((s) => s.text)]));
|
|
121
|
+
} catch { lessons = new Map(); }
|
|
122
|
+
const out = [];
|
|
123
|
+
for (const r of rows) {
|
|
124
|
+
if (!live.has(r) || r.disposition !== 'refuted') continue;
|
|
125
|
+
const t = Date.parse(r.checked_at || r.at || r.date || '');
|
|
126
|
+
if (!(t >= from && t <= to)) continue;
|
|
127
|
+
const id = String(r.initiative_id);
|
|
128
|
+
const ls = lessons.get('experiment:' + id) || lessons.get('initiative:' + id) || [];
|
|
129
|
+
const lesson = ls.length ? one(ls[ls.length - 1].text, 160) : '';
|
|
130
|
+
const found = firstSentence(r.evidence);
|
|
131
|
+
const text = (found || 'The outcome check refuted it') + (lesson ? `; learned ${lesson.replace(/^learned\s+/i, '')}` : '');
|
|
132
|
+
out.push({ id, title: titleOf(root, id), at: t, text: one(text, 280), source: { label: 'outcome check', ref: at.get(r) } });
|
|
133
|
+
}
|
|
134
|
+
return out.sort((a, b) => b.at - a.at);
|
|
135
|
+
}
|
|
136
|
+
function decisionOf(root, today) {
|
|
137
|
+
try {
|
|
138
|
+
const OG = require('./okr-goals.js');
|
|
139
|
+
const okr = OG.read(root);
|
|
140
|
+
const ks = [];
|
|
141
|
+
for (const o of (okr.ok && okr.objectives) || []) for (const k of o.krs || []) if (k.decision && DAY_RE.test(String(k.due || '')) && k.due >= today) ks.push(Object.assign({ otoken: o.token }, k));
|
|
142
|
+
ks.sort((a, b) => a.due.localeCompare(b.due));
|
|
143
|
+
for (const k of ks) {
|
|
144
|
+
const ref = okrRef(root, okr.files, k.token, k.otoken);
|
|
145
|
+
if (ref) return { text: one(`${k.title}, by ${short(k.due)}`, 200), source: { label: 'OKR file', ref } };
|
|
146
|
+
}
|
|
147
|
+
} catch { /* no OKR file */ }
|
|
148
|
+
try {
|
|
149
|
+
const DJ = require('./decision-journal.js');
|
|
150
|
+
const J = DJ.read(root);
|
|
151
|
+
const es = DJ.fold(J.rows, { today }).filter((e) => (e.state === 'open' || e.state === 'due') && e.decided && DAY_RE.test(String(e.check || ''))).sort((a, b) => a.check.localeCompare(b.check));
|
|
152
|
+
for (const e of es) {
|
|
153
|
+
for (const f of (DJ.filesOf(root).all || [])) {
|
|
154
|
+
const L = linesOf(f) || [];
|
|
155
|
+
const i = L.findIndex((l) => l.includes(`"id":"${e.id}"`) && l.includes('"decision"'));
|
|
156
|
+
if (i >= 0) return { text: one(`Look again at "${one(e.decided, 120)}", by ${short(e.check)}`, 200), source: { label: 'decision journal', ref: relOf(root, f) + ':' + (i + 1) } };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch { /* no journal */ }
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
function asksOf(stateDir) {
|
|
163
|
+
try {
|
|
164
|
+
const A = require('./artefacts.js');
|
|
165
|
+
const F = A.fold(A.readLines(A.file(stateDir)));
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const [scope, m] of F) {
|
|
168
|
+
if (scope !== 'portfolio' && !/^board:/.test(scope)) continue;
|
|
169
|
+
for (const s of ((m.get('narrative') || {}).sets || [])) {
|
|
170
|
+
if (s.field !== 'asks' || !one(s.text)) continue;
|
|
171
|
+
const src = (Array.isArray(s.sources) ? s.sources : []).find((x) => x && x.ref);
|
|
172
|
+
out.push({ at: String(s.at || ''), text: one(s.text, 200), source: src ? { label: one(src.label || 'source', 40), ref: one(src.ref, 200) } : null });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return out.sort((a, b) => b.at.localeCompare(a.at));
|
|
176
|
+
} catch { return []; }
|
|
177
|
+
}
|
|
178
|
+
function gather(root, { today, stateDir } = {}) {
|
|
179
|
+
const abs = path.resolve(String(root));
|
|
180
|
+
const dir = stateDir || stateDirOf(abs);
|
|
181
|
+
const t = DAY_RE.test(String(today || '')) ? today : new Date().toISOString().slice(0, 10);
|
|
182
|
+
return { goals: goalsOf(abs, t), failed: dir || abs ? failedOf(abs, t, dir) : [], decision: decisionOf(abs, t), asks: dir ? asksOf(dir) : [] };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* ── the pack ─────────────────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
186
|
+
const cited = (x) => !!(x && one(x.text) && x.source && one(x.source.ref));
|
|
187
|
+
function packOf({ product, today, goals = [], failed = [], decision = null, asks = [] } = {}) {
|
|
188
|
+
if (!DAY_RE.test(String(today || ''))) return null;
|
|
189
|
+
let dropped = 0;
|
|
190
|
+
const keep = (xs, n) => { const all = (xs || []).filter((x) => x && one(x.text)); const ok = all.filter(cited); dropped += all.length - ok.length; return ok.slice(0, n); };
|
|
191
|
+
const sections = [];
|
|
192
|
+
const g = keep(goals, MAX.goals);
|
|
193
|
+
if (g.length) sections.push({ key: 'goals', head: HEAD.goals, lines: g.map((x) => ({ text: one(x.text, 200), source: x.source })) });
|
|
194
|
+
const f = keep(failed, MAX.failed);
|
|
195
|
+
if (f.length) sections.push({ key: 'failed', head: HEAD.failed, bets: f.map((x) => ({ id: x.id, title: one(x.title || x.id, 120), lines: [{ text: one(x.text, 280), source: x.source }] })) });
|
|
196
|
+
const d = keep(decision ? [decision] : [], 1);
|
|
197
|
+
if (d.length) sections.push({ key: 'decision', head: HEAD.decision, lines: [{ text: one(d[0].text, 200), source: d[0].source }] });
|
|
198
|
+
const a = keep(asks, MAX.asks);
|
|
199
|
+
if (a.length) sections.push({ key: 'asks', head: HEAD.asks, lines: a.map((x) => ({ text: one(x.text, 200), source: x.source })) });
|
|
200
|
+
if (!sections.length) return null;
|
|
201
|
+
const month = MONTHS[+today.slice(5, 7) - 1];
|
|
202
|
+
const name = one(product || 'Product', 80);
|
|
203
|
+
return { title: `${name} · ${month} review`, product: name, month, slot: today.slice(0, 7) + '-01', today, sections, dropped };
|
|
204
|
+
}
|
|
205
|
+
const srcText = (s) => (s && s.ref ? ` — source: \`${String(s.ref).replace(/`/g, "'")}\`` : '');
|
|
206
|
+
function markdown(pack) {
|
|
207
|
+
const out = ['---', 'type: Reference', `title: ${JSON.stringify(pack.title)}`, `timestamp: ${pack.today}`, 'kind: review-pack', 'status: draft', '---', '',
|
|
208
|
+
`# ${pack.title}`, '', `✦ ${SAID}`, ''];
|
|
209
|
+
for (const s of pack.sections) {
|
|
210
|
+
out.push(`## ${s.head}`, '');
|
|
211
|
+
if (s.bets) for (const b of s.bets) { out.push(`### ${b.title} (${b.id})`, ''); for (const l of b.lines) out.push(`- ${l.text}${srcText(l.source)}`); out.push(''); }
|
|
212
|
+
else { for (const l of s.lines) out.push(`- ${l.text}${srcText(l.source)}`); out.push(''); }
|
|
213
|
+
}
|
|
214
|
+
return out.join('\n');
|
|
215
|
+
}
|
|
216
|
+
const KEY_OF = Object.fromEntries(Object.entries(HEAD).map(([k, v]) => [v.toLowerCase(), k]));
|
|
217
|
+
function readPack(text) {
|
|
218
|
+
const body = String(text || '').replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '');
|
|
219
|
+
const L = body.split(/\r?\n/);
|
|
220
|
+
const t = L.find((l) => /^#\s/.test(l));
|
|
221
|
+
const sections = [];
|
|
222
|
+
let cur = null, bet = null;
|
|
223
|
+
for (const l of L) {
|
|
224
|
+
const h = /^##\s+(.+?)\s*$/.exec(l);
|
|
225
|
+
if (h) { cur = { key: KEY_OF[h[1].toLowerCase()] || 'other', head: one(h[1], 80), lines: [] }; sections.push(cur); bet = null; continue; }
|
|
226
|
+
const b = cur && /^###\s+(.+?)(?:\s+\(([A-Za-z0-9._-]{1,80})\))?\s*$/.exec(l);
|
|
227
|
+
if (b) { bet = b[2] || ''; cur.lines.push({ text: one(b[1], 160), head: true, id: bet }); continue; }
|
|
228
|
+
const li = cur && /^\s*[-*]\s+(.+)$/.exec(l);
|
|
229
|
+
if (li) {
|
|
230
|
+
const m = /^(.*?)\s+—\s+source:\s+`([^`]+)`\s*$/.exec(li[1]);
|
|
231
|
+
cur.lines.push(m ? { text: one(m[1], 300), source: one(m[2], 200) } : { text: one(li[1], 300), source: '' });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return { title: t ? one(t.replace(/^#\s+/, ''), 160) : '', sections: sections.filter((s) => s.lines.length) };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* ── writing ── */
|
|
238
|
+
const heard = new Set(); // the report readers (lib/report-readers.js attach) hear each pack this process writes, as they hear a cadence draft
|
|
239
|
+
/** Call fn({ root, cadence, slot, path }) after each pack this process writes. → an unsubscribe function */
|
|
240
|
+
function onWritten(fn) { if (typeof fn === 'function') heard.add(fn); return () => heard.delete(fn); }
|
|
241
|
+
/* ──────────────────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
242
|
+
function write(root, pack, { redraft = false } = {}) {
|
|
243
|
+
if (!pack || !pack.slot) return { ok: false, status: 400, error: 'there is no pack to write: no part has a line with a source' };
|
|
244
|
+
const RD = require('./report-draft.js'), IS = require('./issues.js');
|
|
245
|
+
const abs = path.resolve(String(root));
|
|
246
|
+
const rel = RD.draftPath(abs, CADENCE, pack.slot);
|
|
247
|
+
const file = path.join(abs, rel);
|
|
248
|
+
if (IS.isIssued(IS.readIssues(abs), CADENCE, pack.slot)) return { ok: false, status: 409, error: `the ${pack.month} pack is shared (issued) and frozen; nothing was written` };
|
|
249
|
+
let st = null; try { st = fs.lstatSync(file); } catch { st = null; }
|
|
250
|
+
if (st && !st.isFile()) return { ok: false, status: 403, error: `${rel} is not a regular file; nothing was written` };
|
|
251
|
+
if (st && !redraft) return { ok: true, path: rel, written: false, why: 'the pack exists; it is yours to edit (add --redraft to write it again)' };
|
|
252
|
+
const md = markdown(pack);
|
|
253
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
|
|
254
|
+
try { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(tmp, md, { flag: 'wx' }); fs.renameSync(tmp, file); } catch (e) {
|
|
255
|
+
try { fs.unlinkSync(tmp); } catch { /* not made */ }
|
|
256
|
+
return { ok: false, status: 500, error: 'could not write the pack: ' + one(e && e.message, 160) };
|
|
257
|
+
}
|
|
258
|
+
console.info(`[review-pack] wrote ${rel} sections=${pack.sections.length} dropped=${pack.dropped}`);
|
|
259
|
+
for (const fn of heard) { try { fn({ root: abs, cadence: CADENCE, slot: pack.slot, path: rel }); } catch { /* a listener never fails the write */ } }
|
|
260
|
+
return { ok: true, path: rel, written: true, sha: IS.blobSha(Buffer.from(md, 'utf8')) };
|
|
261
|
+
}
|
|
262
|
+
function due(today) {
|
|
263
|
+
if (!DAY_RE.test(String(today || ''))) return false;
|
|
264
|
+
const y = +today.slice(0, 4), m = +today.slice(5, 7), d = +today.slice(8, 10);
|
|
265
|
+
const last = new Date(Date.UTC(y, m, 0)).getUTCDate();
|
|
266
|
+
return d > last - DUE_DAYS;
|
|
267
|
+
}
|
|
268
|
+
/** Does Library draft due packs by itself here? PMOS_REVIEW_PACK=0 turns it off, =1 on; by default a scratch server (its own PMOS_STATE_DIR,
|
|
269
|
+
* which every suite sets) writes no pack into the repositories it hosts (the lib/profile-default.js rule). */
|
|
270
|
+
function autoOn(env = process.env) {
|
|
271
|
+
const v = String((env && env.PMOS_REVIEW_PACK) || '').trim();
|
|
272
|
+
if (v === '0') return false;
|
|
273
|
+
if (v === '1') return true;
|
|
274
|
+
return !String((env && env.PMOS_STATE_DIR) || '').trim();
|
|
275
|
+
}
|
|
276
|
+
function itemsOf({ ws, name, packs } = {}) {
|
|
277
|
+
const about = { ws: ws || null, product: one(name, 80), ini: null, title: '', program: null, programTitle: '' };
|
|
278
|
+
return (packs || []).filter((k) => k && k.path).map((k) => ({
|
|
279
|
+
id: 'pack:' + ws + ':' + k.path, kind: 'doc', name: k.title || 'Review pack', sub: '', kindText: 'Review pack', type: 'Review pack', family: 'reports',
|
|
280
|
+
state: k.issued ? 'Issued' : 'Draft', v: null, gate: false, came: '', by: 'PMOS', ed: null, about: { ...about }, product: about.product,
|
|
281
|
+
scope: ws ? ['p:' + ws] : [], updated: k.updated || k.slot || null, held: null, path: k.path, open: { doc: { ws, path: k.path } },
|
|
282
|
+
pack: { sections: k.sections || [], cadence: k.cadence, slot: k.slot, sha: k.sha, issued: !!k.issued },
|
|
283
|
+
}));
|
|
284
|
+
}
|
|
285
|
+
function nameOf(w) { return one((w && (w.label || w.name || w.id)) || 'Product', 80); }
|
|
286
|
+
function draftFor(root, { product, today, stateDir, redraft = false } = {}) {
|
|
287
|
+
if (!redraft) {
|
|
288
|
+
// a pack that exists is the PM's to edit: say so before reading any record
|
|
289
|
+
try { const rel = require('./report-draft.js').draftPath(path.resolve(String(root)), CADENCE, String(today).slice(0, 7) + '-01'); if (fs.existsSync(path.join(path.resolve(String(root)), rel))) return { ok: true, path: rel, written: false, why: 'the pack exists; it is yours to edit (add --redraft to write it again)' }; } catch { /* write() says */ }
|
|
290
|
+
}
|
|
291
|
+
const pack = packOf({ product, today, ...gather(root, { today, stateDir }) });
|
|
292
|
+
if (!pack) return { ok: true, written: false, why: 'nothing to cite: no goal, failed bet, decision or ask has a record' };
|
|
293
|
+
return Object.assign(write(root, pack, { redraft }), { pack });
|
|
294
|
+
}
|
|
295
|
+
const tried = new Map(); // ws → the day it was last tried: one read a day per product, not one a minute
|
|
296
|
+
async function tick(ctx, now = new Date()) {
|
|
297
|
+
const today = new Date(now).toISOString().slice(0, 10);
|
|
298
|
+
if (!ctx || !due(today)) return [];
|
|
299
|
+
const out = [];
|
|
300
|
+
const list = typeof ctx.workspaceList === 'function' ? ctx.workspaceList() : [];
|
|
301
|
+
for (const w of list) {
|
|
302
|
+
if (!w || !w.root || tried.get(w.id || w.root) === today) continue;
|
|
303
|
+
tried.set(w.id || w.root, today);
|
|
304
|
+
try {
|
|
305
|
+
let dir = null; try { dir = ctx.stateDirFor ? ctx.stateDirFor(w) : null; } catch { dir = null; }
|
|
306
|
+
const r = draftFor(w.root, { product: nameOf(w), today, stateDir: dir || undefined });
|
|
307
|
+
out.push({ ws: w.id, ok: r.ok, path: r.path, written: !!r.written, why: r.why || r.error });
|
|
308
|
+
} catch (e) { out.push({ ws: w.id, ok: false, why: one(e && e.message, 160) }); }
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
function list(root) {
|
|
313
|
+
const RD = require('./report-draft.js'), IS = require('./issues.js');
|
|
314
|
+
const abs = path.resolve(String(root));
|
|
315
|
+
let dirRel; try { dirRel = path.posix.dirname(RD.draftPath(abs, CADENCE, '2000-01-01')); } catch { return []; }
|
|
316
|
+
let names = []; try { names = fs.readdirSync(path.join(abs, dirRel)).filter((n) => /^\d{4}-\d{2}-01\.md$/.test(n)); } catch { return []; }
|
|
317
|
+
let iss = null; try { iss = IS.readIssues(abs); } catch { iss = null; }
|
|
318
|
+
const out = [];
|
|
319
|
+
for (const n of names.sort().reverse()) {
|
|
320
|
+
let text = ''; try { text = fs.readFileSync(path.join(abs, dirRel, n), 'utf8'); } catch { continue; }
|
|
321
|
+
const slot = n.slice(0, 10), P = readPack(text);
|
|
322
|
+
let at = ''; try { at = fs.statSync(path.join(abs, dirRel, n)).mtime.toISOString(); } catch { at = ''; }
|
|
323
|
+
out.push({ path: dirRel + '/' + n, cadence: CADENCE, slot, title: P.title || slot, sections: P.sections, sha: IS.blobSha(Buffer.from(text, 'utf8')), issued: !!(iss && IS.isIssued(iss, CADENCE, slot)), updated: at });
|
|
324
|
+
}
|
|
325
|
+
return out;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/* ── pmos report pack ─────────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
329
|
+
function cli(args, { root, now = Date.now(), say = (s) => process.stdout.write(s + '\n'), product } = {}) {
|
|
330
|
+
const has = (f) => args.includes('--' + f);
|
|
331
|
+
const val = (f) => { const i = args.indexOf('--' + f); return i >= 0 ? args[i + 1] : undefined; };
|
|
332
|
+
const t = val('now') ? Date.parse(val('now')) : now;
|
|
333
|
+
if (!Number.isFinite(t)) { say('--now needs a time, for example 2026-11-25T09:00:00Z'); return 2; }
|
|
334
|
+
const today = new Date(t).toISOString().slice(0, 10);
|
|
335
|
+
const abs = path.resolve(String(root || process.cwd()));
|
|
336
|
+
const r = draftFor(abs, { product: product || val('product') || path.basename(abs), today, redraft: has('redraft') });
|
|
337
|
+
if (has('json')) { say(JSON.stringify({ ok: r.ok, path: r.path || null, written: !!r.written, why: r.why || r.error || null, pack: r.pack || null })); return r.ok ? 0 : 1; }
|
|
338
|
+
if (!r.ok) { say(r.error); return 1; }
|
|
339
|
+
if (!r.pack) { say(r.why); return 0; }
|
|
340
|
+
say(r.written ? `Wrote ${r.path}. Edit it in Library › Reports, then share it.` : `${r.path}: ${r.why}.`);
|
|
341
|
+
if (r.pack.dropped) say(`${r.pack.dropped} ${r.pack.dropped === 1 ? 'line was' : 'lines were'} left out: no record to cite.`);
|
|
342
|
+
return 0;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const resetTick = () => tried.clear();
|
|
346
|
+
module.exports = { resetTick, onWritten, itemsOf, autoOn, CADENCE, WINDOW_DAYS, DUE_DAYS, MAX, HEAD, SAID, gather, packOf, markdown, readPack, write, due, draftFor, tick, list, cli, stateDirOf };
|
|
347
|
+
|
|
348
|
+
if (require.main === module && process.argv[2] === 'count') console.log(list(process.argv[3] || process.cwd()).length);
|