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(slack-app): stop duplicating thread messages in agent context (#87014)

Vojta Bartoše16cfbbd5d56
diff --git a/products/slack_app/backend/services/slack_messages.py b/products/slack_app/backend/services/slack_messages.py
index 63ac91126d1c..ef6bb8b1ebb5 100644
--- a/products/slack_app/backend/services/slack_messages.py
+++ b/products/slack_app/backend/services/slack_messages.py
@@ -193,6 +193,11 @@ def extract_message_text(msg: dict) -> str:
pieces.append(text)
blocks = msg.get("blocks") or []
+ if text:
+ # A `rich_text` block is Slack's structured mirror of `text`. Flattening it drops
+ # mentions and emoji, yielding a near-duplicate the exact-match dedup below can't
+ # catch — skip it and let `text` (which keeps both) speak for that content.
+ blocks = [b for b in blocks if not (isinstance(b, dict) and b.get("type") == "rich_text")]
attachments = msg.get("attachments") or []
try:
pieces.extend(flatten_block_text(blocks))
diff --git a/products/slack_app/backend/tests/services/slack_messages/test_collect_thread_messages.py b/products/slack_app/backend/tests/services/slack_messages/test_collect_thread_messages.py
index 067ad476aef7..0544421a8a8c 100644
--- a/products/slack_app/backend/tests/services/slack_messages/test_collect_thread_messages.py
+++ b/products/slack_app/backend/tests/services/slack_messages/test_collect_thread_messages.py
@@ -154,6 +154,65 @@ class TestExtractMessageText:
},
"duplicated\ndifferent",
),
+ (
+ # Human messages carry a rich_text mirror of `text` whose flattening drops
+ # mentions and emoji — a near-duplicate that must not reach the agent prompt.
+ "rich_text_mirror_of_text_is_skipped",
+ {
+ "text": "never seen it work anywhere :joy: cc <@UCLEO>",
+ "blocks": [
+ {
+ "type": "rich_text",
+ "elements": [
+ {
+ "type": "rich_text_section",
+ "elements": [
+ {"type": "text", "text": "never seen it work anywhere "},
+ {"type": "emoji", "name": "joy"},
+ {"type": "text", "text": " cc "},
+ {"type": "user", "user_id": "UCLEO"},
+ ],
+ }
+ ],
+ }
+ ],
+ },
+ "never seen it work anywhere :joy: cc <@UCLEO>",
+ ),
+ (
+ "rich_text_still_flattened_when_text_empty",
+ {
+ "text": "",
+ "blocks": [
+ {
+ "type": "rich_text",
+ "elements": [
+ {
+ "type": "rich_text_section",
+ "elements": [{"type": "text", "text": "bot content only in blocks"}],
+ }
+ ],
+ }
+ ],
+ },
+ "bot content only in blocks",
+ ),
+ (
+ "non_rich_text_blocks_kept_alongside_text",
+ {
+ "text": "🔴 Alert firing",
+ "blocks": [
+ {
+ "type": "rich_text",
+ "elements": [
+ {"type": "rich_text_section", "elements": [{"type": "text", "text": "🔴 Alert firing"}]}
+ ],
+ },
+ {"type": "section", "text": {"type": "mrkdwn", "text": "value 42 exceeded threshold 10"}},
+ ],
+ },
+ "🔴 Alert firing\nvalue 42 exceeded threshold 10",
+ ),
(
"no_content_returns_empty",
{"text": "", "blocks": [], "attachments": []},