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): retry navigate after install completes — restore browser on app launch

Louis Beaumontf31d437e05b1
diff --git a/apps/screenpipe-app-tauri/components/browser-sidebar.tsx b/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
index 2ee7ce6be..c87d0f7ec 100644
--- a/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
+++ b/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
@@ -207,6 +207,7 @@ export function BrowserSidebar({ conversationId }: BrowserSidebarProps) {
cancelled = true;
};
}
+ let unlistenReady: (() => void) | null = null;
(async () => {
const conv = await loadConversationFile(conversationId).catch(() => null);
if (cancelled) return;
@@ -219,7 +220,24 @@ export function BrowserSidebar({ conversationId }: BrowserSidebarProps) {
setVisible(true);
setCollapsed(wasCollapsed);
setCurrentUrl(url);
- invoke("owned_browser_navigate", { url }).catch(() => {});
+ // The webview install runs on a background task that retries
+ // until the app's Tauri runtime has booted. On cold start a chat
+ // with a saved `browserState.url` opens fast enough that this
+ // navigate() lands before install finishes — Rust returns
+ // "owned-browser not initialized", we swallow it, and the
+ // browser silently fails to restore. Retry once when Rust emits
+ // `owned-browser:ready` so the saved state survives app quit.
+ const tryNavigate = () =>
+ invoke("owned_browser_navigate", { url }).catch((e) => {
+ const msg = typeof e === "string" ? e : String(e);
+ return msg.includes("not initialized") ? "retry" : null;
+ });
+ const first = await tryNavigate();
+ if (!cancelled && first === "retry") {
+ unlistenReady = await listen("owned-browser:ready", () => {
+ tryNavigate();
+ });
+ }
// If collapsed, hide the webview right away — pushBounds wouldn't
// run because the placeholder isn't mounted.
if (wasCollapsed) invoke("owned_browser_hide").catch(() => {});
@@ -232,6 +250,7 @@ export function BrowserSidebar({ conversationId }: BrowserSidebarProps) {
})();
return () => {
cancelled = true;
+ if (unlistenReady) unlistenReady();
};
}, [conversationId]);
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 7e753beb9..5d619133b 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
@@ -63,6 +63,12 @@ pub const WEBVIEW_LABEL: &str = "owned-browser";
/// position the webview, and persist the URL to the active chat.
const NAVIGATE_EVENT: &str = "owned-browser:navigate";
+/// Emitted to the frontend exactly once when `spawn_install_when_ready`
+/// finishes building the top-level webview and attaching the handle to
+/// the registry. Lets `BrowserSidebar` retry a per-conversation
+/// `owned_browser_navigate` that lost the install race on cold start.
+const READY_EVENT: &str = "owned-browser:ready";
+
/// Marker prefix for `document.title`-based result delivery. The bridge JS
/// sets `document.title = "<MARKER>:<json>"`; the Rust eval polls
/// `WebviewWindow::title()` until it sees this prefix and parses the
@@ -326,6 +332,14 @@ pub fn spawn_install_when_ready(
Ok(handle) => {
owned_browser.attach(handle).await;
info!("owned-browser ready");
+ // Notify the frontend so any sidebar that tried to call
+ // `owned_browser_navigate` before install finished can
+ // retry. Without this, opening a chat with a saved
+ // `browserState.url` during the install race silently
+ // dropped the navigate (Rust returns "not initialized",
+ // frontend swallows in `.catch(() => {})`) and the
+ // browser never restored on next app launch.
+ let _ = app.emit(READY_EVENT, ());
return;
}
Err(e) => {