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 unkeyed tool results

Magnus Müllerc93558adb3fa
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index f36193908..1478cae39 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -714,10 +714,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The wrapper treats reasoning tokens as completion usage, matching Browser Use's LLM usage accounting, and preserves terminal-reported cumulative `total_tokens` instead of recomputing totals from visible output tokens only.
- Proof: `test_rust_history_reconstructs_terminal_reasoning_token_usage`.
+141. Rust terminal unkeyed tool-result reconstruction
+ - Terminal tool result events without `tool_call_id` are now paired back to Browser Use action results by ordered tool name fallback.
+ - This preserves CLI/manual tool paths such as `python`, where `tool.started`, `tool.output`, and `tool.finished` can omit call ids, while explicit call-id matching still takes precedence and transient streaming chunks stay out of final action history.
+ - Proof: `test_rust_history_reconstructs_terminal_unkeyed_tool_results`.
+
## 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` (112 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (113 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 257e63107..0d8ecaabc 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1002,13 +1002,15 @@ def _tool_call_from_payload(payload: dict[str, Any], fallback_id: str) -> dict[s
name = _safe_tool_action_name(payload.get('name') or function.get('name'))
if name is None:
return None
- call_id = payload.get('tool_call_id') or payload.get('call_id') or payload.get('id') or fallback_id
+ raw_call_id = payload.get('tool_call_id') or payload.get('call_id') or payload.get('id')
+ call_id = raw_call_id or fallback_id
arguments = payload.get('arguments')
if arguments is None:
arguments = function.get('arguments')
return {
'name': name,
'tool_call_id': str(call_id),
+ '_has_explicit_tool_call_id': raw_call_id is not None,
'arguments': _tool_arguments(arguments),
}
@@ -1087,6 +1089,57 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
return results
+def _unkeyed_tool_results(events: list[dict[str, Any]]) -> list[tuple[str, dict[str, Any]]]:
+ 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'):
+ 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' and payload.get('stream') is True:
+ continue
+ if event_type == 'tool.finished':
+ name = payload.get('name')
+ previous = next(
+ (
+ result
+ for result in reversed(results)
+ if isinstance(name, str)
+ and name
+ and result[0] != 'tool.finished'
+ and result[1].get('name') == name
+ ),
+ None,
+ )
+ if previous is not None:
+ continue
+ results.append((event_type, payload))
+ return results
+
+
+def _matching_unkeyed_tool_result_index(
+ results: list[tuple[str, dict[str, Any]]],
+ used_indices: set[int],
+ name: Any,
+ *,
+ allow_any: bool,
+) -> int | None:
+ if isinstance(name, str) and name:
+ for index, (_event_type, payload) in enumerate(results):
+ if index not in used_indices and payload.get('name') == name:
+ return index
+ if not allow_any:
+ return None
+ for index in range(len(results)):
+ if index not in used_indices:
+ return index
+ return None
+
+
def _response_input_item_tool_payload(payload: dict[str, Any]) -> dict[str, Any]:
item = payload.get('item')
if not isinstance(item, dict):
@@ -1273,9 +1326,21 @@ def _action_results_from_tool_calls(
append_terminal_result: bool = True,
) -> list[ActionResult]:
tool_results = _tool_results_by_call_id(events)
+ unkeyed_results = _unkeyed_tool_results(events)
+ used_unkeyed_result_indices: set[int] = set()
action_results: list[ActionResult] = []
for tool_call in tool_calls:
event_type, payload = tool_results.get(str(tool_call.get('tool_call_id')), ('', {}))
+ if not event_type:
+ unkeyed_result_index = _matching_unkeyed_tool_result_index(
+ unkeyed_results,
+ used_unkeyed_result_indices,
+ tool_call.get('name'),
+ allow_any=not tool_call.get('_has_explicit_tool_call_id', True),
+ )
+ if unkeyed_result_index is not None:
+ used_unkeyed_result_indices.add(unkeyed_result_index)
+ event_type, payload = unkeyed_results[unkeyed_result_index]
text = _tool_result_text(payload)
if event_type == 'tool.finished' and not text:
name = payload.get('name') or tool_call.get('name') or 'tool'
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index ccc1f1b07..225745d6e 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -577,6 +577,61 @@ def test_rust_history_reconstructs_terminal_tool_finished_results():
assert history.final_result() == 'Example Domain'
+def test_rust_history_reconstructs_terminal_unkeyed_tool_results():
+ 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': 'python',
+ 'arguments': {'code': "print('full output')"},
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'payload': {
+ 'name': 'python',
+ 'stream': True,
+ 'text': 'partial output',
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'payload': {
+ 'name': 'python',
+ 'ok': True,
+ 'text': 'full output',
+ },
+ },
+ {
+ 'type': 'tool.finished',
+ 'payload': {
+ 'name': '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('full output')"
+ assert first_result.extracted_content == 'full output'
+ assert first_result.long_term_memory == 'full output'
+ assert history.action_history()[0][0]['result'] == 'full output'
+ assert 'partial output' not in json.dumps(history.action_history(), sort_keys=True)
+ assert history.final_result() == 'final answer'
+
+
def test_rust_history_synthesizes_done_action_from_terminal_completion():
from browser_use.rust.service import _history_from_events