Skip to content

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

Preserve Rust terminal tool abort precedence

Magnus Müller3324f2a70fea
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index cf68b9f38..ab0c0b0c8 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -749,10 +749,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This preserves current-main unified exec streamed stdout/stderr on Python-visible `ActionResult.extracted_content`, `long_term_memory`, and `action_history()` instead of falling back to synthetic tool-completion text.
- Proof: `test_rust_history_reconstructs_terminal_tool_output_deltas`.
+148. Rust terminal paired tool-abort precedence
+ - Terminal tool calls that emit `tool.aborted` followed by the paired `tool.failed` now keep the abort-specific Browser Use error.
+ - This preserves current-main user-aborted tool semantics on Python-visible `history.errors()`, per-action `ActionResult.error`, and final fallback errors instead of downgrading them to generic tool-failure messages.
+ - Proof: `test_rust_history_preserves_terminal_tool_abort_when_failed_event_follows`.
+
## 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` (119 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (120 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.py
index e9f6c1559..ab160ca60 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -955,7 +955,8 @@ def _recoverable_failure_from_events(events: list[dict[str, Any]]) -> str | None
event_type = _event_type(event)
payload = _event_payload(event)
if event_type == 'tool.failed':
- error = _tool_failure_message(payload)
+ abort_payload = _matching_tool_abort_payload(events, payload)
+ error = _tool_abort_message(abort_payload) if abort_payload is not None else _tool_failure_message(payload)
if error:
return error
if event_type == 'tool.aborted':
@@ -985,6 +986,29 @@ def _tool_abort_message(payload: dict[str, Any]) -> str | None:
return f'{name} aborted: {error.strip()}' if isinstance(name, str) and name else error.strip()
+def _tool_result_key(payload: dict[str, Any]) -> tuple[str, str] | None:
+ call_id = payload.get('tool_call_id') or payload.get('call_id')
+ if call_id:
+ return ('call_id', str(call_id))
+ name = payload.get('name')
+ if isinstance(name, str) and name:
+ return ('name', name)
+ return None
+
+
+def _matching_tool_abort_payload(events: list[dict[str, Any]], payload: dict[str, Any]) -> dict[str, Any] | None:
+ key = _tool_result_key(payload)
+ if key is None:
+ return None
+ for event in events:
+ if _event_type(event) != 'tool.aborted':
+ continue
+ abort_payload = _event_payload(event)
+ if _tool_result_key(abort_payload) == key:
+ return abort_payload
+ return None
+
+
def _safe_tool_action_name(value: Any) -> str | None:
if not isinstance(value, str):
return None
@@ -1118,6 +1142,8 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
merged_payload['text'] = f'{previous_text if isinstance(previous_text, str) else ""}{delta_text}'
results[key] = (event_type, merged_payload)
continue
+ if event_type == 'tool.failed' and previous is not None and previous[0] == 'tool.aborted':
+ continue
if event_type == 'tool.finished' and previous is not None and previous[0] != 'tool.finished':
continue
results[key] = (event_type, payload)
@@ -1182,6 +1208,21 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
continue
if event_type == 'tool.output' and payload.get('stream') is True:
continue
+ if event_type == 'tool.failed':
+ name = payload.get('name')
+ previous_abort = next(
+ (
+ result
+ for result in reversed(results)
+ if isinstance(name, str)
+ and name
+ and result[0] == 'tool.aborted'
+ and result[1].get('name') == name
+ ),
+ None,
+ )
+ if previous_abort is not None:
+ continue
if event_type == 'tool.finished':
name = payload.get('name')
previous = next(
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 57173d293..e8d251435 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3800,6 +3800,51 @@ def test_rust_history_surfaces_terminal_cancellation_and_tool_abort_messages():
assert aborted.action_results()[-1].error == 'browser aborted: aborted by user'
+def test_rust_history_preserves_terminal_tool_abort_when_failed_event_follows():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}},
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'arguments': {'cmd': 'sleep 60'},
+ },
+ },
+ {
+ 'type': 'tool.aborted',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'error': 'aborted by user after 1.0s',
+ },
+ },
+ {
+ 'type': 'tool.failed',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'error': 'aborted by user after 1.0s',
+ },
+ },
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ assert history.final_result() is None
+ assert history.is_done() is False
+ assert history.errors() == ['exec_command aborted: aborted by user after 1.0s']
+ assert history.action_results()[0].error == 'exec_command aborted: aborted by user after 1.0s'
+ assert history.action_results()[-1].error == 'exec_command aborted: aborted by user after 1.0s'
+
+
def test_rust_history_surfaces_terminal_session_interrupted_message():
from browser_use.rust.service import _history_from_events