conversation-session-compact-auto-actions.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. E2E_REPLY_TOKEN,
  5. waitForV4CompactMarker,
  6. waitForV4AssistantMessageContaining,
  7. assertVisibleV4UserMessagesNotContaining,
  8. clickV4Send,
  9. prepareV4ConversationE2E,
  10. sendV4Prompt,
  11. setV4ComposerText,
  12. waitForV4ConversationState,
  13. waitForV4ComposerText,
  14. waitForV4UserMessageContaining,
  15. } from "#e2e/helpers/v4-conversation";
  16. import {
  17. countUpstreamRequestsContainingAll,
  18. expectNoUpstreamRequestForTextWithin,
  19. waitForUpstreamRequest,
  20. waitForUpstreamRequestContaining,
  21. } from "#e2e/helpers/conversation-session-network";
  22. import { getQueueItems, type QueueItemSnapshot } from "#e2e/helpers/conversation-session-queue";
  23. const ACTION_MARKER = "E2E_AUTO_ACTIONS";
  24. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  25. describe("会话区 Auto Compact Actions E2E", () => {
  26. after(async () => {
  27. await browser.electron.restoreAllMocks();
  28. await clearAppData();
  29. });
  30. it("auto compacting 中普通文本和 /goal 入队,重复 /compact 被拒绝;compact 完成后继续消费 queue", async function () {
  31. this.timeout(240000);
  32. await prepareV4ConversationE2E();
  33. const runId = Date.now();
  34. // 当前 planner 保留最近一轮;仅一轮历史没有可压缩分区,必须先完成两轮。
  35. for (const phase of ["WARMUP", "READY"]) {
  36. const seedPrompt = `${ACTION_MARKER}_CONTEXT_SEED_${phase}_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  37. await sendV4Prompt(seedPrompt);
  38. await waitForV4ComposerText("", "auto compact seed 发送后输入框未清空");
  39. await waitForUpstreamRequestContaining(`${ACTION_MARKER}_CONTEXT_SEED_${phase}_${runId}`);
  40. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  41. await waitForV4ConversationState(
  42. (state) => state.state === "idle" && state.queueCount === 0,
  43. "auto compact seed 未完成",
  44. 90000,
  45. );
  46. }
  47. const compactRequestsBefore = await countUpstreamRequestsContainingAll([
  48. COMPACT_REQUEST_SENTINEL,
  49. ACTION_MARKER,
  50. ]);
  51. const pendingPrompt = `${ACTION_MARKER}_PENDING_TEXT_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  52. await sendV4Prompt(pendingPrompt);
  53. await waitForV4ComposerText("", "auto compact actions pending 文本发送后输入框没有清空");
  54. await waitForV4UserMessageContaining(pendingPrompt);
  55. // 以真实摘要请求建立 compacting 窗口,流保持挂起;不能因 skipped 提前返回并漏测动作。
  56. await browser.waitUntil(
  57. async () =>
  58. (await countUpstreamRequestsContainingAll([COMPACT_REQUEST_SENTINEL, ACTION_MARKER])) ===
  59. compactRequestsBefore + 1,
  60. {
  61. timeout: 30000,
  62. timeoutMsg: "auto compact actions 没有捕获到自动 compact 请求",
  63. },
  64. );
  65. await setV4ComposerText("/compact");
  66. await clickV4Send();
  67. await waitForV4ComposerText("", "auto compacting 中重复 /compact 后输入框没有清空");
  68. await expectNoUpstreamRequestForTextWithin(COMPACT_REQUEST_SENTINEL, 800);
  69. expect(
  70. await countUpstreamRequestsContainingAll([COMPACT_REQUEST_SENTINEL, ACTION_MARKER]),
  71. ).toBe(compactRequestsBefore + 1);
  72. await assertVisibleV4UserMessagesNotContaining("/compact");
  73. const queuedTextPrompt = `${ACTION_MARKER}_QUEUED_TEXT_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  74. await sendV4Prompt(queuedTextPrompt);
  75. await waitForV4ComposerText("", "auto compacting 中普通文本发送后输入框没有清空");
  76. const queuedText = await waitForQueueItemContaining(`${ACTION_MARKER}_QUEUED_TEXT_${runId}`);
  77. expect(queuedText.kind).toBe("text");
  78. const queuedGoalMarker = `${ACTION_MARKER}_QUEUED_GOAL_${runId}`;
  79. const queuedGoalPrompt = `/goal ${queuedGoalMarker}`;
  80. await sendV4Prompt(queuedGoalPrompt);
  81. await waitForV4ComposerText("", "auto compacting 中 /goal 发送后输入框没有清空");
  82. const queuedGoal = await waitForQueueItemContaining(`${ACTION_MARKER}_QUEUED_GOAL_${runId}`);
  83. expect(queuedGoal.kind).toBe("goal");
  84. expect(
  85. await countUpstreamRequestsContainingAll([COMPACT_REQUEST_SENTINEL, ACTION_MARKER]),
  86. ).toBe(compactRequestsBefore + 1);
  87. // auto compact 成功后应先继续原 pendingAction,再继续消费 compacting 期间追加的 future queue;
  88. // 录制轨迹不能停在 queue 仍挂起的中间态,否则人工 review 看不到正常收尾。
  89. await releaseControlledReplay("upstream-auto-actions-compact-slow");
  90. await waitForV4CompactMarker({ origin: "auto", status: "success" });
  91. await waitForMainModelRequest(`${ACTION_MARKER}_PENDING_TEXT_${runId}`);
  92. await waitForMainModelRequest(`${ACTION_MARKER}_QUEUED_TEXT_${runId}`);
  93. await waitForV4UserMessageContaining(queuedTextPrompt);
  94. await waitForV4ConversationState(
  95. (snapshot) =>
  96. snapshot.state === "idle" &&
  97. snapshot.queueCount === 0 &&
  98. (snapshot.targetObjective ?? "").includes(queuedGoalMarker),
  99. "auto compact 完成后没有继续消费 pendingAction 和 compacting 期间追加的 text/goal queue",
  100. 120000,
  101. );
  102. });
  103. });
  104. async function waitForMainModelRequest(marker: string) {
  105. await waitForUpstreamRequest(
  106. {
  107. excludes: [COMPACT_REQUEST_SENTINEL],
  108. includes: [marker],
  109. },
  110. `auto compact 后没有继续发起主模型请求: ${marker}`,
  111. 90000,
  112. );
  113. }
  114. async function waitForQueueItemContaining(text: string): Promise<QueueItemSnapshot> {
  115. let latest: QueueItemSnapshot[] = [];
  116. await browser.waitUntil(
  117. async () => {
  118. latest = await getQueueItems();
  119. return latest.some((item) => item.content.includes(text));
  120. },
  121. {
  122. timeout: 30000,
  123. timeoutMsg: `队列中没有出现 ${text}; latest=${JSON.stringify(latest)}`,
  124. },
  125. );
  126. const item = latest.find((candidate) => candidate.content.includes(text));
  127. if (!item) {
  128. throw new Error(`队列项 ${text} 在等待后仍不存在`);
  129. }
  130. return item;
  131. }