Skip to content

Commit ee94417

Browse files
fix(server): recover leftover worktree folders and keep them in cleanup (adopt pingdotgg#24398)
Adopts upstream pingdotgg#24398 (head 7e369b6, open). In Alpha, storage cleanup logged "storage cleanup failed for worktree" about every ten minutes for one delegated thread whose managed worktree folder was left empty, because it stat()ed the missing `.git`. Cleanup now keeps such a folder as "Not a linked worktree", and a thread resuming in a managed folder holding only `node_modules`, `.next` or nothing clears it and recreates its checkout. Fork-specific: the leftover removal runs ahead of the fork's recreateMissingWorktree, so its failure still settles the start as "Couldn't recreate thread workspace". The new tests pass the real path service (Windows), and the startup-ordering fixture is now a linked checkout so that an empty folder does not count as leftover. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fa87256 commit ee94417

4 files changed

Lines changed: 123 additions & 9 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,65 @@ effectIt.effect("does not resume a continuation stopped while recovery was in fl
11931193
}),
11941194
);
11951195

1196+
effectIt.effect.each([["node_modules"], ["node_modules", ".next"]])(
1197+
"recreates a managed worktree folder that holds only %s",
1198+
(leftovers) =>
1199+
Effect.gen(function* () {
1200+
const fileSystem = yield* FileSystem.FileSystem;
1201+
const path = yield* Path.Path;
1202+
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-start-" });
1203+
const worktreePath = path.join(workspaceRoot, "project", "worktree");
1204+
// What a junction-skipping `git worktree remove` leaves behind: no `.git`, no files.
1205+
for (const leftover of leftovers) {
1206+
yield* fileSystem.makeDirectory(path.join(worktreePath, leftover), { recursive: true });
1207+
}
1208+
const harness = makeLocalCommandHarness({
1209+
text: "Continue",
1210+
worktreePath,
1211+
worktreesDir: workspaceRoot,
1212+
fileSystem,
1213+
path,
1214+
failReadsAfterRunning: true,
1215+
});
1216+
// `git worktree add` refuses a non-empty target, so the leftover must be gone first.
1217+
harness.createWorktree.mockImplementation(() =>
1218+
fileSystem.exists(worktreePath).pipe(
1219+
Effect.tap((exists) => Effect.sync(() => expect(exists).toBe(false))),
1220+
Effect.as({} as never),
1221+
Effect.orDie,
1222+
),
1223+
);
1224+
yield* harness.start;
1225+
expect(harness.createWorktree).toHaveBeenCalledWith({
1226+
cwd: "/tmp/native-account-project",
1227+
refName: "feature/restore",
1228+
path: worktreePath,
1229+
});
1230+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1231+
);
1232+
1233+
effectIt.effect("leaves a managed worktree folder with other files untouched", () =>
1234+
Effect.gen(function* () {
1235+
const fileSystem = yield* FileSystem.FileSystem;
1236+
const path = yield* Path.Path;
1237+
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-start-" });
1238+
const worktreePath = path.join(workspaceRoot, "project", "worktree");
1239+
yield* fileSystem.makeDirectory(path.join(worktreePath, "node_modules"), { recursive: true });
1240+
yield* fileSystem.writeFileString(path.join(worktreePath, "notes.md"), "keep me");
1241+
const harness = makeLocalCommandHarness({
1242+
text: "Continue",
1243+
worktreePath,
1244+
worktreesDir: workspaceRoot,
1245+
fileSystem,
1246+
path,
1247+
failReadsAfterRunning: true,
1248+
});
1249+
yield* harness.start;
1250+
expect(harness.createWorktree).not.toHaveBeenCalled();
1251+
expect(yield* fileSystem.readFileString(path.join(worktreePath, "notes.md"))).toBe("keep me");
1252+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1253+
);
1254+
11961255
effectIt.effect.each(["cleanup-first", "startup-first"])(
11971256
"coordinates provider startup with %s",
11981257
(order) =>
@@ -1202,6 +1261,8 @@ effectIt.effect.each(["cleanup-first", "startup-first"])(
12021261
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-start-" });
12031262
const worktreePath = path.join(workspaceRoot, "project", "worktree");
12041263
yield* fileSystem.makeDirectory(worktreePath, { recursive: true });
1264+
// A linked checkout; an empty managed folder is recreated as a leftover.
1265+
yield* fileSystem.writeFileString(path.join(worktreePath, ".git"), "gitdir: elsewhere");
12051266
const release = yield* Deferred.make<void>();
12061267
const readEntered = yield* Deferred.make<void>();
12071268
const openEntered = yield* Deferred.make<void>();

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import {
5858
pendingRestartCancelledBackgroundWork,
5959
restartCancelledBackgroundWorkNote,
6060
} from "./RestartBackgroundNote.ts";
61-
import { recreateMissingWorktree } from "./WorktreeRecreate.ts";
61+
import { recreateMissingWorktree, WorktreeRecreateError } from "./WorktreeRecreate.ts";
6262

6363
export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStartError>()(
6464
"ProviderTurnStartError",
@@ -70,6 +70,9 @@ export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStart
7070

7171
const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
7272

73+
// Folders `git worktree remove` can leave behind when they are junctions or locked.
74+
const LEFTOVER_WORKTREE_ENTRIES = new Set(["node_modules", ".next"]);
75+
7376
/**
7477
* Claude fails these before it reads the prompt: replacing a process running background work,
7578
* launch arguments whose appended system prompt cannot be read, or an MCP registration that
@@ -531,7 +534,20 @@ export const layer: Layer.Layer<
531534
const exists = yield* fileSystem
532535
.exists(worktreePath)
533536
.pipe(Effect.orElseSucceed(() => true));
534-
if (!exists && (yield* isManagedWorktreePath(worktreePath))) {
537+
const managed = yield* isManagedWorktreePath(worktreePath);
538+
// A `git worktree remove` that skips a junctioned `node_modules` leaves
539+
// a folder with no checkout. Only that leftover is cleared, never user files.
540+
const leftover =
541+
exists &&
542+
managed &&
543+
!(yield* fileSystem
544+
.exists(path.join(worktreePath, ".git"))
545+
.pipe(Effect.orElseSucceed(() => true))) &&
546+
(yield* fileSystem.readDirectory(worktreePath).pipe(
547+
Effect.map((entries) => entries.every((entry) => LEFTOVER_WORKTREE_ENTRIES.has(entry))),
548+
Effect.orElseSucceed(() => false),
549+
));
550+
if (managed && (!exists || leftover)) {
535551
const project = yield* projects.getById(projection.thread.projectId).pipe(
536552
Effect.map(Option.getOrUndefined),
537553
Effect.orElseSucceed(() => undefined),
@@ -543,12 +559,29 @@ export const layer: Layer.Layer<
543559
branch,
544560
});
545561
const recreateResult = yield* Effect.result(
546-
recreateMissingWorktree({
547-
gitWorkflow,
548-
cwd: project.workspaceRoot,
549-
worktreePath,
550-
branch,
551-
}),
562+
(leftover
563+
? fileSystem.remove(worktreePath, { recursive: true }).pipe(
564+
Effect.mapError(
565+
(cause) =>
566+
new WorktreeRecreateError({
567+
worktreePath,
568+
branch,
569+
reason: "create-failed",
570+
cause,
571+
}),
572+
),
573+
)
574+
: Effect.void
575+
).pipe(
576+
Effect.andThen(
577+
recreateMissingWorktree({
578+
gitWorkflow,
579+
cwd: project.workspaceRoot,
580+
worktreePath,
581+
branch,
582+
}),
583+
),
584+
),
552585
);
553586
if (recreateResult._tag === "Failure") {
554587
if (input.willRetry === true) return yield* recreateResult.failure;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,25 @@ it.live("preserves late tracked edits when submodules were never initialized", (
664664
),
665665
);
666666

667+
it.live("keeps a managed worktree folder left with only node_modules", () =>
668+
runCleanupTest(
669+
Effect.gen(function* () {
670+
const { service, fs, repo, worktree, command } = yield* cleanupFixture();
671+
yield* fs.makeDirectory(`${worktree}/node_modules/pkg`, { recursive: true });
672+
for (const entry of yield* fs.readDirectory(worktree)) {
673+
if (entry !== "node_modules") yield* fs.remove(`${worktree}/${entry}`, { recursive: true });
674+
}
675+
yield* command(repo, ["worktree", "prune"]);
676+
const report = yield* service.runNow;
677+
expect(report.entries.find((entry) => entry.path === worktree)).toMatchObject({
678+
outcome: "kept",
679+
reason: "Not a linked worktree",
680+
});
681+
expect(yield* fs.exists(`${worktree}/node_modules/pkg`)).toBe(true);
682+
}),
683+
),
684+
);
685+
667686
describe("subagent worktree ownership", () => {
668687
it.effect.each([
669688
"idle",

‎apps/server/src/storageCleanup.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,8 @@ const make = Effect.gen(function* () {
805805
return keep("outside the managed worktree folder");
806806
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
807807
return keep("contains a project checkout");
808-
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
808+
const gitPath = path.join(worktreePath, ".git");
809+
if (!(yield* fs.exists(gitPath)) || (yield* fs.stat(gitPath)).type !== "File")
809810
return keep("not a linked worktree");
810811
if (yield* branchChanged(worktreePath, thread.branch))
811812
return keep("repository or branch changed");

0 commit comments

Comments
 (0)