Skip to content

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

Trim Rust multi actions before later done

Magnus Müller37de9e6a34e3
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 5c8375c75..9bb6f07ca 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -941,10 +941,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Stopped pre-run executions still finalize usage summaries, telemetry, lifecycle update events, final guidance, signal handlers, and cleanup while returning a normal stopped history error.
- Proof: `test_rust_agent_run_stopped_before_terminal_skips_step_hooks`.
+186. Rust Agent multi-action done-order parity
+ - Rust-backed `Agent.multi_act(...)` now mirrors Browser Use's rule that a `done` action is only executed as the first/single action in a batch.
+ - When `done` appears after earlier actions, the Rust wrapper stops before serializing that `done` payload into terminal follow-up/run instructions.
+ - Proof: `test_rust_agent_multi_act_ignores_later_done_actions`.
+
## 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` (157 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (158 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`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 934dc408d..6729fdb5e 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3666,7 +3666,18 @@ class Agent(Generic[Context, AgentStructuredOutput]):
serialized as a follow-up instruction for the active Rust session. A
standalone `done` action preserves Browser Use's local completion semantics.
"""
- payloads = [_action_payload(action) for action in actions]
+ payloads = []
+ total_actions = len(actions)
+ for index, action in enumerate(actions):
+ payload = _action_payload(action)
+ if index > 0 and payload.get('done') is not None:
+ self.logger.debug(
+ f'Done action is allowed only as a single action - stopped after action {index} / {total_actions}.'
+ )
+ break
+ payloads.append(payload)
+ if payload.get('done') is not None:
+ break
if not payloads:
return []
if len(payloads) == 1:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 6b04e0374..3318b0042 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -5198,6 +5198,43 @@ async def test_rust_agent_multi_act_routes_actions_to_followup():
assert results[0].extracted_content == 'actions applied'
+async def test_rust_agent_multi_act_ignores_later_done_actions():
+ from browser_use.rust import Agent
+ from browser_use.rust.service import _history_from_events
+
+ agent = Agent(task='act without premature done')
+ agent.terminal_session_id = '12345678-1234-1234-1234-123456789abc'
+ seen = []
+
+ async def fake_follow_up(task, max_steps=None):
+ seen.append((task, max_steps))
+ return _history_from_events(
+ [{'event_type': 'session.done', 'payload': {'result': 'clicked only'}}],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ agent.follow_up = fake_follow_up
+
+ results = await agent.multi_act(
+ [
+ {'click_element': {'index': 2}},
+ {'done': {'text': 'should not execute', 'success': True}},
+ {'input_text': {'index': 3, 'text': 'should not execute'}},
+ ]
+ )
+
+ assert seen[0][1] == 1
+ assert '"click_element"' in seen[0][0]
+ assert '"done"' not in seen[0][0]
+ assert 'should not execute' not in seen[0][0]
+ assert '"input_text"' not in seen[0][0]
+ assert results[0].extracted_content == 'clicked only'
+
+
async def test_rust_agent_exposes_action_replay_helper_methods():
from types import SimpleNamespace