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

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