Skip to content

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

perf(api): cache Autumn concurrency and rate limits in the ACUC (#4948)

Gergő Móricz843188c8fda7
diff --git a/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts b/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts
index 86629c064..51602a938 100644
--- a/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts
+++ b/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts
@@ -42,8 +42,10 @@ vi.mock("../../services/autumn/autumn.service", async importOriginal => {
return {
...actual,
autumnService: {
- getRateLimitMultiplier: vi.fn().mockResolvedValue(1),
- getKnownRateLimitMultiplier: vi.fn().mockResolvedValue(1),
+ getTeamLimits: vi.fn().mockResolvedValue({
+ concurrency_limit: 2,
+ rate_limit_multiplier: 1,
+ }),
},
};
});
diff --git a/apps/api/src/__tests__/routes/interact-agent-concurrency.routes.test.ts b/apps/api/src/__tests__/routes/interact-agent-concurrency.routes.test.ts
index eb9e2642c..e38146cc6 100644
--- a/apps/api/src/__tests__/routes/interact-agent-concurrency.routes.test.ts
+++ b/apps/api/src/__tests__/routes/interact-agent-concurrency.routes.test.ts
@@ -54,10 +54,11 @@ vi.mock("../../services/autumn/autumn.service", async importOriginal => {
return {
...actual,
autumnService: {
- getRateLimitMultiplier: vi.fn().mockResolvedValue(1),
- getKnownRateLimitMultiplier: vi.fn().mockResolvedValue(1),
// Free plan: Autumn grants CONCURRENCY 2.
- getConcurrencyLimit: vi.fn().mockResolvedValue(2),
+ getTeamLimits: vi.fn().mockResolvedValue({
+ concurrency_limit: 2,
+ rate_limit_multiplier: 1,
+ }),
checkCredits: vi.fn().mockResolvedValue(null),
},
};
diff --git a/apps/api/src/__tests__/snips/v2/concurrency.test.ts b/apps/api/src/__tests__/snips/v2/concurrency.test.ts
index 8baf90f87..a02add7cf 100644
--- a/apps/api/src/__tests__/snips/v2/concurrency.test.ts
+++ b/apps/api/src/__tests__/snips/v2/concurrency.test.ts
@@ -1,4 +1,10 @@
-import { describeIf, TEST_PRODUCTION, TEST_SUITE_WEBSITE } from "../lib";
+import request from "supertest";
+import {
+ describeIf,
+ TEST_API_URL,
+ TEST_PRODUCTION,
+ TEST_SUITE_WEBSITE,
+} from "../lib";
import {
batchScrapeWithConcurrencyTracking,
crawlWithConcurrencyTracking,
@@ -10,6 +16,29 @@ const accountConcurrencyLimit = 20;
describeIf(TEST_PRODUCTION)("Concurrency queue and limit", () => {
const base = TEST_SUITE_WEBSITE;
+ it.concurrent(
+ "reports the team's limit from a cold and a warm ACUC alike",
+ async () => {
+ const identity = await idmux({
+ name: "concurrency/reports the team's limit from a cold and a warm ACUC alike",
+ concurrency: accountConcurrencyLimit,
+ credits: 100,
+ });
+
+ const maxConcurrency = async () => {
+ const res = await request(TEST_API_URL)
+ .get("/v2/team/queue-status")
+ .set("Authorization", `Bearer ${identity.apiKey}`);
+ expect(res.statusCode).toBe(200);
+ return res.body.maxConcurrency;
+ };
+
+ expect(await maxConcurrency()).toBe(accountConcurrencyLimit);
+ expect(await maxConcurrency()).toBe(accountConcurrencyLimit);
+ },
+ 60000,
+ );
+
it.concurrent(
"crawl utilizes full concurrency limit and doesn't go over",
async () => {
diff --git a/apps/api/src/controllers/__tests__/auth.test.ts b/apps/api/src/controllers/__tests__/auth.test.ts
index 6ca3ebb81..569bdc25c 100644
--- a/apps/api/src/controllers/__tests__/auth.test.ts
+++ b/apps/api/src/controllers/__tests__/auth.test.ts
@@ -96,8 +96,13 @@ vi.mock("../../lib/spur", () => ({
}));
vi.mock("../../services/autumn/autumn.service", () => ({
+ DEFAULT_TEAM_LIMITS: {
+ concurrency_limit: 2,
+ rate_limit_multiplier: 1,
+ is_paid_plan: false,
+ },
autumnService: {
- getRateLimitMultiplier: vi.fn(),
+ getTeamLimits: vi.fn(),
},
}));
@@ -105,6 +110,14 @@ vi.mock("../../services/agent-sponsor", () => ({
getAgentSponsorStatus: vi.fn(),
}));
+function mockMultiplier(rate_limit_multiplier: number) {
+ vi.mocked(autumnService.getTeamLimits).mockResolvedValue({
+ concurrency_limit: 2,
+ rate_limit_multiplier,
+ is_paid_plan: false,
+ });
+}
+
describe("authenticateUser", () => {
const originalUseDbAuth = config.USE_DB_AUTHENTICATION;
const originalKeylessProxySecret = config.KEYLESS_PROXY_SECRET;
@@ -120,7 +133,7 @@ describe("authenticateUser", () => {
beforeEach(() => {
vi.mocked(isKeylessConfigured).mockReturnValue(false);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
vi.mocked(getAutumnRateLimiter).mockReturnValue({
consume: vi.fn().mockResolvedValue(undefined),
} as never);
@@ -492,7 +505,7 @@ describe("authenticateUser", () => {
vi.mocked(redlock.using).mockImplementation(
async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
const consume = vi.fn().mockResolvedValue(undefined);
vi.mocked(getAutumnRateLimiter).mockReturnValue({ consume } as never);
@@ -529,7 +542,7 @@ describe("authenticateUser", () => {
flags: null,
},
]);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
vi.mocked(getAutumnRateLimiter).mockReturnValue({
consume: vi.fn().mockResolvedValue(undefined),
} as never);
@@ -551,7 +564,11 @@ describe("authenticateUser", () => {
"11111111-1111-1111-8111-111111111111",
"hosted_mcp_oauth",
);
- expect(getValue).not.toHaveBeenCalled();
+ // The team's limits may come from the team ACUC, but the credential's
+ // own ACUC is never read from or written to Redis.
+ expect(getValue).not.toHaveBeenCalledWith(
+ expect.stringMatching(/^acuc_(general|hosted_mcp_oauth)_/),
+ );
expect(setValue).not.toHaveBeenCalled();
});
@@ -756,7 +773,7 @@ describe("authenticateUser", () => {
vi.mocked(redlock.using).mockImplementation(
async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);
+ mockMultiplier(50);
const auth = await authenticateUser(
{
@@ -770,9 +787,8 @@ describe("authenticateUser", () => {
);
expect(auth.success).toBe(true);
- // The override replaces the whole base × multiplier computation, so the
- // Autumn multiplier is never fetched and a neutral 1 is passed instead.
- expect(autumnService.getRateLimitMultiplier).not.toHaveBeenCalled();
+ // The override replaces the whole base × multiplier computation, so a
+ // neutral 1 is passed instead of the team's multiplier.
expect(getAutumnRateLimiter).toHaveBeenCalledWith(
RateLimiterMode.Scrape,
1,
@@ -796,7 +812,7 @@ describe("authenticateUser", () => {
vi.mocked(redlock.using).mockImplementation(
async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);
+ mockMultiplier(50);
const auth = await authenticateUser(
{
@@ -810,7 +826,7 @@ describe("authenticateUser", () => {
);
expect(auth.success).toBe(true);
- expect(autumnService.getRateLimitMultiplier).toHaveBeenCalledTimes(1);
+ expect(autumnService.getTeamLimits).toHaveBeenCalledTimes(1);
expect(getAutumnRateLimiter).toHaveBeenCalledWith(
RateLimiterMode.Scrape,
50,
@@ -847,7 +863,7 @@ describe("authenticateUser", () => {
});
it("floors a free team's multiplier at hobby for a trusted agent request", async () => {
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
const auth = await authenticateUser(
agentRequest("agent-secret"),
@@ -864,7 +880,7 @@ describe("authenticateUser", () => {
});
it("leaves a paid plan's multiplier alone for a trusted agent request", async () => {
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);
+ mockMultiplier(50);
await authenticateUser(
agentRequest("agent-secret"),
@@ -880,7 +896,7 @@ describe("authenticateUser", () => {
});
it("does not floor the multiplier when the agent interop secret is wrong", async () => {
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
await authenticateUser(
agentRequest("not-the-secret"),
@@ -897,7 +913,7 @@ describe("authenticateUser", () => {
it("does not floor the multiplier when no agent interop secret is configured", async () => {
config.AGENT_INTEROP_SECRET = undefined;
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
await authenticateUser(
agentRequest("agent-secret"),
@@ -923,7 +939,7 @@ describe("authenticateUser", () => {
flags: overrideFlags,
},
]);
- vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ mockMultiplier(1);
await authenticateUser(
agentRequest("agent-secret"),
@@ -932,8 +948,7 @@ describe("authenticateUser", () => {
);
// The override replaces the whole base × multiplier computation, so the
- // floor never applies and the Autumn multiplier is never fetched.
- expect(autumnService.getRateLimitMultiplier).not.toHaveBeenCalled();
+ // floor never applies.
expect(getAutumnRateLimiter).toHaveBeenCalledWith(
RateLimiterMode.Scrape,
1,
@@ -1061,7 +1076,7 @@ describe("authenticateUser", () => {
allow,
);
- expect(autumnService.getRateLimitMultiplier).toHaveBeenCalledWith(
+ expect(autumnService.getTeamLimits).toHaveBeenCalledWith(
"team-mcp",
"org-mcp",
);
@@ -1212,7 +1227,7 @@ describe("authenticateUser", () => {
);
if (!auth.success) throw new Error("expected fallback auth to succeed");
expect(auth.chunk?.team_id).toBe("team-mcp");
- expect(autumnService.getRateLimitMultiplier).toHaveBeenCalledWith(
+ expect(autumnService.getTeamLimits).toHaveBeenCalledWith(
"team-mcp",
"org-mcp",
);
@@ -1344,6 +1359,182 @@ describe("authenticateUser", () => {
);
});
+ describe("Autumn limits on the ACUC", () => {
+ const apiKey = "00000000-0000-4000-8000-000000000000";
+ const keyRow = {
+ api_key: apiKey,
+ api_key_id: 1,
+ team_id: "team-1",
+ org_id: "org-1",
+ flags: null,
+ };
+ const known = {
+ concurrency_limit: 7,
+ rate_limit_multiplier: 25,
+ is_paid_plan: false,
+ };
+ const failOpen = {
+ concurrency_limit: 200,
+ rate_limit_multiplier: 2500,
+ is_paid_plan: true,
+ };
+ const authRequest = {
+ headers: { authorization: `Bearer ${apiKey}` },
+ socket: { remoteAddress: "127.0.0.1" },
+ };
+
+ beforeEach(() => {
+ config.USE_DB_AUTHENTICATION = true;
+ vi.mocked(getValue).mockResolvedValue(null);
+ vi.mocked(redlock.using).mockImplementation(
+ async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
+ );
+ vi.mocked(authCreditUsageChunk).mockImplementation(
+ async () => [{ ...keyRow }] as never,
+ );
+ vi.mocked(authCreditUsageChunkFromTeam).mockImplementation(
+ async () => [{ ...keyRow }] as never,
+ );
+ });
+
+ it("builds the key ACUC with the team's limits and caches it for the full TTL", async () => {
+ vi.mocked(autumnService.getTeamLimits).mockResolvedValue(known);
+
+ const auth = await authenticateUser(
+ authRequest,
+ {},
+ RateLimiterMode.Scrape,
+ );
+
+ expect(auth.success).toBe(true);
+ expect(autumnService.getTeamLimits).toHaveBeenCalledTimes(1);
+ expect(autumnService.getTeamLimits).toHaveBeenCalledWith(
+ "team-1",
+ "org-1",
+ );
+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(
+ RateLimiterMode.Scrape,
+ 25,
+ null,
+ );
+ await vi.waitFor(() => expect(setValue).toHaveBeenCalled());
+ const [key, value, ttl] = vi.mocked(setValue).mock.calls[0];
+ expect(key).toBe(`acuc_general_${apiKey}_scrape`);
+ expect(JSON.parse(value)).toMatchObject(known);
+ expect(ttl).toBe(600);
+ });
+
+ it("puts the fail-open limits on the chunk and caches it only briefly when Autumn fails", async () => {
+ vi.mocked(autumnService.getTeamLimits).mockRejectedValue(
+ new Error("Autumn down"),
+ );
+
+ await authenticateUser(authRequest, {}, RateLimiterMode.Scrape);
+
+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(
+ RateLimiterMode.Scrape,
+ 2500,
+ null,
+ );
+ await vi.waitFor(() => expect(setValue).toHaveBeenCalled());
+ const [, value, ttl] = vi.mocked(setValue).mock.calls[0];
+ expect(JSON.parse(value)).toMatchObject(failOpen);
+ expect(ttl).toBe(60);
+ });
+
+ it("reads the limits off a cached ACUC without asking Autumn", async () => {
+ vi.mocked(getValue).mockResolvedValue(
+ JSON.stringify({ ...keyRow, ...known }),
+ );
+
+ await authenticateUser(authRequest, {}, RateLimiterMode.Scrape);
+
+ expect(autumnService.getTeamLimits).not.toHaveBeenCalled();
+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(
+ RateLimiterMode.Scrape,
+ 25,
+ null,
+ );
+ });
+
+ it("fills a cached ACUC that predates the limits with one live read, writing nothing back", async () => {
+ vi.mocked(getValue).mockResolvedValue(JSON.stringify(keyRow));
+ vi.mocked(autumnService.getTeamLimits).mockResolvedValue(known);
+
+ await expect(getACUCTeam("team-1")).resolves.toMatchObject(known);
+
+ expect(autumnService.getTeamLimits).toHaveBeenCalledTimes(1);
+ expect(setValue).not.toHaveBeenCalled();
+ });
+
+ it("fills a cached ACUC that predates is_paid_plan with one live read", async () => {
+ vi.mocked(getValue).mockResolvedValue(
+ JSON.stringify({
+ ...keyRow,
+ concurrency_limit: 7,
+ rate_limit_multiplier: 25,
+ }),
+ );
+ vi.mocked(autumnService.getTeamLimits).mockResolvedValue({
+ ...known,
+ is_paid_plan: true,
+ });
+
+ await expect(getACUCTeam("team-1")).resolves.toMatchObject({
+ is_paid_plan: true,
+ });
+ expect(autumnService.getTeamLimits).toHaveBeenCalledTimes(1);
+ });
+
+ it("builds the team ACUC with the fail-open limits cached only briefly", async () => {
+ vi.mocked(autumnService.getTeamLimits).mockRejectedValue(
+ new Error("Autumn down"),
+ );
+
+ await expect(getACUCTeam("team-1")).resolves.toMatchObject(failOpen);
+
+ await vi.waitFor(() => expect(setValue).toHaveBeenCalled());
+ const [key, value, ttl] = vi.mocked(setValue).mock.calls[0];
+ expect(key).toBe("acuc_team_team-1_scrape");
+ expect(JSON.parse(value)).toMatchObject(failOpen);
+ expect(ttl).toBe(60);
+ });
+
+ it("builds a new chunk instead of mutating the row it read", async () => {
+ const row = { ...keyRow };
+ vi.mocked(authCreditUsageChunkFromTeam).mockResolvedValue([row] as never);
+ vi.mocked(autumnService.getTeamLimits).mockResolvedValue(known);
+
+ await expect(getACUCTeam("team-1")).resolves.toMatchObject(known);
+ expect(row).toEqual(keyRow);
+ });
+
+ it("gives an uncached key chunk the team ACUC's limits without asking Autumn", async () => {
+ config.MCP_DELEGATED_CREDENTIAL_SECRET = "mcp-delegation-secret";
+ vi.mocked(getValue).mockImplementation(async key =>
+ key === "acuc_team_team-1_scrape"
+ ? JSON.stringify({ ...keyRow, ...known })
+ : null,
+ );
+
+ await authenticateUser(
+ {
+ headers: { authorization: `Bearer ${signDelegation()}` },
+ socket: { remoteAddress: "127.0.0.1" },
+ },
+ {},
+ RateLimiterMode.Scrape,
+ );
+
+ expect(autumnService.getTeamLimits).not.toHaveBeenCalled();
+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(
+ RateLimiterMode.Scrape,
+ 25,
+ null,
+ );
+ });
+ });
+
it("clears purpose-qualified and legacy ACUC cache entries", async () => {
await clearACUC("api-key");
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 14250612b..8e6e6c3f9 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -37,11 +37,7 @@ import {
AuthCreditUsageChunkRow,
} from "../db/rpc";
import { AuthResponse, RateLimiterMode } from "../types";
-import {
- AuthCreditUsageChunk,
- AuthCreditUsageChunkFromTeam,
- TeamFlags,
-} from "./v1/types";
+import { AuthCreditUsageChunk, AuthCreditUsageChunkFromTeam } from "./v1/types";
import {
FIRECRAWL_REST_RESOURCE,
OAuthIntrospectionUnavailableError,
@@ -49,9 +45,59 @@ import {
} from "../services/oauth-token-introspection";
import type { OAuthIntrospectionResponse } from "../services/oauth-token-introspection";
import { verifyMcpDelegatedCredential } from "../lib/mcp-delegated-credential";
-import { autumnService } from "../services/autumn/autumn.service";
+import {
+ autumnService,
+ DEFAULT_TEAM_LIMITS,
+ type TeamLimits,
+} from "../services/autumn/autumn.service";
import { ReplyError } from "ioredis";
+const ACUC_TTL_SECONDS = 600;
+// A chunk built on the fail-open limits is cached only briefly, so a
+// transient Autumn error is retried within a minute.
+const ACUC_FAIL_OPEN_TTL_SECONDS = 60;
+// When Autumn can't answer, fail open rather than throttle or gate real teams
+// on the low defaults. Generous but bounded: the concurrency queue cap still
+// applies.
+const FAIL_OPEN_LIMITS: TeamLimits = {
+ concurrency_limit: 200,
+ rate_limit_multiplier: 2500,
+ is_paid_plan: true,
+};
+
+/** The team's effective limits, and how long a chunk carrying them may live. */
+async function readTeamLimits(
+ teamId: string,
+ orgId: string | null,
+): Promise<{ limits: TeamLimits; ttl: number }> {
+ try {
+ return {
+ limits: await autumnService.getTeamLimits(teamId, orgId),
+ ttl: ACUC_TTL_SECONDS,
+ };
+ } catch (error) {
+ logger.error("Autumn limits unavailable, failing open", { teamId, error });
+ return { limits: FAIL_OPEN_LIMITS, ttl: ACUC_FAIL_OPEN_TTL_SECONDS };
+ }
+}
+
+/** A cached chunk, with live limits when it predates them (not written back). */
+async function withLimits<T extends AuthCreditUsageChunkFromTeam>(
+ chunk: T,
+): Promise<T> {
+ if (
+ chunk.concurrency_limit !== undefined &&
+ chunk.rate_limit_multiplier !== undefined &&
+ chunk.is_paid_plan !== undefined
+ ) {
+ return chunk;
+ }
+ return {
+ ...chunk,
+ ...(await readTeamLimits(chunk.team_id, chunk.org_id)).limits,
+ };
+}
+
function normalizedApiIsUuid(potentialUuid: string): boolean {
// Check if the string is a valid UUID
return isValidUuid(potentialUuid);
@@ -65,6 +111,7 @@ async function setCachedACUC(
| null
| ((acuc: AuthCreditUsageChunk) => AuthCreditUsageChunk | null),
credentialPurpose: "general" | "hosted_mcp_oauth" = "general",
+ ttl = ACUC_TTL_SECONDS,
) {
const cacheKeyACUC = `acuc_${credentialPurpose}_${api_key}_${is_extract ? "extract" : "scrape"}`;
const redLockKey = `lock_${cacheKeyACUC}`;
@@ -87,8 +134,7 @@ async function setCachedACUC(
throw signal.error;
}
- // Cache for 10 minutes. - mogery
- await setValue(cacheKeyACUC, JSON.stringify(acuc), 600, true);
+ await setValue(cacheKeyACUC, JSON.stringify(acuc), ttl, true);
});
} catch (error) {
logger.error("Error updating cached ACUC", {
@@ -108,6 +154,7 @@ const mockPreviewACUC: (
flags: null,
is_banned: false,
is_extract,
+ ...DEFAULT_TEAM_LIMITS,
});
const mockACUC: () => AuthCreditUsageChunk = () => ({
@@ -118,6 +165,7 @@ const mockACUC: () => AuthCreditUsageChunk = () => ({
flags: null,
is_banned: false,
is_extract: false,
+ ...DEFAULT_TEAM_LIMITS,
});
/**
@@ -206,7 +254,7 @@ async function getACUC(
}
if (cachedACUC !== null) {
try {
- return JSON.parse(cachedACUC);
+ return await withLimits(JSON.parse(cachedACUC));
} catch (error) {
logger.warn("Ignoring malformed ACUC cache entry", {
error,
@@ -258,16 +306,28 @@ async function getACUC(
? null
: (data[0] as any);
- if (chunk) {
- chunk.is_extract = isExtract;
- }
+ if (chunk === null) return null;
// NOTE: Should we cache null chunks? - mogery
- if (chunk !== null && useCache) {
- setCachedACUC(api_key, isExtract, chunk, credentialPurpose);
+ if (useCache) {
+ const { limits, ttl } = await readTeamLimits(chunk.team_id, chunk.org_id);
+ const built = { ...chunk, is_extract: isExtract, ...limits };
+ setCachedACUC(api_key, isExtract, built, credentialPurpose, ttl);
+ return built;
}
- return chunk;
+ // An uncached chunk (a hosted MCP credential) takes the team ACUC's
+ // limits, so authenticating it per request never costs an Autumn read.
+ const team = await getACUCTeam(chunk.team_id).catch(() => null);
+ const limits =
+ team ?? (await readTeamLimits(chunk.team_id, chunk.org_id)).limits;
+ return {
+ ...chunk,
+ is_extract: isExtract,
+ concurrency_limit: limits.concurrency_limit,
+ rate_limit_multiplier: limits.rate_limit_multiplier,
+ is_paid_plan: limits.is_paid_plan,
+ };
} else {
return null;
}
@@ -282,6 +342,7 @@ async function setCachedACUCTeam(
| ((
acuc: AuthCreditUsageChunkFromTeam,
) => AuthCreditUsageChunkFromTeam | null),
+ ttl = ACUC_TTL_SECONDS,
) {
const cacheKeyACUC = `acuc_team_${team_id}_${is_extract ? "extract" : "scrape"}`;
const redLockKey = `lock_${cacheKeyACUC}`;
@@ -304,8 +365,7 @@ async function setCachedACUCTeam(
throw signal.error;
}
- // Cache for 10 minutes. - mogery
- await setValue(cacheKeyACUC, JSON.stringify(acuc), 600, true);
+ await setValue(cacheKeyACUC, JSON.stringify(acuc), ttl, true);
});
} catch (error) {
logger.error("Error updating cached ACUC", {
@@ -368,7 +428,7 @@ export async function getACUCTeam(
// A corrupt entry is a miss, not a failure: callers that fall back to a
// null org on a throw would otherwise take the high fail-open limits.
try {
- return JSON.parse(cachedACUC);
+ return await withLimits(JSON.parse(cachedACUC));
} catch (error) {
logger.warn("Ignoring malformed ACUC cache entry", {
cacheKey: cacheKeyACUC,
@@ -421,12 +481,17 @@ export async function getACUCTeam(
? null
: (data[0] as any);
+ if (chunk === null) return null;
+
+ const { limits, ttl } = await readTeamLimits(chunk.team_id, chunk.org_id);
+ const built = { ...chunk, ...limits };
+
// NOTE: Should we cache null chunks? - mogery
- if (chunk !== null && useCache) {
- setCachedACUCTeam(team_id, isExtract, chunk);
+ if (useCache) {
+ setCachedACUCTeam(team_id, isExtract, built, ttl);
}
- return chunk ? { ...chunk, is_extract: isExtract } : null;
+ return { ...built, is_extract: isExtract };
} else {
return null;
}
@@ -717,20 +782,16 @@ export async function authenticateUser(
* replaces the whole computation.
*/
async function buildAuthenticatedRateLimiter(
- teamId: string,
- orgId: string | null | undefined,
+ chunk: AuthCreditUsageChunk,
mode: RateLimiterMode,
- flags: TeamFlags,
minMultiplier?: number,
): Promise<RateLimiterRedis> {
+ const flags = chunk.flags;
let multiplier: number;
if (getRateLimitOverride(mode, flags?.rateLimitOverrides) !== undefined) {
multiplier = 1;
} else {
- multiplier = await autumnService.getRateLimitMultiplier(
- teamId,
- orgId ?? null,
- );
+ multiplier = chunk.rate_limit_multiplier;
if (minMultiplier !== undefined) {
multiplier = Math.max(multiplier, minMultiplier);
}
@@ -834,10 +895,8 @@ async function supaAuthenticateUser(
teamId = chunk.team_id;
subscriptionData = { team_id: teamId };
rateLimiter = await buildAuthenticatedRateLimiter(
- teamId,
- chunk.org_id,
+ chunk,
mode,
- chunk.flags,
minRateMultiplier,
);
} else if (token.startsWith("fco_")) {
@@ -904,10 +963,8 @@ async function supaAuthenticateUser(
team_id: teamId,
};
rateLimiter = await buildAuthenticatedRateLimiter(
- teamId,
- chunk.org_id,
+ chunk,
mode,
- chunk.flags,
minRateMultiplier,
);
} else {
@@ -953,10 +1010,8 @@ async function supaAuthenticateUser(
team_id: teamId,
};
rateLimiter = await buildAuthenticatedRateLimiter(
- teamId,
- chunk.org_id,
+ chunk,
mode,
- chunk.flags,
minRateMultiplier,
);
}
diff --git a/apps/api/src/controllers/v0/crawl.ts b/apps/api/src/controllers/v0/crawl.ts
index eb9818ff3..2188943b3 100644
--- a/apps/api/src/controllers/v0/crawl.ts
+++ b/apps/api/src/controllers/v0/crawl.ts
@@ -299,7 +299,7 @@ export async function crawlController(req: Request, res: Response) {
let jobPriority = await getJobPriority({
team_id,
- org_id: orgId,
+ acuc: chunk,
basePriority: 21,
});
const billing = {
@@ -373,7 +373,7 @@ export async function crawlController(req: Request, res: Response) {
apiKeyId: chunk?.api_key_id ?? null,
},
jobId,
- await getJobPriority({ team_id, org_id: orgId, basePriority: 15 }),
+ await getJobPriority({ team_id, acuc: chunk, basePriority: 15 }),
);
await addCrawlJob(id, jobId, logger);
}
diff --git a/apps/api/src/controllers/v0/scrape.ts b/apps/api/src/controllers/v0/scrape.ts
index 90319d219..257da3ddb 100644
--- a/apps/api/src/controllers/v0/scrape.ts
+++ b/apps/api/src/controllers/v0/scrape.ts
@@ -3,7 +3,11 @@ import { Request, Response } from "express";
import { autumnService } from "../../services/autumn/autumn.service";
import { authenticateUser } from "../auth";
import { RateLimiterMode, AuthResponse } from "../../types";
-import { TeamFlags, toLegacyDocument, url as urlSchema } from "../v1/types";
+import {
+ AuthCreditUsageChunk,
+ toLegacyDocument,
+ url as urlSchema,
+} from "../v1/types";
import { isUrlBlocked } from "../../scraper/WebScraper/utils/blocklist"; // Import the isUrlBlocked function
import {
defaultPageOptions,
@@ -44,9 +48,7 @@ async function scrapeHelper(
pageOptions: PageOptions,
extractorOptions: ExtractorOptions,
timeout: number,
- flags: TeamFlags,
- org_id: string | null,
- apiKeyId: number | null,
+ acuc: AuthCreditUsageChunk | null,
): Promise<{
success: boolean;
error?: string;
@@ -73,9 +75,9 @@ async function scrapeHelper(
}
if (
- isUrlBlocked(url, flags, {
+ isUrlBlocked(url, acuc?.flags ?? null, {
team_id,
- org_id,
+ org_id: acuc?.org_id ?? null,
origin: req.body?.origin ?? null,
})
) {
@@ -94,7 +96,7 @@ async function scrapeHelper(
team_id,
);
- internalOptions.orgId = org_id;
+ internalOptions.orgId = acuc?.org_id ?? null;
internalOptions.saveScrapeResultToGCS = process.env
.GCS_FIRE_ENGINE_BUCKET_NAME
? true
@@ -116,10 +118,10 @@ async function scrapeHelper(
},
startTime: Date.now(),
zeroDataRetention: false, // not supported on v0
- apiKeyId,
+ apiKeyId: acuc?.api_key_id ?? null,
},
jobId,
- await getJobPriority({ team_id, org_id, basePriority: 10 }),
+ await getJobPriority({ team_id, acuc, basePriority: 10 }),
false,
true,
);
@@ -322,9 +324,7 @@ export async function scrapeController(req: Request, res: Response) {
pageOptions,
extractorOptions,
timeout,
- chunk?.flags ?? null,
- chunk?.org_id ?? null,
- chunk?.api_key_id ?? null,
+ chunk,
);
let doc = result.data;
diff --git a/apps/api/src/controllers/v0/search.ts b/apps/api/src/controllers/v0/search.ts
index 506ce7f2e..ada26d479 100644
--- a/apps/api/src/controllers/v0/search.ts
+++ b/apps/api/src/controllers/v0/search.ts
@@ -15,7 +15,7 @@ import { addScrapeJob, waitForJob } from "../../services/queue-jobs";
import { getJobPriority } from "../../lib/job-priority";
import {
fromLegacyScrapeOptions,
- TeamFlags,
+ AuthCreditUsageChunk,
toLegacyDocument,
} from "../v1/types";
import { fromV0Combo } from "../v2/types";
@@ -40,15 +40,16 @@ async function searchHelper(
crawlerOptions: any,
pageOptions: PageOptions,
searchOptions: SearchOptions,
- flags: TeamFlags,
- org_id: string | null,
- api_key_id: number | null,
+ acuc: AuthCreditUsageChunk | null,
): Promise<{
success: boolean;
error?: string;
data?: any;
returnCode: number;
}> {
+ const flags = acuc?.flags ?? null;
+ const org_id = acuc?.org_id ?? null;
+ const api_key_id = acuc?.api_key_id ?? null;
const query = req.body.query;
const advanced = false;
if (!query) {
@@ -129,7 +130,7 @@ async function searchHelper(
const jobPriority = await getJobPriority({
team_id,
- org_id,
+ acuc,
basePriority: 20,
});
const billing = {
@@ -301,9 +302,7 @@ export async function searchController(req: Request, res: Response) {
crawlerOptions,
pageOptions,
searchOptions,
- chunk?.flags ?? null,
- chunk?.org_id ?? null,
- chunk?.api_key_id ?? null,
+ chunk,
);
const endTime = new Date().getTime();
const timeTakenInSeconds = (endTime - startTime) / 1000;
diff --git a/apps/api/src/controllers/v1/batch-scrape.ts b/apps/api/src/controllers/v1/batch-scrape.ts
index 4de3f5591..27f103c06 100644
--- a/apps/api/src/controllers/v1/batch-scrape.ts
+++ b/apps/api/src/controllers/v1/batch-scrape.ts
@@ -403,7 +403,7 @@ export async function batchScrapeController(
// set base to 21
jobPriority = await getJobPriority({
team_id: req.auth.team_id,
- org_id: req.acuc?.org_id ?? null,
+ acuc: req.acuc,
basePriority: 21,
});
}
diff --git a/apps/api/src/controllers/v1/concurrency-check.ts b/apps/api/src/controllers/v1/concurrency-check.ts
index b91f9df31..baee75487 100644
--- a/apps/api/src/controllers/v1/concurrency-check.ts
+++ b/apps/api/src/controllers/v1/concurrency-check.ts
@@ -5,7 +5,7 @@ import {
} from "./types";
import { Response } from "express";
import { getCombinedTeamActiveCount } from "../../services/worker/nuq-router";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
// Basically just middleware and error wrapping
export async function concurrencyCheckController(
@@ -17,11 +17,7 @@ export async function concurrencyCheckController(
return res.status(200).json({
success: true,
concurrency: activeJobsOfTeam,
- maxConcurrency: await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- // acuc is optional on the v1 request; null is the same org-less answer
- // the limit already gave when it could not name one.
- req.acuc?.org_id ?? null,
- ),
+ maxConcurrency:
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit,
});
}
diff --git a/apps/api/src/controllers/v1/crawl.ts b/apps/api/src/controllers/v1/crawl.ts
index 7d2deb81b..5f709a9d8 100644
--- a/apps/api/src/controllers/v1/crawl.ts
+++ b/apps/api/src/controllers/v1/crawl.ts
@@ -36,9 +36,9 @@ import { checkUrl } from "../../lib/threat-protection";
import { UnsafeDomainBlockedError } from "../../lib/threat-protection/error";
import { calculateThreatScanCredits } from "../../lib/scrape-billing";
import { billTeam } from "../../services/billing/credit_billing";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import { emitRejectedScrapeActivityEvent } from "../../lib/siem-logging";
import { requestCreditsShards } from "../../lib/request-credits-store";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
export async function crawlController(
req: RequestWithAuth<{}, CrawlResponse, CrawlRequest>,
@@ -282,10 +282,8 @@ export async function crawlController(
req.body.maxConcurrency !== undefined
? Math.min(
req.body.maxConcurrency,
- await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- ),
+ req.acuc?.concurrency_limit ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit,
)
: undefined,
zeroDataRetention,
diff --git a/apps/api/src/controllers/v1/queue-status.ts b/apps/api/src/controllers/v1/queue-status.ts
index a80b0b69c..f3f17a5c7 100644
--- a/apps/api/src/controllers/v1/queue-status.ts
+++ b/apps/api/src/controllers/v1/queue-status.ts
@@ -13,8 +13,8 @@ import {
cleanOldConcurrencyLimitEntries,
getConcurrencyLimitActiveJobsCount,
getConcurrencyQueueJobsCount,
- getEffectiveConcurrencyLimit,
} from "../../lib/concurrency-limit";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
type QueueStatusResponse = {
success: boolean;
@@ -77,10 +77,8 @@ export async function queueStatusController(
jobsInQueue: activeJobsOfTeam + queuedJobsOfTeam,
activeJobsInQueue: activeJobsOfTeam,
waitingJobsInQueue: queuedJobsOfTeam,
- maxConcurrency: await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- ),
+ maxConcurrency:
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit,
mostRecentSuccess: mostRecentSuccess
? new Date(mostRecentSuccess).toISOString()
diff --git a/apps/api/src/controllers/v1/scrape.ts b/apps/api/src/controllers/v1/scrape.ts
index fdbb51132..0f0dc794b 100644
--- a/apps/api/src/controllers/v1/scrape.ts
+++ b/apps/api/src/controllers/v1/scrape.ts
@@ -50,7 +50,7 @@ import {
import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
import { resolveThreatProtection } from "../../lib/threat-protection/request";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
export async function scrapeController(
req: RequestWithAuth<{}, ScrapeResponse, ScrapeRequest>,
@@ -256,16 +256,13 @@ async function scrapeControllerInner(
doc = await teamConcurrencySemaphore.withSemaphore(
req.auth.team_id,
jobId,
- await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- ),
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit,
aborter.signal,
timeout ?? 60_000,
async limited => {
const jobPriority = await getJobPriority({
team_id: req.auth.team_id,
- org_id: req.acuc?.org_id ?? null,
+ acuc: req.acuc,
basePriority: 10,
});
diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts
index 4a5297282..987bf3846 100644
--- a/apps/api/src/controllers/v1/types.ts
+++ b/apps/api/src/controllers/v1/types.ts
@@ -1337,6 +1337,12 @@ export type AuthCreditUsageChunk = {
// appended on JS-side
is_extract?: boolean;
+ // The team's effective Autumn limits and plan, always set by
+ // getACUC/getACUCTeam.
+ concurrency_limit: number;
+ rate_limit_multiplier: number;
+ is_paid_plan: boolean;
+
// Agent signup: populated when the key is agent-provisioned
_agentSponsor?: {
status: "pending" | "verified" | "blocked";
diff --git a/apps/api/src/controllers/v2/__tests__/browser-agent-concurrency.test.ts b/apps/api/src/controllers/v2/__tests__/browser-agent-concurrency.test.ts
index c3eba8b2d..1c234534b 100644
--- a/apps/api/src/controllers/v2/__tests__/browser-agent-concurrency.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/browser-agent-concurrency.test.ts
@@ -4,7 +4,7 @@ import { v4 as uuidv4 } from "uuid";
const SECRET = "agent-secret";
const mocks = vi.hoisted(() => ({
- teamConcurrency: null as number | null,
+ teamConcurrency: 2,
held: new Set<string>(),
reserveExternalSlot: vi.fn(),
}));
@@ -29,7 +29,6 @@ vi.mock("../../../lib/logger", () => {
vi.mock("../../../services/autumn/autumn.service", () => ({
autumnService: {
- getConcurrencyLimit: vi.fn(async () => mocks.teamConcurrency),
checkCredits: vi.fn(async () => null),
},
}));
@@ -145,7 +144,13 @@ function makeReq(interop: Interop) {
team_id: TEAM_ID,
agentInterop: agentInteropStatus({ body, headers }),
},
- acuc: { org_id: "org-1", api_key_id: null, flags: null },
+ acuc: {
+ org_id: "org-1",
+ api_key_id: null,
+ flags: null,
+ concurrency_limit: mocks.teamConcurrency,
+ rate_limit_multiplier: 1,
+ },
};
}
@@ -166,7 +171,7 @@ describe("browser create concurrency for trusted agent requests", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.held.clear();
- mocks.teamConcurrency = null;
+ mocks.teamConcurrency = FREE_LIMIT;
mocks.reserveExternalSlot.mockImplementation(
async (_team: string, id: string, _ttl: number, limit: number) => {
if (mocks.held.size >= limit) return false;
diff --git a/apps/api/src/controllers/v2/__tests__/crawl-prompt-zdr.test.ts b/apps/api/src/controllers/v2/__tests__/crawl-prompt-zdr.test.ts
index 33922f789..c5dfbb347 100644
--- a/apps/api/src/controllers/v2/__tests__/crawl-prompt-zdr.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/crawl-prompt-zdr.test.ts
@@ -45,9 +45,7 @@ vi.mock("../../../lib/request-credits-store", () => ({
requestCreditsShards: vi.fn().mockReturnValue(1),
}));
-vi.mock("../../../lib/concurrency-limit", () => ({
- getEffectiveConcurrencyLimit: vi.fn().mockResolvedValue(10),
-}));
+vi.mock("../../../lib/concurrency-limit", () => ({}));
vi.mock("../../../lib/crawl-redis", () => ({
crawlToCrawler: vi.fn(() => ({
diff --git a/apps/api/src/controllers/v2/__tests__/scrape-browser.test.ts b/apps/api/src/controllers/v2/__tests__/scrape-browser.test.ts
index 2d3fb26f6..fc76952fc 100644
--- a/apps/api/src/controllers/v2/__tests__/scrape-browser.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/scrape-browser.test.ts
@@ -122,7 +122,6 @@ vi.mock("../../../lib/browser-sessions", () => ({
}));
vi.mock("../../../lib/concurrency-limit", () => ({
- getEffectiveConcurrencyLimit: vi.fn(async () => 10),
getConcurrencyLimitActiveJobsCount: vi.fn(),
pushConcurrencyLimitActiveJob: vi.fn(() => Promise.resolve()),
removeConcurrencyLimitActiveJob: vi.fn(() => Promise.resolve()),
@@ -163,6 +162,7 @@ vi.mock("../../../services/logging/log_job", () => ({
}));
vi.mock("../../../services/autumn/autumn.service", () => ({
+ DEFAULT_TEAM_LIMITS: { concurrency_limit: 2, rate_limit_multiplier: 1 },
autumnService: {
checkCredits: vi.fn(async () => ({ allowed: true })),
},
diff --git a/apps/api/src/controllers/v2/batch-scrape.ts b/apps/api/src/controllers/v2/batch-scrape.ts
index 6e9cd196c..61c03a9bb 100644
--- a/apps/api/src/controllers/v2/batch-scrape.ts
+++ b/apps/api/src/controllers/v2/batch-scrape.ts
@@ -459,7 +459,7 @@ export async function batchScrapeController(
// set base to 21
jobPriority = await getJobPriority({
team_id: req.auth.team_id,
- org_id: req.acuc?.org_id ?? null,
+ acuc: req.acuc,
basePriority: 21,
});
}
diff --git a/apps/api/src/controllers/v2/concurrency-check.ts b/apps/api/src/controllers/v2/concurrency-check.ts
index 3c93ee090..ff40a9c55 100644
--- a/apps/api/src/controllers/v2/concurrency-check.ts
+++ b/apps/api/src/controllers/v2/concurrency-check.ts
@@ -5,7 +5,7 @@ import {
} from "./types";
import { Response } from "express";
import { getCombinedTeamActiveCount } from "../../services/worker/nuq-router";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
// Basically just middleware and error wrapping
export async function concurrencyCheckController(
@@ -24,9 +24,7 @@ export async function concurrencyCheckController(
return res.status(200).json({
success: true,
concurrency: activeJobsOfTeam,
- maxConcurrency: await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc.org_id,
- ),
+ maxConcurrency:
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit,
});
}
diff --git a/apps/api/src/controllers/v2/crawl.ts b/apps/api/src/controllers/v2/crawl.ts
index 34dbd6f68..0f24298c6 100644
--- a/apps/api/src/controllers/v2/crawl.ts
+++ b/apps/api/src/controllers/v2/crawl.ts
@@ -40,12 +40,12 @@ import { checkUrl } from "../../lib/threat-protection";
import { UnsafeDomainBlockedError } from "../../lib/threat-protection/error";
import { calculateThreatScanCredits } from "../../lib/scrape-billing";
import { billTeam } from "../../services/billing/credit_billing";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import { emitRejectedScrapeActivityEvent } from "../../lib/siem-logging";
import {
initializeRequestCredits,
requestCreditsShards,
} from "../../lib/request-credits-store";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
export async function crawlController(
req: RequestWithAuth<{}, CrawlResponse, CrawlRequest>,
@@ -327,10 +327,8 @@ export async function crawlController(
});
});
- const effectiveConcurrency = await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- );
+ const effectiveConcurrency =
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
const sc: StoredCrawl = {
originUrl: req.body.url,
crawlerOptions: toV0CrawlerOptions(finalCrawlerOptions),
diff --git a/apps/api/src/controllers/v2/parse.ts b/apps/api/src/controllers/v2/parse.ts
index 042646e04..2f5c47043 100644
--- a/apps/api/src/controllers/v2/parse.ts
+++ b/apps/api/src/controllers/v2/parse.ts
@@ -34,7 +34,6 @@ import {
} from "../../lib/keyless";
import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import path from "node:path";
import {
DOCUMENT_EXTENSIONS,
@@ -53,6 +52,7 @@ import {
PDF_CONTENT_TYPES,
PDF_EXTENSIONS,
} from "../../lib/parse-formats";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
const AGENT_INTEROP_CONCURRENCY_BOOST = 3;
@@ -488,10 +488,8 @@ export async function parseController(
}
req.on("close", () => aborter.abort());
- const baseConcurrency = await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- );
+ const baseConcurrency =
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
const concurrency = boostConcurrency
? baseConcurrency * AGENT_INTEROP_CONCURRENCY_BOOST
: baseConcurrency;
@@ -505,7 +503,7 @@ export async function parseController(
async limited => {
const jobPriority = await getJobPriority({
team_id: req.auth.team_id,
- org_id: req.acuc?.org_id ?? null,
+ acuc: req.acuc,
basePriority: 10,
});
diff --git a/apps/api/src/controllers/v2/queue-status.ts b/apps/api/src/controllers/v2/queue-status.ts
index 3cc6e892e..b3111c8f9 100644
--- a/apps/api/src/controllers/v2/queue-status.ts
+++ b/apps/api/src/controllers/v2/queue-status.ts
@@ -13,8 +13,8 @@ import {
cleanOldConcurrencyLimitEntries,
getConcurrencyLimitActiveJobsCount,
getConcurrencyQueueJobsCount,
- getEffectiveConcurrencyLimit,
} from "../../lib/concurrency-limit";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
type QueueStatusResponse = {
success: boolean;
@@ -77,10 +77,8 @@ export async function queueStatusController(
jobsInQueue: activeJobsOfTeam + queuedJobsOfTeam,
activeJobsInQueue: activeJobsOfTeam,
waitingJobsInQueue: queuedJobsOfTeam,
- maxConcurrency: await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- ),
+ maxConcurrency:
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit,
mostRecentSuccess: mostRecentSuccess
? new Date(mostRecentSuccess).toISOString()
diff --git a/apps/api/src/controllers/v2/scrape.ts b/apps/api/src/controllers/v2/scrape.ts
index 03cd81884..d8a854d1a 100644
--- a/apps/api/src/controllers/v2/scrape.ts
+++ b/apps/api/src/controllers/v2/scrape.ts
@@ -50,8 +50,8 @@ import { projectScrapeCredits } from "../../lib/keyless-credit-projection";
import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";
import { resolveThreatProtection } from "../../lib/threat-protection/request";
import { emitRejectedScrapeActivityEvent } from "../../lib/siem-logging";
-import { getEffectiveConcurrencyLimit } from "../../lib/concurrency-limit";
import { isAgentInteropSecretValid } from "../../lib/agent-interop";
+import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";
const AGENT_INTEROP_CONCURRENCY_BOOST = 3;
@@ -351,10 +351,8 @@ export async function scrapeController(
}
req.on("close", () => aborter.abort());
- const baseConcurrency = await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- );
+ const baseConcurrency =
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
const concurrency = boostConcurrency
? baseConcurrency * AGENT_INTEROP_CONCURRENCY_BOOST
: baseConcurrency;
@@ -368,7 +366,7 @@ export async function scrapeController(
async limited => {
const jobPriority = await getJobPriority({
team_id: req.auth.team_id,
- org_id: req.acuc?.org_id ?? null,
+ acuc: req.acuc,
basePriority: 10,
});
diff --git a/apps/api/src/lib/__tests__/job-priority.test.ts b/apps/api/src/lib/__tests__/job-priority.test.ts
index f511d48c8..7b69fbcc2 100644
--- a/apps/api/src/lib/__tests__/job-priority.test.ts
+++ b/apps/api/src/lib/__tests__/job-priority.test.ts
@@ -1,11 +1,8 @@
import type { Mock } from "vitest";
import { vi } from "vitest";
-// Hermetic: job-priority talks to services/redis (not queue-service) and to
-// Autumn, so both are stubbed here. controllers/auth is stubbed too and
-// asserted never to be called — the org is the caller's to pass, because
-// getJobPriority runs once per discovered link inside a crawl and must not
-// turn into a Redis GET per link.
+// Hermetic: job-priority talks to services/redis (not queue-service) and reads
+// the team's limits off its ACUC, so both are stubbed here.
vi.mock("../../services/redis", () => ({
redisEvictConnection: {
sadd: vi.fn(),
@@ -15,27 +12,29 @@ vi.mock("../../services/redis", () => ({
},
}));
-vi.mock("../../services/autumn/autumn.service", () => ({
- autumnService: {
- getRateLimitMultiplier: vi.fn(),
- },
-}));
-
vi.mock("../../controllers/auth", () => ({
getACUCTeam: vi.fn(),
}));
+vi.mock("../../services/autumn/autumn.service", () => ({
+ DEFAULT_TEAM_LIMITS: { concurrency_limit: 2, rate_limit_multiplier: 1 },
+}));
+
import {
getJobPriority,
addJobPriority,
deleteJobPriority,
} from "../job-priority";
import { redisEvictConnection } from "../../services/redis";
-import { autumnService } from "../../services/autumn/autumn.service";
import { getACUCTeam } from "../../controllers/auth";
import {} from "../../types";
-const getRateLimitMultiplier = autumnService.getRateLimitMultiplier as Mock;
+function mockMultiplier(rate_limit_multiplier: number) {
+ vi.mocked(getACUCTeam).mockResolvedValue({
+ team_id: "team1",
+ rate_limit_multiplier,
+ } as never);
+}
// Multipliers that land on the plan tiers the priority cases below assume.
const STANDARD_MULTIPLIER = 50;
@@ -43,7 +42,7 @@ const HOBBY_MULTIPLIER = 10;
const FREE_MULTIPLIER = 1;
beforeEach(() => {
- getRateLimitMultiplier.mockResolvedValue(FREE_MULTIPLIER);
+ mockMultiplier(FREE_MULTIPLIER);
});
describe("Job Priority Tests", () => {
@@ -77,36 +76,36 @@ describe("Job Priority Tests", () => {
test("getJobPriority should return correct priority based on plan and set length", async () => {
const team_id = "team1";
- getRateLimitMultiplier.mockResolvedValue(STANDARD_MULTIPLIER);
+ mockMultiplier(STANDARD_MULTIPLIER);
(redisEvictConnection.scard as Mock).mockResolvedValue(150);
- const priority = await getJobPriority({ team_id, org_id: null });
+ const priority = await getJobPriority({ team_id });
expect(priority).toBe(10);
(redisEvictConnection.scard as Mock).mockResolvedValue(250);
- const priorityExceeded = await getJobPriority({ team_id, org_id: null });
+ const priorityExceeded = await getJobPriority({ team_id });
expect(priorityExceeded).toBe(20); // basePriority + Math.ceil((250 - 200) * 0.2)
});
test("getJobPriority should handle different plans correctly", async () => {
const team_id = "team1";
- getRateLimitMultiplier.mockResolvedValue(HOBBY_MULTIPLIER);
+ mockMultiplier(HOBBY_MULTIPLIER);
(redisEvictConnection.scard as Mock).mockResolvedValue(50);
- let priority = await getJobPriority({ team_id, org_id: null });
+ let priority = await getJobPriority({ team_id });
expect(priority).toBe(10);
(redisEvictConnection.scard as Mock).mockResolvedValue(150);
- priority = await getJobPriority({ team_id, org_id: null });
+ priority = await getJobPriority({ team_id });
expect(priority).toBe(25); // basePriority + Math.ceil((150 - 100) * 0.3)
- getRateLimitMultiplier.mockResolvedValue(FREE_MULTIPLIER);
+ mockMultiplier(FREE_MULTIPLIER);
(redisEvictConnection.scard as Mock).mockResolvedValue(25);
- priority = await getJobPriority({ team_id, org_id: null });
+ priority = await getJobPriority({ team_id });
expect(priority).toBe(10);
(redisEvictConnection.scard as Mock).mockResolvedValue(60);
- priority = await getJobPriority({ team_id, org_id: null });
+ priority = await getJobPriority({ team_id });
expect(priority).toBe(28); // basePriority + Math.ceil((60 - 25) * 0.5)
});
@@ -165,25 +164,24 @@ describe("Job Priority Tests", () => {
});
// ---------------------------------------------------------------------------
-// Where the org comes from: the caller, and nowhere else.
+// Where the multiplier comes from: the caller's ACUC when it holds one.
// ---------------------------------------------------------------------------
-describe("the org the caller supplies", () => {
- it("goes straight to the rate-limit multiplier, with no ACUC lookup", async () => {
- (redisEvictConnection.scard as Mock).mockResolvedValue(1);
-
- await getJobPriority({ team_id: "team1", org_id: "org-1" });
+describe("the ACUC the caller supplies", () => {
+ it("reads the multiplier off the ACUC the caller holds", async () => {
+ // 2500 lands on the top tier: no penalty at this set size.
+ (redisEvictConnection.scard as Mock).mockResolvedValue(250);
+ const acuc = { team_id: "team1", rate_limit_multiplier: 2500 } as never;
- expect(getRateLimitMultiplier).toHaveBeenCalledWith("team1", "org-1");
+ expect(await getJobPriority({ team_id: "team1", acuc })).toBe(10);
expect(getACUCTeam).not.toHaveBeenCalled();
});
- it("passes a null org through as the caller gave it", async () => {
+ it("reads the team's ACUC when the caller holds none", async () => {
(redisEvictConnection.scard as Mock).mockResolvedValue(1);
- await getJobPriority({ team_id: "team1", org_id: null });
+ await getJobPriority({ team_id: "team1" });
- expect(getRateLimitMultiplier).toHaveBeenCalledWith("team1", null);
- expect(getACUCTeam).not.toHaveBeenCalled();
+ expect(getACUCTeam).toHaveBeenCalledWith("team1");
});
});
diff --git a/apps/api/src/lib/browser-lifecycle.ts b/apps/api/src/lib/browser-lifecycle.ts
index 26cd1efe1..651bd07b6 100644
--- a/apps/api/src/lib/browser-lifecycle.ts
+++ b/apps/api/src/lib/browser-lifecycle.ts
@@ -26,15 +26,15 @@ import {
BROWSER_CREDITS_PER_HOUR,
INTERACT_CREDITS_PER_HOUR,
} from "./browser-billing";
-import {
- getEffectiveConcurrencyLimit,
- HOBBY_CONCURRENCY_LIMIT,
-} from "./concurrency-limit";
+import { HOBBY_CONCURRENCY_LIMIT } from "./concurrency-limit";
import {
reserveExternalSlot,
mirrorExternalSlotRelease,
} from "../services/worker/nuq-router";
-import { autumnService } from "../services/autumn/autumn.service";
+import {
+ autumnService,
+ DEFAULT_TEAM_LIMITS,
+} from "../services/autumn/autumn.service";
import { billTeam } from "../services/billing/credit_billing";
import { orgIdForTeam } from "./team-org";
import { logRequest } from "../services/logging/log_job";
@@ -161,10 +161,8 @@ async function createBrowserSessionInternal(
const estimatedCredits = shouldBill
? calculateBrowserSessionCredits(options.ttl * 1000)
: 0;
- const teamLimit = await getEffectiveConcurrencyLimit(
- req.auth.team_id,
- req.acuc?.org_id ?? null,
- );
+ const teamLimit =
+ req.acuc?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
// An agent run opens browsers against the team's own slots, so a free team
// (2) is throttled by its own agent. Floor trusted agent traffic at hobby,
// as the rate limiter does; plans at or above hobby are unchanged.
diff --git a/apps/api/src/lib/concurrency-limit.ts b/apps/api/src/lib/concurrency-limit.ts
index 92b7642ec..cd7a1f9b8 100644
--- a/apps/api/src/lib/concurrency-limit.ts
+++ b/apps/api/src/lib/concurrency-limit.ts
@@ -18,13 +18,10 @@ import {
pushConcurrencyLimitActiveJob,
removeConcurrencyLimitActiveJob,
} from "./concurrency-redis";
-import { autumnService } from "../services/autumn/autumn.service";
-import { orgIdForTeam } from "./team-org";
+import { getACUCTeam } from "../controllers/auth";
+import { DEFAULT_TEAM_LIMITS } from "../services/autumn/autumn.service";
import { reportPipelineError } from "./redis-pipeline";
-// Fallback when Autumn can't give us a concurrency value.
-const DEFAULT_CONCURRENCY_LIMIT = 2;
-
/**
* CONCURRENCY granted by the Autumn `hobby` plan (firecrawl-web
* autumn.config.ts). Pairs with HOBBY_RATE_LIMIT_MULTIPLIER in
@@ -32,23 +29,6 @@ const DEFAULT_CONCURRENCY_LIMIT = 2;
*/
export const HOBBY_CONCURRENCY_LIMIT = 5;
-/**
- * Returns the team's effective concurrency limit from Autumn's CONCURRENCY
- * balance. Autumn is authoritative; when the entity is missing we fall back to
- * the low default of 2. When Autumn errors, getConcurrencyLimit already returns
- * a high fail-open value, so that carries through here.
- */
-export async function getEffectiveConcurrencyLimit(
- teamId: string,
- /** The team's org, from the ACUC the caller already holds. Required so a
- * caller cannot silently omit it and take the high fail-open limit; pass
- * null only when the team genuinely has no org. */
- orgId: string | null,
-): Promise<number> {
- const autumnValue = await autumnService.getConcurrencyLimit(teamId, orgId);
- return autumnValue ?? DEFAULT_CONCURRENCY_LIMIT;
-}
-
const constructKey = constructConcurrencyLimitKey;
const constructQueueKey = (team_id: string) =>
"concurrency-limit-queue:" + team_id;
@@ -349,13 +329,10 @@ export async function concurrentJobDone(job: NuQJob<any>) {
await cleanOldCrawlConcurrencyLimitEntries(job.data.crawl_id);
}
- // The org rides the job payload; the ACUC answers for a job enqueued
- // without one (monitor jobs null the field deliberately). Once per call,
- // not once per job promoted below.
- const maxTeamConcurrency = await getEffectiveConcurrencyLimit(
- job.data.team_id,
- job.data.internalOptions?.orgId ?? (await orgIdForTeam(job.data.team_id)),
- );
+ // Once per call, not once per job promoted below.
+ const maxTeamConcurrency =
+ (await getACUCTeam(job.data.team_id).catch(() => null))
+ ?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
let staleSkipped = 0;
while (staleSkipped < 100) {
diff --git a/apps/api/src/lib/concurrency-queue-reconciler.ts b/apps/api/src/lib/concurrency-queue-reconciler.ts
index e3f913698..9b48c214b 100644
--- a/apps/api/src/lib/concurrency-queue-reconciler.ts
+++ b/apps/api/src/lib/concurrency-queue-reconciler.ts
@@ -5,7 +5,6 @@ import { scrapeQueue, type NuQJob } from "../services/worker/nuq";
import { type ScrapeJobData } from "../types";
import {
getConcurrencyLimitActiveJobs,
- getEffectiveConcurrencyLimit,
getNextConcurrentJob,
MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
@@ -16,7 +15,8 @@ import {
} from "./concurrency-limit";
import { getCrawl } from "./crawl-redis";
import { logger as _logger } from "./logger";
-import { orgIdForTeam } from "./team-org";
+import { getACUCTeam } from "../controllers/auth";
+import { DEFAULT_TEAM_LIMITS } from "../services/autumn/autumn.service";
interface ReconcileOptions {
teamId?: string;
@@ -83,7 +83,6 @@ async function getQueuedJobIDs(teamId: string): Promise<Set<string>> {
async function reconcileTeam(
ownerId: string,
- orgId: string | null,
teamLogger: Logger,
): Promise<{ jobsStarted: number; jobsRequeued: number } | null> {
const backloggedJobIDs = new Set(
@@ -113,7 +112,9 @@ async function reconcileTeam(
// TODO: gate crawl + extract against one combined pool (sum of both active
// counts vs the single limit) instead of applying the same limit to each
// type independently.
- const teamConcurrency = await getEffectiveConcurrencyLimit(ownerId, orgId);
+ const teamConcurrency =
+ (await getACUCTeam(ownerId))?.concurrency_limit ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit;
const maxCrawlConcurrency = teamConcurrency;
const maxExtractConcurrency = teamConcurrency;
@@ -203,7 +204,6 @@ async function reconcileTeam(
async function drainQueue(
ownerId: string,
- orgId: string | null,
teamLogger: Logger,
): Promise<{ jobsPromoted: number; staleSkipped: number }> {
// Autumn's CONCURRENCY balance is a single per-team pool, so crawl and
@@ -211,7 +211,9 @@ async function drainQueue(
// TODO: gate crawl + extract against one combined pool (sum of both active
// counts vs the single limit) instead of applying the same limit to each
// type independently.
- const teamConcurrency = await getEffectiveConcurrencyLimit(ownerId, orgId);
+ const teamConcurrency =
+ (await getACUCTeam(ownerId))?.concurrency_limit ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit;
const maxCrawlConcurrency = teamConcurrency;
const maxExtractConcurrency = teamConcurrency;
@@ -338,19 +340,14 @@ export async function reconcileConcurrencyQueue(
const teamLogger = logger.child({ teamId: ownerId });
try {
- // The reconciler has no request ACUC and no job payload to read, so the
- // team's ACUC is the only org source; once per team per pass, shared by
- // both limit lookups below.
- const orgId = await orgIdForTeam(ownerId);
-
- const teamResult = await reconcileTeam(ownerId, orgId, teamLogger);
+ const teamResult = await reconcileTeam(ownerId, teamLogger);
if (teamResult !== null) {
result.teamsWithDrift++;
result.jobsStarted += teamResult.jobsStarted;
result.jobsRequeued += teamResult.jobsRequeued;
}
- const drainResult = await drainQueue(ownerId, orgId, teamLogger);
+ const drainResult = await drainQueue(ownerId, teamLogger);
if (drainResult.jobsPromoted > 0 || drainResult.staleSkipped > 0) {
result.jobsStarted += drainResult.jobsPromoted;
teamLogger.info("Queue drain promoted jobs", {
diff --git a/apps/api/src/lib/extract/document-scraper.ts b/apps/api/src/lib/extract/document-scraper.ts
index c015d2346..32d61983c 100644
--- a/apps/api/src/lib/extract/document-scraper.ts
+++ b/apps/api/src/lib/extract/document-scraper.ts
@@ -52,7 +52,6 @@ export async function scrapeDocument(
const jobId = uuidv7();
const jobPriority = await getJobPriority({
team_id: options.teamId,
- org_id: options.orgId ?? null,
basePriority: 10,
});
diff --git a/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts b/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
index 4c78b50c4..40ce84409 100644
--- a/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
+++ b/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
@@ -54,7 +54,6 @@ export async function scrapeDocument_F0(
const jobId = uuidv7();
const jobPriority = await getJobPriority({
team_id: options.teamId,
- org_id: options.orgId ?? null,
basePriority: 10,
});
diff --git a/apps/api/src/lib/job-priority.ts b/apps/api/src/lib/job-priority.ts
index 187ff6859..10e010f59 100644
--- a/apps/api/src/lib/job-priority.ts
+++ b/apps/api/src/lib/job-priority.ts
@@ -1,6 +1,8 @@
import { redisEvictConnection } from "../services/redis";
import { logger } from "./logger";
-import { autumnService } from "../services/autumn/autumn.service";
+import { getACUCTeam } from "../controllers/auth";
+import { DEFAULT_TEAM_LIMITS } from "../services/autumn/autumn.service";
+import type { AuthCreditUsageChunkFromTeam } from "../controllers/v1/types";
import { inferPlanPriorityFromMultiplier } from "../services/rate-limiter";
const SET_KEY_PREFIX = "limit_team_id:";
@@ -31,14 +33,13 @@ export async function deleteJobPriority(team_id, job_id) {
export async function getJobPriority({
team_id,
- org_id,
+ acuc,
basePriority = 10,
}: {
team_id: string;
- /** The team's org, from the ACUC the caller already holds. Required so a
- * caller cannot silently omit it and fall back to the high fail-open
- * limits; pass null only when the caller genuinely has no org. */
- org_id: string | null;
+ /** The team's ACUC, when the caller holds one. Pass it on hot loops, which
+ * would otherwise read the team's ACUC once per call. */
+ acuc?: AuthCreditUsageChunkFromTeam | null;
basePriority?: number;
from_extract?: boolean;
}): Promise<number> {
@@ -53,12 +54,9 @@ export async function getJobPriority({
const setLength = await redisEvictConnection.scard(setKey);
// Plan priority is inferred from the team's Autumn rate-limit multiplier.
- // The org is threaded in by the caller: this runs once per discovered link
- // inside a crawl, so it must not do an ACUC lookup of its own.
- const multiplier = await autumnService.getRateLimitMultiplier(
- team_id,
- org_id,
- );
+ const multiplier =
+ (acuc ?? (await getACUCTeam(team_id)))?.rate_limit_multiplier ??
+ DEFAULT_TEAM_LIMITS.rate_limit_multiplier;
const { bucketLimit, planModifier } =
inferPlanPriorityFromMultiplier(multiplier);
diff --git a/apps/api/src/search/scrape.ts b/apps/api/src/search/scrape.ts
index acd89fafc..2fbd45245 100644
--- a/apps/api/src/search/scrape.ts
+++ b/apps/api/src/search/scrape.ts
@@ -222,7 +222,6 @@ export async function scrapeSearchResults(
const jobPriority = await getJobPriority({
team_id: options.teamId,
- org_id: options.orgId ?? null,
basePriority: 10,
});
diff --git a/apps/api/src/services/alexandria/access.test.ts b/apps/api/src/services/alexandria/access.test.ts
index 4fa79ca59..fc80fbe59 100644
--- a/apps/api/src/services/alexandria/access.test.ts
+++ b/apps/api/src/services/alexandria/access.test.ts
@@ -1,11 +1,9 @@
-const mocks = vi.hoisted(() => ({ request: vi.fn(), multiplier: vi.fn() }));
+const mocks = vi.hoisted(() => ({ request: vi.fn(), team: vi.fn() }));
vi.mock("../../config", () => ({
config: { USE_DB_AUTHENTICATION: true, FIRECRAWL_DASHBOARD_URL: "https://d" },
}));
vi.mock("./client", () => ({ exchangeRequest: mocks.request }));
-vi.mock("../autumn/autumn.service", () => ({
- autumnService: { getKnownRateLimitMultiplier: mocks.multiplier },
-}));
+vi.mock("../../controllers/auth", () => ({ getACUCTeam: mocks.team }));
import { authorizeProviders } from "./access";
const calls = [
@@ -357,7 +355,7 @@ describe("paid-plan-only capabilities", () => {
mocks.request.mockResolvedValue(
requirements(["news/wiims", "news/search"]),
);
- mocks.multiplier.mockResolvedValue(1);
+ mocks.team.mockResolvedValue({ is_paid_plan: false });
const denied = await authorizeProviders("team", wiim, enabled, "org");
expect(denied?.status).toBe(403);
expect(denied?.body).toEqual(
@@ -366,28 +364,37 @@ describe("paid-plan-only capabilities", () => {
expect(String((denied?.body as { error: string }).error)).toContain(
"benzinga/news/wiims",
);
- expect(mocks.multiplier).toHaveBeenCalledWith("team", "org");
+ expect(mocks.team).toHaveBeenCalledWith("team");
// One requirements read, and nothing else: no quote, no execution.
expect(mocks.request).toHaveBeenCalledTimes(1);
});
it("admits a paid-plan team, and does not consult the plan for an ungated capability", async () => {
mocks.request.mockResolvedValue(requirements(["news/wiims"]));
- mocks.multiplier.mockResolvedValue(10);
+ mocks.team.mockResolvedValue({ is_paid_plan: true });
expect(
await authorizeProviders("team", wiim, enabled, "org"),
).toBeUndefined();
- mocks.multiplier.mockClear();
- mocks.multiplier.mockResolvedValue(1);
+ mocks.team.mockClear();
+ mocks.team.mockResolvedValue({ is_paid_plan: false });
expect(
await authorizeProviders("team", pressReleases, enabled, "org"),
).toBeUndefined();
- expect(mocks.multiplier).not.toHaveBeenCalled();
+ expect(mocks.team).not.toHaveBeenCalled();
+ });
+
+ it("refuses a team with no ACUC", async () => {
+ mocks.request.mockResolvedValue(requirements(["news/wiims"]));
+ mocks.team.mockResolvedValue(null);
+ const denied = await authorizeProviders("team", wiim, enabled, "org");
+ expect(denied?.body).toEqual(
+ expect.objectContaining({ code: "paid_plan_required" }),
+ );
});
it("lets a team that bypasses credit checks through, and treats a missing field as ungated", async () => {
mocks.request.mockResolvedValue(requirements(["news/wiims"]));
- mocks.multiplier.mockResolvedValue(1);
+ mocks.team.mockResolvedValue({ is_paid_plan: false });
expect(
await authorizeProviders(
"team",
@@ -405,7 +412,7 @@ describe("paid-plan-only capabilities", () => {
it("still requires the agreement first: an unaccepted paid team is refused on terms, not plan", async () => {
mocks.request.mockResolvedValue(requirements(["news/wiims"]));
- mocks.multiplier.mockResolvedValue(10);
+ mocks.team.mockResolvedValue({ is_paid_plan: true });
const denied = await authorizeProviders("team", wiim, {}, null);
expect(denied?.status).toBe(403);
expect(denied?.body).not.toEqual(
@@ -413,41 +420,3 @@ describe("paid-plan-only capabilities", () => {
);
});
});
-
-it("fails closed when the plan cannot be known: no org, a preview team or an Autumn error", async () => {
- mocks.request.mockResolvedValue({
- status: 200,
- body: {
- providers: [
- {
- provider: "benzinga",
- required: true,
- terms: { key: "benzinga", version: "C-1" },
- paidPlanOnlyCapabilities: ["news/wiims"],
- },
- ],
- },
- });
- const enabled = {
- organizationDataSourceAccess: {
- benzinga: {
- status: "enabled",
- termsKey: "benzinga",
- termsVersion: "C-1",
- },
- },
- };
- const wiim = [
- { provider: "benzinga", capability: "news/wiims", options: {} },
- ];
- // The service answers null for every "cannot know" case; the gate never
- // treats that as paid.
- mocks.multiplier.mockResolvedValue(null);
- const denied = await authorizeProviders("team", wiim, enabled, null);
- expect(denied?.status).toBe(503);
- expect(denied?.body).toEqual(
- expect.objectContaining({ code: "plan_verification_unavailable" }),
- );
- expect(mocks.multiplier).toHaveBeenCalledWith("team", null);
- expect(mocks.request).toHaveBeenCalledTimes(1);
-});
diff --git a/apps/api/src/services/alexandria/access.ts b/apps/api/src/services/alexandria/access.ts
index ec317d82a..5491e4ba2 100644
--- a/apps/api/src/services/alexandria/access.ts
+++ b/apps/api/src/services/alexandria/access.ts
@@ -10,8 +10,7 @@ import {
matchesAcceptance,
type LedgerAcceptance,
} from "./terms";
-import { autumnService } from "../autumn/autumn.service";
-import { HOBBY_RATE_LIMIT_MULTIPLIER } from "../rate-limiter";
+import { getACUCTeam } from "../../controllers/auth";
const requirementsSchema = z.object({
providers: z.array(
@@ -121,30 +120,10 @@ export async function authorizeProviders(
const gated = calls.filter(call =>
paidPlanOnly.has(`${call.provider}/${call.capability}`),
);
- // A licence that permits the payload only in response to a paid request
- // (Benzinga Schedule C.4: full text, WIIM, analyst ratings). Credits alone
- // do not prove payment, because a free team spends signup credits; the plan
- // does. Autumn's rate-limit multiplier is 1 on the free plan and at least the
- // hobby floor on every paid one. It comes from the entity read the rate
- // limiter caches per team, so a warm cache costs nothing and a cold one
- // costs the fetch the limiter would have made anyway. This gate fails
- // closed: a team whose plan cannot be known (no org to bill, a preview team,
- // an Autumn error) is refused, where the rate limiter would fail open, since
- // delivering licensed content to a possibly free team is the mistake the
- // licence forbids. Internal teams that bypass credit checks are not
- // customers and pass.
+ // Licensed payloads that may only answer a paid request: refuse teams that
+ // aren't on a paid plan. Credit-check bypass teams pass.
if (gated.length > 0 && flags?.bypassCreditChecks !== true) {
- const multiplier = await autumnService.getKnownRateLimitMultiplier(
- teamId,
- orgId,
- );
- if (multiplier === null)
- return refusal(
- 503,
- "Your plan could not be verified for a paid-plan-only capability. No provider was executed.",
- { code: "plan_verification_unavailable" },
- );
- if (multiplier < HOBBY_RATE_LIMIT_MULTIPLIER) {
+ if (!(await getACUCTeam(teamId))?.is_paid_plan) {
const addresses = [
...new Set(gated.map(call => `${call.provider}/${call.capability}`)),
].join(", ");
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 327d7b9fa..b1a171c04 100644
--- a/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
+++ b/apps/api/src/services/autumn/__tests__/autumn.service.test.ts
@@ -21,6 +21,7 @@ const {
mockCheck,
mockFinalize,
mockGetOrCreate,
+ mockCustomerGet,
mockEntityGet,
mockEntityCreate,
mockAutumnClient,
@@ -39,11 +40,12 @@ const {
const mockGetOrCreate = vi
.fn<(args: any) => Promise<unknown>>()
.mockResolvedValue({ id: "org-1" });
+ const mockCustomerGet = vi.fn<(args: any) => Promise<unknown>>();
const mockEntityGet = vi.fn<(args: any) => Promise<unknown>>();
const mockEntityCreate = vi.fn<(args: any) => Promise<unknown>>();
const mockAutumnClient = {
- customers: { getOrCreate: mockGetOrCreate },
+ customers: { getOrCreate: mockGetOrCreate, get: mockCustomerGet },
entities: { get: mockEntityGet, create: mockEntityCreate },
balances: { finalize: mockFinalize },
check: mockCheck,
@@ -72,6 +74,7 @@ const {
mockCheck,
mockFinalize,
mockGetOrCreate,
+ mockCustomerGet,
mockEntityGet,
mockEntityCreate,
mockAutumnClient,
@@ -150,6 +153,7 @@ beforeEach(() => {
mockFinalize.mockResolvedValue(undefined);
mockEntityGet.mockResolvedValue(makeEntity(0));
mockEntityCreate.mockResolvedValue({ id: "team-1" });
+ mockCustomerGet.mockResolvedValue({ subscriptions: [] });
});
// ---------------------------------------------------------------------------
@@ -747,66 +751,165 @@ describe("featureIdForBillingEndpoint", () => {
});
// ---------------------------------------------------------------------------
-// getEntityLimits fallback behavior (concurrency + rate-limit multiplier)
+// getTeamLimits (concurrency + rate-limit multiplier)
// ---------------------------------------------------------------------------
-describe("entity limits fallback", () => {
+describe("getTeamLimits", () => {
+ const lowDefaults = {
+ concurrency_limit: 2,
+ rate_limit_multiplier: 1,
+ is_paid_plan: false,
+ };
+ const subscription = (planId: string, overrides: object = {}) => ({
+ planId,
+ status: "active",
+ addOn: false,
+ ...overrides,
+ });
+
it("returns the Autumn values on the happy path", async () => {
- const svc = makeService();
mockEntityGet.mockResolvedValue({
balances: {
CONCURRENCY: { remaining: 7 },
rate_limits: { granted: 25 },
},
});
+ mockCustomerGet.mockResolvedValue({
+ subscriptions: [subscription("hobby")],
+ });
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBe(7);
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(25);
+ expect(await makeService().getTeamLimits("team-1", "org-1")).toEqual({
+ concurrency_limit: 7,
+ rate_limit_multiplier: 25,
+ is_paid_plan: true,
+ });
+ expect(mockEntityGet).toHaveBeenCalledWith({
+ customerId: "org-1",
+ entityId: "team-1",
+ });
+ expect(mockCustomerGet).toHaveBeenCalledWith({ customerId: "org-1" });
});
- it("falls back LOW (null / multiplier 1) when the entity is missing (404)", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
+ it("starts the customer read without waiting for the entity read", async () => {
+ let resolveEntity: (entity: unknown) => void = () => {};
+ mockEntityGet.mockReturnValue(
+ new Promise(resolve => {
+ resolveEntity = resolve;
+ }),
+ );
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBeNull();
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(1);
+ const pending = makeService().getTeamLimits("team-1", "org-1");
+ expect(mockCustomerGet).toHaveBeenCalledTimes(1);
+ resolveEntity({ balances: {} });
+ expect(await pending).toEqual(lowDefaults);
});
- it("falls back LOW when the entity exists but the balances are absent", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue({ balances: {} });
+ it.each([
+ ["a priced plan", [subscription("standard")], true],
+ ["a legacy plan", [subscription("legacy_standard")], true],
+ ["an enterprise plan", [subscription("enterprise_10m")], true],
+ ["the free plan", [subscription("free")], false],
+ ["the free-boost plan", [subscription("free_1_5k")], false],
+ [
+ "free plus the pay-as-you-go add-on",
+ [subscription("free"), subscription("pay_as_you_go", { addOn: true })],
+ false,
+ ],
+ [
+ "only a paid add-on",
+ [subscription("credit_pack_1k", { addOn: true })],
+ false,
+ ],
+ [
+ "free with a paid plan only scheduled",
+ [subscription("free"), subscription("hobby", { status: "scheduled" })],
+ false,
+ ],
+ ["no subscriptions", [], false],
+ ])("is_paid_plan for %s is %s", async (_case, subscriptions, expected) => {
+ mockCustomerGet.mockResolvedValue({ subscriptions });
+
+ const limits = await makeService().getTeamLimits("team-1", "org-1");
+
+ expect(limits.is_paid_plan).toBe(expected);
+ });
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBeNull();
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(1);
+ it.each([
+ [
+ "the entity is missing (404)",
+ () => mockEntityGet.mockRejectedValue({ statusCode: 404 }),
+ ],
+ [
+ "the balances are absent",
+ () => mockEntityGet.mockResolvedValue({ balances: {} }),
+ ],
+ ])("resolves to the low defaults when %s", async (_case, arrange) => {
+ arrange();
+
+ expect(await makeService().getTeamLimits("team-1", "org-1")).toEqual(
+ lowDefaults,
+ );
});
- it("fails OPEN with high limits when Autumn errors (not a 404)", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 500 });
+ it("keeps the entity's limits but reports no paid plan when the customer is missing (404)", async () => {
+ mockEntityGet.mockResolvedValue({
+ balances: {
+ CONCURRENCY: { remaining: 7 },
+ rate_limits: { granted: 25 },
+ },
+ });
+ mockCustomerGet.mockRejectedValue({ statusCode: 404 });
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBe(200);
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(2500);
+ expect(await makeService().getTeamLimits("team-1", "org-1")).toEqual({
+ concurrency_limit: 7,
+ rate_limit_multiplier: 25,
+ is_paid_plan: false,
+ });
});
- it("fails OPEN when Autumn throws a non-HTTP error", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue(new Error("ECONNREFUSED"));
+ it.each([
+ ["the entity read errors (not a 404)", mockEntityGet, { statusCode: 500 }],
+ [
+ "the entity read throws a non-HTTP error",
+ mockEntityGet,
+ new Error("ECONNREFUSED"),
+ ],
+ [
+ "the customer read errors (not a 404)",
+ mockCustomerGet,
+ { statusCode: 500 },
+ ],
+ ])("throws when %s", async (_case, read, error) => {
+ read.mockRejectedValue(error);
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBe(200);
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(2500);
+ await expect(
+ makeService().getTeamLimits("team-1", "org-1"),
+ ).rejects.toBeDefined();
});
- it("does NOT cache the error fail-open value — retries Autumn next call", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValueOnce({ statusCode: 500 });
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBe(200);
+ it("throws without asking Autumn when there is no org", async () => {
+ await expect(makeService().getTeamLimits("team-1", null)).rejects.toThrow();
+ expect(mockEntityGet).not.toHaveBeenCalled();
+ expect(mockCustomerGet).not.toHaveBeenCalled();
+ });
- // Autumn recovers on the next call; the earlier error must not be pinned.
+ it("reads Autumn on every call, keeping no cache of its own", async () => {
+ const svc = makeService();
mockEntityGet.mockResolvedValue({
balances: { CONCURRENCY: { remaining: 3 }, rate_limits: { granted: 10 } },
});
- expect(await svc.getConcurrencyLimit("team-1", "org-1")).toBe(3);
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(10);
+ await svc.getTeamLimits("team-1", "org-1");
+ await svc.getTeamLimits("team-1", "org-1");
+ expect(mockEntityGet).toHaveBeenCalledTimes(2);
+ expect(mockCustomerGet).toHaveBeenCalledTimes(2);
+ });
+
+ it("answers the low defaults for a preview team without asking Autumn", async () => {
+ expect(await makeService().getTeamLimits("preview_x", "org-1")).toEqual(
+ lowDefaults,
+ );
+ expect(mockEntityGet).not.toHaveBeenCalled();
+ expect(mockCustomerGet).not.toHaveBeenCalled();
});
});
@@ -2293,62 +2396,3 @@ describe("provisioning by route", () => {
},
);
});
-
-describe("getKnownRateLimitMultiplier (fail-closed plan reads)", () => {
- it("isolates cached entitlements by organization while sharing reads within an organization", async () => {
- const svc = makeService();
- mockEntityGet
- .mockResolvedValueOnce({ balances: { rate_limits: { granted: 25 } } })
- .mockResolvedValueOnce({ balances: { rate_limits: { granted: 1 } } });
- expect(await svc.getRateLimitMultiplier("team-1", "org-a")).toBe(25);
- expect(await svc.getKnownRateLimitMultiplier("team-1", "org-b")).toBe(1);
- expect(await svc.getKnownRateLimitMultiplier("team-1", "org-a")).toBe(25);
- expect(await svc.getRateLimitMultiplier("team-1", "org-b")).toBe(1);
- expect(mockEntityGet).toHaveBeenCalledTimes(2);
- expect(mockEntityGet).toHaveBeenNthCalledWith(1, { customerId: "org-a", entityId: "team-1" });
- expect(mockEntityGet).toHaveBeenNthCalledWith(2, { customerId: "org-b", entityId: "team-1" });
- });
-
- it("refuses a missing organization even after caching a paid entitlement", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue({ balances: { rate_limits: { granted: 25 } } });
- expect(await svc.getKnownRateLimitMultiplier("team-1", "org-1")).toBe(25);
- expect(await svc.getKnownRateLimitMultiplier("team-1", null)).toBeNull();
- expect(mockEntityGet).toHaveBeenCalledTimes(1);
- });
-
- it("answers the entitled multiplier, and 1 when the entity is missing or has no balance", async () => {
- const svc = makeService();
- mockEntityGet.mockResolvedValue({
- balances: { rate_limits: { granted: 25 } },
- });
- expect(await svc.getKnownRateLimitMultiplier("team-1", "org-1")).toBe(25);
-
- const missing = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 404 });
- expect(await missing.getKnownRateLimitMultiplier("team-2", "org-1")).toBe(
- 1,
- );
-
- const bare = makeService();
- mockEntityGet.mockResolvedValue({ balances: {} });
- expect(await bare.getKnownRateLimitMultiplier("team-3", "org-1")).toBe(1);
- });
-
- it("answers null, never the fail-open fallback, when Autumn errors, when there is no org, or for a preview team", async () => {
- const svc = makeService();
- mockEntityGet.mockRejectedValue({ statusCode: 500 });
- expect(await svc.getKnownRateLimitMultiplier("team-1", "org-1")).toBeNull();
- // The throttling read still fails open on the same outage: the two callers
- // want opposite failure directions from one entity read.
- expect(await svc.getRateLimitMultiplier("team-1", "org-1")).toBe(2500);
-
- mockEntityGet.mockResolvedValue({
- balances: { rate_limits: { granted: 25 } },
- });
- expect(await svc.getKnownRateLimitMultiplier("team-4", null)).toBeNull();
- expect(
- await svc.getKnownRateLimitMultiplier("preview_x", "org-1"),
- ).toBeNull();
- });
-});
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
index 0bf1da9be..3fb1882ca 100644
--- a/apps/api/src/services/autumn/autumn.service.ts
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -63,7 +63,23 @@ export function featureIdForBillingEndpoint(endpoint?: string): string {
return endpoint === "search" ? SEARCH_CREDITS_FEATURE_ID : CREDITS_FEATURE_ID;
}
+/** A team's Autumn-derived limits and plan, as cached on its ACUC. */
+export type TeamLimits = {
+ concurrency_limit: number;
+ rate_limit_multiplier: number;
+ is_paid_plan: boolean;
+};
+
+/** Limits for a team with no elevated entitlement (a 404 or no balance). */
+export const DEFAULT_TEAM_LIMITS: TeamLimits = {
+ concurrency_limit: 2,
+ rate_limit_multiplier: 1,
+ is_paid_plan: false,
+};
+
const AUTUMN_DEFAULT_PLAN_ID = "free";
+// Mirrors FREE_PLAN_IDS in firecrawl-web's utils/autumn/pick-largest.ts.
+const FREE_PLAN_IDS = new Set([AUTUMN_DEFAULT_PLAN_ID, "free_1_5k"]);
/**
* 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
@@ -810,214 +826,52 @@ export class AutumnService {
}
}
- // Cache the team's entity-derived limits briefly so concurrency enforcement
- // and rate-limit gating on every scrape/crawl/browser request don't fan out
- // to Autumn each time. Both the CONCURRENCY limit and the rate-limit
- // multiplier come from a single entity.get, so one cache entry (and one
- // Autumn round-trip per organization/team per TTL window) serves both callers.
- private entityLimitsCache = new BoundedMap<
- string,
- {
- concurrency: number | null;
- rateLimitMultiplier: number | null;
- expiresAt: number;
- }
- >(50_000);
- private static readonly ENTITY_LIMITS_TTL_MS = 60_000;
-
- // Fail-open fallbacks used ONLY when Autumn itself errors (network / 5xx /
- // unexpected exception) so a billing-API outage doesn't throttle real
- // customers down to the low defaults. A 404 or an absent balance is NOT an
- // error — it legitimately means the team has no elevated entitlement, so
- // those keep falling back low (concurrency 2, multiplier 1). These values are
- // intentionally generous but bounded (the concurrency queue cap still
- // applies).
- private static readonly ERROR_FALLBACK_CONCURRENCY = 200;
- private static readonly ERROR_FALLBACK_RATE_MULTIPLIER = 2500;
-
/**
- * Fetches the team's Autumn entity once and derives both the CONCURRENCY
- * limit and the rate-limit multiplier from it. Each team has its own Autumn
- * entity, so the entity balances are per-team regardless of whether the org
- * has one or many teams.
- *
- * Returns nulls when Autumn is not configured, the entity is missing (404),
- * or a balance isn't present — these mean "no elevated entitlement", so
- * callers fall back to the low defaults. When Autumn itself errors (network /
- * 5xx / unexpected exception), or the caller can name no org, we instead fail
- * OPEN, returning the high ERROR_FALLBACK_* limits so a billing outage
- * doesn't throttle real teams.
+ * The team's limits from one uncached Autumn entity read, and its plan from
+ * a customer read made in parallel (the ACUC caches both). An entity 404
+ * gives the low default limits; a customer 404 gives no paid plan. A team is on a paid plan when its org has an
+ * active, non-add-on subscription to a plan outside FREE_PLAN_IDS. Throws
+ * when Autumn errors or the team has no org.
*/
- private async getEntityLimits(
+ async getTeamLimits(
teamId: string,
orgId: string | null,
- ): Promise<{
- concurrency: number | null;
- rateLimitMultiplier: number | null;
- }> {
- const read = await this.readEntityLimits(teamId, orgId);
- switch (read.outcome) {
- case "unconfigured":
- return { concurrency: null, rateLimitMultiplier: null };
- case "known":
- return {
- concurrency: read.concurrency,
- rateLimitMultiplier: read.rateLimitMultiplier,
- };
- case "no_org":
- // No org means no Autumn entity to ask about. Fail OPEN on the high
- // limits, the same answer this method already gives when it cannot
- // reach Autumn — and the same one the service's own org lookup
- // produced by throwing into the catch below. Not cached, for the same
- // reason.
- logger.error(
- "Autumn getEntityLimits has no org for the team, falling back to high limits",
- { teamId },
- );
- return {
- concurrency: AutumnService.ERROR_FALLBACK_CONCURRENCY,
- rateLimitMultiplier: AutumnService.ERROR_FALLBACK_RATE_MULTIPLIER,
- };
- case "error":
- // Any other failure means we couldn't reach Autumn / it errored. Fail
- // OPEN with high limits rather than throttling the team to the low
- // defaults. Deliberately not cached, so we retry Autumn on the next
- // request instead of pinning the team to the fallback for the TTL
- // window.
- logger.error(
- "Autumn getEntityLimits failed — billing API may be unavailable, falling back to high limits",
- { teamId, error: read.error },
- );
- return {
- concurrency: AutumnService.ERROR_FALLBACK_CONCURRENCY,
- rateLimitMultiplier: AutumnService.ERROR_FALLBACK_RATE_MULTIPLIER,
- };
- }
- }
-
- /**
- * The team's entitled rate-limit multiplier when Autumn can actually answer,
- * or null when it cannot: no Autumn client, a preview team, a team with no
- * org to bill against, or an Autumn error. For a gate that must fail closed,
- * such as a licence that permits a payload only in response to a paid
- * request; getRateLimitMultiplier fails open because throttling a real team
- * during a billing outage is the worse mistake there. A missing entity or an
- * absent balance is a real answer, 1: the free plan.
- */
- async getKnownRateLimitMultiplier(
- teamId: string,
- orgId: string | null,
- ): Promise<number | null> {
- if (!orgId) return null;
- const read = await this.readEntityLimits(teamId, orgId);
- return read.outcome === "known" ? (read.rateLimitMultiplier ?? 1) : null;
- }
-
- /**
- * One entity read behind both getEntityLimits and
- * getKnownRateLimitMultiplier. It reports what happened rather than picking
- * a fallback, so each caller can fail in its own direction. Only "known" is
- * an answer (an entity, or a 404 meaning no elevated entitlement) and only
- * "known" is cached; the other outcomes are retried on the next request.
- */
- private async readEntityLimits(
- teamId: string,
- orgId: string | null,
- ): Promise<
- | { outcome: "unconfigured" }
- | { outcome: "no_org" }
- | { outcome: "error"; error: unknown }
- | {
- outcome: "known";
- concurrency: number | null;
- rateLimitMultiplier: number | null;
- }
- > {
+ ): Promise<TeamLimits> {
if (!autumnClient || this.isPreviewTeam(teamId)) {
- return { outcome: "unconfigured" };
- }
-
- if (!orgId) return { outcome: "no_org" };
-
- const cacheKey = `${orgId}:${teamId}`;
- const now = Date.now();
- const cached = this.entityLimitsCache.get(cacheKey);
- if (cached && cached.expiresAt > now) {
- return {
- outcome: "known",
- concurrency: cached.concurrency,
- rateLimitMultiplier: cached.rateLimitMultiplier,
- };
+ return DEFAULT_TEAM_LIMITS;
}
+ if (!orgId) throw new Error("The team has no org to read limits for");
- const store = (
- concurrency: number | null,
- rateLimitMultiplier: number | null,
- ) => {
- this.entityLimitsCache.set(cacheKey, {
- concurrency,
- rateLimitMultiplier,
- expiresAt: now + AutumnService.ENTITY_LIMITS_TTL_MS,
- });
- return { outcome: "known" as const, concurrency, rateLimitMultiplier };
+ const nullOn404 = (error: unknown) => {
+ if (this.getErrorStatus(error) === 404) return null;
+ throw error;
};
-
- try {
- const entity: any = await autumnClient.entities.get({
- customerId: orgId,
- entityId: teamId,
- });
- const balances = entity?.balances ?? {};
-
+ const [entity, customer] = await Promise.all([
+ autumnClient.entities
+ .get({ customerId: orgId, entityId: teamId })
+ .catch(nullOn404),
+ autumnClient.customers.get({ customerId: orgId }).catch(nullOn404),
+ ]);
+ const balances: Record<string, any> = entity?.balances ?? {};
+
+ return {
// CONCURRENCY: use `remaining` (the post-drain effective per-team cap;
// `granted` would surface the pre-drain inherited customer total).
- const concurrency = sanitizeBalanceValue(
- balances[CONCURRENCY_FEATURE_ID]?.remaining,
- );
-
+ concurrency_limit:
+ sanitizeBalanceValue(balances[CONCURRENCY_FEATURE_ID]?.remaining) ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit,
// rate_limits: a static per-plan multiplier that is never consumed, so
// read `granted` (the entitled amount) rather than `remaining`.
- const rateLimitMultiplier = sanitizeBalanceValue(
- balances[RATE_LIMIT_FEATURE_ID]?.granted,
- );
-
- return store(concurrency, rateLimitMultiplier);
- } catch (error) {
- // 404 = the entity genuinely doesn't exist in Autumn (not an error):
- // fall back low, and cache it so we don't re-query for a team we know is
- // absent.
- if (this.getErrorStatus(error) === 404) return store(null, null);
- return { outcome: "error", error };
- }
- }
-
- /**
- * Reads the team's allowed concurrent-browser count from Autumn's
- * entity-scoped CONCURRENCY balance. Returns null when the entity is missing
- * or there's no balance — callers fall back to the low default via
- * getEffectiveConcurrencyLimit. On an Autumn error it returns the high
- * ERROR_FALLBACK_CONCURRENCY (fail open) rather than null.
- */
- async getConcurrencyLimit(
- teamId: string,
- orgId: string | null,
- ): Promise<number | null> {
- return (await this.getEntityLimits(teamId, orgId)).concurrency;
- }
-
- /**
- * Reads the team's rate-limit multiplier from Autumn's `rate_limits` feature.
- * Effective rate limits are `base × multiplier`. Falls back to a multiplier of
- * 1 when the feature is missing or the entity doesn't exist, so callers don't
- * have to; on an Autumn error it fails open with the high
- * ERROR_FALLBACK_RATE_MULTIPLIER instead. Shares a single cached entity fetch
- * with getConcurrencyLimit, so it adds no Autumn call.
- */
- async getRateLimitMultiplier(
- teamId: string,
- orgId: string | null,
- ): Promise<number> {
- return (await this.getEntityLimits(teamId, orgId)).rateLimitMultiplier ?? 1;
+ rate_limit_multiplier:
+ sanitizeBalanceValue(balances[RATE_LIMIT_FEATURE_ID]?.granted) ??
+ DEFAULT_TEAM_LIMITS.rate_limit_multiplier,
+ is_paid_plan: (customer?.subscriptions ?? []).some(
+ subscription =>
+ subscription.status === "active" &&
+ !subscription.addOn &&
+ !FREE_PLAN_IDS.has(subscription.planId),
+ ),
+ };
}
/**
diff --git a/apps/api/src/services/integrations/slack/commands.ts b/apps/api/src/services/integrations/slack/commands.ts
index 869f93ed8..436e3b929 100644
--- a/apps/api/src/services/integrations/slack/commands.ts
+++ b/apps/api/src/services/integrations/slack/commands.ts
@@ -18,8 +18,7 @@ import {
type MonitorRow,
} from "../../monitoring/types";
import { getTeamBalance } from "../../autumn/usage";
-import { autumnService } from "../../autumn/autumn.service";
-import { orgIdForTeam } from "../../../lib/team-org";
+import { getACUCTeam } from "../../../controllers/auth";
import { getCombinedTeamActiveCount } from "../../worker/nuq-router";
import type { SlackInstallationRow } from "./types";
import { escapeSlackText, slackLink } from "./messages";
@@ -478,8 +477,8 @@ async function accountResponse(
const teamId = installation.team_id;
const [balance, concurrencyLimit, activeJobs] = await Promise.all([
getTeamBalance(teamId).catch(() => null),
- orgIdForTeam(teamId)
- .then(orgId => autumnService.getConcurrencyLimit(teamId, orgId))
+ getACUCTeam(teamId)
+ .then(acuc => acuc?.concurrency_limit ?? null)
.catch(() => null),
getCombinedTeamActiveCount(teamId).catch(() => null),
]);
diff --git a/apps/api/src/services/queue-jobs.ts b/apps/api/src/services/queue-jobs.ts
index 83d5f3797..81cafc013 100644
--- a/apps/api/src/services/queue-jobs.ts
+++ b/apps/api/src/services/queue-jobs.ts
@@ -5,7 +5,6 @@ import {
getConcurrencyLimitActiveJobs,
getConcurrencyQueueJobsCount,
getCrawlConcurrencyLimitActiveJobs,
- getEffectiveConcurrencyLimit,
getTeamQueueLimit,
MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
@@ -19,6 +18,8 @@ import { sendNotificationWithCustomDays } from "./notification/email_notificatio
import { shouldSendConcurrencyLimitNotification } from "./notification/notification-check";
import { getJobFromGCS, removeJobFromGCS } from "../lib/gcs-jobs";
import { Document } from "../controllers/v1/types";
+import { getACUCTeam } from "../controllers/auth";
+import { DEFAULT_TEAM_LIMITS } from "./autumn/autumn.service";
import { getCrawl } from "../lib/crawl-redis";
import { Logger } from "winston";
import { ScrapeJobTimeoutError, TransportableError } from "../lib/error";
@@ -38,7 +39,6 @@ import {
import { serializeTraceContext } from "../lib/otel-tracer";
import { isSelfHosted } from "../lib/deployment";
import { MONITOR_CHECK_STALE_TIMEOUT_MS } from "./monitoring/stale";
-import { orgIdForTeam } from "../lib/team-org";
// Queue-wait deadline for a backlogged job (how long its owner still cares about the result)
function backlogTimeoutMs(data: ScrapeJobData): number {
@@ -346,25 +346,6 @@ async function maybeSendConcurrencyNotificationFdb(
}
}
-// The org for a team's concurrency lookup. It rides the job payload,
-// snapshotted from the request ACUC at acceptance; every job here is one
-// team's, so any of them answers. The ACUC is the fallback for a job enqueued
-// without one (monitor jobs null the field deliberately — it also gates
-// blocklist enforcement), so a monitor team is still gated on its real limit
-// rather than falling open. One resolution per enqueue, never per job.
-async function orgIdForEnqueue(
- jobs: ScrapeJobData[],
- teamId: string,
-): Promise<string | null> {
- return (
- jobs
- .map(d =>
- "internalOptions" in d ? (d.internalOptions?.orgId ?? null) : null,
- )
- .find(o => o !== null) ?? (await orgIdForTeam(teamId))
- );
-}
-
async function addScrapeJobRaw(
webScraperOptions: ScrapeJobData,
jobId: string,
@@ -418,10 +399,9 @@ async function addScrapeJobRaw(
}
}
- maxConcurrency = await getEffectiveConcurrencyLimit(
- webScraperOptions.team_id,
- await orgIdForEnqueue([webScraperOptions], webScraperOptions.team_id),
- );
+ maxConcurrency =
+ (await getACUCTeam(webScraperOptions.team_id).catch(() => null))
+ ?.concurrency_limit ?? DEFAULT_TEAM_LIMITS.concurrency_limit;
if (concurrencyLimited === null) {
const now = Date.now();
@@ -714,13 +694,9 @@ export async function addScrapeJobs(
addToCQ = jobsForcedToCQ;
} else {
const now = Date.now();
- maxConcurrency = await getEffectiveConcurrencyLimit(
- teamId,
- await orgIdForEnqueue(
- allTeamJobs.map(j => j.data),
- teamId,
- ),
- );
+ maxConcurrency =
+ (await getACUCTeam(teamId).catch(() => null))?.concurrency_limit ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit;
await cleanOldConcurrencyLimitEntries(teamId, now);
currentActiveConcurrency = (
diff --git a/apps/api/src/services/worker/nuq-router.ts b/apps/api/src/services/worker/nuq-router.ts
index b296799d9..9156a4250 100644
--- a/apps/api/src/services/worker/nuq-router.ts
+++ b/apps/api/src/services/worker/nuq-router.ts
@@ -3,8 +3,7 @@ import { logger as _logger } from "../../lib/logger";
import { config } from "../../config";
import { RateLimiterMode, ScrapeJobData } from "../../types";
import { getACUCTeam } from "../../controllers/auth";
-import { orgIdForTeam } from "../../lib/team-org";
-import { autumnService } from "../autumn/autumn.service";
+import { DEFAULT_TEAM_LIMITS } from "../autumn/autumn.service";
import { redisEvictConnection } from "../../services/redis";
import { isSelfHosted } from "../../lib/deployment";
import { getApiKeyConcurrencyLimit } from "../../lib/api-key-concurrency";
@@ -268,24 +267,9 @@ export async function fdbEnqueueScrapeJobs(
}> {
let teamLimit: number | null = null;
if (!isSelfHosted()) {
- // The org rides the job payload, snapshotted from the request ACUC at
- // acceptance; every job in a batch is one team's, so any of them answers.
- // The ACUC lookup is the fallback for a job enqueued without one (monitor
- // jobs null the field deliberately — it also gates blocklist enforcement),
- // so a monitor team is still gated on its real limit rather than falling
- // open. Batches are enqueued per discovered link, so the payload org keeps
- // the common path free of a lookup.
- const orgId =
- jobs
- .map(j =>
- "internalOptions" in j.data
- ? (j.data.internalOptions?.orgId ?? null)
- : null,
- )
- .find(o => o !== null) ?? (await orgIdForTeam(teamId));
- const autumnLimit = await autumnService.getConcurrencyLimit(teamId, orgId);
- // fdbForced: leave unlimited (null) when Autumn has no concurrency value.
- teamLimit = fdbForced() ? autumnLimit : (autumnLimit ?? 2);
+ teamLimit =
+ (await getACUCTeam(teamId).catch(() => null))?.concurrency_limit ??
+ DEFAULT_TEAM_LIMITS.concurrency_limit;
}
const queueCap =
diff --git a/apps/api/src/services/worker/scrape-worker.ts b/apps/api/src/services/worker/scrape-worker.ts
index 1d6392bc1..a9aa3a685 100644
--- a/apps/api/src/services/worker/scrape-worker.ts
+++ b/apps/api/src/services/worker/scrape-worker.ts
@@ -128,11 +128,10 @@ if (require.main === module) {
warmExchangeCatalog();
}
-// The org for a job's Autumn lookups. It rides the job payload, snapshotted
-// from the request ACUC at acceptance; the ACUC answers only for a job
-// enqueued without one (monitor jobs null it deliberately, since the field
-// also gates blocklist enforcement) — the same lookup getJobPriority used to
-// make for itself, now hoisted to once per job instead of once per link.
+// The org for a job's billing. It rides the job payload, snapshotted from the
+// request ACUC at acceptance; the ACUC answers only for a job enqueued without
+// one (monitor jobs null it deliberately, since the field also gates blocklist
+// enforcement).
async function orgIdForJob(
orgIdFromJob: string | null | undefined,
teamId: string,
@@ -721,10 +720,10 @@ async function processJob(job: NuQJob<ScrapeJobSingleUrls>) {
}
}
- // Hoisted: one org resolution per job, not one per discovered link.
- const crawlOrgId =
+ // Hoisted: one ACUC read per job, not one per discovered link.
+ const crawlACUC =
discoveredLinks.length > 0
- ? await orgIdForJob(sc.internalOptions?.orgId, sc.team_id)
+ ? await getACUCTeam(sc.team_id).catch(() => null)
: null;
for (const link of discoveredLinks) {
@@ -732,7 +731,7 @@ async function processJob(job: NuQJob<ScrapeJobSingleUrls>) {
// This seems to work really welel
const jobPriority = await getJobPriority({
team_id: sc.team_id,
- org_id: crawlOrgId,
+ acuc: crawlACUC,
basePriority: job.data.crawl_id ? 20 : 10,
});
const jobId = uuidv7();
@@ -1393,7 +1392,6 @@ async function processKickoffJob(job: NuQJob<ScrapeJobKickoff>) {
jobId,
await getJobPriority({
team_id: job.data.team_id,
- org_id: await orgIdForJob(sc.internalOptions?.orgId, job.data.team_id),
basePriority: 15,
}),
);
@@ -1511,10 +1509,6 @@ async function processKickoffJob(job: NuQJob<ScrapeJobKickoff>) {
let jobPriority = await getJobPriority({
team_id: job.data.team_id,
- org_id: await orgIdForJob(
- job.data.internalOptions?.orgId,
- job.data.team_id,
- ),
basePriority: 21,
});
logger.debug("Using job priority " + jobPriority, { jobPriority });
@@ -1688,7 +1682,6 @@ async function processKickoffSitemapJob(job: NuQJob<ScrapeJobKickoffSitemap>) {
const jobPriority = await getJobPriority({
team_id: job.data.team_id,
- org_id: await orgIdForJob(sc.internalOptions?.orgId, job.data.team_id),
basePriority: 21,
});