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
fix(e2e): stop a local suite run stealing the developer's Space
Louis Beaumontaa5059cd5988
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/commands.rs b/apps/screenpipe-app-tauri/src-tauri/src/commands.rsindex 3721670b2..ec5df7d15 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/commands.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/commands.rs@@ -13,6 +13,7 @@ use crate::{};#[cfg(target_os = "macos")]use crate::window::GatedPanelPlacement;+use crate::window::GatedWindowPlacement;use sha2::{Digest, Sha256};use tauri::{Emitter, Manager};#[cfg(not(target_os = "macos"))]@@ -1567,7 +1568,7 @@ pub async fn open_pipe_window(.title(title.clone()).inner_size(1200.0, 850.0).min_inner_size(600.0, 400.0)- .focused(true)+ .focused_gated(true).fullscreen(false);#[cfg(target_os = "macos")]@@ -1606,9 +1607,7 @@ pub async fn open_pipe_window(});// Only try to manipulate window if creation succeeded- if let Err(e) = window.set_focus() {- error!("failed to set window focus: {}", e);- }+ crate::window::focus_window(&window);if let Err(e) = window.show() {error!("failed to show window: {}", e);}@@ -1896,7 +1895,7 @@ pub async fn open_login_window().title("sign in to screenpipe").inner_size(460.0, 700.0)- .focused(true);+ .focused_gated(true);if fresh_session {let profile_dir = std::env::temp_dir().join(&label);@@ -1971,7 +1970,7 @@ pub async fn open_google_calendar_auth_window(// If already open, just focus itif let Some(w) = app_handle.get_webview_window(label) {let _ = w.show();- let _ = w.set_focus();+ crate::window::focus_window(&w);return Ok(());}@@ -1982,7 +1981,7 @@ pub async fn open_google_calendar_auth_window(WebviewWindowBuilder::new(&app_handle, label, WebviewUrl::External(parsed_url)).title("connect google calendar").inner_size(500.0, 700.0)- .focused(true);+ .focused_gated(true);#[cfg(target_os = "macos")]{@@ -2065,14 +2064,7 @@ pub async fn show_window_activated(#[cfg(target_os = "macos")]{app_handle- .run_on_main_thread(|| {- use objc::{msg_send, sel, sel_impl};- use tauri_nspanel::cocoa::base::id;- unsafe {- let ns_app: id = msg_send![objc::class!(NSApplication), sharedApplication];- let _: () = msg_send![ns_app, activateIgnoringOtherApps: true];- }- })+ .run_on_main_thread(crate::window::activate_app_if_allowed).map_err(|e| format!("failed to activate app: {}", e))?;}show_window(app_handle, window).await@@ -2157,7 +2149,7 @@ pub async fn set_window_always_on_top_native(.lock().unwrap_or_else(|e| e.into_inner()).transition(&label_for_main_thread, current_level, always_on_top);- let _: () = unsafe { msg_send![ns_window, setLevel: target_level] };+ unsafe { crate::window::ns::set_level_gated(ns_window as _, target_level) };info!(window_label = %label_for_main_thread,current_level,@@ -3154,11 +3146,11 @@ pub(crate) async fn show_shortcut_reminder_impl(.title("").inner_size(window_width, window_height).position(x, y)- .visible_on_all_workspaces(true)- .always_on_top(true)+ .visible_on_all_workspaces_gated(true)+ .always_on_top_gated(true).decorations(false).skip_taskbar(true)- .focused(false)+ .focused_gated(false).transparent(true).visible(false).shadow(false)@@ -3425,7 +3417,7 @@ pub async fn show_notification_inbox(app_handle: tauri::AppHandle) -> Result<(),}let _ = window.set_position(tauri::Position::Logical(tauri::LogicalPosition::new(x, y)));let _ = window.show();- let _ = window.set_focus();+ crate::window::focus_window(&window);return Ok(());}@@ -3437,14 +3429,14 @@ pub async fn show_notification_inbox(app_handle: tauri::AppHandle) -> Result<(),.title("").inner_size(window_width, window_height).position(x, y)- .visible_on_all_workspaces(true)- .always_on_top(true)+ .visible_on_all_workspaces_gated(true)+ .always_on_top_gated(true).decorations(false).skip_taskbar(true).transparent(true).shadow(false).resizable(false)- .focused(true)+ .focused_gated(true).build().map(crate::window::finalize_webview_window).map_err(|e| {@@ -3633,11 +3625,11 @@ pub async fn show_notification_panel(.title("").inner_size(window_width, window_height).position(x, y)- .visible_on_all_workspaces(true)- .always_on_top(true)+ .visible_on_all_workspaces_gated(true)+ .always_on_top_gated(true).decorations(false).skip_taskbar(true)- .focused(false)+ .focused_gated(false).transparent(true).visible(false).shadow(false)diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rsindex 0dcd299ed..4034f80b1 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs@@ -1024,6 +1024,11 @@ async fn main() {#[cfg(all(target_os = "macos", feature = "e2e"))]if !crate::window::window_activation_allowed() {let _ = app_handle.set_activation_policy(tauri::ActivationPolicy::Accessory);+ // Accessory alone is not enough: tao activates the app itself in+ // `applicationDidFinishLaunching`, after this setup hook has already+ // run, and any ungated path can activate it again mid-run. Hand the+ // foreground straight back whenever that happens.+ crate::window::install_non_activating_guard();}// Create macOS app menu with Settingsdiff --git a/apps/screenpipe-app-tauri/src-tauri/src/viewer.rs b/apps/screenpipe-app-tauri/src-tauri/src/viewer.rsindex 65f6fb407..b615e355f 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/viewer.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/viewer.rs@@ -17,6 +17,8 @@ use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder};use tokio::io::AsyncReadExt;use tracing::{error, info};+use crate::window::GatedWindowPlacement;+const VIEWER_LABEL_PREFIX: &str = "viewer-";const MAX_VIEWER_FILE_BYTES: u64 = 10 * 1024 * 1024;@@ -149,7 +151,7 @@ pub async fn open_viewer_window(app: AppHandle, path: String) -> Result<(), Striif let Some(window) = app.get_webview_window(&label) {let _ = window.show();- let _ = window.set_focus();+ crate::window::focus_window(&window);return Ok(());}@@ -161,7 +163,7 @@ pub async fn open_viewer_window(app: AppHandle, path: String) -> Result<(), Stri.min_inner_size(400.0, 300.0).accept_first_mouse(true).resizable(true)- .focused(true);+ .focused_gated(true);#[cfg(target_os = "macos")]let builder = builder.hidden_title(true);diff --git a/apps/screenpipe-app-tauri/src-tauri/src/window/mod.rs b/apps/screenpipe-app-tauri/src-tauri/src/window/mod.rsindex f61545d42..b4d66e5f7 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/window/mod.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/window/mod.rs@@ -39,6 +39,9 @@ static E2E_ACTIVATION_ALLOWED: std::sync::atomic::AtomicBool =/// `e2e_set_activation_allowed` command, or a whole run can opt in with/// `SCREENPIPE_E2E_ALLOW_ACTIVATION=1`.///+/// CI is exempt: there is no developer to interrupt on a runner, and the suite+/// there should exercise the same window placement users get.+////// Always true in shipping builds — this never changes what users see.#[cfg(feature = "e2e")]pub fn window_activation_allowed() -> bool {@@ -46,11 +49,12 @@ pub fn window_activation_allowed() -> bool {use std::sync::Once;static INIT: Once = Once::new();INIT.call_once(|| {- let from_env = matches!(+ let opted_in = matches!(std::env::var("SCREENPIPE_E2E_ALLOW_ACTIVATION").as_deref(),Ok("1") | Ok("true"));- E2E_ACTIVATION_ALLOWED.store(from_env, Ordering::SeqCst);+ let on_ci = std::env::var("CI").is_ok_and(|v| !v.is_empty() && v != "0" && v != "false");+ E2E_ACTIVATION_ALLOWED.store(opted_in || on_ci, Ordering::SeqCst);});E2E_ACTIVATION_ALLOWED.load(Ordering::SeqCst)}@@ -70,12 +74,8 @@ pub const fn window_activation_allowed() -> bool {/// `set_focus()`, unless an e2e run has activation suppressed.///-/// Windows/Linux only: the macOS show paths raise panels and call-/// `activateIgnoringOtherApps` instead, and those are gated at their own-/// callsites. Commands whose entire contract is activation-/// (`show_window_activated`) still call `set_focus` directly, so the regression-/// guard for them exercises the real thing.-#[cfg(not(target_os = "macos"))]+/// On macOS `set_focus` is what drags the developer's Space along with it, so+/// every show path routes through here rather than calling it directly.pub fn focus_window(window: &tauri::WebviewWindow) {if !window_activation_allowed() {return;@@ -139,6 +139,164 @@ pub(crate) fn make_panel_key_if_allowed(panel: &tauri_nspanel::raw_nspanel::RawN}}+/// `[NSApp activateIgnoringOtherApps:YES]`, unless an e2e run is non-activating.+///+/// This is the single call that yanks a developer out of their fullscreen Space,+/// so it exists once instead of at each of its callsites.+#[cfg(target_os = "macos")]+pub(crate) fn activate_app_if_allowed() {+ if !window_activation_allowed() {+ return;+ }+ use objc::{msg_send, sel, sel_impl};+ use tauri_nspanel::cocoa::base::id;+ unsafe {+ let ns_app: id = msg_send![objc::class!(NSApplication), sharedApplication];+ let _: () = msg_send![ns_app, activateIgnoringOtherApps: true];+ }+}++/// Give the foreground back the instant anything makes the app active.+///+/// The per-callsite gates above stop screenpipe *asking* for the foreground, but+/// they cannot cover everything: tao calls `activateIgnoringOtherApps` itself+/// from `applicationDidFinishLaunching`, which runs after Tauri's setup hook, and+/// AppKit activates an app for reasons of its own. One observer on+/// `NSApplicationDidBecomeActiveNotification` is the backstop — whatever wins the+/// race, the developer gets their app back on the next runloop pass.+///+/// `deactivate` is deferred rather than called inline because AppKit ignores it+/// while it is still delivering the activation it would undo.+#[cfg(all(target_os = "macos", feature = "e2e"))]+pub fn install_non_activating_guard() {+ use objc::{class, msg_send, sel, sel_impl};+ use std::sync::Once;+ use tauri_nspanel::cocoa::base::{id, nil};+ use tauri_nspanel::cocoa::foundation::NSString;++ static INIT: Once = Once::new();+ INIT.call_once(|| {+ let registered = std::panic::catch_unwind(|| unsafe {+ let center: id = msg_send![class!(NSNotificationCenter), defaultCenter];+ let name = NSString::alloc(nil).init_str("NSApplicationDidBecomeActiveNotification");++ let block = block::ConcreteBlock::new(move |_notification: id| {+ // A spec that asserts activation opts in for its own duration.+ if window_activation_allowed() {+ return;+ }+ let ns_app: id = msg_send![class!(NSApplication), sharedApplication];+ let _: () = msg_send![+ ns_app,+ performSelectorOnMainThread: sel!(deactivate)+ withObject: nil+ waitUntilDone: false+ ];+ });+ let block = block.copy();++ let _: id = msg_send![+ center,+ addObserverForName: name+ object: nil+ queue: nil+ usingBlock: &*block+ ];+ });++ // No success log: this runs before the tracing subscriber is attached+ // later in the same setup hook, so anything emitted here is dropped.+ // eprintln survives, and only the failure is worth the noise.+ if registered.is_err() {+ eprintln!("e2e: failed to install the non-activating guard");+ }+ });+}++/// Placement gates for windows we drive as a raw `NSWindow` rather than through+/// `RawNSPanel` (the Search overlay, the Rewind panel, the permission window).+/// Same contract as [`GatedPanelPlacement`]: normal level and no cross-Space+/// bits when an e2e run is non-intrusive.+#[cfg(target_os = "macos")]+pub(crate) mod ns {+ use super::window_activation_allowed;+ use objc::{msg_send, sel, sel_impl};+ use tauri_nspanel::cocoa::base::id;++ /// CanJoinAllSpaces | MoveToActiveSpace | FullScreenAuxiliary.+ const INTRUSIVE_BEHAVIOR: u64 = (1 << 0) | (1 << 1) | (1 << 8);++ /// # Safety+ /// `ns_win` must be a live `NSWindow` and the caller must be on the main thread.+ pub(crate) unsafe fn set_level_gated(ns_win: id, level: i64) {+ let level = if window_activation_allowed() { level } else { 0 };+ let _: () = msg_send![ns_win, setLevel: level];+ }++ /// # Safety+ /// `ns_win` must be a live `NSWindow` and the caller must be on the main thread.+ pub(crate) unsafe fn set_collection_behavior_gated(ns_win: id, behavior: u64) {+ let behavior = if window_activation_allowed() {+ behavior+ } else {+ behavior & !INTRUSIVE_BEHAVIOR+ };+ let _: () = msg_send![ns_win, setCollectionBehavior: behavior];+ }++ /// # Safety+ /// `ns_win` must be a live `NSWindow` and the caller must be on the main thread.+ pub(crate) unsafe fn make_key_if_allowed(ns_win: id) {+ if window_activation_allowed() {+ let _: () = msg_send![ns_win, makeKeyWindow];+ }+ }++ /// Order the window in either way; only take key focus when allowed.+ ///+ /// # Safety+ /// `ns_win` must be a live `NSWindow` and the caller must be on the main thread.+ pub(crate) unsafe fn make_key_and_order_front_if_allowed(ns_win: id) {+ if window_activation_allowed() {+ let _: () = msg_send![ns_win, makeKeyAndOrderFront: std::ptr::null::<+ objc::runtime::Object,+ >()];+ } else {+ let _: () =+ msg_send![ns_win, orderFront: std::ptr::null::<objc::runtime::Object>()];+ }+ }+}++/// Builder-time placement flags, gated by the same switch.+///+/// `.focused(true)`, `.always_on_top(true)` and+/// `.visible_on_all_workspaces(true)` are applied by the window server at+/// creation, before any of the runtime gates above can run — so a non-intrusive+/// e2e run has to clamp them here or a freshly built window still lands on top+/// of whatever the developer is doing.+pub(crate) trait GatedWindowPlacement {+ fn focused_gated(self, focused: bool) -> Self;+ fn always_on_top_gated(self, on_top: bool) -> Self;+ fn visible_on_all_workspaces_gated(self, visible: bool) -> Self;+}++impl<R: tauri::Runtime, M: tauri::Manager<R>> GatedWindowPlacement+ for tauri::webview::WebviewWindowBuilder<'_, R, M>+{+ fn focused_gated(self, focused: bool) -> Self {+ self.focused(focused && window_activation_allowed())+ }++ fn always_on_top_gated(self, on_top: bool) -> Self {+ self.always_on_top(on_top && window_activation_allowed())+ }++ fn visible_on_all_workspaces_gated(self, visible: bool) -> Self {+ self.visible_on_all_workspaces(visible && window_activation_allowed())+ }+}+/// Finalize a newly created webview window with cross-cutting resilience hooks./// Keep this as the single post-build entrypoint for window creation callsites.pub fn finalize_webview_window(window: tauri::WebviewWindow) -> tauri::WebviewWindow {diff --git a/apps/screenpipe-app-tauri/src-tauri/src/window/show.rs b/apps/screenpipe-app-tauri/src-tauri/src/window/show.rsindex c4f49fb12..1c386761c 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/window/show.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/window/show.rs@@ -32,6 +32,7 @@ use super::util::screen_aware_size;use tauri::Emitter;#[cfg(target_os = "macos")]use super::GatedPanelPlacement;+use super::GatedWindowPlacement;#[cfg(target_os = "macos")]use tauri_nspanel::ManagerExt;#[cfg(target_os = "macos")]@@ -580,12 +581,12 @@ impl ShowRewindWindow {let ns_win = ns_win as id;unsafe {begin_search_focus_session();- let _: () = msg_send![ns_win, setLevel: 1002_i64];+ crate::window::ns::set_level_gated(ns_win, 1002_i64);let current: i32 = msg_send![ns_win, styleMask];let _: () = msg_send![ns_win, setStyleMask: current | 128];let _: () = msg_send![ns_win, setBecomesKeyOnlyIfNeeded: true];let _: () = msg_send![ns_win, orderFront: std::ptr::null::<objc::runtime::Object>()];- let _: () = msg_send![ns_win, makeKeyWindow];+ crate::window::ns::make_key_if_allowed(ns_win);make_nswindow_webview_first_responder(ns_win);}}@@ -644,11 +645,15 @@ impl ShowRewindWindow {let behavior: u64 =msg_send![ns_win as cocoa_id, collectionBehavior];let move_to_active: u64 = 1 << 1;- let _: () = msg_send![ns_win as cocoa_id, setCollectionBehavior: behavior | move_to_active];+ crate::window::ns::set_collection_behavior_gated(+ ns_win as cocoa_id,+ behavior | move_to_active,+ );// Bring window to front and make it key- let _: () =- msg_send![ns_win as cocoa_id, makeKeyAndOrderFront: cocoa_nil];+ crate::window::ns::make_key_and_order_front_if_allowed(+ ns_win as cocoa_id,+ );// Set WKWebView as first responder so keyboard input works.// Without this, re-showing an existing Settings window may@@ -781,7 +786,7 @@ impl ShowRewindWindow {.min_inner_size(800.0, 600.0).decorations(true).visible(false)- .focused(false)+ .focused_gated(false).transparent(false);super::finalize_webview_window(builder.build()?)};@@ -804,7 +809,7 @@ impl ShowRewindWindow {.decorations(true).skip_taskbar(true).visible(false)- .focused(false)+ .focused_gated(false).transparent(false).on_page_load(move |win, payload| {if matches!(@@ -1085,10 +1090,10 @@ impl ShowRewindWindow {"/overlay",main_label_for_mode("fullscreen"),)- .always_on_top(true)+ .always_on_top_gated(true).decorations(false).skip_taskbar(true)- .focused(false)+ .focused_gated(false).transparent(true).visible(false).hidden_title(true)@@ -1141,13 +1146,13 @@ impl ShowRewindWindow {main_label_for_mode("fullscreen"),).title("screenpipe")- .visible_on_all_workspaces(true)- .always_on_top(true)+ .visible_on_all_workspaces_gated(true)+ .always_on_top_gated(true).decorations(false).resizable(false).maximizable(false).minimizable(false)- .focused(false)+ .focused_gated(false).transparent(true).visible(false).skip_taskbar(true)@@ -1200,7 +1205,7 @@ impl ShowRewindWindow {.min_inner_size(800.0, 600.0).decorations(true).visible(false)- .focused(false)+ .focused_gated(false).transparent(false).on_page_load(move |win, payload| {if matches!(payload.event(), tauri::webview::PageLoadEvent::Finished) {@@ -1435,7 +1440,7 @@ impl ShowRewindWindow {None => "/home".to_string(),};- let builder = self.window_builder(app, &url).focused(true);+ let builder = self.window_builder(app, &url).focused_gated(true);#[cfg(target_os = "macos")]let builder = {use tauri::window::Color;@@ -1548,12 +1553,12 @@ impl ShowRewindWindow {let clamped_min = (min.0.min(width), min.1.min(height));let builder = self.window_builder(app, "/onboarding")- .visible_on_all_workspaces(true)+ .visible_on_all_workspaces_gated(true).min_inner_size(clamped_min.0, clamped_min.1).inner_size(width, height).minimizable(false).maximizable(false)- .focused(true);+ .focused_gated(true);let window = super::finalize_webview_window(builder.build()?);window@@ -1572,9 +1577,9 @@ impl ShowRewindWindow {.window_builder(app, "/chat").inner_size(650.0, 800.0).min_inner_size(500.0, 600.0)- .focused(false)+ .focused_gated(false).visible(false)- .always_on_top(chat_always_on_top)+ .always_on_top_gated(chat_always_on_top).hidden_title(true);let window = super::finalize_webview_window(builder.build()?);@@ -1649,8 +1654,8 @@ impl ShowRewindWindow {.window_builder(app, "/chat").inner_size(650.0, 800.0).min_inner_size(500.0, 600.0)- .focused(true)- .always_on_top(chat_always_on_top);+ .focused_gated(true)+ .always_on_top_gated(chat_always_on_top);super::finalize_webview_window(builder.build()?)};@@ -1662,8 +1667,8 @@ impl ShowRewindWindow {.inner_size(420.0, 420.0).min_inner_size(400.0, 350.0).resizable(false)- .focused(true)- .always_on_top(true)+ .focused_gated(true)+ .always_on_top_gated(true).center();#[cfg(target_os = "macos")]let builder = builder.hidden_title(true);@@ -1750,12 +1755,12 @@ impl ShowRewindWindow {.shadow(true).decorations(false).transparent(true)- .always_on_top(true)- .visible_on_all_workspaces(true)+ .always_on_top_gated(true)+ .visible_on_all_workspaces_gated(true).inner_size(bar_w, bar_h).min_inner_size(SEARCH_BAR_MIN_W, 56.0).position(x, y)- .focused(focus)+ .focused_gated(focus).resizable(true);let window = super::finalize_webview_window(builder.build()?);@@ -1782,7 +1787,7 @@ impl ShowRewindWindow {object_setClass(ns_win, nspanel_class);// Level 1002 — above fullscreen (CGShieldingWindowLevel+2)- let _: () = msg_send![ns_win, setLevel: 1002_i64];+ crate::window::ns::set_level_gated(ns_win, 1002_i64);// NSNonactivatingPanelMask (128) — appear over fullscreen// without triggering a Space switch@@ -1790,7 +1795,7 @@ impl ShowRewindWindow {let _: () = msg_send![ns_win, setStyleMask: current | 128];// CanJoinAllSpaces (1) + FullScreenAuxiliary (256)- let _: () = msg_send![ns_win, setCollectionBehavior: 257_u64];+ crate::window::ns::set_collection_behavior_gated(ns_win, 257_u64);let _: () = msg_send![ns_win, setHidesOnDeactivate: false];let _: () = msg_send![ns_win, setBecomesKeyOnlyIfNeeded: true];@@ -1800,7 +1805,7 @@ impl ShowRewindWindow {if focus {begin_search_focus_session();let _: () = msg_send![ns_win, orderFront: nil];- let _: () = msg_send![ns_win, makeKeyWindow];+ crate::window::ns::make_key_if_allowed(ns_win);make_nswindow_webview_first_responder(ns_win);}}