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(a11y): Windows walk budgets inoperative — mid-walk deadline, fragile-provider gap, lightweight focused metadata (#4917)

Ezra Ellette88b82f772a21
diff --git a/crates/screenpipe-a11y/src/platform/windows.rs b/crates/screenpipe-a11y/src/platform/windows.rs
index c90d7ae9b..0e5566bff 100644
--- a/crates/screenpipe-a11y/src/platform/windows.rs
+++ b/crates/screenpipe-a11y/src/platform/windows.rs
@@ -1704,6 +1704,46 @@ pub(crate) fn get_effective_app_name(hwnd: HWND, pid: u32) -> String {
effective
}
+/// Cheaply resolve the focused window's (app name, window title) without any
+/// UIA/COM calls: `GetForegroundWindow` + cached process-name lookup +
+/// `GetWindowTextW`. Used by the engine's capture throttles (walk budget,
+/// terminal-OCR rate limit) to identify the focused app on non-AppSwitch
+/// triggers. The app name goes through [`get_effective_app_name`] so it matches
+/// the names produced by the tree walker and app-switch events.
+///
+/// Returns `None` when there is no usable foreground window, including the
+/// transient shell-internal windows (MSCTFIME UI, Shell_TrayWnd) that briefly
+/// steal focus on clicks — attributing those to explorer.exe would poison the
+/// per-app throttle state.
+pub fn get_focused_app_window_lightweight() -> Option<(String, Option<String>)> {
+ unsafe {
+ let hwnd = GetForegroundWindow();
+ if hwnd.is_invalid() {
+ return None;
+ }
+ if is_transient_shell_window(hwnd) {
+ return None;
+ }
+
+ let mut pid: u32 = 0;
+ GetWindowThreadProcessId(hwnd, Some(&mut pid));
+ if pid == 0 {
+ return None;
+ }
+ let app_name = get_effective_app_name(hwnd, pid);
+
+ let mut title_buf = [0u16; 512];
+ let len = GetWindowTextW(hwnd, &mut title_buf);
+ let window_title = if len > 0 {
+ Some(String::from_utf16_lossy(&title_buf[..len as usize]))
+ } else {
+ None
+ };
+
+ Some((app_name, window_title))
+ }
+}
+
fn get_process_name_uncached(pid: u32) -> Option<String> {
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::System::Diagnostics::ToolHelp::{
diff --git a/crates/screenpipe-a11y/src/platform/windows_uia.rs b/crates/screenpipe-a11y/src/platform/windows_uia.rs
index bbecb47b1..02899f075 100644
--- a/crates/screenpipe-a11y/src/platform/windows_uia.rs
+++ b/crates/screenpipe-a11y/src/platform/windows_uia.rs
@@ -9,13 +9,15 @@
use crate::config::UiCaptureConfig;
use crate::events::{AccessibilityNode, ElementBounds, ElementContext, WindowTreeSnapshot};
+use crate::tree::TruncationReason;
use chrono::Utc;
use crossbeam_channel::Sender;
use parking_lot::Mutex;
use std::collections::hash_map::DefaultHasher;
+use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
-use std::sync::Arc;
+use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use tracing::{debug, error, trace, warn};
@@ -90,6 +92,114 @@ pub struct ClickElementRequest {
pub timestamp: chrono::DateTime<chrono::Utc>,
}
+/// Default wall-clock budget when the caller doesn't provide one (tests,
+/// ad-hoc captures). Generous on purpose — the capture pipelines pass their
+/// own, much tighter budget via `capture_window_tree_bounded`.
+#[cfg(test)]
+const DEFAULT_TREE_CAPTURE_TIMEOUT: Duration = Duration::from_secs(5);
+
+/// A captured tree plus whether (and why) the walk stopped early.
+pub(crate) struct CapturedTree {
+ pub root: AccessibilityNode,
+ pub truncation: TruncationReason,
+}
+
+/// Shared node-count + wall-clock budget for a single tree capture.
+/// Records the first limit hit so callers can report truncation.
+struct TreeBudget {
+ max_elements: usize,
+ /// `None` = node cap only. Used for the cached path: its subtree is
+ /// already materialized in-process by the time nodes are built, so a
+ /// deadline there can only discard data that was already paid for —
+ /// it cannot shorten the target app's freeze.
+ deadline: Option<Instant>,
+ count: usize,
+ truncation: TruncationReason,
+}
+
+impl TreeBudget {
+ fn new(max_elements: usize, deadline: Instant) -> Self {
+ Self {
+ max_elements,
+ deadline: Some(deadline),
+ count: 0,
+ truncation: TruncationReason::None,
+ }
+ }
+
+ /// Budget bounded by node count only (cached, in-process tree builds).
+ fn node_capped(max_elements: usize) -> Self {
+ Self {
+ max_elements,
+ deadline: None,
+ count: 0,
+ truncation: TruncationReason::None,
+ }
+ }
+
+ /// True once a limit is hit; records the reason on first hit.
+ fn exhausted(&mut self) -> bool {
+ if self.count >= self.max_elements {
+ if self.truncation == TruncationReason::None {
+ self.truncation = TruncationReason::MaxNodes;
+ }
+ return true;
+ }
+ if let Some(deadline) = self.deadline {
+ if Instant::now() >= deadline {
+ if self.truncation == TruncationReason::None {
+ self.truncation = TruncationReason::Timeout;
+ }
+ return true;
+ }
+ }
+ false
+ }
+}
+
+/// How long a "this window needs the TreeWalker fallback" verdict stays valid.
+/// TTL'd because HWNDs get recycled by the OS and a provider can start
+/// populating the cached subtree later; re-probing costs one cross-process
+/// call, so a stale verdict is cheap to correct.
+const WALKER_FALLBACK_MEMO_TTL: Duration = Duration::from_secs(300);
+
+/// Process-wide memo of windows whose UIA provider requires the per-element
+/// TreeWalker fallback (Chromium/Electron). Shared across capture pipelines —
+/// the paired-capture path recreates its `UiaContext` per walk, so this can't
+/// live on the context.
+fn walker_fallback_memo() -> &'static Mutex<HashMap<isize, Instant>> {
+ static MEMO: OnceLock<Mutex<HashMap<isize, Instant>>> = OnceLock::new();
+ MEMO.get_or_init(|| Mutex::new(HashMap::new()))
+}
+
+/// True if `hwnd` was recently observed to need the TreeWalker fallback.
+fn needs_walker_fallback(hwnd: HWND) -> bool {
+ let mut memo = walker_fallback_memo().lock();
+ let key = hwnd.0 as isize;
+ match memo.get(&key) {
+ Some(seen) if seen.elapsed() < WALKER_FALLBACK_MEMO_TTL => true,
+ Some(_) => {
+ memo.remove(&key);
+ false
+ }
+ None => false,
+ }
+}
+
+/// Record that `hwnd`'s provider needs the TreeWalker fallback.
+fn remember_walker_fallback(hwnd: HWND) {
+ let mut memo = walker_fallback_memo().lock();
+ if memo.len() >= 64 {
+ memo.retain(|_, seen| seen.elapsed() < WALKER_FALLBACK_MEMO_TTL);
+ }
+ memo.insert(hwnd.0 as isize, Instant::now());
+}
+
+/// Drop the fallback verdict for `hwnd` (cached path produced a real tree).
+fn forget_walker_fallback(hwnd: HWND) {
+ walker_fallback_memo().lock().remove(&(hwnd.0 as isize));
+}
+
/// UIA context holding COM objects (single-thread only, not Send)
pub(crate) struct UiaContext {
automation: IUIAutomation,
@@ -167,52 +277,116 @@ impl UiaContext {
}
}
- /// Capture the full accessibility tree of a window by HWND.
- /// Uses CacheRequest to batch all property reads into minimal cross-process calls.
- /// Falls back to TreeWalker for apps whose UIA providers don't populate
- /// the cached subtree (Chromium, Electron, etc.).
+ /// Capture the full accessibility tree of a window by HWND with a default
+ /// wall-clock budget. Kept as the convenience entry point for tests —
+ /// production pipelines pass their own budget via
+ /// [`Self::capture_window_tree_bounded`].
+ #[cfg(test)]
pub(crate) fn capture_window_tree(
&self,
hwnd: HWND,
max_elements: usize,
) -> Option<AccessibilityNode> {
+ self.capture_window_tree_bounded(hwnd, max_elements, DEFAULT_TREE_CAPTURE_TIMEOUT)
+ .map(|captured| captured.root)
+ }
+
+ /// Capture the full accessibility tree of a window by HWND, bounded by a
+ /// node cap and a wall-clock deadline.
+ ///
+ /// Uses CacheRequest to batch all property reads into minimal cross-process
+ /// calls. Falls back to TreeWalker for apps whose UIA providers don't
+ /// populate the cached subtree (Chromium, Electron, etc.).
+ ///
+ /// The deadline bounds the per-element TreeWalker fallback (~2 cross-process
+ /// COM round-trips per element, serviced on the target app's UI thread — the
+ /// unbounded-freeze path), checked before every element fetch so it overruns
+ /// by at most one element. The cached path is different: its one
+ /// `ElementFromHandleBuildCache` call is synchronous and cannot be
+ /// interrupted, and afterwards the subtree is already materialized
+ /// in-process — so building it is bounded by the node cap only. Truncating
+ /// that local build on the deadline would discard data the target app
+ /// already paid to produce without shortening its freeze.
+ pub(crate) fn capture_window_tree_bounded(
+ &self,
+ hwnd: HWND,
+ max_elements: usize,
+ timeout: Duration,
+ ) -> Option<CapturedTree> {
+ let deadline = Instant::now() + timeout;
+
+ // Windows already known to need the per-element fallback (Chromium/
+ // Electron) go straight to it, so the walker gets the full wall-clock
+ // budget instead of paying for a cached attempt that returns a handful
+ // of titlebar nodes every single walk.
+ if needs_walker_fallback(hwnd) {
+ let mut walker_budget = TreeBudget::new(max_elements, deadline);
+ if let Some(root) = self.capture_window_tree_walker(hwnd, &mut walker_budget) {
+ return Some(CapturedTree {
+ root,
+ truncation: walker_budget.truncation,
+ });
+ }
+ // Walker setup failed (window gone, provider error) — fall through
+ // to the cached path rather than dropping the capture.
+ }
+
unsafe {
let element = self
.automation
.ElementFromHandleBuildCache(hwnd, &self.cache_request)
.ok()?;
- let mut count = 0;
- let root = self.build_node(&element, max_elements, &mut count);
+ // Node cap only: the whole subtree was materialized by the single
+ // COM call above; building nodes from it is local work.
+ let mut budget = TreeBudget::node_capped(max_elements);
+ let root = self.build_node(&element, &mut budget);
+ let count = budget.count;
// Some UIA providers (notably Chromium/Electron) don't populate the
// cached subtree via ElementFromHandleBuildCache, returning only a
// handful of titlebar nodes. When this happens, fall back to
- // TreeWalker which makes individual COM calls per element.
+ // TreeWalker which makes individual COM calls per element. The
+ // fallback runs against the original deadline (so a slow cached
+ // attempt shrinks its window) with a fresh node budget, matching
+ // the count comparison below.
if count <= 10 {
- if let Some(walker_root) = self.capture_window_tree_walker(hwnd, max_elements) {
+ let mut walker_budget = TreeBudget::new(max_elements, deadline);
+ if let Some(walker_root) = self.capture_window_tree_walker(hwnd, &mut walker_budget)
+ {
let walker_count = walker_root.node_count();
if walker_count > count {
debug!(
"Cache returned {} nodes, walker returned {} - using walker result",
count, walker_count
);
- return Some(walker_root);
+ remember_walker_fallback(hwnd);
+ return Some(CapturedTree {
+ root: walker_root,
+ truncation: walker_budget.truncation,
+ });
}
}
+ } else {
+ // Cached path works for this window — clear any stale memo
+ // (HWND recycling, provider started populating the cache).
+ forget_walker_fallback(hwnd);
}
- Some(root)
+ Some(CapturedTree {
+ root,
+ truncation: budget.truncation,
+ })
}
}
/// Fallback tree capture using TreeWalker for UIA providers that don't
/// support cached subtree (e.g. Chromium, Electron apps).
- /// Walks the tree element-by-element via GetFirstChild/GetNextSibling.
+ /// Walks the tree element-by-element via COM calls, bounded by `budget`.
fn capture_window_tree_walker(
&self,
hwnd: HWND,
- max_elements: usize,
+ budget: &mut TreeBudget,
) -> Option<AccessibilityNode> {
unsafe {
// Get a live element (required for TreeWalker navigation)
@@ -222,21 +396,20 @@ impl UiaContext {
.BuildUpdatedCache(&self.walker_cache_request)
.ok()?;
- let mut count = 0;
- Some(self.build_node_walker(&cached_root, max_elements, &mut count))
+ Some(self.build_node_walker(&cached_root, budget))
}
}
/// Recursively build an AccessibilityNode using TreeWalker navigation.
/// Unlike build_node which reads pre-cached children, this walks the tree
- /// one element at a time via COM calls.
+ /// one element at a time via COM calls (~2 cross-process round trips per
+ /// element), so the budget's deadline is checked before every fetch.
fn build_node_walker(
&self,
element: &IUIAutomationElement,
- max_elements: usize,
- count: &mut usize,
+ budget: &mut TreeBudget,
) -> AccessibilityNode {
- *count += 1;
+ budget.count += 1;
let control_type = self.get_control_type_name(element);
let name = self.get_cached_string(element, UIA_NamePropertyId);
@@ -256,23 +429,23 @@ impl UiaContext {
self.get_cached_string(element, UIA_LocalizedControlTypePropertyId);
let mut children = Vec::new();
- if *count < max_elements {
+ if !budget.exhausted() {
unsafe {
// Navigate to first child via TreeWalker
if let Ok(child) = self
.tree_walker
.GetFirstChildElementBuildCache(element, &self.walker_cache_request)
{
- children.push(self.build_node_walker(&child, max_elements, count));
+ children.push(self.build_node_walker(&child, budget));
// Iterate siblings
let mut current = child;
- while *count < max_elements {
+ while !budget.exhausted() {
match self
.tree_walker
.GetNextSiblingElementBuildCache(&current, &self.walker_cache_request)
{
Ok(next) => {
- children.push(self.build_node_walker(&next, max_elements, count));
+ children.push(self.build_node_walker(&next, budget));
current = next;
}
Err(_) => break,
@@ -304,13 +477,14 @@ impl UiaContext {
}
/// Recursively build an AccessibilityNode from a cached UIA element.
+ /// The subtree is already materialized in-process, so per-node work is a
+ /// local property read; callers pass a node-cap-only budget.
fn build_node(
&self,
element: &IUIAutomationElement,
- max_elements: usize,
- count: &mut usize,
+ budget: &mut TreeBudget,
) -> AccessibilityNode {
- *count += 1;
+ budget.count += 1;
let control_type = self.get_control_type_name(element);
let name = self.get_cached_string(element, UIA_NamePropertyId);
@@ -330,17 +504,17 @@ impl UiaContext {
self.get_cached_string(element, UIA_LocalizedControlTypePropertyId);
let mut children = Vec::new();
- if *count < max_elements {
+ if !budget.exhausted() {
unsafe {
// Walk cached children (already fetched via TreeScope_Subtree)
if let Ok(child_array) = element.GetCachedChildren() {
if let Ok(len) = child_array.Length() {
for i in 0..len {
- if *count >= max_elements {
+ if budget.exhausted() {
break;
}
if let Ok(child) = child_array.GetElement(i) {
- children.push(self.build_node(&child, max_elements, count));
+ children.push(self.build_node(&child, budget));
}
}
}
@@ -926,7 +1100,7 @@ const FRAGILE_UIA_TREE_PROVIDERS: &[&str] = &["outlook.exe"];
/// Returns true if `app_name` is a process whose UIA provider can crash when we capture
/// its full accessibility tree. See [`FRAGILE_UIA_TREE_PROVIDERS`].
-fn is_fragile_uia_tree_provider(app_name: &str) -> bool {
+pub(crate) fn is_fragile_uia_tree_provider(app_name: &str) -> bool {
let lower = app_name.to_ascii_lowercase();
FRAGILE_UIA_TREE_PROVIDERS.iter().any(|p| lower == *p)
}
@@ -969,9 +1143,24 @@ fn capture_and_send(
return;
}
- // Capture the tree
- let root = match uia.capture_window_tree(hwnd, config.tree_max_elements) {
- Some(root) => root,
+ // Capture the tree, bounded by a wall-clock deadline: the per-element
+ // TreeWalker fallback (Chromium/Electron) is serviced synchronously on the
+ // target app's UI thread, so an unbounded walk visibly freezes the app.
+ const PERIODIC_TREE_WALK_TIMEOUT: Duration = Duration::from_millis(250);
+ let root = match uia.capture_window_tree_bounded(
+ hwnd,
+ config.tree_max_elements,
+ PERIODIC_TREE_WALK_TIMEOUT,
+ ) {
+ Some(captured) => {
+ if captured.truncation != TruncationReason::None {
+ debug!(
+ "Tree capture for {} truncated ({:?})",
+ app_name, captured.truncation
+ );
+ }
+ captured.root
+ }
None => {
trace!("Failed to capture tree for hwnd {:?}", hwnd.0);
return;
@@ -1162,6 +1351,107 @@ mod tests {
assert!(!is_fragile_uia_tree_provider(""));
}
+ #[test]
+ fn test_tree_budget_max_nodes() {
+ let mut budget = TreeBudget::new(2, Instant::now() + Duration::from_secs(60));
+ assert!(!budget.exhausted());
+ assert_eq!(budget.truncation, TruncationReason::None);
+
+ budget.count = 2;
+ assert!(budget.exhausted());
+ assert_eq!(budget.truncation, TruncationReason::MaxNodes);
+
+ // First-hit reason sticks even if the deadline also passes later.
+ budget.deadline = Some(Instant::now() - Duration::from_secs(1));
+ assert!(budget.exhausted());
+ assert_eq!(budget.truncation, TruncationReason::MaxNodes);
+ }
+
+ #[test]
+ fn test_tree_budget_deadline() {
+ let mut budget = TreeBudget::new(10_000, Instant::now() - Duration::from_millis(1));
+ assert!(budget.exhausted());
+ assert_eq!(budget.truncation, TruncationReason::Timeout);
+ }
+
+ #[test]
+ fn test_tree_budget_node_capped_ignores_time() {
+ // Cached-path budget: no deadline, only the node cap binds.
+ let mut budget = TreeBudget::node_capped(3);
+ assert!(!budget.exhausted());
+ budget.count = 3;
+ assert!(budget.exhausted());
+ assert_eq!(budget.truncation, TruncationReason::MaxNodes);
+ }
+
+ #[test]
+ fn test_walker_fallback_memo_roundtrip() {
+ // Distinct pointer value so parallel tests sharing the process-wide
+ // memo can't interfere.
+ let hwnd = HWND(0x5EED_F00D_usize as *mut _);
+
+ assert!(!needs_walker_fallback(hwnd));
+ remember_walker_fallback(hwnd);
+ assert!(needs_walker_fallback(hwnd));
+ forget_walker_fallback(hwnd);
+ assert!(!needs_walker_fallback(hwnd));
+ }
+
+ /// Live test: the wall-clock deadline bounds a real capture.
+ /// Focus a Chromium/Electron window (whose provider forces the
+ /// per-element TreeWalker fallback) to see `Timeout` truncation — the
+ /// deadline binds that path. Windows served by the cached path return
+ /// their full (node-capped) tree regardless of the deadline, since the
+ /// subtree arrives in one COM call. Run:
+ /// cargo test -p screenpipe-a11y test_live_bounded_capture -- --ignored --nocapture
+ #[test]
+ #[ignore]
+ fn test_live_bounded_capture_truncates() {
+ unsafe {
+ CoInitializeEx(None, COINIT_APARTMENTTHREADED)
+ .ok()
+ .expect("COM init failed");
+ }
+ let uia = UiaContext::new().expect("UIA init failed");
+ let hwnd = unsafe { GetForegroundWindow() };
+ assert!(!hwnd.is_invalid(), "No foreground window");
+
+ // Reference walk with a generous budget.
+ let start = std::time::Instant::now();
+ let full = uia
+ .capture_window_tree_bounded(hwnd, 10_000, Duration::from_secs(10))
+ .expect("capture failed");
+ let full_ms = start.elapsed().as_millis();
+ println!(
+ "full walk: {} nodes in {}ms (truncation: {:?})",
+ full.root.node_count(),
+ full_ms,
+ full.truncation
+ );
+
+ // Tightly bounded walk must come back promptly. The deadline can't
+ // interrupt the initial ElementFromHandleBuildCache COM call, so the
+ // assertion is loose — the point is it doesn't run for seconds.
+ let start = std::time::Instant::now();
+ let bounded = uia
+ .capture_window_tree_bounded(hwnd, 10_000, Duration::from_millis(50))
+ .expect("capture failed");
+ let bounded_ms = start.elapsed().as_millis();
+ println!(
+ "bounded 50ms: {} nodes in {}ms (truncation: {:?})",
+ bounded.root.node_count(),
+ bounded_ms,
+ bounded.truncation
+ );
+
+ assert!(
+ bounded_ms < 5_000,
+ "bounded walk took {}ms — deadline not honored",
+ bounded_ms
+ );
+ unsafe { CoUninitialize() };
+ }
+
#[test]
fn test_tree_hash_differs() {
let node1 = AccessibilityNode {
diff --git a/crates/screenpipe-a11y/src/tree/windows.rs b/crates/screenpipe-a11y/src/tree/windows.rs
index e97f92bb6..97fc3498e 100644
--- a/crates/screenpipe-a11y/src/tree/windows.rs
+++ b/crates/screenpipe-a11y/src/tree/windows.rs
@@ -216,6 +216,15 @@ impl TreeWalkerPlatform for WindowsTreeWalker {
return Ok(TreeWalkResult::Skipped(SkipReason::ExcludedApp));
}
+ // Skip apps whose in-process UIA providers crash the host process when an
+ // external client materializes their full subtree (Outlook Classic on Sent
+ // Items) — same exemption as the periodic worker path. OCR still captures
+ // the on-screen content for these apps.
+ if crate::platform::windows_uia::is_fragile_uia_tree_provider(&app_name) {
+ debug!(app = %app_name, pid, "a11y: skipped — fragile UIA tree provider (crash-prone)");
+ return Ok(TreeWalkResult::Skipped(SkipReason::ExcludedApp));
+ }
+
// Get window title
let window_name = unsafe {
let mut buf = [0u16; 512];
@@ -275,11 +284,18 @@ impl TreeWalkerPlatform for WindowsTreeWalker {
return Ok(TreeWalkResult::NotFound);
}
- // Capture the accessibility tree
- let root = match uia.capture_window_tree(hwnd, effective_max_nodes) {
- Some(tree) => tree,
- None => return Ok(TreeWalkResult::NotFound),
- };
+ // Capture the accessibility tree, bounded by the remaining wall-clock
+ // budget. The in-walk deadline truncates the per-element TreeWalker
+ // fallback (Chromium/Electron), which is serviced synchronously on the
+ // target app's UI thread and freezes it for the duration of the walk.
+ let remaining_budget = effective_timeout.saturating_sub(start.elapsed());
+ let captured =
+ match uia.capture_window_tree_bounded(hwnd, effective_max_nodes, remaining_budget) {
+ Some(captured) => captured,
+ None => return Ok(TreeWalkResult::NotFound),
+ };
+ let root = captured.root;
+ let truncation_reason = captured.truncation;
// Get monitor dimensions for normalizing element bounds to 0-1 coords
let monitor_rect = get_monitor_rect(hwnd);
@@ -342,7 +358,6 @@ impl TreeWalkerPlatform for WindowsTreeWalker {
walk_duration
);
- // Windows walker doesn't have timeout-based truncation yet — report as complete
// Per-app document_path resolution from on-disk state files
// (Obsidian config + VS Code-fork state.vscdb). Returns None
// for any unknown app or any failure — never panics.
@@ -360,8 +375,8 @@ impl TreeWalkerPlatform for WindowsTreeWalker {
walk_duration,
content_hash,
simhash,
- truncated: false,
- truncation_reason: super::TruncationReason::None,
+ truncated: truncation_reason != super::TruncationReason::None,
+ truncation_reason,
max_depth_reached: 0,
}))
}
@@ -1019,9 +1034,11 @@ mod tests {
#[test]
fn test_incognito_detection() {
use crate::incognito::is_title_private;
- assert!(is_title_private("Enter Password - Chrome"));
assert!(is_title_private("Private Browsing - Firefox"));
assert!(is_title_private("New Tab - Google Chrome (Incognito)"));
assert!(!is_title_private("Calculator"));
+ // Bare "password"/"private" words are deliberately NOT incognito
+ // indicators (false-positive reduction — see incognito/titles.rs).
+ assert!(!is_title_private("Enter Password - Chrome"));
}
}
diff --git a/crates/screenpipe-engine/src/event_driven_capture.rs b/crates/screenpipe-engine/src/event_driven_capture.rs
index 981940caf..4923d4cde 100644
--- a/crates/screenpipe-engine/src/event_driven_capture.rs
+++ b/crates/screenpipe-engine/src/event_driven_capture.rs
@@ -1911,6 +1911,7 @@ fn is_ocr_heavy_app(app_name: &str) -> bool {
|| contains_ascii_case_insensitive(app_name, "hyper")
|| contains_ascii_case_insensitive(app_name, "warp")
|| app_name.eq_ignore_ascii_case("obsidian")
+ || app_name.eq_ignore_ascii_case("obsidian.exe")
}
fn contains_ascii_case_insensitive(haystack: &str, needle: &str) -> bool {
@@ -1928,6 +1929,11 @@ fn is_lock_screen_app(app_name: &str) -> bool {
app_name.eq_ignore_ascii_case("loginwindow")
|| app_name.eq_ignore_ascii_case("screensaverengine")
|| app_name.eq_ignore_ascii_case("lockscreen")
+ // Windows: LockApp.exe hosts the lock screen UI, LogonUI.exe the
+ // credential prompt. Both must be recognized here — an unrecognized
+ // Some(app) would otherwise clear the global screen-locked flag.
+ || app_name.eq_ignore_ascii_case("lockapp.exe")
+ || app_name.eq_ignore_ascii_case("logonui.exe")
}
/// How long the persistent stream's frame-delivery sequence may stay flat
@@ -2598,7 +2604,54 @@ fn get_focused_metadata_lightweight() -> Option<LightweightFocusedMetadata> {
})
}
-#[cfg(not(any(target_os = "macos", target_os = "linux")))]
+/// Windows: `GetForegroundWindow` + cached process-name lookup — no UIA/COM.
+/// The app name matches what the tree walker reports (both go through
+/// `get_effective_app_name`), so the walk budget's per-app cost tracking keys
+/// line up across `should_walk` and `record_walk`.
+///
+/// **Caching**: capture triggers fire on every click / typing pause / visual
+/// change, and the focused window rarely changes between triggers. A 1-second
+/// TTL bounds staleness below human perception while collapsing the common
+/// case to a single atomic load — same rationale as the macOS implementation.
+#[cfg(target_os = "windows")]
+fn get_focused_metadata_lightweight() -> Option<LightweightFocusedMetadata> {
+ use arc_swap::ArcSwap;
+ use std::sync::OnceLock;
+ use std::time::{Duration, Instant};
+
+ const CACHE_TTL: Duration = Duration::from_secs(1);
+
+ static CACHE: OnceLock<ArcSwap<(Option<LightweightFocusedMetadata>, Instant)>> =
+ OnceLock::new();
+ let cache = CACHE.get_or_init(|| {
+ // Seed with an already-expired timestamp. Windows `Instant` counts
+ // from boot, so subtracting can fail in the first seconds after boot
+ // (autostart) — fall back to "fresh", costing one stale-second at most.
+ let expired = Instant::now()
+ .checked_sub(CACHE_TTL + Duration::from_secs(1))
+ .unwrap_or_else(Instant::now);
+ ArcSwap::from_pointee((None, expired))
+ });
+
+ let now = Instant::now();
+ {
+ let snap = cache.load();
+ if now.duration_since(snap.1) < CACHE_TTL {
+ return snap.0.clone();
+ }
+ }
+
+ let fresh = screenpipe_a11y::platform::windows::get_focused_app_window_lightweight().map(
+ |(app_name, window_name)| LightweightFocusedMetadata {
+ app_name: Some(app_name),
+ window_name,
+ },
+ );
+ cache.store(std::sync::Arc::new((fresh.clone(), now)));
+ fresh
+}
+
+#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn get_focused_metadata_lightweight() -> Option<LightweightFocusedMetadata> {
None
}
@@ -3222,7 +3275,11 @@ mod tests {
assert!(is_ocr_heavy_app("WezTerm"));
assert!(is_ocr_heavy_app("com.mitchellh.ghostty.WARP"));
assert!(is_ocr_heavy_app("Obsidian"));
+ // Windows process names carry the extension.
+ assert!(is_ocr_heavy_app("Obsidian.exe"));
+ assert!(is_ocr_heavy_app("wezterm-gui.exe"));
assert!(!is_ocr_heavy_app("Chrome"));
+ assert!(!is_ocr_heavy_app("chrome.exe"));
}
#[test]
@@ -3230,7 +3287,12 @@ mod tests {
assert!(is_lock_screen_app("loginwindow"));
assert!(is_lock_screen_app("ScreenSaverEngine"));
assert!(is_lock_screen_app("LOCKSCREEN"));
+ // Windows lock screen host + credential UI. Must be recognized or a
+ // Some(app) at the lock screen clears the global screen-locked flag.
+ assert!(is_lock_screen_app("LockApp.exe"));
+ assert!(is_lock_screen_app("LogonUI.exe"));
assert!(!is_lock_screen_app("Finder"));
+ assert!(!is_lock_screen_app("explorer.exe"));
}
#[test]