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.

feat(alerts): show full investigation summaries in history and email (#126371)

Matt Puada8d3939bcb2
diff --git a/products/subscriptions/backend/logic/email_formatting.py b/posthog/helpers/email_formatting.py
similarity index 100%
rename from products/subscriptions/backend/logic/email_formatting.py
rename to posthog/helpers/email_formatting.py
diff --git a/posthog/tasks/alerts/test/test_investigation_summary.py b/posthog/tasks/alerts/test/test_investigation_summary.py
new file mode 100644
index 000000000000..8d3da1915075
--- /dev/null
+++ b/posthog/tasks/alerts/test/test_investigation_summary.py
@@ -0,0 +1,56 @@
+import pytest
+from unittest.mock import MagicMock
+
+from posthog.tasks.alerts.utils import (
+ INVESTIGATION_SUMMARY_TEASER_MAX_CHARS,
+ chat_investigation_summary,
+ investigation_summary_teaser,
+)
+
+from products.alerts.backend.models import AlertConfiguration
+
+
+ "input,expected",
+ [
+ (None, None),
+ ("", None),
+ (" ", None),
+ ("Traffic doubled after a marketing campaign launch.", "Traffic doubled after a marketing campaign launch."),
+ (" hello ", "hello."),
+ (
+ "- `signup_count` rose to **240** at _10:00 UTC_\n- Most new users came from [the docs](https://example.com)",
+ "signup_count rose to 240 at 10:00 UTC. Most new users came from the docs.",
+ ),
+ ("Latency rose for <none> service.", "Latency rose for <none> service."),
+ ],
+)
+def test_investigation_summary_teaser_basic(input: str | None, expected: str | None) -> None:
+ assert investigation_summary_teaser(input) == expected
+
+
+def test_investigation_summary_teaser_clips_long_text_with_ellipsis() -> None:
+ text = "x" * (INVESTIGATION_SUMMARY_TEASER_MAX_CHARS + 50)
+ clipped = investigation_summary_teaser(text)
+ assert clipped is not None
+ assert len(clipped) == INVESTIGATION_SUMMARY_TEASER_MAX_CHARS
+ assert clipped.endswith("…")
+
+
+def test_investigation_summary_teaser_clips_on_sentence_boundary() -> None:
+ text = "All systems nominal here. " * 40 # > the cap, sentence ends past the midpoint
+ clipped = investigation_summary_teaser(text)
+ assert clipped is not None
+ # Clips on a full sentence (not mid-word) and always signals truncation with an ellipsis.
+ assert clipped.endswith("…")
+ body = clipped.removesuffix(" …")
+ assert body.endswith(".")
+ assert text.startswith(body)
+
+
+def test_chat_investigation_summary_keeps_metrics_summaries_whole() -> None:
+ alert = MagicMock(spec=AlertConfiguration)
+ alert.detector_config = None
+ summary = "http_requests_total *rose* on <none> " + "x " * 400
+
+ assert chat_investigation_summary(alert, summary) == summary
diff --git a/posthog/tasks/alerts/utils.py b/posthog/tasks/alerts/utils.py
index 854eeaef98b5..56e99991dc13 100644
--- a/posthog/tasks/alerts/utils.py
+++ b/posthog/tasks/alerts/utils.py
@@ -1,3 +1,4 @@
+import re
from collections.abc import Collection
from contextlib import ExitStack
from datetime import UTC, datetime, timedelta
@@ -6,13 +7,16 @@ from zoneinfo import ZoneInfo
from django.db.models import Q
from django.utils import timezone
+from django.utils.html import format_html
import pytz
import structlog
+from markdown_it import MarkdownIt
from posthog.schema import AlertCalculationInterval, AlertState, ChartDisplayType, NodeKind, TrendsQuery
from posthog.dataclasses import frozen
+from posthog.helpers.email_formatting import render_ai_email_html
from posthog.models.user import User
from posthog.ph_client import ph_background_capture
from posthog.slo.context import get_current_slo
@@ -54,6 +58,14 @@ logger = structlog.get_logger(__name__)
INSIGHT_ALERT_FIRING_EVENT = "$insight_alert_firing"
+INVESTIGATION_SUMMARY_TEASER_MAX_CHARS = 500
+
+# Matches a sentence-ending punctuation mark followed by whitespace or end-of-string,
+# used to clip the summary teaser on a sentence boundary instead of mid-word.
+_SENTENCE_END_RE = re.compile(r"[.!?](?=\s|$)")
+
+_SUMMARY_MARKDOWN = MarkdownIt("commonmark")
+
# How long the insight stack keeps evaluating an alert after the platform takes it over, so the two
# stacks' checks can be compared. It sends nothing for the alert in that time.
PLATFORM_SHADOW = timedelta(days=7)
@@ -260,11 +272,77 @@ def trigger_alert_hog_functions(alert: AlertConfiguration, properties: dict) ->
return receipts
+@frozen
+class InvestigationSummaryLine:
+ """The entry of `breaches` that holds the investigation summary.
+
+ `breaches` carries the plain-text teaser, which chat destinations show as is. Email
+ renders `markdown` in place of that entry.
+ """
+
+ index: int
+ markdown: str
+
+
+def _clip_summary(text: str) -> str:
+ if len(text) <= INVESTIGATION_SUMMARY_TEASER_MAX_CHARS:
+ return text
+ window = text[:INVESTIGATION_SUMMARY_TEASER_MAX_CHARS]
+ # Prefer clipping at the last complete sentence, but only if it keeps enough of the
+ # teaser — otherwise a short lead sentence would drop most of the summary.
+ sentence_ends = [match.end() for match in _SENTENCE_END_RE.finditer(window)]
+ if sentence_ends and sentence_ends[-1] >= INVESTIGATION_SUMMARY_TEASER_MAX_CHARS // 2:
+ # Always append the ellipsis: the boundary might be an abbreviation (e.g. "U.S."),
+ # not a real sentence end, so a clean-looking stop would hide that we dropped the rest.
+ return window[: sentence_ends[-1]].rstrip() + " …"
+ # No usable sentence boundary — fall back to the last word boundary with an ellipsis.
+ word_end = window.rstrip().rfind(" ")
+ if word_end != -1:
+ return window[:word_end].rstrip() + "…"
+ return text[: INVESTIGATION_SUMMARY_TEASER_MAX_CHARS - 1].rstrip() + "…"
+
+
+def investigation_summary_teaser(summary: str | None) -> str | None:
+ """The Markdown investigation summary as short plain text, for chat destinations.
+
+ Email and the alert history render the full Markdown. Chat destinations show the text
+ as is, so they get the words without markup, clamped like a teaser. The notebook has
+ the full write-up.
+ """
+ if not summary or not summary.strip():
+ return None
+ sentences: list[str] = []
+ for token in _SUMMARY_MARKDOWN.parse(summary):
+ if token.type != "inline":
+ continue
+ words = "".join(
+ child.content if child.type in ("text", "code_inline", "html_inline") else " "
+ for child in token.children or []
+ if child.type in ("text", "code_inline", "html_inline", "softbreak", "hardbreak")
+ )
+ sentence = " ".join(words.split())
+ if sentence:
+ sentences.append(sentence if sentence[-1] in ".!?:;" else f"{sentence}.")
+ if not sentences:
+ return None
+ return _clip_summary(" ".join(sentences))
+
+
+def chat_investigation_summary(alert: AlertConfiguration, summary: str | None) -> str | None:
+ """The stored investigation summary in the form chat destinations show.
+
+ Only the anomaly investigation agent writes Markdown. The metrics alert investigation
+ stores plain text, which chat shows as is.
+ """
+ return investigation_summary_teaser(summary) if alert.detector_config else summary
+
+
def send_notifications_for_breaches(
alert: AlertConfiguration,
breaches: list[str],
idempotency_key: str,
extra_properties: dict[str, Any] | None = None,
+ investigation_summary: InvestigationSummaryLine | None = None,
) -> list[AlertDelivery]:
"""A stable idempotency_key (typically alert_check.id) lets MessagingRecord enforce
per-recipient at-most-once email delivery on retries.
@@ -280,6 +358,14 @@ def send_notifications_for_breaches(
insight_url = f"/project/{alert.team.pk}/insights/{alert.insight.short_id}"
alert_url = f"{insight_url}?alert_id={alert.id}"
logger.info("send_notifications_for_breaches", alert_id=alert.id, anomaly_count=len(breaches))
+ match_descriptions_html: list[str] = []
+ if investigation_summary is not None:
+ match_descriptions_html = [
+ render_ai_email_html(investigation_summary.markdown)
+ if index == investigation_summary.index
+ else format_html("<p>{}</p>", breach)
+ for index, breach in enumerate(breaches)
+ ]
email_sent = send_alert_email(
recipients=email_targets,
campaign_key=campaign_key,
@@ -287,6 +373,7 @@ def send_notifications_for_breaches(
template_name="alert_check_firing",
template_context={
"match_descriptions": breaches,
+ "match_descriptions_html": match_descriptions_html,
"insight_url": insight_url,
"insight_name": alert.insight.name,
"alert_url": alert_url,
@@ -540,6 +627,7 @@ def dispatch_alert_notification(
extra_properties: dict[str, Any] | None = None,
idempotency_key: str | None = None,
render_chart: bool = True,
+ investigation_summary: InvestigationSummaryLine | None = None,
) -> list[AlertDelivery] | None:
"""Route an AlertCheck to the correct notification sender.
@@ -655,7 +743,9 @@ def dispatch_alert_notification(
.order_by("tag__name")[:10]
),
metric_name=alert.insight.name or alert.insight.derived_name or "Insight",
- summary=properties.get("investigation_summary") or alert_check.investigation_summary,
+ summary=chat_investigation_summary(
+ alert, properties.get("investigation_summary") or alert_check.investigation_summary
+ ),
verdict=properties.get("investigation_verdict") or alert_check.investigation_verdict,
investigation_status=alert_check.investigation_status,
detected_at=detected_at,
@@ -677,13 +767,13 @@ def dispatch_alert_notification(
chart_url = prepare_alert_insight_chart_url(alert=alert, alert_check=alert_check)
if chart_url:
properties["insight_chart_url"] = chart_url
- # Only forward extra_properties when there's something to add, keeping the
- # common threshold-alert call unchanged when nothing was attached.
+ # Only forward what is set, keeping the common threshold-alert call unchanged.
+ optional_kwargs: dict[str, Any] = {}
if properties:
- return send_notifications_for_breaches(
- alert, breaches, idempotency_key=key, extra_properties=properties
- )
- return send_notifications_for_breaches(alert, breaches, idempotency_key=key)
+ optional_kwargs["extra_properties"] = properties
+ if investigation_summary is not None:
+ optional_kwargs["investigation_summary"] = investigation_summary
+ return send_notifications_for_breaches(alert, breaches, idempotency_key=key, **optional_kwargs)
case _:
raise AssertionError(f"dispatch_alert_notification: unhandled alert state: {alert_check.state}")
diff --git a/posthog/templates/email/alert_check_firing.html b/posthog/templates/email/alert_check_firing.html
index 8d569b6fdefd..cf7dac76be10 100644
--- a/posthog/templates/email/alert_check_firing.html
+++ b/posthog/templates/email/alert_check_firing.html
@@ -7,11 +7,17 @@
The <a href="{% absolute_uri alert_url %}">{{ alert_name }}</a> alert is firing for <a href="{% absolute_uri insight_url %}">{{ insight_name }}</a> in project <strong>{{ project_name }}</strong>:
{% endif %}
</p>
- <ul>
- {% for item in match_descriptions %}
- <li>{{ item }}</li>
+ {% if match_descriptions_html %}
+ {% for item in match_descriptions_html %}
+ <div>{{ item }}</div>
{% endfor %}
- </ul>
+ {% else %}
+ <ul>
+ {% for item in match_descriptions %}
+ <li>{{ item }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
<div class="mb mt text-center">
<a class="button" href="{% absolute_uri alert_url %}">Manage alert</a>
</div>
diff --git a/posthog/temporal/ai/anomaly_investigation/notebook.py b/posthog/temporal/ai/anomaly_investigation/notebook.py
index fe7bcbc25a31..1b26cc67fb3f 100644
--- a/posthog/temporal/ai/anomaly_investigation/notebook.py
+++ b/posthog/temporal/ai/anomaly_investigation/notebook.py
@@ -2,6 +2,9 @@ import json
import uuid
from dataclasses import dataclass
+from markdown_it import MarkdownIt
+from markdown_it.token import Token
+
from posthog.temporal.ai.anomaly_investigation.report import InvestigationReport
from products.alerts.backend.models.alert import AlertCheck, AlertConfiguration
@@ -15,6 +18,8 @@ from products.notebooks.backend.facade.content import (
)
from products.product_analytics.backend.facade.models import Insight
+_SUMMARY_MARKDOWN = MarkdownIt("commonmark")
+
VERDICT_LABEL = {
"true_positive": "True positive",
"false_positive": "False positive",
@@ -43,7 +48,8 @@ def build_investigation_markdown(ctx: NotebookRenderContext) -> str:
content.append(create_heading_with_text("Verdict", level=2))
verdict_text = VERDICT_LABEL.get(ctx.report.verdict, ctx.report.verdict)
- content.append(create_paragraph_with_text(f"{verdict_text} — {ctx.report.summary}"))
+ content.append(create_paragraph_with_text(verdict_text))
+ content.extend(_summary_content(ctx.report.summary))
metric_meaning = ctx.report.metric_meaning.strip()
if metric_meaning:
@@ -77,6 +83,43 @@ def build_investigation_markdown(ctx: NotebookRenderContext) -> str:
return convert_notebook_content_to_markdown({"type": "doc", "content": content})
+def _summary_content(summary: str) -> TipTapContent:
+ """Rebuild the agent's Markdown summary as plain paragraphs and bullet lists.
+
+ The summary is model output, so it becomes escaped plain text. Inline formatting is dropped:
+ the notebook serializer cannot keep a code span closed when the code holds a backtick.
+ """
+ content: TipTapContent = []
+ items: list[str] = []
+ list_depth = 0
+ for token in _SUMMARY_MARKDOWN.parse(summary):
+ if token.type in ("bullet_list_open", "ordered_list_open"):
+ list_depth += 1
+ elif token.type in ("bullet_list_close", "ordered_list_close"):
+ list_depth -= 1
+ if list_depth == 0 and items:
+ content.append(create_bullet_list(items))
+ items = []
+ elif token.type == "inline":
+ text = _plain_text(token.children or [])
+ if not text:
+ continue
+ if list_depth:
+ items.append(text)
+ else:
+ content.append(create_paragraph_with_text(text))
+ return content
+
+
+def _plain_text(children: list[Token]) -> str:
+ words = "".join(
+ child.content if child.type in ("text", "code_inline", "html_inline") else " "
+ for child in children
+ if child.type in ("text", "code_inline", "html_inline", "softbreak", "hardbreak")
+ )
+ return " ".join(words.split())
+
+
def _summary_line(ctx: NotebookRenderContext) -> str:
detector_type = (ctx.alert.detector_config or {}).get("type") or "threshold"
triggered_dates = ctx.alert_check.triggered_dates or []
diff --git a/posthog/temporal/ai/anomaly_investigation/prompts.py b/posthog/temporal/ai/anomaly_investigation/prompts.py
index b341d218c0e6..058addb71a72 100644
--- a/posthog/temporal/ai/anomaly_investigation/prompts.py
+++ b/posthog/temporal/ai/anomaly_investigation/prompts.py
@@ -35,7 +35,7 @@ Final JSON schema (emit exactly these keys):
{
"verdict": "true_positive" | "false_positive" | "inconclusive",
"metric_meaning": "One sentence: what the alerted number counts, read off the metric definition.",
- "summary": "1-3 sentence plain-English summary of what happened.",
+ "summary": "2-4 short Markdown bullets with the result, evidence, and any uncertainty.",
"hypotheses": [
{
"title": "Short name of the hypothesis.",
@@ -142,6 +142,15 @@ Guidelines:
inferred from the metric's name sends them to inspect infrastructure that may
not be there, which costs more than saying you don't know. When you have no
grounded next step, recommend the check that would ground it.
+- Write the summary as 2-4 short Markdown bullets. Lead with what changed, then
+ give the evidence and any uncertainty. Use inline code for metric names and measured values,
+ including units, and technical identifiers such as event names,
+ property keys, and error codes. Italicize dates, times, durations, and comparison windows.
+ Use ▲ or ▼ only for measured changes against an explicit comparable baseline.
+ Keep absolute values and changes distinct. Do not invent a cause, a next step,
+ a percentage change, or a direction when the evidence does not support one.
+ Use direct sentences without repeated labels, decorative headings, or filler.
+ Do not repeat the verdict, alert name, or notebook URL in the summary.
- Keep summaries concrete and short. No filler. No apologies. No hedging
beyond what the data supports. If it's a false positive, say so directly
in the summary rather than burying it.
diff --git a/posthog/temporal/ai/anomaly_investigation/report.py b/posthog/temporal/ai/anomaly_investigation/report.py
index 4d3f194dcf89..7bba9ed6ee62 100644
--- a/posthog/temporal/ai/anomaly_investigation/report.py
+++ b/posthog/temporal/ai/anomaly_investigation/report.py
@@ -159,7 +159,9 @@ class InvestigationReport(BaseModel):
"reasoning about causes, and never infer it from the insight's name."
),
)
- summary: str = Field(description="1-3 sentence plain-English summary of what happened.")
+ summary: str = Field(
+ description="2-4 short Markdown bullets. Use inline code for metric names and values; italicize times. Preserve evidence and uncertainty."
+ )
hypotheses: list[InvestigationHypothesis] = Field(
default_factory=list,
description="Ordered by likelihood; top 2-3 hypotheses.",
diff --git a/posthog/temporal/ai/anomaly_investigation/workflow.py b/posthog/temporal/ai/anomaly_investigation/workflow.py
index 7f33f208f7aa..96f4cb2633a4 100644
--- a/posthog/temporal/ai/anomaly_investigation/workflow.py
+++ b/posthog/temporal/ai/anomaly_investigation/workflow.py
@@ -6,7 +6,6 @@ Triggered from posthog/temporal/alerts/workflows.py for a firing check the budge
from __future__ import annotations
-import re
import json
import asyncio
from dataclasses import dataclass
@@ -21,13 +20,16 @@ from asgiref.sync import sync_to_async
from temporalio import activity, workflow
from temporalio.common import RetryPolicy
+from posthog.dataclasses import frozen
from posthog.models import Team, User
from posthog.tasks.alerts.charts import png_to_b64, render_series_chart
from posthog.tasks.alerts.metric_definition import describe_metric_definition
from posthog.tasks.alerts.utils import (
+ InvestigationSummaryLine,
_inconclusive_is_suppressed,
_should_suppress_notification,
dispatch_alert_notification,
+ investigation_summary_teaser,
prepare_alert_insight_chart_url,
record_alert_delivery,
)
@@ -70,8 +72,6 @@ ANOMALY_INVESTIGATION_ACTIVITY_START_TO_CLOSE = 40 * 60 # 40 minutes
ANOMALY_INVESTIGATION_ACTIVITY_HEARTBEAT_TIMEOUT = 5 * 60 # 5 minutes
ANOMALY_INVESTIGATION_ACTIVITY_MAX_ATTEMPTS = 2
-MAX_SUMMARY_CHARS = 500
-
_VERDICT_LABELS = {
"true_positive": "True positive",
"false_positive": "False positive",
@@ -86,10 +86,6 @@ _VERDICT_CHANGE_FOLLOWUP_KEY = "investigation_verdict_change"
# rejected and silently dropped.
_MAX_DESCRIPTION_CHARS = 3000
-# Matches a sentence-ending punctuation mark followed by whitespace or end-of-string,
-# used to clip the summary teaser on a sentence boundary instead of mid-word.
-_SENTENCE_END_RE = re.compile(r"[.!?](?=\s|$)")
-
@dataclass
class AnomalyInvestigationWorkflowInputs:
@@ -236,12 +232,12 @@ async def investigate_anomaly_activity(inputs: AnomalyInvestigationWorkflowInput
# last verdict of an earlier check in the same firing episode.
episode = await sync_to_async(episode_investigations, thread_sensitive=False)(alert, alert_check)
- summary_for_list = _truncate_summary(result.report.summary)
+ summary = result.report.summary.strip()
await sync_to_async(AlertCheck.objects.filter(id=alert_check.id).update, thread_sensitive=False)(
investigation_notebook_id=notebook.id,
investigation_status=InvestigationStatus.DONE,
investigation_verdict=result.report.verdict,
- investigation_summary=summary_for_list,
+ investigation_summary=summary or None,
investigation_error=None,
)
@@ -258,7 +254,7 @@ async def investigate_anomaly_activity(inputs: AnomalyInvestigationWorkflowInput
alert_check=alert_check,
verdict=result.report.verdict,
previous_verdict=episode.previous_verdict,
- summary=summary_for_list or "",
+ summary=summary,
notebook_short_id=notebook.short_id,
insight_chart_url=insight_chart_url,
)
@@ -504,7 +500,7 @@ def _deliver_investigation_outcome(
)
return
- breaches = _build_breach_descriptions(
+ descriptions = _build_breach_descriptions(
alert_check=check,
verdict=verdict,
previous_verdict=previous_verdict,
@@ -524,7 +520,12 @@ def _deliver_investigation_outcome(
# render_chart=False: the chart was already rendered above, before this
# transaction took its row lock.
deliveries = dispatch_alert_notification(
- alert, check, breaches, extra_properties=extra_properties or None, render_chart=False
+ alert,
+ check,
+ descriptions.lines,
+ extra_properties=extra_properties or None,
+ render_chart=False,
+ investigation_summary=descriptions.summary,
)
record_alert_delivery(alert, check, deliveries, stamp_on_empty=True)
except Exception:
@@ -569,7 +570,7 @@ def _dispatch_verdict_change_followup(
if receipts.get(_VERDICT_CHANGE_FOLLOWUP_KEY):
return
- breaches = _build_breach_descriptions(
+ descriptions = _build_breach_descriptions(
alert_check=check,
verdict=verdict,
previous_verdict=previous_verdict,
@@ -590,9 +591,10 @@ def _dispatch_verdict_change_followup(
deliveries = dispatch_alert_notification(
alert,
check,
- breaches,
+ descriptions.lines,
extra_properties=extra_properties,
idempotency_key=f"{check.id}:investigation-verdict-change",
+ investigation_summary=descriptions.summary,
# The caller holds a row lock, and this check was already notified, so
# prepare_alert_insight_chart_url would return None anyway.
render_chart=False,
@@ -632,6 +634,12 @@ def _dispatch_verdict_change_followup(
)
+@frozen
+class _BreachDescriptions:
+ lines: list[str]
+ summary: InvestigationSummaryLine | None
+
+
def _build_breach_descriptions(
*,
alert_check,
@@ -639,10 +647,12 @@ def _build_breach_descriptions(
previous_verdict: str | None,
summary: str,
notebook_short_id: str | None,
-) -> list[str]:
+) -> _BreachDescriptions:
"""Compose the strings that populate the `match_descriptions` list in the
existing alert email template. Keeps the current template working while
giving gated notifications richer body content.
+
+ The summary line holds a plain-text teaser. Email renders the full Markdown in its place.
"""
lines: list[str] = []
if previous_verdict and verdict and verdict != previous_verdict:
@@ -664,41 +674,15 @@ def _build_breach_descriptions(
verdict_label = _VERDICT_LABELS.get(verdict or "", "")
if verdict_label:
lines.append(f"Investigation verdict: {verdict_label}.")
- if summary:
- lines.append(summary)
+ summary_line = None
+ teaser = investigation_summary_teaser(summary)
+ if teaser:
+ summary_line = InvestigationSummaryLine(index=len(lines), markdown=summary)
+ lines.append(teaser)
if notebook_short_id:
notebook_url = absolute_uri(f"/notebooks/{notebook_short_id}")
lines.append(f"See {notebook_url} for the full investigation.")
- return lines
-
-
-def _truncate_summary(summary: str | None) -> str | None:
- """Clamp the agent's summary for list rendering and email/Slack follow-ups.
-
- The full write-up already lives in the notebook — this field is just a teaser.
- """
- if not summary:
- return None
- trimmed = summary.strip()
- if not trimmed:
- return None
- if len(trimmed) <= MAX_SUMMARY_CHARS:
- return trimmed
-
- window = trimmed[:MAX_SUMMARY_CHARS]
- # Prefer clipping at the last complete sentence, but only if it keeps enough of the
- # teaser — otherwise a short lead sentence would drop most of the summary.
- sentence_ends = [match.end() for match in _SENTENCE_END_RE.finditer(window)]
- if sentence_ends and sentence_ends[-1] >= MAX_SUMMARY_CHARS // 2:
- # Always append the ellipsis: the boundary might be an abbreviation (e.g. "U.S."),
- # not a real sentence end, so a clean-looking stop would hide that we dropped the rest.
- return window[: sentence_ends[-1]].rstrip() + " …"
-
- # No usable sentence boundary — fall back to the last word boundary with an ellipsis.
- word_end = window.rstrip().rfind(" ")
- if word_end != -1:
- return window[:word_end].rstrip() + "…"
- return trimmed[: MAX_SUMMARY_CHARS - 1].rstrip() + "…"
+ return _BreachDescriptions(lines=lines, summary=summary_line)
async def _update_status(alert_check, status: str) -> None:
diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_notebook.py b/posthog/temporal/tests/ai/test_anomaly_investigation_notebook.py
index d131d0217340..551861ea635d 100644
--- a/posthog/temporal/tests/ai/test_anomaly_investigation_notebook.py
+++ b/posthog/temporal/tests/ai/test_anomaly_investigation_notebook.py
@@ -1,9 +1,17 @@
+import pytest
from posthog.test.base import BaseTest
-from posthog.temporal.ai.anomaly_investigation.notebook import NotebookRenderContext, build_investigation_markdown
+from markdown_it import MarkdownIt
+
+from posthog.temporal.ai.anomaly_investigation.notebook import (
+ NotebookRenderContext,
+ _summary_content,
+ build_investigation_markdown,
+)
from posthog.temporal.ai.anomaly_investigation.report import InvestigationHypothesis, InvestigationReport
from products.alerts.backend.models.alert import AlertCheck, AlertConfiguration
+from products.notebooks.backend.facade.content import convert_notebook_content_to_markdown
from products.product_analytics.backend.facade.models import Insight
@@ -28,7 +36,7 @@ class TestAnomalyInvestigationNotebook(BaseTest):
report = InvestigationReport(
verdict="true_positive",
metric_meaning="Unique users who loaded a page under /pricing each day.",
- summary="Traffic doubled after a marketing campaign launch.",
+ summary="- **Traffic doubled** after the `spring_launch` campaign.\n- Check [the report](https://example.com) with _marketing_.",
hypotheses=[
InvestigationHypothesis(
title="Marketing launch",
@@ -51,6 +59,10 @@ class TestAnomalyInvestigationNotebook(BaseTest):
assert "## Recommendations" in markdown
assert "### Run details" in markdown
assert "- Campaign ID 42 launched at 2024-06-01 08:00" in markdown
+ # The summary keeps its bullets as plain text, and its link stays text.
+ assert "- Traffic doubled after the spring_launch campaign." in markdown
+ assert "- Check the report with marketing." in markdown
+ assert "example.com" not in markdown
# The source insight stays live through a SavedInsightNode Query component.
assert '<Query title="pageviews/day"' in markdown
@@ -70,3 +82,16 @@ class TestAnomalyInvestigationNotebook(BaseTest):
assert "## Recommendations" not in markdown
assert "## What this metric measures" not in markdown
assert "## Verdict" in markdown
+
+
+ "summary",
+ [
+ "See [the report](https://evil.example/collect) now.",
+ "See ``x` [click](https://evil.example/collect) `y`` now.",
+ ],
+)
+def test_summary_links_never_render_as_links(summary: str) -> None:
+ markdown = convert_notebook_content_to_markdown({"type": "doc", "content": _summary_content(summary)})
+
+ assert "<a " not in MarkdownIt("commonmark").render(markdown)
diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_summary.py b/posthog/temporal/tests/ai/test_anomaly_investigation_summary.py
deleted file mode 100644
index d57205daa23d..000000000000
--- a/posthog/temporal/tests/ai/test_anomaly_investigation_summary.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import pytest
-
-from posthog.temporal.ai.anomaly_investigation.workflow import MAX_SUMMARY_CHARS, _truncate_summary
-
-
- "input,expected",
- [
- (None, None),
- ("", None),
- (" ", None),
- ("Traffic doubled after a marketing campaign launch.", "Traffic doubled after a marketing campaign launch."),
- (" hello ", "hello"),
- ],
-)
-def test_truncate_summary_basic(input, expected) -> None:
- assert _truncate_summary(input) == expected
-
-
-def test_truncate_summary_clips_long_text_with_ellipsis() -> None:
- text = "x" * (MAX_SUMMARY_CHARS + 50)
- clipped = _truncate_summary(text)
- assert clipped is not None
- assert len(clipped) == MAX_SUMMARY_CHARS
- assert clipped.endswith("…")
-
-
-def test_truncate_summary_clips_on_sentence_boundary() -> None:
- text = "All systems nominal here. " * 40 # > MAX_SUMMARY_CHARS, sentence ends past the midpoint
- clipped = _truncate_summary(text)
- assert clipped is not None
- # Clips on a full sentence (not mid-word) and always signals truncation with an ellipsis.
- assert clipped.endswith("…")
- body = clipped.removesuffix(" …")
- assert body.endswith(".")
- assert text.startswith(body)
diff --git a/posthog/temporal/tests/ai/test_anomaly_investigation_verdict.py b/posthog/temporal/tests/ai/test_anomaly_investigation_verdict.py
index e6f4b89ac57d..a049e0ebf34a 100644
--- a/posthog/temporal/tests/ai/test_anomaly_investigation_verdict.py
+++ b/posthog/temporal/tests/ai/test_anomaly_investigation_verdict.py
@@ -77,10 +77,14 @@ class TestInvestigationVerdictPersistence(NonAtomicBaseTest):
@patch("temporalio.activity.info")
async def test_true_positive_verdict_is_persisted(self, mock_info, _heartbeat, mock_run, _prepare) -> None:
mock_info.return_value.heartbeat_timeout = None
+ # Longer than the chat teaser, so a clip on the stored value would fail the summary assertion.
+ summary = (
+ "- **Confirmed spike** caused by campaign launch.\n" + "- Traffic came from the `pricing` page.\n" * 20
+ )
mock_run.return_value = InvestigationRunResult(
report=InvestigationReport(
verdict="true_positive",
- summary="Confirmed spike caused by campaign launch.",
+ summary=summary,
hypotheses=[],
recommendations=[],
),
@@ -100,7 +104,7 @@ class TestInvestigationVerdictPersistence(NonAtomicBaseTest):
await sync_to_async(self.alert_check.refresh_from_db)()
assert self.alert_check.investigation_status == InvestigationStatus.DONE
assert self.alert_check.investigation_verdict == InvestigationVerdict.TRUE_POSITIVE
- assert self.alert_check.investigation_summary == "Confirmed spike caused by campaign launch."
+ assert self.alert_check.investigation_summary == summary.strip()
assert self.alert_check.investigation_notebook_id is not None
# A follow-up agent appends through the MCP cell tools, which only accept a markdown notebook.
diff --git a/products/alerts/backend/test/test_email_notifications.py b/products/alerts/backend/test/test_email_notifications.py
index ff559331cbec..98fbf35e9a07 100644
--- a/products/alerts/backend/test/test_email_notifications.py
+++ b/products/alerts/backend/test/test_email_notifications.py
@@ -2,7 +2,33 @@ from unittest.mock import MagicMock, call, patch
from django.template.loader import render_to_string
+from posthog.tasks.alerts.utils import InvestigationSummaryLine, send_notifications_for_breaches
+
from products.alerts.backend.facade.email import send_alert_email
+from products.alerts.backend.models import AlertConfiguration
+
+
+def test_investigation_summary_renders_markdown_in_email_and_plain_text_in_destinations() -> None:
+ alert = MagicMock(spec=AlertConfiguration)
+ alert.get_subscribed_users_emails.return_value = ["[email protected]"]
+ markdown = "- `signup_count` reached **40**"
+ breaches = ["Anomaly in <script>series</script>.", "signup_count reached 40.", "See the notebook."]
+
+ with (
+ patch("posthog.tasks.alerts.utils.send_alert_email") as email,
+ patch("posthog.tasks.alerts.utils.trigger_alert_hog_functions", return_value=[]) as destinations,
+ ):
+ send_notifications_for_breaches(
+ alert,
+ breaches,
+ "example-check",
+ investigation_summary=InvestigationSummaryLine(index=1, markdown=markdown),
+ )
+
+ html = render_to_string("email/alert_check_firing.html", email.call_args.kwargs["template_context"])
+ assert "<strong>40</strong>" in html
+ assert "<p>Anomaly in &lt;script&gt;series&lt;/script&gt;.</p>" in html
+ assert destinations.call_args.kwargs["properties"]["breaches"] == "\n".join(breaches)
@patch("products.alerts.backend.facade.email.is_email_available", return_value=True)
diff --git a/products/alerts/frontend/logic/alertLogic.ts b/products/alerts/frontend/logic/alertLogic.ts
index 3f461adfcbd6..e40bea6a1966 100644
--- a/products/alerts/frontend/logic/alertLogic.ts
+++ b/products/alerts/frontend/logic/alertLogic.ts
@@ -110,6 +110,7 @@ export interface alertLogicValues {
alertLoading: boolean
checksHistoryParams: ChecksHistoryParams
checksHistoryTablePage: number
+ expandedInvestigationChecks: Record<string, boolean>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -151,6 +152,9 @@ export interface alertLogicActions {
setChecksHistoryTablePage: (page: number) => {
page: number
}
+ toggleInvestigationSummary: (checkId: string) => {
+ checkId: string
+ }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -178,6 +182,7 @@ export const alertLogic = kea<alertLogicType>([
key(({ alertId }) => alertId ?? 'new'),
actions({
+ toggleInvestigationSummary: (checkId: string) => ({ checkId }),
setChecksHistoryParams: (limit: number, offset: number) => ({ limit, offset }),
// Low-level reducer setter — updates `alertHistoryView` only. Prefer `selectAlertHistoryView`
// from UI code so pagination + checks fetch are reset in the same flow.
@@ -190,6 +195,12 @@ export const alertLogic = kea<alertLogicType>([
}),
reducers(() => ({
+ expandedInvestigationChecks: [
+ {} as Record<string, boolean>,
+ {
+ toggleInvestigationSummary: (state, { checkId }) => ({ ...state, [checkId]: !state[checkId] }),
+ },
+ ],
checksHistoryParams: [
initialChecksHistoryParams(),
{
diff --git a/products/alerts/frontend/views/AlertHistorySection.tsx b/products/alerts/frontend/views/AlertHistorySection.tsx
index eede9b22d4c6..fdb64062ba47 100644
--- a/products/alerts/frontend/views/AlertHistorySection.tsx
+++ b/products/alerts/frontend/views/AlertHistorySection.tsx
@@ -1,8 +1,9 @@
import { useActions, useValues } from 'kea'
-import { useMemo } from 'react'
+import { useLayoutEffect, useMemo, useRef, useState } from 'react'
import { IconGraph, IconNotebook } from '@posthog/icons'
import {
+ LemonButton,
LemonSegmentedButton,
LemonSkeleton,
LemonTable,
@@ -18,6 +19,7 @@ import { EmptyMessage } from 'lib/components/EmptyMessage/EmptyMessage'
import { TZLabel } from 'lib/components/TZLabel'
import { dayjs } from 'lib/dayjs'
import { IconOpenInNew } from 'lib/lemon-ui/icons'
+import { LemonMarkdown } from 'lib/lemon-ui/LemonMarkdown'
import { formatDate } from 'lib/utils/datetime'
import { humanFriendlyNumber } from 'lib/utils/numbers'
import { capitalizeFirstLetter } from 'lib/utils/strings'
@@ -50,13 +52,31 @@ const VERDICT_CONFIG: Record<InvestigationVerdict, { label: string; className: s
},
}
-function InvestigationCell({ check }: { check: AlertCheck }): JSX.Element {
+function InvestigationCell({ check, alertId }: { check: AlertCheck; alertId: AlertType['id'] }): JSX.Element {
+ const logic = alertLogic({ alertId })
+ const { expandedInvestigationChecks } = useValues(logic)
+ const { toggleInvestigationSummary } = useActions(logic)
+ const expanded = !!expandedInvestigationChecks[check.id]
+ const summaryRef = useRef<HTMLDivElement>(null)
+ const [isClamped, setIsClamped] = useState(false)
const status = check.investigation_status
const shortId = check.investigation_notebook_short_id
const summary = check.investigation_summary?.trim() || null
const verdict = check.investigation_verdict ?? null
const suppressed = !!check.notification_suppressed_by_agent
+ useLayoutEffect(() => {
+ const element = summaryRef.current
+ if (!element || expanded) {
+ return
+ }
+ const measure = (): void => setIsClamped(element.scrollHeight > element.clientHeight)
+ measure()
+ const observer = new ResizeObserver(measure)
+ observer.observe(element)
+ return () => observer.disconnect()
+ }, [summary, expanded])
+
if (status === 'done') {
const verdictCfg = verdict ? VERDICT_CONFIG[verdict] : null
return (
@@ -68,17 +88,33 @@ function InvestigationCell({ check }: { check: AlertCheck }): JSX.Element {
</LemonTag>
</Tooltip>
)}
- <Tooltip title={summary || undefined}>
- <div className="text-sm leading-normal line-clamp-2 text-muted">
- {verdictCfg && (
- <Tooltip title={verdictCfg.tooltip}>
- <span className={`font-semibold ${verdictCfg.className}`}>{verdictCfg.label}</span>
- </Tooltip>
+ {verdictCfg && (
+ <Tooltip title={verdictCfg.tooltip}>
+ <span className={`font-semibold ${verdictCfg.className}`}>{verdictCfg.label}</span>
+ </Tooltip>
+ )}
+ {summary && (
+ <>
+ <div
+ ref={summaryRef}
+ className={`text-sm leading-normal text-muted ${expanded ? '' : 'line-clamp-2'}`}
+ >
+ <LemonMarkdown disableImages="all" disableLinks disableMentions lowKeyHeadings>
+ {summary}
+ </LemonMarkdown>
+ </div>
+ {(expanded || isClamped) && (
+ <LemonButton
+ type="tertiary"
+ size="xsmall"
+ onClick={() => toggleInvestigationSummary(check.id)}
+ aria-expanded={expanded}
+ >
+ {expanded ? 'Show less' : 'Show more'}
+ </LemonButton>
)}
- {verdictCfg && summary && <span> — </span>}
- {summary && <span>{summary}</span>}
- </div>
- </Tooltip>
+ </>
+ )}
{shortId && (
<Link to={`/notebooks/${shortId}`} className="inline-flex items-center gap-1 text-sm">
<IconNotebook /> View notebook <IconOpenInNew className="text-xs" />
@@ -235,7 +271,7 @@ export function AlertHistorySection({
if (investigationAgentEnabled) {
columns.push({
title: 'Investigation',
- render: (_value, check) => <InvestigationCell check={check} />,
+ render: (_value, check) => <InvestigationCell check={check} alertId={alertId} />,
})
}
if (isAnyRowSqlAlert) {
@@ -295,6 +331,7 @@ export function AlertHistorySection({
})
return columns
}, [
+ alertId,
alertHistoryIsAnomalyDetection,
investigationAgentEnabled,
isAnyRowSqlAlert,
diff --git a/products/subscriptions/backend/logic/email_subscriptions.py b/products/subscriptions/backend/logic/email_subscriptions.py
index 798e0007b1e1..9c4340c370b7 100644
--- a/products/subscriptions/backend/logic/email_subscriptions.py
+++ b/products/subscriptions/backend/logic/email_subscriptions.py
@@ -4,10 +4,10 @@ from typing import Optional
import structlog
from posthog.email import EmailMessage, raise_if_delivery_rejected
+from posthog.helpers.email_formatting import render_ai_email_html
from posthog.utils import absolute_uri
from products.exports.backend.models.exported_asset import ExportedAsset
-from products.subscriptions.backend.logic.email_formatting import render_ai_email_html
from products.subscriptions.backend.logic.subscription_utils import (
MAX_INSIGHTS,
UTM_TAGS_BASE,
diff --git a/products/subscriptions/backend/temporal/ai_subscription/delivery.py b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
index 706771a93802..a84eed8cc25f 100644
--- a/products/subscriptions/backend/temporal/ai_subscription/delivery.py
+++ b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
@@ -15,6 +15,7 @@ from slack_sdk.errors import SlackApiError
from posthog.email import EmailMessage, raise_if_delivery_rejected
from posthog.exceptions_capture import capture_exception
+from posthog.helpers.email_formatting import render_ai_email_html
from posthog.helpers.markdown_safety import is_allowed_link_url, strip_external_links_markdown
from posthog.integrations import IntegrationRecord
from posthog.models import Team, User
@@ -25,7 +26,6 @@ from posthog.utils import absolute_uri
from products.exports.backend.facade.api import get_delivery_image_url
from products.slack_app.backend.facade.api import slack_followup_invite
from products.subscriptions.backend.facade.contracts import PromptRejectedError, SubscriptionTriggerType
-from products.subscriptions.backend.logic.email_formatting import render_ai_email_html
from products.subscriptions.backend.logic.slack_subscriptions import (
UTM_TAGS_BASE,
SlackDeliveryResult,