@devries-cameron20 / t3code-1 / commits / 5aea99a308
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): turns recreate a worktree folder that lost its checkout (#34894)

Julius Marminge committed Nov 13, 2026 at 09:31 UTC 5aea99a308f805e932b6557ffa9a593a548f2b10
4 files changed +82 -5
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+29 -1
@@ -1127,6 +1127,8 @@ effectIt.effect(
1127
1128 const expectedTips = {
1129 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: "" },
1132 deleted: { tip: projectTip, file: "project.txt", upstream: null, base: "project-current" },
1133 pushed: {
1134 tip: threadTip,
@@ -1171,7 +1173,12 @@ effectIt.effect(
1173 yield* git(workspaceRoot, ["remote", "remove", "origin"]);
1174 }
1175 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") {
1182 yield* git(workspaceRoot, ["worktree", "prune"]);
1183 yield* git(workspaceRoot, ["branch", "-D", "feature/restore"]);
1184 }
@@ -1220,6 +1227,27 @@ effectIt.effect(
1227 }).pipe(Effect.scoped, Effect.provide(layerRealGit)),
1228 );
1229
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 +
1251 effectIt.effect(
1252 "preserves recreation interrupts and retries, then fails the final attempt before provider start",
1253 () =>
apps/server/src/storageCleanup.test.ts
+18
@@ -2332,6 +2332,24 @@ describe("storage cleanup reports and local file policies", () => {
2332 }),
2333 ),
2334 );
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 + );
2353 it.live("skips a stored worktree path whose parent is a regular file", () =>
2354 runCleanupTest(
2355 Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+4 -2
@@ -1010,8 +1010,10 @@ const make = Effect.gen(function* () {
1010 return "worktree is a symbolic link";
1011 if (!roots.some((root) => inside(root, realPath)))
1012 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";
1017 return null;
1018 });
1019 const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>`
apps/server/src/workspace/restoreMissingWorktree.ts
+31 -2
@@ -33,6 +33,9 @@ class WorktreeRecreateNoBaseError extends Schema.TaggedError<WorktreeRecreateNoB
33 }
34 }
35
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 +
39 interface WorktreeThread {
40 readonly id: ThreadId;
41 readonly projectId: ProjectId;
@@ -42,10 +45,33 @@ interface WorktreeThread {
45
46 export const makeRestoreMissingWorktree = Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 + const path = yield* Path.Path;
49 const gitWorkflow = yield* GitWorkflowService.GitWorkflowService;
50 const projects = yield* ProjectService.ProjectService;
51 const isManagedWorktreePath = yield* makeIsManagedWorktreePath;
52
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 +
75 const resolveRecreateBase = Effect.fn("restoreMissingWorktree.resolveRecreateBase")(function* (
76 cwd: string,
77 remoteName: string | null,
@@ -116,7 +142,8 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
142 const { worktreePath, branch } = thread;
143 if (worktreePath === null || branch === null) return;
144 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;
147 const project = yield* projects.getById(thread.projectId).pipe(
148 Effect.map(Option.getOrUndefined),
149 Effect.orElseSucceed(() => undefined),
@@ -127,7 +154,9 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
154 worktreePath,
155 branch,
156 });
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 })),
160 Effect.mapError((cause) =>
161 cause._tag === "WorktreeRecreateNoBaseError"
162 ? cause