Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Allow large Rust SDK history responses

Magnus Müller185bc16934fb
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 5676b0175..02ca3a8dc 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -17,6 +17,8 @@ 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, and
final 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.
## Current Proof
@@ -24,6 +26,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- terminal `cargo test -p browser-use-cli sdk_ -- --nocapture`
- 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 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 the
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 5a1286911..1a720d6f5 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -220,6 +220,7 @@ class RustSdkClient:
self._pending: dict[int, asyncio.Future[Any]] = {}
self._write_lock = asyncio.Lock()
self.stderr_lines: list[str] = []
+ self.stream_limit = int(os.environ.get('BROWSER_USE_SDK_STREAM_LIMIT_BYTES', str(64 * 1024 * 1024)))
async def start(self) -> None:
if self.process is not None and self.process.returncode is None:
@@ -230,6 +231,7 @@ class RustSdkClient:
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=self.env,
+ limit=self.stream_limit,
)
self._reader_task = asyncio.create_task(self._read_stdout())
self._stderr_task = asyncio.create_task(self._read_stderr())
@@ -280,6 +282,10 @@ class RustSdkClient:
for task in (self._reader_task, self._stderr_task):
if task is not None:
task.cancel()
+ await asyncio.gather(
+ *(task for task in (self._reader_task, self._stderr_task) if task is not None and task is not asyncio.current_task()),
+ return_exceptions=True,
+ )
self._fail_all(RustAgentError('Rust SDK server closed'))
self.process = None
@@ -297,6 +303,10 @@ class RustSdkClient:
self._fail_all(RustAgentError(f'Invalid Rust SDK JSON-RPC line: {line}: {exc}'))
return
self._handle_message(message)
+ except Exception as exc:
+ message = f'Rust SDK stdout reader failed: {exc}'
+ self.stderr_lines.append(message)
+ self._fail_all(RustAgentError(message))
finally:
if self._pending:
detail = '\n'.join(self.stderr_lines[-20:])
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index d07ea8223..71ce8b4b7 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -2323,6 +2323,26 @@ async def test_rust_agent_runs_through_sdk_and_reuses_session_for_followup(monke
assert fake_sdk.calls[1][1]['max_steps'] == 5
+async def test_rust_sdk_client_reads_large_json_rpc_lines():
+ import browser_use.rust.service as rust_service
+
+ script = """
+import json
+import sys
+
+sys.stdin.readline()
+print(json.dumps({"jsonrpc": "2.0", "id": 1, "result": {"text": "x" * 70000}}), flush=True)
+"""
+ client = rust_service.RustSdkClient([sys.executable, '-c', script], {'PYTHONUNBUFFERED': '1'})
+
+ try:
+ result = await client.call('large.response')
+ finally:
+ await client.close()
+
+ assert result == {'text': 'x' * 70000}
+
+
def test_rust_agent_bridges_llm_credentials_to_terminal_env(monkeypatch):
from browser_use.rust import Agent