conversation-session-toolsearch-scope.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
  4. import { toolSearchBlocks } from "#e2e/helpers/toolsearch-provider";
  5. const LOOKUP = "mcp__catalog__lookup";
  6. const BLOCKED = "mcp__catalog__blocked";
  7. const CHILD_SEARCH = "toolu_toolsearch_child";
  8. describe("ToolSearch MCP 与子 Agent 作用域(真实 CLI / stdio)", () => {
  9. it("TS06/TS07: hint、alwaysLoad、禁用工具、并发搜索及 child 独立发现", async function () {
  10. this.timeout(180_000);
  11. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-scope");
  12. try {
  13. const callLog = join(fixture.root, "mcp.ndjson");
  14. const server = {
  15. type: "stdio",
  16. command: process.execPath,
  17. args: [resolve("test/e2e/fixtures/mcp/toolsearch-server.mjs")],
  18. env: { E2E_TOOLSEARCH_MCP_LOG: callLog },
  19. };
  20. await fixture.configure({
  21. features: { memory: false, skill: false, mcp: true, subagent: true },
  22. permission: {
  23. mode: "yolo",
  24. allowedTools: ["Agent", "ToolSearch", LOOKUP],
  25. disallowedTools: [BLOCKED],
  26. },
  27. mcp: { servers: { catalog: server, resident_server: { ...server, alwaysLoad: true } } },
  28. });
  29. const agentDir = join(fixture.workspace, ".zcode", "agents");
  30. await mkdir(agentDir, { recursive: true });
  31. await writeFile(
  32. join(agentDir, "toolsearch-scoped.md"),
  33. `---\nname: toolsearch-scoped\ndescription: ToolSearch scoped fixture\ntools: ToolSearch, ${LOOKUP}\n---\nE2E_TOOLSEARCH_CHILD_SYSTEM\nSearch only your allowed catalog.\n`,
  34. );
  35. fixture.setResponse((body, response) => {
  36. const child = JSON.stringify(body.system).includes("E2E_TOOLSEARCH_CHILD_SYSTEM");
  37. const blocks = toolSearchBlocks(body);
  38. const result = (id: string) =>
  39. blocks.some((block) => block.type === "tool_result" && block.tool_use_id === id);
  40. let stage = "scope-done";
  41. if (child && !result(CHILD_SEARCH)) stage = "child-search";
  42. else if (!child && body.tools?.length) {
  43. if (!result("toolu_search_hint")) stage = "scope-searches";
  44. else if (!result("toolu_lookup")) stage = "scope-lookup";
  45. else if (!result("toolu_blocked")) stage = "scope-blocked";
  46. else if (!result("toolu_agent")) stage = "scope-agent";
  47. }
  48. fixture.reply(body, response, stage);
  49. });
  50. const run = await fixture.start([
  51. "--toolsearch-mode",
  52. "native",
  53. `--disallowedTools=${BLOCKED}`,
  54. "-p",
  55. "E2E_TOOLSEARCH_SCOPE 验证 MCP 与子 Agent。",
  56. ]).done;
  57. expect(run.code).toBe(0);
  58. const main = fixture.requests.filter(
  59. ({ body }) =>
  60. body.tools?.length &&
  61. !JSON.stringify(body.system).includes("E2E_TOOLSEARCH_CHILD_SYSTEM"),
  62. );
  63. expect(main.length).toBeGreaterThanOrEqual(4);
  64. const first = main[0]!.body;
  65. expect(first.tools?.some((tool) => tool.name === LOOKUP)).toBe(false);
  66. expect(first.tools?.some((tool) => tool.name === BLOCKED)).toBe(false);
  67. for (const name of ["mcp__catalog__resident", "mcp__resident_server__lookup"]) {
  68. expect(first.tools?.find((tool) => tool.name === name)).toBeDefined();
  69. expect(first.tools?.find((tool) => tool.name === name)?.defer_loading).toBeUndefined();
  70. }
  71. const searched = main[1]!.body;
  72. for (const name of [LOOKUP, "TodoRead"])
  73. expect(searched.tools?.find((tool) => tool.name === name)?.defer_loading).toBe(true);
  74. for (const id of ["toolu_search_hint", "toolu_search_todo"]) {
  75. expect(
  76. toolSearchBlocks(searched).filter(
  77. (block) => block.type === "tool_result" && block.tool_use_id === id && !block.is_error,
  78. ),
  79. ).toHaveLength(1);
  80. }
  81. const mcpCalls = await readFile(callLog, "utf8");
  82. expect(mcpCalls).toContain('"name":"lookup","value":"verified"');
  83. expect(mcpCalls).not.toContain('"name":"blocked"');
  84. const blockedResult = toolSearchBlocks(main.at(-1)!.body).find(
  85. (block) => block.tool_use_id === "toolu_blocked",
  86. );
  87. expect(blockedResult?.is_error).toBe(true);
  88. const child = fixture.requests.filter(({ body }) =>
  89. JSON.stringify(body.system).includes("E2E_TOOLSEARCH_CHILD_SYSTEM"),
  90. );
  91. expect(child).toHaveLength(2);
  92. expect(child[0]!.body.tools?.some((tool) => tool.name === "ToolSearch")).toBe(true);
  93. expect(child[0]!.body.tools?.some((tool) => tool.name === LOOKUP)).toBe(false);
  94. expect(child[1]!.body.tools?.find((tool) => tool.name === LOOKUP)?.defer_loading).toBe(true);
  95. const childResult = toolSearchBlocks(child[1]!.body).find(
  96. (block) => block.tool_use_id === CHILD_SEARCH,
  97. );
  98. expect(childResult?.content).toEqual([{ type: "tool_reference", tool_name: LOOKUP }]);
  99. for (const request of child) {
  100. expect(request.body.tools?.some((tool) => ["TodoRead", BLOCKED].includes(tool.name))).toBe(
  101. false,
  102. );
  103. }
  104. const agentResult = toolSearchBlocks(main.at(-1)!.body).find(
  105. (block) => block.tool_use_id === "toolu_agent",
  106. );
  107. expect(agentResult).toBeDefined();
  108. expect(agentResult?.is_error).not.toBe(true);
  109. } finally {
  110. await fixture.close();
  111. }
  112. });
  113. });