Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

refactor(api): stop reading zeroed ACUC credit fields (Autumn is SoT) (#3920)

firecrawl-spring[bot]797b118c9deb
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 5c9edad40..629c65c9b 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -36,7 +36,7 @@ function normalizedApiIsUuid(potentialUuid: string): boolean {
return isValidUuid(potentialUuid);
}
-export async function setCachedACUC(
+async function setCachedACUC(
api_key: string,
is_extract: boolean,
acuc:
@@ -325,7 +325,7 @@ export async function getACUC(
}
}
-export async function setCachedACUCTeam(
+async function setCachedACUCTeam(
team_id: string,
is_extract: boolean,
acuc:
diff --git a/apps/api/src/controllers/v1/crawl.ts b/apps/api/src/controllers/v1/crawl.ts
index d813648e9..545943ddb 100644
--- a/apps/api/src/controllers/v1/crawl.ts
+++ b/apps/api/src/controllers/v1/crawl.ts
@@ -142,7 +142,10 @@ export async function crawlController(
api_key_id: req.acuc?.api_key_id ?? null,
});
- let { remainingCredits } = req.account!;
+ // checkCreditsMiddleware (always runs before this controller) is the source
+ // of truth: Infinity when Autumn allows the request, the real remaining when
+ // it clamps a low-credit crawl. Default to no clamp if it's somehow unset.
+ let remainingCredits = req.account?.remainingCredits ?? Infinity;
const useDbAuthentication = config.USE_DB_AUTHENTICATION;
if (!useDbAuthentication) {
remainingCredits = Infinity;
diff --git a/apps/api/src/controllers/v2/crawl.ts b/apps/api/src/controllers/v2/crawl.ts
index 4dea44fc0..27f1b3c75 100644
--- a/apps/api/src/controllers/v2/crawl.ts
+++ b/apps/api/src/controllers/v2/crawl.ts
@@ -146,7 +146,10 @@ export async function crawlController(
api_key_id: req.acuc?.api_key_id ?? null,
});
- let { remainingCredits } = req.account!;
+ // checkCreditsMiddleware (always runs before this controller) is the source
+ // of truth: Infinity when Autumn allows the request, the real remaining when
+ // it clamps a low-credit crawl. Default to no clamp if it's somehow unset.
+ let remainingCredits = req.account?.remainingCredits ?? Infinity;
const useDbAuthentication = config.USE_DB_AUTHENTICATION;
if (!useDbAuthentication) {
remainingCredits = Infinity;
diff --git a/apps/api/src/routes/shared.ts b/apps/api/src/routes/shared.ts
index 3328ad6cc..7a461ebcc 100644
--- a/apps/api/src/routes/shared.ts
+++ b/apps/api/src/routes/shared.ts
@@ -260,13 +260,6 @@ export function authMiddleware(
req.auth = { team_id, org_id };
req.acuc = chunk ?? undefined;
- if (chunk) {
- req.account = {
- remainingCredits: chunk.price_should_be_graceful
- ? chunk.remaining_credits + chunk.price_credits
- : chunk.remaining_credits,
- };
- }
next();
})().catch(err => next(err));
};
diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
index 8d8e5e4e7..d79762a20 100644
--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts
+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
@@ -11,8 +11,6 @@ const {
trackCredits,
refundCredits,
billTeam6,
- setCachedACUC,
- setCachedACUCTeam,
} = vi.hoisted(() => {
const logger: any = {
info: vi.fn(),
@@ -27,8 +25,6 @@ const {
trackCredits: vi.fn<(args: any) => Promise<boolean>>(),
refundCredits: vi.fn<(args: any) => Promise<void>>(),
billTeam6: vi.fn<(params: any) => Promise<{ api_key: string }[]>>(),
- setCachedACUC: vi.fn(),
- setCachedACUCTeam: vi.fn(),
};
});
@@ -57,11 +53,6 @@ vi.mock("../../../db/rpc", () => ({
billTeam6,
}));
-vi.mock("../../../controllers/auth", () => ({
- setCachedACUC,
- setCachedACUCTeam,
-}));
-
let queue: string[] = [];
const billedTeams = new Set<string>();
const locks = new Map<string, string>();
diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts
index f236b2ee3..6e98f8c1b 100644
--- a/apps/api/src/services/billing/batch_billing.ts
+++ b/apps/api/src/services/billing/batch_billing.ts
@@ -3,7 +3,6 @@ import { getRedisConnection } from "../queue-service";
import { billTeam6 } from "../../db/rpc";
import * as Sentry from "@sentry/node";
import { withAuth } from "../../lib/withAuth";
-import { setCachedACUC, setCachedACUCTeam } from "../../controllers/auth";
import {
autumnService,
featureIdForBillingEndpoint,
@@ -319,38 +318,6 @@ export async function queueBillingOperation(
);
await processBillingBatch();
}
- // TODO is there a better way to do this?
-
- // Update cached credits used immediately to provide accurate feedback to users
- // This is optimistic - actual billing happens in batch
- // Should we add this?
- // I guess batch is fast enough that it's fine
-
- // if (config.USE_DB_AUTHENTICATION) {
- // (async () => {
- // // Get API keys for this team to update in cache
- // const { data } = await supabase_service
- // .from("api_keys")
- // .select("key")
- // .eq("team_id", team_id);
-
- // for (const apiKey of (data ?? []).map(x => x.key)) {
- // await setCachedACUC(apiKey, (acuc) =>
- // acuc
- // ? {
- // ...acuc,
- // credits_used: acuc.credits_used + credits,
- // adjusted_credits_used: acuc.adjusted_credits_used + credits,
- // remaining_credits: acuc.remaining_credits - credits,
- // }
- // : null,
- // );
- // }
- // })().catch(error => {
- // logger.error("Failed to update cached credits", { error, team_id });
- // });
- // }
-
return { success: true };
} catch (error) {
logger.error("Error queueing billing operation", { error, team_id });
@@ -413,34 +380,6 @@ async function supaBillTeam(
_logger.warn("Failed to add team to billed_teams set", { err, team_id });
});
- // Update cached ACUC to reflect the new credit usage
- (async () => {
- for (const apiKey of (data ?? []).map(x => x.api_key)) {
- await setCachedACUC(apiKey, is_extract, acuc =>
- acuc
- ? {
- ...acuc,
- credits_used: acuc.credits_used + credits,
- adjusted_credits_used: acuc.adjusted_credits_used + credits,
- remaining_credits: acuc.remaining_credits - credits,
- }
- : null,
- );
- await setCachedACUCTeam(team_id, is_extract, acuc =>
- acuc
- ? {
- ...acuc,
- credits_used: acuc.credits_used + credits,
- adjusted_credits_used: acuc.adjusted_credits_used + credits,
- remaining_credits: acuc.remaining_credits - credits,
- }
- : null,
- );
- }
- })().catch(error => {
- _logger.error("Failed to update cached credits", { error, team_id });
- });
-
return { success: true, data };
}
diff --git a/apps/api/src/services/notification/email_notification.ts b/apps/api/src/services/notification/email_notification.ts
index dbda0a2ec..9912b2fb5 100644
--- a/apps/api/src/services/notification/email_notification.ts
+++ b/apps/api/src/services/notification/email_notification.ts
@@ -334,7 +334,7 @@ async function sendNotificationInternal(
if (config.SLACK_ADMIN_WEBHOOK_URL && emails.length > 0) {
sendSlackWebhook(
- `${getNotificationString(notificationType)}: Team ${team_id}, with email ${emails[0].email}. Number of credits used: ${chunk.adjusted_credits_used} | Number of credits in the plan: ${chunk.price_credits}`,
+ `${getNotificationString(notificationType)}: Team ${team_id}, with email ${emails[0].email}. Number of credits in the plan: ${chunk.price_credits}`,
false,
config.SLACK_ADMIN_WEBHOOK_URL,
).catch(error => {