conversation-session-edit.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import { waitForUpstreamNetworkCapture } from "#e2e/helpers/upstream-capture";
  3. import {
  4. E2E_REPLY_TOKEN,
  5. assertVisibleV4UserMessagesNotContaining,
  6. clickV4Stop,
  7. clickV4QueueItemSendNow,
  8. prepareV4ConversationE2E,
  9. sendV4Prompt,
  10. startNewV4Draft,
  11. setV4ComposerText,
  12. clickV4Send,
  13. waitForV4AssistantMessageContaining,
  14. waitForV4ConversationState,
  15. waitForV4ComposerText,
  16. waitForV4UserMessageContaining,
  17. waitForLatestV4CommandAck,
  18. } from "#e2e/helpers/v4-conversation";
  19. import {
  20. buildReadonlyToolPrompt,
  21. ensureReadonlyToolFixtureFile,
  22. } from "#e2e/helpers/conversation-session-readonly-tool";
  23. import {
  24. countUpstreamRequestsContaining,
  25. expectNoUpstreamRequestForTextWithin,
  26. waitForUpstreamRequestContaining,
  27. } from "#e2e/helpers/conversation-session-network";
  28. import { editV4UserMessageContaining as editUserMessageContaining } from "#e2e/helpers/v4-message-actions";
  29. import { getV4MessageAction, waitForV4MessageAction } from "#e2e/helpers/v4-message-actions";
  30. import {
  31. getQueueItems,
  32. waitForQueueContaining,
  33. waitForQueueCount,
  34. } from "#e2e/helpers/conversation-session-queue";
  35. import { waitForCompactMarkerStatus } from "#e2e/helpers/conversation-session-compact";
  36. import { waitForToastContaining } from "#e2e/helpers/conversation-session-toast";
  37. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  38. const COMPACT_DUPLICATE_BLOCKED_TOAST_TEXTS = [
  39. "A compaction is already running or queued.",
  40. "已有压缩任务正在运行或排队。",
  41. ];
  42. const EDIT_INTERRUPTED_SOURCE_REPLY_PREFIX = "deep";
  43. const EDIT_INTERRUPTED_RERUN_REPLY_TOKEN = "upstream-e2e-edit-rerun-ok";
  44. describe("会话区 Edit User Query E2E", () => {
  45. after(async () => {
  46. await browser.electron.restoreAllMocks();
  47. await clearAppData();
  48. });
  49. it("user query edit 应立即重跑;held queue 存在时仍保留 queue,running latest edit 可抢占", async function () {
  50. this.timeout(220000);
  51. await prepareV4ConversationE2E();
  52. // 原因:旧聚合 helper 隐式创建只读文件;V4 用例显式保留工具执行前置。
  53. await ensureReadonlyToolFixtureFile();
  54. const runId = Date.now();
  55. const originalPrompt = `E2E_EDIT_ORIGINAL_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  56. await sendV4Prompt(originalPrompt);
  57. await waitForUpstreamNetworkCapture(`E2E_EDIT_ORIGINAL_${runId}`);
  58. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  59. await waitForV4ConversationState(
  60. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  61. "edit 前首轮没有完成",
  62. 90000,
  63. );
  64. expect((await getV4MessageAction("assistant", "edit", E2E_REPLY_TOKEN))?.exists).not.toBe(true);
  65. const editedPrompt = buildReadonlyToolPrompt(`E2E_EDIT_RERUN_${runId}`);
  66. await editUserMessageContaining(`E2E_EDIT_ORIGINAL_${runId}`, editedPrompt);
  67. // 已复现单次点击后 resync、没有模型请求;记录 ACK,不能把命令拒绝归为模型缺响应。
  68. const editAck = await waitForLatestV4CommandAck("editUserQuery");
  69. if (editAck.status !== "accepted") {
  70. throw new Error(`首次编辑提交未接纳: ${JSON.stringify(editAck)}`);
  71. }
  72. await waitForV4ComposerText("", "edit 提交后输入框没有清空");
  73. await waitForUpstreamNetworkCapture(`E2E_EDIT_RERUN_${runId}`);
  74. await waitForV4UserMessageContaining(`E2E_EDIT_RERUN_${runId}`);
  75. await assertVisibleV4UserMessagesNotContaining(`E2E_EDIT_ORIGINAL_${runId}`);
  76. await waitForV4ConversationState(
  77. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  78. "edit 重跑完成后会话没有回到 idle",
  79. 90000,
  80. );
  81. await startNewV4Draft();
  82. const slowPrompt = `E2E_SLOW_STREAM E2E_EDIT_HELD_RUNNING_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  83. await sendV4Prompt(slowPrompt);
  84. await waitForV4ComposerText("", "held queue 场景首轮发送后输入框没有清空");
  85. await waitForV4ConversationState(
  86. (snapshot) => snapshot.state === "streaming",
  87. "held queue 场景首轮没有进入 streaming",
  88. 30000,
  89. );
  90. const heldPrompt = `E2E_EDIT_HELD_QUEUE_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  91. await sendV4Prompt(heldPrompt);
  92. await waitForQueueContaining(`E2E_EDIT_HELD_QUEUE_${runId}`);
  93. const heldQueue = await waitForQueueCount(1);
  94. const heldQueueItemId = heldQueue[0]?.id;
  95. expect(heldQueueItemId).toBeTruthy();
  96. await clickV4Stop();
  97. await waitForV4ConversationState(
  98. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 1,
  99. "stop 后没有保留 held queue",
  100. 30000,
  101. );
  102. await expectNoUpstreamRequestForTextWithin(`E2E_EDIT_HELD_QUEUE_${runId}`, 900);
  103. // 修复原因:edit 重跑必须精确命中 case-local controlled stream;
  104. // 否则会回退到 legacy 90.2s slow stream,并在 90s 等待边界误报成产品 bug。
  105. const heldEditPrompt = `E2E_SLOW_STREAM E2E_EDIT_HELD_RERUN_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  106. await editUserMessageContaining(`E2E_EDIT_HELD_RUNNING_${runId}`, heldEditPrompt);
  107. await waitForUpstreamRequestContaining(`E2E_EDIT_HELD_RERUN_${runId}`);
  108. await waitForV4UserMessageContaining(`E2E_EDIT_HELD_RERUN_${runId}`);
  109. await waitForV4ConversationState(
  110. (snapshot) => snapshot.state === "streaming" && snapshot.queueCount === 1,
  111. "held queue 存在时 edit 没有立即重跑并保留 queue",
  112. 30000,
  113. );
  114. expect((await getQueueItems())[0]?.id).toBe(heldQueueItemId);
  115. await waitForV4MessageAction("user", "edit", `E2E_EDIT_HELD_RERUN_${runId}`);
  116. const followupPrompt = `E2E_EDIT_RUNNING_FOLLOWUP_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  117. await sendV4Prompt(followupPrompt);
  118. await waitForQueueContaining(`E2E_EDIT_RUNNING_FOLLOWUP_${runId}`);
  119. await waitForQueueCount(2);
  120. const goalPrompt = `/goal E2E_EDIT_RUNNING_GOAL_${runId}`;
  121. await sendV4Prompt(goalPrompt);
  122. await waitForQueueContaining(`E2E_EDIT_RUNNING_GOAL_${runId}`);
  123. let queue = await waitForQueueCount(3);
  124. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  125. await expectNoUpstreamRequestForTextWithin(`E2E_EDIT_RUNNING_GOAL_${runId}`, 800);
  126. await waitForV4ConversationState(
  127. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 3,
  128. "edit 重跑完成后没有稳定保留 3 条 queue",
  129. 90000,
  130. );
  131. const compactRequestsBefore = await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL);
  132. await sendV4Prompt("/compact");
  133. await waitForV4ComposerText("", "held queue 下手动 compact 发送后输入框没有清空");
  134. // 暂停队列不会自动执行维护命令;从 compact 队列项明确立即发送,保留原有三项。
  135. const compactQueue = await waitForQueueCount(4);
  136. expect(compactQueue[3]?.kind).toBe("compact");
  137. await clickV4QueueItemSendNow(compactQueue[3]!.id);
  138. const startedMarker = await waitForCompactMarkerStatus("started", "manual");
  139. expect(startedMarker.inputId).toBeTruthy();
  140. await browser.waitUntil(
  141. async () =>
  142. (await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL)) ===
  143. compactRequestsBefore + 1,
  144. {
  145. timeout: 10000,
  146. timeoutMsg: "held queue 下没有捕获到手动 compact 请求",
  147. },
  148. );
  149. queue = await waitForQueueCount(3);
  150. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  151. const duplicateCompactRequestsBefore =
  152. await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL);
  153. await setV4ComposerText("/compact");
  154. await clickV4Send();
  155. await waitForToastContaining(
  156. COMPACT_DUPLICATE_BLOCKED_TOAST_TEXTS,
  157. "compacting 中重复 /compact 没有弹出 duplicate blocked toast",
  158. );
  159. await expectNoUpstreamRequestForTextWithin(COMPACT_REQUEST_SENTINEL, 800);
  160. expect(await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL)).toBe(
  161. duplicateCompactRequestsBefore,
  162. );
  163. queue = await waitForQueueCount(3);
  164. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  165. await assertVisibleV4UserMessagesNotContaining("/compact");
  166. const completedMarker = await waitForCompactMarkerStatus(
  167. "completed",
  168. "manual",
  169. startedMarker.inputId,
  170. );
  171. expect(completedMarker.inputId).toBe(startedMarker.inputId);
  172. await waitForV4ConversationState(
  173. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 3,
  174. "held queue 下手动 compact 完成后没有继续保留 queue",
  175. 90000,
  176. );
  177. });
  178. it("中断模型回答后编辑已发送 user query 并重新提交,不应触发 turnIndex 解析错误", async function () {
  179. this.timeout(160000);
  180. await prepareV4ConversationE2E();
  181. await ensureReadonlyToolFixtureFile();
  182. const runId = Date.now();
  183. const sourceMarker = `E2E_EDIT_INTERRUPTED_RESUBMIT_SOURCE_${runId}`;
  184. const rerunMarker = `E2E_EDIT_INTERRUPTED_RESUBMIT_RERUN_${runId}`;
  185. const rerunReplyToken = EDIT_INTERRUPTED_RERUN_REPLY_TOKEN;
  186. const sourcePrompt = `E2E_SLOW_STREAM ${sourceMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  187. await sendV4Prompt(sourcePrompt);
  188. await waitForV4ComposerText("", "interrupted resubmit 源 prompt 发送后输入框没有清空");
  189. const runningSnapshot = await waitForV4ConversationState(
  190. (snapshot) =>
  191. snapshot.state === "streaming" && Boolean(snapshot.sessionId || snapshot.taskId),
  192. "interrupted resubmit 源 prompt 没有进入 streaming",
  193. 30000,
  194. );
  195. const taskId = runningSnapshot.sessionId || runningSnapshot.taskId;
  196. if (!taskId) {
  197. throw new Error("interrupted resubmit 源 prompt 缺少 taskId/sessionId");
  198. }
  199. // 修复原因:默认 replay 在首个 assistant delta 之前 stop 时会触发 stream
  200. // recovery;真实 capture 下等完整回复又可能错过 stop 按钮,所以等首个 delta 后立刻中断。
  201. await waitForV4AssistantMessageContaining(EDIT_INTERRUPTED_SOURCE_REPLY_PREFIX);
  202. await clickV4Stop();
  203. await waitForV4UserMessageContaining(sourceMarker);
  204. await waitForV4ConversationState(
  205. (snapshot) =>
  206. (snapshot.sessionId || snapshot.taskId) === taskId &&
  207. snapshot.state !== "streaming" &&
  208. snapshot.queueCount === 0,
  209. "interrupted resubmit stop 后没有进入可编辑的 completed(interrupted) 状态",
  210. 30000,
  211. );
  212. // 修复原因:interrupted edit 场景需要用不同回复区分旧 assistant 与 edit 后 rerun 结果,
  213. // 否则 UI 上看到同样 token 时无法判断是否真的执行了新 query。
  214. const editedPrompt = `${rerunMarker}: Reply with exactly "${rerunReplyToken}" and no other text.`;
  215. await editUserMessageContaining(sourceMarker, editedPrompt);
  216. await waitForV4ComposerText("", "interrupted resubmit edit 提交后输入框没有清空");
  217. await waitForUpstreamRequestContaining(rerunMarker);
  218. await waitForV4UserMessageContaining(rerunMarker);
  219. await assertVisibleV4UserMessagesNotContaining(sourceMarker);
  220. await waitForV4AssistantMessageContaining(rerunReplyToken);
  221. await waitForV4ConversationState(
  222. (snapshot) =>
  223. (snapshot.sessionId || snapshot.taskId) === taskId &&
  224. snapshot.state === "idle" &&
  225. snapshot.queueCount === 0,
  226. "interrupted resubmit edit 重跑后没有回到 idle",
  227. 90000,
  228. );
  229. });
  230. });