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 artifact attachments

Magnus Müllera3dc8db8c33b
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index d1354757c..d39b15c59 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -654,10 +654,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This makes `history.screenshot_paths()` and `history.screenshots()` work for Rust-backed histories when terminal current-main records `tool.image` events or image paths on `tool.output`.
- Proof: `test_rust_history_reconstructs_terminal_screenshot_paths`.
+129. Rust terminal artifact attachment reconstruction
+ - Terminal non-image artifact events are now reconstructed into Browser Use `ActionResult.attachments`.
+ - 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`.
+
## 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` (100 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (101 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 d3b13cdc7..c1b053ea0 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -718,6 +718,24 @@ def _result_file_pointer(payload: dict[str, Any]) -> str | None:
return None
+def _artifact_attachment_pointer(value: Any) -> str | None:
+ if isinstance(value, str) and value:
+ return value
+ if not isinstance(value, dict):
+ return None
+ kind = value.get('kind')
+ mime = value.get('mime_type') or value.get('mime')
+ if isinstance(kind, str) and kind.lower() == 'image':
+ return None
+ if isinstance(mime, str) and mime.lower().startswith('image/'):
+ return None
+ for key in ('url', 'file_url', 'path'):
+ pointer = value.get(key)
+ if isinstance(pointer, str) and pointer:
+ return pointer
+ return None
+
+
def _result_from_events(events: list[dict[str, Any]]) -> str | None:
for event in reversed(events):
if _event_type(event) != 'session.done':
@@ -735,11 +753,27 @@ def _result_from_events(events: list[dict[str, Any]]) -> str | None:
def _attachments_from_events(events: list[dict[str, Any]]) -> list[str] | None:
attachments: list[str] = []
for event in events:
- if _event_type(event) != 'session.done':
- continue
- result_file = _result_file_pointer(_event_payload(event))
- if result_file and result_file not in attachments:
- attachments.append(result_file)
+ event_type = _event_type(event)
+ payload = _event_payload(event)
+ pointers: list[str] = []
+ if event_type == 'session.done':
+ result_file = _result_file_pointer(payload)
+ if result_file:
+ pointers.append(result_file)
+ elif event_type in ('artifact.created', 'tool.output_spilled'):
+ pointer = _artifact_attachment_pointer(payload.get('artifact'))
+ if pointer:
+ pointers.append(pointer)
+ elif event_type in ('tool.output', 'tool.failed'):
+ artifacts = payload.get('artifacts')
+ if isinstance(artifacts, list):
+ for artifact in artifacts:
+ pointer = _artifact_attachment_pointer(artifact)
+ if pointer:
+ pointers.append(pointer)
+ for pointer in pointers:
+ if pointer not in attachments:
+ attachments.append(pointer)
return attachments or None
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index a420f6678..40e1bac58 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -598,6 +598,71 @@ def test_rust_history_exposes_result_file_attachments():
assert results[0].attachments == ['file:///tmp/report.json']
+def test_rust_history_reconstructs_terminal_artifact_attachments():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-artifacts',
+ 'arguments': {'code': "write_file('report.csv', rows)"},
+ },
+ },
+ {
+ 'type': 'tool.output',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-artifacts',
+ 'text': 'Generated report.csv',
+ 'artifacts': [
+ {'path': '/tmp/report.csv', 'kind': 'file', 'mime': 'text/csv'},
+ {'path': '/tmp/screenshot.png', 'kind': 'image', 'mime_type': 'image/png'},
+ ],
+ },
+ },
+ {
+ 'type': 'artifact.created',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-artifacts',
+ 'artifact': {'path': '/tmp/report.csv', 'kind': 'file', 'mime': 'text/csv'},
+ },
+ },
+ {
+ 'type': 'tool.output_spilled',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-large',
+ 'artifact': {'path': '/tmp/large-output.txt', 'original_tokens_estimate': 75000},
+ },
+ },
+ {
+ 'event_type': 'session.done',
+ 'payload': {
+ 'result': 'Generated reports.',
+ 'result_file': {'url': 'file:///tmp/final.json', 'path': '/tmp/final.json'},
+ },
+ },
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ attachments = ['/tmp/report.csv', '/tmp/large-output.txt', 'file:///tmp/final.json']
+
+ assert history.final_result() == 'Generated reports.'
+ assert history.action_results()[-1].attachments == attachments
+ assert history.last_action() == {
+ 'done': {'text': 'Generated reports.', 'success': True, 'files_to_display': attachments}
+ }
+
+
def test_rust_agent_translates_browser_use_args_to_terminal(monkeypatch):
from browser_use.rust import Agent