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
Read Rust SDK stdout in chunks
Magnus Müller725cfab5f162
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 318111b8f..42ef74986 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -17,8 +17,10 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- The returned normalized event history is reconstructed into Browser Use-compatible`AgentHistoryList`, callbacks, usage, telemetry, Laminar replay, downloads, andfinal result handling.- - The SDK stdout reader accepts large JSON-RPC response lines, which are expected- when normalized history includes screenshots, tool output, and observability data.+ - The SDK stdout reader accepts large JSON-RPC response lines by reading stdout in+ chunks and splitting newline-delimited JSON-RPC manually. This avoids asyncio's+ separator-limit failure when normalized history includes screenshots, tool+ output, and observability data in a single response line.- SDK server `agent.event` / `agent.projected_event` notifications are retained andsurfaced as concise in-flight progress logs, so GitHub-runner evals can show wherea Rust-backed run is spending time before the final history response arrives.@@ -92,6 +94,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import- 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 `uv run pytest tests/ci/test_rust_agent.py -k 'rust_sdk_client_reads_large_json_rpc_lines or rust_sdk_client_queues_agent_notifications_before_response' -q`- evaluations-internal `uv run python -m py_compile eval/service.py`- evaluations-internal `PYTHONPATH="$PWD" uv run pytest tests/test_service_cli.py::test_progress_updates_tolerate_transient_failures_by_default tests/test_service_cli.py::test_run_stage_with_progress_heartbeat_refreshes_active_stage -q`- browser-use process-backed smoke withdiff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 2eeb48a1b..4de9993fc 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -224,6 +224,8 @@ class RustSdkClient: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)))+ self.read_chunk_size = int(os.environ.get('BROWSER_USE_SDK_READ_CHUNK_BYTES', str(1024 * 1024)))+ self.max_line_bytes = int(os.environ.get('BROWSER_USE_SDK_MAX_LINE_BYTES', str(512 * 1024 * 1024)))async def start(self) -> None:if self.process is not None and self.process.returncode is None:@@ -295,17 +297,30 @@ class RustSdkClient:async def _read_stdout(self) -> None:assert self.process is not Noneassert self.process.stdout is not None+ buffer = bytearray()try:- async for raw_line in self.process.stdout:- line = raw_line.decode('utf-8', errors='replace').strip()- if not line:- continue- try:- message = json.loads(line)- except json.JSONDecodeError as exc:- self._fail_all(RustAgentError(f'Invalid Rust SDK JSON-RPC line: {line}: {exc}'))+ while True:+ chunk = await self.process.stdout.read(self.read_chunk_size)+ if not chunk:+ if buffer and not self._handle_stdout_line(bytes(buffer)):+ return+ return+ buffer.extend(chunk)+ while True:+ newline_index = buffer.find(b'\n')+ if newline_index < 0:+ break+ raw_line = bytes(buffer[:newline_index])+ del buffer[: newline_index + 1]+ if not self._handle_stdout_line(raw_line):+ return+ if len(buffer) > self.max_line_bytes:+ self._fail_all(+ RustAgentError(+ f'Rust SDK JSON-RPC line exceeded {self.max_line_bytes} bytes without newline'+ )+ )return- self._handle_message(message)except Exception as exc:message = f'Rust SDK stdout reader failed: {exc}'self.stderr_lines.append(message)@@ -318,6 +333,18 @@ class RustSdkClient:message = f'{message}: {detail}'self._fail_all(RustAgentError(message))+ def _handle_stdout_line(self, raw_line: bytes) -> bool:+ line = raw_line.decode('utf-8', errors='replace').strip()+ if not line:+ return True+ try:+ message = json.loads(line)+ except json.JSONDecodeError as exc:+ self._fail_all(RustAgentError(f'Invalid Rust SDK JSON-RPC line: {line}: {exc}'))+ return False+ self._handle_message(message)+ return True+async def _read_stderr(self) -> None:assert self.process is not Noneassert self.process.stderr is not Nonediff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 5c516ca4b..90b7e3bc3 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2323,9 +2323,11 @@ async def test_rust_agent_runs_through_sdk_and_reuses_session_for_followup(monkeassert fake_sdk.calls[1][1]['max_steps'] == 5-async def test_rust_sdk_client_reads_large_json_rpc_lines():+async def test_rust_sdk_client_reads_large_json_rpc_lines(monkeypatch):import browser_use.rust.service as rust_service+ monkeypatch.setenv('BROWSER_USE_SDK_STREAM_LIMIT_BYTES', '4096')+ monkeypatch.setenv('BROWSER_USE_SDK_READ_CHUNK_BYTES', '1024')script = """import jsonimport sys