: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(flags): handle flag dependencies in user blast radius (#63351)
artyaz5cce2b73db31
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.pyindex c2b286284d2a..eeb4774224c5 100644--- a/posthog/hogql/property.py+++ b/posthog/hogql/property.py@@ -807,7 +807,11 @@ def property_to_expr(else:raise QueryError(f"property_to_expr with property of type {type(property).__name__} not implemented")- if property.type == "hogql":+ if property.type == "flag":+ # Flag dependencies are evaluated at flag-matching time and can't be expressed+ # in HogQL — return a neutral filter, mirroring the FlagPropertyFilter handling above.+ return ast.Constant(value=1)+ elif property.type == "hogql":tag_contains_user_hogql()return parse_expr(property.key, cache_origin=CacheOrigin.USER)elif property.type == "event_metadata" and scope == "group" and GROUP_KEY_PATTERN.match(property.key) is not None:diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.pyindex 95bb4e9fd64b..6755a02bdd2b 100644--- a/posthog/hogql/test/test_property.py+++ b/posthog/hogql/test/test_property.py@@ -1749,6 +1749,30 @@ class TestProperty(BaseTest):result = property_to_expr([prop], self.team)self.assertEqual(result, ast.Constant(value=1))+ # Flag dependency conditions also arrive as plain dicts (e.g. via the user blast radius API),+ # taking the legacy Property path instead of FlagPropertyFilter+ @parameterized.expand([("enabled", True), ("disabled", False), ("variant", "control")])+ def test_flag_dependency_dict_produces_neutral_expr(self, _name: str, value: Any):+ result = self._property_to_expr({"type": "flag", "key": "123", "value": value, "operator": "flag_evaluates_to"})+ self.assertEqual(result, ast.Constant(value=1))++ def test_flag_dependency_combined_with_person_property(self):+ flag_prop: dict[str, Any] = {"type": "flag", "key": "123", "value": False, "operator": "flag_evaluates_to"}+ result = self._property_to_expr(+ PropertyGroup(+ type=PropertyOperatorType.AND,+ values=[+ Property(**flag_prop),+ Property(type="person", key="email", value="[email protected]", operator="exact"),+ ],+ ),+ scope="person",+ )+ self.assertIsInstance(result, ast.And)+ self.assertEqual(cast(ast.And, result).exprs[0], ast.Constant(value=1))+ # The person filter must survive as a real comparison, not collapse to neutral too+ self.assertIsInstance(cast(ast.And, result).exprs[1], ast.CompareOperation)+class TestPropertyIsSetIsNotSetWithData(APIBaseTest):# Sentinel to indicate a property should not be included in the eventdiff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.pyindex bff009ca539f..5e6a2366aa21 100644--- a/products/feature_flags/backend/api/test/test_feature_flag.py+++ b/products/feature_flags/backend/api/test/test_feature_flag.py@@ -61,6 +61,7 @@ from products.feature_flags.backend.models.feature_flag import (FeatureFlagDashboards,get_feature_flags_for_team_in_cache,)+from products.feature_flags.backend.user_blast_radius import get_user_blast_radius_personsfrom products.product_analytics.backend.models.insight import Insightfrom products.product_tours.backend.models import ProductTourfrom products.surveys.backend.models import Survey@@ -8431,6 +8432,187 @@ class TestBlastRadius(ClickhouseTestMixin, APIBaseTest):response_json = response.json()self.assertLessEqual({"affected": 4, "total": 10}.items(), response_json.items())+ def test_user_blast_radius_with_flag_dependency(self):+ for i in range(10):+ _create_person(+ team_id=self.team.pk,+ distinct_ids=[f"person{i}"],+ properties={"group": f"{i}"},+ )++ dependency_flag = FeatureFlag.objects.create(+ team=self.team,+ key="dependency-flag",+ created_by=self.user,+ filters={"groups": [{"properties": [], "rollout_percentage": 100}]},+ )++ # Flag dependencies can't be evaluated in HogQL, so they are neutral for the estimate+ response = self.client.post(+ f"/api/projects/{self.team.id}/feature_flags/user_blast_radius",+ {+ "condition": {+ "properties": [+ {+ "key": str(dependency_flag.pk),+ "type": "flag",+ "value": False,+ "operator": "flag_evaluates_to",+ }+ ],+ "rollout_percentage": 100,+ }+ },+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertLessEqual({"affected": 10, "total": 10}.items(), response.json().items())++ def test_user_blast_radius_with_flag_dependency_and_person_property(self):+ for i in range(10):+ _create_person(+ team_id=self.team.pk,+ distinct_ids=[f"person{i}"],+ properties={"group": f"{i}"},+ )++ dependency_flag = FeatureFlag.objects.create(+ team=self.team,+ key="dependency-flag",+ created_by=self.user,+ filters={"groups": [{"properties": [], "rollout_percentage": 100}]},+ )++ # The flag dependency is ignored, but the person property filter still applies+ response = self.client.post(+ f"/api/projects/{self.team.id}/feature_flags/user_blast_radius",+ {+ "condition": {+ "properties": [+ {+ "key": str(dependency_flag.pk),+ "type": "flag",+ "value": True,+ "operator": "flag_evaluates_to",+ },+ {+ "key": "group",+ "type": "person",+ "value": [0, 1, 2, 3],+ "operator": "exact",+ },+ ],+ "rollout_percentage": 100,+ }+ },+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertLessEqual({"affected": 4, "total": 10}.items(), response.json().items())++ def test_user_blast_radius_with_groups_and_flag_dependency(self):+ create_group_type_mapping_without_created_at(+ team=self.team,+ project_id=self.team.project_id,+ group_type="organization",+ group_type_index=0,+ )++ for i in range(10):+ create_group(+ team_id=self.team.pk,+ group_type_index=0,+ group_key=f"org:{i}",+ properties={"industry": f"{i}"},+ )++ dependency_flag = FeatureFlag.objects.create(+ team=self.team,+ key="dependency-flag",+ created_by=self.user,+ filters={"groups": [{"properties": [], "rollout_percentage": 100}]},+ )++ # The flag dependency is neutral for group-scoped blast radius too: it must not raise+ # on the missing group_type_index, and the group property filter still applies+ response = self.client.post(+ f"/api/projects/{self.team.id}/feature_flags/user_blast_radius",+ {+ "condition": {+ "properties": [+ {+ "key": str(dependency_flag.pk),+ "type": "flag",+ "value": True,+ "operator": "flag_evaluates_to",+ },+ {+ "key": "industry",+ "type": "group",+ "value": [0, 1, 2, 3],+ "operator": "exact",+ "group_type_index": 0,+ },+ ],+ "rollout_percentage": 25,+ },+ "group_type_index": 0,+ },+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertLessEqual({"affected": 4, "total": 10}.items(), response.json().items())++ def test_user_blast_radius_persons_with_groups_and_flag_dependency(self):+ create_group_type_mapping_without_created_at(+ team=self.team,+ project_id=self.team.project_id,+ group_type="organization",+ group_type_index=0,+ )++ for i in range(10):+ create_group(+ team_id=self.team.pk,+ group_type_index=0,+ group_key=f"org:{i}",+ properties={"industry": f"{i}"},+ )++ dependency_flag = FeatureFlag.objects.create(+ team=self.team,+ key="dependency-flag",+ created_by=self.user,+ filters={"groups": [{"properties": [], "rollout_percentage": 100}]},+ )++ # The persons path runs the same flag-dependency skip as the count path: a group-scoped flag+ # dependency must not raise on the missing group_type_index, and the group filter still applies.+ affected = get_user_blast_radius_persons(+ self.team,+ {+ "properties": [+ {+ "key": str(dependency_flag.pk),+ "type": "flag",+ "value": True,+ "operator": "flag_evaluates_to",+ },+ {+ "key": "industry",+ "type": "group",+ "value": [0, 1, 2, 3],+ "operator": "exact",+ "group_type_index": 0,+ },+ ],+ "rollout_percentage": 25,+ },+ group_type_index=0,+ )++ self.assertEqual(set(affected), {"org:0", "org:1", "org:2", "org:3"})+@freeze_time("2024-01-11")def test_user_blast_radius_with_relative_date_filters(self):for i in range(8):diff --git a/products/feature_flags/backend/user_blast_radius.py b/products/feature_flags/backend/user_blast_radius.pyindex 6a1e7d65b8dd..c319683e1e46 100644--- a/products/feature_flags/backend/user_blast_radius.py+++ b/products/feature_flags/backend/user_blast_radius.py@@ -167,6 +167,10 @@ def _get_group_blast_radius(team: Team, filter: Filter, group_type_index: GroupT# Validate all group properties have correct group_type_indexfor property in properties:+ if property.type == "flag":+ # Flag dependencies are evaluated at flag-matching time, not in the group query,+ # and carry no group_type_index — skip validation and let property_to_expr neutralize them.+ continueif property.key == "$group_key":# Special case: $group_key doesn't need a group_type_index as it refers to the key itselfproperty.group_type_index = group_type_index@@ -441,6 +445,10 @@ def _get_group_blast_radius_persons(# Validate all group properties have correct group_type_indexfor property in properties:+ if property.type == "flag":+ # Flag dependencies are evaluated at flag-matching time, not in the group query,+ # and carry no group_type_index — skip validation and let property_to_expr neutralize them.+ continueif property.key == "$group_key":property.group_type_index = group_type_indexelif property.group_type_index is None or property.group_type_index != group_type_index: