# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-attestation-replay-across-federated-trust-domains-before-executing-a-negotiated-policy.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-attestation-replay-across-federated-trust-domains-before-executing-a-negotiated-policy.warrant.json
# Markdown SHA-256:   d812672d6fd612250022a685766e72c9fbdf9338e49d4ddbbaa93280be20d7d1
# Code AST SHA-256:   ea2e416c761eb75207a085e7c3e250bddb634118f488d5185c6047b522293052
# Verified Classes:   AttestationBindingError, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationBindingError(RuntimeError):
    """Raised when a capability warrant is authentic but not bound to the
    current negotiation context (peer drift, nonce replay, or clock skew)."""


@dataclass(frozen=True)
class Warrant:
    subject: str
    resource_digest: str
    peers: frozenset
    nonce: str
    policy_version: str
    valid_until: float  # in issuer (P) clock domain


def verify_and_bind(
    warrant: Warrant,
    presenting_peer: str,
    requested_digest: str,
    seen_nonces: dict,
    issuer_clock_offset: float,
    max_skew: float,
    now_local: float,
) -> None:
    # 1. Peer-set binding: the presenting executor must be a committed peer.
    if presenting_peer not in warrant.peers:
        raise AttestationBindingError(
            f"peer {presenting_peer!r} not in committed peer set "
            f"{sorted(warrant.peers)!r} for nonce {warrant.nonce!r}"
        )

    # 2. Resource binding: digest must match exactly, not just be present.
    if warrant.resource_digest != requested_digest:
        raise AttestationBindingError(
            f"resource digest mismatch: warrant={warrant.resource_digest!r} "
            f"request={requested_digest!r}"
        )

    # 3. Nonce replay: a seen nonce bound to a different tuple is a replay.
    prior = seen_nonces.get(warrant.nonce)
    expected = (warrant.subject, warrant.resource_digest, warrant.peers)
    if prior is not None and prior != expected:
        raise AttestationBindingError(
            f"nonce {warrant.nonce!r} replayed against different binding: "
            f"prior={prior!r} now={expected!r}"
        )

    # 4. Clock-domain normalization: convert issuer validity into local time.
    if abs(issuer_clock_offset) > max_skew:
        raise AttestationBindingError(
            f"issuer clock offset {issuer_clock_offset}s exceeds max skew "
            f"{max_skew}s; cannot normalize valid_until"
        )
    local_valid_until = warrant.valid_until - issuer_clock_offset
    if now_local >= local_valid_until - max_skew:
        raise AttestationBindingError(
            f"warrant expired after skew adjustment: "
            f"local_valid_until={local_valid_until} now={now_local}"
        )

    # 5. Commit the binding only after all checks pass.
    seen_nonces[warrant.nonce] = expected
