Skip to content

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

Deduplicate Rust follow-up lifecycle events

Magnus Müllerb118006e9a9b
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 6027f0f30..4ca47f3a7 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -896,10 +896,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Interrupted follow-ups log usage summaries, record `KeyboardInterrupt` telemetry, dispatch the final task update, unregister signal handlers, and close browser resources before returning.
- Proof: `test_rust_agent_follow_up_finalizes_after_keyboard_interrupt`.
+177. Rust Agent run-delegated follow-up lifecycle parity
+ - Browser Use-style `Agent.add_new_task(...); Agent.run(...)` follow-ups now reuse the run-level lifecycle initialization instead of initializing twice inside the terminal follow-up helper.
+ - Run-delegated follow-ups emit one `CreateAgentTaskEvent` and one `UpdateAgentTaskEvent`, matching the single task lifecycle expected by Browser Use callers.
+ - Proof: `test_rust_agent_run_follow_up_dispatches_single_task_lifecycle`.
+
## 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` (148 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (149 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 5e61a9936..e5ad4c617 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2991,7 +2991,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return self.history
if self.state.follow_up_task and self.terminal_session_id:
self.state.follow_up_task = False
- return await self._follow_up_terminal(self.task, max_steps=max_steps, resolved_max_steps=max_steps)
+ return await self._follow_up_terminal(
+ self.task, max_steps=max_steps, resolved_max_steps=max_steps, initialize_lifecycle=False
+ )
self._log_main_execution_start(max_steps)
returncode, stdout_text, stderr_text = await self._run_process(self._run_argv(max_steps), timeout_seconds=self.settings.step_timeout)
@@ -3050,8 +3052,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
task: str,
max_steps: int | None,
resolved_max_steps: int,
+ initialize_lifecycle: bool = True,
) -> AgentHistoryList[AgentStructuredOutput]:
- self._initialize_run_lifecycle_state()
+ if initialize_lifecycle:
+ self._initialize_run_lifecycle_state()
started = time.time()
binary = find_browser_use_terminal_binary()
returncode, stdout_text, stderr_text = await self._run_process(
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index a99852f7f..411dd40ff 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3710,6 +3710,59 @@ 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_follow_up_dispatches_single_task_lifecycle(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ dispatched = []
+ process_calls = []
+ load_count = 0
+
+ class EventBus:
+ def dispatch(self, event):
+ dispatched.append(event)
+ return event
+
+ agent = Agent(task='Start lifecycle follow-up.', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.eventbus = EventBus()
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ process_calls.append(argv)
+ if len(process_calls) == 1:
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+ return 0, '', ''
+
+ async def fake_load_events():
+ nonlocal load_count
+ load_count += 1
+ return [{'event_type': 'session.done', 'payload': {'result': f'lifecycle follow-up {load_count}'}}]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ history = await agent.run(max_steps=4)
+
+ assert history.final_result() == 'lifecycle follow-up 1'
+ assert [type(event).__name__ for event in dispatched] == [
+ 'CreateAgentSessionEvent',
+ 'CreateAgentTaskEvent',
+ 'UpdateAgentTaskEvent',
+ ]
+
+ dispatched.clear()
+ agent.add_new_task('Continue the lifecycle follow-up.')
+ agent.eventbus = EventBus()
+
+ history = await agent.run(max_steps=4)
+
+ assert history.final_result() == 'lifecycle follow-up 2'
+ assert len(process_calls) == 3
+ assert process_calls[1][-3] == 'followup'
+ assert [type(event).__name__ for event in dispatched] == ['CreateAgentTaskEvent', 'UpdateAgentTaskEvent']
+ assert dispatched[0].llm_model == 'gpt-test'
+ assert dispatched[1].done_output == 'lifecycle follow-up 2'
+
+
async def test_rust_agent_run_logs_browser_use_lifecycle_dispatch(monkeypatch):
from browser_use.rust import Agent