@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.
@@ -0,0 +1,373 @@
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
+
18
+ import * as net from 'net';
19
+ import * as http from 'http';
20
+
21
+ // ─── Public types ────────────────────────────────────────────────────────────
22
+
23
+ export type ConsoleStatus = 'reused' | 'started' | 'failed' | 'refused';
24
+
25
+ export interface ConsoleLaunchResult {
26
+ status: ConsoleStatus;
27
+ url: string;
28
+ port: number;
29
+ host: string;
30
+ workspaceDir: string;
31
+ reason?: string;
32
+ nextAction?: string;
33
+ /** True when an existing console was detected and reused. */
34
+ reused: boolean;
35
+ /** True when a browser should/has been opened (skipped in --json mode). */
36
+ browserOpened: boolean;
37
+ }
38
+
39
+ export interface ConsoleLaunchOptions {
40
+ workspaceDir: string;
41
+ /** Preferred port. Defaults to 3100. */
42
+ preferredPort?: number;
43
+ /** Optional override host. Must be loopback; non-loopback is refused. */
44
+ host?: string;
45
+ /** When true, the launcher does not open the browser. */
46
+ skipBrowser?: boolean;
47
+ /** Timeout in ms to wait for a freshly-spawned console to become ready. */
48
+ readyTimeoutMs?: number;
49
+ }
50
+
51
+ const DEFAULT_PORT = 3100;
52
+ const DEFAULT_HOST = '127.0.0.1';
53
+ const PORT_FALLBACK_LIMIT = 20; // try 3100..3119 before giving up
54
+
55
+ // ─── Loopback safety (ERR-049) ──────────────────────────────────────────────
56
+
57
+ /** Returns true if the host resolves to a loopback address. */
58
+ export function isLoopbackHost(host: string): boolean {
59
+ if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]') return true;
60
+ // Reject any IPv4 host that doesn't start with 127.
61
+ if (/^127\./.test(host)) return true;
62
+ return false;
63
+ }
64
+
65
+ /**
66
+ * Normalize loopback host for safe use in listen/http.request.
67
+ * - [::1] → ::1 (strip brackets; Node net/http don't want brackets in host field)
68
+ * - localhost, 127.x.x.x, ::1 → pass through
69
+ * - non-loopback → return as-is (caller must reject via isLoopbackHost first)
70
+ */
71
+ export function normalizeLoopbackHost(host: string): string {
72
+ if (host === '[::1]') return '::1';
73
+ return host;
74
+ }
75
+
76
+ /**
77
+ * Build a valid Console URL from a normalized loopback host and port.
78
+ * - IPv6 ::1 → http://[::1]:port (brackets required for valid URL)
79
+ * - IPv4/localhost → http://host:port (unchanged)
80
+ */
81
+ export function buildConsoleUrl(host: string, port: number): string {
82
+ if (host === '::1') return `http://[::1]:${port}`;
83
+ return `http://${host}:${port}`;
84
+ }
85
+
86
+ // ─── Port detection (ERR-022: bounded) ───────────────────────────────────────
87
+
88
+ /** Returns true if the port on the given host accepts a TCP connection. */
89
+ export async function isPortInUse(host: string, port: number, timeoutMs = 800): Promise<boolean> {
90
+ if (Object.hasOwn(globalThis, '__mockIsPortInUse')) {
91
+ const mock = Reflect.get(globalThis, '__mockIsPortInUse') as (
92
+ h: string,
93
+ p: number,
94
+ t?: number
95
+ ) => Promise<boolean>;
96
+ return mock(host, port, timeoutMs);
97
+ }
98
+ return new Promise((resolve) => {
99
+ const socket = new net.Socket();
100
+ let settled = false;
101
+ const done = (inUse: boolean) => {
102
+ if (settled) return;
103
+ settled = true;
104
+ try { socket.destroy(); } catch { /* ignore */ }
105
+ resolve(inUse);
106
+ };
107
+ socket.setTimeout(timeoutMs);
108
+ socket.once('connect', () => done(true));
109
+ socket.once('timeout', () => done(false));
110
+ socket.once('error', (err: { code?: string; message?: string }) => {
111
+ // ECONNREFUSED → port is free; EHOSTUNREACH/ENETUNREACH → also free.
112
+ if (err.code === 'ECONNREFUSED' || err.code === 'EHOSTUNREACH' || err.code === 'ENETUNREACH') {
113
+ done(false);
114
+ } else {
115
+ done(false);
116
+ }
117
+ });
118
+ try {
119
+ socket.connect(port, host);
120
+ } catch {
121
+ done(false);
122
+ }
123
+ });
124
+ }
125
+
126
+ export interface HealthProbeOptions {
127
+ host: string;
128
+ port: number;
129
+ timeoutMs?: number;
130
+ /** Optional auth token (PD_CONSOLE_TOKEN) for authenticated health probes. */
131
+ token?: string;
132
+ }
133
+
134
+ /** Probe a port to see if it serves a healthy PD Console. */
135
+ export async function probeConsoleHealth(opts: HealthProbeOptions): Promise<{ healthy: boolean; reason?: string }> {
136
+ const { host, port, timeoutMs = 1500, token } = opts;
137
+
138
+ if (Object.hasOwn(globalThis, '__mockProbeConsoleHealth')) {
139
+ const mock = Reflect.get(globalThis, '__mockProbeConsoleHealth') as (
140
+ o: HealthProbeOptions
141
+ ) => Promise<{ healthy: boolean; reason?: string }>;
142
+ return mock(opts);
143
+ }
144
+ return new Promise((resolve) => {
145
+ const headers: Record<string, string> = {};
146
+ if (token) {
147
+ headers.Authorization = `Bearer ${token}`;
148
+ }
149
+ const req = http.request(
150
+ { host, port, path: '/api/health', method: 'GET', timeout: timeoutMs, headers },
151
+ (res) => {
152
+ // 401 means auth required — treat as unhealthy, not a generic error
153
+ if (res.statusCode === 401) {
154
+ resolve({ healthy: false, reason: 'console health endpoint returned 401 (unauthorized) — check PD_CONSOLE_TOKEN' });
155
+ return;
156
+ }
157
+ if (res.statusCode !== 200) {
158
+ resolve({ healthy: false, reason: `console health endpoint returned status ${res.statusCode ?? 'no-status'}` });
159
+ return;
160
+ }
161
+ let data = '';
162
+ res.on('data', (chunk: string) => {
163
+ data += chunk;
164
+ });
165
+ res.on('end', () => {
166
+ try {
167
+ const body = JSON.parse(data) as unknown;
168
+ if (body && typeof body === 'object') {
169
+ const isHealthy =
170
+ (Object.hasOwn(body, 'healthy') && Reflect.get(body, 'healthy') === true) ||
171
+ (Object.hasOwn(body, 'success') && Reflect.get(body, 'success') === true);
172
+ if (isHealthy) {
173
+ resolve({ healthy: true });
174
+ } else {
175
+ resolve({ healthy: false, reason: 'console health JSON was missing healthy/success markers' });
176
+ }
177
+ } else {
178
+ resolve({ healthy: false, reason: 'console health endpoint returned non-object JSON' });
179
+ }
180
+ } catch (err) {
181
+ resolve({ healthy: false, reason: `failed to parse console health JSON: ${err instanceof Error ? err.message : String(err)}` });
182
+ }
183
+ });
184
+ },
185
+ );
186
+ req.on('timeout', () => {
187
+ req.destroy();
188
+ resolve({ healthy: false, reason: 'console health probe timed out' });
189
+ });
190
+ req.on('error', (err: Error) => {
191
+ resolve({ healthy: false, reason: `console health probe error: ${err.message}` });
192
+ });
193
+ req.end();
194
+ });
195
+ }
196
+
197
+ /** Find the first available port in [preferred..preferred+limit]. */
198
+ export async function findAvailablePort(
199
+ host: string,
200
+ preferred: number,
201
+ limit = PORT_FALLBACK_LIMIT,
202
+ ): Promise<number | null> {
203
+ for (let i = 0; i < limit; i++) {
204
+ const candidate = preferred + i;
205
+ if (candidate > 65535 || candidate < 1) {
206
+ break;
207
+ }
208
+ if (!(await isPortInUse(host, candidate))) return candidate;
209
+ }
210
+ return null;
211
+ }
212
+
213
+ // ─── Browser opener (best-effort, no throw) ──────────────────────────────────
214
+
215
+ /**
216
+ * Open the system browser. Best-effort — failures are reported but do not
217
+ * crash the launcher.
218
+ */
219
+ export async function openBrowser(url: string): Promise<{ opened: boolean; reason?: string; nextAction?: string }> {
220
+ const { spawn } = await import('child_process');
221
+ const { platform } = process;
222
+
223
+ let cmd: string;
224
+ let args: string[];
225
+
226
+ if (platform === 'win32') {
227
+ cmd = 'cmd';
228
+ args = ['/c', 'start', '""', url];
229
+ } else if (platform === 'darwin') {
230
+ cmd = 'open';
231
+ args = [url];
232
+ } else {
233
+ cmd = 'xdg-open';
234
+ args = [url];
235
+ }
236
+
237
+ return new Promise((resolve) => {
238
+ try {
239
+ const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
240
+
241
+ let resolved = false;
242
+ const timer = setTimeout(() => {
243
+ if (!resolved) {
244
+ resolved = true;
245
+ child.unref();
246
+ resolve({ opened: true });
247
+ }
248
+ }, 100);
249
+
250
+ child.on('error', (err) => {
251
+ if (!resolved) {
252
+ resolved = true;
253
+ clearTimeout(timer);
254
+ resolve({
255
+ opened: false,
256
+ reason: `Failed to spawn browser process: ${err.message}`,
257
+ nextAction: `Ensure your system has '${cmd}' available in PATH or open the URL manually.`
258
+ });
259
+ }
260
+ });
261
+ } catch (err) {
262
+ resolve({
263
+ opened: false,
264
+ reason: err instanceof Error ? err.message : String(err),
265
+ nextAction: 'Ensure child_process is available or open the URL manually.'
266
+ });
267
+ }
268
+ });
269
+ }
270
+
271
+ // ─── Main launcher orchestrator ──────────────────────────────────────────────
272
+
273
+ /**
274
+ * Orchestrates the seed-friendly Console launch:
275
+ * 1. Validate the host is loopback (refuse otherwise)
276
+ * 2. If a healthy console is already running on the preferred port, reuse it
277
+ * 3. Otherwise find the next available port starting at the preferred one
278
+ *
279
+ * NOTE: This function does NOT spawn the server. The caller (CLI command) is
280
+ * responsible for actually launching the child process; the result returned
281
+ * here tells the caller which port to bind to and whether the existing port
282
+ * already serves a console.
283
+ */
284
+ export interface OrchestratorResult {
285
+ status: ConsoleStatus;
286
+ url: string;
287
+ port: number;
288
+ host: string;
289
+ reused: boolean;
290
+ reason?: string;
291
+ nextAction?: string;
292
+ }
293
+
294
+ export interface OrchestratorInput {
295
+ workspaceDir: string;
296
+ preferredPort?: number;
297
+ host?: string;
298
+ /** Optional auth token for health probes (PD_CONSOLE_TOKEN). */
299
+ token?: string;
300
+ }
301
+
302
+ export async function planConsoleLaunch(input: OrchestratorInput): Promise<OrchestratorResult> {
303
+ if (Object.hasOwn(globalThis, '__mockPlanConsoleLaunch')) {
304
+ const mock = Reflect.get(globalThis, '__mockPlanConsoleLaunch') as (
305
+ i: OrchestratorInput
306
+ ) => Promise<OrchestratorResult>;
307
+ return mock(input);
308
+ }
309
+ const { token } = input;
310
+ const rawHost = input.host ?? DEFAULT_HOST;
311
+ const host = normalizeLoopbackHost(rawHost);
312
+ const preferredPort = input.preferredPort ?? DEFAULT_PORT;
313
+
314
+ if (!isLoopbackHost(host)) {
315
+ return {
316
+ status: 'refused',
317
+ url: '',
318
+ port: preferredPort,
319
+ host,
320
+ reused: false,
321
+ reason: `Non-loopback host refused: '${host}'. Only 127.0.0.1, ::1, and 'localhost' are allowed.`,
322
+ nextAction: 'Use the default loopback host (do not pass --host 0.0.0.0 or any LAN address).',
323
+ };
324
+ }
325
+
326
+ // Step 1: Is there already a healthy console on the preferred port?
327
+ const health = await probeConsoleHealth({ host, port: preferredPort, token });
328
+ if (health.healthy) {
329
+ return {
330
+ status: 'reused',
331
+ url: buildConsoleUrl(host, preferredPort),
332
+ port: preferredPort,
333
+ host,
334
+ reused: true,
335
+ };
336
+ }
337
+
338
+ // Step 2: Is the preferred port simply occupied by something else?
339
+ const preferredInUse = await isPortInUse(host, preferredPort);
340
+
341
+ if (preferredInUse) {
342
+ // Try to find a free port in the fallback range.
343
+ const freePort = await findAvailablePort(host, preferredPort + 1, PORT_FALLBACK_LIMIT - 1);
344
+ if (freePort === null) {
345
+ return {
346
+ status: 'failed',
347
+ url: '',
348
+ port: preferredPort,
349
+ host,
350
+ reused: false,
351
+ reason: `No available port in ${preferredPort + 1}..${preferredPort + PORT_FALLBACK_LIMIT}`,
352
+ nextAction: `Stop the process holding port ${preferredPort} (e.g., another Console instance), or free a port in the fallback range.`,
353
+ };
354
+ }
355
+ return {
356
+ status: 'started',
357
+ url: buildConsoleUrl(host, freePort),
358
+ port: freePort,
359
+ host,
360
+ reused: false,
361
+ reason: `Preferred port ${preferredPort} was busy; using ${freePort} instead`,
362
+ };
363
+ }
364
+
365
+ // Preferred port is free → bind there.
366
+ return {
367
+ status: 'started',
368
+ url: buildConsoleUrl(host, preferredPort),
369
+ port: preferredPort,
370
+ host,
371
+ reused: false,
372
+ };
373
+ }