conversation-session-edit.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { clearAppData } from "../../../helpers/desktop-app.js";
  2. import { waitForUpstreamNetworkCapture } from "../../../helpers/upstream-capture.js";
  3. import {
  4. E2E_REPLY_TOKEN,
  5. assertVisibleUserMessagesNotContaining,
  6. buildReadonlyToolPrompt,
  7. clickChatStop,
  8. countUpstreamRequestsContaining,
  9. editUserMessageContaining,
  10. expectNoUpstreamRequestForTextWithin,
  11. getEditButtonForAssistantContaining,
  12. getQueueItems,
  13. getTaskStoreSnapshot,
  14. prepareConversationE2E,
  15. sendPrompt,
  16. startNewTask,
  17. typeChatPrompt,
  18. clickChatSend,
  19. waitForAssistantMessageContaining,
  20. waitForChatState,
  21. waitForCompactMarkerStatus,
  22. waitForComposerText,
  23. waitForUpstreamRequestContaining,
  24. waitForEditButtonForUserContaining,
  25. waitForQueueContaining,
  26. waitForQueueCount,
  27. waitForToastContaining,
  28. waitForUserMessageContaining,
  29. } from "../../../helpers/conversation-session.js";
  30. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  31. const COMPACT_DUPLICATE_BLOCKED_TOAST_TEXTS = [
  32. "A compaction is already running or queued.",
  33. "已有压缩任务正在运行或排队。",
  34. ];
  35. const RESOLVE_USER_MESSAGE_ERROR = "Cannot resolve user message";
  36. const REWIND_RUNNING_ERROR = "Cannot rewind while a prompt is running";
  37. const EDIT_INTERRUPTED_SOURCE_REPLY_PREFIX = "deep";
  38. const EDIT_INTERRUPTED_RERUN_REPLY_TOKEN = "upstream-e2e-edit-rerun-ok";
  39. describe("会话区 Edit User Query E2E", () => {
  40. after(async () => {
  41. await browser.electron.restoreAllMocks();
  42. await clearAppData();
  43. });
  44. it("user query edit 应立即重跑;held queue 存在时仍保留 queue,running latest edit 可抢占", async function () {
  45. this.timeout(220000);
  46. await prepareConversationE2E();
  47. const runId = Date.now();
  48. const originalPrompt = `E2E_EDIT_ORIGINAL_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  49. await sendPrompt(originalPrompt);
  50. await waitForUpstreamNetworkCapture(`E2E_EDIT_ORIGINAL_${runId}`);
  51. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  52. await waitForChatState(
  53. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  54. "edit 前首轮没有完成",
  55. 90000,
  56. );
  57. expect((await getEditButtonForAssistantContaining(E2E_REPLY_TOKEN))?.exists).not.toBe(true);
  58. const editedPrompt = buildReadonlyToolPrompt(`E2E_EDIT_RERUN_${runId}`);
  59. await editUserMessageContaining(`E2E_EDIT_ORIGINAL_${runId}`, editedPrompt);
  60. await waitForComposerText("", "edit 提交后输入框没有清空");
  61. await waitForUpstreamNetworkCapture(`E2E_EDIT_RERUN_${runId}`);
  62. await waitForUserMessageContaining(`E2E_EDIT_RERUN_${runId}`);
  63. await assertVisibleUserMessagesNotContaining(`E2E_EDIT_ORIGINAL_${runId}`);
  64. await waitForChatState(
  65. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  66. "edit 重跑完成后会话没有回到 idle",
  67. 90000,
  68. );
  69. await startNewTask();
  70. const slowPrompt = `E2E_SLOW_STREAM E2E_EDIT_HELD_RUNNING_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  71. await sendPrompt(slowPrompt);
  72. await waitForComposerText("", "held queue 场景首轮发送后输入框没有清空");
  73. await waitForChatState(
  74. (snapshot) => snapshot.state === "streaming",
  75. "held queue 场景首轮没有进入 streaming",
  76. 30000,
  77. );
  78. const heldPrompt = `E2E_EDIT_HELD_QUEUE_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  79. await sendPrompt(heldPrompt);
  80. await waitForQueueContaining(`E2E_EDIT_HELD_QUEUE_${runId}`);
  81. const heldQueue = await waitForQueueCount(1);
  82. const heldQueueItemId = heldQueue[0]?.id;
  83. expect(heldQueueItemId).toBeTruthy();
  84. await clickChatStop();
  85. await waitForChatState(
  86. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 1,
  87. "stop 后没有保留 held queue",
  88. 30000,
  89. );
  90. await expectNoUpstreamRequestForTextWithin(`E2E_EDIT_HELD_QUEUE_${runId}`, 900);
  91. // 修复原因:edit 重跑必须精确命中 case-local controlled stream;
  92. // 否则会回退到 legacy 90.2s slow stream,并在 90s 等待边界误报成产品 bug。
  93. const heldEditPrompt = `E2E_SLOW_STREAM E2E_EDIT_HELD_RERUN_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  94. await editUserMessageContaining(`E2E_EDIT_HELD_RUNNING_${runId}`, heldEditPrompt);
  95. await waitForUpstreamRequestContaining(`E2E_EDIT_HELD_RERUN_${runId}`);
  96. await waitForUserMessageContaining(`E2E_EDIT_HELD_RERUN_${runId}`);
  97. await waitForChatState(
  98. (snapshot) => snapshot.state === "streaming" && snapshot.queueCount === 1,
  99. "held queue 存在时 edit 没有立即重跑并保留 queue",
  100. 30000,
  101. );
  102. expect((await getQueueItems())[0]?.id).toBe(heldQueueItemId);
  103. await waitForEditButtonForUserContaining(`E2E_EDIT_HELD_RERUN_${runId}`);
  104. const followupPrompt = `E2E_EDIT_RUNNING_FOLLOWUP_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  105. await sendPrompt(followupPrompt);
  106. await waitForQueueContaining(`E2E_EDIT_RUNNING_FOLLOWUP_${runId}`);
  107. await waitForQueueCount(2);
  108. const goalPrompt = `/goal E2E_EDIT_RUNNING_GOAL_${runId}`;
  109. await sendPrompt(goalPrompt);
  110. await waitForQueueContaining(`E2E_EDIT_RUNNING_GOAL_${runId}`);
  111. let queue = await waitForQueueCount(3);
  112. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  113. await expectNoUpstreamRequestForTextWithin(`E2E_EDIT_RUNNING_GOAL_${runId}`, 800);
  114. await waitForChatState(
  115. (snapshot) =>
  116. snapshot.state !== "streaming" &&
  117. snapshot.runtimeStatus === "completed" &&
  118. snapshot.queueCount === 3,
  119. "edit 重跑完成后没有稳定保留 3 条 queue",
  120. 90000,
  121. );
  122. const compactRequestsBefore = await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL);
  123. await sendPrompt("/compact");
  124. await waitForComposerText("", "held queue 下手动 compact 发送后输入框没有清空");
  125. const startedMarker = await waitForCompactMarkerStatus("started", "manual");
  126. expect(startedMarker.inputId).toBeTruthy();
  127. await browser.waitUntil(
  128. async () =>
  129. (await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL)) ===
  130. compactRequestsBefore + 1,
  131. {
  132. timeout: 10000,
  133. timeoutMsg: "held queue 下没有捕获到手动 compact 请求",
  134. },
  135. );
  136. queue = await waitForQueueCount(3);
  137. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  138. const duplicateCompactRequestsBefore =
  139. await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL);
  140. await typeChatPrompt("/compact");
  141. await clickChatSend();
  142. await waitForToastContaining(
  143. COMPACT_DUPLICATE_BLOCKED_TOAST_TEXTS,
  144. "compacting 中重复 /compact 没有弹出 duplicate blocked toast",
  145. );
  146. await expectNoUpstreamRequestForTextWithin(COMPACT_REQUEST_SENTINEL, 800);
  147. expect(await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL)).toBe(
  148. duplicateCompactRequestsBefore,
  149. );
  150. queue = await waitForQueueCount(3);
  151. expect(queue.map((item) => item.kind)).toEqual(["text", "text", "goal"]);
  152. await assertVisibleUserMessagesNotContaining("/compact");
  153. const completedMarker = await waitForCompactMarkerStatus(
  154. "completed",
  155. "manual",
  156. startedMarker.inputId,
  157. );
  158. expect(completedMarker.operationId).toBeTruthy();
  159. await waitForChatState(
  160. (snapshot) =>
  161. snapshot.state !== "streaming" &&
  162. snapshot.runtimeStatus === "completed" &&
  163. snapshot.queueCount === 3,
  164. "held queue 下手动 compact 完成后没有继续保留 queue",
  165. 90000,
  166. );
  167. });
  168. it("中断模型回答后编辑已发送 user query 并重新提交,不应触发 turnIndex 解析错误", async function () {
  169. this.timeout(160000);
  170. await prepareConversationE2E();
  171. const runId = Date.now();
  172. const sourceMarker = `E2E_EDIT_INTERRUPTED_RESUBMIT_SOURCE_${runId}`;
  173. const rerunMarker = `E2E_EDIT_INTERRUPTED_RESUBMIT_RERUN_${runId}`;
  174. const rerunReplyToken = EDIT_INTERRUPTED_RERUN_REPLY_TOKEN;
  175. const sourcePrompt = `E2E_SLOW_STREAM ${sourceMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  176. await sendPrompt(sourcePrompt);
  177. await waitForComposerText("", "interrupted resubmit 源 prompt 发送后输入框没有清空");
  178. const runningSnapshot = await waitForChatState(
  179. (snapshot) =>
  180. snapshot.state === "streaming" && Boolean(snapshot.sessionId || snapshot.taskId),
  181. "interrupted resubmit 源 prompt 没有进入 streaming",
  182. 30000,
  183. );
  184. const taskId = runningSnapshot.sessionId || runningSnapshot.taskId;
  185. if (!taskId) {
  186. throw new Error("interrupted resubmit 源 prompt 缺少 taskId/sessionId");
  187. }
  188. // 修复原因:默认 replay 在首个 assistant delta 之前 stop 时会触发 stream
  189. // recovery;真实 capture 下等完整回复又可能错过 stop 按钮,所以等首个 delta 后立刻中断。
  190. await waitForAssistantMessageContaining(EDIT_INTERRUPTED_SOURCE_REPLY_PREFIX);
  191. await clickChatStop();
  192. await waitForUserMessageContaining(sourceMarker);
  193. await waitForChatState(
  194. (snapshot) =>
  195. (snapshot.sessionId || snapshot.taskId) === taskId &&
  196. snapshot.state !== "streaming" &&
  197. snapshot.runtimeStatus === "completed" &&
  198. snapshot.queueCount === 0,
  199. "interrupted resubmit stop 后没有进入可编辑的 completed(interrupted) 状态",
  200. 30000,
  201. );
  202. // 修复原因:interrupted edit 场景需要用不同回复区分旧 assistant 与 edit 后 rerun 结果,
  203. // 否则 UI 上看到同样 token 时无法判断是否真的执行了新 query。
  204. const editedPrompt = `${rerunMarker}: Reply with exactly "${rerunReplyToken}" and no other text.`;
  205. // 修复原因:Stop 后 renderer completed/stop ACK 可能早于 CLI protocol 的 active turn finally。
  206. // D10 只验证 interrupted user message 不再触发 turnIndex 解析错误;如果 active turn
  207. // 尚未释放,rewind busy guard 是可接受的即时编辑结果,不能强制要求 rerun 一定发出。
  208. await browser.pause(500);
  209. await editUserMessageContaining(sourceMarker, editedPrompt);
  210. await waitForComposerText("", "interrupted resubmit edit 提交后输入框没有清空");
  211. const outcome = await waitForInterruptedEditOutcomeWithoutResolveUserMessageError(
  212. rerunMarker,
  213. taskId,
  214. );
  215. if (outcome === "busy") {
  216. await expectNoUpstreamRequestForTextWithin(rerunMarker, 800);
  217. return;
  218. }
  219. await waitForUserMessageContaining(rerunMarker);
  220. await assertVisibleUserMessagesNotContaining(sourceMarker);
  221. await waitForAssistantMessageContaining(rerunReplyToken);
  222. await waitForChatState(
  223. (snapshot) =>
  224. (snapshot.sessionId || snapshot.taskId) === taskId &&
  225. snapshot.state === "idle" &&
  226. snapshot.queueCount === 0,
  227. "interrupted resubmit edit 重跑后没有回到 idle",
  228. 90000,
  229. );
  230. });
  231. });
  232. async function waitForInterruptedEditOutcomeWithoutResolveUserMessageError(
  233. expectedText: string,
  234. taskId: string,
  235. ): Promise<"rerun" | "busy"> {
  236. let latestStore: Awaited<ReturnType<typeof getTaskStoreSnapshot>> | null = null;
  237. let requestCount = 0;
  238. try {
  239. await browser.waitUntil(
  240. async () => {
  241. const [nextRequestCount, nextStore] = await Promise.all([
  242. countUpstreamRequestsContaining(expectedText),
  243. getTaskStoreSnapshot(taskId),
  244. ]);
  245. requestCount = nextRequestCount;
  246. latestStore = nextStore;
  247. const taskErrorText = getTaskErrorText(latestStore);
  248. return (
  249. requestCount > 0 ||
  250. hasCannotResolveUserMessageError(taskErrorText) ||
  251. hasRewindRunningError(taskErrorText)
  252. );
  253. },
  254. {
  255. timeout: 45000,
  256. timeoutMsg: `interrupted resubmit edit 没有发起重跑请求: ${expectedText}`,
  257. },
  258. );
  259. } catch (error) {
  260. latestStore = await getTaskStoreSnapshot(taskId);
  261. throw new Error(
  262. [
  263. `interrupted resubmit edit 没有发起重跑请求: ${expectedText}`,
  264. `requestCount=${requestCount}`,
  265. `taskStore=${JSON.stringify(latestStore)}`,
  266. ].join("; "),
  267. { cause: error },
  268. );
  269. }
  270. const taskErrorText = getTaskErrorText(latestStore);
  271. if (hasCannotResolveUserMessageError(taskErrorText)) {
  272. throw new Error(`interrupted resubmit edit 触发了 user message 解析错误: ${taskErrorText}`);
  273. }
  274. if (requestCount > 0) {
  275. return "rerun";
  276. }
  277. if (hasRewindRunningError(taskErrorText)) {
  278. return "busy";
  279. }
  280. throw new Error(
  281. `interrupted resubmit edit 没有得到 rerun 或 busy guard: ${expectedText}; taskStore=${JSON.stringify(
  282. latestStore,
  283. )}`,
  284. );
  285. }
  286. function getTaskErrorText(snapshot: Awaited<ReturnType<typeof getTaskStoreSnapshot>> | null) {
  287. return [snapshot?.error, snapshot?.uiError].filter(Boolean).join("\n");
  288. }
  289. function hasCannotResolveUserMessageError(errorText: string) {
  290. return (
  291. errorText.includes(RESOLVE_USER_MESSAGE_ERROR) ||
  292. errorText.includes("Cannot resolve user message for turnlndex")
  293. );
  294. }
  295. function hasRewindRunningError(errorText: string) {
  296. return errorText.includes(REWIND_RUNNING_ERROR);
  297. }