# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-attest-its-hardware-root-when-a-verifiable-credential-s-subject-key-outlives-the-tee-quote-that-bound-it.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-attest-its-hardware-root-when-a-verifiable-credential-s-subject-key-outlives-the-tee-quote-that-bound-it.warrant.json
# Markdown SHA-256:   bc7adb6dbf7dff4d1d6348bd4cc705a2a94ff541802b273be983e0728a03e639
# Code AST SHA-256:   6a9c239edddb76a91ab87c8438f8ccf4d7eec44fac8ad6399dd6225132c52cef
# Verified Classes:   AttestationDecoherenceError, Credential, Quote, SessionContext
# Defensive Guards:   8 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationDecoherenceError(RuntimeError):
    """Raised when a credential's subject key is no longer provably bound to a
    live hardware root for the current session (nonce/TCB/platform mismatch)."""
    pass


@dataclass(frozen=True)
class Credential:
    subject_key: str
    expiration: float          # absolute epoch seconds
    revocation_ok: bool


@dataclass(frozen=True)
class Quote:
    subject_key: str
    nonce: str
    tcb_level: int
    platform_id: str
    issued_at: float


@dataclass(frozen=True)
class SessionContext:
    nonce: str
    presented_platform_id: str
    now: float


def verify_federated_agent(
    cred: Credential,
    quote: Quote,
    ctx: SessionContext,
    nonce_store: dict,
    current_tcb_level: int,
    max_quote_age_s: float = 300.0,
) -> bool:
    # 1. credential clock
    if not cred.revocation_ok:
        raise AttestationDecoherenceError("credential revoked")
    if ctx.now >= cred.expiration:
        raise AttestationDecoherenceError("credential expired")

    # 2. nonce must be the current session's, single-use, and unconsumed
    if quote.nonce != ctx.nonce:
        raise AttestationDecoherenceError(
            f"quote nonce {quote.nonce!r} != session nonce {ctx.nonce!r} (replay?)"
        )
    if nonce_store.get(ctx.nonce) != "issued":
        raise AttestationDecoherenceError("nonce absent, reused, or expired")

    # 3. quote freshness bounded by attestation clock, not credential clock
    if ctx.now - quote.issued_at > max_quote_age_s:
        raise AttestationDecoherenceError("quote stale beyond attestation cadence")

    # 4. key binding: quote must certify THIS subject key
    if quote.subject_key != cred.subject_key:
        raise AttestationDecoherenceError("quote binds a different subject key")

    # 5. platform identity must match the presenting host
    if quote.platform_id != ctx.presented_platform_id:
        raise AttestationDecoherenceError(
            "platform identity mismatch: key presented from unquoted host"
        )

    # 6. TCB must be current per freshly-pulled collateral, not quote's claim
    if quote.tcb_level != current_tcb_level:
        raise AttestationDecoherenceError(
            f"TCB level {quote.tcb_level} != current {current_tcb_level}; re-attest"
        )

    nonce_store[ctx.nonce] = "consumed"
    return True
