|
| 1 | +# Author: Green Mountain Systems AI Inc. |
| 2 | +# Donated to IAB Tech Lab |
| 3 | + |
| 4 | +"""Deterministic spend-ceiling guard for money-committing paths. |
| 5 | +
|
| 6 | +Nothing on the buyer's booking path previously compared the final price |
| 7 | +to the campaign's limits: ``max_cpm`` reached the selection LLM only as |
| 8 | +prose and the seller only as an advisory search filter, so a seller |
| 9 | +quoting far above the buyer's ceiling would still be issued a Deal ID. |
| 10 | +
|
| 11 | +``enforce_spend_ceiling`` closes that hole. It is pure, deterministic |
| 12 | +code — no LLM involvement, no configuration flag to disable it — and is |
| 13 | +called at every point where the buyer commits money: |
| 14 | +
|
| 15 | +* ``RequestDealTool._create_deal_response`` (before a Deal ID is minted) |
| 16 | +* ``DealBookingFlow._execute_bookings`` (before booked lines are created) |
| 17 | +
|
| 18 | +Note: ``MultiSellerOrchestrator`` already enforces its own bounds |
| 19 | +(``evaluate_and_rank`` filters quotes by ``max_cpm``; ``select_and_book`` |
| 20 | +skips quotes whose minimum spend exceeds the remaining budget), so it is |
| 21 | +intentionally not routed through this guard. |
| 22 | +
|
| 23 | +Fail-open policy: when a limit is None (the caller never supplied a |
| 24 | +max_cpm or budget), the corresponding check is skipped and a warning is |
| 25 | +logged. This is an explicit choice to preserve current demo behavior for |
| 26 | +callers that never configure limits; it is NOT a bypass for configured |
| 27 | +limits — a supplied limit is always enforced. |
| 28 | +""" |
| 29 | + |
| 30 | +import logging |
| 31 | + |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | +__all__ = ["SpendCeilingExceeded", "enforce_spend_ceiling"] |
| 35 | + |
| 36 | + |
| 37 | +class SpendCeilingExceeded(Exception): |
| 38 | + """Raised when a deal or booking would breach a spend limit. |
| 39 | +
|
| 40 | + Deliberately does NOT subclass ValueError/RuntimeError so that broad |
| 41 | + ``except (OSError, ValueError, RuntimeError)`` handlers on the tool |
| 42 | + paths cannot swallow it by accident — call sites must handle the |
| 43 | + rejection explicitly. |
| 44 | +
|
| 45 | + Attributes: |
| 46 | + final_cpm: Computed final CPM that was checked (or None). |
| 47 | + max_cpm: CPM ceiling that was breached (or None). |
| 48 | + total_cost: Computed total cost that was checked (or None). |
| 49 | + budget: Budget ceiling that was breached (or None). |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + message: str, |
| 55 | + *, |
| 56 | + final_cpm: float | None = None, |
| 57 | + max_cpm: float | None = None, |
| 58 | + total_cost: float | None = None, |
| 59 | + budget: float | None = None, |
| 60 | + ) -> None: |
| 61 | + super().__init__(message) |
| 62 | + self.final_cpm = final_cpm |
| 63 | + self.max_cpm = max_cpm |
| 64 | + self.total_cost = total_cost |
| 65 | + self.budget = budget |
| 66 | + |
| 67 | + |
| 68 | +def enforce_spend_ceiling( |
| 69 | + final_cpm: float | None = None, |
| 70 | + total_cost: float | None = None, |
| 71 | + max_cpm: float | None = None, |
| 72 | + budget: float | None = None, |
| 73 | +) -> None: |
| 74 | + """Enforce CPM and budget ceilings before money is committed. |
| 75 | +
|
| 76 | + Pure deterministic guard: compares actuals against limits and raises |
| 77 | + when a limit would be breached. At-or-under a limit is allowed. |
| 78 | +
|
| 79 | + Args: |
| 80 | + final_cpm: The computed final CPM about to be committed. |
| 81 | + total_cost: The total cost about to be committed. |
| 82 | + max_cpm: The campaign's maximum acceptable CPM (limit). |
| 83 | + budget: The campaign's budget (limit). |
| 84 | +
|
| 85 | + Raises: |
| 86 | + SpendCeilingExceeded: When final_cpm > max_cpm or |
| 87 | + total_cost > budget (both comparisons only run when both |
| 88 | + sides are present). |
| 89 | + """ |
| 90 | + if max_cpm is None and budget is None: |
| 91 | + # Fail-open (explicit choice): no limits were supplied, so there |
| 92 | + # is nothing to enforce. Allow the spend but log a warning so |
| 93 | + # unbounded money paths are visible in logs. This preserves |
| 94 | + # current demo behavior for callers without configured limits. |
| 95 | + logger.warning( |
| 96 | + "Spend ceiling check skipped: no max_cpm or budget limit supplied " |
| 97 | + "(final_cpm=%s, total_cost=%s). Spend is unbounded.", |
| 98 | + final_cpm, |
| 99 | + total_cost, |
| 100 | + ) |
| 101 | + return |
| 102 | + |
| 103 | + if max_cpm is not None and final_cpm is not None and final_cpm > max_cpm: |
| 104 | + raise SpendCeilingExceeded( |
| 105 | + f"Final CPM ${final_cpm:.2f} exceeds max CPM ceiling ${max_cpm:.2f}", |
| 106 | + final_cpm=final_cpm, |
| 107 | + max_cpm=max_cpm, |
| 108 | + total_cost=total_cost, |
| 109 | + budget=budget, |
| 110 | + ) |
| 111 | + |
| 112 | + if budget is not None and total_cost is not None and total_cost > budget: |
| 113 | + raise SpendCeilingExceeded( |
| 114 | + f"Total cost ${total_cost:,.2f} exceeds budget ${budget:,.2f}", |
| 115 | + final_cpm=final_cpm, |
| 116 | + max_cpm=max_cpm, |
| 117 | + total_cost=total_cost, |
| 118 | + budget=budget, |
| 119 | + ) |
0 commit comments