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.

chore(flags): drop has_encrypted_payloads from cached flag responses (#57892)

Phil Haack10742bf85fae
diff --git a/posthog/api/feature_flag.py b/posthog/api/feature_flag.py
index e2ef3684dc63..e85f79338ca3 100644
--- a/posthog/api/feature_flag.py
+++ b/posthog/api/feature_flag.py
@@ -2233,7 +2233,6 @@ class MinimalFeatureFlagSerializer(serializers.ModelSerializer):
"deleted",
"active",
"ensure_experience_continuity",
- "has_encrypted_payloads",
"version",
"evaluation_runtime",
"bucketing_identifier",
diff --git a/posthog/models/feature_flag/feature_flag.py b/posthog/models/feature_flag/feature_flag.py
index 5003f447702c..1b3b706877be 100644
--- a/posthog/models/feature_flag/feature_flag.py
+++ b/posthog/models/feature_flag/feature_flag.py
@@ -576,7 +576,7 @@ class FeatureFlagOverride(models.Model):
def get_feature_flags(
team: Optional["Team"] = None,
project_id: Optional[int] = None,
- exclude_encrypted_remote_config: bool = False,
+ exclude_encrypted_payloads: bool = False,
) -> list[FeatureFlag]:
"""
Fetch FeatureFlag objects for a team or project.
@@ -587,9 +587,11 @@ def get_feature_flags(
Args:
team: Team to get flags for (mutually exclusive with project_id)
project_id: Project ID to get flags for (mutually exclusive with team)
- exclude_encrypted_remote_config: If True, exclude flags where both
- is_remote_configuration=True AND has_encrypted_payloads=True.
- These flags can only be accessed via the /remote_config endpoint.
+ exclude_encrypted_payloads: If True, exclude flags with
+ has_encrypted_payloads=True. These flags can only be accessed
+ via the /remote_config endpoint, which handles decryption.
+ The model invariant guarantees has_encrypted_payloads implies
+ is_remote_configuration, so this filter covers all encrypted flags.
Returns:
List of FeatureFlag model instances with evaluation tags pre-loaded
@@ -610,9 +612,10 @@ def get_feature_flags(
# avoiding N+1 queries when serializing many flags.
qs = FeatureFlag.objects.filter(**filter_kwargs)
- # Exclude encrypted remote config flags at the database level if requested
- if exclude_encrypted_remote_config:
- qs = qs.filter(~Q(is_remote_configuration=True, has_encrypted_payloads=True))
+ # Use .exclude() (not .filter(=False)) so legacy rows with NULL
+ # has_encrypted_payloads remain included, matching prior behavior.
+ if exclude_encrypted_payloads:
+ qs = qs.exclude(has_encrypted_payloads=True)
qs = qs.annotate(
evaluation_tag_names_agg=ArrayAgg(
diff --git a/posthog/models/feature_flag/flags_cache.py b/posthog/models/feature_flag/flags_cache.py
index 1392607fe848..6e1a9d89358f 100644
--- a/posthog/models/feature_flag/flags_cache.py
+++ b/posthog/models/feature_flag/flags_cache.py
@@ -332,8 +332,7 @@ def _get_feature_flags_for_service(team: Team) -> dict[str, Any]:
dependency metadata (stages, missing deps, transitive deps), and cohorts contains
serialized cohort definitions referenced by the flags (including transitive deps).
"""
- # Exclude encrypted remote config flags at DB level for efficiency
- flags = get_feature_flags(team=team, exclude_encrypted_remote_config=True)
+ flags = get_feature_flags(team=team, exclude_encrypted_payloads=True)
flags_data = serialize_feature_flags(flags)
evaluation_metadata = _compute_flag_dependencies(flags_data)
@@ -374,16 +373,15 @@ def _get_feature_flags_for_teams_batch(teams: list[Team]) -> dict[int, dict[str,
# Load all flags for all teams in one query with evaluation tags pre-loaded.
# Include disabled flags (active=False) so flag dependencies can reference them
# and evaluate them as false, rather than raising DependencyNotFound errors.
- # Exclude encrypted remote config flags - they can only be accessed via the
+ # Exclude encrypted payload flags - they can only be accessed via the
# dedicated /remote_config endpoint which handles decryption.
# Note: We intentionally don't select_related("team") here because we only need
# team_id (already on the model) for grouping, and the Team objects are already
# loaded by the caller. Avoiding the join saves memory.
all_flags = list(
- FeatureFlag.objects.filter(
- ~Q(is_remote_configuration=True, has_encrypted_payloads=True),
- team__in=teams,
- ).annotate(
+ FeatureFlag.objects.filter(team__in=teams)
+ .exclude(has_encrypted_payloads=True)
+ .annotate(
evaluation_tag_names_agg=ArrayAgg(
"flag_evaluation_contexts__evaluation_context__name",
filter=Q(flag_evaluation_contexts__isnull=False),
diff --git a/posthog/models/feature_flag/local_evaluation.py b/posthog/models/feature_flag/local_evaluation.py
index f87848296e84..ce506315e7e2 100644
--- a/posthog/models/feature_flag/local_evaluation.py
+++ b/posthog/models/feature_flag/local_evaluation.py
@@ -529,10 +529,8 @@ def _get_flags_response_for_local_evaluation_batch(
# serialize — one DB round trip instead of two.
all_flags = list(
FeatureFlag.objects.db_manager(DATABASE_FOR_LOCAL_EVALUATION)
- .filter(
- ~Q(is_remote_configuration=True, has_encrypted_payloads=True),
- team_id__in=team_ids,
- )
+ .filter(team_id__in=team_ids)
+ .exclude(has_encrypted_payloads=True)
.exclude(id__in=survey_flag_ids)
.annotate(
evaluation_tag_names_agg=ArrayAgg(
diff --git a/posthog/models/feature_flag/test/test_flags_cache.py b/posthog/models/feature_flag/test/test_flags_cache.py
index 1522ca268557..4bb72af1d771 100644
--- a/posthog/models/feature_flag/test/test_flags_cache.py
+++ b/posthog/models/feature_flag/test/test_flags_cache.py
@@ -288,11 +288,11 @@ class TestServiceFlagsCache(BaseTest):
[
# (is_remote_config, has_encrypted, should_include, description)
(False, False, True, "regular_flag"),
- (False, True, True, "encrypted_but_not_remote_config"),
+ (False, True, False, "encrypted_but_not_remote_config"),
(True, False, True, "unencrypted_remote_config"),
(True, True, False, "encrypted_remote_config"),
(None, False, True, "null_remote_config_unencrypted"),
- (None, True, True, "null_remote_config_encrypted"),
+ (None, True, False, "null_remote_config_encrypted"),
(False, None, True, "regular_flag_null_encrypted"),
(True, None, True, "remote_config_null_encrypted"),
(None, None, True, "legacy_flag_both_null"),
@@ -301,8 +301,11 @@ class TestServiceFlagsCache(BaseTest):
def test_filtering_matrix_for_service(self, is_remote_config, has_encrypted, should_include, desc):
"""Test filtering behavior for all combinations of is_remote_configuration and has_encrypted_payloads.
- This parameterized test covers all 9 combinations including NULL values to ensure
- legacy flags (created before these fields existed) are handled correctly.
+ Any flag with has_encrypted_payloads=True is excluded — these can only be
+ accessed via /remote_config. The model invariant (clean() + serializer
+ validation) guarantees True implies is_remote_configuration=True, but the
+ filter is intentionally strict to defend against invariant violations.
+ NULL has_encrypted_payloads is preserved (legacy flags pre-dating the field).
"""
FeatureFlag.objects.create(
team=self.team,
@@ -325,22 +328,18 @@ class TestServiceFlagsCache(BaseTest):
[
# (is_remote_config, has_encrypted, should_include, description)
(False, False, True, "regular_flag"),
- (False, True, True, "encrypted_but_not_remote_config"),
+ (False, True, False, "encrypted_but_not_remote_config"),
(True, False, True, "unencrypted_remote_config"),
(True, True, False, "encrypted_remote_config"),
(None, False, True, "null_remote_config_unencrypted"),
- (None, True, True, "null_remote_config_encrypted"),
+ (None, True, False, "null_remote_config_encrypted"),
(False, None, True, "regular_flag_null_encrypted"),
(True, None, True, "remote_config_null_encrypted"),
(None, None, True, "legacy_flag_both_null"),
]
)
def test_filtering_matrix_for_teams_batch(self, is_remote_config, has_encrypted, should_include, desc):
- """Test batch function filtering for all combinations of is_remote_configuration and has_encrypted_payloads.
-
- This parameterized test covers all 9 combinations including NULL values to ensure
- legacy flags (created before these fields existed) are handled correctly in batch loading.
- """
+ """Mirrors test_filtering_matrix_for_service for the batch loader path."""
FeatureFlag.objects.create(
team=self.team,
key=f"flag-{desc}",
diff --git a/posthog/models/feature_flag/test/test_local_evaluation.py b/posthog/models/feature_flag/test/test_local_evaluation.py
index 7bf77531b1fc..ec049044a2a1 100644
--- a/posthog/models/feature_flag/test/test_local_evaluation.py
+++ b/posthog/models/feature_flag/test/test_local_evaluation.py
@@ -662,6 +662,47 @@ class TestLocalEvaluationBatch(BaseTest):
result = _get_flags_response_for_local_evaluation_batch([], True)
assert result == {}
+ @parameterized.expand(
+ [
+ # (is_remote_config, has_encrypted, should_include, description)
+ (False, False, True, "regular_flag"),
+ (False, True, False, "encrypted_but_not_remote_config"),
+ (True, False, True, "unencrypted_remote_config"),
+ (True, True, False, "encrypted_remote_config"),
+ (None, False, True, "null_remote_config_unencrypted"),
+ (None, True, False, "null_remote_config_encrypted"),
+ (False, None, True, "regular_flag_null_encrypted"),
+ (True, None, True, "remote_config_null_encrypted"),
+ (None, None, True, "legacy_flag_both_null"),
+ ]
+ )
+ def test_batch_filtering_matrix_for_encrypted_payloads(self, is_remote_config, has_encrypted, should_include, desc):
+ """Mirrors test_filtering_matrix_for_teams_batch in test_flags_cache.py for the local evaluation batch path.
+
+ Any flag with has_encrypted_payloads=True is excluded — these can only be
+ accessed via /remote_config. The model invariant (clean() + serializer
+ validation) guarantees True implies is_remote_configuration=True, but the
+ filter is intentionally strict to defend against invariant violations.
+ NULL has_encrypted_payloads is preserved (legacy flags pre-dating the field).
+ """
+ team = self._create_team_with_project(f"Team {desc}")
+ FeatureFlag.objects.create(
+ team=team,
+ key=f"flag-{desc}",
+ created_by=self.user,
+ is_remote_configuration=is_remote_config,
+ has_encrypted_payloads=has_encrypted,
+ filters={"groups": [{"properties": [], "rollout_percentage": 100}]},
+ )
+
+ results = _get_flags_response_for_local_evaluation_batch([team], True)
+ flag_keys = {f["key"] for f in results[team.id]["flags"]}
+
+ if should_include:
+ assert f"flag-{desc}" in flag_keys, f"Expected flag-{desc} to be included"
+ else:
+ assert f"flag-{desc}" not in flag_keys, f"Expected flag-{desc} to be excluded"
+
def test_batch_two_teams_flags_isolated(self):
team_a = self._create_team_with_project("Team A")
team_b = self._create_team_with_project("Team B")
diff --git a/products/early_access_features/frontend/generated/api.schemas.ts b/products/early_access_features/frontend/generated/api.schemas.ts
index e67f63670198..1144eeccccb0 100644
--- a/products/early_access_features/frontend/generated/api.schemas.ts
+++ b/products/early_access_features/frontend/generated/api.schemas.ts
@@ -54,8 +54,6 @@ export interface MinimalFeatureFlagApi {
active?: boolean
/** @nullable */
ensure_experience_continuity?: boolean | null
- /** @nullable */
- has_encrypted_payloads?: boolean | null
/**
* @minimum -2147483648
* @maximum 2147483647
diff --git a/products/feature_flags/frontend/generated/api.schemas.ts b/products/feature_flags/frontend/generated/api.schemas.ts
index b8c67e8e4554..9f6984ce9085 100644
--- a/products/feature_flags/frontend/generated/api.schemas.ts
+++ b/products/feature_flags/frontend/generated/api.schemas.ts
@@ -981,8 +981,6 @@ export interface MinimalFeatureFlagApi {
active?: boolean
/** @nullable */
ensure_experience_continuity?: boolean | null
- /** @nullable */
- has_encrypted_payloads?: boolean | null
/**
* @minimum -2147483648
* @maximum 2147483647
diff --git a/products/product_tours/frontend/generated/api.schemas.ts b/products/product_tours/frontend/generated/api.schemas.ts
index 6cfb1ac11cf9..739f7e2c7e98 100644
--- a/products/product_tours/frontend/generated/api.schemas.ts
+++ b/products/product_tours/frontend/generated/api.schemas.ts
@@ -54,8 +54,6 @@ export interface MinimalFeatureFlagApi {
active?: boolean
/** @nullable */
ensure_experience_continuity?: boolean | null
- /** @nullable */
- has_encrypted_payloads?: boolean | null
/**
* @minimum -2147483648
* @maximum 2147483647
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 205e889caceb..9ae82c0d2402 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -2332,7 +2332,6 @@ class TestSurvey(APIBaseTest):
"deleted": False,
"active": False,
"ensure_experience_continuity": False,
- "has_encrypted_payloads": False,
"version": ANY, # Add version field with ANY matcher
"evaluation_runtime": "all",
"evaluation_contexts": [],
diff --git a/products/surveys/frontend/generated/api.schemas.ts b/products/surveys/frontend/generated/api.schemas.ts
index 68042922061e..8af65ee1b4e7 100644
--- a/products/surveys/frontend/generated/api.schemas.ts
+++ b/products/surveys/frontend/generated/api.schemas.ts
@@ -69,8 +69,6 @@ export interface MinimalFeatureFlagApi {
active?: boolean
/** @nullable */
ensure_experience_continuity?: boolean | null
- /** @nullable */
- has_encrypted_payloads?: boolean | null
/**
* @minimum -2147483648
* @maximum 2147483647
diff --git a/rust/feature-flags/src/flags/flag_models.rs b/rust/feature-flags/src/flags/flag_models.rs
index c78245dd0e9c..9e60e9804fc7 100644
--- a/rust/feature-flags/src/flags/flag_models.rs
+++ b/rust/feature-flags/src/flags/flag_models.rs
@@ -216,8 +216,8 @@ pub enum BucketingIdentifier {
/// follow the expand-and-contract pattern. Golden fixture contract test:
/// cargo test -p feature-flags test_hypercache_contract
///
-/// Note: Python also emits `has_encrypted_payloads`, which Rust intentionally
-/// ignores (serde drops unknown fields).
+/// Encrypted-payload flags are excluded at the Python query layer, so this struct
+/// has no field for `has_encrypted_payloads`.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct FeatureFlag {
pub id: FeatureFlagId,
diff --git a/rust/feature-flags/tests/fixtures/hypercache_contract.json b/rust/feature-flags/tests/fixtures/hypercache_contract.json
index 9da39a017bda..fd436cbd45e9 100644
--- a/rust/feature-flags/tests/fixtures/hypercache_contract.json
+++ b/rust/feature-flags/tests/fixtures/hypercache_contract.json
@@ -51,7 +51,6 @@
"deleted": false,
"active": true,
"ensure_experience_continuity": true,
- "has_encrypted_payloads": false,
"version": 3,
"evaluation_runtime": "all",
"bucketing_identifier": "device_id",
@@ -76,7 +75,6 @@
"deleted": false,
"active": true,
"ensure_experience_continuity": false,
- "has_encrypted_payloads": false,
"version": 1,
"evaluation_runtime": "all",
"bucketing_identifier": null,
@@ -108,7 +106,6 @@
"deleted": false,
"active": true,
"ensure_experience_continuity": false,
- "has_encrypted_payloads": false,
"version": 1,
"evaluation_runtime": "all",
"bucketing_identifier": null,
@@ -141,7 +138,6 @@
"deleted": false,
"active": true,
"ensure_experience_continuity": false,
- "has_encrypted_payloads": false,
"version": 2,
"evaluation_runtime": "all",
"bucketing_identifier": null,
@@ -174,7 +170,6 @@
"deleted": false,
"active": true,
"ensure_experience_continuity": false,
- "has_encrypted_payloads": false,
"version": 1,
"evaluation_runtime": "all",
"bucketing_identifier": null,
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 63faf7cfe762..3019aa9fff10 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -15441,8 +15441,6 @@ export namespace Schemas {
active?: boolean;
/** @nullable */
ensure_experience_continuity?: boolean | null;
- /** @nullable */
- has_encrypted_payloads?: boolean | null;
/**
* @minimum -2147483648
* @maximum 2147483647