Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

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

Cihanaf960262e280
diff --git a/server/src/lib/plan-guard-ai-usage.test.js b/server/src/lib/plan-guard-ai-usage.test.js
index 51fca7ca7..b2ed930c7 100644
--- a/server/src/lib/plan-guard-ai-usage.test.js
+++ b/server/src/lib/plan-guard-ai-usage.test.js
@@ -68,6 +68,8 @@ afterEach(() => {
vi.useRealTimers();
delete process.env.IS_CLOUD;
vi.doUnmock('../config/supabase.js');
+ // One test reads plans.js mocked: here, so a failing one can't pass it on.
+ vi.doUnmock('../config/plans.js');
});
describe('getAiUsage', () => {
@@ -174,13 +176,33 @@ describe('getAiUsage', () => {
});
it('reads a budget of nothing as used up, as the AI features are', async () => {
- org = { ...org, plan: 'enterprise', plan_overrides: { improveMonthlyBudgetUsd: 0 } };
+ // No plan has one, and a budget set by hand below the plan's isn't
+ // taken (#1598): a plan read with 0 all the same still reads used up.
+ vi.resetModules();
+ vi.doMock('../config/plans.js', async (importOriginal) => {
+ const real = await importOriginal();
+ return {
+ ...real,
+ getPlan: (id) => {
+ const plan = real.getPlan(id);
+ return { ...plan, limits: { ...plan.limits, improveMonthlyBudgetUsd: 0 } };
+ },
+ };
+ });
+ guard = await import('./plan-guard.js');
spend = [];
const usage = await guard.getAiUsage('o1');
expect(usage.percentUsed).toBe(100);
expect(usage.features).toEqual([]);
});
+ it('meters an Enterprise budget set below the plan’s on the plan’s $50 (#1598)', async () => {
+ org = { ...org, plan: 'enterprise', plan_overrides: { improveMonthlyBudgetUsd: 0 } };
+ spend = [{ feature: 'improve', cost_usd: 5 }];
+ const usage = await guard.getAiUsage('o1');
+ expect(usage.percentUsed).toBe(10);
+ });
+
it('has no meter without an active plan', async () => {
org = { ...org, subscription_status: 'incomplete' };
await expect(guard.getAiUsage('o1')).rejects.toMatchObject({ statusCode: 402 });
diff --git a/server/src/lib/plan-guard.js b/server/src/lib/plan-guard.js
index 857b0d336..77f5a0927 100644
--- a/server/src/lib/plan-guard.js
+++ b/server/src/lib/plan-guard.js
@@ -38,7 +38,7 @@ export class PlanLimitError extends Error {
* organizations.plan_overrides (jsonb) — mirrors web getOrgPlan().
*
* The AI budget is the plan's $50 unless Ansvisor sets a customer's own in
- * `improveMonthlyBudgetUsd` (2026-10-27: $50 or more, a finite number, set per customer); the
+ * `improveMonthlyBudgetUsd` (2026-10-27: $50 or more, set per customer; anything else keeps $50); the
* contract's value doesn't raise it. `contractMonthlyUsd` is the contract's
* monthly value, entered when the deal is signed. It sets the relevance
* budget (VIS-011) to 0.1% of the contract (never below the plan default),
@@ -54,12 +54,14 @@ export function applyPlanOverrides(plan, org) {
) {
const overrides = org.plan_overrides;
const limits = { ...plan.limits, ...overrides };
- // A budget set by hand is used only as a number: null would read as
- // unlimited, and "80$" would fail every AI call. Anything else keeps
- // the plan's.
+ // A budget set by hand is used only as a number of at least the plan's
+ // ($50, Enterprise's floor): null or -1 would read as unlimited, 0 or
+ // another number below it (-5) would answer every AI call "used up",
+ // and "80$" would fail every one. Anything else keeps the plan's.
const own = overrides.improveMonthlyBudgetUsd;
- if (own !== undefined && !(typeof own === 'number' && Number.isFinite(own))) {
- limits.improveMonthlyBudgetUsd = plan.limits.improveMonthlyBudgetUsd;
+ const floor = plan.limits.improveMonthlyBudgetUsd;
+ if (own !== undefined && !(typeof own === 'number' && Number.isFinite(own) && own >= floor)) {
+ limits.improveMonthlyBudgetUsd = floor;
}
const usd = Number(overrides.contractMonthlyUsd);
if (overrides.relevanceMonthlyBudgetUsd === undefined && Number.isFinite(usd) && usd > 0) {
diff --git a/server/src/lib/plan-guard.test.js b/server/src/lib/plan-guard.test.js
index 428366a3b..66bf027ed 100644
--- a/server/src/lib/plan-guard.test.js
+++ b/server/src/lib/plan-guard.test.js
@@ -75,6 +75,14 @@ describe('applyPlanOverrides — the Enterprise AI budget', () => {
}
});
+ it('keeps the plan’s budget, its floor, for one set below it (#1598)', () => {
+ // 0 or -5 would answer every AI call "used up", -1 would read as unlimited.
+ for (const own of [0, -5, -1, 4.98]) {
+ expect(budget({ improveMonthlyBudgetUsd: own })).toBe(4.99);
+ }
+ expect(budget({ improveMonthlyBudgetUsd: 4.99 })).toBe(4.99);
+ });
+
it('keeps the default without a usable contract value', () => {
expect(budget({ maxPrompts: 900 })).toBe(4.99);
expect(budget({ contractMonthlyUsd: 'n/a' })).toBe(4.99);