Skip to content

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.

refactor(backend): consolidate LaunchDarkly feature flag management (#10632)

Zamil Majdya2059c6023c4
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/__init__.py b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py
deleted file mode 100644
index ac0a3bc6ef..0000000000
--- a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py
+++ /dev/null
@@ -1,84 +0,0 @@
-import pytest
-from ldclient import LDClient
-
-from autogpt_libs.feature_flag.client import (
- feature_flag,
- is_feature_enabled,
- mock_flag_variation,
-)
-
-
-def ld_client(mocker):
- client = mocker.Mock(spec=LDClient)
- mocker.patch("ldclient.get", return_value=client)
- client.is_initialized.return_value = True
- return client
-
-
-async def test_feature_flag_enabled(ld_client):
- ld_client.variation.return_value = True
-
- @feature_flag("test-flag")
- async def test_function(user_id: str):
- return "success"
-
- result = test_function(user_id="test-user")
- assert result == "success"
- ld_client.variation.assert_called_once()
-
-
-async def test_feature_flag_unauthorized_response(ld_client):
- ld_client.variation.return_value = False
-
- @feature_flag("test-flag")
- async def test_function(user_id: str):
- return "success"
-
- result = test_function(user_id="test-user")
- assert result == {"error": "disabled"}
-
-
-def test_mock_flag_variation(ld_client):
- with mock_flag_variation("test-flag", True):
- assert ld_client.variation("test-flag", None, False)
-
- with mock_flag_variation("test-flag", False):
- assert ld_client.variation("test-flag", None, False)
-
-
-def test_is_feature_enabled(ld_client):
- """Test the is_feature_enabled helper function."""
- ld_client.is_initialized.return_value = True
- ld_client.variation.return_value = True
-
- result = is_feature_enabled("test-flag", "user123", default=False)
- assert result is True
-
- ld_client.variation.assert_called_once()
- call_args = ld_client.variation.call_args
- assert call_args[0][0] == "test-flag" # flag_key
- assert call_args[0][2] is False # default value
-
-
-def test_is_feature_enabled_not_initialized(ld_client):
- """Test is_feature_enabled when LaunchDarkly is not initialized."""
- ld_client.is_initialized.return_value = False
-
- result = is_feature_enabled("test-flag", "user123", default=True)
- assert result is True # Should return default
-
- ld_client.variation.assert_not_called()
-
-
-def test_is_feature_enabled_exception(mocker):
- """Test is_feature_enabled when get_client() raises an exception."""
- mocker.patch(
- "autogpt_libs.feature_flag.client.get_client",
- side_effect=Exception("Client error"),
- )
-
- result = is_feature_enabled("test-flag", "user123", default=True)
- assert result is True # Should return default
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/config.py b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/config.py
deleted file mode 100644
index e01c285d1e..0000000000
--- a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/config.py
+++ /dev/null
@@ -1,15 +0,0 @@
-from pydantic import Field
-from pydantic_settings import BaseSettings, SettingsConfigDict
-
-
-class Settings(BaseSettings):
- launch_darkly_sdk_key: str = Field(
- default="",
- description="The Launch Darkly SDK key",
- validation_alias="LAUNCH_DARKLY_SDK_KEY",
- )
-
- model_config = SettingsConfigDict(case_sensitive=True, extra="ignore")
-
-
-SETTINGS = Settings()
diff --git a/autogpt_platform/backend/.env.example b/autogpt_platform/backend/.env.example
index 21aef0c95f..dc15c031b8 100644
--- a/autogpt_platform/backend/.env.example
+++ b/autogpt_platform/backend/.env.example
@@ -72,6 +72,8 @@ TURNSTILE_SECRET_KEY=
## This is the verify URL
TURNSTILE_VERIFY_URL=https://challenges.cloudflare.com/turnstile/v0/siteverify
+LAUNCH_DARKLY_SDK_KEY=
+
## == INTEGRATION CREDENTIALS == ##
# Each set of server side credentials is required for the corresponding 3rd party
# integration to work.
diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator.py b/autogpt_platform/backend/backend/executor/activity_status_generator.py
index eea0faa449..f45acdd81f 100644
--- a/autogpt_platform/backend/backend/executor/activity_status_generator.py
+++ b/autogpt_platform/backend/backend/executor/activity_status_generator.py
@@ -6,20 +6,17 @@ import json
import logging
from typing import TYPE_CHECKING, Any, NotRequired, TypedDict
-from autogpt_libs.feature_flag.client import is_feature_enabled
from pydantic import SecretStr
from backend.blocks.llm import LlmModel, llm_call
from backend.data.block import get_block
from backend.data.execution import ExecutionStatus, NodeExecutionResult
from backend.data.model import APIKeyCredentials, GraphExecutionStats
+from backend.util.feature_flag import Flag, is_feature_enabled
from backend.util.retry import func_retry
from backend.util.settings import Settings
from backend.util.truncate import truncate
-# LaunchDarkly feature flag key for AI activity status generation
-AI_ACTIVITY_STATUS_FLAG_KEY = "ai-agent-execution-summary"
-
if TYPE_CHECKING:
from backend.executor import DatabaseManagerAsyncClient
@@ -103,9 +100,7 @@ async def generate_activity_status_for_execution(
AI-generated activity status string, or None if feature is disabled
"""
# Check LaunchDarkly feature flag for AI activity status generation with full context support
- if not await is_feature_enabled(
- AI_ACTIVITY_STATUS_FLAG_KEY, user_id, default=False
- ):
+ if not await is_feature_enabled(Flag.AI_ACTIVITY_STATUS, user_id):
logger.debug("AI activity status generation is disabled via LaunchDarkly")
return None
diff --git a/autogpt_platform/backend/backend/server/rest_api.py b/autogpt_platform/backend/backend/server/rest_api.py
index 40ff268042..b414bd4207 100644
--- a/autogpt_platform/backend/backend/server/rest_api.py
+++ b/autogpt_platform/backend/backend/server/rest_api.py
@@ -9,10 +9,6 @@ import fastapi.responses
import pydantic
import starlette.middleware.cors
import uvicorn
-from autogpt_libs.feature_flag.client import (
- initialize_launchdarkly,
- shutdown_launchdarkly,
-)
from autogpt_libs.logging.utils import generate_uvicorn_config
from fastapi.exceptions import RequestValidationError
from fastapi.routing import APIRoute
@@ -41,6 +37,7 @@ from backend.server.external.api import external_app
from backend.server.middleware.security import SecurityHeadersMiddleware
from backend.util import json
from backend.util.cloud_storage import shutdown_cloud_storage_handler
+from backend.util.feature_flag import initialize_launchdarkly, shutdown_launchdarkly
from backend.util.service import UnhealthyServiceError
settings = backend.util.settings.Settings()
diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.py
index eef333aa2a..8460d2eebc 100644
--- a/autogpt_platform/backend/backend/server/routers/v1.py
+++ b/autogpt_platform/backend/backend/server/routers/v1.py
@@ -8,7 +8,6 @@ from typing import Annotated, Any, Sequence
import pydantic
import stripe
from autogpt_libs.auth.middleware import auth_middleware
-from autogpt_libs.feature_flag.client import feature_flag
from fastapi import (
APIRouter,
Body,
@@ -85,6 +84,7 @@ from backend.server.utils import get_user_id
from backend.util.clients import get_scheduler_client
from backend.util.cloud_storage import get_cloud_storage_handler
from backend.util.exceptions import GraphValidationError, NotFoundError
+from backend.util.feature_flag import feature_flag
from backend.util.settings import Settings
from backend.util.virus_scanner import scan_content_safe
diff --git a/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py b/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
index fa84d041cd..261510fee0 100644
--- a/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
+++ b/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
@@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, Any, Literal
if TYPE_CHECKING:
from backend.executor import DatabaseManagerAsyncClient
-from autogpt_libs.feature_flag.client import is_feature_enabled
from pydantic import ValidationError
from backend.data.execution import ExecutionStatus
@@ -16,6 +15,7 @@ from backend.server.v2.AutoMod.models import (
ModerationConfig,
)
from backend.util.exceptions import ModerationError
+from backend.util.feature_flag import Flag, is_feature_enabled
from backend.util.request import Requests
from backend.util.settings import Settings
@@ -51,7 +51,7 @@ class AutoModManager:
return None
# Check if AutoMod feature is enabled for this user
- if not is_feature_enabled("AutoMod", graph_exec.user_id, default=False):
+ if not await is_feature_enabled(Flag.AUTOMOD, graph_exec.user_id):
logger.debug(f"AutoMod feature not enabled for user {graph_exec.user_id}")
return None
@@ -141,7 +141,7 @@ class AutoModManager:
return None
# Check if AutoMod feature is enabled for this user
- if not is_feature_enabled("AutoMod", user_id, default=False):
+ if not await is_feature_enabled(Flag.AUTOMOD, user_id):
logger.debug(f"AutoMod feature not enabled for user {user_id}")
return None
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py b/autogpt_platform/backend/backend/util/feature_flag.py
similarity index 64%
rename from autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py
rename to autogpt_platform/backend/backend/util/feature_flag.py
index 188ee4198a..9af0453479 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py
+++ b/autogpt_platform/backend/backend/util/feature_flag.py
@@ -1,26 +1,42 @@
import contextlib
import logging
+from enum import Enum
from functools import wraps
from typing import Any, Awaitable, Callable, TypeVar
import ldclient
+from autogpt_libs.utils.cache import async_ttl_cache
from fastapi import HTTPException
from ldclient import Context, LDClient
from ldclient.config import Config
from typing_extensions import ParamSpec
-from autogpt_libs.utils.cache import async_ttl_cache
-
-from .config import SETTINGS
+from backend.util.settings import Settings
logger = logging.getLogger(__name__)
+# Load settings at module level
+settings = Settings()
+
P = ParamSpec("P")
T = TypeVar("T")
_is_initialized = False
+class Flag(str, Enum):
+ """
+ Centralized enum for all LaunchDarkly feature flags.
+
+ Add new flags here to ensure consistency across the codebase.
+ """
+
+ AUTOMOD = "AutoMod"
+ AI_ACTIVITY_STATUS = "ai-agent-execution-summary"
+ BETA_BLOCKS = "beta-blocks"
+ AGENT_ACTIVITY = "agent-activity"
+
+
def get_client() -> LDClient:
"""Get the LaunchDarkly client singleton."""
if not _is_initialized:
@@ -29,7 +45,7 @@ def get_client() -> LDClient:
def initialize_launchdarkly() -> None:
- sdk_key = SETTINGS.launch_darkly_sdk_key
+ sdk_key = settings.secrets.launch_darkly_sdk_key
logger.debug(
f"Initializing LaunchDarkly with SDK key: {'present' if sdk_key else 'missing'}"
)
@@ -79,6 +95,8 @@ async def _fetch_user_context_data(user_id: str) -> Context:
builder.anonymous(False)
if user.role:
builder.set("role", user.role)
+ # It's weird, I know, but it is what it is.
+ builder.set("custom", {"role": user.role})
if user.email:
builder.set("email", user.email)
builder.set("email_domain", user.email.split("@")[-1])
@@ -89,13 +107,16 @@ async def _fetch_user_context_data(user_id: str) -> Context:
return builder.build()
-async def is_feature_enabled(
+async def get_feature_flag_value(
flag_key: str,
user_id: str,
- default: bool = False,
-) -> bool:
+ default: Any = None,
+) -> Any:
"""
- Check if a feature flag is enabled for a user.
+ Get the raw value of a feature flag for a user.
+
+ This is the generic function that returns the actual flag value,
+ which could be a boolean, string, number, or JSON object.
Args:
flag_key: The LaunchDarkly feature flag key
@@ -103,18 +124,27 @@ async def is_feature_enabled(
default: Default value if LaunchDarkly is unavailable or flag evaluation fails
Returns:
- True if feature is enabled, False otherwise
+ The flag value from LaunchDarkly
"""
try:
client = get_client()
+ # Check if client is initialized
+ if not client.is_initialized():
+ logger.debug(
+ f"LaunchDarkly not initialized, using default={default} for {flag_key}"
+ )
+ return default
+
# Get user context from Supabase
context = await _fetch_user_context_data(user_id)
# Evaluate flag
result = client.variation(flag_key, context, default)
- logger.debug(f"Feature flag {flag_key} for user {user_id}: {result}")
+ logger.debug(
+ f"Feature flag {flag_key} for user {user_id}: {result} (type: {type(result).__name__})"
+ )
return result
except Exception as e:
@@ -124,6 +154,39 @@ async def is_feature_enabled(
return default
+async def is_feature_enabled(
+ flag_key: Flag,
+ user_id: str,
+ default: bool = False,
+) -> bool:
+ """
+ Check if a feature flag is enabled for a user.
+
+ Args:
+ flag_key: The Flag enum value
+ user_id: The user ID to evaluate the flag for
+ default: Default value if LaunchDarkly is unavailable or flag evaluation fails
+
+ Returns:
+ True if feature is enabled, False otherwise
+ """
+ result = await get_feature_flag_value(flag_key.value, user_id, default)
+
+ # If the result is already a boolean, return it
+ if isinstance(result, bool):
+ return result
+
+ # Log a warning if the flag is not returning a boolean
+ logger.warning(
+ f"Feature flag {flag_key} returned non-boolean value: {result} (type: {type(result).__name__}). "
+ f"This flag should be configured as a boolean in LaunchDarkly. Using default={default}"
+ )
+
+ # Return the default if we get a non-boolean value
+ # This prevents objects from being incorrectly treated as True
+ return default
+
+
def feature_flag(
flag_key: str,
default: bool = False,
@@ -153,10 +216,20 @@ def feature_flag(
)
is_enabled = default
else:
- # Use the simplified function
- is_enabled = await is_feature_enabled(
+ # Use the internal function directly since we have a raw string flag_key
+ flag_value = await get_feature_flag_value(
flag_key, str(user_id), default
)
+ # Ensure we treat flag value as boolean
+ if isinstance(flag_value, bool):
+ is_enabled = flag_value
+ else:
+ # Log warning and use default for non-boolean values
+ logger.warning(
+ f"Feature flag {flag_key} returned non-boolean value: {flag_value} (type: {type(flag_value).__name__}). "
+ f"Using default={default}"
+ )
+ is_enabled = default
if not is_enabled:
raise HTTPException(status_code=404, detail="Feature not available")
diff --git a/autogpt_platform/backend/backend/util/feature_flag_test.py b/autogpt_platform/backend/backend/util/feature_flag_test.py
new file mode 100644
index 0000000000..9bd99809ff
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/feature_flag_test.py
@@ -0,0 +1,113 @@
+import pytest
+from fastapi import HTTPException
+from ldclient import LDClient
+
+from backend.util.feature_flag import (
+ Flag,
+ feature_flag,
+ is_feature_enabled,
+ mock_flag_variation,
+)
+
+
+def ld_client(mocker):
+ client = mocker.Mock(spec=LDClient)
+ mocker.patch("ldclient.get", return_value=client)
+ client.is_initialized.return_value = True
+ return client
+
+
+async def test_feature_flag_enabled(ld_client):
+ ld_client.variation.return_value = True
+
+ @feature_flag("test-flag")
+ async def test_function(user_id: str):
+ return "success"
+
+ result = await test_function(user_id="test-user")
+ assert result == "success"
+ ld_client.variation.assert_called_once()
+
+
+async def test_feature_flag_unauthorized_response(ld_client):
+ ld_client.variation.return_value = False
+
+ @feature_flag("test-flag")
+ async def test_function(user_id: str):
+ return "success"
+
+ with pytest.raises(HTTPException) as exc_info:
+ await test_function(user_id="test-user")
+ assert exc_info.value.status_code == 404
+
+
+def test_mock_flag_variation(ld_client):
+ with mock_flag_variation("test-flag", True):
+ assert ld_client.variation("test-flag", None, False) is True
+
+ with mock_flag_variation("test-flag", False):
+ assert ld_client.variation("test-flag", None, True) is False
+
+
+async def test_is_feature_enabled(ld_client):
+ """Test the is_feature_enabled helper function."""
+ ld_client.is_initialized.return_value = True
+ ld_client.variation.return_value = True
+
+ result = await is_feature_enabled(Flag.AUTOMOD, "user123", default=False)
+ assert result is True
+
+ ld_client.variation.assert_called_once()
+ call_args = ld_client.variation.call_args
+ assert call_args[0][0] == "AutoMod" # flag_key
+ assert call_args[0][2] is False # default value
+
+
+async def test_is_feature_enabled_not_initialized(ld_client):
+ """Test is_feature_enabled when LaunchDarkly is not initialized."""
+ ld_client.is_initialized.return_value = False
+
+ result = await is_feature_enabled(Flag.AGENT_ACTIVITY, "user123", default=True)
+ assert result is True # Should return default
+
+ ld_client.variation.assert_not_called()
+
+
+async def test_is_feature_enabled_exception(mocker):
+ """Test is_feature_enabled when get_client() raises an exception."""
+ mocker.patch(
+ "backend.util.feature_flag.get_client",
+ side_effect=Exception("Client error"),
+ )
+
+ result = await is_feature_enabled(Flag.AGENT_ACTIVITY, "user123", default=True)
+ assert result is True # Should return default
+
+
+def test_flag_enum_values():
+ """Test that Flag enum has expected values."""
+ assert Flag.AUTOMOD == "AutoMod"
+ assert Flag.AI_ACTIVITY_STATUS == "ai-agent-execution-summary"
+ assert Flag.BETA_BLOCKS == "beta-blocks"
+ assert Flag.AGENT_ACTIVITY == "agent-activity"
+
+
+async def test_is_feature_enabled_with_flag_enum(mocker):
+ """Test is_feature_enabled function with Flag enum."""
+ mock_get_feature_flag_value = mocker.patch(
+ "backend.util.feature_flag.get_feature_flag_value"
+ )
+ mock_get_feature_flag_value.return_value = True
+
+ result = await is_feature_enabled(Flag.AUTOMOD, "user123")
+
+ assert result is True
+ # Should call with the flag's string value
+ mock_get_feature_flag_value.assert_called_once()
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index f5b872c352..1117037dfe 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -525,6 +525,12 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
# AutoMod API credentials
automod_api_key: str = Field(default="", description="AutoMod API key")
+ # LaunchDarkly feature flags
+ launch_darkly_sdk_key: str = Field(
+ default="",
+ description="The LaunchDarkly SDK key for feature flag management",
+ )
+
ayrshare_api_key: str = Field(default="", description="Ayrshare API Key")
ayrshare_jwt_key: str = Field(default="", description="Ayrshare private Key")
# Add more secret fields as needed