Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(billing): carry Firebill's reset time into out-of-credits 402s (#5532)
Rakshith Ramprakash9992cd4b2699
diff --git a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts b/apps/api/src/services/autumn/__tests__/autumn.service.test.tsindex a5f67e769..9afa22eb7 100644--- a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts@@ -1252,6 +1252,37 @@ describe("firebill routing", () => {});});+ // The reset time the out-of-credits 402 names. Every org is routed, so this+ // is the only path it can come from.+ it("carries firebill's reset time on a check", async () => {+ state.configRef = firebillConfig();+ mockFetch.mockResolvedValue(+ new Response(+ JSON.stringify({+ success: true,+ allowed: false,+ remaining: 3,+ resets_at: 1_798_761_600_000,+ }),+ { status: 200 },+ ),+ );+ const svc = makeService();++ const result = await svc.checkCredits({+ teamId: "team-1",+ orgId: "org-1",+ value: 100,+ properties: {},+ });++ expect(result).toEqual({+ allowed: false,+ remaining: 3,+ resetsAt: 1_798_761_600_000,+ });+ });+// The same property as the charge paths, and it bites harder on the gate: a// provisioned org that misses firebill on a charge is billed to the wrong// account, but one that misses firebill on the *gate* is refused outright —diff --git a/apps/api/src/services/autumn/__tests__/firebill.test.ts b/apps/api/src/services/autumn/__tests__/firebill.test.tsindex 0a5c90c99..1493a3fab 100644--- a/apps/api/src/services/autumn/__tests__/firebill.test.ts+++ b/apps/api/src/services/autumn/__tests__/firebill.test.ts@@ -851,6 +851,46 @@ describe("firebillCheck", () => {});});+ // What a 402 names as the time credits come back.+ it("passes through the balance's reset time", async () => {+ answer({+ success: true,+ allowed: false,+ remaining: 0,+ resets_at: 1_798_761_600_000,+ });+ await expect(firebillCheck(checkParams)).resolves.toEqual({+ status: "answered",+ allowed: false,+ remaining: 0,+ resetsAt: 1_798_761_600_000,+ });+ });++ // A reset time is decoration on the answer, never a reason to fail open.+ it.each([+ ["a string", "2026-12-01T00:00:00Z"],+ ["a fraction", 1.5],+ ["zero", 0],+ ["past what a Date can hold", 8_640_000_000_000_001],+ ["null", null],+ ])(+ "drops a resets_at that is %s and keeps the answer",+ async (_label, resetsAt) => {+ answer({+ success: true,+ allowed: false,+ remaining: 0,+ resets_at: resetsAt,+ });+ await expect(firebillCheck(checkParams)).resolves.toEqual({+ status: "answered",+ allowed: false,+ remaining: 0,+ });+ },+ );+it("treats an absent remaining on an ALLOWED check as unbounded, not zero", async () => {answer({ success: true, allowed: true });await expect(firebillCheck(checkParams)).resolves.toEqual({diff --git a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts b/apps/api/src/services/autumn/__tests__/mcp-credits.test.tsindex 8cb3fcd1a..68e0da6f0 100644--- a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts+++ b/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts@@ -204,6 +204,32 @@ describe("the entity a charge is made against", () => {]);});+ it("carries the MCP allowance's reset time from firebill", async () => {+ mockFetch.mockImplementation(+ async () =>+ new Response(+ JSON.stringify({+ success: true,+ allowed: false,+ remaining: 0,+ resets_at: 1_798_848_000_000,+ }),+ ),+ );++ const result = await new AutumnService().checkCredits({+ ...charge,+ featureId: MCP_CREDITS_FEATURE_ID,+ });++ expect(result).toEqual(+ expect.objectContaining({+ allowed: false,+ resetsAt: 1_798_848_000_000,+ }),+ );+ });+it("checks and holds MCP credits with an empty entity", async () => {mockFetch.mockImplementation(async () =>diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.tsindex 99db94582..e5b92fa04 100644--- a/apps/api/src/services/autumn/autumn.service.ts+++ b/apps/api/src/services/autumn/autumn.service.ts@@ -551,6 +551,7 @@ export class AutumnService {return {allowed: result.allowed,remaining: result.remaining,+ ...(result.resetsAt ? { resetsAt: result.resetsAt } : {}),...(gatewayProvisioned ? { gatewayProvisioned } : {}),};}diff --git a/apps/api/src/services/autumn/firebill.ts b/apps/api/src/services/autumn/firebill.tsindex f0fe0eb05..0c5d4fb96 100644--- a/apps/api/src/services/autumn/firebill.ts+++ b/apps/api/src/services/autumn/firebill.ts@@ -248,6 +248,16 @@ const checkAnswerSchema = z.discriminatedUnion("success", [success: z.literal(true),allowed: z.boolean(),remaining: z.number().finite().optional().catch(undefined),+ // Epoch ms, within what a Date can hold: the 402 renders it with+ // toISOString(). Caught to undefined: a mis-shaped reset time must not turn+ // an answer into a fail-open.+ resets_at: z+ .number()+ .int()+ .positive()+ .max(8_640_000_000_000_000)+ .optional()+ .catch(undefined),}),z.object({ success: z.literal(false) }),]);@@ -660,7 +670,13 @@ async function firebillAttempt(* yes/no, `unavailable` means firebill could not answer at all.*/type FirebillCheckResult =- | { status: "answered"; allowed: boolean; remaining: number }+ | {+ status: "answered";+ allowed: boolean;+ remaining: number;+ /** When the balance behind the answer next resets, in epoch ms. */+ resetsAt?: number;+ }| { status: "unavailable" };/**@@ -795,7 +811,12 @@ export async function firebillCheck({remaining,});}- return { status: "answered", allowed: body.allowed, remaining };+ return {+ status: "answered",+ allowed: body.allowed,+ remaining,+ ...(body.resets_at ? { resetsAt: body.resets_at } : {}),+ };} catch (error) {const failure = classifyTransportError(error);// **Not "firebill is unavailable".** The request never completed, which is