main-tool-pool.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { describe, expect, it } from "vitest";
  2. import { createSessionId } from "@zcode/contracts";
  3. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  4. import { createTestSessionEventStore } from "./test-event-store.js";
  5. import { createTestModelFactory } from "./test-runtime-model.js";
  6. describe("main agent tool pool", () => {
  7. it("does not expose subagent tools when subagents are disabled", () => {
  8. const runtime = createTestAgentRuntime(
  9. createSessionId("runtime-without-model-factory"),
  10. {
  11. mode: "build",
  12. subagents: { enabled: false },
  13. workingDirectory: "/workspace/project",
  14. },
  15. { eventStore: createTestSessionEventStore() },
  16. );
  17. expect(runtime.getToolRegistry().has("Agent")).toBe(false);
  18. expect(runtime.getToolRegistry().has("Task")).toBe(false);
  19. });
  20. it("registers the embedded-search Explore tool pool with read-only behavior enforced by prompt", () => {
  21. const defaultRuntime = createTestAgentRuntime(
  22. createSessionId("runtime-explore-toolset-default"),
  23. {
  24. toolset: "explore",
  25. },
  26. {
  27. eventStore: createTestSessionEventStore(),
  28. },
  29. );
  30. const runtime = createTestAgentRuntime(
  31. createSessionId("runtime-explore-toolset"),
  32. {
  33. toolset: "explore",
  34. toolAllowlist: ["Read", "Glob", "Bash", "Write"],
  35. },
  36. {
  37. eventStore: createTestSessionEventStore(),
  38. },
  39. );
  40. // 无 skillPort 故不含 Skill;WebSearch 是普通 client-side wrapper。
  41. // 顺序按 builtInTools 注册顺序。
  42. expect(defaultRuntime.getToolRegistry().list()).toEqual([
  43. "ToolSearch",
  44. "DeferredToolCall",
  45. "Read",
  46. "Bash",
  47. "WebFetch",
  48. "WebSearch",
  49. "TodoWrite",
  50. ]);
  51. // allowlist ["Read","Glob","Bash","Write"] 与 Explore 白名单求交后,
  52. // 默认 embedded branch 隐藏 Glob/Grep,Write 被 Explore 白名单排除。
  53. expect(runtime.getToolRegistry().list()).toEqual(["Read", "Bash"]);
  54. });
  55. it("keeps Glob/Grep hidden when main toolAllowlist includes them", () => {
  56. const runtime = createTestAgentRuntime(
  57. createSessionId("runtime-main-allowlist-search-tools"),
  58. {
  59. mode: "build",
  60. toolAllowlist: ["Read", "Glob", "Grep", "Bash"],
  61. workingDirectory: "/workspace/project",
  62. },
  63. {
  64. eventStore: createTestSessionEventStore(),
  65. },
  66. );
  67. expect(runtime.getToolRegistry().list()).toEqual(["Read", "Bash"]);
  68. });
  69. it("does not expose node_repl just because a browser control port exists", () => {
  70. const runtimeWithoutFeature = createTestAgentRuntime(
  71. createSessionId("runtime-browser-port-without-feature"),
  72. {
  73. mode: "build",
  74. workingDirectory: "/workspace/project",
  75. },
  76. {
  77. browserControlPort: {
  78. list: async () => [],
  79. execute: async () => ({ ok: true, elapsedMs: 0 }),
  80. },
  81. eventStore: createTestSessionEventStore(),
  82. },
  83. );
  84. const runtimeWithFeature = createTestAgentRuntime(
  85. createSessionId("runtime-browser-port-with-feature"),
  86. {
  87. mode: "build",
  88. runtimeFeatures: { browserUse: true, nodeRepl: true },
  89. workingDirectory: "/workspace/project",
  90. },
  91. {
  92. browserControlPort: {
  93. list: async () => [],
  94. execute: async () => ({ ok: true, elapsedMs: 0 }),
  95. },
  96. eventStore: createTestSessionEventStore(),
  97. },
  98. );
  99. expect(runtimeWithoutFeature.getToolRegistry().has("js")).toBe(false);
  100. expect(runtimeWithFeature.getToolRegistry().has("js")).toBe(true);
  101. });
  102. it("removes disallowed built-in tools from the main runtime registry", () => {
  103. const runtime = createTestAgentRuntime(
  104. createSessionId("runtime-main-disallowed-tools"),
  105. {
  106. mode: "build",
  107. toolDisallowlist: ["Bash(git *)", "web_search"],
  108. workingDirectory: "/workspace/project",
  109. },
  110. {
  111. eventStore: createTestSessionEventStore(),
  112. },
  113. );
  114. expect(runtime.getToolRegistry().has("Bash")).toBe(false);
  115. expect(runtime.getToolRegistry().has("WebSearch")).toBe(false);
  116. expect(runtime.getToolRegistry().has("Read")).toBe(true);
  117. });
  118. it("keeps disallowed built-in tools out of provider-visible requests", async () => {
  119. const requests: any[] = [];
  120. const runtime = createTestAgentRuntime(
  121. createSessionId("runtime-main-disallowed-provider-tools"),
  122. {
  123. mode: "build",
  124. toolAllowlist: ["Read", "Edit", "Bash", "WebSearch"],
  125. toolDisallowlist: ["Bash(git *)", "WebSearch"],
  126. workingDirectory: "/workspace/project",
  127. },
  128. {
  129. eventStore: createTestSessionEventStore(),
  130. modelFactory: createTestModelFactory({
  131. properties: { supportsNativeWebSearch: true },
  132. async generateText(request: any) {
  133. requests.push(request);
  134. return {
  135. finishReason: "stop",
  136. providerMetadata: undefined,
  137. text: "done",
  138. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  139. };
  140. },
  141. } as never),
  142. },
  143. );
  144. await runtime.executeTurn("Use selected tools");
  145. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual(["Edit", "Read"]);
  146. });
  147. it("hides automation mutation tools only for an automation turn and restores them next turn", async () => {
  148. const requests: any[] = [];
  149. const runtime = createTestAgentRuntime(
  150. createSessionId("runtime-main-turn-disallowed-provider-tools"),
  151. {
  152. mode: "build",
  153. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete"],
  154. workingDirectory: "/workspace/project",
  155. },
  156. {
  157. automationPort: {
  158. create: async () => {
  159. throw new Error("not called");
  160. },
  161. update: async () => {
  162. throw new Error("not called");
  163. },
  164. delete: async () => undefined,
  165. list: async () => [],
  166. },
  167. eventStore: createTestSessionEventStore(),
  168. modelFactory: createTestModelFactory({
  169. properties: { supportsNativeWebSearch: true },
  170. async generateText(request: any) {
  171. requests.push(request);
  172. return {
  173. finishReason: "stop",
  174. providerMetadata: undefined,
  175. text: "done",
  176. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  177. };
  178. },
  179. } as never),
  180. },
  181. );
  182. await runtime.executeTurn("Automation turn", undefined, {
  183. toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
  184. });
  185. await runtime.executeTurn("Normal user turn");
  186. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  187. const normalTurnTools = requests[1].tools.map((tool: any) => tool.name);
  188. expect(automationTurnTools).toEqual(["CronList"]);
  189. expect(normalTurnTools).toEqual(
  190. expect.arrayContaining(["CronCreate", "CronList", "CronUpdate", "CronDelete"]),
  191. );
  192. });
  193. it("hides automation mutation tools when automation queryId reaches provider", async () => {
  194. const requests: any[] = [];
  195. const runtime = createTestAgentRuntime(
  196. createSessionId("runtime-main-automation-query-provider-tools"),
  197. {
  198. mode: "build",
  199. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete"],
  200. workingDirectory: "/workspace/project",
  201. },
  202. {
  203. automationPort: {
  204. create: async () => {
  205. throw new Error("not called");
  206. },
  207. update: async () => {
  208. throw new Error("not called");
  209. },
  210. delete: async () => undefined,
  211. list: async () => [],
  212. },
  213. eventStore: createTestSessionEventStore(),
  214. modelFactory: createTestModelFactory({
  215. properties: { supportsNativeWebSearch: true },
  216. async generateText(request: any) {
  217. requests.push(request);
  218. return {
  219. finishReason: "stop",
  220. providerMetadata: undefined,
  221. text: "done",
  222. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  223. };
  224. },
  225. } as never),
  226. },
  227. );
  228. await runtime.executeTurn("Automation turn", undefined, {
  229. queryId: "automation-parent:1784199000000" as never,
  230. });
  231. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  232. expect(automationTurnTools).toEqual(["CronList"]);
  233. });
  234. it("keeps provider-visible order for allowlisted main tool requests", async () => {
  235. const requests: any[] = [];
  236. const runtime = createTestAgentRuntime(
  237. createSessionId("runtime-main-allowlist-provider-order"),
  238. {
  239. mode: "build",
  240. toolAllowlist: ["Read", "Edit", "Bash"],
  241. workingDirectory: "/workspace/project",
  242. },
  243. {
  244. eventStore: createTestSessionEventStore(),
  245. modelFactory: createTestModelFactory({
  246. properties: { supportsNativeWebSearch: true },
  247. async generateText(request: any) {
  248. requests.push(request);
  249. return {
  250. finishReason: "stop",
  251. providerMetadata: undefined,
  252. text: "done",
  253. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  254. };
  255. },
  256. } as never),
  257. },
  258. );
  259. await runtime.executeTurn("Use selected tools");
  260. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual(["Bash", "Edit", "Read"]);
  261. });
  262. it("orders provider-visible explore allowlists before appending local-only tools", async () => {
  263. const requests: any[] = [];
  264. const runtime = createTestAgentRuntime(
  265. createSessionId("runtime-explore-allowlist-provider-order"),
  266. {
  267. mode: "build",
  268. toolset: "explore",
  269. toolAllowlist: ["TodoWrite", "WebFetch", "Read", "Bash"],
  270. workingDirectory: "/workspace/project",
  271. },
  272. {
  273. eventStore: createTestSessionEventStore(),
  274. modelFactory: createTestModelFactory({
  275. properties: { supportsNativeWebSearch: true },
  276. async generateText(request: any) {
  277. requests.push(request);
  278. return {
  279. finishReason: "stop",
  280. providerMetadata: undefined,
  281. text: "done",
  282. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  283. };
  284. },
  285. } as never),
  286. },
  287. );
  288. await runtime.executeTurn("Use selected tools");
  289. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual([
  290. "Bash",
  291. "Read",
  292. "TodoWrite",
  293. "WebFetch",
  294. ]);
  295. });
  296. it("keeps the provider-visible embedded branch when the session shell is CMD", () => {
  297. const runtime = createTestAgentRuntime(
  298. createSessionId("runtime-main-cmd-direct-search-tools"),
  299. {
  300. mode: "build",
  301. workingDirectory: "/workspace/project",
  302. },
  303. {
  304. eventStore: createTestSessionEventStore(),
  305. },
  306. );
  307. expect(runtime.getToolRegistry().has("Glob")).toBe(false);
  308. expect(runtime.getToolRegistry().has("Grep")).toBe(false);
  309. runtime.initializeSessionShellEnvironmentIfNeeded({
  310. dialect: "cmd",
  311. display: { name: "CMD" },
  312. id: "cmd",
  313. label: "CMD",
  314. path: "cmd.exe",
  315. source: "user-config",
  316. });
  317. const byName = new Map(runtime.getTools().map((tool) => [tool.name, tool]));
  318. expect(byName.has("Glob")).toBe(false);
  319. expect(byName.has("Grep")).toBe(false);
  320. expect(byName.get("Bash")?.description ?? "").toContain(
  321. "Avoid using this tool to run `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands",
  322. );
  323. expect(byName.get("EnterPlanMode")?.description ?? "").toContain(
  324. "using `find`/Glob, `grep`/Grep, and Read",
  325. );
  326. });
  327. });