Skip to content

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

Reconstruct Rust browser script URLs

Magnus Müller14ac3ba0dedf
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index d68ed87d1..b44a42947 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -619,10 +619,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The wrapper uses the latest cumulative `total_token_usage` snapshot so Python callers see prompt, cached prompt, completion, total token, and per-model usage values for terminal-main runs.
- Proof: `test_rust_history_reconstructs_terminal_token_count_usage`.
+122. Rust Agent terminal browser-script URL reconstruction
+ - Terminal current-main `tool.output` events from browser scripts are now scanned for page URL/title fields and reconstructed into Browser Use `BrowserStateHistory`.
+ - This makes `AgentHistoryList.urls()` reflect pages visited by the Rust terminal core instead of returning an empty URL when terminal main emits browser state through script output summaries.
+ - Proof: `test_rust_history_reconstructs_terminal_browser_script_urls`.
+
## 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` (94 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (95 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 dffa674d7..8951210cc 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -790,15 +790,69 @@ def _failure_from_events(events: list[dict[str, Any]]) -> str | None:
return None
+def _browser_url(value: Any) -> str:
+ if not isinstance(value, str):
+ return ''
+ value = value.strip()
+ if value.startswith(('http://', 'https://', 'about:', 'file://')):
+ return value
+ return ''
+
+
+def _browser_state_candidates(value: Any) -> list[tuple[str, str, str]]:
+ candidates: list[tuple[str, str, str]] = []
+ if isinstance(value, dict):
+ url = _browser_url(value.get('url'))
+ if url:
+ title = str(value.get('title') or '')
+ target_id = str(value.get('target_id') or value.get('targetId') or value.get('tab_id') or value.get('tabId') or 'tab-0')
+ candidates.append((url, title, target_id))
+ for child in value.values():
+ candidates.extend(_browser_state_candidates(child))
+ elif isinstance(value, list):
+ for child in value:
+ candidates.extend(_browser_state_candidates(child))
+ return candidates
+
+
+def _browser_script_navigation_candidates(payload: dict[str, Any]) -> list[tuple[str, str, str]]:
+ if payload.get('name') != 'browser_script':
+ return []
+ arguments = payload.get('arguments')
+ if not isinstance(arguments, dict):
+ return []
+ code = arguments.get('code')
+ if not isinstance(code, str) or not re.search(r'\b(?:goto_url|new_tab|open_tab|navigate)\s*\(', code):
+ return []
+ candidates = []
+ for match in URL_PATTERN.finditer(code):
+ url = _browser_url(match.group(0))
+ if url:
+ candidates.append((url, '', 'tab-0'))
+ return candidates
+
+
def _browser_state_from_events(events: list[dict[str, Any]]) -> BrowserStateHistory:
url = ''
title = ''
tabs: list[TabInfo] = []
for event in events:
- if _event_type(event) not in ('browser.connected', 'browser.reconnected', 'browser.target_changed', 'browser.page', 'browser.state'):
+ event_type = _event_type(event)
+ if event_type in ('tool.output', 'tool.started'):
+ payload = _event_payload(event)
+ if event_type == 'tool.started':
+ candidates = _browser_script_navigation_candidates(payload)
+ else:
+ candidates = _browser_state_candidates(payload)
+ for candidate_url, candidate_title, candidate_target_id in candidates:
+ url = candidate_url
+ title = candidate_title or title
+ tabs = [TabInfo(url=url, title=title, target_id=candidate_target_id)]
+ continue
+ if event_type not in ('browser.connected', 'browser.reconnected', 'browser.target_changed', 'browser.page', 'browser.state'):
continue
payload = _event_payload(event)
- url = str(payload.get('url') or url)
+ url = _browser_url(payload.get('url')) or url
title = str(payload.get('title') or title)
raw_tabs = payload.get('tabs')
if isinstance(raw_tabs, list):
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index e8719428e..3891ac392 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -256,6 +256,67 @@ def test_rust_events_reconstruct_browser_use_history():
assert history.usage.total_completion_tokens == 7
+def test_rust_history_reconstructs_terminal_browser_script_urls():
+ from browser_use.rust.service import _history_from_events
+
+ output_history = _history_from_events(
+ [
+ {
+ 'type': 'tool.output',
+ 'payload': {
+ 'name': 'browser_script',
+ 'ok': True,
+ 'outputs': [
+ {
+ 'label': 'start',
+ 'summary': {
+ 'kind': 'page',
+ 'title': 'AND Digital',
+ 'url': 'https://www.and.digital/',
+ },
+ 'value': {
+ 'title': 'AND Digital',
+ 'url': 'https://www.and.digital/',
+ 'target': {'target_id': 'target-1'},
+ },
+ }
+ ],
+ },
+ },
+ {'event_type': 'session.done', 'payload': {'result': 'final answer'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ assert output_history.urls() == ['https://www.and.digital/']
+ assert output_history.history[0].state.title == 'AND Digital'
+ assert output_history.history[0].state.tabs[0].url == 'https://www.and.digital/'
+
+ navigation_history = _history_from_events(
+ [
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'arguments': {'code': "info = new_tab('https://example.com')\nwait_for_load(timeout=10)"},
+ },
+ },
+ {'event_type': 'session.done', 'payload': {'result': 'Example Domain'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ assert navigation_history.urls() == ['https://example.com']
+
+
def test_rust_history_reconstructs_terminal_token_count_usage():
from browser_use.rust.service import _history_from_events