Skip to content

Automate browser based workflows with AI

Fix run-scoped file URI resolution (SKY-16915) (#8778)

Aaron Perezf09b973b797b
diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.py
index d459ed7ea..41dee20c9 100644
--- a/skyvern/forge/sdk/api/files.py
+++ b/skyvern/forge/sdk/api/files.py
@@ -855,13 +855,42 @@ async def resolve_local_or_download_file(
) -> str:
"""Resolve a file input to a local path.
- Absolute paths are validated against the run's download directory; anything else is downloaded.
+ Run-local paths are validated against the run's download directory; anything else is downloaded.
"""
- # Absolute paths are the run-local convention; treating all non-remote strings as paths would misroute bad URLs.
- if file_url.startswith("/"):
- resolved = validate_local_file_path(file_url, run_id)
+ parsed_url = urlparse(file_url)
+ local_path: str | None = file_url if file_url.startswith("/") else None
+ if parsed_url.scheme == "file":
+ candidate_path = parse_uri_to_path(file_url)
+ download_root = Path(settings.DOWNLOAD_PATH).absolute()
+ candidate = Path(candidate_path)
+ local_download_root = False
+ if (
+ organization_id
+ and organization_id not in (".", "..")
+ and not os.path.isabs(organization_id)
+ and "/" not in organization_id
+ and "\\" not in organization_id
+ ):
+ resolved_candidate = candidate.resolve()
+ local_download_root = any(
+ resolved_candidate.is_relative_to((Path(root) / settings.ENV / organization_id).resolve())
+ for root in _LOCAL_DOWNLOAD_ROOTS
+ )
+ if not local_download_root and (
+ candidate.is_relative_to(download_root) or candidate.resolve().is_relative_to(download_root.resolve())
+ ):
+ # Preserve org-scoped local uploads; other files under DOWNLOAD_PATH must be run-scoped.
+ file_id = await uploaded_file_id_for_local_uri(file_url, organization_id)
+ if file_id is not None:
+ file_url = file_id
+ else:
+ local_path = candidate_path
+
+ # Explicit local paths are run-scoped; arbitrary strings must not be treated as paths instead of URLs.
+ if local_path is not None:
+ resolved = validate_local_file_path(local_path, run_id)
if not os.path.isfile(resolved):
- raise FileNotFoundError(f"Local file not found: {file_url}")
+ raise FileNotFoundError(f"Local file not found: {local_path}")
if max_size_mb is not None and os.path.getsize(resolved) > max_size_mb * 1024 * 1024:
raise DownloadFileMaxSizeExceeded(max_size_mb)
return resolved
diff --git a/skyvern/forge/sdk/copilot/credential_pause.py b/skyvern/forge/sdk/copilot/credential_pause.py
index 9fd847912..875ef879c 100644
--- a/skyvern/forge/sdk/copilot/credential_pause.py
+++ b/skyvern/forge/sdk/copilot/credential_pause.py
@@ -392,6 +392,19 @@ async def check_credential_pause_resumable(
_validate_pending_pause(record, resume_token)
+async def _record_turn_resumed(organization_id: str, chat_id: str, turn_id: str) -> None:
+ # Reconcile reads the pause before the turn marker, so stamping first leaves no instant where a
+ # resumed turn shows neither a live pause nor a resume and can be reclaimed as abandoned.
+ try:
+ await app.DATABASE.workflow_params.record_pending_copilot_turn_credential_resume(
+ organization_id=organization_id,
+ workflow_copilot_chat_id=chat_id,
+ turn_id=turn_id,
+ )
+ except Exception:
+ LOG.warning("Could not record the credential pause resume on the turn marker", exc_info=True)
+
+
async def resolve_credential_pause(
cache: Any,
*,
@@ -414,6 +427,7 @@ async def resolve_credential_pause(
"""
active_key = credential_pause_active_key(organization_id, workflow_copilot_chat_id, turn_id)
lock_key = _credential_pause_lock_key(organization_id, workflow_copilot_chat_id, turn_id)
+ await _record_turn_resumed(organization_id, workflow_copilot_chat_id, turn_id)
async with cache.get_lock(lock_key):
record = _validate_pending_pause(_decode_active_pause(await cache.get(active_key)), resume_token)
ttl = _credential_pause_record_ttl(settings.WORKFLOW_COPILOT_CREDENTIAL_PAUSE_TIMEOUT_SECONDS)
@@ -574,6 +588,8 @@ async def finish_manual_sign_in(
) -> bool:
"""Settle a Done claim; False means the claim was lost and the caller must discard the profile."""
active_key = credential_pause_active_key(organization_id, workflow_copilot_chat_id, turn_id)
+ if signed_in is not None:
+ await _record_turn_resumed(organization_id, workflow_copilot_chat_id, turn_id)
async with cache.get_lock(_credential_pause_lock_key(organization_id, workflow_copilot_chat_id, turn_id)):
record = _decode_active_pause(await cache.get(active_key))
if record is None or record.status != "resolving" or record.claim_id != claim.claim_id:
@@ -1023,6 +1039,7 @@ async def _run_credential_pause(
if waited is not None:
break
# The waiter's last look can race a sign-in start or a Done claim; settle that under the lock.
+ await _record_turn_resumed(organization_id, chat_id, turn_id)
settled = await _invalidate_active_pause_record(keep_live=True)
if isinstance(settled, str):
wait_seconds = await _seconds_left_on_pause(ctx, resume_token)
@@ -1038,14 +1055,7 @@ async def _run_credential_pause(
# Can't act on a rescued resolution mid-unwind, only avoid corrupting state.
await _invalidate_active_pause_record()
raise
- try:
- await app.DATABASE.workflow_params.record_pending_copilot_turn_credential_resume(
- organization_id=organization_id,
- workflow_copilot_chat_id=chat_id,
- turn_id=turn_id,
- )
- except Exception:
- LOG.warning("Could not record the credential pause resume on the turn marker", exc_info=True)
+ await _record_turn_resumed(organization_id, chat_id, turn_id)
if resolution is None and not invalidated:
invalidated_resolution = await _invalidate_active_pause_record()
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index 98ec7ef38..f2e026671 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -11039,7 +11039,9 @@ class DownloadToS3Block(Block):
uri = None
try:
uri = self._get_s3_uri(uploads_organization_id, workflow_run_id)
- await self._upload_file_to_s3(uri, file_path, cleanup_file=not self.url.startswith("/"))
+ input_path = parse_uri_to_path(self.url) if urlparse(self.url).scheme == "file" else self.url
+ is_original_file = os.path.isabs(input_path) and os.path.realpath(input_path) == os.path.realpath(file_path)
+ await self._upload_file_to_s3(uri, file_path, cleanup_file=not is_original_file)
except Exception as e:
LOG.error("DownloadToS3Block Failed to upload file to S3", uri=uri, error=str(e))
raise e
diff --git a/skyvern/forge/sdk/workflow/models/pdf_fill_block.py b/skyvern/forge/sdk/workflow/models/pdf_fill_block.py
index 838baf166..2318b156f 100644
--- a/skyvern/forge/sdk/workflow/models/pdf_fill_block.py
+++ b/skyvern/forge/sdk/workflow/models/pdf_fill_block.py
@@ -11,6 +11,7 @@ import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any, ClassVar, Literal, cast
+from urllib.parse import urlparse
import aiofiles
import pdfplumber
@@ -26,7 +27,8 @@ from skyvern.forge.prompts import prompt_engine
from skyvern.forge.sdk.api.files import (
download_file,
get_path_for_workflow_download_directory,
- validate_local_file_path,
+ parse_uri_to_path,
+ resolve_local_or_download_file,
)
from skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandler
from skyvern.forge.sdk.api.llm.api_handler_factory import (
@@ -245,13 +247,15 @@ class PdfFillBlock(Block):
# a user-supplied local path is never deleted.
if settings.ENV == "local" and os.path.exists(self.file_url):
return self.file_url, False
- if self.file_url.startswith("/"):
+ parsed_url = urlparse(self.file_url)
+ if self.file_url.startswith("/") or parsed_url.scheme == "file":
context = skyvern_context.current()
run_id = context.run_id if context and context.run_id else workflow_run_id
- resolved = validate_local_file_path(self.file_url, run_id)
- if not os.path.isfile(resolved):
- raise FileNotFoundError(f"Local file not found: {self.file_url}")
- return resolved, False
+ resolved = await resolve_local_or_download_file(self.file_url, run_id, organization_id)
+ is_temp = parsed_url.scheme == "file" and os.path.realpath(resolved) != os.path.realpath(
+ parse_uri_to_path(self.file_url)
+ )
+ return resolved, is_temp
return await download_file(self.file_url, organization_id=organization_id), True
@staticmethod
diff --git a/skyvern/forge/sdk/workflow/models/split_pdf_block.py b/skyvern/forge/sdk/workflow/models/split_pdf_block.py
index bbc8cce05..f539aca9d 100644
--- a/skyvern/forge/sdk/workflow/models/split_pdf_block.py
+++ b/skyvern/forge/sdk/workflow/models/split_pdf_block.py
@@ -5,6 +5,7 @@ import json
import os
from pathlib import Path
from typing import Any, ClassVar, Literal, cast
+from urllib.parse import urlparse
import structlog
from pypdf import PdfReader, PdfWriter
@@ -17,7 +18,8 @@ from skyvern.forge.prompts import prompt_engine
from skyvern.forge.sdk.api.files import (
download_file,
get_path_for_workflow_download_directory,
- validate_local_file_path,
+ parse_uri_to_path,
+ resolve_local_or_download_file,
)
from skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandler
from skyvern.forge.sdk.api.llm.api_handler_factory import (
@@ -135,13 +137,15 @@ class SplitPdfBlock(Block):
async def _resolve_source_pdf(self, workflow_run_id: str, organization_id: str | None) -> tuple[str, bool]:
if settings.ENV == "local" and os.path.exists(self.file_url):
return self.file_url, False
- if self.file_url.startswith("/"):
+ parsed_url = urlparse(self.file_url)
+ if self.file_url.startswith("/") or parsed_url.scheme == "file":
context = skyvern_context.current()
run_id = context.run_id if context and context.run_id else workflow_run_id
- resolved = validate_local_file_path(self.file_url, run_id)
- if not os.path.isfile(resolved):
- raise FileNotFoundError(f"Local file not found: {self.file_url}")
- return resolved, False
+ resolved = await resolve_local_or_download_file(self.file_url, run_id, organization_id)
+ is_temp = parsed_url.scheme == "file" and os.path.realpath(resolved) != os.path.realpath(
+ parse_uri_to_path(self.file_url)
+ )
+ return resolved, is_temp
return await download_file(self.file_url, organization_id=organization_id), True
@staticmethod
@@ -304,8 +308,11 @@ class SplitPdfBlock(Block):
reader: PdfReader,
documents: list[dict[str, Any]],
base_dir: Path,
+ source_pdf_path: str,
) -> list[tuple[dict[str, Any], bytes]]:
written: list[tuple[dict[str, Any], bytes]] = []
+ source_path = Path(source_pdf_path).resolve()
+ used_paths: set[tuple[str, str]] = set()
for document in documents:
start_page = cast(int, document["start_page"])
end_page = cast(int, document["end_page"])
@@ -320,14 +327,25 @@ class SplitPdfBlock(Block):
folder = cast(str, document.get("folder") or "")
output_dir = base_dir.joinpath(*folder.split("/")) if folder else base_dir
output_dir.mkdir(parents=True, exist_ok=True)
- output_path = output_dir / cast(str, document["name"])
+ output_name = cast(str, document["name"])
+ output_path = output_dir / output_name
+ while (
+ (folder, output_name) in used_paths
+ or output_path.resolve() == source_path
+ or (output_path.exists() and output_path.samefile(source_path))
+ ):
+ used_paths.add((folder, output_name))
+ output_name = SplitPdfBlock._deduplicate_document_name(folder, output_name, used_paths)
+ output_path = output_dir / output_name
+ used_paths.add((folder, output_name))
output_path.write_bytes(pdf_bytes)
doc_meta = dict(document)
doc_meta.update(
{
+ "name": output_name,
"file_path": str(output_path),
- "file_name": document["name"],
+ "file_name": output_name,
"file_size": len(pdf_bytes),
"page_range": [start_page, end_page],
"page_count": end_page - start_page + 1,
@@ -479,7 +497,7 @@ class SplitPdfBlock(Block):
)
base_dir = get_path_for_workflow_download_directory(workflow_run_id)
- written = self._write_split_documents(reader, documents, base_dir)
+ written = self._write_split_documents(reader, documents, base_dir, source_pdf_path)
artifact_org_id = organization_id or workflow_run_context.organization_id
artifact_block = await self._resolve_workflow_run_block(workflow_run_block_id, artifact_org_id)
diff --git a/tests/unit/test_files_resolve_local_or_download.py b/tests/unit/test_files_resolve_local_or_download.py
index 789a4257d..4275e4a1b 100644
--- a/tests/unit/test_files_resolve_local_or_download.py
+++ b/tests/unit/test_files_resolve_local_or_download.py
@@ -128,6 +128,112 @@ async def test_resolve_local_file_inside_run_download_dir(tmp_path: Path, monkey
assert await files.resolve_local_or_download_file(str(path), run_id) == str(path.resolve())
+async def test_resolve_local_file_uri_inside_run_download_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ run_id = "wr_local"
+ path = _run_file(tmp_path, monkeypatch, run_id, name="file with space.txt")
+
+ assert await files.resolve_local_or_download_file(path.as_uri(), run_id) == str(path.resolve())
+
+
[email protected]("case", ["another_run", "symlink_escape", "missing_run_id"])
+async def test_resolve_file_uri_rejects_out_of_scope_paths(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, case: str
+) -> None:
+ downloads_dir = tmp_path / "downloads"
+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(downloads_dir))
+ monkeypatch.setattr(settings, "ENV", "local")
+ monkeypatch.setattr(files, "REPO_ROOT_DIR", str(tmp_path))
+
+ current_run_file = downloads_dir / "wr_local" / "current.txt"
+ current_run_file.parent.mkdir(parents=True)
+ current_run_file.write_text("current run")
+ other_run_file = downloads_dir / "wr_other" / "other.txt"
+ other_run_file.parent.mkdir()
+ other_run_file.write_text("other run")
+ escaped_file = current_run_file.parent / "escape.txt"
+ escaped_file.symlink_to(other_run_file)
+
+ if case == "another_run":
+ uri, run_id = other_run_file.as_uri(), "wr_local"
+ elif case == "symlink_escape":
+ uri, run_id = escaped_file.as_uri(), "wr_local"
+ else:
+ uri, run_id = current_run_file.as_uri(), None
+
+ with pytest.raises(PermissionError):
+ await files.resolve_local_or_download_file(uri, run_id)
+
+
+async def test_resolve_managed_local_upload_under_download_root_uses_storage_authorization(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ downloads_dir = tmp_path / "downloads"
+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(downloads_dir))
+ monkeypatch.setattr(settings, "ENV", "local")
+ storage = LocalStorage(artifact_path=str(downloads_dir))
+ uri, _ = await storage.save_legacy_file(
+ organization_id="org-1", filename="input.csv", fileObj=io.BytesIO(b"row_id,url\n")
+ )
+ row = SimpleNamespace(file_id="file_1", storage_uri=uri, expires_at=None)
+
+ async def get_uploaded_file_by_storage_uri(*, storage_uri: str, organization_id: str) -> SimpleNamespace | None:
+ return row if (storage_uri, organization_id) == (uri, "org-1") else None
+
+ async def get_uploaded_file(*, file_id: str, organization_id: str) -> SimpleNamespace | None:
+ return row if (file_id, organization_id) == ("file_1", "org-1") else None
+
+ monkeypatch.setattr(forge_app, "STORAGE", storage)
+ monkeypatch.setattr(
+ forge_app,
+ "DATABASE",
+ SimpleNamespace(
+ uploaded_files=SimpleNamespace(
+ get_uploaded_file_by_storage_uri=get_uploaded_file_by_storage_uri,
+ get_uploaded_file=get_uploaded_file,
+ )
+ ),
+ )
+
+ path = await files.resolve_local_or_download_file(uri, "wr_1", organization_id="org-1")
+
+ assert Path(path).read_bytes() == b"row_id,url\n"
+ with pytest.raises(PermissionError):
+ await files.resolve_local_or_download_file(uri, "wr_1", organization_id="org-2")
+
+
+async def test_resolve_org_download_snapshot_nested_under_download_root_uses_legacy_storage(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ downloads_dir = tmp_path / "downloads"
+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(downloads_dir))
+ monkeypatch.setattr(settings, "ENV", "local")
+ monkeypatch.setattr(files, "_LOCAL_DOWNLOAD_ROOTS", set())
+ artifact_root = downloads_dir / "artifact-store"
+ storage = LocalStorage(artifact_path=str(artifact_root))
+ source = artifact_root / "downloads" / "local" / "org-1" / "wr_1" / "source.pdf"
+ source.parent.mkdir(parents=True)
+ source.write_bytes(b"saved run download")
+ uri = source.as_uri()
+
+ monkeypatch.setattr(forge_app, "STORAGE", storage)
+ monkeypatch.setattr(
+ forge_app,
+ "DATABASE",
+ SimpleNamespace(uploaded_files=SimpleNamespace(get_uploaded_file_by_storage_uri=AsyncMock(return_value=None))),
+ )
+
+ resolved = await files.resolve_local_or_download_file(uri, "wr_1", organization_id="org-1")
+
+ assert resolved == str(source.resolve())
+ assert Path(resolved).read_bytes() == b"saved run download"
+ with pytest.raises(PermissionError):
+ await files.resolve_local_or_download_file(uri, "wr_1", organization_id="org-2")
+
+
@pytest.mark.asyncio
async def test_resolve_local_file_rejects_outside_run_download_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
diff --git a/tests/unit/test_pdf_fill_block.py b/tests/unit/test_pdf_fill_block.py
index 6605601cc..d107fe6a1 100644
--- a/tests/unit/test_pdf_fill_block.py
+++ b/tests/unit/test_pdf_fill_block.py
@@ -4,6 +4,7 @@ import io
import re
from datetime import UTC, datetime
from pathlib import Path
+from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, MagicMock
@@ -23,6 +24,7 @@ from pypdf.generic import (
)
from skyvern.forge import app
+from skyvern.forge.sdk.api import files
from skyvern.forge.sdk.utils.tesseract_languages import tesseract_language_arg, tesseract_ocr_packages
from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
from skyvern.forge.sdk.workflow.models import pdf_fill_block
@@ -446,6 +448,25 @@ async def test_resolve_source_pdf_accepts_run_local_absolute_path_in_non_local_e
assert is_temp is False
+async def test_resolve_source_pdf_accepts_run_local_file_uri_without_temporary_cleanup(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ run_id = "run_pdf_fill_file_uri"
+ _install_context(monkeypatch, run_id)
+ source_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "source.pdf")
+ _write_fillable_pdf(source_pdf)
+ monkeypatch.setattr("skyvern.forge.sdk.workflow.models.pdf_fill_block.settings.ENV", "production")
+ monkeypatch.setattr(files.app, "STORAGE", SimpleNamespace(manages_local_file_uri=lambda *_: False))
+ block = _make_block(file_url=source_pdf.as_uri())
+
+ resolved, is_temp = await block._resolve_source_pdf(run_id, organization_id="org-1")
+
+ assert resolved == str(source_pdf.resolve())
+ assert is_temp is False
+ assert source_pdf.exists()
+
+
@pytest.mark.asyncio
async def test_flat_pdf_without_tesseract_fails(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
run_id = "run_flat_pdf_no_ocr"
diff --git a/tests/unit/test_split_pdf_block.py b/tests/unit/test_split_pdf_block.py
index 0da9e0c2f..e6535ae28 100644
--- a/tests/unit/test_split_pdf_block.py
+++ b/tests/unit/test_split_pdf_block.py
@@ -2,10 +2,14 @@ from __future__ import annotations
from datetime import UTC, datetime
from pathlib import Path
+from types import SimpleNamespace
from typing import Any
+import pytest
from pypdf import PdfReader, PdfWriter
+from skyvern.config import settings
+from skyvern.forge.sdk.api import files
from skyvern.forge.sdk.workflow.models.parameter import OutputParameter
from skyvern.forge.sdk.workflow.models.split_pdf_block import SplitPdfBlock
from skyvern.forge.sdk.workflow.workflow_definition_converter import block_yaml_to_block
@@ -31,6 +35,14 @@ def _write_blank_pdf(path: Path, page_count: int) -> None:
writer.write(f)
+def _write_pdf_with_page_widths(path: Path, widths: list[int]) -> None:
+ writer = PdfWriter()
+ for width in widths:
+ writer.add_blank_page(width=width, height=400)
+ with path.open("wb") as f:
+ writer.write(f)
+
+
def test_sanitize_split_plan_accepts_valid_documents_and_preserves_order() -> None:
raw_response = {
"documents": [
@@ -106,7 +118,7 @@ def test_write_split_documents_writes_expected_pdf_ranges(tmp_path: Path) -> Non
{"name": "second.pdf", "folder": "packet_b/nested", "start_page": 3, "end_page": 5},
]
- written = SplitPdfBlock._write_split_documents(reader, documents, tmp_path / "downloads")
+ written = SplitPdfBlock._write_split_documents(reader, documents, tmp_path / "downloads", str(source_pdf))
assert len(written) == 2
for meta, pdf_bytes in written:
@@ -123,6 +135,62 @@ def test_write_split_documents_writes_expected_pdf_ranges(tmp_path: Path) -> Non
assert Path(written[1][0]["file_path"]).relative_to(tmp_path / "downloads") == Path("packet_b/nested/second.pdf")
+def test_write_split_documents_does_not_overwrite_source_pdf(tmp_path: Path) -> None:
+ source_pdf = tmp_path / "source.pdf"
+ _write_blank_pdf(source_pdf, page_count=2)
+ original_bytes = source_pdf.read_bytes()
+ reader = PdfReader(source_pdf)
+ documents = [{"name": "source.pdf", "folder": "", "start_page": 1, "end_page": 1}]
+
+ written = SplitPdfBlock._write_split_documents(reader, documents, tmp_path, str(source_pdf))
+
+ output_path = Path(written[0][0]["file_path"])
+ assert source_pdf.read_bytes() == original_bytes
+ assert output_path != source_pdf
+ assert output_path.name == "source_2.pdf"
+ assert len(PdfReader(output_path).pages) == 1
+
+
+def test_write_split_documents_does_not_overwrite_prior_output_while_avoiding_source(tmp_path: Path) -> None:
+ source_pdf = tmp_path / "downloads" / "invoice.pdf"
+ source_pdf.parent.mkdir(parents=True)
+ _write_pdf_with_page_widths(source_pdf, [300, 500])
+ original_bytes = source_pdf.read_bytes()
+ reader = PdfReader(source_pdf)
+ documents = [
+ {"name": "invoice.pdf", "folder": "", "start_page": 1, "end_page": 1},
+ {"name": "invoice_2.pdf", "folder": "", "start_page": 2, "end_page": 2},
+ ]
+
+ written = SplitPdfBlock._write_split_documents(reader, documents, source_pdf.parent, str(source_pdf))
+
+ output_paths = [Path(meta["file_path"]) for meta, _ in written]
+ assert output_paths[0].name == "invoice_2.pdf"
+ assert len(set(output_paths)) == 2
+ assert source_pdf.read_bytes() == original_bytes
+ assert [PdfReader(path).pages[0].mediabox.width for path in output_paths] == [300, 500]
+ assert all(path.read_bytes() == pdf_bytes for path, (_, pdf_bytes) in zip(output_paths, written))
+ assert [meta["page_range"] for meta, _ in written] == [[1, 1], [2, 2]]
+
+
+def test_write_split_documents_does_not_overwrite_duplicate_names_without_source_collision(tmp_path: Path) -> None:
+ source_pdf = tmp_path / "source.pdf"
+ _write_pdf_with_page_widths(source_pdf, [300, 500])
+ reader = PdfReader(source_pdf)
+ documents = [
+ {"name": "duplicate.pdf", "folder": "", "start_page": 1, "end_page": 1},
+ {"name": "duplicate.pdf", "folder": "", "start_page": 2, "end_page": 2},
+ ]
+
+ written = SplitPdfBlock._write_split_documents(reader, documents, tmp_path / "downloads", str(source_pdf))
+
+ output_paths = [Path(meta["file_path"]) for meta, _ in written]
+ assert len(set(output_paths)) == 2
+ assert [path.name for path in output_paths] == ["duplicate.pdf", "duplicate_2.pdf"]
+ assert [PdfReader(path).pages[0].mediabox.width for path in output_paths] == [300, 500]
+ assert all(path.read_bytes() == pdf_bytes for path, (_, pdf_bytes) in zip(output_paths, written))
+
+
def test_split_pdf_yaml_to_block_conversion() -> None:
yaml_block = SplitPdfBlockYAML(
block_type=BlockType.SPLIT_PDF,
@@ -151,3 +219,29 @@ def test_split_pdf_yaml_rejects_raw_custom_llm_key() -> None:
assert yaml_block.llm_key is None
assert yaml_block.model is None
+
+
+async def test_split_pdf_resolves_run_local_file_uri_without_temporary_cleanup(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ run_id = "run_split_pdf_file_uri"
+ downloads_dir = tmp_path / "downloads"
+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(downloads_dir))
+ monkeypatch.setattr(settings, "ENV", "production")
+ monkeypatch.setattr(files.app, "STORAGE", SimpleNamespace(manages_local_file_uri=lambda *_: False))
+ source = downloads_dir / run_id / "source.pdf"
+ source.parent.mkdir(parents=True)
+ _write_blank_pdf(source, page_count=1)
+ block = SplitPdfBlock(
+ label="split_pdf",
+ output_parameter=_make_output_parameter(),
+ file_url=source.as_uri(),
+ prompt="Split by document.",
+ )
+
+ resolved, is_temp = await block._resolve_source_pdf(run_id, organization_id="org-1")
+
+ assert resolved == str(source.resolve())
+ assert is_temp is False
+ assert source.exists()
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index f0b247933..0c1b00c26 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -32,6 +32,7 @@ from skyvern.forge import app
from skyvern.forge.sdk.api.llm.exceptions import LLMProviderError
from skyvern.forge.sdk.artifact.models import ArtifactType
from skyvern.forge.sdk.copilot import agent as agent_module
+from skyvern.forge.sdk.copilot import credential_pause
from skyvern.forge.sdk.copilot.ask_user import QuestionInteraction, QuestionResponse
from skyvern.forge.sdk.copilot.browser_ablation import CopilotEvalMode
from skyvern.forge.sdk.copilot.build_test_connect_failure import (
@@ -122,6 +123,7 @@ from tests.unit.copilot_route_test_support import (
terminal_narrative_payload,
)
from tests.unit.services.test_browser_recording_code_first import draft_for, make_click, make_input, make_url_change
+from tests.unit.test_copilot_credential_pause import _FakeCache
@pytest.fixture
@@ -6088,6 +6090,46 @@ async def test_reconcile_leaves_a_turn_that_just_resumed_from_a_credential_pause
assert await workflow_copilot_route._claim_turn_finalisation(chat, "turn-a")
[email protected]("answer", ["connected", "skip", "signed_in"])
+async def test_reconcile_between_a_card_answer_and_the_waiter_leaves_the_turn(
+ sqlite_engine: AsyncEngine, monkeypatch: pytest.MonkeyPatch, answer: str
+) -> None:
+ """An answer consumes the pause before the waiter wakes; a history read in that gap must not take the turn."""
+ repo = WorkflowParametersRepository(BaseAlchemyDB(sqlite_engine).Session)
+ monkeypatch.setattr(app.DATABASE, "workflow_params", repo)
+ cache = _FakeCache()
+ monkeypatch.setattr(app, "CACHE", cache)
+ chat_id = await _start_abandoned_turn(repo, "turn-a")
+ token = credential_pause._new_resume_token()
+ sign_in = credential_pause.ManualSignIn(
+ browser_session_id="pbs_debug", login_urls=["https://portal.example.com/login"], profile_name="Sign-in"
+ )
+ cache.store[credential_pause.credential_pause_active_key("org", chat_id, "turn-a")] = (
+ credential_pause._encode_active_pause(token, datetime.now(UTC) + timedelta(minutes=5), manual_sign_in=sign_in)
+ )
+ ids = {"organization_id": "org", "workflow_copilot_chat_id": chat_id, "turn_id": "turn-a"}
+ if answer == "signed_in":
+ claim = await credential_pause.claim_manual_sign_in(cache, resume_token=token, **ids)
+ profile = credential_pause.SignedInProfile(
+ browser_profile_id="bp_signed_in", profile_name="Sign-in", site="portal.example.com", cookie_count=1
+ )
+ assert await credential_pause.finish_manual_sign_in(cache, claim=claim, signed_in=profile, **ids)
+ else:
+ await credential_pause.resolve_credential_pause(
+ cache, resume_token=token, action=answer, credential_id="cred_1", **ids
+ )
+ assert await credential_pause.credential_pause_is_active("org", chat_id, "turn-a") is False
+
+ await workflow_copilot_route._reconcile_interrupted_copilot_turns(
+ await repo.get_workflow_copilot_chat_by_id("org", chat_id), "org"
+ )
+
+ assert [m for m in await repo.get_workflow_copilot_chat_messages(chat_id) if m.sender == "ai"] == []
+ chat = await repo.get_workflow_copilot_chat_by_id("org", chat_id)
+ assert await workflow_copilot_route._claim_turn_finalisation(chat, "turn-a")
+
+
@pytest_asyncio.fixture(params=["sqlite", "postgres"])
async def fence_repo(
request: pytest.FixtureRequest, sqlite_engine: AsyncEngine
diff --git a/tests/unit/workflow/test_download_to_s3_block.py b/tests/unit/workflow/test_download_to_s3_block.py
new file mode 100644
index 000000000..c319a2308
--- /dev/null
+++ b/tests/unit/workflow/test_download_to_s3_block.py
@@ -0,0 +1,60 @@
+from __future__ import annotations
+
+from datetime import UTC, datetime
+from pathlib import Path
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from skyvern.config import settings
+from skyvern.forge.sdk.api import files
+from skyvern.forge.sdk.workflow.models.block import DownloadToS3Block
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter
+
+
[email protected]("upload_fails", [False, True], ids=["success", "failure"])
+async def test_download_to_s3_preserves_same_run_file_uri(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, upload_fails: bool
+) -> None:
+ run_id = "wr_test"
+ path = tmp_path / "downloads" / run_id / "input.txt"
+ path.parent.mkdir(parents=True)
+ path.write_text("original")
+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))
+ monkeypatch.setattr(settings, "ENV", "local")
+ monkeypatch.setattr(files, "_storage_manages_file", lambda *_: False)
+
+ workflow_run_context = MagicMock()
+ workflow_run_context.has_parameter.return_value = False
+ workflow_run_context.has_value.return_value = False
+
+ class FakeS3Client:
+ async def upload_file_from_path(self, *, uri: str, file_path: str) -> None:
+ if upload_fails:
+ raise RuntimeError("upload failed")
+
+ block = DownloadToS3Block(
+ label="download_to_s3",
+ output_parameter=OutputParameter(
+ output_parameter_id="op_test",
+ workflow_id="w_test",
+ key="uploaded_file",
+ created_at=datetime.now(UTC),
+ modified_at=datetime.now(UTC),
+ ),
+ url=path.as_uri(),
+ )
+ monkeypatch.setattr(DownloadToS3Block, "get_workflow_run_context", lambda self, _: workflow_run_context)
+ monkeypatch.setattr(DownloadToS3Block, "record_output_parameter_value", AsyncMock())
+ monkeypatch.setattr(DownloadToS3Block, "get_async_aws_client", lambda self: FakeS3Client())
+ monkeypatch.setattr("skyvern.forge.sdk.workflow.models.block.skyvern_context.current", lambda: None)
+
+ if upload_fails:
+ with pytest.raises(RuntimeError, match="upload failed"):
+ await block.execute(run_id, "", organization_id="org_test")
+ else:
+ result = await block.execute(run_id, "", organization_id="org_test")
+ assert result.success
+
+ assert path.read_text() == "original"