@principles/pd-cli 1.152.12 → 1.152.13

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 (296) hide show
  1. package/package.json +13 -7
  2. package/CHANGELOG.md +0 -23
  3. package/dist/commands/__tests__/intent-flag-wiring.test.d.ts +0 -9
  4. package/dist/commands/__tests__/intent-flag-wiring.test.d.ts.map +0 -1
  5. package/dist/commands/__tests__/intent-flag-wiring.test.js +0 -237
  6. package/dist/commands/__tests__/intent-flag-wiring.test.js.map +0 -1
  7. package/dist/commands/__tests__/legacy-cleanup.test.d.ts +0 -18
  8. package/dist/commands/__tests__/legacy-cleanup.test.d.ts.map +0 -1
  9. package/dist/commands/__tests__/legacy-cleanup.test.js +0 -467
  10. package/dist/commands/__tests__/legacy-cleanup.test.js.map +0 -1
  11. package/dist/commands/__tests__/mvp-smoke.test.d.ts +0 -15
  12. package/dist/commands/__tests__/mvp-smoke.test.d.ts.map +0 -1
  13. package/dist/commands/__tests__/mvp-smoke.test.js +0 -262
  14. package/dist/commands/__tests__/mvp-smoke.test.js.map +0 -1
  15. package/dist/commands/__tests__/pain-retry-maxTokens-flag.test.d.ts +0 -2
  16. package/dist/commands/__tests__/pain-retry-maxTokens-flag.test.d.ts.map +0 -1
  17. package/dist/commands/__tests__/pain-retry-maxTokens-flag.test.js +0 -60
  18. package/dist/commands/__tests__/pain-retry-maxTokens-flag.test.js.map +0 -1
  19. package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts +0 -21
  20. package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts.map +0 -1
  21. package/dist/commands/__tests__/rulecode-flag-wiring.test.js +0 -186
  22. package/dist/commands/__tests__/rulecode-flag-wiring.test.js.map +0 -1
  23. package/dist/commands/__tests__/rulecode-handler.test.d.ts +0 -16
  24. package/dist/commands/__tests__/rulecode-handler.test.d.ts.map +0 -1
  25. package/dist/commands/__tests__/rulecode-handler.test.js +0 -285
  26. package/dist/commands/__tests__/rulecode-handler.test.js.map +0 -1
  27. package/dist/commands/__tests__/run-rulehost-flag-wiring.test.d.ts +0 -24
  28. package/dist/commands/__tests__/run-rulehost-flag-wiring.test.d.ts.map +0 -1
  29. package/dist/commands/__tests__/run-rulehost-flag-wiring.test.js +0 -232
  30. package/dist/commands/__tests__/run-rulehost-flag-wiring.test.js.map +0 -1
  31. package/dist/commands/__tests__/runtime-activation-nextaction.test.d.ts +0 -15
  32. package/dist/commands/__tests__/runtime-activation-nextaction.test.d.ts.map +0 -1
  33. package/dist/commands/__tests__/runtime-activation-nextaction.test.js +0 -73
  34. package/dist/commands/__tests__/runtime-activation-nextaction.test.js.map +0 -1
  35. package/dist/commands/__tests__/runtime-probe-config.test.d.ts +0 -20
  36. package/dist/commands/__tests__/runtime-probe-config.test.d.ts.map +0 -1
  37. package/dist/commands/__tests__/runtime-probe-config.test.js +0 -389
  38. package/dist/commands/__tests__/runtime-probe-config.test.js.map +0 -1
  39. package/dist/commands/__tests__/telemetry-flag-wiring.test.d.ts +0 -9
  40. package/dist/commands/__tests__/telemetry-flag-wiring.test.d.ts.map +0 -1
  41. package/dist/commands/__tests__/telemetry-flag-wiring.test.js +0 -76
  42. package/dist/commands/__tests__/telemetry-flag-wiring.test.js.map +0 -1
  43. package/dist/commands/__tests__/version.test.d.ts +0 -2
  44. package/dist/commands/__tests__/version.test.d.ts.map +0 -1
  45. package/dist/commands/__tests__/version.test.js +0 -238
  46. package/dist/commands/__tests__/version.test.js.map +0 -1
  47. package/dist/commands/runtime-uat.guard.test.d.ts +0 -2
  48. package/dist/commands/runtime-uat.guard.test.d.ts.map +0 -1
  49. package/dist/commands/runtime-uat.guard.test.js +0 -153
  50. package/dist/commands/runtime-uat.guard.test.js.map +0 -1
  51. package/dist/services/__tests__/cli-output.test.d.ts +0 -18
  52. package/dist/services/__tests__/cli-output.test.d.ts.map +0 -1
  53. package/dist/services/__tests__/cli-output.test.js +0 -103
  54. package/dist/services/__tests__/cli-output.test.js.map +0 -1
  55. package/dist/services/__tests__/evaluator-runner-deps.test.d.ts +0 -2
  56. package/dist/services/__tests__/evaluator-runner-deps.test.d.ts.map +0 -1
  57. package/dist/services/__tests__/evaluator-runner-deps.test.js +0 -343
  58. package/dist/services/__tests__/evaluator-runner-deps.test.js.map +0 -1
  59. package/dist/services/__tests__/rulehost-readiness.test.d.ts +0 -2
  60. package/dist/services/__tests__/rulehost-readiness.test.d.ts.map +0 -1
  61. package/dist/services/__tests__/rulehost-readiness.test.js +0 -311
  62. package/dist/services/__tests__/rulehost-readiness.test.js.map +0 -1
  63. package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts +0 -18
  64. package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts.map +0 -1
  65. package/dist/services/__tests__/runtime-adapter-resolver.test.js +0 -673
  66. package/dist/services/__tests__/runtime-adapter-resolver.test.js.map +0 -1
  67. package/dist/utils/production-workspace-guard.test.d.ts +0 -2
  68. package/dist/utils/production-workspace-guard.test.d.ts.map +0 -1
  69. package/dist/utils/production-workspace-guard.test.js +0 -137
  70. package/dist/utils/production-workspace-guard.test.js.map +0 -1
  71. package/scripts/llm-dogfood.ts +0 -456
  72. package/scripts/migrate-illegal-expected-decision.ts +0 -252
  73. package/src/commands/__tests__/intent-flag-wiring.test.ts +0 -282
  74. package/src/commands/__tests__/legacy-cleanup.test.ts +0 -602
  75. package/src/commands/__tests__/mvp-smoke.test.ts +0 -300
  76. package/src/commands/__tests__/pain-retry-maxTokens-flag.test.ts +0 -73
  77. package/src/commands/__tests__/rulecode-flag-wiring.test.ts +0 -237
  78. package/src/commands/__tests__/rulecode-handler.test.ts +0 -369
  79. package/src/commands/__tests__/run-rulehost-flag-wiring.test.ts +0 -292
  80. package/src/commands/__tests__/runtime-activation-nextaction.test.ts +0 -81
  81. package/src/commands/__tests__/runtime-probe-config.test.ts +0 -432
  82. package/src/commands/__tests__/telemetry-flag-wiring.test.ts +0 -84
  83. package/src/commands/__tests__/version.test.ts +0 -251
  84. package/src/commands/admission-gate.ts +0 -30
  85. package/src/commands/artifact.ts +0 -82
  86. package/src/commands/build-trajectory-evidence.ts +0 -298
  87. package/src/commands/candidate.ts +0 -1250
  88. package/src/commands/codex-ingest-catchup.ts +0 -114
  89. package/src/commands/codex-ingest-quarantine.ts +0 -127
  90. package/src/commands/codex-reconcile.ts +0 -84
  91. package/src/commands/codex-setup.ts +0 -476
  92. package/src/commands/codex-worker.ts +0 -185
  93. package/src/commands/command-helpers.ts +0 -24
  94. package/src/commands/config-doctor.ts +0 -193
  95. package/src/commands/console.ts +0 -797
  96. package/src/commands/context.ts +0 -72
  97. package/src/commands/demo-story-a.ts +0 -241
  98. package/src/commands/diagnose.ts +0 -756
  99. package/src/commands/errors-list.ts +0 -441
  100. package/src/commands/evolution-tasks-list.ts +0 -48
  101. package/src/commands/evolution-tasks-show.ts +0 -62
  102. package/src/commands/flow.ts +0 -60
  103. package/src/commands/health-codex.ts +0 -576
  104. package/src/commands/health.ts +0 -315
  105. package/src/commands/history.ts +0 -63
  106. package/src/commands/intent.ts +0 -457
  107. package/src/commands/legacy-cleanup.ts +0 -531
  108. package/src/commands/legacy-import.ts +0 -104
  109. package/src/commands/mvp-smoke.ts +0 -160
  110. package/src/commands/pain-evidence.ts +0 -226
  111. package/src/commands/pain-list.ts +0 -239
  112. package/src/commands/pain-record.ts +0 -612
  113. package/src/commands/pain-retry.ts +0 -916
  114. package/src/commands/principles-stats.ts +0 -700
  115. package/src/commands/proven-channel-baseline.ts +0 -115
  116. package/src/commands/quality-scorecard.ts +0 -272
  117. package/src/commands/remediation-output.ts +0 -66
  118. package/src/commands/rulecode.ts +0 -465
  119. package/src/commands/run.ts +0 -89
  120. package/src/commands/runtime-activation.ts +0 -1629
  121. package/src/commands/runtime-artifact-repair.ts +0 -524
  122. package/src/commands/runtime-canary.ts +0 -395
  123. package/src/commands/runtime-compatibility-scan.ts +0 -105
  124. package/src/commands/runtime-diagnostics-export.ts +0 -229
  125. package/src/commands/runtime-features.ts +0 -153
  126. package/src/commands/runtime-gfi-snapshot.ts +0 -135
  127. package/src/commands/runtime-health-snapshot.ts +0 -108
  128. package/src/commands/runtime-init.ts +0 -429
  129. package/src/commands/runtime-internalization-enqueue-successors.ts +0 -530
  130. package/src/commands/runtime-internalization-integrity-repair.ts +0 -79
  131. package/src/commands/runtime-internalization-integrity.ts +0 -107
  132. package/src/commands/runtime-internalization-queue.ts +0 -135
  133. package/src/commands/runtime-internalization-retry.ts +0 -191
  134. package/src/commands/runtime-internalization-run-once.ts +0 -791
  135. package/src/commands/runtime-internalization-run-rulehost.ts +0 -749
  136. package/src/commands/runtime-internalization-wake-once.ts +0 -87
  137. package/src/commands/runtime-pain-flood-simulation.ts +0 -121
  138. package/src/commands/runtime-pruning.ts +0 -438
  139. package/src/commands/runtime-recovery-failed-tasks.ts +0 -258
  140. package/src/commands/runtime-recovery.ts +0 -109
  141. package/src/commands/runtime-synthetic-baseline.ts +0 -70
  142. package/src/commands/runtime-uat.guard.test.ts +0 -175
  143. package/src/commands/runtime-uat.ts +0 -380
  144. package/src/commands/runtime.ts +0 -473
  145. package/src/commands/samples-list.ts +0 -39
  146. package/src/commands/samples-review.ts +0 -36
  147. package/src/commands/task.ts +0 -271
  148. package/src/commands/telemetry.ts +0 -349
  149. package/src/commands/trace.ts +0 -174
  150. package/src/commands/trajectory-db-unavailable.ts +0 -19
  151. package/src/commands/trajectory.ts +0 -64
  152. package/src/commands/version.ts +0 -49
  153. package/src/config-reader.ts +0 -122
  154. package/src/index.ts +0 -1276
  155. package/src/legacy/legacy-import.ts +0 -179
  156. package/src/legacy/session-history-import.ts +0 -203
  157. package/src/resolve-workspace.ts +0 -106
  158. package/src/services/__tests__/cli-output.test.ts +0 -130
  159. package/src/services/__tests__/evaluator-runner-deps.test.ts +0 -382
  160. package/src/services/__tests__/rulehost-readiness.test.ts +0 -363
  161. package/src/services/__tests__/runtime-adapter-resolver.test.ts +0 -793
  162. package/src/services/cli-output.ts +0 -95
  163. package/src/services/config-doctor.ts +0 -697
  164. package/src/services/console-launcher.ts +0 -537
  165. package/src/services/demo-rule-compiler.ts +0 -40
  166. package/src/services/demo-story-a-runner.ts +0 -471
  167. package/src/services/mainline-snapshot-assembler.ts +0 -579
  168. package/src/services/pain-flood-simulation-runner.ts +0 -462
  169. package/src/services/pd-config-loader.ts +0 -433
  170. package/src/services/proven-channel-baseline-runner.ts +0 -157
  171. package/src/services/quality-scorecard/data-extractor.ts +0 -150
  172. package/src/services/quality-scorecard/local-evaluator.ts +0 -142
  173. package/src/services/quality-scorecard/strong-model-gate.ts +0 -202
  174. package/src/services/resolve-runtime-from-pd-config.ts +0 -230
  175. package/src/services/rulehost-pipeline-runner.ts +0 -1214
  176. package/src/services/rulehost-readiness.ts +0 -326
  177. package/src/services/runtime-adapter-resolver.ts +0 -364
  178. package/src/services/synthetic-baseline-runner.ts +0 -400
  179. package/src/services/version-report.ts +0 -253
  180. package/src/services/workspace-tool-semantics.ts +0 -26
  181. package/src/utils/path-security.ts +0 -96
  182. package/src/utils/production-workspace-guard.test.ts +0 -157
  183. package/src/utils/production-workspace-guard.ts +0 -210
  184. package/tests/bdd/cli-contract.steps.ts +0 -346
  185. package/tests/bdd/principles-stats.steps.ts +0 -211
  186. package/tests/bdd/support/gherkin-loader.ts +0 -147
  187. package/tests/bdd/support/repo-root.ts +0 -62
  188. package/tests/bdd/support/vitest-bdd.ts +0 -179
  189. package/tests/commands/admission-gate-coverage.test.ts +0 -79
  190. package/tests/commands/build-trajectory-evidence.test.ts +0 -508
  191. package/tests/commands/candidate-audit-repair.test.ts +0 -421
  192. package/tests/commands/candidate-intake.test.ts +0 -732
  193. package/tests/commands/candidate-internalization-backfill.test.ts +0 -548
  194. package/tests/commands/candidate-internalize-lineage.test.ts +0 -628
  195. package/tests/commands/candidate-internalize.test.ts +0 -299
  196. package/tests/commands/candidate-route.test.ts +0 -329
  197. package/tests/commands/candidate-show.test.ts +0 -95
  198. package/tests/commands/cli-command-tree.test.ts +0 -144
  199. package/tests/commands/cli-help-snapshot.test.ts +0 -155
  200. package/tests/commands/cli-skill-contract.test.ts +0 -121
  201. package/tests/commands/codex-consent-reversibility.steps.test.ts +0 -299
  202. package/tests/commands/codex-ingest-quarantine.test.ts +0 -136
  203. package/tests/commands/codex-setup.test.ts +0 -297
  204. package/tests/commands/codex-worker-daemon.test.ts +0 -95
  205. package/tests/commands/codex-worker-registration.test.ts +0 -207
  206. package/tests/commands/config-doctor.test.ts +0 -336
  207. package/tests/commands/console-launcher-edge-cases.test.ts +0 -388
  208. package/tests/commands/console-open.test.ts +0 -1381
  209. package/tests/commands/context.test.ts +0 -114
  210. package/tests/commands/demo-story-a.test.ts +0 -318
  211. package/tests/commands/diagnose.test.ts +0 -1803
  212. package/tests/commands/errors-list.test.ts +0 -679
  213. package/tests/commands/evolution-tasks-list.test.ts +0 -41
  214. package/tests/commands/evolution-tasks-show.test.ts +0 -46
  215. package/tests/commands/health-codex.test.ts +0 -349
  216. package/tests/commands/health-host-wiring.test.ts +0 -76
  217. package/tests/commands/health-reliability.test.ts +0 -99
  218. package/tests/commands/health.test.ts +0 -413
  219. package/tests/commands/intent.test.ts +0 -321
  220. package/tests/commands/legacy-cleanup.test.ts +0 -148
  221. package/tests/commands/pain-evidence.test.ts +0 -518
  222. package/tests/commands/pain-list.test.ts +0 -219
  223. package/tests/commands/pain-record-async.test.ts +0 -335
  224. package/tests/commands/pain-record-session-parser.test.ts +0 -228
  225. package/tests/commands/pain-record.test.ts +0 -851
  226. package/tests/commands/pain-retry.test.ts +0 -1121
  227. package/tests/commands/plugin-config-resolution-cutover.test.ts +0 -229
  228. package/tests/commands/pri-393-runtime-config-unification.test.ts +0 -443
  229. package/tests/commands/principles-stats-wiring.test.ts +0 -135
  230. package/tests/commands/principles-stats.test.ts +0 -274
  231. package/tests/commands/product-path-regression.test.ts +0 -86
  232. package/tests/commands/proven-channel-baseline.test.ts +0 -441
  233. package/tests/commands/rulecode.test.ts +0 -177
  234. package/tests/commands/run-rulehost-handler.test.ts +0 -908
  235. package/tests/commands/runtime-activation-approve-flag-wiring.test.ts +0 -227
  236. package/tests/commands/runtime-activation-deactivate-flag-wiring.test.ts +0 -167
  237. package/tests/commands/runtime-activation-dispatch-flag-wiring.test.ts +0 -262
  238. package/tests/commands/runtime-activation-governance-audit.test.ts +0 -95
  239. package/tests/commands/runtime-activation-list-flag-wiring.test.ts +0 -193
  240. package/tests/commands/runtime-activation-promote-flag-wiring.test.ts +0 -248
  241. package/tests/commands/runtime-activation-shadow-telemetry.test.ts +0 -130
  242. package/tests/commands/runtime-activation.test.ts +0 -2052
  243. package/tests/commands/runtime-artifact-repair-registration.test.ts +0 -74
  244. package/tests/commands/runtime-artifact-repair.test.ts +0 -404
  245. package/tests/commands/runtime-canary.test.ts +0 -430
  246. package/tests/commands/runtime-compatibility-scan.test.ts +0 -146
  247. package/tests/commands/runtime-diagnostics-export.test.ts +0 -175
  248. package/tests/commands/runtime-features.test.ts +0 -249
  249. package/tests/commands/runtime-health-snapshot.test.ts +0 -358
  250. package/tests/commands/runtime-init-empty-workspace.test.ts +0 -252
  251. package/tests/commands/runtime-init.test.ts +0 -420
  252. package/tests/commands/runtime-internalization-enqueue-successors.test.ts +0 -782
  253. package/tests/commands/runtime-internalization-integrity-repair.test.ts +0 -239
  254. package/tests/commands/runtime-internalization-integrity.test.ts +0 -140
  255. package/tests/commands/runtime-internalization-queue.test.ts +0 -380
  256. package/tests/commands/runtime-internalization-retry-owner-authority.test.ts +0 -431
  257. package/tests/commands/runtime-internalization-run-once-evaluator-parity.test.ts +0 -269
  258. package/tests/commands/runtime-internalization-run-once-language.test.ts +0 -303
  259. package/tests/commands/runtime-internalization-run-once-real-config.test.ts +0 -237
  260. package/tests/commands/runtime-internalization-run-once-rollout-parity.test.ts +0 -706
  261. package/tests/commands/runtime-internalization-run-once.test.ts +0 -1791
  262. package/tests/commands/runtime-internalization-wake-once.test.ts +0 -171
  263. package/tests/commands/runtime-internalization.test.ts +0 -53
  264. package/tests/commands/runtime-pain-flood-simulation.test.ts +0 -418
  265. package/tests/commands/runtime-pruning.test.ts +0 -694
  266. package/tests/commands/runtime-recovery-failed-tasks.test.ts +0 -328
  267. package/tests/commands/runtime-recovery.test.ts +0 -120
  268. package/tests/commands/runtime-synthetic-baseline.test.ts +0 -249
  269. package/tests/commands/runtime-uat.test.ts +0 -397
  270. package/tests/commands/runtime.test.ts +0 -392
  271. package/tests/commands/samples-list.test.ts +0 -43
  272. package/tests/commands/samples-review.test.ts +0 -41
  273. package/tests/commands/task.test.ts +0 -153
  274. package/tests/commands/telemetry.test.ts +0 -190
  275. package/tests/commands/trace.test.ts +0 -316
  276. package/tests/config-reader.test.ts +0 -143
  277. package/tests/e2e/candidate-intake-e2e.test.ts +0 -338
  278. package/tests/e2e/cli-full-flow.test.ts +0 -233
  279. package/tests/e2e/cross-package-acceptance.test.ts +0 -635
  280. package/tests/helpers/pd-cli-path.ts +0 -5
  281. package/tests/pd-cli-smoke.test.ts +0 -13
  282. package/tests/resolve-workspace.test.ts +0 -196
  283. package/tests/services/config-doctor-signal-health.test.ts +0 -109
  284. package/tests/services/console-launcher.test.ts +0 -114
  285. package/tests/services/demo-rule-compiler.test.ts +0 -242
  286. package/tests/services/mainline-snapshot-assembler.test.ts +0 -572
  287. package/tests/services/pd-config-loader.test.ts +0 -505
  288. package/tests/services/proven-channel-baseline-runner.test.ts +0 -30
  289. package/tests/services/quality-scorecard/strong-model-gate.test.ts +0 -133
  290. package/tests/services/resolve-runtime-from-pd-config.test.ts +0 -485
  291. package/tests/services/rulehost-pipeline-e2e.test.ts +0 -538
  292. package/tests/services/rulehost-pipeline-runner.test.ts +0 -1242
  293. package/tests/services/workspace-tool-semantics.test.ts +0 -76
  294. package/tests/utils/path-security.test.ts +0 -180
  295. package/tsconfig.json +0 -26
  296. package/vitest.config.ts +0 -10
