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(app): stop repeated mic focus capture restarts (#5099)
Louis Beaumontaf1b19e36bd9
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rsindex 59b3d4003..e31110483 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs@@ -146,6 +146,33 @@ mod specta_bindings;mod vault;mod viewer;+#[cfg(target_os = "macos")]+/// Tracks the observed permission transition so repeated focus events cannot+/// restart capture while the audio status cache is still empty.+struct MicFocusRecoveryTracker {+ permission_was_granted: AtomicBool,+}++#[cfg(target_os = "macos")]+impl MicFocusRecoveryTracker {+ const fn new() -> Self {+ Self {+ permission_was_granted: AtomicBool::new(false),+ }+ }++ fn should_restart_capture(&self, permission_granted: bool, audio_devices_empty: bool) -> bool {+ let permission_was_granted = self+ .permission_was_granted+ .swap(permission_granted, Ordering::SeqCst);++ permission_granted && !permission_was_granted && audio_devices_empty+ }+}++#[cfg(target_os = "macos")]+static MIC_FOCUS_RECOVERY: MicFocusRecoveryTracker = MicFocusRecoveryTracker::new();+use health::start_health_check;use log_files::{get_log_files, get_screenpipe_data_dir};use shortcuts::{@@ -663,14 +690,16 @@ async fn main() {tauri::WindowEvent::Focused(true) => {let app = window.app_handle().clone();tauri::async_runtime::spawn(async move {- if !permissions::check_microphone_permission().permitted() {- return;- }- if !health::get_audio_device_status().is_empty() {+ let permission_granted =+ permissions::check_microphone_permission().permitted();+ let audio_devices_empty = health::get_audio_device_status().is_empty();+ if !MIC_FOCUS_RECOVERY+ .should_restart_capture(permission_granted, audio_devices_empty)+ {return;}info!(- "Microphone permission newly granted (focus return) — restarting capture for audio reinit"+ "Microphone permission became available with no audio devices (focus return) — restarting capture once for audio reinit");permissions::restart_capture_on_mic_grant(app).await;});@@ -2018,6 +2047,45 @@ async fn main() {});}+#[cfg(all(test, target_os = "macos"))]+mod mic_focus_recovery_tests {+ use super::MicFocusRecoveryTracker;++ #[test]+ fn repeated_focus_with_empty_audio_status_restarts_only_once() {+ let tracker = MicFocusRecoveryTracker::new();++ assert!(tracker.should_restart_capture(true, true));+ assert!(!tracker.should_restart_capture(true, true));+ assert!(!tracker.should_restart_capture(true, true));+ }++ #[test]+ fn temporary_empty_audio_status_does_not_look_like_a_new_permission_grant() {+ let tracker = MicFocusRecoveryTracker::new();++ assert!(!tracker.should_restart_capture(true, false));+ assert!(!tracker.should_restart_capture(true, true));+ }++ #[test]+ fn permission_revoke_rearms_focus_recovery() {+ let tracker = MicFocusRecoveryTracker::new();++ assert!(tracker.should_restart_capture(true, true));+ assert!(!tracker.should_restart_capture(false, true));+ assert!(tracker.should_restart_capture(true, true));+ }++ #[test]+ fn missing_permission_never_restarts_capture() {+ let tracker = MicFocusRecoveryTracker::new();++ assert!(!tracker.should_restart_capture(false, true));+ assert!(!tracker.should_restart_capture(false, false));+ }+}+#[cfg(test)]pub fn specta_builder() -> tauri_specta::Builder<tauri::Wry> {define_specta_builder!()