Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Mediate browser and CDP downloads (#8135)
Aaron Perez3e8f015f6e96
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 1446651a9..33f5eef4a 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -4185,6 +4185,8 @@ class ForgeAgent:cache_key=cache_key,ttl_seconds=3600, # 1 hour)+ if cache_data is None:+ return# Store cache metadata in contextcontext.vertex_cache_name = cache_data["name"]diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.pyindex a56ce430b..e9e5394b1 100644--- a/skyvern/forge/sdk/api/files.py+++ b/skyvern/forge/sdk/api/files.py@@ -240,6 +240,7 @@ async def fetch_file_bytes(None,current_url,next_url,+ strip_cross_origin_credentials=True,)source_url, current_url = current_url, next_url@@ -449,6 +450,7 @@ async def download_file(None,current_url,next_url,+ strip_cross_origin_credentials=True,)source_url, current_url = current_url, next_urlraise SkyvernHTTPException(diff --git a/skyvern/forge/sdk/api/llm/vertex_cache_manager.py b/skyvern/forge/sdk/api/llm/vertex_cache_manager.pyindex 8d0bc90fe..fac1066b9 100644--- a/skyvern/forge/sdk/api/llm/vertex_cache_manager.py+++ b/skyvern/forge/sdk/api/llm/vertex_cache_manager.py@@ -9,6 +9,7 @@ Unlike the Anthropic-style cache_control markers, Vertex AI requires:"""import json+import refrom datetime import datetimefrom typing import Any@@ -20,9 +21,29 @@ from google.auth.transport.requests import Requestfrom google.oauth2 import service_accountfrom skyvern.config import settings+from skyvern.utils.token_counter import count_tokensLOG = structlog.get_logger()+# /cachedContents rejects caches below a per-model token minimum (400 with min_total_token_count).+# Vertex docs: 32,769 tokens for Gemini 1.5; 2,048 for Gemini 2.x; 4,096 for Gemini 3.x — the 3.x GA+# floor is what broke every cache create when gemini-3.1-flash-lite moved off its -preview id+# (SKY-10043 §9). Unknown families fail high to the 3.x floor.+_MIN_CACHED_CONTENT_TOKENS_GEMINI_1X = 32769+_MIN_CACHED_CONTENT_TOKENS_GEMINI_2X = 2048+_MIN_CACHED_CONTENT_TOKENS_DEFAULT = 4096+++def min_cached_content_tokens(model_name: str) -> int:+ match = re.search(r"gemini-(\d+)", model_name.lower())+ if match:+ major = int(match.group(1))+ if major < 2:+ return _MIN_CACHED_CONTENT_TOKENS_GEMINI_1X+ if major == 2:+ return _MIN_CACHED_CONTENT_TOKENS_GEMINI_2X+ return _MIN_CACHED_CONTENT_TOKENS_DEFAULT+class VertexCacheManager:"""@@ -86,7 +107,7 @@ class VertexCacheManager:cache_key: str,ttl_seconds: int = 3600,system_instruction: str | None = None,- ) -> dict[str, Any]:+ ) -> dict[str, Any] | None:"""Create a cache object using Vertex AI's /cachedContents API.@@ -98,8 +119,24 @@ class VertexCacheManager:system_instruction: Optional system instruction to includeReturns:- Cache data with 'name', 'expireTime', etc.+ Cache data with 'name', 'expireTime', etc., or None when the content is+ below the model's minimum cacheable size and creation was skipped."""+ min_tokens = min_cached_content_tokens(model_name)+ content_token_count = count_tokens(static_content)+ if system_instruction:+ content_token_count += count_tokens(system_instruction)+ if content_token_count < min_tokens:+ LOG.info(+ "Static content below Vertex cachedContents minimum, skipping cache creation",+ sampling=True,+ cache_key=cache_key,+ model=model_name,+ content_token_count=content_token_count,+ min_required_tokens=min_tokens,+ )+ return None+# Check if cache already exists for this keyif cache_key in self._cache_registry:cache_data = self._cache_registry[cache_key]diff --git a/tests/unit/test_vertex_cache_min_tokens.py b/tests/unit/test_vertex_cache_min_tokens.pynew file mode 100644index 000000000..1f009b039--- /dev/null+++ b/tests/unit/test_vertex_cache_min_tokens.py@@ -0,0 +1,109 @@+"""Tests for the Vertex cachedContents minimum-size guard (SKY-9983 / SKY-10043)."""++from typing import Any+from unittest.mock import MagicMock++import pytest++from skyvern.forge.sdk.api.llm.vertex_cache_manager import VertexCacheManager, min_cached_content_tokens+from skyvern.utils.token_counter import count_tokens++SMALL_CONTENT = "tiny static prompt"+MID_CONTENT = "word " * 3000 # between the 2.x and 3.x minimums+BIG_CONTENT = "word " * 5000 # above every minimum++[email protected](autouse=True)+def _token_size_sanity() -> None:+ assert count_tokens(SMALL_CONTENT) < 2048+ assert 2048 <= count_tokens(MID_CONTENT) < 4096+ assert count_tokens(BIG_CONTENT) >= 4096+++def manager(monkeypatch: pytest.MonkeyPatch) -> VertexCacheManager:+ mgr = VertexCacheManager(project_id="test-project")+ monkeypatch.setattr(VertexCacheManager, "_get_access_token", lambda self: "test-token")+ return mgr+++def _mock_post(monkeypatch: pytest.MonkeyPatch) -> MagicMock:+ response = MagicMock()+ response.status_code = 200+ response.json.return_value = {"name": "cachedContents/abc", "expireTime": "2099-01-01T00:00:00Z"}+ post = MagicMock(return_value=response)+ monkeypatch.setattr("skyvern.forge.sdk.api.llm.vertex_cache_manager.requests.post", post)+ return post+++class TestMinCachedContentTokens:+ @pytest.mark.parametrize(+ ("model_name", "expected"),+ [+ ("gemini-1.5-flash", 32769),+ ("gemini-1.5-pro", 32769),+ ("vertex_ai/gemini-1.5-pro", 32769),+ ("gemini-2.5-flash", 2048),+ ("gemini-2.5-flash-lite", 2048),+ ("gemini-2.5-pro", 2048),+ ("gemini-3-flash-preview", 4096),+ ("gemini-3.1-flash-lite", 4096),+ ("some-unknown-model", 4096),+ ],+ )+ def test_per_model_minimums(self, model_name: str, expected: int) -> None:+ assert min_cached_content_tokens(model_name) == expected+++class TestCreateCacheGuard:+ def test_skips_below_minimum_without_any_api_call(+ self, manager: VertexCacheManager, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ post = _mock_post(monkeypatch)+ result = manager.create_cache(model_name="gemini-3.1-flash-lite", static_content=SMALL_CONTENT, cache_key="k1")+ assert result is None+ post.assert_not_called()+ assert "k1" not in manager._cache_registry++ def test_ga_minimum_regression_case(self, manager: VertexCacheManager, monkeypatch: pytest.MonkeyPatch) -> None:+ """The SKY-10043 incident shape: content that satisfies the 2.x minimum but not the 3.x GA one."""+ post = _mock_post(monkeypatch)+ skipped = manager.create_cache(model_name="gemini-3.1-flash-lite", static_content=MID_CONTENT, cache_key="k-3x")+ assert skipped is None+ post.assert_not_called()++ created: dict[str, Any] | None = manager.create_cache(+ model_name="gemini-2.5-flash", static_content=MID_CONTENT, cache_key="k-2x"+ )+ assert created is not None+ assert created["name"] == "cachedContents/abc"+ post.assert_called_once()++ def test_gemini_1x_minimum_not_conflated_with_2x(+ self, manager: VertexCacheManager, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """Content above the 2.x/3.x floors is still below Gemini 1.5's 32,769-token minimum."""+ post = _mock_post(monkeypatch)+ result = manager.create_cache(model_name="gemini-1.5-pro", static_content=BIG_CONTENT, cache_key="k-1x")+ assert result is None+ post.assert_not_called()++ def test_creates_above_minimum(self, manager: VertexCacheManager, monkeypatch: pytest.MonkeyPatch) -> None:+ post = _mock_post(monkeypatch)+ result = manager.create_cache(model_name="gemini-3.1-flash-lite", static_content=BIG_CONTENT, cache_key="k2")+ assert result is not None+ assert manager._cache_registry["k2"]["name"] == "cachedContents/abc"+ post.assert_called_once()++ def test_system_instruction_counts_toward_minimum(+ self, manager: VertexCacheManager, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ post = _mock_post(monkeypatch)+ result = manager.create_cache(+ model_name="gemini-3.1-flash-lite",+ static_content=MID_CONTENT,+ cache_key="k3",+ system_instruction=MID_CONTENT,+ )+ assert result is not None+ post.assert_called_once()