subagent-profile.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createBuiltInExploreAgentProfile,
  4. createBuiltInGeneralPurposeAgentProfile,
  5. formatAgentProfilesForPrompt,
  6. isBuiltInExploreAgentProfile,
  7. normalizeAgentProfiles,
  8. parseAgentProfileFromMarkdown,
  9. } from "../src/subagent/profile.js";
  10. describe("subagent profile Markdown parser", () => {
  11. it.each(['{"providerId":"wrong","modelId":"wrong"}', "null", '{"modelId":"broken"}'])(
  12. "中间字段不参与正式 Markdown 读取:%s",
  13. (value) => {
  14. const result = parseAgentProfileFromMarkdown({
  15. source: "user",
  16. content: `---
  17. name: reviewer
  18. description: Review changes
  19. modelSelection: ${value}
  20. model: custom-openai/legacy
  21. thoughtLevel: high
  22. ---
  23. Review carefully.`,
  24. });
  25. expect(result.profile?.modelSelection).toEqual({
  26. providerId: "custom-openai",
  27. modelId: "legacy",
  28. options: { reasoningLevel: "high" },
  29. });
  30. },
  31. );
  32. it("parses only exact persistent memory scopes", () => {
  33. const exact = parseAgentProfileFromMarkdown({
  34. source: "project",
  35. content: `---
  36. name: reviewer
  37. description: Review changes
  38. memory: project
  39. ---
  40. Review carefully.`,
  41. });
  42. const padded = parseAgentProfileFromMarkdown({
  43. source: "project",
  44. content: `---
  45. name: reviewer
  46. description: Review changes
  47. memory: " project "
  48. ---
  49. Review carefully.`,
  50. });
  51. expect(exact.diagnostic).toBeUndefined();
  52. expect(exact.profile?.memory).toBe("project");
  53. expect(padded.diagnostic?.code).toBe("agent_invalid_memory_scope");
  54. expect(padded.profile).toMatchObject({ name: "reviewer", description: "Review changes" });
  55. expect(padded.profile?.memory).toBeUndefined();
  56. });
  57. it("preserves an explicitly empty tools list for persistent memory projection", () => {
  58. const result = parseAgentProfileFromMarkdown({
  59. source: "project",
  60. content: `---
  61. name: reviewer
  62. description: Review changes
  63. memory: project
  64. tools: []
  65. ---
  66. Review carefully.`,
  67. });
  68. expect(result.profile?.tools).toEqual([]);
  69. });
  70. it("parses the P0 custom agent fields consumed by zcode-cli", () => {
  71. const result = parseAgentProfileFromMarkdown({
  72. source: "project",
  73. path: "/workspace/.zcode/agents/zcode-reviewer.md",
  74. content: `---
  75. name: zcode-reviewer
  76. description: 检查实现边界\\n输出中文结论
  77. model: custom-openai/reviewer-model
  78. thoughtLevel: high
  79. tools: Read, Grep Bash(ls -la)
  80. disallowedTools:
  81. - Edit
  82. - Write
  83. skills: code review
  84. color: cyan
  85. permissionMode: acceptEdits
  86. maxTurns: "7"
  87. background: "true"
  88. injectAgentsMd: "false"
  89. mcpServers:
  90. - local-tools
  91. ---
  92. 你是 zcode-reviewer。
  93. 只输出中文。`,
  94. });
  95. expect(result.diagnostic).toBeUndefined();
  96. expect(result.profile).toMatchObject({
  97. background: true,
  98. color: "cyan",
  99. description: "检查实现边界\n输出中文结论",
  100. disallowedTools: ["Edit", "Write"],
  101. maxTurns: 7,
  102. injectAgentsMd: false,
  103. modelSelection: {
  104. providerId: "custom-openai",
  105. modelId: "reviewer-model",
  106. options: { reasoningLevel: "high" },
  107. },
  108. name: "zcode-reviewer",
  109. path: "/workspace/.zcode/agents/zcode-reviewer.md",
  110. skills: ["code", "review"],
  111. source: "project",
  112. systemPrompt: "你是 zcode-reviewer。\n只输出中文。",
  113. tools: ["Read", "Grep", "Bash"],
  114. });
  115. expect(result.profile?.mcpServers).toEqual(["local-tools"]);
  116. expect(result.profile?.permissionMode).toBeUndefined();
  117. });
  118. it("preserves concrete provider and custom model values from frontmatter", () => {
  119. const providerModel = parseAgentProfileFromMarkdown({
  120. source: "user",
  121. path: "/home/user/.zcode/agents/reviewer.md",
  122. content: `---
  123. name: reviewer
  124. description: Use a concrete provider model
  125. model: custom-openai/gpt-5.4
  126. thoughtLevel: high
  127. ---
  128. Review carefully.`,
  129. });
  130. const customModel = parseAgentProfileFromMarkdown({
  131. source: "user",
  132. path: "/home/user/.zcode/agents/reviewer-custom.md",
  133. content: `---
  134. name: reviewer-custom
  135. description: Use the UI custom model value
  136. model: custom:custom-openai:gpt-5.4
  137. ---
  138. Review carefully.`,
  139. });
  140. expect(providerModel.profile?.modelSelection).toEqual({
  141. providerId: "custom-openai",
  142. modelId: "gpt-5.4",
  143. options: { reasoningLevel: "high" },
  144. });
  145. expect(customModel.profile?.modelSelection).toEqual({
  146. providerId: "custom-openai",
  147. modelId: "gpt-5.4",
  148. });
  149. });
  150. it("accepts only server-name lists for mcpServers", () => {
  151. const invalidFrontmatterValues = [
  152. "mcpServers: local-tools",
  153. `mcpServers:
  154. local-tools:
  155. type: stdio`,
  156. "mcpServers: { local-tools: { type: stdio } }",
  157. "mcpServers: [{ type: stdio, command: local-mcp }]",
  158. "mcpServers: [local-tools, 42]",
  159. "mcpServers: [local-tools,, analytics]",
  160. "mcpServers: [null]",
  161. "mcpServers: [-1, 1.5]",
  162. "mcpServers: [[alpha, beta]]",
  163. "mcpServers:",
  164. ];
  165. for (const mcpServers of invalidFrontmatterValues) {
  166. const result = parseAgentProfileFromMarkdown({
  167. source: "user",
  168. content: `---
  169. name: reviewer
  170. description: Review code
  171. ${mcpServers}
  172. ---
  173. Review carefully.`,
  174. });
  175. expect(result.profile).toBeUndefined();
  176. expect(result.diagnostic?.code).toBe("agent_invalid_mcp_servers");
  177. }
  178. });
  179. it("accepts an explicit empty mcpServers list and trims server names", () => {
  180. const empty = parseAgentProfileFromMarkdown({
  181. source: "user",
  182. content: `---
  183. name: empty-scope
  184. description: Use the parent MCP snapshot
  185. mcpServers: []
  186. ---`,
  187. });
  188. const scoped = parseAgentProfileFromMarkdown({
  189. source: "project",
  190. content: `---
  191. name: scoped
  192. description: Use selected parent MCP servers
  193. mcpServers: [" local-tools ", analytics, "plugin:tools:", "[alpha]"]
  194. ---`,
  195. });
  196. expect(empty.diagnostic).toBeUndefined();
  197. expect(empty.profile?.mcpServers).toEqual([]);
  198. expect(scoped.diagnostic).toBeUndefined();
  199. expect(scoped.profile?.mcpServers).toEqual([
  200. "local-tools",
  201. "analytics",
  202. "plugin:tools:",
  203. "[alpha]",
  204. ]);
  205. });
  206. it("keeps invalid nested syntax from reviving a non-MCP list", () => {
  207. const result = parseAgentProfileFromMarkdown({
  208. source: "user",
  209. content: `---
  210. name: reviewer
  211. description: Review code
  212. tools:
  213. invalid: nested
  214. - Read
  215. ---`,
  216. });
  217. expect(result.diagnostic).toBeUndefined();
  218. expect(result.profile?.tools).toBeUndefined();
  219. });
  220. it("formats available profiles for Agent tool descriptions with built-in default agents", () => {
  221. const prompt = formatAgentProfilesForPrompt([
  222. {
  223. name: "zcode-reviewer",
  224. description: "检查实现边界。",
  225. source: "project",
  226. systemPrompt: "只输出中文。",
  227. tools: ["Read"],
  228. background: true,
  229. },
  230. ]);
  231. expect(prompt).toContain("Available agent types and the tools they have access to:");
  232. expect(prompt).toContain("general-purpose");
  233. expect(prompt).toContain("Explore");
  234. expect(prompt).toContain("- zcode-reviewer: 检查实现边界。 (Tools: Read)");
  235. expect(prompt).not.toContain("tools: Read");
  236. expect(prompt).not.toContain("background: true");
  237. expect(prompt).not.toContain("model:");
  238. });
  239. it("assigns explicit identity colors to built-in agents", () => {
  240. expect(createBuiltInGeneralPurposeAgentProfile().color).toBe("blue");
  241. expect(createBuiltInExploreAgentProfile().color).toBe("cyan");
  242. });
  243. it("uses explicit AGENTS.md injection defaults for built-in agents", () => {
  244. expect(createBuiltInGeneralPurposeAgentProfile().injectAgentsMd).toBe(true);
  245. expect(createBuiltInExploreAgentProfile().injectAgentsMd).toBe(false);
  246. });
  247. it("parses explicit AGENTS.md injection while leaving missing custom values unset", () => {
  248. const explicit = parseAgentProfileFromMarkdown({
  249. source: "user",
  250. content: `---
  251. name: inheriting-reviewer
  252. description: Review with workspace instructions
  253. injectAgentsMd: true
  254. ---
  255. Review carefully.`,
  256. });
  257. const missing = parseAgentProfileFromMarkdown({
  258. source: "user",
  259. content: `---
  260. name: legacy-reviewer
  261. description: Legacy profile without the new field
  262. ---
  263. Review carefully.`,
  264. });
  265. expect(explicit.profile?.injectAgentsMd).toBe(true);
  266. expect(missing.profile?.injectAgentsMd).toBeUndefined();
  267. });
  268. it("identifies Explore by both name and loader-owned source", () => {
  269. expect(isBuiltInExploreAgentProfile({ name: "Explore", source: "built-in" })).toBe(true);
  270. expect(isBuiltInExploreAgentProfile({ name: "Explore", source: "user" })).toBe(false);
  271. expect(isBuiltInExploreAgentProfile({ name: "Explore", source: "project" })).toBe(false);
  272. expect(isBuiltInExploreAgentProfile({ name: "general-purpose", source: "built-in" })).toBe(
  273. false,
  274. );
  275. });
  276. it("keeps a custom Explore profile intact when it overrides the built-in profile", () => {
  277. const profiles = normalizeAgentProfiles(
  278. [
  279. {
  280. name: "Explore",
  281. description: "Custom mutable explorer",
  282. source: "project",
  283. systemPrompt: "Use the project workflow.",
  284. modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" },
  285. tools: ["Write", "mcp__local-tools__query"],
  286. },
  287. ],
  288. {
  289. builtInModelSelectionOverrides: {
  290. Explore: {
  291. providerId: "custom-openai",
  292. modelId: "glm-5.2",
  293. options: { reasoningLevel: "max" },
  294. },
  295. },
  296. },
  297. );
  298. expect(profiles.find((profile) => profile.name === "Explore")).toMatchObject({
  299. source: "project",
  300. modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" },
  301. tools: ["Write", "mcp__local-tools__query"],
  302. });
  303. });
  304. it("formats a custom Explore with its declared tools", () => {
  305. const prompt = formatAgentProfilesForPrompt([
  306. {
  307. name: "Explore",
  308. description: "Custom mutable explorer",
  309. source: "user",
  310. systemPrompt: "Use the user workflow.",
  311. tools: ["Write", "mcp__local-tools__query"],
  312. },
  313. ]);
  314. expect(prompt).toContain(
  315. "- Explore: Custom mutable explorer (Tools: Write, mcp__local-tools__query)",
  316. );
  317. expect(prompt).not.toContain("Tools: Read, Bash, WebFetch, WebSearch, TodoWrite");
  318. });
  319. it("leaves built-in Explore on inherited model by default", () => {
  320. expect(createBuiltInExploreAgentProfile().modelSelection).toBeUndefined();
  321. expect(
  322. normalizeAgentProfiles([]).find((profile) => profile.name === "Explore")?.modelSelection,
  323. ).toBeUndefined();
  324. });
  325. it("applies built-in model overrides without changing custom profile precedence", () => {
  326. const profiles = normalizeAgentProfiles(
  327. [
  328. {
  329. name: "code-reviewer",
  330. description: "Review code",
  331. source: "user",
  332. systemPrompt: "Review carefully.",
  333. modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" },
  334. },
  335. ],
  336. {
  337. builtInModelSelectionOverrides: {
  338. Explore: {
  339. providerId: "custom-openai",
  340. modelId: "glm-5.2",
  341. options: { reasoningLevel: "max" },
  342. },
  343. "general-purpose": {
  344. providerId: "custom-openai",
  345. modelId: "gpt-5.4",
  346. options: { reasoningLevel: "high" },
  347. },
  348. },
  349. },
  350. );
  351. expect(profiles.find((profile) => profile.name === "general-purpose")?.modelSelection).toEqual({
  352. providerId: "custom-openai",
  353. modelId: "gpt-5.4",
  354. options: { reasoningLevel: "high" },
  355. });
  356. expect(profiles.find((profile) => profile.name === "Explore")?.modelSelection).toEqual({
  357. providerId: "custom-openai",
  358. modelId: "glm-5.2",
  359. options: { reasoningLevel: "max" },
  360. });
  361. expect(profiles.find((profile) => profile.name === "code-reviewer")?.modelSelection).toEqual({
  362. providerId: "custom-openai",
  363. modelId: "gpt-5.4",
  364. });
  365. });
  366. it("does not materialize built-in options without a model Selection", () => {
  367. const profiles = normalizeAgentProfiles([]);
  368. expect(profiles.find((profile) => profile.name === "Explore")?.modelSelection).toBeUndefined();
  369. });
  370. it("formats built-in Explore tools for the embedded search branch", () => {
  371. const prompt = formatAgentProfilesForPrompt([], {
  372. embeddedSearchEnabled: true,
  373. });
  374. expect(prompt).toContain("Explore");
  375. expect(prompt).toContain("Tools: Read, Bash, WebFetch, WebSearch, TodoWrite");
  376. expect(prompt).not.toContain("Glob");
  377. expect(prompt).not.toContain("Grep");
  378. });
  379. });