: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): invalidate results fingerprint when excluding variants (#78050)
Rodrigo Ilorod727d3d77f05
diff --git a/products/experiments/backend/recalculation.py b/products/experiments/backend/recalculation.pyindex 00e7706daebe..5143973d5eb2 100644--- a/products/experiments/backend/recalculation.py+++ b/products/experiments/backend/recalculation.py@@ -390,6 +390,7 @@ def _recalc_fingerprints_for_run(experiment: Experiment, recalc: ExperimentMetristats_method,experiment.exposure_criteria,only_count_matured_users=experiment.only_count_matured_users,+ excluded_variants=experiment.excluded_variants,)fingerprints[metric_uuid] = compute_recalc_fingerprint(config_fp)return fingerprints@@ -453,6 +454,7 @@ def build_timeseries_cold_start_payload(experiment: Experiment) -> dict | None:stats_method,experiment.exposure_criteria,only_count_matured_users=experiment.only_count_matured_users,+ excluded_variants=experiment.excluded_variants,)row = (ExperimentMetricResult.objects.filter(diff --git a/products/experiments/backend/temporal/recalculation_logic.py b/products/experiments/backend/temporal/recalculation_logic.pyindex c6f926a25d6e..e90fbd07449f 100644--- a/products/experiments/backend/temporal/recalculation_logic.py+++ b/products/experiments/backend/temporal/recalculation_logic.py@@ -599,6 +599,7 @@ def _calculate_experiment_metric_for_recalculation_sync(get_experiment_stats_method(experiment),experiment.exposure_criteria,only_count_matured_users=experiment.only_count_matured_users,+ excluded_variants=experiment.excluded_variants,)recalc_fp = compute_recalc_fingerprint(config_fp)diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.pyindex 952cbdd97d0b..1b803415b017 100644--- a/products/experiments/backend/temporal/test_recalculation_activities.py+++ b/products/experiments/backend/temporal/test_recalculation_activities.py@@ -805,6 +805,40 @@ class TestCalculateActivity(BaseTest):mock_runner.assert_called_once()+ def test_excluded_variants_change_recomputes(self):+ # A cached row computed before a variant was excluded must not satisfy the skip check: the exclusion+ # set feeds the fingerprint, so the stale row's fingerprint no longer matches and the query re-runs.+ metric = _mean_metric("m1")+ exp = self._experiment(flag_key="calc-excluded", metrics=[metric])+ query_to = datetime.fromisoformat(_QUERY_TO)+ recalc_fp = compute_recalc_fingerprint(+ compute_metric_fingerprint(+ metric,+ exp.start_date,+ get_experiment_stats_method(exp),+ exp.exposure_criteria,+ only_count_matured_users=exp.only_count_matured_users,+ )+ )+ ExperimentMetricResult.objects.create(+ experiment=exp,+ metric_uuid="m1",+ fingerprint=recalc_fp,+ query_from=query_to,+ query_to=query_to,+ status=ExperimentMetricResult.Status.COMPLETED,+ result={"stale": True},+ )+ exp.excluded_variants = ["test"]+ exp.save(update_fields=["excluded_variants"])+ recalc = self._recalc(exp, metric_uuids=["m1"])++ with patch("products.experiments.backend.temporal.recalculation_logic.ExperimentQueryRunner") as mock_runner:+ mock_runner.return_value.run.return_value.model_dump.return_value = {}+ _calculate(exp.id, "m1", str(recalc.id), _QUERY_TO)++ mock_runner.assert_called_once()+def test_store_result_updates_existing_row_with_different_fingerprint_in_place(self):# The unique constraint is (experiment, metric_uuid, query_to); fingerprint is not part of it. A row may# already occupy that key under a different fingerprint (an earlier run written under the old per-rundiff --git a/products/experiments/backend/test/test_recalculation_service.py b/products/experiments/backend/test/test_recalculation_service.pyindex 1a6bb31467e0..c54c933e07e4 100644--- a/products/experiments/backend/test/test_recalculation_service.py+++ b/products/experiments/backend/test/test_recalculation_service.py@@ -315,6 +315,40 @@ class TestRecalculationService(BaseTest):assert results[0]["result"] == {"ok": True}assert results[0]["error_message"] is None+ def test_changing_excluded_variants_invalidates_run_results(self):+ exp = self._launched_experiment(flag_key="reuse-excluded")+ assert exp.start_date is not None # _launched_experiment always sets it; narrow for the create below+ query_to = datetime(2026, 1, 10, tzinfo=UTC)+ recalc_fp = compute_recalc_fingerprint(+ compute_metric_fingerprint(+ _mean_metric("m1"),+ exp.start_date,+ get_experiment_stats_method(exp),+ exp.exposure_criteria,+ only_count_matured_users=exp.only_count_matured_users,+ excluded_variants=exp.excluded_variants,+ )+ )+ ExperimentMetricResult.objects.create(+ experiment=exp,+ metric_uuid="m1",+ fingerprint=recalc_fp,+ query_from=exp.start_date,+ query_to=query_to,+ status=ExperimentMetricResult.Status.COMPLETED,+ result={"some": "result"},+ )+ recalc = ExperimentMetricsRecalculation.objects.create(+ team=self.team, experiment=exp, metric_uuids=["m1"], query_to=query_to+ )+ assert [r["metric_uuid"] for r in get_run_results(recalc)] == ["m1"]++ # Excluding a variant alters what the metric computes, so the cached row must stop matching.+ exp.excluded_variants = ["test"]+ exp.save(update_fields=["excluded_variants"])+ recalc.refresh_from_db()+ assert get_run_results(recalc) == []+def test_get_run_results_strips_step_sessions(self):# step_sessions carries per-step person IDs, session IDs, and event UUIDs. It powers the frontend's# "view sessions per step" affordance off a separate per-metric query, so it does not belong in the