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(api): stop batch billing from inserting credit_usage rows (#5699)
Micah Stairs7a02bb143d74
diff --git a/apps/api/src/db/rpc.ts b/apps/api/src/db/rpc.tsindex 8bf02e853..195e17a6d 100644--- a/apps/api/src/db/rpc.ts+++ b/apps/api/src/db/rpc.ts@@ -66,19 +66,6 @@ export function agentConsumeFreeRequestIfLeft();}-export function billTeam7(params: {- team_id: string;- subscription_id: string | null;- credits: number;- api_key_id: number | null;- is_extract: boolean;-}): Promise<{ api_key: string }[]> {- return execRows(- db,- sql`select * from bill_team_7(_team_id => ${params.team_id}, sub_id => ${params.subscription_id}, credits => ${params.credits}, i_api_key_id => ${params.api_key_id}, is_extract_param => ${params.is_extract})`,- );-}-export function diffGetLastScrape(i_team_id: string,i_url: string,diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.tsindex cfe223432..e9c62ea26 100644--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts@@ -8,6 +8,7 @@ const {withAuth,trackCredits,refundCredits,+ reportExchangeBilling,billTeam7,getACUCTeam,} = vi.hoisted(() => {@@ -22,6 +23,7 @@ const {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>>(),};@@ -45,6 +47,10 @@ vi.mock("../../autumn/autumn.service", () => ({endpoint === "search" ? "SEARCH_CREDITS" : "CREDITS",}));+vi.mock("../../../lib/exchange", () => ({+ reportExchangeBilling,+}));+vi.mock("../../../db/rpc", () => ({billTeam7,}));@@ -151,53 +157,96 @@ beforeEach(() => {});describe("processBillingBatch", () => {- it("commits the ledger but never re-tracks usage to Autumn", async () => {- // Even when an op was not request-tracked, the batch must not track usage- // to Autumn β request-time tracking is the single source, so re-tracking- // here would double-count. The batch only commits the ledger.+ 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.queue = [makeOp()];await processBillingBatch();- expect(billTeam7).toHaveBeenCalled();+ expect(billTeam7).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();+ expect(queue).toHaveLength(0);+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 10 credits",+ );});- it("does not re-track even when the op was already tracked at request time", async () => {+ it("does not re-track or refund when the op was already tracked at request time", async () => {queue = [makeOp({ autumnTrackInRequest: true })];await processBillingBatch();- expect(billTeam7).toHaveBeenCalled();+ expect(billTeam7).not.toHaveBeenCalled();expect(trackCredits).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();});- it("refunds request-tracked credits when billing returns success false", async () => {- queue = [makeOp({ autumnTrackInRequest: true })];- billTeam7.mockRejectedValueOnce(new Error("db failed"));+ it("confirms Exchange accesses for a committed group", async () => {+ queue = [+ makeOp({+ autumnTrackInRequest: true,+ exchange_access_event_id: "evt-1",+ billing_reference: "ref-1",+ }),+ ];await processBillingBatch();- expect(refundCredits).toHaveBeenCalledWith({- teamId: "team-1",- orgId: "org-1",- value: 10,- properties: {- source: "processBillingBatch",- endpoint: "extract",- apiKeyId: 123,- },- featureId: "CREDITS",+ expect(billTeam7).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();+ expect(reportExchangeBilling).toHaveBeenCalledWith({+ accessEventId: "evt-1",+ status: "confirmed",+ billingReference: "ref-1",});});- it("refunds when billing throws", async () => {- queue = [makeOp({ autumnTrackInRequest: true })];- billTeam7.mockRejectedValueOnce(new Error("rpc exploded"));+ it("finalizes every group without refunding", async () => {+ queue = [+ makeOp({+ team_id: "team-1",+ autumnTrackInRequest: true,+ }),+ makeOp({+ team_id: "team-2",+ autumnTrackInRequest: true,+ }),+ ];await processBillingBatch();- expect(refundCredits).toHaveBeenCalledWith({+ expect(billTeam7).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();+ expect(trackCredits).not.toHaveBeenCalled();+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 10 credits",+ );+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-2 for 10 credits",+ );+ });++ it("refunds request-tracked credits when finalization returns success false, then continues", async () => {+ queue = [+ makeOp({+ team_id: "team-1",+ autumnTrackInRequest: true,+ exchange_access_event_id: "evt-failed",+ }),+ makeOp({+ team_id: "team-2",+ autumnTrackInRequest: true,+ exchange_access_event_id: "evt-ok",+ }),+ ];+ withAuth.mockImplementationOnce(() => async () => ({ success: false }));++ await processBillingBatch();++ expect(refundCredits).toHaveBeenCalledExactlyOnceWith({teamId: "team-1",orgId: "org-1",value: 10,@@ -208,9 +257,18 @@ describe("processBillingBatch", () => {},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("continues processing later groups when an Autumn refund fails", async () => {+ it("refunds when finalization throws and still finalizes the next group if the refund fails", async () => {queue = [makeOp({team_id: "team-1",@@ -221,14 +279,14 @@ describe("processBillingBatch", () => {autumnTrackInRequest: true,}),];- billTeam7- .mockRejectedValueOnce(new Error("db failed"))- .mockResolvedValueOnce([]);+ withAuth.mockImplementationOnce(() => async () => {+ throw new Error("finalization failed");+ });refundCredits.mockRejectedValueOnce(new Error("refund failed"));await processBillingBatch();- expect(refundCredits).toHaveBeenCalledWith({+ expect(refundCredits).toHaveBeenCalledExactlyOnceWith({teamId: "team-1",orgId: "org-1",value: 10,@@ -239,9 +297,14 @@ describe("processBillingBatch", () => {},featureId: "CREDITS",});- expect(billTeam7).toHaveBeenCalledTimes(2);- // The batch never tracks usage to Autumn, regardless of the request-time flag.- expect(trackCredits).not.toHaveBeenCalled();+ 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();});// Transitional: operations enqueued before org_id was carried. Remove with@@ -251,13 +314,14 @@ describe("processBillingBatch", () => {makeOp({ org_id: undefined, autumnTrackInRequest: true }),makeOp({ org_id: undefined, autumnTrackInRequest: true }),];- billTeam7.mockRejectedValue(new Error("db failed"));await processBillingBatch();expect(getACUCTeam).toHaveBeenCalledTimes(1);- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ teamId: "team-1", orgId: "org-legacy" }),+ expect(billTeam7).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 20 credits",);});@@ -282,122 +346,37 @@ describe("processBillingBatch", () => {);});- it("bills a legacy operation whose team is confirmed to have no org", async () => {+ it("finalizes a legacy operation whose team is confirmed to have no org", async () => {queue = [makeOp({ org_id: undefined, autumnTrackInRequest: true })];getACUCTeam.mockResolvedValue({ team_id: "team-1", org_id: null });- billTeam7.mockRejectedValueOnce(new Error("db failed"));await processBillingBatch();- expect(billTeam7).toHaveBeenCalled();- // A confirmed null is an org-less team: the refund is skipped, not deferred.+ 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();expect(queue).toHaveLength(0);});it("does not look anything up for an operation that carries its org", async () => {queue = [makeOp({ org_id: "org-1", autumnTrackInRequest: true })];- billTeam7.mockRejectedValueOnce(new Error("db failed"));await processBillingBatch();expect(getACUCTeam).not.toHaveBeenCalled();- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ teamId: "team-1", orgId: "org-1" }),- );- });-- it("refunds against an org resolved at refund time when the op recorded null", async () => {- queue = [makeOp({ org_id: null, autumnTrackInRequest: true })];- billTeam7.mockRejectedValueOnce(new Error("db failed"));-- await processBillingBatch();-- expect(getACUCTeam).toHaveBeenCalledWith("team-1");- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ teamId: "team-1", orgId: "org-legacy" }),- );- });-- it("resolves the refund-time org once per team across a batch", async () => {- queue = [- makeOp({ org_id: null, autumnTrackInRequest: true }),- makeOp({ org_id: null, autumnTrackInRequest: true, api_key_id: 456 }),- ];- billTeam7.mockRejectedValue(new Error("db failed"));-- await processBillingBatch();-- expect(getACUCTeam).toHaveBeenCalledTimes(1);- expect(refundCredits).toHaveBeenCalledTimes(2);- });-- it("skips the refund when the refund-time lookup confirms no org", async () => {- queue = [makeOp({ org_id: null, autumnTrackInRequest: true })];- getACUCTeam.mockResolvedValue({ team_id: "team-1", org_id: null });- billTeam7.mockRejectedValueOnce(new Error("db failed"));-- await processBillingBatch();-- expect(refundCredits).not.toHaveBeenCalled();- expect(logger.warn).toHaveBeenCalledWith(- "Skipping Autumn refund: no org for the team",- { team_id: "team-1", credits: 10 },- );- });-- it("retries at refund time past a lookup that failed earlier in the batch", async () => {- queue = [- // The legacy op's lookup throws and is memoized unresolved; the null-org- // op that follows must not inherit that failure.- makeOp({ org_id: undefined, autumnTrackInRequest: true }),- makeOp({ org_id: null, autumnTrackInRequest: true, api_key_id: 456 }),- ];- getACUCTeam- .mockRejectedValueOnce(new Error("acuc unavailable"))- .mockResolvedValue({ team_id: "team-1", org_id: "org-legacy" });- billTeam7.mockRejectedValue(new Error("db failed"));-- await processBillingBatch();-- expect(getACUCTeam).toHaveBeenCalledTimes(2);- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ teamId: "team-1", orgId: "org-legacy" }),- );- });-- it("spends the refund-time retry once per team across a batch", async () => {- queue = [- // The legacy op's lookup throws and is memoized unresolved; the two- // null-org groups that follow share the single retry it earns, so an- // outage costs one extra call rather than one per group.- makeOp({ org_id: undefined, autumnTrackInRequest: true }),- makeOp({ org_id: null, autumnTrackInRequest: true, api_key_id: 456 }),- makeOp({ org_id: null, autumnTrackInRequest: true, api_key_id: 789 }),- ];- getACUCTeam.mockRejectedValue(new Error("acuc unavailable"));- billTeam7.mockRejectedValue(new Error("db failed"));-- await processBillingBatch();-- expect(getACUCTeam).toHaveBeenCalledTimes(2);+ expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();});- it("skips the refund when the refund-time lookup throws", async () => {+ it("finalizes an operation that recorded a null org without a refund lookup", async () => {queue = [makeOp({ org_id: null, autumnTrackInRequest: true })];- getACUCTeam.mockRejectedValue(new Error("acuc unavailable"));- billTeam7.mockRejectedValueOnce(new Error("db failed"));await processBillingBatch();+ expect(getACUCTeam).not.toHaveBeenCalled();+ expect(billTeam7).not.toHaveBeenCalled();expect(refundCredits).not.toHaveBeenCalled();- expect(logger.warn).toHaveBeenCalledWith(- "Skipping Autumn refund: no org for the team",- { team_id: "team-1", credits: 10 },- );- // The op still billed and is not requeued: only the legacy branch defers.- expect(billTeam7).toHaveBeenCalled();expect(queue).toHaveLength(0);});});@@ -411,28 +390,19 @@ describe("hosted MCP charges", () => {...overrides,});- it("writes a hosted charge as one ordinary ledger row", async () => {- queue = [hosted()];-- await processBillingBatch();-- expect(billTeam7).toHaveBeenCalledExactlyOnceWith(- expect.objectContaining({ credits: 5, subscription_id: null }),- );- });-- it("refunds a hosted charge and a regular one each to the feature it was charged on", async () => {+ it("finalizes a hosted charge without writing credit_usage or refunding it", async () => {queue = [hosted(), makeOp({ autumnTrackInRequest: true })];- billTeam7.mockRejectedValue(new Error("db failed"));await processBillingBatch();- expect(refundCredits).toHaveBeenCalledTimes(2);- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ value: 5, featureId: "MCP_CREDITS" }),+ expect(billTeam7).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();+ expect(trackCredits).not.toHaveBeenCalled();+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 5 credits",);- expect(refundCredits).toHaveBeenCalledWith(- expect.objectContaining({ value: 10, featureId: "CREDITS" }),+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 10 credits",);});});@@ -460,7 +430,11 @@ describe("startBillingBatchProcessing", () => {queue = [makeOp()];await vi.advanceTimersByTimeAsync(15000);- expect(billTeam7).toHaveBeenCalledTimes(1);+ expect(billTeam7).not.toHaveBeenCalled();+ expect(refundCredits).not.toHaveBeenCalled();expect(queue).toHaveLength(0);+ expect(logger.info).toHaveBeenCalledWith(+ "β Successfully billed team team-1 for 10 credits",+ );});});diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.tsindex e50a6f35e..3fe810fe2 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 { billTeam7 } from "../../db/rpc";import { withAuth } from "../../lib/withAuth";import {autumnService,@@ -204,8 +203,12 @@ async function refundRequestTrackedCredits(}/**- * Dequeues pending billing operations from Redis, groups them by team, and- * commits each group to Supabase via the `bill_team_7` RPC.+ * 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+ * accesses.*/export async function processBillingBatch() {const redis = getRedisConnection();@@ -345,7 +348,8 @@ export async function processBillingBatch() {}try {- // Execute the actual billing+ // 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.",@@ -379,7 +383,7 @@ export async function processBillingBatch() {`β Successfully billed team ${group.team_id} for ${group.total_credits} credits`,);- // Ledger commit only β usage is tracked to Autumn at request time, not here.+ // Usage is tracked to Autumn at request time, not here.// The debit is committed: confirm the Exchange accesses it covered// once the batch lock is released.@@ -517,9 +521,9 @@ export async function queueBillingOperation(async function supaBillTeam(team_id: string,credits: number,- api_key_id: number | null,+ _api_key_id: number | null,__logger?: any,- is_extract: boolean = false,+ _is_extract: boolean = false,) {const _logger = (__logger ?? logger).child({module: "credit_billing",@@ -534,22 +538,12 @@ async function supaBillTeam(_logger.info(`Batch billing team ${team_id} for ${credits} credits`);- // Perform the actual database operation- let data: { api_key: string }[];- try {- data = await billTeam7({- team_id,- subscription_id: null,- credits,- api_key_id: api_key_id ?? null,- is_extract,- });- } catch (error) {- _logger.error("Failed to bill team.", { error });- return { success: false, error };- }-- return { success: true, data };+ // `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 exit