# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-silent-capability-widening-when-a-federated-peer-s-trust-broker-re-signs-a-warrant-under-a-looser-policy-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-silent-capability-widening-when-a-federated-peer-s-trust-broker-re-signs-a-warrant-under-a-looser-policy-epoch.warrant.json
# Markdown SHA-256:   237a07077afbf47d057afb7efbbc615f7f6c3a2e8a50e6363fd104915f80e4d8
# Code AST SHA-256:   fb1eadfbccc95907bb9f1c831a7dbd64b0bb51c34c8593b05ca6f004e6adec42
# Verified Classes:   CapabilityWideningError, EpochDriftError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityWideningError(RuntimeError):
    """Raised when a presented warrant's effective scope exceeds the session-pinned ceiling."""
    pass


class EpochDriftError(CapabilityWideningError):
    """Raised when widening coincides with a policy_epoch change, requiring re-harmonization."""
    pass


def verify_warrant(warrant: dict, pinned_ceiling: set, pinned_epoch: str) -> None:
    presented_scope = set(warrant["effective_scope"])
    presented_epoch = warrant["policy_epoch"]

    if not warrant.get("signature_valid", False):
        raise CapabilityWideningError("warrant signature invalid; reject before scope check")

    widened = not presented_scope.issubset(pinned_ceiling)
    epoch_changed = presented_epoch != pinned_epoch

    if widened and epoch_changed:
        raise EpochDriftError(
            f"scope widened under epoch rotation {pinned_epoch}->{presented_epoch}: "
            f"added={presented_scope - pinned_ceiling}; suspend session and re-harmonize"
        )
    if widened:
        raise CapabilityWideningError(
            f"scope widened without epoch change: added={presented_scope - pinned_ceiling}; "
            f"peer broker violation, quarantine"
        )
    # Equivalent or narrower: safe to invoke.
