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
Preload CDP browser for Rust initial navigation
Magnus Müllerdef63f1ab3e5
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 043311068..6fa0790c0 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -41,6 +41,11 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly importempty browser tool results while a script is still active and keeps thenext model turn on the intended observe/cancel path instead of repeatedlynavigating or reconnecting.+ - Simple initial navigation actions on existing CDP-backed browser sessions+ now execute once before the Rust SDK run starts. Evals that provision a+ Browser Use Cloud browser therefore hand Rust a browser already focused on+ the task's start URL instead of relying only on a prompt hint from+ `about:blank`.## Current Proof@@ -53,6 +58,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- terminal `cargo test -p browser-use-cli sdk_provider_run_config_maps_browser_use_options_to_rust_core -- --nocapture`- browser-use `uv run python -m py_compile browser_use/rust/service.py`- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_history_surfaces_running_browser_script_observe_instruction -q`+- browser-use `uv run pytest tests/ci/test_rust_agent.py -k 'run_executes_initial_actions_before_sdk or initial_actions_pre_navigate_existing_cdp_session' -q`- browser-use `uv run pytest tests/ci/test_rust_agent.py`- browser-use `uv run pytest tests/ci/test_rust_agent.py -k 'sdk_client_reads_large_json_rpc_lines or sdk_and_reuses_session or translates_browser_use_args_to_terminal'`- browser-use `uv run pytest tests/ci/test_rust_agent.py -k 'sdk_client_queues_agent_notifications_before_response or sdk_client_reads_large_json_rpc_lines or sdk_and_reuses_session or translates_browser_use_args_to_terminal'`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 4fb0b8ba4..ca8ece902 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -1090,13 +1090,19 @@ def _llm_terminal_command(llm: Any, *, existing_session: bool = False) -> str:def _navigation_url_from_action(action: Any) -> str | None:+ params = _initial_navigation_params_from_action(action)+ return params[0] if params is not None else None+++def _initial_navigation_params_from_action(action: Any) -> tuple[str, bool] | None:if not isinstance(action, dict):return Nonefor name, payload in action.items():if name in ('open_tab', 'go_to_url', 'navigate') and isinstance(payload, dict):url = payload.get('url')if isinstance(url, str) and url:- return url+ new_tab = name == 'open_tab' or bool(payload.get('new_tab'))+ return url, new_tabreturn None@@ -4052,6 +4058,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.initial_actions = (self._convert_initial_actions(self.initial_action_payloads) if self.initial_action_payloads else None)+ self._initial_actions_executed = False+ self._pending_history_prefix: list[AgentHistory] = []self.task = _task_with_schema(_task_with_available_files(_task_with_sensitive_data_context(@@ -4678,6 +4686,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):await self._finalize_run_cleanup()return self.history+ prefix_start = len(self.history.history)+ await self._execute_initial_actions(allow_terminal_run=False)+ if len(self.history.history) > prefix_start:+ self._pending_history_prefix = list(self.history.history[prefix_start:])await self._call_callback(on_step_start, self)self._log_main_execution_start(max_steps)followup = bool(self.state.follow_up_task and self._sdk_agent_id)@@ -4830,6 +4842,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):output_model_schema=self.output_model_schema,process_error=process_error,)+ if self._pending_history_prefix:+ self.result.history = [*self._pending_history_prefix, *self.result.history]+ self._pending_history_prefix = []self.history = self.resultawait self._apply_terminal_usage_costs(self.last_events)self._sync_state_from_history()@@ -5482,12 +5497,18 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.task = original_taskreturn history.action_results()- async def _execute_initial_actions(self) -> None:+ async def _execute_initial_actions(self, *, allow_terminal_run: bool = True) -> None:"""Execute configured Browser Use initial actions through the Rust-backed action path."""- if not self.initial_actions or self.state.follow_up_task:+ if not self.initial_actions or self.state.follow_up_task or self._initial_actions_executed:returnself.logger.debug(f'⚡ Executing {len(self.initial_actions)} initial actions...')- result = await self.multi_act(self.initial_actions)+ result = await self._execute_direct_initial_navigation_actions()+ if result is None:+ if not allow_terminal_run:+ self.logger.debug('Initial actions left for Rust task context; direct CDP navigation was not available')+ return+ result = await self.multi_act(self.initial_actions)+ self._initial_actions_executed = Trueif result and self.initial_url and result[0].long_term_memory:result[0].long_term_memory = f'Found initial url and automatically loaded it. {result[0].long_term_memory}'self.state.last_result = result@@ -5530,6 +5551,32 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.logger.debug('📝 Saved initial actions to history as step 0')self.logger.debug('Initial actions completed')+ async def _execute_direct_initial_navigation_actions(self) -> list[ActionResult] | None:+ """Pre-navigate existing CDP-backed browser sessions before the Rust agent starts."""+ if self.browser_session is None:+ return None+ if not (_extract_cdp_url(self.browser_session) or _extract_profile_cdp_url(self.browser_profile)):+ return None+ navigate_to = getattr(self.browser_session, 'navigate_to', None)+ if not callable(navigate_to):+ return None+ payloads = [_action_payload(action) for action in self.initial_actions or []]+ nav_actions = [_initial_navigation_params_from_action(payload) for payload in payloads]+ if not nav_actions or any(action is None for action in nav_actions):+ return None+ results: list[ActionResult] = []+ for url, new_tab in nav_actions:+ try:+ await navigate_to(url, new_tab=new_tab)+ except Exception as exc:+ message = f'Initial navigation to {url} failed: {exc}'+ self.logger.warning(message)+ results.append(ActionResult(error=message))+ continue+ text = f'Navigated to {url}'+ results.append(ActionResult(extracted_content=text, long_term_memory=text))+ return results+async def _execute_history_step(self, history_item: AgentHistory, delay: float) -> list[ActionResult]:"""Replay a Browser Use history step when Python action models are available."""if self.browser_session is None:diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 3808b2012..6c6227aaf 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -7018,6 +7018,40 @@ async def test_rust_agent_take_step_executes_initial_actions_on_first_step():assert is_valid is True+async def test_rust_agent_run_executes_initial_actions_before_sdk():+ from browser_use.rust import Agent+ from browser_use.rust.service import _history_from_events++ agent = Agent(+ task='start on example',+ initial_actions=[{'navigate': {'url': 'https://example.com', 'new_tab': False}}],+ )+ seen = []++ async def fake_execute_initial_actions(*, allow_terminal_run=True):+ seen.append(('initial_actions', allow_terminal_run))++ async def fake_run_sdk_agent(**kwargs):+ seen.append(('sdk', kwargs['max_steps']))+ return _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'done'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ agent._execute_initial_actions = fake_execute_initial_actions+ agent._run_sdk_agent = fake_run_sdk_agent+ agent._initialize_run_lifecycle_state = lambda: None++ history = await agent.run(max_steps=3)++ assert seen == [('initial_actions', False), ('sdk', 3)]+ assert history.final_result() == 'done'++async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monkeypatch):from browser_use.rust import Agent@@ -7190,6 +7224,38 @@ async def test_rust_agent_multi_act_ignores_later_done_actions():assert results[0].extracted_content == 'clicked only'+async def test_rust_agent_initial_actions_pre_navigate_existing_cdp_session():+ from types import SimpleNamespace++ from browser_use.rust import Agent++ class FakeBrowserSession:+ id = 'browser-cdp'+ cdp_url = 'wss://cloud-browser.example/devtools/browser/session'+ browser_profile = SimpleNamespace(cdp_url=cdp_url)++ def __init__(self):+ self.calls = []++ async def navigate_to(self, url, new_tab=False):+ self.calls.append((url, new_tab))++ browser_session = FakeBrowserSession()+ agent = Agent(+ task='read the page',+ browser_session=browser_session,+ initial_actions=[{'navigate': {'url': 'https://example.com', 'new_tab': False}}],+ )++ await agent._execute_initial_actions(allow_terminal_run=False)++ assert browser_session.calls == [('https://example.com', False)]+ assert agent._initial_actions_executed is True+ assert agent.state.last_result[0].extracted_content == 'Navigated to https://example.com'+ assert agent.history.history[0].metadata.step_number == 0+ assert agent.history.history[0].model_output.action == agent.initial_actions++async def test_rust_agent_exposes_action_replay_helper_methods(monkeypatch):from types import SimpleNamespace