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, 02:00 UTC
perf(server): skip unnecessary worktree cleanup scans (#32933)
Co-authored-by: GPT 6.1 Sol <codex@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Grok 4.7 <noreply@x.ai>
Wout Stiens committed
Nov 8, 2026 at 22:25 UTC
b76a5958890f8d1dbcc150e1d435a3dced222202
2 files changed
+157
-73
apps/server/src/storageCleanup.test.ts
+65
@@ -34,6 +34,7 @@ import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
34
import * as TerminalManager from "./terminal/Manager.ts";
35
import { CodexProviderCapabilitiesV2 } from "./orchestration-v2/Adapters/CodexAdapterV2.ts";
36
import { ServerActivation } from "./serverActivation.ts";
37
+import { withWorkspaceLease } from "./workspace/workspaceLease.ts";
38
import { describe, expect, it } from "@effect/vitest";
39
import {
40
EventId,
@@ -350,6 +351,7 @@ const cleanupFixture = (
351
let onLiveCheck: Effect.Effect<void> = Effect.void;
352
let outboxStatus: string | null = null;
353
let fullStatusCalls = 0;
354
+ let gitInspections = 0;
355
let snapshotReads = 0;
356
let terminalListener: ((event: TerminalMetadataStreamEvent) => Effect.Effect<void>) | undefined;
357
let events: Stream.Stream<OrchestrationV2DomainEvent> = Stream.empty;
@@ -359,6 +361,7 @@ const cleanupFixture = (
361
...git,
362
execute: (input) =>
363
Effect.gen(function* () {
364
+ gitInspections++;
365
// Before Git's own clean check: inside `git worktree remove`, or on
366
// Windows the same status check the removal makes before deleting.
367
if (
@@ -554,6 +557,7 @@ const cleanupFixture = (
557
...paths,
558
},
559
}) ?? Effect.void,
560
+ gitInspections: () => gitInspections,
561
fullStatusCalls: () => fullStatusCalls,
562
snapshotReads: () => snapshotReads,
563
sessionLiveChecks: () => sessionLiveChecks,
@@ -2240,6 +2244,67 @@ describe("storage cleanup reports and local file policies", () => {
2244
}),
2245
),
2246
);
2247
+ it.live.each([
2248
+ { days: 8, reason: "Inactive for 1 of 8 days" },
2249
+ { days: null, reason: "No rules apply" },
2250
+ ])("keeps an ineligible age/delete-only worktree without Git ($days)", ({ days, reason }) =>
2251
+ runCleanupTest(
2252
+ Effect.gen(function* () {
2253
+ const fixture = yield* cleanupFixture({
2254
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
2255
+ worktreeAfterDays: days,
2256
+ worktreeOnDelete: true,
2257
+ });
2258
+ fixture.setThreads([
2259
+ shell({
2260
+ branch: "feature",
2261
+ worktreePath: fixture.worktree,
2262
+ latestUserMessageAt: DateTime.makeUnsafe(
2263
+ DateTime.toEpochMillis(yield* DateTime.now) - DAY_MS,
2264
+ ),
2265
+ }),
2266
+ ]);
2267
+ yield* fixture.fs.writeFileString(`${fixture.worktree}/tracked.txt`, "local edit\n");
2268
+ const report = yield* fixture.service.runNow;
2269
+ expect(report.entries[0]).toMatchObject({
2270
+ outcome: "kept",
2271
+ reason,
2272
+ bytes: null,
2273
+ });
2274
+ expect(fixture.gitInspections()).toBe(0);
2275
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
2276
+ }),
2277
+ ),
2278
+ );
2279
+ it.live("lets a turn acquire the checkout during accounting and protects its new activity", () =>
2280
+ runCleanupTest(
2281
+ Effect.gen(function* () {
2282
+ const fixture = yield* pausedMeasurementFixture();
2283
+ const cleanup = yield* fixture.service.runNow.pipe(Effect.forkChild);
2284
+ yield* Deferred.await(fixture.measuring);
2285
+ yield* withWorkspaceLease(
2286
+ fixture.worktree,
2287
+ Effect.sync(() => {
2288
+ fixture.setThreads([
2289
+ shell({
2290
+ branch: "feature",
2291
+ worktreePath: fixture.worktree,
2292
+ status: "running",
2293
+ }),
2294
+ ]);
2295
+ }),
2296
+ );
2297
+ yield* Deferred.succeed(fixture.resume, undefined);
2298
+ const report = yield* Fiber.join(cleanup);
2299
+ expect(report.entries[0]).toMatchObject({
2300
+ outcome: "kept",
2301
+ reason: "Thread activity or shared worktree changed since check",
2302
+ bytes: null,
2303
+ });
2304
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
2305
+ }),
2306
+ ),
2307
+ );
2308
it.live("still removes a worktree when measuring its size fails", () =>
2309
runCleanupTest(
2310
Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+92
-73
@@ -836,6 +836,20 @@ const make = Effect.gen(function* () {
836
);
837
if (root !== null && !isFilesystemRoot(root, path)) roots.push(root);
838
}
839
+ const worktreePathReason = Effect.fn("StorageCleanup.worktreePathReason")(function* (
840
+ worktreePath: string,
841
+ ) {
842
+ // Allow symlinked parents, never a symlinked worktree directory.
843
+ const realPath = yield* fs.realPath(worktreePath);
844
+ const realParent = yield* fs.realPath(path.dirname(worktreePath));
845
+ if (realPath !== path.join(realParent, path.basename(worktreePath)))
846
+ return "worktree is a symbolic link";
847
+ if (!roots.some((root) => inside(root, realPath)))
848
+ return "outside the managed worktree folder";
849
+ if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
850
+ return "not a linked worktree";
851
+ return null;
852
+ });
853
const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>`
854
SELECT t.payload_json, p.workspace_root AS "workspaceRoot"
855
FROM orchestration_v2_projection_threads t
@@ -924,21 +938,6 @@ const make = Effect.gen(function* () {
938
if (group.some((member) => member.branch !== thread.branch))
939
return keep("a subagent thread's branch differs from this worktree's branch");
940
if (hasTerminal(worktreePath)) return keep(waitForStop("open terminal"));
927
- // Compare canonical roots while allowing symlinked parents, never symlinked worktree directories.
928
- const realPath = yield* fs.realPath(worktreePath);
929
- const realParent = yield* fs.realPath(path.dirname(worktreePath));
930
- if (realPath !== path.join(realParent, path.basename(worktreePath)))
931
- return keep("worktree is a symbolic link");
932
- if (!roots.some((root) => inside(root, realPath)))
933
- return keep("outside the managed worktree folder");
934
- if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
935
- return keep("contains a project checkout");
936
- if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
937
- return keep("not a linked worktree");
938
- if (yield* branchChanged(worktreePath, thread.branch))
939
- return keep("repository or branch changed");
940
- const changes = yield* localChangesReason(worktreePath, settings);
941
- if (changes !== null) return keep(changes);
941
if (
942
[...group, ...settlementChain.slice(1)].some(
943
(member) =>
@@ -948,7 +947,6 @@ const make = Effect.gen(function* () {
947
)
948
)
949
return keep(waitForStop("settled moments ago"));
951
- const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
950
const old =
951
!deleted &&
952
settings.worktreeAfterDays !== null &&
@@ -968,7 +966,31 @@ const make = Effect.gen(function* () {
966
: old
967
? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} days`
968
: `settled for ${settledDays} days`;
971
- if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
969
+ const notDueReason =
970
+ !deleted && settings.worktreeAfterDays !== null
971
+ ? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} of ${settings.worktreeAfterDays} days`
972
+ : settings.worktreeSettledAfterDays !== null
973
+ ? settledFor === null
974
+ ? "not settled"
975
+ : `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
976
+ : settings.worktreeOnMerge
977
+ ? "not merged"
978
+ : settings.worktreeUnchanged
979
+ ? "has commits beyond the default branch"
980
+ : "no rules apply";
981
+ // Only Git-based rules can make a not-yet-due worktree eligible, so skip Git otherwise.
982
+ if (!eligible && !settings.worktreeUnchanged && !settings.worktreeOnMerge)
983
+ return keep(notDueReason);
984
+ const pathReason = yield* worktreePathReason(worktreePath);
985
+ if (pathReason !== null) return keep(pathReason);
986
+ if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
987
+ return keep("contains a project checkout");
988
+ if (yield* branchChanged(worktreePath, thread.branch))
989
+ return keep("repository or branch changed");
990
+ const changes = yield* localChangesReason(worktreePath, settings);
991
+ if (changes !== null) return keep(changes);
992
+ const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
993
+ if (!eligible) {
994
const repositoryCwd = path.resolve(project.workspaceRoot);
995
const remotes = yield* git.execute({
996
operation: "StorageCleanup.listRemotes",
@@ -1063,18 +1085,7 @@ const make = Effect.gen(function* () {
1085
}
1086
if (!eligible && remote === null) return keep("has commits beyond the project branch");
1087
}
1066
- if (!eligible)
1067
- return keep(
1068
- !deleted && settings.worktreeAfterDays !== null
1069
- ? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} of ${settings.worktreeAfterDays} days`
1070
- : settings.worktreeSettledAfterDays !== null
1071
- ? settledFor === null
1072
- ? "not settled"
1073
- : `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
1074
- : settings.worktreeOnMerge
1075
- ? "not merged"
1076
- : "has commits beyond the default branch",
1077
- );
1088
+ if (!eligible) return keep(notDueReason);
1089
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
1090
const blockedSinceCheck = Effect.fnUntraced(function* () {
1091
const latestProjects = yield* projectStore.listShells();
@@ -1165,53 +1176,61 @@ const make = Effect.gen(function* () {
1176
});
1177
const blocked = yield* blockedSinceCheck();
1178
if (blocked !== null) return keep(blocked);
1179
+ // Size is an estimate; provider startup must not wait for directory traversal.
1180
const bytes = yield* measureWorktree(worktreePath);
1169
- const blockedWhileMeasuring = yield* blockedSinceCheck();
1170
- if (blockedWhileMeasuring !== null) return keep(blockedWhileMeasuring);
1171
- if (yield* branchChanged(worktreePath, thread.branch))
1172
- return keep("repository or branch changed since check");
1173
- if (
1174
- (yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" })).commitSha !==
1175
- head.commitSha
1176
- )
1177
- return keep("commit changed since check");
1178
- const finalChanges = yield* localChangesReason(worktreePath, settings);
1179
- if (finalChanges !== null) return keep(finalChanges);
1180
- const currentSettings = yield* settingsService.getSettings;
1181
- const current = resolveWorktreeCleanup(currentSettings, thread.projectId);
1182
- if (
1183
- currentSettings.autoResumeLimitedThreads !== serverSettings.autoResumeLimitedThreads ||
1184
- currentSettings.snoozeLimitedThreads !== serverSettings.snoozeLimitedThreads ||
1185
- Object.keys(settings).some(
1186
- (key) =>
1187
- current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
1188
- )
1189
- )
1190
- return keep("settings changed since check");
1191
- // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1192
- if (settings.worktreeKeepWhen === "tracked-changes")
1193
- yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
1194
- const submodules = yield* submoduleRemoval(worktreePath, settings);
1195
- if (submodules.reason !== null) return keep(submodules.reason);
1196
- if (submodules.force) {
1197
- const changes = yield* localChangesReason(worktreePath, settings);
1198
- if (changes !== null) return keep(changes);
1199
- }
1200
- yield* removeLinkedWorktree({
1201
- operation: "StorageCleanup.cleanupGit",
1202
- cwd: project.workspaceRoot,
1181
+ yield* withWorkspaceLease(
1182
worktreePath,
1204
- // Git requires force for submodules; recursive checks above cannot make removal atomic.
1205
- force: submodules.force,
1206
- execute: git.execute,
1207
- run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"),
1208
- });
1209
- entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });
1210
- yield* gitManager.invalidateStatus(project.workspaceRoot);
1211
- // Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1212
- yield* Effect.logInfo("storage cleanup removed worktree", { threadId: thread.id });
1183
+ Effect.gen(function* () {
1184
+ const pathReason = yield* worktreePathReason(worktreePath);
1185
+ if (pathReason !== null) return keep(pathReason);
1186
+ const blockedWhileMeasuring = yield* blockedSinceCheck();
1187
+ if (blockedWhileMeasuring !== null) return keep(blockedWhileMeasuring);
1188
+ if (yield* branchChanged(worktreePath, thread.branch))
1189
+ return keep("repository or branch changed since check");
1190
+ if (
1191
+ (yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" })).commitSha !==
1192
+ head.commitSha
1193
+ )
1194
+ return keep("commit changed since check");
1195
+ const finalChanges = yield* localChangesReason(worktreePath, settings);
1196
+ if (finalChanges !== null) return keep(finalChanges);
1197
+ const currentSettings = yield* settingsService.getSettings;
1198
+ const current = resolveWorktreeCleanup(currentSettings, thread.projectId);
1199
+ if (
1200
+ currentSettings.autoResumeLimitedThreads !==
1201
+ serverSettings.autoResumeLimitedThreads ||
1202
+ currentSettings.snoozeLimitedThreads !== serverSettings.snoozeLimitedThreads ||
1203
+ Object.keys(settings).some(
1204
+ (key) =>
1205
+ current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
1206
+ )
1207
+ )
1208
+ return keep("settings changed since check");
1209
+ // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1210
+ if (settings.worktreeKeepWhen === "tracked-changes")
1211
+ yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
1212
+ const submodules = yield* submoduleRemoval(worktreePath, settings);
1213
+ if (submodules.reason !== null) return keep(submodules.reason);
1214
+ if (submodules.force) {
1215
+ const changes = yield* localChangesReason(worktreePath, settings);
1216
+ if (changes !== null) return keep(changes);
1217
+ }
1218
+ yield* removeLinkedWorktree({
1219
+ operation: "StorageCleanup.cleanupGit",
1220
+ cwd: project.workspaceRoot,
1221
+ worktreePath,
1222
+ // Git requires force for submodules; recursive checks above cannot make removal atomic.
1223
+ force: submodules.force,
1224
+ execute: git.execute,
1225
+ run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"),
1226
+ });
1227
+ entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });
1228
+ yield* gitManager.invalidateStatus(project.workspaceRoot);
1229
+ // Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1230
+ yield* Effect.logInfo("storage cleanup removed worktree", { threadId: thread.id });
1231
+ }),
1232
+ );
1233
}).pipe(
1214
- (effect) => withWorkspaceLease(worktreePath, effect),
1234
Effect.catch((error) =>
1235
Effect.gen(function* () {
1236
entries.push({