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(vision): query frontmost PID once per capture cycle, fix popover SQL

Louis Beaumont05d3832aec52
diff --git a/apps/screenpipe-app-tauri/components/rewind/timeline/app-context-popover.tsx b/apps/screenpipe-app-tauri/components/rewind/timeline/app-context-popover.tsx
index 2275107f0..784db0036 100644
--- a/apps/screenpipe-app-tauri/components/rewind/timeline/app-context-popover.tsx
+++ b/apps/screenpipe-app-tauri/components/rewind/timeline/app-context-popover.tsx
@@ -72,24 +72,30 @@ export function AppContextPopover({
const escapedApp = appName.replace(/'/g, "''");
const query = `
- SELECT 'summary' as type, COUNT(*) as val, COUNT(DISTINCT window_name) as val2, NULL as name
- FROM frames
- WHERE app_name = '${escapedApp}'
- AND timestamp BETWEEN '${startISO}' AND '${endISO}'
+ SELECT * FROM (
+ SELECT 'summary' as type, COUNT(*) as val, COUNT(DISTINCT window_name) as val2, NULL as name
+ FROM frames
+ WHERE app_name = '${escapedApp}'
+ AND timestamp BETWEEN '${startISO}' AND '${endISO}'
+ )
UNION ALL
- SELECT 'window' as type, COUNT(*) as val, NULL as val2, window_name as name
- FROM frames
- WHERE app_name = '${escapedApp}'
- AND timestamp BETWEEN '${startISO}' AND '${endISO}'
- AND window_name IS NOT NULL AND window_name != ''
- GROUP BY window_name ORDER BY val DESC LIMIT 5
+ SELECT * FROM (
+ SELECT 'window' as type, COUNT(*) as val, NULL as val2, window_name as name
+ FROM frames
+ WHERE app_name = '${escapedApp}'
+ AND timestamp BETWEEN '${startISO}' AND '${endISO}'
+ AND window_name IS NOT NULL AND window_name != ''
+ GROUP BY window_name ORDER BY val DESC LIMIT 5
+ )
UNION ALL
- SELECT 'url' as type, COUNT(*) as val, NULL as val2, browser_url as name
- FROM frames
- WHERE app_name = '${escapedApp}'
- AND timestamp BETWEEN '${startISO}' AND '${endISO}'
- AND browser_url IS NOT NULL AND browser_url != ''
- GROUP BY browser_url ORDER BY val DESC LIMIT 5
+ SELECT * FROM (
+ SELECT 'url' as type, COUNT(*) as val, NULL as val2, browser_url as name
+ FROM frames
+ WHERE app_name = '${escapedApp}'
+ AND timestamp BETWEEN '${startISO}' AND '${endISO}'
+ AND browser_url IS NOT NULL AND browser_url != ''
+ GROUP BY browser_url ORDER BY val DESC LIMIT 5
+ )
`;
fetch("http://localhost:3030/raw_sql", {
diff --git a/crates/screenpipe-vision/Cargo.toml b/crates/screenpipe-vision/Cargo.toml
index 653e0859b..250ba21db 100644
--- a/crates/screenpipe-vision/Cargo.toml
+++ b/crates/screenpipe-vision/Cargo.toml
@@ -93,7 +93,7 @@ path = "examples/websocket.rs"
sck-rs = { git = "https://github.com/screenpipe/sck-rs" }
xcap = "0.8.1"
libc = "0.2.168"
-cidre = { git = "https://github.com/yury/cidre.git", features = ["vn", "cv", "cg"] }
+cidre = { git = "https://github.com/yury/cidre.git", features = ["vn", "cv", "cg", "app"] }
accessibility-sys = { git = "https://github.com/eiz/accessibility.git", branch = "master" }
core-foundation = "0.10"
core-graphics = "0.24"
diff --git a/crates/screenpipe-vision/src/capture_screenshot_by_window.rs b/crates/screenpipe-vision/src/capture_screenshot_by_window.rs
index 56b89e92a..143e37424 100644
--- a/crates/screenpipe-vision/src/capture_screenshot_by_window.rs
+++ b/crates/screenpipe-vision/src/capture_screenshot_by_window.rs
@@ -376,14 +376,40 @@ struct WindowData {
image_buffer: image::RgbaImage,
}
+/// Query the frontmost application PID once, so all windows in a capture cycle
+/// agree on which app is focused. Without this, each window.is_focused() call
+/// independently queries NSWorkspace, and the active app can change mid-iteration
+/// causing multiple apps to appear as "focused" simultaneously.
+#[cfg(target_os = "macos")]
+fn get_frontmost_pid() -> Option<i32> {
+ let workspace = cidre::ns::Workspace::shared();
+ let apps = workspace.running_apps();
+ for app in apps.iter() {
+ if app.is_active() {
+ return Some(app.pid());
+ }
+ }
+ None
+}
+
/// Get all visible windows using the appropriate backend
#[cfg(target_os = "macos")]
fn get_all_windows() -> Result<Vec<WindowData>, Box<dyn Error>> {
- if use_sck_rs() {
- get_all_windows_sck()
+ let mut windows = if use_sck_rs() {
+ get_all_windows_sck()?
} else {
- get_all_windows_xcap()
+ get_all_windows_xcap()?
+ };
+
+ // Fix focus: query frontmost PID once and apply consistently to all windows.
+ // This prevents multiple apps from being marked focused in the same capture cycle.
+ if let Some(frontmost_pid) = get_frontmost_pid() {
+ for window in &mut windows {
+ window.is_focused = window.process_id == frontmost_pid;
+ }
}
+
+ Ok(windows)
}
#[cfg(target_os = "macos")]