@principles/pd-cli 1.74.0 → 1.76.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 (38) hide show
  1. package/dist/commands/config-doctor.d.ts +3 -6
  2. package/dist/commands/config-doctor.d.ts.map +1 -1
  3. package/dist/commands/config-doctor.js +30 -31
  4. package/dist/commands/config-doctor.js.map +1 -1
  5. package/dist/commands/console.d.ts +18 -0
  6. package/dist/commands/console.d.ts.map +1 -1
  7. package/dist/commands/console.js +439 -0
  8. package/dist/commands/console.js.map +1 -1
  9. package/dist/commands/runtime-features.d.ts +23 -8
  10. package/dist/commands/runtime-features.d.ts.map +1 -1
  11. package/dist/commands/runtime-features.js +72 -31
  12. package/dist/commands/runtime-features.js.map +1 -1
  13. package/dist/index.js +51 -15
  14. package/dist/index.js.map +1 -1
  15. package/dist/services/config-doctor.d.ts +26 -66
  16. package/dist/services/config-doctor.d.ts.map +1 -1
  17. package/dist/services/config-doctor.js +197 -374
  18. package/dist/services/config-doctor.js.map +1 -1
  19. package/dist/services/console-launcher.d.ts +110 -0
  20. package/dist/services/console-launcher.d.ts.map +1 -0
  21. package/dist/services/console-launcher.js +282 -0
  22. package/dist/services/console-launcher.js.map +1 -0
  23. package/dist/services/pd-config-loader.d.ts +64 -0
  24. package/dist/services/pd-config-loader.d.ts.map +1 -0
  25. package/dist/services/pd-config-loader.js +156 -0
  26. package/dist/services/pd-config-loader.js.map +1 -0
  27. package/package.json +1 -1
  28. package/src/commands/config-doctor.ts +30 -30
  29. package/src/commands/console.ts +445 -1
  30. package/src/commands/runtime-features.ts +98 -44
  31. package/src/index.ts +55 -16
  32. package/src/services/config-doctor.ts +236 -425
  33. package/src/services/console-launcher.ts +373 -0
  34. package/src/services/pd-config-loader.ts +213 -0
  35. package/tests/commands/config-doctor.test.ts +207 -506
  36. package/tests/commands/console-open.test.ts +773 -0
  37. package/tests/commands/runtime-features.test.ts +220 -85
  38. package/tests/services/pd-config-loader.test.ts +479 -0
@@ -1,7 +1,31 @@
1
1
  import * as path from 'path';
2
2
  import * as fs from 'fs';
3
- import { spawn } from 'child_process';
3
+ import { spawn, type ChildProcess } from 'child_process';
4
4
  import { resolveWorkspaceDir } from '../resolve-workspace.js';
5
+ import {
6
+ planConsoleLaunch,
7
+ openBrowser,
8
+ isLoopbackHost,
9
+ normalizeLoopbackHost,
10
+ probeConsoleHealth,
11
+ type ConsoleLaunchResult,
12
+ } from '../services/console-launcher.js';
13
+
14
+ function sleep(ms: number): Promise<void> {
15
+ return new Promise((res) => setTimeout(res, ms));
16
+ }
17
+
18
+ interface ConsoleOpenOptions {
19
+ workspace?: string;
20
+ port?: string;
21
+ host?: string;
22
+ json?: boolean;
23
+ noAuth?: boolean;
24
+ /** Skip browser opening even in non-JSON mode. */
25
+ noBrowser?: boolean;
26
+ }
27
+
28
+ // ─── Backward-compatible top-level launcher (pd console) ─────────────────────
5
29
 
