# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-s-capability-attestation-before-sla-sealing-and-how-does-the-attestation-lease-skew-boundary-manifest.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-s-capability-attestation-before-sla-sealing-and-how-does-the-attestation-lease-skew-boundary-manifest.warrant.json
# Markdown SHA-256:   7a8f9b2f9678f3097055947a3887d9a713577c6ae5bf83b299bba8c11c77bb70
# Code AST SHA-256:   a2f736dfd2e461db37dd709cafebcec3f14b5dce0849cb0852878ed9e7e873ec
# Verified Classes:   AttestationLeaseSkewError, Attestation, SLA
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta


class AttestationLeaseSkewError(RuntimeError):
    """Raised when an SLA would be sealed against an attestation whose lease
    does not co-extend with the obligation, or whose epoch cannot be pinned."""


@dataclass(frozen=True)
class Attestation:
    rev_epoch: int
    valid_until: datetime
    throughput: int
    cost_ceiling: float


@dataclass(frozen=True)
class SLA:
    horizon_end: datetime
    required_throughput: int
    budget_ceiling: float
    pinned_epoch: int | None
    has_reattest_clause: bool


def seal_sla(att: Attestation, sla: SLA, now: datetime,
             epoch_observable: bool) -> str:
    # 1. Validity: the claim must currently be live.
    if att.valid_until <= now:
        raise AttestationLeaseSkewError(
            f"attestation expired at {att.valid_until}, now={now}"
        )

    # 2. Coverage: the lease must span the obligation, or a re-attestation
    #    clause must bind both parties to a shared, pinned epoch.
    horizon_overrun = att.valid_until < sla.horizon_end
    if horizon_overrun and not sla.has_reattest_clause:
        raise AttestationLeaseSkewError(
            f"lease ends {att.valid_until} before SLA horizon {sla.horizon_end} "
            f"and no re-attestation clause present; refusing to seal on a "
            f"decaying claim (rev_epoch={att.rev_epoch})"
        )

    # 3. Epoch pinning: without a pinned epoch the contract is ambiguous
    #    across the two enforcement planes.
    if sla.pinned_epoch is None or sla.pinned_epoch != att.rev_epoch:
        raise AttestationLeaseSkewError(
            f"SLA epoch {sla.pinned_epoch} != attestation epoch "
            f"{att.rev_epoch}; contract would bind two different epochs"
        )

    # 4. Downgrade observability: the sealer must be able to see epoch advance.
    if not epoch_observable:
        raise AttestationLeaseSkewError(
            f"cannot subscribe to rev_epoch advance for epoch "
            f"{att.rev_epoch}; downgrade during open SLA would be undetectable"
        )

    # 5. Envelope sufficiency at seal time.
    if att.throughput < sla.required_throughput:
        raise AttestationLeaseSkewError(
            f"attested throughput {att.throughput} < required "
            f"{sla.required_throughput}"
        )
    if att.cost_ceiling > sla.budget_ceiling:
        raise AttestationLeaseSkewError(
            f"attested cost ceiling {att.cost_ceiling} exceeds budget "
            f"{sla.budget_ceiling}"
        )

    return f"sealed against rev_epoch={att.rev_epoch}"


def revalidate_on_epoch_advance(sealed_epoch: int, new_epoch: int,
                                sla_open: bool) -> None:
    if sla_open and new_epoch != sealed_epoch:
        raise AttestationLeaseSkewError(
            f"epoch advanced {sealed_epoch}->{new_epoch} during open SLA; "
            f"re-seal required before next obligation window"
        )
