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
Recover nested Rust SDK final events
Magnus Müllera36f7d625e73
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 19f092723..40008cc9b 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -141,6 +141,11 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly importobserved stream. If a run is cancelled or the final transport fails after`session.done`, Python reconstructs `AgentHistoryList`, usage, and finaloutput from the notification stream instead of returning an empty result.+ - Browser-use Rust SDK notification recovery now accepts both top-level+ `event_type` records and nested SDK event envelopes. Runs that visibly emit+ `session.done` in GitHub runner progress logs therefore reconstruct the+ final answer even if the final JSON-RPC history response is empty or+ compacted differently.- Terminal provider overload errors are treated as retryable transientcapacity failures. This prevents a single Claude/OpenAI `server overloaded`response from becoming an immediate no-output eval failure.@@ -210,6 +215,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- terminal `cargo test -p browser-use-cli sdk_json_rpc_agent_run_returns_child_usage_events_separately -- --nocapture`- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_agent_prices_sdk_child_usage_events_without_overriding_parent_result -q`- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_agent_runs_through_sdk_and_reuses_session_for_followup tests/ci/test_rust_agent.py::test_rust_agent_recovers_final_result_from_sdk_notifications_after_transport_error tests/ci/test_rust_agent.py::test_rust_agent_preserves_sdk_notification_history_on_cancel -q`+- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_agent_recovers_final_result_from_sdk_notifications_after_transport_error tests/ci/test_rust_agent.py::test_rust_agent_recovers_nested_sdk_notification_events tests/ci/test_rust_agent.py::test_rust_agent_prices_sdk_child_usage_events_without_overriding_parent_result -q`- evaluations-internal `uv run python -m py_compile eval/service.py`- evaluations-internal `python -m py_compile eval/task_types.py`- evaluations-internal `PYTHONPATH=. uv run pytest tests/test_service_cli.py -q -k 'usage_aliases or trims_oversized_history_fields or rust_eval_uses_adapter_initial_navigation_default or rust_eval_preserves_explicit_direct_initial_navigation_override'`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 4eea12d38..f201789e2 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -481,8 +481,20 @@ def _sdk_notification_events(sdk: Any) -> list[dict[str, Any]]:if not isinstance(params, dict):continueevent = params.get('event')- if not isinstance(event, dict) or not isinstance(event.get('event_type'), str):+ if not isinstance(event, dict):continue+ if not isinstance(event.get('event_type'), str):+ payload = event.get('payload')+ if not isinstance(payload, dict) or not isinstance(payload.get('event_type'), str):+ continue+ event = {+ 'seq': payload.get('seq', event.get('seq')),+ 'id': payload.get('id', event.get('id')),+ 'session_id': payload.get('session_id', event.get('session_id')),+ 'ts_ms': payload.get('ts_ms', event.get('ts_ms')),+ 'event_type': payload.get('event_type'),+ 'payload': payload.get('payload') if isinstance(payload.get('payload'), dict) else {},+ }identity = (event.get('seq'), event.get('id'), event.get('event_type'))if identity in seen:continuediff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex e25825920..51588d90b 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2715,6 +2715,65 @@ async def test_rust_agent_recovers_final_result_from_sdk_notifications_after_traassert [event['event_type'] for event in agent.last_events] == ['session.input', 'session.done']+async def test_rust_agent_recovers_nested_sdk_notification_events(monkeypatch):+ from browser_use.rust import Agent++ class LLM:+ model = 'fake'++ class FakeSdk:+ stderr_lines = []++ def __init__(self):+ self.notifications = [+ {+ 'method': 'agent.event',+ 'params': {+ 'event': {+ 'kind': 'observed',+ 'payload': {+ 'seq': 1,+ 'id': 'event-1',+ 'event_type': 'session.input',+ 'payload': {'text': 'task'},+ },+ }+ },+ },+ {+ 'method': 'agent.event',+ 'params': {+ 'event': {+ 'kind': 'observed',+ 'payload': {+ 'seq': 2,+ 'id': 'event-2',+ 'event_type': 'session.done',+ 'payload': {'result': 'final from nested notification', 'success': True},+ },+ }+ },+ },+ ]++ async def call(self, method, params):+ return {'history': {'success': True, 'done': True, 'events': []}}++ fake_sdk = FakeSdk()++ async def fake_ensure_sdk_client(self):+ return fake_sdk++ monkeypatch.setattr(Agent, '_ensure_sdk_client', fake_ensure_sdk_client)++ agent = Agent(task='task', llm=LLM(), directly_open_url=False)+ history = await agent.run(max_steps=3)++ assert history.final_result() == 'final from nested notification'+ assert history.is_successful() is True+ assert [event['event_type'] for event in agent.last_events] == ['session.input', 'session.done']++async def test_rust_agent_preserves_sdk_notification_history_on_cancel(monkeypatch):import asyncio