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): salvage report when investigation loop fails (#106334)

Paul D'Ambra9913ece2878a
diff --git a/posthog/temporal/ai/anomaly_investigation/runner.py b/posthog/temporal/ai/anomaly_investigation/runner.py
index a20c56e7da01..ce906c5cf47c 100644
--- a/posthog/temporal/ai/anomaly_investigation/runner.py
+++ b/posthog/temporal/ai/anomaly_investigation/runner.py
@@ -268,8 +268,12 @@ async def run_investigation(
response = await llm_with_tools.ainvoke(messages, config=config)
except Exception as err:
logger.warning("anomaly_investigation.llm_invoke_error", extra={"error": str(err)})
+ recovered_report = _salvage_from_history(report_args_history) or _fallback_report(
+ f"LLM tool-calling loop failed: {err}"
+ )
+ recovered_report.tool_calls_used = tool_calls_used
return InvestigationRunResult(
- report=_fallback_report(f"LLM tool-calling loop failed: {err}"),
+ report=recovered_report,
tool_calls_used=tool_calls_used,
model=AGENT_MODEL,
)
@@ -314,6 +318,8 @@ async def run_investigation(
else:
try:
content = await handler(args)
+ if not isinstance(content, str) or not content.startswith("Error"):
+ report_args_history.clear()
except Exception as err:
logger.warning("anomaly_investigation.tool_error", extra={"tool": name, "error": str(err)})
content = f"Tool {name} failed: {err}"
diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_runner.py b/posthog/temporal/tests/ai/test_anomaly_investigation_runner.py
index 1feb576cf5ba..1bfd23c4ab2e 100644
--- a/posthog/temporal/tests/ai/test_anomaly_investigation_runner.py
+++ b/posthog/temporal/tests/ai/test_anomaly_investigation_runner.py
@@ -1,5 +1,5 @@
import pytest
-from unittest.mock import MagicMock, patch
+from unittest.mock import AsyncMock, MagicMock, patch
from langchain_core.messages import AIMessage
from parameterized import parameterized
@@ -307,7 +307,10 @@ class _ScriptedRunnable:
self._responses = list(responses)
async def ainvoke(self, messages, config=None):
- return self._responses.pop(0)
+ response = self._responses.pop(0)
+ if isinstance(response, Exception):
+ raise response
+ return response
def _budget_burning_turn() -> AIMessage:
@@ -335,6 +338,148 @@ _VALID_REPORT_ARGS = {
}
+ "report_args,following_turns,expected_verdict,expected_tool_calls,tool_error",
+ [
+ pytest.param(_UNRECOVERABLE_REPORT_ARGS, [], "true_positive", 1, False, id="salvage_previous_report"),
+ pytest.param({"verdict": "maybe", "summary": "x"}, [], "inconclusive", 1, False, id="fallback_without_salvage"),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "fetch_metric_series", "args": {}, "id": "call-2"}])],
+ "inconclusive",
+ 2,
+ False,
+ id="do_not_salvage_report_before_new_evidence",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [
+ AIMessage(content="", tool_calls=[{"name": "fetch_metric_series", "args": {}, "id": "call-2"}]),
+ _report_turn(_UNRECOVERABLE_REPORT_ARGS),
+ ],
+ "true_positive",
+ 2,
+ False,
+ id="salvage_report_after_new_evidence",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [
+ AIMessage(
+ content="",
+ tool_calls=[
+ {"name": FINAL_REPORT_TOOL_NAME, "args": _UNRECOVERABLE_REPORT_ARGS, "id": "report-2"},
+ {"name": "fetch_metric_series", "args": {}, "id": "call-2"},
+ ],
+ )
+ ],
+ "inconclusive",
+ 2,
+ False,
+ id="do_not_salvage_report_before_tool_in_same_turn",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [
+ AIMessage(
+ content="",
+ tool_calls=[
+ {"name": "fetch_metric_series", "args": {}, "id": "call-2"},
+ {"name": FINAL_REPORT_TOOL_NAME, "args": _UNRECOVERABLE_REPORT_ARGS, "id": "report-2"},
+ ],
+ )
+ ],
+ "inconclusive",
+ 2,
+ False,
+ id="do_not_salvage_report_after_tool_in_same_turn",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "fetch_metric_series", "args": {}, "id": "call-2"}])],
+ "true_positive",
+ 2,
+ True,
+ id="salvage_report_after_failed_tool",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "fetch_metric_series", "args": {}, "id": "call-2"}])],
+ "true_positive",
+ 2,
+ "Error: no insight bound to this investigation.",
+ id="salvage_report_after_tool_error_result",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "fetch_metric_series", "args": {}, "id": "call-2"}])],
+ "true_positive",
+ 2,
+ "Error fetching series: query failed",
+ id="salvage_report_after_series_error_result",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "simulate_detector", "args": {}, "id": "call-2"}])],
+ "true_positive",
+ 2,
+ "Error running simulation: query failed",
+ id="salvage_report_after_simulation_error_result",
+ ),
+ pytest.param(
+ _UNRECOVERABLE_REPORT_ARGS,
+ [AIMessage(content="", tool_calls=[{"name": "missing_tool", "args": {}, "id": "call-2"}])],
+ "true_positive",
+ 2,
+ False,
+ id="salvage_report_after_unknown_tool",
+ ),
+ ],
+)
+async def test_loop_failure_keeps_best_report_and_tool_count(
+ report_args: dict,
+ following_turns: list[AIMessage],
+ expected_verdict: str,
+ expected_tool_calls: int,
+ tool_error: bool | str,
+) -> None:
+ llm = MagicMock()
+ llm.bind_tools.side_effect = lambda tools: _ScriptedRunnable(
+ [
+ AIMessage(content="", tool_calls=[{"name": "noop_tool", "args": {}, "id": "call-1"}]),
+ _report_turn(report_args),
+ *following_turns,
+ RuntimeError("LLM unavailable"),
+ ]
+ )
+
+ with (
+ patch("ee.hogai.llm.MaxChatAnthropic", return_value=llm),
+ patch("posthog.temporal.ai.anomaly_investigation.runner.posthoganalytics") as mock_module,
+ patch(
+ "posthog.temporal.ai.anomaly_investigation.runner.InvestigationToolkit.fetch_metric_series",
+ new_callable=AsyncMock,
+ return_value=tool_error if isinstance(tool_error, str) else "New metric evidence",
+ side_effect=RuntimeError("tool failed") if tool_error is True else None,
+ ),
+ patch(
+ "posthog.temporal.ai.anomaly_investigation.runner.InvestigationToolkit.simulate_detector",
+ new_callable=AsyncMock,
+ return_value=tool_error if isinstance(tool_error, str) else "New metric evidence",
+ ),
+ ):
+ mock_module.default_client = None
+ result = await run_investigation(
+ team=MagicMock(id=1),
+ user=MagicMock(id=2),
+ anomaly_context="anomaly context",
+ )
+
+ assert result.report.verdict == expected_verdict
+ assert result.tool_calls_used == expected_tool_calls
+ assert result.report.tool_calls_used == expected_tool_calls
+
+
@pytest.mark.parametrize(
"finalize_responses,expected_hypothesis_count",
[