Skip to content

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

Bridge Rust agent initial actions

Magnus Müller9acae49cf68d
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 65330f08a..42f1f4b72 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -85,10 +85,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- Remote CDP and explicit browser mode environment variables still override the profile preference.
- Proof: `test_rust_agent_translates_browser_profile_headless` and `test_rust_agent_browser_mode_env_overrides_profile_headless`.
+16. Initial actions context bridge
+ - Single navigation startup keeps the direct `First navigate...` behavior.
+ - Multi-step or non-navigation `initial_actions` are now passed to the Rust terminal task as an ordered Browser Use action list instead of being silently stored and dropped.
+ - Proof: `test_rust_agent_preserves_ordered_initial_actions_context`.
+
## Current Verification
- `python3 -m py_compile 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` (24 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (25 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- Managed-headless end-to-end:
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 8769bfbcd..3b33a1a5e 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -95,25 +95,36 @@ def _extract_headless_preference(browser_session: BrowserSession | None, browser
return None
+def _navigation_url_from_action(action: Any) -> str | None:
+ if not isinstance(action, dict):
+ return None
+ for name, payload in action.items():
+ if name in ('open_tab', 'go_to_url', 'navigate') and isinstance(payload, dict):
+ url = payload.get('url')
+ if isinstance(url, str) and url:
+ return url
+ return None
+
+
def _initial_navigation_url(initial_actions: Any) -> str | None:
if not isinstance(initial_actions, list):
return None
for action in initial_actions:
- if not isinstance(action, dict):
- continue
- for name, payload in action.items():
- if name in ('open_tab', 'go_to_url', 'navigate') and isinstance(payload, dict):
- url = payload.get('url')
- if isinstance(url, str) and url:
- return url
+ url = _navigation_url_from_action(action)
+ if url:
+ return url
return None
-def _task_with_initial_navigation(task: str, initial_actions: Any) -> str:
- url = _initial_navigation_url(initial_actions)
- if not url:
+def _task_with_initial_actions(task: str, initial_actions: Any) -> str:
+ if not isinstance(initial_actions, list) or not initial_actions:
return task
- return f'First navigate to {url!r}, then complete the task.\n\n{task}'
+ if len(initial_actions) == 1:
+ url = _navigation_url_from_action(initial_actions[0])
+ if url:
+ return f'First navigate to {url!r}, then complete the task.\n\n{task}'
+ actions = json.dumps(initial_actions, indent=2, default=str)
+ return f'Before the task, perform these Browser Use initial actions in order:\n{actions}\n\nThen complete the task.\n\n{task}'
def _task_with_schema(task: str, output_model_schema: type[BaseModel] | None) -> str:
@@ -459,7 +470,7 @@ class Agent(Generic[AgentStructuredOutput]):
initial_actions = [{'navigate': {'url': self.initial_url, 'new_tab': False}}]
self.initial_actions = initial_actions
self.task = _task_with_schema(
- _task_with_available_files(_task_with_initial_navigation(task, initial_actions), self.available_file_paths),
+ _task_with_available_files(_task_with_initial_actions(task, initial_actions), self.available_file_paths),
output_model_schema,
)
self.session_id: str | None = None
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index ced302c69..42d2e9706 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -181,6 +181,24 @@ def test_rust_agent_mirrors_direct_url_startup():
assert "First navigate to 'https://example.com'" in agent.task
+def test_rust_agent_preserves_ordered_initial_actions_context():
+ from browser_use.rust import Agent
+
+ agent = Agent(
+ task='Report what is visible after setup.',
+ initial_actions=[
+ {'navigate': {'url': 'https://example.com', 'new_tab': False}},
+ {'click_element_by_index': {'index': 3}},
+ ],
+ )
+
+ assert 'Browser Use initial actions in order' in agent.task
+ assert '"navigate"' in agent.task
+ assert '"click_element_by_index"' in agent.task
+ assert 'https://example.com' in agent.task
+ assert 'Then complete the task.' in agent.task
+
+
def test_rust_agent_skips_ambiguous_or_excluded_direct_urls():
from browser_use.rust import Agent