runtime-config.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. import { describe, expect, it } from "vitest";
  2. import { createConfig } from "@zcode/adapters/config";
  3. import { ZCODE_CUA_OFFICIAL_PLUGIN_ID, ZCODE_PLUGIN_ID_ENV_KEY } from "@zcode/shared";
  4. import {
  5. ModelConfig,
  6. ModelOptionSpecsConfig,
  7. ModelPropertiesConfig,
  8. ProviderRegistry,
  9. } from "@zcode/provider";
  10. import {
  11. resolveAppRuntimeConfig as resolveProductionAppRuntimeConfig,
  12. runtimeConfigLogContext,
  13. } from "../src/app/runtime-config.js";
  14. import { createApiKeyProviderConfig } from "./provider-config-fixtures.js";
  15. const workingDirectory = "/workspace/app";
  16. const testModelSelection = {
  17. providerId: "test-provider",
  18. modelId: "test-model",
  19. } as const;
  20. function resolveAppRuntimeConfig(input: Parameters<typeof resolveProductionAppRuntimeConfig>[0]) {
  21. return resolveProductionAppRuntimeConfig({
  22. ...input,
  23. options: {
  24. ...input.options,
  25. runtimeConfig: {
  26. modelSelection: testModelSelection,
  27. ...input.options.runtimeConfig,
  28. },
  29. },
  30. });
  31. }
  32. function createConfigResult() {
  33. return createConfig({
  34. cliOverrides: {
  35. model: {
  36. main: {
  37. provider: "provider:test",
  38. model: "glm-0531",
  39. kind: "anthropic",
  40. baseURL: "http://old.example.test/v1",
  41. },
  42. lite: {
  43. provider: "provider:test",
  44. model: "glm-0531-lite",
  45. kind: "anthropic",
  46. baseURL: "http://old.example.test/v1",
  47. },
  48. },
  49. },
  50. env: {},
  51. skipUserConfig: true,
  52. workingDirectory,
  53. });
  54. }
  55. function createMainOnlyConfigResult() {
  56. return createConfig({
  57. cliOverrides: {
  58. model: {
  59. main: {
  60. provider: "provider:test",
  61. model: "glm-0531",
  62. kind: "anthropic",
  63. baseURL: "http://old.example.test/v1",
  64. },
  65. },
  66. },
  67. env: {},
  68. skipUserConfig: true,
  69. workingDirectory,
  70. });
  71. }
  72. describe("resolveAppRuntimeConfig", () => {
  73. it.each([
  74. {
  75. env: {},
  76. expected: { defaultTimeoutMs: 120_000, maxTimeoutMs: 600_000 },
  77. name: "defaults",
  78. },
  79. {
  80. env: { BASH_DEFAULT_TIMEOUT_MS: "250", BASH_MAX_TIMEOUT_MS: "900" },
  81. expected: { defaultTimeoutMs: 250, maxTimeoutMs: 900 },
  82. name: "valid overrides",
  83. },
  84. {
  85. env: { BASH_DEFAULT_TIMEOUT_MS: "invalid", BASH_MAX_TIMEOUT_MS: "0" },
  86. expected: { defaultTimeoutMs: 120_000, maxTimeoutMs: 600_000 },
  87. name: "invalid overrides",
  88. },
  89. {
  90. env: { BASH_DEFAULT_TIMEOUT_MS: "500000", BASH_MAX_TIMEOUT_MS: "300000" },
  91. expected: { defaultTimeoutMs: 500_000, maxTimeoutMs: 500_000 },
  92. name: "max below default below built-in max",
  93. },
  94. {
  95. env: { BASH_DEFAULT_TIMEOUT_MS: "900000", BASH_MAX_TIMEOUT_MS: "300000" },
  96. expected: { defaultTimeoutMs: 900_000, maxTimeoutMs: 900_000 },
  97. name: "max below default",
  98. },
  99. {
  100. env: { BASH_DEFAULT_TIMEOUT_MS: "700000", BASH_MAX_TIMEOUT_MS: "nope" },
  101. expected: { defaultTimeoutMs: 700_000, maxTimeoutMs: 700_000 },
  102. name: "invalid max above built-in default",
  103. },
  104. {
  105. env: { BASH_DEFAULT_TIMEOUT_MS: "250ms", BASH_MAX_TIMEOUT_MS: "900ms" },
  106. expected: { defaultTimeoutMs: 250, maxTimeoutMs: 900 },
  107. name: "leading decimal digits with unit suffixes",
  108. },
  109. ])("injects the Bash timeout policy from $name env", ({ env, expected }) => {
  110. const resolved = resolveAppRuntimeConfig({
  111. cliStorageRoot: "/tmp/zcode-cli",
  112. configResult: createConfigResult(),
  113. options: { env },
  114. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  115. subagentProfiles: [],
  116. workingDirectory,
  117. });
  118. expect(resolved.runtimeConfig.bashTimeoutPolicy).toEqual(expected);
  119. });
  120. it("preserves an explicitly injected Bash timeout policy", () => {
  121. const resolved = resolveAppRuntimeConfig({
  122. cliStorageRoot: "/tmp/zcode-cli",
  123. configResult: createConfigResult(),
  124. options: {
  125. env: { BASH_DEFAULT_TIMEOUT_MS: "250" },
  126. runtimeConfig: {
  127. bashTimeoutPolicy: { defaultTimeoutMs: 10, maxTimeoutMs: 20 },
  128. },
  129. },
  130. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  131. subagentProfiles: [],
  132. workingDirectory,
  133. });
  134. expect(resolved.runtimeConfig.bashTimeoutPolicy).toEqual({
  135. defaultTimeoutMs: 10,
  136. maxTimeoutMs: 20,
  137. });
  138. });
  139. it("projects canonical memory inputs and supported switches", () => {
  140. const resolved = resolveAppRuntimeConfig({
  141. cliStorageRoot: "/tmp/zcode-cli",
  142. configResult: createConfig({
  143. cliOverrides: {
  144. features: { memory: false },
  145. memory: {
  146. use: false,
  147. },
  148. model: {
  149. main: {
  150. provider: "provider:test",
  151. model: "glm-0531",
  152. kind: "anthropic",
  153. baseURL: "http://old.example.test/v1",
  154. },
  155. },
  156. },
  157. env: {},
  158. skipUserConfig: true,
  159. workingDirectory,
  160. }),
  161. options: {},
  162. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  163. subagentProfiles: [],
  164. workingDirectory,
  165. workspaceIdentity: " ssh:example:/workspace/app ",
  166. });
  167. expect(resolved.runtimeConfig.memory).toEqual({
  168. cliStorageRoot: "/tmp/zcode-cli",
  169. enabled: false,
  170. use: false,
  171. workspaceIdentity: "ssh:example:/workspace/app",
  172. });
  173. });
  174. it("defaults the runtime memory switches to enabled", () => {
  175. const resolved = resolveAppRuntimeConfig({
  176. cliStorageRoot: "/tmp/zcode-cli",
  177. configResult: createConfigResult(),
  178. options: {},
  179. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  180. subagentProfiles: [],
  181. workingDirectory,
  182. });
  183. expect(resolved.runtimeConfig.memory).toEqual({
  184. cliStorageRoot: "/tmp/zcode-cli",
  185. enabled: true,
  186. use: true,
  187. workspaceIdentity: undefined,
  188. });
  189. expect(runtimeConfigLogContext(resolved.runtimeConfig, workingDirectory)).toMatchObject({
  190. memoryExtractionEnabled: true,
  191. });
  192. });
  193. it("preserves an explicit disabled memory Extraction gate and logs it", () => {
  194. const resolved = resolveAppRuntimeConfig({
  195. cliStorageRoot: "/tmp/zcode-cli",
  196. configResult: createConfigResult(),
  197. options: {
  198. runtimeConfig: {
  199. memory: { extractionEnabled: false },
  200. },
  201. },
  202. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  203. subagentProfiles: [],
  204. workingDirectory,
  205. });
  206. expect(resolved.runtimeConfig.memory?.extractionEnabled).toBe(false);
  207. expect(runtimeConfigLogContext(resolved.runtimeConfig, workingDirectory)).toMatchObject({
  208. memoryEnabled: true,
  209. memoryExtractionEnabled: false,
  210. memoryUse: true,
  211. });
  212. });
  213. it("passes the initial Session Selection without recreating ModelSelection state", () => {
  214. const sessionModelSelection = {
  215. providerId: "deepseek-custom",
  216. modelId: "deepseek-v4-pro",
  217. options: { reasoningLevel: "high" },
  218. };
  219. const resolved = resolveAppRuntimeConfig({
  220. cliStorageRoot: "/tmp/zcode-cli",
  221. configResult: createMainOnlyConfigResult(),
  222. options: {
  223. runtimeConfig: {
  224. modelSelection: sessionModelSelection,
  225. workingDirectory,
  226. },
  227. },
  228. workingDirectory,
  229. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  230. subagentProfiles: [],
  231. });
  232. expect(resolved.runtimeConfig.modelSelection).toEqual(sessionModelSelection);
  233. expect(resolved.runtimeConfig).not.toHaveProperty("liteModelSelection");
  234. expect(resolved.runtimeConfig).not.toHaveProperty("liteModelProviderOptions");
  235. });
  236. it("进程 Registry 模式不从通用 RuntimeConfig 补全未知模型", () => {
  237. const configResult = createMainOnlyConfigResult();
  238. const resolved = resolveAppRuntimeConfig({
  239. cliStorageRoot: "/tmp/zcode-cli",
  240. configResult,
  241. options: {
  242. providerRegistry: new ProviderRegistry([]),
  243. runtimeConfig: {
  244. modelSelection: {
  245. providerId: "legacy-provider",
  246. modelId: "legacy-model",
  247. },
  248. workingDirectory,
  249. },
  250. },
  251. workingDirectory,
  252. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  253. subagentProfiles: [],
  254. });
  255. expect(resolved.runtimeConfig.contextWindow).toBeUndefined();
  256. expect(resolved.runtimeConfig.maxOutputTokens).toBeUndefined();
  257. });
  258. it("keeps default title generation bound to the current runtime model", () => {
  259. const resolved = resolveAppRuntimeConfig({
  260. cliStorageRoot: "/tmp/zcode-cli",
  261. configResult: createConfigResult(),
  262. options: {
  263. runtimeConfig: {
  264. titleGeneration: {},
  265. workingDirectory,
  266. },
  267. },
  268. workingDirectory,
  269. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  270. subagentProfiles: [],
  271. });
  272. expect(resolved.runtimeConfig.titleGeneration).toEqual({ timeoutMs: 60_000 });
  273. });
  274. it("preserves an explicit title generation timeout override", () => {
  275. const resolved = resolveAppRuntimeConfig({
  276. cliStorageRoot: "/tmp/zcode-cli",
  277. configResult: createConfigResult(),
  278. options: {
  279. runtimeConfig: {
  280. titleGeneration: {
  281. timeoutMs: 30_000,
  282. },
  283. workingDirectory,
  284. },
  285. },
  286. workingDirectory,
  287. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  288. subagentProfiles: [],
  289. });
  290. expect(resolved.runtimeConfig.titleGeneration?.timeoutMs).toBe(30_000);
  291. });
  292. it("keeps model format facts on Registry Models instead of Runtime Config", () => {
  293. const providerRegistry = new ProviderRegistry([
  294. {
  295. providerId: "provider:test",
  296. config: createApiKeyProviderConfig({
  297. apiFormat: "anthropic-messages",
  298. apiKey: "test-key",
  299. baseURL: "https://registry.example.test",
  300. models: ["glm-0531"],
  301. }),
  302. models: [
  303. {
  304. modelId: "glm-0531",
  305. config: new ModelConfig({
  306. properties: new ModelPropertiesConfig({
  307. requiresMfjsToolSchema: false,
  308. contextWindow: 128_000,
  309. inputFormat: {
  310. supportsText: true,
  311. supportsImage: false,
  312. supportsVideo: false,
  313. supportsAudio: false,
  314. supportsPdf: true,
  315. },
  316. outputFormat: { supportsText: true },
  317. supportsToolCall: true,
  318. supportsJsonSchemaOutput: true,
  319. supportsNativeWebSearch: false,
  320. supportsMidConversationSystem: true,
  321. }),
  322. optionSpecs: new ModelOptionSpecsConfig({
  323. maxOutputTokens: {
  324. max: 32_000,
  325. map: '{"max_tokens":maxOutputTokens}',
  326. },
  327. }),
  328. }),
  329. },
  330. ],
  331. },
  332. ]);
  333. const resolved = resolveAppRuntimeConfig({
  334. cliStorageRoot: "/tmp/zcode-cli",
  335. configResult: createConfigResult(),
  336. options: { providerRegistry },
  337. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  338. subagentProfiles: [],
  339. workingDirectory,
  340. });
  341. expect(resolved.runtimeConfig).not.toHaveProperty("modelInputMediaCapabilities");
  342. });
  343. it("projects explicit session tool filters into the runtime tool surface", () => {
  344. // Explicit filters come from ZCode Protocol session/create or the headless CLI
  345. // flags; they are the only inputs that shape the provider-visible tool surface.
  346. const configResult = createConfig({
  347. cliOverrides: {
  348. model: {
  349. main: {
  350. provider: "provider:test",
  351. model: "glm-0531",
  352. kind: "anthropic",
  353. baseURL: "http://old.example.test/v1",
  354. },
  355. },
  356. },
  357. env: {},
  358. skipUserConfig: true,
  359. workingDirectory,
  360. });
  361. const resolved = resolveAppRuntimeConfig({
  362. cliStorageRoot: "/tmp/zcode-cli",
  363. configResult,
  364. options: {
  365. runtimeConfig: {
  366. toolAllowlist: ["mcp__zcode-cua__open_application"],
  367. toolDisallowlist: ["TodoWrite"],
  368. workingDirectory,
  369. },
  370. },
  371. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  372. subagentProfiles: [],
  373. workingDirectory,
  374. });
  375. expect(resolved.runtimeConfig.toolAllowlist).toEqual(["mcp__zcode-cua__open_application"]);
  376. expect(resolved.runtimeConfig.toolDisallowlist).toEqual(["TodoWrite"]);
  377. });
  378. it("does not project config permission.allowedTools into the runtime tool surface", () => {
  379. // permission.allowedTools/disallowedTools keep their original semantics
  380. // (auto-approve / execution-time deny). Projecting them into tool
  381. // registration would silently hide every other tool for existing configs.
  382. const configResult = createConfig({
  383. cliOverrides: {
  384. model: {
  385. main: {
  386. provider: "provider:test",
  387. model: "glm-0531",
  388. kind: "anthropic",
  389. baseURL: "http://old.example.test/v1",
  390. },
  391. },
  392. permission: {
  393. allowedTools: ["WebSearch"],
  394. disallowedTools: ["TodoWrite"],
  395. },
  396. },
  397. env: {},
  398. skipUserConfig: true,
  399. workingDirectory,
  400. });
  401. const resolved = resolveAppRuntimeConfig({
  402. cliStorageRoot: "/tmp/zcode-cli",
  403. configResult,
  404. options: {
  405. runtimeConfig: {
  406. workingDirectory,
  407. },
  408. },
  409. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  410. subagentProfiles: [],
  411. workingDirectory,
  412. });
  413. expect(resolved.runtimeConfig.toolAllowlist).toBeUndefined();
  414. expect(resolved.runtimeConfig.toolDisallowlist).toBeUndefined();
  415. });
  416. it("preserves an explicit title generation model override", () => {
  417. const titleModelSelection = {
  418. providerId: "builtin:title-provider",
  419. modelId: "title-model",
  420. options: { reasoningLevel: "high" },
  421. };
  422. const resolved = resolveAppRuntimeConfig({
  423. cliStorageRoot: "/tmp/zcode-cli",
  424. configResult: createConfigResult(),
  425. options: {
  426. runtimeConfig: {
  427. titleGeneration: {
  428. modelSelection: titleModelSelection,
  429. },
  430. workingDirectory,
  431. },
  432. },
  433. workingDirectory,
  434. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  435. subagentProfiles: [],
  436. });
  437. expect(resolved.runtimeConfig.titleGeneration?.modelSelection).toEqual(titleModelSelection);
  438. });
  439. it("configures the explicit internal embedded search compatibility backend", () => {
  440. const resolved = resolveAppRuntimeConfig({
  441. cliStorageRoot: "/tmp/zcode-cli",
  442. configResult: createConfigResult(),
  443. options: {
  444. env: {
  445. ZCODE_EMBEDDED_SEARCH_COMMAND: "/tmp/zcode-test",
  446. },
  447. },
  448. workingDirectory,
  449. });
  450. expect(resolved.runtimeConfig.embeddedSearchBackend).toEqual({
  451. kind: "internal-cli",
  452. command: "/tmp/zcode-test",
  453. args: ["__internal-search"],
  454. });
  455. });
  456. it("preserves an explicit embedded search backend override", () => {
  457. const backend = {
  458. kind: "argv0-dispatch" as const,
  459. command: "/tmp/zcode-argv0",
  460. };
  461. const resolved = resolveAppRuntimeConfig({
  462. cliStorageRoot: "/tmp/zcode-cli",
  463. configResult: createConfigResult(),
  464. options: {
  465. runtimeConfig: {
  466. embeddedSearchBackend: backend,
  467. workingDirectory,
  468. },
  469. },
  470. workingDirectory,
  471. });
  472. expect(resolved.runtimeConfig.embeddedSearchBackend).toBe(backend);
  473. });
  474. it("enables runtime hooks when enabled plugins provide hooks", () => {
  475. const resolved = resolveAppRuntimeConfig({
  476. cliStorageRoot: "/tmp/zcode-cli",
  477. configResult: createConfigResult(),
  478. options: {},
  479. subagentOutputRootDir: "/tmp/zcode-cli/agents",
  480. subagentProfiles: [],
  481. pluginHooks: {
  482. SessionStart: [
  483. {
  484. matcher: "startup",
  485. hooks: [
  486. {
  487. type: "command",
  488. command: "echo hook",
  489. plugin: {
  490. dataPath: "/tmp/plugin-data",
  491. id: "superpowers@zcode-plugins-official",
  492. name: "superpowers",
  493. rootPath: "/tmp/superpowers",
  494. },
  495. },
  496. ],
  497. },
  498. ],
  499. },
  500. workingDirectory,
  501. });
  502. expect(resolved.runtimeConfig.hooks?.enabled).toBe(true);
  503. expect(resolved.runtimeConfig.hooks?.events.SessionStart?.[0]?.hooks[0]).toMatchObject({
  504. command: "echo hook",
  505. type: "command",
  506. });
  507. });
  508. it("merges plugin MCP servers when protocol provides user MCP overrides", () => {
  509. const resolved = resolveAppRuntimeConfig({
  510. cliStorageRoot: "/tmp/zcode-cli",
  511. configResult: createConfigResult(),
  512. options: {
  513. runtimeConfig: {
  514. mcp: {
  515. enabled: true,
  516. servers: {
  517. context7: {
  518. type: "stdio",
  519. command: "npx",
  520. args: ["-y", "@upstash/context7-mcp"],
  521. },
  522. },
  523. },
  524. workingDirectory,
  525. },
  526. },
  527. pluginMcpServers: {
  528. "plugin:chrome-devtools-mcp:chrome-devtools": {
  529. type: "stdio",
  530. command: "npx",
  531. args: ["-y", "chrome-devtools-mcp@latest"],
  532. },
  533. },
  534. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  535. subagentProfiles: [],
  536. workingDirectory,
  537. });
  538. expect(resolved.configuredMcpServers).toEqual({
  539. "plugin:chrome-devtools-mcp:chrome-devtools": {
  540. type: "stdio",
  541. command: "npx",
  542. args: ["-y", "chrome-devtools-mcp@latest"],
  543. },
  544. context7: {
  545. type: "stdio",
  546. command: "npx",
  547. args: ["-y", "@upstash/context7-mcp"],
  548. },
  549. });
  550. expect(resolved.runtimeConfig.mcp?.servers).toEqual(resolved.configuredMcpServers);
  551. });
  552. it("keeps official CUA provenance only while the bundled plugin config is unmodified", () => {
  553. const name = "plugin:computer-use:computer-use";
  554. const officialPluginConfig = {
  555. type: "stdio" as const,
  556. command: "/Applications/ZCode Computer Use.app/Contents/MacOS/server",
  557. env: { [ZCODE_PLUGIN_ID_ENV_KEY]: ZCODE_CUA_OFFICIAL_PLUGIN_ID },
  558. };
  559. const baseInput = {
  560. cliStorageRoot: "/tmp/zcode-cli",
  561. configResult: createConfigResult(),
  562. pluginMcpServers: { [name]: officialPluginConfig },
  563. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  564. subagentProfiles: [],
  565. workingDirectory,
  566. };
  567. const official = resolveAppRuntimeConfig({
  568. ...baseInput,
  569. options: { runtimeConfig: { mcp: { enabled: true } }, workingDirectory },
  570. });
  571. expect(official.runtimeConfig.mcp?.trustedOfficialCuaServerNames).toEqual([name]);
  572. const projectSpoof = resolveAppRuntimeConfig({
  573. ...baseInput,
  574. options: {
  575. runtimeConfig: {
  576. mcp: {
  577. enabled: true,
  578. servers: {
  579. [name]: {
  580. type: "stdio",
  581. command: "/tmp/attacker-command",
  582. env: { [ZCODE_PLUGIN_ID_ENV_KEY]: ZCODE_CUA_OFFICIAL_PLUGIN_ID },
  583. },
  584. },
  585. },
  586. workingDirectory,
  587. },
  588. },
  589. });
  590. expect(projectSpoof.runtimeConfig.mcp?.trustedOfficialCuaServerNames).toEqual([]);
  591. expect(projectSpoof.configuredMcpServers[name]).toMatchObject({
  592. command: "/tmp/attacker-command",
  593. });
  594. });
  595. it("keeps the host node_repl identity reserved from user MCP overrides", () => {
  596. const builtInNodeRepl = {
  597. type: "stdio" as const,
  598. command: process.execPath,
  599. args: ["/runtime/node-repl.js"],
  600. };
  601. const resolved = resolveAppRuntimeConfig({
  602. builtInMcpServers: { node_repl: builtInNodeRepl },
  603. cliStorageRoot: "/tmp/zcode-cli",
  604. configResult: createConfigResult(),
  605. options: {
  606. runtimeConfig: {
  607. mcp: {
  608. enabled: true,
  609. servers: {
  610. node_repl: { type: "stdio", command: "malicious-node-repl" },
  611. },
  612. },
  613. workingDirectory,
  614. },
  615. },
  616. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  617. workingDirectory,
  618. });
  619. expect(resolved.configuredMcpServers.node_repl).toEqual(builtInNodeRepl);
  620. });
  621. it("injects CUA bridge credentials without granting node_repl official-frame authority", () => {
  622. const resolved = resolveAppRuntimeConfig({
  623. builtInMcpServers: {
  624. node_repl: { type: "stdio", command: process.execPath, args: ["/runtime/node-repl.js"] },
  625. },
  626. cliStorageRoot: "/tmp/zcode-cli",
  627. configResult: createConfigResult(),
  628. options: { runtimeConfig: { mcp: { enabled: true } }, workingDirectory },
  629. pluginRuntimeFeatures: { computerUse: true },
  630. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  631. workingDirectory,
  632. });
  633. // node_repl 仍可被 bridge 注入,但不能把通用 JS server 标成 official CUA server。
  634. expect(resolved.runtimeConfig.mcp?.trustedOfficialCuaServerNames).toEqual([]);
  635. });
  636. it("merges loaded subagent profiles into runtime config", () => {
  637. const resolved = resolveAppRuntimeConfig({
  638. cliStorageRoot: "/tmp/zcode-cli",
  639. configResult: createConfigResult(),
  640. options: {
  641. runtimeConfig: {
  642. subagents: {
  643. profiles: [
  644. {
  645. name: "inline-agent",
  646. description: "内联 agent",
  647. source: "project",
  648. systemPrompt: "内联 prompt",
  649. },
  650. ],
  651. },
  652. },
  653. },
  654. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  655. subagentProfiles: [
  656. {
  657. name: "loaded-agent",
  658. description: "加载 agent",
  659. source: "user",
  660. systemPrompt: "加载 prompt",
  661. },
  662. ],
  663. workingDirectory,
  664. });
  665. expect(resolved.runtimeConfig.subagents?.outputRootDir).toBe("/tmp/zcode-cli/agents-output");
  666. expect(resolved.runtimeConfig.subagents?.profiles?.map((profile) => profile.name)).toEqual([
  667. "inline-agent",
  668. "loaded-agent",
  669. ]);
  670. });
  671. it("merges built-in subagent model overrides with explicit runtime config taking precedence", () => {
  672. const resolved = resolveAppRuntimeConfig({
  673. cliStorageRoot: "/tmp/zcode-cli",
  674. configResult: createConfigResult(),
  675. builtInSubagentModelSelectionOverrides: {
  676. Explore: {
  677. providerId: "custom-openai",
  678. modelId: "glm-5.2",
  679. options: { reasoningLevel: "max" },
  680. },
  681. "general-purpose": {
  682. providerId: "custom-openai",
  683. modelId: "gpt-5.4",
  684. options: { reasoningLevel: "high" },
  685. },
  686. },
  687. options: {
  688. runtimeConfig: {
  689. subagents: {
  690. builtInModelSelectionOverrides: {
  691. Explore: { providerId: "custom-openai", modelId: "explicit-explore" },
  692. },
  693. },
  694. },
  695. },
  696. subagentOutputRootDir: "/tmp/zcode-cli/agents-output",
  697. subagentProfiles: [],
  698. workingDirectory,
  699. });
  700. expect(resolved.runtimeConfig.subagents?.builtInModelSelectionOverrides).toEqual({
  701. Explore: { providerId: "custom-openai", modelId: "explicit-explore" },
  702. "general-purpose": {
  703. providerId: "custom-openai",
  704. modelId: "gpt-5.4",
  705. options: { reasoningLevel: "high" },
  706. },
  707. });
  708. });
  709. });
  710. // 灰度门(docs/dynamic-workflow/launch.md「Gray release」)由协议服务端写进 create-app
  711. // 的 options.runtimeConfig;本函数必须原样透传,不得补默认值也不得被后面的显式键覆盖。
  712. describe("resolveAppRuntimeConfig:dynamicWorkflowEnabled 透传", () => {
  713. it.each([
  714. { input: { dynamicWorkflowEnabled: false }, expected: false },
  715. { input: { dynamicWorkflowEnabled: true }, expected: true },
  716. // 未参与灰度的调用方(TUI / headless)不写该字段,装配层也不能替它决定。
  717. { input: {}, expected: undefined },
  718. ])("%j", ({ input, expected }) => {
  719. const { runtimeConfig } = resolveAppRuntimeConfig({
  720. cliStorageRoot: "/tmp/zcode-cli-storage",
  721. configResult: createConfigResult(),
  722. options: { runtimeConfig: input },
  723. subagentOutputRootDir: "/tmp/zcode-subagent-output",
  724. workingDirectory,
  725. });
  726. expect(runtimeConfig.dynamicWorkflowEnabled).toBe(expected);
  727. });
  728. // launch.md「On demand: activation」:按需标志同样原样透传,装配层不替调用方决定。
  729. it.each([
  730. { input: { dynamicWorkflowToolsOnDemand: true }, expected: true },
  731. { input: { dynamicWorkflowToolsOnDemand: false }, expected: false },
  732. { input: {}, expected: undefined },
  733. ])("dynamicWorkflowToolsOnDemand %j", ({ input, expected }) => {
  734. const { runtimeConfig } = resolveAppRuntimeConfig({
  735. cliStorageRoot: "/tmp/zcode-cli-storage",
  736. configResult: createConfigResult(),
  737. options: { runtimeConfig: input },
  738. subagentOutputRootDir: "/tmp/zcode-subagent-output",
  739. workingDirectory,
  740. });
  741. expect(runtimeConfig.dynamicWorkflowToolsOnDemand).toBe(expected);
  742. });
  743. });