Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Match Playwright selector-engine semantics in the skycdp facade (SKY-13461) (#8209)
Shuchang Zhengc658bc9eb0fd
diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.jsonindex ce7648605..8fc097bc9 100644--- a/docs/api-reference/openapi.json+++ b/docs/api-reference/openapi.json@@ -10461,6 +10461,17 @@}],"title": "Single Option Click"+ },+ "desired_state": {+ "anyOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ],+ "title": "Desired State"}},"type": "object",diff --git a/skyvern/client/types/click_context.py b/skyvern/client/types/click_context.pyindex 998e74770..56b20976d 100644--- a/skyvern/client/types/click_context.py+++ b/skyvern/client/types/click_context.py@@ -9,6 +9,7 @@ from ..core.pydantic_utilities import IS_PYDANTIC_V2, UniversalBaseModelclass ClickContext(UniversalBaseModel):thought: typing.Optional[str] = Nonesingle_option_click: typing.Optional[bool] = None+ desired_state: typing.Optional[bool] = Noneif IS_PYDANTIC_V2:model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2diff --git a/skyvern/core/script_generations/real_skyvern_page_ai.py b/skyvern/core/script_generations/real_skyvern_page_ai.pyindex d6d875a8b..a6dff3ecd 100644--- a/skyvern/core/script_generations/real_skyvern_page_ai.py+++ b/skyvern/core/script_generations/real_skyvern_page_ai.py@@ -48,6 +48,7 @@ from skyvern.webeye.actions.actions import (UploadFileAction,)from skyvern.webeye.actions.handler import (+ ActionHandler,get_actual_value_of_parameter_if_secret,handle_click_action,handle_hover_action,@@ -275,26 +276,47 @@ class RealSkyvernPageAi(SkyvernPageAi):task, step.step_id, step.order, self.scraped_page, json_response.get("actions", []))action = cast(ClickAction, actions[0])- result = await handle_click_action(action, self.page, self.scraped_page, task, step)- if result and result[-1].success is False:- raise SkyvernActionFailed(result[-1].exception_message or "Click action returned success=False")++ # Run the registered click setup (e.g. the selected-state guard) the+ # same way ActionHandler._handle_action does, so this cached AI-click+ # path can't re-toggle an already-selected control. There, any nonempty+ # setup result is terminal: a failure raises, and any other result means+ # the setup already stood in for the click, so suppress the physical one.+ setup_suppressed_click = False+ setup = ActionHandler.get_setup_for_action_type(action.action_type)+ if setup is not None:+ setup_results = await setup(action, self.page, self.scraped_page, task, step)+ if setup_results:+ if setup_results[-1].success is False:+ raise SkyvernActionFailed(+ setup_results[-1].exception_message or "Click action setup returned success=False"+ )+ setup_suppressed_click = True++ if not setup_suppressed_click:+ result = await handle_click_action(action, self.page, self.scraped_page, task, step)+ if result and result[-1].success is False:+ raise SkyvernActionFailed(result[-1].exception_message or "Click action returned success=False")xpath = action.get_xpath()selector = f"xpath={xpath}" if xpath else selector# Record element-level fallback episode for the script reviewer (code_v2 only).# This fires when a cached script's selector failed (or was missing) and# ai_click succeeded. The episode gives the reviewer the AI-found action data- # so it can write a proper selector.- await self._record_element_fallback_episode(- context=context,- action_type="click",- failed_selector=failed_selector,- intention=intention,- action=action,- block_label=block_label,- recoverable_marker_id=recoverable_marker_id,- v3_parent_episode_id=v3_parent_episode_id,- )+ # so it can write a proper selector. A setup-suppressed click was a deliberate+ # no-op (e.g. the control already held the desired state), not a selector miss,+ # so it must not record a corrective episode.+ if not setup_suppressed_click:+ await self._record_element_fallback_episode(+ context=context,+ action_type="click",+ failed_selector=failed_selector,+ intention=intention,+ action=action,+ block_label=block_label,+ recoverable_marker_id=recoverable_marker_id,+ v3_parent_episode_id=v3_parent_episode_id,+ )return selectorexcept SkyvernActionFailed:diff --git a/skyvern/forge/prompts/skyvern/extract-action-static.j2 b/skyvern/forge/prompts/skyvern/extract-action-static.j2index 5e1fc49c8..75b6c3cad 100644--- a/skyvern/forge/prompts/skyvern/extract-action-static.j2+++ b/skyvern/forge/prompts/skyvern/extract-action-static.j2@@ -46,6 +46,7 @@ Reply in JSON format with the following keys:{%- if slim_output not in ('safe', 'terse') %}"thought": str, // Describe how you decided that this action is a single choice option or multi-choice option.{% endif %}"single_option_click": bool, // True if the click is the only choice to proceed towards the goal, regardless of different user context or input. False if there are multiple valid options that depend on user input. Examples: clicking a login button to login is True (it's the only way to login); clicking a radio button for a multi-choice question (e.g., selecting "male", "female", or "other" for gender) is False (the choice depends on user input). When clicking on radio buttons, dropdown options, or any element that represents one of multiple possible selections, this should be False. If the click is intended to open the dropdown menu in order to select an option from the dropdown, this should be False.+ "desired_state": bool, // Set this ONLY when the CLICK is meant to drive a selectable/toggle control — a checkbox, switch, radio, or an option/tab that reports a selected state — to a terminal on/off state. true means leave the control selected/checked/on; false means leave it unselected/unchecked/off. Selecting a radio button or an option is generally true; explicitly turning a checkbox or switch off is false. Use null for ordinary buttons, links, a click that only opens a dropdown/menu, or anything that is not setting a control to a terminal on/off state.},"context": { // The context for INPUT_TEXT or SELECT_OPTION action only. null if not INPUT_TEXT or SELECT_OPTION action. Extract the following detailed information from the "reasoning", and double-check the information by analysing the HTML elements.{%- if slim_output not in ('safe', 'terse') %}diff --git a/skyvern/forge/prompts/skyvern/extract-action.j2 b/skyvern/forge/prompts/skyvern/extract-action.j2index ccb27fae8..34b1f484a 100644--- a/skyvern/forge/prompts/skyvern/extract-action.j2+++ b/skyvern/forge/prompts/skyvern/extract-action.j2@@ -46,6 +46,7 @@ Reply in JSON format with the following keys:{%- if slim_output not in ('safe', 'terse') %}"thought": str, // Describe how you decided that this action is a single choice option or multi-choice option.{% endif %}"single_option_click": bool, // True if the click is the only choice to proceed towards the goal, regardless of different user context or input. False if there are multiple valid options that depend on user input. Examples: clicking a login button to login is True (it's the only way to login); clicking a radio button for a multi-choice question (e.g., selecting "male", "female", or "other" for gender) is False (the choice depends on user input). When clicking on radio buttons, dropdown options, or any element that represents one of multiple possible selections, this should be False. If the click is intended to open the dropdown menu in order to select an option from the dropdown, this should be False.+ "desired_state": bool, // Set this ONLY when the CLICK is meant to drive a selectable/toggle control — a checkbox, switch, radio, or an option/tab that reports a selected state — to a terminal on/off state. true means leave the control selected/checked/on; false means leave it unselected/unchecked/off. Selecting a radio button or an option is generally true; explicitly turning a checkbox or switch off is false. Use null for ordinary buttons, links, a click that only opens a dropdown/menu, or anything that is not setting a control to a terminal on/off state.},"context": { // The context for INPUT_TEXT or SELECT_OPTION action only. null if not INPUT_TEXT or SELECT_OPTION action. Extract the following detailed information from the "reasoning", and double-check the information by analysing the HTML elements.{%- if slim_output not in ('safe', 'terse') %}diff --git a/skyvern/forge/prompts/skyvern/single-click-action.j2 b/skyvern/forge/prompts/skyvern/single-click-action.j2index 3601f3076..260f2a533 100644--- a/skyvern/forge/prompts/skyvern/single-click-action.j2+++ b/skyvern/forge/prompts/skyvern/single-click-action.j2@@ -19,6 +19,9 @@ Reply in JSON format with the following keys:"double_click": bool, // True only when the element requires a double-click to activate (e.g., opening a file in a file manager, entering edit mode in a spreadsheet cell, or expanding a tree node). Defaults to false for normal clicks. Do not use double-click for regular buttons, links, form elements, clickable grid rows, icons, or dashboard cards."download": bool, // If true, the browser will trigger a download by clicking the element. If false, the browser will click the element without triggering a download."file_url": str, // The url of the file to upload if applicable. This field can be present for CLICK only if the click is to upload the file. It should be null otherwise.+ "click_context": { // Context for driving a selectable/toggle control to a terminal state. null for ordinary clicks.+ "desired_state": bool // Set this ONLY when the click is meant to drive a selectable/toggle control — a checkbox, switch, radio, or an option/tab that reports a selected state — to a terminal on/off state. true means leave the control selected/checked/on; false means leave it unselected/unchecked/off. Use null for ordinary buttons, links, a click that only opens a dropdown/menu, or anything that is not setting a control to a terminal on/off state.+ }}]}diff --git a/skyvern/webeye/actions/actions.py b/skyvern/webeye/actions/actions.pyindex cec2d2ade..e5dea1189 100644--- a/skyvern/webeye/actions/actions.py+++ b/skyvern/webeye/actions/actions.py@@ -103,6 +103,10 @@ class InputOrSelectContext(BaseModel):class ClickContext(BaseModel):thought: str | None = Nonesingle_option_click: bool | None = None+ # Level-triggered toggle intent set by the planner: True = leave the control+ # selected/checked/on, False = leave it unselected/unchecked/off, None = ordinary+ # edge-triggered click. Old persisted actions and absent field decode to None.+ desired_state: bool | None = Noneclass Action(BaseModel):diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex de6ade496..4ca93e291 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -3502,6 +3502,13 @@ class ActionHandler:) -> None:cls._setup_action_types[action_type] = handler+ @classmethod+ def get_setup_for_action_type(+ cls,+ action_type: ActionType,+ ) -> Callable[[Action, Page, ScrapedPage, Task, Step], Awaitable[list[ActionResult]]] | None:+ return cls._setup_action_types.get(action_type)+@classmethoddef register_teardown_for_action_type(cls,@@ -4550,6 +4557,252 @@ async def _retarget_disabled_element_for_click(return child_element+def _parse_aria_boolean(value: str | None) -> bool | None:+ """Interpret an ARIA boolean-state string. Returns True/False only for an exact "true"/"false"+ (case-insensitive, trimmed); anything else -- absent, "mixed", malformed -- returns None so the+ caller treats the state as unreadable and falls open to an ordinary click."""+ if value is None:+ return None+ normalized = value.strip().casefold()+ if normalized == "true":+ return True+ if normalized == "false":+ return False+ return None+++async def _is_single_select_option_highlight(element: SkyvernElement) -> bool:+ """True when a live aria-selected="true" belongs to a single-select option, where ARIA treats it+ as the pre-commit keyboard highlight rather than committed state (mirrors+ _custom_select_matched_state_confirms_pre_click): explicit role=option with no+ aria-multiselectable="true" ancestor-or-self. In a multiselectable container aria-selected is+ committed, so this returns False and the value stays readable."""+ if (await element.get_attr("role", mode="dynamic") or "").strip().casefold() != "option":+ return False+ multiselectable = element.get_locator().locator("xpath=ancestor-or-self::*[@aria-multiselectable][1]")+ if await multiselectable.count() == 0:+ return True+ return (await multiselectable.get_attribute("aria-multiselectable") or "").strip().casefold() != "true"+++async def _resolve_live_selected_state(element: SkyvernElement) -> bool | None:+ """Read one generic, live observable of a control's selected/checked state, or None when no+ boolean observable is readable (unknown control, malformed value, or a detached/unreadable+ element) so the caller falls open to an ordinary click. Native checkbox/radio inputs report+ through is_checked(); other controls expose an exact aria-checked/aria-pressed/aria-selected+ boolean read live. Role only chooses which observable to read and whether a bare aria-selected+ value is trustworthy; it never implies desired intent."""+ try:+ if element.get_tag_name() == "input":+ input_type = (await element.get_attr("type") or "").strip().casefold()+ if input_type in ("checkbox", "radio"):+ return await element.is_checked()+ return None+ for aria_attr in ("aria-checked", "aria-pressed", "aria-selected"):+ parsed = _parse_aria_boolean(await element.get_attr(aria_attr, mode="dynamic"))+ if parsed is None:+ continue+ if aria_attr == "aria-selected" and parsed and await _is_single_select_option_highlight(element):+ # A single-select option's aria-selected="true" is a keyboard highlight, not committed+ # state, so leave it unreadable and let the physical click commit the selection.+ return None+ return parsed+ return None+ except Exception:+ LOG.debug("Failed to read live selected state; continuing with an ordinary click", element_id=element.get_id())+ return None+++async def _get_associated_checkbox_label_locator(element: SkyvernElement) -> Locator | None:+ ancestor_label_locator = element.get_locator().locator("xpath=ancestor::label[1]")+ if await ancestor_label_locator.count() > 0:+ return ancestor_label_locator++ input_id = await element.get_attr("id", mode="dynamic")+ if not input_id:+ return None++ explicit_label_locator = element.get_frame().locator(f"label[for={json.dumps(input_id)}]")+ if await explicit_label_locator.count() > 0:+ return explicit_label_locator.first++ return None+++async def _set_native_checkbox_state(element: SkyvernElement, should_check: bool) -> bool:+ """Drive a native checkbox/radio input to ``should_check`` and report whether the final state+ matches. Sets through the input, falling back to a visible associated label when the input is+ hidden/non-actionable (skipping a label that forwards its click to an interactive descendant)."""+ locator = element.get_locator()+ try:+ if await locator.is_checked(timeout=settings.BROWSER_ACTION_TIMEOUT_MS) == should_check:+ return True+ except Exception:+ # Keep moving: check()/uncheck() are state-setting operations for actionable inputs,+ # and the label fallback below verifies the final state for hidden inputs.+ LOG.warning("Failed to read checkbox state before setting it", element_id=element.get_id(), exc_info=True)++ try:+ if should_check:+ await element.check()+ else:+ await element.uncheck()+ return True+ except Exception:+ LOG.warning(+ "Failed to set checkbox state through input, trying associated label",+ element_id=element.get_id(),+ should_check=should_check,+ exc_info=True,+ )++ label_locator = await _get_associated_checkbox_label_locator(element)+ if label_locator is None:+ return False++ try:+ if not await label_locator.is_visible():+ return False+ if await SkyvernElement._label_click_forwards_to_descendant(label_locator, fail_closed=True):+ LOG.warning(+ "Associated checkbox label contains an interactive descendant",+ element_id=element.get_id(),+ should_check=should_check,+ )+ return False+ await label_locator.click(timeout=settings.BROWSER_ACTION_TIMEOUT_MS)+ return await locator.is_checked(timeout=settings.BROWSER_ACTION_TIMEOUT_MS) == should_check+ except Exception:+ LOG.warning(+ "Failed to set checkbox state through associated label",+ element_id=element.get_id(),+ should_check=should_check,+ exc_info=True,+ )+ return False+++_LABEL_CONTROL_STATE_JS = r"""+(el) => {+ const control = el.control;+ if (!control) return null;+ if (!el.hasAttribute("for")) {+ const controls = Array.from(+ el.querySelectorAll("button, input, meter, output, progress, select, textarea")+ ).filter((c) => !(c instanceof HTMLInputElement && c.type === "hidden"));+ if (controls.length !== 1 || control !== controls[0]) return null;+ }+ if (control instanceof HTMLInputElement && (control.type === "checkbox" || control.type === "radio")) {+ return control.checked;+ }+ return null;+}+"""+++async def _read_label_control_state(label: SkyvernElement) -> bool | None:+ """Read the live checked state of a label's bound control the way browser label activation+ resolves it, via the browser's own ``HTMLLabelElement.control``. For an explicit ``for=`` label+ the id-referenced labelable element is used directly (a for= label never falls back to+ descendants); for an implicit label the single labelable descendant at any depth or visibility+ (no scraped-interactability filter) is cross-checked against ``el.control``. Returns None -- fall+ open -- when no control resolves, when an implicit label has zero or several labelable descendants+ or the browser disagrees with the unique candidate, when the control is not a native+ checkbox/radio, or when the read fails. State is read in-page rather than via a mapped+ SkyvernElement because a display:none control is never scraped and has no unique_id."""+ try:+ state = await _evaluate_element_scoped(label, _LABEL_CONTROL_STATE_JS)+ except Exception:+ LOG.debug(+ "Failed to read label control state; continuing with an ordinary click",+ element_id=label.get_id(),+ )+ return None+ return state if isinstance(state, bool) else None+++async def _resolve_effective_click_state(element: SkyvernElement, dom: DomUtil) -> bool | None:+ """Live selected/checked state of the control a click on ``element`` actually toggles: the+ element's own observable, or, for a <label>, its spec-bound control (mapped explicit for=+ control first, else the in-page label.control read). None = unreadable; callers fall open."""+ if element.get_tag_name() != "label":+ return await _resolve_live_selected_state(element)+ control = await element.find_label_for(dom)+ if control is not None:+ return await _resolve_live_selected_state(control)+ return await _read_label_control_state(element)+++async def _apply_label_desired_click_state(+ action: actions.ClickAction, label: SkyvernElement, desired_state: bool, dom: DomUtil+) -> list[ActionResult] | None:+ """A <label> click forwards activation to its spec-bound control, so observe that control's+ live state rather than the label's (labels carry no checked/selected observable). Suppress the+ redundant click when the bound control already holds the desired state; on a mismatch, or when+ no bound control resolves or its state is unreadable, fall through to a single ordinary label+ click, whose forwarding performs the one toggle. The control is resolved deterministically via+ the spec-defined association (explicit for=-id, else the wrapped labelable descendant read+ in-page); state is never driven through the label."""+ control_state = await _resolve_effective_click_state(label, dom)+ if control_state is None:+ LOG.info("Label click has no readable bound-control state, continuing the normal click", action=action)+ return None+ if control_state == desired_state:+ LOG.info(+ "Label's bound control already in the desired state, suppressing the redundant click",+ action=action,+ desired_state=desired_state,+ )+ return [ActionAbort()]+ LOG.info(+ "Label's bound control differs from the desired state, continuing with a single normal click",+ action=action,+ desired_state=desired_state,+ )+ return None+++async def _apply_desired_click_state(+ action: actions.ClickAction, element: SkyvernElement, desired_state: bool, dom: DomUtil+) -> list[ActionResult] | None:+ """Drive a selectable control to an explicit terminal state idempotently. Returns+ [ActionAbort()] to suppress the physical click -- when the control already matches the desired+ state, or after a native checkbox/radio is set here -- or None to fall through to a single+ ordinary click when the live state is unreadable (fail open) or a custom control must be clicked+ once to change. Never converts an explicit desired_state=False into a check."""+ if element.get_tag_name() == "label":+ return await _apply_label_desired_click_state(action, element, desired_state, dom)+ live_state = await _resolve_live_selected_state(element)+ if live_state is None:+ LOG.info("No readable selected state, continuing the normal click", action=action)+ return None+ if live_state == desired_state:+ LOG.info(+ "Control already in the desired state, suppressing the redundant click",+ action=action,+ desired_state=desired_state,+ )+ return [ActionAbort()]+ if element.get_tag_name() == "input":+ input_type = (await element.get_attr("type") or "").strip().casefold()+ if input_type == "radio" and not desired_state:+ # A radio can't be turned off by clicking it -- only selecting another radio in the+ # group clears it -- so skip the doomed uncheck() and let a single ordinary click run.+ LOG.info("A radio can't be unchecked in place, continuing with a single normal click", action=action)+ return None+ LOG.info("Setting the native control to the desired state", action=action, desired_state=desired_state)+ if await _set_native_checkbox_state(element, should_check=desired_state):+ return [ActionAbort()]+ LOG.warning("Failed to set the native control to the desired state, continuing the normal click", action=action)+ return None+ LOG.info(+ "Custom control differs from the desired state, continuing with a single normal click",+ action=action,+ desired_state=desired_state,+ )+ return None++@traced(name="skyvern.agent.action.click")async def handle_click_action(action: actions.ClickAction,@@ -4611,6 +4864,17 @@ async def handle_click_action(# Wait after getting element to allow any dynamic changesawait asyncio.sleep(get_wait_time(wait_config, "post_click_delay", default=0.3))+ # Level-triggered toggle intent (ClickContext.desired_state) is resolved here, with the live+ # element in hand and before any physical click: suppress a redundant click when the control+ # already holds the desired state, drive a native checkbox/radio to it, or fall through to a+ # single ordinary click for a custom-control mismatch or an unreadable state.+ if action.click_context is not None and action.click_context.desired_state is not None:+ desired_state_result = await _apply_desired_click_state(+ action, skyvern_element, action.click_context.desired_state, dom+ )+ if desired_state_result is not None:+ return desired_state_result+# dynamically validate the attr, since it could change into enabled after the previous actionsif await skyvern_element.is_disabled(dynamic=True):child = await _retarget_disabled_element_for_click(@@ -4620,6 +4884,15 @@ async def handle_click_action()if child is not None:skyvern_element = child+ # Retarget moved the click to the descendant that will actually receive it, so re-run+ # the same guard on the child: the pre-retarget pass observed the (unreadable) wrapper+ # and fell open, and without this the retargeted child could still be re-toggled.+ if action.click_context is not None and action.click_context.desired_state is not None:+ desired_state_result = await _apply_desired_click_state(+ action, skyvern_element, action.click_context.desired_state, dom+ )+ if desired_state_result is not None:+ return desired_state_resultelif not await SkyvernElement.wait_until_enabled(skyvern_element):LOG.warning("Try to click on a disabled element",diff --git a/tests/unit/golden_prompts/extract-action-static.control.txt b/tests/unit/golden_prompts/extract-action-static.control.txtindex 00f7e68c4..95073bdec 100644--- a/tests/unit/golden_prompts/extract-action-static.control.txt+++ b/tests/unit/golden_prompts/extract-action-static.control.txt@@ -39,6 +39,7 @@ Reply in JSON format with the following keys:"click_context": { // The context for CLICK action only. null if not CLICK action"thought": str, // Describe how you decided that this action is a single choice option or multi-choice option."single_option_click": bool, // True if the click is the only choice to proceed towards the goal, regardless of different user context or input. False if there are multiple valid options that depend on user input. Examples: clicking a login button to login is True (it's the only way to login); clicking a radio button for a multi-choice question (e.g., selecting "male", "female", or "other" for gender) is False (the choice depends on user input). When clicking on radio buttons, dropdown options, or any element that represents one of multiple possible selections, this should be False. If the click is intended to open the dropdown menu in order to select an option from the dropdown, this should be False.+ "desired_state": bool, // Set this ONLY when the CLICK is meant to drive a selectable/toggle control — a checkbox, switch, radio, or an option/tab that reports a selected state — to a terminal on/off state. true means leave the control selected/checked/on; false means leave it unselected/unchecked/off. Selecting a radio button or an option is generally true; explicitly turning a checkbox or switch off is false. Use null for ordinary buttons, links, a click that only opens a dropdown/menu, or anything that is not setting a control to a terminal on/off state.},"context": { // The context for INPUT_TEXT or SELECT_OPTION action only. null if not INPUT_TEXT or SELECT_OPTION action. Extract the following detailed information from the "reasoning", and double-check the information by analysing the HTML elements."thought": str, // A string to describe how you double-check the context information to ensure the accuracy.diff --git a/tests/unit/golden_prompts/extract-action.control.txt b/tests/unit/golden_prompts/extract-action.control.txtindex 380be5afd..c3b53a69f 100644--- a/tests/unit/golden_prompts/extract-action.control.txt+++ b/tests/unit/golden_prompts/extract-action.control.txt@@ -39,6 +39,7 @@ Reply in JSON format with the following keys:"click_context": { // The context for CLICK action only. null if not CLICK action"thought": str, // Describe how you decided that this action is a single choice option or multi-choice option."single_option_click": bool, // True if the click is the only choice to proceed towards the goal, regardless of different user context or input. False if there are multiple valid options that depend on user input. Examples: clicking a login button to login is True (it's the only way to login); clicking a radio button for a multi-choice question (e.g., selecting "male", "female", or "other" for gender) is False (the choice depends on user input). When clicking on radio buttons, dropdown options, or any element that represents one of multiple possible selections, this should be False. If the click is intended to open the dropdown menu in order to select an option from the dropdown, this should be False.+ "desired_state": bool, // Set this ONLY when the CLICK is meant to drive a selectable/toggle control — a checkbox, switch, radio, or an option/tab that reports a selected state — to a terminal on/off state. true means leave the control selected/checked/on; false means leave it unselected/unchecked/off. Selecting a radio button or an option is generally true; explicitly turning a checkbox or switch off is false. Use null for ordinary buttons, links, a click that only opens a dropdown/menu, or anything that is not setting a control to a terminal on/off state.},"context": { // The context for INPUT_TEXT or SELECT_OPTION action only. null if not INPUT_TEXT or SELECT_OPTION action. Extract the following detailed information from the "reasoning", and double-check the information by analysing the HTML elements."thought": str, // A string to describe how you double-check the context information to ensure the accuracy.diff --git a/tests/unit/test_actions.py b/tests/unit/test_actions.pyindex 1d4525668..896d705d0 100644--- a/tests/unit/test_actions.py+++ b/tests/unit/test_actions.py@@ -1,4 +1,5 @@from datetime import datetime+from pathlib import Pathfrom unittest.mock import MagicMockimport pytest@@ -25,6 +26,7 @@ from skyvern.webeye.actions.actions import (Action,ActionStatus,ClickAction,+ ClickContext,ClosePageAction,ExtractAction,GotoUrlAction,@@ -819,3 +821,65 @@ def test_tab_actions_registered_for_db_hydration() -> None:assert ACTION_TYPE_TO_CLASS[ActionType.NEW_TAB] is NewTabActionassert ACTION_TYPE_TO_CLASS[ActionType.SWITCH_TAB] is SwitchTabAction+++# ---------------------------------------------------------------------------+# ClickContext.desired_state — level-triggered toggle intent (SKY-13916)+# ---------------------------------------------------------------------------++_CLICK_PROMPT_DIR = Path(__file__).parent.parent.parent / "skyvern" / "forge" / "prompts" / "skyvern"+++def test_click_context_desired_state_defaults_to_none() -> None:+ assert ClickContext().desired_state is None+ assert ClickContext(single_option_click=True).desired_state is None++[email protected]("desired_state", [True, False, None])+def test_click_action_parse_roundtrips_desired_state(desired_state: bool | None) -> None:+ action = parse_action(+ action={+ "action_type": "CLICK",+ "element_id": "e1",+ "reasoning": "toggle the control",+ "click_context": {"single_option_click": False, "desired_state": desired_state},+ },+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.click_context is not None+ assert action.click_context.desired_state is desired_state+++def test_click_action_parse_legacy_without_click_context_is_none() -> None:+ action = parse_action(+ action={"action_type": "CLICK", "element_id": "e1", "reasoning": "click the button"},+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.click_context is None+++def test_click_action_parse_legacy_click_context_without_desired_state_is_none() -> None:+ action = parse_action(+ action={+ "action_type": "CLICK",+ "element_id": "e1",+ "reasoning": "click the option",+ "click_context": {"single_option_click": True},+ },+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.click_context is not None+ assert action.click_context.desired_state is None+++ "template",+ ["extract-action.j2", "extract-action-static.j2", "single-click-action.j2"],+)+def test_desired_state_documented_in_click_prompts(template: str) -> None:+ # The planner prompts and the cached single-click re-derivation prompt must offer+ # desired_state so the deterministic setup guard can read level-triggered toggle intent.+ assert "desired_state" in (_CLICK_PROMPT_DIR / template).read_text()diff --git a/tests/unit/test_browser_effect_sink_enumeration.py b/tests/unit/test_browser_effect_sink_enumeration.pyindex 04540b128..80e4c2ade 100644--- a/tests/unit/test_browser_effect_sink_enumeration.py+++ b/tests/unit/test_browser_effect_sink_enumeration.py@@ -20,8 +20,8 @@ _DISCOVERED_BROWSER_API_CALLS = {"skyvern/webeye/actions/handler.py": Counter({"bring_to_front": 2,- "check": 1,- "click": 21,+ "check": 2,+ "click": 22,"clear": 4,"close": 5,"dblclick": 2,@@ -41,7 +41,7 @@ _DISCOVERED_BROWSER_API_CALLS = {"send": 3,"set_files": 2,"set_input_files": 1,- "uncheck": 1,+ "uncheck": 2,"wheel": 5,}),@@ -201,14 +201,14 @@ def test_discovered_browser_api_lower_bound_is_stable() -> None:}assert observed == _DISCOVERED_BROWSER_API_CALLS- assert sum(sum(methods.values()) for methods in observed.values()) == 143+ assert sum(sum(methods.values()) for methods in observed.values()) == 146handler_candidates = _candidate_signatures("skyvern/webeye/actions/handler.py", _CANDIDATE_METHODS)classified_non_browser = Counter({signature: count for signature, count in handler_candidates.items() if signature in _NON_BROWSER_CANDIDATES})assert classified_non_browser == _NON_BROWSER_CANDIDATESassert sum(_NON_BROWSER_CANDIDATES.values()) == 5- assert sum(sum(methods.values()) for methods in observed.values()) - sum(_NON_BROWSER_CANDIDATES.values()) == 138+ assert sum(sum(methods.values()) for methods in observed.values()) - sum(_NON_BROWSER_CANDIDATES.values()) == 141def test_every_raw_evaluate_call_is_classified() -> None:diff --git a/tests/unit/test_click_desired_state.py b/tests/unit/test_click_desired_state.pynew file mode 100644index 000000000..bd378b165--- /dev/null+++ b/tests/unit/test_click_desired_state.py@@ -0,0 +1,803 @@+"""Deterministic ``ClickContext.desired_state`` execution inside+``handle_click_action`` (SKY-13916).++The level-triggered toggle guard lives in the shared OSS click handler: it reads+one live selected-state observable after the element is resolved and before the+physical click, then suppresses the click when the control already holds the+desired state, drives a native checkbox/radio to the desired state, or falls+through to a single ordinary click for a custom-control mismatch or an unreadable+state. Role never implies intent — it only chooses which observable to read.+"""++from __future__ import annotations++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from skyvern.webeye.actions import handler+from skyvern.webeye.actions.actions import ClickAction, ClickContext+from skyvern.webeye.actions.responses import ActionAbort, ActionSuccess+++class FakeLabelLocator:+ def __init__(+ self,+ checkbox_locator: FakeCheckboxLocator,+ visible: bool = True,+ exists: bool = True,+ present_descendants: list[str] | None = None,+ descendant_inspection_error: Exception | None = None,+ ) -> None:+ self.checkbox_locator = checkbox_locator+ self.visible = visible+ self.exists = exists+ self.present_descendants = present_descendants or []+ self.descendant_inspection_error = descendant_inspection_error+ self.click_count = 0++ @property+ def first(self) -> FakeLabelLocator:+ return self++ async def count(self) -> int:+ return int(self.exists)++ async def is_visible(self) -> bool:+ return self.visible++ async def click(self, timeout: int | None = None, position: dict[str, int] | None = None) -> None:+ self.click_count += 1+ self.checkbox_locator.checked = not self.checkbox_locator.checked++ def locator(self, selector: str) -> FakeInteractiveDescendantLocator:+ return FakeInteractiveDescendantLocator(+ selector=selector,+ present_descendants=self.present_descendants,+ inspection_error=self.descendant_inspection_error,+ )+++class FakeInteractiveDescendantLocator:+ def __init__(self, selector: str, present_descendants: list[str], inspection_error: Exception | None) -> None:+ self.selector = selector+ self.present_descendants = present_descendants+ self.inspection_error = inspection_error++ async def count(self) -> int:+ if self.inspection_error is not None:+ raise self.inspection_error+ queried = {part.strip() for part in self.selector.split(",")}+ return sum(1 for descendant in self.present_descendants if descendant in queried)+++class FakeCheckboxLocator:+ def __init__(self, checked: bool) -> None:+ self.checked = checked+ self.label_locator = FakeLabelLocator(self)++ async def is_checked(self, timeout: int | None = None) -> bool:+ return self.checked++ def locator(self, selector: str) -> FakeLabelLocator:+ assert selector == "xpath=ancestor::label[1]"+ return self.label_locator+++class FakeCheckboxElement:+ """Minimal SkyvernElement stand-in for driving ``_set_native_checkbox_state``."""++ def __init__(self, checked: bool, input_toggle_fails: bool = False) -> None:+ self.locator = FakeCheckboxLocator(checked)+ self.explicit_label_locator = FakeLabelLocator(self.locator)+ self.input_toggle_fails = input_toggle_fails+ self.check_count = 0+ self.uncheck_count = 0++ def get_locator(self) -> FakeCheckboxLocator:+ return self.locator++ def get_id(self) -> str:+ return "checkbox-id"++ async def get_attr(self, attr_name: str, mode: str | None = None) -> str | None:+ assert attr_name == "id"+ return "checkbox-id"++ def get_frame(self) -> FakeFrame:+ return FakeFrame(self.explicit_label_locator)++ async def check(self) -> None:+ self.check_count += 1+ if self.input_toggle_fails:+ raise RuntimeError("input is not visible")+ self.locator.checked = True++ async def uncheck(self) -> None:+ self.uncheck_count += 1+ if self.input_toggle_fails:+ raise RuntimeError("input is not visible")+ self.locator.checked = False+++class FakeFrame:+ def __init__(self, explicit_label_locator: FakeLabelLocator) -> None:+ self.explicit_label_locator = explicit_label_locator++ def locator(self, selector: str) -> FakeLabelLocator:+ assert selector == 'label[for="checkbox-id"]'+ return self.explicit_label_locator+++class FakeAncestorLocator:+ """Resolves the nearest ancestor-or-self ``[aria-multiselectable]`` for+ ``_is_single_select_option_highlight``: ``value`` is that attribute's value, or ``None`` when no+ such ancestor exists (count 0)."""++ def __init__(self, value: str | None) -> None:+ self._value = value++ async def count(self) -> int:+ return 0 if self._value is None else 1++ async def get_attribute(self, name: str) -> str | None:+ assert name == "aria-multiselectable"+ return self._value+++class FakeElementLocator:+ def __init__(self, multiselectable_ancestor: str | None) -> None:+ self._multiselectable_ancestor = multiselectable_ancestor++ def locator(self, selector: str) -> FakeAncestorLocator:+ assert selector == "xpath=ancestor-or-self::*[@aria-multiselectable][1]"+ return FakeAncestorLocator(self._multiselectable_ancestor)+++class FakeToggleElement:+ """Attribute reads are served from a static dict; ``checked`` is the live value+ returned by ``is_checked()`` for native inputs (``None`` models an unreadable+ input); ``attr_error`` simulates a detached element when a live ARIA attribute+ is queried; ``multiselectable_ancestor`` is the value of the nearest ancestor-or-self+ ``[aria-multiselectable]`` (``None`` models no such ancestor)."""++ def __init__(+ self,+ tag_name: str,+ attributes: dict[str, str] | None = None,+ *,+ checked: bool | None = None,+ attr_error: Exception | None = None,+ multiselectable_ancestor: str | None = None,+ ) -> None:+ self._tag_name = tag_name+ self._attributes = attributes or {}+ self._checked = checked+ self._attr_error = attr_error+ self._locator = FakeElementLocator(multiselectable_ancestor)++ def get_tag_name(self) -> str:+ return self._tag_name++ def get_id(self) -> str:+ return "el"++ def get_locator(self) -> FakeElementLocator:+ return self._locator++ async def get_attr(self, attr_name: str, mode: str = "auto", timeout: float | None = None) -> str | None:+ if self._attr_error is not None and attr_name in ("aria-selected", "aria-checked", "aria-pressed"):+ raise self._attr_error+ return self._attributes.get(attr_name)++ async def is_checked(self, timeout: float | None = None) -> bool | None:+ return self._checked+++def _click_action() -> ClickAction:+ return ClickAction(element_id="el", reasoning="toggle")+++# ---------------------------------------------------------------------------+# _resolve_live_selected_state — one generic, live observable of selectedness+# ---------------------------------------------------------------------------++[email protected]("input_type", ["checkbox", "radio"])[email protected]("checked", [True, False])+async def test_resolve_reads_native_input_is_checked(input_type: str, checked: bool) -> None:+ element = FakeToggleElement("input", {"type": input_type}, checked=checked)+ assert await handler._resolve_live_selected_state(element) is checked+++async def test_resolve_non_toggle_input_returns_none() -> None:+ element = FakeToggleElement("input", {"type": "text"}, checked=True)+ assert await handler._resolve_live_selected_state(element) is None++[email protected]("aria_attr", ["aria-checked", "aria-pressed", "aria-selected"])[email protected]("value,expected", [("true", True), ("false", False)])+async def test_resolve_reads_exact_aria_boolean(aria_attr: str, value: str, expected: bool) -> None:+ # A multiselectable container keeps a bare aria-selected readable as committed state; without it+ # a single-select option's aria-selected="true" is treated as an unreadable highlight (below).+ element = FakeToggleElement("div", {"role": "option", aria_attr: value}, multiselectable_ancestor="true")+ assert await handler._resolve_live_selected_state(element) is expected+++async def test_resolve_single_select_option_aria_selected_true_is_unreadable() -> None:+ # A single-select option's aria-selected="true" is the pre-commit keyboard highlight, not+ # committed state, so it must read as unreadable (None) and let the click commit the selection.+ element = FakeToggleElement("div", {"role": "option", "aria-selected": "true"})+ assert await handler._resolve_live_selected_state(element) is None+++async def test_resolve_multiselect_option_aria_selected_true_reads_true() -> None:+ element = FakeToggleElement("div", {"role": "option", "aria-selected": "true"}, multiselectable_ancestor="true")+ assert await handler._resolve_live_selected_state(element) is True+++async def test_resolve_tab_aria_selected_true_reads_true() -> None:+ # aria-selected on a tab is committed state; only role=option treats it as a highlight.+ element = FakeToggleElement("div", {"role": "tab", "aria-selected": "true"})+ assert await handler._resolve_live_selected_state(element) is True+++async def test_resolve_prefers_aria_checked_over_pressed_and_selected() -> None:+ element = FakeToggleElement("div", {"aria-checked": "false", "aria-pressed": "true", "aria-selected": "true"})+ assert await handler._resolve_live_selected_state(element) is False++[email protected]("aria_value", ["yes", "TRUE?", "mixed", ""])+async def test_resolve_malformed_aria_returns_none(aria_value: str) -> None:+ element = FakeToggleElement("div", {"role": "option", "aria-selected": aria_value})+ assert await handler._resolve_live_selected_state(element) is None+++async def test_resolve_read_exception_returns_none() -> None:+ element = FakeToggleElement("div", {"role": "option"}, attr_error=RuntimeError("detached"))+ assert await handler._resolve_live_selected_state(element) is None++[email protected]("attributes", [{}, {"role": "button"}, {"href": "https://example.test/next"}])+async def test_resolve_control_without_boolean_observable_returns_none(attributes: dict[str, str]) -> None:+ element = FakeToggleElement("button", attributes)+ assert await handler._resolve_live_selected_state(element) is None+++# ---------------------------------------------------------------------------+# _apply_desired_click_state — [ActionAbort()] suppresses; None falls through+# ---------------------------------------------------------------------------++[email protected]("aria_attr", ["aria-selected", "aria-checked", "aria-pressed"])+async def test_apply_custom_control_matching_true_suppresses(aria_attr: str) -> None:+ # Multiselectable so the aria-selected param is committed state, not a single-select highlight.+ element = FakeToggleElement("div", {"role": "option", aria_attr: "true"}, multiselectable_ancestor="true")+ result = await handler._apply_desired_click_state(_click_action(), element, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_custom_control_matching_false_suppresses() -> None:+ element = FakeToggleElement("div", {"role": "switch", "aria-checked": "false"})+ result = await handler._apply_desired_click_state(_click_action(), element, False, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_custom_control_mismatch_true_falls_through_one_click() -> None:+ element = FakeToggleElement("div", {"role": "option", "aria-selected": "false"})+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None+++async def test_apply_single_select_option_aria_selected_false_desired_false_suppresses() -> None:+ # The highlight ambiguity only poisons aria-selected="true"; a single-select option's+ # aria-selected="false" stays readable, so desired_state=False suppresses (clicking it would+ # select the option) instead of falling open to a click.+ element = FakeToggleElement("div", {"role": "option", "aria-selected": "false"})+ result = await handler._apply_desired_click_state(_click_action(), element, False, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_custom_control_mismatch_false_falls_through_without_checking() -> None:+ element = FakeToggleElement("button", {"aria-pressed": "true"})+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ assert await handler._apply_desired_click_state(_click_action(), element, False, MagicMock()) is None+ set_state.assert_not_awaited()++[email protected]("aria_value", ["yes", "TRUE?", "mixed", ""])+async def test_apply_malformed_custom_state_falls_open(aria_value: str) -> None:+ element = FakeToggleElement("div", {"role": "option", "aria-selected": aria_value})+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None+++async def test_apply_unreadable_custom_state_falls_open() -> None:+ element = FakeToggleElement("div", {"role": "option"}, attr_error=RuntimeError("detached"))+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None+++ "tag_name,attributes",+ [("button", {}), ("button", {"role": "button"}), ("a", {"href": "https://example.test/next"})],+)+async def test_apply_desired_state_on_non_selectable_control_falls_open(+ tag_name: str, attributes: dict[str, str]+) -> None:+ element = FakeToggleElement(tag_name, attributes)+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None++[email protected]("input_type", ["checkbox", "radio"])+async def test_apply_native_input_desired_true_when_unchecked_sets_and_suppresses(input_type: str) -> None:+ element = FakeToggleElement("input", {"type": input_type}, checked=False)+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ result = await handler._apply_desired_click_state(_click_action(), element, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+ set_state.assert_awaited_once()+ assert set_state.await_args.kwargs["should_check"] is True+++async def test_apply_native_checkbox_desired_false_when_checked_unsets_and_suppresses() -> None:+ element = FakeToggleElement("input", {"type": "checkbox"}, checked=True)+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ result = await handler._apply_desired_click_state(_click_action(), element, False, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+ set_state.assert_awaited_once()+ assert set_state.await_args.kwargs["should_check"] is False+++async def test_apply_native_radio_desired_false_when_checked_falls_through_without_setter() -> None:+ # A radio can't be turned off by clicking it (only selecting another radio clears it), so an+ # explicit desired_state=False on a checked radio must skip the doomed uncheck() and fall+ # through to a single ordinary click rather than a wasted state-setter attempt.+ element = FakeToggleElement("input", {"type": "radio"}, checked=True)+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ assert await handler._apply_desired_click_state(_click_action(), element, False, MagicMock()) is None+ set_state.assert_not_awaited()+++async def test_apply_native_checkbox_matches_live_suppresses_without_setter() -> None:+ element = FakeToggleElement("input", {"type": "checkbox"}, checked=True)+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ result = await handler._apply_desired_click_state(_click_action(), element, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+ set_state.assert_not_awaited()+++async def test_apply_native_checkbox_unreadable_live_state_falls_open() -> None:+ element = FakeToggleElement("input", {"type": "checkbox"}, checked=None)+ set_state = AsyncMock(return_value=True)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None+ set_state.assert_not_awaited()+++async def test_apply_native_checkbox_setter_failure_falls_through() -> None:+ element = FakeToggleElement("input", {"type": "checkbox"}, checked=False)+ set_state = AsyncMock(return_value=False)+ with patch.object(handler, "_set_native_checkbox_state", set_state):+ assert await handler._apply_desired_click_state(_click_action(), element, True, MagicMock()) is None+ set_state.assert_awaited_once()+++# ---------------------------------------------------------------------------+# _set_native_checkbox_state — native input set with associated-label fallback+# ---------------------------------------------------------------------------+++async def test_native_set_uses_input_when_actionable() -> None:+ element = FakeCheckboxElement(checked=False)+ assert await handler._set_native_checkbox_state(element, should_check=True)+ assert element.locator.checked is True+ assert element.check_count == 1+ assert element.locator.label_locator.click_count == 0+++async def test_native_set_falls_back_to_visible_label_for_hidden_input() -> None:+ element = FakeCheckboxElement(checked=False, input_toggle_fails=True)+ assert await handler._set_native_checkbox_state(element, should_check=True)+ assert element.locator.checked is True+ assert element.locator.label_locator.click_count == 1+++async def test_native_set_skips_when_already_matching() -> None:+ element = FakeCheckboxElement(checked=True)+ assert await handler._set_native_checkbox_state(element, should_check=True)+ assert element.check_count == 0+ assert element.uncheck_count == 0+ assert element.locator.label_locator.click_count == 0+++# ---------------------------------------------------------------------------+# handle_click_action — the guard runs after element resolution, before the click+# ---------------------------------------------------------------------------+++async def _run_handle_click(*, click_context: ClickContext | None, element: MagicMock) -> tuple[list, AsyncMock]:+ dom = MagicMock()+ dom.get_skyvern_element_by_id = AsyncMock(return_value=element)+ page = MagicMock(url="https://example.test/page")+ page.evaluate = AsyncMock(return_value=False)+ chain = AsyncMock(return_value=[ActionSuccess()])+ incremental = MagicMock()+ incremental.start_listen_dom_increment = AsyncMock()+ incremental.stop_listen_dom_increment = AsyncMock()++ with (+ patch.object(handler, "DomUtil", return_value=dom),+ patch.object(handler, "get_or_create_wait_config", new=AsyncMock(return_value=None)),+ patch.object(handler.asyncio, "sleep", new=AsyncMock()),+ patch.object(handler, "chain_click", new=chain),+ patch.object(handler, "resolve_engine_selection_for_task", MagicMock(return_value=None)),+ patch.object(handler.SkyvernFrame, "create_instance", new=AsyncMock(return_value=MagicMock())),+ patch.object(handler, "IncrementalScrapePage", return_value=incremental),+ patch.object(handler, "handle_sequential_click_for_dropdown", new=AsyncMock(return_value=None)),+ ):+ action = ClickAction(element_id="control", click_context=click_context)+ results = await handler.handle_click_action(action, page, MagicMock(), MagicMock(), MagicMock())+ return results, chain+++def _fall_through_element(tag_name: str, attr_value: str | None) -> MagicMock:+ element = MagicMock()+ element.get_id.return_value = "control"+ element.get_tag_name.return_value = tag_name+ element.get_attr = AsyncMock(return_value=attr_value)+ element.is_disabled = AsyncMock(return_value=False)+ element.scroll_into_view = AsyncMock()+ element.get_element_handler = AsyncMock(return_value=MagicMock())+ element.has_attr = AsyncMock(return_value=False)+ element.get_frame.return_value = MagicMock()+ return element+++def _single_select_option_element() -> MagicMock:+ """A role=option reporting aria-selected="true" with no multiselectable ancestor — the guard+ must read this as an unreadable highlight and fall open to the physical click."""+ attrs = {"role": "option", "aria-selected": "true"}+ element = MagicMock()+ element.get_id.return_value = "control"+ element.get_tag_name.return_value = "div"+ element.get_attr = AsyncMock(side_effect=lambda name, *args, **kwargs: attrs.get(name))+ element.get_locator.return_value = FakeElementLocator(None)+ element.is_disabled = AsyncMock(return_value=False)+ element.scroll_into_view = AsyncMock()+ element.get_element_handler = AsyncMock(return_value=MagicMock())+ element.has_attr = AsyncMock(return_value=False)+ element.get_frame.return_value = MagicMock()+ return element+++async def test_handle_click_suppresses_already_satisfied_state() -> None:+ element = _fall_through_element("div", "true")+ results, chain = await _run_handle_click(click_context=ClickContext(desired_state=True), element=element)++ assert len(results) == 1 and isinstance(results[0], ActionAbort)+ chain.assert_not_awaited()+++async def test_handle_click_without_desired_state_runs_physical_click() -> None:+ element = _fall_through_element("button", None)+ results, chain = await _run_handle_click(click_context=None, element=element)++ chain.assert_awaited_once()+ assert results and isinstance(results[-1], ActionSuccess)+++async def test_handle_click_custom_mismatch_falls_through_to_physical_click() -> None:+ element = _fall_through_element("div", "false")+ _results, chain = await _run_handle_click(click_context=ClickContext(desired_state=True), element=element)++ chain.assert_awaited_once()+++async def test_apply_single_select_option_highlight_falls_open_one_click() -> None:+ # A single-select option's aria-selected="true" is only a highlight, so through the full handler+ # the guard falls open and the physical click still commits the selection exactly once.+ element = _single_select_option_element()+ _results, chain = await _run_handle_click(click_context=ClickContext(desired_state=True), element=element)++ chain.assert_awaited_once()+++# ---------------------------------------------------------------------------+# <label> targets — observe the spec-bound control (for= / wrapped input), not the+# label. Equal suppresses; mismatch / unresolved / unreadable falls through to one+# ordinary label click (whose forwarding performs the single toggle). Never drives+# the control's state through the label.+# ---------------------------------------------------------------------------+++class FakeLabelElement:+ def __init__(+ self,+ *,+ label_for: FakeToggleElement | None = None,+ has_for_attr: bool = False,+ wrapped_state: bool | None = None,+ for_control_state: bool | None = None,+ wrapped_error: Exception | None = None,+ ) -> None:+ self._label_for = label_for+ self.has_for_attr = has_for_attr+ self.wrapped_state = wrapped_state+ self.for_control_state = for_control_state+ self.wrapped_error = wrapped_error++ def get_tag_name(self) -> str:+ return "label"++ def get_id(self) -> str:+ return "label-el"++ async def find_label_for(self, dom: object) -> FakeToggleElement | None:+ return self._label_for+++async def _fake_evaluate_element_scoped(element: FakeLabelElement, expression: str, arg: object = None) -> bool | None:+ """Stand-in for the in-page label-control read (``_evaluate_element_scoped`` + JS). Models the JS+ outcome from the label's spec so assertions target behavior, not JS text: a configured error+ models a probe failure; an explicit ``for=`` label returns ``el.control``'s checked state+ (``for_control_state`` -- a bool for a resolved hidden toggle, None for a dangling / non-labelable+ / non-toggle control, with no descendant fallback); an implicit label returns ``wrapped_state``+ (the resolved single labelable descendant, None for zero / multiple / non-toggle). Routed through+ the real ``_read_label_control_state``, so its exception -> None and non-bool -> None handling is+ exercised too."""+ if element.wrapped_error is not None:+ raise element.wrapped_error+ if element.has_for_attr:+ return element.for_control_state+ return element.wrapped_state+++async def test_apply_label_target_reads_bound_control_state_and_suppresses() -> None:+ control = FakeToggleElement("input", {"type": "checkbox"}, checked=True)+ label = FakeLabelElement(label_for=control)+ result = await handler._apply_desired_click_state(_click_action(), label, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_label_target_mismatch_falls_through_one_click() -> None:+ control = FakeToggleElement("input", {"type": "checkbox"}, checked=False)+ label = FakeLabelElement(label_for=control)+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_bound_control_unreadable_state_falls_open() -> None:+ control = FakeToggleElement("input", {"type": "checkbox"}, checked=None)+ label = FakeLabelElement(label_for=control)+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_wrapped_control_live_state_matches_suppresses() -> None:+ # No for= and no scraped child resolves, so a deep-nested or display:none wrapped checkbox is+ # read live in-page. Already checked + desired_state=True -> suppress the redundant click that+ # would otherwise uncheck it (the double-toggle this guard exists to prevent).+ label = FakeLabelElement(wrapped_state=True)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ result = await handler._apply_desired_click_state(_click_action(), label, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_label_wrapped_control_live_state_mismatch_falls_through_one_click() -> None:+ # Live state differs from the desired state: fall through to one ordinary label click (whose+ # forwarding performs the single toggle) and never drive the control's state through the label.+ label = FakeLabelElement(wrapped_state=False)+ set_state = AsyncMock(return_value=True)+ with (+ patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped),+ patch.object(handler, "_set_native_checkbox_state", set_state),+ ):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+ set_state.assert_not_awaited()+++async def test_apply_label_wrapped_control_unresolvable_falls_open() -> None:+ # evaluate -> None models zero / multiple / non-checkbox-radio labelable descendants (or a+ # browser/candidate disagreement): the state is unreadable, so fall open to an ordinary click.+ label = FakeLabelElement(wrapped_state=None)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_wrapped_control_probe_failure_falls_open() -> None:+ label = FakeLabelElement(wrapped_error=RuntimeError("detached"))+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_with_for_attr_never_reads_descendants() -> None:+ # Per the HTML labeled-control algorithm a for= label never falls back to descendants. When+ # el.control does not resolve to a native toggle (for_control_state=None), even a resolvable+ # checked wrapped descendant (wrapped_state=True) must not suppress: the JS gate -- mirrored by+ # the fake, which never consults wrapped_state once has_for_attr is set -- skips the descendant+ # scan for for= labels, so fall open to a single ordinary click.+ label = FakeLabelElement(has_for_attr=True, for_control_state=None, wrapped_state=True)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_wrapped_input_child_resolves_and_suppresses() -> None:+ # Migrated off the removed static-tree child stub: the wrapped control's state is now read+ # in-page. Already unchecked + desired_state=False -> suppress (never toggle an already-satisfied+ # control on), exercising the guard's other suppression direction.+ label = FakeLabelElement(wrapped_state=False)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ result = await handler._apply_desired_click_state(_click_action(), label, False, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_label_explicit_for_hidden_control_matches_suppresses() -> None:+ # <label for="x"> pointing at a display:none checked checkbox: the hidden input is never scraped+ # (no unique_id), so find_label_for cannot map it and the control's checked state is read in-page+ # via el.control. Already checked + desired_state=True -> suppress the click that HTML label+ # activation would otherwise forward to the hidden input, unchecking it.+ label = FakeLabelElement(has_for_attr=True, label_for=None, for_control_state=True)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ result = await handler._apply_desired_click_state(_click_action(), label, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++async def test_apply_label_explicit_for_hidden_control_mismatch_falls_through_one_click() -> None:+ # Explicit hidden control reads the opposite of the desired state: fall through to one ordinary+ # label click (whose forwarding performs the single toggle) and never drive state through the label.+ label = FakeLabelElement(has_for_attr=True, label_for=None, for_control_state=False)+ set_state = AsyncMock(return_value=True)+ with (+ patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped),+ patch.object(handler, "_set_native_checkbox_state", set_state),+ ):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+ set_state.assert_not_awaited()+++async def test_apply_label_explicit_for_unresolvable_falls_open() -> None:+ # el.control resolves to nothing usable (dangling id / non-labelable / non-toggle) -> None ->+ # fall open to an ordinary click.+ label = FakeLabelElement(has_for_attr=True, label_for=None, for_control_state=None)+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ assert await handler._apply_desired_click_state(_click_action(), label, True, MagicMock()) is None+++async def test_apply_label_explicit_mapped_control_takes_precedence() -> None:+ # find_label_for stays first: when the explicit control is scraped and mapped, its state is read+ # through the mapped SkyvernElement (ARIA-capable) and the in-page el.control read is never+ # consulted. Mapped control checked + desired True -> suppress; for_control_state=False (which+ # would mismatch) plus a probe that raises if consulted both prove the mapped read wins.+ control = FakeToggleElement("input", {"type": "checkbox"}, checked=True)+ label = FakeLabelElement(+ label_for=control,+ has_for_attr=True,+ for_control_state=False,+ wrapped_error=RuntimeError("in-page read must not run when the control is mapped"),+ )+ with patch.object(handler, "_evaluate_element_scoped", _fake_evaluate_element_scoped):+ result = await handler._apply_desired_click_state(_click_action(), label, True, MagicMock())+ assert result is not None and len(result) == 1 and isinstance(result[0], ActionAbort)+++# ---------------------------------------------------------------------------+# Disabled-wrapper retarget — the pre-retarget guard falls open on an unreadable+# wrapper, the click is retargeted to its enabled single-chain descendant, and the+# same guard is reapplied to that child so an already-satisfied control is not+# re-toggled. A child that the guard can't suppress still gets one ordinary click.+# ---------------------------------------------------------------------------+++async def _run_handle_click_retarget(+ *, wrapper: MagicMock, child: MagicMock, desired_state: bool+) -> tuple[list, AsyncMock]:+ dom = MagicMock()+ dom.get_skyvern_element_by_id = AsyncMock(return_value=wrapper)+ dom.safe_get_skyvern_element_by_id = AsyncMock(return_value=child)+ page = MagicMock(url="https://example.test/page")+ page.evaluate = AsyncMock(return_value=False)+ chain = AsyncMock(return_value=[ActionSuccess()])+ incremental = MagicMock()+ incremental.start_listen_dom_increment = AsyncMock()+ incremental.stop_listen_dom_increment = AsyncMock()++ with (+ patch.object(handler, "DomUtil", return_value=dom),+ patch.object(handler, "get_or_create_wait_config", new=AsyncMock(return_value=None)),+ patch.object(handler.asyncio, "sleep", new=AsyncMock()),+ patch.object(handler, "chain_click", new=chain),+ patch.object(handler, "resolve_engine_selection_for_task", MagicMock(return_value=None)),+ patch.object(handler.SkyvernFrame, "create_instance", new=AsyncMock(return_value=MagicMock())),+ patch.object(handler, "IncrementalScrapePage", return_value=incremental),+ patch.object(handler, "handle_sequential_click_for_dropdown", new=AsyncMock(return_value=None)),+ ):+ action = ClickAction(element_id="wrapper", click_context=ClickContext(desired_state=desired_state))+ results = await handler.handle_click_action(action, page, MagicMock(), MagicMock(), MagicMock())+ return results, chain+++def _disabled_wrapper() -> MagicMock:+ wrapper = MagicMock()+ wrapper.get_id.return_value = "wrapper"+ wrapper.get_tag_name.return_value = "div"+ wrapper.get_attr = AsyncMock(return_value=None)+ wrapper.is_disabled = AsyncMock(return_value=True)+ wrapper.find_deepest_interactable_descendant_in_single_chain = MagicMock(return_value="child")+ return wrapper+++def _retarget_child(*, tag_name: str, attr_value: str | None, checked: bool | None) -> MagicMock:+ child = MagicMock()+ child.get_id.return_value = "child"+ child.get_tag_name.return_value = tag_name+ child.get_attr = AsyncMock(return_value=attr_value)+ child.is_checked = AsyncMock(return_value=checked)+ child.is_disabled = AsyncMock(return_value=False)+ child.scroll_into_view = AsyncMock()+ child.get_element_handler = AsyncMock(return_value=MagicMock())+ child.has_attr = AsyncMock(return_value=False)+ child.get_frame.return_value = MagicMock()+ return child+++async def test_handle_click_reapplies_guard_on_retargeted_child() -> None:+ wrapper = _disabled_wrapper()+ child = _retarget_child(tag_name="input", attr_value="checkbox", checked=True)+ results, chain = await _run_handle_click_retarget(wrapper=wrapper, child=child, desired_state=True)++ assert len(results) == 1 and isinstance(results[0], ActionAbort)+ chain.assert_not_awaited()+++async def test_handle_click_retargeted_child_mismatch_clicks_once() -> None:+ wrapper = _disabled_wrapper()+ child = _retarget_child(tag_name="div", attr_value="false", checked=None)+ _results, chain = await _run_handle_click_retarget(wrapper=wrapper, child=child, desired_state=True)++ chain.assert_awaited_once()diff --git a/tests/unit/test_script_skyvern_page.py b/tests/unit/test_script_skyvern_page.pyindex 9b77a4bb1..91adf9ef3 100644--- a/tests/unit/test_script_skyvern_page.py+++ b/tests/unit/test_script_skyvern_page.py@@ -8,6 +8,7 @@ and wait() (accepts both seconds= and timeout_ms= parameter styles)."""import asyncio+import contextlibimport inspectimport reimport time@@ -29,6 +30,9 @@ from skyvern.exceptions import (SkyvernHTTPException,)from skyvern.services import script_service+from skyvern.webeye.actions.action_types import ActionType+from skyvern.webeye.actions.handler import ActionHandler+from skyvern.webeye.actions.responses import ActionAbort, ActionFailure, ActionSuccessclass _KeyboardStub:@@ -2948,3 +2952,240 @@ async def test_get_or_create_browser_state_does_not_record_session_on_acquire_faskyvern_context.reset()assert ctx.browser_session_id is None # requested session not recorded because the attach failed+++class TestAiClickRunsRegisteredClickSetup:+ """SKY-13916: the cached-script AI click path must run the registered+ ActionHandler setup for the click action before dispatching the physical+ click, mirroring ActionHandler._handle_action. The setup carries the+ selected-state guard; bypassing it re-clicked an already-selected control.++ Any nonempty setup result is terminal, mirroring _handle_action (which+ returns early whenever setup yields results): a failure surfaces as+ SkyvernActionFailed and falls back to the raw selector when one exists; any+ other nonempty result -- an ActionAbort, or an ActionSuccess from a setup+ that already performed the click -- suppresses the physical click, records+ the episode, and returns the resolved selector. Only an empty setup result+ proceeds to the physical click.+ """++ _MODULE = "skyvern.core.script_generations.real_skyvern_page_ai"++ def _build_page_ai(self):+ page_ai = RealSkyvernPageAi.__new__(RealSkyvernPageAi)+ page_ai._maybe_run_v3_midrun = AsyncMock(return_value=(None, False))+ page_ai._refresh_scraped_page = AsyncMock()+ page_ai._record_element_fallback_episode = AsyncMock()+ page_ai.current_label = None+ scraped = MagicMock()+ scraped.build_element_tree.return_value = "tree"+ page_ai.scraped_page = scraped+ page = MagicMock()+ page.url = "https://example.com"+ locator = MagicMock()+ locator.click = AsyncMock()+ page.locator.return_value = locator+ page_ai.page = page+ return page_ai, locator++ @staticmethod+ def _context():+ return SimpleNamespace(+ organization_id="org_1",+ task_id="task_1",+ step_id="step_1",+ workflow_run_id="wr_1",+ workflow_permanent_id="wpid_1",+ script_revision_id="rev_1",+ code_version=2,+ tz_info=None,+ prompt="goal",+ script_run_parameters=None,+ log=[],+ )++ @contextlib.contextmanager+ def _patched(self, *, setup_result, handle_result):+ ctx = self._context()+ fake_action = MagicMock()+ fake_action.action_type = ActionType.CLICK+ fake_action.get_xpath.return_value = "//button[@id='x']"++ fake_setup = AsyncMock(return_value=setup_result) if setup_result is not None else None+ handle_spy = AsyncMock(return_value=handle_result)+ llm_handler = AsyncMock(return_value={"actions": [{"element_id": "e1"}]})+ registry = {ActionType.CLICK: fake_setup} if fake_setup is not None else {}++ m = self._MODULE+ with contextlib.ExitStack() as stack:+ enter = stack.enter_context+ enter(patch(f"{m}.skyvern_context.ensure_context", return_value=ctx))+ enter(patch(f"{m}.skyvern_context.current", return_value=ctx))+ enter(+ patch(+ f"{m}.app.DATABASE.tasks.get_step",+ new=AsyncMock(return_value=SimpleNamespace(step_id="step_1", order=0)),+ )+ )+ enter(+ patch(+ f"{m}.app.DATABASE.tasks.get_task",+ new=AsyncMock(return_value=SimpleNamespace(task_id="task_1")),+ )+ )+ enter(patch(f"{m}.prompt_engine.load_prompt", return_value="p"))+ enter(patch(f"{m}._resolve_assist_llm_handler", new=AsyncMock(return_value=llm_handler)))+ enter(patch(f"{m}.parse_actions", return_value=[fake_action]))+ enter(patch(f"{m}.handle_click_action", new=handle_spy))+ enter(patch.dict(ActionHandler._setup_action_types, registry, clear=True))+ yield SimpleNamespace(fake_action=fake_action, fake_setup=fake_setup, handle_spy=handle_spy)++ @pytest.mark.asyncio+ async def test_abort_from_setup_suppresses_physical_click(self):+ page_ai, _locator = self._build_page_ai()+ with self._patched(setup_result=[ActionAbort()], handle_result=[ActionSuccess()]) as spies:+ result = await page_ai.ai_click(selector="#btn", intention="toggle owner")++ spies.fake_setup.assert_awaited_once()+ spies.handle_spy.assert_not_awaited()+ # A suppressed (no-op) click is not a selector miss: it must not record a+ # corrective fallback episode, or the reviewer would rewrite a click that+ # deliberately did nothing.+ page_ai._record_element_fallback_episode.assert_not_awaited()+ assert result == "xpath=//button[@id='x']"++ @pytest.mark.asyncio+ async def test_success_from_setup_suppresses_physical_click(self):+ page_ai, _locator = self._build_page_ai()+ with self._patched(setup_result=[ActionSuccess()], handle_result=[ActionSuccess()]) as spies:+ result = await page_ai.ai_click(selector="#btn", intention="submit")++ spies.fake_setup.assert_awaited_once()+ spies.handle_spy.assert_not_awaited()+ page_ai._record_element_fallback_episode.assert_not_awaited()+ assert result == "xpath=//button[@id='x']"++ @pytest.mark.asyncio+ async def test_empty_setup_runs_physical_click_once(self):+ page_ai, _locator = self._build_page_ai()+ with self._patched(setup_result=[], handle_result=[ActionSuccess()]) as spies:+ await page_ai.ai_click(selector="#btn", intention="submit")++ spies.fake_setup.assert_awaited_once()+ spies.handle_spy.assert_awaited_once()+ # A real physical click on the AI-found element still records the fallback episode.+ page_ai._record_element_fallback_episode.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_no_registered_setup_runs_physical_click_once(self):+ page_ai, _locator = self._build_page_ai()+ with self._patched(setup_result=None, handle_result=[ActionSuccess()]) as spies:+ await page_ai.ai_click(selector="#btn", intention="submit")++ spies.handle_spy.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_setup_failure_falls_back_to_selector(self):+ page_ai, locator = self._build_page_ai()+ with self._patched(setup_result=[ActionFailure(Exception("boom"))], handle_result=[ActionSuccess()]) as spies:+ result = await page_ai.ai_click(selector="#btn", intention="submit")++ spies.handle_spy.assert_not_awaited()+ locator.click.assert_awaited_once()+ assert result == "#btn"++ @pytest.mark.asyncio+ async def test_setup_failure_without_selector_raises(self):+ page_ai, _locator = self._build_page_ai()+ with self._patched(setup_result=[ActionFailure(Exception("boom"))], handle_result=[ActionSuccess()]) as spies:+ with pytest.raises(SkyvernActionFailed):+ await page_ai.ai_click(selector=None, intention="submit")++ spies.handle_spy.assert_not_awaited()+++class TestAiActAppliesDesiredStateGuard:+ """SKY-13916: ai_act routes a CLICK straight to the shared handle_click_action,+ which now owns the level-triggered ``desired_state`` guard. So the cached+ ai_act path must not re-click a control that already holds the desired state --+ without any click-setup bridge of its own."""++ _MODULE = "skyvern.core.script_generations.real_skyvern_page_ai"++ def _build_page_ai(self):+ page_ai = RealSkyvernPageAi.__new__(RealSkyvernPageAi)+ page_ai._refresh_scraped_page = AsyncMock()+ scraped = MagicMock()+ scraped.build_element_tree.return_value = "tree"+ scraped.support_economy_elements_tree.return_value = False+ page_ai.scraped_page = scraped+ page = MagicMock()+ page.url = "https://example.test/page"+ page.evaluate = AsyncMock(return_value=False)+ page_ai.page = page+ return page_ai++ @staticmethod+ def _already_selected_element():+ element = MagicMock()+ element.get_id.return_value = "control"+ element.get_tag_name.return_value = "div"+ element.get_attr = AsyncMock(return_value="true")+ element.is_disabled = AsyncMock(return_value=False)+ element.scroll_into_view = AsyncMock()+ element.get_element_handler = AsyncMock(return_value=MagicMock())+ element.has_attr = AsyncMock(return_value=False)+ element.get_frame.return_value = MagicMock()+ return element++ @pytest.mark.asyncio+ async def test_ai_act_click_suppresses_already_satisfied_state(self):+ from skyvern.webeye.actions import handler+ from skyvern.webeye.actions.actions import ClickAction, ClickContext++ page_ai = self._build_page_ai()+ ctx = SimpleNamespace(+ organization_id="org_1",+ task_id="task_1",+ step_id="step_1",+ tz_info=None,+ prompt="goal",+ current_step_actions=None,+ )+ task = SimpleNamespace(task_id="task_1", workflow_run_id="wr_1", organization_id="org_1")+ step = SimpleNamespace(step_id="step_1", order=0)+ infer_handler = AsyncMock(+ return_value={"inferred_actions": [{"action_type": "CLICK", "confidence_float": 0.9}]}+ )+ click_handler = AsyncMock(return_value={"actions": [{"element_id": "control"}]})+ action = ClickAction(element_id="control", click_context=ClickContext(desired_state=True))++ dom = MagicMock()+ dom.get_skyvern_element_by_id = AsyncMock(return_value=self._already_selected_element())+ chain = AsyncMock(return_value=[ActionSuccess()])+ incremental = MagicMock()+ incremental.start_listen_dom_increment = AsyncMock()+ incremental.stop_listen_dom_increment = AsyncMock()++ m = self._MODULE+ with (+ patch(f"{m}.skyvern_context.ensure_context", return_value=ctx),+ patch(f"{m}.app.DATABASE.tasks.get_task", new=AsyncMock(return_value=task)),+ patch(f"{m}.app.DATABASE.tasks.get_step", new=AsyncMock(return_value=step)),+ patch(f"{m}.prompt_engine.load_prompt", return_value="p"),+ patch(f"{m}._resolve_assist_llm_handler", new=AsyncMock(side_effect=[infer_handler, click_handler])),+ patch(f"{m}.parse_actions", return_value=[action]),+ patch.object(handler, "DomUtil", return_value=dom),+ patch.object(handler, "get_or_create_wait_config", new=AsyncMock(return_value=None)),+ patch.object(handler.asyncio, "sleep", new=AsyncMock()),+ patch.object(handler, "chain_click", new=chain),+ patch.object(handler, "resolve_engine_selection_for_task", MagicMock(return_value=None)),+ patch.object(handler.SkyvernFrame, "create_instance", new=AsyncMock(return_value=MagicMock())),+ patch.object(handler, "IncrementalScrapePage", return_value=incremental),+ patch.object(handler, "handle_sequential_click_for_dropdown", new=AsyncMock(return_value=None)),+ ):+ await page_ai.ai_act("toggle the owner on")++ # The control already reads selected, so handle_click_action must suppress the+ # physical click; ai_act neither dispatches a click nor raises.+ chain.assert_not_awaited()