Skip to content

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

Surface running browser script state in Rust history

Magnus Müllerce484e098ee0
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index dfc1ea95c..043311068 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -35,16 +35,24 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
runtime, matching the live model transport bridge's `block_in_place` usage.
This prevents SDK evals from stalling after `model.turn.request` before the
configured response-open/stream-idle timeout can make progress.
+ - Running `browser_script` calls now preserve the `run_id` and observe
+ instruction in Rust event persistence, replay reconstruction, and Python
+ `AgentHistoryList` reconstruction. This prevents eval traces from showing
+ empty browser tool results while a script is still active and keeps the
+ next model turn on the intended observe/cancel path instead of repeatedly
+ navigating or reconnecting.
## Current Proof
- terminal `cargo check -p browser-use-cli`
- terminal `cargo test -p browser-use-cli sdk_ -- --nocapture`
- terminal `cargo test -p browser-use-cli sdk_run_runtime_supports_model_transport_blocking_bridge -- --nocapture`
+- terminal `cargo test -p browser-use-agent running_browser_script -- --nocapture`
- terminal `cargo test -p browser-use-cli sdk_json_rpc_agent_run_task_executes_fake_backend_with_normalized_history -- --nocapture`
- terminal `cargo test -p browser-use-llm stream_ -- --nocapture`
- terminal `cargo test -p browser-use-cli sdk_provider_run_config_maps_browser_use_options_to_rust_core -- --nocapture`
- browser-use `uv run python -m py_compile browser_use/rust/service.py`
+- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_history_surfaces_running_browser_script_observe_instruction -q`
- browser-use `uv run pytest tests/ci/test_rust_agent.py`
- browser-use `uv run pytest tests/ci/test_rust_agent.py -k 'sdk_client_reads_large_json_rpc_lines or sdk_and_reuses_session or translates_browser_use_args_to_terminal'`
- browser-use `uv run pytest tests/ci/test_rust_agent.py -k 'sdk_client_queues_agent_notifications_before_response or sdk_client_reads_large_json_rpc_lines or sdk_and_reuses_session or translates_browser_use_args_to_terminal'`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 2765dbf27..4fb0b8ba4 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2149,6 +2149,9 @@ def _tool_result_text(payload: dict[str, Any]) -> str | None:
return text
content = payload.get('content')
text = _tool_output_text(content)
+ if text:
+ return text
+ text = _browser_script_running_tool_text(payload)
if text:
return text
for key in ('summary', 'data', 'outputs'):
@@ -2158,6 +2161,20 @@ def _tool_result_text(payload: dict[str, Any]) -> str | None:
return None
+def _browser_script_running_tool_text(payload: dict[str, Any]) -> str | None:
+ if payload.get('name') != 'browser_script' or payload.get('status') != 'running':
+ return None
+ parts = ['browser_script is still running.']
+ run_id = payload.get('run_id')
+ if isinstance(run_id, str) and run_id:
+ parts.append(f'run_id: {run_id}')
+ observe_ms = _int_value(payload.get('next_observe_ms')) or 1000
+ parts.append(
+ f'Next step: call browser_script with action="observe", run_id="{run_id}", and observe_timeout_ms={observe_ms}.'
+ )
+ return '\n'.join(parts)
+
+
def _synthetic_tool_result_text(name: str) -> str:
if name == 'update_plan':
return 'Plan updated'
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 50ab268c7..3808b2012 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -8388,6 +8388,49 @@ def test_rust_history_surfaces_terminal_tool_failure_message():
assert history.action_results()[-1].error == 'browser_script failed: RuntimeError: navigation failed'
+def test_rust_history_surfaces_running_browser_script_observe_instruction():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}},
+ {
+ 'event_type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'arguments': {'code': "goto_url('https://example.com')\nwait_for_load(30)"},
+ },
+ },
+ {
+ 'event_type': 'tool.output',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'ok': True,
+ 'status': 'running',
+ 'run_id': 'bs-123',
+ 'next_observe_ms': 7000,
+ 'text': '',
+ },
+ },
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ tool_result = history.action_results()[0]
+ assert tool_result.error is None
+ assert tool_result.extracted_content is not None
+ assert 'browser_script is still running.' in tool_result.extracted_content
+ assert 'run_id: bs-123' in tool_result.extracted_content
+ assert 'action="observe"' in tool_result.extracted_content
+ assert 'observe_timeout_ms=7000' in tool_result.extracted_content
+
+
def test_rust_history_surfaces_terminal_cancellation_and_tool_abort_messages():
from browser_use.rust.service import _history_from_events