peaks-cli 1.2.7 → 1.2.8

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 (30) hide show
  1. package/bin/peaks.js +0 -0
  2. package/dist/src/cli/commands/core-artifact-commands.js +36 -1
  3. package/dist/src/cli/commands/perf-commands.d.ts +3 -0
  4. package/dist/src/cli/commands/perf-commands.js +41 -0
  5. package/dist/src/cli/commands/progress-close-kill.d.ts +51 -0
  6. package/dist/src/cli/commands/progress-close-kill.js +152 -0
  7. package/dist/src/cli/commands/progress-commands.d.ts +3 -0
  8. package/dist/src/cli/commands/progress-commands.js +348 -0
  9. package/dist/src/cli/commands/progress-start-spawn.d.ts +59 -0
  10. package/dist/src/cli/commands/progress-start-spawn.js +114 -0
  11. package/dist/src/cli/commands/progress-watch-render.d.ts +80 -0
  12. package/dist/src/cli/commands/progress-watch-render.js +308 -0
  13. package/dist/src/cli/program.js +4 -0
  14. package/dist/src/services/config/config-types.d.ts +20 -0
  15. package/dist/src/services/config/config-types.js +5 -1
  16. package/dist/src/services/perf/perf-baseline-service.d.ts +70 -0
  17. package/dist/src/services/perf/perf-baseline-service.js +213 -0
  18. package/dist/src/services/progress/progress-service.d.ts +179 -0
  19. package/dist/src/services/progress/progress-service.js +276 -0
  20. package/dist/src/services/session/index.d.ts +1 -1
  21. package/dist/src/services/session/index.js +1 -1
  22. package/dist/src/services/session/session-manager.d.ts +53 -8
  23. package/dist/src/services/session/session-manager.js +150 -3
  24. package/dist/src/services/skills/skill-presence-service.d.ts +27 -1
  25. package/dist/src/services/skills/skill-presence-service.js +112 -9
  26. package/dist/src/shared/version.d.ts +1 -1
  27. package/dist/src/shared/version.js +1 -1
  28. package/package.json +6 -2
  29. package/skills/peaks-qa/SKILL.md +13 -0
  30. package/skills/peaks-rd/SKILL.md +76 -0
