Skip to content

perf(discord): eager multibot detection from msg.author#465

Merged
thepagent merged 1 commit intoopenabdev:mainfrom
chaodu-agent:feat/multibot-mentions-optimize
Apr 19, 2026
Merged

perf(discord): eager multibot detection from msg.author#465
thepagent merged 1 commit intoopenabdev:mainfrom
chaodu-agent:feat/multibot-mentions-optimize

Conversation

@chaodu-agent
Copy link
Copy Markdown
Collaborator

@chaodu-agent chaodu-agent commented Apr 19, 2026

Summary

Builds on #464. Eliminates unnecessary API fetches in multibot-mentions mode.

Problem

In #464, bot_participated_in_thread() fetches up to 200 messages on every unmentioned message in MultibotMentions mode — even in single-bot threads where no other bot will ever be found. This is because cached_involved = true could not short-circuit without knowing other_bot_present.

Solution

Detect multi-bot threads eagerly from msg.author instead of fetching history:

// Before gating logic — zero API call
if in_thread && msg.author.bot && msg.author.id != bot_id {
    cache multibot_threads[thread_id] = now
}

When a bot message arrives in a thread, we already know it is multi-bot. No fetch needed.

This allows bot_participated_in_thread() to short-circuit on cached_involved alone:

// Before (fetched every time for MultibotMentions):
if cached_involved && cached_multibot { return (true, true); }
if cached_involved && mode != MultibotMentions { return (true, false); }
// ↑ MultibotMentions with cached_involved but !cached_multibot → fetch

// After (always short-circuits):
if cached_involved { return (true, cached_multibot); }
// ↑ cached_multibot is populated eagerly from msg.author

Impact

Scenario Before After
Single-bot thread, unmentioned msg Fetch 200 msgs every time Zero fetch (cache hit)
Multi-bot thread, after detection Zero fetch (cache hit) Zero fetch (cache hit)
First bot message in thread Fetch (to check involvement) Fetch (same) + cache multibot

Cold-start note

After bot restart, both caches are empty. If a thread is already multi-bot but the next message is from a user (not a bot), early detection won't trigger and cached_multibot = false. However, cached_involved is also false (cleared on restart), so bot_participated_in_thread will fetch — and the fetch includes other_bot_present detection, which repopulates the cache. This is safe and consistent with #464 behavior — just one fallback fetch on the first message after restart.

Changes (src/discord.rs, +12/-6)

  • Add early multibot detection from msg.author before gating logic
  • Simplify bot_participated_in_thread: cached_involved → return (true, cached_multibot) immediately, no mode-specific branching

Instead of fetching thread history to detect other bots on every
unmentioned message, detect multi-bot threads eagerly from msg.author:
when a bot message arrives in a thread, cache multibot_threads
immediately — zero API calls needed.

This eliminates per-message fetches in MultibotMentions mode for
single-bot threads (which would never find other bots but fetched
every time to check).

- Add early detection before gating: msg.author.bot → cache multibot
- Simplify bot_participated_in_thread: cached_involved → return
  immediately with cached_multibot, no MultibotMentions special-casing

Builds on openabdev#464
@chaodu-agent chaodu-agent requested a review from thepagent as a code owner April 19, 2026 13:12
@github-actions github-actions bot added the closing-soon PR missing Discord Discussion URL — will auto-close in 3 days label Apr 19, 2026
@github-actions
Copy link
Copy Markdown

⚠️ This PR is missing a Discord Discussion URL in the body.

All PRs must reference a prior Discord discussion to ensure community alignment before implementation.

Please edit the PR description to include a link like:

Discord Discussion URL: https://discord.com/channels/...

This PR will be automatically closed in 3 days if the link is not added.

@thepagent thepagent merged commit 4e8d9f2 into openabdev:main Apr 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

closing-soon PR missing Discord Discussion URL — will auto-close in 3 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants