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): floor agent interop traffic at the hobby rate-limit multiplier (#4583)
Rakshith Ramprakash7812a4a324d8
diff --git a/apps/api/src/controllers/__tests__/auth.test.ts b/apps/api/src/controllers/__tests__/auth.test.tsindex 10bcb22ef..71d53fb88 100644--- a/apps/api/src/controllers/__tests__/auth.test.ts+++ b/apps/api/src/controllers/__tests__/auth.test.ts@@ -9,6 +9,7 @@ import { deleteKey, getValue, setValue } from "../../services/redis";import {getAutumnRateLimiter,getRateLimiter,+ HOBBY_RATE_LIMIT_MULTIPLIER,} from "../../services/rate-limiter";import {consumeKeylessRequest,@@ -102,6 +103,7 @@ describe("authenticateUser", () => {const originalIntrospectUrl = config.OAUTH_INTROSPECT_URL;const originalIntrospectSecret = config.OAUTH_INTROSPECT_SECRET;const originalPreviewToken = config.PREVIEW_TOKEN;+ const originalAgentInteropSecret = config.AGENT_INTEROP_SECRET;beforeEach(() => {vi.mocked(isKeylessConfigured).mockReturnValue(false);@@ -120,6 +122,7 @@ describe("authenticateUser", () => {config.OAUTH_INTROSPECT_URL = originalIntrospectUrl;config.OAUTH_INTROSPECT_SECRET = originalIntrospectSecret;config.PREVIEW_TOKEN = originalPreviewToken;+ config.AGENT_INTEROP_SECRET = originalAgentInteropSecret;vi.unstubAllGlobals();vi.clearAllMocks();});@@ -590,6 +593,130 @@ describe("authenticateUser", () => {);});+ describe("agent interop rate-limit floor", () => {+ const flags = {};+ const agentRequest = (auth: string) => ({+ headers: {+ authorization: "Bearer 00000000-0000-4000-8000-000000000000",+ },+ socket: { remoteAddress: "127.0.0.1" },+ body: { __agentInterop: { auth, requestId: "req-1", shouldBill: true } },+ });++ beforeEach(() => {+ config.USE_DB_AUTHENTICATION = true;+ config.AGENT_INTEROP_SECRET = "agent-secret";+ vi.mocked(getValue).mockResolvedValue(null);+ 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),+ );+ });++ it("floors a free team's multiplier at hobby for a trusted agent request", async () => {+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);++ const auth = await authenticateUser(+ agentRequest("agent-secret"),+ {},+ RateLimiterMode.Scrape,+ );++ expect(auth.success).toBe(true);+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ HOBBY_RATE_LIMIT_MULTIPLIER,+ flags,+ );+ });++ it("leaves a paid plan's multiplier alone for a trusted agent request", async () => {+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(50);++ await authenticateUser(+ agentRequest("agent-secret"),+ {},+ RateLimiterMode.Scrape,+ );++ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ 50,+ flags,+ );+ });++ it("does not floor the multiplier when the agent interop secret is wrong", async () => {+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);++ await authenticateUser(+ agentRequest("not-the-secret"),+ {},+ RateLimiterMode.Scrape,+ );++ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ 1,+ flags,+ );+ });++ 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);++ await authenticateUser(+ agentRequest("agent-secret"),+ {},+ RateLimiterMode.Scrape,+ );++ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ 1,+ flags,+ );+ });++ it("lets a per-team override win over the floor for a trusted agent request", async () => {+ const overrideFlags = { 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: overrideFlags,+ },+ ]);+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);++ await authenticateUser(+ agentRequest("agent-secret"),+ {},+ RateLimiterMode.Scrape,+ );++ // 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();+ expect(getAutumnRateLimiter).toHaveBeenCalledWith(+ RateLimiterMode.Scrape,+ 1,+ overrideFlags,+ );+ });+ });+it("leaves the preview token on the static rate limiter", async () => {config.USE_DB_AUTHENTICATION = true;config.PREVIEW_TOKEN = "preview-token";diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.tsindex 45998e46d..241b64055 100644--- a/apps/api/src/controllers/auth.ts+++ b/apps/api/src/controllers/auth.ts@@ -9,7 +9,9 @@ import {getRateLimiter,getAutumnRateLimiter,getRateLimitOverride,+ HOBBY_RATE_LIMIT_MULTIPLIER,} from "../services/rate-limiter";+import { isAgentInteropSecretValid } from "../lib/agent-interop";import {KEYLESS_FREE_TIER_LIMIT_MESSAGE,consumeKeylessRequest,@@ -639,20 +641,40 @@ export async function authenticateUser(* on to getAutumnRateLimiter, which stays the only place deciding the final* limit. An override makes the multiplier irrelevant, so we skip fetching it* from Autumn in that case rather than paying for a value that is discarded.+ *+ * `minMultiplier` floors the Autumn multiplier (trusted agent traffic passes+ * the hobby multiplier). It never applies on top of an override, which already+ * replaces the whole computation.*/async function buildAuthenticatedRateLimiter(teamId: string,orgId: string | null | undefined,mode: RateLimiterMode,flags: TeamFlags,+ minMultiplier?: number,): Promise<RateLimiterRedis> {- const multiplier =- getRateLimitOverride(mode, flags?.rateLimitOverrides) !== undefined- ? 1- : await autumnService.getRateLimitMultiplier(teamId, orgId);+ let multiplier: number;+ if (getRateLimitOverride(mode, flags?.rateLimitOverrides) !== undefined) {+ multiplier = 1;+ } else {+ multiplier = await autumnService.getRateLimitMultiplier(teamId, orgId);+ if (minMultiplier !== undefined) {+ multiplier = Math.max(multiplier, minMultiplier);+ }+ }return getAutumnRateLimiter(mode, multiplier, flags);}+/**+ * Whether the request carries a valid `__agentInterop` secret, i.e. comes from+ * the trusted internal agent service. Read from the raw body because auth runs+ * before the controller's zod parse — the same shape checkCreditsMiddleware+ * relies on. Presence of the block alone is never trusted; only the secret.+ */+function isTrustedAgentInteropRequest(req): boolean {+ return isAgentInteropSecretValid(req.body?.__agentInterop?.auth);+}+async function supaAuthenticateUser(req,res,@@ -681,6 +703,14 @@ async function supaAuthenticateUser(req.socket.remoteAddress) as string;const iptoken = incomingIP + token;+ // An agent run fans one customer request out into ~10 sub-requests against+ // the team's own bucket, so a free team (×1) gets throttled by its own agent.+ // Floor trusted agent traffic at the hobby multiplier; paid plans already+ // meet it and are unchanged.+ const minRateMultiplier = isTrustedAgentInteropRequest(req)+ ? HOBBY_RATE_LIMIT_MULTIPLIER+ : undefined;+let rateLimiter: RateLimiterRedis;let subscriptionData: { team_id: string } | null = null;let normalizedApi: string;@@ -737,6 +767,7 @@ async function supaAuthenticateUser(chunk.org_id,mode,chunk.flags,+ minRateMultiplier,);} else if (token.startsWith("fco_")) {// OAuth access token — resolve via introspection endpoint@@ -806,6 +837,7 @@ async function supaAuthenticateUser(chunk.org_id,mode,chunk.flags,+ minRateMultiplier,);} else {normalizedApi = parseApi(token);@@ -837,6 +869,7 @@ async function supaAuthenticateUser(chunk.org_id,mode,chunk.flags,+ minRateMultiplier,);}diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.tsindex 7abc68484..978cca5ca 100644--- a/apps/api/src/services/rate-limiter.ts+++ b/apps/api/src/services/rate-limiter.ts@@ -124,6 +124,15 @@ export function getAutumnRateLimiter(return createRateLimiter(`${mode}`, rateLimit);}+/**+ * Autumn rate-limit multiplier granted to the hobby plan. Trusted agent+ * traffic (a valid `__agentInterop` secret) is floored at this multiplier so a+ * free team's agent runs are limited like hobby rather than at ×1; see+ * buildAuthenticatedRateLimiter in controllers/auth.ts. Paid plans already+ * meet or exceed it, so the floor only ever lifts free.+ */+export const HOBBY_RATE_LIMIT_MULTIPLIER = 10;+/*** Plan-priority tiers keyed by the minimum Autumn rate-limit multiplier that* qualifies. Values mirror the tuned production `plan_priority` for each plan.@@ -140,7 +149,11 @@ const PLAN_PRIORITY_TIERS: {planModifier: number;}[] = [{ minMultiplier: 1, bucketLimit: 25, planModifier: 0.5 }, // free- { minMultiplier: 10, bucketLimit: 100, planModifier: 0.3 }, // hobby+ {+ minMultiplier: HOBBY_RATE_LIMIT_MULTIPLIER,+ bucketLimit: 100,+ planModifier: 0.3,+ }, // hobby{ minMultiplier: 50, bucketLimit: 200, planModifier: 0.2 }, // standard{ minMultiplier: 500, bucketLimit: 400, planModifier: 0.1 }, // growth{ minMultiplier: 1000, bucketLimit: 400, planModifier: 0.1 }, // scale