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 tool output deltas

Magnus Müller568edc5b930b
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 7e01fe4ac..cf68b9f38 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -744,10 +744,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This preserves Rust current-main browser summary GIFs in Python-visible final `ActionResult.attachments` and synthesized `done.files_to_display`, even though generic image artifacts remain filtered from file attachments.
- Proof: `test_rust_history_reconstructs_terminal_capture_curation_gif_attachment`.
+147. Rust terminal tool-output-delta reconstruction
+ - Terminal `tool.output_delta` streaming chunks are now folded into Browser Use action results when no final textual `tool.output` arrives.
+ - 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`.
+
## 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` (118 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (119 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 0f79e098c..e9f6c1559 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1087,15 +1087,37 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
results: 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', 'tool.aborted', 'tool.finished', 'model.response.input_item'):
+ if event_type not in (
+ 'tool.output',
+ 'tool.output_delta',
+ 'tool.failed',
+ 'tool.aborted',
+ 'tool.finished',
+ 'model.response.input_item',
+ ):
continue
payload = _event_payload(event)
if event_type == 'model.response.input_item':
payload = _response_input_item_tool_payload(payload)
+ if event_type == 'tool.output_delta':
+ delta_text = _tool_output_delta_text(payload)
+ if not delta_text:
+ continue
call_id = payload.get('tool_call_id') or payload.get('call_id')
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'):
+ 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 ''
+ for payload_key, payload_value in payload.items():
+ if payload_key != 'text':
+ merged_payload[payload_key] = payload_value
+ 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.finished' and previous is not None and previous[0] != 'tool.finished':
continue
results[key] = (event_type, payload)
@@ -1106,13 +1128,58 @@ def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[
results: list[tuple[str, dict[str, Any]]] = []
for event in events:
event_type = _event_type(event)
- if event_type not in ('tool.output', 'tool.failed', 'tool.aborted', 'tool.finished', 'model.response.input_item'):
+ if event_type not in (
+ 'tool.output',
+ 'tool.output_delta',
+ 'tool.failed',
+ 'tool.aborted',
+ 'tool.finished',
+ 'model.response.input_item',
+ ):
continue
payload = _event_payload(event)
if event_type == 'model.response.input_item':
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':
+ delta_text = _tool_output_delta_text(payload)
+ if not delta_text:
+ continue
+ name = payload.get('name')
+ previous = next(
+ (
+ result
+ for result in reversed(results)
+ if isinstance(name, str)
+ and name
+ and result[0] == 'tool.output_delta'
+ and result[1].get('name') == name
+ ),
+ None,
+ )
+ if previous is not None:
+ previous_text = previous[1].get('text')
+ previous[1]['text'] = f'{previous_text if isinstance(previous_text, str) else ""}{delta_text}'
+ continue
+ payload = dict(payload)
+ payload['text'] = delta_text
+ elif event_type == 'tool.output':
+ name = payload.get('name')
+ previous_delta_index = next(
+ (
+ index
+ 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][1].get('name') == name
+ ),
+ None,
+ )
+ if previous_delta_index is not None and payload.get('stream') is not True:
+ results[previous_delta_index] = (event_type, payload)
+ continue
if event_type == 'tool.output' and payload.get('stream') is True:
continue
if event_type == 'tool.finished':
@@ -1190,6 +1257,14 @@ def _tool_output_text(value: Any) -> str | None:
return None
+def _tool_output_delta_text(payload: dict[str, Any]) -> str | None:
+ for key in ('text', 'delta', 'chunk'):
+ value = payload.get(key)
+ if isinstance(value, str) and value:
+ return value
+ return None
+
+
def _structured_tool_output_text(value: Any) -> str | None:
if isinstance(value, str) and value.strip():
return value.strip()
@@ -1379,8 +1454,12 @@ def _action_results_from_tool_calls(
action_results.append(
ActionResult(
error=error,
- extracted_content=text if event_type in ('tool.output', 'tool.finished', 'model.response.input_item') else None,
- long_term_memory=text if event_type in ('tool.output', 'tool.finished', 'model.response.input_item') else None,
+ extracted_content=text
+ if event_type in ('tool.output', 'tool.output_delta', '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')
+ else None,
)
)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 1a79c3a42..57173d293 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -603,6 +603,63 @@ def test_rust_history_reconstructs_terminal_tool_finished_results():
assert history.final_result() == 'Example Domain'
+def test_rust_history_reconstructs_terminal_tool_output_deltas():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'python',
+ 'tool_call_id': 'call-python',
+ 'arguments': {'code': "print('hello world')"},
+ },
+ },
+ {
+ 'type': 'tool.output_delta',
+ 'payload': {
+ 'name': 'python',
+ 'tool_call_id': 'call-python',
+ 'stream': 'stdout',
+ 'text': 'hello',
+ },
+ },
+ {
+ 'type': 'tool.output_delta',
+ 'payload': {
+ 'name': 'python',
+ 'tool_call_id': 'call-python',
+ 'stream': 'stdout',
+ 'text': ' world\n',
+ },
+ },
+ {
+ 'type': 'tool.finished',
+ 'payload': {
+ 'name': 'python',
+ 'tool_call_id': 'call-python',
+ },
+ },
+ {'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() == ['python', 'done']
+ assert history.model_actions()[0]['python']['code'] == "print('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_unkeyed_tool_results():
from browser_use.rust.service import _history_from_events