Skip to content

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

Wait for Rust agent resume before run

Magnus Müller732a42584260
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index f95cd000f..30c5526b1 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -931,10 +931,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Interrupted initial actions are swallowed like Browser Use's first-step path, allowing the caller's one-step execution to continue with current agent state.
- Proof: `test_rust_agent_take_step_executes_initial_actions_on_first_step`.
+184. Rust Agent run pause/resume parity
+ - Terminal-backed `Agent.run()` now waits when the agent is paused before Rust terminal execution instead of returning a paused-run error history.
+ - 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`.
+
## 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` (155 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (156 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 4f00e8c08..5cc97f08e 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2974,23 +2974,30 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._finalize_run_cleanup()
return self.history
if self.state.paused:
- finished = time.time()
- self.result = _history_from_events(
- [],
- model=self.model,
- started=started,
- finished=finished,
- output_model_schema=self.output_model_schema,
- process_error='Rust agent is paused before terminal run.',
- )
- self.history = self.result
- await self._log_run_usage_summary()
- self._record_run_telemetry(max_steps=max_steps, agent_run_error='Rust agent is paused 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
+ self.logger.debug('Agent paused before Rust terminal run, waiting to resume...')
+ await self._external_pause_event.wait()
+ signal_handler = getattr(self, '_run_signal_handler', None)
+ reset = getattr(signal_handler, 'reset', None)
+ if callable(reset):
+ reset()
+ if await self._should_stop_before_run():
+ finished = time.time()
+ self.result = _history_from_events(
+ [],
+ model=self.model,
+ started=started,
+ finished=finished,
+ output_model_schema=self.output_model_schema,
+ process_error='Rust agent stopped before terminal run.',
+ )
+ self.history = self.result
+ 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
if self.state.follow_up_task and self.terminal_session_id:
self.state.follow_up_task = False
await self._call_callback(on_step_start, self)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index cba5f5f76..2208a9fa2 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3335,6 +3335,73 @@ async def test_rust_agent_run_registers_browser_use_signal_handler(monkeypatch):
assert captured_events[0].error_message == 'SIGINT: Cancelled by user'
+async def test_rust_agent_run_waits_for_resume_before_terminal(monkeypatch):
+ import asyncio
+
+ 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 = []
+
+ class FakeSignalHandler:
+ def __init__(self, **kwargs):
+ events.append('init_signal')
+
+ def register(self):
+ events.append('register_signal')
+
+ def unregister(self):
+ events.append('unregister_signal')
+
+ def reset(self):
+ events.append('reset_signal')
+
+ monkeypatch.setattr(rust_service, 'SignalHandler', FakeSignalHandler)
+ agent = Agent(task='Wait for resume before running.', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.pause()
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ events.append(('run_process', agent.state.paused, timeout_seconds))
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [{'event_type': 'session.done', 'payload': {'result': 'resumed run'}}]
+
+ async def on_step_start(callback_agent):
+ events.append(('start', callback_agent.state.paused))
+
+ def on_step_end(callback_agent):
+ events.append(('end', callback_agent.history.final_result()))
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ run_task = asyncio.create_task(agent.run(max_steps=4, on_step_start=on_step_start, on_step_end=on_step_end))
+ for _ in range(20):
+ await asyncio.sleep(0)
+ if 'register_signal' in events:
+ break
+
+ assert 'register_signal' in events
+ assert run_task.done() is False
+ assert [event for event in events if isinstance(event, tuple)] == []
+
+ agent.resume()
+ history = await asyncio.wait_for(run_task, timeout=2)
+
+ assert history.final_result() == 'resumed run'
+ assert events == [
+ 'init_signal',
+ 'register_signal',
+ 'reset_signal',
+ ('start', False),
+ ('run_process', False, agent.settings.step_timeout),
+ ('end', 'resumed run'),
+ 'unregister_signal',
+ ]
+
+
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