conversation-session-v4-goal-background-queue.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // BG34:active goal 仅剩 background Agent 时输入直发;notification verifier 完成后自动消费 future queue。
  2. import { TID_V4_COMPOSER } from "@zcode/shared/test-ids";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import {
  5. countUpstreamRequests,
  6. findFirstUpstreamRequestIndex,
  7. getUpstreamRequestRecordCount,
  8. waitForUpstreamRequest,
  9. } from "#e2e/helpers/conversation-session-network";
  10. import {
  11. getV4GoalProjection,
  12. getV4PaneSnapshot,
  13. getV4QueueItems,
  14. prepareV4ConversationE2E,
  15. sendV4Prompt,
  16. switchV4Mode,
  17. waitForV4Pane,
  18. waitForV4QueueCount,
  19. waitForV4TimelineContaining,
  20. } from "#e2e/helpers/v4-conversation";
  21. const E2E_TIMEOUT_MS = 180000;
  22. describe("BG34 v4 goal background queue", () => {
  23. after(async () => {
  24. await browser.electron.restoreAllMocks();
  25. await clearAppData();
  26. });
  27. it("background-only 输入直发,verifier pass 后自动消费 future queue", async function () {
  28. this.timeout(E2E_TIMEOUT_MS);
  29. await prepareV4ConversationE2E();
  30. await switchV4Mode("yolo");
  31. await sendV4Prompt("E2E_BACKGROUND_GOAL_SEED establish the v4 session");
  32. await waitForV4TimelineContaining("background-goal-seed-ok", 45000);
  33. await waitForV4Pane(
  34. (snapshot) => snapshot.sessionId !== null && snapshot.sessionId !== "draft",
  35. "BG34 seed 后没有建立 session",
  36. 45000,
  37. );
  38. const goal =
  39. '/goal E2E_BACKGROUND_GOAL: Finish only after consuming the background agent result and then reply with "upstream-e2e-ok".';
  40. const directPrompt =
  41. "E2E_BACKGROUND_GOAL_DIRECT_PROMPT: Send this immediately while the goal background Agent is still running.";
  42. const verifierQueuePrompt =
  43. "E2E_BACKGROUND_GOAL_VERIFIER_QUEUE: Drain this only after the verifier marks the goal complete.";
  44. const requestsBeforeGoal = await getUpstreamRequestRecordCount();
  45. await sendV4Prompt(goal);
  46. await waitForV4TimelineContaining("background-goal-agent-launched-ok", 60000);
  47. await waitForV4Pane(
  48. (snapshot) => !snapshot.canStop,
  49. "background Agent launch 后 main turn 没有先回到 idle",
  50. 45000,
  51. );
  52. expect((await getV4GoalProjection())?.status).toBe("active");
  53. expect(await getComposerInputRouting()).toBe("startNow");
  54. const launchIndex = await waitForFirstRequestIndex(
  55. {
  56. includes: ["E2E_BACKGROUND_GOAL", "Async agent launched successfully"],
  57. },
  58. requestsBeforeGoal - 1,
  59. "goal background Agent launch tool result 没有进入父模型请求",
  60. );
  61. await sendV4Prompt(directPrompt);
  62. expect(await getV4QueueItems()).toHaveLength(0);
  63. const directIndex = await waitForFirstRequestIndex(
  64. {
  65. includes: ["E2E_BACKGROUND_GOAL_DIRECT_PROMPT"],
  66. lastUserMessageIncludes: ["E2E_BACKGROUND_GOAL_DIRECT_PROMPT"],
  67. },
  68. launchIndex,
  69. "active goal 只剩 background Agent 时普通消息没有直接进入 provider request",
  70. );
  71. await waitForV4TimelineContaining("background-goal-direct-prompt-ok", 45000);
  72. await waitForV4Pane(
  73. (snapshot) => !snapshot.canStop,
  74. "background-only 普通消息完成后 main turn 没有回到 idle",
  75. 45000,
  76. );
  77. const notificationIndex = await waitForFirstRequestIndex(
  78. {
  79. includes: ["<task-notification>", "E2E_BACKGROUND_GOAL_CHILD_RESULT"],
  80. lastUserMessageIncludes: ["<task-notification>"],
  81. },
  82. directIndex,
  83. "goal background Agent completion notification 没有进入父模型请求",
  84. );
  85. await waitForV4TimelineContaining("E2E_BACKGROUND_GOAL_NOTIFICATION_CONSUMED", 60000);
  86. const verifierIndex = await waitForFirstRequestIndex(
  87. {
  88. includes: ["E2E_BACKGROUND_GOAL", "Verify whether the active session goal"],
  89. },
  90. notificationIndex,
  91. "background notification 后没有触发 goal verifier",
  92. );
  93. await sendV4Prompt(verifierQueuePrompt);
  94. await waitForV4QueueCount(1, 30000);
  95. expect((await getV4QueueItems())[0]?.text).toContain("E2E_BACKGROUND_GOAL_VERIFIER_QUEUE");
  96. expect(
  97. await countUpstreamRequests(
  98. {
  99. includes: ["E2E_BACKGROUND_GOAL_VERIFIER_QUEUE"],
  100. lastUserMessageIncludes: ["E2E_BACKGROUND_GOAL_VERIFIER_QUEUE"],
  101. },
  102. { afterIndex: verifierIndex },
  103. ),
  104. ).toBe(0);
  105. // UI status panel 把 durable target complete 投影成 verified;CLI 回调仍由
  106. // TargetChangedPayload.target.status === "complete" 触发。
  107. await browser.waitUntil(async () => (await getV4GoalProjection())?.status === "verified", {
  108. timeout: 90000,
  109. timeoutMsg: "background goal verifier 没有把 target 标记为 complete",
  110. });
  111. const drainedIndex = await waitForFirstRequestIndex(
  112. {
  113. includes: ["E2E_BACKGROUND_GOAL_VERIFIER_QUEUE"],
  114. lastUserMessageIncludes: ["E2E_BACKGROUND_GOAL_VERIFIER_QUEUE"],
  115. },
  116. verifierIndex,
  117. "TargetChanged(complete) 后没有自动消费 future queue",
  118. );
  119. expect(drainedIndex).toBeGreaterThan(verifierIndex);
  120. await waitForV4TimelineContaining("background-goal-verifier-queue-drained-ok", 60000);
  121. await waitForV4QueueCount(0, 30000);
  122. expect((await getV4GoalProjection())?.status).toBe("verified");
  123. expect((await getV4PaneSnapshot()).timelineText).toContain(
  124. "E2E_BACKGROUND_GOAL_VERIFIER_QUEUE",
  125. );
  126. });
  127. });
  128. async function getComposerInputRouting() {
  129. return browser.execute((composerTestId) => {
  130. return (
  131. document
  132. .querySelector<HTMLElement>(`[data-testid="${composerTestId}"]`)
  133. ?.getAttribute("data-input-routing") ?? null
  134. );
  135. }, TID_V4_COMPOSER);
  136. }
  137. async function waitForFirstRequestIndex(
  138. query: Parameters<typeof waitForUpstreamRequest>[0],
  139. afterIndex: number,
  140. timeoutMsg: string,
  141. ) {
  142. await waitForUpstreamRequest(query, timeoutMsg, 60000, { afterIndex });
  143. const index = await findFirstUpstreamRequestIndex(query, { afterIndex });
  144. if (index === null) {
  145. throw new Error(`${timeoutMsg}; request disappeared after wait`);
  146. }
  147. return index;
  148. }