Skip to content

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

Invoke Rust follow-up end hooks

Magnus Müller57b472934910
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 4ca47f3a7..31ece56ad 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -901,10 +901,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- 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`.
+178. Rust Agent run-delegated follow-up end-hook parity
+ - Browser Use-style `Agent.add_new_task(...); Agent.run(..., on_step_end=...)` follow-ups now invoke the supplied end hook after reconstructed Rust history is available.
+ - Run-delegated follow-ups preserve the normal callback order: step-start hook, terminal execution, new-step callback, step-end hook, done callback.
+ - Proof: `test_rust_agent_run_follow_up_invokes_on_step_end`.
+
## 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` (149 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (150 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 e5ad4c617..056a0ce12 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2992,7 +2992,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):
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, initialize_lifecycle=False
+ self.task,
+ max_steps=max_steps,
+ resolved_max_steps=max_steps,
+ initialize_lifecycle=False,
+ on_step_end=on_step_end,
)
self._log_main_execution_start(max_steps)
@@ -3053,6 +3057,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
max_steps: int | None,
resolved_max_steps: int,
initialize_lifecycle: bool = True,
+ on_step_end: AgentHookFunc | None = None,
) -> AgentHistoryList[AgentStructuredOutput]:
if initialize_lifecycle:
self._initialize_run_lifecycle_state()
@@ -3089,6 +3094,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._check_and_update_downloads('follow_up')
await self._save_conversation_if_requested()
await self._call_new_step_callback()
+ await self._call_callback(on_step_end, self)
await self._call_done_callback()
await self._generate_gif_if_requested()
self._log_final_outcome_messages()
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 411dd40ff..1a99cef16 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3763,6 +3763,51 @@ async def test_rust_agent_run_follow_up_dispatches_single_task_lifecycle(monkeyp
assert dispatched[1].done_output == 'lifecycle follow-up 2'
+async def test_rust_agent_run_follow_up_invokes_on_step_end(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ callbacks = []
+ process_calls = []
+ load_count = 0
+ agent = Agent(task='Start follow-up callbacks.', llm=type('LLM', (), {'model': 'gpt-test'})())
+
+ 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'callback follow-up {load_count}'}}]
+
+ async def on_step_start(callback_agent):
+ callbacks.append(('start', callback_agent.task))
+
+ def on_step_end(callback_agent):
+ callbacks.append(('end', callback_agent.history.final_result()))
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ history = await agent.run(max_steps=4, on_step_start=on_step_start, on_step_end=on_step_end)
+
+ assert history.final_result() == 'callback follow-up 1'
+ assert callbacks == [('start', 'Start follow-up callbacks.'), ('end', 'callback follow-up 1')]
+
+ callbacks.clear()
+ agent.add_new_task('Continue follow-up callbacks.')
+
+ history = await agent.run(max_steps=4, on_step_start=on_step_start, on_step_end=on_step_end)
+
+ assert history.final_result() == 'callback follow-up 2'
+ assert callbacks == [('start', 'Continue follow-up callbacks.'), ('end', 'callback follow-up 2')]
+ assert len(process_calls) == 3
+ assert process_calls[1][-3] == 'followup'
+
+
async def test_rust_agent_run_logs_browser_use_lifecycle_dispatch(monkeypatch):
from browser_use.rust import Agent