Skip to content

Commit a1884a2

Browse files
fix(server,web): keep old worktree locations managed and reject drive roots
- Remember previous custom locations so cleanup and review diffs still cover worktrees left there after the setting changes. - Reject filesystem roots as a location; they would widen the review boundary to the whole drive. - Cleanup compares canonical paths (symlinked drives work) and skips only an unavailable root instead of the whole sweep. - Give the location row its own environment-scoped search entry. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bc540a1 commit a1884a2

11 files changed

Lines changed: 124 additions & 19 deletions

File tree

‎apps/server/src/review/ReviewService.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function makeLayer(input: {
1515
readonly workspaceRoot: string;
1616
readonly baseDir: string;
1717
readonly detectCalls?: Array<{ readonly cwd: string }>;
18+
readonly worktreesDirectory?: string;
19+
readonly previousWorktreesDirectories?: ReadonlyArray<string>;
1820
}) {
1921
return ReviewService.layer.pipe(
2022
Layer.provide(
@@ -29,7 +31,12 @@ function makeLayer(input: {
2931
}),
3032
),
3133
Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
32-
Layer.provide(ServerSettings.ServerSettingsService.layerTest()),
34+
Layer.provide(
35+
ServerSettings.ServerSettingsService.layerTest({
36+
worktreesDirectory: input.worktreesDirectory ?? "",
37+
previousWorktreesDirectories: [...(input.previousWorktreesDirectories ?? [])],
38+
}),
39+
),
3340
Layer.provide(ServerConfig.layerTest(input.workspaceRoot, input.baseDir)),
3441
Layer.provideMerge(NodeServices.layer),
3542
);
@@ -92,6 +99,37 @@ describe("ReviewService", () => {
9299
}).pipe(Effect.provide(NodeServices.layer)),
93100
);
94101

