Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Fix: Script.is_pinned AttributeError breaking all cached execution (#5285)
pedrohsdbbb6d63686148
diff --git a/fern/openapi/skyvern_openapi.json b/fern/openapi/skyvern_openapi.jsonindex d263724df..897c7054b 100644--- a/fern/openapi/skyvern_openapi.json+++ b/fern/openapi/skyvern_openapi.json@@ -2444,24 +2444,6 @@},"description": "Number of items per page"},- {- "name": "vault_type",- "in": "query",- "required": false,- "schema": {- "anyOf": [- {- "$ref": "#/components/schemas/CredentialVaultType"- },- {- "type": "null"- }- ],- "description": "Filter credentials by vault type (e.g. 'custom', 'bitwarden', 'azure_vault')",- "title": "Vault Type"- },- "description": "Filter credentials by vault type (e.g. 'custom', 'bitwarden', 'azure_vault')"- },{"name": "x-api-key","in": "header",@@ -13769,7 +13751,7 @@}],"title": "Run With",- "description": "Whether to run the task with agent or code. Null means use the default.",+ "description": "Whether to run the task with agent or code.","examples": ["agent","code"@@ -16094,9 +16076,15 @@"title": "Extra Http Headers"},"run_with": {- "type": "string",- "title": "Run With",- "default": "agent"+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "title": "Run With"},"ai_fallback": {"type": "boolean",@@ -16339,9 +16327,15 @@"default": "published"},"run_with": {- "type": "string",- "title": "Run With",- "default": "agent"+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "title": "Run With"},"ai_fallback": {"type": "boolean",@@ -17139,21 +17133,6 @@],"title": "Failure Reason"},- "failure_category": {- "anyOf": [- {- "items": {- "additionalProperties": true,- "type": "object"- },- "type": "array"- },- {- "type": "null"- }- ],- "title": "Failure Category"- },"parent_workflow_run_id": {"anyOf": [{@@ -17985,7 +17964,7 @@}],"title": "Run With",- "description": "Whether to run the workflow with agent or code. Null inherits from the workflow setting.",+ "description": "Whether to run the workflow with agent or code.","examples": ["agent","code"@@ -18259,10 +18238,16 @@"description": "Type of run - always workflow_run for workflow runs"},"run_with": {- "type": "string",+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],"title": "Run With","description": "Whether the workflow run was executed with agent or code",- "default": "agent","examples": ["agent","code"diff --git a/skyvern/forge/sdk/db/mixins/scripts.py b/skyvern/forge/sdk/db/mixins/scripts.pyindex 005ac38f0..7db8b69b4 100644--- a/skyvern/forge/sdk/db/mixins/scripts.py+++ b/skyvern/forge/sdk/db/mixins/scripts.py@@ -452,7 +452,7 @@ class ScriptsMixin:workflow_run_id: str | None = None,cache_key: str | None = None,statuses: list[ScriptStatus] | None = None,- ) -> Script | None:+ ) -> tuple[Script | None, bool]:"""Get the best script version linked to a workflow by a specific cache_key_value.Selection priority:@@ -460,6 +460,10 @@ class ScriptsMixin:current when pinning occurred (i.e. the version the pinned rowpoints to). This ensures pinned scripts are immutable.2. Otherwise, return the latest version by version number.++ Returns:+ A tuple of (script, is_pinned) where is_pinned indicates whether+ the returned script came from a pinned workflow_script row."""async with self.Session() as session:# Build the query: join workflow_scripts with scripts@@ -505,7 +509,7 @@ class ScriptsMixin:)pinned = (await session.scalars(pinned_query)).first()if pinned:- return convert_to_script(pinned)+ return convert_to_script(pinned), True# --- Fall back to latest version ---latest_query = (@@ -516,7 +520,7 @@ class ScriptsMixin:.limit(1))script = (await session.scalars(latest_query)).first()- return convert_to_script(script) if script else None+ return (convert_to_script(script), False) if script else (None, False)@db_operation("get_workflow_cache_key_count")async def get_workflow_cache_key_count(diff --git a/skyvern/forge/sdk/db/repositories/scripts.py b/skyvern/forge/sdk/db/repositories/scripts.pyindex e1673c580..9a12607cc 100644--- a/skyvern/forge/sdk/db/repositories/scripts.py+++ b/skyvern/forge/sdk/db/repositories/scripts.py@@ -505,8 +505,13 @@ class ScriptsRepository(BaseRepository):workflow_run_id: str | None = None,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."""+ ) -> tuple[Script | None, bool]:+ """Get latest script version linked to a workflow by a specific cache_key_value.++ Returns:+ A tuple of (script, is_pinned). The repository implementation does not+ support pinned queries, so is_pinned is always False.+ """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@@ -539,7 +544,7 @@ class ScriptsRepository(BaseRepository):query = query.order_by(ScriptModel.created_at.desc(), ScriptModel.version.desc()).limit(1)script = (await session.scalars(query)).first()- return convert_to_script(script) if script else None+ return (convert_to_script(script), False) if script else (None, False)@db_operation("get_workflow_cache_key_count")async def get_workflow_cache_key_count(diff --git a/skyvern/forge/sdk/routes/scripts.py b/skyvern/forge/sdk/routes/scripts.pyindex 5e7823081..67e554a16 100644--- a/skyvern/forge/sdk/routes/scripts.py+++ b/skyvern/forge/sdk/routes/scripts.py@@ -734,7 +734,7 @@ async def get_workflow_script_blocks(statuses=[ScriptStatus.published],)if workflow_script:- published_script = await workflow_script_service.get_workflow_script_by_cache_key_value(+ published_script, _is_pinned = await workflow_script_service.get_workflow_script_by_cache_key_value(organization_id=current_org.organization_id,workflow_permanent_id=workflow_permanent_id,cache_key_value=workflow_script.cache_key_value,@@ -752,7 +752,7 @@ async def get_workflow_script_blocks()# Fall back to latest version (for runs without a workflow_script entry)- published_script, _ = await workflow_script_service.get_workflow_script(+ published_script, _rendered_cache_key, _is_pinned = await workflow_script_service.get_workflow_script(workflow=workflow,workflow_run=workflow_run,status=ScriptStatus.published,@@ -770,7 +770,7 @@ async def get_workflow_script_blocks(cache_key = block_script_request.cache_key or workflow.cache_key or ""status = block_script_request.status- script = await workflow_script_service.get_workflow_script_by_cache_key_value(+ script, _is_pinned = await workflow_script_service.get_workflow_script_by_cache_key_value(organization_id=current_org.organization_id,workflow_permanent_id=workflow_permanent_id,workflow_run_id=block_script_request.workflow_run_id,diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 184b4b98d..5e1f41189 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -1032,7 +1032,9 @@ class WorkflowService:try:# Check if there's a related workflow script that should be used instead- workflow_script, _ = await workflow_script_service.get_workflow_script(workflow, workflow_run, block_labels)+ workflow_script, _, script_is_pinned = await workflow_script_service.get_workflow_script(+ workflow, workflow_run, block_labels+ )current_context = skyvern_context.current()if current_context:if workflow_script:@@ -1048,6 +1050,7 @@ class WorkflowService:block_labels=block_labels,block_outputs=block_outputs,script=workflow_script,+ script_is_pinned=script_is_pinned,)# Check if there's a finally block configured@@ -1223,6 +1226,7 @@ class WorkflowService:block_labels: list[str] | None = None,block_outputs: dict[str, Any] | None = None,script: Script | None = None,+ script_is_pinned: bool = False,) -> tuple[WorkflowRun, set[str]]:organization_id = organization.organization_idworkflow_run_id = workflow_run.workflow_run_id@@ -1297,7 +1301,7 @@ class WorkflowService:)if loaded_script_module:# Mark static (pinned) scripts so complete() skips LLM verification- if script.is_pinned:+ if script_is_pinned:pinned_ctx = skyvern_context.current()if pinned_ctx:pinned_ctx.is_static_script = True@@ -4847,7 +4851,7 @@ class WorkflowService:# request is not madereturn None- existing_script, rendered_cache_key_value = await workflow_script_service.get_workflow_script(+ existing_script, rendered_cache_key_value, _is_pinned = await workflow_script_service.get_workflow_script(workflow,workflow_run,block_labels,@@ -4941,7 +4945,7 @@ class WorkflowService:to handle race conditions where another process regenerated while we waited."""# Double-check: another process may have regenerated while we waited for lock- fresh_script = await workflow_script_service.get_workflow_script_by_cache_key_value(+ fresh_script, _is_pinned = await workflow_script_service.get_workflow_script_by_cache_key_value(organization_id=workflow.organization_id,workflow_permanent_id=workflow.workflow_permanent_id,cache_key_value=rendered_cache_key_value,diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.pyindex a814cf532..f48e42457 100644--- a/skyvern/services/script_service.py+++ b/skyvern/services/script_service.py@@ -1476,7 +1476,7 @@ async def _regenerate_script_block_after_ai_fallback(if not cache_key_value:cache_key_value = cache_key # Fallback- existing_script = await app.DATABASE.get_workflow_script_by_cache_key_value(+ existing_script, _is_pinned = await app.DATABASE.get_workflow_script_by_cache_key_value(organization_id=organization_id,workflow_permanent_id=workflow.workflow_permanent_id,cache_key_value=cache_key_value,diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.pyindex 6f90b4daf..ba6ba2436 100644--- a/skyvern/services/workflow_script_service.py+++ b/skyvern/services/workflow_script_service.py@@ -84,7 +84,7 @@ def workflow_has_conditionals(workflow: Workflow) -> bool:# Cache for workflow scripts - only stores non-None results-_workflow_script_cache: TTLCache[tuple, "Script"] = TTLCache(maxsize=128, ttl=60 * 60)+_workflow_script_cache: TTLCache[tuple, tuple["Script", bool]] = TTLCache(maxsize=128, ttl=60 * 60)def _make_workflow_script_cache_key(@@ -155,7 +155,7 @@ async def generate_or_update_pending_workflow_script(context.script_id = script.script_idcontext.script_revision_id = script.script_revision_id- _, rendered_cache_key_value = await get_workflow_script(+ _script, rendered_cache_key_value, _is_pinned = await get_workflow_script(workflow=workflow,workflow_run=workflow_run,status=ScriptStatus.pending,@@ -175,10 +175,10 @@ async def get_workflow_script(workflow_run: WorkflowRun,block_labels: list[str] | None = None,status: ScriptStatus = ScriptStatus.published,-) -> tuple[Script | None, str]:+) -> tuple[Script | None, str, bool]:"""Check if there's a related workflow script that should be used instead of running the workflow.- Returns the tuple of (script, rendered_cache_key_value).+ Returns the tuple of (script, rendered_cache_key_value, is_pinned)."""cache_key = workflow.cache_key or ""rendered_cache_key_value = ""@@ -226,10 +226,10 @@ async def get_workflow_script(if block_labels:# Do not generate script or run script if block_labels is provided- return None, rendered_cache_key_value+ return None, rendered_cache_key_value, False# Check if there are existing cached scripts for this workflow + cache_key_value- existing_script = await get_workflow_script_by_cache_key_value(+ existing_script, is_pinned = await get_workflow_script_by_cache_key_value(organization_id=workflow.organization_id,workflow_permanent_id=workflow.workflow_permanent_id,cache_key_value=rendered_cache_key_value,@@ -243,8 +243,9 @@ async def get_workflow_script(script_id=existing_script.script_id,cache_key_value=rendered_cache_key_value,workflow_run_id=workflow_run.workflow_run_id,+ is_pinned=is_pinned,)- return existing_script, rendered_cache_key_value+ return existing_script, rendered_cache_key_value, is_pinnedLOG.info("No cached script found for workflow (cache miss)",@@ -253,7 +254,7 @@ async def get_workflow_script(workflow_run_id=workflow_run.workflow_run_id,workflow_permanent_id=workflow.workflow_permanent_id,)- return None, rendered_cache_key_value+ return None, rendered_cache_key_value, Falseexcept Exception as e:LOG.warning(@@ -263,7 +264,7 @@ async def get_workflow_script(error=str(e),exc_info=True,)- return None, rendered_cache_key_value+ return None, rendered_cache_key_value, Falseasync def get_workflow_script_by_cache_key_value(@@ -274,7 +275,13 @@ async def get_workflow_script_by_cache_key_value(cache_key: str | None = None,statuses: list[ScriptStatus] | None = None,use_cache: bool = False,-) -> Script | None:+) -> tuple[Script | None, bool]:+ """Look up the best script for a workflow + cache_key_value.++ Returns:+ A tuple of (script, is_pinned) where is_pinned indicates whether the+ returned script came from a pinned workflow_script row.+ """if use_cache:cache_key_tuple = _make_workflow_script_cache_key(organization_id=organization_id,@@ -289,7 +296,7 @@ async def get_workflow_script_by_cache_key_value(return _workflow_script_cache[cache_key_tuple]# Cache miss - fetch from database- result = await app.DATABASE.get_workflow_script_by_cache_key_value(+ script, is_pinned = await app.DATABASE.get_workflow_script_by_cache_key_value(organization_id=organization_id,workflow_permanent_id=workflow_permanent_id,cache_key_value=cache_key_value,@@ -299,10 +306,10 @@ async def get_workflow_script_by_cache_key_value()# Only cache non-None results- if result is not None:- _workflow_script_cache[cache_key_tuple] = result+ if script is not None:+ _workflow_script_cache[cache_key_tuple] = (script, is_pinned)- return result+ return script, is_pinnedreturn await app.DATABASE.get_workflow_script_by_cache_key_value(organization_id=organization_id,@@ -1194,7 +1201,7 @@ async def create_script_version_from_review(# Create the workflow script mapping for cache lookupif workflow_run:- _, rendered_cache_key_value = await get_workflow_script(+ _script, rendered_cache_key_value, _is_pinned = await get_workflow_script(workflow=workflow,workflow_run=workflow_run,status=ScriptStatus.published,