Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Recording: size waits to page-busy span; collapse consecutive waits (#6847)
Cindy Li0c1a9ad4eb24
diff --git a/skyvern/services/browser_recording/service.py b/skyvern/services/browser_recording/service.pyindex 4757d3b44..eeaa470d5 100644--- a/skyvern/services/browser_recording/service.py+++ b/skyvern/services/browser_recording/service.py@@ -322,8 +322,31 @@ class Processor:# of this event through subsequent state machinesbreak+ # NOTE: append-only — the live interpreter calls this each iteration and+ # tracks emitted actions by index, so collapsing here would shrink the list+ # and drop a later wait. Collapsing happens in the raw process() path only.return actions+ @staticmethod+ def _collapse_consecutive_waits(actions: list[Action]) -> list[Action]:+ collapsed: list[Action] = []++ for action in actions:+ previous = collapsed[-1] if collapsed else None+ if isinstance(action, ActionWait) and isinstance(previous, ActionWait):+ collapsed[-1] = ActionWait(+ kind=ActionKind.WAIT.value,+ target=previous.target,+ timestamp_start=previous.timestamp_start,+ timestamp_end=action.timestamp_end,+ url=action.url,+ duration_ms=previous.duration_ms + action.duration_ms,+ )+ continue+ collapsed.append(action)++ return collapsed+def dedupe_block_labels(self, suspects: list[OutputBlock]) -> list[OutputBlock]:"""Detect if any block labels are duplicated, and, if so, rename them for@@ -569,7 +592,7 @@ class Processor:**summarize_exfiltrated_recording_events(events),**self.identity,)- actions = self.events_to_actions(events)+ actions = self._collapse_consecutive_waits(self.events_to_actions(events))blocks = await self.actions_to_blocks(actions)parameters = self.blocks_to_parameters(blocks)diff --git a/skyvern/services/browser_recording/state_machines/wait.py b/skyvern/services/browser_recording/state_machines/wait.pyindex d4bf178af..7a0a0896b 100644--- a/skyvern/services/browser_recording/state_machines/wait.py+++ b/skyvern/services/browser_recording/state_machines/wait.py@@ -51,16 +51,21 @@ class StateMachineWait(StateMachine):return Noneif self.last_event_timestamp is not None:- duration_ms = int(event.params.timestamp - self.last_event_timestamp)+ gap_ms = int(event.params.timestamp - self.last_event_timestamp)- if duration_ms >= self.threshold_ms:- if not self._page_was_active(self.last_event_timestamp, event.params.timestamp):+ if gap_ms >= self.threshold_ms:+ # Size the wait to the page-busy span, not the full gap (drops the idle/think/lag tail).+ last_activity_ms = self._last_activity_in_gap_ms(self.last_event_timestamp, event.params.timestamp)+ busy_ms = int(last_activity_ms - self.last_event_timestamp) if last_activity_ms is not None else 0++ if last_activity_ms is None or busy_ms < self.threshold_ms:LOG.debug(- "~ suppressing wait action: no page activity during idle gap",- duration_ms=duration_ms,+ "~ suppressing wait action: page settled before the wait threshold",+ gap_ms=gap_ms,+ busy_ms=busy_ms,)else:- LOG.debug("~ emitting wait action", duration_ms=duration_ms)+ LOG.debug("~ emitting wait action", gap_ms=gap_ms, busy_ms=busy_ms)action_target = ActionTarget(class_name=None,@@ -75,9 +80,9 @@ class StateMachineWait(StateMachine):kind=ActionKind.WAIT.value,target=action_target,timestamp_start=self.last_event_timestamp,- timestamp_end=event.params.timestamp,+ timestamp_end=last_activity_ms,url=event.params.url,- duration_ms=duration_ms,+ duration_ms=busy_ms,)self.reset()@@ -88,11 +93,12 @@ class StateMachineWait(StateMachine):return None- def _page_was_active(self, gap_start_ms: float, gap_end_ms: float) -> bool:+ def _last_activity_in_gap_ms(self, gap_start_ms: float, gap_end_ms: float) -> float | None:while self.page_activity_timestamps_ms and self.page_activity_timestamps_ms[0] < gap_start_ms:self.page_activity_timestamps_ms.popleft()- return any(timestamp <= gap_end_ms for timestamp in self.page_activity_timestamps_ms)+ in_gap = [timestamp for timestamp in self.page_activity_timestamps_ms if timestamp <= gap_end_ms]+ return max(in_gap) if in_gap else Nonedef on_action(self, action: Action, current_actions: list[Action]) -> bool:if action.kind == ActionKind.HOVER:diff --git a/tests/unit/services/test_browser_recording.py b/tests/unit/services/test_browser_recording.pyindex 303526510..f27c8375f 100644--- a/tests/unit/services/test_browser_recording.py+++ b/tests/unit/services/test_browser_recording.py@@ -536,14 +536,15 @@ def test_wait_suppressed_when_page_idle() -> None:assert [action.kind for action in actions] == [ActionKind.CLICK]-def test_wait_emitted_when_page_showed_network_activity() -> None:+def test_wait_emitted_and_sized_to_page_busy_span() -> None:target = dict(id="button-1", skyId="sky-123", tagName="BUTTON", text=["Click me"])events = [make_click_event(target=target, timestamp=1000.0),- # page was loading during the idle gap (cdp timestamps are seconds)+ # page busy until ~7s after the click (cdp timestamps are seconds)make_cdp_event("net:activity", timestamp_seconds=4.0, params={"count": 12}),- make_focus_event(target=target, timestamp=8000.0),+ make_cdp_event("net:activity", timestamp_seconds=7.0, params={"count": 3}),+ make_focus_event(target=target, timestamp=9000.0),]processor = Processor(PBS_ID, ORG_ID, WP_ID)@@ -552,7 +553,60 @@ def test_wait_emitted_when_page_showed_network_activity() -> None:assert [action.kind for action in actions] == [ActionKind.CLICK, ActionKind.WAIT]wait_action = actions[1]assert isinstance(wait_action, ActionWait)- assert wait_action.duration_ms == 7000+ # busy span = last activity (7000) - click (1000); the 2s idle tail is excluded.+ assert wait_action.duration_ms == 6000+++def test_wait_suppressed_when_page_settles_before_threshold() -> None:+ target = dict(id="button-1", skyId="sky-123", tagName="BUTTON", text=["Click me"])++ events = [+ make_click_event(target=target, timestamp=1000.0),+ # page settles ~1.5s in, then a long idle tail — below the wait threshold+ make_cdp_event("net:activity", timestamp_seconds=2.5, params={"count": 4}),+ make_focus_event(target=target, timestamp=12000.0),+ ]++ processor = Processor(PBS_ID, ORG_ID, WP_ID)+ actions = processor.events_to_actions(events)++ assert [action.kind for action in actions] == [ActionKind.CLICK]+++def _two_consecutive_wait_events() -> list[t.Any]:+ # Two busy stretches (focus events produce no action) yield two adjacent waits;+ # a wait resets the timer, so each stretch needs its own pair of focus events.+ target = dict(id="button-1", skyId="sky-123", tagName="BUTTON", text=["Click me"])+ return [+ make_focus_event(target=target, timestamp=1000.0),+ make_cdp_event("net:activity", timestamp_seconds=6.5, params={"count": 9}),+ make_focus_event(target=target, timestamp=7000.0),+ make_focus_event(target=target, timestamp=13000.0),+ make_cdp_event("net:activity", timestamp_seconds=18.5, params={"count": 9}),+ make_focus_event(target=target, timestamp=19000.0),+ ]+++def test_events_to_actions_keeps_waits_separate_for_live_path() -> None:+ # events_to_actions feeds the incremental live interpreter, which tracks+ # actions by index, so it must stay append-only (no collapsing here).+ processor = Processor(PBS_ID, ORG_ID, WP_ID)+ actions = processor.events_to_actions(_two_consecutive_wait_events())++ assert [action.kind for action in actions] == [ActionKind.WAIT, ActionKind.WAIT]+++def test_collapse_consecutive_waits_merges_durations() -> None:+ processor = Processor(PBS_ID, ORG_ID, WP_ID)+ actions = processor.events_to_actions(_two_consecutive_wait_events())++ collapsed = Processor._collapse_consecutive_waits(actions)++ assert [action.kind for action in collapsed] == [ActionKind.WAIT]+ wait_action = collapsed[0]+ assert isinstance(wait_action, ActionWait)+ # 5500ms + 5500ms busy spans summed into a single wait.+ assert wait_action.duration_ms == 11000def test_wait_ignores_activity_outside_the_idle_gap() -> None: