Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
feat(api): add per-endpoint org rate-limit overrides
Claude20193410e06c
diff --git a/apps/api/src/controllers/__tests__/auth.test.ts b/apps/api/src/controllers/__tests__/auth.test.tsindex 1f9b80d85..b8febf4bc 100644--- a/apps/api/src/controllers/__tests__/auth.test.ts+++ b/apps/api/src/controllers/__tests__/auth.test.ts@@ -88,6 +88,7 @@ describe("authenticateUser", () => {config.MCP_DELEGATED_CREDENTIAL_SECRET;const originalIntrospectUrl = config.OAUTH_INTROSPECT_URL;const originalIntrospectSecret = config.OAUTH_INTROSPECT_SECRET;+ const originalPreviewToken = config.PREVIEW_TOKEN;beforeEach(() => {vi.mocked(isKeylessConfigured).mockReturnValue(false);@@ -105,6 +106,7 @@ describe("authenticateUser", () => {originalMcpDelegatedCredentialSecret;config.OAUTH_INTROSPECT_URL = originalIntrospectUrl;config.OAUTH_INTROSPECT_SECRET = originalIntrospectSecret;+ config.PREVIEW_TOKEN = originalPreviewToken;vi.unstubAllGlobals();vi.clearAllMocks();});@@ -497,6 +499,64 @@ describe("authenticateUser", () => {});});+ it("passes the org rate-limit overrides to the API-key rate limiter", async () => {+ config.USE_DB_AUTHENTICATION = true;+ vi.mocked(getValue).mockResolvedValue(null);+ const flags = { rateLimitOverrides: { scrape: 42 } };+ vi.mocked(authCreditUsageChunk).mockResolvedValue([+ {+ api_key: "00000000-0000-4000-8000-000000000000",+ api_key_id: 1,+ team_id: "team-1",+ org_id: "org-1",+ flags,+ },+ ]);+ vi.mocked(redlock.using).mockImplementation(+ async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),+ );+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);++ const auth = await authenticateUser(+ {+ headers: {+ authorization: "Bearer 00000000-0000-4000-8000-000000000000",+ },+ socket: { remoteAddress: "127.0.0.1" },+ },+ {},+ RateLimiterMode.Scrape,+ );++ expect(auth.success).toBe(true);+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ 50,+ flags,+ );+ });++ it("leaves the preview token on the static rate limiter", async () => {+ config.USE_DB_AUTHENTICATION = true;+ config.PREVIEW_TOKEN = "preview-token";+ vi.mocked(getRateLimiter).mockReturnValue({+ consume: vi.fn().mockResolvedValue(undefined),+ } as never);++ const auth = await authenticateUser(+ {+ headers: { authorization: "Bearer preview-token" },+ socket: { remoteAddress: "127.0.0.1" },+ },+ {},+ RateLimiterMode.Scrape,+ );++ expect(auth.success).toBe(true);+ expect(getRateLimiter).toHaveBeenCalledWith(RateLimiterMode.Preview);+ expect(getAutumnRateLimiter).not.toHaveBeenCalled();+ });+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.tsindex 0c1bb4aad..0bb0f870d 100644--- a/apps/api/src/controllers/auth.ts+++ b/apps/api/src/controllers/auth.ts@@ -26,7 +26,11 @@ import {AuthCreditUsageChunkRow,} from "../db/rpc";import { AuthResponse, RateLimiterMode } from "../types";-import { AuthCreditUsageChunk, AuthCreditUsageChunkFromTeam } from "./v1/types";+import {+ AuthCreditUsageChunk,+ AuthCreditUsageChunkFromTeam,+ TeamFlags,+} from "./v1/types";import {FIRECRAWL_REST_RESOURCE,OAuthIntrospectionUnavailableError,@@ -570,14 +574,18 @@ export async function authenticateUser(* Builds the rate limiter for an authenticated team from its Autumn rate-limit* multiplier. Shared by the OAuth and API-key paths so their limiter setup* can't diverge.+ *+ * The org flags carry the optional per-endpoint override, so they are passed+ * on to getAutumnRateLimiter.*/async function buildAuthenticatedRateLimiter(teamId: string,orgId: string | null | undefined,mode: RateLimiterMode,+ flags: TeamFlags,): Promise<RateLimiterRedis> {const multiplier = await autumnService.getRateLimitMultiplier(teamId, orgId);- return getAutumnRateLimiter(mode, multiplier);+ return getAutumnRateLimiter(mode, multiplier, flags);}async function supaAuthenticateUser(@@ -663,6 +671,7 @@ async function supaAuthenticateUser(teamId,chunk.org_id,mode,+ chunk.flags,);} else if (token.startsWith("fco_")) {// OAuth access token — resolve via introspection endpoint@@ -731,6 +740,7 @@ async function supaAuthenticateUser(teamId,chunk.org_id,mode,+ chunk.flags,);} else {normalizedApi = parseApi(token);@@ -761,6 +771,7 @@ async function supaAuthenticateUser(teamId,chunk.org_id,mode,+ chunk.flags,);}diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.tsindex 64f8a8e3d..df135e902 100644--- a/apps/api/src/controllers/v1/types.ts+++ b/apps/api/src/controllers/v1/types.ts@@ -23,6 +23,7 @@ import { ProductProfile } from "../../types/product";import { MenuProfile } from "../../types/menu";import { threatProtectionOverrideSchema } from "../../lib/threat-protection/config";import { auditMetadataSchema } from "../../lib/siem-logging/types";+import type { RateLimiterMode } from "../../types";type Format =| "markdown"@@ -1336,6 +1337,15 @@ export type TeamFlags = {>;// routes the team's new queue work to the FoundationDB backendnuqFdb?: boolean;+ /**+ * Per-endpoint rate-limit overrides, in requests per minute. A value here+ * replaces the computed limit for that mode, so the Autumn multiplier is+ * not applied. The map is sparse: a mode that is absent keeps the normal+ * computation. Only a finite integer above zero is used; any other value is+ * ignored. Read by getAutumnRateLimiter, so it never affects the preview+ * token.+ */+ rateLimitOverrides?: Partial<Record<RateLimiterMode, number>>;} | null;export type AuthCreditUsageChunkFromTeam = Omit<diff --git a/apps/api/src/services/rate-limiter.test.ts b/apps/api/src/services/rate-limiter.test.tsindex 709b32904..c27beda36 100644--- a/apps/api/src/services/rate-limiter.test.ts+++ b/apps/api/src/services/rate-limiter.test.ts@@ -1,4 +1,18 @@+import { vi } from "vitest";import { config } from "../config";+import { RateLimiterMode } from "../types";+import type { TeamFlags } from "../controllers/v1/types";++// The module builds a Redis client at import time. Stub ioredis so the limiter+// can be constructed without a live server.+vi.mock("ioredis", () => ({+ default: class {+ defineCommand() {}+ },+}));++import { getAutumnRateLimiter } from "./rate-limiter";+// import {// getRateLimiter,// serverRateLimiter,@@ -369,3 +383,79 @@ import { config } from "../config";// });// });// TODO: FIX++describe("getAutumnRateLimiter", () => {+ const flagsWith = (overrides: unknown): TeamFlags =>+ ({ rateLimitOverrides: overrides }) as TeamFlags;++ it("multiplies the base limit when there is no override", () => {+ expect(getAutumnRateLimiter(RateLimiterMode.Scrape, 5).points).toBe(50);+ expect(getAutumnRateLimiter(RateLimiterMode.Scrape, 5, null).points).toBe(+ 50,+ );+ expect(+ getAutumnRateLimiter(RateLimiterMode.Scrape, 5, {} as TeamFlags).points,+ ).toBe(50);+ });++ it("replaces the computed limit with the override for that mode", () => {+ const limiter = getAutumnRateLimiter(+ RateLimiterMode.Scrape,+ 5,+ flagsWith({ [RateLimiterMode.Scrape]: 42 }),+ );++ expect(limiter.points).toBe(42);+ });++ it("keeps the normal limit when only another mode is overridden", () => {+ const limiter = getAutumnRateLimiter(+ RateLimiterMode.Scrape,+ 5,+ flagsWith({ [RateLimiterMode.Map]: 42 }),+ );++ expect(limiter.points).toBe(50);+ });++ it("applies the override to a mode that has no base limit", () => {+ // Research is not multiplier-scaled, so it normally uses the fallback 100.+ expect(getAutumnRateLimiter(RateLimiterMode.Research, 5).points).toBe(100);+ expect(+ getAutumnRateLimiter(+ RateLimiterMode.Research,+ 5,+ flagsWith({ [RateLimiterMode.Research]: 7 }),+ ).points,+ ).toBe(7);+ });++ it.each([+ ["zero", 0],+ ["a negative number", -10],+ ["a non-integer", 12.5],+ ["NaN", Number.NaN],+ ["Infinity", Number.POSITIVE_INFINITY],+ ["a numeric string", "42"],+ ["null", null],+ ["an object", { rpm: 42 }],+ ])("ignores an override of %s", (_label, value) => {+ const limiter = getAutumnRateLimiter(+ RateLimiterMode.Scrape,+ 5,+ flagsWith({ [RateLimiterMode.Scrape]: value }),+ );++ expect(limiter.points).toBe(50);+ });++ it("ignores an override map that is not an object", () => {+ expect(+ getAutumnRateLimiter(RateLimiterMode.Scrape, 5, flagsWith(null)).points,+ ).toBe(50);+ expect(+ getAutumnRateLimiter(RateLimiterMode.Scrape, 5, flagsWith("scrape=42"))+ .points,+ ).toBe(50);+ });+});diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.tsindex 51806d573..911496bb4 100644--- a/apps/api/src/services/rate-limiter.ts+++ b/apps/api/src/services/rate-limiter.ts@@ -1,6 +1,7 @@import { RateLimiterRedis } from "rate-limiter-flexible";import { config } from "../config";import { RateLimiterMode } from "../types";+import type { TeamFlags } from "../controllers/v1/types";import Redis from "ioredis";export const redisRateLimitClient = new Redis(config.REDIS_RATE_LIMIT_URL!, {@@ -72,16 +73,45 @@ export function getRateLimiter(mode: RateLimiterMode): RateLimiterRedis {return createRateLimiter(`${mode}`, rateLimit);}+/**+ * Reads the per-minute override for one mode from the org flags. Returns+ * undefined when there is no usable override.+ *+ * Bad config is skipped, not thrown on: the value is validated on write, but a+ * broken entry must never break authentication. Only a finite integer above+ * zero counts.+ */+function getRateLimitOverride(+ mode: RateLimiterMode,+ overrides: unknown,+): number | undefined {+ if (typeof overrides !== "object" || overrides === null) return undefined;+ const value = (overrides as Record<string, unknown>)[mode];+ if (typeof value !== "number") return undefined;+ if (!Number.isInteger(value) || value <= 0) return undefined;+ return value;+}+/*** Builds the per-minute rate limiter for an authenticated team from its Autumn* rate-limit multiplier: the effective limit is `base × multiplier` for* multiplier-scaled modes (default ×1). Modes without a base fall back to the* static table.+ *+ * An org-level override for the mode replaces that whole computation, so the+ * multiplier and the base table are both discarded. Modes without an override+ * keep the normal result.*/export function getAutumnRateLimiter(mode: RateLimiterMode,multiplier: number = 1,+ flags?: TeamFlags,): RateLimiterRedis {+ const override = getRateLimitOverride(mode, flags?.rateLimitOverrides);+ if (override !== undefined) {+ return createRateLimiter(`${mode}`, override);+ }+const base = BASE_RATE_LIMITS[mode];let rateLimit: number;if (base !== undefined) {