Skip to content

Automate browser based workflows with AI

Arm an escalating convergence directive when Copilot authors code blocks without metadata (SKY-11949) (#7533)

Andrew Neilson79b0bf1de53a
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 4ca37a843..e5dd7cffa 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -3712,6 +3712,7 @@ def _adjudicate_output_contract_ladder_after_reject(
*,
workflow_yaml: str,
current_fingerprint: str,
+ steer_only: bool = False,
) -> OutputContractActuation | None:
"""Run the actuation ladder at the shared deficiency seam so a signature whose formation
remains incomplete advances toward an advisory-consumed run or a typed terminal within the
@@ -3744,6 +3745,7 @@ def _adjudicate_output_contract_ladder_after_reject(
current_fingerprint=current_fingerprint,
advisory_run_grantable=blockers == [_OUTPUT_CONTRACT_REJECT_REASON_CODE],
declaration_paths=evaluation.declaration_paths,
+ steer_only=steer_only,
)
if actuation.kind == OutputContractActuationKind.BLOCKED_TERMINAL:
_stash_output_source_unobservable_terminal(
@@ -3759,6 +3761,130 @@ def _adjudicate_output_contract_ladder_after_reject(
return actuation
+_METADATA_CONVERGENCE_DIRECTIVE_BLOCKER = "missing_code_artifact_metadata"
+
+
+def _metadata_reject_directive_payload(
+ *,
+ missing_fields_by_label: dict[str, list[str]],
+ required_paths: set[str],
+ escalate: bool,
+) -> dict[str, Any]:
+ directive: dict[str, Any] = {
+ "rung": 2 if escalate else 1,
+ "missing_fields_by_label": {label: list(fields) for label, fields in missing_fields_by_label.items()},
+ }
+ if escalate:
+ # "evidence_refs_or_observation_refs" is a messaging label for the either-one requirement;
+ # the skeleton must only offer real CodeArtifactMetadata fields the model can set.
+ directive["metadata_fill_in_skeleton"] = {
+ label: {
+ field: "" if field == "declared_goal" else []
+ for raw_field in fields
+ for field in (
+ ("evidence_refs", "observation_refs")
+ if raw_field == "evidence_refs_or_observation_refs"
+ else (raw_field,)
+ )
+ }
+ for label, fields in missing_fields_by_label.items()
+ }
+ if required_paths:
+ directive["required_output_paths"] = sorted(required_paths)
+ directive["extraction_schema_template"] = _schema_template_for_required_paths(required_paths)
+ return directive
+
+
+def _emit_metadata_convergence_directive(
+ *,
+ signature: str,
+ block_label: str,
+ missing_fields_by_label: dict[str, list[str]],
+ required_paths: set[str],
+ escalate: bool,
+) -> dict[str, Any]:
+ LOG.info(
+ "copilot_output_contract_spine_structure_directive_emitted",
+ block_label=block_label,
+ canonical_output_contract_signature=signature,
+ spine_split_blockers=[_METADATA_CONVERGENCE_DIRECTIVE_BLOCKER],
+ spine_stage_count=None,
+ rung=2 if escalate else 1,
+ )
+ return _metadata_reject_directive_payload(
+ missing_fields_by_label=missing_fields_by_label,
+ required_paths=required_paths,
+ escalate=escalate,
+ )
+
+
+def _metadata_reject_seam_fingerprint(
+ workflow_yaml: str, signature: str, missing_fields_by_label: dict[str, list[str]]
+) -> str:
+ return hashlib.sha256(
+ (
+ _output_contract_structural_fingerprint(workflow_yaml, signature)
+ + json.dumps(missing_fields_by_label, sort_keys=True)
+ ).encode("utf-8")
+ ).hexdigest()
+
+
+def _adjudicate_metadata_reject_ladder(
+ ctx: AgentContext,
+ *,
+ workflow_yaml: str,
+ raw_metadata: object,
+ missing_labels: list[str],
+ required_paths: set[str],
+) -> dict[str, Any] | None:
+ """Route the metadata-less reject through the shared actuation ladder in steer-only mode so a
+ repeated identical structural key escalates the convergence directive instead of re-issuing the
+ same steer, never minting an advisory run or a terminal at this seam."""
+ if copilot_author_time_gate_log_only_enabled(ctx, _OUTPUT_CONTRACT_ABLATION_GATE_ID):
+ return None
+ signature = _output_contract_signature(ctx=ctx, required_paths=required_paths)
+ block_label = missing_labels[0] if len(missing_labels) == 1 else ""
+ if not signature or not block_label:
+ return None
+ missing_fields_by_label = _metadata_missing_required_fields_by_label(
+ raw_metadata, labels=missing_labels, missing_labels=missing_labels
+ )
+ seam_fingerprint = _metadata_reject_seam_fingerprint(workflow_yaml, signature, missing_fields_by_label)
+ escalate = _prior_output_contract_directive_unconsumed(ctx, signature, seam_fingerprint)
+ evaluation = _OutputContractEvaluation(
+ block_label=block_label,
+ artifact_id="",
+ required_paths=required_paths,
+ observation_paths=required_paths,
+ declaration_paths=set(),
+ source="missing_code_artifact_metadata",
+ reason_code="metadata_reject",
+ missing_metadata_paths=[],
+ missing_schema_paths=[],
+ missing_return_paths=[],
+ shape_violations=[],
+ canonical_signature=signature,
+ payload={"reason_code": "metadata_reject"},
+ repair_context=None,
+ )
+ actuation = _adjudicate_output_contract_ladder_after_reject(
+ ctx,
+ evaluation,
+ workflow_yaml=workflow_yaml,
+ current_fingerprint=seam_fingerprint,
+ steer_only=True,
+ )
+ if actuation is None or actuation.kind != OutputContractActuationKind.STRUCTURE_DIRECTIVE:
+ return None
+ return _emit_metadata_convergence_directive(
+ signature=signature,
+ block_label=block_label,
+ missing_fields_by_label=missing_fields_by_label,
+ required_paths=required_paths,
+ escalate=escalate,
+ )
+
+
def _record_output_contract_reject(
ctx: AgentContext,
evaluation: _OutputContractEvaluation,
@@ -3766,6 +3892,7 @@ def _record_output_contract_reject(
summary: str,
authored_structural_fingerprint: str = "",
workflow_yaml: str = "",
+ raw_metadata: object = None,
) -> dict[str, Any]:
# A candidate so incomplete that no required child paths are derivable must still consume the
# reject budget: an empty set would silently skip counting and degrade the turn to the generic
@@ -3781,14 +3908,39 @@ def _record_output_contract_reject(
payload = dict(evaluation.payload)
payload["output_contract_reject_count"] = count
payload["output_contract_reject_budget"] = _MAX_OUTPUT_CONTRACT_REJECTS
+ missing_fields_by_label = (
+ _metadata_missing_required_fields_by_label(raw_metadata, labels=[evaluation.block_label], missing_labels=[])
+ if evaluation.block_label
+ else {}
+ )
+ directive_fingerprint = (
+ _metadata_reject_seam_fingerprint(workflow_yaml, evaluation.canonical_signature, missing_fields_by_label)
+ if missing_fields_by_label
+ else authored_structural_fingerprint
+ )
+ directive_escalate = _prior_output_contract_directive_unconsumed(
+ ctx, evaluation.canonical_signature, directive_fingerprint
+ )
actuation = _adjudicate_output_contract_ladder_after_reject(
ctx,
evaluation,
workflow_yaml=workflow_yaml,
- current_fingerprint=authored_structural_fingerprint,
+ current_fingerprint=directive_fingerprint,
)
if actuation is not None:
payload["output_contract_actuation"] = actuation.kind.value
+ if (
+ actuation.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE
+ and evaluation.block_label
+ and missing_fields_by_label
+ ):
+ payload["metadata_convergence_directive"] = _emit_metadata_convergence_directive(
+ signature=evaluation.canonical_signature,
+ block_label=evaluation.block_label,
+ missing_fields_by_label=missing_fields_by_label,
+ required_paths=evaluation.required_paths,
+ escalate=directive_escalate,
+ )
if actuation.kind != OutputContractActuationKind.STRUCTURE_DIRECTIVE:
latest_outcome = ctx.latest_recorded_build_test_outcome
if (
@@ -4915,6 +5067,7 @@ def _actuate_output_contract_bail(
current_fingerprint: str,
advisory_run_grantable: bool = False,
declaration_paths: set[str] | None = None,
+ steer_only: bool = False,
) -> OutputContractActuation:
click_only_spine = _output_contract_click_only_spine(target_code, declaration_paths)
observed_required_values = _observed_required_output_values(ctx, required_paths)
@@ -4948,6 +5101,11 @@ def _actuate_output_contract_bail(
)
if actuation.kind == OutputContractActuationKind.ADVISORY_RUN and not _run_authority_permits_dispatch(ctx):
actuation = OutputContractActuation(OutputContractActuationKind.STRUCTURE_DIRECTIVE, actuation.family)
+ if steer_only and actuation.kind in {
+ OutputContractActuationKind.ADVISORY_RUN,
+ OutputContractActuationKind.BLOCKED_TERMINAL,
+ }:
+ actuation = OutputContractActuation(OutputContractActuationKind.STRUCTURE_DIRECTIVE, actuation.family)
payload: AuthorTimeGateAblationPayload = {
"actuation_kind": actuation.kind.value,
"family": actuation.family.value,
@@ -4979,10 +5137,16 @@ def _actuate_output_contract_bail(
if actuation.kind == OutputContractActuationKind.ADVISORY_RUN:
_grant_output_contract_advisory_run(ctx, signature)
_arm_pending_run_evidence(ctx, signature, required_paths)
- elif actuation.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE and not evidence.prior_directive_unconsumed:
+ elif (
+ actuation.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE
+ and not evidence.prior_directive_unconsumed
+ and not steer_only
+ ):
_record_output_contract_actuation_progress(ctx, signature)
if (
- actuation.kind not in {OutputContractActuationKind.BLOCKED_TERMINAL, OutputContractActuationKind.ADVISORY_RUN}
+ not steer_only
+ and actuation.kind
+ not in {OutputContractActuationKind.BLOCKED_TERMINAL, OutputContractActuationKind.ADVISORY_RUN}
and click_only_spine
and not observed_required_values
):
@@ -5611,6 +5775,7 @@ def _metadata_contract_run_preflight_reject(
summary="Submitted workflow does not satisfy the requested output contract before run.",
authored_structural_fingerprint=authored_fingerprint,
workflow_yaml=workflow_yaml,
+ raw_metadata=raw_code_artifact_metadata,
)
if (
evaluation.can_attempt_run or _OUTPUT_CONTRACT_VALUE_REQUIRED_REASON_CODE not in evaluation.shape_violations
@@ -11564,6 +11729,7 @@ async def _update_workflow(
summary="Submitted workflow does not satisfy the requested output contract.",
authored_structural_fingerprint=authored_fingerprint,
workflow_yaml=workflow_yaml,
+ raw_metadata=params.get("code_artifact_metadata"),
)
if allow_static_output_uncertainty and _output_contract_advisory_granted(
ctx, output_contract_evaluation.canonical_signature
@@ -11675,14 +11841,24 @@ async def _update_workflow(
_record_code_authoring_guardrail_reject(ctx, defer_churn_stop=True)
elif missing_metadata_reject_count < 1 and not budget_terminal:
_record_code_authoring_guardrail_reject(ctx)
+ metadata_convergence_directive = _adjudicate_metadata_reject_ladder(
+ ctx,
+ workflow_yaml=workflow_yaml,
+ raw_metadata=params.get("code_artifact_metadata"),
+ missing_labels=missing_labels,
+ required_paths=required_child_output_paths,
+ )
+ metadata_reject_data = _code_repair_progress_data(
+ metadata_repair_context,
+ missing_requested_output_facts=missing_metadata_output_facts,
+ metadata_repair_contract=metadata_repair_contract,
+ )
+ if metadata_convergence_directive is not None:
+ metadata_reject_data["metadata_convergence_directive"] = metadata_convergence_directive
return reject(
error=missing_metadata_error,
user_facing_summary=_compiled_authoring_user_summary(),
- data=_code_repair_progress_data(
- metadata_repair_context,
- missing_requested_output_facts=missing_metadata_output_facts,
- metadata_repair_contract=metadata_repair_contract,
- ),
+ data=metadata_reject_data,
repair_context=metadata_repair_context,
record_repair_context_outcome=False,
)
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index 290963080..50e06217d 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -5173,6 +5173,117 @@ class TestCodeRepairProgressClassification:
assert result["data"]["missing_extraction_schema_paths"] == ["output.record_id"]
assert result["data"]["missing_code_return_paths"] == ["output.record_id"]
+ def _run_preflight_metadata_reject_ctx(self) -> CopilotContext:
+ ctx = _code_only_ctx()
+ ctx.last_code_authoring_repair_context = CodeAuthoringRepairContext(
+ block_label="validate_start",
+ reason_code="metadata_reject",
+ required_goal_value_paths=["output.record_id"],
+ required_extraction_schema_paths=["output.record_id"],
+ required_code_return_paths=["output.record_id"],
+ metadata_contract_source="requested_output_contract",
+ metadata_contract_reason_code="requested_output_contract_missing_output_coverage",
+ )
+ return ctx
+
+ _RUN_PREFLIGHT_CLICK_YAML = _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: validate_start
+ code: |
+ await page.locator("#start").click()
+ """
+ )
+
+ def test_run_preflight_metadata_reject_arms_directive_and_emits_once(self) -> None:
+ ctx = self._run_preflight_metadata_reject_ctx()
+ with capture_logs() as logs:
+ result = workflow_update_module._metadata_contract_run_preflight_reject(
+ ctx, self._RUN_PREFLIGHT_CLICK_YAML, []
+ )
+ assert result is not None and result["ok"] is False
+ directive = result["data"]["metadata_convergence_directive"]
+ assert directive["rung"] == 1
+ assert "declared_goal" in directive["missing_fields_by_label"]["validate_start"]
+ emits = [log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]
+ assert len(emits) == 1
+
+ def test_run_preflight_metadata_reject_repeat_does_not_reissue_emit(self) -> None:
+ ctx = self._run_preflight_metadata_reject_ctx()
+ with capture_logs() as first_logs:
+ first = workflow_update_module._metadata_contract_run_preflight_reject(
+ ctx, self._RUN_PREFLIGHT_CLICK_YAML, []
+ )
+ ctx.output_contract_bail_actuated_this_call = False
+ with capture_logs() as second_logs:
+ workflow_update_module._metadata_contract_run_preflight_reject(ctx, self._RUN_PREFLIGHT_CLICK_YAML, [])
+ assert first is not None and first["data"]["metadata_convergence_directive"]["rung"] == 1
+ assert len([log for log in first_logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]) == 1
+ assert [log for log in second_logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT] == []
+
+ def test_run_preflight_value_required_reject_arms_no_directive(self) -> None:
+ ctx = self._run_preflight_metadata_reject_ctx()
+ workflow_yaml = _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: validate_start
+ code: |
+ return {"output": {"summary": "ok"}}
+ """
+ )
+ metadata = [
+ {
+ "block_label": "validate_start",
+ "claimed_outcomes": [
+ {
+ "goal_value_paths": ["output"],
+ "extraction_schema": '{"type":"object","properties":{"output":{"type":"object"}}}',
+ }
+ ],
+ "terminal_verifier_expectations": [{"goal_value_paths": ["output"]}],
+ }
+ ]
+ with capture_logs() as logs:
+ result = workflow_update_module._metadata_contract_run_preflight_reject(ctx, workflow_yaml, metadata)
+ assert result is not None and result["ok"] is False
+ assert "metadata_convergence_directive" not in result["data"]
+ assert [log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT] == []
+
+ def test_cross_seam_write_arm_then_metadata_less_preflight_escalates(self) -> None:
+ ctx = self._run_preflight_metadata_reject_ctx()
+ first = workflow_update_module._metadata_contract_run_preflight_reject(ctx, self._RUN_PREFLIGHT_CLICK_YAML, [])
+ assert first is not None and first["data"]["metadata_convergence_directive"]["rung"] == 1
+ ctx.output_contract_bail_actuated_this_call = False
+ directive = workflow_update_module._adjudicate_metadata_reject_ladder(
+ ctx,
+ workflow_yaml=self._RUN_PREFLIGHT_CLICK_YAML,
+ raw_metadata=[],
+ missing_labels=["validate_start"],
+ required_paths={"output.record_id"},
+ )
+ assert directive is not None
+ assert directive["rung"] == 2
+ assert "metadata_fill_in_skeleton" in directive
+
+ def test_metadata_less_preflight_never_grants_advisory_run(self) -> None:
+ ctx = self._run_preflight_metadata_reject_ctx()
+ directive = workflow_update_module._adjudicate_metadata_reject_ladder(
+ ctx,
+ workflow_yaml=self._RUN_PREFLIGHT_CLICK_YAML,
+ raw_metadata=[],
+ missing_labels=["validate_start"],
+ required_paths={"output.record_id"},
+ )
+ signature = workflow_update_module._output_contract_signature(ctx=ctx, required_paths={"output.record_id"})
+ assert directive is not None and directive["rung"] == 1
+ assert workflow_update_module._output_contract_advisory_granted(ctx, signature) is False
+
def test_metadata_contract_scaffold_uses_recorded_paths_before_request_policy(self) -> None:
ctx = _code_only_ctx()
ctx.request_policy = RequestPolicy(
@@ -12181,6 +12292,224 @@ def _submitted_with_sibling_code(sibling_code: str) -> str:
)
+_METADATA_LESS_DIRECTIVE_EVENT = "copilot_output_contract_spine_structure_directive_emitted"
+
+_RECORDED_METADATA_REJECT_BLOCK_LABEL = "validate_public_start_service_path"
+
+
+def _recorded_metadata_reject_yaml() -> str:
+ return _yaml(
+ f"""
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {{parameter_type: output, key: workflow_output}}
+ blocks:
+ - block_type: code
+ label: {_RECORDED_METADATA_REJECT_BLOCK_LABEL}
+ code: |
+ await page.locator("#start").click()
+ """
+ )
+
+
+def _metadata_less_output_yaml(selector: str = "#search-submit") -> str:
+ return _yaml(
+ f"""
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {{parameter_type: output, key: workflow_output}}
+ blocks:
+ - block_type: code
+ label: extract_provider
+ code: |
+ await page.locator("{selector}").click()
+ """
+ )
+
+
+def _metadata_less_page_read_yaml() -> str:
+ return _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {parameter_type: output, key: workflow_output}
+ blocks:
+ - block_type: code
+ label: extract_provider
+ code: |
+ await page.locator("#search-submit").click()
+ value = await page.locator("#npi").inner_text()
+ return {"npi": value}
+ """
+ )
+
+
+def _metadata_less_two_block_yaml() -> str:
+ return _yaml(
+ """
+ title: Provider lookup
+ workflow_definition:
+ parameters:
+ - {parameter_type: output, key: workflow_output}
+ blocks:
+ - block_type: code
+ label: first_block
+ description: extract the provider data
+ code: |
+ await page.locator("#a").click()
+ - block_type: code
+ label: second_block
+ description: extract the license data
+ code: |
+ await page.locator("#b").click()
+ """
+ )
+
+
+class TestMetadataLessRejectArming:
+ @pytest.mark.asyncio
+ async def test_metadata_less_reject_arms_rung_one_directive(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ with capture_logs() as logs:
+ result = await _update_workflow(
+ {"workflow_yaml": _metadata_less_output_yaml()}, ctx, allow_missing_credentials=True
+ )
+ assert result["ok"] is False
+ directive = result["data"]["metadata_convergence_directive"]
+ assert directive["rung"] == 1
+ assert directive["missing_fields_by_label"]["extract_provider"]
+ assert "declared_goal" in directive["missing_fields_by_label"]["extract_provider"]
+ assert [log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]
+ assert len([log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]) == 1
+ assert not ctx.output_contract_spine_directive_blockers_by_attempt_key
+ assert not ctx.output_contract_spine_directive_stage_count_by_attempt_key
+ assert not ctx.output_contract_actuation_by_signature
+ assert not ctx.output_contract_declick_attempted_by_signature
+ assert not ctx.output_contract_actuation_count_by_signature
+
+ @pytest.mark.asyncio
+ async def test_identical_repeat_escalates_to_rung_two_skeleton(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ yaml_text = _metadata_less_output_yaml()
+ await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ with capture_logs() as logs:
+ second = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ directive = second["data"]["metadata_convergence_directive"]
+ assert directive["rung"] == 2
+ skeleton = directive["metadata_fill_in_skeleton"]["extract_provider"]
+ assert skeleton["declared_goal"] == ""
+ assert skeleton["claimed_outcomes"] == []
+ assert "evidence_refs_or_observation_refs" not in skeleton
+ assert skeleton["evidence_refs"] == []
+ assert skeleton["observation_refs"] == []
+ rung_two_emits = [log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]
+ assert len(rung_two_emits) == 1 and rung_two_emits[0]["rung"] == 2
+
+ @pytest.mark.asyncio
+ async def test_structural_progress_resets_to_rung_one(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ click_yaml = _metadata_less_output_yaml()
+ await _update_workflow({"workflow_yaml": click_yaml}, ctx, allow_missing_credentials=True)
+ repeat = await _update_workflow({"workflow_yaml": click_yaml}, ctx, allow_missing_credentials=True)
+ assert repeat["data"]["metadata_convergence_directive"]["rung"] == 2
+ progressed = await _update_workflow(
+ {"workflow_yaml": _metadata_less_page_read_yaml()}, ctx, allow_missing_credentials=True
+ )
+ assert progressed["ok"] is False
+ assert progressed["data"]["metadata_convergence_directive"]["rung"] == 1
+
+ @pytest.mark.asyncio
+ async def test_log_only_mode_leaves_seam_inert(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ monkeypatch.setattr(
+ workflow_update_module, "copilot_author_time_gate_log_only_enabled", lambda *_args, **_kwargs: True
+ )
+ ctx = _code_only_ctx()
+ with capture_logs() as logs:
+ result = await _update_workflow(
+ {"workflow_yaml": _metadata_less_output_yaml()}, ctx, allow_missing_credentials=True
+ )
+ assert result["ok"] is False
+ assert "metadata_convergence_directive" not in result["data"]
+ assert not ctx.output_contract_armed_directive_fingerprint_by_signature
+ assert [log for log in logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT] == []
+
+ @pytest.mark.asyncio
+ async def test_multiple_missing_labels_fail_closed(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ result = await _update_workflow(
+ {"workflow_yaml": _metadata_less_two_block_yaml()}, ctx, allow_missing_credentials=True
+ )
+ assert result["ok"] is False
+ assert ctx.latest_recorded_build_test_outcome.reason_code == "metadata_reject"
+ assert "metadata_convergence_directive" not in result["data"]
+
+ @pytest.mark.asyncio
+ async def test_churn_stash_fires_at_cap_with_arming_active(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ yaml_text = _metadata_less_output_yaml()
+ for index in range(MAX_CODE_AUTHORING_GUARDRAIL_REJECTS - 1):
+ result = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ assert result["ok"] is False
+ assert result["data"]["metadata_convergence_directive"]["rung"] in (1, 2)
+ assert ctx.code_authoring_guardrail_reject_count == index + 1
+ assert ctx.blocker_signal is None
+ final = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ assert final["ok"] is False
+ assert ctx.code_authoring_guardrail_reject_count == MAX_CODE_AUTHORING_GUARDRAIL_REJECTS
+ churn = ctx.blocker_signal
+ assert isinstance(churn, CopilotToolBlockerSignal)
+ assert churn.internal_reason_code == "code_authoring_guardrail_churn"
+
+ @pytest.mark.asyncio
+ async def test_seam_replay_emits_once_then_escalates(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ yaml_text = _metadata_less_output_yaml()
+ with capture_logs() as first_logs:
+ first = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ with capture_logs() as second_logs:
+ second = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ assert first["data"]["metadata_convergence_directive"]["rung"] == 1
+ assert second["data"]["metadata_convergence_directive"]["rung"] == 2
+ assert len([log for log in first_logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]) == 1
+ second_emits = [log for log in second_logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]
+ assert len(second_emits) == 1 and second_emits[0]["rung"] == 2
+ assert not ctx.output_contract_spine_directive_blockers_by_attempt_key
+ assert not ctx.output_contract_actuation_by_signature
+
+ @pytest.mark.asyncio
+ async def test_recorded_reject_identity_arms_and_next_author_consumes(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _code_only_ctx()
+ yaml_text = _recorded_metadata_reject_yaml()
+ assert not ctx.output_contract_armed_directive_fingerprint_by_signature
+ with capture_logs() as first_logs:
+ first = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ assert first["ok"] is False
+ assert ctx.latest_recorded_build_test_outcome.reason_code == "metadata_reject"
+ directive = first["data"]["metadata_convergence_directive"]
+ assert directive["rung"] == 1
+ assert "declared_goal" in directive["missing_fields_by_label"][_RECORDED_METADATA_REJECT_BLOCK_LABEL]
+ assert len([log for log in first_logs if log["event"] == _METADATA_LESS_DIRECTIVE_EVENT]) == 1
+ assert ctx.output_contract_armed_directive_fingerprint_by_signature
+
+ second = await _update_workflow({"workflow_yaml": yaml_text}, ctx, allow_missing_credentials=True)
+ escalated = second["data"]["metadata_convergence_directive"]
+ assert escalated["rung"] == 2
+ assert _RECORDED_METADATA_REJECT_BLOCK_LABEL in escalated["metadata_fill_in_skeleton"]
+
+
class TestWholeTrajectoryImposition:
def test_imposes_over_unscouted_browser_fill_in_selected_block(self) -> None:
ctx = _quote_ctx()
diff --git a/tests/unit/test_copilot_output_contract_actuation.py b/tests/unit/test_copilot_output_contract_actuation.py
index 8b5d59c0b..31d47c38d 100644
--- a/tests/unit/test_copilot_output_contract_actuation.py
+++ b/tests/unit/test_copilot_output_contract_actuation.py
@@ -1105,6 +1105,65 @@ def test_click_only_declick_flag_clears_when_source_becomes_observable() -> None
assert signature not in ctx.output_contract_declick_attempted_by_signature
+def test_steer_only_downgrades_advisory_run_and_grants_nothing() -> None:
+ signature = "sig_steer_advisory"
+ baseline_ctx = _advisory_ctx()
+ baseline_ctx.output_contract_reject_count_by_signature[signature] = 1
+ baseline_ctx.output_contract_armed_directive_fingerprint_by_signature[signature] = _FINGERPRINT
+ baseline = wu._actuate_output_contract_bail(
+ baseline_ctx,
+ blockers=list(_STRUCTURAL_BLOCKERS),
+ target_code=_PAGE_READ_CODE,
+ required_paths={"output.confirmation_number"},
+ signature=signature,
+ current_fingerprint=_FINGERPRINT,
+ )
+ assert baseline.kind == OutputContractActuationKind.ADVISORY_RUN
+
+ ctx = _advisory_ctx()
+ ctx.output_contract_reject_count_by_signature[signature] = 1
+ ctx.output_contract_armed_directive_fingerprint_by_signature[signature] = _FINGERPRINT
+ steered = wu._actuate_output_contract_bail(
+ ctx,
+ blockers=list(_STRUCTURAL_BLOCKERS),
+ target_code=_PAGE_READ_CODE,
+ required_paths={"output.confirmation_number"},
+ signature=signature,
+ current_fingerprint=_FINGERPRINT,
+ steer_only=True,
+ )
+ assert steered.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE
+ assert ctx.output_contract_actuation_by_signature == {}
+ assert ctx.output_contract_actuation_count_by_signature == {}
+
+
+def test_steer_only_never_terminals_click_only_and_skips_declick() -> None:
+ signature = "sig_steer_click_only"
+ ctx = _advisory_ctx()
+ first = wu._actuate_output_contract_bail(
+ ctx,
+ blockers=list(_STRUCTURAL_BLOCKERS),
+ target_code=_CLICK_ONLY_CODE,
+ required_paths={"output.confirmation_number"},
+ signature=signature,
+ current_fingerprint="fp_0",
+ steer_only=True,
+ )
+ assert first.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE
+ assert signature not in ctx.output_contract_declick_attempted_by_signature
+ second = wu._actuate_output_contract_bail(
+ ctx,
+ blockers=list(_STRUCTURAL_BLOCKERS),
+ target_code=_CLICK_ONLY_CODE,
+ required_paths={"output.confirmation_number"},
+ signature=signature,
+ current_fingerprint="fp_1",
+ steer_only=True,
+ )
+ assert second.kind == OutputContractActuationKind.STRUCTURE_DIRECTIVE
+ assert second.reason_code != OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODE
+
+
def test_observed_required_values_exact_and_lineage_match() -> None:
ctx = SimpleNamespace(scouted_output_covered_paths={"output.order.id"}, composition_page_evidence=None)
assert wu._observed_required_output_values(ctx, {"output.order.id"}) is True
@@ -1275,6 +1334,35 @@ def test_same_fresh_signature_emits_one_reject_before_typed_advisory(monkeypatch
assert ctx.output_contract_actuation_by_signature[signature] == OutputContractAdvisoryState.GRANTED
+def test_has_metadata_reject_attaches_no_metadata_convergence_directive(monkeypatch: pytest.MonkeyPatch) -> None:
+ ctx = _ladder_ctx()
+ evaluation = _make_evaluation("sig_complete_metadata", block_label="collect")
+ monkeypatch.setattr(wu, "_record_code_authoring_guardrail_reject", lambda _ctx: None)
+ complete_metadata = [
+ {
+ "block_label": "collect",
+ "declared_goal": "Collect the provider summary",
+ "claimed_outcomes": ["summary extracted"],
+ "page_dependencies": ["provider page"],
+ "completion_criteria": ["summary non-empty"],
+ "terminal_verifier_expectations": ["summary rendered"],
+ "evidence_refs": ["scout:provider-page"],
+ }
+ ]
+
+ payload = wu._record_output_contract_reject(
+ ctx,
+ evaluation,
+ summary="Value-required deficiency on a metadata-complete block.",
+ authored_structural_fingerprint="fp_complete_metadata",
+ workflow_yaml=_PAGE_READ_YAML,
+ raw_metadata=complete_metadata,
+ )
+
+ assert payload["output_contract_actuation"] == OutputContractActuationKind.STRUCTURE_DIRECTIVE.value
+ assert "metadata_convergence_directive" not in payload
+
+
def test_reject_seam_metadata_required_reaches_advisory_before_no_source_terminal() -> None:
ctx = _ladder_ctx()
signature = "sig_metadata_required"