@devries-cameron20 / t3code-1 / commits / 48c8cf787e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a failed worktree add no longer leaves its new branch behind (#28373)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 18:30 UTC 48c8cf787e7595625c757434e5e3458ff19db411
2 files changed +46 -4
apps/server/src/vcs/GitVcsDriverCore.test.ts
+32
@@ -4317,6 +4317,38 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4317 }),
4318 );
4319
4320 + // regression: #28354 a failed worktree add left its new branch behind and blocked retries
4321 + it.effect("removes the new branch when the worktree cannot be added", () =>
4322 + Effect.gen(function* () {
4323 + const cwd = yield* makeTmpDir();
4324 + const { initialBranch } = yield* initRepoWithCommit(cwd);
4325 + const path = yield* Path.Path;
4326 + const occupiedPath = yield* makeTmpDir("git-worktrees-");
4327 + yield* writeTextFile(occupiedPath, "existing.txt", "occupied\n");
4328 + const driver = yield* GitVcsDriver.GitVcsDriver;
4329 +
4330 + const failed = yield* driver
4331 + .createWorktree({
4332 + cwd,
4333 + path: occupiedPath,
4334 + refName: initialBranch,
4335 + newRefName: "t3/collide",
4336 + })
4337 + .pipe(Effect.result);
4338 +
4339 + assert.isTrue(Result.isFailure(failed));
4340 + assert.equal(yield* git(cwd, ["branch", "--list", "t3/collide"]), "");
4341 + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "retry");
4342 + yield* driver.createWorktree({
4343 + cwd,
4344 + path: worktreePath,
4345 + refName: initialBranch,
4346 + newRefName: "t3/collide",
4347 + });
4348 + assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "t3/collide");
4349 + }),
4350 + );
4351 +
4352 it.effect("creates parallel worktrees in one repository without racing on its config", () =>
4353 Effect.gen(function* () {
4354 const cwd = yield* makeTmpDir();
apps/server/src/vcs/GitVcsDriverCore.ts
+14 -4
@@ -4188,15 +4188,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4188 }
4189 worktreePath = path.join(parentDir, repoName, directoryName);
4190 }
4191 - if (input.newRefName) {
4191 + const newRefName = input.newRefName;
4192 + if (newRefName) {
4193 // Kept apart from the checkout so the shared-config lock only covers the tracking write.
4194 yield* createBranchAt(
4195 "GitVcsDriver.createWorktree.createBranch",
4195 - { cwd: input.cwd, branch: input.newRefName, startPoint: input.refName },
4196 + { cwd: input.cwd, branch: newRefName, startPoint: input.refName },
4197 "git worktree add failed",
4198 );
4199 }
4199 - const args = ["worktree", "add", "--", worktreePath, input.newRefName ?? input.refName];
4200 + const args = ["worktree", "add", "--", worktreePath, newRefName ?? input.refName];
4201 const progress = options?.progress;
4202 const onCheckoutProgress = progress?.onCheckoutProgress;
4203
@@ -4234,6 +4235,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4235 }
4236 : {}),
4237 },
4238 + ).pipe(
4239 + Effect.onError(() =>
4240 + newRefName
4241 + ? deleteLocalBranch({ cwd: input.cwd, refName: newRefName, force: true }).pipe(
4242 + Effect.catch((cause) =>
4243 + Effect.logWarning("could not remove the branch of a failed worktree", { cause }),
4244 + ),
4245 + )
4246 + : Effect.void,
4247 + ),
4248 );
4249
4250 if (progress?.onWorktreeClaimed) {
@@ -4319,7 +4330,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4330 );
4331 }
4332
4322 - const newRefName = input.newRefName;
4333 if (newRefName && base) {
4334 yield* Effect.gen(function* () {
4335 yield* writeConfigValue({