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

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