diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 8c627525b4..1e71d8be58 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -1,5 +1,6 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it, describe } from "@effect/vitest"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -8,11 +9,16 @@ import * as PlatformError from "effect/PlatformError"; import * as Scope from "effect/Scope"; import * as Sink from "effect/Sink"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { GitCommandError } from "@t3tools/contracts"; import { ServerConfig } from "../config.ts"; -import { splitNullSeparatedGitStdoutPaths } from "./GitVcsDriverCore.ts"; +import { + makeGitVcsDriverCore, + splitNullSeparatedGitStdoutPaths, + WORKTREE_REMOVE_TIMEOUT_MS, +} from "./GitVcsDriverCore.ts"; import * as GitVcsDriver from "./GitVcsDriver.ts"; const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), { @@ -38,6 +44,55 @@ const makeNonRepositoryHandle = () => getOutputFd: () => Stream.empty, }); +const makeWorktreeRemovalLayer = (input: { + readonly exitCode: Effect.Effect; + readonly commands: Array>; + readonly timeoutMs?: number; +}) => { + const spawner = ChildProcessSpawner.make((command) => + Effect.sync(() => { + if (!ChildProcess.isStandardCommand(command)) { + return assert.fail("expected a standard Git command"); + } + input.commands.push(command.args); + let running = true; + return ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(1), + exitCode: input.exitCode.pipe( + Effect.tap(() => + Effect.sync(() => { + running = false; + }), + ), + ), + isRunning: Effect.sync(() => running), + kill: () => + Effect.sync(() => { + running = false; + }), + unref: Effect.succeed(Effect.void), + stdin: Sink.drain, + stdout: Stream.empty, + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }); + }), + ); + const nodeServicesLayer = Layer.merge( + NodeServices.layer, + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + + return Layer.effect( + GitVcsDriver.GitVcsDriver, + makeGitVcsDriverCore( + input.timeoutMs === undefined ? {} : { worktreeRemoveTimeoutMs: input.timeoutMs }, + ), + ).pipe(Layer.provide(ServerConfigLayer), Layer.provideMerge(nodeServicesLayer)); +}; + const makeTmpDir = ( prefix = "git-vcs-driver-test-", ): Effect.Effect => @@ -641,6 +696,53 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => { }); describe("worktree operations", () => { + it.effect("uses the extended deadline while allowing a slow worktree removal", () => { + const commands: Array> = []; + const layer = makeWorktreeRemovalLayer({ + commands, + exitCode: Effect.sleep(Duration.millis(50)).pipe( + Effect.as(ChildProcessSpawner.ExitCode(0)), + ), + timeoutMs: 100, + }); + + return TestClock.withLive( + Effect.gen(function* () { + assert.equal(WORKTREE_REMOVE_TIMEOUT_MS, 300_000); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* driver.removeWorktree({ cwd: "/repo", path: "/repo-worktrees/feature" }); + + assert.deepStrictEqual(commands, [["worktree", "remove", "/repo-worktrees/feature"]]); + }).pipe(Effect.provide(layer)), + ); + }); + + it.effect("still fails a worktree removal that exceeds its cleanup deadline", () => { + const commands: Array> = []; + const layer = makeWorktreeRemovalLayer({ + commands, + exitCode: Effect.never, + timeoutMs: 25, + }); + + return TestClock.withLive( + Effect.gen(function* () { + const driver = yield* GitVcsDriver.GitVcsDriver; + const error = yield* driver + .removeWorktree({ cwd: "/repo", path: "/repo-worktrees/stuck" }) + .pipe(Effect.flip); + + assert.deepInclude(error, { + _tag: "GitCommandError", + operation: "GitVcsDriver.removeWorktree", + detail: "Git command timed out.", + cwd: "/repo", + }); + assert.deepStrictEqual(commands, [["worktree", "remove", "/repo-worktrees/stuck"]]); + }).pipe(Effect.provide(layer)), + ); + }); + it.effect("creates and removes a worktree for a new refName", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 6293ff7c29..915b35f98f 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -38,6 +38,7 @@ import { import { ServerConfig } from "../config.ts"; const DEFAULT_TIMEOUT_MS = 30_000; +export const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5)); const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]"; const PREPARED_COMMIT_PATCH_MAX_OUTPUT_BYTES = 49_000; @@ -642,7 +643,13 @@ const collectOutput = Effect.fnUntraced(function* ( }; }); -export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* () { +export interface GitVcsDriverCoreOptions { + readonly worktreeRemoveTimeoutMs?: number; +} + +export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ( + options: GitVcsDriverCoreOptions = {}, +) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -2395,7 +2402,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } args.push(input.path); yield* executeGit("GitVcsDriver.removeWorktree", input.cwd, args, { - timeoutMs: 15_000, + // Removing untracked dependency trees is filesystem-bound and can take + // minutes on Windows. Keep the operation supervised, but give it a + // cleanup-specific ceiling instead of the short interactive deadline. + timeoutMs: options.worktreeRemoveTimeoutMs ?? WORKTREE_REMOVE_TIMEOUT_MS, fallbackErrorDetail: "git worktree remove failed", }); });