: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
fix(slack-app): include block text from thread root in agent context (#57614)
posthog[bot]7ff4332d4f74
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.pyindex 22984210757b..efac46641737 100644--- a/products/slack_app/backend/api.py+++ b/products/slack_app/backend/api.py@@ -581,6 +581,73 @@ def _extract_explicit_repo(text: str, all_repos: list[str]) -> str | None:return None+def _flatten_block_text(node: Any) -> list[str]:+ """Best-effort plain-text extraction from a Slack block-kit subtree.++ Slack alert posts (subscriptions, log alerts, hog-function destinations) often+ put the substantive content in `blocks` while the top-level `text` field is a+ short fallback (or empty). Walking the block tree lets us surface that content+ to the agent. Always wrap call sites in try/except — Slack block schemas evolve.+ """+ if node is None:+ return []+ if isinstance(node, str):+ stripped = node.strip()+ return [stripped] if stripped else []+ if isinstance(node, list):+ out: list[str] = []+ for item in node:+ out.extend(_flatten_block_text(item))+ return out+ if isinstance(node, dict):+ # `context` blocks can carry useful labels — recurse into `elements` only.+ if node.get("type") == "context":+ return _flatten_block_text(node.get("elements"))+ # Skip interactive/decorative blocks that carry no information for the agent.+ if node.get("type") in ("actions", "divider", "image"):+ return []+ out = []+ for key in ("text", "fields", "elements", "title", "pretext", "fallback"):+ if key in node:+ out.extend(_flatten_block_text(node[key]))+ return out+ return []+++def _extract_message_text(msg: dict) -> str:+ # Always include `text` and `blocks`/`attachments`: PostHog's own alert templates put+ # the headline in `text` and the values/details in blocks. Dedup so a string repeated+ # across both (e.g. text == header block) shows up once.+ pieces: list[str] = []+ text = (msg.get("text") or "").strip()+ if text:+ pieces.append(text)++ blocks = msg.get("blocks") or []+ attachments = msg.get("attachments") or []+ try:+ pieces.extend(_flatten_block_text(blocks))+ except Exception:+ logger.warning("slack_thread_block_flatten_failed", exc_info=True)+ try:+ pieces.extend(_flatten_block_text(attachments))+ except Exception:+ logger.warning("slack_thread_attachment_flatten_failed", exc_info=True)++ seen: set[str] = set()+ deduped: list[str] = []+ for piece in pieces:+ if piece and piece not in seen:+ seen.add(piece)+ deduped.append(piece)+ return "\n".join(deduped)+++def _resolve_bot_author_label(msg: dict) -> str:+ bot_profile = msg.get("bot_profile") or {}+ return bot_profile.get("name") or msg.get("username") or "Bot"++def _collect_thread_messages(slack: SlackIntegration, integration: Integration, channel: str, thread_ts: str, our_bot_id: str | None) -> list[dict[str, str]]:@@ -608,11 +675,19 @@ def _collect_thread_messages(messages = []for msg in raw_messages:+ # Skip our own bot's posts to avoid loops where the agent ingests its own replies.if our_bot_id and msg.get("bot_id") == our_bot_id:continue+user_id = msg.get("user")- username = resolve_user(user_id) if user_id else "Unknown"- text = replace_user_mentions(msg.get("text", ""))+ if user_id:+ username = resolve_user(user_id)+ elif msg.get("bot_id"):+ username = _resolve_bot_author_label(msg)+ else:+ username = "Unknown"++ text = replace_user_mentions(_extract_message_text(msg))messages.append({"user": username, "text": text})return messagesdiff --git a/products/slack_app/backend/tests/test_collect_thread_messages.py b/products/slack_app/backend/tests/test_collect_thread_messages.pynew file mode 100644index 000000000000..ab4ea7d60c75--- /dev/null+++ b/products/slack_app/backend/tests/test_collect_thread_messages.py@@ -0,0 +1,300 @@+import pytest+from unittest.mock import MagicMock, patch++from parameterized import parameterized++from posthog.models.integration import Integration, SlackIntegration+from posthog.models.organization import Organization+from posthog.models.team.team import Team++from products.slack_app.backend.api import _collect_thread_messages, _extract_message_text, _flatten_block_text+++class TestFlattenBlockText:+ @parameterized.expand(+ [+ ("none", None, []),+ ("plain_string", "hello", ["hello"]),+ ("whitespace_string", " ", []),+ ("padded_string", " hi ", ["hi"]),+ ("non_str_scalar", 12345, []),+ ("empty_section", {"type": "section"}, []),+ (+ "section_text",+ {"type": "section", "text": {"type": "mrkdwn", "text": "🔴 Alert firing"}},+ ["🔴 Alert firing"],+ ),+ (+ "section_fields",+ {+ "type": "section",+ "fields": [+ {"type": "mrkdwn", "text": "*Threshold:* 10"},+ {"type": "mrkdwn", "text": "*Value:* 42"},+ ],+ },+ ["*Threshold:* 10", "*Value:* 42"],+ ),+ (+ "header",+ {"type": "header", "text": {"type": "plain_text", "text": "High error rate"}},+ ["High error rate"],+ ),+ (+ "context_elements",+ {+ "type": "context",+ "elements": [+ {"type": "mrkdwn", "text": "service: web"},+ {"type": "mrkdwn", "text": "severity: critical"},+ ],+ },+ ["service: web", "severity: critical"],+ ),+ (+ "rich_text_nested",+ {+ "type": "rich_text",+ "elements": [+ {+ "type": "rich_text_section",+ "elements": [{"type": "text", "text": "hello world"}],+ }+ ],+ },+ ["hello world"],+ ),+ ]+ )+ def test_flatten_cases(self, _name: str, node, expected: list[str]) -> None:+ assert _flatten_block_text(node) == expected++ def test_skips_actions_and_dividers(self) -> None:+ blocks = [+ {"type": "header", "text": {"type": "plain_text", "text": "Title"}},+ {"type": "divider"},+ {+ "type": "actions",+ "elements": [{"type": "button", "text": {"type": "plain_text", "text": "Click me"}}],+ },+ ]+ assert _flatten_block_text(blocks) == ["Title"]+++class TestExtractMessageText:+ @parameterized.expand(+ [+ (+ "text_only_no_blocks",+ {"text": "hello world"},+ "hello world",+ ),+ (+ "text_and_block_with_extra_detail_combines",+ {+ "text": "🔴 Alert firing",+ "blocks": [+ {"type": "header", "text": {"type": "plain_text", "text": "🔴 Alert firing"}},+ {+ "type": "section",+ "text": {"type": "mrkdwn", "text": "value 42 exceeded threshold 10"},+ },+ ],+ },+ "🔴 Alert firing\nvalue 42 exceeded threshold 10",+ ),+ (+ "blocks_only_falls_back",+ {+ "text": "",+ "blocks": [+ {"type": "section", "text": {"type": "mrkdwn", "text": "real content"}},+ ],+ },+ "real content",+ ),+ (+ "attachments_fallback",+ {+ "text": "",+ "blocks": [],+ "attachments": [+ {"fallback": "PostHog alert: signups dropped 30% week over week"},+ ],+ },+ "PostHog alert: signups dropped 30% week over week",+ ),+ (+ "whitespace_text_treated_as_empty",+ {+ "text": " ",+ "blocks": [+ {"type": "section", "text": {"type": "mrkdwn", "text": "real content"}},+ ],+ },+ "real content",+ ),+ (+ "dedupes_repeats_across_text_and_blocks",+ {+ "text": "duplicated",+ "blocks": [+ {"type": "section", "text": {"type": "mrkdwn", "text": "duplicated"}},+ {"type": "section", "text": {"type": "mrkdwn", "text": "different"}},+ ],+ },+ "duplicated\ndifferent",+ ),+ (+ "no_content_returns_empty",+ {"text": "", "blocks": [], "attachments": []},+ "",+ ),+ (+ "missing_keys_returns_empty",+ {},+ "",+ ),+ ]+ )+ def test_extract_cases(self, _name: str, msg: dict, expected: str) -> None:+ assert _extract_message_text(msg) == expected+++@patch("products.slack_app.backend.api._get_slack_user_info")+class TestCollectThreadMessages:+ @pytest.fixture(autouse=True)+ def setup(self, db):+ self.organization = Organization.objects.create(name="Test Org")+ self.team = Team.objects.create(organization=self.organization, name="Test Team")+ self.integration = Integration.objects.create(+ team=self.team,+ kind="slack-posthog-code",+ integration_id="T12345",+ sensitive_config={"access_token": "xoxb-test"},+ )+ self.slack = MagicMock(spec=SlackIntegration)+ self.slack.client = MagicMock()++ def _set_thread(self, messages: list[dict]) -> None:+ self.slack.client.conversations_replies.return_value = {"messages": messages}++ def test_alert_blocks_only_message_is_included(self, mock_get_user_info):+ # PostHog alert: substantive content lives only in blocks, no text field.+ self._set_thread(+ [+ {+ "bot_id": "B_ALERTS",+ "bot_profile": {"name": "PostHog"},+ "text": "",+ "blocks": [+ {+ "type": "header",+ "text": {"type": "plain_text", "text": "🔴 Log alert 'High Error Rate' is firing"},+ },+ {+ "type": "section",+ "text": {+ "type": "mrkdwn",+ "text": "Result count *42* exceeded threshold *10* over 5m",+ },+ },+ ],+ },+ {"user": "U_ANDY", "text": "<@UBOT> was that really an anomaly?"},+ ]+ )+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "andy"}}}++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id="B_OUR_CODE_BOT")++ assert len(result) == 2+ assert result[0]["user"] == "PostHog"+ assert "🔴 Log alert 'High Error Rate' is firing" in result[0]["text"]+ assert "Result count *42* exceeded threshold *10*" in result[0]["text"]+ assert result[1]["user"] == "andy"+ assert "was that really an anomaly?" in result[1]["text"]++ def test_skips_our_own_bot_messages(self, mock_get_user_info):+ self._set_thread(+ [+ {"bot_id": "B_OUR_CODE_BOT", "text": "Working on it..."},+ {"user": "U_ANDY", "text": "thanks"},+ ]+ )+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "andy"}}}++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id="B_OUR_CODE_BOT")++ assert len(result) == 1+ assert result[0]["user"] == "andy"++ def test_other_bot_uses_bot_profile_name(self, mock_get_user_info):+ self._set_thread(+ [+ {+ "bot_id": "B_GRAFANA",+ "bot_profile": {"name": "Grafana"},+ "text": "alert: latency p95 above 2s",+ }+ ]+ )++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id="B_OUR_CODE_BOT")++ assert result == [{"user": "Grafana", "text": "alert: latency p95 above 2s"}]+ mock_get_user_info.assert_not_called()++ def test_bot_without_profile_falls_back_to_username_field(self, mock_get_user_info):+ self._set_thread([{"bot_id": "B_HOOK", "username": "PostHog Webhook", "text": "ping"}])++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id=None)++ assert result == [{"user": "PostHog Webhook", "text": "ping"}]++ def test_preserves_user_mention_replacement(self, mock_get_user_info):+ self._set_thread([{"user": "U_ANDY", "text": "hey <@UBOT> can you help"}])+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "posthog-code"}}}++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id=None)++ assert result[0]["text"] == "hey @posthog-code can you help"++ def test_includes_thread_root_when_our_bot_id_is_none(self, mock_get_user_info):+ # Defensive: if auth_test() somehow returned no bot_id we still process the thread.+ self._set_thread(+ [+ {"bot_id": "B_ALERT", "bot_profile": {"name": "PostHog"}, "text": "alert"},+ {"user": "U_ANDY", "text": "follow up"},+ ]+ )+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "andy"}}}++ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id=None)++ assert [m["user"] for m in result] == ["PostHog", "andy"]++ def test_block_extraction_failure_does_not_break_collection(self, mock_get_user_info):+ # If block flattening blows up for one message, the rest of the thread still flows.+ self._set_thread(+ [+ {+ "bot_id": "B_ALERT",+ "bot_profile": {"name": "PostHog"},+ "text": "",+ "blocks": [{"type": "section"}],+ },+ {"user": "U_ANDY", "text": "still here"},+ ]+ )+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "andy"}}}++ with patch(+ "products.slack_app.backend.api._flatten_block_text",+ side_effect=RuntimeError("boom"),+ ):+ result = _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id="B_OUR")++ assert len(result) == 2+ assert result[0]["user"] == "PostHog"+ assert result[1]["text"] == "still here"