@@ -0,0 +1,308 @@
1
+ /**
2
+ * In-place progress renderer for `peaks progress watch`.
3
+ *
4
+ * Goals (in order of importance):
5
+ * 1. **In-place overwrite.** Every tick the dynamic rows
6
+ * (status line + progress bar) get rewritten, NOT
7
+ * appended. We bypass `io.stdout` (which is
8
+ * `console.log` and adds a trailing `\n` per call) and
9
+ * write directly to `process.stdout`. The cursor-up
10
+ * and erase-line escapes are the same ones terminal-kit
11
+ * emits; we do not need a terminal-kit dependency for
12
+ * them.
13
+ * 2. **Clear PEAKS-CLI branding.** A static 3-line header
14
+ * with the brand bar, the project root, and the
15
+ * progress-file path is painted once. The user always
16
+ * sees what they are looking at, even after the
17
+ * watch loop has overwritten the dynamic rows a
18
+ * thousand times.
19
+ * 3. **Graceful degrade.** When stdout is not a TTY
20
+ * (CI / pipe / `--json`), we fall back to a single
21
+ * static snapshot per tick (no cursor moves, no
22
+ * SGR colour) and a single newline. This keeps the
23
+ * tool scriptable without dropping into a wall of
24
+ * escape codes.
25
+ *
26
+ * Token-cost note: this module is rendered to the user's
27
+ * terminal, never into LLM context. The watch side has
28
+ * zero token cost.
29
+ */
30
+ import chalk from 'chalk';
31
+ // ─────────────────────────────────────────────────────────────────────
32
+ // Raw byte writes. We do NOT go through `io.stdout` because the
33
+ // default `io.stdout` is `console.log` and appends a trailing
34
+ // newline, which would defeat in-place overwrite.
35
+ // ─────────────────────────────────────────────────────────────────────
36
+ function rawWrite(text) {
37
+ process.stdout.write(text);
38
+ }
39
+ /**
40
+ * Number of lines that the dynamic dashboard occupies. We
41
+ * rewrite exactly this many rows per tick via cursor-up +
42
+ * erase-line, so the static header above and the
43
+ * `press Ctrl-C` footer below stay put.
44
+ */
45
+ const DYNAMIC_LINES = 2;
46
+ /** ANSI: cursor up N rows. */
47
+ const CURSOR_UP_N = (n) => `\x1b[${n}A`;
48
+ /** ANSI: erase the current row from cursor to end-of-line. */
49
+ const ERASE_LINE = '\x1b[2K';
50
+ /** ANSI: reset all SGR attributes. */
51
+ const RESET = '\x1b[0m';
52
+ const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
53
+ const PHASE_LABEL = {
54
+ starting: 'starting',
55
+ running: 'running',
56
+ verifying: 'verifying',
57
+ completing: 'completing',
58
+ finished: 'finished',
59
+ failed: 'failed',
60
+ idle: 'idle'
61
+ };
62
+ const PHASE_COLOR = {
63
+ starting: chalk.cyan,
64
+ running: chalk.cyan,
65
+ verifying: chalk.cyan,
66
+ completing: chalk.cyan,
67
+ finished: chalk.green,
68
+ failed: chalk.red,
69
+ idle: chalk.gray
70
+ };
71
+ function pickSpinnerFrame(tick) {
72
+ return SPINNER_FRAMES[Math.abs(tick) % SPINNER_FRAMES.length];
73
+ }
74
+ function formatElapsed(ms) {
75
+ const totalSeconds = Math.max(0, Math.floor(ms / 1000));
76
+ const hours = Math.floor(totalSeconds / 3600);
77
+ const minutes = Math.floor((totalSeconds % 3600) / 60);
78
+ const seconds = totalSeconds % 60;
79
+ if (hours > 0) {
80
+ return `${hours}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
81
+ }
82
+ return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
83
+ }
84
+ /**
85
+ * `elapsedMs` based fake progress, 0..1, capped at 1 after
86
+ * 10 minutes. Visual cue that the watch is alive; NOT a real
87
+ * percent-complete.
88
+ */
89
+ function computeFakeProgress(data) {
90
+ if (data === null)
91
+ return 0;
92
+ const startedAtMs = new Date(data.current.startedAt).getTime();
93
+ if (Number.isNaN(startedAtMs))
94
+ return 0;
95
+ const elapsedMs = Math.max(0, Date.now() - startedAtMs);
96
+ const upperBoundMs = 10 * 60 * 1000;
97
+ return Math.min(1, elapsedMs / upperBoundMs);
98
+ }
99
+ // ─────────────────────────────────────────────────────────────────────
100
+ // ASCII art for the PEAKS-CLI brand bar. Kept in code (not a
101
+ // dependency) so the user sees the brand even when terminal-kit
102
+ // is unavailable.
103
+ // ─────────────────────────────────────────────────────────────────────
104
+ /**
105
+ * Three-row ASCII wordmark for PEAKS-CLI. Each letter is 5
106
+ * columns wide; P-E-A-K-S take 5 letters (25 cols), the
107
+ * dash takes 9 cols (with surrounding space), C-L-I take 3
108
+ * letters (15 cols). Total ≈ 49 cols, fits in 80-col
109
+ * terminals without wrapping.
110
+ *
111
+ * The block-character density (full blocks on the
112
+ * prominent rows) makes the brand visually dominant
113
+ * against the gray "sub-agent progress watch" tagline
114
+ * that sits below — so the user always knows whose
115
+ * dashboard they are looking at, even when the title
116
+ * bar is hidden.
117
+ */
118
+ const PEAKS_CLI_ASCII = [
119
+ '█████ █████ █████ ██ ██ ██████ █████ ██ ███',
120
+ '█ █ █ █ █ █ ██ ██ ██ ██ ██ ██ ',
121
+ '█████ █████ █████ ██████ ██ ████ █████ ███'
122
+ ];
123
+ /**
124
+ * Render the static header: the three-line big PEAKS-CLI
125
+ * wordmark, a separator, a small "sub-agent progress watch"
126
+ * tagline, and the project / file path. Painted ONCE at the
127
+ * top of the watch, then never touched again.
128
+ *
129
+ * Visual hierarchy (per user feedback):
130
+ * 1. PEAKS-CLI — 3-line big block art, bold cyan
131
+ * 2. separator
132
+ * 3. sub-agent progress watch — 1 line, smaller, gray
133
+ * 4. project / path — 1 line each, gray
134
+ */
135
+ function renderHeader(projectRoot, progressFilePath, isTty) {
136
+ if (!isTty) {
137
+ // Non-TTY: emit a single header line, no colour. The
138
+ // dashboard still emits 2 dynamic lines per tick, so
139
+ // consumers that need to parse the output can rely on
140
+ // a known line count.
141
+ return [
142
+ `PEAKS-CLI · sub-agent progress watch · project=${projectRoot}`,
143
+ `path: ${progressFilePath}`
144
+ ].join('\n') + '\n';
145
+ }
146
+ // PEAKS-CLI block-art rows in bold cyan (the "big" brand).
147
+ const brandLines = PEAKS_CLI_ASCII.map((row) => chalk.bold.cyan(` ${row}`));
148
+ // The tagline below the brand — small, gray, no big
149
+ // chrome. The user sees the brand first, the
150
+ // descriptor second.
151
+ const tagline = chalk.gray(' sub-agent progress watch');
152
+ const projLine = chalk.gray(` project: ${projectRoot}`);
153
+ const pathLine = chalk.gray(` path: ${progressFilePath}`);
154
+ const separator = chalk.gray(' ' + '─'.repeat(60));
155
+ return [
156
+ brandLines[0] ?? '',
157
+ brandLines[1] ?? '',
158
+ brandLines[2] ?? '',
159
+ separator,
160
+ tagline,
161
+ projLine,
162
+ pathLine,
163
+ ''
164
+ ].join('\n');
165
+ }
166
+ /**
167
+ * Render the 2-line dynamic dashboard. Status row on top
168
+ * (spinner + phase + elapsed + step), progress bar on the
169
+ * bottom. When `data` is null (no progress file yet), we
170
+ * still show a live spinner + a "waiting…" message so the
171
+ * user knows the watch is alive.
172
+ */
173
+ function renderDynamicRows(data, tick, width, isTty) {
174
+ const progressFraction = computeFakeProgress(data);
175
+ if (data === null) {
176
+ return {
177
+ status: isTty
178
+ ? ` ${chalk.gray(pickSpinnerFrame(tick))} ${chalk.gray('idle')} ${chalk.gray('(no progress file yet — sub-agent has not started)')}`
179
+ : `idle (no progress file yet)`,
180
+ bar: isTty ? renderBar(0, width, isTty) : ''
181
+ };
182
+ }
183
+ const startedAtMs = new Date(data.current.startedAt).getTime();
184
+ const elapsedMs = Number.isNaN(startedAtMs) ? 0 : Math.max(0, Date.now() - startedAtMs);
185
+ const phase = PHASE_LABEL[data.current.phase];
186
+ const step = data.current.step.length > 60 ? data.current.step.slice(0, 57) + '...' : data.current.step;
187
+ const verdict = data.current.verdict ? ` verdict=${data.current.verdict}` : '';
188
+ const role = data.role ? ` role=${data.role}` : '';
189
+ const spinner = pickSpinnerFrame(tick);
190
+ if (!isTty) {
191
+ return {
192
+ status: `${spinner} ${phase} ${formatElapsed(elapsedMs)} ${step}${role}${verdict}`,
193
+ bar: ''
194
+ };
195
+ }
196
+ const colorize = PHASE_COLOR[data.current.phase] ?? chalk.cyan;
197
+ const spinnerColor = phase === 'failed' ? chalk.red
198
+ : phase === 'finished' ? chalk.green
199
+ : chalk.cyan;
200
+ const statusLine = ` ${spinnerColor(spinner)} ${colorize(phase.padEnd(11))} ${chalk.yellow(formatElapsed(elapsedMs))} ${step}${chalk.gray(role)}${verdict ? chalk.gray(verdict) : ''}`;
201
+ return {
202
+ status: statusLine,
203
+ bar: renderBar(progressFraction, width, isTty)
204
+ };
205
+ }
206
+ function renderBar(fraction, width, isTty) {
207
+ if (!isTty)
208
+ return '';
209
+ // 8ths-of-cell block characters: ░ (empty) U+2591, full blocks
210
+ // U+2588..U+258F for the partial last cell.
211
+ const barCells = Math.max(10, Math.min(50, Math.floor(width * 0.4)));
212
+ const filled = Math.round(barCells * Math.max(0, Math.min(1, fraction)) * 8);
213
+ const fullBlocks = Math.floor(filled / 8);
214
+ const fracBlock = filled % 8;
215
+ const emptyCells = barCells * 8 - filled;
216
+ const fullStr = '█'.repeat(fullBlocks);
217
+ const fracStr = fracBlock > 0 ? String.fromCharCode(0x2588 + (8 - fracBlock)) : '';
218
+ const emptyStr = '░'.repeat(Math.floor(emptyCells / 8));
219
+ const percent = String(Math.round(fraction * 100)).padStart(3, ' ');
220
+ return ` ${chalk.green(fullStr + fracStr + emptyStr)} ${chalk.gray(`${percent}%`)}`;
221
+ }
222
+ export class WatchRenderer {
223
+ projectRoot;
224
+ progressFilePath;
225
+ width;
226
+ isTty;
227
+ hasRenderedDynamic = false;
228
+ constructor(options) {
229
+ this.projectRoot = options.projectRoot;
230
+ this.progressFilePath = options.progressFilePath;
231
+ this.width = (process.stdout.columns ?? 120) - 1;
232
+ this.isTty = process.stdout.isTTY === true;
233
+ }
234
+ /**
235
+ * Paint the static header once at the top of the watch
236
+ * (the PEAKS-CLI wordmark, separator, project, path). Then
237
+ * paint the dynamic rows for the first tick. From here on
238
+ * the dynamic rows are the only thing we touch.
239
+ */
240
+ start() {
241
+ rawWrite(renderHeader(this.projectRoot, this.progressFilePath, this.isTty));
242
+ this.paintDynamicOnce(null, 0);
243
+ }
244
+ /**
245
+ * Repaint the 2 dynamic rows in place. First call moves
246
+ * the cursor up N rows from the bottom of the previously
247
+ * painted block; subsequent calls do the same.
248
+ */
249
+ tick(data, tickCount) {
250
+ if (this.hasRenderedDynamic) {
251
+ // Move cursor up to the top of the previously-painted
252
+ // dynamic block.
253
+ rawWrite(CURSOR_UP_N(DYNAMIC_LINES));
254
+ }
255
+ this.paintDynamicOnce(data, tickCount);
256
+ }
257
+ paintDynamicOnce(data, tick) {
258
+ const { status, bar } = renderDynamicRows(data, tick, this.width, this.isTty);
259
+ // Erase-then-rewrite the status row.
260
+ rawWrite(ERASE_LINE + status + '\n');
261
+ // Erase-then-rewrite the bar row. (In non-TTY mode the
262
+ // bar is empty; we still emit a newline so the line
263
+ // count stays consistent.)
264
+ rawWrite(ERASE_LINE + bar + '\n');
265
+ this.hasRenderedDynamic = true;
266
+ }
267
+ /**
268
+ * Paint a final 2-line verdict + a farewell line below the
269
+ * dashboard, then return. The cursor stays at the bottom
270
+ * of the farewell so the user's shell prompt lands on the
271
+ * next row.
272
+ */
273
+ finalize(data) {
274
+ // Rewrite the dynamic block one last time so the user can
275
+ // read the verdict.
276
+ if (this.hasRenderedDynamic) {
277
+ rawWrite(CURSOR_UP_N(DYNAMIC_LINES));
278
+ }
279
+ this.paintDynamicOnce(data, Number.MAX_SAFE_INTEGER);
280
+ // Then emit the farewell BELOW the dashboard, in green.
281
+ const verdictSuffix = data.current.verdict !== undefined ? ` (verdict=${data.current.verdict})` : '';
282
+ const farewell = this.isTty
283
+ ? chalk.green(`✔ peaks progress watch: sub-agent reached phase=${data.current.phase}${verdictSuffix} at ${new Date().toISOString()}. Auto-closing watch window.`)
284
+ : `peaks progress watch: sub-agent reached phase=${data.current.phase}${verdictSuffix} at ${new Date().toISOString()}. Auto-closing watch window.`;
285
+ rawWrite(ERASE_LINE + farewell + '\n');
286
+ }
287
+ /**
288
+ * Force a non-ANSI snapshot of the current state, used by
289
+ * the `--once` mode and for fallback when stdout is not a
290
+ * TTY. Does NOT touch the cursor state — safe to call from
291
+ * any context.
292
+ */
293
+ static snapshot(data) {
294
+ return renderDynamicRows(data, 0, 80, false);
295
+ }
296
+ }
297
+ /**
298
+ * Strip ANSI escapes from a string. Used for visible-length
299
+ * accounting; not for re-painting.
300
+ */
301
+ export function stripAnsi(input) {
302
+ // eslint-disable-next-line no-control-regex
303
+ return input.replace(/\x1b\[[0-9;]*m/g, '');
304
+ }
305
+ /** Reset terminal SGR — used on early-return error paths. */
306
+ export function resetTerminal() {
307
+ rawWrite(RESET);
308
+ }
@@ -9,6 +9,8 @@ import { registerCapabilityWorkerConfigAndSCCommands } from './commands/capabili
9
9
  import { registerCodegraphCommands } from './commands/codegraph-commands.js';
10
10
  import { registerMcpCommands } from './commands/mcp-commands.js';
11
11
  import { registerOpenSpecCommands } from './commands/openspec-commands.js';
12
+ import { registerPerfCommands } from './commands/perf-commands.js';
13
+ import { registerProgressCommands } from './commands/progress-commands.js';
12
14
  import { registerProjectCommands } from './commands/project-commands.js';
13
15
  import { registerRequestCommands } from './commands/request-commands.js';
14
16
  import { registerScanCommands } from './commands/scan-commands.js';
@@ -79,6 +81,8 @@ Run peaks (no arguments) for a quickstart. You likely want one of:
79
81
  registerCodegraphCommands(program, io);
80
82
  registerMcpCommands(program, io);
81
83
  registerOpenSpecCommands(program, io);
84
+ registerPerfCommands(program, io);
85
+ registerProgressCommands(program, io);
82
86
  registerProjectCommands(program, io);
83
87
  registerRequestCommands(program, io);
84
88
  registerScanCommands(program, io);
@@ -58,6 +58,26 @@ export type PeaksConfig = {
58
58
  tokens: TokenConfig;
59
59
  providers: ModelProviderConfig;
60
60
  proxy: ProxyConfig;
61
+ /**
62
+ * Sub-agent progress surfacing knobs. The `peaks progress watch`
63
+ * CLI (intended to be run in a separate terminal tab while the
64
+ * LLM is working) reads `.peaks/<sid>/system/subagent-progress.json`
65
+ * and renders elapsed / spinner / sub-step in real time. The
66
+ * `enabled` flag is a kill-switch for users who find the watch
67
+ * distracting; the `heartbeatIntervalMs` lets power users tune
68
+ * the write cadence. Both default to sensible values so stock
69
+ * projects get the feature out of the box.
70
+ *
71
+ * Optional on the type level so older test fixtures / hand-
72
+ * written config files do not have to know about it; the
73
+ * `DEFAULT_CONFIG.progress` block supplies the runtime defaults
74
+ * and `config get` will surface a synthesised block when the
75
+ * field is absent.
76
+ */
77
+ progress?: {
78
+ enabled: boolean;
79
+ heartbeatIntervalMs: number;
80
+ };
61
81
  };
62
82
  export type ConfigLayer = 'user' | 'project';
63
83
  export type ConfigGetOptions = {
@@ -11,5 +11,9 @@ export const DEFAULT_CONFIG = {
11
11
  model: 'minimax-2.7'
12
12
  }
13
13
  },
14
- proxy: {}
14
+ proxy: {},
15
+ progress: {
16
+ enabled: true,
17
+ heartbeatIntervalMs: 60000
18
+ }
15
19
  };
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Performance baseline scaffolding for the RD stage.
3
+ *
4
+ * peaks-solo's RD stage runs before the QA stage. The user-facing pain is
5
+ * that performance tests (lighthouse / k6 / project-local benches / etc.)
6
+ * have historically only been run at QA Gate A4 — too late in the loop,
7
+ * because a slow regression discovered at QA triggers a return-to-rd
8
+ * cycle, the RD ships another "fix", QA re-runs, and the same cycle
9
+ * repeats up to 3 times before the slice ships.
10
+ *
11
+ * `peaks perf baseline` is the user-visible artifact of a deliberate
12
+ * compromise: keep the heavy performance measurement as something the
13
+ * RD runs themselves (lighthouse is project-shape dependent and we
14
+ * don't want to bake a lighthouse dependency into the CLI), but capture
15
+ * the result in a stable, scaffolded file under
16
+ * `.peaks/<sid>/rd/perf-baseline.md` so QA Gate A4 has a known-good
17
+ * reference to diff against. The CLI itself only writes the scaffold
18
+ * and records the path; the actual measurement is a project-local
19
+ * concern that lives in the README, not in peaks-cli.
20
+ *
21
+ * The four-grounds check (per the skill-primary-CLI-auxiliary dev
22
+ * preference):
23
+ * 1. hook/script/CI invokability — yes, a hook can call this CLI
24
+ * to scaffold the file on session
25
+ * init, similar to session bootstrap.
26
+ * 2. JSON envelope that gates a downstream decision — yes,
27
+ * peaks-rd reads the result and
28
+ * attaches it to the handoff.
29
+ * 3. Destructive --apply side effect — yes, default dry-run.
30
+ * 4. Machine-enforced gate that prose cannot enforce — no, the
31
+ * measurement still lives in
32
+ * the LLM / project tools. We do
33
+ * NOT add a lint gate here.
34
+ *
35
+ * Net: CLI is justified. The destructive --apply default is dry-run,
36
+ * matching the rest of peaks-cli's scaffolding pattern.
37
+ */
38
+ export type PerfBaselineInitOptions = {
39
+ projectRoot: string;
40
+ apply?: boolean;
41
+ reason?: string;
42
+ };
43
+ export type PerfBaselinePlan = {
44
+ apply: boolean;
45
+ projectRoot: string;
46
+ sessionId: string | null;
47
+ perfBaselinePath: string | null;
48
+ plannedWrites: Array<{
49
+ path: string;
50
+ kind: 'directory' | 'file';
51
+ bytes: number;
52
+ content: string;
53
+ }>;
54
+ alreadyInitialized: boolean;
55
+ existingFiles: string[];
56
+ };
57
+ export type PerfBaselineResult = PerfBaselinePlan & {
58
+ writtenFiles: string[];
59
+ createdDirectories: string[];
60
+ reason?: string;
61
+ };
62
+ export declare function executePerfBaselineInit(options: PerfBaselineInitOptions): Promise<PerfBaselineResult>;
63
+ /**
64
+ * Re-exported so the CLI command can fall back to a project-root
65
+ * resolution when the caller did not pass --project. The CLI does
66
+ * the same findProjectRoot walk that `workspace init` does; this
67
+ * helper exists for the command layer to import without reaching
68
+ * into config-safety directly.
69
+ */
70
+ export declare function resolveProjectRootFromCwd(cwd: string): string;
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Performance baseline scaffolding for the RD stage.
3
+ *
4
+ * peaks-solo's RD stage runs before the QA stage. The user-facing pain is
5
+ * that performance tests (lighthouse / k6 / project-local benches / etc.)
6
+ * have historically only been run at QA Gate A4 — too late in the loop,
7
+ * because a slow regression discovered at QA triggers a return-to-rd
8
+ * cycle, the RD ships another "fix", QA re-runs, and the same cycle
9
+ * repeats up to 3 times before the slice ships.
10
+ *
11
+ * `peaks perf baseline` is the user-visible artifact of a deliberate
12
+ * compromise: keep the heavy performance measurement as something the
13
+ * RD runs themselves (lighthouse is project-shape dependent and we
14
+ * don't want to bake a lighthouse dependency into the CLI), but capture
15
+ * the result in a stable, scaffolded file under
16
+ * `.peaks/<sid>/rd/perf-baseline.md` so QA Gate A4 has a known-good
17
+ * reference to diff against. The CLI itself only writes the scaffold
18
+ * and records the path; the actual measurement is a project-local
19
+ * concern that lives in the README, not in peaks-cli.
20
+ *
21
+ * The four-grounds check (per the skill-primary-CLI-auxiliary dev
22
+ * preference):
23
+ * 1. hook/script/CI invokability — yes, a hook can call this CLI
24
+ * to scaffold the file on session
25
+ * init, similar to session bootstrap.
26
+ * 2. JSON envelope that gates a downstream decision — yes,
27
+ * peaks-rd reads the result and
28
+ * attaches it to the handoff.
29
+ * 3. Destructive --apply side effect — yes, default dry-run.
30
+ * 4. Machine-enforced gate that prose cannot enforce — no, the
31
+ * measurement still lives in
32
+ * the LLM / project tools. We do
33
+ * NOT add a lint gate here.
34
+ *
35
+ * Net: CLI is justified. The destructive --apply default is dry-run,
36
+ * matching the rest of peaks-cli's scaffolding pattern.
37
+ */
38
+ import { mkdir, writeFile } from 'node:fs/promises';
39
+ import { existsSync } from 'node:fs';
40
+ import { join } from 'node:path';
41
+ import { getSessionId } from '../session/session-manager.js';
42
+ import { findProjectRoot } from '../config/config-safety.js';
43
+ const README_BODY = `# Performance baseline
44
+
45
+ > Scaffolding for the RD-side performance baseline. Created by
46
+ > \`peaks perf baseline\`. The actual measurement is the RD's
47
+ > responsibility — see the "How to fill this in" section below.
48
+
49
+ ## Why this exists
50
+
51
+ The QA stage's Gate A4 (performance check) compares the slice's
52
+ performance against the most recent baseline. Without an RD-side
53
+ baseline, the first time Gate A4 runs it has nothing to compare
54
+ against and any regression it finds is a blind-side surprise.
55
+ Capturing the baseline at the RD stage — right after the
56
+ implementation lands and before QA picks it up — closes that
57
+ gap and prevents the "QA returns 3 times for the same perf
58
+ regression" loop.
59
+
60
+ ## What to capture
61
+
62
+ For each performance-sensitive code path in the slice, record:
63
+
64
+ - **Path / route** — which entry point (page, hook, API) the
65
+ measurement targets.
66
+ - **Workload** — what you did with it (cold load, hot loop, the
67
+ exact N of records the slice introduces).
68
+ - **Tool** — lighthouse / k6 / autocannon / project-local bench
69
+ script. Match the tool to the workload; do not introduce a new
70
+ one if the project already has a benchmark script.
71
+ - **Metrics** — at minimum LCP / FCP / TBT / CLS for frontend,
72
+ p50/p95/p99 latency + rps for backend, rss / heap growth
73
+ for long-running services.
74
+ - **Baseline value** — the number you measured, with units.
75
+ - **Threshold** — what the slice's PRD / acceptance criteria
76
+ consider acceptable. If the PRD does not specify, leave this
77
+ field as \`TBD (ask PM)\` and surface it in the RD handoff.
78
+
79
+ ## How to fill this in
80
+
81
+ 1. Run the project's chosen performance tool against the
82
+ implementation you just landed. If the project does not have
83
+ a tool yet, the lightest first step is the chrome devtools
84
+ performance tab on the touched route.
85
+ 2. For each metric, copy the row from "What to capture" into
86
+ the "Results" table below and fill in the number.
87
+ 3. The threshold is the bar QA Gate A4 will compare against.
88
+ Be conservative — if the threshold is tighter than what the
89
+ tool reports, Gate A4 will fail.
90
+
91
+ ## Results
92
+
93
+ | Path / route | Workload | Tool | Metric | Baseline | Threshold |
94
+ |---|---|---|---|---|---|
95
+ | | | | | | |
96
+
97
+ ## Notes
98
+
99
+ - If the slice is documentation-only or has no user-visible
100
+ performance surface, write \`N/A — no perf surface\` here and
101
+ surface that fact in the RD handoff.
102
+ - If the measurement exceeded the threshold on the first run,
103
+ do NOT loosen the threshold to make it pass. The right move
104
+ is to optimise the implementation and re-measure, or to
105
+ surface the trade-off to the PRD owner for a threshold bump.
106
+
107
+ ## Handoff
108
+
109
+ - to peaks-qa: the \`Results\` table is the input to Gate A4.
110
+ Without it QA cannot establish a comparison baseline.
111
+ - to peaks-sc: any threshold bumps captured here belong in the
112
+ release notes if the threshold moved.
113
+ `;
114
+ function renderBaselineTemplate() {
115
+ return README_BODY;
116
+ }
117
+ function buildPlan(projectRoot, apply) {
118
+ const sessionId = getSessionId(projectRoot);
119
+ const sessionRoot = sessionId !== null
120
+ ? join(projectRoot, '.peaks', sessionId)
121
+ : null;
122
+ const perfBaselinePath = sessionRoot !== null
123
+ ? join(sessionRoot, 'rd', 'perf-baseline.md')
124
+ : null;
125
+ const plannedWrites = [];
126
+ if (sessionRoot !== null && perfBaselinePath !== null) {
127
+ plannedWrites.push({
128
+ path: join(sessionRoot, 'rd'),
129
+ kind: 'directory',
130
+ bytes: 0,
131
+ content: ''
132
+ });
133
+ plannedWrites.push({
134
+ path: perfBaselinePath,
135
+ kind: 'file',
136
+ bytes: 0,
137
+ content: renderBaselineTemplate()
138
+ });
139
+ for (const write of plannedWrites) {
140
+ if (write.kind === 'file') {
141
+ write.bytes = Buffer.byteLength(write.content, 'utf8');
142
+ }
143
+ }
144
+ }
145
+ return {
146
+ apply,
147
+ projectRoot,
148
+ sessionId,
149
+ perfBaselinePath,
150
+ plannedWrites,
151
+ alreadyInitialized: false,
152
+ existingFiles: []
153
+ };
154
+ }
155
+ /**
156
+ * Idempotency: skip writes for the perf-baseline file when it
157
+ * already exists. Re-running `peaks perf baseline` on the same
158
+ * session is a normal RD retry pattern (re-measurement, threshold
159
+ * adjustment, etc.); we must not blow away hand-edited content.
160
+ */
161
+ async function planPerfBaselineInit(options) {
162
+ const plan = buildPlan(options.projectRoot, options.apply ?? false);
163
+ if (plan.perfBaselinePath !== null && existsSync(plan.perfBaselinePath)) {
164
+ plan.alreadyInitialized = true;
165
+ plan.existingFiles = [plan.perfBaselinePath];
166
+ plan.plannedWrites = [];
167
+ }
168
+ return plan;
169
+ }
170
+ export async function executePerfBaselineInit(options) {
171
+ const plan = await planPerfBaselineInit(options);
172
+ const writtenFiles = [];
173
+ const createdDirectories = [];
174
+ if (plan.sessionId === null) {
175
+ return {
176
+ ...plan,
177
+ ...(options.reason !== undefined ? { reason: options.reason } : {}),
178
+ writtenFiles,
179
+ createdDirectories
180
+ };
181
+ }
182
+ if (plan.apply && !plan.alreadyInitialized) {
183
+ for (const write of plan.plannedWrites) {
184
+ if (write.kind === 'directory') {
185
+ if (!existsSync(write.path)) {
186
+ await mkdir(write.path, { recursive: true });
187
+ createdDirectories.push(write.path);
188
+ }
189
+ continue;
190
+ }
191
+ if (write.content.length === 0)
192
+ continue;
193
+ await writeFile(write.path, write.content, 'utf8');
194
+ writtenFiles.push(write.path);
195
+ }
196
+ }
197
+ return {
198
+ ...plan,
199
+ ...(options.reason !== undefined ? { reason: options.reason } : {}),
200
+ writtenFiles,
201
+ createdDirectories
202
+ };
203
+ }
204
+ /**
205
+ * Re-exported so the CLI command can fall back to a project-root
206
+ * resolution when the caller did not pass --project. The CLI does
207
+ * the same findProjectRoot walk that `workspace init` does; this
208
+ * helper exists for the command layer to import without reaching
209
+ * into config-safety directly.
210
+ */
211
+ export function resolveProjectRootFromCwd(cwd) {
212
+ return findProjectRoot(cwd) ?? cwd;
213
+ }