# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-did-s-verifiable-credential-at-a2a-federation-handshake-despite-valid-signature-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-did-s-verifiable-credential-at-a2a-federation-handshake-despite-valid-signature-chains.warrant.json
# Markdown SHA-256:   7481b26c1735a96f376d047956b9e426a127df67a75ff535637bc70e9112ef06
# Code AST SHA-256:   b73a9e0c015a467a209dad5232307fb413fd47d59ad81779e6df5ba000d301ad
# Verified Classes:   FederationTrustError, Credential, TrustContext
# Defensive Guards:   9 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class FederationTrustError(RuntimeError):
    """Raised when a peer's verifiable credential fails the conjunctive admission predicate."""
    pass


@dataclass
class Credential:
    issuer_did: str
    subject_did: str
    capability_scope: str
    issuance_date: float
    valid_until: Optional[float]
    proof_valid: bool
    revocation_status: str  # "active" | "revoked" | "superseded" | "unknown"


@dataclass
class TrustContext:
    presenting_peer_did: str
    requested_scope: str
    trusted_anchors: set
    anchor_doc_fresh: bool
    now: float
    clock_skew_bound: float = 30.0


def admit_peer(cred: Credential, ctx: TrustContext) -> None:
    if not cred.proof_valid:
        raise FederationTrustError("proof verification failed")

    if cred.issuer_did not in ctx.trusted_anchors:
        raise FederationTrustError(f"issuer {cred.issuer_did} not a trusted anchor")

    if not ctx.anchor_doc_fresh:
        raise FederationTrustError("anchor DID document stale; re-resolve before admit")

    if cred.subject_did != ctx.presenting_peer_did:
        raise FederationTrustError(
            f"subject binding mismatch: credential for {cred.subject_did}, "
            f"presented by {ctx.presenting_peer_did}"
        )

    if cred.valid_until is None:
        raise FederationTrustError("credential lacks validUntil; non-expiring not permitted in this profile")

    if ctx.now > cred.valid_until + ctx.clock_skew_bound:
        raise FederationTrustError(
            f"credential expired at {cred.valid_until}, now {ctx.now}"
        )

    if cred.issuance_date > ctx.now + ctx.clock_skew_bound:
        raise FederationTrustError("issuanceDate in the future beyond skew bound")

    if cred.revocation_status in ("revoked", "superseded", "unknown"):
        raise FederationTrustError(
            f"revocation status {cred.revocation_status!r}; fail-closed"
        )

    if not ctx.requested_scope.startswith(cred.capability_scope):
        raise FederationTrustError(
            f"requested scope {ctx.requested_scope} exceeds credential scope {cred.capability_scope}"
        )
