@principles/pd-cli 1.73.2 → 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.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * PD Console Launcher — seed-friendly Console startup with port detection,
3
+ * reuse, and browser opening.
4
+ *
5
+ * PRI-300 MVP UX:
6
+ * - Default port 3100; auto-falls back to next available if 3100 is busy
7
+ * - Reuses a running Console if one is already serving on the chosen port
8
+ * - Refuses non-loopback hosts (ERR-049 loopback safety)
9
+ * - Opens the system browser on success (skipped under --json)
10
+ * - Emits `reason` + `nextAction` for every failure
11
+ *
12
+ * Constraints (no daemonization, no public bind, no manual port):
13
+ * - Does NOT start a background service
14
+ * - Does NOT bind to non-loopback hosts
15
+ * - Does NOT require user-configured ports (uses 3100 then 3101..)
16
+ */
17
+ export type ConsoleStatus = 'reused' | 'started' | 'failed' | 'refused';
18
+ export interface ConsoleLaunchResult {
19
+ status: ConsoleStatus;
20
+ url: string;
21
+ port: number;
22
+ host: string;
23
+ workspaceDir: string;
24
+ reason?: string;
25
+ nextAction?: string;
26
+ /** True when an existing console was detected and reused. */
27
+ reused: boolean;
28
+ /** True when a browser should/has been opened (skipped in --json mode). */
29
+ browserOpened: boolean;
30
+ }
31
+ export interface ConsoleLaunchOptions {
32
+ workspaceDir: string;
33
+ /** Preferred port. Defaults to 3100. */
34
+ preferredPort?: number;
35
+ /** Optional override host. Must be loopback; non-loopback is refused. */
36
+ host?: string;
37
+ /** When true, the launcher does not open the browser. */
38
+ skipBrowser?: boolean;
39
+ /** Timeout in ms to wait for a freshly-spawned console to become ready. */
40
+ readyTimeoutMs?: number;
41
+ }
42
+ /** Returns true if the host resolves to a loopback address. */
43
+ export declare function isLoopbackHost(host: string): boolean;
44
+ /**
45
+ * Normalize loopback host for safe use in listen/http.request.
46
+ * - [::1] → ::1 (strip brackets; Node net/http don't want brackets in host field)
47
+ * - localhost, 127.x.x.x, ::1 → pass through
48
+ * - non-loopback → return as-is (caller must reject via isLoopbackHost first)
49
+ */
50
+ export declare function normalizeLoopbackHost(host: string): string;
51
+ /**
52
+ * Build a valid Console URL from a normalized loopback host and port.
53
+ * - IPv6 ::1 → http://[::1]:port (brackets required for valid URL)
54
+ * - IPv4/localhost → http://host:port (unchanged)
55
+ */
56
+ export declare function buildConsoleUrl(host: string, port: number): string;
57
+ /** Returns true if the port on the given host accepts a TCP connection. */
58
+ export declare function isPortInUse(host: string, port: number, timeoutMs?: number): Promise<boolean>;
59
+ export interface HealthProbeOptions {
60
+ host: string;
61
+ port: number;
62
+ timeoutMs?: number;
63
+ /** Optional auth token (PD_CONSOLE_TOKEN) for authenticated health probes. */
64
+ token?: string;
65
+ }
66
+ /** Probe a port to see if it serves a healthy PD Console. */
67
+ export declare function probeConsoleHealth(opts: HealthProbeOptions): Promise<{
68
+ healthy: boolean;
69
+ reason?: string;
70
+ }>;
71
+ /** Find the first available port in [preferred..preferred+limit]. */
72
+ export declare function findAvailablePort(host: string, preferred: number, limit?: number): Promise<number | null>;
73
+ /**
74
+ * Open the system browser. Best-effort — failures are reported but do not
75
+ * crash the launcher.
76
+ */
77
+ export declare function openBrowser(url: string): Promise<{
78
+ opened: boolean;
79
+ reason?: string;
80
+ nextAction?: string;
81
+ }>;
82
+ /**
83
+ * Orchestrates the seed-friendly Console launch:
84
+ * 1. Validate the host is loopback (refuse otherwise)
85
+ * 2. If a healthy console is already running on the preferred port, reuse it
86
+ * 3. Otherwise find the next available port starting at the preferred one
87
+ *
88
+ * NOTE: This function does NOT spawn the server. The caller (CLI command) is
89
+ * responsible for actually launching the child process; the result returned
90
+ * here tells the caller which port to bind to and whether the existing port
91
+ * already serves a console.
92
+ */
93
+ export interface OrchestratorResult {
94
+ status: ConsoleStatus;
95
+ url: string;
96
+ port: number;
97
+ host: string;
98
+ reused: boolean;
99
+ reason?: string;
100
+ nextAction?: string;
101
+ }
102
+ export interface OrchestratorInput {
103
+ workspaceDir: string;
104
+ preferredPort?: number;
105
+ host?: string;
106
+ /** Optional auth token for health probes (PD_CONSOLE_TOKEN). */
107
+ token?: string;
108
+ }
109
+ export declare function planConsoleLaunch(input: OrchestratorInput): Promise<OrchestratorResult>;
110
+ //# sourceMappingURL=console-launcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-launcher.d.ts","sourceRoot":"","sources":["../../src/services/console-launcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAOH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;AAExE,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,aAAa,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,6DAA6D;IAC7D,MAAM,EAAE,OAAO,CAAC;IAChB,2EAA2E;IAC3E,aAAa,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,oBAAoB;IACnC,YAAY,EAAE,MAAM,CAAC;IACrB,wCAAwC;IACxC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,yEAAyE;IACzE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yDAAyD;IACzD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,2EAA2E;IAC3E,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAQD,+DAA+D;AAC/D,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAKpD;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG1D;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAGlE;A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C;IACd,gEAAgE;IAChE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuE7F"}
@@ -0,0 +1,282 @@
1
+ /**
2
+ * PD Console Launcher — seed-friendly Console startup with port detection,
3
+ * reuse, and browser opening.
4
+ *
5
+ * PRI-300 MVP UX:
6
+ * - Default port 3100; auto-falls back to next available if 3100 is busy
7
+ * - Reuses a running Console if one is already serving on the chosen port
8
+ * - Refuses non-loopback hosts (ERR-049 loopback safety)
9
+ * - Opens the system browser on success (skipped under --json)
10
+ * - Emits `reason` + `nextAction` for every failure
11
+ *
12
+ * Constraints (no daemonization, no public bind, no manual port):
13
+ * - Does NOT start a background service
14
+ * - Does NOT bind to non-loopback hosts
15
+ * - Does NOT require user-configured ports (uses 3100 then 3101..)
16
+ */
17
+ import * as net from 'net';
18
+ import * as http from 'http';
19
+ const DEFAULT_PORT = 3100;
20
+ const DEFAULT_HOST = '127.0.0.1';
21
+ const PORT_FALLBACK_LIMIT = 20; // try 3100..3119 before giving up
22
+ // ─── Loopback safety (ERR-049) ──────────────────────────────────────────────
23
+ /** Returns true if the host resolves to a loopback address. */
24
+ export function isLoopbackHost(host) {
25
+ if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]')
26
+ return true;
27
+ // Reject any IPv4 host that doesn't start with 127.
28
+ if (/^127\./.test(host))
29
+ return true;
30
+ return false;
31
+ }
32
+ /**
33
+ * Normalize loopback host for safe use in listen/http.request.
34
+ * - [::1] → ::1 (strip brackets; Node net/http don't want brackets in host field)
35
+ * - localhost, 127.x.x.x, ::1 → pass through
36
+ * - non-loopback → return as-is (caller must reject via isLoopbackHost first)
37
+ */
38
+ export function normalizeLoopbackHost(host) {
39
+ if (host === '[::1]')
40
+ return '::1';
41
+ return host;
42
+ }
43
+ /**
44
+ * Build a valid Console URL from a normalized loopback host and port.
45
+ * - IPv6 ::1 → http://[::1]:port (brackets required for valid URL)
46
+ * - IPv4/localhost → http://host:port (unchanged)
47
+ */
48
+ export function buildConsoleUrl(host, port) {
49
+ if (host === '::1')
50
+ return `http://[::1]:${port}`;
51
+ return `http://${host}:${port}`;
52
+ }
53
+ // ─── Port detection (ERR-022: bounded) ───────────────────────────────────────
54
+ /** Returns true if the port on the given host accepts a TCP connection. */
55
+ export async function isPortInUse(host, port, timeoutMs = 800) {
56
+ if (Object.hasOwn(globalThis, '__mockIsPortInUse')) {
57
+ const mock = Reflect.get(globalThis, '__mockIsPortInUse');
58
+ return mock(host, port, timeoutMs);
59
+ }
60
+ return new Promise((resolve) => {
61
+ const socket = new net.Socket();
62
+ let settled = false;
63
+ const done = (inUse) => {
64
+ if (settled)
65
+ return;
66
+ settled = true;
67
+ try {
68
+ socket.destroy();
69
+ }
70
+ catch { /* ignore */ }
71
+ resolve(inUse);
72
+ };
73
+ socket.setTimeout(timeoutMs);
74
+ socket.once('connect', () => done(true));
75
+ socket.once('timeout', () => done(false));
76
+ socket.once('error', (err) => {
77
+ // ECONNREFUSED → port is free; EHOSTUNREACH/ENETUNREACH → also free.
78
+ if (err.code === 'ECONNREFUSED' || err.code === 'EHOSTUNREACH' || err.code === 'ENETUNREACH') {
79
+ done(false);
80
+ }
81
+ else {
82
+ done(false);
83
+ }
84
+ });
85
+ try {
86
+ socket.connect(port, host);
87
+ }
88
+ catch {
89
+ done(false);
90
+ }
91
+ });
92
+ }
93
+ /** Probe a port to see if it serves a healthy PD Console. */
94
+ export async function probeConsoleHealth(opts) {
95
+ const { host, port, timeoutMs = 1500, token } = opts;
96
+ if (Object.hasOwn(globalThis, '__mockProbeConsoleHealth')) {
97
+ const mock = Reflect.get(globalThis, '__mockProbeConsoleHealth');
98
+ return mock(opts);
99
+ }
100
+ return new Promise((resolve) => {
101
+ const headers = {};
102
+ if (token) {
103
+ headers.Authorization = `Bearer ${token}`;
104
+ }
105
+ const req = http.request({ host, port, path: '/api/health', method: 'GET', timeout: timeoutMs, headers }, (res) => {
106
+ // 401 means auth required — treat as unhealthy, not a generic error
107
+ if (res.statusCode === 401) {
108
+ resolve({ healthy: false, reason: 'console health endpoint returned 401 (unauthorized) — check PD_CONSOLE_TOKEN' });
109
+ return;
110
+ }
111
+ if (res.statusCode !== 200) {
112
+ resolve({ healthy: false, reason: `console health endpoint returned status ${res.statusCode ?? 'no-status'}` });
113
+ return;
114
+ }
115
+ let data = '';
116
+ res.on('data', (chunk) => {
117
+ data += chunk;
118
+ });
119
+ res.on('end', () => {
120
+ try {
121
+ const body = JSON.parse(data);
122
+ if (body && typeof body === 'object') {
123
+ const isHealthy = (Object.hasOwn(body, 'healthy') && Reflect.get(body, 'healthy') === true) ||
124
+ (Object.hasOwn(body, 'success') && Reflect.get(body, 'success') === true);
125
+ if (isHealthy) {
126
+ resolve({ healthy: true });
127
+ }
128
+ else {
129
+ resolve({ healthy: false, reason: 'console health JSON was missing healthy/success markers' });
130
+ }
131
+ }
132
+ else {
133
+ resolve({ healthy: false, reason: 'console health endpoint returned non-object JSON' });
134
+ }
135
+ }
136
+ catch (err) {
137
+ resolve({ healthy: false, reason: `failed to parse console health JSON: ${err instanceof Error ? err.message : String(err)}` });
138
+ }
139
+ });
140
+ });
141
+ req.on('timeout', () => {
142
+ req.destroy();
143
+ resolve({ healthy: false, reason: 'console health probe timed out' });
144
+ });
145
+ req.on('error', (err) => {
146
+ resolve({ healthy: false, reason: `console health probe error: ${err.message}` });
147
+ });
148
+ req.end();
149
+ });
150
+ }
151
+ /** Find the first available port in [preferred..preferred+limit]. */
152
+ export async function findAvailablePort(host, preferred, limit = PORT_FALLBACK_LIMIT) {
153
+ for (let i = 0; i < limit; i++) {
154
+ const candidate = preferred + i;
155
+ if (candidate > 65535 || candidate < 1) {
156
+ break;
157
+ }
158
+ if (!(await isPortInUse(host, candidate)))
159
+ return candidate;
160
+ }
161
+ return null;
162
+ }
163
+ // ─── Browser opener (best-effort, no throw) ──────────────────────────────────
164
+ /**
165
+ * Open the system browser. Best-effort — failures are reported but do not
166
+ * crash the launcher.
167
+ */
168
+ export async function openBrowser(url) {
169
+ const { spawn } = await import('child_process');
170
+ const { platform } = process;
171
+ let cmd;
172
+ let args;
173
+ if (platform === 'win32') {
174
+ cmd = 'cmd';
175
+ args = ['/c', 'start', '""', url];
176
+ }
177
+ else if (platform === 'darwin') {
178
+ cmd = 'open';
179
+ args = [url];
180
+ }
181
+ else {
182
+ cmd = 'xdg-open';
183
+ args = [url];
184
+ }
185
+ return new Promise((resolve) => {
186
+ try {
187
+ const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
188
+ let resolved = false;
189
+ const timer = setTimeout(() => {
190
+ if (!resolved) {
191
+ resolved = true;
192
+ child.unref();
193
+ resolve({ opened: true });
194
+ }
195
+ }, 100);
196
+ child.on('error', (err) => {
197
+ if (!resolved) {
198
+ resolved = true;
199
+ clearTimeout(timer);
200
+ resolve({
201
+ opened: false,
202
+ reason: `Failed to spawn browser process: ${err.message}`,
203
+ nextAction: `Ensure your system has '${cmd}' available in PATH or open the URL manually.`
204
+ });
205
+ }
206
+ });
207
+ }
208
+ catch (err) {
209
+ resolve({
210
+ opened: false,
211
+ reason: err instanceof Error ? err.message : String(err),
212
+ nextAction: 'Ensure child_process is available or open the URL manually.'
213
+ });
214
+ }
215
+ });
216
+ }
217
+ export async function planConsoleLaunch(input) {
218
+ if (Object.hasOwn(globalThis, '__mockPlanConsoleLaunch')) {
219
+ const mock = Reflect.get(globalThis, '__mockPlanConsoleLaunch');
220
+ return mock(input);
221
+ }
222
+ const { token } = input;
223
+ const rawHost = input.host ?? DEFAULT_HOST;
224
+ const host = normalizeLoopbackHost(rawHost);
225
+ const preferredPort = input.preferredPort ?? DEFAULT_PORT;
226
+ if (!isLoopbackHost(host)) {
227
+ return {
228
+ status: 'refused',
229
+ url: '',
230
+ port: preferredPort,
231
+ host,
232
+ reused: false,
233
+ reason: `Non-loopback host refused: '${host}'. Only 127.0.0.1, ::1, and 'localhost' are allowed.`,
234
+ nextAction: 'Use the default loopback host (do not pass --host 0.0.0.0 or any LAN address).',
235
+ };
236
+ }
237
+ // Step 1: Is there already a healthy console on the preferred port?
238
+ const health = await probeConsoleHealth({ host, port: preferredPort, token });
239
+ if (health.healthy) {
240
+ return {
241
+ status: 'reused',
242
+ url: buildConsoleUrl(host, preferredPort),
243
+ port: preferredPort,
244
+ host,
245
+ reused: true,
246
+ };
247
+ }
248
+ // Step 2: Is the preferred port simply occupied by something else?
249
+ const preferredInUse = await isPortInUse(host, preferredPort);
250
+ if (preferredInUse) {
251
+ // Try to find a free port in the fallback range.
252
+ const freePort = await findAvailablePort(host, preferredPort + 1, PORT_FALLBACK_LIMIT - 1);
253
+ if (freePort === null) {
254
+ return {
255
+ status: 'failed',
256
+ url: '',
257
+ port: preferredPort,
258
+ host,
259
+ reused: false,
260
+ reason: `No available port in ${preferredPort + 1}..${preferredPort + PORT_FALLBACK_LIMIT}`,
261
+ nextAction: `Stop the process holding port ${preferredPort} (e.g., another Console instance), or free a port in the fallback range.`,
262
+ };
263
+ }
264
+ return {
265
+ status: 'started',
266
+ url: buildConsoleUrl(host, freePort),
267
+ port: freePort,
268
+ host,
269
+ reused: false,
270
+ reason: `Preferred port ${preferredPort} was busy; using ${freePort} instead`,
271
+ };
272
+ }
273
+ // Preferred port is free → bind there.
274
+ return {
275
+ status: 'started',
276
+ url: buildConsoleUrl(host, preferredPort),
277
+ port: preferredPort,
278
+ host,
279
+ reused: false,
280
+ };
281
+ }
282
+ //# sourceMappingURL=console-launcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-launcher.js","sourceRoot":"","sources":["../../src/services/console-launcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,GAAG,MAAM,KAAK,CAAC;AAC3B,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAgC7B,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,YAAY,GAAG,WAAW,CAAC;AAC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;QACjD,MAAM,QAAQ,GAAG,MAAM,iBAAiB,CAAC,IAAI,EAAE,aAAa,GAAG,CAAC,EAAE,mBAAmB,GAAG,CAAC,CAAC,CAAC;QAC3F,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACtB,OAAO;gBACL,MAAM,EAAE,QAAQ;gBAChB,GAAG,EAAE,EAAE;gBACP,IAAI,EAAE,aAAa;gBACnB,IAAI;gBACJ,MAAM,EAAE,KAAK;gBACb,MAAM,EAAE,wBAAwB,aAAa,GAAG,CAAC,KAAK,aAAa,GAAG,mBAAmB,EAAE;gBAC3F,UAAU,EAAE,iCAAiC,aAAa,0EAA0E;aACrI,CAAC;QACJ,CAAC;QACD,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,GAAG,EAAE,eAAe,CAAC,IAAI,EAAE,QAAQ,CAAC;YACpC,IAAI,EAAE,QAAQ;YACd,IAAI;YACJ,MAAM,EAAE,KAAK;YACb,MAAM,EAAE,kBAAkB,aAAa,oBAAoB,QAAQ,UAAU;SAC9E,CAAC;IACJ,CAAC;IAED,uCAAuC;IACvC,OAAO;QACL,MAAM,EAAE,SAAS;QACjB,GAAG,EAAE,eAAe,CAAC,IAAI,EAAE,aAAa,CAAC;QACzC,IAAI,EAAE,aAAa;QACnB,IAAI;QACJ,MAAM,EAAE,KAAK;KACd,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/pd-cli",
3
- "version": "1.73.2",
3
+ "version": "1.75.0",
4
4
  "description": "PD CLI — Pain recording, sample management, and evolution tasks",
