# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-sign-a-cross-org-slashing-attestation-when-equivocation-evidence-arrives-after-the-peer-s-evidence-window.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-sign-a-cross-org-slashing-attestation-when-equivocation-evidence-arrives-after-the-peer-s-evidence-window.warrant.json
# Markdown SHA-256:   ce4500a44ceccb9fc67bf86b03f99fba2cd570569ee760b71548752243c45e5a
# Code AST SHA-256:   ddd90250e7998fb9b81e4062251b741c6131d360a94ce688ee8121b0308c2b40
# Verified Classes:   UnsoundAttestationError, EpochContext
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class UnsoundAttestationError(RuntimeError):
    """Raised when a signer is asked to emit a NON_SLASH attestation it cannot ground."""
    pass


@dataclass
class EpochContext:
    epoch_open: float
    epoch_close: float
    evidence_deadline: float          # signer's local deadline
    propagation_bound: float          # measured max gossip latency
    clock_skew_verified: bool
    inbound_sequence_contiguous: bool


def sign_attestation(ctx: EpochContext, verdict: str, peer_id: str) -> dict:
    if verdict == "SLASH":
        # Positive evidence: always signable, fast and final.
        return {"peer": peer_id, "verdict": "SLASH", "final": True}

    if verdict != "NON_SLASH":
        raise UnsoundAttestationError(f"unknown verdict {verdict!r} for peer {peer_id}")

    # A NON_SLASH signature is a global claim over the whole epoch.
    # It is only sound if the local deadline is late enough that no evidence
    # can still arrive unseen before epoch close, and the signer's view is
    # clock-verified and gap-free.
    latest_safe_deadline = ctx.epoch_close - ctx.propagation_bound
    if ctx.evidence_deadline < latest_safe_deadline:
        raise UnsoundAttestationError(
            f"refusing NON_SLASH for {peer_id}: evidence_deadline "
            f"{ctx.evidence_deadline:.1f} < epoch_close - propagation_bound "
            f"{latest_safe_deadline:.1f}; evidence may still arrive unseen"
        )
    if not ctx.clock_skew_verified:
        raise UnsoundAttestationError(
            f"refusing NON_SLASH for {peer_id}: clock skew unverified, "
            f"deadline comparison is meaningless"
        )
    if not ctx.inbound_sequence_contiguous:
        raise UnsoundAttestationError(
            f"refusing NON_SLASH for {peer_id}: inbound evidence sequence has a gap"
        )

    # Provisional until epoch close; may be superseded by SLASH.
    return {"peer": peer_id, "verdict": "NON_SLASH", "final": False}


def attest_or_abstain(ctx: EpochContext, verdict: str, peer_id: str) -> dict:
    try:
        return sign_attestation(ctx, verdict, peer_id)
    except UnsoundAttestationError as exc:
        # Abstain: emit no signature, mark unattested. Always safe.
        return {"peer": peer_id, "verdict": "UNATTESTED", "reason": str(exc)}
