# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-verifiable-capability-credential-as-expired-at-evaluation-rather-than-expired-at-issuance-and-how-does-the.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-verifiable-capability-credential-as-expired-at-evaluation-rather-than-expired-at-issuance-and-how-does-the.warrant.json
# Markdown SHA-256:   0c51f99f2d97d0aa0c743e2d7f60ddf2e97cd5911e9da8c78a9923e162742f50
# Code AST SHA-256:   18ae011dc6ed2dff89c0a981dd4ae73839c7d5555c1d0a6bc3a0cce1eae63af9
# Verified Classes:   StaleAttestationError, CapabilityVC, LiveDescriptor
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleAttestationError(RuntimeError):
    """Raised when a VC passes temporal/signature checks but no longer
    describes the subject's live runtime descriptor at evaluation time."""


@dataclass(frozen=True)
class CapabilityVC:
    subject: str
    tool: str
    scope: str
    model: str
    schema_version: str
    issued_at: float
    expires_at: float
    signature_valid: bool


@dataclass(frozen=True)
class LiveDescriptor:
    """What the peer actually reports in the A2A handshake right now."""
    subject: str
    tool: str
    scope: str
    model: str
    schema_version: str
    last_reprovisioned_at: float


def admit_peer(
    vc: CapabilityVC,
    live: LiveDescriptor,
    now: float,
    cache_populated_at: Optional[float] = None,
) -> bool:
    # Temporal + signature gate: necessary, not sufficient.
    if not vc.signature_valid:
        raise StaleAttestationError(f"signature invalid for {vc.subject}")
    if now >= vc.expires_at:
        raise StaleAttestationError(
            f"credential for {vc.subject} expired at {vc.expires_at} (now={now})"
        )

    # Descriptor re-bind: the attestation must still describe the subject.
    if vc.subject != live.subject:
        raise StaleAttestationError(
            f"subject mismatch: vc={vc.subject} live={live.subject}"
        )
    mismatches = {
        "tool": (vc.tool, live.tool),
        "scope": (vc.scope, live.scope),
        "model": (vc.model, live.model),
        "schema_version": (vc.schema_version, live.schema_version),
    }
    drifted = {k: v for k, v in mismatches.items() if v[0] != v[1]}
    if drifted:
        raise StaleAttestationError(
            f"attestation drift for {vc.subject}: {drifted}; "
            f"vc issued_at={vc.issued_at}, live reprovisioned_at={live.last_reprovisioned_at}"
        )

    # Issuance clock vs. evaluation clock: a cached VC older than the
    # peer's last re-provisioning is a snapshot of a past truth.
    if vc.issued_at < live.last_reprovisioned_at:
        raise StaleAttestationError(
            f"credential for {vc.subject} predates last re-provisioning "
            f"({vc.issued_at} < {live.last_reprovisioned_at})"
        )
    if cache_populated_at is not None and cache_populated_at < live.last_reprovisioned_at:
        raise StaleAttestationError(
            f"cached credential for {vc.subject} populated at {cache_populated_at} "
            f"before re-provisioning at {live.last_reprovisioned_at}; re-fetch required"
        )

    return True
