Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(#4507): count Codex credits only for a positive numeric balance or unlimited

Rino Cheng3a4bb37a8592
diff --git a/server/plan-usage.test.ts b/server/plan-usage.test.ts
index f62e5b1ccf..c14b881c97 100644
--- a/server/plan-usage.test.ts
+++ b/server/plan-usage.test.ts
@@ -173,6 +173,23 @@ describe("plan usage parsers", () => {
expect(JSON.stringify(parsed)).not.toContain(SECRET);
});
+ it("marks Codex credits only for a positive balance or an unlimited account", () => {
+ const flag = (credits: unknown) => parseCodexUsage({ plan_type: "plus", ...(credits === undefined ? {} : { credits }) }).codexCredits;
+ expect(flag({ has_credits: true, unlimited: false, balance: "12.50" })).toBe(true);
+ expect(flag({ has_credits: false, unlimited: true, balance: null })).toBe(true);
+ const odd = [
+ undefined, null, "yes", 1, [], {},
+ { has_credits: "true", unlimited: 1 }, { has_credits: null, unlimited: null },
+ { has_credits: false, unlimited: false, balance: "0" },
+ { has_credits: true, unlimited: false, balance: "0" },
+ { has_credits: true, balance: null },
+ { has_credits: true, balance: "abc" },
+ { has_credits: true, balance: true },
+ { has_credits: true, balance: "1,234.00" },
+ ];
+ for (const credits of odd) expect(flag(credits)).toBeUndefined();
+ });
+
it("keeps Codex model windows separate from the account plan", () => {
const parsed = parseCodexUsage({
plan_type: "pro",
diff --git a/server/plan-usage.ts b/server/plan-usage.ts
index fbd1c97d6d..1cfe17a4cb 100644
--- a/server/plan-usage.ts
+++ b/server/plan-usage.ts
@@ -217,7 +217,7 @@ function codexHasCredits(value: unknown): boolean {
if (credits.unlimited === true) return true;
if (credits.has_credits !== true) return false;
const balance = typeof credits.balance === "string" ? Number(credits.balance) : credits.balance;
- return !(typeof balance === "number" && balance <= 0);
+ return typeof balance === "number" && Number.isFinite(balance) && balance > 0;
}
/** Whether Codex's ChatGPT credits are on and not used up: sends then go on past a full window and are paid from them. */