main-tool-pool.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. pause: async () => false,
  166. list: async () => [],
  167. },
  168. eventStore: createTestSessionEventStore(),
  169. modelFactory: createTestModelFactory({
  170. properties: { supportsNativeWebSearch: true },
  171. async generateText(request: any) {
  172. requests.push(request);
  173. return {
  174. finishReason: "stop",
  175. providerMetadata: undefined,
  176. text: "done",
  177. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  178. };
  179. },
  180. } as never),
  181. },
  182. );
  183. await runtime.executeTurn("Automation turn", undefined, {
  184. toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
  185. });
  186. await runtime.executeTurn("Normal user turn");
  187. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  188. const normalTurnTools = requests[1].tools.map((tool: any) => tool.name);
  189. expect(automationTurnTools).toEqual(["CronList"]);
  190. expect(normalTurnTools).toEqual(
  191. expect.arrayContaining(["CronCreate", "CronList", "CronUpdate", "CronDelete"]),
  192. );
  193. });
  194. it("exposes CronPauseSelf only for a turn that carries the loop automationId", async () => {
  195. const requests: any[] = [];
  196. const runtime = createTestAgentRuntime(
  197. createSessionId("runtime-main-pause-self-provider-tools"),
  198. {
  199. mode: "build",
  200. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete", "CronPauseSelf"],
  201. workingDirectory: "/workspace/project",
  202. },
  203. {
  204. automationPort: {
  205. create: async () => {
  206. throw new Error("not called");
  207. },
  208. update: async () => {
  209. throw new Error("not called");
  210. },
  211. delete: async () => undefined,
  212. pause: async () => false,
  213. list: async () => [],
  214. },
  215. eventStore: createTestSessionEventStore(),
  216. modelFactory: createTestModelFactory({
  217. properties: { supportsNativeWebSearch: true },
  218. async generateText(request: any) {
  219. requests.push(request);
  220. return {
  221. finishReason: "stop",
  222. providerMetadata: undefined,
  223. text: "done",
  224. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  225. };
  226. },
  227. } as never),
  228. },
  229. );
  230. // Host admission 显式传入 automationId 的执行轮:只读 CronList + 本轮自暂停。
  231. await runtime.executeTurn("Automation turn", undefined, {
  232. automationId: "automation-1",
  233. toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
  234. });
  235. // 只有 denylist 兜底信号、没有 automationId 的轮:没有可暂停目标,必须隐藏 CronPauseSelf。
  236. await runtime.executeTurn("Fallback automation turn", undefined, {
  237. toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
  238. });
  239. await runtime.executeTurn("Normal user turn");
  240. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  241. const fallbackTurnTools = requests[1].tools.map((tool: any) => tool.name);
  242. const normalTurnTools = requests[2].tools.map((tool: any) => tool.name);
  243. expect(automationTurnTools).toEqual(["CronList", "CronPauseSelf"]);
  244. expect(fallbackTurnTools).toEqual(["CronList"]);
  245. expect(normalTurnTools).toEqual(
  246. expect.arrayContaining(["CronCreate", "CronList", "CronUpdate", "CronDelete"]),
  247. );
  248. expect(normalTurnTools).not.toContain("CronPauseSelf");
  249. });
  250. it("hides automation mutation tools when automation queryId reaches provider", async () => {
  251. const requests: any[] = [];
  252. const runtime = createTestAgentRuntime(
  253. createSessionId("runtime-main-automation-query-provider-tools"),
  254. {
  255. mode: "build",
  256. toolAllowlist: ["CronCreate", "CronList", "CronUpdate", "CronDelete"],
  257. workingDirectory: "/workspace/project",
  258. },
  259. {
  260. automationPort: {
  261. create: async () => {
  262. throw new Error("not called");
  263. },
  264. update: async () => {
  265. throw new Error("not called");
  266. },
  267. delete: async () => undefined,
  268. pause: async () => false,
  269. list: async () => [],
  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("Automation turn", undefined, {
  287. queryId: "automation-parent:1784199000000" as never,
  288. });
  289. const automationTurnTools = requests[0].tools.map((tool: any) => tool.name);
  290. expect(automationTurnTools).toEqual(["CronList"]);
  291. });
  292. it("keeps provider-visible order for allowlisted main tool requests", async () => {
  293. const requests: any[] = [];
  294. const runtime = createTestAgentRuntime(
  295. createSessionId("runtime-main-allowlist-provider-order"),
  296. {
  297. mode: "build",
  298. toolAllowlist: ["Read", "Edit", "Bash"],
  299. workingDirectory: "/workspace/project",
  300. },
  301. {
  302. eventStore: createTestSessionEventStore(),
  303. modelFactory: createTestModelFactory({
  304. properties: { supportsNativeWebSearch: true },
  305. async generateText(request: any) {
  306. requests.push(request);
  307. return {
  308. finishReason: "stop",
  309. providerMetadata: undefined,
  310. text: "done",
  311. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  312. };
  313. },
  314. } as never),
  315. },
  316. );
  317. await runtime.executeTurn("Use selected tools");
  318. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual(["Bash", "Edit", "Read"]);
  319. });
  320. it("orders provider-visible explore allowlists before appending local-only tools", async () => {
  321. const requests: any[] = [];
  322. const runtime = createTestAgentRuntime(
  323. createSessionId("runtime-explore-allowlist-provider-order"),
  324. {
  325. mode: "build",
  326. toolset: "explore",
  327. toolAllowlist: ["TodoWrite", "WebFetch", "Read", "Bash"],
  328. workingDirectory: "/workspace/project",
  329. },
  330. {
  331. eventStore: createTestSessionEventStore(),
  332. modelFactory: createTestModelFactory({
  333. properties: { supportsNativeWebSearch: true },
  334. async generateText(request: any) {
  335. requests.push(request);
  336. return {
  337. finishReason: "stop",
  338. providerMetadata: undefined,
  339. text: "done",
  340. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  341. };
  342. },
  343. } as never),
  344. },
  345. );
  346. await runtime.executeTurn("Use selected tools");
  347. expect(requests[0].tools.map((tool: any) => tool.name)).toEqual([
  348. "Bash",
  349. "Read",
  350. "TodoWrite",
  351. "WebFetch",
  352. ]);
  353. });
  354. it("keeps the provider-visible embedded branch when the session shell is CMD", () => {
  355. const runtime = createTestAgentRuntime(
  356. createSessionId("runtime-main-cmd-direct-search-tools"),
  357. {
  358. mode: "build",
  359. workingDirectory: "/workspace/project",
  360. },
  361. {
  362. eventStore: createTestSessionEventStore(),
  363. },
  364. );
  365. expect(runtime.getToolRegistry().has("Glob")).toBe(false);
  366. expect(runtime.getToolRegistry().has("Grep")).toBe(false);
  367. runtime.initializeSessionShellEnvironmentIfNeeded({
  368. dialect: "cmd",
  369. display: { name: "CMD" },
  370. id: "cmd",
  371. label: "CMD",
  372. path: "cmd.exe",
  373. source: "user-config",
  374. });
  375. const byName = new Map(runtime.getTools().map((tool) => [tool.name, tool]));
  376. expect(byName.has("Glob")).toBe(false);
  377. expect(byName.has("Grep")).toBe(false);
  378. expect(byName.get("Bash")?.description ?? "").toContain(
  379. "Avoid using this tool to run `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands",
  380. );
  381. expect(byName.get("EnterPlanMode")?.description ?? "").toContain(
  382. "using `find`/Glob, `grep`/Grep, and Read",
  383. );
  384. });
  385. });