main-tool-pool.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. "Read",
  44. "Bash",
  45. "WebFetch",
  46. "WebSearch",
  47. "TodoWrite",
  48. ]);
  49. // allowlist ["Read","Glob","Bash","Write"] 与 Explore 白名单求交后,
  50. // 默认 embedded branch 隐藏 Glob/Grep,Write 被 Explore 白名单排除。
  51. expect(runtime.getToolRegistry().list()).toEqual(["Read", "Bash"]);
  52. });
  53. it("keeps Glob/Grep hidden when main toolAllowlist includes them", () => {
  54. const runtime = createTestAgentRuntime(
  55. createSessionId("runtime-main-allowlist-search-tools"),
  56. {
  57. mode: "build",
  58. toolAllowlist: ["Read", "Glob", "Grep", "Bash"],
  59. workingDirectory: "/workspace/project",
  60. },
  61. {
  62. eventStore: createTestSessionEventStore(),
  63. },
  64. );
  65. expect(runtime.getToolRegistry().list()).toEqual(["Read", "Bash"]);
  66. });
  67. it("does not expose node_repl just because a browser control port exists", () => {
  68. const runtimeWithoutFeature = createTestAgentRuntime(
  69. createSessionId("runtime-browser-port-without-feature"),
  70. {
  71. mode: "build",
  72. workingDirectory: "/workspace/project",
  73. },
  74. {
  75. browserControlPort: {
  76. list: async () => [],
  77. execute: async () => ({ ok: true, elapsedMs: 0 }),
  78. },
  79. eventStore: createTestSessionEventStore(),
  80. },
  81. );
  82. const runtimeWithFeature = createTestAgentRuntime(
  83. createSessionId("runtime-browser-port-with-feature"),
  84. {
  85. mode: "build",
  86. runtimeFeatures: { browserUse: true, nodeRepl: true },
  87. workingDirectory: "/workspace/project",
  88. },
  89. {
  90. browserControlPort: {
  91. list: async () => [],
  92. execute: async () => ({ ok: true, elapsedMs: 0 }),
  93. },
  94. eventStore: createTestSessionEventStore(),
  95. },
  96. );
  97. expect(runtimeWithoutFeature.getToolRegistry().has("js")).toBe(false);
  98. expect(runtimeWithFeature.getToolRegistry().has("js")).toBe(true);
  99. });
  100. it("removes disallowed built-in tools from the main runtime registry", () => {
  101. const runtime = createTestAgentRuntime(
  102. createSessionId("runtime-main-disallowed-tools"),
  103. {
  104. mode: "build",
  105. toolDisallowlist: ["Bash(git *)", "web_search"],
  106. workingDirectory: "/workspace/project",
  107. },
  108. {
  109. eventStore: createTestSessionEventStore(),
  110. },
  111. );
  112. expect(runtime.getToolRegistry().has("Bash")).toBe(false);
  113. expect(runtime.getToolRegistry().has("WebSearch")).toBe(false);
  114. expect(runtime.getToolRegistry().has("Read")).toBe(true);
  115. });
  116. it("keeps disallowed built-in tools out of provider-visible requests", async () => {
  117. const requests: any[] = [];
  118. const runtime = createTestAgentRuntime(
  119. createSessionId("runtime-main-disallowed-provider-tools"),
  120. {
  121. mode: "build",
  122. toolAllowlist: ["Read", "Edit", "Bash", "WebSearch"],
  123. toolDisallowlist: ["Bash(git *)", "WebSearch"],
  124. workingDirectory: "/workspace/project",
  125. },
  126. {
  127. eventStore: createTestSessionEventStore(),
  128. modelFactory: createTestModelFactory({
  129. properties: { supportsNativeWebSearch: true },
  130. async generateText(request: any) {
  131. requests.push(request);
  132. return {
  133. finishReason: "stop",
  134. providerMetadata: undefined,
  135. text: "done",
  136. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  137. };
  138. },
  139. } as never),
  140. },
  141. );
  142. await runtime.executeTurn("Use selected tools");
  143. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual(["Edit", "Read"]);
  144. });
  145. it("hides automation mutation tools only for an automation turn and restores them next turn", async () => {
  146. const requests: any[] = [];
  147. const runtime = createTestAgentRuntime(
  148. createSessionId("runtime-main-turn-disallowed-provider-tools"),
  149. {
  150. mode: "build",
  151. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete"],
  152. workingDirectory: "/workspace/project",
  153. },
  154. {
  155. automationPort: {
  156. create: async () => {
  157. throw new Error("not called");
  158. },
  159. update: async () => {
  160. throw new Error("not called");
  161. },
  162. delete: async () => undefined,
  163. list: async () => [],
  164. },
  165. eventStore: createTestSessionEventStore(),
  166. modelFactory: createTestModelFactory({
  167. properties: { supportsNativeWebSearch: true },
  168. async generateText(request: any) {
  169. requests.push(request);
  170. return {
  171. finishReason: "stop",
  172. providerMetadata: undefined,
  173. text: "done",
  174. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  175. };
  176. },
  177. } as never),
  178. },
  179. );
  180. await runtime.executeTurn("Automation turn", undefined, {
  181. toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
  182. });
  183. await runtime.executeTurn("Normal user turn");
  184. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  185. const normalTurnTools = requests[1].tools.map((tool: any) => tool.name);
  186. expect(automationTurnTools).toEqual(["CronList"]);
  187. expect(normalTurnTools).toEqual(
  188. expect.arrayContaining(["CronCreate", "CronList", "CronUpdate", "CronDelete"]),
  189. );
  190. });
  191. it("hides automation mutation tools when automation queryId reaches provider", async () => {
  192. const requests: any[] = [];
  193. const runtime = createTestAgentRuntime(
  194. createSessionId("runtime-main-automation-query-provider-tools"),
  195. {
  196. mode: "build",
  197. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete"],
  198. workingDirectory: "/workspace/project",
  199. },
  200. {
  201. automationPort: {
  202. create: async () => {
  203. throw new Error("not called");
  204. },
  205. update: async () => {
  206. throw new Error("not called");
  207. },
  208. delete: async () => undefined,
  209. list: async () => [],
  210. },
  211. eventStore: createTestSessionEventStore(),
  212. modelFactory: createTestModelFactory({
  213. properties: { supportsNativeWebSearch: true },
  214. async generateText(request: any) {
  215. requests.push(request);
  216. return {
  217. finishReason: "stop",
  218. providerMetadata: undefined,
  219. text: "done",
  220. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  221. };
  222. },
  223. } as never),
  224. },
  225. );
  226. await runtime.executeTurn("Automation turn", undefined, {
  227. queryId: "automation-parent:1784199000000" as never,
  228. });
  229. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  230. expect(automationTurnTools).toEqual(["CronList"]);
  231. });
  232. it("keeps provider-visible order for allowlisted main tool requests", async () => {
  233. const requests: any[] = [];
  234. const runtime = createTestAgentRuntime(
  235. createSessionId("runtime-main-allowlist-provider-order"),
  236. {
  237. mode: "build",
  238. toolAllowlist: ["Read", "Edit", "Bash"],
  239. workingDirectory: "/workspace/project",
  240. },
  241. {
  242. eventStore: createTestSessionEventStore(),
  243. modelFactory: createTestModelFactory({
  244. properties: { supportsNativeWebSearch: true },
  245. async generateText(request: any) {
  246. requests.push(request);
  247. return {
  248. finishReason: "stop",
  249. providerMetadata: undefined,
  250. text: "done",
  251. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  252. };
  253. },
  254. } as never),
  255. },
  256. );
  257. await runtime.executeTurn("Use selected tools");
  258. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual(["Bash", "Edit", "Read"]);
  259. });
  260. it("orders provider-visible explore allowlists before appending local-only tools", async () => {
  261. const requests: any[] = [];
  262. const runtime = createTestAgentRuntime(
  263. createSessionId("runtime-explore-allowlist-provider-order"),
  264. {
  265. mode: "build",
  266. toolset: "explore",
  267. toolAllowlist: ["TodoWrite", "WebFetch", "Read", "Bash"],
  268. workingDirectory: "/workspace/project",
  269. },
  270. {
  271. eventStore: createTestSessionEventStore(),
  272. modelFactory: createTestModelFactory({
  273. properties: { supportsNativeWebSearch: true },
  274. async generateText(request: any) {
  275. requests.push(request);
  276. return {
  277. finishReason: "stop",
  278. providerMetadata: undefined,
  279. text: "done",
  280. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  281. };
  282. },
  283. } as never),
  284. },
  285. );
  286. await runtime.executeTurn("Use selected tools");
  287. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual([
  288. "Bash",
  289. "Read",
  290. "TodoWrite",
  291. "WebFetch",
  292. ]);
  293. });
  294. it("keeps the provider-visible embedded branch when the session shell is CMD", () => {
  295. const runtime = createTestAgentRuntime(
  296. createSessionId("runtime-main-cmd-direct-search-tools"),
  297. {
  298. mode: "build",
  299. workingDirectory: "/workspace/project",
  300. },
  301. {
  302. eventStore: createTestSessionEventStore(),
  303. },
  304. );
  305. expect(runtime.getToolRegistry().has("Glob")).toBe(false);
  306. expect(runtime.getToolRegistry().has("Grep")).toBe(false);
  307. runtime.initializeSessionShellEnvironmentIfNeeded({
  308. dialect: "cmd",
  309. display: { name: "CMD" },
  310. id: "cmd",
  311. label: "CMD",
  312. path: "cmd.exe",
  313. source: "user-config",
  314. });
  315. const byName = new Map(runtime.getTools().map((tool) => [tool.name, tool]));
  316. expect(byName.has("Glob")).toBe(false);
  317. expect(byName.has("Grep")).toBe(false);
  318. expect(byName.get("Bash")?.description ?? "").toContain(
  319. "Avoid using this tool to run `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands",
  320. );
  321. expect(byName.get("EnterPlanMode")?.description ?? "").toContain(
  322. "using `find`/Glob, `grep`/Grep, and Read",
  323. );
  324. });
  325. it("does not let updateConfig overwrite the session shell snapshot", () => {
  326. const runtime = createTestAgentRuntime(
  327. createSessionId("runtime-main-shell-snapshot-immutable"),
  328. {
  329. bashShellSelection: {
  330. dialect: "cmd",
  331. display: { name: "CMD" },
  332. id: "cmd",
  333. label: "CMD",
  334. path: "cmd.exe",
  335. source: "user-config",
  336. },
  337. mode: "build",
  338. workingDirectory: "/workspace/project",
  339. },
  340. {
  341. eventStore: createTestSessionEventStore(),
  342. },
  343. );
  344. (runtime.updateConfig as (patch: unknown) => void)({
  345. bashShellSelection: {
  346. dialect: "git-bash",
  347. display: { name: "Git Bash" },
  348. id: "auto:git-bash",
  349. label: "Git Bash",
  350. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  351. source: "auto-detected",
  352. },
  353. });
  354. const byName = new Map(runtime.getTools().map((tool) => [tool.name, tool]));
  355. expect(byName.has("Glob")).toBe(false);
  356. expect(byName.has("Grep")).toBe(false);
  357. expect(byName.get("Bash")?.description ?? "").toContain(
  358. "Avoid using this tool to run `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands",
  359. );
  360. });
  361. });