Skip to content

Automate browser based workflows with AI

SKY-11180 Fix Copilot directory synthesized code binding (#6702)

Andrew Neilsond02ead94fb41
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index c522c08bd..e071c4c51 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -44,13 +44,22 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
)
from skyvern.forge.sdk.copilot.blocker_signal import to_trace_data as blocker_signal_to_trace_data
from skyvern.forge.sdk.copilot.build_phase import initial_build_phase
-from skyvern.forge.sdk.copilot.code_block_synthesis import render_synthesized_offer_text, synthesize_code_block
+from skyvern.forge.sdk.copilot.code_block_synthesis import (
+ is_optional_dismissal_only_trajectory,
+ render_synthesized_offer_text,
+ synthesize_code_block,
+)
from skyvern.forge.sdk.copilot.completion_criteria_store import (
StoredCriteriaSnapshot,
build_turn_state,
reconcile_completion_criteria,
)
-from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy, CopilotConfig, normalize_block_authoring_policy
+from skyvern.forge.sdk.copilot.config import (
+ SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD,
+ BlockAuthoringPolicy,
+ CopilotConfig,
+ normalize_block_authoring_policy,
+)
from skyvern.forge.sdk.copilot.context import (
COPILOT_RESPONSE_TYPES,
AgentResult,
@@ -523,12 +532,22 @@ def _synthesized_block_offer_prompt(ctx: CopilotContext | None) -> str:
if ctx.update_workflow_called:
LOG.debug("copilot_synthesized_block_offer_skipped", reason="already_authored")
return ""
- if ctx.synthesized_block_offered:
- LOG.debug("copilot_synthesized_block_offer_skipped", reason="already_offered")
- return ""
if not ctx.scout_trajectory:
LOG.debug("copilot_synthesized_block_offer_skipped", reason="empty_trajectory")
return ""
+ trajectory_len = len(ctx.scout_trajectory)
+ previous_offer_len = ctx.synthesized_block_offered_trajectory_len
+ if ctx.synthesized_block_offered and trajectory_len < previous_offer_len + SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD:
+ LOG.debug(
+ "copilot_synthesized_block_offer_skipped",
+ reason="already_offered",
+ previous_trajectory_len=previous_offer_len,
+ trajectory_len=trajectory_len,
+ )
+ return ""
+ if is_optional_dismissal_only_trajectory(ctx.scout_trajectory):
+ LOG.debug("copilot_synthesized_block_offer_skipped", reason="optional_dismissal_only")
+ return ""
synthesized = synthesize_code_block(ctx.scout_trajectory, reached_download_target=ctx.reached_download_target)
if synthesized is None:
LOG.debug(
@@ -538,9 +557,11 @@ def _synthesized_block_offer_prompt(ctx: CopilotContext | None) -> str:
)
return ""
ctx.synthesized_block_offered = True
+ ctx.synthesized_block_offered_trajectory_len = trajectory_len
LOG.info(
"copilot_synthesized_block_offer_rendered",
- trajectory_len=len(ctx.scout_trajectory),
+ trajectory_len=trajectory_len,
+ previous_trajectory_len=previous_offer_len,
code_len=len(synthesized.code),
)
goal = ctx.block_goal_main_goal or ctx.user_message or ""
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index b530405b4..a6fe54344 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -8,9 +8,11 @@ that runs on the raw ``page`` object the copilot code block executes against.
from __future__ import annotations
+import io
import json
import keyword
import re
+import tokenize
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, field
from typing import Any
@@ -28,7 +30,16 @@ _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"
+_OPTIONAL_DISMISSAL_VAR = "_scout_optional_dismissal"
_ENTRY_LOCATOR_VARS = (_ENTRY_TARGET_VAR, _ENTRY_RESUME_TARGET_VAR, _ENTRY_OPENER_VAR)
+_INTERNAL_SCOUT_VARS = (
+ _ENTRY_TARGET_VAR,
+ _ENTRY_REUSED_VAR,
+ _ENTRY_RESUME_AFTER_AUTH_VAR,
+ _ENTRY_RESUME_TARGET_VAR,
+ _ENTRY_OPENER_VAR,
+ _OPTIONAL_DISMISSAL_VAR,
+)
# Base name for the download var bound by `async with page.expect_download() as <name>:`.
_DOWNLOAD_VAR_BASE = "dl_info"
@@ -44,6 +55,32 @@ _CREDENTIAL_FIELDS = frozenset({"username", "password", "totp"})
CREDENTIAL_FILL_CODE_PATTERN = re.compile(r"\.fill\([A-Za-z_]\w*\.\w+\)")
_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"})
+_OPTIONAL_DISMISSAL_NAME_PATTERN = re.compile(
+ r"\b(?:accept|agree|allow|consent|cookies?|decline|reject|refuse|dismiss|got it|no thanks)\b|^(?:ok|okay)$",
+ re.I,
+)
+_OPTIONAL_DISMISSAL_SELECTOR_PATTERN = re.compile(
+ r"(?:acceptcookies|cookies?|consent|decline|reject|refuse|dismiss|close)", re.I
+)
+# Used only after the scout captured an unnamed structural/not-decline cookie click
+# and a later durable target exists; this generic text fallback keeps replay conditional.
+_COOKIE_ACCEPT_FALLBACK_LOCATOR_SELECTOR = "button:has-text('Accept')"
+_NOT_DECLINE_BUTTON_SELECTOR_PATTERN = re.compile(r"^button:not\(\.decline\)(?::nth-of-type\(\d+\))?$", re.I)
+_COOKIE_ACCEPT_TEXT_XPATH_PATTERN = re.compile(
+ r"""^//button\[\s*normalize-space\(\)\s*=\s*(['"])accept\1\s*\]$""", re.I
+)
+_BODY_ROOTED_INDEXED_BUTTON_XPATH_PATTERN = re.compile(
+ r"""^(?:/\*\[name\(\)=["']html["']\]\[1\])?"""
+ r"""(?:/\*\[name\(\)=["']body["']\]\[1\])"""
+ r"""(?:/\*\[name\(\)=["'][a-z0-9_-]+["']\]\[\d+\])*"""
+ r"""/\*\[name\(\)=["']button["']\]\[\d+\]$""",
+ re.I,
+)
+_STRUCTURAL_DISMISSAL_SELECTOR_PATTERN = re.compile(
+ r"^(?:[.#][A-Za-z_][\w-]*\s+)?button(?::nth-(?:of-type|child)\(\d+\))$"
+ r"|^button:not\(\.decline\)(?::nth-of-type\(\d+\))?$",
+ re.I,
+)
_SYNTHESIZED_BLOCK_LABEL = "scout_synthesized_browser_steps"
@@ -411,11 +448,50 @@ def _is_generic_entry_opener_click(interaction: Mapping[str, Any]) -> bool:
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]):
+def _is_optional_dismissal_click(interaction: Mapping[str, Any]) -> bool:
+ if str(interaction.get("tool_name") or "") != "click":
+ return False
+ role = str(interaction.get("role") or "").strip().lower()
+ if role and role not in {"button", "link"}:
return False
- first_source_url = str(trajectory[0].get("source_url") or "").strip()
- for interaction in trajectory[1:]:
+ selector = str(interaction.get("selector") or "").strip()
+ name = str(interaction.get("accessible_name") or "").strip()
+ if name and _OPTIONAL_DISMISSAL_NAME_PATTERN.search(name):
+ return True
+ return bool(selector and _OPTIONAL_DISMISSAL_SELECTOR_PATTERN.search(selector))
+
+
+def _is_cookie_accept_xpath_selector(selector: str) -> bool:
+ normalized_selector = selector.strip().lower()
+ if normalized_selector.startswith("xpath="):
+ normalized_selector = normalized_selector[len("xpath=") :].strip()
+ return bool(
+ _COOKIE_ACCEPT_TEXT_XPATH_PATTERN.match(normalized_selector)
+ or _BODY_ROOTED_INDEXED_BUTTON_XPATH_PATTERN.match(normalized_selector)
+ )
+
+
+def _is_structural_dismissal_click(interaction: Mapping[str, Any]) -> bool:
+ if str(interaction.get("tool_name") or "") != "click":
+ return False
+ role = str(interaction.get("role") or "").strip().lower()
+ if role and role not in {"button", "link"}:
+ return False
+ if str(interaction.get("accessible_name") or "").strip():
+ return False
+ selector = str(interaction.get("selector") or "").strip()
+ return bool(
+ selector
+ and (_STRUCTURAL_DISMISSAL_SELECTOR_PATTERN.search(selector) or _is_cookie_accept_xpath_selector(selector))
+ )
+
+
+def _has_later_durable_fallback_target(
+ trajectory: Sequence[Mapping[str, Any]],
+ start_index: int,
+) -> bool:
+ first_source_url = str(trajectory[start_index].get("source_url") or "").strip()
+ for interaction in trajectory[start_index + 1 :]:
source_url = str(interaction.get("source_url") or "").strip()
if first_source_url and source_url and source_url != first_source_url:
continue
@@ -424,6 +500,47 @@ def _should_prefer_durable_entry_target(trajectory: Sequence[Mapping[str, Any]])
return False
+def _is_optional_or_structural_dismissal_click(interaction: Mapping[str, Any]) -> bool:
+ if _is_optional_dismissal_click(interaction):
+ return True
+ return _is_structural_dismissal_click(interaction)
+
+
+def is_optional_dismissal_only_trajectory(trajectory: Sequence[Mapping[str, Any]]) -> bool:
+ return bool(trajectory) and all(
+ _is_optional_or_structural_dismissal_click(interaction) for interaction in trajectory
+ )
+
+
+def _optional_dismissal_locator_expr(interaction: Mapping[str, Any], fallback_locator: str) -> str:
+ selector = str(interaction.get("selector") or "").strip()
+ if _NOT_DECLINE_BUTTON_SELECTOR_PATTERN.match(selector) or _is_cookie_accept_xpath_selector(selector):
+ # Anonymous structural/cookie-shaped selectors are brittle across reloads; name-matched dismissals
+ # keep the captured locator because the accessible name is the durable anchor.
+ return f"page.locator({_py_str(_COOKIE_ACCEPT_FALLBACK_LOCATOR_SELECTOR)})"
+ return fallback_locator
+
+
+def _should_prefer_durable_entry_target(trajectory: Sequence[Mapping[str, Any]]) -> bool:
+ if not trajectory or not (
+ _is_generic_entry_opener_click(trajectory[0])
+ or _is_optional_dismissal_click(trajectory[0])
+ or _is_structural_dismissal_click(trajectory[0])
+ ):
+ return False
+ return _has_later_durable_fallback_target(trajectory, 0)
+
+
+def _code_uses_name(source: str, name: str) -> bool:
+ try:
+ return any(
+ token.type == tokenize.NAME and token.string == name
+ for token in tokenize.generate_tokens(io.StringIO(source).readline)
+ )
+ except (IndentationError, tokenize.TokenError):
+ return bool(re.search(rf"\b{re.escape(name)}\b", source))
+
+
def _entry_target_locator(
trajectory: Sequence[Mapping[str, Any]], *, strict_selectors: bool, prefer_durable: bool = False
) -> tuple[str, int]:
@@ -531,12 +648,18 @@ def synthesize_code_block(
break
if entry_url:
entry_trajectory = trajectory[entry_index:]
+ optional_dismissal_prefix = (
+ _is_optional_or_structural_dismissal_click(entry_trajectory[0]) if entry_trajectory else False
+ )
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,
)
+ if optional_dismissal_prefix and fallback_entry_relative_index == 0:
+ fallback_entry_target = ""
+ fallback_entry_relative_index = -1
fallback_entry_index = (
entry_index + fallback_entry_relative_index if fallback_entry_relative_index >= 0 else entry_index
)
@@ -556,7 +679,9 @@ def synthesize_code_block(
)
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
+ entry_replay_start_index = (
+ fallback_entry_index if fallback_entry_index > entry_index and not optional_dismissal_prefix 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:
@@ -725,8 +850,20 @@ def synthesize_code_block(
line_start = len(lines) + 1
if tool_name == "click":
- lines.append(f"{action_indent}await {locator}.click()")
- lines.append(f"{action_indent}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})")
+ if _is_optional_or_structural_dismissal_click(interaction):
+ optional_locator = _optional_dismissal_locator_expr(interaction, locator)
+ lines.append(f"{action_indent}{_OPTIONAL_DISMISSAL_VAR} = {optional_locator}")
+ lines.append(f"{action_indent}if await {_OPTIONAL_DISMISSAL_VAR}.count() > 0:")
+ lines.append(f"{action_indent}{_INDENT}try:")
+ lines.append(f"{action_indent}{_INDENT * 2}await {_OPTIONAL_DISMISSAL_VAR}.first.click(timeout=1000)")
+ lines.append(
+ f"{action_indent}{_INDENT * 2}await page.wait_for_load_state({_py_str(_DOMCONTENTLOADED)})"
+ )
+ lines.append(f"{action_indent}{_INDENT}except Exception:")
+ lines.append(f"{action_indent}{_INDENT * 2}pass")
+ else:
+ 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)
@@ -737,6 +874,14 @@ def synthesize_code_block(
typed_value = str(interaction.get("typed_value") or "").strip()
if typed_value:
parameter["default_value"] = typed_value
+ typed_length = interaction.get("typed_length")
+ if strict_selectors and typed_length is not None:
+ try:
+ typed_length_int = int(typed_length)
+ except (TypeError, ValueError):
+ typed_length_int = 0
+ if typed_length_int > 0:
+ parameter["typed_length"] = str(typed_length_int)
parameters.append(parameter)
if typed_identity is not None:
typed_param_keys[typed_identity] = param_key
@@ -803,6 +948,16 @@ def synthesize_code_block(
if not lines:
return None
+ emitted_code = "\n".join(lines)
+ for scout_var in _INTERNAL_SCOUT_VARS:
+ if not _code_uses_name(emitted_code, scout_var):
+ continue
+ # Code-block safe vars expose `Exception`, not `NameError`; this cleanup only
+ # swallows missing generated scout locals after guarded branches.
+ lines.append(f"{_INDENT}try:")
+ lines.append(f"{_INDENT * 2}del {scout_var}")
+ lines.append(f"{_INDENT}except Exception:")
+ lines.append(f"{_INDENT * 2}pass")
if steps:
steps[-1]["line_end"] = len(lines)
diff --git a/skyvern/forge/sdk/copilot/config.py b/skyvern/forge/sdk/copilot/config.py
index 300ac60bd..5c2af53c6 100644
--- a/skyvern/forge/sdk/copilot/config.py
+++ b/skyvern/forge/sdk/copilot/config.py
@@ -35,6 +35,7 @@ def download_scout_act_required_for_policy(block_authoring_policy: BlockAuthorin
DEFAULT_PROMPT_TEMPLATE = "workflow-copilot-agent.j2"
DEFAULT_MAX_TURNS = 35
DEFAULT_TOKEN_BUDGET = 90_000
+SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD = 3
SCREENSHOT_DROPPED_NUDGE = (
"Your previous screenshot was dropped from context to recover from a token-budget overflow. "
diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.py
index 99d09b0cb..b1f61e9f8 100644
--- a/skyvern/forge/sdk/copilot/enforcement.py
+++ b/skyvern/forge/sdk/copilot/enforcement.py
@@ -21,7 +21,11 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
stash_blocker_signal,
)
from skyvern.forge.sdk.copilot.build_phase import DISCOVERY_PERMITTED_PHASES
-from skyvern.forge.sdk.copilot.code_block_synthesis import render_synthesized_offer_text, synthesize_code_block
+from skyvern.forge.sdk.copilot.code_block_synthesis import (
+ is_optional_dismissal_only_trajectory,
+ render_synthesized_offer_text,
+ synthesize_code_block,
+)
from skyvern.forge.sdk.copilot.config import (
DEFAULT_ENFORCEMENT_NUDGES,
DEFAULT_TOKEN_BUDGET,
@@ -45,6 +49,7 @@ from skyvern.forge.sdk.copilot.config import (
POST_UPDATE_NUDGE,
PRE_DISCOVERY_URL_QUESTION_NUDGE,
SCREENSHOT_DROPPED_NUDGE,
+ SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD,
BlockAuthoringPolicy,
CopilotConfig,
normalize_block_authoring_policy,
@@ -1632,11 +1637,10 @@ def _maybe_synthesized_block_offer_msg(ctx: Any) -> dict[str, Any] | None:
Returns a single user message wrapping the synthesized Playwright block, or
None when the policy/latch/empty-trajectory guards do not hold. Shares the
- one-shot latch with the pre-authoring prompt-side offer, so whichever fires
- first wins and the model sees the offer at most once.
+ latch with the pre-authoring prompt-side offer. The initial offer suppresses
+ near-duplicate repeats, but a materially longer scout trajectory can refresh
+ the deterministic code before the model authors the workflow.
"""
- if getattr(ctx, "synthesized_block_offered", False):
- return None
if getattr(ctx, "update_workflow_called", False):
return None
if normalize_block_authoring_policy(getattr(ctx, "block_authoring_policy", None)) != (
@@ -1646,6 +1650,15 @@ def _maybe_synthesized_block_offer_msg(ctx: Any) -> dict[str, Any] | None:
trajectory = getattr(ctx, "scout_trajectory", None) or []
if not trajectory:
return None
+ if is_optional_dismissal_only_trajectory(trajectory):
+ return None
+ trajectory_len = len(trajectory)
+ previous_offer_len = getattr(ctx, "synthesized_block_offered_trajectory_len", 0) or 0
+ if (
+ getattr(ctx, "synthesized_block_offered", False)
+ and trajectory_len < previous_offer_len + SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD
+ ):
+ return None
synthesized = synthesize_code_block(
trajectory, reached_download_target=getattr(ctx, "reached_download_target", None)
)
@@ -1653,6 +1666,7 @@ def _maybe_synthesized_block_offer_msg(ctx: Any) -> dict[str, Any] | None:
return None
ctx.synthesized_block_offered = True
+ ctx.synthesized_block_offered_trajectory_len = trajectory_len
goal = getattr(ctx, "block_goal_main_goal", "") or getattr(ctx, "user_message", "") or ""
return {"role": "user", "content": render_synthesized_offer_text(synthesized, trajectory, goal=goal)}
diff --git a/skyvern/forge/sdk/copilot/mcp_adapter.py b/skyvern/forge/sdk/copilot/mcp_adapter.py
index f6b66407e..e4488ecd2 100644
--- a/skyvern/forge/sdk/copilot/mcp_adapter.py
+++ b/skyvern/forge/sdk/copilot/mcp_adapter.py
@@ -68,6 +68,7 @@ _POST_HOOK_CONTEXT_ROLLBACK_FIELDS = (
"last_auto_acted_signature",
"reached_download_target",
"synthesized_block_offered",
+ "synthesized_block_offered_trajectory_len",
)
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index a91853578..3a338822e 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -276,6 +276,7 @@ class AgentContext:
# expect_download step from it. Selector is the observed download link, not necessarily a trajectory click.
reached_download_target: ReachedDownloadTarget | None = None
synthesized_block_offered: bool = False
+ synthesized_block_offered_trajectory_len: int = 0
# Count of times the scout-act download gate rejected a download-intent block this turn. Bounds
# the author->scout->re-author cycle so a genuinely un-scoutable affordance halts honestly.
download_scout_required_rejections: int = 0
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index b9d19b27d..753537d73 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -1,9 +1,12 @@
from __future__ import annotations
import ast
+import io
import json
+import keyword
import re
import textwrap
+import tokenize
from collections.abc import Iterator, Mapping
from dataclasses import dataclass
from dataclasses import field as dataclass_field
@@ -420,7 +423,15 @@ def _normalize_code_artifact_metadata_detailed(
)
)
block_code = str(code_blocks[label].get("code") or "")
- return_shape_error = _extraction_return_shape_error(label, dumped, block_code)
+ require_declared_output = impose_defaults and (
+ len(code_blocks) == 1 or _block_declares_output_intent(code_blocks[label])
+ )
+ return_shape_error = _extraction_return_shape_error(
+ label,
+ dumped,
+ block_code,
+ require_declared_output=require_declared_output,
+ )
if return_shape_error is not None:
item_violations.append(return_shape_error)
schema_conformance_error = _extraction_schema_conformance_error(label, dumped, block_code)
@@ -736,9 +747,150 @@ def _code_block_safety_errors(workflow_yaml: str | None, prior_yaml: str | None)
def _code_block_parameter_keys(block: Mapping[str, Any]) -> frozenset[str]:
raw_keys = block.get("parameter_keys")
- if not isinstance(raw_keys, list):
- return frozenset()
- return frozenset(key for key in raw_keys if isinstance(key, str) and key)
+ keys = {key for key in raw_keys if isinstance(key, str) and key} if isinstance(raw_keys, list) else set()
+ # Synthesized blocks may submit full parameter rows before the persist seam
+ # re-derives them into `parameter_keys`, so validate both sources.
+ raw_parameters = block.get("parameters")
+ if isinstance(raw_parameters, list):
+ keys.update(
+ str(parameter.get("key") or "").strip()
+ for parameter in raw_parameters
+ if isinstance(parameter, Mapping) and str(parameter.get("key") or "").strip()
+ )
+ return frozenset(keys)
+
+
+def _conflict_marker_for_line(line: str) -> str | None:
+ # Match marker text after incidental whitespace, while callers decide
+ # whether indented markers are valid YAML string content for their surface.
+ stripped = line.strip()
+ if not stripped:
+ return None
+ if stripped == "=======":
+ return stripped
+ for prefix in ("<<<<<<<", ">>>>>>>"):
+ if stripped == prefix or stripped.startswith(f"{prefix} "):
+ return stripped
+ return None
+
+
+def _raw_workflow_yaml_conflict_marker_error(workflow_yaml: str) -> str | None:
+ for line_number, line in enumerate(workflow_yaml.splitlines(), start=1):
+ marker = _conflict_marker_for_line(line)
+ if marker is not None and line == line.lstrip():
+ return (
+ f"Workflow YAML contains unresolved conflict marker `{marker}` on line {line_number}. "
+ "Remove every git conflict marker line and submit valid workflow YAML before retrying."
+ )
+ return None
+
+
+def _declared_workflow_parameter_keys(parsed: dict[str, Any]) -> set[str] | None:
+ workflow_definition = parsed.get("workflow_definition")
+ if not isinstance(workflow_definition, dict):
+ return set()
+ parameters = workflow_definition.get("parameters")
+ if parameters is None:
+ return set()
+ if not isinstance(parameters, list):
+ return None
+ return {
+ str(parameter.get("key") or "").strip()
+ for parameter in parameters
+ if isinstance(parameter, Mapping) and str(parameter.get("key") or "").strip()
+ }
+
+
+_ORDERED_CHILD_BLOCK_LIST_KEYS = ("loop_blocks", "blocks")
+_ORDERED_BRANCH_LIST_KEYS = ("branch_conditions", "branches", "ordered_branches")
+
+
+def _code_block_parameter_contract_error(workflow_yaml: str) -> str | None:
+ parsed = parse_workflow_yaml(workflow_yaml)
+ if not isinstance(parsed, dict):
+ return None
+ declared_parameter_keys = _declared_workflow_parameter_keys(parsed)
+ if declared_parameter_keys is None:
+ return "Unable to validate code-block parameter keys: workflow_definition.parameters must be a list."
+ registered_download_keys = set(REGISTERED_DOWNLOAD_OUTPUT_KEYS)
+
+ errors: list[str] = []
+
+ def block_output_key(block: Mapping[str, Any]) -> str | None:
+ label = str(block.get("label") or "").strip()
+ return f"{label}_output" if label else None
+
+ def check_code_block(block: Mapping[str, Any], available_parameter_keys: set[str]) -> None:
+ label = str(block.get("label") or "").strip() or "unlabeled code block"
+ code = str(block.get("code") or "")
+ for line_number, line in enumerate(code.splitlines(), start=1):
+ marker = _conflict_marker_for_line(line)
+ if marker is not None:
+ errors.append(
+ f"Code block `{label}` contains unresolved conflict marker `{marker}` on code line "
+ f"{line_number}. Remove every git conflict marker line before retrying."
+ )
+ break
+ parameter_keys = _code_block_parameter_keys(block)
+ registered_keys = sorted(parameter_keys & registered_download_keys)
+ if registered_keys:
+ joined = ", ".join(f"`{key}`" for key in registered_keys)
+ errors.append(
+ f"Code block `{label}` lists registered download output key(s) {joined} in `parameter_keys`, "
+ "but the execution layer injects registered download output keys only after a browser download "
+ "fires. Remove them from `parameter_keys` and return a small descriptor instead."
+ )
+ undeclared_keys = sorted(parameter_keys - available_parameter_keys - registered_download_keys)
+ if undeclared_keys:
+ joined = ", ".join(f"`{key}`" for key in undeclared_keys)
+ errors.append(
+ f"Code block `{label}` lists undeclared `parameter_keys`: {joined}. Declare each workflow input "
+ "under `workflow_definition.parameters`, use a prior block output key such as "
+ "`<block_label>_output`, or remove stale `parameter_keys` entries before retrying."
+ )
+
+ def visit_branch(branch: Mapping[str, Any], available_parameter_keys: set[str]) -> None:
+ for key in _ORDERED_CHILD_BLOCK_LIST_KEYS:
+ # Child scopes inherit known keys without leaking their outputs back into sibling branches.
+ visit_blocks(branch.get(key), set(available_parameter_keys))
+ for branch_key in _ORDERED_BRANCH_LIST_KEYS:
+ branches = branch.get(branch_key)
+ if not isinstance(branches, list):
+ continue
+ for nested_branch in branches:
+ if isinstance(nested_branch, Mapping):
+ # Branch scopes intentionally isolate output keys from their parent branch.
+ visit_branch(nested_branch, set(available_parameter_keys))
+
+ def visit_blocks(blocks: Any, available_parameter_keys: set[str]) -> set[str]:
+ if not isinstance(blocks, list):
+ return available_parameter_keys
+ for block in blocks:
+ if not isinstance(block, Mapping):
+ continue
+ if _enum_or_string_name(block.get("block_type")) == BlockType.CODE.value:
+ check_code_block(block, available_parameter_keys)
+ for key in _ORDERED_CHILD_BLOCK_LIST_KEYS:
+ visit_blocks(block.get(key), set(available_parameter_keys))
+ for branch_key in _ORDERED_BRANCH_LIST_KEYS:
+ branches = block.get(branch_key)
+ if not isinstance(branches, list):
+ continue
+ for branch in branches:
+ if isinstance(branch, Mapping):
+ visit_branch(branch, set(available_parameter_keys))
+ output_key = block_output_key(block)
+ if output_key:
+ available_parameter_keys.add(output_key)
+ return available_parameter_keys
+
+ workflow_definition = parsed.get("workflow_definition")
+ blocks = workflow_definition.get("blocks") if isinstance(workflow_definition, dict) else None
+ try:
+ visit_blocks(blocks, set(declared_parameter_keys))
+ except RecursionError:
+ return "Workflow YAML nesting is too deep to validate."
+ return "\n".join(errors) if errors else None
def _code_seam_rejection_user_summary(*, metadata_rejected: bool, code_rejected: bool) -> str:
@@ -783,6 +935,81 @@ def _workflow_code_blocks(parsed: dict[str, Any]) -> list[dict[str, Any]]:
]
+_CODE_OUTPUT_INTENT_RE = re.compile(
+ r"\b(?:extract|output|read\s+back|capture\s+(?:the\s+)?(?:data|fields|values)|return\s+structured)\b",
+ re.I,
+)
+
+
+def _raw_code_artifact_metadata_empty(raw_metadata: Any) -> bool:
+ return raw_metadata in (None, [], {})
+
+
+def _workflow_declares_output_parameter(parsed: Mapping[str, Any]) -> bool:
+ definition = parsed.get("workflow_definition")
+ if not isinstance(definition, Mapping):
+ return False
+ parameters = definition.get("parameters")
+ if not isinstance(parameters, list):
+ return False
+ return any(
+ isinstance(parameter, Mapping) and _enum_or_string_name(parameter.get("parameter_type")) == "output"
+ for parameter in parameters
+ )
+
+
+def _block_declares_output_intent(block: Mapping[str, Any]) -> bool:
+ text = " ".join(str(block.get(field_name) or "") for field_name in ("prompt", "description", "title", "label"))
+ return bool(_CODE_OUTPUT_INTENT_RE.search(text))
+
+
+def _existing_metadata_covers_output(label: str, existing_metadata: Any) -> bool:
+ if not isinstance(existing_metadata, Mapping):
+ return False
+ metadata = existing_metadata.get(label)
+ return isinstance(metadata, Mapping) and _artifact_declares_goal_values(metadata)
+
+
+def _missing_code_artifact_metadata_error(workflow_yaml: str, ctx: AgentContext, raw_metadata: Any) -> str | None:
+ if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
+ return None
+ parsed = parse_workflow_yaml(workflow_yaml)
+ if not isinstance(parsed, Mapping):
+ return None
+ code_blocks = _workflow_code_blocks(dict(parsed))
+ if not code_blocks:
+ return None
+ has_workflow_output = _workflow_declares_output_parameter(parsed)
+ output_intent_blocks = [block for block in code_blocks if _block_declares_output_intent(block)]
+ if has_workflow_output and not output_intent_blocks and len(code_blocks) == 1:
+ output_intent_blocks = list(code_blocks)
+ labels = [
+ str(block.get("label") or "").strip() or "this code block"
+ for block in (output_intent_blocks if has_workflow_output else code_blocks)
+ if has_workflow_output or _block_declares_output_intent(block)
+ ]
+ if not labels:
+ return None
+ existing_metadata = getattr(ctx, "code_artifact_metadata", None)
+ if _raw_code_artifact_metadata_empty(raw_metadata):
+ missing = [label for label in labels if not _existing_metadata_covers_output(label, existing_metadata)]
+ else:
+ missing = [
+ label
+ for label in labels
+ if not _raw_metadata_covers_output_label(raw_metadata, label, candidate_labels=labels)
+ ]
+ if not missing:
+ return None
+ joined = ", ".join(f"`{label}`" for label in missing)
+ return (
+ f"Code-only browser workflow output block(s) {joined} must pass `code_artifact_metadata` with concrete "
+ "`goal_value_paths` before saving or testing. Include an `extraction_schema` when the user requested "
+ "structured fields, and make the code return a keyed dict/list or expose top-level locals matching those "
+ "goal path roots."
+ )
+
+
def _submitted_code_block_changed(block: Mapping[str, Any], prior_yaml: str | None) -> bool:
label = str(block.get("label") or "").strip()
if not label or not prior_yaml:
@@ -843,6 +1070,73 @@ def _is_ignorable_entry_opener_drop(dropped: Mapping[str, Any], diagnostics: Syn
)
+def _submitted_suffix_after_synthesized_code(submitted_code: str, synthesized_code: str) -> str:
+ # Preserve a pure suffix appended after the synthesized steps. Returns empty for
+ # prepended extraction scaffolding; that shape is handled by preserve_submitted_extraction.
+ submitted = textwrap.dedent(submitted_code).strip()
+ synthesized = textwrap.dedent(synthesized_code).strip()
+ if not submitted or not synthesized:
+ return ""
+ if submitted == synthesized:
+ return ""
+ if submitted.startswith(synthesized):
+ suffix = submitted[len(synthesized) :]
+ return textwrap.dedent(suffix).lstrip("\n").rstrip()
+ submitted_lines = submitted_code.strip("\n").splitlines()
+ synthesized_lines = synthesized_code.strip("\n").splitlines()
+ if len(submitted_lines) <= len(synthesized_lines):
+ return ""
+ submitted_prefix = "\n".join(submitted_lines[: len(synthesized_lines)])
+ if textwrap.dedent(submitted_prefix).strip() != textwrap.dedent(synthesized_code).strip():
+ return ""
+ return textwrap.dedent("\n".join(submitted_lines[len(synthesized_lines) :])).lstrip("\n").rstrip()
+
+
+def _raw_metadata_item_mapping(raw_item: Any) -> Mapping[str, Any] | None:
+ if isinstance(raw_item, CodeArtifactMetadata):
+ return raw_item.model_dump(mode="json", exclude_none=True)
+ if isinstance(raw_item, Mapping):
+ return raw_item
+ return None
+
+
+def _raw_metadata_covers_output_label(raw_metadata: Any, label: str, *, candidate_labels: list[str]) -> bool:
+ if not label:
+ return False
+ unlabeled_declares_goal_values = False
+ for raw_item in _code_artifact_metadata_items(raw_metadata):
+ item = _raw_metadata_item_mapping(raw_item)
+ if item is None:
+ continue
+ item_label = str(item.get("block_label") or "").strip()
+ if item_label == label and _artifact_declares_goal_values(item):
+ return True
+ if not item_label and _artifact_declares_goal_values(item):
+ unlabeled_declares_goal_values = True
+ return len(candidate_labels) == 1 and unlabeled_declares_goal_values
+
+
+def _raw_metadata_declares_goal_values_for_block(raw_metadata: Any, label: str) -> bool:
+ if not label:
+ return False
+ for raw_item in _code_artifact_metadata_items(raw_metadata):
+ item = _raw_metadata_item_mapping(raw_item)
+ if item is None:
+ continue
+ item_label = str(item.get("block_label") or "").strip()
+ if item_label and item_label != label:
+ continue
+ if _artifact_declares_goal_values(item):
+ return True
+ return False
+
+
+def _is_submitted_code_synthesized_only(submitted_code: str, synthesized_code: str) -> bool:
+ submitted = textwrap.dedent(submitted_code).strip()
+ synthesized = textwrap.dedent(synthesized_code).strip()
+ return bool(submitted and synthesized and submitted == synthesized)
+
+
def _wrapped_code_ast(code: str) -> ast.AST | None:
body = "\n".join(f" {line}" for line in code.splitlines())
if not body.strip():
@@ -1250,6 +1544,148 @@ def _submitted_string_parameter_default(
return candidate, None
+def _string_parameter_default_value(parameter: Mapping[str, Any]) -> str | None:
+ if _is_credential_parameter(parameter):
+ return None
+ parameter_type = str(parameter.get("parameter_type") or "").lower()
+ workflow_parameter_type = str(parameter.get("workflow_parameter_type") or "").lower()
+ if parameter_type and parameter_type != "workflow":
+ return None
+ if workflow_parameter_type and workflow_parameter_type != "string":
+ return None
+ default_value = parameter.get("default_value")
+ return default_value if isinstance(default_value, str) else None
+
+
+def _matching_string_parameter_key_by_default(
+ parameters: list[Any],
+ *,
+ default_value: str,
+ exclude_key: str,
+) -> str | None:
+ if not default_value or _SECRET_LIKE_LITERAL_RE.search(default_value):
+ return None
+ matches: list[str] = []
+ for parameter in parameters:
+ if not isinstance(parameter, dict):
+ continue
+ key = str(parameter.get("key") or "").strip()
+ if not key or key == exclude_key:
+ continue
+ if _string_parameter_default_value(parameter) == default_value:
+ matches.append(key)
+ return matches[0] if len(matches) == 1 else None
+
+
+def _matching_string_parameter_key_by_default_length(
+ parameters: list[Any],
+ *,
+ default_length: int | None,
+ exclude_key: str,
+) -> str | None:
+ if default_length is None or default_length <= 0:
+ return None
+ matches: list[str] = []
+ for parameter in parameters:
+ if not isinstance(parameter, dict):
+ continue
+ key = str(parameter.get("key") or "").strip()
+ if not key or key == exclude_key:
+ continue
+ default_value = _string_parameter_default_value(parameter)
+ if not default_value or _SECRET_LIKE_LITERAL_RE.search(default_value):
+ continue
+ if len(default_value) == default_length:
+ matches.append(key)
+ return matches[0] if len(matches) == 1 else None
+
+
+def _drop_parameter_key(parameters: list[Any], key: str) -> None:
+ parameters[:] = [
+ parameter
+ for parameter in parameters
+ if not (isinstance(parameter, dict) and str(parameter.get("key") or "").strip() == key)
+ ]
+
+
+def _coerce_positive_int(value: Any) -> int | None:
+ try:
+ parsed = int(value)
+ except (TypeError, ValueError):
+ return None
+ return parsed if parsed > 0 else None
+
+
+def _replace_python_identifier(source: str, old: str, new: str) -> str:
+ if old == new:
+ return source
+ if not _is_python_identifier(old):
+ return source
+ try:
+ tokens = [
+ token._replace(string=new) if token.type == tokenize.NAME and token.string == old else token
+ for token in tokenize.generate_tokens(io.StringIO(source).readline)
+ ]
+ replaced = tokenize.untokenize(tokens)
+ ast.parse(replaced)
+ except (SyntaxError, tokenize.TokenError):
+ return source
+ return replaced
+
+
+def _is_python_identifier(value: str) -> bool:
+ return value.isidentifier() and not keyword.iskeyword(value)
+
+
+def _combined_string_default_expression(
+ parameters: list[Any],
+ *,
+ synthesized_default: str,
+ typed_length: int | None,
+) -> tuple[list[str], str] | None:
+ if not synthesized_default and typed_length is None:
+ return None
+
+ candidates: list[tuple[str, str]] = []
+ for parameter in parameters:
+ if not isinstance(parameter, dict):
+ continue
+ key = str(parameter.get("key") or "").strip()
+ if not _is_python_identifier(key):
+ continue
+ default_value = _string_parameter_default_value(parameter)
+ if not default_value or _SECRET_LIKE_LITERAL_RE.search(default_value):
+ continue
+ candidates.append((key, default_value))
+
+ matches: list[tuple[list[str], str]] = []
+ for first_index, (first_key, first_default) in enumerate(candidates):
+ for last_key, last_default in candidates[first_index + 1 :]:
+ combined_default = f"{first_default} {last_default}".strip()
+ if synthesized_default:
+ if combined_default != synthesized_default:
+ continue
+ elif len(combined_default) != typed_length:
+ continue
+ matches.append(([first_key, last_key], f'(str({first_key}) + " " + str({last_key}))'))
+ if len(matches) > 1:
+ LOG.debug(
+ "copilot_synthesized_parameter_combined_default_ambiguous",
+ match_count=len(matches),
+ synthesized_default_present=bool(synthesized_default),
+ typed_length=typed_length,
+ )
+ return None
+ return matches[0] if matches else None
+
+
+class _SynthesizedParameterReconciliation(NamedTuple):
+ parameter_keys: list[str]
+ violations: list[str]
+ aliases: dict[str, str]
+ expressions: dict[str, str]
+
+
def _reconcile_synthesized_parameters(
*,
parsed: dict[str, Any],
@@ -1257,16 +1693,20 @@ def _reconcile_synthesized_parameters(
submitted_code: str,
synthesized_parameters: list[dict[str, str]],
scout_trajectory: list[ScoutedInteraction],
-) -> tuple[list[str], list[str]]:
+) -> _SynthesizedParameterReconciliation:
workflow_definition = parsed.get("workflow_definition")
if not isinstance(workflow_definition, dict):
- return [], ["Unable to bind synthesized parameters: workflow_definition is missing."]
+ return _SynthesizedParameterReconciliation(
+ [], ["Unable to bind synthesized parameters: workflow_definition is missing."], {}, {}
+ )
parameters = workflow_definition.get("parameters")
if parameters is None:
parameters = []
workflow_definition["parameters"] = parameters
if not isinstance(parameters, list):
- return [], ["Unable to bind synthesized parameters: workflow_definition.parameters must be a list."]
+ return _SynthesizedParameterReconciliation(
+ [], ["Unable to bind synthesized parameters: workflow_definition.parameters must be a list."], {}, {}
+ )
existing_by_key = {
str(param.get("key")): param for param in parameters if isinstance(param, dict) and param.get("key")
@@ -1274,6 +1714,8 @@ def _reconcile_synthesized_parameters(
existing_credentials = credential_params(parameters)
parameter_keys: list[str] = []
violations: list[str] = []
+ aliases: dict[str, str] = {}
+ expressions: dict[str, str] = {}
non_credential_synthesized = [param for param in synthesized_parameters if not param.get("credential_id")]
typed_lengths = [
int(interaction.get("typed_length") or 0)
@@ -1293,6 +1735,8 @@ def _reconcile_synthesized_parameters(
credential_id = str(synthesized_param.get("credential_id") or "").strip()
existing = existing_by_key.get(key)
+ synthesized_default = str(synthesized_param.get("default_value") or "").strip()
+ typed_length = _coerce_positive_int(synthesized_param.get("typed_length"))
if credential_id:
if existing is not None:
if existing_credentials.get(key) != credential_id:
@@ -1311,19 +1755,39 @@ def _reconcile_synthesized_parameters(
continue
if existing is not None:
+ alias_key = _matching_string_parameter_key_by_default(
+ parameters,
+ default_value=synthesized_default,
+ exclude_key=key,
+ )
+ if alias_key is not None:
+ aliases[key] = alias_key
+ parameter_keys[-1] = alias_key
+ _drop_parameter_key(parameters, key)
+ continue
+ alias_key = _matching_string_parameter_key_by_default_length(
+ parameters,
+ default_length=typed_length,
+ exclude_key=key,
+ )
+ if alias_key is not None:
+ aliases[key] = alias_key
+ parameter_keys[-1] = alias_key
+ _drop_parameter_key(parameters, key)
+ continue
if _is_credential_parameter(existing):
violations.append(
f"Unable to bind synthesized parameter `{key}`: submitted parameter is credential-typed."
)
+ elif synthesized_default:
+ existing_default = _string_parameter_default_value(existing)
+ if existing_default != synthesized_default:
+ violations.append(
+ f"Unable to bind synthesized parameter `{key}`: "
+ "submitted parameter default does not match the scout record."
+ )
continue
- if len(non_credential_synthesized) != 1:
- violations.append(
- f"Unable to bind synthesized parameter `{key}`: missing submitted workflow parameter and literal binding is ambiguous."
- )
- continue
-
- synthesized_default = str(synthesized_param.get("default_value") or "").strip()
if synthesized_default:
# Narrow defense-in-depth backstop for synthesized rows; values
# captured from live type_text scouting are fully screened by
@@ -1333,9 +1797,63 @@ def _reconcile_synthesized_parameters(
f"Unable to bind synthesized parameter `{key}`: synthesized default looks credential-like."
)
continue
+ alias_key = _matching_string_parameter_key_by_default(
+ parameters,
+ default_value=synthesized_default,
+ exclude_key=key,
+ )
+ if alias_key is not None:
+ aliases[key] = alias_key
+ parameter_keys[-1] = alias_key
+ continue
+ combined_expression = _combined_string_default_expression(
+ parameters,
+ synthesized_default=synthesized_default,
+ typed_length=typed_length,
+ )
+ if combined_expression is not None:
+ expression_keys, expression = combined_expression
+ parameter_keys.pop()
+ for expression_key in expression_keys:
+ if expression_key not in parameter_keys:
+ parameter_keys.append(expression_key)
+ expressions[key] = expression
+ continue
parameters.append(_string_parameter_row(synthesized_default, key))
continue
+ if len(non_credential_synthesized) != 1:
+ alias_key = _matching_string_parameter_key_by_default_length(
+ parameters,
+ default_length=typed_length,
+ exclude_key=key,
+ )
+ if alias_key is not None:
+ aliases[key] = alias_key
+ parameter_keys[-1] = alias_key
+ continue
+
+ combined_expression = _combined_string_default_expression(
+ parameters,
+ synthesized_default=synthesized_default,
+ typed_length=typed_length,
+ )
+ if combined_expression is not None:
+ expression_keys, expression = combined_expression
+ parameter_keys.pop()
+ for expression_key in expression_keys:
+ if expression_key not in parameter_keys:
+ parameter_keys.append(expression_key)
+ expressions[key] = expression
+ continue
+
+ if len(non_credential_synthesized) != 1:
+ # Fall through only after length aliasing and combined-expression binding both miss.
+ violations.append(
+ f"Unable to bind synthesized parameter `{key}`: missing submitted workflow parameter and literal binding is ambiguous."
+ )
+ continue
+
typed_length = typed_lengths[0] if len(typed_lengths) == 1 else None
submitted_parameter, error = _submitted_string_parameter_default(
parameters,
@@ -1365,7 +1883,9 @@ def _reconcile_synthesized_parameters(
)
code_block["parameter_keys"] = parameter_keys
- return parameter_keys, violations
+ # Submitted synthesized parameter rows are re-derived from scout evidence before persistence.
+ code_block.pop("parameters", None)
+ return _SynthesizedParameterReconciliation(parameter_keys, violations, aliases, expressions)
def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext) -> _SynthesizedCodeImpositionResult:
@@ -1425,29 +1945,52 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
violations.append("Unable to impose synthesized code block: locator provenance was not byte-equal.")
break
- parameter_keys, parameter_violations = _reconcile_synthesized_parameters(
+ parameter_reconciliation = _reconcile_synthesized_parameters(
parsed=parsed,
code_block=code_block,
submitted_code=submitted_code,
synthesized_parameters=synthesized.parameters,
scout_trajectory=scout_trajectory,
)
- violations.extend(parameter_violations)
+ violations.extend(parameter_reconciliation.violations)
if violations:
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml, violations=violations)
- code_block["code"] = textwrap.dedent(synthesized.code).lstrip("\n")
+ extraction_suffix = _submitted_suffix_after_synthesized_code(submitted_code, synthesized.code)
+ raw_metadata = getattr(ctx, "raw_code_artifact_metadata", None)
+ preserve_submitted_extraction = (
+ bool(raw_metadata)
+ and _raw_metadata_declares_goal_values_for_block(raw_metadata, str(code_block.get("label") or ""))
+ and not extraction_suffix
+ and not _is_submitted_code_synthesized_only(submitted_code, synthesized.code)
+ )
+ imposed_code = textwrap.dedent(submitted_code if preserve_submitted_extraction else synthesized.code).lstrip("\n")
+ if extraction_suffix:
+ imposed_code = imposed_code.rstrip() + "\n" + extraction_suffix.rstrip() + "\n"
+ for old_key, new_key in parameter_reconciliation.aliases.items():
+ imposed_code = _replace_python_identifier(imposed_code, old_key, new_key)
+ for old_key, expression in parameter_reconciliation.expressions.items():
+ imposed_code = _replace_python_identifier(imposed_code, old_key, expression)
+ code_block["code"] = imposed_code
credential_parameter_keys = [
str(param.get("key") or "") for param in synthesized.parameters if str(param.get("credential_id") or "").strip()
]
substitutions = {
"block_label": str(code_block.get("label") or ""),
"source_trajectory_count": len(scout_trajectory),
- "parameter_keys": parameter_keys,
+ "parameter_keys": parameter_reconciliation.parameter_keys,
"credential_parameter_keys": credential_parameter_keys,
"selector_provenance": diagnostics.locator_provenance,
"prior_source": prior_source,
}
+ if parameter_reconciliation.aliases:
+ substitutions["parameter_aliases"] = parameter_reconciliation.aliases
+ if parameter_reconciliation.expressions:
+ substitutions["parameter_expressions"] = parameter_reconciliation.expressions
+ if extraction_suffix:
+ substitutions["preserved_extraction_suffix"] = True
+ if preserve_submitted_extraction:
+ substitutions["preserved_submitted_extraction_code"] = True
return _SynthesizedCodeImpositionResult(
workflow_yaml=yaml.safe_dump(parsed, sort_keys=False),
substitutions=substitutions,
@@ -1606,7 +2149,66 @@ def _artifact_declares_goal_values(artifact: Mapping[str, Any]) -> bool:
return False
-def _extraction_return_shape_error(label: str, artifact: Mapping[str, Any], code: str) -> str | None:
+def _artifact_goal_value_roots(artifact: Mapping[str, Any]) -> set[str]:
+ roots: set[str] = set()
+ for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):
+ for row in _artifact_rows(artifact.get(field_name)):
+ for path in _artifact_goal_value_paths(row.get("goal_value_paths")):
+ root = path.split(".", 1)[0].split("[", 1)[0].strip()
+ if root:
+ roots.add(root)
+ return roots
+
+
+def _target_names(target: ast.expr) -> set[str]:
+ if isinstance(target, ast.Name):
+ return {target.id}
+ if isinstance(target, (ast.Tuple, ast.List)):
+ names: set[str] = set()
+ for element in target.elts:
+ names.update(_target_names(element))
+ return names
+ return set()
+
+
+def _assigned_top_level_names(statements: list[ast.stmt]) -> set[str]:
+ names: set[str] = set()
+ for node in _iter_top_level_scope(statements):
+ if isinstance(node, ast.Assign):
+ for target in node.targets:
+ names.update(_target_names(target))
+ elif isinstance(node, ast.AnnAssign):
+ names.update(_target_names(node.target))
+ elif isinstance(node, (ast.AugAssign, ast.NamedExpr)):
+ names.update(_target_names(node.target))
+ elif isinstance(node, (ast.For, ast.AsyncFor)):
+ names.update(_target_names(node.target))
+ return names
+
+
+def _missing_declared_output_roots(code: str, goal_roots: set[str]) -> set[str] | None:
+ if not goal_roots:
+ return None
+ try:
+ tree = ast.parse(textwrap.dedent(code).strip() or "pass")
+ except SyntaxError:
+ # Code safety reports syntax errors first; avoid layering output-root errors onto invalid code.
+ return None
+ scope_statements = list(_iter_top_level_scope(tree.body))
+ if any(isinstance(node, ast.Return) and node.value is not None for node in scope_statements):
+ return None
+ assigned_names = _assigned_top_level_names(tree.body)
+ missing = goal_roots - assigned_names
+ return missing or None
+
+
+def _extraction_return_shape_error(
+ label: str,
+ artifact: Mapping[str, Any],
+ code: str,
+ *,
+ require_declared_output: bool = False,
+) -> str | None:
"""Reject an extraction-intent code block whose declared goal values do not reach
the block output as a keyed structure: a flat text blob, or structured data trapped
in an uninvoked nested function. Extraction-intent is the existing `goal_value_paths`
@@ -1630,6 +2232,16 @@ def _extraction_return_shape_error(label: str, artifact: Mapping[str, Any], code
"Call that function and return its result (e.g. `return await run(page)`), or build the keyed "
"structure at the top level so the declared goal values reach the block output."
)
+ if require_declared_output:
+ missing_roots = _missing_declared_output_roots(code, _artifact_goal_value_roots(artifact))
+ if missing_roots:
+ missing = ", ".join(f"`{root}`" for root in sorted(missing_roots))
+ return (
+ f"Code block `{label}` declares `goal_value_paths` but does not return a keyed structure or leave "
+ f"top-level output local(s) matching the declared path root(s): {missing}. Add an explicit "
+ "structured `return` (a dict, or an array of objects for repeated records), or assign those "
+ "top-level locals so the implicit code-block output contains the declared goal values."
+ )
return None
@@ -2169,11 +2781,19 @@ async def _update_workflow(
return {"ok": False, "error": authority_error}
workflow_yaml = params["workflow_yaml"]
+ raw_conflict_marker_error = _raw_workflow_yaml_conflict_marker_error(workflow_yaml)
+ if raw_conflict_marker_error is not None:
+ return {
+ "ok": False,
+ "error": raw_conflict_marker_error,
+ "user_facing_summary": _compiled_authoring_user_summary(),
+ }
# Tool wrappers run authority/loop guards before calling here. The composition
# gate below consumes these refs, so they must be visible before validation.
ctx.raw_block_observation_refs = params.get("raw_block_observation_refs", params.get("block_observation_refs"))
ctx.block_observation_refs = normalize_block_observation_refs(params.get("block_observation_refs"))
ctx.raw_code_artifact_metadata = params.get("raw_code_artifact_metadata", params.get("code_artifact_metadata"))
+ # Imposition reconciles synthesized aliases/parameters before the persisted YAML contract is checked.
imposition = _maybe_impose_synthesized_code_block(workflow_yaml, ctx)
if imposition.violations:
return {
@@ -2190,6 +2810,24 @@ async def _update_workflow(
"user_facing_summary": _compiled_authoring_user_summary(),
}
params["workflow_yaml"] = workflow_yaml
+ parameter_contract_error = _code_block_parameter_contract_error(workflow_yaml)
+ if parameter_contract_error is not None:
+ return {
+ "ok": False,
+ "error": parameter_contract_error,
+ "user_facing_summary": _compiled_authoring_user_summary(),
+ }
+ missing_metadata_error = _missing_code_artifact_metadata_error(
+ workflow_yaml,
+ ctx,
+ params.get("code_artifact_metadata"),
+ )
+ if missing_metadata_error is not None:
+ return {
+ "ok": False,
+ "error": missing_metadata_error,
+ "user_facing_summary": _compiled_authoring_user_summary(),
+ }
scout_trajectory = getattr(ctx, "scout_trajectory", None)
normalization = _normalize_code_artifact_metadata_detailed(
params.get("code_artifact_metadata"),
diff --git a/tests/unit/test_copilot_code_artifact_metadata_violations.py b/tests/unit/test_copilot_code_artifact_metadata_violations.py
index 29c06a533..6bc9ab93a 100644
--- a/tests/unit/test_copilot_code_artifact_metadata_violations.py
+++ b/tests/unit/test_copilot_code_artifact_metadata_violations.py
@@ -31,6 +31,7 @@ def _code_block_yaml(label: str) -> str:
label: {label}
code: |
await page.goto("https://example.com/")
+ return {{"records": [{{"number": "123"}}]}}
"""
).strip()
@@ -127,10 +128,12 @@ class TestAccumulateAllViolations:
label: block_one
code: |
await page.goto("https://example.com/")
+ return {"records": [{"number": "123"}]}
- block_type: code
label: block_two
code: |
await page.goto("https://example.com/")
+ return {"records": [{"number": "123"}]}
"""
).strip()
normalized, error = _normalize_code_artifact_metadata(
@@ -584,7 +587,7 @@ class TestExtractionReturnShape:
def test_keyed_dict_return_passes(self) -> None:
code = """
await page.goto("https://example.com/")
- return {"records": [{"number": "1-25-80030"}]}
+ return {"records": [{"number": "REC-001"}]}
"""
normalized, error = _normalize_code_artifact_metadata(
[_extraction_metadata("my_block", ["records[].number"])],
@@ -635,7 +638,7 @@ class TestExtractionUninvokedNestedReturn:
def test_uninvoked_nested_structured_function_is_rejected(self) -> None:
code = """
async def run(page):
- result = {"records": [{"number": "1-25-80030"}]}
+ result = {"records": [{"number": "REC-001"}]}
return result
"""
normalized, error = _normalize_code_artifact_metadata(
@@ -650,7 +653,7 @@ class TestExtractionUninvokedNestedReturn:
def test_invoked_and_returned_nested_function_passes(self) -> None:
code = """
async def run(page):
- return {"records": [{"number": "1-25-80030"}]}
+ return {"records": [{"number": "REC-001"}]}
return await run(page)
"""
normalized, error = _normalize_code_artifact_metadata(
@@ -663,7 +666,7 @@ class TestExtractionUninvokedNestedReturn:
def test_invoked_and_bound_nested_function_passes(self) -> None:
code = """
async def run(page):
- return {"records": [{"number": "1-25-80030"}]}
+ return {"records": [{"number": "REC-001"}]}
data = await run(page)
"""
normalized, error = _normalize_code_artifact_metadata(
@@ -676,7 +679,7 @@ class TestExtractionUninvokedNestedReturn:
def test_top_level_structured_local_passes(self) -> None:
code = """
await page.goto("https://example.com/")
- records = [{"number": "1-25-80030"}]
+ records = [{"number": "REC-001"}]
"""
normalized, error = _normalize_code_artifact_metadata(
[_extraction_metadata("my_block", ["records[].number"])],
@@ -719,7 +722,7 @@ class TestExtractionUninvokedNestedReturn:
rows = await page.locator(".row").all()
except Exception:
rows = []
- return {"records": [{"number": "1-25-80030"}]}
+ return {"records": [{"number": "REC-001"}]}
"""
normalized, error = _normalize_code_artifact_metadata(
[_extraction_metadata("my_block", ["records[].number"])],
@@ -731,7 +734,7 @@ class TestExtractionUninvokedNestedReturn:
def test_capture_then_wrap_rebind_passes(self) -> None:
code = """
text = await page.locator("#results").inner_text()
- text = {"records": [{"number": "1-25-80030"}]}
+ text = {"records": [{"number": "REC-001"}]}
return text
"""
normalized, error = _normalize_code_artifact_metadata(
@@ -866,7 +869,7 @@ class TestFlatStringClassifier:
# string_locals so the final structured return is not falsely rejected.
code = """
text = await page.inner_text("body")
- text = {"records": [{"number": "1-25-80030"}]}
+ text = {"records": [{"number": "REC-001"}]}
return text
"""
assert _code_block_returns_flat_string(textwrap.dedent(code)) is False
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index f74ca94bc..2282f2a6e 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -54,6 +54,19 @@ _SAFE_CODE_YAML = _yaml(
"""
)
+_SAFE_EXTRACTION_CODE_YAML = _yaml(
+ """
+ title: Registry lookup
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: search_registry
+ code: |
+ await page.goto("https://example.com/search")
+ records = [{"number": "REC-001"}]
+ """
+)
+
def _code_yaml(
code: str,
@@ -76,7 +89,7 @@ def _code_yaml(
"parameter_type": "workflow",
"workflow_parameter_type": "string",
"key": "provider_query",
- "default_value": "Taylor Brooks",
+ "default_value": "Sample Search",
}
]
if nested:
@@ -98,7 +111,7 @@ _SUBMITTED_LITERAL_YAML = _yaml(
- block_type: code
label: search_registry
code: |
- await page.locator("input[placeholder='Search']").fill("Taylor Brooks")
+ await page.locator("input[placeholder='Search']").fill("Sample Search")
await page.locator("button.lookup").click()
"""
)
@@ -123,7 +136,7 @@ _SUBMITTED_LOCAL_CONSTANT_YAML = _yaml(
- block_type: code
label: search_registry
code: |
- provider_query = "Taylor Brooks"
+ provider_query = "Sample Search"
await page.locator("input[placeholder='Search']").fill(str(provider_query))
"""
)
@@ -136,7 +149,7 @@ _SUBMITTED_COMPUTED_PARAMETER_YAML = _yaml(
- parameter_type: workflow
workflow_parameter_type: string
key: provider_query
- default_value: Taylor Brooks
+ default_value: Sample Search
blocks:
- block_type: code
label: search_registry
@@ -153,7 +166,7 @@ _SUBMITTED_MIXED_LITERAL_YAML = _yaml(
- block_type: code
label: search_registry
code: |
- await page.locator("input[placeholder='Search']").fill("Taylor Brooks")
+ await page.locator("input[placeholder='Search']").fill("Sample Search")
await page.locator("#other").fill(provider_name)
"""
)
@@ -261,6 +274,11 @@ def _credential_code_yaml(*, code: str, credential_id: str = "cred_missing") ->
)
+def _directory_blocks_yaml(blocks: str) -> str:
+ indented_blocks = textwrap.indent(textwrap.dedent(blocks).strip(), " ")
+ return f"title: Directory lookup\nworkflow_definition:\n blocks:\n{indented_blocks}\n"
+
+
def _credential_fill_interaction(
field: str,
*,
@@ -495,6 +513,273 @@ class TestCodeSafetySeam:
assert ctx.code_artifact_metadata == {}
+class TestCodeBlockParameterPersistSeam:
+ @pytest.mark.asyncio
+ async def test_undeclared_parameter_key_rejects_before_persist(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ ctx.workflow_yaml = _SAFE_CODE_YAML
+ submitted = _directory_blocks_yaml(
+ """
+ - block_type: code
+ label: search_directory
+ parameter_keys: [address_or_postal_code]
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "search_directory" in result["error"]
+ assert "`address_or_postal_code`" in result["error"]
+ assert "workflow_definition.parameters" in result["error"]
+ assert ctx.workflow_yaml == _SAFE_CODE_YAML
+
+ @pytest.mark.asyncio
+ async def test_nested_code_block_undeclared_parameter_key_rejects(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ submitted = _directory_blocks_yaml(
+ """
+ - block_type: loop
+ label: retry_search
+ loop_blocks:
+ - block_type: code
+ label: nested_directory_search
+ parameter_keys: [address_or_postal_code]
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "nested_directory_search" in result["error"]
+ assert "`address_or_postal_code`" in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ def test_deeply_nested_parameter_contract_returns_validation_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ block: dict[str, object] = {
+ "block_type": "code",
+ "label": "search_directory",
+ "parameter_keys": ["search_location"],
+ "code": "print(search_location)",
+ }
+ for index in range(1100):
+ block = {
+ "block_type": "loop",
+ "label": f"loop_{index}",
+ "loop_blocks": [block],
+ }
+ parsed = {
+ "workflow_definition": {
+ "parameters": [{"key": "search_location"}],
+ "blocks": [block],
+ }
+ }
+ monkeypatch.setattr(workflow_update_module, "parse_workflow_yaml", lambda _workflow_yaml: parsed)
+
+ assert (
+ workflow_update_module._code_block_parameter_contract_error("workflow_definition: {}")
+ == "Workflow YAML nesting is too deep to validate."
+ )
+
+ @pytest.mark.asyncio
+ async def test_declared_workflow_string_parameter_key_is_accepted(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ submitted = _code_yaml(
+ "await page.locator('#query').fill(str(provider_query))",
+ parameter_keys=["provider_query"],
+ workflow_param=True,
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ assert ctx.workflow_yaml == submitted
+
+ @pytest.mark.asyncio
+ async def test_new_declared_workflow_parameter_key_is_accepted_on_later_edit(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ ctx.workflow_yaml = _code_yaml(
+ "await page.locator('#query').fill(str(provider_query))",
+ parameter_keys=["provider_query"],
+ workflow_param=True,
+ )
+ submitted = _yaml(
+ """
+ title: Registry lookup
+ workflow_definition:
+ parameters:
+ - {parameter_type: workflow, workflow_parameter_type: string, key: provider_query, default_value: Sample Search}
+ - {parameter_type: workflow, workflow_parameter_type: string, key: search_location, default_value: Example City}
+ blocks:
+ - block_type: code
+ label: search_registry
+ parameter_keys: [provider_query, search_location]
+ code: |
+ await page.locator("#query").fill(str(provider_query))
+ await page.locator("#location").fill(str(search_location))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ assert ctx.workflow_yaml == submitted
+
+ @pytest.mark.asyncio
+ async def test_prior_block_output_parameter_key_is_accepted(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ submitted = _directory_blocks_yaml(
+ """
+ - block_type: code
+ label: search_registry
+ code: |
+ return {"records": []}
+ - block_type: code
+ label: summarize_registry
+ parameter_keys: [search_registry_output]
+ code: |
+ print(search_registry_output)
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ assert ctx.workflow_yaml == submitted
+
+ @pytest.mark.parametrize(
+ ("submitted", "label", "key"),
+ [
+ (
+ _directory_blocks_yaml(
+ """
+ - block_type: code
+ label: summarize_registry
+ parameter_keys: [search_registry_output]
+ code: |
+ print(search_registry_output)
+ - block_type: code
+ label: search_registry
+ code: |
+ return {"records": []}
+ """
+ ),
+ "summarize_registry",
+ "search_registry_output",
+ ),
+ (
+ _directory_blocks_yaml(
+ """
+ - block_type: loop
+ label: retry_search
+ loop_blocks:
+ - block_type: code
+ label: nested_directory_search
+ parameter_keys: [retry_search_output]
+ code: |
+ print(retry_search_output)
+ """
+ ),
+ "nested_directory_search",
+ "retry_search_output",
+ ),
+ ],
+ )
+ @pytest.mark.asyncio
+ async def test_unavailable_output_parameter_key_rejects(
+ self, monkeypatch: pytest.MonkeyPatch, submitted: str, label: str, key: str
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert label in result["error"]
+ assert f"`{key}`" in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ @pytest.mark.parametrize("marker", ["<<<<<<< HEAD", "======="])
+ @pytest.mark.asyncio
+ async def test_raw_workflow_yaml_conflict_marker_rejects_before_persist(
+ self, monkeypatch: pytest.MonkeyPatch, marker: str
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ ctx.workflow_yaml = _SAFE_CODE_YAML
+ submitted = f"{marker}\n{_SAFE_CODE_YAML}"
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert f"conflict marker `{marker}`" in result["error"]
+ assert "line 1" in result["error"]
+ assert ctx.workflow_yaml == _SAFE_CODE_YAML
+
+ @pytest.mark.asyncio
+ async def test_conflict_marker_inside_code_block_has_marker_specific_error(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: search_registry
+ code: |
+ <<<<<<< HEAD
+ await page.goto("https://example.com/search")
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "Code block `search_registry` contains unresolved conflict marker `<<<<<<< HEAD`" in result["error"]
+ assert "not valid Python" not in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ @pytest.mark.asyncio
+ async def test_registered_download_output_keys_are_not_parameter_keys(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ submitted = _yaml(
+ """
+ title: Statement download
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: download_statement
+ parameter_keys: [downloaded_files]
+ code: |
+ print(downloaded_files)
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "download_statement" in result["error"]
+ assert "`downloaded_files`" in result["error"]
+ assert "execution layer injects registered download output keys" in result["error"]
+
+
class TestCompiledAuthoringImposition:
def _provider_search_ctx(self) -> CopilotContext:
ctx = _code_only_ctx()
@@ -551,7 +836,7 @@ class TestCompiledAuthoringImposition:
"parameter_type": "workflow",
"workflow_parameter_type": "string",
"key": "provider_name",
- "default_value": "Taylor Brooks",
+ "default_value": "Sample Search",
}
]
assert result["data"]["imposed_substitutions"] == {
@@ -582,14 +867,171 @@ class TestCompiledAuthoringImposition:
{"workflow_yaml": _SUBMITTED_LITERAL_YAML, "code_artifact_metadata": metadata}, ctx
)
+ assert result["ok"] is False
+ assert "does not return a keyed structure" in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ @pytest.mark.asyncio
+ async def test_output_intent_requires_artifact_metadata_before_persist(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = self._provider_search_ctx()
+ submitted = _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {parameter_type: output, key: provider_result}
+ blocks:
+ - block_type: code
+ label: search_registry
+ prompt: Search the registry and return structured provider result data.
+ code: |
+ await page.locator("input[placeholder='Search']").fill("Sample Search")
+ await page.locator("button.lookup").click()
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "must pass `code_artifact_metadata`" in result["error"]
+ assert "goal_value_paths" in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ @pytest.mark.asyncio
+ async def test_output_intent_rejects_partial_metadata_without_output_label(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = self._provider_search_ctx()
+ submitted = _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {parameter_type: output, key: provider_result}
+ blocks:
+ - block_type: code
+ label: open_registry
+ prompt: Open the registry.
+ code: |
+ await page.goto("https://example.com/search")
+ - block_type: code
+ label: extract_registry
+ prompt: Return structured provider result data.
+ code: |
+ records = [{"number": "REC-001"}]
+ """
+ )
+
+ result = await _update_workflow(
+ {
+ "workflow_yaml": submitted,
+ "code_artifact_metadata": [_terminal_metadata("open_registry", "open the registry")],
+ },
+ ctx,
+ )
+
+ assert result["ok"] is False
+ assert "extract_registry" in result["error"]
+ assert "must pass `code_artifact_metadata`" in result["error"]
+ assert ctx.workflow_yaml == ""
+
+ @pytest.mark.asyncio
+ async def test_imposition_preserves_submitted_extraction_suffix(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = self._provider_search_ctx()
+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)
+ assert synthesized is not None
+ submitted_code = synthesized.code.rstrip() + '\nrecords = [{"number": "REC-001", "status": "credentialed"}]\n'
+ submitted = yaml.safe_dump(
+ {
+ "title": "Provider lookup",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "workflow",
+ "workflow_parameter_type": "string",
+ "key": "provider_name",
+ "default_value": "Sample Search",
+ }
+ ],
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "search_registry",
+ "code": submitted_code,
+ }
+ ],
+ },
+ },
+ sort_keys=False,
+ )
+ metadata = [_terminal_metadata("search_registry", "search the registry")]
+
+ result = await _update_workflow({"workflow_yaml": submitted, "code_artifact_metadata": metadata}, ctx)
+
assert result["ok"] is True
- assert "structured_return_skeleton" not in result["data"]["imposed_substitutions"]
+ assert result["data"]["imposed_substitutions"]["preserved_extraction_suffix"] is True
parsed = parse_workflow_yaml(ctx.workflow_yaml)
assert isinstance(parsed, dict)
block = _single_code_block(parsed)
assert "<fill" not in block["code"]
assert "<fill: captured value>" not in block["code"]
assert 'await page.locator("#provInput").fill(str(provider_name))' in block["code"]
+ assert 'records = [{"number": "REC-001", "status": "credentialed"}]' in block["code"]
+
+ @pytest.mark.asyncio
+ async def test_imposition_preserves_custom_extraction_code_with_goal_metadata(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = self._provider_search_ctx()
+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)
+ assert synthesized is not None
+ synthesized_code = textwrap.dedent(synthesized.code).lstrip()
+ submitted_code = (
+ "records = []\n"
+ + synthesized_code.rstrip()
+ + '\nrecords.append({"number": "REC-001", "status": "credentialed"})\n'
+ )
+ submitted = yaml.safe_dump(
+ {
+ "title": "Provider lookup",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "workflow",
+ "workflow_parameter_type": "string",
+ "key": "provider_name",
+ "default_value": "Sample Search",
+ }
+ ],
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "search_registry",
+ "code": submitted_code,
+ }
+ ],
+ },
+ },
+ sort_keys=False,
+ )
+ metadata = [_terminal_metadata("search_registry", "search the registry")]
+
+ result = await _update_workflow({"workflow_yaml": submitted, "code_artifact_metadata": metadata}, ctx)
+
+ assert result["ok"] is True
+ assert result["data"]["imposed_substitutions"]["preserved_submitted_extraction_code"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["code"].startswith("records = []")
+ assert 'await page.locator("#provInput").fill(str(provider_name))' in block["code"]
+ assert 'records.append({"number": "REC-001", "status": "credentialed"})' in block["code"]
def _download_ctx(self) -> CopilotContext:
from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
@@ -859,6 +1301,351 @@ class TestCompiledAuthoringImposition:
assert "Unable to bind synthesized parameter `provider_name`" in result["error"]
assert ctx.workflow_yaml == ""
+ @pytest.mark.asyncio
+ async def test_synthesized_internal_parameter_aliases_to_existing_declared_default(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#locInput",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 17,
+ "typed_value": "Example City, USA",
+ "role": "textbox",
+ "accessible_name": "Address or postal code",
+ "trajectory_index": 0,
+ }
+ ]
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ parameters:
+ - {key: search_location, default_value: "Example City, USA"}
+ - {key: address_or_postal_code, default_value: "Example City, USA"}
+ blocks:
+ - block_type: code
+ label: search_directory
+ parameter_keys: [address_or_postal_code]
+ parameters:
+ - {key: address_or_postal_code, default_value: "Example City, USA"}
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["parameter_keys"] == ["search_location"]
+ assert "parameters" not in block
+ assert "str(search_location)" in block["code"]
+ assert "address_or_postal_code" not in block["code"]
+ parameters = parsed["workflow_definition"]["parameters"]
+ assert [parameter["key"] for parameter in parameters] == ["search_location"]
+ assert result["data"]["imposed_substitutions"]["parameter_aliases"] == {
+ "address_or_postal_code": "search_location"
+ }
+
+ @pytest.mark.asyncio
+ async def test_multi_input_synthesized_parameter_aliases_before_ambiguity_guard(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#locInput",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 17,
+ "typed_value": "Example City, USA",
+ "role": "textbox",
+ "accessible_name": "Address or postal code",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "type_text",
+ "selector": "#firstName",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 5,
+ "typed_value": "Given",
+ "role": "textbox",
+ "accessible_name": "Provider First Name",
+ "trajectory_index": 1,
+ },
+ ]
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ parameters:
+ - {key: search_location, default_value: "Example City, USA"}
+ - {key: provider_first_name, default_value: "Given"}
+ blocks:
+ - block_type: code
+ label: search_directory
+ parameter_keys: [address_or_postal_code, provider_first_name]
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ await page.locator("#firstName").fill(str(provider_first_name))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["parameter_keys"] == ["search_location", "provider_first_name"]
+ assert "str(search_location)" in block["code"]
+ assert "str(provider_first_name)" in block["code"]
+ assert "address_or_postal_code" not in block["code"]
+ assert result["data"]["imposed_substitutions"]["parameter_aliases"] == {
+ "address_or_postal_code": "search_location"
+ }
+
+ @pytest.mark.asyncio
+ async def test_synthesized_parameter_aliases_by_typed_length_to_declared_default(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#locInput",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 17,
+ "role": "textbox",
+ "accessible_name": "Address or postal code",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "type_text",
+ "selector": "#firstName",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 5,
+ "role": "textbox",
+ "accessible_name": "Provider First Name",
+ "trajectory_index": 1,
+ },
+ ]
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ parameters:
+ - {key: search_location, default_value: "Example City, USA"}
+ - {key: provider_first_name, default_value: "Given"}
+ blocks:
+ - block_type: code
+ label: search_directory
+ parameter_keys: [address_or_postal_code, provider_first_name]
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ await page.locator("#firstName").fill(str(provider_first_name))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["parameter_keys"] == ["search_location", "provider_first_name"]
+ assert "str(search_location)" in block["code"]
+ assert "address_or_postal_code" not in block["code"]
+ assert result["data"]["imposed_substitutions"]["parameter_aliases"] == {
+ "address_or_postal_code": "search_location"
+ }
+
+ @pytest.mark.asyncio
+ async def test_synthesized_provider_search_key_rewrites_to_declared_first_last_inputs(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#locInput",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 17,
+ "role": "textbox",
+ "accessible_name": "Address or postal code",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "type_text",
+ "selector": "#providerSearch",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 12,
+ "role": "textbox",
+ "accessible_name": "Provider name or identifier",
+ "trajectory_index": 1,
+ },
+ ]
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ parameters:
+ - {key: search_location, default_value: "Example City, USA"}
+ - {key: provider_first_name, default_value: "Given"}
+ - {key: provider_last_name, default_value: "Family"}
+ blocks:
+ - block_type: code
+ label: search_directory
+ parameter_keys:
+ - address_or_postal_code
+ - provider_name_or_identifier
+ code: |
+ await page.locator("#location").fill(str(address_or_postal_code))
+ await page.locator("#providerSearch").fill(str(provider_name_or_identifier))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["parameter_keys"] == ["search_location", "provider_first_name", "provider_last_name"]
+ assert "str(search_location)" in block["code"]
+ assert '(str(provider_first_name) + " " + str(provider_last_name))' in block["code"]
+ assert "address_or_postal_code" not in block["code"]
+ assert "provider_name_or_identifier" not in block["code"]
+ assert result["data"]["imposed_substitutions"]["parameter_aliases"] == {
+ "address_or_postal_code": "search_location"
+ }
+ assert result["data"]["imposed_substitutions"]["parameter_expressions"] == {
+ "provider_name_or_identifier": ('(str(provider_first_name) + " " + str(provider_last_name))')
+ }
+
+ def test_identifier_rewrite_skips_string_literals_and_comments(self) -> None:
+ source = (
+ 'await page.locator("#providerSearch").fill(str(provider_query))\n'
+ "# provider_query should stay readable in comments\n"
+ 'message = "provider_query should stay readable in strings"\n'
+ )
+
+ rewritten = workflow_update_module._replace_python_identifier(
+ source,
+ "provider_query",
+ '(str(provider_first_name) + " " + str(provider_last_name))',
+ )
+
+ ast.parse(rewritten)
+ assert 'str((str(provider_first_name) + " " + str(provider_last_name)))' in rewritten
+ assert "# provider_query should stay readable in comments" in rewritten
+ assert '"provider_query should stay readable in strings"' in rewritten
+
+ def test_identifier_rewrite_preserves_multiline_block_shape(self) -> None:
+ source = (
+ "if provider_query:\n"
+ ' await page.locator("#providerSearch").fill(str(provider_query))\n'
+ "else:\n"
+ ' await page.locator("#providerSearch").fill("")\n'
+ )
+
+ rewritten = workflow_update_module._replace_python_identifier(source, "provider_query", "provider_name")
+
+ assert rewritten == (
+ "if provider_name:\n"
+ ' await page.locator("#providerSearch").fill(str(provider_name))\n'
+ "else:\n"
+ ' await page.locator("#providerSearch").fill("")\n'
+ )
+
+ def test_ambiguous_combined_default_match_logs_and_returns_none(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ debug_events: list[tuple[str, dict[str, object]]] = []
+ monkeypatch.setattr(
+ workflow_update_module,
+ "LOG",
+ SimpleNamespace(debug=lambda event, **kwargs: debug_events.append((event, kwargs))),
+ )
+
+ result = workflow_update_module._combined_string_default_expression(
+ [
+ {"key": "first_name", "default_value": "Given"},
+ {"key": "last_name", "default_value": "Family"},
+ {"key": "given_name", "default_value": "Given"},
+ {"key": "family_name", "default_value": "Family"},
+ ],
+ synthesized_default="Given Family",
+ typed_length=None,
+ )
+
+ assert result is None
+ assert debug_events == [
+ (
+ "copilot_synthesized_parameter_combined_default_ambiguous",
+ {"match_count": 3, "synthesized_default_present": True, "typed_length": None},
+ )
+ ]
+
+ @pytest.mark.asyncio
+ async def test_synthesized_provider_search_key_rewrites_to_short_first_last_inputs(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#providerSearch",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 12,
+ "role": "textbox",
+ "accessible_name": "Provider name or identifier",
+ "trajectory_index": 0,
+ }
+ ]
+ submitted = _yaml(
+ """
+ title: Directory lookup
+ workflow_definition:
+ parameters:
+ - {key: first_name, default_value: "Given"}
+ - {key: last_name, default_value: "Family"}
+ blocks:
+ - block_type: code
+ label: search_directory
+ parameter_keys:
+ - provider_name_or_identifier
+ code: |
+ await page.locator("#providerSearch").fill(str(provider_name_or_identifier))
+ """
+ )
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ parsed = parse_workflow_yaml(ctx.workflow_yaml)
+ assert isinstance(parsed, dict)
+ block = _single_code_block(parsed)
+ assert block["parameter_keys"] == ["first_name", "last_name"]
+ assert '(str(first_name) + " " + str(last_name))' in block["code"]
+ assert "provider_name_or_identifier" not in block["code"]
+ assert result["data"]["imposed_substitutions"]["parameter_expressions"] == {
+ "provider_name_or_identifier": ('(str(first_name) + " " + str(last_name))')
+ }
+
@pytest.mark.asyncio
async def test_single_local_string_constant_is_lifted_for_synthesized_key(
self, monkeypatch: pytest.MonkeyPatch
@@ -879,7 +1666,7 @@ class TestCompiledAuthoringImposition:
"parameter_type": "workflow",
"workflow_parameter_type": "string",
"key": "provider_name",
- "default_value": "Taylor Brooks",
+ "default_value": "Sample Search",
}
]
@@ -905,7 +1692,7 @@ class TestCompiledAuthoringImposition:
"parameter_type": "workflow",
"workflow_parameter_type": "string",
"key": synthesized_key,
- "default_value": "Taylor Brooks",
+ "default_value": "Sample Search",
}
]
assert result["data"]["imposed_substitutions"]["parameter_keys"] == [synthesized_key]
@@ -1057,6 +1844,7 @@ class TestSeamSalvageIntoContext:
label: block_one
code: |
await page.goto("https://example.com/search")
+ records = [{"number": "REC-001"}]
- block_type: code
label: block_two
code: |
@@ -1140,7 +1928,9 @@ class TestSeamSalvageIntoContext:
async def test_minimal_metadata_with_trajectory_produces_no_violation_error(self) -> None:
ctx = _code_only_ctx()
metadata = [_terminal_metadata("search_registry", "search the registry")]
- result = await _update_workflow({"workflow_yaml": _SAFE_CODE_YAML, "code_artifact_metadata": metadata}, ctx)
+ result = await _update_workflow(
+ {"workflow_yaml": _SAFE_EXTRACTION_CODE_YAML, "code_artifact_metadata": metadata}, ctx
+ )
# The seam may reject later (credential checks need the app); the metadata
# contract itself must not be the rejection.
error_text = str(result.get("error") or "")
@@ -1157,10 +1947,12 @@ class TestStaleLabelSeamFlow:
# neither the metadata gate nor the stale-block-metadata validation
# path can bounce the submission back to the model.
ctx = _code_only_ctx()
- ctx.workflow_yaml = _SAFE_CODE_YAML
+ ctx.workflow_yaml = _SAFE_EXTRACTION_CODE_YAML
metadata = [_terminal_metadata("search_certificant_stale", "search the registry")]
- result = await _update_workflow({"workflow_yaml": _SAFE_CODE_YAML, "code_artifact_metadata": metadata}, ctx)
+ result = await _update_workflow(
+ {"workflow_yaml": _SAFE_EXTRACTION_CODE_YAML, "code_artifact_metadata": metadata}, ctx
+ )
error_text = str(result.get("error") or "")
assert "Artifact metadata" not in error_text
@@ -1169,7 +1961,7 @@ class TestStaleLabelSeamFlow:
assert ctx.code_artifact_metadata["search_registry"]["artifact_id"] == "code_artifact:search_registry"
# The seam never rewrites YAML labels, so its output cannot trip the
# stale-block-metadata validation that fires on label/title renames.
- assert _detect_stale_block_metadata(_SAFE_CODE_YAML, ctx.workflow_yaml) == []
+ assert _detect_stale_block_metadata(_SAFE_EXTRACTION_CODE_YAML, ctx.workflow_yaml) == []
@pytest.mark.asyncio
async def test_malformed_per_entry_refs_normalize_without_scout_interactions(self) -> None:
@@ -1184,7 +1976,9 @@ class TestStaleLabelSeamFlow:
}
]
- result = await _update_workflow({"workflow_yaml": _SAFE_CODE_YAML, "code_artifact_metadata": metadata}, ctx)
+ result = await _update_workflow(
+ {"workflow_yaml": _SAFE_EXTRACTION_CODE_YAML, "code_artifact_metadata": metadata}, ctx
+ )
error_text = str(result.get("error") or "")
assert "Artifact metadata" not in error_text
diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.py
index 49266b731..756a49038 100644
--- a/tests/unit/test_copilot_code_block_synthesis.py
+++ b/tests/unit/test_copilot_code_block_synthesis.py
@@ -20,6 +20,7 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
_SYNTHESIZED_BLOCK_LABEL,
CREDENTIAL_FILL_TOOL_NAME,
build_synthesized_artifact_metadata,
+ is_optional_dismissal_only_trajectory,
render_synthesized_offer_text,
synthesize_code_block,
)
@@ -268,6 +269,25 @@ class TestActionSynthesis:
# Raw typed value is never captured.
assert "value" not in result.code
+ def test_strict_type_text_carries_typed_length_without_value(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/",
+ typed_length=19,
+ role="textbox",
+ accessible_name="Address or postal code",
+ )
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ assert result.parameters == [{"key": "address_or_postal_code", "typed_length": "19"}]
+ assert "Example City" not in result.code
+
def test_type_text_defaults_are_private_reused_only_for_same_field_identity(self) -> None:
def typed(selector: str, name: str, url: str = "https://example.com/") -> dict[str, Any]:
return _interaction(
@@ -315,6 +335,347 @@ class TestActionSynthesis:
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")'
+ assert " del _scout_entry_target" in lines
+
+ def test_optional_cookie_dismissal_is_conditional_and_uses_durable_entry_target(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector="#accept-consent",
+ source_url="https://example.com/find",
+ role="button",
+ accessible_name="Accept cookies",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ]
+ )
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert ' await page.goto("https://example.com/find", wait_until="domcontentloaded")' in lines
+ assert ' await _scout_entry_target.wait_for(state="visible")' in lines
+ assert ' _scout_optional_dismissal = page.locator("#accept-consent")' in lines
+ assert " if await _scout_optional_dismissal.count() > 0:" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert result.code.index("_scout_optional_dismissal") < result.code.index(
+ 'await page.locator("#locInput").fill'
+ )
+ assert " del _scout_entry_target" in lines
+ assert " del _scout_optional_dismissal" in lines
+ assert result.parameters == [{"key": "city_county_or_zip_code", "default_value": "Example City"}]
+ ast.parse("async def _block(page):\n" + result.code)
+
+ def test_optional_cookie_decline_is_conditional_and_not_entry_target(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector="button.decline",
+ source_url="https://example.com/find",
+ role="button",
+ accessible_name="Decline cookies",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert ' _scout_optional_dismissal = page.locator("button.decline")' in lines
+ assert " if await _scout_optional_dismissal.count() > 0:" in lines
+ assert 'await _scout_entry_target.wait_for(state="visible")' in result.code
+ assert 'await page.locator("button.decline").click()' not in result.code
+
+ def test_close_named_action_is_not_optional_dismissal_by_name_only(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector="#account-action",
+ source_url="https://example.com/settings",
+ role="button",
+ accessible_name="Close account",
+ )
+ ]
+
+ assert is_optional_dismissal_only_trajectory(trajectory) is False
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ assert 'await page.locator("#account-action").click()' in result.code
+ assert "_scout_optional_dismissal" not in result.code
+
+ def test_internal_scout_cleanup_ignores_names_inside_literals(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector="#start",
+ source_url="https://example.com/_scout_optional_dismissal",
+ )
+ ]
+ )
+
+ assert result is not None
+ assert 'await page.goto("https://example.com/_scout_optional_dismissal"' in result.code
+ assert " del _scout_entry_target" in result.code
+ assert " del _scout_optional_dismissal" not in result.code
+
+ def test_structural_cookie_button_is_conditional_when_durable_target_follows(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector=".btns button:nth-of-type(2)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ _interaction(
+ "type_text",
+ selector="#npiInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="Provider ID",
+ typed_value="ID-12345",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#npiInput")'
+ assert ' _scout_optional_dismissal = page.locator(".btns button:nth-of-type(2)")' in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert 'await page.locator(".btns button:nth-of-type(2)").click()' not in result.code
+ assert result.code.index("_scout_optional_dismissal") < result.code.index(
+ 'await page.locator("#npiInput").fill'
+ )
+
+ def test_not_decline_cookie_button_is_conditional_when_durable_target_follows(self) -> None:
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector="button:not(.decline):nth-of-type(6)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert 'await page.locator("button:not(.decline):nth-of-type(6)").click()' not in result.code
+
+ def test_cookie_accept_xpath_is_conditional_when_durable_target_follows(self) -> None:
+ cookie_accept_xpath = (
+ 'xpath=/*[name()="html"][1]/*[name()="body"][1]/*[name()="div"][1]'
+ '/*[name()="div"][2]/*[name()="div"][1]/*[name()="button"][2]'
+ )
+
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector=cookie_accept_xpath,
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert cookie_accept_xpath not in result.code
+ assert result.code.index("_scout_optional_dismissal") < result.code.index(
+ 'await page.locator("#locInput").fill'
+ )
+
+ def test_normalized_accept_xpath_is_conditional_when_durable_target_follows(self) -> None:
+ accept_xpath = "xpath=//button[normalize-space()='Accept']"
+
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector=accept_xpath,
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert accept_xpath not in result.code
+
+ def test_bare_normalized_accept_xpath_is_conditional_when_durable_target_follows(self) -> None:
+ accept_xpath = "//button[normalize-space()='Accept']"
+
+ result = synthesize_code_block(
+ [
+ _interaction(
+ "click",
+ selector=accept_xpath,
+ source_url="https://example.com/find",
+ role=None,
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ],
+ strict_selectors=True,
+ )
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' _scout_entry_target = page.locator("#locInput")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert accept_xpath not in result.code
+
+ def test_one_step_not_decline_cookie_button_is_not_entry_target(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector="button:not(.decline)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ ]
+ assert is_optional_dismissal_only_trajectory(trajectory) is True
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' await page.goto("https://example.com/find", wait_until="domcontentloaded")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert 'await _scout_entry_target.wait_for(state="visible")' not in result.code
+ assert 'page.locator("button:not(.decline)")' not in result.code
+ ast.parse("async def _block(page):\n" + result.code)
+
+ def test_one_step_structural_cookie_button_is_not_entry_target(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector=".btns button:nth-of-type(2)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ ]
+ assert is_optional_dismissal_only_trajectory(trajectory) is True
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' await page.goto("https://example.com/find", wait_until="domcontentloaded")'
+ assert ' _scout_optional_dismissal = page.locator(".btns button:nth-of-type(2)")' in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert 'await _scout_entry_target.wait_for(state="visible")' not in result.code
+ assert 'await page.locator(".btns button:nth-of-type(2)").click()' not in result.code
+
+ def test_one_step_bare_accept_xpath_is_not_entry_target(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector="//button[normalize-space()='Accept']",
+ source_url="https://example.com/find",
+ role=None,
+ ),
+ ]
+ assert is_optional_dismissal_only_trajectory(trajectory) is True
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ lines = result.code.splitlines()
+ assert lines[0] == ' await page.goto("https://example.com/find", wait_until="domcontentloaded")'
+ assert " _scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in lines
+ assert " await _scout_optional_dismissal.first.click(timeout=1000)" in lines
+ assert 'await _scout_entry_target.wait_for(state="visible")' not in result.code
+ assert "//button[normalize-space()='Accept']" not in result.code
+
+ def test_optional_dismissal_with_durable_target_is_offerable(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector="button:not(.decline)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ ]
+
+ assert is_optional_dismissal_only_trajectory(trajectory) is False
def test_press_enter_uses_keyboard_when_no_selector(self) -> None:
result = synthesize_code_block([_interaction("press_key", key="Enter")])