conversation-session-subagent-respond-to-coordinator.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. import { registerIncomingMessageActiveCases } from "../helpers/incoming-message-active-cases.js";
  2. import {
  3. INCOMING_MESSAGE_MODES,
  4. prepareIncomingMessageCapability,
  5. } from "../helpers/incoming-message-capability.js";
  6. import { assertIncomingMessage } from "../helpers/incoming-message-evidence.js";
  7. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  8. import { join } from "node:path";
  9. import { TID_TOOL_SUMMARY_TRIGGER, TID_V4_SUBAGENT_OPEN_SIDE_PANE } from "@zcode/shared/test-ids";
  10. import { clearAppData } from "../helpers/desktop-app.js";
  11. import type { E2ENetworkCaptureArtifact } from "../helpers/network-capture-proxy.js";
  12. import {
  13. findFirstUpstreamRequestIndex,
  14. getUpstreamRequestEvidence,
  15. getUpstreamRequestRecordCount,
  16. waitForUpstreamRequest,
  17. } from "../helpers/conversation-session-network.js";
  18. import { resolveE2ERuntimePath } from "../helpers/e2e-runtime-paths.js";
  19. import {
  20. getToolCallBlockByToolCallId,
  21. waitForToolCallBlockByToolCallId,
  22. } from "../helpers/conversation-session-tool.js";
  23. import {
  24. clickV4Stop,
  25. getV4PaneSnapshot,
  26. getV4ConfigProjection,
  27. prepareV4ConversationE2E,
  28. sendV4Prompt,
  29. startNewV4Draft,
  30. switchV4Mode,
  31. waitForV4Pane,
  32. waitForV4TimelineContaining,
  33. } from "../helpers/v4-conversation.js";
  34. const E2E_TIMEOUT_MS = 240_000;
  35. const E2E_ROOT = resolveE2ERuntimePath("conversation-session-subagent-respond-to-coordinator");
  36. const RELEASE_FILE = join(E2E_ROOT, "release-child-bash");
  37. const FINAL_RELEASE_FILE = join(E2E_ROOT, "release-child-final");
  38. const ERROR_CHILD_RELEASE_FILE = join(E2E_ROOT, "release-error-child");
  39. const CONTINUED_WORK_FILE = join(E2E_ROOT, "continued-work.txt");
  40. const PARENT_AGENT_TOOL_CALL_ID = "toolu_e2e_subagent_response_agent";
  41. const RESPOND_TO_COORDINATOR_TOOL_CALL_ID = "toolu_e2e_subagent_respond_to_coordinator";
  42. const TASK_OUTPUT_TOOL_CALL_ID = "toolu_e2e_task_output_card";
  43. const ERROR_AGENT_TOOL_CALL_ID = "toolu_e2e_error_agent";
  44. const ERROR_RESPOND_TO_COORDINATOR_TOOL_CALL_ID = "toolu_e2e_error_respond_to_coordinator";
  45. const ERROR_TASK_OUTPUT_TOOL_CALL_ID = "toolu_e2e_error_task_output";
  46. const MISSING_TASK_ID = "task_e2e_missing";
  47. // Bug 原因:工具执行失败已经统一使用通用生命周期文案;旧断言仍期待 TaskOutput / RespondToCoordinator
  48. // 的专属失败文案,导致产品正确显示“执行失败”时 E2E 误报。中英文都按共享状态文案校验。
  49. const TOOL_EXECUTION_FAILURE_LABELS = ["执行失败", "Failed"] as const;
  50. describe("会话区 BG25/O18/O19 coordinator 与 TaskOutput 专属卡片 E2E", () => {
  51. before(async function () {
  52. this.timeout(E2E_TIMEOUT_MS);
  53. await prepareV4ConversationE2E();
  54. });
  55. beforeEach(async function () {
  56. this.timeout(E2E_TIMEOUT_MS);
  57. await mkdir(E2E_ROOT, { recursive: true });
  58. await rm(RELEASE_FILE, { force: true });
  59. await rm(FINAL_RELEASE_FILE, { force: true });
  60. await rm(ERROR_CHILD_RELEASE_FILE, { force: true });
  61. await writeFile(CONTINUED_WORK_FILE, "E2E_SUBAGENT_CONTINUED_WORK\n", "utf-8");
  62. await startNewV4Draft();
  63. // Bug 原因:legacy helper 会遍历所有 workspace,只要任一旧 workspace 是 yolo
  64. // 就误判当前 v4 draft 已切换,实际 session 仍以 build 启动,child Bash 永久等权限。
  65. // 必须通过当前 v4 composer 明确写入 draft mode,再启动本 case 的 session。
  66. await selectCurrentV4FullAccessMode();
  67. });
  68. afterEach(async () => {
  69. await writeFile(RELEASE_FILE, "release\n", "utf-8").catch(() => undefined);
  70. await writeFile(FINAL_RELEASE_FILE, "release\n", "utf-8").catch(() => undefined);
  71. await writeFile(ERROR_CHILD_RELEASE_FILE, "release\n", "utf-8").catch(() => undefined);
  72. await stopIfBusy();
  73. await rm(E2E_ROOT, { recursive: true, force: true }).catch(() => undefined);
  74. });
  75. after(async () => {
  76. await browser.electron.restoreAllMocks();
  77. await clearAppData();
  78. });
  79. for (const { label, mcs } of INCOMING_MESSAGE_MODES) {
  80. it(`${label} 渲染 child coordinator 回复与 completed TaskOutput,同时保持父子可见性边界`, async function () {
  81. this.timeout(E2E_TIMEOUT_MS);
  82. await prepareIncomingMessageCapability(mcs);
  83. await selectCurrentV4FullAccessMode();
  84. const prompt =
  85. "E2E_SUBAGENT_RESPOND_TO_COORDINATOR: Launch a background agent, ask it E2E_SUBAGENT_COORDINATOR_QUESTION while it is running, consume its response, and wait for E2E_SUBAGENT_FINAL_RESULT.";
  86. const requestCountBefore = await getUpstreamRequestRecordCount();
  87. // Bug 原因:v4 已删除 legacy chat-input/chat-view;旧 BG25 在 before hook
  88. // 结束,RespondToCoordinator 主链路实际未运行。这里统一改读 v4 pane/timeline。
  89. await sendV4Prompt(prompt);
  90. await waitForV4TimelineContaining("E2E_SUBAGENT_RESPOND_TO_COORDINATOR", 60000);
  91. await waitForToolCallBlockByToolCallId(PARENT_AGENT_TOOL_CALL_ID, 60_000);
  92. const sendMessageResultIndex = await waitForFirstRequestIndex(
  93. {
  94. includes: [
  95. "E2E_SUBAGENT_RESPOND_TO_COORDINATOR",
  96. "E2E_SUBAGENT_COORDINATOR_QUESTION",
  97. "sent to its active turn",
  98. ],
  99. },
  100. requestCountBefore,
  101. "SendMessage delivery result 没有进入 parent provider request",
  102. );
  103. await waitForV4Pane(
  104. (snapshot) => !snapshot.canStop,
  105. "parent SendMessage ack 没有完成",
  106. 30000,
  107. );
  108. await writeFile(RELEASE_FILE, "release\n", "utf-8");
  109. const childResponseToolsIndex = await waitForFirstRequestIndex(
  110. {
  111. includes: [
  112. "E2E_SUBAGENT_CHILD_BASH_RELEASED",
  113. "The coordinator sent a message while you were working:",
  114. "E2E_SUBAGENT_COORDINATOR_QUESTION",
  115. "E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD",
  116. ],
  117. },
  118. sendMessageResultIndex,
  119. "child 没有在 Bash tool result 后收到 coordinator question",
  120. );
  121. const childContinuedWorkIndex = await waitForFirstRequestIndex(
  122. {
  123. includes: [
  124. "E2E_SUBAGENT_RESPONSE_QUEUED",
  125. "was queued for the coordinator",
  126. "E2E_SUBAGENT_CONTINUED_WORK",
  127. ],
  128. },
  129. childResponseToolsIndex,
  130. "RespondToCoordinator 与 Read tool result 没有共同进入 child 后续 request",
  131. );
  132. const parentResponseIndex = await waitForFirstRequestIndex(
  133. {
  134. includes: [
  135. "<subagent-message>",
  136. "E2E_SUBAGENT_RESPONSE_QUEUED",
  137. "E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD",
  138. ],
  139. },
  140. sendMessageResultIndex,
  141. "parent provider request 没有消费 model-only subagent response",
  142. );
  143. await waitForV4TimelineContaining("E2E_SUBAGENT_COORDINATOR_CONSUMED", 90000);
  144. await waitForV4Pane(
  145. (snapshot) => !snapshot.canStop,
  146. "parent 消费 coordinator response 后没有回到 idle",
  147. 90_000,
  148. );
  149. // Bug 原因:child 原来固定 sleep 1 秒后结束,慢机器上 completion notification
  150. // 会先并入 parent response turn,使 strict replay 无法分别验证两次 provider request。
  151. // 只有 parent 已消费 response 且回到 idle 后才放行 child 终态,固定产品要求的顺序。
  152. await writeFile(FINAL_RELEASE_FILE, "release\n", "utf-8");
  153. const childFinalIndex = await waitForFirstRequestIndex(
  154. { includes: ["E2E_SUBAGENT_CONTINUED_WORK_TOOL_DONE"] },
  155. childContinuedWorkIndex,
  156. "child 回复 coordinator 后没有继续执行普通工作 tool",
  157. );
  158. const notificationIndex = await waitForFirstRequestIndex(
  159. {
  160. includes: ["<task-notification>", "E2E_SUBAGENT_FINAL_RESULT"],
  161. lastUserMessageIncludes: ["<task-notification>"],
  162. },
  163. Math.min(parentResponseIndex, childFinalIndex),
  164. "child final completion notification 没有进入 parent provider request",
  165. );
  166. const childEvidence = await getUpstreamRequestEvidence(
  167. {
  168. includes: [
  169. "The coordinator sent a message while you were working:",
  170. "E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD",
  171. ],
  172. },
  173. { afterIndex: childResponseToolsIndex - 1, beforeIndex: childResponseToolsIndex + 1 },
  174. );
  175. await assertIncomingMessage(childEvidence[0]!.requestJson, {
  176. presentation: "coordinator_steer",
  177. marker: "E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD",
  178. role: mcs ? "system" : "user",
  179. evidenceLabel: label,
  180. // summary 只用于 UI 预览,child 只收到 message。
  181. body: "E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD",
  182. });
  183. const peerEvidence = await getUpstreamRequestEvidence(
  184. { includes: ["<subagent-message>"] },
  185. { afterIndex: parentResponseIndex - 1, beforeIndex: parentResponseIndex + 1 },
  186. );
  187. await assertIncomingMessage(peerEvidence[0]!.requestJson, {
  188. presentation: "subagent_reply",
  189. evidenceLabel: label,
  190. marker: "E2E_SUBAGENT_RESPONSE_QUEUED",
  191. role: "user",
  192. });
  193. const notificationEvidence = await getUpstreamRequestEvidence(
  194. { includes: ["<task-notification>", "E2E_SUBAGENT_FINAL_RESULT"] },
  195. { afterIndex: notificationIndex - 1, beforeIndex: notificationIndex + 1 },
  196. );
  197. await assertIncomingMessage(notificationEvidence[0]!.requestJson, {
  198. presentation: "task_notification",
  199. evidenceLabel: label,
  200. marker: "E2E_SUBAGENT_FINAL_RESULT",
  201. role: "user",
  202. });
  203. const responsePosition = await getRequestMarkerPosition(
  204. parentResponseIndex,
  205. "<subagent-message>",
  206. );
  207. const notificationPosition = await getRequestMarkerPosition(
  208. notificationIndex,
  209. "<task-notification>",
  210. );
  211. expect(compareRequestPositions(responsePosition, notificationPosition)).toBeLessThan(0);
  212. // v4 P9 只在父 timeline 保留 Agent/subagent 摘要,child tool rows 归 child
  213. // conversation,不再嵌进父 Agent 卡。BG25 的产品语义是双向消息链与 child
  214. // 继续执行,前面的 provider request 顺序已覆盖;旧 renderer-store 镜像断言应移除。
  215. const timeline = await getV4PaneSnapshot();
  216. expect(timeline.timelineText).not.toContain("E2E_SUBAGENT_RESPONSE_QUEUED");
  217. expect(timeline.timelineText).not.toContain("<subagent-message>");
  218. expect(await getToolCallBlockByToolCallId(RESPOND_TO_COORDINATOR_TOOL_CALL_ID)).toBeNull();
  219. await waitForV4Pane(
  220. (snapshot) => !snapshot.canStop,
  221. "RespondToCoordinator completion 后 parent 会话没有回到 idle",
  222. 90_000,
  223. );
  224. await assertTaskOutputCard();
  225. await assertRespondToCoordinatorCard();
  226. // 修复原因:BG25 不覆盖 compact;若 spec 误回 manual-review,小窗口配置会插入无关 divider。
  227. const compactMarkerCount = await browser.execute(
  228. () =>
  229. document.querySelectorAll('[data-row-kind="timelineMarker"][data-marker-type="compact"]')
  230. .length,
  231. );
  232. expect(compactMarkerCount).toBe(0);
  233. });
  234. }
  235. it("组合渲染 TaskOutput 未知 ID 与 child RespondToCoordinator 输入错误", async function () {
  236. this.timeout(E2E_TIMEOUT_MS);
  237. // Bug 原因:beforeEach 的全局新建会按当前导航决定 cwd。单独运行时它分配独立目录,
  238. // child Bash 按相对路径等待 release 文件,而测试写在 ZCodeProject 下,child 永远等不到;
  239. // 排在 O18 之后才因上一个用例留在夹具项目而通过。这里显式回到夹具项目草稿。
  240. await prepareV4ConversationE2E();
  241. await selectCurrentV4FullAccessMode();
  242. const requestCountBefore = await getUpstreamRequestRecordCount();
  243. await sendV4Prompt(
  244. "E2E_TOOL_RENDERER_ERROR_CARDS: Launch the controlled background child and wait for E2E_ERROR_AGENT_COMPLETED.",
  245. );
  246. await waitForV4TimelineContaining("E2E_TOOL_RENDERER_ERROR_CARDS", 60_000);
  247. await waitForToolCallBlockByToolCallId(ERROR_AGENT_TOOL_CALL_ID, 60_000);
  248. const agentLaunchResultIndex = await waitForFirstRequestIndex(
  249. {
  250. includes: [
  251. "E2E_TOOL_RENDERER_ERROR_CARDS",
  252. "E2E_RESPOND_ERROR_CHILD",
  253. "Async agent launched successfully",
  254. ],
  255. },
  256. requestCountBefore,
  257. "错误卡片 case 的 background Agent 启动结果没有进入 parent provider request",
  258. );
  259. await waitForV4TimelineContaining("E2E_ERROR_AGENT_STARTED", 60_000);
  260. await waitForV4Pane(
  261. (snapshot) => !snapshot.canStop,
  262. "错误卡片 case 的 Agent 启动 turn 没有回到 idle",
  263. 60_000,
  264. );
  265. await writeFile(ERROR_CHILD_RELEASE_FILE, "release\n", "utf-8");
  266. const childValidationErrorIndex = await waitForFirstRequestIndex(
  267. {
  268. includes: [
  269. ERROR_RESPOND_TO_COORDINATOR_TOOL_CALL_ID,
  270. "InputValidationError: RespondToCoordinator failed due to the following issue:",
  271. "The required parameter `message` is missing",
  272. ],
  273. lastUserMessageIncludes: [
  274. "InputValidationError: RespondToCoordinator failed due to the following issue:",
  275. ],
  276. },
  277. agentLaunchResultIndex,
  278. "RespondToCoordinator 输入校验错误没有进入 child provider continuation",
  279. );
  280. await waitForFirstRequestIndex(
  281. {
  282. includes: ["<task-notification>", "E2E_RESPOND_ERROR_CHILD_DONE"],
  283. lastUserMessageIncludes: ["<task-notification>", "E2E_RESPOND_ERROR_CHILD_DONE"],
  284. },
  285. childValidationErrorIndex,
  286. "错误 child 的完成通知没有进入 parent provider request",
  287. );
  288. await waitForV4TimelineContaining("E2E_ERROR_AGENT_COMPLETED", 60_000);
  289. await waitForV4Pane(
  290. (snapshot) => !snapshot.canStop,
  291. "错误 child 完成后 parent 没有回到 idle",
  292. 60_000,
  293. );
  294. expect(
  295. await getToolCallBlockByToolCallId(ERROR_RESPOND_TO_COORDINATOR_TOOL_CALL_ID),
  296. ).toBeNull();
  297. await assertTaskOutputErrorCard();
  298. await assertRespondToCoordinatorErrorCard();
  299. });
  300. registerIncomingMessageActiveCases();
  301. });
  302. async function assertTaskOutputCard(): Promise<void> {
  303. const requestCountBefore = await getUpstreamRequestRecordCount();
  304. await sendV4Prompt(
  305. "E2E_TASK_OUTPUT_TOOL_CARD: Call TaskOutput with block=false and timeout=0 for the completed background agent, then report E2E_TASK_OUTPUT_TOOL_CARD_DONE.",
  306. );
  307. await waitForFirstRequestIndex(
  308. {
  309. includes: [
  310. "toolu_e2e_task_output_card",
  311. "<retrieval_status>success</retrieval_status>",
  312. "<task_id>agent_",
  313. "<status>completed</status>",
  314. "E2E_SUBAGENT_FINAL_RESULT",
  315. ],
  316. },
  317. requestCountBefore,
  318. "TaskOutput completed result 没有进入 parent provider request",
  319. );
  320. await waitForV4TimelineContaining("E2E_TASK_OUTPUT_TOOL_CARD_DONE", 60_000);
  321. await waitForV4Pane(
  322. (snapshot) => !snapshot.canStop,
  323. "TaskOutput tool turn 没有回到 idle",
  324. 60_000,
  325. );
  326. const collapsed = await waitForToolCallBlockByToolCallId(TASK_OUTPUT_TOOL_CALL_ID, 30_000);
  327. expect(collapsed.status).toBe("completed");
  328. expect(collapsed.text).toMatch(/agent_[A-Za-z0-9_-]+/u);
  329. expect(["已获取", "Output retrieved"].some((label) => collapsed.text.includes(label))).toBe(true);
  330. expect(collapsed.text).not.toContain("E2E_SUBAGENT_FINAL_RESULT");
  331. assertTaskOutputProviderFieldsHidden(collapsed.text);
  332. const collapsedState = await readToolCardState(collapsed.testId);
  333. expect(collapsedState.ariaExpanded).toBe("false");
  334. expect(collapsedState.hasCollapsibleContent).toBe(true);
  335. await browser.execute(
  336. (blockTestId, summaryTriggerPrefix) => {
  337. const block = document.querySelector<HTMLElement>(`[data-testid="${blockTestId}"]`);
  338. block?.querySelector<HTMLElement>(`[data-testid^="${summaryTriggerPrefix}-"]`)?.click();
  339. },
  340. collapsed.testId,
  341. TID_TOOL_SUMMARY_TRIGGER,
  342. );
  343. await browser.waitUntil(
  344. async () => {
  345. const state = await readToolCardState(collapsed.testId);
  346. return state.ariaExpanded === "true" && state.text.includes("E2E_SUBAGENT_FINAL_RESULT");
  347. },
  348. {
  349. timeout: 10_000,
  350. timeoutMsg: "TaskOutput 卡片展开后没有显示 task output",
  351. },
  352. );
  353. const expandedState = await readToolCardState(collapsed.testId);
  354. assertTaskOutputProviderFieldsHidden(expandedState.text);
  355. }
  356. async function assertRespondToCoordinatorCard(): Promise<void> {
  357. await openSubagentSidePane(PARENT_AGENT_TOOL_CALL_ID);
  358. const responseCard = await waitForToolCallBlockByToolCallId(
  359. RESPOND_TO_COORDINATOR_TOOL_CALL_ID,
  360. 30_000,
  361. );
  362. expect(responseCard.status).toBe("completed");
  363. expect(responseCard.text).toContain("E2E_SUBAGENT_RESPONSE_QUEUED");
  364. expect(["回复已排队", "Reply queued"].some((label) => responseCard.text.includes(label))).toBe(
  365. true,
  366. );
  367. expect(responseCard.text).not.toContain("E2E_SUBAGENT_COORDINATOR_QUESTION_PAYLOAD");
  368. expect(responseCard.text).not.toContain("was queued for the coordinator");
  369. expect(responseCard.text).not.toMatch(/response_[A-Za-z0-9_-]+/u);
  370. const responseCardState = await readToolCardState(responseCard.testId);
  371. expect(responseCardState.ariaExpanded).toBeNull();
  372. expect(responseCardState.hasCollapsibleContent).toBe(false);
  373. }
  374. async function assertTaskOutputErrorCard(): Promise<void> {
  375. const requestCountBefore = await getUpstreamRequestRecordCount();
  376. await sendV4Prompt(
  377. `E2E_TASK_OUTPUT_ERROR_CARD: Call TaskOutput for ${MISSING_TASK_ID}, then report E2E_TASK_OUTPUT_ERROR_DONE.`,
  378. );
  379. await waitForFirstRequestIndex(
  380. {
  381. includes: [
  382. ERROR_TASK_OUTPUT_TOOL_CALL_ID,
  383. `<tool_use_error>No task found with ID: ${MISSING_TASK_ID}</tool_use_error>`,
  384. ],
  385. lastUserMessageIncludes: [`No task found with ID: ${MISSING_TASK_ID}`],
  386. },
  387. requestCountBefore,
  388. "TaskOutput 未知 ID 错误没有进入 parent provider continuation",
  389. );
  390. await waitForV4TimelineContaining("E2E_TASK_OUTPUT_ERROR_DONE", 60_000);
  391. await waitForV4Pane(
  392. (snapshot) => !snapshot.canStop,
  393. "TaskOutput 错误 turn 没有回到 idle",
  394. 60_000,
  395. );
  396. const card = await waitForToolCallBlockByToolCallId(ERROR_TASK_OUTPUT_TOOL_CALL_ID, 30_000);
  397. expect(card.status).toBe("failed");
  398. expect(card.text).toContain(MISSING_TASK_ID);
  399. expect(TOOL_EXECUTION_FAILURE_LABELS.some((label) => card.text.includes(label))).toBe(true);
  400. expect(card.text).not.toContain("No task found with ID");
  401. expect(card.text).not.toContain("<tool_use_error>");
  402. const state = await readToolCardState(card.testId);
  403. expect(state.ariaExpanded).toBeNull();
  404. expect(state.hasCollapsibleContent).toBe(false);
  405. }
  406. async function assertRespondToCoordinatorErrorCard(): Promise<void> {
  407. await openSubagentSidePane(ERROR_AGENT_TOOL_CALL_ID);
  408. const card = await waitForToolCallBlockByToolCallId(
  409. ERROR_RESPOND_TO_COORDINATOR_TOOL_CALL_ID,
  410. 30_000,
  411. );
  412. expect(card.status).toBe("failed");
  413. expect(card.text).toContain("E2E_RESPOND_ERROR_SUMMARY");
  414. expect(TOOL_EXECUTION_FAILURE_LABELS.some((label) => card.text.includes(label))).toBe(true);
  415. expect(card.text).not.toContain("InputValidationError");
  416. expect(card.text).not.toContain("The required parameter");
  417. expect(card.text).not.toContain("<tool_use_error>");
  418. const state = await readToolCardState(card.testId);
  419. expect(state.ariaExpanded).toBeNull();
  420. expect(state.hasCollapsibleContent).toBe(false);
  421. }
  422. async function openSubagentSidePane(parentAgentToolCallId: string): Promise<void> {
  423. const parentAgent = await waitForToolCallBlockByToolCallId(parentAgentToolCallId, 30_000);
  424. const parentAgentElement = await $(`[data-testid="${parentAgent.testId}"]`);
  425. const openChildAction = await parentAgentElement.$(
  426. `[data-testid^="${TID_V4_SUBAGENT_OPEN_SIDE_PANE}-"]`,
  427. );
  428. await openChildAction.waitForDisplayed({
  429. timeout: 10_000,
  430. timeoutMsg: "BG25 parent Agent 卡没有 child conversation 入口",
  431. });
  432. const actionTestId = await openChildAction.getAttribute("data-testid");
  433. const childSessionId = actionTestId?.slice(`${TID_V4_SUBAGENT_OPEN_SIDE_PANE}-`.length);
  434. if (!childSessionId) {
  435. throw new Error("BG25 parent Agent 卡的 child session 入口缺少 session id");
  436. }
  437. // 测试原因:生产包中的 Agent 摘要可能在 completion 投影时重挂载,单次
  438. // WebDriver element click 会落到旧节点。每轮都从当前 DOM 重新定位并点击,
  439. // 只以目标 child session pane 真正可见作为成功条件,不改生产交互。
  440. for (let attempt = 0; attempt < 3; attempt += 1) {
  441. await browser.execute(
  442. (blockTestId, actionId) => {
  443. document
  444. .querySelector<HTMLElement>(`[data-testid="${blockTestId}"] [data-testid="${actionId}"]`)
  445. ?.click();
  446. },
  447. parentAgent.testId,
  448. actionTestId,
  449. );
  450. try {
  451. await browser.waitUntil(
  452. () =>
  453. browser.execute(
  454. (expectedChildSessionId) =>
  455. Boolean(
  456. document.querySelector(
  457. '[data-side-pane-tab-id^="subagent-session:"][data-state="active"]',
  458. ),
  459. ) &&
  460. Array.from(
  461. document.querySelectorAll<HTMLElement>(
  462. `[data-session-id="${expectedChildSessionId}"]`,
  463. ),
  464. ).some((pane) => pane.offsetParent !== null),
  465. childSessionId,
  466. ),
  467. {
  468. timeout: 10_000,
  469. timeoutMsg: "BG25 child conversation 没有在右侧面板打开",
  470. },
  471. );
  472. return;
  473. } catch {
  474. if (attempt === 2) {
  475. throw new Error("BG25 child conversation 没有在右侧面板打开");
  476. }
  477. }
  478. }
  479. }
  480. function assertTaskOutputProviderFieldsHidden(text: string): void {
  481. expect(text).not.toContain("<retrieval_status>");
  482. expect(text).not.toContain("<task_id>");
  483. expect(text).not.toContain("<task_type>");
  484. expect(text).not.toContain("<status>");
  485. expect(text).not.toContain("<exit_code>");
  486. expect(text).not.toContain("<output>");
  487. expect(text).not.toContain("<error>");
  488. expect(text).not.toContain("<persisted-output>");
  489. expect(text).not.toContain("E2E RespondToCoordinator background child");
  490. }
  491. function readToolCardState(blockTestId: string): Promise<{
  492. ariaExpanded: string | null;
  493. hasCollapsibleContent: boolean;
  494. text: string;
  495. }> {
  496. return browser.execute(
  497. (toolBlockTestId, summaryTriggerPrefix) => {
  498. const block = document.querySelector<HTMLElement>(`[data-testid="${toolBlockTestId}"]`);
  499. const summary = block?.querySelector<HTMLElement>(
  500. `[data-testid^="${summaryTriggerPrefix}-"]`,
  501. );
  502. return {
  503. ariaExpanded: summary?.getAttribute("aria-expanded") ?? null,
  504. hasCollapsibleContent: Boolean(block?.querySelector('[data-slot="collapsible-content"]')),
  505. text: block?.innerText.replace(/\u00a0/g, " ").trim() ?? "",
  506. };
  507. },
  508. blockTestId,
  509. TID_TOOL_SUMMARY_TRIGGER,
  510. );
  511. }
  512. async function waitForFirstRequestIndex(
  513. query: Parameters<typeof waitForUpstreamRequest>[0],
  514. afterIndex: number,
  515. timeoutMsg: string,
  516. ) {
  517. await waitForUpstreamRequest(query, timeoutMsg, 60_000, { afterIndex });
  518. const index = await findFirstUpstreamRequestIndex(query, { afterIndex });
  519. if (index === null) {
  520. throw new Error(`${timeoutMsg}; request disappeared after wait`);
  521. }
  522. return index;
  523. }
  524. async function getRequestMarkerPosition(requestIndex: number, marker: string) {
  525. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  526. if (!capturePath) {
  527. throw new Error("E2E_PROVIDER_CAPTURE_PATH is required for request ordering assertions");
  528. }
  529. const artifact = JSON.parse(await readFile(capturePath, "utf-8")) as E2ENetworkCaptureArtifact;
  530. const body = JSON.stringify(artifact.records[requestIndex]?.requestJson ?? "");
  531. const contentIndex = body.indexOf(marker);
  532. if (contentIndex < 0) {
  533. throw new Error(`request ${requestIndex} does not contain marker ${marker}`);
  534. }
  535. return { requestIndex, contentIndex };
  536. }
  537. function compareRequestPositions(
  538. left: { requestIndex: number; contentIndex: number },
  539. right: { requestIndex: number; contentIndex: number },
  540. ) {
  541. return left.requestIndex === right.requestIndex
  542. ? left.contentIndex - right.contentIndex
  543. : left.requestIndex - right.requestIndex;
  544. }
  545. async function selectCurrentV4FullAccessMode() {
  546. // Bug 根因:legacy session store 的 workspace configOptions 已不是 V4 草稿模式的
  547. // 权威投影,且 DOM element.click() 无法稳定确认 Radix Select。复用 V4 helper 的
  548. // 键盘确认链路,并以 composer 的 data-mode 投影判断配置命令已经回流。
  549. await switchV4Mode("yolo");
  550. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "yolo", {
  551. timeout: 30_000,
  552. timeoutMsg: "BG25 当前 v4 composer 没有收敛到 Full access mode",
  553. });
  554. }
  555. async function stopIfBusy() {
  556. const snapshot = await getV4PaneSnapshot();
  557. if (!snapshot.canStop) return;
  558. await clickV4Stop();
  559. await waitForV4Pane(
  560. (state) => !state.canStop,
  561. "RespondToCoordinator E2E teardown stop 后没有回到 idle",
  562. 60_000,
  563. );
  564. }