Skip to content

Automate browser based workflows with AI

fix: support workflow run ID search fallback (#6788)

Suchintan3734d076c94e
diff --git a/skyvern/forge/sdk/db/repositories/workflow_runs.py b/skyvern/forge/sdk/db/repositories/workflow_runs.py
index 865ad2403..22993aa96 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_runs.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_runs.py
@@ -5,7 +5,20 @@ from typing import TYPE_CHECKING, Any, Callable
from typing import cast as typing_cast
import structlog
-from sqlalchemy import Text, and_, cast, exists, func, literal, literal_column, or_, select, update
+from sqlalchemy import (
+ ColumnElement,
+ Label,
+ Text,
+ and_,
+ cast,
+ exists,
+ func,
+ literal,
+ literal_column,
+ or_,
+ select,
+ update,
+)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.exc import SQLAlchemyError
@@ -49,7 +62,7 @@ from skyvern.forge.sdk.workflow.models.workflow import (
WorkflowRunParameter,
WorkflowRunStatus,
)
-from skyvern.schemas.runs import ProxyLocationInput, RunType
+from skyvern.schemas.runs import MAX_SEARCH_FETCH_LIMIT, ProxyLocationInput, RunType
LOG = structlog.get_logger()
@@ -96,6 +109,29 @@ class WorkflowRunsRepository(BaseRepository):
self._workflow_parameter_reader = workflow_parameter_reader
self._dialect_name = dialect_name
+ @staticmethod
+ def _workflow_deleted_expr(
+ workflow_permanent_id_col: ColumnElement[str | None],
+ organization_id_col: ColumnElement[str],
+ ) -> Label[bool]:
+ active_workflow_exists = (
+ select(1)
+ .select_from(WorkflowModel)
+ .where(
+ and_(
+ WorkflowModel.workflow_permanent_id == workflow_permanent_id_col,
+ WorkflowModel.organization_id == organization_id_col,
+ WorkflowModel.deleted_at.is_(None),
+ )
+ )
+ .correlate_except(WorkflowModel)
+ .exists()
+ )
+ return and_(
+ workflow_permanent_id_col.isnot(None),
+ ~active_workflow_exists,
+ ).label("workflow_deleted")
+
@db_operation("get_running_workflow_runs_info_globally")
async def get_running_workflow_runs_info_globally(
self,
@@ -546,17 +582,7 @@ class WorkflowRunsRepository(BaseRepository):
TaskRunModel.workflow_permanent_id,
WorkflowRunModel.workflow_permanent_id,
)
- # True iff this row's workflow_permanent_id has no active (deleted_at IS NULL) version.
- workflow_deleted_expr = and_(
- effective_wpid.isnot(None),
- ~exists().where(
- and_(
- WorkflowModel.workflow_permanent_id == effective_wpid,
- WorkflowModel.organization_id == TaskRunModel.organization_id,
- WorkflowModel.deleted_at.is_(None),
- )
- ),
- ).label("workflow_deleted")
+ workflow_deleted_expr = self._workflow_deleted_expr(effective_wpid, TaskRunModel.organization_id)
query = (
select(
TaskRunModel.task_run_id.label("task_run_id"),
@@ -602,10 +628,60 @@ class WorkflowRunsRepository(BaseRepository):
)
offset = (page - 1) * page_size
- query = query.order_by(TaskRunModel.created_at.desc()).offset(offset).limit(page_size)
+ # Search merges task_runs and fallback workflow_runs before slicing, so each source fetches enough rows.
+ query_limit = min(page * page_size, MAX_SEARCH_FETCH_LIMIT) if search_key else page_size
+ query = query.order_by(TaskRunModel.created_at.desc()).limit(query_limit)
+ if not search_key:
+ query = query.offset(offset)
result = await session.execute(query)
- return [dict(row) for row in result.mappings().all()]
+ rows = [dict(row) for row in result.mappings().all()]
+
+ if search_key:
+ task_run_exists = (
+ select(1)
+ .select_from(TaskRunModel)
+ .where(TaskRunModel.organization_id == WorkflowRunModel.organization_id)
+ .where(TaskRunModel.run_id == WorkflowRunModel.workflow_run_id)
+ .correlate_except(TaskRunModel)
+ .exists()
+ )
+ fallback_query = (
+ select(
+ WorkflowRunModel.workflow_run_id.label("task_run_id"),
+ WorkflowRunModel.workflow_run_id.label("run_id"),
+ literal(RunType.workflow_run.value).label("task_run_type"),
+ WorkflowRunModel.status.label("status"),
+ WorkflowModel.title.label("title"),
+ WorkflowRunModel.started_at.label("started_at"),
+ WorkflowRunModel.finished_at.label("finished_at"),
+ WorkflowRunModel.created_at.label("created_at"),
+ WorkflowRunModel.workflow_permanent_id.label("workflow_permanent_id"),
+ WorkflowRunModel.script_run.label("script_run"),
+ WorkflowModel.title.label("searchable_text"),
+ self._workflow_deleted_expr(
+ WorkflowRunModel.workflow_permanent_id,
+ WorkflowRunModel.organization_id,
+ ),
+ )
+ .select_from(WorkflowRunModel)
+ .outerjoin(WorkflowModel, WorkflowModel.workflow_id == WorkflowRunModel.workflow_id)
+ .filter(WorkflowRunModel.organization_id == organization_id)
+ .filter(WorkflowRunModel.parent_workflow_run_id.is_(None))
+ .filter(WorkflowRunModel.debug_session_id.is_(None))
+ .filter(WorkflowRunModel.copilot_session_id.is_(None))
+ .filter(~task_run_exists)
+ )
+ fallback_query = self._apply_workflow_run_search_key_filter(fallback_query, search_key)
+ if status:
+ fallback_query = fallback_query.filter(WorkflowRunModel.status.in_(status))
+ fallback_query = fallback_query.order_by(WorkflowRunModel.created_at.desc()).limit(query_limit)
+ fallback_result = await session.execute(fallback_query)
+ rows.extend(dict(row) for row in fallback_result.mappings().all())
+ rows.sort(key=lambda row: row["created_at"], reverse=True)
+ rows = rows[offset : offset + page_size]
+
+ return rows
@read_retry()
@db_operation("get_workflow_run", log_errors=False)
@@ -827,12 +903,18 @@ class WorkflowRunsRepository(BaseRepository):
return [convert_to_workflow_run(workflow_run) for workflow_run in workflow_runs]
@staticmethod
- def _apply_search_key_filter(query, search_key: str | None): # type: ignore[no-untyped-def]
+ def _apply_workflow_run_search_key_filter(query, search_key: str | None): # type: ignore[no-untyped-def]
if not search_key:
return query
- key_like = f"%{search_key}%"
+ # Call only on WorkflowRunModel queries that already join WorkflowModel. The TaskRunModel query in
+ # get_all_runs_v2 uses its own filter so workflow-title search cannot add an implicit workflows FROM.
# Match workflow_run_id directly
- id_matches = WorkflowRunModel.workflow_run_id.ilike(key_like)
+ id_matches = WorkflowRunModel.workflow_run_id.icontains(search_key, autoescape=True)
+ workflow_title_matches = WorkflowModel.title.icontains(search_key, autoescape=True)
+ workflow_permanent_id_matches = WorkflowRunModel.workflow_permanent_id.icontains(
+ search_key,
+ autoescape=True,
+ )
# Match parameter key or description (only for non-deleted parameter definitions)
# Use EXISTS to avoid duplicate rows and to keep pagination correct
param_key_desc_exists = exists(
@@ -846,8 +928,8 @@ class WorkflowRunsRepository(BaseRepository):
.where(WorkflowParameterModel.deleted_at.is_(None))
.where(
or_(
- WorkflowParameterModel.key.ilike(key_like),
- WorkflowParameterModel.description.ilike(key_like),
+ WorkflowParameterModel.key.icontains(search_key, autoescape=True),
+ WorkflowParameterModel.description.icontains(search_key, autoescape=True),
)
)
)
@@ -856,14 +938,23 @@ class WorkflowRunsRepository(BaseRepository):
select(1)
.select_from(WorkflowRunParameterModel)
.where(WorkflowRunParameterModel.workflow_run_id == WorkflowRunModel.workflow_run_id)
- .where(WorkflowRunParameterModel.value.ilike(key_like))
+ .where(WorkflowRunParameterModel.value.icontains(search_key, autoescape=True))
)
# Match extra HTTP headers (cast JSON to text for search, skip NULLs)
extra_headers_match = and_(
WorkflowRunModel.extra_http_headers.isnot(None),
- func.cast(WorkflowRunModel.extra_http_headers, Text()).ilike(key_like),
+ func.cast(WorkflowRunModel.extra_http_headers, Text()).icontains(search_key, autoescape=True),
+ )
+ return query.where(
+ or_(
+ id_matches,
+ workflow_title_matches,
+ workflow_permanent_id_matches,
+ param_key_desc_exists,
+ param_value_exists,
+ extra_headers_match,
+ )
)
- return query.where(or_(id_matches, param_key_desc_exists, param_value_exists, extra_headers_match))
def _apply_error_code_filter(self, query, error_code: str | None): # type: ignore[no-untyped-def]
if not error_code:
@@ -935,7 +1026,7 @@ class WorkflowRunsRepository(BaseRepository):
.filter(WorkflowRunModel.copilot_session_id.is_(None))
)
- query = self._apply_search_key_filter(query, search_key)
+ query = self._apply_workflow_run_search_key_filter(query, search_key)
query = self._apply_error_code_filter(query, error_code)
if status:
@@ -1055,7 +1146,7 @@ class WorkflowRunsRepository(BaseRepository):
)
if exclude_child_runs:
query = query.filter(WorkflowRunModel.parent_workflow_run_id.is_(None))
- query = self._apply_search_key_filter(query, search_key)
+ query = self._apply_workflow_run_search_key_filter(query, search_key)
query = self._apply_error_code_filter(query, error_code)
if status:
query = query.filter(WorkflowRunModel.status.in_(status))
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index b21e02d42..4ee4d2c44 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -129,6 +129,7 @@ from skyvern.schemas.artifacts import EntityType, entity_type_to_param
from skyvern.schemas.folders import Folder, FolderCreate, FolderUpdate, UpdateWorkflowFolderRequest
from skyvern.schemas.runs import (
CUA_ENGINES,
+ MAX_SEARCH_FETCH_LIMIT,
BlockRunRequest,
BlockRunResponse,
BulkCancelRunsRequest,
@@ -3349,6 +3350,11 @@ async def get_runs_v2(
),
) -> Response:
analytics.capture("skyvern-oss-agent-runs-v2-get")
+ if search_key and (page - 1) * page_size >= MAX_SEARCH_FETCH_LIMIT:
+ raise HTTPException(
+ status_code=http_status.HTTP_400_BAD_REQUEST,
+ detail=f"Search pagination is limited to the first {MAX_SEARCH_FETCH_LIMIT} matches. Use a narrower search.",
+ )
rows = await app.DATABASE.workflow_runs.get_all_runs_v2(
current_org.organization_id,
diff --git a/skyvern/schemas/runs.py b/skyvern/schemas/runs.py
index e7af263e6..34c865f86 100644
--- a/skyvern/schemas/runs.py
+++ b/skyvern/schemas/runs.py
@@ -61,6 +61,8 @@ from skyvern.schemas.run_enums import ( # noqa: F401
from skyvern.utils.secret_headers import mask_header_values
from skyvern.utils.url_validators import validate_url
+MAX_SEARCH_FETCH_LIMIT = 1000
+
# Type checkers need string Literal values, while pydantic's discriminated
# union preserves enum instances when runtime Literals use the enum members.
if TYPE_CHECKING:
diff --git a/tests/unit/forge/sdk/db/test_workflow_runs_repository.py b/tests/unit/forge/sdk/db/test_workflow_runs_repository.py
index 3d20a7008..0d83169fb 100644
--- a/tests/unit/forge/sdk/db/test_workflow_runs_repository.py
+++ b/tests/unit/forge/sdk/db/test_workflow_runs_repository.py
@@ -11,6 +11,7 @@ from sqlalchemy.exc import IntegrityError
from skyvern.forge.sdk.db.repositories.workflow_runs import WorkflowRunsRepository
from skyvern.forge.sdk.workflow.models.parameter import WorkflowParameter, WorkflowParameterType
+from skyvern.schemas.runs import MAX_SEARCH_FETCH_LIMIT
def _make_workflow_parameter(
@@ -62,6 +63,10 @@ def _where_clause_sql(query: Any) -> str:
return str(query.whereclause.compile(compile_kwargs={"literal_binds": True}))
+def _query_sql(query: Any) -> str:
+ return str(query.compile(compile_kwargs={"literal_binds": True}))
+
+
def _assert_not_filtering_copilot_authored_workflows(where_clause: str) -> None:
assert "workflows.created_by" not in where_clause
assert "workflows.edited_by" not in where_clause
@@ -187,10 +192,10 @@ async def test_get_all_runs_v2_search_key_matches_run_id_and_workflow_permanent_
"""Regression test for SKY-8795: searching by run_id (wr_*/tsk_*) or wpid_*
on the global runs page must match the underlying ID columns, not only
`searchable_text` (which contains only title + url)."""
- captured: dict[str, Any] = {}
+ captured_queries: list[Any] = []
async def _execute(query):
- captured["query"] = query
+ captured_queries.append(query)
return _EmptyExecuteResult()
session = MagicMock()
@@ -198,17 +203,29 @@ async def test_get_all_runs_v2_search_key_matches_run_id_and_workflow_permanent_
repo = WorkflowRunsRepository(session_factory=lambda: _SessionContext(session), debug_enabled=False)
- await repo.get_all_runs_v2(organization_id="o_test", search_key="wr_abc123")
+ await repo.get_all_runs_v2(organization_id="o_test", page=10, page_size=100, search_key="wr_abc123")
- # Inspect the WHERE clause specifically — both columns are also in the SELECT
- # list, so a substring check on the full SQL would be a false positive.
- where_clause = _where_clause_sql(captured["query"])
+ assert len(captured_queries) == 2
+
+ # Inspect WHERE clauses specifically — these columns are also in SELECT lists,
+ # so substring checks on full SQL would be false positives.
+ where_clause = _where_clause_sql(captured_queries[0])
assert "task_runs.run_id" in where_clause
# WPID search must match across both task_runs and the joined workflow_runs
# so legacy rows with task_runs.workflow_permanent_id=NULL still hit.
assert "coalesce(task_runs.workflow_permanent_id, workflow_runs.workflow_permanent_id)" in where_clause
# autoescape rewrites '_' to e.g. '/_' so check the distinctive suffix.
assert "abc123" in where_clause
+ assert ".".join(("workflows", "title")) not in where_clause
+
+ fallback_where_clause = _where_clause_sql(captured_queries[1])
+ assert "workflow_runs.workflow_run_id" in fallback_where_clause
+ assert ".".join(("workflows", "title")) in fallback_where_clause
+ assert "workflow_runs.workflow_permanent_id" in fallback_where_clause
+ assert "task_runs.run_id = workflow_runs.workflow_run_id" in fallback_where_clause
+
+ for query in captured_queries:
+ assert f"LIMIT {MAX_SEARCH_FETCH_LIMIT}" in _query_sql(query)
@pytest.mark.asyncio
diff --git a/tests/unit/test_runs_v2_route.py b/tests/unit/test_runs_v2_route.py
index d0795b057..076368373 100644
--- a/tests/unit/test_runs_v2_route.py
+++ b/tests/unit/test_runs_v2_route.py
@@ -11,6 +11,7 @@ from skyvern.exceptions import WorkflowNotFound
from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
from skyvern.forge.sdk.routes import agent_protocol
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRequestBody, WorkflowRunStatus
+from skyvern.schemas.runs import MAX_SEARCH_FETCH_LIMIT
@pytest.mark.asyncio
@@ -69,6 +70,28 @@ async def test_get_runs_v2_serializes_mapping_rows_from_database(monkeypatch: py
]
+async def test_get_runs_v2_rejects_search_page_beyond_fetch_cap(monkeypatch: pytest.MonkeyPatch) -> None:
+ mock_workflow_runs = SimpleNamespace(get_all_runs_v2=AsyncMock(return_value=[]))
+ mock_database = SimpleNamespace(workflow_runs=mock_workflow_runs)
+ monkeypatch.setattr(agent_protocol.app, "DATABASE", mock_database)
+
+ page_size = 100
+ page = (MAX_SEARCH_FETCH_LIMIT // page_size) + 1
+
+ with pytest.raises(HTTPException) as exc_info:
+ await agent_protocol.get_runs_v2(
+ current_org=SimpleNamespace(organization_id="org_123"),
+ page=page,
+ page_size=page_size,
+ search_key="wr_abc123",
+ )
+
+ assert exc_info.value.status_code == 400
+ assert str(MAX_SEARCH_FETCH_LIMIT) in exc_info.value.detail
+ mock_workflow_runs.get_all_runs_v2.assert_not_awaited()
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
("handler", "expected_exclude_child_runs"),