Skip to content

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

Fix macOS focused-app capture with AX observers

Tim Bowen228301196cf4
diff --git a/crates/screenpipe-accessibility/src/platform/macos.rs b/crates/screenpipe-accessibility/src/platform/macos.rs
index 6c12ca471..fd0658035 100644
--- a/crates/screenpipe-accessibility/src/platform/macos.rs
+++ b/crates/screenpipe-accessibility/src/platform/macos.rs
@@ -688,6 +688,108 @@ extern "C" fn tap_callback(
// App/Window Observer
// ============================================================================
+struct FocusState {
+ last_app: Option<String>,
+ last_pid: i32,
+ last_window: Option<String>,
+}
+
+struct ObserverCallbackState {
+ tx: Sender<UiEvent>,
+ start: Instant,
+ config: UiCaptureConfig,
+ current_app: Arc<Mutex<Option<String>>>,
+ current_window: Arc<Mutex<Option<String>>>,
+ focus: Mutex<FocusState>,
+ refresh_requested: Arc<AtomicBool>,
+}
+
+fn emit_focus_state(state: &ObserverCallbackState) {
+ let Some((pid, name)) = get_focused_app_info() else {
+ return;
+ };
+
+ if !state.config.should_capture_app(&name) {
+ return;
+ }
+
+ let mut focus = state.focus.lock();
+ let app_changed = focus.last_app.as_ref() != Some(&name) || focus.last_pid != pid;
+
+ if app_changed {
+ *state.current_app.lock() = Some(name.clone());
+
+ if state.config.capture_app_switch {
+ let focused_element = get_focused_element_context(&state.config);
+
+ let mut event = UiEvent::app_switch(
+ Utc::now(),
+ state.start.elapsed().as_millis() as u64,
+ name.clone(),
+ pid,
+ );
+ event.element = focused_element;
+ let _ = state.tx.try_send(event);
+ }
+
+ focus.last_app = Some(name.clone());
+ focus.last_pid = pid;
+ }
+
+ let window_title = get_focused_window_title(pid);
+ let should_capture = window_title
+ .as_ref()
+ .map(|w| state.config.should_capture_window(w))
+ .unwrap_or(true);
+
+ if should_capture && (window_title != focus.last_window || app_changed) {
+ *state.current_window.lock() = window_title.clone();
+
+ if state.config.capture_window_focus {
+ let focused_element = get_focused_element_context(&state.config);
+
+ let event = UiEvent {
+ id: None,
+ timestamp: Utc::now(),
+ relative_ms: state.start.elapsed().as_millis() as u64,
+ data: EventData::WindowFocus {
+ app: name,
+ title: window_title.clone().map(|s| truncate(&s, 200)),
+ },
+ app_name: None,
+ window_title: None,
+ browser_url: None,
+ element: focused_element,
+ frame_id: None,
+ };
+ let _ = state.tx.try_send(event);
+ }
+
+ focus.last_window = window_title;
+ }
+}
+
+extern "C" fn ax_focus_observer_callback(
+ _observer: &mut ax::Observer,
+ _elem: &mut ax::UiElement,
+ notification: &ax::Notification,
+ context: *mut std::ffi::c_void,
+) {
+ if context.is_null() {
+ return;
+ }
+
+ let state = unsafe { &*(context as *const ObserverCallbackState) };
+
+ if notification == ax::notification::app_activated()
+ || notification == ax::notification::app_deactivated()
+ {
+ state.refresh_requested.store(true, Ordering::SeqCst);
+ }
+
+ emit_focus_state(state);
+}
+
fn run_app_observer(
tx: Sender<UiEvent>,
stop: Arc<AtomicBool>,
@@ -697,92 +799,161 @@ fn run_app_observer(
current_window: Arc<Mutex<Option<String>>>,
) {
let workspace = ns::Workspace::shared();
+ let mut notification_center = workspace.notification_center();
+ let refresh_requested = Arc::new(AtomicBool::new(true));
+ let callback_state = Box::new(ObserverCallbackState {
+ tx,
+ start,
+ config,
+ current_app,
+ current_window,
+ focus: Mutex::new(FocusState {
+ last_app: None,
+ last_pid: 0,
+ last_window: None,
+ }),
+ refresh_requested: refresh_requested.clone(),
+ });
+ let callback_state_ptr = Box::into_raw(callback_state);
+
+ // NSWorkspace still helps for session/space lifecycle changes, but app-to-app
+ // reattachment comes from AX notifications on the observed app itself.
+ let _workspace_observers = vec![
+ notification_center.add_observer_guard(
+ ns::workspace::notification::did_activate_app(),
+ None,
+ None,
+ {
+ let refresh_requested = refresh_requested.clone();
+ move |_note| {
+ refresh_requested.store(true, Ordering::SeqCst);
+ }
+ },
+ ),
+ notification_center.add_observer_guard(
+ ns::workspace::notification::active_space_did_change(),
+ None,
+ None,
+ {
+ let refresh_requested = refresh_requested.clone();
+ move |_note| {
+ refresh_requested.store(true, Ordering::SeqCst);
+ }
+ },
+ ),
+ notification_center.add_observer_guard(
+ ns::workspace::notification::did_unhide_app(),
+ None,
+ None,
+ {
+ let refresh_requested = refresh_requested.clone();
+ move |_note| {
+ refresh_requested.store(true, Ordering::SeqCst);
+ }
+ },
+ ),
+ notification_center.add_observer_guard(
+ ns::workspace::notification::did_wake(),
+ None,
+ None,
+ {
+ let refresh_requested = refresh_requested.clone();
+ move |_note| {
+ refresh_requested.store(true, Ordering::SeqCst);
+ }
+ },
+ ),
+ notification_center.add_observer_guard(
+ ns::workspace::notification::session_did_become_active(),
+ None,
+ None,
+ {
+ let refresh_requested = refresh_requested.clone();
+ move |_note| {
+ refresh_requested.store(true, Ordering::SeqCst);
+ }
+ },
+ ),
+ ];
- let mut last_app: Option<String> = None;
- let mut last_pid: i32 = 0;
- let mut last_window: Option<String> = None;
-
- while !stop.load(Ordering::Relaxed) {
- let apps = workspace.running_apps();
- let active_app = apps.iter().find(|app| app.is_active());
-
- if let Some(app) = active_app {
- let name = app
- .localized_name()
- .map(|s| s.to_string())
- .unwrap_or_else(|| "?".to_string());
- let pid = app.pid();
-
- // Check exclusions
- if !config.should_capture_app(&name) {
- std::thread::sleep(std::time::Duration::from_millis(100));
- continue;
- }
+ let run_loop = cf::RunLoop::current();
+ let run_loop_mode = cf::RunLoopMode::default();
+ let mut observed_pid: i32 = 0;
+ let mut observer: Option<cidre::arc::R<ax::Observer>> = None;
- let app_changed = last_app.as_ref() != Some(&name) || last_pid != pid;
+ let mut reattach_observer = || {
+ let Some((pid, _name)) = get_focused_app_info() else {
+ return;
+ };
- if app_changed {
- // Update shared state for event tap thread
- *current_app.lock() = Some(name.clone());
+ if observed_pid == pid {
+ emit_focus_state(unsafe { &*callback_state_ptr });
+ return;
+ }
- if config.capture_app_switch {
- // Capture focused element's context (including text field values)
- let focused_element = get_focused_element_context(&config);
+ if let Some(existing) = observer.take() {
+ run_loop.remove_src(existing.run_loop_src(), run_loop_mode);
+ }
- let mut event = UiEvent::app_switch(
- Utc::now(),
- start.elapsed().as_millis() as u64,
- name.clone(),
- pid,
- );
- event.element = focused_element;
- let _ = tx.try_send(event);
- }
- last_app = Some(name.clone());
- last_pid = pid;
+ let app = ax::UiElement::with_app_pid(pid);
+ let mut new_observer = match ax::Observer::with_cb(pid, ax_focus_observer_callback) {
+ Ok(observer) => observer,
+ Err(err) => {
+ error!("failed to create AXObserver for pid {}: {:?}", pid, err);
+ observed_pid = 0;
+ return;
}
+ };
- // Check window change
- let window_title = get_focused_window_title(pid);
-
- // Check window exclusions
- let should_capture = window_title
- .as_ref()
- .map(|w| config.should_capture_window(w))
- .unwrap_or(true);
+ let context = callback_state_ptr as *mut std::ffi::c_void;
+ for notification in [
+ ax::notification::app_activated(),
+ ax::notification::app_deactivated(),
+ ax::notification::focused_window_changed(),
+ ax::notification::focused_ui_element_changed(),
+ ] {
+ if let Err(err) = new_observer.add_notification(&app, notification, context) {
+ debug!(
+ "failed to register AX notification {:?} for pid {}: {:?}",
+ notification, pid, err
+ );
+ }
+ }
- if should_capture && (window_title != last_window || app_changed) {
- // Update shared state for event tap thread
- *current_window.lock() = window_title.clone();
+ run_loop.add_src(new_observer.run_loop_src(), run_loop_mode);
+ observed_pid = pid;
+ observer = Some(new_observer);
+ emit_focus_state(unsafe { &*callback_state_ptr });
+ };
- if config.capture_window_focus {
- // Capture focused element's context (including text field values)
- let focused_element = get_focused_element_context(&config);
+ while !stop.load(Ordering::Relaxed) {
+ cf::RunLoop::run_in_mode(run_loop_mode, 0.1, true);
- let event = UiEvent {
- id: None,
- timestamp: Utc::now(),
- relative_ms: start.elapsed().as_millis() as u64,
- data: EventData::WindowFocus {
- app: name,
- title: window_title.clone().map(|s| truncate(&s, 200)),
- },
- app_name: None,
- window_title: None,
- browser_url: None,
- element: focused_element,
- frame_id: None,
- };
- let _ = tx.try_send(event);
- }
- last_window = window_title;
- }
+ if refresh_requested.swap(false, Ordering::SeqCst) {
+ reattach_observer();
}
+ }
+
+ if let Some(existing) = observer.take() {
+ run_loop.remove_src(existing.run_loop_src(), run_loop_mode);
+ }
- std::thread::sleep(std::time::Duration::from_millis(100));
+ unsafe {
+ drop(Box::from_raw(callback_state_ptr));
}
}
+fn get_focused_app_info() -> Option<(i32, String)> {
+ let sys = ax::UiElement::sys_wide();
+ let app = sys.focused_app().ok()?;
+ let pid = app.pid().ok()?;
+ let name = ns::RunningApp::with_pid(pid)
+ .and_then(|app| app.localized_name())
+ .map(|s| s.to_string())
+ .unwrap_or_else(|| "?".to_string());
+ Some((pid, name))
+}
+
// ============================================================================
// Accessibility Helpers
// ============================================================================
diff --git a/crates/screenpipe-accessibility/src/tree/macos.rs b/crates/screenpipe-accessibility/src/tree/macos.rs
index 86ef456d6..cb61451e5 100644
--- a/crates/screenpipe-accessibility/src/tree/macos.rs
+++ b/crates/screenpipe-accessibility/src/tree/macos.rs
@@ -213,11 +213,19 @@ impl MacosTreeWalker {
fn walk_focused_window_inner(&self) -> Result<Option<TreeSnapshot>> {
let start = Instant::now();
- // 1. Get the focused (active) application via NSWorkspace
- let workspace = ns::Workspace::shared();
- let apps = workspace.running_apps();
- let active_app = apps.iter().find(|app| app.is_active());
- let Some(app) = active_app else {
+ // 1. Get the focused application via the AX system-wide element.
+ // This stays within the accessibility stack instead of relying on
+ // NSWorkspace's foreground-app state from a background thread.
+ let sys = ax::UiElement::sys_wide();
+ let focused_app = match sys.focused_app() {
+ Ok(app) => app,
+ Err(_) => return Ok(None),
+ };
+ let pid = match focused_app.pid() {
+ Ok(pid) => pid,
+ Err(_) => return Ok(None),
+ };
+ let Some(app) = ns::RunningApp::with_pid(pid) else {
return Ok(None);
};
@@ -225,7 +233,6 @@ impl MacosTreeWalker {
.localized_name()
.map(|s| s.to_string())
.unwrap_or_default();
- let pid = app.pid();
// Skip excluded apps (password managers, etc.)
let app_lower = app_name.to_lowercase();
@@ -252,7 +259,7 @@ impl MacosTreeWalker {
}
// 2. Get the focused window via AX API
- let mut ax_app = ax::UiElement::with_app_pid(pid);
+ let mut ax_app = focused_app;
let _ = ax_app.set_messaging_timeout_secs(self.config.element_timeout_secs);
// Enable accessibility for Chromium/Electron apps. These apps only build
diff --git a/crates/screenpipe-server/src/event_driven_capture.rs b/crates/screenpipe-server/src/event_driven_capture.rs
index 52aab62b0..7a29e7945 100644
--- a/crates/screenpipe-server/src/event_driven_capture.rs
+++ b/crates/screenpipe-server/src/event_driven_capture.rs
@@ -549,6 +549,48 @@ struct CaptureOutput {
image: image::DynamicImage,
}
+fn resolve_capture_metadata(
+ tree_snapshot: Option<&screenpipe_accessibility::tree::TreeSnapshot>,
+ trigger: &CaptureTrigger,
+) -> (Option<String>, Option<String>, Option<String>) {
+ let (mut app_name, mut window_name, browser_url) = match tree_snapshot {
+ Some(snap) => (
+ Some(snap.app_name.clone()),
+ Some(snap.window_name.clone()),
+ snap.browser_url.clone(),
+ ),
+ None => (None, None, None),
+ };
+
+ match trigger {
+ CaptureTrigger::AppSwitch {
+ app_name: trigger_app_name,
+ } if !trigger_app_name.is_empty() => {
+ if app_name.as_deref() != Some(trigger_app_name.as_str()) {
+ debug!(
+ "focused app mismatch on app_switch: trigger='{}', tree={:?}; using trigger value",
+ trigger_app_name, app_name
+ );
+ }
+ app_name = Some(trigger_app_name.clone());
+ }
+ CaptureTrigger::WindowFocus {
+ window_name: trigger_window_name,
+ } if !trigger_window_name.is_empty() => {
+ if window_name.as_deref() != Some(trigger_window_name.as_str()) {
+ debug!(
+ "focused window mismatch on window_focus: trigger='{}', tree={:?}; using trigger value",
+ trigger_window_name, window_name
+ );
+ }
+ window_name = Some(trigger_window_name.clone());
+ }
+ _ => {}
+ }
+
+ (app_name, window_name, browser_url)
+}
+
/// Perform a single event-driven capture.
///
/// When `previous_content_hash` is `Some` and matches the current accessibility
@@ -611,15 +653,10 @@ async fn do_capture(
}
}
- // Use tree snapshot metadata for app/window/url if available
- let (app_name_owned, window_name_owned, browser_url_owned) = match &tree_snapshot {
- Some(snap) => (
- Some(snap.app_name.clone()),
- Some(snap.window_name.clone()),
- snap.browser_url.clone(),
- ),
- None => (None, None, None),
- };
+ // Use tree metadata by default, but for focus-change triggers prefer the
+ // event payload when the tree lags or reports the wrong frontmost target.
+ let (app_name_owned, window_name_owned, browser_url_owned) =
+ resolve_capture_metadata(tree_snapshot.as_ref(), trigger);
// Skip lock screen / screensaver — these waste disk and pollute timeline.
// Also update the global SCREEN_IS_LOCKED flag so subsequent loop iterations