Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Record Rust eval Laminar summaries
Magnus Müllerf644efbcad5f
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex b499f9722..3a841e094 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -1198,6 +1198,16 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- The eval harness now initializes Laminar for trace-only runs when `LMNR_PROJECT_API_KEY` is present, records Laminar/GitHub links in run/task metadata, and defaults GitHub-dispatched max steps to 75.- Proof: terminal `browser_script_initial_wait_defaults_to_seven_seconds_and_clamps_env`, terminal `browser_script_start_observe_finishes_slow_scripts`, terminal `script_oversized_png_images_warn_in_stdout_without_media_payload`, terminal `bare_browser_connect_resolves_to_selected_remote_cdp_mode`, eval `py_compile eval/service.py eval/server.py eval/task_types.py`, terminal `py_compile crates/browser-use-browser/src/browser_script_helpers.py`, and terminal `cargo build -q -p browser-use-cli`.+237. Rust Agent eval finalization isolation parity+ - Terminal fallback capture GIF generation can be disabled with `BU_DISABLE_FALLBACK_CAPTURE_GIF=1`, and the eval runner sets this by default so the final `done` step is not delayed by nonessential media encoding.+ - Terminal browser_script output auto-collection can be scoped to a per-session artifact directory with `BU_BROWSER_SCRIPT_SESSION_OUTPUTS=1`, and the eval runner sets this by default to prevent parallel tasks from attaching each other's cwd files.+ - Proof: terminal `fallback_capture_recording_can_be_disabled_for_eval_runs`, terminal `browser_script_session_outputs_dir_isolates_parallel_cwd_files`, eval `py_compile eval/service.py eval/server.py eval/task_types.py`, and terminal `cargo build -q -p browser-use-cli`.++238. Rust Agent eval Laminar observability parity+ - Eval `evaluation`, `executor`, `stage.*`, and `evaluate_task_result` spans now receive explicit Laminar attributes and compact outputs instead of relying on ignored decorator input/output.+ - The Rust-backed Python `Agent.run()` now mirrors the normal Agent's `agent.run` observability entry point and records terminal-session summaries into the active Laminar span.+ - Proof: `uv run pytest -q tests/ci/test_rust_agent.py -k laminar_run_summary --disable-warnings`, `uv run python -m py_compile browser_use/rust/service.py tests/ci/test_rust_agent.py`, and eval `.venv/bin/python -m py_compile eval/service.py eval/server.py eval/task_types.py`.+## 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 browser_use/llm/models.py tests/ci/test_rust_agent.py tests/ci/models/test_llm_model_factory.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 5bd0587d5..43bebc780 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -69,6 +69,7 @@ from browser_use.utils import (check_latest_browser_use_version,get_browser_use_version,get_git_info,+ is_placeholder_url,time_execution_async,time_execution_sync,)@@ -1506,6 +1507,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):# Remove trailing punctuation that's not part of URLsurl = re.sub(r'[.,;:!?()\[\]]+$', '', url)+ if is_placeholder_url(url):+ self.logger.debug(f'Excluding placeholder URL from auto-navigation: {url}')+ continue+# Check if URL ends with a file extension that should be excludedurl_lower = url.lower()should_exclude = False@@ -2325,12 +2330,23 @@ def _align_rust_agent_signatures() -> None:continuetry:rust_method.__signature__ = inspect.signature(browser_use_method)+ rust_method.__annotations__ = dict(getattr(browser_use_method, '__annotations__', {}))except (TypeError, ValueError, AttributeError):continuetry: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/code_use/utils.py b/browser_use/code_use/utils.pyindex 8c00193fd..cddb1b6d7 100644--- a/browser_use/code_use/utils.py+++ b/browser_use/code_use/utils.py@@ -2,6 +2,8 @@import re+from browser_use.utils import is_placeholder_url+def truncate_message_content(content: str, max_length: int = 10000) -> str:"""Truncate message content to max_length characters for history."""@@ -61,6 +63,8 @@ def extract_url_from_task(task: str) -> str | None:# Remove trailing punctuation that's not part of URLsurl = re.sub(r'[.,;:!?()\[\]]+$', '', url)+ if is_placeholder_url(url):+ continue# Add https:// if missingif not url.startswith(('http://', 'https://')):url = 'https://' + urldiff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex e0b024c3e..54107bd11 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -51,6 +51,7 @@ from browser_use.dom.views import DOMInteractedElementfrom browser_use.filesystem.file_system import FileSystemfrom browser_use.llm.base import BaseChatModelfrom browser_use.llm.messages import BaseMessage, ContentPartImageParam, ContentPartTextParam+from browser_use.observability import observefrom browser_use.screenshots.service import ScreenshotServicefrom browser_use.telemetry.service import ProductTelemetryfrom browser_use.telemetry.views import AgentTelemetryEvent@@ -65,6 +66,7 @@ from browser_use.utils import (check_latest_browser_use_version,get_browser_use_version,get_git_info,+ is_placeholder_url,)@@ -77,11 +79,73 @@ AgentNewStepCallback = (AgentDoneCallback = Callable[[AgentHistoryList], Awaitable[None]] | Callable[[AgentHistoryList], None]logger = logging.getLogger(__name__)+try:+ from lmnr import Laminar # type: ignore+except ImportError:+ Laminar = None # type: ignore+class RustAgentError(RuntimeError):"""Raised when the Rust terminal core cannot run a task."""+def _laminar_ready() -> bool:+ if Laminar is None:+ return False+ try:+ return bool(Laminar.is_initialized())+ except Exception:+ return False+++def _laminar_preview(value: Any, limit: int = 2000) -> Any:+ if value is None or isinstance(value, (bool, int, float)):+ return value+ text = value if isinstance(value, str) else json.dumps(value, default=str)+ if len(text) <= limit:+ return text+ return text[:limit] + f'...[truncated {len(text) - limit} chars]'+++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))+ }+ if not safe_attributes:+ return+ try:+ Laminar.set_span_attributes(safe_attributes)+ except Exception:+ logger.debug('Failed to set Laminar span attributes', exc_info=True)+++def _laminar_set_span_output(output: Any) -> None:+ if not _laminar_ready():+ return+ try:+ Laminar.set_span_output(output)+ except Exception:+ logger.debug('Failed to set Laminar span output', exc_info=True)+++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))+ }+ try:+ Laminar.event(name, safe_attributes or None)+ except Exception:+ logger.debug('Failed to emit Laminar event %s', name, exc_info=True)++def find_browser_use_terminal_binary() -> str:"""Find the terminal binary used by the Rust-backed Browser Use Agent."""env_path = os.environ.get('BROWSER_USE_TERMINAL_BINARY')@@ -804,6 +868,8 @@ def _extract_start_url(task: str) -> str | None:for match in re.finditer(pattern, task_without_emails):url = re.sub(r'[.,;:!?()\[\]]+$', '', match.group(0))url_lower = url.lower()+ if is_placeholder_url(url):+ continueif any(f'.{ext}' in url_lower for ext in excluded_extensions):continuecontext_start = max(0, match.start() - 20)@@ -2931,6 +2997,73 @@ class Agent(Generic[Context, AgentStructuredOutput]):except Exception as exc:self.logger.error(f'Failed to log telemetry event: {exc}', exc_info=True)+ def _record_laminar_run_observability(+ self,+ *,+ max_steps: int,+ duration_seconds: float | None,+ process_error: str | None = None,+ ) -> None:+ """Populate the current Laminar agent.run span for terminal-backed runs."""+ if not _laminar_ready():+ return+ errors = self.history.errors()+ usage = self.history.usage+ final_result = self.history.final_result()+ model = getattr(self.llm, 'model', None) or self.model+ provider = getattr(self.llm, 'provider', None) or 'rust-terminal'+ summary = {+ 'runtime': 'browser_use.rust',+ 'model': model,+ 'provider': provider,+ 'max_steps': max_steps,+ 'steps': self.history.number_of_steps(),+ 'is_done': self.history.is_done(),+ 'is_successful': self.history.is_successful(),+ 'errors_count': len(errors or []),+ 'errors_preview': _laminar_preview(errors, limit=1500),+ 'final_result_preview': _laminar_preview(final_result, limit=2000),+ 'terminal_session_id': self.terminal_session_id,+ 'browser_use_session_id': self.session_id,+ 'terminal_events_count': len(self.last_events or []),+ 'duration_seconds': duration_seconds,+ 'process_error': process_error,+ }+ if usage is not None:+ summary.update(+ {+ 'usage_total_tokens': usage.total_tokens,+ 'usage_prompt_tokens': usage.total_prompt_tokens,+ 'usage_completion_tokens': usage.total_completion_tokens,+ }+ )+ _laminar_set_span_attributes(+ {+ 'runtime': summary['runtime'],+ 'model': model,+ 'provider': provider,+ 'max_steps': max_steps,+ 'steps': summary['steps'],+ 'is_done': summary['is_done'],+ 'is_successful': summary['is_successful'],+ 'terminal_session_id': self.terminal_session_id,+ 'terminal_events_count': summary['terminal_events_count'],+ 'duration_seconds': duration_seconds,+ 'process_error': process_error,+ }+ )+ _laminar_set_span_output(summary)+ _laminar_event(+ 'agent.run.terminal_summary',+ {+ 'runtime': summary['runtime'],+ 'steps': summary['steps'],+ 'is_successful': summary['is_successful'],+ 'duration_seconds': duration_seconds,+ 'terminal_events_count': summary['terminal_events_count'],+ },+ )+async def _log_run_usage_summary(self) -> None:"""Log Browser Use token usage summary for a completed Rust-backed run."""await self.token_cost_service.log_usage_summary()@@ -3018,6 +3151,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):if not hasattr(self, '_task_start_time') or not hasattr(self, '_session_start_time'):self._initialize_run_lifecycle_state()await self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=max_steps,+ duration_seconds=None,+ process_error=agent_run_error,+ )self._record_run_telemetry(max_steps=max_steps, agent_run_error=agent_run_error)self._dispatch_run_update_event()self._log_final_outcome_messages()@@ -3049,6 +3187,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):else:self.logger.info(f' {action_header}')+ @observe(name='agent.run', ignore_input=True, ignore_output=True)async def run(self,max_steps: int = 100,@@ -3093,6 +3232,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):)self.history = self.resultawait self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=max_steps,+ duration_seconds=finished - started,+ process_error='Rust agent stopped before terminal run.',+ )self._record_run_telemetry(max_steps=max_steps, agent_run_error='Rust agent stopped before terminal run.')self._dispatch_run_update_event()self._log_final_outcome_messages()@@ -3117,6 +3261,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):)self.history = self.resultawait self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=max_steps,+ duration_seconds=finished - started,+ process_error='Rust agent stopped before terminal run.',+ )self._record_run_telemetry(max_steps=max_steps, agent_run_error='Rust agent stopped before terminal run.')self._dispatch_run_update_event()self._log_final_outcome_messages()@@ -3136,17 +3285,59 @@ class Agent(Generic[Context, AgentStructuredOutput]):await self._call_callback(on_step_start, self)self._log_main_execution_start(max_steps)- returncode, stdout_text, stderr_text = await self._run_process(self._run_argv(max_steps), timeout_seconds=self.settings.step_timeout)+ start_returncode, start_stdout_text, start_stderr_text = await self._run_process(+ self._start_argv(), timeout_seconds=self.settings.step_timeout+ )+ self.terminal_session_id = self._session_id_from_stdout(start_stdout_text)+ if start_returncode or not self.terminal_session_id:+ finished = time.time()+ self.last_stdout = start_stdout_text+ self.last_stderr = start_stderr_text+ events = await self._load_events() if self.terminal_session_id else []+ process_error = start_stderr_text.strip() or (+ f'browser-use-terminal exited with code {start_returncode}'+ if start_returncode+ else 'browser-use-terminal did not print a session id.'+ )+ self.last_events = events+ self.result = _history_from_events(+ events,+ model=self.model,+ started=started,+ finished=finished,+ output_model_schema=self.output_model_schema,+ process_error=process_error,+ )+ self.history = self.result+ self._sync_state_from_history()+ await self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=max_steps,+ duration_seconds=finished - started,+ process_error=process_error,+ )+ self._record_run_telemetry(max_steps=max_steps, agent_run_error=process_error)+ self._dispatch_run_update_event()+ await self._check_and_update_downloads('run')+ await self._save_conversation_if_requested()+ await self._call_new_step_callback()+ await self._call_step_end_callbacks(on_step_end)+ await self._call_done_callback()+ await self._generate_gif_if_requested()+ self._log_final_outcome_messages()+ await self._finalize_run_cleanup()+ return self.history++ returncode, stdout_text, stderr_text = await self._run_process(+ self._run_existing_argv(max_steps), timeout_seconds=self.settings.step_timeout+ )finished = time.time()- self.last_stdout = stdout_text- self.last_stderr = stderr_text- self.terminal_session_id = self._session_id_from_stdout(stdout_text)+ self.last_stdout = '\n'.join(part for part in (start_stdout_text.strip(), stdout_text.strip()) if part)+ self.last_stderr = '\n'.join(part for part in (start_stderr_text.strip(), stderr_text.strip()) if part)events = await self._load_events()process_error = Noneif returncode:process_error = stderr_text.strip() or f'browser-use-terminal exited with code {returncode}'- elif not self.terminal_session_id:- process_error = 'browser-use-terminal did not print a session id.'self.last_events = eventsself.result = _history_from_events(events,@@ -3159,6 +3350,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.history = self.resultself._sync_state_from_history()await self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=max_steps,+ duration_seconds=finished - started,+ process_error=process_error,+ )self._record_run_telemetry(max_steps=max_steps, agent_run_error=process_error)self._dispatch_run_update_event()await self._check_and_update_downloads('run')@@ -3234,6 +3430,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.history = self.resultself._sync_state_from_history()await self._log_run_usage_summary()+ self._record_laminar_run_observability(+ max_steps=resolved_max_steps,+ duration_seconds=finished - started,+ process_error=process_error,+ )self._record_run_telemetry(max_steps=resolved_max_steps, agent_run_error=process_error)self._dispatch_run_update_event()await self._check_and_update_downloads('follow_up')@@ -4166,6 +4367,15 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.model,]+ def _start_argv(self) -> list[str]:+ binary = find_browser_use_terminal_binary()+ return [+ binary,+ *self._state_dir_args(),+ 'start',+ self.task,+ ]+async def _run_process(self, argv: list[str], timeout_seconds: int | None = None) -> tuple[int, str, str]:proc = await asyncio.create_subprocess_exec(*argv,diff --git a/browser_use/utils.py b/browser_use/utils.pyindex efd4d2a18..d822ae5e3 100644--- a/browser_use/utils.py+++ b/browser_use/utils.py@@ -24,6 +24,20 @@ URL_PATTERN = re.compile(r'https?://[^\s<>"\']+|www\.[^\s<>"\']+|[^\s<>"\']+\.[alogger = logging.getLogger(__name__)++def is_placeholder_url(url: str) -> bool:+ """Return True for mock placeholder hostnames like https://XXX.XX."""+ parsed_url = urlparse(url if '://' in url else f'https://{url}')+ hostname = (parsed_url.hostname or '').strip('.').lower()+ if not hostname:+ return False++ labels = [label for label in hostname.split('.') if label]+ if labels and labels[0] == 'www':+ labels = labels[1:]++ return len(labels) >= 2 and all(re.fullmatch(r'x+', label) for label in labels)+# Import error types - these may need to be adjusted based on actual import pathstry:from openai import BadRequestError as OpenAIBadRequestErrordiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 783e64ee3..3b7ae3e19 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2712,6 +2712,8 @@ def test_rust_agent_logs_direct_url_startup_like_browser_use(monkeypatch):BrowserUseAgent(task='Open example.com and report the title.', llm=LLM())RustAgent(task='Open example.com and report the title.', llm=LLM())+ BrowserUseAgent(task='Use https://XXX.XX only as an example price placeholder.', llm=LLM())+ RustAgent(task='Use https://XXX.XX only as an example price placeholder.', llm=LLM())expected = ['🔗 Found URL in task: https://example.com, adding as initial action...']assert browser_use_logger.infos == expected@@ -2719,12 +2721,21 @@ def test_rust_agent_logs_direct_url_startup_like_browser_use(monkeypatch):def test_rust_agent_exposes_task_helper_methods():+ from browser_use.agent.service import _PythonAgent as BrowserUseAgentfrom browser_use.rust import Agentclass Answer(BaseModel):answer: str+ class LLM:+ model = 'gpt-test'+ provider = 'test'++ async def ainvoke(self, messages, output_format=None, **kwargs):+ return type('Result', (), {'usage': None})()+agent = Agent(task='Open example.com and report the title.')+ browser_use_agent = BrowserUseAgent(task='Inspect URL extraction.', llm=LLM(), directly_open_url=False)enhanced = agent._enhance_task_with_schema('Return the answer.', Answer)@@ -2734,6 +2745,8 @@ def test_rust_agent_exposes_task_helper_methods():assert agent._extract_start_url('Open example.com and report the title.') == 'https://example.com'assert agent._extract_start_url('Email [email protected] only.') is Noneassert 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 Nonedef test_rust_agent_exposes_url_text_helper_methods():@@ -3741,7 +3754,7 @@ async def test_rust_agent_run_follow_up_logs_main_execution_start(monkeypatch):process_calls.append(argv)if len(process_calls) == 1:return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''- if len(process_calls) == 2:+ if len(process_calls) == 3:assert argv[-3] == 'followup'assert 'Starting main execution loop with max 5 steps...' in logger.debugsreturn 0, '', ''@@ -3763,7 +3776,7 @@ async def test_rust_agent_run_follow_up_logs_main_execution_start(monkeypatch):history = await agent.run(max_steps=5)assert history.final_result() == 'logged follow-up execution 2'- assert len(process_calls) == 3+ assert len(process_calls) == 4assert logger.errors == []@@ -3812,16 +3825,20 @@ async def test_rust_agent_run_registers_browser_use_signal_handler(monkeypatch):monkeypatch.setattr(rust_service, 'SignalHandler', FakeSignalHandler)agent = Agent(task='Handle run signals.', llm=type('LLM', (), {'model': 'gpt-test'})())agent.telemetry = Telemetry()+ process_calls = 0async def fake_run_process(argv, timeout_seconds=None):- assert events == ['init', 'register']+ nonlocal process_calls+ process_calls += 1+ assert events in (['init', 'register'], ['init', 'register', 'flush'])assert len(handlers) == 1assert handlers[0].exit_on_second_int is True- handlers[0].pause_callback()- assert agent.state.paused is True- handlers[0].resume_callback()- assert agent.state.paused is False- handlers[0].custom_exit_callback()+ if process_calls == 1:+ handlers[0].pause_callback()+ assert agent.state.paused is True+ handlers[0].resume_callback()+ assert agent.state.paused is False+ handlers[0].custom_exit_callback()return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():@@ -3901,6 +3918,7 @@ async def test_rust_agent_run_waits_for_resume_before_terminal(monkeypatch):'reset_signal',('start', False),('run_process', False, agent.settings.step_timeout),+ ('run_process', False, agent.settings.step_timeout),('end', 'resumed run'),'unregister_signal',]@@ -4656,7 +4674,7 @@ async def test_rust_agent_run_dispatches_browser_use_lifecycle_events(monkeypatcmonkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')dispatched = []- run_count = 0+ load_count = 0class EventBus:def dispatch(self, event):@@ -4667,12 +4685,12 @@ async def test_rust_agent_run_dispatches_browser_use_lifecycle_events(monkeypatcagent.eventbus = EventBus()async def fake_run_process(argv, timeout_seconds=None):- nonlocal run_count- run_count += 1return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():- return [{'event_type': 'session.done', 'payload': {'result': f'dispatched {run_count}'}}]+ nonlocal load_count+ load_count += 1+ return [{'event_type': 'session.done', 'payload': {'result': f'dispatched {load_count}'}}]agent._run_process = fake_run_processagent._load_events = fake_load_events@@ -4746,8 +4764,8 @@ async def test_rust_agent_run_follow_up_dispatches_single_task_lifecycle(monkeyphistory = await agent.run(max_steps=4)assert history.final_result() == 'lifecycle follow-up 2'- assert len(process_calls) == 3- assert process_calls[1][-3] == 'followup'+ assert len(process_calls) == 4+ assert process_calls[2][-3] == 'followup'assert [type(event).__name__ for event in dispatched] == ['CreateAgentTaskEvent', 'UpdateAgentTaskEvent']assert dispatched[0].llm_model == 'gpt-test'assert dispatched[1].done_output == 'lifecycle follow-up 2'@@ -4794,8 +4812,8 @@ async def test_rust_agent_run_follow_up_invokes_on_step_end(monkeypatch):assert history.final_result() == 'callback follow-up 2'assert callbacks == [('start', 'Continue follow-up callbacks.'), ('end', 'callback follow-up 2')]- assert len(process_calls) == 3- assert process_calls[1][-3] == 'followup'+ assert len(process_calls) == 4+ assert process_calls[2][-3] == 'followup'async def test_rust_agent_direct_follow_up_updates_task_state_and_transcript(tmp_path, monkeypatch):@@ -4822,7 +4840,7 @@ async def test_rust_agent_direct_follow_up_updates_task_state_and_transcript(tmpprocess_calls.append(argv)if len(process_calls) == 1:return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''- if len(process_calls) == 2:+ if len(process_calls) == 3:return 0, 'followup accepted\n', 'followup stderr\n'return 0, 'rerun stdout\n', 'rerun stderr\n'@@ -4842,7 +4860,7 @@ async def test_rust_agent_direct_follow_up_updates_task_state_and_transcript(tmpassert agent.task == 'Direct raw follow-up task.'assert agent.state.follow_up_task is Falseassert '<follow_up_user_request> Direct raw follow-up task. </follow_up_user_request>' in agent.message_manager.task- assert process_calls[1][-3:] == [+ assert process_calls[2][-3:] == ['followup','12345678-1234-1234-1234-123456789abc','Direct raw follow-up task.',@@ -5298,7 +5316,7 @@ async def test_rust_agent_add_new_task_preserves_browser_use_raw_followup_task(mawait agent.run(max_steps=2)- assert process_calls[1][-3:] == ['followup', '12345678-1234-1234-1234-123456789abc', 'Follow up with raw text.']+ assert process_calls[2][-3:] == ['followup', '12345678-1234-1234-1234-123456789abc', 'Follow up with raw text.']def test_rust_agent_initializes_browser_use_session_and_file_system(tmp_path):@@ -5521,6 +5539,41 @@ async def test_rust_agent_keeps_browser_use_session_id_separate_from_terminal_seassert agent.session_id != agent.terminal_session_id+async def test_rust_agent_loads_started_session_events_after_terminal_timeout(monkeypatch):+ from browser_use.rust import Agent++ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')+ agent = Agent(task='timeout after start', llm=type('LLM', (), {'model': 'gpt-test'})(), step_timeout=5)+ process_commands = []+ stored_events = [+ {'event_type': 'browser.state', 'payload': {'url': 'https://example.com', 'title': 'Example'}},+ {'event_type': 'model.usage', 'payload': {'input_tokens': 11, 'output_tokens': 7}},+ ]++ async def fake_run_process(argv, timeout_seconds=None):+ process_commands.append(argv[-2] if argv[-2:] == ['start', agent.task] else argv[-4])+ if process_commands[-1] == 'start':+ return 0, '12345678-1234-1234-1234-123456789abc\n', ''+ return 124, '', 'browser-use-terminal timed out after 5 seconds'++ async def fake_load_events():+ assert agent.terminal_session_id == '12345678-1234-1234-1234-123456789abc'+ return stored_events++ agent._run_process = fake_run_process+ agent._load_events = fake_load_events++ history = await agent.run(max_steps=3)++ assert process_commands == ['start', 'run-codex-session']+ assert agent.terminal_session_id == '12345678-1234-1234-1234-123456789abc'+ assert agent.last_events == stored_events+ assert history.urls() == ['https://example.com']+ assert history.usage is not None+ assert history.usage.total_prompt_tokens == 11+ assert history.errors() == ['browser-use-terminal timed out after 5 seconds']++async def test_rust_agent_invokes_browser_use_style_callbacks(monkeypatch):from browser_use.rust import Agent@@ -5534,7 +5587,8 @@ async def test_rust_agent_invokes_browser_use_style_callbacks(monkeypatch):async def fake_run_process(argv, timeout_seconds=None):assert timeout_seconds == agent.settings.step_timeout- seen.append(('argv', argv[-4]))+ command = 'start' if argv[-2:] == ['start', agent.task] else argv[-4]+ seen.append(('argv', command))return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():@@ -5554,7 +5608,8 @@ async def test_rust_agent_invokes_browser_use_style_callbacks(monkeypatch):assert history.final_result() == 'ok'assert seen == [('start', True),- ('argv', 'run-codex'),+ ('argv', 'start'),+ ('argv', 'run-codex-session'),('end', True),('done', 'ok'),]@@ -6440,10 +6495,12 @@ async def test_rust_agent_step_runs_single_terminal_turn_and_updates_state(monkeresult = await agent.step()assert result is None- assert len(seen) == 1+ assert len(seen) == 2assert seen[0][0][0] == '/tmp/browser-use-terminal'- assert 'max_turns=1' in seen[0][0]+ assert seen[0][0][-2:] == ['start', 'step once']+ assert 'max_turns=1' in seen[1][0]assert seen[0][1] == agent.settings.step_timeout+ assert seen[1][1] == agent.settings.step_timeoutassert agent.history.final_result() == 'step answer'assert agent.state.last_result is not Noneassert agent.state.last_result[-1].is_done is True@@ -6467,7 +6524,8 @@ async def test_rust_agent_execute_step_runs_one_turn_with_callbacks(monkeypatch))async def fake_run_process(argv, timeout_seconds=None):- seen.append(('argv', argv[-4], timeout_seconds))+ command = 'start' if argv[-2:] == ['start', agent.task] else argv[-4]+ seen.append(('argv', command, timeout_seconds))return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''async def fake_load_events():@@ -6493,7 +6551,8 @@ async def test_rust_agent_execute_step_runs_one_turn_with_callbacks(monkeypatch)assert is_done is Trueassert seen == [('start', True),- ('argv', 'run-codex', agent.settings.step_timeout),+ ('argv', 'start', agent.settings.step_timeout),+ ('argv', 'run-codex-session', agent.settings.step_timeout),('end', True),('done', 'execute step answer'),]@@ -7009,6 +7068,59 @@ async def test_rust_agent_trace_and_cloud_auth_helpers():assert 'trace answer' in trace_object['trace_details']['complete_history']+def test_rust_agent_laminar_run_summary_populates_current_span(monkeypatch):+ from browser_use.rust import Agent+ import browser_use.rust.service as rust_service+ from browser_use.rust.service import _history_from_events++ class FakeLaminar:+ attributes = []+ outputs = []+ events = []++ @staticmethod+ def is_initialized():+ return True++ @classmethod+ def set_span_attributes(cls, attributes):+ cls.attributes.append(attributes)++ @classmethod+ def set_span_output(cls, output):+ cls.outputs.append(output)++ @classmethod+ def event(cls, name, attributes=None):+ cls.events.append((name, attributes))++ monkeypatch.setattr(rust_service, 'Laminar', FakeLaminar)++ agent = Agent(task='Trace terminal observability.', llm=type('LLM', (), {'model': 'gpt-test'})())+ agent.terminal_session_id = 'terminal-session-1'+ agent.last_events = [+ {'event_type': 'model.turn.completed', 'payload': {'turn': 1}},+ {'event_type': 'session.done', 'payload': {'result': 'laminar answer'}},+ ]+ agent.history = _history_from_events(+ agent.last_events,+ model='gpt-test',+ started=1.0,+ finished=4.0,+ output_model_schema=None,+ process_error=None,+ )++ agent._record_laminar_run_observability(max_steps=7, duration_seconds=3.0)++ assert FakeLaminar.attributes[-1]['runtime'] == 'browser_use.rust'+ assert FakeLaminar.attributes[-1]['terminal_session_id'] == 'terminal-session-1'+ assert FakeLaminar.outputs[-1]['final_result_preview'] == 'laminar answer'+ assert FakeLaminar.outputs[-1]['steps'] == agent.history.number_of_steps()+ assert FakeLaminar.outputs[-1]['terminal_events_count'] == 2+ assert FakeLaminar.events[-1][0] == 'agent.run.terminal_summary'++async def test_rust_agent_authenticate_cloud_sync_logs_browser_use_warning(monkeypatch):from browser_use.agent.service import _PythonAgent as BrowserUseAgentfrom browser_use.rust import Agent as RustAgent