Skip to content

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

Use printed browser script output for page state

Magnus Müller768eeaf9174d
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 46ce1578a..318111b8f 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -55,6 +55,14 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
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.
+ - Printed `browser_script` page probes are also used to reconstruct browser
+ state when they contain `page_info()` dictionaries, `list_tabs()` rows, or
+ a bare URL. This keeps `Page State` aligned with visible tool output even
+ when the script used `print(info)` instead of `emit_output(info, ...)`.
+ - Terminal `list_tabs()` hides the internal `Starting agent ...` about:blank
+ placeholder tab. This prevents the model from mistaking the startup tab
+ for a user-relevant target and spending turns on unnecessary
+ reconnect/reattach recovery after a successful navigation.
- 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
@@ -70,6 +78,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- 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-browser browser_script_list_tabs_hides_agent_startup_placeholder -- --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`
@@ -77,6 +86,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- 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 -k 'printed_browser_script_page_info_as_state or 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 90c7f5fb0..2eeb48a1b 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
+import ast
import base64
import hashlib
import inspect
@@ -2540,15 +2541,52 @@ def _browser_state_candidates(value: Any) -> list[tuple[str, str, str]]:
url = _browser_url(value.get('url'))
if url:
title = str(value.get('title') or '')
+ raw_target = value.get('target')
+ nested_target_id = ''
+ if isinstance(raw_target, dict):
+ nested_target_id = str(
+ raw_target.get('target_id') or raw_target.get('targetId') or raw_target.get('tab_id') or raw_target.get('tabId') or ''
+ )
target_id = str(
- value.get('target_id') or value.get('targetId') or value.get('tab_id') or value.get('tabId') or 'tab-0'
+ value.get('target_id')
+ or value.get('targetId')
+ or value.get('tab_id')
+ or value.get('tabId')
+ or nested_target_id
+ or 'tab-0'
)
candidates.append((url, title, target_id))
- for child in value.values():
+ for key, child in value.items():
+ if key in ('url', 'live_url', 'title'):
+ continue
candidates.extend(_browser_state_candidates(child))
elif isinstance(value, list):
for child in value:
candidates.extend(_browser_state_candidates(child))
+ elif isinstance(value, str):
+ text = value.strip()
+ url = _browser_url(text)
+ if url:
+ candidates.append((url, '', 'tab-0'))
+ if len(text) > 50_000:
+ return candidates
+ segments = [text]
+ if '\n' in text:
+ segments.extend(line.strip() for line in text.splitlines() if line.strip().startswith(('{', '[')))
+ seen_segments: set[str] = set()
+ for segment in segments:
+ if segment in seen_segments or not segment.startswith(('{', '[')):
+ continue
+ seen_segments.add(segment)
+ parsed = None
+ for parser in (json.loads, ast.literal_eval):
+ try:
+ parsed = parser(segment)
+ break
+ except (SyntaxError, ValueError, TypeError, json.JSONDecodeError):
+ continue
+ if isinstance(parsed, (dict, list)):
+ candidates.extend(_browser_state_candidates(parsed))
return candidates
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 5af621024..5c516ca4b 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -7146,6 +7146,49 @@ def test_rust_history_uses_browser_script_lifecycle_outputs_as_result():
assert history.history[0].state.url == 'https://example.com'
+def test_rust_history_uses_printed_browser_script_page_info_as_state():
+ 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': 'info = page_info()\nprint(info)'},
+ },
+ },
+ {
+ 'event_type': 'browser_script.completed',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'ok': True,
+ 'status': 'finished',
+ 'text': (
+ "{'url': 'https://example.com/', 'title': 'Example Domain', "
+ "'target': {'targetId': 'target-1', 'url': 'https://example.com/', 'title': 'Example Domain'}}"
+ ),
+ },
+ },
+ {'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,
+ )
+
+ state = history.history[0].state
+ assert state.url == 'https://example.com/'
+ assert state.title == 'Example Domain'
+ assert state.tabs[0].target_id == 'target-1'
+
+
async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monkeypatch):
from browser_use.rust import Agent