Skip to content

Automate browser based workflows with AI

fix(SKY-11153): land adopted-session downloads run-side via eager save + url re-fetch (#6679)

Andrew Neilson3010c0ab51df
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index 8f7f57c59..3fdcc6755 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -158,6 +158,66 @@ def _download_target_path(download_dir: Path, suggested_filename: str | None) ->
return download_dir / f"{uuid.uuid4()}-{stem or 'download'}{suffix}"
+async def _save_adopted_session_download(
+ download: Download,
+ page: Page,
+ download_dir: Path,
+ workflow_run_id: str | None = None,
+) -> Path | None:
+ """Land an adopted-session download's bytes into download_dir, returning the file path or None.
+
+ Eager save_as is the only protection against the worker pod tearing the shared browser down before a
+ deferred save_as runs.
+ """
+ download_target = _download_target_path(download_dir, download.suggested_filename)
+ try:
+ await download.save_as(download_target)
+ if download_target.exists() and download_target.stat().st_size > 0:
+ return download_target
+ download_target.unlink(missing_ok=True)
+ LOG.warning(
+ "Adopted-session eager save_as produced an empty file; re-fetching download url",
+ download_dir=str(download_dir),
+ workflow_run_id=workflow_run_id,
+ )
+ except Exception:
+ download_target.unlink(missing_ok=True)
+ LOG.warning(
+ "Adopted-session eager save_as failed; re-fetching download url",
+ download_dir=str(download_dir),
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+
+ try:
+ response = await page.context.request.get(download.url)
+ if response.status != 200:
+ LOG.error(
+ "Adopted-session download url re-fetch returned non-200 status",
+ status=response.status,
+ workflow_run_id=workflow_run_id,
+ )
+ return None
+ # APIResponse.body() has no streaming variant, so a large download peaks at 2x its size in RSS.
+ body = await response.body()
+ if not body:
+ LOG.error(
+ "Adopted-session download url re-fetch returned an empty body",
+ workflow_run_id=workflow_run_id,
+ )
+ return None
+ download_target.write_bytes(body)
+ return download_target
+ except Exception:
+ LOG.error(
+ "Adopted-session download url re-fetch failed",
+ download_dir=str(download_dir),
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ return None
+
+
def _remove_download_listener(page: Page, callback: Callable[[Download], None]) -> None:
off = getattr(page, "off", None)
if callable(off):
@@ -831,6 +891,36 @@ class ActionHandler:
download_signal_poll_iterations=download_signal_poll_iterations,
)
+ if (
+ task.browser_session_id
+ and captured_download is not None
+ and not download_event_fallback_attempted
+ ):
+ download_event_fallback_attempted = True
+ saved_path = await _save_adopted_session_download(
+ captured_download,
+ page,
+ download_dir,
+ workflow_run_id=task.workflow_run_id,
+ )
+ if saved_path is not None:
+ download_event_fallback_used = True
+ download_triggered = True
+ LOG.info(
+ "Saved adopted-session download to active run directory",
+ download_dir=download_dir,
+ download_target=str(saved_path),
+ workflow_run_id=task.workflow_run_id,
+ )
+ break
+ download_event_fallback_failed = True
+ LOG.warning(
+ "Adopted-session download could not be saved or re-fetched",
+ download_dir=download_dir,
+ workflow_run_id=task.workflow_run_id,
+ )
+ break
+
list_files_after = await _list_observed_download_files()
if len(list_files_after) > len(list_files_before):
@@ -847,7 +937,8 @@ class ActionHandler:
break
if (
- captured_download is not None
+ not task.browser_session_id
+ and captured_download is not None
and download_event_captured_at is not None
and not download_event_fallback_attempted
and time.monotonic() - download_event_captured_at >= _download_event_grace_seconds
diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.py
index f1e0b8cfc..b5de11402 100644
--- a/skyvern/webeye/browser_factory.py
+++ b/skyvern/webeye/browser_factory.py
@@ -198,6 +198,14 @@ def set_popup_video_listener(browser_context: BrowserContext, browser_artifacts:
asyncio.ensure_future(_on_page(page))
+def _redact_url_query(url: str) -> str:
+ # Download URLs are often S3 presigned, carrying an X-Amz signature in the query — drop it before logging.
+ try:
+ return urlparse(url)._replace(query="").geturl()
+ except Exception:
+ return "<redacted>"
+
+
def set_download_file_listener(
browser_context: BrowserContext, download_timeout: float | None = None, **kwargs: Any
) -> None:
@@ -208,6 +216,16 @@ def set_download_file_listener(
try:
async with asyncio.timeout(download_timeout or BROWSER_DOWNLOAD_TIMEOUT):
file_path = await download.path()
+ if not file_path.exists():
+ # On an adopted persistent session the bytes live on the run connection, not
+ # this worker connection; saving is the run side's job, so skip rather than crash.
+ LOG.warning(
+ "Download artifact absent on this connection; skipping worker-side rename",
+ workflow_run_id=workflow_run_id,
+ task_id=task_id,
+ suggested_filename=download.suggested_filename,
+ )
+ return
if file_path.suffix:
return
@@ -216,7 +234,7 @@ def set_download_file_listener(
workflow_run_id=workflow_run_id,
task_id=task_id,
suggested_filename=download.suggested_filename,
- url=download.url,
+ url=_redact_url_query(download.url),
)
suffix = Path(download.suggested_filename).suffix
if suffix:
diff --git a/tests/unit/test_adopted_session_download_save.py b/tests/unit/test_adopted_session_download_save.py
new file mode 100644
index 000000000..230c764c6
--- /dev/null
+++ b/tests/unit/test_adopted_session_download_save.py
@@ -0,0 +1,139 @@
+"""Eager-save-then-refetch for adopted persistent-session downloads.
+
+On an adopted session the run connection owns the download artifact, but in prod the
+worker pod can tear the shared browser down before a deferred save_as runs. The helper
+saves eagerly and, when save_as raises (TargetClosedError) or yields a 0-byte file,
+re-fetches the replayable download url through the run page's request context.
+"""
+
+from pathlib import Path
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from skyvern.webeye.actions.handler import _save_adopted_session_download
+
+PDF_BODY = b"%PDF-1.4\n" + b"x" * 830
+
+
+def _download(suggested: str = "153743777.pdf", url: str = "https://example.com/download") -> MagicMock:
+ download = MagicMock()
+ download.suggested_filename = suggested
+ download.url = url
+ download.save_as = AsyncMock()
+ return download
+
+
+def _page_with_refetch(status: int = 200, body: bytes = PDF_BODY) -> MagicMock:
+ response = MagicMock()
+ response.status = status
+ response.body = AsyncMock(return_value=body)
+ page = MagicMock()
+ page.context.request.get = AsyncMock(return_value=response)
+ return page
+
+
+async def test_happy_path_eager_save_writes_bytes(tmp_path) -> None:
+ download = _download()
+
+ async def _save(target: object) -> None:
+ Path(str(target)).write_bytes(PDF_BODY)
+
+ download.save_as.side_effect = _save
+ page = _page_with_refetch()
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is not None and saved.exists()
+ assert saved.read_bytes() == PDF_BODY
+ page.context.request.get.assert_not_awaited()
+
+
+async def test_save_as_raises_target_closed_falls_back_to_refetch(tmp_path) -> None:
+ download = _download()
+ download.save_as.side_effect = Exception("Target page, context or browser has been closed")
+ page = _page_with_refetch()
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is not None and saved.exists()
+ assert saved.read_bytes() == PDF_BODY
+ page.context.request.get.assert_awaited_once_with(download.url)
+
+
+async def test_zero_byte_save_as_falls_back_to_refetch(tmp_path) -> None:
+ download = _download()
+
+ async def _save_empty(target: object) -> None:
+ Path(str(target)).write_bytes(b"")
+
+ download.save_as.side_effect = _save_empty
+ page = _page_with_refetch()
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is not None and saved.exists()
+ assert saved.read_bytes() == PDF_BODY
+ page.context.request.get.assert_awaited_once_with(download.url)
+ # the empty placeholder must not survive alongside the recovered file
+ assert sorted(p.name for p in tmp_path.iterdir()) == [saved.name]
+
+
+async def test_refetch_non_200_returns_none(tmp_path) -> None:
+ download = _download()
+ download.save_as.side_effect = Exception("closed")
+ page = _page_with_refetch(status=403, body=b"forbidden")
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is None
+ assert list(tmp_path.iterdir()) == []
+
+
+async def test_refetch_empty_body_returns_none(tmp_path) -> None:
+ download = _download()
+ download.save_as.side_effect = Exception("closed")
+ page = _page_with_refetch(status=200, body=b"")
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is None
+ assert list(tmp_path.iterdir()) == []
+
+
+async def test_refetch_raises_returns_none(tmp_path) -> None:
+ download = _download()
+ download.save_as.side_effect = Exception("closed")
+ page = MagicMock()
+ page.context.request.get = AsyncMock(side_effect=Exception("connection gone"))
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is None
+ assert list(tmp_path.iterdir()) == []
+
+
+async def test_partial_save_as_then_failed_refetch_leaves_no_orphan(tmp_path) -> None:
+ """A partial (non-empty) write followed by a save_as raise must not orphan a corrupt file
+ when the subsequent re-fetch also fails."""
+ download = _download()
+
+ async def _save_partial_then_raise(target: object) -> None:
+ Path(str(target)).write_bytes(b"%PDF-1.4 truncated")
+ raise Exception("Target page, context or browser has been closed")
+
+ download.save_as.side_effect = _save_partial_then_raise
+ page = MagicMock()
+ page.context.request.get = AsyncMock(side_effect=Exception("connection gone"))
+
+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")
+
+ assert saved is None
+ assert list(tmp_path.iterdir()) == []
diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.py
index 85c1af812..ff9175475 100644
--- a/tests/unit/test_download_file_action_handler.py
+++ b/tests/unit/test_download_file_action_handler.py
@@ -3,6 +3,7 @@ import tempfile
import time
from collections.abc import Callable
from datetime import UTC, datetime
+from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -1654,3 +1655,206 @@ async def test_handle_action_logs_warning_when_late_native_appears_after_xhr_fal
assert kwargs["xhr_fallback_files"] == ["report.zip"]
assert kwargs["post_settle_extra_file_count"] == 1
assert kwargs["post_settle_extra_files"] == ["native-late.zip"]
+
+
+async def test_handle_action_adopted_session_lands_download_via_eager_save(
+ span_exporter: InMemorySpanExporter,
+) -> None:
+ """On an adopted persistent session the run connection saves the download eagerly at event
+ time and the bytes land in the run dir."""
+ now = datetime.now(UTC)
+ organization = make_organization(now)
+ task = make_task(
+ now,
+ organization,
+ workflow_run_id="wr-1",
+ browser_session_id="bs-1",
+ download_timeout=30.0,
+ )
+ step = make_step(now, task, step_id="step-1", status=StepStatus.created, order=0, output=None)
+
+ page = MagicMock()
+ page.url = "https://example.com/download"
+ page.context.browser = None
+ page.evaluate = AsyncMock()
+ page.expose_binding = AsyncMock()
+ download_callbacks: dict[str, Callable[[object], None]] = {}
+ page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)
+
+ browser_state = MagicMock()
+ browser_state.list_valid_pages = AsyncMock(return_value=[page])
+
+ scraped_page = ScrapedPage(
+ elements=[],
+ element_tree=[],
+ element_tree_trimmed=[],
+ _browser_state=browser_state,
+ _clean_up_func=AsyncMock(return_value=[]),
+ _scrape_exclude=None,
+ )
+
+ action = ClickAction(
+ element_id="download-link",
+ download=True,
+ organization_id=task.organization_id,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ )
+
+ download = MagicMock()
+ download.suggested_filename = "report.pdf"
+ download.url = "https://example.com/presigned/report.pdf"
+
+ async def save_download(target_path: str | os.PathLike[str]) -> None:
+ with open(target_path, "wb") as f:
+ f.write(b"%PDF-1.4 report bytes")
+
+ download.save_as = AsyncMock(side_effect=save_download)
+
+ async def mock_inner_handle_action(*args: object, **kwargs: object) -> list[ActionSuccess]:
+ download_callbacks["download"](download)
+ return [ActionSuccess()]
+
+ with tempfile.TemporaryDirectory() as temp_root:
+ primary_dir = os.path.join(temp_root, "bs-1")
+ os.makedirs(primary_dir)
+
+ mock_app = MagicMock()
+ mock_app.BROWSER_MANAGER.get_for_task.return_value = browser_state
+ mock_app.DATABASE.workflow_params.create_action = AsyncMock(return_value=action)
+ mock_app.STORAGE.list_downloaded_files_in_browser_session = AsyncMock(return_value=[])
+ wait_for_downloads = AsyncMock()
+
+ with (
+ patch.object(ActionHandler, "_handle_action", side_effect=mock_inner_handle_action),
+ patch("skyvern.webeye.actions.handler.get_download_dir", return_value=primary_dir),
+ patch("skyvern.webeye.actions.handler.skyvern_context.current", return_value=MagicMock(run_id="bs-1")),
+ patch(
+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",
+ new=wait_for_downloads,
+ ),
+ patch("skyvern.webeye.actions.handler.app", mock_app),
+ ):
+ results = await ActionHandler.handle_action(
+ scraped_page=scraped_page,
+ task=task,
+ step=step,
+ page=page,
+ action=action,
+ )
+
+ landed_files = sorted(os.listdir(primary_dir))
+
+ assert results[-1].download_triggered is True
+ assert action.download_triggered is True
+ assert len(landed_files) == 1 and landed_files[0].endswith("-report.pdf")
+ assert results[-1].downloaded_files == landed_files
+ download.save_as.assert_awaited_once()
+ # eager save wins; the url re-fetch is not needed when save_as succeeds
+ page.context.request.get.assert_not_called()
+ span_attrs = _download_wait_span_attrs(span_exporter)
+ assert span_attrs["download_event_fallback_used"] is True
+
+
+async def test_handle_action_adopted_session_refetches_when_save_as_target_closed(
+ span_exporter: InMemorySpanExporter,
+) -> None:
+ """When the worker tears the shared browser down and the run's save_as raises while the run
+ request context is still alive, the adopted-session path re-fetches the replayable url and the
+ bytes still land in the run dir."""
+ now = datetime.now(UTC)
+ organization = make_organization(now)
+ task = make_task(
+ now,
+ organization,
+ workflow_run_id="wr-1",
+ browser_session_id="bs-1",
+ download_timeout=30.0,
+ )
+ step = make_step(now, task, step_id="step-1", status=StepStatus.created, order=0, output=None)
+
+ page = MagicMock()
+ page.url = "https://example.com/download"
+ page.context.browser = None
+ page.evaluate = AsyncMock()
+ page.expose_binding = AsyncMock()
+ download_callbacks: dict[str, Callable[[object], None]] = {}
+ page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)
+
+ refetched_bytes = b"%PDF-1.4 refetched report bytes"
+ refetch_response = MagicMock()
+ refetch_response.status = 200
+ refetch_response.body = AsyncMock(return_value=refetched_bytes)
+ page.context.request.get = AsyncMock(return_value=refetch_response)
+
+ browser_state = MagicMock()
+ browser_state.list_valid_pages = AsyncMock(return_value=[page])
+
+ scraped_page = ScrapedPage(
+ elements=[],
+ element_tree=[],
+ element_tree_trimmed=[],
+ _browser_state=browser_state,
+ _clean_up_func=AsyncMock(return_value=[]),
+ _scrape_exclude=None,
+ )
+
+ action = ClickAction(
+ element_id="download-link",
+ download=True,
+ organization_id=task.organization_id,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ )
+
+ download = MagicMock()
+ download.suggested_filename = "report.pdf"
+ download.url = "https://example.com/presigned/report.pdf"
+ download.save_as = AsyncMock(side_effect=Exception("Target page, context or browser has been closed"))
+
+ async def mock_inner_handle_action(*args: object, **kwargs: object) -> list[ActionSuccess]:
+ download_callbacks["download"](download)
+ return [ActionSuccess()]
+
+ with tempfile.TemporaryDirectory() as temp_root:
+ primary_dir = os.path.join(temp_root, "bs-1")
+ os.makedirs(primary_dir)
+
+ mock_app = MagicMock()
+ mock_app.BROWSER_MANAGER.get_for_task.return_value = browser_state
+ mock_app.DATABASE.workflow_params.create_action = AsyncMock(return_value=action)
+ mock_app.STORAGE.list_downloaded_files_in_browser_session = AsyncMock(return_value=[])
+ wait_for_downloads = AsyncMock()
+
+ with (
+ patch.object(ActionHandler, "_handle_action", side_effect=mock_inner_handle_action),
+ patch("skyvern.webeye.actions.handler.get_download_dir", return_value=primary_dir),
+ patch("skyvern.webeye.actions.handler.skyvern_context.current", return_value=MagicMock(run_id="bs-1")),
+ patch(
+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",
+ new=wait_for_downloads,
+ ),
+ patch("skyvern.webeye.actions.handler.app", mock_app),
+ ):
+ results = await ActionHandler.handle_action(
+ scraped_page=scraped_page,
+ task=task,
+ step=step,
+ page=page,
+ action=action,
+ )
+
+ landed = sorted(os.listdir(primary_dir))
+ landed_bytes = Path(primary_dir, landed[0]).read_bytes() if landed else b""
+
+ assert results[-1].download_triggered is True
+ assert action.download_triggered is True
+ assert len(landed) == 1 and landed[0].endswith("-report.pdf")
+ assert landed_bytes == refetched_bytes
+ assert results[-1].downloaded_files == landed
+ download.save_as.assert_awaited_once()
+ page.context.request.get.assert_awaited_once_with(download.url)
+ span_attrs = _download_wait_span_attrs(span_exporter)
+ assert span_attrs["download_event_fallback_used"] is True
diff --git a/tests/unit/test_proxy_url_custom.py b/tests/unit/test_proxy_url_custom.py
index fd32b0ae8..50e16f8ab 100644
--- a/tests/unit/test_proxy_url_custom.py
+++ b/tests/unit/test_proxy_url_custom.py
@@ -3,7 +3,23 @@ import pytest
from skyvern.config import settings
from skyvern.forge.sdk.db.utils import deserialize_proxy_location, serialize_proxy_location
from skyvern.schemas.runs import GeoTarget, ProxyLocation
-from skyvern.webeye.browser_factory import BrowserContextFactory, _redact_proxy_url
+from skyvern.webeye.browser_factory import BrowserContextFactory, _redact_proxy_url, _redact_url_query
+
+
+def test_redact_url_query_strips_presigned_signature() -> None:
+ redacted = _redact_url_query(
+ "https://bucket.s3.amazonaws.com/docs/report?X-Amz-Signature=deadbeef&X-Amz-Credential=AKIA"
+ )
+ assert redacted == "https://bucket.s3.amazonaws.com/docs/report"
+ assert "X-Amz-Signature" not in redacted
+
+
+def test_redact_url_query_keeps_url_without_query() -> None:
+ assert _redact_url_query("https://example.com/docs/report.pdf") == "https://example.com/docs/report.pdf"
+
+
+def test_redact_url_query_plain_string_passes_through() -> None:
+ assert _redact_url_query("not-a-url") == "not-a-url"
def test_redact_proxy_url_strips_password() -> None: