@principles/pd-cli 1.107.0 → 1.108.1

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 (43) hide show
  1. package/dist/commands/__tests__/mvp-smoke.test.d.ts +15 -0
  2. package/dist/commands/__tests__/mvp-smoke.test.d.ts.map +1 -0
  3. package/dist/commands/__tests__/mvp-smoke.test.js +245 -0
  4. package/dist/commands/__tests__/mvp-smoke.test.js.map +1 -0
  5. package/dist/commands/__tests__/runtime-probe-config.test.d.ts +20 -0
  6. package/dist/commands/__tests__/runtime-probe-config.test.d.ts.map +1 -0
  7. package/dist/commands/__tests__/runtime-probe-config.test.js +388 -0
  8. package/dist/commands/__tests__/runtime-probe-config.test.js.map +1 -0
  9. package/dist/commands/command-helpers.d.ts +19 -0
  10. package/dist/commands/command-helpers.d.ts.map +1 -0
  11. package/dist/commands/command-helpers.js +22 -0
  12. package/dist/commands/command-helpers.js.map +1 -0
  13. package/dist/commands/mvp-smoke.d.ts +30 -0
  14. package/dist/commands/mvp-smoke.d.ts.map +1 -0
  15. package/dist/commands/mvp-smoke.js +139 -0
  16. package/dist/commands/mvp-smoke.js.map +1 -0
  17. package/dist/commands/runtime.d.ts +6 -0
  18. package/dist/commands/runtime.d.ts.map +1 -1
  19. package/dist/commands/runtime.js +139 -13
  20. package/dist/commands/runtime.js.map +1 -1
  21. package/dist/commands/task.d.ts +11 -0
  22. package/dist/commands/task.d.ts.map +1 -1
  23. package/dist/commands/task.js +63 -2
  24. package/dist/commands/task.js.map +1 -1
  25. package/dist/index.js +7 -29
  26. package/dist/index.js.map +1 -1
  27. package/dist/services/mainline-snapshot-assembler.d.ts.map +1 -1
  28. package/dist/services/mainline-snapshot-assembler.js +22 -4
  29. package/dist/services/mainline-snapshot-assembler.js.map +1 -1
  30. package/dist/services/resolve-runtime-from-pd-config.d.ts +11 -0
  31. package/dist/services/resolve-runtime-from-pd-config.d.ts.map +1 -1
  32. package/dist/services/resolve-runtime-from-pd-config.js +31 -1
  33. package/dist/services/resolve-runtime-from-pd-config.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/commands/__tests__/mvp-smoke.test.ts +284 -0
  36. package/src/commands/__tests__/runtime-probe-config.test.ts +431 -0
  37. package/src/commands/command-helpers.ts +24 -0
  38. package/src/commands/mvp-smoke.ts +160 -0
  39. package/src/commands/runtime.ts +133 -13
  40. package/src/commands/task.ts +68 -3
  41. package/src/index.ts +9 -29
  42. package/src/services/mainline-snapshot-assembler.ts +23 -3
  43. package/src/services/resolve-runtime-from-pd-config.ts +41 -0
