Skip to content

Automate browser based workflows with AI

refactor(SKY-11708): extract conditional-episode and cache-tracking phases from _execute_single_block (#7361)

Aaron Perezef10e62c5bc9
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 358cafa68..d24cc0a36 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -1481,8 +1481,6 @@ class ForgeAgent:
)
prefetched_summary_task: asyncio.Task[dict[str, Any]] | None = None
current_artifact_task: asyncio.Task | None = None
- pdf_auto_download_src: str | None = None
- pdf_auto_download_used_bytes: bool = False
async def await_background_artifact_task() -> None:
nonlocal current_artifact_task
@@ -1567,231 +1565,27 @@ class ForgeAgent:
detailed_agent_step_output.scraped_page = scraped_page
detailed_agent_step_output.extract_action_prompt = extract_action_prompt
- actions: list[Action]
-
- # If prepare_step_execution injected actions (e.g. proactive captcha solving),
- # skip LLM entirely and use the injected actions directly.
- if injected_actions is not None:
- LOG.info(
- "Using injected actions from prepare_step_execution, skipping LLM",
- step_id=step.step_id,
- num_actions=len(injected_actions),
- action_types=[a.action_type for a in injected_actions],
- )
- actions = injected_actions
- elif engine == RunEngine.openai_cua:
- actions, new_cua_response = await self._generate_cua_actions(
- task=task,
- step=step,
- scraped_page=scraped_page,
- previous_response=cua_response,
- engine=engine,
- )
- detailed_agent_step_output.cua_response = new_cua_response
- elif engine == RunEngine.anthropic_cua:
- assert llm_caller is not None
- actions = await self._generate_anthropic_actions(
- task=task,
- step=step,
- scraped_page=scraped_page,
- llm_caller=llm_caller,
- )
- elif engine == RunEngine.ui_tars and not await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
- "DISABLE_UI_TARS_CUA",
- task.workflow_run_id or task.task_id,
- properties={"organization_id": task.organization_id},
- ):
- assert llm_caller is not None
- actions = await self._generate_ui_tars_actions(
- task=task,
- step=step,
- scraped_page=scraped_page,
- llm_caller=llm_caller,
- )
- elif engine == RunEngine.yutori_navigator:
- assert llm_caller is not None
- actions = await self._generate_yutori_navigator_actions(
- task=task,
- step=step,
- scraped_page=scraped_page,
- llm_caller=llm_caller,
- )
-
- else:
- if is_extraction_task:
- actions = [
- await self.create_extract_action(
- task,
- step,
- scraped_page,
- prefetched_summary_task=prefetched_summary_task,
- )
- ]
- else:
- llm_key_override = task.llm_key
- # FIXME: Redundant engine check?
- if engine in CUA_ENGINES:
- self.async_operation_pool.run_operation(task.task_id, AgentPhase.llm)
- llm_key_override = None
-
- llm_api_handler = LLMAPIHandlerFactory.get_override_llm_api_handler(
- llm_key_override, default=app.LLM_API_HANDLER
- )
- # Add caching flag to context for monitoring
- if use_caching:
- context = skyvern_context.current()
- if context:
- context.use_prompt_caching = True
-
- if not reuse_speculative_llm_response:
- json_response = await llm_api_handler(
- prompt=extract_action_prompt,
- prompt_name=prompt_name,
- step=step,
- screenshots=[] if without_page_information else scraped_page.screenshots,
- system_prompt=task.workflow_system_prompt,
- )
- else:
- LOG.debug(
- "Using speculative extract-actions response",
- step_id=step.step_id,
- )
- if json_response is None:
- raise MissingExtractActionsResponse()
- try:
- # Check for PDF viewer: either <embed type="application/pdf"> or
- # Edge's PDF interstitial <iframe src="data:application/pdf;base64,...">
- pdf_src = scraped_page.check_pdf_viewer_embed()
- if not pdf_src:
- pdf_src = await scraped_page.check_pdf_iframe()
- if pdf_src and not should_auto_download_pdf(pdf_src):
- LOG.info(
- "PDF source already downloaded, skipping auto-download",
- step_id=step.step_id,
- )
- pdf_src = None
- if pdf_src:
- LOG.info("Generate DownloadFileAction for PDF viewer page", step_id=step.step_id)
- pdf_bytes: bytes | None = None
- download_url: str | None = None
-
- # Check if the src is a data URI with base64 encoded PDF
- # Format: data:application/pdf[;charset=...];base64,<base64_data>
- if pdf_src.startswith("data:application/pdf"):
- # Use more precise regex to extract base64 data after the base64, prefix
- # This pattern matches: data:application/pdf[;optional_params];base64,<data>
- m = re.search(r"data:application/pdf[^;]*;base64,(.+)", pdf_src, re.S)
- if not m:
- raise PDFEmbedBase64DecodeError(
- pdf_embed_src=pdf_src,
- reason="Failed to extract base64 data from PDF embed src. Expected format: data:application/pdf[;charset=...];base64,<data>",
- )
-
- base64_data = m.group(1)
- LOG.info(
- "Found base64 data in PDF src",
- step_id=step.step_id,
- base64_data_length=len(base64_data),
- )
-
- # Decode base64 data with error handling
- try:
- pdf_bytes = base64.b64decode(base64_data, validate=True)
- except Exception as e:
- raise PDFEmbedBase64DecodeError(
- pdf_embed_src=pdf_src,
- reason=f"Failed to decode base64 data: {str(e)}",
- ) from e
- else:
- # If not a data URI, treat it as a URL
- LOG.info(
- "Found PDF src as URL (not base64 data)",
- step_id=step.step_id,
- download_url=pdf_src,
- )
- download_url = pdf_src
-
- actions = [
- DownloadFileAction(
- reasoning="Downloading the file from the PDF viewer.",
- organization_id=task.organization_id,
- workflow_run_id=task.workflow_run_id,
- task_id=task.task_id,
- step_id=step.step_id,
- step_order=step.order,
- action_order=0,
- file_name=f"{uuid.uuid4()}.pdf",
- byte=pdf_bytes,
- download_url=download_url,
- download=True,
- )
- ]
- pdf_auto_download_src = pdf_src
- pdf_auto_download_used_bytes = pdf_bytes is not None
- else:
- otp_json_response, otp_actions = await self.handle_potential_OTP_actions(
- task, step, scraped_page, browser_state, json_response
- )
- if otp_actions:
- detailed_agent_step_output.llm_response = otp_json_response
- actions = otp_actions
- else:
- actions = parse_actions(
- task, step.step_id, step.order, scraped_page, json_response["actions"]
- )
-
- if context:
- context.pop_totp_code(task.task_id)
- except NoTOTPVerificationCodeFound:
- # Surface only the source poll_otp_value actually queried so
- # the failure_reason tells the customer which delivery endpoint
- # went silent.
- timeout_reasoning = _build_totp_timeout_reasoning(task)
- LOG.warning(
- "TOTP polling timed out — terminating task",
- task_id=task.task_id,
- workflow_run_id=task.workflow_run_id,
- totp_verification_url=strip_query_params(task.totp_verification_url)
- if task.totp_verification_url
- else None,
- totp_identifier=task.totp_identifier,
- organization_id=task.organization_id,
- )
- actions = [
- TerminateAction(
- organization_id=task.organization_id,
- workflow_run_id=task.workflow_run_id,
- task_id=task.task_id,
- step_id=step.step_id,
- step_order=step.order,
- action_order=0,
- reasoning=timeout_reasoning,
- intention=timeout_reasoning,
- errors=[TimeoutGetTOTPVerificationCodeError().to_user_defined_error()],
- )
- ]
- except FailedToGetTOTPVerificationCode as e:
- actions = [
- TerminateAction(
- reasoning=f"Failed to get TOTP verification code. Going to terminate. Reason: {e.reason}",
- intention=f"Failed to get TOTP verification code. Going to terminate. Reason: {e.reason}",
- organization_id=task.organization_id,
- workflow_run_id=task.workflow_run_id,
- task_id=task.task_id,
- step_id=step.step_id,
- step_order=step.order,
- action_order=0,
- errors=[GetTOTPVerificationCodeError(reason=e.reason).to_user_defined_error()],
- )
- ]
-
- if reuse_speculative_llm_response and speculative_llm_metadata:
- await self._persist_speculative_llm_metadata(
- step,
- speculative_llm_metadata,
- screenshots=scraped_page.screenshots,
- )
- speculative_llm_metadata = None
+ actions, pdf_auto_download_src, pdf_auto_download_used_bytes = await self._generate_step_actions(
+ task=task,
+ step=step,
+ browser_state=browser_state,
+ engine=engine,
+ scraped_page=scraped_page,
+ detailed_agent_step_output=detailed_agent_step_output,
+ injected_actions=injected_actions,
+ is_extraction_task=is_extraction_task,
+ prefetched_summary_task=prefetched_summary_task,
+ cua_response=cua_response,
+ llm_caller=llm_caller,
+ extract_action_prompt=extract_action_prompt,
+ prompt_name=prompt_name,
+ use_caching=use_caching,
+ without_page_information=without_page_information,
+ json_response=json_response,
+ reuse_speculative_llm_response=reuse_speculative_llm_response,
+ speculative_llm_metadata=speculative_llm_metadata,
+ context=context,
+ )
detailed_agent_step_output.actions = actions
record_validation_span_attrs(_step_span, task, actions)
@@ -2260,6 +2054,257 @@ class ForgeAgent:
await _cancel_pending_prefetch_task(prefetched_summary_task)
await await_background_artifact_task()
+ async def _generate_step_actions(
+ self,
+ *,
+ task: Task,
+ step: Step,
+ browser_state: BrowserState,
+ engine: RunEngine,
+ scraped_page: ScrapedPage,
+ detailed_agent_step_output: DetailedAgentStepOutput,
+ injected_actions: list[Action] | None,
+ is_extraction_task: bool,
+ prefetched_summary_task: asyncio.Task[dict[str, Any]] | None,
+ cua_response: OpenAIResponse | None,
+ llm_caller: LLMCaller | None,
+ extract_action_prompt: str,
+ prompt_name: str,
+ use_caching: bool,
+ without_page_information: bool,
+ json_response: dict[str, Any] | None,
+ reuse_speculative_llm_response: bool,
+ speculative_llm_metadata: SpeculativeLLMMetadata | None,
+ context: SkyvernContext | None,
+ ) -> tuple[list[Action], str | None, bool]:
+ pdf_auto_download_src: str | None = None
+ pdf_auto_download_used_bytes = False
+ actions: list[Action]
+ # If prepare_step_execution injected actions (e.g. proactive captcha solving),
+ # skip LLM entirely and use the injected actions directly.
+ if injected_actions is not None:
+ LOG.info(
+ "Using injected actions from prepare_step_execution, skipping LLM",
+ step_id=step.step_id,
+ num_actions=len(injected_actions),
+ action_types=[a.action_type for a in injected_actions],
+ )
+ actions = injected_actions
+ elif engine == RunEngine.openai_cua:
+ actions, new_cua_response = await self._generate_cua_actions(
+ task=task,
+ step=step,
+ scraped_page=scraped_page,
+ previous_response=cua_response,
+ engine=engine,
+ )
+ detailed_agent_step_output.cua_response = new_cua_response
+ elif engine == RunEngine.anthropic_cua:
+ assert llm_caller is not None
+ actions = await self._generate_anthropic_actions(
+ task=task,
+ step=step,
+ scraped_page=scraped_page,
+ llm_caller=llm_caller,
+ )
+ elif engine == RunEngine.ui_tars and not await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
+ "DISABLE_UI_TARS_CUA",
+ task.workflow_run_id or task.task_id,
+ properties={"organization_id": task.organization_id},
+ ):
+ assert llm_caller is not None
+ actions = await self._generate_ui_tars_actions(
+ task=task,
+ step=step,
+ scraped_page=scraped_page,
+ llm_caller=llm_caller,
+ )
+ elif engine == RunEngine.yutori_navigator:
+ assert llm_caller is not None
+ actions = await self._generate_yutori_navigator_actions(
+ task=task,
+ step=step,
+ scraped_page=scraped_page,
+ llm_caller=llm_caller,
+ )
+
+ else:
+ if is_extraction_task:
+ actions = [
+ await self.create_extract_action(
+ task,
+ step,
+ scraped_page,
+ prefetched_summary_task=prefetched_summary_task,
+ )
+ ]
+ else:
+ llm_key_override = task.llm_key
+ # FIXME: Redundant engine check?
+ if engine in CUA_ENGINES:
+ self.async_operation_pool.run_operation(task.task_id, AgentPhase.llm)
+ llm_key_override = None
+
+ llm_api_handler = LLMAPIHandlerFactory.get_override_llm_api_handler(
+ llm_key_override, default=app.LLM_API_HANDLER
+ )
+ # Add caching flag to context for monitoring
+ if use_caching:
+ context = skyvern_context.current()
+ if context:
+ context.use_prompt_caching = True
+
+ if not reuse_speculative_llm_response:
+ json_response = await llm_api_handler(
+ prompt=extract_action_prompt,
+ prompt_name=prompt_name,
+ step=step,
+ screenshots=[] if without_page_information else scraped_page.screenshots,
+ system_prompt=task.workflow_system_prompt,
+ )
+ else:
+ LOG.debug(
+ "Using speculative extract-actions response",
+ step_id=step.step_id,
+ )
+ if json_response is None:
+ raise MissingExtractActionsResponse()
+ try:
+ # Check for PDF viewer: either <embed type="application/pdf"> or
+ # Edge's PDF interstitial <iframe src="data:application/pdf;base64,...">
+ pdf_src = scraped_page.check_pdf_viewer_embed()
+ if not pdf_src:
+ pdf_src = await scraped_page.check_pdf_iframe()
+ if pdf_src and not should_auto_download_pdf(pdf_src):
+ LOG.info(
+ "PDF source already downloaded, skipping auto-download",
+ step_id=step.step_id,
+ )
+ pdf_src = None
+ if pdf_src:
+ LOG.info("Generate DownloadFileAction for PDF viewer page", step_id=step.step_id)
+ pdf_bytes: bytes | None = None
+ download_url: str | None = None
+
+ # Check if the src is a data URI with base64 encoded PDF
+ # Format: data:application/pdf[;charset=...];base64,<base64_data>
+ if pdf_src.startswith("data:application/pdf"):
+ # Use more precise regex to extract base64 data after the base64, prefix
+ # This pattern matches: data:application/pdf[;optional_params];base64,<data>
+ m = re.search(r"data:application/pdf[^;]*;base64,(.+)", pdf_src, re.S)
+ if not m:
+ raise PDFEmbedBase64DecodeError(
+ pdf_embed_src=pdf_src,
+ reason="Failed to extract base64 data from PDF embed src. Expected format: data:application/pdf[;charset=...];base64,<data>",
+ )
+
+ base64_data = m.group(1)
+ LOG.info(
+ "Found base64 data in PDF src",
+ step_id=step.step_id,
+ base64_data_length=len(base64_data),
+ )
+
+ # Decode base64 data with error handling
+ try:
+ pdf_bytes = base64.b64decode(base64_data, validate=True)
+ except Exception as e:
+ raise PDFEmbedBase64DecodeError(
+ pdf_embed_src=pdf_src,
+ reason=f"Failed to decode base64 data: {str(e)}",
+ ) from e
+ else:
+ # If not a data URI, treat it as a URL
+ LOG.info(
+ "Found PDF src as URL (not base64 data)",
+ step_id=step.step_id,
+ download_url=pdf_src,
+ )
+ download_url = pdf_src
+
+ actions = [
+ DownloadFileAction(
+ reasoning="Downloading the file from the PDF viewer.",
+ organization_id=task.organization_id,
+ workflow_run_id=task.workflow_run_id,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ step_order=step.order,
+ action_order=0,
+ file_name=f"{uuid.uuid4()}.pdf",
+ byte=pdf_bytes,
+ download_url=download_url,
+ download=True,
+ )
+ ]
+ pdf_auto_download_src = pdf_src
+ pdf_auto_download_used_bytes = pdf_bytes is not None
+ else:
+ otp_json_response, otp_actions = await self.handle_potential_OTP_actions(
+ task, step, scraped_page, browser_state, json_response
+ )
+ if otp_actions:
+ detailed_agent_step_output.llm_response = otp_json_response
+ actions = otp_actions
+ else:
+ actions = parse_actions(
+ task, step.step_id, step.order, scraped_page, json_response["actions"]
+ )
+
+ if context:
+ context.pop_totp_code(task.task_id)
+ except NoTOTPVerificationCodeFound:
+ # Surface only the source poll_otp_value actually queried so
+ # the failure_reason tells the customer which delivery endpoint
+ # went silent.
+ timeout_reasoning = _build_totp_timeout_reasoning(task)
+ LOG.warning(
+ "TOTP polling timed out — terminating task",
+ task_id=task.task_id,
+ workflow_run_id=task.workflow_run_id,
+ totp_verification_url=strip_query_params(task.totp_verification_url)
+ if task.totp_verification_url
+ else None,
+ totp_identifier=task.totp_identifier,
+ organization_id=task.organization_id,
+ )
+ actions = [
+ TerminateAction(
+ organization_id=task.organization_id,
+ workflow_run_id=task.workflow_run_id,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ step_order=step.order,
+ action_order=0,
+ reasoning=timeout_reasoning,
+ intention=timeout_reasoning,
+ errors=[TimeoutGetTOTPVerificationCodeError().to_user_defined_error()],
+ )
+ ]
+ except FailedToGetTOTPVerificationCode as e:
+ actions = [
+ TerminateAction(
+ reasoning=f"Failed to get TOTP verification code. Going to terminate. Reason: {e.reason}",
+ intention=f"Failed to get TOTP verification code. Going to terminate. Reason: {e.reason}",
+ organization_id=task.organization_id,
+ workflow_run_id=task.workflow_run_id,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ step_order=step.order,
+ action_order=0,
+ errors=[GetTOTPVerificationCodeError(reason=e.reason).to_user_defined_error()],
+ )
+ ]
+
+ if reuse_speculative_llm_response and speculative_llm_metadata:
+ await self._persist_speculative_llm_metadata(
+ step,
+ speculative_llm_metadata,
+ screenshots=scraped_page.screenshots,
+ )
+ speculative_llm_metadata = None
+ return actions, pdf_auto_download_src, pdf_auto_download_used_bytes
+
async def _generate_cua_actions(
self,
task: Task,
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index dcc8ab920..8e85bc44b 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -3660,77 +3660,13 @@ class WorkflowService:
and branch_metadata
and is_adaptive_caching(workflow, workflow_run)
):
- try:
- # Extract the branch expressions and results for the reviewer.
- # Evaluations from ConditionalBlock.execute() stop at the first
- # matched branch (break on match), so unevaluated branches
- # (including the default) may be missing. We merge runtime
- # evaluations with the full branch definitions so the script
- # reviewer always sees every branch — this is critical for the
- # branch-return validator which checks that generated code only
- # returns labels/indices from the defined branches.
- evaluations = branch_metadata.get("evaluations", [])
- eval_by_index: dict[int, dict] = {}
- for ev in evaluations:
- idx = ev.get("branch_index")
- if idx is not None:
- eval_by_index[idx] = ev
-
- expressions = []
- if hasattr(block, "ordered_branches"):
- for idx, b in enumerate(block.ordered_branches):
- ev = eval_by_index.get(idx)
- expr_info = {
- "original_expression": (
- ev.get("original_expression")
- if ev
- else (b.criteria.expression if b.criteria else None)
- ),
- "rendered_expression": ev.get("rendered_expression") if ev else None,
- "result": ev.get("result") if ev else None,
- "is_default": ev.get("is_default", b.is_default) if ev else b.is_default,
- "next_block_label": b.next_block_label,
- }
- expressions.append(expr_info)
- else:
- # Fallback: no ordered_branches, use evaluations as-is
- for ev in evaluations:
- expressions.append(
- {
- "original_expression": ev.get("original_expression"),
- "rendered_expression": ev.get("rendered_expression"),
- "result": ev.get("result"),
- "is_default": ev.get("is_default", False),
- "next_block_label": ev.get("next_block_label"),
- }
- )
- cond_context = skyvern_context.current()
- cond_episode = await app.DATABASE.scripts.create_fallback_episode(
- organization_id=organization_id,
- workflow_permanent_id=workflow.workflow_permanent_id,
- workflow_run_id=workflow_run_id,
- block_label=block.label,
- fallback_type="conditional_agent",
- error_message=None,
- script_revision_id=cond_context.script_revision_id if cond_context else None,
- agent_actions={
- "block_type": "conditional",
- "branch_taken": branch_metadata.get("branch_taken"),
- "branch_index": branch_metadata.get("branch_index"),
- "expressions": expressions,
- },
- )
- await app.DATABASE.scripts.update_fallback_episode(
- episode_id=cond_episode.episode_id,
- organization_id=organization_id,
- fallback_succeeded=True,
- )
- except Exception:
- LOG.warning(
- "Failed to record conditional episode",
- block_label=block.label,
- exc_info=True,
- )
+ await self._record_conditional_agent_episode(
+ workflow=workflow,
+ block=block,
+ branch_metadata=branch_metadata,
+ workflow_run_id=workflow_run_id,
+ organization_id=organization_id,
+ )
if not workflow_run_block_result:
if attempt.script_exception is not None:
@@ -3743,54 +3679,17 @@ class WorkflowService:
)
return workflow_run, blocks_to_update, workflow_run_block_result, True, branch_metadata
- # Determine which block statuses are eligible for caching
- cacheable_statuses = {BlockStatus.completed}
- if workflow.generate_script_on_terminal:
- cacheable_statuses.add(BlockStatus.terminated)
-
- if (
- not block_executed_with_code
- and block.label
- and block.label not in script_blocks_by_label
- and workflow_run_block_result.status in cacheable_statuses
- and block.block_type in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
- # For traditional caching (code_version < 2), only track blocks
- # for regeneration when actually running with code. Agent-mode runs
- # should not trigger regeneration — doing so creates an infinite loop
- # where every run deletes and regenerates the script because blocks
- # always execute via agent and are never in script_blocks_by_label.
- and (is_adaptive_caching(workflow, workflow_run) or is_script_run)
- ):
- blocks_to_update.add(block.label)
-
- # NOTE: continue_on_failure block failures are handled by the Script
- # Reviewer (triggered at end-of-run, capped at 5/day via Redis), NOT by
- # regenerating the entire script here. The fallback episode is already
- # recorded and the reviewer will patch the specific block that failed.
- # See _trigger_script_reviewer() for the capped reviewer flow.
-
- # Track uncached loop block children for regeneration.
- # Loop block children execute via block.py's execute_*_loop_helper(),
- # bypassing _execute_single_block. Recursively walk all nesting levels
- # so deeply nested blocks (e.g., file_download inside a double-nested
- # loop) get cached functions generated.
- if (
- isinstance(block, (ForLoopBlock, WhileLoopBlock))
- and (is_adaptive_caching(workflow, workflow_run) or is_script_run)
- and workflow_run_block_result.status in cacheable_statuses
- ):
- previous_labels = set(blocks_to_update)
- _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
- new_labels = sorted(blocks_to_update - previous_labels)
- if new_labels:
- LOG.info(
- "Loop block child blocks marked for caching",
- parent_label=block.label,
- child_labels=new_labels,
- child_count=len(new_labels),
- workflow_run_id=workflow_run_id,
- workflow_permanent_id=workflow.workflow_permanent_id,
- )
+ self._track_blocks_for_script_generation(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ workflow_run_id=workflow_run_id,
+ block=block,
+ workflow_run_block_result=workflow_run_block_result,
+ block_executed_with_code=block_executed_with_code,
+ is_script_run=is_script_run,
+ script_blocks_by_label=script_blocks_by_label,
+ blocks_to_update=blocks_to_update,
+ )
workflow_run, should_stop = await self._handle_block_result_status(
block=block,
@@ -3823,6 +3722,147 @@ class WorkflowService:
)
return workflow_run, blocks_to_update, workflow_run_block_result, True, branch_metadata
+ async def _record_conditional_agent_episode(
+ self,
+ *,
+ workflow: Workflow,
+ block: ConditionalBlock,
+ branch_metadata: dict[str, Any],
+ workflow_run_id: str,
+ organization_id: str,
+ ) -> None:
+ try:
+ # Extract the branch expressions and results for the reviewer.
+ # Evaluations from ConditionalBlock.execute() stop at the first
+ # matched branch (break on match), so unevaluated branches
+ # (including the default) may be missing. We merge runtime
+ # evaluations with the full branch definitions so the script
+ # reviewer always sees every branch — this is critical for the
+ # branch-return validator which checks that generated code only
+ # returns labels/indices from the defined branches.
+ evaluations = branch_metadata.get("evaluations", [])
+ eval_by_index: dict[int, dict] = {}
+ for ev in evaluations:
+ idx = ev.get("branch_index")
+ if idx is not None:
+ eval_by_index[idx] = ev
+
+ expressions = []
+ if hasattr(block, "ordered_branches"):
+ for idx, b in enumerate(block.ordered_branches):
+ ev = eval_by_index.get(idx)
+ expr_info = {
+ "original_expression": (
+ ev.get("original_expression") if ev else (b.criteria.expression if b.criteria else None)
+ ),
+ "rendered_expression": ev.get("rendered_expression") if ev else None,
+ "result": ev.get("result") if ev else None,
+ "is_default": ev.get("is_default", b.is_default) if ev else b.is_default,
+ "next_block_label": b.next_block_label,
+ }
+ expressions.append(expr_info)
+ else:
+ # Fallback: no ordered_branches, use evaluations as-is
+ for ev in evaluations:
+ expressions.append(
+ {
+ "original_expression": ev.get("original_expression"),
+ "rendered_expression": ev.get("rendered_expression"),
+ "result": ev.get("result"),
+ "is_default": ev.get("is_default", False),
+ "next_block_label": ev.get("next_block_label"),
+ }
+ )
+ cond_context = skyvern_context.current()
+ cond_episode = await app.DATABASE.scripts.create_fallback_episode(
+ organization_id=organization_id,
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ workflow_run_id=workflow_run_id,
+ block_label=block.label,
+ fallback_type="conditional_agent",
+ error_message=None,
+ script_revision_id=cond_context.script_revision_id if cond_context else None,
+ agent_actions={
+ "block_type": "conditional",
+ "branch_taken": branch_metadata.get("branch_taken"),
+ "branch_index": branch_metadata.get("branch_index"),
+ "expressions": expressions,
+ },
+ )
+ await app.DATABASE.scripts.update_fallback_episode(
+ episode_id=cond_episode.episode_id,
+ organization_id=organization_id,
+ fallback_succeeded=True,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to record conditional episode",
+ block_label=block.label,
+ exc_info=True,
+ )
+
+ def _track_blocks_for_script_generation(
+ self,
+ *,
+ workflow: Workflow,
+ workflow_run: WorkflowRun,
+ workflow_run_id: str,
+ block: BlockTypeVar,
+ workflow_run_block_result: BlockResult,
+ block_executed_with_code: bool,
+ is_script_run: bool,
+ script_blocks_by_label: dict[str, Any],
+ blocks_to_update: set[str],
+ ) -> None:
+ # Determine which block statuses are eligible for caching
+ cacheable_statuses = {BlockStatus.completed}
+ if workflow.generate_script_on_terminal:
+ cacheable_statuses.add(BlockStatus.terminated)
+
+ if (
+ not block_executed_with_code
+ and block.label
+ and block.label not in script_blocks_by_label
+ and workflow_run_block_result.status in cacheable_statuses
+ and block.block_type in BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+ # For traditional caching (code_version < 2), only track blocks
+ # for regeneration when actually running with code. Agent-mode runs
+ # should not trigger regeneration — doing so creates an infinite loop
+ # where every run deletes and regenerates the script because blocks
+ # always execute via agent and are never in script_blocks_by_label.
+ and (is_adaptive_caching(workflow, workflow_run) or is_script_run)
+ ):
+ blocks_to_update.add(block.label)
+
+ # NOTE: continue_on_failure block failures are handled by the Script
+ # Reviewer (triggered at end-of-run, capped at 5/day via Redis), NOT by
+ # regenerating the entire script here. The fallback episode is already
+ # recorded and the reviewer will patch the specific block that failed.
+ # See _trigger_script_reviewer() for the capped reviewer flow.
+
+ # Track uncached loop block children for regeneration.
+ # Loop block children execute via block.py's execute_*_loop_helper(),
+ # bypassing _execute_single_block. Recursively walk all nesting levels
+ # so deeply nested blocks (e.g., file_download inside a double-nested
+ # loop) get cached functions generated.
+ if (
+ isinstance(block, (ForLoopBlock, WhileLoopBlock))
+ and (is_adaptive_caching(workflow, workflow_run) or is_script_run)
+ and workflow_run_block_result.status in cacheable_statuses
+ ):
+ previous_labels = set(blocks_to_update)
+ _collect_uncached_loop_children(block, script_blocks_by_label, blocks_to_update)
+ new_labels = sorted(blocks_to_update - previous_labels)
+ if new_labels:
+ LOG.info(
+ "Loop block child blocks marked for caching",
+ parent_label=block.label,
+ child_labels=new_labels,
+ child_count=len(new_labels),
+ workflow_run_id=workflow_run_id,
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ )
+
async def _try_execute_block_with_script(
self,
*,