conversation-session-background-guide-steer.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  2. import { clearAppData, readSettings } from "#e2e/helpers/desktop-app";
  3. import { waitForUpstreamNetworkRequestStarted } from "#e2e/helpers/upstream-capture";
  4. import {
  5. clickV4Stop,
  6. clickV4Send,
  7. getV4ConversationState,
  8. getV4ComposerText,
  9. prepareV4ConversationE2E,
  10. sendV4Prompt,
  11. startNewV4Draft,
  12. waitForV4AssistantMessageContaining,
  13. waitForV4ConversationState,
  14. waitForV4ComposerText,
  15. waitForV4UserMessageContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. import {
  18. findFirstUpstreamRequestIndex,
  19. getUpstreamRequestRecordCount,
  20. waitForUpstreamRequest,
  21. } from "#e2e/helpers/conversation-session-network";
  22. import { getQueueItems } from "#e2e/helpers/conversation-session-queue";
  23. import { waitForToolCallBlockByToolCallId } from "#e2e/helpers/conversation-session-tool";
  24. import { respondToToolCrossProductBlockers } from "#e2e/helpers/conversation-session-tool-cross-product";
  25. const BACKGROUND_GUIDE_STEER_TIMEOUT_MS = 180000;
  26. describe("会话区 Background Guide Steer E2E", () => {
  27. afterEach(async () => {
  28. await stopIfBusy();
  29. });
  30. after(async () => {
  31. await browser.electron.restoreAllMocks();
  32. await clearAppData();
  33. });
  34. it("background Agent 创建过程中发送 guide 消息,应随 launch tool result 合流进下一次模型请求", async function () {
  35. this.timeout(BACKGROUND_GUIDE_STEER_TIMEOUT_MS);
  36. await prepareV4ConversationE2E();
  37. expect((await readSettings()).zcodeInteractionBehavior).toBe("guide");
  38. await startNewV4Draft();
  39. const runId = Date.now();
  40. const launchMarker = `E2E_BACKGROUND_GUIDE_STEER_LAUNCH_${runId}`;
  41. const guideMarker = `E2E_BACKGROUND_GUIDE_STEER_MESSAGE_${runId}`;
  42. const requestCountBefore = await getUpstreamRequestRecordCount();
  43. await sendV4Prompt(
  44. `${launchMarker}: Start a background agent and keep this main turn active while I guide it.`,
  45. );
  46. await waitForV4ComposerText("", "background guide steer launch prompt 发送后输入框没有清空");
  47. await waitForV4UserMessageContaining(launchMarker);
  48. await waitForUpstreamNetworkRequestStarted(launchMarker);
  49. await waitForV4ConversationState(
  50. (snapshot) => snapshot.state === "streaming",
  51. "background guide steer 首轮请求开始后没有保持 streaming",
  52. 30000,
  53. );
  54. await sendPromptWithoutInputClick(
  55. `${guideMarker}: This guidance should be coalesced with the background launch tool result.`,
  56. );
  57. await waitForV4ComposerText("", "background guide steer 引导消息发送后输入框没有清空");
  58. await waitForGuideProjection(guideMarker);
  59. await waitForToolCallBlockByToolCallId("toolu_e2e_background_guide_steer_agent", 60000);
  60. await respondToToolCrossProductBlockers();
  61. const coalescedIndex = await waitForFirstRequestIndex(
  62. {
  63. includes: [launchMarker, guideMarker, "Async agent launched successfully"],
  64. lastUserMessageExcludes: ["<task-notification>"],
  65. },
  66. requestCountBefore,
  67. "background guide steer 没有随 Agent launch tool result 合流进下一次模型请求",
  68. );
  69. await waitForV4AssistantMessageContaining("background-guide-steer-launch-consumed-ok");
  70. await waitForV4ConversationState(
  71. (snapshot) => snapshot.queueCount === 0,
  72. "background guide steer launch 合流后仍残留普通 queue",
  73. 30000,
  74. );
  75. const notificationIndex = await waitForFirstRequestIndex(
  76. {
  77. includes: ["<task-notification>", "E2E_BACKGROUND_GUIDE_STEER_CHILD_DONE"],
  78. lastUserMessageIncludes: ["<task-notification>"],
  79. },
  80. coalescedIndex,
  81. "background guide steer Agent completion notification 没有进入父模型请求",
  82. );
  83. expect(notificationIndex).toBeGreaterThan(coalescedIndex);
  84. await waitForV4AssistantMessageContaining("background-guide-steer-notification-consumed-ok");
  85. await waitForV4ConversationState(
  86. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  87. "background guide steer notification 后会话没有回到 idle",
  88. 90000,
  89. );
  90. });
  91. });
  92. async function waitForFirstRequestIndex(
  93. query: Parameters<typeof waitForUpstreamRequest>[0],
  94. afterIndex: number,
  95. timeoutMsg: string,
  96. ) {
  97. await waitForUpstreamRequest(query, timeoutMsg, 45000, { afterIndex });
  98. const index = await findFirstUpstreamRequestIndex(query, { afterIndex });
  99. if (index === null) {
  100. throw new Error(`${timeoutMsg}; request disappeared after wait`);
  101. }
  102. return index;
  103. }
  104. async function waitForGuideProjection(marker: string) {
  105. await browser.waitUntil(
  106. async () => {
  107. const queue = await getQueueItems();
  108. if (queue.some((item) => item.content.includes(marker) && item.kind === "turn-steer")) {
  109. return true;
  110. }
  111. const snapshot = await getV4ConversationState();
  112. return snapshot.queueCount === 0;
  113. },
  114. {
  115. timeout: 30000,
  116. timeoutMsg: `background guide steer 没有观察到 guide 投影或合流: ${marker}`,
  117. },
  118. );
  119. }
  120. async function sendPromptWithoutInputClick(prompt: string) {
  121. const focused = await browser.execute((inputTestId) => {
  122. const input = document.querySelector<HTMLElement>(`[data-testid="${inputTestId}"]`);
  123. input?.focus();
  124. return document.activeElement === input;
  125. // V4 已移除旧 chat-input;仍使用键盘输入,保持不点击输入框的引导场景。
  126. }, TID_V4_COMPOSER_INPUT);
  127. expect(focused).toBe(true);
  128. await browser.keys(prompt);
  129. await browser.waitUntil(async () => (await getV4ComposerText()) === prompt, {
  130. timeout: 10000,
  131. timeoutMsg: `background guide steer 没有通过键盘写入 prompt: ${prompt}`,
  132. });
  133. await clickV4Send();
  134. }
  135. async function stopIfBusy() {
  136. const snapshot = await getV4ConversationState().catch(() => null);
  137. if (snapshot?.state !== "streaming") {
  138. return;
  139. }
  140. await clickV4Stop().catch(() => undefined);
  141. await waitForV4ConversationState(
  142. (candidate) => candidate.state !== "streaming",
  143. "background guide steer 清理阶段没有退出 streaming",
  144. 30000,
  145. ).catch(() => undefined);
  146. }