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 stop pause parity

Magnus Müllerdba2ba947541
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index d5213b25b..f38056492 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -466,10 +466,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- The wrapper keeps a fallback initial profile for lightweight custom sessions while reflecting later browser-session profile changes through the public property.
- Proof: `test_rust_agent_browser_profile_property_tracks_session_profile`.
+92. Rust Agent stop/pause lifecycle helper parity
+ - The Rust-backed `Agent` now exposes Browser Use's async `_check_stop_or_pause(...)` helper.
+ - The helper evaluates external stop callbacks, sets stopped state for should-stop callbacks, and raises `InterruptedError` for stopped or paused agents.
+ - Proof: `test_rust_agent_check_stop_or_pause_matches_browser_use_lifecycle`.
+
## 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` (65 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (66 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 aebbcd6a1..aeafe647b 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1777,6 +1777,30 @@ class Agent(Generic[AgentStructuredOutput]):
'stderr': self.last_stderr,
}
+ async def _check_stop_or_pause(self) -> None:
+ """Check Browser Use-style stop/pause controls and raise when interrupted."""
+ if self.register_should_stop_callback is not None:
+ should_stop = self.register_should_stop_callback()
+ if inspect.isawaitable(should_stop):
+ should_stop = await should_stop
+ if should_stop:
+ self.logger.info('External callback requested stop')
+ self.state.stopped = True
+ raise InterruptedError
+
+ if self.register_external_agent_status_raise_error_callback is not None:
+ should_stop = self.register_external_agent_status_raise_error_callback()
+ if inspect.isawaitable(should_stop):
+ should_stop = await should_stop
+ if should_stop:
+ raise InterruptedError
+
+ if self.state.stopped:
+ raise InterruptedError
+
+ if self.state.paused:
+ raise InterruptedError
+
async def _should_stop_before_run(self) -> bool:
if self.state.stopped:
return True
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index c1ff08834..7fd2a3baa 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1249,6 +1249,32 @@ 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_check_stop_or_pause_matches_browser_use_lifecycle():
+ from browser_use.rust import Agent
+
+ seen = []
+
+ async def should_stop():
+ seen.append('should_stop')
+ return True
+
+ agent = Agent(task='check controls', register_should_stop_callback=should_stop)
+
+ with pytest.raises(InterruptedError):
+ await agent._check_stop_or_pause()
+
+ assert seen == ['should_stop']
+ assert agent.state.stopped is True
+
+ paused_agent = Agent(task='check pause')
+ paused_agent.pause()
+
+ with pytest.raises(InterruptedError):
+ await paused_agent._check_stop_or_pause()
+
+ assert paused_agent.state.paused is True
+
+
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