@@ -32,6 +32,7 @@ import type { PlatformError } from "effect/PlatformError";
3232import * as Schedule from "effect/Schedule" ;
3333import * as Stream from "effect/Stream" ;
3434import * as SubscriptionRef from "effect/SubscriptionRef" ;
35+ import { parse as parseYaml } from "yaml" ;
3536
3637import * as ServerConfig from "./config.ts" ;
3738import * as GitManager from "./git/GitManager.ts" ;
@@ -55,6 +56,13 @@ const decodeCleanupThread = Schema.decodeUnknownEffect(
5556const 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
5967const 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