@devries-cameron20 / t3code-1 / commits / b68d29b2f7
Fork 0

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

fix(server): storage cleanup removes clean detached worktrees whose commits are on a branch (#34967)

Julius Marminge committed Nov 19, 2026 at 12:32 UTC b68d29b2f75812f147e09db5cdd4f12c32b4ff19
2 files changed +59 -3
apps/server/src/storageCleanup.test.ts
+28 -2
@@ -3261,8 +3261,6 @@ describe("storage cleanup reports and local file policies", () => {
3261 const { service, fs, repo, worktree, command, fullStatusCalls, threadBranches } =
3262 yield* cleanupFixture();
3263 const changed = { outcome: "kept", reason: "Repository or branch changed" };
3264 - yield* command(worktree, ["checkout", "--detach"]);
3265 - expect((yield* service.runNow).entries[0]).toMatchObject(changed);
3264 yield* command(worktree, ["switch", "--orphan", "other"]);
3265 expect((yield* service.runNow).entries[0]).toMatchObject(changed);
3266 yield* command(worktree, ["tag", "feature", "feature"]);
@@ -3293,6 +3291,34 @@ describe("storage cleanup reports and local file policies", () => {
3291 }),
3292 ),
3293 );
3294 + it.live("removes a clean detached worktree only when every commit is on a branch", () =>
3295 + runCleanupTest(
3296 + Effect.gen(function* () {
3297 + const { service, fs, worktree, command } = yield* cleanupFixture();
3298 + yield* command(worktree, ["checkout", "--detach"]);
3299 + yield* fs.writeFileString(`${worktree}/tracked.txt`, "unpushed\n");
3300 + yield* command(worktree, ["commit", "-am", "unpushed"]);
3301 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
3302 + yield* command(worktree, ["checkout", "--detach", "main"]);
3303 + yield* command(worktree, ["-c", "sequence.editor=echo break >>", "rebase", "-i", "--root"]);
3304 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
3305 + yield* command(worktree, ["rebase", "--abort"]);
3306 + yield* command(worktree, ["checkout", "-b", "topic", "main"]);
3307 + yield* command(worktree, ["commit", "--allow-empty", "-m", "topic"]);
3308 + yield* command(worktree, ["checkout", "--detach", "main"]);
3309 + yield* fs.writeFileString(`${worktree}/tracked.txt`, "autostashed\n");
3310 + yield* command(worktree, ["merge", "--autostash", "--no-ff", "--no-commit", "topic"]);
3311 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
3312 + yield* command(worktree, ["merge", "--abort"]);
3313 + expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("autostashed\n");
3314 + yield* command(worktree, ["checkout", "--", "tracked.txt"]);
3315 + yield* command(worktree, ["bisect", "start"]);
3316 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
3317 + yield* command(worktree, ["bisect", "reset"]);
3318 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "removed" });
3319 + }),
3320 + ),
3321 + );
3322 it.live.each([
3323 ["any-local-files", ".env", "kept"],
3324 ["uncommitted-changes", ".env", "removed"],
apps/server/src/storageCleanup.ts
+31 -1
@@ -729,7 +729,37 @@ const make = Effect.gen(function* () {
729 // Detached HEAD exits 1; full refs avoid shortening when a tag shares the branch name.
730 const head = yield* read(["symbolic-ref", "--quiet", "HEAD"], 1);
731 if (head === null) return true;
732 - const ref = head.exitCode === 0 ? head.stdout.trim() : "";
732 + // Removal keeps branches but drops a detached HEAD, so only commits held elsewhere are safe.
733 + if (head.exitCode !== 0) {
734 + // A paused rebase, merge or bisect would lose its progress (and any autostash) on removal.
735 + const operations = yield* read([
736 + "rev-parse",
737 + "--git-path",
738 + "rebase-merge",
739 + "--git-path",
740 + "rebase-apply",
741 + "--git-path",
742 + "MERGE_HEAD",
743 + "--git-path",
744 + "MERGE_AUTOSTASH",
745 + "--git-path",
746 + "BISECT_LOG",
747 + ]);
748 + if (operations === null) return true;
749 + for (const state of operations.stdout.trim().split(/\r?\n/)) {
750 + if (yield* fs.exists(path.resolve(cwd, state))) return true;
751 + }
752 + const unreachable = yield* read([
753 + "rev-list",
754 + "--max-count=1",
755 + "HEAD",
756 + "--not",
757 + "--branches",
758 + "--remotes",
759 + ]);
760 + return unreachable === null || unreachable.stdout.trim() !== "";
761 + }
762 + const ref = head.stdout.trim();
763 const name = ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length).trim() : null;
764 return name !== branch;
765 });