fix(ambient): bypass discord bot gating for ambient buffer#1226
Merged
Conversation
Bot messages in ambient-configured channels/threads were incorrectly blocked by the discord-level bot message gating (allow_bot_messages mode + trusted_bot_ids) before reaching the ambient routing logic. This caused ambient mode to never observe other bots' messages even when [ambient.discord].allow_bot_messages = true, because the discord-level gate runs first and returns early for non-@mention bot messages. Fix: Move thread detection before bot gating and add an ambient early-route that lets non-mention bot messages bypass discord-level bot gating entirely. The ambient-level allow_bot_messages config remains the sole gatekeeper for bot messages entering the ambient buffer. Flow after fix: 1. Thread detection (moved up) 2. Ambient context check (moved up) 3. Bot msg + ambient context + no mention → ambient buffer (NEW) 4. Bot message gating (unchanged, but ambient msgs never reach here) 5. Normal ambient routing (handles human msgs + bot @mentions) Closes #1197
This comment has been minimized.
This comment has been minimized.
added 2 commits
June 27, 2026 23:33
Adds tracing::debug logs for both the buffered and rejected paths in the ambient early-route block, making it easier to diagnose ambient bot message routing issues.
Avoid unnecessary to_channel() API calls for non-mention bot messages when ambient mode is not configured. Previously these would have been rejected at bot gating without the extra async call; moving thread detection up should not regress that path. When ambient is None and allow_bot_messages is Off or Mentions, non- mention bot messages return immediately without hitting to_channel(). When allow_bot_messages is All, thread detection is still needed for normal dispatch, so we fall through.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
thepagent
approved these changes
Jun 27, 2026
Collaborator
Author
LGTM ✅SummaryAmbient-bound bot messages now correctly bypass discord-level bot gating. The handler reordering is minimal and safe — no behavioral change for non-ambient deployments. Findings
Verified
DecisionReady to merge. Follow-up items (test + docs) tracked separately. |
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.
Summary
Bot messages in ambient-configured channels/threads were incorrectly blocked by the discord-level bot message gating (
allow_bot_messagesmode +trusted_bot_ids) before reaching the ambient routing logic.This caused ambient mode to never observe other bots' messages even when
[ambient.discord].allow_bot_messages = true, because the discord-level gate returns early for non-@mention bot messages inMentionsmode.Before (broken)
flowchart TD A[Bot message arrives] --> B{Bot gating: allow_bot_messages mode} B -->|"Off"| X[❌ return - BLOCKED] B -->|"Mentions"| C{Is @mention?} C -->|No| X C -->|Yes| D[Thread detection] B -->|"All"| D D --> E{In ambient context?} E -->|Yes| F[Ambient buffer] E -->|No| G[Normal dispatch] style X fill:#f66,stroke:#333 style F fill:#6f6,stroke:#333Problem: Non-mention bot messages are killed at step B and never reach ambient routing at step E.
After (fixed)
flowchart TD A[Bot message arrives] --> B[Thread detection] B --> C{In ambient context?} C -->|Yes| D{Is @mention?} D -->|No| E{ambient.allow_bot_messages?} E -->|Yes| F[✅ Ambient buffer] E -->|No| X1[❌ return] D -->|Yes| G[Discard buffer + fall through] G --> H{Bot gating: allow_bot_messages mode} C -->|No| H H -->|Pass| I[Normal dispatch] H -->|Fail| X2[❌ return] style F fill:#6f6,stroke:#333 style X1 fill:#f66,stroke:#333 style X2 fill:#f66,stroke:#333Fix: Thread detection and ambient context check run before bot gating. Non-mention bot messages in ambient context bypass discord-level bot gating entirely.
Root Cause
The original handler order:
Bot messages without @mention were killed at step 1 and never reached step 3.
Fix
Reorder the handler to resolve ambient context before bot gating:
The
[ambient.discord].allow_bot_messagesconfig remains the sole gatekeeper for bot messages entering the ambient buffer — discord-level bot gating no longer interferes.Testing
rustfmt --checkpasses (syntactically valid)[ambient.discord].allow_bot_messages = false(bot msgs still rejected at ambient level)Closes #1197