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

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