Skip to content

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

Add Rust agent constructor signature parity

Magnus Müllered089f6e64fd
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 0b02fafa8..252d518b6 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -511,10 +511,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- The helper captures browser state with screenshots/recent events, updates downloads, logs step context, refreshes page-filtered action models, creates state messages with sensitive-data and available-file context, and applies last-step/failure done-only switching.
- Proof: `test_rust_agent_exposes_prepare_context_helper_method`.
+101. Rust Agent constructor signature order parity
+ - The Rust-backed `Agent.__init__(...)` now preserves Browser Use's constructor parameter order, including the `source`, `file_system_path`, and `task_id` tail after `injected_agent_state`.
+ - Positional constructor calls that rely on Browser Use's public signature now route those values to the same Rust wrapper fields while retaining keyword compatibility.
+ - Proof: `test_rust_agent_constructor_signature_matches_browser_use_order`.
+
## 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` (74 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (75 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`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index ab7948f2a..e6cd27dbc 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1079,6 +1079,8 @@ class Agent(Generic[AgentStructuredOutput]):
max_history_items: int | None = None,
page_extraction_llm: Any | None = None,
injected_agent_state: AgentState | None = None,
+ source: str | None = None,
+ file_system_path: str | None = None,
task_id: str | None = None,
calculate_cost: bool = False,
display_files_in_done_text: bool = True,
@@ -1090,8 +1092,6 @@ class Agent(Generic[AgentStructuredOutput]):
include_recent_events: bool = False,
sample_images: list[Any] | None = None,
final_response_after_failure: bool = True,
- file_system_path: str | None = None,
- source: str | None = None,
_url_shortening_limit: int = 25,
**kwargs: Any,
) -> None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 9cf3805ae..520d068c3 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import inspect
import importlib.util
import json
import sys
@@ -33,6 +34,37 @@ def test_agent_package_export_uses_rust_wrapper():
assert AgentPackageAgent is RustAgent
+def test_rust_agent_constructor_signature_matches_browser_use_order(tmp_path):
+ from browser_use.agent.service import Agent as BrowserUseAgent
+ from browser_use.rust import Agent as RustAgent
+
+ browser_use_params = list(inspect.signature(BrowserUseAgent.__init__).parameters)
+ rust_params = list(inspect.signature(RustAgent.__init__).parameters)
+
+ assert rust_params == browser_use_params
+
+ values = []
+ for param in list(inspect.signature(RustAgent.__init__).parameters.values())[1:]:
+ if param.name == 'task':
+ values.append('Check constructor parity.')
+ elif param.name == 'source':
+ values.append('signature-source')
+ elif param.name == 'file_system_path':
+ values.append(str(tmp_path / 'agent-files'))
+ elif param.name == 'task_id':
+ values.append('signature-task-id')
+ break
+ else:
+ assert param.default is not inspect.Parameter.empty
+ values.append(param.default)
+
+ agent = RustAgent(*values)
+
+ assert agent.source == 'signature-source'
+ assert agent.file_system_path == str(tmp_path / 'agent-files')
+ assert agent.task_id == 'signature-task-id'
+
+
def test_rust_events_reconstruct_browser_use_history():
from browser_use.rust.service import _history_from_events