YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: improve error handling and crash prevention
Louis Beaumontcae977aedde8
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.toml b/screenpipe-app-tauri/src-tauri/Cargo.tomlindex 0c9e1b618..a1e140c5e 100644--- a/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -1,6 +1,6 @@[package]name = "screenpipe-app"-version = "2.0.116"+version = "2.0.117"description = "24/7 memory for your desktop. 100% local."authors = ["you"]license = ""diff --git a/screenpipe-app-tauri/src-tauri/src/config.rs b/screenpipe-app-tauri/src-tauri/src/config.rsindex 819e5f339..c8f393e41 100644--- a/screenpipe-app-tauri/src-tauri/src/config.rs+++ b/screenpipe-app-tauri/src-tauri/src/config.rs@@ -5,7 +5,9 @@ pub fn get_base_dir(app: &tauri::AppHandle,custom_path: Option<String>,) -> anyhow::Result<PathBuf> {- let default_path = app.path().local_data_dir().unwrap().join("screenpipe");+ let default_path = app.path().local_data_dir()+ .map_err(|e| anyhow::anyhow!("Could not get local data directory: {}", e))?+ .join("screenpipe");let local_data_dir = custom_path.map(PathBuf::from).unwrap_or(default_path);diff --git a/screenpipe-app-tauri/src-tauri/src/main.rs b/screenpipe-app-tauri/src-tauri/src/main.rsindex 42f57301b..1e2492780 100755--- a/screenpipe-app-tauri/src-tauri/src/main.rs+++ b/screenpipe-app-tauri/src-tauri/src/main.rs@@ -757,7 +757,7 @@ async fn main() {let _ = window.app_handle().set_activation_policy(tauri::ActivationPolicy::Regular);- window.hide().unwrap();+ let _ = window.hide();api.prevent_close();}_ => {}@@ -775,13 +775,11 @@ async fn main() {None,)).plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {+ // Gracefully handle case where no windows exist yet (can happen during early init)let windows = app.webview_windows();- windows- .values()- .next()- .expect("Sorry, no window found")- .set_focus()- .expect("Can't focus window!");+ if let Some(window) = windows.values().next() {+ let _ = window.set_focus();+ }})).plugin(tauri_plugin_updater::Builder::new().build()).plugin(tauri_plugin_global_shortcut::Builder::new().build());@@ -891,19 +889,27 @@ async fn main() {}// Logging setup- let base_dir =- get_base_dir(app_handle, None).expect("Failed to ensure local data directory");+ let base_dir = get_base_dir(app_handle, None)+ .unwrap_or_else(|e| {+ eprintln!("Failed to get base dir, using fallback: {}", e);+ dirs::home_dir()+ .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))+ .join(".screenpipe")+ });// Set up rolling file appender+ let log_dir = get_screenpipe_data_dir(app.handle())+ .unwrap_or_else(|_| {+ dirs::home_dir()+ .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))+ .join(".screenpipe")+ });let file_appender = RollingFileAppender::builder().rotation(Rotation::DAILY).filename_prefix("screenpipe-app").filename_suffix("log").max_log_files(5)- .build(- get_screenpipe_data_dir(app.handle())- .unwrap_or_else(|_| dirs::home_dir().unwrap().join(".screenpipe")),- )?;+ .build(log_dir)?;// Create a custom layer for file logginglet file_layer = tracing_subscriber::fmt::layer()@@ -944,15 +950,24 @@ async fn main() {let path = base_dir.join("store.bin");if !path.exists() {- let _ = File::create(path.clone()).unwrap();+ if let Err(e) = File::create(path.clone()) {+ error!("Failed to create store.bin: {}", e);+ }}// Store setup and initialization - must be done first- let store = store::init_store(&app.handle()).unwrap();+ // Use unwrap_or_default to prevent crashes from corrupted stores+ let store = store::init_store(&app.handle()).unwrap_or_else(|e| {+ error!("Failed to init settings store, using defaults: {}", e);+ store::SettingsStore::default()+ });app.manage(store.clone());// Initialize onboarding store- let onboarding_store = store::init_onboarding_store(&app.handle()).unwrap();+ let onboarding_store = store::init_onboarding_store(&app.handle()).unwrap_or_else(|e| {+ error!("Failed to init onboarding store, using defaults: {}", e);+ store::OnboardingStore::default()+ });app.manage(onboarding_store.clone());// Show onboarding window if not completeddiff --git a/screenpipe-app-tauri/src-tauri/src/space_monitor.rs b/screenpipe-app-tauri/src-tauri/src/space_monitor.rsindex 8464f0109..de76431cc 100644--- a/screenpipe-app-tauri/src-tauri/src/space_monitor.rs+++ b/screenpipe-app-tauri/src-tauri/src/space_monitor.rs@@ -5,7 +5,7 @@use crate::commands::hide_main_window;use tauri::AppHandle;-use tracing::debug;+use tracing::{debug, error};/// Sets up a listener for macOS Space changes./// When the active Space changes, hides the main overlay window.@@ -23,31 +23,38 @@ pub fn setup_space_listener(app: AppHandle) {// Clone app handle for use in the blocklet app_for_block = app.clone();- unsafe {- let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];- let notification_center: id = msg_send![workspace, notificationCenter];-- // NSWorkspaceActiveSpaceDidChangeNotification- let notification_name =- NSString::alloc(nil).init_str("NSWorkspaceActiveSpaceDidChangeNotification");-- // Create the block that will be called when space changes- let block = block::ConcreteBlock::new(move |_notification: id| {- debug!("macOS Space changed, hiding overlay");- hide_main_window(&app_for_block);- });- let block = block.copy();-- // Add observer for the notification- let _: id = msg_send![- notification_center,- addObserverForName: notification_name- object: workspace- queue: nil- usingBlock: &*block- ];-- debug!("macOS Space change listener registered successfully");+ // Wrap in catch_unwind to prevent panics from crashing the app+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ unsafe {+ let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];+ let notification_center: id = msg_send![workspace, notificationCenter];++ // NSWorkspaceActiveSpaceDidChangeNotification+ let notification_name =+ NSString::alloc(nil).init_str("NSWorkspaceActiveSpaceDidChangeNotification");++ // Create the block that will be called when space changes+ let block = block::ConcreteBlock::new(move |_notification: id| {+ debug!("macOS Space changed, hiding overlay");+ hide_main_window(&app_for_block);+ });+ let block = block.copy();++ // Add observer for the notification+ let _: id = msg_send![+ notification_center,+ addObserverForName: notification_name+ object: workspace+ queue: nil+ usingBlock: &*block+ ];++ debug!("macOS Space change listener registered successfully");+ }+ }));++ if let Err(e) = result {+ error!("Failed to setup space listener: {:?}", e);}});}