conversation-session-v4-auto-drain-running-fork.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // PV4-03/PV4-08 pending:Q0 running 时先把 Q1/Q2 入 CLI FIFO;Q1 auto-drain
  2. // 形成稳定 A1,Q2 随后仍 running 时精确 fork A1。
  3. import { TID_V4_FORK, TID_V4_ROW } from "@zcode/shared/test-ids";
  4. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  5. import { clearAppData } from "#e2e/helpers/desktop-app";
  6. import { waitForUpstreamNetworkRequestStarted } from "#e2e/helpers/upstream-capture";
  7. import {
  8. clickV4Stop,
  9. getV4PaneSnapshot,
  10. getV4QueueItems,
  11. prepareV4ConversationE2E,
  12. selectV4TaskById,
  13. sendV4Prompt,
  14. waitForV4Pane,
  15. waitForV4QueueCount,
  16. } from "#e2e/helpers/v4-conversation";
  17. const Q0_MARKER = "E2E_V4_AUTODRAIN_FORK_Q0";
  18. const A0_MARKER = "E2E_V4_AUTODRAIN_FORK_A0";
  19. const Q1_MARKER = "E2E_V4_AUTODRAIN_FORK_Q1";
  20. const A1_MARKER = "E2E_V4_AUTODRAIN_FORK_A1";
  21. const Q2_MARKER = "E2E_V4_AUTODRAIN_FORK_Q2";
  22. const A2_PARTIAL_MARKER = "E2E_V4_AUTODRAIN_FORK_A2_PARTIAL";
  23. describe("PV4-08 auto-drain stable assistant running fork", () => {
  24. let runningParentSessionId: string | null = null;
  25. afterEach(async () => {
  26. if (runningParentSessionId) {
  27. await selectV4TaskById(runningParentSessionId).catch(() => undefined);
  28. }
  29. await stopCurrentTurnIfNeeded();
  30. runningParentSessionId = null;
  31. });
  32. after(async () => {
  33. await browser.electron.restoreAllMocks();
  34. await clearAppData();
  35. });
  36. it("Q1 auto-drain 成 A1 后,Q2 running 时 fork A1,父继续且 child 前缀固定", async function () {
  37. this.timeout(180000);
  38. await prepareV4ConversationE2E();
  39. const runId = Date.now();
  40. const q0 = `${Q0_MARKER}_${runId}: hold the first turn while Q1 and Q2 queue.`;
  41. const q1 = `${Q1_MARKER}_${runId}: auto-drain this input and reply with ${A1_MARKER}.`;
  42. const q2 = `${Q2_MARKER}_${runId}: keep streaming while A1 is forked.`;
  43. await sendV4Prompt(q0);
  44. const parent = await waitForV4Pane(
  45. (snapshot) =>
  46. snapshot.canStop &&
  47. snapshot.sessionId !== null &&
  48. snapshot.sessionId !== "draft" &&
  49. snapshot.timelineText.includes(A0_MARKER),
  50. "Q0 没有进入 controlled running 窗口",
  51. 30000,
  52. );
  53. runningParentSessionId = parent.sessionId;
  54. await sendV4Prompt(q1);
  55. await waitForV4QueueCount(1, 30000);
  56. await sendV4Prompt(q2);
  57. await waitForV4QueueCount(2, 30000);
  58. expect((await getV4QueueItems()).map((item) => item.text).join("\n")).toContain(Q1_MARKER);
  59. expect((await getV4QueueItems()).map((item) => item.text).join("\n")).toContain(Q2_MARKER);
  60. // 队列已接收两条输入才释放 Q0;不依赖固定的 30 秒窗口。
  61. await releaseControlledReplay("v4-auto-drain-fork-q0-controlled");
  62. // Q0 完成后 Q1、Q2 必须按 CLI admission FIFO 自动 drain。断言 A1 已稳定、
  63. // Q2 已从 queue 晋升为 active turn,才能证明 fork target 不是普通 direct-send 历史。
  64. await waitForUpstreamNetworkRequestStarted(Q2_MARKER);
  65. const q2Running = await waitForV4Pane(
  66. (snapshot) =>
  67. snapshot.canStop &&
  68. snapshot.timelineText.includes(Q1_MARKER) &&
  69. snapshot.timelineText.includes(A1_MARKER) &&
  70. snapshot.timelineText.includes(Q2_MARKER) &&
  71. // 请求开始早于 Renderer 收到首个 delta;必须等实际部分回复出现再 fork。
  72. snapshot.timelineText.includes(A2_PARTIAL_MARKER),
  73. "Q1/A1 没有完成 auto-drain,或 Q2 没有继续进入 running",
  74. 60000,
  75. );
  76. expect(await getV4QueueItems()).toHaveLength(0);
  77. expect(q2Running.timelineText).toContain(A2_PARTIAL_MARKER);
  78. await waitForForkActionOnRow(A1_MARKER);
  79. expect(await clickForkActionOnRow(A1_MARKER)).toBe(true);
  80. const child = await waitForV4Pane(
  81. (snapshot) =>
  82. snapshot.sessionId !== null &&
  83. snapshot.sessionId !== "draft" &&
  84. snapshot.sessionId !== runningParentSessionId &&
  85. snapshot.timelineText.includes(A1_MARKER),
  86. "Q2 running 时 fork A1 没有创建 child",
  87. 30000,
  88. );
  89. // child 固定在 A1 的 stable logical boundary:Q0/A0、Q1/A1 全部存在;
  90. // Q2 user/partial、父 active work 与未来状态一律不能越过边界。
  91. expect(child.timelineText).toContain(Q0_MARKER);
  92. expect(child.timelineText).toContain(A0_MARKER);
  93. expect(child.timelineText).toContain(Q1_MARKER);
  94. expect(child.timelineText).toContain(A1_MARKER);
  95. expect(child.timelineText).not.toContain(Q2_MARKER);
  96. expect(child.timelineText).not.toContain(A2_PARTIAL_MARKER);
  97. expect(await getV4QueueItems()).toHaveLength(0);
  98. if (!runningParentSessionId) throw new Error("fork 前没有记录 parent sessionId");
  99. await selectV4TaskById(runningParentSessionId);
  100. const parentStillRunning = await waitForV4Pane(
  101. (snapshot) =>
  102. snapshot.sessionId === runningParentSessionId &&
  103. snapshot.canStop &&
  104. snapshot.timelineText.includes(Q2_MARKER),
  105. "fork A1 后父 Q2 没有继续运行",
  106. 15000,
  107. );
  108. expect(parentStillRunning.timelineText).toContain(A2_PARTIAL_MARKER);
  109. // UI 每次点击都会生成新 commandId,无法构造“同一 commandId 重试”。该幂等边界
  110. // 必须由 protocol/native probe 注入固定 commandId;本 pending case 不用双击冒充覆盖。
  111. await clickV4Stop();
  112. await waitForV4Pane(
  113. (snapshot) => !snapshot.canStop,
  114. "auto-drain running fork 收尾 stop 没有完成",
  115. 30000,
  116. );
  117. });
  118. });
  119. async function waitForForkActionOnRow(marker: string) {
  120. await browser.waitUntil(async () => hasForkActionOnRow(marker), {
  121. timeout: 30000,
  122. timeoutMsg: `稳定 assistant row 没有权威 canFork action: ${marker}`,
  123. });
  124. }
  125. async function hasForkActionOnRow(marker: string) {
  126. return browser.execute(
  127. (expectedMarker, rowPrefix, forkPrefix) => {
  128. const rows = Array.from(
  129. document.querySelectorAll<HTMLElement>(`[data-testid^="${rowPrefix}-"]`),
  130. );
  131. return rows.some((candidate) => {
  132. if (!candidate.innerText.includes(expectedMarker)) return false;
  133. const rowId = candidate.dataset.rowId;
  134. return Boolean(rowId && document.querySelector(`[data-testid="${forkPrefix}-${rowId}"]`));
  135. });
  136. },
  137. marker,
  138. TID_V4_ROW,
  139. TID_V4_FORK,
  140. );
  141. }
  142. async function clickForkActionOnRow(marker: string) {
  143. return browser.execute(
  144. (expectedMarker, rowPrefix, forkPrefix) => {
  145. const rows = Array.from(
  146. document.querySelectorAll<HTMLElement>(`[data-testid^="${rowPrefix}-"]`),
  147. );
  148. const row = rows.find((candidate) => {
  149. if (!candidate.innerText.includes(expectedMarker)) return false;
  150. const rowId = candidate.dataset.rowId;
  151. return Boolean(rowId && document.querySelector(`[data-testid="${forkPrefix}-${rowId}"]`));
  152. });
  153. const rowId = row?.dataset.rowId;
  154. const button = rowId
  155. ? document.querySelector<HTMLButtonElement>(`[data-testid="${forkPrefix}-${rowId}"]`)
  156. : null;
  157. button?.click();
  158. return Boolean(button);
  159. },
  160. marker,
  161. TID_V4_ROW,
  162. TID_V4_FORK,
  163. );
  164. }
  165. async function stopCurrentTurnIfNeeded() {
  166. const snapshot = await getV4PaneSnapshot().catch(() => null);
  167. if (!snapshot?.canStop) return;
  168. await clickV4Stop();
  169. await waitForV4Pane(
  170. (next) => !next.canStop,
  171. "auto-drain running fork cleanup 没有退出 streaming",
  172. 30000,
  173. );
  174. }