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
Surface Rust terminal cancellation aborts
Magnus Müller073273c4341a
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex d86b00aa3..150f6dc83 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -664,10 +664,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- This makes `number_of_steps()`, `model_outputs()`, `model_thoughts()`, `action_history()`, `urls()`, screenshot paths, and final done state follow Browser Use step semantics while preserving collapsed behavior for event logs without terminal model-turn boundaries.- Proof: `test_rust_history_reconstructs_terminal_model_turn_steps`.+131. Rust terminal cancellation and tool-abort reconstruction+ - Terminal `session.cancelled` events are now reconstructed as Browser Use history errors instead of falling through to the generic missing-result fallback.+ - Terminal `tool.aborted` events now surface action-level abort messages and final history errors when no terminal completion result is available.+ - Proof: `test_rust_history_surfaces_terminal_cancellation_and_tool_abort_messages`.+## 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` (102 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (103 tests)- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 98a6ff034..ce6ef8fbb 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -822,6 +822,12 @@ def _failure_from_events(events: list[dict[str, Any]]) -> str | None:error = payload.get('error') or payload.get('message')if isinstance(error, str) and error:return error+ if event_type == 'session.cancelled':+ payload = _event_payload(event)+ reason = payload.get('reason') or payload.get('message') or payload.get('error')+ if isinstance(reason, str) and reason.strip():+ return f'Rust terminal session was cancelled: {reason.strip()}'+ return 'Rust terminal session was cancelled.'if event_type in ('agent.failed', 'agent.cancelled'):payload = _event_payload(event)inner_payload = payload.get('payload')@@ -841,6 +847,10 @@ def _recoverable_failure_from_events(events: list[dict[str, Any]]) -> str | Noneerror = _tool_failure_message(payload)if error:return error+ if event_type == 'tool.aborted':+ error = _tool_abort_message(payload)+ if error:+ return errorif event_type in ('model.turn.error', 'model.turn.context_overflow'):error = payload.get('error') or payload.get('message') or payload.get('reason')if isinstance(error, str) and error.strip():@@ -856,6 +866,14 @@ def _tool_failure_message(payload: dict[str, Any]) -> str | None:return f'{name} failed: {error.strip()}' if isinstance(name, str) and name else error.strip()+def _tool_abort_message(payload: dict[str, Any]) -> str | None:+ error = payload.get('error') or payload.get('reason') or payload.get('message')+ if not isinstance(error, str) or not error.strip():+ error = 'aborted'+ name = payload.get('name')+ return f'{name} aborted: {error.strip()}' if isinstance(name, str) and name else error.strip()++def _safe_tool_action_name(value: Any) -> str | None:if not isinstance(value, str):return None@@ -921,7 +939,7 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[stresults: dict[str, tuple[str, dict[str, Any]]] = {}for event in events:event_type = _event_type(event)- if event_type not in ('tool.output', 'tool.failed'):+ if event_type not in ('tool.output', 'tool.failed', 'tool.aborted'):continuepayload = _event_payload(event)call_id = payload.get('tool_call_id') or payload.get('call_id')@@ -1019,7 +1037,12 @@ def _action_results_from_tool_calls(for tool_call in tool_calls:event_type, payload = tool_results.get(str(tool_call.get('tool_call_id')), ('', {}))text = _tool_result_text(payload)- error = _tool_failure_message(payload) if event_type == 'tool.failed' else None+ if event_type == 'tool.failed':+ error = _tool_failure_message(payload)+ elif event_type == 'tool.aborted':+ error = _tool_abort_message(payload)+ else:+ error = Noneaction_results.append(ActionResult(error=error,diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex eb4393678..f894b8c38 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2954,6 +2954,60 @@ def test_rust_history_surfaces_terminal_tool_failure_message():assert history.action_results()[-1].error == 'browser_script failed: RuntimeError: navigation failed'+def test_rust_history_surfaces_terminal_cancellation_and_tool_abort_messages():+ from browser_use.rust.service import _history_from_events++ cancelled = _history_from_events(+ [+ {'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}},+ {'event_type': 'session.cancelled', 'payload': {'reason': 'user requested stop'}},+ ],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ aborted = _history_from_events(+ [+ {'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}},+ {+ 'type': 'tool.started',+ 'payload': {+ 'name': 'browser',+ 'tool_call_id': 'call-browser',+ 'arguments': {'cmd': 'connect managed --headless'},+ },+ },+ {+ 'type': 'tool.aborted',+ 'payload': {+ 'name': 'browser',+ 'tool_call_id': 'call-browser',+ 'error': 'aborted by user',+ },+ },+ ],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ assert cancelled.final_result() is None+ assert cancelled.is_done() is False+ assert cancelled.errors() == ['Rust terminal session was cancelled: user requested stop']+ assert cancelled.action_results()[-1].error == 'Rust terminal session was cancelled: user requested stop'++ assert aborted.final_result() is None+ assert aborted.is_done() is False+ assert aborted.errors() == ['browser aborted: aborted by user']+ assert aborted.action_results()[0].error == 'browser aborted: aborted by user'+ assert aborted.action_results()[-1].error == 'browser aborted: aborted by user'++def test_real_v8_smoke_selects_case_by_index_and_task_id(tmp_path):module = _load_real_v8_smoke_module()dataset = tmp_path / 'real_v8.json'