feat: notify admins when a user crosses an AI budget threshold (#27415)

Implements:
https://linear.app/codercom/issue/AIGOV-289/notify-users-and-admins-on-budget-warning-and-limit-reached

Notify admins when a user crosses an AI budget threshold, complementing
the user-facing notifications from
https://github.com/coder/coder/pull/27346

When a priced interception pushes a user's period spend across the
warning (85%) or limit (100%) threshold, the Owners and User Admins now
receive an admin notification naming the affected user, alongside the
user's own notification. The affected user is excluded from the admin
recipients since they already get the user-facing copy. Delivery is
best-effort: a failure to enqueue is logged and never blocks recording
the interception.

The admin templates always show the effective group the spend is
attributed to, and note when the limit comes from a per-user override
rather than the group budget.

Depends on https://github.com/coder/coder/pull/27346

## Screenshots:
<img width="1101" height="440" alt="image"
src="https://github.com/user-attachments/assets/eb731088-05c8-47bd-9d06-fc9d07f63a08"
/>

<img width="468" height="391" alt="image"
src="https://github.com/user-attachments/assets/b89b76a6-3fa8-4735-99a2-43e119a7a7e3"
/>
This commit is contained in:
Yevhenii Shcherbina
2026-07-27 17:02:28 -04:00
committed by GitHub
parent 51ac968d5a
commit 2574e6b785
13 changed files with 602 additions and 54 deletions
@@ -1497,6 +1497,45 @@ func TestNotificationTemplates_Golden(t *testing.T) {
Data: map[string]any{},
},
},
{
name: "TemplateAIBudgetWarningAdmin",
id: notifications.TemplateAIBudgetWarningAdmin,
payload: types.MessagePayload{
UserName: "Bobby",
UserEmail: "bobby@coder.com",
UserUsername: "bobby",
Labels: map[string]string{
"username": "alice",
"threshold": "85",
"limit": "$1000.00",
"period": "monthly",
"limit_source": "group",
"effective_group_name": "Engineering",
"period_start": "July 1, 2026",
"period_end": "August 1, 2026",
},
Data: map[string]any{},
},
},
{
name: "TemplateAIBudgetLimitReachedAdmin",
id: notifications.TemplateAIBudgetLimitReachedAdmin,
payload: types.MessagePayload{
UserName: "Bobby",
UserEmail: "bobby@coder.com",
UserUsername: "bobby",
Labels: map[string]string{
"username": "alice",
"limit": "$1000.00",
"period": "monthly",
"limit_source": "user_override",
"effective_group_name": "Engineering",
"period_start": "July 1, 2026",
"period_end": "August 1, 2026",
},
Data: map[string]any{},
},
},
}
// We must have a test case for every notification_template. This is enforced below: