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 model turn steps

Magnus Müller8cede5db95ea
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index d39b15c59..d86b00aa3 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -659,10 +659,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This covers `tool.output` artifact lists, duplicate `artifact.created` records, spilled tool-output artifacts, and terminal `session.done` result files while keeping image artifacts on the screenshot path surface.
- Proof: `test_rust_history_reconstructs_terminal_artifact_attachments`.
+130. Rust terminal model-turn step reconstruction
+ - Current-main terminal runs are now reconstructed into one Browser Use `AgentHistory` item per `model.turn.request` block instead of collapsing every run into a single synthetic step.
+ - This makes `number_of_steps()`, `model_outputs()`, `model_thoughts()`, `action_history()`, `urls()`, screenshot paths, and final done state follow Browser Use step semantics while preserving collapsed behavior for event logs without terminal model-turn boundaries.
+ - Proof: `test_rust_history_reconstructs_terminal_model_turn_steps`.
+
## 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` (101 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (102 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 c1b053ea0..98a6ff034 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1012,6 +1012,7 @@ def _action_results_from_tool_calls(
attachments: list[str] | None,
failure: str | None,
is_done: bool,
+ append_terminal_result: bool = True,
) -> list[ActionResult]:
tool_results = _tool_results_by_call_id(events)
action_results: list[ActionResult] = []
@@ -1027,6 +1028,9 @@ def _action_results_from_tool_calls(
)
)
+ if not append_terminal_result:
+ return action_results
+
final_action = ActionResult(
is_done=is_done,
success=True if is_done else None,
@@ -1042,6 +1046,76 @@ def _action_results_from_tool_calls(
return action_results
+def _terminal_turn_spans(events: list[dict[str, Any]]) -> list[tuple[int, int]]:
+ starts = [index for index, event in enumerate(events) if _event_type(event) == 'model.turn.request']
+ if len(starts) <= 1:
+ return []
+ return [(start, starts[index + 1] if index + 1 < len(starts) else len(events)) for index, start in enumerate(starts)]
+
+
+def _event_time_seconds(event: dict[str, Any], fallback: float) -> float:
+ ts_ms = event.get('ts_ms')
+ if isinstance(ts_ms, (int, float)):
+ return float(ts_ms) / 1000.0
+ ts = event.get('timestamp') or event.get('time')
+ if isinstance(ts, (int, float)):
+ return float(ts)
+ return fallback
+
+
+def _history_items_from_terminal_turns(
+ events: list[dict[str, Any]],
+ *,
+ started: float,
+ finished: float,
+ final_result: str | None,
+ attachments: list[str] | None,
+ failure: str | None,
+ is_done: bool,
+) -> list[AgentHistory] | None:
+ spans = _terminal_turn_spans(events)
+ if not spans:
+ return None
+ history: list[AgentHistory] = []
+ for step_number, (start_index, end_index) in enumerate(spans, start=1):
+ step_events = events[start_index:end_index]
+ if not step_events:
+ continue
+ is_final_step = step_number == len(spans)
+ step_final_result = final_result if is_final_step else None
+ step_failure = failure if is_final_step else None
+ step_is_done = is_done if is_final_step else False
+ step_attachments = attachments if is_final_step else None
+ tool_calls = _tool_calls_with_final_done(
+ _tool_started_calls(step_events),
+ final_result=step_final_result,
+ attachments=step_attachments,
+ is_done=step_is_done,
+ )
+ if not tool_calls and step_final_result is None and step_failure is None:
+ continue
+ state_events = events[:end_index]
+ step_start = _event_time_seconds(step_events[0], started)
+ step_end = _event_time_seconds(step_events[-1], finished if is_final_step else step_start)
+ history.append(
+ AgentHistory(
+ model_output=_model_output_from_tool_calls(tool_calls, step_events),
+ result=_action_results_from_tool_calls(
+ tool_calls,
+ step_events,
+ final_result=step_final_result,
+ attachments=step_attachments,
+ failure=step_failure,
+ is_done=step_is_done,
+ append_terminal_result=is_final_step,
+ ),
+ state=_browser_state_from_events(state_events),
+ metadata=StepMetadata(step_start_time=step_start, step_end_time=step_end, step_number=step_number),
+ )
+ )
+ return history or None
+
+
def _browser_url(value: Any) -> str:
if not isinstance(value, str):
return ''
@@ -1259,6 +1333,22 @@ 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)
+ history_items = _history_items_from_terminal_turns(
+ events,
+ started=started,
+ finished=finished,
+ final_result=final_result,
+ attachments=attachments,
+ failure=failure,
+ is_done=is_done,
+ )
+ if history_items is not None:
+ history_list: AgentHistoryList[AgentStructuredOutput] = AgentHistoryList(
+ history=history_items,
+ usage=_usage_from_events(events, model),
+ )
+ history_list._output_model_schema = output_model_schema
+ return history_list
tool_calls = _tool_calls_with_final_done(
_tool_started_calls(events),
final_result=final_result,
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 40e1bac58..eb4393678 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -448,6 +448,73 @@ def test_rust_history_synthesizes_done_action_from_terminal_completion():
assert history.action_history()[0][-1]['result'] == 'Example Domain'
+def test_rust_history_reconstructs_terminal_model_turn_steps():
+ from browser_use.rust.service import _history_from_events
+
+ events = [
+ {'type': 'session.created', 'ts_ms': 1_000, 'payload': {}},
+ {'type': 'model.turn.request', 'ts_ms': 2_000, 'payload': {'model': 'gpt-test', 'attempt': 0}},
+ {'type': 'model.stream_delta', 'ts_ms': 2_010, 'payload': {'text': 'Connect the browser.'}},
+ {
+ 'type': 'tool.started',
+ 'ts_ms': 2_020,
+ 'payload': {
+ 'name': 'browser',
+ 'tool_call_id': 'call-browser',
+ 'arguments': {'cmd': 'connect managed --headless'},
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'ts_ms': 2_030,
+ 'payload': {'name': 'browser', 'tool_call_id': 'call-browser', 'text': 'Connected browser'},
+ },
+ {
+ 'type': 'browser.state',
+ 'ts_ms': 2_040,
+ 'payload': {'url': 'https://example.com', 'title': 'Example Domain'},
+ },
+ {'type': 'model.turn.request', 'ts_ms': 3_000, 'payload': {'model': 'gpt-test', 'attempt': 0}},
+ {'type': 'model.stream_delta', 'ts_ms': 3_010, 'payload': {'text': 'Return the page title.'}},
+ {
+ 'type': 'tool.started',
+ 'ts_ms': 3_020,
+ 'payload': {
+ 'name': 'done',
+ 'tool_call_id': 'call-done',
+ 'arguments': {'text': 'Example Domain', 'success': True},
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'ts_ms': 3_030,
+ 'payload': {'name': 'done', 'tool_call_id': 'call-done', 'text': 'done:Example Domain'},
+ },
+ {'type': 'session.done', 'ts_ms': 3_040, 'payload': {'result': 'Example Domain'}},
+ ]
+
+ history = _history_from_events(
+ events,
+ model='gpt-test',
+ started=1.0,
+ finished=4.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ action_history = history.action_history()
+
+ assert history.number_of_steps() == 2
+ assert history.urls() == ['https://example.com', 'https://example.com']
+ assert history.action_names() == ['browser', 'done']
+ assert history.model_outputs()[0].memory == 'Connect the browser.'
+ assert history.model_outputs()[1].memory == 'Return the page title.'
+ assert action_history[0][0]['result'] == 'Connected browser'
+ assert action_history[1][0]['result'] == 'Example Domain'
+ assert history.final_result() == 'Example Domain'
+ assert history.is_done() is True
+
+
def test_rust_history_reconstructs_terminal_streamed_model_thoughts():
from browser_use.rust.service import _history_from_events