conversation-session-sse-stall-diagnostic.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import {
  3. getUpstreamRequestEvidence,
  4. getLatestUpstreamToolResultByToolCallId,
  5. } from "#e2e/helpers/conversation-session-network";
  6. import {
  7. E2E_READONLY_TOOL_FILE_CONTENT,
  8. ensureReadonlyToolFixtureFile,
  9. } from "#e2e/helpers/conversation-session-readonly-tool";
  10. import { listToolCallBlocks } from "#e2e/helpers/conversation-session-tool";
  11. import {
  12. clickV4Stop,
  13. getV4ConversationState,
  14. getV4PaneSnapshot,
  15. prepareV4ConversationE2E,
  16. sendV4Prompt,
  17. startNewV4Draft,
  18. waitForV4AssistantMessageContaining,
  19. waitForV4Pane,
  20. waitForV4TimelineContaining,
  21. } from "#e2e/helpers/v4-conversation";
  22. const TEST_TIMEOUT_MS = 180_000;
  23. const OUTCOME_TIMEOUT_MS = 15_000;
  24. const IDLE_TIMEOUT_LOWER_BOUND_MS = 700;
  25. const LATE_SENTINEL_OFFSET_MS = 5_000;
  26. interface StallScenario {
  27. cutoffMarker?: string;
  28. finishMarker: string;
  29. firstFixtureId: string;
  30. id: string;
  31. marker: string;
  32. partialHistoryMustBeDiscarded?: string;
  33. recovery: "required" | "diagnostic";
  34. secondFixtureIds: string[];
  35. stallRequestIndex?: number;
  36. toolCallId?: string;
  37. toolExecution: "forbidden" | "none" | "required";
  38. }
  39. const SCENARIOS: StallScenario[] = [
  40. {
  41. finishMarker: "E2E_SSE_STALL_PRELUDE_DONE",
  42. firstFixtureId: "sse-stall-prelude-stalled",
  43. id: "SSE-ST-01 message_start",
  44. marker: "E2E_SSE_STALL_PRELUDE",
  45. recovery: "required",
  46. secondFixtureIds: ["sse-stall-prelude-recovered"],
  47. toolExecution: "none",
  48. },
  49. {
  50. cutoffMarker: "E2E_SSE_STALL_THINKING_PARTIAL",
  51. finishMarker: "E2E_SSE_STALL_THINKING_DONE",
  52. firstFixtureId: "sse-stall-thinking-stalled",
  53. id: "SSE-ST-02 thinking_delta",
  54. marker: "E2E_SSE_STALL_THINKING",
  55. partialHistoryMustBeDiscarded: "E2E_SSE_STALL_THINKING_PARTIAL",
  56. recovery: "required",
  57. secondFixtureIds: ["sse-stall-thinking-recovered"],
  58. toolExecution: "none",
  59. },
  60. {
  61. cutoffMarker: "E2E_SSE_STALL_TEXT_PARTIAL",
  62. finishMarker: "E2E_SSE_STALL_TEXT_DONE",
  63. firstFixtureId: "sse-stall-text-stalled",
  64. id: "SSE-ST-03 text_delta",
  65. marker: "E2E_SSE_STALL_TEXT",
  66. recovery: "diagnostic",
  67. secondFixtureIds: ["sse-stall-text-recovered"],
  68. toolExecution: "none",
  69. },
  70. {
  71. finishMarker: "E2E_SSE_STALL_TOOL_INPUT_DONE",
  72. firstFixtureId: "sse-stall-tool-input-stalled",
  73. id: "SSE-ST-04 incomplete tool input",
  74. marker: "E2E_SSE_STALL_TOOL_INPUT",
  75. partialHistoryMustBeDiscarded: "E2E_SSE_STALL_TOOL_INPUT_PARTIAL",
  76. recovery: "diagnostic",
  77. secondFixtureIds: ["sse-stall-tool-input-recovered"],
  78. toolCallId: "toolu_e2e_sse_stall_partial_read",
  79. toolExecution: "forbidden",
  80. },
  81. {
  82. finishMarker: "E2E_SSE_STALL_TOOL_COMMITTED_DONE",
  83. firstFixtureId: "sse-stall-tool-committed-stalled",
  84. id: "SSE-ST-05 committed tool block",
  85. marker: "E2E_SSE_STALL_TOOL_COMMITTED",
  86. recovery: "required",
  87. secondFixtureIds: ["sse-stall-tool-committed-continuation"],
  88. toolCallId: "toolu_e2e_sse_stall_committed_read",
  89. toolExecution: "required",
  90. },
  91. {
  92. finishMarker: "E2E_SSE_STALL_POST_TOOL_DONE",
  93. firstFixtureId: "sse-stall-post-tool-continuation-stalled",
  94. id: "SSE-ST-06 after successful tool execution",
  95. marker: "E2E_SSE_STALL_POST_TOOL",
  96. recovery: "required",
  97. secondFixtureIds: ["sse-stall-post-tool-continuation-recovered"],
  98. stallRequestIndex: 1,
  99. toolCallId: "toolu_e2e_sse_stall_post_tool_read",
  100. toolExecution: "required",
  101. },
  102. ];
  103. describe("SSE event 中途停发的真实 Desktop E2E diagnostic", () => {
  104. before(async function () {
  105. this.timeout(TEST_TIMEOUT_MS);
  106. await ensureReadonlyToolFixtureFile();
  107. await prepareV4ConversationE2E();
  108. });
  109. beforeEach(async function () {
  110. this.timeout(TEST_TIMEOUT_MS);
  111. await startNewV4Draft();
  112. });
  113. afterEach(async () => {
  114. const pane = await getV4PaneSnapshot().catch(() => null);
  115. if (!pane?.canStop) return;
  116. await clickV4Stop().catch(() => undefined);
  117. await waitForV4Pane(
  118. (snapshot) => !snapshot.canStop,
  119. "SSE stall diagnostic afterEach stop 没有退出 streaming",
  120. 30_000,
  121. ).catch(() => undefined);
  122. });
  123. after(async () => {
  124. await browser.electron.restoreAllMocks();
  125. await clearAppData();
  126. });
  127. for (const scenario of SCENARIOS) {
  128. it(`${scenario.id}: stall 后采集恢复、UI 与工具副作用`, async function () {
  129. this.timeout(TEST_TIMEOUT_MS);
  130. await runStallScenario(scenario);
  131. });
  132. }
  133. });
  134. async function runStallScenario(scenario: StallScenario): Promise<void> {
  135. await sendV4Prompt(`${scenario.marker}: observe the provider stream without adding assumptions.`);
  136. const stallRequestIndex = scenario.stallRequestIndex ?? 0;
  137. await browser.waitUntil(
  138. async () => {
  139. const evidence = await getUpstreamRequestEvidence({ includes: [scenario.marker] });
  140. return evidence[stallRequestIndex]?.fixtureId === scenario.firstFixtureId;
  141. },
  142. {
  143. timeout: 30_000,
  144. timeoutMsg: `${scenario.id} 目标 stall 请求没有进入 replay server`,
  145. },
  146. );
  147. if (scenario.cutoffMarker === "E2E_SSE_STALL_TEXT_PARTIAL") {
  148. // 正文 cutoff 必须先真实投影到 live UI,不能只凭 fixture 声称越过 visible boundary。
  149. await waitForV4TimelineContaining(scenario.cutoffMarker, 2_000);
  150. } else {
  151. // 其余 cutoff 不强制要求当前 UI 显示 reasoning/tool partial,但给事件进入 runtime 留出窗口。
  152. await browser.pause(180);
  153. }
  154. const stallPane = await getV4PaneSnapshot();
  155. const stallRuntime = await getV4ConversationState();
  156. expect(stallPane.canStop).toBe(true);
  157. expect(stallRuntime.state).toBe("streaming");
  158. const outcomeElapsedMs = await waitForOutcome(scenario);
  159. expect(outcomeElapsedMs).toBeGreaterThanOrEqual(IDLE_TIMEOUT_LOWER_BOUND_MS);
  160. // fixture 的下一个 ping 在 5s;必须在它到达前由 idle timeout 触发恢复或 terminal,
  161. // 否则测到的是 delayed EOF,而不是“无 event”保护。
  162. expect(outcomeElapsedMs).toBeLessThan(LATE_SENTINEL_OFFSET_MS);
  163. let evidence = await getUpstreamRequestEvidence({ includes: [scenario.marker] });
  164. expect(evidence[stallRequestIndex]?.fixtureId).toBe(scenario.firstFixtureId);
  165. expect(evidence.length).toBeLessThanOrEqual(stallRequestIndex + 2);
  166. const second = evidence[stallRequestIndex + 1];
  167. if (scenario.recovery === "required") {
  168. expect(second).toBeDefined();
  169. }
  170. if (second) {
  171. expect(scenario.secondFixtureIds).toContain(second.fixtureId);
  172. expect(second.statusCode).toBe(200);
  173. await waitForV4AssistantMessageContaining(scenario.finishMarker, 30_000);
  174. }
  175. await waitForV4Pane(
  176. (snapshot) => !snapshot.canStop,
  177. `${scenario.id} 没有在 idle timeout/recovery 后退出 streaming`,
  178. 30_000,
  179. );
  180. evidence = await getUpstreamRequestEvidence({ includes: [scenario.marker] });
  181. expect(evidence.length).toBe(second ? stallRequestIndex + 2 : stallRequestIndex + 1);
  182. const recoveryRequestText = second ? JSON.stringify(second.requestJson) : "";
  183. if (second && scenario.partialHistoryMustBeDiscarded) {
  184. expect(recoveryRequestText).not.toContain(scenario.partialHistoryMustBeDiscarded);
  185. }
  186. const toolBlocks = scenario.toolCallId
  187. ? (await listToolCallBlocks()).filter((block) => block.toolCallId === scenario.toolCallId)
  188. : [];
  189. expect(toolBlocks.length).toBeLessThanOrEqual(1);
  190. const toolResult = scenario.toolCallId
  191. ? await getLatestUpstreamToolResultByToolCallId(scenario.toolCallId)
  192. : null;
  193. if (scenario.toolExecution === "forbidden") {
  194. expect(toolResult).toBeNull();
  195. }
  196. if (scenario.toolExecution === "required") {
  197. expect(toolResult?.isError).toBe(false);
  198. expect(toolResult?.content).toContain(E2E_READONLY_TOOL_FILE_CONTENT);
  199. }
  200. if (second?.fixtureId === "sse-stall-tool-committed-continuation") {
  201. expect(recoveryRequestText).toContain("toolu_e2e_sse_stall_committed_read");
  202. }
  203. const settledPane = await getV4PaneSnapshot();
  204. const settledRuntime = await getV4ConversationState();
  205. const result = {
  206. case: scenario.id,
  207. cutoffVisibleAfterRecovery:
  208. scenario.cutoffMarker === undefined
  209. ? null
  210. : settledPane.timelineText.includes(scenario.cutoffMarker),
  211. finishVisible: settledPane.timelineText.includes(scenario.finishMarker),
  212. outcomeElapsedMs,
  213. requests: evidence.map((record) => ({
  214. fixtureId: record.fixtureId,
  215. responseEventCount: record.responseEventCount,
  216. status: record.status,
  217. })),
  218. settled: summarizeRuntime(settledPane, settledRuntime),
  219. stall: summarizeRuntime(stallPane, stallRuntime),
  220. toolBlockCount: toolBlocks.length,
  221. toolResult: toolResult
  222. ? {
  223. contentIncludesReadonlyFixture: toolResult.content.includes(
  224. E2E_READONLY_TOOL_FILE_CONTENT,
  225. ),
  226. isError: toolResult.isError,
  227. }
  228. : null,
  229. };
  230. console.info(`[sse-stall-diagnostic] ${JSON.stringify(result)}`);
  231. }
  232. async function waitForOutcome(scenario: StallScenario): Promise<number> {
  233. const startedAtMs = Date.now();
  234. const recoveryRequestCount = (scenario.stallRequestIndex ?? 0) + 2;
  235. await browser.waitUntil(
  236. async () => {
  237. const evidence = await getUpstreamRequestEvidence({ includes: [scenario.marker] });
  238. const recoveryRequest = evidence[recoveryRequestCount - 1];
  239. // 修复原因:请求先建档,fixture 与响应稍后回填;只等数量会读到 null。
  240. // 等本次恢复响应完成抓包,不要求被 watchdog 中止的旧请求变成 complete。
  241. if (recoveryRequest) return recoveryRequest.status !== "pending";
  242. const pane = await getV4PaneSnapshot();
  243. return !pane.canStop;
  244. },
  245. {
  246. interval: 100,
  247. timeout: OUTCOME_TIMEOUT_MS,
  248. timeoutMsg: `${scenario.id} 在 ${OUTCOME_TIMEOUT_MS}ms 内既未恢复也未进入 terminal`,
  249. },
  250. );
  251. return Date.now() - startedAtMs;
  252. }
  253. function summarizeRuntime(
  254. pane: Awaited<ReturnType<typeof getV4PaneSnapshot>>,
  255. runtime: Awaited<ReturnType<typeof getV4ConversationState>>,
  256. ) {
  257. return {
  258. activeInput: Boolean(runtime.activeInputId),
  259. canStop: pane.canStop,
  260. rowCount: pane.rowCount,
  261. runtimeStatus: runtime.runtimeStatus,
  262. state: runtime.state,
  263. timelineTail: pane.timelineText.slice(-240),
  264. };
  265. }