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(owned-browser): survive tray-only mode

Louis Beaumont28869d02cc56
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index e5d3a54f4..4fb27edf0 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -9726,7 +9726,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.4.105"
+version = "2.4.106"
dependencies = [
"anyhow",
"arboard",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index b5253eb1c..88a10d9fa 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.4.106"
+version = "2.4.107"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index 47942ff97..366eeb45b 100755
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -1672,24 +1672,17 @@ async fn main() {
// Webview build is async — kick it off in the background and
// attach the handle once the WebviewWindow is ready. Until
// then, /connections/browsers/owned-default/eval returns 503.
+ //
+ // `spawn_install_when_ready` survives tray-only mode by
+ // listening for `window-focused` events instead of giving
+ // up after a fixed budget.
let owned_browser =
screenpipe_connect::connections::browser::OwnedBrowser::default_instance();
- {
- let owned_for_install = owned_browser.clone();
- let app_for_install = app_for_owned.clone();
- let data_dir_for_install = config.data_dir.clone();
- tauri::async_runtime::spawn(async move {
- match crate::owned_browser::install_with_retry(&app_for_install, data_dir_for_install).await {
- Ok(handle) => {
- owned_for_install.attach(handle).await;
- info!("owned-browser ready");
- }
- Err(e) => {
- warn!("owned-browser install failed: {e} — agent will see ready=false");
- }
- }
- });
- }
+ crate::owned_browser::spawn_install_when_ready(
+ app_for_owned.clone(),
+ config.data_dir.clone(),
+ owned_browser.clone(),
+ );
// Phase 1: Start server core
let server = match server_core::ServerCore::start(
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs b/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
index 1d09a88dc..f282ee7d8 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
@@ -142,33 +142,111 @@ impl OwnedWebviewHandle for TauriOwnedHandle {
// Install — build the child webview, wire up the result listener, return handle
// ---------------------------------------------------------------------------
-/// Wait for the main window to exist, then call [`install`]. The server
-/// thread can race window creation in `bun tauri dev`, so a single attempt
-/// often fails with "parent window 'main' not found"; retrying for a few
-/// seconds covers that gap without permanently giving up. After the wait
-/// budget the final error is returned and logged at the call site.
-pub async fn install_with_retry(
- app: &AppHandle,
+/// Spawn the owned-browser install on a background task that survives
+/// tray-only mode. The previous fixed 30-attempt × 500ms loop (15s budget)
+/// failed silently when the user had the app running with the main window
+/// closed — by the time the budget expired, the agent permanently saw
+/// `ready=false` until the next recording restart.
+///
+/// New behavior: a fast retry loop covers the normal window-creation race
+/// (`bun tauri dev` server thread vs WebviewWindow creation), then the task
+/// switches into a passive watcher driven by the existing `window-focused`
+/// Tauri event and a 30-second fallback poll. As soon as the main window
+/// appears the install runs and attaches.
+pub fn spawn_install_when_ready(
+ app: AppHandle,
screenpipe_dir: PathBuf,
-) -> Result<Arc<dyn OwnedWebviewHandle>, String> {
- const MAX_ATTEMPTS: u32 = 30;
- const BACKOFF: Duration = Duration::from_millis(500);
- let mut last_err = String::from("not attempted");
- for attempt in 1..=MAX_ATTEMPTS {
- match install(app, screenpipe_dir.clone()).await {
- Ok(handle) => return Ok(handle),
- Err(e) => {
- last_err = e;
- tracing::debug!(
- "owned-browser install attempt {attempt}/{MAX_ATTEMPTS} failed: {last_err}"
+ owned_browser: std::sync::Arc<screenpipe_connect::connections::browser::OwnedBrowser>,
+) {
+ tauri::async_runtime::spawn(async move {
+ // Phase 1 — fast retry for the dev-server race where the main window
+ // is in the process of being created at the same moment recording
+ // starts. Same budget as before (15s) — most installs land here.
+ const FAST_ATTEMPTS: u32 = 30;
+ const FAST_BACKOFF: Duration = Duration::from_millis(500);
+ let mut last_err = String::new();
+ for attempt in 1..=FAST_ATTEMPTS {
+ match install(&app, screenpipe_dir.clone()).await {
+ Ok(handle) => {
+ owned_browser.attach(handle).await;
+ info!("owned-browser ready");
+ return;
+ }
+ Err(e) => {
+ last_err = e;
+ tracing::debug!(
+ "owned-browser install attempt {attempt}/{FAST_ATTEMPTS}: {last_err}"
+ );
+ tokio::time::sleep(FAST_BACKOFF).await;
+ }
+ }
+ }
+
+ // Phase 2 — main window probably closed/hidden (app in tray-only
+ // mode). Don't give up. Listen for the existing `window-focused`
+ // event (emitted by `show_main_window`) and retry on each kick;
+ // poll every 30s as a fallback in case the event is missed.
+ info!(
+ "owned-browser: main window not present after {FAST_ATTEMPTS} attempts \
+ ({last_err}) — switching to event-driven retry"
+ );
+
+ let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(8);
+ let _listener_id = app.listen("window-focused", move |_event| {
+ // Use try_send so we don't backpressure on the listener thread.
+ let _ = tx.try_send(());
+ });
+
+ // Cap total wait at 30 minutes per recording session — beyond that
+ // the user almost certainly isn't going to open the window, and we
+ // don't want a leaked task hanging around forever. The next
+ // recording restart will spawn a fresh attempt.
+ let deadline = std::time::Instant::now() + Duration::from_secs(30 * 60);
+ let mut last_log = std::time::Instant::now();
+ loop {
+ if std::time::Instant::now() >= deadline {
+ warn!(
+ "owned-browser install gave up after 30 minutes of waiting \
+ for the main window — agent will see ready=false until next \
+ recording restart"
);
- tokio::time::sleep(BACKOFF).await;
+ return;
+ }
+ tokio::select! {
+ _ = rx.recv() => {
+ // Window-focused fired — try install on the next tick to
+ // give the WebviewWindow a moment to settle.
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ _ = tokio::time::sleep(Duration::from_secs(30)) => {
+ // Fallback poll. No log on this branch — it fires twice
+ // a minute and would be noise.
+ }
+ }
+ if app.get_webview_window(PARENT_WINDOW_LABEL).is_none() {
+ // Main window still doesn't exist; loop and wait for the
+ // next event/poll.
+ if last_log.elapsed() > Duration::from_secs(300) {
+ tracing::debug!("owned-browser: still waiting for main window");
+ last_log = std::time::Instant::now();
+ }
+ continue;
+ }
+ match install(&app, screenpipe_dir.clone()).await {
+ Ok(handle) => {
+ owned_browser.attach(handle).await;
+ info!("owned-browser ready (after main window appeared)");
+ return;
+ }
+ Err(e) => {
+ // The window was visible in the check above but install
+ // still failed. Could be a closing race; loop and try
+ // again.
+ tracing::debug!("owned-browser delayed install: {e}");
+ }
}
}
- }
- Err(format!(
- "owned-browser install gave up after {MAX_ATTEMPTS} attempts: {last_err}"
- ))
+ });
}
/// Create the owned-browser child webview if it doesn't exist yet, register
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
index 3df259017..1561a4c8b 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
@@ -611,24 +611,15 @@ pub async fn spawn_screenpipe(
// Owned-browser: create the connect-side instance and kick off the
// webview install in the background. The engine starts immediately;
// the handle attaches when the WebviewWindow is ready.
+ //
+ // `spawn_install_when_ready` survives tray-only mode by listening for
+ // `window-focused` events instead of giving up after a fixed budget.
let owned_browser = screenpipe_connect::connections::browser::OwnedBrowser::default_instance();
- {
- let owned_for_install = owned_browser.clone();
- let data_dir_for_install = recording_config.data_dir.clone();
- tauri::async_runtime::spawn(async move {
- match crate::owned_browser::install_with_retry(&app_for_owned, data_dir_for_install)
- .await
- {
- Ok(handle) => {
- owned_for_install.attach(handle).await;
- info!("owned-browser ready");
- }
- Err(e) => {
- warn!("owned-browser install failed: {e} — agent will see ready=false");
- }
- }
- });
- }
+ crate::owned_browser::spawn_install_when_ready(
+ app_for_owned,
+ recording_config.data_dir.clone(),
+ owned_browser.clone(),
+ );
let on_pipe_output: Option<screenpipe_core::pipes::OnPipeOutputLine> = Some(
std::sync::Arc::new(move |pipe_name: &str, exec_id: i64, line: &str| {
let inner = if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(line) {