Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
merge upstream/main, resolve install_lite.sh conflict (take upstream)
ShawnPana6bb73ab27449
diff --cc browser_use/skill_cli/daemon.pyindex f21217e78,27b521410..37951de35--- a/browser_use/skill_cli/daemon.py+++ b/browser_use/skill_cli/daemon.py@@@ -72,102 -65,54 +72,113 @@@ class Daemonif self._session is not None:return self._session- from browser_use.skill_cli.sessions import SessionInfo, create_browser_session-- logger.info(- f'Creating session (headed={self.headed}, profile={self.profile}, cdp_url={self.cdp_url}, use_cloud={self.use_cloud})'- )-- bs = await create_browser_session(- self.headed,- self.profile,- self.cdp_url,- use_cloud=self.use_cloud,- cloud_timeout=self.cloud_timeout,- cloud_proxy_country_code=self.cloud_proxy_country_code,- cloud_profile_id=self.cloud_profile_id,- )- await bs.start()-- self._session = SessionInfo(- name=self.session,- headed=self.headed,- profile=self.profile,- cdp_url=self.cdp_url,- browser_session=bs,- use_cloud=self.use_cloud,- )- self._browser_watchdog_task = asyncio.create_task(self._watch_browser())- return self._session+ async with self._session_lock:+ # Double-check after acquiring lock+ if self._session is not None:+ return self._session++ from browser_use.skill_cli.sessions import SessionInfo, create_browser_session++ logger.info(+ f'Creating session (headed={self.headed}, profile={self.profile}, cdp_url={self.cdp_url}, use_cloud={self.use_cloud})'+ )++ bs = await create_browser_session(+ self.headed,+ self.profile,+ self.cdp_url,+ use_cloud=self.use_cloud,+ cloud_timeout=self.cloud_timeout,+ cloud_proxy_country_code=self.cloud_proxy_country_code,+ cloud_profile_id=self.cloud_profile_id,+ )+ await bs.start()++ # Wait for Chrome to stabilize after CDP setup before accepting commands+ try:+ await bs.get_browser_state_summary()+ except Exception:+ pass++ # Create action handler for direct command execution (no event bus)+ from browser_use.skill_cli.actions import ActionHandler++ actions = ActionHandler(bs)++ self._session = SessionInfo(+ name=self.session,+ headed=self.headed,+ profile=self.profile,+ cdp_url=self.cdp_url,+ browser_session=bs,+ actions=actions,+ use_cloud=self.use_cloud,+ )+ self._browser_watchdog_task = asyncio.create_task(self._watch_browser())++ # Initialize tab ownership for multi-agent isolation+ from browser_use.skill_cli.tab_ownership import TabOwnershipManager+ from browser_use.skill_cli.utils import get_home_dir++ self._tab_ownership = TabOwnershipManager(bs)+ self._tab_ownership.set_agents_file(get_home_dir() / 'agents.json')++ # Register initial tabs with tab ownership (no event bus)+ if bs.session_manager:+ for target in bs.session_manager.get_all_page_targets():+ self._tab_ownership.on_tab_created(target.target_id)++ # Start periodic agent cleanup+ self._agent_cleanup_task = asyncio.create_task(self._cleanup_stale_agents())++ # Start idle timeout watchdog+ self._idle_watchdog_task = asyncio.create_task(self._watch_idle())++ return self._sessionasync def _watch_browser(self) -> None:- """Poll BrowserSession.is_cdp_connected every 2s. Shutdown when browser dies."""+ """Poll BrowserSession.is_cdp_connected every 2s. Shutdown when browser dies.++ Skips checks while the BrowserSession is reconnecting. If reconnection fails,+ next poll will see is_cdp_connected=False and trigger shutdown.+ """while self.running:await asyncio.sleep(2.0)- if self._session and not self._session.browser_session.is_cdp_connected:+ if not self._session:+ continue+ bs = self._session.browser_session+ # Don't shut down while a reconnection attempt is in progress+ if bs.is_reconnecting:+ continue+ if not bs.is_cdp_connected:logger.info('Browser disconnected, shutting down daemon')- await self.shutdown()+ if not self._shutdown_task or self._shutdown_task.done():+ self._shutdown_task = asyncio.create_task(self.shutdown())return+ async def _watch_idle(self) -> None:+ """Shutdown daemon after idle_timeout seconds of no commands."""+ while self.running:+ await asyncio.sleep(60.0)+ if self._last_command_time > 0:+ import time++ idle = time.monotonic() - self._last_command_time+ if idle >= self._idle_timeout:+ logger.info(f'Daemon idle for {idle:.0f}s, shutting down')+ await self.shutdown()+ return++ async def _cleanup_stale_agents(self) -> None:+ """Periodically clean up contexts for agents whose parent process is dead."""+ while self.running:+ await asyncio.sleep(30.0)+ if self._tab_ownership:+ try:+ await self._tab_ownership.cleanup_stale_agents()+ except Exception as e:+ logger.debug(f'Agent cleanup error: {e}')+async def handle_connection(self,reader: asyncio.StreamReader,