# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-late-but-valid-contract-net-award-when-the-bid-sla-window-and-the-attestation-epoch-have-diverged.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-late-but-valid-contract-net-award-when-the-bid-sla-window-and-the-attestation-epoch-have-diverged.warrant.json
# Markdown SHA-256:   38d0bcacb69b6fc251a4bb86b1e38d20c819c6aff753adc71f4e4efbeca13d8d
# Code AST SHA-256:   1bf414c2738d7170ee30675dcc954eaec0c5c7238b7f0d79fcae8fb032b6f4bb
# Verified Classes:   EpochUnfulfillable
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EpochUnfulfillable(RuntimeError):
    """Raised when an award is locally SLA-valid but the active attestation
    epoch will retire before the contractor can mint an outcome attestation."""
    def __init__(self, received_at: float, epoch_retire: float, required_retention: float):
        self.received_at = received_at
        self.epoch_retire = epoch_retire
        self.required_retention = required_retention
        super().__init__(
            f"epoch retires at {epoch_retire:.1f}s but retention through "
            f"{received_at + required_retention:.1f}s is required "
            f"(shortfall {required_retention - (epoch_retire - received_at):.1f}s)"
        )


def accept_award(award, bid, epoch, mint_latency_bound: float) -> str:
    """Contractor-side acceptance gate. Returns 'accept' or raises EpochUnfulfillable."""
    received_at = award.received_at
    local_deadline = bid.deadline + bid.grace_margin

    # Local SLA validity (necessary, not sufficient).
    if received_at > local_deadline:
        raise ValueError("award past local SLA deadline; ordinary timeout path")

    required_retention = bid.sla_exec + mint_latency_bound
    epoch_retire = epoch.retire_time_at(received_at)

    # The non-obvious boundary: locally valid, globally unfulfillable.
    if epoch_retire < received_at + required_retention:
        raise EpochUnfulfillable(received_at, epoch_retire, required_retention)

    return "accept"
