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
Keep Rust handoff on preloaded CDP page
Magnus Müllerf4b2e06b3465
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex ff4e704f9..f5d68afd4 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -46,6 +46,10 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly importBrowser Use Cloud browser therefore hand Rust a browser already focused onthe task's start URL instead of relying only on a prompt hint from`about:blank`.+ - Completed initial CDP navigations are passed to the Rust SDK as current-page+ context and the stale `First navigate to ...` prefix is removed from the+ SDK task. This keeps Rust from repeating a navigation that Python already+ confirmed against the Browser Use Cloud browser.- Terminal Rust SDK runs no longer advertise subagent tools unless the runhas a configured child-agent runner. This prevents eval tasks from spendingmodel turns on `spawn_agent` calls that can only fail with "subagents are@@ -67,7 +71,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 -k 'initial_actions_pre_navigate_existing_cdp_session or run_executes_initial_actions_before_sdk or run_hands_off_completed_initial_navigation_as_context' -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 ca8ece902..aeb29b432 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -1127,6 +1127,44 @@ def _task_with_initial_actions(task: str, initial_actions: Any) -> str:return f'Before the task, perform these Browser Use initial actions in order:\n{actions}\n\nThen complete the task.\n\n{task}'+def _task_with_completed_initial_navigation_context(+ task: str, completed_urls: list[str], initial_actions: Any = None+) -> str:+ urls = [url for url in completed_urls if isinstance(url, str) and url]+ if not urls:+ return task+ cleaned_task = task+ if len(urls) == 1:+ prefix = f'First navigate to {urls[0]!r}, then complete the task.\n\n'+ if cleaned_task.startswith(prefix):+ cleaned_task = cleaned_task[len(prefix) :]+ return (+ f'The browser session is already open at {urls[0]!r}. '+ 'Continue from the current page. Do not navigate to that same start URL again unless browser status shows a different URL.'+ f'\n\n{cleaned_task}'+ )+ initial_action_urls: list[str] = []+ if isinstance(initial_actions, list):+ for action in initial_actions:+ url = _navigation_url_from_action(action)+ if url:+ initial_action_urls.append(url)+ if initial_action_urls == urls:+ actions = json.dumps(initial_actions, indent=2, default=str)+ prefix = (+ f'Before the task, perform these Browser Use initial actions in order:\n{actions}\n\n'+ 'Then complete the task.\n\n'+ )+ if cleaned_task.startswith(prefix):+ cleaned_task = cleaned_task[len(prefix) :]+ completed = ', '.join(repr(url) for url in urls)+ return (+ f'The browser session has already completed these initial navigations: {completed}. '+ 'Continue from the current browser state. Do not repeat those navigation steps unless browser status shows a different page.'+ f'\n\n{cleaned_task}'+ )++def _task_with_schema(task: str, output_model_schema: type[BaseModel] | None) -> str:if output_model_schema is None:return task@@ -4059,6 +4097,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):self._convert_initial_actions(self.initial_action_payloads) if self.initial_action_payloads else None)self._initial_actions_executed = False+ self._completed_initial_navigation_urls: list[str] = []self._pending_history_prefix: list[AgentHistory] = []self.task = _task_with_schema(_task_with_available_files(@@ -4693,14 +4732,19 @@ class Agent(Generic[Context, AgentStructuredOutput]):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)+ task = self.task+ if not followup:+ task = _task_with_completed_initial_navigation_context(+ task, self._completed_initial_navigation_urls, self.initial_action_payloads+ )self.state.follow_up_task = Falsereturn await self._run_sdk_agent(- task=self.task,+ task=task,max_steps=max_steps,started=started,on_step_end=on_step_end,source='follow_up' if followup else 'run',- followups=[self.task] if followup else None,+ followups=[task] if followup else None,)async def follow_up(@@ -5553,6 +5597,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):async def _execute_direct_initial_navigation_actions(self) -> list[ActionResult] | None:"""Pre-navigate existing CDP-backed browser sessions before the Rust agent starts."""+ self._completed_initial_navigation_urls = []if self.browser_session is None:return Noneif not (_extract_cdp_url(self.browser_session) or _extract_profile_cdp_url(self.browser_profile)):@@ -5574,6 +5619,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):results.append(ActionResult(error=message))continuetext = f'Navigated to {url}'+ self._completed_initial_navigation_urls.append(url)results.append(ActionResult(extracted_content=text, long_term_memory=text))return resultsdiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 6c6227aaf..56c59f328 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -7052,6 +7052,54 @@ async def test_rust_agent_run_executes_initial_actions_before_sdk():assert history.final_result() == 'done'+async def test_rust_agent_run_hands_off_completed_initial_navigation_as_context():+ from types import SimpleNamespace++ from browser_use.rust import Agent+ from browser_use.rust.service import _history_from_events++ 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}}],+ )+ seen = []++ async def fake_run_sdk_agent(**kwargs):+ seen.append(kwargs['task'])+ 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._run_sdk_agent = fake_run_sdk_agent+ agent._initialize_run_lifecycle_state = lambda: None++ history = await agent.run(max_steps=3)++ assert history.final_result() == 'done'+ assert browser_session.calls == [('https://example.com', False)]+ assert agent._completed_initial_navigation_urls == ['https://example.com']+ assert "already open at 'https://example.com'" in seen[0]+ assert not seen[0].startswith("First navigate to 'https://example.com'")++async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monkeypatch):from browser_use.rust import Agent@@ -7251,6 +7299,7 @@ async def test_rust_agent_initial_actions_pre_navigate_existing_cdp_session():assert browser_session.calls == [('https://example.com', False)]assert agent._initial_actions_executed is True+ assert agent._completed_initial_navigation_urls == ['https://example.com']assert agent.state.last_result[0].extracted_content == 'Navigated to https://example.com'assert agent.history.history[0].metadata.step_number == 0assert agent.history.history[0].model_output.action == agent.initial_actions