conversation-session-toolsearch-recovery.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { DatabaseSync } from "node:sqlite";
  2. import { join, resolve } from "node:path";
  3. import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
  4. import { TOOLSEARCH_CALL_ID } from "#e2e/helpers/toolsearch-provider";
  5. interface StoredPart {
  6. id: string;
  7. session_id: string;
  8. data: string;
  9. }
  10. describe("ToolSearch 冷恢复异常与当前权限(真实 CLI / SQLite)", () => {
  11. for (const metadata of ["legacy", "corrupt"] as const) {
  12. it(`TS05: ${metadata} metadata 沿旧文本路径恢复,不伪造发现`, async function () {
  13. this.timeout(90_000);
  14. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
  15. try {
  16. const run = await fixture.start([
  17. "--toolsearch-mode",
  18. "native",
  19. "-p",
  20. "E2E_TOOLSEARCH_INITIAL 搜索。",
  21. ]).done;
  22. expect(run.code).toBe(0);
  23. const { sessionId } = JSON.parse(run.stdout) as { sessionId: string };
  24. const db = new DatabaseSync(join(fixture.root, "session.sqlite"));
  25. try {
  26. const row = db
  27. .prepare("select id, data from part where json_extract(data, '$.callID') = ?")
  28. .get(TOOLSEARCH_CALL_ID) as unknown as StoredPart;
  29. expect(row).toBeDefined();
  30. const part = JSON.parse(row.data);
  31. expect(part.state.metadata.modelContent).toEqual([
  32. { type: "tool_reference", toolName: "TodoRead" },
  33. ]);
  34. if (metadata === "legacy") delete part.state.metadata.modelContent;
  35. else part.state.metadata.modelContent = [{ type: "tool_reference", toolName: 7 }];
  36. db.prepare("update part set data = ? where id = ?").run(JSON.stringify(part), row.id);
  37. } finally {
  38. db.close();
  39. }
  40. const start = fixture.requests.length;
  41. const cold = await fixture.start([
  42. "--toolsearch-mode",
  43. "native",
  44. "--resume",
  45. sessionId,
  46. "-p",
  47. "E2E_TOOLSEARCH_COLD 检查恢复。",
  48. ]).done;
  49. expect(cold.code).toBe(0);
  50. const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
  51. expect(first.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
  52. expect(JSON.stringify(first.messages)).not.toContain('"type":"tool_reference"');
  53. expect(JSON.stringify(first.messages)).toContain(TOOLSEARCH_CALL_ID);
  54. } finally {
  55. await fixture.close();
  56. }
  57. });
  58. }
  59. it("TS04: MCP 移除后过滤旧引用,重新启用后恢复发现", async function () {
  60. this.timeout(120_000);
  61. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
  62. const name = "mcp__catalog__lookup";
  63. const server = {
  64. type: "stdio",
  65. command: process.execPath,
  66. args: [resolve("test/e2e/fixtures/mcp/toolsearch-server.mjs")],
  67. };
  68. try {
  69. await fixture.configure({
  70. features: { memory: false, skill: false, subagent: false, mcp: true },
  71. mcp: { servers: { catalog: server } },
  72. });
  73. fixture.setResponse((body, response) => {
  74. const text = JSON.stringify(body.messages);
  75. fixture.reply(
  76. body,
  77. response,
  78. body.tools?.length &&
  79. !text.includes(TOOLSEARCH_CALL_ID) &&
  80. !text.includes("E2E_TOOLSEARCH_COLD")
  81. ? "mcp-search"
  82. : "done",
  83. );
  84. });
  85. const initial = await fixture.start([
  86. "--toolsearch-mode",
  87. "native",
  88. "-p",
  89. "E2E_TOOLSEARCH_INITIAL 搜索 MCP。",
  90. ]).done;
  91. expect(initial.code).toBe(0);
  92. const { sessionId } = JSON.parse(initial.stdout) as { sessionId: string };
  93. expect(JSON.stringify(fixture.requests.at(-1)!.body.messages)).toContain(
  94. `"tool_name":"${name}"`,
  95. );
  96. for (const enabled of [false, true]) {
  97. await fixture.configure({
  98. mcp: {
  99. servers: {
  100. catalog: { ...server, enabled, env: { E2E_TOOLSEARCH_SCHEMA_REVISION: "2" } },
  101. },
  102. },
  103. });
  104. const start = fixture.requests.length;
  105. const result = await fixture.start([
  106. "--toolsearch-mode",
  107. "native",
  108. "--resume",
  109. sessionId,
  110. "-p",
  111. "E2E_TOOLSEARCH_COLD 检查当前工具池。",
  112. ]).done;
  113. expect(result.code).toBe(0);
  114. const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
  115. expect(first.tools?.some((tool) => tool.name === name)).toBe(enabled);
  116. expect(JSON.stringify(first.messages).includes(`"tool_name":"${name}"`)).toBe(enabled);
  117. if (enabled) {
  118. expect(first.tools?.find((tool) => tool.name === name)?.input_schema).toMatchObject({
  119. required: ["value", "revision"],
  120. });
  121. }
  122. }
  123. } finally {
  124. await fixture.close();
  125. }
  126. });
  127. it("TS07: 搜索参数仍在流入时中断,冷恢复不产生发现", async function () {
  128. this.timeout(120_000);
  129. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
  130. const interruptedCall = "toolu_search_interrupted";
  131. try {
  132. const seed = await fixture.start([
  133. "--toolsearch-mode",
  134. "native",
  135. "-p",
  136. "E2E_TOOLSEARCH_SEED 稳定历史。",
  137. ]).done;
  138. expect(seed.code).toBe(0);
  139. const { sessionId } = JSON.parse(seed.stdout) as { sessionId: string };
  140. fixture.setResponse((body, response) => {
  141. // 参数流 fixture 故意不结束;取消屏障仍是 CLI 实际发出的输入流事件。
  142. fixture.reply(
  143. body,
  144. response,
  145. body.tools?.length ? "search-partial" : "auxiliary",
  146. !body.tools?.length,
  147. );
  148. });
  149. const active = fixture.start(
  150. [
  151. "--toolsearch-mode",
  152. "native",
  153. "--resume",
  154. sessionId,
  155. "-p",
  156. "E2E_TOOLSEARCH_CANCEL 中断搜索。",
  157. ],
  158. "stream-json",
  159. );
  160. await browser.waitUntil(async () => active.output().includes(interruptedCall), {
  161. timeout: 30_000,
  162. timeoutMsg: "CLI 尚未发出 ToolSearch 输入流事件",
  163. });
  164. active.child.kill("SIGINT");
  165. await active.done;
  166. fixture.setResponse((body, response) => fixture.reply(body, response));
  167. const start = fixture.requests.length;
  168. const cold = await fixture.start([
  169. "--toolsearch-mode",
  170. "native",
  171. "--resume",
  172. sessionId,
  173. "-p",
  174. "E2E_TOOLSEARCH_COLD_CANCEL 恢复。",
  175. ]).done;
  176. expect(cold.code).toBe(0);
  177. const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
  178. expect(first.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
  179. expect(JSON.stringify(first.messages)).not.toContain('"type":"tool_reference"');
  180. } finally {
  181. await fixture.close();
  182. }
  183. });
  184. });