# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-hardware-attested-peer-identity-that-verifies-but-no-longer-binds.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-hardware-attested-peer-identity-that-verifies-but-no-longer-binds.warrant.json
# Markdown SHA-256:   5da7dbb15a6e73c8582717ee47ee4448dfcbefef465d883477f2334549a41548
# Code AST SHA-256:   07e6ba18b67b034753bc4090890e69cad2f133b761ec1ef6d73e1ea26c18dff9
# Verified Classes:   NonBindingAttestationError, Presentation
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class NonBindingAttestationError(RuntimeError):
    """Raised when a verifiable presentation is cryptographically valid but no
    longer binds the acting agent incarnation to the attested measurement."""


@dataclass(frozen=True)
class Presentation:
    subject_key: str
    nonce: str
    nonce_issued_at: float
    measurement_epoch: int          # monotonic counter advanced on every runtime transition
    pcr_digest: bytes
    quote_verified: bool
    transitive: bool = False
    minted_epoch: int | None = None  # epoch at which a transitive credential was minted


def require_binding(
    pres: Presentation,
    *,
    expected_nonce: str,
    current_epoch: int,
    max_nonce_age_s: float = 30.0,
    now: float | None = None,
) -> None:
    """Refuse any presentation whose attestation does not cover the runtime that
    will execute the next action. Verification success is necessary, not sufficient."""
    now = time.time() if now is None else now

    if not pres.quote_verified:
        raise NonBindingAttestationError(
            f"quote for {pres.subject_key} failed cryptographic verification"
        )

    if pres.nonce != expected_nonce:
        raise NonBindingAttestationError(
            "nonce mismatch: presentation was not minted for this challenge"
        )

    if now - pres.nonce_issued_at > max_nonce_age_s:
        raise NonBindingAttestationError(
            f"nonce age {now - pres.nonce_issued_at:.1f}s exceeds {max_nonce_age_s}s"
        )

    # The core boundary: a fresh nonce over a stale measurement is non-binding.
    if pres.measurement_epoch < current_epoch:
        raise NonBindingAttestationError(
            f"non-binding attestation: measured epoch {pres.measurement_epoch} "
            f"predates acting epoch {current_epoch} (runtime transitioned unmeasured)"
        )

    # Transitive credentials must not outlive the epoch they were minted in.
    if pres.transitive:
        if pres.minted_epoch is None:
            raise NonBindingAttestationError(
                "transitive presentation carries no mint epoch; lifetime unbounded"
            )
        if pres.minted_epoch < current_epoch:
            raise NonBindingAttestationError(
                f"transitive credential minted at epoch {pres.minted_epoch} is stale "
                f"for acting epoch {current_epoch} (hot-reload laundering)"
            )
