fix(usage): keep Command Code extra credits out of the monthly-cap usage

With a known plan cap, used was cap minus ALL remaining credits, so purchased or
free credits hid an exhausted monthly allowance (used=0, 100% left). Used now
follows the monthly pool only; the extra credits still extend the total so the
account stays spendable for quota preflight.
This commit is contained in:
serenity
2026-11-19 00:45:30 -03:00
committed by diegosouzapw
parent 6858939017
commit c0cbf22446
3 changed files with 40 additions and 9 deletions

View File

@@ -231,9 +231,17 @@ export async function getCommandCodeUsage(apiKey: string, connectionId?: string)
const weekly = windowQuota(windowLimits.weekly, "Weekly window");
if (weekly) quotas.weekly = weekly;
const creditsTotal = creditCap ?? periodUsed + remaining;
// The cap bounds the monthly allowance only. Purchased/free credits are a
// separate pool: they never shrink the monthly usage (cap - monthly), but
// they stay spendable, so they extend the total next to `remaining`.
const monthlyRemaining = Math.max(0, toNumber(creditsObj.monthlyCredits, 0));
const extraRemaining = remaining - monthlyRemaining;
const creditsTotal =
creditCap != null
? Math.max(creditCap, monthlyRemaining) + extraRemaining
: periodUsed + remaining;
const creditsUsed =
creditCap != null ? Math.max(0, creditCap - remaining) : Math.max(0, periodUsed);
creditCap != null ? Math.max(0, creditCap - monthlyRemaining) : Math.max(0, periodUsed);
const creditsRemainingPct =
creditsTotal > 0
? Math.round((remaining / creditsTotal) * 1000) / 10

View File

@@ -12,7 +12,12 @@ const { getCommandCodeUsage } = __testing;
const originalFetch = globalThis.fetch;
type QuotaShape = { used: number; total: number; remaining?: number };
type QuotaShape = {
used: number;
total: number;
remaining?: number;
remainingPercentage?: number;
};
function jsonResponse(body: unknown, status = 200) {
return new Response(JSON.stringify(body), {
@@ -40,11 +45,11 @@ function subscription(planId: string) {
const SUMMARY = { totalCost: 0 };
function installMockFetch(planId: string) {
function installMockFetch(planId: string, credits: unknown = CREDITS) {
globalThis.fetch = async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes("/alpha/whoami")) return jsonResponse(WHOAMI);
if (url.includes("/alpha/billing/credits")) return jsonResponse(CREDITS);
if (url.includes("/alpha/billing/credits")) return jsonResponse(credits);
if (url.includes("/alpha/billing/subscriptions")) return jsonResponse(subscription(planId));
if (url.includes("/alpha/usage/summary")) return jsonResponse(SUMMARY);
return jsonResponse({ error: `unhandled fetch: ${url}` }, 500);
@@ -88,6 +93,23 @@ describe("Command Code short plan ids and credit caps", () => {
assert.equal(r.quotas?.credits?.used, 36);
});
it("an exhausted monthly allowance shows as used even when extra credits remain", async () => {
// Purchased/free credits are a separate pool from the plan's monthly cap: they
// must not be subtracted from the cap (that reported used=0 / 100% left), but
// they still count as spendable balance on top of the cap.
installMockFetch("go", {
credits: { monthlyCredits: 0, purchasedCredits: 10, freeCredits: 0 },
windowLimits: {},
});
const r = (await getCommandCodeUsage("cc-key")) as {
quotas?: { credits?: QuotaShape };
};
assert.equal(r.quotas?.credits?.used, 10); // the whole 10-credit monthly cap
assert.equal(r.quotas?.credits?.remaining, 10); // the purchased credits
assert.equal(r.quotas?.credits?.total, 20); // monthly cap + extra credits
assert.equal(r.quotas?.credits?.remainingPercentage, 50);
});
it("a plan without a known cap keeps the spend-derived total", async () => {
installMockFetch("individual-max-10x");
const r = (await getCommandCodeUsage("cc-key")) as {

View File

@@ -154,10 +154,11 @@ describe("Command Code usage dispatch", () => {
const remainingCredits = 34.9522404823 + 0.0446690956;
assert.ok(Math.abs((r.quotas!.credits!.remaining ?? 0) - remainingCredits) < 1e-9);
// individual-goat has a known 70-credit cap: the total is the cap and used
// follows from remaining (the summary spend can lag the credit pools).
assert.ok(Math.abs(r.quotas!.credits!.used - (70 - remainingCredits)) < 1e-9);
assert.ok(Math.abs(r.quotas!.credits!.total - 70) < 1e-9);
// individual-goat has a known 70-credit cap: used follows from the monthly
// pool left under the cap (the summary spend can lag the credit pools), and
// the purchased credits extend the total without counting as monthly usage.
assert.ok(Math.abs(r.quotas!.credits!.used - (70 - 34.9522404823)) < 1e-9);
assert.ok(Math.abs(r.quotas!.credits!.total - (70 + 0.0446690956)) < 1e-9);
assert.equal(r.quotas!.credits!.resetAt, "2026-09-05T22:27:41.000Z");
assert.equal(r.quotas!.credits!.currency, "USD");
});