conversation-session-dynamic-workflow-gray-always-on-cross-session.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import { clearAppData, pinE2ELocale } from "#e2e/helpers/desktop-app";
  2. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  3. import {
  4. approveV4Permission,
  5. getV4PaneSnapshot,
  6. hasV4PermissionDialog,
  7. prepareV4ConversationE2E,
  8. selectV4TaskById,
  9. sendV4Prompt,
  10. startNewV4Draft,
  11. waitForV4AssistantMessageContaining,
  12. waitForV4Pane,
  13. waitForV4PermissionDialog,
  14. } from "#e2e/helpers/v4-conversation";
  15. import {
  16. getLatestUpstreamToolResultByToolCallId,
  17. getUpstreamRequestEvidence,
  18. } from "#e2e/helpers/conversation-session-network";
  19. // run 与别的会话(docs/dynamic-workflow/launch.md「Amending this session's runs」、`FillWorkflowHole` 的
  20. // 「Approval」、docs/dynamic-workflow/presentation.md「The sidebar run line」、
  21. // docs/dynamic-workflow/transcript-and-notifications.md):
  22. // run 属于发起它的会话;切到别的会话时它照常前进,通知只叫醒它自己的会话,
  23. // 而别的会话动它(补全、修订)要经过确认窗。
  24. //
  25. // 文件名带 `gray-always-on`:配置 mock 网关按文件名下发 alwaysOn,工作流工具才在工具集里。
  26. // case-local 回放只决定模型调用;会话切换、run 引擎、所属会话判定、确认窗与通知投递走产品链路。
  27. // 两个子代理的回答用受控流挂起,由用例在切到别的会话之后释放,不靠延时抢时序。
  28. const RUN_NAME = "E2E 跨会话调研";
  29. const HOLE_RUN_NAME = "E2E 跨会话留白";
  30. const COMPLETION_CARD = '[data-workflow-completion-card="true"]';
  31. const NOTIFICATION_ROW = '[data-testid^="chat-workflow-notification-row-"]';
  32. const digestOf = (runId: string) =>
  33. $(`[data-workflow-run-digest="true"][data-workflow-run-id="${runId}"]`);
  34. const runLineOf = (sessionId: string) =>
  35. $(`[data-testid="task-item-${sessionId}"] [data-workflow-run-line="true"]`);
  36. let sessionA = "";
  37. let sessionB = "";
  38. let sessionHoleOwner = "";
  39. let runId = "";
  40. let holeRunId = "";
  41. // 回放的 `{{latestWorkflowRunId}}` 只读当前请求里的留白通知形状。模型从 `ListWorkflowRuns` 或用户那里
  42. // 拿到别的会话的 run id 都一样:这里由用户消息带上真实 id,所属会话的判定仍由产品读 run 的记录。
  43. const carryRunId = (id: string) => `<workflow-hole><run-id>${id}</run-id></workflow-hole>`;
  44. async function readSessionId(): Promise<string> {
  45. const { sessionId } = await getV4PaneSnapshot();
  46. if (!sessionId) throw new Error("主 pane 没有打开会话");
  47. return sessionId;
  48. }
  49. async function startSessionAndWaitForReply(prompt: string, reply: string): Promise<string> {
  50. await startNewV4Draft();
  51. await sendV4Prompt(prompt);
  52. await waitForV4AssistantMessageContaining(reply, 60000);
  53. await waitForV4Pane((pane) => pane.sessionId !== "draft" && !pane.canStop, "新会话没有结束回复");
  54. return readSessionId();
  55. }
  56. async function readStationStatuses(id: string): Promise<string[]> {
  57. const stations = await digestOf(id).$$('[data-testid="workflow-timeline-station"]');
  58. const statuses: string[] = [];
  59. for (const station of stations) {
  60. statuses.push((await station.getAttribute("data-station-status")) ?? "");
  61. }
  62. return statuses;
  63. }
  64. async function readPillStatuses(id: string): Promise<Record<string, string>> {
  65. const statuses: Record<string, string> = {};
  66. for (const pill of await digestOf(id).$$('[data-testid="workflow-agent-pill"]')) {
  67. // 失败原因:a6fb6916a8 给完成的子代理 pill 加了 sr-only 的「已完成」文字,整颗 pill 的
  68. // 文本变成「名字+状态」。名字只读 `.wf-pill-name`,状态仍读 data-agent-status。
  69. const name = (await pill.$(".wf-pill-name").getText()).trim();
  70. statuses[name] = (await pill.getAttribute("data-agent-status")) ?? "";
  71. }
  72. return statuses;
  73. }
  74. async function waitForRunLineText(sessionId: string, text: string, timeoutMsg: string) {
  75. await browser.waitUntil(
  76. async () => {
  77. const line = runLineOf(sessionId);
  78. return (await line.isExisting()) && (await line.getText()).includes(text);
  79. },
  80. { timeout: 120000, timeoutMsg },
  81. );
  82. }
  83. const readPermissionWindowText = () =>
  84. browser.execute(() => {
  85. const listbox = Array.from(document.querySelectorAll<HTMLElement>('[role="listbox"]')).find(
  86. (element) =>
  87. ["Permission required", "需要权限"].includes(element.getAttribute("aria-label") ?? ""),
  88. );
  89. return listbox?.closest<HTMLElement>(".bg-popover")?.innerText ?? "";
  90. });
  91. async function waitForToolResult(toolCallId: string) {
  92. await browser.waitUntil(
  93. async () => Boolean(await getLatestUpstreamToolResultByToolCallId(toolCallId)),
  94. { timeout: 60000, timeoutMsg: `${toolCallId} 没有结果回到模型` },
  95. );
  96. return (await getLatestUpstreamToolResultByToolCallId(toolCallId))!;
  97. }
  98. describe("动态工作流 · run 与别的会话", () => {
  99. before(async () => {
  100. await prepareV4ConversationE2E();
  101. // 断言中文文案:固定界面语言,不跟随宿主系统语言(容器里默认英文)。
  102. await pinE2ELocale("zh-CN");
  103. });
  104. after(async () => {
  105. await browser.electron.restoreAllMocks();
  106. await clearAppData();
  107. });
  108. it("DWW-01 切到别的会话时 run 照常前进:A 的侧栏运行行跟上阶段,B 没有运行行;切回 A,run 卡已跟上进度", async function () {
  109. this.timeout(300000);
  110. await sendV4Prompt("E2E_DWW_A_START 用工作流调研材料并汇总。");
  111. await approveV4Permission();
  112. await browser.waitUntil(
  113. async () =>
  114. (await getLatestUpstreamToolResultByToolCallId("toolu_dww_create"))?.isError === false,
  115. { timeout: 60000, timeoutMsg: "CreateWorkflow 未执行成功" },
  116. );
  117. const toolResult = await getLatestUpstreamToolResultByToolCallId("toolu_dww_create");
  118. const launched = /started in the background with ID: (\S+?)\./.exec(toolResult?.content ?? "");
  119. expect(launched).not.toBeNull();
  120. runId = launched![1]!;
  121. await waitForV4AssistantMessageContaining("E2E_DWW_A_STARTED", 60000);
  122. await waitForV4Pane((pane) => !pane.canStop, "A 会话的启动回复没有结束");
  123. sessionA = await readSessionId();
  124. // 第一阶段的子代理被挂起:run 停在「调研」。
  125. await digestOf(runId).waitForDisplayed({ timeout: 120000 });
  126. await browser.waitUntil(async () => (await readPillStatuses(runId))["调研员"] === "running", {
  127. timeout: 120000,
  128. timeoutMsg: "调研员没有进入运行态",
  129. });
  130. await waitForRunLineText(sessionA, "调研", "A 的侧栏运行行没有写出当前阶段「调研」");
  131. // 切到 B:这边没有 run,A 的运行行还在(live run 总是画出,不管会话是否打开)。
  132. sessionB = await startSessionAndWaitForReply(
  133. "E2E_DWW_B_HELLO 这个会话和工作流无关。",
  134. "E2E_DWW_B_REPLY",
  135. );
  136. expect(sessionB).not.toBe(sessionA);
  137. await expect(digestOf(runId)).not.toBeDisplayed();
  138. await expect(runLineOf(sessionB)).not.toBeExisting();
  139. await waitForRunLineText(sessionA, "调研", "切到 B 之后 A 的运行行丢了");
  140. // B 在前台时放行第一阶段:侧栏的数据来自索引,A 没打开也要跟上「汇总」。
  141. await releaseControlledReplay("dww-agent-a");
  142. await waitForRunLineText(sessionA, "汇总", "A 不在前台时侧栏运行行没有跟上「汇总」");
  143. expect(await runLineOf(sessionA).getAttribute("data-run-status")).toBe("running");
  144. await expect(runLineOf(sessionB)).not.toBeExisting();
  145. // 切回 A:卡直接是离开期间的最新进度,不是离开前的样子。
  146. await selectV4TaskById(sessionA);
  147. await digestOf(runId).waitForDisplayed({ timeout: 60000 });
  148. await browser.waitUntil(
  149. async () => {
  150. const pills = await readPillStatuses(runId);
  151. return pills["调研员"] === "done" && pills["汇总员"] === "running";
  152. },
  153. { timeout: 60000, timeoutMsg: "切回 A 后 run 卡没有跟上离开期间的进度" },
  154. );
  155. expect(await readStationStatuses(runId)).toEqual(["done", "running"]);
  156. expect(await digestOf(runId).getAttribute("data-workflow-run-status")).toBe("running");
  157. await waitForRunLineText(sessionA, "汇总", "切回 A 后侧栏运行行不再写「汇总」");
  158. });
  159. it("DWW-02 run 在 B 在前台时结束:只有 A 的模型被叫醒,B 的请求里没有它;A 的运行行写「已完成」留到打开 A,打开后折叠并看到通知行与完成卡", async function () {
  160. this.timeout(300000);
  161. await selectV4TaskById(sessionB);
  162. await releaseControlledReplay("dww-agent-c");
  163. // 已结束且未确认:运行行换成中性词,一直留到会话被打开(presentation.md「Acknowledgement」)。
  164. await waitForRunLineText(sessionA, "已完成", "A 的运行行没有写「已完成」");
  165. expect(await runLineOf(sessionA).getAttribute("data-run-status")).toBe("completed");
  166. // 只叫醒 A:完成通知是 A 会话的一个请求,带 A 的历史。
  167. await browser.waitUntil(
  168. async () => {
  169. const [wake] = await getUpstreamRequestEvidence({
  170. includes: [`<description>${RUN_NAME}</description>`],
  171. lastUserMessageIncludes: ["<task-notification>"],
  172. });
  173. return wake?.statusCode === 200;
  174. },
  175. { timeout: 120000, timeoutMsg: "A 的模型没有收到完成通知" },
  176. );
  177. const wakes = await getUpstreamRequestEvidence({
  178. includes: [`<description>${RUN_NAME}</description>`],
  179. lastUserMessageIncludes: ["<task-notification>"],
  180. });
  181. expect(wakes.map((wake) => wake.fixtureId)).toEqual(["dww-notified"]);
  182. const wakeBody = JSON.stringify(wakes[0]?.requestJson);
  183. expect(wakeBody).toContain("E2E_DWW_A_START");
  184. expect(wakeBody).not.toContain("E2E_DWW_B_HELLO");
  185. // B 这边什么都没发生:前台没有通知行、没有完成卡,也没有被叫醒的轮次。
  186. expect((await getV4PaneSnapshot()).sessionId).toBe(sessionB);
  187. await expect($(NOTIFICATION_ROW)).not.toBeDisplayed();
  188. await expect($(COMPLETION_CARD)).not.toBeDisplayed();
  189. await expect(runLineOf(sessionB)).not.toBeExisting();
  190. // B 的下一轮请求是证据:通知若误投给 B,会出现在这一轮的历史里。
  191. await sendV4Prompt("E2E_DWW_B_AFTER 再说一句。");
  192. await waitForV4AssistantMessageContaining("E2E_DWW_B_AFTER_REPLY", 60000);
  193. await waitForV4Pane((pane) => !pane.canStop, "B 会话的回复没有结束");
  194. const bRequests = await getUpstreamRequestEvidence({ includes: ["E2E_DWW_B_HELLO"] });
  195. expect(bRequests.length).toBeGreaterThanOrEqual(2);
  196. for (const request of bRequests) {
  197. const body = JSON.stringify(request.requestJson);
  198. expect(body).not.toContain(runId);
  199. expect(body).not.toContain(RUN_NAME);
  200. }
  201. // 打开 A:确认这个 run,运行行收起;通知行与完成卡都在 A 这边。
  202. await waitForRunLineText(sessionA, "已完成", "B 的轮次之后 A 的运行行不该收起");
  203. await selectV4TaskById(sessionA);
  204. await browser.waitUntil(async () => !(await runLineOf(sessionA).isExisting()), {
  205. timeout: 30000,
  206. timeoutMsg: "打开 A 之后已结束 run 的运行行没有收起",
  207. });
  208. await waitForV4AssistantMessageContaining("E2E_DWW_A_DONE", 60000);
  209. const row = await $(NOTIFICATION_ROW);
  210. await row.waitForDisplayed({ timeout: 60000 });
  211. expect(await row.getText()).toContain("工作流已完成");
  212. expect(await row.getText()).toContain(RUN_NAME);
  213. const card = await $(COMPLETION_CARD);
  214. await card.waitForDisplayed({ timeout: 60000 });
  215. expect(await (await card.$('[data-testid="workflow-card-name"]').getText()).trim()).toBe(
  216. RUN_NAME,
  217. );
  218. await browser.waitUntil(
  219. async () =>
  220. (await card
  221. .$('[data-testid="workflow-completion-figure-subagents"]')
  222. .getAttribute("data-value")) === "2" &&
  223. (await card
  224. .$('[data-testid="workflow-completion-figure-phases"]')
  225. .getAttribute("data-value")) === "2",
  226. { timeout: 10000, timeoutMsg: "完成卡的子代理 / 阶段数字不是 2 / 2" },
  227. );
  228. });
  229. it("DWW-03 B 的模型补全别的会话的留白:引擎在 A 的进程里够不着,不弹窗直接拒绝并说明下一步;窗里也就不会出现裸的站点 id;run 原样停在留白上", async function () {
  230. this.timeout(300000);
  231. await startNewV4Draft();
  232. await sendV4Prompt("E2E_DWW_H_START 使用工作流演示留白。");
  233. await approveV4Permission();
  234. await browser.waitUntil(
  235. async () =>
  236. (await getLatestUpstreamToolResultByToolCallId("toolu_dww_h_create"))?.isError === false,
  237. { timeout: 60000, timeoutMsg: "留白 run 的 CreateWorkflow 未执行成功" },
  238. );
  239. const toolResult = await getLatestUpstreamToolResultByToolCallId("toolu_dww_h_create");
  240. const launched = /started in the background with ID: (\S+?)\./.exec(toolResult?.content ?? "");
  241. expect(launched).not.toBeNull();
  242. holeRunId = launched![1]!;
  243. await waitForV4AssistantMessageContaining("E2E_DWW_H_WAITING", 120000);
  244. await waitForV4Pane((pane) => !pane.canStop, "留白通知的回复没有结束");
  245. sessionHoleOwner = await readSessionId();
  246. await selectV4TaskById(sessionB);
  247. await sendV4Prompt(`E2E_DWW_CROSS_FILL 补全这个 run 的留白。${carryRunId(holeRunId)}`);
  248. // 这里曾先弹窗,用户批准后才被拒(launch.md「Approval」)。
  249. const refused = await waitForToolResult("toolu_dww_cross_fill");
  250. expect(refused.isError).toBe(true);
  251. expect(refused.content).toContain("hole_not_waiting");
  252. expect(refused.content).toContain("the session that started it");
  253. expect(await hasV4PermissionDialog()).toBe(false);
  254. await waitForV4Pane((pane) => !pane.canStop, "B 被拒之后 turn 没有收口");
  255. // 运行行留在 run 自己的会话下,留白还开着。
  256. await waitForRunLineText(sessionHoleOwner, "决定分组 · 等待补全", "被拒之后 run 不该变化");
  257. await expect(runLineOf(sessionB)).not.toBeExisting();
  258. });
  259. it("DWW-04 B 的模型修订别的会话还在跑的 run:本进程停不了它,不弹窗直接拒绝,不起新 run,run 原样停在留白上", async function () {
  260. this.timeout(180000);
  261. await sendV4Prompt(`E2E_DWW_CROSS_AMEND 调整这个 run。${carryRunId(holeRunId)}`);
  262. const refused = await waitForToolResult("toolu_dww_cross_amend");
  263. expect(refused.isError).toBe(true);
  264. expect(refused.content).toContain("workflow_run_held_elsewhere");
  265. expect(await hasV4PermissionDialog()).toBe(false);
  266. await waitForV4Pane((pane) => !pane.canStop, "B 被拒之后 turn 没有收口");
  267. await waitForRunLineText(sessionHoleOwner, "决定分组 · 等待补全", "被拒之后 run 不该变化");
  268. expect(await runLineOf(sessionHoleOwner).getAttribute("data-run-id")).toBe(holeRunId);
  269. await expect(runLineOf(sessionB)).not.toBeExisting();
  270. });
  271. it("DWW-05 回到 run 自己的会话:同一处留白、同样的补全不弹窗直接接进 run;通知行翻成「留白已补全」,完成通知只回到这个会话", async function () {
  272. this.timeout(240000);
  273. await selectV4TaskById(sessionHoleOwner);
  274. await sendV4Prompt("E2E_DWW_H_OWN_FILL 补全决定分组。");
  275. // 没人点过窗:工具成功返回本身就证明这一步没有确认窗(launch.md「Approval」)。
  276. await browser.waitUntil(
  277. async () =>
  278. (await getLatestUpstreamToolResultByToolCallId("toolu_dww_h_own_fill"))?.isError === false,
  279. { timeout: 60000, timeoutMsg: "本会话的 FillWorkflowHole 未执行成功(可能在等确认窗)" },
  280. );
  281. expect(await hasV4PermissionDialog()).toBe(false);
  282. await waitForV4AssistantMessageContaining("E2E_DWW_H_DONE", 120000);
  283. const notices = await getUpstreamRequestEvidence({
  284. includes: [`<description>${HOLE_RUN_NAME}</description>`],
  285. lastUserMessageIncludes: ["<task-notification>"],
  286. });
  287. expect(notices.map((notice) => notice.fixtureId)).toEqual(["dww-h-notified"]);
  288. const noticeBody = JSON.stringify(notices[0]?.requestJson);
  289. expect(noticeBody).toContain("E2E_DWW_H_START");
  290. expect(noticeBody).not.toContain("E2E_DWW_CROSS_FILL");
  291. const filledRow = await $(`${NOTIFICATION_ROW}[data-hole-state="filled"]`);
  292. await filledRow.waitForExist({ timeout: 60000 });
  293. await browser.waitUntil(
  294. async () => String(await filledRow.getProperty("textContent")).includes("留白已补全"),
  295. { timeout: 15000, timeoutMsg: "通知行没有翻成「留白已补全」" },
  296. );
  297. await digestOf(holeRunId).waitForExist({ timeout: 60000 });
  298. expect(await digestOf(holeRunId).getAttribute("data-workflow-run-status")).toBe("completed");
  299. // run 在会话打开时结束:运行行随即收起。
  300. await browser.waitUntil(async () => !(await runLineOf(sessionHoleOwner).isExisting()), {
  301. timeout: 30000,
  302. timeoutMsg: "会话在前台时完成的 run 仍在侧栏留着运行行",
  303. });
  304. });
  305. it("DWW-06 B 的模型修订别的会话已结束的 run:弹「调整此工作流?」,批准后新 run 在 B 名下跑完,沿用 run 名,通知回到 B", async function () {
  306. this.timeout(240000);
  307. await selectV4TaskById(sessionB);
  308. await sendV4Prompt(`E2E_DWW_B_AMEND 调整这个 run。${carryRunId(holeRunId)}`);
  309. await waitForV4PermissionDialog();
  310. await browser.waitUntil(
  311. async () => (await readPermissionWindowText()).includes("调整此工作流?"),
  312. { timeout: 30000, timeoutMsg: "B 的确认窗不是调整工作流的窗" },
  313. );
  314. await approveV4Permission();
  315. const amended = await waitForToolResult("toolu_dww_b_amend");
  316. expect(amended.isError).toBe(false);
  317. expect(amended.content).toContain(holeRunId);
  318. await waitForV4AssistantMessageContaining("E2E_DWW_B_AMEND_DONE", 120000);
  319. const notices = await getUpstreamRequestEvidence({
  320. includes: [`<description>${HOLE_RUN_NAME}</description>`, "E2E_DWW_B_AMEND"],
  321. lastUserMessageIncludes: ["<task-notification>"],
  322. });
  323. expect(notices.map((notice) => notice.fixtureId)).toEqual(["dww-b-amend-notified"]);
  324. });
  325. });