CodeIssuesPull RequestsActionsSecurityInsights
✨ AI
More
Settings

feat(ai): enforced budgets + the callModel funnel — metering that can refuse #5626

MergedXSccantynz wants to mergefeat/ai-metering-coremainopened 19h ago3/4 tasks
ccantynzcommented 19h ago

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).

  • Migration 0136: ai_cost_events.org_id (+index) and org_ai_budgets (monthly_cents, per_category_cents JSONB → per-feature allocation, the "big corps allocate so much AI funding" model).
  • recordAiCost carries orgId, derived from repositoryId via a 5-min-cached lookup when not passed.
  • assertAiSpend() — the first pre-call refusal path: platform daily ceiling → org monthly cap → per-category allocation; throws AiBudgetExceededError (scope + spent + cap). Fails open on ledger faults; cap 0 = no cap, never "spend nothing".
  • callModel() — gate → call → noteAiFailurerecordAiCost in 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 use callModel().
  • Agent daily caps enforced for real: hosted loop pre-checks getAgentUsage and refuses before spending; chargeAgent's previously-discarded boolean is honoured, with overshoot recorded via recordAgentOverdraft instead of silently vanishing from the books.

Test plan

  • New: ai-metering-coverage (2), ai-budget-enforcement (4) — pass
  • Neighbors: agent-multiplayer, ai-budget, ai-cost-tracker, hosted-claude-loop, hosted-loop-secrets, hosted-loop-spend-floor — 85 pass, 0 fail
  • tsc --noEmit clean, bun build clean
  • bun run db:migrate on deploy applies 0136
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.

Analyze →
⮌ Merged

This pull request was merged into main.

c comment · e edit title · m merge · a approve · r request changes · ? shortcuts