Skip to content

Automate browser based workflows with AI

Add script generation plumbing for adaptive caching (6/8) (#4920)

pedrohsdb0fe0a4921dc0
diff --git a/skyvern/core/script_generations/generate_script.py b/skyvern/core/script_generations/generate_script.py
index 6274e15f4..9c40d1191 100644
--- a/skyvern/core/script_generations/generate_script.py
+++ b/skyvern/core/script_generations/generate_script.py
@@ -42,6 +42,7 @@ class ScriptBlockSource:
workflow_run_id: str | None
workflow_run_block_id: str | None
input_fields: list[str] | None
+ requires_agent: bool | None = None
def _build_existing_field_assignments(
@@ -180,6 +181,50 @@ ACTIONS_WITH_XPATH = [
"upload_file",
"select_option",
]
+
+
+def _build_semantic_selector(act: dict[str, Any]) -> str | None:
+ """Build a semantic CSS selector from element data.
+
+ Priority order:
+ 1. aria-label (most reliable semantic identifier)
+ 2. placeholder (for inputs)
+ 3. name attribute (common form field identifier)
+ 4. text content (for buttons, links)
+ 5. Fall back to None (use ai='fallback' with only prompt=)
+ """
+ element_data = act.get("skyvern_element_data") or {}
+ attrs = element_data.get("attributes", {})
+ tag = element_data.get("tagName", "")
+ text = (element_data.get("text") or "").strip()
+
+ # Try aria-label first
+ aria_label = attrs.get("aria-label", "")
+ if aria_label:
+ escaped = aria_label.replace('"', '\\"')
+ return f'{tag}[aria-label="{escaped}"]' if tag else f'[aria-label="{escaped}"]'
+
+ # Try placeholder (for input/textarea)
+ placeholder = attrs.get("placeholder", "")
+ if placeholder and tag in ("input", "textarea"):
+ escaped = placeholder.replace('"', '\\"')
+ return f'{tag}[placeholder="{escaped}"]'
+
+ # Try name attribute
+ name = attrs.get("name", "")
+ if name and tag in ("input", "textarea", "select"):
+ escaped = name.replace('"', '\\"')
+ return f'{tag}[name="{escaped}"]'
+
+ # Try text content (for buttons, links, labels)
+ if text and tag in ("button", "a"):
+ short_text = text[:50].replace('"', '\\"')
+ return f'{tag}:has-text("{short_text}")'
+
+ # No good semantic selector — return None (caller will use ai-only mode)
+ return None
+
+
ACTIONS_OPT_OUT_INTENTION_FOR_PROMPT = ["extract"]
INDENT = " " * 4
@@ -562,6 +607,7 @@ def _action_to_stmt(
task: dict[str, Any],
assign_to_output: bool = False,
value_to_param: dict[str, str] | None = None,
+ use_semantic_selectors: bool = False,
) -> cst.BaseStatement:
"""
Turn one Action dict into:
@@ -580,22 +626,41 @@ def _action_to_stmt(
args: list[cst.Arg] = []
if method in ACTIONS_WITH_XPATH:
- args.append(
- cst.Arg(
- keyword=cst.Name("selector"),
- value=_value(f"xpath={act['xpath']}"),
- whitespace_after_arg=cst.ParenthesizedWhitespace(
- indent=True,
- last_line=cst.SimpleWhitespace(INDENT),
- ),
+ if use_semantic_selectors:
+ semantic = _build_semantic_selector(act)
+ if semantic:
+ args.append(
+ cst.Arg(
+ keyword=cst.Name("selector"),
+ value=_value(semantic),
+ whitespace_after_arg=cst.ParenthesizedWhitespace(
+ indent=True,
+ last_line=cst.SimpleWhitespace(INDENT),
+ ),
+ )
+ )
+ # If no semantic selector, skip selector arg — ai with prompt= handles it
+ else:
+ args.append(
+ cst.Arg(
+ keyword=cst.Name("selector"),
+ value=_value(f"xpath={act['xpath']}"),
+ whitespace_after_arg=cst.ParenthesizedWhitespace(
+ indent=True,
+ last_line=cst.SimpleWhitespace(INDENT),
+ ),
+ )
)
- )
if method == "click":
- ai_mode = GENERATE_CODE_AI_MODE_PROACTIVE
- click_context = act.get("click_context")
- if click_context and isinstance(click_context, dict) and click_context.get("single_option_click"):
+ if use_semantic_selectors:
+ # With semantic selectors, try selector first, AI only if miss
ai_mode = GENERATE_CODE_AI_MODE_FALLBACK
+ else:
+ ai_mode = GENERATE_CODE_AI_MODE_PROACTIVE
+ click_context = act.get("click_context")
+ if click_context and isinstance(click_context, dict) and click_context.get("single_option_click"):
+ ai_mode = GENERATE_CODE_AI_MODE_FALLBACK
args.append(
cst.Arg(
keyword=cst.Name("ai"),
@@ -954,11 +1019,139 @@ def _collect_block_input_fields(
return all_fields
+def _is_form_filling_block(
+ actions: list[dict[str, Any]],
+) -> bool:
+ """Check if a block's actions indicate a form-filling pattern (4+ input/select actions)."""
+ form_action_count = sum(
+ 1 for a in actions if a.get("action_type") in (ActionType.INPUT_TEXT, ActionType.SELECT_OPTION)
+ )
+ return form_action_count >= 4
+
+
+def _build_form_filling_block_fn(
+ block: dict[str, Any],
+ actions: list[dict[str, Any]],
+ value_to_param: dict[str, str] | None = None,
+) -> FunctionDef:
+ """Generate a FIELD_MAP-based block function from V1 actions.
+
+ Instead of procedural page.fill()/page.select_option() calls, produces a
+ FIELD_MAP dictionary + page.fill_form() call for dynamic form filling.
+ """
+ name = _safe_name(block.get("label") or block.get("title") or f"block_{block.get('workflow_run_block_id')}")
+ cache_key = block.get("label") or block.get("title") or f"block_{block.get('workflow_run_block_id')}"
+
+ # Build FIELD_MAP entries from actions
+ field_map_lines: list[str] = []
+ seen_keys: set[str] = set()
+
+ navigation_goal = block.get("navigation_goal") or "Fill out the form"
+
+ for act in actions:
+ action_type = act.get("action_type")
+ if action_type not in (ActionType.INPUT_TEXT, ActionType.SELECT_OPTION):
+ continue
+
+ intention = act.get("intention") or act.get("reasoning") or ""
+ # field_name is set by hydrate_input_text_actions_with_field_names()
+ # but may not match actual workflow parameter keys. Use value_to_param
+ # to look up the real parameter key from the literal value.
+ field_name = act.get("field_name")
+
+ # Look up the actual workflow parameter key via the literal action value
+ actual_param: str | None = None
+ if value_to_param:
+ if action_type == ActionType.INPUT_TEXT:
+ literal_value = act.get("text", "")
+ elif action_type == ActionType.SELECT_OPTION:
+ option = act.get("option", {})
+ literal_value = option.get("value") or option.get("label") or ""
+ else:
+ literal_value = ""
+ if literal_value and literal_value in value_to_param:
+ actual_param = value_to_param[literal_value]
+
+ # Determine the snake_case key for this field
+ if field_name:
+ key = sanitize_variable_name(field_name)
+ elif intention:
+ key = sanitize_variable_name(intention[:40])
+ else:
+ continue
+
+ if key in seen_keys:
+ continue
+ seen_keys.add(key)
+
+ # Determine action type for FIELD_MAP
+ if action_type == ActionType.SELECT_OPTION:
+ fm_action = "select"
+ else:
+ fm_action = "fill"
+
+ # Build the label from intention/reasoning
+ label = intention.lower().strip() if intention else key.replace("_", " ")
+ if len(label) > 60:
+ label = label[:60]
+
+ # Use actual_param (from value_to_param lookup) if found, else fall back to field_name.
+ # If neither exists, this field has no parameter → use ai='proactive'.
+ param = actual_param or field_name
+ param_str = repr(param) if param else "None"
+ ai_str = repr("proactive") if not param else repr("fallback")
+ prompt = intention or f"Fill the {key.replace('_', ' ')} field"
+
+ entry = (
+ f" {repr(key)}: {{\n"
+ f' "param": {param_str},\n'
+ f' "action": {repr(fm_action)},\n'
+ f' "ai": {ai_str},\n'
+ f' "labels": [{repr(label)}],\n'
+ f' "prompt": {repr(prompt)},\n'
+ f" }},"
+ )
+ field_map_lines.append(entry)
+
+ # Build the function body as a string, then parse it
+ field_map_body = "\n".join(field_map_lines)
+
+ # Include page.goto(url) if the block has a URL, just like _build_block_fn does
+ goto_line = ""
+ if block.get("url"):
+ goto_line = f" await page.goto({repr(block['url'])})\n"
+
+ func_code = (
+ f"async def {name}(page: SkyvernPage, context: RunContext):\n"
+ f"{goto_line}"
+ f" FIELD_MAP = {{\n"
+ f"{field_map_body}\n"
+ f" }}\n"
+ f" await page.fill_form(FIELD_MAP, context, navigation_goal={repr(navigation_goal)})\n"
+ f" if not await page.structural_validate():\n"
+ f" await page.complete()\n"
+ )
+
+ # Parse and add decorator
+ parsed = cst.parse_module(func_code)
+ func_def = parsed.body[0]
+ assert isinstance(func_def, FunctionDef)
+
+ # Add the @skyvern.cached decorator
+ decorated = func_def.with_changes(decorators=[_make_decorator(cache_key, block)])
+ return decorated
+
+
def _build_block_fn(
block: dict[str, Any],
actions: list[dict[str, Any]],
value_to_param: dict[str, str] | None = None,
+ use_semantic_selectors: bool = False,
) -> FunctionDef:
+ # Check if this block is form-filling (4+ input_text/select actions)
+ if use_semantic_selectors and _is_form_filling_block(actions):
+ return _build_form_filling_block_fn(block, actions, value_to_param)
+
name = _safe_name(block.get("label") or block.get("title") or f"block_{block.get('workflow_run_block_id')}")
cache_key = block.get("label") or block.get("title") or f"block_{block.get('workflow_run_block_id')}"
body_stmts: list[cst.BaseStatement] = []
@@ -979,7 +1172,15 @@ def _build_block_fn(
# For extraction blocks, assign extract action results to output variable
assign_to_output = act["action_type"] == "extract"
- body_stmts.append(_action_to_stmt(act, block, assign_to_output=assign_to_output, value_to_param=value_to_param))
+ body_stmts.append(
+ _action_to_stmt(
+ act,
+ block,
+ assign_to_output=assign_to_output,
+ value_to_param=value_to_param,
+ use_semantic_selectors=use_semantic_selectors,
+ )
+ )
# add complete action
block_type = block.get("block_type")
@@ -2325,6 +2526,8 @@ async def generate_workflow_script_python_code(
pending: bool = False,
cached_blocks: dict[str, ScriptBlockSource] | None = None,
updated_block_labels: set[str] | None = None,
+ use_semantic_selectors: bool = False,
+ adaptive_caching: bool = False,
) -> str:
"""
Build a LibCST Module and emit .code (PEP-8-formatted source).
@@ -2436,12 +2639,17 @@ async def generate_workflow_script_python_code(
block_workflow_run_block_id = cached_source.workflow_run_block_id
else:
block_fn_def = _build_block_fn(
- task, actions_by_task.get(task.get("task_id", ""), []), value_to_param=value_to_param
+ task,
+ actions_by_task.get(task.get("task_id", ""), []),
+ value_to_param=value_to_param,
+ use_semantic_selectors=use_semantic_selectors,
)
temp_module = cst.Module(body=[block_fn_def])
block_code = temp_module.code
- block_stmt = _build_block_statement(task, value_to_param=value_to_param)
+ # run_signature is executed in a scope without `context`, so don't
+ # parameterize the prompt — use literal strings instead.
+ block_stmt = _build_block_statement(task, value_to_param=None)
run_signature_module = cst.Module(body=[block_stmt])
run_signature = run_signature_module.code.strip()
@@ -2499,6 +2707,7 @@ async def generate_workflow_script_python_code(
child_block,
actions_by_task.get(child_block.get("task_id", ""), []),
value_to_param=value_to_param,
+ use_semantic_selectors=use_semantic_selectors,
)
task_v2_block_body.append(cst.EmptyLine())
task_v2_block_body.append(cst.EmptyLine())
@@ -2507,7 +2716,7 @@ async def generate_workflow_script_python_code(
temp_module = cst.Module(body=task_v2_block_body)
block_code = temp_module.code
- task_v2_stmt = _build_block_statement(task_v2, value_to_param=value_to_param)
+ task_v2_stmt = _build_block_statement(task_v2, value_to_param=None)
run_signature = cst.Module(body=[task_v2_stmt]).code.strip()
if script_id and script_revision_id and organization_id:
@@ -2577,6 +2786,56 @@ async def generate_workflow_script_python_code(
# at module level ("async for outside async function"). The for-loop code is
# already correctly inlined inside run_workflow() via _build_block_statement().
+ # --- agent-required blocks (adaptive caching) -----------------------
+ # Structural blocks (conditional, text_prompt, wait) can't be code-generated
+ # initially. Create script_block entries with requires_agent=True so the runtime
+ # knows to execute them via agent even when ai_fallback=False. The script reviewer
+ # can later upgrade these to code by setting requires_agent=False and providing
+ # a run_signature.
+ _AGENT_REQUIRED_BLOCK_TYPES = {"conditional", "text_prompt", "wait"}
+ if adaptive_caching:
+ agent_required_blocks = [b for b in blocks if b["block_type"] in _AGENT_REQUIRED_BLOCK_TYPES]
+ for arb in agent_required_blocks:
+ arb_label = arb.get("label") or f"{arb['block_type']}_{arb.get('workflow_run_block_id')}"
+ # Check if the reviewer has already provided code for this block
+ cached_source = cached_blocks.get(arb_label)
+ if cached_source and cached_source.run_signature and not cached_source.requires_agent:
+ # Reviewer upgraded this block to code — preserve it
+ if script_id and script_revision_id and organization_id:
+ try:
+ await create_or_update_script_block(
+ block_code=cached_source.code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=arb_label,
+ update=pending,
+ run_signature=cached_source.run_signature,
+ workflow_run_id=cached_source.workflow_run_id,
+ workflow_run_block_id=cached_source.workflow_run_block_id,
+ requires_agent=False,
+ )
+ except Exception as e:
+ LOG.error("Failed to preserve coded agent block", error=str(e), exc_info=True)
+ append_block_code(cached_source.code)
+ else:
+ # Create a requires_agent entry (no code, no run_signature)
+ placeholder_code = f"# Block '{arb_label}' ({arb['block_type']}) — executed via agent"
+ if script_id and script_revision_id and organization_id:
+ try:
+ await create_or_update_script_block(
+ block_code=placeholder_code,
+ script_revision_id=script_revision_id,
+ script_id=script_id,
+ organization_id=organization_id,
+ block_label=arb_label,
+ update=pending,
+ run_signature=None,
+ requires_agent=True,
+ )
+ except Exception as e:
+ LOG.error("Failed to create agent-required script block", error=str(e), exc_info=True)
+
# --- preserve cached blocks from unexecuted branches ----------------
# When a workflow has conditional blocks, not all branches execute in a single run.
# transform_workflow_run_to_code_gen_input() only returns blocks that executed,
@@ -2593,6 +2852,10 @@ async def generate_workflow_script_python_code(
for flb in for_loop_blocks:
label = flb.get("label") or f"for_loop_{flb.get('workflow_run_block_id')}"
processed_labels.add(label)
+ if adaptive_caching:
+ for arb in [b for b in blocks if b["block_type"] in _AGENT_REQUIRED_BLOCK_TYPES]:
+ arb_label = arb.get("label") or f"{arb['block_type']}_{arb.get('workflow_run_block_id')}"
+ processed_labels.add(arb_label)
preserved_count = 0
for cached_label, cached_source in cached_blocks.items():
@@ -2710,6 +2973,7 @@ async def create_or_update_script_block(
workflow_run_id: str | None = None,
workflow_run_block_id: str | None = None,
input_fields: list[str] | None = None,
+ requires_agent: bool | None = None,
) -> None:
"""
Create a script block in the database and save the block code to a script file.
@@ -2726,6 +2990,7 @@ async def create_or_update_script_block(
workflow_run_id: The workflow run that generated this cached block
workflow_run_block_id: The workflow run block that generated this cached block
input_fields: Workflow parameter field names referenced by this block's cached actions
+ requires_agent: Whether this block must be executed via agent (None = don't change on update)
"""
block_code_bytes = block_code if isinstance(block_code, bytes) else block_code.encode("utf-8")
try:
@@ -2745,8 +3010,12 @@ async def create_or_update_script_block(
workflow_run_id=workflow_run_id,
workflow_run_block_id=workflow_run_block_id,
input_fields=input_fields,
+ requires_agent=requires_agent if requires_agent is not None else False,
)
- elif any(value is not None for value in [run_signature, workflow_run_id, workflow_run_block_id, input_fields]):
+ elif any(
+ value is not None
+ for value in [run_signature, workflow_run_id, workflow_run_block_id, input_fields, requires_agent]
+ ):
# Update metadata when new values are provided
script_block = await app.DATABASE.update_script_block(
script_block_id=script_block.script_block_id,
@@ -2755,6 +3024,7 @@ async def create_or_update_script_block(
workflow_run_id=workflow_run_id,
workflow_run_block_id=workflow_run_block_id,
input_fields=input_fields,
+ requires_agent=requires_agent,
)
# Step 4: Create script file for the block
diff --git a/skyvern/core/script_generations/run_initializer.py b/skyvern/core/script_generations/run_initializer.py
index 0d0250039..1a529ca7a 100644
--- a/skyvern/core/script_generations/run_initializer.py
+++ b/skyvern/core/script_generations/run_initializer.py
@@ -1,18 +1,124 @@
+import hashlib
from typing import Any
+import structlog
+from cachetools import TTLCache
from pydantic import BaseModel
+from skyvern.core.script_generations.canonical_fields import CANONICAL_CATEGORIES
from skyvern.core.script_generations.script_skyvern_page import ScriptSkyvernPage, script_run_context_manager
from skyvern.core.script_generations.skyvern_page import RunContext, SkyvernPage
from skyvern.forge import app
+from skyvern.forge.prompts import prompt_engine
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.workflow.models.parameter import WorkflowParameterType
+LOG = structlog.get_logger()
+
+# Cache for LLM-extracted canonical values. Key: hash of (text_blobs + category names).
+# TTL of 1 hour, max 256 entries. Identical inputs across runs skip the LLM call.
+_extraction_cache: TTLCache[str, dict[str, str | None]] = TTLCache(maxsize=256, ttl=3600)
+
+
+async def _extract_canonical_values(parameters: dict[str, Any]) -> dict[str, str | None]:
+ """Extract canonical field values from ALL parameters — no magic names.
+
+ Strategy:
+ 1. Direct mapping: if a parameter key matches a canonical category's param
+ name (e.g., "email" → email category), use the value directly. No LLM needed.
+ 2. LLM extraction: for long text blobs (>100 chars), run a single LLM call
+ to extract remaining canonical values.
+
+ Returns a dict keyed by canonical category name -> extracted value (or None).
+ """
+ result: dict[str, str | None] = {}
+ param_to_category = {c.param: c.name for c in CANONICAL_CATEGORIES if c.param}
+ text_blobs: list[str] = []
+
+ for key, value in parameters.items():
+ if not isinstance(value, str) or not value:
+ continue
+
+ # Direct mapping: parameter key matches a canonical category's param
+ if key in param_to_category:
+ result[param_to_category[key]] = value
+ continue
+
+ # Collect long text values for LLM extraction
+ if len(value) > 100:
+ text_blobs.append(value)
+
+ # LLM extraction for remaining categories
+ if text_blobs:
+ remaining_cats = [c for c in CANONICAL_CATEGORIES if c.name not in result]
+ if remaining_cats:
+ combined_text = "\n\n".join(text_blobs)
+ llm_result = await _llm_extract_from_text(combined_text, remaining_cats)
+ result.update(llm_result)
+
+ LOG.info(
+ "extract_canonical_values: complete",
+ direct_count=sum(1 for k, v in result.items() if v is not None and k in param_to_category.values()),
+ llm_count=sum(1 for v in result.values() if v is not None)
+ - sum(1 for k, v in result.items() if v is not None and k in param_to_category.values()),
+ total_count=sum(1 for v in result.values() if v is not None),
+ )
+ return result
+
+
+async def _llm_extract_from_text(
+ input_text: str,
+ categories: list[Any],
+) -> dict[str, str | None]:
+ """Extract canonical field values from a text blob using a single LLM call.
+
+ Results are cached by hash of (input_text + category names) so identical
+ inputs across runs skip the LLM call entirely.
+
+ Returns a dict keyed by canonical category name -> extracted value (or None).
+ """
+ # Build cache key from input text + sorted category names
+ cat_names_str = ",".join(sorted(c.name for c in categories))
+ cache_key = hashlib.sha256(f"{input_text}|{cat_names_str}".encode()).hexdigest()
+
+ cached = _extraction_cache.get(cache_key)
+ if cached is not None:
+ LOG.info("llm_extract_from_text: cache hit, skipping LLM call", cache_key=cache_key[:12])
+ return cached
+
+ cat_descs = [{"name": c.name, "prompt": c.prompt} for c in categories]
+
+ prompt_text = prompt_engine.load_prompt(
+ template="extract-applicant-parameters",
+ input_text=input_text,
+ categories=cat_descs,
+ )
+
+ try:
+ skyvern_ctx = skyvern_context.current()
+ org_id = skyvern_ctx.organization_id if skyvern_ctx else None
+ result = await app.SECONDARY_LLM_API_HANDLER(
+ prompt=prompt_text,
+ prompt_name="extract-applicant-parameters",
+ organization_id=org_id,
+ )
+ if isinstance(result, dict):
+ valid_names = {c.name for c in categories}
+ filtered = {k: (str(v) if v is not None else None) for k, v in result.items() if k in valid_names}
+ LOG.info("llm_extract_from_text: extracted values", count=sum(1 for v in filtered.values() if v))
+ _extraction_cache[cache_key] = filtered
+ return filtered
+ except Exception as e:
+ LOG.warning("llm_extract_from_text: LLM call failed, skipping", error_type=type(e).__name__, exc_info=True)
+
+ return {}
+
async def setup(
parameters: dict[str, Any],
generated_parameter_cls: type[BaseModel] | None = None,
browser_session_id: str | None = None,
+ adaptive_caching: bool = False,
) -> tuple[SkyvernPage, RunContext]:
# transform any secrets/credential parameters. For example, if there's only one credential in the parameters: {"cred_12345": "cred_12345"},
# it should be transformed to {"cred_12345": {"username": "secret_5fBoa_username", "password": "secret_5fBoa_password"}}
@@ -28,31 +134,19 @@ async def setup(
if parameter.workflow_parameter_type == WorkflowParameterType.CREDENTIAL_ID:
parameters[key] = workflow_run_context.values[key]
context.script_run_parameters.update(parameters)
+
+ # Pre-extract structured values from all parameters (1 LLM call max, cached for entire run)
+ if adaptive_caching:
+ extracted_params = await _extract_canonical_values(parameters)
+ else:
+ extracted_params = {}
+
skyvern_page = await ScriptSkyvernPage.create(browser_session_id=browser_session_id)
run_context = RunContext(
parameters=parameters,
page=skyvern_page,
- # TODO: generate all parameters with llm here - then we can skip generating input text one by one in the fill/type methods
generated_parameters=generated_parameter_cls().model_dump() if generated_parameter_cls else None,
+ extracted_params=extracted_params,
)
script_run_context_manager.set_run_context(run_context)
return skyvern_page, run_context
-
-
-# async def transform_parameters(parameters: dict[str, Any] | BaseModel | None = None, generated_parameter_cls: type[BaseModel] | None = None) -> dict[str, Any] | None:
-# if parameters is None:
-# return None
-
-# if generated_parameter_cls:
-# if isinstance(parameters, dict):
-# # TODO: use llm to generate
-# return generated_parameter_cls.model_validate(parameters)
-# if isinstance(parameters, BaseModel):
-# return parameters
-# return generated_parameter_cls.model_validate(parameters)
-# else:
-# if isinstance(parameters, dict):
-# return parameters
-# if isinstance(parameters, BaseModel):
-# return parameters.model_dump()
-# return parameters
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index 98e3019df..5b624fd64 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -679,7 +679,9 @@ async def _update_workflow_block(
failure_reason = str(billing_error)
final_output = None
else:
- final_output = None
+ # Non-task blocks (conditionals, etc.) — preserve the output as-is.
+ # final_output is already set to `output` at line 596.
+ pass
await app.DATABASE.update_workflow_run_block(
workflow_run_block_id=workflow_run_block_id,
@@ -1244,6 +1246,9 @@ async def _regenerate_script_block_after_ai_fallback(
2. create a completely new script, with only the current block's script being different as it's newly generated.
-
"""
+ # TODO: Re-enable inline script regeneration after the script reviewer (workflow_script_service)
+ # handles post-run review. This code path is intentionally disabled — the reviewer-based approach
+ # in workflow_script_service.py is the preferred mechanism for script improvement.
LOG.info("skipping script regeneration after AI fallback")
return None
try:
@@ -1540,6 +1545,12 @@ async def run_task(
context = skyvern_context.ensure_context()
context.prompt = prompt
try:
+ # Navigate to the target URL before running cached code, just like
+ # NavigationBlock does in the non-cached path.
+ if url:
+ run_context = script_run_context_manager.ensure_run_context()
+ await run_context.page.goto(url)
+
await _prepare_cached_block_inputs(cache_key, prompt)
output = await _run_cached_function(cached_fn)
@@ -1928,6 +1939,66 @@ async def extract(
return block_result.output_parameter_value
+async def conditional(
+ label: str,
+) -> dict[str, Any]:
+ """Evaluate a conditional block using cached Python code instead of an LLM call.
+
+ The cached function (registered via @skyvern.cached) evaluates the branch condition
+ in pure Python and returns {"next_block_label": "...", "branch_index": N}.
+ """
+ cached_fn = script_run_context_manager.get_cached_fn(label)
+ if not cached_fn:
+ raise Exception(f"No cached function for conditional block '{label}'")
+
+ # Create workflow run block entry (no task needed for conditional blocks)
+ workflow_run_block_id, _, _ = await _create_workflow_block_run_and_task(
+ block_type=BlockType.CONDITIONAL,
+ label=label,
+ )
+
+ # Inject upstream block outputs into run_context.parameters so the cached
+ # function can access them (e.g., context.parameters["extract_docs_output"])
+ run_context = script_run_context_manager.ensure_run_context()
+ context = skyvern_context.current()
+ if context and context.workflow_run_id:
+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(context.workflow_run_id)
+ for key, value in workflow_run_context.values.items():
+ if key not in run_context.parameters:
+ run_context.parameters[key] = value
+
+ try:
+ result = await cached_fn(page=run_context.page, context=run_context)
+ if not isinstance(result, dict) or "next_block_label" not in result:
+ raise Exception(f"Conditional function '{label}' must return dict with 'next_block_label', got: {result}")
+
+ # Build branch_metadata in the format ConditionalBlock.execute() produces
+ branch_metadata: dict[str, Any] = {
+ "branch_taken": result.get("next_block_label"),
+ "branch_index": result.get("branch_index"),
+ "next_block_label": result.get("next_block_label"),
+ }
+
+ if workflow_run_block_id:
+ await _update_workflow_block(
+ workflow_run_block_id,
+ BlockStatus.completed,
+ output=branch_metadata,
+ label=label,
+ )
+ return branch_metadata
+
+ except Exception:
+ if workflow_run_block_id:
+ await _update_workflow_block(
+ workflow_run_block_id,
+ BlockStatus.failed,
+ failure_reason="Conditional code evaluation failed",
+ label=label,
+ )
+ raise
+
+
async def validate(
complete_criterion: str | None = None,
terminate_criterion: str | None = None,
diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.py
index 8000aa0f4..8819c4412 100644
--- a/skyvern/services/workflow_script_service.py
+++ b/skyvern/services/workflow_script_service.py
@@ -1,4 +1,8 @@
import base64
+import hashlib
+import re
+import urllib.parse
+from typing import Any
import structlog
from cachetools import TTLCache
@@ -19,6 +23,45 @@ LOG = structlog.get_logger()
jinja_sandbox_env = SandboxedEnvironment()
+def _jinja_domain_filter(url: str) -> str:
+ """Extract the domain (netloc) from a URL for use as a cache key grouping.
+
+ Examples:
+ "https://jobs.lever.co/textnow/abc" → "jobs.lever.co"
+ "https://boards.greenhouse.io/robinhood/jobs/123" → "boards.greenhouse.io"
+ """
+ try:
+ return urllib.parse.urlparse(str(url)).netloc or str(url)
+ except Exception:
+ return str(url)
+
+
+jinja_sandbox_env.filters["domain"] = _jinja_domain_filter
+
+
+def _extract_first_block_domain(workflow: Workflow, parameters: dict[str, Any]) -> str:
+ """Extract the domain from the first block that has a URL field.
+
+ Used to automatically enrich the cache key with the target domain so that
+ the same workflow running against different sites gets separate cached scripts.
+ Returns empty string if no block URL is found.
+ """
+ try:
+ blocks = get_all_blocks(workflow.workflow_definition.blocks)
+ for block in blocks:
+ url_template = getattr(block, "url", None)
+ if not url_template:
+ continue
+ rendered_url = jinja_sandbox_env.from_string(str(url_template)).render(parameters)
+ if rendered_url:
+ domain = _jinja_domain_filter(rendered_url)
+ if domain:
+ return domain
+ except Exception:
+ pass
+ return ""
+
+
def workflow_has_conditionals(workflow: Workflow) -> bool:
"""
Check if a workflow contains any conditional blocks.
@@ -147,6 +190,16 @@ async def get_workflow_script(
rendered_cache_key_value = jinja_sandbox_env.from_string(cache_key).render(parameters)
+ # Auto-enrich with domain when using the default cache key.
+ # This ensures the same workflow running against different sites gets
+ # separate cached scripts (e.g., "default:fanr.gov.ae" vs "default:search.gov.hk").
+ if rendered_cache_key_value in ("default", ""):
+ domain = _extract_first_block_domain(workflow, parameters)
+ if domain:
+ rendered_cache_key_value = (
+ f"{rendered_cache_key_value}:{domain}" if rendered_cache_key_value else domain
+ )
+
if block_labels:
# Do not generate script or run script if block_labels is provided
return None, rendered_cache_key_value
@@ -274,6 +327,7 @@ async def _load_cached_script_block_sources(
workflow_run_id=script_block.workflow_run_id,
workflow_run_block_id=script_block.workflow_run_block_id,
input_fields=script_block.input_fields,
+ requires_agent=script_block.requires_agent,
)
return cached_blocks
@@ -333,6 +387,8 @@ async def generate_workflow_script(
pending=pending,
cached_blocks=cached_block_sources,
updated_block_labels=updated_block_labels,
+ use_semantic_selectors=workflow.adaptive_caching,
+ adaptive_caching=workflow.adaptive_caching,
)
except Exception:
LOG.error("Failed to generate workflow script source", exc_info=True)
@@ -360,7 +416,7 @@ async def generate_workflow_script(
pending=pending,
)
- # check if an existing drfat workflow script exists for this workflow run
+ # check if an existing draft workflow script exists for this workflow run
existing_pending_workflow_script = None
status = ScriptStatus.published
if pending:
@@ -383,3 +439,415 @@ async def generate_workflow_script(
workflow_run_id=workflow_run.workflow_run_id,
status=status,
)
+
+
+_IMPORT_RE = re.compile(r"^(?:import |from \S+ import )")
+
+
+def _split_imports(code: str) -> tuple[list[str], str]:
+ """Split leading import statements from reviewer-generated code.
+
+ Returns (import_lines, remaining_code) where import_lines are any
+ ``import ...`` or ``from ... import ...`` lines that appear before the
+ first non-import, non-blank line (i.e. the ``@skyvern.cached`` decorator
+ or ``async def``).
+ """
+ code_lines = code.split("\n")
+ imports: list[str] = []
+ body_start = 0
+ for i, line in enumerate(code_lines):
+ stripped = line.strip()
+ if not stripped:
+ continue
+ if _IMPORT_RE.match(stripped):
+ imports.append(stripped)
+ body_start = i + 1
+ else:
+ break
+ # Skip blank lines between imports and function body
+ while body_start < len(code_lines) and not code_lines[body_start].strip():
+ body_start += 1
+ return imports, "\n".join(code_lines[body_start:])
+
+
+def _hoist_imports(lines: list[str], new_imports: list[str]) -> None:
+ """Insert new import lines into the file's existing import section (mutates *lines*).
+
+ Imports are deduplicated against existing lines and inserted after the last
+ existing top-level import statement.
+ """
+ if not new_imports:
+ return
+ existing = {line.strip() for line in lines}
+ # Deduplicate against both existing file lines and within new_imports itself
+ seen: set[str] = set()
+ to_add: list[str] = []
+ for imp in new_imports:
+ if imp not in existing and imp not in seen:
+ to_add.append(imp)
+ seen.add(imp)
+ if not to_add:
+ return
+
+ # Find the last top-level import line
+ last_import_idx = -1
+ for i, line in enumerate(lines):
+ stripped = line.strip()
+ if _IMPORT_RE.match(stripped):
+ last_import_idx = i
+ elif stripped and not stripped.startswith("#") and last_import_idx >= 0:
+ # Hit first non-import, non-blank, non-comment line after imports
+ break
+
+ insert_at = last_import_idx + 1 if last_import_idx >= 0 else 0
+ for offset, imp in enumerate(to_add):
+ lines.insert(insert_at + offset, imp)
+
+
+def _patch_main_py(main_py_content: str, updated_blocks: dict[str, str]) -> str:
+ """Replace cached block functions in main.py with updated code from the reviewer.
+
+ Each block function in main.py starts with ``@skyvern.cached(cache_key = 'LABEL')``
+ and ends just before the next ``@skyvern.cached`` decorator or the end of the file.
+ This function splices in the new code for each updated block.
+
+ If the reviewer output contains leading ``import`` statements, they are hoisted
+ to the file's import section so they don't break decorator/function syntax.
+ """
+ lines = main_py_content.split("\n")
+
+ # Collect imports from all updated blocks first, then hoist once
+ all_new_imports: list[str] = []
+ cleaned_blocks: dict[str, str] = {}
+ for label, code in updated_blocks.items():
+ imports, body = _split_imports(code)
+ all_new_imports.extend(imports)
+ cleaned_blocks[label] = body
+
+ _hoist_imports(lines, all_new_imports)
+
+ # Build index: block_label -> (start_line, end_line) in main.py
+ decorator_pattern = re.compile(r"^@skyvern\.cached\(\s*cache_key\s*=\s*['\"]([^'\"]+)['\"]\s*\)")
+ block_positions: list[tuple[str, int]] = [] # (label, line_index)
+ for i, line in enumerate(lines):
+ m = decorator_pattern.match(line.strip())
+ if m:
+ block_positions.append((m.group(1), i))
+
+ if not block_positions:
+ # No existing blocks found — append all updated blocks at the end
+ for label, code in cleaned_blocks.items():
+ new_code = code.rstrip("\n")
+ decorator_line = f"@skyvern.cached(cache_key = '{label}')"
+ lines.extend(["", "", decorator_line, new_code])
+ return "\n".join(lines)
+
+ # Track which labels were patched (to identify new blocks that need appending)
+ patched_labels: set[str] = set()
+ existing_labels = {label for label, _ in block_positions}
+
+ # Process in reverse order so line insertions don't shift earlier indices
+ for idx in range(len(block_positions) - 1, -1, -1):
+ label, start = block_positions[idx]
+ if label not in cleaned_blocks:
+ continue
+
+ patched_labels.add(label)
+
+ # End of this block = start of next block, or end of file
+ if idx + 1 < len(block_positions):
+ end = block_positions[idx + 1][1]
+ else:
+ end = len(lines)
+
+ # Strip trailing blank lines from the replacement to avoid accumulation
+ new_code = cleaned_blocks[label].rstrip("\n")
+
+ # Ensure the @skyvern.cached decorator is present so the function gets
+ # registered at import time. The reviewer output may or may not include it.
+ if not new_code.lstrip().startswith("@skyvern.cached"):
+ decorator_line = f"@skyvern.cached(cache_key = '{label}')"
+ lines[start:end] = [decorator_line, new_code, "", ""]
+ else:
+ lines[start:end] = [new_code, "", ""]
+
+ # Append NEW blocks that don't exist in main.py yet (e.g., conditional blocks
+ # being upgraded from agent-required to code for the first time).
+ for label, code in cleaned_blocks.items():
+ if label in existing_labels:
+ continue
+ new_code = code.rstrip("\n")
+ decorator_line = f"@skyvern.cached(cache_key = '{label}')"
+ lines.extend(["", "", decorator_line, new_code])
+
+ return "\n".join(lines)
+
+
+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.
+
+ After this fix is deployed, every new version will have a patched main.py, so
+ the base revision will always have one. The v1 fallback exists for bootstrapping
+ (versions created before this fix existed).
+ """
+
+ async def _load_main_py_from_revision(revision_id: str) -> str | None:
+ files = await app.DATABASE.get_script_files(
+ script_revision_id=revision_id,
+ organization_id=organization_id,
+ )
+ for f in files:
+ if f.file_path == "main.py" and f.artifact_id:
+ artifact = await app.DATABASE.get_artifact_by_id(f.artifact_id, organization_id)
+ if artifact:
+ content = await app.ARTIFACT_MANAGER.retrieve_artifact(artifact)
+ if content:
+ return content.decode("utf-8") if isinstance(content, bytes) else content
+ return None
+
+ # Try the base revision first (has cumulative patches from all prior reviews)
+ result = await _load_main_py_from_revision(base_revision_id)
+ if result:
+ return result
+
+ # Fall back to v1 (bootstrapping: base was created before this fix)
+ v1_script = await app.DATABASE.get_script(
+ script_id=script_id,
+ organization_id=organization_id,
+ version=1,
+ )
+ if v1_script and v1_script.script_revision_id != base_revision_id:
+ return await _load_main_py_from_revision(v1_script.script_revision_id)
+
+ return None
+
+
+async def create_script_version_from_review(
+ organization_id: str,
+ workflow_permanent_id: str,
+ base_script: Script,
+ updated_blocks: dict[str, str],
+ workflow: Workflow,
+ workflow_run: WorkflowRun,
+ conditional_blocks: dict[str, str] | None = None,
+) -> Script | None:
+ """Create a new script version incorporating updated block code from the AI reviewer.
+
+ Args:
+ organization_id: The organization ID.
+ workflow_permanent_id: The workflow permanent ID.
+ base_script: The script revision being improved.
+ updated_blocks: Dict of {block_label: updated_code} from the reviewer.
+ workflow: The workflow model.
+ workflow_run: The workflow run that triggered the review.
+
+ Returns:
+ The new Script revision, or None if creation failed.
+ """
+ try:
+ # Create a new script version
+ new_script = await app.DATABASE.create_script(
+ organization_id=organization_id,
+ script_id=base_script.script_id,
+ version=base_script.version + 1,
+ run_id=workflow_run.workflow_run_id,
+ )
+
+ # Copy existing script blocks from the base revision
+ existing_blocks = await app.DATABASE.get_script_blocks_by_script_revision_id(
+ script_revision_id=base_script.script_revision_id,
+ organization_id=organization_id,
+ )
+
+ conditional_blocks = conditional_blocks or {}
+
+ for sb in existing_blocks:
+ if sb.script_block_label in updated_blocks:
+ # This block has an updated version from the reviewer
+ updated_code = updated_blocks[sb.script_block_label]
+ content_bytes = updated_code.encode("utf-8")
+ content_hash = hashlib.sha256(content_bytes).hexdigest()
+ file_path = f"blocks/{sb.script_block_label}.py"
+
+ # Upload code to S3 and create script file DB record
+ artifact_id = await app.ARTIFACT_MANAGER.create_script_file_artifact(
+ organization_id=organization_id,
+ script_id=new_script.script_id,
+ script_version=new_script.version,
+ file_path=file_path,
+ data=content_bytes,
+ )
+ new_file = await app.DATABASE.create_script_file(
+ script_revision_id=new_script.script_revision_id,
+ script_id=new_script.script_id,
+ organization_id=organization_id,
+ file_path=file_path,
+ file_name=f"{sb.script_block_label}.py",
+ file_type="file",
+ content_hash=f"sha256:{content_hash}",
+ file_size=len(content_bytes),
+ mime_type="text/x-python",
+ artifact_id=artifact_id,
+ )
+
+ # Determine if this is a conditional block being upgraded to code.
+ # If so, flip requires_agent to False and set the run_signature.
+ is_conditional_upgrade = sb.script_block_label in conditional_blocks
+ block_requires_agent = False if is_conditional_upgrade else sb.requires_agent
+ block_run_signature = (
+ f'await skyvern.conditional(label="{sb.script_block_label}")'
+ if is_conditional_upgrade
+ else sb.run_signature
+ )
+
+ # Create script block entry pointing to the new file
+ await app.DATABASE.create_script_block(
+ organization_id=organization_id,
+ script_id=new_script.script_id,
+ script_revision_id=new_script.script_revision_id,
+ script_block_label=sb.script_block_label,
+ script_file_id=new_file.file_id,
+ run_signature=block_run_signature,
+ workflow_run_id=workflow_run.workflow_run_id,
+ input_fields=sb.input_fields,
+ requires_agent=block_requires_agent,
+ )
+ else:
+ # Copy existing block as-is
+ await app.DATABASE.create_script_block(
+ organization_id=organization_id,
+ script_id=new_script.script_id,
+ script_revision_id=new_script.script_revision_id,
+ script_block_label=sb.script_block_label,
+ script_file_id=sb.script_file_id,
+ run_signature=sb.run_signature,
+ workflow_run_id=sb.workflow_run_id,
+ workflow_run_block_id=sb.workflow_run_block_id,
+ input_fields=sb.input_fields,
+ requires_agent=sb.requires_agent,
+ )
+
+ # Patch main.py with updated block functions and copy non-block files.
+ # Prefers the base revision's main.py (cumulative patches), falls back to v1.
+ main_py_content = await _find_main_py_content(
+ script_id=base_script.script_id,
+ organization_id=organization_id,
+ base_revision_id=base_script.script_revision_id,
+ )
+ if main_py_content:
+ patched_main = _patch_main_py(main_py_content, updated_blocks)
+ patched_bytes = patched_main.encode("utf-8")
+ patched_hash = hashlib.sha256(patched_bytes).hexdigest()
+
+ main_artifact_id = await app.ARTIFACT_MANAGER.create_script_file_artifact(
+ organization_id=organization_id,
+ script_id=new_script.script_id,
+ script_version=new_script.version,
+ file_path="main.py",
+ data=patched_bytes,
+ )
+ await app.DATABASE.create_script_file(
+ script_revision_id=new_script.script_revision_id,
+ script_id=new_script.script_id,
+ organization_id=organization_id,
+ file_path="main.py",
+ file_name="main.py",
+ file_type="file",
+ content_hash=f"sha256:{patched_hash}",
+ file_size=len(patched_bytes),
+ mime_type="text/x-python",
+ artifact_id=main_artifact_id,
+ )
+ LOG.info(
+ "Patched main.py with updated block code",
+ script_id=new_script.script_id,
+ version=new_script.version,
+ patched_blocks=list(updated_blocks.keys()),
+ main_py_size=len(patched_bytes),
+ )
+
+ # Copy non-block files (e.g., .skyvern metadata) from the base revision
+ # or v1 — whichever has the full file set
+ source_files = await app.DATABASE.get_script_files(
+ script_revision_id=base_script.script_revision_id,
+ organization_id=organization_id,
+ )
+ if not any(f.file_path != "main.py" and not f.file_path.startswith("blocks/") for f in source_files):
+ # Base revision has no non-block files, fall back to v1
+ v1_script = await app.DATABASE.get_script(
+ script_id=base_script.script_id,
+ organization_id=organization_id,
+ version=1,
+ )
+ if v1_script:
+ source_files = await app.DATABASE.get_script_files(
+ script_revision_id=v1_script.script_revision_id,
+ organization_id=organization_id,
+ )
+
+ updated_block_file_paths = {f"blocks/{label}.py" for label in updated_blocks}
+ for f in source_files:
+ # Skip main.py (already patched) and updated block files (already created)
+ if f.file_path == "main.py" or f.file_path in updated_block_file_paths:
+ continue
+ await app.DATABASE.create_script_file(
+ script_revision_id=new_script.script_revision_id,
+ script_id=new_script.script_id,
+ organization_id=organization_id,
+ file_path=f.file_path,
+ file_name=f.file_name,
+ file_type=f.file_type,
+ content_hash=f.content_hash,
+ file_size=f.file_size,
+ mime_type=f.mime_type,
+ artifact_id=f.artifact_id,
+ )
+ else:
+ LOG.warning(
+ "Could not find main.py to patch for new script version",
+ script_id=base_script.script_id,
+ version=new_script.version,
+ )
+
+ # Create the workflow script mapping for cache lookup
+ _, rendered_cache_key_value = await get_workflow_script(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ status=ScriptStatus.published,
+ )
+
+ await app.DATABASE.create_workflow_script(
+ organization_id=organization_id,
+ script_id=new_script.script_id,
+ workflow_permanent_id=workflow_permanent_id,
+ cache_key=workflow.cache_key or "",
+ cache_key_value=rendered_cache_key_value,
+ workflow_id=workflow.workflow_id,
+ workflow_run_id=workflow_run.workflow_run_id,
+ status=ScriptStatus.published,
+ )
+
+ # Clear the in-memory cache so the new version is picked up
+ clear_workflow_script_cache(
+ organization_id=organization_id,
+ workflow_permanent_id=workflow_permanent_id,
+ )
+
+ LOG.info(
+ "Created new script version from AI review",
+ organization_id=organization_id,
+ workflow_permanent_id=workflow_permanent_id,
+ base_version=base_script.version,
+ new_version=new_script.version,
+ updated_block_count=len(updated_blocks),
+ )
+
+ return new_script
+
+ except Exception:
+ LOG.exception(
+ "Failed to create script version from review",
+ organization_id=organization_id,
+ workflow_permanent_id=workflow_permanent_id,
+ )
+ return None