# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-a-contract-net-bidder-reject-a-winning-bid-when-the-sla-deadline-is-expressible-only-as-a-relative-offset.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-a-contract-net-bidder-reject-a-winning-bid-when-the-sla-deadline-is-expressible-only-as-a-relative-offset.warrant.json
# Markdown SHA-256:   ac669c97b1120fce3708df926319b4dfb7627a98a56da44c90072f8666fb0829
# Code AST SHA-256:   49715db30f813640c91088eafc48b12b79d4df267a9da28b70de4ab3e60495c9
# Verified Classes:   SLAReferenceFrameError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time


class SLAReferenceFrameError(RuntimeError):
    """Raised when a winning bid is infeasible under the award-time reference frame."""
    pass


def validate_award(
    absolute_deadline_ts: float,
    award_timestamp: float,
    commit_offset_s: float,
    max_clock_skew_s: float = 0.0,
    safety_margin_s: float = 0.05,
) -> None:
    """
    Re-validate a winning bid's relative commitment against the manager's
    absolute deadline, at award time, under the execution reference frame.

    Raises SLAReferenceFrameError if the bid cannot complete before the
    deadline even though it may have passed proposal-time feasibility.
    """
    effective_slack = absolute_deadline_ts - award_timestamp
    # Inflate the bidder's claimed offset by the worst-case clock skew,
    # because the bidder computed commit_offset_s against its own clock.
    required = commit_offset_s + max_clock_skew_s + safety_margin_s

    if required > effective_slack:
        raise SLAReferenceFrameError(
            f"Winning bid infeasible at award time: "
            f"required={required:.3f}s "
            f"(commit={commit_offset_s:.3f}s + skew={max_clock_skew_s:.3f}s "
            f"+ margin={safety_margin_s:.3f}s) "
            f"> effective_slack={effective_slack:.3f}s "
            f"(deadline={absolute_deadline_ts:.3f}, award={award_timestamp:.3f}). "
            f"Proposal-time feasibility does not imply award-time feasibility."
        )


def award_with_fallback(
    ranked_bids: list[dict],
    absolute_deadline_ts: float,
    award_timestamp: float,
    max_clock_skew_s: float = 0.0,
) -> dict:
    """
    Award the highest-ranked bid that survives award-time re-validation.
    Falls through to the next bid rather than re-soliciting, since
    re-solicitation consumes another round-trip and worsens the margin.
    """
    for bid in ranked_bids:
        try:
            validate_award(
                absolute_deadline_ts=absolute_deadline_ts,
                award_timestamp=award_timestamp,
                commit_offset_s=bid["commit_offset_s"],
                max_clock_skew_s=max_clock_skew_s,
            )
        except SLAReferenceFrameError:
            continue
        return bid
    raise SLAReferenceFrameError(
        "No bid in the ranked set is feasible under the award-time "
        "reference frame; all offsets exceed effective slack."
    )
