conversation-session-plugin-resident-freeze-cold-resume.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, DEFAULT_WORKSPACE, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  4. import {
  5. getPluginReferenceCatalog,
  6. restartPluginLifecycleApp,
  7. } from "#e2e/helpers/plugin-management-lifecycle";
  8. import {
  9. captureArtifactAdvertisesToolName,
  10. findUpstreamRequestContaining,
  11. PLUGIN_MCP_PLUGIN_ID,
  12. PLUGIN_MCP_TOOL_NAME,
  13. } from "#e2e/helpers/plugin-mcp-skill";
  14. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  15. import {
  16. prepareV4ConversationE2E,
  17. selectV4TaskById,
  18. sendV4Prompt,
  19. waitForV4AssistantMessageContaining,
  20. getV4PaneSnapshot,
  21. } from "#e2e/helpers/v4-conversation";
  22. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  23. import { waitForUpstreamRequestContaining } from "#e2e/helpers/conversation-session-network";
  24. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  25. const RESIDENT_MARKER = "E2E_WPL_008_RESIDENT_ENABLED";
  26. const NEW_SESSION_MARKER = "E2E_WPL_008_NEW_SESSION_DISABLED";
  27. const COLD_RESUME_MARKER = "E2E_WPL_008_COLD_RESUME_DISABLED";
  28. const RESIDENT_REPLY = "wpl-008-resident-ok";
  29. const NEW_SESSION_REPLY = "wpl-008-new-session-ok";
  30. const COLD_RESUME_REPLY = "wpl-008-cold-resume-ok";
  31. describe("WPL-008 Workspace Plugin resident freeze and cold resume E2E", () => {
  32. after(async () => {
  33. await browser.electron.restoreAllMocks();
  34. await clearAppData();
  35. });
  36. it("freezes resident S1, reloads new S2, and cold-resumes S1 with the new config", async function () {
  37. this.timeout(300000);
  38. await prepareV4ConversationE2E();
  39. await ensureToolCrossProductFullAccessMode();
  40. // 准备阶段已进入项目空草稿;设置权限后再新建会丢失该草稿的 full-access 选择。
  41. await sendV4Prompt(
  42. `${RESIDENT_MARKER}: Call ${PLUGIN_MCP_TOOL_NAME} exactly once, then reply with exactly ${RESIDENT_REPLY}.`,
  43. );
  44. await assertPluginToolAdvertisementAndCompletion(RESIDENT_MARKER, RESIDENT_REPLY);
  45. const residentSessionId = (await getV4PaneSnapshot()).sessionId;
  46. expect(residentSessionId).not.toBeNull();
  47. if (!residentSessionId) return;
  48. const residentCatalog = await getPluginReferenceCatalog({ sessionId: residentSessionId });
  49. expect(residentCatalog.authority).toBe("session");
  50. expectPluginCatalogState(residentCatalog.plugins, true);
  51. await disableWorkspacePlugin();
  52. const workspaceCatalog = await getPluginReferenceCatalog();
  53. expect(workspaceCatalog.authority).toBe("workspace");
  54. expectPluginCatalogState(workspaceCatalog.plugins, false);
  55. // 同一个 resident App 的 Session-owned catalog 不热加载新配置。
  56. const stillResidentCatalog = await getPluginReferenceCatalog({ sessionId: residentSessionId });
  57. expectPluginCatalogState(stillResidentCatalog.plugins, true);
  58. await startDraftFromProject(DEFAULT_WORKSPACE);
  59. await sendV4Prompt(
  60. `${NEW_SESSION_MARKER}: Do not call tools; reply with exactly ${NEW_SESSION_REPLY}.`,
  61. );
  62. await assertPluginToolNotAdvertised(NEW_SESSION_MARKER);
  63. await waitForV4AssistantMessageContaining(NEW_SESSION_REPLY);
  64. // 完整重启 Desktop/Host/CLI 后再打开 S1,证明 cold resume 重建 App 时读取当前 Workspace 配置。
  65. await restartPluginLifecycleApp();
  66. await selectV4TaskById(residentSessionId);
  67. await browser.waitUntil(
  68. async () => (await getV4PaneSnapshot()).sessionId === residentSessionId,
  69. { timeout: 60000, timeoutMsg: `S1 没有在 cold resume 后恢复: ${residentSessionId}` },
  70. );
  71. const coldCatalog = await waitForSessionPluginReferenceCatalog(residentSessionId);
  72. expect(coldCatalog.authority).toBe("session");
  73. expectPluginCatalogState(coldCatalog.plugins, false);
  74. await sendV4Prompt(
  75. `${COLD_RESUME_MARKER}: Do not call tools; reply with exactly ${COLD_RESUME_REPLY}.`,
  76. );
  77. await assertPluginToolNotAdvertised(COLD_RESUME_MARKER);
  78. await waitForV4AssistantMessageContaining(COLD_RESUME_REPLY);
  79. });
  80. });
  81. async function disableWorkspacePlugin(): Promise<void> {
  82. const { workspace } = getE2EAppDataPaths();
  83. const configPath = join(workspace, ".zcode", "config.json");
  84. const config = JSON.parse(await readFile(configPath, "utf8")) as {
  85. plugins?: { enabledPlugins?: Record<string, boolean> };
  86. };
  87. await writeFile(
  88. configPath,
  89. `${JSON.stringify(
  90. {
  91. ...config,
  92. plugins: {
  93. ...config.plugins,
  94. enabledPlugins: {
  95. ...config.plugins?.enabledPlugins,
  96. [PLUGIN_MCP_PLUGIN_ID]: false,
  97. },
  98. },
  99. },
  100. null,
  101. 2,
  102. )}\n`,
  103. "utf8",
  104. );
  105. }
  106. async function assertPluginToolAdvertisementAndCompletion(
  107. marker: string,
  108. expectedReply: string,
  109. ): Promise<void> {
  110. await waitForUpstreamRequestContaining(marker, 60000);
  111. const requestRecord = await findUpstreamRequestContaining(marker, 60000, {
  112. advertisedToolName: PLUGIN_MCP_TOOL_NAME,
  113. });
  114. expect(requestRecord).not.toBeNull();
  115. if (!requestRecord) return;
  116. expect(captureArtifactAdvertisesToolName(requestRecord.requestJson, PLUGIN_MCP_TOOL_NAME)).toBe(
  117. true,
  118. );
  119. // 驻留和冷恢复用例都必须能观察已折叠的工具记录。
  120. await waitForToolCallBlockByToolName(PLUGIN_MCP_TOOL_NAME, 60000);
  121. await waitForV4AssistantMessageContaining(expectedReply);
  122. }
  123. function expectPluginCatalogState(
  124. plugins: Awaited<ReturnType<typeof getPluginReferenceCatalog>>["plugins"],
  125. enabled: boolean,
  126. ): void {
  127. const plugin = plugins.find((entry) => entry.pluginId === PLUGIN_MCP_PLUGIN_ID);
  128. expect(plugin).toBeDefined();
  129. expect(plugin?.enabled).toBe(enabled);
  130. if (!enabled) {
  131. expect(plugin?.mcpServerNames).toEqual([]);
  132. }
  133. }
  134. async function waitForSessionPluginReferenceCatalog(
  135. sessionId: string,
  136. ): Promise<Awaited<ReturnType<typeof getPluginReferenceCatalog>>> {
  137. let catalog: Awaited<ReturnType<typeof getPluginReferenceCatalog>> | undefined;
  138. await browser.waitUntil(
  139. async () => {
  140. try {
  141. catalog = await getPluginReferenceCatalog({ sessionId });
  142. return true;
  143. } catch (error) {
  144. // cold resume 时 Renderer 会先恢复选中态,CLI App 随后才完成 session/resume;
  145. // 只把这个明确的暂态当作未就绪,其他协议错误立即暴露。
  146. if (
  147. error instanceof Error &&
  148. error.message.includes(`Session is not active: ${sessionId}`)
  149. ) {
  150. return false;
  151. }
  152. throw error;
  153. }
  154. },
  155. {
  156. interval: 250,
  157. timeout: 60000,
  158. timeoutMsg: `S1 cold resume 后 Plugin catalog 没有就绪: ${sessionId}`,
  159. },
  160. );
  161. if (!catalog) throw new Error(`S1 cold resume 后缺少 Plugin catalog: ${sessionId}`);
  162. return catalog;
  163. }
  164. async function assertPluginToolNotAdvertised(marker: string): Promise<void> {
  165. await waitForUpstreamRequestContaining(marker, 60000);
  166. const requestRecord = await findUpstreamRequestContaining(marker, 60000);
  167. expect(requestRecord).not.toBeNull();
  168. if (!requestRecord) return;
  169. expect(captureArtifactAdvertisesToolName(requestRecord.requestJson, PLUGIN_MCP_TOOL_NAME)).toBe(
  170. false,
  171. );
  172. }