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): ignore mentions glued to a package path (#85612)

Manoel Aranda Netof5f10d9ad738
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.py
index 898cd2862c3e..c4e401afd88d 100644
--- a/products/slack_app/backend/api.py
+++ b/products/slack_app/backend/api.py
@@ -1158,6 +1158,45 @@ def _app_authorship_ignore_reason(event: dict[str, Any]) -> str | None:
return None
+_MENTION_WITH_TRAILING_SLASH_RE = re.compile(r"<@[A-Z0-9]+>(/?)")
+
+
+def _every_mention_is_a_path_segment(event: dict[str, Any]) -> bool:
+ """Whether every user mention in the text is glued to a following ``/``.
+
+ Slack linkifies a typed ``@PostHog`` even inside an org-scoped package or repo path, so
+ ``@PostHog/react-native-plugin`` reaches us as ``<@BOT>/react-native-plugin`` and fires a
+ real ``app_mention``. Nobody addressed the app in that message, they named a package, and
+ answering it starts an agent run against a prompt that is only the path's tail.
+
+ Requiring *every* mention to be glued keeps this from firing on a message that also tags
+ the app properly, because the app's own mention can't be told apart from a teammate's
+ without a ``users.info`` round-trip that this gate runs too early to afford.
+ """
+ text = event.get("text")
+ if not isinstance(text, str):
+ return False
+ trailing_slashes = _MENTION_WITH_TRAILING_SLASH_RE.findall(text)
+ return bool(trailing_slashes) and all(trailing_slashes)
+
+
+def _path_mention_drop_properties(event: dict[str, Any]) -> dict[str, Any]:
+ """Analytics context specific to a ``path_mention`` drop.
+
+ The drop count on its own can't separate a harmless package paste from a real request
+ the gate ate, so it can't tell us whether the gate is tuned right. Word count splits
+ them: a message that is only ``@PostHog/posthog-js`` counts one word, and anything
+ higher means prose surrounded the path, which is the shape worth reviewing.
+ """
+ text = event.get("text")
+ if not isinstance(text, str):
+ text = ""
+ return {
+ "slack_mention_count": len(_MENTION_WITH_TRAILING_SLASH_RE.findall(text)),
+ "slack_message_word_count": len(text.split()),
+ }
+
+
def _app_mention_ignore_reason(event: dict[str, Any]) -> str | None:
"""Return a short reason if this app_mention shouldn't trigger the coding agent, else None.
@@ -1167,10 +1206,16 @@ def _app_mention_ignore_reason(event: dict[str, Any]) -> str | None:
- "bot_author" / "app_authored": see ``_app_authorship_ignore_reason``. Foreign bots
that quote `<@PostHog>` in their text (incident bots, alert relays, our own
notifications integration) would trigger reply loops on every re-post.
+ - "path_mention": see ``_every_mention_is_a_path_segment``.
"""
if event.get("edited") or event.get("subtype") == "message_changed":
return "edit"
- return _app_authorship_ignore_reason(event)
+ authorship = _app_authorship_ignore_reason(event)
+ if authorship:
+ return authorship
+ if _every_mention_is_a_path_segment(event):
+ return "path_mention"
+ return None
def _thread_message_event_has_files(event: dict[str, Any]) -> bool:
@@ -1960,14 +2005,20 @@ def route_posthog_code_event_to_relevant_region(
if event_type == "app_mention":
ignore_reason = _app_mention_ignore_reason(event)
if ignore_reason:
+ drop_context: dict[str, Any] = (
+ _path_mention_drop_properties(event) if ignore_reason == "path_mention" else {}
+ )
logger.info(
"slack_app_event_app_mention_ignored",
reason=ignore_reason,
slack_team_id=slack_team_id,
channel=event.get("channel"),
message_ts=event.get("ts"),
+ **drop_context,
+ )
+ _report_slack_mention_dropped(
+ event, slack_team_id, reason=f"ignored:{ignore_reason}", replied=False, **drop_context
)
- _report_slack_mention_dropped(event, slack_team_id, reason=f"ignored:{ignore_reason}", replied=False)
return ROUTE_HANDLED_LOCALLY
else:
ignore_reason = _thread_message_ignore_reason(event)
diff --git a/products/slack_app/backend/tests/test_posthog_code_event_handler.py b/products/slack_app/backend/tests/test_posthog_code_event_handler.py
index c82b858faeb9..cfc06e83bfc0 100644
--- a/products/slack_app/backend/tests/test_posthog_code_event_handler.py
+++ b/products/slack_app/backend/tests/test_posthog_code_event_handler.py
@@ -16,6 +16,7 @@ from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.team.team import Team
from posthog.models.user import User
+from products.slack_app.backend.api import _app_mention_ignore_reason
from products.slack_app.backend.models import SlackSettings, SlackUserProfileCache
from products.slack_app.backend.tests.helpers import sign_slack_request
@@ -47,6 +48,23 @@ class TestLinkSharedUrlRegion(SimpleTestCase):
assert _link_shared_url_region(event) == expected
+class TestAppMentionIgnoreReason(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("package_path_only", "<@U0BOT>/react-native-plugin", "path_mention"),
+ ("repo_path_mid_sentence", "have a look at <@U0BOT>/posthog-js", "path_mention"),
+ # A glued path alongside a real tag means somebody is addressing the app for real,
+ # and we can't tell which of the two mentions is ours without a users.info call.
+ ("path_plus_tagged_mention", "<@U0BOT>/posthog-js is broken <@U0BOT> fix it", None),
+ ("plain_mention", "<@U0BOT> fix the login redirect", None),
+ ("no_mention", "fix the login redirect", None),
+ ]
+ )
+ def test_mentions_glued_to_a_path_are_ignored(self, _name: str, text: str, expected: str | None) -> None:
+ event = {"type": "app_mention", "channel": "C001", "user": "U123", "ts": "1234.5678", "text": text}
+ assert _app_mention_ignore_reason(event) == expected
+
+
class TestPostHogCodeEventHandler(SimpleTestCase):
def setUp(self):
self.client = APIClient()
@@ -400,15 +418,29 @@ class TestRoutePostHogCodeEventToRelevantRegion(TestCase):
@parameterized.expand(
[
- ("edited_field", {"edited": {"user": "U123", "ts": "1234.7777"}}, "ignored:edit"),
- ("message_changed_subtype", {"subtype": "message_changed"}, "ignored:edit"),
- ("bot_id", {"bot_id": "B0ALERT"}, "ignored:bot_author"),
- ("bot_profile", {"bot_profile": {"name": "Mendral", "id": "B0ALERT"}}, "ignored:bot_author"),
+ ("edited_field", {"edited": {"user": "U123", "ts": "1234.7777"}}, "ignored:edit", {}),
+ ("message_changed_subtype", {"subtype": "message_changed"}, "ignored:edit", {}),
+ ("bot_id", {"bot_id": "B0ALERT"}, "ignored:bot_author", {}),
+ ("bot_profile", {"bot_profile": {"name": "Mendral", "id": "B0ALERT"}}, "ignored:bot_author", {}),
# Still dropped, but under its own reason so the volume of app-posted-as-a-human
# mentions is measurable rather than hidden inside the bot bucket.
- ("app_id", {"app_id": "A0ALERT"}, "ignored:app_authored"),
- ("bot_message_subtype", {"subtype": "bot_message"}, "ignored:bot_author"),
- ("slackbot_user", {"user": "USLACKBOT"}, "ignored:bot_author"),
+ ("app_id", {"app_id": "A0ALERT"}, "ignored:app_authored", {}),
+ ("bot_message_subtype", {"subtype": "bot_message"}, "ignored:bot_author", {}),
+ ("slackbot_user", {"user": "USLACKBOT"}, "ignored:bot_author", {}),
+ # The word count is what tells a bare package paste from a real request the gate
+ # ate, so it has to survive onto the captured event, not just the log line.
+ (
+ "path_mention",
+ {"text": "<@U0BOT>/react-native-plugin"},
+ "ignored:path_mention",
+ {"slack_mention_count": 1, "slack_message_word_count": 1},
+ ),
+ (
+ "path_mention_with_prose",
+ {"text": "have a look at <@U0BOT>/posthog-js"},
+ "ignored:path_mention",
+ {"slack_mention_count": 1, "slack_message_word_count": 5},
+ ),
]
)
@patch("products.slack_app.backend.api.posthoganalytics.capture")
@@ -420,6 +452,7 @@ class TestRoutePostHogCodeEventToRelevantRegion(TestCase):
_name,
ignore_marker: dict,
expected_drop_reason: str,
+ expected_extra_properties: dict,
mock_sync_connect,
mock_asyncio_run,
mock_capture,
@@ -445,6 +478,8 @@ class TestRoutePostHogCodeEventToRelevantRegion(TestCase):
assert capture_kwargs["event"] == SLACK_MENTION_DROPPED_EVENT
assert capture_kwargs["properties"]["drop_reason"] == expected_drop_reason
assert capture_kwargs["properties"]["replied"] is False
+ for key, value in expected_extra_properties.items():
+ assert capture_kwargs["properties"][key] == value
@patch("products.slack_app.backend.api.posthoganalytics.capture")
@patch("products.slack_app.backend.api._post_slack_user_feedback")