From 23c1119d9c58b68849859393c58187bfea0234d0 Mon Sep 17 00:00:00 2001 From: GB Date: Tue, 17 Nov 2026 07:27:52 -0700 Subject: [PATCH 1/2] fix(server): worktree recovery no longer fails on Windows when the emptied folder is in use Recovering a worktree folder that lost its checkout deleted the folder before running `git worktree add`. On Windows that delete fails with EBUSY while any process (the thread's terminal, the provider, Explorer) still has the folder as its cwd, so sends failed with "Could not recreate the worktree". git accepts an empty target, so clear only the leftover entries and keep the folder. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.test.ts | 55 +++++++++++++++++++ .../src/workspace/restoreMissingWorktree.ts | 14 ++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index edec4cad58790..aaad7b18fbd8e 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -337,6 +337,61 @@ effectIt.effect("restores a missing worktree before a send only under the worksp }).pipe(Effect.provide(layerRestoreLeaseTest)), ); +effectIt.effect("restores into a leftover worktree folder that cannot itself be removed", () => { + const worktreePath = `${managedWorktreesDir}/project/feature-restore`; + // Windows refuses to remove a folder that is still some process's cwd. + const remove = vi.fn((target: string) => + target === worktreePath + ? Effect.fail( + PlatformError.systemError({ + _tag: "Busy", + module: "FileSystem", + method: "remove", + pathOrDescriptor: target, + }), + ) + : Effect.void, + ); + const createWorktree = vi.fn(() => Effect.succeed({} as never)); + return Effect.gen(function* () { + const { restore } = yield* ThreadWorktreeRestore; + yield* restore({ + id: ThreadId.make("thread_leftover_restore"), + projectId: ProjectId.make("project_leftover_restore"), + worktreePath, + branch: "feature/restore", + }); + expect(remove).toHaveBeenCalledWith(`${worktreePath}/node_modules`, { recursive: true }); + expect(createWorktree).toHaveBeenCalledTimes(1); + }).pipe( + Effect.provide( + layerThreadWorktreeRestore.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(FileSystem.FileSystem, { + exists: (target: string) => Effect.succeed(!target.endsWith("/.git")), + readDirectory: () => Effect.succeed(["node_modules"]), + stat: () => Effect.succeed({ type: "Directory" }), + realPath: (target: string) => Effect.succeed(normalize(target)), + remove, + } as never), + layerWorktreeLocation(), + Layer.mock(GitWorkflow.GitWorkflowService)({ + pruneWorktrees: () => Effect.void, + createWorktree, + listLocalBranchNames: () => Effect.succeed(["feature/restore"]), + }), + Layer.mock(ProjectService.ProjectService)({ + getById: () => + Effect.succeedSome({ workspaceRoot: "/tmp/provider-turn-start-project" } as never), + }), + ), + ), + ), + ), + ); +}); + it.each([ { label: "directly in the scratch root", folder: "/tmp/t3-home/scratch/notes", recreated: true }, { label: "below another folder", folder: "/tmp/t3-home/scratch/notes/deep", recreated: false }, diff --git a/apps/server/src/workspace/restoreMissingWorktree.ts b/apps/server/src/workspace/restoreMissingWorktree.ts index dee23025dbf41..c2c3c00eb7741 100644 --- a/apps/server/src/workspace/restoreMissingWorktree.ts +++ b/apps/server/src/workspace/restoreMissingWorktree.ts @@ -182,8 +182,18 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () { worktreePath, branch, }); - // `git worktree add` refuses a non-empty target. - yield* (leftover ? fileSystem.remove(worktreePath, { recursive: true }) : Effect.void).pipe( + // `git worktree add` refuses a non-empty target but accepts an empty one. Keep the folder + // itself: Windows cannot remove a folder that is still a terminal's or provider's cwd. + const clearLeftovers = Effect.suspend(() => fileSystem.readDirectory(worktreePath)).pipe( + Effect.flatMap((entries) => + Effect.forEach( + entries, + (entry) => fileSystem.remove(path.join(worktreePath, entry), { recursive: true }), + { discard: true }, + ), + ), + ); + yield* (leftover ? clearLeftovers : Effect.void).pipe( Effect.andThen(recreateMissingWorktree({ cwd: project.workspaceRoot, worktreePath, branch })), Effect.mapError((cause) => cause._tag === "WorktreeRecreateNoBaseError" From aa574012de796a5261fbc3060c1dbfc037507fca Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 15:08:37 +0000 Subject: [PATCH 2/2] test(server): startup harness accepts an existing empty worktree folder Worktree recovery now keeps the emptied folder, as `git worktree add` does. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/ProviderTurnStartService.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index aaad7b18fbd8e..d12e6fd1e8bdb 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -2500,7 +2500,9 @@ effectIt.effect.each(["cleanup-first", "startup-first"])( failReadsAfterRunning: true, }); harness.createWorktree.mockImplementation(() => - fileSystem.makeDirectory(worktreePath).pipe(Effect.as({} as never), Effect.orDie), + fileSystem + .makeDirectory(worktreePath, { recursive: true }) + .pipe(Effect.as({} as never), Effect.orDie), ); const open = harness.open.getMockImplementation()!; harness.open.mockImplementation(() =>