Skip to content

Commit 5aea99a

Browse files
fix(server): turns recreate a worktree folder that lost its checkout (pingdotgg#34894)
1 parent 713731f commit 5aea99a

4 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,8 @@ effectIt.effect(
11271127

11281128
const expectedTips = {
11291129
retained: { tip: threadTip, file: "thread.txt", upstream: null, base: "" },
1130+
// A failed removal can leave the folder behind with only dependencies inside.
1131+
leftover: { tip: threadTip, file: "thread.txt", upstream: null, base: "" },
11301132
deleted: { tip: projectTip, file: "project.txt", upstream: null, base: "project-current" },
11311133
pushed: {
11321134
tip: threadTip,
@@ -1171,7 +1173,12 @@ effectIt.effect(
11711173
yield* git(workspaceRoot, ["remote", "remove", "origin"]);
11721174
}
11731175
yield* fileSystem.remove(worktreePath, { recursive: true });
1174-
if (scenario !== "retained") {
1176+
if (scenario === "leftover") {
1177+
yield* fileSystem.makeDirectory(path.join(worktreePath, "node_modules"), {
1178+
recursive: true,
1179+
});
1180+
}
1181+
if (scenario !== "retained" && scenario !== "leftover") {
11751182
yield* git(workspaceRoot, ["worktree", "prune"]);
11761183
yield* git(workspaceRoot, ["branch", "-D", "feature/restore"]);
11771184
}
@@ -1220,6 +1227,27 @@ effectIt.effect(
12201227
}).pipe(Effect.scoped, Effect.provide(layerRealGit)),
12211228
);
12221229

1230+
effectIt.effect("leaves a managed worktree folder without .git that holds user files", () =>
1231+
Effect.gen(function* () {
1232+
const fileSystem = yield* FileSystem.FileSystem;
1233+
const path = yield* Path.Path;
1234+
const worktreesDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-leftover-" });
1235+
const worktreePath = path.join(worktreesDir, "project", "restore");
1236+
yield* fileSystem.makeDirectory(path.join(worktreePath, "node_modules"), { recursive: true });
1237+
yield* fileSystem.writeFileString(path.join(worktreePath, "notes.md"), "keep me");
1238+
const harness = makeLocalCommandHarness({
1239+
text: "Continue",
1240+
worktreePath,
1241+
worktreesDir,
1242+
fileSystem,
1243+
failReadsAfterRunning: true,
1244+
});
1245+
yield* harness.start;
1246+
expect(harness.createWorktree).not.toHaveBeenCalled();
1247+
expect(yield* fileSystem.readFileString(path.join(worktreePath, "notes.md"))).toBe("keep me");
1248+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1249+
);
1250+
12231251
effectIt.effect(
12241252
"preserves recreation interrupts and retries, then fails the final attempt before provider start",
12251253
() =>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,24 @@ describe("storage cleanup reports and local file policies", () => {
23322332
}),
23332333
),
23342334
);
2335+
it.effect("keeps a thread's worktree folder that lost its .git link", () =>
2336+
runCleanupTest(
2337+
Effect.gen(function* () {
2338+
yield* TestClock.setTime(NOW_MS);
2339+
const fixture = yield* cleanupFixture();
2340+
fixture.setThreads([
2341+
shell({ branch: "feature", worktreePath: fixture.worktree, archivedAt: at(-DAY_MS) }),
2342+
]);
2343+
yield* fixture.fs.remove(`${fixture.worktree}/.git`);
2344+
yield* fixture.fs.makeDirectory(`${fixture.worktree}/node_modules`);
2345+
const report = yield* fixture.service.runNow;
2346+
expect(report.entries).toEqual([
2347+
expect.objectContaining({ path: fixture.worktree, outcome: "kept" }),
2348+
]);
2349+
expect(yield* fixture.fs.exists(`${fixture.worktree}/node_modules`)).toBe(true);
2350+
}),
2351+
),
2352+
);
23352353
it.live("skips a stored worktree path whose parent is a regular file", () =>
23362354
runCleanupTest(
23372355
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,10 @@ const make = Effect.gen(function* () {
10101010
return "worktree is a symbolic link";
10111011
if (!roots.some((root) => inside(root, realPath)))
10121012
return "outside the managed worktree folder";
1013-
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
1014-
return "not a linked worktree";
1013+
const gitLink = yield* fs
1014+
.stat(path.join(worktreePath, ".git"))
1015+
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined));
1016+
if (gitLink?.type !== "File") return "not a linked worktree";
10151017
return null;
10161018
});
10171019
const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>`

‎apps/server/src/workspace/restoreMissingWorktree.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class WorktreeRecreateNoBaseError extends Schema.TaggedError<WorktreeRecreateNoB
3333
}
3434
}
3535

36+
// `git worktree remove` skips pnpm junctions on Windows, leaving these behind without a checkout.
37+
const LEFTOVER_WORKTREE_ENTRIES = new Set(["node_modules", ".next"]);
38+
3639
interface WorktreeThread {
3740
readonly id: ThreadId;
3841
readonly projectId: ProjectId;
@@ -42,10 +45,33 @@ interface WorktreeThread {
4245

4346
export const makeRestoreMissingWorktree = Effect.gen(function* () {
4447
const fileSystem = yield* FileSystem.FileSystem;
48+
const path = yield* Path.Path;
4549
const gitWorkflow = yield* GitWorkflowService.GitWorkflowService;
4650
const projects = yield* ProjectService.ProjectService;
4751
const isManagedWorktreePath = yield* makeIsManagedWorktreePath;
4852

53+
// Only folders holding nothing but leftovers are cleared, never user files.
54+
const isLeftoverFolder = (worktreePath: string) =>
55+
fileSystem.exists(path.join(worktreePath, ".git")).pipe(
56+
Effect.flatMap((hasGit) =>
57+
hasGit
58+
? Effect.succeed(false)
59+
: fileSystem.readDirectory(worktreePath).pipe(
60+
Effect.flatMap((entries) =>
61+
Effect.forEach(entries, (entry) =>
62+
LEFTOVER_WORKTREE_ENTRIES.has(entry)
63+
? fileSystem
64+
.stat(path.join(worktreePath, entry))
65+
.pipe(Effect.map((info) => info.type === "Directory"))
66+
: Effect.succeed(false),
67+
),
68+
),
69+
Effect.map((leftovers) => leftovers.every(Boolean)),
70+
),
71+
),
72+
Effect.orElseSucceed(() => false),
73+
);
74+
4975
const resolveRecreateBase = Effect.fn("restoreMissingWorktree.resolveRecreateBase")(function* (
5076
cwd: string,
5177
remoteName: string | null,
@@ -116,7 +142,8 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
116142
const { worktreePath, branch } = thread;
117143
if (worktreePath === null || branch === null) return;
118144
const exists = yield* fileSystem.exists(worktreePath).pipe(Effect.orElseSucceed(() => true));
119-
if (exists || !(yield* isManagedWorktreePath(worktreePath))) return;
145+
const leftover = exists && (yield* isLeftoverFolder(worktreePath));
146+
if ((exists && !leftover) || !(yield* isManagedWorktreePath(worktreePath))) return;
120147
const project = yield* projects.getById(thread.projectId).pipe(
121148
Effect.map(Option.getOrUndefined),
122149
Effect.orElseSucceed(() => undefined),
@@ -127,7 +154,9 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
127154
worktreePath,
128155
branch,
129156
});
130-
yield* recreateMissingWorktree({ cwd: project.workspaceRoot, worktreePath, branch }).pipe(
157+
// `git worktree add` refuses a non-empty target.
158+
yield* (leftover ? fileSystem.remove(worktreePath, { recursive: true }) : Effect.void).pipe(
159+
Effect.andThen(recreateMissingWorktree({ cwd: project.workspaceRoot, worktreePath, branch })),
131160
Effect.mapError((cause) =>
132161
cause._tag === "WorktreeRecreateNoBaseError"
133162
? cause

0 commit comments

Comments
 (0)