fix(slack): ignore app_mention re-fires from message edits#59517
Merged
Conversation
Slack sends a fresh app_mention with a new event_id when a user edits a message that already mentioned the bot. The current dispatcher derives the Temporal workflow id from event_id, so the edit's workflow id differs from the original's and USE_EXISTING does not deduplicate it. Both workflows then run the followup activity, find no SlackThreadTaskMapping yet (the original hasn't written it), and each create their own task. Detect these events at dispatch via `event.edited` / `subtype: message_changed` and short-circuit before starting a workflow. Generated-By: PostHog Code Task-Id: 41dd411e-73f2-46f3-976e-409f6e27ce7b
Contributor
|
Reviews (1): Last reviewed commit: "fix(slack): ignore app_mention re-fires ..." | Re-trigger Greptile |
Contributor
ClickHouse migration SQL per cloud environmentNo ClickHouse migrations changed in this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Editing a Slack message that mentions
@PostHog(the coding-agent bot) created a second task in parallel with the original. The user expects one task per intent; the edit should not spawn another.Slack re-fires
app_mentionwith a freshevent_idwhenever a previously-mentioning message is edited. The dispatcher inproducts/slack_app/backend/api.pyderives the Temporal workflow id fromevent_id, so the edit's workflow id differs from the original's and Temporal'sUSE_EXISTINGpolicy does not deduplicate it. Both workflows runforward_posthog_code_followup_activity, find noSlackThreadTaskMappingyet (the original workflow hasn't written it), and each fall through tocreate_posthog_code_task_for_repo_activity— producing twoTaskrows for the same intent.Changes
_is_app_mention_edit(event)helper that returns true when Slack marks the event as an edit (event.editedset, orsubtype: "message_changed").route_posthog_code_event_to_relevant_region, short-circuitapp_mentionevents that match before any workflow is started, logposthog_code_event_edit_ignored, and returnROUTE_HANDLED_LOCALLY.Rejected alternative: re-keying the Temporal workflow id on
(slack_team_id, channel, thread_ts)soUSE_EXISTINGwould dedupe the edit automatically. This would also have absorbed legitimate same-thread follow-ups into the running workflow, which is already whatforward_posthog_code_followup_activityis designed to handle as a separate workflow invocation — so re-keying would have changed semantics beyond fixing the bug.Not rejected but worth noting: Slack also emits
app_mentionwhen a user edits a previously-non-mentioning message to add the bot mention. In that caseevent.editedis still set, so this guard will drop those too. That tradeoff matches the user's expectation as described and avoids the racier "checkSlackThreadTaskMappingbefore dispatch" path, which is subject to the same race that produced the bug.How did you test this code?
I'm Claude / PostHog Code. The Claude Code for web environment did not have a working Python 3.12.12 toolchain available (only 3.12.13 builds are published), so I did not run the Django test suite locally. Verified instead:
ruff checkandruff format --checkpass on both touched files.ast.parseconfirms the test file and api.py are syntactically valid.edited_field,message_changed_subtype) cover both Slack markers and assert thatsync_connect/asyncio.runare never invoked when the edit guard fires.CI is the source of truth for the test run.
Publish to changelog?
no
🤖 Agent context
Authored by PostHog Code (Claude Opus 4.7). The user reported the duplicate-task behavior, and I traced it through
products/slack_app/backend/api.py→posthog/temporal/ai/posthog_code_slack_mention.py. The investigation considered three fixes:(team, channel, thread_ts)— rejected as overly broad, would change follow-up semantics.SlackThreadTaskMappinglookup — rejected because it doesn't fix the race (the original workflow may not have written the mapping by the time the edit arrives), and would break legitimate same-thread follow-ups which intentionally start a new workflow.Reviewers: please confirm the assumption that Slack does not fire
app_mentionfor edits-that-newly-add-a-mention withoutevent.editedbeing set (matches Slack's documented behavior and matches the user-reported scenario, but I have not verified against a live Slack webhook capture).Created with PostHog Code