e2e-runtime-paths.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { join, posix, resolve } from "node:path";
  2. import { readdir, stat } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. export const E2E_RUNTIME_RELATIVE_ROOT = ".zcode-e2e";
  5. export const E2E_RUNTIME_ROOT_REPLAY_TOKEN = "{{e2eRuntimeRoot}}";
  6. const E2E_AGENT_WORKSPACE_NAME = "ZCodeProject";
  7. interface ResolveE2EHomeDirOptions {
  8. cwd?: string;
  9. env?: Record<string, string | undefined>;
  10. }
  11. export function resolveE2EHomeDir({
  12. cwd = process.cwd(),
  13. env = process.env,
  14. }: ResolveE2EHomeDirOptions = {}): string {
  15. const configuredHome =
  16. env.ZCODE_E2E_HOME_DIR?.trim() ||
  17. env.ZCODE_DATA_BASE_DIR?.trim() ||
  18. env.ZCODE_DESKTOP_HOME_DIR?.trim();
  19. return resolve(cwd, configuredHome || ".e2e-home");
  20. }
  21. export function resolveE2EStorageRoot(e2eHomeDir = resolveE2EHomeDir()): string {
  22. // Bug 根因:回滚 storage profile 时删除了仍被 E2E 用例依赖的路径 helper。
  23. // E2E 继续隔离 HOME,但 CLI 根固定使用历史目录 .zcode,不再区分 dev/stable profile。
  24. return join(e2eHomeDir, ".zcode");
  25. }
  26. export function resolveE2ERuntimeRoot(e2eHomeDir = resolveE2EHomeDir()): string {
  27. // Bug 根因:conversation E2E 的 Agent 实际 cwd 是隔离 HOME 下的
  28. // ZCodeProject,而不是 conversation backing workspace。Node 若写到后者,
  29. // Bash 的相对路径就会永远等不到同一个文件,Windows 上还会叠加 shell 路径差异。
  30. return join(e2eHomeDir, E2E_AGENT_WORKSPACE_NAME, E2E_RUNTIME_RELATIVE_ROOT);
  31. }
  32. export function resolveE2ERuntimePath(...segments: string[]): string {
  33. return join(resolveE2ERuntimeRoot(), ...segments);
  34. }
  35. export function resolveE2EToolPath(...segments: string[]): string {
  36. // Bug 根因:Windows Node 与 Git Bash 会把裸 POSIX 临时根解析成不同目录。
  37. // 工具参数保留绝对路径,但统一成 Windows 文件 API 同样接受的正斜杠形式,
  38. // 让 JSON replay fixture 不需要猜测盘符转义或 Git Bash mount。
  39. return resolveE2ERuntimePath(...segments).replaceAll("\\", "/");
  40. }
  41. export function resolveE2EShellPath(...segments: string[]): string {
  42. return posix.join(E2E_RUNTIME_RELATIVE_ROOT, ...segments);
  43. }
  44. export function createE2EReplayFixtureVariables(
  45. e2eHomeDir = resolveE2EHomeDir(),
  46. ): Readonly<Record<string, string>> {
  47. return {
  48. e2eRuntimeRoot: resolveE2ERuntimeRoot(e2eHomeDir).replaceAll("\\", "/"),
  49. };
  50. }
  51. /** 前台 Bash 仍在运行时尚无 tool_result;按真实 session/tool 查找唯一输出,不能固定旧 HOME 根。 */
  52. export async function findE2EBashOutputPath(
  53. sessionId: string,
  54. toolCallId: string,
  55. {
  56. tempRoot = tmpdir(),
  57. storageRoot = resolveE2EStorageRoot(),
  58. }: { tempRoot?: string; storageRoot?: string } = {},
  59. ): Promise<string | null> {
  60. const directories = (await readdir(tempRoot, { withFileTypes: true }))
  61. .filter((entry) => entry.isDirectory() && entry.name.startsWith("zcode-bash-"))
  62. .map((entry) => join(tempRoot, entry.name));
  63. const candidates = [...directories, join(storageRoot, "cli", "exec")].map((root) =>
  64. join(root, sessionId, `${toolCallId}-stdout.log`),
  65. );
  66. const existing = (
  67. await Promise.all(
  68. candidates.map(async (path) => {
  69. try {
  70. return (await stat(path)).isFile() ? path : null;
  71. } catch (error) {
  72. if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
  73. throw error;
  74. }
  75. }),
  76. )
  77. ).filter((path): path is string => path !== null);
  78. if (existing.length > 1)
  79. throw new Error(
  80. `Bash 没有唯一实际输出文件: ${sessionId}/${toolCallId}; ${JSON.stringify(existing)}`,
  81. );
  82. return existing[0] ?? null;
  83. }