# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-attestation-that-verifies-cryptographically-but-violates-its-own-epistemic-warrant-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-attestation-that-verifies-cryptographically-but-violates-its-own-epistemic-warrant-chain.warrant.json
# Markdown SHA-256:   b205c297c6b9cdbad4ddf946135ededdcd640b815a6a84d1bbf81a018b7ce385
# Code AST SHA-256:   ae2ef115b8eb006581f707377a1a93f906a81177338b54c7b8b8af17c0c6fc7e
# Verified Classes:   WarrantChainGapError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class WarrantChainGapError(RuntimeError):
    """Raised when a federated attestation verifies cryptographically but the
    receiver cannot reconstruct the full constraint set of the witnessed claim."""
    pass


def admit_federated_attestation(attestation, local_policy, parser):
    # Step 1: authenticity — necessary, not sufficient.
    if not attestation.verify_signature(local_policy.trust_anchor):
        raise WarrantChainGapError(
            f"attestation {attestation.id} failed signature check against "
            f"anchor {local_policy.trust_anchor.id}"
        )

    # Step 2: warrant — reconstruct constraints from the witnessed payload.
    witness_payload = attestation.witness_payload
    if witness_payload is None:
        raise WarrantChainGapError(
            f"attestation {attestation.id} carries no witnessed payload; "
            f"cannot reconstruct constraint set — treat as unconstrained-refused"
        )

    try:
        witnessed_constraints = parser.parse_constraints(witness_payload)
    except parser.UnknownDialectError as e:
        # The partial case: signature ok, warrant indeterminate.
        # Refuse constrained operations; do not default to accept.
        raise WarrantChainGapError(
            f"attestation {attestation.id}: witness dialect unparseable "
            f"({e}); re-attestation may have widened the grant"
        ) from e

    # Step 3: fidelity — re-attested claim must not exceed witnessed grant.
    reattested = set(attestation.claimed_constraints)
    witnessed = set(witnessed_constraints)
    widened = reattested - witnessed
    if widened:
        raise WarrantChainGapError(
            f"attestation {attestation.id} is widened: re-attested constraints "
            f"{sorted(widened)} absent from witnessed grant; refusing and "
            f"flagging authority {attestation.authority_id}"
        )

    # Step 4: narrow to the witnessed set and admit.
    return local_policy.narrow_to(witnessed)
