Skip to content

fix(ambient): bypass discord bot gating for ambient buffer#1226

Merged
thepagent merged 3 commits into
mainfrom
fix/ambient-bot-gating-bypass
Jun 27, 2026
Merged

fix(ambient): bypass discord bot gating for ambient buffer#1226
thepagent merged 3 commits into
mainfrom
fix/ambient-bot-gating-bypass

Conversation

@chaodu-agent

@chaodu-agent chaodu-agent commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

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 returns early for non-@mention bot messages in Mentions mode.

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:#333
Loading

Problem: 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:#333
Loading

Fix: 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:

  1. Bot message gating (early return for non-mention)
  2. Thread detection
  3. Ambient context check + routing

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:

  1. Thread detection (moved up)
  2. Ambient context check (moved up)
  3. NEW: Bot msg + ambient context + no mention → route directly to ambient buffer
  4. Bot message gating (unchanged, but ambient-bound msgs never reach here)
  5. Normal ambient routing (handles human msgs + bot @mentions in ambient)

The [ambient.discord].allow_bot_messages config remains the sole gatekeeper for bot messages entering the ambient buffer — discord-level bot gating no longer interferes.

Testing

  • rustfmt --check passes (syntactically valid)
  • No behavioral change for non-ambient paths (bot gating logic unchanged)
  • No behavioral change when [ambient.discord].allow_bot_messages = false (bot msgs still rejected at ambient level)

Closes #1197

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
@chaodu-agent chaodu-agent requested a review from thepagent as a code owner June 27, 2026 23:31
@chaodu-agent

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.
@chaodu-agent

This comment has been minimized.

@chaodu-agent

This comment has been minimized.

@thepagent thepagent enabled auto-merge (squash) June 27, 2026 23:51
@thepagent thepagent merged commit e9226ba into main Jun 27, 2026
35 checks passed
@chaodu-agent

Copy link
Copy Markdown
Collaborator Author

LGTM ✅

Summary

Ambient-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

# Severity Finding Location Status
1 🟢 Thread detection + ambient context correctly moved before bot gating discord.rs:464
2 🟢 Early-route condition precise: bot + ambient + no mention → buffer discord.rs:533
3 🟢 Early-gate optimization preserves non-ambient fast path (no regression) discord.rs:464
4 🟢 Debug tracing added for both buffered and rejected paths discord.rs:536,567
5 🟢 ChannelRef construction matches existing ambient routing pattern discord.rs:551
6 🟡 Unit test coverage for ambient early-route Follow-up
7 🟡 docs/ambient.md update for bot message behavior docs/ Follow-up

Verified

  • cargo check passes (CI build-builder green in 13m36s)
  • All 16 Dockerfile smoke-tests pass
  • All 15 unified smoke-tests pass
  • No behavioral change when [ambient.discord].allow_bot_messages = false
  • No perf regression for non-ambient deployments (early-gate returns before to_channel())

Decision

Ready to merge. Follow-up items (test + docs) tracked separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants