# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-scope-narrowing-replay-in-aaif-delegated-credential-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-scope-narrowing-replay-in-aaif-delegated-credential-chains.warrant.json
# Markdown SHA-256:   a94c6c5dbc2b207979325d6c43a1997235e38274570745dfece095246bad648d
# Code AST SHA-256:   b3943b2ec0c6da65c6f6713508c840db8dc5c2829c58c61a4744fc1d12e6782d
# Verified Classes:   DelegationReplayError, CapabilityToken
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class DelegationReplayError(RuntimeError):
    """Raised when a capability token is presented by a party whose warrant does not subsume the leaf's warrant class."""
    pass


class CapabilityToken:
    def __init__(self, subject_key: str, scope: frozenset, warrant: frozenset,
                 parent_hash: str | None, depth: int, exp: float,
                 presenter_binding: str, revocation_epoch: int):
        self.subject_key = subject_key
        self.scope = scope
        self.warrant = warrant
        self.parent_hash = parent_hash
        self.depth = depth
        self.exp = exp
        self.presenter_binding = presenter_binding
        self.revocation_epoch = revocation_epoch


def verify_presentation(token: CapabilityToken, presenter_key: str,
                        presenter_scope: frozenset, presenter_warrant: frozenset,
                        current_epoch: int, now: float) -> None:
    if token.presenter_binding != presenter_key:
        raise DelegationReplayError(
            f"presenter {presenter_key} != bound presenter {token.presenter_binding} "
            f"at depth {token.depth}"
        )
    if now > token.exp:
        raise DelegationReplayError(f"token expired at {token.exp}, now {now}")
    if token.revocation_epoch < current_epoch:
        raise DelegationReplayError(
            f"token epoch {token.revocation_epoch} < current {current_epoch}; "
            f"ancestor revoked"
        )
    if presenter_key == token.subject_key:
        return
    # Ancestor presentation: require scope subset AND warrant subsumption.
    if not presenter_scope.issubset(token.scope):
        raise DelegationReplayError(
            f"ancestor scope {presenter_scope} not subset of leaf {token.scope}"
        )
    if not token.warrant.issubset(presenter_warrant):
        raise DelegationReplayError(
            f"ancestor warrant {presenter_warrant} does not subsume leaf warrant "
            f"{token.warrant}; replay would misattribute policy path"
        )
