: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
feat(experiments): include linked flag changes in experiment activity (#88220)
Juraj Majerik802f518f88db
diff --git a/products/experiments/backend/presentation/views.py b/products/experiments/backend/presentation/views.pyindex 2481895fbce5..c70a03541668 100644--- a/products/experiments/backend/presentation/views.py+++ b/products/experiments/backend/presentation/views.py@@ -728,9 +728,9 @@ class EnterpriseExperimentsViewSet("""Change history for this experiment.- Returns a paginated audit trail of changes to the experiment and its holdouts- and shared metrics: who made each change, what changed (field-level before/after- values), and when. Ordered newest first.+ Returns a paginated audit trail of changes to the experiment, its holdouts and+ shared metrics, and its linked feature flag: who made each change, what changed+ (field-level before/after values), and when. Ordered newest first."""limit = request.validated_query_data["limit"]page = request.validated_query_data["page"]@@ -741,16 +741,26 @@ class EnterpriseExperimentsViewSet(# object's own id, so they need their own type-matched clauses. The experiment's own# clause must exclude those detail types in turn: an unrelated holdout or shared# metric whose pk collides with this experiment's id would otherwise leak in.- activity_filter = Q(item_id=str(experiment.id)) & ~Q(detail__type__in=["holdout", "shared_metric"])+ # The linked flag's changes log under the FeatureFlag scope, so each clause carries+ # its own scope instead of a single outer scope filter.+ activity_filter = Q(scope="Experiment", item_id=str(experiment.id)) & ~Q(+ detail__type__in=["holdout", "shared_metric"]+ )if experiment.holdout_id is not None:- activity_filter |= Q(item_id=str(experiment.holdout_id), detail__type="holdout")+ activity_filter |= Q(scope="Experiment", item_id=str(experiment.holdout_id), detail__type="holdout")saved_metric_ids = [str(pk) for pk in experiment.saved_metrics.values_list("id", flat=True)]if saved_metric_ids:- activity_filter |= Q(item_id__in=saved_metric_ids, detail__type="shared_metric")+ activity_filter |= Q(scope="Experiment", item_id__in=saved_metric_ids, detail__type="shared_metric")+ # The flag's history stays behind the flag's own access controls: a viewer of the+ # experiment may have "none" access to the linked flag.+ if experiment.feature_flag_id is not None and self.user_access_control.check_access_level_for_object(+ experiment.feature_flag, required_level="viewer"+ ):+ activity_filter |= Q(scope="FeatureFlag", item_id=str(experiment.feature_flag_id))activity_query = (ActivityLog.objects.select_related("user")- .filter(activity_filter, team_id=self.team_id, scope="Experiment")+ .filter(activity_filter, team_id=self.team_id).order_by("-created_at"))activity_page = get_activity_page(activity_query, limit, page)diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.pyindex dac35575e168..f5420dd24e36 100644--- a/products/experiments/backend/test/test_presentation_api.py+++ b/products/experiments/backend/test/test_presentation_api.py@@ -17,6 +17,7 @@ from parameterized import parameterizedfrom rest_framework import statusfrom posthog.auth import IDJagAccessTokenAuthentication, OAuthAccessTokenAuthentication, PersonalAPIKeyAuthentication+from posthog.constants import AvailableFeaturefrom posthog.models import Organization, OrganizationMembership, Teamfrom posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.personal_api_key import PersonalAPIKey@@ -7131,6 +7132,7 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe)self.assertEqual(create_response.status_code, status.HTTP_201_CREATED)experiment_id = create_response.json()["id"]+ flag_id = create_response.json()["feature_flag"]["id"]self.client.patch(f"/api/projects/{self.team.id}/experiments/{experiment_id}/", {"description": "Updated"})self.client.patch(f"/api/projects/{self.team.id}/experiment_holdouts/{holdout_id}/", {"name": "Renamed holdout"}@@ -7138,15 +7140,21 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTeself.client.patch(f"/api/projects/{self.team.id}/experiment_saved_metrics/{saved_metric_id}/", {"name": "Renamed metric"})+ flag_patch_response = self.client.patch(+ f"/api/projects/{self.team.id}/feature_flags/{flag_id}/", {"active": False}+ )+ self.assertEqual(flag_patch_response.status_code, status.HTTP_200_OK)other_response = self.client.post(f"/api/projects/{self.team.id}/experiments/",{"name": "Unrelated experiment", "feature_flag_key": "activity-endpoint-two"},)other_experiment_id = other_response.json()["id"]+ other_flag_id = other_response.json()["feature_flag"]["id"]self.client.patch(f"/api/projects/{self.team.id}/experiments/{other_experiment_id}/", {"description": "Unrelated update"})+ self.client.patch(f"/api/projects/{self.team.id}/feature_flags/{other_flag_id}/", {"active": False})# An unrelated shared metric whose pk collides with the experiment's idActivityLog.objects.create(team_id=self.team.pk,@@ -7156,6 +7164,16 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTeactivity="updated",detail={"type": "shared_metric", "name": "Colliding metric"},)+ # Same ids under an unrelated scope: leaks if any clause drops its scope match+ for colliding_item_id in (str(experiment_id), str(flag_id)):+ ActivityLog.objects.create(+ team_id=self.team.pk,+ organization_id=self.organization.id,+ scope="Insight",+ item_id=colliding_item_id,+ activity="updated",+ detail={"name": "Colliding insight"},+ )response = self.client.get(f"/api/projects/{self.team.id}/experiments/{experiment_id}/activity?limit=50")self.assertEqual(response.status_code, status.HTTP_200_OK)@@ -7163,7 +7181,11 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTeitem_ids = {entry["item_id"] for entry in results}self.assertNotIn(str(other_experiment_id), item_ids)- self.assertLessEqual(item_ids, {str(experiment_id), str(holdout_id), str(saved_metric_id)})+ self.assertLessEqual(item_ids, {str(experiment_id), str(holdout_id), str(saved_metric_id), str(flag_id)})+ flag_entries = [entry for entry in results if entry["scope"] == "FeatureFlag"]+ self.assertEqual({entry["item_id"] for entry in flag_entries}, {str(flag_id)})+ self.assertIn("updated", [entry["activity"] for entry in flag_entries])+ self.assertNotIn("Colliding insight", {(entry["detail"] or {}).get("name") for entry in results})own_activities = [entry["activity"] for entry in results if entry["item_id"] == str(experiment_id)]self.assertIn("created", own_activities)self.assertIn("updated", own_activities)@@ -7177,6 +7199,37 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe))+ def test_activity_endpoint_omits_flag_entries_without_flag_access(self):+ self.organization.available_product_features = [+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}+ ]+ self.organization.save()++ create_response = self.client.post(+ f"/api/projects/{self.team.id}/experiments/",+ {"name": "Restricted flag", "feature_flag_key": "activity-restricted-flag"},+ )+ self.assertEqual(create_response.status_code, status.HTTP_201_CREATED)+ experiment_id = create_response.json()["id"]+ flag_id = create_response.json()["feature_flag"]["id"]+ flag_patch_response = self.client.patch(+ f"/api/projects/{self.team.id}/feature_flags/{flag_id}/", {"active": False}+ )+ self.assertEqual(flag_patch_response.status_code, status.HTTP_200_OK)++ AccessControl.objects.create(+ team=self.team, resource="feature_flag", resource_id=str(flag_id), access_level="none"+ )+ # The flag's creator keeps access regardless of access controls, so query as a plain member+ other_user = User.objects.create_and_join(self.organization, "[email protected]", None)+ self.client.force_login(other_user)++ response = self.client.get(f"/api/projects/{self.team.id}/experiments/{experiment_id}/activity?limit=50")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ results = response.json()["results"]+ self.assertTrue(results)+ self.assertEqual({entry["scope"] for entry in results}, {"Experiment"})+def test_web_experiment_activity_logging_excludes_parameters_through_main_endpoint(self):feature_flag = FeatureFlag.objects.create(team=self.team,diff --git a/products/experiments/frontend/generated/api.ts b/products/experiments/frontend/generated/api.tsindex 0175e25912e8..d96320181bdb 100644--- a/products/experiments/frontend/generated/api.ts+++ b/products/experiments/frontend/generated/api.ts@@ -434,9 +434,9 @@ export const getExperimentsActivityRetrieveUrl = (/*** Change history for this experiment.*- * Returns a paginated audit trail of changes to the experiment and its holdouts- * and shared metrics: who made each change, what changed (field-level before/after- * values), and when. Ordered newest first.+ * Returns a paginated audit trail of changes to the experiment, its holdouts and+ * shared metrics, and its linked feature flag: who made each change, what changed+ * (field-level before/after values), and when. Ordered newest first.*/export const experimentsActivityRetrieve = async (projectId: string,diff --git a/services/mcp/src/generated/experiments/api.ts b/services/mcp/src/generated/experiments/api.tsindex 735a75c1ac38..6897af481a17 100644--- a/services/mcp/src/generated/experiments/api.ts+++ b/services/mcp/src/generated/experiments/api.ts@@ -10921,9 +10921,9 @@ export const ExperimentsDestroyParams = /* @__PURE__ */ zod.object({/*** Change history for this experiment.*- * Returns a paginated audit trail of changes to the experiment and its holdouts- * and shared metrics: who made each change, what changed (field-level before/after- * values), and when. Ordered newest first.+ * Returns a paginated audit trail of changes to the experiment, its holdouts and+ * shared metrics, and its linked feature flag: who made each change, what changed+ * (field-level before/after values), and when. Ordered newest first.*/export const ExperimentsActivityRetrieveParams = /* @__PURE__ */ zod.object({id: zod.number().describe('A unique integer value identifying this experiment.'),