Skip to content

fix(discord): pass guild_id to slash-command session source - #91634

Closed
jondgilbert wants to merge 1 commit into
NousResearch:mainfrom
jondgilbert:fix/discord-slash-guild-id
Closed

jondgilbert wants to merge 1 commit into
NousResearch:mainfrom
jondgilbert:fix/discord-slash-guild-id

Conversation

@jondgilbert

@jondgilbert jondgilbert commented Aug 21, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes the Discord /new and /reset (and all other simple slash commands) resolving to the wrong session namespace when gateway.multiplex_profiles + profile_routes are in use.

_build_slash_event in plugins/platforms/discord/adapter.py built the SessionSource via self.build_source(...) without guild_id, whereas the regular message path passes it. Since ProfileRoute.matches requires both guild_id AND chat_id, the route never matched — source.profile stayed None, and _resolve_profile_for_key fell back to the active/default profile (agent:main) instead of the routed profile (e.g. agent:work). Result: /new reset the wrong session and the routed profile context never cleared.

This adds _interaction_guild_id (resolves interaction.guild_id, falling back to interaction.channel.guild.id to match the message path) and passes it into build_source in _build_slash_event and the thread-session path.

Related Issue

Fixes #91633

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Changes Made

  • plugins/platforms/discord/adapter.py
    • Added _interaction_guild_id(interaction) helper.
    • _build_slash_event now passes guild_id=self._interaction_guild_id(interaction).
    • Thread-session path (_dispatch_thread_session) passes the same guild_id.
  • tests/gateway/test_discord_slash_commands.py
    • Assert event.source.guild_id in test_run_simple_slash_executes_when_defer_interaction_expired and test_build_slash_event_preserves_thread_context.
    • Added test_build_slash_event_uses_interaction_guild_id covering the interaction.guild_id path.

How to Test

  1. Configure gateway.multiplex_profiles: true + profile_routes entry {platform: discord, guild_id: G, chat_id: C, profile: work}.
  2. Run /new in channel C.
  3. Before: reset targets agent:main:.... After: reset targets agent:work:... and the work conversation clears.

Unit tests:

venv/bin/python -m pytest tests/gateway/test_discord_slash_commands.py -q
# 14 passed

Checklist

Code

  • I have read the Contributing Guide
  • My commit messages follow Conventional Commits
  • I searched for existing PRs — none found
  • My PR contains only changes related to this fix
  • I have run pytest tests/ -q and all tests pass
  • I have added tests for my changes
  • I have tested on my platform: Ubuntu 24.04

Documentation & Housekeeping

  • N/A — no config keys, docs, or architecture changed

Slash commands (/new, /reset, and all other simple slash commands) built
their SessionSource in _build_slash_event without a guild_id, while the
regular message path passes it. With gateway.multiplex_profiles and
profile_routes keyed on guild_id+chat_id, the missing guild_id meant the
route never matched: source.profile stayed empty and the session resolved
to the active/default profile (agent:main) instead of the routed profile
(e.g. agent:work). /new then reset the wrong session namespace and the
routed profile's context never cleared.

Add _interaction_guild_id (resolves interaction.guild_id, falling back to
interaction.channel.guild.id to match the message path) and pass it into
build_source in _build_slash_event and the thread-session path.
@alt-glitch alt-glitch added type/bug Something isn't working comp/plugins Plugin system and bundled plugins platform/discord Discord bot adapter area/config Config system, migrations, profiles P3 Low — cosmetic, nice to have sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades labels Aug 21, 2026
@Enough1122

Copy link
Copy Markdown

AI code review — automated review for reference; please use your judgment.

Correct and minimal: slash-command events now carry the same guild identity as the message path, the resolver degrades gracefully through interaction.guild_id → channel.guild.id → None (DMs stay guild-less), and both construction sites are covered.

  • tests/gateway/test_discord_slash_commands.py:400 — nit (coverage) — the fallback branch (guild_id absent but channel.guild.id present) and the DM shape (resolves to None) aren't pinned; both are cheap SimpleNamespace cases — worth adding since the fallback is exactly what older discord.py object shapes will exercise.

No blocking issues found.

— reviewer-b (automated review)

@teknium1

teknium1 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this PR. Merged via #101247 (0437fe6) on current main — handoffs fail closed and load secrets off-loop; Discord slash commands honor profile_routes.

#101247 won as the consolidated fix because it covers the whole multiplex-profile bug class in one change (with tests) rather than the single symptom addressed here; this PR is superseded by it.
You are credited via Co-authored-by / in the PR body of #101247 as noted there.

If anything from your original change is still missing on main >= 0437fe6, please open a fresh PR/issue against main and tag it. Thanks again.

@teknium1 teknium1 closed this Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have platform/discord Discord bot adapter sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(discord): /new and /reset reset the wrong session under multiplex_profiles

4 participants