Skip to content

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

Reconstruct browser script lifecycle output

Magnus Müller198987735dd4
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index f5d68afd4..46ce1578a 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -50,6 +50,11 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
context and the stale `First navigate to ...` prefix is removed from the
SDK task. This keeps Rust from repeating a navigation that Python already
confirmed against the Browser Use Cloud browser.
+ - Structured `browser_script` lifecycle events now preserve outputs, summaries,
+ images, and browser state through terminal event persistence and Python
+ history reconstruction. This prevents the first post-navigation page probe
+ from appearing blank in eval/Laminar history when the script emitted only
+ `emit_output(...)` or screenshots and no stdout text.
- Terminal Rust SDK runs no longer advertise subagent tools unless the run
has a configured child-agent runner. This prevents eval tasks from spending
model turns on `spawn_agent` calls that can only fail with "subagents are
@@ -64,12 +69,14 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- 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-agent runtime_browser_backend_records_script_lifecycle -- --nocapture`
- terminal `cargo test -p browser-use-agent subagent_tools -- --nocapture`
- terminal `cargo test -p browser-use-agent spawn_agent_agent_type_guidance_discourages_default_override -- --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 -k 'browser_script_lifecycle_outputs_as_result or initial_actions_pre_navigate_existing_cdp_session or run_hands_off_completed_initial_navigation_as_context' -q`
- 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 -k 'initial_actions_pre_navigate_existing_cdp_session or run_executes_initial_actions_before_sdk or run_hands_off_completed_initial_navigation_as_context' -q`
- browser-use `uv run pytest tests/ci/test_rust_agent.py`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index aeb29b432..90c7f5fb0 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1722,17 +1722,21 @@ def _command_waiting_payload(payload: dict[str, Any], previous_payload: dict[str
return next_payload
-_TOOL_OUTPUT_DELTA_EVENTS = ('tool.output_delta', 'exec_command.output_delta')
+_TOOL_OUTPUT_DELTA_EVENTS = ('tool.output_delta', 'exec_command.output_delta', 'browser_script.output_delta')
+_BROWSER_SCRIPT_RESULT_EVENTS = ('browser_script.completed', 'browser_script.cancelled', 'browser_script.failed')
_TEXTUAL_TOOL_RESULT_EVENTS = (
'tool.output',
'tool.output_delta',
'exec_command.output_delta',
+ 'browser_script.output_delta',
'command.waiting',
'exec_command.end',
'tool.finished',
'model.response.input_item',
+ 'browser_script.completed',
+ 'browser_script.cancelled',
)
_MAX_TERMINAL_LONG_TERM_TEXT_LENGTH = 1000
@@ -1895,12 +1899,14 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
'tool.output',
'tool.output_delta',
'exec_command.output_delta',
+ 'browser_script.output_delta',
'command.waiting',
'exec_command.end',
'tool.failed',
'tool.aborted',
'tool.finished',
'model.response.input_item',
+ *_BROWSER_SCRIPT_RESULT_EVENTS,
):
continue
payload = _event_payload(event)
@@ -1976,12 +1982,14 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
'tool.output',
'tool.output_delta',
'exec_command.output_delta',
+ 'browser_script.output_delta',
'command.waiting',
'exec_command.end',
'tool.failed',
'tool.aborted',
'tool.finished',
'model.response.input_item',
+ *_BROWSER_SCRIPT_RESULT_EVENTS,
):
continue
payload = _event_payload(event)
@@ -2202,6 +2210,10 @@ def _tool_result_text(payload: dict[str, Any]) -> str | None:
text = _structured_tool_output_text(payload.get(key))
if text:
return text
+ if payload.get('name') == 'browser_script' and payload.get('ok') is True:
+ status = payload.get('status')
+ if status in ('finished', 'completed') or status is None:
+ return 'browser_script completed'
return None
@@ -2402,6 +2414,8 @@ def _action_results_from_tool_calls(
text = _synthetic_tool_result_text(str(name))
if event_type == 'tool.failed':
error = _tool_failure_message(payload)
+ elif event_type == 'browser_script.failed':
+ error = _tool_failure_message(payload) or 'browser_script failed'
elif event_type == 'tool.aborted':
error = _tool_abort_message(payload)
elif event_type == 'exec_command.end':
@@ -2594,7 +2608,7 @@ def _tool_image_attachments_by_name(events: list[dict[str, Any]]) -> list[tuple[
image_paths: list[str] = []
if event_type == 'tool.image':
_append_unique_attachment(image_paths, _image_path(payload.get('image')))
- elif event_type in ('tool.output', 'tool.failed'):
+ elif event_type in ('tool.output', 'tool.failed', *_BROWSER_SCRIPT_RESULT_EVENTS):
images = payload.get('images')
if isinstance(images, list):
for image in images:
@@ -2614,7 +2628,7 @@ def _screenshot_path_from_events(events: list[dict[str, Any]]) -> str | None:
if image_path:
screenshot_path = image_path
continue
- if event_type not in ('tool.output', 'tool.failed'):
+ if event_type not in ('tool.output', 'tool.failed', *_BROWSER_SCRIPT_RESULT_EVENTS):
continue
images = payload.get('images')
if not isinstance(images, list):
@@ -2649,7 +2663,7 @@ def _browser_state_from_events(events: list[dict[str, Any]]) -> BrowserStateHist
tabs: list[TabInfo] = []
for event in events:
event_type = _event_type(event)
- if event_type in ('tool.output', 'tool.started'):
+ if event_type in ('tool.output', 'tool.started', *_BROWSER_SCRIPT_RESULT_EVENTS):
payload = _event_payload(event)
if event_type == 'tool.started':
candidates = _browser_script_navigation_candidates(payload)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 56c59f328..5af621024 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -7100,6 +7100,52 @@ async def test_rust_agent_run_hands_off_completed_initial_navigation_as_context(
assert not seen[0].startswith("First navigate to 'https://example.com'")
+def test_rust_history_uses_browser_script_lifecycle_outputs_as_result():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {'event_type': 'model.turn.request', 'payload': {}},
+ {
+ 'event_type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'arguments': {'code': 'emit_output(page_info(), label="page_info")'},
+ },
+ },
+ {
+ 'event_type': 'browser_script.completed',
+ 'payload': {
+ 'name': 'browser_script',
+ 'ok': True,
+ 'status': 'finished',
+ 'run_id': 'bs-test',
+ 'outputs': [
+ {
+ 'label': 'page_info',
+ 'value': {'url': 'https://example.com', 'title': 'Example'},
+ 'summary': {'kind': 'observed', 'output_label': 'page_info'},
+ }
+ ],
+ },
+ },
+ {'event_type': 'model.turn.request', 'payload': {}},
+ {'event_type': 'session.done', 'payload': {'result': 'done'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ result = history.history[0].result[0]
+ assert result.extracted_content is not None
+ assert 'https://example.com' in result.extracted_content
+ assert history.history[0].state.url == 'https://example.com'
+
+
async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monkeypatch):
from browser_use.rust import Agent