From 870d6943ed0d8da9292ab4ff5d456d60174ea218 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 5 Mar 2026 16:14:36 -0800 Subject: [PATCH] Run Windows batch launchers via shell and harden test cleanup - Launch Windows .bat/.cmd wrappers directly with `shell` set to COMSPEC to preserve argument/path handling (including spaced paths) - Update process runner tests to cover the new Windows launch behavior - Add `removeDirectoryBestEffort` and use it in integration/tests to reduce flaky temp-dir cleanup failures --- .../OrchestrationEngineHarness.integration.ts | 5 +- apps/server/src/git/Layers/GitCore.test.ts | 7 +-- .../Layers/CheckpointReactor.test.ts | 3 +- apps/server/src/processRunner.test.ts | 47 +++++++++++++++---- apps/server/src/processRunner.ts | 25 ++-------- .../testUtils/removeDirectoryBestEffort.ts | 40 ++++++++++++++++ 6 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 apps/server/src/testUtils/removeDirectoryBestEffort.ts diff --git a/apps/server/integration/OrchestrationEngineHarness.integration.ts b/apps/server/integration/OrchestrationEngineHarness.integration.ts index 9c7fae9d51..ace8744856 100644 --- a/apps/server/integration/OrchestrationEngineHarness.integration.ts +++ b/apps/server/integration/OrchestrationEngineHarness.integration.ts @@ -57,6 +57,7 @@ import { type TestProviderAdapterHarness, } from "./TestProviderAdapter.integration.ts"; import { ServerConfig } from "../src/config.ts"; +import { removeDirectoryBestEffort } from "../src/testUtils/removeDirectoryBestEffort.ts"; function runGit(cwd: string, args: ReadonlyArray) { return execFileSync("git", args, { @@ -388,9 +389,7 @@ export const makeOrchestrationIntegrationHarness = Effect.gen(function* () { yield* shutdown.pipe( Effect.ensuring( - Effect.sync(() => { - fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); - }), + Effect.promise(() => removeDirectoryBestEffort(rootDir)), ), ); }); diff --git a/apps/server/src/git/Layers/GitCore.test.ts b/apps/server/src/git/Layers/GitCore.test.ts index 900d3218ff..8d494931ad 100644 --- a/apps/server/src/git/Layers/GitCore.test.ts +++ b/apps/server/src/git/Layers/GitCore.test.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdtempSync, rmSync } from "node:fs"; +import { existsSync, mkdtempSync } from "node:fs"; import os from "node:os"; import path from "node:path"; @@ -13,6 +13,7 @@ import { GitCoreLive } from "./GitCore.ts"; import { GitCore, type GitCoreShape } from "../Services/GitCore.ts"; import { GitCommandError } from "../Errors.ts"; import { runProcess } from "../../processRunner.ts"; +import { removeDirectoryBestEffort } from "../../testUtils/removeDirectoryBestEffort.ts"; // ── Helpers ── @@ -24,9 +25,9 @@ const GitCoreTestLayer = GitCoreLive.pipe( const TestLayer = Layer.mergeAll(NodeServices.layer, GitServiceTestLayer, GitCoreTestLayer); const tempDirs = new Set(); -afterEach(() => { +afterEach(async () => { for (const dir of tempDirs) { - rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); + await removeDirectoryBestEffort(dir); } tempDirs.clear(); }); diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts index dbee690da6..521b243676 100644 --- a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts +++ b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts @@ -38,6 +38,7 @@ import { } from "../../provider/Services/ProviderService.ts"; import { checkpointRefForThreadTurn } from "../../checkpointing/Utils.ts"; import { ServerConfig } from "../../config.ts"; +import { removeDirectoryBestEffort } from "../../testUtils/removeDirectoryBestEffort.ts"; const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value); const asSessionId = (value: string): ProviderSessionId => ProviderSessionId.makeUnsafe(value); @@ -210,7 +211,7 @@ describe("CheckpointReactor", () => { while (tempDirs.length > 0) { const dir = tempDirs.pop(); if (dir) { - fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); + await removeDirectoryBestEffort(dir); } } }); diff --git a/apps/server/src/processRunner.test.ts b/apps/server/src/processRunner.test.ts index c8b13be211..a50ca708f5 100644 --- a/apps/server/src/processRunner.test.ts +++ b/apps/server/src/processRunner.test.ts @@ -102,10 +102,9 @@ describe("resolveProcessLaunchPlan", () => { }, }); - expect(plan.command).toBe("C:\\Windows\\System32\\cmd.exe"); - expect(plan.args.slice(0, 3)).toEqual(["/d", "/s", "/c"]); - expect(plan.args[3]).toBe(`"${wrapperPath}" "C:\\repo\\a&b.ts"`); - expect(plan.shell).toBe(false); + expect(plan.command).toBe(wrapperPath); + expect(plan.args).toEqual(["C:\\repo\\a&b.ts"]); + expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe"); }); }); @@ -130,8 +129,38 @@ describe("resolveProcessLaunchPlan", () => { }, }); - expect(plan.command).toBe("C:\\Windows\\System32\\cmd.exe"); - expect(plan.args[3]).toBe(`"${wrapperPath}" "pkg@^1.0"`); + expect(plan.command).toBe(wrapperPath); + expect(plan.args).toEqual(["pkg@^1.0"]); + expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe"); + }); + }); + + it("preserves spaced batch launcher paths on windows", () => { + withTempDir((rootDir) => { + const dir = path.join(rootDir, "runner admin"); + fs.mkdirSync(dir); + const wrapperPath = path.join(dir, "code.CMD"); + fs.writeFileSync(wrapperPath, "@echo off\r\n"); + + const plan = resolveProcessLaunchPlan("code", ["--status"], { + env: { + PATH: dir, + PATHEXT: ".COM;.EXE;.BAT;.CMD", + COMSPEC: "C:\\Windows\\System32\\cmd.exe", + }, + inheritParentEnv: false, + runtimeEnvironment: { + platform: "windows", + pathStyle: "windows", + isWsl: false, + windowsInteropMode: "windows-native", + wslDistroName: null, + }, + }); + + expect(plan.command).toBe(wrapperPath); + expect(plan.args).toEqual(["--status"]); + expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe"); }); }); @@ -191,9 +220,9 @@ describe("makeRuntimeCommand", () => { }, }); - expect(command.command).toBe("C:\\Windows\\System32\\cmd.exe"); - expect(command.args).toEqual(["/d", "/s", "/c", `"${wrapperPath}" "C:\\repo\\a&b.ts"`]); - expect(command.options.shell).toBe(false); + expect(command.command).toBe(wrapperPath); + expect(command.args).toEqual(["C:\\repo\\a&b.ts"]); + expect(command.options.shell).toBe("C:\\Windows\\System32\\cmd.exe"); }); }); }); diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.ts index 30018717ae..67c51e375e 100644 --- a/apps/server/src/processRunner.ts +++ b/apps/server/src/processRunner.ts @@ -164,25 +164,6 @@ function resolveWindowsCommand( return null; } -function quoteWindowsBatchArgument(argument: string): string { - if (argument.length === 0) { - return '""'; - } - - const escaped = argument - .replaceAll("%", "%%") - .replace(/(\\*)"/g, (_match, slashes: string) => `${slashes}${slashes}\\"`) - .replace(/(\\+)$/g, "$1$1"); - return `"${escaped}"`; -} - -function buildWindowsBatchCommandLine( - command: string, - args: ReadonlyArray, -): string { - return [quoteWindowsBatchArgument(command), ...args.map(quoteWindowsBatchArgument)].join(" "); -} - function resolveWindowsCommandShell(env: NodeJS.ProcessEnv): string { return env.ComSpec ?? env.COMSPEC ?? process.env.ComSpec ?? process.env.COMSPEC ?? "cmd.exe"; } @@ -224,9 +205,9 @@ export function resolveProcessLaunchPlan( if (resolved.kind === "batch") { return { - command: resolveWindowsCommandShell(env), - args: ["/d", "/s", "/c", buildWindowsBatchCommandLine(resolved.path, args)], - shell: false, + command: resolved.path, + args: [...args], + shell: resolveWindowsCommandShell(env), runtimeEnvironment, }; } diff --git a/apps/server/src/testUtils/removeDirectoryBestEffort.ts b/apps/server/src/testUtils/removeDirectoryBestEffort.ts new file mode 100644 index 0000000000..671d1c5eb8 --- /dev/null +++ b/apps/server/src/testUtils/removeDirectoryBestEffort.ts @@ -0,0 +1,40 @@ +import fs from "node:fs"; + +const RETRYABLE_REMOVE_ERROR_CODES = new Set(["EBUSY", "ENOTEMPTY", "EPERM"]); + +function isRetryableRemoveError(error: unknown): boolean { + if (!error || typeof error !== "object") { + return false; + } + + const code = "code" in error ? error.code : undefined; + return typeof code === "string" && RETRYABLE_REMOVE_ERROR_CODES.has(code); +} + +export async function removeDirectoryBestEffort( + dir: string, + options?: { + readonly maxRetries?: number; + readonly retryDelayMs?: number; + }, +): Promise { + const maxRetries = options?.maxRetries ?? 20; + const retryDelayMs = options?.retryDelayMs ?? 100; + + for (let attempt = 0; attempt <= maxRetries; attempt += 1) { + try { + fs.rmSync(dir, { recursive: true, force: true }); + return; + } catch (error) { + if (!isRetryableRemoveError(error)) { + throw error; + } + + if (attempt === maxRetries) { + return; + } + + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); + } + } +}