Skip to content

Automate browser based workflows with AI

Fix pinned scripts: always fetch pinned version and prevent corrections (#5261)

pedrohsdbe869039f60f0
diff --git a/skyvern/forge/sdk/db/agent_db.py b/skyvern/forge/sdk/db/agent_db.py
index edd2537aa..beb791018 100644
--- a/skyvern/forge/sdk/db/agent_db.py
+++ b/skyvern/forge/sdk/db/agent_db.py
@@ -7300,43 +7300,70 @@ class AgentDB(BaseAlchemyDB):
cache_key: str | None = None,
statuses: list[ScriptStatus] | None = None,
) -> Script | None:
- """Get latest script version linked to a workflow by a specific cache_key_value."""
+ """Get the best script version linked to a workflow by a specific cache_key_value.
+
+ Selection priority:
+ 1. If a pinned workflow_script row exists, return the version that was
+ current when pinning occurred (i.e. the version the pinned row
+ points to). This ensures pinned scripts are immutable.
+ 2. Otherwise, return the latest version by version number.
+ """
try:
async with self.Session() as session:
# Build the query: join workflow_scripts with scripts
# Join on both script_id and organization_id to leverage uc_org_script_version index
- query = (
- select(ScriptModel)
- .join(
- WorkflowScriptModel,
- and_(
- ScriptModel.organization_id == WorkflowScriptModel.organization_id,
- ScriptModel.script_id == WorkflowScriptModel.script_id,
- ),
- )
- .where(
- WorkflowScriptModel.organization_id == organization_id,
- WorkflowScriptModel.workflow_permanent_id == workflow_permanent_id,
- WorkflowScriptModel.cache_key_value == cache_key_value,
- WorkflowScriptModel.deleted_at.is_(None),
- )
- )
+ base_where = [
+ WorkflowScriptModel.organization_id == organization_id,
+ WorkflowScriptModel.workflow_permanent_id == workflow_permanent_id,
+ WorkflowScriptModel.cache_key_value == cache_key_value,
+ WorkflowScriptModel.deleted_at.is_(None),
+ ]
- if workflow_run_id:
- query = query.where(WorkflowScriptModel.workflow_run_id == workflow_run_id)
- # Pin to the script version that existed when this run's
- # workflow_script entry was created, not the latest version.
- query = query.where(ScriptModel.created_at <= WorkflowScriptModel.created_at)
+ base_join = and_(
+ ScriptModel.organization_id == WorkflowScriptModel.organization_id,
+ ScriptModel.script_id == WorkflowScriptModel.script_id,
+ )
+ # Filters shared by both pinned and latest queries
+ shared_where: list[Any] = []
if cache_key is not None:
- query = query.where(WorkflowScriptModel.cache_key == cache_key)
-
+ shared_where.append(WorkflowScriptModel.cache_key == cache_key)
+
+ # Filters only for the latest-version (non-pinned) query.
+ # workflow_run_id scopes to the current run's row, which won't
+ # match the pinned row (it belongs to the run where pinning
+ # occurred). statuses may also exclude a pinned row whose
+ # status has since changed; pinned scripts should always win.
+ latest_only_where: list[Any] = []
+ if workflow_run_id:
+ latest_only_where.append(WorkflowScriptModel.workflow_run_id == workflow_run_id)
+ latest_only_where.append(ScriptModel.created_at <= WorkflowScriptModel.created_at)
if statuses is not None and len(statuses) > 0:
- query = query.where(WorkflowScriptModel.status.in_(statuses))
+ latest_only_where.append(WorkflowScriptModel.status.in_(statuses))
- query = query.order_by(ScriptModel.version.desc()).limit(1)
+ # --- Try pinned version first ---
+ pinned_query = (
+ select(ScriptModel)
+ .join(WorkflowScriptModel, base_join)
+ .where(*base_where, *shared_where, WorkflowScriptModel.is_pinned.is_(True))
+ # Use the version that was active when the pinned row was created
+ .where(ScriptModel.created_at <= WorkflowScriptModel.created_at)
+ .order_by(ScriptModel.version.desc())
+ .limit(1)
+ )
+ pinned = (await session.scalars(pinned_query)).first()
+ if pinned:
+ return convert_to_script(pinned)
- script = (await session.scalars(query)).first()
+ # --- Fall back to latest version ---
+ latest_query = (
+ select(ScriptModel)
+ .join(WorkflowScriptModel, base_join)
+ .where(*base_where, *shared_where, *latest_only_where)
+ .order_by(ScriptModel.version.desc())
+ .limit(1)
+ )
+ script = (await session.scalars(latest_query)).first()
return convert_to_script(script) if script else None
except SQLAlchemyError:
LOG.error("SQLAlchemyError", exc_info=True)
diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.py
index c9318f820..6f90b4daf 100644
--- a/skyvern/services/workflow_script_service.py
+++ b/skyvern/services/workflow_script_service.py
@@ -1011,6 +1011,22 @@ async def create_script_version_from_review(
The new Script revision, or None if creation failed.
"""
try:
+ # Defense-in-depth: refuse to create a correction for a pinned script.
+ # _trigger_script_reviewer() already gates on is_script_pinned(), but
+ # that check can be bypassed when the skyvern context is missing.
+ # Guard here so no code path can mutate a pinned script.
+ if await app.DATABASE.is_script_pinned(
+ organization_id=organization_id,
+ script_id=base_script.script_id,
+ ):
+ LOG.info(
+ "Skipping script correction — script is pinned",
+ organization_id=organization_id,
+ script_id=base_script.script_id,
+ workflow_permanent_id=workflow_permanent_id,
+ )
+ return None
+
# Create a new script version
new_script = await app.DATABASE.create_script(
organization_id=organization_id,