Skip to content

Commit f76a4eb

Browse files
committed
fix(server): clean unchanged worktrees without a remote
1 parent dd14848 commit f76a4eb

3 files changed

Lines changed: 97 additions & 37 deletions

File tree

‎apps/server/src/storageCleanup.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,35 @@ describe("storage cleanup reports and local file policies", () => {
19581958
}),
19591959
),
19601960
);
1961+
it.live.each([
1962+
["unchanged", "removed", "No commits beyond the project branch"],
1963+
["unmerged", "kept", "Has commits beyond the project branch"],
1964+
["detached", "kept", "Project branch is unavailable"],
1965+
] as const)("cleans archived local-only worktrees: %s", ([state, outcome, reason]) =>
1966+
runCleanupTest(
1967+
Effect.gen(function* () {
1968+
const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
1969+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
1970+
worktreeUnchanged: true,
1971+
worktreeOnMerge: true,
1972+
});
1973+
yield* command(repo, ["branch", "-m", "main", "trunk"]);
1974+
setThreads([shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) })]);
1975+
if (state === "unmerged") {
1976+
yield* fs.writeFileString(`${worktree}/tracked.txt`, "unmerged work\n");
1977+
yield* command(worktree, ["commit", "-am", "unmerged work"]);
1978+
}
1979+
if (state === "detached") yield* command(repo, ["checkout", "--detach"]);
1980+
1981+
const report = yield* service.runNow;
1982+
expect(report.entries[0]).toMatchObject({ outcome, reason });
1983+
expect(yield* fs.exists(worktree)).toBe(outcome === "kept");
1984+
expect(
1985+
(yield* command(repo, ["show-ref", "--verify", "refs/heads/feature"])).stdout,
1986+
).not.toBe("");
1987+
}),
1988+
),
1989+
);
19611990
it.live(
19621991
"discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
19631992
() =>

‎apps/server/src/storageCleanup.ts‎

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -851,42 +851,66 @@ const make = Effect.gen(function* () {
851851
: `settled for ${settledDays} days`;
852852
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
853853
const repositoryCwd = path.resolve(project.workspaceRoot);
854-
const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
855-
const defaultBranchKey = `${repositoryCwd}\0${remote}`;
856-
let branch = defaultBranches.get(defaultBranchKey);
857-
if (branch === undefined) {
858-
branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
859-
// Without a local HEAD ref, only the remote can identify its default;
860-
// a conventional name may be a feature branch.
861-
if (branch === null) {
862-
branch = yield* git
863-
.execute({
864-
operation: "StorageCleanup.resolveDefaultBranch",
865-
cwd: repositoryCwd,
866-
args: ["ls-remote", "--symref", "--", remote, "HEAD"],
867-
timeoutMs: 20_000,
868-
})
869-
.pipe(
870-
Effect.map(
871-
(result) =>
872-
/^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
873-
),
874-
Effect.orElseSucceed(() => null),
875-
);
876-
}
877-
defaultBranches.set(defaultBranchKey, branch);
878-
}
879-
if (branch === null) return keep("default branch is unavailable");
880-
const defaultRef = `refs/remotes/${remote}/${branch}`;
881-
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
882-
if (!refreshed.has(defaultRef)) {
883-
yield* git.fetchRemoteTrackingBranch({
854+
const remotes = yield* git.execute({
855+
operation: "StorageCleanup.listRemotes",
856+
cwd: repositoryCwd,
857+
args: ["remote"],
858+
});
859+
const remote =
860+
remotes.stdout.trim() === ""
861+
? null
862+
: yield* git.resolvePrimaryRemoteName(repositoryCwd);
863+
let branch: string | null | undefined;
864+
let defaultRef: string;
865+
if (remote === null) {
866+
if (!settings.worktreeUnchanged)
867+
return keep("no remote is configured for merged-worktree cleanup");
868+
const projectHead = yield* git.execute({
869+
operation: "StorageCleanup.resolveProjectBranch",
884870
cwd: repositoryCwd,
885-
remoteName: remote,
886-
remoteBranch: branch,
871+
args: ["symbolic-ref", "--quiet", "HEAD"],
872+
allowNonZeroExit: true,
887873
});
888-
refreshed.add(defaultRef);
889-
refreshedDefaultRefs.set(repositoryCwd, refreshed);
874+
if (projectHead.exitCode !== 0) return keep("project branch is unavailable");
875+
defaultRef = projectHead.stdout.trim();
876+
branch = defaultRef.slice("refs/heads/".length);
877+
} else {
878+
const defaultBranchKey = `${repositoryCwd}\0${remote}`;
879+
branch = defaultBranches.get(defaultBranchKey);
880+
if (branch === undefined) {
881+
branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
882+
// Without a local HEAD ref, only the remote can identify its default;
883+
// a conventional name may be a feature branch.
884+
if (branch === null) {
885+
branch = yield* git
886+
.execute({
887+
operation: "StorageCleanup.resolveDefaultBranch",
888+
cwd: repositoryCwd,
889+
args: ["ls-remote", "--symref", "--", remote, "HEAD"],
890+
timeoutMs: 20_000,
891+
})
892+
.pipe(
893+
Effect.map(
894+
(result) =>
895+
/^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
896+
),
897+
Effect.orElseSucceed(() => null),
898+
);
899+
}
900+
defaultBranches.set(defaultBranchKey, branch);
901+
}
902+
if (branch === null) return keep("default branch is unavailable");
903+
defaultRef = `refs/remotes/${remote}/${branch}`;
904+
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
905+
if (!refreshed.has(defaultRef)) {
906+
yield* git.fetchRemoteTrackingBranch({
907+
cwd: repositoryCwd,
908+
remoteName: remote,
909+
remoteBranch: branch,
910+
});
911+
refreshed.add(defaultRef);
912+
refreshedDefaultRefs.set(repositoryCwd, refreshed);
913+
}
890914
}
891915
const base = yield* git.resolveCommit({
892916
cwd: worktreePath,
@@ -900,8 +924,12 @@ const make = Effect.gen(function* () {
900924
});
901925
const integrated = ancestor.exitCode === 0;
902926
eligible = integrated && settings.worktreeUnchanged;
903-
if (eligible) removalReason = "no commits beyond the default branch";
904-
if (!eligible && settings.worktreeOnMerge && thread.branch !== null) {
927+
if (eligible)
928+
removalReason =
929+
remote === null
930+
? "no commits beyond the project branch"
931+
: "no commits beyond the default branch";
932+
if (!eligible && remote !== null && settings.worktreeOnMerge && thread.branch !== null) {
905933
const pullRequest = yield* gitManager.branchPullRequest(
906934
{ cwd: worktreePath, branch: thread.branch },
907935
{ refresh: true },
@@ -914,6 +942,7 @@ const make = Effect.gen(function* () {
914942
});
915943
if (eligible) removalReason = "pull request was merged";
916944
}
945+
if (!eligible && remote === null) return keep("has commits beyond the project branch");
917946
}
918947
if (!eligible)
919948
return keep(

‎docs/user/project-settings.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ captures and log retention remain machine-wide.
188188

189189
Worktrees can be removed after a chosen number of inactive days, after a thread settles, after
190190
merging, or when they have no commits beyond the default branch. Set settled retention to 0 days
191-
to remove a worktree at the next cleanup after settlement. Only T3-managed worktrees are eligible.
191+
to remove a worktree at the next cleanup after settlement. In repositories without remotes,
192+
unchanged-worktree cleanup compares against the branch checked out in the project workspace;
193+
if that checkout has a detached HEAD, the worktree is kept. Only T3-managed worktrees are eligible.
192194
Active sessions and shared worktrees prevent removal. **Keep worktrees with local changes** defaults
193195
to **Uncommitted changes**, which protects tracked edits and untracked files but deletes ignored
194196
files such as `.env` and build output. **Any local files** also protects ignored files other than

0 commit comments

Comments
 (0)