@@ -1,793 +0,0 @@
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
-
18
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
19
-
20
- // ─── Mocks ─────────────────────────────────────────────────────────────────
21
-
22
- // Track constructor calls so we can assert which adapter was constructed.
23
- const mockTestDoubleCtor = vi.fn();
24
- const mockPiAiCtor = vi.fn();
25
- const mockOpenClawCliCtor = vi.fn();
26
- const mockL2AgentLoopCtor = vi.fn();
27
- const mockValidateRuntimeConfig = vi.fn();
28
- const mockIsRuntimeConfigError = vi.fn();
29
- const mockBuildL2PrincipleReader = vi.fn();
30
-
31
- vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
32
- const actual = await importOriginal<Record<string, unknown>>();
33
- return {
34
- ...actual,
35
- TestDoubleRuntimeAdapter: vi.fn(function (opts: unknown) {
36
- mockTestDoubleCtor(opts);
37
- return { __type: 'TestDoubleRuntimeAdapter', opts };
38
- }),
39
- PiAiRuntimeAdapter: vi.fn(function (opts: unknown) {
40
- mockPiAiCtor(opts);
41
- return { __type: 'PiAiRuntimeAdapter', opts };
42
- }),
43
- OpenClawCliRuntimeAdapter: vi.fn(function (opts: unknown) {
44
- mockOpenClawCliCtor(opts);
45
- return { __type: 'OpenClawCliRuntimeAdapter', opts };
46
- }),
47
- L2AgentLoopAdapter: vi.fn(function (opts: unknown, deps: unknown) {
48
- mockL2AgentLoopCtor(opts, deps);
49
- return { __type: 'L2AgentLoopAdapter', opts, deps };
50
- }),
51
- validateRuntimeConfig: mockValidateRuntimeConfig,
52
- isRuntimeConfigError: mockIsRuntimeConfigError,
53
- buildL2PrincipleReaderFromLedger: mockBuildL2PrincipleReader.mockReturnValue({
54
- listActivePrinciples: vi.fn(),
55
- }),
56
- };
57
- });
58
-
59
- // PRI-443 Phase 5: loadLedger now imported from
60
- // @principles/core/principle-tree-ledger (I/O module) instead of runtime-v2 barrel
61
- vi.mock('@principles/core/principle-tree-ledger', () => ({
62
- loadLedger: vi.fn().mockReturnValue({ tree: { principles: {} } }),
63
- }));
64
-
65
- const mockLoadPdConfig = vi.fn();
66
- const mockComputeFlagsFromLoadResult = vi.fn();
67
- vi.mock('../pd-config-loader.js', () => ({
68
- loadPdConfig: mockLoadPdConfig,
69
- computeFlagsFromLoadResult: mockComputeFlagsFromLoadResult,
70
- }));
71
-
72
- const mockResolveRuntimeFromPdConfig = vi.fn();
73
- vi.mock('../resolve-runtime-from-pd-config.js', () => ({
74
- resolveRuntimeFromPdConfig: mockResolveRuntimeFromPdConfig,
75
- }));
76
-
77
- // Import AFTER mocks are set up.
78
- const { resolveRuntimeAdapterFromConfig, ConfigResolutionError } = await import('../runtime-adapter-resolver.js');
79
-
80
- // ─── Test Helpers ──────────────────────────────────────────────────────────
81
-
82
- function makeValidPiAiConfig(overrides: Record<string, unknown> = {}): Record<string, unknown> {
83
- return {
84
- ok: true,
85
- runtimeKind: 'pi-ai',
86
- provider: 'openai',
87
- model: 'gpt-4',
88
- apiKeyEnv: 'OPENAI_API_KEY',
89
- baseUrl: undefined,
90
- maxRetries: 3,
91
- timeoutMs: 300000,
92
- agentId: 'main',
93
- ...overrides,
94
- };
95
- }
96
-
97
- function makeValidOpenClawConfig(overrides: Record<string, unknown> = {}): Record<string, unknown> {
98
- return {
99
- ok: true,
100
- runtimeKind: 'openclaw-cli',
101
- openclawMode: 'local',
102
- timeoutMs: 300000,
103
- agentId: 'main',
104
- ...overrides,
105
- };
106
- }
107
-
108
- function makeConfigError(overrides: Record<string, unknown> = {}): Record<string, unknown> {
109
- return {
110
- ok: false,
111
- reason: 'missing-config',
112
- message: 'runtime config not found',
113
- nextAction: 'Create .pd/config.yaml',
114
- ...overrides,
115
- };
116
- }
117
-
118
- function makeResolvedConfig(result: Record<string, unknown>): Record<string, unknown> {
119
- return {
120
- result,
121
- legacyWarnings: [],
122
- configLoadResult: { config: null, source: '.pd/config.yaml' },
123
- configSource: '.pd/config.yaml',
124
- runtimeProfileId: null,
125
- runtimeProfileLabel: null,
126
- };
127
- }
128
-
129
- // ─── Tests ─────────────────────────────────────────────────────────────────
130
-
131
- describe('resolveRuntimeAdapterFromConfig (PRI-431)', () => {
132
- beforeEach(() => {
133
- vi.clearAllMocks();
134
- // Default: isRuntimeConfigError returns false (config is valid)
135
- mockIsRuntimeConfigError.mockReturnValue(false);
136
- // Default: validateRuntimeConfig does nothing (config is valid)
137
- mockValidateRuntimeConfig.mockImplementation(() => {
138
- // no-op: valid config
139
- });
140
- // Default: feature flags have l2_dreamer disabled
141
- mockLoadPdConfig.mockReturnValue({ ok: true, effective: {}, source: 'defaults' });
142
- mockComputeFlagsFromLoadResult.mockReturnValue({
143
- flags: {},
144
- enabledChannels: [],
145
- warnings: [],
146
- });
147
- });
148
-
149
- // ── ConfigResolutionError class ──────────────────────────────────────────
150
-
151
- describe('ConfigResolutionError', () => {
152
- it('has name "ConfigResolutionError" and preserves message + kind + missing + nextAction', () => {
153
- const err = new ConfigResolutionError('boom', 'missing-fields', {
154
- missing: ['provider', 'model'],
155
- nextAction: 'Set provider and model flags.',
156
- });
157
- expect(err).toBeInstanceOf(Error);
158
- expect(err.name).toBe('ConfigResolutionError');
159
- expect(err.message).toBe('boom');
160
- expect(err.kind).toBe('missing-fields');
161
- expect(err.missing).toEqual(['provider', 'model']);
162
- expect(err.nextAction).toBe('Set provider and model flags.');
163
- });
164
-
165
- it('allows missing and nextAction fields to be undefined', () => {
166
- const err = new ConfigResolutionError('boom', 'invalid-config');
167
- expect(err.missing).toBeUndefined();
168
- expect(err.nextAction).toBeUndefined();
169
- });
170
- });
171
-
172
- // ── test-double branch ───────────────────────────────────────────────────
173
-
174
- describe('test-double branch', () => {
175
- it('returns adapter from testDoublePayloadBuilder when allowTestDouble is true', () => {
176
- const fakeAdapter = { __type: 'custom-test-double' };
177
- const builder = vi.fn(() => fakeAdapter as never);
178
-
179
- const result = resolveRuntimeAdapterFromConfig({
180
- runtimeKind: 'test-double',
181
- workspaceDir: '/ws',
182
- allowTestDouble: true,
183
- testDoublePayloadBuilder: builder,
184
- });
185
-
186
- expect(result).toBe(fakeAdapter);
187
- expect(builder).toHaveBeenCalledTimes(1);
188
- expect(builder).toHaveBeenCalledWith(expect.objectContaining({
189
- runtimeKind: 'test-double',
190
- workspaceDir: '/ws',
191
- allowTestDouble: true,
192
- }));
193
- });
194
-
195
- it('throws ConfigResolutionError when allowTestDouble is false', () => {
196
- const builder = vi.fn();
197
-
198
- expect(() => {
199
- resolveRuntimeAdapterFromConfig({
200
- runtimeKind: 'test-double',
201
- workspaceDir: '/ws',
202
- allowTestDouble: false,
203
- testDoublePayloadBuilder: builder,
204
- });
205
- }).toThrow(ConfigResolutionError);
206
-
207
- expect(builder).not.toHaveBeenCalled();
208
- });
209
-
210
- it('throws ConfigResolutionError when allowTestDouble is undefined (default false)', () => {
211
- const builder = vi.fn();
212
-
213
- expect(() => {
214
- resolveRuntimeAdapterFromConfig({
215
- runtimeKind: 'test-double',
216
- workspaceDir: '/ws',
217
- testDoublePayloadBuilder: builder,
218
- });
219
- }).toThrow(ConfigResolutionError);
220
- });
221
- });
222
-
223
- // ── pi-ai branch ─────────────────────────────────────────────────────────
224
-
225
- describe('pi-ai branch', () => {
226
- it('returns PiAiRuntimeAdapter when config is valid', () => {
227
- const config = makeValidPiAiConfig();
228
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
229
-
230
- const result = resolveRuntimeAdapterFromConfig({
231
- runtimeKind: 'pi-ai',
232
- workspaceDir: '/ws',
233
- });
234
-
235
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
236
- expect(mockPiAiCtor).toHaveBeenCalledTimes(1);
237
- expect(mockValidateRuntimeConfig).toHaveBeenCalledWith(config);
238
- });
239
-
240
- it('throws ConfigResolutionError when validateRuntimeConfig throws', () => {
241
- const config = makeValidPiAiConfig();
242
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
243
- mockValidateRuntimeConfig.mockImplementation(() => {
244
- throw new Error('missing provider');
245
- });
246
-
247
- expect(() => {
248
- resolveRuntimeAdapterFromConfig({
249
- runtimeKind: 'pi-ai',
250
- workspaceDir: '/ws',
251
- });
252
- }).toThrow(ConfigResolutionError);
253
- });
254
-
255
- it('CLI timeoutMs override takes precedence over config timeoutMs', () => {
256
- const config = makeValidPiAiConfig({ timeoutMs: 300000 });
257
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
258
-
259
- resolveRuntimeAdapterFromConfig({
260
- runtimeKind: 'pi-ai',
261
- workspaceDir: '/ws',
262
- timeoutMs: 60000,
263
- });
264
-
265
- expect(mockPiAiCtor).toHaveBeenCalledWith(
266
- expect.objectContaining({
267
- timeoutMs: 60000,
268
- }),
269
- );
270
- });
271
-
272
- it('uses config timeoutMs when CLI timeoutMs is not provided', () => {
273
- const config = makeValidPiAiConfig({ timeoutMs: 120000 });
274
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
275
-
276
- resolveRuntimeAdapterFromConfig({
277
- runtimeKind: 'pi-ai',
278
- workspaceDir: '/ws',
279
- });
280
-
281
- expect(mockPiAiCtor).toHaveBeenCalledWith(
282
- expect.objectContaining({
283
- timeoutMs: 120000,
284
- }),
285
- );
286
- });
287
- });
288
-
289
- // ── L2 dreamer sub-branch ────────────────────────────────────────────────
290
-
291
- describe('L2 dreamer sub-branch', () => {
292
- it('returns L2AgentLoopAdapter when runnerKind=dreamer + l2ArtifactReader + l2StateDir + flag enabled', () => {
293
- const config = makeValidPiAiConfig();
294
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
295
- mockComputeFlagsFromLoadResult.mockReturnValue({
296
- flags: { l2_dreamer: { id: 'l2_dreamer', enabled: true, category: 'quiet' } },
297
- enabledChannels: [],
298
- warnings: [],
299
- });
300
- const fakeArtifactReader = { readArtifact: vi.fn() };
301
-
302
- const result = resolveRuntimeAdapterFromConfig({
303
- runtimeKind: 'pi-ai',
304
- workspaceDir: '/ws',
305
- runnerKind: 'dreamer',
306
- l2ArtifactReader: fakeArtifactReader as never,
307
- l2StateDir: '/ws/.principles',
308
- });
309
-
310
- expect(result).toHaveProperty('__type', 'L2AgentLoopAdapter');
311
- expect(mockL2AgentLoopCtor).toHaveBeenCalledTimes(1);
312
- expect(mockBuildL2PrincipleReader).toHaveBeenCalled();
313
- });
314
-
315
- it('falls back to PiAiRuntimeAdapter when l2_dreamer flag is disabled', () => {
316
- const config = makeValidPiAiConfig();
317
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
318
- mockComputeFlagsFromLoadResult.mockReturnValue({
319
- flags: { l2_dreamer: { id: 'l2_dreamer', enabled: false, category: 'quiet' } },
320
- enabledChannels: [],
321
- warnings: [],
322
- });
323
-
324
- const result = resolveRuntimeAdapterFromConfig({
325
- runtimeKind: 'pi-ai',
326
- workspaceDir: '/ws',
327
- runnerKind: 'dreamer',
328
- l2ArtifactReader: { readArtifact: vi.fn() } as never,
329
- l2StateDir: '/ws/.principles',
330
- });
331
-
332
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
333
- expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
334
- });
335
-
336
- it('falls back to PiAiRuntimeAdapter when l2ArtifactReader is missing', () => {
337
- const config = makeValidPiAiConfig();
338
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
339
- mockComputeFlagsFromLoadResult.mockReturnValue({
340
- flags: { l2_dreamer: { id: 'l2_dreamer', enabled: true, category: 'quiet' } },
341
- enabledChannels: [],
342
- warnings: [],
343
- });
344
-
345
- const result = resolveRuntimeAdapterFromConfig({
346
- runtimeKind: 'pi-ai',
347
- workspaceDir: '/ws',
348
- runnerKind: 'dreamer',
349
- l2StateDir: '/ws/.principles',
350
- });
351
-
352
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
353
- expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
354
- });
355
-
356
- it('falls back to PiAiRuntimeAdapter when runnerKind is not dreamer', () => {
357
- const config = makeValidPiAiConfig();
358
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
359
- mockComputeFlagsFromLoadResult.mockReturnValue({
360
- flags: { l2_dreamer: { id: 'l2_dreamer', enabled: true, category: 'quiet' } },
361
- enabledChannels: [],
362
- warnings: [],
363
- });
364
-
365
- const result = resolveRuntimeAdapterFromConfig({
366
- runtimeKind: 'pi-ai',
367
- workspaceDir: '/ws',
368
- runnerKind: 'philosopher',
369
- l2ArtifactReader: { readArtifact: vi.fn() } as never,
370
- l2StateDir: '/ws/.principles',
371
- });
372
-
373
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
374
- expect(mockL2AgentLoopCtor).not.toHaveBeenCalled();
375
- });
376
- });
377
-
378
- // ── openclaw-cli branch ──────────────────────────────────────────────────
379
-
380
- describe('openclaw-cli branch', () => {
381
- it('returns OpenClawCliRuntimeAdapter when openclawMode is provided in config', () => {
382
- const config = makeValidOpenClawConfig({ openclawMode: 'local' });
383
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
384
-
385
- const result = resolveRuntimeAdapterFromConfig({
386
- runtimeKind: 'openclaw-cli',
387
- workspaceDir: '/ws',
388
- });
389
-
390
- expect(result).toHaveProperty('__type', 'OpenClawCliRuntimeAdapter');
391
- expect(mockOpenClawCliCtor).toHaveBeenCalledWith(
392
- expect.objectContaining({
393
- runtimeMode: 'local',
394
- workspaceDir: '/ws',
395
- }),
396
- );
397
- });
398
-
399
- it('throws ConfigResolutionError when openclawMode is missing from config', () => {
400
- const config = makeValidOpenClawConfig({ openclawMode: undefined });
401
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
402
-
403
- expect(() => {
404
- resolveRuntimeAdapterFromConfig({
405
- runtimeKind: 'openclaw-cli',
406
- workspaceDir: '/ws',
407
- });
408
- }).toThrow(ConfigResolutionError);
409
- });
410
- });
411
-
412
- // ── config branch (delegates to resolveRuntimeFromPdConfig) ───────────────
413
-
414
- describe('config branch', () => {
415
- it('delegates to pi-ai when config resolves to pi-ai', () => {
416
- const config = makeValidPiAiConfig();
417
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
418
-
419
- const result = resolveRuntimeAdapterFromConfig({
420
- runtimeKind: 'config',
421
- workspaceDir: '/ws',
422
- });
423
-
424
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
425
- // PRI-719 (+review): the resolver threads the optional per-call options
426
- // (agentName + ignoreAgentEnabled, both undefined when the caller does
427
- // not select a peer stage).
428
- expect(mockResolveRuntimeFromPdConfig).toHaveBeenCalledWith('/ws', { agentName: undefined, ignoreAgentEnabled: undefined });
429
- });
430
-
431
- it('delegates to openclaw-cli when config resolves to openclaw-cli', () => {
432
- const config = makeValidOpenClawConfig();
433
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
434
-
435
- const result = resolveRuntimeAdapterFromConfig({
436
- runtimeKind: 'config',
437
- workspaceDir: '/ws',
438
- });
439
-
440
- expect(result).toHaveProperty('__type', 'OpenClawCliRuntimeAdapter');
441
- });
442
-
443
- it('throws ConfigResolutionError when resolveRuntimeFromPdConfig returns error', () => {
444
- const error = makeConfigError();
445
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
446
- mockIsRuntimeConfigError.mockReturnValue(true);
447
-
448
- expect(() => {
449
- resolveRuntimeAdapterFromConfig({
450
- runtimeKind: 'config',
451
- workspaceDir: '/ws',
452
- });
453
- }).toThrow(ConfigResolutionError);
454
- });
455
- });
456
-
457
- // ── Unsupported runtime ──────────────────────────────────────────────────
458
-
459
- describe('unsupported runtime', () => {
460
- it('throws plain Error for unsupported runtime kind', () => {
461
- expect(() => {
462
- resolveRuntimeAdapterFromConfig({
463
- runtimeKind: 'unsupported-runtime',
464
- workspaceDir: '/ws',
465
- });
466
- }).toThrow(/Unsupported runtime kind/);
467
- });
468
-
469
- it('does not throw ConfigResolutionError for unsupported runtime', () => {
470
- let caught: unknown = null;
471
- try {
472
- resolveRuntimeAdapterFromConfig({
473
- runtimeKind: 'unsupported-runtime',
474
- workspaceDir: '/ws',
475
- });
476
- } catch (err) {
477
- caught = err;
478
- }
479
- expect(caught).not.toBeInstanceOf(ConfigResolutionError);
480
- expect(caught).toBeInstanceOf(Error);
481
- });
482
- });
483
-
484
- // ── PRI-431 Step 1d: New options for diagnose.ts migration ───────────────
485
-
486
- describe('agentId option (openclaw-cli branch)', () => {
487
- it('passes agentId to OpenClawCliRuntimeAdapter when provided', () => {
488
- const config = makeValidOpenClawConfig({ openclawMode: 'local' });
489
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
490
-
491
- resolveRuntimeAdapterFromConfig({
492
- runtimeKind: 'openclaw-cli',
493
- workspaceDir: '/ws',
494
- agentId: 'diagnostician',
495
- });
496
-
497
- expect(mockOpenClawCliCtor).toHaveBeenCalledWith(
498
- expect.objectContaining({
499
- agentId: 'diagnostician',
500
- runtimeMode: 'local',
501
- workspaceDir: '/ws',
502
- }),
503
- );
504
- });
505
-
506
- it('does not pass agentId when omitted (backward compat)', () => {
507
- const config = makeValidOpenClawConfig({ openclawMode: 'local' });
508
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
509
-
510
- resolveRuntimeAdapterFromConfig({
511
- runtimeKind: 'openclaw-cli',
512
- workspaceDir: '/ws',
513
- });
514
-
515
- const [firstCall] = mockOpenClawCliCtor.mock.calls;
516
- expect(firstCall).toBeDefined();
517
- if (!firstCall) throw new Error('expected OpenClawCliRuntimeAdapter constructor call');
518
- const [callArgs] = firstCall;
519
- expect(callArgs).toBeDefined();
520
- if (!callArgs) throw new Error('expected first constructor argument');
521
- expect(callArgs).not.toHaveProperty('agentId');
522
- });
523
- });
524
-
525
- describe('configOptional option (pi-ai branch)', () => {
526
- it('does NOT throw when config returns error and configOptional is true', () => {
527
- const error = makeConfigError();
528
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
529
- mockIsRuntimeConfigError.mockReturnValue(true);
530
-
531
- // Should NOT throw — proceeds with piAiOverrides alone
532
- const result = resolveRuntimeAdapterFromConfig({
533
- runtimeKind: 'pi-ai',
534
- workspaceDir: '/ws',
535
- configOptional: true,
536
- piAiOverrides: {
537
- provider: 'openrouter',
538
- model: 'anthropic/claude-sonnet-4',
539
- apiKeyEnv: 'OPENROUTER_API_KEY',
540
- },
541
- });
542
-
543
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
544
- expect(mockPiAiCtor).toHaveBeenCalledWith(
545
- expect.objectContaining({
546
- provider: 'openrouter',
547
- model: 'anthropic/claude-sonnet-4',
548
- apiKeyEnv: 'OPENROUTER_API_KEY',
549
- }),
550
- );
551
- });
552
-
553
- it('throws ConfigResolutionError with missing-fields when configOptional is true and overrides are incomplete', () => {
554
- const error = makeConfigError();
555
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
556
- mockIsRuntimeConfigError.mockReturnValue(true);
557
-
558
- let caught: unknown = null;
559
- try {
560
- resolveRuntimeAdapterFromConfig({
561
- runtimeKind: 'pi-ai',
562
- workspaceDir: '/ws',
563
- configOptional: true,
564
- piAiOverrides: {
565
- provider: 'openrouter',
566
- // model and apiKeyEnv missing
567
- },
568
- });
569
- } catch (err) {
570
- caught = err;
571
- }
572
-
573
- expect(caught).toBeInstanceOf(ConfigResolutionError);
574
- const err = caught as InstanceType<typeof ConfigResolutionError>;
575
- expect(err.kind).toBe('missing-fields');
576
- expect(err.missing).toEqual(expect.arrayContaining(['model', 'apiKeyEnv']));
577
- });
578
-
579
- it('does NOT call validateRuntimeConfig when configOptional is true and config failed', () => {
580
- const error = makeConfigError();
581
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(error));
582
- mockIsRuntimeConfigError.mockReturnValue(true);
583
-
584
- resolveRuntimeAdapterFromConfig({
585
- runtimeKind: 'pi-ai',
586
- workspaceDir: '/ws',
587
- configOptional: true,
588
- piAiOverrides: {
589
- provider: 'openrouter',
590
- model: 'anthropic/claude-sonnet-4',
591
- apiKeyEnv: 'OPENROUTER_API_KEY',
592
- },
593
- });
594
-
595
- expect(mockValidateRuntimeConfig).not.toHaveBeenCalled();
596
- });
597
-
598
- it('does NOT call validateRuntimeConfig when configOptional is true and config is valid (PR review fix)', () => {
599
- // PR review P1 fix: original diagnose.ts never called validateRuntimeConfig.
600
- // When configOptional=true, skip it entirely to avoid behavior change where
601
- // validateRuntimeConfig would reject configs missing baseUrl for non-built-in
602
- // providers, even when the user passes --baseUrl on the CLI.
603
- const config = makeValidPiAiConfig({ baseUrl: undefined });
604
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
605
- mockIsRuntimeConfigError.mockReturnValue(false);
606
-
607
- resolveRuntimeAdapterFromConfig({
608
- runtimeKind: 'pi-ai',
609
- workspaceDir: '/ws',
610
- configOptional: true,
611
- piAiOverrides: {
612
- provider: 'openrouter',
613
- model: 'anthropic/claude-sonnet-4',
614
- apiKeyEnv: 'OPENROUTER_API_KEY',
615
- baseUrl: 'https://openrouter.ai/api/v1',
616
- },
617
- });
618
-
619
- expect(mockValidateRuntimeConfig).not.toHaveBeenCalled();
620
- expect(mockPiAiCtor).toHaveBeenCalledWith(
621
- expect.objectContaining({
622
- provider: 'openrouter',
623
- baseUrl: 'https://openrouter.ai/api/v1',
624
- }),
625
- );
626
- });
627
-
628
- it('does manual missing-field check on merged values when configOptional is true and config is valid', () => {
629
- // PR review P1 fix: original diagnose.ts always did the manual missing-field check
630
- // on merged values (not just when config failed). When configOptional=true, replicate
631
- // that behavior — check merged provider/model/apiKeyEnv even when config is valid.
632
- const config = makeValidPiAiConfig({ provider: undefined, model: undefined, apiKeyEnv: undefined });
633
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
634
- mockIsRuntimeConfigError.mockReturnValue(false);
635
-
636
- let caught: unknown = null;
637
- try {
638
- resolveRuntimeAdapterFromConfig({
639
- runtimeKind: 'pi-ai',
640
- workspaceDir: '/ws',
641
- configOptional: true,
642
- // No overrides — config has undefined provider/model/apiKeyEnv
643
- });
644
- } catch (err) {
645
- caught = err;
646
- }
647
-
648
- expect(caught).toBeInstanceOf(ConfigResolutionError);
649
- const err = caught as InstanceType<typeof ConfigResolutionError>;
650
- expect(err.kind).toBe('missing-fields');
651
- expect(err.missing).toEqual(expect.arrayContaining(['provider', 'model', 'apiKeyEnv']));
652
- });
653
- });
654
-
655
- describe('validateApiKeyEnv option (pi-ai branch)', () => {
656
- let savedEnv: string | undefined;
657
-
658
- beforeEach(() => {
659
- savedEnv = process.env.TEST_API_KEY_FOR_RESOLVER;
660
- });
661
-
662
- afterEach(() => {
663
- if (savedEnv === undefined) {
664
- delete process.env.TEST_API_KEY_FOR_RESOLVER;
665
- } else {
666
- process.env.TEST_API_KEY_FOR_RESOLVER = savedEnv;
667
- }
668
- });
669
-
670
- it('throws ConfigResolutionError when validateApiKeyEnv is true and env var is unset', () => {
671
- const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
672
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
673
- delete process.env.TEST_API_KEY_FOR_RESOLVER;
674
-
675
- let caught: unknown = null;
676
- try {
677
- resolveRuntimeAdapterFromConfig({
678
- runtimeKind: 'pi-ai',
679
- workspaceDir: '/ws',
680
- validateApiKeyEnv: true,
681
- });
682
- } catch (err) {
683
- caught = err;
684
- }
685
-
686
- expect(caught).toBeInstanceOf(ConfigResolutionError);
687
- const err = caught as InstanceType<typeof ConfigResolutionError>;
688
- expect(err.kind).toBe('invalid-config');
689
- expect(err.message).toContain('TEST_API_KEY_FOR_RESOLVER');
690
- expect(mockPiAiCtor).not.toHaveBeenCalled();
691
- });
692
-
693
- it('creates adapter successfully when validateApiKeyEnv is true and env var is set', () => {
694
- const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
695
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
696
- process.env.TEST_API_KEY_FOR_RESOLVER = 'test-key-value';
697
-
698
- const result = resolveRuntimeAdapterFromConfig({
699
- runtimeKind: 'pi-ai',
700
- workspaceDir: '/ws',
701
- validateApiKeyEnv: true,
702
- });
703
-
704
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
705
- expect(mockPiAiCtor).toHaveBeenCalledTimes(1);
706
- });
707
-
708
- it('does NOT check process.env when validateApiKeyEnv is false (default)', () => {
709
- const config = makeValidPiAiConfig({ apiKeyEnv: 'TEST_API_KEY_FOR_RESOLVER' });
710
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
711
- delete process.env.TEST_API_KEY_FOR_RESOLVER;
712
-
713
- // Should NOT throw even though env var is unset
714
- const result = resolveRuntimeAdapterFromConfig({
715
- runtimeKind: 'pi-ai',
716
- workspaceDir: '/ws',
717
- // validateApiKeyEnv not provided — default false
718
- });
719
-
720
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
721
- });
722
- });
723
-
724
- describe('onConfigResolved callback', () => {
725
- it('calls onConfigResolved with the full resolved object when config is valid', () => {
726
- const config = makeValidPiAiConfig();
727
- const resolved = makeResolvedConfig(config);
728
- mockResolveRuntimeFromPdConfig.mockReturnValue(resolved);
729
-
730
- const callback = vi.fn();
731
- resolveRuntimeAdapterFromConfig({
732
- runtimeKind: 'pi-ai',
733
- workspaceDir: '/ws',
734
- onConfigResolved: callback,
735
- });
736
-
737
- expect(callback).toHaveBeenCalledTimes(1);
738
- expect(callback).toHaveBeenCalledWith(resolved);
739
- });
740
-
741
- it('calls onConfigResolved even when config returns error (if configOptional is true)', () => {
742
- const error = makeConfigError();
743
- const resolved = makeResolvedConfig(error);
744
- mockResolveRuntimeFromPdConfig.mockReturnValue(resolved);
745
- mockIsRuntimeConfigError.mockReturnValue(true);
746
-
747
- const callback = vi.fn();
748
- resolveRuntimeAdapterFromConfig({
749
- runtimeKind: 'pi-ai',
750
- workspaceDir: '/ws',
751
- configOptional: true,
752
- piAiOverrides: {
753
- provider: 'openrouter',
754
- model: 'anthropic/claude-sonnet-4',
755
- apiKeyEnv: 'OPENROUTER_API_KEY',
756
- },
757
- onConfigResolved: callback,
758
- });
759
-
760
- expect(callback).toHaveBeenCalledTimes(1);
761
- expect(callback).toHaveBeenCalledWith(resolved);
762
- });
763
-
764
- it('does NOT call onConfigResolved for test-double branch (no config resolution)', () => {
765
- const callback = vi.fn();
766
- const fakeAdapter = { __type: 'custom-test-double' };
767
- const builder = vi.fn(() => fakeAdapter as never);
768
-
769
- resolveRuntimeAdapterFromConfig({
770
- runtimeKind: 'test-double',
771
- workspaceDir: '/ws',
772
- allowTestDouble: true,
773
- testDoublePayloadBuilder: builder,
774
- onConfigResolved: callback,
775
- });
776
-
777
- expect(callback).not.toHaveBeenCalled();
778
- });
779
-
780
- it('does not throw when onConfigResolved is omitted (backward compat)', () => {
781
- const config = makeValidPiAiConfig();
782
- mockResolveRuntimeFromPdConfig.mockReturnValue(makeResolvedConfig(config));
783
-
784
- // Should not throw
785
- const result = resolveRuntimeAdapterFromConfig({
786
- runtimeKind: 'pi-ai',
787
- workspaceDir: '/ws',
788
- });
789
-
790
- expect(result).toHaveProperty('__type', 'PiAiRuntimeAdapter');
791
- });
792
- });
793
- });