Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(sky-8861): refresh block output downloaded_files at API time (#5673)
Shuchang Zheng56483c3be439
diff --git a/skyvern/forge/sdk/artifact/storage/base.py b/skyvern/forge/sdk/artifact/storage/base.pyindex 4bbded38a..16ac807c0 100644--- a/skyvern/forge/sdk/artifact/storage/base.py+++ b/skyvern/forge/sdk/artifact/storage/base.py@@ -31,6 +31,7 @@ def _file_infos_from_download_artifacts(artifacts: list[Artifact]) -> list[FileIchecksum=artifact.checksum,filename=filename,modified_at=artifact.created_at,+ artifact_id=artifact.artifact_id,))return infosdiff --git a/skyvern/forge/sdk/schemas/files.py b/skyvern/forge/sdk/schemas/files.pyindex eaf96946d..9877da836 100644--- a/skyvern/forge/sdk/schemas/files.py+++ b/skyvern/forge/sdk/schemas/files.py@@ -10,3 +10,8 @@ class FileInfo(BaseModel):checksum: str | None = Field(None, description="SHA-256 checksum of the file")filename: str | None = Field(None, description="Original filename")modified_at: datetime | None = Field(None, description="Modified time of the file")+ # Optional: when the FileInfo is built from a DOWNLOAD Artifact row, the+ # row's id is carried through so persisted snapshots (e.g. block outputs)+ # can rebuild fresh signed URLs at API-fetch time even if the snapshot's+ # ``url`` was minted before the artifact-first read existed.+ artifact_id: str | None = Field(None, description="Artifact row id for refresh-on-read")diff --git a/skyvern/forge/sdk/schemas/tasks.py b/skyvern/forge/sdk/schemas/tasks.pyindex a3009bc95..3e7d62dbe 100644--- a/skyvern/forge/sdk/schemas/tasks.py+++ b/skyvern/forge/sdk/schemas/tasks.py@@ -415,6 +415,11 @@ class TaskOutput(BaseModel):failure_category: list[dict[str, Any]] | None = Nonedownloaded_files: list[FileInfo] | None = Nonedownloaded_file_urls: list[str] | None = None # For backward compatibility+ # Persisted artifact ids for refresh-on-read (mirrors task_screenshot_artifact_ids).+ # When set, the workflow-run-status response rebuilds ``downloaded_files`` and+ # ``downloaded_file_urls`` from these IDs every API fetch so a presigned URL+ # captured at execution time never makes it to the client.+ downloaded_file_artifact_ids: list[str] | None = Nonetask_screenshots: list[str] | None = Noneworkflow_screenshots: list[str] | None = Nonetask_screenshot_artifact_ids: list[str] | None = None@@ -429,6 +434,10 @@ class TaskOutput(BaseModel):) -> TaskOutput:# For backward compatibility, extract just the URLs from FileInfo objectsdownloaded_file_urls = [file_info.url for file_info in downloaded_files] if downloaded_files else None+ # Carry artifact ids through so the API can rebuild fresh signed URLs.+ downloaded_file_artifact_ids = (+ [fi.artifact_id for fi in downloaded_files if fi.artifact_id] if downloaded_files else None+ ) or Nonereturn TaskOutput(task_id=task.task_id,@@ -439,6 +448,7 @@ class TaskOutput(BaseModel):failure_category=task.failure_category,downloaded_files=downloaded_files,downloaded_file_urls=downloaded_file_urls,+ downloaded_file_artifact_ids=downloaded_file_artifact_ids,task_screenshot_artifact_ids=task_screenshot_artifact_ids,workflow_screenshot_artifact_ids=workflow_screenshot_artifact_ids,)diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex ddd8d7395..1b7afc307 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -54,6 +54,7 @@ from skyvern.forge import appfrom skyvern.forge.failure_classifier import classify_from_failure_reasonfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.artifact.models import Artifact, ArtifactType+from skyvern.forge.sdk.artifact.storage.base import _file_infos_from_download_artifactsfrom skyvern.forge.sdk.cache import extraction_cachefrom skyvern.forge.sdk.cache.factory import CacheFactoryfrom skyvern.forge.sdk.core import skyvern_context@@ -514,6 +515,28 @@ class WorkflowService:urls = await app.ARTIFACT_MANAGER.get_share_links_with_bundle_support(artifacts)return [u for u in urls if u is not None]+ async def _file_infos_from_download_artifact_ids(+ self,+ artifact_ids: list[str],+ organization_id: str | None,+ ) -> list[FileInfo]:+ """Rebuild ``FileInfo`` objects from DOWNLOAD artifact IDs.++ Used to refresh persisted block-output ``downloaded_files`` snapshots:+ the URL captured at execution time may be a legacy presigned S3 URL,+ but the artifact row has everything we need to mint a fresh signed+ ``/v1/artifacts/{id}/content`` URL on each API fetch.+ """+ if not artifact_ids or not organization_id:+ return []+ artifacts = await app.DATABASE.artifacts.get_artifacts_by_ids(artifact_ids, organization_id)+ if not artifacts:+ return []+ # Preserve the input order so block outputs render files in save order.+ by_id = {a.artifact_id: a for a in artifacts}+ ordered = [by_id[aid] for aid in artifact_ids if aid in by_id]+ return _file_infos_from_download_artifacts(ordered)+async def _refresh_output_screenshot_urls(self,value: Any,@@ -529,6 +552,11 @@ class WorkflowService:For backwards compatibility with old data that stored URLs directly (now expired),we also check for task_id and regenerate URLs using the task_id lookup.++ Also refreshes ``downloaded_files`` / ``downloaded_file_urls`` from+ ``downloaded_file_artifact_ids`` so block outputs return short signed+ ``/v1/artifacts/{id}/content`` URLs even when the URL captured at+ execution time was a legacy presigned S3 URL."""if isinstance(value, dict):# Check if this looks like a TaskOutput with screenshot artifact IDs (new format)@@ -548,6 +576,14 @@ class WorkflowService:value["workflow_screenshot_artifact_ids"],organization_id,)+ if value.get("downloaded_file_artifact_ids"):+ refreshed = await self._file_infos_from_download_artifact_ids(+ value["downloaded_file_artifact_ids"],+ organization_id,+ )+ if refreshed:+ value["downloaded_files"] = [fi.model_dump(mode="json") for fi in refreshed]+ value["downloaded_file_urls"] = [fi.url for fi in refreshed]elif has_old_format:# Old format (backwards compat): regenerate URLs using task_id lookuptask_id = value.get("task_id")diff --git a/tests/unit/test_block_output_download_refresh.py b/tests/unit/test_block_output_download_refresh.pynew file mode 100644index 000000000..c066001f5--- /dev/null+++ b/tests/unit/test_block_output_download_refresh.py@@ -0,0 +1,236 @@+"""Tests for refresh-on-read of block output downloaded_files (SKY-8861 follow-up).++When a task block completes, ``TaskOutput.from_task`` snapshots the current+``downloaded_files`` URL list into the persisted block output. If the URL+captured at execution time was a legacy presigned S3 URL (because the+artifact row didn't exist yet, or the run pre-dates SKY-8861), the API+fetch would otherwise serve that stale URL.++This change persists ``downloaded_file_artifact_ids`` alongside the URLs+and rebuilds ``downloaded_files`` / ``downloaded_file_urls`` from those+IDs on every workflow-run-status response.+"""++from __future__ import annotations++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType+from skyvern.forge.sdk.schemas.files import FileInfo+++def _make_artifact(artifact_id: str, uri: str, checksum: str | None = "sha-x") -> Artifact:+ return Artifact(+ artifact_id=artifact_id,+ artifact_type=ArtifactType.DOWNLOAD,+ uri=uri,+ organization_id="o_1",+ run_id="wr_1",+ checksum=checksum,+ created_at="2026-04-25T00:00:00Z",+ modified_at="2026-04-25T00:00:00Z",+ )+++def test_task_output_carries_artifact_ids_through():+ """``TaskOutput.from_task`` extracts artifact_ids from the FileInfo list so+ they survive into the persisted block output."""+ from skyvern.forge.sdk.schemas.tasks import TaskOutput, TaskStatus++ task = MagicMock()+ task.task_id = "tsk_1"+ task.status = TaskStatus.completed+ task.extracted_information = None+ task.failure_reason = None+ task.errors = []+ task.failure_category = None++ files = [+ FileInfo(+ url="https://api.skyvern.com/v1/artifacts/a_1/content?sig=x",+ checksum="sha-1",+ filename="invoice.pdf",+ artifact_id="a_1",+ ),+ FileInfo(+ url="https://api.skyvern.com/v1/artifacts/a_2/content?sig=y",+ checksum="sha-2",+ filename="report.pdf",+ artifact_id="a_2",+ ),+ ]+ output = TaskOutput.from_task(task, downloaded_files=files)++ assert output.downloaded_file_artifact_ids == ["a_1", "a_2"]+ assert output.downloaded_file_urls == [+ "https://api.skyvern.com/v1/artifacts/a_1/content?sig=x",+ "https://api.skyvern.com/v1/artifacts/a_2/content?sig=y",+ ]+++def test_task_output_artifact_ids_none_when_files_lack_artifact_id():+ """Legacy FileInfo without artifact_id (older code path or non-DB path) leaves+ ``downloaded_file_artifact_ids`` None — refresh-on-read becomes a no-op for+ these snapshots and the stored URLs are served as-is."""+ from skyvern.forge.sdk.schemas.tasks import TaskOutput, TaskStatus++ task = MagicMock()+ task.task_id = "tsk_1"+ task.status = TaskStatus.completed+ task.extracted_information = None+ task.failure_reason = None+ task.errors = []+ task.failure_category = None++ files = [+ FileInfo(+ url="https://skyvern-uploads.s3.amazonaws.com/.../legacy.pdf?sig=x",+ checksum="sha-1",+ filename="legacy.pdf",+ artifact_id=None,+ ),+ ]+ output = TaskOutput.from_task(task, downloaded_files=files)++ assert output.downloaded_file_artifact_ids is None+ # URL list still populated for backward compat consumers.+ assert output.downloaded_file_urls == [+ "https://skyvern-uploads.s3.amazonaws.com/.../legacy.pdf?sig=x",+ ]+++async def test_refresh_rebuilds_downloaded_files_from_artifact_ids():+ """The refresh walker rebuilds ``downloaded_files`` and ``downloaded_file_urls``+ from ``downloaded_file_artifact_ids`` so a presigned-URL snapshot becomes a+ short signed artifact URL on every API fetch."""+ from skyvern.forge.sdk.workflow.service import WorkflowService++ persisted_block_output = {+ "task_id": "tsk_1",+ "task_screenshot_artifact_ids": [],+ "workflow_screenshot_artifact_ids": [],+ "downloaded_file_artifact_ids": ["a_1"],+ "downloaded_files": [+ {+ "url": "https://skyvern-uploads.s3.amazonaws.com/.../stale.pdf?sig=expired",+ "checksum": "sha-1",+ "filename": "stale.pdf",+ "modified_at": "2026-04-23T00:00:00Z",+ }+ ],+ "downloaded_file_urls": [+ "https://skyvern-uploads.s3.amazonaws.com/.../stale.pdf?sig=expired",+ ],+ }++ fresh_artifact = _make_artifact("a_1", "s3://skyvern-uploads/downloads/local/o_1/wr_1/stale.pdf", checksum="sha-1")+ fresh_file_info = FileInfo(+ url="https://api.skyvern.com/v1/artifacts/a_1/content?expiry=fresh&kid=k&sig=s",+ checksum="sha-1",+ filename="stale.pdf",+ modified_at=fresh_artifact.created_at,+ artifact_id="a_1",+ )++ with (+ patch(+ "skyvern.forge.sdk.workflow.service.app.DATABASE.artifacts.get_artifacts_by_ids",+ new=AsyncMock(return_value=[fresh_artifact]),+ ),+ patch(+ "skyvern.forge.sdk.workflow.service._file_infos_from_download_artifacts",+ return_value=[fresh_file_info],+ ),+ ):+ service = WorkflowService()+ refreshed = await service._refresh_output_screenshot_urls(+ persisted_block_output, organization_id="o_1", workflow_run_id="wr_1"+ )++ assert refreshed["downloaded_files"][0]["url"].startswith("https://api.skyvern.com/v1/artifacts/a_1/content")+ assert refreshed["downloaded_file_urls"] == [+ "https://api.skyvern.com/v1/artifacts/a_1/content?expiry=fresh&kid=k&sig=s"+ ]+ # checksum from the artifact row is preserved.+ assert refreshed["downloaded_files"][0]["checksum"] == "sha-1"+++async def test_refresh_leaves_legacy_outputs_untouched():+ """Block outputs persisted before this change have no+ ``downloaded_file_artifact_ids`` field. Refresh must not invent rows or+ blow them away — leave the legacy URLs in place so they keep working+ until they expire."""+ from skyvern.forge.sdk.workflow.service import WorkflowService++ persisted_block_output = {+ "task_id": "tsk_1",+ "task_screenshot_artifact_ids": [],+ "workflow_screenshot_artifact_ids": [],+ # No downloaded_file_artifact_ids field — legacy snapshot.+ "downloaded_files": [+ {+ "url": "https://skyvern-uploads.s3.amazonaws.com/.../legacy.pdf?sig=x",+ "checksum": "sha-1",+ "filename": "legacy.pdf",+ "modified_at": None,+ }+ ],+ "downloaded_file_urls": [+ "https://skyvern-uploads.s3.amazonaws.com/.../legacy.pdf?sig=x",+ ],+ }++ mock_get = AsyncMock() # must NOT be called++ with patch(+ "skyvern.forge.sdk.workflow.service.app.DATABASE.artifacts.get_artifacts_by_ids",+ mock_get,+ ):+ service = WorkflowService()+ refreshed = await service._refresh_output_screenshot_urls(+ persisted_block_output, organization_id="o_1", workflow_run_id="wr_1"+ )++ assert refreshed["downloaded_files"][0]["url"] == "https://skyvern-uploads.s3.amazonaws.com/.../legacy.pdf?sig=x"+ mock_get.assert_not_awaited()+++async def test_refresh_handles_missing_artifact_rows():+ """Artifact ids stored on the snapshot may not resolve (row deleted by+ data scrubber, etc.). Don't crash — keep the stored URLs as a last resort."""+ from skyvern.forge.sdk.workflow.service import WorkflowService++ persisted_block_output = {+ "task_id": "tsk_1",+ "task_screenshot_artifact_ids": [],+ "workflow_screenshot_artifact_ids": [],+ "downloaded_file_artifact_ids": ["a_missing"],+ "downloaded_files": [+ {+ "url": "https://skyvern-uploads.s3.amazonaws.com/.../old.pdf?sig=x",+ "checksum": "sha-1",+ "filename": "old.pdf",+ "modified_at": None,+ }+ ],+ "downloaded_file_urls": [+ "https://skyvern-uploads.s3.amazonaws.com/.../old.pdf?sig=x",+ ],+ }++ with patch(+ "skyvern.forge.sdk.workflow.service.app.DATABASE.artifacts.get_artifacts_by_ids",+ new=AsyncMock(return_value=[]),+ ):+ service = WorkflowService()+ refreshed = await service._refresh_output_screenshot_urls(+ persisted_block_output, organization_id="o_1", workflow_run_id="wr_1"+ )++ # Falls back to whatever URL was already stored; doesn't blank out.+ assert refreshed["downloaded_files"][0]["url"] == "https://skyvern-uploads.s3.amazonaws.com/.../old.pdf?sig=x"