@principles/pd-cli 1.108.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.
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-runtime-from-pd-config.d.ts","sourceRoot":"","sources":["../../src/services/resolve-runtime-from-pd-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAMH,OAAO,KAAK,EACV,mBAAmB,EACnB,aAAa,EAEd,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,MAAM,WAAW,2BAA2B;IAC1C,8CAA8C;IAC9C,MAAM,EAAE,mBAAmB,CAAC;IAC5B,+EAA+E;IAC/E,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,0EAA0E;IAC1E,gBAAgB,EAAE,kBAAkB,CAAC;IACrC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,EACpB,SAAS,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAuC,GAC5E,2BAA2B,CA6C7B;AAGD;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE;IACT,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,EACD,SAAS,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAuC,GAC5E,2BAA2B,GAAG;IAAE,YAAY,EAAE,aAAa,GAAG,IAAI,CAAA;CAAE,CAoBtE"}
1
+ {"version":3,"file":"resolve-runtime-from-pd-config.d.ts","sourceRoot":"","sources":["../../src/services/resolve-runtime-from-pd-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAOH,OAAO,KAAK,EACV,mBAAmB,EACnB,aAAa,EAEd,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,MAAM,WAAW,2BAA2B;IAC1C,8CAA8C;IAC9C,MAAM,EAAE,mBAAmB,CAAC;IAC5B,+EAA+E;IAC/E,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,0EAA0E;IAC1E,gBAAgB,EAAE,kBAAkB,CAAC;IACrC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC;;;;OAIG;IACH,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;CACpC;AAkBD;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,EACpB,SAAS,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAuC,GAC5E,2BAA2B,CA0D7B;AAGD;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE;IACT,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,EACD,SAAS,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAuC,GAC5E,2BAA2B,GAAG;IAAE,YAAY,EAAE,aAAa,GAAG,IAAI,CAAA;CAAE,CAoBtE"}
@@ -12,8 +12,26 @@
12
12
  * - ERR-002: fail loud with reason + nextAction
13
13
  * - EP-07: runtime state source alignment
14
14
  */
15
- import { resolveRuntimeConfigFromPdConfig, isRuntimeConfigError, } from '@principles/core/runtime-v2';
15
+ import { resolveRuntimeConfigFromPdConfig, isRuntimeConfigError, resolveAgentRuntimeBinding, } from '@principles/core/runtime-v2';
16
16
  import { loadPdConfig } from './pd-config-loader.js';
17
+ /**
18
+ * Build a profile label matching the format used by `pd config doctor`.
19
+ * Mirrors `buildProfileLabel` in `pd-config-redaction.ts` (core).
20
+ */
21
+ function buildProfileLabel(profileId, profile) {
22
+ if (profile.type === 'openclaw') {
23
+ const parts = ['openclaw'];
24
+ if (profile.provider)
25
+ parts.push(profile.provider);
26
+ if (profile.model)
27
+ parts.push(profile.model);
28
+ if (profile.source && !profile.provider && !profile.model)
29
+ parts.push(profile.source);
30
+ return parts.join(': ');
31
+ }
32
+ // pi-ai
33
+ return `pi-ai: ${profile.provider ?? 'unknown'}/${profile.model ?? 'unknown'}`;
34
+ }
17
35
  /**
18
36
  * Resolve runtime configuration exclusively from .pd/config.yaml.
19
37
  *
@@ -49,9 +67,19 @@ export function resolveRuntimeFromPdConfig(workspaceDir, getEnvVar = (name) => p
49
67
  legacyWarnings,
50
68
  configLoadResult,
51
69
  configSource: '.pd/config.yaml',
70
+ runtimeProfileId: null,
71
+ runtimeProfileLabel: null,
52
72
  };
53
73
  }
54
74
  const result = resolveRuntimeConfigFromPdConfig(configLoadResult.effective, getEnvVar);
75
+ // PRI-402: Extract profile ID and label for probe output alignment with doctor
76
+ let runtimeProfileId = null;
77
+ let runtimeProfileLabel = null;
78
+ const bindingResult = resolveAgentRuntimeBinding(configLoadResult.effective, 'diagnostician');
79
+ if (bindingResult.ok) {
80
+ runtimeProfileId = bindingResult.profileId;
81
+ runtimeProfileLabel = buildProfileLabel(bindingResult.profileId, bindingResult.profile);
82
+ }
55
83
  const legacyWarnings = configLoadResult.legacyFilesDetected.length > 0
56
84
  ? [
57
85
  `Legacy config files detected: ${configLoadResult.legacyFilesDetected.join(', ')}. ` +
@@ -63,6 +91,8 @@ export function resolveRuntimeFromPdConfig(workspaceDir, getEnvVar = (name) => p
63
91
  legacyWarnings,
64
92
  configLoadResult,
65
93
  configSource: '.pd/config.yaml',
94
+ runtimeProfileId,
95
+ runtimeProfileLabel,
66
96
  };
67
97
  }
68
98
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-runtime-from-pd-config.js","sourceRoot":"","sources":["../../src/services/resolve-runtime-from-pd-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EACL,gCAAgC,EAChC,oBAAoB,GACrB,MAAM,6BAA6B,CAAC;AAMrC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAcrD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,0BAA0B,CACxC,YAAoB,EACpB,YAAkD,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;IAE7E,MAAM,gBAAgB,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;IAEpD,4EAA4E;IAC5E,yEAAyE;IACzE,uDAAuD;IACvD,IAAI,CAAC,gBAAgB,CAAC,EAAE,EAAE,CAAC;QACzB,MAAM,CAAC,UAAU,CAAC,GAAG,gBAAgB,CAAC,MAAM,CAAC;QAC7C,MAAM,MAAM,GAAuB;YACjC,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,oBAAoB,UAAU,EAAE,MAAM,IAAI,SAAS,EAAE;YAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,8BAA8B;YAC7D,UAAU,EAAE,UAAU,EAAE,UAAU,IAAI,sCAAsC;SAC7E,CAAC;QAEF,MAAM,cAAc,GAAG,gBAAgB,CAAC,mBAAmB,CAAC,MAAM,GAAG,CAAC;YACpE,CAAC,CAAC;gBACE,iCAAiC,gBAAgB,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI;oBACpF,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SAAS,EAAE,SAAS,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS;KACnD,CAAC;IAEF,OAAO,EAAE,GAAG,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;AAC3C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/pd-cli",
3
- "version": "1.108.0",
3
+ "version": "1.108.1",
4
4
  "description": "PD CLI — Pain recording, sample management, and evolution tasks",
5
5
  "type": "module",
6
6
  "bin": {
@@ -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
+ });