# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-agent-identity-attestation-the-aaif-credential-chain-depth-versus-trust-domain-epoch-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-agent-identity-attestation-the-aaif-credential-chain-depth-versus-trust-domain-epoch-boundary.warrant.json
# Markdown SHA-256:   32befda9b4c0bdc339127c9e169763429776cd2a53d6fde8f2197cfdbcf73051
# Code AST SHA-256:   d52109e2c704793c7a42fe6bd376cba9c18b2b8dad74b37de3c7954338413ea4
# Verified Classes:   DelegationAuthorityEpochSkew, EpochStateStale
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class DelegationAuthorityEpochSkew(RuntimeError):
    """Terminal refusal: a delegated scope exceeds the delegating domain's
    current AAIF policy epoch. Not retryable; do not loop."""
    pass


class EpochStateStale(RuntimeError):
    """Retryable deferral: cached epoch state exceeds max staleness."""
    pass


def verify_federated_attestation(chain, epoch_cache, max_epoch_staleness_s, now):
    # Condition 1: anchor termination (assumed pre-checked by caller).
    # Condition 2: signatures and revocation (assumed pre-checked).
    # Condition 3+4: per-edge epoch authority, against a pinned snapshot.
    pinned = {}
    for edge in chain.delegation_edges:
        domain = edge.issuer_domain
        if domain not in pinned:
            state = epoch_cache.get(domain)
            if state is None or (now - state.fetched_at) > max_epoch_staleness_s:
                raise EpochStateStale(
                    f"epoch state for {domain} stale by "
                    f"{now - state.fetched_at if state else 'unknown'}s"
                )
            pinned[domain] = state
        current = pinned[domain]
        if not current.delegable_scopes.issuperset(edge.delegated_scopes):
            excess = edge.delegated_scopes - current.delegable_scopes
            raise DelegationAuthorityEpochSkew(
                f"domain {domain} epoch {current.epoch_sequence} does not "
                f"permit delegation of {sorted(excess)}; edge minted under "
                f"epoch {edge.issuance_epoch}. Re-obtain delegation under "
                f"current epoch; do not retry."
            )
    return True