6
30
  interface ConsoleOptions {
7
31
  workspace?: string;
@@ -119,3 +143,423 @@ export async function handleConsole(opts: ConsoleOptions = {}): Promise<void> {
119
143
  process.on('SIGINT', cleanup);
120
144
  process.on('SIGTERM', cleanup);
121
145
  }
146
+
147
+ // ─── Seed-friendly launcher (pd console open) — PRI-300 ─────────────────────
148
+
149
+ /**
150
+ * Launch (or reuse) the PD Console with seed-friendly defaults:
151
+ * - Default port 3100; auto-falls back to next free port
152
+ * - Reuses an existing healthy Console if one is already running
153
+ * - Opens the system browser on success (skipped in --json)
154
+ * - Refuses non-loopback hosts
155
+ * - Emits structured reason+nextAction on every failure path
156
+ */
157
+ export async function handleConsoleOpen(opts: ConsoleOpenOptions = {}): Promise<void> {
158
+ // 1) Loopback safety (ERR-049: refuse non-loopback) — check FIRST so we
159
+ // never reveal runtime information about non-loopback hosts and refuse before workspace resolution.
160
+ const rawHost = opts.host ?? '127.0.0.1';
161
+ // Normalize IPv6 bracket notation: [::1] → ::1 (ERR-049)
162
+ const host = normalizeLoopbackHost(rawHost);
163
+ if (!isLoopbackHost(host)) {
164
+ const result: ConsoleLaunchResult = {
165
+ status: 'refused',
166
+ url: '',
167
+ port: 0,
168
+ host,
169
+ workspaceDir: '',
170
+ reused: false,
171
+ browserOpened: false,
172
+ reason: `Non-loopback host refused: '${host}'. Console binds to loopback only.`,
173
+ nextAction: 'Use the default (127.0.0.1) or "localhost". Do not pass --host 0.0.0.0 or a LAN address.',
174
+ };
175
+ if (opts.json) {
176
+ console.log(JSON.stringify(result, null, 2));
177
+ } else {
178
+ console.error(`error: ${result.reason}`);
179
+ console.error(`next: ${result.nextAction}`);
180
+ }
181
+ process.exit(1);
182
+ return;
183
+ }
184
+
185
+ // 2) Resolve workspace (ERR-040: fail loud if missing)
186
+ let workspaceDir: string;
187
+ try {
188
+ workspaceDir = opts.workspace ? path.resolve(opts.workspace) : resolveWorkspaceDir();
189
+ } catch (err) {
190
+ const message = err instanceof Error ? err.message : String(err);
191
+ const result: ConsoleLaunchResult = {
192
+ status: 'failed',
193
+ url: '',
194
+ port: 0,
195
+ host,
196
+ workspaceDir: '',
197
+ reused: false,
198
+ browserOpened: false,
199
+ reason: 'workspace_missing',
200
+ nextAction: 'Pass --workspace <path>, set PD_WORKSPACE_DIR, or run from within an initialized workspace.',
201
+ };
202
+ if (opts.json) {
203
+ console.log(JSON.stringify({ ...result, message }, null, 2));
204
+ } else {
205
+ console.error(`error: ${message}`);
206
+ console.error(`next: ${result.nextAction}`);
207
+ }
208
+ process.exit(1);
209
+ return;
210
+ }
211
+
212
+ // 3) Strict port parsing
213
+ let preferredPort = 3100;
214
+ if (opts.port !== undefined) {
215
+ if (!/^\d+$/.test(opts.port)) {
216
+ const result: ConsoleLaunchResult = {
217
+ status: 'failed',
218
+ url: '',
219
+ port: 0,
220
+ host,
221
+ workspaceDir,
222
+ reused: false,
223
+ browserOpened: false,
224
+ reason: `Invalid --port: '${opts.port}'. Must be an integer 1..65535.`,
225
+ nextAction: 'Use --port 3100 (default) or another valid port number.',
226
+ };
227
+ if (opts.json) {
228
+ console.log(JSON.stringify(result, null, 2));
229
+ } else {
230
+ console.error(`error: ${result.reason}`);
231
+ console.error(`next: ${result.nextAction}`);
232
+ }
233
+ process.exit(1);
234
+ return;
235
+ }
236
+ preferredPort = Number(opts.port);
237
+ if (preferredPort < 1 || preferredPort > 65535) {
238
+ const result: ConsoleLaunchResult = {
239
+ status: 'failed',
240
+ url: '',
241
+ port: 0,
242
+ host,
243
+ workspaceDir,
244
+ reused: false,
245
+ browserOpened: false,
246
+ reason: `Invalid --port: '${opts.port}'. Must be an integer 1..65535.`,
247
+ nextAction: 'Use --port 3100 (default) or another valid port number.',
248
+ };
249
+ if (opts.json) {
250
+ console.log(JSON.stringify(result, null, 2));
251
+ } else {
252
+ console.error(`error: ${result.reason}`);
253
+ console.error(`next: ${result.nextAction}`);
254
+ }
255
+ process.exit(1);
256
+ return;
257
+ }
258
+ }
259
+
260
+ // 3) Check that the console runtime is installed (ERR-040: fail loud if missing)
261
+ const consoleDir = getConsoleDir();
262
+ if (!consoleDir) {
263
+ const result: ConsoleLaunchResult = {
264
+ status: 'failed',
265
+ url: '',
266
+ port: 0,
267
+ host: '127.0.0.1',
268
+ workspaceDir,
269
+ reused: false,
270
+ browserOpened: false,
271
+ reason: 'console_runtime_not_installed',
272
+ nextAction: 'Run: npx create-principles-disciple',
273
+ };
274
+ if (opts.json) {
275
+ console.log(JSON.stringify(result, null, 2));
276
+ } else {
277
+ console.error(`error: ${result.reason}`);
278
+ console.error(`next: ${result.nextAction}`);
279
+ }
280
+ process.exit(1);
281
+ return;
282
+ }
283
+ const serverEntry = path.join(consoleDir, 'dist', 'server.js');
284
+ if (!fs.existsSync(serverEntry)) {
285
+ const result: ConsoleLaunchResult = {
286
+ status: 'failed',
287
+ url: '',
288
+ port: 0,
289
+ host: '127.0.0.1',
290
+ workspaceDir,
291
+ reused: false,
292
+ browserOpened: false,
293
+ reason: 'console_server_entry_missing',
294
+ nextAction: `Re-run installer: npx create-principles-disciple (expected ${serverEntry})`,
295
+ };
296
+ if (opts.json) {
297
+ console.log(JSON.stringify(result, null, 2));
298
+ } else {
299
+ console.error(`error: ${result.reason}`);
300
+ console.error(`next: ${result.nextAction}`);
301
+ }
302
+ process.exit(1);
303
+ return;
304
+ }
305
+
306
+ // 4) Read auth token for health probes (PD_CONSOLE_TOKEN)
307
+ const token = process.env.PD_CONSOLE_TOKEN;
308
+
309
+ // 5) Plan the launch (reuse or fresh bind)
310
+ let plan;
311
+ try {
312
+ plan = await planConsoleLaunch({ workspaceDir, preferredPort, host, token });
313
+ } catch (err) {
314
+ const message = err instanceof Error ? err.message : String(err);
315
+ const result: ConsoleLaunchResult = {
316
+ status: 'failed',
317
+ url: '',
318
+ port: preferredPort,
319
+ host,
320
+ workspaceDir,
321
+ reused: false,
322
+ browserOpened: false,
323
+ reason: 'launch_plan_error',
324
+ nextAction: 'Inspect logs and retry.',
325
+ };
326
+ if (opts.json) {
327
+ console.log(JSON.stringify({ ...result, message }, null, 2));
328
+ } else {
329
+ console.error(`error: launch plan failed: ${message}`);
330
+ }
331
+ process.exit(1);
332
+ return;
333
+ }
334
+
335
+ if (plan.status === 'refused') {
336
+ const result: ConsoleLaunchResult = {
337
+ status: 'refused',
338
+ url: '',
339
+ port: plan.port,
340
+ host: plan.host,
341
+ workspaceDir,
342
+ reused: false,
343
+ browserOpened: false,
344
+ reason: plan.reason,
345
+ nextAction: plan.nextAction,
346
+ };
347
+ if (opts.json) {
348
+ console.log(JSON.stringify(result, null, 2));
349
+ } else {
350
+ console.error(`error: ${result.reason}`);
351
+ console.error(`next: ${result.nextAction}`);
352
+ }
353
+ process.exit(1);
354
+ return;
355
+ }
356
+
357
+ if (plan.status === 'reused') {
358
+ // Existing console — verify health one more time (already healthy from plan, but be safe)
359
+ const health = await probeConsoleHealth({ host: plan.host, port: plan.port, token });
360
+ if (!health.healthy) {
361
+ const result: ConsoleLaunchResult = {
362
+ status: 'failed',
363
+ url: '',
364
+ port: plan.port,
365
+ host: plan.host,
366
+ workspaceDir,
367
+ reused: false,
368
+ browserOpened: false,
369
+ reason: `port_in_use_by_non_console: ${health.reason ?? 'health probe failed'}`,
370
+ nextAction: 'Stop the conflicting process, or use --port <free> to bind a different port.',
371
+ };
372
+ if (opts.json) {
373
+ console.log(JSON.stringify(result, null, 2));
374
+ } else {
375
+ console.error(`error: ${result.reason}`);
376
+ console.error(`next: ${result.nextAction}`);
377
+ }
378
+ process.exit(1);
379
+ return;
380
+ }
381
+ // Reused path — do not spawn. Optionally open browser.
382
+ let browserOpened = false;
383
+ let browserWarning: string | undefined;
384
+ if (!opts.json && !opts.noBrowser) {
385
+ const result = await openBrowser(plan.url);
386
+ browserOpened = result.opened;
387
+ if (!result.opened) {
388
+ browserWarning = result.reason;
389
+ }
390
+ }
391
+ const out: ConsoleLaunchResult = {
392
+ status: 'reused',
393
+ url: plan.url,
394
+ port: plan.port,
395
+ host: plan.host,
396
+ workspaceDir,
397
+ reused: true,
398
+ browserOpened,
399
+ nextAction: browserOpened
400
+ ? 'Browser opened to the running Console.'
401
+ : `Open ${plan.url} in your browser to access the Console.`,
402
+ };
403
+ if (browserWarning) out.reason = `browser_open_failed: ${browserWarning}`;
404
+ if (opts.json) {
405
+ console.log(JSON.stringify(out, null, 2));
406
+ } else {
407
+ console.log(`Reusing existing Console at ${plan.url}`);
408
+ if (browserOpened) {
409
+ console.log('Browser opened.');
410
+ } else if (browserWarning) {
411
+ console.log(`Browser not opened: ${browserWarning}`);
412
+ console.log(out.nextAction);
413
+ } else {
414
+ console.log(out.nextAction);
415
+ }
416
+ }
417
+ return;
418
+ }
419
+
420
+ // 5) Fresh spawn path
421
+ const args = [serverEntry, '--workspace', workspaceDir, '--port', String(plan.port), '--host', plan.host];
422
+ if (opts.noAuth) args.push('--no-auth');
423
+
424
+ const child: ChildProcess = spawn(process.execPath, args, {
425
+ stdio: opts.json ? 'pipe' : 'inherit',
426
+ env: { ...process.env },
427
+ });
428
+
429
+ let resolved = false;
430
+ const resolveOnce = (fn: () => void) => {
431
+ if (resolved) return;
432
+ resolved = true;
433
+ fn();
434
+ };
435
+
436
+ const cleanup = () => {
437
+ resolveOnce(() => {
438
+ try { child.kill('SIGTERM'); } catch { /* ignore */ }
439
+ process.exit(0);
440
+ });
441
+ };
442
+
443
+ child.on('error', (err) => {
444
+ resolveOnce(() => {
445
+ const result: ConsoleLaunchResult = {
446
+ status: 'failed',
447
+ url: '',
448
+ port: plan.port,
449
+ host: plan.host,
450
+ workspaceDir,
451
+ reused: false,
452
+ browserOpened: false,
453
+ reason: `console_spawn_failed: ${err.message}`,
454
+ nextAction: 'Check Node.js and package path configuration.',
455
+ };
456
+ if (opts.json) {
457
+ console.log(JSON.stringify(result, null, 2));
458
+ } else {
459
+ console.error(`error: ${result.reason}`);
460
+ }
461
+ try { child.kill('SIGTERM'); } catch { /* ignore */ }
462
+ process.exit(1);
463
+ });
464
+ });
465
+
466
+ process.on('SIGINT', cleanup);
467
+ process.on('SIGTERM', cleanup);
468
+
469
+ // 7) Wait for console ready (bounded poll)
470
+ const readyDeadline = Date.now() + 15_000;
471
+ let ready = false;
472
+ while (Date.now() < readyDeadline) {
473
+ if (child.exitCode !== null) break;
474
+ const h = await probeConsoleHealth({ host: plan.host, port: plan.port, timeoutMs: 1000, token });
475
+ if (h.healthy) { ready = true; break; }
476
+ await sleep(250);
477
+ }
478
+
479
+ if (child.exitCode !== null && child.exitCode !== 0) {
480
+ const result: ConsoleLaunchResult = {
481
+ status: 'failed',
482
+ url: '',
483
+ port: plan.port,
484
+ host: plan.host,
485
+ workspaceDir,
486
+ reused: false,
487
+ browserOpened: false,
488
+ reason: `console_exited_with_code_${child.exitCode}`,
489
+ nextAction: 'Check console logs above. Re-run: npx create-principles-disciple',
490
+ };
491
+ if (opts.json) {
492
+ console.log(JSON.stringify(result, null, 2));
493
+ } else {
494
+ console.error(`error: ${result.reason}`);
495
+ }
496
+ process.exit(typeof child.exitCode === 'number' ? child.exitCode : 1);
497
+ return;
498
+ }
499
+
500
+ if (!ready) {
501
+ // Clean up: kill the orphan child
502
+ try { child.kill('SIGTERM'); } catch { /* ignore */ }
503
+ const result: ConsoleLaunchResult = {
504
+ status: 'failed',
505
+ url: '',
506
+ port: plan.port,
507
+ host: plan.host,
508
+ workspaceDir,
509
+ reused: false,
510
+ browserOpened: false,
511
+ reason: 'console_health_check_timeout',
512
+ nextAction: 'Increase timeout, free system resources, or re-run: npx create-principles-disciple',
513
+ };
514
+ if (opts.json) {
515
+ console.log(JSON.stringify(result, null, 2));
516
+ } else {
517
+ console.error(`error: ${result.reason}`);
518
+ console.error(`next: ${result.nextAction}`);
519
+ }
520
+ process.exit(1);
521
+ return;
522
+ }
523
+
524
+ // 7) Console is ready → optionally open browser, emit result, then keep child running
525
+ let browserOpened = false;
526
+ let browserWarning: string | undefined;
527
+ if (!opts.json && !opts.noBrowser) {
528
+ const r = await openBrowser(plan.url);
529
+ browserOpened = r.opened;
530
+ if (!r.opened) browserWarning = r.reason;
531
+ }
532
+
533
+ const out: ConsoleLaunchResult = {
534
+ status: 'started',
535
+ url: plan.url,
536
+ port: plan.port,
537
+ host: plan.host,
538
+ workspaceDir,
539
+ reused: false,
540
+ browserOpened,
541
+ nextAction: browserOpened
542
+ ? 'Browser opened to the Console. Press Ctrl+C to stop.'
543
+ : `Open ${plan.url} in your browser. Press Ctrl+C to stop.`,
544
+ };
545
+ if (plan.reason) out.reason = plan.reason;
546
+ if (browserWarning) {
547
+ out.reason = out.reason ? `${out.reason}; browser_open_failed: ${browserWarning}` : `browser_open_failed: ${browserWarning}`;
548
+ }
549
+
550
+ if (opts.json) {
551
+ // Single JSON object on stdout, then keep child attached.
552
+ console.log(JSON.stringify(out, null, 2));
553
+ } else {
554
+ console.log(`\nConsole ready: ${plan.url}`);
555
+ console.log(`Workspace: ${workspaceDir}`);
556
+ if (plan.reason) console.log(`Note: ${plan.reason}`);
557
+ if (browserOpened) {
558
+ console.log('Browser opened. Press Ctrl+C to stop.');
559
+ } else {
560
+ console.log(`Open ${plan.url} in your browser. Press Ctrl+C to stop.`);
561
+ }
562
+ }
563
+
564
+
565
+ }
@@ -1,103 +1,157 @@
1
+ /**
2
+ * pd runtime features — Show effective feature flags from .pd/config.yaml.
3
+ *
4
+ * PRI-305: Cutover from .pd/feature-flags.yaml to .pd/config.yaml.
5
+ * Uses core computeFeatureFlagsFromConfig for flag computation.
6
+ * --json outputs a single parseable JSON object.
7
+ * Missing config uses core defaults with nextAction.
8
+ * Malformed config fails loud with reason and nextAction.
9
+ * No secret output.
10
+ */
11
+
1
12
  import * as path from 'path';
2
- import { loadEffectiveFeatureFlags } from '../services/feature-flag-loader.js';
13
+ import { loadPdConfig, computeFlagsFromLoadResult } from '../services/pd-config-loader.js';
3
14
  import { resolveWorkspaceDir } from '../resolve-workspace.js';
4
15
 
5
- export interface FeatureFlagsStatusOutput {
6
- status: 'ok' | 'degraded';
7
- source: string;
16
+ // ── Output types ─────────────────────────────────────────────────────────────
17
+
18
+ export interface RuntimeFeaturesOutput {
19
+ status: 'ok' | 'degraded' | 'failed';
20
+ source: 'defaults' | 'user_config' | 'malformed';
8
21
  configPath: string;
9
- flags: {
22
+ features: {
10
23
  id: string;
11
24
  category: string;
12
25
  enabled: boolean;
13
- since: string;
14
- description?: string;
15
26
  }[];
16
- warnings: string[];
27
+ enabledMvpChannels: string[];
17
28
  totalFlags: number;
18
29
  enabledCount: number;
19
30
  disabledCount: number;
31
+ warnings: string[];
20
32
  reason?: string;
21
33
  nextAction?: string;
34
+ /** Malformed config errors (only present when source=malformed) */
35
+ errors?: { path: string; reason: string; nextAction: string }[];
22
36
  }
23
37
 
24
- interface FeaturesOptions {
25
- workspace?: string;
26
- json?: boolean;
27
- }
38
+ // ── Build output ─────────────────────────────────────────────────────────────
28
39
 
29
- export function buildFeatureFlagsStatus(workspaceDir: string): FeatureFlagsStatusOutput {
30
- const effective = loadEffectiveFeatureFlags(workspaceDir);
31
- const flags = Object.values(effective.flags);
32
- const enabledCount = flags.filter(f => f.enabled).length;
33
- const hasWarnings = effective.warnings.length > 0;
34
-
35
- return {
36
- status: hasWarnings ? 'degraded' : 'ok',
37
- source: effective.source,
38
- configPath: effective.configPath,
39
- flags: flags.map(f => ({
40
- id: f.id,
41
- category: f.category,
42
- enabled: f.enabled,
43
- since: f.since,
44
- ...(f.description ? { description: f.description } : {}),
45
- })),
46
- warnings: effective.warnings,
47
- totalFlags: flags.length,
40
+ export function buildRuntimeFeaturesStatus(workspaceDir: string): RuntimeFeaturesOutput {
41
+ const loadResult = loadPdConfig(workspaceDir);
42
+ const flags = computeFlagsFromLoadResult(loadResult);
43
+
44
+ const allFlags = Object.values(flags.flags);
45
+ const enabledCount = allFlags.filter(f => f.enabled).length;
46
+ const features = allFlags.map(f => ({
47
+ id: f.id,
48
+ category: f.category,
49
+ enabled: f.enabled,
50
+ }));
51
+
52
+ // Determine status
53
+ let status: RuntimeFeaturesOutput['status'] = 'ok';
54
+ const warnings = [...loadResult.warnings, ...flags.warnings];
55
+
56
+ if (!loadResult.ok) {
57
+ status = 'failed';
58
+ } else if (warnings.length > 0) {
59
+ status = 'degraded';
60
+ }
61
+
62
+ const output: RuntimeFeaturesOutput = {
63
+ status,
64
+ source: loadResult.ok ? loadResult.source : 'malformed',
65
+ configPath: loadResult.configPath,
66
+ features,
67
+ enabledMvpChannels: [...flags.enabledChannels],
68
+ totalFlags: allFlags.length,
48
69
  enabledCount,
49
- disabledCount: flags.length - enabledCount,
50
- ...(hasWarnings ? {
51
- reason: `Config warnings: ${effective.warnings.join('; ')}`,
52
- nextAction: 'Review feature-flags.yaml for malformed overrides or unknown flags',
53
- } : {}),
70
+ disabledCount: allFlags.length - enabledCount,
71
+ warnings,
54
72
  };
73
+
74
+ // Add reason and nextAction for non-ok states
75
+ if (!loadResult.ok) {
76
+ output.reason = `Config validation failed: ${loadResult.errors.map(e => e.reason).join('; ')}`;
77
+ output.nextAction = loadResult.errors[0]?.nextAction ?? 'Fix .pd/config.yaml and retry';
78
+ output.errors = loadResult.errors;
79
+ } else if (warnings.length > 0) {
80
+ output.reason = `Config warnings: ${warnings.slice(0, 3).join('; ')}`;
81
+ output.nextAction = 'Review .pd/config.yaml for warnings';
82
+ }
83
+
84
+ return output;
55
85
  }
56
86
 
57
- function formatTextOutput(output: FeatureFlagsStatusOutput): string {
87
+ // ── Text formatting ──────────────────────────────────────────────────────────
88
+
89
+ function formatTextOutput(output: RuntimeFeaturesOutput): string {
58
90
  const lines: string[] = [];
59
91
 
60
- lines.push('PD Feature Flags Status');
92
+ lines.push('PD Runtime Features');
61
93
  lines.push(`source: ${output.source}`);
62
94
  lines.push(`config: ${output.configPath}`);
63
95
  lines.push('');
64
96
 
65
- const categoryOrder = ['core', 'quiet', 'gone', 'legacy_retire'] as const;
97
+ const categoryOrder = ['core', 'quiet', 'gone'] as const;
66
98
  for (const category of categoryOrder) {
67
- const categoryFlags = output.flags.filter(f => f.category === category);
99
+ const categoryFlags = output.features.filter(f => f.category === category);
68
100
  if (categoryFlags.length === 0) continue;
69
101
 
70
102
  lines.push(` ${category.toUpperCase()} (${categoryFlags.length})`);
71
103
  for (const flag of categoryFlags) {
72
104
  const icon = flag.enabled ? '+' : '-';
73
- lines.push(` [${icon}] ${flag.id} (since ${flag.since})${flag.description ? ` — ${flag.description}` : ''}`);
105
+ lines.push(` [${icon}] ${flag.id}`);
74
106
  }
75
107
  lines.push('');
76
108
  }
77
109
 
78
110
  lines.push(`Total: ${output.totalFlags} flags, ${output.enabledCount} enabled, ${output.disabledCount} disabled`);
111
+ lines.push(`MVP channels: ${output.enabledMvpChannels.length > 0 ? output.enabledMvpChannels.join(', ') : '(none)'}`);
79
112
 
80
113
  if (output.warnings.length > 0) {
81
114
  lines.push('');
82
115
  lines.push('Warnings:');
83
- for (const warning of output.warnings) {
84
- lines.push(` [!] ${warning}`);
116
+ for (const w of output.warnings) {
117
+ lines.push(` [!] ${w}`);
118
+ }
119
+ }
120
+
121
+ if (output.errors && output.errors.length > 0) {
122
+ lines.push('');
123
+ lines.push('Errors:');
124
+ for (const e of output.errors) {
125
+ lines.push(` [x] ${e.path}: ${e.reason}`);
126
+ lines.push(` → ${e.nextAction}`);
85
127
  }
86
128
  }
87
129
 
88
130
  return lines.join('\n');
89
131
  }
90
132
 
133
+ // ── CLI handler ──────────────────────────────────────────────────────────────
134
+
135
+ interface FeaturesOptions {
136
+ workspace?: string;
137
+ json?: boolean;
138
+ }
139
+
91
140
  export async function handleRuntimeFeaturesStatus(opts: FeaturesOptions): Promise<void> {
92
141
  const workspaceDir = opts.workspace
93
142
  ? path.resolve(opts.workspace)
94
143
  : resolveWorkspaceDir();
95
144
 
96
- const output = buildFeatureFlagsStatus(workspaceDir);
145
+ const output = buildRuntimeFeaturesStatus(workspaceDir);
97
146
 
98
147
  if (opts.json) {
148
+ // JSON mode: single parseable object on stdout
99
149
  console.log(JSON.stringify(output, null, 2));
100
150
  } else {
101
151
  console.log(formatTextOutput(output));
102
152
  }
153
+
154
+ if (output.status === 'failed') {
155
+ process.exitCode = 1;
156
+ }
103
157
  }