# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-silent-capability-downgrade-when-a2a-v1-0-agent-card-discovery-resolves-against-a-stale-or-partially-reachable-peer-regi.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-silent-capability-downgrade-when-a2a-v1-0-agent-card-discovery-resolves-against-a-stale-or-partially-reachable-peer-regi.warrant.json
# Markdown SHA-256:   4548401bbb32dc768526432e06e32980bff50a1ca8ffc798eb2a00eb5338b939
# Code AST SHA-256:   83e88fab224cd7450b71741fb000d63f0ea153df37fafc50083eacb1e0df2b32
# Verified Classes:   CapabilityDowngradeError, ScopeBearingFieldMissingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityDowngradeError(RuntimeError):
    """Raised when an A2A peer executes a skill other than the one advertised in the discovered AgentCard."""
    pass


class ScopeBearingFieldMissingError(CapabilityDowngradeError):
    """Raised when a scope-bearing input field was silently dropped by peer execution."""
    pass


SCOPE_BEARING_FIELDS = frozenset({
    "pageRange", "modelTier", "maxTokens", "language",
    "redactionPolicy", "outputFormat", "filter",
})


def verify_execution_attestation(
    advertised_card: dict,
    task_response: dict,
    submitted_input: dict,
) -> None:
    advertised_ids = {s["id"] for s in advertised_card.get("skills", [])}
    executed = task_response.get("metadata", {}).get("skillExecuted")

    if executed is None:
        raise CapabilityDowngradeError(
            f"peer omitted skillExecuted attestation; cannot confirm "
            f"advertised skills {sorted(advertised_ids)} were enforced"
        )

    if executed not in advertised_ids:
        raise CapabilityDowngradeError(
            f"advertised skill set {sorted(advertised_ids)} but peer executed "
            f"{executed!r}; discovery-enforcement divergence — re-discover with "
            f"If-None-Match against executing replica ETag"
        )

    echoed = set(task_response.get("metadata", {}).get("inputFieldsHonored", []))
    dropped_scope = (SCOPE_BEARING_FIELDS & set(submitted_input)) - echoed
    if dropped_scope:
        raise ScopeBearingFieldMissingError(
            f"peer executed {executed!r} but silently dropped scope-bearing "
            f"fields {sorted(dropped_scope)}; artifacts have inflated scope and "
            f"must not reach the verifier"
        )
