# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-signed-aaif-revocation-receipt-as-stale-and-unenforceable-rather-than-authoritative-the-epoch-skew-revocati.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-signed-aaif-revocation-receipt-as-stale-and-unenforceable-rather-than-authoritative-the-epoch-skew-revocati.warrant.json
# Markdown SHA-256:   a135bbdc942ffff33c2c53d2f6a4b521ebccb8943736705e501f66c01b13cb89
# Code AST SHA-256:   733bf8d18f2e39eeeafe8e3d39fe1518858a1c4c8a67c70d5bea4389799db370
# Verified Classes:   RevocationEpochSkewError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class RevocationEpochSkewError(RuntimeError):
    """Raised when a revocation receipt's epoch is behind the receiver's trust epoch
    and the receipt cannot prove inheritance to the current key via the
    previous_key_fingerprint chain."""


def apply_revocation_receipt(receipt: dict, receiver_state: dict) -> str:
    subject = receipt["subject_id"]
    receipt_epoch = receipt["revocation_epoch"]
    current_epoch = receiver_state["trust_epoch"][subject]
    current_fp = receiver_state["current_key_fingerprint"][subject]
    revoked_fp = receipt.get("revoked_key_fingerprint")

    if revoked_fp is None:
        raise RevocationEpochSkewError(
            f"receipt for {subject} omits revoked_key_fingerprint; "
            f"slot-prefix matching across epoch {receipt_epoch}->{current_epoch} "
            f"is unsafe and must be quarantined"
        )

    if receipt_epoch == current_epoch:
        if revoked_fp != current_fp:
            raise RevocationEpochSkewError(
                f"epoch-matched receipt for {subject} names fingerprint "
                f"{revoked_fp} but receiver holds {current_fp}"
            )
        return "applied"

    if receipt_epoch > current_epoch:
        raise RevocationEpochSkewError(
            f"receipt for {subject} claims future epoch {receipt_epoch} "
            f"vs receiver {current_epoch}; reject as forged or misrouted"
        )

    # receipt_epoch < current_epoch: require inheritance proof
    lineage = receiver_state["key_lineage"].get(subject, [])
    try:
        idx = lineage.index(revoked_fp)
    except ValueError:
        raise RevocationEpochSkewError(
            f"revoked fingerprint {revoked_fp} not in lineage for {subject}; "
            f"receipt is stale and cannot be applied to current key {current_fp}"
        )
    chain = lineage[idx:]
    if chain[-1] != current_fp:
        raise RevocationEpochSkewError(
            f"lineage chain for {subject} terminates at {chain[-1]}, "
            f"not current key {current_fp}; chain broken, quarantine"
        )
    if receipt.get("rotation_reason") == "scheduled":
        raise RevocationEpochSkewError(
            f"successor for {subject} rotated on schedule, not compromise; "
            f"stale receipt at epoch {receipt_epoch} must not sever epoch {current_epoch}"
        )
    return "applied_inherited"
