conversation-session-running-send-now-subagent.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { clearAppData, waitForDefaultWorkspaceReady } from "#e2e/helpers/desktop-app";
  2. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  3. import { assertPlainUserInput } from "#e2e/helpers/incoming-message-evidence";
  4. import {
  5. getUpstreamRequestEvidence,
  6. waitForUpstreamRequest,
  7. waitForUpstreamRequestContaining,
  8. } from "#e2e/helpers/conversation-session-network";
  9. import {
  10. clickFirstQueueSendNow,
  11. waitForQueueOrderContaining,
  12. } from "#e2e/helpers/conversation-session-queue";
  13. import { waitForToolCallBlockByToolCallId } from "#e2e/helpers/conversation-session-tool";
  14. import {
  15. clickV4Stop,
  16. getV4ComposerBackgroundWorkCounts,
  17. getV4ConversationState,
  18. prepareV4ConversationE2E,
  19. selectV4TaskById,
  20. sendV4Prompt,
  21. waitForV4AssistantMessageContaining,
  22. waitForV4ComposerText,
  23. waitForV4ConversationState,
  24. waitForV4UserMessageContaining,
  25. } from "#e2e/helpers/v4-conversation";
  26. // M07n:前台 Agent 运行中点队列项“立即发送”,子代理转入后台继续运行,不随旧轮取消。
  27. // fixture 见 fixtures/upstream/conversation-session/conversation-session-running-send-now-subagent.json。
  28. const M07N_TIMEOUT_MS = 240000;
  29. const PARENT_MARKER = "E2E_SEND_NOW_SUBAGENT_PARENT";
  30. const PARENT_DONE = "E2E_SEND_NOW_SUBAGENT_PARENT_DONE";
  31. const CHILD_PROMPT_PREFIX = "E2E_SEND_NOW_SUBAGENT_CHILD: work slowly";
  32. const CHILD_RESULT = "E2E_SEND_NOW_SUBAGENT_CHILD_RESULT";
  33. const QUEUED_PROMPT = "E2E_SEND_NOW_SUBAGENT_QUEUED: reply after the send-now.";
  34. const QUEUED_REPLY = "E2E_SEND_NOW_SUBAGENT_QUEUED_REPLY";
  35. const TOOL_CALL_ID = "toolu_e2e_send_now_subagent_agent";
  36. /** 子代理回复的受控流 fixture:测试断言运行中计数后才放行。 */
  37. const CHILD_FIXTURE_ID = "running-send-now-subagent-child-held";
  38. const ASYNC_LAUNCHED_TEXT = "Async agent launched successfully";
  39. const USER_STOPPED_TOOL_RESULT_PREFIX = "The user doesn't want to proceed with this tool use.";
  40. const INTERRUPT_MARKER_PREFIX = "[Request interrupted by user";
  41. describe("会话区立即发送时前台子代理转后台 E2E", () => {
  42. before(async function () {
  43. this.timeout(M07N_TIMEOUT_MS);
  44. await prepareV4ConversationE2E();
  45. });
  46. afterEach(async () => {
  47. const snapshot = await getV4ConversationState().catch(() => null);
  48. if (snapshot?.state === "streaming") await clickV4Stop().catch(() => undefined);
  49. });
  50. after(async () => {
  51. await browser.electron.restoreAllMocks();
  52. await clearAppData();
  53. });
  54. it("M07n:前台 Agent 运行中点立即发送,Agent 转后台,完成后通知回到父模型", async function () {
  55. this.timeout(M07N_TIMEOUT_MS);
  56. await sendV4Prompt(`${PARENT_MARKER}: launch one foreground Agent and wait for it.`);
  57. await waitForV4ComposerText("", "M07n 父消息发送后输入框没有清空");
  58. await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 60000);
  59. // 子代理请求已到 replay server,回复由受控流挂起:此时父轮停在等待前台 Agent。
  60. await waitForUpstreamRequest(
  61. { lastUserMessageIncludes: [CHILD_PROMPT_PREFIX] },
  62. "M07n 子代理请求没有到达 replay server",
  63. 60000,
  64. );
  65. await waitForV4ConversationState(
  66. (snapshot) => snapshot.state === "streaming",
  67. "M07n 前台 Agent 运行中父会话没有保持 running",
  68. 15000,
  69. );
  70. await sendV4Prompt(QUEUED_PROMPT);
  71. await waitForV4ComposerText("", "M07n 排队消息发送后输入框没有清空");
  72. const queued = await waitForQueueOrderContaining(
  73. ["E2E_SEND_NOW_SUBAGENT_QUEUED"],
  74. "M07n 点立即发送前没有形成 text queue",
  75. );
  76. const sentNow = await clickFirstQueueSendNow();
  77. expect(sentNow.id).toBe(queued[0]?.id);
  78. await waitForUpstreamRequestContaining("E2E_SEND_NOW_SUBAGENT_QUEUED:", 30000);
  79. await waitForV4UserMessageContaining("E2E_SEND_NOW_SUBAGENT_QUEUED");
  80. await waitForV4AssistantMessageContaining(QUEUED_REPLY, 30000);
  81. // 新一轮请求:Agent 的结果是成功的 async_launched,不是停止文字,也没有中断标记;
  82. // 被点消息是这批 tool_result 之后的普通 user 文本。工具描述里本来就有 <task-notification>,
  83. // 所以按请求顺序取第一条含被点消息的请求,不按正文排除通知。
  84. const [queuedRequest] = await getUpstreamRequestEvidence({
  85. includes: ["E2E_SEND_NOW_SUBAGENT_QUEUED:"],
  86. excludes: ["Generate a concise title"],
  87. });
  88. expect(queuedRequest).toBeDefined();
  89. const queuedRequestText = JSON.stringify(queuedRequest!.requestJson);
  90. expect(queuedRequestText).toContain(TOOL_CALL_ID);
  91. expect(queuedRequestText).toContain(ASYNC_LAUNCHED_TEXT);
  92. expect(queuedRequestText).not.toContain(USER_STOPPED_TOOL_RESULT_PREFIX);
  93. expect(queuedRequestText).not.toContain(INTERRUPT_MARKER_PREFIX);
  94. await assertPlainUserInput(queuedRequest!.requestJson, {
  95. body: QUEUED_PROMPT,
  96. evidenceLabel: "send-now-subagent",
  97. });
  98. // 父会话已空闲,子代理仍在后台运行(Activity 胶囊的运行中子代理计数);Agent 卡片是启动成功,不是已停止。
  99. await waitForV4ConversationState(
  100. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 0,
  101. "M07n 被点消息回复后父会话没有回到 idle",
  102. 30000,
  103. );
  104. expect(await readRunningSubagentCount()).toBe(1);
  105. expect((await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 10000)).status).toBe("completed");
  106. // 修复原因:原来子代理回复固定延迟 20 秒,慢机器上可能在上面的计数断言前就完成,计数变成 0。
  107. // 改为受控流:计数断言做完才放行子代理,断言不再依赖时钟。
  108. await releaseControlledReplay(CHILD_FIXTURE_ID);
  109. // 子代理被放行后返回结果:completed notification 回到父模型。
  110. await waitForUpstreamRequest(
  111. {
  112. includes: [
  113. PARENT_MARKER,
  114. CHILD_RESULT,
  115. `<tool-use-id>${TOOL_CALL_ID}</tool-use-id>`,
  116. "<status>completed</status>",
  117. ],
  118. lastUserMessageIncludes: ["<task-notification>", "<status>completed</status>"],
  119. },
  120. "M07n 后台子代理完成通知没有进入父模型请求",
  121. 60000,
  122. );
  123. await waitForV4AssistantMessageContaining(PARENT_DONE, 60000);
  124. await waitForV4ConversationState(
  125. (snapshot) => snapshot.state !== "streaming",
  126. "M07n 完成通知轮次没有回到 idle",
  127. 30000,
  128. );
  129. await browser.waitUntil(async () => (await readRunningSubagentCount()) === 0, {
  130. timeout: 30000,
  131. timeoutMsg: "M07n 子代理完成后 Activity 胶囊仍显示运行中的子代理",
  132. });
  133. expect((await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 10000)).status).toBe("completed");
  134. // 冷恢复:重启应用后从持久化记录重建“前台 Agent → async_launched → 后台完成”这组事件。
  135. // 子代理已完成才重启;运行中重启会结束 agent 进程,测的就变成崩溃恢复。
  136. const sessionId = (await getV4ConversationState()).taskId!;
  137. await browser.reloadSession();
  138. await waitForDefaultWorkspaceReady(60_000);
  139. await selectV4TaskById(sessionId);
  140. await waitForV4ConversationState(
  141. (snapshot) => snapshot.taskId === sessionId,
  142. "M07n 冷恢复没有回到原会话",
  143. 30_000,
  144. );
  145. await waitForV4UserMessageContaining("E2E_SEND_NOW_SUBAGENT_QUEUED");
  146. await waitForV4AssistantMessageContaining(QUEUED_REPLY, 30000);
  147. await waitForV4AssistantMessageContaining(PARENT_DONE, 30000);
  148. expect((await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 30000)).status).toBe("completed");
  149. expect(await readRunningSubagentCount()).toBe(0);
  150. });
  151. });
  152. // 修复原因:原来读右上角状态面板的 data-running-agent-count;staging 0876b488d3 退役了该面板,
  153. // 计数恒为 0。改读 Composer Activity 胶囊的运行中子代理计数,没有运行项时胶囊不计数,记为 0。
  154. async function readRunningSubagentCount(): Promise<number> {
  155. return (await getV4ComposerBackgroundWorkCounts())?.subagentCount ?? 0;
  156. }