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

feat: hybrid OCR capture for canvas-rendered apps (Google Docs, Figma)

Louis Beaumont4d2b05990971
diff --git a/crates/screenpipe-engine/src/paired_capture.rs b/crates/screenpipe-engine/src/paired_capture.rs
index 27492fd18..fa75616cb 100644
--- a/crates/screenpipe-engine/src/paired_capture.rs
+++ b/crates/screenpipe-engine/src/paired_capture.rs
@@ -123,8 +123,17 @@ pub async fn paired_capture(
.map(|s| !s.text_content.is_empty())
.unwrap_or(false);
- // Only run OCR when accessibility tree returned no text or app prefers OCR
- let (ocr_text, ocr_text_json) = if !has_accessibility_text {
+ // Check if accessibility text is "thin" — the tree returned SOME text
+ // (toolbar, sidebar, browser chrome) but likely missed the main content.
+ // This happens with canvas-rendered apps (Google Docs, Figma, etc.)
+ // where the document body is invisible to the accessibility tree.
+ let a11y_is_thin = has_accessibility_text
+ && tree_snapshot
+ .map(|s| a11y_content_is_thin(s, ctx.window_name))
+ .unwrap_or(false);
+
+ // Run OCR when: no a11y text, app prefers OCR, OR a11y text is thin (hybrid)
+ let (ocr_text, ocr_text_json) = if !has_accessibility_text || a11y_is_thin {
// Windows native OCR is async, so call it directly (not inside spawn_blocking)
#[cfg(target_os = "windows")]
{
@@ -199,10 +208,16 @@ pub async fn paired_capture(
}
};
- // Determine text source: "accessibility" when tree nodes were available, "ocr" for fallback
+ // Determine text source: "accessibility" when tree nodes were available,
+ // "ocr" for fallback, "hybrid" when both ran (thin a11y supplemented by OCR)
let (final_text, text_source) = if let Some(ref text) = accessibility_text {
if text.is_empty() {
(None, None)
+ } else if tree_json.is_some() && a11y_is_thin && !ocr_text.is_empty() {
+ // Hybrid: a11y had chrome text, OCR captured the real content.
+ // Keep a11y text as accessibility_text (structured); OCR data is
+ // stored separately in the ocr_text table via ocr_data below.
+ (Some(text.as_str()), Some("hybrid"))
} else if tree_json.is_some() {
(Some(text.as_str()), Some("accessibility"))
} else {
@@ -292,13 +307,16 @@ pub async fn paired_capture(
}
/// Walk the accessibility tree for the currently focused window.
-/// Returns the text content and app/window metadata.
+/// Returns a `TreeWalkResult` distinguishing found/skipped/not-found states.
///
/// This is a blocking operation that should be spawned on a blocking thread.
-pub fn walk_accessibility_tree(config: &TreeWalkerConfig) -> Option<TreeSnapshot> {
+pub fn walk_accessibility_tree(
+ config: &TreeWalkerConfig,
+) -> screenpipe_a11y::tree::TreeWalkResult {
+ use screenpipe_a11y::tree::TreeWalkResult;
let walker = create_tree_walker(config.clone());
match walker.walk_focused_window() {
- Ok(Some(snapshot)) => {
+ Ok(TreeWalkResult::Found(snapshot)) => {
debug!(
"tree walk: app={}, window={}, text_len={}, nodes={}, structured_nodes={}, dur={:?}",
snapshot.app_name,
@@ -308,17 +326,114 @@ pub fn walk_accessibility_tree(config: &TreeWalkerConfig) -> Option<TreeSnapshot
snapshot.nodes.len(),
snapshot.walk_duration
);
- Some(snapshot)
+ TreeWalkResult::Found(snapshot)
+ }
+ Ok(TreeWalkResult::Skipped) => {
+ debug!("tree walk: window skipped (incognito/filtered)");
+ TreeWalkResult::Skipped
}
- Ok(None) => {
+ Ok(TreeWalkResult::NotFound) => {
debug!("tree walk: no focused window found");
- None
+ TreeWalkResult::NotFound
}
Err(e) => {
warn!("tree walk failed: {}", e);
- None
+ TreeWalkResult::NotFound
+ }
+ }
+}
+
+/// Known canvas-rendered apps/sites where the accessibility tree returns only
+/// UI chrome (toolbar, sidebar) but the main content is drawn on a <canvas>
+/// or GPU surface and invisible to the a11y tree.
+const CANVAS_APP_PATTERNS: &[&str] = &[
+ "google docs",
+ "google sheets",
+ "google slides",
+ "google drawings",
+ "figma",
+ "excalidraw",
+ "miro",
+ "canva",
+ "tldraw",
+];
+
+/// Check if the accessibility tree captured mostly UI chrome and likely missed
+/// the actual content. Returns `true` when OCR should supplement a11y data.
+///
+/// Two checks:
+/// 1. **Known canvas apps**: window title matches a known pattern → always thin.
+/// 2. **Content density heuristic**: classify nodes by role; if <30% of text
+/// characters come from content roles (vs toolbar/menu chrome), it's thin.
+fn a11y_content_is_thin(
+ snap: &screenpipe_a11y::tree::TreeSnapshot,
+ window_name: Option<&str>,
+) -> bool {
+ // 1. Known canvas-rendered apps — always need OCR
+ if let Some(win) = window_name {
+ let win_lower = win.to_lowercase();
+ if CANVAS_APP_PATTERNS
+ .iter()
+ .any(|pat| win_lower.contains(pat))
+ {
+ debug!("a11y_content_is_thin: known canvas app '{}'", win);
+ return true;
+ }
+ }
+
+ // 2. Content density heuristic
+ // Chrome roles: buttons, menus, toolbars — UI controls, not document content
+ const CHROME_ROLES: &[&str] = &[
+ "AXButton",
+ "AXMenuItem",
+ "AXMenuBar",
+ "AXMenu",
+ "AXToolbar",
+ "AXTabGroup",
+ "AXTab",
+ "AXPopUpButton",
+ "AXCheckBox",
+ "AXRadioButton",
+ "AXDisclosureTriangle",
+ "AXSlider",
+ "AXIncrementor",
+ "AXComboBox",
+ "AXScrollBar",
+ ];
+
+ let mut content_chars: usize = 0;
+ let mut total_chars: usize = 0;
+
+ for node in &snap.nodes {
+ let len = node.text.len();
+ if len == 0 {
+ continue;
}
+ total_chars += len;
+ if !CHROME_ROLES.iter().any(|r| node.role == *r) {
+ content_chars += len;
+ }
+ }
+
+ // Very little text overall — likely missing content
+ if total_chars < 100 {
+ debug!(
+ "a11y_content_is_thin: total_chars={} < 100, treating as thin",
+ total_chars
+ );
+ return true;
+ }
+
+ let ratio = content_chars as f64 / total_chars as f64;
+ if ratio < 0.3 {
+ debug!(
+ "a11y_content_is_thin: content_ratio={:.2} < 0.3 (content={}, total={})",
+ ratio, content_chars, total_chars
+ );
+ return true;
}
+
+ false
}
/// Sanitize PII from OCR text_json (a JSON string of bounding-box entries).
@@ -561,4 +676,95 @@ mod tests {
let sanitized_text: Option<String> = Some(text).map(|t| t.to_string());
assert_eq!(sanitized_text.as_deref(), Some("[email protected]"));
}
+
+ // --- a11y_content_is_thin tests ---
+
+ fn make_snap(nodes: Vec<AccessibilityTreeNode>) -> TreeSnapshot {
+ let text = nodes.iter().map(|n| n.text.as_str()).collect::<Vec<_>>().join("\n");
+ TreeSnapshot {
+ app_name: "Test".to_string(),
+ window_name: "Test Window".to_string(),
+ text_content: text,
+ nodes,
+ browser_url: None,
+ timestamp: Utc::now(),
+ node_count: 0,
+ walk_duration: std::time::Duration::from_millis(1),
+ content_hash: 0,
+ simhash: 0,
+ truncated: false,
+ truncation_reason: screenpipe_a11y::tree::TruncationReason::None,
+ max_depth_reached: 0,
+ }
+ }
+
+ #[test]
+ fn test_thin_known_canvas_app_google_docs() {
+ let snap = make_snap(vec![
+ AccessibilityTreeNode { role: "AXStaticText".into(), text: "Lots of real content here that is very long and should normally be fine".into(), depth: 0, bounds: None },
+ ]);
+ // Google Docs in window title → always thin regardless of content
+ assert!(a11y_content_is_thin(&snap, Some("Untitled - Google Docs")));
+ }
+
+ #[test]
+ fn test_thin_known_canvas_app_figma() {
+ let snap = make_snap(vec![]);
+ assert!(a11y_content_is_thin(&snap, Some("My Design - Figma")));
+ }
+
+ #[test]
+ fn test_not_thin_normal_webpage() {
+ // Normal webpage: mostly AXStaticText content
+ let snap = make_snap(vec![
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Menu".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXStaticText".into(), text: "This is a long article about dogs. Dogs are domesticated descendants of wolves. They were the first species to be domesticated over 14,000 years ago.".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXLink".into(), text: "Read more about canine history".into(), depth: 1, bounds: None },
+ ]);
+ assert!(!a11y_content_is_thin(&snap, Some("Dog - Wikipedia")));
+ }
+
+ #[test]
+ fn test_thin_mostly_chrome() {
+ // All buttons/menus, very little content — like a canvas app's toolbar
+ let snap = make_snap(vec![
+ AccessibilityTreeNode { role: "AXButton".into(), text: "File".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Edit".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "View".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Insert".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Format".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Tools".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Help".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Undo".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Redo".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Cut".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Copy".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Paste".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Select All".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXMenuItem".into(), text: "Find and Replace".into(), depth: 1, bounds: None },
+ AccessibilityTreeNode { role: "AXStaticText".into(), text: "Untitled".into(), depth: 0, bounds: None },
+ ]);
+ // >70% chrome text
+ assert!(a11y_content_is_thin(&snap, Some("Untitled document")));
+ }
+
+ #[test]
+ fn test_thin_very_little_text() {
+ let snap = make_snap(vec![
+ AccessibilityTreeNode { role: "AXStaticText".into(), text: "Loading...".into(), depth: 0, bounds: None },
+ ]);
+ // < 100 chars total
+ assert!(a11y_content_is_thin(&snap, Some("Some App")));
+ }
+
+ #[test]
+ fn test_not_thin_vscode() {
+ // VS Code: lots of AXStaticText from editor content
+ let snap = make_snap(vec![
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Explorer".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXButton".into(), text: "Search".into(), depth: 0, bounds: None },
+ AccessibilityTreeNode { role: "AXStaticText".into(), text: "fn main() { println!(\"hello world\"); } // This is a Rust program with many lines of code that form a substantial amount of content text in the editor buffer area".into(), depth: 1, bounds: None },
+ ]);
+ assert!(!a11y_content_is_thin(&snap, Some("main.rs - Visual Studio Code")));
+ }
}