The best way to control Claude Code, Codex and more from any computer or phone
fix(server): remove worktrees through their owning repository (#33586)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Luis Taniça committed
Nov 14, 2026 at 16:55 UTC
29d517dbac0b3807303f9c317abb6a7879093f6e
2 files changed
+48
-2
apps/server/src/storageCleanup.test.ts
+30
@@ -2704,6 +2704,36 @@ describe("storage cleanup reports and local file policies", () => {
2704
}),
2705
),
2706
);
2707
+ it.effect("removes a worktree owned by a repository other than the project root", () =>
2708
+ runCleanupTest(
2709
+ Effect.gen(function* () {
2710
+ yield* TestClock.setTime(NOW_MS);
2711
+ const fixture = yield* cleanupFixture();
2712
+ const { fs, command, config } = fixture;
2713
+ const oldRepo = yield* fs.realPath(
2714
+ yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-old-repo-" }),
2715
+ );
2716
+ yield* command(oldRepo, ["init", "-b", "main"]);
2717
+ yield* command(oldRepo, ["config", "user.email", "cleanup@example.test"]);
2718
+ yield* command(oldRepo, ["config", "user.name", "Cleanup test"]);
2719
+ yield* fs.writeFileString(`${oldRepo}/tracked.txt`, "original\n");
2720
+ yield* command(oldRepo, ["add", "."]);
2721
+ yield* command(oldRepo, ["commit", "-m", "initial"]);
2722
+ yield* fs.makeDirectory(`${config.worktreesDir}/old-repo`, { recursive: true });
2723
+ const oldWorktree = `${config.worktreesDir}/old-repo/feature`;
2724
+ yield* command(oldRepo, ["worktree", "add", "-b", "feature", oldWorktree]);
2725
+ fixture.setThreads([shell({ branch: "feature", worktreePath: oldWorktree })]);
2726
+ const report = yield* fixture.service.runNow;
2727
+ expect(report.entries.find((entry) => entry.path === oldWorktree)).toMatchObject({
2728
+ outcome: "removed",
2729
+ });
2730
+ expect(yield* fs.exists(oldWorktree)).toBe(false);
2731
+ expect((yield* command(oldRepo, ["worktree", "list", "--porcelain"])).stdout).not.toContain(
2732
+ oldWorktree,
2733
+ );
2734
+ }),
2735
+ ),
2736
+ );
2737
it.effect.each([
2738
[
2739
"its owner's branch is cleared",
apps/server/src/storageCleanup.ts
+18
-2
@@ -800,6 +800,21 @@ const make = Effect.gen(function* () {
800
return changed === undefined ? null : reason(changed.file);
801
});
802
803
+ // A worktree can outlive its project moving to another clone. Git removes a
804
+ // worktree through the repository that registered it, not through the
805
+ // project's current checkout, so resolve the owning repository first.
806
+ const worktreeRepository = Effect.fn("StorageCleanup.worktreeRepository")(function* (
807
+ worktreePath: string,
808
+ ) {
809
+ const commonDir = yield* git.execute({
810
+ operation: "StorageCleanup.worktreeRepository",
811
+ cwd: worktreePath,
812
+ args: ["rev-parse", "--git-common-dir"],
813
+ });
814
+ const resolved = path.resolve(worktreePath, commonDir.stdout.trim());
815
+ return path.basename(resolved) === ".git" ? path.dirname(resolved) : resolved;
816
+ });
817
+
818
const localChangesReason = Effect.fn("StorageCleanup.localChangesReason")(function* (
819
cwd: string,
820
rules: WorktreeCleanupRules,
@@ -1526,14 +1541,15 @@ const make = Effect.gen(function* () {
1541
const changes = yield* localChangesReason(worktreePath, settings);
1542
if (changes !== null) return keep(changes);
1543
}
1544
+ const repository = yield* worktreeRepository(worktreePath);
1545
yield* removeLinkedWorktree({
1546
operation: "StorageCleanup.cleanupGit",
1531
- cwd: project.workspaceRoot,
1547
+ cwd: repository,
1548
worktreePath,
1549
// Git requires force for submodules; recursive checks above cannot make removal atomic.
1550
force: submodules.force,
1551
execute: git.execute,
1536
- run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"),
1552
+ run: (args) => cleanupGit(repository, [...args], "git worktree remove"),
1553
});
1554
entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });
1555
yield* gitManager.invalidateStatus(project.workspaceRoot);