feat(ai): enforced budgets + the callModel funnel — metering that can refuse #5626
ccantynzAI Reviewcommented 19h ago
AI Triage
(no summary)
Priority: medium Risk area: mixed
Suggested labels: (no label suggestions) Suggested reviewers: (no reviewer suggestions)
Suggestions only — nothing has been applied. The PR author stays in control.
gluecron[bot]🤖 botAI Reviewcommented 19h ago
AI review unavailable
The platform's AI balance is exhausted, so AI generation is temporarily unavailable. Nothing was lost. You can queue this as a repair for the internal agent from the repository's Health page, or try again once the balance is restored. The PR is otherwise unchanged.
Cross-repo impact
See what breaks downstream if this PR merges.
⮌ Merged
This pull request was merged into main.
c comment · e edit title · m merge · a approve · r request changes · ? shortcuts
Summary
PR A of the enterprise metering build (from the 2026-09-02 audit: 74 call sites / 16 metered / 5 quota-checked / zero dollar-cap refusals; per-org spend unrepresentable).
ai_cost_events.org_id(+index) andorg_ai_budgets(monthly_cents,per_category_centsJSONB → per-feature allocation, the "big corps allocate so much AI funding" model).recordAiCostcarriesorgId, derived fromrepositoryIdvia a 5-min-cached lookup when not passed.assertAiSpend()— the first pre-call refusal path: platform daily ceiling → org monthly cap → per-category allocation; throwsAiBudgetExceededError(scope + spent + cap). Fails open on ledger faults; cap 0 = no cap, never "spend nothing".callModel()— gate → call →noteAiFailure→recordAiCostin one wrapper; the only sanctioned way for new AI code to reach the model.ai-metering-coverage.test.ts— pins the 54 legacy direct-caller files as a shrink-only list; a new direct caller fails CI with instructions to usecallModel().getAgentUsageand refuses before spending;chargeAgent's previously-discarded boolean is honoured, with overshoot recorded viarecordAgentOverdraftinstead of silently vanishing from the books.Test plan
tsc --noEmitclean,bun buildcleanbun run db:migrateon deploy applies 0136