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 deltas

Magnus Müller5e75ed667da5
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index a5d24a780..a085773f0 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -769,10 +769,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This preserves current-main long-running command state 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_command_waiting_result`.
+152. Rust terminal exec-command output-delta reconstruction
+ - Terminal `exec_command.output_delta` events now reconstruct live unified-exec process output when no generic final `tool.output` is present.
+ - This preserves current-main exec-specific streamed `chunk` payloads on Python-visible `ActionResult.extracted_content`, `long_term_memory`, and `action_history()` while avoiding duplicate text when terminal also emits the paired generic `tool.output_delta`.
+ - Proof: `test_rust_history_reconstructs_terminal_exec_command_output_deltas`.
+
## 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` (123 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (124 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 e110d53a8..b0e9f3eac 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1020,9 +1020,13 @@ def _command_waiting_payload(payload: dict[str, Any], previous_payload: dict[str
return next_payload
+_TOOL_OUTPUT_DELTA_EVENTS = ('tool.output_delta', 'exec_command.output_delta')
+
+
_TEXTUAL_TOOL_RESULT_EVENTS = (
'tool.output',
'tool.output_delta',
+ 'exec_command.output_delta',
'command.waiting',
'exec_command.end',
'tool.finished',
@@ -1030,6 +1034,29 @@ _TEXTUAL_TOOL_RESULT_EVENTS = (
)
+def _is_redundant_paired_output_delta(
+ previous_event_type: str,
+ previous_payload: dict[str, Any],
+ event_type: str,
+ payload: dict[str, Any],
+ delta_text: str,
+) -> bool:
+ if previous_event_type not in _TOOL_OUTPUT_DELTA_EVENTS or previous_event_type == event_type:
+ return False
+ previous_text = previous_payload.get('text')
+ if not isinstance(previous_text, str) or not previous_text.endswith(delta_text):
+ return False
+ previous_stream = previous_payload.get('stream')
+ stream = payload.get('stream')
+ if previous_stream is not None and stream is not None and previous_stream != stream:
+ return False
+ previous_session_id = previous_payload.get('session_id') or previous_payload.get('process_id')
+ session_id = payload.get('session_id') or payload.get('process_id')
+ if previous_session_id is not None and session_id is not None and str(previous_session_id) != str(session_id):
+ return False
+ return True
+
+
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:
@@ -1164,6 +1191,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.output_delta',
'command.waiting',
'exec_command.end',
'tool.failed',
@@ -1175,7 +1203,7 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
payload = _event_payload(event)
if event_type == 'model.response.input_item':
payload = _response_input_item_tool_payload(payload)
- if event_type == 'tool.output_delta':
+ if event_type in _TOOL_OUTPUT_DELTA_EVENTS:
delta_text = _tool_output_delta_text(payload)
if not delta_text:
continue
@@ -1185,11 +1213,17 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
if call_id:
key = str(call_id)
previous = results.get(key)
- if event_type == 'tool.output_delta':
- if previous is not None and previous[0] not in ('tool.output_delta', 'tool.finished'):
+ if event_type in _TOOL_OUTPUT_DELTA_EVENTS:
+ if previous is not None and previous[0] not in (*_TOOL_OUTPUT_DELTA_EVENTS, 'tool.finished'):
+ continue
+ if (
+ previous is not None
+ and delta_text
+ 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] == 'tool.output_delta' else dict(payload)
- previous_text = merged_payload.get('text') if previous is not None and previous[0] == 'tool.output_delta' 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
@@ -1229,6 +1263,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.output_delta',
'command.waiting',
'exec_command.end',
'tool.failed',
@@ -1242,7 +1277,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
payload = _response_input_item_tool_payload(payload)
if payload.get('tool_call_id') or payload.get('call_id'):
continue
- if event_type == 'tool.output_delta':
+ if event_type in _TOOL_OUTPUT_DELTA_EVENTS:
delta_text = _tool_output_delta_text(payload)
if not delta_text:
continue
@@ -1253,7 +1288,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
for result in reversed(results)
if isinstance(name, str)
and name
- and result[0] == 'tool.output_delta'
+ and result[0] in _TOOL_OUTPUT_DELTA_EVENTS
and result[1].get('name') == name
),
None,
@@ -1272,7 +1307,7 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
for index in range(len(results) - 1, -1, -1)
if isinstance(name, str)
and name
- and results[index][0] == 'tool.output_delta'
+ and results[index][0] in _TOOL_OUTPUT_DELTA_EVENTS
and results[index][1].get('name') == name
),
None,
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 0804e550f..a7c1bc118 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -660,6 +660,69 @@ def test_rust_history_reconstructs_terminal_tool_output_deltas():
assert history.final_result() == 'final answer'
+def test_rust_history_reconstructs_terminal_exec_command_output_deltas():
+ 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 'hello world'"},
+ },
+ },
+ {
+ 'type': 'tool.output_delta',
+ 'payload': {
+ 'name': 'exec_command',
+ 'tool_call_id': 'call-exec',
+ 'process_id': '1000',
+ 'session_id': 1000,
+ 'stream': 'stdout',
+ 'text': 'hello ',
+ },
+ },
+ {
+ 'type': 'exec_command.output_delta',
+ 'payload': {
+ 'call_id': 'call-exec',
+ 'process_id': '1000',
+ 'session_id': 1000,
+ 'stream': 'stdout',
+ 'chunk': 'hello ',
+ },
+ },
+ {
+ 'type': 'exec_command.output_delta',
+ 'payload': {
+ 'call_id': 'call-exec',
+ 'process_id': '1000',
+ 'session_id': 1000,
+ 'stream': 'stdout',
+ 'chunk': 'world\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 'hello world'"
+ assert first_result.extracted_content == 'hello world'
+ assert first_result.long_term_memory == 'hello world'
+ assert history.action_history()[0][0]['result'] == 'hello world'
+ 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