: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): match replay usage by JSONB containment instead of integer cast (#73902)
jake sciottoe41bacf249e8
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.pyindex 87d572bd3ec5..65fb215d4042 100644--- a/products/feature_flags/backend/api/feature_flag.py+++ b/products/feature_flags/backend/api/feature_flag.py@@ -14,6 +14,7 @@ from django.conf import settingsfrom django.contrib.postgres.aggregates import ArrayAggfrom django.db import IntegrityError, transactionfrom django.db.models import Count, Prefetch, Q, QuerySet, deletion+from django.db.models.functions import JSONObjectimport grpcimport requests@@ -2896,19 +2897,15 @@ class FeatureFlagViewSet())- # Annotate with replay settings usage to avoid N+1 queries- # This checks if any team in the same project uses this flag for session recording- # Extract the 'id' key from the JSONB field and cast to integer for safe comparison- from django.db.models import IntegerField- from django.db.models.functions import Cast-+ # Matches the containment check in FeatureFlagSerializer.get_is_used_in_replay_settings,+ # so the annotated and unannotated paths agree. Containment never casts, so a+ # non-integer id in the JSON yields False instead of erroring the query.queryset = queryset.annotate(is_used_in_replay_settings_annotation=Exists(Team.objects.filter(project_id=OuterRef("team__project_id"),+ session_recording_linked_flag__contains=JSONObject(id=OuterRef("id")),)- .annotate(json_flag_id=Cast("session_recording_linked_flag__id", IntegerField()))- .filter(json_flag_id=OuterRef("id"))))diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambrindex d1e3d292359a..b0bbe8ae6fce 100644--- a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr+++ b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr@@ -1608,7 +1608,7 @@(SELECT 1 AS "a"FROM "posthog_team" U0WHERE (U0."project_id" = ("posthog_team"."project_id")- AND ((U0."session_recording_linked_flag" -> 'id'))::integer = ("posthog_featureflag"."id"))+ AND U0."session_recording_linked_flag" @> (JSONB_BUILD_OBJECT(('id')::text, "posthog_featureflag"."id")))LIMIT 1) AS "is_used_in_replay_settings_annotation","posthog_user"."id","posthog_user"."password",diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.pyindex 3fd7e9b2ddce..dd4d74e4b325 100644--- a/products/feature_flags/backend/api/test/test_feature_flag.py+++ b/products/feature_flags/backend/api/test/test_feature_flag.py@@ -8293,6 +8293,36 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):assert body["updated"] == [{"id": flag.id, "tags": ["foo"]}]assert body["skipped"] == []+ def test_bulk_update_tags_with_non_integer_replay_linked_flag_id(self):+ # Replay usage must be computed by JSONB containment, never by casting the stored id+ # to integer: a sibling team's non-integer session_recording_linked_flag id would+ # error every flags queryset in the project, including bulk_update_tags and list.+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="replay-cast-test")+ self.team.session_recording_linked_flag = {"id": flag.id, "key": flag.key}+ self.team.save()+ Team.objects.create(+ organization=self.organization,+ project=self.team.project,+ session_recording_linked_flag={"id": "not-an-int", "key": "some-key"},+ )++ response = self.client.post(+ f"/api/projects/{self.team.id}/feature_flags/bulk_update_tags/",+ {"ids": [flag.id], "action": "add", "tags": ["foo"]},+ format="json",+ )+ assert response.status_code == status.HTTP_200_OK, response.json()+ body = response.json()+ assert body["updated"] == [{"id": flag.id, "tags": ["foo"]}]+ assert body["skipped"] == []++ # A wrong OuterRef correlation in the annotation would still return 200s, so assert+ # the linked flag actually reports replay usage through the list endpoint.+ list_response = self.client.get(f"/api/projects/{self.team.id}/feature_flags/")+ assert list_response.status_code == status.HTTP_200_OK, list_response.json()+ listed = next(f for f in list_response.json()["results"] if f["id"] == flag.id)+ assert listed["is_used_in_replay_settings"] is True+# Lives in the feature-flag test file (instead of test_insight.py) so the# full bulk-ops PAT regression story — positive feature-flag cases and the# negative cross-resource block — stays adjacent for reviewers of #57885.