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 execute step parity

Magnus Müller9a69b90cb4e8
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 5df670356..9134f5229 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -451,10 +451,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- Rust terminal session identity is tracked separately as `agent.terminal_session_id` and is used for `run-codex-session`, event loading, follow-ups, and transcript metadata.
- Proof: `test_rust_agent_keeps_browser_use_session_id_separate_from_terminal_session`.
+89. Rust Agent execute-step helper parity
+ - The Rust-backed `Agent` now exposes Browser Use's async `_execute_step(...)` helper and routes it through a single Rust terminal turn.
+ - The helper preserves Browser Use-style step start/end callbacks, returns the done state, and records timeout errors in `state.last_result`.
+ - Proof: `test_rust_agent_execute_step_runs_one_turn_with_callbacks`.
+
## 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` (62 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (63 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`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 5e13285e2..c3a91d836 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1456,6 +1456,27 @@ class Agent(Generic[AgentStructuredOutput]):
is_valid = not history.has_errors()
return history.is_done(), is_valid
+ async def _execute_step(
+ self,
+ step: int,
+ max_steps: int,
+ step_info: AgentStepInfo,
+ on_step_start: AgentHookFunc | None = None,
+ on_step_end: AgentHookFunc | None = None,
+ ) -> bool:
+ """Execute one Browser Use-style run step through the Rust terminal core."""
+ self.state.n_steps = max(self.state.n_steps, step_info.step_number)
+ try:
+ history = await self.run(max_steps=1, on_step_start=on_step_start, on_step_end=on_step_end)
+ except TimeoutError:
+ error_msg = f'Step {step + 1} timed out after {self.settings.step_timeout} seconds'
+ self.state.consecutive_failures += 1
+ self.state.last_result = [ActionResult(error=error_msg)]
+ await self._call_callback(on_step_end, self)
+ return False
+ _ = max_steps
+ return history.is_done()
+
async def multi_act(self, actions: list[Any]) -> list[ActionResult]:
"""Execute Browser Use action models through the Rust-backed session.
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 31bd8720e..060bc75d2 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1324,6 +1324,55 @@ async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monke
assert agent.state.last_result[-1].extracted_content == 'step answer'
+async def test_rust_agent_execute_step_runs_one_turn_with_callbacks(monkeypatch):
+ from browser_use.agent.views import AgentStepInfo
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ seen = []
+
+ def done_callback(history):
+ seen.append(('done', history.final_result()))
+
+ agent = Agent(
+ task='execute one step',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ register_done_callback=done_callback,
+ )
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ seen.append(('argv', argv[-4], timeout_seconds))
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [{'event_type': 'session.done', 'payload': {'result': 'execute step answer'}}]
+
+ async def on_step_start(callback_agent):
+ seen.append(('start', callback_agent is agent))
+
+ def on_step_end(callback_agent):
+ seen.append(('end', callback_agent is agent))
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ is_done = await agent._execute_step(
+ step=0,
+ max_steps=1,
+ step_info=AgentStepInfo(step_number=0, max_steps=1),
+ on_step_start=on_step_start,
+ on_step_end=on_step_end,
+ )
+
+ assert is_done is True
+ assert seen == [
+ ('start', True),
+ ('argv', 'run-codex', agent.settings.step_timeout),
+ ('end', True),
+ ('done', 'execute step answer'),
+ ]
+
+
async def test_rust_agent_multi_act_preserves_done_action():
from browser_use.rust import Agent