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): the frontend can read its feature flags from the backend (#15383)
Reinier van der Leer92f70dc31d0f
diff --git a/autogpt_platform/backend/backend/api/features/feature_flags/__init__.py b/autogpt_platform/backend/backend/api/features/feature_flags/__init__.pynew file mode 100644index 0000000000..8b13789179--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/feature_flags/__init__.py@@ -0,0 +1 @@+diff --git a/autogpt_platform/backend/backend/api/features/feature_flags/routes.py b/autogpt_platform/backend/backend/api/features/feature_flags/routes.pynew file mode 100644index 0000000000..c8f29d4323--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/feature_flags/routes.py@@ -0,0 +1,26 @@+from typing import Annotated++from autogpt_libs.auth import get_optional_user_id+from fastapi import APIRouter, Query, Security++from backend.util.feature_flag.frontend import (+ FrontendFlagValues,+ evaluate_frontend_flags,+)++router = APIRouter(prefix="/feature-flags", tags=["feature-flags"])++[email protected]("", operation_id="get_feature_flags")+async def get_feature_flags(+ user_id: Annotated[str | None, Security(get_optional_user_id)] = None,+ visitor_id: Annotated[+ str | None,+ Query(+ description="A signed-out visitor's analytics id, sent only with their "+ "analytics consent; ignored for a signed-in caller or when malformed."+ ),+ ] = None,+) -> FrontendFlagValues:+ """The frontend's flags for the caller, evaluated as the backend's own reads are."""+ return await evaluate_frontend_flags(user_id, visitor_id)diff --git a/autogpt_platform/backend/backend/api/features/feature_flags/routes_test.py b/autogpt_platform/backend/backend/api/features/feature_flags/routes_test.pynew file mode 100644index 0000000000..ca583e3ace--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/feature_flags/routes_test.py@@ -0,0 +1,471 @@+import asyncio+import os+import uuid+from typing import Any, ClassVar+from unittest.mock import AsyncMock++import fastapi+import httpx+import pytest+from autogpt_libs.auth import get_optional_user_id+from ldclient import Context, LDClient+from ldclient.config import Config as LDConfig+from ldclient.integrations.test_data import TestData+from ldclient.interfaces import DataSourceState, EventProcessor, UpdateProcessor+from posthog import Posthog+from posthog.types import FeatureFlag++import backend.util.feature_flag as ff+from backend.util.feature_flag import (+ Flag,+ frontend,+ get_feature_flag_value,+ is_feature_enabled,+)+from backend.util.feature_flag import posthog as ph+from backend.util.feature_flag.frontend import (+ FrontendFlag,+ _anonymous_flags,+ _defined_flags,+)+from backend.util.settings import FeatureFlagBackend++from .routes import router++SEARCH_TERMS = ["Research", "Marketing"]+VISITOR_TERMS = ["Sales"]+VISITOR = "0b6c1f3e-visitor-0001"+USER_ID = "3f2b8c1e-7a4d-4e9b-9c2a-5d6e7f8a9b0c"+PINNING = FrontendFlag.CHAT_PINNING.value+HIRE_EXPERTS = FrontendFlag.HIRE_EXPERTS.value+CHAT_SEARCH = FrontendFlag.CHAT_SEARCH.value+STREAM_SHADOW = FrontendFlag.COPILOT_STREAM_SHADOW.value++app = fastapi.FastAPI()+app.include_router(router, prefix="/api")++[email protected](autouse=True)+def launchdarkly(mocker, monkeypatch: pytest.MonkeyPatch):+ """A real LaunchDarkly client serving fixed targeting, and no env overrides.++ Yields every analytics event the client would have sent LaunchDarkly.+ """+ for name in list(os.environ):+ if name.startswith(+ ("FORCE_FLAG_", "NEXT_PUBLIC_FORCE_FLAG_", "FORCE_ALL_FLAGS")+ ):+ monkeypatch.delenv(name)+ td = TestData.data_source()+ td.update(+ td.flag(FrontendFlag.COPILOT_VOICE_MODE.value)+ .boolean_flag()+ .variation_for_user("u-staff", True)+ .fallthrough_variation(False)+ )+ td.update(+ td.flag(FrontendFlag.HIRE_EXPERTS.value).boolean_flag().variation_for_all(True)+ )+ td.update(+ td.flag(FrontendFlag.MARKETPLACE_SEARCH_TERMS.value)+ .variations(SEARCH_TERMS, VISITOR_TERMS)+ .variation_for_user(f"visitor:{VISITOR}", 1)+ .fallthrough_variation(0)+ )+ td.update(+ td.flag(PINNING)+ .boolean_flag()+ .variation_for_user(f"visitor:{VISITOR}", True)+ .variation_for_user(f"visitor:{'v' * 65}", True)+ .variation_for_user("visitor:visitor:bad$id", True)+ .variation_for_user(USER_ID, True)+ .if_match("email_domain", "agpt.co")+ .then_return(True)+ .fallthrough_variation(False)+ )+ _RecordedEvents.sent.clear()+ client = LDClient(+ LDConfig(+ "sdk-test", update_processor_class=td, event_processor_class=_RecordedEvents+ )+ )+ mocker.patch.object(+ ff.settings.config, "feature_flag_backend", FeatureFlagBackend.LAUNCHDARKLY+ )+ mocker.patch.object(ff.settings.secrets, "launch_darkly_sdk_key", "sdk-test")+ monkeypatch.setattr(ff, "_init_attempted", True)+ mocker.patch("backend.util.feature_flag.ldclient.get", return_value=client)+ _anonymous_flags.cache_clear()+ _defined_flags.cache_clear()+ yield _RecordedEvents.sent+ _anonymous_flags.cache_clear()+ _defined_flags.cache_clear()+ app.dependency_overrides.clear()+ client.close()++[email protected]("caller", ["u-staff", "u-other", None])+async def test_each_flag_answers_what_the_backends_own_read_answers(caller):+ body = await _get_flags(caller)++ as_backend = caller or "anonymous"+ assert body["configured"] is True+ assert body["flags"][+ FrontendFlag.COPILOT_VOICE_MODE.value+ ] is await is_feature_enabled(Flag.COPILOT_VOICE_MODE, as_backend)+ assert body["flags"][+ FrontendFlag.MARKETPLACE_SEARCH_TERMS.value+ ] == await get_feature_flag_value(+ FrontendFlag.MARKETPLACE_SEARCH_TERMS.value, as_backend+ )+++async def test_a_flag_the_vendor_has_no_definition_of_is_answered_null():+ flags = (await _get_flags("u-staff"))["flags"]++ # Null serves the page's default now; left out, the page would wait for good.+ assert (+ flags.get(CHAT_SEARCH, "left out"),+ flags.get(STREAM_SHADOW, "left out"),+ flags[HIRE_EXPERTS],+ ) == (None, None, True)+++async def test_a_defined_flag_the_vendor_could_not_answer_is_left_out(mocker):+ # A failed user lookup makes every answer a guess about someone else.+ mocker.patch(+ "backend.util.feature_flag._fetch_user_context_status",+ AsyncMock(+ return_value=(Context.builder(USER_ID).anonymous(True).build(), False)+ ),+ )++ flags = (await _get_flags(USER_ID))["flags"]++ # A default here would read as an answer and 404 a gated route for a user who has it.+ assert (HIRE_EXPERTS in flags, flags.get(CHAT_SEARCH, "left out")) == (False, None)++[email protected]("ready_first", [False, True])+async def test_a_signed_out_answer_is_kept_long_only_when_complete(+ mocker, monkeypatch, ready_first+):+ """A vendor that is down or starting is asked again within seconds, while a+ complete answer keeps serving every signed-out visitor from one read."""+ monkeypatch.setattr(frontend, "PARTIAL_TTL_SECONDS", 0.01)+ ready = ff.ldclient.get()+ not_ready = LDClient(LDConfig("sdk-test", offline=True))+ get = mocker.patch(+ "backend.util.feature_flag.ldclient.get",+ return_value=ready if ready_first else not_ready,+ )+ try:+ first = (await _get_flags(None))["flags"]+ get.return_value = not_ready if ready_first else ready+ await asyncio.sleep(0.05)+ second = (await _get_flags(None))["flags"]+ finally:+ not_ready.close()++ # Either way the second read sees the first, complete answer.+ assert (HIRE_EXPERTS in first, second.get(HIRE_EXPERTS)) == (ready_first, True)+++ "data_source,body",+ [+ # A rejected key ends LaunchDarkly's attempts; the route gates then serve+ # defaults, and the page must too rather than poll forever.+ ("_RejectedKey", {"flags": {}, "configured": False}),+ # Still connecting past the start-up wait: the page keeps asking.+ ("_StillConnecting", {"flags": {}, "configured": True}),+ ],+)+async def test_a_vendor_that_cannot_answer_says_whether_it_ever_will(+ mocker, data_source, body+):+ client = LDClient(+ LDConfig(+ "sdk-test", update_processor_class=globals()[data_source], send_events=False+ ),+ start_wait=0,+ )+ mocker.patch("backend.util.feature_flag.ldclient.get", return_value=client)+ try:+ answer = await _get_flags("u-staff")+ finally:+ client.close()++ assert answer == body+++async def test_a_launchdarkly_client_not_ready_yet_leaves_every_flag_out(mocker):+ # Offline, the client answers every read as CLIENT_NOT_READY, as while it starts.+ client = LDClient(LDConfig("sdk-test", offline=True))+ mocker.patch("backend.util.feature_flag.ldclient.get", return_value=client)+ try:+ flags = (await _get_flags("u-staff"))["flags"]+ finally:+ client.close()++ assert flags == {}+++async def test_a_signed_in_read_asks_posthog_nothing_about_flags_it_lacks(mocker):+ """Each would be a remote /flags call on the executor every flag read shares."""+ mocker.patch.object(+ ff.settings.config, "feature_flag_backend", FeatureFlagBackend.POSTHOG+ )+ client = _posthog_splitting_pinning_and_gating_hire_on_email()+ remote = mocker.patch.object(+ client, "_get_flags_decision", side_effect=ConnectionError+ )+ mocker.patch.object(client, "capture")+ mocker.patch.object(ph, "get_flag_client", return_value=client)+ try:+ flags = (await _get_flags("u-staff"))["flags"]+ finally:+ client.shutdown()++ # hire-experts is defined but needs /flags; nothing else may ask.+ assert (remote.call_count, flags.get(CHAT_SEARCH, "left out")) == (1, None)++[email protected]("loaded", [True, False])+async def test_on_posthog_only_loaded_definitions_can_rule_a_flag_undefined(+ mocker, loaded+):+ mocker.patch.object(+ ff.settings.config, "feature_flag_backend", FeatureFlagBackend.POSTHOG+ )+ client = _posthog_splitting_pinning_and_gating_hire_on_email()+ if not loaded:+ client.feature_flags = None+ mocker.patch.object(client, "load_feature_flags")+ mocker.patch.object(client, "_get_flags_decision", side_effect=ConnectionError)+ mocker.patch.object(ph, "get_flag_client", return_value=client)+ try:+ flags = (await _get_flags("u-staff"))["flags"]+ finally:+ client.shutdown()++ # hire-experts is defined but needs /flags, which is down: left out either way.+ expected = (False, None) if loaded else (False, "left out")+ assert (HIRE_EXPERTS in flags, flags.get(CHAT_SEARCH, "left out")) == expected+++async def test_with_no_vendor_configured_every_flag_is_the_frontends_default(mocker):+ mocker.patch.object(ff.settings.secrets, "launch_darkly_sdk_key", "")++ body = await _get_flags("u-staff")++ assert body["configured"] is False+++ "user_id,visitor_id,terms",+ [("u-other", None, SEARCH_TERMS), (None, VISITOR, VISITOR_TERMS)],+)+async def test_an_env_override_forces_a_switch_but_never_a_payload(+ monkeypatch, user_id, visitor_id, terms+):+ monkeypatch.setenv("FORCE_FLAG_COPILOT_VOICE_MODE", "true")+ monkeypatch.setenv("FORCE_FLAG_MARKETPLACE_SEARCH_TERMS", "true")+ # Forced although the vendor has no definition of it.+ monkeypatch.setenv("FORCE_FLAG_CHAT_SEARCH", "true")++ flags = (await _get_flags(user_id, visitor_id))["flags"]++ assert flags[FrontendFlag.COPILOT_VOICE_MODE.value] is True+ assert flags[FrontendFlag.MARKETPLACE_SEARCH_TERMS.value] == terms+ assert flags[CHAT_SEARCH] is True+++async def test_a_consenting_visitor_is_bucketed_by_their_own_id():+ own = (await _get_flags(None, VISITOR))["flags"]+ shared = (await _get_flags(None))["flags"]++ assert own[PINNING] is True+ assert shared[PINNING] is False+++async def test_a_signed_in_caller_is_never_bucketed_by_a_visitor_id():+ flags = (await _get_flags("u-other", VISITOR))["flags"]++ assert flags[PINNING] is False++[email protected]("malformed", ["v" * 65, "visitor:bad$id"])+async def test_a_malformed_visitor_id_counts_as_none(malformed):+ flags = (await _get_flags(None, malformed))["flags"]++ assert flags[PINNING] is False+++async def test_a_users_id_sent_as_a_visitor_id_is_never_looked_up_as_that_user(+ mocker,+):+ # The lookup would hand back an agpt.co user, whom the flag's rule turns on.+ employee = Context.builder("u-staff").set("email_domain", "agpt.co").build()+ lookup = mocker.patch(+ "backend.util.feature_flag._fetch_user_context_status",+ AsyncMock(return_value=(employee, True)),+ )++ flags = (await _get_flags(None, USER_ID))["flags"]++ assert flags[PINNING] is False+ assert USER_ID not in [call.args[0] for call in lookup.await_args_list]+++async def test_a_users_id_sent_as_a_visitor_id_never_matches_that_users_targets(+ mocker,+):+ mocker.patch(+ "backend.util.feature_flag._fetch_user_context_status",+ AsyncMock(return_value=(Context.builder(USER_ID).build(), True)),+ )++ as_user = (await _get_flags(USER_ID))["flags"]+ as_visitor = (await _get_flags(None, USER_ID))["flags"]++ assert (as_user[PINNING], as_visitor[PINNING]) == (True, False)+++async def test_fresh_visitor_ids_send_launchdarkly_nothing(launchdarkly):+ await _get_flags(None)+ launchdarkly.clear()++ for _ in range(20):+ await _get_flags(None, str(uuid.uuid4()))++ assert launchdarkly == []+++async def test_fresh_visitor_ids_cost_posthog_nothing(mocker):+ """No billed event, no remote /flags call and no turn on the executor every+ flag read shares: the id is the caller's to pick, so a new one must be free."""+ mocker.patch.object(+ ff.settings.config, "feature_flag_backend", FeatureFlagBackend.POSTHOG+ )+ client = _posthog_splitting_pinning_and_gating_hire_on_email()+ mocker.patch.object(ph, "get_flag_client", return_value=client)+ remote = mocker.patch.object(+ client,+ "_get_flags_decision",+ return_value={+ "flags": {+ HIRE_EXPERTS: FeatureFlag.from_value_and_payload(+ HIRE_EXPERTS, True, None+ )+ }+ },+ )+ events = mocker.patch.object(client, "capture")+ executor = mocker.patch.object(+ ph._read_executor, "submit", wraps=ph._read_executor.submit+ )+ try:+ await _get_flags(None)+ for spy in (remote, events, executor):+ spy.reset_mock()+ visitors = [+ (await _get_flags(None, str(uuid.uuid4())))["flags"] for _ in range(40)+ ]+ finally:+ client.shutdown()++ assert (remote.call_count, events.call_count, executor.call_count) == (0, 0, 0)+ # Each is still bucketed by their own id where the definitions decide it, and+ # takes the shared signed-out answer where only PostHog's servers could.+ assert {v.get(PINNING) for v in visitors} == {True, False}+ assert all(v.get(HIRE_EXPERTS) is True for v in visitors)+++class _StillConnecting(UpdateProcessor):+ """A data source that has not received flags yet."""++ def __init__(self, config: LDConfig, store: Any, ready: Any):+ self.config = config++ def start(self) -> None:+ pass++ def stop(self) -> None:+ pass++ def is_alive(self) -> bool:+ return True++ def initialized(self) -> bool:+ return False+++class _RejectedKey(_StillConnecting):+ """Gives up for good, as LaunchDarkly's own data sources do on a 401."""++ def start(self) -> None:+ sink = self.config.data_source_update_sink+ assert sink is not None+ sink.update_status(DataSourceState.OFF, None)+++class _RecordedEvents(EventProcessor):+ sent: ClassVar[list[Any]] = []++ def __init__(self, config: LDConfig):+ pass++ def send_event(self, event: Any) -> None:+ self.sent.append(event)++ def flush(self) -> None:+ pass++ def stop(self) -> None:+ pass+++def _posthog_splitting_pinning_and_gating_hire_on_email() -> Posthog:+ """Local definitions: pinning is a 50% split, hire-experts needs an email."""+ client = Posthog("phc-test", secret_key="phx-test", enable_local_evaluation=False)+ client.feature_flags = [+ {+ "id": 1,+ "key": PINNING,+ "active": True,+ "filters": {"groups": [{"properties": [], "rollout_percentage": 50}]},+ },+ {+ "id": 2,+ "key": HIRE_EXPERTS,+ "active": True,+ "filters": {+ "groups": [+ {+ "properties": [+ {+ "key": "email",+ "type": "person",+ "operator": "regex",+ "value": r"@agpt\.co$",+ }+ ],+ "rollout_percentage": 100,+ }+ ]+ },+ },+ ]+ return client+++async def _get_flags(user_id: str | None, visitor_id: str | None = None) -> dict:+ app.dependency_overrides[get_optional_user_id] = lambda: user_id+ params = {"visitor_id": visitor_id} if visitor_id else {}+ async with httpx.AsyncClient(+ transport=httpx.ASGITransport(app=app), base_url="http://test"+ ) as client:+ response = await client.get("/api/feature-flags", params=params)+ assert response.status_code == 200+ return response.json()diff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.pyindex 19c093f4cd..ff5d6305df 100644--- a/autogpt_platform/backend/backend/api/rest_api.py+++ b/autogpt_platform/backend/backend/api/rest_api.py@@ -43,6 +43,7 @@ import backend.api.features.chat.speech as chat_speechimport backend.api.features.desktop_previewimport backend.api.features.experimentsimport backend.api.features.experts.routes as experts_routes+import backend.api.features.feature_flags.routes as feature_flags_routesimport backend.api.features.graph_executions.review.routesimport backend.api.features.graph_executions.routes as graph_executions_routesimport backend.api.features.graphs.routes as graphs_routes@@ -542,6 +543,7 @@ app.include_router(experts_routes.public_router, tags=["v2", "experts"], prefix=app.include_router(experts_routes.router, tags=["v2", "experts"], prefix="/api")app.include_router(memory_routes.router, tags=["v2", "memory"], prefix="/api")app.include_router(home_routes.router, prefix="/api")+app.include_router(feature_flags_routes.router, prefix="/api")app.include_router(backend.api.features.otto.routes.router, tags=["v2", "otto"], prefix="/api/otto")diff --git a/autogpt_platform/backend/backend/util/feature_flag/__init__.py b/autogpt_platform/backend/backend/util/feature_flag/__init__.pyindex 30daf4851f..ba70029d39 100644--- a/autogpt_platform/backend/backend/util/feature_flag/__init__.py+++ b/autogpt_platform/backend/backend/util/feature_flag/__init__.py@@ -16,6 +16,7 @@ from fastapi import HTTPException, Securityfrom ldclient import Context, LDClientfrom ldclient.config import Configfrom ldclient.evaluation import EvaluationDetail+from ldclient.interfaces import DataSourceStatefrom typing_extensions import ParamSpecfrom backend.util.cache import cached@@ -33,6 +34,9 @@ settings = Settings()P = ParamSpec("P")T = TypeVar("T")+# The one key every signed-out caller without a consented analytics id is read as.+ANONYMOUS_USER = "anonymous"+_is_initialized = False_init_attempted = False@@ -46,6 +50,11 @@ MAX_CONCURRENT_SHADOW_EVALUATIONS = 100_unanswered_flags_reported: set[str] = set()+# Flags that run a PostHog experiment. Only a read of one of these sends the+# billed `$feature_flag_called` event, which is how an experiment counts exposure.+EXPERIMENT_FLAGS = frozenset({"subscription-pricing-page-initial-state"})++class Flag(str, Enum):"""Centralized enum for all feature flags.@@ -459,6 +468,36 @@ async def get_feature_flag_value(return value+def evaluate_flags_in_memory(flag_keys: list[str], key: str) -> dict[str, Any]:+ """Each of *flag_keys* the vendor answers in memory for the anonymous *key*.++ No network, no analytics event and no executor, so a caller may pick *key*:+ a new one costs only the hashing. A flag that needs more is left out.+ """+ context = _anonymous_context(key)+ if settings.config.feature_flag_backend is FeatureFlagBackend.POSTHOG:+ values = posthog.evaluate_flags_in_memory(+ flag_keys, key, _person_properties(context)+ )+ else:+ values = _launchdarkly_flags_in_memory(context)+ return {+ flag_key: values[flag_key]+ for flag_key in flag_keys+ if values.get(flag_key) is not None+ }+++def defined_flag_keys() -> frozenset[str] | None:+ """Every flag the serving vendor has a definition of, or None if it cannot tell yet.++ A key outside the set will never be answered, so a reader can stop waiting on it.+ """+ if settings.config.feature_flag_backend is FeatureFlagBackend.POSTHOG:+ return posthog.defined_flag_keys()+ return _launchdarkly_flag_keys()++async def _evaluate_flag_value(flag_key: str,user_id: str,@@ -478,7 +517,9 @@ async def _evaluate_flag_value(context = (_with_request_attributes(user_context, attributes), context_resolved)backend = settings.config.feature_flag_backendif backend is FeatureFlagBackend.POSTHOG:- result = await _evaluate_posthog(flag_key, user_id, default, context)+ result = await _evaluate_posthog(+ flag_key, user_id, default, context, exposure=flag_key in EXPERIMENT_FLAGS+ )elif backend is FeatureFlagBackend.DUAL:result = await _evaluate_dual(flag_key, user_id, default, context)else:@@ -598,12 +639,15 @@ async def _evaluate_posthog(user_id: str,default: Any = None,context: tuple[Context, bool] | None = None,+ *,+ exposure: bool = False,) -> tuple[Any, bool]:"""``(value, evaluated)`` from PostHog for one raw flag read.A degraded context lookup makes the answer unauthoritative for the samereason it does on LaunchDarkly: the flag evaluates fine, it just answers- for a user without the targeted attributes.+ for a user without the targeted attributes. Only a served *exposure*+ sends the experiment's event; dual mode's shadow read never does."""user_context, context_resolved = context or await _fetch_user_context_status(user_id@@ -613,6 +657,7 @@ async def _evaluate_posthog(user_id,_person_properties(user_context),default,+ exposure=exposure,)return value, evaluated and context_resolved@@ -667,6 +712,29 @@ async def _evaluate_launchdarkly(return default, False+def _launchdarkly_flags_in_memory(context: Context) -> dict[str, Any]:+ # all_flags_state, unlike variation, sends LaunchDarkly no event per context.+ if not is_configured():+ return {}+ try:+ state = get_client().all_flags_state(context)+ except Exception as e:+ logger.warning(f"LaunchDarkly in-memory flag evaluation failed: {e}")+ return {}+ return state.to_values_map() if state.valid else {}+++def _launchdarkly_flag_keys() -> frozenset[str] | None:+ if not is_configured():+ return None+ try:+ state = get_client().all_flags_state(_anonymous_context(ANONYMOUS_USER))+ except Exception as e:+ logger.warning(f"Listing LaunchDarkly flags failed: {e}")+ return None+ return frozenset(state.to_values_map()) if state.valid else None++def _person_properties(context: Context) -> dict[str, Any]:"""PostHog person properties for one read: the user's, plus ``environment``.@@ -688,14 +756,16 @@ def _context_properties(context: Context) -> dict[str, Any]:because PostHog targets flat properties. ``email`` is sent because theported email rules and the ``ld:employee`` cohort match on it, and a ruleon a property the read omits cannot be evaluated in-process.+ ``is_anonymous`` is true only for the shared ``ANONYMOUS_USER`` key, so a+ rollout can leave out the visitors nobody can measure."""if context.anonymous:- return {}+ return {"is_anonymous": context.key == ANONYMOUS_USER}return {attribute: context.get(attribute)for attribute in ("role", "email", "email_domain", "created_at", "country")if context.get(attribute) is not None- }+ } | {"is_anonymous": False}def _user_digest(user_id: str) -> str:@@ -839,12 +909,20 @@ async def evaluate_feature_flag(irreversible — a failed read is indistinguishable from a real "off" on thevalue alone."""+ return await _evaluate_boolean_flag(flag_key.value, user_id, default)+++async def _evaluate_boolean_flag(+ flag_key: str,+ user_id: str,+ default: bool = False,+) -> tuple[bool, bool]:override = _env_flag_override(flag_key)if override is not None:logger.debug(f"Feature flag {flag_key} overridden by env: {override}")return override, True- result, evaluated = await _evaluate_flag_value(flag_key.value, user_id, default)+ result, evaluated = await _evaluate_flag_value(flag_key, user_id, default)# If the result is already a boolean, return itif isinstance(result, bool):@@ -858,7 +936,7 @@ async def evaluate_feature_flag(# A misconfigured flag is not an answer either: fall back to the default,# but never let a caller take an irreversible action on it.- _record_flag_for_sentry(flag_key.value, default, False)+ _record_flag_for_sentry(flag_key, default, False)return default, False@@ -971,7 +1049,7 @@ def create_feature_flag_dependency(or None for anonymous access."""# For routes that don't require authentication, use anonymous context- check_user_id = user_id or "anonymous"+ check_user_id = user_id or ANONYMOUS_USER# A local env override (per-flag FORCE_FLAG_*, or the FORCE_ALL_FLAGS# master switch) wins over whichever vendor is configured, and applies@@ -1030,6 +1108,14 @@ def _flag_backend_configured() -> bool:return posthog.is_configured()+def _flag_backend_gave_up() -> bool:+ """Whether the serving vendor has stopped trying to start, as on a rejected key."""+ if serves_launchdarkly():+ status = get_client().data_source_status_provider.status+ return status.state is DataSourceState.OFF+ return False++def _flag_backend_initialized() -> bool:"""Whether the configured backend can answer right now."""if serves_launchdarkly():diff --git a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py b/autogpt_platform/backend/backend/util/feature_flag/backends_test.pyindex 76526abb02..6428c3a79c 100644--- a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py+++ b/autogpt_platform/backend/backend/util/feature_flag/backends_test.py@@ -9,12 +9,14 @@ import uuidimport pytestimport sentry_sdkimport sentry_sdk.feature_flags+import urllib3.connectionpoolfrom fastapi import HTTPExceptionfrom ldclient import Context, LDClientfrom ldclient.config import Config as LDConfigfrom ldclient.evaluation import EvaluationDetailfrom ldclient.integrations.test_data import TestDatafrom posthog import Posthog+from posthog.types import FeatureFlagResultfrom sentry_sdk.tracing import Spanimport backend.util.feature_flag as ff@@ -23,10 +25,13 @@ from backend.util.feature_flag import (Flag,evaluate_feature_flag,feature_flag,+ frontend,is_feature_enabled,)from backend.util.settings import AppEnvironment, Config, FeatureFlagBackend+PRICING_EXPERIMENT = "subscription-pricing-page-initial-state"+@pytest.fixture(autouse=True)async def no_leaked_shadow_evaluations():@@ -92,12 +97,11 @@ def use_backend(mocker, backend: FeatureFlagBackend):def stub_posthog(mocker, *, value, payload=None):- """Stand in for a PostHog evaluation snapshot for one flag."""- snapshot = mocker.Mock()- snapshot.get_flag.return_value = value- snapshot.get_flag_payload.return_value = payload+ """Stand in for PostHog's answer for one flag."""client = mocker.Mock()- client.evaluate_flags.return_value = snapshot+ client.get_feature_flag_result.return_value = (+ FeatureFlagResult.from_value_and_payload("flag", value, payload)+ )mocker.patch.object(ph, "get_flag_client", return_value=client)return client@@ -196,7 +200,7 @@ class TestPostHogBackend:async def test_an_evaluation_that_raises_is_not(self, mocker, user_context):use_backend(mocker, FeatureFlagBackend.POSTHOG)client = stub_posthog(mocker, value=True)- client.evaluate_flags.side_effect = Exception("evaluation exploded")+ client.get_feature_flag_result.side_effect = Exception("evaluation exploded")assert await evaluate_feature_flag(Flag.HIRE_EXPERTS, "u-1") == (False, False)@@ -253,12 +257,13 @@ class TestPostHogBackend:await evaluate_feature_flag(Flag.HIRE_EXPERTS, "u-1")- _, kwargs = client.evaluate_flags.call_args+ _, kwargs = client.get_feature_flag_result.call_argsassert kwargs["person_properties"] == {"role": "admin","email": "[email protected]","email_domain": "agpt.co","created_at": "2026-05-07T12:00:00+00:00",+ "is_anonymous": False,"environment": ff.settings.config.app_env.value,}@@ -292,20 +297,75 @@ class TestPostHogBackend:assert result == expected@pytest.mark.asyncio- async def test_an_anonymous_context_carries_only_the_environment(self, mocker):- """The `"system"`-keyed config flags have no user to describe, but can- still differ between dev and prod."""+ async def test_an_anonymous_context_carries_only_is_anonymous_and_the_environment(+ self, mocker+ ):+ """The `"system"`-keyed config flags have no user to describe and are not+ the shared signed-out key, but can still differ between dev and prod."""use_backend(mocker, FeatureFlagBackend.POSTHOG)client = stub_posthog(mocker, value=True, payload={"TIER": 1.5})await ff.get_feature_flag_value("copilot-tier-multipliers", "system")- args, kwargs = client.evaluate_flags.call_args- assert args[0] == "system"+ args, kwargs = client.get_feature_flag_result.call_args+ assert args[1] == "system"assert kwargs["person_properties"] == {- "environment": ff.settings.config.app_env.value+ "is_anonymous": False,+ "environment": ff.settings.config.app_env.value,}+ @pytest.mark.asyncio+ async def test_a_split_on_is_anonymous_leaves_out_only_the_shared_signed_out_key(+ self, mocker+ ):+ """A 20% rollout "where is_anonymous is not true" buckets consented+ visitors by their own id and never the visitors nobody can measure."""+ use_backend(mocker, FeatureFlagBackend.POSTHOG)+ posthog_client = _posthog_with_a_split_on_is_anonymous()+ mocker.patch.object(ph, "get_flag_client", return_value=posthog_client)+ frontend._anonymous_flags.cache_clear()+ frontend._defined_flags.cache_clear()+ try:+ anonymous = await frontend.evaluate_frontend_flags(None)+ visitors = [+ await frontend.evaluate_frontend_flags(None, f"{i:08d}-visitor")+ for i in range(200)+ ]+ finally:+ frontend._anonymous_flags.cache_clear()+ frontend._defined_flags.cache_clear()+ posthog_client.shutdown()++ split = frontend.FrontendFlag.CHAT_SEARCH+ share = sum(v.flags.get(split) is True for v in visitors) / len(visitors)+ # One assertion, so a failure shows both halves.+ assert (anonymous.flags.get(split), 0.1 < share < 0.3) == (False, True), share++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "flag_key,events",+ [(PRICING_EXPERIMENT, 1), (Flag.HIRE_EXPERTS.value, 0)],+ )+ async def test_only_an_experiment_read_sends_posthog_its_exposure_event(+ self, mocker, monkeypatch, flag_key, events+ ):+ """`$feature_flag_called` is billed per event, and only an experiment+ counts its exposures with it."""+ use_backend(mocker, FeatureFlagBackend.POSTHOG)+ monkeypatch.setattr(+ urllib3.connectionpool.HTTPConnectionPool, "urlopen", _refuse_network+ )+ posthog_client = _posthog_rolled_out(flag_key)+ capture = mocker.patch.object(posthog_client, "capture")+ mocker.patch.object(ph, "get_flag_client", return_value=posthog_client)+ try:+ value = await ff.get_feature_flag_value(flag_key, "u-1")+ finally:+ posthog_client.shutdown()++ sent = [call.args[0] for call in capture.call_args_list]+ assert (value, sent) == (True, ["$feature_flag_called"] * events)+@pytest.mark.asyncio@pytest.mark.parametrize("app_env,expected",@@ -350,6 +410,28 @@ class TestPostHogBackend:class TestDualBackend:+ @pytest.mark.asyncio+ async def test_the_shadow_read_of_an_experiment_sends_posthog_no_exposure(+ self, mocker, monkeypatch, ld_client, user_context+ ):+ """LaunchDarkly's answer is the one served, so PostHog's arm was never shown."""+ use_backend(mocker, FeatureFlagBackend.DUAL)+ ld_client.variation_detail.return_value = served(True)+ monkeypatch.setattr(+ urllib3.connectionpool.HTTPConnectionPool, "urlopen", _refuse_network+ )+ posthog_client = _posthog_rolled_out(PRICING_EXPERIMENT)+ capture = mocker.patch.object(posthog_client, "capture")+ read = mocker.spy(posthog_client, "get_feature_flag_result")+ mocker.patch.object(ph, "get_flag_client", return_value=posthog_client)+ try:+ await ff.get_feature_flag_value(PRICING_EXPERIMENT, "u-1")+ await drain_shadow_evaluations()+ finally:+ posthog_client.shutdown()++ assert (read.call_count, capture.call_count) == (1, 0)+@pytest.mark.asyncioasync def test_launchdarkly_answers_even_when_posthog_disagrees(self, mocker, ld_client, user_context@@ -1093,6 +1175,52 @@ def _posthog_with_an_employee_cohort() -> Posthog:return client+def _posthog_with_a_split_on_is_anonymous() -> Posthog:+ """A 20% rollout whose condition leaves out `is_anonymous: true`."""+ client = Posthog("phc-test", secret_key="phx-test", enable_local_evaluation=False)+ client.feature_flags = [+ {+ "id": 2,+ "key": "chat-search",+ "active": True,+ "filters": {+ "groups": [+ {+ "properties": [+ {+ "key": "is_anonymous",+ "type": "person",+ "operator": "is_not",+ "value": ["true"],+ }+ ],+ "rollout_percentage": 20,+ }+ ]+ },+ }+ ]+ client._get_flags_decision = _no_remote_flags+ return client+++def _posthog_rolled_out(flag_key: str) -> Posthog:+ client = Posthog("phc-test", secret_key="phx-test", enable_local_evaluation=False)+ client.feature_flags = [+ {+ "id": 1,+ "key": flag_key,+ "active": True,+ "filters": {"groups": [{"properties": [], "rollout_percentage": 100}]},+ }+ ]+ return client+++def _refuse_network(pool, method, url, *args, **kwargs):+ raise urllib3.exceptions.ProtocolError(f"no network in tests: {method} {url}")++def _posthog_rolled_out_to_dev_only() -> Posthog:"""A flag whose one release condition is ``environment = dev``."""client = Posthog("phc-test", secret_key="phx-test", enable_local_evaluation=False)diff --git a/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py b/autogpt_platform/backend/backend/util/feature_flag/definition_cache.pyindex b53a52ea52..8698233d5d 100644--- a/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py+++ b/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py@@ -19,10 +19,7 @@ import timeimport uuidfrom typing import Any, Callable-from posthog.flag_definition_cache import (- FlagDefinitionCacheData,- FlagDefinitionCacheProvider,-)+from posthog.flag_definition_cache import FlagDefinitionCacheDatafrom prometheus_client import Counterfrom backend.data.redis_client import connect_once@@ -72,7 +69,9 @@ return 0"""-def get_flag_definition_cache() -> FlagDefinitionCacheProvider | None:+def get_flag_definition_cache() -> (+ "RedisFlagDefinitionCache | MemoryFlagDefinitionCache | None"+):"""The provider the PostHog client shares its flag definitions through.None leaves the SDK polling PostHog once per process, which is what it does@@ -119,6 +118,11 @@ class RedisFlagDefinitionCache:# What this process's SDK last installed, fetched or read.self._definitions: FlagDefinitionCacheData | None = None+ @property+ def definitions(self) -> FlagDefinitionCacheData | None:+ """The definitions this process last loaded successfully, fetched or shared."""+ return self._definitions+def should_fetch_flag_definitions(self) -> bool:try:redis = self._redis()@@ -257,6 +261,11 @@ class MemoryFlagDefinitionCache:self._fetched_at = 0.0self._lock = threading.Lock()+ @property+ def definitions(self) -> FlagDefinitionCacheData | None:+ with self._lock:+ return self._data+def should_fetch_flag_definitions(self) -> bool:with self._lock:fresh = (diff --git a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.pyindex 914eca2b9e..b2c5c93f98 100644--- a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py+++ b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py@@ -9,7 +9,7 @@ import pytestfrom ldclient import Context, LDClientfrom ldclient.evaluation import EvaluationDetailfrom posthog import Posthog-from posthog.request import GetResponse+from posthog.request import APIError, GetResponsefrom pydantic import ValidationErrorimport backend.data.redis_client as redis_client@@ -592,6 +592,53 @@ class TestStartupLoad:assert await ph.evaluate_flag("shared-flag", "u-1") == (True, True)+class TestWhichFlagsTheProjectHas:+ """Only a load that succeeded says which flags exist; the SDK also empties its+ definitions on a rejected key or a quota limit."""++ @pytest.fixture(autouse=True)+ def configured(self, mocker, redis):+ mocker.patch.object(ph, "_client", None)+ mocker.patch.object(ph, "_init_attempted", False)+ mocker.patch.object(ph, "_shut_down", False)+ mocker.patch.object(ph, "_definition_cache", None)+ mocker.patch.object(ph.settings.secrets, "posthog_api_key", "phc_test")+ mocker.patch.object(ph.settings.secrets, "posthog_secret_key", "phx_personal")+ mocker.patch.object(+ ph, "get_flag_definition_cache", return_value=provider(redis)+ )+ yield+ ph.shutdown_posthog_flags()++ @pytest.mark.parametrize("shared", [True, False])+ def test_a_project_that_loaded_with_no_flags_has_none(+ self, mocker, redis, fetches, shared+ ):+ if not shared:+ mocker.patch.object(+ ph,+ "get_flag_definition_cache",+ return_value=cache.MemoryFlagDefinitionCache(refresh_interval=REFRESH),+ )+ fetches.return_value = GetResponse(data={**DEFINITIONS, "flags": []}, etag="e2")++ ph.initialize_posthog_flags()++ assert ph.defined_flag_keys() == frozenset()++ @pytest.mark.parametrize("status", [401, 402])+ def test_definitions_a_failed_load_emptied_are_not_known(self, fetches, status):+ ph.initialize_posthog_flags()+ assert ph.defined_flag_keys() == frozenset({"shared-flag"})+ fetches.side_effect = APIError(status, "rejected")++ client = ph.get_flag_client()+ assert client is not None+ client.load_feature_flags()++ assert (client.feature_flags, ph.defined_flag_keys()) == ([], None)++@pytest.mark.skipif(not is_tcp_port_reachable(redis_client.HOST, redis_client.PORT),reason="no local Redis reachable; the lock scripts need one to run",diff --git a/autogpt_platform/backend/backend/util/feature_flag/frontend.py b/autogpt_platform/backend/backend/util/feature_flag/frontend.pynew file mode 100644index 0000000000..abeafde12d--- /dev/null+++ b/autogpt_platform/backend/backend/util/feature_flag/frontend.py@@ -0,0 +1,181 @@+"""The flags the browser reads, evaluated here so it gets the backend's answer."""++import asyncio+import re+from enum import Enum+from typing import Any++from pydantic import BaseModel, Field, GetJsonSchemaHandler+from pydantic.json_schema import JsonSchemaValue+from pydantic_core import CoreSchema++from backend.util.cache import cached++from . import (+ ANONYMOUS_USER,+ Flag,+ _env_flag_override,+ _evaluate_boolean_flag,+ _evaluate_flag_value,+ _flag_backend_configured,+ _flag_backend_gave_up,+ defined_flag_keys,+ evaluate_flags_in_memory,+)++# A consenting visitor's analytics id (a UUID); anything else counts as no id.+VISITOR_ID = re.compile(r"[A-Za-z0-9-]{8,64}")+# A visitor's flag key; no user id has a colon, so it can never target a user.+VISITOR_KEY = "visitor:{}"+# How long a signed-out answer missing a flag is served before the vendor is asked again.+PARTIAL_TTL_SECONDS = 5+++class FrontendFlag(str, Enum):+ """Every flag the frontend reads; the frontend's `Flag` is generated from this.++ A key the backend reads too comes from the backend's `Flag`, so it is written once.+ """++ MARKETPLACE_SEARCH_TERMS = "marketplace-search-terms"+ ENABLE_PLATFORM_PAYMENT = Flag.ENABLE_PLATFORM_PAYMENT.value+ ARTIFACTS_PAGE = "artifacts-page"+ CHAT_MODE_OPTION = Flag.CHAT_MODE_OPTION.value+ GENERIC_TRIGGER_AGENTS = Flag.GENERIC_TRIGGER_AGENTS.value+ CHAT_SEARCH = "chat-search"+ AUTOGPT_NEW_LAYOUT = "autogpt-new-layout"+ CHAT_WORKSPACE_FILES = "chat-workspace-files"+ CHAT_PINNING = "chat-pinning"+ TASK_PROGRESS_BAR = "task-progress-bar"+ HIRE_EXPERTS = Flag.HIRE_EXPERTS.value+ SKILLS_HUB = Flag.SKILLS_HUB.value+ # Until this is on for everyone, /profile/settings stays un-redirected+ # (`useNewSettingsRedirect`) so the toggles stay reachable: flip both together.+ SETTINGS_NOTIFICATIONS = "settings-notifications"+ ONBOARDING_BRAIN_DUMP = Flag.ONBOARDING_BRAIN_DUMP.value+ ONBOARDING_EXPERT_TEAM = Flag.ONBOARDING_EXPERT_TEAM.value+ GRAPHITI_MEMORY = Flag.GRAPHITI_MEMORY.value+ GRAPHITI_COMMUNITIES_ENABLED = Flag.GRAPHITI_COMMUNITIES_ENABLED.value+ DREAM_PASS_ENABLED = Flag.DREAM_PASS_ENABLED.value+ DREAM_PASS_INVALIDATE_ENTITY = Flag.DREAM_PASS_INVALIDATE_ENTITY.value+ # Platform key -> visible on the Bots settings page; only an explicit false hides one.+ COPILOT_BOT_PLATFORMS = "copilot-bot-platforms"+ COPILOT_VOICE_MODE = Flag.COPILOT_VOICE_MODE.value+ COPILOT_AUTO_MODE = Flag.COPILOT_AUTO_MODE.value+ # The share of chat sessions whose stream is shadowed and checked, 0 to 1.+ COPILOT_STREAM_SHADOW = "copilot-stream-shadow"+ COPILOT_STREAM_RUNTIME = "copilot-stream-runtime"++ @classmethod+ def __get_pydantic_json_schema__(+ cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler+ ) -> JsonSchemaValue:+ # Orval names the generated const's members from this, so `Flag.X` keeps its names.+ schema = handler(core_schema)+ handler.resolve_ref_schema(schema)["x-enum-varnames"] = [m.name for m in cls]+ return schema+++# Served as payloads, so an env override (a bare true/false) never stands in for them.+PAYLOAD_FLAGS = frozenset(+ {+ FrontendFlag.MARKETPLACE_SEARCH_TERMS,+ FrontendFlag.COPILOT_BOT_PLATFORMS,+ FrontendFlag.COPILOT_STREAM_SHADOW,+ }+)+++class FrontendFlagValues(BaseModel):+ flags: dict[FrontendFlag, Any] = Field(+ description="Each flag the vendor answered, or null for one it has no definition "+ "of, which takes the frontend's default; one left out has no answer yet."+ )+ configured: bool = Field(+ description="False when no flag vendor is configured or it gave up starting, "+ "so every flag is its default."+ )+++async def evaluate_frontend_flags(+ user_id: str | None, visitor_id: str | None = None+) -> FrontendFlagValues:+ """The frontend's flags for the caller, read exactly as the backend reads them.++ A signed-out caller is bucketed by *visitor_id*, the analytics id its page sends+ only with consent, wherever the vendor answers in memory; otherwise it reads as+ `ANONYMOUS_USER`. A signed-in caller ignores it.+ """+ if user_id is not None:+ flags = await _evaluate(user_id)+ elif visitor_id is not None and VISITOR_ID.fullmatch(visitor_id):+ flags = await _visitor_flags(visitor_id)+ else:+ flags = await _anonymous_flags()+ # A vendor that gave up for good serves every default, as the route gates do;+ # one still connecting leaves its flags out, so the page asks again.+ configured = _flag_backend_configured() and not _flag_backend_gave_up()+ return FrontendFlagValues(flags=flags, configured=configured)+++@cached(maxsize=1, ttl_seconds=30)+async def _anonymous_flags() -> dict[FrontendFlag, Any]:+ # Every signed-out visitor gets the same answer, so one read serves them all.+ flags = await _evaluate(ANONYMOUS_USER)+ if len(flags) < len(FrontendFlag):+ # A vendor that is down or starting is asked again soon; one read per+ # window still answers the whole flood of visitors meanwhile.+ asyncio.get_running_loop().call_later(+ PARTIAL_TTL_SECONDS, _anonymous_flags.cache_clear+ )+ return flags+++# Listing LaunchDarkly's flags evaluates all of them; "cannot tell yet" is retried.+@cached(maxsize=1, ttl_seconds=30, cache_none=False)+def _defined_flags() -> frozenset[str] | None:+ return defined_flag_keys()+++async def _visitor_flags(visitor_id: str) -> dict[FrontendFlag, Any]:+ # The caller picks the id, so its own bucket is only what the vendor answers in+ # memory; everything else is the one shared signed-out read.+ own = evaluate_flags_in_memory(+ [flag.value for flag in FrontendFlag], VISITOR_KEY.format(visitor_id)+ )+ flags = dict(await _anonymous_flags())+ for flag in FrontendFlag:+ if _takes_own_answer(flag, own.get(flag.value)):+ flags[flag] = own[flag.value]+ return flags+++async def _evaluate(user_id: str) -> dict[FrontendFlag, Any]:+ defined = _defined_flags()+ # A flag the vendor has no definition of is never read (on PostHog that is a+ # /flags call each) and is answered None, so the page stops waiting for it.+ readable = [f for f in FrontendFlag if defined is None or f.value in defined]+ results = await asyncio.gather(*(_read(flag, user_id) for flag in readable))+ undefined = {flag: _forced(flag) for flag in FrontendFlag if flag not in readable}+ return undefined | {+ flag: value+ for flag, (value, answered) in zip(readable, results)+ if answered and value is not None+ }+++async def _read(flag: FrontendFlag, user_id: str) -> tuple[Any, bool]:+ if flag in PAYLOAD_FLAGS:+ return await _evaluate_flag_value(flag.value, user_id)+ return await _evaluate_boolean_flag(flag.value, user_id)+++def _forced(flag: FrontendFlag) -> bool | None:+ return None if flag in PAYLOAD_FLAGS else _env_flag_override(flag.value)+++def _takes_own_answer(flag: FrontendFlag, value: Any) -> bool:+ # A switch forced by env stays forced, as in the shared read; a payload never is.+ return value is not None and (+ flag in PAYLOAD_FLAGS or _env_flag_override(flag.value) is None+ )diff --git a/autogpt_platform/backend/backend/util/feature_flag/posthog.py b/autogpt_platform/backend/backend/util/feature_flag/posthog.pyindex 413d2c19ab..a3cf31dd37 100644--- a/autogpt_platform/backend/backend/util/feature_flag/posthog.py+++ b/autogpt_platform/backend/backend/util/feature_flag/posthog.py@@ -6,6 +6,7 @@ module dispatches to this one, so a back-import would be a cycle.import asyncioimport functools+import jsonimport loggingimport threadingfrom concurrent.futures import ThreadPoolExecutor@@ -15,7 +16,12 @@ from posthog import Posthogfrom backend.util.settings import Settings-from .definition_cache import get_flag_definition_cache, refresh_interval_seconds+from .definition_cache import (+ MemoryFlagDefinitionCache,+ RedisFlagDefinitionCache,+ get_flag_definition_cache,+ refresh_interval_seconds,+)logger = logging.getLogger(__name__)@@ -29,6 +35,8 @@ _read_executor = ThreadPoolExecutor()_client: Posthog | None = None+# The provider _client loads through; it alone sees which loads succeeded.+_definition_cache: RedisFlagDefinitionCache | MemoryFlagDefinitionCache | None = None# The executor evaluates flags from two event loops on two threads; a second# client built by a racing first read would leak its poller thread._client_lock = threading.Lock()@@ -61,16 +69,19 @@ async def evaluate_flag(distinct_id: str,person_properties: dict[str, Any] | None = None,default: Any = None,+ *,+ exposure: bool = False,) -> tuple[Any, bool]:"""``(value, evaluated)`` for one raw flag read.``evaluated`` is False whenever *default* is standing in for an answer- PostHog could not give. Unlike LaunchDarkly this needs no inference:- ``get_flag`` returns ``None`` exactly when the flag was not resolved, and- ``False`` when it conclusively matched no release condition.+ PostHog could not give. Unlike LaunchDarkly this needs no inference: the+ SDK returns no result exactly when the flag was not resolved, and a+ disabled one when it conclusively matched no release condition.A flag carrying a payload resolves to that payload, mirroring how the- JSON-valued LaunchDarkly flags return their variation value.+ JSON-valued LaunchDarkly flags return their variation value. Only an+ *exposure* read sends the billed ``$feature_flag_called`` event."""client = get_flag_client()if client is None:@@ -78,16 +89,17 @@ async def evaluate_flag(return default, Falsetry:- # evaluate_flags does network I/O whenever the local-evaluation poller- # has no definition for the flag, so it cannot run on the event loop.- snapshot = await asyncio.get_running_loop().run_in_executor(+ # The read does network I/O whenever the local-evaluation poller has+ # no definition for the flag, so it cannot run on the event loop.+ result = await asyncio.get_running_loop().run_in_executor(_read_executor,functools.partial(- client.evaluate_flags,+ client.get_feature_flag_result,+ flag_key,distinct_id,person_properties=person_properties or None,- flag_keys=[flag_key],only_evaluate_locally=reads_another_project(),+ send_feature_flag_events=exposure,),)except Exception as e:@@ -96,19 +108,58 @@ async def evaluate_flag()return default, False- value = snapshot.get_flag(flag_key)- if value is None:+ if result is None:logger.debug(f"PostHog returned no answer for {flag_key}, using {default}")return default, False# A conclusive "off" is the answer. Serving a payload here instead would# turn it into a non-boolean, which every caller reads as "could not# evaluate" — the one distinction this evaluator exists to preserve.- if value is False:+ if not result.enabled:return False, True- payload = snapshot.get_flag_payload(flag_key)- return (payload if payload is not None else value), True+ return (result.payload if result.payload is not None else result.get_value()), True+++def evaluate_flags_in_memory(+ flag_keys: list[str], distinct_id: str, person_properties: dict[str, Any]+) -> dict[str, Any]:+ """The flags the loaded definitions answer for *distinct_id*, on this thread.++ Never fetches definitions, never asks ``/flags`` and fires no+ ``$feature_flag_called``; a flag the definitions cannot decide is left out.+ """+ client = get_flag_client()+ # With no definitions loaded the SDK would fetch them before evaluating.+ if client is None or not client.feature_flags:+ return {}+ try:+ result = client.get_all_flags_and_payloads(+ distinct_id,+ person_properties=person_properties,+ only_evaluate_locally=True,+ flag_keys_to_evaluate=flag_keys,+ )+ except Exception as e:+ logger.warning(f"PostHog in-memory flag evaluation failed: {e}")+ return {}+ payloads = result.get("featureFlagPayloads") or {}+ return {+ key: value if value is False else _decoded(payloads.get(key), value)+ for key, value in (result.get("featureFlags") or {}).items()+ }+++def defined_flag_keys() -> frozenset[str] | None:+ """Every flag the loaded definitions hold, or None while that is unknown."""+ client = get_flag_client()+ if client is None or client.feature_flags is None:+ return None+ # The SDK also empties its definitions on a rejected key or a quota limit;+ # only a load that succeeded empty means the project has no flags.+ if not client.feature_flags and not _loaded_empty():+ return None+ return frozenset(flag["key"] for flag in client.feature_flags)def initialize_posthog_flags() -> None:@@ -133,10 +184,11 @@ def initialize_posthog_flags() -> None:def shutdown_posthog_flags() -> None:- global _client, _init_attempted, _shut_down+ global _client, _definition_cache, _init_attempted, _shut_downwith _client_lock:client = _client_client = None+ _definition_cache = None# Clear the "did we try" gate even when no client was built, or an# unconfigured first attempt latches it shut for the life of the process._init_attempted = False@@ -168,7 +220,7 @@ def get_flag_client() -> Posthog | None:def _build_client() -> Posthog | None:- global _client, _init_attempted+ global _client, _definition_cache, _init_attemptedif not is_configured():_init_attempted = Truelogger.warning("PostHog API key not configured; flag reads will not resolve")@@ -185,6 +237,7 @@ def _build_client() -> Posthog | None:# One shared refresher, so the definitions bill stops scaling with replica# count. Without a provider the SDK polls PostHog once per process.definition_cache = get_flag_definition_cache() if secret_key else None+ _definition_cache = definition_cache_client = Posthog(_flag_project_key(),host=settings.secrets.posthog_host,@@ -204,3 +257,18 @@ def _build_client() -> Posthog | None:f"definition cache: {type(definition_cache).__name__ if definition_cache else 'off'})")return _client+++def _decoded(payload: Any, value: Any) -> Any:+ """A matched flag's payload, which definitions hold as JSON text, else *value*."""+ if not payload:+ return value+ try:+ return json.loads(payload)+ except (TypeError, ValueError):+ return payload+++def _loaded_empty() -> bool:+ last = _definition_cache.definitions if _definition_cache else None+ return last is not None and not last.get("flags")diff --git a/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py b/autogpt_platform/backend/backend/util/feature_flag/posthog_test.pyindex aed68dd93f..c6e637eb45 100644--- a/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py+++ b/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py@@ -7,6 +7,7 @@ from concurrent.futures import ThreadPoolExecutorimport pytestfrom posthog import Posthog+from posthog.types import FeatureFlagResultimport backend.util.feature_flag.posthog as ph@@ -29,11 +30,8 @@ def configure(mocker, *, api_key="phc_test", secret_key="", flags_key=""):mocker.patch.object(ph.settings.secrets, "posthog_flags_api_key", flags_key)-def snapshot(mocker, *, value, payload=None):- result = mocker.Mock()- result.get_flag.return_value = value- result.get_flag_payload.return_value = payload- return result+def answer(*, value, payload=None):+ return FeatureFlagResult.from_value_and_payload("flag", value, payload)class TestClientConstruction:@@ -217,12 +215,12 @@ class TestTheFlagProject:):configure(mocker, flags_key=flags_key)client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(mocker, value=True)+ client.get_feature_flag_result.return_value = answer(value=True)mocker.patch.object(ph, "get_flag_client", return_value=client)await ph.evaluate_flag("hire-experts", "u-1")- _, kwargs = client.evaluate_flags.call_args+ _, kwargs = client.get_feature_flag_result.call_argsassert kwargs["only_evaluate_locally"] is only_locally@@ -260,21 +258,66 @@ class TestASeparateFlagProjectThroughTheRealSDK:]client, remote = _real_client(mocker, flags_key="phc_prod")try:- assert await ph.evaluate_flag("hire-experts", "u-1") == (True, True)- assert await ph.evaluate_flag("not-in-the-definitions", "u-1") == (- None,- False,+ # Exposure reads, the only ones that form $feature_flag_called.+ assert await ph.evaluate_flag("hire-experts", "u-1", exposure=True) == (+ True,+ True,)+ assert await ph.evaluate_flag(+ "not-in-the-definitions", "u-1", exposure=True+ ) == (None, False)client.flush()finally:ph.shutdown_posthog_flags()- # The SDK still forms the $feature_flag_called events; none is uploaded.+ # The SDK still forms the events; none is uploaded.assert captured.calledassert not remote.calledassert not any(upload.called for upload in uploads)+class TestInMemoryRead:+ def test_definitions_not_loaded_yet_are_never_fetched(self, mocker):+ """Fetching them first is the SDK's default, and a network call per read."""+ client = Posthog(+ "phc-test", secret_key="phx-test", enable_local_evaluation=False+ )+ fetch = mocker.patch.object(client, "load_feature_flags")+ remote = mocker.patch.object(client, "_get_flags_decision")+ mocker.patch.object(ph, "get_flag_client", return_value=client)+ try:+ values = ph.evaluate_flags_in_memory(["hire-experts"], "visitor:v-1", {})+ finally:+ client.shutdown()++ assert (values, fetch.call_count, remote.call_count) == ({}, 0, 0)++ def test_a_matched_payload_is_served_decoded(self, mocker):+ client = Posthog(+ "phc-test", secret_key="phx-test", enable_local_evaluation=False+ )+ client.feature_flags = [+ _rolled_out("marketplace-search-terms", 100, payload='["Research"]'),+ _rolled_out("hire-experts", 100),+ _rolled_out("chat-search", 0, payload='["unused"]'),+ ]+ mocker.patch.object(ph, "get_flag_client", return_value=client)+ try:+ values = ph.evaluate_flags_in_memory(+ ["marketplace-search-terms", "hire-experts", "chat-search"],+ "visitor:v-1",+ {},+ )+ finally:+ client.shutdown()++ assert values == {+ "marketplace-search-terms": ["Research"],+ "hire-experts": True,+ "chat-search": False,+ }++class TestRawRead:@pytest.mark.asyncioasync def test_an_unconfigured_client_returns_the_default(self, mocker):@@ -288,7 +331,7 @@ class TestRawRead:@pytest.mark.asyncioasync def test_a_resolved_flag_is_evaluated(self, mocker):client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(mocker, value=False)+ client.get_feature_flag_result.return_value = answer(value=False)mocker.patch.object(ph, "get_flag_client", return_value=client)assert await ph.evaluate_flag("hire-experts", "u-1") == (False, True)@@ -296,7 +339,7 @@ class TestRawRead:@pytest.mark.asyncioasync def test_an_unresolved_flag_is_not(self, mocker):client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(mocker, value=None)+ client.get_feature_flag_result.return_value = answer(value=None)mocker.patch.object(ph, "get_flag_client", return_value=client)assert await ph.evaluate_flag("hire-experts", "u-1", default=True) == (@@ -307,8 +350,8 @@ class TestRawRead:@pytest.mark.asyncioasync def test_a_payload_wins_over_the_flag_value(self, mocker):client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(- mocker, value=True, payload={"daily": 5}+ client.get_feature_flag_result.return_value = answer(+ value=True, payload={"daily": 5})mocker.patch.object(ph, "get_flag_client", return_value=client)@@ -322,8 +365,8 @@ class TestRawRead:"""Serving the payload here would make an authoritative "off" read asa non-boolean, i.e. as "could not evaluate"."""client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(- mocker, value=False, payload={"daily": 5}+ client.get_feature_flag_result.return_value = answer(+ value=False, payload={"daily": 5})mocker.patch.object(ph, "get_flag_client", return_value=client)@@ -332,7 +375,7 @@ class TestRawRead:@pytest.mark.asyncioasync def test_a_variant_key_is_returned_as_the_value(self, mocker):client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(mocker, value="control")+ client.get_feature_flag_result.return_value = answer(value="control")mocker.patch.object(ph, "get_flag_client", return_value=client)assert await ph.evaluate_flag("stripe-product-id-topup", "u-1") == (@@ -343,20 +386,19 @@ class TestRawRead:@pytest.mark.asyncioasync def test_the_read_is_scoped_to_one_flag(self, mocker):client = mocker.Mock()- client.evaluate_flags.return_value = snapshot(mocker, value=True)+ client.get_feature_flag_result.return_value = answer(value=True)mocker.patch.object(ph, "get_flag_client", return_value=client)await ph.evaluate_flag("hire-experts", "u-1", {"role": "admin"})- args, kwargs = client.evaluate_flags.call_args- assert args[0] == "u-1"- assert kwargs["flag_keys"] == ["hire-experts"]+ args, kwargs = client.get_feature_flag_result.call_args+ assert args == ("hire-experts", "u-1")assert kwargs["person_properties"] == {"role": "admin"}@pytest.mark.asyncioasync def test_a_failed_evaluation_returns_the_default(self, mocker):client = mocker.Mock()- client.evaluate_flags.side_effect = Exception("connection refused")+ client.get_feature_flag_result.side_effect = Exception("connection refused")mocker.patch.object(ph, "get_flag_client", return_value=client)assert await ph.evaluate_flag("hire-experts", "u-1", default=True) == (@@ -375,10 +417,10 @@ class TestRawRead:with lock:entered += 1release.wait(10)- return snapshot(mocker, value=True)+ return answer(value=True)client = mocker.Mock()- client.evaluate_flags.side_effect = blocking_read+ client.get_feature_flag_result.side_effect = blocking_readmocker.patch.object(ph, "get_flag_client", return_value=client)async def scenario():@@ -427,3 +469,10 @@ def _real_client(mocker, *, flags_key: str):client, "_get_flags_decision", side_effect=ConnectionError("offline"))return client, remote+++def _rolled_out(key: str, percentage: int, payload: str | None = None) -> dict:+ filters: dict = {"groups": [{"properties": [], "rollout_percentage": percentage}]}+ if payload is not None:+ filters["payloads"] = {"true": payload}+ return {"id": 1, "key": key, "active": True, "filters": filters}diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.defaultindex 8b7007d73e..ed5ff22594 100644--- a/autogpt_platform/frontend/.env.default+++ b/autogpt_platform/frontend/.env.default@@ -69,7 +69,8 @@ NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID=687ab1372f497809b131e06e# Which vendor answers a flag read: launchdarkly (default), posthog, or dual# (read both, report every disagreement, and serve LaunchDarkly when it is# configured — otherwise PostHog, since LaunchDarkly's answer never arrives-# without its provider).+# without its provider), or server (ask our backend, which answers from the+# vendor its FEATURE_FLAG_BACKEND names, as the backend's own reads see it).NEXT_PUBLIC_FEATURE_FLAG_BACKEND=launchdarkly# Local-dev master switch: turn EVERY boolean feature flag on at once, without# listing them. Off by default. A per-flag NEXT_PUBLIC_FORCE_FLAG_<NAME> stilldiff --git a/autogpt_platform/frontend/CONTRIBUTING.md b/autogpt_platform/frontend/CONTRIBUTING.mdindex 532ace6672..ad9422bd6e 100644--- a/autogpt_platform/frontend/CONTRIBUTING.md+++ b/autogpt_platform/frontend/CONTRIBUTING.md@@ -528,9 +528,13 @@ Local dev and Playwright:Adding new flags:-1. Add the flag to the `Flag` enum and `FlagValues` type-2. Provide a mock value in the mock map-3. Configure the flag in LaunchDarkly+1. Add the flag to `FrontendFlag` in `backend/backend/util/feature_flag/frontend.py`; the frontend's `Flag` is generated from it (`pnpm generate:api`)+2. Give it a default in `defaultFlags` (`use-get-flag.ts`); `tsc` fails until you do+3. Configure the flag in the vendor that answers it, by `NEXT_PUBLIC_FEATURE_FLAG_BACKEND`:+ - `launchdarkly`: LaunchDarkly+ - `posthog`: PostHog+ - `dual`: both; create it in LaunchDarkly, then copy it over with `backend/scripts/sync_feature_flags_to_posthog.py`+ - `server`: whichever the backend's `FEATURE_FLAG_BACKEND` names, by the same rule---diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 23f4b771ba..5f6718266f 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -7858,6 +7858,46 @@}}},+ "/api/feature-flags": {+ "get": {+ "tags": ["feature-flags"],+ "summary": "Get Feature Flags",+ "description": "The frontend's flags for the caller, evaluated as the backend's own reads are.",+ "operationId": "get_feature_flags",+ "security": [{ "HTTPBearer": [] }],+ "parameters": [+ {+ "name": "visitor_id",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "description": "A signed-out visitor's analytics id, sent only with their analytics consent; ignored for a signed-in caller or when malformed.",+ "title": "Visitor Id"+ },+ "description": "A signed-out visitor's analytics id, sent only with their analytics consent; ignored for a signed-in caller or when malformed."+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/FrontendFlagValues" }+ }+ }+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },"/api/files/upload": {"post": {"tags": ["v1", "files"],@@ -23247,6 +23287,82 @@"required": ["uuid", "forgotten"],"title": "ForgetFactResponse"},+ "FrontendFlag": {+ "type": "string",+ "enum": [+ "marketplace-search-terms",+ "enable-platform-payment",+ "artifacts-page",+ "chat-mode-option",+ "generic-trigger-agents",+ "chat-search",+ "autogpt-new-layout",+ "chat-workspace-files",+ "chat-pinning",+ "task-progress-bar",+ "hire-experts",+ "skills-hub",+ "settings-notifications",+ "onboarding-brain-dump",+ "onboarding-expert-team",+ "graphiti-memory",+ "graphiti-communities-enabled",+ "dream-pass-enabled",+ "dream-pass-invalidate-entity",+ "copilot-bot-platforms",+ "copilot-voice-mode",+ "copilot-auto-mode",+ "copilot-stream-shadow",+ "copilot-stream-runtime"+ ],+ "title": "FrontendFlag",+ "description": "Every flag the frontend reads; the frontend's `Flag` is generated from this.\n\nA key the backend reads too comes from the backend's `Flag`, so it is written once.",+ "x-enum-varnames": [+ "MARKETPLACE_SEARCH_TERMS",+ "ENABLE_PLATFORM_PAYMENT",+ "ARTIFACTS_PAGE",+ "CHAT_MODE_OPTION",+ "GENERIC_TRIGGER_AGENTS",+ "CHAT_SEARCH",+ "AUTOGPT_NEW_LAYOUT",+ "CHAT_WORKSPACE_FILES",+ "CHAT_PINNING",+ "TASK_PROGRESS_BAR",+ "HIRE_EXPERTS",+ "SKILLS_HUB",+ "SETTINGS_NOTIFICATIONS",+ "ONBOARDING_BRAIN_DUMP",+ "ONBOARDING_EXPERT_TEAM",+ "GRAPHITI_MEMORY",+ "GRAPHITI_COMMUNITIES_ENABLED",+ "DREAM_PASS_ENABLED",+ "DREAM_PASS_INVALIDATE_ENTITY",+ "COPILOT_BOT_PLATFORMS",+ "COPILOT_VOICE_MODE",+ "COPILOT_AUTO_MODE",+ "COPILOT_STREAM_SHADOW",+ "COPILOT_STREAM_RUNTIME"+ ]+ },+ "FrontendFlagValues": {+ "properties": {+ "flags": {+ "additionalProperties": true,+ "propertyNames": { "$ref": "#/components/schemas/FrontendFlag" },+ "type": "object",+ "title": "Flags",+ "description": "Each flag the vendor answered, or null for one it has no definition of, which takes the frontend's default; one left out has no answer yet."+ },+ "configured": {+ "type": "boolean",+ "title": "Configured",+ "description": "False when no flag vendor is configured or it gave up starting, so every flag is its default."+ }+ },+ "type": "object",+ "required": ["flags", "configured"],+ "title": "FrontendFlagValues"+ },"GenerateTestDataRequest": {"properties": {"script_type": {diff --git a/autogpt_platform/frontend/src/app/providers.tsx b/autogpt_platform/frontend/src/app/providers.tsxindex 20f8ff1ba9..a269b0e7be 100644--- a/autogpt_platform/frontend/src/app/providers.tsx+++ b/autogpt_platform/frontend/src/app/providers.tsx@@ -14,7 +14,7 @@ import {} from "@/providers/posthog/posthog-provider";import { AdsConversionTracker } from "@/services/analytics/AdsConversionTracker";import { AttributionReporter } from "@/services/analytics/AttributionReporter";-import { LaunchDarklyProvider } from "@/services/feature-flags/feature-flag-provider";+import { FeatureFlagProvider } from "@/services/feature-flags/feature-flag-provider";import { QueryClientProvider } from "@tanstack/react-query";import { ThemeProvider, ThemeProviderProps } from "next-themes";import { NuqsAdapter } from "nuqs/adapters/next/app";@@ -38,13 +38,13 @@ export function Providers({ children, ...props }: ThemeProviderProps) {</Suspense><CredentialsProvider><OrgTeamProvider>- <LaunchDarklyProvider>+ <FeatureFlagProvider><OnboardingProvider><ThemeProvider forcedTheme="light" {...props}><TooltipProvider>{children}</TooltipProvider></ThemeProvider></OnboardingProvider>- </LaunchDarklyProvider>+ </FeatureFlagProvider></OrgTeamProvider></CredentialsProvider></BackendAPIProvider>diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/feature-flag-provider.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/feature-flag-provider.test.tsxindex 3aaa8748ca..25b8cb974f 100644--- a/autogpt_platform/frontend/src/services/feature-flags/__tests__/feature-flag-provider.test.tsx+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/feature-flag-provider.test.tsx@@ -1,7 +1,7 @@import { act, render, screen } from "@testing-library/react";import type { ReactNode } from "react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { LaunchDarklyProvider } from "../feature-flag-provider";+import { FeatureFlagProvider } from "../feature-flag-provider";import { Flag, useFlagStatus } from "../use-get-flag";const ld = vi.hoisted(() => ({@@ -57,7 +57,7 @@ function HireProbe() {);}-describe("LaunchDarklyProvider while the session loads", () => {+describe("FeatureFlagProvider while the session loads", () => {beforeEach(() => {ld.props.length = 0;ld.flags = {};@@ -66,9 +66,9 @@ describe("LaunchDarklyProvider while the session loads", () => {it("renders the page instead of a spinner, so server HTML carries it", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><h1>Maria</h1>- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(screen.getByRole("heading", { name: "Maria" })).toBeDefined();@@ -76,9 +76,9 @@ describe("LaunchDarklyProvider while the session loads", () => {it("defers LaunchDarkly until the context is known", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(ld.props.at(-1)).toEqual({@@ -89,9 +89,9 @@ describe("LaunchDarklyProvider while the session loads", () => {it("keeps gated UI in the not-answered state rather than off", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(screen.getByText("hire: false ready: false")).toBeDefined();@@ -99,17 +99,17 @@ describe("LaunchDarklyProvider while the session loads", () => {it("hands the resolved session to LaunchDarkly without remounting the page", () => {const { rerender } = render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);const probe = screen.getByText(/^hire:/);auth.state = { user: { id: "user-1" }, isUserLoading: false };rerender(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(ld.props.at(-1)?.context).toMatchObject({@@ -126,9 +126,9 @@ describe("LaunchDarklyProvider while the session loads", () => {describe("flag resolution timeout while LaunchDarkly is deferred", () => {function renderProbe() {return render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);}@@ -161,9 +161,9 @@ describe("flag resolution timeout while LaunchDarkly is deferred", () => {auth.state = { user: { id: "user-1" }, isUserLoading: false };rerender(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);act(() => {vi.advanceTimersByTime(4_000);@@ -172,9 +172,9 @@ describe("flag resolution timeout while LaunchDarkly is deferred", () => {ld.flags = { "hire-experts": true };rerender(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(screen.getByText("hire: true ready: true")).toBeDefined();@@ -185,9 +185,9 @@ describe("flag resolution timeout while LaunchDarkly is deferred", () => {auth.state = { user: null, isUserLoading: false };rerender(- <LaunchDarklyProvider>+ <FeatureFlagProvider><HireProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);act(() => {vi.advanceTimersByTime(4_999);diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsxindex 41626fb3a6..80c6419716 100644--- a/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsx+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/ld-init-timeout.test.tsx@@ -2,7 +2,7 @@ import { render, screen } from "@testing-library/react";import type { ReactNode } from "react";import { describe, expect, it, vi } from "vitest";import { LD_INIT_TIMEOUT_SECONDS } from "../constants";-import { LaunchDarklyProvider } from "../feature-flag-provider";+import { FeatureFlagProvider } from "../feature-flag-provider";import { Flag, useGetFlag } from "../use-get-flag";// launchdarkly-js-sdk-common warns above this many SECONDS ("We recommend a@@ -63,9 +63,9 @@ function FlagProbe() {describe("LaunchDarkly initialisation timeout", () => {it("gives the SDK a timeout in seconds, below the threshold it warns about", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><FlagProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(ld.timeouts).toEqual([LD_INIT_TIMEOUT_SECONDS]);@@ -76,9 +76,9 @@ describe("LaunchDarkly initialisation timeout", () => {it("renders children on the default flag values while LaunchDarkly has not answered", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><FlagProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(screen.getByText("payment: false")).toBeDefined();@@ -86,9 +86,9 @@ describe("LaunchDarkly initialisation timeout", () => {it("wires no LaunchDarkly-only Sentry inspector; the flag seam records for every vendor", () => {render(- <LaunchDarklyProvider>+ <FeatureFlagProvider><FlagProbe />- </LaunchDarklyProvider>,+ </FeatureFlagProvider>,);expect(ld.options.at(-1)).toBeUndefined();diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/server-flags.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/server-flags.test.tsxnew file mode 100644index 0000000000..882990be22--- /dev/null+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/server-flags.test.tsx@@ -0,0 +1,428 @@+import {+ afterAll,+ afterEach,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from "vitest";++// Read once at load by `flag-backend`, so it has to be set before the imports below.+const previousBackend = vi.hoisted(() => {+ const previous = process.env.NEXT_PUBLIC_FEATURE_FLAG_BACKEND;+ process.env.NEXT_PUBLIC_FEATURE_FLAG_BACKEND = "server";+ return previous;+});++import { getGetFeatureFlagsMockHandler } from "@/app/api/__generated__/endpoints/feature-flags/feature-flags.msw";+import type { FrontendFlagValues } from "@/app/api/__generated__/models/frontendFlagValues";+import openapi from "@/app/api/openapi.json";+import { useAuthStore } from "@/lib/auth/hooks/useAuthStore";+import { server } from "@/mocks/mock-server";+import { environment } from "@/services/environment";+import {+ answerCookiebot,+ configureCookiebot,+ installCookiebot,+ removeCookiebot,+} from "@/tests/integrations/cookiebot";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook, waitFor } from "@testing-library/react";+import { http, HttpResponse } from "msw";+import type { ReactNode } from "react";+import { Flag, useFlagStatus } from "../use-get-flag";++vi.mock("@/app/(platform)/marketplace/components/HeroSection/helpers", () => ({+ DEFAULT_SEARCH_TERMS: [],+}));++const HIRE_EXPERTS = "hire-experts";+const VISITOR = "0b6c1f3e-visitor-0001";++vi.mock("posthog-js", () => ({+ default: { __loaded: true, get_distinct_id: () => VISITOR },+}));++beforeEach(() => {+ signIn("u-1");+});++afterAll(() => {+ if (previousBackend === undefined) {+ delete process.env.NEXT_PUBLIC_FEATURE_FLAG_BACKEND;+ } else {+ process.env.NEXT_PUBLIC_FEATURE_FLAG_BACKEND = previousBackend;+ }+ useAuthStore.setState({ user: null, isUserLoading: true });+});++describe("server backend", () => {+ it("serves the server's answer, and a flag it left out is not answered yet", async () => {+ serveFlags({ flags: { [HIRE_EXPERTS]: true }, configured: true });+ const { result } = renderHook(+ () => ({+ hire: useFlagStatus(Flag.HIRE_EXPERTS),+ search: useFlagStatus(Flag.CHAT_SEARCH),+ }),+ { wrapper: withQueryClient() },+ );++ await waitFor(() => expect(result.current.hire.answered).toBe(true));+ expect(result.current.hire.enabled).toBe(true);+ // A gated route must keep waiting rather than 404 on a flag the vendor never answered.+ expect(result.current.search).toMatchObject({+ enabled: false,+ answered: false,+ });+ });++ it("answers every flag with its default when the server has no vendor", async () => {+ serveFlags({ flags: {}, configured: false });++ const { result } = renderHook(() => useFlagStatus(Flag.CHAT_SEARCH), {+ wrapper: withQueryClient(),+ });++ await waitFor(() => expect(result.current.answered).toBe(true));+ expect(result.current.enabled).toBe(false);+ });++ describe("while the server has no answer for a flag", () => {+ beforeEach(() => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ });++ afterEach(() => {+ vi.useRealTimers();+ });++ it("asks again until the vendor answers it, then stops", async () => {+ const answers = [{}, {}, { [HIRE_EXPERTS]: true }];+ let requests = 0;+ serveFlags(() => ({+ flags: answers[Math.min(requests++, answers.length - 1)],+ configured: true,+ }));++ const { result } = renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(requests).toBe(1));+ await act(() => vi.advanceTimersByTimeAsync(60_000));++ expect(result.current).toMatchObject({ enabled: true, answered: true });+ const answeredAfter = requests;+ await act(() => vi.advanceTimersByTimeAsync(10 * 60_000));+ expect(requests).toBe(answeredAfter);+ });++ it("serves the default at once for a flag the vendor has no definition of", async () => {+ let requests = 0;+ serveFlags(() => {+ requests++;+ return { flags: { [HIRE_EXPERTS]: null }, configured: true };+ });++ const { result } = renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() =>+ expect(result.current).toMatchObject({+ enabled: false,+ answered: true,+ }),+ );+ await act(() => vi.advanceTimersByTimeAsync(10 * 60_000));++ expect(requests).toBe(1);+ });++ it("asks again after a request that failed, until one succeeds", async () => {+ let requests = 0;+ server.use(+ http.get("/api/proxy/api/feature-flags", () =>+ requests++ === 0+ ? HttpResponse.json({ detail: "bad gateway" }, { status: 502 })+ : HttpResponse.json({+ flags: { [HIRE_EXPERTS]: true },+ configured: true,+ }),+ ),+ );++ const { result } = renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(requests).toBe(1));+ await act(() => vi.advanceTimersByTimeAsync(60_000));++ expect(result.current).toMatchObject({ enabled: true, answered: true });+ });++ it("backs off on failed requests too, so an outage is not hammered", async () => {+ let requests = 0;+ server.use(+ http.get("/api/proxy/api/feature-flags", () => {+ requests++;+ return HttpResponse.json({ detail: "bad gateway" }, { status: 502 });+ }),+ );++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(requests).toBe(1));+ await act(() => vi.advanceTimersByTimeAsync(10 * 60_000));++ // Every 2.5 s would be 240; backing off to a minute is a dozen or so.+ expect(requests).toBeGreaterThan(4);+ expect(requests).toBeLessThan(20);+ });++ it("backs off to about once a minute when it never answers", async () => {+ let requests = 0;+ serveFlags(() => {+ requests++;+ return { flags: {}, configured: true };+ });++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(requests).toBe(1));+ await act(() => vi.advanceTimersByTimeAsync(10 * 60_000));++ // Every 5 s would be 120; backing off to a minute is a dozen or so.+ expect(requests).toBeGreaterThan(4);+ expect(requests).toBeLessThan(20);+ });++ it.each([+ [+ "a flag only another component reads",+ { flags: { [HIRE_EXPERTS]: true }, configured: true },+ ],+ [+ "any flag, with no vendor to wait for",+ { flags: {}, configured: false },+ ],+ ])("never asks again over %s", async (_, response) => {+ let requests = 0;+ serveFlags(() => {+ requests++;+ return response;+ });++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(requests).toBe(1));+ await act(() => vi.advanceTimersByTimeAsync(10 * 60_000));++ expect(requests).toBe(1);+ });+ });++ it("never shows one user's flags to the next while theirs load", async () => {+ let requests = 0;+ server.use(+ getGetFeatureFlagsMockHandler(async () => {+ if (requests++ === 0) {+ return { flags: { [HIRE_EXPERTS]: true }, configured: true };+ }+ await new Promise((resolve) => setTimeout(resolve, 200));+ return { flags: { [HIRE_EXPERTS]: false }, configured: true };+ }),+ );+ const seen = renderRecording(() => useFlagStatus(Flag.HIRE_EXPERTS));+ await waitFor(() =>+ expect(seen.at(-1)).toMatchObject({ enabled: true, answered: true }),+ );+ const before = seen.length;++ act(() => signIn("u-2"));+ await waitFor(() =>+ expect(seen.at(-1)).toMatchObject({ enabled: false, answered: true }),+ );++ expect(seen.slice(before).some((s) => s.enabled && s.answered)).toBe(false);+ });++ it("waits for the session, then reads each signed-in user's own flags", async () => {+ const answers = [true, false];+ let requests = 0;+ serveFlags(() => ({+ flags: { [HIRE_EXPERTS]: answers[requests++] },+ configured: true,+ }));+ useAuthStore.setState({ isUserLoading: true });++ const { result } = renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await new Promise((resolve) => setTimeout(resolve, 50));+ expect(requests).toBe(0);++ act(() => signIn("u-1"));+ await waitFor(() => expect(requests).toBe(1));+ await waitFor(() => expect(result.current.enabled).toBe(true));+ act(() => signIn("u-2"));+ await waitFor(() => expect(requests).toBe(2));+ await waitFor(() =>+ expect(result.current).toMatchObject({ enabled: false, answered: true }),+ );+ });+});++describe("a signed-out visitor", () => {+ beforeEach(() => {+ useAuthStore.setState({ user: null, isUserLoading: false });+ vi.spyOn(environment, "isPostHogEnabled").mockReturnValue("phc_test");+ configureCookiebot();+ });++ afterEach(() => {+ removeCookiebot();+ vi.unstubAllEnvs();+ vi.restoreAllMocks();+ });++ it("who allowed analytics is read under their own PostHog id", async () => {+ installCookiebot({ statistics: true });+ const sent = recordVisitorIDs();++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });++ await waitFor(() => expect(sent).toEqual([VISITOR]));+ });++ it.each([+ ["declined", { statistics: false }],+ ["has not answered", undefined],+ ])("who %s sends no id", async (_, answer) => {+ installCookiebot(answer);+ const sent = recordVisitorIDs();++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });++ await waitFor(() => expect(sent).toEqual([null]));+ });++ it("sends no id once signed in, consent or not", async () => {+ installCookiebot({ statistics: true });+ signIn("u-1");+ const sent = recordVisitorIDs();++ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });++ await waitFor(() => expect(sent).toEqual([null]));+ });++ it("keeps the shared answers until their own land when they allow analytics", async () => {+ installCookiebot();+ server.use(+ getGetFeatureFlagsMockHandler(async ({ request }) => {+ if (!new URL(request.url).searchParams.get("visitor_id")) {+ return { flags: { [HIRE_EXPERTS]: false }, configured: true };+ }+ await new Promise((resolve) => setTimeout(resolve, 200));+ return { flags: { [HIRE_EXPERTS]: true }, configured: true };+ }),+ );+ const seen = renderRecording(() => useFlagStatus(Flag.HIRE_EXPERTS));+ await waitFor(() =>+ expect(seen.at(-1)).toMatchObject({ enabled: false, answered: true }),+ );+ const before = seen.length;++ act(() => answerCookiebot({ statistics: true }));+ await waitFor(() => expect(seen.at(-1)).toMatchObject({ enabled: true }));++ // A gate must not drop back to "not answered yet" while their own load.+ expect(seen.slice(before).every((s) => s.answered)).toBe(true);+ });++ it("is read again under their id when they allow analytics", async () => {+ installCookiebot();+ const sent = recordVisitorIDs();+ renderHook(() => useFlagStatus(Flag.HIRE_EXPERTS), {+ wrapper: withQueryClient(),+ });+ await waitFor(() => expect(sent).toEqual([null]));++ act(() => answerCookiebot({ statistics: true }));++ await waitFor(() => expect(sent).toEqual([null, VISITOR]));+ });+});++describe("the frontend's flags are the set the endpoint serves", () => {+ it("keys the endpoint's flags by the same enum the frontend reads from", () => {+ const schemas = openapi.components.schemas;+ const response =+ openapi.paths["/api/feature-flags"].get.responses["200"].content[+ "application/json"+ ].schema.$ref;++ expect(response).toBe("#/components/schemas/FrontendFlagValues");+ expect(schemas.FrontendFlagValues.properties.flags.propertyNames.$ref).toBe(+ "#/components/schemas/FrontendFlag",+ );+ expect([...schemas.FrontendFlag.enum].sort()).toEqual(+ Object.values(Flag).sort(),+ );+ });+});++function renderRecording<T>(read: () => T) {+ const seen: T[] = [];+ renderHook(+ () => {+ const value = read();+ seen.push(value);+ return value;+ },+ { wrapper: withQueryClient() },+ );+ return seen;+}++function recordVisitorIDs() {+ const sent: Array<string | null> = [];+ server.use(+ getGetFeatureFlagsMockHandler(({ request }) => {+ sent.push(new URL(request.url).searchParams.get("visitor_id"));+ return { flags: {}, configured: true };+ }),+ );+ return sent;+}++function serveFlags(response: FrontendFlagValues | (() => FrontendFlagValues)) {+ server.use(getGetFeatureFlagsMockHandler(response));+}++function signIn(userID: string) {+ useAuthStore.setState({+ user: { id: userID, email: `${userID}@example.com`, user_metadata: {} },+ isUserLoading: false,+ });+}++function withQueryClient() {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return function Wrapper({ children }: { children: ReactNode }) {+ return (+ <QueryClientProvider client={client}>{children}</QueryClientProvider>+ );+ };+}diff --git a/autogpt_platform/frontend/src/services/feature-flags/backends/server.ts b/autogpt_platform/frontend/src/services/feature-flags/backends/server.tsnew file mode 100644index 0000000000..1148e01e28--- /dev/null+++ b/autogpt_platform/frontend/src/services/feature-flags/backends/server.ts@@ -0,0 +1,74 @@+"use client";++import {+ getGetFeatureFlagsQueryKey,+ useGetFeatureFlags,+ type getFeatureFlagsResponse,+} from "@/app/api/__generated__/endpoints/feature-flags/feature-flags";+import { useAuthStore } from "@/lib/auth/hooks/useAuthStore";+import { useConsent } from "@/services/consent/useConsent";+import { environment } from "@/services/environment";+import posthog from "posthog-js";+import type { FlagSourceResult } from "../flag-source";++const FLAGS_STALE_MS = 60_000;+const UNANSWERED_RETRY_MS = 5_000;++export function useServerFlag(key: string): FlagSourceResult {+ const userID = useAuthStore((state) => state.user?.id ?? null);+ const isUserLoading = useAuthStore((state) => state.isUserLoading);+ const { analytics } = useConsent();+ const visitorID = userID === null ? consentedVisitorID(analytics) : null;+ const params = visitorID ? { visitor_id: visitorID } : undefined;+ // Keyed by user and visitor id, so signing in or out, or answering the+ // consent banner, reads the matching flags rather than the cached ones.+ const { data } = useGetFeatureFlags(params, {+ query: {+ queryKey: [...getGetFeatureFlagsQueryKey(params), userID],+ enabled: !isUserLoading,+ staleTime: FLAGS_STALE_MS,+ // A consent answer keeps the last flags until the new ones land; another+ // user never sees them.+ placeholderData: (previous, previousQuery) =>+ previousQuery?.queryKey.at(-1) === userID ? previous : undefined,+ // A failed request, or a 200 leaving this flag out (vendor down or+ // starting), is asked again, backing off to once a minute.+ refetchInterval: ({ state }) =>+ (state.status === "error" || isUnanswered(state.data, key)) &&+ Math.min(+ UNANSWERED_RETRY_MS *+ 2 ** (state.dataUpdateCount + state.errorUpdateCount - 1),+ FLAGS_STALE_MS,+ ),+ select: (response) => (response.status === 200 ? response.data : null),+ },+ });++ if (!data) return { value: undefined, resolved: false };+ // A flag left out has no answer yet, so it resolves only with no vendor to wait+ // for; one answered null has no definition there, so its default stands.+ return {+ value: data.flags[key],+ resolved: key in data.flags || !data.configured,+ };+}++// Only with analytics consent does a signed-out visitor get their own bucket;+// without it the server reads them as the one shared anonymous visitor.+function consentedVisitorID(analytics: boolean) {+ if (!analytics || !environment.isPostHogEnabled() || !posthog.__loaded) {+ return null;+ }+ return posthog.get_distinct_id() || null;+}++function isUnanswered(+ response: getFeatureFlagsResponse | undefined,+ key: string,+) {+ return (+ response?.status === 200 &&+ response.data.configured &&+ !(key in response.data.flags)+ );+}diff --git a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsxindex 1aac229396..e1a19e6f13 100644--- a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx+++ b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx@@ -7,11 +7,11 @@ import { useMemo } from "react";import { getAnonymousID } from "../analytics/anonymous-id";import { environment } from "../environment";import { LD_INIT_TIMEOUT_SECONDS } from "./constants";-import { usesLaunchDarkly } from "./flag-backend";+import { FLAG_BACKEND, usesLaunchDarkly } from "./flag-backend";import { FlagResolutionContext } from "./flag-resolution";import { buildLDContext } from "./helpers";-export function LaunchDarklyProvider({ children }: { children: ReactNode }) {+export function FeatureFlagProvider({ children }: { children: ReactNode }) {const { user, isUserLoading } = useAuth();const envEnabled = usesLaunchDarkly() && environment.areFeatureFlagsEnabled();const clientId = environment.getLaunchDarklyClientId();@@ -26,6 +26,15 @@ export function LaunchDarklyProvider({ children }: { children: ReactNode }) {return buildLDContext(user, getAnonymousID());}, [user, isUserLoading]);+ if (FLAG_BACKEND === "server") {+ // The server reads flags for the signed-in user, so the wait starts once the session is known.+ return (+ <FlagResolutionContext.Provider value={!isUserLoading}>+ {children}+ </FlagResolutionContext.Provider>+ );+ }+if (!envEnabled) {return <>{children}</>;}diff --git a/autogpt_platform/frontend/src/services/feature-flags/flag-backend.ts b/autogpt_platform/frontend/src/services/feature-flags/flag-backend.tsindex 4d075d1939..01dcd814e5 100644--- a/autogpt_platform/frontend/src/services/feature-flags/flag-backend.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/flag-backend.ts@@ -1,20 +1,21 @@-export type FlagBackend = "launchdarkly" | "posthog" | "dual";+// `server` asks our backend, which answers from whichever vendor it is configured for.+export type FlagBackend = "launchdarkly" | "posthog" | "dual" | "server";// Read as a literal so Next/Turbopack inlines it into the client bundle; a// dynamic ``process.env[name]`` lookup is always empty in the browser.const configured = process.env.NEXT_PUBLIC_FEATURE_FLAG_BACKEND;export const FLAG_BACKEND: FlagBackend =- configured === "posthog" || configured === "dual"+ configured === "posthog" || configured === "dual" || configured === "server"? configured: "launchdarkly";export function usesLaunchDarkly() {- return FLAG_BACKEND !== "posthog";+ return FLAG_BACKEND === "launchdarkly" || FLAG_BACKEND === "dual";}export function usesPostHog() {- return FLAG_BACKEND !== "launchdarkly";+ return FLAG_BACKEND === "posthog" || FLAG_BACKEND === "dual";}// Mirrors ``environment.isPostHogEnabled`` — including its cloud check, sincediff --git a/autogpt_platform/frontend/src/services/feature-flags/flag-source.ts b/autogpt_platform/frontend/src/services/feature-flags/flag-source.tsindex 71b611d360..ab1964938d 100644--- a/autogpt_platform/frontend/src/services/feature-flags/flag-source.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/flag-source.ts@@ -3,6 +3,7 @@import { useDualFlag } from "./backends/dual";import { useLaunchDarklyFlag } from "./backends/launchdarkly";import { usePostHogFlag } from "./backends/posthog";+import { useServerFlag } from "./backends/server";import { FLAG_BACKEND } from "./flag-backend";export interface FlagSourceResult {@@ -21,8 +22,10 @@ export function useFlagSource(key: string): FlagSourceResult {// Picked at module load, not per render: the backend is a build-time constant,// so the hooks a component calls stay stable for the life of the app.const useSelectedFlagSource =- FLAG_BACKEND === "posthog"- ? usePostHogFlag- : FLAG_BACKEND === "dual"- ? useDualFlag- : useLaunchDarklyFlag;+ FLAG_BACKEND === "server"+ ? useServerFlag+ : FLAG_BACKEND === "posthog"+ ? usePostHogFlag+ : FLAG_BACKEND === "dual"+ ? useDualFlag+ : useLaunchDarklyFlag;diff --git a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.tsindex 28a144d5af..27996c261a 100644--- a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts@@ -6,68 +6,14 @@ import * as Sentry from "@sentry/nextjs";import type { FeatureFlagsIntegration } from "@sentry/nextjs";import { useEffect, useState } from "react";import { FLAG_BACKEND, isPostHogFlagsEnabled } from "./flag-backend";+import { FrontendFlag as Flag } from "@/app/api/__generated__/models/frontendFlag";import { useFlagResolutionStarted } from "./flag-resolution";import { useFlagSource } from "./flag-source";import { usePwFlagOverrides } from "./pw-flag-overrides";-export enum Flag {- MARKETPLACE_SEARCH_TERMS = "marketplace-search-terms",- ENABLE_PLATFORM_PAYMENT = "enable-platform-payment",- ARTIFACTS_PAGE = "artifacts-page",- CHAT_MODE_OPTION = "chat-mode-option",- GENERIC_TRIGGER_AGENTS = "generic-trigger-agents",- CHAT_SEARCH = "chat-search",- AUTOGPT_NEW_LAYOUT = "autogpt-new-layout",- CHAT_WORKSPACE_FILES = "chat-workspace-files",- CHAT_PINNING = "chat-pinning",- TASK_PROGRESS_BAR = "task-progress-bar",- HIRE_EXPERTS = "hire-experts",- // Reveals the marketplace Skills shelf and the skill listing pages.- SKILLS_HUB = "skills-hub",- // Reveals the notification-preferences card on /settings/account. The card- // is built but its design is still being reworked, so it ships dark and is- // targeted at AGPT staff in LaunchDarkly. Until this is on for everyone,- // /profile/settings stays un-redirected (``useNewSettingsRedirect``) so the- // toggles remain reachable for everyone else — flip both together.- SETTINGS_NOTIFICATIONS = "settings-notifications",- // Replaces the onboarding pillbox step with the voice brain dump.- // Mirror of the backend ``Flag`` enum — the endpoints 404 when off, so- // both sides must agree. Off renders the pillbox flow untouched.- ONBOARDING_BRAIN_DUMP = "onboarding-brain-dump",- // Child of HIRE_EXPERTS: the greeting page builds a team from the brain- // dump. Mirror of the backend ``Flag`` enum; both must be on.- ONBOARDING_EXPERT_TEAM = "onboarding-expert-team",- // Graphiti memory + dream-system gates. Mirror of the backend- // ``Flag`` enum in ``backend/util/feature_flag.py``. Frontend reads- // them when memory/dream-related UI surfaces ship (P6+ on the- // dream-system roadmap). They default false below to match the- // backend's fail-closed gating (default=False, opt-in only) — a- // LaunchDarkly outage or missing flag key must not switch the- // feature on. Use ``NEXT_PUBLIC_FORCE_FLAG_*`` env overrides to- // enable the stack for local-dev / Playwright runs.- GRAPHITI_MEMORY = "graphiti-memory",- GRAPHITI_COMMUNITIES_ENABLED = "graphiti-communities-enabled",- DREAM_PASS_ENABLED = "dream-pass-enabled",- DREAM_PASS_INVALIDATE_ENTITY = "dream-pass-invalidate-entity",- // JSON flag mapping copilot-bot platform key (lowercase) -> visible on the- // Bots settings page. Lets ops hide a platform (e.g. Slack while its- // Marketplace review is pending) without a deploy. Missing keys default to- // visible — only an explicit ``false`` hides a card.- COPILOT_BOT_PLATFORMS = "copilot-bot-platforms",- // Voice mode on /copilot: hands-free listen → send → speak → listen.- // Mirror of the backend ``Flag`` enum — the speech endpoint 404s when off,- // so both sides must agree. Fail-closed.- COPILOT_VOICE_MODE = "copilot-voice-mode",- // The chat's approval mode selector. Mirror of the backend ``Flag`` enum,- // which ignores a sent mode when off. Fail-closed.- COPILOT_AUTO_MODE = "copilot-auto-mode",- // The share of chat sessions whose stream the converter shadows and checks- // against the server, 0 to 1 (a PostHog payload). 0 is off.- COPILOT_STREAM_SHADOW = "copilot-stream-shadow",- // The chat's own stream runtime in place of the AI SDK's: resume at a- // cursor, render from the persisted rows. Frontend only; fail-closed.- COPILOT_STREAM_RUNTIME = "copilot-stream-runtime",-}+// Declared once, on the backend (`FrontendFlag` in `backend/util/feature_flag/frontend.py`),+// which is also the set `/api/feature-flags` serves.+export { Flag };const isPwMockEnabled = process.env.NEXT_PUBLIC_PW_TEST === "true";@@ -102,7 +48,7 @@ const defaultFlags = {[Flag.COPILOT_AUTO_MODE]: false,[Flag.COPILOT_STREAM_SHADOW]: 0,[Flag.COPILOT_STREAM_RUNTIME]: false,-};+} satisfies Record<Flag, unknown>;type FlagValues = typeof defaultFlags;@@ -322,6 +268,8 @@ export function resolveFlagValue<T extends Flag>(// mocks stub. This is the same question asked of whichever vendor is configured.function areFeatureFlagsEnabled() {switch (FLAG_BACKEND) {+ case "server":+ return true;case "posthog":return isPostHogFlagsEnabled();case "dual":diff --git a/docs/engineering/feature-flags.md b/docs/engineering/feature-flags.mdindex f2d1d765d0..1dd1224d2f 100644--- a/docs/engineering/feature-flags.md+++ b/docs/engineering/feature-flags.md@@ -80,6 +80,17 @@ dev-only conditions like `environment = dev`. A condition like`environment = prod` or "`environment` is not `dev`" would turn on in prod'sbackend reads but stay off in prod's browser until prod runs `server` mode.+**Splitting signed-out visitors.** A visitor who allowed analytics is bucketed+under `visitor:<their PostHog id>`, but only by conditions their read can+decide: a rollout percentage, `environment` and `is_anonymous`, with no email.+A condition group on another person property or a cohort (the migrated+"email in employees", for one) can't be decided for a visitor, so a visitor+whom no decidable group matches gets the shared signed-out answer instead.+"Employees OR 20%" is then all or nothing for visitors, whichever side the+shared `anonymous` key lands on. For a real 20% of visitors, add+`is_anonymous` is not `true` to the rollout group: the shared answer is then+off, and only each visitor's own bucket turns the flag on.+## Sharing PostHog's flag definitionsPostHog evaluates flags locally, in the process, from a set of flag definitions