@@ -0,0 +1,431 @@
1
+ /**
2
+ * PRI-402: pd runtime probe reads .pd/config.yaml for pi-ai config.
3
+ *
4
+ * Tests cover:
5
+ * - probe reads config from .pd/config.yaml when --workspace is provided
6
+ * - JSON output includes configSource, runtimeProfileId, runtimeProfileLabel
7
+ * - explicit --provider overrides config.yaml
8
+ * - fail-loud JSON when config.yaml is missing or incomplete
9
+ * - program.parseAsync against real Commander registration (EP-04)
10
+ *
11
+ * ERR refs:
12
+ * - EP-04 (CLI gate): --json stdout single object, process.exit(1) + return
13
+ * - EP-03 (fail loud): structured JSON with reason + nextAction on failure
14
+ * - EP-07 (source alignment): probe and doctor read same config source
15
+ * - ERR-004 (source alignment): profileId/label must match doctor output
16
+ * - ERR-021 (handler-only tests): add program.parseAsync tests
17
+ * - ERR-029 (fail-loud JSON): config missing → structured JSON, not bare error
18
+ */
19
+
20
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
21
+ import { Command } from 'commander';
22
+ import * as fs from 'fs';
23
+ import * as path from 'path';
24
+ import * as os from 'os';
25
+
26
+ // ─── Mocks ─────────────────────────────────────────────────────────────────
27
+
28
+ // Mock only probeRuntime so we don't need a real LLM provider.
29
+ // All other core functions (validatePdConfig, computeEffectivePdConfig,
30
+ // resolveAgentRuntimeBinding, etc.) use their real implementations.
31
+ const mockProbeRuntime = vi.fn();
32
+ vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
33
+ const actual = await importOriginal<Record<string, unknown>>();
34
+ return {
35
+ ...actual,
36
+ probeRuntime: mockProbeRuntime,
37
+ };
38
+ });
39
+
40
+ const { handleRuntimeProbe } = await import('../runtime.js');
41
+
42
+ // ─── Test Setup ─────────────────────────────────────────────────────────────
43
+
44
+ const capturedStdout: string[] = [];
45
+ const capturedStderr: string[] = [];
46
+ let capturedExitCode: number | null = null;
47
+
48
+ const originalExit = process.exit;
49
+ const originalLog = console.log;
50
+ const originalError = console.error;
51
+ const originalWarn = console.warn;
52
+ const originalEnv = { ...process.env };
53
+
54
+ beforeEach(() => {
55
+ capturedExitCode = null;
56
+ capturedStdout.length = 0;
57
+ capturedStderr.length = 0;
58
+ process.exit = vi.fn(((code?: number) => {
59
+ capturedExitCode = code ?? 0;
60
+ }) as typeof process.exit);
61
+ console.log = vi.fn((...args: unknown[]) => { capturedStdout.push(args.join(' ')); });
62
+ console.error = vi.fn((...args: unknown[]) => { capturedStderr.push(args.join(' ')); });
63
+ console.warn = vi.fn(() => { /* capture warnings */ });
64
+ mockProbeRuntime.mockReset();
65
+ process.env = { ...originalEnv, LMSTUDIO_API_KEY: 'test-key-for-pri-402' };
66
+ });
67
+
68
+ afterEach(() => {
69
+ process.exit = originalExit;
70
+ console.log = originalLog;
71
+ console.error = originalError;
72
+ console.warn = originalWarn;
73
+ process.env = originalEnv;
74
+ });
75
+
76
+ // ─── Helper: create temp workspace with .pd/config.yaml ────────────────────
77
+
78
+ function createTempWorkspace(configYaml: string): string {
79
+ const tmpDir = path.join(os.tmpdir(), `pd-probe-test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
80
+ const pdDir = path.join(tmpDir, '.pd');
81
+ fs.mkdirSync(pdDir, { recursive: true });
82
+ // Replace __WORKSPACE_DIR__ placeholder with actual path
83
+ const resolvedYaml = configYaml.replace(/__WORKSPACE_DIR__/g, tmpDir.replace(/\\/g, '/'));
84
+ fs.writeFileSync(path.join(pdDir, 'config.yaml'), resolvedYaml, 'utf-8');
85
+ return tmpDir;
86
+ }
87
+
88
+ function cleanupWorkspace(dir: string): void {
89
+ try {
90
+ fs.rmSync(dir, { recursive: true, force: true });
91
+ } catch {
92
+ // best effort
93
+ }
94
+ }
95
+
96
+ const PI_AI_CONFIG_YAML = `
97
+ version: 1
98
+ features:
99
+ prompt: { category: core, enabled: true }
100
+ code_tool_hook: { category: core, enabled: true }
101
+ defer_archive: { category: core, enabled: true }
102
+ workspace:
103
+ default: __WORKSPACE_DIR__
104
+ internalAgents:
105
+ defaultRuntime: pi-ai.lmstudio
106
+ agents:
107
+ diagnostician:
108
+ enabled: true
109
+ runtimeProfile: pi-ai.lmstudio
110
+ dreamer:
111
+ enabled: true
112
+ philosopher:
113
+ enabled: true
114
+ scribe:
115
+ enabled: true
116
+ artificer:
117
+ enabled: true
118
+ runtimeProfiles:
119
+ pi-ai.lmstudio:
120
+ type: pi-ai
121
+ provider: lmstudio
122
+ model: qwen3.6-27b-mtp
123
+ apiKeyEnv: LMSTUDIO_API_KEY
124
+ baseUrl: http://localhost:1234/v1
125
+ `;
126
+
127
+ // ─── Tests: probe reads config from .pd/config.yaml ────────────────────────
128
+
129
+ describe('PRI-402: probe reads .pd/config.yaml for pi-ai config', () => {
130
+ it('reads provider/model from config.yaml when --workspace provided without --provider', async () => {
131
+ const workspace = createTempWorkspace(PI_AI_CONFIG_YAML);
132
+ try {
133
+ mockProbeRuntime.mockResolvedValue({
134
+ runtimeKind: 'pi-ai',
135
+ provider: 'lmstudio',
136
+ model: 'qwen3.6-27b-mtp',
137
+ health: { healthy: true, degraded: false, warnings: [], lastCheckedAt: '2026-01-01T00:00:00Z' },
138
+ capabilities: { streaming: true },
139
+ });
140
+
141
+ await handleRuntimeProbe({
142
+ runtime: 'pi-ai',
143
+ workspace,
144
+ json: true,
145
+ });
146
+
147
+ // probeRuntime should be called with config.yaml values
148
+ expect(mockProbeRuntime).toHaveBeenCalled();
149
+ const callArgs = mockProbeRuntime.mock.calls[0]?.[0];
150
+ expect(callArgs?.provider).toBe('lmstudio');
151
+ expect(callArgs?.model).toBe('qwen3.6-27b-mtp');
152
+ expect(callArgs?.apiKeyEnv).toBe('LMSTUDIO_API_KEY');
153
+
154
+ // JSON output should contain configSource, runtimeProfileId, runtimeProfileLabel
155
+ const output = JSON.parse(capturedStdout.join(''));
156
+ expect(output.configSource).toBe('.pd/config.yaml');
157
+ expect(output.runtimeProfileId).toBe('pi-ai.lmstudio');
158
+ expect(output.runtimeProfileLabel).toBe('pi-ai: lmstudio/qwen3.6-27b-mtp');
159
+ expect(output.ok).toBe(true);
160
+ } finally {
161
+ cleanupWorkspace(workspace);
162
+ }
163
+ });
164
+
165
+ it('explicit --provider overrides config.yaml value', async () => {
166
+ const workspace = createTempWorkspace(PI_AI_CONFIG_YAML);
167
+ try {
168
+ mockProbeRuntime.mockResolvedValue({
169
+ runtimeKind: 'pi-ai',
170
+ provider: 'openrouter',
171
+ model: 'qwen3.6-27b-mtp',
172
+ health: { healthy: true, degraded: false, warnings: [], lastCheckedAt: '2026-01-01T00:00:00Z' },
173
+ capabilities: { streaming: true },
174
+ });
175
+
176
+ await handleRuntimeProbe({
177
+ runtime: 'pi-ai',
178
+ workspace,
179
+ provider: 'openrouter',
180
+ model: 'anthropic/claude-sonnet-4',
181
+ apiKeyEnv: 'OPENROUTER_API_KEY',
182
+ json: true,
183
+ });
184
+
185
+ const callArgs = mockProbeRuntime.mock.calls[0]?.[0];
186
+ // CLI flags override config.yaml
187
+ expect(callArgs?.provider).toBe('openrouter');
188
+ expect(callArgs?.model).toBe('anthropic/claude-sonnet-4');
189
+ expect(callArgs?.apiKeyEnv).toBe('OPENROUTER_API_KEY');
190
+
191
+ // Profile info still comes from config.yaml (EP-07: source alignment)
192
+ const output = JSON.parse(capturedStdout.join(''));
193
+ expect(output.configSource).toBe('.pd/config.yaml');
194
+ expect(output.runtimeProfileId).toBe('pi-ai.lmstudio');
195
+ } finally {
196
+ cleanupWorkspace(workspace);
197
+ }
198
+ });
199
+
200
+ it('fail-loud JSON when config.yaml is missing and no --provider', async () => {
201
+ const workspace = path.join(os.tmpdir(), `pd-probe-test-missing-${Date.now()}`);
202
+ fs.mkdirSync(workspace, { recursive: true });
203
+ // No .pd/config.yaml created
204
+ try {
205
+ await handleRuntimeProbe({
206
+ runtime: 'pi-ai',
207
+ workspace,
208
+ json: true,
209
+ });
210
+
211
+ expect(capturedExitCode).toBe(1);
212
+ const output = JSON.parse(capturedStdout.join(''));
213
+ expect(output.ok).toBe(false);
214
+ expect(output.status).toBe('failed');
215
+ expect(typeof output.reason).toBe('string');
216
+ expect(typeof output.nextAction).toBe('string');
217
+ // Single parseable JSON object (EP-04 Rule 1)
218
+ expect(Array.isArray(output)).toBe(false);
219
+ } finally {
220
+ cleanupWorkspace(workspace);
221
+ }
222
+ });
223
+
224
+ it('fail-loud JSON when provider missing from config.yaml', async () => {
225
+ const workspace = createTempWorkspace(`
226
+ version: 1
227
+ features:
228
+ prompt: { category: core, enabled: true }
229
+ code_tool_hook: { category: core, enabled: true }
230
+ defer_archive: { category: core, enabled: true }
231
+ workspace:
232
+ default: __WORKSPACE_DIR__
233
+ internalAgents:
234
+ defaultRuntime: pi-ai.broken
235
+ agents:
236
+ diagnostician:
237
+ enabled: true
238
+ runtimeProfile: pi-ai.broken
239
+ runtimeProfiles:
240
+ pi-ai.broken:
241
+ type: pi-ai
242
+ # Missing provider, model, apiKeyEnv
243
+ `);
244
+ try {
245
+ await handleRuntimeProbe({
246
+ runtime: 'pi-ai',
247
+ workspace,
248
+ json: true,
249
+ });
250
+
251
+ expect(capturedExitCode).toBe(1);
252
+ const output = JSON.parse(capturedStdout.join(''));
253
+ expect(output.ok).toBe(false);
254
+ expect(output.status).toBe('failed');
255
+ expect(typeof output.reason).toBe('string');
256
+ expect(typeof output.nextAction).toBe('string');
257
+ } finally {
258
+ cleanupWorkspace(workspace);
259
+ }
260
+ });
261
+
262
+ it('fail-loud JSON when apiKeyEnv env var is not set', async () => {
263
+ const workspace = createTempWorkspace(PI_AI_CONFIG_YAML);
264
+ try {
265
+ // Remove the API key env var
266
+ delete process.env.LMSTUDIO_API_KEY;
267
+
268
+ await handleRuntimeProbe({
269
+ runtime: 'pi-ai',
270
+ workspace,
271
+ json: true,
272
+ });
273
+
274
+ expect(capturedExitCode).toBe(1);
275
+ const output = JSON.parse(capturedStdout.join(''));
276
+ expect(output.ok).toBe(false);
277
+ // When apiKeyEnv is not set, resolveRuntimeConfigFromPdConfig returns
278
+ // a config error (not_ready), so the error comes from the config resolution path
279
+ expect(typeof output.reason).toBe('string');
280
+ expect(typeof output.nextAction).toBe('string');
281
+ } finally {
282
+ cleanupWorkspace(workspace);
283
+ }
284
+ });
285
+
286
+ it('human-readable output includes Profile and Config lines', async () => {
287
+ const workspace = createTempWorkspace(PI_AI_CONFIG_YAML);
288
+ try {
289
+ mockProbeRuntime.mockResolvedValue({
290
+ runtimeKind: 'pi-ai',
291
+ provider: 'lmstudio',
292
+ model: 'qwen3.6-27b-mtp',
293
+ health: { healthy: true, degraded: false, warnings: [], lastCheckedAt: '2026-01-01T00:00:00Z' },
294
+ capabilities: { streaming: true },
295
+ });
296
+
297
+ await handleRuntimeProbe({
298
+ runtime: 'pi-ai',
299
+ workspace,
300
+ json: false,
301
+ });
302
+
303
+ const output = capturedStdout.join('\n');
304
+ expect(output).toContain('Profile:');
305
+ expect(output).toContain('pi-ai: lmstudio/qwen3.6-27b-mtp');
306
+ expect(output).toContain('Config:');
307
+ expect(output).toContain('.pd/config.yaml');
308
+ } finally {
309
+ cleanupWorkspace(workspace);
310
+ }
311
+ });
312
+ });
313
+
314
+ // ─── Tests: program.parseAsync against real Commander registration (EP-04) ──
315
+
316
+ // Import the real command registration function to test actual production wiring
317
+ const { registerRuntimeProbeCommand } = await import('../runtime.js');
318
+
319
+ interface CapturedAction {
320
+ opts: Record<string, unknown> | null;
321
+ }
322
+
323
+ function attachCapture(cmd: Command, state: CapturedAction): void {
324
+ // Override the action handler to capture opts without calling the real handler
325
+ cmd.action((...args: unknown[]) => {
326
+ // Find the opts object (last non-Command, non-null object arg)
327
+ let optsArg: Record<string, unknown> | null = null;
328
+ for (let i = args.length - 1; i >= 0; i--) {
329
+ const arg: unknown = args[i];
330
+ if (arg !== null && typeof arg === 'object' && !(arg instanceof Command)) {
331
+ optsArg = arg as Record<string, unknown>;
332
+ break;
333
+ }
334
+ }
335
+ state.opts = optsArg ?? {};
336
+ // Do NOT call original action (would call handleRuntimeProbe which needs real runtime)
337
+ });
338
+ }
339
+
340
+ function freshProgram(): Command {
341
+ const program = new Command();
342
+ program.name('pd').exitOverride();
343
+ return program;
344
+ }
345
+
346
+ describe('PRI-402: probe command flag wiring (EP-04 real registration)', () => {
347
+ it('registers --runtime as required option via real registration', () => {
348
+ const program = freshProgram();
349
+ const runtimeCmd = program.command('runtime');
350
+ const probeCmd = registerRuntimeProbeCommand(runtimeCmd);
351
+
352
+ const runtimeOpt = probeCmd.options.find((o) => o.long === '--runtime');
353
+ expect(runtimeOpt).toBeDefined();
354
+ expect(runtimeOpt?.required).toBe(true);
355
+ });
356
+
357
+ it('registers --workspace with -w shorthand via real registration', () => {
358
+ const program = freshProgram();
359
+ const runtimeCmd = program.command('runtime');
360
+ const probeCmd = registerRuntimeProbeCommand(runtimeCmd);
361
+
362
+ const wsOpt = probeCmd.options.find((o) => o.short === '-w');
363
+ expect(wsOpt).toBeDefined();
364
+ expect(wsOpt?.long).toBe('--workspace');
365
+ });
366
+
367
+ it('parses --runtime pi-ai --workspace <dir> --json correctly via real registration', async () => {
368
+ const program = freshProgram();
369
+ const runtimeCmd = program.command('runtime');
370
+ const probeCmd = registerRuntimeProbeCommand(runtimeCmd);
371
+ const captured: CapturedAction = { opts: null };
372
+ attachCapture(probeCmd, captured);
373
+
374
+ await program.parseAsync(['node', 'pd', 'runtime', 'probe', '--runtime', 'pi-ai', '--workspace', '/tmp/test', '--json']);
375
+
376
+ expect(captured.opts).not.toBeNull();
377
+ if (!captured.opts) throw new Error('captured.opts is null');
378
+ expect(captured.opts.runtime).toBe('pi-ai');
379
+ expect(captured.opts.workspace).toBe('/tmp/test');
380
+ expect(captured.opts.json).toBe(true);
381
+ });
382
+
383
+ it('parses --runtime config --workspace <dir> correctly via real registration', async () => {
384
+ const program = freshProgram();
385
+ const runtimeCmd = program.command('runtime');
386
+ const probeCmd = registerRuntimeProbeCommand(runtimeCmd);
387
+ const captured: CapturedAction = { opts: null };
388
+ attachCapture(probeCmd, captured);
389
+
390
+ await program.parseAsync(['node', 'pd', 'runtime', 'probe', '--runtime', 'config', '--workspace', '/tmp/test']);
391
+
392
+ expect(captured.opts).not.toBeNull();
393
+ if (!captured.opts) throw new Error('captured.opts is null');
394
+ expect(captured.opts.runtime).toBe('config');
395
+ expect(captured.opts.workspace).toBe('/tmp/test');
396
+ });
397
+ });
398
+
399
+ // ─── Tests: resolve-runtime-from-pd-config profile extraction ───────────────
400
+
401
+ describe('PRI-402: resolveRuntimeWithOverrides returns profile info', () => {
402
+ it('returns runtimeProfileId and runtimeProfileLabel from config.yaml', async () => {
403
+ const workspace = createTempWorkspace(PI_AI_CONFIG_YAML);
404
+ try {
405
+ const { resolveRuntimeWithOverrides } = await import('../../services/resolve-runtime-from-pd-config.js');
406
+ const result = resolveRuntimeWithOverrides(workspace, {});
407
+
408
+ expect(result.configSource).toBe('.pd/config.yaml');
409
+ expect(result.runtimeProfileId).toBe('pi-ai.lmstudio');
410
+ expect(result.runtimeProfileLabel).toBe('pi-ai: lmstudio/qwen3.6-27b-mtp');
411
+ } finally {
412
+ cleanupWorkspace(workspace);
413
+ }
414
+ });
415
+
416
+ it('returns default profile when config.yaml is missing', async () => {
417
+ const workspace = path.join(os.tmpdir(), `pd-probe-test-noprofile-${Date.now()}`);
418
+ fs.mkdirSync(workspace, { recursive: true });
419
+ try {
420
+ const { resolveRuntimeWithOverrides } = await import('../../services/resolve-runtime-from-pd-config.js');
421
+ const result = resolveRuntimeWithOverrides(workspace, {});
422
+
423
+ // Missing config → defaults, which use openclaw.default as defaultRuntime
424
+ expect(result.configSource).toBe('.pd/config.yaml');
425
+ expect(result.runtimeProfileId).toBe('openclaw.default');
426
+ expect(typeof result.runtimeProfileLabel).toBe('string');
427
+ } finally {
428
+ cleanupWorkspace(workspace);
429
+ }
430
+ });
431
+ });
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Command registration helpers (PRI-397 / C5 follow-up).
3
+ *
4
+ * These helpers are the single source of truth for which options a command
5
+ * supports. They are called by BOTH:
6
+ * - `packages/pd-cli/src/index.ts` (production registration)
7
+ * - parser-level tests (mvp-smoke.test.ts)
8
+ *
9
+ * Tests reuse the same registration functions so a typo in production
10
+ * (e.g., a flag mismatch between registration and handler) shows up at
11
+ * `program.parseAsync(...)` time, not just at handler dispatch (EP-04).
12
+ */
13
+
14
+ import type { Command } from 'commander';
15
+
16
+ /**
17
+ * Add the standard `--workspace <path>` / `--json` flag pair to a command.
18
+ * Returns the same command for chaining.
19
+ */
20
+ export function withWorkspaceAndJson(cmd: Command): Command {
21
+ return cmd
22
+ .option('-w, --workspace <path>', 'Workspace directory')
23
+ .option('--json', 'Output raw JSON');
24
+ }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * pd mvp smoke — MVP mainline readiness check (PRI-397 / C5).
3
+ *
4
+ * Assembles a MainlineSnapshot from the shared reader, judges it via the
5
+ * pure assertMainlineContract, and outputs a single JSON verdict.
6
+ *
7
+ * Rules (EP-04, EP-02):
8
+ * - Read-only by default. NEVER mutates workspace state.
9
+ * - Uses the shared mainline-snapshot-assembler (no new chain logic).
10
+ * - --json mode: stdout = exactly one parseable JSON object — even on failure.
11
+ * Every degraded/refused path emits a structured `{ok, reason, nextAction}`
12
+ * JSON so CI/script consumers can branch on outcome (EP-04 Rule 6).
13
+ * - Human mode: formatted output to stdout.
14
+ * - Exit code: 0 on overall === 'ok', 1 on overall === 'violation' or failure.
15
+ */
16
+
17
+ import type { Command } from 'commander';
18
+ import {
19
+ assembleMainlineSnapshot,
20
+ assertMainlineContract,
21
+ } from '../services/mainline-snapshot-assembler.js';
22
+ import { resolveWorkspaceDir } from '../resolve-workspace.js';
23
+ import { withWorkspaceAndJson } from './command-helpers.js';
24
+
25
+ export interface MvpSmokeOptions {
26
+ workspace?: string;
27
+ json?: boolean;
28
+ }
29
+
30
+ /**
31
+ * Classify a thrown error so the failure JSON can name a real next action.
32
+ * EP-03: never silently swallow; emit a structured reason + actionable next step.
33
+ */
34
+ function classifySmokeError(err: unknown): { reason: string; nextAction: string } {
35
+ if (err instanceof Error) {
36
+ const msg = err.message;
37
+ const lower = msg.toLowerCase();
38
+ // No .pd/state.db on disk (fresh / reset workspace) is the post-PRI-398
39
+ // expected first failure — name the recovery path explicitly.
40
+ if (
41
+ msg.includes('SQLITE_CANTOPEN') ||
42
+ lower.includes('no such file') ||
43
+ lower.includes('no such table') ||
44
+ lower.includes('cannot open database') ||
45
+ lower.includes('directory does not exist')
46
+ ) {
47
+ return {
48
+ reason: `Workspace database is missing or unreadable: ${msg}`,
49
+ nextAction: 'Run "pd runtime internalization integrity-repair --confirm" or restore the workspace; this command requires a bootstrapped .pd/state.db.',
50
+ };
51
+ }
52
+ if (lower.includes('workspace') && lower.includes('not configured')) {
53
+ return {
54
+ reason: msg,
55
+ nextAction: 'Set --workspace <path>, PD_WORKSPACE_DIR, or add workspace.default to .pd/config.yaml.',
56
+ };
57
+ }
58
+ return {
59
+ reason: msg,
60
+ nextAction: 'Inspect the workspace state and retry. Run "pd config doctor --json" to validate the workspace.',
61
+ };
62
+ }
63
+ return {
64
+ reason: `Unknown failure: ${String(err)}`,
65
+ nextAction: 'Inspect the workspace state and retry. Run "pd config doctor --json" to validate the workspace.',
66
+ };
67
+ }
68
+
69
+ export async function handleMvpSmoke(opts: MvpSmokeOptions): Promise<void> {
70
+ // Pass through resolveWorkspaceDir to honor the consistency warning for
71
+ // --workspace flags that disagree with the config default. The resolver
72
+ // returns an absolute, normalized path either way.
73
+ const workspaceDir = resolveWorkspaceDir(opts.workspace);
74
+
75
+ let result;
76
+ try {
77
+ result = await assembleMainlineSnapshot({ workspaceDir });
78
+ } catch (err: unknown) {
79
+ const { reason, nextAction } = classifySmokeError(err);
80
+ if (opts.json) {
81
+ // EP-04 Rule 1 + 6: stdout = exactly one parseable JSON object carrying
82
+ // a structured reason and nextAction, even on the failure path.
83
+ console.log(JSON.stringify({
84
+ ok: false,
85
+ reason,
86
+ nextAction,
87
+ workspace: workspaceDir,
88
+ }, null, 2));
89
+ } else {
90
+ console.error(`MVP smoke failed: ${reason}`);
91
+ console.error(`nextAction: ${nextAction}`);
92
+ }
93
+ process.exit(1);
94
+ return;
95
+ }
96
+
97
+ const verdict = assertMainlineContract(result.snapshot);
98
+
99
+ if (opts.json) {
100
+ console.log(JSON.stringify({
101
+ ok: verdict.overall === 'ok',
102
+ verdict,
103
+ warnings: result.warnings,
104
+ resolvedPainId: result.resolvedPainId,
105
+ }, null, 2));
106
+ } else {
107
+ console.log(`\nMVP Smoke — ${workspaceDir}\n`);
108
+ console.log(` Overall: ${verdict.overall}`);
109
+ console.log(` Pain ID: ${verdict.painId ?? '(none)'}`);
110
+ console.log(` Generated At: ${verdict.generatedAt}\n`);
111
+
112
+ console.log(' Stages:');
113
+ for (const s of verdict.stages) {
114
+ const icon = s.status === 'ok' ? ' OK' : s.status === 'violation' ? ' VIOLATION' : ' SKIP';
115
+ console.log(` [${icon}] ${s.stage}`);
116
+ console.log(` ${s.reason}`);
117
+ if (s.nextAction) {
118
+ console.log(` nextAction: ${s.nextAction}`);
119
+ }
120
+ console.log('');
121
+ }
122
+
123
+ if (result.warnings.length > 0) {
124
+ console.log(' Warnings:');
125
+ for (const w of result.warnings) {
126
+ console.log(` - ${w}`);
127
+ }
128
+ console.log('');
129
+ }
130
+ }
131
+
132
+ if (verdict.overall === 'violation') {
133
+ process.exit(1);
134
+ return;
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Register the `pd mvp` parent command and its `smoke` subcommand.
140
+ *
141
+ * Single source of truth for both production (`index.ts`) and parser tests
142
+ * (`mvp-smoke.test.ts`). If a test passes against this function, the same
143
+ * registration runs in production — flag typos surface at parseAsync time.
144
+ */
145
+ export function registerMvpCommands(program: Command): Command {
146
+ const mvpCmd = program
147
+ .command('mvp')
148
+ .description('MVP readiness commands');
149
+
150
+ withWorkspaceAndJson(
151
+ mvpCmd
152
+ .command('smoke')
153
+ .description('Check MVP mainline readiness: assemble snapshot → assert contract → structured verdict')
154
+ .action(async (opts: { workspace?: string; json?: boolean }) => {
155
+ await handleMvpSmoke({ workspace: opts.workspace, json: opts.json });
156
+ }),
157
+ );
158
+
159
+ return mvpCmd;
160
+ }