Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-12155): thread contingent antecedent path into author-time output contract + mint-time invariant (#7276)
Andrew Neilson77db77a5a5f3
diff --git a/skyvern/forge/sdk/copilot/completion_criteria_store.py b/skyvern/forge/sdk/copilot/completion_criteria_store.pyindex 5f975f6c1..a3a574ffc 100644--- a/skyvern/forge/sdk/copilot/completion_criteria_store.py+++ b/skyvern/forge/sdk/copilot/completion_criteria_store.py@@ -37,6 +37,7 @@ from skyvern.forge.sdk.copilot.request_policy import (judgment_truth_condition_key,normalized_criterion_outcome_key,requested_output_path_for_field,+ resolve_mint_degrade,typed_expected_output_value_key,)@@ -177,7 +178,10 @@ def criteria_from_json(raw: Any) -> tuple[CompletionCriterion, ...]:)classification_output_key = _coerce_classification_output_key(item.get("classification_output_key"))expected_classification = _coerce_expected_classification(item.get("expected_classification"))- contingent_on = item.get("contingent_on")+ contingent_on_raw = item.get("contingent_on")+ contingent_on = (+ contingent_on_raw.strip() if isinstance(contingent_on_raw, str) and contingent_on_raw.strip() else None+ )contingent_antecedent_output_path = _normalize_contingent_antecedent_output_path(item.get("contingent_antecedent_output_path"))@@ -208,9 +212,7 @@ def criteria_from_json(raw: Any) -> tuple[CompletionCriterion, ...]:CompletionCriterion(id=criterion_id,outcome=outcome,- contingent_on=contingent_on.strip()- if isinstance(contingent_on, str) and contingent_on.strip()- else None,+ contingent_on=contingent_on,contingent_antecedent_output_path=contingent_antecedent_output_path,deliverable_kind=_normalize_deliverable_kind(item.get("deliverable_kind")),implicit=bool(item.get("implicit")),@@ -225,9 +227,9 @@ def criteria_from_json(raw: Any) -> tuple[CompletionCriterion, ...]:classification_output_key=classification_output_key,expected_classification=expected_classification,requested_output_corroborator=bool(item.get("requested_output_corroborator")),- mint_degrade="turn_unsatisfiable_fallback"- if item.get("mint_degrade") == "turn_unsatisfiable_fallback"- else None,+ mint_degrade=resolve_mint_degrade(+ item.get("mint_degrade"), contingent_on, contingent_antecedent_output_path+ ),judgment_truth_condition=_coerce_judgment_truth_condition(item.get("judgment_predicate"), item.get("judgment_polarity_when_holds")),diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 6cd2ebf40..5f9272465 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -209,6 +209,8 @@ RequestedOutputEvidenceSource = Literal[]JudgmentPredicate = Literal["login_gate_blocks_target"]_JUDGMENT_PREDICATES: frozenset[str] = frozenset(get_args(JudgmentPredicate))+MintDegrade = Literal["turn_unsatisfiable_fallback", "contingent_missing_antecedent"]+MINT_DEGRADE_VALUES: frozenset[str] = frozenset(get_args(MintDegrade))_CRITERION_KINDS: frozenset[str] = frozenset({"outcome", "terminal_action", "validation_classification"})_TERMINAL_ACTION_FAMILIES: frozenset[str] = frozenset({"request", "application", "form", "order"})_EXPECTED_OUTPUT_SHAPES: frozenset[str] = frozenset(get_args(ExpectedOutputShape))@@ -293,7 +295,7 @@ class CompletionCriterion:classification_output_key: str | None = Noneexpected_classification: ClassificationTarget | None = Nonerequested_output_corroborator: bool = False- mint_degrade: Literal["turn_unsatisfiable_fallback"] | None = None+ mint_degrade: MintDegrade | None = Nonejudgment_truth_condition: JudgmentTruthCondition | None = None@@ -389,6 +391,14 @@ class RequestPolicy:data[f"{prefix}_evidence_source"] = criterion.requested_output_evidence_sourceif criterion.expected_output_shape:data[f"{prefix}_expected_output_shape"] = criterion.expected_output_shape+ mint_degraded_criteria = [+ criterion for criterion in self.completion_criteria if criterion.mint_degrade is not None+ ]+ data["mint_degraded_criterion_count"] = len(mint_degraded_criteria)+ for index, criterion in enumerate(mint_degraded_criteria[:_MAX_TRACE_COMPLETION_CRITERIA]):+ prefix = f"mint_degraded_criterion_{index}"+ data[f"{prefix}_id"] = criterion.id+ data[f"{prefix}_mint_degrade"] = criterion.mint_degradereturn datadef prompt_summary(self) -> str:@@ -1702,7 +1712,19 @@ def is_fallback_floor_base_criterion(criterion: CompletionCriterion) -> bool:def is_turn_unsatisfiable_fallback_degraded(criterion: CompletionCriterion) -> bool:- return criterion.mint_degrade == "turn_unsatisfiable_fallback"+ return criterion.mint_degrade is not None+++def resolve_mint_degrade(+ stored_value: object,+ contingent_on: str | None,+ contingent_antecedent_output_path: str | None,+) -> MintDegrade | None:+ if isinstance(stored_value, str) and stored_value in MINT_DEGRADE_VALUES:+ return cast(MintDegrade, stored_value)+ if contingent_on and contingent_antecedent_output_path is None:+ return "contingent_missing_antecedent"+ return None_FALLBACK_LITERAL_MIN_CHARS = 4@@ -1783,6 +1805,26 @@ def _mark_turn_unsatisfiable_fallback_criteria(policy: RequestPolicy) -> None:policy.completion_criteria = marked+def _degrade_pathless_contingent_criteria(policy: RequestPolicy) -> None:+ swept: list[CompletionCriterion] = []+ degraded_ids: list[str] = []+ for criterion in policy.completion_criteria:+ resolved = resolve_mint_degrade(+ criterion.mint_degrade, criterion.contingent_on, criterion.contingent_antecedent_output_path+ )+ if resolved != criterion.mint_degrade:+ criterion = replace(criterion, mint_degrade=resolved)+ degraded_ids.append(criterion.id)+ swept.append(criterion)+ policy.completion_criteria = swept+ if degraded_ids:+ LOG.info(+ "copilot_contingent_criterion_mint_degraded",+ criterion_ids=degraded_ids,+ mint_degrade="contingent_missing_antecedent",+ )++def build_classifier_fallback_floor(ids: list[str]) -> list[CompletionCriterion]:floor = [CompletionCriterion(@@ -1850,6 +1892,7 @@ def _classifier_fallback_policy()_apply_classifier_typed_requested_output_corroborators(policy)_mark_turn_unsatisfiable_fallback_criteria(policy)+ _degrade_pathless_contingent_criteria(policy)if policy.graded_completion_criteria():policy.completion_contract_status = "present"return policy@@ -2101,6 +2144,7 @@ async def _classify_request(_apply_requested_output_completion_criteria(policy, user_message, requested_output_path_aliases)_apply_validation_classification_completion_criteria(policy)_apply_classifier_typed_requested_output_corroborators(policy)+ _degrade_pathless_contingent_criteria(policy)policy.completion_contract_status = ("present" if policy.completion_contract or policy.graded_completion_criteria() else "absent")diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.pyindex a6f42c8c6..26ad8c3ab 100644--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py@@ -1680,12 +1680,33 @@ def _requested_output_child_paths(ctx: AgentContext) -> set[str]:continueif getattr(criterion, "kind", None) == "validation_classification":continue+ if getattr(criterion, "mint_degrade", None) is not None:+ continuepath = _canonical_requested_output_path(getattr(criterion, "output_path", None))if path and _output_path_has_child(path):paths.add(path)return paths+def _contingent_antecedent_child_paths(ctx: AgentContext) -> set[str]:+ if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:+ return set()+ paths: set[str] = set()+ for criterion in _active_completion_criteria(ctx):+ if not isinstance(criterion, CompletionCriterion):+ continue+ if criterion.mint_degrade is not None:+ continue+ if criterion.level == "definition" and not (+ criterion.output_path and _is_judgment_boolean_criterion(criterion)+ ):+ continue+ path = _canonical_requested_output_path(criterion.contingent_antecedent_output_path)+ if path and _output_path_has_child(path):+ paths.add(path)+ return paths++def _active_completion_criteria(ctx: AgentContext) -> list[CompletionCriterion]:request_policy = ctx.request_policyif request_policy is None:@@ -1741,13 +1762,22 @@ def _required_child_output_paths_for_authoring(ctx: AgentContext) -> tuple[set[s)-def _missing_requested_output_facts(paths: Iterable[str], *, reason_code: str) -> list[dict[str, object]]:+_DECLARATION_REQUIRED_VALUE_STATUS = "declaration_required_default_none"+++def _missing_requested_output_facts(+ paths: Iterable[str],+ *,+ reason_code: str,+ declaration_paths: set[str] | None = None,+) -> list[dict[str, object]]:+ declaration_paths = declaration_paths or set()return [{"output_path": path,"output_root": _output_path_root(path),"reason_code": reason_code,- "value_status": "no_typed_value",+ "value_status": (_DECLARATION_REQUIRED_VALUE_STATUS if path in declaration_paths else "no_typed_value"),}for path in sorted(paths)]@@ -1758,22 +1788,38 @@ def _single_repair_block_label(block_labels: list[str]) -> str:return labels[0] if len(labels) == 1 else ""+def _normalized_repair_paths(paths: Iterable[str]) -> list[str]:+ return sorted(dict.fromkeys(str(path).strip() for path in paths if str(path).strip()))+++def _declaration_repair_sentence(declaration_paths: Iterable[str]) -> str:+ declaration_text = ", ".join(_normalized_repair_paths(declaration_paths))+ if not declaration_text:+ return ""+ return (+ f" Declare {declaration_text} in the extraction_schema and the returned structure with value None "+ "unless the run actually hits that condition; never source it from the page."+ )++def _metadata_repair_contract(*,block_labels: list[str],required_paths: Iterable[str],source: str,reason_code: str,+ declaration_paths: Iterable[str] = (),) -> dict[str, object] | None:- paths = sorted(dict.fromkeys(str(path).strip() for path in required_paths if str(path).strip()))+ goal_paths = _normalized_repair_paths(required_paths)+ union_paths = sorted(dict.fromkeys([*goal_paths, *_normalized_repair_paths(declaration_paths)]))block_label = _single_repair_block_label(block_labels)- if not paths or not block_label:+ if not union_paths or not block_label:return Nonereturn {"block_label": block_label,- "required_goal_value_paths": paths,- "required_extraction_schema_paths": paths,- "required_code_return_paths": paths,+ "required_goal_value_paths": goal_paths,+ "required_extraction_schema_paths": union_paths,+ "required_code_return_paths": union_paths,"source": source,"reason_code": reason_code,}@@ -1786,25 +1832,29 @@ def _metadata_output_repair_context(coverage_reason_code: str,source: str,summary: str,+ declaration_paths: Iterable[str] = (),) -> CodeAuthoringRepairContext | None:- paths = sorted(dict.fromkeys(str(path).strip() for path in required_paths if str(path).strip()))+ goal_paths = _normalized_repair_paths(required_paths)+ declaration = _normalized_repair_paths(declaration_paths)+ union_paths = sorted(dict.fromkeys([*goal_paths, *declaration]))block_label = _single_repair_block_label(block_labels)- if not paths or not block_label:+ if not union_paths or not block_label:return None- path_text = ", ".join(paths)+ path_text = ", ".join(union_paths)return CodeAuthoringRepairContext(block_label=block_label,reason_code="metadata_reject",runtime_failure_class=coverage_reason_code,runtime_failure_reason=summary,- required_goal_value_paths=paths,- required_extraction_schema_paths=paths,- required_code_return_paths=paths,+ required_goal_value_paths=goal_paths,+ required_extraction_schema_paths=union_paths,+ required_code_return_paths=union_paths,metadata_contract_source=source,metadata_contract_reason_code=coverage_reason_code,repair_instruction=("Declare code_artifact_metadata goal_value_paths and extraction_schema for required output paths "f"{path_text}, make the code return those paths, then rerun update_and_run_blocks."+ + _declaration_repair_sentence(declaration)),)@@ -1827,6 +1877,8 @@ class _OutputContractEvaluation:block_label: strartifact_id: strrequired_paths: set[str]+ observation_paths: set[str]+ declaration_paths: set[str]source: strreason_code: strmissing_metadata_paths: list[str]@@ -1917,15 +1969,19 @@ def _path_segments(path: str) -> list[tuple[str, bool]]:return segments-def _schema_template_for_required_paths(required_paths: Iterable[str]) -> dict[str, Any]:+def _schema_template_for_required_paths(+ required_paths: Iterable[str],+ declaration_paths: Iterable[str] = (),+) -> dict[str, Any]:schema: dict[str, Any] = {"type": "object", "properties": {}, "required": []}+ declaration = _metadata_contract_required_paths(declaration_paths)def ensure_required(container: dict[str, Any], key: str) -> None:required = container.setdefault("required", [])if isinstance(required, list) and key not in required:required.append(key)- for path in sorted(_metadata_contract_required_paths(required_paths)):+ for path in sorted(_metadata_contract_required_paths(required_paths) | declaration):segments = _path_segments(path)container = schemafor index, (key, is_array) in enumerate(segments):@@ -1950,6 +2006,9 @@ def _schema_template_for_required_paths(required_paths: Iterable[str]) -> dict[sitems.setdefault("required", [])container = itemscontinue+ if is_leaf and path in declaration:+ properties.setdefault(key, {"type": ["string", "null"]})+ continuechild = properties.setdefault(key, {} if is_leaf else {"type": "object", "properties": {}, "required": []})if isinstance(child, dict) and not is_leaf:child.setdefault("type", "object")@@ -1959,8 +2018,11 @@ def _schema_template_for_required_paths(required_paths: Iterable[str]) -> dict[sreturn schema-def _schema_template_text_for_required_paths(required_paths: Iterable[str]) -> str:- return json.dumps(_schema_template_for_required_paths(required_paths), sort_keys=True)+def _schema_template_text_for_required_paths(+ required_paths: Iterable[str],+ declaration_paths: Iterable[str] = (),+) -> str:+ return json.dumps(_schema_template_for_required_paths(required_paths, declaration_paths), sort_keys=True)def _metadata_contract_template(@@ -1969,10 +2031,12 @@ def _metadata_contract_template(required_paths: set[str],source: str,reason_code: str,+ declaration_paths: set[str] | None = None,) -> dict[str, Any]:+ declaration_paths = declaration_paths or set()artifact_id = _artifact_id_for_block_label(block_label)- schema_text = _schema_template_text_for_required_paths(required_paths)- paths = sorted(required_paths)+ schema_text = _schema_template_text_for_required_paths(required_paths | declaration_paths, declaration_paths)+ paths = sorted(required_paths - declaration_paths)return {"block_label": block_label,"artifact_id": artifact_id,@@ -1998,12 +2062,21 @@ def _metadata_contract_template(}-def _return_skeleton_for_required_paths(required_paths: Iterable[str]) -> str:- paths = sorted(_metadata_contract_required_paths(required_paths))+def _return_skeleton_for_required_paths(+ required_paths: Iterable[str],+ declaration_paths: Iterable[str] = (),+) -> str:+ declaration = _metadata_contract_required_paths(declaration_paths)+ paths = sorted(_metadata_contract_required_paths(required_paths) | declaration)roots = sorted({_output_path_root(path) for path in paths if _output_path_root(path)})if not roots:return ""if roots == ["output"]:+ declaration_children = {+ child+ for path in declaration+ if (child := _output_path_direct_child(path, "output")) and _return_scaffold_name_is_safe(child)+ }child_names = sorted({child@@ -2012,7 +2085,9 @@ def _return_skeleton_for_required_paths(required_paths: Iterable[str]) -> str:})if child_names:- pairs = ", ".join(f'"{name}": {name}' for name in child_names)+ pairs = ", ".join(+ f'"{name}": None' if name in declaration_children else f'"{name}": {name}' for name in child_names+ )return f'return {{"output": {{{pairs}}}}}'return "return output"if len(roots) == 1 and _return_scaffold_name_is_safe(roots[0]):@@ -2329,44 +2404,86 @@ def _runtime_output_repair_contract_from_recorded_outcome(ctx: AgentContext) ->)-def _output_contract_required_paths_source(ctx: AgentContext) -> tuple[set[str], str, str]:+@dataclass(frozen=True)+class _OutputContractRequiredPaths:+ """Two-lane contract: observation paths must be sourced from the page/run; declaration paths+ must only be declared in the returned structure (None when the contingency never fires)."""++ observation_paths: set[str]+ declaration_paths: set[str]+ source: str+ reason_code: str++ @property+ def union(self) -> set[str]:+ return self.observation_paths | self.declaration_paths+++def _output_contract_required_paths_source(ctx: AgentContext) -> _OutputContractRequiredPaths:runtime_contract = _runtime_output_repair_contract_from_recorded_outcome(ctx)+ antecedent_paths = _contingent_antecedent_child_paths(ctx)if runtime_contract is not None:- return (- runtime_contract.required_paths - _independent_judgment_output_paths(ctx),- runtime_contract.source,- runtime_contract.reason_code,+ runtime_observation_paths = runtime_contract.required_paths - _independent_judgment_output_paths(ctx)+ return _OutputContractRequiredPaths(+ observation_paths=runtime_observation_paths,+ declaration_paths=antecedent_paths - runtime_observation_paths,+ source=runtime_contract.source,+ reason_code=runtime_contract.reason_code,)- required_paths, source, reason_code = _required_child_output_paths_for_authoring(ctx)+ observation_paths, source, reason_code = _required_child_output_paths_for_authoring(ctx)repair_context = getattr(ctx, "last_code_authoring_repair_context", None)- if required_paths or not isinstance(repair_context, CodeAuthoringRepairContext):- return required_paths, source, reason_code- if repair_context.reason_code != "metadata_reject":- return required_paths, source, reason_code- required_paths = _metadata_contract_required_paths(- [- *repair_context.required_goal_value_paths,- *repair_context.required_extraction_schema_paths,- *repair_context.required_code_return_paths,- ]- )- required_paths -= _independent_judgment_output_paths(ctx)- source = str(repair_context.metadata_contract_source or "").strip() or "metadata_reject"- reason_code = (- str(repair_context.metadata_contract_reason_code or "").strip()- or str(repair_context.runtime_failure_class or "").strip()- or "metadata_reject"+ if (+ not observation_paths+ and isinstance(repair_context, CodeAuthoringRepairContext)+ and repair_context.reason_code == "metadata_reject"+ ):+ goal_paths = _metadata_contract_required_paths(repair_context.required_goal_value_paths)+ rehydrated = _metadata_contract_required_paths(+ [+ *repair_context.required_goal_value_paths,+ *repair_context.required_extraction_schema_paths,+ *repair_context.required_code_return_paths,+ ]+ )+ rehydrated -= _independent_judgment_output_paths(ctx)+ # An antecedent the repair contract carried only in schema/return roles stays in the+ # declaration lane on rehydration; the goal role is the observation-lane record.+ observation_paths = rehydrated - (antecedent_paths - goal_paths)+ source = str(repair_context.metadata_contract_source or "").strip() or "metadata_reject"+ reason_code = (+ str(repair_context.metadata_contract_reason_code or "").strip()+ or str(repair_context.runtime_failure_class or "").strip()+ or "metadata_reject"+ )+ return _OutputContractRequiredPaths(+ observation_paths=observation_paths,+ declaration_paths=antecedent_paths - observation_paths,+ source=source,+ reason_code=reason_code,)- return required_paths, source, reason_code-def _code_return_is_static_advisory_candidate(code: str, required_paths: set[str]) -> bool:- if not required_paths:+def _declaration_envelope_paths(declaration_paths: set[str]) -> set[str]:+ return declaration_paths | {_output_path_root(path) for path in declaration_paths}+++def _code_return_is_static_advisory_candidate(+ code: str,+ required_paths: set[str],+ declaration_paths: set[str] | None = None,+) -> bool:+ declaration_paths = declaration_paths or set()+ if not (required_paths - declaration_paths):return False- produced = _code_block_produced_output_roots(code)- if not produced.abstained:+ declaration_envelope = _declaration_envelope_paths(declaration_paths)+ produced_paths = _code_block_produced_output_paths(code)+ if produced_paths - declaration_envelope:return False- if _code_block_produced_output_paths(code):+ produced = _code_block_produced_output_roots(code)+ # A stamped declaration-only return (e.g. blocker: None) keeps the block abstained for the+ # observation lane; only observation-lane production disqualifies the advisory.+ declaration_only_production = bool(produced_paths) and produced_paths <= declaration_envelope+ if not produced.abstained and not declaration_only_production:return Falsetry:tree = ast.parse(textwrap.dedent(code).strip() or "pass")@@ -2388,7 +2505,12 @@ def _evaluate_output_contract_for_code_block(if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:return Noneruntime_contract = _runtime_output_repair_contract_from_recorded_outcome(ctx)- required_paths, source, reason_code = _output_contract_required_paths_source(ctx)+ contract = _output_contract_required_paths_source(ctx)+ required_paths = contract.union+ observation_paths = contract.observation_paths+ declaration_paths = contract.declaration_paths+ source = contract.source+ reason_code = contract.reason_codeif not required_paths:return Noneeffective_metadata = raw_code_artifact_metadata@@ -2400,7 +2522,7 @@ def _evaluate_output_contract_for_code_block(ctx,workflow_yaml,effective_metadata,- required_paths,+ observation_paths,)code_blocks = _workflow_yaml_code_blocks_by_label(workflow_yaml)target_block = code_blocks.get(block_label) if block_label else None@@ -2410,9 +2532,11 @@ def _evaluate_output_contract_for_code_block(submitted_code_paths = (_code_block_produced_output_paths(str(target_block.get("code") or "")) if target_block is not None else set())- missing_metadata_paths = sorted(required_paths - submitted_goal_paths)+ missing_metadata_paths = sorted(observation_paths - submitted_goal_paths)missing_schema_paths = sorted(required_paths - submitted_schema_paths)missing_return_paths = sorted(required_paths - submitted_code_paths)+ missing_observation_return_paths = sorted(observation_paths - submitted_code_paths)+ missing_declaration_return_paths = sorted(declaration_paths - submitted_code_paths)shape_violations: list[str] = []if not block_label:shape_violations.append("ambiguous_output_owner" if owner_labels else "missing_output_owner")@@ -2449,7 +2573,7 @@ def _evaluate_output_contract_for_code_block(]post_steering_static_return_advisory = (allow_static_return_advisory- and bool(missing_return_paths)+ and bool(missing_observation_return_paths)and not missing_metadata_pathsand not missing_schema_pathsand not run_gating_shape_violations@@ -2461,14 +2585,16 @@ def _evaluate_output_contract_for_code_block()static_return_advisory = (allow_static_return_advisory- and bool(missing_return_paths)+ and bool(missing_observation_return_paths)and not missing_metadata_pathsand not missing_schema_pathsand not run_gating_shape_violationsand target_block is not Noneand (post_steering_static_return_advisory- or _code_return_is_static_advisory_candidate(str(target_block.get("code") or ""), required_paths)+ or _code_return_is_static_advisory_candidate(+ str(target_block.get("code") or ""), required_paths, declaration_paths+ )))separated_spine_run_eligible = (@@ -2477,26 +2603,32 @@ def _evaluate_output_contract_for_code_block(and not missing_schema_pathsand not run_gating_shape_violations)- run_eligible = static_return_advisory or separated_spine_run_eligible- effective_missing_return_paths = [] if static_return_advisory else missing_return_paths+ # Declaration paths are trivially satisfiable by construction, so no advisory door may waive+ # them; only observation-lane return misses are advisory-eligible.+ run_eligible = (static_return_advisory or separated_spine_run_eligible) and not missing_declaration_return_paths+ effective_missing_return_paths = (+ missing_declaration_return_paths if static_return_advisory else missing_return_paths+ )runtime_signature = _runtime_output_contract_signature(runtime_contract)artifact_id = _artifact_id_for_block_label(block_label) if block_label else ""metadata_repair_contract = (_metadata_repair_contract(block_labels=[block_label],- required_paths=required_paths,+ required_paths=observation_paths,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,)if block_labelelse None)repair = _metadata_output_repair_context(block_labels=[block_label] if block_label else [],- required_paths=required_paths,+ required_paths=observation_paths,coverage_reason_code=reason_code,source=source,summary="Submitted workflow does not satisfy the requested output contract.",+ declaration_paths=declaration_paths,)missing_paths = sorted(set(missing_metadata_paths)@@ -2509,13 +2641,14 @@ def _evaluate_output_contract_for_code_block("block_label": block_label,"artifact_id": artifact_id,"canonical_required_child_paths": sorted(required_paths),+ "declaration_only_child_paths": sorted(declaration_paths),"source": source,"metadata_contract_source": source,"metadata_contract_reason_code": reason_code,"missing_goal_value_paths": missing_metadata_paths,"missing_extraction_schema_paths": missing_schema_paths,"missing_code_return_paths": effective_missing_return_paths,- "static_return_advisory_paths": missing_return_paths if static_return_advisory else [],+ "static_return_advisory_paths": missing_observation_return_paths if static_return_advisory else [],"post_steering_static_return_advisory": post_steering_static_return_advisory,"shape_violations": shape_violations,"can_attempt_run": run_eligible,@@ -2533,16 +2666,18 @@ def _evaluate_output_contract_for_code_block(required_paths=required_paths,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,)if block_labelelse None),- "extraction_schema": _schema_template_for_required_paths(required_paths),- "return_skeleton": _return_skeleton_for_required_paths(required_paths),+ "extraction_schema": _schema_template_for_required_paths(required_paths, declaration_paths),+ "return_skeleton": _return_skeleton_for_required_paths(required_paths, declaration_paths),},"missing_requested_output_facts": _missing_requested_output_facts(missing_paths,reason_code=reason_code,+ declaration_paths=declaration_paths,),}if _SEPARATED_SPINE_SHAPE_REQUIRED_REASON_CODE in shape_violations and block_label:@@ -2563,10 +2698,11 @@ def _evaluate_output_contract_for_code_block(repair.spine_split_blockers = list(blockers)if not block_label and signature in ctx.output_contract_output_owner_directive_candidates_by_signature:repair = _output_owner_ambiguity_repair_context(- required_paths=required_paths,+ required_paths=observation_paths,owner_labels=owner_labels,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,)payload["output_owner_directive"] = {"output_owner_candidate_labels": repair.output_owner_candidate_labels}progress_data = _code_repair_progress_data(@@ -2579,6 +2715,8 @@ def _evaluate_output_contract_for_code_block(block_label=block_label,artifact_id=artifact_id,required_paths=required_paths,+ observation_paths=observation_paths,+ declaration_paths=declaration_paths,source=source,reason_code=reason_code,missing_metadata_paths=missing_metadata_paths,@@ -2621,10 +2759,11 @@ def _adjudicate_output_contract_ladder_after_reject(ctx,blockers=blockers,target_code=target_code,- required_paths=evaluation.required_paths,+ required_paths=evaluation.observation_paths,signature=signature,current_fingerprint=current_fingerprint,advisory_run_grantable=blockers == [_OUTPUT_CONTRACT_REJECT_REASON_CODE],+ declaration_paths=evaluation.declaration_paths,)if actuation.kind == OutputContractActuationKind.BLOCKED_TERMINAL:_stash_output_source_unobservable_terminal(@@ -2778,10 +2917,13 @@ def _output_contract_reject_result(else:path_text = ", ".join(str(path) for path in data.get("canonical_required_child_paths", []) or [])path_suffix = f" Required requested output paths: {path_text}." if path_text else ""+ declaration_suffix = _declaration_repair_sentence(+ str(path) for path in data.get("declaration_only_child_paths", []) or []+ )error = (f"{tool_name} cannot proceed until the submitted workflow satisfies the requested output contract. ""Use the returned code_artifact_metadata, extraction_schema, and return skeleton templates exactly for "- "the canonical required child paths." + path_suffix+ "the canonical required child paths." + path_suffix + declaration_suffix)return {"ok": False,@@ -2794,17 +2936,17 @@ def _output_contract_reject_result(def _ensure_metadata_contract_rows(item: dict[str, Any],*,- required_paths: set[str],+ goal_value_paths: set[str],schema_text: str,) -> None:for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):rows = _artifact_mutable_rows(item.get(field_name))if not rows:- item[field_name] = [{"goal_value_paths": sorted(required_paths)}]+ item[field_name] = [{"goal_value_paths": sorted(goal_value_paths)}]rows = _artifact_mutable_rows(item.get(field_name))for row in rows:- if not _artifact_goal_value_paths(row.get("goal_value_paths")):- row["goal_value_paths"] = sorted(required_paths)+ if goal_value_paths and not _artifact_goal_value_paths(row.get("goal_value_paths")):+ row["goal_value_paths"] = sorted(goal_value_paths)if schema_text and not str(row.get("extraction_schema") or "").strip():row["extraction_schema"] = schema_text@@ -2825,8 +2967,10 @@ def _output_contract_scaffold_target_label(raw_code_artifact_metadata: object,required_paths: set[str],*,+ declaration_paths: set[str] | None = None,allow_missing_static_return: bool = False,) -> str:+ declaration_paths = declaration_paths or set()label, owner_labels = _target_output_contract_block_label(ctx,workflow_yaml,@@ -2840,7 +2984,9 @@ def _output_contract_scaffold_target_label(if target_block is None:return ""target_code = str(target_block.get("code") or "")- if not allow_missing_static_return and not required_paths <= _code_block_produced_output_paths(target_code):+ if not allow_missing_static_return and not (+ required_paths | declaration_paths+ ) <= _code_block_produced_output_paths(target_code):return ""if not allow_missing_static_return and _scout_spine_requires_separated_blocks(ctx):synthesized = synthesize_code_block(@@ -2861,17 +3007,21 @@ def _apply_metadata_contract_scaffold(required_paths: set[str],source: str,reason_code: str,+ declaration_paths: set[str] | None = None,allow_missing_static_return: bool = False,) -> object:+ declaration_paths = declaration_paths or set()+ union_paths = required_paths | declaration_pathsif _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:return raw_code_artifact_metadata- if not required_paths:+ if not union_paths:return raw_code_artifact_metadatalabel = _output_contract_scaffold_target_label(ctx,workflow_yaml,raw_code_artifact_metadata,required_paths,+ declaration_paths=declaration_paths,allow_missing_static_return=allow_missing_static_return,)if not label:@@ -2879,20 +3029,20 @@ def _apply_metadata_contract_scaffold(existing_item = _metadata_item_for_block_label(raw_code_artifact_metadata, label)prior_item = _metadata_item_for_block_label(getattr(ctx, "code_artifact_metadata", None), label)if _metadata_item_declares_extraction_schema(existing_item) and not _metadata_item_effective_schema_text(- existing_item, required_paths+ existing_item, union_paths):return raw_code_artifact_metadataif _metadata_item_declares_extraction_schema(prior_item) and not _metadata_item_effective_schema_text(- prior_item, required_paths+ prior_item, union_paths):return raw_code_artifact_metadataschema_text = (- _metadata_item_effective_schema_text(existing_item, required_paths)+ _metadata_item_effective_schema_text(existing_item, union_paths)or _metadata_item_effective_schema_text(prior_item,- required_paths,+ union_paths,)- or _schema_template_text_for_required_paths(required_paths)+ or _schema_template_text_for_required_paths(union_paths, declaration_paths))items = [copy.deepcopy(item)@@ -2913,7 +3063,7 @@ def _apply_metadata_contract_scaffold(items[target_index] = targettarget["block_label"] = labeltarget["artifact_id"] = _artifact_id_for_block_label(label)- _ensure_metadata_contract_rows(target, required_paths=required_paths, schema_text=schema_text)+ _ensure_metadata_contract_rows(target, goal_value_paths=required_paths, schema_text=schema_text)return items@@ -2922,23 +3072,24 @@ def _scaffold_metadata_contract_for_update(workflow_yaml: str,raw_code_artifact_metadata: object,) -> tuple[object, bool]:- required_paths, source, reason_code = _output_contract_required_paths_source(ctx)- if not required_paths:+ contract = _output_contract_required_paths_source(ctx)+ if not contract.union:return raw_code_artifact_metadata, Falsesignature = _output_contract_signature(ctx=ctx,workflow_yaml=workflow_yaml,- source=source,- reason_code=reason_code,- required_paths=required_paths,+ source=contract.source,+ reason_code=contract.reason_code,+ required_paths=contract.union,)scaffolded = _apply_metadata_contract_scaffold(ctx,workflow_yaml,raw_code_artifact_metadata,- required_paths=required_paths,- source=source,- reason_code=reason_code,+ required_paths=contract.observation_paths,+ source=contract.source,+ reason_code=contract.reason_code,+ declaration_paths=contract.declaration_paths,allow_missing_static_return=_output_contract_advisory_granted(ctx, signature)or _output_contract_reject_count(ctx, signature) >= _MAX_OUTPUT_CONTRACT_STEERING_REJECTS,)@@ -2952,14 +3103,15 @@ def _apply_metadata_contract_schema_to_workflow_yaml() -> str:if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:return workflow_yaml- required_paths, source, reason_code = _output_contract_required_paths_source(ctx)+ contract = _output_contract_required_paths_source(ctx)+ required_paths = contract.unionif not required_paths:return workflow_yamllabel, owner_labels = _target_output_contract_block_label(ctx,workflow_yaml,raw_code_artifact_metadata,- required_paths,+ contract.observation_paths,)if not label or owner_labels != [label]:return workflow_yaml@@ -2985,8 +3137,8 @@ def _apply_metadata_contract_schema_to_workflow_yaml("copilot_output_contract_schema_projected_to_workflow",block_label=label,canonical_required_child_paths=sorted(required_paths),- source=source,- reason_code=reason_code,+ source=contract.source,+ reason_code=contract.reason_code,)return yaml.safe_dump(parsed, sort_keys=False)@@ -3101,6 +3253,7 @@ def _attempt_separated_spine_split(target_code: str,synthesized: SynthesizedCodeBlock,required_paths: set[str],+ declaration_paths: set[str] | None = None,) -> _SpineSplitOutcome:code_block = next((block for block in _workflow_code_blocks(parsed) if str(block.get("label") or "").strip() == label),@@ -3130,7 +3283,7 @@ def _attempt_separated_spine_split(return _SpineSplitOutcome(None, ["extraction_suffix_contains_browser_actions"], None)keyed_extraction, static_violations = _extraction_code_with_required_static_return(- extraction_suffix, required_paths=required_paths+ extraction_suffix, required_paths=required_paths, declaration_paths=declaration_paths)if static_violations:return _SpineSplitOutcome(None, ["static_return_envelope_unavailable"], None)@@ -3206,17 +3359,19 @@ def _output_owner_ambiguity_repair_context(owner_labels: list[str],source: str,reason_code: str,+ declaration_paths: set[str] | None = None,) -> CodeAuthoringRepairContext:- paths = sorted(dict.fromkeys(str(path).strip() for path in required_paths if str(path).strip()))+ goal_paths = _normalized_repair_paths(required_paths)+ union_paths = sorted(dict.fromkeys([*goal_paths, *_normalized_repair_paths(declaration_paths or set())]))candidates = sorted(dict.fromkeys(str(label).strip() for label in owner_labels if str(label).strip()))- path_text = ", ".join(paths)+ path_text = ", ".join(union_paths)return CodeAuthoringRepairContext(block_label="",reason_code=OUTPUT_OWNER_AMBIGUITY_REASON_CODE,runtime_failure_class=reason_code,- required_goal_value_paths=paths,- required_extraction_schema_paths=paths,- required_code_return_paths=paths,+ required_goal_value_paths=goal_paths,+ required_extraction_schema_paths=union_paths,+ required_code_return_paths=union_paths,metadata_contract_source=source,metadata_contract_reason_code=reason_code,output_owner_candidate_labels=candidates,@@ -3258,11 +3413,11 @@ def _code_reads_page_value(code: str) -> bool:return False-def _output_contract_click_only_spine(target_code: str) -> bool:+def _output_contract_click_only_spine(target_code: str, declaration_paths: set[str] | None = None) -> bool:mutations, _, ambiguous = _browser_surface_for_code(target_code)if not (mutations or ambiguous):return False- if _code_block_produced_output_paths(target_code):+ if _code_block_produced_output_paths(target_code) - _declaration_envelope_paths(declaration_paths or set()):return Falsereturn not _code_reads_page_value(target_code)@@ -3574,8 +3729,9 @@ def _actuate_output_contract_bail(signature: str,current_fingerprint: str,advisory_run_grantable: bool = False,+ declaration_paths: set[str] | None = None,) -> OutputContractActuation:- click_only_spine = _output_contract_click_only_spine(target_code)+ click_only_spine = _output_contract_click_only_spine(target_code, declaration_paths)observed_required_values = _observed_required_output_values(ctx, required_paths)if not click_only_spine or observed_required_values:_clear_declick_attempt(ctx, signature)@@ -3631,13 +3787,144 @@ def _actuate_output_contract_bail(return actuation+def _merge_declaration_children_into_literal_returns(code: str, declaration_paths: set[str]) -> str:+ if {_output_path_root(path) for path in declaration_paths} != {"output"}:+ return ""+ declaration_children = sorted(+ {+ child+ for path in declaration_paths+ if (child := _output_path_direct_child(path, "output")) and _return_scaffold_name_is_safe(child)+ }+ )+ if not declaration_children:+ return ""+ try:+ tree = ast.parse(code)+ except SyntaxError:+ return ""+ lines = code.splitlines()+ insertions: list[tuple[int, int, str]] = []+ for node in _iter_top_level_scope(tree.body):+ if not isinstance(node, ast.Return) or not isinstance(node.value, ast.Dict):+ continue+ output_value: ast.expr | None = None+ for key, value in zip(node.value.keys, node.value.values):+ if isinstance(key, ast.Constant) and key.value == "output":+ output_value = value+ break+ if output_value is None:+ pairs = ", ".join(f'"{name}": None' for name in declaration_children)+ insert_text = f'"output": {{{pairs}}}'+ target = node.value+ elif isinstance(output_value, ast.Dict):+ existing_keys = {+ key.value for key in output_value.keys if isinstance(key, ast.Constant) and isinstance(key.value, str)+ }+ missing_children = [name for name in declaration_children if name not in existing_keys]+ if not missing_children:+ continue+ insert_text = ", ".join(f'"{name}": None' for name in missing_children)+ target = output_value+ else:+ return ""+ # Front insertion keeps any later literal or unpacked entries authoritative over the+ # stamped None defaults.+ suffix = ", " if (target.keys or target.values) else ""+ insertions.append((target.lineno - 1, target.col_offset + 1, insert_text + suffix))+ if not insertions:+ return ""+ for line_index, col, text in sorted(insertions, reverse=True):+ if line_index < 0 or line_index >= len(lines) or col > len(lines[line_index]):+ return ""+ lines[line_index] = lines[line_index][:col] + text + lines[line_index][col:]+ return "\n".join(lines)+++def _code_with_declared_contract_defaults(code: str, declaration_paths: set[str]) -> str:+ stripped_code = textwrap.dedent(code).strip()+ if not stripped_code or not declaration_paths:+ return ""+ if declaration_paths <= _code_block_produced_output_paths(stripped_code):+ return ""+ merged = _merge_declaration_children_into_literal_returns(stripped_code, declaration_paths)+ if merged and declaration_paths <= _code_block_produced_output_paths(merged):+ return merged+ try:+ tree = ast.parse(stripped_code)+ except SyntaxError:+ return ""+ if any(isinstance(node, ast.Return) for node in _iter_top_level_scope(tree.body)):+ return ""+ appended, violations = _extraction_code_with_required_static_return(+ stripped_code,+ required_paths=set(),+ declaration_paths=declaration_paths,+ )+ if violations or appended == stripped_code:+ return ""+ if declaration_paths <= _code_block_produced_output_paths(appended):+ return appended+ return ""+++def _stamp_declaration_contract_defaults(+ ctx: AgentContext,+ workflow_yaml: str,+ raw_code_artifact_metadata: object,+ contract: _OutputContractRequiredPaths,+ signature: str,+) -> tuple[str, bool]:+ if not contract.declaration_paths:+ return workflow_yaml, False+ label, owner_labels = _target_output_contract_block_label(+ ctx,+ workflow_yaml,+ raw_code_artifact_metadata,+ contract.observation_paths,+ )+ if not label or owner_labels != [label]:+ return workflow_yaml, False+ target_block = _workflow_yaml_code_blocks_by_label(workflow_yaml).get(label)+ if target_block is None:+ return workflow_yaml, False+ stamped_code = _code_with_declared_contract_defaults(+ str(target_block.get("code") or ""), contract.declaration_paths+ )+ if not stamped_code:+ return workflow_yaml, False+ parsed = parse_workflow_yaml(workflow_yaml)+ if not isinstance(parsed, dict):+ return workflow_yaml, False+ applied = False+ for block in _workflow_code_blocks(parsed):+ if str(block.get("label") or "").strip() == label:+ block["code"] = stamped_code.rstrip() + "\n"+ applied = True+ break+ if not applied:+ return workflow_yaml, False+ LOG.info(+ "copilot_output_contract_declaration_stamped",+ block_label=label,+ declaration_only_child_paths=sorted(contract.declaration_paths),+ canonical_output_contract_signature=signature,+ )+ return yaml.safe_dump(parsed, sort_keys=False), True++def _impose_output_contract_envelope_after_steering(ctx: AgentContext,workflow_yaml: str,raw_code_artifact_metadata: object,) -> tuple[str, object, bool]:ctx.output_contract_bail_actuated_this_call = False- required_paths, source, reason_code = _output_contract_required_paths_source(ctx)+ contract = _output_contract_required_paths_source(ctx)+ required_paths = contract.union+ observation_paths = contract.observation_paths+ declaration_paths = contract.declaration_paths+ source = contract.source+ reason_code = contract.reason_codeif not required_paths:return workflow_yaml, raw_code_artifact_metadata, Falsesignature = _output_contract_signature(@@ -3647,28 +3934,37 @@ def _impose_output_contract_envelope_after_steering(reason_code=reason_code,required_paths=required_paths,)+ # The stamp precedes the steering-reject wait and ignores advisory state so no+ # acceptance route can persist a block that omits the declaration paths.+ workflow_yaml, declaration_stamped = _stamp_declaration_contract_defaults(+ ctx,+ workflow_yaml,+ raw_code_artifact_metadata,+ contract,+ signature,+ )_reopen_dispatch_lacked_bound_extraction(ctx, signature)runtime_attempt_key = _runtime_output_repair_attempt_key(ctx, workflow_yaml, required_paths, source, reason_code)if _output_contract_reject_count(ctx, signature) < _MAX_OUTPUT_CONTRACT_STEERING_REJECTS and _runtime_output_repair_attempt_recorded(ctx, runtime_attempt_key):- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedlabel, owner_labels = _target_output_contract_block_label(ctx,workflow_yaml,raw_code_artifact_metadata,- required_paths,+ observation_paths,)if not label or owner_labels != [label]:_arm_output_contract_output_owner_directive(ctx, signature=signature, owner_labels=owner_labels)- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedparsed = parse_workflow_yaml(workflow_yaml)if not isinstance(parsed, dict):- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedcode_blocks = _workflow_yaml_code_blocks_by_label(workflow_yaml)target_block = code_blocks.get(label)if target_block is None:- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedtarget_code = str(target_block.get("code") or "")current_fingerprint = _output_contract_structural_fingerprint(workflow_yaml, signature)if _scout_spine_requires_separated_blocks(ctx):@@ -3685,15 +3981,17 @@ def _impose_output_contract_envelope_after_steering(target_code=target_code,synthesized=synthesized,required_paths=required_paths,+ declaration_paths=declaration_paths,)if split.imposed_yaml is not None:scaffolded_metadata = _apply_metadata_contract_scaffold(ctx,split.imposed_yaml,raw_code_artifact_metadata,- required_paths=required_paths,+ required_paths=observation_paths,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,)_mark_output_contract_imposed(ctx, signature)_record_runtime_output_repair_attempt(ctx, runtime_attempt_key)@@ -3708,10 +4006,11 @@ def _impose_output_contract_envelope_after_steering(ctx,blockers=split.blockers,target_code=target_code,- required_paths=required_paths,+ required_paths=observation_paths,signature=signature,current_fingerprint=current_fingerprint,advisory_run_grantable=True,+ declaration_paths=declaration_paths,)if actuation.kind == OutputContractActuationKind.BLOCKED_TERMINAL:_stash_output_source_unobservable_terminal(@@ -3722,11 +4021,11 @@ def _impose_output_contract_envelope_after_steering(signature=signature,blockers=split.blockers,)- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedif actuation.kind == OutputContractActuationKind.ADVISORY_RUN:- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedif copilot_author_time_gate_log_only_enabled():- return workflow_yaml, raw_code_artifact_metadata, False+ return workflow_yaml, raw_code_artifact_metadata, declaration_stampedattempt_key = _output_contract_spine_directive_attempt_key(signature=signature, block_label=label, workflow_yaml=workflow_yaml)@@ -3739,16 +4038,21 @@ def _impose_output_contract_envelope_after_steering(signature=signature,)_record_armed_directive_fingerprint(ctx, signature, current_fingerprint)- return workflow_yaml, raw_code_artifact_metadata, False- keyed_code, violations = _extraction_code_with_required_static_return(target_code, required_paths=required_paths)+ return workflow_yaml, raw_code_artifact_metadata, declaration_stamped+ keyed_code, violations = _extraction_code_with_required_static_return(+ target_code,+ required_paths=required_paths,+ declaration_paths=declaration_paths,+ )if violations:scaffolded_metadata = _apply_metadata_contract_scaffold(ctx,workflow_yaml,raw_code_artifact_metadata,- required_paths=required_paths,+ required_paths=observation_paths,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,allow_missing_static_return=True,)scaffolded = scaffolded_metadata is not raw_code_artifact_metadata@@ -3773,9 +4077,10 @@ def _impose_output_contract_envelope_after_steering(ctx,blockers=["static_return_envelope_unavailable"],target_code=target_code,- required_paths=required_paths,+ required_paths=observation_paths,signature=signature,current_fingerprint=current_fingerprint,+ declaration_paths=declaration_paths,)if actuation.kind == OutputContractActuationKind.BLOCKED_TERMINAL:_stash_output_source_unobservable_terminal(@@ -3786,7 +4091,7 @@ def _impose_output_contract_envelope_after_steering(signature=signature,blockers=["static_return_envelope_unavailable"],)- return workflow_yaml, scaffolded_metadata, scaffolded+ return workflow_yaml, scaffolded_metadata, scaffolded or declaration_stampedchanged_code = keyed_code != textwrap.dedent(target_code).strip()if changed_code:for block in _workflow_code_blocks(parsed):@@ -3798,9 +4103,10 @@ def _impose_output_contract_envelope_after_steering(ctx,workflow_yaml,raw_code_artifact_metadata,- required_paths=required_paths,+ required_paths=observation_paths,source=source,reason_code=reason_code,+ declaration_paths=declaration_paths,)scaffolded = scaffolded_metadata is not raw_code_artifact_metadataapplied = changed_code or scaffolded@@ -3815,7 +4121,7 @@ def _impose_output_contract_envelope_after_steering(return_envelope_applied=changed_code,metadata_scaffold_applied=scaffolded,)- return workflow_yaml, scaffolded_metadata, applied+ return workflow_yaml, scaffolded_metadata, applied or declaration_stampeddef _metadata_contract_run_preflight_reject(@@ -3902,7 +4208,7 @@ def _metadata_contract_run_preflight_reject(workflow_yaml=workflow_yaml,)if advisory_granted:- _arm_pending_run_evidence(ctx, evaluation.canonical_signature, set(evaluation.required_paths))+ _arm_pending_run_evidence(ctx, evaluation.canonical_signature, set(evaluation.observation_paths))return Noneif evaluation.repair_context is not None:ctx.last_code_authoring_repair_context = evaluation.repair_context@@ -4135,7 +4441,10 @@ def _extraction_code_with_required_static_return(code: str,*,required_paths: set[str],+ declaration_paths: set[str] | None = None,) -> tuple[str, list[str]]:+ declaration_paths = declaration_paths or set()+ required_paths = required_paths | declaration_pathsstripped_code = textwrap.dedent(code).strip()if not stripped_code or not required_paths:return stripped_code, []@@ -4145,8 +4454,13 @@ def _extraction_code_with_required_static_return(tree = ast.parse(stripped_code)except SyntaxError:return stripped_code, []+ declaration_children = {+ child+ for path in declaration_paths+ if (child := _output_path_direct_child(path, "output")) and _return_scaffold_name_is_safe(child)+ }if any(isinstance(node, ast.Return) for node in _iter_top_level_scope(tree.body)):- replacement = _replace_direct_child_local_return(stripped_code, tree, required_paths)+ replacement = _replace_direct_child_local_return(stripped_code, tree, required_paths, declaration_children)if replacement and required_paths <= _code_block_produced_output_paths(replacement):return replacement, []missing = sorted(required_paths - _code_block_produced_output_paths(stripped_code))@@ -4170,7 +4484,7 @@ def _extraction_code_with_required_static_return(for path in required_pathsif (child := _output_path_direct_child(path, "output"))and _return_scaffold_name_is_safe(child)- and child in assigned_names+ and (child in assigned_names or child in declaration_children)})required_child_names = {@@ -4179,7 +4493,12 @@ def _extraction_code_with_required_static_return(if (child := _output_path_direct_child(path, "output")) and _return_scaffold_name_is_safe(child)}if child_names and set(child_names) == required_child_names:- child_pairs = ", ".join(f'"{name}": {name}' for name in child_names)+ child_pairs = ", ".join(+ f'"{name}": None'+ if name in declaration_children and name not in assigned_names+ else f'"{name}": {name}'+ for name in child_names+ )candidate = stripped_code + f'\nreturn {{"output": {{{child_pairs}}}}}'if not candidate and len(roots) == 1:candidate = _single_mapping_local_static_return_candidate(stripped_code, next(iter(roots)), required_paths)@@ -4227,7 +4546,13 @@ def _single_mapping_local_static_return_candidate(code: str, root: str, requiredreturn code + f'\nreturn {{"{root}": {mapping_local}}}'-def _replace_direct_child_local_return(code: str, tree: ast.Module, required_paths: set[str]) -> str:+def _replace_direct_child_local_return(+ code: str,+ tree: ast.Module,+ required_paths: set[str],+ declaration_children: set[str] | None = None,+) -> str:+ declaration_children = declaration_children or set()roots = {_output_path_root(path) for path in required_paths if _output_path_root(path)}if roots != {"output"}:return ""@@ -4238,9 +4563,10 @@ def _replace_direct_child_local_return(code: str, tree: ast.Module, required_patif (child := _output_path_direct_child(path, "output")) and _return_scaffold_name_is_safe(child)})- if len(child_names) != 1:+ local_child_names = [child for child in child_names if child not in declaration_children]+ if len(local_child_names) != 1:return ""- child_name = child_names[0]+ child_name = local_child_names[0]returns = [node for node in tree.body if isinstance(node, ast.Return)]if len(returns) != 1:return ""@@ -4256,7 +4582,10 @@ def _replace_direct_child_local_return(code: str, tree: ast.Module, required_patif indent_match is None:return ""indent = indent_match.group(0)- replacement = f'{indent}return {{"output": {{"{child_name}": {child_name}}}}}'+ pairs = ", ".join(+ f'"{name}": None' if name in declaration_children else f'"{name}": {name}' for name in child_names+ )+ replacement = f'{indent}return {{"output": {{{pairs}}}}}'return "\n".join([*lines[: return_node.lineno - 1],@@ -6396,7 +6725,7 @@ def _selector_join_parameter_alias_by_authored_fill(def _synthesized_parameter_binding_reject_count(ctx: AgentContext | None) -> int:if ctx is None:return 0- required_paths, _, _ = _output_contract_required_paths_source(ctx)+ required_paths = _output_contract_required_paths_source(ctx).unionif required_paths:contract_count = _output_contract_reject_count(ctx,@@ -8194,7 +8523,7 @@ async def _update_workflow(imposition.repair_context is not Noneand imposition.repair_context.reason_code == _SYNTHESIZED_PARAMETER_BINDING_AMBIGUOUS_REASON_CODE):- required_paths, _, _ = _output_contract_required_paths_source(ctx)+ required_paths = _output_contract_required_paths_source(ctx).union_record_output_contract_family_reject(ctx,required_paths,diff --git a/tests/unit/copilot_test_helpers.py b/tests/unit/copilot_test_helpers.pyindex 32937f65e..ed65a2849 100644--- a/tests/unit/copilot_test_helpers.py+++ b/tests/unit/copilot_test_helpers.py@@ -178,6 +178,7 @@ def make_completion_criterion(classification_output_key: str | None = None,expected_classification: str | bool | None = None,requested_output_corroborator: bool = False,+ mint_degrade: str | None = None,) -> CompletionCriterion:return CompletionCriterion(id=cid,@@ -196,4 +197,5 @@ def make_completion_criterion(classification_output_key=classification_output_key,expected_classification=expected_classification,requested_output_corroborator=requested_output_corroborator,+ mint_degrade=mint_degrade, # type: ignore[arg-type])diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.pyindex 803c04e62..2c48b6176 100644--- a/tests/unit/test_copilot_code_block_persist_seam.py+++ b/tests/unit/test_copilot_code_block_persist_seam.py@@ -4126,7 +4126,7 @@ class TestCodeRepairProgressClassification:judgment_only_ctx = _code_only_ctx()judgment_only_ctx.request_policy = RequestPolicy(completion_criteria=[judgment_criterion])- judgment_only_paths, _, _ = workflow_update_module._output_contract_required_paths_source(judgment_only_ctx)+ judgment_only_paths = workflow_update_module._output_contract_required_paths_source(judgment_only_ctx).unionassert judgment_only_paths == set()@@ -4142,7 +4142,8 @@ class TestCodeRepairProgressClassification:])- required_paths, source, reason_code = workflow_update_module._output_contract_required_paths_source(ctx)+ contract = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths, source, reason_code = contract.union, contract.source, contract.reason_codeassert required_paths == {"output.record_id"}assert source == "requested_output_contract"@@ -4163,7 +4164,7 @@ class TestCodeRepairProgressClassification:])- required_paths, _, _ = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths = workflow_update_module._output_contract_required_paths_source(ctx).unionassert required_paths == set()@@ -4194,7 +4195,7 @@ class TestCodeRepairProgressClassification:metadata_contract_reason_code="requested_output_contract_missing_output_coverage",)- required_paths, _, _ = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths = workflow_update_module._output_contract_required_paths_source(ctx).unionresult = workflow_update_module._metadata_contract_run_preflight_reject(ctx, _SAFE_CODE_YAML, [])assert required_paths == set()@@ -4227,7 +4228,8 @@ class TestCodeRepairProgressClassification:metadata_contract_reason_code="requested_output_contract_missing_output_coverage",)- required_paths, source, reason_code = workflow_update_module._output_contract_required_paths_source(ctx)+ contract = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths, source, reason_code = contract.union, contract.source, contract.reason_codeassert required_paths == {"output.record_id"}assert source == "requested_output_contract"@@ -4268,7 +4270,7 @@ class TestCodeRepairProgressClassification:],)- required_paths, _, _ = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths = workflow_update_module._output_contract_required_paths_source(ctx).unionassert required_paths == set()@@ -4322,7 +4324,8 @@ class TestCodeRepairProgressClassification:],)- required_paths, source, reason_code = workflow_update_module._output_contract_required_paths_source(ctx)+ contract = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths, source, reason_code = contract.union, contract.source, contract.reason_codeassert required_paths == {"output.record_id"}assert source == "runtime_output_repair"@@ -4470,7 +4473,8 @@ class TestCodeRepairProgressClassification:ctx = _code_only_ctx()ctx.latest_recorded_build_test_outcome = outcome- required_paths, source, reason_code = workflow_update_module._output_contract_required_paths_source(ctx)+ contract = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths, source, reason_code = contract.union, contract.source, contract.reason_codeassert required_paths == {"output.public_form_exists","output.recommended_next_action",@@ -6951,6 +6955,8 @@ class TestSeparatedSpineViolationActuation:block_label="extract_record",artifact_id="art-1",required_paths={"output.confirmation_number"},+ observation_paths={"output.confirmation_number"},+ declaration_paths=set(),source="requested_output_contract",reason_code="requested_output_contract_missing_output_coverage",missing_metadata_paths=[],@@ -7220,7 +7226,8 @@ class TestSeparatedSpineImpositionRunEligibility:scaffolded_metadata, _applied = workflow_update_module._scaffold_metadata_contract_for_update(ctx, workflow_yaml, [])- required_paths, source, reason_code = workflow_update_module._output_contract_required_paths_source(ctx)+ contract = workflow_update_module._output_contract_required_paths_source(ctx)+ required_paths, source, reason_code = contract.union, contract.source, contract.reason_coderead_label, _owner_labels = workflow_update_module._target_output_contract_block_label(ctx, workflow_yaml, scaffolded_metadata, required_paths)@@ -12599,3 +12606,96 @@ def _auto_act_scout_ctx() -> AgentContext:class _AutoActClickServer:async def call_internal_tool(self, tool_name: str, args: dict[str, object]) -> dict[str, object]:return {"ok": True, "data": {"selector": args.get("selector")}}+++def _declaration_stamp_ctx() -> CopilotContext:+ ctx = _code_only_ctx()+ ctx.turn_id = "t-decl"+ ctx.scout_trajectory = []+ ctx.request_policy = RequestPolicy(+ completion_criteria=[+ CompletionCriterion(+ id="c_record",+ outcome="The returned record includes record id.",+ output_path="output.record_id",+ ),+ CompletionCriterion(+ id="c_blocker",+ outcome="A blocker is reported when the site blocks submission.",+ contingent_on="the site blocks submission",+ contingent_antecedent_output_path="output.blocker",+ ),+ ]+ )+ return ctx+++class TestDeclarationContractStamp:+ def test_zero_output_block_persists_declared_blocker_at_ceiling(self) -> None:+ ctx = _declaration_stamp_ctx()+ signature = workflow_update_module._stable_output_contract_key(+ "turn:t-decl", {"output.record_id", "output.blocker"}+ )+ ctx.output_contract_reject_count_by_signature = {+ signature: workflow_update_module._MAX_OUTPUT_CONTRACT_STEERING_REJECTS+ }+ workflow_yaml = _collapsed_spine_yaml('await page.click("#submit")')++ new_yaml, _metadata, applied = workflow_update_module._impose_output_contract_envelope_after_steering(+ ctx, workflow_yaml, []+ )++ assert applied is True+ code = str(workflow_blocks(parse_workflow_yaml(new_yaml))[0].get("code") or "")+ produced = workflow_update_module._code_block_produced_output_paths(code)+ assert "output.blocker" in produced # nosemgrep: incomplete-url-substring-sanitization+ assert "output.record_id" not in produced+ assert '"blocker": None' in code++ def test_stamp_applies_before_steering_reject_wait(self) -> None:+ ctx = _declaration_stamp_ctx()+ workflow_yaml = _collapsed_spine_yaml('await page.click("#submit")\nreturn {}')++ new_yaml, _metadata, applied = workflow_update_module._impose_output_contract_envelope_after_steering(+ ctx, workflow_yaml, []+ )++ assert applied is True+ code = str(workflow_blocks(parse_workflow_yaml(new_yaml))[0].get("code") or "")+ assert 'return {"output": {"blocker": None}}' in code++ def test_stamp_is_idempotent_across_calls(self) -> None:+ ctx = _declaration_stamp_ctx()+ workflow_yaml = _collapsed_spine_yaml('await page.click("#submit")')++ first_yaml, _metadata, _applied = workflow_update_module._impose_output_contract_envelope_after_steering(+ ctx, workflow_yaml, []+ )+ again_yaml, _metadata2, _applied2 = workflow_update_module._impose_output_contract_envelope_after_steering(+ ctx, first_yaml, []+ )++ assert again_yaml == first_yaml++ def test_stamp_skips_without_single_owner_block(self) -> None:+ ctx = _declaration_stamp_ctx()+ workflow_yaml = _yaml(+ "title: Two blocks\n"+ "workflow_definition:\n"+ " blocks:\n"+ " - block_type: code\n"+ " label: first\n"+ " code: |\n"+ ' await page.click("#a")\n'+ " - block_type: code\n"+ " label: second\n"+ " code: |\n"+ ' await page.click("#b")\n'+ )++ new_yaml, _metadata, applied = workflow_update_module._impose_output_contract_envelope_after_steering(+ ctx, workflow_yaml, []+ )++ assert applied is False+ assert new_yaml == workflow_yamldiff --git a/tests/unit/test_copilot_completion_criteria_lifecycle.py b/tests/unit/test_copilot_completion_criteria_lifecycle.pyindex 195c7fb01..cc22ec0d3 100644--- a/tests/unit/test_copilot_completion_criteria_lifecycle.py+++ b/tests/unit/test_copilot_completion_criteria_lifecycle.py@@ -285,12 +285,75 @@ def test_reconcile_no_criteria_anywhere_is_noop() -> None:),id="deliverable-kind",),+ pytest.param(+ (+ _criterion(+ "c0",+ "A blocker is reported to the user.",+ contingent_on="the site blocks submission",+ mint_degrade="contingent_missing_antecedent",+ ),+ ),+ id="contingent-mint-degrade",+ ),],)def test_criteria_json_round_trip_preserves_fields(criteria: tuple[CompletionCriterion, ...]) -> None:assert criteria_from_json(criteria_to_json(criteria)) == criteria+def test_criteria_from_json_degrades_stored_pathless_contingent() -> None:+ (criterion,) = criteria_from_json(+ [+ {+ "id": "c0",+ "outcome": "A blocker is reported to the user.",+ "contingent_on": "the site blocks submission",+ }+ ]+ )+ assert criterion.mint_degrade == "contingent_missing_antecedent"+++def test_criteria_from_json_keeps_wellformed_contingent_undegraded() -> None:+ (criterion,) = criteria_from_json(+ [+ {+ "id": "c0",+ "outcome": "A blocker is reported to the user.",+ "contingent_on": "the site blocks submission",+ "contingent_antecedent_output_path": "output.blocker",+ }+ ]+ )+ assert criterion.mint_degrade is None+++def test_criteria_from_json_coerces_unknown_mint_degrade_to_none() -> None:+ (criterion,) = criteria_from_json([{"id": "c0", "outcome": "done", "mint_degrade": "bogus"}])+ assert criterion.mint_degrade is None+++def test_adopt_stored_rehydrates_pathless_contingent_as_degraded() -> None:+ stored_criteria = criteria_from_json(+ [+ {"id": "c0", "outcome": "The request is submitted."},+ {+ "id": "c1",+ "outcome": "A blocker is reported to the user.",+ "contingent_on": "the site blocks submission",+ },+ ]+ )+ stored = StoredCriteriaSet(set_id="set_1", goal_epoch=1, criteria=stored_criteria)++ decision = reconcile_completion_criteria(StoredCriteriaSnapshot(active=stored, next_epoch=2), [], actionable=True)++ assert decision.action == "adopt_stored"+ degrades = {criterion.id: criterion.mint_degrade for criterion in decision.criteria}+ assert degrades == {"c0": None, "c1": "contingent_missing_antecedent"}++def test_criteria_from_json_normalizes_unknown_deliverable_kind() -> None:(criterion,) = criteria_from_json([diff --git a/tests/unit/test_copilot_output_contract_actuation.py b/tests/unit/test_copilot_output_contract_actuation.pyindex b71163e2f..c5ec5de65 100644--- a/tests/unit/test_copilot_output_contract_actuation.py+++ b/tests/unit/test_copilot_output_contract_actuation.py@@ -8,15 +8,19 @@ OSS-synced: only synthetic labels and RFC-2606 placeholder identifiers.from __future__ import annotationsimport itertools+import jsonfrom types import SimpleNamespaceimport pytestfrom skyvern.config import settings+from skyvern.forge.sdk.copilot import enforcementfrom skyvern.forge.sdk.copilot.blocker_signal import (assert_clean_user_facing_text,build_output_source_unobservable_blocker_signal,)+from skyvern.forge.sdk.copilot.build_test_outcome import RecordedBuildTestOutcome+from skyvern.forge.sdk.copilot.context import CodeAuthoringRepairContext, CopilotContextfrom skyvern.forge.sdk.copilot.output_contracts import (OUTPUT_CONTRACT_ACTUATION_EXHAUSTED_REASON_CODE,OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODE,@@ -28,12 +32,14 @@ from skyvern.forge.sdk.copilot.output_contracts import (classify_output_contract_bail_family,resolve_output_contract_actuation,)+from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, RequestPolicyfrom skyvern.forge.sdk.copilot.runtime import (output_contract_ladder_unresolved,record_author_time_gate_ablation_event,)from skyvern.forge.sdk.copilot.tools import workflow_update as wufrom skyvern.forge.sdk.copilot.turn_halt import TurnHaltKind, turn_halt_from_blocker_signal+from tests.unit.copilot_test_helpers import make_copilot_ctx_ALL_SPLIT_BLOCKERS = ["static_return_envelope_unavailable",@@ -1020,6 +1026,8 @@ def _make_evaluation(signature: str, *, block_label: str = "", shape_violations:block_label=block_label,artifact_id="",required_paths={"output.confirmation_number"},+ observation_paths={"output.confirmation_number"},+ declaration_paths=set(),source="requested_output",reason_code="output_contract_required",missing_metadata_paths=[],@@ -1358,3 +1366,509 @@ def test_advisory_grant_downgraded_to_directive_when_run_authority_forbids_dispa)assert actuation.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVEassert signature not in ctx.output_contract_actuation_by_signature+++def _antecedent_ctx(*criteria: CompletionCriterion) -> CopilotContext:+ ctx = make_copilot_ctx()+ ctx.block_authoring_policy = wu.BlockAuthoringPolicy.CODE_ONLY_BROWSER+ ctx.request_policy = RequestPolicy(completion_criteria=list(criteria))+ return ctx+++def _blocker_contingent_criterion() -> CompletionCriterion:+ return CompletionCriterion(+ id="c_blocker",+ outcome="A blocker is reported when the site blocks submission.",+ contingent_on="the site blocks submission",+ contingent_antecedent_output_path="output.blocker",+ )+++def test_contingent_antecedent_joins_declaration_lane_schema_and_skeleton() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == {"output.record_id"}+ assert contract.declaration_paths == {"output.blocker"}+ assert contract.union == {"output.record_id", "output.blocker"}+ assert contract.source == "requested_output_contract"+ assert contract.reason_code == "requested_output_contract_missing_output_coverage"+ schema = wu._schema_template_for_required_paths(contract.union, contract.declaration_paths)+ output_properties = schema["properties"]["output"]+ assert output_properties["properties"]["blocker"] == {"type": ["string", "null"]}+ assert output_properties["properties"]["record_id"] == {}+ assert set(output_properties["required"]) == {"record_id", "blocker"}+ skeleton = wu._return_skeleton_for_required_paths(contract.union, contract.declaration_paths)+ assert skeleton == 'return {"output": {"blocker": None, "record_id": record_id}}'+++def test_contingent_antecedent_alone_forms_declaration_only_contract() -> None:+ ctx = _antecedent_ctx(_blocker_contingent_criterion())++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == set()+ assert contract.declaration_paths == {"output.blocker"}+ assert contract.union == {"output.blocker"}+++def test_antecedent_overlapping_requested_output_stays_observation() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ CompletionCriterion(+ id="c_overlap",+ outcome="The record id is reported when lookup is blocked.",+ contingent_on="the lookup is blocked",+ contingent_antecedent_output_path="output.record_id",+ ),+ )++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == {"output.record_id"}+ assert contract.declaration_paths == set()+++def test_judgment_and_classification_criteria_antecedents_still_contribute() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_judgment",+ outcome="The run reports whether a login gate blocks the target.",+ output_path="output.login_gate_blocks_target",+ expected_output_shape="goal_judgment_boolean",+ requested_output_evidence_source="independent_run_evidence",+ contingent_on="a login gate blocks the target",+ contingent_antecedent_output_path="output.login_blocker",+ ),+ CompletionCriterion(+ id="c_classify",+ outcome="The run classifies the reached path.",+ kind="validation_classification",+ classification_output_key="path_classification",+ expected_classification="login_gated",+ contingent_on="the flow is blocked",+ contingent_antecedent_output_path="output.flow_blocker",+ ),+ )++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == set()+ assert contract.declaration_paths == {"output.login_blocker", "output.flow_blocker"}+++def test_definition_level_antecedent_does_not_contribute() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_definition",+ outcome="The workflow definition names a blocker report.",+ level="definition",+ contingent_on="the site blocks submission",+ contingent_antecedent_output_path="output.blocker",+ )+ )++ assert wu._contingent_antecedent_child_paths(ctx) == set()+++def test_mint_degraded_antecedent_does_not_contribute() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_degraded",+ outcome="A blocker is reported when the site blocks submission.",+ contingent_on="the site blocks submission",+ contingent_antecedent_output_path="output.blocker",+ mint_degrade="turn_unsatisfiable_fallback",+ )+ )++ assert wu._contingent_antecedent_child_paths(ctx) == set()+++def test_recorded_outcome_source_unions_antecedent() -> None:+ ctx = _antecedent_ctx(_blocker_contingent_criterion())+ ctx.latest_recorded_build_test_outcome = RecordedBuildTestOutcome(+ phase="persisted_block_run",+ attempted_tool="update_and_run_blocks",+ verdict="repairable_failure",+ reason_code="outcome_not_demonstrated",+ structural_failure_identity="completion:typed-output",+ missing_requested_output_facts=[+ {"output_path": "output.recorded_value", "output_root": "output", "value_status": "no_typed_value"},+ ],+ )++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == {"output.recorded_value"}+ assert contract.declaration_paths == {"output.blocker"}+ assert contract.source == "recorded_outcome"+++def test_metadata_reject_merge_still_fires_with_antecedent_declaration() -> None:+ ctx = _antecedent_ctx(_blocker_contingent_criterion())+ ctx.last_code_authoring_repair_context = CodeAuthoringRepairContext(+ block_label="collect",+ reason_code="metadata_reject",+ required_goal_value_paths=["output.record_id"],+ metadata_contract_source="metadata_reject",+ metadata_contract_reason_code="metadata_contract_missing",+ )++ contract = wu._output_contract_required_paths_source(ctx)++ assert contract.observation_paths == {"output.record_id"}+ assert contract.declaration_paths == {"output.blocker"}+ assert contract.source == "metadata_reject"+ assert contract.reason_code == "metadata_contract_missing"+++def test_metadata_reject_round_trip_preserves_lanes() -> None:+ repair = wu._metadata_output_repair_context(+ block_labels=["collect"],+ required_paths={"output.record_id"},+ coverage_reason_code="metadata_contract_missing",+ source="metadata_reject",+ summary="missing contract",+ declaration_paths={"output.blocker"},+ )+ assert repair is not None+ assert repair.required_goal_value_paths == ["output.record_id"]+ assert repair.required_extraction_schema_paths == ["output.blocker", "output.record_id"]+ assert repair.required_code_return_paths == ["output.blocker", "output.record_id"]+ rehydration_ctx = _antecedent_ctx(_blocker_contingent_criterion())+ rehydration_ctx.last_code_authoring_repair_context = repair++ contract = wu._output_contract_required_paths_source(rehydration_ctx)++ assert contract.observation_paths == {"output.record_id"}+ assert contract.declaration_paths == {"output.blocker"}+++def test_evaluation_routes_lanes_per_consumer() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ workflow_yaml = (+ "title: Record lookup\n"+ "workflow_definition:\n"+ " blocks:\n"+ " - block_type: code\n"+ " label: extract_record\n"+ " code: |\n"+ ' record_id = await page.inner_text("#record")\n'+ " return record_id\n"+ )++ evaluation = wu._evaluate_output_contract_for_code_block(ctx, workflow_yaml, [])++ assert evaluation is not None+ assert evaluation.block_label == "extract_record"+ assert evaluation.required_paths == {"output.record_id", "output.blocker"}+ assert evaluation.observation_paths == {"output.record_id"}+ assert evaluation.declaration_paths == {"output.blocker"}+ assert "output.blocker" not in evaluation.missing_metadata_paths+ assert "output.blocker" in evaluation.missing_schema_paths # nosemgrep: incomplete-url-substring-sanitization+ assert "output.blocker" in evaluation.missing_return_paths # nosemgrep: incomplete-url-substring-sanitization+ payload = evaluation.payload+ assert payload["canonical_required_child_paths"] == ["output.blocker", "output.record_id"]+ assert payload["declaration_only_child_paths"] == ["output.blocker"]+ assert payload["missing_goal_value_paths"] == ["output.record_id"]+ assert payload["satisfying_templates"]["return_skeleton"] == (+ 'return {"output": {"blocker": None, "record_id": record_id}}'+ )+ template = payload["satisfying_templates"]["code_artifact_metadata"]+ assert template["claimed_outcomes"][0]["goal_value_paths"] == ["output.record_id"]+ blocker_facts = [+ fact for fact in payload["missing_requested_output_facts"] if fact["output_path"] == "output.blocker"+ ]+ assert blocker_facts and blocker_facts[0]["value_status"] == "declaration_required_default_none"+ record_facts = [+ fact for fact in payload["missing_requested_output_facts"] if fact["output_path"] == "output.record_id"+ ]+ assert record_facts and record_facts[0]["value_status"] == "no_typed_value"+ assert evaluation.repair_context is not None+ assert evaluation.repair_context.required_goal_value_paths == ["output.record_id"]+ assert evaluation.repair_context.required_code_return_paths == ["output.blocker", "output.record_id"]+ assert evaluation.canonical_signature == wu._stable_output_contract_key(+ wu._output_contract_scope_key(ctx), evaluation.required_paths+ )+++def test_declaration_only_contract_evaluates_with_single_block_owner() -> None:+ ctx = _antecedent_ctx(_blocker_contingent_criterion())+ workflow_yaml = (+ "title: Blocker only\n"+ "workflow_definition:\n"+ " blocks:\n"+ " - block_type: code\n"+ " label: submit\n"+ " code: |\n"+ ' await page.click("#submit")\n'+ " return {}\n"+ )++ evaluation = wu._evaluate_output_contract_for_code_block(ctx, workflow_yaml, [])++ assert evaluation is not None+ assert evaluation.block_label == "submit"+ assert evaluation.observation_paths == set()+ assert "output.blocker" in evaluation.missing_return_paths # nosemgrep: incomplete-url-substring-sanitization+ assert evaluation.missing_metadata_paths == []+++def test_repair_context_goal_role_never_feeds_enforcement_uncovered_paths() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ ctx.last_code_authoring_repair_context = wu._metadata_output_repair_context(+ block_labels=["extract_record"],+ required_paths={"output.record_id"},+ coverage_reason_code="metadata_contract_missing",+ source="metadata_reject",+ summary="missing contract",+ declaration_paths={"output.blocker"},+ )++ uncovered = enforcement.uncovered_requested_output_paths(ctx)++ assert "output.blocker" not in uncovered+ assert "output.record_id" in uncovered+++def test_static_return_imposition_renders_declaration_as_none_literal() -> None:+ code = 'record_id = await page.inner_text("#record")'++ keyed, violations = wu._extraction_code_with_required_static_return(+ code,+ required_paths={"output.record_id"},+ declaration_paths={"output.blocker"},+ )++ assert violations == []+ assert keyed.endswith('return {"output": {"blocker": None, "record_id": record_id}}')+ produced = wu._code_block_produced_output_paths(keyed)+ assert {"output.record_id", "output.blocker"} <= produced+++def test_scaffolded_metadata_goal_rows_exclude_declaration_paths() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ workflow_yaml = (+ "title: Record lookup\n"+ "workflow_definition:\n"+ " blocks:\n"+ " - block_type: code\n"+ " label: extract_record\n"+ " code: |\n"+ ' record_id = await page.inner_text("#record")\n'+ ' return {"output": {"record_id": record_id, "blocker": None}}\n'+ )++ scaffolded, applied = wu._scaffold_metadata_contract_for_update(ctx, workflow_yaml, [])++ assert applied is True+ row = scaffolded[0]["claimed_outcomes"][0]+ assert row["goal_value_paths"] == ["output.record_id"]+ schema = json.loads(row["extraction_schema"])+ assert schema["properties"]["output"]["properties"]["blocker"] == {"type": ["string", "null"]}+ assert set(schema["properties"]["output"]["required"]) == {"record_id", "blocker"}+++def test_mint_degraded_output_path_leaves_observation_lane() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_degraded",+ outcome="The returned record includes record id.",+ output_path="output.record_id",+ contingent_on="the lookup is blocked",+ mint_degrade="contingent_missing_antecedent",+ )+ )++ assert wu._requested_output_child_paths(ctx) == set()+ assert wu._output_contract_required_paths_source(ctx).union == set()+++def test_runtime_repair_contract_carries_declaration_lane_with_stable_signature() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ pre_run = wu._output_contract_required_paths_source(ctx)+ pre_signature = wu._stable_output_contract_key(wu._output_contract_scope_key(ctx), pre_run.union)+ ctx.latest_recorded_build_test_outcome = RecordedBuildTestOutcome(+ phase="persisted_block_run",+ attempted_tool="update_and_run_blocks",+ verdict="repairable_failure",+ reason_code="outcome_not_demonstrated",+ workflow_run_id="wr_run_1",+ runtime_output_repair_facts=[+ {"workflow_run_id": "wr_run_1", "output_path": "output.record_id", "block_label": "extract_record"}+ ],+ )++ post_run = wu._output_contract_required_paths_source(ctx)++ assert post_run.source == "runtime_output_repair"+ assert post_run.observation_paths == {"output.record_id"}+ assert post_run.declaration_paths == {"output.blocker"}+ assert wu._stable_output_contract_key(wu._output_contract_scope_key(ctx), post_run.union) == pre_signature+++def test_declaration_only_production_still_counts_as_abstained_candidate() -> None:+ code = 'await page.click("#submit")\nreturn {"output": {"blocker": None}}'++ assert (+ wu._code_return_is_static_advisory_candidate(code, {"output.record_id", "output.blocker"}, {"output.blocker"})+ is True+ )+++def test_observation_production_disqualifies_advisory_candidate() -> None:+ code = 'value = await page.inner_text("#r")\nreturn {"output": {"record_id": value}}'++ assert (+ wu._code_return_is_static_advisory_candidate(code, {"output.record_id", "output.blocker"}, {"output.blocker"})+ is False+ )+++def test_declaration_only_contract_is_never_advisory_candidate() -> None:+ code = 'await page.click("#submit")'++ assert wu._code_return_is_static_advisory_candidate(code, {"output.blocker"}, {"output.blocker"}) is False+++def _declaration_waiver_yaml(code_body: str) -> str:+ indented = "\n".join(f" {line}" for line in code_body.splitlines())+ return (+ "title: Record lookup\n"+ "workflow_definition:\n"+ " blocks:\n"+ " - block_type: code\n"+ " label: extract_record\n"+ " code: |\n"+ f"{indented}\n"+ )+++def _declaration_waiver_metadata(union: set[str]) -> list[dict[str, object]]:+ return [+ wu._metadata_contract_template(+ block_label="extract_record",+ required_paths=union,+ source="requested_output_contract",+ reason_code="requested_output_contract_missing_output_coverage",+ declaration_paths={"output.blocker"},+ )+ ]+++def test_post_steering_advisory_never_waives_declaration_return_miss() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ union = {"output.record_id", "output.blocker"}+ signature = wu._stable_output_contract_key(wu._output_contract_scope_key(ctx), union)+ ctx.output_contract_reject_count_by_signature = {signature: wu._MAX_OUTPUT_CONTRACT_STEERING_REJECTS}+ workflow_yaml = _declaration_waiver_yaml("result = await collect()\nreturn result")++ evaluation = wu._evaluate_output_contract_for_code_block(+ ctx, workflow_yaml, _declaration_waiver_metadata(union), allow_static_return_advisory=True+ )++ assert evaluation is not None+ assert evaluation.payload["post_steering_static_return_advisory"] is True+ assert evaluation.payload["static_return_advisory_paths"] == ["output.record_id"]+ assert evaluation.missing_return_paths == ["output.blocker"]+ assert evaluation.can_attempt_run is False+++def test_stamped_declaration_with_advisory_accepts_run() -> None:+ ctx = _antecedent_ctx(+ CompletionCriterion(+ id="c_record", outcome="The returned record includes record id.", output_path="output.record_id"+ ),+ _blocker_contingent_criterion(),+ )+ union = {"output.record_id", "output.blocker"}+ signature = wu._stable_output_contract_key(wu._output_contract_scope_key(ctx), union)+ ctx.output_contract_reject_count_by_signature = {signature: wu._MAX_OUTPUT_CONTRACT_STEERING_REJECTS}+ workflow_yaml = _declaration_waiver_yaml('await page.click("#submit")\nreturn {"output": {"blocker": None}}')++ evaluation = wu._evaluate_output_contract_for_code_block(+ ctx, workflow_yaml, _declaration_waiver_metadata(union), allow_static_return_advisory=True+ )++ assert evaluation is not None+ assert evaluation.missing_return_paths == []+ assert evaluation.payload["static_return_advisory_paths"] == ["output.record_id"]+ assert evaluation.can_attempt_run is True+++def test_merge_declaration_children_into_empty_literal_return() -> None:+ merged = wu._merge_declaration_children_into_literal_returns("return {}", {"output.blocker"})++ assert merged == 'return {"output": {"blocker": None}}'+++def test_merge_declaration_children_into_existing_output_literal() -> None:+ code = 'value = await page.inner_text("#r")\nreturn {"output": {"record_id": value}}'++ merged = wu._merge_declaration_children_into_literal_returns(code, {"output.blocker"})++ assert merged.endswith('return {"output": {"blocker": None, "record_id": value}}')+ assert {"output.blocker", "output.record_id"} <= wu._code_block_produced_output_paths(merged)+++def test_code_with_declared_contract_defaults_appends_return_when_abstained() -> None:+ stamped = wu._code_with_declared_contract_defaults('await page.click("#submit")', {"output.blocker"})++ assert stamped.endswith('return {"output": {"blocker": None}}')+++def test_code_with_declared_contract_defaults_is_idempotent() -> None:+ stamped = wu._code_with_declared_contract_defaults('await page.click("#submit")', {"output.blocker"})++ assert wu._code_with_declared_contract_defaults(stamped, {"output.blocker"}) == ""+++def test_code_with_declared_contract_defaults_skips_dynamic_return() -> None:+ assert wu._code_with_declared_contract_defaults("result = await collect()\nreturn result", {"output.blocker"}) == ""+++def test_stamped_declaration_keeps_click_only_spine_classification() -> None:+ code = 'await page.click("#submit")\nreturn {"output": {"blocker": None}}'++ assert wu._output_contract_click_only_spine(code, {"output.blocker"}) is True+ assert wu._output_contract_click_only_spine(code) is False+++def test_observation_production_is_not_click_only_spine() -> None:+ code = 'await page.click("#submit")\nreturn {"output": {"record_id": "x"}}'++ assert wu._output_contract_click_only_spine(code, {"output.blocker"}) is Falsediff --git a/tests/unit/test_copilot_request_policy_completion_criteria.py b/tests/unit/test_copilot_request_policy_completion_criteria.pyindex cdd284d58..9525f80c2 100644--- a/tests/unit/test_copilot_request_policy_completion_criteria.py+++ b/tests/unit/test_copilot_request_policy_completion_criteria.py@@ -22,8 +22,10 @@ from skyvern.forge.sdk.copilot.request_policy import (RequestPolicy,_apply_requested_output_completion_criteria,_apply_validation_classification_completion_criteria,+ _classifier_fallback_policy,_classify_request,_criterion_grounding_mode,+ _degrade_pathless_contingent_criteria,_parse_completion_criteria,_render_active_criteria_for_prompt,build_classifier_fallback_floor,@@ -1540,6 +1542,7 @@ def test_active_criteria_rendering_includes_optional_fields({"outcome": "A provider blocker is reported to the user.","contingent_on": "the provider site blocks online submission",+ "mint_degrade": "contingent_missing_antecedent",},id="contingent_on",),@@ -2079,3 +2082,110 @@ def test_scope_boundary_boolean_on_non_enumerated_path_keeps_judgment_invariant(assert kept.kind == "outcome"assert kept.expected_output_value is Trueassert kept.requested_output_evidence_source == "independent_run_evidence"+++async def test_classifier_success_path_degrades_pathless_contingent_criterion() -> None:+ policy = await _policy_for_message(+ "Submit the request and report any blocker.",+ [+ {"outcome": "A blocker is reported to the user.", "contingent_on": "the site blocks submission"},+ {+ "outcome": "A blocker is reported with evidence.",+ "contingent_on": "the site blocks submission with evidence",+ "contingent_antecedent_output_path": "output.blocker",+ },+ ],+ )++ by_contingent = {c.contingent_on: c for c in policy.completion_criteria if c.contingent_on}+ assert by_contingent["the site blocks submission"].mint_degrade == "contingent_missing_antecedent"+ assert by_contingent["the site blocks submission with evidence"].mint_degrade is None+++async def test_canonicalization_carry_of_pathless_contingent_is_mint_degraded() -> None:+ policy = await _policy_for_message(+ "Return a final record with NPI.",+ [+ {+ "outcome": "The returned record includes NPI.",+ "output_path": "output.npi",+ "contingent_on": "the provider site allows online lookup",+ }+ ],+ )++ (canonical,) = (c for c in policy.completion_criteria if c.output_path == "output.npi")+ assert canonical.contingent_on == "the provider site allows online lookup"+ assert canonical.contingent_antecedent_output_path is None+ assert canonical.mint_degrade == "contingent_missing_antecedent"+++def test_fallback_policy_upholds_contingent_mint_invariant() -> None:+ policy = _classifier_fallback_policy(+ [],+ raw_secret_present=False,+ failure_kind="provider_error",+ user_message="return the status for the record",+ )++ assert policy.completion_criteria+ assert all(+ criterion.contingent_antecedent_output_path is not None or criterion.mint_degrade is not None+ for criterion in policy.completion_criteria+ if criterion.contingent_on+ )+++def test_degrade_sweep_targets_only_pathless_contingent_criteria() -> None:+ policy = RequestPolicy(+ completion_criteria=[+ _criterion("c0", "A blocker is reported to the user.", contingent_on="the site blocks submission"),+ _criterion(+ "c1",+ "A blocker is reported with evidence.",+ contingent_on="the site blocks submission with evidence",+ contingent_antecedent_output_path="output.blocker",+ ),+ _criterion("c2", "The request is submitted."),+ ]+ )++ _degrade_pathless_contingent_criteria(policy)++ degrades = {criterion.id: criterion.mint_degrade for criterion in policy.completion_criteria}+ assert degrades == {"c0": "contingent_missing_antecedent", "c1": None, "c2": None}+++def test_degrade_sweep_preserves_existing_mint_degrade() -> None:+ policy = RequestPolicy(+ completion_criteria=[+ replace(+ _criterion("c0", "return the status", contingent_on="the site blocks submission"),+ mint_degrade="turn_unsatisfiable_fallback",+ )+ ]+ )++ _degrade_pathless_contingent_criteria(policy)++ assert policy.completion_criteria[0].mint_degrade == "turn_unsatisfiable_fallback"+++def test_to_trace_data_reports_mint_degraded_criteria_without_output_path() -> None:+ policy = RequestPolicy(+ completion_criteria=[+ replace(+ _criterion("c0", "A blocker is reported to the user.", contingent_on="the site blocks submission"),+ mint_degrade="contingent_missing_antecedent",+ ),+ _criterion("c1", "The request is submitted."),+ ]+ )++ data = policy.to_trace_data()++ assert data["mint_degraded_criterion_count"] == 1+ assert data["mint_degraded_criterion_0_id"] == "c0"+ assert data["mint_degraded_criterion_0_mint_degrade"] == "contingent_missing_antecedent"diff --git a/tests/unit/test_copilot_sky11865_mint_satisfiability.py b/tests/unit/test_copilot_sky11865_mint_satisfiability.pyindex b0801597d..7690fd520 100644--- a/tests/unit/test_copilot_sky11865_mint_satisfiability.py+++ b/tests/unit/test_copilot_sky11865_mint_satisfiability.py@@ -454,6 +454,52 @@ def test_recorded_branch_no_meaningful_output_degrades() -> None:assert outcome.reason_code == "fallback_floor_turn_unsatisfiable"+def test_contingent_missing_antecedent_counts_as_degraded() -> None:+ criterion = CompletionCriterion(+ id="c1",+ outcome="A blocker is reported to the user.",+ level="run",+ contingent_on="the site blocks submission",+ mint_degrade="contingent_missing_antecedent",+ )+ assert is_turn_unsatisfiable_fallback_degraded(criterion) is True+ assert is_turn_unsatisfiable_fallback_degraded(replace(criterion, mint_degrade=None)) is False+++def test_contingent_missing_antecedent_carries_and_blocks_credit_when_unsatisfied() -> None:+ result = carry_degraded_criterion_ids(+ CompletionVerificationResult(+ status="evaluated",+ criterion_ids=["c1", "floor"],+ verdicts=[+ CriterionVerdict(criterion_id="c1", state="unsatisfied", reason_code="no_evidence"),+ CriterionVerdict(criterion_id="floor", state="satisfied", reason_code="evidence_confirms"),+ ],+ ),+ [+ CompletionCriterion(+ id="c1",+ outcome="A blocker is reported to the user.",+ level="run",+ contingent_on="the site blocks submission",+ mint_degrade="contingent_missing_antecedent",+ )+ ],+ )+ assert "c1" in result.degraded_criterion_ids+ assert result.is_fully_satisfied() is False+++def test_satisfied_degraded_criterion_still_credits_on_merits() -> None:+ result = CompletionVerificationResult(+ status="evaluated",+ criterion_ids=["c1"],+ verdicts=[CriterionVerdict(criterion_id="c1", state="satisfied", reason_code="evidence_confirms")],+ degraded_criterion_ids=["c1"],+ )+ assert result.is_fully_satisfied() is True++def test_recorded_branch_ambiguous_code_with_failed_block_stays_repairable() -> None:result = {"data": {