@@ -88,6 +88,8 @@ import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts
8888import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
8989import * as Scheduler from "../scheduling/Scheduler.ts" ;
9090import * as SecretRequests from "../secrets/SecretRequests.ts" ;
91+ import * as DeviceHost from "../device/DeviceHost.ts" ;
92+ import * as DeviceService from "../device/DeviceService.ts" ;
9193import * as PreviewManager from "../preview/Manager.ts" ;
9294import * as TerminalManager from "../terminal/Manager.ts" ;
9395import * as ResourceCleanupService from "./ResourceCleanupService.ts" ;
@@ -368,8 +370,59 @@ const layerScheduledTasksTest = ScheduledTaskService.layer.pipe(
368370 ) ,
369371 ) ,
370372) ;
373+ const bootedSimulatorReady : DeviceHost . DeviceHostAgentReady = {
374+ nodePath : process . execPath ,
375+ hub : { origin : "http://device.test" } ,
376+ agentDevice : null ,
377+ helpers : { serveSimAxSettings : null , serveSimCli : null } ,
378+ run : ( ) => Effect . die ( "Thread cleanup must not run host commands" ) ,
379+ releaseAgentSession : ( ) => Effect . void ,
380+ devices : {
381+ list : Effect . succeed ( {
382+ devices : [
383+ {
384+ id : "phone" ,
385+ name : "iPhone" ,
386+ platform : "ios" ,
387+ version : "26" ,
388+ booted : true ,
389+ physical : false ,
390+ } ,
391+ ] ,
392+ detail : undefined ,
393+ } ) ,
394+ boot : ( ) => Effect . die ( "The simulator is already booted" ) ,
395+ attach : ( ) => Effect . void ,
396+ shutdown : ( ) => Effect . die ( "Thread cleanup must keep the simulator booted" ) ,
397+ screenshot : ( ) => Effect . die ( "unused screenshot" ) ,
398+ } ,
399+ } ;
400+ const bootedSimulatorHost = DeviceHost . DeviceHost . of ( {
401+ id : "local" ,
402+ label : "Local" ,
403+ summary : Effect . succeed ( {
404+ id : "local" ,
405+ label : "Local" ,
406+ kind : "local" ,
407+ hubInstalled : true ,
408+ agentDeviceInstalled : true ,
409+ platforms : [ { platform : "ios" , available : true } ] ,
410+ } ) ,
411+ platformAvailability : ( platform ) => Effect . succeed ( { platform, available : true } ) ,
412+ ensureReady : ( ) => Effect . succeed ( bootedSimulatorReady ) ,
413+ ensureAgentReady : ( ) => Effect . succeed ( bootedSimulatorReady ) ,
414+ current : Effect . succeed ( bootedSimulatorReady ) ,
415+ stopAgent : Effect . void ,
416+ stop : Effect . void ,
417+ } ) ;
371418const layerResourceCleanupTest = ResourceCleanupService . layer . pipe (
372419 Layer . provideMerge ( layerScheduledTasksTest ) ,
420+ Layer . provideMerge (
421+ Layer . effect (
422+ DeviceService . DeviceService ,
423+ DeviceService . makeWithHosts ( new Map ( [ [ bootedSimulatorHost . id , bootedSimulatorHost ] ] ) ) ,
424+ ) ,
425+ ) ,
373426 Layer . provide (
374427 Layer . mergeAll (
375428 Layer . mock ( PreviewManager . PreviewManager ) ( { close : ( ) => Effect . void } ) ,
@@ -545,6 +598,61 @@ it.layer(layerProjectDeletionTest)("project deletion during thread commands", (i
545598 } ) ,
546599 ) ;
547600
601+ it . effect . each ( [ "archive" , "thread delete" , "project delete" ] as const ) (
602+ "%s releases the thread's devices and keeps them booted" ,
603+ ( lifecycle ) =>
604+ Effect . gen ( function * ( ) {
605+ const projects = yield * ProjectService . ProjectService ;
606+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
607+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
608+ const devices = yield * DeviceService . DeviceService ;
609+ yield * devices . configure ( { enabled : true , agentAccessEnabled : true } ) ;
610+ const projectId = ProjectId . make ( `device-cleanup-project:${ lifecycle } ` ) ;
611+ const threadId = ThreadId . make ( `device-cleanup-thread:${ lifecycle } ` ) ;
612+ const otherThreadId = ThreadId . make ( `device-surviving-thread:${ lifecycle } ` ) ;
613+ yield * projects . create ( {
614+ commandId : CommandId . make ( `device-cleanup-create-project:${ lifecycle } ` ) ,
615+ projectId,
616+ title : "Device cleanup" ,
617+ workspaceRoot : `/work/device-cleanup/${ lifecycle } ` ,
618+ } ) ;
619+ for ( const id of [ threadId , otherThreadId ] ) {
620+ yield * orchestrator . dispatch ( {
621+ type : "thread.create" ,
622+ commandId : CommandId . make ( `device-cleanup-create:${ id } ` ) ,
623+ createdBy : "user" ,
624+ creationSource : "web" ,
625+ threadId : id ,
626+ projectId : id === threadId ? projectId : ProjectId . make ( "device-cleanup-other-project" ) ,
627+ title : "Device user" ,
628+ modelSelection,
629+ runtimeMode : "full-access" ,
630+ interactionMode : "default" ,
631+ branch : null ,
632+ worktreePath : null ,
633+ } ) ;
634+ yield * devices . open ( { threadId : id , deviceId : "phone" , platform : "ios" } ) ;
635+ }
636+ const otherSessions = yield * devices . sessionsForThread ( otherThreadId ) ;
637+
638+ const commandId = CommandId . make ( `device-cleanup:${ lifecycle } ` ) ;
639+ if ( lifecycle === "project delete" ) {
640+ yield * projects . delete ( { commandId, projectId, force : true } ) ;
641+ } else {
642+ yield * orchestrator . dispatch ( {
643+ type : lifecycle === "archive" ? "thread.archive" : "thread.delete" ,
644+ commandId,
645+ threadId,
646+ } ) ;
647+ }
648+ yield * worker . drain ( ) ;
649+
650+ assert . deepEqual ( yield * devices . sessionsForThread ( threadId ) , [ ] ) ;
651+ assert . deepEqual ( yield * devices . sessionsForThread ( otherThreadId ) , otherSessions ) ;
652+ assert . isTrue ( ( yield * devices . state ) . devices [ 0 ] ?. booted ) ;
653+ } ) ,
654+ ) ;
655+
548656 it . effect ( "waits for an in-flight thread update before planning deletion" , ( ) =>
549657 Effect . gen ( function * ( ) {
550658 const projects = yield * ProjectService . ProjectService ;
0 commit comments