@principles/pd-cli 1.114.0 → 1.116.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.
Files changed (50) hide show
  1. package/dist/commands/diagnose.d.ts.map +1 -1
  2. package/dist/commands/diagnose.js +153 -132
  3. package/dist/commands/diagnose.js.map +1 -1
  4. package/dist/commands/runtime-features.d.ts.map +1 -1
  5. package/dist/commands/runtime-features.js +2 -7
  6. package/dist/commands/runtime-features.js.map +1 -1
  7. package/dist/commands/runtime-internalization-integrity-repair.d.ts.map +1 -1
  8. package/dist/commands/runtime-internalization-integrity-repair.js +15 -31
  9. package/dist/commands/runtime-internalization-integrity-repair.js.map +1 -1
  10. package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -1
  11. package/dist/commands/runtime-internalization-run-once.js +246 -326
  12. package/dist/commands/runtime-internalization-run-once.js.map +1 -1
  13. package/dist/commands/runtime-recovery.d.ts.map +1 -1
  14. package/dist/commands/runtime-recovery.js +9 -8
  15. package/dist/commands/runtime-recovery.js.map +1 -1
  16. package/dist/services/__tests__/cli-output.test.d.ts +18 -0
  17. package/dist/services/__tests__/cli-output.test.d.ts.map +1 -0
  18. package/dist/services/__tests__/cli-output.test.js +103 -0
  19. package/dist/services/__tests__/cli-output.test.js.map +1 -0
  20. package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts +18 -0
  21. package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts.map +1 -0
  22. package/dist/services/__tests__/runtime-adapter-resolver.test.js +651 -0
  23. package/dist/services/__tests__/runtime-adapter-resolver.test.js.map +1 -0
  24. package/dist/services/cli-output.d.ts +61 -0
  25. package/dist/services/cli-output.d.ts.map +1 -0
  26. package/dist/services/cli-output.js +72 -0
  27. package/dist/services/cli-output.js.map +1 -0
  28. package/dist/services/demo-rule-compiler.d.ts.map +1 -1
  29. package/dist/services/demo-rule-compiler.js +30 -6
  30. package/dist/services/demo-rule-compiler.js.map +1 -1
  31. package/dist/services/runtime-adapter-resolver.d.ts +105 -0
  32. package/dist/services/runtime-adapter-resolver.d.ts.map +1 -0
  33. package/dist/services/runtime-adapter-resolver.js +188 -0
  34. package/dist/services/runtime-adapter-resolver.js.map +1 -0
  35. package/package.json +1 -1
  36. package/src/commands/diagnose.ts +146 -138
  37. package/src/commands/runtime-features.ts +2 -6
  38. package/src/commands/runtime-internalization-integrity-repair.ts +16 -28
  39. package/src/commands/runtime-internalization-run-once.ts +242 -353
  40. package/src/commands/runtime-recovery.ts +9 -7
  41. package/src/services/__tests__/cli-output.test.ts +130 -0
  42. package/src/services/__tests__/runtime-adapter-resolver.test.ts +772 -0
  43. package/src/services/cli-output.ts +95 -0
  44. package/src/services/demo-rule-compiler.ts +35 -15
  45. package/src/services/runtime-adapter-resolver.ts +339 -0
  46. package/tests/commands/diagnose.test.ts +7 -3
  47. package/tests/commands/run-rulehost-handler.test.ts +253 -0
  48. package/tests/commands/runtime-internalization-run-once.test.ts +11 -0
  49. package/tests/commands/runtime-recovery.test.ts +27 -4
  50. package/tests/services/demo-rule-compiler.test.ts +242 -0
