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 21, 17:15 UTC
fix(server): unchanged-worktree cleanup waits for the thread to settle or archive (#35941)
Julius Marminge committed
Nov 12, 2026 at 20:34 UTC
ec918277f0a72b1e0c74a55f75f8a68e93405250
3 files changed
+67
-13
apps/server/src/storageCleanup.test.ts
+47
-3
@@ -2799,15 +2799,55 @@ describe("storage cleanup reports and local file policies", () => {
2799
}),
2800
),
2801
);
2802
+ it.live("keeps an unsettled new thread's unchanged worktree after its first turn", () =>
2803
+ runCleanupTest(
2804
+ Effect.gen(function* () {
2805
+ const { service, fs, worktree, setThreads } = yield* cleanupFixture({
2806
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
2807
+ worktreeSettledAfterDays: 11,
2808
+ worktreeOnMerge: true,
2809
+ worktreeUnchanged: true,
2810
+ });
2811
+ const now = yield* DateTime.now;
2812
+ const minutesAgo = (minutes: number) => DateTime.subtract(now, { minutes });
2813
+ const thread = shell({
2814
+ branch: "feature",
2815
+ worktreePath: worktree,
2816
+ status: "completed",
2817
+ createdAt: minutesAgo(40),
2818
+ latestUserMessageAt: minutesAgo(40),
2819
+ latestRunRequestedAt: minutesAgo(40),
2820
+ latestRunStartedAt: minutesAgo(40),
2821
+ latestRunCompletedAt: minutesAgo(20),
2822
+ updatedAt: minutesAgo(20),
2823
+ });
2824
+ setThreads([thread]);
2825
+
2826
+ const report = yield* service.runNow;
2827
+ expect(report.entries[0]).toMatchObject({ outcome: "kept" });
2828
+ expect(yield* fs.exists(worktree)).toBe(true);
2829
+
2830
+ setThreads([{ ...thread, settledAt: minutesAgo(10), updatedAt: minutesAgo(10) }]);
2831
+ const settled = yield* service.runNow;
2832
+ expect(settled.entries[0]).toMatchObject({
2833
+ outcome: "removed",
2834
+ reason: "No commits beyond the project branch",
2835
+ });
2836
+ }),
2837
+ ),
2838
+ );
2839
it.live(
2840
"discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
2841
() =>
2842
runCleanupTest(
2843
Effect.gen(function* () {
2807
- const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2844
+ const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
2845
...DEFAULT_SERVER_SETTINGS.storageCleanup,
2846
worktreeUnchanged: true,
2847
});
2848
+ setThreads([
2849
+ shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) }),
2850
+ ]);
2851
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
2852
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
2853
yield* command(repo, ["remote", "add", "origin", remote]);
@@ -2829,10 +2869,13 @@ describe("storage cleanup reports and local file policies", () => {
2869
() =>
2870
runCleanupTest(
2871
Effect.gen(function* () {
2832
- const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2872
+ const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
2873
...DEFAULT_SERVER_SETTINGS.storageCleanup,
2874
worktreeUnchanged: true,
2875
});
2876
+ setThreads([
2877
+ shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) }),
2878
+ ]);
2879
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
2880
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
2881
yield* command(repo, ["remote", "add", "origin", remote]);
@@ -2857,10 +2900,11 @@ describe("storage cleanup reports and local file policies", () => {
2900
it.live("keeps the worktree when the remote default is unavailable even if main exists", () =>
2901
runCleanupTest(
2902
Effect.gen(function* () {
2860
- const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2903
+ const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
2904
...DEFAULT_SERVER_SETTINGS.storageCleanup,
2905
worktreeUnchanged: true,
2906
});
2907
+ setThreads([shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) })]);
2908
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
2909
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
2910
yield* command(repo, ["remote", "add", "origin", remote]);
apps/server/src/storageCleanup.ts
+17
-8
@@ -1174,6 +1174,9 @@ const make = Effect.gen(function* () {
1174
settledFor !== null &&
1175
settings.worktreeSettledAfterDays !== null &&
1176
settledFor >= settings.worktreeSettledAfterDays * DAY_MS;
1177
+ // A worktree is unchanged before its first commit, so only finished threads qualify.
1178
+ const unchangedApplies =
1179
+ settings.worktreeUnchanged && (settledThread !== undefined || thread.archivedAt !== null);
1180
let eligible = deleted || old || settled;
1181
let removalReason = deleted
1182
? "thread was deleted"
@@ -1189,12 +1192,13 @@ const make = Effect.gen(function* () {
1192
: `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
1193
: settings.worktreeOnMerge
1194
? "not merged"
1192
- : settings.worktreeUnchanged
1195
+ : unchangedApplies
1196
? "has commits beyond the default branch"
1194
- : "no rules apply";
1197
+ : settings.worktreeUnchanged
1198
+ ? "not settled"
1199
+ : "no rules apply";
1200
// Only Git-based rules can make a not-yet-due worktree eligible, so skip Git otherwise.
1196
- if (!eligible && !settings.worktreeUnchanged && !settings.worktreeOnMerge)
1197
- return keep(notDueReason);
1201
+ if (!eligible && !unchangedApplies && !settings.worktreeOnMerge) return keep(notDueReason);
1202
const pathReason = yield* worktreePathReason(worktreePath);
1203
if (pathReason !== null) return keep(pathReason);
1204
if (yield* containsProjectRoot(worktreePaths, [project, ...snapshot.projects]))
@@ -1218,8 +1222,12 @@ const make = Effect.gen(function* () {
1222
let branch: string | null | undefined;
1223
let defaultRef: string;
1224
if (remote === null) {
1221
- if (!settings.worktreeUnchanged)
1222
- return keep("no remote is configured for merged-worktree cleanup");
1225
+ if (!unchangedApplies)
1226
+ return keep(
1227
+ settings.worktreeUnchanged
1228
+ ? notDueReason
1229
+ : "no remote is configured for merged-worktree cleanup",
1230
+ );
1231
const projectHead = yield* git.execute({
1232
operation: "StorageCleanup.resolveProjectBranch",
1233
cwd: repositoryCwd,
@@ -1278,7 +1286,7 @@ const make = Effect.gen(function* () {
1286
allowNonZeroExit: true,
1287
});
1288
const integrated = ancestor.exitCode === 0;
1281
- eligible = integrated && settings.worktreeUnchanged;
1289
+ eligible = integrated && unchangedApplies;
1290
if (eligible)
1291
removalReason =
1292
remote === null
@@ -1297,7 +1305,8 @@ const make = Effect.gen(function* () {
1305
});
1306
if (eligible) removalReason = "pull request was merged";
1307
}
1300
- if (!eligible && remote === null) return keep("has commits beyond the project branch");
1308
+ if (!eligible && remote === null)
1309
+ return keep(integrated ? notDueReason : "has commits beyond the project branch");
1310
}
1311
if (!eligible) return keep(notDueReason);
1312
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
docs/user/project-settings.md
+3
-2
@@ -227,8 +227,9 @@ manually. Custom applies separate worktree rules to the selected project or chec
227
captures and log retention remain machine-wide.
228
229
Worktrees can be removed after a chosen number of inactive days, after a thread settles, after
230
-merging, or when they have no commits beyond the default branch. Set settled retention to 0 days
231
-to remove a worktree at the next cleanup after settlement. In repositories without remotes,
230
+merging, or when they have no commits beyond the default branch and their thread is settled or
231
+archived. Set settled retention to 0 days to remove a worktree at the next cleanup after
232
+settlement. In repositories without remotes,
233
unchanged-worktree cleanup compares against the branch checked out in the project workspace;
234
if that checkout has a detached HEAD, the worktree is kept. Only T3-managed worktrees are eligible.
235
Active sessions and shared worktrees prevent removal. **Keep worktrees with local changes** defaults