@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22import fs from 'node:fs' ;
33import os from 'node:os' ;
44import path from 'node:path' ;
5+ import { setTimeout as sleep } from 'node:timers/promises' ;
56import { createAgentDeviceClient } from '../../../src/agent-device-client.ts' ;
67import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client' ;
78import type { AgentDeviceClient } from '../../../src/client/client-types.ts' ;
@@ -130,7 +131,7 @@ export async function createProviderScenarioHarness(
130131 setSession : ( name , session ) => sessionStore . set ( name , session ) ,
131132 close : async ( ) => {
132133 await deviceRuntimeGateway . shutdown ( ) ;
133- removeProviderScenarioTempDir ( sessionDir ) ;
134+ await removeProviderScenarioTempDir ( sessionDir ) ;
134135 } ,
135136 } ;
136137}
@@ -164,12 +165,21 @@ export async function withProviderScenarioTempDir<TResult>(
164165 try {
165166 return await run ( dir ) ;
166167 } finally {
167- removeProviderScenarioTempDir ( dir ) ;
168+ await removeProviderScenarioTempDir ( dir ) ;
168169 }
169170}
170171
171- function removeProviderScenarioTempDir ( dir : string ) : void {
172- fs . rmSync ( dir , PROVIDER_SCENARIO_TEMP_REMOVE_OPTIONS ) ;
172+ async function removeProviderScenarioTempDir ( dir : string ) : Promise < void > {
173+ for ( let attempt = 0 ; ; attempt += 1 ) {
174+ try {
175+ fs . rmSync ( dir , PROVIDER_SCENARIO_TEMP_REMOVE_OPTIONS ) ;
176+ return ;
177+ } catch ( error ) {
178+ const code = ( error as NodeJS . ErrnoException ) . code ;
179+ if ( attempt >= 5 || ! [ 'EBUSY' , 'ENOTEMPTY' , 'EPERM' ] . includes ( code ?? '' ) ) throw error ;
180+ await sleep ( 50 * ( attempt + 1 ) ) ;
181+ }
182+ }
173183}
174184
175185export function restoreEnv ( key : string , previous : string | undefined ) : void {
0 commit comments