Repository navigation
fix(matrix): mentions should work with displayName labels (with help from Antigravity) - #55393
Conversation
Greptile SummaryThis PR fixes a real bug in the Matrix integration's mention-detection logic: modern Matrix clients (Element Desktop, Element X) format Key observations:
Confidence Score: 4/5Safe to merge; the fix is correctly scoped and introduces no security regression, but unit test coverage for the new path is missing. The logic change is minimal and correct — the MXID href gate still enforces the real security boundary, and the display name is only used as an additional label match after that check passes. The conditional fetch in the handler avoids unnecessary async calls for plain-text messages, and existing tests that assert getMemberDisplayName is not called remain valid. The only gap is the absence of unit tests for the new displayName matching scenario, which lowers confidence slightly from 5. extensions/matrix/src/matrix/monitor/mentions.ts — the new displayName code path has no unit test coverage in mentions.test.ts.
|
| Filename | Overview |
|---|---|
| extensions/matrix/src/matrix/monitor/mentions.ts | Adds optional displayName parameter threaded through resolveMentions → checkFormattedBodyMention → isVisibleMentionLabel. Logic is correct; MXID href check still runs first. Minor redundant toLowerCase on the display name candidate. No unit tests for the new path. |
| extensions/matrix/src/matrix/monitor/handler.ts | Conditionally fetches selfDisplayName via getMemberDisplayName only when formatted_body is present, then passes it into resolveMentions. Guard prevents unnecessary async call for plain-text messages; existing tests remain valid. |
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/mentions.ts
Line: 47
Comment:
**Redundant `.toLowerCase()` on already-normalized value**
`extractVisibleMentionText` calls `normalizeVisibleMentionText`, which already applies `.toLowerCase()` before returning. The value is then lowercased a second time by the `.map((value) => value.toLowerCase())` at the end of the `candidates` chain. This is harmless but redundant and may be mildly confusing to future readers. Since the other candidates need the map, consider documenting that this entry is already normalized.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/mentions.ts
Line: 105-110
Comment:
**No unit tests added for the new `displayName` matching path**
`mentions.test.ts` has good coverage for the existing mention strategies (MXID, localpart, regex, room mentions), but has no tests at all for the new `displayName` parameter introduced in this PR. Without a test, the core scenario this PR fixes — a modern Matrix client formatting the mention anchor label as the bot's display name — has no regression guardrail.
Consider adding at minimum two tests to `mentions.test.ts`:
1. A positive case: `formatted_body` with a matrix.to link pointing to the bot's MXID whose visible label equals the bot's `displayName` → `wasMentioned` should be `true`.
2. A negative/security case: same label matching `displayName`, but the `href` targets a *different* user's MXID → `wasMentioned` should be `false` (to confirm the MXID gate still holds).
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "Fixed testing issue:" | Re-trigger Greptile
| @@ -43,6 +44,7 @@ function isVisibleMentionLabel(params: { | |||
| params.userId.trim().toLowerCase(), | |||
| localpart, | |||
| localpart ? `@${localpart}` : null, | |||
| params.displayName ? extractVisibleMentionText(params.displayName) : null, | |||
There was a problem hiding this comment.
Redundant
.toLowerCase() on already-normalized value
extractVisibleMentionText calls normalizeVisibleMentionText, which already applies .toLowerCase() before returning. The value is then lowercased a second time by the .map((value) => value.toLowerCase()) at the end of the candidates chain. This is harmless but redundant and may be mildly confusing to future readers. Since the other candidates need the map, consider documenting that this entry is already normalized.
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/mentions.ts
Line: 47
Comment:
**Redundant `.toLowerCase()` on already-normalized value**
`extractVisibleMentionText` calls `normalizeVisibleMentionText`, which already applies `.toLowerCase()` before returning. The value is then lowercased a second time by the `.map((value) => value.toLowerCase())` at the end of the `candidates` chain. This is harmless but redundant and may be mildly confusing to future readers. Since the other candidates need the map, consider documenting that this entry is already normalized.
How can I resolve this? If you propose a fix, please make it concise.| export function resolveMentions(params: { | ||
| content: RoomMessageEventContent; | ||
| userId?: string | null; | ||
| displayName?: string | null; | ||
| text?: string; | ||
| mentionRegexes: RegExp[]; |
There was a problem hiding this comment.
No unit tests added for the new
displayName matching path
mentions.test.ts has good coverage for the existing mention strategies (MXID, localpart, regex, room mentions), but has no tests at all for the new displayName parameter introduced in this PR. Without a test, the core scenario this PR fixes — a modern Matrix client formatting the mention anchor label as the bot's display name — has no regression guardrail.
Consider adding at minimum two tests to mentions.test.ts:
- A positive case:
formatted_bodywith a matrix.to link pointing to the bot's MXID whose visible label equals the bot'sdisplayName→wasMentionedshould betrue. - A negative/security case: same label matching
displayName, but thehreftargets a different user's MXID →wasMentionedshould befalse(to confirm the MXID gate still holds).
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/mentions.ts
Line: 105-110
Comment:
**No unit tests added for the new `displayName` matching path**
`mentions.test.ts` has good coverage for the existing mention strategies (MXID, localpart, regex, room mentions), but has no tests at all for the new `displayName` parameter introduced in this PR. Without a test, the core scenario this PR fixes — a modern Matrix client formatting the mention anchor label as the bot's display name — has no regression guardrail.
Consider adding at minimum two tests to `mentions.test.ts`:
1. A positive case: `formatted_body` with a matrix.to link pointing to the bot's MXID whose visible label equals the bot's `displayName` → `wasMentioned` should be `true`.
2. A negative/security case: same label matching `displayName`, but the `href` targets a *different* user's MXID → `wasMentioned` should be `false` (to confirm the MXID gate still holds).
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
This coverage has now been implemented
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 06d6195dbd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -43,6 +44,7 @@ function isVisibleMentionLabel(params: { | |||
| params.userId.trim().toLowerCase(), | |||
| localpart, | |||
| localpart ? `@${localpart}` : null, | |||
| params.displayName ? extractVisibleMentionText(params.displayName) : null, | |||
There was a problem hiding this comment.
Decode HTML entities in display-name mention matching
The new display-name candidate path compares extractVisibleMentionText(params.displayName) against anchor text without decoding HTML entities, so mentions fail when a display name contains escapable characters. In Matrix formatted HTML, a label like R&D Bot is typically rendered as R&D Bot; this now normalizes to r&d bot for the visible label but r&d bot for the room display name, causing resolveMentions to miss real mentions in requireMention rooms for those users.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I feel this is out of scope for this patch. Bots with HTML entities in their display name can be addressed in future work.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6be782a4eb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const selfDisplayName = content.formatted_body | ||
| ? await getMemberDisplayName(roomId, selfUserId).catch(() => undefined) | ||
| : undefined; |
There was a problem hiding this comment.
Avoid per-message self profile lookups for formatted_body mentions
The new selfDisplayName assignment now awaits getMemberDisplayName(roomId, selfUserId) for every event that has formatted_body, even before mention gating. In extensions/matrix/src/matrix/monitor/room-info.ts, getMemberDisplayName only caches when displayname exists and returns userId without caching on missing state or errors, so rooms where the bot has no room display name (or state reads fail) will trigger a remote member-state lookup on every formatted message. This adds avoidable network latency and can throttle message processing in busy rooms.
Useful? React with 👍 / 👎.
6be782a to
e12b024
Compare
e12b024 to
8a4520a
Compare
- Newer matrix clients use displayName inside the anchor text - Previously it only checked for a match on MXID or localpart
- We previously triggered expect(getMemberDisplayName).not.toHaveBeenCalled(); - We now only fetch the displayName when the message contains a formatted_body, as we only need to search those messages for mentions using displayName
d76614a to
c6df37c
Compare
|
Merged via squash.
Thanks @nickludlam! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c6df37ce14
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…from Antigravity) (openclaw#55393) Merged via squash. Prepared head SHA: c6df37c Co-authored-by: nickludlam <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
Summary
Matrix direct mentions are parsed from HTML like
<a href="https://matrix.to/#/@botname:example.org">label</a>The current system expects direct mentions to use localpart (botname) or MXID (@botName:example.org) as the anchor's label
Newer Matrix clients will format mentions using the displayName (Bot Name) as the label
This bug prevented Matrix groups where a setting of
requireMention: truewas used from working correctlyThe
checkFormattedBodyMention()method now takes an additionaldisplayNameparameter to allow for this to match correctlyProblem:
Why it matters: Without this you cannot talk to bots in shared rooms correctly on Matrix where
requireMentionis setWhat changed: displayName has been added as a parameter to include it in the detection system
What did NOT change (scope boundary): Nothing else in Matrix was touched
I've attached a file which logged out the variables as they are seen when a message with a mention comes through which I used during debugging.
matrix_mention_detection_failure.txt
Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause / Regression History (if applicable)
git blame, prior PR, issue, or refactor if known):Regression Test Plan (if applicable)
N/A
User-visible / Behavior Changes
None
Security Impact (required)
Yes, explain risk + mitigation:Repro + Verification
Environment
Steps
requireMention: trueExpected
Actual
Evidence
Attach at least one:
Human Verification (required)
What you personally verified (not just CI), and how:
Review Conversations
If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.
Compatibility / Migration
Risks and Mitigations
None