Skip to content

Automate browser based workflows with AI

Tell Copilot the saved-block download call in the browser-code description (SKY-17671) (#8800)

Andrew Neilsona710ee5bd38f
diff --git a/skyvern/forge/sdk/copilot/tools/browser_code.py b/skyvern/forge/sdk/copilot/tools/browser_code.py
index 5858ac983..538f661da 100644
--- a/skyvern/forge/sdk/copilot/tools/browser_code.py
+++ b/skyvern/forge/sdk/copilot/tools/browser_code.py
@@ -46,6 +46,7 @@ from skyvern.forge.sdk.copilot.mcp_adapter import (
is_redaction_withheld,
scrub_model_facing_tool_result,
)
+from skyvern.forge.sdk.copilot.reached_download_target import DownloadClaimHelperContract
from skyvern.forge.sdk.copilot.runtime import (
SENSITIVE_ORIGIN_ACTIVE_RUN_PAGE_ERROR,
SENSITIVE_ORIGIN_PAGE_ERROR,
@@ -82,7 +83,8 @@ MAX_CODE_CHARS = 20_000
MAX_VALUE_CHARS = 32_000
SESSION_LIFETIME_SECONDS = float(TOTAL_TIMEOUT_SECONDS + HARD_BACKSTOP_ALLOWANCE_SECONDS)
-TOOL_DESCRIPTION = """Run Python against the live browser in a persistent interpreter.
+TOOL_DESCRIPTION = (
+ """Run Python against the live browser in a persistent interpreter.
`target` names the browser: 'debug' (default) is the one this chat drives; 'last_run' is the one the
most recent test run executed in, when that run minted its own. A continuation on the page a run
@@ -128,7 +130,11 @@ Browser API (async, Playwright-shaped):
- `await search_web(query, max_results=10)` is the saved block's search helper: it calls the server-side
search API, touches no tab, and returns the same result shape as the `search_web` tool.
- workbench-only, not valid in a saved block: `tabs`, `switch_tab`, `click_and_wait_for_popup`,
- `click_and_download`, and `files`.
+ `click_and_download`, and `files`. A saved block claims a download with
+ `"""
+ + DownloadClaimHelperContract().call
+ + """`, which is not available here;
+ `get_block_schema` for block type `code` returns its parameters.
Not available: imports, names starting with `_`, event listeners (`page.on`, `expect_*`), cookies,
request interception, and new browser contexts. Using one returns an error that says so.
@@ -148,6 +154,7 @@ block should read and promoted unchanged.
Limits: code up to 20,000 characters; `value` up to 32,000 characters of JSON and `stdout` up to 16 KB,
cut beyond that, so return or print a summary; the first 50 operations are listed; a chat turn holds at
most 32 files of 16 MB each."""
+)
TOOL_SCHEMA: dict[str, Any] = {
"type": "object",
diff --git a/skyvern/forge/sdk/encrypt/aes.py b/skyvern/forge/sdk/encrypt/aes.py
index 32a664282..bb2f8764b 100644
--- a/skyvern/forge/sdk/encrypt/aes.py
+++ b/skyvern/forge/sdk/encrypt/aes.py
@@ -39,6 +39,9 @@ class AES(BaseEncryptor):
self._encryption_params(fallback_salt, fallback_iv)
for fallback_salt, fallback_iv in (fallback_decrypt_keys or [])
]
+ # PBKDF2 blocks the event loop for tens of ms and its output depends only on secret and salt, so derive once
+ # per salt. Filled on first use rather than here so processes that never encrypt don't pay for it at boot.
+ self._derived_keys: dict[bytes, bytes] = {}
def method(self) -> EncryptMethod:
return EncryptMethod.AES
@@ -51,13 +54,18 @@ class AES(BaseEncryptor):
)
def _derive_key(self, salt: bytes | None = None) -> bytes:
- kdf = PBKDF2HMAC(
- algorithm=hashes.SHA256(),
- length=32,
- salt=salt if salt is not None else self.salt,
- iterations=100000,
- )
- return kdf.derive(self.secret_key)
+ salt = salt if salt is not None else self.salt
+ key = self._derived_keys.get(salt)
+ if key is None:
+ kdf = PBKDF2HMAC(
+ algorithm=hashes.SHA256(),
+ length=32,
+ salt=salt,
+ iterations=100000,
+ )
+ key = kdf.derive(self.secret_key)
+ self._derived_keys[salt] = key
+ return key
async def encrypt(self, plaintext: str) -> str:
try:
diff --git a/skyvern/forge/sdk/encrypt/base.py b/skyvern/forge/sdk/encrypt/base.py
index 4f6cc3c95..78644c329 100644
--- a/skyvern/forge/sdk/encrypt/base.py
+++ b/skyvern/forge/sdk/encrypt/base.py
@@ -10,7 +10,7 @@ class TokenDecryptionError(Exception):
"""No configured key decrypts this ciphertext.
Terminal by construction: the candidate key set is fixed for the life of the
- process, so a retry re-derives the same keys and fails identically. Callers that
+ process, so a retry uses the same keys and fails identically. Callers that
poll must stop rather than re-dial.
"""
diff --git a/tests/unit/test_aes_fallback_decrypt.py b/tests/unit/test_aes_fallback_decrypt.py
index 1e00674f6..602fd1441 100644
--- a/tests/unit/test_aes_fallback_decrypt.py
+++ b/tests/unit/test_aes_fallback_decrypt.py
@@ -1,5 +1,9 @@
+from typing import Any
+
import pytest
+from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
+from skyvern.forge.sdk.encrypt import aes as aes_module
from skyvern.forge.sdk.encrypt.aes import AES
from skyvern.forge.sdk.encrypt.base import TokenDecryptionError
@@ -9,6 +13,8 @@ PRIMARY_IV = "primary_iv_xxxxxxxxx"
PRIOR_SALT = "prior_salt_value_xxxxxxxxxxxxxxxxx"
PRIOR_IV = "prior_iv_value_xxxxx"
LEGACY_PRIMARY_CIPHERTEXT = "rvmea7ou1gzyata3OwKEQg=="
+# "prior path" under PRIOR_SALT/PRIOR_IV, pinned so a key-derivation change that strands stored ciphertext fails here.
+LEGACY_PRIOR_CIPHERTEXT = "siwQyNbc6raO5BfK8ed/aw=="
@pytest.mark.asyncio
@@ -86,3 +92,27 @@ async def test_decrypt_tries_multiple_fallbacks_in_order() -> None:
],
)
assert await aes.decrypt(ciphertext) == "third match"
+
+
+async def test_derives_each_salt_key_once_and_still_reads_existing_ciphertext(monkeypatch: pytest.MonkeyPatch) -> None:
+ derived_salts: list[bytes] = []
+
+ def counting_pbkdf2(**kwargs: Any) -> PBKDF2HMAC:
+ derived_salts.append(kwargs["salt"])
+ return PBKDF2HMAC(**kwargs)
+
+ monkeypatch.setattr(aes_module, "PBKDF2HMAC", counting_pbkdf2)
+ aes = AES(
+ secret_key=SECRET,
+ salt=PRIMARY_SALT,
+ iv=PRIMARY_IV,
+ fallback_decrypt_keys=[(PRIOR_SALT, PRIOR_IV)],
+ )
+
+ for _ in range(3):
+ assert await aes.decrypt(LEGACY_PRIMARY_CIPHERTEXT) == "legacy primary"
+ assert await aes.decrypt(LEGACY_PRIOR_CIPHERTEXT) == "prior path"
+ assert await aes.decrypt(await aes.encrypt("round trip")) == "round trip"
+
+ assert len(derived_salts) == 2, "PBKDF2 must run once per salt per process, not on every encrypt/decrypt"