# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-rotated-aaif-credential-as-a-revocation-rather-than-a-refresh.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-rotated-aaif-credential-as-a-revocation-rather-than-a-refresh.warrant.json
# Markdown SHA-256:   ba6f465434e884feec60039934aa589c0d96fdee555b954c268f2e824be0f68f
# Code AST SHA-256:   a79295614b79ccb52e2ec5f299018e2c35aaa37bf4f18580b2cc0cc617036c5f
# Verified Classes:   WarrantRevocationError, AACredential
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class WarrantRevocationError(RuntimeError):
    """Raised when a successor credential is a revocation tombstone, not a refresh."""
    pass


@dataclass(frozen=True)
class AACredential:
    token: str
    warrant_hash: str
    parent_revocation_epoch: int
    subject: str
    instance_id: str
    scope: frozenset


def validate_successor(current: AACredential, successor: AACredential) -> AACredential:
    if successor.warrant_hash != current.warrant_hash:
        raise WarrantRevocationError(
            f"successor warrant {successor.warrant_hash!r} != current "
            f"{current.warrant_hash!r}: not a refresh of this warrant"
        )
    if successor.parent_revocation_epoch < current.parent_revocation_epoch:
        raise WarrantRevocationError(
            f"epoch regression {successor.parent_revocation_epoch} < "
            f"{current.parent_revocation_epoch}: stale replica, reject"
        )
    if successor.parent_revocation_epoch > current.parent_revocation_epoch:
        raise WarrantRevocationError(
            f"epoch advanced {current.parent_revocation_epoch} -> "
            f"{successor.parent_revocation_epoch}: warrant family touched, "
            f"successor is a tombstone; halt task and checkpoint"
        )
    if successor.subject != current.subject or successor.instance_id != current.instance_id:
        raise WarrantRevocationError(
            "successor bound to a different subject/instance: handoff, not refresh"
        )
    if not successor.scope.issubset(current.scope):
        raise WarrantRevocationError(
            f"scope escalation on refresh: {successor.scope - current.scope} not in current"
        )
    return successor
