conversation-session-tool-error-surface.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { clearAppData } from "../../../helpers/desktop-app.js";
  2. import {
  3. prepareConversationE2E,
  4. sendPrompt,
  5. waitForAssistantMessageContaining,
  6. waitForChatState,
  7. waitForComposerText,
  8. waitForUpstreamRequest,
  9. waitForToolCallBlockByToolCallId,
  10. waitForUserMessageContaining,
  11. } from "../../../helpers/conversation-session.js";
  12. import { getToolCallDiagnostics } from "../../../helpers/conversation-session-tool-diagnostics.js";
  13. const PARENT_MARKER = "E2E_TOOL_ERROR_SURFACE_PARENT";
  14. const STALE_CHILD_MARKER = "E2E_TOOL_ERROR_SURFACE_STALE_CHILD";
  15. const HEALTHY_CHILD_MARKER = "E2E_TOOL_ERROR_SURFACE_HEALTHY_CHILD";
  16. const HEALTHY_CHILD_DONE = "E2E_TOOL_ERROR_SURFACE_HEALTHY_OK";
  17. const PARENT_DONE = "E2E_TOOL_ERROR_SURFACE_PARENT_DONE";
  18. const STALE_AGENT_TYPE = "e2e-stale-error-reviewer";
  19. const HEALTHY_AGENT_TYPE = "e2e-healthy-error-reviewer";
  20. const STALE_PROVIDER_ID = "e2e-stale-provider";
  21. const STALE_MODEL_ID = "e2e-stale-model";
  22. const STALE_ERROR_TEXT = `Model provider is not configured: ${STALE_PROVIDER_ID}`;
  23. const GENERIC_TURN_ERROR_TEXT = "Turn execution failed";
  24. const STALE_AGENT_TOOL_CALL_ID = "toolu_e2e_tool_error_surface_stale_agent";
  25. const HEALTHY_AGENT_TOOL_CALL_ID = "toolu_e2e_tool_error_surface_healthy_agent";
  26. describe("会话区 toolcall 错误展示 E2E", () => {
  27. after(async () => {
  28. await browser.electron.restoreAllMocks();
  29. await clearAppData();
  30. });
  31. it("Z01: Agent 失败工具卡展示真实 provider 根因且不阻断同轮 sibling Agent", async function () {
  32. this.timeout(180000);
  33. await prepareConversationE2E();
  34. const runId = Date.now();
  35. const marker = `${PARENT_MARKER}_${runId}`;
  36. const prompt = [
  37. `${marker}: launch two Agent tools in the same assistant response.`,
  38. `One must use subagent_type "${STALE_AGENT_TYPE}" for ${STALE_CHILD_MARKER}.`,
  39. `The other must use subagent_type "${HEALTHY_AGENT_TYPE}" for ${HEALTHY_CHILD_MARKER}.`,
  40. `After both tool results return, reply with exactly ${PARENT_DONE}.`,
  41. ].join(" ");
  42. await sendPrompt(prompt);
  43. await waitForComposerText("", "tool error surface 首发后输入框没有清空");
  44. await waitForUserMessageContaining(marker);
  45. await waitForToolCallBlockByToolCallId(STALE_AGENT_TOOL_CALL_ID, 60000);
  46. await waitForToolCallBlockByToolCallId(HEALTHY_AGENT_TOOL_CALL_ID, 60000);
  47. const staleTool = await waitForRendererStoreToolCall(
  48. STALE_AGENT_TOOL_CALL_ID,
  49. (toolCall) =>
  50. toolCall.status === "failed" &&
  51. toolCall.error?.includes(STALE_ERROR_TEXT) === true,
  52. "没有等到 failed Agent 工具卡携带真实 provider 错误",
  53. );
  54. expect(staleTool.toolName).toBe("Agent");
  55. expect(staleTool.error).toContain(STALE_ERROR_TEXT);
  56. expect(staleTool.error).not.toContain(GENERIC_TURN_ERROR_TEXT);
  57. expect(staleTool.rawErrorMessage ?? "").toContain(STALE_ERROR_TEXT);
  58. expect(staleTool.rawErrorDetail ?? "").toContain(GENERIC_TURN_ERROR_TEXT);
  59. expect(staleTool.rawErrorDetail ?? "").toContain(STALE_PROVIDER_ID);
  60. expect(staleTool.rawErrorDetail ?? "").toContain(STALE_MODEL_ID);
  61. const healthyTool = await waitForRendererStoreToolCall(
  62. HEALTHY_AGENT_TOOL_CALL_ID,
  63. (toolCall) => toolCall.status === "completed",
  64. "同轮 healthy Agent 没有在 stale Agent 失败后继续完成",
  65. );
  66. expect(healthyTool.toolName).toBe("Agent");
  67. expect(healthyTool.error).toBeNull();
  68. await waitForToolBlockStatus(STALE_AGENT_TOOL_CALL_ID, "failed");
  69. await waitForToolBlockStatus(HEALTHY_AGENT_TOOL_CALL_ID, "completed");
  70. await waitForUpstreamRequest(
  71. {
  72. excludes: [GENERIC_TURN_ERROR_TEXT],
  73. includes: [
  74. marker,
  75. STALE_AGENT_TOOL_CALL_ID,
  76. HEALTHY_AGENT_TOOL_CALL_ID,
  77. STALE_ERROR_TEXT,
  78. HEALTHY_CHILD_DONE,
  79. ],
  80. },
  81. "父 continuation 没有同时收到真实失败 tool_result 和 sibling Agent 成功结果",
  82. 60000,
  83. );
  84. await waitForAssistantMessageContaining(PARENT_DONE);
  85. await waitForChatState(
  86. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  87. "tool error surface case 完成后没有回到 idle",
  88. 60000,
  89. );
  90. });
  91. });
  92. interface RendererStoreToolCallSnapshot {
  93. error: string | null;
  94. rawErrorDetail: string | null;
  95. rawErrorMessage: string | null;
  96. status: string | null;
  97. toolId: string | null;
  98. toolName: string | null;
  99. }
  100. async function waitForRendererStoreToolCall(
  101. toolCallId: string,
  102. predicate: (toolCall: RendererStoreToolCallSnapshot) => boolean,
  103. timeoutMsg: string,
  104. timeout = 60000,
  105. ) {
  106. let latest: RendererStoreToolCallSnapshot | null = null;
  107. await browser.waitUntil(
  108. async () => {
  109. latest = await readRendererStoreToolCall(toolCallId);
  110. return latest !== null && predicate(latest);
  111. },
  112. {
  113. timeout,
  114. timeoutMsg: `${timeoutMsg}; latest=${JSON.stringify(latest)}`,
  115. },
  116. );
  117. const result = await readRendererStoreToolCall(toolCallId);
  118. if (!result) {
  119. throw new Error(`renderer store 中没有 toolCallId=${toolCallId}`);
  120. }
  121. return result;
  122. }
  123. async function waitForToolBlockStatus(
  124. toolCallId: string,
  125. status: string,
  126. timeout = 60000,
  127. ) {
  128. let latestStatus: string | null = null;
  129. await browser.waitUntil(
  130. async () => {
  131. const diagnostics = await getToolCallDiagnostics({
  132. type: "toolCallId",
  133. value: toolCallId,
  134. });
  135. latestStatus = diagnostics.matchedBlock?.status ?? null;
  136. return latestStatus === status;
  137. },
  138. {
  139. timeout,
  140. timeoutMsg: `toolCallId=${toolCallId} 没有进入 ${status}; latestStatus=${latestStatus}`,
  141. },
  142. );
  143. }
  144. async function readRendererStoreToolCall(toolCallId: string) {
  145. const diagnostics = await getToolCallDiagnostics(null);
  146. for (const message of diagnostics.rendererStore.messages) {
  147. const match = message.toolCalls.find(
  148. (toolCall) =>
  149. toolCall.toolId === toolCallId ||
  150. toolCall.toolId?.startsWith(`${toolCallId}:`) === true,
  151. );
  152. if (match) {
  153. return match;
  154. }
  155. }
  156. return null;
  157. }