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
Retry Rust history reruns like Browser Use
Magnus Müller1bec5d3a4c65
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex b412c013e..db3cd55d8 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -986,10 +986,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- Parse/tool-call failures now use Browser Use's model-specific module logger path, and interrupted steps continue to log without mutating `state.last_result`.- Proof: `test_rust_agent_handle_step_error_logs_browser_use_failure_prefix`.+195. Rust Agent history-rerun retry parity+ - Rust-backed `rerun_history(...)` now honors Browser Use's public `max_retries`, `skip_failures`, and `delay_between_actions` controls around Rust-core reruns.+ - Reruns retry both terminal process exceptions and returned Browser Use history errors, raise after the final attempt when `skip_failures=False`, and otherwise return an error `ActionResult` instead of silently dropping the failure.+ - Proof: `test_rust_agent_rerun_history_honors_retry_and_skip_controls`.+## Current Verification- `python3 -m py_compile browser_use/agent/service.py 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` (163 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (164 tests)- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`@@ -1063,6 +1068,9 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- Existing-session follow-up end-to-end:- `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal BROWSER_USE_RUST_BROWSER_MODE=managed-headless BROWSER_USE_RUST_STATE_DIR=/tmp/browser-use-rust-followup-smoke timeout 420 uv run python - <<'PY' ...`- Output: first run `Example Domain`; follow-up `example.com`.+- History load/rerun Python API end-to-end:+ - Source `/home/exedev/.evaluation_tool_env`, set `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal`, set `BROWSER_USE_RUST_BROWSER_MODE=managed-headless`, run one `Agent` to save history, then run `Agent.load_and_rerun(history_file, max_retries=2, delay_between_actions=0.1)`.+ - Output: first run `Example Domain`; saved history file exists; rerun results include `Example Domain`.- Parallel managed-headless Python API smoke batch:- Source `/home/exedev/.evaluation_tool_env`, set `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal`, set `BROWSER_USE_RUST_BROWSER_MODE=managed-headless`, and run two `uv run python - <<'PY' ...` Agent scripts in parallel.- Follow-up/callback script output: `{"smoke": "python_api_followup_callbacks", "first": "Example Domain", "second": "example.com", "callback_kinds": ["step", "done", "step", "done"], "conversation_files": 2}`.diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex a4b663ef7..09726ffcd 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -3853,12 +3853,34 @@ class Agent(Generic[Context, AgentStructuredOutput]):The Python Agent replays serialized Python action models. Rust terminalhistories do not expose those action models, so this compatibility path- reruns the current task through the Rust core and returns reconstructed- action results.+ reruns the current task through the Rust core while preserving the public+ retry and skip-failure controls from Browser Use's rerun API."""- _ = (history, max_retries, skip_failures, delay_between_actions)- result = await self.run(max_steps=self.kwargs.get('max_steps', 100))- return result.action_results()+ _ = history+ max_retries = max(1, max_retries)+ last_results: list[ActionResult] = []+ last_error: str | None = None+ for attempt in range(1, max_retries + 1):+ try:+ result = await self.run(max_steps=self.kwargs.get('max_steps', 100))+ last_results = result.action_results()+ errors = [error for error in result.errors() if error]+ if not errors:+ return last_results+ last_error = errors[0]+ except Exception as error:+ last_results = []+ last_error = str(error)++ if attempt < max_retries:+ self.logger.warning(f'Rerun failed (attempt {attempt}/{max_retries}), retrying...')+ await asyncio.sleep(delay_between_actions)++ error_msg = f'Rerun failed after {max_retries} attempts: {last_error or "unknown error"}'+ self.logger.error(error_msg)+ if not skip_failures:+ raise RuntimeError(error_msg)+ return last_results or [ActionResult(error=error_msg)]async def load_and_rerun(self, history_file: str | Path | None = None, **kwargs) -> list[ActionResult]:"""Load a saved Rust-backed Browser Use history and rerun the task."""diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 25b7ade66..99431a3e6 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -5196,6 +5196,85 @@ async def test_rust_agent_rerun_history_delegates_to_rust_run():assert results[0].extracted_content == 'rerun answer'+async def test_rust_agent_rerun_history_honors_retry_and_skip_controls(monkeypatch):+ import browser_use.rust.service as rust_service+ from browser_use.agent.views import ActionResult+ from browser_use.rust import Agent+ from browser_use.rust.service import _history_from_events++ previous = _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'previous answer'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )+ success = _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'retry success'}}],+ model='gpt-test',+ started=3.0,+ finished=4.0,+ output_model_schema=None,+ process_error=None,+ )+ sleeps = []++ async def fake_sleep(delay):+ sleeps.append(delay)++ monkeypatch.setattr(rust_service.asyncio, 'sleep', fake_sleep)++ class ErrorHistory:+ def action_results(self):+ return [ActionResult(error='history failed')]++ def errors(self):+ return ['history failed']++ agent = Agent(task='rerun retry', max_steps=5)+ attempts = []++ async def fake_run(max_steps=100, on_step_start=None, on_step_end=None):+ attempts.append(max_steps)+ if len(attempts) == 1:+ raise RuntimeError('process failed')+ if len(attempts) == 2:+ return ErrorHistory()+ return success++ agent.run = fake_run++ results = await agent.rerun_history(previous, max_retries=3, delay_between_actions=0.1)++ assert attempts == [5, 5, 5]+ assert sleeps == [0.1, 0.1]+ assert results[0].extracted_content == 'retry success'++ failing_agent = Agent(task='rerun strict failure', max_steps=2)+ strict_attempts = []+ sleeps.clear()++ async def always_fail(max_steps=100, on_step_start=None, on_step_end=None):+ strict_attempts.append(max_steps)+ raise RuntimeError('still broken')++ failing_agent.run = always_fail++ with pytest.raises(RuntimeError, match='Rerun failed after 2 attempts: still broken'):+ await failing_agent.rerun_history(previous, max_retries=2, skip_failures=False, delay_between_actions=0.25)++ assert strict_attempts == [2, 2]+ assert sleeps == [0.25]++ skip_agent = Agent(task='rerun skipped failure')+ skip_agent.run = always_fail++ results = await skip_agent.rerun_history(previous, max_retries=1, skip_failures=True)++ assert results[-1].error == 'Rerun failed after 1 attempts: still broken'++async def test_rust_agent_load_and_rerun_loads_saved_rust_history(tmp_path):from browser_use.rust import Agentfrom browser_use.rust.service import _history_from_events