Skip to content

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

Fix Rust Agent wrapper initialization parity

Magnus Müllere22b6fe5fc1e
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 60620ec49..b88320917 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -1528,6 +1528,11 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This targets the 2026-06-04 five-task gate where unrelated long-running tasks lost turns to transient `browser_fetch failed ... Failed to fetch` tool failures after already discovering useful page/API state.
- Proof: terminal `cargo test -q -p browser-use-browser browser_script_browser_fetch_single_returns_structured_errors_by_default -- --nocapture`, terminal `cargo test -q -p browser-use-agent browser_tool_descriptions_preserve_interaction_skills -- --nocapture`, terminal `cargo fmt --check -p browser-use-browser -p browser-use-agent`, terminal `git diff --check -- crates/browser-use-browser/src/browser_script_helpers.py crates/browser-use-browser/src/lib.rs prompts/browser-script-tool-description.md`, and terminal `cargo build -q -p browser-use-cli --bin browser-use-terminal`.
+289. Rust Agent wrapper initialization and public eval ref parity
+ - `browser_use.rust.Agent` now owns its Browser Use signature-alignment hook instead of importing the Rust wrapper from the Python `Agent` module at import time, preserving `from browser_use.rust import Agent` while keeping the previous Python `Agent` class available as `_PythonAgent`.
+ - Rust wrapper construction initializes action/output models and verifies the LLM even when `save_conversation_path` is not set, matching normal Python `Agent` initialization and preventing hosted eval refs from producing empty one-step histories because setup was skipped.
+ - Proof: browser-use `uv run pytest -q tests/ci/test_rust_agent.py -k 'rust_agent_initializes_action_models_without_conversation_path or rust_agent_setup_action_models_signature_matches_browser_use or rust_agent_resolves_conversation_path_like_browser_use'`.
+
## Current Verification
- terminal `cargo test -q -p browser-use-browser browser_script_browser_fetch_single_returns_structured_errors_by_default -- --nocapture` on commit `e109049`
@@ -1535,6 +1540,8 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- terminal `cargo fmt --check -p browser-use-browser -p browser-use-agent`
- terminal `git diff --check -- crates/browser-use-browser/src/browser_script_helpers.py crates/browser-use-browser/src/lib.rs prompts/browser-script-tool-description.md`
- terminal `cargo build -q -p browser-use-cli --bin browser-use-terminal`
+- browser-use `uv run pytest -q tests/ci/test_rust_agent.py -k 'rust_agent_initializes_action_models_without_conversation_path or rust_agent_setup_action_models_signature_matches_browser_use or rust_agent_resolves_conversation_path_like_browser_use'`
+- browser-use `uv run ruff format browser_use/agent/service.py browser_use/rust/service.py tests/ci/test_rust_agent.py`
- evaluations-internal `PYTHONPATH=. uv run pytest tests/test_service_cli.py -k 'agent_sdk_judge'` on commit `40e2360`
- evaluations-internal `PYTHONPATH=. uv run pytest tests/test_service_cli.py -k 'agent_sdk_judge_timeout or single_generous or timeout_default or timeout_inherits or explicit_timeouts or service_cli_defaults_to_100'` on commit `40e2360`
- evaluations-internal `git diff --check -- eval/judges/agent_sdk_judge.py tests/test_service_cli.py`
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 3409fcc5d..a36130469 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -4140,36 +4140,3 @@ class Agent(Generic[Context, AgentStructuredOutput]):
_PythonAgent = Agent
-
-
-def _align_rust_agent_signatures() -> None:
- from browser_use.rust.service import Agent as RustAgent
-
- for name, browser_use_method in vars(_PythonAgent).items():
- if name.startswith('__') and name != '__init__':
- continue
- rust_method = getattr(RustAgent, name, None)
- if rust_method is None or not callable(browser_use_method) or not callable(rust_method):
- continue
- try:
- rust_method.__signature__ = inspect.signature(browser_use_method)
- rust_method.__annotations__ = dict(getattr(browser_use_method, '__annotations__', {}))
- except (TypeError, ValueError, AttributeError):
- continue
- try:
- RustAgent.__signature__ = inspect.signature(_PythonAgent)
- except (TypeError, ValueError, AttributeError):
- pass
- agent_hook_func = Callable[[_PythonAgent], Awaitable[None]]
- for name in ('run', 'run_sync'):
- rust_method = getattr(RustAgent, name, None)
- if rust_method is None:
- continue
- try:
- rust_method.__annotations__['on_step_start'] = agent_hook_func | None
- rust_method.__annotations__['on_step_end'] = agent_hook_func | None
- except (TypeError, AttributeError, KeyError):
- continue
-
-
-_align_rust_agent_signatures()
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 81100d02a..5fc5c28c6 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -30,6 +30,7 @@ from browser_use.agent.cloud_events import (
CreateAgentTaskEvent,
UpdateAgentTaskEvent,
)
+from browser_use.agent.judge import construct_judge_messages
from browser_use.agent.message_manager.utils import save_conversation
from browser_use.agent.message_manager.service import MessageManager
from browser_use.agent.prompts import SystemPrompt
@@ -43,6 +44,7 @@ from browser_use.agent.views import (
AgentState,
AgentStepInfo,
AgentStructuredOutput,
+ JudgementResult,
MessageCompactionSettings,
StepMetadata,
)
@@ -78,8 +80,7 @@ from browser_use.utils import (
Context = TypeVar('Context')
AgentHookFunc = Callable[['Agent'], Awaitable[None]]
AgentNewStepCallback = (
- Callable[[BrowserStateSummary, AgentOutput, int], None]
- | Callable[[BrowserStateSummary, AgentOutput, int], Awaitable[None]]
+ Callable[[BrowserStateSummary, AgentOutput, int], None] | Callable[[BrowserStateSummary, AgentOutput, int], Awaitable[None]]
)
AgentDoneCallback = Callable[[AgentHistoryList], Awaitable[None]] | Callable[[AgentHistoryList], None]
logger = logging.getLogger(__name__)
@@ -115,11 +116,7 @@ def _laminar_preview(value: Any, limit: int = 2000) -> Any:
def _laminar_set_span_attributes(attributes: dict[str, Any]) -> None:
if not _laminar_ready():
return
- safe_attributes = {
- key: value
- for key, value in attributes.items()
- if isinstance(value, (str, bool, int, float))
- }
+ safe_attributes = {key: value for key, value in attributes.items() if isinstance(value, (str, bool, int, float))}
if not safe_attributes:
return
try:
@@ -140,11 +137,7 @@ def _laminar_set_span_output(output: Any) -> None:
def _laminar_event(name: str, attributes: dict[str, Any] | None = None) -> None:
if not _laminar_ready():
return
- safe_attributes = {
- key: value
- for key, value in (attributes or {}).items()
- if isinstance(value, (str, bool, int, float))
- }
+ safe_attributes = {key: value for key, value in (attributes or {}).items() if isinstance(value, (str, bool, int, float))}
try:
Laminar.event(name, safe_attributes or None)
except Exception:
@@ -177,9 +170,7 @@ def find_browser_use_terminal_binary() -> str:
for candidate in candidates:
if candidate.exists():
return str(candidate)
- raise RustAgentError(
- 'Could not find browser-use-terminal. Set BROWSER_USE_TERMINAL_BINARY or build the terminal CLI.'
- )
+ raise RustAgentError('Could not find browser-use-terminal. Set BROWSER_USE_TERMINAL_BINARY or build the terminal CLI.')
def _model_name(llm: Any | None) -> str:
@@ -358,7 +349,9 @@ def _managed_browser_profile_dir(browser_session: BrowserSession | None, browser
return None
-def _managed_browser_executable_path(browser_session: BrowserSession | None, browser_profile: BrowserProfile | None) -> str | None:
+def _managed_browser_executable_path(
+ browser_session: BrowserSession | None, browser_profile: BrowserProfile | None
+) -> str | None:
session_profile = getattr(browser_session, 'browser_profile', None)
for profile in (session_profile, browser_profile, browser_session):
value = getattr(profile, 'executable_path', None)
@@ -662,7 +655,7 @@ def _warn_sensitive_data_domain_constraints(
if not sensitive_data:
return
if not allowed_domains:
- logger.error(
+ logger.warning(
'⚠️ Agent(sensitive_data=••••••••) was provided but Browser(allowed_domains=[...]) is not locked down! ⚠️\n'
' ☠️ If the agent visits a malicious website and encounters a prompt-injection attack, your sensitive_data may be exposed!\n\n'
' \n'
@@ -965,7 +958,9 @@ def _compaction_replay_start_seq(event: dict[str, Any]) -> int | None:
def _events_after_terminal_compaction(events: list[dict[str, Any]]) -> list[dict[str, Any]]:
- compaction_index = next((index for index in range(len(events) - 1, -1, -1) if _event_type(events[index]) == 'session.compacted'), None)
+ compaction_index = next(
+ (index for index in range(len(events) - 1, -1, -1) if _event_type(events[index]) == 'session.compacted'), None
+ )
if compaction_index is None:
return events
replay_start_seq = _compaction_replay_start_seq(events[compaction_index])
@@ -1142,7 +1137,9 @@ def _attachments_from_events(events: list[dict[str, Any]]) -> list[str] | None:
def _json_result_candidates(text: str) -> list[str]:
candidates = [text.strip()]
- candidates.extend(match.group(1).strip() for match in re.finditer(r'```(?:json)?\s*(.*?)```', text, re.IGNORECASE | re.DOTALL))
+ candidates.extend(
+ match.group(1).strip() for match in re.finditer(r'```(?:json)?\s*(.*?)```', text, re.IGNORECASE | re.DOTALL)
+ )
decoder = json.JSONDecoder()
for index, char in enumerate(text):
if char not in '{[':
@@ -1224,7 +1221,9 @@ def _recoverable_failure_from_events(events: list[dict[str, Any]]) -> str | None
error = _tool_abort_message(payload)
if error:
return error
- if event_type == 'exec_command.end' and not _matching_tool_result_payload(events, payload, ('tool.failed', 'tool.aborted')):
+ if event_type == 'exec_command.end' and not _matching_tool_result_payload(
+ events, payload, ('tool.failed', 'tool.aborted')
+ ):
error = _exec_command_end_failure_message(payload)
if error:
return error
@@ -1521,8 +1520,12 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
and _is_redundant_paired_output_delta(previous[0], previous[1], event_type, payload, delta_text)
):
continue
- merged_payload = dict(previous[1]) if previous is not None and previous[0] in _TOOL_OUTPUT_DELTA_EVENTS else dict(payload)
- previous_text = merged_payload.get('text') if previous is not None and previous[0] in _TOOL_OUTPUT_DELTA_EVENTS else ''
+ merged_payload = (
+ dict(previous[1]) if previous is not None and previous[0] in _TOOL_OUTPUT_DELTA_EVENTS else dict(payload)
+ )
+ previous_text = (
+ merged_payload.get('text') if previous is not None and previous[0] in _TOOL_OUTPUT_DELTA_EVENTS else ''
+ )
for payload_key, payload_value in payload.items():
if payload_key != 'text':
merged_payload[payload_key] = payload_value
@@ -1540,11 +1543,16 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
continue
results[key] = (event_type, _command_waiting_payload(payload, previous[1] if previous is not None else None))
continue
- if event_type == 'exec_command.end' and previous is not None and previous[0] in (
- 'tool.output',
- 'tool.failed',
- 'tool.aborted',
- 'model.response.input_item',
+ if (
+ event_type == 'exec_command.end'
+ and previous is not None
+ and previous[0]
+ in (
+ 'tool.output',
+ 'tool.failed',
+ 'tool.aborted',
+ 'model.response.input_item',
+ )
):
continue
if event_type == 'tool.failed' and previous is not None and previous[0] == 'tool.aborted':
@@ -1585,10 +1593,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
(
result
for result in reversed(results)
- if isinstance(name, str)
- and name
- and result[0] in _TOOL_OUTPUT_DELTA_EVENTS
- and result[1].get('name') == name
+ if isinstance(name, str) and name and result[0] in _TOOL_OUTPUT_DELTA_EVENTS and result[1].get('name') == name
),
None,
)
@@ -1620,15 +1625,19 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
(
index
for index in range(len(results) - 1, -1, -1)
- if isinstance(name, str)
- and name
- and results[index][1].get('name') == name
+ if isinstance(name, str) and name and results[index][1].get('name') == name
),
None,
)
if previous_result_index is not None:
previous_event_type, previous_payload = results[previous_result_index]
- if previous_event_type in ('tool.output', 'tool.failed', 'tool.aborted', 'exec_command.end', 'model.response.input_item'):
+ if previous_event_type in (
+ 'tool.output',
+ 'tool.failed',
+ 'tool.aborted',
+ 'exec_command.end',
+ 'model.response.input_item',
+ ):
continue
results[previous_result_index] = (event_type, _command_waiting_payload(payload, previous_payload))
continue
@@ -1641,9 +1650,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
(
index
for index in range(len(results) - 1, -1, -1)
- if isinstance(name, str)
- and name
- and results[index][1].get('name') == name
+ if isinstance(name, str) and name and results[index][1].get('name') == name
),
None,
)
@@ -1661,10 +1668,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
(
result
for result in reversed(results)
- if isinstance(name, str)
- and name
- and result[0] == 'tool.aborted'
- and result[1].get('name') == name
+ if isinstance(name, str) and name and result[0] == 'tool.aborted' and result[1].get('name') == name
),
None,
)
@@ -1676,10 +1680,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
(
result
for result in reversed(results)
- if isinstance(name, str)
- and name
- and result[0] != 'tool.finished'
- and result[1].get('name') == name
+ if isinstance(name, str) and name and result[0] != 'tool.finished' and result[1].get('name') == name
),
None,
)
@@ -2103,7 +2104,9 @@ def _browser_state_candidates(value: Any) -> list[tuple[str, str, str]]:
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')
+ 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))
@@ -2350,9 +2353,7 @@ def _chat_invoke_usage_from_payload(usage: dict[str, Any]) -> ChatInvokeUsage |
def _reasoning_output_tokens(usage: dict[str, Any]) -> int:
return _int_value(
- usage.get('reasoning_output_tokens')
- or usage.get('output_reasoning_tokens')
- or usage.get('reasoning_tokens')
+ usage.get('reasoning_output_tokens') or usage.get('output_reasoning_tokens') or usage.get('reasoning_tokens')
)
@@ -2360,9 +2361,7 @@ def _usage_completion_tokens(usage: dict[str, Any]) -> int:
return _int_value(usage.get('output_tokens')) + _reasoning_output_tokens(usage)
-def _usage_total_tokens(
- usage: dict[str, Any], input_tokens: int, cache_creation_tokens: int, completion_tokens: int
-) -> int:
+def _usage_total_tokens(usage: dict[str, Any], input_tokens: int, cache_creation_tokens: int, completion_tokens: int) -> int:
computed_total = input_tokens + cache_creation_tokens + completion_tokens
if 'cache_read_input_tokens' in usage or 'cache_creation_input_tokens' in usage:
return computed_total
@@ -2396,9 +2395,7 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
cached_input_tokens += event_cached_input_tokens
cache_creation_tokens += event_cache_creation_tokens
completion_tokens += event_completion_tokens
- total_tokens += _usage_total_tokens(
- usage, event_input_tokens, event_cache_creation_tokens, event_completion_tokens
- )
+ total_tokens += _usage_total_tokens(usage, event_input_tokens, event_cache_creation_tokens, event_completion_tokens)
cost += _float_value(usage.get('cost_usd') or usage.get('cost') or payload.get('cost_usd') or payload.get('cost'))
invocations += 1
continue
@@ -3016,7 +3013,7 @@ def _record_laminar_terminal_tool_spans(events: list[dict[str, Any]], *, max_spa
event_type, payload = result if result is not None else (None, {})
input_messages = _terminal_laminar_tool_input_message(tool_call)
output_messages = _terminal_laminar_tool_output_message(event_type, payload)
- with _laminar_start_span(f"rust_core.tool.{tool_call.get('name') or 'tool'}", input=input_messages, span_type='TOOL'):
+ with _laminar_start_span(f'rust_core.tool.{tool_call.get("name") or "tool"}', input=input_messages, span_type='TOOL'):
_laminar_set_span_attributes(
{
'runtime': 'browser_use.rust',
@@ -3550,8 +3547,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
if 'deepseek' in model_name:
self.logger.warning('⚠️ DeepSeek models do not support use_vision=True yet. Setting use_vision=False for now...')
self.settings.use_vision = False
- if 'grok' in model_name:
- self.logger.warning('⚠️ XAI models do not support use_vision=True yet. Setting use_vision=False for now...')
+ if 'grok-3' in model_name or 'grok-code' in model_name:
+ self.logger.warning('⚠️ This XAI model does not support use_vision=True yet. Setting use_vision=False for now...')
self.settings.use_vision = False
logger.debug(
f'{" +vision" if self.settings.use_vision else ""}'
@@ -3586,9 +3583,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.browser_accept_downloads, self.browser_downloads_path = _extract_browser_downloads(
self.browser_session, self.browser_profile
)
- self.browser_no_viewport, self.browser_viewport = _extract_browser_viewport(
- self.browser_session, self.browser_profile
- )
+ self.browser_no_viewport, self.browser_viewport = _extract_browser_viewport(self.browser_session, self.browser_profile)
self.browser_storage_state = _extract_browser_storage_state(self.browser_session, self.browser_profile)
self.sensitive_data_context = _sensitive_data_context(sensitive_data)
_warn_sensitive_data_domain_constraints(self.logger, sensitive_data, self.allowed_domains)
@@ -3606,7 +3601,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.logger.info(f'🔗 Found URL in task: {self.initial_url}, adding as initial action...')
initial_actions = [{'navigate': {'url': self.initial_url, 'new_tab': False}}]
self.initial_action_payloads = list(initial_actions or [])
- self.initial_actions = self._convert_initial_actions(self.initial_action_payloads) if self.initial_action_payloads else None
+ self.initial_actions = (
+ self._convert_initial_actions(self.initial_action_payloads) if self.initial_action_payloads else None
+ )
self.task = _task_with_schema(
_task_with_available_files(
_task_with_sensitive_data_context(
@@ -3713,9 +3710,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return
provider = getattr(self.llm, 'provider', None) or 'rust-terminal'
model = getattr(self.llm, 'model', None) or self.model
- self.logger.info(
- f'Starting a browser-use agent with version {self.version}, with provider={provider} and model={model}'
- )
+ self.logger.info(f'Starting a browser-use agent with version {self.version}, with provider={provider} and model={model}')
def _log_main_execution_start(self, max_steps: int) -> None:
"""Log Browser Use's main execution-loop start for terminal-backed runs."""
@@ -3797,6 +3792,67 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.logger.info('Did the Agent not work as expected? Let us fix this!')
self.logger.info(' Please open a short issue here: https://github.com/browser-use/browser-use/issues')
+ @observe(ignore_input=True, ignore_output=False)
+ async def _judge_trace(self) -> JudgementResult | None:
+ """Judge the reconstructed Rust terminal trace with the configured judge LLM."""
+ input_messages = construct_judge_messages(
+ task=self.task,
+ final_result=self.history.final_result() or '',
+ agent_steps=self.history.agent_steps(),
+ screenshot_paths=[path for path in self.history.screenshot_paths() if path is not None],
+ max_images=10,
+ ground_truth=self.settings.ground_truth,
+ use_vision=self.settings.use_vision,
+ )
+
+ kwargs: dict[str, Any] = {'output_format': JudgementResult}
+ if getattr(self.judge_llm, 'provider', None) == 'browser-use':
+ kwargs['request_type'] = 'judge'
+ kwargs['session_id'] = self.session_id
+
+ try:
+ response = await self.judge_llm.ainvoke(input_messages, **kwargs)
+ return response.completion # type: ignore[return-value]
+ except Exception as exc:
+ self.logger.error(f'Judge trace failed: {exc}')
+ return None
+
+ async def _judge_and_log(self) -> None:
+ """Run judge evaluation and attach the verdict to the final action result."""
+ judgement = await self._judge_trace()
+ if not self.history.history:
+ return
+ last_step = self.history.history[-1]
+ if not last_step.result:
+ return
+ last_result = last_step.result[-1]
+ if not last_result.is_done:
+ return
+
+ last_result.judgement = judgement
+ self_reported_success = last_result.success
+ if not judgement:
+ return
+ if self_reported_success is True and judgement.verdict is True:
+ return
+
+ judge_log = '\n'
+ if self_reported_success is True and judgement.verdict is False:
+ judge_log += '⚠️ \033[33mAgent reported success but judge thinks task failed\033[0m\n'
+
+ verdict_color = '\033[32m' if judgement.verdict else '\033[31m'
+ verdict_text = '✅ PASS' if judgement.verdict else '❌ FAIL'
+ judge_log += f'⚖️ {verdict_color}Judge Verdict: {verdict_text}\033[0m\n'
+ if judgement.failure_reason:
+ judge_log += f' Failure Reason: {judgement.failure_reason}\n'
+ if judgement.reached_captcha:
+ self.logger.warning(
+ 'Agent was blocked by a captcha. Cloud browsers include stealth fingerprinting and proxy rotation to avoid this.\n'
+ ' Try: Browser(use_cloud=True) | Get an API key: https://cloud.browser-use.com?utm_source=oss&utm_medium=captcha_nudge'
+ )
+ judge_log += f' {judgement.reasoning}\n'
+ self.logger.info(judge_log)
+
def _log_agent_event(self, max_steps: int, agent_run_error: str | None = None) -> None:
"""Emit Browser Use telemetry for a Rust-backed run."""
usage = self.history.usage
@@ -5207,7 +5263,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):
"""Get Browser Use-style trace and trace_details data for the Rust-backed run."""
def extract_task_website(task_text: str) -> str | None:
- match = re.search(r'https?://[^\s<>"\']+|www\.[^\s<>"\']+|[^\s<>"\']+\.[a-z]{2,}(?:/[^\s<>"\']*)?', task_text, re.IGNORECASE)
+ match = re.search(
+ r'https?://[^\s<>"\']+|www\.[^\s<>"\']+|[^\s<>"\']+\.[a-z]{2,}(?:/[^\s<>"\']*)?', task_text, re.IGNORECASE
+ )
return match.group(0) if match else None
def json_default(value: Any) -> str:
@@ -5251,7 +5309,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):
'action_history_truncated': json.dumps(action_history, default=json_default) if action_history else None,
'action_errors': json.dumps(action_errors, default=json_default) if action_errors else None,
'urls': json.dumps(urls, default=json_default) if urls else None,
- 'final_result_response_truncated': final_result[:20000] if final_result and len(final_result) > 20000 else final_result,
+ 'final_result_response_truncated': final_result[:20000]
+ if final_result and len(final_result) > 20000
+ else final_result,
'self_report_completed': 1 if self.history.is_done() else 0,
'self_report_success': 1 if self.history.is_successful() else 0,
'duration': self.history.total_duration_seconds(),
@@ -5588,3 +5648,36 @@ class Agent(Generic[Context, AgentStructuredOutput]):
Agent.__module__ = 'browser_use.agent.service'
Agent.__doc__ = None
+
+
+def _align_browser_use_agent_signatures() -> None:
+ from browser_use.agent.service import _PythonAgent
+
+ for name, browser_use_method in vars(_PythonAgent).items():
+ if name.startswith('__') and name != '__init__':
+ continue
+ rust_method = getattr(Agent, name, None)
+ if rust_method is None or not callable(browser_use_method) or not callable(rust_method):
+ continue
+ try:
+ rust_method.__signature__ = inspect.signature(browser_use_method)
+ rust_method.__annotations__ = dict(getattr(browser_use_method, '__annotations__', {}))
+ except (TypeError, ValueError, AttributeError):
+ continue
+ try:
+ Agent.__signature__ = inspect.signature(_PythonAgent)
+ except (TypeError, ValueError, AttributeError):
+ pass
+ agent_hook_func = Callable[[_PythonAgent], Awaitable[None]]
+ for name in ('run', 'run_sync'):
+ rust_method = getattr(Agent, name, None)
+ if rust_method is None:
+ continue
+ try:
+ rust_method.__annotations__['on_step_start'] = agent_hook_func | None
+ rust_method.__annotations__['on_step_end'] = agent_hook_func | None
+ except (TypeError, AttributeError, KeyError):
+ continue
+
+
+_align_browser_use_agent_signatures()
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 770ea1dd1..88b52ad91 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -138,9 +138,7 @@ def test_rust_agent_runtime_signatures_match_browser_use_callable_surface():
rust_callables = {name for name, value in vars(RustAgent).items() if callable(value) and not name.startswith('__')}
for method_name in sorted(browser_use_callables & rust_callables):
- assert inspect.signature(getattr(RustAgent, method_name)) == inspect.signature(
- getattr(BrowserUseAgent, method_name)
- )
+ assert inspect.signature(getattr(RustAgent, method_name)) == inspect.signature(getattr(BrowserUseAgent, method_name))
def test_rust_agent_constructor_type_hints_match_browser_use_core_params():
@@ -562,7 +560,7 @@ def test_rust_history_reconstructs_terminal_model_tool_call_actions():
'payload': {
'name': 'browser_script',
'tool_call_id': 'call-browser',
- 'arguments': {'code': "duplicate_should_not_replace_original()"},
+ 'arguments': {'code': 'duplicate_should_not_replace_original()'},
},
},
{
@@ -1042,7 +1040,7 @@ def test_rust_history_reconstructs_terminal_structured_tool_output_results():
'payload': {
'name': 'browser_script',
'tool_call_id': 'call-browser',
- 'arguments': {'code': "return page.summary()"},
+ 'arguments': {'code': 'return page.summary()'},
},
},
{
@@ -2056,9 +2054,7 @@ def test_rust_history_reconstructs_terminal_artifact_attachments():
assert history.final_result() == 'Generated reports.'
assert history.action_results()[-1].attachments == attachments
- assert history.last_action() == {
- 'done': {'text': 'Generated reports.', 'success': True, 'files_to_display': attachments}
- }
+ assert history.last_action() == {'done': {'text': 'Generated reports.', 'success': True, 'files_to_display': attachments}}
def test_rust_history_reconstructs_terminal_capture_curation_gif_attachment():
@@ -3013,10 +3009,7 @@ def test_rust_agent_exposes_task_helper_methods():
assert agent._extract_start_url('Open https://example.com/report.pdf and summarize it.') is None
assert agent._extract_start_url('Use https://XXX.XX as a placeholder in the table.') is None
assert browser_use_agent._extract_start_url('Use https://XXX.XX as a placeholder in the table.') is None
- numbered_task = (
- '1. Navigate to https://elibrary.ferc.gov/eLibrary/search.\\n'
- '2. Ensure "General Search" is selected.'
- )
+ numbered_task = '1. Navigate to https://elibrary.ferc.gov/eLibrary/search.\\n2. Ensure "General Search" is selected.'
assert agent._extract_start_url(numbered_task) == 'https://elibrary.ferc.gov/eLibrary/search'
assert browser_use_agent._extract_start_url(numbered_task) == 'https://elibrary.ferc.gov/eLibrary/search'
@@ -3895,8 +3888,7 @@ async def test_rust_agent_run_logs_first_step_startup(monkeypatch):
async def fake_run_process(argv, timeout_seconds=None):
assert (
- f'Starting a browser-use agent with version {agent.version}, '
- 'with provider=startup-provider and model=gpt-startup'
+ f'Starting a browser-use agent with version {agent.version}, with provider=startup-provider and model=gpt-startup'
) in logger.infos
return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
@@ -5336,9 +5328,7 @@ async def test_rust_agent_exposes_step_finalization_helper_methods(tmp_path):
assert Path(agent.history.history[0].state.screenshot_path).read_bytes() == b'png-bytes'
assert [type(event).__name__ for event in agent.eventbus.dispatched] == ['CreateAgentStepEvent']
assert agent.eventbus.dispatched[0].agent_task_id == str(agent.task_id)
- assert agent.eventbus.dispatched[0].actions == [
- {'done': {'text': 'final answer', 'success': True, 'files_to_display': []}}
- ]
+ assert agent.eventbus.dispatched[0].actions == [{'done': {'text': 'final answer', 'success': True, 'files_to_display': []}}]
assert agent.eventbus.dispatched[0].url == 'https://example.com/final'
assert agent.eventbus.dispatched[0].screenshot_url.startswith('data:image/jpeg;base64,')
@@ -5510,7 +5500,9 @@ async def test_rust_agent_post_process_logs_browser_use_result_messages(monkeypa
assert logger.debugs == [f'🔄 Step {agent.state.n_steps}: Consecutive failures: 1']
agent.state.last_result = [
- ActionResult(is_done=True, success=True, extracted_content='final answer', attachments=['file:///tmp/a.txt', 'file:///tmp/b.txt'])
+ ActionResult(
+ is_done=True, success=True, extracted_content='final answer', attachments=['file:///tmp/a.txt', 'file:///tmp/b.txt']
+ )
]
await agent._post_process()
assert agent.state.consecutive_failures == 0
@@ -7006,9 +6998,7 @@ async def test_rust_agent_exposes_action_replay_helper_methods(monkeypatch):
async def get_browser_state_summary(self, include_screenshot=False):
assert include_screenshot is False
- return SimpleNamespace(
- dom_state=SimpleNamespace(selector_map={7: SimpleNamespace(element_hash='matching-hash')})
- )
+ return SimpleNamespace(dom_state=SimpleNamespace(selector_map={7: SimpleNamespace(element_hash='matching-hash')}))
class FakeAction:
def __init__(self):
@@ -7066,11 +7056,14 @@ async def test_rust_agent_exposes_action_replay_helper_methods(monkeypatch):
assert action.index == 7
assert replay_calls == [[action]]
assert results[0].extracted_content == 'replayed'
- assert await agent._update_action_indices(
- SimpleNamespace(element_hash='missing-hash'),
- FakeAction(),
- SimpleNamespace(dom_state=SimpleNamespace(selector_map={1: SimpleNamespace(element_hash='other-hash')})),
- ) is None
+ assert (
+ await agent._update_action_indices(
+ SimpleNamespace(element_hash='missing-hash'),
+ FakeAction(),
+ SimpleNamespace(dom_state=SimpleNamespace(selector_map={1: SimpleNamespace(element_hash='other-hash')})),
+ )
+ is None
+ )
async def test_rust_agent_exposes_model_output_helper_methods(monkeypatch, tmp_path):
@@ -7123,7 +7116,7 @@ async def test_rust_agent_exposes_model_output_helper_methods(monkeypatch, tmp_p
self.agent.ActionModel(done={'text': shortened_text, 'success': True}),
self.agent.ActionModel(done={'text': 'extra action', 'success': True}),
],
- )
+ ),
)
seen_callbacks = []
@@ -7177,7 +7170,7 @@ async def test_rust_agent_exposes_model_output_helper_methods(monkeypatch, tmp_p
memory='none',
next_goal='retry',
action=[],
- )
+ ),
)
return SimpleNamespace(
usage=None,
@@ -7186,7 +7179,7 @@ async def test_rust_agent_exposes_model_output_helper_methods(monkeypatch, tmp_p
memory='remember',
next_goal='finish',
action=[self.agent.ActionModel(done={'text': 'retried action', 'success': True})],
- )
+ ),
)
retry_llm = RetryLLM()
@@ -7696,6 +7689,20 @@ def test_rust_agent_trace_metadata_matches_browser_use_helpers(monkeypatch):
assert json.loads(trace['git_info']) == {'branch': 'trace-branch', 'commit_hash': 'abc123'}
+def test_rust_agent_initializes_action_models_without_conversation_path():
+ from browser_use.rust import Agent
+
+ agent = Agent(
+ task='Initialize action models without saving conversations.',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ directly_open_url=False,
+ )
+
+ assert agent.settings.save_conversation_path is None
+ assert agent.AgentOutput is not None
+ assert agent.DoneAgentOutput is not None
+
+
async def test_rust_agent_saves_terminal_conversation(tmp_path, monkeypatch):
from browser_use.rust import Agent