@@ -141,6 +141,22 @@ const handleFatalStartupError = Effect.fn("desktop.startup.handleFatalStartupErr
141141const fatalStartupCause = < E > ( stage : string , cause : Cause . Cause < E > ) =>
142142 handleFatalStartupError ( stage , Cause . pretty ( cause ) ) . pipe ( Effect . andThen ( Effect . failCause ( cause ) ) ) ;
143143
144+ export const restartBackendAfterCuaDriverExit = Effect . fn (
145+ "desktop.restartBackendAfterCuaDriverExit" ,
146+ ) ( function * (
147+ backend : Pick < DesktopBackendPool . DesktopBackendInstance , "snapshot" | "stop" | "start" > ,
148+ isQuitting : Effect . Effect < boolean > ,
149+ ) {
150+ const snapshot = yield * backend . snapshot ;
151+ if ( ! snapshot . desiredRunning || ( yield * isQuitting ) ) return false ;
152+
153+ yield * backend . stop ( ) ;
154+ if ( yield * isQuitting ) return false ;
155+
156+ yield * backend . start ;
157+ return true ;
158+ } ) ;
159+
144160const bootstrap = Effect . gen ( function * ( ) {
145161 const pool = yield * DesktopBackendPool . DesktopBackendPool ;
146162 const primaryBackend = yield * pool . primary ;
@@ -308,20 +324,44 @@ const startup = Effect.gen(function* () {
308324 } )
309325 . pipe ( Effect . ignore ) ;
310326 } ) ;
311- yield * cuaDriver . start . pipe (
327+ const cuaStarted = yield * cuaDriver . start . pipe (
328+ Effect . as ( true ) ,
312329 Effect . catchTags ( {
313330 CuaDriverModuleLoadError : ( error ) =>
314- continueWithoutCua ( { modulePath : error . modulePath , cause : error . cause } ) ,
315- CuaDriverNotConfiguredError : ( ) => continueWithoutCua ( { } ) ,
331+ continueWithoutCua ( { modulePath : error . modulePath , cause : error . cause } ) . pipe (
332+ Effect . as ( false ) ,
333+ ) ,
334+ CuaDriverNotConfiguredError : ( ) => continueWithoutCua ( { } ) . pipe ( Effect . as ( false ) ) ,
316335 CuaDriverPermissionError : ( error ) =>
317336 continueWithoutCua ( {
318337 accessibility : error . accessibility ,
319338 screenRecording : error . screenRecording ,
320- } ) ,
339+ } ) . pipe ( Effect . as ( false ) ) ,
321340 CuaDriverStartError : ( error ) =>
322- continueWithoutCua ( { binaryPath : error . binaryPath , cause : error . cause } ) ,
341+ continueWithoutCua ( { binaryPath : error . binaryPath , cause : error . cause } ) . pipe (
342+ Effect . as ( false ) ,
343+ ) ,
323344 } ) ,
324345 ) ;
346+ if ( cuaStarted ) {
347+ const pool = yield * DesktopBackendPool . DesktopBackendPool ;
348+ const primaryBackend = yield * pool . primary ;
349+ const state = yield * DesktopState . DesktopState ;
350+ yield * cuaDriver . awaitUnavailable . pipe (
351+ Effect . andThen ( restartBackendAfterCuaDriverExit ( primaryBackend , Ref . get ( state . quitting ) ) ) ,
352+ Effect . tap ( ( restarted ) =>
353+ restarted
354+ ? logStartupInfo ( "desktop backend restarted without unavailable cua-driver" )
355+ : Effect . void ,
356+ ) ,
357+ Effect . catchCause ( ( cause ) =>
358+ logStartupError ( "failed to remove unavailable cua-driver from desktop backend" , {
359+ cause,
360+ } ) ,
361+ ) ,
362+ Effect . forkScoped ,
363+ ) ;
364+ }
325365 }
326366 yield * appIdentity . configure ;
327367 yield * applicationMenu . configure ;
0 commit comments