feat(mattermost): thread context + in-thread auto-response (Slack parity) - #43791
Closed
crisap94 wants to merge 1 commit into
Closed
feat(mattermost): thread context + in-thread auto-response (Slack parity)#43791crisap94 wants to merge 1 commit into
crisap94 wants to merge 1 commit into
Conversation
…ity)
The Mattermost adapter only saw the single message it was @mentioned in and
required a fresh @mention every turn. The Slack adapter already solves both;
this ports that pattern to Mattermost.
- In-thread auto-response: after the first @mention in a thread, subsequent
messages in that thread auto-trigger the bot. Opt out with
MATTERMOST_STRICT_MENTION=true (mirrors SLACK_STRICT_MENTION).
- Thread context: on the first turn in a pre-existing thread, prior messages
are fetched (GET /posts/{root}/thread) and injected via channel_context.
- Security: injected thread history is filtered to allowlisted authors
(MATTERMOST_ALLOWED_USERS / *_ALLOW_ALL_USERS). The authz layer only checks
the triggering author; channel_context bypasses it, so unfiltered history
would leak non-allowlisted users' messages into the model.
- Session-key parity: _has_active_session_for_thread passes the adapter's real
chat_type so the key matches what handle_message persists.
Adds TestMattermostThreadBehavior (12 cases) and documents
MATTERMOST_STRICT_MENTION + the allowlist-filtered context behavior.
Related: NousResearch#38362, NousResearch#38152, NousResearch#37144 (thread-context PRs without the allowlist filter).
crisap94
force-pushed
the
feat/mattermost-thread-context-and-autorespond
branch
from
June 10, 2026 22:03
164a0cb to
89ca9db
Compare
Author
|
Superseded — split into two focused, stacked PRs for clearer review:
Closing this consolidated PR in favor of those. Thanks! |
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.
What does this PR do?
Brings the Mattermost adapter to parity with the Slack adapter for thread handling, and closes a security gap that surfaces once thread history is loaded.
Two behaviors users expect in a thread were missing on Mattermost:
@mentionedinside an existing thread, it only ever saw the single message it was tagged in — never the prior conversation it was pulled into. (_handle_ws_eventbuilds theMessageEventstraight from the triggering post, with nochannel_context.)@mentionedeach time, which makes a back-and-forth feel broken.The Slack adapter already solves both (
_mentioned_threads,_fetch_thread_context,_has_active_session_for_thread,SLACK_STRICT_MENTION). This ports that exact pattern to Mattermost.Security: thread context must respect the user allowlist
The user allowlist is enforced in
gateway/authz_mixin.py::_is_user_authorized, per dispatched message, by author — downstream of the adapter. That means:MessageEvent.channel_context, which authz never inspects. Loading the whole thread verbatim would feed a non-allowlisted user's messages straight into the model (an unauthorized-content / prompt-injection vector).So
_fetch_thread_contextfilters historical posts to authors inMATTERMOST_ALLOWED_USERS(or everyone whenMATTERMOST_ALLOW_ALL_USERS/GATEWAY_ALLOW_ALL_USERSis set), mirroring the authz allowlist. The bot's own posts and system posts are excluded as well. This is the piece the existing thread-context PRs (#38362, #38152) do not have.Related Issue
Related (all address Mattermost thread context, none adds in-thread auto-response or the allowlist filter): #38362, #38152, #37144.
Fixes #
Type of Change
Changes Made
All adapter changes in
plugins/platforms/mattermost/adapter.py:_strict_mention()— readsMATTERMOST_STRICT_MENTION(defaultfalse) +config.extraoverride, mirroring_slack_strict_mention._has_active_session_for_thread()— mirrors Slack, but passes the adapter's realchat_type(_CHANNEL_TYPE_MAP[...]) intobuild_session_key, so the key matches whathandle_messagepersists (a hardcoded"group"like Slack uses would never match for"O"channels)._fetch_thread_context()— fetchesGET /posts/{root}/thread, fails open via the existing_api_get, TTL-cached, excludes the trigger/bot/system posts, filters to allowlisted authors, and injects viaMessageEvent.channel_context.strict_mention,in_mentioned_thread, andhas_session; registers a thread on@mentiononly when not strict; preserves@mentionstripping.MessageEvent(...), guarded by_has_active_session_for_threadso it runs once per thread.tests/gateway/test_mattermost.py— newTestMattermostThreadBehavior(12 cases): strict-mention resolution, context fetch/exclusion, allowlist filtering (+ allow-all), fail-open, auto-response, strict-mode regression, non-thread/DM regressions, and session-key chat_type correctness.website/docs/user-guide/messaging/mattermost.mdandwebsite/docs/reference/environment-variables.mddocumentMATTERMOST_STRICT_MENTION, the in-thread behavior, and the allowlist-filtered context.No new config keys in
cli-config.yaml.example(behavior is env/config.extra-driven, consistent with the existingMATTERMOST_REQUIRE_MENTION/MATTERMOST_FREE_RESPONSE_CHANNELS).How to Test
MATTERMOST_ALLOWED_USERS=<your_id>and start the gateway with a bot in a channel.@mentionthe bot → its reply reflects the prior thread messages (context loaded). Post a follow-up without mentioning it → it still replies (auto-response).MATTERMOST_STRICT_MENTION=true→ the bot only replies when@mentionedeach turn (old behavior).pytest tests/gateway/test_mattermost.py -q→ 55 passed.Checklist
Code
feat(mattermost): ...)pytest tests/gateway/test_mattermost.py -q→ 55 passedDocumentation & Housekeeping
website/docs/)cli-config.yaml.example— N/A (no new config keys)CONTRIBUTING.md/AGENTS.md— N/A (no architecture/workflow change)