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

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