: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
feat(alerts): bump investigation agent model and gate inbox signals on verdict (#73260)
Andrew Maguire45ac938d2fcd
diff --git a/posthog/tasks/alerts/investigation_notifications.py b/posthog/tasks/alerts/investigation_notifications.pyindex 731789919c55..f0ebc2bfdaf1 100644--- a/posthog/tasks/alerts/investigation_notifications.py+++ b/posthog/tasks/alerts/investigation_notifications.py@@ -40,9 +40,10 @@ INVESTIGATION_NOTIFY_GRACE_MINUTES = 5# Fallback for non-terminal investigations (RUNNING / PENDING / SKIPPED / null). Must# exceed the activity's worst case — `ANOMALY_INVESTIGATION_ACTIVITY_START_TO_CLOSE`-# (20 min) × `ANOMALY_INVESTIGATION_ACTIVITY_MAX_ATTEMPTS` (2) — so a healthy long-# investigation isn't preempted by a duplicate force-dispatch.-INVESTIGATION_RUNNING_GRACE_MINUTES = 45+# (30 min) × `ANOMALY_INVESTIGATION_ACTIVITY_MAX_ATTEMPTS` (2) = 60 min — plus slack, so a+# healthy long investigation on its second attempt isn't preempted by a duplicate+# force-dispatch that would fire a notification the verdict gate was meant to hold.+INVESTIGATION_RUNNING_GRACE_MINUTES = 70def run_investigation_notification_safety_net() -> int:diff --git a/posthog/temporal/ai/anomaly_investigation/runner.py b/posthog/temporal/ai/anomaly_investigation/runner.pyindex f3095cbf578c..dfe28b38d398 100644--- a/posthog/temporal/ai/anomaly_investigation/runner.py+++ b/posthog/temporal/ai/anomaly_investigation/runner.py@@ -42,12 +42,16 @@ from products.alerts.backend.models.alert import AlertConfigurationlogger = logging.getLogger(__name__)MAX_TOOL_CALLS = 10-AGENT_MODEL = "claude-sonnet-4-6"+AGENT_MODEL = "claude-sonnet-5"FINAL_REPORT_TOOL_NAME = "submit_investigation_report"MAX_TOOL_RESULT_CHARS = 12_000 # ~3K tokens per call — keeps 10 calls well under the context limit.+# Sonnet 5 runs adaptive thinking by default, which counts against max_tokens —+# leave headroom so a thinking-heavy turn can't truncate the final report.+MAX_OUTPUT_TOKENS = 8192# Per-request cap. The surrounding Temporal activity has its own (longer) deadline;# this guards against a single stuck HTTP call hanging for the whole activity budget.-LLM_REQUEST_TIMEOUT_SECONDS = 90.0+# Sized for adaptive-thinking turns, which run longer than plain tool-call turns.+LLM_REQUEST_TIMEOUT_SECONDS = 180.0ToolHandler = Callable[[dict[str, Any]], Awaitable[str]]@@ -133,14 +137,19 @@ async def run_investigation("input_schema": InvestigationReport.model_json_schema(),}+ # No temperature: Sonnet 5 rejects non-default sampling params with a 400.llm = MaxChatAnthropic(model=AGENT_MODEL,team=team,user=user,billable=True,inject_context=True,- max_retries=2,- temperature=0,+ # One in-request retry absorbs a transient blip cheaply; the Temporal activity retry+ # (maximum_attempts=2) is the outer safety net. Keeping this low bounds the aggregate+ # LLM wall-clock so a run stays inside the activity's start_to_close deadline instead of+ # being killed mid-flight (which would skip the fallback report and re-run the whole agent).+ max_retries=1,+ max_tokens=MAX_OUTPUT_TOKENS,default_request_timeout=LLM_REQUEST_TIMEOUT_SECONDS,posthog_properties={"ai_product": "alert_investigation_agent"},)@@ -157,7 +166,12 @@ async def run_investigation(final_report_tool,])- llm_with_final_report = llm.bind_tools([final_report_tool], tool_choice=FINAL_REPORT_TOOL_NAME)+ # Auto tool choice, not forced: Sonnet 5's default thinking mode only supports auto/none tool+ # choice, so forcing a specific tool returns a 400 and this finalize turn would fall through to+ # the generic fallback report. Binding *only* the final-report tool, plus the explicit "submit+ # now" nudge on the budget-exhausted turn, reliably elicits the call without forcing it; if the+ # model returns plain text instead, the text-JSON fallback in _parse_report still recovers it.+ llm_with_final_report = llm.bind_tools([final_report_tool])# Without a langchain CallbackHandler attached, MaxChatAnthropic's posthog_properties# never reach AI observability — langchain-anthropic itself doesn't emit $ai_* events.@@ -250,7 +264,7 @@ async def run_investigation(logger.warning("anomaly_investigation.tool_error", extra={"tool": name, "error": str(err)})content = f"Tool {name} failed: {err}"# Guard against runaway tool responses pushing the conversation past- # Anthropic's 200K-token context window. Keep the first slice; if the+ # the model's context window. Keep the first slice; if the# agent needs more it can issue a narrower query.if isinstance(content, str) and len(content) > MAX_TOOL_RESULT_CHARS:content = content[:MAX_TOOL_RESULT_CHARS] + "\n[truncated — narrow the query for more]"diff --git a/posthog/temporal/ai/anomaly_investigation/workflow.py b/posthog/temporal/ai/anomaly_investigation/workflow.pyindex 3b3c15462c98..a6315dff93d9 100644--- a/posthog/temporal/ai/anomaly_investigation/workflow.py+++ b/posthog/temporal/ai/anomaly_investigation/workflow.py@@ -49,7 +49,12 @@ SIGNAL_SOURCE_PRODUCT = "analytics"SIGNAL_SOURCE_TYPE = "anomaly_investigation"-ANOMALY_INVESTIGATION_ACTIVITY_START_TO_CLOSE = 20 * 60 # 20 minutes+# Sized to cover a realistic worst-case sequential agent run: up to MAX_TOOL_CALLS + 1 LLM turns,+# each capped at the runner's per-request timeout (thinking turns run long). 20 min got tight once+# that per-request timeout rose to 180s, so a slow thinking-heavy run could blow the deadline and be+# killed mid-flight — skipping the runner's fallback path and re-running the whole agent. 30 min+# keeps a realistic run inside a single activity attempt; the pathological tail falls to the retry.+ANOMALY_INVESTIGATION_ACTIVITY_START_TO_CLOSE = 30 * 60 # 30 minutesANOMALY_INVESTIGATION_ACTIVITY_HEARTBEAT_TIMEOUT = 5 * 60 # 5 minutesANOMALY_INVESTIGATION_ACTIVITY_MAX_ATTEMPTS = 2@@ -201,9 +206,20 @@ async def investigate_anomaly_activity(inputs: AnomalyInvestigationWorkflowInputnotebook_short_id=notebook.short_id,)- # Surface the completed investigation to the Signals inbox. Best-effort: the investigation- # itself has already succeeded and been persisted, so a failure to emit must not fail the- # activity (and trigger a re-run of the whole agent).+ # Surface the completed investigation to the Signals inbox, gated on the verdict so+ # false-positive fires don't become inbox noise (the verdict stays auditable on the+ # AlertCheck and in the notebook). Best-effort: the investigation itself has already+ # succeeded and been persisted, so a failure to emit must not fail the activity+ # (and trigger a re-run of the whole agent).+ if not should_emit_investigation_signal(result.report.verdict, alert.investigation_inconclusive_action):+ logger.info(+ "anomaly_investigation.signal_skipped",+ alert_id=str(alert.id),+ alert_check_id=str(alert_check.id),+ verdict=result.report.verdict,+ )+ return+try:await _emit_investigation_signal(team=team,@@ -222,6 +238,20 @@ async def investigate_anomaly_activity(inputs: AnomalyInvestigationWorkflowInput)+def should_emit_investigation_signal(verdict: str | None, inconclusive_action: str | None) -> bool:+ """Whether a completed investigation should surface in the Signals inbox.++ True positives always emit; false positives never do. Inconclusive follows the+ alert's `investigation_inconclusive_action` policy, mirroring the notification+ gate, so one per-alert knob controls both surfaces.+ """+ if verdict == "true_positive":+ return True+ if verdict == "inconclusive":+ return (inconclusive_action or "notify") == "notify"+ return False++def _build_investigation_signal_extra(*,alert: AlertConfiguration,diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_signal.py b/posthog/temporal/tests/ai/test_anomaly_investigation_signal.pyindex 7a00b91f54d5..36164a9c02bb 100644--- a/posthog/temporal/tests/ai/test_anomaly_investigation_signal.py+++ b/posthog/temporal/tests/ai/test_anomaly_investigation_signal.py@@ -1,5 +1,11 @@+from parameterized import parameterized+from posthog.temporal.ai.anomaly_investigation.report import InvestigationReport-from posthog.temporal.ai.anomaly_investigation.workflow import _MAX_DESCRIPTION_CHARS, _build_signal_description+from posthog.temporal.ai.anomaly_investigation.workflow import (+ _MAX_DESCRIPTION_CHARS,+ _build_signal_description,+ should_emit_investigation_signal,+)class TestBuildSignalDescription:@@ -42,3 +48,21 @@ class TestBuildSignalDescription:assert "verdict: true positive" in descriptionassert "id 42" in description+++class TestShouldEmitInvestigationSignal:+ # Guards the verdict gate: a regression here floods the Signals inbox with+ # false-positive investigation reports again.+ @parameterized.expand(+ [+ ("true_positive", "notify", True),+ ("true_positive", "suppress", True),+ ("false_positive", "notify", False),+ ("false_positive", "suppress", False),+ ("inconclusive", "notify", True),+ ("inconclusive", "suppress", False),+ ("inconclusive", None, True),+ ]+ )+ def test_gating_by_verdict_and_policy(self, verdict: str, inconclusive_action: str | None, expected: bool) -> None:+ assert should_emit_investigation_signal(verdict, inconclusive_action) is expecteddiff --git a/products/alerts/backend/api/alert.py b/products/alerts/backend/api/alert.pyindex ff6dfc13afc6..356b23baa85c 100644--- a/products/alerts/backend/api/alert.py+++ b/products/alerts/backend/api/alert.py@@ -356,7 +356,7 @@ class AlertSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerializeinvestigation_inconclusive_action = serializers.ChoiceField(choices=[("notify", "Notify"), ("suppress", "Suppress")],required=False,- help_text="How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.",+ help_text="How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.",)state = serializers.CharField(read_only=True,diff --git a/products/alerts/frontend/generated/api.schemas.ts b/products/alerts/frontend/generated/api.schemas.tsindex 2e6b6df02d83..54178736f77d 100644--- a/products/alerts/frontend/generated/api.schemas.ts+++ b/products/alerts/frontend/generated/api.schemas.ts@@ -655,7 +655,7 @@ export interface AlertApi {investigation_agent_enabled?: boolean/** When enabled (and investigation_agent_enabled is on), notification dispatch is held until the investigation agent produces a verdict. Notifications are suppressed when the verdict is false_positive (and optionally when inconclusive). A safety-net task force-fires after a few minutes if the investigation stalls. */investigation_gates_notifications?: boolean- /** How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.+ /** How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.** * `notify` - Notify* * `suppress` - Suppress */@@ -740,7 +740,7 @@ export interface PatchedAlertApi {investigation_agent_enabled?: boolean/** When enabled (and investigation_agent_enabled is on), notification dispatch is held until the investigation agent produces a verdict. Notifications are suppressed when the verdict is false_positive (and optionally when inconclusive). A safety-net task force-fires after a few minutes if the investigation stalls. */investigation_gates_notifications?: boolean- /** How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.+ /** How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.** * `notify` - Notify* * `suppress` - Suppress */diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 27378d8d262d..ced72a06bc2c 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -9856,7 +9856,7 @@ export namespace Schemas {investigation_agent_enabled?: boolean;/** When enabled (and investigation_agent_enabled is on), notification dispatch is held until the investigation agent produces a verdict. Notifications are suppressed when the verdict is false_positive (and optionally when inconclusive). A safety-net task force-fires after a few minutes if the investigation stalls. */investigation_gates_notifications?: boolean;- /** How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.+ /** How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.** * `notify` - Notify* * `suppress` - Suppress */@@ -46762,7 +46762,7 @@ export namespace Schemas {investigation_agent_enabled?: boolean;/** When enabled (and investigation_agent_enabled is on), notification dispatch is held until the investigation agent produces a verdict. Notifications are suppressed when the verdict is false_positive (and optionally when inconclusive). A safety-net task force-fires after a few minutes if the investigation stalls. */investigation_gates_notifications?: boolean;- /** How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.+ /** How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.** * `notify` - Notify* * `suppress` - Suppress */diff --git a/services/mcp/src/generated/alerts/api.ts b/services/mcp/src/generated/alerts/api.tsindex 83d43bddf5b3..6d5a5a481c6b 100644--- a/services/mcp/src/generated/alerts/api.ts+++ b/services/mcp/src/generated/alerts/api.ts@@ -1299,7 +1299,7 @@ export const AlertsCreateBody = /* @__PURE__ */ zod.object({.describe('\* `notify` - Notify\n\* `suppress` - Suppress').optional().describe(- "How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.\n\n\* `notify` - Notify\n\* `suppress` - Suppress"+ "How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.\n\n\* `notify` - Notify\n\* `suppress` - Suppress"),})@@ -2628,7 +2628,7 @@ export const AlertsPartialUpdateBody = /* @__PURE__ */ zod.object({.describe('\* `notify` - Notify\n\* `suppress` - Suppress').optional().describe(- "How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.\n\n\* `notify` - Notify\n\* `suppress` - Suppress"+ "How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.\n\n\* `notify` - Notify\n\* `suppress` - Suppress"),})diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.jsonindex a309321e86c6..97cacfe55201 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json@@ -2240,7 +2240,7 @@"type": "boolean"},"investigation_inconclusive_action": {- "description": "How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.\n\n* `notify` - Notify\n* `suppress` - Suppress",+ "description": "How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.\n\n* `notify` - Notify\n* `suppress` - Suppress","enum": ["notify", "suppress"],"type": "string"},diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.jsonindex 1ed7f3f2ad86..bee2683eafc6 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json@@ -2244,7 +2244,7 @@"type": "boolean"},"investigation_inconclusive_action": {- "description": "How to handle an 'inconclusive' verdict when notifications are gated. 'notify' is the safe default — an agent that can't be sure is itself useful signal.\n\n* `notify` - Notify\n* `suppress` - Suppress",+ "description": "How to handle an 'inconclusive' verdict: whether gated notifications fire and whether the investigation surfaces in the Signals inbox. 'notify' is the safe default — an agent that can't be sure is itself useful signal. False positives never reach the inbox regardless of this setting.\n\n* `notify` - Notify\n* `suppress` - Suppress","enum": ["notify", "suppress"],"type": "string"},