Skip to content

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

Initialise autumn (#3055)

Charlie Lamb9fd8dbe22dae
diff --git a/apps/api/.env.local b/apps/api/.env.local
index 9fa41498b..e7186e569 100644
--- a/apps/api/.env.local
+++ b/apps/api/.env.local
@@ -12,4 +12,4 @@ ANTHROPIC_API_KEY=
BULL_AUTH_KEY=
LOGTAIL_KEY=
PLAYWRIGHT_MICROSERVICE_URL=
-SEARCHAPI_API_KEY=
+SEARCHAPI_API_KEY=
\ No newline at end of file
diff --git a/apps/api/package.json b/apps/api/package.json
index bd162867e..7d5c4422a 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -98,6 +98,7 @@
"ajv-formats": "^3.0.1",
"amqplib": "^0.10.9",
"async-mutex": "^0.5.0",
+ "autumn-js": "1.0.0-beta.6",
"axios": "^1.13.5",
"body-parser": "^1.20.3",
"bullmq": "^5.56.7",
diff --git a/apps/api/pnpm-lock.yaml b/apps/api/pnpm-lock.yaml
index 3e55f106c..29868ef90 100644
--- a/apps/api/pnpm-lock.yaml
+++ b/apps/api/pnpm-lock.yaml
@@ -99,6 +99,9 @@ importers:
async-mutex:
specifier: ^0.5.0
version: 0.5.0
+ autumn-js:
+ specifier: 1.0.0-beta.6
+ version: 1.0.0-beta.6([email protected])
axios:
specifier: ^1.13.5
version: 1.13.5
@@ -3017,6 +3020,26 @@ packages:
resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
+ resolution: {integrity: sha512-OUdPvoPQVhcg/pxYuUExgiE/P2OPSvwyiTt6r7q5cTz5QEW8WSaBprc8IsEaT7ChB3usYfdh15O9cnEdgXeUvw==}
+ peerDependencies:
+ better-auth: ^1.3.17
+ better-call: ^1.0.12
+ hono: ^4.0.0
+ next: ^14.0.0 || ^15.0.0
+ react: ^18.0.0 || ^19.0.0
+ peerDependenciesMeta:
+ better-auth:
+ optional: true
+ better-call:
+ optional: true
+ hono:
+ optional: true
+ next:
+ optional: true
+ react:
+ optional: true
+
resolution: {integrity: sha512-aR99oXhpEDGo0UuAlYcn2iGRds30k366Zfa05XWScR9QaQD4JYiP3/1Qt1u7YlefUOK+cn0CcwoL1oefavQUlQ==}
peerDependencies:
@@ -3380,6 +3403,10 @@ packages:
resolution: {integrity: sha512-8vDa8Qxvr/+d94hSh5P3IJwI5t8/c0KsMp+g8bNw9cY2icONa5aPfvKeieW1WlG0WQYwwhJ7mjui2xtiePQSXw==}
+ resolution: {integrity: sha512-+8VxcR21HhTy8nOt6jf20w0c9CADrw1O8d+VZ/YzzCt4bJ3uBjw+D1q2osAB8RnpwwaeYBxy0HyKQxD5JBMuuQ==}
+ engines: {node: '>=14.16'}
+
resolution: {integrity: sha512-HGFtf8yhuhGhqO07SV79tRp+br4MnbdjeVxotpn1QBl30pcLLCQjX5b2295ll0fv8RKDKsmWYrl05usHM9CewQ==}
peerDependencies:
@@ -3692,6 +3719,10 @@ packages:
resolution: {integrity: sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-qWeTREPoT7I0bifpPUXtxkZJ1XJzxWtfoWWkdVGqa+eCr3SHW/Ocp89o8vLvbUuQnadybJpjOKu4V+RwO6sGng==}
+ engines: {node: '>=14.16'}
+
resolution: {integrity: sha512-6BN9trH7bp3qvnrRyzsBz+g3lZxTNZTbVO2EV1CS0WIcDbawYVdYvGflME/9QP0h0pYlCDBCTjYa9nZzMDpyxQ==}
engines: {node: '>= 0.8'}
@@ -4898,6 +4929,10 @@ packages:
resolution: {integrity: sha512-mAZTtNCeetKMH+pSjrb76NAM8V9a05I9aBZOHztWy/UqcJdQYNsf59vrRKWnojAT9Y+GbIvoTBC++CPHqpDBhQ==}
engines: {node: '>=0.6'}
+ resolution: {integrity: sha512-5fBfMOcDi5SA9qj5jZhWAcTtDfKF5WFdd2uD9nVNlbxVv1baq65aALy6qofpNEGELHvisjjasxQp7BlM9gvMzw==}
+ engines: {node: '>=18'}
+
resolution: {integrity: sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==}
@@ -5026,6 +5061,9 @@ packages:
resolution: {integrity: sha512-s+pyvQeIKIZ0dx5iJiQk1tPLJAWln39+MI5jtM8wnyws+G5azk+dMnMX0qfbqNetKKNgcWWOdi0sfm+FbQbgdQ==}
engines: {node: '>=10.0.0'}
+ resolution: {integrity: sha512-1HSG1ENTj7Kkm5muMnXuzzfdDOf7CFnbSYFA+H3Fp/rB9lOCxCPgy1jlZxTKyFoC5jJay8Mmc+VbPLYRjzYLrA==}
+
resolution: {integrity: sha512-I+kNjW0udB4Fetr3vvtRuYZJS0PcSPyyvBcH5sDdoV8DFs5E4W2pTr7aiMlKfPxANTClP9RlqCPolj9dd5MsEA==}
@@ -5161,6 +5199,10 @@ packages:
resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==}
engines: {node: '>=0.10.0'}
+ resolution: {integrity: sha512-qxQJTx2ryR0Dw0ITYyekNQWpz6f8dGd7vffGNflQQ3Iqj9NJ6qiZ7ELpZsJ/QBhIVAiDfXdag3+Gp8RvWa62AA==}
+ engines: {node: '>=12'}
+
resolution: {integrity: sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==}
engines: {node: '>= 10.x'}
@@ -8536,6 +8578,14 @@ snapshots:
+ dependencies:
+ query-string: 9.3.1
+ rou3: 0.6.3
+ zod: 4.1.12
+ optionalDependencies:
+ react: 18.3.1
+
dependencies:
axios: 1.13.5
@@ -8921,6 +8971,8 @@ snapshots:
+
@@ -9291,6 +9343,8 @@ snapshots:
dependencies:
to-regex-range: 5.0.1
+
dependencies:
debug: 2.6.9
@@ -10775,6 +10829,12 @@ snapshots:
dependencies:
side-channel: 1.1.0
+ dependencies:
+ decode-uri-component: 0.4.1
+ filter-obj: 5.1.0
+ split-on-first: 3.0.0
+
@@ -10908,6 +10968,8 @@ snapshots:
+
dependencies:
'@swc/helpers': 0.5.17
@@ -11065,6 +11127,8 @@ snapshots:
+
diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts
index e82b16789..4ee2f1214 100644
--- a/apps/api/src/config.ts
+++ b/apps/api/src/config.ts
@@ -47,6 +47,9 @@ const configSchema = z.object({
OPENROUTER_API_KEY: z.string().optional(),
LLAMAPARSE_API_KEY: z.string().optional(),
STRIPE_SECRET_KEY: z.string().optional(),
+ AUTUMN_SECRET_KEY: z.string().optional(),
+ AUTUMN_EXPERIMENT: z.string().optional(),
+ AUTUMN_EXPERIMENT_PERCENT: z.coerce.number().default(100),
RESEND_API_KEY: z.string().optional(),
PREVIEW_TOKEN: z.string().optional(),
SEARCH_PREVIEW_TOKEN: z.string().optional(),
diff --git a/apps/api/src/controllers/v0/admin/create-user.ts b/apps/api/src/controllers/v0/admin/create-user.ts
index 07d0b89da..10d21b764 100644
--- a/apps/api/src/controllers/v0/admin/create-user.ts
+++ b/apps/api/src/controllers/v0/admin/create-user.ts
@@ -4,6 +4,10 @@ import { supabase_service } from "../../../services/supabase";
import crypto from "crypto";
import { z } from "zod";
import { apiKeyToFcApiKey } from "../../../lib/parseApi";
+import {
+ autumnService,
+ isAutumnEnabled,
+} from "../../../services/autumn/autumn.service";
async function addCoupon(teamId: string, integration: any) {
if (!integration.coupon_credits) {
@@ -215,6 +219,13 @@ export async function integCreateUserController(req: Request, res: Response) {
});
}
+ if (isAutumnEnabled()) {
+ await autumnService.ensureTeamProvisioned({
+ teamId,
+ orgId: existingTeam[0].org_id,
+ });
+ }
+
alreadyExisted = true;
logger.info("Found existing team from existing user", {
@@ -229,7 +240,9 @@ export async function integCreateUserController(req: Request, res: Response) {
.single();
if (newOrgError) {
logger.error("Failed to create organization", { error: newOrgError });
- return res.status(500).json({ error: "Failed to create organization" });
+ return res
+ .status(500)
+ .json({ error: "Failed to create organization" });
}
// create a new team with this referrer, linked to the org
@@ -254,7 +267,11 @@ export async function integCreateUserController(req: Request, res: Response) {
.update({ name: teamId })
.eq("id", newOrg.id);
if (orgNameError) {
- logger.warn("Failed to update org name to team ID", { error: orgNameError, orgId: newOrg.id, teamId });
+ logger.warn("Failed to update org name to team ID", {
+ error: orgNameError,
+ orgId: newOrg.id,
+ teamId,
+ });
}
// Link team to org in join table
@@ -265,8 +282,19 @@ export async function integCreateUserController(req: Request, res: Response) {
team_id: teamId,
});
if (orgLinkError) {
- logger.error("Failed to link team to organization", { error: orgLinkError });
- return res.status(500).json({ error: "Failed to link team to organization" });
+ logger.error("Failed to link team to organization", {
+ error: orgLinkError,
+ });
+ return res
+ .status(500)
+ .json({ error: "Failed to link team to organization" });
+ }
+
+ if (isAutumnEnabled()) {
+ await autumnService.ensureTeamProvisioned({
+ teamId,
+ orgId: newOrg.id,
+ });
}
const { error: newUserTeamError } = await supabase_service
@@ -351,6 +379,21 @@ export async function integCreateUserController(req: Request, res: Response) {
apiKey = apiKeyFc.key;
+ if (isAutumnEnabled()) {
+ // Look up org_id for the trigger-created team so ensureTeamProvisioned
+ // can evaluate the stable percent gate without a redundant query.
+ const { data: triggerTeam } = await supabase_service
+ .from("teams")
+ .select("org_id")
+ .eq("id", teamId)
+ .single();
+
+ await autumnService.ensureTeamProvisioned({
+ teamId,
+ orgId: triggerTeam?.org_id ?? undefined,
+ });
+ }
+
await addCoupon(teamId, integration);
logger.info("Created new user from scratch", {
diff --git a/apps/api/src/routes/shared.ts b/apps/api/src/routes/shared.ts
index 7d982a811..884c1b951 100644
--- a/apps/api/src/routes/shared.ts
+++ b/apps/api/src/routes/shared.ts
@@ -119,6 +119,7 @@ export function checkCreditsMiddleware(
req.auth.team_id,
minimum ?? 1,
);
+ //todo(autumn) add .check call here
if (chunk) {
req.acuc = chunk;
}
diff --git a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
new file mode 100644
index 000000000..1e104fd04
--- /dev/null
+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
@@ -0,0 +1,459 @@
+/**
+ * Unit tests for AutumnService.
+ *
+ * All external I/O is mocked:
+ * - autumnClient β†’ jest.fn() stubs on customers / entities / track
+ * - supabase_rr_service β†’ stubbed Supabase query builder
+ */
+
+import { jest } from "@jest/globals";
+
+// ---------------------------------------------------------------------------
+// Mocks β€” must be declared before the import under test so Jest hoists them.
+// ---------------------------------------------------------------------------
+
+const mockTrack = jest
+ .fn<(args: any) => Promise<void>>()
+ .mockResolvedValue(undefined);
+const mockGetOrCreate = jest
+ .fn<(args: any) => Promise<unknown>>()
+ .mockResolvedValue({ id: "org-1" });
+const mockEntityGet = jest.fn<(args: any) => Promise<unknown>>();
+const mockEntityCreate = jest.fn<(args: any) => Promise<unknown>>();
+
+const mockAutumnClient = {
+ customers: { getOrCreate: mockGetOrCreate },
+ entities: { get: mockEntityGet, create: mockEntityCreate },
+ track: mockTrack,
+};
+
+// Mutable reference so individual tests can set it to null to simulate missing key.
+let autumnClientRef: typeof mockAutumnClient | null = mockAutumnClient;
+
+jest.mock("../client", () => ({
+ get autumnClient() {
+ return autumnClientRef;
+ },
+}));
+
+// Minimal Supabase query-builder stub: .from().select().eq().single() β†’ resolves data/error.
+const makeSupabaseStub = (data: unknown, error: unknown = null) => ({
+ from: () => ({
+ select: () => ({
+ eq: () => ({
+ single: () => Promise.resolve({ data, error }),
+ gte: () => Promise.resolve({ data: [], error: null }),
+ }),
+ gte: () => Promise.resolve({ data: [], error: null }),
+ }),
+ }),
+});
+
+let supabaseStubData: { data: unknown; error: unknown } = {
+ data: { org_id: "org-1" },
+ error: null,
+};
+
+jest.mock("../../supabase", () => ({
+ get supabase_rr_service() {
+ return makeSupabaseStub(supabaseStubData.data, supabaseStubData.error);
+ },
+}));
+
+jest.mock("../../../config", () => ({
+ config: {
+ AUTUMN_EXPERIMENT: "true",
+ AUTUMN_EXPERIMENT_PERCENT: 100,
+ },
+}));
+
+// Import AFTER mocks are wired up.
+import {
+ AutumnService,
+ BoundedMap,
+ BoundedSet,
+ isAutumnEnabled,
+ orgBucket,
+} from "../autumn.service";
+import { config } from "../../../config";
+
+// ---------------------------------------------------------------------------
+// Helpers
+// ---------------------------------------------------------------------------
+
+function makeService() {
+ return new AutumnService();
+}
+
+function makeEntity(usage: number) {
+ return { balances: { CREDITS: { usage } } };
+}
+
+// ---------------------------------------------------------------------------
+// Test setup
+// ---------------------------------------------------------------------------
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ autumnClientRef = mockAutumnClient;
+ supabaseStubData = { data: { org_id: "org-1" }, error: null };
+ mockEntityGet.mockResolvedValue(makeEntity(0));
+ mockEntityCreate.mockResolvedValue({ id: "team-1" });
+});
+
+// ---------------------------------------------------------------------------
+// BoundedMap / BoundedSet (via observable side-effects on the caches)
+// ---------------------------------------------------------------------------
+
+describe("BoundedMap eviction", () => {
+ it("never exceeds its cap", () => {
+ const m = new BoundedMap<number, number>(3);
+ m.set(1, 1);
+ m.set(2, 2);
+ m.set(3, 3);
+ expect(m.size).toBe(3);
+ m.set(4, 4); // evicts key 1
+ expect(m.size).toBe(3);
+ expect(m.has(1)).toBe(false);
+ expect(m.has(4)).toBe(true);
+ });
+
+ it("does not evict on update of existing key", () => {
+ const m = new BoundedMap<number, number>(2);
+ m.set(1, 1);
+ m.set(2, 2);
+ m.set(1, 99); // update, not a new entry
+ expect(m.size).toBe(2);
+ expect(m.get(1)).toBe(99);
+ expect(m.has(2)).toBe(true);
+ });
+});
+
+describe("BoundedSet eviction", () => {
+ it("never exceeds its cap", () => {
+ const s = new BoundedSet<number>(3);
+ s.add(1);
+ s.add(2);
+ s.add(3);
+ expect(s.size).toBe(3);
+ s.add(4); // evicts value 1
+ expect(s.size).toBe(3);
+ expect(s.has(1)).toBe(false);
+ expect(s.has(4)).toBe(true);
+ });
+
+ it("does not evict on re-add of existing value", () => {
+ const s = new BoundedSet<number>(2);
+ s.add(1);
+ s.add(2);
+ s.add(1); // already present, no eviction
+ expect(s.size).toBe(2);
+ expect(s.has(2)).toBe(true);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// 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("marks team as ensured without a second getEntity when createEntity succeeds", async () => {
+ const svc = makeService();
+ // First getEntity returns null β†’ entity doesn't exist yet.
+ mockEntityGet.mockResolvedValue(null);
+ 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);
+ });
+
+ it("marks team as ensured on 409 conflict without a second getEntity", async () => {
+ const svc = makeService();
+ mockEntityGet.mockResolvedValue(null);
+ // 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.mockResolvedValue(null);
+ 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.reserveCredits({ teamId: "team-1", value: 5 });
+ const callsAfterWarm = mockEntityGet.mock.calls.length;
+
+ // Subsequent call β€” should not touch provisioning.
+ await svc.reserveCredits({ teamId: "team-1", value: 5 });
+
+ // No additional getEntity calls for provisioning.
+ expect(mockEntityGet.mock.calls.length).toBe(callsAfterWarm);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// reserveCredits
+// ---------------------------------------------------------------------------
+
+describe("reserveCredits", () => {
+ it("returns false when autumnClient is null", async () => {
+ autumnClientRef = null;
+ const svc = makeService();
+ const result = await svc.reserveCredits({ teamId: "team-1", value: 10 });
+ expect(result).toBe(false);
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+
+ it("returns false for preview teams", async () => {
+ const svc = makeService();
+ const result = await svc.reserveCredits({
+ teamId: "preview_abc",
+ value: 10,
+ });
+ expect(result).toBe(false);
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+
+ it("calls track with correct feature and value on happy path", async () => {
+ const svc = makeService();
+
+ const result = await svc.reserveCredits({
+ teamId: "team-1",
+ value: 42,
+ properties: { source: "test" },
+ });
+
+ expect(result).toBe(true);
+ // track should have been called for the actual usage event (at minimum).
+ const trackCalls = mockTrack.mock.calls;
+ const usageCall = trackCalls.find(
+ (c: any[]) => c[0].featureId === "CREDITS" && c[0].value === 42,
+ );
+ expect(usageCall).toBeDefined();
+ });
+});
+
+// ---------------------------------------------------------------------------
+// refundCredits
+// ---------------------------------------------------------------------------
+
+describe("refundCredits", () => {
+ it("calls track with the negated value", async () => {
+ const svc = makeService();
+ await svc.refundCredits({ teamId: "team-1", value: 30 });
+
+ const refundCall = mockTrack.mock.calls.find(
+ (c: any[]) => c[0].value === -30,
+ );
+ expect(refundCall).toBeDefined();
+ expect((refundCall as any[])[0].properties?.source).toBe("autumn_refund");
+ });
+
+ it("is a no-op when autumnClient is null", async () => {
+ autumnClientRef = null;
+ const svc = makeService();
+ await svc.refundCredits({ teamId: "team-1", value: 30 });
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+
+ it("is a no-op for preview teams", async () => {
+ const svc = makeService();
+ await svc.refundCredits({ teamId: "preview_abc", value: 30 });
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+});
+
+// ---------------------------------------------------------------------------
+// isAutumnEnabled / experiment gating
+// ---------------------------------------------------------------------------
+
+describe("orgBucket", () => {
+ it("is deterministic β€” same orgId always returns the same bucket", () => {
+ const id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
+ expect(orgBucket(id)).toBe(orgBucket(id));
+ });
+
+ it("returns a value in [0, 100)", () => {
+ const ids = [
+ "00000000-0000-0000-0000-000000000000",
+ "ffffffff-ffff-ffff-ffff-ffffffffffff",
+ "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
+ ];
+ for (const id of ids) {
+ const b = orgBucket(id);
+ expect(b).toBeGreaterThanOrEqual(0);
+ expect(b).toBeLessThan(100);
+ }
+ });
+
+ it("strips dashes and uses first 8 hex chars", () => {
+ // "a1b2c3d4" β†’ parseInt("a1b2c3d4", 16) = 2712847316 β†’ 2712847316 % 100 = 16
+ expect(orgBucket("a1b2c3d4-0000-0000-0000-000000000000")).toBe(16);
+ });
+});
+
+describe("isAutumnEnabled", () => {
+ afterEach(() => {
+ // Restore defaults for other tests.
+ (config as any).AUTUMN_EXPERIMENT = "true";
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 100;
+ });
+
+ it("returns true when experiment is enabled and percent is 100", () => {
+ expect(isAutumnEnabled()).toBe(true);
+ });
+
+ it("returns true without orgId even when percent < 100 (fast bail-out only)", () => {
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 0;
+ // Without orgId the percent gate is skipped β€” only the on/off flag matters.
+ expect(isAutumnEnabled()).toBe(true);
+ });
+
+ it("returns false when AUTUMN_EXPERIMENT is not 'true'", () => {
+ (config as any).AUTUMN_EXPERIMENT = undefined;
+ expect(isAutumnEnabled()).toBe(false);
+ });
+
+ it("returns false for an orgId whose bucket >= percent", () => {
+ // orgBucket("a1b2c3d4-...") = 16, so percent=10 should exclude it.
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 10;
+ expect(isAutumnEnabled("a1b2c3d4-0000-0000-0000-000000000000")).toBe(false);
+ });
+
+ it("returns true for an orgId whose bucket < percent", () => {
+ // orgBucket("a1b2c3d4-...") = 16, so percent=50 should include it.
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 50;
+ expect(isAutumnEnabled("a1b2c3d4-0000-0000-0000-000000000000")).toBe(true);
+ });
+});
+
+describe("experiment gate on reserveCredits", () => {
+ afterEach(() => {
+ (config as any).AUTUMN_EXPERIMENT = "true";
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 100;
+ });
+
+ it("reserveCredits returns false when experiment is disabled", async () => {
+ (config as any).AUTUMN_EXPERIMENT = undefined;
+ const svc = makeService();
+ const result = await svc.reserveCredits({ teamId: "team-1", value: 10 });
+ expect(result).toBe(false);
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+
+ it("reserveCredits returns false when org is outside the percent bucket", async () => {
+ // Supabase returns org whose bucket (16) is >= percent (10).
+ supabaseStubData = {
+ data: { org_id: "a1b2c3d4-0000-0000-0000-000000000000" },
+ error: null,
+ };
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 10;
+ const svc = makeService();
+ const result = await svc.reserveCredits({ teamId: "team-1", value: 10 });
+ expect(result).toBe(false);
+ expect(mockTrack).not.toHaveBeenCalled();
+ });
+
+ it("reserveCredits succeeds when org is inside the percent bucket", async () => {
+ // Supabase returns org whose bucket (16) is < percent (50).
+ supabaseStubData = {
+ data: { org_id: "a1b2c3d4-0000-0000-0000-000000000000" },
+ error: null,
+ };
+ (config as any).AUTUMN_EXPERIMENT_PERCENT = 50;
+ const svc = makeService();
+ const result = await svc.reserveCredits({ teamId: "team-1", value: 10 });
+ expect(result).toBe(true);
+ expect(mockTrack).toHaveBeenCalled();
+ });
+
+ it("refundCredits still works when experiment is disabled (guard is autumnReserved)", async () => {
+ (config as any).AUTUMN_EXPERIMENT = undefined;
+ const svc = makeService();
+ // Warm the caches so refund can resolve the tracking context.
+ (config as any).AUTUMN_EXPERIMENT = "true";
+ await svc.reserveCredits({ teamId: "team-1", value: 10 });
+ jest.clearAllMocks();
+
+ // Disable experiment β€” refund must still succeed to avoid orphaned credits.
+ (config as any).AUTUMN_EXPERIMENT = undefined;
+ mockTrack.mockResolvedValue(undefined);
+ await svc.refundCredits({ teamId: "team-1", value: 10 });
+ expect(mockTrack).toHaveBeenCalled();
+ });
+
+ it("ensureTeamProvisioned still works when experiment is disabled (handled by firecrawl-web)", async () => {
+ (config as any).AUTUMN_EXPERIMENT = undefined;
+ const svc = makeService();
+ await svc.ensureTeamProvisioned({ teamId: "team-1", orgId: "org-1" });
+ // Provisioning should proceed β€” firecrawl-web edge functions do this
+ // regardless, so gating API-side provisioning is unnecessary.
+ expect(mockGetOrCreate).toHaveBeenCalled();
+ });
+});
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
new file mode 100644
index 000000000..f35934c43
--- /dev/null
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -0,0 +1,391 @@
+import { config } from "../../config";
+import { logger } from "../../lib/logger";
+import { supabase_rr_service } from "../supabase";
+import { autumnClient } from "./client";
+import type {
+ CreateEntityParams,
+ CreateEntityResult,
+ EnsureOrgProvisionedParams,
+ EnsureTeamProvisionedParams,
+ GetEntityParams,
+ GetOrCreateCustomerParams,
+ TrackCreditsParams,
+ TrackParams,
+} from "./types";
+
+const CREDITS_FEATURE_ID = "CREDITS";
+
+/**
+ * Deterministic bucket for an org UUID.
+ *
+ * Takes the first 8 hex digits of the id (after stripping dashes) and maps
+ * them to an integer in [0, 100). The same orgId always lands in the same
+ * bucket so the experiment decision is stable across requests.
+ */
+export function orgBucket(orgId: string): number {
+ const hex = orgId.replace(/-/g, "").slice(0, 8);
+ return parseInt(hex, 16) % 100;
+}
+
+/**
+ * Returns true when the Autumn experiment is active.
+ *
+ * Without an orgId the check is a simple on/off flag β€” useful as a fast
+ * bail-out before the orgId is known. When an orgId is supplied the
+ * stable percent gate is also evaluated so the same org always gets the
+ * same answer.
+ *
+ * Only checked at the top-level billing entry point (`reserveCredits`).
+ * NOT checked by `refundCredits` (already guarded by `autumnReserved`) or
+ * `ensureTeamProvisioned` (handled by firecrawl-web edge functions
+ * independently).
+ */
+export function isAutumnEnabled(orgId?: string): boolean {
+ if (config.AUTUMN_EXPERIMENT !== "true") return false;
+ if (!orgId || config.AUTUMN_EXPERIMENT_PERCENT >= 100) return true;
+ return orgBucket(orgId) < config.AUTUMN_EXPERIMENT_PERCENT;
+}
+
+const AUTUMN_DEFAULT_PLAN_ID = "free";
+/**
+ * Size-bounded Map with FIFO eviction. When the map is at capacity the oldest
+ * inserted entry is removed before inserting the new one, keeping memory usage
+ * at most O(max) regardless of how many unique keys are seen over time.
+ */
+export class BoundedMap<K, V> extends Map<K, V> {
+ constructor(private readonly max: number) {
+ super();
+ }
+
+ set(key: K, value: V): this {
+ if (!this.has(key) && this.size >= this.max) {
+ this.delete(this.keys().next().value as K);
+ }
+ return super.set(key, value);
+ }
+}
+
+/**
+ * Size-bounded Set with FIFO eviction. Mirrors BoundedMap for set semantics.
+ */
+export class BoundedSet<V> extends Set<V> {
+ constructor(private readonly max: number) {
+ super();
+ }
+
+ add(value: V): this {
+ if (!this.has(value) && this.size >= this.max) {
+ this.delete(this.values().next().value as V);
+ }
+ return super.add(value);
+ }
+}
+
+/**
+ * Wraps Autumn customer/entity provisioning and usage tracking for team credit billing.
+ */
+export class AutumnService {
+ private customerOrgCache = new BoundedMap<string, string>(50_000);
+ private ensuredOrgs = new BoundedSet<string>(50_000);
+ private ensuredTeams = new BoundedSet<string>(50_000);
+
+ private isPreviewTeam(teamId: string): boolean {
+ return teamId === "preview" || teamId.startsWith("preview_");
+ }
+
+ private async lookupOrgIdForTeam(teamId: string): Promise<string> {
+ const { data, error } = await supabase_rr_service
+ .from("teams")
+ .select("org_id")
+ .eq("id", teamId)
+ .single();
+
+ if (error) throw error;
+ if (!data?.org_id) {
+ throw new Error(`Missing org_id for team ${teamId}`);
+ }
+
+ return data.org_id;
+ }
+
+ private getErrorStatus(error: unknown): number | undefined {
+ const status = (error as any)?.statusCode ?? (error as any)?.status;
+ if (typeof status === "number") return status;
+ const responseStatus = (error as any)?.response?.status;
+ 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 {
+ 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.warn("Autumn getOrCreateCustomer failed", { 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.warn("Autumn getEntity failed", { customerId, entityId, error });
+ return null;
+ }
+ }
+
+ private async createEntity({
+ customerId,
+ entityId,
+ featureId,
+ name,
+ }: CreateEntityParams): Promise<CreateEntityResult> {
+ if (!autumnClient) return { ok: false, conflict: false };
+
+ try {
+ const entity = await autumnClient.entities.create({
+ customerId,
+ entityId,
+ featureId,
+ name: name ?? undefined,
+ });
+ 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.warn("Autumn createEntity failed", {
+ customerId,
+ entityId,
+ featureId,
+ error,
+ });
+ return { ok: false, conflict: false };
+ }
+ }
+
+ private async track({
+ customerId,
+ entityId,
+ featureId,
+ value,
+ properties,
+ }: TrackParams): Promise<void> {
+ if (!autumnClient) return;
+
+ try {
+ await autumnClient.track({
+ customerId,
+ entityId,
+ featureId,
+ value,
+ properties,
+ });
+ logger.info("Autumn track succeeded", {
+ customerId,
+ entityId,
+ featureId,
+ value,
+ });
+ } catch (error) {
+ logger.warn("Autumn track failed", {
+ customerId,
+ entityId,
+ featureId,
+ value,
+ error,
+ });
+ }
+ }
+
+ /**
+ * 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 is performed first so that already-provisioned
+ * teams incur no HTTP calls β€” not even the `ensureOrgProvisioned` round-trip.
+ */
+ async ensureTeamProvisioned({
+ teamId,
+ orgId,
+ name,
+ }: EnsureTeamProvisionedParams): Promise<void> {
+ if (!autumnClient) return;
+ if (this.isPreviewTeam(teamId)) return;
+ // Fast path: team is already fully provisioned.
+ if (this.ensuredTeams.has(teamId)) return;
+
+ try {
+ const resolvedOrgId = orgId ?? (await this.lookupOrgIdForTeam(teamId));
+ this.customerOrgCache.set(teamId, resolvedOrgId);
+ await this.ensureOrgProvisioned({ orgId: resolvedOrgId });
+
+ const entity = await this.getEntity({
+ customerId: resolvedOrgId,
+ entityId: teamId,
+ });
+
+ if (!entity) {
+ const result = await this.createEntity({
+ customerId: resolvedOrgId,
+ entityId: teamId,
+ featureId: CREDITS_FEATURE_ID,
+ name,
+ });
+ if (result.ok || 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(teamId);
+ }
+ // Genuine error: leave ensuredTeams empty so the next request retries.
+ return;
+ }
+
+ this.ensuredTeams.add(teamId);
+ } catch (error) {
+ logger.warn("Autumn ensureTeamProvisioned failed", { teamId, error });
+ }
+ }
+
+ /**
+ * Resolves the orgId for a team, returning the cached value when available
+ * and populating the cache on miss. Does NOT provision anything.
+ */
+ private async resolveOrgId(teamId: string): Promise<string> {
+ const cached = this.customerOrgCache.get(teamId);
+ if (cached) return cached;
+ const orgId = await this.lookupOrgIdForTeam(teamId);
+ this.customerOrgCache.set(teamId, orgId);
+ return orgId;
+ }
+
+ /**
+ * Resolves and warms the Autumn customer/entity context needed before tracking usage.
+ *
+ * When both caches are warm (orgId known + team fully provisioned) we return
+ * immediately without calling ensureTeamProvisioned, avoiding redundant
+ * map/set lookups on every billing operation.
+ */
+ private async ensureTrackingContext(teamId: string): Promise<string> {
+ const orgId = await this.resolveOrgId(teamId);
+ if (!this.ensuredTeams.has(teamId)) {
+ await this.ensureTeamProvisioned({ teamId, orgId });
+ }
+ return orgId;
+ }
+
+ /**
+ * Records a credit usage event in Autumn at request time.
+ * Returns true on success, false if Autumn is unavailable or an error occurs.
+ *
+ * The experiment gate is evaluated here β€” once per request β€” using a stable
+ * bucket derived from the org UUID so the same org always gets the same
+ * answer for a given AUTUMN_EXPERIMENT_PERCENT value.
+ */
+ async reserveCredits({
+ teamId,
+ value,
+ properties,
+ }: TrackCreditsParams): Promise<boolean> {
+ if (!isAutumnEnabled()) return false; // fast bail-out: experiment off
+ if (!autumnClient) return false;
+ if (this.isPreviewTeam(teamId)) return false;
+
+ try {
+ const orgId = await this.resolveOrgId(teamId);
+ if (!isAutumnEnabled(orgId)) return false; // stable percent gate
+
+ const customerId = await this.ensureTrackingContext(teamId);
+ await this.track({
+ customerId,
+ entityId: teamId,
+ featureId: CREDITS_FEATURE_ID,
+ value,
+ properties,
+ });
+ return true;
+ } catch (error) {
+ logger.warn("Autumn reserveCredits failed", {
+ teamId,
+ value,
+ error,
+ });
+ return false;
+ }
+ }
+
+ /**
+ * Reverses a prior reserveCredits call by tracking a negative usage event.
+ */
+ async refundCredits({
+ teamId,
+ value,
+ properties,
+ }: TrackCreditsParams): Promise<void> {
+ if (!autumnClient) return;
+ if (this.isPreviewTeam(teamId)) return;
+
+ try {
+ const customerId = await this.ensureTrackingContext(teamId);
+ await this.track({
+ customerId,
+ entityId: teamId,
+ featureId: CREDITS_FEATURE_ID,
+ value: -value,
+ properties: { ...properties, source: "autumn_refund" },
+ });
+ } catch (error) {
+ logger.warn("Autumn refundCredits failed", { teamId, value, error });
+ }
+ }
+}
+
+export const autumnService = new AutumnService();
diff --git a/apps/api/src/services/autumn/client.ts b/apps/api/src/services/autumn/client.ts
new file mode 100644
index 000000000..08416d085
--- /dev/null
+++ b/apps/api/src/services/autumn/client.ts
@@ -0,0 +1,16 @@
+import { Autumn } from "autumn-js";
+import { config } from "../../config";
+import { logger } from "../../lib/logger";
+
+let missingKeyWarned = false;
+
+if (!config.AUTUMN_SECRET_KEY && !missingKeyWarned) {
+ logger.warn(
+ "AUTUMN_SECRET_KEY is not set - add AUTUMN_SECRET_KEY to enable Autumn",
+ );
+ missingKeyWarned = true;
+}
+
+export const autumnClient = config.AUTUMN_SECRET_KEY
+ ? new Autumn({ secretKey: config.AUTUMN_SECRET_KEY, timeoutMs: 5000 })
+ : null;
diff --git a/apps/api/src/services/autumn/types.ts b/apps/api/src/services/autumn/types.ts
new file mode 100644
index 000000000..9b72346d5
--- /dev/null
+++ b/apps/api/src/services/autumn/types.ts
@@ -0,0 +1,49 @@
+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;
+};
+
+export type TrackParams = {
+ customerId: string;
+ entityId?: string;
+ featureId: string;
+ value: number;
+ properties?: Record<string, unknown>;
+};
+
+export type EnsureOrgProvisionedParams = {
+ orgId: string;
+ name?: string | null;
+ email?: string | null;
+};
+
+export type EnsureTeamProvisionedParams = {
+ teamId: string;
+ orgId?: string | null;
+ name?: string | null;
+};
+
+export type TrackCreditsParams = {
+ teamId: string;
+ value: number;
+ properties?: Record<string, unknown>;
+};
+
+export type CreateEntityResult =
+ | { ok: true; entity: unknown }
+ | { ok: false; conflict: true }
+ | { ok: false; conflict: false };
diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts
index 8878c2145..4b65a870e 100644
--- a/apps/api/src/services/billing/batch_billing.ts
+++ b/apps/api/src/services/billing/batch_billing.ts
@@ -5,6 +5,7 @@ import { supabase_service } from "../supabase";
import * as Sentry from "@sentry/node";
import { withAuth } from "../../lib/withAuth";
import { setCachedACUC, setCachedACUCTeam } from "../../controllers/auth";
+import { autumnService } from "../autumn/autumn.service";
// Configuration constants
const BATCH_KEY = "billing_batch";
@@ -21,6 +22,8 @@ interface BillingOperation {
is_extract: boolean;
timestamp: string;
api_key_id: number | null;
+ /** True if credits were pre-reserved in Autumn at request time via reserveCredits(). */
+ autumnReserved: boolean;
}
// Grouped billing operations for batch processing
@@ -52,7 +55,14 @@ async function releaseLock() {
logger.info("πŸ”“ Released billing batch processing lock");
}
-// Main function to process the billing batch
+/**
+ * Dequeues pending billing operations from Redis, groups them by team, and
+ * commits each group to Supabase via the `bill_team_6` RPC.
+ *
+ * For groups where credits were pre-reserved in Autumn (`autumnReserved: true`),
+ * a refund is issued on failure; nothing is done on success. For unreserved
+ * groups (legacy / BullMQ path), Autumn is updated post-commit.
+ */
export async function processBillingBatch() {
const redis = getRedisConnection();
@@ -120,9 +130,16 @@ export async function processBillingBatch() {
continue;
}
+ // Compute per-group credit split before billing so the catch block can
+ // issue a refund even if supaBillTeam throws.
+ const reservedCredits = group.operations
+ .filter(op => op.autumnReserved)
+ .reduce((sum, op) => sum + op.credits, 0);
+ const unreservedCredits = group.total_credits - reservedCredits;
+
try {
// Execute the actual billing
- await withAuth(supaBillTeam, {
+ const billingResult = await withAuth(supaBillTeam, {
success: true,
message: "No DB, bypassed.",
})(
@@ -134,9 +151,43 @@ export async function processBillingBatch() {
group.is_extract,
);
+ if (!billingResult.success) {
+ logger.warn(
+ `⚠️ Billing returned success: false for team ${group.team_id}, skipping Autumn tracking`,
+ { billingResult, team_id: group.team_id, credits: group.total_credits },
+ );
+ // Refund only the credits that were actually reserved in Autumn.
+ if (reservedCredits > 0) {
+ void autumnService.refundCredits({
+ teamId: group.team_id,
+ value: reservedCredits,
+ properties: {
+ source: "processBillingBatch_failure",
+ apiKeyId: group.api_key_id,
+ subscriptionId: group.subscription_id,
+ },
+ });
+ }
+ continue;
+ }
+
logger.info(
- `βœ… Successfully billed team ${group.team_id} for ${group.total_credits} ${group.is_extract ? "tokens" : "credits"}`,
+ `βœ… Successfully billed team ${group.team_id} for ${group.total_credits} credits`,
);
+
+ // Track only unreserved credits post-commit; reserved credits were
+ // already recorded in Autumn at request time.
+ if (unreservedCredits > 0) {
+ void autumnService.reserveCredits({
+ teamId: group.team_id,
+ value: unreservedCredits,
+ properties: {
+ source: "processBillingBatch",
+ apiKeyId: group.api_key_id,
+ subscriptionId: group.subscription_id,
+ },
+ });
+ }
} catch (error) {
logger.error(`❌ Failed to bill team ${group.team_id}`, {
error,
@@ -149,6 +200,18 @@ export async function processBillingBatch() {
credits: group.total_credits,
},
});
+ // Billing threw before committing β€” refund any Autumn-reserved credits.
+ if (reservedCredits > 0) {
+ void autumnService.refundCredits({
+ teamId: group.team_id,
+ value: reservedCredits,
+ properties: {
+ source: "processBillingBatch_exception",
+ apiKeyId: group.api_key_id,
+ subscriptionId: group.subscription_id,
+ },
+ });
+ }
}
}
@@ -182,13 +245,20 @@ export function startBillingBatchProcessing() {
batchInterval.unref();
}
-// Add a billing operation to the queue
+/**
+ * Enqueues a billing operation for async batch processing.
+ *
+ * Pass `autumnReserved: true` if credits were already reserved in Autumn via
+ * `autumnService.reserveCredits()` β€” the batch processor will refund on
+ * `bill_team_6` failure and skip re-tracking on success.
+ */
export async function queueBillingOperation(
team_id: string,
subscription_id: string | null | undefined,
credits: number,
api_key_id: number | null,
is_extract: boolean = false,
+ autumnReserved: boolean = false,
) {
// Skip queuing for preview teams
if (team_id === "preview" || team_id.startsWith("preview_")) {
@@ -211,6 +281,7 @@ export async function queueBillingOperation(
is_extract,
timestamp: new Date().toISOString(),
api_key_id,
+ autumnReserved,
};
// Add operation to Redis list
@@ -320,7 +391,11 @@ async function supaBillTeam(
return { success: false, error };
}
- await getRedisConnection().sadd("billed_teams", team_id);
+ // Fire-and-forget β€” a Redis failure here must not trigger a false Autumn refund
+ // after bill_team_6 has already committed.
+ getRedisConnection().sadd("billed_teams", team_id).catch(err => {
+ _logger.warn("Failed to add team to billed_teams set", { err, team_id });
+ });
// Update cached ACUC to reflect the new credit usage
(async () => {
diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts
index 51c8510f7..ed0db30ab 100644
--- a/apps/api/src/services/billing/credit_billing.ts
+++ b/apps/api/src/services/billing/credit_billing.ts
@@ -8,6 +8,7 @@ import { AuthCreditUsageChunk } from "../../controllers/v1/types";
import { autoCharge } from "./auto_charge";
import { getValue, setValue } from "../redis";
import { queueBillingOperation } from "./batch_billing";
+import { autumnService } from "../autumn/autumn.service";
import type { Logger } from "winston";
/**
@@ -20,7 +21,6 @@ export async function billTeam(
api_key_id: number | null,
logger?: Logger,
) {
- // Maintain the withAuth wrapper for authentication
return withAuth(
async (
team_id: string,
@@ -29,13 +29,20 @@ export async function billTeam(
api_key_id: number | null,
logger: Logger | undefined,
) => {
- // Within the authenticated context, queue the billing operation
+ // Reserve in Autumn at request time; await so autumnReserved is accurate.
+ // billTeam is fire-and-forget at call sites, so this doesn't block responses.
+ const autumnReserved = await autumnService.reserveCredits({
+ teamId: team_id,
+ value: credits,
+ properties: { source: "billTeam", apiKeyId: api_key_id },
+ });
return queueBillingOperation(
team_id,
subscription_id,
credits,
api_key_id,
false,
+ autumnReserved,
);
},
{ success: true, message: "No DB, bypassed." },
diff --git a/apps/api/src/services/indexing/index-worker.ts b/apps/api/src/services/indexing/index-worker.ts
index b527fc3c6..5b4d3a881 100644
--- a/apps/api/src/services/indexing/index-worker.ts
+++ b/apps/api/src/services/indexing/index-worker.ts
@@ -803,6 +803,5 @@ const BROWSER_ACTIVITY_INSERT_INTERVAL = 10000;
clearInterval(browserActivityInterval);
clearInterval(omceInserterInterval);
clearInterval(billingTallyInterval);
-
logger.info("All workers shut down, exiting process");
})();
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 7c9a59985..0dfe14f8f 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -40,6 +40,7 @@ x-common-env: &common-env
MODEL_NAME: ${MODEL_NAME}
MODEL_EMBEDDING_NAME: ${MODEL_EMBEDDING_NAME}
OLLAMA_BASE_URL: ${OLLAMA_BASE_URL}
+ AUTUMN_SECRET_KEY: ${AUTUMN_SECRET_KEY}
SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL}
BULL_AUTH_KEY: ${BULL_AUTH_KEY}
TEST_API_KEY: ${TEST_API_KEY}