Skip to content

[Bug fix] Matrix: include sender name in group message envelopes (#27355)#27401

Closed
koushikxd wants to merge 2 commits into
openclaw:mainfrom
koushikxd:fix/matrix-group-sender-identity
Closed

[Bug fix] Matrix: include sender name in group message envelopes (#27355)#27401
koushikxd wants to merge 2 commits into
openclaw:mainfrom
koushikxd:fix/matrix-group-sender-identity

Conversation

@koushikxd

Copy link
Copy Markdown

Fixes the bug where the bot resolves "me/I" to the wrong user in Matrix group chats (#27355).

What changed

In extensions/matrix/src/matrix/monitor/handler.ts, the message body passed to the session transcript was built using formatAgentEnvelope, which put only the room name in the header — no sender. So the LLM couldn't tell who sent which message in shared group sessions and fell back to whoever had messaged last.

Switched to formatInboundEnvelope with chatType + sender params, which prepends the sender's display name to the body for non-DM messages. This is exactly how Telegram, Discord, and Signal already handle it.

Before:
[Matrix MyRoom 08:25 UTC] show me my recent code commits

After:
[Matrix MyRoom 08:25 UTC] Bu: show me my recent code commits

DMs are unaffected (chatType: "direct" skips the sender prefix). Thread messages also correctly get the sender name.

Testing

  • Manually verified with a 2-user Matrix group room (groupPolicy: allowlist, requireMention: true)
  • DM behavior unchanged
  • pnpm build && pnpm check && pnpm test passes

@openclaw-barnacle openclaw-barnacle Bot added channel: matrix Channel integration: matrix size: XS labels Feb 26, 2026
@greptile-apps

greptile-apps Bot commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes Matrix group message sender disambiguation by switching from formatAgentEnvelope to formatInboundEnvelope, which prepends sender names to non-DM messages. This aligns Matrix with how Telegram, Discord, and Slack handle group messages.

Changes:

  • Replaced formatAgentEnvelope with formatInboundEnvelope at line 501-510
  • Added sender parameter with name and username fields (username extracted from Matrix ID)
  • Added chatType parameter to differentiate message contexts

Issue found:

  • Thread messages use chatType: "thread", which is not recognized by normalizeChatType() and gets treated as undefined, causing isDirect = true. This means thread messages won't get sender names prepended, partially breaking the fix. Should use "channel" for threads (matching Discord/Slack pattern).

Confidence Score: 2/5

  • Has a logical error that partially breaks the intended fix for thread messages
  • While the core approach is correct (switching to formatInboundEnvelope to add sender names), the implementation has a critical bug: using chatType: "thread" for thread messages causes them to be treated as direct messages internally, preventing the sender name from being prepended. This means thread messages in Matrix groups will still suffer from the same disambiguation issue the PR aims to fix.
  • extensions/matrix/src/matrix/monitor/handler.ts - Line 508 needs the chatType logic corrected

Last reviewed commit: 1a2742e

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

channel: "Matrix",
from: envelopeFrom,
timestamp: eventTs ?? undefined,
previousTimestamp,
envelope: envelopeOptions,
body: textWithId,
chatType: threadRootId ? "thread" : isDirectMessage ? "direct" : "channel",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"thread" is not a recognized chat type and will be normalized to undefined by normalizeChatType(), causing thread messages to be treated as direct messages (no sender prefix). Use "channel" instead, matching Discord and Slack's pattern

Suggested change
chatType: threadRootId ? "thread" : isDirectMessage ? "direct" : "channel",
chatType: isDirectMessage ? "direct" : "channel",
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/handler.ts
Line: 508

Comment:
`"thread"` is not a recognized chat type and will be normalized to `undefined` by `normalizeChatType()`, causing thread messages to be treated as direct messages (no sender prefix). Use `"channel"` instead, matching Discord and Slack's pattern

```suggestion
        chatType: isDirectMessage ? "direct" : "channel",
```

How can I resolve this? If you propose a fix, please make it concise.

@koushikxd koushikxd force-pushed the fix/matrix-group-sender-identity branch from 1a2742e to a0aa104 Compare February 26, 2026 10:43
@steipete

Copy link
Copy Markdown
Contributor

Landed to main via maintainer fast-path (commit 8483e01a6) and includes the requested Matrix sender-label + BodyForAgent fixes and regression tests.

Closing this PR as superseded-by-landed.

Thanks @koushikxd for the report + patch direction.

@koushikxd

koushikxd commented Feb 26, 2026

Copy link
Copy Markdown
Author

@steipete Awesome, thanks for picking it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: matrix Channel integration: matrix size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants