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 22, 08:15 UTC
fix(server): clean unchanged worktrees without a remote (#27175)
Co-authored-by: maria-rcks <maria@kuuro.net>
melvinmng committed
Nov 2, 2026 at 05:42 UTC
ff46c6b07c9455153e1b365c728c6d4ad772c860
3 files changed
+98
-37
apps/server/src/storageCleanup.test.ts
+30
@@ -2101,6 +2101,36 @@ describe("storage cleanup reports and local file policies", () => {
2101
}),
2102
),
2103
);
2104
+ it.live.each([
2105
+ ["unchanged", "removed", "No commits beyond the project branch"],
2106
+ ["unmerged", "kept", "Has commits beyond the project branch"],
2107
+ ["detached", "kept", "Project branch is unavailable"],
2108
+ ["merged-only", "kept", "No remote is configured for merged-worktree cleanup"],
2109
+ ] as const)("cleans archived local-only worktrees: %s", ([state, outcome, reason]) =>
2110
+ runCleanupTest(
2111
+ Effect.gen(function* () {
2112
+ const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
2113
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
2114
+ worktreeUnchanged: state !== "merged-only",
2115
+ worktreeOnMerge: true,
2116
+ });
2117
+ yield* command(repo, ["branch", "-m", "main", "trunk"]);
2118
+ setThreads([shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) })]);
2119
+ if (state === "unmerged") {
2120
+ yield* fs.writeFileString(`${worktree}/tracked.txt`, "unmerged work\n");
2121
+ yield* command(worktree, ["commit", "-am", "unmerged work"]);
2122
+ }
2123
+ if (state === "detached") yield* command(repo, ["checkout", "--detach"]);
2124
+
2125
+ const report = yield* service.runNow;
2126
+ expect(report.entries[0]).toMatchObject({ outcome, reason });
2127
+ expect(yield* fs.exists(worktree)).toBe(outcome === "kept");
2128
+ expect(
2129
+ (yield* command(repo, ["show-ref", "--verify", "refs/heads/feature"])).stdout,
2130
+ ).not.toBe("");
2131
+ }),
2132
+ ),
2133
+ );
2134
it.live(
2135
"discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
2136
() =>
apps/server/src/storageCleanup.ts
+65
-36
@@ -892,42 +892,66 @@ const make = Effect.gen(function* () {
892
: `settled for ${settledDays} days`;
893
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
894
const repositoryCwd = path.resolve(project.workspaceRoot);
895
- const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
896
- const defaultBranchKey = `${repositoryCwd}\0${remote}`;
897
- let branch = defaultBranches.get(defaultBranchKey);
898
- if (branch === undefined) {
899
- branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
900
- // Without a local HEAD ref, only the remote can identify its default;
901
- // a conventional name may be a feature branch.
902
- if (branch === null) {
903
- branch = yield* git
904
- .execute({
905
- operation: "StorageCleanup.resolveDefaultBranch",
906
- cwd: repositoryCwd,
907
- args: ["ls-remote", "--symref", "--", remote, "HEAD"],
908
- timeoutMs: 20_000,
909
- })
910
- .pipe(
911
- Effect.map(
912
- (result) =>
913
- /^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
914
- ),
915
- Effect.orElseSucceed(() => null),
916
- );
917
- }
918
- defaultBranches.set(defaultBranchKey, branch);
919
- }
920
- if (branch === null) return keep("default branch is unavailable");
921
- const defaultRef = `refs/remotes/${remote}/${branch}`;
922
- const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
923
- if (!refreshed.has(defaultRef)) {
924
- yield* git.fetchRemoteTrackingBranch({
895
+ const remotes = yield* git.execute({
896
+ operation: "StorageCleanup.listRemotes",
897
+ cwd: repositoryCwd,
898
+ args: ["remote"],
899
+ });
900
+ const remote =
901
+ remotes.stdout.trim() === ""
902
+ ? null
903
+ : yield* git.resolvePrimaryRemoteName(repositoryCwd);
904
+ let branch: string | null | undefined;
905
+ let defaultRef: string;
906
+ if (remote === null) {
907
+ if (!settings.worktreeUnchanged)
908
+ return keep("no remote is configured for merged-worktree cleanup");
909
+ const projectHead = yield* git.execute({
910
+ operation: "StorageCleanup.resolveProjectBranch",
911
cwd: repositoryCwd,
926
- remoteName: remote,
927
- remoteBranch: branch,
912
+ args: ["symbolic-ref", "--quiet", "HEAD"],
913
+ allowNonZeroExit: true,
914
});
929
- refreshed.add(defaultRef);
930
- refreshedDefaultRefs.set(repositoryCwd, refreshed);
915
+ if (projectHead.exitCode !== 0) return keep("project branch is unavailable");
916
+ defaultRef = projectHead.stdout.trim();
917
+ branch = defaultRef.slice("refs/heads/".length);
918
+ } else {
919
+ const defaultBranchKey = `${repositoryCwd}\0${remote}`;
920
+ branch = defaultBranches.get(defaultBranchKey);
921
+ if (branch === undefined) {
922
+ branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
923
+ // Without a local HEAD ref, only the remote can identify its default;
924
+ // a conventional name may be a feature branch.
925
+ if (branch === null) {
926
+ branch = yield* git
927
+ .execute({
928
+ operation: "StorageCleanup.resolveDefaultBranch",
929
+ cwd: repositoryCwd,
930
+ args: ["ls-remote", "--symref", "--", remote, "HEAD"],
931
+ timeoutMs: 20_000,
932
+ })
933
+ .pipe(
934
+ Effect.map(
935
+ (result) =>
936
+ /^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
937
+ ),
938
+ Effect.orElseSucceed(() => null),
939
+ );
940
+ }
941
+ defaultBranches.set(defaultBranchKey, branch);
942
+ }
943
+ if (branch === null) return keep("default branch is unavailable");
944
+ defaultRef = `refs/remotes/${remote}/${branch}`;
945
+ const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
946
+ if (!refreshed.has(defaultRef)) {
947
+ yield* git.fetchRemoteTrackingBranch({
948
+ cwd: repositoryCwd,
949
+ remoteName: remote,
950
+ remoteBranch: branch,
951
+ });
952
+ refreshed.add(defaultRef);
953
+ refreshedDefaultRefs.set(repositoryCwd, refreshed);
954
+ }
955
}
956
const base = yield* git.resolveCommit({
957
cwd: worktreePath,
@@ -941,8 +965,12 @@ const make = Effect.gen(function* () {
965
});
966
const integrated = ancestor.exitCode === 0;
967
eligible = integrated && settings.worktreeUnchanged;
944
- if (eligible) removalReason = "no commits beyond the default branch";
945
- if (!eligible && settings.worktreeOnMerge && thread.branch !== null) {
968
+ if (eligible)
969
+ removalReason =
970
+ remote === null
971
+ ? "no commits beyond the project branch"
972
+ : "no commits beyond the default branch";
973
+ if (!eligible && remote !== null && settings.worktreeOnMerge && thread.branch !== null) {
974
const pullRequest = yield* gitManager.branchPullRequest(
975
{ cwd: worktreePath, branch: thread.branch },
976
{ refresh: true },
@@ -955,6 +983,7 @@ const make = Effect.gen(function* () {
983
});
984
if (eligible) removalReason = "pull request was merged";
985
}
986
+ if (!eligible && remote === null) return keep("has commits beyond the project branch");
987
}
988
if (!eligible)
989
return keep(
docs/user/project-settings.md
+3
-1
@@ -206,7 +206,9 @@ captures and log retention remain machine-wide.
206
207
Worktrees can be removed after a chosen number of inactive days, after a thread settles, after
208
merging, or when they have no commits beyond the default branch. Set settled retention to 0 days
209
-to remove a worktree at the next cleanup after settlement. Only T3-managed worktrees are eligible.
209
+to remove a worktree at the next cleanup after settlement. In repositories without remotes,
210
+unchanged-worktree cleanup compares against the branch checked out in the project workspace;
211
+if that checkout has a detached HEAD, the worktree is kept. Only T3-managed worktrees are eligible.
212
Active sessions and shared worktrees prevent removal. **Keep worktrees with local changes** defaults
213
to **Uncommitted changes**, which protects tracked edits and untracked files but deletes ignored
214
files such as `.env` and build output. **Any local files** also protects ignored files other than