Skip to content

Commit a000973

Browse files
fix(server): storage cleanup treats symlinked spellings of a worktree as one checkout (pingdotgg#25735)
1 parent d7a0b53 commit a000973

2 files changed

Lines changed: 152 additions & 44 deletions

File tree

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

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,52 @@ describe("subagent worktree ownership", () => {
12121212
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
12131213
);
12141214

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+
12151261
it.live.each([
12161262
"running",
12171263
"pending",
@@ -2299,18 +2345,30 @@ describe("storage cleanup reports and local file policies", () => {
22992345
}),
23002346
),
23012347
);
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]) =>
23052356
runCleanupTest(
23062357
Effect.gen(function* () {
23072358
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+
}
23082366
if (timing === "before-check")
2309-
fixture.setProjectRoots([fixture.repo, `${fixture.worktree}/removed-checkout`]);
2367+
fixture.setProjectRoots([fixture.repo, `${worktree}/removed-checkout`]);
23102368
const cleanup = yield* fixture.service.runNow.pipe(Effect.forkChild);
23112369
if (timing === "while-measuring") {
23122370
yield* Deferred.await(fixture.measuring);
2313-
fixture.setProjectRoots([fixture.repo, `${fixture.worktree}/removed-checkout`]);
2371+
fixture.setProjectRoots([fixture.repo, `${worktree}/removed-checkout`]);
23142372
}
23152373
yield* Deferred.succeed(fixture.resume, undefined);
23162374
const report = yield* Fiber.join(cleanup);
@@ -2615,6 +2673,23 @@ describe("storage cleanup reports and local file policies", () => {
26152673
}),
26162674
),
26172675
);
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+
);
26182693
it.live("compares the checked-out branch without a full status", () =>
26192694
runCleanupTest(
26202695
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,6 @@ const make = Effect.gen(function* () {
346346
!path.isAbsolute(relative)
347347
);
348348
};
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-
363349
// pnpm imports packages by clone or reflink when its store shares a device
364350
// with the worktree. Those files have one link but share blocks with the
365351
// store, so removing them frees almost nothing. A store on another device
@@ -500,18 +486,21 @@ const make = Effect.gen(function* () {
500486
});
501487

502488
const containsProjectRoot = Effect.fn("StorageCleanup.containsProjectRoot")(function* (
503-
worktreePath: string,
489+
worktreePaths: ReadonlyArray<string>,
504490
projects: ReadonlyArray<{ readonly workspaceRoot: string }>,
505491
) {
492+
const contains = (projectPath: string) =>
493+
worktreePaths.some(
494+
(worktreePath) =>
495+
pathKey(projectPath) === pathKey(worktreePath) || inside(worktreePath, projectPath),
496+
);
506497
for (const project of projects) {
507498
const projectPath = path.resolve(project.workspaceRoot);
508-
if (pathKey(projectPath) === pathKey(worktreePath) || inside(worktreePath, projectPath))
509-
return true;
499+
if (contains(projectPath)) return true;
510500
const realPath = yield* fs
511501
.realPath(projectPath)
512502
.pipe(Effect.orElseSucceed(() => projectPath));
513-
if (pathKey(realPath) === pathKey(worktreePath) || inside(worktreePath, realPath))
514-
return true;
503+
if (contains(realPath)) return true;
515504
}
516505
return false;
517506
});
@@ -1040,20 +1029,58 @@ const make = Effect.gen(function* () {
10401029
const threadsById = new Map(snapshot.threads.map((thread) => [thread.id, thread]));
10411030
const refreshedDefaultRefs = new Map<string, Set<string>>();
10421031
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!),
10461071
);
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!),
10491075
);
1076+
const deletedByPath = Map.groupBy(deletedThreads, (_thread, index) => deletedKeys[index]!);
10501077
const candidates = [
10511078
...[...groups].map(
1052-
([worktreePath, group]) =>
1079+
([worktreeKey, group]) =>
10531080
// A deleted thread still owns the worktree its remaining subagents share,
10541081
// but only deletion cleanup acts on it; otherwise the live rules apply.
10551082
deletedByPath
1056-
.get(worktreePath)
1083+
.get(worktreeKey)
10571084
?.find(
10581085
(deleted) =>
10591086
resolveWorktreeCleanup(serverSettings, deleted.projectId).worktreeOnDelete &&
@@ -1062,13 +1089,15 @@ const make = Effect.gen(function* () {
10621089
worktreeOwner(group) ??
10631090
group[0]!,
10641091
),
1065-
...deletedThreads.filter((thread) => !groups.has(path.resolve(thread.worktreePath!))),
1092+
...deletedThreads.filter((_thread, index) => !groups.has(deletedKeys[index]!)),
10661093
];
10671094
for (const thread of candidates) {
10681095
const settings = resolveWorktreeCleanup(serverSettings, thread.projectId);
10691096
if (!worktreeCleanupEnabled(settings)) continue;
10701097
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) ?? [];
10721101
const activityAt = Math.max(...group.map(storageCleanupActivityAt));
10731102
const deleted = "workspaceRoot" in thread;
10741103
const settlementChain = deleted
@@ -1122,7 +1151,7 @@ const make = Effect.gen(function* () {
11221151
return keep(waitForStop("thread is running or has pending work"));
11231152
if (group.some((member) => member.branch !== thread.branch))
11241153
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"));
11261155
if (
11271156
[...group, ...settlementChain.slice(1)].some(
11281157
(member) =>
@@ -1168,7 +1197,7 @@ const make = Effect.gen(function* () {
11681197
return keep(notDueReason);
11691198
const pathReason = yield* worktreePathReason(worktreePath);
11701199
if (pathReason !== null) return keep(pathReason);
1171-
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
1200+
if (yield* containsProjectRoot(worktreePaths, [project, ...snapshot.projects]))
11721201
return keep("contains a project checkout");
11731202
if (yield* branchChanged(worktreePath, thread.branch))
11741203
return keep("repository or branch changed");
@@ -1274,12 +1303,14 @@ const make = Effect.gen(function* () {
12741303
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
12751304
const blockedSinceCheck = Effect.fnUntraced(function* () {
12761305
const latestProjects = yield* projectStore.listShells();
1277-
if (yield* containsProjectRoot(worktreePath, [project, ...latestProjects]))
1306+
if (yield* containsProjectRoot(worktreePaths, [project, ...latestProjects]))
12781307
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");
12831314
if (deleted) {
12841315
if (
12851316
!resolveWorktreeCleanup(yield* settingsService.getSettings, thread.projectId)
@@ -1300,20 +1331,23 @@ const make = Effect.gen(function* () {
13001331
projections.getThreadShell(entry.threadId),
13011332
);
13021333
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+
);
13031337
const latestOwner = worktreeOwner(deleted ? [thread, ...latestGroup] : latestGroup);
13041338
if (
13051339
latestGroup.length !== group.length ||
13061340
latestOwner?.id !== thread.id ||
13071341
latestOwner?.branch !== thread.branch ||
1308-
latestGroup.some((shell) => {
1342+
latestGroup.some((shell, index) => {
13091343
const previous = group.find((member) => member.id === shell.id);
13101344
return (
13111345
previous === undefined ||
13121346
shell.branch !== thread.branch ||
13131347
!Equal.equals(shell.settledAt, previous.settledAt) ||
13141348
(shell.settledAt !== null && !Equal.equals(shell.updatedAt, previous.updatedAt)) ||
13151349
!storageCleanupThreadIdle(shell, now, serverSettings) ||
1316-
path.resolve(shell.worktreePath!) !== worktreePath ||
1350+
latestKeys[index] !== worktreeKey ||
13171351
storageCleanupActivityAt(shell) !== storageCleanupActivityAt(previous)
13181352
);
13191353
})
@@ -1350,9 +1384,8 @@ const make = Effect.gen(function* () {
13501384
);
13511385
// Ready persisted sessions may have released their runtime; only live sessions protect the path.
13521386
for (const session of sessions) {
1353-
const cwd = path.resolve(session.cwd);
13541387
if (
1355-
(pathKey(cwd) === pathKey(worktreePath) || inside(worktreePath, cwd)) &&
1388+
(yield* occupies(worktreePaths, session.cwd)) &&
13561389
(yield* providerSessions.isLive(session.id))
13571390
)
13581391
return waitForStop("provider session is still open");

0 commit comments

Comments
 (0)