@@ -13,6 +13,8 @@ import * as Effect from "effect/Effect";
1313import * as FileSystem from "effect/FileSystem" ;
1414import * as Fiber from "effect/Fiber" ;
1515import * as Path from "effect/Path" ;
16+ import * as PlatformError from "effect/PlatformError" ;
17+ import * as TestClock from "effect/testing/TestClock" ;
1618import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner" ;
1719import * as EffectAcpErrors from "effect-acp/errors" ;
1820import type * as EffectAcpSchema from "effect-acp/schema" ;
@@ -31,6 +33,85 @@ const runtimeTempDirectoryMarkerContents = (directory: string, ownerProcessId: n
3133 `${ JSON . stringify ( { schemaVersion : 1 , ownerProcessId, directory } ) } \n` ;
3234
3335it . layer ( NodeServices . layer ) ( "makeAntigravityAcpRuntime" , ( it ) => {
36+ it . effect (
37+ "preserves ownership after exhausted cleanup retries so a later sweep can recover" ,
38+ ( ) =>
39+ Effect . gen ( function * ( ) {
40+ const fileSystem = yield * FileSystem . FileSystem ;
41+ const path = yield * Path . Path ;
42+ const crypto = yield * Crypto . Crypto ;
43+ const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
44+ const orphan = yield * Effect . acquireRelease (
45+ fileSystem . makeTempDirectory ( { prefix : "t3-antigravity-runtime-2147483647-" } ) ,
46+ ( directory ) =>
47+ fileSystem . remove ( directory , { recursive : true , force : true } ) . pipe ( Effect . orDie ) ,
48+ ) ;
49+ const marker = path . join ( orphan , runtimeTempDirectoryMarkerName ) ;
50+ const markerContents = runtimeTempDirectoryMarkerContents (
51+ path . basename ( orphan ) ,
52+ 2147483647 ,
53+ ) ;
54+ yield * fileSystem . writeFileString ( marker , markerContents ) ;
55+ const payload = path . join ( orphan , "_MEIlocked" ) ;
56+ yield * fileSystem . makeDirectory ( payload ) ;
57+ yield * fileSystem . writeFileString ( path . join ( payload , "payload" ) , "locked fixture" ) ;
58+ const attempts = yield * Effect . forEach ( [ 0 , 1 , 2 , 3 , 4 , 5 ] , ( ) => Deferred . make < void > ( ) ) ;
59+ const lockedError = PlatformError . systemError ( {
60+ _tag : "PermissionDenied" ,
61+ module : "FileSystem" ,
62+ method : "remove" ,
63+ description : "EPERM: extraction is in use" ,
64+ } ) ;
65+ let locked = true ;
66+ let failures = 0 ;
67+ const runtimeFileSystem = FileSystem . FileSystem . of ( {
68+ ...fileSystem ,
69+ readDirectory : ( directory ) =>
70+ directory === path . dirname ( orphan )
71+ ? Effect . succeed ( [ path . basename ( orphan ) ] )
72+ : fileSystem . readDirectory ( directory ) ,
73+ remove : ( directory , options ) =>
74+ Effect . gen ( function * ( ) {
75+ if ( locked && ( directory === orphan || directory === payload ) ) {
76+ // Whole-directory removal can unlink the marker before a locked sibling fails.
77+ if ( directory === orphan && options ?. recursive ) {
78+ yield * fileSystem . remove ( marker , { force : true } ) ;
79+ }
80+ yield * Deferred . succeed ( attempts [ failures ++ ] ! , undefined ) ;
81+ return yield * lockedError ;
82+ }
83+ return yield * fileSystem . remove ( directory , options ) ;
84+ } ) ,
85+ // Stop after the sweep, before allocating or launching an unrelated runtime.
86+ makeTempDirectory : ( ) => Effect . fail ( lockedError ) ,
87+ } ) ;
88+ const sweep = makeAntigravityAcpRuntime ( {
89+ childProcessSpawner,
90+ fileSystem : runtimeFileSystem ,
91+ path,
92+ platform : "win32" ,
93+ spawn : { command : process . execPath , args : [ ] , cwd : process . cwd ( ) } ,
94+ cwd : process . cwd ( ) ,
95+ clientInfo : { name : "t3-code-test" , version : "0.0.0" } ,
96+ } ) . pipe ( Effect . provideService ( Crypto . Crypto , crypto ) , Effect . scoped , Effect . exit ) ;
97+ const firstSweep = yield * sweep . pipe ( Effect . forkChild ) ;
98+ for ( const [ index , attempt ] of attempts . entries ( ) ) {
99+ yield * Deferred . await ( attempt ) ;
100+ if ( index < attempts . length - 1 ) {
101+ yield * TestClock . adjust ( `${ 100 * 2 ** index } millis` ) ;
102+ }
103+ }
104+ yield * Fiber . join ( firstSweep ) ;
105+ expect ( failures ) . toBe ( 6 ) ;
106+ expect ( yield * fileSystem . exists ( payload ) ) . toBe ( true ) ;
107+ expect ( yield * fileSystem . readFileString ( marker ) ) . toBe ( markerContents ) ;
108+
109+ locked = false ;
110+ yield * sweep ;
111+ expect ( yield * fileSystem . exists ( orphan ) ) . toBe ( false ) ;
112+ } ) ,
113+ ) ;
114+
34115 it . effect ( "reclaims only owned orphans and removes the current extraction when interrupted" , ( ) =>
35116 Effect . gen ( function * ( ) {
36117 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments