Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): load feature flags at startup in every service that reads them (#15370)

Reinier van der Leere6178dd2c75f
diff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.py
index 4f41fdcb3f..afda27afb5 100644
--- a/autogpt_platform/backend/backend/api/rest_api.py
+++ b/autogpt_platform/backend/backend/api/rest_api.py
@@ -106,7 +106,6 @@ from backend.util.exceptions import (
NotFoundError,
PreconditionFailed,
)
-from backend.util.feature_flag import initialize_feature_flags, shutdown_feature_flags
from backend.util.service import UnhealthyServiceError
from backend.util.workspace_storage import shutdown_workspace_storage
@@ -128,18 +127,6 @@ logging.getLogger("autogpt_libs").setLevel(logging.INFO)
_webhook_bot_backend = BotBackend()
-def feature_flag_context():
- if settings.config.app_env != backend.util.settings.AppEnvironment.LOCAL:
- initialize_feature_flags()
- try:
- yield
- finally:
- shutdown_feature_flags()
- else:
- yield
-
-
@contextlib.asynccontextmanager
async def lifespan_context(app: fastapi.FastAPI):
verify_auth_settings()
@@ -202,8 +189,7 @@ async def lifespan_context(app: fastapi.FastAPI):
# Fail-hard: the catalog is load-bearing — a broken load stops the boot.
backend.data.llm_registry.load_catalog()
- with feature_flag_context():
- yield
+ yield
try:
from backend.api.features.integrations.codex import codex_login_coordinator
diff --git a/autogpt_platform/backend/backend/api/ws_api.py b/autogpt_platform/backend/backend/api/ws_api.py
index f6c115d7f7..2eb5cf3e11 100644
--- a/autogpt_platform/backend/backend/api/ws_api.py
+++ b/autogpt_platform/backend/backend/api/ws_api.py
@@ -339,6 +339,8 @@ async def health():
class WebsocketServer(AppProcess):
+ reads_feature_flags = False
+
def run(self):
logger.info(f"CORS allow origins: {settings.config.backend_cors_allow_origins}")
cors_params = build_cors_params(
diff --git a/autogpt_platform/backend/backend/cli/main.py b/autogpt_platform/backend/backend/cli/main.py
index e7c960a20e..6bed8e2854 100644
--- a/autogpt_platform/backend/backend/cli/main.py
+++ b/autogpt_platform/backend/backend/cli/main.py
@@ -60,6 +60,9 @@ def start():
class MainApp(AppProcess):
+ # Only launches the services, each of which starts its own flags.
+ reads_feature_flags = False
+
def run(self):
from backend import app
diff --git a/autogpt_platform/backend/backend/copilot/bot/app.py b/autogpt_platform/backend/backend/copilot/bot/app.py
index f4960bf4a5..dbdec9694e 100644
--- a/autogpt_platform/backend/backend/copilot/bot/app.py
+++ b/autogpt_platform/backend/backend/copilot/bot/app.py
@@ -37,6 +37,9 @@ _NO_ADAPTER_SLEEP_SECONDS = 3600
class CoPilotChatBridge(AppService):
"""Bridges Otto to external chat platforms via per-platform adapters."""
+ # Its chat turns are gated by PlatformLinkingManager, which reads the flags.
+ reads_feature_flags = False
+
def __init__(self):
super().__init__()
# Flipped to True once `_run_adapters` reaches its blocking gather
diff --git a/autogpt_platform/backend/backend/executor/batch_executor.py b/autogpt_platform/backend/backend/executor/batch_executor.py
index 1162e872aa..cf60a36e5e 100644
--- a/autogpt_platform/backend/backend/executor/batch_executor.py
+++ b/autogpt_platform/backend/backend/executor/batch_executor.py
@@ -424,6 +424,7 @@ class BatchExecutor(AppService):
Health check just confirms the loop thread is alive.
"""
+ reads_feature_flags = False
_loop_thread: threading.Thread | None = None
_shutdown_event: threading.Event
diff --git a/autogpt_platform/backend/backend/executor/jobstore_test.py b/autogpt_platform/backend/backend/executor/jobstore_test.py
index bac1ef0697..0c60eb4ddc 100644
--- a/autogpt_platform/backend/backend/executor/jobstore_test.py
+++ b/autogpt_platform/backend/backend/executor/jobstore_test.py
@@ -318,7 +318,6 @@ def _scheduler_wired_to(store):
with (
patch(f"{_SCHEDULER_PATH}.BackgroundScheduler", return_value=MagicMock()),
patch(f"{_SCHEDULER_PATH}.load_dotenv"),
- patch(f"{_SCHEDULER_PATH}._init_feature_flags_for_scheduler"),
patch(f"{_SCHEDULER_PATH}.asyncio.new_event_loop", return_value=MagicMock()),
patch(f"{_SCHEDULER_PATH}.threading.Thread", return_value=MagicMock()),
patch(f"{_SCHEDULER_PATH}.create_engine", return_value=MagicMock()),
diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.py
index 42280d5432..cd8ccff76d 100644
--- a/autogpt_platform/backend/backend/executor/scheduler.py
+++ b/autogpt_platform/backend/backend/executor/scheduler.py
@@ -80,7 +80,6 @@ from backend.util.exceptions import (
NotFoundError,
UserPaywalledError,
)
-from backend.util.feature_flag import initialize_feature_flags, shutdown_feature_flags
from backend.util.logging import PrefixFilter
from backend.util.retry import func_retry
from backend.util.service import (
@@ -90,7 +89,7 @@ from backend.util.service import (
endpoint_to_async,
expose,
)
-from backend.util.settings import AppEnvironment, Config
+from backend.util.settings import Config
def _extract_schema_from_url(database_url) -> tuple[str, str]:
@@ -128,27 +127,6 @@ SCHEDULER_OPERATION_TIMEOUT_SECONDS = 300 # 5 minutes for scheduler operations
SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS = 1800
-def _init_feature_flags_for_scheduler() -> None:
- """Eagerly initialize the feature flag backend unless running LOCAL.
-
- Mirrors ``rest_api.py``'s ``feature_flag_context``: the @expose flag
- gates (e.g. ``add_nightly_batch_schedule``'s ``DREAM_PASS_ENABLED``
- check) fail closed, so evaluating them against a lazily-initialized
- client right after a pod restart would silently skip registrations
- until the first lazy init completed. Skipped LOCAL, where no vendor is
- configured and flags resolve to their mock defaults.
- """
- if config.app_env != AppEnvironment.LOCAL:
- initialize_feature_flags()
-
-
-def _shutdown_feature_flags_for_scheduler() -> None:
- """Reverse of ``_init_feature_flags_for_scheduler`` — only tears down the
- client when it was actually initialized (non-LOCAL)."""
- if config.app_env != AppEnvironment.LOCAL:
- shutdown_feature_flags()
-
-
# The Stripe tier sweep pages through every active subscription, so it needs a
# generous ceiling relative to the per-op default.
STRIPE_RECONCILE_TIMEOUT_SECONDS = 1800 # 30 minutes
@@ -2127,11 +2105,6 @@ class Scheduler(AppService):
def run_service(self):
load_dotenv()
- # Eagerly initialize LaunchDarkly before any @expose flag gate can
- # run (see the helper for why lazy init would skip registrations
- # after a pod restart).
- _init_feature_flags_for_scheduler()
-
# Initialize the event loop for async jobs
global _event_loop
_event_loop = asyncio.new_event_loop()
@@ -2443,10 +2416,6 @@ class Scheduler(AppService):
logger.info("⏳ Waiting for event loop thread to finish...")
_event_loop_thread.join(timeout=SCHEDULER_OPERATION_TIMEOUT_SECONDS)
- # Reverse order of run_service: LD was initialized before the
- # scheduler started, so close it after all jobs have drained.
- _shutdown_feature_flags_for_scheduler()
-
super().cleanup()
def _persist_schedule(
diff --git a/autogpt_platform/backend/backend/executor/scheduler_test.py b/autogpt_platform/backend/backend/executor/scheduler_test.py
index 70d9ecff47..4472697a37 100644
--- a/autogpt_platform/backend/backend/executor/scheduler_test.py
+++ b/autogpt_platform/backend/backend/executor/scheduler_test.py
@@ -839,60 +839,6 @@ def test_build_trigger_unix_dow_various_cases(
assert nxt.strftime("%A") == expected_dow
-# ---------------------------------------------------------------------------
-# Feature flag lifecycle — the scheduler eagerly inits the flag backend in
-# run_service (so @expose flag gates don't fail-closed right after a pod
-# restart) and tears it down in cleanup, both gated on the non-LOCAL app env.
-# Test the gate at the boundary where the symbols are used (the scheduler).
-# ---------------------------------------------------------------------------
-
-
-class TestFeatureFlagLifecycle:
- def test_init_runs_when_app_env_not_local(self) -> None:
- from backend.executor import scheduler as sched
- from backend.util.settings import AppEnvironment
-
- with (
- patch.object(sched.config, "app_env", AppEnvironment.PRODUCTION),
- patch.object(sched, "initialize_feature_flags") as init,
- ):
- sched._init_feature_flags_for_scheduler()
- init.assert_called_once()
-
- def test_init_skipped_when_app_env_local(self) -> None:
- from backend.executor import scheduler as sched
- from backend.util.settings import AppEnvironment
-
- with (
- patch.object(sched.config, "app_env", AppEnvironment.LOCAL),
- patch.object(sched, "initialize_feature_flags") as init,
- ):
- sched._init_feature_flags_for_scheduler()
- init.assert_not_called()
-
- def test_shutdown_runs_when_app_env_not_local(self) -> None:
- from backend.executor import scheduler as sched
- from backend.util.settings import AppEnvironment
-
- with (
- patch.object(sched.config, "app_env", AppEnvironment.PRODUCTION),
- patch.object(sched, "shutdown_feature_flags") as shutdown,
- ):
- sched._shutdown_feature_flags_for_scheduler()
- shutdown.assert_called_once()
-
- def test_shutdown_skipped_when_app_env_local(self) -> None:
- from backend.executor import scheduler as sched
- from backend.util.settings import AppEnvironment
-
- with (
- patch.object(sched.config, "app_env", AppEnvironment.LOCAL),
- patch.object(sched, "shutdown_feature_flags") as shutdown,
- ):
- sched._shutdown_feature_flags_for_scheduler()
- shutdown.assert_not_called()
-
-
def _counter(name: str, **labels) -> float:
from prometheus_client import REGISTRY
diff --git a/autogpt_platform/backend/backend/util/feature_flag/__init__.py b/autogpt_platform/backend/backend/util/feature_flag/__init__.py
index 24039db7d4..583dffaf1b 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/__init__.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/__init__.py
@@ -238,7 +238,10 @@ def _stop_shadow_evaluations() -> None:
global _shadow_evaluations_stopped
_shadow_evaluations_stopped = True
for task in list(_shadow_evaluations):
- task.cancel()
+ # A service's cleanup can close the task's loop first, and cancelling
+ # a task on a closed loop raises.
+ if not task.get_loop().is_closed():
+ task.cancel()
_shadow_evaluations.clear()
@@ -298,9 +301,8 @@ def shutdown_launchdarkly() -> None:
if not _is_initialized:
# `initialize_launchdarkly` returns early when no SDK key is configured,
# so `ldclient.set_config` was never called and `ldclient.get()` would
- # raise "set_config was not called". Callers pair init/shutdown on
- # app_env alone (see `rest_api.feature_flag_context` and
- # `scheduler._shutdown_feature_flags_for_scheduler`), so an unconfigured
+ # raise "set_config was not called". `AppProcess.execute_run_command`
+ # pairs init/shutdown on app_env alone, so an unconfigured
# non-LOCAL deployment would otherwise raise out of service teardown and
# leave the process alive instead of exiting.
return
diff --git a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py b/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
index c8e11bc15d..5347b3b1ea 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
@@ -664,6 +664,29 @@ class TestShutdownStopsShadowEvaluations:
assert ff._shadow_evaluations == set()
+ def test_a_read_left_on_a_closed_loop_does_not_stop_teardown(
+ self, mocker, ld_client, user_context
+ ):
+ use_backend(mocker, FeatureFlagBackend.DUAL)
+ mocker.patch.object(ff, "shutdown_launchdarkly")
+ posthog_shutdown = mocker.patch.object(ph, "shutdown_posthog_flags")
+ ld_client.variation_detail.return_value = served(True)
+
+ async def never_finishes(*args, **kwargs):
+ await asyncio.sleep(30)
+ return False, True
+
+ mocker.patch.object(ph, "evaluate_flag", side_effect=never_finishes)
+ loop = asyncio.new_event_loop()
+ loop.run_until_complete(evaluate_feature_flag(Flag.HIRE_EXPERTS, "u-1"))
+ # What an AppService's cleanup does to its loop before flags shut down.
+ loop.close()
+
+ ff.shutdown_feature_flags()
+
+ posthog_shutdown.assert_called_once()
+ assert ff._shadow_evaluations == set()
+
@pytest.mark.asyncio
async def test_a_read_during_teardown_starts_no_new_client(
self, mocker, ld_client, user_context
diff --git a/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py b/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py
index fb4fe23cb8..b53a52ea52 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/definition_cache.py
@@ -6,8 +6,8 @@ with replica count. Here one elected process refreshes the definitions and
writes them to Redis; every other process reads that copy.
Implements PostHog's own ``FlagDefinitionCacheProvider`` protocol, which the
-SDK calls only from its poller thread — never from a flag read, so no
-evaluation can block on Redis.
+SDK calls from its poller thread and from the load that starts it: at startup
+where ``initialize_posthog_flags`` runs, otherwise on the first flag read.
"""
import json
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.py
index 365aa1b1e1..914eca2b9e 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
@@ -91,6 +91,15 @@ def redis():
return FakeRedis()
+def fetches(mocker):
+ """Every definitions fetch the SDK makes, with one canned answer."""
+ return mocker.patch(
+ "posthog.client.get",
+ return_value=GetResponse(data=DEFINITIONS, etag="e1"),
+ )
+
+
@pytest.fixture
def logs():
"""`caplog` captures nothing for `backend.*` under the app's logging config."""
@@ -428,14 +437,6 @@ class TestDefaultBackendIsUntouched:
class TestAgainstTheRealSDK:
"""The SDK's own poll, driven through the provider."""
- @pytest.fixture
- def fetches(self, mocker):
- """Every definitions fetch the SDK makes, with one canned answer."""
- return mocker.patch(
- "posthog.client.get",
- return_value=GetResponse(data=DEFINITIONS, etag="e1"),
- )
-
def client(self, provider_impl) -> Posthog:
return Posthog(
"phc_test",
@@ -518,6 +519,79 @@ class TestAgainstTheRealSDK:
fetches.assert_called_once()
+class TestStartupLoad:
+ """The SDK loads definitions on the first read unless startup has already."""
+
+ @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.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)
+ )
+ # A read reports itself to PostHog analytics, which would leave the machine.
+ mocker.patch.object(Posthog, "capture")
+ yield
+ ph.shutdown_posthog_flags()
+
+ @pytest.mark.asyncio
+ async def test_definitions_are_loaded_before_the_first_read(self, fetches):
+ ph.initialize_posthog_flags()
+ fetches.assert_called_once()
+
+ assert await ph.evaluate_flag("shared-flag", "u-1") == (True, True)
+ fetches.assert_called_once()
+
+ def test_a_follower_loads_the_shared_copy_without_fetching(self, redis, fetches):
+ provider(redis).should_fetch_flag_definitions() # someone else leads
+ store_definitions(redis)
+
+ ph.initialize_posthog_flags()
+
+ fetches.assert_not_called()
+ client = ph.get_flag_client()
+ assert client is not None
+ assert client.feature_flags == DEFINITIONS["flags"]
+
+ @pytest.mark.asyncio
+ async def test_a_failed_startup_fetch_leaves_the_first_read_to_retry(
+ self, mocker, fetches
+ ):
+ fetches.side_effect = [
+ ConnectionError("PostHog is unreachable"),
+ fetches.return_value,
+ ]
+ warn = mocker.patch.object(ph.logger, "warning")
+
+ ph.initialize_posthog_flags()
+
+ warn.assert_called_once()
+ assert await ph.evaluate_flag("shared-flag", "u-1") == (True, True)
+
+ @pytest.mark.asyncio
+ async def test_a_startup_load_that_raises_does_not_stop_startup(
+ self, mocker, fetches
+ ):
+ load = Posthog.load_feature_flags
+ failures = iter([RuntimeError("can't start new thread")])
+
+ def first_load_raises(client):
+ if error := next(failures, None):
+ raise error
+ load(client)
+
+ mocker.patch.object(Posthog, "load_feature_flags", first_load_raises)
+ warn = mocker.patch.object(ph.logger, "warning")
+
+ ph.initialize_posthog_flags()
+
+ warn.assert_called()
+ assert await ph.evaluate_flag("shared-flag", "u-1") == (True, True)
+
+
@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/posthog.py b/autogpt_platform/backend/backend/util/feature_flag/posthog.py
index 0b830977c9..e044a898fc 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/posthog.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/posthog.py
@@ -96,10 +96,24 @@ async def evaluate_flag(
def initialize_posthog_flags() -> None:
- """Build the flag client eagerly so its definition poller is warm."""
+ """Build the flag client and load its definitions before any read needs them."""
global _shut_down
_shut_down = False
- get_flag_client()
+ client = get_flag_client()
+ if client is None or not client.enable_local_evaluation:
+ return
+
+ # The SDK otherwise makes this same call on the first flag read, which then
+ # waits for it; going through it keeps the shared cache deciding who fetches.
+ try:
+ client.load_feature_flags()
+ except Exception as e:
+ logger.warning(f"Loading PostHog flag definitions at startup failed: {e}")
+ if client.feature_flags is None:
+ logger.warning(
+ "PostHog flag definitions did not load at startup; the first flag read "
+ "will retry"
+ )
def shutdown_posthog_flags() -> None:
diff --git a/autogpt_platform/backend/backend/util/process.py b/autogpt_platform/backend/backend/util/process.py
index 1c00249e5e..ba15818c94 100644
--- a/autogpt_platform/backend/backend/util/process.py
+++ b/autogpt_platform/backend/backend/util/process.py
@@ -4,16 +4,19 @@ import signal
import sys
from abc import ABC, abstractmethod
from multiprocessing import Process, get_all_start_methods, set_start_method
-from typing import Optional
+from typing import ClassVar, Optional
+from backend.util.feature_flag import initialize_feature_flags, shutdown_feature_flags
from backend.util.logging import configure_logging
from backend.util.metrics import sentry_init
from backend.util.retry import stop_retry_loops
from backend.util.secrets_guard import check_secrets
-from backend.util.settings import set_service_name
+from backend.util.settings import AppEnvironment, Config, set_service_name
logger = logging.getLogger(__name__)
+config = Config()
+
# How long ``AppProcess.stop`` waits for a child to act on a signal before
# escalating. Long enough for an orderly shutdown, short enough that a wedged
# child cannot hold the parent open indefinitely.
@@ -28,6 +31,9 @@ class AppProcess(ABC):
process: Optional[Process] = None
_shutting_down: bool = False
_cleaned_up: bool = False
+ # Opens the flag vendor's client before `run`, so no read waits on its
+ # first load. A service that never reads a flag opts out.
+ reads_feature_flags: ClassVar[bool] = True
if "forkserver" in get_all_start_methods():
set_start_method("forkserver", force=True)
@@ -61,6 +67,10 @@ class AppProcess(ABC):
def execute_run_command(self, silent):
signal.signal(signal.SIGTERM, self._self_terminate)
signal.signal(signal.SIGINT, self._self_terminate)
+ # LOCAL has no vendor configured; its flags resolve to mock defaults.
+ start_flags = (
+ self.reads_feature_flags and config.app_env is not AppEnvironment.LOCAL
+ )
try:
if silent:
@@ -69,6 +79,8 @@ class AppProcess(ABC):
set_service_name(self.service_name)
logger.info(f"[{self.service_name}] Starting...")
+ if start_flags:
+ initialize_feature_flags()
self.run()
except BaseException as e:
logger.warning(
@@ -84,11 +96,16 @@ class AppProcess(ABC):
except Exception:
pass # Silently ignore if Sentry isn't available
finally:
- if not self._cleaned_up:
- self._cleaned_up = True
- logger.info(f"[{self.service_name}] 🧹 Running cleanup")
- self.cleanup()
- logger.info(f"[{self.service_name}] ✅ Cleanup done")
+ try:
+ if not self._cleaned_up:
+ self._cleaned_up = True
+ logger.info(f"[{self.service_name}] 🧹 Running cleanup")
+ self.cleanup()
+ logger.info(f"[{self.service_name}] ✅ Cleanup done")
+ finally:
+ # After cleanup, which can drain work that still reads flags.
+ if start_flags:
+ shutdown_feature_flags()
logger.info(f"[{self.service_name}] 🛑 Terminated")
@staticmethod
diff --git a/autogpt_platform/backend/backend/util/process_test.py b/autogpt_platform/backend/backend/util/process_test.py
index 6f0b710d6f..c8959a7d3e 100644
--- a/autogpt_platform/backend/backend/util/process_test.py
+++ b/autogpt_platform/backend/backend/util/process_test.py
@@ -1,11 +1,15 @@
+import importlib
import logging
import multiprocessing
import signal
import threading
import time
+import pytest
+
from backend.util import process as process_module
from backend.util.process import STOP_TIMEOUT_SECONDS, AppProcess
+from backend.util.settings import AppEnvironment
def _ignore_sigterm_forever(ready):
@@ -121,3 +125,67 @@ def test_stop_is_a_noop_without_a_process():
assert service.process is None
service.stop()
assert service.process is None
+
+
+STARTS_FLAGS = ["initialize_flags", "run", "cleanup", "shutdown_flags"]
+NO_FLAGS = ["run", "cleanup"]
+
+
+ "service, lifecycle",
+ [
+ ("backend.api.rest_api.AgentServer", STARTS_FLAGS),
+ ("backend.copilot.executor.manager.CoPilotExecutor", STARTS_FLAGS),
+ ("backend.data.db_manager.DatabaseManager", STARTS_FLAGS),
+ ("backend.executor.manager.ExecutionManager", STARTS_FLAGS),
+ ("backend.executor.scheduler.Scheduler", STARTS_FLAGS),
+ ("backend.notifications.notifications.NotificationManager", STARTS_FLAGS),
+ ("backend.platform_linking.manager.PlatformLinkingManager", STARTS_FLAGS),
+ ("backend.api.ws_api.WebsocketServer", NO_FLAGS),
+ ("backend.executor.batch_executor.BatchExecutor", NO_FLAGS),
+ ("backend.copilot.bot.app.CoPilotChatBridge", NO_FLAGS),
+ ("backend.cli.main.MainApp", NO_FLAGS),
+ ],
+)
+def test_a_service_that_reads_flags_starts_them_before_it_runs(
+ mocker, service, lifecycle
+):
+ """Otherwise its first flag read waits for the vendor's first load."""
+ module, name = service.rsplit(".", 1)
+ instance = object.__new__(getattr(importlib.import_module(module), name))
+ order = _record_lifecycle(mocker, instance)
+ mocker.patch.object(process_module.config, "app_env", AppEnvironment.PRODUCTION)
+
+ instance.execute_run_command(silent=False)
+
+ assert order == lifecycle
+
+
+def test_local_starts_no_flags(mocker):
+ instance = _NoopProcess()
+ order = _record_lifecycle(mocker, instance)
+ mocker.patch.object(process_module.config, "app_env", AppEnvironment.LOCAL)
+
+ instance.execute_run_command(silent=False)
+
+ assert order == NO_FLAGS
+
+
+def _record_lifecycle(mocker, instance: AppProcess) -> list[str]:
+ order: list[str] = []
+
+ def record(step: str):
+ return lambda *args, **kwargs: order.append(step)
+
+ mocker.patch.object(
+ process_module, "initialize_feature_flags", record("initialize_flags")
+ )
+ mocker.patch.object(
+ process_module, "shutdown_feature_flags", record("shutdown_flags")
+ )
+ mocker.patch.object(instance, "run", record("run"))
+ mocker.patch.object(instance, "cleanup", record("cleanup"))
+ # Keeps the test runner's own signal handlers and service name.
+ mocker.patch.object(process_module.signal, "signal")
+ mocker.patch.object(process_module, "set_service_name")
+ return order