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,perf): kill rAF poll + dedupe addChildWindow — was driving WindowServer + replayd to 100%

Louis Beaumontf4b90b5c8221
diff --git a/apps/screenpipe-app-tauri/components/browser-sidebar.tsx b/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
index c87d0f7ec..7655d5125 100644
--- a/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
+++ b/apps/screenpipe-app-tauri/components/browser-sidebar.tsx
@@ -265,40 +265,68 @@ export function BrowserSidebar({ conversationId }: BrowserSidebarProps) {
const el = placeholderRef.current;
if (!el) return;
pushBounds();
- const ro = new ResizeObserver(() => pushBounds());
- ro.observe(el);
- const w = getCurrentWindow();
- const unlistenMovedP = w.listen("tauri://move", () => pushBounds());
- const unlistenResizedP = w.listen("tauri://resize", () => pushBounds());
-
- // ResizeObserver only fires on *size* changes — position changes (e.g.
- // a sibling's flex-basis growing, the chat history sidebar collapsing,
- // a JSX restructure landing via HMR) leave the native webview stuck at
- // stale screen coords. Poll the rect each animation frame and re-push
- // only when something actually moved. Cheap (single getBoundingClientRect
- // per RAF) and bulletproof against any layout-tree change we don't
- // explicitly subscribe to.
+ // Burst-mode rect poll. ResizeObserver only fires on *size* changes —
+ // position changes (sibling flex-basis growing, chat-history sidebar
+ // collapsing, JSX restructure via HMR) leave the native webview at
+ // stale screen coords. So we still poll via rAF, but only for a short
+ // window after a known trigger (mount, ResizeObserver fire, window
+ // move/resize). When the rect is stable for BURST_MS, the loop stops
+ // and idle CPU drops to zero. Previously this rAF ran forever, which
+ // pinned tauri://localhost at ~80% and cascaded into WindowServer +
+ // replayd via the per-frame bind/setBounds path.
+ const BURST_MS = 500;
let raf = 0;
- let last = { x: 0, y: 0, w: 0, h: 0 };
+ let last = el.getBoundingClientRect();
+ let lastChangeAt = performance.now();
+
const tick = () => {
const r = el.getBoundingClientRect();
if (
- r.left !== last.x ||
- r.top !== last.y ||
- r.width !== last.w ||
- r.height !== last.h
+ r.left !== last.left ||
+ r.top !== last.top ||
+ r.width !== last.width ||
+ r.height !== last.height
) {
- last = { x: r.left, y: r.top, w: r.width, h: r.height };
+ last = r;
+ lastChangeAt = performance.now();
pushBounds();
}
- raf = requestAnimationFrame(tick);
+ if (performance.now() - lastChangeAt < BURST_MS) {
+ raf = requestAnimationFrame(tick);
+ } else {
+ raf = 0;
+ }
};
- raf = requestAnimationFrame(tick);
+
+ const scheduleBurst = () => {
+ lastChangeAt = performance.now();
+ if (!raf) {
+ last = el.getBoundingClientRect();
+ raf = requestAnimationFrame(tick);
+ }
+ };
+
+ const ro = new ResizeObserver(scheduleBurst);
+ ro.observe(el);
+ // Also observe the panel's flex parent — a sibling's flex-basis change
+ // (chat history sidebar collapse, app sidebar toggle) shifts our
+ // position without changing our own size, but the host's content
+ // dimensions do change.
+ const host = panelRef.current?.parentElement;
+ if (host) ro.observe(host);
+
+ const w = getCurrentWindow();
+ const unlistenMovedP = w.listen("tauri://move", scheduleBurst);
+ const unlistenResizedP = w.listen("tauri://resize", scheduleBurst);
+
+ // Initial burst covers the slide-in animation (~200ms) and any
+ // post-mount layout settling.
+ scheduleBurst();
return () => {
ro.disconnect();
- cancelAnimationFrame(raf);
+ if (raf) cancelAnimationFrame(raf);
unlistenMovedP.then((fn) => fn()).catch(() => {});
unlistenResizedP.then((fn) => fn()).catch(() => {});
};
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 1ab2640b7..8a10c7a85 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/owned_browser.rs
@@ -46,6 +46,8 @@ use async_trait::async_trait;
use screenpipe_connect::connections::browser::{EvalResult, OwnedWebviewHandle};
use std::path::PathBuf;
use std::sync::Arc;
+#[cfg(target_os = "macos")]
+use std::sync::OnceLock;
use std::time::{Duration, Instant};
use tauri::{
AppHandle, Emitter, LogicalPosition, LogicalSize, Manager, WebviewUrl, WebviewWindow,
@@ -500,10 +502,19 @@ pub async fn owned_browser_set_bounds(
// floating-globally above other apps' windows. Replaces the old
// `always_on_top: true` approach which caused the browser to sit on
// top of MT5 / Claude.ai / etc. when the user switched apps.
- // Idempotent for same-parent calls; switching parents auto-removes
- // from the old one (a window can have at most one parent in Cocoa).
+ // Switching parents auto-removes from the old one (a window can have
+ // at most one parent in Cocoa). Cache the bound label so we only
+ // call addChildWindow on actual parent change — set_bounds runs on
+ // every frame during slide-in / drag-resize, and re-binding 60×/s
+ // wakes WindowServer + replayd into a feedback loop.
#[cfg(target_os = "macos")]
- bind_owned_browser_to_parent(&app, &parent).await?;
+ {
+ let mut current = bound_parent().lock().await;
+ if current.as_deref() != Some(parent.as_str()) {
+ bind_owned_browser_to_parent(&app, &parent).await?;
+ *current = Some(parent.clone());
+ }
+ }
webview_window
.set_position(LogicalPosition::new(screen_x, screen_y))
@@ -514,6 +525,20 @@ pub async fn owned_browser_set_bounds(
webview_window.show().map_err(|e| e.to_string())
}
+/// macOS only: parent label currently bound via `addChildWindow:`. The
+/// owned-browser is a singleton, so a single global slot is sufficient.
+/// Read/written from `owned_browser_set_bounds` to skip redundant
+/// bind calls — `addChildWindow:` is documented idempotent but each
+/// call still walks NSWindow's child list and fires
+/// runningboard/WindowServer notifications, which under per-frame
+/// invocation drives the SCK audio-filter daemon (replayd) into a
+/// restart loop.
+#[cfg(target_os = "macos")]
+fn bound_parent() -> &'static Mutex<Option<String>> {
+ static BOUND_PARENT: OnceLock<Mutex<Option<String>>> = OnceLock::new();
+ BOUND_PARENT.get_or_init(|| Mutex::new(None))
+}
+
/// macOS only: make the owned-browser a child of the named host window
/// via `[NSWindow addChildWindow:ordered:NSWindowAbove]`. Once bound,
/// the OS propagates parent visibility (orderOut / miniaturize / app