AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform): cap and cache public store search embeddings (#15703)
Reinier van der Leerdbdb84c72361
diff --git a/autogpt_platform/backend/backend/api/features/search/embeddings.py b/autogpt_platform/backend/backend/api/features/search/embeddings.pyindex 738bb4ddfd..c5252f51c7 100644--- a/autogpt_platform/backend/backend/api/features/search/embeddings.py+++ b/autogpt_platform/backend/backend/api/features/search/embeddings.py@@ -12,18 +12,26 @@ Feature-specific wrappers live next to the feature they belong to:"""import asyncio+import hashlibimport loggingimport time+from contextvars import ContextVarfrom typing import Anyimport prismafrom prisma.enums import ContentType+from redis.exceptions import RedisClusterException, RedisErrorfrom tiktoken import encoding_for_modelfrom backend.api.features.search.content_handlers import CONTENT_HANDLERS+from backend.api.features.search.rate_limit import (+ public_search_embeddings_enabled,+ take_public_search_embedding_slot,+)from backend.data.db import execute_raw_with_schema, query_raw_with_schema+from backend.data.redis_client import get_redis_asyncfrom backend.util.clients import get_openai_client-from backend.util.json import dumps+from backend.util.json import dumps, loadsfrom backend.util.settings import Settingslogger = logging.getLogger(__name__)@@ -41,6 +49,17 @@ EMBEDDING_MODEL = _settings.config.store_embedding_modelEMBEDDING_DIM = 1536# OpenAI embedding token limit (8,191 with 1 token buffer for safety)EMBEDDING_MAX_TOKENS = 8191+# Repeated searches and later result pages reuse a query's embedding this long.+QUERY_EMBEDDING_TTL_SECONDS = 600+_QUERY_EMBEDDING_REDIS_TIMEOUT_SECONDS = 0.25+_CACHE_FAILURES = (+ RedisError,+ RedisClusterException,+ ConnectionError,+ OSError,+ asyncio.TimeoutError,+ ValueError,+)# Single source of truth for the content types the maintenance jobs# (backfill + orphan cleanup) walk. BLOCK is first so foundational content@@ -154,14 +173,72 @@ async def generate_embedding(text: str) -> list[float]:return embedding-async def embed_query(query: str) -> list[float]:+class PublicSearchBudgetSpent(Exception):+ """Store search has requested all the new query embeddings it may this minute."""+++# A caller that needs to know whether its search was refused an embedding sets a+# list here; a list, so a refusal made in a task the search spawns still reaches it.+refused_public_queries: ContextVar[list[str] | None] = ContextVar(+ "refused_public_queries", default=None+)+++async def embed_query(query: str, *, public_search: bool = False) -> list[float]:"""- Generate embedding for a search query.+ Generate embedding for a search query, reusing the embedding of the same+ query from the last ``QUERY_EMBEDDING_TTL_SECONDS``.- Same as generate_embedding but with clearer intent.+ With ``public_search``, a new embedding needs a slot from store search's+ per-minute budget; without one this raises ``PublicSearchBudgetSpent``. A+ budget of 0 raises it for every query, cached or not.Raises exceptions on failure - caller should handle."""- return await generate_embedding(query)+ query = " ".join(query.split())+ if public_search and not public_search_embeddings_enabled():+ raise PublicSearchBudgetSpent()+ digest = hashlib.sha256(query.encode()).hexdigest()+ key = f"search:query-embedding:{EMBEDDING_MODEL}:{digest}"+ if cached := await _cached_query_embedding(key):+ return cached+ if public_search and not await take_public_search_embedding_slot():+ if (refused := refused_public_queries.get()) is not None:+ refused.append(query)+ raise PublicSearchBudgetSpent()+ embedding = await generate_embedding(query)+ await _cache_query_embedding(key, embedding)+ return embedding+++async def _cached_query_embedding(key: str) -> list[float] | None:+ try:+ cached = await asyncio.wait_for(+ _redis_get(key), timeout=_QUERY_EMBEDDING_REDIS_TIMEOUT_SECONDS+ )+ return loads(cached) if cached else None+ except _CACHE_FAILURES as e:+ logger.debug("Query embedding cache read failed: %s", e)+ return None+++async def _cache_query_embedding(key: str, embedding: list[float]) -> None:+ try:+ await asyncio.wait_for(+ _redis_set(key, dumps(embedding)),+ timeout=_QUERY_EMBEDDING_REDIS_TIMEOUT_SECONDS,+ )+ except _CACHE_FAILURES as e:+ logger.debug("Query embedding cache write failed: %s", e)+++async def _redis_get(key: str) -> str | bytes | None:+ redis = await get_redis_async()+ return await redis.get(key)+++async def _redis_set(key: str, value: str) -> None:+ redis = await get_redis_async()+ await redis.set(key, value, ex=QUERY_EMBEDDING_TTL_SECONDS)def embedding_to_vector_string(embedding: list[float]) -> str:diff --git a/autogpt_platform/backend/backend/api/features/search/embeddings_test.py b/autogpt_platform/backend/backend/api/features/search/embeddings_test.pyindex 3a71683e7f..851807e8a1 100644--- a/autogpt_platform/backend/backend/api/features/search/embeddings_test.py+++ b/autogpt_platform/backend/backend/api/features/search/embeddings_test.py@@ -1,5 +1,6 @@"""Tests for the generic embedding service in search/embeddings.py."""+import uuidfrom unittest.mock import AsyncMock, MagicMock, patchimport prisma@@ -7,7 +8,7 @@ import pytestfrom prisma import Prismafrom prisma.enums import ContentType-from backend.api.features.search import embeddings+from backend.api.features.search import embeddings, rate_limit@pytest.fixture(autouse=True)@@ -167,14 +168,60 @@ async def test_embedding_to_vector_string():@pytest.mark.asyncio(loop_scope="session")-async def test_embed_query():- """Test embed_query function (alias for generate_embedding)."""- with patch(- "backend.api.features.search.embeddings.generate_embedding"- ) as mock_generate:- mock_generate.return_value = [0.1, 0.2, 0.3]+async def test_a_repeated_query_reuses_its_embedding():+ # Unique per run: the cache is the real Redis, shared with later runs.+ query = f"repeat {uuid.uuid4().hex}"+ generate = AsyncMock(return_value=[0.1, 0.2, 0.3])+ with patch.object(embeddings, "generate_embedding", generate):+ first = await embeddings.embed_query(query)+ again = await embeddings.embed_query(f" {query.replace(' ', ' ')} ")++ assert first == again == [0.1, 0.2, 0.3]+ generate.assert_awaited_once_with(query)++[email protected](loop_scope="session")+async def test_a_public_query_past_the_budget_is_not_embedded():+ generate = AsyncMock(return_value=[0.1])+ with (+ patch.object(embeddings, "generate_embedding", generate),+ patch.object(+ embeddings,+ "take_public_search_embedding_slot",+ AsyncMock(return_value=False),+ ),+ pytest.raises(embeddings.PublicSearchBudgetSpent),+ ):+ await embeddings.embed_query(f"spent {uuid.uuid4().hex}", public_search=True)++ generate.assert_not_awaited()+- result = await embeddings.embed_query("test query")[email protected](loop_scope="session")+async def test_with_the_budget_off_a_public_query_ignores_a_cached_embedding(+ monkeypatch,+):+ query = f"off {uuid.uuid4().hex}"+ with patch.object(embeddings, "generate_embedding", AsyncMock(return_value=[0.4])):+ await embeddings.embed_query(query)+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_embeddings_per_minute", 0+ )++ with pytest.raises(embeddings.PublicSearchBudgetSpent):+ await embeddings.embed_query(query, public_search=True)+ assert await embeddings.embed_query(query) == [0.4]++[email protected](loop_scope="session")+async def test_only_a_new_public_embedding_takes_from_the_budget():+ query = f"cached {uuid.uuid4().hex}"+ slot = AsyncMock(return_value=False)+ with (+ patch.object(embeddings, "generate_embedding", AsyncMock(return_value=[0.4])),+ patch.object(embeddings, "take_public_search_embedding_slot", slot),+ ):+ await embeddings.embed_query(query)+ assert await embeddings.embed_query(query, public_search=True) == [0.4]- assert result == [0.1, 0.2, 0.3]- mock_generate.assert_called_once_with("test query")+ slot.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/api/features/search/hybrid_search.py b/autogpt_platform/backend/backend/api/features/search/hybrid_search.pyindex 1a064e5e7f..b753a25708 100644--- a/autogpt_platform/backend/backend/api/features/search/hybrid_search.py+++ b/autogpt_platform/backend/backend/api/features/search/hybrid_search.py@@ -29,6 +29,7 @@ from typing_extensions import NotRequired, TypedDictfrom backend.api.features.search.embeddings import (EMBEDDING_DIM,+ PublicSearchBudgetSpent,embed_query,embedding_to_vector_string,)@@ -185,6 +186,7 @@ async def unified_hybrid_search(user_id: str | None = None,lexical_query: str | None = None,prefix_match: bool = False,+ public_search: bool = False,) -> tuple[list[HybridSearchRow], int]:"""Unified hybrid search across all content types.@@ -208,6 +210,8 @@ async def unified_hybrid_search(of ``query`` (e.g. ``"youtube summarize video"`` for a goallike ``"summarize a YouTube video..."``). Defaults to``query`` when None.+ public_search: Count a new query embedding against store search's+ per-minute budget, and rank by text alone once it is spent.Returns:Tuple of (results list, total count)@@ -259,13 +263,18 @@ async def unified_hybrid_search(offset = (page - 1) * page_size# Generate query embedding with graceful degradation+ query_embedding: list[float] | Nonetry:- query_embedding = await embed_query(query)+ query_embedding = await embed_query(query, public_search=public_search)+ except PublicSearchBudgetSpent:+ query_embedding = Noneexcept Exception as e:logger.warning(f"Failed to generate query embedding - falling back to lexical-only search: {e}. ""Check that openai_internal_api_key is configured and OpenAI API is accessible.")+ query_embedding = None+ if query_embedding is None:query_embedding = [0.0] * EMBEDDING_DIM# Redistribute semantic weight to lexicaltotal_non_semantic = weights.lexical + weights.category + weights.recencydiff --git a/autogpt_platform/backend/backend/api/features/search/hybrid_search_test.py b/autogpt_platform/backend/backend/api/features/search/hybrid_search_test.pyindex d8dce4302e..74dd836bca 100644--- a/autogpt_platform/backend/backend/api/features/search/hybrid_search_test.py+++ b/autogpt_platform/backend/backend/api/features/search/hybrid_search_test.py@@ -312,7 +312,15 @@ async def test_unified_hybrid_search_custom_weights():@pytest.mark.asyncio(loop_scope="session")@pytest.mark.integration-async def test_unified_hybrid_search_graceful_degradation():+ "error, warns",+ [+ (Exception("Embedding generation failed"), True),+ (embeddings.PublicSearchBudgetSpent(), False),+ ],+ ids=["embedding-failed", "budget-spent"],+)+async def test_unified_hybrid_search_graceful_degradation(error, warns, caplog):"""Test unified search gracefully degrades when embeddings unavailable."""mock_results = [{@@ -337,7 +345,7 @@ async def test_unified_hybrid_search_graceful_degradation():patch("backend.api.features.search.hybrid_search.embed_query") as mock_embed,):mock_query.return_value = mock_results- mock_embed.side_effect = Exception("Embedding generation failed")+ mock_embed.side_effect = error# Should NOT raise - graceful degradationresults, total = await unified_hybrid_search(@@ -348,6 +356,7 @@ async def test_unified_hybrid_search_graceful_degradation():assert len(results) == 1assert total == 1+ assert ("Failed to generate query embedding" in caplog.text) is warns@pytest.mark.asyncio(loop_scope="session")diff --git a/autogpt_platform/backend/backend/api/features/search/rate_limit.py b/autogpt_platform/backend/backend/api/features/search/rate_limit.pyindex 5807443566..9cb5636388 100644--- a/autogpt_platform/backend/backend/api/features/search/rate_limit.py+++ b/autogpt_platform/backend/backend/api/features/search/rate_limit.py@@ -10,19 +10,33 @@ spend without backpressure.Fixed-window counter in Redis (INCR + EXPIRE on first hit). Approximateby design — under high concurrency two calls may both pass against thesame snapshot, same trade-off as :mod:`backend.copilot.rate_limit`.++Store search (``/api/store/search``, ``/api/store/agents?search_query=``)+is public, so it is limited per account when signed in and per client address+otherwise (:func:`enforce_public_search_rate_limit`), and the new query embeddings it+requests share one per-minute budget across all clients+(:func:`take_public_search_embedding_slot`)."""from __future__ import annotations+import asyncio+import hashlibimport logging+import mathfrom datetime import UTC, datetime+from typing import Any, castimport fastapi+import sentry_sdk+from redis.exceptions import RedisClusterException, RedisErrorfrom backend.data.redis_client import get_redis_asyncfrom backend.monitoring.instrumentation import record_rate_limit_hit+from backend.util.settings import Settingslogger = logging.getLogger(__name__)+settings = Settings()# 120 req/minute / user = 2 QPS sustained, generous for the 200 ms-debounced# search-as-you-type frontend (~5 keystrokes/sec collapse to ~1 call/sec)@@ -30,6 +44,33 @@ logger = logging.getLogger(__name__)GLOBAL_SEARCH_WINDOW_SECONDS = 60GLOBAL_SEARCH_MAX_REQUESTS = 120+PUBLIC_SEARCH_WINDOW_SECONDS = 60+PUBLIC_SEARCH_EMBEDDINGS_KEY_PREFIX = "store:search:embeddings"+PUBLIC_SEARCH_EMBEDDINGS_ALERT_PERCENT = 80+PUBLIC_SEARCH_EMBEDDINGS_ALERT_TAG = "store_search_embedding_budget"+# One short deadline for the connect and the command: an outage must fall+# through fast instead of holding the request on redis-py's retry ladder.+PUBLIC_SEARCH_REDIS_TIMEOUT_SECONDS = 0.25++_REDIS_FAILURES = (+ RedisError,+ RedisClusterException,+ ConnectionError,+ OSError,+ asyncio.TimeoutError,+ ValueError,+)++# INCR, and set the TTL only on the INCR that opened the window, in one atomic+# step so a new key can never be left without an expiry.+_INCR_OPEN_WINDOW = """+local count = redis.call('INCR', KEYS[1])+if count == 1 then+ redis.call('EXPIRE', KEYS[1], ARGV[1])+end+return count+"""+def _window_key(user_id: str, *, now: datetime) -> str:"""Per-user fixed-window key. Bucket aligned to ``GLOBAL_SEARCH_WINDOW_SECONDS``@@ -77,3 +118,114 @@ async def enforce_global_search_rate_limit(user_id: str) -> None:f"{GLOBAL_SEARCH_WINDOW_SECONDS}s). Try again shortly."),)+++async def enforce_public_search_rate_limit(client: str, *, user_id: str | None) -> None:+ """Raise HTTP 429 with ``Retry-After`` when the caller has made more store+ searches this minute than its limit: ``public_search_user_requests_per_minute``+ for a signed-in account, ``public_search_requests_per_minute`` for a signed-out+ ``client`` address.++ Fails open on Redis trouble; the embedding budget still bounds the cost.+ """+ if user_id:+ caller = f"user:{user_id}"+ limit = settings.config.public_search_user_requests_per_minute+ else:+ caller = f"ip:{client}"+ limit = settings.config.public_search_requests_per_minute+ now = datetime.now(UTC)+ caller_key = hashlib.sha256(caller.encode()).hexdigest()[:16]+ bucket = int(now.timestamp()) // PUBLIC_SEARCH_WINDOW_SECONDS+ try:+ count = await _count_in_window(f"store:search:rl:{caller_key}:{bucket}")+ except _REDIS_FAILURES as e:+ logger.warning(f"Store search rate-limit check failed open: {e!r}")+ return++ if count > limit:+ record_rate_limit_hit("/api/store/search", caller_key)+ raise fastapi.HTTPException(+ status_code=429,+ detail=(+ f"Store search rate limit exceeded ({limit} requests per "+ f"{PUBLIC_SEARCH_WINDOW_SECONDS}s). Try again shortly."+ ),+ headers={"Retry-After": str(_seconds_left_in_window(now))},+ )+++async def take_public_search_embedding_slot() -> bool:+ """Whether store search may request one more new query embedding this+ minute, out of ``public_search_embeddings_per_minute`` shared by all clients.++ Fails closed on Redis trouble: the search still answers, ranked by text.+ Records one Sentry event a minute when the budget is+ ``PUBLIC_SEARCH_EMBEDDINGS_ALERT_PERCENT`` percent used, and one when it is spent.+ A budget of 0 turns embeddings off: no slot, no count, no alert.+ """+ if not public_search_embeddings_enabled():+ return False+ now = datetime.now(UTC)+ bucket = int(now.timestamp()) // PUBLIC_SEARCH_WINDOW_SECONDS+ try:+ count = await _count_in_window(+ f"{PUBLIC_SEARCH_EMBEDDINGS_KEY_PREFIX}:{bucket}"+ )+ except _REDIS_FAILURES as e:+ logger.warning(f"Store search embedding budget unavailable: {e!r}")+ return False++ limit = settings.config.public_search_embeddings_per_minute+ # Each count is reached by exactly one call per window, so each fires once.+ if count == math.ceil(limit * PUBLIC_SEARCH_EMBEDDINGS_ALERT_PERCENT / 100):+ _alert_embedding_budget(+ "nearly_spent",+ f"Store search has used {count} of its {limit} query embeddings "+ "for this minute",+ )+ elif count == limit + 1:+ _alert_embedding_budget(+ "spent",+ f"Store search used its {limit} query embeddings for this minute; "+ "ranking by text alone until the next one",+ )+ return count <= limit+++def public_search_embeddings_enabled() -> bool:+ """False when ``public_search_embeddings_per_minute`` is 0: store search then+ always ranks by text alone."""+ return settings.config.public_search_embeddings_per_minute > 0+++def _alert_embedding_budget(stage: str, message: str) -> None:+ """Log at ERROR so the Sentry event exists, tagged and fingerprinted per stage+ so an alert rule can select it and each stage stays one issue."""+ tags = {"alert": PUBLIC_SEARCH_EMBEDDINGS_ALERT_TAG, "budget_stage": stage}+ with sentry_sdk.new_scope() as scope:+ for tag, value in tags.items():+ scope.set_tag(tag, value)+ scope.fingerprint = [PUBLIC_SEARCH_EMBEDDINGS_ALERT_TAG, stage]+ logger.error(message, extra={"json_fields": tags})+++async def _count_in_window(key: str) -> int:+ """This call's position in the window ``key`` counts."""+ return await asyncio.wait_for(+ _incr_open_window(key), timeout=PUBLIC_SEARCH_REDIS_TIMEOUT_SECONDS+ )+++async def _incr_open_window(key: str) -> int:+ redis = await get_redis_async()+ return await cast(+ Any,+ redis.eval(_INCR_OPEN_WINDOW, 1, key, str(PUBLIC_SEARCH_WINDOW_SECONDS)),+ )+++def _seconds_left_in_window(now: datetime) -> int:+ return PUBLIC_SEARCH_WINDOW_SECONDS - (+ int(now.timestamp()) % PUBLIC_SEARCH_WINDOW_SECONDS+ )diff --git a/autogpt_platform/backend/backend/api/features/search/rate_limit_test.py b/autogpt_platform/backend/backend/api/features/search/rate_limit_test.pyindex 6720da7f94..2891cc0eb6 100644--- a/autogpt_platform/backend/backend/api/features/search/rate_limit_test.py+++ b/autogpt_platform/backend/backend/api/features/search/rate_limit_test.py@@ -2,6 +2,9 @@from __future__ import annotations+import logging+import uuid+from datetime import UTC, datetimefrom unittest.mock import AsyncMock, MagicMockimport fastapi@@ -116,3 +119,122 @@ async def test_over_limit_records_a_rate_limit_hit(fake_redis):assert (_counter("autogpt_rate_limit_hits_total", endpoint="/api/search") == before + 1)+++# The store search limits below run against the real Redis: the window and its+# expiry are Redis's to keep, so a double would only restate the code.+++def frozen_minute(monkeypatch):+ """Pin every call to 30 s into one minute, so no test straddles a window,+ with room for a cold Redis connection."""++ class _Frozen(datetime):+ @classmethod+ def now(cls, tz=None):+ return datetime(2026, 1, 1, 12, 0, 30, tzinfo=UTC)++ monkeypatch.setattr(rate_limit, "datetime", _Frozen)+ monkeypatch.setattr(rate_limit, "PUBLIC_SEARCH_REDIS_TIMEOUT_SECONDS", 10)+++async def test_a_store_search_client_past_its_limit_gets_429_with_retry_after(+ frozen_minute, monkeypatch+):+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_requests_per_minute", 2+ )+ client = f"client-{uuid.uuid4().hex}"+ await rate_limit.enforce_public_search_rate_limit(client, user_id=None)+ await rate_limit.enforce_public_search_rate_limit(client, user_id=None)++ with pytest.raises(fastapi.HTTPException) as exc_info:+ await rate_limit.enforce_public_search_rate_limit(client, user_id=None)++ assert exc_info.value.status_code == 429+ assert exc_info.value.headers == {"Retry-After": "30"}+ await rate_limit.enforce_public_search_rate_limit(+ f"other-{uuid.uuid4().hex}", user_id=None+ )+++async def test_store_search_embeds_only_its_budget_per_minute(+ frozen_minute, monkeypatch+):+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_embeddings_per_minute", 2+ )+ monkeypatch.setattr(+ rate_limit,+ "PUBLIC_SEARCH_EMBEDDINGS_KEY_PREFIX",+ f"test:store:search:embeddings:{uuid.uuid4().hex}",+ )++ slots = [await rate_limit.take_public_search_embedding_slot() for _ in range(3)]++ assert slots == [True, True, False]+++async def test_store_search_reports_its_embedding_budget_once_nearly_spent_and_once_spent(+ frozen_minute, monkeypatch, caplog+):+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_embeddings_per_minute", 5+ )+ monkeypatch.setattr(+ rate_limit,+ "PUBLIC_SEARCH_EMBEDDINGS_KEY_PREFIX",+ f"test:store:search:embeddings:{uuid.uuid4().hex}",+ )++ errors_after_each_call = []+ with caplog.at_level(logging.ERROR, logger=rate_limit.logger.name):+ for _ in range(8):+ await rate_limit.take_public_search_embedding_slot()+ errors_after_each_call.append(len(_errors(caplog)))++ assert errors_after_each_call == [0, 0, 0, 1, 1, 2, 2, 2]+ assert [vars(r)["json_fields"]["budget_stage"] for r in _errors(caplog)] == [+ "nearly_spent",+ "spent",+ ]+++async def test_a_disabled_embedding_budget_takes_no_slot_and_raises_no_alert(+ frozen_minute, monkeypatch, caplog+):+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_embeddings_per_minute", 0+ )+ monkeypatch.setattr(+ rate_limit,+ "PUBLIC_SEARCH_EMBEDDINGS_KEY_PREFIX",+ f"test:store:search:embeddings:{uuid.uuid4().hex}",+ )++ with caplog.at_level(logging.ERROR, logger=rate_limit.logger.name):+ slots = [await rate_limit.take_public_search_embedding_slot() for _ in range(3)]++ assert slots == [False, False, False]+ assert _errors(caplog) == []+++async def test_without_redis_store_search_is_not_limited_but_embeds_nothing(mocker):+ mocker.patch.object(+ rate_limit,+ "get_redis_async",+ AsyncMock(side_effect=ConnectionError("redis down")),+ )++ await rate_limit.enforce_public_search_rate_limit("203.0.113.9", user_id=None)+ assert await rate_limit.take_public_search_embedding_slot() is False+++def _errors(caplog) -> list[logging.LogRecord]:+ return [r for r in caplog.records if r.levelno == logging.ERROR]diff --git a/autogpt_platform/backend/backend/api/features/store/cache.py b/autogpt_platform/backend/backend/api/features/store/cache.pyindex e81a9cd84a..d976cbdc89 100644--- a/autogpt_platform/backend/backend/api/features/store/cache.py+++ b/autogpt_platform/backend/backend/api/features/store/cache.py@@ -1,6 +1,14 @@+import logging++from redis.exceptions import RedisClusterException, RedisError++from backend.api.features.search.embeddings import refused_public_queriesfrom backend.util.cache import cachedfrom . import db as store_db+from . import model as store_model++logger = logging.getLogger(__name__)############################################################# Caches #######################@@ -15,6 +23,53 @@ def clear_all_caches():_get_cached_creator_details.cache_clear()+async def get_public_store_agents(+ featured: bool,+ creator: str | None,+ sorted_by: store_db.StoreAgentsSortOptions | None,+ search_query: str | None,+ category: str | None,+ page: int,+ page_size: int,+) -> store_model.StoreAgentsResponse:+ """Cached store agents for a public route, whose searches count against the store+ search embedding budget. A search ranked by text alone because that budget was+ spent is dropped from the cache, so it is ranked fully once the budget refills."""+ refused: list[str] = []+ token = refused_public_queries.set(refused)+ try:+ agents = await _get_cached_store_agents(+ featured=featured,+ creator=creator,+ sorted_by=sorted_by,+ search_query=search_query,+ category=category,+ page=page,+ page_size=page_size,+ public_search=True,+ )+ finally:+ refused_public_queries.reset(token)+ if refused:+ # The cache read and write already degrade on Redis trouble; this must too.+ try:+ _get_cached_store_agents.cache_delete(+ featured=featured,+ creator=creator,+ sorted_by=sorted_by,+ search_query=search_query,+ category=category,+ page=page,+ page_size=page_size,+ public_search=True,+ )+ except (RedisError, RedisClusterException, ConnectionError, OSError) as e:+ logger.warning(+ f"Could not drop a text-ranked store search from the cache: {e!r}"+ )+ return agents++# Cache store agents list for 5 minutes# Different cache entries for different query combinations@cached(maxsize=5000, ttl_seconds=300, shared_cache=True)@@ -26,6 +81,7 @@ async def _get_cached_store_agents(category: str | None,page: int,page_size: int,+ public_search: bool = False,):"""Cached helper to get store agents."""return await store_db.get_store_agents(@@ -36,6 +92,7 @@ async def _get_cached_store_agents(category=category,page=page,page_size=page_size,+ public_search=public_search,)diff --git a/autogpt_platform/backend/backend/api/features/store/cache_test.py b/autogpt_platform/backend/backend/api/features/store/cache_test.pynew file mode 100644index 0000000000..89752c3f9e--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/store/cache_test.py@@ -0,0 +1,109 @@+"""The public store search cache, against the real Redis."""++import uuid+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.api.features.search import embeddings, rate_limit+from backend.util import cache as cache_util++from . import cache as store_cache+from . import hybrid_search as store_hybrid_search++[email protected](loop_scope="session")+async def test_a_search_ranked_by_text_alone_is_recomputed_and_a_full_one_cached():+ # Unique per run: the cache is the real Redis, shared with later runs.+ query = f"sheets {uuid.uuid4().hex}"+ rows = AsyncMock(return_value=[_agent_row()])+ slot = AsyncMock(return_value=False)+ vector = AsyncMock(return_value=[0.1] * embeddings.EMBEDDING_DIM)+ with (+ patch.object(store_hybrid_search, "query_raw_with_schema", rows),+ patch.object(embeddings, "take_public_search_embedding_slot", slot),+ patch.object(embeddings, "generate_embedding", vector),+ ):+ for _ in range(2):+ await _search(query)+ assert rows.await_count == 2++ slot.return_value = True+ for _ in range(2):+ await _search(query)+ assert rows.await_count == 3++[email protected](loop_scope="session")+async def test_with_the_embedding_budget_off_a_text_ranked_search_is_cached():+ rows = AsyncMock(return_value=[_agent_row()])+ with (+ patch.object(store_hybrid_search, "query_raw_with_schema", rows),+ patch.object(+ rate_limit.settings.config, "public_search_embeddings_per_minute", 0+ ),+ ):+ query = f"sheets {uuid.uuid4().hex}"+ for _ in range(2):+ await _search(query)++ assert rows.await_count == 1++[email protected](loop_scope="session")+async def test_a_search_ranked_by_text_alone_answers_while_the_cache_is_unreachable():+ with (+ patch.object(+ store_hybrid_search,+ "query_raw_with_schema",+ AsyncMock(return_value=[_agent_row()]),+ ),+ patch.object(+ embeddings,+ "take_public_search_embedding_slot",+ AsyncMock(return_value=False),+ ),+ patch.object(+ cache_util, "_get_redis", MagicMock(side_effect=ConnectionError("down"))+ ),+ ):+ response = await _search(f"sheets {uuid.uuid4().hex}")++ assert [agent.slug for agent in response.agents] == ["sheets-agent"]+++async def _search(query: str):+ return await store_cache.get_public_store_agents(+ featured=False,+ creator=None,+ sorted_by=None,+ search_query=query,+ category=None,+ page=1,+ page_size=20,+ )+++def _agent_row() -> dict:+ return {+ "slug": "sheets-agent",+ "agent_name": "Sheets Agent",+ "agent_image": ["sheets.png"],+ "creator_username": "creator",+ "creator_avatar": "avatar.png",+ "sub_heading": "Works with sheets",+ "description": "Reads and writes sheets",+ "runs": 10,+ "rating": 4.5,+ "categories": ["data"],+ "featured": False,+ "is_available": True,+ "updated_at": "2025-01-01T00:00:00Z",+ "semantic_score": 0.0,+ "lexical_score": 0.5,+ "category_score": 0.0,+ "recency_score": 0.1,+ "popularity_score": 0.2,+ "combined_score": 0.3,+ "total_count": 1,+ }diff --git a/autogpt_platform/backend/backend/api/features/store/db.py b/autogpt_platform/backend/backend/api/features/store/db.pyindex b913d1ded3..88b2b864c5 100644--- a/autogpt_platform/backend/backend/api/features/store/db.py+++ b/autogpt_platform/backend/backend/api/features/store/db.py@@ -59,6 +59,7 @@ async def get_store_agents(category: str | None = None,page: int = 1,page_size: int = 20,+ public_search: bool = False,) -> store_model.StoreAgentsResponse:"""Get PUBLIC store agents from the StoreAgent view.@@ -67,6 +68,8 @@ async def get_store_agents(- With search_query: Uses hybrid search (semantic + lexical)- Fallback: If embeddings unavailable, gracefully degrades to lexical-only- Rationale: User-facing endpoint prioritizes availability over accuracy+ - public_search: a new query embedding takes a slot from store search's+ per-minute budget, and the search ranks by text alone once it is spentNote: Admin operations (approval) use fail-fast to prevent inconsistent state."""@@ -96,6 +99,7 @@ async def get_store_agents(sorted_by="relevance", # Use hybrid scoring for relevancepage=page,page_size=page_size,+ public_search=public_search,)search_used_hybrid = Trueexcept Exception as e:diff --git a/autogpt_platform/backend/backend/api/features/store/hybrid_search.py b/autogpt_platform/backend/backend/api/features/store/hybrid_search.pyindex 691d8bff37..349d10cd9a 100644--- a/autogpt_platform/backend/backend/api/features/store/hybrid_search.py+++ b/autogpt_platform/backend/backend/api/features/store/hybrid_search.py@@ -15,6 +15,7 @@ from typing import Any, Literal, castfrom backend.api.features.search.embeddings import (EMBEDDING_DIM,+ PublicSearchBudgetSpent,embed_query,embedding_to_vector_string,)@@ -79,11 +80,14 @@ async def hybrid_search(page_size: int = 20,weights: StoreAgentSearchWeights | None = None,min_score: float | None = None,+ public_search: bool = False,) -> tuple[list[dict[str, Any]], int]:"""Hybrid search for store agents with full metadata.Uses UnifiedContentEmbedding for search, joins to StoreAgent for metadata.+ ``public_search`` ranks by text alone once store search's per-minute+ embedding budget is spent."""query = query.strip()if not query:@@ -106,12 +110,17 @@ async def hybrid_search(offset = (page - 1) * page_size# Generate query embedding with graceful degradation+ query_embedding: list[float] | Nonetry:- query_embedding = await embed_query(query)+ query_embedding = await embed_query(query, public_search=public_search)+ except PublicSearchBudgetSpent:+ query_embedding = Noneexcept Exception as e:logger.warning(f"Failed to generate query embedding - falling back to lexical-only search: {e}")+ query_embedding = None+ if query_embedding is None:query_embedding = [0.0] * EMBEDDING_DIMtotal_non_semantic = (weights.lexical + weights.category + weights.recency + weights.popularitydiff --git a/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py b/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.pyindex 4d0ce3c1e5..0bcf1521f6 100644--- a/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py+++ b/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py@@ -122,7 +122,15 @@ async def test_hybrid_search_with_custom_schema():@pytest.mark.asyncio(loop_scope="session")@pytest.mark.integration-async def test_hybrid_search_without_embeddings():+ "error, warns",+ [+ (Exception("Embedding generation failed"), True),+ (embeddings.PublicSearchBudgetSpent(), False),+ ],+ ids=["embedding-failed", "budget-spent"],+)+async def test_hybrid_search_without_embeddings(error, warns, caplog):"""Test hybrid search gracefully degrades when embeddings are unavailable."""mock_results = [{@@ -153,7 +161,7 @@ async def test_hybrid_search_without_embeddings():with patch("backend.api.features.store.hybrid_search.query_raw_with_schema") as mock_query:- mock_embed.side_effect = Exception("Embedding generation failed")+ mock_embed.side_effect = errormock_query.return_value = mock_resultsresults, total = await hybrid_search(@@ -165,6 +173,7 @@ async def test_hybrid_search_without_embeddings():assert len(results) == 1assert results[0]["slug"] == "test-agent"assert total == 1+ assert ("Failed to generate query embedding" in caplog.text) is warns@pytest.mark.asyncio(loop_scope="session")diff --git a/autogpt_platform/backend/backend/api/features/store/routes.py b/autogpt_platform/backend/backend/api/features/store/routes.pyindex eb5da0bf52..2451b00c55 100644--- a/autogpt_platform/backend/backend/api/features/store/routes.py+++ b/autogpt_platform/backend/backend/api/features/store/routes.py@@ -14,6 +14,8 @@ from starlette.datastructures import Headersimport backend.data.graphimport backend.util.jsonfrom backend.api.features.search import hybrid_search as search_engine+from backend.api.features.search.rate_limit import enforce_public_search_rate_limit+from backend.api.utils.client_ip import ClientIPfrom backend.copilot.rate_limit import enforce_payment_paywallfrom backend.util.exceptions import NotFoundErrorfrom backend.util.models import Pagination@@ -35,6 +37,9 @@ router = fastapi.APIRouter()SubmissionSortKey = Literal["submitted", "runs"]SubmissionSortDir = Literal["asc", "desc"]+# Same bound as global search: caps what one query can cost to embed.+MAX_SEARCH_QUERY_LENGTH = 512+############################################################# Profile Endpoints ############@@ -84,7 +89,8 @@ async def update_or_create_profile(tags=["store", "public"],)async def unified_search(- query: str,+ client: ClientIP,+ query: str = Query(max_length=MAX_SEARCH_QUERY_LENGTH),content_types: list[prisma.enums.ContentType] | None = Query(default=None,description="Content types to search. If not specified, searches all.",@@ -101,6 +107,7 @@ async def unified_search(Combines semantic (embedding-based) and lexical (text-based) search for best results."""+ await enforce_public_search_rate_limit(client, user_id=user_id)# Perform unified hybrid searchresults, total = await search_engine.unified_hybrid_search(@@ -109,6 +116,7 @@ async def unified_search(user_id=user_id,page=page,page_size=page_size,+ public_search=True,)# Convert results to response model@@ -150,6 +158,7 @@ async def unified_search(tags=["store", "public"],)async def get_agents(+ client: ClientIP,featured: bool = Query(default=False, description="Filter to only show featured agents"),@@ -158,7 +167,9 @@ async def get_agents(),category: str | None = Query(default=None, description="Filter agents by category"),search_query: str | None = Query(- default=None, description="Literal + semantic search on names and descriptions"+ default=None,+ max_length=MAX_SEARCH_QUERY_LENGTH,+ description="Literal + semantic search on names and descriptions",),sorted_by: store_db.StoreAgentsSortOptions | None = Query(default=None,@@ -166,6 +177,9 @@ async def get_agents(),page: int = Query(ge=1, default=1),page_size: int = Query(ge=1, default=20),+ user_id: str | None = Security(+ autogpt_libs.auth.get_optional_user_id, use_cache=False+ ),) -> store_model.StoreAgentsResponse:"""Get a paginated list of agents from the marketplace,@@ -179,7 +193,9 @@ async def get_agents(- Agent Details - Similar Agents- Creator Details - Agents By Creator"""- agents = await store_cache._get_cached_store_agents(+ if search_query and search_query.strip():+ await enforce_public_search_rate_limit(client, user_id=user_id)+ agents = await store_cache.get_public_store_agents(featured=featured,creator=creator,sorted_by=sorted_by,diff --git a/autogpt_platform/backend/backend/api/features/store/routes_test.py b/autogpt_platform/backend/backend/api/features/store/routes_test.pyindex e55c130572..02a9d7f0ae 100644--- a/autogpt_platform/backend/backend/api/features/store/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/store/routes_test.py@@ -1,5 +1,6 @@import datetimeimport json+import uuidimport fastapiimport fastapi.testclient@@ -95,6 +96,7 @@ def test_get_agents_defaults(category=None,page=1,page_size=20,+ public_search=True,)@@ -141,6 +143,7 @@ def test_get_agents_featured(category=None,page=1,page_size=20,+ public_search=True,)@@ -187,6 +190,7 @@ def test_get_agents_by_creator(category=None,page=1,page_size=20,+ public_search=True,)@@ -233,6 +237,7 @@ def test_get_agents_sorted(category=None,page=1,page_size=20,+ public_search=True,)@@ -279,6 +284,7 @@ def test_get_agents_search(category=None,page=1,page_size=20,+ public_search=True,)@@ -324,6 +330,7 @@ def test_get_agents_category(category="test-category",page=1,page_size=20,+ public_search=True,)@@ -372,6 +379,7 @@ def test_get_agents_pagination(category=None,page=2,page_size=5,+ public_search=True,)@@ -393,6 +401,124 @@ def test_get_agents_malformed_request(mocker: pytest_mock.MockFixture):mock_db_call.assert_not_called()+def one_search_a_minute(monkeypatch):+ from backend.api.features.search import rate_limit++ class _Frozen(datetime.datetime):+ @classmethod+ def now(cls, tz=None):+ return datetime.datetime(2026, 1, 1, 12, 0, 30, tzinfo=datetime.UTC)++ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_requests_per_minute", 1+ )+ monkeypatch.setattr(rate_limit, "datetime", _Frozen)+ # A cold Redis connection can outlast the deadline, and the limit then fails open.+ monkeypatch.setattr(rate_limit, "PUBLIC_SEARCH_REDIS_TIMEOUT_SECONDS", 10)+++ "path", ["/search?query=sheets", "/agents?search_query=sheets"]+)+def test_a_caller_without_the_frontends_token_is_held_to_the_search_limit(+ mocker: pytest_mock.MockFixture, one_search_a_minute, path: str+) -> None:+ _mock_search_backends(mocker)+ caller = _a_new_caller()++ first = caller.get(path, headers=_claimed_address("192.0.2.1"))+ blocked = caller.get(path, headers=_claimed_address("192.0.2.2"))++ assert first.status_code == 200+ assert blocked.status_code == 429+ assert blocked.headers["Retry-After"] == "30"+++ "path", ["/search?query=sheets", "/agents?search_query=sheets"]+)+def test_signed_in_searches_count_per_account_and_signed_out_ones_per_address(+ mocker: pytest_mock.MockFixture, one_search_a_minute, monkeypatch, path: str+) -> None:+ from backend.api.features.search import rate_limit++ _mock_search_backends(mocker)+ monkeypatch.setattr(+ rate_limit.settings.config, "public_search_user_requests_per_minute", 2+ )+ app.dependency_overrides[get_optional_user_id] = _user_from_test_header+ office = _a_new_caller()+ alice, bob = _signed_in(), _signed_in()++ alice_statuses = [office.get(path, headers=alice).status_code for _ in range(3)]+ bob_statuses = [office.get(path, headers=bob).status_code for _ in range(2)]+ signed_out_statuses = [office.get(path).status_code for _ in range(2)]++ assert alice_statuses == [200, 200, 429]+ assert bob_statuses == [200, 200]+ assert signed_out_statuses == [200, 429]+++def test_listing_agents_without_a_search_is_not_limited(+ mocker: pytest_mock.MockFixture, one_search_a_minute+) -> None:+ _mock_search_backends(mocker)+ caller = _a_new_caller()++ statuses = [caller.get("/agents").status_code for _ in range(3)]++ assert statuses == [200, 200, 200]+++def test_store_search_counts_against_the_public_embedding_budget(+ mocker: pytest_mock.MockFixture,+) -> None:+ engine = _mock_search_backends(mocker)++ response = _a_new_caller().get("/search?query=sheets")++ assert response.status_code == 200+ assert engine.await_args.kwargs["public_search"] is True+++def _a_new_caller() -> fastapi.testclient.TestClient:+ """A client with a socket address of its own, so no test shares its window."""+ return fastapi.testclient.TestClient(app, client=(f"caller-{uuid.uuid4().hex}", 1))+++def _signed_in() -> dict[str, str]:+ return {"X-Test-User": f"user-{uuid.uuid4().hex}"}+++def _user_from_test_header(+ x_test_user: str | None = fastapi.Header(default=None),+) -> str | None:+ return x_test_user+++def _claimed_address(ip: str) -> dict[str, str]:+ return {"CF-Connecting-IP": ip, "X-Forwarded-For": ip, "X-Real-IP": ip}+++def _mock_search_backends(mocker: pytest_mock.MockFixture):+ mocker.patch(+ "backend.api.features.store.db.get_store_agents",+ return_value=store_model.StoreAgentsResponse(+ agents=[],+ pagination=store_model.Pagination(+ current_page=1, total_items=0, total_pages=0, page_size=20+ ),+ ),+ )+ return mocker.patch.object(+ store_routes.search_engine,+ "unified_hybrid_search",+ new_callable=mocker.AsyncMock,+ return_value=([], 0),+ )++def test_get_agent_details(mocker: pytest_mock.MockFixture,snapshot: Snapshot,diff --git a/autogpt_platform/backend/backend/api/utils/client_ip.py b/autogpt_platform/backend/backend/api/utils/client_ip.pynew file mode 100644index 0000000000..297c2b8a9c--- /dev/null+++ b/autogpt_platform/backend/backend/api/utils/client_ip.py@@ -0,0 +1,31 @@+from typing import Annotated++from autogpt_libs.auth.service import frontend_service_claims+from fastapi import Depends, Header, Request++CLIENT_IP_SCOPE = "client-ip"+CLIENT_IP_TOKEN_HEADER = "X-Client-IP-Token"+++async def client_ip(+ request: Request,+ token: Annotated[+ str | None, Header(alias=CLIENT_IP_TOKEN_HEADER, include_in_schema=False)+ ] = None,+) -> str:+ """The calling visitor's IP, for per-visitor limits on public routes.++ Browser calls arrive from the frontend's server, so the frontend signs the+ visitor's IP into a short-lived service token. Without a valid one, the+ caller is keyed by its connecting address, which uvicorn reads from+ X-Forwarded-For only when the peer is a proxy listed in FORWARDED_ALLOW_IPS.+ """+ if token:+ claims = await frontend_service_claims(token, CLIENT_IP_SCOPE)+ ip = claims.get("ip") if claims else None+ if isinstance(ip, str) and ip:+ return ip+ return request.client.host if request.client else "unknown"+++ClientIP = Annotated[str, Depends(client_ip)]diff --git a/autogpt_platform/backend/backend/api/utils/client_ip_test.py b/autogpt_platform/backend/backend/api/utils/client_ip_test.pynew file mode 100644index 0000000000..2ff9554c63--- /dev/null+++ b/autogpt_platform/backend/backend/api/utils/client_ip_test.py@@ -0,0 +1,114 @@+from datetime import UTC, datetime++import fastapi+import jwt+import pytest+from autogpt_libs.auth import config as auth_config+from autogpt_libs.auth import jwt_utils+from autogpt_libs.auth.service import FRONTEND_SERVICE_SUBJECT, SERVICE_TOKEN_AUDIENCE+from cryptography.hazmat.primitives.asymmetric import ec+from httpx import ASGITransport, AsyncClient+from jwt.algorithms import ECAlgorithm++from backend.api.utils.client_ip import CLIENT_IP_SCOPE, ClientIP++SOCKET_IP = "198.51.100.1"+# Every header a proxy might use for the client, set by the caller itself.+CLAIMED_ADDRESS = {+ "CF-Connecting-IP": "192.0.2.10",+ "X-Forwarded-For": "192.0.2.10",+ "X-Real-IP": "192.0.2.10",+ "True-Client-IP": "192.0.2.10",+}++app = fastapi.FastAPI()++[email protected]("/client")+async def _client(client: ClientIP) -> str:+ return client+++def frontend_key(monkeypatch):+ """The frontend's JWKS signing key, as the backend would fetch it."""+ key = ec.generate_private_key(ec.SECP256R1())+ jwk = ECAlgorithm.to_jwk(key.public_key(), as_dict=True)+ jwk.update({"kid": "frontend-1", "alg": "ES256", "use": "sig"})+ monkeypatch.setenv("JWT_JWKS_URL", "https://platform.example.com/api/auth/jwks")+ monkeypatch.setattr(auth_config, "_settings", auth_config.Settings())+ monkeypatch.setattr(jwt_utils, "_jwks_client", None)+ monkeypatch.setattr(jwt_utils, "_jwks_client_url", None)+ monkeypatch.setattr(jwt.PyJWKClient, "fetch_data", lambda self: {"keys": [jwk]})+ return key+++async def test_the_ip_the_frontend_signed_is_the_client(frontend_key):+ token = _ip_token(frontend_key, "203.0.113.7")+ headers = {"X-Client-IP-Token": token, **CLAIMED_ADDRESS}++ assert await _client_of(headers) == "203.0.113.7"+++ "forge",+ [+ lambda key: None,+ lambda key: _ip_token(ec.generate_private_key(ec.SECP256R1()), "203.0.113.7"),+ lambda key: _ip_token(key, "203.0.113.7", scope="client-country"),+ lambda key: _ip_token(key, "203.0.113.7", sub="user-1", aud="authenticated"),+ lambda key: _ip_token(key, "203.0.113.7", exp=1),+ lambda key: jwt.encode(+ {+ "sub": FRONTEND_SERVICE_SUBJECT,+ "aud": SERVICE_TOKEN_AUDIENCE,+ "scope": CLIENT_IP_SCOPE,+ "ip": "203.0.113.7",+ },+ "a-shared-secret-anyone-could-guess-0123456789",+ algorithm="HS256",+ ),+ lambda key: _ip_token(key, ["203.0.113.7"]),+ ],+ ids=[+ "missing",+ "foreign-key",+ "other-scope",+ "user-token",+ "expired",+ "hs256",+ "non-string",+ ],+)+async def test_without_a_token_the_frontend_issued_the_caller_is_its_own_address(+ frontend_key, forge+):+ headers = dict(CLAIMED_ADDRESS)+ if token := forge(frontend_key):+ headers["X-Client-IP-Token"] = token++ assert await _client_of(headers) == SOCKET_IP+++def _ip_token(key, ip, **overrides) -> str:+ now = int(datetime.now(UTC).timestamp())+ claims = {+ "sub": FRONTEND_SERVICE_SUBJECT,+ "aud": SERVICE_TOKEN_AUDIENCE,+ "scope": CLIENT_IP_SCOPE,+ "ip": ip,+ "iat": now,+ "exp": now + 60,+ **overrides,+ }+ return jwt.encode(claims, key, algorithm="ES256", headers={"kid": "frontend-1"})+++async def _client_of(headers: dict[str, str]) -> str:+ transport = ASGITransport(app=app, client=(SOCKET_IP, 4321))+ async with AsyncClient(transport=transport, base_url="https://example.com") as c:+ response = await c.get("/client", headers=headers)+ assert response.status_code == 200+ return response.json()diff --git a/autogpt_platform/backend/backend/util/metrics_test.py b/autogpt_platform/backend/backend/util/metrics_test.pyindex 830c2cac56..9869fd494e 100644--- a/autogpt_platform/backend/backend/util/metrics_test.py+++ b/autogpt_platform/backend/backend/util/metrics_test.py@@ -23,6 +23,7 @@ from sentry_sdk.utils import event_from_exception, json_dumps# Imported at module scope on purpose: AppProcess calls sentry_init() in its# class body, so the guard has to hold at collection time, not just in a test.import backend.util.process+from backend.api.utils.client_ip import CLIENT_IP_TOKEN_HEADERfrom backend.util import feature_flag, metrics, tracingfrom backend.util.exceptions import InsufficientBalanceErrorfrom backend.util.metrics import (@@ -690,6 +691,8 @@ def _event_with_ip_headers() -> dict:"Cf-Ipcity": "Warsaw","Cf-Iplatitude": "52.2","X-Vercel-Ip-City": "Warsaw",+ # The frontend's signed client-IP token carries the visitor's IP too.+ CLIENT_IP_TOKEN_HEADER: "203.0.113.7","Cf-Ray": "8a1b2c3d4e5f6789-WAW",},},diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 25224fa403..76a214f31b 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -407,6 +407,24 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):"emit 1536-dim vectors to match the hardcoded pgvector column."),)+ public_search_requests_per_minute: int = Field(+ default=30,+ ge=1,+ description="Store search requests one signed-out client may make per minute.",+ )+ public_search_user_requests_per_minute: int = Field(+ default=120,+ ge=1,+ description="Store search requests one signed-in account may make per minute.",+ )+ public_search_embeddings_per_minute: int = Field(+ default=1000,+ ge=0,+ description=(+ "New query embeddings store search may request per minute across all "+ "clients; past it, store search ranks by text match alone."+ ),+ )model_config = SettingsConfigDict(env_file=".env",diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.defaultindex 1e78d45611..2ff1b300f3 100644--- a/autogpt_platform/frontend/.env.default+++ b/autogpt_platform/frontend/.env.default@@ -29,11 +29,6 @@ AUTH_REQUIRE_EMAIL_VERIFICATION=false# whose mail goes to a known temporary-inbox host) are refused. "true" allows them.# AUTH_ALLOW_DISPOSABLE_EMAILS=false-# The sign-in IP log and IP blocks take the client IP from X-Forwarded-For only-# on Vercel or with this set: set it only behind a proxy that overwrites that-# header. Without it they get no IP; per-IP auth limits read the header anyway.-# TRUST_X_FORWARDED_FOR=false-# Social login providers (enabled when both values of a pair are set)# AUTH_GOOGLE_CLIENT_ID=# AUTH_GOOGLE_CLIENT_SECRET=@@ -144,3 +139,9 @@ NEXT_PUBLIC_VAPID_PUBLIC_KEY=BBg49iVTWthVbRYphwmZNvZyiSJDqtSO4nmLxDzLKe3Oo9jbtu0# OpenAI (for voice transcription)OPENAI_API_KEY=++# Set to true when the proxy in front of the frontend overwrites X-Forwarded-For+# with the visitor's address; Vercel's edge already does. Store search is then+# limited per visitor, and the sign-in IP log and IP blocks get the client IP.+# Without it they get no IP; per-IP auth limits read the header anyway.+TRUST_X_FORWARDED_FOR=diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.tsindex 91a1a635a8..d458828599 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts@@ -9,12 +9,21 @@ const mockNotFound = vi.hoisted(() =>);const mockGetServerUser = vi.hoisted(() => vi.fn());const mockPrefetchLibrary = vi.hoisted(() => vi.fn());+const mockPrefetchStoreAgents = vi.hoisted(() => vi.fn());+const mockGetClientIPToken = vi.hoisted(() => vi.fn());vi.mock("next/navigation", () => ({ notFound: mockNotFound }));+vi.mock("next/headers", () => ({+ headers: async () => new Headers({ "x-forwarded-for": "203.0.113.7" }),+}));vi.mock("@/app/api/__generated__/endpoints/store/store", () => ({getV2GetSpecificAgent: mockGetSpecificAgent,prefetchGetV2GetSpecificAgentQuery: vi.fn(),- prefetchGetV2ListStoreAgentsQuery: vi.fn(),+ prefetchGetV2ListStoreAgentsQuery: mockPrefetchStoreAgents,+}));++vi.mock("@/lib/auth/client-ip-token", () => ({+ getClientIPToken: mockGetClientIPToken,}));vi.mock("@/app/api/__generated__/endpoints/library/library", () => ({@@ -39,6 +48,8 @@ describe("generateMetadata", () => {mockGetServerUser.mockResolvedValue({ user: null });mockNotFound.mockClear();mockPrefetchLibrary.mockClear();+ mockPrefetchStoreAgents.mockClear();+ mockGetClientIPToken.mockResolvedValue(null);});describe.each([@@ -88,6 +99,21 @@ describe("generateMetadata", () => {);});+ test("searches for similar agents as the visitor, by their signed IP", async () => {+ mockGetClientIPToken.mockResolvedValue("signed");+ mockGetSpecificAgent.mockResolvedValue({ status: 200, data: {} });++ await MarketplaceAgentPage({ params: Promise.resolve(params) });++ const [visitorHeaders] = mockGetClientIPToken.mock.calls[0];+ expect(visitorHeaders.get("x-forwarded-for")).toBe("203.0.113.7");+ expect(mockPrefetchStoreAgents).toHaveBeenCalledWith(+ expect.anything(),+ { search_query: "an agent" },+ { request: { headers: { "X-Client-IP-Token": "signed" } } },+ );+ });+test("previews the agent's own name, sub-heading and image", async () => {mockGetSpecificAgent.mockResolvedValue({data: {diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsxindex 509beb6a44..3ac701fab5 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx@@ -8,10 +8,13 @@ import {} from "@/app/api/__generated__/endpoints/store/store";import { StoreAgentDetails } from "@/app/api/__generated__/models/storeAgentDetails";import { getQueryClient } from "@/lib/react-query/queryClient";+import { getClientIPToken } from "@/lib/auth/client-ip-token";import { getServerUser } from "@/lib/auth/server/getServerUser";+import { CLIENT_IP_TOKEN_HEADER_NAME } from "@/lib/constants";import { buildPageMetadata } from "@/lib/metadata";import { dehydrate, HydrationBoundary } from "@tanstack/react-query";import { Metadata } from "next";+import { headers } from "next/headers";import { MainAgentPage } from "../../../components/MainAgentPage/MainAgentPage";export const dynamic = "force-dynamic";@@ -50,9 +53,7 @@ export default async function MarketplaceAgentPage({prefetchGetV2ListStoreAgentsQuery(queryClient, {creator: creator_lower,}),- prefetchGetV2ListStoreAgentsQuery(queryClient, {- search_query: params.slug.replace(/-/g, " "),- }),+ prefetchSimilarAgents(queryClient, params.slug),]);const { user } = await getServerUser();@@ -89,3 +90,22 @@ async function getAgentOrNotFound(creator: string, slug: string) {throw error;}}++// The backend limits searches per visitor, so this one carries the visitor's IP.+async function prefetchSimilarAgents(+ queryClient: ReturnType<typeof getQueryClient>,+ slug: string,+) {+ const clientIPToken = await getClientIPToken(await headers());+ return prefetchGetV2ListStoreAgentsQuery(+ queryClient,+ { search_query: slug.replace(/-/g, " ") },+ clientIPToken+ ? {+ request: {+ headers: { [CLIENT_IP_TOKEN_HEADER_NAME]: clientIPToken },+ },+ }+ : undefined,+ );+}diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 516a426f36..5ad2c1296b 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -16900,6 +16900,7 @@"summary": "List store agents","description": "Get a paginated list of agents from the marketplace,\nwith optional filtering and sorting.\n\nUsed for:\n- Home Page Featured Agents\n- Home Page Top Agents\n- Search Results\n- Agent Details - Other Agents By Creator\n- Agent Details - Similar Agents\n- Creator Details - Agents By Creator","operationId": "getV2List store agents",+ "security": [{ "HTTPBearer": [] }],"parameters": [{"name": "featured",@@ -16940,7 +16941,10 @@"in": "query","required": false,"schema": {- "anyOf": [{ "type": "string" }, { "type": "null" }],+ "anyOf": [+ { "type": "string", "maxLength": 512 },+ { "type": "null" }+ ],"description": "Literal + semantic search on names and descriptions","title": "Search Query"},@@ -17572,7 +17576,7 @@"name": "query","in": "query","required": true,- "schema": { "type": "string", "title": "Query" }+ "schema": { "type": "string", "maxLength": 512, "title": "Query" }},{"name": "content_types",diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.tsindex 919e678a95..3ae54365b2 100644--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts@@ -9,12 +9,17 @@ vi.mock("@/lib/auth/country-token", () => ({getCountryToken: vi.fn(),}));+vi.mock("@/lib/auth/client-ip-token", () => ({+ getClientIPToken: vi.fn(),+}));+vi.mock("@/services/environment", () => ({environment: {getAGPTServerBaseUrl: vi.fn(() => "https://backend.test"),},}));+import { getClientIPToken } from "@/lib/auth/client-ip-token";import { getCountryToken } from "@/lib/auth/country-token";import { getServerAuthToken } from "@/lib/auth/server/getServerAuthToken";import { GET, POST } from "../route";@@ -39,6 +44,10 @@ describe("proxy route — handler pass-through", () => {vi.mocked(getCountryToken).mockImplementation(async (country) => `signed-${country}`,);+ vi.mocked(getClientIPToken).mockReset();+ vi.mocked(getClientIPToken).mockImplementation(+ async (headers) => `signed-ip-${headers.get("x-forwarded-for")}`,+ );vi.stubGlobal("fetch", vi.fn());});@@ -297,6 +306,69 @@ describe("proxy route — handler pass-through", () => {expect(sentHeaders.get("x-client-country-token")).toBeNull();});+ it.each([+ ["api/store/search", "?query=sheets"],+ ["api/store/agents", "?search_query=sheets"],+ ])(+ "sends the visitor's IP to %s only as the proxy's own signed token",+ async (route, query) => {+ vi.mocked(fetch).mockResolvedValue(+ new Response("{}", {+ status: 200,+ headers: { "Content-Type": "application/json" },+ }),+ );++ const req = new NextRequest(+ `https://app.test/api/proxy/${route}${query}`,+ {+ headers: {+ "x-forwarded-for": "203.0.113.7",+ "x-client-ip-token": "forged",+ },+ },+ );+ await GET(req, makeParams(route.split("/")));++ const sentHeaders = vi.mocked(fetch).mock.calls[0][1]!.headers as Headers;+ expect(sentHeaders.get("x-client-ip-token")).toBe(+ "signed-ip-203.0.113.7",+ );+ expect(sentHeaders.get("x-forwarded-for")).toBeNull();+ },+ );++ it.each([+ ["api/store/agents", ""],+ ["api/store/agents", "?search_query=%20"],+ ["api/v1/items", "?query=sheets"],+ ])(+ "sends no client IP token to %s%s, even one the browser sent",+ async (route, query) => {+ vi.mocked(fetch).mockResolvedValue(+ new Response("{}", {+ status: 200,+ headers: { "Content-Type": "application/json" },+ }),+ );++ const req = new NextRequest(+ `https://app.test/api/proxy/${route}${query}`,+ {+ headers: {+ "x-forwarded-for": "203.0.113.7",+ "x-client-ip-token": "forged",+ },+ },+ );+ await GET(req, makeParams(route.split("/")));++ const sentHeaders = vi.mocked(fetch).mock.calls[0][1]!.headers as Headers;+ expect(sentHeaders.get("x-client-ip-token")).toBeNull();+ expect(getClientIPToken).not.toHaveBeenCalled();+ },+ );+it("omits Authorization header when no token is available", async () => {vi.mocked(getServerAuthToken).mockResolvedValueOnce(null);vi.mocked(fetch).mockResolvedValue(diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.tsindex 9f35b49031..ac14e0627d 100644--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts@@ -21,6 +21,18 @@ export function isPrivateStoreMediaRequest(path: string[]): boolean {);}+// The store searches the backend limits per visitor, by the signed client IP.+export function isStoreSearchRequest(+ path: string[],+ params: URLSearchParams,+): boolean {+ const route = path.join("/");+ if (route === "api/store/search") return true;+ return (+ route === "api/store/agents" && Boolean(params.get("search_query")?.trim())+ );+}+export function isPrivateStoreVideoRequest(path: string[]): boolean {return isPrivateStoreMediaRequest(path) && path[5] === "videos";}diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.tsindex a7f55926f0..bf5aee1320 100644--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts@@ -1,9 +1,11 @@import {API_KEY_HEADER_NAME,CLIENT_COUNTRY_TOKEN_HEADER_NAME,+ CLIENT_IP_TOKEN_HEADER_NAME,IMPERSONATION_HEADER_NAME,VERCEL_COUNTRY_HEADER_NAME,} from "@/lib/constants";+import { getClientIPToken } from "@/lib/auth/client-ip-token";import { getCountryToken } from "@/lib/auth/country-token";import { getServerAuthToken } from "@/lib/auth/server/getServerAuthToken";import { environment } from "@/services/environment";@@ -18,6 +20,7 @@ import {getWorkspaceDownloadErrorMessage,isPrivateStoreMediaRequest,isPrivateStoreVideoRequest,+ isStoreSearchRequest,isWorkspaceDownloadRequest,watchResponseStart,} from "./route.helpers";@@ -104,6 +107,7 @@ async function signCountry(country: string) {async function buildForwardHeaders(req: NextRequest,token: string | null,+ path: string[],): Promise<Headers> {const headers = new Headers();for (const name of FORWARDED_REQUEST_HEADERS) {@@ -117,6 +121,10 @@ async function buildForwardHeaders(const country = req.headers.get(VERCEL_COUNTRY_HEADER_NAME);const countryToken = country ? await signCountry(country) : null;if (countryToken) headers.set(CLIENT_COUNTRY_TOKEN_HEADER_NAME, countryToken);+ if (isStoreSearchRequest(path, req.nextUrl.searchParams)) {+ const ipToken = await getClientIPToken(req.headers);+ if (ipToken) headers.set(CLIENT_IP_TOKEN_HEADER_NAME, ipToken);+ }if (token) {headers.set("authorization", `Bearer ${token}`);}@@ -240,7 +248,7 @@ async function handler(return await handleWorkspaceDownload(backendUrl, token);}- const headers = await buildForwardHeaders(req, token);+ const headers = await buildForwardHeaders(req, token, path);if (isPrivateMedia) {headers.set("cache-control", "no-store");}diff --git a/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.ts b/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.tsindex 31d18739b5..98d39b2cdc 100644--- a/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.ts+++ b/autogpt_platform/frontend/src/lib/__tests__/sentry-filters.test.ts@@ -14,6 +14,7 @@ import {logClientRequestFailure,resetClientRequestFailureLog,} from "@/app/api/mutators/request-failure-log";+import { CLIENT_IP_TOKEN_HEADER_NAME } from "@/lib/constants";import {CLIENT_IGNORE_ERRORS,@@ -201,6 +202,8 @@ describe("Sentry user privacy filters", () => {"http.request.header.cf_connecting_ip","http.request.header.true_client_ip","http.request.header.x_client_ip",+ // The frontend's signed client-IP token carries the visitor's IP too.+ `http.request.header.${CLIENT_IP_TOKEN_HEADER_NAME.toLowerCase().replace(/-/g, "_")}`,"http.request.header.x_vercel_forwarded_for","http.request.header.x_vercel_proxied_for","http.request.header.x_vercel_ip_city",diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/client-ip-token.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/client-ip-token.test.tsnew file mode 100644index 0000000000..26fa649caa--- /dev/null+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/client-ip-token.test.ts@@ -0,0 +1,81 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const mintServiceTokenMock = vi.fn();+vi.mock("../service-token", () => ({+ mintServiceToken: (...args: unknown[]) => mintServiceTokenMock(...args),+}));++import { CLIENT_IP_SCOPE, getClientIPToken } from "../client-ip-token";++function forwardedFor(value: string) {+ return new Headers({ "x-forwarded-for": value });+}++beforeEach(() => {+ vi.stubEnv("VERCEL", "1");+ vi.useFakeTimers();+ mintServiceTokenMock.mockReset();+ mintServiceTokenMock.mockImplementation(+ async (_scope: string, claims: { ip: string }) =>+ `signed-${claims.ip}-${Date.now()}`,+ );+});++afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllEnvs();+});++describe("getClientIPToken", () => {+ it("signs the visitor's address, the first forwarded one, under the client-ip scope", async () => {+ vi.setSystemTime(1_000_000);++ const token = await getClientIPToken(+ forwardedFor(" 203.0.113.7 , 10.0.0.1"),+ );++ expect(token).toBe("signed-203.0.113.7-1000000");+ expect(mintServiceTokenMock).toHaveBeenCalledWith(CLIENT_IP_SCOPE, {+ ip: "203.0.113.7",+ });+ });++ it("reuses a visitor's token for 30 seconds, then mints a fresh one", async () => {+ vi.setSystemTime(2_000_000);+ const first = await getClientIPToken(forwardedFor("2001:db8::1"));++ vi.setSystemTime(2_000_000 + 29_000);+ expect(await getClientIPToken(forwardedFor("2001:db8::1"))).toBe(first);++ vi.setSystemTime(2_000_000 + 30_000);+ expect(await getClientIPToken(forwardedFor("2001:db8::1"))).not.toBe(first);+ expect(mintServiceTokenMock).toHaveBeenCalledTimes(2);+ });++ it("signs nothing without a usable address", async () => {+ expect(await getClientIPToken(new Headers())).toBeNull();+ for (const junk of ["", "unknown", "203.0.113.7; DROP", "a".repeat(46)]) {+ expect(await getClientIPToken(forwardedFor(junk))).toBeNull();+ }+ expect(mintServiceTokenMock).not.toHaveBeenCalled();+ });++ it("signs an address only where the proxy in front vouches for it", async () => {+ vi.stubEnv("VERCEL", "");+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "");+ vi.stubEnv("TRUST_X_FORWARDED_FOR", "");++ expect(await getClientIPToken(forwardedFor("198.51.100.8"))).toBeNull();+ expect(mintServiceTokenMock).not.toHaveBeenCalled();++ vi.stubEnv("TRUST_X_FORWARDED_FOR", "true");+ expect(await getClientIPToken(forwardedFor("198.51.100.8"))).not.toBeNull();+ });++ it("answers null when signing fails", async () => {+ vi.spyOn(console, "error").mockImplementation(() => {});+ mintServiceTokenMock.mockRejectedValueOnce(new Error("no jwks"));++ expect(await getClientIPToken(forwardedFor("198.51.100.4"))).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/lib/auth/client-ip-token.ts b/autogpt_platform/frontend/src/lib/auth/client-ip-token.tsnew file mode 100644index 0000000000..e0b5453a4f--- /dev/null+++ b/autogpt_platform/frontend/src/lib/auth/client-ip-token.ts@@ -0,0 +1,44 @@+import { environment } from "@/services/environment";+import { mintServiceToken } from "./service-token";++export const CLIENT_IP_SCOPE = "client-ip";++// As for the country token: reuse a token for half its 60s life. Cleared when+// full, so one instance never holds more than this many visitors.+const REUSE_MS = 30_000;+const MAX_CACHED = 1_000;++const minted = new Map<string, { token: string; mintedAt: number }>();++/**+ * The visitor's IP, as a proxy in front of this server reported it, in a frontend+ * service token the backend can verify. Backend limits on public routes count per+ * visitor by it. Null when no trusted proxy reports the address, when it is not+ * usable, or when signing fails; the backend then keys on its own peer address.+ */+export async function getClientIPToken(requestHeaders: Headers) {+ const ip = readClientIP(requestHeaders);+ if (!ip) return null;+ const cached = minted.get(ip);+ if (cached && Date.now() - cached.mintedAt < REUSE_MS) return cached.token;+ try {+ const token = await mintServiceToken(CLIENT_IP_SCOPE, { ip });+ if (minted.size >= MAX_CACHED) minted.clear();+ minted.set(ip, { token, mintedAt: Date.now() });+ return token;+ } catch (error) {+ console.error("[client-ip] Could not sign the client IP", error);+ return null;+ }+}++// x-forwarded-for is the visitor's address only where the proxy in front of this+// server overwrites it: Vercel's edge does, and so does one an operator vouches for+// with TRUST_X_FORWARDED_FOR. Next's own server keeps whatever the caller sent.+function readClientIP(requestHeaders: Headers) {+ if (!environment.isVercel() && process.env.TRUST_X_FORWARDED_FOR !== "true") {+ return null;+ }+ const ip = requestHeaders.get("x-forwarded-for")?.split(",")[0]?.trim();+ return ip && /^[0-9A-Fa-f:.]{2,45}$/.test(ip) ? ip : null;+}diff --git a/autogpt_platform/frontend/src/lib/constants.ts b/autogpt_platform/frontend/src/lib/constants.tsindex ad00c64de1..57f9f26826 100644--- a/autogpt_platform/frontend/src/lib/constants.ts+++ b/autogpt_platform/frontend/src/lib/constants.ts@@ -12,6 +12,7 @@ export const IMPERSONATION_COOKIE_NAME = "admin-impersonate-user-id";// API key authenticationexport const API_KEY_HEADER_NAME = "X-API-Key";export const CLIENT_COUNTRY_TOKEN_HEADER_NAME = "X-Client-Country-Token";+export const CLIENT_IP_TOKEN_HEADER_NAME = "X-Client-IP-Token";export const VERCEL_COUNTRY_HEADER_NAME = "x-vercel-ip-country";// Routesdiff --git a/autogpt_platform/single-container/run-frontend.sh b/autogpt_platform/single-container/run-frontend.shindex dd9c7f0065..a3cfab8301 100755--- a/autogpt_platform/single-container/run-frontend.sh+++ b/autogpt_platform/single-container/run-frontend.sh@@ -53,7 +53,7 @@ build_frontend_environment() {NEXT_TELEMETRY_DISABLED=1PORT=3001HOSTNAME=127.0.0.1- # nginx overwrites X-Forwarded-For with the peer address, so it is the client IP.+ # Only nginx reaches 127.0.0.1:3001, and it overwrites X-Forwarded-For.TRUST_X_FORWARDED_FOR=true)