Skip to content

Commit ec91827

Browse files
fix(server): unchanged-worktree cleanup waits for the thread to settle or archive (pingdotgg#35941)
1 parent 67d91a0 commit ec91827

3 files changed

Lines changed: 67 additions & 13 deletions

File tree

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,15 +2799,55 @@ describe("storage cleanup reports and local file policies", () => {
27992799
}),
28002800
),
28012801
);
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+
);
28022839
it.live(
28032840
"discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
28042841
() =>
28052842
runCleanupTest(
28062843
Effect.gen(function* () {
2807-
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2844+
const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
28082845
...DEFAULT_SERVER_SETTINGS.storageCleanup,
28092846
worktreeUnchanged: true,
28102847
});
2848+
setThreads([
2849+
shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) }),
2850+
]);
28112851
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
28122852
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
28132853
yield* command(repo, ["remote", "add", "origin", remote]);
@@ -2829,10 +2869,13 @@ describe("storage cleanup reports and local file policies", () => {
28292869
() =>
28302870
runCleanupTest(
28312871
Effect.gen(function* () {
2832-
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2872+
const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
28332873
...DEFAULT_SERVER_SETTINGS.storageCleanup,
28342874
worktreeUnchanged: true,
28352875
});
2876+
setThreads([
2877+
shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) }),
2878+
]);
28362879
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
28372880
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
28382881
yield* command(repo, ["remote", "add", "origin", remote]);
@@ -2857,10 +2900,11 @@ describe("storage cleanup reports and local file policies", () => {
28572900
it.live("keeps the worktree when the remote default is unavailable even if main exists", () =>
28582901
runCleanupTest(
28592902
Effect.gen(function* () {
2860-
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
2903+
const { service, fs, repo, worktree, command, setThreads } = yield* cleanupFixture({
28612904
...DEFAULT_SERVER_SETTINGS.storageCleanup,
28622905
worktreeUnchanged: true,
28632906
});
2907+
setThreads([shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) })]);
28642908
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
28652909
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
28662910
yield* command(repo, ["remote", "add", "origin", remote]);

‎apps/server/src/storageCleanup.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ const make = Effect.gen(function* () {
11741174
settledFor !== null &&
11751175
settings.worktreeSettledAfterDays !== null &&
11761176
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);
11771180
let eligible = deleted || old || settled;
11781181
let removalReason = deleted
11791182
? "thread was deleted"
@@ -1189,12 +1192,13 @@ const make = Effect.gen(function* () {
11891192
: `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
11901193
: settings.worktreeOnMerge
11911194
? "not merged"
1192-
: settings.worktreeUnchanged
1195+
: unchangedApplies
11931196
? "has commits beyond the default branch"
1194-
: "no rules apply";
1197+
: settings.worktreeUnchanged
1198+
? "not settled"
1199+
: "no rules apply";
11951200
// 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);
11981202
const pathReason = yield* worktreePathReason(worktreePath);
11991203
if (pathReason !== null) return keep(pathReason);
12001204
if (yield* containsProjectRoot(worktreePaths, [project, ...snapshot.projects]))
@@ -1218,8 +1222,12 @@ const make = Effect.gen(function* () {
12181222
let branch: string | null | undefined;
12191223
let defaultRef: string;
12201224
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+
);
12231231
const projectHead = yield* git.execute({
12241232
operation: "StorageCleanup.resolveProjectBranch",
12251233
cwd: repositoryCwd,
@@ -1278,7 +1286,7 @@ const make = Effect.gen(function* () {
12781286
allowNonZeroExit: true,
12791287
});
12801288
const integrated = ancestor.exitCode === 0;
1281-
eligible = integrated && settings.worktreeUnchanged;
1289+
eligible = integrated && unchangedApplies;
12821290
if (eligible)
12831291
removalReason =
12841292
remote === null
@@ -1297,7 +1305,8 @@ const make = Effect.gen(function* () {
12971305
});
12981306
if (eligible) removalReason = "pull request was merged";
12991307
}
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");
13011310
}
13021311
if (!eligible) return keep(notDueReason);
13031312
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.

‎docs/user/project-settings.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ manually. Custom applies separate worktree rules to the selected project or chec
227227
captures and log retention remain machine-wide.
228228

229229
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,
232233
unchanged-worktree cleanup compares against the branch checked out in the project workspace;
233234
if that checkout has a detached HEAD, the worktree is kept. Only T3-managed worktrees are eligible.
234235
Active sessions and shared worktrees prevent removal. **Keep worktrees with local changes** defaults

0 commit comments

Comments
 (0)