conversation-session-model-switch-auto-compact.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  3. import {
  4. assertUpstreamRequestCapture,
  5. assertUpstreamThoughtLevelCapture,
  6. waitForUpstreamNetworkCapture,
  7. } from "#e2e/helpers/upstream-capture";
  8. import {
  9. countUpstreamRequests,
  10. getUpstreamRequestEvidence,
  11. waitForUpstreamRequest,
  12. } from "#e2e/helpers/conversation-session-network";
  13. import {
  14. UPSTREAM_MODEL,
  15. UPSTREAM_SECONDARY_MODEL,
  16. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  17. UPSTREAM_THOUGHT_LEVEL,
  18. ensureUpstreamModelForE2E,
  19. selectUpstreamModelById,
  20. selectUpstreamThoughtLevelValue,
  21. } from "#e2e/helpers/upstream-provider";
  22. import {
  23. E2E_REPLY_TOKEN,
  24. clickV4Stop,
  25. getV4PaneSnapshot,
  26. getV4CompactMarkers,
  27. prepareV4ConversationE2E,
  28. sendV4Prompt,
  29. startNewV4Draft,
  30. waitForV4CompactMarker,
  31. waitForV4ComposerText,
  32. waitForV4ConversationState,
  33. } from "#e2e/helpers/v4-conversation";
  34. const ACTION = "E2E_AUTO_ACTIONS";
  35. const COMPACT = "CRITICAL: Respond with TEXT ONLY";
  36. const GATE = "upstream-auto-actions-compact-slow";
  37. describe("会话区下次发送模型与 Auto Compact E2E", () => {
  38. before(async () => {
  39. await prepareV4ConversationE2E();
  40. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  41. });
  42. beforeEach(async () => {
  43. await startNewV4Draft();
  44. await selectConfig(UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  45. });
  46. afterEach(async () => {
  47. if ((await getV4PaneSnapshot()).canStop) await clickV4Stop();
  48. });
  49. after(async () => {
  50. await browser.electron.restoreAllMocks();
  51. await clearAppData();
  52. });
  53. it("N05: 选择小窗口模型不触发压缩,下一条普通输入采用新模型", async function () {
  54. this.timeout(150000);
  55. const id = Date.now();
  56. await sendAndComplete(`${ACTION}_MODEL_SWITCH_GUARD_CONTEXT_${id}`);
  57. await selectConfig(UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  58. await expectNoMenuCompact();
  59. const marker = `${ACTION}_PENDING_TEXT_NEXT_${id}`;
  60. await sendAndComplete(marker);
  61. await assertModel(marker, UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  62. });
  63. it("N05: 发消息前改回原模型,撤销待发送选择且不触发压缩", async function () {
  64. this.timeout(150000);
  65. const id = Date.now();
  66. await sendAndComplete(`${ACTION}_MODEL_SWITCH_CANCEL_CONTEXT_${id}`);
  67. await selectConfig(UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  68. await expectNoMenuCompact();
  69. await selectConfig(UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  70. await expectNoMenuCompact();
  71. const marker = `${ACTION}_PENDING_TEXT_CANCEL_${id}`;
  72. await sendAndComplete(marker);
  73. await assertModel(marker, UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  74. });
  75. it("N06: 自动压缩中改选模型,已提交输入保留原模型,下一输入采用新模型", async function () {
  76. this.timeout(240000);
  77. const id = Date.now();
  78. // 当前 planner 保留最近一轮;两轮历史才有可压缩分区,不能将 noop 算作覆盖。
  79. for (const phase of ["WARMUP", "READY"]) {
  80. await sendAndComplete(`${ACTION}_CONTEXT_SEED_${phase}_${id}`);
  81. }
  82. const pending = `${ACTION}_PENDING_TEXT_ADMITTED_${id}`;
  83. await sendV4Prompt(prompt(pending));
  84. await waitForV4ComposerText("", "自动压缩前的输入没有被接受");
  85. await waitForUpstreamRequest({ includes: [COMPACT, ACTION] }, "没有产生自动压缩请求");
  86. await waitForV4CompactMarker({ origin: "auto", status: "running" });
  87. // 由响应门闩保持真实在途窗口,菜单操作完成后才释放,不靠延时竞争。
  88. await selectConfig(UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  89. expect(await countUpstreamRequests({ includes: [COMPACT, ACTION] })).toBe(1);
  90. await releaseControlledReplay(GATE);
  91. const compact = await waitForUpstreamNetworkCapture(COMPACT);
  92. assertUpstreamRequestCapture(compact, { expectedText: COMPACT, model: UPSTREAM_MODEL });
  93. expect(compact.requestJson).toMatchObject({ stream: true });
  94. expect(compact.responseHeaders["content-type"]).toContain("text/event-stream");
  95. await waitForV4CompactMarker({ origin: "auto", status: "success" });
  96. await assertModel(pending, UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  97. await waitForIdle();
  98. expect(await countUpstreamRequests({ includes: [COMPACT, ACTION] })).toBe(1);
  99. const next = `${ACTION}_PENDING_TEXT_AFTER_${id}`;
  100. await sendV4Prompt(prompt(next));
  101. await waitForV4ComposerText("", "切换后的新输入未被接受");
  102. // 新输入切到另一个小窗口模型后可再次触发正常自动压缩;它必须使用新输入的模型。
  103. // 旧 fixture 每次都挂起,若只释放第一条摘要,会把合法的第二次压缩误报为卡死。
  104. await browser.waitUntil(
  105. async () => (await countUpstreamRequests({ includes: [COMPACT, ACTION] })) === 2,
  106. {
  107. timeout: 30000,
  108. timeoutMsg: "新输入未触发目标小窗口模型的自动压缩",
  109. },
  110. );
  111. const summaries = await getUpstreamRequestEvidence({ includes: [COMPACT, ACTION] });
  112. expect(summaries[1]?.requestJson).toMatchObject({ model: UPSTREAM_SECONDARY_MODEL });
  113. await releaseControlledReplay(GATE);
  114. await assertModel(next, UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  115. await waitForIdle();
  116. expect(await countUpstreamRequests({ includes: [COMPACT, ACTION] })).toBe(2);
  117. });
  118. });
  119. function prompt(marker: string) {
  120. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  121. }
  122. async function selectConfig(model: string, thought: string) {
  123. await selectUpstreamModelById(model);
  124. await selectUpstreamThoughtLevelValue(thought);
  125. }
  126. async function waitForIdle() {
  127. await waitForV4ConversationState(
  128. (state) => state.state === "idle" && state.queueCount === 0,
  129. "输入完成后没有回到 idle",
  130. 90000,
  131. );
  132. }
  133. async function sendAndComplete(marker: string) {
  134. await sendV4Prompt(prompt(marker));
  135. await waitForV4ComposerText("", "普通输入没有被接受");
  136. await waitForUpstreamNetworkCapture(marker);
  137. await waitForIdle();
  138. }
  139. async function assertModel(marker: string, model: string, thought: string) {
  140. await waitForUpstreamRequest(
  141. { lastUserMessageIncludes: [marker], excludes: [COMPACT] },
  142. "没有实际主模型请求",
  143. );
  144. const record = await waitForUpstreamNetworkCapture(marker);
  145. assertUpstreamRequestCapture(record, { expectedText: marker, model });
  146. assertUpstreamThoughtLevelCapture(record, thought);
  147. }
  148. async function expectNoMenuCompact() {
  149. // 当前契约只设置下一输入意图,不存在选择模型即确认压缩的入口。
  150. expect(await getV4CompactMarkers()).toHaveLength(0);
  151. expect(await countUpstreamRequests({ includes: [COMPACT, ACTION] })).toBe(0);
  152. expect(
  153. await browser.execute(
  154. () =>
  155. document.body.innerText.includes("需要压缩上下文后再切换模型") ||
  156. document.body.innerText.includes("Compress context before switching models"),
  157. ),
  158. ).toBe(false);
  159. }