: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): Stop logging running-time calculator drift to experiment history (#101606)
Juraj Majerikcae61e57ea5f
diff --git a/products/experiments/backend/activity_logging.py b/products/experiments/backend/activity_logging.pyindex 51e3af74d797..2a0ca984405d 100644--- a/products/experiments/backend/activity_logging.py+++ b/products/experiments/backend/activity_logging.py@@ -1,12 +1,33 @@from typing import Any-from posthog.models.activity_logging.activity_log import AuditableScope, Detail, changes_between, log_activity+from posthog.models.activity_logging.activity_log import AuditableScope, Change, Detail, changes_between, log_activityfrom posthog.models.signals import model_activity_signal, mutable_receiverfrom posthog.models.user import Userfrom products.experiments.backend.models.experiment import Experimentfrom products.experiments.backend.models.web_experiment import WebExperiment+# Kept in sync with DERIVED_RUNNING_TIME_KEYS in+# frontend/src/scenes/experiments/activity-descriptions/experimentChangeDescription.tsx.+DERIVED_RUNNING_TIME_KEYS = ("recommended_running_time", "recommended_sample_size")+++def _without_derived_running_time_keys(value: object) -> dict[str, object]:+ if not isinstance(value, dict):+ return {}+ return {key: item for key, item in value.items() if key not in DERIVED_RUNNING_TIME_KEYS}+++def _is_derived_running_time_drift(change: Change) -> bool:+ if change.field != "running_time_calculation":+ return False+ # The validator lets falsy non-dict values (for example []) through, and those are+ # never calculator drift, so a transition involving one must stay logged.+ for value in (change.before, change.after):+ if value is not None and not isinstance(value, dict):+ return False+ return _without_derived_running_time_keys(change.before) == _without_derived_running_time_keys(change.after)+@mutable_receiver(model_activity_signal, sender=Experiment)@mutable_receiver(model_activity_signal, sender=WebExperiment)@@ -37,6 +58,12 @@ def handle_experiment_change(# get cleared to null during updates, producing a noisy diffchanges = [change for change in changes if change.field != "parameters"]+ # Opening the calculator re-saves the recomputed outputs, so they drift as exposure data+ # changes. A change earns a log entry only when a calculator input was edited.+ # log_activity drops an "updated" activity whose changes end up empty, so a pure-drift+ # save produces no row at all.+ changes = [change for change in changes if not _is_derived_running_time_drift(change)]+log_activity(organization_id=after_update.team.organization_id,team_id=after_update.team_id,diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.pyindex c792090c261b..af2005e85512 100644--- a/products/experiments/backend/test/test_presentation_api.py+++ b/products/experiments/backend/test/test_presentation_api.py@@ -7232,6 +7232,61 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTeself.assertIn("description", change_fields)self.assertNotIn("parameters", change_fields)+ def test_running_time_calculation_output_drift_writes_no_activity_row(self):+ feature_flag = FeatureFlag.objects.create(+ team=self.team,+ name="Running time drift flag",+ key="running-time-drift",+ filters={},+ )+ experiment = Experiment.objects.create(+ team=self.team,+ created_by=self.user,+ name="Running time drift",+ feature_flag=feature_flag,+ running_time_calculation={+ "minimum_detectable_effect": 5,+ "recommended_sample_size": 1000,+ "recommended_running_time": 14,+ },+ )++ drift_response = self.client.patch(+ f"/api/projects/{self.team.id}/experiments/{experiment.id}/",+ {+ "running_time_calculation": {+ "minimum_detectable_effect": 5,+ "recommended_sample_size": 2000,+ "recommended_running_time": 28,+ }+ },+ format="json",+ )+ self.assertEqual(drift_response.status_code, status.HTTP_200_OK)+ self.assertEqual(+ ActivityLog.objects.filter(scope="Experiment", item_id=str(experiment.id), activity="updated").count(),+ 0,+ )++ input_response = self.client.patch(+ f"/api/projects/{self.team.id}/experiments/{experiment.id}/",+ {+ "running_time_calculation": {+ "minimum_detectable_effect": 10,+ "recommended_sample_size": 500,+ "recommended_running_time": 7,+ }+ },+ format="json",+ )+ self.assertEqual(input_response.status_code, status.HTTP_200_OK)+ activity_log = ActivityLog.objects.filter(+ scope="Experiment", item_id=str(experiment.id), activity="updated"+ ).latest("created_at")+ assert activity_log.detail is not None+ change_fields = [change["field"] for change in activity_log.detail["changes"]]+ self.assertIn("running_time_calculation", change_fields)+def test_experiment_saved_metric_activity_logging_shows_correct_user_for_updates(self):"""Test that experiment saved metric activity logs show the correct user for both creation and updates."""