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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // 官方文档插件已拆分;旧 document-skills:judge 不在当前目录,不能验证模型覆盖。
  30. const AGENT_ID = "plugin:documents@zcode-plugins-official:visual-judge";
  31. const PARENT = "E2E_PLUGIN_OVERRIDE_COLD_PARENT";
  32. const CHILD = "E2E_PLUGIN_OVERRIDE_COLD_CHILD";
  33. const RESULT = "E2E_PLUGIN_OVERRIDE_COLD_RESULT";
  34. const FINAL = "plugin-override-cold-done";
  35. describe("PLM-LC-020 插件 Subagent 覆盖冷启动请求", () => {
  36. after(async () => {
  37. await browser.electron.restoreAllMocks();
  38. await clearAppData();
  39. });
  40. it("完整重启后 judge 使用已保存模型/high,父 continuation 保持原模型/max", async () => {
  41. requirePluginManualReview();
  42. await prepareV4ConversationE2E({ skipProvider: true });
  43. const selection = {
  44. providerId: UPSTREAM_ALTERNATE_PROVIDER_ID,
  45. modelId: UPSTREAM_ALTERNATE_MODEL,
  46. options: { reasoningLevel: "high" },
  47. };
  48. // SE-04 已验证 UI 保存;这里仅构造同格式测试副本,冷启动后不再调用设置服务。
  49. await writeFile(
  50. join(resolveE2EStorageRoot(), "v2", "agents-state.json"),
  51. JSON.stringify({
  52. ...(await readAgentsState()),
  53. pluginAgentModelSelectionOverrides: { [AGENT_ID]: selection },
  54. }),
  55. );
  56. await restartIntoWorkspacePreservingProfile();
  57. await prepareV4ConversationE2E({ skipProvider: true });
  58. // Recent 是下次创建偏好,不会覆盖现有草稿的 high;实际选择父配置后再验证 child 隔离。
  59. await switchV4Model(UPSTREAM_PROVIDER_ID, UPSTREAM_MODEL, "max");
  60. await sendV4PromptAndWaitAccepted(
  61. `${PARENT}: Use Agent with subagent_type documents:visual-judge, then report its result.`,
  62. PARENT,
  63. "插件覆盖冷启动消息未被接受",
  64. );
  65. await waitForV4AssistantMessageContaining(FINAL);
  66. await waitForV4ConversationState(
  67. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  68. "插件执行后未回到 idle",
  69. 30_000,
  70. );
  71. expect((await waitForToolCallBlockByToolName("Agent")).status).toBe("completed");
  72. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH;
  73. if (!capturePath) throw new Error("缺少插件请求 capture 文件路径");
  74. const capture = JSON.parse(await readFile(capturePath, "utf8")) as E2ENetworkCaptureArtifact;
  75. // 不能只搜整份请求:父 continuation 也包含 child prompt;按最新 user block 的类型区分。
  76. const child = capture.records.find((record) => latestUserContains(record, "text", CHILD));
  77. const parent = capture.records.find((record) =>
  78. latestUserContains(record, "tool_result", RESULT),
  79. );
  80. if (!child || !parent) throw new Error("缺少独立 child 或父 tool_result continuation 请求");
  81. assertUpstreamRequestCapture(child, { expectedText: CHILD, model: UPSTREAM_ALTERNATE_MODEL });
  82. assertUpstreamThoughtLevelCapture(child, "high");
  83. assertUpstreamRequestCapture(parent, { expectedText: RESULT, model: UPSTREAM_MODEL });
  84. assertUpstreamThoughtLevelCapture(parent, "max");
  85. expect((await readAgentsState()).pluginAgentModelSelectionOverrides).toEqual({
  86. [AGENT_ID]: selection,
  87. });
  88. });
  89. });
  90. function latestUserContains(
  91. record: E2ENetworkCaptureRecord,
  92. type: string,
  93. marker: string,
  94. ): boolean {
  95. if (record.method !== "POST" || record.status !== "complete") return false;
  96. const request = record.requestJson as { messages?: Array<{ role?: string; content?: unknown }> };
  97. const content = request?.messages?.findLast((message) => message.role === "user")?.content;
  98. return (
  99. Array.isArray(content) &&
  100. content.some((part) => part?.type === type && JSON.stringify(part).includes(marker))
  101. );
  102. }