@devries-cameron20 / t3code Mirror · read-only / commits / d0e427d4c3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 20:45 UTC

fix(server): storage cleanup no longer counts pnpm clones as freed space (#22713)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 21, 2026 at 00:33 UTC d0e427d4c3266395b81a1f37a70236aa4968c99d
2 files changed +82
apps/server/src/storageCleanup.test.ts
+53
@@ -1241,6 +1241,59 @@ describe("storage cleanup reports and local file policies", () => {
1241 }),
1242 ),
1243 );
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/pkg@1.0.0`, { recursive: true });
1269 + yield* fs.makeDirectory(`${nodeModules}/.vite`);
1270 + yield* fs.writeFileString(
1271 + `${nodeModules}/.pnpm/pkg@1.0.0/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/pkg@1.0.0/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 + );
1297 it.live("still removes a worktree when measuring its size fails", () =>
1298 runCleanupTest(
1299 Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+29
@@ -32,6 +32,7 @@ import type { PlatformError } from "effect/PlatformError";
32 import * as Schedule from "effect/Schedule";
33 import * as Stream from "effect/Stream";
34 import * as SubscriptionRef from "effect/SubscriptionRef";
35 +import { parse as parseYaml } from "yaml";
36
37 import * as ServerConfig from "./config.ts";
38 import * as GitManager from "./git/GitManager.ts";
@@ -55,6 +56,13 @@ const decodeCleanupThread = Schema.decodeUnknownEffect(
56 const decodeCleanupSession = Schema.decodeUnknownEffect(
57 Schema.fromJsonString(OrchestrationV2ProviderSessionJson),
58 );
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 +);
66
67 const DAY_MS = 86_400_000;
68 // After a settle, the project's settle script needs time to open its
@@ -247,14 +255,35 @@ const make = Effect.gen(function* () {
255 );
256 });
257
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 +
277 const measureWorktree = Effect.fn("storageCleanup.measureWorktree")(
278 function* (worktreePath: string) {
279 const root = yield* fs.realPath(worktreePath);
280 + const skipped = yield* sharedPnpmVirtualStore(root);
281 const pending = [root];
282 let entries = 1;
283 let bytes = 0;
284 while (pending.length > 0) {
285 const target = pending.pop()!;
286 + if (target === skipped) continue;
287 // Effect's stat follows links. Probe with readLink first to get lstat
288 // semantics, including skipping dangling links and directory links.
289 const isLink = yield* fs.readLink(target).pipe(