@principles/pd-cli 1.74.0 → 1.75.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.
@@ -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
+ }
package/src/index.ts CHANGED
@@ -46,6 +46,7 @@ import { handleRuntimeActivationDispatch } from './commands/runtime-activation.j
46
46
  import { handleProvenChannelBaseline } from './commands/proven-channel-baseline.js';
47
47
  import { handleDemoStoryA } from './commands/demo-story-a.js';
48
48
  import { handleRuntimeFeaturesStatus } from './commands/runtime-features.js';
49
+ import { handleConfigDoctor } from './commands/config-doctor.js';
49
50
 
50
51
  import { createRequire } from 'module';
51
52
  const require = createRequire(import.meta.url);
@@ -55,8 +56,9 @@ const program = new Command();
55
56
 
56
57
  program
57
58
  .name('pd')
58
- .description('PD CLI 鈥?Pain recording, sample management, and evolution tasks')
59
- .version(pkg.version);
59
+ .description('PD CLI Pain recording, sample management, and evolution tasks')
60
+ .version(pkg.version)
61
+ .enablePositionalOptions();
60
62
 
61
63
  const painCmd = program
62
64
  .command('pain')
@@ -367,6 +369,19 @@ runtimeCmd
367
369
  });
368
370
  });
369
371
 
372
+ const configCmd = program
373
+ .command('config')
374
+ .description('PD configuration discovery and diagnosis');
375
+
376
+ configCmd
377
+ .command('doctor')
378
+ .description('Show PD + OpenClaw config locations, feature flags, and provider connectivity (PRI-299)')
379
+ .option('-w, --workspace <path>', 'Workspace directory')
380
+ .option('--json', 'Output raw JSON')
381
+ .action(async (opts) => {
382
+ await handleConfigDoctor({ workspace: opts.workspace, json: opts.json });
383
+ });
384
+
370
385
  const demoCmd = program
371
386
  .command('demo')
372
387
  .description('Demo scenarios for MVP validation');
@@ -813,9 +828,18 @@ const _legacyCleanupCmd = legacyCmd
813
828
  await handleLegacyCleanup(opts.workspace, apply);
814
829
  });
815
830
 
816
- program
831
+ const consoleCmd = program
817
832
  .command('console')
818
- .description('Start the pd-console web UI for principle review')
833
+ .description('Start the pd-console web UI for principle review (default: legacy launcher)')
834
+ .passThroughOptions()
835
+ .option('-w, --workspace <path>', 'Workspace directory')
836
+ .option('-p, --port <port>', 'Port to listen on', '3100')
837
+ .option('--no-auth', 'Disable authentication (local dev only)', false)
838
+ .option('--json', 'Output JSON status', false);
839
+
840
+ consoleCmd
841
+ .command('start')
842
+ .description('Legacy launcher — start the pd-console on the requested port (no reuse, no browser open)')
819
843
  .option('-w, --workspace <path>', 'Workspace directory')
820
844
  .option('-p, --port <port>', 'Port to listen on', '3100')
821
845
  .option('--no-auth', 'Disable authentication (local dev only)', false)
@@ -825,27 +849,42 @@ program
825
849
  await handleConsole({
826
850
  workspace: opts.workspace,
827
851
  port: opts.port,
828
- noAuth: opts.noAuth,
852
+ noAuth: opts.auth === false,
829
853
  json: opts.json,
830
854
  });
831
855
  });
832
856
 
833
- // PRI-299: PD Config Doctor 鈥?surface config paths, feature flags, and provider connectivity
834
- const configCmd = program
835
- .command('config')
836
- .description('PD config diagnostics');
837
-
838
- configCmd
839
- .command('doctor')
840
- .description('Inspect PD / OpenClaw config paths, feature flags, and provider connectivity (PRI-299)')
857
+ // PRI-300: seed-friendly Console launcher with reuse + auto-port + browser open
858
+ consoleCmd
859
+ .command('open')
860
+ .description('Open the pd-console in your browser (PRI-300) — auto-port, reuses running console, loopback-only')
841
861
  .option('-w, --workspace <path>', 'Workspace directory')
842
- .option('--json', 'Output a single parseable JSON object on stdout', false)
862
+ .option('-p, --port <port>', 'Preferred port (default 3100; auto-falls back to next free port)')
863
+ .option('--host <host>', 'Loopback host (default 127.0.0.1; non-loopback refused)')
864
+ .option('--no-auth', 'Disable authentication (local dev only)', false)
865
+ .option('--no-browser', 'Do not open the system browser on success', false)
866
+ .option('--json', 'Output JSON status (suppresses browser open)', false)
843
867
  .action(async (opts) => {
844
- const { handleConfigDoctor } = await import('./commands/config-doctor.js');
845
- await handleConfigDoctor({
868
+ const { handleConsoleOpen } = await import('./commands/console.js');
869
+ await handleConsoleOpen({
846
870
  workspace: opts.workspace,
871
+ port: opts.port,
872
+ host: opts.host,
873
+ noAuth: opts.auth === false,
874
+ noBrowser: opts.browser === false,
847
875
  json: opts.json,
848
876
  });
849
877
  });
850
878
 
879
+ // Default `pd console` → legacy launcher (backward compat)
880
+ consoleCmd.action(async (opts) => {
881
+ const { handleConsole } = await import('./commands/console.js');
882
+ await handleConsole({
883
+ workspace: opts.workspace,
884
+ port: opts.port,
885
+ noAuth: opts.auth === false,
886
+ json: opts.json,
887
+ });
888
+ });
889
+
851
890
  program.parse();