subagents.test.ts 23 KB

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