Skip to content

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

Skip Rust step hooks when stopped before run

Magnus Müllere5d3146af057
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 30c5526b1..5c8375c75 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -936,10 +936,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Resuming clears the pause gate, resets the run signal handler when available, and then invokes `on_step_start` and the Rust terminal subprocess in normal Browser Use order.
- Proof: `test_rust_agent_run_waits_for_resume_before_terminal`.
+185. Rust Agent pre-run stop hook parity
+ - Terminal-backed `Agent.run()` no longer invokes step hooks when the agent was stopped before Rust terminal execution starts.
+ - 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`.
+
## 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` (156 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (157 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 5cc97f08e..934dc408d 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2969,7 +2969,6 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._log_run_usage_summary()
self._record_run_telemetry(max_steps=max_steps, agent_run_error='Rust agent stopped before terminal run.')
self._dispatch_run_update_event()
- await self._call_callback(on_step_end, self)
self._log_final_outcome_messages()
await self._finalize_run_cleanup()
return self.history
@@ -2994,7 +2993,6 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._log_run_usage_summary()
self._record_run_telemetry(max_steps=max_steps, agent_run_error='Rust agent stopped before terminal run.')
self._dispatch_run_update_event()
- await self._call_callback(on_step_end, self)
self._log_final_outcome_messages()
await self._finalize_run_cleanup()
return self.history
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 2208a9fa2..6b04e0374 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3402,6 +3402,60 @@ async def test_rust_agent_run_waits_for_resume_before_terminal(monkeypatch):
]
+async def test_rust_agent_run_stopped_before_terminal_skips_step_hooks(monkeypatch):
+ import browser_use.rust.service as rust_service
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ events = []
+ captured_events = []
+
+ class FakeSignalHandler:
+ def __init__(self, **kwargs):
+ events.append('init_signal')
+
+ def register(self):
+ events.append('register_signal')
+
+ def unregister(self):
+ events.append('unregister_signal')
+
+ class TokenCostService:
+ async def log_usage_summary(self):
+ events.append('usage_summary')
+
+ class Telemetry:
+ def capture(self, event):
+ captured_events.append(event)
+
+ monkeypatch.setattr(rust_service, 'SignalHandler', FakeSignalHandler)
+ agent = Agent(task='Stop before terminal run.', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.token_cost_service = TokenCostService()
+ agent.telemetry = Telemetry()
+ agent.stop()
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ events.append('run_process')
+ raise AssertionError('terminal process should not start')
+
+ async def on_step_start(callback_agent):
+ events.append('start')
+
+ def on_step_end(callback_agent):
+ events.append('end')
+
+ agent._run_process = fake_run_process
+
+ history = await agent.run(max_steps=3, on_step_start=on_step_start, on_step_end=on_step_end)
+
+ assert history.errors() == ['Rust agent stopped before terminal run.']
+ assert events == ['init_signal', 'register_signal', 'usage_summary', 'unregister_signal']
+ assert agent.state.stopped is True
+ assert len(captured_events) == 1
+ assert captured_events[0].max_steps == 3
+ assert captured_events[0].error_message == 'Rust agent stopped before terminal run.'
+
+
async def test_rust_agent_run_finalizes_after_terminal_exception(monkeypatch):
import browser_use.rust.service as rust_service
from browser_use.rust import Agent