Skip to content

Commit 62c74a9

Browse files
saphidgithub-actions[bot]claude
authored
fix(server): stop the branch rename before cancelling worktree setup (pingdotgg#18443)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: SAPHID <[email protected]>
1 parent 66cae3d commit 62c74a9

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,55 @@ it.effect("cancels tracked setup before provider work is released", () =>
26192619
}),
26202620
);
26212621

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+
26222671
it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
26232672
Effect.gen(function* () {
26242673
const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Cause from "effect/Cause";
2323
import * as Context from "effect/Context";
2424
import * as Effect from "effect/Effect";
2525
import * as Exit from "effect/Exit";
26+
import * as Fiber from "effect/Fiber";
2627
import * as Layer from "effect/Layer";
2728
import * as Option from "effect/Option";
2829
import * as Ref from "effect/Ref";
@@ -307,6 +308,9 @@ const make = Effect.gen(function* () {
307308
let setupTerminalId: string | null = null;
308309
let workspaceRecorded = false;
309310
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;
310314
if (input.workspaceStrategy.type === "worktree") {
311315
yield* setupTracker.begin({
312316
threadId,
@@ -486,7 +490,7 @@ const make = Effect.gen(function* () {
486490
) {
487491
const oldBranch = branch;
488492
const worktreeCwd = worktreePath;
489-
yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
493+
branchRename = yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
490494
Effect.flatMap(({ branch: newBranch, exactName }) =>
491495
git.renameBranch({
492496
cwd: worktreeCwd,
@@ -683,6 +687,7 @@ const make = Effect.gen(function* () {
683687
createdWorktreePath &&
684688
((cancelled && !runReleased) || !workspaceRecorded)
685689
) {
690+
if (branchRename !== null) yield* Fiber.interrupt(branchRename);
686691
const removedPath = createdWorktreePath;
687692
// The thread forgets the worktree only once it is gone; a failed
688693
// removal leaves the directory for the user to clean up rather than

0 commit comments

Comments
 (0)