: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): refresh cached results when the end date changes (#129155)
Andersae7cd74afd58
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.pyindex 7702cf819bfd..f68270127850 100644--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py@@ -732,4 +732,9 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):payload = super().get_cache_payload()payload["experiment_response_version"] = 2payload["stats_method"] = self.stats_method+ # The window ends at the experiment's end date, which the query does not carry, so a new end date must miss+ # the result of the earlier window. A running experiment adds nothing, so its key stays the same and the cache+ # age limit refreshes its moving window end.+ if self.experiment.end_date is not None:+ payload["end_date"] = self.experiment.end_date.isoformat()return payloaddiff --git a/products/experiments/backend/hogql_queries/test/test_experiment_query_runner_single_flight.py b/products/experiments/backend/hogql_queries/test/test_experiment_query_runner_single_flight.pyindex 1142e3c44e96..fdef95b388c7 100644--- a/products/experiments/backend/hogql_queries/test/test_experiment_query_runner_single_flight.py+++ b/products/experiments/backend/hogql_queries/test/test_experiment_query_runner_single_flight.py@@ -29,9 +29,9 @@ class TestExperimentQueryRunnerSingleFlight(APIBaseTest):},created_by=self.user,)- experiment = Experiment.objects.create(name="test-experiment", team=self.team, feature_flag=feature_flag)+ self.experiment = Experiment.objects.create(name="test-experiment", team=self.team, feature_flag=feature_flag)self.query = ExperimentQuery(- experiment_id=experiment.id,+ experiment_id=self.experiment.id,kind="ExperimentQuery",metric=ExperimentMeanMetric(source=EventsNode(event="$pageview", math=ExperimentMetricMathType.TOTAL)),)@@ -50,3 +50,19 @@ class TestExperimentQueryRunnerSingleFlight(APIBaseTest):results_request.get_cache_key(),results_request.single_flight_variant(),)++ @parameterized.expand(+ [+ ("the_experiment_ends", None, datetime(2026, 1, 10, tzinfo=UTC)),+ ("the_end_date_moves", datetime(2026, 1, 10, tzinfo=UTC), datetime(2026, 1, 20, tzinfo=UTC)),+ ]+ )+ def test_an_end_date_change_changes_the_cache_key(+ self, _name: str, end_date: datetime | None, new_end_date: datetime+ ) -> None:+ Experiment.objects.filter(id=self.experiment.id).update(end_date=end_date)+ before = ExperimentQueryRunner(query=self.query, team=self.team).get_cache_key()++ Experiment.objects.filter(id=self.experiment.id).update(end_date=new_end_date)++ assert ExperimentQueryRunner(query=self.query, team=self.team).get_cache_key() != before