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

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