runtime-config.test.ts 25 KB

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