@devries-cameron20 / t3code-1 / commits / 62c74a92f9
Fork 0

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

fix(server): stop the branch rename before cancelling worktree setup (#18443)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: SAPHID <saphid@users.noreply.github.com>

Alex Southwell committed Oct 19, 2026 at 07:06 UTC 62c74a92f9ccef521489f19ea02b7075dc00b18c
2 files changed +55 -1
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+49
@@ -2619,6 +2619,55 @@ it.effect("cancels tracked setup before provider work is released", () =>
2619 }),
2620 );
2621
2622 +it.effect("stops an in-flight branch rename when tracked setup is cancelled", () =>
2623 + Effect.gen(function* () {
2624 + const setupEntered = yield* Deferred.make<void>();
2625 + const branchNameStarted = yield* Deferred.make<void>();
2626 + const allowBranchName = yield* Deferred.make<void>();
2627 + const branchNameInterrupted = yield* Deferred.make<void>();
2628 + const renameAttempted = yield* Deferred.make<void>();
2629 + const harness = makeHarness({
2630 + renameBranch: (input) =>
2631 + Deferred.succeed(renameAttempted, undefined).pipe(Effect.as({ branch: input.newBranch })),
2632 + runSetup: () => Deferred.succeed(setupEntered, undefined).pipe(Effect.andThen(Effect.never)),
2633 + generateBranchName: () =>
2634 + Deferred.succeed(branchNameStarted, undefined).pipe(
2635 + Effect.andThen(Deferred.await(allowBranchName)),
2636 + Effect.as({ branch: "generated-branch" }),
2637 + Effect.onInterrupt(() => Deferred.succeed(branchNameInterrupted, undefined)),
2638 + ),
2639 + });
2640 + yield* Effect.gen(function* () {
2641 + const launches = yield* ThreadLaunch.ThreadLaunchService;
2642 + const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2643 + const threads = yield* ThreadManagement.ThreadManagementService;
2644 + const launched = yield* launches.launch(
2645 + launchInput({
2646 + command: "launch:cancel-renaming",
2647 + thread: "thread:cancel-renaming",
2648 + message: "Start",
2649 + workspace: { type: "worktree", baseRef: "main" },
2650 + }),
2651 + );
2652 + yield* Deferred.await(setupEntered);
2653 + yield* Deferred.await(branchNameStarted);
2654 + assert.isTrue(yield* tracker.cancel(launched.threadId));
2655 + // Let a rename that survived the cancel finish, then check it never ran.
2656 + yield* Deferred.succeed(allowBranchName, undefined);
2657 + yield* Effect.raceFirst(
2658 + Deferred.await(branchNameInterrupted),
2659 + Deferred.await(renameAttempted),
2660 + );
2661 + assert.isTrue(
2662 + yield* Deferred.isDone(branchNameInterrupted),
2663 + "the cancelled setup's branch rename kept running",
2664 + );
2665 + assert.equal(harness.renameBranch.mock.calls.length, 0);
2666 + assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.worktreePath);
2667 + }).pipe(Effect.provide(harness.layer));
2668 + }),
2669 +);
2670 +
2671 it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
2672 Effect.gen(function* () {
2673 const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>();
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+6 -1
@@ -23,6 +23,7 @@ import * as Cause from "effect/Cause";
23 import * as Context from "effect/Context";
24 import * as Effect from "effect/Effect";
25 import * as Exit from "effect/Exit";
26 +import * as Fiber from "effect/Fiber";
27 import * as Layer from "effect/Layer";
28 import * as Option from "effect/Option";
29 import * as Ref from "effect/Ref";
@@ -307,6 +308,9 @@ const make = Effect.gen(function* () {
308 let setupTerminalId: string | null = null;
309 let workspaceRecorded = false;
310 let runReleased = false;
311 + // The background branch rename records the worktree again when it lands,
312 + // so cleanup stops it before forgetting a removed worktree.
313 + let branchRename: Fiber.Fiber<unknown> | null = null;
314 if (input.workspaceStrategy.type === "worktree") {
315 yield* setupTracker.begin({
316 threadId,
@@ -486,7 +490,7 @@ const make = Effect.gen(function* () {
490 ) {
491 const oldBranch = branch;
492 const worktreeCwd = worktreePath;
489 - yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
493 + branchRename = yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
494 Effect.flatMap(({ branch: newBranch, exactName }) =>
495 git.renameBranch({
496 cwd: worktreeCwd,
@@ -683,6 +687,7 @@ const make = Effect.gen(function* () {
687 createdWorktreePath &&
688 ((cancelled && !runReleased) || !workspaceRecorded)
689 ) {
690 + if (branchRename !== null) yield* Fiber.interrupt(branchRename);
691 const removedPath = createdWorktreePath;
692 // The thread forgets the worktree only once it is gone; a failed
693 // removal leaves the directory for the user to clean up rather than