Skip to content

Commit dc0ce64

Browse files
committed
fix(storage): recheck activity after measuring worktrees
1 parent cbb68a8 commit dc0ce64

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import * as Effect from "effect/Effect";
33
import * as Context from "effect/Context";
4+
import * as Deferred from "effect/Deferred";
5+
import * as Fiber from "effect/Fiber";
46
import * as FileSystem from "effect/FileSystem";
57
import * as Layer from "effect/Layer";
68
import type * as Path from "effect/Path";
@@ -370,6 +372,47 @@ describe("storage cleanup reports and local file policies", () => {
370372
}),
371373
),
372374
);
375+
it.live("keeps a worktree when its thread starts during size measurement", () =>
376+
runCleanupTest(
377+
Effect.gen(function* () {
378+
const fs = yield* FileSystem.FileSystem;
379+
const measuring = yield* Deferred.make<void>();
380+
const resume = yield* Deferred.make<void>();
381+
let measuredPath: string | undefined;
382+
const fixture = yield* cleanupFixture.pipe(
383+
Effect.provideService(FileSystem.FileSystem, {
384+
...fs,
385+
readDirectory: (directory) =>
386+
Effect.gen(function* () {
387+
if (directory === measuredPath) {
388+
yield* Deferred.succeed(measuring, undefined);
389+
yield* Deferred.await(resume);
390+
}
391+
return yield* fs.readDirectory(directory);
392+
}),
393+
}),
394+
);
395+
measuredPath = yield* fs.realPath(fixture.worktree);
396+
fixture.setPolicy("tracked-changes");
397+
yield* fs.writeFileString(`${fixture.worktree}/notes.txt`, "untracked\n");
398+
const cleanup = yield* fixture.service.runNow.pipe(Effect.forkChild);
399+
yield* Deferred.await(measuring);
400+
fixture.setThreads([
401+
shell({ branch: "feature", worktreePath: fixture.worktree, status: "running" }),
402+
]);
403+
yield* Deferred.succeed(resume, undefined);
404+
const report = yield* Fiber.join(cleanup);
405+
expect(report.entries[0]).toMatchObject({
406+
outcome: "kept",
407+
reason: "Thread activity or shared worktree changed since check",
408+
bytes: null,
409+
});
410+
expect(report.bytesFreed).toBe(0);
411+
expect(yield* fs.exists(fixture.worktree)).toBe(true);
412+
expect(yield* fs.readFileString(`${fixture.worktree}/notes.txt`)).toBe("untracked\n");
413+
}),
414+
),
415+
);
373416
it("decodes reports from servers without size fields", () => {
374417
const report = decodeCleanupReport({
375418
trigger: "manual",

‎apps/server/src/storageCleanup.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,9 @@ const make = Effect.gen(function* () {
478478
? "not merged"
479479
: "has commits beyond the default branch",
480480
);
481-
// Re-read after Git/host calls so a queued turn, resumed session or new
482-
// thread sharing this path cancels the removal.
481+
const bytes = yield* measureWorktree(worktreePath);
482+
// Re-read after Git/host calls and size measurement so a queued turn,
483+
// resumed session or new thread sharing this path cancels the removal.
483484
const latestSnapshot = yield* readThreads();
484485
if (yield* containsProjectRoot(worktreePath, [project, ...latestSnapshot.projects]))
485486
return keep("contains a project checkout");
@@ -545,7 +546,6 @@ const make = Effect.gen(function* () {
545546
)
546547
)
547548
return keep("settings changed since check");
548-
const bytes = yield* measureWorktree(worktreePath);
549549
// Clean only untracked files; Git must still refuse removal if a tracked
550550
// edit arrives after our last status check.
551551
if (settings.worktreeKeepWhen === "tracked-changes")

0 commit comments

Comments
 (0)