conversation-session-running-send-now.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { TID_CHAT_QUEUE_SEND_NOW_BUTTON, TID_V4_QUEUE_ITEM_SEND_NOW } from "@zcode/shared";
  2. import { clearAppData } from "../helpers/desktop-app.js";
  3. import {
  4. INCOMING_MESSAGE_MODES,
  5. prepareIncomingMessageCapability,
  6. } from "../helpers/incoming-message-capability.js";
  7. import { assertIncomingMessage, wireText } from "../helpers/incoming-message-evidence.js";
  8. import {
  9. UPSTREAM_MODEL,
  10. UPSTREAM_THOUGHT_LEVEL,
  11. selectUpstreamModelById,
  12. selectUpstreamThoughtLevelValue,
  13. } from "../helpers/upstream-provider.js";
  14. import {
  15. countUpstreamRequestsContaining,
  16. getUpstreamRequestEvidence,
  17. expectNoUpstreamRequestForTextWithin,
  18. waitForUpstreamRequestContaining,
  19. } from "../helpers/conversation-session-network.js";
  20. import {
  21. clickFirstQueueSendNow,
  22. clickQueueSendNow,
  23. getQueueItems,
  24. waitForQueueCount,
  25. waitForQueueOrderContaining,
  26. } from "../helpers/conversation-session-queue.js";
  27. import {
  28. E2E_REPLY_TOKEN,
  29. clickV4Stop,
  30. getV4ConversationState,
  31. getV4GoalProjection,
  32. getV4Messages,
  33. prepareV4ConversationE2E,
  34. sendV4Prompt,
  35. startNewV4Draft,
  36. waitForV4ComposerText,
  37. waitForV4ConversationState,
  38. waitForV4UserMessageContaining,
  39. waitForV4AssistantMessageContaining,
  40. } from "../helpers/v4-conversation.js";
  41. const RUNNING_SEND_NOW_TIMEOUT_MS = 180000;
  42. describe("会话区 Running Queue 立即引导 E2E", () => {
  43. before(async function () {
  44. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  45. await prepareV4ConversationE2E();
  46. });
  47. beforeEach(async function () {
  48. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  49. await startNewV4Draft();
  50. await selectUpstreamModelById(UPSTREAM_MODEL);
  51. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  52. });
  53. afterEach(async () => {
  54. await stopIfBusy();
  55. });
  56. after(async () => {
  57. await browser.electron.restoreAllMocks();
  58. await clearAppData();
  59. });
  60. for (const { label, mcs } of INCOMING_MESSAGE_MODES) {
  61. it(`M07a ${label}:running 中点击队首立即引导,消费原文并以 user reminder 降级`, async function () {
  62. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  63. await prepareIncomingMessageCapability(mcs, "queue");
  64. const runId = Date.now();
  65. await startSlowRunningTurn(`E2E_RUNNING_SEND_NOW_HEAD_BASE_${runId}`);
  66. // 必须等当前轮产生 text-only assistant;更早轮次的文字不能作为这次抢占的锚点。
  67. await waitForV4AssistantMessageContaining("send-now-head-partial");
  68. const queuedMarker = `E2E_RUNNING_SEND_NOW_HEAD_${runId}`;
  69. await enqueueTextPrompt(queuedMarker);
  70. const queuedBeforeSendNow = await waitForQueueOrderContaining(
  71. [queuedMarker],
  72. "running send-now 队首点击前没有形成 text queue",
  73. );
  74. expect(queuedBeforeSendNow[0]?.kind).toBe("text");
  75. const requestsBefore = await countUpstreamRequestsContaining(queuedMarker);
  76. const sentNowItem = await clickFirstQueueSendNow();
  77. expect(sentNowItem.id).toBe(queuedBeforeSendNow[0]?.id);
  78. expect(sentNowItem.content).toContain(queuedMarker);
  79. await waitForUpstreamRequestContaining(queuedMarker, 60000);
  80. await waitForV4UserMessageContaining(queuedMarker);
  81. await waitForV4ConversationState(
  82. (snapshot) => snapshot.queueCount === 0,
  83. "running send-now 队首消息没有从 queue 消费",
  84. 45000,
  85. );
  86. expect(await countUpstreamRequestsContaining(queuedMarker)).toBeGreaterThan(requestsBefore);
  87. const evidence = await getUpstreamRequestEvidence({
  88. includes: [queuedMarker],
  89. excludes: ["Generate a concise title"],
  90. });
  91. const wire = await assertIncomingMessage(evidence.at(-1)!.requestJson, {
  92. presentation: "user_steer",
  93. marker: queuedMarker,
  94. body: buildQueuedPrompt(queuedMarker),
  95. // 保留当前轮的 text-only assistant 后,MCS 开启也必须降级。
  96. role: "user",
  97. evidenceLabel: `send-now-head-${label}`,
  98. });
  99. expect(wire.messages[wire.index - 1]?.role).toBe("assistant");
  100. expect(wireText(wire.messages[wire.index - 1]?.content)).toContain("send-now-head-partial");
  101. const userRows = (await getV4Messages("user")).filter((message) =>
  102. message.text.includes(queuedMarker),
  103. );
  104. expect(userRows).toHaveLength(1);
  105. expect(
  106. await $(`[data-row-id="${userRows[0]!.id}"] [data-v4-user-input-bubble]`).getText(),
  107. ).toBe(buildQueuedPrompt(queuedMarker));
  108. });
  109. }
  110. it("running 中点击非队首 queued text 立即引导,应提升并只消费被点消息", async function () {
  111. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  112. const runId = Date.now();
  113. await startSlowRunningTurn(`E2E_RUNNING_SEND_NOW_PROMOTE_BASE_${runId}`);
  114. const firstMarker = `E2E_RUNNING_SEND_NOW_PROMOTE_FIRST_${runId}`;
  115. const secondMarker = `E2E_RUNNING_SEND_NOW_PROMOTE_SECOND_${runId}`;
  116. await enqueueTextPrompt(firstMarker);
  117. await enqueueTextPrompt(secondMarker, { slow: true });
  118. const queuedBeforeSendNow = await waitForQueueOrderContaining(
  119. [firstMarker, secondMarker],
  120. "running send-now 非队首点击前没有按 FIFO 呈现两条 text",
  121. );
  122. expect(queuedBeforeSendNow.map((item) => item.kind)).toEqual([
  123. "text",
  124. "text",
  125. ]);
  126. const remainingQueueItem = queuedBeforeSendNow[0]!;
  127. const selectedQueueItem = queuedBeforeSendNow[1]!;
  128. const firstRequestsBefore =
  129. await countUpstreamRequestsContaining(firstMarker);
  130. const secondRequestsBefore =
  131. await countUpstreamRequestsContaining(secondMarker);
  132. const sentNowItem = await clickQueueSendNow(selectedQueueItem.id);
  133. expect(sentNowItem.id).toBe(selectedQueueItem.id);
  134. expect(sentNowItem.content).toContain(secondMarker);
  135. await waitForUpstreamRequestContaining(secondMarker, 60000);
  136. await waitForV4UserMessageContaining(secondMarker);
  137. await waitForV4ConversationState(
  138. (snapshot) => snapshot.queueCount === 1,
  139. "running send-now 非队首被点项消费后剩余 queue 数量不对",
  140. 45000,
  141. );
  142. const queuedAfterSendNow = await waitForQueueCount(
  143. 1,
  144. "running send-now 非队首被点项消费后 queue 没有只剩未选项",
  145. );
  146. expect(queuedAfterSendNow[0]?.id).toBe(remainingQueueItem.id);
  147. expect(queuedAfterSendNow[0]?.content).toContain(firstMarker);
  148. expect(queuedAfterSendNow[0]?.content).not.toContain(secondMarker);
  149. const selectedUserMessage = (await getV4Messages("user")).find((message) =>
  150. message.text.includes(secondMarker),
  151. );
  152. expect(selectedUserMessage).toBeDefined();
  153. // V4 timeline message id 与 runtime command queue id 属于不同身份域;
  154. // 被点项是否保留以唯一 marker、用户行与剩余 queue item 共同证明。
  155. await expectNoUpstreamRequestForTextWithin(firstMarker, 900);
  156. expect(await countUpstreamRequestsContaining(firstMarker)).toBe(
  157. firstRequestsBefore,
  158. );
  159. expect(await countUpstreamRequestsContaining(secondMarker)).toBeGreaterThan(
  160. secondRequestsBefore,
  161. );
  162. });
  163. it("running 中点击 queued /goal 立即引导,应按 goal 语义消费并更新目标", async function () {
  164. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  165. const runId = Date.now();
  166. await startSlowRunningTurn(`E2E_RUNNING_SEND_NOW_GOAL_BASE_${runId}`);
  167. const goalMarker = `E2E_RUNNING_SEND_NOW_GOAL_${runId}`;
  168. await sendV4Prompt(goalPrompt(goalMarker));
  169. await waitForV4ComposerText("", "running send-now goal 发送后输入框没有清空");
  170. const queue = await waitForQueueOrderContaining(
  171. [goalMarker],
  172. "running send-now goal 点击前没有形成 goal queue",
  173. );
  174. expect(queue[0]?.kind).toBe("goal");
  175. const sentNowItem = await clickFirstQueueSendNow();
  176. expect(sentNowItem.kind).toBe("goal");
  177. expect(sentNowItem.content).toContain(goalMarker);
  178. await browser.waitUntil(
  179. async () => {
  180. const [snapshot, goal] = await Promise.all([
  181. getV4ConversationState(),
  182. getV4GoalProjection(),
  183. ]);
  184. return (
  185. snapshot.queueCount === 0 &&
  186. goal?.objective?.includes(goalMarker) === true &&
  187. ["active", "verifying"].includes(goal.status ?? "")
  188. );
  189. },
  190. {
  191. timeout: 45000,
  192. timeoutMsg: "running send-now goal 没有按 goal intent 消费并设置目标",
  193. },
  194. );
  195. });
  196. it("running 中连续快速点两次立即引导只消费一次被点项,并保留未点 queue item", async function () {
  197. this.timeout(RUNNING_SEND_NOW_TIMEOUT_MS);
  198. const runId = Date.now();
  199. await startSlowRunningTurn(`E2E_RUNNING_SEND_NOW_DOUBLE_BASE_${runId}`);
  200. const firstMarker = `E2E_RUNNING_SEND_NOW_DOUBLE_FIRST_${runId}`;
  201. const secondMarker = `E2E_RUNNING_SEND_NOW_DOUBLE_SECOND_${runId}`;
  202. await enqueueTextPrompt(firstMarker);
  203. await enqueueTextPrompt(secondMarker, { slow: true });
  204. const queuedBeforeSendNow = await waitForQueueOrderContaining(
  205. [firstMarker, secondMarker],
  206. "running send-now double click 前没有形成两条 text queue",
  207. );
  208. const remainingQueueItem = queuedBeforeSendNow[0]!;
  209. const selectedQueueItem = queuedBeforeSendNow[1]!;
  210. const firstRequestsBefore =
  211. await countUpstreamRequestsContaining(firstMarker);
  212. const secondRequestsBefore =
  213. await countUpstreamRequestsContaining(secondMarker);
  214. await doubleClickQueueSendNow(selectedQueueItem.id);
  215. await waitForUpstreamRequestContaining(secondMarker, 60000);
  216. await waitForV4UserMessageContaining(secondMarker);
  217. await waitForV4ConversationState(
  218. (snapshot) => snapshot.queueCount === 1,
  219. "running send-now double click 后没有保持只消费被点项",
  220. 45000,
  221. );
  222. // 修复原因:连续 click 会生成两个 commandId,但 CLI 对 queue item 的 reservation 是
  223. // 执行权边界;第二条命令必须被拒绝,不能重复 stop/drain 或顺带消费未点项。
  224. await expectNoUpstreamRequestForTextWithin(firstMarker, 900);
  225. const queuedAfterDoubleClick = await waitForQueueCount(
  226. 1,
  227. "running send-now double click 后未点 queue item 没有保持在队列",
  228. );
  229. expect(queuedAfterDoubleClick[0]?.id).toBe(remainingQueueItem.id);
  230. expect(queuedAfterDoubleClick[0]?.content).toContain(firstMarker);
  231. expect(await countUpstreamRequestsContaining(firstMarker)).toBe(firstRequestsBefore);
  232. expect(await countUpstreamRequestsContaining(secondMarker)).toBe(
  233. secondRequestsBefore + 1,
  234. );
  235. const selectedUserMessages = (await getV4Messages("user")).filter((message) =>
  236. message.text.includes(secondMarker),
  237. );
  238. expect(selectedUserMessages).toHaveLength(1);
  239. const remainingUserMessages = (await getV4Messages("user")).filter((message) =>
  240. message.text.includes(firstMarker),
  241. );
  242. expect(remainingUserMessages).toHaveLength(0);
  243. });
  244. });
  245. function buildRunningPrompt(marker: string) {
  246. return `E2E_HOLD_STREAM ${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  247. }
  248. function buildQueuedPrompt(marker: string, options: { slow?: boolean } = {}) {
  249. const prefix = options.slow ? "E2E_SLOW_STREAM " : "";
  250. return `${prefix}${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  251. }
  252. function goalPrompt(marker: string) {
  253. return `/goal ${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  254. }
  255. async function startSlowRunningTurn(marker: string) {
  256. await sendV4Prompt(buildRunningPrompt(marker));
  257. await waitForV4ComposerText("", "running send-now 基础消息发送后输入框没有清空");
  258. await waitForV4UserMessageContaining(marker);
  259. await waitForV4ConversationState(
  260. (snapshot) => snapshot.state === "streaming",
  261. "running send-now 基础消息没有进入 streaming",
  262. 30000,
  263. );
  264. }
  265. async function enqueueTextPrompt(
  266. marker: string,
  267. options: { slow?: boolean } = {},
  268. ) {
  269. await sendV4Prompt(buildQueuedPrompt(marker, options));
  270. await waitForV4ComposerText(
  271. "",
  272. `running send-now queued text 没有清空: ${marker}`,
  273. );
  274. }
  275. async function doubleClickQueueSendNow(promptId: string) {
  276. const clicked = await browser.execute(
  277. (buttonTestIds) => {
  278. const button = buttonTestIds
  279. .map((buttonTestId) =>
  280. document.querySelector<HTMLButtonElement>(
  281. `[data-testid="${buttonTestId}"]`,
  282. ),
  283. )
  284. .find((candidate) => candidate !== null);
  285. if (!button || button.disabled) {
  286. return false;
  287. }
  288. // 复现原因:该 case 要覆盖用户连续快速点两次“立即”,两个 click 必须在同一轮
  289. // React 状态刷新前发出,才能覆盖重复 stop/drain 的幂等边界。
  290. button.click();
  291. button.click();
  292. return true;
  293. },
  294. [
  295. `${TID_V4_QUEUE_ITEM_SEND_NOW}-${promptId}`,
  296. `${TID_CHAT_QUEUE_SEND_NOW_BUTTON}-${promptId}`,
  297. ],
  298. );
  299. expect(clicked).toBe(true);
  300. }
  301. async function stopIfBusy() {
  302. const snapshot = await getV4ConversationState().catch(() => null);
  303. if (snapshot?.state !== "streaming") {
  304. return;
  305. }
  306. await clickV4Stop().catch(() => undefined);
  307. await waitForV4ConversationState(
  308. (candidate) => candidate.state !== "streaming",
  309. "running send-now 清理阶段没有退出 streaming",
  310. 30000,
  311. ).catch(() => undefined);
  312. await getQueueItems().catch(() => []);
  313. }