Fix persistence logic for persistent vs support sessions

- Persistent agents (no code) now add to startup on launch
- All agents add to startup, cleanup removes it on explicit exit
- Persistent agents reconnect automatically on disconnect
- Support sessions still exit without reconnecting

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-28 17:02:21 -07:00
parent 4b29dbe6c8
commit 52c47b2de1

View File

@@ -165,13 +165,12 @@ async fn main() -> Result<()> {
} }
/// Clean up before exiting (remove from startup, etc.) /// Clean up before exiting (remove from startup, etc.)
fn cleanup_on_exit(is_support_session: bool) { /// Called when user explicitly ends session or support session completes
if is_support_session { fn cleanup_on_exit() {
info!("Cleaning up before exit"); info!("Cleaning up before exit");
if let Err(e) = startup::remove_from_startup() { if let Err(e) = startup::remove_from_startup() {
warn!("Failed to remove from startup: {}", e); warn!("Failed to remove from startup: {}", e);
} }
}
} }
async fn run_agent(config: config::Config) -> Result<()> { async fn run_agent(config: config::Config) -> Result<()> {
@@ -181,11 +180,11 @@ async fn run_agent(config: config::Config) -> Result<()> {
let hostname = config.hostname(); let hostname = config.hostname();
// Add to startup so we reconnect after reboot // Add to startup so we reconnect after reboot
if is_support_session { // Persistent agents (no support code) should ALWAYS be in startup
// Support sessions only need startup temporarily while active
if let Err(e) = startup::add_to_startup() { if let Err(e) = startup::add_to_startup() {
warn!("Failed to add to startup: {}. Agent won't persist through reboot.", e); warn!("Failed to add to startup: {}. Agent won't persist through reboot.", e);
} }
}
// Create tray icon // Create tray icon
let tray = match tray::TrayController::new(&hostname, config.support_code.as_deref()) { let tray = match tray::TrayController::new(&hostname, config.support_code.as_deref()) {
@@ -233,14 +232,14 @@ async fn run_agent(config: config::Config) -> Result<()> {
// Check if this is a user-initiated exit // Check if this is a user-initiated exit
if error_msg.contains("USER_EXIT") { if error_msg.contains("USER_EXIT") {
info!("Session ended by user"); info!("Session ended by user");
cleanup_on_exit(is_support_session); cleanup_on_exit();
return Ok(()); return Ok(());
} }
// Check if this is a cancellation // Check if this is a cancellation
if error_msg.contains("SESSION_CANCELLED") { if error_msg.contains("SESSION_CANCELLED") {
info!("Session was cancelled by technician"); info!("Session was cancelled by technician");
cleanup_on_exit(is_support_session); cleanup_on_exit();
show_message_box( show_message_box(
"Support Session Ended", "Support Session Ended",
"The support session was cancelled by the technician.\n\nThis window will close automatically.", "The support session was cancelled by the technician.\n\nThis window will close automatically.",
@@ -271,7 +270,7 @@ async fn run_agent(config: config::Config) -> Result<()> {
// Check if connection was rejected due to cancelled code // Check if connection was rejected due to cancelled code
if error_msg.contains("cancelled") { if error_msg.contains("cancelled") {
info!("Support code was cancelled before connection"); info!("Support code was cancelled before connection");
cleanup_on_exit(is_support_session); cleanup_on_exit();
show_message_box( show_message_box(
"Support Session Cancelled", "Support Session Cancelled",
"This support session has been cancelled.\n\nPlease contact your technician for a new support code.", "This support session has been cancelled.\n\nPlease contact your technician for a new support code.",
@@ -287,7 +286,7 @@ async fn run_agent(config: config::Config) -> Result<()> {
// For support sessions, don't reconnect if something goes wrong // For support sessions, don't reconnect if something goes wrong
if is_support_session { if is_support_session {
info!("Support session ended, not reconnecting"); info!("Support session ended, not reconnecting");
cleanup_on_exit(is_support_session); cleanup_on_exit();
return Ok(()); return Ok(());
} }
@@ -295,7 +294,7 @@ async fn run_agent(config: config::Config) -> Result<()> {
if let Some(ref t) = tray { if let Some(ref t) = tray {
if t.exit_requested() { if t.exit_requested() {
info!("Exit requested by user"); info!("Exit requested by user");
cleanup_on_exit(is_support_session); cleanup_on_exit();
return Ok(()); return Ok(());
} }
} }