runtime-config.test.ts 26 KB

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