conversation-session-goal-run-cases.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /* eslint-disable max-lines -- 22 个历史 MR-GOAL 编号集中保留,便于 catalog 与正式门禁逐条对账。 */
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. countUpstreamRequestsContaining,
  5. expectNoUpstreamRequestForTextWithin,
  6. waitForUpstreamRequest,
  7. waitForUpstreamRequestContaining,
  8. } from "#e2e/helpers/conversation-session-network";
  9. import {
  10. clickFirstQueueSendNow,
  11. clickQueueEdit,
  12. clickQueueRemove,
  13. getQueueItems,
  14. waitForQueueContaining,
  15. waitForQueueCount,
  16. waitForQueueOrderContaining,
  17. } from "#e2e/helpers/conversation-session-queue";
  18. import { expandAssistantHistoriesWithContent } from "#e2e/helpers/conversation-session-tool";
  19. import { getV4ComposerEditState, waitForV4ComposerEditClosed } from "#e2e/helpers/v4-composer-edit";
  20. import {
  21. E2E_REPLY_TOKEN,
  22. assertVisibleV4UserMessagesNotContaining,
  23. clickV4Stop,
  24. editFirstV4UserQuery,
  25. dragV4QueueItemOnto,
  26. getV4Messages,
  27. getV4ConversationState,
  28. getV4ComposerText,
  29. getV4CompactMarkers,
  30. prepareV4ConversationE2E,
  31. selectV4TaskById,
  32. sendV4Prompt,
  33. sendV4PausedQueueKeep,
  34. startNewV4Draft,
  35. type V4CompactMarkerSnapshot,
  36. waitForV4AssistantMessageContaining,
  37. waitForV4CompactMarker,
  38. waitForV4ComposerText,
  39. waitForV4ConversationState,
  40. waitForV4Edit,
  41. waitForV4Fork,
  42. waitForV4UserMessageContaining,
  43. } from "#e2e/helpers/v4-conversation";
  44. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  45. const CASE_TIMEOUT_MS = 180000;
  46. describe("会话区 Goal Run 正式 E2E", () => {
  47. before(async () => {
  48. await prepareV4ConversationE2E();
  49. });
  50. afterEach(async () => {
  51. await stopCurrentWork();
  52. });
  53. after(async () => {
  54. await browser.electron.restoreAllMocks();
  55. await clearAppData();
  56. });
  57. it("MR-GOAL-01 completed 无 goal 时设置 goal", async function () {
  58. this.timeout(CASE_TIMEOUT_MS);
  59. await freshTask();
  60. const marker = goalFastMarker(1);
  61. await sendGoal(marker);
  62. const snapshot = await waitForGoalObjective(marker);
  63. expect(snapshot.queueCount).toBe(0);
  64. expect(snapshot.targetStatus).toBe("active");
  65. await waitForV4UserMessageContaining(marker);
  66. });
  67. it("MR-GOAL-02 completed 已有 goal 时更新 goal", async function () {
  68. this.timeout(CASE_TIMEOUT_MS);
  69. await freshTask();
  70. const first = goalFastMarker(2, "OLD");
  71. const updated = goalSlowMarker(2, "NEW");
  72. await sendGoal(first);
  73. await waitForGoalComplete(first);
  74. await sendGoal(updated);
  75. const snapshot = await waitForGoalObjective(updated);
  76. expect(snapshot.targetObjective).not.toContain(first);
  77. expect(snapshot.queueCount).toBe(0);
  78. });
  79. it("MR-GOAL-03 running 无 goal 时发送 /goal 入队", async function () {
  80. this.timeout(CASE_TIMEOUT_MS);
  81. await freshTask();
  82. const running = slowMarker(3);
  83. const goal = goalSlowMarker(3);
  84. await startSlowPrompt(running);
  85. await sendV4Prompt(goalPrompt(goal));
  86. const queue = await waitForQueueCount(1);
  87. expect(queue[0]?.kind).toBe("goal");
  88. expect(queue[0]?.content).toContain(goal);
  89. await expectNoUpstreamRequestForTextWithin(goal, 600);
  90. });
  91. it("MR-GOAL-04 running 已有 goal 时发送新 /goal 入队", async function () {
  92. this.timeout(CASE_TIMEOUT_MS);
  93. await freshTask();
  94. const existing = goalFastMarker(4, "OLD");
  95. const running = slowMarker(4);
  96. const queued = goalSlowMarker(4, "NEW");
  97. await sendGoal(existing);
  98. await waitForGoalComplete(existing);
  99. await startSlowPrompt(running);
  100. await sendV4Prompt(goalPrompt(queued));
  101. const queue = await waitForQueueCount(1);
  102. expect(queue[0]?.kind).toBe("goal");
  103. expect(queue[0]?.content).toContain(queued);
  104. expect((await getV4ConversationState()).targetObjective).toContain(existing);
  105. });
  106. it("MR-GOAL-05 goal running 中 stop 后 target paused", async function () {
  107. this.timeout(CASE_TIMEOUT_MS);
  108. await freshTask();
  109. const goal = goalSlowMarker(5);
  110. await sendGoal(goal);
  111. await waitForGoalObjective(goal);
  112. await clickV4Stop();
  113. const snapshot = await waitForV4ConversationState(
  114. (candidate) =>
  115. candidate.state !== "streaming" && candidate.targetObjective?.includes(goal) === true,
  116. "MR-GOAL-05 Stop 后 goal work 没有结束",
  117. 30000,
  118. );
  119. expect(snapshot.targetStatus).toBe("paused");
  120. });
  121. it("MR-GOAL-06 interrupted held queue 后显式保留并启动新 /goal", async function () {
  122. this.timeout(CASE_TIMEOUT_MS);
  123. await freshTask();
  124. const running = slowMarker(6);
  125. const first = goalSlowMarker(6, "FIRST");
  126. const second = goalSlowMarker(6, "SECOND");
  127. await startSlowPrompt(running);
  128. await sendV4Prompt(goalPrompt(first));
  129. await waitForQueueCount(1);
  130. await clickV4Stop();
  131. await waitForHeldQueue(1);
  132. await sendV4PausedQueueKeep(goalPrompt(second));
  133. const snapshot = await waitForGoalObjective(second);
  134. const queue = await waitForQueueCount(1);
  135. expect(snapshot.targetObjective).toContain(second);
  136. expect(queue[0]?.kind).toBe("goal");
  137. expect(queue[0]?.content).toContain(first);
  138. });
  139. it("MR-GOAL-07 held goal queue 可编辑、重排、删除", async function () {
  140. this.timeout(CASE_TIMEOUT_MS);
  141. await freshTask();
  142. const running = slowMarker(7);
  143. const first = goalSlowMarker(7, "FIRST");
  144. const second = goalSlowMarker(7, "SECOND");
  145. const third = goalSlowMarker(7, "THIRD");
  146. const edited = goalSlowMarker(7, "SECOND_EDITED");
  147. await startSlowPrompt(running);
  148. await sendV4Prompt(goalPrompt(first));
  149. await sendV4Prompt(goalPrompt(second));
  150. await sendV4Prompt(goalPrompt(third));
  151. await waitForQueueCount(3);
  152. const initial = await getQueueItems();
  153. const firstId = requireQueueId(initial[0]?.id, "first");
  154. const secondId = requireQueueId(initial[1]?.id, "second");
  155. const thirdId = requireQueueId(initial[2]?.id, "third");
  156. await dragV4QueueItemOnto(thirdId, firstId);
  157. await waitForQueueOrderContaining([third, first, second]);
  158. await clickV4Stop();
  159. await waitForHeldQueue(3);
  160. // 最后一条 enqueue 的 command ACK 可以先于 composer pending finally 投影到 queue;
  161. // 这时首个 edit click 会按产品 guard 被忽略。等 busy 收口后重试真实按钮动作,
  162. // 并以 composer 进入该项的编辑态作为成功边界(队列项原位冻结,不再删除)。
  163. await browser.waitUntil(
  164. async () => {
  165. const state = await getV4ComposerEditState();
  166. if (state?.kind === "queueItem" && state.targetId === secondId) {
  167. return true;
  168. }
  169. await clickQueueEdit(secondId);
  170. return false;
  171. },
  172. {
  173. timeout: 30000,
  174. timeoutMsg: "MR-GOAL-07 goal queue item 没有进入 composer 编辑态",
  175. },
  176. );
  177. await browser.waitUntil(async () => (await getV4ComposerText())?.includes(second) === true, {
  178. timeout: 30000,
  179. timeoutMsg: "MR-GOAL-07 编辑态没有载入 goal 原文",
  180. });
  181. // MR-GOAL-07 只覆盖 held queue CRUD,不与 active goal streaming 的 revision
  182. // CAS 做交叉。冻结项之外的项仍可删除。
  183. await waitForHeldQueue(3);
  184. await waitForQueueOrderContaining([third, first, second]);
  185. await clickQueueRemove(firstId);
  186. const queueAfterDelete = await waitForQueueCount(2);
  187. expect(queueAfterDelete.map((item) => item.id)).toEqual([thirdId, secondId]);
  188. expect(queueAfterDelete.map((item) => item.kind)).toEqual(["goal", "goal"]);
  189. // 编辑态下发送走 editQueueItem 原地替换,不启动新 goal,也不弹暂停队列确认。
  190. await sendV4Prompt(goalPrompt(edited));
  191. await waitForV4ComposerEditClosed("MR-GOAL-07 goal 编辑提交后没有退出编辑态", 30000);
  192. const queueAfterEditedGoal = await waitForQueueOrderContaining([third, edited]);
  193. expect(queueAfterEditedGoal.map((item) => item.id)).toEqual([thirdId, secondId]);
  194. expect(queueAfterEditedGoal.map((item) => item.kind)).toEqual(["goal", "goal"]);
  195. await expectNoUpstreamRequestForTextWithin(edited, 800);
  196. });
  197. it("MR-GOAL-08 manual compacting 中发送 /goal 只入队", async function () {
  198. this.timeout(CASE_TIMEOUT_MS);
  199. await freshTask();
  200. await buildManualCompactHistory(8);
  201. const goal = goalSlowMarker(8);
  202. await sendV4Prompt("/compact");
  203. await waitForV4CompactMarker({ origin: "manual", status: "running" }, 30000);
  204. await sendV4Prompt(goalPrompt(goal));
  205. const queue = await waitForQueueCount(1);
  206. expect(queue[0]?.kind).toBe("goal");
  207. expect(queue[0]?.content).toContain(goal);
  208. });
  209. it("MR-GOAL-09 auto compact 前置于 queued goal", async function () {
  210. this.timeout(CASE_TIMEOUT_MS);
  211. await freshTask();
  212. const running = highUsageSlowMarker(9);
  213. const goal = goalFastMarker(9);
  214. const existingRows = await getAutoCompactRowIds();
  215. await startSlowPrompt(running);
  216. await sendV4Prompt(goalPrompt(goal));
  217. await waitForQueueContaining(goal);
  218. await waitForNewAutoCompactSuccess(existingRows);
  219. await waitForUpstreamRequestContaining(goal, 90000);
  220. await waitForV4ConversationState(
  221. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  222. "MR-GOAL-09 auto compact 后 queued goal 没有消费",
  223. 90000,
  224. );
  225. });
  226. it("MR-GOAL-10 切换 session 时 goal 跟随当前 session", async function () {
  227. this.timeout(CASE_TIMEOUT_MS);
  228. await freshTask();
  229. const goal = goalSlowMarker(10, "A");
  230. await sendGoal(goal);
  231. const sessionA = await waitForGoalObjective(goal);
  232. await clickV4Stop();
  233. await waitForHeldQueue(0);
  234. await freshTask();
  235. const sessionB = await sendFastPrompt(fastMarker(10, "B"));
  236. requireTaskId(sessionA.taskId, "session A");
  237. requireTaskId(sessionB.taskId, "session B");
  238. await selectV4TaskById(sessionA.taskId as string);
  239. const restoredA = await waitForV4ConversationState(
  240. (snapshot) =>
  241. snapshot.taskId === sessionA.taskId && snapshot.targetObjective?.includes(goal) === true,
  242. "MR-GOAL-10 没有恢复 session A 的 paused goal",
  243. 30000,
  244. );
  245. expect(restoredA.targetStatus).toBe("paused");
  246. await selectV4TaskById(sessionB.taskId as string);
  247. const restoredB = await waitForV4ConversationState(
  248. (snapshot) => snapshot.taskId === sessionB.taskId,
  249. "MR-GOAL-10 没有切回 session B",
  250. 30000,
  251. );
  252. expect(restoredB.targetObjective).toBeNull();
  253. });
  254. it("MR-GOAL-11 queued goal send-now 后更新目标", async function () {
  255. this.timeout(CASE_TIMEOUT_MS);
  256. await freshTask();
  257. const oldGoal = goalFastMarker(11, "OLD");
  258. const running = slowMarker(11);
  259. const newGoal = goalSlowMarker(11, "NEW");
  260. await sendGoal(oldGoal);
  261. await waitForGoalComplete(oldGoal);
  262. await startSlowPrompt(running);
  263. await sendV4Prompt(goalPrompt(newGoal));
  264. await waitForQueueContaining(newGoal);
  265. await clickFirstQueueSendNow();
  266. // 修复原因:Send Now 会先投影新 goal,再异步删除 queue item。只等待
  267. // objective 会读到 target 已更新但 queueCount 仍为 1 的短暂混合状态。
  268. const snapshot = await waitForActiveGoalWithEmptyQueue(newGoal);
  269. expect(snapshot.queueCount).toBe(0);
  270. expect(snapshot.targetObjective).not.toContain(oldGoal);
  271. });
  272. it("MR-GOAL-12 goal continuation stop 后 queue 保留且不自动消费", async function () {
  273. this.timeout(CASE_TIMEOUT_MS);
  274. await freshTask();
  275. const goal = goalSlowMarker(12);
  276. const queued = fastMarker(12);
  277. await sendGoal(goal);
  278. await waitForGoalObjective(goal);
  279. await sendV4Prompt(simplePrompt(queued));
  280. await waitForQueueContaining(queued);
  281. const requestsBefore = await countUpstreamRequestsContaining(queued);
  282. await clickV4Stop();
  283. const snapshot = await waitForHeldQueue(1);
  284. expect(snapshot.targetStatus).toBe("paused");
  285. expect(await countUpstreamRequestsContaining(queued)).toBe(requestsBefore);
  286. });
  287. it("MR-GOAL-13 verifier 是 completion-blocking work", async function () {
  288. this.timeout(CASE_TIMEOUT_MS);
  289. await freshTask();
  290. const goal = goalVerifierSlowMarker(13);
  291. const queued = fastMarker(13);
  292. await sendGoal(goal);
  293. await waitForVerifierRequest(goal);
  294. await sendV4Prompt(simplePrompt(queued));
  295. const queue = await waitForQueueCount(1);
  296. expect(queue[0]?.kind).toBe("text");
  297. await expectNoUpstreamRequestForTextWithin(queued, 600);
  298. expect((await getV4ConversationState()).state).toBe("streaming");
  299. });
  300. it("MR-GOAL-14 verifier 中 text/goal 保持 FIFO,compact 不抢占", async function () {
  301. this.timeout(CASE_TIMEOUT_MS);
  302. await freshTask();
  303. const goal = goalVerifierSlowMarker(14, "ACTIVE");
  304. const textOne = fastMarker(14, "TEXT_ONE");
  305. const queuedGoal = goalFastMarker(14, "QUEUED");
  306. const textTwo = fastMarker(14, "TEXT_TWO");
  307. await sendGoal(goal);
  308. await waitForVerifierRequest(goal);
  309. await sendQueuedPromptAndWait(simplePrompt(textOne), textOne);
  310. await sendQueuedPromptAndWait(goalPrompt(queuedGoal), queuedGoal);
  311. const compactBefore = await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL);
  312. await sendQueuedPromptAndWait("/compact", "/compact");
  313. await sendQueuedPromptAndWait(simplePrompt(textTwo), textTwo);
  314. const queue = await waitForQueueOrderContaining([textOne, queuedGoal, "/compact", textTwo]);
  315. expect(queue.map((item) => item.kind)).toEqual(["text", "goal", "compact", "text"]);
  316. expect(await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL)).toBe(compactBefore);
  317. await assertVisibleV4UserMessagesNotContaining("/compact");
  318. });
  319. it("MR-GOAL-15 立即消费 typed goal 保留 goal intent", async function () {
  320. this.timeout(CASE_TIMEOUT_MS);
  321. await freshTask();
  322. const running = slowMarker(15);
  323. const goal = goalSlowMarker(15);
  324. await startSlowPrompt(running);
  325. await sendV4Prompt(goalPrompt(goal));
  326. await waitForQueueContaining(goal);
  327. await clickFirstQueueSendNow();
  328. const snapshot = await waitForActiveGoalWithEmptyQueue(goal);
  329. expect(snapshot.queueCount).toBe(0);
  330. expect(snapshot.targetStatus).toBe("active");
  331. });
  332. it("MR-GOAL-16 manual compact success 后消费 queued goal", async function () {
  333. this.timeout(CASE_TIMEOUT_MS);
  334. await freshTask();
  335. await buildManualCompactHistory(16);
  336. const goal = goalFastMarker(16);
  337. await sendV4Prompt("/compact");
  338. await waitForV4CompactMarker({ origin: "manual", status: "running" }, 30000);
  339. await sendV4Prompt(goalPrompt(goal));
  340. await waitForQueueContaining(goal);
  341. await waitForV4CompactMarker({ origin: "manual", status: "success" }, 90000);
  342. await waitForUpstreamRequestContaining(goal, 90000);
  343. await waitForV4ConversationState(
  344. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  345. "MR-GOAL-16 compact success 后 queued goal 没有消费",
  346. 90000,
  347. );
  348. });
  349. it("MR-GOAL-17 auto compact success 后消费 queued goal", async function () {
  350. this.timeout(CASE_TIMEOUT_MS);
  351. await freshTask();
  352. const running = highUsageSlowMarker(17);
  353. const goal = goalFastMarker(17);
  354. const existingRows = await getAutoCompactRowIds();
  355. await startSlowPrompt(running);
  356. await sendV4Prompt(goalPrompt(goal));
  357. await waitForQueueContaining(goal);
  358. await waitForNewAutoCompactSuccess(existingRows);
  359. await waitForUpstreamRequestContaining(goal, 90000);
  360. await waitForV4ConversationState(
  361. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  362. "MR-GOAL-17 auto compact success 后 queued goal 没有消费",
  363. 90000,
  364. );
  365. });
  366. it("MR-GOAL-18 goal complete 后 edit/fork/compact 控件恢复", async function () {
  367. this.timeout(CASE_TIMEOUT_MS);
  368. await freshTask();
  369. const seed = compactHistoryMarker(18, "SEED");
  370. const goal = goalFastMarker(18);
  371. await sendFastPrompt(seed);
  372. await sendGoal(goal);
  373. await waitForGoalComplete(goal);
  374. await expandAssistantHistoriesWithContent();
  375. // controlOnly 的 /goal 行不支持直接 edit;完成后恢复的是既有普通 query 的
  376. // edit 与 assistant fork 控件。V4 行以 rowId 而非 legacy messageId 标识,
  377. // 必须读取 V4 action 合同,避免旧 message helper 把已存在按钮误报为 null。
  378. await waitForV4Edit();
  379. await waitForV4Fork();
  380. await sendV4Prompt("/compact");
  381. await waitForV4CompactMarker({ origin: "manual", status: "success" }, 90000);
  382. });
  383. it("MR-GOAL-19 edit latest 普通 query 保持既有 goal scope", async function () {
  384. this.timeout(CASE_TIMEOUT_MS);
  385. await freshTask();
  386. const goal = goalFastMarker(19);
  387. const beforeEdit = fastMarker(19, "BEFORE_EDIT");
  388. const afterEdit = fastMarker(19, "AFTER_EDIT");
  389. await sendGoal(goal);
  390. await waitForGoalComplete(goal);
  391. await sendFastPrompt(beforeEdit);
  392. await editFirstV4UserQuery(simplePrompt(afterEdit));
  393. await waitForUpstreamRequestContaining(afterEdit, 45000);
  394. const existingGoal = await waitForV4ConversationState(
  395. (snapshot) => snapshot.state === "idle" && snapshot.targetObjective?.includes(goal) === true,
  396. "MR-GOAL-19 edit 普通 query 后 existing goal scope 丢失",
  397. 90000,
  398. );
  399. expect(existingGoal.targetObjective).toContain(goal);
  400. await freshTask();
  401. const noGoalBefore = fastMarker(19, "NO_GOAL_BEFORE");
  402. const noGoalAfter = fastMarker(19, "NO_GOAL_AFTER");
  403. await sendFastPrompt(noGoalBefore);
  404. await editFirstV4UserQuery(simplePrompt(noGoalAfter));
  405. await waitForUpstreamRequestContaining(noGoalAfter, 45000);
  406. expect(
  407. (
  408. await waitForV4ConversationState(
  409. (snapshot) => snapshot.state === "idle",
  410. "MR-GOAL-19 无 goal edit 后没有回到 idle",
  411. 90000,
  412. )
  413. ).targetObjective,
  414. ).toBeNull();
  415. });
  416. it("MR-GOAL-20 历史 session goal/queue 状态不串", async function () {
  417. this.timeout(CASE_TIMEOUT_MS);
  418. await freshTask();
  419. const goalA = goalSlowMarker(20, "A");
  420. const queueA = goalFastMarker(20, "A_QUEUE");
  421. await sendGoal(goalA);
  422. const sessionA = await waitForGoalObjective(goalA);
  423. await sendV4Prompt(goalPrompt(queueA));
  424. await waitForQueueContaining(queueA);
  425. requireTaskId(sessionA.taskId, "session A");
  426. await freshTask();
  427. const sessionB = await sendFastPrompt(fastMarker(20, "B"));
  428. requireTaskId(sessionB.taskId, "session B");
  429. expect(sessionB.targetObjective).toBeNull();
  430. expect(sessionB.queueCount).toBe(0);
  431. await selectV4TaskById(sessionA.taskId as string);
  432. const restoredA = await waitForV4ConversationState(
  433. (snapshot) =>
  434. snapshot.taskId === sessionA.taskId &&
  435. snapshot.targetObjective?.includes(goalA) === true &&
  436. snapshot.queueCount === 1,
  437. "MR-GOAL-20 session A goal/queue 没有恢复",
  438. 30000,
  439. );
  440. expect(restoredA.targetObjective).toContain(goalA);
  441. });
  442. it("MR-GOAL-21 普通 queue send-now 停止 active goal 输出", async function () {
  443. this.timeout(CASE_TIMEOUT_MS);
  444. await freshTask();
  445. const goal = goalSlowMarker(21);
  446. const queued = fastMarker(21);
  447. const assistantRowIdsBeforeGoal = new Set(
  448. (await getV4Messages("assistant")).flatMap((message) =>
  449. message.id === null ? [] : [message.id],
  450. ),
  451. );
  452. await sendGoal(goal);
  453. await waitForGoalObjective(goal);
  454. await waitForNewGoalContinuationOutput(goal, assistantRowIdsBeforeGoal);
  455. await sendV4Prompt(simplePrompt(queued));
  456. await waitForQueueContaining(queued);
  457. await clickFirstQueueSendNow();
  458. await waitForUpstreamRequestContaining(queued, 90000);
  459. const snapshot = await waitForV4ConversationState(
  460. (candidate) => candidate.queueCount === 0,
  461. "MR-GOAL-21 send-now 后被点文本没有离开 queue",
  462. 30000,
  463. );
  464. expect(snapshot.targetStatus).toBe("paused");
  465. await waitForV4UserMessageContaining(queued);
  466. });
  467. it("MR-GOAL-22 普通 queue send-now 停止 goal verifier", async function () {
  468. this.timeout(CASE_TIMEOUT_MS);
  469. await freshTask();
  470. const goal = goalVerifierSlowMarker(22);
  471. const queued = fastMarker(22);
  472. await sendGoal(goal);
  473. await waitForVerifierRequest(goal);
  474. await sendV4Prompt(simplePrompt(queued));
  475. await waitForQueueContaining(queued);
  476. const verifierBefore = await countUpstreamRequestsContaining(
  477. "Verify whether the active session goal",
  478. );
  479. await clickFirstQueueSendNow();
  480. await waitForUpstreamRequestContaining(queued, 90000);
  481. const snapshot = await waitForV4ConversationState(
  482. (candidate) => candidate.queueCount === 0,
  483. "MR-GOAL-22 send-now 后被点文本没有离开 queue",
  484. 30000,
  485. );
  486. await browser.pause(500);
  487. expect(snapshot.targetStatus).toBe("paused");
  488. expect(await countUpstreamRequestsContaining("Verify whether the active session goal")).toBe(
  489. verifierBefore,
  490. );
  491. });
  492. });
  493. async function freshTask() {
  494. await startNewV4Draft();
  495. await waitForV4ConversationState(
  496. (snapshot) => snapshot.taskId === null && snapshot.queueCount === 0,
  497. "新建任务后没有进入空草稿",
  498. 15000,
  499. );
  500. // 旧 MR-GOAL 用例的入口是 completed session,而不是空 draft。空 draft 直接发送
  501. // /goal 只会预建 session,无法覆盖 completed task 上的 target/queue 语义。
  502. return sendFastPrompt(fastMarker(0, "SETUP"));
  503. }
  504. async function sendFastPrompt(marker: string) {
  505. await sendV4Prompt(simplePrompt(marker));
  506. await waitForV4ComposerText("", `${marker} 发送后输入框没有清空`);
  507. await waitForUpstreamRequestContaining(marker, 30000);
  508. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  509. return waitForV4ConversationState(
  510. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  511. `${marker} 没有完成`,
  512. 60000,
  513. );
  514. }
  515. async function startSlowPrompt(marker: string) {
  516. await sendV4Prompt(simplePrompt(marker));
  517. await waitForV4ComposerText("", `${marker} 发送后输入框没有清空`);
  518. // Bug 根因:只按 bodyIncludes 等待会先命中携带同一 user marker 的 title 请求,
  519. // 随后把尚未进入主 turn 的 completed 快照误报为运行态失败。这里锁定最后一条
  520. // user message,并排除 title/compact,确保 admission 对应 controlled-stream fixture。
  521. await waitForV4UserMessageContaining(marker);
  522. await waitForUpstreamRequest(
  523. {
  524. excludes: ["Generate a concise title", COMPACT_REQUEST_SENTINEL],
  525. lastUserMessageIncludes: [marker],
  526. },
  527. `${marker} 主 turn 请求没有开始`,
  528. 30000,
  529. );
  530. // 运行时证据取 provider 主请求的 pending controlled-stream;completed session
  531. // 恢复下一轮时,renderer 的 task bridge 会在 V4 delta 前短暂保留上一轮 completed,
  532. // 不能让这条 legacy 诊断投影反过来否定已经开始的真实主请求。各 case 随后的
  533. // queue/Stop 断言会继续验证 busy admission,而不是在这里放宽产品结果。
  534. }
  535. async function sendGoal(marker: string) {
  536. await sendV4Prompt(goalPrompt(marker));
  537. await waitForV4ComposerText("", `${marker} goal 发送后输入框没有清空`);
  538. await waitForV4UserMessageContaining(marker);
  539. }
  540. async function sendQueuedPromptAndWait(prompt: string, marker: string) {
  541. await sendV4Prompt(prompt);
  542. // 修复原因:Windows 下连续点击发送后,上一条 prompt 的异步 composer 清空可能覆盖
  543. // 下一条刚写入的内容;每一步必须同时确认输入已清空且本条 marker 已被队列接受。
  544. await waitForV4ComposerText("", `${marker} 入队后输入框没有清空`);
  545. return waitForQueueContaining(marker);
  546. }
  547. function waitForGoalObjective(marker: string) {
  548. return waitForV4ConversationState(
  549. (snapshot) =>
  550. snapshot.state === "streaming" && snapshot.targetObjective?.includes(marker) === true,
  551. `${marker} 没有进入 active goal work`,
  552. 45000,
  553. );
  554. }
  555. async function waitForNewGoalContinuationOutput(
  556. marker: string,
  557. previousAssistantRowIds: ReadonlySet<string>,
  558. ) {
  559. await waitForUpstreamRequest(
  560. {
  561. excludes: [COMPACT_REQUEST_SENTINEL, "Verify whether the active session goal"],
  562. includes: [marker, "Continue working toward the active session goal"],
  563. },
  564. `${marker} goal continuation 请求没有开始`,
  565. 45000,
  566. );
  567. await browser.waitUntil(
  568. async () => {
  569. const messages = await getV4Messages("assistant");
  570. return messages.some(
  571. (message) =>
  572. message.id !== null &&
  573. !previousAssistantRowIds.has(message.id) &&
  574. message.text.includes("deep"),
  575. );
  576. },
  577. {
  578. timeout: 30000,
  579. // Bug 根因:只等 target=active 会早于 continuation 首个 delta;此时马上点击
  580. // send-now 可能与该 delta 的 revision 递增交叉,CAS 按规范返回 stale,case 却
  581. // 误报为 stop 失效。先锁定新 assistant row,才真正进入“active goal 输出中”。
  582. timeoutMsg: `${marker} goal continuation 首段输出没有进入新 assistant row`,
  583. },
  584. );
  585. }
  586. function waitForActiveGoalWithEmptyQueue(marker: string) {
  587. let stableSamples = 0;
  588. return waitForV4ConversationState(
  589. (snapshot) => {
  590. // 修复原因:Send Now 会先更新 store/goal projection,再异步卸载队列 DOM;
  591. // 只等 objective 会在 Windows 上读到 goal 已 active、旧队列节点仍存在的混合快照。
  592. const settled =
  593. snapshot.state === "streaming" &&
  594. snapshot.targetObjective?.includes(marker) === true &&
  595. snapshot.targetStatus === "active" &&
  596. snapshot.queueCount === 0;
  597. stableSamples = settled ? stableSamples + 1 : 0;
  598. return stableSamples >= 2;
  599. },
  600. `${marker} Send Now 后 active goal 与空队列没有稳定收敛`,
  601. 45000,
  602. );
  603. }
  604. function waitForGoalComplete(marker: string) {
  605. return waitForV4ConversationState(
  606. (snapshot) =>
  607. snapshot.state === "idle" &&
  608. snapshot.targetObjective?.includes(marker) === true &&
  609. (snapshot.targetStatus === "complete" ||
  610. snapshot.targetStatus === "completed" ||
  611. snapshot.targetStatus === "verified"),
  612. `${marker} 没有通过 verifier 自然完成`,
  613. 60000,
  614. );
  615. }
  616. async function waitForVerifierRequest(marker: string) {
  617. await waitForUpstreamRequest(
  618. {
  619. excludes: [COMPACT_REQUEST_SENTINEL],
  620. includes: [marker, "Verify whether the active session goal"],
  621. },
  622. `${marker} verifier 请求没有开始`,
  623. 60000,
  624. );
  625. return waitForV4ConversationState(
  626. (snapshot) =>
  627. snapshot.state === "streaming" && snapshot.targetObjective?.includes(marker) === true,
  628. `${marker} verifier 没有保持 completion-blocking 状态`,
  629. 30000,
  630. );
  631. }
  632. function waitForHeldQueue(expectedCount: number) {
  633. return waitForV4ConversationState(
  634. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === expectedCount,
  635. `Stop 后没有保留 ${expectedCount} 条 held queue`,
  636. 30000,
  637. );
  638. }
  639. async function buildManualCompactHistory(caseId: number) {
  640. await sendFastPrompt(compactHistoryMarker(caseId, "A"));
  641. await sendFastPrompt(compactHistoryMarker(caseId, "B"));
  642. }
  643. async function getAutoCompactRowIds() {
  644. await expandAssistantHistoriesWithContent();
  645. return new Set(
  646. (await getV4CompactMarkers())
  647. .filter((marker) => marker.origin === "auto" && marker.rowId !== null)
  648. .map((marker) => marker.rowId as number),
  649. );
  650. }
  651. async function waitForNewAutoCompactSuccess(
  652. existingRowIds: ReadonlySet<number>,
  653. ): Promise<V4CompactMarkerSnapshot> {
  654. let latestMarkers: V4CompactMarkerSnapshot[] = [];
  655. await browser.waitUntil(
  656. async () => {
  657. await expandAssistantHistoriesWithContent();
  658. latestMarkers = await getV4CompactMarkers();
  659. return latestMarkers.some(
  660. (marker) =>
  661. marker.origin === "auto" &&
  662. marker.status === "success" &&
  663. marker.rowId !== null &&
  664. !existingRowIds.has(marker.rowId),
  665. );
  666. },
  667. {
  668. timeout: 90000,
  669. timeoutMsg: `没有等到新的 auto/success marker: ${JSON.stringify(latestMarkers)}`,
  670. },
  671. );
  672. const marker = latestMarkers.find(
  673. (candidate) =>
  674. candidate.origin === "auto" &&
  675. candidate.status === "success" &&
  676. candidate.rowId !== null &&
  677. !existingRowIds.has(candidate.rowId),
  678. );
  679. if (!marker) {
  680. throw new Error("新的 auto/success marker 在等待后仍不存在");
  681. }
  682. return marker;
  683. }
  684. async function stopCurrentWork() {
  685. const snapshot = await getV4ConversationState().catch(() => null);
  686. if (snapshot?.state !== "streaming") {
  687. return;
  688. }
  689. await clickV4Stop().catch(() => undefined);
  690. await waitForV4ConversationState(
  691. (candidate) => candidate.state !== "streaming",
  692. "afterEach Stop 后没有结束当前 work",
  693. 30000,
  694. ).catch(() => undefined);
  695. }
  696. function requireQueueId(value: string | undefined, label: string) {
  697. if (!value) {
  698. throw new Error(`${label} queue item 缺少 id`);
  699. }
  700. return value;
  701. }
  702. function requireTaskId(value: string | null, label: string) {
  703. if (!value) {
  704. throw new Error(`${label} 缺少 taskId`);
  705. }
  706. return value;
  707. }
  708. function simplePrompt(marker: string) {
  709. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  710. }
  711. function goalPrompt(marker: string) {
  712. return `/goal ${goalObjective(marker)}`;
  713. }
  714. function goalObjective(marker: string) {
  715. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  716. }
  717. function fastMarker(caseId: number, suffix = "MAIN") {
  718. return `E2E_GR_FAST_${caseId}_${suffix}_${Date.now()}`;
  719. }
  720. function slowMarker(caseId: number, suffix = "MAIN") {
  721. return `E2E_GR_SLOW_${caseId}_${suffix}_${Date.now()}`;
  722. }
  723. function highUsageSlowMarker(caseId: number) {
  724. return `E2E_GR_HIGH_USAGE_SLOW_${caseId}_${Date.now()}`;
  725. }
  726. function goalFastMarker(caseId: number, suffix = "MAIN") {
  727. return `E2E_GR_GOAL_FAST_${caseId}_${suffix}_${Date.now()}`;
  728. }
  729. function goalSlowMarker(caseId: number, suffix = "MAIN") {
  730. return `E2E_GR_GOAL_SLOW_${caseId}_${suffix}_${Date.now()}`;
  731. }
  732. function goalVerifierSlowMarker(caseId: number, suffix = "MAIN") {
  733. return `E2E_GR_GOAL_VERIFY_SLOW_${caseId}_${suffix}_${Date.now()}`;
  734. }
  735. function compactHistoryMarker(caseId: number, suffix: string) {
  736. return `E2E_GR_COMPACT_HISTORY_${caseId}_${suffix}_${Date.now()}`;
  737. }