Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: tab close uses caller's logical focus, not Chrome's global focus

ShawnPanaa6744f4ade9e
diff --git a/browser_use/skill_cli/commands/browser.py b/browser_use/skill_cli/commands/browser.py
index 944a51291..343f99bfa 100644
--- a/browser_use/skill_cli/commands/browser.py
+++ b/browser_use/skill_cli/commands/browser.py
@@ -203,7 +203,10 @@ async def handle(action: str, session: SessionInfo, params: dict[str, Any]) -> A
return {'error': f'Invalid tab index {tab_index}. Available: 0-{len(page_targets) - 1}'}
target_id = page_targets[tab_index].target_id
else:
- target_id = bs.session_manager.get_focused_target().target_id if bs.session_manager else None
+ # Use caller's logical focus, not Chrome's global focus
+ target_id = bs.agent_focus_target_id
+ if not target_id:
+ target_id = bs.session_manager.get_focused_target().target_id if bs.session_manager else None
if not target_id:
return {'error': 'No focused tab to close'}
cdp_session = await bs.get_or_create_cdp_session(target_id=None, focus=False)
@@ -256,7 +259,10 @@ async def handle(action: str, session: SessionInfo, params: dict[str, Any]) -> A
await cdp_session.cdp_client.send.Target.closeTarget(params={'targetId': tid})
if not tab_indices:
- target_id = bs.session_manager.get_focused_target().target_id if bs.session_manager else None
+ # Use caller's logical focus, not Chrome's global focus
+ target_id = bs.agent_focus_target_id
+ if not target_id:
+ target_id = bs.session_manager.get_focused_target().target_id if bs.session_manager else None
if not target_id:
return {'error': 'No focused tab to close'}
await _close_target(target_id)
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.py
index 79bf180d0..826bd267e 100755
--- a/browser_use/skill_cli/main.py
+++ b/browser_use/skill_cli/main.py
@@ -558,11 +558,20 @@ def ensure_daemon(
stderr=subprocess.DEVNULL,
)
- # Wait for daemon to be ready
- for _ in range(100): # 5 seconds max
- if _is_daemon_alive(session):
+ # Wait for daemon to be ready — use state file for phase-aware waiting
+ deadline = time.time() + 15
+ while time.time() < deadline:
+ probe = _probe_session(session)
+ if probe.socket_reachable:
return
- time.sleep(0.05)
+ # Daemon wrote state and PID is alive — still booting, keep waiting
+ if probe.pid_alive and probe.phase in ('initializing', 'ready', 'starting', 'running'):
+ time.sleep(0.2)
+ continue
+ # Daemon wrote terminal state — startup failed
+ if probe.phase in ('failed', 'stopped'):
+ break
+ time.sleep(0.2)
print('Error: Failed to start daemon', file=sys.stderr)
sys.exit(1)