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 completion action

Magnus Müller17b00e3fed6f
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index a774990fa..f9f0979bf 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -634,10 +634,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This makes `history.model_actions()`, `history.action_names()`, `history.last_action()`, and `history.action_history()` reflect terminal tool calls instead of staying empty when the Rust core ran browser tools.
- Proof: `test_rust_history_reconstructs_terminal_tool_call_actions`.
+125. Rust terminal completion done-action reconstruction
+ - Successful terminal `session.done` events now synthesize a Browser Use `done` action when the terminal model did not emit one explicitly.
+ - This keeps completed Rust-backed histories consistent for `history.last_action()`, `history.action_names()`, `history.model_actions_filtered(['done'])`, and action-history consumers that expect a final Browser Use completion action.
+ - Proof: `test_rust_history_synthesizes_done_action_from_terminal_completion`.
+
## 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` (96 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (97 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 2e4bfa8f0..7621f4b06 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -828,6 +828,30 @@ def _tool_started_calls(events: list[dict[str, Any]]) -> list[dict[str, Any]]:
return tool_calls
+def _tool_calls_with_final_done(
+ tool_calls: list[dict[str, Any]],
+ *,
+ final_result: str | None,
+ attachments: list[str] | None,
+ is_done: bool,
+) -> list[dict[str, Any]]:
+ if not is_done or final_result is None:
+ return tool_calls
+ if tool_calls and tool_calls[-1].get('name') == 'done':
+ return tool_calls
+ done_arguments: dict[str, Any] = {'text': final_result, 'success': True}
+ if attachments:
+ done_arguments['files_to_display'] = attachments
+ return [
+ *tool_calls,
+ {
+ 'name': 'done',
+ 'tool_call_id': 'session.done',
+ 'arguments': done_arguments,
+ },
+ ]
+
+
def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[str, dict[str, Any]]]:
results: dict[str, tuple[str, dict[str, Any]]] = {}
for event in events:
@@ -1095,7 +1119,12 @@ def _history_from_events(
failure = 'Rust terminal session did not produce a final result.'
is_done = final_result is not None and failure is None
attachments = _attachments_from_events(events)
- tool_calls = _tool_started_calls(events)
+ tool_calls = _tool_calls_with_final_done(
+ _tool_started_calls(events),
+ final_result=final_result,
+ attachments=attachments,
+ is_done=is_done,
+ )
history = AgentHistory(
model_output=_model_output_from_tool_calls(tool_calls),
result=_action_results_from_tool_calls(
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index a70f593c5..27a28b88a 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -369,6 +369,45 @@ def test_rust_history_reconstructs_terminal_tool_call_actions():
assert history.action_results()[-1].is_done is True
+def test_rust_history_synthesizes_done_action_from_terminal_completion():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'arguments': {'code': "goto_url('https://example.com')"},
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'text': 'Opened Example Domain',
+ },
+ },
+ {'event_type': 'session.done', 'payload': {'result': 'Example Domain'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ assert history.is_done() is True
+ assert history.action_names() == ['browser_script', 'done']
+ assert history.last_action() == {'done': {'text': 'Example Domain', 'success': True}}
+ assert history.model_actions_filtered(['done']) == [
+ {'done': {'text': 'Example Domain', 'success': True}, 'interacted_element': None}
+ ]
+ assert history.action_history()[0][-1]['result'] == 'Example Domain'
+
+
def test_rust_history_reconstructs_terminal_token_count_usage():
from browser_use.rust.service import _history_from_events
@@ -1537,7 +1576,7 @@ async def test_rust_agent_exposes_logging_helper_methods(monkeypatch):
assert captured_events[0].model == 'gpt-test'
assert captured_events[0].model_provider == 'test-provider'
assert captured_events[0].cdp_url == 'browser.example'
- assert captured_events[0].action_history == [None]
+ assert captured_events[0].action_history == [[{'done': {'text': 'done', 'success': True}}]]
assert captured_events[0].urls_visited == ['https://example.com']
assert captured_events[0].total_input_tokens == 11
assert captured_events[0].total_output_tokens == 7