Skip to content

fix(billing): take a hand-set Enterprise AI budget only at the plan's $50 or more - #1675

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/enterprise-ai-budget-bounds
Oct 28, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/enterprise-ai-budget-bounds

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from #1598 (item 1): a hand-set Enterprise AI budget is taken only at the plan's $50 or more.

The comment said "$50 or more", but any finite number was taken:

  • 0 or a negative number made every AI call answer "budget used up" (reserveAiBudget);
  • -1 read as unlimited.

applyPlanOverrides now takes plan_overrides.improveMonthlyBudgetUsd only when it's a number at or above the plan's budget: Enterprise's $50, the floor that the billing cards and the AI usage tiers name. Anything else keeps the plan's budget. Unlimited AI isn't something a customer gets by hand.

The web only merges overrides raw (no consumer reads the budget there). The meter and the SQL take the budget from the server.

Tests:

  • 0, -5, -1 and just under the plan's budget all keep the plan's;
  • the AI usage meter reads an override of 0 as the plan's $50;
  • the meter's own $0-budget test mocks a plan with 0 instead of setting it by hand.

Review fix (97b38be): that mock is undone in afterEach, so a failing test can't leak it.

Item 2 (#1604's dialog reusing aiUsageCustom) belongs to #1604.

No visible change; this is a guard.

Related issue

Follow-up from #1598's review.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

cd server && npx vitest run src/lib/plan-guard.test.js src/lib/plan-guard-ai-usage.test.js

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Full checks were run in one tree with this batch's ten follow-up branches merged, and again after the review fixes:

  • server format, lint and npm test: 246 files, 3,343 tests passed;
  • web format, lint (0 errors; 7 warnings already on main), typecheck and build: all pass;
  • web vitest: 447 files, 4,352 tests passed (after the review fixes).

A pre-push review was run on all ten branches.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 28, 2026 05:35
… $50 or more

Follow-up from ansvisor#1598 (item 1). The comment said "$50 or more", but any
finite number was taken: 0 or a negative number made every AI call answer
"budget used up" (reserveAiBudget), and -1 read as unlimited.
applyPlanOverrides now takes plan_overrides.improveMonthlyBudgetUsd only
when it's a number at or above the plan's budget (Enterprise's $50, the
floor the billing cards and the AI usage tiers name); anything else keeps
the plan's. Unlimited AI isn't something a customer is given by hand.

Tests: 0, -5, -1 and just under the plan's keep the plan's; the AI usage
meter reads an override of 0 on the plan's $50. The meter's own test of a
$0 budget (still read as used up) now mocks a plan with 0 instead of
setting one by hand.

Item 2 (ansvisor#1604's dialog reusing aiUsageCustom) is ansvisor#1604's to take when it's
rebased.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n't leak it

Review follow-up. The "budget of nothing" test undid its plans.js mock at
the end of its own body, so a failure there left the next tests reading a
$0 budget. The unmock is in afterEach now, beside the supabase one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Item 1 of the #1598 review: Done.

What I checked:

  • Own change. Two commits (da52281, 97b38be) on 3f6a4d4. It merges cleanly on main (b591947), including with #1629's rework of getAiUsage and its tests: the merged "budget of nothing" test keeps this PR's mocked plan and main's features expectation ([]). It merges cleanly with #1670, #1671 and #1674 too.
  • The floor.
    • applyPlanOverrides takes plan_overrides.improveMonthlyBudgetUsd only as a finite number at or above the plan's own budget. 0, -5, -1, 4.98 (against a mocked $4.99) and anything that isn't a number now keep the plan's. A missing key still leaves the plan's budget alone.
    • Only Enterprise organizations reach this branch, and every reader of the budget (reserveAiBudget, the usage reads, the Improve and task-writer checks) goes through resolveOrgPlanByOrgId. The migrations and the web never read the override themselves.
  • Tests.
    • At the head, the five plan-guard* files pass (58). On the tree merged with main, they pass too (60).
    • With main's plan-guard.js and this PR's tests, the new floor test and the new meter test fail.
    • The doUnmock in afterEach, after the resetModules in beforeEach, keeps the mocked plan from reaching the next test.

Before the API deploy: no visible change. The floor applies once the API is deployed.

@gkhngyk
gkhngyk merged commit af96026 into ansvisor:main Oct 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants