# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-peer-s-delegated-authority-before-policy-exchange-completes.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-peer-s-delegated-authority-before-policy-exchange-completes.warrant.json
# Markdown SHA-256:   2692e747a88d2314db39ef1dfaee93a6abc17253f9ace81967c0c788006c4f1e
# Code AST SHA-256:   47393e9d82513c6d1da2db86465c28a23efe4b94f3393a769432789072fd0a40
# Verified Classes:   FederationAuthorityError, AuthorityState, PeerAuthorityView
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum


class FederationAuthorityError(RuntimeError):
    """Raised when an instance is asked to act on delegated authority whose
    governing conditions are not fully established. This is a refusal, not a
    failure: the peer may be perfectly authentic and still indeterminate."""


class AuthorityState(Enum):
    DENY = "deny"              # chain invalid; unauthenticated
    NEGOTIATE = "negotiate"    # authentic but conditions incomplete; no effectful action
    ALLOW = "allow"            # all conditions hold; act within effective authority


@dataclass(frozen=True)
class PeerAuthorityView:
    chain_verified: bool
    trust_anchor_pinned: bool
    obligations_resident: bool
    obligations_digest_matches_token: bool
    revocation_age_seconds: float
    max_revocation_age_seconds: float


def classify(view: PeerAuthorityView) -> AuthorityState:
    if not view.chain_verified:
        return AuthorityState.DENY
    if not view.trust_anchor_pinned:
        return AuthorityState.NEGOTIATE
    if not view.obligations_resident or not view.obligations_digest_matches_token:
        return AuthorityState.NEGOTIATE
    if view.revocation_age_seconds > view.max_revocation_age_seconds:
        return AuthorityState.NEGOTIATE
    return AuthorityState.ALLOW


def require_effectful_authority(view: PeerAuthorityView) -> None:
    """Gate for effectful actions. Negotiation actions must NOT call this;
    they proceed on NEGOTIATE so the conditions can be established."""
    state = classify(view)
    if state is AuthorityState.DENY:
        raise FederationAuthorityError(
            "delegation chain failed verification; peer is unauthenticated"
        )
    if state is AuthorityState.NEGOTIATE:
        raise FederationAuthorityError(
            "peer authentic but authority indeterminate: "
            f"anchor_pinned={view.trust_anchor_pinned} "
            f"obligations_resident={view.obligations_resident} "
            f"digest_match={view.obligations_digest_matches_token} "
            f"revocation_age={view.revocation_age_seconds:.1f}s "
            f"max={view.max_revocation_age_seconds:.1f}s; "
            "refusing effectful action until conditions established"
        )
