Skip to content

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

Match Rust agent state id defaults

Magnus Müllera3a8645d4fa1
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 9d5f197b9..049880e06 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -531,10 +531,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- Explicit `page_extraction_llm` overrides are still preserved, and both defaulted and explicit extraction models can be registered with token usage tracking.
- Proof: `test_rust_agent_defaults_page_extraction_llm_to_main_llm`.
+105. Rust Agent state ID default parity
+ - The Rust-backed Agent now mirrors Browser Use's default `AgentState()` initialization instead of forcing `state.agent_id` to match the public task id.
+ - Explicit `task_id` still controls `agent.id` and `agent.task_id`, while injected `AgentState` objects remain preserved for restored runs.
+ - Proof: `test_rust_agent_state_id_defaults_like_browser_use`.
+
## 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` (78 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (79 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 a472bdf87..2a99ddd69 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1126,7 +1126,7 @@ class Agent(Generic[AgentStructuredOutput]):
self._set_browser_use_version_and_source(source)
self.kwargs = kwargs
self.model = _model_name(llm)
- self.state = injected_agent_state or AgentState(agent_id=self.id)
+ self.state = injected_agent_state or AgentState()
timestamp = int(time.time())
self.agent_directory = Path(tempfile.gettempdir()) / f'browser_use_agent_{self.id}_{timestamp}'
self._set_file_system(file_system_path)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 4be0e60bf..5f20b5610 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -913,6 +913,35 @@ def test_rust_agent_defaults_page_extraction_llm_to_main_llm():
assert str(id(extraction_llm)) in override_agent.token_cost_service.registered_llms
+def test_rust_agent_state_id_defaults_like_browser_use():
+ from browser_use.agent.service import _PythonAgent as BrowserUseAgent
+ from browser_use.agent.views import AgentState
+ from browser_use.rust import Agent as RustAgent
+
+ class LLM:
+ model = 'gpt-test'
+ provider = 'test'
+
+ async def ainvoke(self, messages, output_format=None, **kwargs):
+ return type('Result', (), {'usage': None})()
+
+ task_id = 'explicit_task_id'
+ llm = LLM()
+ browser_use_agent = BrowserUseAgent(task='Inspect state.', llm=llm, task_id=task_id, directly_open_url=False)
+ rust_agent = RustAgent(task='Inspect state.', llm=llm, task_id=task_id, directly_open_url=False)
+
+ assert browser_use_agent.task_id == rust_agent.task_id == task_id
+ assert browser_use_agent.state.agent_id != task_id
+ assert rust_agent.state.agent_id != task_id
+ assert rust_agent.state.n_steps == browser_use_agent.state.n_steps == 1
+
+ injected_state = AgentState(agent_id='restored-state-id')
+ restored_agent = RustAgent(task='Restore state.', injected_agent_state=injected_state)
+
+ assert restored_agent.state is injected_state
+ assert restored_agent.state.agent_id == 'restored-state-id'
+
+
def test_rust_agent_initializes_tools_and_action_models():
from browser_use.rust import Agent
from browser_use.tools.service import Tools