Skip to content

fix: multi-bot accountId isolation in shared gateway#143

Merged
yujiawei merged 3 commits intodmwork-org:mainfrom
Jerry-Xin:fix/multi-bot-accountid-isolation
Apr 1, 2026
Merged

fix: multi-bot accountId isolation in shared gateway#143
yujiawei merged 3 commits intodmwork-org:mainfrom
Jerry-Xin:fix/multi-bot-accountid-isolation

Conversation

@Jerry-Xin
Copy link
Copy Markdown
Collaborator

Problem

When multiple bots share the same OpenClaw Gateway process and are members of the same group, messages/files sent by Bot A could be sent from Bot B's identity (accountId isolation failure).

Root Cause

_groupToAccount (Map<string, string>) used a last-write-wins strategy — whichever bot last processed a group message overwrote the mapping. The handleAction correction logic then replaced the caller's correct accountId with the wrong one.

Trigger Path

  1. Bot B receives a group message → registerGroupToAccount(groupNo, 'bot-b') overwrites Bot A's entry
  2. Bot A sends a message via message tool → handleAction calls resolveAccountForGroup(groupNo) → returns 'bot-b'
  3. Bot A's correct accountId gets "corrected" to Bot B → message sent with Bot B's botToken

Changes

  • Change _groupToAccount from Map<string, string> to Map<string, Set<string>> (1:N mapping)
  • registerGroupToAccount now adds to a Set instead of overwriting
  • resolveAccountForGroup returns undefined when multiple bots share a group (ambiguous)
  • handleAction correction only applies when a single bot definitively owns the group
  • 8 new test cases covering single-bot, multi-bot, and handleAction correction scenarios

Testing

  • All 310 tests pass (including 8 new isolation tests)
  • Verified on live deployment: both text and file attachments sent by correct bot identity in multi-bot group

Fixes: dmwork-org/dmwork-PM#183

When multiple bots share the same OpenClaw Gateway process and are
members of the same group, _groupToAccount (Map<string, string>) used
a last-write-wins strategy — whichever bot last processed a group
message overwrote the mapping. The handleAction correction logic then
replaced the caller's correct accountId with the wrong one, causing
messages/files to be sent from the wrong bot's identity.

Changes:
- Change _groupToAccount from Map<string, string> to Map<string, Set<string>>
  to support 1:N group-to-account mapping
- registerGroupToAccount now adds to a Set instead of overwriting
- resolveAccountForGroup returns undefined when multiple bots share a
  group (ambiguous), only resolves for single-bot groups
- handleAction correction logic uses resolveAccountForGroup and only
  corrects when a definitive answer exists (single bot owns the group)
- Add 8 test cases covering single-bot, multi-bot, and handleAction
  correction scenarios

Fixes: dmwork-org/dmwork-PM#183
Copy link
Copy Markdown
Collaborator

@yujiawei yujiawei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI Fix — Type Check Errors

核心逻辑没问题 👍,就是测试文件有两个类型问题导致 CI 挂了:

1. ctx 缺少 channel 属性

ChannelMessageActionContext 要求 channel 字段,三个 handleAction 测试的 ctx 都要补上:

const ctx = {
  accountId: "wrongBot",
  action: "send",
  channel: "dmwork",  // ← 补这个
  // ... 其余不变
};

2. dmworkPlugin.actions 可能为 undefined

三处 dmworkPlugin.actions!.handleAction(ctx) 需要加非空断言 !(或者统一在 describe 外面断言一次):

// 方式 A:每次调用加 !
await dmworkPlugin.actions!.handleAction(ctx);

// 方式 B:describe 开头断言一次(推荐)
const handleAction = dmworkPlugin.actions?.handleAction;
assert(handleAction, "actions should be defined");

两处改完就能过 CI。

Jerry-Xin and others added 2 commits April 1, 2026 14:36
- Add missing 'channel: "dmwork"' to all handleAction test ctx objects
- Add non-null assertion (!) on handleAction calls for type safety
Copy link
Copy Markdown
Collaborator

@yujiawei yujiawei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI green ✅ 310/310 tests pass. 逻辑正确,类型问题已修。

@yujiawei yujiawei merged commit ca2fdb0 into dmwork-org:main Apr 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [OpenClaw/Adapter] 多 Bot 共享 Gateway 时消息串台,accountId 隔离失效

2 participants