@@ -90,6 +90,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
9090 const timeouts = new Map < string , unknown > ( ) ;
9191 const control : {
9292 failCommand : string | undefined ;
93+ failOnce ?: boolean ;
9394 stateAfterStop ?: string ;
9495 linger : string ;
9596 enabled : boolean ;
@@ -115,6 +116,10 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
115116 invocations . push ( input ) ;
116117 timeouts . set ( command , input . timeout ) ;
117118 const failed = command === control . failCommand ;
119+ if ( failed && control . failOnce ) {
120+ control . failCommand = undefined ;
121+ control . failOnce = false ;
122+ }
118123 if ( ! failed && command === "loginctl enable-linger --no-ask-password 501" )
119124 control . linger = "yes" ;
120125 if ( ! failed && command === "systemctl --user enable t3code.service" ) control . enabled = true ;
@@ -890,6 +895,36 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
890895 } ) ,
891896 ) ;
892897
898+ it . effect (
899+ "restores launchd after a failed drain probe only when a final probe confirms removal" ,
900+ ( ) =>
901+ Effect . gen ( function * ( ) {
902+ const { service, commands, control } = yield * makeHarness ( "darwin" ) ;
903+ const plan = yield * service . install ( ) ;
904+ const polls = yield * Queue . unbounded < void > ( ) ;
905+ control . drainPolls = 1 ;
906+ control . onDrainPoll = Effect . sync ( ( ) => {
907+ control . failCommand = "launchctl print gui/501/com.t3tools.t3code.service" ;
908+ control . failOnce = true ;
909+ } ) . pipe ( Effect . andThen ( Queue . offer ( polls , undefined ) ) , Effect . asVoid ) ;
910+ commands . length = 0 ;
911+
912+ const maintenance = yield * service
913+ . withStopped ( Effect . sync ( ( ) => commands . push ( "repair" ) ) )
914+ . pipe ( Effect . flip , Effect . forkChild ) ;
915+ yield * Queue . take ( polls ) ;
916+ yield * TestClock . adjust ( "1 second" ) ;
917+
918+ expect ( yield * Fiber . join ( maintenance ) ) . toMatchObject ( {
919+ _tag : "BootServiceCommandError" ,
920+ step : "waiting for the launch agent to stop" ,
921+ } ) ;
922+ expect ( commands ) . not . toContain ( "repair" ) ;
923+ expect ( commands . at ( - 2 ) ) . toBe ( "launchctl print gui/501/com.t3tools.t3code.service" ) ;
924+ expect ( commands . at ( - 1 ) ) . toBe ( `launchctl bootstrap gui/501 ${ plan . unitPath } ` ) ;
925+ } ) ,
926+ ) ;
927+
893928 // regression: #11995 macOS 15 rejects `bootout --wait`, so the stop waits on `print`
894929 it . effect ( "starts a booted-out launch agent again only once launchd has removed it" , ( ) =>
895930 Effect . gen ( function * ( ) {
0 commit comments