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
Harden Rust agent result handling
Magnus Müllera67176f51e53
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex c38f25b00..3253e0679 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -52,7 +52,7 @@ def _model_name(llm: Any | None) -> str:value = getattr(llm, attr, None)if isinstance(value, str) and value:return value- return os.environ.get('BROWSER_USE_RUST_MODEL', 'gpt-5.1-codex')+ return os.environ.get('BROWSER_USE_RUST_MODEL', 'gpt-5.3-codex-spark')def _extract_cdp_url(browser_session: BrowserSession | None) -> str | None:@@ -211,6 +211,8 @@ def _history_from_events() -> AgentHistoryList[AgentStructuredOutput]:final_result = _result_from_events(events)failure = process_error or _failure_from_events(events)+ if final_result is None and failure is None:+ failure = 'Rust terminal session did not produce a final result.'is_done = final_result is not None and failure is Noneresult = ActionResult(is_done=is_done,@@ -268,6 +270,8 @@ class Agent(Generic[AgentStructuredOutput]):self.history: AgentHistoryList[AgentStructuredOutput] = AgentHistoryList(history=[], usage=None)self.result: AgentHistoryList[AgentStructuredOutput] | None = Noneself.last_events: list[dict[str, Any]] = []+ self.last_stdout = ''+ self.last_stderr = ''async def run(self,@@ -287,11 +291,15 @@ class Agent(Generic[AgentStructuredOutput]):finished = time.time()stdout_text = stdout.decode(errors='replace')stderr_text = stderr.decode(errors='replace')+ self.last_stdout = stdout_text+ self.last_stderr = stderr_textself.session_id = self._session_id_from_stdout(stdout_text)events = await self._load_events()process_error = Noneif proc.returncode:process_error = stderr_text.strip() or f'browser-use-terminal exited with code {proc.returncode}'+ elif not self.session_id:+ process_error = 'browser-use-terminal did not print a session id.'self.last_events = eventsself.result = _history_from_events(events,diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex ef29caf7a..9e727477a 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -95,6 +95,14 @@ def test_rust_agent_translates_browser_use_args_to_terminal(monkeypatch):assert env['BU_CDP_URL'] == 'wss://browser.example/devtools/browser/1'+def test_rust_agent_default_codex_model_matches_chatgpt_account(monkeypatch):+ from browser_use.rust import Agent++ monkeypatch.delenv('BROWSER_USE_RUST_MODEL', raising=False)++ assert Agent(task='report title').model == 'gpt-5.3-codex-spark'++def test_rust_agent_translates_followup_to_existing_terminal_session(monkeypatch):from browser_use.rust import Agent@@ -125,3 +133,20 @@ def test_rust_history_marks_process_failure_not_done():assert history.is_done() is Falseassert history.is_successful() is Noneassert history.errors() == ['terminal failed']+++def test_rust_history_marks_missing_terminal_result_as_error():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [{'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ assert history.final_result() is None+ assert history.is_done() is False+ assert history.errors() == ['Rust terminal session did not produce a final result.']