Skip to content

Commit b28a21f

Browse files
SunkenInTimeclaude
andcommitted
fix(server): worktree cleanup no longer blocked by links and build output
Automatic worktree cleanup refused to remove any worktree with an ignored path other than node_modules/. Setup scripts that symlink .env into the worktree, and any build output (Flutter's build/ and .dart_tool/), made every worktree permanently ineligible. Ignored symlinks and junctions no longer block removal. A new worktreeDisposablePaths setting (gitignore patterns, default node_modules/) lists ignored paths that are safe to delete, machine-wide or per project through custom worktree cleanup rules. Git matches the patterns. Every other ignored or untracked file still keeps the worktree, now including untracked files hidden by status.showUntrackedFiles=no. Git for Windows reports success from `git worktree remove` but leaves NTFS junctions and their parent directories behind, so a resumed thread found a stub instead of recreating its checkout. removeWorktree now deletes that leftover with fs.rm, which unlinks links without following them. Fixes #13836 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 2a45557 commit b28a21f

16 files changed

Lines changed: 379 additions & 49 deletions

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export const make = Effect.gen(function* () {
227227
threadAutoSettlement: true,
228228
storageCleanup: true,
229229
projectWorktreeCleanup: true,
230+
worktreeDisposablePaths: true,
230231
threadRestartContinuation: true,
231232
projectSettingsOverrides: true,
232233
threadSnooze: true,

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

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
// @effect-diagnostics nodeBuiltinImport:off - Effect's FileSystem cannot create junctions.
2+
import * as NodeFS from "node:fs";
3+
4+
import * as NodeServices from "@effect/platform-node/NodeServices";
5+
import { it } from "@effect/vitest";
26
import {
37
ProjectId,
48
ProviderInstanceId,
@@ -7,7 +11,19 @@ import {
711
type OrchestrationV2ThreadShell,
812
} from "@t3tools/contracts";
913
import * as DateTime from "effect/DateTime";
10-
import { storageCleanupActivityAt, storageCleanupThreadIdle } from "./storageCleanup.ts";
14+
import * as Effect from "effect/Effect";
15+
import * as FileSystem from "effect/FileSystem";
16+
import * as Layer from "effect/Layer";
17+
import * as Path from "effect/Path";
18+
import { describe, expect } from "vite-plus/test";
19+
20+
import * as ServerConfig from "./config.ts";
21+
import {
22+
storageCleanupActivityAt,
23+
storageCleanupKeepsUntrackedFiles,
24+
storageCleanupThreadIdle,
25+
} from "./storageCleanup.ts";
26+
import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
1127

1228
const NOW_MS = Date.parse("2026-06-10T12:00:00.000Z");
1329
const DAY_MS = 24 * 60 * 60 * 1_000;
@@ -104,3 +120,99 @@ describe("V2 storage cleanup eligibility", () => {
104120
return { ...candidate(), status };
105121
}
106122
});
123+
124+
const GitLayer = GitVcsDriver.layer.pipe(
125+
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-storage-cleanup-test-" })),
126+
Layer.provideMerge(NodeServices.layer),
127+
);
128+
129+
const makeRepo = (files: Record<string, string>) =>
130+
Effect.gen(function* () {
131+
const fs = yield* FileSystem.FileSystem;
132+
const path = yield* Path.Path;
133+
const git = yield* GitVcsDriver.GitVcsDriver;
134+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "storage-cleanup-repo-" });
135+
const write = (relativePath: string, contents: string) =>
136+
fs
137+
.makeDirectory(path.dirname(path.join(cwd, relativePath)), { recursive: true })
138+
.pipe(Effect.andThen(fs.writeFileString(path.join(cwd, relativePath), contents)));
139+
yield* git.initRepo({ cwd });
140+
yield* write(".gitignore", "node_modules/\nbuild/\n.dart_tool/\n*.env\nlinked\n");
141+
yield* git.execute({ operation: "test.add", cwd, args: ["add", ".gitignore"] });
142+
yield* git.execute({
143+
operation: "test.commit",
144+
cwd,
145+
args: ["-c", "user.name=Test", "-c", "[email protected]", "commit", "-m", "init"],
146+
});
147+
for (const [relativePath, contents] of Object.entries(files))
148+
yield* write(relativePath, contents);
149+
return cwd;
150+
});
151+
152+
it.layer(GitLayer)("storage cleanup ignored files", (it) => {
153+
const flutterBuild = {
154+
"build/app/output.apk": "apk",
155+
".dart_tool/package_config.json": "{}",
156+
"node_modules/pkg/index.js": "",
157+
// An untracked package left with only its dependency install.
158+
"packages/removed/node_modules/pkg/index.js": "",
159+
};
160+
161+
it.effect("keeps a worktree whose ignored files are not disposable", () =>
162+
Effect.gen(function* () {
163+
const cwd = yield* makeRepo(flutterBuild);
164+
expect(yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/"])).toBe(true);
165+
expect(yield* storageCleanupKeepsUntrackedFiles(cwd, [])).toBe(true);
166+
}),
167+
);
168+
169+
it.effect("removes a worktree whose ignored files all match disposable patterns", () =>
170+
Effect.gen(function* () {
171+
const cwd = yield* makeRepo(flutterBuild);
172+
expect(
173+
yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/", "build/", ".dart_tool/"]),
174+
).toBe(false);
175+
}),
176+
);
177+
178+
it.effect("keeps unknown ignored files next to disposable ones", () =>
179+
Effect.gen(function* () {
180+
const cwd = yield* makeRepo({ ...flutterBuild, "packages/removed/local.env": "TOKEN=1" });
181+
expect(
182+
yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/", "build/", ".dart_tool/"]),
183+
).toBe(true);
184+
}),
185+
);
186+
187+
it.effect("keeps untracked files that status is configured to hide", () =>
188+
Effect.gen(function* () {
189+
const git = yield* GitVcsDriver.GitVcsDriver;
190+
const cwd = yield* makeRepo({ "notes.txt": "draft" });
191+
yield* git.execute({
192+
operation: "test.config",
193+
cwd,
194+
args: ["config", "status.showUntrackedFiles", "no"],
195+
});
196+
expect((yield* git.statusDetailsLocal(cwd)).hasWorkingTreeChanges).toBe(false);
197+
expect(yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/"])).toBe(true);
198+
}),
199+
);
200+
201+
it.effect("never lets an ignored link keep a worktree", () =>
202+
Effect.gen(function* () {
203+
const fs = yield* FileSystem.FileSystem;
204+
const path = yield* Path.Path;
205+
const cwd = yield* makeRepo({ "node_modules/pkg/index.js": "" });
206+
const target = yield* fs.makeTempDirectoryScoped({ prefix: "storage-cleanup-target-" });
207+
yield* fs.writeFileString(path.join(target, "secret.env"), "TOKEN=1");
208+
NodeFS.symlinkSync(target, path.join(cwd, "linked"), "junction");
209+
NodeFS.mkdirSync(path.join(cwd, "infra"));
210+
NodeFS.symlinkSync(target, path.join(cwd, "infra", "linked"), "junction");
211+
expect(yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/"])).toBe(false);
212+
213+
yield* fs.makeDirectory(path.join(cwd, "build"));
214+
yield* fs.writeFileString(path.join(cwd, "build", "output.bin"), "");
215+
expect(yield* storageCleanupKeepsUntrackedFiles(cwd, ["node_modules/"])).toBe(true);
216+
}),
217+
);
218+
});

‎apps/server/src/storageCleanup.ts‎

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,66 @@ export function storageCleanupActivityAt(thread: OrchestrationV2ThreadShell): nu
105105
);
106106
}
107107

108+
// Untracked and ignored files can contain secrets or local datasets, so each one
109+
// keeps the worktree unless it is ignored and matches a disposable gitignore
110+
// pattern or is a symlink or junction. Removal unlinks a link and never touches
111+
// its target.
112+
export const storageCleanupKeepsUntrackedFiles = Effect.fn("StorageCleanup.keepsUntrackedFiles")(
113+
function* (worktreePath: string, disposablePaths: ReadonlyArray<string>) {
114+
const git = yield* GitVcsDriver.GitVcsDriver;
115+
const fs = yield* FileSystem.FileSystem;
116+
const path = yield* Path.Path;
117+
const listUntracked = (args: ReadonlyArray<string>) =>
118+
git
119+
.execute({
120+
operation: "StorageCleanup.untrackedFiles",
121+
cwd: worktreePath,
122+
args: ["ls-files", "--others", "--directory", "-z", ...args],
123+
maxOutputBytes: 64 * 1024,
124+
})
125+
.pipe(
126+
Effect.map((result) =>
127+
result.stdoutTruncated ? null : result.stdout.split("\0").filter(Boolean),
128+
),
129+
);
130+
const listIgnored = (excludes: ReadonlyArray<string>) =>
131+
listUntracked(["--ignored", ...excludes]);
132+
// `status.showUntrackedFiles=no` hides these from the status check, and
133+
// `git worktree remove` deletes them anyway.
134+
const untracked = yield* listUntracked(["--exclude-standard", "--no-empty-directory"]);
135+
if (untracked === null || untracked.length > 0) return true;
136+
const ignored = yield* listIgnored(["--exclude-standard"]);
137+
if (ignored === null) return true;
138+
if (ignored.length === 0) return false;
139+
// Git matches the patterns, so they mean exactly what they mean in .gitignore.
140+
const disposable =
141+
disposablePaths.length === 0
142+
? []
143+
: yield* listIgnored(disposablePaths.map((pattern) => `--exclude=${pattern}`));
144+
if (disposable === null) return true;
145+
for (const entry of ignored) {
146+
// Git also lists an untracked directory that holds only ignored files.
147+
// Its contents are listed separately and decide on their own.
148+
if (
149+
entry.endsWith("/") &&
150+
ignored.some((other) => other !== entry && other.startsWith(entry))
151+
)
152+
continue;
153+
if (
154+
disposable.some(
155+
(match) => entry === match || (match.endsWith("/") && entry.startsWith(match)),
156+
)
157+
)
158+
continue;
159+
const link = yield* fs
160+
.readLink(path.join(worktreePath, entry.replace(/\/$/, "")))
161+
.pipe(Effect.option);
162+
if (Option.isNone(link)) return true;
163+
}
164+
return false;
165+
},
166+
);
167+
108168
export const make = Effect.gen(function* () {
109169
const config = yield* ServerConfig.ServerConfig;
110170
const settingsService = yield* Settings.ServerSettingsService;
@@ -171,6 +231,14 @@ export const make = Effect.gen(function* () {
171231
return false;
172232
});
173233

234+
const untrackedFilesContext = yield* Effect.context<
235+
GitVcsDriver.GitVcsDriver | FileSystem.FileSystem | Path.Path
236+
>();
237+
const keepsUntrackedFiles = (worktreePath: string, disposablePaths: ReadonlyArray<string>) =>
238+
storageCleanupKeepsUntrackedFiles(worktreePath, disposablePaths).pipe(
239+
Effect.provideContext(untrackedFilesContext),
240+
);
241+
174242
const cleanWorktrees = Effect.fn("StorageCleanup.cleanWorktrees")(function* (
175243
serverSettings: ServerSettings,
176244
now: number,
@@ -231,21 +299,7 @@ export const make = Effect.gen(function* () {
231299
if (!status.isRepo || status.branch !== thread.branch || status.hasWorkingTreeChanges)
232300
return;
233301
const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
234-
const ignored = yield* git.execute({
235-
operation: "StorageCleanup.ignoredFiles",
236-
cwd: worktreePath,
237-
args: ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"],
238-
maxOutputBytes: 64 * 1024,
239-
});
240-
// Ignored files can contain secrets or local datasets. Dependency installs
241-
// are reproducible; every other ignored path prevents automatic removal.
242-
if (
243-
ignored.stdoutTruncated ||
244-
ignored.stdout
245-
.split("\0")
246-
.some((entry) => entry !== "" && !/(^|\/)node_modules\/$/.test(entry))
247-
)
248-
return;
302+
if (yield* keepsUntrackedFiles(worktreePath, settings.worktreeDisposablePaths)) return;
249303
const old =
250304
!deleted &&
251305
settings.worktreeAfterDays !== null &&
@@ -345,30 +399,12 @@ export const make = Effect.gen(function* () {
345399
head.commitSha
346400
)
347401
return;
348-
const finalIgnored = yield* git.execute({
349-
operation: "StorageCleanup.ignoredFiles",
350-
cwd: worktreePath,
351-
args: ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"],
352-
maxOutputBytes: 64 * 1024,
353-
});
354-
if (
355-
finalIgnored.stdoutTruncated ||
356-
finalIgnored.stdout
357-
.split("\0")
358-
.some((entry) => entry !== "" && !/(^|\/)node_modules\/$/.test(entry))
359-
)
360-
return;
402+
if (yield* keepsUntrackedFiles(worktreePath, settings.worktreeDisposablePaths)) return;
361403
const current = resolveWorktreeCleanup(
362404
yield* settingsService.getSettings,
363405
thread.projectId,
364406
);
365-
if (
366-
Object.keys(settings).some(
367-
(key) =>
368-
current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
369-
)
370-
)
371-
return;
407+
if (!Equal.equals(current, settings)) return;
372408
yield* git.removeWorktree({ cwd: project.workspaceRoot, path: worktreePath, force: false });
373409
yield* gitManager.invalidateStatus(project.workspaceRoot);
374410
// Preserve branch and path: ProviderTurnStartService recreates the checkout

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,44 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
28952895
}),
28962896
);
28972897

2898+
it.effect("unlinks ignored directory links without deleting their targets", () =>
2899+
Effect.gen(function* () {
2900+
const cwd = yield* makeTmpDir();
2901+
const { initialBranch } = yield* initRepoWithCommit(cwd);
2902+
yield* writeTextFile(cwd, ".gitignore", "linked\n");
2903+
yield* git(cwd, ["add", ".gitignore"]);
2904+
yield* git(cwd, ["commit", "-m", "ignore links"]);
2905+
const fileSystem = yield* FileSystem.FileSystem;
2906+
const pathService = yield* Path.Path;
2907+
const outside = yield* makeTmpDir("git-worktree-link-target-");
2908+
yield* writeTextFile(outside, "keep.txt", "keep\n");
2909+
const worktreePath = pathService.join(yield* makeTmpDir("git-worktrees-"), "linked");
2910+
const driver = yield* GitVcsDriver.GitVcsDriver;
2911+
yield* driver.createWorktree({
2912+
cwd,
2913+
path: worktreePath,
2914+
refName: initialBranch,
2915+
newRefName: "feature/linked",
2916+
});
2917+
// A junction on Windows, which Git for Windows leaves behind; a symlink elsewhere.
2918+
NodeFS.symlinkSync(outside, pathService.join(worktreePath, "linked"), "junction");
2919+
NodeFS.mkdirSync(pathService.join(worktreePath, "linked-parent"));
2920+
NodeFS.symlinkSync(
2921+
outside,
2922+
pathService.join(worktreePath, "linked-parent", "linked"),
2923+
"junction",
2924+
);
2925+
2926+
yield* driver.removeWorktree({ cwd, path: worktreePath });
2927+
2928+
assert.equal(yield* fileSystem.exists(worktreePath), false);
2929+
assert.equal(
2930+
yield* fileSystem.readFileString(pathService.join(outside, "keep.txt")),
2931+
"keep\n",
2932+
);
2933+
}),
2934+
);
2935+
28982936
it.effect("allows worktree removal to run longer than the default command timeout", () =>
28992937
Effect.gen(function* () {
29002938
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
35803580
},
35813581
);
35823582
if (result.exitCode === 0) {
3583+
// Git for Windows never descends into NTFS junctions, such as pnpm's
3584+
// node_modules links. It reports success but leaves them and their parent
3585+
// directories behind, so a resumed thread would find a stub instead of
3586+
// recreating its checkout. Node's rm unlinks links without following them.
3587+
// Git has already unregistered the worktree, so a failure here is logged
3588+
// rather than returned: a retry could never succeed.
3589+
const leftover = path.resolve(input.cwd, input.path);
3590+
if (yield* fileSystem.exists(leftover).pipe(Effect.orElseSucceed(() => false))) {
3591+
yield* fileSystem.remove(leftover, { recursive: true }).pipe(
3592+
Effect.catch((error) =>
3593+
Effect.logWarning("GitVcsDriver.removeWorktree: failed to delete leftover links", {
3594+
path: leftover,
3595+
error,
3596+
}),
3597+
),
3598+
);
3599+
}
35833600
return;
35843601
}
35853602
// Threads can share a worktree path, and worktrees get removed or pruned

0 commit comments

Comments
 (0)