conversation-session-activity-subagent-retention.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  2. import { join } from "node:path";
  3. import { selectTaskById } from "#e2e/helpers/conversation-session";
  4. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart-runtime";
  5. import { DEFAULT_WORKSPACE, clearAppData } from "#e2e/helpers/desktop-app";
  6. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  7. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  8. import {
  9. prepareV4ConversationE2E,
  10. editFirstV4UserQuery,
  11. waitForV4Edit,
  12. waitForV4ConversationState,
  13. getV4PaneSnapshot,
  14. getV4Messages,
  15. sendV4PromptAndWaitAccepted,
  16. waitForV4AssistantMessageContaining,
  17. } from "#e2e/helpers/v4-conversation";
  18. const trigger = '[data-status-capsule="activity"]';
  19. const panel = '[data-status-capsule-content="activity"]';
  20. // ACT02/05/06:真实并行 child,经 UI 精确停止一项,另一项受控完成。
  21. describe("Activity Subagent retention candidate", () => {
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. });
  26. it("保留停止和完成结果,下一轮切换历史入口", async function () {
  27. this.timeout(240000);
  28. await prepareV4ConversationE2E();
  29. await ensureToolCrossProductFullAccessMode();
  30. const marker = "E2E_CAPSULE_FOREGROUND_STOP";
  31. await sendV4PromptAndWaitAccepted(marker, marker, "首发未接纳");
  32. await $(trigger).waitForDisplayed({ timeout: 90000 });
  33. await $(trigger).click();
  34. await browser.waitUntil(
  35. async () => (await $$(`${panel} [data-testid="v4-running-subagent-stop"]`).length) === 2,
  36. { timeout: 90000, timeoutMsg: "两个真实 child 未进入可停止状态" },
  37. );
  38. await browser.waitUntil(
  39. async () => (await $(panel).getText()).includes("Stop target foreground child"),
  40. { timeout: 15000, timeoutMsg: "活动面板动画后标题不可见" },
  41. );
  42. const agents = await $$(`${panel} [data-background-task-kind="agent"]`);
  43. let stopped = false;
  44. for (const agent of agents) {
  45. if ((await agent.getText()).includes("Stop target foreground child")) {
  46. await agent.$('[data-testid="v4-running-subagent-stop"]').click();
  47. stopped = true;
  48. break;
  49. }
  50. }
  51. expect(stopped).toBe(true);
  52. await $(`${panel} [data-activity-result="stopped"]`).waitForDisplayed({ timeout: 60000 });
  53. expect((await $$(`${panel} [data-testid="v4-running-subagent-stop"]`)).length).toBe(1);
  54. await releaseControlledReplay("capsule-foreground-keep-controlled");
  55. await waitForV4AssistantMessageContaining("E2E_CAPSULE_PARENT_DONE", 90000);
  56. await $(`${panel} [data-activity-result="completed"]`).waitForDisplayed({ timeout: 30000 });
  57. expect((await $$(`${panel} [data-activity-result]`)).length).toBe(2);
  58. expect((await $$(`${panel} [data-testid="v4-running-subagent-stop"]`)).length).toBe(0);
  59. expect(await $(trigger).getText()).toMatch(/Completed|已完成/);
  60. expect(await $(`${trigger} [data-status-capsule-secondary]`).getText()).toBe("1");
  61. expect(await $('[data-testid="activity-subagent-directory"]').isExisting()).toBe(false);
  62. const completedId = await $(`${panel} [data-activity-result="completed"]`)
  63. .parentElement()
  64. .getAttribute("data-activity-id");
  65. const sessionId = (await getV4PaneSnapshot()).sessionId!;
  66. await browser.saveScreenshot(
  67. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "activity-subagents-ended.png"),
  68. );
  69. await browser.keys("Escape");
  70. await $(trigger).click();
  71. await $(`${panel} [data-activity-id="${completedId}"] button`).click();
  72. await $(`[data-session-id="${completedId!.slice("agent:".length)}"]`).waitForDisplayed({
  73. timeout: 30000,
  74. });
  75. await browser.keys("Escape");
  76. await selectTaskById(sessionId);
  77. const next = "E2E_ACTIVITY_SUBAGENT_CLEAR";
  78. await sendV4PromptAndWaitAccepted(next, next, "后续输入未接纳");
  79. await $(trigger).waitForDisplayed({ timeout: 30000 });
  80. await $(trigger).click();
  81. await $('[data-testid="activity-subagent-directory"]').waitForDisplayed({ timeout: 30000 });
  82. expect((await $$(`${panel} [data-activity-result]`)).length).toBe(0);
  83. expect(await $(trigger).getText()).toMatch(/Completed|已完成/);
  84. expect(await $(`${trigger} [data-status-capsule-secondary]`).getText()).toBe("1");
  85. await browser.keys("Escape");
  86. await waitForV4AssistantMessageContaining("E2E_ACTIVITY_SUBAGENT_CLEARED", 60000);
  87. const clearedCount = (await getV4Messages()).filter(
  88. (row) => row.role === "assistant" && row.text.includes("E2E_ACTIVITY_SUBAGENT_CLEARED"),
  89. ).length;
  90. await sendV4PromptAndWaitAccepted(
  91. "E2E_ACTIVITY_SUBAGENT_CLEAR_AGAIN",
  92. "E2E_ACTIVITY_SUBAGENT_CLEAR_AGAIN",
  93. "第二次普通输入未接纳",
  94. );
  95. await browser.waitUntil(
  96. async () =>
  97. (await getV4Messages()).filter(
  98. (row) => row.role === "assistant" && row.text.includes("E2E_ACTIVITY_SUBAGENT_CLEARED"),
  99. ).length > clearedCount,
  100. { timeout: 60000, timeoutMsg: "第二次普通输入尚未完成" },
  101. );
  102. await $(trigger).waitForDisplayed({ timeout: 10000 });
  103. await sendV4PromptAndWaitAccepted(
  104. "E2E_ACTIVITY_RESUME_START",
  105. "E2E_ACTIVITY_RESUME_START",
  106. "恢复消息未接纳",
  107. );
  108. await $(trigger).waitForDisplayed({ timeout: 60000 });
  109. await $(trigger).click();
  110. const resumed = $(
  111. `${panel} [data-activity-id="${completedId}"] [data-activity-result="completed"]`,
  112. );
  113. await $(
  114. `${panel} [data-activity-id="${completedId}"] [data-testid="v4-running-subagent-stop"]`,
  115. ).waitForDisplayed({ timeout: 60000 });
  116. await releaseControlledReplay("activity-resume-child");
  117. await resumed.waitForDisplayed({ timeout: 60000 });
  118. await waitForUpstreamRequest(
  119. { includes: ["E2E_ACTIVITY_RESUME_NEW_RESULT", "<task-notification>"] },
  120. "同一 child 新执行没有交付真实结果",
  121. 60000,
  122. );
  123. await browser.keys("Escape");
  124. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE);
  125. await selectTaskById(sessionId);
  126. await $(trigger).waitForDisplayed({ timeout: 60000 });
  127. await $(trigger).click();
  128. await $(
  129. `${panel} [data-activity-id="${completedId}"] [data-activity-result="completed"]`,
  130. ).waitForDisplayed({ timeout: 30000 });
  131. await browser.keys("Escape");
  132. await sendV4PromptAndWaitAccepted(
  133. "E2E_ACTIVITY_SUBAGENT_CLEAR2",
  134. "E2E_ACTIVITY_SUBAGENT_CLEAR2",
  135. "重启后清除消息未接纳",
  136. );
  137. await $(trigger).waitForDisplayed({ timeout: 30000 });
  138. await waitForV4AssistantMessageContaining("E2E_ACTIVITY_SUBAGENT_CLEARED", 60000);
  139. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE);
  140. await selectTaskById(sessionId);
  141. await browser.waitUntil(
  142. async () =>
  143. (await getV4Messages("user")).some((row) =>
  144. row.text.includes("E2E_ACTIVITY_SUBAGENT_CLEAR2"),
  145. ),
  146. { timeout: 30000, timeoutMsg: "重启后历史未恢复" },
  147. );
  148. expect(await $(trigger).isExisting()).toBe(true);
  149. // 回退不能因为旧 child 没有本轮行就清除其完整成功贡献。
  150. await waitForV4Edit();
  151. await editFirstV4UserQuery("E2E_ACTIVITY_REWIND_EDITED");
  152. // 旧 marker 可在更早轮次中存在,必须等待本轮独立响应及实际结算。
  153. await waitForV4AssistantMessageContaining("E2E_ACTIVITY_REWIND_RUNNING", 60000);
  154. const heldMessages = await getV4Messages("assistant");
  155. expect(heldMessages.some((row) => row.text.includes("E2E_ACTIVITY_SUBAGENT_CLEARED"))).toBe(
  156. true,
  157. );
  158. expect(heldMessages.some((row) => row.text.includes("E2E_ACTIVITY_REWIND_DONE"))).toBe(false);
  159. await releaseControlledReplay("activity-rewind-edit-controlled");
  160. await waitForV4AssistantMessageContaining("E2E_ACTIVITY_REWIND_DONE", 60000);
  161. await waitForV4ConversationState(
  162. (state) => state.state !== "streaming",
  163. "编辑响应未结算",
  164. 60000,
  165. );
  166. await browser.waitUntil(
  167. async () =>
  168. (await getV4Messages("user")).some((row) =>
  169. row.text.includes("E2E_ACTIVITY_REWIND_EDITED"),
  170. ),
  171. { timeout: 30000, timeoutMsg: "编辑未生效" },
  172. );
  173. expect(await $(`${trigger} [data-status-capsule-secondary]`).getText()).toBe("1");
  174. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE);
  175. await selectTaskById(sessionId);
  176. await $(trigger).waitForDisplayed({ timeout: 60000 });
  177. expect(await $(`${trigger} [data-status-capsule-secondary]`).getText()).toBe("1");
  178. });
  179. });