conversation-session-subagent-parallel-projection.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { TID_CHAT_SUMMARY_PANEL, TID_V4_SUBAGENT_OPEN_SIDE_PANE } from "@zcode/shared/test-ids";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  4. import {
  5. getV4ComposerBackgroundWorkCounts,
  6. getV4PaneSnapshot,
  7. prepareV4ConversationE2E,
  8. selectV4TaskById,
  9. sendV4Prompt,
  10. startNewV4Draft,
  11. waitForV4Pane,
  12. } from "#e2e/helpers/v4-conversation";
  13. const TIMEOUT_MS = 180000;
  14. const CHILD_COUNT = 7;
  15. const PARENT_MARKER = "E2E_SUBAGENT_PARALLEL_PROJECTION_PARENT";
  16. const PARENT_DONE = "E2E_SUBAGENT_PARALLEL_PROJECTION_PARENT_DONE";
  17. describe("并行 subagent V4 权威投影 manual review", () => {
  18. before(async function () {
  19. this.timeout(TIMEOUT_MS);
  20. await prepareV4ConversationE2E();
  21. });
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. });
  26. it("SAT24: 7 个并行 child 在切换任务前后都保持完整计数", async function () {
  27. this.timeout(TIMEOUT_MS);
  28. await sendV4Prompt(
  29. `${PARENT_MARKER}: launch exactly seven foreground Agent calls in one parallel tool-use response, then reply ${PARENT_DONE} after all children finish.`,
  30. );
  31. const runningParent = await waitForV4Pane(
  32. (snapshot) =>
  33. snapshot.sessionId !== null && snapshot.sessionId !== "draft" && snapshot.canStop,
  34. "SAT24 没有进入 running parent session",
  35. 60000,
  36. );
  37. if (!runningParent.sessionId || runningParent.sessionId === "draft") {
  38. throw new Error(`SAT24 缺少 parent session id: ${JSON.stringify(runningParent)}`);
  39. }
  40. // Bug 原因:旧链路在 SubagentSpawned 后另查 session/subagents;第 7 个 child
  41. // 尚未持久化时查询可能只返回 6,in-flight refresh 又会吞掉后续更新。切换任务会
  42. // 重新查询而“自愈”。这里同时约束投影的两个用户可见消费者,切换前就必须是 7。
  43. await waitForParallelSubagentCounts(CHILD_COUNT, "live-before-switch");
  44. const parentSessionId = runningParent.sessionId;
  45. await startNewV4Draft();
  46. await selectV4TaskById(parentSessionId);
  47. await waitForV4Pane(
  48. (snapshot) => snapshot.sessionId === parentSessionId,
  49. "SAT24 切回后没有恢复 parent session",
  50. 30000,
  51. );
  52. await waitForParallelSubagentCounts(CHILD_COUNT, "restored-after-switch");
  53. await waitForUpstreamRequest(
  54. {
  55. includes: [
  56. "toolu_e2e_parallel_subagent_01",
  57. "toolu_e2e_parallel_subagent_02",
  58. "toolu_e2e_parallel_subagent_03",
  59. "toolu_e2e_parallel_subagent_04",
  60. "toolu_e2e_parallel_subagent_05",
  61. "toolu_e2e_parallel_subagent_06",
  62. "toolu_e2e_parallel_subagent_07",
  63. "E2E_SUBAGENT_PARALLEL_PROJECTION_CHILD_DONE",
  64. ],
  65. },
  66. "SAT24 parent continuation 没有同时收到 7 个 child tool result",
  67. 90000,
  68. );
  69. await waitForV4Pane(
  70. (snapshot) => snapshot.sessionId === parentSessionId && !snapshot.canStop,
  71. "SAT24 的 parent session 没有在 7 个 child 完成后回到 idle",
  72. 90000,
  73. );
  74. });
  75. });
  76. interface ParallelSubagentCounts {
  77. composer: number;
  78. statusPanel: number;
  79. summaryActions: number;
  80. }
  81. async function readParallelSubagentCounts(): Promise<ParallelSubagentCounts> {
  82. const composerCounts = await getV4ComposerBackgroundWorkCounts();
  83. const surfaceCounts = await browser.execute(
  84. (panelTestId, sidePaneActionPrefix) => {
  85. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  86. return {
  87. statusPanel: Number(panel?.dataset.runningAgentCount ?? "0"),
  88. summaryActions: document.querySelectorAll(`[data-testid^="${sidePaneActionPrefix}-"]`)
  89. .length,
  90. };
  91. },
  92. TID_CHAT_SUMMARY_PANEL,
  93. TID_V4_SUBAGENT_OPEN_SIDE_PANE,
  94. );
  95. return {
  96. composer: composerCounts?.subagentCount ?? 0,
  97. ...surfaceCounts,
  98. };
  99. }
  100. async function waitForParallelSubagentCounts(
  101. expected: number,
  102. stage: "live-before-switch" | "restored-after-switch",
  103. ): Promise<void> {
  104. let latest: ParallelSubagentCounts | null = null;
  105. try {
  106. await browser.waitUntil(
  107. async () => {
  108. latest = await readParallelSubagentCounts();
  109. return (
  110. latest.composer === expected &&
  111. latest.statusPanel === expected &&
  112. latest.summaryActions === expected
  113. );
  114. },
  115. {
  116. interval: 100,
  117. timeout: 30000,
  118. timeoutMsg: `${stage} 没有完整显示 ${expected} 个并行 subagent`,
  119. },
  120. );
  121. } catch (error) {
  122. latest = await readParallelSubagentCounts();
  123. const pane = await getV4PaneSnapshot();
  124. throw new Error(
  125. `${stage} 并行 subagent 计数错误; expected=${expected}; latest=${JSON.stringify(latest)}; pane=${JSON.stringify(pane)}`,
  126. { cause: error },
  127. );
  128. }
  129. }