Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-10810 impose scout trajectory at persist seam (#6887)
Andrew Neilson739dd15abda1
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.pyindex b38b7487f..0ae7de371 100644--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py@@ -1059,6 +1059,7 @@ def build_artifact_metadata_skeleton("covered_criteria": [_FILL_CRITERION_ID],"goal_value_paths": ["<fill: output JSON path(s) carrying requested goal values>"],"extraction_schema": _FILL_EXTRACTION_SCHEMA,+ "extraction_schema_provenance": "self_authored","observation_refs": [observation_ref_id],}],diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.pyindex 9c57479c2..b0acb673f 100644--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py@@ -1,6 +1,7 @@from __future__ import annotationsimport ast+import copyimport ioimport jsonimport keyword@@ -144,6 +145,13 @@ class BlockObservationRef(BaseModel):ArtifactEvidenceStatus = Literal["satisfied", "missing", "diagnostic_only", "observed_not_verified"]+ExtractionSchemaProvenance = Literal["self_authored", "user_edited"]+SelectedExtractionMetadataDisposition = Literal[+ "none",+ "browser_spine_replaced_metadata_stale",+ "self_authored_extraction_preserved",+ "sibling_or_suffix_extraction_preserved",+]class CodeArtifactClaimedOutcome(BaseModel):@@ -170,6 +178,7 @@ class CodeArtifactClaimedOutcome(BaseModel):"and validated against it. Same dialect as the legacy `data_schema` lever."),)+ extraction_schema_provenance: ExtractionSchemaProvenance = "self_authored"evidence_refs: list[str] = Field(default_factory=list)observation_refs: list[str] = Field(default_factory=list)required_tokens: list[str] = Field(default_factory=list)@@ -226,6 +235,7 @@ class CodeArtifactTerminalVerifierExpectation(BaseModel):default=None,description="JSON Schema (serialized JSON string) of the confirmed extraction shape terminal verification expects.",)+ extraction_schema_provenance: ExtractionSchemaProvenance = "self_authored"class CodeArtifactExplorationObservation(BaseModel):@@ -419,6 +429,7 @@ def _normalize_code_artifact_metadata_detailed(seen_labels: set[str] = set()for raw_item in items:try:+ raw_item = _default_missing_extraction_schema_provenance(raw_item)metadata = (raw_itemif isinstance(raw_item, CodeArtifactMetadata)@@ -775,6 +786,77 @@ def _code_artifact_metadata_items(raw_metadata: Any) -> list[Any]:return [raw_metadata]+def _default_missing_extraction_schema_provenance(raw_item: Any) -> Any:+ if isinstance(raw_item, CodeArtifactMetadata) or not isinstance(raw_item, Mapping):+ return raw_item+ item = copy.deepcopy(raw_item)+ for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):+ rows = item.get(field_name)+ if not isinstance(rows, list):+ continue+ for row in rows:+ if not isinstance(row, dict):+ continue+ schema = row.get("extraction_schema")+ if schema is None or (isinstance(schema, str) and not schema.strip()):+ continue+ row.setdefault("extraction_schema_provenance", "user_edited")+ return item+++def _downgrade_stale_selected_metadata_item(item: dict[str, Any], selected_label: str) -> None:+ if str(item.get("block_label") or "").strip() != selected_label:+ return+ for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):+ for row in _artifact_mutable_rows(item.get(field_name)):+ row.pop("goal_value_paths", None)+ if field_name == "terminal_verifier_expectations":+ row.pop("criteria_ids", None)+ row.pop("claimed_outcome_ids", None)+ criteria = _artifact_mutable_rows(item.get("completion_criteria"))+ if not criteria:+ item["completion_criteria"] = [+ {+ "id": f"criterion:{_artifact_label_fragment(selected_label)}_diagnostic",+ "text": str(item.get("declared_goal") or selected_label).strip() or selected_label,+ "level": "outcome",+ "terminal": False,+ }+ ]+ criteria = _artifact_mutable_rows(item.get("completion_criteria"))+ for criterion in criteria:+ criterion["level"] = "outcome"+ criterion["terminal"] = False+ criterion_ids = [str(criterion.get("id") or "").strip() for criterion in criteria]+ criterion_ids = [criterion_id for criterion_id in criterion_ids if criterion_id]+ for row in _artifact_mutable_rows(item.get("claimed_outcomes")):+ row.pop("criteria_ids", None)+ if criterion_ids:+ row["covered_criteria"] = list(criterion_ids)+ for row in _artifact_mutable_rows(item.get("terminal_verifier_expectations")):+ if criterion_ids:+ row["criteria_ids"] = list(criterion_ids)+++def _downgrade_stale_selected_goal_value_paths(raw_metadata: Any, selected_label: str) -> Any:+ if raw_metadata in (None, [], {}) or not selected_label:+ return raw_metadata+ scrubbed = copy.deepcopy(raw_metadata)+ if isinstance(scrubbed, list):+ for item in scrubbed:+ if isinstance(item, dict):+ _downgrade_stale_selected_metadata_item(item, selected_label)+ elif isinstance(scrubbed, dict):+ if "block_label" in scrubbed:+ _downgrade_stale_selected_metadata_item(scrubbed, selected_label)+ else:+ for block_label, value in scrubbed.items():+ if block_label == selected_label and isinstance(value, dict):+ value.setdefault("block_label", selected_label)+ _downgrade_stale_selected_metadata_item(value, selected_label)+ return scrubbed++def _workflow_yaml_code_blocks_by_label(workflow_yaml: str | None) -> dict[str, Mapping[str, Any]]:if workflow_yaml is None:return {}@@ -1285,6 +1367,8 @@ class _SynthesizedCodeImpositionResult:substitutions: dict[str, Any] | None = Noneviolations: list[str] = dataclass_field(default_factory=list)repair_context: CodeAuthoringRepairContext | None = None+ scrubbed_selected_metadata_label: str | None = None+ selected_extraction_metadata_disposition: SelectedExtractionMetadataDisposition = "none"_SUBMITTED_LITERAL_METHODS = frozenset({"fill", "type"})@@ -1558,6 +1642,333 @@ def _locator_provenance_is_self_validating(provenance: Mapping[str, Any]) -> booreturn False+_PAGE_MUTATION_METHODS = frozenset(+ {+ "goto",+ "reload",+ "go_back",+ "go_forward",+ "set_content",+ }+)+_LOCATOR_MUTATION_METHODS = frozenset(+ {+ "check",+ "click",+ "dblclick",+ "dispatch_event",+ "drag_to",+ "fill",+ "focus",+ "hover",+ "press",+ "select_option",+ "set_checked",+ "tap",+ "type",+ "uncheck",+ }+)+_PAGE_FACTORY_METHODS = frozenset({"frame_locator", "get_by_role", "locator"})+_PAGE_READ_METHODS = frozenset(+ {+ "get_attribute",+ "inner_html",+ "inner_text",+ "input_value",+ "is_checked",+ "is_disabled",+ "is_editable",+ "is_enabled",+ "is_hidden",+ "is_visible",+ "text_content",+ "wait_for_load_state",+ }+)+_LOCATOR_READ_METHODS = frozenset(+ {+ "all_inner_texts",+ "all_text_contents",+ "count",+ "get_attribute",+ "inner_html",+ "inner_text",+ "input_value",+ "is_checked",+ "is_disabled",+ "is_editable",+ "is_enabled",+ "is_hidden",+ "is_visible",+ "text_content",+ "wait_for",+ }+)+++class _BrowserSurfaceValidation(NamedTuple):+ violations: list[str]+++class _BrowserMutationSignature(NamedTuple):+ method: str+ receiver: str+ call_shape: str+++class _BrowserBindings(NamedTuple):+ page_aliases: set[str]+ locator_aliases: set[str]+ method_aliases: set[str]+++def _code_block_label(block: Mapping[str, Any]) -> str:+ return str(block.get("label") or "").strip() or "unlabeled code block"+++def _call_name(node: ast.AST) -> str:+ try:+ return ast.unparse(node)+ except Exception:+ return node.__class__.__name__+++def _locator_receiver_for_signature(node: ast.AST) -> ast.AST:+ while isinstance(node, ast.Attribute) and node.attr in {"first", "last"}:+ node = node.value+ return node+++def _direct_locator_receiver_signature(node: ast.AST) -> str | None:+ receiver = _locator_receiver_for_signature(node)+ if _is_page_locator_expression(receiver):+ return _call_name(receiver)+ return None+++def _direct_page_method_signature(node: ast.AST) -> str | None:+ if not isinstance(node, ast.Name) or node.id != "page":+ return None+ return "page"+++def _browser_mutation_signature_for_call(node: ast.Call) -> _BrowserMutationSignature | None:+ func = node.func+ if not isinstance(func, ast.Attribute):+ return None+ if func.attr in _LOCATOR_MUTATION_METHODS:+ receiver = _direct_locator_receiver_signature(func.value)+ if receiver is not None:+ return _BrowserMutationSignature(func.attr, receiver, ast.dump(node, include_attributes=False))+ return None+ if func.attr in _PAGE_MUTATION_METHODS:+ receiver = _direct_page_method_signature(func.value)+ if receiver is not None:+ return _BrowserMutationSignature(func.attr, receiver, ast.dump(node, include_attributes=False))+ return None+++def _assigned_value_is_page(value: ast.AST, page_aliases: set[str]) -> bool:+ return isinstance(value, ast.Name) and (value.id == "page" or value.id in page_aliases)+++def _collect_page_aliases(tree: ast.AST) -> set[str]:+ aliases: set[str] = set()+ changed = True+ while changed:+ changed = False+ for node in ast.walk(tree):+ if isinstance(node, ast.Assign) and _assigned_value_is_page(node.value, aliases):+ for target in node.targets:+ before = len(aliases)+ aliases.update(_target_names(target))+ changed = changed or len(aliases) != before+ elif (+ isinstance(node, ast.AnnAssign)+ and node.value is not None+ and _assigned_value_is_page(node.value, aliases)+ ):+ before = len(aliases)+ aliases.update(_target_names(node.target))+ changed = changed or len(aliases) != before+ elif isinstance(node, ast.NamedExpr) and _assigned_value_is_page(node.value, aliases):+ before = len(aliases)+ aliases.update(_target_names(node.target))+ changed = changed or len(aliases) != before+ return aliases+++def _assigned_value_is_locator(value: ast.AST, locator_aliases: set[str]) -> bool:+ return _direct_locator_receiver_signature(value) is not None or (+ isinstance(value, ast.Name) and value.id in locator_aliases+ )+++def _collect_locator_aliases(tree: ast.AST) -> set[str]:+ aliases: set[str] = set()+ changed = True+ while changed:+ changed = False+ for node in ast.walk(tree):+ if isinstance(node, ast.Assign) and _assigned_value_is_locator(node.value, aliases):+ for target in node.targets:+ before = len(aliases)+ aliases.update(_target_names(target))+ changed = changed or len(aliases) != before+ elif (+ isinstance(node, ast.AnnAssign)+ and node.value is not None+ and _assigned_value_is_locator(node.value, aliases)+ ):+ before = len(aliases)+ aliases.update(_target_names(node.target))+ changed = changed or len(aliases) != before+ elif isinstance(node, ast.NamedExpr) and _assigned_value_is_locator(node.value, aliases):+ before = len(aliases)+ aliases.update(_target_names(node.target))+ changed = changed or len(aliases) != before+ return aliases+++def _expr_contains_browser_receiver(node: ast.AST, bindings: _BrowserBindings) -> bool:+ if isinstance(node, ast.Name):+ return node.id == "page" or node.id in bindings.page_aliases or node.id in bindings.locator_aliases+ if _direct_locator_receiver_signature(node) is not None:+ return True+ if isinstance(node, ast.Attribute):+ return _expr_contains_browser_receiver(node.value, bindings)+ return any(_expr_contains_browser_receiver(child, bindings) for child in ast.iter_child_nodes(node))+++def _collect_browser_method_aliases(tree: ast.AST, page_aliases: set[str], locator_aliases: set[str]) -> set[str]:+ aliases: set[str] = set()+ bindings = _BrowserBindings(page_aliases, locator_aliases, set())+ changed = True+ while changed:+ changed = False+ for node in ast.walk(tree):+ value: ast.AST | None = None+ targets: list[ast.expr] = []+ if isinstance(node, ast.Assign):+ value = node.value+ targets.extend(node.targets)+ elif isinstance(node, ast.AnnAssign):+ value = node.value+ targets.append(node.target)+ elif isinstance(node, ast.NamedExpr):+ value = node.value+ targets.append(node.target)+ if (isinstance(value, ast.Attribute) and _expr_contains_browser_receiver(value.value, bindings)) or (+ isinstance(value, ast.Name) and value.id in aliases+ ):+ for target in targets:+ before = len(aliases)+ aliases.update(_target_names(target))+ changed = changed or len(aliases) != before+ return aliases+++def _is_dynamic_browser_dispatch(node: ast.Call, bindings: _BrowserBindings) -> bool:+ func = node.func+ if not isinstance(func, ast.Call):+ return False+ if not isinstance(func.func, ast.Name) or func.func.id != "getattr" or not func.args:+ return False+ return _expr_contains_browser_receiver(func.args[0], bindings)+++def _is_allowed_browser_read_call(node: ast.Call, bindings: _BrowserBindings) -> bool:+ func = node.func+ if not isinstance(func, ast.Attribute):+ return False+ if func.attr in _PAGE_FACTORY_METHODS:+ return isinstance(func.value, ast.Name) and func.value.id == "page"+ if func.attr in _PAGE_READ_METHODS and isinstance(func.value, ast.Name) and func.value.id == "page":+ return True+ if func.attr in _LOCATOR_READ_METHODS:+ return _direct_locator_receiver_signature(func.value) is not None or (+ isinstance(func.value, ast.Name) and func.value.id in bindings.locator_aliases+ )+ return False+++def _browser_surface_for_code(code: str) -> tuple[list[_BrowserMutationSignature], list[str], list[str]]:+ tree = _wrapped_code_ast(code)+ if tree is None:+ return [], [], []+ direct_mutations: list[_BrowserMutationSignature] = []+ unscouted: list[str] = []+ ambiguous: list[str] = []+ page_aliases = _collect_page_aliases(tree)+ locator_aliases = _collect_locator_aliases(tree)+ method_aliases = _collect_browser_method_aliases(tree, page_aliases, locator_aliases)+ bindings = _BrowserBindings(page_aliases, locator_aliases, method_aliases)+ for node in ast.walk(tree):+ if not isinstance(node, ast.Call):+ continue+ if isinstance(node.func, ast.Name) and node.func.id in bindings.method_aliases:+ ambiguous.append(_call_name(node))+ continue+ signature = _browser_mutation_signature_for_call(node)+ if signature is not None:+ direct_mutations.append(signature)+ continue+ if _is_dynamic_browser_dispatch(node, bindings):+ ambiguous.append(_call_name(node))+ continue+ if _is_allowed_browser_read_call(node, bindings):+ continue+ func = node.func+ if isinstance(func, ast.Attribute):+ if _expr_contains_browser_receiver(func.value, bindings):+ ambiguous.append(_call_name(node))+ elif isinstance(func, ast.Name) and (+ any(_expr_contains_browser_receiver(arg, bindings) for arg in node.args)+ or any(_expr_contains_browser_receiver(keyword.value, bindings) for keyword in node.keywords)+ ):+ ambiguous.append(_call_name(node))+ for signature in direct_mutations:+ unscouted.append(f"{signature.receiver}.{signature.method}")+ return direct_mutations, sorted(unscouted), sorted(set(ambiguous))+++def _whole_trajectory_browser_surface_violations(+ *,+ code_blocks: list[dict[str, Any]],+ selected_code_block: dict[str, Any],+ submitted_selected_code: str,+ synthesized_code: str,+) -> _BrowserSurfaceValidation:+ violations: list[str] = []+ for block in code_blocks:+ if block is selected_code_block:+ continue+ label = _code_block_label(block)+ block_code = str(block.get("code") or "")+ block_mutations, _, block_ambiguous = _browser_surface_for_code(block_code)+ if block_mutations:+ action_text = ", ".join(f"{mutation.receiver}.{mutation.method}" for mutation in sorted(block_mutations))+ violations.append(+ f"Unable to impose synthesized code block: `{label}` contains unscouted browser action(s): {action_text}."+ )+ if block_ambiguous:+ violations.append(+ f"Unable to impose synthesized code block: `{label}` contains ambiguous browser action(s): "+ + ", ".join(block_ambiguous)+ + "."+ )+ return _BrowserSurfaceValidation(violations)+++def _browser_surface_contains_full_action_spine(submitted_code: str, synthesized_code: str) -> bool:+ synthesized_mutations, _, _ = _browser_surface_for_code(synthesized_code)+ submitted_mutations, _, submitted_ambiguous = _browser_surface_for_code(submitted_code)+ if submitted_ambiguous:+ return False+ return bool(synthesized_mutations) and submitted_mutations == synthesized_mutations++_IDENTITY_QUALIFIER_BOUNDARY = ("[", "#", ".")_FILTERING_PSEUDO_CLASSES = (":visible",@@ -2418,6 +2829,15 @@ class _SynthesizedParameterReconciliation(NamedTuple):repair_context: CodeAuthoringRepairContext | None = None+def _apply_parameter_reconciliation_to_code(code: str, reconciliation: _SynthesizedParameterReconciliation) -> str:+ reconciled = code+ for old_key, new_key in reconciliation.aliases.items():+ reconciled = _replace_python_identifier(reconciled, old_key, new_key)+ for old_key, expression in reconciliation.expressions.items():+ reconciled = _replace_python_identifier(reconciled, old_key, expression)+ return reconciled++_SYNTHESIZED_PARAMETER_BINDING_AMBIGUOUS_REASON_CODE = "synthesized_parameter_binding_ambiguous"@@ -2738,6 +3158,29 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)if not _locator_provenance_is_self_validating(provenance):violations.append("Unable to impose synthesized code block: locator provenance was not byte-equal.")break+ surface_validation = _whole_trajectory_browser_surface_violations(+ code_blocks=code_blocks,+ selected_code_block=code_block,+ submitted_selected_code=submitted_code,+ synthesized_code=synthesized.code,+ )+ violations.extend(surface_validation.violations)+ extraction_suffix = _submitted_suffix_after_synthesized_code(submitted_code, synthesized.code)+ if extraction_suffix:+ suffix_mutations, _, suffix_ambiguous = _browser_surface_for_code(extraction_suffix)+ if suffix_mutations:+ action_text = ", ".join(f"{mutation.receiver}.{mutation.method}" for mutation in sorted(suffix_mutations))+ violations.append(+ "Unable to impose synthesized code block: extraction suffix contains unscouted browser action(s): "+ + action_text+ + "."+ )+ if suffix_ambiguous:+ violations.append(+ "Unable to impose synthesized code block: extraction suffix contains ambiguous browser action(s): "+ + ", ".join(suffix_ambiguous)+ + "."+ )parameter_reconciliation = _reconcile_synthesized_parameters(parsed=parsed,@@ -2756,21 +3199,60 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)repair_context=repair_context,)- extraction_suffix = _submitted_suffix_after_synthesized_code(submitted_code, synthesized.code)raw_metadata = getattr(ctx, "raw_code_artifact_metadata", None)+ metadata_declares_goal_values = bool(raw_metadata) and _raw_metadata_declares_goal_values_for_block(+ raw_metadata, str(code_block.get("label") or "")+ )+ reconciled_submitted_code = _apply_parameter_reconciliation_to_code(+ textwrap.dedent(submitted_code), parameter_reconciliation+ )+ reconciled_synthesized_code = _apply_parameter_reconciliation_to_code(+ textwrap.dedent(synthesized.code), parameter_reconciliation+ )+ submitted_contains_full_spine = _browser_surface_contains_full_action_spine(+ reconciled_submitted_code, reconciled_synthesized_code+ )+ assigned_submitted_parameter_keys = _assigned_submitted_parameter_keys(+ reconciled_submitted_code,+ parameter_reconciliation.parameter_keys,+ )+ if metadata_declares_goal_values and submitted_contains_full_spine and assigned_submitted_parameter_keys:+ joined = ", ".join(f"`{key}`" for key in assigned_submitted_parameter_keys)+ return _SynthesizedCodeImpositionResult(+ workflow_yaml=workflow_yaml,+ violations=[+ "Unable to impose synthesized code block: submitted extraction code assigns synthesized "+ f"parameter key(s) {joined} before the scout browser spine."+ ],+ repair_context=repair_context,+ )+ selected_mutations, _, selected_ambiguous = _browser_surface_for_code(submitted_code)preserve_submitted_extraction = (- bool(raw_metadata)- and _raw_metadata_declares_goal_values_for_block(raw_metadata, str(code_block.get("label") or ""))+ metadata_declares_goal_values+ and submitted_contains_full_spineand not extraction_suffixand not _is_submitted_code_synthesized_only(submitted_code, synthesized.code))+ append_selected_extraction = (+ metadata_declares_goal_values+ and not submitted_contains_full_spine+ and not selected_mutations+ and not selected_ambiguous+ )+ scrubbed_selected_metadata_label = (+ str(code_block.get("label") or "")+ if metadata_declares_goal_values+ and not preserve_submitted_extraction+ and not extraction_suffix+ and not append_selected_extraction+ else None+ )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)+ elif append_selected_extraction:+ imposed_code = imposed_code.rstrip() + "\n" + textwrap.dedent(submitted_code).strip() + "\n"+ imposed_code = _apply_parameter_reconciliation_to_code(imposed_code, parameter_reconciliation)code_block["code"] = imposed_codecredential_parameter_keys = [str(param.get("key") or "") for param in synthesized.parameters if str(param.get("credential_id") or "").strip()@@ -2791,11 +3273,22 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)substitutions["preserved_extraction_suffix"] = Trueif preserve_submitted_extraction:substitutions["preserved_submitted_extraction_code"] = True+ if scrubbed_selected_metadata_label:+ substitutions["scrubbed_stale_selected_goal_value_paths"] = Trueif forgiven_superseded_bare_drops:substitutions["forgiven_superseded_bare_drops"] = forgiven_superseded_bare_drops+ selected_extraction_metadata_disposition: SelectedExtractionMetadataDisposition = "none"+ if scrubbed_selected_metadata_label:+ selected_extraction_metadata_disposition = "browser_spine_replaced_metadata_stale"+ elif preserve_submitted_extraction:+ selected_extraction_metadata_disposition = "self_authored_extraction_preserved"+ elif extraction_suffix or append_selected_extraction:+ selected_extraction_metadata_disposition = "sibling_or_suffix_extraction_preserved"return _SynthesizedCodeImpositionResult(workflow_yaml=yaml.safe_dump(parsed, sort_keys=False),substitutions=substitutions,+ scrubbed_selected_metadata_label=scrubbed_selected_metadata_label,+ selected_extraction_metadata_disposition=selected_extraction_metadata_disposition,)@@ -2965,6 +3458,8 @@ def _artifact_goal_value_roots(artifact: Mapping[str, Any]) -> set[str]:def _target_names(target: ast.expr) -> set[str]:if isinstance(target, ast.Name):return {target.id}+ if isinstance(target, ast.Starred):+ return _target_names(target.value)if isinstance(target, (ast.Tuple, ast.List)):names: set[str] = set()for element in target.elts:@@ -2973,6 +3468,21 @@ def _target_names(target: ast.expr) -> set[str]:return set()+def _pattern_bound_names(pattern: ast.pattern) -> set[str]:+ if isinstance(pattern, ast.MatchAs):+ bound_names = {pattern.name} if pattern.name else set()+ if pattern.pattern is not None:+ bound_names.update(_pattern_bound_names(pattern.pattern))+ return bound_names+ if isinstance(pattern, ast.MatchStar):+ return {pattern.name} if pattern.name else set()+ names: set[str] = set()+ for child in ast.iter_child_nodes(pattern):+ if isinstance(child, ast.pattern):+ names.update(_pattern_bound_names(child))+ return names++def _assigned_top_level_names(statements: list[ast.stmt]) -> set[str]:names: set[str] = set()for node in _iter_top_level_scope(statements):@@ -2988,6 +3498,36 @@ def _assigned_top_level_names(statements: list[ast.stmt]) -> set[str]:return names+def _assigned_submitted_parameter_keys(code: str, parameter_keys: list[str]) -> list[str]:+ protected = {key for key in parameter_keys if key}+ if not protected:+ return []+ tree = _wrapped_code_ast(code)+ if tree is None:+ return []+ assigned: set[str] = set()+ for node in _submitted_scope_nodes(tree):+ if isinstance(node, ast.Assign):+ for target in node.targets:+ assigned.update(_target_names(target))+ elif isinstance(node, ast.AnnAssign):+ assigned.update(_target_names(node.target))+ elif isinstance(node, (ast.AugAssign, ast.NamedExpr)):+ assigned.update(_target_names(node.target))+ elif isinstance(node, (ast.For, ast.AsyncFor)):+ assigned.update(_target_names(node.target))+ elif isinstance(node, (ast.With, ast.AsyncWith)):+ for item in node.items:+ if item.optional_vars is not None:+ assigned.update(_target_names(item.optional_vars))+ elif isinstance(node, ast.ExceptHandler) and node.name:+ assigned.add(node.name)+ elif isinstance(node, ast.Match):+ for case in node.cases:+ assigned.update(_pattern_bound_names(case.pattern))+ return sorted(assigned & protected)++def _missing_declared_output_roots(code: str, goal_roots: set[str]) -> set[str] | None:if not goal_roots:return None@@ -3067,12 +3607,19 @@ def _parse_extraction_schema(value: Any) -> dict[str, Any] | None:def _artifact_extraction_schema_values(artifact: Mapping[str, Any]) -> list[Any]:- values: list[Any] = []+ return [value for value, _provenance in _artifact_extraction_schema_entries(artifact)]+++def _artifact_extraction_schema_entries(artifact: Mapping[str, Any]) -> list[tuple[Any, ExtractionSchemaProvenance]]:+ values: list[tuple[Any, ExtractionSchemaProvenance]] = []for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):for row in _artifact_rows(artifact.get(field_name)):schema = row.get("extraction_schema")if schema is not None and not (isinstance(schema, str) and not schema.strip()):- values.append(schema)+ provenance: ExtractionSchemaProvenance = (+ "self_authored" if row.get("extraction_schema_provenance") == "self_authored" else "user_edited"+ )+ values.append((schema, provenance))return values@@ -3274,7 +3821,9 @@ def _extraction_schema_incompatibility(return Noneincompatible: set[str] = set()summaries: list[str] = []- for value in _artifact_extraction_schema_values(artifact):+ for value, provenance in _artifact_extraction_schema_entries(artifact):+ if provenance != "user_edited":+ continueif isinstance(value, str) and _is_unfilled_artifact_placeholder(value.strip()):continueparsed = _parse_extraction_schema(value)@@ -3787,6 +4336,16 @@ async def _update_workflow(if _copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER:workflow_yaml = _adopt_exact_declared_parameter_keys_for_unresolved_names(workflow_yaml)params["workflow_yaml"] = workflow_yaml+ if (+ imposition.selected_extraction_metadata_disposition == "browser_spine_replaced_metadata_stale"+ and imposition.scrubbed_selected_metadata_label+ ):+ scrubbed_metadata = _downgrade_stale_selected_goal_value_paths(+ params.get("code_artifact_metadata"),+ imposition.scrubbed_selected_metadata_label,+ )+ params["code_artifact_metadata"] = scrubbed_metadata+ ctx.raw_code_artifact_metadata = scrubbed_metadataparameter_contract_error = _code_block_parameter_contract_error(workflow_yaml)if parameter_contract_error is not None:return reject(diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.pyindex 73014b64a..cfc367dae 100644--- a/tests/unit/test_copilot_code_block_persist_seam.py+++ b/tests/unit/test_copilot_code_block_persist_seam.py@@ -1445,9 +1445,15 @@ 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 == ""+ assert result["ok"] is True+ substitutions = result["data"]["imposed_substitutions"]+ assert substitutions["scrubbed_stale_selected_goal_value_paths"] is True+ assert "preserved_submitted_extraction_code" not in substitutions+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ block = _single_code_block(parsed)+ assert 'await page.locator("#provInput").fill(str(provider_name))' in block["code"]+ assert not workflow_update_module._artifact_declares_goal_values(ctx.code_artifact_metadata["search_registry"])@pytest.mark.asyncioasync def test_output_intent_requires_artifact_metadata_before_persist(@@ -3614,6 +3620,7 @@ class TestCodeAuthoringGuardrailChurnBackstop:_RESALE_URL = "https://example.com/orders"+_QUOTE_URL = "https://example.com/quote"def _resale_ctx(*, refiner_selector: str = 'button[data-action="status"]') -> CopilotContext:@@ -3662,6 +3669,1200 @@ def _resale_submitted_yaml(refiner_selector: str = 'button[data-action="status"])+def _quote_ctx() -> CopilotContext:+ ctx = _code_only_ctx()+ _enable_imposition(ctx)+ ctx.scout_trajectory = [+ {+ "tool_name": "type_text",+ "selector": "#zip",+ "source_url": _QUOTE_URL,+ "typed_length": 5,+ "typed_value": "02110",+ "role": "textbox",+ "accessible_name": "ZIP code",+ "trajectory_index": 0,+ },+ {+ "tool_name": "click",+ "selector": "#continue",+ "source_url": _QUOTE_URL,+ "trajectory_index": 1,+ },+ {+ "tool_name": "click",+ "selector": "#coverage-next",+ "source_url": "https://example.com/quote/coverage",+ "trajectory_index": 2,+ },+ ]+ return ctx+++def _code_blocks(parsed: dict[str, object]) -> dict[str, dict[str, object]]:+ blocks = [block for block in workflow_blocks(parsed) if str(block.get("block_type") or "").lower() == "code"]+ return {str(block.get("label") or ""): block for block in blocks}+++def _submitted_with_sibling_code(sibling_code: str) -> str:+ indented = textwrap.indent(textwrap.dedent(sibling_code).strip(), " " * 14)+ return _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ - block_type: code+ label: preserved_code+ code: |+{indented}+ """+ )+++class TestWholeTrajectoryImposition:+ def test_imposes_over_unscouted_browser_fill_in_selected_block(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#electricDate").fill("2026-07-01")+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricDate" not in code+ assert code.index('page.locator("#zip")') < code.index('page.locator("#continue")')+ assert code.index('page.locator("#continue")') < code.index('page.locator("#coverage-next")')++ def test_imposes_over_unscouted_selected_block_extra_click(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#electricDate").click()+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricDate" not in code+ assert 'page.locator("#zip")' in code+ assert 'page.locator("#continue")' in code+ assert 'page.locator("#coverage-next")' in code++ def test_p10_shaped_selected_surplus_browser_mutations_are_discarded(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#electricPlan").select_option("basic")+ await page.locator("#electricDate").fill("2026-07-01")+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricPlan" not in code+ assert "#electricDate" not in code+ assert result.substitutions["source_trajectory_count"] == 3+ assert code.index('page.locator("#zip")') < code.index('page.locator("#continue")')+ assert code.index('page.locator("#continue")') < code.index('page.locator("#coverage-next")')++ def test_wrong_selected_block_receiver_is_overwritten_by_scout_spine(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#wrongZip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#wrongZip" not in code+ assert 'page.locator("#zip")' in code++ def test_selected_alias_locator_extra_is_discarded_by_imposition(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ target = page.locator("#electricDate")+ await target.fill("2026-07-01")+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricDate" not in code+ assert 'target = page.locator("#electricDate")' not in code+ assert 'page.locator("#zip")' in code+ assert 'page.locator("#coverage-next")' in code++ def test_selected_helper_extra_is_discarded_by_imposition(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ async def clear(target):+ await target.evaluate("node => node.remove()")+ await clear(target=page.locator("#electricDate"))+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricDate" not in code+ assert "async def clear" not in code++ def test_selected_dynamic_extra_is_discarded_by_imposition(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await getattr(page.locator("#electricDate"), "fill")("2026-07-01")+ await page.locator("#coverage-next").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert "#electricDate" not in code+ assert "getattr" not in code++ def test_rejects_extra_changed_block_with_unscouted_browser_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#coverage-next").click()+ - block_type: code+ label: invented_browser_step+ code: |+ await page.locator("#electricDate").fill("2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("unscouted browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_unknown_page_receiver_call(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ await page.evaluate("window.localStorage.clear()")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_unknown_direct_locator_receiver_call(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ await page.locator("#electricDate").evaluate("node => node.remove()")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_unknown_locator_alias_receiver_call(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ target = page.locator("#electricDate")+ await target.evaluate("node => node.remove()")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_page_alias_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ p = page+ await p.goto("https://example.com/other")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_transitive_page_alias_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ p = page+ q = p+ await q.goto("https://example.com/other")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_transitive_locator_alias_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ target = page.locator("#electricDate")+ other = target+ await other.fill("2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_bound_method_alias_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ fill_electric = page.locator("#electricDate").fill+ await fill_electric("2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_transitive_bound_method_alias_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ fill_electric = page.locator("#electricDate").fill+ other = fill_electric+ await other("2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_dynamic_dispatch_on_page(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ await getattr(page, "goto")("https://example.com/other")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_dynamic_dispatch_on_locator_alias(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ target = page.locator("#electricDate")+ await getattr(target, "fill")("2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_helper_receiving_browser_object(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ async def clear(target):+ await target.evaluate("node => node.remove()")+ await clear(page.locator("#electricDate"))+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_helper_receiving_browser_keyword_object(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ async def clear(target):+ await target.evaluate("node => node.remove()")+ await clear(target=page.locator("#electricDate"))+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_rejects_helper_receiving_page_keyword_object(self) -> None:+ ctx = _quote_ctx()+ submitted = _submitted_with_sibling_code(+ """+ async def navigate(page_arg):+ await page_arg.goto("https://example.com/other")+ await navigate(page_arg=page)+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_preserves_simple_extraction_only_block(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ - block_type: code+ label: summarize_quote+ code: |+ heading = await page.locator("h1").inner_text()+ return {{"heading": heading}}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ blocks = _code_blocks(parse_workflow_yaml(result.workflow_yaml))+ assert "#coverage-next" in str(blocks[workflow_update_module._SYNTHESIZED_BLOCK_LABEL]["code"])+ assert str(blocks["summarize_quote"]["code"]).strip() == (+ 'heading = await page.locator("h1").inner_text()\nreturn {"heading": heading}'+ )++ def test_preserves_read_only_selected_extraction_suffix_after_exact_spine(self) -> None:+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ textwrap.dedent(synthesized.code).rstrip()+ + '\nheading = await page.locator("h1").inner_text()\nreturn {"heading": heading}\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ assert result.substitutions["preserved_extraction_suffix"] is True+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert 'await page.locator("#coverage-next").click()' in code+ assert 'heading = await page.locator("h1").inner_text()' in code+ assert code.index('await page.locator("#coverage-next").click()') < code.index(+ 'heading = await page.locator("h1").inner_text()'+ )++ def test_preserves_page_read_only_selected_extraction_suffix_after_exact_spine(self) -> None:+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ textwrap.dedent(synthesized.code).rstrip()+ + '\nheading = await page.inner_text("h1")\nreturn {"heading": heading}\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ assert result.substitutions["preserved_extraction_suffix"] is True+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])+ assert 'await page.locator("#coverage-next").click()' in code+ assert 'heading = await page.inner_text("h1")' in code+ assert code.index('await page.locator("#coverage-next").click()') < code.index(+ 'heading = await page.inner_text("h1")'+ )++ def test_rejects_selected_extraction_suffix_browser_mutation(self) -> None:+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ textwrap.dedent(synthesized.code).rstrip() + '\nawait page.locator("#electricDate").fill("2026-07-01")\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any(+ "extraction suffix contains unscouted browser action" in violation for violation in result.violations+ )+ assert result.substitutions is None++ def test_rejects_selected_extraction_suffix_alias_browser_mutation(self) -> None:+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ textwrap.dedent(synthesized.code).rstrip()+ + '\ntarget = page.locator("#electricDate")\nawait target.fill("2026-07-01")\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any(+ "extraction suffix contains ambiguous browser action" in violation for violation in result.violations+ )+ assert result.substitutions is None++ def test_rejects_ambiguous_helper_browser_mutation(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ - block_type: code+ label: helper_step+ code: |+ async def fill(locator, value):+ await locator.fill(value)+ target = page.locator("#electricDate")+ await fill(target, "2026-07-01")+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any("ambiguous browser action" in violation for violation in result.violations)+ assert result.substitutions is None++ def test_multi_screen_trajectory_persists_in_order_with_proven_locators(self) -> None:+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert result.violations == []+ assert result.substitutions is not None+ block = _single_code_block(parse_workflow_yaml(result.workflow_yaml))+ code = str(block["code"])+ assert code.index('page.locator("#zip")') < code.index('page.locator("#continue")')+ assert code.index('page.locator("#continue")') < code.index('page.locator("#coverage-next")')++ @pytest.mark.asyncio+ async def test_changed_selected_browser_action_args_do_not_preserve_submitted_extraction(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = textwrap.dedent(synthesized.code).replace(+ 'await page.locator("#zip").fill(str(zip_code))',+ 'await page.locator("#zip").fill("99999")',+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ parameter_keys: [zip_code]+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote")+ ],+ },+ ctx,+ )++ assert result["ok"] is True+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ code = str(_single_code_block(parsed)["code"])+ assert 'await page.locator("#zip").fill(str(zip_code))' in code+ assert 'await page.locator("#zip").fill("99999")' not in code++ @pytest.mark.asyncio+ async def test_assigned_parameter_key_does_not_preserve_submitted_extraction(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ 'zip_code = "99999"\n'+ + textwrap.dedent(synthesized.code).rstrip()+ + f'\n{workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output = {{"quote": "Q-001"}}\n'+ + f"return {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output\n"+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ parameters:+ - {{parameter_type: workflow, workflow_parameter_type: string, key: zip_code, default_value: "02110"}}+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ parameter_keys: [zip_code]+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote")+ ],+ },+ ctx,+ )++ assert result["ok"] is False+ assert "assigns synthesized parameter key(s) `zip_code`" in result["error"]++ @pytest.mark.asyncio+ async def test_destructured_parameter_key_does_not_preserve_submitted_extraction(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ '(zip_code,) = ("99999",)\n'+ + textwrap.dedent(synthesized.code).rstrip()+ + f'\n{workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output = {{"quote": "Q-001"}}\n'+ + f"return {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output\n"+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ parameters:+ - {{parameter_type: workflow, workflow_parameter_type: string, key: zip_code, default_value: "02110"}}+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ parameter_keys: [zip_code]+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote")+ ],+ },+ ctx,+ )++ assert result["ok"] is False+ assert "assigns synthesized parameter key(s) `zip_code`" in result["error"]++ @pytest.mark.asyncio+ async def test_starred_parameter_key_does_not_preserve_submitted_extraction(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = (+ '*zip_code, = ["99999"]\n'+ + textwrap.dedent(synthesized.code).rstrip()+ + f'\n{workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output = {{"quote": "Q-001"}}\n'+ + f"return {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}_output\n"+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ parameters:+ - {{parameter_type: workflow, workflow_parameter_type: string, key: zip_code, default_value: "02110"}}+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ parameter_keys: [zip_code]+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote")+ ],+ },+ ctx,+ )++ assert result["ok"] is False+ assert "assigns synthesized parameter key(s) `zip_code`" in result["error"]++ @pytest.mark.asyncio+ async def test_alias_reconciled_selected_spine_preserves_submitted_extraction(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ label = workflow_update_module._SYNTHESIZED_BLOCK_LABEL+ submitted_code = (+ textwrap.dedent(synthesized.code).replace("zip_code", "postal_code").rstrip()+ + f'\n{label}_output = {{"quote": "Q-001"}}\nreturn {label}_output\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ parameters:+ - {{parameter_type: workflow, workflow_parameter_type: string, key: postal_code, default_value: "02110"}}+ blocks:+ - block_type: code+ label: {label}+ parameter_keys: [postal_code]+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [_terminal_metadata(label, "quote")],+ },+ ctx,+ )++ assert result["ok"] is True+ substitutions = result["data"]["imposed_substitutions"]+ assert substitutions["preserved_submitted_extraction_code"] is True+ assert substitutions["parameter_aliases"] == {"zip_code": "postal_code"}+ assert "scrubbed_stale_selected_goal_value_paths" not in substitutions+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ block = _single_code_block(parsed)+ assert block["parameter_keys"] == ["postal_code"]+ code = str(block["code"])+ assert "postal_code" in code+ assert "zip_code" not in code+ assert f"return {label}_output" in code+ artifact = ctx.code_artifact_metadata[label]+ assert workflow_update_module._artifact_declares_goal_values(artifact)++ @pytest.mark.asyncio+ async def test_metadata_selected_extraction_only_imposes_scout_spine(self, monkeypatch: pytest.MonkeyPatch) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ records = [{{"quote": "pending"}}]+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote result")+ ],+ },+ ctx,+ )++ assert result["ok"] is True+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ block = _code_blocks(parsed)[workflow_update_module._SYNTHESIZED_BLOCK_LABEL]+ code = str(block["code"])+ assert result["data"]["imposed_substitutions"]["source_trajectory_count"] == 3+ assert 'await page.locator("#zip").fill(str(zip_code))' in code+ assert 'await page.locator("#continue").click()' in code+ assert 'await page.locator("#coverage-next").click()' in code+ assert 'records = [{"quote": "pending"}]' in code+ assert code.index('await page.locator("#coverage-next").click()') < code.index(+ 'records = [{"quote": "pending"}]'+ )++ @pytest.mark.asyncio+ async def test_stale_selected_goal_paths_do_not_block_imposed_scout_spine(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#electricDate").fill("2026-07-01")+ await page.locator("#coverage-next").click()+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [_terminal_metadata("quote_flow", "quote result")],+ },+ ctx,+ )++ assert result["ok"] is True+ assert result["data"]["imposed_substitutions"]["scrubbed_stale_selected_goal_value_paths"] is True+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ code = str(_single_code_block(parsed)["code"])+ assert "#electricDate" not in code+ assert 'await page.locator("#zip").fill(str(zip_code))' in code+ artifact = ctx.code_artifact_metadata["quote_flow"]+ assert not workflow_update_module._artifact_declares_goal_values(artifact)++ @pytest.mark.asyncio+ async def test_metadata_selected_page_goto_extra_is_discarded(self, monkeypatch: pytest.MonkeyPatch) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.goto("https://example.com/other")+ records = [{"number": "Q-001"}]+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [_terminal_metadata("quote_flow", "quote result")],+ },+ ctx,+ )++ assert result["ok"] is True+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ code = str(_single_code_block(parsed)["code"])+ assert 'page.goto("https://example.com/other")' not in code+ assert 'records = [{"number": "Q-001"}]' not in code+ assert 'await page.locator("#zip").fill(str(zip_code))' in code+ assert result["data"]["imposed_substitutions"]["scrubbed_stale_selected_goal_value_paths"] is True++ @pytest.mark.asyncio+ async def test_valid_selected_extraction_suffix_keeps_goal_path_metadata(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = textwrap.dedent(synthesized.code).rstrip() + '\nreturn {"records": [{"number": "Q-001"}]}\n'+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote result")+ ],+ },+ ctx,+ )++ assert result["ok"] is True+ assert result["data"]["imposed_substitutions"]["preserved_extraction_suffix"] is True+ artifact = ctx.code_artifact_metadata[workflow_update_module._SYNTHESIZED_BLOCK_LABEL]+ assert workflow_update_module._artifact_declares_goal_values(artifact)++ @pytest.mark.asyncio+ async def test_p9_opaque_self_authored_extraction_metadata_is_preserved(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ label = workflow_update_module._SYNTHESIZED_BLOCK_LABEL+ submitted_code = (+ textwrap.dedent(synthesized.code).rstrip()+ + f'\n{label}_output = {{"premium": "$123", "eligible": True}}\nreturn {label}_output\n'+ )+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {label}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )+ schema = (+ '{"type":"object","properties":{"premium":{"type":"string"},'+ '"eligible":{"type":"boolean"}},"required":["premium","eligible"]}'+ )+ metadata = _terminal_metadata(label, "quote result")+ metadata["claimed_outcomes"][0]["goal_value_paths"] = ["premium", "eligible"]+ metadata["claimed_outcomes"][0]["extraction_schema"] = schema+ metadata["claimed_outcomes"][0]["extraction_schema_provenance"] = "self_authored"+ metadata["terminal_verifier_expectations"][0]["goal_value_paths"] = ["premium", "eligible"]+ metadata["terminal_verifier_expectations"][0]["extraction_schema"] = schema+ metadata["terminal_verifier_expectations"][0]["extraction_schema_provenance"] = "self_authored"++ result = await _update_workflow({"workflow_yaml": submitted, "code_artifact_metadata": [metadata]}, ctx)++ assert result["ok"] is True+ assert result["data"]["imposed_substitutions"]["preserved_extraction_suffix"] is True+ assert "scrubbed_stale_selected_goal_value_paths" not in result["data"]["imposed_substitutions"]+ artifact = ctx.code_artifact_metadata[label]+ assert artifact["claimed_outcomes"][0]["goal_value_paths"] == ["premium", "eligible"]+ assert artifact["terminal_verifier_expectations"][0]["goal_value_paths"] == ["premium", "eligible"]+ assert artifact["claimed_outcomes"][0]["extraction_schema"] == schema+ code = str(_single_code_block(parse_workflow_yaml(ctx.workflow_yaml))["code"])+ assert f"return {label}_output" in code++ @pytest.mark.asyncio+ async def test_invalid_selected_extraction_suffix_keeps_goal_path_rejection(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = textwrap.dedent(synthesized.code).rstrip() + "\nheading = 'Review'\n"+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [+ _terminal_metadata(workflow_update_module._SYNTHESIZED_BLOCK_LABEL, "quote result")+ ],+ },+ ctx,+ )++ assert result["ok"] is False+ assert "does not return a keyed structure" in result["error"]+ assert "records" in result["error"]++ def test_rejects_selected_extraction_suffix_page_mutation(self) -> None:+ ctx = _quote_ctx()+ synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)+ assert synthesized is not None+ submitted_code = textwrap.dedent(synthesized.code).rstrip() + '\nawait page.goto("https://example.com/other")\n'+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+{textwrap.indent(submitted_code, " " * 18)}+ """+ )++ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)++ assert any(+ "extraction suffix contains unscouted browser action" in violation for violation in result.violations+ )+ assert result.substitutions is None++ @pytest.mark.asyncio+ async def test_sibling_invalid_goal_paths_still_reject(self, monkeypatch: pytest.MonkeyPatch) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ submitted = _yaml(+ f"""+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ - block_type: code+ label: summarize_quote+ code: |+ return await page.locator("h1").inner_text()+ """+ )++ result = await _update_workflow(+ {"workflow_yaml": submitted, "code_artifact_metadata": [_terminal_metadata("summarize_quote", "summary")]},+ ctx,+ )++ assert result["ok"] is False+ assert "summarize_quote" in result["error"]+ assert "flat text blob" in result["error"]++ @pytest.mark.asyncio+ async def test_p10_shaped_stale_metadata_imposes_scout_spine(self, monkeypatch: pytest.MonkeyPatch) -> None:+ _stub_successful_update(monkeypatch)+ ctx = _quote_ctx()+ submitted = _yaml(+ """+ title: Quote+ workflow_definition:+ blocks:+ - block_type: code+ label: quote_flow+ code: |+ await page.locator("#zip").fill(str(zip_code))+ await page.locator("#continue").click()+ await page.locator("#electricDate").fill("2026-07-01")+ await page.locator("#electricPlan").select_option("basic")+ await page.locator("#coverage-next").click()+ """+ )++ result = await _update_workflow(+ {+ "workflow_yaml": submitted,+ "code_artifact_metadata": [_terminal_metadata("quote_flow", "quote result")],+ },+ ctx,+ )++ assert result["ok"] is True+ parsed = parse_workflow_yaml(ctx.workflow_yaml)+ assert isinstance(parsed, dict)+ code = str(_single_code_block(parsed)["code"])+ assert "#electricDate" not in code+ assert "#electricPlan" not in code+ assert code.index('page.locator("#zip")') < code.index('page.locator("#continue")')+ assert code.index('page.locator("#continue")') < code.index('page.locator("#coverage-next")')++class TestBareDropSupersession:def test_selector_refines_css_accepts_identity_qualifiers(self) -> None:for candidate in (diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.pyindex a380fccdb..52dc84508 100644--- a/tests/unit/test_copilot_code_block_synthesis.py+++ b/tests/unit/test_copilot_code_block_synthesis.py@@ -1730,6 +1730,11 @@ class TestSynthesizedArtifactMetadata:assert metadata["completion_criteria"][0]["text"].startswith("<fill:")assert metadata["claimed_outcomes"][0]["text"].startswith("<fill:")+ def test_skeleton_marks_placeholder_schema_self_authored(self) -> None:+ metadata = build_synthesized_artifact_metadata(_SCOUT_TRAJECTORY)+ assert metadata["claimed_outcomes"][0]["extraction_schema"].startswith("<fill:")+ assert metadata["claimed_outcomes"][0]["extraction_schema_provenance"] == "self_authored"+def test_skeleton_is_byte_identical_per_trajectory(self) -> None:first = build_synthesized_artifact_metadata(_SCOUT_TRAJECTORY)second = build_synthesized_artifact_metadata(_SCOUT_TRAJECTORY)