: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): evaluate the realtime cohort targeting gate per project (#99887)
Matheus Batista95ea659218e1
diff --git a/posthog/api/cohort.py b/posthog/api/cohort.pyindex a9604b11d011..e0ac0a1ed467 100644--- a/posthog/api/cohort.py+++ b/posthog/api/cohort.py@@ -1743,7 +1743,7 @@ class CohortViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsets.ModelVi# Avoid circular import: feature_flag imports cohort modelsfrom products.feature_flags.backend.api.feature_flag import _is_realtime_cohort_flag_targeting_enabled- allow_realtime_backfilled = _is_realtime_cohort_flag_targeting_enabled(self.request)+ allow_realtime_backfilled = _is_realtime_cohort_flag_targeting_enabled(self.request, team=self.team)# The flag's cohort typeahead hits this endpoint on every keystroke, so the# behavioral set is computed once per team and cached (invalidated on cohort# writes); see get_flag_excluded_behavioral_cohort_ids.diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.pyindex b0429e4c4da0..63f46ddb48eb 100644--- a/products/feature_flags/backend/api/feature_flag.py+++ b/products/feature_flags/backend/api/feature_flag.py@@ -394,17 +394,18 @@ def assert_feature_flag_write_scope()-def _is_realtime_cohort_flag_targeting_enabled(request) -> bool:+def _is_realtime_cohort_flag_targeting_enabled(request, *, team: Team) -> bool:"""Check whether the realtime cohort flag targeting feature is enabled for this request."""try:user = getattr(request, "user", None)if user is None or user.is_anonymous:return False+ organization_id = str(team.organization_id)return feature_enabled_or_false(REALTIME_COHORT_FLAG_TARGETING_FLAG,user.distinct_id,- groups={"organization": str(user.organization.id)},- group_properties={"organization": {"id": str(user.organization.id)}},+ groups={"organization": organization_id, "project": str(team.uuid)},+ group_properties={"organization": {"id": organization_id}, "project": {"id": team.id}},only_evaluate_locally=False,send_feature_flag_events=False,)@@ -1582,7 +1583,9 @@ class FeatureFlagSerializer(This avoids a potentially expensive feature_enabled() call for flags that don'treference any cohort properties."""- return _is_realtime_cohort_flag_targeting_enabled(self.context["request"])+ get_team = self.context.get("get_team")+ team = get_team() if get_team else Team.objects.get(pk=self.context["team_id"])+ return _is_realtime_cohort_flag_targeting_enabled(self.context["request"], team=team)def validate_filters(self, filters):# Metrics wrapper: one increment per rejected write. `rejected` means a switch-gateddiff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.pyindex 13729df4235f..7fe572627963 100644--- a/products/feature_flags/backend/api/test/test_feature_flag.py+++ b/products/feature_flags/backend/api/test/test_feature_flag.py@@ -62,6 +62,7 @@ from products.experiments.backend.models.experiment import Experimentfrom products.feature_flags.backend.api.feature_flag import (FLAG_FILTERS_VIOLATION_COUNTER,FLAG_FILTERS_WRITE_COUNTER,+ REALTIME_COHORT_FLAG_TARGETING_FLAG,FeatureFlagSerializer,FeatureFlagStatusResponseSerializer,_flag_write_source,@@ -6204,7 +6205,16 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):expected_detail_fragment,mock_feature_enabled,):- mock_feature_enabled.return_value = flag_enabled+ def gate_enabled_for_request_project(key, _distinct_id, *, groups, group_properties, **_kwargs):+ if key != REALTIME_COHORT_FLAG_TARGETING_FLAG:+ return flag_enabled+ return (+ flag_enabled+ and groups["project"] == str(self.team.uuid)+ and group_properties["project"]["id"] == self.team.id+ )++ mock_feature_enabled.side_effect = gate_enabled_for_request_projectcohort_kwargs: dict[str, Any] = {"team": self.team,