Skip to content

Commit 5ac6b60

Browse files
committed
docs(channels): document bot loop protection
1 parent 4785a07 commit 5ac6b60

9 files changed

Lines changed: 216 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Docs: https://docs.openclaw.ai
179179
- CLI: strip generic OSC terminal escape payloads from sanitized output fields, preventing clipboard/title escape bodies from leaking into commitment tables and other terminal-safe text. Thanks @shakkernerd.
180180
- Codex app-server: match connector-backed plugin approval elicitations by stable connector id so enabled destructive actions no longer fall through to display-name-only rejection.
181181
- Build: replace selected build utility `tsx` preloads with Node native type stripping so Node 26 build paths no longer emit `DEP0205` module loader deprecation warnings. (#78584) Thanks @keshavbotagent.
182+
- Channels/loop-guard: enforce shared per-pair bot loop protection in the core channel-turn kernel, with Discord, Slack, Matrix, and Google Chat supplying bot-pair facts where they can reliably identify accepted bot-authored messages. The generic guard keys on `(scope, conversation, participant pair)`, suppresses every additional bot-to-bot event in either direction once a pair crosses the configured budget, and lifts suppression after `cooldownSeconds`. Defaults are `maxEventsPerWindow: 20`, `windowSeconds: 60`, and `cooldownSeconds: 60` whenever a channel lets bot-authored messages reach dispatch; they can be set globally via `channels.defaults.botLoopProtection` and overridden per channel/account or supported per-conversation config. Fixes #58789. Thanks @pandadev66.
182183
- Media generation: honor configured music and video generation timeouts when tool calls omit `timeoutMs`, matching image generation behavior. (#80687)
183184
- CLI/update/status: label beta-channel plugin fallback and model-pricing refresh failures as warnings, keeping mixed beta/latest plugin cohorts visible without making core update or Gateway reachability look failed. Fixes #80689. Thanks @BKF-Gitty.
184185
- Doctor/plugins: relink managed npm plugin `openclaw` peer dependencies during `doctor --fix`, while refusing to follow package-local `node_modules` symlinks outside the plugin package. (#77412) Thanks @TheCrazyLex.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
summary: "Bot-to-bot loop protection defaults and channel overrides"
3+
read_when:
4+
- Configuring bot-authored channel messages
5+
- Tuning bot-to-bot loop protection
6+
title: "Bot loop protection"
7+
sidebarTitle: "Bot loop protection"
8+
---
9+
10+
# Bot loop protection
11+
12+
OpenClaw can accept messages written by other bots on channels that support `allowBots`.
13+
When that path is enabled, pair loop protection prevents two bot identities from
14+
replying to each other indefinitely.
15+
16+
The guard is enforced by the core channel-turn kernel. Each supporting channel
17+
maps its own inbound event into generic facts: account or scope, conversation id,
18+
sender bot id, and receiver bot id. Core then tracks the participant pair in both
19+
directions, applies a sliding-window budget, and suppresses the pair during a
20+
cooldown after the budget is exceeded.
21+
22+
## Defaults
23+
24+
Pair loop protection is active when a channel lets bot-authored messages reach
25+
dispatch. Built-in defaults are:
26+
27+
- `maxEventsPerWindow: 20` - a bot pair can exchange 20 events within the window
28+
- `windowSeconds: 60` - sliding window length
29+
- `cooldownSeconds: 60` - suppression time after the pair exceeds the budget
30+
31+
The guard does not affect normal human-authored messages, single-bot deployments,
32+
self-message filtering, or one-shot bot replies that stay under the budget.
33+
34+
## Configure shared defaults
35+
36+
Set `channels.defaults.botLoopProtection` once to give every supporting channel
37+
the same baseline. Channel and account overrides can still tune individual
38+
surfaces.
39+
40+
```json5
41+
{
42+
channels: {
43+
defaults: {
44+
botLoopProtection: {
45+
maxEventsPerWindow: 20,
46+
windowSeconds: 60,
47+
cooldownSeconds: 60,
48+
},
49+
},
50+
},
51+
}
52+
```
53+
54+
Set `enabled: false` only when your channel policy intentionally allows
55+
bot-to-bot conversations without automatic suppression.
56+
57+
## Override per channel or account
58+
59+
Supporting channels layer their own config over the shared default. Precedence is:
60+
61+
- `channels.<channel>.<room-or-space>.botLoopProtection`, when the channel supports per-conversation overrides
62+
- `channels.<channel>.accounts.<account>.botLoopProtection`, when the channel supports accounts
63+
- `channels.<channel>.botLoopProtection`, when the channel supports top-level defaults
64+
- `channels.defaults.botLoopProtection`
65+
- built-in defaults
66+
67+
```json5
68+
{
69+
channels: {
70+
defaults: {
71+
botLoopProtection: {
72+
maxEventsPerWindow: 20,
73+
},
74+
},
75+
discord: {
76+
botLoopProtection: {
77+
maxEventsPerWindow: 8,
78+
},
79+
accounts: {
80+
molty: {
81+
allowBots: "mentions",
82+
botLoopProtection: {
83+
maxEventsPerWindow: 5,
84+
cooldownSeconds: 90,
85+
},
86+
},
87+
},
88+
},
89+
slack: {
90+
allowBots: "mentions",
91+
botLoopProtection: {
92+
maxEventsPerWindow: 8,
93+
},
94+
},
95+
matrix: {
96+
allowBots: "mentions",
97+
groups: {
98+
"!roomid:example.org": {
99+
botLoopProtection: {
100+
maxEventsPerWindow: 5,
101+
},
102+
},
103+
},
104+
},
105+
googlechat: {
106+
allowBots: true,
107+
groups: {
108+
"spaces/AAAA": {
109+
botLoopProtection: {
110+
maxEventsPerWindow: 5,
111+
},
112+
},
113+
},
114+
},
115+
},
116+
}
117+
```
118+
119+
## Channel support
120+
121+
- Discord: native `author.bot` facts, keyed by Discord account, channel, and bot pair.
122+
- Slack: native `bot_id` facts for accepted bot-authored messages, keyed by Slack account, channel, and bot pair.
123+
- Matrix: configured Matrix bot accounts, keyed by Matrix account, room, and configured bot pair.
124+
- Google Chat: native `sender.type=BOT` facts for accepted bot-authored messages, keyed by account, space, and bot pair.
125+
126+
Channels that do not expose a reliable inbound bot identity keep using their
127+
normal self-message and access-policy filters. They should not opt into this
128+
guard until they can identify both participants in the bot pair.
129+
130+
See [SDK runtime](/plugins/sdk-runtime#reusable-runtime-utilities) for plugin
131+
implementation details.

‎docs/channels/discord.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,10 +1569,39 @@ openclaw logs --follow
15691569
If you set `channels.discord.allowBots=true`, use strict mention and allowlist rules to avoid loop behavior.
15701570
Prefer `channels.discord.allowBots="mentions"` to only accept bot messages that mention the bot.
15711571

1572+
OpenClaw also ships shared [bot loop protection](/channels/bot-loop-protection). Whenever `allowBots` lets bot-authored messages reach dispatch, Discord maps the inbound event to `(account, channel, bot pair)` facts and the generic pair guard suppresses the pair after it crosses the configured event budget. The guard prevents runaway two-bot loops that previously had to be stopped by Discord rate limits; it does not affect single-bot deployments or one-shot bot replies that stay under the budget.
1573+
1574+
Default settings (active when `allowBots` is set):
1575+
1576+
- `maxEventsPerWindow: 20` -- bot pair can exchange 20 messages within the sliding window
1577+
- `windowSeconds: 60` -- sliding window length
1578+
- `cooldownSeconds: 60` -- once the budget trips, every additional bot-to-bot message in either direction is dropped for one minute
1579+
1580+
Configure the shared default once under `channels.defaults.botLoopProtection`, then override Discord when a legitimate workflow needs more headroom. Precedence is:
1581+
1582+
- `channels.discord.accounts.<account>.botLoopProtection`
1583+
- `channels.discord.botLoopProtection`
1584+
- `channels.defaults.botLoopProtection`
1585+
- built-in defaults
1586+
1587+
Discord uses the generic `maxEventsPerWindow`, `windowSeconds`, and `cooldownSeconds` keys.
1588+
15721589
```json5
15731590
{
15741591
channels: {
1592+
defaults: {
1593+
botLoopProtection: {
1594+
maxEventsPerWindow: 20,
1595+
windowSeconds: 60,
1596+
cooldownSeconds: 60,
1597+
},
1598+
},
15751599
discord: {
1600+
// Optional Discord-wide override. Account blocks override individual
1601+
// fields and inherit omitted fields from here.
1602+
botLoopProtection: {
1603+
maxEventsPerWindow: 4,
1604+
},
15761605
accounts: {
15771606
mantis: {
15781607
// Mantis listens to other bots only when they mention her.
@@ -1585,6 +1614,12 @@ openclaw logs --follow
15851614
// Lets Molty write "@Mantis" and send a real Discord mention.
15861615
Mantis: "MANTIS_DISCORD_USER_ID",
15871616
},
1617+
botLoopProtection: {
1618+
// Allow up to five messages per minute before suppressing the pair.
1619+
maxEventsPerWindow: 5,
1620+
windowSeconds: 60,
1621+
cooldownSeconds: 90,
1622+
},
15881623
},
15891624
},
15901625
},

‎docs/channels/googlechat.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Use these identifiers for delivery and allowlists:
185185
audience: "https://gateway.example.com/googlechat",
186186
webhookPath: "/googlechat",
187187
botUser: "users/1234567890", // optional; helps mention detection
188+
allowBots: false,
188189
dm: {
189190
policy: "pairing",
190191
allowFrom: ["users/1234567890"],
@@ -216,6 +217,7 @@ Notes:
216217
- Message actions expose `send` for text and `upload-file` for explicit attachment sends. `upload-file` accepts `media` / `filePath` / `path` plus optional `message`, `filename`, and thread targeting.
217218
- `typingIndicator` supports `none`, `message` (default), and `reaction` (reaction requires user OAuth).
218219
- Attachments are downloaded through the Chat API and stored in the media pipeline (size capped by `mediaMaxMb`).
220+
- Bot-authored Google Chat messages are ignored by default. If you intentionally set `allowBots: true`, accepted bot-authored messages use shared [bot loop protection](/channels/bot-loop-protection). Configure `channels.defaults.botLoopProtection`, then override with `channels.googlechat.botLoopProtection` or `channels.googlechat.groups.<space>.botLoopProtection` when one space needs a different budget.
219221

220222
Secrets reference details: [Secrets Management](/gateway/secrets).
221223

‎docs/channels/index.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Text is supported everywhere; media and reactions vary by channel.
1818
- WhatsApp setup is install-on-demand: onboarding can show the setup flow before
1919
the plugin package is installed, and the Gateway loads the external
2020
ClawHub/npm plugin only when the channel is actually active.
21+
- Channels that accept bot-authored inbound messages can use shared
22+
[bot loop protection](/channels/bot-loop-protection) to prevent bot pairs from
23+
replying to each other indefinitely.
2124

2225
## Supported channels
2326

‎docs/channels/matrix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Use `allowBots` when you intentionally want inter-agent Matrix traffic:
266266
- `allowBots: true` accepts messages from other configured Matrix bot accounts in allowed rooms and DMs.
267267
- `allowBots: "mentions"` accepts those messages only when they visibly mention this bot in rooms. DMs are still allowed.
268268
- `groups.<room>.allowBots` overrides the account-level setting for one room.
269+
- Accepted configured-bot messages use shared [bot loop protection](/channels/bot-loop-protection). Configure `channels.defaults.botLoopProtection`, then override with `channels.matrix.botLoopProtection` or `channels.matrix.groups.<room>.botLoopProtection` when one room needs a different budget.
269270
- OpenClaw still ignores messages from the same Matrix user ID to avoid self-reply loops.
270271
- Matrix does not expose a native bot flag here; OpenClaw treats "bot-authored" as "sent by another configured Matrix account on this OpenClaw gateway".
271272

‎docs/channels/slack.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,8 @@ Current Slack message actions include `send`, `upload-file`, `download-file`, `r
920920

921921
`allowBots` is conservative for channels and private channels: bot-authored room messages are accepted only when the sending bot is explicitly listed in that room's `users` allowlist, or when at least one explicit Slack owner ID from `channels.slack.allowFrom` is currently a room member. Wildcards and display-name owner entries do not satisfy owner presence. Owner presence uses Slack `conversations.members`; make sure the app has the matching read scope for the room type (`channels:read` for public channels, `groups:read` for private channels). If the member lookup fails, OpenClaw drops the bot-authored room message.
922922

923+
Accepted bot-authored Slack messages use shared [bot loop protection](/channels/bot-loop-protection). Configure `channels.defaults.botLoopProtection` for the default budget, then override with `channels.slack.botLoopProtection` or `channels.slack.channels.<id>.botLoopProtection` when a workspace or channel needs a different limit.
924+
923925
</Tab>
924926
</Tabs>
925927

‎docs/gateway/config-channels.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ WhatsApp runs through the gateway's web channel (Baileys Web). It starts automat
335335
- Use `user:<id>` (DM) or `channel:<id>` (guild channel) for delivery targets; bare numeric IDs are rejected.
336336
- Guild slugs are lowercase with spaces replaced by `-`; channel keys use the slugged name (no `#`). Prefer guild IDs.
337337
- Bot-authored messages are ignored by default. `allowBots: true` enables them; use `allowBots: "mentions"` to only accept bot messages that mention the bot (own messages still filtered).
338+
- Channels that support bot-authored inbound messages can use shared [bot loop protection](/channels/bot-loop-protection). Set `channels.defaults.botLoopProtection` for baseline pair budgets, then override the channel or account only when one surface needs different limits.
338339
- `channels.discord.guilds.<id>.ignoreOtherMentions` (and channel overrides) drops messages that mention another user or role but not the bot (excluding @everyone/@here).
339340
- `channels.discord.mentionAliases` maps stable outbound `@handle` text to Discord user IDs before sending, so known teammates can be mentioned deterministically even when the transient directory cache is empty. Per-account overrides live under `channels.discord.accounts.<accountId>.mentionAliases`.
340341
- `maxLinesPerMessage` (default 17) splits tall messages even when under 2000 chars.

‎docs/plugins/sdk-runtime.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ Internal OpenClaw runtime code has the same direction: load config once at the C
5454

5555
Provider and channel execution paths must use the active runtime config snapshot, not a file snapshot returned for config readback or editing. File snapshots preserve source values such as SecretRef markers for UI and writes; provider callbacks need the resolved runtime view. When a helper may be called with either the active source snapshot or the active runtime snapshot, route through `selectApplicableRuntimeConfig()` before reading credentials.
5656

57+
## Reusable runtime utilities
58+
59+
Use the channel-turn `botLoopProtection` facts for bot-authored inbound messages. Core applies the shared in-memory sliding-window guard before session record and dispatch, without tying the policy to one channel. The guard tracks `(scopeId, conversationId, participant pair)` keys, counts both directions of a pair together, applies a cooldown once the window budget is exceeded, and prunes inactive entries opportunistically.
60+
61+
Channel plugins that expose this behavior to operators should prefer the shared `channels.defaults.botLoopProtection` shape for baseline budgets, then layer channel/provider-specific overrides on top. The shared config uses seconds because it is user-facing:
62+
63+
```typescript
64+
type ChannelBotLoopProtectionConfig = {
65+
enabled?: boolean;
66+
maxEventsPerWindow?: number;
67+
windowSeconds?: number;
68+
cooldownSeconds?: number;
69+
};
70+
```
71+
72+
Pass normalized bot-pair facts with the resolved turn. Core resolves defaults, unit conversion, and `enabled` semantics:
73+
74+
```typescript
75+
return {
76+
channel: "example",
77+
routeSessionKey,
78+
storePath,
79+
ctxPayload,
80+
recordInboundSession,
81+
runDispatch,
82+
botLoopProtection: {
83+
scopeId: "account-1",
84+
conversationId: "channel-1",
85+
senderId: "bot-a",
86+
receiverId: "bot-b",
87+
config: channelConfig.botLoopProtection,
88+
defaultsConfig: runtimeConfig.channels?.defaults?.botLoopProtection,
89+
defaultEnabled: allowBotsMode !== "off",
90+
},
91+
};
92+
```
93+
94+
Use `openclaw/plugin-sdk/pair-loop-guard-runtime` directly only for custom
95+
two-party event loops that do not go through the shared channel-turn kernel.
96+
5797
## Runtime namespaces
5898

5999
<AccordionGroup>

0 commit comments

Comments
 (0)