subagents.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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: {
  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 } = discoverNodePluginsSync({
  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. ];
  379. const markdown =
  380. "---\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";
  381. try {
  382. for (const directory of [
  383. join(storageRoot, "agents"),
  384. join(workingDirectory, ".zcode", "agents"),
  385. join(pluginRoot, "agents"),
  386. ]) {
  387. await mkdir(directory, { recursive: true });
  388. }
  389. for (const path of paths) await writeFile(path, markdown);
  390. for (let load = 0; load < 2; load++) {
  391. const local = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
  392. const plugins = loadPluginAgentProfiles({
  393. plugins: [pluginMetadata({ id: "scope-test", name: "scope-test", rootPath: pluginRoot })],
  394. });
  395. expect(local.diagnostics).toEqual([]);
  396. expect(plugins.diagnostics).toEqual([]);
  397. expect(
  398. local.profiles.find((profile) => profile.source === "user")?.modelSelection?.providerId,
  399. ).toBe("account:zai-individual-coding-plan");
  400. expect(
  401. local.profiles.find((profile) => profile.source === "project")?.modelSelection
  402. ?.providerId,
  403. ).toBe("builtin:zai-coding-plan");
  404. for (const profile of plugins.profiles)
  405. expect(profile.modelSelection).toEqual({
  406. providerId: "builtin:zai-coding-plan",
  407. modelId: "GLM",
  408. options: { reasoningLevel: "high" },
  409. });
  410. expect(plugins.profiles.length).toBeGreaterThan(0);
  411. expect(await readFile(paths[0]!, "utf8")).toBe(
  412. markdown.replace(
  413. "model: builtin:zai-coding-plan",
  414. "model: account:zai-individual-coding-plan",
  415. ),
  416. );
  417. for (const path of paths.slice(1)) expect(await readFile(path, "utf8")).toBe(markdown);
  418. }
  419. } finally {
  420. await rm(root, { force: true, recursive: true });
  421. }
  422. });
  423. it("loads enabled plugin agents as runtime profiles", async () => {
  424. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-"));
  425. const pluginRoot = join(root, "feature-dev");
  426. try {
  427. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  428. await writeFile(
  429. join(pluginRoot, "agents", "code-architect.md"),
  430. `---
  431. name: code-architect
  432. description: 设计功能架构
  433. tools:
  434. - Read
  435. ---
  436. 架构 prompt`,
  437. "utf8",
  438. );
  439. const result = loadPluginAgentProfiles({
  440. plugins: [
  441. pluginMetadata({
  442. id: "feature-dev@claude-plugins-official",
  443. name: "feature-dev",
  444. rootPath: pluginRoot,
  445. }),
  446. ],
  447. });
  448. expect(result.diagnostics).toEqual([]);
  449. expect(result.profiles.map((profile) => profile.name)).toEqual([
  450. "feature-dev:code-architect",
  451. "code-architect",
  452. ]);
  453. expect(result.profiles[0]).toMatchObject({
  454. source: "user",
  455. tools: ["Read"],
  456. systemPrompt: "架构 prompt",
  457. });
  458. } finally {
  459. await rm(root, { force: true, recursive: true });
  460. }
  461. });
  462. // 插件 agent 由模型委派;frontmatter 的 permissionMode 曾按用户级原样生效,
  463. // bypassPermissions 会让子会话以 yolo 运行、全工具免审批。现在保留声明值并打标记,
  464. // 由 core 只取比父会话更严格的模式;官方与第三方市场同一规则。
  465. it.each([
  466. ["zcode-plugins-official", "acceptEdits"],
  467. ["zcode-plugins-official", "auto"],
  468. ["zcode-plugins-official", "bypassPermissions"],
  469. ["zcode-plugins-official", "dontAsk"],
  470. ["zcode-plugins-official", "plan"],
  471. ["third-party", "acceptEdits"],
  472. ["third-party", "auto"],
  473. ["third-party", "bypassPermissions"],
  474. ["third-party", "dontAsk"],
  475. ["third-party", "plan"],
  476. ] as const)(
  477. "marks %s plugin permissionMode %s as restrict-only on canonical and alias profiles",
  478. async (marketplace, permissionMode) => {
  479. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-permission-"));
  480. const pluginRoot = join(root, "fixture");
  481. try {
  482. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  483. await writeFile(
  484. join(pluginRoot, "agents", "code-architect.md"),
  485. `---
  486. name: code-architect
  487. description: 插件 agent
  488. permissionMode: ${permissionMode}
  489. ---
  490. 插件 prompt`,
  491. "utf8",
  492. );
  493. const plugins = [
  494. pluginMetadata({
  495. id: `fixture@${marketplace}`,
  496. marketplace,
  497. name: "fixture",
  498. rootPath: pluginRoot,
  499. }),
  500. ];
  501. const result = loadPluginAgentProfiles({ plugins });
  502. expect(result.profiles.map((profile) => profile.name)).toEqual([
  503. "fixture:code-architect",
  504. "code-architect",
  505. ]);
  506. for (const profile of result.profiles) {
  507. expect(profile).toMatchObject({
  508. permissionMode,
  509. permissionModeRestrictOnly: true,
  510. source: "user",
  511. });
  512. }
  513. } finally {
  514. await rm(root, { force: true, recursive: true });
  515. }
  516. },
  517. );
  518. it("applies plugin agent model overrides from shared subagent state to canonical and alias profiles", async () => {
  519. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-overrides-"));
  520. const storageRoot = join(root, "home");
  521. const pluginRoot = join(root, "document-skills");
  522. try {
  523. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  524. await mkdir(join(storageRoot, "v2"), { recursive: true });
  525. await writeFile(
  526. join(pluginRoot, "agents", "code-architect.md"),
  527. `---
  528. name: code-architect
  529. description: 插件默认模型
  530. model: custom:custom-openai:gpt-5.4
  531. thoughtLevel: low
  532. ---
  533. 架构 prompt`,
  534. "utf8",
  535. );
  536. await writeFile(
  537. join(storageRoot, "v2", "agents-state.json"),
  538. JSON.stringify({
  539. pluginAgentModelOverrides: {
  540. "plugin:document-skills@zcode-plugins-official:code-architect":
  541. "custom:custom-openai:glm-5.2",
  542. "plugin:other@zcode-plugins-official:code-architect": "custom:custom-openai:ignored",
  543. },
  544. pluginAgentThoughtLevelOverrides: {
  545. "plugin:document-skills@zcode-plugins-official:code-architect": "high",
  546. },
  547. }),
  548. "utf8",
  549. );
  550. const plugins = [
  551. pluginMetadata({
  552. id: "document-skills@zcode-plugins-official",
  553. name: "document-skills",
  554. rootPath: pluginRoot,
  555. }),
  556. ];
  557. const state = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
  558. const overridden = loadPluginAgentProfiles({
  559. plugins,
  560. modelSelectionOverrides: state.pluginAgentModelSelectionOverrides,
  561. });
  562. const selection = {
  563. providerId: "custom-openai",
  564. modelId: "glm-5.2",
  565. options: { reasoningLevel: "high" },
  566. };
  567. // 规范名与裸名别名是同一文件的两个调用入口,必须共享同一覆盖。
  568. expect(overridden.profiles.map((profile) => [profile.name, profile.modelSelection])).toEqual([
  569. ["document-skills:code-architect", selection],
  570. ["code-architect", selection],
  571. ]);
  572. // 未传入覆盖时保留 md 声明;插件 loader 不单独读取 state 文件。
  573. const untouched = loadPluginAgentProfiles({ plugins });
  574. expect(untouched.profiles[0]).toMatchObject({
  575. modelSelection: {
  576. providerId: "custom-openai",
  577. modelId: "gpt-5.4",
  578. options: { reasoningLevel: "low" },
  579. },
  580. });
  581. // 覆盖 model 但未指定 thoughtLevel 时,不能沿用 md 里另一模型的档位。
  582. await writeFile(
  583. join(storageRoot, "v2", "agents-state.json"),
  584. JSON.stringify({
  585. pluginAgentModelOverrides: {
  586. "plugin:document-skills@zcode-plugins-official:code-architect":
  587. "custom:custom-openai:glm-5.2",
  588. },
  589. }),
  590. "utf8",
  591. );
  592. const modelOnlyState = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
  593. const modelOnly = loadPluginAgentProfiles({
  594. plugins,
  595. modelSelectionOverrides: modelOnlyState.pluginAgentModelSelectionOverrides,
  596. });
  597. expect(modelOnly.profiles[0]?.modelSelection).toEqual({
  598. providerId: "custom-openai",
  599. modelId: "glm-5.2",
  600. });
  601. } finally {
  602. await rm(root, { force: true, recursive: true });
  603. }
  604. });
  605. it("skips disabled plugin agents and ambiguous bare aliases", async () => {
  606. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-ambiguous-"));
  607. const firstRoot = join(root, "feature-dev");
  608. const secondRoot = join(root, "pr-review-toolkit");
  609. try {
  610. for (const pluginRoot of [firstRoot, secondRoot]) {
  611. await mkdir(join(pluginRoot, "agents"), { recursive: true });
  612. await writeFile(
  613. join(pluginRoot, "agents", "code-architect.md"),
  614. `---
  615. name: code-architect
  616. description: 设计功能架构
  617. ---
  618. architect prompt`,
  619. "utf8",
  620. );
  621. }
  622. const result = loadPluginAgentProfiles({
  623. plugins: [
  624. pluginMetadata({
  625. id: "feature-dev@claude-plugins-official",
  626. name: "feature-dev",
  627. rootPath: firstRoot,
  628. }),
  629. pluginMetadata({
  630. id: "pr-review-toolkit@claude-plugins-official",
  631. name: "pr-review-toolkit",
  632. rootPath: secondRoot,
  633. }),
  634. pluginMetadata({
  635. enabled: false,
  636. id: "disabled@claude-plugins-official",
  637. name: "disabled",
  638. rootPath: join(root, "disabled"),
  639. }),
  640. ],
  641. });
  642. expect(result.profiles.map((profile) => profile.name)).toEqual([
  643. "feature-dev:code-architect",
  644. "pr-review-toolkit:code-architect",
  645. ]);
  646. expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual([
  647. "agent_ambiguous_name",
  648. "agent_ambiguous_name",
  649. ]);
  650. } finally {
  651. await rm(root, { force: true, recursive: true });
  652. }
  653. });
  654. });
  655. it("附加目录 subagent 用限定名并存,权限仍按项目级规则过滤", async () => {
  656. const root = await mkdtemp(join(tmpdir(), "zcode-additional-agents-"));
  657. try {
  658. const main = join(root, "frontend"),
  659. extra = join(root, "backend");
  660. for (const dir of [main, extra]) {
  661. await mkdir(join(dir, ".zcode", "agents"), { recursive: true });
  662. await writeFile(
  663. join(dir, ".zcode", "agents", "review.md"),
  664. "---\nname: review\ndescription: review\npermissionMode: bypassPermissions\n---\nReview",
  665. );
  666. }
  667. const result = await loadZCodeAgentProfiles({
  668. storageRoot: join(root, "state"),
  669. workingDirectory: main,
  670. additionalDirectories: [extra, extra, main],
  671. });
  672. expect(result.profiles.map((p) => p.name).sort()).toEqual(["backend/review", "review"]);
  673. expect(result.profiles.every((p) => p.permissionMode === undefined)).toBe(true);
  674. } finally {
  675. await rm(root, { recursive: true, force: true });
  676. }
  677. });