conversation-session-goal-session-model.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. assertUpstreamThoughtLevelCapture,
  5. waitForUpstreamNetworkRequestStarted,
  6. } from "#e2e/helpers/upstream-capture";
  7. import {
  8. UPSTREAM_MODEL,
  9. UPSTREAM_SECONDARY_MODEL,
  10. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  11. UPSTREAM_THOUGHT_LEVEL,
  12. ensureUpstreamModelForE2E,
  13. getUpstreamThoughtLevelLabels,
  14. getSelectedUpstreamModelLabel,
  15. getSelectedUpstreamThoughtLevelLabel,
  16. selectUpstreamModelById,
  17. selectUpstreamThoughtLevelValue,
  18. } from "#e2e/helpers/upstream-provider";
  19. import {
  20. E2E_REPLY_TOKEN,
  21. clickV4Stop,
  22. getV4ConversationState,
  23. prepareV4ConversationE2E,
  24. selectV4TaskById,
  25. sendV4Prompt,
  26. startNewV4Draft,
  27. waitForV4AssistantMessageContaining,
  28. waitForV4ConversationState,
  29. waitForV4UserMessageContaining,
  30. } from "#e2e/helpers/v4-conversation";
  31. import {
  32. buildReadonlyToolPrompt,
  33. ensureReadonlyToolFixtureFile,
  34. } from "#e2e/helpers/conversation-session-readonly-tool";
  35. import { waitForV4MessageAction } from "#e2e/helpers/v4-message-actions";
  36. import { waitForUpstreamRequestContaining } from "#e2e/helpers/conversation-session-network";
  37. import { waitForQueueContaining, waitForQueueCount } from "#e2e/helpers/conversation-session-queue";
  38. describe("会话区 Goal Session/Model E2E", () => {
  39. afterEach(async () => {
  40. await stopIfStreaming();
  41. });
  42. after(async () => {
  43. await browser.electron.restoreAllMocks();
  44. await clearAppData();
  45. });
  46. it("queued goal 消费时保留入队时的模型和思考深度", async function () {
  47. this.timeout(190000);
  48. await prepareV4ConversationE2E();
  49. // 原因:旧聚合 helper 隐式创建只读文件;V4 用例显式保留工具执行前置。
  50. await ensureReadonlyToolFixtureFile();
  51. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  52. await startNewV4Draft();
  53. const runId = Date.now();
  54. await selectUpstreamModelById(UPSTREAM_MODEL);
  55. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  56. await expectToolbarConfig(UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  57. const runningMarker = `E2E_MODEL_CONFIG_AUTO_DRAIN_RUNNING_GOAL_${runId}`;
  58. // 修复原因:queued goal 必须在首轮仍处于 running 时入队;短回复会让自动消费触发卡在 terminal 边界。
  59. const runningPrompt = [
  60. `${runningMarker}: Respond directly in chat.`,
  61. "Do not use tools or run commands.",
  62. `Write 40 short numbered lines to keep this E2E turn running, then finish with "${E2E_REPLY_TOKEN}".`,
  63. ].join(" ");
  64. await sendV4Prompt(runningPrompt);
  65. await waitForV4UserMessageContaining(runningMarker);
  66. await waitForV4ConversationState(
  67. (snapshot) => snapshot.state === "streaming",
  68. "queued goal model 基础消息没有进入 streaming",
  69. 30000,
  70. );
  71. const queuedGoalMarker = `E2E_GOAL_MODEL_QUEUE_LATEST_${runId}`;
  72. await sendV4Prompt(goalPrompt(queuedGoalMarker));
  73. await waitForQueueContaining(queuedGoalMarker);
  74. await waitForQueueCount(1);
  75. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  76. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  77. await expectToolbarConfig(UPSTREAM_SECONDARY_MODEL, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  78. // 队列和模型选择均就绪后才让首轮完成,避免模型快慢改变用例动作顺序。
  79. await releaseControlledReplay("goal-session-running");
  80. await waitForV4ConversationState(
  81. (snapshot) => snapshot.targetObjective?.includes(queuedGoalMarker) === true,
  82. "queued goal 没有进入运行态",
  83. 30000,
  84. );
  85. await waitForV4AssistantMessageContaining("E2E_GOAL_MODEL_QUEUED_RUNNING");
  86. const goalRecord = await waitForUpstreamNetworkRequestStarted(queuedGoalMarker);
  87. // 受控流已显示正文但尚未结束,capture 的最终 statusCode 此时仍为空。
  88. // Queue 的完整 Submission 在入队时固定,之后 Composer 选择不改写它。
  89. expect(goalRecord.method).toBe("POST");
  90. expect(goalRecord.replay?.fixtureId).toBe("goal-session-queued");
  91. expect(goalRecord.requestJson).toMatchObject({ model: UPSTREAM_MODEL });
  92. assertUpstreamThoughtLevelCapture(goalRecord, UPSTREAM_THOUGHT_LEVEL);
  93. await waitForV4ConversationState(
  94. (snapshot) => snapshot.targetObjective?.includes(queuedGoalMarker) === true,
  95. "queued goal 消费后没有设置目标",
  96. 30000,
  97. );
  98. });
  99. it("goal running 和 queued goal 在 session 切换时不串状态", async function () {
  100. this.timeout(190000);
  101. await prepareV4ConversationE2E({ resetDraftBeforeProvider: true });
  102. await ensureReadonlyToolFixtureFile();
  103. const runId = Date.now();
  104. await selectUpstreamModelById(UPSTREAM_MODEL);
  105. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  106. await expectToolbarConfig(UPSTREAM_MODEL, UPSTREAM_THOUGHT_LEVEL);
  107. const seedPrompt = buildReadonlyToolPrompt(`E2E_GOAL_SESSION_SEED_${runId}`);
  108. await sendV4Prompt(seedPrompt);
  109. await waitForUpstreamRequestContaining(`E2E_GOAL_SESSION_SEED_${runId}`);
  110. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  111. const sessionAIdle = await waitForV4ConversationState(
  112. (snapshot) => snapshot.state === "idle" && Boolean(snapshot.taskId),
  113. "goal session seed 没有完成",
  114. 90000,
  115. );
  116. const sessionA = sessionAIdle.taskId;
  117. if (!sessionA) {
  118. throw new Error("goal session seed 缺少 taskId");
  119. }
  120. const activeGoalMarker = `E2E_GOAL_SESSION_ACTIVE_${runId}`;
  121. await sendV4Prompt(goalPrompt(activeGoalMarker));
  122. await waitForUpstreamRequestContaining(activeGoalMarker);
  123. await waitForV4ConversationState(
  124. (snapshot) =>
  125. snapshot.taskId === sessionA &&
  126. snapshot.state === "streaming" &&
  127. snapshot.targetObjective?.includes(activeGoalMarker) === true,
  128. "session A goal 没有进入 running",
  129. 30000,
  130. );
  131. // E06 允许从更早的稳定 assistant fork,当前 Goal 运行不禁用历史操作。
  132. const forkButton = await waitForV4MessageAction("assistant", "fork", E2E_REPLY_TOKEN);
  133. expect(forkButton.exists).toBe(true);
  134. expect(forkButton.ariaDisabled || forkButton.disabled).toBe(false);
  135. const queuedGoalMarker = `E2E_GOAL_SESSION_QUEUED_${runId}`;
  136. await sendV4Prompt(goalPrompt(queuedGoalMarker));
  137. let queue = await waitForQueueCount(1);
  138. expect(queue[0]?.kind).toBe("goal");
  139. expect(queue[0]?.content).toContain(queuedGoalMarker);
  140. await startNewV4Draft();
  141. await waitForV4ConversationState(
  142. (snapshot) =>
  143. snapshot.taskId !== sessionA &&
  144. snapshot.queueCount === 0 &&
  145. snapshot.targetObjective === null &&
  146. snapshot.targetStatus === null,
  147. "新建 session 不应继承 A 的 goal 或 queue",
  148. 30000,
  149. );
  150. // 修复原因:本 case 只验证 goal/queue 不跨 session 泄漏。
  151. // 新草稿继承哪组模型/思考深度由 model-config 的 I03 覆盖,不能绑定上一条 running session A 的配置。
  152. const draftSnapshot = await getV4ConversationState();
  153. expect(draftSnapshot.targetObjective).toBeNull();
  154. expect(draftSnapshot.queueCount).toBe(0);
  155. await selectV4TaskById(sessionA);
  156. await waitForV4ConversationState(
  157. (snapshot) =>
  158. snapshot.taskId === sessionA &&
  159. snapshot.targetObjective?.includes(activeGoalMarker) === true &&
  160. snapshot.queueCount === 1,
  161. "切回 session A 后没有恢复 goal 与 queued goal",
  162. 30000,
  163. );
  164. queue = await waitForQueueCount(1);
  165. expect(queue[0]?.kind).toBe("goal");
  166. expect(queue[0]?.content).toContain(queuedGoalMarker);
  167. });
  168. });
  169. function goalPrompt(marker: string) {
  170. return `/goal ${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  171. }
  172. async function expectToolbarConfig(model: string, thoughtLevel: string) {
  173. const modelLabel = await getSelectedUpstreamModelLabel();
  174. const acceptedModelValues = [`e2e-upstream/${model}`, `custom:e2e-upstream:${model}`, model];
  175. if (modelLabel.currentValue) {
  176. expect(acceptedModelValues).toContain(modelLabel.currentValue);
  177. } else {
  178. expect(Object.values(modelLabel).join("\n")).toContain(model);
  179. }
  180. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  181. const labelCandidates = Object.values(thoughtLabel)
  182. .map((value) => value.replace(/\s+/g, " ").trim().toLowerCase())
  183. .filter(Boolean);
  184. const expectedLabels = [thoughtLevel, ...getUpstreamThoughtLevelLabels(thoughtLevel)].map(
  185. (label) => label.replace(/\s+/g, " ").trim().toLowerCase(),
  186. );
  187. const matchedThoughtLevel = labelCandidates.some((label) => expectedLabels.includes(label));
  188. if (!matchedThoughtLevel) {
  189. throw new Error(
  190. `思考深度未精确匹配 ${thoughtLevel}: ${JSON.stringify({
  191. expectedLabels,
  192. thoughtLabel,
  193. })}`,
  194. );
  195. }
  196. }
  197. async function stopIfStreaming() {
  198. const snapshot = await waitForV4ConversationState(() => true, "读取 chat 状态失败", 5000);
  199. if (snapshot.state !== "streaming") {
  200. return;
  201. }
  202. await clickV4Stop().catch(() => undefined);
  203. await waitForV4ConversationState(
  204. (candidate) => candidate.state !== "streaming",
  205. "afterEach stop 后没有退出 streaming",
  206. 30000,
  207. ).catch(() => undefined);
  208. }