plugin-subagent-override-cold.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, readAgentsState } from "#e2e/helpers/desktop-app";
  4. import {
  5. assertUpstreamRequestCapture,
  6. assertUpstreamThoughtLevelCapture,
  7. } from "#e2e/helpers/upstream-capture";
  8. import {
  9. UPSTREAM_MODEL,
  10. UPSTREAM_PROVIDER_ID,
  11. UPSTREAM_ALTERNATE_MODEL,
  12. UPSTREAM_ALTERNATE_PROVIDER_ID,
  13. } from "#e2e/helpers/upstream-provider";
  14. import { resolveE2EStorageRoot } from "#e2e/helpers/e2e-runtime-paths";
  15. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart";
  16. import type {
  17. E2ENetworkCaptureArtifact,
  18. E2ENetworkCaptureRecord,
  19. } from "#e2e/helpers/network-capture-proxy";
  20. import { requirePluginManualReview } from "#e2e/helpers/plugin-management-lifecycle";
  21. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  22. import {
  23. prepareV4ConversationE2E,
  24. switchV4Model,
  25. sendV4PromptAndWaitAccepted,
  26. waitForV4AssistantMessageContaining,
  27. waitForV4ConversationState,
  28. } from "#e2e/helpers/v4-conversation";
  29. // 内置文档插件已不再提供子代理;使用 wdio 种子安装的夹具插件 e2e-subagent-refresh:reviewer
  30. // (profile 不声明 model,覆盖能直接生效)。
  31. const AGENT_NAME = "e2e-subagent-refresh:reviewer";
  32. const AGENT_ID = "plugin:e2e-subagent-refresh@e2e-fixture:reviewer";
  33. const PARENT = "E2E_PLUGIN_OVERRIDE_COLD_PARENT";
  34. const CHILD = "E2E_PLUGIN_OVERRIDE_COLD_CHILD";
  35. const RESULT = "E2E_PLUGIN_OVERRIDE_COLD_RESULT";
  36. const FINAL = "plugin-override-cold-done";
  37. describe("PLM-LC-020 插件 Subagent 覆盖冷启动请求", () => {
  38. after(async () => {
  39. await browser.electron.restoreAllMocks();
  40. await clearAppData();
  41. });
  42. it("完整重启后插件 subagent 使用已保存模型/high,父 continuation 保持原模型/max", async () => {
  43. requirePluginManualReview();
  44. await prepareV4ConversationE2E({ skipProvider: true });
  45. const selection = {
  46. providerId: UPSTREAM_ALTERNATE_PROVIDER_ID,
  47. modelId: UPSTREAM_ALTERNATE_MODEL,
  48. options: { reasoningLevel: "high" },
  49. };
  50. // SE-04 已验证 UI 保存;这里仅构造同格式测试副本,冷启动后不再调用设置服务。
  51. await writeFile(
  52. join(resolveE2EStorageRoot(), "v2", "agents-state.json"),
  53. JSON.stringify({
  54. ...(await readAgentsState()),
  55. pluginAgentModelSelectionOverrides: { [AGENT_ID]: selection },
  56. }),
  57. );
  58. await restartIntoWorkspacePreservingProfile();
  59. await prepareV4ConversationE2E({ skipProvider: true });
  60. // Recent 是下次创建偏好,不会覆盖现有草稿的 high;实际选择父配置后再验证 child 隔离。
  61. await switchV4Model(UPSTREAM_PROVIDER_ID, UPSTREAM_MODEL, "max");
  62. await sendV4PromptAndWaitAccepted(
  63. `${PARENT}: Use Agent with subagent_type ${AGENT_NAME}, then report its result.`,
  64. PARENT,
  65. "插件覆盖冷启动消息未被接受",
  66. );
  67. await waitForV4AssistantMessageContaining(FINAL);
  68. await waitForV4ConversationState(
  69. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  70. "插件执行后未回到 idle",
  71. 30_000,
  72. );
  73. expect((await waitForToolCallBlockByToolName("Agent")).status).toBe("completed");
  74. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH;
  75. if (!capturePath) throw new Error("缺少插件请求 capture 文件路径");
  76. const capture = JSON.parse(await readFile(capturePath, "utf8")) as E2ENetworkCaptureArtifact;
  77. // 不能只搜整份请求:父 continuation 也包含 child prompt;按最新 user block 的类型区分。
  78. const child = capture.records.find((record) => latestUserContains(record, "text", CHILD));
  79. const parent = capture.records.find((record) =>
  80. latestUserContains(record, "tool_result", RESULT),
  81. );
  82. if (!child || !parent) throw new Error("缺少独立 child 或父 tool_result continuation 请求");
  83. assertUpstreamRequestCapture(child, { expectedText: CHILD, model: UPSTREAM_ALTERNATE_MODEL });
  84. assertUpstreamThoughtLevelCapture(child, "high");
  85. assertUpstreamRequestCapture(parent, { expectedText: RESULT, model: UPSTREAM_MODEL });
  86. assertUpstreamThoughtLevelCapture(parent, "max");
  87. expect((await readAgentsState()).pluginAgentModelSelectionOverrides).toEqual({
  88. [AGENT_ID]: selection,
  89. });
  90. });
  91. });
  92. function latestUserContains(
  93. record: E2ENetworkCaptureRecord,
  94. type: string,
  95. marker: string,
  96. ): boolean {
  97. if (record.method !== "POST" || record.status !== "complete") return false;
  98. const request = record.requestJson as { messages?: Array<{ role?: string; content?: unknown }> };
  99. const content = request?.messages?.findLast((message) => message.role === "user")?.content;
  100. return (
  101. Array.isArray(content) &&
  102. content.some((part) => part?.type === type && JSON.stringify(part).includes(marker))
  103. );
  104. }