Skip to content

: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.

fix(experiments): stop dropping metrics from AI experiment summary (#66664)

Juraj Majerik6d61cac2d51e
diff --git a/products/experiments/backend/experiment_summary_data_service.py b/products/experiments/backend/experiment_summary_data_service.py
index 16463719b746..c28d7294d36e 100644
--- a/products/experiments/backend/experiment_summary_data_service.py
+++ b/products/experiments/backend/experiment_summary_data_service.py
@@ -6,7 +6,6 @@ from zoneinfo import ZoneInfo
from django.conf import settings
-import posthoganalytics
from posthoganalytics import capture_exception
from posthog.schema import (
@@ -30,10 +29,8 @@ from posthog.hogql.constants import LimitContext
from posthog.clickhouse.client.connection import Workload
from posthog.clickhouse.query_tagging import Product, tags_context
-from posthog.constants import EXPERIMENTS_SYNC_QUERIES_FEATURE_FLAG_KEY
from posthog.event_usage import EventSource
from posthog.hogql_queries.query_runner import ExecutionMode
-from posthog.models.team.team import Team
from posthog.sync import database_sync_to_async
from products.experiments.backend.hogql_queries.experiment_exposures_query_runner import ExperimentExposuresQueryRunner
@@ -57,7 +54,7 @@ class ExposureQueryResult:
pending: bool
-MAX_METRICS_TO_SUMMARIZE = 20
+MAX_METRICS_TO_SUMMARIZE = 50
MAX_CONCURRENT_EXPERIMENT_SUMMARY_QUERIES = 10
# This threshold is just to avoid minor discrepancies in timestamps.
@@ -71,23 +68,6 @@ ExperimentMetricType = Union[
]
-def is_experiments_sync_queries_enabled(team: Team) -> bool:
- """Return whether experiment queries should block on stale cache for this project."""
- return bool(
- posthoganalytics.feature_enabled(
- EXPERIMENTS_SYNC_QUERIES_FEATURE_FLAG_KEY,
- str(team.uuid),
- groups={"organization": str(team.organization_id), "project": str(team.id)},
- group_properties={
- "organization": {"id": str(team.organization_id)},
- "project": {"id": str(team.id), "uuid": str(team.uuid)},
- },
- only_evaluate_locally=True,
- send_feature_flag_events=False,
- )
- )
-
-
def parse_metric_dict(metric_dict: dict) -> ExperimentMetricType | None:
"""Parse a metric dictionary into its typed Pydantic object."""
metric_type = metric_dict.get("metric_type")
@@ -195,11 +175,10 @@ class ExperimentSummaryDataService:
multivariate = feature_flag.filters.get("multivariate", {})
variants = [v.get("key") for v in multivariate.get("variants", []) if v.get("key")]
stats_method = get_experiment_stats_method(experiment)
- execution_mode = (
- ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE
- if is_experiments_sync_queries_enabled(experiment.team)
- else ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE_AND_BLOCKING_ON_MISS
- )
+ # PostHog AI gets one shot at the data — unlike the frontend it can't poll, so a
+ # stale metric returned as "pending" is silently dropped from the summary. Always
+ # block on stale cache so every metric resolves inline.
+ execution_mode = ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE
semaphore = asyncio.Semaphore(MAX_CONCURRENT_EXPERIMENT_SUMMARY_QUERIES)
diff --git a/products/experiments/backend/test/test_experiment_summary_tool.py b/products/experiments/backend/test/test_experiment_summary_tool.py
index 3142c139b1e0..947f14a852f2 100644
--- a/products/experiments/backend/test/test_experiment_summary_tool.py
+++ b/products/experiments/backend/test/test_experiment_summary_tool.py
@@ -337,10 +337,6 @@ class TestExperimentSummaryDataService(ClickhouseTestMixin, APIBaseTest):
mock_exposure_result.last_refresh = datetime(2020, 1, 10, 11, 0, tzinfo=ZoneInfo("UTC"))
with (
- patch(
- "products.experiments.backend.experiment_summary_data_service.posthoganalytics.feature_enabled",
- return_value=False,
- ),
patch(
"products.experiments.backend.experiment_summary_data_service.ExperimentQueryRunner"
) as mock_query_runner_class,
@@ -362,45 +358,7 @@ class TestExperimentSummaryDataService(ClickhouseTestMixin, APIBaseTest):
self.assertFalse(pending_calculation)
self.assertEqual(mock_query_runner_class.call_args.kwargs["limit_context"], LimitContext.QUERY_ASYNC)
self.assertEqual(mock_exposure_runner_class.call_args.kwargs["limit_context"], LimitContext.QUERY_ASYNC)
- self.assertEqual(
- mock_query_runner_class.return_value.run.call_args.kwargs["execution_mode"],
- ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE_AND_BLOCKING_ON_MISS,
- )
- self.assertEqual(
- mock_exposure_runner_class.return_value.run.call_args.kwargs["execution_mode"],
- ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE_AND_BLOCKING_ON_MISS,
- )
-
- @freeze_time("2020-01-10T12:00:00Z")
- async def test_fetch_experiment_data_uses_sync_execution_when_rollout_flag_enabled(self):
- experiment = await self.acreate_experiment(name="query-runner-sync-test", with_metrics=True)
-
- mock_query_result = MagicMock()
- mock_query_result.variant_results = []
- mock_query_result.last_refresh = datetime(2020, 1, 10, 11, 0, tzinfo=ZoneInfo("UTC"))
-
- mock_exposure_result = MagicMock()
- mock_exposure_result.total_exposures = {"control": 500, "test": 500}
- mock_exposure_result.last_refresh = datetime(2020, 1, 10, 11, 0, tzinfo=ZoneInfo("UTC"))
-
- with (
- patch(
- "products.experiments.backend.experiment_summary_data_service.posthoganalytics.feature_enabled",
- return_value=True,
- ),
- patch(
- "products.experiments.backend.experiment_summary_data_service.ExperimentQueryRunner"
- ) as mock_query_runner_class,
- patch(
- "products.experiments.backend.experiment_summary_data_service.ExperimentExposuresQueryRunner"
- ) as mock_exposure_runner_class,
- ):
- mock_query_runner_class.return_value.run.return_value = mock_query_result
- mock_exposure_runner_class.return_value.run.return_value = mock_exposure_result
-
- data_service = ExperimentSummaryDataService(self.team)
- await data_service.fetch_experiment_data(experiment.id)
-
+ # The agent can't poll, so it always blocks on stale cache rather than dropping pending metrics.
self.assertEqual(
mock_query_runner_class.return_value.run.call_args.kwargs["execution_mode"],
ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE,
@@ -496,10 +454,6 @@ class TestExperimentSummaryDataService(ClickhouseTestMixin, APIBaseTest):
mock_exposure_result.last_refresh = datetime(2020, 1, 10, 11, 0, tzinfo=ZoneInfo("UTC"))
with (
- patch(
- "products.experiments.backend.experiment_summary_data_service.posthoganalytics.feature_enabled",
- return_value=False,
- ),
patch(
"products.experiments.backend.experiment_summary_data_service.ExperimentQueryRunner"
) as mock_query_runner_class,
@@ -595,10 +549,6 @@ class TestExperimentSummaryDataService(ClickhouseTestMixin, APIBaseTest):
mock_exposure_result.last_refresh = datetime(2020, 1, 10, 11, 0, tzinfo=ZoneInfo("UTC"))
with (
- patch(
- "products.experiments.backend.experiment_summary_data_service.posthoganalytics.feature_enabled",
- return_value=False,
- ),
patch(
"products.experiments.backend.experiment_summary_data_service.ExperimentQueryRunner"
) as mock_query_runner_class,