Skip to content

Commit d0e427d

Browse files
t3dotggclaude
andauthored
fix(server): storage cleanup no longer counts pnpm clones as freed space (pingdotgg#22713)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 163bdea commit d0e427d

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,59 @@ describe("storage cleanup reports and local file policies", () => {
12411241
}),
12421242
),
12431243
);
1244+
it.live.each(["same-device", "other-device", "no-manifest"] as const)(
1245+
"counts the pnpm virtual store only when its store forces copies (%s)",
1246+
(store) =>
1247+
runCleanupTest(
1248+
Effect.gen(function* () {
1249+
const fs = yield* FileSystem.FileSystem;
1250+
const storeDir = yield* fs.makeTempDirectoryScoped({ prefix: "pnpm-store-" });
1251+
const { service, worktree, setPolicy } = yield* cleanupFixture().pipe(
1252+
Effect.provideService(FileSystem.FileSystem, {
1253+
...fs,
1254+
stat: (target) =>
1255+
fs
1256+
.stat(target)
1257+
.pipe(
1258+
Effect.map((info) =>
1259+
store === "other-device" && target === storeDir
1260+
? { ...info, dev: info.dev + 1n }
1261+
: info,
1262+
),
1263+
),
1264+
}),
1265+
);
1266+
setPolicy("tracked-changes");
1267+
const nodeModules = `${worktree}/node_modules`;
1268+
yield* fs.makeDirectory(`${nodeModules}/.pnpm/[email protected]`, { recursive: true });
1269+
yield* fs.makeDirectory(`${nodeModules}/.vite`);
1270+
yield* fs.writeFileString(
1271+
`${nodeModules}/.pnpm/[email protected]/index.js`,
1272+
"clone ".repeat(10_000),
1273+
);
1274+
yield* fs.writeFileString(`${nodeModules}/.vite/deps.js`, "cache");
1275+
if (store !== "no-manifest")
1276+
yield* fs.writeFileString(
1277+
`${nodeModules}/.modules.yaml`,
1278+
JSON.stringify({ storeDir, virtualStoreDir: ".pnpm" }),
1279+
);
1280+
const counted = [
1281+
...(yield* fs.readDirectory(worktree)).flatMap((name) =>
1282+
name === "node_modules" ? [] : [`${worktree}/${name}`],
1283+
),
1284+
`${nodeModules}/.vite/deps.js`,
1285+
...(store === "no-manifest" ? [] : [`${nodeModules}/.modules.yaml`]),
1286+
...(store === "same-device" ? [] : [`${nodeModules}/.pnpm/[email protected]/index.js`]),
1287+
];
1288+
const bytes = (yield* Effect.forEach(counted, (file) => allocatedBytes(fs, file))).reduce(
1289+
(sum, size) => sum + size,
1290+
0,
1291+
);
1292+
const report = yield* service.runNow;
1293+
expect(report.entries[0]).toMatchObject({ outcome: "removed", bytes });
1294+
}),
1295+
),
1296+
);
12441297
it.live("still removes a worktree when measuring its size fails", () =>
12451298
runCleanupTest(
12461299
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { PlatformError } from "effect/PlatformError";
3232
import * as Schedule from "effect/Schedule";
3333
import * as Stream from "effect/Stream";
3434
import * as SubscriptionRef from "effect/SubscriptionRef";
35+
import { parse as parseYaml } from "yaml";
3536

3637
import * as ServerConfig from "./config.ts";
3738
import * as GitManager from "./git/GitManager.ts";
@@ -55,6 +56,13 @@ const decodeCleanupThread = Schema.decodeUnknownEffect(
5556
const decodeCleanupSession = Schema.decodeUnknownEffect(
5657
Schema.fromJsonString(OrchestrationV2ProviderSessionJson),
5758
);
59+
// pnpm 11 writes this file as JSON, older versions as YAML.
60+
const decodePnpmModulesManifest = Schema.decodeUnknownEffect(
61+
Schema.Struct({
62+
storeDir: Schema.String,
63+
virtualStoreDir: Schema.optional(Schema.String),
64+
}),
65+
);
5866

5967
const DAY_MS = 86_400_000;
6068
// After a settle, the project's settle script needs time to open its
@@ -247,14 +255,35 @@ const make = Effect.gen(function* () {
247255
);
248256
});
249257

258+
// pnpm imports packages by clone or reflink when its store shares a device
259+
// with the worktree. Those files have one link but share blocks with the
260+
// store, so removing them frees almost nothing. A store on another device
261+
// forces real copies, which still count.
262+
const sharedPnpmVirtualStore = Effect.fnUntraced(
263+
function* (root: string) {
264+
const nodeModules = path.join(root, "node_modules");
265+
const text = yield* fs.readFileString(path.join(nodeModules, ".modules.yaml"));
266+
const manifest = yield* decodePnpmModulesManifest(yield* Effect.try(() => parseYaml(text)));
267+
const virtualStore = path.resolve(nodeModules, manifest.virtualStoreDir ?? ".pnpm");
268+
const [store, virtual] = yield* Effect.all([
269+
fs.stat(manifest.storeDir),
270+
fs.stat(virtualStore),
271+
]);
272+
return store.dev === virtual.dev ? virtualStore : null;
273+
},
274+
Effect.orElseSucceed(() => null),
275+
);
276+
250277
const measureWorktree = Effect.fn("storageCleanup.measureWorktree")(
251278
function* (worktreePath: string) {
252279
const root = yield* fs.realPath(worktreePath);
280+
const skipped = yield* sharedPnpmVirtualStore(root);
253281
const pending = [root];
254282
let entries = 1;
255283
let bytes = 0;
256284
while (pending.length > 0) {
257285
const target = pending.pop()!;
286+
if (target === skipped) continue;
258287
// Effect's stat follows links. Probe with readLink first to get lstat
259288
// semantics, including skipping dangling links and directory links.
260289
const isLink = yield* fs.readLink(target).pipe(

0 commit comments

Comments
 (0)