Skip to content

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

Log Rust agent lifecycle dispatches

Magnus Müller03ee67406444
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 94596954f..2735be224 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -881,10 +881,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The setup log exposes the public agent session suffix, task suffix, browser session suffix, and whether the run is launching locally or connecting through CDP.
- Proof: `test_rust_agent_run_logs_browser_use_setup_metadata`.
+174. Rust Agent lifecycle dispatch debug logging parity
+ - Terminal-backed `Agent.run()` now logs Browser Use-style debug lines before dispatching run-start cloud lifecycle events.
+ - The first run logs session and task event dispatches; later runs on the same Browser Use session log only the task dispatch, matching the one-session/many-tasks lifecycle.
+ - Proof: `test_rust_agent_run_logs_browser_use_lifecycle_dispatch`.
+
## 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` (145 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (146 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 37348819f..b0921ce24 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2854,8 +2854,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._ensure_eventbus()
event_agent = self._cloud_event_agent()
if not self.state.session_initialized:
+ self.logger.debug('Dispatching CreateAgentSessionEvent...')
self.eventbus.dispatch(CreateAgentSessionEvent.from_agent(event_agent))
self.state.session_initialized = True
+ self.logger.debug('Dispatching CreateAgentTaskEvent...')
self.eventbus.dispatch(CreateAgentTaskEvent.from_agent(event_agent))
def _dispatch_run_update_event(self) -> None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 5e953a852..9aa6d4446 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3578,6 +3578,68 @@ async def test_rust_agent_run_dispatches_browser_use_lifecycle_events(monkeypatc
assert dispatched[1].done_output == 'dispatched 2'
+async def test_rust_agent_run_logs_browser_use_lifecycle_dispatch(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ dispatched = []
+ run_count = 0
+
+ class RecordingLogger:
+ def __init__(self):
+ self.infos = []
+ self.debugs = []
+ self.errors = []
+
+ def info(self, message, *args, **kwargs):
+ self.infos.append(message)
+
+ def debug(self, message, *args, **kwargs):
+ self.debugs.append(message)
+
+ def error(self, message, *args, **kwargs):
+ self.errors.append(message)
+
+ class EventBus:
+ def dispatch(self, event):
+ dispatched.append(event)
+ return event
+
+ logger = RecordingLogger()
+ monkeypatch.setattr(Agent, 'logger', property(lambda self: logger))
+ agent = Agent(task='Log lifecycle dispatch.', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.eventbus = EventBus()
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ nonlocal run_count
+ run_count += 1
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [{'event_type': 'session.done', 'payload': {'result': f'logged dispatch {run_count}'}}]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ await agent.run(max_steps=1)
+
+ assert 'Dispatching CreateAgentSessionEvent...' in logger.debugs
+ assert 'Dispatching CreateAgentTaskEvent...' in logger.debugs
+
+ logger.debugs.clear()
+ await agent.run(max_steps=1)
+
+ assert 'Dispatching CreateAgentSessionEvent...' not in logger.debugs
+ assert 'Dispatching CreateAgentTaskEvent...' in logger.debugs
+ assert [type(event).__name__ for event in dispatched] == [
+ 'CreateAgentSessionEvent',
+ 'CreateAgentTaskEvent',
+ 'UpdateAgentTaskEvent',
+ 'CreateAgentTaskEvent',
+ 'UpdateAgentTaskEvent',
+ ]
+
+
async def test_rust_agent_exposes_step_finalization_helper_methods(tmp_path):
import base64
import time