Skip to content

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

Dispatch Rust step callbacks per turn

Magnus Müllera683ea974972
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 2ab05909d..19445bb98 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -1016,10 +1016,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Explicit `llm.api_key` and supported `llm.base_url` fields project into the terminal subprocess environment like the Python LLM object would, while omitted credentials preserve the caller's existing environment.
- Proof: `test_rust_agent_bridges_llm_credentials_to_terminal_env`.
+201. Rust Agent per-terminal-turn new-step callback parity
+ - Rust-backed runs now invoke `register_new_step_callback` once for each reconstructed terminal model turn instead of only for the final history item.
+ - Callback step numbers use the Browser Use-facing cumulative step counter, and repeated callback dispatch for the same history object is idempotent.
+ - Proof: `test_rust_agent_invokes_new_step_callback_for_each_terminal_turn`.
+
## 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` (169 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (170 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`
@@ -1099,6 +1104,9 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- LLM object provider/env bridge Python API end-to-end:
- Source `/home/exedev/.evaluation_tool_env`, remove `OPENAI_API_KEY`/`LLM_BROWSER_OPENAI_API_KEY` from `os.environ`, and construct `Agent(..., llm=ChatOpenAI(model="gpt-4o-mini", api_key=OPENAI_API_KEY_FROM_ENV), step_timeout=300)`.
- Output: terminal command `run-openai`, bridged `LLM_BROWSER_OPENAI_API_KEY` present in `_run_env()`, final result `The page title is **Example Domain**.`, and `history.is_successful()` is `True`.
+- Per-terminal-turn callback Python API end-to-end:
+ - Source `/home/exedev/.evaluation_tool_env`, set `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal`, set `BROWSER_USE_RUST_BROWSER_MODE=managed-headless`, and construct `Agent(..., register_new_step_callback=..., step_timeout=300)`.
+ - Output: final result `Example Domain`, `history.number_of_steps()` is `5`, callback count is `5`, and callback step numbers are `[2, 3, 4, 5, 6]`.
- Parallel managed-headless Python API smoke batch:
- Source `/home/exedev/.evaluation_tool_env`, set `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal`, set `BROWSER_USE_RUST_BROWSER_MODE=managed-headless`, and run two `uv run python - <<'PY' ...` Agent scripts in parallel.
- Follow-up/callback script output: `{"smoke": "python_api_followup_callbacks", "first": "Example Domain", "second": "example.com", "callback_kinds": ["step", "done", "step", "done"], "conversation_files": 2}`.
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index ec76e2fcd..1c9844028 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2665,6 +2665,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.last_stdout = ''
self.last_stderr = ''
self._last_synced_history_id: int | None = None
+ self._last_step_callback_history_id: int | None = None
self._external_pause_event = asyncio.Event()
self._external_pause_event.set()
@@ -4131,10 +4132,15 @@ class Agent(Generic[Context, AgentStructuredOutput]):
async def _call_new_step_callback(self) -> None:
if self.register_new_step_callback is None or not self.history.history:
return
- history_item = self.history.history[-1]
- result = self.register_new_step_callback(history_item.state, None, self.state.n_steps)
- if inspect.isawaitable(result):
- await result
+ history_id = id(self.history)
+ if history_id == self._last_step_callback_history_id:
+ return
+ start_step = max(1, self.state.n_steps - len(self.history.history) + 1)
+ for offset, history_item in enumerate(self.history.history):
+ result = self.register_new_step_callback(history_item.state, None, start_step + offset)
+ if inspect.isawaitable(result):
+ await result
+ self._last_step_callback_history_id = history_id
def _sync_state_from_history(self) -> None:
if not self.history.history:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 68ed4f093..0f78736ff 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -5169,6 +5169,68 @@ async def test_rust_agent_invokes_new_step_callback(monkeypatch):
assert seen == [('https://example.com', None, 2)]
+async def test_rust_agent_invokes_new_step_callback_for_each_terminal_turn(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ seen = []
+
+ def new_step_callback(browser_state, model_output, step_number):
+ seen.append((browser_state.url, model_output, step_number))
+
+ agent = Agent(
+ task='run multiple terminal turns',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ register_new_step_callback=new_step_callback,
+ )
+ starting_steps = agent.state.n_steps
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ assert timeout_seconds == agent.settings.step_timeout
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [
+ {'type': 'model.turn.request', 'ts_ms': 1_000, 'payload': {'model': 'gpt-test'}},
+ {'type': 'browser.state', 'ts_ms': 1_010, 'payload': {'url': 'https://first.example', 'title': 'First'}},
+ {
+ 'type': 'tool.started',
+ 'ts_ms': 1_020,
+ 'payload': {'name': 'browser', 'tool_call_id': 'call-browser', 'arguments': {'cmd': 'observe'}},
+ },
+ {
+ 'type': 'tool.output',
+ 'ts_ms': 1_030,
+ 'payload': {'name': 'browser', 'tool_call_id': 'call-browser', 'text': 'first turn observed'},
+ },
+ {'type': 'model.turn.request', 'ts_ms': 2_000, 'payload': {'model': 'gpt-test'}},
+ {'type': 'browser.state', 'ts_ms': 2_010, 'payload': {'url': 'https://example.com', 'title': 'Example'}},
+ {
+ 'type': 'tool.started',
+ 'ts_ms': 2_020,
+ 'payload': {
+ 'name': 'done',
+ 'tool_call_id': 'call-done',
+ 'arguments': {'text': 'Example Domain', 'success': True},
+ },
+ },
+ {'type': 'session.done', 'ts_ms': 2_030, 'payload': {'result': 'Example Domain'}},
+ ]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ history = await agent.run(max_steps=4)
+ await agent._call_new_step_callback()
+
+ assert history.number_of_steps() == 2
+ assert agent.state.n_steps == starting_steps + 2
+ assert seen == [
+ ('https://first.example', None, starting_steps + 1),
+ ('https://example.com', None, starting_steps + 2),
+ ]
+
+
def test_rust_agent_run_sync_delegates_to_async_run(monkeypatch):
from browser_use.rust import Agent