conversation-session-toolsearch-cli.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
  2. import { TOOLSEARCH_CALL_ID, TOOLSEARCH_NOTICE } from "#e2e/helpers/toolsearch-provider";
  3. describe("ToolSearch CLI 真实进程入口", () => {
  4. it("TS02: 普通 Anthropic 默认关闭,flag 强开;resume 不带 flag 降级后还能恢复", async function () {
  5. this.timeout(180_000);
  6. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-cli");
  7. try {
  8. const plain = await fixture.start(["-p", "E2E_TOOLSEARCH_SEED 默认关闭。"]).done;
  9. expect(plain.code).toBe(0);
  10. const initial = fixture.requests.find(({ body }) => body.tools?.length)!.body;
  11. expect(initial.tools?.some((tool) => tool.name === "ToolSearch")).toBe(false);
  12. expect(
  13. initial.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading,
  14. ).toBeUndefined();
  15. const before = fixture.requests.length;
  16. const forced = await fixture.start([
  17. "--toolsearch-mode",
  18. "native",
  19. "-p",
  20. "E2E_TOOLSEARCH_INITIAL 搜索 TodoRead 并执行。",
  21. ]).done;
  22. expect(forced.code).toBe(0);
  23. const summary = JSON.parse(forced.stdout) as { sessionId: string };
  24. const forcedRequests = fixture.requests
  25. .slice(before)
  26. .filter(({ body }) => body.tools?.length);
  27. expect(forcedRequests[0]!.body.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
  28. expect(
  29. forcedRequests.at(-1)!.body.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading,
  30. ).toBe(true);
  31. expect(JSON.stringify(forcedRequests.at(-1)!.body.messages)).toContain(
  32. '"type":"tool_reference","tool_name":"TodoRead"',
  33. );
  34. expect(
  35. JSON.stringify(forcedRequests.at(-1)!.body.messages).match(/Tool loaded\./g),
  36. ).toHaveLength(1);
  37. for (const force of [false, true]) {
  38. const start = fixture.requests.length;
  39. const result = await fixture.start([
  40. ...(force ? ["--toolsearch-mode", "native"] : []),
  41. ...(force ? ["--continue"] : ["--resume", summary.sessionId]),
  42. "-p",
  43. "E2E_TOOLSEARCH_COLD 检查重启后的第一条请求。",
  44. ]).done;
  45. expect(result.code).toBe(0);
  46. expect((JSON.parse(result.stdout) as { sessionId: string }).sessionId).toBe(
  47. summary.sessionId,
  48. );
  49. const cold = fixture.requests.slice(start).filter(({ body }) => body.tools?.length);
  50. expect(cold).toHaveLength(1);
  51. expect(JSON.stringify(cold[0]!.body.messages).match(/Tool loaded\./g) ?? []).toHaveLength(
  52. force ? 1 : 0,
  53. );
  54. expect(cold[0]!.body.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading).toBe(
  55. force ? true : undefined,
  56. );
  57. expect(cold[0]!.body.tools?.some((tool) => tool.name === "ToolSearch")).toBe(force);
  58. expect(JSON.stringify(cold[0]!.body.messages)).toContain(TOOLSEARCH_CALL_ID);
  59. expect(JSON.stringify(cold[0]!.body.messages).includes(TOOLSEARCH_NOTICE)).toBe(force);
  60. expect(JSON.stringify(cold[0]!.body.messages).includes('"type":"tool_reference"')).toBe(
  61. force,
  62. );
  63. }
  64. for (const denied of [true, false]) {
  65. const start = fixture.requests.length;
  66. const scoped = await fixture.start([
  67. "--toolsearch-mode",
  68. "native",
  69. ...(denied ? ["--disallowedTools=TodoRead"] : []),
  70. "--resume",
  71. summary.sessionId,
  72. "-p",
  73. "E2E_TOOLSEARCH_COLD 检查本次工具作用域。",
  74. ]).done;
  75. expect(scoped.code).toBe(0);
  76. const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
  77. expect(first.tools?.some((tool) => tool.name === "TodoRead")).toBe(!denied);
  78. expect(JSON.stringify(first.messages).includes('"tool_name":"TodoRead"')).toBe(!denied);
  79. }
  80. } finally {
  81. await fixture.close();
  82. }
  83. });
  84. it("TS02: ToolSearch 不可用或没有 deferred 工具时,force 不增加协议字段", async function () {
  85. this.timeout(120_000);
  86. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-cli");
  87. try {
  88. const seed = await fixture.start(["-p", "E2E_TOOLSEARCH_SEED 读取完整目录。"]).done;
  89. expect(seed.code).toBe(0);
  90. const catalog = fixture.requests.find(({ body }) => body.tools?.length)!.body.tools!;
  91. for (const excluded of [
  92. ["ToolSearch"],
  93. catalog
  94. .filter((tool) => !["Read", "ToolSearch"].includes(tool.name))
  95. .map((tool) => tool.name),
  96. ]) {
  97. const start = fixture.requests.length;
  98. const result = await fixture.start([
  99. "--toolsearch-mode",
  100. "native",
  101. `--disallowedTools=${excluded.join(",")}`,
  102. "-p",
  103. "E2E_TOOLSEARCH_SEED 受限目录。",
  104. ]).done;
  105. expect(result.code).toBe(0);
  106. const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!;
  107. expect(first.body.tools?.some((tool) => tool.name === "ToolSearch")).toBe(false);
  108. expect(first.body.tools?.some((tool) => tool.defer_loading)).toBe(false);
  109. expect(String(first.headers["anthropic-beta"] ?? "")).not.toContain("advanced-tool-use");
  110. expect(JSON.stringify(first.body.messages)).not.toContain(TOOLSEARCH_NOTICE);
  111. }
  112. } finally {
  113. await fixture.close();
  114. }
  115. });
  116. it("TS02: force + OpenAI 在首个 HTTP 请求前报错", async function () {
  117. this.timeout(90_000);
  118. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-cli");
  119. try {
  120. await fixture.select("openai-chat-completions");
  121. const result = await fixture.start([
  122. "--toolsearch-mode",
  123. "native",
  124. "-p",
  125. "E2E_TOOLSEARCH_SEED 不支持的协议。",
  126. ]).done;
  127. expect(result.code).not.toBe(0);
  128. expect(result.stderr).toContain(
  129. "--toolsearch-mode native requires an Anthropic Messages provider",
  130. );
  131. expect(fixture.requests).toHaveLength(0);
  132. } finally {
  133. await fixture.close();
  134. }
  135. });
  136. it("TS02: target 入口继承 force,完成校验请求降级为无工具作用域", async function () {
  137. this.timeout(120_000);
  138. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-cli");
  139. const verifierPrompt = "Verify whether the active session goal";
  140. try {
  141. fixture.setResponse((body, response) => {
  142. fixture.reply(
  143. body,
  144. response,
  145. JSON.stringify(body).includes(verifierPrompt)
  146. ? `verifier-${body.stream ? "stream" : "json"}`
  147. : undefined,
  148. );
  149. });
  150. const result = await fixture.start([
  151. "--toolsearch-mode",
  152. "native",
  153. "--target",
  154. "E2E_TOOLSEARCH_INITIAL 搜索 TodoRead 后记录已完成任务。",
  155. "--mode",
  156. "yolo",
  157. ]).done;
  158. expect(result.code).toBe(0);
  159. const main = fixture.requests.filter(({ body }) => body.tools?.length);
  160. expect(main[0]!.body.tools?.some((tool) => tool.name === "ToolSearch")).toBe(true);
  161. expect(main[0]!.body.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
  162. expect(main.at(-1)!.body.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading).toBe(
  163. true,
  164. );
  165. const verification = fixture.requests.filter(({ body }) =>
  166. JSON.stringify(body).includes(verifierPrompt),
  167. );
  168. expect(verification).toHaveLength(1);
  169. expect(verification[0]!.body.tools ?? []).toHaveLength(0);
  170. expect(JSON.stringify(verification[0]!.body.messages)).not.toContain(
  171. '"type":"tool_reference"',
  172. );
  173. expect(String(verification[0]!.headers["anthropic-beta"] ?? "")).not.toContain(
  174. "advanced-tool-use",
  175. );
  176. } finally {
  177. await fixture.close();
  178. }
  179. });
  180. });