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, 18:30 UTC
fix(server): storage cleanup treats symlinked spellings of a worktree as one checkout (#25735)
Julius Marminge committed
Nov 12, 2026 at 19:30 UTC
a000973ccb852ad4ab63aaf16b5bf7b226ac9eb2
2 files changed
+152
-44
apps/server/src/storageCleanup.test.ts
+80
-5
@@ -1212,6 +1212,52 @@ describe("subagent worktree ownership", () => {
1212
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
1213
);
1214
1215
+ it.effect("keeps a checkout two top-level threads store through a symlinked parent", () =>
1216
+ Effect.gen(function* () {
1217
+ yield* TestClock.setTime(NOW_MS);
1218
+ const fixture = yield* cleanupFixture({
1219
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
1220
+ worktreeSettledAfterDays: 8,
1221
+ });
1222
+ const linkedParent = `${yield* fixture.fs.makeTempDirectoryScoped({ prefix: "cleanup-link-" })}/worktrees`;
1223
+ yield* fixture.fs.symlink(fixture.config.worktreesDir, linkedParent);
1224
+ const settled = { branch: "feature", settledAt: at(-20 * DAY_MS) };
1225
+ fixture.setThreads([
1226
+ shell({ ...settled, worktreePath: fixture.worktree }),
1227
+ shell({ ...settled, id: ThreadId.make("other"), worktreePath: `${linkedParent}/feature` }),
1228
+ ]);
1229
+ const report = yield* fixture.service.runNow;
1230
+ expect(report.entries).toEqual([
1231
+ expect.objectContaining({ outcome: "kept", reason: "Shared by 2 threads" }),
1232
+ ]);
1233
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
1234
+ }).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
1235
+ );
1236
+
1237
+ it.effect("keeps a checkout two top-level threads store in different case on Windows", () =>
1238
+ Effect.gen(function* () {
1239
+ yield* TestClock.setTime(NOW_MS);
1240
+ const fixture = yield* cleanupFixture({
1241
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
1242
+ worktreeSettledAfterDays: 8,
1243
+ }).pipe(Effect.provideService(HostProcess.Platform, "win32"));
1244
+ const settled = { branch: "feature", settledAt: at(-20 * DAY_MS) };
1245
+ fixture.setThreads([
1246
+ shell({ ...settled, worktreePath: fixture.worktree }),
1247
+ shell({
1248
+ ...settled,
1249
+ id: ThreadId.make("other"),
1250
+ worktreePath: fixture.worktree.toUpperCase(),
1251
+ }),
1252
+ ]);
1253
+ const report = yield* fixture.service.runNow;
1254
+ expect(report.entries).toEqual([
1255
+ expect.objectContaining({ outcome: "kept", reason: "Shared by 2 threads" }),
1256
+ ]);
1257
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
1258
+ }).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
1259
+ );
1260
+
1261
it.live.each([
1262
"running",
1263
"pending",
@@ -2299,18 +2345,30 @@ describe("storage cleanup reports and local file policies", () => {
2345
}),
2346
),
2347
);
2302
- it.live.each(["before-check", "while-measuring"] as const)(
2303
- "keeps a worktree protected by a missing project root (%s)",
2304
- (timing) =>
2348
+ it.live.each([
2349
+ ["before-check", "stored"],
2350
+ ["while-measuring", "stored"],
2351
+ ["before-check", "symlinked parent"],
2352
+ ["while-measuring", "symlinked parent"],
2353
+ ] as const)(
2354
+ "keeps a worktree protected by a missing project root (%s, %s spelling)",
2355
+ ([timing, spelling]) =>
2356
runCleanupTest(
2357
Effect.gen(function* () {
2358
const fixture = yield* pausedMeasurementFixture();
2359
+ let worktree = fixture.worktree;
2360
+ if (spelling === "symlinked parent") {
2361
+ const linkedParent = `${yield* fixture.fs.makeTempDirectoryScoped({ prefix: "cleanup-link-" })}/worktrees`;
2362
+ yield* fixture.fs.symlink(fixture.config.worktreesDir, linkedParent);
2363
+ worktree = `${linkedParent}/feature`;
2364
+ fixture.setThreads([shell({ branch: "feature", worktreePath: worktree })]);
2365
+ }
2366
if (timing === "before-check")
2309
- fixture.setProjectRoots([fixture.repo, `${fixture.worktree}/removed-checkout`]);
2367
+ fixture.setProjectRoots([fixture.repo, `${worktree}/removed-checkout`]);
2368
const cleanup = yield* fixture.service.runNow.pipe(Effect.forkChild);
2369
if (timing === "while-measuring") {
2370
yield* Deferred.await(fixture.measuring);
2313
- fixture.setProjectRoots([fixture.repo, `${fixture.worktree}/removed-checkout`]);
2371
+ fixture.setProjectRoots([fixture.repo, `${worktree}/removed-checkout`]);
2372
}
2373
yield* Deferred.succeed(fixture.resume, undefined);
2374
const report = yield* Fiber.join(cleanup);
@@ -2615,6 +2673,23 @@ describe("storage cleanup reports and local file policies", () => {
2673
}),
2674
),
2675
);
2676
+ it.live("keeps a worktree for a live session in a missing folder under its stored alias", () =>
2677
+ runCleanupTest(
2678
+ Effect.gen(function* () {
2679
+ const fixture = yield* cleanupFixture();
2680
+ const linkedParent = `${yield* fixture.fs.makeTempDirectoryScoped({ prefix: "cleanup-link-" })}/worktrees`;
2681
+ yield* fixture.fs.symlink(fixture.config.worktreesDir, linkedParent);
2682
+ fixture.setThreads([shell({ branch: "feature", worktreePath: `${linkedParent}/feature` })]);
2683
+ fixture.setOpenSessionCwd(`${linkedParent}/feature/removed-folder`);
2684
+ const report = yield* fixture.service.runNow;
2685
+ expect(report.entries[0]).toMatchObject({
2686
+ outcome: "kept",
2687
+ reason: "Provider session is still open",
2688
+ });
2689
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
2690
+ }),
2691
+ ),
2692
+ );
2693
it.live("compares the checked-out branch without a full status", () =>
2694
runCleanupTest(
2695
Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+72
-39
@@ -346,20 +346,6 @@ const make = Effect.gen(function* () {
346
!path.isAbsolute(relative)
347
);
348
};
349
- const hasTerminal = (worktreePath: string) =>
350
- [...liveTerminals.values()]
351
- .flatMap((entries) => [...entries.values()])
352
- .some((terminal) => {
353
- if (terminal.status !== "starting" && terminal.status !== "running") return false;
354
- const cwd = path.resolve(terminal.cwd);
355
- return (
356
- (terminal.worktreePath !== null &&
357
- pathKey(terminal.worktreePath) === pathKey(worktreePath)) ||
358
- pathKey(cwd) === pathKey(worktreePath) ||
359
- inside(worktreePath, cwd)
360
- );
361
- });
362
-
349
// pnpm imports packages by clone or reflink when its store shares a device
350
// with the worktree. Those files have one link but share blocks with the
351
// store, so removing them frees almost nothing. A store on another device
@@ -500,18 +486,21 @@ const make = Effect.gen(function* () {
486
});
487
488
const containsProjectRoot = Effect.fn("StorageCleanup.containsProjectRoot")(function* (
503
- worktreePath: string,
489
+ worktreePaths: ReadonlyArray<string>,
490
projects: ReadonlyArray<{ readonly workspaceRoot: string }>,
491
) {
492
+ const contains = (projectPath: string) =>
493
+ worktreePaths.some(
494
+ (worktreePath) =>
495
+ pathKey(projectPath) === pathKey(worktreePath) || inside(worktreePath, projectPath),
496
+ );
497
for (const project of projects) {
498
const projectPath = path.resolve(project.workspaceRoot);
508
- if (pathKey(projectPath) === pathKey(worktreePath) || inside(worktreePath, projectPath))
509
- return true;
499
+ if (contains(projectPath)) return true;
500
const realPath = yield* fs
501
.realPath(projectPath)
502
.pipe(Effect.orElseSucceed(() => projectPath));
513
- if (pathKey(realPath) === pathKey(worktreePath) || inside(worktreePath, realPath))
514
- return true;
503
+ if (contains(realPath)) return true;
504
}
505
return false;
506
});
@@ -1040,20 +1029,58 @@ const make = Effect.gen(function* () {
1029
const threadsById = new Map(snapshot.threads.map((thread) => [thread.id, thread]));
1030
const refreshedDefaultRefs = new Map<string, Set<string>>();
1031
const defaultBranches = new Map<string, string | null>();
1043
- const groups = Map.groupBy(
1044
- snapshot.threads.filter((thread) => thread.worktreePath !== null),
1045
- (thread) => path.resolve(thread.worktreePath!),
1032
+ // Threads may store one checkout through different symlinked spellings; compare real paths.
1033
+ // Keys are only compared, never opened, so they take pathKey's Windows case folding.
1034
+ const canonicalPaths = new Map<string, string>();
1035
+ const canonical = Effect.fnUntraced(function* (target: string) {
1036
+ const resolved = path.resolve(target);
1037
+ const cached = canonicalPaths.get(resolved);
1038
+ if (cached !== undefined) return cached;
1039
+ const realPath = yield* fs.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved));
1040
+ const key = pathKey(realPath);
1041
+ canonicalPaths.set(resolved, key);
1042
+ return key;
1043
+ });
1044
+ // A cwd in a deleted folder cannot be resolved, so stored spellings are compared too.
1045
+ const occupies = Effect.fnUntraced(function* (
1046
+ worktreePaths: ReadonlyArray<string>,
1047
+ target: string,
1048
+ ) {
1049
+ const targets = [path.resolve(target), yield* canonical(target)];
1050
+ return worktreePaths.some((worktree) =>
1051
+ targets.some((cwd) => pathKey(cwd) === pathKey(worktree) || inside(worktree, cwd)),
1052
+ );
1053
+ });
1054
+ const hasTerminal = Effect.fnUntraced(function* (worktreePaths: ReadonlyArray<string>) {
1055
+ for (const terminal of [...liveTerminals.values()].flatMap((entries) => [
1056
+ ...entries.values(),
1057
+ ])) {
1058
+ if (terminal.status !== "starting" && terminal.status !== "running") continue;
1059
+ if (
1060
+ (terminal.worktreePath !== null &&
1061
+ (yield* occupies(worktreePaths, terminal.worktreePath))) ||
1062
+ (yield* occupies(worktreePaths, terminal.cwd))
1063
+ )
1064
+ return true;
1065
+ }
1066
+ return false;
1067
+ });
1068
+ const liveThreads = snapshot.threads.filter((thread) => thread.worktreePath !== null);
1069
+ const liveKeys = yield* Effect.forEach(liveThreads, (thread) =>
1070
+ canonical(thread.worktreePath!),
1071
);
1047
- const deletedByPath = Map.groupBy(deletedThreads, (thread) =>
1048
- path.resolve(thread.worktreePath!),
1072
+ const groups = Map.groupBy(liveThreads, (_thread, index) => liveKeys[index]!);
1073
+ const deletedKeys = yield* Effect.forEach(deletedThreads, (thread) =>
1074
+ canonical(thread.worktreePath!),
1075
);
1076
+ const deletedByPath = Map.groupBy(deletedThreads, (_thread, index) => deletedKeys[index]!);
1077
const candidates = [
1078
...[...groups].map(
1052
- ([worktreePath, group]) =>
1079
+ ([worktreeKey, group]) =>
1080
// A deleted thread still owns the worktree its remaining subagents share,
1081
// but only deletion cleanup acts on it; otherwise the live rules apply.
1082
deletedByPath
1056
- .get(worktreePath)
1083
+ .get(worktreeKey)
1084
?.find(
1085
(deleted) =>
1086
resolveWorktreeCleanup(serverSettings, deleted.projectId).worktreeOnDelete &&
@@ -1062,13 +1089,15 @@ const make = Effect.gen(function* () {
1089
worktreeOwner(group) ??
1090
group[0]!,
1091
),
1065
- ...deletedThreads.filter((thread) => !groups.has(path.resolve(thread.worktreePath!))),
1092
+ ...deletedThreads.filter((_thread, index) => !groups.has(deletedKeys[index]!)),
1093
];
1094
for (const thread of candidates) {
1095
const settings = resolveWorktreeCleanup(serverSettings, thread.projectId);
1096
if (!worktreeCleanupEnabled(settings)) continue;
1097
const worktreePath = path.resolve(thread.worktreePath!);
1071
- const group = groups.get(worktreePath) ?? [];
1098
+ const worktreeKey = yield* canonical(worktreePath);
1099
+ const worktreePaths = [worktreePath, worktreeKey];
1100
+ const group = groups.get(worktreeKey) ?? [];
1101
const activityAt = Math.max(...group.map(storageCleanupActivityAt));
1102
const deleted = "workspaceRoot" in thread;
1103
const settlementChain = deleted
@@ -1122,7 +1151,7 @@ const make = Effect.gen(function* () {
1151
return keep(waitForStop("thread is running or has pending work"));
1152
if (group.some((member) => member.branch !== thread.branch))
1153
return keep("a subagent thread's branch differs from this worktree's branch");
1125
- if (hasTerminal(worktreePath)) return keep(waitForStop("open terminal"));
1154
+ if (yield* hasTerminal(worktreePaths)) return keep(waitForStop("open terminal"));
1155
if (
1156
[...group, ...settlementChain.slice(1)].some(
1157
(member) =>
@@ -1168,7 +1197,7 @@ const make = Effect.gen(function* () {
1197
return keep(notDueReason);
1198
const pathReason = yield* worktreePathReason(worktreePath);
1199
if (pathReason !== null) return keep(pathReason);
1171
- if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
1200
+ if (yield* containsProjectRoot(worktreePaths, [project, ...snapshot.projects]))
1201
return keep("contains a project checkout");
1202
if (yield* branchChanged(worktreePath, thread.branch))
1203
return keep("repository or branch changed");
@@ -1274,12 +1303,14 @@ const make = Effect.gen(function* () {
1303
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
1304
const blockedSinceCheck = Effect.fnUntraced(function* () {
1305
const latestProjects = yield* projectStore.listShells();
1277
- if (yield* containsProjectRoot(worktreePath, [project, ...latestProjects]))
1306
+ if (yield* containsProjectRoot(worktreePaths, [project, ...latestProjects]))
1307
return "contains a project checkout";
1279
- const latest = (yield* projections.getThreadWorktreePaths({
1280
- includeArchived: true,
1281
- })).filter((entry) => path.resolve(entry.worktreePath) === worktreePath);
1282
- if (hasTerminal(worktreePath)) return waitForStop("open terminal");
1308
+ const bindings = yield* projections.getThreadWorktreePaths({ includeArchived: true });
1309
+ const bindingKeys = yield* Effect.forEach(bindings, (binding) =>
1310
+ canonical(binding.worktreePath),
1311
+ );
1312
+ const latest = bindings.filter((_binding, index) => bindingKeys[index] === worktreeKey);
1313
+ if (yield* hasTerminal(worktreePaths)) return waitForStop("open terminal");
1314
if (deleted) {
1315
if (
1316
!resolveWorktreeCleanup(yield* settingsService.getSettings, thread.projectId)
@@ -1300,12 +1331,15 @@ const make = Effect.gen(function* () {
1331
projections.getThreadShell(entry.threadId),
1332
);
1333
const latestGroup = latestShells.filter((shell) => shell !== null);
1334
+ const latestKeys = yield* Effect.forEach(latestGroup, (shell) =>
1335
+ shell.worktreePath === null ? Effect.succeed(null) : canonical(shell.worktreePath),
1336
+ );
1337
const latestOwner = worktreeOwner(deleted ? [thread, ...latestGroup] : latestGroup);
1338
if (
1339
latestGroup.length !== group.length ||
1340
latestOwner?.id !== thread.id ||
1341
latestOwner?.branch !== thread.branch ||
1308
- latestGroup.some((shell) => {
1342
+ latestGroup.some((shell, index) => {
1343
const previous = group.find((member) => member.id === shell.id);
1344
return (
1345
previous === undefined ||
@@ -1313,7 +1347,7 @@ const make = Effect.gen(function* () {
1347
!Equal.equals(shell.settledAt, previous.settledAt) ||
1348
(shell.settledAt !== null && !Equal.equals(shell.updatedAt, previous.updatedAt)) ||
1349
!storageCleanupThreadIdle(shell, now, serverSettings) ||
1316
- path.resolve(shell.worktreePath!) !== worktreePath ||
1350
+ latestKeys[index] !== worktreeKey ||
1351
storageCleanupActivityAt(shell) !== storageCleanupActivityAt(previous)
1352
);
1353
})
@@ -1350,9 +1384,8 @@ const make = Effect.gen(function* () {
1384
);
1385
// Ready persisted sessions may have released their runtime; only live sessions protect the path.
1386
for (const session of sessions) {
1353
- const cwd = path.resolve(session.cwd);
1387
if (
1355
- (pathKey(cwd) === pathKey(worktreePath) || inside(worktreePath, cwd)) &&
1388
+ (yield* occupies(worktreePaths, session.cwd)) &&
1389
(yield* providerSessions.isLive(session.id))
1390
)
1391
return waitForStop("provider session is still open");