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(support): preserve desktop panics before app setup (#7205)
Ezra Ellette5a8c666be7f6
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/app_panic.rs b/apps/screenpipe-app-tauri/src-tauri/src/app_panic.rsnew file mode 100644index 000000000..7d885fdf4--- /dev/null+++ b/apps/screenpipe-app-tauri/src-tauri/src/app_panic.rs@@ -0,0 +1,57 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++//! Persist desktop panics even before app setup creates the data directory.++use std::path::Path;+use std::sync::atomic::{AtomicBool, Ordering};++static APP_SETUP_STARTED: AtomicBool = AtomicBool::new(false);++pub fn mark_setup_started() {+ APP_SETUP_STARTED.store(true, Ordering::Relaxed);+}++pub fn setup_started() -> bool {+ APP_SETUP_STARTED.load(Ordering::Relaxed)+}++pub fn write_report(+ dir: &Path,+ thread_name: &str,+ location: &str,+ payload: &str,+ backtrace: &dyn std::fmt::Display,+ setup_started: bool,+) -> String {+ // A worker-thread panic need not terminate the app. Only identify the+ // launch outcome when the main thread panics before setup has begun.+ let launch_outcome = if thread_name == "main" && !setup_started {+ "\nLaunch outcome: main thread panicked before app setup"+ } else {+ ""+ };+ let report = format!(+ "PANIC on thread '{}' at {}: {}{}\n\nBacktrace:\n{}",+ thread_name, location, payload, launch_outcome, backtrace+ );+ let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f");+ // This shared writer creates the directory, appends (including a later+ // panic_cannot_unwind), and fsyncs before an abort can end the process.+ screenpipe_engine::crash_log::write_panic_log(dir, &format!("[{}] {}", timestamp, report));+ report+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn only_a_main_thread_panic_before_setup_is_a_failed_launch() {+ let dir = tempfile::tempdir().unwrap();+ for (thread, setup) in [("capture", false), ("main", true)] {+ let report = write_report(dir.path(), thread, "test.rs:1", "failure", &"trace", setup);+ assert!(!report.contains("Launch outcome:"));+ }+ }+}diff --git a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rsindex 66ca0f5bb..23dc4d343 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs@@ -377,22 +377,34 @@ mod tests {#[tokio::test]async fn rotated_panic_survives_collection_limits_and_redaction_with_timestamp() {- let dir = tempdir().unwrap();+ let root = tempdir().unwrap();+ let dir = root.path().join("first-launch");+ assert!(!dir.exists());+ // Exercise the desktop hook's writer before normal app setup has+ // created its data directory, then the existing append/rotation path.+ crate::app_panic::write_report(+ &dir,+ "main",+ r"C:\build\tao-0.35.3\src\platform_impl\windows\event_loop.rs:709:5",+ "assertion failed: subclass_result.as_bool()",+ &"0: tauri::app::Builder::build",+ false,+ );screenpipe_engine::crash_log::write_panic_log(- dir.path(),+ dir.as_path(),"[2026-09-18 19:19:44.000] PANIC on thread 'capture': encoder failed; recording stopped\nBacktrace:\n0: capture_frame\npassword=hunter2",);std::fs::OpenOptions::new().write(true)- .open(dir.path().join("last-panic.log"))+ .open(dir.as_path().join("last-panic.log")).unwrap().set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1789759184)).unwrap();- screenpipe_engine::crash_log::rotate_panic_log(dir.path());- assert!(!dir.path().join("last-panic.log").exists());+ screenpipe_engine::crash_log::rotate_panic_log(dir.as_path());+ assert!(!dir.as_path().join("last-panic.log").exists());for day in 1..=MAX_FILES + 1 {tokio::fs::write(- dir.path()+ dir.as_path().join(format!("screenpipe-app.2026-09-{day:02}.log")),"recording resumed\n",)@@ -400,10 +412,14 @@ mod tests {.unwrap();}- let files = crate::log_files::collect_log_files(&[dir.path().to_path_buf()]).await;+ let files = crate::log_files::collect_log_files(&[dir.as_path().to_path_buf()]).await;let report = redact_files(&owned_log_files(files)).await.unwrap();assert!(report.contains("=== last-panic.log.prev ==="));+ assert!(report.contains("assertion failed: subclass_result.as_bool()"));+ assert!(report.contains("event_loop.rs:709:5"));+ assert!(report.contains("Launch outcome: main thread panicked before app setup"));+ assert!(report.contains("0: tauri::app::Builder::build"));assert!(report.contains("File modified at: 2026-09-18T19:19:44Z"));assert!(report.contains("[2026-09-18 19:19:44.000]"));assert!(report.contains("encoder failed; recording stopped"));diff --git a/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs b/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rsindex 0e183a1de..ca2fb8f2c 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs@@ -1476,29 +1476,41 @@ mod tests {let server = MockServer::start().await;let mut input = request();input.video_ext = Some("mp4".to_string());- let dir = tempfile::tempdir().unwrap();+ let root = tempfile::tempdir().unwrap();+ let dir = root.path().join("first-launch");+ assert!(!dir.exists());+ // Exercise the desktop hook's writer before normal app setup has+ // created its data directory, then the existing append/rotation path.+ crate::app_panic::write_report(+ &dir,+ "main",+ r"C:\build\tao-0.35.3\src\platform_impl\windows\event_loop.rs:709:5",+ "assertion failed: subclass_result.as_bool()",+ &"0: tauri::app::Builder::build",+ false,+ );screenpipe_engine::crash_log::write_panic_log(- dir.path(),+ dir.as_path(),"[2026-09-18 19:19:44.000] PANIC on thread 'capture': encoder failed; recording stopped\nBacktrace:\n0: capture_frame\npassword=hunter2",);std::fs::OpenOptions::new().write(true)- .open(dir.path().join("last-panic.log"))+ .open(dir.as_path().join("last-panic.log")).unwrap().set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1789759184)).unwrap();- screenpipe_engine::crash_log::rotate_panic_log(dir.path());- assert!(!dir.path().join("last-panic.log").exists());+ screenpipe_engine::crash_log::rotate_panic_log(dir.as_path());+ assert!(!dir.as_path().join("last-panic.log").exists());for day in 1..=MAX_LOG_FILES + 1 {tokio::fs::write(- dir.path()+ dir.as_path().join(format!("screenpipe-app.2026-09-{day:02}.log")),"recording resumed\n",).await.unwrap();}- let files = crate::log_files::collect_log_files(&[dir.path().to_path_buf()]).await;+ let files = crate::log_files::collect_log_files(&[dir.as_path().to_path_buf()]).await;let logs = collect_log_text_from_files(files).await;// Exercise the deterministic redaction shared by manual feedback and// unattended logs, without contacting the optional enrichment service.@@ -1596,6 +1608,10 @@ mod tests {.unwrap();let report = std::str::from_utf8(&uploaded.body).unwrap();assert!(report.contains("=== last-panic.log.prev ==="));+ assert!(report.contains("assertion failed: subclass_result.as_bool()"));+ assert!(report.contains("event_loop.rs:709:5"));+ assert!(report.contains("Launch outcome: main thread panicked before app setup"));+ assert!(report.contains("0: tauri::app::Builder::build"));assert!(report.contains("File modified at: 2026-09-18T19:19:44Z"));assert!(report.contains("[2026-09-18 19:19:44.000]"));assert!(report.contains("encoder failed; recording stopped"));diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rsindex 084196532..d73bac0cf 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs@@ -40,6 +40,7 @@ use updates::start_update_check;use window::ShowRewindWindow;mod activity_history;+mod app_panic;mod first_run_summary;mod analytics;mod auth_session;@@ -761,14 +762,9 @@ async fn main() {// Rotate the crash log on startup (don't truncate). Relaunch after a crash// is the common case — truncating loses the message we most need to diagnose.// Previous panic moves to last-panic.log.prev; new file starts empty.- {- let log_dir = screenpipe_core::paths::default_screenpipe_data_dir();- let cur = log_dir.join("last-panic.log");- let prev = log_dir.join("last-panic.log.prev");- if cur.exists() {- let _ = std::fs::rename(&cur, &prev);- }- }+ screenpipe_engine::crash_log::rotate_panic_log(+ &screenpipe_core::paths::default_screenpipe_data_dir(),+ );let default_hook = std::panic::take_hook();std::panic::set_hook(Box::new(move |info| {// Log the actual panic first — before any processing. Once unwinding hits@@ -800,32 +796,18 @@ async fn main() {// Force-capture a backtrace before abort() kills uslet backtrace = std::backtrace::Backtrace::force_capture();- let crash_msg = format!(- "PANIC on thread '{}' at {}: {}\n\nBacktrace:\n{}",- thread_name, location, payload, backtrace+ let crash_msg = app_panic::write_report(+ &screenpipe_core::paths::default_screenpipe_data_dir(),+ &thread_name,+ &location,+ &payload,+ &backtrace,+ app_panic::setup_started(),);// Log to stderr (survives even if tracing isn't initialized yet)eprintln!("{}", crash_msg);- // Write to a crash log file — this survives abort() since we fsync- // Critical for diagnosing panics inside tao's extern "C" callbacks- // (send_event, did_finish_launching) where panic_cannot_unwind → abort()- let log_dir = screenpipe_core::paths::default_screenpipe_data_dir();- let crash_path = log_dir.join("last-panic.log");- // Append instead of truncate — when panic_cannot_unwind fires after- // the original panic, both messages are preserved in the file.- if let Ok(mut f) = std::fs::OpenOptions::new()- .create(true)- .append(true)- .open(&crash_path)- {- use std::io::Write;- let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f");- let _ = writeln!(f, "[{}] {}", timestamp, crash_msg);- let _ = f.sync_all(); // fsync before abort() kills us- }-// Also report to Sentry if initializedsentry::capture_message(&format!(@@ -1140,6 +1122,7 @@ async fn main() {.manage(sync_scheduler).invoke_handler(tauri_helper::tauri_collect_commands!()).setup(move |app| {+ app_panic::mark_setup_started();// Capture before setup does any other work: this callback runs// synchronously inside applicationDidFinishLaunching, while the// kAEOpenApplication event (including Apple's `lgit` login-item