@@ -0,0 +1,651 @@
1
+ /**
2
+ * PRI-431 Step 3: Tests for shared runtime-adapter resolver.
3
+ *
4
+ * These tests capture the behavior of the existing `resolveRuntimeAdapter`
5
+ * function in runtime-internalization-run-once.ts (L222-551) so the extracted
6
+ * `resolveRuntimeAdapterFromConfig` in services/runtime-adapter-resolver.ts
7
+ * preserves the same contract.
8
+ *
9
+ * TDD flow: these tests are RED until resolveRuntimeAdapterFromConfig is implemented.
10
+ *
11
+ * ERR refs:
12
+ * - ERR-001 (no any): all mocks use typed vi.fn()
13
+ * - ERR-005 (no as bypass): no type casts in test assertions
14
+ * - ERR-009 (fail-loud): ConfigResolutionError thrown with structured fields
15
+ * - ERR-013 (Object.hasOwn): feature flag check uses Object.hasOwn
16
+ */
17
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
18
+ // ─── Mocks ─────────────────────────────────────────────────────────────────
19
+ // Track constructor calls so we can assert which adapter was constructed.
20
+ const mockTestDoubleCtor = vi.fn();
21
+ const mockPiAiCtor = vi.fn();
22
+ const mockOpenClawCliCtor = vi.fn();
23
+ const mockL2AgentLoopCtor = vi.fn();
24
+ const mockValidateRuntimeConfig = vi.fn();
25
+ const mockIsRuntimeConfigError = vi.fn();
26
+ const mockBuildL2PrincipleReader = vi.fn();
27
+ vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
28
+ const actual = await importOriginal();
29
+ return {
30
+ ...actual,
31
+ TestDoubleRuntimeAdapter: vi.fn(function (opts) {
32
+ mockTestDoubleCtor(opts);
33
+ return { __type: 'TestDoubleRuntimeAdapter', opts };
34
+ }),
35
+ PiAiRuntimeAdapter: vi.fn(function (opts) {
36
+ mockPiAiCtor(opts);
37
+ return { __type: 'PiAiRuntimeAdapter', opts };
38
+ }),
39
+ OpenClawCliRuntimeAdapter: vi.fn(function (opts) {
40
+ mockOpenClawCliCtor(opts);
41
+ return { __type: 'OpenClawCliRuntimeAdapter', opts };
42
+ }),
43
+ L2AgentLoopAdapter: vi.fn(function (opts, deps) {
44
+ mockL2AgentLoopCtor(opts, deps);
45
+ return { __type: 'L2AgentLoopAdapter', opts, deps };
46
+ }),
47
+ validateRuntimeConfig: mockValidateRuntimeConfig,
48
+ isRuntimeConfigError: mockIsRuntimeConfigError,
49
+ buildL2PrincipleReaderFromLedger: mockBuildL2PrincipleReader.mockReturnValue({
50
+ listActivePrinciples: vi.fn(),
51
+ }),
52
+ loadLedger: vi.fn().mockReturnValue({ tree: { principles: {} } }),
53
+ };
54
+ });
55
+ const mockLoadEffectiveFeatureFlags = vi.fn();
56
+ vi.mock('../feature-flag-loader.js', () => ({
57
+ loadEffectiveFeatureFlags: mockLoadEffectiveFeatureFlags,
58
+ }));
59
+ const mockResolveRuntimeFromPdConfig = vi.fn();
60
+ vi.mock('../resolve-runtime-from-pd-config.js', () => ({
61
+ resolveRuntimeFromPdConfig: mockResolveRuntimeFromPdConfig,
62
+ }));
63
+ // Import AFTER mocks are set up.
64
+ const { resolveRuntimeAdapterFromConfig, ConfigResolutionError } = await import('../runtime-adapter-resolver.js');
65
+ // ─── Test Helpers ──────────────────────────────────────────────────────────
66
+ function makeValidPiAiConfig(overrides = {}) {
67
+ return {
68
+ ok: true,
69
+ runtimeKind: 'pi-ai',
70
+ provider: 'openai',
71
+ model: 'gpt-4',
72
+ apiKeyEnv: 'OPENAI_API_KEY',
73
+ baseUrl: undefined,
74
+ maxRetries: 3,
75
+ timeoutMs: 300000,
76
+ agentId: 'main',
77
+ ...overrides,
78
+ };
79
+ }
80
+ function makeValidOpenClawConfig(overrides = {}) {
81
+ return {
82
+ ok: true,
83
+ runtimeKind: 'openclaw-cli',
84
+ openclawMode: 'local',
85
+ timeoutMs: 300000,
86
+ agentId: 'main',
87
+ ...overrides,
88
+ };
89
+ }
90
+ function makeConfigError(overrides = {}) {
91
+ return {
92
+ ok: false,
93
+ reason: 'missing-config',
94
+ message: 'runtime config not found',
95
+ nextAction: 'Create .pd/config.yaml',
96
+ ...overrides,
97
+ };
98
+ }
99
+ function makeResolvedConfig(result) {
100
+ return {
101
+ result,
102
+ legacyWarnings: [],
103
+ configLoadResult: { config: null, source: '.pd/config.yaml' },
104
+ configSource: '.pd/config.yaml',
105
+ runtimeProfileId: null,
106
+ runtimeProfileLabel: null,
107
+ };
108
+ }
109
+ // ─── Tests ─────────────────────────────────────────────────────────────────
110
+ describe('resolveRuntimeAdapterFromConfig (PRI-431)', () => {
111
+ beforeEach(() => {
112
+ vi.clearAllMocks();
113
+ // Default: isRuntimeConfigError returns false (config is valid)
114
+ mockIsRuntimeConfigError.mockReturnValue(false);
115
+ // Default: validateRuntimeConfig does nothing (config is valid)
116
+ mockValidateRuntimeConfig.mockImplementation(() => {
117
+ // no-op: valid config
118
+ });
119
+ // Default: feature flags have l2_dreamer disabled
120
+ mockLoadEffectiveFeatureFlags.mockReturnValue({
121
+ flags: {},
122
+ warnings: [],
123
+ });
124
+ });
125
+ // ── ConfigResolutionError class ──────────────────────────────────────────
126
+ describe('ConfigResolutionError', () => {
127
+ it('has name "ConfigResolutionError" and preserves message + kind + missing + nextAction', () => {
128
+ const err = new ConfigResolutionError('boom', 'missing-fields', {
129
+ missing: ['provider', 'model'],
130
+ nextAction: 'Set provider and model flags.',
131
+ });
132
+ expect(err).toBeInstanceOf(Error);
133
+ expect(err.name).toBe('ConfigResolutionError');
134
+ expect(err.message).toBe('boom');
135
+ expect(err.kind).toBe('missing-fields');
136
+ expect(err.missing).toEqual(['provider', 'model']);
137
+ expect(err.nextAction).toBe('Set provider and model flags.');
138
+ });
139
+ it('allows missing and nextAction fields to be undefined', () => {
140
+ const err = new ConfigResolutionError('boom', 'invalid-config');
141
+ expect(err.missing).toBeUndefined();
142
+ expect(err.nextAction).toBeUndefined();
143
+ });
144
+ });
145
+ // ── test-double branch ───────────────────────────────────────────────────
146
+ describe('test-double branch', () => {
147
+ it('returns adapter from testDoublePayloadBuilder when allowTestDouble is true', () => {
148
+ const fakeAdapter = { __type: 'custom-test-double' };
149
+ const builder = vi.fn(() => fakeAdapter);
150
+ const result = resolveRuntimeAdapterFromConfig({
151
+ runtimeKind: 'test-double',
152
+ workspaceDir: '/ws',
153
+ allowTestDouble: true,
154
+ testDoublePayloadBuilder: builder,
155
+ });
156
+ expect(result).toBe(fakeAdapter);
157
+ expect(builder).toHaveBeenCalledTimes(1);
158
+ expect(builder).toHaveBeenCalledWith(expect.objectContaining({
159
+ runtimeKind: 'test-double',
160
+ workspaceDir: '/ws',
161
+ allowTestDouble: true,
162
+ }));
163
+ });
164
+ it('throws ConfigResolutionError when allowTestDouble is false', () => {
165
+ const builder = vi.fn();
166
+ expect(() => {
167
+ resolveRuntimeAdapterFromConfig({
168
+ runtimeKind: 'test-double',
169
+ workspaceDir: '/ws',
170
+ allowTestDouble: false,
171
+ testDoublePayloadBuilder: builder,
172
+ });
173
+ }).toThrow(ConfigResolutionError);
174
+ expect(builder).not.toHaveBeenCalled();
175
+ });
176
+ it('throws ConfigResolutionError when allowTestDouble is undefined (default false)', () => {
177
+ const builder = vi.fn();
178
+ expect(() => {
179
+ resolveRuntimeAdapterFromConfig({
180
+ runtimeKind: 'test-double',
181
+ workspaceDir: '/ws',
182
+ testDoublePayloadBuilder: builder,
183
+ });
184
+ }).toThrow(ConfigResolutionError);
185
+ });
186
+ });
187
+ // ── pi-ai branch ─────────────────────────────────────────────────────────
188
+ describe('pi-ai branch', () => {
189
+ it('returns PiAiRuntimeAdapter when config is valid', () => {
190
+ const config = makeValidPiAiConfig();
191
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
192
+ const result = resolveRuntimeAdapterFromConfig({
193
+ runtimeKind: 'pi-ai',
194
+ workspaceDir: '/ws',
195
+ });
196
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
197
+ expect(mockPiAiCtor).toHaveBeenCalledTimes(1);
198
+ expect(mockValidateRuntimeConfig).toHaveBeenCalledWith(config);
199
+ });
200
+ it('throws ConfigResolutionError when validateRuntimeConfig throws', () => {
201
+ const config = makeValidPiAiConfig();
202
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
203
+ mockValidateRuntimeConfig.mockImplementation(() => {
204
+ throw new Error('missing provider');
205
+ });
206
+ expect(() => {
207
+ resolveRuntimeAdapterFromConfig({
208
+ runtimeKind: 'pi-ai',
209
+ workspaceDir: '/ws',
210
+ });
211
+ }).toThrow(ConfigResolutionError);
212
+ });
213
+ it('CLI timeoutMs override takes precedence over config timeoutMs', () => {
214
+ const config = makeValidPiAiConfig({ timeoutMs: 300000 });
215
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
216
+ resolveRuntimeAdapterFromConfig({
217
+ runtimeKind: 'pi-ai',
218
+ workspaceDir: '/ws',
219
+ timeoutMs: 60000,
220
+ });
221
+ expect(mockPiAiCtor).toHaveBeenCalledWith(expect.objectContaining({
222
+ timeoutMs: 60000,
223
+ }));
224
+ });
225
+ it('uses config timeoutMs when CLI timeoutMs is not provided', () => {
226
+ const config = makeValidPiAiConfig({ timeoutMs: 120000 });
227
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
228
+ resolveRuntimeAdapterFromConfig({
229
+ runtimeKind: 'pi-ai',
230
+ workspaceDir: '/ws',
231
+ });
232
+ expect(mockPiAiCtor).toHaveBeenCalledWith(expect.objectContaining({
233
+ timeoutMs: 120000,
234
+ }));
235
+ });
236
+ });
237
+ // ── L2 dreamer sub-branch ────────────────────────────────────────────────
238
+ describe('L2 dreamer sub-branch', () => {
239
+ it('returns L2AgentLoopAdapter when runnerKind=dreamer + l2ArtifactReader + l2StateDir + flag enabled', () => {
240
+ const config = makeValidPiAiConfig();
241
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
242
+ mockLoadEffectiveFeatureFlags.mockReturnValue({
243
+ flags: { l2_dreamer: { enabled: true } },
244
+ warnings: [],
245
+ });
246
+ const fakeArtifactReader = { readArtifact: vi.fn() };
247
+ const result = resolveRuntimeAdapterFromConfig({
248
+ runtimeKind: 'pi-ai',
249
+ workspaceDir: '/ws',
250
+ runnerKind: 'dreamer',
251
+ l2ArtifactReader: fakeArtifactReader,
252
+ l2StateDir: '/ws/.principles',
253
+ });
254
+ expect(result).toHaveProperty('__type', 'L2AgentLoopAdapter');
255
+ expect(mockL2AgentLoopCtor).toHaveBeenCalledTimes(1);
256
+ expect(mockBuildL2PrincipleReader).toHaveBeenCalled();
257
+ });
258
+ it('falls back to PiAiRuntimeAdapter when l2_dreamer flag is disabled', () => {
259
+ const config = makeValidPiAiConfig();
260
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
261
+ mockLoadEffectiveFeatureFlags.mockReturnValue({
262
+ flags: { l2_dreamer: { enabled: false } },
263
+ warnings: [],
264
+ });
265
+ const result = resolveRuntimeAdapterFromConfig({
266
+ runtimeKind: 'pi-ai',
267
+ workspaceDir: '/ws',
268
+ runnerKind: 'dreamer',
269
+ l2ArtifactReader: { readArtifact: vi.fn() },
270
+ l2StateDir: '/ws/.principles',
271
+ });
272
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
273
+ expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
274
+ });
275
+ it('falls back to PiAiRuntimeAdapter when l2ArtifactReader is missing', () => {
276
+ const config = makeValidPiAiConfig();
277
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
278
+ mockLoadEffectiveFeatureFlags.mockReturnValue({
279
+ flags: { l2_dreamer: { enabled: true } },
280
+ warnings: [],
281
+ });
282
+ const result = resolveRuntimeAdapterFromConfig({
283
+ runtimeKind: 'pi-ai',
284
+ workspaceDir: '/ws',
285
+ runnerKind: 'dreamer',
286
+ l2StateDir: '/ws/.principles',
287
+ });
288
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
289
+ expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
290
+ });
291
+ it('falls back to PiAiRuntimeAdapter when runnerKind is not dreamer', () => {
292
+ const config = makeValidPiAiConfig();
293
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
294
+ mockLoadEffectiveFeatureFlags.mockReturnValue({
295
+ flags: { l2_dreamer: { enabled: true } },
296
+ warnings: [],
297
+ });
298
+ const result = resolveRuntimeAdapterFromConfig({
299
+ runtimeKind: 'pi-ai',
300
+ workspaceDir: '/ws',
301
+ runnerKind: 'philosopher',
302
+ l2ArtifactReader: { readArtifact: vi.fn() },
303
+ l2StateDir: '/ws/.principles',
304
+ });
305
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
306
+ expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
307
+ });
308
+ });
309
+ // ── openclaw-cli branch ──────────────────────────────────────────────────
310
+ describe('openclaw-cli branch', () => {
311
+ it('returns OpenClawCliRuntimeAdapter when openclawMode is provided in config', () => {
312
+ const config = makeValidOpenClawConfig({ openclawMode: 'local' });
313
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
314
+ const result = resolveRuntimeAdapterFromConfig({
315
+ runtimeKind: 'openclaw-cli',
316
+ workspaceDir: '/ws',
317
+ });
318
+ expect(result).toHaveProperty('__type', 'OpenClawCliRuntimeAdapter');
319
+ expect(mockOpenClawCliCtor).toHaveBeenCalledWith(expect.objectContaining({
320
+ runtimeMode: 'local',
321
+ workspaceDir: '/ws',
322
+ }));
323
+ });
324
+ it('throws ConfigResolutionError when openclawMode is missing from config', () => {
325
+ const config = makeValidOpenClawConfig({ openclawMode: undefined });
326
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
327
+ expect(() => {
328
+ resolveRuntimeAdapterFromConfig({
329
+ runtimeKind: 'openclaw-cli',
330
+ workspaceDir: '/ws',
331
+ });
332
+ }).toThrow(ConfigResolutionError);
333
+ });
334
+ });
335
+ // ── config branch (delegates to resolveRuntimeFromPdConfig) ───────────────
336
+ describe('config branch', () => {
337
+ it('delegates to pi-ai when config resolves to pi-ai', () => {
338
+ const config = makeValidPiAiConfig();
339
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
340
+ const result = resolveRuntimeAdapterFromConfig({
341
+ runtimeKind: 'config',
342
+ workspaceDir: '/ws',
343
+ });
344
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
345
+ expect(mockResolveRuntimeFromPdConfig).toHaveBeenCalledWith('/ws');
346
+ });
347
+ it('delegates to openclaw-cli when config resolves to openclaw-cli', () => {
348
+ const config = makeValidOpenClawConfig();
349
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
350
+ const result = resolveRuntimeAdapterFromConfig({
351
+ runtimeKind: 'config',
352
+ workspaceDir: '/ws',
353
+ });
354
+ expect(result).toHaveProperty('__type', 'OpenClawCliRuntimeAdapter');
355
+ });
356
+ it('throws ConfigResolutionError when resolveRuntimeFromPdConfig returns error', () => {
357
+ const error = makeConfigError();
358
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
359
+ mockIsRuntimeConfigError.mockReturnValue(true);
360
+ expect(() => {
361
+ resolveRuntimeAdapterFromConfig({
362
+ runtimeKind: 'config',
363
+ workspaceDir: '/ws',
364
+ });
365
+ }).toThrow(ConfigResolutionError);
366
+ });
367
+ });
368
+ // ── Unsupported runtime ──────────────────────────────────────────────────
369
+ describe('unsupported runtime', () => {
370
+ it('throws plain Error for unsupported runtime kind', () => {
371
+ expect(() => {
372
+ resolveRuntimeAdapterFromConfig({
373
+ runtimeKind: 'unsupported-runtime',
374
+ workspaceDir: '/ws',
375
+ });
376
+ }).toThrow(/Unsupported runtime kind/);
377
+ });
378
+ it('does not throw ConfigResolutionError for unsupported runtime', () => {
379
+ let caught = null;
380
+ try {
381
+ resolveRuntimeAdapterFromConfig({
382
+ runtimeKind: 'unsupported-runtime',
383
+ workspaceDir: '/ws',
384
+ });
385
+ }
386
+ catch (err) {
387
+ caught = err;
388
+ }
389
+ expect(caught).not.toBeInstanceOf(ConfigResolutionError);
390
+ expect(caught).toBeInstanceOf(Error);
391
+ });
392
+ });
393
+ // ── PRI-431 Step 1d: New options for diagnose.ts migration ───────────────
394
+ describe('agentId option (openclaw-cli branch)', () => {
395
+ it('passes agentId to OpenClawCliRuntimeAdapter when provided', () => {
396
+ const config = makeValidOpenClawConfig({ openclawMode: 'local' });
397
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
398
+ resolveRuntimeAdapterFromConfig({
399
+ runtimeKind: 'openclaw-cli',
400
+ workspaceDir: '/ws',
401
+ agentId: 'diagnostician',
402
+ });
403
+ expect(mockOpenClawCliCtor).toHaveBeenCalledWith(expect.objectContaining({
404
+ agentId: 'diagnostician',
405
+ runtimeMode: 'local',
406
+ workspaceDir: '/ws',
407
+ }));
408
+ });
409
+ it('does not pass agentId when omitted (backward compat)', () => {
410
+ const config = makeValidOpenClawConfig({ openclawMode: 'local' });
411
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
412
+ resolveRuntimeAdapterFromConfig({
413
+ runtimeKind: 'openclaw-cli',
414
+ workspaceDir: '/ws',
415
+ });
416
+ const callArgs = mockOpenClawCliCtor.mock.calls[0][0];
417
+ expect(callArgs).not.toHaveProperty('agentId');
418
+ });
419
+ });
420
+ describe('configOptional option (pi-ai branch)', () => {
421
+ it('does NOT throw when config returns error and configOptional is true', () => {
422
+ const error = makeConfigError();
423
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
424
+ mockIsRuntimeConfigError.mockReturnValue(true);
425
+ // Should NOT throw — proceeds with piAiOverrides alone
426
+ const result = resolveRuntimeAdapterFromConfig({
427
+ runtimeKind: 'pi-ai',
428
+ workspaceDir: '/ws',
429
+ configOptional: true,
430
+ piAiOverrides: {
431
+ provider: 'openrouter',
432
+ model: 'anthropic/claude-sonnet-4',
433
+ apiKeyEnv: 'OPENROUTER_API_KEY',
434
+ },
435
+ });
436
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
437
+ expect(mockPiAiCtor).toHaveBeenCalledWith(expect.objectContaining({
438
+ provider: 'openrouter',
439
+ model: 'anthropic/claude-sonnet-4',
440
+ apiKeyEnv: 'OPENROUTER_API_KEY',
441
+ }));
442
+ });
443
+ it('throws ConfigResolutionError with missing-fields when configOptional is true and overrides are incomplete', () => {
444
+ const error = makeConfigError();
445
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
446
+ mockIsRuntimeConfigError.mockReturnValue(true);
447
+ let caught = null;
448
+ try {
449
+ resolveRuntimeAdapterFromConfig({
450
+ runtimeKind: 'pi-ai',
451
+ workspaceDir: '/ws',
452
+ configOptional: true,
453
+ piAiOverrides: {
454
+ provider: 'openrouter',
455
+ // model and apiKeyEnv missing
456
+ },
457
+ });
458
+ }
459
+ catch (err) {
460
+ caught = err;
461
+ }
462
+ expect(caught).toBeInstanceOf(ConfigResolutionError);
463
+ const err = caught;
464
+ expect(err.kind).toBe('missing-fields');
465
+ expect(err.missing).toEqual(expect.arrayContaining(['model', 'apiKeyEnv']));
466
+ });
467
+ it('does NOT call validateRuntimeConfig when configOptional is true and config failed', () => {
468
+ const error = makeConfigError();
469
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
470
+ mockIsRuntimeConfigError.mockReturnValue(true);
471
+ resolveRuntimeAdapterFromConfig({
472
+ runtimeKind: 'pi-ai',
473
+ workspaceDir: '/ws',
474
+ configOptional: true,
475
+ piAiOverrides: {
476
+ provider: 'openrouter',
477
+ model: 'anthropic/claude-sonnet-4',
478
+ apiKeyEnv: 'OPENROUTER_API_KEY',
479
+ },
480
+ });
481
+ expect(mockValidateRuntimeConfig).not.toHaveBeenCalled();
482
+ });
483
+ it('does NOT call validateRuntimeConfig when configOptional is true and config is valid (PR review fix)', () => {
484
+ // PR review P1 fix: original diagnose.ts never called validateRuntimeConfig.
485
+ // When configOptional=true, skip it entirely to avoid behavior change where
486
+ // validateRuntimeConfig would reject configs missing baseUrl for non-built-in
487
+ // providers, even when the user passes --baseUrl on the CLI.
488
+ const config = makeValidPiAiConfig({ baseUrl: undefined });
489
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
490
+ mockIsRuntimeConfigError.mockReturnValue(false);
491
+ resolveRuntimeAdapterFromConfig({
492
+ runtimeKind: 'pi-ai',
493
+ workspaceDir: '/ws',
494
+ configOptional: true,
495
+ piAiOverrides: {
496
+ provider: 'openrouter',
497
+ model: 'anthropic/claude-sonnet-4',
498
+ apiKeyEnv: 'OPENROUTER_API_KEY',
499
+ baseUrl: 'https://openrouter.ai/api/v1',
500
+ },
501
+ });
502
+ expect(mockValidateRuntimeConfig).not.toHaveBeenCalled();
503
+ expect(mockPiAiCtor).toHaveBeenCalledWith(expect.objectContaining({
504
+ provider: 'openrouter',
505
+ baseUrl: 'https://openrouter.ai/api/v1',
506
+ }));
507
+ });
508
+ it('does manual missing-field check on merged values when configOptional is true and config is valid', () => {
509
+ // PR review P1 fix: original diagnose.ts always did the manual missing-field check
510
+ // on merged values (not just when config failed). When configOptional=true, replicate
511
+ // that behavior — check merged provider/model/apiKeyEnv even when config is valid.
512
+ const config = makeValidPiAiConfig({ provider: undefined, model: undefined, apiKeyEnv: undefined });
513
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
514
+ mockIsRuntimeConfigError.mockReturnValue(false);
515
+ let caught = null;
516
+ try {
517
+ resolveRuntimeAdapterFromConfig({
518
+ runtimeKind: 'pi-ai',
519
+ workspaceDir: '/ws',
520
+ configOptional: true,
521
+ // No overrides — config has undefined provider/model/apiKeyEnv
522
+ });
523
+ }
524
+ catch (err) {
525
+ caught = err;
526
+ }
527
+ expect(caught).toBeInstanceOf(ConfigResolutionError);
528
+ const err = caught;
529
+ expect(err.kind).toBe('missing-fields');
530
+ expect(err.missing).toEqual(expect.arrayContaining(['provider', 'model', 'apiKeyEnv']));
531
+ });
532
+ });
533
+ describe('validateApiKeyEnv option (pi-ai branch)', () => {
534
+ let savedEnv;
535
+ beforeEach(() => {
536
+ savedEnv = process.env.TEST_API_KEY_FOR_RESOLVER;
537
+ });
538
+ afterEach(() => {
539
+ if (savedEnv === undefined) {
540
+ delete process.env.TEST_API_KEY_FOR_RESOLVER;
541
+ }
542
+ else {
543
+ process.env.TEST_API_KEY_FOR_RESOLVER = savedEnv;
544
+ }
545
+ });
546
+ it('throws ConfigResolutionError when validateApiKeyEnv is true and env var is unset', () => {
547
+ const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
548
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
549
+ delete process.env.TEST_API_KEY_FOR_RESOLVER;
550
+ let caught = null;
551
+ try {
552
+ resolveRuntimeAdapterFromConfig({
553
+ runtimeKind: 'pi-ai',
554
+ workspaceDir: '/ws',
555
+ validateApiKeyEnv: true,
556
+ });
557
+ }
558
+ catch (err) {
559
+ caught = err;
560
+ }
561
+ expect(caught).toBeInstanceOf(ConfigResolutionError);
562
+ const err = caught;
563
+ expect(err.kind).toBe('invalid-config');
564
+ expect(err.message).toContain('TEST_API_KEY_FOR_RESOLVER');
565
+ expect(mockPiAiCtor).not.toHaveBeenCalled();
566
+ });
567
+ it('creates adapter successfully when validateApiKeyEnv is true and env var is set', () => {
568
+ const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
569
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
570
+ process.env.TEST_API_KEY_FOR_RESOLVER = 'test-key-value';
571
+ const result = resolveRuntimeAdapterFromConfig({
572
+ runtimeKind: 'pi-ai',
573
+ workspaceDir: '/ws',
574
+ validateApiKeyEnv: true,
575
+ });
576
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
577
+ expect(mockPiAiCtor).toHaveBeenCalledTimes(1);
578
+ });
579
+ it('does NOT check process.env when validateApiKeyEnv is false (default)', () => {
580
+ const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
581
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
582
+ delete process.env.TEST_API_KEY_FOR_RESOLVER;
583
+ // Should NOT throw even though env var is unset
584
+ const result = resolveRuntimeAdapterFromConfig({
585
+ runtimeKind: 'pi-ai',
586
+ workspaceDir: '/ws',
587
+ // validateApiKeyEnv not provided — default false
588
+ });
589
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
590
+ });
591
+ });
592
+ describe('onConfigResolved callback', () => {
593
+ it('calls onConfigResolved with the full resolved object when config is valid', () => {
594
+ const config = makeValidPiAiConfig();
595
+ const resolved = makeResolvedConfig(config);
596
+ mockResolveRuntimeFromPdConfig.mockReturnValue(resolved);
597
+ const callback = vi.fn();
598
+ resolveRuntimeAdapterFromConfig({
599
+ runtimeKind: 'pi-ai',
600
+ workspaceDir: '/ws',
601
+ onConfigResolved: callback,
602
+ });
603
+ expect(callback).toHaveBeenCalledTimes(1);
604
+ expect(callback).toHaveBeenCalledWith(resolved);
605
+ });
606
+ it('calls onConfigResolved even when config returns error (if configOptional is true)', () => {
607
+ const error = makeConfigError();
608
+ const resolved = makeResolvedConfig(error);
609
+ mockResolveRuntimeFromPdConfig.mockReturnValue(resolved);
610
+ mockIsRuntimeConfigError.mockReturnValue(true);
611
+ const callback = vi.fn();
612
+ resolveRuntimeAdapterFromConfig({
613
+ runtimeKind: 'pi-ai',
614
+ workspaceDir: '/ws',
615
+ configOptional: true,
616
+ piAiOverrides: {
617
+ provider: 'openrouter',
618
+ model: 'anthropic/claude-sonnet-4',
619
+ apiKeyEnv: 'OPENROUTER_API_KEY',
620
+ },
621
+ onConfigResolved: callback,
622
+ });
623
+ expect(callback).toHaveBeenCalledTimes(1);
624
+ expect(callback).toHaveBeenCalledWith(resolved);
625
+ });
626
+ it('does NOT call onConfigResolved for test-double branch (no config resolution)', () => {
627
+ const callback = vi.fn();
628
+ const fakeAdapter = { __type: 'custom-test-double' };
629
+ const builder = vi.fn(() => fakeAdapter);
630
+ resolveRuntimeAdapterFromConfig({
631
+ runtimeKind: 'test-double',
632
+ workspaceDir: '/ws',
633
+ allowTestDouble: true,
634
+ testDoublePayloadBuilder: builder,
635
+ onConfigResolved: callback,
636
+ });
637
+ expect(callback).not.toHaveBeenCalled();
638
+ });
639
+ it('does not throw when onConfigResolved is omitted (backward compat)', () => {
640
+ const config = makeValidPiAiConfig();
641
+ mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
642
+ // Should not throw
643
+ const result = resolveRuntimeAdapterFromConfig({
644
+ runtimeKind: 'pi-ai',
645
+ workspaceDir: '/ws',
646
+ });
647
+ expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
648
+ });
649
+ });
650
+ });
651
+ //# sourceMappingURL=runtime-adapter-resolver.test.js.map