The best way to control Claude Code, Codex and more from any computer or phone
fix(server): cancelling worktree setup no longer leaves its new branch behind (#29781)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 3, 2026 at 20:46 UTC
c3c15f7d3623979e0d93fc1cf0963cc6b9487dcf
2 files changed
+88
-7
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+57
-1
@@ -164,6 +164,10 @@ function makeHarness(options: HarnessOptions = {}) {
164
(_input: Parameters<GitWorkflow.GitWorkflowService["Service"]["removeWorktree"]>[0]) =>
165
Effect.void,
166
);
167
+ const deleteLocalBranch = vi.fn(
168
+ (_input: Parameters<GitWorkflow.GitWorkflowService["Service"]["deleteLocalBranch"]>[0]) =>
169
+ Effect.void,
170
+ );
171
const runSetup = vi.fn(
172
options.runSetup ?? (() => Effect.succeed({ status: "no-script" as const })),
173
);
@@ -208,6 +212,7 @@ function makeHarness(options: HarnessOptions = {}) {
212
remoteExists: options.remoteExists ?? (() => Effect.succeed(true)),
213
remoteBranchExists: () => Effect.succeed(true),
214
removeWorktree,
215
+ deleteLocalBranch,
216
...options.git,
217
}),
218
Layer.succeed(ProjectSetupScriptRunner.ProjectSetupScriptRunner, {
@@ -276,6 +281,7 @@ function makeHarness(options: HarnessOptions = {}) {
281
),
282
createWorktree,
283
removeWorktree,
284
+ deleteLocalBranch,
285
renameBranch,
286
generateBranchName,
287
generateThreadTitle,
@@ -2188,8 +2194,15 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
2194
it.effect("cancels tracked setup before provider work is released", () =>
2195
Effect.gen(function* () {
2196
const entered = yield* Deferred.make<void>();
2197
+ const renamed = yield* Deferred.make<void>();
2198
const harness = makeHarness({
2192
- runSetup: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)),
2199
+ renameBranch: (input) =>
2200
+ Deferred.succeed(renamed, undefined).pipe(Effect.as({ branch: input.newBranch })),
2201
+ runSetup: () =>
2202
+ Deferred.await(renamed).pipe(
2203
+ Effect.andThen(Deferred.succeed(entered, undefined)),
2204
+ Effect.andThen(Effect.never),
2205
+ ),
2206
});
2207
yield* Effect.gen(function* () {
2208
const launches = yield* ThreadLaunch.ThreadLaunchService;
@@ -2210,11 +2223,50 @@ it.effect("cancels tracked setup before provider work is released", () =>
2223
const projection = yield* threads.getThreadProjection(launched.threadId);
2224
assert.equal(projection.runs[0]?.status, "failed");
2225
assert.isNull(projection.thread.worktreePath);
2226
+ assert.deepEqual(harness.deleteLocalBranch.mock.calls[0]?.[0], {
2227
+ cwd: "/repo",
2228
+ refName: "generated-branch",
2229
+ force: true,
2230
+ });
2231
assert.isEmpty(yield* outbox.listByCommandId(CommandId.make(`${input.commandId}:release`)));
2232
}).pipe(Effect.provide(harness.layer));
2233
}),
2234
);
2235
2236
+it.effect(
2237
+ "deletes a claimed worktree's branch when setup is cancelled before checkout returns",
2238
+ () =>
2239
+ Effect.gen(function* () {
2240
+ const claimed = yield* Deferred.make<void>();
2241
+ const harness = makeHarness({
2242
+ createWorktree: (_input, options) =>
2243
+ (options?.progress?.onWorktreeClaimed?.("/repo-worktrees/feature") ?? Effect.void).pipe(
2244
+ Effect.andThen(Deferred.succeed(claimed, undefined)),
2245
+ Effect.andThen(Effect.never),
2246
+ ),
2247
+ });
2248
+ yield* Effect.gen(function* () {
2249
+ const launches = yield* ThreadLaunch.ThreadLaunchService;
2250
+ const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2251
+ const launched = yield* launches.launch(
2252
+ launchInput({
2253
+ command: "launch:cancel-claimed",
2254
+ thread: "thread:cancel-claimed",
2255
+ message: "Start",
2256
+ workspace: { type: "worktree", baseRef: "main" },
2257
+ }),
2258
+ );
2259
+ yield* Deferred.await(claimed);
2260
+ assert.isTrue(yield* tracker.cancel(launched.threadId));
2261
+ assert.equal(harness.removeWorktree.mock.calls[0]?.[0].path, "/repo-worktrees/feature");
2262
+ assert.equal(
2263
+ harness.deleteLocalBranch.mock.calls[0]?.[0].refName,
2264
+ harness.createWorktree.mock.calls[0]?.[0].newRefName,
2265
+ );
2266
+ }).pipe(Effect.provide(harness.layer));
2267
+ }),
2268
+);
2269
+
2270
it.effect("stops an in-flight branch rename when tracked setup is cancelled", () =>
2271
Effect.gen(function* () {
2272
const setupEntered = yield* Deferred.make<void>();
@@ -2259,6 +2311,10 @@ it.effect("stops an in-flight branch rename when tracked setup is cancelled", ()
2311
);
2312
assert.equal(harness.renameBranch.mock.calls.length, 0);
2313
assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.worktreePath);
2314
+ assert.equal(
2315
+ harness.deleteLocalBranch.mock.calls[0]?.[0].refName,
2316
+ harness.createWorktree.mock.calls[0]?.[0].newRefName,
2317
+ );
2318
}).pipe(Effect.provide(harness.layer));
2319
}),
2320
);
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+31
-6
@@ -310,6 +310,7 @@ const make = Effect.gen(function* () {
310
const reused = input.reusedWorktree;
311
const tracked = input.workspaceStrategy.type === "worktree" || reused !== undefined;
312
let createdWorktreePath: string | null = null;
313
+ let createdBranch: string | null = null;
314
let setupTerminalId: string | null = null;
315
let workspaceRecorded = false;
316
let runReleased = false;
@@ -448,6 +449,7 @@ const make = Effect.gen(function* () {
449
onWorktreeClaimed: (path) =>
450
Effect.sync(() => {
451
createdWorktreePath = path;
452
+ createdBranch = branch;
453
}),
454
onCheckoutProgress: (progress) =>
455
setupTracker.stage(threadId, "checkout", { percent: progress.percent }),
@@ -458,6 +460,7 @@ const make = Effect.gen(function* () {
460
worktreePath = worktree.worktree.path;
461
branch = worktree.worktree.refName;
462
createdWorktreePath = worktreePath;
463
+ createdBranch = branch;
464
yield* setupTracker.update(threadId, (snapshot) => ({ ...snapshot, worktreePath, branch }));
465
yield* setupTracker.stageStatus(threadId, "checkout", "done");
466
}
@@ -487,12 +490,22 @@ const make = Effect.gen(function* () {
490
const worktreeCwd = worktreePath;
491
branchRename = yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
492
Effect.flatMap(({ branch: newBranch, exactName }) =>
490
- git.renameBranch({
491
- cwd: worktreeCwd,
492
- oldBranch,
493
- newBranch,
494
- ...(exactName ? { exactName: true } : {}),
495
- }),
493
+ git
494
+ .renameBranch({
495
+ cwd: worktreeCwd,
496
+ oldBranch,
497
+ newBranch,
498
+ ...(exactName ? { exactName: true } : {}),
499
+ })
500
+ .pipe(
501
+ // A cancel must see the renamed branch, or its cleanup deletes the old name.
502
+ Effect.tap((renamed) =>
503
+ Effect.sync(() => {
504
+ createdBranch = renamed.branch;
505
+ }),
506
+ ),
507
+ Effect.uninterruptible,
508
+ ),
509
),
510
Effect.flatMap((renamed) =>
511
Effect.gen(function* () {
@@ -680,10 +693,22 @@ const make = Effect.gen(function* () {
693
) {
694
if (branchRename !== null) yield* Fiber.interrupt(branchRename);
695
const removedPath = createdWorktreePath;
696
+ const removedBranch = createdBranch;
697
// Forget worktrees only after removal succeeds; failed removals must not reuse partially written checkouts.
698
yield* git
699
.removeWorktree({ cwd: project.workspaceRoot, path: removedPath, force: true })
700
.pipe(
701
+ Effect.andThen(
702
+ removedBranch === null
703
+ ? Effect.void
704
+ : git
705
+ .deleteLocalBranch({
706
+ cwd: project.workspaceRoot,
707
+ refName: removedBranch,
708
+ force: true,
709
+ })
710
+ .pipe(Effect.ignoreCause({ log: true })),
711
+ ),
712
Effect.andThen(
713
threads
714
.dispatch({