Skip to content

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

Reconstruct Rust terminal exec command output

Magnus Müllerc58db672ac55
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index ab0c0b0c8..e0a2630f9 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -754,10 +754,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- 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`.
+149. Rust terminal exec-command end-output reconstruction
+ - Terminal `exec_command.end` final output is now reconstructed into Browser Use action results when no generic textual `tool.output` is present.
+ - This preserves current-main unified exec final stdout/stderr on Python-visible `ActionResult.extracted_content`, `long_term_memory`, and `action_history()` even if only the exec lifecycle events were recorded.
+ - Proof: `test_rust_history_reconstructs_terminal_exec_command_end_output`.
+
## 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` (120 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (121 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 ab160ca60..3381c29df 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1114,6 +1114,7 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
if event_type not in (
'tool.output',
'tool.output_delta',
+ 'exec_command.end',
'tool.failed',
'tool.aborted',
'tool.finished',
@@ -1127,6 +1128,8 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
delta_text = _tool_output_delta_text(payload)
if not delta_text:
continue
+ if event_type == 'exec_command.end' and not _tool_result_text(payload):
+ continue
call_id = payload.get('tool_call_id') or payload.get('call_id')
if call_id:
key = str(call_id)
@@ -1142,6 +1145,13 @@ 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 == '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':
continue
if event_type == 'tool.finished' and previous is not None and previous[0] != 'tool.finished':
@@ -1157,6 +1167,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
if event_type not in (
'tool.output',
'tool.output_delta',
+ 'exec_command.end',
'tool.failed',
'tool.aborted',
'tool.finished',
@@ -1206,6 +1217,26 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
if previous_delta_index is not None and payload.get('stream') is not True:
results[previous_delta_index] = (event_type, payload)
continue
+ elif event_type == 'exec_command.end':
+ if not _tool_result_text(payload):
+ continue
+ name = payload.get('name')
+ previous_result_index = next(
+ (
+ index
+ for index in range(len(results) - 1, -1, -1)
+ if isinstance(name, str)
+ and name
+ and results[index][1].get('name') == name
+ ),
+ None,
+ )
+ if previous_result_index is not None:
+ previous_event_type = results[previous_result_index][0]
+ if previous_event_type in ('tool.output', 'tool.failed', 'tool.aborted', 'model.response.input_item'):
+ continue
+ results[previous_result_index] = (event_type, payload)
+ continue
if event_type == 'tool.output' and payload.get('stream') is True:
continue
if event_type == 'tool.failed':
@@ -1496,10 +1527,10 @@ def _action_results_from_tool_calls(
ActionResult(
error=error,
extracted_content=text
- if event_type in ('tool.output', 'tool.output_delta', 'tool.finished', 'model.response.input_item')
+ if event_type in ('tool.output', 'tool.output_delta', 'exec_command.end', 'tool.finished', 'model.response.input_item')
else None,
long_term_memory=text
- if event_type in ('tool.output', 'tool.output_delta', 'tool.finished', 'model.response.input_item')
+ if event_type in ('tool.output', 'tool.output_delta', 'exec_command.end', 'tool.finished', 'model.response.input_item')
else None,
)
)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index e8d251435..42931bfba 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -660,6 +660,50 @@ def test_rust_history_reconstructs_terminal_tool_output_deltas():
assert history.final_result() == 'final answer'
+def test_rust_history_reconstructs_terminal_exec_command_end_output():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'arguments': {'cmd': "printf 'final stdout'"},
+ },
+ },
+ {
+ 'type': 'exec_command.end',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'process_id': '1000',
+ 'session_id': 1000,
+ 'exit_code': 0,
+ 'wall_time_seconds': 0.1,
+ 'output': 'final stdout\n',
+ },
+ },
+ {'event_type': 'session.done', 'payload': {'result': 'final answer'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ first_result = history.action_results()[0]
+
+ assert history.action_names() == ['exec_command', 'done']
+ assert history.model_actions()[0]['exec_command']['cmd'] == "printf 'final stdout'"
+ assert first_result.extracted_content == 'final stdout'
+ assert first_result.long_term_memory == 'final stdout'
+ assert history.action_history()[0][0]['result'] == 'final stdout'
+ assert history.final_result() == 'final answer'
+
+
def test_rust_history_reconstructs_terminal_unkeyed_tool_results():
from browser_use.rust.service import _history_from_events