import * as fs from "node:fs"; import * as path from "node:path"; function runDirectory(projectRunsRoot: string, runId: string): string { const root = path.resolve(projectRunsRoot); if (runId.length === 0 || runId.includes("/") || runId.includes("\\") || runId === "." || runId === "..") { throw new Error(`运行 ID 无效:${runId}`); } const directory = path.resolve(root, runId); if (directory === root || path.dirname(directory) !== root) throw new Error(`运行 ID 无效:${runId}`); return directory; } export async function deleteRunDirectoryAt(projectRunsRoot: string, runId: string): Promise { const directory = runDirectory(projectRunsRoot, runId); try { await fs.promises.lstat(directory); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return false; throw error; } await fs.promises.rm(directory, { recursive: true, force: true }); return true; }