# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-task-despite-an-sla-that-appears-satisfiable.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-task-despite-an-sla-that-appears-satisfiable.warrant.json
# Markdown SHA-256:   0498333395a7579650bd69935f22255d06eccecf7b5c8a749c9ff880a01c0f8a
# Code AST SHA-256:   ec204101d76dfc85635a45486576ecd6a72523a20ae5cd4e8d67d60daaa037aa
# Verified Classes:   SLALoadAssumptionUncertifiable
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SLALoadAssumptionUncertifiable(RuntimeError):
    """Raised when a delegated SLA is arithmetically satisfiable against the
    executor's published envelope but not certifiable against its residual
    capacity under the proposer's (unknown or bursty) load model."""
    pass


def admit_delegation(
    proposal: dict,
    certified_residual_capacity: float,
    required_residual: float,
    proposer_arrival_bound: float | None,
) -> bool:
    """
    proposal: {deadline_s, max_latency_p99_s, error_budget, penalty}
    certified_residual_capacity: throughput guaranteed to a NEW admission
        without degrading already-accepted SLAs, measured over a window
        >= proposer burst correlation length.
    required_residual: residual throughput the proposal's deadline+latency
        bound implies, computed from the task's service-time distribution.
    proposer_arrival_bound: worst-case arrival rate if the proposer can
        certify a bounded arrival process; None if unknown/bursty.
    """
    if required_residual <= certified_residual_capacity:
        return True

    if proposer_arrival_bound is not None:
        # Accept only if the certified bound's worst-case load fits.
        if required_residual <= proposer_arrival_bound:
            return True
        raise SLALoadAssumptionUncertifiable(
            f"proposal requires residual {required_residual:.3f} but "
            f"certified arrival bound is {proposer_arrival_bound:.3f}; "
            f"counter-propose relaxed deadline or reduced concurrency"
        )

    # Arithmetic may close against the published envelope, but the load
    # assumption is uncertifiable. Refuse and counter-propose.
    raise SLALoadAssumptionUncertifiable(
        f"proposal requires residual {required_residual:.3f} > certified "
        f"residual capacity {certified_residual_capacity:.3f} and proposer "
        f"offers no bounded arrival process; refusing delegation"
    )
