Skip to content
TypeScript

fix(server): storage cleanup finds the default branch when the remote has no HEAD ref (#17909)

Tristan Knight28f81abc2bd4
diff --git a/apps/server/src/storageCleanup.test.ts b/apps/server/src/storageCleanup.test.ts
index c99a0fa29ed..fe2c8b86fb2 100644
--- a/apps/server/src/storageCleanup.test.ts
+++ b/apps/server/src/storageCleanup.test.ts
@@ -1567,6 +1567,83 @@ describe("storage cleanup reports and local file policies", () => {
}),
),
);
+ it.live(
+ "discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
+ () =>
+ runCleanupTest(
+ Effect.gen(function* () {
+ const { service, fs, repo, worktree, command } = yield* cleanupFixture({
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
+ worktreeUnchanged: true,
+ });
+ const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
+ yield* command(remote, ["init", "--bare", "-b", "trunk"]);
+ yield* command(repo, ["remote", "add", "origin", remote]);
+ yield* command(repo, ["push", "origin", "main:trunk"]);
+ yield* command(repo, ["update-ref", "-d", "refs/remotes/origin/trunk"]);
+ expect((yield* command(repo, ["for-each-ref", "refs/remotes/origin"])).stdout).toBe("");
+
+ const report = yield* service.runNow;
+ expect(report.entries[0]).toMatchObject({
+ outcome: "removed",
+ reason: "No commits beyond the default branch",
+ });
+ expect(yield* fs.exists(worktree)).toBe(false);
+ }),
+ ),
+ );
+ it.live(
+ "keeps worktree commits present on nondefault main but absent from advertised trunk",
+ () =>
+ runCleanupTest(
+ Effect.gen(function* () {
+ const { service, fs, repo, worktree, command } = yield* cleanupFixture({
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
+ worktreeUnchanged: true,
+ });
+ const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
+ yield* command(remote, ["init", "--bare", "-b", "trunk"]);
+ yield* command(repo, ["remote", "add", "origin", remote]);
+ yield* command(repo, ["push", "origin", "main:trunk"]);
+ yield* fs.writeFileString(`${worktree}/tracked.txt`, "unmerged work\n");
+ yield* command(worktree, ["commit", "-am", "unmerged work"]);
+ yield* command(repo, ["push", "origin", "feature:main"]);
+ expect(
+ (yield* command(repo, ["symbolic-ref", "refs/remotes/origin/HEAD"]).pipe(Effect.exit))
+ ._tag,
+ ).toBe("Failure");
+
+ const report = yield* service.runNow;
+ expect(report.entries[0]).toMatchObject({
+ outcome: "kept",
+ reason: "Has commits beyond the default branch",
+ });
+ expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("unmerged work\n");
+ }),
+ ),
+ );
+ it.live("keeps the worktree when the remote default is unavailable even if main exists", () =>
+ runCleanupTest(
+ Effect.gen(function* () {
+ const { service, fs, repo, worktree, command } = yield* cleanupFixture({
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
+ worktreeUnchanged: true,
+ });
+ const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
+ yield* command(remote, ["init", "--bare", "-b", "trunk"]);
+ yield* command(repo, ["remote", "add", "origin", remote]);
+ // The remote HEAD names an unborn trunk; main exists but is not its default.
+ yield* command(repo, ["push", "origin", "main"]);
+
+ const report = yield* service.runNow;
+ expect(report.entries[0]).toMatchObject({
+ outcome: "kept",
+ reason: "Default branch is unavailable",
+ });
+ expect(yield* fs.exists(worktree)).toBe(true);
+ }),
+ ),
+ );
it.live("preserves tracked edits written after the final status check", () =>
runCleanupTest(
Effect.gen(function* () {
diff --git a/apps/server/src/storageCleanup.ts b/apps/server/src/storageCleanup.ts
index 16d2905ba04..74d24ffa211 100644
--- a/apps/server/src/storageCleanup.ts
+++ b/apps/server/src/storageCleanup.ts
@@ -592,6 +592,7 @@ const make = Effect.gen(function* () {
)).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
const snapshot = yield* readThreads();
const refreshedDefaultRefs = new Map<string, Set<string>>();
+ const defaultBranches = new Map<string, string | null>();
const groups = Map.groupBy(
snapshot.threads.filter((thread) => thread.worktreePath !== null),
(thread) => path.resolve(thread.worktreePath!),
@@ -684,7 +685,30 @@ const make = Effect.gen(function* () {
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
const repositoryCwd = path.resolve(project.workspaceRoot);
const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
- const branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
+ const defaultBranchKey = `${repositoryCwd}\0${remote}`;
+ let branch = defaultBranches.get(defaultBranchKey);
+ if (branch === undefined) {
+ branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
+ // Without a local HEAD ref, only the remote can identify its default;
+ // a conventional name may be a feature branch.
+ if (branch === null) {
+ branch = yield* git
+ .execute({
+ operation: "StorageCleanup.resolveDefaultBranch",
+ cwd: repositoryCwd,
+ args: ["ls-remote", "--symref", "--", remote, "HEAD"],
+ timeoutMs: 20_000,
+ })
+ .pipe(
+ Effect.map(
+ (result) =>
+ /^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
+ ),
+ Effect.orElseSucceed(() => null),
+ );
+ }
+ defaultBranches.set(defaultBranchKey, branch);
+ }
if (branch === null) return keep("default branch is unavailable");
const defaultRef = `refs/remotes/${remote}/${branch}`;
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();