diff --git a/gateway/platforms/matrix.py b/gateway/platforms/matrix.py index 06d222fe8..567b7fd0e 100644 --- a/gateway/platforms/matrix.py +++ b/gateway/platforms/matrix.py @@ -2137,13 +2137,33 @@ class MatrixAdapter(BasePlatformAdapter): return False def _strip_mention(self, body: str) -> str: - """Strip the bot's full MXID (``@user:server``) from *body*. + """Remove explicit bot mentions from message body. - The bare localpart is intentionally *not* stripped — it would - mangle file paths like ``/home/hermes/media/file.png``. + Important: only strip explicit mention tokens (``@user:server`` or + ``@localpart``). Do NOT strip bare words matching the bot localpart, + otherwise normal phrases like "Hermes Agent" become "Agent". """ + if not body: + return "" + + # Strip explicit full MXID mentions. if self._user_id: body = body.replace(self._user_id, "") + + # Strip explicit @localpart mentions only (not bare localpart words). + if self._user_id and ":" in self._user_id: + localpart = self._user_id.split(":")[0].lstrip("@") + if localpart: + body = re.sub( + r'(? str: diff --git a/tests/gateway/test_matrix_mention.py b/tests/gateway/test_matrix_mention.py index ff4032505..c85add575 100644 --- a/tests/gateway/test_matrix_mention.py +++ b/tests/gateway/test_matrix_mention.py @@ -159,7 +159,7 @@ class TestStripMention: assert result == "help me" def test_localpart_preserved(self): - """Localpart-only text is no longer stripped — avoids false positives in paths.""" + """Bare localpart (no @) is preserved — avoids false positives in paths.""" result = self.adapter._strip_mention("hermes help me") assert result == "hermes help me" @@ -168,6 +168,15 @@ class TestStripMention: result = self.adapter._strip_mention("read /home/hermes/config.yaml") assert result == "read /home/hermes/config.yaml" + def test_strip_localpart_when_explicit_at_mention(self): + result = self.adapter._strip_mention("@hermes help me") + assert result == "help me" + + def test_does_not_strip_bare_localpart_word(self): + # Regression: plain words like "Hermes Agent" should not be mutated. + result = self.adapter._strip_mention("Hermes Agent") + assert result == "Hermes Agent" + def test_strip_returns_empty_for_mention_only(self): result = self.adapter._strip_mention("@hermes:example.org") assert result == ""