# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-cross-org-a2a-trust-root-bridge-has-silently-downgraded-aaif-attestation-strength.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-cross-org-a2a-trust-root-bridge-has-silently-downgraded-aaif-attestation-strength.warrant.json
# Markdown SHA-256:   4be5ea3f75cd1d93658cab182a0be930fb378e82aa00dc98c863a7cbca7f95ea
# Code AST SHA-256:   911c31bb8a685030211c703d61540984d3bc5f2a654160b3f0b72a9939ddadab
# Verified Classes:   AttestationDowngradeError, BridgedCard
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class AttestationDowngradeError(RuntimeError):
    """Raised when a bridged A2A card's resolved trust root does not match
    the AAIF credential's signed required_root, i.e. attestation strength
    was silently reduced during cross-org re-anchoring."""


@dataclass(frozen=True)
class BridgedCard:
    issuer: str
    credential_uri: str
    required_root: str      # signed inside the AAIF credential by original issuer
    resolved_root: str      # root the verifier actually anchored against
    attestation_depth: int  # 1 = single software-key root, N = chained


def verify_cross_org_attestation(card: BridgedCard, policy_allows_downgrade: bool = False) -> None:
    if card.resolved_root != card.required_root:
        if not policy_allows_downgrade:
            raise AttestationDowngradeError(
                f"attestation downgrade on bridge: credential {card.credential_uri} "
                f"required_root={card.required_root!r} but verifier anchored "
                f"resolved_root={card.resolved_root!r} via issuer {card.issuer!r} "
                f"(depth={card.attestation_depth}); refusing action"
            )
        # Policy explicitly permits downgrade: record it, do not absorb silently.
        emit_downgrade_event(card)
    if card.attestation_depth < 1:
        raise AttestationDowngradeError(
            f"degenerate attestation chain for {card.credential_uri}: depth={card.attestation_depth}"
        )


def emit_downgrade_event(card: BridgedCard) -> None:
    # Structured, named event so policy engines can escalate rather than pass.
    _ = {
        "event": "AttestationDowngrade",
        "credential": card.credential_uri,
        "from_root": card.required_root,
        "to_root": card.resolved_root,
        "depth": card.attestation_depth,
    }
