# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-s-speculative-scheduling-budget-silently-collapse-when-cross-org-quota-negotiation-returns-a-fractional-reservation.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-s-speculative-scheduling-budget-silently-collapse-when-cross-org-quota-negotiation-returns-a-fractional-reservation.warrant.json
# Markdown SHA-256:   65575006498f30281bb5e5306ea2a65bf6ac07045597643a887bb44dbeb1a95f
# Code AST SHA-256:   659f2aad6b1ef2581f3121be9cd2de71989a9d565fdefec8335a115894aa8d10
# Verified Classes:   PhantomSpeculativeBudgetError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class PhantomSpeculativeBudgetError(RuntimeError):
    """Raised when speculative launch authority exceeds committed, irrevocable ledger units."""
    pass


def compute_launch_authority(reservations):
    """
    Reservations are dicts: {"units": float, "irrevocable": bool, "integral": bool}.
    Only integral + irrevocable reservations grant launch authority.
    Fractional or probabilistic grants contribute to forecast ONLY.
    """
    committed = 0
    forecast_only = 0.0
    for r in reservations:
        units = r["units"]
        if r.get("integral") and r.get("irrevocable"):
            committed += int(units)
        else:
            # fractional / revocable: forecast mass, NOT launch authority
            forecast_only += units
    return committed, forecast_only


def schedule_speculative(reservations, predicted_demand):
    committed, forecast_only = compute_launch_authority(reservations)

    # The guardrail: launch authority is exactly the committed ledger.
    # Fractional mass must NEVER be floored into launch authority.
    launch_authority = committed

    # Sanity: if someone tries to fold forecast into authority, fail closed.
    naive_floor = int(committed + forecast_only)
    if naive_floor > committed:
        raise PhantomSpeculativeBudgetError(
            f"speculative headroom {naive_floor} exceeds committed ledger "
            f"{committed}; fractional forecast mass {forecast_only:.2f} "
            f"must not authorize launches"
        )

    launches = min(launch_authority, predicted_demand)
    return {
        "launches": launches,
        "committed_ledger": committed,
        "forecast_only_mass": forecast_only,
        "needs_committed_negotiation": predicted_demand > committed,
    }


def reconcile_after_collapse(launched, committed_ledger):
    if launched > committed_ledger:
        raise PhantomSpeculativeBudgetError(
            f"{launched} speculative instances launched against committed "
            f"ledger of {committed_ledger}; mid-task quota-wall aborts imminent"
        )
    return True


# Trigger trace: twelve peers each return 0.6, non-irrevocable, non-integral.
reservations = [
    {"units": 0.6, "irrevocable": False, "integral": False}
    for _ in range(12)
]
try:
    schedule_speculative(reservations, predicted_demand=7)
except PhantomSpeculativeBudgetError as e:
    print(f"BLOCKED: {e}")
