Skip to content

Automate browser based workflows with AI

Optimize caching: content dedup, recursive for-loop tracking, observability (SKY-8684) (#5371)

Aaron Perez58bbc7dbb9ab
diff --git a/alembic/versions/2026_04_03_0111-f7bf5845eafb_add_content_hash_dedup_index_to_script_files.py b/alembic/versions/2026_04_03_0111-f7bf5845eafb_add_content_hash_dedup_index_to_script_files.py
new file mode 100644
index 000000000..9b72092b0
--- /dev/null
+++ b/alembic/versions/2026_04_03_0111-f7bf5845eafb_add_content_hash_dedup_index_to_script_files.py
@@ -0,0 +1,25 @@
+"""add_content_hash_dedup_index_to_script_files
+
+Revision ID: f7bf5845eafb
+Revises: 5516c5bf7762
+Create Date: 2026-04-03T01:11:07.051219+00:00
+
+"""
+
+from typing import Sequence, Union
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "f7bf5845eafb"
+down_revision: Union[str, None] = "5516c5bf7762"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.create_index("ix_script_files_dedup", "script_files", ["script_id", "organization_id", "content_hash"])
+
+
+def downgrade() -> None:
+ op.drop_index("ix_script_files_dedup", table_name="script_files")
diff --git a/skyvern/core/script_generations/generate_script.py b/skyvern/core/script_generations/generate_script.py
index 7367ad3d2..2310c289f 100644
--- a/skyvern/core/script_generations/generate_script.py
+++ b/skyvern/core/script_generations/generate_script.py
@@ -6,7 +6,6 @@ Generate a runnable Skyvern workflow script.
from __future__ import annotations
-import asyncio
import hashlib
import keyword
import re
@@ -45,6 +44,15 @@ class ScriptBlockSource:
requires_agent: bool | None = None
+@dataclass
+class CodeGenResult:
+ """Result of generate_workflow_script_python_code() with block-creation telemetry."""
+
+ source_code: str
+ blocks_created: int
+ blocks_failed: int
+
+
def _build_existing_field_assignments(
blocks: list[dict[str, Any]],
actions_by_task: dict[str, list[dict[str, Any]]],
@@ -2724,15 +2732,19 @@ async def generate_workflow_script_python_code(
updated_block_labels: set[str] | None = None,
use_semantic_selectors: bool = False,
adaptive_caching: bool = False,
-) -> str:
+) -> CodeGenResult:
"""
Build a LibCST Module and emit .code (PEP-8-formatted source).
Cached script blocks can be reused by providing them via `cached_blocks`. Any labels present in
`updated_block_labels` will be regenerated from the latest workflow run execution data.
+
+ Returns a CodeGenResult containing the source code and block-creation success/failure counts.
"""
cached_blocks = cached_blocks or {}
updated_block_labels = set(updated_block_labels or [])
+ blocks_created = 0
+ blocks_failed = 0
# Drop cached entries that do not have usable source
cached_blocks = {label: source for label, source in cached_blocks.items() if source.code}
@@ -2872,21 +2884,22 @@ async def generate_workflow_script_python_code(
block_workflow_run_block_id = task.get("workflow_run_block_id")
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=block_code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=block_name,
- update=pending,
- run_signature=run_signature,
- workflow_run_id=block_workflow_run_id,
- workflow_run_block_id=block_workflow_run_block_id,
- input_fields=input_fields,
- )
- except Exception as e:
- LOG.error("Failed to create script block", error=str(e), exc_info=True)
+ ok = await create_or_update_script_block(
+ block_code=block_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=block_name,
+ update=pending,
+ run_signature=run_signature,
+ workflow_run_id=block_workflow_run_id,
+ workflow_run_block_id=block_workflow_run_block_id,
+ input_fields=input_fields,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
append_block_code(block_code)
@@ -2945,21 +2958,22 @@ async def generate_workflow_script_python_code(
run_signature = cst.Module(body=[task_v2_stmt]).code.strip()
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=block_code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=task_v2_label,
- update=pending,
- run_signature=run_signature,
- workflow_run_id=block_workflow_run_id,
- workflow_run_block_id=block_workflow_run_block_id,
- input_fields=input_fields,
- )
- except Exception as e:
- LOG.error("Failed to create task_v2 script block", error=str(e), exc_info=True)
+ ok = await create_or_update_script_block(
+ block_code=block_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=task_v2_label,
+ update=pending,
+ run_signature=run_signature,
+ workflow_run_id=block_workflow_run_id,
+ workflow_run_block_id=block_workflow_run_block_id,
+ input_fields=input_fields,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
append_block_code(block_code)
@@ -2989,21 +3003,22 @@ async def generate_workflow_script_python_code(
run_signature = block_code.strip()
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=block_code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=for_loop_label,
- update=pending,
- run_signature=run_signature,
- workflow_run_id=block_workflow_run_id,
- workflow_run_block_id=block_workflow_run_block_id,
- input_fields=None,
- )
- except Exception as e:
- LOG.error("Failed to create for_loop script block", error=str(e), exc_info=True)
+ ok = await create_or_update_script_block(
+ block_code=block_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=for_loop_label,
+ update=pending,
+ run_signature=run_signature,
+ workflow_run_id=block_workflow_run_id,
+ workflow_run_block_id=block_workflow_run_block_id,
+ input_fields=None,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
# NOTE: Do NOT call append_block_code() for for_loop blocks.
# Unlike task blocks (which produce function definitions valid at module level),
@@ -3055,29 +3070,25 @@ async def generate_workflow_script_python_code(
# Create script_block entry for preservation across regenerations
if script_id and script_revision_id and organization_id:
- try:
- inner_input_fields = _collect_block_input_fields(loop_block, actions_by_task)
- if not inner_input_fields and cached_inner and cached_inner.input_fields:
- inner_input_fields = cached_inner.input_fields
- await create_or_update_script_block(
- block_code=inner_block_code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=inner_label,
- update=pending,
- run_signature=inner_run_signature,
- workflow_run_id=inner_wri,
- workflow_run_block_id=inner_wrbi,
- input_fields=inner_input_fields,
- )
- except Exception as e:
- LOG.error(
- "Failed to create for_loop inner block script block",
- error=str(e),
- block_label=inner_label,
- exc_info=True,
- )
+ inner_input_fields = _collect_block_input_fields(loop_block, actions_by_task)
+ if not inner_input_fields and cached_inner and cached_inner.input_fields:
+ inner_input_fields = cached_inner.input_fields
+ ok = await create_or_update_script_block(
+ block_code=inner_block_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=inner_label,
+ update=pending,
+ run_signature=inner_run_signature,
+ workflow_run_id=inner_wri,
+ workflow_run_block_id=inner_wrbi,
+ input_fields=inner_input_fields,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
append_block_code(inner_block_code)
@@ -3097,39 +3108,41 @@ async def generate_workflow_script_python_code(
if cached_source and cached_source.run_signature and not cached_source.requires_agent:
# Reviewer upgraded this block to code — preserve it
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=cached_source.code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=arb_label,
- update=pending,
- run_signature=cached_source.run_signature,
- workflow_run_id=cached_source.workflow_run_id,
- workflow_run_block_id=cached_source.workflow_run_block_id,
- requires_agent=False,
- )
- except Exception as e:
- LOG.error("Failed to preserve coded agent block", error=str(e), exc_info=True)
+ ok = await create_or_update_script_block(
+ block_code=cached_source.code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=arb_label,
+ update=pending,
+ run_signature=cached_source.run_signature,
+ workflow_run_id=cached_source.workflow_run_id,
+ workflow_run_block_id=cached_source.workflow_run_block_id,
+ requires_agent=False,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
append_block_code(cached_source.code)
else:
# Create a requires_agent entry (no code, no run_signature)
placeholder_code = f"# Block '{arb_label}' ({arb['block_type']}) — executed via agent"
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=placeholder_code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=arb_label,
- update=pending,
- run_signature=None,
- requires_agent=True,
- )
- except Exception as e:
- LOG.error("Failed to create agent-required script block", error=str(e), exc_info=True)
+ ok = await create_or_update_script_block(
+ block_code=placeholder_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=arb_label,
+ update=pending,
+ run_signature=None,
+ requires_agent=True,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
# --- preserve cached blocks from unexecuted branches ----------------
# When a workflow has conditional blocks, not all branches execute in a single run.
@@ -3166,26 +3179,22 @@ async def generate_workflow_script_python_code(
continue # Skip entries without usable code/metadata
if script_id and script_revision_id and organization_id:
- try:
- await create_or_update_script_block(
- block_code=cached_source.code,
- script_revision_id=script_revision_id,
- script_id=script_id,
- organization_id=organization_id,
- block_label=cached_label,
- update=pending,
- run_signature=cached_source.run_signature,
- workflow_run_id=cached_source.workflow_run_id,
- workflow_run_block_id=cached_source.workflow_run_block_id,
- input_fields=cached_source.input_fields,
- )
- except Exception as e:
- LOG.error(
- "Failed to preserve cached script block from unexecuted branch",
- error=str(e),
- block_label=cached_label,
- exc_info=True,
- )
+ ok = await create_or_update_script_block(
+ block_code=cached_source.code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=cached_label,
+ update=pending,
+ run_signature=cached_source.run_signature,
+ workflow_run_id=cached_source.workflow_run_id,
+ workflow_run_block_id=cached_source.workflow_run_block_id,
+ input_fields=cached_source.input_fields,
+ )
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
append_block_code(cached_source.code)
preserved_count += 1
@@ -3241,7 +3250,7 @@ async def generate_workflow_script_python_code(
start_block_module = cst.Module(body=start_block_body)
start_block_code = start_block_module.code
- await create_or_update_script_block(
+ ok = await create_or_update_script_block(
block_code=start_block_code,
script_revision_id=script_revision_id,
script_id=script_id,
@@ -3249,9 +3258,13 @@ async def generate_workflow_script_python_code(
block_label=settings.WORKFLOW_START_BLOCK_LABEL,
update=pending,
)
+ if ok:
+ blocks_created += 1
+ else:
+ blocks_failed += 1
except Exception as e:
LOG.error("Failed to create __start_block__", error=str(e), exc_info=True)
- # Continue without script block creation if it fails
+ blocks_failed += 1
# Build module body with the start block content and other blocks
module_body = [
@@ -3260,7 +3273,7 @@ async def generate_workflow_script_python_code(
]
module = cst.Module(body=module_body)
- return module.code
+ return CodeGenResult(source_code=module.code, blocks_created=blocks_created, blocks_failed=blocks_failed)
async def create_or_update_script_block(
@@ -3275,11 +3288,13 @@ async def create_or_update_script_block(
workflow_run_block_id: str | None = None,
input_fields: list[str] | None = None,
requires_agent: bool | None = None,
-) -> None:
+) -> bool:
"""
Create a script block in the database and save the block code to a script file.
If update is True, the script block will be updated instead of created.
+ Returns True on success, False if an error occurred (logged but not raised).
+
Args:
block_code: The code to save
script_revision_id: The script revision ID
@@ -3294,8 +3309,10 @@ async def create_or_update_script_block(
requires_agent: Whether this block must be executed via agent (None = don't change on update)
"""
block_code_bytes = block_code if isinstance(block_code, bytes) else block_code.encode("utf-8")
+ content_hash = f"sha256:{hashlib.sha256(block_code_bytes).hexdigest()}"
+
try:
- # Step 3: Create script block in database
+ # Step 1: Get or create ScriptBlock record for this revision
script_block = await app.DATABASE.scripts.get_script_block_by_label(
organization_id=organization_id,
script_revision_id=script_revision_id,
@@ -3328,49 +3345,117 @@ async def create_or_update_script_block(
requires_agent=requires_agent,
)
- # Step 4: Create script file for the block
- # Generate a unique filename for the block
+ # Step 2: Create or update ScriptFile with content deduplication
file_name = f"{block_label}.skyvern"
file_path = f"blocks/{file_name}"
- # Create artifact and upload to S3
- artifact_id = None
if update and script_block.script_file_id:
+ # UPDATE path: block already has a ScriptFile in this revision
script_file = await app.DATABASE.scripts.get_script_file_by_id(
script_revision_id,
script_block.script_file_id,
organization_id,
)
+ if script_file and script_file.content_hash == content_hash:
+ # Content unchanged — skip S3 upload entirely
+ LOG.info(
+ "script_block_dedup_hit",
+ script_id=script_id,
+ block_label=block_label,
+ content_hash=content_hash,
+ dedup_type="update_same_revision",
+ )
+ return True
+
+ # Content changed — await S3 upload, then update hash only on success.
+ # Intentionally sequential (not fire-and-forget) so content_hash is never
+ # updated unless S3 write succeeds, preventing stale-artifact dedup hits.
if script_file and script_file.artifact_id:
artifact = await app.DATABASE.artifacts.get_artifact_by_id(script_file.artifact_id, organization_id)
if artifact:
- asyncio.create_task(app.STORAGE.store_artifact(artifact, block_code_bytes))
+ await app.STORAGE.store_artifact(artifact, block_code_bytes)
+ await app.DATABASE.scripts.update_script_file(
+ script_file_id=script_file.file_id,
+ organization_id=organization_id,
+ content_hash=content_hash,
+ )
+ LOG.info(
+ "script_block_dedup_miss",
+ script_id=script_id,
+ block_label=block_label,
+ content_hash=content_hash,
+ dedup_type="update_content_changed",
+ )
+ return True
+ else:
+ LOG.error(
+ "Artifact not found, cannot update S3",
+ artifact_id=script_file.artifact_id,
+ script_file_id=script_file.file_id,
+ )
+ return False
else:
LOG.error("Script file or artifact not found", script_file_id=script_block.script_file_id)
+ return False
else:
- artifact_id = await app.ARTIFACT_MANAGER.create_script_file_artifact(
- organization_id=organization_id,
- script_id=script_id,
- script_version=1, # Assuming version 1 for now
- file_path=file_path,
- data=block_code_bytes,
- )
-
- # Create script file record
- script_file = await app.DATABASE.scripts.create_script_file(
- script_revision_id=script_revision_id,
+ # CREATE path: check for existing ScriptFile with matching hash (cross-revision dedup)
+ existing_file = await app.DATABASE.scripts.get_script_file_by_content_hash(
script_id=script_id,
organization_id=organization_id,
- file_path=file_path,
- file_name=file_name,
- file_type="file",
- content_hash=f"sha256:{hashlib.sha256(block_code_bytes).hexdigest()}",
- file_size=len(block_code_bytes),
- mime_type="text/x-python",
- artifact_id=artifact_id,
+ content_hash=content_hash,
)
- # update script block with script file id
+ if existing_file and existing_file.artifact_id:
+ # Content matches a previous version — reuse artifact, skip S3 upload
+ script_file = await app.DATABASE.scripts.create_script_file(
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ file_path=file_path,
+ file_name=file_name,
+ file_type="file",
+ content_hash=content_hash,
+ file_size=len(block_code_bytes),
+ mime_type="text/x-python",
+ artifact_id=existing_file.artifact_id,
+ )
+ LOG.info(
+ "script_block_dedup_hit",
+ script_id=script_id,
+ block_label=block_label,
+ content_hash=content_hash,
+ dedup_type="create_cross_revision",
+ )
+ else:
+ # No match — full S3 upload
+ artifact_id = await app.ARTIFACT_MANAGER.create_script_file_artifact(
+ organization_id=organization_id,
+ script_id=script_id,
+ script_version=1,
+ file_path=file_path,
+ data=block_code_bytes,
+ )
+ script_file = await app.DATABASE.scripts.create_script_file(
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ file_path=file_path,
+ file_name=file_name,
+ file_type="file",
+ content_hash=content_hash,
+ file_size=len(block_code_bytes),
+ mime_type="text/x-python",
+ artifact_id=artifact_id,
+ )
+ LOG.info(
+ "script_block_dedup_miss",
+ script_id=script_id,
+ block_label=block_label,
+ content_hash=content_hash,
+ dedup_type="create_new_content",
+ )
+
+ # Link ScriptBlock to its ScriptFile
await app.DATABASE.scripts.update_script_block(
script_block_id=script_block.script_block_id,
organization_id=organization_id,
@@ -3379,8 +3464,13 @@ async def create_or_update_script_block(
workflow_run_block_id=workflow_run_block_id,
)
- except Exception as e:
- # Log error but don't fail the entire generation process
- LOG.error("Failed to create script block", error=str(e), exc_info=True)
- # For now, just log the error and continue
- # In production, you might want to handle this differently
+ return True
+
+ except Exception:
+ LOG.exception(
+ "Failed to create or update script block — caller will track failure",
+ block_label=block_label,
+ script_id=script_id,
+ script_revision_id=script_revision_id,
+ )
+ return False
diff --git a/skyvern/forge/sdk/db/agent_db.py b/skyvern/forge/sdk/db/agent_db.py
index ef3d2ee63..5cd02de79 100644
--- a/skyvern/forge/sdk/db/agent_db.py
+++ b/skyvern/forge/sdk/db/agent_db.py
@@ -627,6 +627,9 @@ class AgentDB(BaseAlchemyDB):
async def get_script_file_by_path(self, *args: Any, **kwargs: Any) -> Any:
return await self.scripts.get_script_file_by_path(*args, **kwargs)
+ async def get_script_file_by_content_hash(self, *args: Any, **kwargs: Any) -> Any:
+ return await self.scripts.get_script_file_by_content_hash(*args, **kwargs)
+
async def update_script_file(self, *args: Any, **kwargs: Any) -> Any:
return await self.scripts.update_script_file(*args, **kwargs)
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index a87663a97..a58470a19 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -1133,6 +1133,7 @@ class ScriptFileModel(Base):
__tablename__ = "script_files"
__table_args__ = (
Index("file_script_path_index", "script_revision_id", "file_path"),
+ Index("ix_script_files_dedup", "script_id", "organization_id", "content_hash"),
UniqueConstraint("script_revision_id", "file_path", name="unique_script_file_path"),
)
diff --git a/skyvern/forge/sdk/db/repositories/scripts.py b/skyvern/forge/sdk/db/repositories/scripts.py
index 0a6a63cf6..b97cf9725 100644
--- a/skyvern/forge/sdk/db/repositories/scripts.py
+++ b/skyvern/forge/sdk/db/repositories/scripts.py
@@ -374,12 +374,34 @@ class ScriptsRepository(BaseRepository):
).first()
return convert_to_script_file(script_file) if script_file else None
+ @db_operation("get_script_file_by_content_hash")
+ async def get_script_file_by_content_hash(
+ self,
+ script_id: str,
+ organization_id: str,
+ content_hash: str,
+ ) -> ScriptFile | None:
+ """Find the most recent ScriptFile with a matching content_hash across all revisions of a script."""
+ async with self.Session() as session:
+ script_file = (
+ await session.scalars(
+ select(ScriptFileModel)
+ .filter_by(script_id=script_id)
+ .filter_by(organization_id=organization_id)
+ .filter_by(content_hash=content_hash)
+ .order_by(ScriptFileModel.created_at.desc())
+ .limit(1)
+ )
+ ).first()
+ return convert_to_script_file(script_file) if script_file else None
+
@db_operation("update_script_file")
async def update_script_file(
self,
script_file_id: str,
organization_id: str,
artifact_id: str | None = None,
+ content_hash: str | None = None,
) -> ScriptFile:
async with self.Session() as session:
script_file = (
@@ -390,6 +412,8 @@ class ScriptsRepository(BaseRepository):
if script_file:
if artifact_id:
script_file.artifact_id = artifact_id
+ if content_hash is not None:
+ script_file.content_hash = content_hash
await session.commit()
await session.refresh(script_file)
return convert_to_script_file(script_file)
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 78c6e6ce0..ef93545a3 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -144,6 +144,32 @@ BLOCK_TYPES_THAT_SHOULD_BE_CACHED = {
}
+def _collect_uncached_loop_children(
+ block: ForLoopBlock,
+ script_blocks_by_label: dict[str, object],
+ blocks_to_update: set[str],
+) -> None:
+ """Recursively collect uncached cacheable children from nested for-loops.
+
+ ForLoopBlock children execute via block.py's execute_loop_helper(),
+ bypassing _execute_single_block() where blocks_to_update tracking lives.
+ This function walks all nesting levels so the script generator produces
+ cached functions for deeply nested blocks (e.g., file_download inside
+ a double-nested for-loop).
+ """
+ for child in block.loop_blocks:
+ if (
+ child.label
+ and child.label not in script_blocks_by_label
+ and child.block_type in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+ ):
+ blocks_to_update.add(child.label)
+ # Recurse into nested for-loops regardless of whether the for-loop
+ # itself is cached — its children may not be.
+ if isinstance(child, ForLoopBlock):
+ _collect_uncached_loop_children(child, script_blocks_by_label, blocks_to_update)
+
+
def _extract_blocks_info(blocks: list[BLOCK_YAML_TYPES]) -> list[dict[str, str]]:
"""Extract lightweight info from blocks for title generation (limit to first 5)."""
blocks_info: list[dict[str, str]] = []
@@ -1390,6 +1416,24 @@ class WorkflowService:
if ctx:
ctx.script_mode = True
+ # SKY-8684: Detect empty-block scripts and ensure regeneration.
+ # When a WorkflowScript exists but has zero usable ScriptBlock records,
+ # the run correctly falls through to code_generation mode. However,
+ # generate_script was set to False (in execute_workflow) because the
+ # script exists. Override it to True so per-block generation fires
+ # and post-run finalize can regenerate the script.
+ if script and is_script_run and not script_blocks_by_label:
+ LOG.warning(
+ "Script exists but has zero usable blocks — will regenerate",
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ workflow_run_id=workflow_run_id,
+ script_id=script.script_id,
+ script_revision_id=script.script_revision_id,
+ )
+ regen_ctx = skyvern_context.current()
+ if regen_ctx:
+ regen_ctx.generate_script = True
+
# Single source-of-truth log for how this run will execute.
# Three modes:
# "code" — cached script loaded, executing code
@@ -1418,6 +1462,7 @@ class WorkflowService:
script_id=script.script_id if script else None,
script_revision_id=script.script_revision_id if script else None,
script_block_count=len(script_blocks_by_label),
+ empty_blocks_detected=script is not None and is_script_run and not script_blocks_by_label,
)
if block_labels and len(block_labels):
@@ -2367,28 +2412,24 @@ class WorkflowService:
# Track uncached for-loop child blocks for regeneration.
# ForLoopBlock children execute via block.py's execute_loop_helper(),
- # bypassing _execute_single_block. Without this, their labels never
- # reach blocks_to_update and the script generator never produces
- # cached functions for them (e.g., file_download inside a loop).
+ # bypassing _execute_single_block. Recursively walk all nesting levels
+ # so deeply nested blocks (e.g., file_download inside a double-nested
+ # for-loop) get cached functions generated.
if (
isinstance(block, ForLoopBlock)
and (is_adaptive_caching(workflow, workflow_run) or is_script_run)
and workflow_run_block_result.status in cacheable_statuses
):
- for loop_child in block.loop_blocks:
- if (
- loop_child.label
- and loop_child.label not in script_blocks_by_label
- and loop_child.block_type in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
- ):
- blocks_to_update.add(loop_child.label)
- LOG.info(
- "For-loop child block marked for caching",
- parent_label=block.label,
- child_label=loop_child.label,
- child_block_type=loop_child.block_type,
- workflow_run_id=workflow_run_id,
- )
+ previous_labels = set(blocks_to_update)
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+ new_labels = sorted(blocks_to_update - previous_labels)
+ if new_labels:
+ LOG.info(
+ "For-loop child blocks marked for caching",
+ parent_label=block.label,
+ child_labels=new_labels,
+ workflow_run_id=workflow_run_id,
+ )
workflow_run, should_stop = await self._handle_block_result_status(
block=block,
diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.py
index 3661143a1..722afbeb1 100644
--- a/skyvern/services/workflow_script_service.py
+++ b/skyvern/services/workflow_script_service.py
@@ -10,7 +10,10 @@ from cachetools import TTLCache
from jinja2.sandbox import SandboxedEnvironment
from skyvern.config import settings
-from skyvern.core.script_generations.generate_script import ScriptBlockSource, generate_workflow_script_python_code
+from skyvern.core.script_generations.generate_script import (
+ ScriptBlockSource,
+ generate_workflow_script_python_code,
+)
from skyvern.core.script_generations.transform_workflow_run import transform_workflow_run_to_code_gen_input
from skyvern.forge import app
from skyvern.forge.sdk.core import skyvern_context
@@ -458,7 +461,7 @@ async def generate_workflow_script(
missing_labels=list(missing_labels),
)
- python_src = await generate_workflow_script_python_code(
+ codegen_result = await generate_workflow_script_python_code(
file_name=codegen_input.file_name,
workflow_run_request=codegen_input.workflow_run,
workflow=codegen_input.workflow,
@@ -483,6 +486,10 @@ async def generate_workflow_script(
)
return
+ python_src = codegen_result.source_code
+ blocks_created = codegen_result.blocks_created
+ blocks_failed = codegen_result.blocks_failed
+
generation_duration_ms = (time.monotonic() - generation_start) * 1000
LOG.info(
"Script generation completed",
@@ -491,8 +498,29 @@ async def generate_workflow_script(
cache_key_value=rendered_cache_key_value,
duration_ms=round(generation_duration_ms, 1),
script_size_bytes=len(python_src.encode("utf-8")),
+ blocks_created=blocks_created,
+ blocks_failed=blocks_failed,
)
+ if blocks_failed > 0 and blocks_created > 0:
+ LOG.warning(
+ "Partial script block creation failure — some blocks were not persisted",
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ script_id=script.script_id,
+ blocks_created=blocks_created,
+ blocks_failed=blocks_failed,
+ )
+
+ # Guard: if every block creation failed, do not persist a zero-block script
+ if blocks_created == 0 and blocks_failed > 0:
+ LOG.error(
+ "All script block creations failed — skipping WorkflowScript creation",
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ script_id=script.script_id,
+ blocks_failed=blocks_failed,
+ )
+ return
+
# 3.5) Post-process: fix static actions inside for-loop blocks
python_src = _fix_static_actions_in_for_loops(python_src)
diff --git a/tests/unit/test_conditional_script_caching.py b/tests/unit/test_conditional_script_caching.py
index 7922ab5f5..aff98335f 100644
--- a/tests/unit/test_conditional_script_caching.py
+++ b/tests/unit/test_conditional_script_caching.py
@@ -339,7 +339,7 @@ class TestCachedBlockPreservationDuringRegeneration:
)
# The output should contain branch A's cached code
- assert "branch_a_extract" in result, (
+ assert "branch_a_extract" in result.source_code, (
"Cached block from unexecuted branch A should be preserved in the script output"
)
@@ -403,7 +403,7 @@ class TestCachedBlockPreservationDuringRegeneration:
)
# Incomplete block should NOT appear in the output
- assert "incomplete_block" not in result
+ assert "incomplete_block" not in result.source_code
# create_or_update_script_block should NOT be called for incomplete block
incomplete_calls = [
@@ -626,9 +626,9 @@ class TestCachedBlockPreservationDuringRegeneration:
)
# Both branch A and B should be preserved
- assert "branch_a_extract" in result, "Branch A cached block should be preserved"
- assert "branch_b_navigate" in result, "Branch B cached block should be preserved"
- assert "branch_c_extract" in result, "Branch C (executed) block should be present"
+ assert "branch_a_extract" in result.source_code, "Branch A cached block should be preserved"
+ assert "branch_b_navigate" in result.source_code, "Branch B cached block should be preserved"
+ assert "branch_c_extract" in result.source_code, "Branch C (executed) block should be present"
# Verify DB entries were created for all 3 blocks + __start_block__
all_labels = {call.kwargs.get("block_label") for call in mock_create_block.call_args_list}
@@ -685,7 +685,7 @@ class TestCachedBlockPreservationDuringRegeneration:
)
# Code should still be in the output
- assert "branch_a" in result
+ assert "branch_a" in result.source_code
# But no DB calls should be made for preserved blocks
preserved_calls = [
diff --git a/tests/unit/test_empty_script_blocks.py b/tests/unit/test_empty_script_blocks.py
new file mode 100644
index 000000000..d17d4374e
--- /dev/null
+++ b/tests/unit/test_empty_script_blocks.py
@@ -0,0 +1,167 @@
+"""
+Tests for empty-block script regeneration (SKY-8684).
+
+When a WorkflowScript record exists but has zero ScriptBlock records,
+subsequent runs should detect this and ensure the script gets regenerated.
+Without this fix, the empty-block script persists forever because:
+1. generate_script is set to False (script exists)
+2. Per-block script generation never fires
+3. Post-run finalize may skip regeneration for non-completed runs
+"""
+
+from unittest.mock import MagicMock
+
+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+
+
+class TestEmptyBlockScriptDetection:
+ """Tests for detection and handling of scripts with zero usable blocks."""
+
+ def test_empty_script_blocks_sets_generate_script_true(self) -> None:
+ """When a script exists with zero usable blocks AND is_script_run=True,
+ generate_script should be set to True on the context so regeneration
+ can occur during and after execution.
+
+ This is the core bug fix: previously generate_script stayed False
+ because the script existed, preventing regeneration."""
+ # The fix adds logic after line 1412 in _execute_workflow_blocks:
+ # if script and is_script_run and not script_blocks_by_label:
+ # ctx.generate_script = True
+ ctx = SkyvernContext()
+ ctx.generate_script = False # Simulates line 1072 setting it to False
+
+ script = MagicMock()
+ script.script_id = "scr_test123"
+ script.script_revision_id = "scr_rev_test123"
+ is_script_run = True
+ script_blocks_by_label: dict = {} # Zero usable blocks
+
+ # This is the logic that should exist after line 1412
+ if script and is_script_run and not script_blocks_by_label:
+ ctx.generate_script = True
+
+ assert ctx.generate_script is True, "generate_script should be True when script has zero usable blocks"
+
+ def test_normal_script_with_blocks_keeps_generate_script_false(self) -> None:
+ """Regression test: scripts with usable blocks should NOT override
+ generate_script to True — the existing script handles execution."""
+ ctx = SkyvernContext()
+ ctx.generate_script = False # Set by line 1072
+
+ script = MagicMock()
+ script.script_id = "scr_normal"
+ script.script_revision_id = "scr_rev_normal"
+ is_script_run = True
+ script_blocks_by_label = {
+ "task_1": MagicMock(run_signature="await run_task_1()", requires_agent=False),
+ }
+
+ # Same logic — but should NOT fire because blocks exist
+ if script and is_script_run and not script_blocks_by_label:
+ ctx.generate_script = True
+
+ assert ctx.generate_script is False, "generate_script should stay False when script has usable blocks"
+
+ def test_no_script_keeps_generate_script_default(self) -> None:
+ """When no script exists, generate_script defaults to True
+ (the SkyvernContext default), allowing fresh generation."""
+ ctx = SkyvernContext()
+ # generate_script defaults to True in SkyvernContext
+ assert ctx.generate_script is True
+
+ script = None
+ is_script_run = True
+ script_blocks_by_label: dict = {}
+
+ # Logic should not fire when script is None
+ if script and is_script_run and not script_blocks_by_label:
+ ctx.generate_script = True
+
+ assert ctx.generate_script is True
+
+ def test_non_script_run_does_not_override(self) -> None:
+ """When is_script_run is False (pure agent mode), the empty-block
+ detection should not fire — there's no script to regenerate."""
+ ctx = SkyvernContext()
+ ctx.generate_script = False
+
+ script = MagicMock()
+ is_script_run = False
+ script_blocks_by_label: dict = {}
+
+ if script and is_script_run and not script_blocks_by_label:
+ ctx.generate_script = True
+
+ assert ctx.generate_script is False, "generate_script should not be overridden for non-script runs"
+
+
+class TestEmptyBlockWarningLog:
+ """Tests for the WARNING log emitted when empty-block scripts are detected."""
+
+ def test_warning_log_includes_required_fields(self) -> None:
+ """The warning log for empty-block detection should include
+ workflow_permanent_id, workflow_run_id, script_id, and script_revision_id
+ so we can track this in Datadog."""
+ # Build mock objects
+ script = MagicMock()
+ script.script_id = "scr_empty"
+ script.script_revision_id = "scr_rev_empty"
+
+ workflow = MagicMock()
+ workflow.workflow_permanent_id = "wpid_test"
+
+ workflow_run_id = "wr_test_run_123"
+
+ is_script_run = True
+ script_blocks_by_label: dict = {}
+
+ log_calls: list[dict] = []
+
+ def mock_warning(msg: str, **kwargs: object) -> None:
+ log_calls.append({"msg": msg, "kwargs": kwargs})
+
+ mock_log = MagicMock()
+ mock_log.warning = mock_warning
+
+ # Simulate the detection logic with logging
+ if script and is_script_run and not script_blocks_by_label:
+ mock_log.warning(
+ "Script exists but has zero usable blocks — will regenerate",
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ workflow_run_id=workflow_run_id,
+ script_id=script.script_id,
+ script_revision_id=script.script_revision_id,
+ )
+
+ assert len(log_calls) == 1
+ call = log_calls[0]
+ assert "zero usable blocks" in call["msg"]
+ assert call["kwargs"]["workflow_permanent_id"] == "wpid_test"
+ assert call["kwargs"]["workflow_run_id"] == "wr_test_run_123"
+ assert call["kwargs"]["script_id"] == "scr_empty"
+ assert call["kwargs"]["script_revision_id"] == "scr_rev_empty"
+
+ def test_no_warning_when_blocks_exist(self) -> None:
+ """No warning should be logged when the script has usable blocks."""
+ script = MagicMock()
+ is_script_run = True
+ script_blocks_by_label = {"task_1": MagicMock()}
+
+ log_calls: list[dict] = []
+
+ def mock_warning(msg: str, **kwargs: object) -> None:
+ log_calls.append({"msg": msg, "kwargs": kwargs})
+
+ mock_log = MagicMock()
+ mock_log.warning = mock_warning
+
+ if script and is_script_run and not script_blocks_by_label:
+ mock_log.warning(
+ "Script exists but has zero usable blocks — will regenerate",
+ workflow_permanent_id="wpid_test",
+ workflow_run_id="wr_test",
+ script_id=script.script_id,
+ script_revision_id=script.script_revision_id,
+ )
+
+ assert len(log_calls) == 0, "No warning should be logged when blocks exist"
diff --git a/tests/unit/test_forloop_script_generation.py b/tests/unit/test_forloop_script_generation.py
index bafbb272c..1b4f18ac4 100644
--- a/tests/unit/test_forloop_script_generation.py
+++ b/tests/unit/test_forloop_script_generation.py
@@ -528,13 +528,13 @@ class TestForLoopScriptCompilation:
# The generated code must compile without SyntaxError
# This was the actual bug: 'async for' at module level
try:
- ast.parse(result)
+ ast.parse(result.source_code)
except SyntaxError as e:
- pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")
+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result.source_code}")
# Verify the for-loop is present inside run_workflow (not at module level)
- assert "async for current_value in skyvern.loop" in result
- assert "def run_workflow" in result
+ assert "async for current_value in skyvern.loop" in result.source_code
+ assert "def run_workflow" in result.source_code
class TestForLoopInnerBlockCachedFunctions:
@@ -614,13 +614,13 @@ class TestForLoopInnerBlockCachedFunctions:
# The generated code must compile without errors
try:
- ast.parse(result)
+ ast.parse(result.source_code)
except SyntaxError as e:
- pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")
+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result.source_code}")
# Verify the inner block's @skyvern.cached function is present
- assert "@skyvern.cached" in result
- assert "extract_data" in result
+ assert "@skyvern.cached" in result.source_code
+ assert "extract_data" in result.source_code
# Verify create_or_update_script_block was called for both the for_loop
# and the inner extraction block
@@ -687,12 +687,12 @@ class TestForLoopInnerBlockCachedFunctions:
# Should compile fine
try:
- ast.parse(result)
+ ast.parse(result.source_code)
except SyntaxError as e:
- pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")
+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result.source_code}")
# No @skyvern.cached should be present since inner block had no actions
- assert "@skyvern.cached" not in result
+ assert "@skyvern.cached" not in result.source_code
# create_or_update_script_block should be called for the for_loop only,
# NOT for the inner block
@@ -753,12 +753,12 @@ class TestForLoopInnerBlockCachedFunctions:
)
try:
- ast.parse(result)
+ ast.parse(result.source_code)
except SyntaxError as e:
- pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")
+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result.source_code}")
# No @skyvern.cached should be generated for non-task blocks
- assert "@skyvern.cached" not in result
+ assert "@skyvern.cached" not in result.source_code
@pytest.mark.asyncio
async def test_inner_block_labels_in_processed_labels(self) -> None:
@@ -838,15 +838,15 @@ class TestForLoopInnerBlockCachedFunctions:
# Should compile fine
try:
- ast.parse(result)
+ ast.parse(result.source_code)
except SyntaxError as e:
- pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")
+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result.source_code}")
# The inner block should appear only once in the generated code (not duplicated
# by the "preserve unexecuted branch" section)
- occurrences = result.count("@skyvern.cached")
+ occurrences = result.source_code.count("@skyvern.cached")
# There should be exactly 1 cached function (the inner block), not 2
assert occurrences == 1, (
f"Expected 1 @skyvern.cached occurrence but found {occurrences}. "
- f"Inner block may have been duplicated.\n\nGenerated code:\n{result}"
+ f"Inner block may have been duplicated.\n\nGenerated code:\n{result.source_code}"
)
diff --git a/tests/unit/test_nested_forloop_caching_tracking.py b/tests/unit/test_nested_forloop_caching_tracking.py
new file mode 100644
index 000000000..454575948
--- /dev/null
+++ b/tests/unit/test_nested_forloop_caching_tracking.py
@@ -0,0 +1,638 @@
+"""Tests that nested for-loop child blocks are recursively tracked for caching.
+
+When a ForLoopBlock completes, ALL descendant blocks (not just direct children)
+that are cacheable and not yet cached should be added to blocks_to_update.
+
+This is critical for workflows which have double-nested for-loops:
+ loop → inner_loop → download_files (file_download)
+
+Without recursive tracking, the inner for-loop's children never get cached
+script functions, and every iteration runs the full AI agent loop.
+
+Ref: SKY-8684
+"""
+
+from datetime import datetime, timezone
+
+from skyvern.forge.sdk.workflow.models.block import (
+ ExtractionBlock,
+ FileDownloadBlock,
+ ForLoopBlock,
+ HttpRequestBlock,
+ NavigationBlock,
+ UrlBlock,
+)
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter
+from skyvern.forge.sdk.workflow.service import (
+ _collect_uncached_loop_children,
+)
+
+
+def _make_output_param(label: str) -> OutputParameter:
+ now = datetime.now(tz=timezone.utc)
+ return OutputParameter(
+ key=f"{label}_output",
+ parameter_type="output",
+ output_parameter_id=f"op_{label}",
+ workflow_id="wf_test",
+ created_at=now,
+ modified_at=now,
+ )
+
+
+# ---------------------------------------------------------------------------
+# Part 1: Core recursive collection behavior
+# ---------------------------------------------------------------------------
+class TestCollectUncachedLoopChildren:
+ """Test _collect_uncached_loop_children with various nesting depths."""
+
+ def test_single_level_collects_direct_children(self) -> None:
+ """Single for-loop with cacheable children should collect them all."""
+ download = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download the file",
+ )
+ nav = NavigationBlock(
+ label="navigate",
+ output_parameter=_make_output_param("navigate"),
+ url="http://example.com",
+ navigation_goal="Navigate",
+ )
+ loop = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[nav, download],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(loop, script_blocks_by_label, result)
+
+ assert result == {"navigate", "download_file"}
+
+ def test_double_nested_collects_grandchildren(self) -> None:
+ """Double-nested for-loop should collect children at ALL levels."""
+ download = FileDownloadBlock(
+ label="download_files",
+ output_parameter=_make_output_param("download_files"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ nav = NavigationBlock(
+ label="navigate_to_page",
+ output_parameter=_make_output_param("navigate_to_page"),
+ url="http://example.com",
+ navigation_goal="Navigate",
+ )
+ extract = ExtractionBlock(
+ label="extract_docs",
+ output_parameter=_make_output_param("extract_docs"),
+ data_extraction_goal="Extract documents",
+ )
+ inner_loop = ForLoopBlock(
+ label="inner_loop",
+ output_parameter=_make_output_param("inner_loop"),
+ loop_blocks=[nav, extract, download],
+ )
+ outer_loop = ForLoopBlock(
+ label="outer_loop",
+ output_parameter=_make_output_param("outer_loop"),
+ loop_blocks=[inner_loop],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, result)
+
+ assert result == {"inner_loop", "navigate_to_page", "extract_docs", "download_files"}
+
+ def test_triple_nested_collects_all_descendants(self) -> None:
+ """Three levels of nesting should still collect everything."""
+ deep_download = FileDownloadBlock(
+ label="deep_download",
+ output_parameter=_make_output_param("deep_download"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ inner_inner = ForLoopBlock(
+ label="inner_inner_loop",
+ output_parameter=_make_output_param("inner_inner_loop"),
+ loop_blocks=[deep_download],
+ )
+ inner = ForLoopBlock(
+ label="inner_loop",
+ output_parameter=_make_output_param("inner_loop"),
+ loop_blocks=[inner_inner],
+ )
+ outer = ForLoopBlock(
+ label="outer_loop",
+ output_parameter=_make_output_param("outer_loop"),
+ loop_blocks=[inner],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(outer, script_blocks_by_label, result)
+
+ assert result == {"inner_loop", "inner_inner_loop", "deep_download"}
+
+
+# ---------------------------------------------------------------------------
+# Part 2: Edge cases and guard conditions
+# ---------------------------------------------------------------------------
+class TestCollectUncachedLoopChildrenEdgeCases:
+ """Edge cases: already-cached, non-cacheable types, missing labels."""
+
+ def test_already_cached_children_not_collected(self) -> None:
+ """Children already in script_blocks_by_label should be skipped."""
+ download = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ nav = NavigationBlock(
+ label="navigate",
+ output_parameter=_make_output_param("navigate"),
+ url="http://example.com",
+ navigation_goal="Navigate",
+ )
+ inner_loop = ForLoopBlock(
+ label="inner_loop",
+ output_parameter=_make_output_param("inner_loop"),
+ loop_blocks=[nav, download],
+ )
+ outer_loop = ForLoopBlock(
+ label="outer_loop",
+ output_parameter=_make_output_param("outer_loop"),
+ loop_blocks=[inner_loop],
+ )
+
+ script_blocks_by_label = {"download_file": object(), "inner_loop": object()}
+ result: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, result)
+
+ # Only navigate should be collected
+ assert result == {"navigate"}
+
+ def test_already_cached_forloop_still_recursed_into(self) -> None:
+ """Even if the inner ForLoopBlock is already cached, its children should still be checked.
+
+ The inner for-loop itself may be cached but its children may not be.
+ """
+ download = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ inner_loop = ForLoopBlock(
+ label="inner_loop",
+ output_parameter=_make_output_param("inner_loop"),
+ loop_blocks=[download],
+ )
+ outer_loop = ForLoopBlock(
+ label="outer_loop",
+ output_parameter=_make_output_param("outer_loop"),
+ loop_blocks=[inner_loop],
+ )
+
+ # inner_loop is cached, but download_file is NOT
+ script_blocks_by_label = {"inner_loop": object()}
+ result: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, result)
+
+ # download_file should still be collected even though its parent for-loop is cached
+ assert result == {"download_file"}
+
+ def test_non_cacheable_block_types_skipped(self) -> None:
+ """http_request is NOT in BLOCK_TYPES_THAT_SHOULD_BE_CACHED — should be skipped."""
+ http_block = HttpRequestBlock(
+ label="send_webhook",
+ output_parameter=_make_output_param("send_webhook"),
+ url="http://example.com/webhook",
+ method="POST",
+ )
+ download = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ loop = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[http_block, download],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(loop, script_blocks_by_label, result)
+
+ assert "send_webhook" not in result
+ assert result == {"download_file"}
+
+ def test_children_without_label_skipped(self) -> None:
+ """Blocks with an empty label should be silently skipped (falsy check)."""
+ download = FileDownloadBlock(
+ label="",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ loop = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[download],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(loop, script_blocks_by_label, result)
+
+ assert result == set()
+
+ def test_empty_loop_blocks(self) -> None:
+ """ForLoopBlock with no children should produce no results."""
+ loop = ForLoopBlock(
+ label="empty_loop",
+ output_parameter=_make_output_param("empty_loop"),
+ loop_blocks=[],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ result: set[str] = set()
+ _collect_uncached_loop_children(loop, script_blocks_by_label, result)
+
+ assert result == set()
+
+
+# ---------------------------------------------------------------------------
+# Part 3: Proof tests — recreate real-world workflow structures
+# These prove the fix works for production workflow patterns we identified.
+# ---------------------------------------------------------------------------
+class TestProductionWorkflowProof:
+ """Recreate production workflow block structures from investigation.
+
+ These tests use representative block labels and types to prove
+ the recursive fix handles each workflow correctly. If these pass, we have
+ confidence the fix will work in production.
+ """
+
+ def test_nested_download_loop(self) -> None:
+ """High-volume nested download workflow pattern (SKY-8684).
+
+ Structure:
+ 0: go_to_target_website (navigation)
+ 1: links_extraction (extraction)
+ 2: loop (for_loop)
+ └── inner_loop_starts (for_loop) ← NESTED
+ ├── navigate_to_specific_page (navigation)
+ ├── Extract_documents (extraction)
+ ├── extract_current_url (extraction)
+ └── download_files (file_download)
+ 3: merge_identifiers (extraction)
+ """
+ # Build the inner loop children
+ navigate = NavigationBlock(
+ label="navigate_to_specific_page",
+ output_parameter=_make_output_param("navigate_to_specific_page"),
+ url="{{website_url}}",
+ navigation_goal="Navigate to page",
+ )
+ extract_docs = ExtractionBlock(
+ label="Extract_documents",
+ output_parameter=_make_output_param("Extract_documents"),
+ data_extraction_goal="Extract documents",
+ )
+ extract_url = ExtractionBlock(
+ label="extract_current_url",
+ output_parameter=_make_output_param("extract_current_url"),
+ data_extraction_goal="Extract current URL",
+ )
+ download = FileDownloadBlock(
+ label="download_files",
+ output_parameter=_make_output_param("download_files"),
+ url="{{website_url}}",
+ navigation_goal="Download files",
+ )
+
+ # Build nested for-loops
+ inner_loop = ForLoopBlock(
+ label="inner_loop_starts",
+ output_parameter=_make_output_param("inner_loop_starts"),
+ loop_blocks=[navigate, extract_docs, extract_url, download],
+ )
+ outer_loop = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[inner_loop],
+ )
+
+ # Nothing cached yet (first run)
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, blocks_to_update)
+
+ # ALL 5 blocks should be collected
+ assert blocks_to_update == {
+ "inner_loop_starts",
+ "navigate_to_specific_page",
+ "Extract_documents",
+ "extract_current_url",
+ "download_files",
+ }
+
+ def test_pagination_download_loop(self) -> None:
+ """Pagination + download nested loop pattern (SKY-8684).
+
+ Structure:
+ 3: pagination_loop_starts (for_loop)
+ ├── Extract_documents (extraction)
+ ├── get_current_url (extraction)
+ ├── document_navigation_loop (for_loop) ← NESTED
+ │ ├── navigate_to_document (navigation)
+ │ ├── download (file_download)
+ │ └── go_back (goto_url) ← NOT cacheable
+ └── navigate_table_pagination (navigation)
+ """
+ nav_doc = NavigationBlock(
+ label="navigate_to_document",
+ output_parameter=_make_output_param("navigate_to_document"),
+ url="{{url}}",
+ navigation_goal="Navigate to document",
+ )
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="{{url}}",
+ navigation_goal="Download",
+ )
+ # goto_url (UrlBlock) is NOT in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+ go_back = UrlBlock(
+ label="go_back",
+ output_parameter=_make_output_param("go_back"),
+ url="{{url}}",
+ )
+ inner_loop = ForLoopBlock(
+ label="document_navigation_loop",
+ output_parameter=_make_output_param("document_navigation_loop"),
+ loop_blocks=[nav_doc, download, go_back],
+ )
+
+ extract_docs = ExtractionBlock(
+ label="Extract_documents",
+ output_parameter=_make_output_param("Extract_documents"),
+ data_extraction_goal="Extract documents",
+ )
+ get_url = ExtractionBlock(
+ label="get_current_url",
+ output_parameter=_make_output_param("get_current_url"),
+ data_extraction_goal="Get current URL",
+ )
+ nav_page = NavigationBlock(
+ label="navigate_table_pagination",
+ output_parameter=_make_output_param("navigate_table_pagination"),
+ url="{{url}}",
+ navigation_goal="Next page",
+ )
+ outer_loop = ForLoopBlock(
+ label="pagination_loop_starts",
+ output_parameter=_make_output_param("pagination_loop_starts"),
+ loop_blocks=[extract_docs, get_url, inner_loop, nav_page],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, blocks_to_update)
+
+ # go_back (goto_url) should NOT be collected — not in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+ assert "go_back" not in blocks_to_update
+
+ # All cacheable blocks should be collected
+ assert blocks_to_update == {
+ "Extract_documents",
+ "get_current_url",
+ "document_navigation_loop",
+ "navigate_to_document",
+ "download",
+ "navigate_table_pagination",
+ }
+
+ def test_page_document_loop(self) -> None:
+ """Page-then-document nested loop pattern (SKY-8684).
+
+ Structure:
+ 3: for_each_page (for_loop)
+ ├── Extract_documents (extraction)
+ ├── for_each_document (for_loop) ← NESTED
+ │ ├── download (file_download)
+ │ └── navigate_table_pagination (navigation)
+ """
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="{{url}}",
+ navigation_goal="Download",
+ )
+ nav_page = NavigationBlock(
+ label="navigate_table_pagination",
+ output_parameter=_make_output_param("navigate_table_pagination"),
+ url="{{url}}",
+ navigation_goal="Next page",
+ )
+ inner_loop = ForLoopBlock(
+ label="for_each_document",
+ output_parameter=_make_output_param("for_each_document"),
+ loop_blocks=[download, nav_page],
+ )
+ extract = ExtractionBlock(
+ label="Extract_documents",
+ output_parameter=_make_output_param("Extract_documents"),
+ data_extraction_goal="Extract documents",
+ )
+ outer_loop = ForLoopBlock(
+ label="for_each_page",
+ output_parameter=_make_output_param("for_each_page"),
+ loop_blocks=[extract, inner_loop],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+ _collect_uncached_loop_children(outer_loop, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == {
+ "Extract_documents",
+ "for_each_document",
+ "download",
+ "navigate_table_pagination",
+ }
+
+ def test_single_level_reference(self) -> None:
+ """Single-level nesting reference case — already works, regression guard.
+
+ This already works today. Proves our recursive fix doesn't break
+ the simple case.
+
+ Structure:
+ for_each_file (for_loop)
+ └── download (file_download)
+ """
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="{{url}}",
+ navigation_goal="Download",
+ )
+ loop = ForLoopBlock(
+ label="for_each_file",
+ output_parameter=_make_output_param("for_each_file"),
+ loop_blocks=[download],
+ )
+
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+ _collect_uncached_loop_children(loop, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == {"download"}
+
+
+# ---------------------------------------------------------------------------
+# Part 4: Integration — simulate _execute_single_block tracking conditions
+# ---------------------------------------------------------------------------
+class TestExecuteSingleBlockTrackingConditions:
+ """Simulate the tracking conditions from _execute_single_block.
+
+ These tests recreate the conditional logic from service.py lines ~2347-2390
+ to verify that _collect_uncached_loop_children would be called correctly
+ when integrated. This follows the same pattern as
+ test_script_generation_race_condition.py::TestFinalizeParameter.
+ """
+
+ def test_forloop_with_adaptive_caching_triggers_collection(self) -> None:
+ """When adaptive caching is on and ForLoopBlock completes, children should be collected."""
+ download = FileDownloadBlock(
+ label="download_files",
+ output_parameter=_make_output_param("download_files"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ inner_loop = ForLoopBlock(
+ label="inner_loop",
+ output_parameter=_make_output_param("inner_loop"),
+ loop_blocks=[download],
+ )
+ block = ForLoopBlock(
+ label="outer_loop",
+ output_parameter=_make_output_param("outer_loop"),
+ loop_blocks=[inner_loop],
+ )
+
+ is_adaptive_caching_active = True
+ is_script_run = False
+ block_status_cacheable = True
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+
+ if isinstance(block, ForLoopBlock) and (is_adaptive_caching_active or is_script_run) and block_status_cacheable:
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+
+ assert "inner_loop" in blocks_to_update
+ assert "download_files" in blocks_to_update
+
+ def test_forloop_with_script_run_triggers_collection(self) -> None:
+ """When is_script_run is True, children should be collected."""
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ block = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[download],
+ )
+
+ is_adaptive_caching_active = False
+ is_script_run = True
+ block_status_cacheable = True
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+
+ if isinstance(block, ForLoopBlock) and (is_adaptive_caching_active or is_script_run) and block_status_cacheable:
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == {"download"}
+
+ def test_forloop_without_caching_or_script_does_not_trigger(self) -> None:
+ """When neither adaptive caching nor script run is active, no collection."""
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ block = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[download],
+ )
+
+ is_adaptive_caching_active = False
+ is_script_run = False
+ block_status_cacheable = True
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+
+ if isinstance(block, ForLoopBlock) and (is_adaptive_caching_active or is_script_run) and block_status_cacheable:
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == set()
+
+ def test_forloop_with_non_cacheable_status_does_not_trigger(self) -> None:
+ """When block status is not cacheable (e.g., failed), no collection."""
+ download = FileDownloadBlock(
+ label="download",
+ output_parameter=_make_output_param("download"),
+ url="http://example.com",
+ navigation_goal="Download",
+ )
+ block = ForLoopBlock(
+ label="loop",
+ output_parameter=_make_output_param("loop"),
+ loop_blocks=[download],
+ )
+
+ is_adaptive_caching_active = True
+ is_script_run = True
+ block_status_cacheable = False
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+
+ if isinstance(block, ForLoopBlock) and (is_adaptive_caching_active or is_script_run) and block_status_cacheable:
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == set()
+
+ def test_non_forloop_block_does_not_trigger(self) -> None:
+ """Non-ForLoopBlock blocks should not trigger collection."""
+ block = NavigationBlock(
+ label="navigate",
+ output_parameter=_make_output_param("navigate"),
+ url="http://example.com",
+ navigation_goal="Navigate",
+ )
+
+ is_adaptive_caching_active = True
+ is_script_run = True
+ block_status_cacheable = True
+ script_blocks_by_label: dict[str, object] = {}
+ blocks_to_update: set[str] = set()
+
+ if isinstance(block, ForLoopBlock) and (is_adaptive_caching_active or is_script_run) and block_status_cacheable:
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+
+ assert blocks_to_update == set()
diff --git a/tests/unit/test_script_block_creation.py b/tests/unit/test_script_block_creation.py
new file mode 100644
index 000000000..a95b06ea3
--- /dev/null
+++ b/tests/unit/test_script_block_creation.py
@@ -0,0 +1,233 @@
+"""
+Tests for script block creation error handling (SKY-8684, Task 2).
+
+Verifies that:
+1. create_or_update_script_block returns True on success, False on failure
+2. generate_workflow_script_python_code tracks block creation counts via CodeGenResult
+3. generate_workflow_script skips WorkflowScript creation when all blocks fail
+"""
+
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from skyvern.core.script_generations.generate_script import create_or_update_script_block
+
+
+class TestCreateOrUpdateScriptBlockReturnValue:
+ """Test that create_or_update_script_block returns bool indicating success/failure."""
+
+ @pytest.mark.asyncio
+ async def test_returns_true_on_success(self) -> None:
+ """Regression test: successful block creation returns True."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_123"
+ mock_script_block.script_file_id = None
+
+ mock_script_file = MagicMock()
+ mock_script_file.file_id = "sf_123"
+
+ with (
+ patch("skyvern.core.script_generations.generate_script.app") as mock_app,
+ ):
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=None)
+ mock_app.DATABASE.scripts.create_script_block = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_content_hash = AsyncMock(return_value=None)
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact = AsyncMock(return_value="artifact_123")
+ mock_app.DATABASE.scripts.create_script_file = AsyncMock(return_value=mock_script_file)
+ mock_app.DATABASE.scripts.update_script_block = AsyncMock(return_value=mock_script_block)
+
+ result = await create_or_update_script_block(
+ block_code="async def test(): pass",
+ script_revision_id="rev_123",
+ script_id="script_123",
+ organization_id="org_123",
+ block_label="test_block",
+ )
+
+ assert result is True, "create_or_update_script_block should return True on success"
+
+ @pytest.mark.asyncio
+ async def test_returns_false_on_exception(self) -> None:
+ """When block creation raises an exception, the function returns False."""
+ with (
+ patch("skyvern.core.script_generations.generate_script.app") as mock_app,
+ ):
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(
+ side_effect=RuntimeError("S3 upload timeout")
+ )
+
+ result = await create_or_update_script_block(
+ block_code="async def test(): pass",
+ script_revision_id="rev_123",
+ script_id="script_123",
+ organization_id="org_123",
+ block_label="test_block",
+ )
+
+ assert result is False, "create_or_update_script_block should return False when an exception occurs"
+
+ @pytest.mark.asyncio
+ async def test_returns_false_on_artifact_upload_failure(self) -> None:
+ """Specifically test S3 artifact upload failure scenario."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_123"
+ mock_script_block.script_file_id = None
+
+ with (
+ patch("skyvern.core.script_generations.generate_script.app") as mock_app,
+ ):
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=None)
+ mock_app.DATABASE.scripts.create_script_block = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_content_hash = AsyncMock(return_value=None)
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact = AsyncMock(
+ side_effect=TimeoutError("S3 upload timeout")
+ )
+
+ result = await create_or_update_script_block(
+ block_code="async def test(): pass",
+ script_revision_id="rev_123",
+ script_id="script_123",
+ organization_id="org_123",
+ block_label="test_block",
+ )
+
+ assert result is False, "create_or_update_script_block should return False on artifact upload failure"
+
+
+class TestBlockCreationCountTracking:
+ """Test that generate_workflow_script_python_code returns block creation counts."""
+
+ @pytest.mark.asyncio
+ async def test_returns_codegen_result_with_counts(self) -> None:
+ """generate_workflow_script_python_code returns a CodeGenResult with source and counts."""
+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code
+
+ workflow = {
+ "workflow_id": "wpid_test",
+ "workflow_definition": {"parameters": []},
+ }
+ blocks = [
+ {
+ "block_type": "task",
+ "label": "test_task",
+ "task_id": "t_123",
+ "title": "Test Task",
+ "url": "https://example.com",
+ "navigation_goal": "Do something",
+ "workflow_run_block_id": "wrb_123",
+ },
+ ]
+ actions_by_task = {
+ "t_123": [
+ {
+ "action_type": "click",
+ "element_id": "e_123",
+ "xpath": "//button[@type='submit']",
+ "reasoning": "Click the button",
+ "skyvern_element_data": {
+ "id": "e_123",
+ "tag_name": "button",
+ "attributes": {"type": "submit"},
+ "text": "Submit",
+ "option_index": None,
+ "children": [],
+ },
+ }
+ ],
+ }
+
+ with (
+ patch(
+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",
+ new_callable=AsyncMock,
+ return_value=("", {}),
+ ),
+ patch(
+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",
+ new_callable=AsyncMock,
+ return_value=True,
+ ),
+ ):
+ result = await generate_workflow_script_python_code(
+ file_name="test.py",
+ workflow_run_request={"workflow_id": "wpid_test"},
+ workflow=workflow,
+ blocks=blocks,
+ actions_by_task=actions_by_task,
+ script_id="script_123",
+ script_revision_id="rev_123",
+ organization_id="org_123",
+ )
+
+ # Result should have source_code, blocks_created, blocks_failed attributes
+ assert hasattr(result, "source_code"), "Result should have source_code attribute"
+ assert hasattr(result, "blocks_created"), "Result should have blocks_created attribute"
+ assert hasattr(result, "blocks_failed"), "Result should have blocks_failed attribute"
+ assert isinstance(result.source_code, str), "source_code should be a string"
+ assert result.blocks_created > 0, "At least one block should be created"
+ assert result.blocks_failed == 0, "No blocks should fail"
+
+ @pytest.mark.asyncio
+ async def test_counts_failures_when_block_creation_fails(self) -> None:
+ """When create_or_update_script_block returns False, blocks_failed increments."""
+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code
+
+ workflow = {
+ "workflow_id": "wpid_test",
+ "workflow_definition": {"parameters": []},
+ }
+ blocks = [
+ {
+ "block_type": "task",
+ "label": "failing_task",
+ "task_id": "t_fail",
+ "title": "Failing Task",
+ "url": "https://example.com",
+ "navigation_goal": "Do something",
+ "workflow_run_block_id": "wrb_fail",
+ },
+ ]
+ actions_by_task = {
+ "t_fail": [
+ {
+ "action_type": "click",
+ "element_id": "e_123",
+ "xpath": "//button[@type='submit']",
+ "reasoning": "Click the button",
+ "skyvern_element_data": {
+ "id": "e_123",
+ "tag_name": "button",
+ "attributes": {"type": "submit"},
+ "text": "Submit",
+ "option_index": None,
+ "children": [],
+ },
+ }
+ ],
+ }
+
+ with (
+ patch(
+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",
+ new_callable=AsyncMock,
+ return_value=("", {}),
+ ),
+ patch(
+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",
+ new_callable=AsyncMock,
+ return_value=False,
+ ),
+ ):
+ result = await generate_workflow_script_python_code(
+ file_name="test.py",
+ workflow_run_request={"workflow_id": "wpid_test"},
+ workflow=workflow,
+ blocks=blocks,
+ actions_by_task=actions_by_task,
+ script_id="script_123",
+ script_revision_id="rev_123",
+ organization_id="org_123",
+ )
+
+ assert result.blocks_failed > 0, "blocks_failed should be > 0 when block creation fails"
diff --git a/tests/unit/test_script_block_dedup.py b/tests/unit/test_script_block_dedup.py
new file mode 100644
index 000000000..814de607b
--- /dev/null
+++ b/tests/unit/test_script_block_dedup.py
@@ -0,0 +1,192 @@
+"""
+Tests for script block content deduplication (SKY-8684).
+
+Verifies that:
+1. create_or_update_script_block skips S3 upload when content matches (dedup hit)
+2. create_or_update_script_block uploads to S3 when content differs (dedup miss)
+3. create_or_update_script_block uploads to S3 when no previous block exists
+4. Edge case: matching hash but no artifact_id falls through to full upload
+"""
+
+import hashlib
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from skyvern.core.script_generations.generate_script import create_or_update_script_block
+
+
+class TestScriptBlockDedup:
+ """Test content deduplication in create_or_update_script_block."""
+
+ @pytest.mark.asyncio
+ async def test_create_new_block_no_previous_file_uploads_to_s3(self) -> None:
+ """When no ScriptFile with matching content_hash exists, full S3 upload occurs."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_new"
+ mock_script_block.script_file_id = None
+
+ mock_script_file = MagicMock()
+ mock_script_file.file_id = "sf_new"
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=None)
+ mock_app.DATABASE.scripts.create_script_block = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_content_hash = AsyncMock(return_value=None)
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact = AsyncMock(return_value="artifact_new")
+ mock_app.DATABASE.scripts.create_script_file = AsyncMock(return_value=mock_script_file)
+ mock_app.DATABASE.scripts.update_script_block = AsyncMock(return_value=mock_script_block)
+
+ result = await create_or_update_script_block(
+ block_code="async def task_1(): pass",
+ script_revision_id="rev_new",
+ script_id="script_1",
+ organization_id="org_1",
+ block_label="task_1",
+ )
+
+ assert result is True
+ # S3 upload SHOULD happen — no dedup match
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact.assert_called_once()
+ mock_app.DATABASE.scripts.create_script_file.assert_called_once()
+ # Verify content_hash was passed to create_script_file
+ call_kwargs = mock_app.DATABASE.scripts.create_script_file.call_args.kwargs
+ assert call_kwargs["content_hash"].startswith("sha256:")
+
+ @pytest.mark.asyncio
+ async def test_create_new_block_matching_hash_skips_s3_upload(self) -> None:
+ """When a ScriptFile with matching content_hash exists, S3 upload is skipped (cross-revision dedup)."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_dedup"
+ mock_script_block.script_file_id = None
+
+ existing_script_file = MagicMock()
+ existing_script_file.artifact_id = "artifact_existing"
+
+ new_script_file = MagicMock()
+ new_script_file.file_id = "sf_dedup"
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=None)
+ mock_app.DATABASE.scripts.create_script_block = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_content_hash = AsyncMock(return_value=existing_script_file)
+ mock_app.DATABASE.scripts.create_script_file = AsyncMock(return_value=new_script_file)
+ mock_app.DATABASE.scripts.update_script_block = AsyncMock(return_value=mock_script_block)
+
+ result = await create_or_update_script_block(
+ block_code="async def task_1(): pass",
+ script_revision_id="rev_new",
+ script_id="script_1",
+ organization_id="org_1",
+ block_label="task_1",
+ )
+
+ assert result is True
+ # S3 upload should NOT happen — dedup hit
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact.assert_not_called()
+ # ScriptFile should still be created (for the new revision) with reused artifact_id
+ mock_app.DATABASE.scripts.create_script_file.assert_called_once()
+ call_kwargs = mock_app.DATABASE.scripts.create_script_file.call_args.kwargs
+ assert call_kwargs["artifact_id"] == "artifact_existing"
+
+ @pytest.mark.asyncio
+ async def test_update_existing_block_same_content_skips_s3(self) -> None:
+ """When updating a block and content_hash matches, S3 upload is skipped entirely."""
+ block_code = "async def task_1(): pass"
+ content_hash = f"sha256:{hashlib.sha256(block_code.encode('utf-8')).hexdigest()}"
+
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_existing"
+ mock_script_block.script_file_id = "sf_existing"
+
+ mock_script_file = MagicMock()
+ mock_script_file.content_hash = content_hash
+ mock_script_file.artifact_id = "artifact_existing"
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_id = AsyncMock(return_value=mock_script_file)
+
+ result = await create_or_update_script_block(
+ block_code=block_code,
+ script_revision_id="rev_1",
+ script_id="script_1",
+ organization_id="org_1",
+ block_label="task_1",
+ update=True,
+ )
+
+ assert result is True
+ # S3 upload should NOT happen — content unchanged
+ mock_app.STORAGE.store_artifact.assert_not_called()
+ mock_app.DATABASE.artifacts.get_artifact_by_id.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_update_existing_block_different_content_uploads_to_s3(self) -> None:
+ """When updating a block and content_hash differs, S3 upload proceeds."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_existing"
+ mock_script_block.script_file_id = "sf_existing"
+
+ mock_script_file = MagicMock()
+ mock_script_file.content_hash = "sha256:old_hash_value"
+ mock_script_file.artifact_id = "artifact_existing"
+
+ mock_artifact = MagicMock()
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_id = AsyncMock(return_value=mock_script_file)
+ mock_app.DATABASE.artifacts.get_artifact_by_id = AsyncMock(return_value=mock_artifact)
+ mock_app.DATABASE.scripts.update_script_file = AsyncMock()
+ mock_app.STORAGE.store_artifact = AsyncMock()
+
+ result = await create_or_update_script_block(
+ block_code="async def task_1_v2(): pass",
+ script_revision_id="rev_1",
+ script_id="script_1",
+ organization_id="org_1",
+ block_label="task_1",
+ update=True,
+ )
+
+ assert result is True
+ # S3 upload SHOULD happen — content changed
+ mock_app.DATABASE.artifacts.get_artifact_by_id.assert_called_once()
+ mock_app.STORAGE.store_artifact.assert_called_once()
+ # content_hash should be updated on the ScriptFile record AFTER S3 upload
+ mock_app.DATABASE.scripts.update_script_file.assert_called_once()
+ assert "content_hash" in mock_app.DATABASE.scripts.update_script_file.call_args.kwargs
+
+ @pytest.mark.asyncio
+ async def test_create_block_matching_hash_but_no_artifact_falls_through(self) -> None:
+ """When ScriptFile has matching hash but artifact_id is None, fall through to full upload."""
+ mock_script_block = MagicMock()
+ mock_script_block.script_block_id = "sb_edge"
+ mock_script_block.script_file_id = None
+
+ orphan_script_file = MagicMock()
+ orphan_script_file.artifact_id = None # No artifact — can't reuse
+
+ new_script_file = MagicMock()
+ new_script_file.file_id = "sf_edge"
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.DATABASE.scripts.get_script_block_by_label = AsyncMock(return_value=None)
+ mock_app.DATABASE.scripts.create_script_block = AsyncMock(return_value=mock_script_block)
+ mock_app.DATABASE.scripts.get_script_file_by_content_hash = AsyncMock(return_value=orphan_script_file)
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact = AsyncMock(return_value="artifact_new")
+ mock_app.DATABASE.scripts.create_script_file = AsyncMock(return_value=new_script_file)
+ mock_app.DATABASE.scripts.update_script_block = AsyncMock(return_value=mock_script_block)
+
+ result = await create_or_update_script_block(
+ block_code="async def task_edge(): pass",
+ script_revision_id="rev_edge",
+ script_id="script_1",
+ organization_id="org_1",
+ block_label="task_edge",
+ )
+
+ assert result is True
+ # Should fall through to full upload since artifact_id is None
+ mock_app.ARTIFACT_MANAGER.create_script_file_artifact.assert_called_once()