# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-agent-identity-when-the-revocation-event-and-the-credential-rotation-race-across-a-federation-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-agent-identity-when-the-revocation-event-and-the-credential-rotation-race-across-a-federation-trust-boundary.warrant.json
# Markdown SHA-256:   e0b62387d786308df90709b58fc63763561c7390bb6f516a3078c89a84089bf2
# Code AST SHA-256:   5ba267d9a17b4f5b28e3c5fb894629efadf9fbe49d4d3a15a89403297f9f24ee
# Verified Classes:   RevocationRaceError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class RevocationRaceError(RuntimeError):
    """Raised when a credential's validity cannot be established because
    issuance and revocation ordering are unobservable across the federation."""
    pass


def validate_credential(credential, revocation_record, issuer_is_authoritative, max_propagation_s, now):
    # credential: dict with iat, exp, revocation_epoch, issuer
    # revocation_record: dict with revoked_at, epoch, or None
    if credential["exp"] <= now:
        raise RevocationRaceError("credential expired")

    if revocation_record is None:
        if not issuer_is_authoritative:
            raise RevocationRaceError(
                "replica-issued credential with unknown revocation epoch; "
                "cannot order issuance vs revocation"
            )
        if now - credential["iat"] > max_propagation_s:
            raise RevocationRaceError(
                "credential older than propagation window with no epoch record"
            )
        return True

    # Epoch is the only sound ordering proof across issuers.
    if credential["revocation_epoch"] < revocation_record["epoch"]:
        raise RevocationRaceError(
            f"credential epoch {credential['revocation_epoch']} predates "
            f"known revocation epoch {revocation_record['epoch']}; "
            f"iat={credential['iat']} is NOT a validity proof"
        )

    # Same epoch: credential was minted after the issuer saw this revocation.
    if credential["revocation_epoch"] == revocation_record["epoch"]:
        if not issuer_is_authoritative:
            raise RevocationRaceError(
                "replica issuer claims post-revocation epoch but is not "
                "authoritative; rotation may have raced the revocation feed"
            )
        return True

    return True