102+
it.effect("allows previous custom worktree locations but never a filesystem root", () =>
103+
Effect.gen(function* () {
104+
const fs = yield* FileSystem.FileSystem;
105+
const workspaceRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-review-workspace-" });
106+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-review-base-" });
107+
const previous = yield* fs.makeTempDirectoryScoped({ prefix: "t3-review-old-worktrees-" });
108+
const outsideRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-review-outside-" });
109+
110+
const result = yield* Effect.gen(function* () {
111+
const review = yield* ReviewService.ReviewService;
112+
return yield* review.getDiffPreview({ cwd: previous });
113+
}).pipe(
114+
Effect.provide(
115+
makeLayer({
116+
workspaceRoot,
117+
baseDir,
118+
worktreesDirectory: "/",
119+
previousWorktreesDirectories: [previous],
120+
}),
121+
),
122+
);
123+
assert.strictEqual(result.cwd, previous);
124+
125+
const error = yield* Effect.gen(function* () {
126+
const review = yield* ReviewService.ReviewService;
127+
return yield* review.getDiffPreview({ cwd: outsideRoot }).pipe(Effect.flip);
128+
}).pipe(Effect.provide(makeLayer({ workspaceRoot, baseDir, worktreesDirectory: "/" })));
129+
assert.strictEqual(error._tag, "VcsRepositoryDetectionError");
130+
}).pipe(Effect.provide(NodeServices.layer)),
131+
);
132+
95133
it.effect("allows diff preview cwd inside the configured workspace root", () =>
96134
Effect.gen(function* () {
97135
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/review/ReviewService.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ export const make = Effect.gen(function* () {
7070
operation: "ReviewService.getDiffPreview" | "ReviewService.getDiffFileContents",
7171
cwd: string,
7272
) {
73-
const worktreesDirectory = yield* settings.getSettings.pipe(
74-
Effect.map((current) => current.worktreesDirectory),
75-
Effect.orElseSucceed(() => ""),
73+
const worktreesDirectories = yield* settings.getSettings.pipe(
74+
Effect.orElseSucceed(() => ({ worktreesDirectory: "", previousWorktreesDirectories: [] })),
7675
);
7776
const [candidate, workspaceRoot, worktreesRoots] = yield* Effect.all([
7877
canonicalizePath(cwd),
7978
canonicalizePath(config.cwd),
8079
Effect.forEach(
81-
managedWorktreesDirectories(worktreesDirectory, config.worktreesDir, path),
80+
managedWorktreesDirectories(worktreesDirectories, config.worktreesDir, path),
8281
canonicalizePath,
8382
),
8483
]);

‎apps/server/src/storageCleanup.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,16 @@ export const make = Effect.gen(function* () {
179179
if (!anyWorktreePolicy(serverSettings, worktreeCleanupEnabled)) return;
180180
const roots: Array<string> = [];
181181
for (const directory of managedWorktreesDirectories(
182-
serverSettings.worktreesDirectory,
182+
serverSettings,
183183
config.worktreesDir,
184184
path,
185185
)) {
186-
if (yield* fs.exists(directory)) roots.push(yield* fs.realPath(directory));
186+
// An unmounted drive only skips its own worktrees.
187+
const root = yield* fs.exists(directory).pipe(
188+
Effect.flatMap((exists) => (exists ? fs.realPath(directory) : Effect.succeed(null))),
189+
Effect.orElseSucceed(() => null),
190+
);
191+
if (root !== null) roots.push(root);
187192
}
188193
if (roots.length === 0) return;
189194
const hasDeleteRule = anyWorktreePolicy(serverSettings, (rules) => rules.worktreeOnDelete);
@@ -230,9 +235,13 @@ export const make = Effect.gen(function* () {
230235
)
231236
continue;
232237
yield* Effect.gen(function* () {
233-
if (!roots.some((root) => inside(root, worktreePath)) || !(yield* fs.exists(worktreePath)))
234-
return;
235-
if ((yield* fs.realPath(worktreePath)) !== worktreePath) return;
238+
if (!(yield* fs.exists(worktreePath))) return;
239+
// Roots are canonical, so compare canonical paths. A symlinked parent
240+
// (a linked drive) is fine; a symlinked worktree directory is not.
241+
const realPath = yield* fs.realPath(worktreePath);
242+
const realParent = yield* fs.realPath(path.dirname(worktreePath));
243+
if (realPath !== path.join(realParent, path.basename(worktreePath))) return;
244+
if (!roots.some((root) => inside(root, realPath))) return;
236245
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects])) return;
237246
// A linked worktree has a .git file. Never remove a main checkout.
238247
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File") return;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3008,7 +3008,15 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
30083008
{ worktreesDirectory: "relative/worktrees" },
30093009
)
30103010
.pipe(Effect.flip);
3011-
assert.match(error.detail, /not an absolute path on this machine/);
3011+
assert.match(error.detail, /must be an absolute folder on this machine/);
3012+
3013+
const rootError = yield* driver
3014+
.createWorktree(
3015+
{ cwd, path: null, refName: initialBranch, newRefName: "feature/root-dir" },
3016+
{ worktreesDirectory: "/" },
3017+
)
3018+
.pipe(Effect.flip);
3019+
assert.match(rootError.detail, /not a drive root/);
30123020
}),
30133021
);
30143022

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3358,7 +3358,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
33583358
operation: "GitVcsDriver.createWorktree",
33593359
command: "git worktree add",
33603360
cwd: input.cwd,
3361-
detail: `The worktree location "${options?.worktreesDirectory}" is not an absolute path on this machine. Change it in Settings → Storage.`,
3361+
detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings → Storage.`,
33623362
});
33633363
}
33643364
worktreePath = path.join(parentDir, repoName, sanitizedBranch);

‎apps/server/src/worktreesDirectory.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { expandHomePathWith } from "./pathExpansion.ts";
66
* Directory new worktrees are created under: the `worktreesDirectory`
77
* setting, or `defaultDir` (`<T3 home>/worktrees`) when it is empty. Null when
88
* the setting is not an absolute path on this machine, such as `D:\worktrees`
9-
* configured for a Windows server and synced to a Linux one.
9+
* configured for a Windows server and synced to a Linux one, or when it is a
10+
* filesystem root, which would make every path on that drive look managed.
1011
*/
1112
export function resolveWorktreesDirectory(
1213
setting: string,
@@ -15,15 +16,24 @@ export function resolveWorktreesDirectory(
1516
): string | null {
1617
if (setting === "") return defaultDir;
1718
const expanded = expandHomePathWith(setting, path);
18-
return path.isAbsolute(expanded) ? path.resolve(expanded) : null;
19+
if (!path.isAbsolute(expanded)) return null;
20+
const resolved = path.resolve(expanded);
21+
return path.dirname(resolved) === resolved ? null : resolved;
1922
}
2023

2124
/** Every directory that holds T3-managed worktrees on this machine. */
2225
export function managedWorktreesDirectories(
23-
setting: string,
26+
settings: {
27+
readonly worktreesDirectory: string;
28+
readonly previousWorktreesDirectories: ReadonlyArray<string>;
29+
},
2430
defaultDir: string,
2531
path: Path.Path,
2632
): ReadonlyArray<string> {
27-
const custom = resolveWorktreesDirectory(setting, defaultDir, path);
28-
return custom === null || custom === defaultDir ? [defaultDir] : [defaultDir, custom];
33+
const directories = new Set([defaultDir]);
34+
for (const setting of [settings.worktreesDirectory, ...settings.previousWorktreesDirectories]) {
35+
const directory = resolveWorktreesDirectory(setting, defaultDir, path);
36+
if (directory !== null) directories.add(directory);
37+
}
38+
return [...directories];
2939
}

‎apps/web/src/components/settings/StorageSettings.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import { SettingsScopeNotice } from "./SettingsScopeNotice";
2222
import type { ScopedSettingsTarget } from "./scopedSettings";
2323
import { useSettingsScope } from "./SettingsScopeContext";
24+
import { searchableSetting } from "./settingsSearch";
2425
import {
2526
useClearScopedSettings,
2627
useScopedSettings,
@@ -45,7 +46,7 @@ function WorktreesDirectoryRow() {
4546

4647
return (
4748
<SettingsRow
48-
title="Worktree location"
49+
{...searchableSetting("storage-worktrees-location")}
4950
description={
5051
"Folder where new worktrees are created, on any drive, such as D:\\worktrees or ~/worktrees. Existing worktrees stay where they are. Leave empty to use the T3 home folder."
5152
}

‎apps/web/src/components/settings/settingsSearch.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ export const SETTINGS_SEARCH_ITEMS = [
141141
scope: "project-defaults",
142142
searchTerms: [
143143
"disk storage delete deleted archived threads old inactive merged unchanged worktrees retention days project inherit off custom",
144-
"worktree location folder directory path drive external",
145144
],
146145
},
146+
{
147+
id: "storage-worktrees-location",
148+
title: "Worktree location",
149+
to: "/settings/storage",
150+
scope: "environment-defaults",
151+
searchTerms: ["worktree location folder directory path drive external disk"],
152+
},
147153
{
148154
id: "storage-artifacts",
149155
title: "Artifacts and logs",

‎packages/contracts/src/settings.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,13 @@ export const ServerSettings = Schema.Struct({
12221222
* or `~/worktrees`. Empty uses `<T3 home>/worktrees`.
12231223
*/
12241224
worktreesDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
1225+
/**
1226+
* Custom locations used before the current one. Server-maintained so
1227+
* worktrees left there stay eligible for cleanup and review diffs.
1228+
*/
1229+
previousWorktreesDirectories: Schema.Array(TrimmedString).pipe(
1230+
Schema.withDecodingDefault(Effect.succeed([])),
1231+
),
12251232
responseStreamingMode: ResponseStreamingMode.pipe(
12261233
Schema.withDecodingDefault(Effect.succeed("paragraph" as const)),
12271234
),

‎packages/shared/src/serverSettings.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,3 +795,16 @@ describe("serverSettings helpers", () => {
795795
expect(resolved.pauseWhenOnBattery).toBe(false);
796796
});
797797
});
798+
799+
describe("worktreesDirectory", () => {
800+
it("remembers previous custom locations so their worktrees stay managed", () => {
801+
const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { worktreesDirectory: "/a" });
802+
expect(first.previousWorktreesDirectories).toEqual([]);
803+
const second = applyServerSettingsPatch(first, { worktreesDirectory: "/b" });
804+
expect(second.previousWorktreesDirectories).toEqual(["/a"]);
805+
const reset = applyServerSettingsPatch(second, { worktreesDirectory: "" });
806+
expect(reset.previousWorktreesDirectories).toEqual(["/a", "/b"]);
807+
const back = applyServerSettingsPatch(reset, { worktreesDirectory: "/a" });
808+
expect(back.previousWorktreesDirectories).toEqual(["/b"]);
809+
});
810+
});

0 commit comments

Comments
 (0)