Skip to content

Commit 49ec42e

Browse files
committed
fix(server): clean up interrupted worktree checkout
1 parent 23b3793 commit 49ec42e

2 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,65 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
15571557
});
15581558

15591559
describe("worktree operations", () => {
1560+
for (const state of ["new", "new-missing", "existing", "existing-missing"] as const) {
1561+
it.effect(`cleans up only newly registered worktrees on interruption: ${state}`, () =>
1562+
Effect.gen(function* () {
1563+
const cwd = yield* makeTmpDir();
1564+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1565+
const fs = yield* FileSystem.FileSystem;
1566+
const path = yield* Path.Path;
1567+
const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "interrupted");
1568+
const existing = state.startsWith("existing");
1569+
if (existing) {
1570+
yield* git(cwd, ["worktree", "add", "-b", "feature/existing", worktreePath]);
1571+
if (state === "existing-missing") {
1572+
yield* fs.remove(worktreePath, { recursive: true });
1573+
}
1574+
}
1575+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1576+
const commandFinished = yield* Deferred.make<void>();
1577+
const spawner = ChildProcessSpawner.make((command) =>
1578+
Effect.gen(function* () {
1579+
const handle = yield* delegate.spawn(command);
1580+
return ChildProcess.isStandardCommand(command) &&
1581+
command.args.includes("worktree") &&
1582+
command.args.includes("add")
1583+
? ChildProcessSpawner.makeHandle({
1584+
...handle,
1585+
exitCode: handle.exitCode.pipe(
1586+
Effect.andThen(Deferred.succeed(commandFinished, undefined)),
1587+
Effect.andThen(Effect.never),
1588+
),
1589+
})
1590+
: handle;
1591+
}),
1592+
);
1593+
const driver = yield* makeGitVcsDriverCore().pipe(
1594+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1595+
Effect.provide(ServerConfigLayer),
1596+
);
1597+
const creating = yield* driver
1598+
.createWorktree({
1599+
cwd,
1600+
path: worktreePath,
1601+
refName: initialBranch,
1602+
newRefName: "feature/interrupted",
1603+
})
1604+
.pipe(Effect.forkChild({ startImmediately: true }));
1605+
yield* Deferred.await(commandFinished);
1606+
if (state === "new-missing") {
1607+
yield* fs.remove(worktreePath, { recursive: true });
1608+
}
1609+
yield* Fiber.interrupt(creating);
1610+
1611+
assert.equal(yield* fs.exists(worktreePath), state === "existing");
1612+
const registered = yield* git(cwd, ["worktree", "list", "--porcelain", "-z"]);
1613+
assert.equal(registered.includes("feature/existing"), existing);
1614+
assert.notInclude(registered, "feature/interrupted");
1615+
}),
1616+
);
1617+
}
1618+
15601619
it.effect("uses parallel checkout without skipping filters or hooks", () =>
15611620
Effect.gen(function* () {
15621621
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,22 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30433043
const onCheckoutProgress = progress?.onCheckoutProgress;
30443044

30453045
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
3046+
const registeredWorktrees = yield* runGitStdout(
3047+
"GitVcsDriver.createWorktree.registeredPaths",
3048+
input.cwd,
3049+
["worktree", "list", "--porcelain", "-z"],
3050+
);
3051+
const worktreeAlreadyRegistered =
3052+
registeredWorktrees
3053+
.split("\0")
3054+
.some(
3055+
(field) =>
3056+
field.startsWith("worktree ") &&
3057+
path.resolve(field.slice("worktree ".length)) === path.resolve(worktreePath),
3058+
) ||
3059+
(yield* fileSystem
3060+
.exists(path.join(worktreePath, ".git"))
3061+
.pipe(Effect.orElseSucceed(() => true)));
30463062
yield* executeGit(
30473063
"GitVcsDriver.createWorktree",
30483064
input.cwd,
@@ -3064,12 +3080,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30643080
}
30653081
: {}),
30663082
},
3083+
).pipe(
3084+
Effect.tap(() => progress?.onWorktreeClaimed?.(worktreePath) ?? Effect.void),
3085+
Effect.onInterrupt(() =>
3086+
worktreeAlreadyRegistered
3087+
? Effect.void
3088+
: removeWorktree({ cwd: input.cwd, path: worktreePath, force: true }).pipe(
3089+
Effect.ignoreCause({ log: true }),
3090+
),
3091+
),
30673092
);
30683093

3069-
if (progress?.onWorktreeClaimed) {
3070-
yield* progress.onWorktreeClaimed(worktreePath);
3071-
}
3072-
30733094
// `git worktree add` leaves submodules empty, so a repo that keeps agent
30743095
// skills, tooling or source in one gets a worktree that is quietly missing
30753096
// them. Best-effort: the objects are usually already in the parent's

0 commit comments

Comments
 (0)