From 06c75af46a3d7775fd3961eb0592f9e23a6155e7 Mon Sep 17 00:00:00 2001 From: leeguooooo Date: Wed, 10 Jun 2026 00:42:16 +0900 Subject: [PATCH] fix(connect): passively-discovered tabs no longer steal the active tab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After connect+grouping worked, follow-up eval/get-title/screenshot drifted to a foreign tab: on a shared browser, Target.targetCreated events for tabs the user or OTHER agent sessions open stream in and are drained on every command. The drain path routed them through add_page(), which sets active_page_index to the new page — so the session's active tab silently jumped to a foreign tab and its commands landed there. Add BrowserManager::add_background_page() (push without touching active, dedup by target_id) and use it in the event-drain path. Explicit opens (tab new, the add-and-switch paths) keep using add_page() and still focus the new tab. Closes the last gap in concurrent multi-agent: each session now drives its OWN tab regardless of other sessions'/the user's tab activity. --- cli/src/native/actions.rs | 5 ++++- cli/src/native/browser.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cli/src/native/actions.rs b/cli/src/native/actions.rs index 00b046d..3d50fdf 100644 --- a/cli/src/native/actions.rs +++ b/cli/src/native/actions.rs @@ -672,7 +672,10 @@ impl DaemonState { } let tab_id = mgr.assign_tab_id(); - mgr.add_page(super::browser::PageInfo { + // Passively discovered (event-driven) — must NOT steal the + // active tab, or a foreign/user/other-session tab opening + // hijacks this session's eval/screenshot target. + mgr.add_background_page(super::browser::PageInfo { tab_id, label: None, target_id: te.target_info.target_id.clone(), diff --git a/cli/src/native/browser.rs b/cli/src/native/browser.rs index 6cfc72b..b27c90a 100644 --- a/cli/src/native/browser.rs +++ b/cli/src/native/browser.rs @@ -1532,6 +1532,21 @@ impl BrowserManager { self.active_page_index = index; } + /// Add a passively-discovered page WITHOUT changing the active tab. + /// + /// On a shared browser (ab-connect), `Target.targetCreated` events stream in + /// for tabs the user or OTHER agent sessions open. Those are drained on every + /// command; routing them through `add_page` made the active tab silently jump + /// to a foreign tab, so the session's own `eval`/`get title`/`screenshot` + /// landed on the wrong page. Passively-tracked pages must not steal focus — + /// only explicit opens (`tab new`, switch) set the active tab. + pub fn add_background_page(&mut self, page: PageInfo) { + if self.pages.iter().any(|p| p.target_id == page.target_id) { + return; + } + self.pages.push(page); + } + pub fn update_page_target_info(&mut self, target: &TargetInfo) -> bool { update_page_target_info_in_pages(&mut self.pages, target) }