5
5
  "type": "module",
6
6
  "bin": {
@@ -15,9 +15,11 @@
15
15
  "dependencies": {
16
16
  "@principles/core": "^1.73.0",
17
17
  "commander": "^12.0.0",
18
- "js-yaml": "^4.1.1"
18
+ "js-yaml": "^4.1.1",
19
+ "better-sqlite3": "^12.9.0"
19
20
  },
20
21
  "devDependencies": {
22
+ "@types/better-sqlite3": "^7.6.13",
21
23
  "@types/js-yaml": "^4.0.9",
22
24
  "@types/node": "^25.6.2",
23
25
  "typescript": "^6.0.3",
@@ -0,0 +1,158 @@
1
+ /**
2
+ * pd config doctor — Discover and explain PD / OpenClaw configuration state.
3
+ *
4
+ * PRI-299 MVP UX:
5
+ * - Reports workspace + OpenClaw config paths and existence
6
+ * - Lists effective feature flags and enabled MVP channels
7
+ * - Classifies provider/model/auth connectivity (healthy, auth_missing, rate_limit, etc.)
8
+ * - Emits `reason` + `nextActions` for failures
9
+ * - NEVER leaks tokens, env var values, or raw config bytes
10
+ *
11
+ * Usage:
12
+ * pd config doctor [--workspace <path>] [--json]
13
+ */
14
+
15
+ import * as path from 'path';
16
+ import { resolveWorkspaceDir } from '../resolve-workspace.js';
17
+ import { buildDoctorOutput, type DoctorOutput } from '../services/config-doctor.js';
18
+
19
+ interface DoctorOptions {
20
+ workspace?: string;
21
+ json?: boolean;
22
+ }
23
+
24
+ function formatTextOutput(output: DoctorOutput): string {
25
+ const lines: string[] = [];
26
+ const statusIcon = output.status === 'ok' ? '✓' : output.status === 'degraded' ? '⚠' : '✗';
27
+
28
+ lines.push('PD Config Doctor');
29
+ lines.push(`status: ${statusIcon} ${output.status.toUpperCase()}`);
30
+ lines.push(`workspace: ${output.workspaceDir}`);
31
+ lines.push('');
32
+
33
+ lines.push('PD config paths:');
34
+ for (const [k, v] of Object.entries(output.pdConfigPaths)) {
35
+ const exists = v.exists ? '[exists]' : '[missing]';
36
+ lines.push(` ${k.padEnd(16)} ${exists.padEnd(10)} ${v.path}`);
37
+ }
38
+ lines.push('');
39
+
40
+ lines.push('OpenClaw paths:');
41
+ for (const [k, v] of Object.entries(output.openclawConfigPaths)) {
42
+ const exists = v.exists ? '[exists]' : '[missing]';
43
+ lines.push(` ${k.padEnd(16)} ${exists.padEnd(10)} ${v.path}`);
44
+ }
45
+ lines.push('');
46
+
47
+ lines.push(`Feature flags: source=${output.featureFlags.source}`);
48
+ lines.push(` enabled MVP channels: ${output.featureFlags.enabledMvpChannels.length === 0 ? '(none)' : output.featureFlags.enabledMvpChannels.join(', ')}`);
49
+ if (output.featureFlags.disabledFlags.length > 0) {
50
+ lines.push(` disabled: ${output.featureFlags.disabledFlags.join(', ')}`);
51
+ }
52
+ if (output.featureFlags.warnings.length > 0) {
53
+ lines.push(` warnings: ${output.featureFlags.warnings.length}`);
54
+ }
55
+ lines.push('');
56
+
57
+ lines.push('Provider health:');
58
+ if (output.providerHealth.length === 0) {
59
+ lines.push(' (no providers discovered)');
60
+ } else {
61
+ for (const p of output.providerHealth) {
62
+ const cls = p.classification.toUpperCase();
63
+ const provider = p.provider ?? '(unset)';
64
+ const model = p.model ?? '(unset)';
65
+ const apiKeyEnv = p.apiKeyEnv ?? '(unset)';
66
+ const apiKeyState = p.apiKeyPresent ? 'present' : 'absent';
67
+ lines.push(` [${cls}] ${provider} / ${model}`);
68
+ lines.push(` apiKeyEnv: ${apiKeyEnv} (${apiKeyState})`);
69
+ lines.push(` source: ${p.source}`);
70
+ lines.push(` reason: ${p.reason}`);
71
+ lines.push(` nextAction: ${p.nextAction}`);
72
+ }
73
+ }
74
+ lines.push('');
75
+
76
+ lines.push('Internal agents:');
77
+ if (output.internalAgents && output.internalAgents.correctionObserver) {
78
+ const co = output.internalAgents.correctionObserver;
79
+ const coStatus = co.status.toUpperCase();
80
+ const coProvider = co.provider ?? '(unset)';
81
+ const coModel = co.model ?? '(unset)';
82
+ const coApiKeyEnv = co.apiKeyEnv ?? '(unset)';
83
+ const coApiKeyState = co.apiKeyPresent ? 'present' : 'absent';
84
+ lines.push(` correctionObserver: [${coStatus}]`);
85
+ lines.push(` enabled: ${co.enabled}`);
86
+ lines.push(` flagSource: ${co.flagSource}`);
87
+ lines.push(` configSource:${co.configSource}`);
88
+ if (co.provider || co.model) {
89
+ lines.push(` provider: ${coProvider} / ${coModel}`);
90
+ }
91
+ lines.push(` apiKeyEnv: ${coApiKeyEnv} (${coApiKeyState})`);
92
+ lines.push(` reason: ${co.reason}`);
93
+ lines.push(` nextAction: ${co.nextAction}`);
94
+ } else {
95
+ lines.push(' (no internal agents diagnosed)');
96
+ }
97
+ lines.push('');
98
+
99
+ if (output.warnings.length > 0) {
100
+ lines.push('Warnings:');
101
+ for (const w of output.warnings) {
102
+ lines.push(` [!] ${w}`);
103
+ }
104
+ lines.push('');
105
+ }
106
+
107
+ if (output.reason) {
108
+ lines.push(`Reason: ${output.reason}`);
109
+ lines.push('');
110
+ }
111
+
112
+ if (output.nextActions.length > 0) {
113
+ lines.push('Next actions:');
114
+ for (const a of output.nextActions) {
115
+ lines.push(` → ${a}`);
116
+ }
117
+ }
118
+
119
+ return lines.join('\n');
120
+ }
121
+
122
+ export async function handleConfigDoctor(opts: DoctorOptions): Promise<void> {
123
+ let workspaceDir: string;
124
+ try {
125
+ workspaceDir = opts.workspace ? path.resolve(opts.workspace) : resolveWorkspaceDir();
126
+ } catch (err) {
127
+ const message = err instanceof Error ? err.message : String(err);
128
+ const output = {
129
+ status: 'failed' as const,
130
+ reason: 'workspace_resolution_failed',
131
+ message,
132
+ nextActions: ['Pass --workspace <path> explicitly, or set PD_WORKSPACE_DIR environment variable'],
133
+ };
134
+ if (opts.json) {
135
+ console.log(JSON.stringify(output, null, 2));
136
+ } else {
137
+ console.error(`error: ${message}`);
138
+ }
139
+ process.exit(1);
140
+ return;
141
+ }
142
+
143
+ const output = await buildDoctorOutput({ workspaceDir });
144
+
145
+ if (opts.json) {
146
+ // JSON mode: single parseable object on stdout.
147
+ console.log(JSON.stringify(output, null, 2));
148
+ } else {
149
+ console.log(formatTextOutput(output));
150
+ }
151
+
152
+ if (output.status === 'failed') {
153
+ process.exitCode = 1;
154
+ } else if (output.status === 'degraded') {
155
+ // degraded: do not exit 1, but emit a hint to stderr for the operator.
156
+ console.error(`\nNote: doctor status is degraded. Inspect warnings + nextActions.`);
157
+ }
158
+ }