Skip to content

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

Initialize Rust agent browser session defaults

Magnus Müller6c13063156bd
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 6b6a7334b..17abf93bf 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -396,10 +396,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `f8
- `take_step` drives one Rust terminal turn and returns `(is_done, is_valid)`; `multi_act` preserves local `done` action semantics and routes non-done Browser Use action batches through the active Rust session or a bounded one-step run.
- Proof: `test_rust_agent_take_step_runs_one_terminal_turn`, `test_rust_agent_multi_act_preserves_done_action`, and `test_rust_agent_multi_act_routes_actions_to_followup`.
+78. Rust Agent constructor session/filesystem parity
+ - The Rust-backed `Agent` now initializes a real Browser Use `BrowserSession`/`BrowserProfile` pair when callers do not provide a session, preserving default post-construction access to `agent.browser_session` and `agent.browser_profile`.
+ - The wrapper now creates Browser Use-style `agent_directory`, `file_system`, `file_system_path`, restored `file_system_state`, and download-path tracking attributes while preserving duck-typed profile/session inputs for lightweight tests and custom callers.
+ - Proof: `test_rust_agent_initializes_browser_use_session_and_file_system`.
+
## 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` (52 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (53 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 c1d5dc5e7..1f2e95eec 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -6,6 +6,7 @@ import json
import os
import re
import shutil
+import tempfile
import time
from collections.abc import Awaitable, Callable
from datetime import datetime
@@ -20,6 +21,7 @@ from browser_use.agent.views import ActionResult, AgentHistory, AgentHistoryList
from browser_use.browser import BrowserProfile, BrowserSession
from browser_use.browser.profile import CHROME_DETERMINISTIC_RENDERING_ARGS, CHROME_DISABLE_SECURITY_ARGS, CHROME_DOCKER_ARGS
from browser_use.browser.views import BrowserStateHistory, TabInfo
+from browser_use.filesystem.file_system import FileSystem
from browser_use.tokens.views import ModelUsageStats, UsageSummary
@@ -863,6 +865,48 @@ def _load_rust_history(file_path: str | Path) -> AgentHistoryList:
return AgentHistoryList.model_validate(data)
+def _default_browser_session(
+ agent_id: str,
+ browser_profile: BrowserProfile | None,
+ browser_session: Any | None,
+ browser: Any | None,
+) -> tuple[Any | None, Any | None]:
+ if browser is not None:
+ return browser, getattr(browser, 'browser_profile', browser_profile)
+ if browser_session is not None:
+ return browser_session, getattr(browser_session, 'browser_profile', browser_profile)
+ if browser_profile is not None and not isinstance(browser_profile, BrowserProfile):
+ return None, browser_profile
+ resolved_profile = browser_profile or BrowserProfile()
+ session = BrowserSession(
+ browser_profile=resolved_profile,
+ id=uuid7str()[:-4] + agent_id[-4:],
+ )
+ return session, session.browser_profile
+
+
+def _init_file_system(
+ state: AgentState,
+ agent_directory: Path,
+ file_system_path: str | None,
+) -> tuple[FileSystem, str]:
+ if state.file_system_state and file_system_path:
+ raise ValueError(
+ 'Cannot provide both file_system_state (from agent state) and file_system_path. '
+ 'Either restore from existing state or create new file system at specified path, not both.'
+ )
+ if state.file_system_state:
+ file_system = FileSystem.from_state(state.file_system_state)
+ return file_system, str(file_system.base_dir)
+ if file_system_path:
+ file_system = FileSystem(file_system_path)
+ state.file_system_state = file_system.get_state()
+ return file_system, file_system_path
+ file_system = FileSystem(agent_directory)
+ state.file_system_state = file_system.get_state()
+ return file_system, str(agent_directory)
+
+
def _action_payload(action: Any) -> dict[str, Any]:
"""Serialize a Browser Use action model into JSON-like data for Rust task context."""
if isinstance(action, dict):
@@ -970,8 +1014,12 @@ class Agent(Generic[AgentStructuredOutput]):
self.id = task_id or uuid7str()
self.task_id = self.id
self.llm = llm
- self.browser_profile = browser_profile
- self.browser_session = browser or browser_session
+ self.browser_session, self.browser_profile = _default_browser_session(
+ self.id,
+ browser_profile,
+ browser_session,
+ browser,
+ )
self.tools = controller or tools
self.sensitive_data = sensitive_data
self.register_new_step_callback = register_new_step_callback
@@ -983,6 +1031,9 @@ class Agent(Generic[AgentStructuredOutput]):
self.kwargs = kwargs
self.model = _model_name(llm)
self.state = injected_agent_state or AgentState(agent_id=self.id)
+ timestamp = int(time.time())
+ self.agent_directory = Path(tempfile.gettempdir()) / f'browser_use_agent_{self.id}_{timestamp}'
+ self.file_system, self.file_system_path = _init_file_system(self.state, self.agent_directory, file_system_path)
self.settings = AgentSettings(
use_vision=use_vision,
vision_detail_level=vision_detail_level,
@@ -1028,7 +1079,8 @@ class Agent(Generic[AgentStructuredOutput]):
self.browser_storage_state = _extract_browser_storage_state(self.browser_session, self.browser_profile)
self.sensitive_data_context = _sensitive_data_context(sensitive_data)
self.display_files_in_done_text = display_files_in_done_text
- self.file_system_path = file_system_path
+ self.has_downloads_path = getattr(self.browser_profile, 'downloads_path', None) is not None
+ self._last_known_downloads: list[str] = []
self.directly_open_url = directly_open_url
self.include_recent_events = include_recent_events
self.sample_images = sample_images
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index b24d47e19..148670d72 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -768,6 +768,29 @@ def test_rust_agent_exposes_browser_use_settings():
assert agent.include_recent_events is True
+def test_rust_agent_initializes_browser_use_session_and_file_system(tmp_path):
+ from browser_use.browser import BrowserProfile, BrowserSession
+ from browser_use.rust import Agent
+
+ file_system_path = tmp_path / 'agent-files'
+ downloads_path = tmp_path / 'downloads'
+ agent = Agent(
+ task='Open example.com',
+ task_id='task-1234',
+ browser_profile=BrowserProfile(headless=True, downloads_path=downloads_path),
+ file_system_path=str(file_system_path),
+ )
+
+ assert isinstance(agent.browser_session, BrowserSession)
+ assert agent.browser_session.id.endswith('1234')
+ assert agent.browser_profile is agent.browser_session.browser_profile
+ assert agent.browser_profile.downloads_path == downloads_path
+ assert agent.has_downloads_path is True
+ assert agent.file_system_path == str(file_system_path)
+ assert agent.file_system.base_dir == file_system_path
+ assert agent.state.file_system_state is not None
+
+
def test_rust_agent_adds_available_files_to_task_context():
from browser_use.rust import Agent