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
refactor(api): remove the empty batch billing wrapper (#5701)
Micah Stairsd43f75a05daa
diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.tsindex e9c62ea26..eb040fde9 100644--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts@@ -5,11 +5,9 @@ import { vi } from "vitest";// stays module-level: its factory only captures it lazily.const {logger,- withAuth,trackCredits,refundCredits,reportExchangeBilling,- billTeam7,getACUCTeam,} = vi.hoisted(() => {const logger: any = {@@ -20,11 +18,9 @@ const {};return {logger,- withAuth: vi.fn((fn: any) => fn),trackCredits: vi.fn<(args: any) => Promise<boolean>>(),refundCredits: vi.fn<(args: any) => Promise<void>>(),reportExchangeBilling: vi.fn<(args: any) => Promise<void>>(),- billTeam7: vi.fn<(params: any) => Promise<{ api_key: string }[]>>(),getACUCTeam: vi.fn<(teamId: string) => Promise<any>>(),};});@@ -33,10 +29,6 @@ vi.mock("../../../lib/logger", () => ({logger,}));-vi.mock("../../../lib/withAuth", () => ({- withAuth,-}));-vi.mock("../../autumn/autumn.service", () => ({autumnService: {trackCredits,@@ -51,10 +43,6 @@ vi.mock("../../../lib/exchange", () => ({reportExchangeBilling,}));-vi.mock("../../../db/rpc", () => ({- billTeam7,-}));-// orgIdFromAcuc answers null without it, so the legacy op resolves no org.vi.mock("../../../config", () => ({ config: { USE_DB_AUTHENTICATION: true } }));@@ -150,22 +138,19 @@ beforeEach(() => {queue = [];billedTeams.clear();locks.clear();- billTeam7.mockResolvedValue([]);trackCredits.mockResolvedValue(true);refundCredits.mockResolvedValue(undefined);getACUCTeam.mockResolvedValue({ team_id: "team-1", org_id: "org-legacy" });});describe("processBillingBatch", () => {- it("finalizes the group without writing credit_usage or re-tracking Autumn", async () => {- // Request-time tracking is the charge. The batch must not track again,- // must not refund a charge that finalized, and must not call bill_team_7,- // which only inserted a credit_usage row.+ it("finalizes the group without re-tracking or refunding the Autumn charge", async () => {+ // Request-time tracking is the charge. The batch must not track again+ // and must not refund a charge that finalized.queue = [makeOp()];await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(queue).toHaveLength(0);@@ -179,7 +164,6 @@ describe("processBillingBatch", () => {await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();});@@ -195,7 +179,6 @@ describe("processBillingBatch", () => {await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(reportExchangeBilling).toHaveBeenCalledWith({accessEventId: "evt-1",@@ -218,7 +201,6 @@ describe("processBillingBatch", () => {await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();expect(logger.info).toHaveBeenCalledWith(@@ -229,7 +211,7 @@ describe("processBillingBatch", () => {);});- it("refunds request-tracked credits when finalization returns success false, then continues", async () => {+ it("refunds when finalization throws and still finalizes the next group if the refund fails", async () => {queue = [makeOp({team_id: "team-1",@@ -242,69 +224,42 @@ describe("processBillingBatch", () => {exchange_access_event_id: "evt-ok",}),];- withAuth.mockImplementationOnce(() => async () => ({ success: false }));-- await processBillingBatch();-- expect(refundCredits).toHaveBeenCalledExactlyOnceWith({- teamId: "team-1",- orgId: "org-1",- value: 10,- properties: {- source: "processBillingBatch",- endpoint: "extract",- apiKeyId: 123,- },- featureId: "CREDITS",- });- expect(reportExchangeBilling).toHaveBeenCalledExactlyOnceWith({- accessEventId: "evt-ok",- status: "confirmed",- });- expect(logger.info).toHaveBeenCalledWith(- "β Successfully billed team team-2 for 10 credits",- );- expect(billTeam7).not.toHaveBeenCalled();- expect(trackCredits).not.toHaveBeenCalled();- });-- it("refunds when finalization throws and still finalizes the next group if the refund fails", async () => {- queue = [- makeOp({- team_id: "team-1",- autumnTrackInRequest: true,- }),- makeOp({- team_id: "team-2",- autumnTrackInRequest: true,- }),- ];- withAuth.mockImplementationOnce(() => async () => {- throw new Error("finalization failed");+ logger.info.mockImplementation((message: string) => {+ if (message === "β Successfully billed team team-1 for 10 credits") {+ throw new Error("finalization failed");+ }});refundCredits.mockRejectedValueOnce(new Error("refund failed"));- await processBillingBatch();-- expect(refundCredits).toHaveBeenCalledExactlyOnceWith({- teamId: "team-1",- orgId: "org-1",- value: 10,- properties: {- source: "processBillingBatch",- endpoint: "extract",- apiKeyId: 123,- },- featureId: "CREDITS",- });- expect(logger.warn).toHaveBeenCalledWith(- "Failed to refund Autumn request-tracked credits",- expect.objectContaining({ team_id: "team-1", credits: 10 }),- );- expect(logger.info).toHaveBeenCalledWith(- "β Successfully billed team team-2 for 10 credits",- );- expect(billTeam7).not.toHaveBeenCalled();+ try {+ await processBillingBatch();++ expect(refundCredits).toHaveBeenCalledExactlyOnceWith({+ teamId: "team-1",+ orgId: "org-1",+ value: 10,+ properties: {+ source: "processBillingBatch",+ endpoint: "extract",+ apiKeyId: 123,+ },+ featureId: "CREDITS",+ });+ expect(logger.warn).toHaveBeenCalledWith(+ "Failed to refund Autumn request-tracked credits",+ expect.objectContaining({ team_id: "team-1", credits: 10 }),+ );+ expect(reportExchangeBilling).toHaveBeenCalledExactlyOnceWith({+ accessEventId: "evt-ok",+ status: "confirmed",+ });+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-2 for 10 credits",+ );+ expect(trackCredits).not.toHaveBeenCalled();+ } finally {+ logger.info.mockReset();+ }});// Transitional: operations enqueued before org_id was carried. Remove with@@ -318,7 +273,6 @@ describe("processBillingBatch", () => {await processBillingBatch();expect(getACUCTeam).toHaveBeenCalledTimes(1);- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(logger.info).toHaveBeenCalledWith("β Successfully billed team team-1 for 20 credits",@@ -336,7 +290,6 @@ describe("processBillingBatch", () => {// Nothing billed and nothing refunded for them; both are back on the// queue in their original shape, still without org_id.- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(queue).toHaveLength(2);expect(JSON.parse(queue[0])).not.toHaveProperty("org_id");@@ -352,7 +305,6 @@ describe("processBillingBatch", () => {await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();// A confirmed null is an org-less team: nothing is refunded, and the// charge was never tracked to Autumn for lack of an org.expect(refundCredits).not.toHaveBeenCalled();@@ -365,7 +317,6 @@ describe("processBillingBatch", () => {await processBillingBatch();expect(getACUCTeam).not.toHaveBeenCalled();- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();});@@ -375,7 +326,6 @@ describe("processBillingBatch", () => {await processBillingBatch();expect(getACUCTeam).not.toHaveBeenCalled();- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(queue).toHaveLength(0);});@@ -390,12 +340,11 @@ describe("hosted MCP charges", () => {...overrides,});- it("finalizes a hosted charge without writing credit_usage or refunding it", async () => {+ it("finalizes a hosted charge without refunding it", async () => {queue = [hosted(), makeOp({ autumnTrackInRequest: true })];await processBillingBatch();- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();expect(logger.info).toHaveBeenCalledWith(@@ -430,7 +379,6 @@ describe("startBillingBatchProcessing", () => {queue = [makeOp()];await vi.advanceTimersByTimeAsync(15000);- expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();expect(queue).toHaveLength(0);expect(logger.info).toHaveBeenCalledWith(diff --git a/apps/api/src/services/billing/__tests__/credit_billing.test.ts b/apps/api/src/services/billing/__tests__/credit_billing.test.tsindex 4684b8a61..0d48c9097 100644--- a/apps/api/src/services/billing/__tests__/credit_billing.test.ts+++ b/apps/api/src/services/billing/__tests__/credit_billing.test.ts@@ -138,7 +138,7 @@ describe("billTeam", () => {});// preview/keyless teams have no org, and this is the one boundary that- // accepts that: the ledger is still enqueued, Autumn is simply not told.+ // accepts that: the operation is still enqueued, Autumn is simply not told.it("skips Autumn entirely when the team has no org", async () => {await billTeam("preview_abc", null, 3, null, {endpoint: "search",@@ -187,7 +187,7 @@ describe("billTeam for a hosted MCP call", () => {hostedMcp: true,};- it("charges the whole call on MCP credits and queues an ordinary ledger operation", async () => {+ it("charges the whole call on MCP credits and queues the billing operation", async () => {await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);expect(trackCredits).toHaveBeenCalledExactlyOnceWith(diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.tsindex 3fe810fe2..b00a7e05f 100644--- a/apps/api/src/services/billing/batch_billing.ts+++ b/apps/api/src/services/billing/batch_billing.ts@@ -1,6 +1,5 @@import { logger } from "../../lib/logger";import { getRedisConnection } from "../queue-service";-import { withAuth } from "../../lib/withAuth";import {autumnService,featureIdForBillingEndpoint,@@ -99,9 +98,9 @@ interface BillingOperation {timestamp: string;api_key_id: number | null;autumnTrackInRequest: boolean;- // Exchange access backing this operation, if any: its ledger event is- // confirmed once the debit commits. Failed or ambiguous commits leave- // the event pending for reconciliation rather than voiding it.+ // Exchange access backing this operation, if any. Confirmed once the+ // group finalizes. A finalization that throws leaves the event pending+ // for reconciliation rather than voiding it.exchange_access_event_id?: string;billing_reference?: string;}@@ -204,10 +203,8 @@ async function refundRequestTrackedCredits(/*** Dequeues pending billing operations from Redis and finalizes each group.- * The Autumn charge already happened at request time. This used to record the- * group by calling `bill_team_7`, whose only write is an insert into- * `credit_usage`; that call is skipped. A failed finalization still refunds- * the request-time charge, and a committed group still confirms its Exchange+ * The Autumn charge already happened at request time. A finalization that+ * throws refunds that charge, and a finalized group confirms its Exchange* accesses.*/export async function processBillingBatch() {@@ -218,8 +215,8 @@ export async function processBillingBatch() {return;}- // Exchange operations whose debit committed this run; their ledger- // confirmations are delivered after the lock is released.+ // Exchange operations finalized this run; their confirmations are+ // delivered after the lock is released.const committedExchangeOps: BillingOperation[] = [];try {@@ -348,44 +345,13 @@ export async function processBillingBatch() {}try {- // Finalize the group. Charging already happened; this no longer- // writes a credit_usage row.- const billingResult = await withAuth(supaBillTeam, {- success: true,- message: "No DB, bypassed.",- })(- group.team_id,- group.total_credits,- group.api_key_id,- logger,- group.is_extract,- );-- if (!billingResult.success) {- await refundRequestTrackedCredits(group, resolveOrgIdForTeam);- // Deliberately no Exchange outcome here: supaBillTeam maps thrown- // errors to success: false, and a transport error can occur after- // the debit committed, so voiding could erase a real debit. The- // events stay pending on the Exchange, which flags unresolved- // events for reconciliation.- logger.warn(- `β οΈ Billing returned success: false for team ${group.team_id}`,- {- billingResult,- team_id: group.team_id,- credits: group.total_credits,- },- );- continue;- }-logger.info(`β Successfully billed team ${group.team_id} for ${group.total_credits} credits`,);// Usage is tracked to Autumn at request time, not here.- // The debit is committed: confirm the Exchange accesses it covered+ // The group is finalized: confirm the Exchange accesses it covered// once the batch lock is released.committedExchangeOps.push(...group.operations.filter(@@ -394,8 +360,7 @@ export async function processBillingBatch() {);} catch (error) {await refundRequestTrackedCredits(group, resolveOrgIdForTeam);- // No Exchange outcome here either β same ambiguity as the- // success: false branch above; the events stay pending.+ // No Exchange outcome: the events stay pending for reconciliation.logger.error(`β Failed to bill team ${group.team_id}`, {error,group,@@ -438,8 +403,6 @@ export function startBillingBatchProcessing() {/*** Enqueues a billing operation for async batch processing.- *- * Internal billing operations are batched and committed to Supabase.*/export async function queueBillingOperation(team_id: string,@@ -517,35 +480,6 @@ export async function queueBillingOperation(}}-// Modified version of the billing function for batch operations-async function supaBillTeam(- team_id: string,- credits: number,- _api_key_id: number | null,- __logger?: any,- _is_extract: boolean = false,-) {- const _logger = (__logger ?? logger).child({- module: "credit_billing",- method: "supaBillTeam",- teamId: team_id,- credits,- });-- if (team_id === "preview" || team_id.startsWith("preview_")) {- return { success: true, message: "Preview team, no credits used" };- }-- _logger.info(`Batch billing team ${team_id} for ${credits} credits`);-- // `bill_team_7` inserted one `credit_usage` row and returned the team's API- // keys, which this batch does not read. It does not deduct credits. The- // charge is the request-time Autumn track, so the group is finalized- // without that insert. Success leaves the charge in place and lets the- // caller confirm Exchange accesses.- return { success: true };-}-// Cleanup on exitprocess.on("beforeExit", async () => {if (batchInterval) {