@@ -5,7 +5,27 @@ use crate::managed_agents::{
55 self , kill_stale_tracked_processes, load_managed_agents, save_managed_agents,
66 sync_managed_agent_processes, BackendKind , ManagedAgentProcess ,
77} ;
8- use crate :: util;
8+ use crate :: { prevent_sleep, util} ;
9+
10+ pub ( crate ) fn is_restart_request ( code : Option < i32 > ) -> bool {
11+ code == Some ( tauri:: RESTART_EXIT_CODE )
12+ }
13+
14+ pub ( crate ) fn shut_down_app ( app : & tauri:: AppHandle , shutdown_done : & std:: sync:: atomic:: AtomicBool ) {
15+ use std:: sync:: atomic:: Ordering ;
16+
17+ app. state :: < AppState > ( )
18+ . shutdown_started
19+ . store ( true , Ordering :: SeqCst ) ;
20+ if !shutdown_done. swap ( true , Ordering :: SeqCst ) {
21+ prevent_sleep:: release ( & app. state :: < AppState > ( ) . prevent_sleep ) ;
22+ if let Err ( error) = shutdown_managed_agents ( app) {
23+ eprintln ! ( "buzz-desktop: failed to stop managed agents: {error}" ) ;
24+ }
25+ #[ cfg( feature = "mesh-llm" ) ]
26+ shutdown_mesh_runtime ( app) ;
27+ }
28+ }
929
1030/// Install SIGINT/SIGTERM/SIGHUP cleanup on ctrlc's dedicated handler thread.
1131#[ cfg( unix) ]
@@ -33,6 +53,43 @@ pub(crate) fn install_signal_handler(
3353 }
3454}
3555
56+ #[ cfg( all( feature = "mesh-llm" , target_os = "macos" ) ) ]
57+ fn updated_macos_binary ( current_binary : & std:: path:: Path ) -> Option < std:: path:: PathBuf > {
58+ let macos_directory = current_binary. parent ( ) ?;
59+ if macos_directory. file_name ( ) ? != "MacOS" {
60+ return None ;
61+ }
62+ let contents_directory = macos_directory. parent ( ) ?;
63+ if contents_directory. file_name ( ) ? != "Contents" {
64+ return None ;
65+ }
66+ let info_plist =
67+ plist:: from_file :: < _ , plist:: Dictionary > ( contents_directory. join ( "Info.plist" ) ) . ok ( ) ?;
68+ let binary_name = info_plist. get ( "CFBundleExecutable" ) ?. as_string ( ) ?;
69+ Some ( macos_directory. join ( binary_name) )
70+ }
71+
72+ #[ cfg( all( feature = "mesh-llm" , target_os = "macos" ) ) ]
73+ pub ( crate ) fn relaunch_after_mesh_shutdown ( app : & tauri:: AppHandle ) -> ! {
74+ use std:: process:: Command ;
75+
76+ tauri_plugin_single_instance:: destroy ( app) ;
77+ let env = app. env ( ) ;
78+ match tauri:: process:: current_binary ( & env) {
79+ Ok ( current_binary) => {
80+ let binary = updated_macos_binary ( & current_binary) . unwrap_or ( current_binary) ;
81+ if let Err ( error) = Command :: new ( binary)
82+ . args ( env. args_os . iter ( ) . skip ( 1 ) )
83+ . spawn ( )
84+ {
85+ eprintln ! ( "buzz-desktop: failed to relaunch app: {error}" ) ;
86+ }
87+ }
88+ Err ( error) => eprintln ! ( "buzz-desktop: failed to locate app for relaunch: {error}" ) ,
89+ }
90+ hard_exit_after_mesh_shutdown ( ) ;
91+ }
92+
3693#[ cfg( all( feature = "mesh-llm" , target_os = "macos" ) ) ]
3794pub ( crate ) fn hard_exit_after_mesh_shutdown ( ) -> ! {
3895 // SAFETY: all Buzz-managed subprocesses and the embedded Mesh runtime have
@@ -201,3 +258,15 @@ pub(crate) fn shutdown_managed_agents(app: &tauri::AppHandle) -> Result<(), Stri
201258
202259 Ok ( ( ) )
203260}
261+
262+ #[ cfg( test) ]
263+ mod tests {
264+ use super :: is_restart_request;
265+
266+ #[ test]
267+ fn only_tauri_restart_exit_code_requests_a_relaunch ( ) {
268+ assert ! ( is_restart_request( Some ( tauri:: RESTART_EXIT_CODE ) ) ) ;
269+ assert ! ( !is_restart_request( None ) ) ;
270+ assert ! ( !is_restart_request( Some ( 0 ) ) ) ;
271+ }
272+ }
0 commit comments