subagents.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { discoverNodePlugins } from "@zcode/adapters";
  6. import {
  7. loadPluginAgentTemplates,
  8. resolvePluginAgentProfiles,
  9. loadZCodeAgentProfiles,
  10. } from "../src/subagents.js";
  11. import { loadPluginAgentProfiles } from "./helpers/subagents.js";
  12. function pluginMetadata(input: {
  13. enabled?: boolean;
  14. id: string;
  15. marketplace?: string;
  16. name: string;
  17. rootPath: string;
  18. }) {
  19. return {
  20. commandRootCount: 0,
  21. components: [
  22. {
  23. kind: "agent" as const,
  24. items: [
  25. { name: "code-architect", path: join(input.rootPath, "agents", "code-architect.md") },
  26. ],
  27. },
  28. ],
  29. dataPath: join(input.rootPath, ".data"),
  30. declaredMcpServerNames: [],
  31. enabled: input.enabled ?? true,
  32. id: input.id,
  33. manifestPath: join(input.rootPath, ".claude-plugin", "plugin.json"),
  34. marketplace: input.marketplace ?? "claude-plugins-official",
  35. mcpServerNames: [],
  36. name: input.name,
  37. hookDetails: [],
  38. rootPath: input.rootPath,
  39. skillCount: 0,
  40. skillRootCount: 0,
  41. source: "cache" as const,
  42. version: "0.0.0",
  43. };
  44. }
  45. describe("loadZCodeAgentProfiles", () => {
  46. it("一次读取迁移后的插件覆盖注入规范名和别名,不修改插件文件或串用另一插件覆盖", async () => {
  47. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-selection-"));
  48. try {
  49. const storageRoot = join(root, "state");
  50. const pluginRoot = join(root, "plugin");
  51. const agentId = "plugin:fixture@personal:code-architect";
  52. await mkdir(join(storageRoot, "v2"), { recursive: true });
  53. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  54. const path = join(pluginRoot, "agents", "code-architect.md");
  55. const markdown =
  56. "---\nname: code-architect\ndescription: Architect\nmodel: custom:p:old\nthoughtLevel: low\n---\nPrompt";
  57. await writeFile(path, markdown);
  58. await writeFile(
  59. join(storageRoot, "v2", "agents-state.json"),
  60. JSON.stringify({
  61. pluginAgentModelOverrides: { [agentId]: "custom:builtin%3Azai-coding-plan:GLM" },
  62. pluginAgentThoughtLevelOverrides: { [agentId]: "high" },
  63. }),
  64. );
  65. const state = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
  66. const selection = {
  67. providerId: "account:zai-individual-coding-plan",
  68. modelId: "GLM",
  69. options: { reasoningLevel: "high" },
  70. };
  71. expect(state.pluginAgentModelSelectionOverrides).toEqual({ [agentId]: selection });
  72. const plugins = [
  73. pluginMetadata({ id: "fixture@personal", name: "fixture", rootPath: pluginRoot }),
  74. ];
  75. const templates = loadPluginAgentTemplates({ plugins });
  76. const result = resolvePluginAgentProfiles(
  77. { plugins, modelSelectionOverrides: state.pluginAgentModelSelectionOverrides },
  78. templates,
  79. );
  80. // 保存后只组合已读取模板;清除覆盖恢复插件原值,新增自定义同名定义移除裸名别名。
  81. await rm(path);
  82. const restored = resolvePluginAgentProfiles(
  83. { plugins, reservedProfileNames: ["code-architect"] },
  84. templates,
  85. );
  86. expect(restored.profiles).toHaveLength(1);
  87. expect(restored.profiles[0]?.modelSelection).toEqual({
  88. providerId: "p",
  89. modelId: "old",
  90. options: { reasoningLevel: "low" },
  91. });
  92. await writeFile(path, markdown);
  93. expect(result.profiles.map((p) => [p.name, p.modelSelection])).toEqual([
  94. ["fixture:code-architect", selection],
  95. ["code-architect", selection],
  96. ]);
  97. const modelOnly = { providerId: "p", modelId: "new" };
  98. expect(
  99. loadPluginAgentProfiles({ plugins, modelSelectionOverrides: { [agentId]: modelOnly } })
  100. .profiles[0]?.modelSelection,
  101. ).toEqual(modelOnly);
  102. expect(
  103. loadPluginAgentProfiles({
  104. plugins,
  105. modelSelectionOverrides: { "plugin:other@personal:code-architect": selection },
  106. }).profiles[0]?.modelSelection,
  107. ).toEqual({
  108. providerId: "p",
  109. modelId: "old",
  110. options: { reasoningLevel: "low" },
  111. });
  112. expect(await readFile(path, "utf8")).toBe(markdown);
  113. } finally {
  114. await rm(root, { recursive: true, force: true });
  115. }
  116. });
  117. it("loads only direct user and project Markdown files from beta-compatible roots", async () => {
  118. const root = await mkdtemp(join(tmpdir(), "zcode-subagents-"));
  119. const storageRoot = join(root, "home");
  120. const workingDirectory = join(root, "workspace");
  121. try {
  122. await mkdir(join(storageRoot, "agents"), { recursive: true });
  123. await mkdir(join(workingDirectory, ".zcode", "agents"), { recursive: true });
  124. for (const agentsRoot of [
  125. join(storageRoot, "agents"),
  126. join(workingDirectory, ".zcode", "agents"),
  127. ]) {
  128. await mkdir(join(agentsRoot, "nested"));
  129. await writeFile(
  130. join(agentsRoot, "nested", "ignored.markdown"),
  131. "---\nname: nested-reviewer\ndescription: Nested\n---\nNested prompt",
  132. );
  133. }
  134. await writeFile(
  135. join(storageRoot, "agents", "reviewer.md"),
  136. `---
  137. name: zcode-reviewer
  138. description: 用户级中文审查 agent
  139. ---
  140. 用户级 prompt`,
  141. "utf8",
  142. );
  143. await writeFile(
  144. join(workingDirectory, ".zcode", "agents", "reviewer.md"),
  145. `---
  146. name: zcode-reviewer
  147. description: 项目级中文审查 agent
  148. ---
  149. 项目级 prompt`,
  150. "utf8",
  151. );
  152. const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  153. expect(result.diagnostics).toEqual([]);
  154. expect(result.profiles.map((profile) => profile.source)).toEqual(["user", "project"]);
  155. expect(result.profiles.map((profile) => profile.description)).toEqual([
  156. "用户级中文审查 agent",
  157. "项目级中文审查 agent",
  158. ]);
  159. } finally {
  160. await rm(root, { force: true, recursive: true });
  161. }
  162. });
  163. it("ignores project Markdown permissionMode because project profiles are untrusted", async () => {
  164. const root = await mkdtemp(join(tmpdir(), "zcode-subagents-project-permission-"));
  165. const storageRoot = join(root, "home");
  166. const workingDirectory = join(root, "workspace");
  167. try {
  168. await mkdir(join(storageRoot, "agents"), { recursive: true });
  169. await mkdir(join(workingDirectory, ".zcode", "agents"), { recursive: true });
  170. await writeFile(
  171. join(storageRoot, "agents", "trusted.md"),
  172. `---
  173. name: trusted-runner
  174. description: 用户级运行 agent
  175. permissionMode: bypassPermissions
  176. ---
  177. 用户级 prompt`,
  178. "utf8",
  179. );
  180. await writeFile(
  181. join(workingDirectory, ".zcode", "agents", "attacker.md"),
  182. `---
  183. name: attacker-runner
  184. description: 项目级运行 agent
  185. permissionMode: bypassPermissions
  186. ---
  187. 项目级 prompt`,
  188. "utf8",
  189. );
  190. const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  191. const userProfile = result.profiles.find((profile) => profile.name === "trusted-runner");
  192. const projectProfile = result.profiles.find((profile) => profile.name === "attacker-runner");
  193. expect(userProfile?.permissionMode).toBe("bypassPermissions");
  194. expect(projectProfile?.permissionMode).toBeUndefined();
  195. } finally {
  196. await rm(root, { force: true, recursive: true });
  197. }
  198. });
  199. it("reports diagnostics for invalid agent Markdown without failing the load", async () => {
  200. const root = await mkdtemp(join(tmpdir(), "zcode-subagents-invalid-"));
  201. const storageRoot = join(root, "home");
  202. const workingDirectory = join(root, "workspace");
  203. try {
  204. await mkdir(join(storageRoot, "agents"), { recursive: true });
  205. await writeFile(
  206. join(storageRoot, "agents", "invalid.md"),
  207. `---
  208. name: missing-description
  209. ---
  210. prompt`,
  211. "utf8",
  212. );
  213. const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  214. expect(result.profiles).toEqual([]);
  215. expect(result.diagnostics).toHaveLength(1);
  216. expect(result.diagnostics[0]?.code).toBe("agent_missing_required_frontmatter");
  217. } finally {
  218. await rm(root, { force: true, recursive: true });
  219. }
  220. });
  221. it("skips disabled user Markdown profiles from shared subagent state", async () => {
  222. const root = await mkdtemp(join(tmpdir(), "zcode-subagents-disabled-"));
  223. const storageRoot = join(root, "home");
  224. const workingDirectory = join(root, "workspace");
  225. try {
  226. await mkdir(join(storageRoot, "agents"), { recursive: true });
  227. await mkdir(join(storageRoot, "v2"), { recursive: true });
  228. await writeFile(
  229. join(storageRoot, "agents", "reviewer.md"),
  230. `---
  231. name: zcode-reviewer
  232. description: 用户级中文审查 agent
  233. ---
  234. 用户级 prompt`,
  235. "utf8",
  236. );
  237. await writeFile(
  238. join(storageRoot, "agents", "runner.md"),
  239. `---
  240. name: zcode-runner
  241. description: 用户级运行 agent
  242. ---
  243. 用户级 prompt`,
  244. "utf8",
  245. );
  246. await writeFile(
  247. join(storageRoot, "v2", "agents-state.json"),
  248. JSON.stringify({ disabledAgentIds: ["user:user:zcode-reviewer"] }),
  249. "utf8",
  250. );
  251. const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  252. expect(result.diagnostics).toEqual([]);
  253. expect(result.profiles.map((profile) => profile.name)).toEqual(["zcode-runner"]);
  254. } finally {
  255. await rm(root, { force: true, recursive: true });
  256. }
  257. });
  258. it("loads structured built-in model selections from shared subagent state", async () => {
  259. const root = await mkdtemp(join(tmpdir(), "zcode-subagents-built-in-models-"));
  260. const storageRoot = join(root, "home");
  261. const workingDirectory = join(root, "workspace");
  262. try {
  263. await mkdir(join(storageRoot, "v2"), { recursive: true });
  264. await writeFile(
  265. join(storageRoot, "v2", "agents-state.json"),
  266. JSON.stringify({
  267. builtInModelSelectionOverrides: {
  268. Explore: {
  269. providerId: "custom-openai",
  270. modelId: "glm-5.2",
  271. options: { reasoningLevel: "max" },
  272. },
  273. "general-purpose": {
  274. providerId: "custom-openai",
  275. modelId: "gpt-5.4",
  276. options: { reasoningLevel: "high" },
  277. },
  278. ignored: {
  279. providerId: "custom-openai",
  280. modelId: "ignored",
  281. },
  282. },
  283. disabledAgentIds: [],
  284. }),
  285. "utf8",
  286. );
  287. const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  288. expect(result.diagnostics).toEqual([]);
  289. expect(result.profiles).toEqual([]);
  290. expect(result.builtInModelSelectionOverrides).toEqual({
  291. Explore: {
  292. providerId: "custom-openai",
  293. modelId: "glm-5.2",
  294. options: { reasoningLevel: "max" },
  295. },
  296. "general-purpose": {
  297. providerId: "custom-openai",
  298. modelId: "gpt-5.4",
  299. options: { reasoningLevel: "high" },
  300. },
  301. });
  302. } finally {
  303. await rm(root, { force: true, recursive: true });
  304. }
  305. });
  306. });
  307. describe("loadPluginAgentProfiles", () => {
  308. it.each([
  309. ["agents", "profile.md"],
  310. ["custom-agents", "profile.md"],
  311. ["agents", "code-architect.md"],
  312. ])("loads the discovered source from %s/%s by frontmatter name", async (directory, fileName) => {
  313. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-agent-source-"));
  314. try {
  315. await mkdir(join(root, ".zcode-plugin"));
  316. await writeFile(
  317. join(root, ".zcode-plugin", "plugin.json"),
  318. JSON.stringify({ name: "fixture", agents: directory }),
  319. );
  320. await mkdir(join(root, directory));
  321. const path = join(root, directory, fileName);
  322. await writeFile(
  323. path,
  324. "---\nname: code-architect\ndescription: Source profile\n---\nSource prompt",
  325. );
  326. const { plugins } = await discoverNodePlugins({
  327. config: { enabled: true, dirs: [root], enabledPlugins: {}, options: {} },
  328. env: {},
  329. storageRoot: join(root, "storage"),
  330. workingDirectory: root,
  331. });
  332. const result = loadPluginAgentProfiles({ plugins });
  333. expect(result.diagnostics).toEqual([]);
  334. expect(
  335. result.profiles.map((profile) => [profile.name, profile.path, profile.systemPrompt]),
  336. ).toEqual([
  337. ["fixture:code-architect", path, "Source prompt"],
  338. ["code-architect", path, "Source prompt"],
  339. ]);
  340. expect(result.profiles.find((profile) => profile.name === "fixture:missing")).toBeUndefined();
  341. await rm(path);
  342. const missing = loadPluginAgentProfiles({ plugins });
  343. expect(missing.profiles).toEqual([]);
  344. expect(missing.diagnostics).toMatchObject([{ code: "agent_read_failed", path }]);
  345. } finally {
  346. await rm(root, { force: true, recursive: true });
  347. }
  348. });
  349. it("does not guess a source path for description-only components", async () => {
  350. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-agent-no-source-"));
  351. try {
  352. await mkdir(join(root, "agents"));
  353. await writeFile(
  354. join(root, "agents", "code-architect.md"),
  355. "---\nname: code-architect\ndescription: Undiscovered\n---\nDo not load",
  356. );
  357. const plugin = {
  358. ...pluginMetadata({ id: "fixture@inline", name: "fixture", rootPath: root }),
  359. components: [{ kind: "agent" as const, items: [{ name: "code-architect" }] }],
  360. };
  361. expect(loadPluginAgentProfiles({ plugins: [plugin] })).toMatchObject({
  362. profiles: [],
  363. diagnostics: [],
  364. });
  365. } finally {
  366. await rm(root, { force: true, recursive: true });
  367. }
  368. });
  369. it("SA97-09:首次和再次加载只迁移用户根文件,保留子目录及项目/插件文件", async () => {
  370. const root = await mkdtemp(join(tmpdir(), "zcode-subagent-scope-migration-"));
  371. const storageRoot = join(root, "storage");
  372. const workingDirectory = join(root, "workspace");
  373. const pluginRoot = join(root, "plugin");
  374. const paths = [
  375. join(storageRoot, "agents", "code-architect.md"),
  376. join(workingDirectory, ".zcode", "agents", "code-architect.md"),
  377. join(pluginRoot, "agents", "code-architect.md"),
  378. join(storageRoot, "agents", "legacy-cli", "code-architect.md"),
  379. join(storageRoot, "agents", "legacy-cli", "vendor", "code-architect.markdown"),
  380. ];
  381. const markdown =
  382. "---\r\nname: code-architect\r\ndescription: review\r\n# 保留备注\r\nmodel: builtin:zai-coding-plan/GLM\r\nthoughtLevel: high\r\n---\r\n正文 builtin:zai-coding-plan/GLM\r\n";
  383. try {
  384. for (const directory of [
  385. join(storageRoot, "agents"),
  386. join(workingDirectory, ".zcode", "agents"),
  387. join(pluginRoot, "agents"),
  388. join(storageRoot, "agents", "legacy-cli", "vendor"),
  389. ]) {
  390. await mkdir(directory, { recursive: true });
  391. }
  392. for (const path of paths) await writeFile(path, markdown);
  393. for (let load = 0; load < 2; load++) {
  394. const local = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  395. const plugins = loadPluginAgentProfiles({
  396. plugins: [pluginMetadata({ id: "scope-test", name: "scope-test", rootPath: pluginRoot })],
  397. });
  398. expect(local.diagnostics).toEqual([]);
  399. expect(plugins.diagnostics).toEqual([]);
  400. expect(local.profiles.map((profile) => profile.source)).toEqual(["user", "project"]);
  401. expect(
  402. local.profiles.find((profile) => profile.source === "user")?.modelSelection?.providerId,
  403. ).toBe("account:zai-individual-coding-plan");
  404. expect(
  405. local.profiles.find((profile) => profile.source === "project")?.modelSelection
  406. ?.providerId,
  407. ).toBe("builtin:zai-coding-plan");
  408. for (const profile of plugins.profiles)
  409. expect(profile.modelSelection).toEqual({
  410. providerId: "builtin:zai-coding-plan",
  411. modelId: "GLM",
  412. options: { reasoningLevel: "high" },
  413. });
  414. expect(plugins.profiles.length).toBeGreaterThan(0);
  415. expect(await readFile(paths[0]!, "utf8")).toBe(
  416. markdown.replace(
  417. "model: builtin:zai-coding-plan",
  418. "model: account:zai-individual-coding-plan",
  419. ),
  420. );
  421. for (const path of paths.slice(1)) expect(await readFile(path, "utf8")).toBe(markdown);
  422. }
  423. } finally {
  424. await rm(root, { force: true, recursive: true });
  425. }
  426. });
  427. it("loads enabled plugin agents as runtime profiles", async () => {
  428. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-"));
  429. const pluginRoot = join(root, "feature-dev");
  430. try {
  431. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  432. await writeFile(
  433. join(pluginRoot, "agents", "code-architect.md"),
  434. `---
  435. name: code-architect
  436. description: 设计功能架构
  437. tools:
  438. - Read
  439. ---
  440. 架构 prompt`,
  441. "utf8",
  442. );
  443. const result = loadPluginAgentProfiles({
  444. plugins: [
  445. pluginMetadata({
  446. id: "feature-dev@claude-plugins-official",
  447. name: "feature-dev",
  448. rootPath: pluginRoot,
  449. }),
  450. ],
  451. });
  452. expect(result.diagnostics).toEqual([]);
  453. expect(result.profiles.map((profile) => profile.name)).toEqual([
  454. "feature-dev:code-architect",
  455. "code-architect",
  456. ]);
  457. expect(result.profiles[0]).toMatchObject({
  458. source: "user",
  459. tools: ["Read"],
  460. systemPrompt: "架构 prompt",
  461. });
  462. } finally {
  463. await rm(root, { force: true, recursive: true });
  464. }
  465. });
  466. // 插件 agent 由模型委派;frontmatter 的 permissionMode 曾按用户级原样生效,
  467. // bypassPermissions 会让子会话以 yolo 运行、全工具免审批。现在保留声明值并打标记,
  468. // 由 core 只取比父会话更严格的模式;官方与第三方市场同一规则。
  469. it.each([
  470. ["zcode-plugins-official", "acceptEdits"],
  471. ["zcode-plugins-official", "auto"],
  472. ["zcode-plugins-official", "bypassPermissions"],
  473. ["zcode-plugins-official", "dontAsk"],
  474. ["zcode-plugins-official", "plan"],
  475. ["third-party", "acceptEdits"],
  476. ["third-party", "auto"],
  477. ["third-party", "bypassPermissions"],
  478. ["third-party", "dontAsk"],
  479. ["third-party", "plan"],
  480. ] as const)(
  481. "marks %s plugin permissionMode %s as restrict-only on canonical and alias profiles",
  482. async (marketplace, permissionMode) => {
  483. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-permission-"));
  484. const pluginRoot = join(root, "fixture");
  485. try {
  486. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  487. await writeFile(
  488. join(pluginRoot, "agents", "code-architect.md"),
  489. `---
  490. name: code-architect
  491. description: 插件 agent
  492. permissionMode: ${permissionMode}
  493. ---
  494. 插件 prompt`,
  495. "utf8",
  496. );
  497. const plugins = [
  498. pluginMetadata({
  499. id: `fixture@${marketplace}`,
  500. marketplace,
  501. name: "fixture",
  502. rootPath: pluginRoot,
  503. }),
  504. ];
  505. const result = loadPluginAgentProfiles({ plugins });
  506. expect(result.profiles.map((profile) => profile.name)).toEqual([
  507. "fixture:code-architect",
  508. "code-architect",
  509. ]);
  510. for (const profile of result.profiles) {
  511. expect(profile).toMatchObject({
  512. permissionMode,
  513. permissionModeRestrictOnly: true,
  514. source: "user",
  515. });
  516. }
  517. } finally {
  518. await rm(root, { force: true, recursive: true });
  519. }
  520. },
  521. );
  522. it("applies plugin agent model overrides from shared subagent state to canonical and alias profiles", async () => {
  523. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-overrides-"));
  524. const storageRoot = join(root, "home");
  525. const pluginRoot = join(root, "document-skills");
  526. try {
  527. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  528. await mkdir(join(storageRoot, "v2"), { recursive: true });
  529. await writeFile(
  530. join(pluginRoot, "agents", "code-architect.md"),
  531. `---
  532. name: code-architect
  533. description: 插件默认模型
  534. model: custom:custom-openai:gpt-5.4
  535. thoughtLevel: low
  536. ---
  537. 架构 prompt`,
  538. "utf8",
  539. );
  540. await writeFile(
  541. join(storageRoot, "v2", "agents-state.json"),
  542. JSON.stringify({
  543. pluginAgentModelOverrides: {
  544. "plugin:document-skills@zcode-plugins-official:code-architect":
  545. "custom:custom-openai:glm-5.2",
  546. "plugin:other@zcode-plugins-official:code-architect": "custom:custom-openai:ignored",
  547. },
  548. pluginAgentThoughtLevelOverrides: {
  549. "plugin:document-skills@zcode-plugins-official:code-architect": "high",
  550. },
  551. }),
  552. "utf8",
  553. );
  554. const plugins = [
  555. pluginMetadata({
  556. id: "document-skills@zcode-plugins-official",
  557. name: "document-skills",
  558. rootPath: pluginRoot,
  559. }),
  560. ];
  561. const state = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
  562. const overridden = loadPluginAgentProfiles({
  563. plugins,
  564. modelSelectionOverrides: state.pluginAgentModelSelectionOverrides,
  565. });
  566. const selection = {
  567. providerId: "custom-openai",
  568. modelId: "glm-5.2",
  569. options: { reasoningLevel: "high" },
  570. };
  571. // 规范名与裸名别名是同一文件的两个调用入口,必须共享同一覆盖。
  572. expect(overridden.profiles.map((profile) => [profile.name, profile.modelSelection])).toEqual([
  573. ["document-skills:code-architect", selection],
  574. ["code-architect", selection],
  575. ]);
  576. // 未传入覆盖时保留 md 声明;插件 loader 不单独读取 state 文件。
  577. const untouched = loadPluginAgentProfiles({ plugins });
  578. expect(untouched.profiles[0]).toMatchObject({
  579. modelSelection: {
  580. providerId: "custom-openai",
  581. modelId: "gpt-5.4",
  582. options: { reasoningLevel: "low" },
  583. },
  584. });
  585. // 覆盖 model 但未指定 thoughtLevel 时,不能沿用 md 里另一模型的档位。
  586. await writeFile(
  587. join(storageRoot, "v2", "agents-state.json"),
  588. JSON.stringify({
  589. pluginAgentModelOverrides: {
  590. "plugin:document-skills@zcode-plugins-official:code-architect":
  591. "custom:custom-openai:glm-5.2",
  592. },
  593. }),
  594. "utf8",
  595. );
  596. const modelOnlyState = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
  597. const modelOnly = loadPluginAgentProfiles({
  598. plugins,
  599. modelSelectionOverrides: modelOnlyState.pluginAgentModelSelectionOverrides,
  600. });
  601. expect(modelOnly.profiles[0]?.modelSelection).toEqual({
  602. providerId: "custom-openai",
  603. modelId: "glm-5.2",
  604. });
  605. } finally {
  606. await rm(root, { force: true, recursive: true });
  607. }
  608. });
  609. it("skips disabled plugin agents and ambiguous bare aliases", async () => {
  610. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-ambiguous-"));
  611. const firstRoot = join(root, "feature-dev");
  612. const secondRoot = join(root, "pr-review-toolkit");
  613. try {
  614. for (const pluginRoot of [firstRoot, secondRoot]) {
  615. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  616. await writeFile(
  617. join(pluginRoot, "agents", "code-architect.md"),
  618. `---
  619. name: code-architect
  620. description: 设计功能架构
  621. ---
  622. architect prompt`,
  623. "utf8",
  624. );
  625. }
  626. const result = loadPluginAgentProfiles({
  627. plugins: [
  628. pluginMetadata({
  629. id: "feature-dev@claude-plugins-official",
  630. name: "feature-dev",
  631. rootPath: firstRoot,
  632. }),
  633. pluginMetadata({
  634. id: "pr-review-toolkit@claude-plugins-official",
  635. name: "pr-review-toolkit",
  636. rootPath: secondRoot,
  637. }),
  638. pluginMetadata({
  639. enabled: false,
  640. id: "disabled@claude-plugins-official",
  641. name: "disabled",
  642. rootPath: join(root, "disabled"),
  643. }),
  644. ],
  645. });
  646. expect(result.profiles.map((profile) => profile.name)).toEqual([
  647. "feature-dev:code-architect",
  648. "pr-review-toolkit:code-architect",
  649. ]);
  650. expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual([
  651. "agent_ambiguous_name",
  652. "agent_ambiguous_name",
  653. ]);
  654. } finally {
  655. await rm(root, { force: true, recursive: true });
  656. }
  657. });
  658. });
  659. it("附加目录 subagent 用限定名并存,权限仍按项目级规则过滤", async () => {
  660. const root = await mkdtemp(join(tmpdir(), "zcode-additional-agents-"));
  661. try {
  662. const main = join(root, "frontend"),
  663. extra = join(root, "backend");
  664. for (const dir of [main, extra]) {
  665. await mkdir(join(dir, ".zcode", "agents"), { recursive: true });
  666. await writeFile(
  667. join(dir, ".zcode", "agents", "review.md"),
  668. "---\nname: review\ndescription: review\npermissionMode: bypassPermissions\n---\nReview",
  669. );
  670. }
  671. const result = await loadZCodeAgentProfiles({
  672. storageRoot: join(root, "state"),
  673. workingDirectory: main,
  674. additionalDirectories: [extra, extra, main],
  675. });
  676. expect(result.profiles.map((p) => p.name).sort()).toEqual(["backend/review", "review"]);
  677. expect(result.profiles.every((p) => p.permissionMode === undefined)).toBe(true);
  678. } finally {
  679. await rm(root, { recursive: true, force: true });
  680. }
  681. });