Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. πŸ”₯

refactor(billing): remove inline Autumn provisioning (#5484)

Rakshith Ramprakash34136f6509a0
diff --git a/.github/workflows/test-server.yml b/.github/workflows/test-server.yml
index fad9b219a..5d2edfb3e 100644
--- a/.github/workflows/test-server.yml
+++ b/.github/workflows/test-server.yml
@@ -245,7 +245,7 @@ jobs:
--reporter=default
working-directory: apps/api
- - name: Run Autumn entity provisioning regression tests
+ - name: Run Autumn billing service regression tests
if: matrix.engine == 'fetch' && matrix.proxy == 'no-proxy' && matrix.nuq == 'postgres'
run: pnpm exec vitest run src/services/autumn/__tests__/autumn.service.test.ts --reporter=default
working-directory: apps/api
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 a67d659d8..fa1a8d8cb 100644
--- a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
@@ -129,10 +129,6 @@ import {
BoundedSet,
featureIdForBillingEndpoint,
} from "../autumn.service";
-import {
- autumnCustomerGetOrCreateTotal,
- autumnEntityCreatedInlineTotal,
-} from "../metrics";
// ---------------------------------------------------------------------------
// Helpers
@@ -249,149 +245,6 @@ describe("BoundedSet eviction", () => {
});
});
-// ---------------------------------------------------------------------------
-// ensureTeamProvisioned
-// ---------------------------------------------------------------------------
-
-describe("ensureTeamProvisioned", () => {
- it("skips all HTTP calls for preview teams", async () => {
- const svc = makeService();
- await svc.ensureTeamProvisioned({ teamId: "preview_abc", orgId: "org-1" });
- expect(mockEntityGet).not.toHaveBeenCalled();
- expect(mockEntityCreate).not.toHaveBeenCalled();
- });
-
- it("skips getEntity when team is already in ensuredTeams cache", async () => {
- const svc = makeService();
- // First call β€” populates cache.
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- const callsAfterFirst = mockEntityGet.mock.calls.length;
-
- // Second call β€” should be a no-op.
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet.mock.calls.length).toBe(callsAfterFirst);
- });
-
- it("marks team as ensured without a second getEntity when entity already exists", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue(makeEntity(10));
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- // getEntity called once (existence check), createEntity never called.
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- expect(mockEntityCreate).not.toHaveBeenCalled();
-
- // Second call β€” team is cached, zero additional HTTP calls.
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- });
-
- it.each([
- { statusCode: 404 },
- { status: 404 },
- { response: { status: 404 } },
- ])("creates and caches a missing entity after %j", async error => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue(error);
- mockEntityCreate.mockResolvedValue({ id: "team-1" });
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- // Only one getEntity call (no confirmation get).
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- expect(mockEntityCreate).toHaveBeenCalledTimes(1);
- expect(mockEntityCreate).toHaveBeenCalledWith({
- customerId: "org-1",
- entityId: "team-1",
- featureId: "TEAM",
- });
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- expect(mockEntityCreate).toHaveBeenCalledTimes(1);
- });
-
- it.each([
- { statusCode: 429 },
- { status: 429 },
- { response: { status: 429 } },
- { statusCode: 500 },
- { status: 503 },
- new Error("ECONNRESET"),
- ])(
- "does not create or cache an entity after lookup failure %j",
- async error => {
- const svc = makeService();
- mockEntityGet.mockRejectedValueOnce(error);
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityCreate).not.toHaveBeenCalled();
-
- // The failed lookup must not mark the team as provisioned; retry can recover.
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(2);
- expect(mockEntityCreate).not.toHaveBeenCalled();
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(2);
- },
- );
-
- it("marks team as ensured on 409 conflict without a second getEntity", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
- // createEntity returns null to simulate 409 β€” the mock throws a 409 error
- // to exercise the conflict branch inside createEntity.
- mockEntityCreate.mockRejectedValue(
- Object.assign(new Error("conflict"), { status: 409 }),
- );
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- // Team should still be marked as ensured (entity exists, just raced).
- // Verify by checking that a second provisioning call makes zero HTTP requests.
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- });
-
- it("does NOT mark team as ensured when createEntity has a genuine error", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
- mockEntityCreate.mockRejectedValue(
- Object.assign(new Error("server error"), { status: 500 }),
- );
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- // Second call must re-attempt (team not cached).
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
- expect(mockEntityGet).toHaveBeenCalledTimes(2);
- });
-});
-
-// ---------------------------------------------------------------------------
-// ensureTrackingContext short-circuit (both caches warm)
-// ---------------------------------------------------------------------------
-
-describe("ensureTrackingContext warm-cache short-circuit", () => {
- it("makes zero provisioning HTTP calls when both caches are warm", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue(makeEntity(0));
-
- // Warm the caches.
- await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 5 });
- const callsAfterWarm = mockEntityGet.mock.calls.length;
-
- // Subsequent call β€” should not touch provisioning.
- await svc.trackCredits({ teamId: "team-1", orgId: "org-1", value: 5 });
-
- // No additional getEntity calls for provisioning.
- expect(mockEntityGet.mock.calls.length).toBe(callsAfterWarm);
- });
-});
-
// ---------------------------------------------------------------------------
// Org moves: the caller names the org on every billing call, so a move follows
// it.
@@ -1954,69 +1807,6 @@ describe("the org the caller supplies", () => {
});
});
-// ---------------------------------------------------------------------------
-// Inline-provisioning counters (Β§5 of the middleware credit-check spec): how
-// much the request path is still back-filling.
-// ---------------------------------------------------------------------------
-
-describe("inline provisioning counters", () => {
- const entityCreates = async (path: string) =>
- (await autumnEntityCreatedInlineTotal.get()).values.find(
- v => v.labels.path === path,
- )?.value ?? 0;
-
- const customerCalls = async () =>
- (await autumnCustomerGetOrCreateTotal.get()).values[0]?.value ?? 0;
-
- beforeEach(() => {
- autumnEntityCreatedInlineTotal.reset();
- autumnCustomerGetOrCreateTotal.reset();
- });
-
- it("counts an entity created inline, labelled with the method that did it", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- expect(await entityCreates("ensureTeamProvisioned")).toBe(1);
- expect(await customerCalls()).toBe(1);
- });
-
- it("does not count a 409: the entity was already there", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
- mockEntityCreate.mockRejectedValue(
- Object.assign(new Error("conflict"), { status: 409 }),
- );
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- expect(await entityCreates("ensureTeamProvisioned")).toBe(0);
- });
-
- // The counter measures load put on Autumn, so a call that got there and
- // failed counts the same as one that succeeded.
- it("counts a customer call that reached Autumn and failed", async () => {
- const svc = makeService();
- mockGetOrCreate.mockRejectedValueOnce(new Error("autumn unavailable"));
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- expect(await customerCalls()).toBe(1);
- });
-
- it("counts nothing when the entity is already present", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue(makeEntity(0));
-
- await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
-
- expect(await entityCreates("ensureTeamProvisioned")).toBe(0);
- expect(await customerCalls()).toBe(1);
- });
-});
-
// ---------------------------------------------------------------------------
// Who provisions (Β§2 of the middleware credit-check spec). The monolith no
// longer runs its own customer/entity prefix: firebill creates a missing
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
index 1f2d6b8b6..041787ee8 100644
--- a/apps/api/src/services/autumn/autumn.service.ts
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -12,18 +12,8 @@ import {
firebillCheck,
firebillConfigured,
} from "./firebill";
-import {
- autumnCustomerGetOrCreateTotal,
- autumnEntityCreatedInlineTotal,
-} from "./metrics";
import type {
- CreateEntityParams,
- CreateEntityResult,
- EnsureOrgProvisionedParams,
- EnsureTeamProvisionedParams,
FinalizeCreditsLockParams,
- GetEntityParams,
- GetOrCreateCustomerParams,
LockCreditsParams,
LockCreditsResult,
TrackCreditsParams,
@@ -132,19 +122,12 @@ export class BoundedSet<V> extends Set<V> {
}
/**
- * Wraps Autumn customer/entity provisioning and usage tracking for team credit billing.
+ * Team credit billing through firebill, plus the Autumn reads (limits, auto
+ * top-up) that sit beside it.
*/
export class AutumnService {
private gatewayTeams = new BoundedSet<string>(50_000);
private nonGatewayTeamsUntil = new BoundedMap<string, number>(50_000);
- private ensuredOrgs = new BoundedSet<string>(50_000);
- // Keyed by org AND team: an entity lives under one customer, so a team that
- // moves orgs has to be provisioned again under the new one.
- private ensuredTeams = new BoundedSet<string>(50_000);
-
- private ensuredTeamKey(orgId: string, teamId: string): string {
- return `${orgId}:${teamId}`;
- }
private isPreviewTeam(teamId: string): boolean {
return teamId === "preview" || teamId.startsWith("preview_");
@@ -212,176 +195,6 @@ export class AutumnService {
return typeof responseStatus === "number" ? responseStatus : undefined;
}
- private async getOrCreateCustomer({
- customerId,
- name,
- email,
- autoEnablePlanId = AUTUMN_DEFAULT_PLAN_ID,
- }: GetOrCreateCustomerParams): Promise<unknown | null> {
- if (!autumnClient) return null;
- if (!customerId) return null;
-
- try {
- // Counted before the await so a call that reached Autumn and failed is
- // still counted; the counter measures attempts, not successes.
- autumnCustomerGetOrCreateTotal.inc();
- const customer = await autumnClient.customers.getOrCreate({
- customerId,
- name: name ?? undefined,
- email: email ?? undefined,
- autoEnablePlanId,
- });
- logger.info("Autumn getOrCreateCustomer succeeded", { customerId });
- return customer;
- } catch (error) {
- logger.error(
- "Autumn getOrCreateCustomer failed β€” billing API may be unavailable",
- { customerId, error },
- );
- return null;
- }
- }
-
- private async getEntity({
- customerId,
- entityId,
- }: GetEntityParams): Promise<unknown | null> {
- if (!autumnClient) return null;
-
- try {
- return await autumnClient.entities.get({ customerId, entityId });
- } catch (error) {
- const status = this.getErrorStatus(error);
- if (status === 404) {
- return null;
- }
-
- logger.error("Autumn getEntity failed β€” billing API may be unavailable", {
- customerId,
- entityId,
- error,
- });
- // Only a 404 establishes that the entity is missing. Let provisioning's
- // catch handle other failures without attempting to create the entity.
- throw error;
- }
- }
-
- private async createEntity({
- customerId,
- entityId,
- featureId,
- name,
- path,
- }: CreateEntityParams): Promise<CreateEntityResult> {
- if (!autumnClient) return { ok: false, conflict: false };
-
- try {
- const entity = await autumnClient.entities.create({
- customerId,
- entityId,
- featureId,
- name: name ?? undefined,
- });
- autumnEntityCreatedInlineTotal.labels(path).inc();
- logger.info("Autumn createEntity succeeded", {
- customerId,
- entityId,
- featureId,
- });
- return { ok: true, entity };
- } catch (error) {
- const status = this.getErrorStatus(error);
- if (status === 409) {
- // Entity already exists β€” treat as success for provisioning purposes.
- return { ok: false, conflict: true };
- }
-
- logger.error(
- "Autumn createEntity failed β€” billing API may be unavailable",
- {
- customerId,
- entityId,
- featureId,
- error,
- },
- );
- return { ok: false, conflict: false };
- }
- }
-
- /**
- * Ensures the Autumn customer exists for an org, caching successful lookups in-process.
- */
- async ensureOrgProvisioned({
- orgId,
- name,
- email,
- }: EnsureOrgProvisionedParams): Promise<void> {
- if (this.ensuredOrgs.has(orgId)) return;
- const customer = await this.getOrCreateCustomer({
- customerId: orgId,
- name,
- email,
- });
- if (customer) {
- this.ensuredOrgs.add(orgId);
- }
- }
-
- /**
- * Ensures the Autumn entity exists for a team under its org customer.
- *
- * The `ensuredTeams` check runs before any Autumn call so that a team already
- * provisioned under its current org incurs no HTTP round-trips β€” not even
- * `ensureOrgProvisioned`.
- */
- async ensureTeamProvisioned({
- teamId,
- orgId,
- name,
- }: EnsureTeamProvisionedParams): Promise<void> {
- if (!autumnClient) return;
- if (this.isPreviewTeam(teamId)) return;
-
- try {
- // Fast path: team is already fully provisioned under this org.
- if (this.ensuredTeams.has(this.ensuredTeamKey(orgId, teamId))) {
- return;
- }
- await this.ensureOrgProvisioned({ orgId });
-
- const entity = await this.getEntity({
- customerId: orgId,
- entityId: teamId,
- });
-
- if (!entity) {
- const result = await this.createEntity({
- customerId: orgId,
- entityId: teamId,
- featureId: TEAM_FEATURE_ID,
- name,
- path: "ensureTeamProvisioned",
- });
- if (result.ok || ("conflict" in result && result.conflict)) {
- // Entity was just created, or already existed (409 race) β€” either way
- // it's present. No need for a second getEntity confirmation call.
- this.ensuredTeams.add(this.ensuredTeamKey(orgId, teamId));
- }
- // Genuine error: leave ensuredTeams empty so the next request retries.
- return;
- }
-
- this.ensuredTeams.add(this.ensuredTeamKey(orgId, teamId));
- } catch (error) {
- logger.error(
- "Autumn ensureTeamProvisioned failed β€” billing API may be unavailable",
- { teamId, error },
- );
- }
- }
-
/**
* Checks whether a team has enough balance to cover a request, through
* firebill. Returns null when gating is unavailable and callers should fall
diff --git a/apps/api/src/services/autumn/metrics.ts b/apps/api/src/services/autumn/metrics.ts
index 9e9cde7dc..aa1306a4a 100644
--- a/apps/api/src/services/autumn/metrics.ts
+++ b/apps/api/src/services/autumn/metrics.ts
@@ -100,27 +100,3 @@ export const firebillCheckTotal = new Counter({
help: "Outcomes of credit checks sent to firebill",
labelNames: ["outcome"] as const, // allowed | denied | unavailable
});
-
-/**
- * Entities the request path had to create for itself, by the method that did
- * it. Provisioning is meant to happen at team creation, so anything counted
- * here is the back-fill still being needed, and is what the per-request
- * provisioning prefix is buying. A 409 is not counted: the entity was already
- * there.
- */
-export const autumnEntityCreatedInlineTotal = new Counter({
- name: "firecrawl_autumn_entity_created_inline_total",
- help: "Autumn entities created inline on a billing path",
- labelNames: ["path"] as const, // the method that created it
-});
-
-/**
- * Calls to `customers.get_or_create` attempted against Autumn, counted before
- * the response so a failure counts too. Deliberately not "created": the
- * endpoint answers 200 either way and autumn-js parses only the customer body,
- * so nothing in the response tells a creation from a get.
- */
-export const autumnCustomerGetOrCreateTotal = new Counter({
- name: "firecrawl_autumn_customer_get_or_create_total",
- help: "Autumn customers.get_or_create calls attempted from a billing path",
-});
diff --git a/apps/api/src/services/autumn/types.ts b/apps/api/src/services/autumn/types.ts
index 0e75ed8dd..961a724c5 100644
--- a/apps/api/src/services/autumn/types.ts
+++ b/apps/api/src/services/autumn/types.ts
@@ -1,24 +1,3 @@
-export type GetOrCreateCustomerParams = {
- customerId: string;
- name?: string | null;
- email?: string | null;
- autoEnablePlanId?: string;
-};
-
-export type GetEntityParams = {
- customerId: string;
- entityId: string;
-};
-
-export type CreateEntityParams = {
- customerId: string;
- entityId: string;
- featureId: string;
- name?: string | null;
- /** Method doing the creation, for the inline-creation counter's `path`. */
- path: string;
-};
-
export type TrackParams = {
customerId: string;
entityId?: string;
@@ -46,19 +25,6 @@ export type TrackParams = {
durableRetry?: boolean;
};
-export type EnsureOrgProvisionedParams = {
- orgId: string;
- name?: string | null;
- email?: string | null;
-};
-
-export type EnsureTeamProvisionedParams = {
- teamId: string;
- /** See TrackCreditsParams.orgId. */
- orgId: string;
- name?: string | null;
-};
-
export type LockCreditsParams = {
teamId: string;
value: number;
@@ -137,8 +103,3 @@ export type TrackCreditsParams = {
*/
orgId: string;
};
-
-export type CreateEntityResult =
- | { ok: true; entity: unknown }
- | { ok: false; conflict: true }
- | { ok: false; conflict: false };
diff --git a/apps/api/src/services/monitoring/runner.reservation.test.ts b/apps/api/src/services/monitoring/runner.reservation.test.ts
index 80cde1568..cdee3e4f3 100644
--- a/apps/api/src/services/monitoring/runner.reservation.test.ts
+++ b/apps/api/src/services/monitoring/runner.reservation.test.ts
@@ -49,10 +49,6 @@ vi.mock("../autumn/firebill", () => ({
firebillTrack: firebill.track,
firebillCheck: firebill.check,
}));
-vi.mock("../autumn/metrics", () => ({
- autumnCustomerGetOrCreateTotal: { inc: vi.fn() },
- autumnEntityCreatedInlineTotal: { inc: vi.fn() },
-}));
vi.mock("../../db/connection", () => ({
dbRr: {
select: () => ({