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

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