@devries-cameron20 / t3code Mirror · read-only / commits / 28f81abc2b
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 01:45 UTC

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

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Tristan Knight committed Oct 19, 2026 at 22:10 UTC 28f81abc2bd49afbeef2fabb35fb5427a87a85f1
2 files changed +102 -1
apps/server/src/storageCleanup.test.ts
+77
@@ -1567,6 +1567,83 @@ describe("storage cleanup reports and local file policies", () => {
1567 }),
1568 ),
1569 );
1570 + it.live(
1571 + "discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
1572 + () =>
1573 + runCleanupTest(
1574 + Effect.gen(function* () {
1575 + const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1576 + ...DEFAULT_SERVER_SETTINGS.storageCleanup,
1577 + worktreeUnchanged: true,
1578 + });
1579 + const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1580 + yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1581 + yield* command(repo, ["remote", "add", "origin", remote]);
1582 + yield* command(repo, ["push", "origin", "main:trunk"]);
1583 + yield* command(repo, ["update-ref", "-d", "refs/remotes/origin/trunk"]);
1584 + expect((yield* command(repo, ["for-each-ref", "refs/remotes/origin"])).stdout).toBe("");
1585 +
1586 + const report = yield* service.runNow;
1587 + expect(report.entries[0]).toMatchObject({
1588 + outcome: "removed",
1589 + reason: "No commits beyond the default branch",
1590 + });
1591 + expect(yield* fs.exists(worktree)).toBe(false);
1592 + }),
1593 + ),
1594 + );
1595 + it.live(
1596 + "keeps worktree commits present on nondefault main but absent from advertised trunk",
1597 + () =>
1598 + runCleanupTest(
1599 + Effect.gen(function* () {
1600 + const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1601 + ...DEFAULT_SERVER_SETTINGS.storageCleanup,
1602 + worktreeUnchanged: true,
1603 + });
1604 + const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1605 + yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1606 + yield* command(repo, ["remote", "add", "origin", remote]);
1607 + yield* command(repo, ["push", "origin", "main:trunk"]);
1608 + yield* fs.writeFileString(`${worktree}/tracked.txt`, "unmerged work\n");
1609 + yield* command(worktree, ["commit", "-am", "unmerged work"]);
1610 + yield* command(repo, ["push", "origin", "feature:main"]);
1611 + expect(
1612 + (yield* command(repo, ["symbolic-ref", "refs/remotes/origin/HEAD"]).pipe(Effect.exit))
1613 + ._tag,
1614 + ).toBe("Failure");
1615 +
1616 + const report = yield* service.runNow;
1617 + expect(report.entries[0]).toMatchObject({
1618 + outcome: "kept",
1619 + reason: "Has commits beyond the default branch",
1620 + });
1621 + expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("unmerged work\n");
1622 + }),
1623 + ),
1624 + );
1625 + it.live("keeps the worktree when the remote default is unavailable even if main exists", () =>
1626 + runCleanupTest(
1627 + Effect.gen(function* () {
1628 + const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1629 + ...DEFAULT_SERVER_SETTINGS.storageCleanup,
1630 + worktreeUnchanged: true,
1631 + });
1632 + const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1633 + yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1634 + yield* command(repo, ["remote", "add", "origin", remote]);
1635 + // The remote HEAD names an unborn trunk; main exists but is not its default.
1636 + yield* command(repo, ["push", "origin", "main"]);
1637 +
1638 + const report = yield* service.runNow;
1639 + expect(report.entries[0]).toMatchObject({
1640 + outcome: "kept",
1641 + reason: "Default branch is unavailable",
1642 + });
1643 + expect(yield* fs.exists(worktree)).toBe(true);
1644 + }),
1645 + ),
1646 + );
1647 it.live("preserves tracked edits written after the final status check", () =>
1648 runCleanupTest(
1649 Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+25 -1
@@ -592,6 +592,7 @@ const make = Effect.gen(function* () {
592 )).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
593 const snapshot = yield* readThreads();
594 const refreshedDefaultRefs = new Map<string, Set<string>>();
595 + const defaultBranches = new Map<string, string | null>();
596 const groups = Map.groupBy(
597 snapshot.threads.filter((thread) => thread.worktreePath !== null),
598 (thread) => path.resolve(thread.worktreePath!),
@@ -684,7 +685,30 @@ const make = Effect.gen(function* () {
685 if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
686 const repositoryCwd = path.resolve(project.workspaceRoot);
687 const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
687 - const branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
688 + const defaultBranchKey = `${repositoryCwd}\0${remote}`;
689 + let branch = defaultBranches.get(defaultBranchKey);
690 + if (branch === undefined) {
691 + branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
692 + // Without a local HEAD ref, only the remote can identify its default;
693 + // a conventional name may be a feature branch.
694 + if (branch === null) {
695 + branch = yield* git
696 + .execute({
697 + operation: "StorageCleanup.resolveDefaultBranch",
698 + cwd: repositoryCwd,
699 + args: ["ls-remote", "--symref", "--", remote, "HEAD"],
700 + timeoutMs: 20_000,
701 + })
702 + .pipe(
703 + Effect.map(
704 + (result) =>
705 + /^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
706 + ),
707 + Effect.orElseSucceed(() => null),
708 + );
709 + }
710 + defaultBranches.set(defaultBranchKey, branch);
711 + }
712 if (branch === null) return keep("default branch is unavailable");
713 const defaultRef = `refs/remotes/${remote}/${branch}`;
714 const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();