Skip to content

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

fix: correct step_interval gap calc and survive AX tree iframe detach

Saurav Panda6c57fe75ebaa
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 5b427dedf..ab6ed5cc1 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1354,8 +1354,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
if last_history_item.metadata:
previous_end_time = last_history_item.metadata.step_end_time
- previous_start_time = last_history_item.metadata.step_start_time
- step_interval = max(0, previous_end_time - previous_start_time)
+ step_interval = max(0, self.step_start_time - previous_end_time)
metadata = StepMetadata(
step_number=self.state.n_steps,
step_start_time=self.step_start_time,
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 804cc451f..760e55c53 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -363,12 +363,18 @@ class DomService:
)
ax_tree_requests.append(ax_tree_request)
- # Wait for all requests to complete
- ax_trees = await asyncio.gather(*ax_tree_requests)
+ # Wait for all requests to complete. Use return_exceptions=True so a single
+ # detached frame (TOCTOU race with ad/widget iframes between Page.getFrameTree
+ # and Accessibility.getFullAXTree) doesn't discard AX data from every other
+ # frame, including the main document. See #4778.
+ ax_trees = await asyncio.gather(*ax_tree_requests, return_exceptions=True)
- # Merge all AX nodes into a single array
+ # Merge all AX nodes into a single array, skipping frames that failed.
merged_nodes: list[AXNode] = []
- for ax_tree in ax_trees:
+ for frame_id, ax_tree in zip(all_frame_ids, ax_trees):
+ if isinstance(ax_tree, BaseException):
+ self.logger.debug(f'Skipping AX tree for detached/unreachable frame {frame_id}: {ax_tree}')
+ continue
merged_nodes.extend(ax_tree['nodes'])
return {'nodes': merged_nodes}
diff --git a/tests/ci/test_history_wait_time.py b/tests/ci/test_history_wait_time.py
index 8479e9b29..63169cf2e 100644
--- a/tests/ci/test_history_wait_time.py
+++ b/tests/ci/test_history_wait_time.py
@@ -21,20 +21,26 @@ def test_step_metadata_step_interval_optional():
def test_step_interval_calculation():
- """Test step_interval calculation logic (uses previous step's duration)"""
- # Previous step (Step 1): runs from 100.0 to 102.5 (duration: 2.5s)
- previous_start = 100.0
+ """step_interval is the gap between steps, not the previous step's duration.
+
+ Regression test for #4484: _finalize() previously computed
+ (previous_end - previous_start) which is duration. The correct value is
+ (current_start - previous_end) — the idle time between steps, including
+ LLM decision time (see rerun_history at agent/service.py).
+ """
+ # Previous step ran from 100.0 to 102.5 (duration: 2.5s)
previous_end = 102.5
- previous_duration = previous_end - previous_start
+ # Current step starts at 105.0 — a 2.5s gap (LLM thinking, IO, etc.)
+ current_start = 105.0
- # Current step (Step 2): should have step_interval = previous step's duration
- # This tells the rerun system "wait 2.5s before executing Step 2"
- expected_step_interval = previous_duration
- calculated_step_interval = max(0, previous_end - previous_start)
+ calculated_step_interval = max(0, current_start - previous_end)
- assert abs(calculated_step_interval - expected_step_interval) < 0.001 # Float comparison
assert calculated_step_interval == 2.5
+ # Clamp behavior: if clocks went backwards or timestamps overlap, clamp to 0
+ calculated_step_interval = max(0, 100.0 - 102.5)
+ assert calculated_step_interval == 0
+
def test_step_metadata_serialization_with_step_interval():
"""Test that step_interval is included in metadata serialization"""