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): ignore app_mention re-fires from message edits (#59517)

Richard Solomou1b634ff0e45f
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.py
index 34fdf451ee44..296f68e726a7 100644
--- a/products/slack_app/backend/api.py
+++ b/products/slack_app/backend/api.py
@@ -1074,6 +1074,21 @@ def _posthog_code_flag_subject(integration: Integration) -> User | None:
return fallback.user if fallback else None
+def _is_app_mention_edit(event: dict[str, Any]) -> bool:
+ """Detect app_mention events Slack re-fires when a user edits a mentioning message.
+
+ Why: Slack sends a fresh app_mention with a new event_id when a previously-posted
+ mention is edited, which gives the resulting Temporal workflow a different
+ workflow_id and bypasses USE_EXISTING — so without this guard the edit kicks off
+ a second task in parallel with the original.
+ """
+ if event.get("edited"):
+ return True
+ if event.get("subtype") == "message_changed":
+ return True
+ return False
+
+
def _posthog_code_enabled_for_integration(integration: Integration) -> bool:
"""Runtime gate for the coding agent on app_mention events.
@@ -1174,6 +1189,14 @@ def route_posthog_code_event_to_relevant_region(
if local_match and not (settings.DEBUG and request.get_host() == SLACK_PRIMARY_REGION_DOMAIN):
if event_type == "app_mention":
+ if _is_app_mention_edit(event):
+ logger.info(
+ "posthog_code_event_edit_ignored",
+ slack_team_id=slack_team_id,
+ channel=event.get("channel"),
+ message_ts=event.get("ts"),
+ )
+ return ROUTE_HANDLED_LOCALLY
if not _posthog_code_enabled_for_integration(local_match):
logger.info(
"posthog_code_event_flag_off",
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 0ec27409b139..772347c76346 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
@@ -245,6 +245,35 @@ class TestRoutePostHogCodeEventToRelevantRegion(TestCase):
else:
assert pending_picker is None
+ @parameterized.expand(
+ [
+ ("edited_field", {"edited": {"user": "U123", "ts": "1234.7777"}}),
+ ("message_changed_subtype", {"subtype": "message_changed"}),
+ ]
+ )
+ @patch("products.slack_app.backend.api._posthog_code_enabled_for_integration", return_value=True)
+ @patch("products.slack_app.backend.api.asyncio.run")
+ @patch("products.slack_app.backend.api.sync_connect")
+ @override_settings(DEBUG=False)
+ def test_app_mention_edit_does_not_start_workflow(
+ self,
+ _name,
+ edit_marker: dict,
+ mock_sync_connect,
+ mock_asyncio_run,
+ _mock_flag,
+ ):
+ request = self.factory.post("/slack/event-callback/", HTTP_HOST="eu.posthog.com")
+ edited_event = {**self.event, **edit_marker}
+
+ from products.slack_app.backend.api import ROUTE_HANDLED_LOCALLY, route_posthog_code_event_to_relevant_region
+
+ result = route_posthog_code_event_to_relevant_region(request, edited_event, "T12345")
+
+ assert result == ROUTE_HANDLED_LOCALLY
+ mock_sync_connect.assert_not_called()
+ mock_asyncio_run.assert_not_called()
+
@patch("products.slack_app.backend.api._posthog_code_enabled_for_integration", return_value=False)
@patch("products.slack_app.backend.api.asyncio.run")
@patch("products.slack_app.backend.api.sync_connect")