Skip to content

Automate browser based workflows with AI

Fix: cache file_download blocks inside for-loops (#5323)

pedrohsdbe1fdae2a8da5
diff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.py
index 772a98572..caaec8a71 100644
--- a/skyvern/core/script_generations/skyvern_page.py
+++ b/skyvern/core/script_generations/skyvern_page.py
@@ -3444,25 +3444,40 @@ class RunContext:
def download_selector(self) -> str | None:
"""Build a CSS selector targeting a download link from the current loop value.
- Scans the loop_value dict for URL-like strings, extracts the filename,
- and returns a selector like ``a[href*="filename.pdf"]``. Returns None
- if no URL is found.
+ Tries strategies in order of reliability:
+ 1. URL in values → a[href*="filename.pdf"] (most precise)
+ 2. Title text → a:has-text("title") (works when title IS the link)
+
+ Returns the first viable selector, or None to fall back to AI.
"""
value = self.loop_value
if not value or not isinstance(value, dict):
return None
+
+ texts: list[str] = []
for v in value.values():
- if not isinstance(v, str):
+ if not isinstance(v, str) or not v.strip():
continue
- # Match URL-like strings (http/https or paths with file extensions)
+
+ # Strategy 1: URL-like values → href selector (most reliable)
if re.match(r"https?://", v) or re.match(r"/.*\.\w+", v):
- # Extract filename from URL path
- filename = v.rstrip("/").rsplit("/", 1)[-1]
- # Strip query params
- filename = filename.split("?")[0]
+ filename = v.rstrip("/").rsplit("/", 1)[-1].split("?")[0]
if filename and "." in filename:
- # Escape CSS special characters to avoid malformed selectors
filename = re.sub(r'["\[\]\\]', "", filename)
if filename:
return f'a[href*="{filename}"]'
+
+ texts.append(v.strip())
+
+ if not texts:
+ return None
+
+ # Strategy 2: Direct link text match — many sites make the document
+ # title clickable (e.g., <a href="...">Annual Report 2025</a>).
+ # Use the longest text (likely the title, which is more often the link text).
+ longest = max(texts, key=len)
+ escaped = longest.replace('"', '\\"')
+ if len(escaped) >= 3:
+ return f'a:has-text("{escaped}")'
+
return None
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 970d1052d..749971426 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -2377,6 +2377,31 @@ class WorkflowService:
workflow_run_id=workflow_run_id,
)
+ # 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).
+ 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,
+ )
+
workflow_run, should_stop = await self._handle_block_result_status(
block=block,
block_idx=block_idx,
diff --git a/tests/unit/test_forloop_child_caching_tracking.py b/tests/unit/test_forloop_child_caching_tracking.py
new file mode 100644
index 000000000..df811c2f1
--- /dev/null
+++ b/tests/unit/test_forloop_child_caching_tracking.py
@@ -0,0 +1,158 @@
+"""Tests that for-loop child blocks are tracked in blocks_to_update.
+
+When a ForLoopBlock completes and its child blocks (e.g., file_download)
+are not yet in script_blocks_by_label, they should be added to
+blocks_to_update so the script generator produces cached functions for them.
+
+This was the root cause of file_download blocks inside for-loops never
+getting cached: they executed via block.py's execute_loop_helper() which
+bypasses the blocks_to_update tracking in service.py's _execute_single_block().
+"""
+
+from datetime import datetime, timezone
+
+from skyvern.forge.sdk.workflow.models.block import (
+ BlockType,
+ FileDownloadBlock,
+ ForLoopBlock,
+)
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter
+from skyvern.forge.sdk.workflow.service import BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+
+
+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,
+ )
+
+
+def test_file_download_is_cacheable():
+ """FILE_DOWNLOAD must be in BLOCK_TYPES_THAT_SHOULD_BE_CACHED."""
+ assert BlockType.FILE_DOWNLOAD in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+
+
+def test_forloop_child_labels_can_be_collected():
+ """Verify we can iterate ForLoopBlock.loop_blocks to find uncached children."""
+ download_block = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download the file",
+ )
+ loop_block = ForLoopBlock(
+ label="download_loop",
+ output_parameter=_make_output_param("download_loop"),
+ loop_blocks=[download_block],
+ )
+
+ # Simulate the tracking logic from service.py
+ script_blocks_by_label: dict[str, object] = {} # empty = nothing cached
+ blocks_to_update: set[str] = set()
+
+ for loop_child in loop_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)
+
+ assert "download_file" in blocks_to_update
+
+
+def test_forloop_child_already_cached_not_tracked():
+ """If the child block is already cached, it should NOT be added to blocks_to_update."""
+ download_block = FileDownloadBlock(
+ label="download_file",
+ output_parameter=_make_output_param("download_file"),
+ url="http://example.com",
+ navigation_goal="Download the file",
+ )
+ loop_block = ForLoopBlock(
+ label="download_loop",
+ output_parameter=_make_output_param("download_loop"),
+ loop_blocks=[download_block],
+ )
+
+ # Simulate: download_file is already in script_blocks_by_label
+ script_blocks_by_label = {"download_file": object()}
+ blocks_to_update: set[str] = set()
+
+ for loop_child in loop_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)
+
+ assert "download_file" not in blocks_to_update
+
+
+class TestDownloadSelectorCascade:
+ """Test the download_selector cascade strategies."""
+
+ def test_url_value_returns_href_selector(self):
+ """URL values should produce a[href*=filename] selector."""
+ from unittest.mock import patch
+
+ from skyvern.core.script_generations.skyvern_page import RunContext
+
+ rc = RunContext.__new__(RunContext)
+ loop_val = {"title": "Report", "url": "https://example.com/files/report-2025.pdf"}
+ with patch.object(type(rc), "loop_value", new_callable=lambda: property(lambda self: loop_val)):
+ result = rc.download_selector()
+ assert result == 'a[href*="report-2025.pdf"]'
+
+ def test_text_value_returns_has_text_selector(self):
+ """Non-URL text values should produce a:has-text selector with the longest text."""
+ from unittest.mock import patch
+
+ from skyvern.core.script_generations.skyvern_page import RunContext
+
+ rc = RunContext.__new__(RunContext)
+ loop_val = {"title": "Annual Report 2025", "unique_identifier": "DOC-2025-001"}
+ with patch.object(type(rc), "loop_value", new_callable=lambda: property(lambda self: loop_val)):
+ result = rc.download_selector()
+ assert result == 'a:has-text("Annual Report 2025")'
+
+ def test_no_value_returns_none(self):
+ """Empty or None loop_value should return None."""
+ from unittest.mock import patch
+
+ from skyvern.core.script_generations.skyvern_page import RunContext
+
+ rc = RunContext.__new__(RunContext)
+ with patch.object(type(rc), "loop_value", new_callable=lambda: property(lambda self: None)):
+ result = rc.download_selector()
+ assert result is None
+
+ def test_short_text_skipped(self):
+ """Very short text values (< 3 chars) should be skipped."""
+ from unittest.mock import patch
+
+ from skyvern.core.script_generations.skyvern_page import RunContext
+
+ rc = RunContext.__new__(RunContext)
+ loop_val = {"id": "AB"}
+ with patch.object(type(rc), "loop_value", new_callable=lambda: property(lambda self: loop_val)):
+ result = rc.download_selector()
+ assert result is None
+
+ def test_url_takes_priority_over_text(self):
+ """When both URL and text values exist, URL-based selector wins."""
+ from unittest.mock import patch
+
+ from skyvern.core.script_generations.skyvern_page import RunContext
+
+ rc = RunContext.__new__(RunContext)
+ loop_val = {"title": "Annual Report", "download_url": "https://example.com/report.pdf"}
+ with patch.object(type(rc), "loop_value", new_callable=lambda: property(lambda self: loop_val)):
+ result = rc.download_selector()
+ assert result == 'a[href*="report.pdf"]'