Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Order Rust finalize step event dispatch

Magnus Müller0a3db0eb33d6
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 4ff0115df..8bab3eeb2 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -956,10 +956,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- History item creation and `CreateAgentStepEvent` dispatch remain gated on browser state, matching the Python Agent separation between optional stateful history/events and common step completion logging.
- Proof: `test_rust_agent_finalize_logs_step_completion_without_browser_state`.
+189. Rust Agent finalize ordering parity
+ - Rust-backed `_finalize(...)` now logs the step completion summary once, saves file-system state, and then dispatches `CreateAgentStepEvent`, matching the Python Agent's finalize ordering.
+ - The step event is still created only when browser state and model output are available, while common finalize bookkeeping runs for every completed action result.
+ - Proof: `test_rust_agent_finalize_orders_summary_save_before_step_event`.
+
## 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` (159 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (160 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 4a1076e68..a0b9734f6 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3578,6 +3578,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
if not self.state.last_result:
return
step_start_time = getattr(self, 'step_start_time', step_end_time)
+ step_event = None
if browser_state_summary is not None:
metadata = StepMetadata(
step_number=self.state.n_steps,
@@ -3592,7 +3593,6 @@ class Agent(Generic[Context, AgentStructuredOutput]):
metadata,
state_message=state_message,
)
- self._log_step_completion_summary(step_start_time, self.state.last_result)
if self.state.last_model_output:
actions_data = []
for action in self.state.last_model_output.action or []:
@@ -3605,9 +3605,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
actions_data,
browser_state_summary,
)
- self.eventbus.dispatch(step_event)
self._log_step_completion_summary(step_start_time, self.state.last_result)
self.save_file_system_state()
+ if step_event is not None:
+ self.eventbus.dispatch(step_event)
self.state.n_steps += 1
async def _force_done_after_last_step(self, step_info: AgentStepInfo | None = None) -> None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index ec796194a..8fb352585 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -4357,6 +4357,61 @@ async def test_rust_agent_finalize_logs_step_completion_without_browser_state(mo
assert any('Ran 1 action' in message and 'success=1' in message for message in logger.debugs)
+async def test_rust_agent_finalize_orders_summary_save_before_step_event(monkeypatch, tmp_path):
+ import base64
+ import time
+
+ from browser_use.agent.views import ActionResult
+ from browser_use.rust import Agent
+
+ calls = []
+
+ class RecordingLogger:
+ def debug(self, message, *args, **kwargs):
+ if 'Ran 1 action' in message:
+ calls.append('summary')
+
+ class DomState:
+ selector_map = {}
+
+ class BrowserStateSummary:
+ url = 'https://example.com/finalize-order'
+ title = 'Finalize Order'
+ tabs = []
+ screenshot = base64.b64encode(b'png-bytes').decode('utf-8')
+ dom_state = DomState()
+
+ class EventBus:
+ def __init__(self):
+ self.dispatched = []
+
+ def dispatch(self, event):
+ calls.append('dispatch')
+ self.dispatched.append(event)
+ return event
+
+ logger = RecordingLogger()
+ monkeypatch.setattr(Agent, 'logger', property(lambda self: logger))
+ agent = Agent(task='Finalize order parity.', file_system_path=str(tmp_path / 'agent-files'))
+ agent.eventbus = EventBus()
+ agent.state.last_result = [ActionResult(is_done=True, success=True, extracted_content='final answer')]
+ agent.state.last_model_output = agent.AgentOutput(
+ evaluation_previous_goal='Need answer',
+ memory='Have answer',
+ next_goal='Finish',
+ action=[agent.ActionModel(done={'text': 'final answer', 'success': True})],
+ )
+ agent.step_start_time = time.time() - 0.1
+ monkeypatch.setattr(agent, 'save_file_system_state', lambda: calls.append('save'))
+ starting_steps = agent.state.n_steps
+
+ await agent._finalize(BrowserStateSummary())
+
+ assert calls == ['summary', 'save', 'dispatch']
+ assert agent.state.n_steps == starting_steps + 1
+ assert [type(event).__name__ for event in agent.eventbus.dispatched] == ['CreateAgentStepEvent']
+
+
def test_rust_agent_initializes_message_manager_and_followup_state():
from browser_use.agent.message_manager.service import MessageManager
from browser_use.rust import Agent