@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.
- package/dist/commands/config-doctor.d.ts +3 -6
- package/dist/commands/config-doctor.d.ts.map +1 -1
- package/dist/commands/config-doctor.js +30 -31
- package/dist/commands/config-doctor.js.map +1 -1
- package/dist/commands/console.d.ts +18 -0
- package/dist/commands/console.d.ts.map +1 -1
- package/dist/commands/console.js +439 -0
- package/dist/commands/console.js.map +1 -1
- package/dist/commands/runtime-features.d.ts +23 -8
- package/dist/commands/runtime-features.d.ts.map +1 -1
- package/dist/commands/runtime-features.js +72 -31
- package/dist/commands/runtime-features.js.map +1 -1
- package/dist/index.js +51 -15
- package/dist/index.js.map +1 -1
- package/dist/services/config-doctor.d.ts +26 -66
- package/dist/services/config-doctor.d.ts.map +1 -1
- package/dist/services/config-doctor.js +197 -374
- package/dist/services/config-doctor.js.map +1 -1
- package/dist/services/console-launcher.d.ts +110 -0
- package/dist/services/console-launcher.d.ts.map +1 -0
- package/dist/services/console-launcher.js +282 -0
- package/dist/services/console-launcher.js.map +1 -0
- package/dist/services/pd-config-loader.d.ts +64 -0
- package/dist/services/pd-config-loader.d.ts.map +1 -0
- package/dist/services/pd-config-loader.js +156 -0
- package/dist/services/pd-config-loader.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/config-doctor.ts +30 -30
- package/src/commands/console.ts +445 -1
- package/src/commands/runtime-features.ts +98 -44
- package/src/index.ts +55 -16
- package/src/services/config-doctor.ts +236 -425
- package/src/services/console-launcher.ts +373 -0
- package/src/services/pd-config-loader.ts +213 -0
- package/tests/commands/config-doctor.test.ts +207 -506
- package/tests/commands/console-open.test.ts +773 -0
- package/tests/commands/runtime-features.test.ts +220 -85
- package/tests/services/pd-config-loader.test.ts +479 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PD Config Loader — PRI-305
|
|
3
|
+
*
|
|
4
|
+
* I/O boundary: reads `.pd/config.yaml`, validates via core, computes effective config.
|
|
5
|
+
* This replaces the old `feature-flag-loader.ts` and `workflows.yaml` reading
|
|
6
|
+
* for CLI production paths.
|
|
7
|
+
*
|
|
8
|
+
* ADR-0016: PD owns exactly one user config file.
|
|
9
|
+
* - Missing config → defaults with nextAction
|
|
10
|
+
* - Malformed config → fail loud with errors and nextAction
|
|
11
|
+
* - No secrets in output
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as fs from 'fs';
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import yaml from 'js-yaml';
|
|
17
|
+
import {
|
|
18
|
+
validatePdConfig,
|
|
19
|
+
computeEffectivePdConfig,
|
|
20
|
+
computeFeatureFlagsFromConfig,
|
|
21
|
+
redactPdConfig,
|
|
22
|
+
} from '@principles/core/runtime-v2';
|
|
23
|
+
import type {
|
|
24
|
+
EffectivePdConfig,
|
|
25
|
+
PdConfigValidationResult,
|
|
26
|
+
RedactedPdConfigSummary,
|
|
27
|
+
FeatureFlagsResult,
|
|
28
|
+
} from '@principles/core/runtime-v2';
|
|
29
|
+
|
|
30
|
+
// ── Constants ────────────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
export const PD_CONFIG_DIR = '.pd';
|
|
33
|
+
export const PD_CONFIG_FILENAME = 'config.yaml';
|
|
34
|
+
|
|
35
|
+
// ── Types ────────────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
export type ConfigSource = 'defaults' | 'user_config' | 'malformed';
|
|
38
|
+
|
|
39
|
+
export interface PdConfigLoadResultOk {
|
|
40
|
+
ok: true;
|
|
41
|
+
effective: EffectivePdConfig;
|
|
42
|
+
source: ConfigSource;
|
|
43
|
+
configPath: string;
|
|
44
|
+
/** Warnings from config resolution (not errors) */
|
|
45
|
+
warnings: string[];
|
|
46
|
+
/** If legacy files were detected */
|
|
47
|
+
legacyFilesDetected: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface PdConfigLoadResultErr {
|
|
51
|
+
ok: false;
|
|
52
|
+
source: 'malformed';
|
|
53
|
+
configPath: string;
|
|
54
|
+
errors: { path: string; reason: string; nextAction: string }[];
|
|
55
|
+
/** Fallback defaults are still available */
|
|
56
|
+
defaults: EffectivePdConfig;
|
|
57
|
+
/** Warnings from config resolution */
|
|
58
|
+
warnings: string[];
|
|
59
|
+
legacyFilesDetected: string[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type PdConfigLoadResult = PdConfigLoadResultOk | PdConfigLoadResultErr;
|
|
63
|
+
|
|
64
|
+
// ── Config Path ──────────────────────────────────────────────────────────────
|
|
65
|
+
|
|
66
|
+
export function getPdConfigPath(workspaceDir: string): string {
|
|
67
|
+
return path.join(workspaceDir, PD_CONFIG_DIR, PD_CONFIG_FILENAME);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ── Legacy File Detection ────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
function detectLegacyFiles(workspaceDir: string): string[] {
|
|
73
|
+
const detected: string[] = [];
|
|
74
|
+
const legacyPaths = [
|
|
75
|
+
path.join(workspaceDir, PD_CONFIG_DIR, 'feature-flags.yaml'),
|
|
76
|
+
path.join(workspaceDir, '.state', 'workflows.yaml'),
|
|
77
|
+
];
|
|
78
|
+
for (const p of legacyPaths) {
|
|
79
|
+
if (fs.existsSync(p)) {
|
|
80
|
+
detected.push(p);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return detected;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ── Load PD Config ───────────────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Load and validate `.pd/config.yaml` from the workspace.
|
|
90
|
+
*
|
|
91
|
+
* - Missing file → returns defaults with source='defaults'
|
|
92
|
+
* - Malformed file → returns error result with defaults fallback
|
|
93
|
+
* - Valid file → returns effective config with source='user_config'
|
|
94
|
+
*
|
|
95
|
+
* Never throws on malformed input. Always provides a usable fallback.
|
|
96
|
+
*/
|
|
97
|
+
export function loadPdConfig(workspaceDir: string): PdConfigLoadResult {
|
|
98
|
+
const configPath = getPdConfigPath(workspaceDir);
|
|
99
|
+
const legacyFilesDetected = detectLegacyFiles(workspaceDir);
|
|
100
|
+
|
|
101
|
+
// 1) Config file missing → use defaults
|
|
102
|
+
if (!fs.existsSync(configPath)) {
|
|
103
|
+
const effective = computeEffectivePdConfig(null);
|
|
104
|
+
return {
|
|
105
|
+
ok: true,
|
|
106
|
+
effective,
|
|
107
|
+
source: 'defaults',
|
|
108
|
+
configPath,
|
|
109
|
+
warnings: [
|
|
110
|
+
...effective.warnings,
|
|
111
|
+
...(legacyFilesDetected.length > 0
|
|
112
|
+
? [`Legacy config files detected (${legacyFilesDetected.length}): ${legacyFilesDetected.join(', ')}. PD now uses .pd/config.yaml.`]
|
|
113
|
+
: []),
|
|
114
|
+
],
|
|
115
|
+
legacyFilesDetected,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 2) Read the file
|
|
120
|
+
let raw: string;
|
|
121
|
+
try {
|
|
122
|
+
raw = fs.readFileSync(configPath, 'utf8');
|
|
123
|
+
} catch (err) {
|
|
124
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
125
|
+
const effective = computeEffectivePdConfig(null);
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
source: 'malformed',
|
|
129
|
+
configPath,
|
|
130
|
+
errors: [{ path: '', reason: `Failed to read .pd/config.yaml: ${message}`, nextAction: 'Check file permissions for .pd/config.yaml' }],
|
|
131
|
+
warnings: [],
|
|
132
|
+
defaults: effective,
|
|
133
|
+
legacyFilesDetected,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 3) Parse YAML — treat as unknown (ERR-001)
|
|
138
|
+
let parsed: unknown;
|
|
139
|
+
try {
|
|
140
|
+
parsed = yaml.load(raw, { schema: yaml.JSON_SCHEMA });
|
|
141
|
+
} catch (err) {
|
|
142
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
143
|
+
const effective = computeEffectivePdConfig(null);
|
|
144
|
+
return {
|
|
145
|
+
ok: false,
|
|
146
|
+
source: 'malformed',
|
|
147
|
+
configPath,
|
|
148
|
+
errors: [{ path: '', reason: `YAML parse error in .pd/config.yaml: ${message}`, nextAction: 'Fix YAML syntax in .pd/config.yaml' }],
|
|
149
|
+
warnings: [],
|
|
150
|
+
defaults: effective,
|
|
151
|
+
legacyFilesDetected,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 4) Validate via core (ERR-001, ERR-005: no `as` bypasses)
|
|
156
|
+
const validationResult: PdConfigValidationResult = validatePdConfig(parsed);
|
|
157
|
+
|
|
158
|
+
if (!validationResult.ok) {
|
|
159
|
+
const effective = computeEffectivePdConfig(null);
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
source: 'malformed',
|
|
163
|
+
configPath,
|
|
164
|
+
errors: validationResult.errors.map(e => ({
|
|
165
|
+
path: e.path,
|
|
166
|
+
reason: e.reason,
|
|
167
|
+
nextAction: e.nextAction,
|
|
168
|
+
})),
|
|
169
|
+
warnings: [],
|
|
170
|
+
defaults: effective,
|
|
171
|
+
legacyFilesDetected,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 5) Compute effective config
|
|
176
|
+
const effective = computeEffectivePdConfig(validationResult.value);
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
ok: true,
|
|
180
|
+
effective,
|
|
181
|
+
source: 'user_config',
|
|
182
|
+
configPath,
|
|
183
|
+
warnings: [
|
|
184
|
+
...effective.warnings,
|
|
185
|
+
...(legacyFilesDetected.length > 0
|
|
186
|
+
? [`Legacy config files detected (${legacyFilesDetected.length}): ${legacyFilesDetected.join(', ')}. PD now uses .pd/config.yaml.`]
|
|
187
|
+
: []),
|
|
188
|
+
],
|
|
189
|
+
legacyFilesDetected,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ── Feature Flags from Config ────────────────────────────────────────────────
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Compute feature flags from the loaded PD config.
|
|
197
|
+
* Works with both ok and error results (uses defaults for errors).
|
|
198
|
+
*/
|
|
199
|
+
export function computeFlagsFromLoadResult(result: PdConfigLoadResult): FeatureFlagsResult {
|
|
200
|
+
const effective = result.ok ? result.effective : result.defaults;
|
|
201
|
+
return computeFeatureFlagsFromConfig(effective);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ── Redacted Summary from Config ─────────────────────────────────────────────
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Produce a redacted summary of the PD config for CLI/Console display.
|
|
208
|
+
* Never includes token/API key values or raw provider objects.
|
|
209
|
+
*/
|
|
210
|
+
export function redactLoadResult(result: PdConfigLoadResult): RedactedPdConfigSummary {
|
|
211
|
+
const effective = result.ok ? result.effective : result.defaults;
|
|
212
|
+
return redactPdConfig(effective);
|
|
213
|
+
}
|