UAT: CostBudgetService._check_warning() only checks session-level utilization — org budget approaching limit does not emit BUDGET_WARNING event #5607

Open
opened 2026-04-09 07:46:17 +00:00 by HAL9000 · 0 comments
Owner

Bug Report

Feature Area: cost-session-budgets-safety-profiles
Severity: Medium — warning events are incomplete for org-level budget monitoring


What Was Tested

Code-level analysis of CostBudgetService._check_warning() in cost_budget_service.py.

Expected Behavior (from spec)

Per docs/reference/cost_controls.md:

"Warning | 90% of limit | Log warning, continue execution"

This threshold behavior should apply to all budget tiers in the three-tier hierarchy (plan → session → org). When an org budget approaches its limit, a BUDGET_WARNING event should be emitted.

Actual Behavior (from code analysis)

src/cleveragents/application/services/cost_budget_service.py_check_warning():

def _check_warning(self, session_id: str) -> bool:
    """Return True if the session is above warning threshold."""
    if session_id in self._warned_sessions:
        return False
    session_budget = self._sessions.get(session_id)
    if session_budget is None:
        return False
    util = session_budget.utilization()  # ← Only checks SESSION budget
    if util is None:
        return False
    if util >= self._warning_threshold:
        self._warned_sessions.add(session_id)
        self._emit_warning(session_id)
        return True
    return False

The method only checks the session-level budget utilization. If the org budget is at 95% utilization but the session budget is at 30%, no warning is emitted. This means:

  1. Org-level budget warnings are never emitted
  2. Operators cannot be alerted when org-wide spending approaches the limit
  3. The _warned_sessions set tracks only session-level warnings, with no equivalent for org-level

Code Location

  • src/cleveragents/application/services/cost_budget_service.py_check_warning() method

Steps to Reproduce

  1. Configure an org with max_cost_usd=100.0
  2. Configure a session with max_cost_usd=1000.0 (generous session limit)
  3. Associate the session with the org
  4. Record costs totaling $95.0 against the session (which flows to the org)
  5. Call check_budget_hierarchy() with a small plan_cost
  6. Observe: BudgetCheckResult.warning is False even though org is at 95% utilization

Fix Suggestion

Extend _check_warning() to also check org-level utilization:

def _check_warning(self, session_id: str) -> bool:
    # Check session-level warning
    if session_id not in self._warned_sessions:
        session_budget = self._sessions.get(session_id)
        if session_budget is not None:
            util = session_budget.utilization()
            if util is not None and util >= self._warning_threshold:
                self._warned_sessions.add(session_id)
                self._emit_warning(session_id)
                return True
    
    # Check org-level warning
    org_id = self._session_org.get(session_id)
    if org_id is not None:
        org_acc = self._orgs.get(org_id)
        if org_acc is not None:
            util = org_acc.utilization()
            if util is not None and util >= self._warning_threshold:
                # Emit org-level warning
                return True
    
    return False

Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: uat-tester

## Bug Report **Feature Area**: cost-session-budgets-safety-profiles **Severity**: Medium — warning events are incomplete for org-level budget monitoring --- ## What Was Tested Code-level analysis of `CostBudgetService._check_warning()` in `cost_budget_service.py`. ## Expected Behavior (from spec) Per `docs/reference/cost_controls.md`: > "Warning | 90% of limit | Log warning, continue execution" This threshold behavior should apply to all budget tiers in the three-tier hierarchy (plan → session → org). When an org budget approaches its limit, a `BUDGET_WARNING` event should be emitted. ## Actual Behavior (from code analysis) `src/cleveragents/application/services/cost_budget_service.py` — `_check_warning()`: ```python def _check_warning(self, session_id: str) -> bool: """Return True if the session is above warning threshold.""" if session_id in self._warned_sessions: return False session_budget = self._sessions.get(session_id) if session_budget is None: return False util = session_budget.utilization() # ← Only checks SESSION budget if util is None: return False if util >= self._warning_threshold: self._warned_sessions.add(session_id) self._emit_warning(session_id) return True return False ``` The method **only checks the session-level budget utilization**. If the org budget is at 95% utilization but the session budget is at 30%, no warning is emitted. This means: 1. Org-level budget warnings are never emitted 2. Operators cannot be alerted when org-wide spending approaches the limit 3. The `_warned_sessions` set tracks only session-level warnings, with no equivalent for org-level ## Code Location - `src/cleveragents/application/services/cost_budget_service.py` — `_check_warning()` method ## Steps to Reproduce 1. Configure an org with `max_cost_usd=100.0` 2. Configure a session with `max_cost_usd=1000.0` (generous session limit) 3. Associate the session with the org 4. Record costs totaling $95.0 against the session (which flows to the org) 5. Call `check_budget_hierarchy()` with a small `plan_cost` 6. Observe: `BudgetCheckResult.warning` is `False` even though org is at 95% utilization ## Fix Suggestion Extend `_check_warning()` to also check org-level utilization: ```python def _check_warning(self, session_id: str) -> bool: # Check session-level warning if session_id not in self._warned_sessions: session_budget = self._sessions.get(session_id) if session_budget is not None: util = session_budget.utilization() if util is not None and util >= self._warning_threshold: self._warned_sessions.add(session_id) self._emit_warning(session_id) return True # Check org-level warning org_id = self._session_org.get(session_id) if org_id is not None: org_acc = self._orgs.get(org_id) if org_acc is not None: util = org_acc.utilization() if util is not None and util >= self._warning_threshold: # Emit org-level warning return True return False ``` --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: uat-tester
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#5607
No description provided.