Skip to content

feat(discord): bot turn limits (soft + hard) for bot-to-bot loops#467

Merged
thepagent merged 2 commits intoopenabdev:mainfrom
chaodu-agent:feat/bot-turn-limits
Apr 19, 2026
Merged

feat(discord): bot turn limits (soft + hard) for bot-to-bot loops#467
thepagent merged 2 commits intoopenabdev:mainfrom
chaodu-agent:feat/bot-turn-limits

Conversation

@chaodu-agent
Copy link
Copy Markdown
Collaborator

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

Summary

Closes #466

Add two-layer protection against runaway bot-to-bot mention loops. The existing MAX_CONSECUTIVE_BOT_TURNS is ineffective when bots interleave messages (each bot's own reply breaks the take_while chain, so the counter never reaches the cap).

Design

Soft limit (max_bot_turns, configurable)

  • Default: 20 consecutive bot turns without human intervention
  • When reached: sends warning message, stops responding
  • Human message resets the counter
  • Configurable via max_bot_turns in [discord] config
⚠️ Bot turn limit reached (20/20). A human must reply in this thread to continue bot-to-bot conversation.

Hard limit (HARD_BOT_TURN_LIMIT = 100, not configurable)

  • Absolute cap on bot turns between human interventions
  • Human message resets the counter (same as soft)
  • When reached: sends stop message
🛑 Hard limit reached (100). Bot-to-bot conversation in this thread has been permanently stopped.

How it works

Bot A: @BotB ...     ← soft=1, hard=1
Bot B: @BotA ...     ← soft=2, hard=2
...
Bot B: @BotA ...     ← soft=20 → ⚠️ throttled
Human: continue      ← soft=0, hard=0 (both reset)
Bot A: @BotB ...     ← soft=1, hard=1 (fresh start)

The difference: soft is configurable (default 20), hard is fixed at 100. If someone sets max_bot_turns = 100 or higher, the hard limit is the safety net.

Changes (+63/-0)

  • src/config.rs: add max_bot_turns field (default 20) to DiscordConfig
  • src/discord.rs: add HARD_BOT_TURN_LIMIT = 100 const, bot_turn_counts per-thread (soft, hard) counters, counting + throttle logic (placed after all gating)
  • src/main.rs: initialize bot_turn_counts and max_bot_turns
  • docs/discord.md: document bot turn limits

Why not fix MAX_CONSECUTIVE_BOT_TURNS?

The existing check counts consecutive messages from other bots using take_while. When Bot A and Bot B alternate, each bot's own reply breaks the chain — the counter never exceeds 1. The fundamental issue is the counting method, not the cap value. Our approach counts all bot turns regardless of interleaving.

Related

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:31
@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.

@chaodu-agent chaodu-agent force-pushed the feat/bot-turn-limits branch 2 times, most recently from 59df8f2 to 2191e8b Compare April 19, 2026 13:37
Add two-layer protection against runaway bot-to-bot mention loops:

- Soft limit (max_bot_turns, configurable, default 10): consecutive bot
  turns without human intervention. Sends warning and stops. Human
  message resets the counter.
- Hard limit (HARD_BOT_TURN_LIMIT = 100, not configurable): absolute
  per-thread cap. Permanently stops bot-to-bot conversation.

Both limits send a visible message to the thread so humans and bots
know why the conversation stopped.

This addresses the gap where MAX_CONSECUTIVE_BOT_TURNS is ineffective
for interleaved bot-to-bot mentions (counter resets when own message
breaks the take_while chain).

Closes openabdev#466
@chaodu-agent chaodu-agent force-pushed the feat/bot-turn-limits branch from 2191e8b to cd76d44 Compare April 19, 2026 13:38
@thepagent thepagent merged commit e6c2162 into openabdev:main Apr 19, 2026
9 checks passed
chaodu-agent added a commit to chaodu-agent/openab that referenced this pull request Apr 19, 2026
Human intervention should reset both soft and hard counters.
Previously hard counter never reset, meaning a thread could be
permanently locked out of bot-to-bot conversation even with
human oversight.

Builds on openabdev#467
chaodu-agent added a commit to chaodu-agent/openab that referenced this pull request Apr 19, 2026
Human intervention should reset both soft and hard counters.
Previously hard counter never reset, meaning a thread could be
permanently locked out of bot-to-bot conversation even with
human oversight.

Builds on openabdev#467
chaodu-agent added a commit to chaodu-agent/openab that referenced this pull request Apr 19, 2026
Human intervention should reset both soft and hard counters.
Previously hard counter never reset, meaning a thread could be
permanently locked out of bot-to-bot conversation even with
human oversight.

Extract BotTurnTracker struct for testability:
- on_bot_message() returns TurnResult::{Ok, SoftLimit, HardLimit}
- on_human_message() resets both counters
- 8 unit tests covering all edge cases

Builds on openabdev#467
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.

bug(discord): MAX_CONSECUTIVE_BOT_TURNS ineffective for bot-to-bot mention loops

2 participants