# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-remote-attestation-chain-when-tee-quotes-outlive-the-measurement-they-certify.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-remote-attestation-chain-when-tee-quotes-outlive-the-measurement-they-certify.warrant.json
# Markdown SHA-256:   06695971743154e13a03178751a15b0947a6e16e64fd7c54837c7fdaf08ecc1a
# Code AST SHA-256:   488ff49dd398d16d5f85aa4676f7016d73be9fda64ec25de327d6b86f5e54b6b
# Verified Classes:   StaleAttestationWarrantError, TeeQuote, CapabilityWarrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class StaleAttestationWarrantError(RuntimeError):
    """Raised when a cached TEE-derived capability warrant no longer binds
    to the currently-executing enclave measurement."""
    pass


@dataclass(frozen=True)
class TeeQuote:
    measurement: str          # MRENCLAVE / launch digest named by the quote
    report_data_nonce: str    # nonce the enclave signed over at quote time
    issued_epoch: int         # enclave boot/restart epoch at issuance
    max_quote_age_s: int      # freshness bound the enclave itself declares


@dataclass(frozen=True)
class CapabilityWarrant:
    peer_id: str
    capability: str
    quote: TeeQuote
    cached_at_epoch: int


def admit_tool_call(
    warrant: CapabilityWarrant,
    live_measurement: str,
    live_epoch: int,
    live_nonce_response: Optional[str],
    expected_nonce: str,
) -> bool:
    # Structural signal 1: execution epoch advanced past the warrant's epoch.
    if live_epoch != warrant.cached_at_epoch:
        raise StaleAttestationWarrantError(
            f"peer {warrant.peer_id}: execution epoch {live_epoch} != "
            f"warrant epoch {warrant.cached_at_epoch}; forced re-attestation required"
        )

    # Structural signal 2: live measurement diverges from the quote's measurement.
    if live_measurement != warrant.quote.measurement:
        raise StaleAttestationWarrantError(
            f"peer {warrant.peer_id}: live measurement {live_measurement} != "
            f"quoted measurement {warrant.quote.measurement}; capability drift"
        )

    # Structural signal 3: no live nonce challenge answered by the current enclave.
    if live_nonce_response is None or live_nonce_response != expected_nonce:
        raise StaleAttestationWarrantError(
            f"peer {warrant.peer_id}: warrant not bound to current execution instance "
            f"(nonce challenge unanswered)"
        )

    return True


def admit_with_auditor_consensus(
    planner_warrant: CapabilityWarrant,
    auditor_warrant: CapabilityWarrant,
) -> bool:
    if planner_warrant.quote.issued_epoch != auditor_warrant.quote.issued_epoch:
        # Conflicting attestation epochs: refuse, do not resolve by recency.
        raise StaleAttestationWarrantError(
            f"peer {planner_warrant.peer_id}: planner epoch "
            f"{planner_warrant.quote.issued_epoch} != auditor epoch "
            f"{auditor_warrant.quote.issued_epoch}; deadlock, force re-handshake"
        )
    return True
