Skip to content

Commit fa516f0

Browse files
committed
fix(storage): keep orphan reports useful across sweeps
1 parent 0c33f56 commit fa516f0

4 files changed

Lines changed: 216 additions & 18 deletions

File tree

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,144 @@ describe("subagent worktree ownership", () => {
677677
});
678678

679679
describe("orphan worktree reports", () => {
680+
it.live("keeps orphan results visible when thread results fill the report", () =>
681+
runCleanupTest(
682+
Effect.gen(function* () {
683+
const fixture = yield* cleanupFixture();
684+
fixture.setThreads(
685+
Array.from({ length: 205 }, (_, index) =>
686+
shell({
687+
id: ThreadId.make(`busy-${index}`),
688+
branch: "busy",
689+
worktreePath: `${fixture.config.worktreesDir}/busy-${index}`,
690+
status: "running",
691+
}),
692+
),
693+
);
694+
for (let index = 0; index < 205; index++) {
695+
yield* fixture.fs.makeDirectory(`${fixture.config.worktreesDir}/busy-${index}`);
696+
}
697+
const orphans = ["one", "two"].map(
698+
(name) => `${fixture.config.worktreesDir}/repository/${name}`,
699+
);
700+
for (const orphan of orphans) {
701+
yield* fixture.fs.makeDirectory(orphan, { recursive: true });
702+
yield* fixture.fs.utimes(orphan, 1, 1);
703+
}
704+
const report = yield* fixture.service.runNow;
705+
expect(report.entries).toHaveLength(200);
706+
expect(
707+
report.entries
708+
.slice(0, 2)
709+
.map((entry) => entry.path)
710+
.sort(),
711+
).toEqual(orphans);
712+
expect(report.counts).toEqual({ removed: 0, kept: 207, failed: 0 });
713+
expect(report.omittedCount).toBe(7);
714+
}),
715+
),
716+
);
717+
718+
it.live.each(["canonical", "win32", "linux"] as const)(
719+
"skips an ancestor of a referenced thread worktree (%s)",
720+
(platform) =>
721+
runCleanupTest(
722+
Effect.gen(function* () {
723+
const fixture = yield* cleanupFixture().pipe(
724+
Effect.provideService(HostProcess.Platform, platform === "win32" ? "win32" : "linux"),
725+
);
726+
fixture.setThreads([]);
727+
const ancestor = `${fixture.config.worktreesDir}/repository/stack`;
728+
const worktree = `${ancestor}/feature`;
729+
yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "nested", worktree]);
730+
const alias = `${fixture.repo}/alias`;
731+
if (platform === "canonical") yield* fixture.fs.symlink(worktree, alias);
732+
fixture.setStoredWorktreePaths([
733+
platform === "canonical"
734+
? alias
735+
: platform === "win32"
736+
? worktree.toUpperCase()
737+
: worktree,
738+
]);
739+
yield* fixture.fs.utimes(ancestor, 1, 1);
740+
expect((yield* fixture.service.runNow).entries).toEqual([]);
741+
expect(yield* fixture.fs.exists(worktree)).toBe(true);
742+
}),
743+
),
744+
);
745+
746+
it.live("waits an hour before reporting a folder without a thread row", () =>
747+
runCleanupTest(
748+
Effect.gen(function* () {
749+
const fixture = yield* cleanupFixture();
750+
fixture.setThreads([]);
751+
const folder = `${fixture.config.worktreesDir}/review-guide-123/checkout`;
752+
yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "review", folder]);
753+
expect((yield* fixture.service.runNow).entries).toEqual([]);
754+
yield* fixture.fs.utimes(folder, 1, 1);
755+
expect((yield* fixture.service.runNow).entries).toEqual([
756+
expect.objectContaining({
757+
path: folder,
758+
outcome: "kept",
759+
reason: "No thread uses this worktree",
760+
}),
761+
]);
762+
expect(yield* fixture.fs.exists(`${folder}/tracked.txt`)).toBe(true);
763+
}),
764+
),
765+
);
766+
767+
it.live("continues reporting when a project root cannot be probed", () =>
768+
runCleanupTest(
769+
Effect.gen(function* () {
770+
const fixture = yield* cleanupFixture();
771+
fixture.setThreads([]);
772+
const loop = `${fixture.repo}/loop`;
773+
yield* fixture.fs.symlink(loop, loop);
774+
fixture.setProjectRoots([fixture.repo, loop]);
775+
const orphan = `${fixture.config.worktreesDir}/repository/orphan`;
776+
yield* fixture.fs.makeDirectory(orphan, { recursive: true });
777+
yield* fixture.fs.utimes(orphan, 1, 1);
778+
expect((yield* fixture.service.runNow).entries).toEqual([
779+
expect.objectContaining({ path: orphan, outcome: "kept", bytes: 0 }),
780+
]);
781+
}),
782+
),
783+
);
784+
785+
it.live("reuses measured orphan sizes until the folder is no longer an orphan", () =>
786+
runCleanupTest(
787+
Effect.gen(function* () {
788+
const fs = yield* FileSystem.FileSystem;
789+
let orphan = "";
790+
let walks = 0;
791+
const fixture = yield* cleanupFixture().pipe(
792+
Effect.provideService(FileSystem.FileSystem, {
793+
...fs,
794+
readDirectory: (directory, options) => {
795+
if (directory === orphan) walks++;
796+
return fs.readDirectory(directory, options);
797+
},
798+
}),
799+
);
800+
fixture.setThreads([]);
801+
orphan = `${fixture.config.worktreesDir}/repository/orphan`;
802+
yield* fs.makeDirectory(orphan, { recursive: true });
803+
yield* fs.writeFileString(`${orphan}/file`, "x");
804+
yield* fs.utimes(orphan, 1, 1);
805+
expect((yield* fixture.service.runNow).entries[0]?.bytes).toBe(1);
806+
yield* fs.writeFileString(`${orphan}/file`, "changed");
807+
expect((yield* fixture.service.runNow).entries[0]?.bytes).toBe(1);
808+
expect(walks).toBe(1);
809+
fixture.setStoredWorktreePaths([orphan]);
810+
expect((yield* fixture.service.runNow).entries).toEqual([]);
811+
fixture.setStoredWorktreePaths([]);
812+
expect((yield* fixture.service.runNow).entries[0]?.bytes).toBe(7);
813+
expect(walks).toBe(2);
814+
}),
815+
),
816+
);
817+
680818
it.live.each(["nested", "direct"] as const)(
681819
"reports a linked orphan's size without removing its files (%s)",
682820
(location) =>
@@ -693,6 +831,7 @@ describe("orphan worktree reports", () => {
693831
yield* fixture.fs.writeFileString(`${outside}/external`, "outside data");
694832
yield* fixture.fs.symlink(outside, `${orphan}/linked-directory`);
695833
yield* fixture.fs.symlink(`${outside}/missing`, `${orphan}/dangling-link`);
834+
yield* fixture.fs.utimes(orphan, 1, 1);
696835
const report = yield* fixture.service.runNow;
697836
expect(report.entries).toEqual([
698837
{
@@ -737,6 +876,7 @@ describe("orphan worktree reports", () => {
737876
);
738877
if (location !== "active")
739878
fixture.setStoredWorktreePaths([fixture.worktree, worktreePath]);
879+
yield* fixture.fs.utimes(worktreePath, 1, 1);
740880
const report = yield* fixture.service.runNow;
741881
expect(report.entries.filter((entry) => entry.threadId === null)).toEqual([]);
742882
expect(yield* fixture.fs.exists(worktreePath)).toBe(true);
@@ -763,6 +903,7 @@ describe("orphan worktree reports", () => {
763903
? `${worktreePath}/nested`
764904
: alias,
765905
]);
906+
yield* fixture.fs.utimes(worktreePath, 1, 1);
766907
expect((yield* fixture.service.runNow).entries).toEqual([]);
767908
expect(yield* fixture.fs.exists(worktreePath)).toBe(true);
768909
}),
@@ -821,6 +962,7 @@ describe("orphan worktree reports", () => {
821962
fixture.setProjectRoots([alias]);
822963
}
823964
fixture.setSettings({ worktreesDirectory: root });
965+
yield* fs.utimes(orphan, 1, 1);
824966
const report = yield* fixture.service.runNow;
825967
expect(report.entries).toEqual([
826968
expect.objectContaining({
@@ -846,6 +988,7 @@ describe("orphan worktree reports", () => {
846988
yield* fixture.fs.makeDirectory(removed);
847989
yield* fixture.fs.remove(removed, { recursive: true });
848990
fixture.setProjectRoots([fixture.repo, removed]);
991+
yield* fixture.fs.utimes(orphan, 1, 1);
849992
const report = yield* fixture.service.runNow;
850993
expect(report.entries).toEqual([
851994
expect.objectContaining({
@@ -870,6 +1013,7 @@ describe("orphan worktree reports", () => {
8701013
yield* fixture.fs.makeDirectory(orphan, { recursive: true });
8711014
yield* fixture.fs.writeFileString(`${orphan}/file`, "x");
8721015
fixture.setProjectRoots([fixture.repo, project]);
1016+
yield* fixture.fs.utimes(orphan, 1, 1);
8731017
const report = yield* fixture.service.runNow;
8741018
expect(report.entries).toEqual([
8751019
expect.objectContaining({ path: orphan, outcome: "kept", bytes: 1 }),
@@ -910,6 +1054,7 @@ describe("orphan worktree reports", () => {
9101054
const folder = `${fixture.config.worktreesDir}/repository/leftover`;
9111055
yield* fixture.fs.makeDirectory(folder, { recursive: true });
9121056
yield* fixture.fs.writeFileString(`${folder}/payload`, "leftover data");
1057+
yield* fixture.fs.utimes(folder, 1, 1);
9131058
const report = yield* fixture.service.runNow;
9141059
expect(report.entries).toEqual([
9151060
expect.objectContaining({
@@ -938,6 +1083,7 @@ describe("orphan worktree reports", () => {
9381083
yield* fixture.fs.makeDirectory(orphan, { recursive: true });
9391084
yield* fixture.fs.writeFileString(`${orphan}/file`, "x");
9401085
yield* fixture.fs.chmod(blocked, 0);
1086+
yield* fixture.fs.utimes(orphan, 1, 1);
9411087
const report = yield* fixture.service.runNow.pipe(
9421088
Effect.ensuring(fixture.fs.chmod(blocked, 0o700).pipe(Effect.orDie)),
9431089
);
@@ -964,6 +1110,7 @@ describe("orphan worktree reports", () => {
9641110
const nested = `${parent}/Nested`;
9651111
for (const folder of [referenced, project, nested]) {
9661112
yield* fixture.fs.makeDirectory(`${folder}/child`, { recursive: true });
1113+
yield* fixture.fs.utimes(folder, 1, 1);
9671114
}
9681115
fixture.setStoredWorktreePaths([fixture.worktree, referenced.toUpperCase()]);
9691116
fixture.setProjectRoots([fixture.repo, project, `${nested}/child`]);
@@ -984,6 +1131,7 @@ describe("orphan worktree reports", () => {
9841131
const orphan = `${fixture.config.worktreesDir}/repository/orphan-${i}`;
9851132
yield* fixture.fs.makeDirectory(orphan, { recursive: true });
9861133
yield* fixture.fs.writeFileString(`${orphan}/file`, "x");
1134+
yield* fixture.fs.utimes(orphan, 1, 1);
9871135
}
9881136
const report = yield* fixture.service.runNow;
9891137
expect(report.entries).toHaveLength(200);

‎apps/server/src/storageCleanup.ts‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const DAY_MS = 86_400_000;
6060
// After a settle, the project's settle script needs time to open its
6161
// terminal, which then protects the worktree.
6262
const SETTLE_SCRIPT_GRACE_MS = 60_000;
63+
// Worktree creation can precede its thread row, including review-guide checkouts.
64+
const ORPHAN_WORKTREE_GRACE_MS = 60 * 60 * 1_000;
6365

6466
const sentence = (text: string) => text.charAt(0).toUpperCase() + text.slice(1);
6567
const REPORT_ENTRY_LIMIT = 200;
@@ -210,6 +212,7 @@ const make = Effect.gen(function* () {
210212
const fs = yield* FileSystem.FileSystem;
211213
const path = yield* Path.Path;
212214
const liveTerminals = new Map<string, Map<string, TerminalSummary>>();
215+
const orphanSizes = new Map<string, number | null>();
213216
const noteTerminal = (terminal: TerminalSummary) => {
214217
const threadTerminals =
215218
liveTerminals.get(terminal.threadId) ?? new Map<string, TerminalSummary>();
@@ -336,7 +339,9 @@ const make = Effect.gen(function* () {
336339
);
337340
}
338341
const projectRoots = (yield* Effect.forEach(
339-
yield* Effect.filter(projects, (project) => fs.exists(path.resolve(project.workspaceRoot))),
342+
yield* Effect.filter(projects, (project) =>
343+
fs.exists(path.resolve(project.workspaceRoot)).pipe(Effect.orElseSucceed(() => true)),
344+
),
340345
(project) => {
341346
const resolved = path.resolve(project.workspaceRoot);
342347
return fs.realPath(resolved).pipe(
@@ -366,28 +371,35 @@ const make = Effect.gen(function* () {
366371
});
367372
// Share one walk budget across all orphans, instead of 30 seconds per folder.
368373
// Unmeasured folders still appear with an unknown size.
369-
const measurementDeadline = (yield* Clock.currentTimeMillis) + 30_000;
374+
const now = yield* Clock.currentTimeMillis;
375+
const measurementDeadline = now + 30_000;
370376
const reported = new Set<string>();
371377
const report = Effect.fnUntraced(
372378
function* (worktreePath: string) {
373379
const key = pathKey(worktreePath);
374380
if (
375381
reported.has(key) ||
376382
referenced.has(key) ||
383+
[...referenced].some((reference) => inside(worktreePath, reference)) ||
377384
projectRoots.some((root) => pathKey(root) === key || inside(worktreePath, root))
378385
)
379386
return;
387+
const modified = Option.getOrNull((yield* fs.stat(worktreePath)).mtime);
388+
if (modified !== null && modified.getTime() > now - ORPHAN_WORKTREE_GRACE_MS) return;
380389
const gitPath = path.join(worktreePath, ".git");
381390
const linked = (yield* fs.exists(gitPath)) && (yield* fs.stat(gitPath)).type === "File";
382391
reported.add(key);
383392
const remaining = measurementDeadline - (yield* Clock.currentTimeMillis);
384-
const bytes =
385-
remaining > 0
386-
? yield* measureWorktree(worktreePath).pipe(
387-
Effect.timeout(remaining),
388-
Effect.orElseSucceed(() => null),
389-
)
390-
: null;
393+
if (!orphanSizes.has(key) && remaining > 0) {
394+
orphanSizes.set(
395+
key,
396+
yield* measureWorktree(worktreePath).pipe(
397+
Effect.timeout(remaining),
398+
Effect.orElseSucceed(() => null),
399+
),
400+
);
401+
}
402+
const bytes = orphanSizes.get(key) ?? null;
391403
entries.push({
392404
kind: "worktree",
393405
outcome: "kept",
@@ -419,6 +431,9 @@ const make = Effect.gen(function* () {
419431
}).pipe(Effect.orElseSucceed(() => undefined));
420432
}
421433
}
434+
for (const key of orphanSizes.keys()) {
435+
if (!reported.has(key)) orphanSizes.delete(key);
436+
}
422437
});
423438

424439
// Whether cwd stopped being a checkout of this branch. Two small Git reads,
@@ -973,14 +988,19 @@ const make = Effect.gen(function* () {
973988
counts[entry.outcome]++;
974989
if (entry.outcome !== "kept") bytesFreed += entry.bytes ?? 0;
975990
}
976-
const priority = { failed: 0, removed: 1, kept: 2 };
991+
const priority = (entry: StorageCleanupReportEntry) =>
992+
entry.outcome === "failed"
993+
? 0
994+
: entry.outcome === "removed"
995+
? 1
996+
: entry.kind === "worktree" && entry.threadId === null
997+
? 2
998+
: 3;
977999
const latestReport: StorageCleanupReport = {
9781000
trigger,
9791001
startedAt: DateTime.formatIso(DateTime.makeUnsafe(now)),
9801002
finishedAt: DateTime.formatIso(yield* DateTime.now),
981-
entries: entries
982-
.sort((a, b) => priority[a.outcome] - priority[b.outcome])
983-
.slice(0, REPORT_ENTRY_LIMIT),
1003+
entries: entries.sort((a, b) => priority(a) - priority(b)).slice(0, REPORT_ENTRY_LIMIT),
9841004
counts,
9851005
bytesFreed,
9861006
omittedCount: Math.max(0, entries.length - REPORT_ENTRY_LIMIT),

0 commit comments

Comments
 (0)