Skip to content

Automate browser based workflows with AI

feat(SKY-12181): verify Copilot outputs the page confirms, instead of always hedging (dormant until SKY-12306) (#7312)

Andrew Neilson4ad169f64f8a
diff --git a/skyvern/forge/sdk/copilot/build_test_outcome.py b/skyvern/forge/sdk/copilot/build_test_outcome.py
index e63e76bbe..fb4893ba4 100644
--- a/skyvern/forge/sdk/copilot/build_test_outcome.py
+++ b/skyvern/forge/sdk/copilot/build_test_outcome.py
@@ -1712,6 +1712,17 @@ def _block_output_evidence_ref_label(evidence_ref: str | None) -> str:
return _bounded_ref(evidence_ref.removeprefix("block_outputs:").split(".", 1)[0])
+def registered_output_payload_binds_output_path(
+ payloads: Sequence[Mapping[str, object]],
+ output_path: str,
+) -> bool:
+ for item in payloads:
+ value, present = _registered_output_value_for_path(item, output_path)
+ if present and not _is_empty_output_value(value):
+ return True
+ return False
+
+
def _registered_output_value_for_path(item: Mapping[str, object], output_path: str) -> tuple[object | None, bool]:
value = item.get("value")
key = _safe_str(item.get("output_parameter_key"))
diff --git a/skyvern/forge/sdk/copilot/completion_criteria_store.py b/skyvern/forge/sdk/copilot/completion_criteria_store.py
index a3a574ffc..af1738fd6 100644
--- a/skyvern/forge/sdk/copilot/completion_criteria_store.py
+++ b/skyvern/forge/sdk/copilot/completion_criteria_store.py
@@ -34,6 +34,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
_normalize_deliverable_kind,
is_defer_authoring_durable_fill_criterion,
is_fallback_floor_criterion,
+ is_presence_only_requested_output_criterion,
judgment_truth_condition_key,
normalized_criterion_outcome_key,
requested_output_path_for_field,
@@ -393,12 +394,7 @@ def apply_requested_output_producer_floor(
criteria = tuple(criteria)
requested, _remaining = split_requested_output_criteria(list(criteria))
presence_only_ids = {
- criterion.id
- for criterion in requested
- if criterion.expected_output_value is None
- and criterion.expected_output_shape is None
- and criterion.deliverable_kind is None
- and criterion.mint_degrade is None
+ criterion.id for criterion in requested if is_presence_only_requested_output_criterion(criterion)
}
if not presence_only_ids:
return criteria, ()
@@ -407,7 +403,16 @@ def apply_requested_output_producer_floor(
for criterion in criteria:
if criterion.id in presence_only_ids:
rekeyed_paths.append(criterion.output_path or "")
- floored.append(replace(criterion, output_path=None, level="run", kind="outcome"))
+ floored.append(
+ replace(
+ criterion,
+ output_path=None,
+ level="run",
+ kind="outcome",
+ requested_output_floor_rekeyed=True,
+ floor_rekeyed_from_path=criterion.floor_rekeyed_from_path or criterion.output_path,
+ )
+ )
else:
floored.append(criterion)
return tuple(floored), tuple(rekeyed_paths)
diff --git a/skyvern/forge/sdk/copilot/completion_verification.py b/skyvern/forge/sdk/copilot/completion_verification.py
index e133f462f..4f3c6925f 100644
--- a/skyvern/forge/sdk/copilot/completion_verification.py
+++ b/skyvern/forge/sdk/copilot/completion_verification.py
@@ -113,6 +113,8 @@ class CompletionVerificationResult:
contingent_antecedent_output_path_by_criterion_id: dict[str, str] = field(default_factory=dict)
structural_unfired_criterion_ids: list[str] = field(default_factory=list)
degraded_criterion_ids: list[str] = field(default_factory=list)
+ floor_rekeyed_criterion_ids: list[str] = field(default_factory=list)
+ floor_rekeyed_output_path_by_criterion_id: dict[str, str] = field(default_factory=dict)
requested_output_criteria_count: int = 0
def is_fully_satisfied(self) -> bool:
@@ -1959,6 +1961,7 @@ _INDEPENDENT_REQUESTED_OUTPUT_CORROBORATOR_SOURCES = frozenset(
{"independent_page_evidence", "registered_output_parameter", "registered_artifact_content"}
)
_SELF_EMITTED_EVIDENCE_SOURCES = frozenset({"runtime_output", "same_record_context"})
+_FLOOR_REKEYED_DELIVERABLE_CREDIT_SOURCES: frozenset[EvidenceSourceKind] = frozenset({"independent_page_evidence"})
def _is_satisfied_observed_end_state_verdict(verdict: CriterionVerdict) -> bool:
@@ -2035,6 +2038,20 @@ def _has_independent_satisfied_requested_output_corroborator(
)
+def _judgment_tier_satisfied_corroborator(
+ verdict_by_id: dict[str, CriterionVerdict],
+ criterion_id: str,
+) -> CriterionVerdict | None:
+ for verdict in verdict_by_id.values():
+ if (
+ verdict.satisfied
+ and _is_requested_output_corroborator_id(verdict.criterion_id, criterion_id)
+ and verdict.evidence_source in _FLOOR_REKEYED_DELIVERABLE_CREDIT_SOURCES
+ ):
+ return verdict
+ return None
+
+
def _is_requested_output_corroborator_id(candidate_id: str, criterion_id: str) -> bool:
prefix = f"{criterion_id}__requested_output_corroborator"
if candidate_id == prefix:
@@ -2175,6 +2192,8 @@ def combine_verification_results(
contingent_path_by_id = dict(contingent_antecedent_output_path_by_criterion_id or {})
structural_unfired_ids = list(structural_unfired_criterion_ids)
degraded_ids: list[str] = []
+ floor_rekeyed_ids: list[str] = []
+ floor_rekeyed_path_by_id: dict[str, str] = {}
if run_result is not None:
contingent_ids = list(dict.fromkeys([*contingent_ids, *run_result.contingent_criterion_ids]))
contingent_on_by_id.update(run_result.contingent_on_by_criterion_id)
@@ -2183,6 +2202,8 @@ def combine_verification_results(
dict.fromkeys([*structural_unfired_ids, *run_result.structural_unfired_criterion_ids])
)
degraded_ids = list(dict.fromkeys([*degraded_ids, *run_result.degraded_criterion_ids]))
+ floor_rekeyed_ids = list(dict.fromkeys([*floor_rekeyed_ids, *run_result.floor_rekeyed_criterion_ids]))
+ floor_rekeyed_path_by_id.update(run_result.floor_rekeyed_output_path_by_criterion_id)
if run_result is not None and run_result.status != "evaluated":
return CompletionVerificationResult(
status=run_result.status,
@@ -2193,6 +2214,8 @@ def combine_verification_results(
contingent_antecedent_output_path_by_criterion_id=contingent_path_by_id,
structural_unfired_criterion_ids=structural_unfired_ids,
degraded_criterion_ids=degraded_ids,
+ floor_rekeyed_criterion_ids=floor_rekeyed_ids,
+ floor_rekeyed_output_path_by_criterion_id=floor_rekeyed_path_by_id,
requested_output_criteria_count=requested_output_criteria_count,
)
if run_result is not None:
@@ -2223,6 +2246,8 @@ def combine_verification_results(
contingent_antecedent_output_path_by_criterion_id=contingent_path_by_id,
structural_unfired_criterion_ids=structural_unfired_ids,
degraded_criterion_ids=degraded_ids,
+ floor_rekeyed_criterion_ids=floor_rekeyed_ids,
+ floor_rekeyed_output_path_by_criterion_id=floor_rekeyed_path_by_id,
requested_output_criteria_count=requested_output_criteria_count,
)
@@ -2336,6 +2361,47 @@ def zero_requested_output_criteria_credit(
)
+@dataclass(frozen=True)
+class FloorRekeyedDeliverableCredit:
+ criterion_ids: tuple[str, ...]
+ evidence_sources: tuple[str, ...]
+ evidence_refs: tuple[str, ...]
+ output_paths: tuple[str, ...]
+
+
+def floor_rekeyed_deliverable_credit(
+ result: CompletionVerificationResult | None,
+) -> FloorRekeyedDeliverableCredit | None:
+ """Credit a floor-rekeyed presence-only deliverable on a fully satisfied run when every marked
+ criterion carries its own satisfied requested-output corroborator whose derived evidence_source is
+ judgment-tier. Registered-output and self-emitted corroboration earn no credit and keep hedging."""
+ if result is None or result.status != "evaluated" or not result.is_fully_satisfied():
+ return None
+ marked_ids = list(dict.fromkeys(result.floor_rekeyed_criterion_ids))
+ if not marked_ids:
+ return None
+ verdict_by_id = {verdict.criterion_id: verdict for verdict in result.verdicts}
+ evidence_sources: list[str] = []
+ evidence_refs: list[str] = []
+ output_paths: list[str] = []
+ for criterion_id in marked_ids:
+ marked = verdict_by_id.get(criterion_id)
+ if marked is None:
+ return None
+ corroborator = _judgment_tier_satisfied_corroborator(verdict_by_id, criterion_id)
+ if corroborator is None or corroborator.evidence_source is None:
+ return None
+ evidence_sources.append(corroborator.evidence_source)
+ evidence_refs.append(corroborator.evidence_ref or "")
+ output_paths.append(result.floor_rekeyed_output_path_by_criterion_id.get(criterion_id, ""))
+ return FloorRekeyedDeliverableCredit(
+ criterion_ids=tuple(marked_ids),
+ evidence_sources=tuple(evidence_sources),
+ evidence_refs=tuple(evidence_refs),
+ output_paths=tuple(output_paths),
+ )
+
+
def carry_degraded_criterion_ids(
result: CompletionVerificationResult,
criteria: Iterable[CompletionCriterion],
@@ -2347,6 +2413,25 @@ def carry_degraded_criterion_ids(
return replace(result, degraded_criterion_ids=merged)
+def carry_floor_rekeyed_criterion_ids(
+ result: CompletionVerificationResult,
+ criteria: Iterable[CompletionCriterion],
+) -> CompletionVerificationResult:
+ marked = [criterion for criterion in criteria if criterion.requested_output_floor_rekeyed]
+ if not marked and not result.floor_rekeyed_criterion_ids:
+ return result
+ merged = list(dict.fromkeys([*result.floor_rekeyed_criterion_ids, *(criterion.id for criterion in marked)]))
+ output_path_by_id = dict(result.floor_rekeyed_output_path_by_criterion_id)
+ for criterion in marked:
+ if criterion.floor_rekeyed_from_path:
+ output_path_by_id.setdefault(criterion.id, criterion.floor_rekeyed_from_path)
+ return replace(
+ result,
+ floor_rekeyed_criterion_ids=merged,
+ floor_rekeyed_output_path_by_criterion_id=output_path_by_id,
+ )
+
+
async def evaluate_completion_criteria(
criteria: list[CompletionCriterion],
snapshot: RunEvidenceSnapshot,
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index 5f9272465..7fd62293b 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -297,6 +297,8 @@ class CompletionCriterion:
requested_output_corroborator: bool = False
mint_degrade: MintDegrade | None = None
judgment_truth_condition: JudgmentTruthCondition | None = None
+ requested_output_floor_rekeyed: bool = False
+ floor_rekeyed_from_path: str | None = None
@dataclass
@@ -1411,6 +1413,15 @@ def _non_requested_output_run_corroborator(criterion: CompletionCriterion) -> bo
)
+def is_presence_only_requested_output_criterion(criterion: CompletionCriterion) -> bool:
+ return (
+ criterion.expected_output_value is None
+ and criterion.expected_output_shape is None
+ and criterion.deliverable_kind is None
+ and criterion.mint_degrade is None
+ )
+
+
def _validation_classification_output_path(output_path: str | None) -> bool:
return (
output_path in _VALIDATION_CLASSIFICATION_BOOLEAN_OUTPUT_TARGETS
diff --git a/skyvern/forge/sdk/copilot/tools/completion.py b/skyvern/forge/sdk/copilot/tools/completion.py
index 4daf0a6e4..88ce7b3b4 100644
--- a/skyvern/forge/sdk/copilot/tools/completion.py
+++ b/skyvern/forge/sdk/copilot/tools/completion.py
@@ -21,6 +21,7 @@ from skyvern.forge.sdk.copilot.completion_verification import (
_contingent_metadata_for_criteria,
_is_structural_requested_output_abstention,
carry_degraded_criterion_ids,
+ carry_floor_rekeyed_criterion_ids,
combine_verification_results,
evaluate_completion_criteria,
grade_definition_criteria,
@@ -414,6 +415,7 @@ async def _maybe_run_completion_verification_from_page_observation(
if _classifier_status(copilot_ctx) == "fallback" and not run_criteria:
verification = _no_gradeable_run_plane_result(criterion_ids)
verification = carry_degraded_criterion_ids(verification, criteria)
+ verification = carry_floor_rekeyed_criterion_ids(verification, criteria)
copilot_ctx.completion_verification_result = verification
record_completion_verification(copilot_ctx, verification)
_record_adjudication_on_turn_state(copilot_ctx, verification)
@@ -570,6 +572,7 @@ async def _maybe_run_completion_verification_from_page_observation(
)
verification = carry_degraded_criterion_ids(verification, criteria)
+ verification = carry_floor_rekeyed_criterion_ids(verification, criteria)
if (
isinstance(existing, CompletionVerificationResult)
and not verification.is_fully_satisfied()
@@ -1202,7 +1205,10 @@ async def _maybe_run_completion_verification(
criteria = _completion_verification_criteria(copilot_ctx)
criteria = _reconcile_download_completion_criterion(copilot_ctx, result, criteria)
verification = await _completion_verification_from_run_result(copilot_ctx, result, handler_start, criteria)
- return carry_degraded_criterion_ids(verification, criteria) if verification is not None else None
+ if verification is None:
+ return None
+ verification = carry_degraded_criterion_ids(verification, criteria)
+ return carry_floor_rekeyed_criterion_ids(verification, criteria)
async def _completion_verification_from_run_result(
diff --git a/skyvern/forge/sdk/copilot/tools/run_execution.py b/skyvern/forge/sdk/copilot/tools/run_execution.py
index 415a00c5e..b1c322bed 100644
--- a/skyvern/forge/sdk/copilot/tools/run_execution.py
+++ b/skyvern/forge/sdk/copilot/tools/run_execution.py
@@ -33,6 +33,7 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (
latest_recorded_build_test_outcome_repeated,
record_build_test_outcome,
recorded_outcome_from_run_blocks_result,
+ registered_output_payload_binds_output_path,
run_backed_repair_evidence_exists,
)
from skyvern.forge.sdk.copilot.challenge_evidence import (
@@ -57,6 +58,7 @@ from skyvern.forge.sdk.copilot.completion_verification import (
CriterionVerdict,
DeliveredUnverifiedTerminalState,
degraded_contract_delivered_unverified_terminal_state,
+ floor_rekeyed_deliverable_credit,
only_structural_requested_output_abstentions,
zero_requested_output_criteria_credit,
)
@@ -3178,6 +3180,36 @@ def _zero_requested_output_criteria_delivered_unverified(
has_meaningful_registered_output=_has_meaningful_registered_output_payload(data),
):
return None
+ credit = floor_rekeyed_deliverable_credit(completion_verification)
+ if credit is not None:
+ payloads = _registered_output_parameter_payloads(data)
+ payload_keys = sorted(
+ {key for item in payloads for key in [item.get("output_parameter_key")] if isinstance(key, str) and key}
+ )
+ unbound_paths = [
+ output_path
+ for output_path in credit.output_paths
+ if output_path and not registered_output_payload_binds_output_path(payloads, output_path)
+ ]
+ if unbound_paths:
+ LOG.info(
+ "copilot.completion.floor_rekeyed_credit_payload_unbound",
+ workflow_run_id=workflow_run_id,
+ criterion_ids=list(credit.criterion_ids),
+ unbound_output_paths=unbound_paths,
+ registered_output_keys=payload_keys,
+ )
+ else:
+ LOG.info(
+ "copilot.completion.floor_rekeyed_deliverable_credit",
+ workflow_run_id=workflow_run_id,
+ criterion_ids=list(credit.criterion_ids),
+ evidence_sources=list(credit.evidence_sources),
+ evidence_refs=list(credit.evidence_refs),
+ credited_output_paths=list(credit.output_paths),
+ registered_output_keys=payload_keys,
+ )
+ return None
copilot_ctx.last_test_suspicious_success = False
copilot_ctx.last_test_failure_reason = None
copilot_ctx.suspicious_success_nudge_count = 0
diff --git a/tests/unit/test_copilot_completion_verification.py b/tests/unit/test_copilot_completion_verification.py
index db3f62732..08a550867 100644
--- a/tests/unit/test_copilot_completion_verification.py
+++ b/tests/unit/test_copilot_completion_verification.py
@@ -19,9 +19,12 @@ from skyvern.forge.sdk.copilot.agent import (
_verified_workflow_or_none,
)
from skyvern.forge.sdk.copilot.completion_criteria_store import (
+ StoredCriteriaSet,
+ StoredCriteriaSnapshot,
apply_requested_output_producer_floor,
criteria_from_json,
criteria_to_json,
+ reconcile_completion_criteria,
)
from skyvern.forge.sdk.copilot.completion_output_grounding import (
_schema_boolean_output_paths,
@@ -34,12 +37,16 @@ from skyvern.forge.sdk.copilot.completion_verification import (
CompletionVerificationResult,
CriterionVerdict,
DeliveredUnverifiedTerminalState,
+ EvidenceSourceKind,
+ FloorRekeyedDeliverableCredit,
RunEvidenceSnapshot,
_coerce_result,
_structured_record_has_identifier,
+ carry_floor_rekeyed_criterion_ids,
combine_verification_results,
degraded_contract_delivered_unverified_terminal_state,
evaluate_completion_criteria,
+ floor_rekeyed_deliverable_credit,
grade_definition_criteria,
grade_fallback_floor_reached_end_state_criteria,
grade_present_value_criteria,
@@ -80,6 +87,8 @@ from skyvern.forge.sdk.copilot.request_policy import (
CompletionCriterion,
JudgmentTruthCondition,
RequestPolicy,
+ _apply_classifier_typed_requested_output_corroborators,
+ _apply_requested_output_completion_criteria,
_parse_completion_criteria,
build_classifier_fallback_floor,
)
@@ -3346,6 +3355,491 @@ def test_zero_requested_output_criteria_credit_requires_evaluated_full_satisfact
)
+_PAGE_EVIDENCE_DELIVERABLE_REF = "block_outputs:post_run_page_observation.document_name"
+
+
+def _corroborated_abstention_verdicts(
+ *,
+ corroborator_source: EvidenceSourceKind | None,
+ marked_id: str = "deliverable",
+ marked_output_path: str = "output.document_name",
+ marked_evidence_ref: str = _PAGE_EVIDENCE_DELIVERABLE_REF,
+ corroborator_evidence_ref: str | None = None,
+) -> list[CriterionVerdict]:
+ return [
+ CriterionVerdict(
+ criterion_id=marked_id,
+ state="unsatisfied",
+ reason_code="structurally_abstained",
+ evidence_ref=marked_evidence_ref,
+ output_path=marked_output_path,
+ has_exact_value=False,
+ ),
+ CriterionVerdict(
+ criterion_id=f"{marked_id}__requested_output_corroborator",
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_source=corroborator_source,
+ evidence_ref=corroborator_evidence_ref,
+ ),
+ ]
+
+
+def _observed_end_state_verdict(cid: str = "deliverable") -> CriterionVerdict:
+ return CriterionVerdict(
+ criterion_id=cid,
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_ref="observed_end_state_url",
+ )
+
+
+def _floored_run_plane_verdicts(
+ *,
+ corroborator_source: EvidenceSourceKind | None,
+ marked_id: str = "deliverable",
+ corroborator_evidence_ref: str | None = _PAGE_EVIDENCE_DELIVERABLE_REF,
+) -> list[CriterionVerdict]:
+ return [
+ CriterionVerdict(
+ criterion_id=marked_id,
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_ref="block_outputs:post_run_page_observation",
+ evidence_source="independent_page_evidence",
+ ),
+ CriterionVerdict(
+ criterion_id=f"{marked_id}__requested_output_corroborator",
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_source=corroborator_source,
+ evidence_ref=corroborator_evidence_ref,
+ ),
+ ]
+
+
+def _floor_rekeyed_result(
+ verdicts: list[CriterionVerdict],
+ marked_ids: list[str],
+ output_path_by_id: dict[str, str] | None = None,
+) -> CompletionVerificationResult:
+ return CompletionVerificationResult(
+ status="evaluated",
+ criterion_ids=[verdict.criterion_id for verdict in verdicts],
+ verdicts=list(verdicts),
+ floor_rekeyed_criterion_ids=list(marked_ids),
+ floor_rekeyed_output_path_by_criterion_id=dict(output_path_by_id or {}),
+ )
+
+
+def test_floor_rekeyed_credit_grants_on_independent_page_evidence() -> None:
+ result = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(
+ corroborator_source="independent_page_evidence",
+ corroborator_evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ ["deliverable"],
+ )
+
+ credit = floor_rekeyed_deliverable_credit(result)
+
+ assert isinstance(credit, FloorRekeyedDeliverableCredit)
+ assert credit.criterion_ids == ("deliverable",)
+ assert credit.evidence_sources == ("independent_page_evidence",)
+ assert credit.evidence_refs == (_PAGE_EVIDENCE_DELIVERABLE_REF,)
+
+
+def test_floor_rekeyed_credit_grants_on_run_plane_marked_verdict() -> None:
+ result = _floor_rekeyed_result(
+ _floored_run_plane_verdicts(corroborator_source="independent_page_evidence"),
+ ["deliverable"],
+ {"deliverable": "output.document_name"},
+ )
+
+ credit = floor_rekeyed_deliverable_credit(result)
+
+ assert credit is not None
+ assert credit.evidence_sources == ("independent_page_evidence",)
+ assert credit.output_paths == ("output.document_name",)
+
+
+def test_floor_rekeyed_credit_withholds_without_corroborator() -> None:
+ result = _floor_rekeyed_result([_observed_end_state_verdict()], ["deliverable"])
+
+ assert result.is_fully_satisfied() is True
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+ "corroborator_source",
+ ["registered_output_parameter", "registered_artifact_content"],
+)
+def test_floor_rekeyed_credit_withholds_on_registered_corroborator_source(
+ corroborator_source: EvidenceSourceKind,
+) -> None:
+ result = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(corroborator_source=corroborator_source),
+ ["deliverable"],
+ )
+
+ assert result.is_fully_satisfied() is True
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+ "corroborator_source",
+ ["runtime_output", "same_record_context", None],
+)
+def test_floor_rekeyed_credit_withholds_on_self_emitted_corroborator(
+ corroborator_source: EvidenceSourceKind | None,
+) -> None:
+ result = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(corroborator_source=corroborator_source),
+ ["deliverable"],
+ )
+
+ assert result.is_fully_satisfied() is True
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+def test_floor_rekeyed_credit_requires_every_marked_id_page_grounded() -> None:
+ verdicts = [
+ *_corroborated_abstention_verdicts(
+ corroborator_source="independent_page_evidence",
+ marked_id="deliverable_a",
+ corroborator_evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ *_corroborated_abstention_verdicts(corroborator_source="runtime_output", marked_id="deliverable_b"),
+ ]
+ result = _floor_rekeyed_result(verdicts, ["deliverable_a", "deliverable_b"])
+
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+def test_floor_rekeyed_credit_none_without_marked_ids() -> None:
+ result = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(corroborator_source="independent_page_evidence"),
+ [],
+ )
+
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+def test_floor_rekeyed_credit_ignores_corroborator_scoped_to_other_id() -> None:
+ verdicts = [
+ *_corroborated_abstention_verdicts(corroborator_source="runtime_output"),
+ CriterionVerdict(
+ criterion_id="unrelated__requested_output_corroborator",
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_source="independent_page_evidence",
+ evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ ]
+ result = _floor_rekeyed_result(verdicts, ["deliverable"])
+
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+def test_floor_rekeyed_credit_none_when_partial_satisfaction() -> None:
+ verdicts = [
+ *_corroborated_abstention_verdicts(
+ corroborator_source="independent_page_evidence",
+ corroborator_evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ CriterionVerdict(criterion_id="other", state="unsatisfied", reason_code="no_evidence"),
+ ]
+ result = _floor_rekeyed_result(verdicts, ["deliverable"])
+
+ assert result.is_fully_satisfied() is False
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+def test_floor_rekeyed_credit_none_for_kill_shape() -> None:
+ assert floor_rekeyed_deliverable_credit(_evaluated(("login", True))) is None
+
+
+def test_floor_rekeyed_credit_does_not_mutate_result() -> None:
+ result = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(
+ corroborator_source="independent_page_evidence",
+ corroborator_evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ ["deliverable"],
+ )
+ before = replace(result)
+
+ floor_rekeyed_deliverable_credit(result)
+
+ assert result == before
+
+
+def test_carry_floor_rekeyed_ids_from_marked_criteria() -> None:
+ criteria = [
+ CompletionCriterion(id="deliverable", outcome="Document name is shown.", requested_output_floor_rekeyed=True),
+ CompletionCriterion(id="plain", outcome="Login succeeds."),
+ ]
+ result = CompletionVerificationResult(status="evaluated", criterion_ids=["deliverable", "plain"])
+
+ carried = carry_floor_rekeyed_criterion_ids(result, criteria)
+
+ assert carried.floor_rekeyed_criterion_ids == ["deliverable"]
+
+
+def test_combine_threads_floor_rekeyed_ids_from_run_result() -> None:
+ run_result = CompletionVerificationResult(
+ status="evaluated",
+ criterion_ids=["deliverable"],
+ verdicts=[CriterionVerdict(criterion_id="deliverable", state="satisfied", reason_code="evidence_confirms")],
+ floor_rekeyed_criterion_ids=["deliverable"],
+ )
+
+ combined = combine_verification_results(["deliverable"], run_result, [])
+
+ assert combined.floor_rekeyed_criterion_ids == ["deliverable"]
+
+
+def test_floor_marker_re_derived_across_persistence_round_trip() -> None:
+ fresh = [
+ CompletionCriterion(id="deliverable", outcome="Document name is shown.", output_path="output.document_name")
+ ]
+
+ floored_first, rekeyed_first = apply_requested_output_producer_floor(fresh)
+ assert rekeyed_first == ("output.document_name",)
+ assert floored_first[0].requested_output_floor_rekeyed is True
+
+ persisted = criteria_to_json(fresh)
+ assert persisted[0]["output_path"] == "output.document_name"
+ assert "requested_output_floor_rekeyed" not in persisted[0]
+
+ stored = criteria_from_json(persisted)
+ assert stored[0].requested_output_floor_rekeyed is False
+
+ snapshot = StoredCriteriaSnapshot(
+ active=StoredCriteriaSet(set_id="set_1", goal_epoch=1, criteria=stored), next_epoch=2
+ )
+ decision = reconcile_completion_criteria(snapshot, fresh, actionable=True)
+ assert decision.action == "adopt_stored"
+
+ floored_again, rekeyed_again = apply_requested_output_producer_floor(decision.criteria)
+ assert rekeyed_again == ("output.document_name",)
+ assert floored_again[0].requested_output_floor_rekeyed is True
+
+
+def test_floor_idempotent_on_already_floored_set() -> None:
+ fresh = [
+ CompletionCriterion(id="deliverable", outcome="Document name is shown.", output_path="output.document_name")
+ ]
+ floored_once, _ = apply_requested_output_producer_floor(fresh)
+
+ floored_twice, rekeyed_twice = apply_requested_output_producer_floor(floored_once)
+
+ assert rekeyed_twice == ()
+ assert floored_twice[0].requested_output_floor_rekeyed is True
+
+
+def test_floor_rekeyed_credit_grants_when_floor_path_binds_delivered_payload() -> None:
+ ctx = _ctx_with_blocks("code")
+ verification = _floor_rekeyed_result(
+ _floored_run_plane_verdicts(corroborator_source="independent_page_evidence"),
+ ["deliverable"],
+ {"deliverable": "output.document_name"},
+ )
+
+ with capture_logs() as logs:
+ recorded = _record_run_blocks_result(
+ ctx,
+ _registered_output_result({"document_name": "Resale certificate"}),
+ completion_verification=verification,
+ )
+
+ assert recorded is not None
+ assert recorded.verdict == "demonstrated"
+ granted = [entry for entry in logs if entry.get("event") == "copilot.completion.floor_rekeyed_deliverable_credit"]
+ assert granted and granted[0]["credited_output_paths"] == ["output.document_name"]
+ assert granted[0]["registered_output_keys"] == ["bill_statement"]
+
+
+def test_floor_rekeyed_credit_withheld_when_floor_path_absent_from_payload() -> None:
+ ctx = _ctx_with_blocks("code")
+ verification = _floor_rekeyed_result(
+ _floored_run_plane_verdicts(corroborator_source="independent_page_evidence"),
+ ["deliverable"],
+ {"deliverable": "output.document_name"},
+ )
+
+ with capture_logs() as logs:
+ recorded = _record_run_blocks_result(
+ ctx, _registered_output_result({"summary": "raw"}), completion_verification=verification
+ )
+
+ assert recorded is not None
+ assert recorded.verdict == "not_evaluated"
+ assert ctx.delivered_unverified_terminal is True
+ unbound = [
+ entry for entry in logs if entry.get("event") == "copilot.completion.floor_rekeyed_credit_payload_unbound"
+ ]
+ assert unbound and unbound[0]["unbound_output_paths"] == ["output.document_name"]
+ assert any(entry.get("event") == "copilot.completion.zero_requested_output_credit_withheld" for entry in logs)
+
+
+def test_floor_rekeyed_deliverable_reaches_verified_success() -> None:
+ ctx = _ctx_with_blocks("code")
+ verification = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(
+ corroborator_source="independent_page_evidence",
+ corroborator_evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ ),
+ ["deliverable"],
+ )
+
+ with capture_logs() as logs:
+ recorded = _record_run_blocks_result(
+ ctx, _registered_output_result({"summary": "raw"}), completion_verification=verification
+ )
+
+ assert recorded is not None
+ assert recorded.verdict == "demonstrated"
+ assert ctx.delivered_unverified_terminal is False
+ assert ctx.turn_halt is None
+ granted = [entry for entry in logs if entry.get("event") == "copilot.completion.floor_rekeyed_deliverable_credit"]
+ assert granted and granted[0]["criterion_ids"] == ["deliverable"]
+ assert granted[0]["evidence_sources"] == ["independent_page_evidence"]
+ assert not any(entry.get("event") == "copilot.completion.zero_requested_output_credit_withheld" for entry in logs)
+
+
+def _floored_policy_criteria(criteria: list[CompletionCriterion], user_message: str) -> list[CompletionCriterion]:
+ policy = RequestPolicy(completion_criteria=criteria)
+ _apply_requested_output_completion_criteria(policy, user_message)
+ _apply_classifier_typed_requested_output_corroborators(policy)
+ floored, _rekeyed = apply_requested_output_producer_floor(policy.completion_criteria)
+ return list(floored)
+
+
+def _satisfied_page_evidence_result(criteria: list[CompletionCriterion]) -> CompletionVerificationResult:
+ verdicts = [
+ CriterionVerdict(
+ criterion_id=criterion.id,
+ state="satisfied",
+ reason_code="evidence_confirms",
+ evidence_source="independent_page_evidence",
+ evidence_ref=_PAGE_EVIDENCE_DELIVERABLE_REF,
+ )
+ for criterion in criteria
+ ]
+ result = CompletionVerificationResult(
+ status="evaluated",
+ criterion_ids=[criterion.id for criterion in criteria],
+ verdicts=verdicts,
+ )
+ return carry_floor_rekeyed_criterion_ids(result, criteria)
+
+
+def test_floor_rekeyed_credit_is_inert_when_the_seam_mints_no_per_deliverable_corroborator() -> None:
+ criteria = _floored_policy_criteria(
+ [
+ CompletionCriterion(
+ id="run_end_state",
+ outcome="The run opens the order-level document list and selects the demand document row.",
+ )
+ ],
+ "Return a final record with document name.",
+ )
+ marked_id = "__copilot_requested_output__output_document_name"
+ result = _satisfied_page_evidence_result(criteria)
+
+ assert result.floor_rekeyed_criterion_ids == [marked_id]
+ assert result.is_fully_satisfied() is True
+ assert [criterion.id for criterion in criteria if criterion.requested_output_corroborator] == []
+ assert floor_rekeyed_deliverable_credit(result) is None
+
+
+async def test_floor_rekeyed_deliverable_is_credited_through_the_verification_producer(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ policy = RequestPolicy(
+ completion_criteria=[
+ CompletionCriterion(
+ id="c1",
+ outcome="The run captures the document status from the order documents list.",
+ output_path="output.doc_status",
+ )
+ ]
+ )
+ _apply_classifier_typed_requested_output_corroborators(policy)
+ floored, rekeyed_paths = apply_requested_output_producer_floor(policy.completion_criteria)
+ policy.completion_criteria = list(floored)
+ assert rekeyed_paths == ("output.doc_status",)
+ assert "c1__requested_output_corroborator" in {criterion.id for criterion in floored}
+
+ async def handler(**_: object) -> dict:
+ return {
+ "verdicts": [
+ {
+ "criterion_id": criterion.id,
+ "satisfied": True,
+ "reason_code": "evidence_confirms",
+ "evidence_ref": "block_outputs:post_run_page_observation.visible_text_excerpt",
+ }
+ for criterion in policy.completion_criteria
+ ]
+ }
+
+ async def handler_lookup(_ctx: object) -> object:
+ return handler
+
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools.completion._completion_verification_handler",
+ handler_lookup,
+ )
+ ctx = _ctx_with_blocks("code")
+ ctx.request_policy = policy
+ ctx.composition_page_evidence = {
+ "workflow_run_id": "wr_x",
+ "observed_after_workflow_run": True,
+ "visible_text_excerpt": "Resale certificate - Delivered",
+ }
+ result = _registered_output_result({"doc_status": "Delivered"})
+
+ verification = await _maybe_run_completion_verification(ctx, result, time.monotonic())
+
+ assert verification is not None
+ assert verification.floor_rekeyed_criterion_ids == ["c1"]
+ assert verification.floor_rekeyed_output_path_by_criterion_id == {"c1": "output.doc_status"}
+ assert verification.is_fully_satisfied() is True
+
+ with capture_logs() as logs:
+ recorded = _record_run_blocks_result(ctx, result, completion_verification=verification)
+
+ assert recorded is not None
+ assert recorded.verdict == "demonstrated"
+ granted = [entry for entry in logs if entry.get("event") == "copilot.completion.floor_rekeyed_deliverable_credit"]
+ assert granted and granted[0]["criterion_ids"] == ["c1"]
+ assert granted[0]["evidence_sources"] == ["independent_page_evidence"]
+
+
+def test_floor_rekeyed_runtime_output_only_still_withholds() -> None:
+ ctx = _ctx_with_blocks("code")
+ verification = _floor_rekeyed_result(
+ _corroborated_abstention_verdicts(corroborator_source="runtime_output"),
+ ["deliverable"],
+ )
+
+ with capture_logs() as logs:
+ recorded = _record_run_blocks_result(
+ ctx, _registered_output_result({"summary": "raw"}), completion_verification=verification
+ )
+
+ assert recorded is not None
+ assert recorded.verdict == "not_evaluated"
+ assert ctx.delivered_unverified_terminal is True
+ assert ctx.turn_halt is not None
+ assert ctx.turn_halt.kind.value == "delivered_unverified"
+ assert any(entry.get("event") == "copilot.completion.zero_requested_output_credit_withheld" for entry in logs)
+ assert not any(entry.get("event") == "copilot.completion.floor_rekeyed_deliverable_credit" for entry in logs)
+
+
def test_zero_requested_output_criteria_withholds_verified_success() -> None:
ctx = _ctx_with_blocks("code")