# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-s-verifiable-intent-attestation-at-negotiation-time-rather-than-defer-verification-to-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-s-verifiable-intent-attestation-at-negotiation-time-rather-than-defer-verification-to-execution.warrant.json
# Markdown SHA-256:   4e1245786560b993c01d4a32615363cb30a0e5366f5fc10e1a67a4eb8da7c8b5
# Code AST SHA-256:   f2b7a5a8b1c82aef9790124cf24a531c3722f7ef6475e50e16590cad2cb43cb1
# Verified Classes:   AttestationRejected, PredicateKind, BoundPredicate, IntentAttestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class AttestationRejected(RuntimeError):
    """Raised when a peer intent attestation cannot be accepted at negotiation time."""
    pass


class PredicateKind(Enum):
    MONOTONE = "monotone"
    NON_MONOTONE = "non_monotone"


@dataclass(frozen=True)
class BoundPredicate:
    name: str
    kind: PredicateKind
    recheckable_at_execution: bool
    recheck_aborts_on_fail: bool
    has_revocation_channel: bool


@dataclass(frozen=True)
class IntentAttestation:
    issuer: str
    signature_valid: bool
    valid_until: float
    predicates: tuple[BoundPredicate, ...]


def classify_acceptance(att: IntentAttestation, now: float) -> str:
    if not att.signature_valid:
        raise AttestationRejected(f"signature invalid for issuer={att.issuer}")
    if now >= att.valid_until:
        raise AttestationRejected(f"attestation expired for issuer={att.issuer}")

    for p in att.predicates:
        if p.kind is PredicateKind.MONOTONE:
            continue
        if p.has_revocation_channel:
            continue
        if p.recheckable_at_execution and p.recheck_aborts_on_fail:
            continue
        # Non-monotone, no revocation channel, no abort-typed recheck:
        # deferring verification would launder revocation into retryable contention.
        raise AttestationRejected(
            f"predicate '{p.name}' is non-monotone with no revocation channel "
            f"and no abort-typed execution re-check; must reject at negotiation, "
            f"not defer to execution"
        )
    return "accepted"


def negotiate(att: IntentAttestation, now: float) -> str:
    try:
        return classify_acceptance(att, now)
    except AttestationRejected as e:
        # Surface as a contract-formation failure, never as a retryable execution error.
        raise AttestationRejected(f"negotiation aborted: {e}") from e
