@@ -144,6 +144,18 @@ async function waitForPolicyJob(
144144 throw new Error ( "policy job did not finish" ) ;
145145}
146146
147+ async function waitForCondition (
148+ description : string ,
149+ condition : ( ) => boolean ,
150+ timeoutMs = 8_000 ,
151+ ) : Promise < void > {
152+ const deadline = Date . now ( ) + timeoutMs ;
153+ while ( ! condition ( ) ) {
154+ if ( Date . now ( ) >= deadline ) throw new Error ( `timed out waiting for ${ description } ` ) ;
155+ await Bun . sleep ( 20 ) ;
156+ }
157+ }
158+
147159/** Windows can keep SQLite/job handles briefly after stop; retry only transient cleanup codes. */
148160function removeTree ( path : string ) : void {
149161 let lastError : unknown ;
@@ -313,13 +325,20 @@ describe("storage mutation coordinator", () => {
313325
314326 test ( "cleanup quarantine and permanent are rejected while restore holds slot after file moves" , async ( ) => {
315327 const home = isolatedCodexHome ! . path ;
316- const holdMs = 2500 ;
328+ const movedReadyPath = join ( testDir , "restore-files-moved.ready" ) ;
329+ const releaseRestorePath = join ( testDir , "release-restore" ) ;
317330 setRestoreTrashJobTestHooks ( {
318- restoreTest : { holdAfterFileMovesMs : holdMs } ,
331+ restoreTest : {
332+ pauseAfterFileMoves : {
333+ readyPath : movedReadyPath ,
334+ releasePath : releaseRestorePath ,
335+ } ,
336+ } ,
319337 } ) ;
320338 seedArchivedPair ( home ) ;
321339
322340 const server = startServer ( 0 ) ;
341+ let restorePromise : Promise < Response > | null = null ;
323342 try {
324343 const preview = await previewDigest ( server . url , 50 ) ;
325344 const cleanupRes = await fetch ( new URL ( "/api/storage/cleanup" , server . url ) , {
@@ -336,17 +355,17 @@ describe("storage mutation coordinator", () => {
336355 const remainingPreview = await previewDigest ( server . url , 50 ) ;
337356 expect ( remainingPreview . count ) . toBe ( 1 ) ;
338357
339- const restorePromise = fetch ( new URL ( "/api/storage/trash/restore" , server . url ) , {
358+ restorePromise = fetch ( new URL ( "/api/storage/trash/restore" , server . url ) , {
340359 method : "POST" ,
341360 headers : { "content-type" : "application/json" } ,
342361 body : JSON . stringify ( { id : trashId } ) ,
343362 } ) ;
344363
345364 const restoredPath = join ( home , "archived_sessions" , "rollout-old.jsonl" ) ;
346- const movedDeadline = Date . now ( ) + 8000 ;
347- while ( ! existsSync ( restoredPath ) && Date . now ( ) < movedDeadline ) {
348- await Bun . sleep ( 20 ) ;
349- }
365+ await waitForCondition (
366+ "restore worker to finish file moves" ,
367+ ( ) => existsSync ( movedReadyPath ) ,
368+ ) ;
350369 expect ( existsSync ( restoredPath ) ) . toBe ( true ) ;
351370 expect ( existsSync ( join ( trashStage , "rollout-old.jsonl" ) ) ) . toBe ( false ) ;
352371 expect ( existsSync ( join ( trashStage , "restore-pending.json" ) ) ) . toBe ( true ) ;
@@ -378,6 +397,7 @@ describe("storage mutation coordinator", () => {
378397 expect ( permanentDuring . status ) . toBe ( 409 ) ;
379398 expect ( ( await permanentDuring . json ( ) ) . error ) . toBe ( "storage_mutation_busy" ) ;
380399
400+ writeFileSync ( releaseRestorePath , "release\n" ) ;
381401 const restoreRes = await restorePromise ;
382402 expect ( restoreRes . status ) . toBe ( 200 ) ;
383403 const restored = await restoreRes . json ( ) ;
@@ -386,26 +406,41 @@ describe("storage mutation coordinator", () => {
386406 expect ( threadCount ( home ) ) . toBe ( 2 ) ;
387407 expect ( readFileSync ( restoredPath , "utf8" ) ) . toBe ( "o" . repeat ( 100 ) ) ;
388408 } finally {
409+ // Never strand the Worker if an assertion above fails; also consume the
410+ // request so its rejection cannot leak into the next isolated test.
411+ writeFileSync ( releaseRestorePath , "release\n" ) ;
412+ if ( restorePromise ) await restorePromise . catch ( ( ) => undefined ) ;
389413 await stopRaceServer ( server ) ;
390414 }
391415 } , { timeout : 45_000 } ) ;
392416
393417 test ( "restore is rejected while cleanup holds the shared mutation slot" , async ( ) => {
394418 const home = isolatedCodexHome ! . path ;
395- const blockMs = 1200 ;
396- setArchivedCleanupJobTestHooks ( { blockMs } ) ;
419+ const cleanupReadyPath = join ( testDir , "cleanup-slot-acquired.ready" ) ;
420+ const releaseCleanupPath = join ( testDir , "release-cleanup" ) ;
421+ setArchivedCleanupJobTestHooks ( {
422+ pauseAfterAcquire : {
423+ kind : "cleanup" ,
424+ readyPath : cleanupReadyPath ,
425+ releasePath : releaseCleanupPath ,
426+ } ,
427+ } ) ;
397428 seedArchivedPair ( home ) ;
398429
399430 const server = startServer ( 0 ) ;
431+ let cleanupPromise : Promise < Response > | null = null ;
400432 try {
401433 const preview = await previewDigest ( server . url , 50 ) ;
402- const cleanupPromise = fetch ( new URL ( "/api/storage/cleanup" , server . url ) , {
434+ cleanupPromise = fetch ( new URL ( "/api/storage/cleanup" , server . url ) , {
403435 method : "POST" ,
404436 headers : { "content-type" : "application/json" } ,
405437 body : JSON . stringify ( { percent : 50 , mode : "quarantine" , digest : preview . digest } ) ,
406438 } ) ;
407439
408- await Bun . sleep ( 80 ) ;
440+ await waitForCondition (
441+ "manual cleanup to acquire the storage mutation slot" ,
442+ ( ) => existsSync ( cleanupReadyPath ) ,
443+ ) ;
409444
410445 const restoreAttempt = await fetch ( new URL ( "/api/storage/trash/restore" , server . url ) , {
411446 method : "POST" ,
@@ -418,6 +453,7 @@ describe("storage mutation coordinator", () => {
418453 expect ( existsSync ( join ( home , "archived_sessions" , "rollout-new.jsonl" ) ) ) . toBe ( true ) ;
419454 expect ( trashStageCount ( home ) ) . toBe ( 0 ) ;
420455
456+ writeFileSync ( releaseCleanupPath , "release\n" ) ;
421457 const cleanupRes = await cleanupPromise ;
422458 expect ( cleanupRes . status ) . toBe ( 200 ) ;
423459 const cleanup = await cleanupRes . json ( ) ;
@@ -427,6 +463,8 @@ describe("storage mutation coordinator", () => {
427463 expect ( existsSync ( join ( home , "archived_sessions" , "rollout-new.jsonl" ) ) ) . toBe ( true ) ;
428464 expect ( threadCount ( home ) ) . toBe ( 1 ) ;
429465 } finally {
466+ writeFileSync ( releaseCleanupPath , "release\n" ) ;
467+ if ( cleanupPromise ) await cleanupPromise . catch ( ( ) => undefined ) ;
430468 await stopRaceServer ( server ) ;
431469 }
432470 } , { timeout : 30_000 } ) ;
0 commit comments