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): keep thread root when collecting context for @PostHog tasks (#59174)

Andrew Maguire4694d78a057a
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.py
index 2f38900436b6..fcae6d07b02b 100644
--- a/products/slack_app/backend/api.py
+++ b/products/slack_app/backend/api.py
@@ -674,9 +674,13 @@ def _collect_thread_messages(
return re.sub(r"<@([A-Z0-9]+)>", replace_mention, text)
messages = []
- for msg in raw_messages:
+ for index, msg in enumerate(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:
+ # Never skip the thread root: the agent only ever posts as a reply, so msg 0 is
+ # always the originating message (e.g. a PostHog alert) that's the actual context
+ # for the task. Filtering it by bot_id breaks workspaces where the alerting Slack
+ # app and the `@PostHog` code app share an installation identity.
+ if index > 0 and our_bot_id and msg.get("bot_id") == our_bot_id:
continue
user_id = msg.get("user")
diff --git a/products/slack_app/backend/tests/test_collect_thread_messages.py b/products/slack_app/backend/tests/test_collect_thread_messages.py
index 6d07aa8eb317..704592b86017 100644
--- a/products/slack_app/backend/tests/test_collect_thread_messages.py
+++ b/products/slack_app/backend/tests/test_collect_thread_messages.py
@@ -215,19 +215,60 @@ class TestCollectThreadMessages:
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):
+ def test_skips_our_own_bot_reply_messages(self, mock_get_user_info):
+ # Our own bot replies (e.g. "Working on it...") must be filtered so the agent
+ # doesn't ingest its own status updates as context on a re-mention.
self._set_thread(
[
- {"bot_id": "B_OUR_CODE_BOT", "text": "Working on it..."},
- {"user": "U_ANDY", "text": "thanks"},
+ {"user": "U_ANDY", "text": "@PostHog please look at this", "ts": "1.000"},
+ {"bot_id": "B_OUR_CODE_BOT", "text": "Working on it...", "ts": "2.000"},
+ {"user": "U_ANDY", "text": "thanks", "ts": "3.000"},
]
)
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"
+ assert [m["ts"] for m in result] == ["1.000", "3.000"]
+ assert all(m["user"] == "andy" for m in result)
+
+ def test_keeps_thread_root_even_when_bot_id_matches_our_own(self, mock_get_user_info):
+ # Regression: in workspaces where the alerting Slack app and the `@PostHog` code
+ # app share an installation identity, the alert that opened the thread has the same
+ # `bot_id` as `our_bot_id`. We must still include it — the agent only posts as a
+ # reply, never as a thread root, so msg 0 is always the originating context.
+ self._set_thread(
+ [
+ {
+ "bot_id": "B_OUR_CODE_BOT",
+ "bot_profile": {"name": "PostHog"},
+ "text": "",
+ "blocks": [
+ {
+ "type": "header",
+ "text": {
+ "type": "plain_text",
+ "text": "Alert 'Headline anomaly: Trial activated' firing for insight",
+ },
+ },
+ {
+ "type": "section",
+ "text": {"type": "mrkdwn", "text": "Anomaly detected on 2026-05-19"},
+ },
+ ],
+ "ts": "1.000",
+ },
+ {"user": "U_ANDY", "text": "<@UBOT> lets investigate this one", "ts": "2.000"},
+ ]
+ )
+ 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 "Alert 'Headline anomaly: Trial activated'" in result[0]["text"]
+ assert result[1]["user"] == "andy"
def test_other_bot_uses_bot_profile_name(self, mock_get_user_info):
self._set_thread(