conversation-session-plugin-remote-identity-workspace.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { Buffer } from "node:buffer";
  2. import { posix } from "node:path";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import { getPluginList, getPluginReferenceCatalog } from "#e2e/helpers/plugin-management-lifecycle";
  5. import {
  6. connectSSHWorkspaceAndSendFirstTurn,
  7. createSSHLifecycleProvider,
  8. type SSHLifecycleProvider,
  9. } from "#e2e/helpers/ssh-remote-lifecycle";
  10. import {
  11. readDualSSHRuntimeConfig,
  12. readHostProcesses,
  13. runSSHCommand,
  14. waitForRemoteTab,
  15. type SSHConnectionConfig,
  16. } from "#e2e/helpers/ssh-remote-p0";
  17. import { prepareV4ConversationE2E, selectV4TaskById } from "#e2e/helpers/v4-conversation";
  18. const CASE_MARKER = "E2E_WPL_010_REMOTE_IDENTITY";
  19. const PLUGIN_NAME = "wpl-remote-identity";
  20. const PLUGIN_ID = `${PLUGIN_NAME}@inline`;
  21. // 服务器队列沿用 /workspace;本机非 root Docker 用户必须显式提供本轮专用目录。
  22. const ALLOWED_REMOTE_WORKSPACE = process.env.ZCODE_E2E_WPL010_WORKSPACE?.trim() || "/workspace";
  23. describe("WPL-010 remote Workspace Plugin identity/path authority E2E", () => {
  24. let providerA: SSHLifecycleProvider | null = null;
  25. let providerB: SSHLifecycleProvider | null = null;
  26. let targetA: SSHConnectionConfig | null = null;
  27. let targetB: SSHConnectionConfig | null = null;
  28. let workspacePath = "";
  29. after(async () => {
  30. await providerA?.tunnel.close().catch(() => undefined);
  31. await providerB?.tunnel.close().catch(() => undefined);
  32. if (targetA && workspacePath) {
  33. await cleanupRemoteFixture(targetA, workspacePath).catch(() => undefined);
  34. }
  35. if (targetB && workspacePath) {
  36. await cleanupRemoteFixture(targetB, workspacePath).catch(() => undefined);
  37. }
  38. await browser.electron.restoreAllMocks();
  39. await clearAppData();
  40. });
  41. it("reads the same path from each target while isolating catalog and runtime by identity", async function () {
  42. this.timeout(40 * 60_000);
  43. const dual = readDualSSHRuntimeConfig(CASE_MARKER);
  44. [targetA, targetB] = dual.targets;
  45. workspacePath = dual.workspacePath;
  46. await seedRemoteFixture(targetA, workspacePath, "A");
  47. await seedRemoteFixture(targetB, workspacePath, "B");
  48. const remoteConfigA = await readRemoteWorkspaceConfig(targetA, workspacePath);
  49. const remoteConfigB = await readRemoteWorkspaceConfig(targetB, workspacePath);
  50. expect(remoteConfigA).toContain('"marker": "A"');
  51. expect(remoteConfigA).not.toContain('"marker": "B"');
  52. expect(remoteConfigB).toContain('"marker": "B"');
  53. expect(remoteConfigB).not.toContain('"marker": "A"');
  54. await prepareV4ConversationE2E({ skipProvider: true });
  55. const initialHosts = await readHostProcesses();
  56. expect(initialHosts).toHaveLength(1);
  57. providerA = await createSSHLifecycleProvider(targetA, `${CASE_MARKER}_A`);
  58. const taskA = await connectSSHWorkspaceAndSendFirstTurn({
  59. caseMarker: CASE_MARKER,
  60. config: targetA,
  61. label: "A",
  62. provider: providerA,
  63. workspacePath,
  64. });
  65. const workspaceCatalogA = await getPluginReferenceCatalog({
  66. workspaceIdentity: taskA.identity,
  67. workspacePath,
  68. });
  69. expectCatalogMarker(workspaceCatalogA.plugins, "A");
  70. const pluginA = (await getPluginList(workspacePath, taskA.identity)).plugins.find(
  71. (plugin) => plugin.id === PLUGIN_ID,
  72. );
  73. expect(pluginA).toMatchObject({
  74. configuredOptions: { marker: "A" },
  75. enabled: true,
  76. enabledSource: "workspace",
  77. rootPath: posix.join(workspacePath, "plugin"),
  78. });
  79. const sessionCatalogA = await getPluginReferenceCatalog({
  80. sessionId: taskA.taskId,
  81. workspaceIdentity: taskA.identity,
  82. workspacePath,
  83. });
  84. expect(sessionCatalogA.authority).toBe("session");
  85. expectCatalogMarker(sessionCatalogA.plugins, "A");
  86. // Bug 根因:两个 remote lifecycle provider 使用同一个测试 model id;若在连接 A 前
  87. // 连续创建 A/B,后创建的 B 会替换模型列表中的 A。按目标串行创建可保持真实切换顺序,
  88. // 同时避免测试夹具先于 identity 隔离断言互相覆盖。
  89. providerB = await createSSHLifecycleProvider(targetB, `${CASE_MARKER}_B`);
  90. const taskB = await connectSSHWorkspaceAndSendFirstTurn({
  91. caseMarker: CASE_MARKER,
  92. config: targetB,
  93. label: "B",
  94. provider: providerB,
  95. workspacePath,
  96. });
  97. const workspaceCatalogB = await getPluginReferenceCatalog({
  98. workspaceIdentity: taskB.identity,
  99. workspacePath,
  100. });
  101. expectCatalogMarker(workspaceCatalogB.plugins, "B");
  102. const pluginB = (await getPluginList(workspacePath, taskB.identity)).plugins.find(
  103. (plugin) => plugin.id === PLUGIN_ID,
  104. );
  105. expect(pluginB).toMatchObject({
  106. configuredOptions: { marker: "B" },
  107. enabled: true,
  108. enabledSource: "workspace",
  109. rootPath: posix.join(workspacePath, "plugin"),
  110. });
  111. const sessionCatalogB = await getPluginReferenceCatalog({
  112. sessionId: taskB.taskId,
  113. workspaceIdentity: taskB.identity,
  114. workspacePath,
  115. });
  116. expect(sessionCatalogB.authority).toBe("session");
  117. expectCatalogMarker(sessionCatalogB.plugins, "B");
  118. expect(taskA.workspacePath).toBe(taskB.workspacePath);
  119. expect(taskA.identity).not.toBe(taskB.identity);
  120. expect(taskA.remoteSessionId).not.toBe(taskB.remoteSessionId);
  121. await waitForRemoteTab({
  122. caseMarker: CASE_MARKER,
  123. expectedIdentity: taskA.identity,
  124. expectedPath: workspacePath,
  125. expectedRemoteSessionId: taskA.remoteSessionId,
  126. requireActive: false,
  127. });
  128. await waitForRemoteTab({
  129. caseMarker: CASE_MARKER,
  130. expectedIdentity: taskB.identity,
  131. expectedPath: workspacePath,
  132. expectedRemoteSessionId: taskB.remoteSessionId,
  133. });
  134. expect(await readHostProcesses()).toEqual(initialHosts);
  135. // 非活动 remote Session 不保留在当前 target 的 Agent 内;先经真实 task 导航切回 A,
  136. // 再查它的 session-owned catalog,证明 B 的异步加载没有覆盖 A 的 workspace-keyed 状态。
  137. await selectV4TaskById(taskA.taskId, 30_000);
  138. await waitForRemoteTab({
  139. caseMarker: CASE_MARKER,
  140. expectedIdentity: taskA.identity,
  141. expectedPath: workspacePath,
  142. expectedRemoteSessionId: taskA.remoteSessionId,
  143. });
  144. expectCatalogMarker(
  145. (
  146. await getPluginReferenceCatalog({
  147. sessionId: taskA.taskId,
  148. workspaceIdentity: taskA.identity,
  149. workspacePath,
  150. })
  151. ).plugins,
  152. "A",
  153. );
  154. expectCatalogMarker(sessionCatalogA.plugins, "A");
  155. expectCatalogMarker(sessionCatalogB.plugins, "B");
  156. });
  157. });
  158. async function seedRemoteFixture(
  159. target: SSHConnectionConfig,
  160. workspacePath: string,
  161. marker: "A" | "B",
  162. ): Promise<void> {
  163. const pluginRoot = posix.join(workspacePath, "plugin");
  164. const manifest = {
  165. name: PLUGIN_NAME,
  166. version: "0.0.0",
  167. description: `WPL-010 target ${marker}`,
  168. skills: "skills",
  169. userConfig: {
  170. marker: {
  171. type: "string",
  172. title: "Target marker",
  173. },
  174. },
  175. };
  176. const config = {
  177. plugins: {
  178. dirs: [pluginRoot],
  179. enabledPlugins: { [PLUGIN_ID]: true },
  180. options: { [PLUGIN_ID]: { marker } },
  181. },
  182. };
  183. const skill = [
  184. "---",
  185. `name: target-${marker.toLowerCase()}`,
  186. `description: WPL-010 target ${marker} runtime marker`,
  187. "---",
  188. "",
  189. `WPL_010_TARGET_${marker}`,
  190. "",
  191. ].join("\n");
  192. const command = [
  193. "set -eu",
  194. `rm -rf -- ${quoteShell(workspacePath)}`,
  195. `mkdir -p -- ${quoteShell(posix.join(pluginRoot, ".zcode-plugin"))}`,
  196. `mkdir -p -- ${quoteShell(posix.join(pluginRoot, "skills", "identity"))}`,
  197. `mkdir -p -- ${quoteShell(posix.join(workspacePath, ".zcode"))}`,
  198. writeBase64Command(
  199. posix.join(pluginRoot, ".zcode-plugin", "plugin.json"),
  200. `${JSON.stringify(manifest, null, 2)}\n`,
  201. ),
  202. writeBase64Command(posix.join(pluginRoot, "skills", "identity", "SKILL.md"), skill),
  203. writeBase64Command(
  204. posix.join(workspacePath, ".zcode", "config.json"),
  205. `${JSON.stringify(config, null, 2)}\n`,
  206. ),
  207. ].join(" && ");
  208. const result = await runSSHCommand(target, command);
  209. expect(result.code).toBe(0);
  210. expect(result.stderr).toBe("");
  211. }
  212. async function readRemoteWorkspaceConfig(
  213. target: SSHConnectionConfig,
  214. workspacePath: string,
  215. ): Promise<string> {
  216. const result = await runSSHCommand(
  217. target,
  218. `cat -- ${quoteShell(posix.join(workspacePath, ".zcode", "config.json"))}`,
  219. );
  220. expect(result.code).toBe(0);
  221. return result.stdout;
  222. }
  223. async function cleanupRemoteFixture(
  224. target: SSHConnectionConfig,
  225. workspacePath: string,
  226. ): Promise<void> {
  227. if (workspacePath !== ALLOWED_REMOTE_WORKSPACE) {
  228. throw new Error(`${CASE_MARKER}: refusing to clean unexpected remote path ${workspacePath}`);
  229. }
  230. await runSSHCommand(target, `rm -rf -- ${quoteShell(workspacePath)}`);
  231. }
  232. function expectCatalogMarker(
  233. plugins: Awaited<ReturnType<typeof getPluginReferenceCatalog>>["plugins"],
  234. marker: "A" | "B",
  235. ): void {
  236. const plugin = plugins.find((entry) => entry.pluginId === PLUGIN_ID);
  237. expect(plugin).toMatchObject({
  238. enabled: true,
  239. skillQualifiedNames: [`${PLUGIN_NAME}:target-${marker.toLowerCase()}`],
  240. });
  241. expect(plugin?.skillQualifiedNames).not.toContain(
  242. `${PLUGIN_NAME}:target-${marker === "A" ? "b" : "a"}`,
  243. );
  244. }
  245. function writeBase64Command(path: string, content: string): string {
  246. const encoded = Buffer.from(content, "utf8").toString("base64");
  247. return `printf '%s' ${quoteShell(encoded)} | base64 -d > ${quoteShell(path)}`;
  248. }
  249. function quoteShell(value: string): string {
  250. return `'${value.replaceAll("'", "'\\''")}'`;
  251. }