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
Surface Rust SDK progress notifications
Magnus Müller7e96b756a5cc
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 02ca3a8dc..e81eeb164 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -19,6 +19,9 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly importfinal result handling.- The SDK stdout reader accepts large JSON-RPC response lines, which are expectedwhen normalized history includes screenshots, tool output, and observability data.+ - SDK server `agent.event` / `agent.projected_event` notifications are retained and+ surfaced as concise in-flight progress logs, so GitHub-runner evals can show where+ a Rust-backed run is spending time before the final history response arrives.## Current Proof@@ -27,6 +30,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- browser-use `uv run python -m py_compile browser_use/rust/service.py`- 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'`- browser-use process-backed smoke with`BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal`,proving `Agent.run()` calls the real SDK server and `Agent.follow_up()` reuses thediff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 1a720d6f5..ed30b4fa0 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -220,6 +220,8 @@ class RustSdkClient:self._pending: dict[int, asyncio.Future[Any]] = {}self._write_lock = asyncio.Lock()self.stderr_lines: list[str] = []+ self.notifications: list[dict[str, Any]] = []+ self.notification_queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue()self.stream_limit = int(os.environ.get('BROWSER_USE_SDK_STREAM_LIMIT_BYTES', str(64 * 1024 * 1024)))async def start(self) -> None:@@ -326,6 +328,16 @@ class RustSdkClient:if not isinstance(message, dict):self._fail_all(RustAgentError(f'Rust SDK server emitted non-object JSON-RPC message: {message!r}'))return+ method = message.get('method')+ if method in {'agent.event', 'agent.projected_event'}:+ notification = {+ 'method': method,+ 'params': message.get('params') if isinstance(message.get('params'), dict) else {},+ }+ self.notifications.append(notification)+ del self.notifications[:-2000]+ self.notification_queue.put_nowait(notification)+ returnif 'id' not in message:returnrequest_id = message.get('id')@@ -355,6 +367,80 @@ class RustSdkClient:self._pending.clear()+def _sdk_preview(value: Any, limit: int = 180) -> str | None:+ if value is None:+ return None+ if isinstance(value, str):+ text = value+ else:+ try:+ text = json.dumps(value, default=str)+ except Exception:+ text = str(value)+ text = ' '.join(text.split())+ if not text:+ return None+ if len(text) <= limit:+ return text+ return text[:limit] + f'...[{len(text) - limit} more chars]'+++def _sdk_payload_label(payload: dict[str, Any]) -> str | None:+ for key in ('name', 'tool_name', 'tool', 'task', 'command', 'url', 'title', 'result', 'error', 'message'):+ value = _sdk_preview(payload.get(key))+ if value:+ return f'{key}={value}'+ arguments = payload.get('arguments')+ if isinstance(arguments, dict):+ for key in ('name', 'cmd', 'command', 'code', 'url'):+ value = _sdk_preview(arguments.get(key))+ if value:+ return f'{key}={value}'+ return None+++def _sdk_notification_summary(notification: dict[str, Any]) -> str | None:+ method = notification.get('method')+ params = notification.get('params')+ if not isinstance(params, dict):+ return None+ event = params.get('event')+ if not isinstance(event, dict):+ return None+ payload = event.get('payload')+ if not isinstance(payload, dict):+ payload = {}+ kind = event.get('kind') or event.get('event_type') or event.get('type')+ if isinstance(kind, dict):+ kind = kind.get('type') or kind.get('name')+ if not isinstance(kind, str) or not kind:+ kind = str(method or 'sdk.notification')++ observed_type = payload.get('event_type')+ observed_payload = payload.get('payload')+ if isinstance(observed_type, str) and observed_type:+ kind = observed_type+ if isinstance(observed_payload, dict):+ payload = observed_payload++ if kind in {+ 'model.stream_delta',+ 'model.thinking_delta',+ 'tool.output_delta',+ 'browser.script.output_delta',+ 'python.output_delta',+ 'exec_command.output_delta',+ }:+ return None++ label = _sdk_payload_label(payload)+ if method == 'agent.projected_event':+ projected_kind = event.get('kind')+ if isinstance(projected_kind, str) and projected_kind:+ kind = f'projected.{projected_kind}'+ return f'{kind} {label}'.strip() if label else kind++class _LegacyProcessSdkClient:"""Test-only adapter for old `_run_process` monkeypatches."""@@ -4669,6 +4755,16 @@ class Agent(Generic[Context, AgentStructuredOutput]):method = 'agent.run' if self._sdk_agent_id or followups else 'agent.run_task'params = self._sdk_run_params(max_steps=max_steps, task=task, followups=followups)self._active_sdk_run_id = self.terminal_session_id or self._sdk_agent_id+ progress_task: asyncio.Task[Any] | None = None+ if isinstance(sdk, RustSdkClient):+ self.logger.info(+ 'Rust SDK %s starting: max_steps=%s browser_mode=%s browser_id=%s',+ method,+ params.get('max_steps'),+ params.get('browser_mode'),+ params.get('browser_id') or '<new>',+ )+ progress_task = asyncio.create_task(self._log_sdk_progress(sdk))try:result = await sdk.call(method, params)except asyncio.CancelledError:@@ -4679,6 +4775,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):raiseprocess_error = str(exc) or exc.__class__.__name__finally:+ if progress_task is not None:+ progress_task.cancel()+ with suppress(asyncio.CancelledError):+ await progress_taskself._active_sdk_run_id = Nonefinished = time.time()self.last_stdout = str(getattr(sdk, 'stdout_text', '') or '')@@ -4733,6 +4833,23 @@ class Agent(Generic[Context, AgentStructuredOutput]):await self._finalize_run_cleanup()return self.history+ async def _log_sdk_progress(self, sdk: RustSdkClient) -> None:+ seen = 0+ last_summary: str | None = None+ last_logged_at = 0.0+ while True:+ notification = await sdk.notification_queue.get()+ seen += 1+ summary = _sdk_notification_summary(notification)+ if not summary:+ continue+ now = time.time()+ if summary == last_summary and now - last_logged_at < 30:+ continue+ last_summary = summary+ last_logged_at = now+ self.logger.info('Rust SDK progress #%s: %s', seen, summary)+follow_up_task = follow_up@propertydiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 71ce8b4b7..50ab268c7 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2343,6 +2343,51 @@ print(json.dumps({"jsonrpc": "2.0", "id": 1, "result": {"text": "x" * 70000}}),assert result == {'text': 'x' * 70000}+async def test_rust_sdk_client_queues_agent_notifications_before_response():+ import browser_use.rust.service as rust_service++ script = r"""+import json+import sys++sys.stdin.readline()+print(json.dumps({+ "jsonrpc": "2.0",+ "method": "agent.event",+ "params": {+ "run_id": "run-1",+ "session_id": "session-1",+ "event": {"kind": "AgentStarted", "payload": {"task": "inspect"}}+ },+}), flush=True)+print(json.dumps({+ "jsonrpc": "2.0",+ "method": "agent.projected_event",+ "params": {+ "run_id": "run-1",+ "session_id": "session-1",+ "event": {"kind": "item_started", "payload": {"name": "browser_script"}}+ },+}), flush=True)+print(json.dumps({"jsonrpc": "2.0", "id": 1, "result": {"ok": True}}), flush=True)+"""+ client = rust_service.RustSdkClient([sys.executable, '-c', script], {'PYTHONUNBUFFERED': '1'})++ try:+ result = await client.call('run.with.notifications')+ first_notification = await client.notification_queue.get()+ second_notification = await client.notification_queue.get()+ finally:+ await client.close()++ assert result == {'ok': True}+ assert [item['method'] for item in client.notifications] == ['agent.event', 'agent.projected_event']+ assert first_notification['params']['event']['kind'] == 'AgentStarted'+ assert second_notification['params']['event']['kind'] == 'item_started'+ assert rust_service._sdk_notification_summary(first_notification) == 'AgentStarted task=inspect'+ assert rust_service._sdk_notification_summary(second_notification) == 'projected.item_started name=browser_script'++def test_rust_agent_bridges_llm_credentials_to_terminal_env(monkeypatch):from browser_use.rust import Agent