Skip to content

Automate browser based workflows with AI

Report no success rate instead of 0% when an analytics window has no final runs (SKY-16218) (#8520)

Aaron Perez9355f47447e1
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index 9ee22cbd5..cec9d4cd1 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -16987,16 +16987,30 @@ class WorkflowTriggerBlock(Block):
# — the flag is written once, at spawn time, for both paths.
async_parent_context = skyvern_context.current()
async_inherited_trigger_type = async_parent_context.trigger_type if async_parent_context else None
- triggered_workflow_run = await run_workflow(
- workflow_id=resolved_workflow_permanent_id,
- organization=organization,
- workflow_request=workflow_request,
- request=None,
- background_tasks=None,
- parent_workflow_run_id=workflow_run_id,
- ignore_inherited_workflow_system_prompt=self.ignore_workflow_system_prompt,
- trigger_type=async_inherited_trigger_type,
- )
+ # run_workflow replaces the current context with the child's identity (setup_workflow_run).
+ # Scope the whole dispatch in a placeholder child context — mirroring the synchronous branch
+ # above — so the token reset restores the parent's exact context on every exit; otherwise the
+ # child identity leaks past the await and mislabels the parent's remaining execution.
+ with skyvern_context.scoped(
+ skyvern_context.SkyvernContext(
+ run_id=async_parent_context.run_id if async_parent_context else None,
+ root_workflow_run_id=async_parent_context.root_workflow_run_id
+ if async_parent_context
+ else None,
+ copilot_session_id=async_parent_context.copilot_session_id if async_parent_context else None,
+ trigger_type=async_inherited_trigger_type,
+ )
+ ):
+ triggered_workflow_run = await run_workflow(
+ workflow_id=resolved_workflow_permanent_id,
+ organization=organization,
+ workflow_request=workflow_request,
+ request=None,
+ background_tasks=None,
+ parent_workflow_run_id=workflow_run_id,
+ ignore_inherited_workflow_system_prompt=self.ignore_workflow_system_prompt,
+ trigger_type=async_inherited_trigger_type,
+ )
except Exception as e:
error_msg = get_user_facing_exception_message(e)
return await _fail(f"Failed to dispatch triggered workflow: {error_msg}")
diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.py
index 656395530..7619fa383 100644
--- a/skyvern/webeye/utils/dom.py
+++ b/skyvern/webeye/utils/dom.py
@@ -1359,6 +1359,13 @@ class SkyvernElement:
async def move_mouse_to(
self, page: Page, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS
) -> tuple[float, float]:
+ # Resolve the element into the live viewport before reading its box, then
+ # re-read the box. Reading it first can yield a document-level off-screen
+ # coordinate (e.g. y far below the fold) that would become the humanized
+ # cursor target and drive a cross-document cursor traversal (SKY-15195).
+ # scroll_into_view is the element's established scroll/actionability contract.
+ await self.scroll_into_view(timeout=timeout)
+
bounding_box = await self.get_locator().bounding_box(timeout=timeout)
if not bounding_box:
raise NoElementBoudingBox(element_id=self.get_id())
@@ -1369,8 +1376,6 @@ class SkyvernElement:
dest_x = uniform(x + epsilon, x + width - epsilon) if width > 2 * epsilon else (x + width) / 2
dest_y = uniform(y + epsilon, y + height - epsilon) if height > 2 * epsilon else (y + height) / 2
- # TODO: a better way to check if the element is out of current viewport
- # eg: x > window.innerWidth or y > window.innerHeight; part of the element is out of the viewport
if dest_x < 0 or dest_y < 0:
raise ElementOutOfCurrentViewport(element_id=self.get_id())
diff --git a/tests/unit/webeye/test_move_mouse_to_scroll.py b/tests/unit/webeye/test_move_mouse_to_scroll.py
new file mode 100644
index 000000000..289209e75
--- /dev/null
+++ b/tests/unit/webeye/test_move_mouse_to_scroll.py
@@ -0,0 +1,73 @@
+"""SkyvernElement.move_mouse_to must scroll the element into the live viewport
+*before* reading its bounding box (SKY-15195).
+
+The incident (the reported staging run) generated a cursor target from a stale
+document-level bounding box (y ~ 5044) because the setup pre-move read the box
+before any scroll. move_mouse_to must resolve the element into view first so the
+re-read box is a viewport coordinate, never an off-screen one.
+"""
+
+from __future__ import annotations
+
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from skyvern.webeye.utils import dom as dom_module
+from skyvern.webeye.utils.dom import SkyvernElement
+
+
+def _make_element(order: list[str], bbox: dict) -> SkyvernElement:
+ element = SkyvernElement.__new__(SkyvernElement)
+
+ locator = MagicMock()
+
+ async def _bbox(*_args, **_kwargs) -> dict:
+ order.append("bbox")
+ return bbox
+
+ async def _scroll_if_needed(*_args, **_kwargs) -> None:
+ order.append("scroll")
+
+ locator.bounding_box = AsyncMock(side_effect=_bbox)
+ locator.scroll_into_view_if_needed = AsyncMock(side_effect=_scroll_if_needed)
+
+ async def _scroll_into_view(*_args, **_kwargs) -> None:
+ order.append("scroll")
+
+ element.get_locator = MagicMock(return_value=locator) # type: ignore[method-assign]
+ element.get_id = MagicMock(return_value="el-under-test") # type: ignore[method-assign]
+ element.scroll_into_view = AsyncMock(side_effect=_scroll_into_view) # type: ignore[method-assign]
+ return element
+
+
+async def test_move_mouse_to_scrolls_before_reading_bounding_box() -> None:
+ order: list[str] = []
+ element = _make_element(order, {"x": 300, "y": 400, "width": 40, "height": 20})
+ page = MagicMock()
+
+ with patch.object(dom_module.EventStrategyFactory, "move_cursor", new=AsyncMock()):
+ await element.move_mouse_to(page)
+
+ assert "scroll" in order, "move_mouse_to must scroll the element into view"
+ assert "bbox" in order, "move_mouse_to must read the bounding box"
+ assert order.index("scroll") < order.index("bbox"), f"scroll must precede bounding-box read; got {order}"
+
+
+async def test_move_mouse_to_dispatches_rescrolled_in_viewport_coordinate() -> None:
+ # After scroll the re-read box is in-viewport; the cursor target handed to
+ # the active strategy must be that in-viewport coordinate.
+ order: list[str] = []
+ element = _make_element(order, {"x": 300, "y": 400, "width": 40, "height": 20})
+ page = MagicMock()
+
+ with patch.object(dom_module.EventStrategyFactory, "move_cursor", new=AsyncMock()) as move_cursor:
+ dest_x, dest_y = await element.move_mouse_to(page)
+
+ move_cursor.assert_awaited_once()
+ called_x, called_y = move_cursor.await_args.args[1], move_cursor.await_args.args[2]
+ assert 300 <= called_x <= 340
+ assert 400 <= called_y <= 420
+ assert (called_x, called_y) == (dest_x, dest_y)
diff --git a/tests/unit/workflow/test_workflow_trigger_block.py b/tests/unit/workflow/test_workflow_trigger_block.py
index 964758cb1..5f6402909 100644
--- a/tests/unit/workflow/test_workflow_trigger_block.py
+++ b/tests/unit/workflow/test_workflow_trigger_block.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import asyncio
import json
from datetime import datetime, timezone
from typing import Any
@@ -675,6 +676,236 @@ async def test_sync_trigger_closes_fresh_session_when_setup_raises() -> None:
)
+def _make_parent_context() -> SkyvernContext:
+ """A parent workflow-run context seeded with the identity and flag fields a real run carries."""
+ return SkyvernContext(
+ organization_id="org_parent",
+ organization_name="Org Parent",
+ task_id="tsk_parent",
+ workflow_id="wf_parent",
+ workflow_permanent_id="wfp_parent",
+ workflow_run_id="wr_parent",
+ root_workflow_run_id="wr_parent",
+ run_id="wr_parent",
+ browser_session_id="pbs_parent",
+ copilot_session_id="chat_parent",
+ feature_flag_entries={"PARENT_FLAG": True},
+ use_flex_llm_routing=True,
+ )
+
+
+def _make_child_identity_context() -> SkyvernContext:
+ """The child identity ``run_workflow`` installs via ``skyvern_context.replace`` (service.py)."""
+ return SkyvernContext(
+ organization_id="org_parent",
+ organization_name="Org Parent",
+ workflow_id="wf_child",
+ workflow_permanent_id="wfp_child",
+ workflow_run_id="wr_child",
+ root_workflow_run_id="wr_parent",
+ run_id="wr_parent",
+ )
+
+
+def _assert_parent_context_intact(parent: SkyvernContext) -> None:
+ """The exact parent object is ambient again and none of its fields were mutated."""
+ assert skyvern_context.current() is parent
+ assert parent.workflow_run_id == "wr_parent"
+ assert parent.workflow_id == "wf_parent"
+ assert parent.workflow_permanent_id == "wfp_parent"
+ assert parent.root_workflow_run_id == "wr_parent"
+ assert parent.run_id == "wr_parent"
+ assert parent.task_id == "tsk_parent"
+ assert parent.browser_session_id == "pbs_parent"
+ assert parent.organization_id == "org_parent"
+ assert parent.copilot_session_id == "chat_parent"
+ assert parent.feature_flag_entries == {"PARENT_FLAG": True}
+ assert parent.use_flex_llm_routing is True
+
+
+async def _run_async_trigger(run_workflow_stub: Any) -> tuple[dict[str, Any], dict[str, Any]]:
+ """Execute a fire-and-forget WorkflowTriggerBlock with ``run_workflow`` stubbed.
+
+ Returns ``(build_result_kwargs, record_output_context)`` — the latter captures the ambient
+ context at the moment the block records its own output parameter, so a test can assert the
+ parent's output is attributed to the parent and not the dispatched child.
+ """
+ block = _make_block(wait_for_completion=False)
+
+ organization = MagicMock()
+ organization.organization_id = "org_parent"
+ organization.organization_name = "Org Parent"
+
+ captured: dict[str, Any] = {}
+ record_ctx: dict[str, Any] = {}
+
+ async def _build_result(**kwargs: Any) -> Any:
+ captured["success"] = kwargs.get("success")
+ captured["failure_reason"] = kwargs.get("failure_reason")
+ return MagicMock()
+
+ async def _capture_record(*_: Any, **__: Any) -> None:
+ record_ctx["ctx"] = skyvern_context.current()
+
+ with (
+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,
+ patch("skyvern.services.workflow_service.run_workflow", side_effect=run_workflow_stub),
+ patch.object(WorkflowTriggerBlock, "get_workflow_run_context", lambda self, workflow_run_id: MagicMock()),
+ patch.object(WorkflowTriggerBlock, "format_potential_template_parameters", lambda self, ctx: None),
+ patch.object(WorkflowTriggerBlock, "_check_trigger_depth", AsyncMock(return_value=0)),
+ patch.object(WorkflowTriggerBlock, "record_output_parameter_value", AsyncMock(side_effect=_capture_record)),
+ patch.object(WorkflowTriggerBlock, "build_block_result", AsyncMock(side_effect=_build_result)),
+ ):
+ mock_app.DATABASE.organizations.get_organization = AsyncMock(return_value=organization)
+ await block.execute(
+ workflow_run_id="wr_parent",
+ workflow_run_block_id="wrb_parent",
+ organization_id="org_parent",
+ browser_session_id=None,
+ )
+ return captured, record_ctx
+
+
+async def test_async_trigger_restores_parent_context_on_success() -> None:
+ # The fire-and-forget branch calls run_workflow, which replaces the ambient context with the
+ # child's identity (service.py setup_workflow_run). Without a scope the child identity survives
+ # the await and mislabels the parent's remaining execution; the dispatch must be scoped like the
+ # synchronous branch so the exact parent object is ambient again afterward.
+ parent = _make_parent_context()
+ child_identity = _make_child_identity_context()
+ observed: dict[str, Any] = {}
+
+ async def _run_workflow(**_: Any) -> Any:
+ observed["entry"] = skyvern_context.current()
+ skyvern_context.replace(child_identity)
+ observed["after_replace"] = skyvern_context.current()
+ result = MagicMock()
+ result.workflow_run_id = "wr_child"
+ return result
+
+ skyvern_context.set(parent)
+ try:
+ captured, record_ctx = await _run_async_trigger(_run_workflow)
+
+ # Dispatch runs under its own child scope — never the parent object.
+ assert observed["entry"] is not None
+ assert observed["entry"] is not parent
+ assert observed["after_replace"] is child_identity
+ # Dispatch succeeded and the parent's output was recorded under the restored parent.
+ assert captured["success"] is True
+ assert record_ctx["ctx"] is parent
+ _assert_parent_context_intact(parent)
+ finally:
+ skyvern_context.reset()
+
+
+async def test_async_trigger_restores_parent_context_on_dispatch_exception() -> None:
+ # A dispatch failure after run_workflow has already mutated the context must still restore the
+ # parent before the failure is recorded, so the parent's failure output is attributed correctly.
+ parent = _make_parent_context()
+ child_identity = _make_child_identity_context()
+
+ async def _run_workflow(**_: Any) -> Any:
+ skyvern_context.replace(child_identity)
+ raise RuntimeError("dispatch boom")
+
+ skyvern_context.set(parent)
+ try:
+ captured, record_ctx = await _run_async_trigger(_run_workflow)
+
+ assert captured["success"] is False
+ assert captured["failure_reason"] is not None
+ assert "Failed to dispatch triggered workflow" in captured["failure_reason"]
+ assert record_ctx["ctx"] is parent
+ _assert_parent_context_intact(parent)
+ finally:
+ skyvern_context.reset()
+
+
+async def test_async_trigger_restores_parent_context_on_cancellation() -> None:
+ # Cancellation raised mid-dispatch must propagate unchanged (never swallowed by the except
+ # Exception clause or a finally) while the parent context is still restored.
+ parent = _make_parent_context()
+ child_identity = _make_child_identity_context()
+
+ async def _run_workflow(**_: Any) -> Any:
+ skyvern_context.replace(child_identity)
+ raise asyncio.CancelledError()
+
+ skyvern_context.set(parent)
+ try:
+ with pytest.raises(asyncio.CancelledError):
+ await _run_async_trigger(_run_workflow)
+ _assert_parent_context_intact(parent)
+ finally:
+ skyvern_context.reset()
+
+
+async def test_nested_async_triggers_isolate_each_level() -> None:
+ # Root dispatches a child which, in-process, dispatches a grandchild. Each dispatch must see its
+ # own scope, and LIFO restoration must return to the child level after the grandchild unwinds and
+ # to the root object after the child unwinds.
+ root = _make_parent_context()
+ child_identity = _make_child_identity_context()
+ grandchild_identity = SkyvernContext(
+ organization_id="org_parent",
+ organization_name="Org Parent",
+ workflow_id="wf_grandchild",
+ workflow_permanent_id="wfp_grandchild",
+ workflow_run_id="wr_grandchild",
+ root_workflow_run_id="wr_parent",
+ run_id="wr_parent",
+ )
+
+ inner_block = _make_block(wait_for_completion=False)
+ observed: dict[str, Any] = {}
+ calls = {"n": 0}
+
+ async def _nested_run_workflow(**_: Any) -> Any:
+ calls["n"] += 1
+ if calls["n"] == 1:
+ observed["root_entry"] = skyvern_context.current()
+ skyvern_context.replace(child_identity)
+ observed["root_after_replace"] = skyvern_context.current()
+ await inner_block.execute(
+ workflow_run_id="wr_child",
+ workflow_run_block_id="wrb_child",
+ organization_id="org_parent",
+ browser_session_id=None,
+ )
+ observed["root_after_inner"] = skyvern_context.current()
+ result = MagicMock()
+ result.workflow_run_id = "wr_child"
+ return result
+ observed["child_entry"] = skyvern_context.current()
+ skyvern_context.replace(grandchild_identity)
+ result = MagicMock()
+ result.workflow_run_id = "wr_grandchild"
+ return result
+
+ skyvern_context.set(root)
+ try:
+ captured, _ = await _run_async_trigger(_nested_run_workflow)
+
+ assert calls["n"] == 2
+ assert captured["success"] is True
+ # Every dispatch level runs under a scope distinct from the others.
+ assert observed["root_entry"] is not root
+ assert observed["root_after_replace"] is child_identity
+ assert observed["child_entry"] is not root
+ assert observed["child_entry"] is not child_identity
+ # LIFO restoration: back to the child level after the grandchild unwinds.
+ assert observed["root_after_inner"] is child_identity
+ # And back to the exact root object after the child dispatch unwinds.
+ _assert_parent_context_intact(root)
+ finally:
+ skyvern_context.reset()
+
+
class TestBlockMetadata:
"""Verify basic block properties."""