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): skip behavioral validation for static cohorts (#65961)

tsushanth431ec6bef268
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.py
index c589e81e6cf8..157f33e1bf95 100644
--- a/products/feature_flags/backend/api/feature_flag.py
+++ b/products/feature_flags/backend/api/feature_flag.py
@@ -1341,7 +1341,20 @@ class FeatureFlagSerializer(
initial_cohort: Cohort = Cohort.objects.get(
pk=cast(str | int, prop.value), team__project_id=self.context["project_id"]
)
- dependency_cohorts = get_all_cohort_dependencies(initial_cohort, stop_traversal_at_static=True)
+ # Static cohorts (including one-time snapshots) hold a
+ # materialised person list. The populating criteria may
+ # still be stored on the record, but they are inert – the
+ # cohort no longer re-evaluates them, and the Rust engine's
+ # extract_dependencies returns an empty set for them. Skip
+ # both the behavioural property check and the dependency walk
+ # so snapshot cohorts can be used in flags without an extra
+ # export step, even when their inert criteria reference
+ # another cohort. See #65270.
+ dependency_cohorts = (
+ []
+ if initial_cohort.is_static
+ else get_all_cohort_dependencies(initial_cohort, stop_traversal_at_static=True)
+ )
for cohort in [initial_cohort, *dependency_cohorts]:
# Static cohorts have materialized membership, any preserved behavioral
# filters are display-only and never evaluated, so skip them.
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index 5e6a2366aa21..0e0d8f226f89 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -5396,6 +5396,51 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
if expected_detail_fragment is not None:
self.assertIn(expected_detail_fragment, response.json()["detail"])
+ def test_snapshot_cohort_referencing_behavioral_cohort_is_allowed_in_flag(self):
+ # A snapshot cohort is is_static=True but retains its populating criteria,
+ # which can reference another (behavioral) cohort. The dependency walk must
+ # be skipped for static cohorts so the referenced behavioral cohort doesn't
+ # block flag creation — matching the Rust engine, whose extract_dependencies
+ # returns an empty set for static cohorts.
+ behavioral_cohort = Cohort.objects.create(
+ team=self.team,
+ name="behavioral-dep",
+ filters={
+ "properties": {
+ "type": "AND",
+ "values": [
+ {
+ "key": "$pageview",
+ "event_type": "events",
+ "time_value": 2,
+ "time_interval": "week",
+ "value": "performed_event_first_time",
+ "type": "behavioral",
+ },
+ ],
+ }
+ },
+ )
+ snapshot_cohort = Cohort.objects.create(
+ team=self.team,
+ name="snapshot",
+ is_static=True,
+ filters={
+ "properties": {
+ "type": "AND",
+ "values": [
+ {"key": "id", "type": "cohort", "value": behavioral_cohort.id},
+ ],
+ }
+ },
+ )
+
+ self._create_flag_with_properties(
+ "snapshot-cohort-flag",
+ [{"key": "id", "type": "cohort", "value": snapshot_cohort.id}],
+ expected_status=status.HTTP_201_CREATED,
+ )
+
def test_validation_group_properties(self):
groups_request = self._create_flag_with_properties(
"groups-flag",