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
feat(health): expose ui_recorder status so /health answers 'is clipboard capture on?' (#3477)
Louis Beaumont75db0b1a7ac2
diff --git a/crates/screenpipe-engine/src/routes/health.rs b/crates/screenpipe-engine/src/routes/health.rsindex 0280ba3b3..e264939c5 100644--- a/crates/screenpipe-engine/src/routes/health.rs+++ b/crates/screenpipe-engine/src/routes/health.rs@@ -17,7 +17,9 @@ use tokio::sync::RwLock;use tracing::{debug, warn};use crate::server::AppState;-use crate::ui_recorder::{tree_walker_snapshot, TreeWalkerSnapshot};+use crate::ui_recorder::{+ tree_walker_snapshot, ui_recorder_status_snapshot, TreeWalkerSnapshot, UiRecorderStatus,+};/// Cached health response to avoid recomputing on every poll./// Multiple WebSocket clients + HTTP polls can call /health dozens of@@ -85,6 +87,10 @@ pub struct HealthCheckResponse {pub audio_pipeline: Option<AudioPipelineHealthInfo>,#[serde(skip_serializing_if = "Option::is_none")]pub accessibility: Option<TreeWalkerSnapshot>,+ /// UI/input/clipboard recorder status. Surfaces "configured but not running"+ /// distinctly from "off" so users can tell why ui_events stopped writing.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub ui_recorder: Option<UiRecorderStatus>,#[serde(skip_serializing_if = "Option::is_none")]pub pool_stats: Option<PoolHealthInfo>,/// True when vision capture loop is alive but DB writes have stopped (pool exhaustion).@@ -727,6 +733,17 @@ async fn health_check_inner(state: &Arc<AppState>) -> HealthCheckResponse {None}},+ ui_recorder: {+ let snap = ui_recorder_status_snapshot();+ // Only attach when start_ui_recording has touched the atomics —+ // otherwise the field is meaningless noise for users who never+ // enabled UI capture.+ if snap.configured || snap.events_inserted > 0 {+ Some(snap)+ } else {+ None+ }+ },audio_pipeline: if !state.audio_disabled {let is_paused = state.audio_manager@@ -945,6 +962,7 @@ mod tests {pipeline: None,audio_pipeline: None,accessibility: None,+ ui_recorder: None,pool_stats: None,vision_db_write_stalled: false,audio_db_write_stalled: false,diff --git a/crates/screenpipe-engine/src/ui_recorder.rs b/crates/screenpipe-engine/src/ui_recorder.rsindex a6a087007..e5185c278 100644--- a/crates/screenpipe-engine/src/ui_recorder.rs+++ b/crates/screenpipe-engine/src/ui_recorder.rs@@ -9,7 +9,7 @@use anyhow::Result;use screenpipe_a11y::{ExtractionThreadPriority, UiCaptureConfig, UiRecorder};use screenpipe_db::{DatabaseManager, InsertUiEvent};-use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};use std::sync::Arc;use std::time::Duration;use tracing::{debug, error, info, warn};@@ -170,6 +170,68 @@ pub fn tree_walker_snapshot() -> TreeWalkerSnapshot {.unwrap_or_default()}+/// Point-in-time status of the UI recorder. Exposed on `/health` so users+/// can tell whether input/clipboard capture is actually running — distinct+/// failure modes (config off, permissions denied, recorder errored) all+/// look the same from the DB ("ui_events stopped writing") but are very+/// different to recover from.+#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, oasgen::OaSchema)]+pub struct UiRecorderStatus {+ /// Did the runtime config request UI recording?+ pub configured: bool,+ /// Did the recorder's event loop actually start? False when configured+ /// is true but permissions were denied or `UiRecorder::start()` failed.+ pub running: bool,+ /// Is clipboard content capture configured? Subset of `configured`.+ pub clipboard_capture: bool,+ /// Lifetime count of events the recorder has flushed to the DB.+ pub events_inserted: u64,+ /// Wall-clock time of the most recent successful event-batch flush.+ pub last_event_at: Option<chrono::DateTime<chrono::Utc>>,+}++// Atomic-backed status so the flush_batch hot path doesn't need a mutex.+// `last_event_at_unix` of 0 means "never written yet".+static UI_RECORDER_CONFIGURED: AtomicBool = AtomicBool::new(false);+static UI_RECORDER_RUNNING: AtomicBool = AtomicBool::new(false);+static UI_RECORDER_CLIPBOARD: AtomicBool = AtomicBool::new(false);+static UI_RECORDER_EVENTS_INSERTED: AtomicU64 = AtomicU64::new(0);+static UI_RECORDER_LAST_EVENT_UNIX: AtomicU64 = AtomicU64::new(0);++fn set_ui_recorder_state(configured: bool, running: bool, clipboard: bool) {+ UI_RECORDER_CONFIGURED.store(configured, Ordering::Relaxed);+ UI_RECORDER_RUNNING.store(running, Ordering::Relaxed);+ UI_RECORDER_CLIPBOARD.store(clipboard, Ordering::Relaxed);+}++fn record_ui_event_flush(n: u64) {+ if n == 0 {+ return;+ }+ UI_RECORDER_EVENTS_INSERTED.fetch_add(n, Ordering::Relaxed);+ let now = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_secs())+ .unwrap_or(0);+ UI_RECORDER_LAST_EVENT_UNIX.store(now, Ordering::Relaxed);+}++/// Read the latest UI recorder status snapshot.+pub fn ui_recorder_status_snapshot() -> UiRecorderStatus {+ let last = UI_RECORDER_LAST_EVENT_UNIX.load(Ordering::Relaxed);+ UiRecorderStatus {+ configured: UI_RECORDER_CONFIGURED.load(Ordering::Relaxed),+ running: UI_RECORDER_RUNNING.load(Ordering::Relaxed),+ clipboard_capture: UI_RECORDER_CLIPBOARD.load(Ordering::Relaxed),+ events_inserted: UI_RECORDER_EVENTS_INSERTED.load(Ordering::Relaxed),+ last_event_at: if last > 0 {+ chrono::DateTime::<chrono::Utc>::from_timestamp(last as i64, 0)+ } else {+ None+ },+ }+}+/// Handle for managing the UI recorderpub struct UiRecorderHandle {stop_flag: Arc<AtomicBool>,@@ -226,6 +288,7 @@ pub async fn start_ui_recording() -> Result<UiRecorderHandle> {if !config.enabled {info!("UI event capture is disabled");+ set_ui_recorder_state(false, false, false);return Ok(UiRecorderHandle {stop_flag: Arc::new(AtomicBool::new(true)),task_handle: None,@@ -247,6 +310,9 @@ pub async fn start_ui_recording(let perms = recorder.request_permissions();if !perms.all_granted() {error!("UI capture permissions denied. UI event recording will be disabled.");+ // configured=true, running=false makes the failure mode legible:+ // "user asked for it, but it isn't actually running."+ set_ui_recorder_state(true, false, config.capture_clipboard_content);return Ok(UiRecorderHandle {stop_flag: Arc::new(AtomicBool::new(true)),task_handle: None,@@ -268,10 +334,13 @@ pub async fn start_ui_recording(Ok(h) => h,Err(e) => {error!("Failed to start UI recorder: {}", e);+ set_ui_recorder_state(true, false, config.capture_clipboard_content);return Err(e);}};+ set_ui_recorder_state(true, true, config.capture_clipboard_content);+// Spawn the event processing tasklet task_handle = tokio::spawn(async move {let session_id = Uuid::new_v4().to_string();@@ -413,6 +482,7 @@ pub async fn start_ui_recording(}handle.stop();+ UI_RECORDER_RUNNING.store(false, Ordering::Relaxed);info!("UI recording session ended: {}", session_id);});@@ -439,6 +509,7 @@ async fn flush_batch(match db.insert_ui_events_batch(batch).await {Ok(inserted) => {debug!("Flushed {} UI events to database", inserted);+ record_ui_event_flush(inserted as u64);*consecutive_failures = 0;}Err(e) => {@@ -493,6 +564,42 @@ mod tests {assert!(flag_clone.load(Ordering::Relaxed));}+ #[test]+ fn ui_recorder_status_reflects_state_and_flush() {+ // Note: globals are process-wide, but no other test in this binary+ // touches these atomics, so this single test is race-free.+ set_ui_recorder_state(true, true, true);+ let snap = ui_recorder_status_snapshot();+ assert!(snap.configured);+ assert!(snap.running);+ assert!(snap.clipboard_capture);++ let before = snap.events_inserted;+ record_ui_event_flush(0); // no-op+ assert_eq!(ui_recorder_status_snapshot().events_inserted, before);+ assert!(+ ui_recorder_status_snapshot().last_event_at.is_none()+ || ui_recorder_status_snapshot().last_event_at == snap.last_event_at,+ "zero-batch flush must not bump last_event_at"+ );++ record_ui_event_flush(3);+ let after = ui_recorder_status_snapshot();+ assert_eq!(after.events_inserted, before + 3);+ assert!(+ after.last_event_at.is_some(),+ "successful flush stamps a timestamp"+ );++ // disabled path: configured=false, running=false, no clipboard.+ set_ui_recorder_state(false, false, false);+ let off = ui_recorder_status_snapshot();+ assert!(!off.configured && !off.running && !off.clipboard_capture);+ // Counter and timestamp persist across state transitions — they're+ // lifetime metrics, not per-session.+ assert_eq!(off.events_inserted, after.events_inserted);+ }+#[tokio::test]async fn test_spawn_channel_pattern_clean_shutdown() {// Verifies the spawn+channel pattern doesn't panic when the