conversation-session-toolsearch-lifecycle.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { expandAssistantHistoriesWithContent } from "#e2e/helpers/conversation-session-tool-diagnostics";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import { restartIntoWorkspace } from "#e2e/helpers/model-provider-restart";
  4. import {
  5. clickFirstV4Fork,
  6. editFirstV4UserQuery,
  7. getFirstV4ForkRowId,
  8. getV4PaneSnapshot,
  9. selectV4TaskById,
  10. switchV4Model,
  11. waitForV4Edit,
  12. waitForV4Fork,
  13. waitForV4Pane,
  14. } from "#e2e/helpers/v4-conversation";
  15. import {
  16. readToolSearchRequests,
  17. TOOLSEARCH_TODO_CALL_ID,
  18. TOOLSEARCH_NOTICE,
  19. } from "#e2e/helpers/toolsearch-provider";
  20. import {
  21. compactToolSearchTask,
  22. expectToolResultPair,
  23. expectToolSearchDisabled,
  24. newToolSearchTask,
  25. setupToolSearchDesktop,
  26. TOOLSEARCH_MODEL,
  27. TOOLSEARCH_PLAIN_PROVIDER,
  28. TOOLSEARCH_PLAN_PROVIDER,
  29. toolSearchTurn,
  30. } from "#e2e/helpers/toolsearch-desktop";
  31. describe("ToolSearch provider、续写和历史生命周期", () => {
  32. before(async () => {
  33. await setupToolSearchDesktop();
  34. });
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("TS03: 同一 session 双向切 provider,默认持续关闭 ToolSearch", async function () {
  40. this.timeout(180_000);
  41. await newToolSearchTask();
  42. await toolSearchTurn("E2E_TOOLSEARCH_INITIAL 搜索 TodoRead 并执行。");
  43. const sessionId = (await getV4PaneSnapshot()).sessionId;
  44. await switchV4Model(TOOLSEARCH_PLAIN_PROVIDER, TOOLSEARCH_MODEL, "");
  45. const [off] = await toolSearchTurn("E2E_TOOLSEARCH_OFF 继续。");
  46. expectToolSearchDisabled(off!);
  47. expectToolResultPair(off!, TOOLSEARCH_TODO_CALL_ID);
  48. expect(JSON.stringify(off!.requestJson.messages)).not.toContain("tool_reference");
  49. await switchV4Model(TOOLSEARCH_PLAN_PROVIDER, TOOLSEARCH_MODEL, "");
  50. const [on] = await toolSearchTurn("E2E_TOOLSEARCH_ON 继续。");
  51. expectToolSearchDisabled(on!);
  52. expectToolResultPair(on!, TOOLSEARCH_TODO_CALL_ID);
  53. expect(JSON.stringify(on!.requestJson.messages)).not.toContain('"type":"tool_reference"');
  54. expect((await getV4PaneSnapshot()).sessionId).toBe(sessionId);
  55. });
  56. it("TS09: 默认关闭时三次 Continue 与冷恢复均不新增工具公告", async function () {
  57. this.timeout(120_000);
  58. await newToolSearchTask();
  59. const requests = await toolSearchTurn("E2E_TOOLSEARCH_CONTINUE 分三次续写后结束。");
  60. expect(requests).toHaveLength(4);
  61. for (const request of requests) expectToolSearchDisabled(request);
  62. const [next] = await toolSearchTurn("E2E_TOOLSEARCH_AFTER_CONTINUE 正常下一轮。");
  63. expectToolSearchDisabled(next!);
  64. const text = JSON.stringify(next!.requestJson.messages);
  65. expect(text).toContain("E2E_TOOLSEARCH_PARTIAL_1");
  66. expect(text).toContain("E2E_TOOLSEARCH_PARTIAL_3");
  67. // 续写提示按隐藏记录写入历史:下一轮与冷恢复都原样回放三条。
  68. expect(countContinue(text)).toBe(3);
  69. const sessionId = (await getV4PaneSnapshot()).sessionId!;
  70. await restartIntoWorkspace();
  71. await selectV4TaskById(sessionId);
  72. const [cold] = await toolSearchTurn("E2E_TOOLSEARCH_COLD_CONTINUE 检查续写的冷恢复。");
  73. expectToolSearchDisabled(cold!);
  74. const coldText = JSON.stringify(cold!.requestJson.messages);
  75. expect(coldText).toContain("E2E_TOOLSEARCH_PARTIAL_1");
  76. expect(coldText).toContain("E2E_TOOLSEARCH_PARTIAL_3");
  77. expect(countContinue(coldText)).toBe(3);
  78. });
  79. for (const disabledDuringCompact of [false, true]) {
  80. it(`TS04: ${disabledDuringCompact ? "普通 provider" : "Coding Plan"} 默认关闭期间 compact 后冷恢复`, async function () {
  81. this.timeout(240_000);
  82. await newToolSearchTask();
  83. await toolSearchTurn("E2E_TOOLSEARCH_INITIAL 搜索并执行后压缩。");
  84. const sessionId = (await getV4PaneSnapshot()).sessionId!;
  85. if (disabledDuringCompact)
  86. await switchV4Model(TOOLSEARCH_PLAIN_PROVIDER, TOOLSEARCH_MODEL, "");
  87. await compactToolSearchTask();
  88. const [after] = await toolSearchTurn("E2E_TOOLSEARCH_AFTER_COMPACT 核对压缩后的请求。");
  89. if (disabledDuringCompact) expectToolSearchDisabled(after!);
  90. else {
  91. expectToolSearchDisabled(after!);
  92. }
  93. // 摘要移走旧调用;默认关闭时仍按当前目录发送完整 schema。
  94. expect(JSON.stringify(after!.requestJson.messages)).not.toContain(TOOLSEARCH_TODO_CALL_ID);
  95. await restartIntoWorkspace();
  96. await selectV4TaskById(sessionId);
  97. await switchV4Model(TOOLSEARCH_PLAN_PROVIDER, TOOLSEARCH_MODEL, "");
  98. const cold = await toolSearchTurn("E2E_TOOLSEARCH_COLD_COMPACT 检查首请求。");
  99. expect(cold).toHaveLength(1);
  100. expectToolSearchDisabled(cold[0]!);
  101. expect(JSON.stringify(cold[0]!.requestJson.messages)).not.toContain(TOOLSEARCH_TODO_CALL_ID);
  102. });
  103. }
  104. it("TS05: 默认关闭时 fork 旧稳定轮不继承未来工具调用", async function () {
  105. this.timeout(180_000);
  106. await newToolSearchTask();
  107. await toolSearchTurn("E2E_TOOLSEARCH_SEED 尚未搜索的稳定轮。");
  108. const parentId = (await getV4PaneSnapshot()).sessionId!;
  109. await waitForV4Fork();
  110. const forkRowId = await getFirstV4ForkRowId();
  111. await toolSearchTurn("E2E_TOOLSEARCH_INITIAL 现在搜索 TodoRead。");
  112. // 历史轮会折叠;“第一个可见按钮”可能属于最新轮,必须点预先记录的旧稳定轮。
  113. await expandAssistantHistoriesWithContent();
  114. // 修复原因:直接 click 只点到触发器;工作区在 git 仓库内时 fork 是下拉菜单,需再选「在此处 fork」。
  115. // helper 同时兼容非 git 工作区,并支持指定行。
  116. expect(await clickFirstV4Fork(forkRowId)).toBe(true);
  117. await waitForV4Pane(
  118. (s) => !!s.sessionId && s.sessionId !== "draft" && s.sessionId !== parentId,
  119. "fork 未切换到子分支",
  120. 30_000,
  121. );
  122. const [child] = await toolSearchTurn("E2E_TOOLSEARCH_FORK 检查 fork 点工具池。");
  123. expectToolSearchDisabled(child!);
  124. await selectV4TaskById(parentId);
  125. const [parent] = await toolSearchTurn("E2E_TOOLSEARCH_AFTER_FORK 父分支检查。");
  126. expectToolSearchDisabled(parent!);
  127. expectToolResultPair(parent!, TOOLSEARCH_TODO_CALL_ID);
  128. });
  129. it("TS05: 默认关闭时编辑首轮不带回旧工具调用", async function () {
  130. this.timeout(120_000);
  131. await newToolSearchTask();
  132. await toolSearchTurn("E2E_TOOLSEARCH_INITIAL 原始搜索轮。");
  133. const before = (await readToolSearchRequests()).length;
  134. await waitForV4Edit();
  135. await editFirstV4UserQuery("E2E_TOOLSEARCH_EDITED 不搜索的新分支。");
  136. await browser.waitUntil(async () => (await readToolSearchRequests()).length > before, {
  137. timeout: 45_000,
  138. });
  139. await waitForV4Pane((s) => !s.canStop, "编辑重跑未结束", 30_000);
  140. const edited = (await readToolSearchRequests())[before]!;
  141. expectToolSearchDisabled(edited);
  142. expect(JSON.stringify(edited.requestJson.messages)).not.toContain(TOOLSEARCH_TODO_CALL_ID);
  143. expect(JSON.stringify(edited.requestJson.messages)).not.toContain(TOOLSEARCH_NOTICE);
  144. });
  145. });
  146. function countContinue(text: string): number {
  147. return text.split("Output token limit hit. Resume directly").length - 1;
  148. }