Skip to content

Automate browser based workflows with AI

SKY-11057 Fix Copilot download synthesis replay (#6638)

Andrew Neilsonf9d6f260379e
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index 943021052..c40aa1114 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -23,12 +23,24 @@ from skyvern.utils.strings import escape_code_fences
_MAX_STEPS = 60
_INDENT = " "
+_DOMCONTENTLOADED = "domcontentloaded"
+_ENTRY_TARGET_VAR = "_scout_entry_target"
+_ENTRY_REUSED_VAR = "_scout_entry_reused_current_page"
+_ENTRY_RESUME_AFTER_AUTH_VAR = "_scout_entry_resume_after_auth"
+_ENTRY_RESUME_TARGET_VAR = "_scout_entry_resume_target"
+_ENTRY_OPENER_VAR = "_scout_entry_opener"
+_ENTRY_LOCATOR_VARS = (_ENTRY_TARGET_VAR, _ENTRY_RESUME_TARGET_VAR, _ENTRY_OPENER_VAR)
# Base name for the download var bound by `async with page.expect_download() as <name>:`.
_DOWNLOAD_VAR_BASE = "dl_info"
+_DOWNLOAD_FILENAME_VAR_BASE = "downloaded_file_name"
+_DOWNLOAD_PATH_VAR_BASE = "_downloaded_file_path"
+_DOWNLOAD_OUTPUT_VAR_BASE = "downloaded_files"
CREDENTIAL_FILL_TOOL_NAME = "fill_credential_field"
_CREDENTIAL_FIELDS = frozenset({"username", "password", "totp"})
+_ENTRY_TARGET_TOOLS = frozenset({"click", "type_text", CREDENTIAL_FILL_TOOL_NAME, "select_option", "press_key"})
+_DURABLE_FALLBACK_ENTRY_TARGET_TOOLS = frozenset({"type_text", CREDENTIAL_FILL_TOOL_NAME, "select_option"})
_SYNTHESIZED_BLOCK_LABEL = "scout_synthesized_browser_steps"
@@ -70,8 +82,16 @@ _RESERVED_PARAM_NAMES = frozenset(
"json",
"html",
"Exception",
+ _ENTRY_TARGET_VAR,
+ _ENTRY_REUSED_VAR,
+ _ENTRY_RESUME_AFTER_AUTH_VAR,
+ _ENTRY_RESUME_TARGET_VAR,
+ _ENTRY_OPENER_VAR,
_DOWNLOAD_VAR_BASE,
f"{_DOWNLOAD_VAR_BASE}_file",
+ _DOWNLOAD_FILENAME_VAR_BASE,
+ _DOWNLOAD_PATH_VAR_BASE,
+ _DOWNLOAD_OUTPUT_VAR_BASE,
}
)
@@ -323,6 +343,102 @@ def _credential_param_key(interaction: Mapping[str, Any], used: set[str]) -> str
return _unique_key(_safe_param_base(name or "credential"), used)
+def _is_durable_fallback_entry_target(interaction: Mapping[str, Any]) -> bool:
+ tool_name = str(interaction.get("tool_name") or "")
+ if tool_name not in _DURABLE_FALLBACK_ENTRY_TARGET_TOOLS:
+ return False
+ if tool_name == CREDENTIAL_FILL_TOOL_NAME:
+ credential_id = str(interaction.get("credential_id") or "").strip()
+ credential_field = str(interaction.get("credential_field") or "").strip()
+ return bool(credential_id) and credential_field in _CREDENTIAL_FIELDS
+ if tool_name == "select_option":
+ return bool(str(interaction.get("value") or "").strip())
+ return True
+
+
+def _is_generic_entry_opener_click(interaction: Mapping[str, Any]) -> bool:
+ if str(interaction.get("tool_name") or "") != "click":
+ return False
+ if str(interaction.get("accessible_name") or "").strip():
+ return False
+ selector = str(interaction.get("selector") or "").strip().lower()
+ role = str(interaction.get("role") or "").strip().lower()
+ if role not in {"", "button"}:
+ return False
+ return selector == "button" or bool(re.match(r"^button(?:\\.icon|:nth-)", selector))
+
+
+def _should_prefer_durable_entry_target(trajectory: Sequence[Mapping[str, Any]]) -> bool:
+ if not trajectory or not _is_generic_entry_opener_click(trajectory[0]):
+ return False
+ first_source_url = str(trajectory[0].get("source_url") or "").strip()
+ for interaction in trajectory[1:]:
+ source_url = str(interaction.get("source_url") or "").strip()
+ if first_source_url and source_url and source_url != first_source_url:
+ continue
+ if _is_durable_fallback_entry_target(interaction):
+ return True
+ return False
+
+
+def _entry_target_locator(
+ trajectory: Sequence[Mapping[str, Any]], *, strict_selectors: bool, prefer_durable: bool = False
+) -> tuple[str, int]:
+ first_locator = ""
+ first_index = -1
+ for index, interaction in enumerate(trajectory):
+ tool_name = str(interaction.get("tool_name") or "")
+ if tool_name not in _ENTRY_TARGET_TOOLS:
+ continue
+ if tool_name == "press_key" and not interaction.get("selector"):
+ continue
+ locator = _locator_expr(interaction, [], strict_selectors=strict_selectors)
+ if not locator:
+ continue
+ if not first_locator:
+ first_locator = locator
+ first_index = index
+ if prefer_durable and _is_durable_fallback_entry_target(interaction):
+ return locator, index
+ if not prefer_durable:
+ return locator, index
+ return first_locator, first_index
+
+
+def _entry_target_locator_expr(trajectory: Sequence[Mapping[str, Any]], *, strict_selectors: bool) -> str:
+ locator, _index = _entry_target_locator(trajectory, strict_selectors=strict_selectors)
+ return locator
+
+
+def _post_auth_resume_locator(trajectory: Sequence[Mapping[str, Any]], *, strict_selectors: bool) -> tuple[str, int]:
+ last_credential_index = -1
+ for index, interaction in enumerate(trajectory):
+ if str(interaction.get("tool_name") or "") == CREDENTIAL_FILL_TOOL_NAME:
+ last_credential_index = index
+ if last_credential_index < 0:
+ return "", -1
+
+ submit_index = -1
+ for index in range(last_credential_index + 1, len(trajectory)):
+ if str(trajectory[index].get("tool_name") or "") == "click":
+ submit_index = index
+ break
+ if submit_index < 0:
+ return "", -1
+
+ for index in range(submit_index + 1, len(trajectory)):
+ interaction = trajectory[index]
+ tool_name = str(interaction.get("tool_name") or "")
+ if tool_name not in _ENTRY_TARGET_TOOLS or _is_generic_entry_opener_click(interaction):
+ continue
+ if tool_name == "press_key" and not interaction.get("selector"):
+ continue
+ locator = _locator_expr(interaction, [], strict_selectors=strict_selectors)
+ if locator:
+ return locator, index
+ return "", -1
+
+
def synthesize_code_block(
trajectory: Sequence[Mapping[str, Any]],
*,
@@ -342,6 +458,12 @@ def synthesize_code_block(
typed_param_keys: dict[tuple[str, str, str, str], str] = {}
credential_param_keys: dict[str, str] = {}
used_download_vars: set[str] = set()
+ compile_download_target = (
+ settings.COPILOT_DOWNLOAD_RUNG_SYNTHESIS_ENABLED
+ and reached_download_target is not None
+ and not reached_download_target.already_registered
+ and bool(reached_download_target.selector)
+ )
def append_step(description: str, action_type: str, line_start: int) -> None:
steps.append(
@@ -356,6 +478,9 @@ def synthesize_code_block(
entry_url = ""
entry_index = -1
+ entry_replay_condition_active = False
+ entry_replay_start_index = 0
+ entry_post_auth_resume_index = 0
for index, interaction in enumerate(trajectory):
candidate = str(interaction.get("source_url") or "").strip()
if candidate:
@@ -363,19 +488,148 @@ def synthesize_code_block(
entry_index = index
break
if entry_url:
+ entry_trajectory = trajectory[entry_index:]
+ prefer_durable_entry_target = compile_download_target or _should_prefer_durable_entry_target(entry_trajectory)
+ fallback_entry_target, fallback_entry_relative_index = _entry_target_locator(
+ entry_trajectory,
+ strict_selectors=strict_selectors,
+ prefer_durable=prefer_durable_entry_target,
+ )
+ fallback_entry_index = (
+ entry_index + fallback_entry_relative_index if fallback_entry_relative_index >= 0 else entry_index
+ )
+ post_auth_resume_target, post_auth_resume_relative_index = _post_auth_resume_locator(
+ entry_trajectory,
+ strict_selectors=strict_selectors,
+ )
+ entry_post_auth_resume_index = (
+ entry_index + post_auth_resume_relative_index
+ if post_auth_resume_relative_index > fallback_entry_relative_index
+ else 0
+ )
+ download_entry_target = (
+ f"page.locator({_py_str(reached_download_target.selector)})"
+ if compile_download_target and reached_download_target is not None
+ else ""
+ )
+ entry_target = download_entry_target if download_entry_target else fallback_entry_target
+ entry_replay_condition_active = bool(download_entry_target and fallback_entry_target)
+ entry_replay_start_index = fallback_entry_index if fallback_entry_index > entry_index else 0
if entry_index > 0:
notes.append("entry URL taken from a later interaction; earlier steps had no source_url")
+ if entry_replay_condition_active and fallback_entry_index > entry_index:
+ notes.append("download fallback entry target taken from a later durable interaction")
+ if entry_post_auth_resume_index:
+ notes.append("entry fallback can resume after authentication when login controls stay hidden")
+ elif fallback_entry_index > entry_index:
+ notes.append("entry replay starts at a later durable interaction")
+ entry_recovery_clicks: list[str] = []
+ if fallback_entry_index > entry_index:
+ for recovery_index in range(entry_index, fallback_entry_index):
+ recovery_interaction = trajectory[recovery_index]
+ if not _is_generic_entry_opener_click(recovery_interaction):
+ continue
+ recovery_locator = _locator_expr(
+ recovery_interaction,
+ notes,
+ diagnostics=diagnostics,
+ trajectory_index=recovery_index,
+ tool_name="click",
+ strict_selectors=strict_selectors,
+ )
+ if recovery_locator:
+ entry_recovery_clicks.append(recovery_locator)
+ if entry_recovery_clicks:
+ notes.append("entry fallback replays a generic opener only when the durable target stays hidden")
line_start = len(lines) + 1
- lines.append(f"{_INDENT}await page.goto({_py_str(_scrub_url_for_code_literal(entry_url))})")
- lines.append(f'{_INDENT}await page.wait_for_load_state("load")')
+ if entry_target:
+ if entry_replay_condition_active:
+ lines.append(f"{_INDENT}{_ENTRY_REUSED_VAR} = False")
+ if entry_post_auth_resume_index:
+ lines.append(f"{_INDENT}{_ENTRY_RESUME_AFTER_AUTH_VAR} = False")
+ lines.append(f"{_INDENT}{_ENTRY_TARGET_VAR} = {entry_target}")
+ lines.append(f"{_INDENT}try:")
+ lines.append(f'{_INDENT * 2}await {_ENTRY_TARGET_VAR}.wait_for(state="visible", timeout=1000)')
+ if entry_replay_condition_active:
+ lines.append(f"{_INDENT * 2}{_ENTRY_REUSED_VAR} = True")
+ lines.append(f"{_INDENT}except Exception:")
+ lines.append(
+ f"{_INDENT * 2}await page.goto("
+ f"{_py_str(_scrub_url_for_code_literal(entry_url))}, wait_until={_py_str(_DOMCONTENTLOADED)})"
+ )
+ post_goto_indent = 2
+ if entry_replay_condition_active:
+ lines.append(f"{_INDENT * 2}try:")
+ lines.append(f'{_INDENT * 3}await {_ENTRY_TARGET_VAR}.wait_for(state="visible", timeout=1000)')
+ lines.append(f"{_INDENT * 3}{_ENTRY_REUSED_VAR} = True")
+ lines.append(f"{_INDENT * 2}except Exception:")
+ post_goto_indent = 3
+ if fallback_entry_target and fallback_entry_target != entry_target:
+ lines.append(f"{_INDENT * post_goto_indent}{_ENTRY_TARGET_VAR} = {fallback_entry_target}")
+ if entry_recovery_clicks or entry_post_auth_resume_index:
+ lines.append(f"{_INDENT * post_goto_indent}try:")
+ lines.append(
+ f"{_INDENT * (post_goto_indent + 1)}await {_ENTRY_TARGET_VAR}.wait_for("
+ f'state="visible", timeout=1000)'
+ )
+ lines.append(f"{_INDENT * post_goto_indent}except Exception:")
+ if entry_post_auth_resume_index:
+ lines.append(
+ f"{_INDENT * (post_goto_indent + 1)}{_ENTRY_RESUME_TARGET_VAR} = {post_auth_resume_target}"
+ )
+ lines.append(f"{_INDENT * (post_goto_indent + 1)}try:")
+ lines.append(
+ f"{_INDENT * (post_goto_indent + 2)}await {_ENTRY_RESUME_TARGET_VAR}.wait_for("
+ f'state="visible", timeout=1000)'
+ )
+ lines.append(f"{_INDENT * (post_goto_indent + 2)}{_ENTRY_RESUME_AFTER_AUTH_VAR} = True")
+ lines.append(f"{_INDENT * (post_goto_indent + 1)}except Exception:")
+ recovery_indent = post_goto_indent + 2
+ else:
+ recovery_indent = post_goto_indent + 1
+ for recovery_locator in entry_recovery_clicks:
+ lines.append(f"{_INDENT * recovery_indent}{_ENTRY_OPENER_VAR} = {recovery_locator}")
+ lines.append(f"{_INDENT * recovery_indent}if await {_ENTRY_OPENER_VAR}.count() == 1:")
+ lines.append(f"{_INDENT * (recovery_indent + 1)}await {_ENTRY_OPENER_VAR}.click()")
+ lines.append(
+ f"{_INDENT * (recovery_indent + 1)}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})"
+ )
+ lines.append(f'{_INDENT * recovery_indent}await {_ENTRY_TARGET_VAR}.wait_for(state="visible")')
+ else:
+ lines.append(f'{_INDENT * post_goto_indent}await {_ENTRY_TARGET_VAR}.wait_for(state="visible")')
+ else:
+ lines.append(
+ f"{_INDENT}await page.goto("
+ f"{_py_str(_scrub_url_for_code_literal(entry_url))}, wait_until={_py_str(_DOMCONTENTLOADED)})"
+ )
+ if entry_replay_condition_active:
+ lines.append(f"{_INDENT}if not {_ENTRY_REUSED_VAR}:")
+ if entry_post_auth_resume_index:
+ lines.append(f"{_INDENT * 2}if not {_ENTRY_RESUME_AFTER_AUTH_VAR}:")
+ lines.append(f"{_INDENT * 3}pass")
+ elif entry_post_auth_resume_index:
+ lines.append(f"{_INDENT}if not {_ENTRY_RESUME_AFTER_AUTH_VAR}:")
append_step(f"Open {entry_url}", "goto_url", line_start)
emitted = 0
+
+ def action_indent_for(trajectory_index: int) -> str:
+ if entry_replay_condition_active:
+ if entry_post_auth_resume_index and trajectory_index < entry_post_auth_resume_index:
+ return _INDENT * 3
+ return _INDENT * 2
+ if entry_post_auth_resume_index and trajectory_index < entry_post_auth_resume_index:
+ return _INDENT * 2
+ return _INDENT
+
for trajectory_index, interaction in enumerate(trajectory):
if emitted >= _MAX_STEPS:
diagnostics.truncated = True
notes.append(f"trajectory truncated at {_MAX_STEPS} steps")
break
+ if entry_replay_start_index and trajectory_index < entry_replay_start_index:
+ continue
+ action_indent = action_indent_for(trajectory_index)
tool_name = str(interaction.get("tool_name") or "")
if tool_name == "press_key":
@@ -399,7 +653,7 @@ def synthesize_code_block(
)
line_start = len(lines) + 1
if locator:
- lines.append(f"{_INDENT}await {locator}.press({_py_str(key)})")
+ lines.append(f"{action_indent}await {locator}.press({_py_str(key)})")
else:
if strict_selectors:
diagnostics.dropped_interactions.append(
@@ -410,8 +664,8 @@ def synthesize_code_block(
}
)
continue
- lines.append(f"{_INDENT}await page.keyboard.press({_py_str(key)})")
- lines.append(f'{_INDENT}await page.wait_for_load_state("load")')
+ lines.append(f"{action_indent}await page.keyboard.press({_py_str(key)})")
+ lines.append(f"{action_indent}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})")
append_step(f"Press {key}", "keypress", line_start)
emitted += 1
continue
@@ -429,8 +683,8 @@ def synthesize_code_block(
line_start = len(lines) + 1
if tool_name == "click":
- lines.append(f"{_INDENT}await {locator}.click()")
- lines.append(f'{_INDENT}await page.wait_for_load_state("load")')
+ lines.append(f"{action_indent}await {locator}.click()")
+ lines.append(f"{action_indent}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})")
append_step(f"Click {_step_target(interaction)}", "click", line_start)
elif tool_name == "type_text":
typed_identity = _typed_value_identity(interaction)
@@ -444,7 +698,7 @@ def synthesize_code_block(
parameters.append(parameter)
if typed_identity is not None:
typed_param_keys[typed_identity] = param_key
- lines.append(f"{_INDENT}await {locator}.fill(str({param_key}))")
+ lines.append(f"{action_indent}await {locator}.fill(str({param_key}))")
append_step(f"Type into {_step_target(interaction)}", "input_text", line_start)
elif tool_name == CREDENTIAL_FILL_TOOL_NAME:
credential_id = str(interaction.get("credential_id") or "").strip()
@@ -464,7 +718,7 @@ def synthesize_code_block(
credential_param_key = _credential_param_key(interaction, used_param_keys)
credential_param_keys[credential_id] = credential_param_key
parameters.append({"key": credential_param_key, "credential_id": credential_id})
- lines.append(f"{_INDENT}await {locator}.fill({credential_param_key}.{credential_field})")
+ lines.append(f"{action_indent}await {locator}.fill({credential_param_key}.{credential_field})")
elif tool_name == "select_option":
value = str(interaction.get("value") or "").strip()
if not value:
@@ -473,8 +727,8 @@ def synthesize_code_block(
{"trajectory_index": trajectory_index, "tool_name": tool_name, "reason_code": "missing_value"}
)
continue
- lines.append(f"{_INDENT}await {locator}.select_option({_py_str(value)})")
- lines.append(f'{_INDENT}await page.wait_for_load_state("load")')
+ lines.append(f"{action_indent}await {locator}.select_option({_py_str(value)})")
+ lines.append(f"{action_indent}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})")
append_step(f"Select {value} in {_step_target(interaction)}", "select_option", line_start)
else:
notes.append(f"skipped unsupported interaction tool_name={tool_name!r}")
@@ -484,12 +738,10 @@ def synthesize_code_block(
continue
emitted += 1
- if (
- settings.COPILOT_DOWNLOAD_RUNG_SYNTHESIS_ENABLED
- and reached_download_target is not None
- and not reached_download_target.already_registered
- and reached_download_target.selector
- ):
+ if entry_replay_condition_active and emitted == 0 and not entry_post_auth_resume_index:
+ lines.append(f"{_INDENT * 2}pass")
+
+ if compile_download_target and reached_download_target is not None:
# The download affordance is observed in nav_targets, not necessarily a trajectory click, so the
# download is an appended terminal step compiled from the typed target — never an in-place click upgrade.
# Awaiting the download value lands the file in the run-scoped downloads dir; the execution-layer
@@ -503,6 +755,8 @@ def synthesize_code_block(
if not lines:
return None
+ if steps:
+ steps[-1]["line_end"] = len(lines)
diagnostics.emitted_interaction_count = emitted
code = "\n".join(lines) + "\n"
diff --git a/skyvern/forge/sdk/copilot/tools/blockers.py b/skyvern/forge/sdk/copilot/tools/blockers.py
index 81728b921..a99d21bfc 100644
--- a/skyvern/forge/sdk/copilot/tools/blockers.py
+++ b/skyvern/forge/sdk/copilot/tools/blockers.py
@@ -24,6 +24,7 @@ from skyvern.forge.sdk.copilot.failure_tracking import (
PER_TOOL_BUDGET_FAILURE_CATEGORY,
)
from skyvern.forge.sdk.copilot.loop_detection import detect_failed_tool_step_loop_for_ctx, detect_tool_loop
+from skyvern.forge.sdk.copilot.reached_download_target import REGISTERED_DOWNLOAD_OUTPUT_KEYS
from skyvern.forge.sdk.copilot.runtime import AgentContext
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRun, WorkflowRunStatus
from skyvern.schemas.workflows import BlockType
@@ -1127,11 +1128,33 @@ def _iter_goal_value_path_values(value: Any, path_parts: list[str]) -> list[Any]
def _code_output_goal_paths_have_content(value: Any, goal_value_paths: list[str]) -> bool:
for path in goal_value_paths:
values = _iter_goal_value_path_values(value, _normalize_goal_value_path(path))
+ if not values and _goal_value_path_targets_registered_download(path):
+ values = _registered_download_output_values(value)
if not any(_code_output_has_goal_content(item) for item in values):
return False
return True
+def _goal_value_path_targets_registered_download(path: str) -> bool:
+ normalized = path.strip()
+ if normalized.startswith("$."):
+ normalized = normalized[2:]
+ elif normalized.startswith("$"):
+ normalized = normalized[1:]
+ head = normalized.split(".", 1)[0].split("[", 1)[0].strip()
+ return head in REGISTERED_DOWNLOAD_OUTPUT_KEYS
+
+
+def _registered_download_output_values(value: Any) -> list[Any]:
+ if not isinstance(value, Mapping):
+ return []
+ return [value[key] for key in REGISTERED_DOWNLOAD_OUTPUT_KEYS if key in value]
+
+
+def _code_output_has_registered_download_content(value: Any) -> bool:
+ return any(_code_output_has_goal_content(item) for item in _registered_download_output_values(value))
+
+
def _active_block_run_budget_seconds(ctx: AgentContext) -> int:
remaining = _copilot_seconds_remaining(ctx)
if remaining is None:
@@ -1557,7 +1580,9 @@ def _analyze_run_blocks(
goal_value_paths = _goal_value_paths_for_code_block(copilot_ctx, block.get("label"))
if goal_value_paths:
has_data_blocks = True
- if _code_output_goal_paths_have_content(extracted, goal_value_paths):
+ if _code_output_has_registered_download_content(extracted) or _code_output_goal_paths_have_content(
+ extracted, goal_value_paths
+ ):
any_data_output = True
else:
# Terminal goal paths are conjunctive: one missing
diff --git a/skyvern/forge/sdk/copilot/tools/guardrails.py b/skyvern/forge/sdk/copilot/tools/guardrails.py
index 4e0bc02e6..4d0613efa 100644
--- a/skyvern/forge/sdk/copilot/tools/guardrails.py
+++ b/skyvern/forge/sdk/copilot/tools/guardrails.py
@@ -171,6 +171,13 @@ def _download_intent_block_labels(workflow_yaml: str | None) -> list[str]:
return labels
+def _has_reached_download_target(ctx: Any) -> bool:
+ target = getattr(ctx, "reached_download_target", None)
+ if not isinstance(target, ReachedDownloadTarget):
+ return False
+ return target.already_registered or bool(target.selector.strip())
+
+
def _download_scout_required_error(copilot_ctx: Any, workflow_yaml: str | None) -> str | None:
"""Reject authoring a download-intent code block until the affordance has been scout-acted
this turn, so the skyvern_evaluate post-hook can populate the reached-download target and the
@@ -186,6 +193,8 @@ def _download_scout_required_error(copilot_ctx: Any, workflow_yaml: str | None)
download_labels = _download_intent_block_labels(workflow_yaml)
if not download_labels:
return None
+ if _has_reached_download_target(copilot_ctx):
+ return None
if turn_has_scout_interaction(copilot_ctx):
return None
prior_rejections = int(getattr(copilot_ctx, "download_scout_required_rejections", 0) or 0)
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 3c0b3b4de..10639f430 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -1034,7 +1034,10 @@ def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContex
scout_trajectory = ctx.scout_trajectory
if not isinstance(scout_trajectory, list) or not scout_trajectory:
return workflow_yaml, []
- synthesized = synthesize_code_block(scout_trajectory)
+ synthesized = synthesize_code_block(
+ scout_trajectory,
+ reached_download_target=getattr(ctx, "reached_download_target", None),
+ )
if synthesized is None:
return workflow_yaml, []
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index 5314cb215..c3d695c53 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -12,9 +12,11 @@ from types import SimpleNamespace
import pytest
import yaml
+from skyvern.config import settings
from skyvern.forge.sdk.copilot.blocker_signal import assert_clean_user_facing_text
from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
from skyvern.forge.sdk.copilot.context import CopilotContext
+from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
from skyvern.forge.sdk.copilot.tools import (
_code_block_safety_errors,
_detect_stale_block_metadata,
@@ -675,6 +677,50 @@ class TestCompiledAuthoringImposition:
block = _single_code_block(parse_workflow_yaml(ctx.workflow_yaml))
assert "async with page.expect_download()" in block["code"]
+ @pytest.mark.asyncio
+ async def test_imposition_carries_reached_download_target_to_synthesized_code(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ monkeypatch.setattr(settings, "COPILOT_DOWNLOAD_RUNG_SYNTHESIS_ENABLED", True)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "div.stmt-row",
+ "source_url": "https://example.com/bills",
+ "trajectory_index": 0,
+ }
+ ]
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+ workflow_yaml = _yaml(
+ """
+ title: Download report
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: download_report
+ code: |
+ await page.goto("https://example.com/bills")
+ await page.locator("div.stmt-row").click()
+ """
+ )
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(workflow_yaml, ctx)
+
+ assert result.violations == []
+ parsed = parse_workflow_yaml(result.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert "expect_download" in block["code"]
+ assert 'await page.locator("[href=\\"/files/report.pdf\\"]").click()' in block["code"]
+
@pytest.mark.asyncio
async def test_unchanged_prior_code_does_not_impose(self, monkeypatch: pytest.MonkeyPatch) -> None:
_stub_successful_update(monkeypatch)
diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.py
index 131c36648..bb2dddf7c 100644
--- a/tests/unit/test_copilot_code_block_synthesis.py
+++ b/tests/unit/test_copilot_code_block_synthesis.py
@@ -19,6 +19,7 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
_DOWNLOAD_VAR_BASE,
_MAX_STEPS,
_SYNTHESIZED_BLOCK_LABEL,
+ CREDENTIAL_FILL_TOOL_NAME,
build_synthesized_artifact_metadata,
render_synthesized_offer_text,
synthesize_code_block,
@@ -203,12 +204,16 @@ class TestActionSynthesis:
{"key": "coupon_code", "default_value": "example_sku_123"},
]
- def test_goto_entry_url_and_load_state(self) -> None:
+ def test_entry_url_is_selector_gated_and_uses_domcontentloaded(self) -> None:
result = synthesize_code_block([_interaction("click", selector="#go", source_url="https://example.com/start")])
assert result is not None
lines = result.code.splitlines()
- assert lines[0] == ' await page.goto("https://example.com/start")'
- assert lines[1] == ' await page.wait_for_load_state("load")'
+ assert lines[0] == ' _scout_entry_target = page.locator("#go")'
+ assert lines[1] == " try:"
+ assert lines[2] == ' await _scout_entry_target.wait_for(state="visible", timeout=1000)'
+ assert lines[3] == " except Exception:"
+ assert lines[4] == ' await page.goto("https://example.com/start", wait_until="domcontentloaded")'
+ assert lines[5] == ' await _scout_entry_target.wait_for(state="visible")'
def test_press_enter_uses_keyboard_when_no_selector(self) -> None:
result = synthesize_code_block([_interaction("press_key", key="Enter")])
@@ -268,7 +273,7 @@ class TestParamKeySafety:
def _emitted_wrapper(code: str, param_keys: list[str]) -> str:
# Mirror block.py generate_async_user_function: the param keys become the wrapper signature.
signature = ", ".join(f"{key}=None" for key in param_keys)
- body = "\n".join(f" {line.strip()}" for line in code.splitlines())
+ body = "\n".join(f" {line}" for line in code.splitlines())
return f"async def wrapper({signature}):\n{body or ' pass'}"
def test_keyword_accessible_name_yields_bindable_identifier(self) -> None:
@@ -518,7 +523,10 @@ class TestTrajectoryFidelity:
assert result is not None
assert "secret-token-fragment" not in result.code
- assert 'await page.goto("https://example.com/search?q=provider#__redacted__")' in result.code
+ assert (
+ 'await page.goto("https://example.com/search?q=provider#__redacted__", wait_until="domcontentloaded")'
+ in result.code
+ )
assert metadata["page_dependencies"][0]["url_hint"] == "https://example.com/search?q=provider#__redacted__"
@@ -579,14 +587,14 @@ class TestStepEmission:
assert block is not None
code_lines = block.code.splitlines()
goto_step, click_step, fill_step, key_step = block.steps
- assert (goto_step["line_start"], goto_step["line_end"]) == (1, 2)
- assert code_lines[goto_step["line_start"] - 1].lstrip().startswith("await page.goto(")
- assert (click_step["line_start"], click_step["line_end"]) == (3, 4)
+ assert (goto_step["line_start"], goto_step["line_end"]) == (1, 6)
+ assert code_lines[goto_step["line_start"] - 1].lstrip().startswith("_scout_entry_target = ")
+ assert (click_step["line_start"], click_step["line_end"]) == (7, 8)
assert ".click()" in code_lines[click_step["line_start"] - 1]
- assert (fill_step["line_start"], fill_step["line_end"]) == (5, 5)
+ assert (fill_step["line_start"], fill_step["line_end"]) == (9, 9)
assert ".fill(" in code_lines[fill_step["line_start"] - 1]
assert key_step["action_type"] == "keypress"
- assert (key_step["line_start"], key_step["line_end"]) == (6, 7)
+ assert (key_step["line_start"], key_step["line_end"]) == (10, len(code_lines))
assert "press" in code_lines[key_step["line_start"] - 1]
# Spans are contiguous and cover the whole block.
assert block.steps[0]["line_start"] == 1
@@ -695,9 +703,7 @@ class TestLineBoundaryEscaping:
@staticmethod
def _parses(code: str) -> ast.Module:
- wrapper = "async def __wrapper__(payload=None):\n" + "\n".join(
- f" {line.strip()}" for line in code.splitlines()
- )
+ wrapper = "async def __wrapper__(payload=None):\n" + "\n".join(f" {line}" for line in code.splitlines())
return ast.parse(wrapper)
def test_accessible_name_boundary_codepoints_keep_block_parseable(self) -> None:
@@ -1167,10 +1173,49 @@ def _download_target(**fields: Any) -> ReachedDownloadTarget:
class TestDownloadRungSynthesis:
+ def test_post_auth_resume_skips_login_prefix_without_download_target(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ CREDENTIAL_FILL_TOOL_NAME,
+ selector="#user",
+ source_url="https://example.com/bills",
+ credential_id="cred_123",
+ credential_name="mock_portal_login",
+ credential_field="username",
+ ),
+ _interaction("click", selector="#contBtn"),
+ _interaction(
+ CREDENTIAL_FILL_TOOL_NAME,
+ selector="#pass",
+ credential_id="cred_123",
+ credential_name="mock_portal_login",
+ credential_field="password",
+ ),
+ _interaction("click", selector="#signinBtn"),
+ _interaction("click", selector="#current-statement-row"),
+ ],
+ )
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == " _scout_entry_resume_after_auth = False"
+ assert lines[1] == ' _scout_entry_target = page.locator("#user")'
+ assert ' await page.goto("https://example.com/bills", wait_until="domcontentloaded")' in lines
+ assert ' _scout_entry_resume_target = page.locator("#current-statement-row")' in lines
+ assert " _scout_entry_resume_after_auth = True" in lines
+ assert " if not _scout_entry_resume_after_auth:" in lines
+ assert ' await page.locator("#user").fill(mock_portal_login.username)' in lines
+ assert ' await page.locator("#current-statement-row").click()' in lines
+ assert "_scout_entry_reused_current_page" not in result.code
+ assert result.parameters == [{"key": "mock_portal_login", "credential_id": "cred_123"}]
+ ast.parse("async def _block(page):\n" + result.code)
+
def test_appended_terminal_step_compiled_from_typed_target(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(settings, "COPILOT_DOWNLOAD_RUNG_SYNTHESIS_ENABLED", True)
result = synthesize_code_block([_nav_click()], reached_download_target=_download_target())
assert result is not None
+ assert "_scout_entry_reused_current_page = False" in result.code
+ assert 'await page.goto("https://example.com/bills", wait_until="domcontentloaded")' in result.code
assert f"async with page.expect_download() as {_DOWNLOAD_VAR_BASE}:" in result.code
download_obj = f"{_DOWNLOAD_VAR_BASE}_file"
assert f"{download_obj} = await {_DOWNLOAD_VAR_BASE}.value" in result.code
diff --git a/tests/unit/test_copilot_run_output_content_gate.py b/tests/unit/test_copilot_run_output_content_gate.py
index fa0f584fa..137448ca8 100644
--- a/tests/unit/test_copilot_run_output_content_gate.py
+++ b/tests/unit/test_copilot_run_output_content_gate.py
@@ -388,6 +388,40 @@ def test_top_level_array_goal_value_paths_keep_clean_path() -> None:
assert _is_outcome_evidence_candidate(ctx, result) is True
+def test_downloaded_files_satisfy_registered_download_goal_paths() -> None:
+ result = _run_result(
+ [
+ _code_block(
+ "download_statement",
+ {
+ "downloaded_file_name": "statement.pdf",
+ "downloaded_files": [{"filename": "statement.pdf"}],
+ },
+ )
+ ]
+ )
+ ctx = _ctx(result["data"]["blocks"])
+ entry = _terminal_metadata_entry("download_statement")
+ entry["claimed_outcomes"][0]["goal_value_paths"] = ["invoice_pdf"]
+ entry["terminal_verifier_expectations"] = [
+ {
+ "id": "expectation:download_statement_terminal",
+ "criteria_ids": ["criterion:goal_0"],
+ "goal_value_paths": ["invoice_pdf"],
+ }
+ ]
+ ctx.code_artifact_metadata = {"download_statement": entry}
+
+ _, empty_data_blocks, _ = _analyze_run_blocks(result, ctx)
+ assert empty_data_blocks is False
+ assert _is_outcome_evidence_candidate(ctx, result) is True
+
+ _record_run_blocks_result(ctx, result, completion_verification=None)
+ assert ctx.last_test_ok is True
+ assert ctx.last_test_suspicious_success is False
+ assert ctx.last_full_workflow_test_ok is True
+
+
def test_array_goal_value_path_does_not_match_scalar_root() -> None:
result = _run_result([_code_block("expand_result_rows", {"number": "12345"})])
ctx = _ctx(result["data"]["blocks"])