Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
[SKY-8743] token termination grace period seconds (#5376)
Aaron Perezb4043728026f
diff --git a/docs/integrations/mcp.mdx b/docs/integrations/mcp.mdxindex 2f6b1df2e..6d85debdf 100644--- a/docs/integrations/mcp.mdx+++ b/docs/integrations/mcp.mdx@@ -8,16 +8,18 @@ The Skyvern MCP server lets AI assistants like Claude Desktop, Claude Code, Code## What you can do-The MCP server exposes 33 tools across 6 categories:+The MCP server exposes 75+ tools across many categories. Here are the key ones:| Category | Key tools | What they do ||----------|-----------|--------------|-| **Browser session management** | `skyvern_browser_session_create`, `skyvern_browser_session_close`, `skyvern_browser_session_list`, `skyvern_browser_session_connect` | Open, manage, and reuse browser sessions |-| **Browser actions** | `skyvern_act`, `skyvern_navigate`, `skyvern_click`, `skyvern_type`, `skyvern_scroll`, `skyvern_select_option`, `skyvern_press_key`, `skyvern_wait` | Control the browser with natural language or CSS/XPath selectors |-| **Data extraction** | `skyvern_extract`, `skyvern_screenshot`, `skyvern_evaluate` | Pull structured data from pages, capture screenshots, run JavaScript |-| **Validation** | `skyvern_validate` | Check page conditions using AI (returns true/false) |-| **Credentials** | `skyvern_credential_list`, `skyvern_credential_get`, `skyvern_credential_delete` | Look up stored credentials for login flows |-| **Workflows** | `skyvern_workflow_create`, `skyvern_workflow_run`, `skyvern_workflow_status`, `skyvern_workflow_get`, `skyvern_workflow_update`, `skyvern_workflow_delete` | Build and execute multi-step automations |+| **Browser session management** | `skyvern_browser_session_create`, `skyvern_browser_session_close`, `skyvern_browser_session_list`, `skyvern_browser_session_get`, `skyvern_browser_session_connect` | Open, manage, and reuse browser sessions — including connecting to a local browser via CDP |+| **Browser actions** | `skyvern_act`, `skyvern_navigate`, `skyvern_click`, `skyvern_type`, `skyvern_hover`, `skyvern_scroll`, `skyvern_select_option`, `skyvern_press_key`, `skyvern_wait` | Control the browser with natural language, CSS/XPath selectors, or both (hybrid mode) |+| **Data extraction** | `skyvern_extract`, `skyvern_screenshot`, `skyvern_evaluate` | Pull structured JSON from pages, capture screenshots, run JavaScript |+| **Validation** | `skyvern_validate` | Assert page conditions in natural language (returns true/false with reasoning) |+| **Credentials & login** | `skyvern_login`, `skyvern_credential_list`, `skyvern_credential_get`, `skyvern_credential_delete` | Log in with stored credentials (supports Skyvern vault, Bitwarden, 1Password, Azure Key Vault) with automatic 2FA/TOTP |+| **Workflows** | `skyvern_workflow_create`, `skyvern_workflow_run`, `skyvern_workflow_status`, `skyvern_workflow_get`, `skyvern_workflow_update`, `skyvern_workflow_delete`, `skyvern_workflow_cancel` | Build, run, and manage multi-step automations with 23 block types |++Additional tools cover tab management, iframe switching, drag-and-drop, file upload, clipboard access, network/console inspection, HAR recording, web storage, auth state persistence, folder management, and cached script management.Your AI assistant decides which tools to call based on your instructions. For example, asking "go to Hacker News and get the top post title" triggers `skyvern_browser_session_create`, `skyvern_navigate`, `skyvern_extract`, and `skyvern_browser_session_close` automatically.@@ -45,8 +47,21 @@ claude mcp add-json skyvern '{"type":"http","url":"https://api.skyvern.com/mcp/"</Tab><Tab title="Claude Desktop">+**macOS / Windows (recommended, no Node.js required)**++1. Download [`skyvern-claude-desktop.mcpb`](https://github.com/Skyvern-AI/skyvern/raw/main/skyvern/cli/mcpb/releases/skyvern-claude-desktop.mcpb).+2. Double-click the downloaded `.mcpb` file.+3. When Claude Desktop opens the Skyvern installer preview, click **Install**.+4. In the **Configure Skyvern** dialog, paste your Skyvern API key.+5. Leave **Skyvern MCP URL** set to `https://api.skyvern.com/mcp/` unless you are connecting to a different deployment, then click **Save**.++If double-clicking is blocked by your system, you can instead open **Claude Desktop > Settings > Extensions > Install Extension** and select the downloaded file manually.++The bundle defaults to `https://api.skyvern.com/mcp/` and removes the separate Node.js requirement.++**Linux, self-hosted, or advanced fallback (requires Node.js >= 20)**-Add to your Claude Desktop config file:+Add this manual bridge to your Claude Desktop config file:- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`- **Linux**: `~/.config/Claude/claude_desktop_config.json`@@ -68,7 +83,7 @@ Add to your Claude Desktop config file:}```-This uses [mcp-remote](https://www.npmjs.com/package/mcp-remote) to bridge the remote MCP server to stdio. Requires Node.js >= 20.+This fallback uses [mcp-remote](https://www.npmjs.com/package/mcp-remote) to bridge the remote MCP server to stdio. It still requires Node.js >= 20.</Tab><Tab title="Cursor">@@ -131,7 +146,11 @@ x-api-key = "SKYVERN_API_KEY"</Tab></Tabs>-That's it — no Python, no `pip install`, no local server. Your AI assistant connects directly to Skyvern Cloud over HTTPS. Claude Desktop uses [mcp-remote](https://www.npmjs.com/package/mcp-remote) to bridge the connection (requires Node.js >= 20).+That's it — no Python, no `pip install`, no local server. Your AI assistant connects directly to Skyvern Cloud over HTTPS. Claude Desktop on macOS and Windows can use the one-click Skyvern bundle; Linux and advanced setups can still use [mcp-remote](https://www.npmjs.com/package/mcp-remote) with Node.js >= 20.++<Tip>+Prefer a CLI? For Skyvern Cloud, `pip install skyvern` then run `skyvern setup`. For the local self-hosted path, run `skyvern quickstart` or `skyvern init` and choose Claude Code during the MCP step.+</Tip>## Switch an existing MCP config@@ -160,7 +179,11 @@ If a config file exists but does not already contain a Skyvern entry, run `skyve## Alternative: Local mode (self-hosted)-Use this if you are self-hosting Skyvern and want the MCP server to talk to your own instance instead of Skyvern Cloud. This runs a lightweight Python process on your machine that connects to your local Skyvern server. Requires Python 3.11, 3.12, or 3.13.+Use this if you are self-hosting Skyvern and want the MCP server to talk to your own instance instead of Skyvern Cloud. This runs a lightweight Python process on your machine that connects to your local Skyvern server.++<Tip title="Supported Python Versions" icon="fa-brands fa-python">+Local mode requires Python 3.11, 3.12, or 3.13.+</Tip>```bashpip install skyvern@@ -298,6 +321,58 @@ Replace `/usr/bin/python3` with the output of `which python3`. For Skyvern Cloud<source src="https://github.com/user-attachments/assets/70cfe310-24dc-431a-adde-e72691f198a7" type="video/mp4" /></video>+## Connect to your local browser++You can point your AI assistant at a browser running on your machine instead of a cloud browser. This lets you automate internal tools on localhost, reuse existing login sessions, and verify local dev changes — all through the same MCP tools.++Start a local browser with a tunnel so Skyvern Cloud can reach it:++```bash+skyvern browser serve --tunnel --use-local-profile+```++This launches Chrome with your existing cookies and logins, and creates an ngrok tunnel. Your AI assistant can then connect to it:++> "Connect to my local browser and check if the dashboard at localhost:3000 renders correctly"++The `--use-local-profile` flag clones auth-relevant data from your default Chrome profile (cookies, saved passwords, local storage) so the browser starts pre-authenticated. Your original profile is never modified.++<Tip>+This is especially powerful during development: make a code change, then ask your AI assistant to navigate to `localhost:3000` and verify it works — fix code and validate in the same prompt.+</Tip>++<Warning>+Always use `--api-key` when exposing your browser via tunnel. Without it, anyone with the ngrok URL has full browser control.+</Warning>++<Accordion title="Claude Desktop bundle updates">+Private Claude Desktop bundles do not auto-update. Download the latest [`skyvern-claude-desktop.mcpb`](https://github.com/Skyvern-AI/skyvern/raw/main/skyvern/cli/mcpb/releases/skyvern-claude-desktop.mcpb) again, then reinstall it by double-clicking the bundle or using **Settings > Extensions**.+</Accordion>++## QA your frontend changes++For the local self-hosted path, run `skyvern quickstart` or `skyvern init`, choose `local`, then choose `Claude Code` during the MCP step. That writes local stdio MCP config and installs Claude Code skills — including `/qa`, which reads your git diff, generates browser tests, and runs them against your dev server.++```bash+pip install skyvern+skyvern quickstart+# or: skyvern init+```++Then make a frontend change and type `/qa` in Claude Code. It will:++1. Read your `git diff` and the full source of each changed file+2. Generate targeted test cases (plus regression tests for adjacent behavior)+3. Open a browser against your running dev server+4. Run the tests — navigate, click, fill forms, check the DOM+5. Report pass/fail with screenshots++For localhost testing, start your local Skyvern backend and frontend. You do not need `skyvern browser serve --tunnel` for this path.++<Tip>+The `/qa` skill uses `skyvern_evaluate` for fast DOM assertions (~10ms each) and `skyvern_act` for natural-language interactions. Tests are generated fresh from each diff — no test files to maintain.+</Tip>+## Troubleshooting<Accordion title="Invalid API key or 401 errors">diff --git a/skyvern/core/script_generations/real_skyvern_page_ai.py b/skyvern/core/script_generations/real_skyvern_page_ai.pyindex d676f8788..d57f423ae 100644--- a/skyvern/core/script_generations/real_skyvern_page_ai.py+++ b/skyvern/core/script_generations/real_skyvern_page_ai.py@@ -163,8 +163,18 @@ class RealSkyvernPageAi(SkyvernPageAi):intention: str,data: str | dict[str, Any] | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None,+ block_label: str | None = None,) -> str | None:- """Click an element using AI to locate it based on intention."""+ """Click an element using AI to locate it based on intention.++ Args:+ failed_selector: The original CSS selector that failed before falling+ back to AI. Used to record an element-level fallback episode so+ the script reviewer can fix the selector. Only set when called from+ the ai='fallback' path in skyvern_page.py.+ block_label: The cached block label (from SkyvernPage.current_label).+ """try:# Build the element tree of the current page for the promptcontext = skyvern_context.ensure_context()@@ -210,6 +220,20 @@ class RealSkyvernPageAi(SkyvernPageAi):raise Exception(result[-1].exception_message)xpath = action.get_xpath()selector = f"xpath={xpath}" if xpath else selector++ # Record element-level fallback episode for the script reviewer (code_v2 only).+ # This fires when a cached script's selector failed (or was missing) and+ # ai_click succeeded. The episode gives the reviewer the AI-found action data+ # so it can write a proper selector.+ await self._record_element_fallback_episode(+ context=context,+ action_type="click",+ failed_selector=failed_selector,+ intention=intention,+ action=action,+ block_label=block_label,+ )+return selectorexcept Exception:LOG.exception(@@ -233,6 +257,8 @@ class RealSkyvernPageAi(SkyvernPageAi):totp_identifier: str | None = None,totp_url: str | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None,+ block_label: str | None = None,) -> str:"""Input text into an element using AI to determine the value."""@@ -349,6 +375,14 @@ class RealSkyvernPageAi(SkyvernPageAi):result = await handle_input_text_action(action, self.page, self.scraped_page, task, step)if result and result[-1].success is False:raise Exception(result[-1].exception_message)+ await self._record_element_fallback_episode(+ context=context,+ action_type="fill",+ failed_selector=failed_selector,+ intention=intention,+ action=action,+ block_label=block_label,+ )else:locator = self.page.locator(selector)await handler_utils.input_sequentially(locator, transformed_value, timeout=timeout)@@ -645,6 +679,94 @@ class RealSkyvernPageAi(SkyvernPageAi):self._store_classify_result(result)return result+ async def _record_element_fallback_episode(+ self,+ context: skyvern_context.SkyvernContext,+ action_type: str,+ failed_selector: str | None,+ intention: str,+ action: Any,+ block_label: str | None = None,+ ) -> None:+ """Record an element-level fallback episode when ai_click/ai_input_text fires+ because a CSS selector failed or was missing. Gated on code_version >= 2.++ This gives the script reviewer the signal AND the action data (css_suggestion,+ element attributes) it needs to write a proper selector for the next script version.+ """+ if failed_selector is None:+ # None means the caller didn't pass failed_selector — this is a direct+ # ai_click call (not from the ai='fallback' path), so don't record.+ return+ if (context.code_version or 0) < 2:+ return+ if not context.workflow_run_id or not context.workflow_permanent_id:+ return+ try:+ # Build agent_actions data for the reviewer+ action_data: dict[str, Any] = {+ "action_type": action_type,+ "intention": intention,+ "failed_selector": failed_selector if failed_selector else "(missing — no selector= argument)",+ }+ if hasattr(action, "element_id"):+ action_data["element_id"] = action.element_id+ if hasattr(action, "skyvern_element_data") and action.skyvern_element_data:+ # This contains xpath, tag, text, attributes — the data the reviewer+ # needs to derive a stable selector+ el_data = action.skyvern_element_data+ action_data["element_tag"] = el_data.get("tag", "")+ action_data["element_text"] = el_data.get("text", "")[:200]+ action_data["all_attributes"] = {+ k: v+ for k, v in el_data.items()+ if k+ in (+ "id",+ "name",+ "class",+ "aria-label",+ "placeholder",+ "type",+ "role",+ "data-testid",+ "href",+ "value",+ "title",+ )+ }+ xpath = action.get_xpath() if hasattr(action, "get_xpath") else None+ if xpath:+ action_data["css_suggestion"] = f"xpath={xpath}"+ if hasattr(action, "reasoning"):+ action_data["reasoning"] = action.reasoning++ error_msg = (+ f"Selector {'failed' if failed_selector else 'missing'} on page.{action_type}(), "+ f"AI fallback succeeded. "+ f"Original selector: {failed_selector or '(none)'}. "+ f"Intention: {intention}"+ )+ await app.DATABASE.scripts.create_fallback_episode(+ organization_id=context.organization_id or "",+ workflow_permanent_id=context.workflow_permanent_id,+ workflow_run_id=context.workflow_run_id,+ block_label=block_label or self.current_label or "unknown",+ fallback_type="element",+ script_revision_id=context.script_revision_id,+ error_message=error_msg,+ page_url=self.page.url,+ agent_actions=action_data,+ )+ LOG.info(+ "Recorded element fallback episode for selector failure",+ block_label=block_label or self.current_label,+ action_type=action_type,+ failed_selector=failed_selector,+ )+ except Exception:+ LOG.warning("Failed to record element fallback episode for selector failure", exc_info=True)+@staticmethoddef _store_classify_result(result: str) -> None:"""Store the classify result on SkyvernContext for fallback episode recording."""diff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.pyindex 96b7c17a6..097783b79 100644--- a/skyvern/core/script_generations/skyvern_page.py+++ b/skyvern/core/script_generations/skyvern_page.py@@ -314,6 +314,7 @@ class SkyvernPage(Page):if ai == "fallback":# try to click the element with the original selector firsterror_to_raise = None+ original_selector = selector # preserve for fallback episode recordingif selector:try:locator = self._locator_scope.locator(selector).first@@ -351,6 +352,8 @@ class SkyvernPage(Page):intention=prompt,data=data,timeout=timeout,+ failed_selector=original_selector or "",+ block_label=self.current_label,)if error_to_raise:raise error_to_raise@@ -887,6 +890,7 @@ class SkyvernPage(Page):if ai == "fallback":error_to_raise = Noneoriginal_value = value+ original_selector = selector # preserve for fallback episode recordingif selector:try:value = await self.get_actual_value(@@ -920,6 +924,8 @@ class SkyvernPage(Page):totp_identifier=totp_identifier,totp_url=totp_url,timeout=timeout,+ failed_selector=original_selector or "",+ block_label=self.current_label,)if error_to_raise:raise error_to_raisediff --git a/skyvern/core/script_generations/skyvern_page_ai.py b/skyvern/core/script_generations/skyvern_page_ai.pyindex f90511183..fc66480fe 100644--- a/skyvern/core/script_generations/skyvern_page_ai.py+++ b/skyvern/core/script_generations/skyvern_page_ai.py@@ -14,6 +14,8 @@ class SkyvernPageAi(Protocol):intention: str,data: str | dict[str, Any] | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None,+ block_label: str | None = None,) -> str | None:"""Click an element using AI to locate it based on intention."""...@@ -27,6 +29,8 @@ class SkyvernPageAi(Protocol):totp_identifier: str | None = None,totp_url: str | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None,+ block_label: str | None = None,) -> str:"""Input text into an element using AI to determine the value."""...diff --git a/skyvern/core/script_generations/workflow_wrappers.py b/skyvern/core/script_generations/workflow_wrappers.pyindex 81c258882..2a3eea38e 100644--- a/skyvern/core/script_generations/workflow_wrappers.py+++ b/skyvern/core/script_generations/workflow_wrappers.py@@ -21,8 +21,6 @@ def workflow(def cached(cache_key: str) -> Callable:def decorator(func: Callable) -> Callable:- script_run_context_manager.set_cached_fn(cache_key, func)-async def wrapper(page: SkyvernPage, context: RunContext, *args: Any, **kwargs: Any) -> Any:# Store the function in context.cached_fnspage.current_label = cache_key@@ -31,6 +29,8 @@ def cached(cache_key: str) -> Callable:finally:page.current_label = None+ # Register the wrapper (not func) so callers get the label-setting behaviour+ script_run_context_manager.set_cached_fn(cache_key, wrapper)return wrapperreturn decoratordiff --git a/skyvern/forge/prompts/skyvern/script-reviewer-extraction.j2 b/skyvern/forge/prompts/skyvern/script-reviewer-extraction.j2index f47428590..80f11f189 100644--- a/skyvern/forge/prompts/skyvern/script-reviewer-extraction.j2+++ b/skyvern/forge/prompts/skyvern/script-reviewer-extraction.j2@@ -6,7 +6,7 @@ You are a script reviewer for a browser automation system. Your job is to make a3. The existing code is CORRECT — do NOT remove or rewrite existing paths.4. Return ONLY valid Python. No explanations, no comments about what you changed.5. Every `page.classify()` MUST have an `else` branch that calls `await page.element_fallback(navigation_goal="...")` followed by a generic `page.extract()`.-6. Add `text_patterns` derived from the page text in the episode data below.+6. Add `text_patterns` derived from the page text in the episode data below. **NEVER include PII in text_patterns** — no email addresses, no personal names, no account numbers, no customer-specific identifiers. Use generic page structure indicators instead (form labels, button text, section headings, navigation links).7. Keep classify option descriptions short (< 10 words) and mutually exclusive.8. **PARAMETER NAMES**:a. The `{{ "{{ param }}" }}` placeholders in the extraction goal and the **Workflow Parameter Keys** below are the KNOWN parameters. Always use `context.parameters['param']` for these.diff --git a/skyvern/forge/prompts/skyvern/script-reviewer.j2 b/skyvern/forge/prompts/skyvern/script-reviewer.j2index 1d4a2dad3..6d8f0254f 100644--- a/skyvern/forge/prompts/skyvern/script-reviewer.j2+++ b/skyvern/forge/prompts/skyvern/script-reviewer.j2@@ -5,7 +5,7 @@ You are a script reviewer for a browser automation system. Your job is to update2. Use 4-space indentation consistently. Never mix tabs and spaces.3. Return ONLY valid Python. No explanations, no comments about what you changed.4. Every `page.classify()` MUST have an `else` branch that calls `await page.element_fallback(navigation_goal="...")`.-5. Add `text_patterns` derived from the page text in the episode data below.+5. Add `text_patterns` derived from the page text in the episode data below. **NEVER include PII in text_patterns** — no email addresses, no personal names, no account numbers, no customer-specific identifiers. Use generic page structure indicators instead (form labels, button text, section headings, navigation links). Wrong: `"Welcome John Smith, [email protected], Billing & Payments"`. Right: `"Welcome, Billing & Payments, Sign in, Logout"`.6. Keep classify option descriptions short (< 10 words) and mutually exclusive.7. **SEMANTIC SELECTORS**: Use `ai='fallback'` with label-based selectors — NEVER copy xpaths from other branches. Build selectors using `:has-text()` (case-insensitive substring match). Examples: `selector='label:has-text("Full name") input'`, `selector='button:has-text("Submit")'`. If no semantic selector is possible (complex widgets), use `ai='fallback'` with only a `prompt=`.8. **ACCUMULATE SELECTORS**: NEVER remove a working selector. When the same field has different labels across variants (e.g. "Website" vs "URL"), use comma-separated CSS selectors: `selector='label:has-text("Website") input, label:has-text("URL") input'`. The first match wins.diff --git a/skyvern/forge/sdk/api/aws.py b/skyvern/forge/sdk/api/aws.pyindex 878364c3d..a553e0a0d 100644--- a/skyvern/forge/sdk/api/aws.py+++ b/skyvern/forge/sdk/api/aws.py@@ -1,3 +1,5 @@+import io+import timefrom enum import StrEnumfrom mimetypes import add_type, guess_typefrom typing import IO, Any@@ -5,6 +7,7 @@ from urllib.parse import urlparseimport aioboto3import structlog+from botocore.exceptions import ClientErrorfrom types_boto3_batch.client import BatchClientfrom types_boto3_ec2.client import EC2Clientfrom types_boto3_ecs.client import ECSClient@@ -18,6 +21,7 @@ add_type("application/json", ".har")add_type("text/plain", ".log")add_type("application/zstd", ".zst")+_S3_OPERATION_RETRIES = 2LOG = structlog.get_logger()@@ -53,12 +57,60 @@ class AsyncAWSClient:) -> None:self.region_name = region_name or settings.AWS_REGIONself._endpoint_url = endpoint_url+ self._aws_access_key_id = aws_access_key_id+ self._aws_secret_access_key = aws_secret_access_key+ self._profile_name = profile_name+ self._create_session()++ def _create_session(self) -> None:self.session = aioboto3.Session(- aws_access_key_id=aws_access_key_id,- aws_secret_access_key=aws_secret_access_key,- profile_name=profile_name,+ aws_access_key_id=self._aws_access_key_id,+ aws_secret_access_key=self._aws_secret_access_key,+ profile_name=self._profile_name,)+ def refresh_session(self) -> None:+ """Recreate the session to pick up refreshed credentials (e.g., rotated web identity tokens)."""+ LOG.info("Refreshing AWS session to pick up new credentials")+ self._create_session()++ def _is_expired_token_error(self, error: Exception) -> bool:+ """Check if an exception is an AWS ExpiredTokenException."""+ return isinstance(error, ClientError) and error.response.get("Error", {}).get("Code") == "ExpiredTokenException"++ async def _s3_with_retry(+ self,+ op_name: str,+ operation: Any,+ *,+ before_retry: Any | None = None,+ **log_kwargs: Any,+ ) -> Any:+ """Execute an S3 operation with automatic retry on expired AWS token.++ Args:+ op_name: Human-readable name for logging (e.g. "upload", "download").+ operation: Async callable that creates an S3 client and performs the operation.+ before_retry: Optional callable invoked before retrying. Return False to abort the retry.+ **log_kwargs: Extra fields passed to the warning log on retry.++ Raises the original exception on non-token errors or when retry is exhausted / aborted.+ """+ for attempt in range(_S3_OPERATION_RETRIES):+ try:+ return await operation()+ except Exception as e:+ if attempt == 0 and self._is_expired_token_error(e):+ LOG.warning(+ f"AWS token expired during {op_name}, refreshing session and retrying",+ **log_kwargs,+ )+ self.refresh_session()+ if before_retry is not None and before_retry() is False:+ raise+ continue+ raise+def _ecs_client(self) -> ECSClient:return self.session.client(AWSClientType.ECS, region_name=self.region_name, endpoint_url=self._endpoint_url)@@ -130,7 +182,8 @@ class AsyncAWSClient:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/put_object.htmlif storage_class not in S3StorageClass:raise ValueError(f"Invalid storage class: {storage_class}. Must be one of {list(S3StorageClass)}")- try:++ async def _op() -> str:async with self._s3_client() as client:parsed_uri = S3Uri(uri)extra_args = {"Tagging": self._create_tag_string(tags)} if tags else {}@@ -142,6 +195,9 @@ class AsyncAWSClient:**extra_args,)return uri++ try:+ return await self._s3_with_retry("upload", _op, uri=uri)except Exception:LOG.exception("S3 upload failed.", uri=uri)return None@@ -156,7 +212,8 @@ class AsyncAWSClient:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/upload_fileobj.html#upload-fileobjif storage_class not in S3StorageClass:raise ValueError(f"Invalid storage class: {storage_class}. Must be one of {list(S3StorageClass)}")- try:++ async def _op() -> str:async with self._s3_client() as client:parsed_uri = S3Uri(uri)extra_args: dict[str, Any] = {"StorageClass": str(storage_class)}@@ -170,6 +227,17 @@ class AsyncAWSClient:)LOG.debug("Upload file stream success", uri=uri)return uri++ def _rewind_stream() -> bool | None:+ try:+ file_obj.seek(0)+ except (OSError, io.UnsupportedOperation):+ LOG.warning("Cannot rewind stream for retry, failing upload", uri=uri)+ return False+ return None++ try:+ return await self._s3_with_retry("stream upload", _op, before_retry=_rewind_stream, uri=uri)except Exception:LOG.exception("S3 upload stream failed.", uri=uri)return None@@ -185,7 +253,7 @@ class AsyncAWSClient:content_type: str | None = None,) -> None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/upload_file.html- try:+ async def _op() -> None:async with self._s3_client() as client:parsed_uri = S3Uri(uri)extra_args: dict[str, Any] = {"StorageClass": str(storage_class)}@@ -205,6 +273,9 @@ class AsyncAWSClient:Key=parsed_uri.key,ExtraArgs=extra_args,)++ try:+ await self._s3_with_retry("upload", _op, uri=uri)except Exception as e:LOG.exception("S3 upload failed.", uri=uri)if raise_exception:@@ -212,13 +283,14 @@ class AsyncAWSClient:async def download_file(self, uri: str, log_exception: bool = True) -> bytes | None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/get_object.html- try:+ async def _op() -> bytes:async with self._s3_client() as client:parsed_uri = S3Uri(uri)-- # Get full object including bodyresponse = await client.get_object(Bucket=parsed_uri.bucket, Key=parsed_uri.key)return await response["Body"].read()++ try:+ return await self._s3_with_retry("download", _op, uri=uri)except Exception:if log_exception:LOG.exception("S3 download failed", uri=uri)@@ -226,19 +298,25 @@ class AsyncAWSClient:async def delete_file(self, uri: str, log_exception: bool = True) -> None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/delete_object.html- try:+ async def _op() -> None:async with self._s3_client() as client:parsed_uri = S3Uri(uri)await client.delete_object(Bucket=parsed_uri.bucket, Key=parsed_uri.key)++ try:+ await self._s3_with_retry("delete", _op, uri=uri)except Exception:if log_exception:LOG.exception("S3 delete failed", uri=uri)async def get_object_info(self, uri: str) -> dict:- async with self._s3_client() as client:- parsed_uri = S3Uri(uri)- # Only get object metadata without the body- return await client.head_object(Bucket=parsed_uri.bucket, Key=parsed_uri.key)+ async def _op() -> dict:+ async with self._s3_client() as client:+ parsed_uri = S3Uri(uri)+ # Only get object metadata without the body+ return await client.head_object(Bucket=parsed_uri.bucket, Key=parsed_uri.key)++ return await self._s3_with_retry("head_object", _op, uri=uri)async def get_file_metadata(self,@@ -267,8 +345,8 @@ class AsyncAWSClient:async def create_presigned_urls(self, uris: list[str]) -> list[str] | None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/generate_presigned_url.html- presigned_urls = []- try:+ async def _op() -> list[str]:+ presigned_urls = []async with self._s3_client() as client:for uri in uris:parsed_uri = S3Uri(uri)@@ -278,24 +356,29 @@ class AsyncAWSClient:ExpiresIn=settings.PRESIGNED_URL_EXPIRATION,)presigned_urls.append(url)-return presigned_urls++ try:+ return await self._s3_with_retry("presigned URL generation", _op)except Exception:LOG.exception("Failed to create presigned url for S3 objects.", uris=uris)return Noneasync def list_files(self, uri: str) -> list[str]:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/paginator/ListObjectsV2.html- object_keys: list[str] = []- parsed_uri = S3Uri(uri)- async with self._s3_client() as client:- async for page in client.get_paginator("list_objects_v2").paginate(- Bucket=parsed_uri.bucket, Prefix=parsed_uri.key- ):- if "Contents" in page:- for obj in page["Contents"]:- object_keys.append(obj["Key"])- return object_keys+ async def _op() -> list[str]:+ object_keys: list[str] = []+ parsed_uri = S3Uri(uri)+ async with self._s3_client() as client:+ async for page in client.get_paginator("list_objects_v2").paginate(+ Bucket=parsed_uri.bucket, Prefix=parsed_uri.key+ ):+ if "Contents" in page:+ for obj in page["Contents"]:+ object_keys.append(obj["Key"])+ return object_keys++ return await self._s3_with_retry("list_files", _op, uri=uri)async def delete_files(self, bucket: str, keys: list[str]) -> None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/delete_objects.html@@ -309,20 +392,16 @@ class AsyncAWSClient:if not keys:return- try:+ async def _op() -> None:async with self._s3_client() as client:- # Format the objects for the delete_objects callobjects = [{"Key": key} for key in keys]-response = await client.delete_objects(Bucket=bucket,Delete={"Objects": objects,- "Quiet": False, # Set to True to suppress response details+ "Quiet": False,},)-- # Log any errors that occurred during deletionif "Errors" in response:for error in response["Errors"]:LOG.error(@@ -332,6 +411,9 @@ class AsyncAWSClient:code=error.get("Code"),message=error.get("Message"),)++ try:+ await self._s3_with_retry("delete_files", _op, bucket=bucket)except Exception as e:LOG.exception("Failed to delete files from S3", bucket=bucket, keys_count=len(keys))raise e@@ -347,11 +429,15 @@ class AsyncAWSClient:days: Number of days to keep the restored object available (default: 1)tier: Restoration tier - "Standard" (3-5 hours) or "Expedited" (1-5 minutes)"""- try:++ async def _op() -> None:async with self._s3_client() as client:await client.restore_object(Bucket=bucket, Key=key, RestoreRequest={"Days": days, "GlacierJobParameters": {"Tier": tier}})++ try:+ await self._s3_with_retry("restore_object", _op, bucket=bucket, key=key)except Exception as e:LOG.exception("Failed to restore S3 object", bucket=bucket, key=key, tier=tier)raise e@@ -523,10 +609,16 @@ def tag_set_to_dict(tag_set: list[dict[str, str]]) -> dict[str, str]:_aws_client: AsyncAWSClient | None = None+_aws_client_created_at: float = 0.0+_AWS_CLIENT_TTL_SECONDS: float = 45 * 60 # 45 mins - before the 1-hour projected token expirydef get_aws_client() -> AsyncAWSClient:- global _aws_client- if _aws_client is None:+ global _aws_client, _aws_client_created_at+ now = time.monotonic()+ if _aws_client is None or (now - _aws_client_created_at) > _AWS_CLIENT_TTL_SECONDS:+ if _aws_client is not None:+ LOG.info("Recreating AWS client (TTL expired)", ttl_seconds=_AWS_CLIENT_TTL_SECONDS)_aws_client = AsyncAWSClient()+ _aws_client_created_at = nowreturn _aws_clientdiff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.pyindex 1d4f6a9bf..9c2e23f99 100644--- a/skyvern/forge/sdk/core/skyvern_context.py+++ b/skyvern/forge/sdk/core/skyvern_context.py@@ -45,6 +45,7 @@ class SkyvernContext:use_artifact_bundling: bool = False# script run context+ code_version: int | None = Nonescript_id: str | None = Nonescript_revision_id: str | None = Noneaction_order: int = 0diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex ef93545a3..fd08bbf41 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -937,6 +937,16 @@ class WorkflowService:browser_profile_id = workflow_run.browser_profile_idclose_browser_on_completion = browser_session_id is None and not workflow_run.browser_address+ # Guard: if the run was canceled while queued (before Temporal picked it up),+ # don't overwrite the canceled status with running.+ if workflow_run.status == WorkflowRunStatus.canceled:+ LOG.info(+ "Workflow run was canceled before execution started, skipping",+ workflow_run_id=workflow_run_id,+ organization_id=organization_id,+ )+ return workflow_run+# Set workflow run status to running, create workflow run parametersworkflow_run = await self.mark_workflow_run_as_running(workflow_run_id=workflow_run_id)@@ -1282,6 +1292,7 @@ class WorkflowService:context = skyvern_context.ensure_context()context.script_id = script.script_idcontext.script_revision_id = script.script_revision_id+ context.code_version = workflow.code_versiontry:script_blocks = await app.DATABASE.scripts.get_script_blocks_by_script_revision_id(script_revision_id=script.script_revision_id,diff --git a/skyvern/library/skyvern_browser_page_ai.py b/skyvern/library/skyvern_browser_page_ai.pyindex 267aac7f8..a72ea35c9 100644--- a/skyvern/library/skyvern_browser_page_ai.py+++ b/skyvern/library/skyvern_browser_page_ai.py@@ -40,8 +40,16 @@ class SdkSkyvernPageAi(SkyvernPageAi):intention: str,data: str | dict[str, Any] | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None, # noqa: ARG002 — accepted for Protocol compat, no episode recording in library path+ block_label: str | None = None, # noqa: ARG002) -> str | None:- """Click an element using AI via API call."""+ """Click an element using AI via API call.++ Note: failed_selector/block_label are accepted for SkyvernPageAi Protocol+ compatibility but intentionally ignored. The library path (SDK/CLI) lacks+ the workflow context (code_version, workflow_run_id, DB) needed to create+ fallback episodes. Episode recording lives in real_skyvern_page_ai.py.+ """LOG.info("AI click", intention=intention, workflow_run_id=self._browser.workflow_run_id)@@ -69,6 +77,8 @@ class SdkSkyvernPageAi(SkyvernPageAi):totp_identifier: str | None = None,totp_url: str | None = None,timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS,+ failed_selector: str | None = None, # noqa: ARG002 — Protocol compat, see ai_click docstring+ block_label: str | None = None, # noqa: ARG002) -> str:"""Input text into an element using AI via API call."""diff --git a/skyvern/services/script_reviewer.py b/skyvern/services/script_reviewer.pyindex a32a11c8f..3caf933fe 100644--- a/skyvern/services/script_reviewer.py+++ b/skyvern/services/script_reviewer.py@@ -948,6 +948,21 @@ class ScriptReviewer:current_prompt = self._build_retry_prompt(updated_code, proactive_error, function_signature)continue+ # Validate missing selectors on ai='fallback' interaction methods+ missing_selector_error = self._validate_missing_selectors(updated_code)+ if missing_selector_error is not None:+ LOG.warning(+ "ScriptReviewer: missing selector detected, retrying",+ block_label=block_label,+ attempt=attempt,+ error=missing_selector_error,+ )+ if attempt < max_attempts:+ current_prompt = self._build_retry_prompt(+ updated_code, missing_selector_error, function_signature+ )+ continue+# Validate fragile auto-generated selectorsfragile_error = self._validate_fragile_selectors(updated_code)if fragile_error is not None:@@ -2008,6 +2023,48 @@ class ScriptReviewer:f"and only invokes the LLM if the selector fails.")+ def _validate_missing_selectors(self, code: str) -> str | None:+ """Flag interaction methods that have ai='fallback' but no selector= argument.++ When page.click(ai='fallback', prompt='...') has no selector, the CSS-try+ block is skipped entirely and AI click fires as the primary path on every run,+ burning LLM tokens silently. The block "succeeds" via AI so no fallback episode+ is created and the reviewer never gets a signal to fix it.++ Returns an error message or None if no issues found.+ """+ issues: list[str] = []+ lines = code.split("\n")+ i = 0+ while i < len(lines):+ stripped = lines[i].lstrip()+ if stripped.startswith("#"):+ i += 1+ continue+ match = self._PAGE_CALL_RE.search(lines[i])+ if match and match.group(1) in self._INTERACTION_METHODS:+ end_line = self._find_call_end(lines, i)+ call_text = "\n".join(lines[i : end_line + 1]) if end_line > i else lines[i]+ has_fallback = bool(re.search(r"""\bai\s*=\s*['"]fallback['"]""", call_text))+ has_selector = bool(re.search(r"""\bselector\s*=""", call_text))+ if has_fallback and not has_selector:+ issues.append(f"page.{match.group(1)}() on line {i + 1}")+ i = end_line + 1+ else:+ i += 1++ if not issues:+ return None++ return (+ f"Missing selector on interaction methods with ai='fallback': {', '.join(issues[:5])}. "+ f"When ai='fallback' is set but no selector= is provided, the CSS-try block is "+ f"skipped entirely and AI click fires as the PRIMARY path on every run, burning "+ f"LLM tokens silently with no fallback episode created. Add a selector= argument "+ f"with a stable CSS selector (aria-label, placeholder, name, role, :has-text()) "+ f"so the element is found without an LLM call. Keep ai='fallback' as a safety net."+ )+# Known auto-generated ID patterns from popular web frameworks.# These IDs change across deployments/sessions and break cached selectors._FRAGILE_ID_PATTERNS: list[re.Pattern[str]] = [@@ -2089,6 +2146,14 @@ class ScriptReviewer:# but in practice LLM-generated selectors rarely contain such values._DATE_RE: re.Pattern[str] = re.compile(r"\b(?:\d{1,2}/\d{1,2}/\d{4}|\d{4}-\d{2}-\d{2})\b")+ # Regex for email addresses in text_patterns (PII that should not be in cached scripts)+ _EMAIL_RE: re.Pattern[str] = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")++ # Regex to extract text_patterns dict values from page.classify() calls.+ # Matches: text_patterns={"key": "value with PII", ...}+ # Uses a simple approach: find text_patterns={, then extract all string values.+ _TEXT_PATTERN_VALUE_RE: re.Pattern[str] = re.compile(r"""["']([^"']{10,})["']""")+# Regex for :has-text("X") where X is very short (1-2 chars) — likely hardcoded run data_SHORT_HAS_TEXT_RE: re.Pattern[str] = re.compile(r""":has-text\(\s*['"](.{1,2})['"]\s*\)""")@@ -2161,14 +2226,45 @@ class ScriptReviewer:)i = end_line + 1 if match and end_line > i else i + 1+ # Check text_patterns in page.classify() calls for PII (email addresses).+ # text_patterns are baked into the script and stored in S3/DB — PII should not leak.+ tp_start = code.find("text_patterns")+ while tp_start != -1:+ # Find the closing brace of the text_patterns dict+ brace_depth = 0+ tp_end = tp_start+ for j in range(tp_start, len(code)):+ if code[j] == "{":+ brace_depth += 1+ elif code[j] == "}":+ brace_depth -= 1+ if brace_depth == 0:+ tp_end = j + 1+ break+ # Guard: if no brace was found (e.g. text_patterns in a comment or+ # variable name), advance past this occurrence to avoid infinite loop+ if tp_end == tp_start:+ tp_end = tp_start + len("text_patterns")+ tp_block = code[tp_start:tp_end]+ tp_line = code[:tp_start].count("\n") + 1+ # Check for email addresses+ for email_match in self._EMAIL_RE.finditer(tp_block):+ issues.append(+ f"line {tp_line}: text_patterns contains email address '{email_match.group()}' — "+ f"remove PII from text_patterns, use generic page indicators instead"+ )+ break # one email per text_patterns block is enough+ tp_start = code.find("text_patterns", tp_end)+if not issues:return Nonereturn (f"Hardcoded run-specific data detected: {'; '.join(issues[:3])}. "- f"Dates, invoice numbers, and other per-run values must NOT be hardcoded in "- f"selectors or prompts. Use context.parameters['key'] for dynamic values, "- f"or use ai='fallback' with a generic prompt that describes the intent."+ f"Dates, invoice numbers, email addresses, and other per-run or per-user values must NOT "+ f"be hardcoded in selectors, prompts, or text_patterns. Use context.parameters['key'] for "+ f"dynamic values, or use generic page structure indicators (form labels, button text, "+ f"navigation links) in text_patterns.")# Regexes for extracting branch return values from generated conditional code.diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.pyindex 722afbeb1..3ccf76079 100644--- a/skyvern/services/workflow_script_service.py+++ b/skyvern/services/workflow_script_service.py@@ -986,6 +986,213 @@ def _patch_main_py(main_py_content: str, updated_blocks: dict[str, str]) -> str:return "\n".join(lines)+async def _reconstruct_main_py_from_blocks(+ base_main_py: str,+ new_revision_id: str,+ organization_id: str,+ updated_blocks: dict[str, str],+) -> str | None:+ """Reconstruct main.py from the header + individual block files when _patch_main_py fails.++ Takes the header (imports, models, workflow function) from the base main.py+ and appends every block's code — both newly reviewed blocks and existing ones+ carried forward from the base. Each block file was individually compile-verified,+ so concatenation should produce valid Python.++ Returns the reconstructed source, or None if reconstruction fails.+ """+ try:+ # Extract the header: everything before the first @skyvern.cached decorator+ lines = base_main_py.split("\n")+ decorator_pattern = re.compile(r"^@skyvern\.cached\(\s*cache_key\s*=\s*['\"]([^'\"]+)['\"]\s*\)")+ header_end = len(lines)+ for i, line in enumerate(lines):+ if decorator_pattern.match(line.strip()):+ header_end = i+ break+ # Strip trailing blank lines from header+ while header_end > 0 and not lines[header_end - 1].strip():+ header_end -= 1+ header = "\n".join(lines[:header_end])++ # Collect all block code: updated blocks from the reviewer, plus existing+ # blocks from the new revision's block files (which were either freshly+ # uploaded or copied from the base revision)+ block_codes: dict[str, str] = {}++ # Load all block files for this revision from the DB+ block_files = await app.DATABASE.scripts.get_script_files(+ script_revision_id=new_revision_id,+ organization_id=organization_id,+ )+ for f in block_files:+ if not f.file_path.startswith("blocks/") or not f.artifact_id:+ continue+ # Extract label from path: "blocks/login.py" -> "login"+ label = f.file_path.removeprefix("blocks/").removesuffix(".py").removesuffix(".skyvern")+ if label in updated_blocks:+ # Prefer the reviewer's version (it's compile-verified)+ block_codes[label] = updated_blocks[label]+ else:+ # Load existing block content from S3+ artifact = await app.DATABASE.artifacts.get_artifact_by_id(f.artifact_id, organization_id)+ if artifact:+ content = await app.ARTIFACT_MANAGER.retrieve_artifact(artifact)+ if content:+ block_codes[label] = content.decode("utf-8") if isinstance(content, bytes) else content++ # Also include any updated blocks that don't have block files yet (new blocks)+ for label, code in updated_blocks.items():+ if label not in block_codes:+ block_codes[label] = code++ if not block_codes:+ LOG.warning("No block code found for reconstruction")+ return None++ # Assemble: header + all blocks with decorators+ parts = [header, ""]+ for label, code in block_codes.items():+ code = code.rstrip("\n")+ # Hoist any leading imports (same as _patch_main_py does)+ imports, body = _split_imports(code)+ if imports:+ # Inject into header (crude but safe — duplicates are harmless)+ for imp in imports:+ if imp not in header:+ parts.insert(1, imp)+ # Add the block with its decorator if not already present+ if not body.lstrip().startswith("@skyvern.cached"):+ parts.extend(["", f"@skyvern.cached(cache_key = '{label}')", body])+ else:+ parts.extend(["", body])++ reconstructed = "\n".join(parts) + "\n"+ reconstructed = _fix_static_actions_in_for_loops(reconstructed)++ # Final compile check+ compile(reconstructed, "<reconstructed_main.py>", "exec")+ LOG.info(+ "Successfully reconstructed main.py from block files",+ block_count=len(block_codes),+ block_labels=sorted(block_codes.keys()),+ )+ return reconstructed+ except SyntaxError as exc:+ LOG.error(+ "Reconstructed main.py has syntax error — code assembly issue",+ failure_type="syntax_error",+ error=str(exc),+ lineno=exc.lineno,+ )+ return None+ except Exception as exc:+ LOG.exception(+ "Failed to reconstruct main.py — infrastructure issue (S3/DB)",+ failure_type="infra",+ error=str(exc),+ )+ return None+++async def _llm_fix_broken_main_py(+ broken_source: str,+ syntax_error: SyntaxError,+ organization_id: str,+ max_attempts: int = 2,+) -> str | None:+ """Ask an LLM to fix a syntax error in main.py when mechanical reconstruction fails.++ This is a last-resort fix for rare cases where _patch_main_py corrupts the script+ AND reconstruction from block files also fails. The LLM sees the broken source ++ error and makes a targeted syntax fix. Called infrequently, so cost is negligible.++ Returns the fixed source, or None if the LLM can't fix it.+ """+ # Guard against sending very large scripts to the LLM — cap at 50KB+ # (typical scripts are 2-12KB; anything larger suggests an accumulation bug)+ max_source_bytes = 50_000+ if len(broken_source.encode("utf-8")) > max_source_bytes:+ LOG.warning(+ "Skipping LLM fix — script too large",+ source_bytes=len(broken_source.encode("utf-8")),+ max_bytes=max_source_bytes,+ )+ return None++ for attempt in range(1, max_attempts + 1):+ try:+ error_context = (+ f"Line {syntax_error.lineno}: {syntax_error.msg}" if syntax_error.lineno else str(syntax_error)+ )+ prompt = (+ "The following Python script has a syntax error introduced during automated assembly. "+ "Fix ONLY the syntax error. Do not change any logic, selectors, parameters, or function behavior. "+ "Return the complete fixed Python script and nothing else.\n\n"+ f"SYNTAX ERROR: {error_context}\n\n"+ f"BROKEN SCRIPT:\n```python\n{broken_source}\n```"+ )+ response = await app.SCRIPT_REVIEWER_LLM_API_HANDLER(+ prompt=prompt,+ prompt_name="fix-broken-main-py",+ step=None,+ organization_id=organization_id,+ )+ # Extract code from response — log which extraction path fired+ fixed_code: str | None = None+ extraction_path = "none"+ if isinstance(response, str):+ fixed_code = response+ extraction_path = "raw_string"+ elif isinstance(response, dict):+ for key in ("code", "fixed_code", "text"):+ if response.get(key):+ fixed_code = response[key]+ extraction_path = f"dict['{key}']"+ break+ if not fixed_code:+ for k, v in response.items():+ if isinstance(v, str) and "async def " in v:+ fixed_code = v+ extraction_path = f"dict_scan['{k}']"+ break+ if not fixed_code:+ LOG.warning("LLM fix: no code in response", attempt=attempt, extraction_path=extraction_path)+ continue+ LOG.info("LLM fix: extracted code", attempt=attempt, extraction_path=extraction_path)++ # Strip markdown code fences if present+ if "```python" in fixed_code:+ fixed_code = fixed_code.split("```python", 1)[1]+ if "```" in fixed_code:+ fixed_code = fixed_code.split("```", 1)[0]+ elif "```" in fixed_code:+ fixed_code = fixed_code.split("```", 1)[1]+ if "```" in fixed_code:+ fixed_code = fixed_code.split("```", 1)[0]+ fixed_code = fixed_code.strip()++ # Verify the fix compiles+ compile(fixed_code, "<llm-fixed-main.py>", "exec")+ LOG.info(+ "LLM successfully fixed broken main.py",+ attempt=attempt,+ original_error=error_context,+ )+ return fixed_code+ except SyntaxError as new_err:+ LOG.warning(+ "LLM fix still has syntax error",+ attempt=attempt,+ error=str(new_err),+ )+ # Update the error for the next attempt+ syntax_error = new_err+ except Exception:+ LOG.exception("LLM fix attempt failed", attempt=attempt)+ return None++async def _find_main_py_content(script_id: str, organization_id: str, base_revision_id: str) -> str | None:"""Find main.py content, preferring the base revision then falling back to v1.@@ -1156,6 +1363,54 @@ async def create_script_version_from_review(if main_py_content:patched_main = _patch_main_py(main_py_content, updated_blocks)patched_main = _fix_static_actions_in_for_loops(patched_main)++ # Validate the patched main.py compiles. If the splice corrupted it,+ # reconstruct from individual block files (which are all compile-verified+ # by the reviewer) instead of persisting a broken script.+ try:+ compile(patched_main, "<patched_main.py>", "exec")+ except SyntaxError as exc:+ LOG.warning(+ "Patched main.py has syntax error — reconstructing from block files",+ script_id=new_script.script_id,+ error=str(exc),+ lineno=exc.lineno,+ organization_id=organization_id,+ )+ reconstructed = await _reconstruct_main_py_from_blocks(+ base_main_py=main_py_content,+ new_revision_id=new_script.script_revision_id,+ organization_id=organization_id,+ updated_blocks=updated_blocks,+ )+ if reconstructed:+ patched_main = reconstructed+ else:+ # Reconstruction failed too — ask an LLM to fix the syntax error.+ # This is rare and cheap; the LLM sees the full script + error.+ llm_fixed = await _llm_fix_broken_main_py(+ broken_source=patched_main,+ syntax_error=exc,+ organization_id=organization_id,+ )+ if llm_fixed:+ patched_main = llm_fixed+ else:+ # All recovery attempts failed. Delete the workflow script+ # mapping so the next run falls back to AI and regenerates+ # a completely fresh script.+ LOG.error(+ "All main.py recovery attempts failed — deleting workflow script mapping",+ script_id=new_script.script_id,+ organization_id=organization_id,+ workflow_permanent_id=workflow_permanent_id,+ )+ await app.DATABASE.scripts.delete_workflow_scripts_by_permanent_id(+ organization_id=organization_id,+ workflow_permanent_id=workflow_permanent_id,+ )+ return new_script+patched_bytes = patched_main.encode("utf-8")patched_hash = hashlib.sha256(patched_bytes).hexdigest()diff --git a/skyvern/webeye/scraper/domUtils.js b/skyvern/webeye/scraper/domUtils.jsindex af412daf4..413198a0d 100644--- a/skyvern/webeye/scraper/domUtils.js+++ b/skyvern/webeye/scraper/domUtils.js@@ -2837,6 +2837,17 @@ async function getIncrementElements(wait_until_finished = true) {for (let idx = 0; idx < sortedDepth.length; idx++) {const depth = sortedDepth[idx];const treeList = window.globalDomDepthMap.get(depth);+ if (!treeList) {+ _jsConsoleError(+ "getIncrementElements: treeList is undefined for depth",+ depth,+ "mapSize=",+ window.globalDomDepthMap.size,+ "sortedDepth=",+ sortedDepth,+ );+ continue;+ }const removeDupAndConcatChildren = async (element) => {let children = element.children;diff --git a/tests/unit/test_aws_client_ttl.py b/tests/unit/test_aws_client_ttl.pynew file mode 100644index 000000000..b8e38876e--- /dev/null+++ b/tests/unit/test_aws_client_ttl.py@@ -0,0 +1,203 @@+import time+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from botocore.exceptions import ClientError++from skyvern.forge.sdk.api import aws++_EXPIRED_TOKEN_ERROR = ClientError(+ {"Error": {"Code": "ExpiredTokenException", "Message": "Token expired"}},+ "S3Operation",+)++[email protected](autouse=True)+def reset_aws_client():+ """Reset the global singleton before each test."""+ aws._aws_client = None+ aws._aws_client_created_at = 0.0+ yield+ aws._aws_client = None+ aws._aws_client_created_at = 0.0+++def test_get_aws_client_returns_same_instance_within_ttl():+ client1 = aws.get_aws_client()+ client2 = aws.get_aws_client()+ assert client1 is client2+++def test_get_aws_client_recreates_after_ttl():+ client1 = aws.get_aws_client()+ # Simulate TTL expiry by backdating the creation time+ aws._aws_client_created_at = time.monotonic() - (aws._AWS_CLIENT_TTL_SECONDS + 1)+ client2 = aws.get_aws_client()+ assert client1 is not client2+++def test_refresh_session_creates_new_session():+ client = aws.get_aws_client()+ old_session = client.session+ client.refresh_session()+ assert client.session is not old_session+++async def test_upload_file_retries_on_expired_token():+ """upload_file_from_path should refresh the session and retry once on ExpiredTokenException."""+ mock_upload = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, None])++ client = aws.get_aws_client()++ with patch.object(client, "_s3_client") as mock_s3_ctx:+ mock_s3_client = AsyncMock()+ mock_s3_client.upload_file = mock_upload+ mock_s3_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_s3_client)+ mock_s3_ctx.return_value.__aexit__ = AsyncMock(return_value=False)++ with patch.object(client, "refresh_session") as mock_refresh:+ await client.upload_file_from_path(+ uri="s3://test-bucket/test-key.png",+ file_path="/tmp/test.png",+ )+ mock_refresh.assert_called_once()+ assert mock_upload.call_count == 2+++async def test_upload_file_stream_resets_cursor_on_retry():+ """upload_file_stream should seek(0) before retrying to avoid truncated uploads."""+ from io import BytesIO++ mock_upload = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, None])+ file_obj = BytesIO(b"test data")++ client = aws.get_aws_client()++ with patch.object(client, "_s3_client") as mock_s3_ctx:+ mock_s3_client = AsyncMock()+ mock_s3_client.upload_fileobj = mock_upload+ mock_s3_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_s3_client)+ mock_s3_ctx.return_value.__aexit__ = AsyncMock(return_value=False)++ with patch.object(client, "refresh_session"):+ result = await client.upload_file_stream(+ uri="s3://test-bucket/test-key.bin",+ file_obj=file_obj,+ )+ assert result == "s3://test-bucket/test-key.bin"+ assert mock_upload.call_count == 2+++async def test_upload_file_stream_fails_if_stream_not_seekable():+ """Non-seekable streams should not retry (would produce truncated uploads)."""+ import io++ mock_upload = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, None])++ # Create a stream that raises on seek+ class NonSeekableStream(io.RawIOBase):+ def read(self, n=-1):+ return b"test data"++ def seek(self, offset, whence=0):+ raise io.UnsupportedOperation("seek")++ file_obj = NonSeekableStream()+ client = aws.get_aws_client()++ with patch.object(client, "_s3_client") as mock_s3_ctx:+ mock_s3_client = AsyncMock()+ mock_s3_client.upload_fileobj = mock_upload+ mock_s3_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_s3_client)+ mock_s3_ctx.return_value.__aexit__ = AsyncMock(return_value=False)++ with patch.object(client, "refresh_session"):+ result = await client.upload_file_stream(+ uri="s3://test-bucket/test-key.bin",+ file_obj=file_obj,+ )+ assert result is None+ assert mock_upload.call_count == 1+++def _make_s3_client_mock(client_obj: AsyncMock) -> MagicMock:+ """Helper to create a mock _s3_client context manager wrapping a mock boto client."""+ ctx = MagicMock()+ ctx.__aenter__ = AsyncMock(return_value=client_obj)+ ctx.__aexit__ = AsyncMock(return_value=False)+ return ctx+++async def test_upload_file_retries_on_expired_token_put_object():+ """upload_file (put_object) should now retry on expired token."""+ mock_put = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, None])+ client = aws.get_aws_client()++ mock_s3 = AsyncMock()+ mock_s3.put_object = mock_put++ with patch.object(client, "_s3_client", return_value=_make_s3_client_mock(mock_s3)):+ with patch.object(client, "refresh_session") as mock_refresh:+ result = await client.upload_file(+ uri="s3://test-bucket/test-key.png",+ data=b"image bytes",+ )+ mock_refresh.assert_called_once()+ assert mock_put.call_count == 2+ assert result == "s3://test-bucket/test-key.png"+++async def test_download_file_retries_on_expired_token():+ """download_file should retry on expired token."""+ body_mock = AsyncMock()+ body_mock.read = AsyncMock(return_value=b"file contents")+ mock_get = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, {"Body": body_mock}])+ client = aws.get_aws_client()++ mock_s3 = AsyncMock()+ mock_s3.get_object = mock_get++ with patch.object(client, "_s3_client", return_value=_make_s3_client_mock(mock_s3)):+ with patch.object(client, "refresh_session") as mock_refresh:+ result = await client.download_file(uri="s3://test-bucket/test-key.bin")+ mock_refresh.assert_called_once()+ assert mock_get.call_count == 2+ assert result == b"file contents"+++async def test_delete_file_retries_on_expired_token():+ """delete_file should retry on expired token."""+ mock_delete = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, None])+ client = aws.get_aws_client()++ mock_s3 = AsyncMock()+ mock_s3.delete_object = mock_delete++ with patch.object(client, "_s3_client", return_value=_make_s3_client_mock(mock_s3)):+ with patch.object(client, "refresh_session") as mock_refresh:+ await client.delete_file(uri="s3://test-bucket/test-key.bin")+ mock_refresh.assert_called_once()+ assert mock_delete.call_count == 2+++async def test_get_object_info_retries_on_expired_token():+ """get_object_info should retry on expired token."""+ mock_head = AsyncMock(side_effect=[_EXPIRED_TOKEN_ERROR, {"ContentLength": 42}])+ client = aws.get_aws_client()++ mock_s3 = AsyncMock()+ mock_s3.head_object = mock_head++ with patch.object(client, "_s3_client", return_value=_make_s3_client_mock(mock_s3)):+ with patch.object(client, "refresh_session") as mock_refresh:+ result = await client.get_object_info(uri="s3://test-bucket/test-key.bin")+ mock_refresh.assert_called_once()+ assert mock_head.call_count == 2+ assert result == {"ContentLength": 42}diff --git a/tests/unit/test_script_reviewer_quality.py b/tests/unit/test_script_reviewer_quality.pyindex f935f7906..f638269ef 100644--- a/tests/unit/test_script_reviewer_quality.py+++ b/tests/unit/test_script_reviewer_quality.py@@ -322,3 +322,122 @@ async def block_fn(page, context):"""# f-string selectors won't match the date regex in the selector valueassert self.reviewer._validate_hardcoded_run_data(code) is None++ def test_email_in_text_patterns_flagged(self) -> None:+ """Email addresses in text_patterns are PII that should not be in cached scripts."""+ code = """+async def block_fn(page, context):+ state = await page.classify(+ options={"login": "login page", "dashboard": "dashboard"},+ text_patterns={+ "login": "Welcome to Portal, Username, Password, Sign in",+ "dashboard": "Logout, [email protected], John Smith, Billing",+ },+ )+"""+ error = self.reviewer._validate_hardcoded_run_data(code)+ assert error is not None+ assert "email" in error.lower()++ def test_text_patterns_without_email_ok(self) -> None:+ """Generic text_patterns without PII should pass."""+ code = """+async def block_fn(page, context):+ state = await page.classify(+ options={"login": "login page", "dashboard": "dashboard"},+ text_patterns={+ "login": "Welcome, Username, Password, Sign in",+ "dashboard": "Logout, Billing & Payments, Service Management",+ },+ )+"""+ assert self.reviewer._validate_hardcoded_run_data(code) is None+++class TestValidateMissingSelectors:+ """Tests for _validate_missing_selectors."""++ def setup_method(self) -> None:+ self.reviewer = ScriptReviewer()++ def test_fallback_with_selector_is_fine(self) -> None:+ code = """+async def block_fn(page, context):+ await page.click(selector='button:has-text("Submit")', ai='fallback', prompt='submit')+"""+ assert self.reviewer._validate_missing_selectors(code) is None++ def test_fallback_without_selector_flagged(self) -> None:+ """ai='fallback' with no selector= silently uses AI as primary path."""+ code = """+async def block_fn(page, context):+ await page.click(ai='fallback', prompt='Click Billing & Payments')+"""+ error = self.reviewer._validate_missing_selectors(code)+ assert error is not None+ assert "page.click()" in error+ assert "Missing selector" in error++ def test_fill_without_selector_flagged(self) -> None:+ code = """+async def block_fn(page, context):+ await page.fill(ai='fallback', prompt='Enter username', value='test')+"""+ error = self.reviewer._validate_missing_selectors(code)+ assert error is not None+ assert "page.fill()" in error++ def test_no_ai_arg_not_flagged(self) -> None:+ """Calls without ai='fallback' are not our concern here."""+ code = """+async def block_fn(page, context):+ await page.click(prompt='Click something')+"""+ assert self.reviewer._validate_missing_selectors(code) is None++ def test_proactive_without_selector_not_flagged(self) -> None:+ """ai='proactive' without selector is caught by _validate_proactive_misuse, not this validator."""+ code = """+async def block_fn(page, context):+ await page.click(ai='proactive', prompt='Click something')+"""+ assert self.reviewer._validate_missing_selectors(code) is None++ def test_multiline_call_flagged(self) -> None:+ code = """+async def block_fn(page, context):+ await page.click(+ ai='fallback',+ prompt='Click Billing & Payments',+ )+"""+ error = self.reviewer._validate_missing_selectors(code)+ assert error is not None+ assert "page.click()" in error++ def test_multiline_with_selector_ok(self) -> None:+ code = """+async def block_fn(page, context):+ await page.click(+ selector='a:has-text("Billing")',+ ai='fallback',+ prompt='Click billing link',+ )+"""+ assert self.reviewer._validate_missing_selectors(code) is None++ def test_comments_ignored(self) -> None:+ code = """+async def block_fn(page, context):+ # await page.click(ai='fallback', prompt='old code')+ await page.click(selector='button', ai='fallback', prompt='submit')+"""+ assert self.reviewer._validate_missing_selectors(code) is None++ def test_non_interaction_methods_ignored(self) -> None:+ """Methods like page.wait, page.complete are not interaction methods."""+ code = """+async def block_fn(page, context):+ await page.wait(ai='fallback', prompt='wait for page')+"""+ assert self.reviewer._validate_missing_selectors(code) is None