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: throttle input device recovery logging + safe Instant arithmetic
Louis Beaumont1fcf921cf1dd
diff --git a/crates/screenpipe-audio/src/audio_manager/device_monitor.rs b/crates/screenpipe-audio/src/audio_manager/device_monitor.rsindex 2fbf15603..0cded36aa 100644--- a/crates/screenpipe-audio/src/audio_manager/device_monitor.rs+++ b/crates/screenpipe-audio/src/audio_manager/device_monitor.rs@@ -170,13 +170,16 @@ pub async fn start_device_monitor(let mut failed_devices: HashMap<String, (u32, Instant)> = HashMap::new();let mut output_recovery_backoff = OutputRecoveryBackoff::new();let mut no_input_retry_count: u32 = 0;+ let mut last_no_input_log: Option<Instant> = None;// Central handler restart cooldown: max 3 restarts in a 5-minute windowlet mut central_restart_times: Vec<Instant> = Vec::new();let central_restart_exhausted = std::sync::atomic::AtomicBool::new(false);let mut model_restart_pending = false;let model_refresh_cooldown = Duration::from_secs(30);- let mut last_model_refresh = Instant::now() - model_refresh_cooldown;+ let mut last_model_refresh = Instant::now()+ .checked_sub(model_refresh_cooldown)+ .unwrap_or(Instant::now());// Initialize tracker with current defaultslet _ = default_tracker.check_input_changed();@@ -509,13 +512,29 @@ pub async fn start_device_monitor(if !has_input {no_input_retry_count += 1;++ // Throttle logging after many retries to avoid spamming logs+ // (e.g. Bluetooth device disconnected permanently)+ let should_log = if no_input_retry_count <= 10 {+ true // always log first 10 attempts+ } else {+ // After 10 attempts, log once per 60s+ match last_no_input_log {+ Some(t) => t.elapsed().as_secs() >= 60,+ None => true,+ }+ };+match default_input_device() {Ok(default_input) => {let device_name = default_input.to_string();- warn!(- "[DEVICE_RECOVERY] no input device running (attempt {}), starting default: {}",- no_input_retry_count, device_name- );+ if should_log {+ warn!(+ "[DEVICE_RECOVERY] no input device running (attempt {}), starting default: {}",+ no_input_retry_count, device_name+ );+ last_no_input_log = Some(Instant::now());+ }match audio_manager.start_device(&default_input).await {Ok(()) => {failed_devices.remove(&device_name);@@ -526,18 +545,24 @@ pub async fn start_device_monitor();}Err(e) => {- warn!(- "[DEVICE_RECOVERY] failed to start input device {} (attempt {}): {}",- device_name, no_input_retry_count, e- );+ if should_log {+ warn!(+ "[DEVICE_RECOVERY] failed to start input device {} (attempt {}): {}",+ device_name, no_input_retry_count, e+ );+ last_no_input_log = Some(Instant::now());+ }}}}Err(e) => {- warn!(- "[DEVICE_RECOVERY] no input device running and default_input_device() failed (attempt {}): {}",- no_input_retry_count, e- );+ if should_log {+ warn!(+ "[DEVICE_RECOVERY] no input device running and default_input_device() failed (attempt {}): {}",+ no_input_retry_count, e+ );+ last_no_input_log = Some(Instant::now());+ }}}} else {diff --git a/crates/screenpipe-audio/src/idle_detector.rs b/crates/screenpipe-audio/src/idle_detector.rsindex 1954d720c..18cacd7c4 100644--- a/crates/screenpipe-audio/src/idle_detector.rs+++ b/crates/screenpipe-audio/src/idle_detector.rs@@ -138,7 +138,7 @@ mod tests {// Simulate being idle: set threshold very high and backdate last_above_thresholdlet detector = IdleDetector::new(100.0);*detector.last_above_threshold.write().unwrap() =- Instant::now() - std::time::Duration::from_secs(60);+ Instant::now().checked_sub(std::time::Duration::from_secs(60)).unwrap_or(Instant::now());assert!(detector.is_idle(),"should be idle after stable period exceeds threshold"diff --git a/crates/screenpipe-engine/src/event_driven_capture.rs b/crates/screenpipe-engine/src/event_driven_capture.rsindex 61c967881..41a1b856c 100644--- a/crates/screenpipe-engine/src/event_driven_capture.rs+++ b/crates/screenpipe-engine/src/event_driven_capture.rs@@ -268,7 +268,7 @@ pub async fn event_driven_capture_loop({// Small delay to let the monitor settle after startuptokio::time::sleep(Duration::from_millis(500)).await;- state.last_capture = Instant::now() - Duration::from_millis(500); // allow capture+ state.last_capture = Instant::now().checked_sub(Duration::from_millis(500)).unwrap_or(Instant::now()); // allow capturematch do_capture(&db,&monitor,@@ -979,7 +979,7 @@ mod tests {assert!(!state.needs_idle_capture());// Simulate waiting- state.last_capture = Instant::now() - Duration::from_millis(150);+ state.last_capture = Instant::now().checked_sub(Duration::from_millis(150)).unwrap_or(Instant::now());assert!(state.needs_idle_capture());}@@ -991,7 +991,7 @@ mod tests {};let mut state = EventDrivenCapture::new(config);- state.last_capture = Instant::now() - Duration::from_millis(150);+ state.last_capture = Instant::now().checked_sub(Duration::from_millis(150)).unwrap_or(Instant::now());assert!(state.needs_idle_capture());state.mark_captured();diff --git a/crates/screenpipe-engine/src/meeting_detector.rs b/crates/screenpipe-engine/src/meeting_detector.rsindex 17abfb680..ccc9a7b98 100644--- a/crates/screenpipe-engine/src/meeting_detector.rs+++ b/crates/screenpipe-engine/src/meeting_detector.rs@@ -2628,7 +2628,7 @@ mod tests {#[test]fn test_confirming_to_idle_timeout() {let state = MeetingState::Confirming {- since: Instant::now() - CONFIRM_TIMEOUT - Duration::from_secs(1),+ since: Instant::now().checked_sub(CONFIRM_TIMEOUT + Duration::from_secs(1)).unwrap_or(Instant::now()),app: "Zoom".to_string(),profile_index: 0,};@@ -2742,7 +2742,7 @@ mod tests {meeting_id: 42,app: "Zoom".to_string(),started_at: Utc::now(),- since: Instant::now() - ENDING_TIMEOUT - Duration::from_secs(1),+ since: Instant::now().checked_sub(ENDING_TIMEOUT + Duration::from_secs(1)).unwrap_or(Instant::now()),};let results: Vec<ScanResult> = vec![];let (new_state, action) = advance_state(state, &results);@@ -2795,7 +2795,7 @@ mod tests {meeting_id: 42,app: "Zoom".to_string(),started_at: Utc::now(),- since: Instant::now() - ENDING_TIMEOUT - Duration::from_secs(1),+ since: Instant::now().checked_sub(ENDING_TIMEOUT + Duration::from_secs(1)).unwrap_or(Instant::now()),};let (state, action) = advance_state(state, &[]);assert!(matches!(state, MeetingState::Idle));@@ -2870,7 +2870,7 @@ mod tests {meeting_id: 42,app: "Zoom".to_string(),started_at: Utc::now(),- since: Instant::now() - ENDING_TIMEOUT - Duration::from_secs(1),+ since: Instant::now().checked_sub(ENDING_TIMEOUT + Duration::from_secs(1)).unwrap_or(Instant::now()),};let (new_state, ended_id) = handle_no_apps_running(state);assert!(matches!(new_state, MeetingState::Idle));@@ -2897,7 +2897,7 @@ mod tests {meeting_id: -1,app: "Zoom".to_string(),started_at: Utc::now(),- since: Instant::now() - ENDING_TIMEOUT - Duration::from_secs(1),+ since: Instant::now().checked_sub(ENDING_TIMEOUT + Duration::from_secs(1)).unwrap_or(Instant::now()),};let (_, ended_id) = handle_no_apps_running(state);assert!(ended_id.is_none(), "should not end meeting with id=-1");