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(alerts): pass the alert config into the anomaly investigation simulation (#89887)

posthog[bot]88997b515b33
diff --git a/posthog/temporal/ai/anomaly_investigation/tools.py b/posthog/temporal/ai/anomaly_investigation/tools.py
index 707580d6e8ee..dc9708d3c6ab 100644
--- a/posthog/temporal/ai/anomaly_investigation/tools.py
+++ b/posthog/temporal/ai/anomaly_investigation/tools.py
@@ -116,6 +116,11 @@ def _run_detector_simulation(
# is chosen by config.series_index. Without this the simulation defaults to series 0,
# so the investigation analyzes a different series than the one that actually fired.
series_index=(alert.config or {}).get("series_index", 0),
+ # Pass the full alert config too: HogQLDetectorExtractor.simulate reads config.column to
+ # pick which numeric column to score. Without it a SQL insight with several numeric
+ # columns fails with "more than one of them is numeric", so the investigation gets no
+ # series and no baseline.
+ config=alert.config,
date_from=date_from,
user=alert.created_by,
)
diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_tools.py b/posthog/temporal/tests/ai/test_anomaly_investigation_tools.py
index 566d9847c76d..a81c342655ff 100644
--- a/posthog/temporal/tests/ai/test_anomaly_investigation_tools.py
+++ b/posthog/temporal/tests/ai/test_anomaly_investigation_tools.py
@@ -2,7 +2,15 @@ from typing import Any
from unittest.mock import MagicMock, patch
-from posthog.schema import BaseMathType, ChartDisplayType, EventsNode, IntervalType, TrendsFilter, TrendsQuery
+from posthog.schema import (
+ BaseMathType,
+ ChartDisplayType,
+ EventsNode,
+ HogQLQuery,
+ IntervalType,
+ TrendsFilter,
+ TrendsQuery,
+)
from posthog.caching.insight_result import InsightResult
from posthog.temporal.ai.anomaly_investigation.tools import _run_detector_simulation
@@ -52,3 +60,37 @@ def test_run_detector_simulation_returns_the_alerts_configured_series(mock_calcu
assert not isinstance(result, str)
assert result["data"] == configured_series[:-1]
+
+
+@patch("products.alerts.backend.evaluation.hogql.calculate_for_query_based_insight")
+def test_run_detector_simulation_scores_the_configured_column_of_a_multi_numeric_sql_result(
+ mock_calculate: MagicMock,
+) -> None:
+ # zscore needs 31 samples; give the series headroom so it scores.
+ failure_rate = [10.0 + (i % 7) for i in range(40)]
+ run_count = [1800.0 + i for i in range(len(failure_rate))]
+ days = [f"2026-07-{(i % 30) + 1:02d}" for i in range(len(failure_rate))]
+ # Two numeric columns: without the alert's config the simulation can't pick which one to score.
+ rows = [[days[i], failure_rate[i], run_count[i]] for i in range(len(failure_rate))]
+ mock_calculate.return_value = InsightResult(
+ result=rows,
+ columns=["day", "failure_rate_pct", "run_count"],
+ timezone="UTC",
+ last_refresh=None,
+ cache_key="",
+ is_cached=False,
+ )
+
+ insight = MagicMock(spec=Insight)
+ insight.query = HogQLQuery(query="SELECT day, failure_rate_pct, run_count FROM ci_runs").model_dump()
+ alert = MagicMock(spec=AlertConfiguration)
+ alert.insight = insight
+ alert.config = {"type": "HogQLAlertConfig", "column": "failure_rate_pct", "evaluation": "last_row"}
+ alert.detector_config = {"type": "zscore", "threshold": 0.9, "window": 7}
+ alert.created_by = None
+
+ result = _run_detector_simulation(alert=alert, team=MagicMock(), date_from=None)
+
+ assert not isinstance(result, str)
+ # Scores failure_rate_pct, not run_count. The data is the tail of the configured column.
+ assert result["data"] == failure_rate[-len(result["data"]) :]