: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(experiments): Give lifecycle actions proper history copy (#101853)
Juraj Majerikdc8f0a58ea81
diff --git a/frontend/src/scenes/experiments/activity-descriptions/experimentChangeDescription.tsx b/frontend/src/scenes/experiments/activity-descriptions/experimentChangeDescription.tsxindex 24f8d41ae7a8..5b2680d3f959 100644--- a/frontend/src/scenes/experiments/activity-descriptions/experimentChangeDescription.tsx+++ b/frontend/src/scenes/experiments/activity-descriptions/experimentChangeDescription.tsx@@ -51,6 +51,8 @@ type AllowedExperimentFields = Pick<| 'excluded_variants'| 'primary_metrics_ordered_uuids'| 'secondary_metrics_ordered_uuids'+ | 'archived'+ | 'description'> & {deleted: boolean}@@ -150,6 +152,14 @@ export const getExperimentChangeDescription = (}}+ /**+ * a start_date clear rewrites the whole row to the 'reset' activity in the backend+ * handler, so this only renders for rows logged before that rewrite shipped+ */+ if (action === 'deleted') {+ return 'reset experiment:'+ }+return 'changed the start date'}).with({ field: 'end_date' }, ({ action, before, after }) => {@@ -160,6 +170,10 @@ export const getExperimentChangeDescription = (return 'stopped experiment'}+ if (action === 'deleted') {+ return 'removed the end date of'+ }+return 'changed the end date'}).with({ field: 'conclusion' }, ({ action, before, after }) => {@@ -183,8 +197,16 @@ export const getExperimentChangeDescription = ()}+ if (action === 'deleted') {+ return 'removed the conclusion of'+ }+return 'changed the conclusion'})+ .with({ field: 'archived' }, ({ after }) =>+ after === true ? 'archived experiment:' : 'unarchived experiment:'+ )+ .with({ field: 'description' }, () => 'updated the description').with({ field: 'metrics', action: 'created', before: null }, () => 'added the first metric to').with({ field: 'metrics', action: 'changed' }, ({ before, after }) =>getMetricChanges(before as ExperimentMetric[], after as ExperimentMetric[])diff --git a/frontend/src/scenes/experiments/experimentActivityDescriber.test.tsx b/frontend/src/scenes/experiments/experimentActivityDescriber.test.tsxindex 078544fd4f56..a9055f5e6344 100644--- a/frontend/src/scenes/experiments/experimentActivityDescriber.test.tsx+++ b/frontend/src/scenes/experiments/experimentActivityDescriber.test.tsx@@ -413,6 +413,113 @@ describe('experimentActivityDescriber', () => {expect(text).not.toContain(': on')})+ it('describes a reset entry without narrating the cleared fields', () => {+ const result = experimentActivityDescriber(+ baseLogItem({+ activity: 'reset',+ detail: {+ name: 'Checkout funnel',+ changes: [+ {+ type: ActivityScope.EXPERIMENT,+ action: 'deleted',+ field: 'start_date',+ before: '2026-06-18T14:25:34Z',+ after: null,+ },+ {+ type: ActivityScope.EXPERIMENT,+ action: 'deleted',+ field: 'end_date',+ before: '2026-07-18T14:25:34Z',+ after: null,+ },+ ],+ merge: null,+ trigger: null,+ },+ })+ )+ const text = textOf(result)+ expect(text).toContain('reset experiment')+ expect(text).not.toContain('start date')+ expect(text).not.toContain('end date')+ })++ it('keeps a row for a standalone end date removal', () => {+ const result = experimentActivityDescriber(+ baseLogItem({+ activity: 'updated',+ detail: {+ name: 'Checkout funnel',+ changes: [+ {+ type: ActivityScope.EXPERIMENT,+ action: 'deleted',+ field: 'end_date',+ before: '2026-07-18T14:25:34Z',+ after: null,+ },+ ],+ merge: null,+ trigger: null,+ },+ })+ )+ expect(textOf(result)).toContain('removed the end date')+ })++ it.each([+ [true, 'archived experiment'],+ [false, 'unarchived experiment'],+ ])('describes an archived change to %s', (after, expected) => {+ const result = experimentActivityDescriber(+ baseLogItem({+ activity: 'updated',+ detail: {+ name: 'Checkout funnel',+ changes: [+ {+ type: ActivityScope.EXPERIMENT,+ action: 'changed',+ field: 'archived',+ before: !after,+ after,+ },+ ],+ merge: null,+ trigger: null,+ },+ })+ )+ expect(textOf(result)).toContain(expected)+ })++ it('names the shipped variant', () => {+ const result = experimentActivityDescriber(+ baseLogItem({+ activity: 'variant_shipped',+ detail: {+ name: 'Checkout funnel',+ changes: [+ {+ type: ActivityScope.EXPERIMENT,+ action: 'created',+ field: 'shipped_variant',+ before: null,+ after: 'test-b',+ },+ ],+ merge: null,+ trigger: null,+ },+ })+ )+ const text = textOf(result)+ expect(text).toContain('shipped variant')+ expect(text).toContain('test-b')+ })+it('keeps a row for a comment-only conclusion edit', () => {const result = experimentActivityDescriber(baseLogItem({diff --git a/frontend/src/scenes/experiments/experimentActivityDescriber.tsx b/frontend/src/scenes/experiments/experimentActivityDescriber.tsxindex 4ece3b9a0b1d..512ee9dfe0b6 100644--- a/frontend/src/scenes/experiments/experimentActivityDescriber.tsx+++ b/frontend/src/scenes/experiments/experimentActivityDescriber.tsx@@ -270,6 +270,37 @@ export const experimentActivityDescriber = (logItem: ActivityLogItem): Humanized),}})+ .with({ activity: 'reset' }, ({ item_id, detail }) => {+ return {+ description: (+ <SentenceList+ prefix={<ActivityLogUserName logItem={logItem} />}+ listParts={['reset experiment:']}+ suffix={nameOrLinkToExperiment(detail.name, item_id)}+ />+ ),+ }+ })+ .with({ activity: 'variant_shipped' }, ({ item_id, detail }) => {+ const variantKey = detail.changes?.find((change) => change.field === 'shipped_variant')?.after+ return {+ description: (+ <SentenceList+ prefix={<ActivityLogUserName logItem={logItem} />}+ listParts={[+ typeof variantKey === 'string' ? (+ <span>+ shipped variant <strong>{variantKey}</strong> for+ </span>+ ) : (+ 'shipped a variant for'+ ),+ ]}+ suffix={nameOrLinkToExperiment(detail.name, item_id)}+ />+ ),+ }+ }).with({ activity: 'exposure_frozen' }, ({ item_id, detail }) => {return {description: (diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.pyindex e6cd00c92775..150aeda190af 100644--- a/posthog/models/activity_logging/activity_log.py+++ b/posthog/models/activity_logging/activity_log.py@@ -685,6 +685,8 @@ field_exclusions: dict[AuditableScope, list[str]] = {"experimenttosavedmetric_set",# Optimistic-concurrency counter, not a user-meaningful change."version",+ # Internal pointer to the flag-cleanup task, not a user-meaningful change.+ "flag_cleanup_task_id",],"ExperimentSavedMetric": ["experiments",diff --git a/products/experiments/backend/activity_logging.py b/products/experiments/backend/activity_logging.pyindex 2a0ca984405d..0284836c9b3c 100644--- a/products/experiments/backend/activity_logging.py+++ b/products/experiments/backend/activity_logging.py@@ -50,6 +50,10 @@ def handle_experiment_change(after_deleted = getattr(after_update, "deleted", None)if before_deleted is not None and after_deleted is not None and before_deleted != after_deleted:activity = "restored" if after_deleted is False else "deleted"+ # Clearing the start date returns the experiment to draft, which is what a reset does,+ # whichever endpoint performed the write.+ elif activity == "updated" and before_update.start_date is not None and after_update.start_date is None:+ activity = "reset"changes = changes_between(scope, previous=before_update, current=after_update)diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex 0a4a4b2a869d..303996aee6a4 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -44,7 +44,7 @@ from posthog.exceptions import (ClickHouseQueryMemoryLimitExceeded,ClickHouseQueryTimeOut,)-from posthog.models.activity_logging.activity_log import Detail, log_activity+from posthog.models.activity_logging.activity_log import Change, Detail, log_activityfrom posthog.models.activity_logging.model_activity import is_impersonated_sessionfrom posthog.models.activity_logging.utils import get_changed_fields_localfrom posthog.models.filters.filter import Filter@@ -3204,6 +3204,22 @@ class ExperimentService:shipped_fields.append("conclusion_comment")self._bump_version_and_save(experiment, update_fields=shipped_fields)+ # The flag rewrite logs under the FeatureFlag scope and the experiment save logs only+ # end_date/conclusion, so without this entry the History tab never names the shipped variant.+ log_activity(+ organization_id=self.team.organization_id,+ team_id=self.team.pk,+ user=self.user,+ was_impersonated=is_impersonated_session(request) if request else False,+ item_id=experiment.pk,+ scope="Experiment",+ activity="variant_shipped",+ detail=Detail(+ name=experiment.name,+ changes=[Change(type="Experiment", action="created", field="shipped_variant", after=variant_key)],+ ),+ )+self._report_experiment_variant_shipped(experiment, variant_key=variant_key, release_to_everyone=release_to_everyone, request=request)diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex d67e642e1565..0231c17a2c15 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -4408,6 +4408,10 @@ class TestExperimentService(APIBaseTest):assert reset.conclusion is Noneassert reset.conclusion_comment is Noneassert reset.flag_cleanup_task_id is None+ assert (+ ActivityLog.objects.filter(scope="Experiment", item_id=str(experiment.pk)).latest("created_at").activity+ == "reset"+ )def test_reset_experiment_leaves_feature_flag_unchanged(self):experiment = self._create_running_experiment(name="Reset Flag", feature_flag_key="reset-flag-unchanged")diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.pyindex d84769f3b2b0..27fe752871ad 100644--- a/products/experiments/backend/test/test_presentation_api.py+++ b/products/experiments/backend/test/test_presentation_api.py@@ -5934,6 +5934,13 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):# Default behavior: existing groups preserved, no catch-all prependedself.assertEqual(flag_filters["groups"], original_groups)+ activity_log = ActivityLog.objects.filter(+ scope="Experiment", item_id=str(experiment_id), activity="variant_shipped"+ ).latest("created_at")+ assert activity_log.detail is not None+ shipped_change = next(c for c in activity_log.detail["changes"] if c["field"] == "shipped_variant")+ self.assertEqual(shipped_change["after"], "test")+def test_ship_variant_endpoint_release_to_everyone_prepends_catch_all(self):data = self._create_running_experiment(name="Ship Everyone", flag_key="ship-everyone-flag")experiment_id = data["id"]