Skip to content

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

Add Rust agent history rerun helpers

Magnus Müller6e1a623ed4ad
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index adf6682aa..8f420a9b7 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -100,10 +100,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- Domain lists preserve caller order, set values are made deterministic, and duplicate profile entries are ignored.
- Proof: `test_rust_agent_adds_browser_profile_domain_constraints`.
+19. Rust-compatible history rerun helpers
+ - `Agent.rerun_history()` and `Agent.load_and_rerun()` are now available on the Rust-backed wrapper.
+ - Saved Rust histories can be loaded, and reruns execute through the Rust terminal core while returning Browser Use-style `ActionResult` lists.
+ - Proof: `test_rust_agent_rerun_history_delegates_to_rust_run` and `test_rust_agent_load_and_rerun_loads_saved_rust_history`.
+
## 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` (27 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (29 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.py
index eddfd120b..cbd58486f 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -418,6 +418,20 @@ def _history_from_events(
return history_list
+def _load_rust_history(file_path: str | Path) -> AgentHistoryList:
+ with open(file_path, encoding='utf-8') as history_file:
+ data = json.load(history_file)
+ if not isinstance(data, dict):
+ raise RustAgentError(f'Invalid Browser Use history file: {file_path}')
+ for item in data.get('history', []):
+ if isinstance(item, dict):
+ item['model_output'] = None
+ state = item.get('state')
+ if isinstance(state, dict) and 'interacted_element' not in state:
+ state['interacted_element'] = None
+ return AgentHistoryList.model_validate(data)
+
+
class Agent(Generic[AgentStructuredOutput]):
"""Browser Use-style Agent backed by the Rust browser-use-terminal core."""
@@ -665,6 +679,31 @@ class Agent(Generic[AgentStructuredOutput]):
file_path = 'AgentHistory.json'
self.history.save_to_file(file_path, sensitive_data=self.sensitive_data)
+ async def rerun_history(
+ self,
+ history: AgentHistoryList,
+ max_retries: int = 3,
+ skip_failures: bool = True,
+ delay_between_actions: float = 2.0,
+ ) -> list[ActionResult]:
+ """Rerun through the Rust core and return Browser Use-style action results.
+
+ The Python Agent replays serialized Python action models. Rust terminal
+ histories do not expose those action models, so this compatibility path
+ reruns the current task through the Rust core and returns reconstructed
+ action results.
+ """
+ _ = (history, max_retries, skip_failures, delay_between_actions)
+ result = await self.run(max_steps=self.kwargs.get('max_steps', 100))
+ return result.action_results()
+
+ async def load_and_rerun(self, history_file: str | Path | None = None, **kwargs: Any) -> list[ActionResult]:
+ """Load a saved Rust-backed Browser Use history and rerun the task."""
+ if not history_file:
+ history_file = 'AgentHistory.json'
+ history = _load_rust_history(history_file)
+ return await self.rerun_history(history, **kwargs)
+
def pause(self) -> None:
"""Pause the Rust-backed agent before the next terminal run."""
self.state.paused = True
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index c2f12994c..f0b22548d 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -418,6 +418,70 @@ def test_rust_agent_lifecycle_state_and_save_history(tmp_path):
assert 'saved answer' in history_file.read_text(encoding='utf-8')
+async def test_rust_agent_rerun_history_delegates_to_rust_run():
+ from browser_use.rust import Agent
+ from browser_use.rust.service import _history_from_events
+
+ agent = Agent(task='rerun start', max_steps=8)
+ 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,
+ )
+ seen = []
+
+ async def fake_run(max_steps=100, on_step_start=None, on_step_end=None):
+ seen.append(max_steps)
+ return _history_from_events(
+ [{'event_type': 'session.done', 'payload': {'result': 'rerun answer'}}],
+ model='gpt-test',
+ started=3.0,
+ finished=4.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ agent.run = fake_run
+
+ results = await agent.rerun_history(previous)
+
+ assert seen == [8]
+ assert len(results) == 1
+ assert results[0].extracted_content == 'rerun answer'
+
+
+async def test_rust_agent_load_and_rerun_loads_saved_rust_history(tmp_path):
+ from browser_use.rust import Agent
+ from browser_use.rust.service import _history_from_events
+
+ agent = Agent(task='rerun saved')
+ history = _history_from_events(
+ [{'event_type': 'session.done', 'payload': {'result': 'loaded answer'}}],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+ history_file = tmp_path / 'history.json'
+ history.save_to_file(history_file)
+ seen = []
+
+ async def fake_rerun_history(loaded_history, **kwargs):
+ seen.append((loaded_history.final_result(), kwargs))
+ return loaded_history.action_results()
+
+ agent.rerun_history = fake_rerun_history
+
+ results = await agent.load_and_rerun(history_file, max_retries=2)
+
+ assert seen == [('loaded answer', {'max_retries': 2})]
+ assert results[0].extracted_content == 'loaded answer'
+
+
async def test_rust_agent_trace_and_cloud_auth_helpers():
from browser_use.rust import Agent
from browser_use.rust.service import _history_from_events