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(subscriptions): remove repeated titles from AI Slack reports (#123377)

Matt Pua6627de311ac5
diff --git a/products/subscriptions/backend/temporal/ai_subscription/delivery.py b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
index e5be92ddd297..7344a03cc6a1 100644
--- a/products/subscriptions/backend/temporal/ai_subscription/delivery.py
+++ b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
@@ -30,7 +30,6 @@ from products.subscriptions.backend.logic.slack_subscriptions import (
UTM_TAGS_BASE,
SlackDeliveryResult,
SlackMessage,
- _subscription_header_blocks,
_summary_rich_text,
deliver_slack_message_data,
)
@@ -47,7 +46,6 @@ from products.subscriptions.backend.models.subscription import (
AIQueryPlanStatus,
Subscription,
SubscriptionDelivery,
- SubscriptionResource,
get_unsubscribe_token,
)
from products.subscriptions.backend.temporal.ai_subscription.report_pipeline import AiReportResult, generate_ai_report
@@ -598,15 +596,17 @@ def _build_ai_slack_message(
channel = channel_id_from_target(subscription.target_value)
markdown = strip_external_links_markdown(markdown, additional_allowed_origin=absolute_uri("/"))
env: EnvType = {}
- _MARKDOWN_RENDERER.parse(markdown, env)
+ tokens = _MARKDOWN_RENDERER.parse(markdown, env)
+ if tokens and tokens[0].type == "heading_open" and tokens[0].tag == "h1" and tokens[0].map:
+ markdown = "".join(markdown.splitlines(keepends=True)[tokens[0].map[1] :]).lstrip("\r\n")
sections = _safe_slack_markdown_sections(markdown)
title = subscription.title or "Your PostHog AI report"
first_section = sections[0] if sections else "_No report content was generated._"
+ heading = f'Your report "{title}" is ready! 🎉'
blocks: list[dict] = [
- *_subscription_header_blocks(
- subscription, SubscriptionResource("AI prompt", title, subscription.url or ""), False
- ),
+ {"type": "divider"},
+ {"type": "header", "text": {"type": "plain_text", "text": heading[:150]}},
*_slack_report_blocks(first_section, env),
]
for chart in (charts or []) if subscription.includes_delivery_part("include_images") else []:
diff --git a/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py b/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py
index f263ee19a858..8f0d52bf99cf 100644
--- a/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py
+++ b/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py
@@ -424,19 +424,17 @@ class TestChartsOnSlackMessages:
_mock_subscription(), "A short report.", delivery_id=_DELIVERY_ID, charts=[_CHART]
)
- assert [block["type"] for block in message.blocks[:5]] == [
+ assert [block["type"] for block in message.blocks[:4]] == [
"divider",
"header",
- "context",
"markdown",
"image",
]
- assert message.blocks[3]["text"] == "A short report."
- assert message.blocks[1]["text"]["text"] == "Your subscription to Weekly report is ready! 🎉"
- assert message.blocks[2]["elements"][0]["text"] == "🤖 AI prompt: Weekly report"
- assert message.blocks[4]["image_url"] == _CHART["image_url"]
- assert message.blocks[4]["alt_text"] == "signups by day"
- assert message.blocks[4]["title"] == {"type": "plain_text", "text": "signups by day"}
+ assert message.blocks[2]["text"] == "A short report."
+ assert message.blocks[1]["text"]["text"] == 'Your report "Weekly report" is ready! 🎉'
+ assert message.blocks[3]["image_url"] == _CHART["image_url"]
+ assert message.blocks[3]["alt_text"] == "signups by day"
+ assert message.blocks[3]["title"] == {"type": "plain_text", "text": "signups by day"}
def test_an_untitled_chart_posts_no_title_block(self) -> None:
message = _build_ai_slack_message(
@@ -446,8 +444,8 @@ class TestChartsOnSlackMessages:
charts=[{"title": "", "image_url": "https://ph.test/img.png"}],
)
- assert "title" not in message.blocks[4]
- assert message.blocks[4]["alt_text"] == "Chart"
+ assert "title" not in message.blocks[3]
+ assert message.blocks[3]["alt_text"] == "Chart"
def test_a_chartless_report_posts_no_image_blocks(self) -> None:
message = _build_message("A short report.")
@@ -560,7 +558,7 @@ class TestBuildAISlackMessage:
def test_complex_findings_keep_native_markdown(self, content: str) -> None:
markdown = f"## Main result\n\n{content}"
message = _build_message(markdown)
- assert message.blocks[3] == {"type": "markdown", "text": markdown}
+ assert message.blocks[2] == {"type": "markdown", "text": markdown}
def test_single_section_report_has_no_thread_messages(self) -> None:
message = _build_message("A short report.")
@@ -570,18 +568,24 @@ class TestBuildAISlackMessage:
assert all(text.strip() for text in section_texts), "no empty section text allowed"
@pytest.mark.parametrize(
- "markdown",
+ "markdown,expected",
[
- "## Weekly report\n\n| Day | Sessions |\n|---|---|\n| Monday | **42** |",
- "**Bold** and *italic* with [dashboard](https://app.posthog.com/insights/abc)",
- "```\n| Day | Sessions |\n| Monday | 42 |\n```",
- "x" * SLACK_MARKDOWN_MESSAGE_LIMIT,
- "",
+ ("## Weekly report\n\n| Day | Sessions |\n|---|---|\n| Monday | **42** |", None),
+ ("**Bold** and *italic* with [dashboard](https://app.posthog.com/insights/abc)", None),
+ ("```\n| Day | Sessions |\n| Monday | 42 |\n```", None),
+ ("x" * SLACK_MARKDOWN_MESSAGE_LIMIT, None),
+ ("", "_No report content was generated._"),
+ ("# Generated report title\n\n## Adoption\n\nReport content.", "## Adoption\n\nReport content."),
+ ("Generated report title\n=====\n\nReport content.", "Report content."),
+ ("# Generated report title", "_No report content was generated._"),
+ ("```\n# Keep this heading\n```", None),
+ ("# Generated report title\n\n # Keep this code\n value = 1", " # Keep this code\n value = 1"),
],
)
- def test_preserves_report_markdown(self, markdown: str) -> None:
+ def test_preserves_report_markdown(self, markdown: str, expected: str | None) -> None:
message = _build_message(markdown)
- assert message.blocks[3] == {"type": "markdown", "text": markdown or "_No report content was generated._"}
+ assert message.blocks[1]["text"]["text"] == 'Your report "Weekly report" is ready! 🎉'
+ assert message.blocks[2] == {"type": "markdown", "text": markdown if expected is None else expected}
assert message.thread_messages == []
def test_long_report_overflows_into_thread(self) -> None:
@@ -611,8 +615,8 @@ class TestBuildAISlackMessage:
charts=[_CHART],
)
- assert [block["type"] for block in message.blocks] == ["divider", "header", "context", "markdown"]
- assert message.blocks[3]["text"] == "A short report."
+ assert [block["type"] for block in message.blocks] == ["divider", "header", "markdown"]
+ assert message.blocks[2]["text"] == "A short report."
def test_with_images_delivery_keeps_charts_without_posthog_controls(self) -> None:
subscription = _mock_subscription()
@@ -634,11 +638,10 @@ class TestBuildAISlackMessage:
assert [block["type"] for block in message.blocks] == [
"divider",
"header",
- "context",
"markdown",
"image",
]
- assert message.blocks[4]["image_url"] == _CHART["image_url"]
+ assert message.blocks[3]["image_url"] == _CHART["image_url"]
class TestBuildAITeamsCard: