Skip to content

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

fix(api): tell Gateway account users who manages their credits on a 402 (#5356)

Erik Engervallf748a79097e2
diff --git a/apps/api/src/__tests__/routes/check-credits-middleware.test.ts b/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
index fd8a7bfbd..08679b2a4 100644
--- a/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
+++ b/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
@@ -130,6 +130,36 @@ describe("checkCreditsMiddleware – Autumn overage handling", () => {
);
});
+ it("tells a Gateway account to contact its platform on a 402", async () => {
+ checkCreditsMock.mockResolvedValue({
+ allowed: false,
+ remaining: 0,
+ gatewayProvisioned: true,
+ });
+
+ const { res } = await runMiddleware(buildReq());
+
+ expect(res.status).toHaveBeenCalledWith(402);
+ expect(res.json).toHaveBeenCalledWith(
+ expect.objectContaining({
+ success: false,
+ code: "platform_managed_credits",
+ error: expect.stringContaining("contact that platform"),
+ }),
+ );
+ });
+
+ it("keeps the generic 402 for an account that is not a Gateway account", async () => {
+ checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
+
+ const { res } = await runMiddleware(buildReq());
+
+ expect(res.status).toHaveBeenCalledWith(402);
+ expect(res.json).toHaveBeenCalledWith(
+ expect.not.objectContaining({ code: expect.anything() }),
+ );
+ });
+
it("adjusts crawl limit down when Autumn denies but some credits remain", async () => {
checkCreditsMock
.mockResolvedValueOnce({ allowed: false, remaining: 5 })
diff --git a/apps/api/src/routes/shared.ts b/apps/api/src/routes/shared.ts
index b5b15922c..5b8efe929 100644
--- a/apps/api/src/routes/shared.ts
+++ b/apps/api/src/routes/shared.ts
@@ -267,6 +267,19 @@ export function checkCreditsMiddleware(
path: req.path,
},
);
+ // A Gateway account's credits come from the platform that created
+ // it, so its user has no plan of their own to upgrade.
+ if (
+ !res.headersSent &&
+ req.auth.team_id !== CREDIT_CHECK_EXEMPT_TEAM_ID &&
+ autumnResult.gatewayProvisioned === true
+ ) {
+ return res.status(402).json({
+ success: false,
+ code: "platform_managed_credits",
+ error: `Insufficient ${currencyName} to perform this request. This account's ${currencyName} are managed by the platform that created it, so contact that platform for more ${currencyName}.`,
+ });
+ }
if (
!res.headersSent &&
req.auth.team_id !== CREDIT_CHECK_EXEMPT_TEAM_ID
diff --git a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
index b40817650..518d7de2a 100644
--- a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
@@ -1272,7 +1272,11 @@ describe("firebill routing", () => {
// 500 is the funder's pool. Straight to Autumn this would have been the
// ghost's own balance, and a drained ghost would have been a 402.
- expect(result).toEqual({ allowed: true, remaining: 500 });
+ expect(result).toEqual({
+ allowed: true,
+ remaining: 500,
+ gatewayProvisioned: true,
+ });
expect(mockCheck).not.toHaveBeenCalled();
expect(String(mockFetch.mock.calls[0][0])).toBe(
"http://firebill.test/v1/check",
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
index f98ba0072..2260a60a1 100644
--- a/apps/api/src/services/autumn/autumn.service.ts
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -481,14 +481,17 @@ export class AutumnService {
private async resolveBillingRoute(
teamId: string,
orgId: string,
- ): Promise<{ customerId: string; routed: boolean }> {
- const routed = shouldRouteToFirebill(orgId, {
- gatewayProvisioned: await this.isGatewayProvisioned(teamId),
- });
+ ): Promise<{
+ customerId: string;
+ routed: boolean;
+ gatewayProvisioned: boolean;
+ }> {
+ const gatewayProvisioned = await this.isGatewayProvisioned(teamId);
+ const routed = shouldRouteToFirebill(orgId, { gatewayProvisioned });
if (!routed && !this.ensuredTeams.has(this.ensuredTeamKey(orgId, teamId))) {
await this.ensureTeamProvisioned({ teamId, orgId });
}
- return { customerId: orgId, routed };
+ return { customerId: orgId, routed, gatewayProvisioned };
}
/**
@@ -504,15 +507,15 @@ export class AutumnService {
}: TrackCreditsParams): Promise<{
allowed: boolean;
remaining: number;
+ /** Set only for a Gateway account, funded by the platform that created it. */
+ gatewayProvisioned?: boolean;
} | null> {
if (!autumnClient || this.isPreviewTeam(teamId)) {
return null;
}
try {
- const { customerId, routed } = await this.resolveBillingRoute(
- teamId,
- orgId,
- );
+ const { customerId, routed, gatewayProvisioned } =
+ await this.resolveBillingRoute(teamId, orgId);
// Mirrors track() and lockCredits(). Without this branch the gate reads
// the ghost's balance alone, and a gateway ghost is designed to spend
@@ -539,7 +542,11 @@ export class AutumnService {
properties,
});
if (result.status === "unavailable") return null;
- return { allowed: result.allowed, remaining: result.remaining };
+ return {
+ allowed: result.allowed,
+ remaining: result.remaining,
+ ...(gatewayProvisioned ? { gatewayProvisioned } : {}),
+ };
}
const { allowed, balance } = await autumnClient.check({
@@ -560,7 +567,11 @@ export class AutumnService {
allowed,
remaining,
});
- return { allowed, remaining };
+ return {
+ allowed,
+ remaining,
+ ...(gatewayProvisioned ? { gatewayProvisioned } : {}),
+ };
} catch (error) {
logger.error(
"Autumn checkCredits failed — billing API may be unavailable, falling back",