Skip to content

Automate browser based workflows with AI

Add event trigger dispatcher (#SKY-18483) (#8938)

Aaron Perezefa027db45eb
diff --git a/alembic/versions/2026_10_16_2221-0c453e5042cb_add_workflow_run_setup_completion_marker.py b/alembic/versions/2026_10_16_2221-0c453e5042cb_add_workflow_run_setup_completion_marker.py
new file mode 100644
index 000000000..bc46f7b01
--- /dev/null
+++ b/alembic/versions/2026_10_16_2221-0c453e5042cb_add_workflow_run_setup_completion_marker.py
@@ -0,0 +1,32 @@
+"""add workflow run setup completion marker
+
+Revision ID: 0c453e5042cb
+Revises: 50c59227239d
+Create Date: 2026-10-16T22:21:24.323746+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "0c453e5042cb"
+down_revision: Union[str, None] = "50c59227239d"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.add_column(
+ "workflow_runs",
+ sa.Column("setup_completed", sa.Boolean(), server_default=sa.true(), nullable=False),
+ )
+
+
+def downgrade() -> None:
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.drop_column("workflow_runs", "setup_completed")
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index 59e32bfec..9270e9f51 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -989,6 +989,7 @@ class WorkflowRunModel(Base):
browser_address = Column(String, nullable=True, index=True)
script_run = Column(JSON, nullable=True)
job_id = Column(String, nullable=True, index=True)
+ setup_completed = Column(Boolean, nullable=False, default=True, server_default=sqlalchemy.true())
task_queue = Column(String, nullable=True)
target_cluster = Column(String, nullable=True)
depends_on_workflow_run_id = Column(String, nullable=True, index=True)
diff --git a/skyvern/forge/sdk/db/repositories/workflow_runs.py b/skyvern/forge/sdk/db/repositories/workflow_runs.py
index f3803f30f..989946200 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_runs.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_runs.py
@@ -924,6 +924,7 @@ class WorkflowRunsRepository(BaseRepository):
workflow_definition_sha256=workflow_definition_sha256,
proxy_location=serialize_proxy_location(proxy_location),
status="created",
+ setup_completed=trigger_type != WorkflowRunTriggerType.event,
webhook_callback_url=webhook_callback_url,
totp_verification_url=totp_verification_url,
totp_identifier=totp_identifier,
@@ -1077,6 +1078,7 @@ class WorkflowRunsRepository(BaseRepository):
started_at: datetime | None | object = _UNSET,
queued_at: datetime | None | object = _UNSET,
finished_at: datetime | None | object = _UNSET,
+ setup_completed: bool | None = None,
) -> WorkflowRun:
async with self.Session() as session:
query = select(WorkflowRunModel).filter_by(workflow_run_id=workflow_run_id)
@@ -1085,6 +1087,8 @@ class WorkflowRunsRepository(BaseRepository):
query = query.with_for_update().execution_options(populate_existing=True)
workflow_run = (await session.scalars(query)).first()
if workflow_run:
+ if setup_completed is not None:
+ workflow_run.setup_completed = setup_completed
if status:
workflow_run.status = status
if status and status == WorkflowRunStatus.queued and workflow_run.queued_at is None:
diff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.py
index bb9cc8ecc..42b7706d0 100644
--- a/skyvern/forge/sdk/db/utils.py
+++ b/skyvern/forge/sdk/db/utils.py
@@ -817,6 +817,7 @@ def convert_to_workflow_run(
cdp_connect_headers=workflow_run_model.cdp_connect_headers,
browser_address=workflow_run_model.browser_address,
job_id=workflow_run_model.job_id,
+ setup_completed=workflow_run_model.setup_completed,
depends_on_workflow_run_id=workflow_run_model.depends_on_workflow_run_id,
sequential_key=workflow_run_model.sequential_key,
sequential_credential_id=workflow_run_model.sequential_credential_id,
diff --git a/skyvern/forge/sdk/workflow/models/workflow.py b/skyvern/forge/sdk/workflow/models/workflow.py
index 80d3bc60c..f4b1924f8 100644
--- a/skyvern/forge/sdk/workflow/models/workflow.py
+++ b/skyvern/forge/sdk/workflow/models/workflow.py
@@ -348,6 +348,7 @@ class WorkflowRun(BaseModel):
reuse_bound_key: str | None = Field(default=None, exclude=True)
# Internal routing: the worker queue the run was dispatched to. Never an API field.
task_queue: str | None = Field(default=None, exclude=True)
+ setup_completed: bool = Field(default=True, exclude=True)
# Digest of the definition the run was created against; the saved row can be overwritten in place later.
workflow_definition_sha256: str | None = Field(default=None, exclude=True)
debug_session_id: str | None = None
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 7f60355bb..6c764c788 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -3771,6 +3771,20 @@ class WorkflowService:
)
raise
+ if resolved_trigger_type == WorkflowRunTriggerType.event:
+ try:
+ workflow_run = await app.DATABASE.workflow_runs.update_workflow_run(
+ workflow_run.workflow_run_id,
+ setup_completed=True,
+ )
+ except SQLAlchemyError as error:
+ await outer_session.rollback()
+ LOG.warning(
+ "Failed to persist event workflow run setup completion",
+ workflow_run_id=workflow_run.workflow_run_id,
+ error_type=type(error).__name__,
+ )
+
return workflow_run
@staticmethod
diff --git a/skyvern/services/workflow_service.py b/skyvern/services/workflow_service.py
index d57e34277..6b5045a5b 100644
--- a/skyvern/services/workflow_service.py
+++ b/skyvern/services/workflow_service.py
@@ -9,7 +9,7 @@ from skyvern.config import settings
from skyvern.exceptions import SkyvernHTTPException
from skyvern.forge import app
from skyvern.forge.request_logging import get_request_principal_state
-from skyvern.forge.sdk.db.enums import BrowserSeedSource, WorkflowRunTriggerType
+from skyvern.forge.sdk.db.enums import BrowserSeedSource, WorkflowRunStatus, WorkflowRunTriggerType
from skyvern.forge.sdk.executor.factory import AsyncExecutorFactory
from skyvern.forge.sdk.schemas.organizations import Organization
from skyvern.forge.sdk.workflow.exceptions import InvalidTemplateWorkflowPermanentId
@@ -170,9 +170,8 @@ async def _prepare_workflow(
post_dispatch_effects: list[Callable[[], Awaitable[None]]] | None = None,
integration_platform: str | None = None,
) -> tuple[WorkflowRun, Workflow]:
- if template:
- if workflow_id not in await app.STORAGE.retrieve_global_workflows():
- raise InvalidTemplateWorkflowPermanentId(workflow_permanent_id=workflow_id)
+ if template and workflow_id not in await app.STORAGE.retrieve_global_workflows():
+ raise InvalidTemplateWorkflowPermanentId(workflow_permanent_id=workflow_id)
# One ambient session keeps the version lookup in the same transaction as the run insert, as it
# was when setup_workflow_run resolved the version itself, and one connection across setup's commits.
@@ -251,6 +250,7 @@ async def run_workflow(
tag_write_context: TagWriteContext | None = None,
created_by: str | None = None,
refuse_unusable_parameters_before_create: bool = False,
+ workflow_run_id: str | None = None,
) -> WorkflowRun:
workflow_run, _ = await run_workflow_returning_owned_workflow(
workflow_id=workflow_id,
@@ -274,6 +274,7 @@ async def run_workflow(
tag_write_context=tag_write_context,
created_by=created_by,
refuse_unusable_parameters_before_create=refuse_unusable_parameters_before_create,
+ workflow_run_id=workflow_run_id,
)
return workflow_run
@@ -300,6 +301,7 @@ async def run_workflow_returning_owned_workflow(
tag_write_context: TagWriteContext | None = None,
created_by: str | None = None,
refuse_unusable_parameters_before_create: bool = False,
+ workflow_run_id: str | None = None,
) -> tuple[WorkflowRun, Workflow | None]:
# Fail fast before the run is prepared/persisted: reject a run-level browser_type this runtime
# cannot honor with a 4xx, rather than accepting it and failing at launch. No-op when unset or on
@@ -313,44 +315,142 @@ async def run_workflow_returning_owned_workflow(
post_dispatch_effects: list[Callable[[], Awaitable[None]]] = []
request_identity = get_request_principal_state()
try:
- workflow_run, workflow = await _prepare_workflow(
- workflow_id=workflow_id,
- organization=organization,
- workflow_request=workflow_request,
- template=template,
- version=version,
- max_steps=max_steps,
- request_id=request_id,
- parent_workflow_run_id=parent_workflow_run_id,
- trigger_type=trigger_type,
- integration_platform=request_identity.integration_platform if request_identity else None,
- workflow_schedule_id=workflow_schedule_id,
- retried_from_workflow_run_id=retried_from_workflow_run_id,
- fallback_attempt=fallback_attempt,
- ignore_inherited_workflow_system_prompt=ignore_inherited_workflow_system_prompt,
- tag_write_context=tag_write_context,
- created_by=created_by,
- refuse_unusable_parameters_before_create=refuse_unusable_parameters_before_create,
- post_dispatch_effects=post_dispatch_effects,
+ workflow: Workflow | None = None
+ workflow_run = (
+ await app.DATABASE.workflow_runs.get_workflow_run(
+ workflow_run_id, organization_id=organization.organization_id
+ )
+ if workflow_run_id
+ else None
+ )
+ existing_workflow_run = workflow_run is not None
+ task_run = (
+ await app.DATABASE.tasks.get_run(
+ workflow_run.workflow_run_id,
+ organization_id=organization.organization_id,
+ )
+ if workflow_run is not None and existing_workflow_run
+ else None
)
+ if workflow_run is None:
+ workflow_run, workflow = await _prepare_workflow(
+ workflow_id=workflow_id,
+ organization=organization,
+ workflow_request=workflow_request,
+ template=template,
+ version=version,
+ max_steps=max_steps,
+ request_id=request_id,
+ parent_workflow_run_id=parent_workflow_run_id,
+ trigger_type=trigger_type,
+ integration_platform=request_identity.integration_platform if request_identity else None,
+ workflow_schedule_id=workflow_schedule_id,
+ workflow_run_id=workflow_run_id,
+ retried_from_workflow_run_id=retried_from_workflow_run_id,
+ fallback_attempt=fallback_attempt,
+ ignore_inherited_workflow_system_prompt=ignore_inherited_workflow_system_prompt,
+ tag_write_context=tag_write_context,
+ created_by=created_by,
+ refuse_unusable_parameters_before_create=refuse_unusable_parameters_before_create,
+ post_dispatch_effects=post_dispatch_effects,
+ )
+ if workflow_run_id is not None:
+ if (
+ workflow_run.workflow_permanent_id != workflow_id
+ or workflow_run.trigger_type != trigger_type
+ or workflow_run.created_by != created_by
+ ):
+ raise SkyvernHTTPException("Workflow run ID is already in use.", HTTPStatus.CONFLICT)
+ if workflow_run.trigger_type == WorkflowRunTriggerType.event and not workflow_run.setup_completed:
+ if task_run is None and not existing_workflow_run:
+ task_run = await app.DATABASE.tasks.get_run(
+ workflow_run.workflow_run_id,
+ organization_id=organization.organization_id,
+ )
+ if task_run is None:
+ raise SkyvernHTTPException(
+ "Workflow run setup is incomplete; refusing to resume it.",
+ HTTPStatus.SERVICE_UNAVAILABLE,
+ )
+ # TaskRun is written only after setup_workflow_run returns, so it proves setup completed.
+ workflow_run = await app.DATABASE.workflow_runs.update_workflow_run(
+ workflow_run.workflow_run_id,
+ setup_completed=True,
+ )
+ if WorkflowRunStatus(workflow_run.status).is_final():
+ return workflow_run, None
+ if workflow is None:
+ if existing_workflow_run and workflow_run.trigger_type == WorkflowRunTriggerType.event:
+ workflow = await app.DATABASE.workflows.get_workflow_for_workflow_run(
+ workflow_run.workflow_run_id,
+ organization_id=organization.organization_id,
+ filter_deleted=False,
+ )
+ if workflow is None:
+ raise SkyvernHTTPException("Workflow version not found.", HTTPStatus.NOT_FOUND)
+ else:
+ workflow = await app.WORKFLOW_SERVICE.resolve_workflow_for_run(
+ workflow_permanent_id=workflow_id,
+ organization=organization,
+ is_template_workflow=template,
+ resolved_workflow_id=workflow_run.workflow_id,
+ )
+ if existing_workflow_run and workflow_run.trigger_type == WorkflowRunTriggerType.event:
+ workflow_parameters = await app.WORKFLOW_SERVICE.get_workflow_parameters(workflow.workflow_id)
+ expected_parameters = app.WORKFLOW_SERVICE._resolve_request_parameter_values(
+ workflow_parameters, workflow_request.data
+ ).values
+ if expected_parameters:
+ existing_parameters = await app.DATABASE.workflow_runs.get_workflow_run_parameters(
+ workflow_run.workflow_run_id
+ )
+ existing_parameter_ids = {parameter.workflow_parameter_id for parameter, _ in existing_parameters}
+ if any(
+ parameter.workflow_parameter_id not in existing_parameter_ids
+ for parameter, _ in expected_parameters
+ ):
+ raise SkyvernHTTPException(
+ "Workflow run setup is incomplete; refusing to resume it.",
+ HTTPStatus.SERVICE_UNAVAILABLE,
+ )
+ if existing_workflow_run and task_run is None:
+ await app.DATABASE.tasks.create_task_run(
+ task_run_type=RunType.workflow_run,
+ organization_id=organization.organization_id,
+ run_id=workflow_run.workflow_run_id,
+ title=workflow.title,
+ status=RunStatus.queued,
+ workflow_permanent_id=workflow_id,
+ parent_workflow_run_id=workflow_run.parent_workflow_run_id,
+ debug_session_id=workflow_run.debug_session_id,
+ )
+ elif workflow is None:
+ raise RuntimeError("Workflow preparation returned no workflow")
# A template run resolves its workflow without an org filter, so it can be another org's row, which must
# reach neither the executor nor a caller that echoes it back.
owned_workflow = workflow if workflow.organization_id == organization.organization_id else None
- await AsyncExecutorFactory.get_executor().execute_workflow(
- request=request,
- background_tasks=background_tasks,
- organization=organization,
- workflow_id=workflow_run.workflow_id,
- workflow_run_id=workflow_run.workflow_run_id,
- workflow_permanent_id=workflow_run.workflow_permanent_id,
- max_steps_override=max_steps,
- browser_session_id=workflow_run.browser_session_id,
- api_key=api_key,
- block_labels=block_labels,
- block_outputs=block_outputs,
- resolved_workflow=owned_workflow,
+ # Temporal persists its deterministic workflow ID before submit; unlike a Batch job ID, it must be retried.
+ temporal_workflow_id = f"{organization.organization_id}-{workflow_run.workflow_run_id}"
+ skip_executor = existing_workflow_run and (
+ WorkflowRunStatus(workflow_run.status) in {WorkflowRunStatus.running, WorkflowRunStatus.paused}
+ or (workflow_run.job_id is not None and workflow_run.job_id != temporal_workflow_id)
)
+ if not skip_executor:
+ await AsyncExecutorFactory.get_executor().execute_workflow(
+ request=request,
+ background_tasks=background_tasks,
+ organization=organization,
+ workflow_id=workflow_run.workflow_id,
+ workflow_run_id=workflow_run.workflow_run_id,
+ workflow_permanent_id=workflow_run.workflow_permanent_id,
+ max_steps_override=max_steps,
+ browser_session_id=workflow_run.browser_session_id,
+ api_key=api_key,
+ block_labels=block_labels,
+ block_outputs=block_outputs,
+ resolved_workflow=owned_workflow,
+ )
finally:
for effect in post_dispatch_effects:
with contained_effect("post-dispatch bookkeeping"):