Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(flags): drop disabled and server-only flags from the bootstrap (#120095)

Paul D'Ambrad4d0ae2bb865
diff --git a/posthog/test/test_get_context_for_template.py b/posthog/test/test_get_context_for_template.py
index bd2571b89721..ddf06f0f87db 100644
--- a/posthog/test/test_get_context_for_template.py
+++ b/posthog/test/test_get_context_for_template.py
@@ -99,14 +99,18 @@ STABLE_CHUNKS = StableChunks(
class TestFlagsForAppEvaluationContext(SimpleTestCase):
@parameterized.expand(
[
- ("no_contexts_is_kept", None, True),
- ("empty_contexts_is_kept", [], True),
- ("app_context_is_kept", ["posthog-web-app", "posthog-website"], True),
- ("other_context_is_dropped", ["posthog-website"], False),
+ ("no_contexts_is_kept", {"evaluation_contexts": None}, True),
+ ("empty_contexts_is_kept", {"evaluation_contexts": []}, True),
+ ("app_context_is_kept", {"evaluation_contexts": ["posthog-web-app", "posthog-website"]}, True),
+ ("other_context_is_dropped", {"evaluation_contexts": ["posthog-website"]}, False),
+ ("active_is_kept", {"active": True}, True),
+ ("disabled_is_dropped", {"active": False}, False),
+ ("client_runtime_is_kept", {"evaluation_runtime": "client"}, True),
+ ("server_runtime_is_dropped", {"evaluation_runtime": "server"}, False),
]
)
- def test_keeps_flags_the_app_evaluates(self, _name, evaluation_contexts, kept):
- definitions = [{"key": "the-flag", "evaluation_contexts": evaluation_contexts}]
+ def test_keeps_flags_the_app_evaluates(self, _name, definition, kept):
+ definitions = [{"key": "the-flag", **definition}]
actual = flags_for_app_evaluation_context({"the-flag": True, "untagged": True}, definitions)
diff --git a/posthog/utils.py b/posthog/utils.py
index dc3b6744ef25..985717352689 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -494,19 +494,25 @@ def flags_for_app_evaluation_context(
) -> dict[str, Any] | None:
"""
Drops flags that /flags leaves out for the app, which sends `evaluation_contexts: ['posthog-web-app']`
- from `loadPostHogJS.tsx`. The Python SDK evaluates locally without applying evaluation contexts.
+ from `loadPostHogJS.tsx`. The Python SDK evaluates locally without applying evaluation contexts, and it
+ also returns disabled flags and server-only flags, which /flags never sends to a browser.
"""
# The SDK returns no flags at all when analytics is disabled, for example in tests or with OPT_OUT_CAPTURE.
if flags is None:
return None
- excluded = {
- definition["key"]
- for definition in definitions or []
- if definition.get("evaluation_contexts") and APP_EVALUATION_CONTEXT not in definition["evaluation_contexts"]
- }
+ excluded = {definition["key"] for definition in definitions or [] if not _app_evaluates_flag(definition)}
return {key: value for key, value in flags.items() if key not in excluded}
+def _app_evaluates_flag(definition: dict[str, Any]) -> bool:
+ contexts = definition.get("evaluation_contexts")
+ return (
+ definition.get("active", True) is not False
+ and definition.get("evaluation_runtime") != "server"
+ and (not contexts or APP_EVALUATION_CONTEXT in contexts)
+ )
+
+
@frozen
class _FlagIdentity:
person_properties: dict[str, Any]