Skip to content

Commit 71c2d3a

Browse files
fix(server): storage report lists project worktrees nested inside plain folders (pingdotgg#25883)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 28eff11 commit 71c2d3a

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,71 @@ describe("orphan worktree reports", () => {
17971797
),
17981798
);
17991799

1800+
it.live("reports linked worktrees nested deeper inside a plain folder", () =>
1801+
runCleanupTest(
1802+
Effect.gen(function* () {
1803+
const fixture = yield* cleanupFixture();
1804+
fixture.setThreads([]);
1805+
const replay = `${fixture.config.worktreesDir}/repository/replay`;
1806+
const deep = `${fixture.config.worktreesDir}/repository/deep`;
1807+
const worktrees = [`${deep}/a/b`, `${replay}/1376`];
1808+
for (const [index, worktree] of worktrees.entries()) {
1809+
yield* fixture.command(fixture.repo, [
1810+
"worktree",
1811+
"add",
1812+
"-b",
1813+
`nested-${index}`,
1814+
worktree,
1815+
]);
1816+
}
1817+
for (const folder of [...worktrees, `${deep}/a`, deep, replay]) {
1818+
yield* fixture.fs.utimes(folder, 1, 1);
1819+
}
1820+
const report = yield* fixture.service.runNow;
1821+
expect(
1822+
report.entries
1823+
.map((entry) => ({ path: entry.path, reason: entry.reason }))
1824+
.sort((left, right) => String(left.path).localeCompare(String(right.path))),
1825+
).toEqual(worktrees.map((path) => ({ path, reason: "No thread uses this worktree" })));
1826+
}),
1827+
),
1828+
);
1829+
1830+
it.live("still reports a plain folder whose nested worktree registration is stale", () =>
1831+
runCleanupTest(
1832+
Effect.gen(function* () {
1833+
const fixture = yield* cleanupFixture();
1834+
fixture.setThreads([]);
1835+
const replay = `${fixture.config.worktreesDir}/repository/replay`;
1836+
const worktree = `${replay}/1376`;
1837+
yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "locked", worktree]);
1838+
yield* fixture.command(fixture.repo, ["worktree", "lock", worktree]);
1839+
yield* fixture.fs.rename(worktree, `${replay}/moved`);
1840+
yield* fixture.fs.makeDirectory(worktree);
1841+
yield* fixture.fs.utimes(replay, 1, 1);
1842+
expect((yield* fixture.service.runNow).entries.map((entry) => entry.path)).toEqual([
1843+
replay,
1844+
]);
1845+
}),
1846+
),
1847+
);
1848+
1849+
it.live("does not report nested worktrees inside a project checkout", () =>
1850+
runCleanupTest(
1851+
Effect.gen(function* () {
1852+
const fixture = yield* cleanupFixture();
1853+
fixture.setThreads([]);
1854+
const project = `${fixture.config.worktreesDir}/repository/replay/project`;
1855+
const worktree = `${project}/nested`;
1856+
yield* fixture.fs.makeDirectory(project, { recursive: true });
1857+
fixture.setProjectRoots([fixture.repo, project]);
1858+
yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "nested", worktree]);
1859+
for (const folder of [worktree, project]) yield* fixture.fs.utimes(folder, 1, 1);
1860+
expect((yield* fixture.service.runNow).entries).toEqual([]);
1861+
}),
1862+
),
1863+
);
1864+
18001865
it.live("continues after an inaccessible directory's git probe", (context) =>
18011866
runCleanupTest(
18021867
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,26 @@ const make = Effect.gen(function* () {
545545
},
546546
Effect.orElseSucceed(() => undefined),
547547
);
548+
// Linked worktrees can sit deeper inside a plain folder, like <repository>/replay/<n>. Ask
549+
// the projects' Git for them rather than walking folders that may hold large non-git trees.
550+
let registered: ReadonlyArray<string> | undefined;
551+
const nestedWorktrees = Effect.fnUntraced(function* (folder: string, managedRoot: string) {
552+
if (yield* fs.exists(path.join(folder, ".git"))) return [];
553+
registered ??= (yield* Effect.forEach(new Set(projectRoots), (root) =>
554+
git.listWorktreePaths(root).pipe(Effect.orElseSucceed((): string[] => [])),
555+
)).flat();
556+
// Locked registrations outlive their directory, whose path may since hold something else.
557+
return yield* Effect.filter(
558+
registered.filter(
559+
(worktree) => inside(folder, worktree) && !inProject(worktree, managedRoot),
560+
),
561+
(worktree) =>
562+
Effect.gen(function* () {
563+
if (pathKey(yield* fs.realPath(worktree)) !== pathKey(worktree)) return false;
564+
return (yield* fs.stat(path.join(worktree, ".git"))).type === "File";
565+
}).pipe(Effect.orElseSucceed(() => false)),
566+
);
567+
});
548568
for (const root of roots) {
549569
// Worktrees can be <root>/<repository>/<branch> or explicitly <root>/<name>.
550570
for (const projectDirectory of yield* directories(root, root)) {
@@ -556,7 +576,8 @@ const make = Effect.gen(function* () {
556576
return;
557577
}
558578
for (const worktreePath of yield* directories(projectDirectory, root)) {
559-
yield* report(worktreePath);
579+
const nested = yield* nestedWorktrees(worktreePath, root);
580+
for (const target of nested.length > 0 ? nested : [worktreePath]) yield* report(target);
560581
}
561582
}).pipe(Effect.orElseSucceed(() => undefined));
562583
}

0 commit comments

Comments
 (0)