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
Enforce Rust agent step timeout
Magnus Müller894588567902
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 4eac35383..807a60481 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -70,10 +70,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai- This preserves the Browser Use constructor argument and gives the Rust core enough information to inspect user-provided files when needed.- Proof: `test_rust_agent_adds_available_files_to_task_context`.+13. Terminal subprocess timeout+ - Browser Use `step_timeout` is now enforced for Rust terminal subprocess runs.+ - A timed-out terminal run is killed and surfaced as a normal history error instead of hanging the Python wrapper indefinitely.+ - Proof: `test_rust_agent_terminal_process_timeout`.+## Current Verification- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`-- `uv run pytest -q tests/ci/test_rust_agent.py` (20 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (21 tests)- `cargo build -q -p browser-use-cli`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`- Managed-headless end-to-end:diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 415857d0d..ebf374380 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -497,7 +497,7 @@ class Agent(Generic[AgentStructuredOutput]):self.state.follow_up_task = Falsereturn await self.follow_up(self.task, max_steps=max_steps)- returncode, stdout_text, stderr_text = await self._run_process(self._run_argv(max_steps))+ returncode, stdout_text, stderr_text = await self._run_process(self._run_argv(max_steps), timeout_seconds=self.settings.step_timeout)finished = time.time()self.last_stdout = stdout_textself.last_stderr = stderr_text@@ -531,12 +531,14 @@ class Agent(Generic[AgentStructuredOutput]):started = time.time()binary = find_browser_use_terminal_binary()returncode, stdout_text, stderr_text = await self._run_process(- [binary, *self._state_dir_args(), 'followup', self.session_id, task]+ [binary, *self._state_dir_args(), 'followup', self.session_id, task],+ timeout_seconds=self.settings.step_timeout,)if returncode:raise RustAgentError(stderr_text or stdout_text)returncode, _stdout_text, stderr_text = await self._run_process(- self._run_existing_argv(max_steps if max_steps is not None else self.kwargs.get('max_steps', 100))+ self._run_existing_argv(max_steps if max_steps is not None else self.kwargs.get('max_steps', 100)),+ timeout_seconds=self.settings.step_timeout,)finished = time.time()process_error = None@@ -698,14 +700,22 @@ class Agent(Generic[AgentStructuredOutput]):self.model,]- async def _run_process(self, argv: list[str]) -> tuple[int, str, str]:+ async def _run_process(self, argv: list[str], timeout_seconds: int | None = None) -> tuple[int, str, str]:proc = await asyncio.create_subprocess_exec(*argv,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE,env=self._run_env(),)- stdout, stderr = await proc.communicate()+ try:+ stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout_seconds)+ except TimeoutError:+ proc.kill()+ stdout, stderr = await proc.communicate()+ stdout_text = stdout.decode(errors='replace')+ stderr_text = stderr.decode(errors='replace')+ timeout_error = f'browser-use-terminal timed out after {timeout_seconds} seconds'+ return 124, stdout_text, '\n'.join(part for part in (stderr_text.strip(), timeout_error) if part)return proc.returncode or 0, stdout.decode(errors='replace'), stderr.decode(errors='replace')async def _call_callback(self, callback: AgentHookFunc | None, *args: Any) -> None:diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 3162d55ba..65db959ea 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport importlib.utilimport json+import sysfrom pathlib import Pathimport pytest@@ -217,7 +218,8 @@ async def test_rust_agent_invokes_browser_use_style_callbacks(monkeypatch):agent = Agent(task='start', llm=type('LLM', (), {'model': 'gpt-test'})(), register_done_callback=done_callback)- async def fake_run_process(argv):+ async def fake_run_process(argv, timeout_seconds=None):+ assert timeout_seconds == agent.settings.step_timeoutseen.append(('argv', argv[-4]))return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''@@ -259,7 +261,8 @@ async def test_rust_agent_invokes_new_step_callback(monkeypatch):register_new_step_callback=new_step_callback,)- async def fake_run_process(argv):+ async def fake_run_process(argv, timeout_seconds=None):+ assert timeout_seconds == agent.settings.step_timeoutreturn 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():@@ -357,7 +360,8 @@ async def test_rust_agent_saves_terminal_conversation(tmp_path, monkeypatch):save_conversation_path=tmp_path,)- async def fake_run_process(argv):+ async def fake_run_process(argv, timeout_seconds=None):+ assert timeout_seconds == agent.settings.step_timeoutreturn 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():@@ -381,6 +385,21 @@ async def test_rust_agent_saves_terminal_conversation(tmp_path, monkeypatch):assert agent.state.n_steps == 2+async def test_rust_agent_terminal_process_timeout():+ from browser_use.rust import Agent++ agent = Agent(task='slow', step_timeout=1)++ returncode, stdout, stderr = await agent._run_process(+ [sys.executable, '-c', 'import time; time.sleep(5)'],+ timeout_seconds=0.01,+ )++ assert returncode == 124+ assert stdout == ''+ assert 'timed out after 0.01 seconds' in stderr++def test_rust_history_marks_process_failure_not_done():from browser_use.rust.service import _history_from_events