Skip to content

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

Match Rust done-only guidance

Magnus Müller9833b6d181e4
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 8bab3eeb2..e70d7cf31 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -961,10 +961,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The step event is still created only when browser state and model output are available, while common finalize bookkeeping runs for every completed action result.
- Proof: `test_rust_agent_finalize_orders_summary_save_before_step_event`.
+190. Rust Agent done-only guidance parity
+ - Rust-backed `_force_done_after_last_step(...)` and `_force_done_after_failure(...)` now add the same Browser Use done-only context guidance, including the warning that tools shown in history/examples are unavailable.
+ - The helpers also emit the same debug markers before switching `AgentOutput` to `DoneAgentOutput`.
+ - Proof: `test_rust_agent_done_only_guidance_matches_browser_use`.
+
## 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` (160 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (161 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 a0b9734f6..b6fe5c5da 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3617,9 +3617,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return
from browser_use.llm.messages import UserMessage
- msg = 'You reached max_steps - this is your last step. Your only tool available is the "done" tool. No other tool is available.'
- msg += '\nIf the task is not yet fully finished as requested by the user, set success in "done" to false. Else success to true.'
+ msg = 'You reached max_steps - this is your last step. Your only tool available is the "done" tool. No other tool is available. All other tools which you see in history or examples are not available.'
+ msg += '\nIf the task is not yet fully finished as requested by the user, set success in "done" to false! E.g. if not all steps are fully completed. Else success to true.'
msg += '\nInclude everything you found out for the ultimate task in the done text.'
+ self.logger.debug('Last step finishing up')
self._message_manager._add_context_message(UserMessage(content=msg))
self.AgentOutput = self.DoneAgentOutput
@@ -3630,9 +3631,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
from browser_use.llm.messages import UserMessage
msg = f'You failed {self.settings.max_failures} times. Therefore we terminate the agent.'
- msg += '\nYour only tool available is the "done" tool. No other tool is available.'
- msg += '\nIf the task is not yet fully finished as requested by the user, set success in "done" to false. Else success to true.'
+ msg += '\nYour only tool available is the "done" tool. No other tool is available. All other tools which you see in history or examples are not available.'
+ msg += '\nIf the task is not yet fully finished as requested by the user, set success in "done" to false! E.g. if not all steps are fully completed. Else success to true.'
msg += '\nInclude everything you found out for the ultimate task in the done text.'
+ self.logger.debug('Force done action, because we reached max_failures.')
self._message_manager._add_context_message(UserMessage(content=msg))
self.AgentOutput = self.DoneAgentOutput
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 8fb352585..a9827755a 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -4412,6 +4412,41 @@ async def test_rust_agent_finalize_orders_summary_save_before_step_event(monkeyp
assert [type(event).__name__ for event in agent.eventbus.dispatched] == ['CreateAgentStepEvent']
+async def test_rust_agent_done_only_guidance_matches_browser_use(monkeypatch):
+ from browser_use.agent.views import AgentStepInfo
+ from browser_use.rust import Agent
+
+ class RecordingLogger:
+ def __init__(self):
+ self.debugs = []
+
+ def debug(self, message, *args, **kwargs):
+ self.debugs.append(message)
+
+ messages = []
+ logger = RecordingLogger()
+ monkeypatch.setattr(Agent, 'logger', property(lambda self: logger))
+ agent = Agent(task='Done-only guidance parity.')
+ monkeypatch.setattr(agent._message_manager, '_add_context_message', lambda message: messages.append(message.content))
+
+ await agent._force_done_after_last_step(AgentStepInfo(step_number=2, max_steps=3))
+ assert agent.AgentOutput is agent.DoneAgentOutput
+ assert logger.debugs == ['Last step finishing up']
+ assert len(messages) == 1
+ assert 'All other tools which you see in history or examples are not available.' in messages[-1]
+ assert 'set success in "done" to false! E.g. if not all steps are fully completed.' in messages[-1]
+
+ agent.AgentOutput = None
+ agent.state.consecutive_failures = agent.settings.max_failures
+ await agent._force_done_after_failure()
+ assert agent.AgentOutput is agent.DoneAgentOutput
+ assert logger.debugs[-1] == 'Force done action, because we reached max_failures.'
+ assert len(messages) == 2
+ assert messages[-1].startswith(f'You failed {agent.settings.max_failures} times. Therefore we terminate the agent.')
+ assert 'All other tools which you see in history or examples are not available.' in messages[-1]
+ assert 'set success in "done" to false! E.g. if not all steps are fully completed.' in messages[-1]
+
+
def test_rust_agent_initializes_message_manager_and_followup_state():
from browser_use.agent.message_manager.service import MessageManager
from browser_use.rust import Agent