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 SDK finals from retained events
Magnus Müller0776c739165e
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 40008cc9b..b65569e13 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -146,6 +146,14 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import`session.done` in GitHub runner progress logs therefore reconstruct thefinal answer even if the final JSON-RPC history response is empty orcompacted differently.+ - Browser-use Rust SDK history reconstruction now prefers retained live+ notifications when the final JSON-RPC response history is present but lacks+ a final result. Stale response-side compaction errors no longer override a+ `session.done` event that Python already observed from the stream.+ - Browser-use Rust SDK billing reconstruction now falls back to the SDK+ response's aggregate `history.usage` when compacted histories omit usage+ events. This keeps dashboard usage nonzero even when the event payload is+ compacted before it reaches Python.- 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.@@ -216,6 +224,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- 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`+- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_agent_recovers_nested_sdk_notification_events tests/ci/test_rust_agent.py::test_rust_agent_prefers_notification_final_when_response_history_lacks_result tests/ci/test_rust_agent.py::test_rust_agent_uses_sdk_history_usage_when_events_do_not_include_usage 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 f201789e2..425d485d8 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -3048,6 +3048,33 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary)+def _usage_tokens(summary: UsageSummary | None) -> int:+ if summary is None:+ return 0+ return max(+ _int_value(summary.total_tokens),+ _int_value(summary.total_prompt_tokens)+ + _int_value(summary.total_prompt_cache_creation_tokens)+ + _int_value(summary.total_completion_tokens),+ )+++def _usage_event_from_sdk_history_usage(raw_usage: Any) -> dict[str, Any] | None:+ if not isinstance(raw_usage, dict):+ return None+ if _chat_invoke_usage_from_payload(raw_usage) is None:+ return None+ return {+ 'event_type': 'token_count',+ 'payload': {+ 'info': {+ 'last_token_usage': raw_usage,+ 'total_token_usage': raw_usage,+ }+ },+ }++async def _usage_from_events_with_costs(events: list[dict[str, Any]],model: str,@@ -5092,6 +5119,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):notification_events = _sdk_notification_events(sdk)child_events: list[dict[str, Any]] = []usage_events: list[dict[str, Any]] = []+ response_usage_event: dict[str, Any] | None = Noneif isinstance(result, dict):agent_id = result.get('agent_id')session_id = result.get('session_id')@@ -5116,20 +5144,45 @@ class Agent(Generic[Context, AgentStructuredOutput]):raw_usage_events = history_payload.get('usage_events')if isinstance(raw_usage_events, list):usage_events = [event for event in raw_usage_events if isinstance(event, dict)]+ response_usage_event = _usage_event_from_sdk_history_usage(history_payload.get('usage'))elif process_error is None:process_error = 'Rust SDK server returned an invalid response.'+ events_result = _result_from_events(events)+ notification_result = _result_from_events(notification_events)+ used_notification_events = Falseif notification_events and (- not events or _sdk_events_truncated_for_transport(events) or len(notification_events) > len(events)+ not events+ or _sdk_events_truncated_for_transport(events)+ or len(notification_events) > len(events)+ or (notification_result is not None and events_result is None)):events = notification_eventschild_events = []usage_events = notification_events+ events_result = notification_result+ used_notification_events = Trueif not usage_events:usage_events = [*events, *child_events]- if _result_from_events(events) is not None and (+ if response_usage_event is not None:+ current_usage = _usage_from_events(usage_events, self.model)+ response_usage = _usage_from_events([response_usage_event], self.model)+ if _usage_tokens(response_usage) > _usage_tokens(current_usage):+ usage_events = [response_usage_event]+ if self.logger.isEnabledFor(logging.INFO):+ self.logger.info(+ 'Rust SDK reconstructed history: response_events=%s notification_events=%s usage_events=%s final_from=%s usage_tokens=%s',+ len(events),+ len(notification_events),+ len(usage_events),+ 'events' if events_result is not None else 'none',+ _usage_tokens(_usage_from_events(usage_events, self.model)),+ )+ if events_result is not None and (process_error == 'CancelledError' or _sdk_transport_error_after_final_result(process_error)):process_error = None+ if used_notification_events and events_result is not None:+ process_error = Noneself.last_events = eventsself.last_child_events = child_eventsself.last_usage_events = usage_eventsdiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 51588d90b..27b124555 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2774,6 +2774,149 @@ async def test_rust_agent_recovers_nested_sdk_notification_events(monkeypatch):assert [event['event_type'] for event in agent.last_events] == ['session.input', 'session.done']+async def test_rust_agent_prefers_notification_final_when_response_history_lacks_result(monkeypatch):+ from browser_use.rust import Agent++ class LLM:+ model = 'fake'++ def notification(seq: int, event_type: str, payload: dict):+ return {+ 'method': 'agent.event',+ 'params': {+ 'event': {+ 'seq': seq,+ 'id': f'event-{seq}',+ 'event_type': event_type,+ 'payload': payload,+ }+ },+ }++ class FakeSdk:+ stderr_lines = []++ def __init__(self):+ self.notifications = [+ notification(1, 'session.input', {'text': 'task'}),+ notification(+ 2,+ 'token_count',+ {+ 'info': {+ 'last_token_usage': {'input_tokens': 100, 'cached_input_tokens': 40, 'output_tokens': 10, 'total_tokens': 110},+ 'total_token_usage': {'input_tokens': 100, 'cached_input_tokens': 40, 'output_tokens': 10, 'total_tokens': 110},+ }+ },+ ),+ notification(+ 3,+ 'token_count',+ {+ 'info': {+ 'last_token_usage': {'input_tokens': 200, 'cached_input_tokens': 60, 'output_tokens': 20, 'total_tokens': 220},+ 'total_token_usage': {+ 'input_tokens': 300,+ 'cached_input_tokens': 100,+ 'output_tokens': 30,+ 'total_tokens': 330,+ },+ }+ },+ ),+ notification(4, 'session.done', {'result': 'final from retained notifications', 'success': True}),+ ]++ async def call(self, method, params):+ return {+ 'history': {+ 'success': False,+ 'done': True,+ 'errors': ['compacted response did not include final tail'],+ 'events': [+ {'event_type': 'session.input', 'payload': {'text': 'task'}},+ {'event_type': 'browser.navigated', 'payload': {'url': 'https://example.com'}},+ {'event_type': 'sdk.transport.truncated', 'payload': {'omitted_events': 100}},+ {'event_type': 'browser.released', 'payload': {}},+ {'event_type': 'agent.turn.completed', 'payload': {}},+ ],+ 'usage_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 retained notifications'+ assert history.is_successful() is True+ assert history.usage is not None+ assert history.usage.total_prompt_tokens == 300+ assert history.usage.total_prompt_cached_tokens == 100+ assert history.usage.total_completion_tokens == 30+ assert history.usage.total_tokens == 330+ assert history.usage.entry_count == 2+ assert [event['event_type'] for event in agent.last_events] == [+ 'session.input',+ 'token_count',+ 'token_count',+ 'session.done',+ ]+++async def test_rust_agent_uses_sdk_history_usage_when_events_do_not_include_usage(monkeypatch):+ from browser_use.rust import Agent++ class LLM:+ model = 'fake'++ class FakeSdk:+ stderr_lines = []+ notifications = []++ async def call(self, method, params):+ return {+ 'history': {+ 'success': True,+ 'done': True,+ 'errors': [],+ 'events': [{'event_type': 'session.done', 'payload': {'result': 'answer', 'success': True}}],+ 'usage_events': [],+ 'usage': {+ 'input_tokens': 120,+ 'cached_input_tokens': 30,+ 'input_cache_creation_tokens': 10,+ 'output_tokens': 15,+ 'total_tokens': 145,+ },+ }+ }++ 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() == 'answer'+ assert history.usage is not None+ assert history.usage.total_prompt_tokens == 120+ assert history.usage.total_prompt_cached_tokens == 30+ assert history.usage.total_prompt_cache_creation_tokens == 10+ assert history.usage.total_completion_tokens == 15+ assert history.usage.total_tokens == 145++async def test_rust_agent_preserves_sdk_notification_history_on_cancel(monkeypatch):import asyncio