Skip to content

Commit 2ae1676

Browse files
fix(server): turns recreate a worktree folder that lost its checkout
A failed removal can leave a thread's managed worktree folder behind with no .git link. Turn start only checked that the folder existed, so the agent ran outside any checkout and git commands reached the repository around it. Turn start now checks for the folder's .git, clears the leftover folder, and recreates the checkout. Storage cleanup keeps such a folder as "not a linked worktree" instead of failing the sweep on the missing .git. Fixes #24390 AI-assisted; verified with the tests and tools listed above.
1 parent 60430b7 commit 2ae1676

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ function startWithMissingWorktree(
141141
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
142142
Layer.succeed(FileSystem.FileSystem, {
143143
exists: () => Effect.succeed(false),
144+
remove: () => Effect.void,
144145
realPath: (target: string) =>
145146
normalize(target) in realPaths
146147
? Effect.succeed(realPaths[normalize(target)])

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export const layer: Layer.Layer<
219219
readonly branch: string;
220220
}) {
221221
const { cwd, worktreePath, branch } = input;
222+
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
222223
yield* gitWorkflow.pruneWorktrees({ cwd });
223224
const localBranchNames = yield* gitWorkflow.listLocalBranchNames(cwd);
224225
if (localBranchNames.includes(branch)) {
@@ -651,8 +652,9 @@ export const layer: Layer.Layer<
651652
}
652653
const { worktreePath, branch } = projection.thread;
653654
if (worktreePath !== null && branch !== null) {
655+
// A failed removal can leave the folder behind without its checkout.
654656
const exists = yield* fileSystem
655-
.exists(worktreePath)
657+
.exists(path.join(worktreePath, ".git"))
656658
.pipe(Effect.orElseSucceed(() => true));
657659
if (!exists && (yield* isManagedWorktreePath(worktreePath))) {
658660
const project = yield* projects.getById(projection.thread.projectId).pipe(

‎apps/server/src/storageCleanup.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,10 @@ const make = Effect.gen(function* () {
944944
return "worktree is a symbolic link";
945945
if (!roots.some((root) => inside(root, realPath)))
946946
return "outside the managed worktree folder";
947-
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
948-
return "not a linked worktree";
947+
const gitLink = yield* fs
948+
.stat(path.join(worktreePath, ".git"))
949+
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined));
950+
if (gitLink?.type !== "File") return "not a linked worktree";
949951
return null;
950952
});
951953
const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>`

0 commit comments

Comments
 (0)