Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Apply Rust terminal compaction replay boundary
Magnus Müllerdfbfe07192c2
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 508e4dfd2..f0926a465 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -704,10 +704,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- This keeps rolled-back model outputs, tool actions, tool results, usage, attachments, and final results off Python-visible surfaces such as `action_names()`, `model_outputs()`, `action_history()`, and `final_result()`.- Proof: `test_rust_history_applies_terminal_session_rollback`.+139. Rust terminal session-compaction replay boundary reconstruction+ - Terminal `session.compacted` events now establish the Browser Use history replay boundary before action/result reconstruction.+ - This keeps pre-compaction model outputs, tool actions, tool results, usage, attachments, and final results off Python-visible history surfaces while preserving post-compaction run data.+ - Proof: `test_rust_history_applies_terminal_session_compaction_boundary`.+## 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` (110 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (111 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.pyindex d68219a61..d4cc640e5 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -747,6 +747,33 @@ def _contextual_event_targets_turn(event: dict[str, Any], target_seq: int | Nonereturn isinstance(before_seq, int) and before_seq == target_seq+def _compaction_replay_start_seq(event: dict[str, Any]) -> int | None:+ replay_from_seq = _event_payload(event).get('replay_from_seq')+ if isinstance(replay_from_seq, bool):+ return None+ if isinstance(replay_from_seq, int):+ return replay_from_seq+ return _event_seq(event)+++def _events_after_terminal_compaction(events: list[dict[str, Any]]) -> list[dict[str, Any]]:+ compaction_index = next((index for index in range(len(events) - 1, -1, -1) if _event_type(events[index]) == 'session.compacted'), None)+ if compaction_index is None:+ return events+ replay_start_seq = _compaction_replay_start_seq(events[compaction_index])+ if replay_start_seq is None:+ return events[compaction_index + 1 :]+ replay_events: list[dict[str, Any]] = []+ for index, event in enumerate(events):+ seq = _event_seq(event)+ if seq is not None:+ if seq > replay_start_seq:+ replay_events.append(event)+ elif index > compaction_index:+ replay_events.append(event)+ return replay_events++def _rollback_last_terminal_user_turn(events: list[dict[str, Any]]) -> bool:user_pos = next((index for index in range(len(events) - 1, -1, -1) if _is_terminal_user_turn_event(events[index])), None)if user_pos is None:@@ -1570,7 +1597,7 @@ def _history_from_events(output_model_schema: type[AgentStructuredOutput] | None,process_error: str | None,) -> AgentHistoryList[AgentStructuredOutput]:- events = _events_after_terminal_rollbacks(events)+ events = _events_after_terminal_rollbacks(_events_after_terminal_compaction(events))final_result = _structured_result_text(_result_from_events(events), output_model_schema)failure = process_error or _failure_from_events(events)if final_result is None and failure is None:diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 77611503b..b415e0e8d 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -762,6 +762,74 @@ def test_rust_history_applies_terminal_session_rollback():assert 'rolled-back.example' not in serialized_actions+def test_rust_history_applies_terminal_session_compaction_boundary():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [+ {'event_type': 'session.input', 'seq': 1, 'payload': {'text': 'old task'}},+ {'event_type': 'model.turn.request', 'seq': 2, 'ts_ms': 2_000, 'payload': {'model': 'gpt-test'}},+ {'event_type': 'model.stream_delta', 'seq': 3, 'payload': {'text': 'Old turn'}},+ {+ 'event_type': 'tool.started',+ 'seq': 4,+ 'payload': {+ 'name': 'browser_script',+ 'tool_call_id': 'call-old',+ 'arguments': {'code': "goto_url('https://old.example')"},+ },+ },+ {+ 'event_type': 'tool.output',+ 'seq': 5,+ 'payload': {'name': 'browser_script', 'tool_call_id': 'call-old', 'text': 'Old removed'},+ },+ {'event_type': 'session.done', 'seq': 6, 'payload': {'result': 'Old result'}},+ {+ 'event_type': 'session.compacted',+ 'seq': 7,+ 'payload': {+ 'replacement_messages': [{'role': 'user', 'content': 'compacted summary'}],+ },+ },+ {'event_type': 'session.followup', 'seq': 8, 'payload': {'text': 'post-compaction task'}},+ {'event_type': 'model.turn.request', 'seq': 9, 'ts_ms': 3_000, 'payload': {'model': 'gpt-test'}},+ {'event_type': 'model.stream_delta', 'seq': 10, 'payload': {'text': 'Post compaction turn'}},+ {+ 'event_type': 'tool.started',+ 'seq': 11,+ 'payload': {+ 'name': 'browser_script',+ 'tool_call_id': 'call-post',+ 'arguments': {'code': "goto_url('https://example.com')"},+ },+ },+ {+ 'event_type': 'tool.output',+ 'seq': 12,+ 'payload': {'name': 'browser_script', 'tool_call_id': 'call-post', 'text': 'Post kept'},+ },+ {'event_type': 'session.done', 'seq': 13, 'payload': {'result': 'Post result'}},+ ],+ model='gpt-test',+ started=1.0,+ finished=4.0,+ output_model_schema=None,+ process_error=None,+ )++ serialized_actions = json.dumps(history.model_actions(), sort_keys=True)++ assert history.number_of_steps() == 1+ assert history.action_names() == ['browser_script', 'done']+ assert history.model_outputs()[0].memory == 'Post compaction turn'+ assert history.action_history()[0][0]['result'] == 'Post kept'+ assert history.final_result() == 'Post result'+ assert 'Old turn' not in serialized_actions+ assert 'old.example' not in serialized_actions+ assert 'Old result' not in (history.final_result() or '')++def test_rust_history_reconstructs_terminal_streamed_model_thoughts():from browser_use.rust.service import _history_from_events