Skip to content

The agent that grows with you

fix(matrix): propagate sender + reply context to media MessageEvents too

kshitije8511efe758f
diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py
index e38a05d5aca..e3ff17654d6 100644
--- a/plugins/platforms/matrix/adapter.py
+++ b/plugins/platforms/matrix/adapter.py
@@ -3682,6 +3682,36 @@ class MatrixAdapter(BasePlatformAdapter):
return
body, is_dm, chat_type, thread_id, display_name, source = ctx
+ # Reply-to detection (mirrors _handle_text_message).
+ reply_to = None
+ in_reply_to = relates_to.get("m.in_reply_to", {})
+ if in_reply_to:
+ reply_to = in_reply_to.get("event_id")
+
+ reply_to_text: Optional[str] = None
+ reply_to_author_id: Optional[str] = None
+ reply_to_author_name: Optional[str] = None
+ if reply_to and body.startswith("> "):
+ reply_to_text, reply_to_author_id = _extract_reply_fallback(body)
+ lines = body.split("\n")
+ stripped = []
+ past_fallback = False
+ for line in lines:
+ if not past_fallback:
+ if line.startswith("> ") or line == ">":
+ continue
+ if line == "":
+ past_fallback = True
+ continue
+ past_fallback = True
+ stripped.append(line)
+ body = "\n".join(stripped) if stripped else body
+
+ if reply_to_author_id:
+ reply_to_author_name = await self._get_display_name(
+ room_id, reply_to_author_id
+ )
+
if msgtype == "m.image" and _looks_like_matrix_image_filename(body):
body = ""
@@ -3701,6 +3731,12 @@ class MatrixAdapter(BasePlatformAdapter):
message_id=event_id,
media_urls=media_urls,
media_types=media_types,
+ reply_to_message_id=reply_to,
+ reply_to_text=reply_to_text,
+ reply_to_author_id=reply_to_author_id,
+ reply_to_author_name=reply_to_author_name,
+ user_id=sender,
+ user_name=display_name,
)
await self.handle_message(msg_event)
diff --git a/tests/gateway/test_matrix_message_event_metadata.py b/tests/gateway/test_matrix_message_event_metadata.py
index 1fba752e1f7..ad0cea9756b 100644
--- a/tests/gateway/test_matrix_message_event_metadata.py
+++ b/tests/gateway/test_matrix_message_event_metadata.py
@@ -197,3 +197,52 @@ async def test_non_reply_message_has_no_reply_context(monkeypatch):
assert msg.reply_to_text is None
assert msg.reply_to_author_id is None
assert msg.reply_to_author_name is None
+
+
+# ---------------------------------------------------------------------------
+# Media message sender + reply context (sibling gap fix)
+# ---------------------------------------------------------------------------
+
+
+async def test_media_message_carries_sender_and_reply_context(monkeypatch):
+ """Media messages must carry sender + reply context, same as text messages.
+
+ A user replying to a photo in Matrix produces a media event with the
+ same inline ``> <@user:server> ...`` reply fallback. The media handler
+ must parse it and populate reply_to_* fields, not just the text handler.
+ """
+ adapter = _make_adapter(monkeypatch=monkeypatch)
+ adapter._startup_ts = time.time() - 10
+
+ # Simulate a reply to a media message — the body carries the reply
+ # fallback prefix. We don't need a real mxc:// URL since the adapter
+ # validates URL format and rejects non-MXC; use a valid mxc:// URL.
+ reply_body = "> <@erin:example.org> nice photo\n\n"
+ event = SimpleNamespace(
+ sender="@frank:example.org",
+ event_id="$media_evt1",
+ room_id="!room1:example.org",
+ timestamp=int(time.time() * 1000),
+ content={
+ "body": reply_body,
+ "msgtype": "m.image",
+ "url": "mxc://example.org/abc123",
+ "info": {"mimetype": "image/png", "size": 1024},
+ "m.relates_to": {"m.in_reply_to": {"event_id": "$target_media1"}},
+ },
+ )
+ await adapter._on_room_message(event)
+
+ adapter.handle_message.assert_awaited_once()
+ msg = adapter.handle_message.await_args.args[0]
+
+ # Sender metadata must be present on media messages too.
+ assert msg.user_id == "@frank:example.org"
+ assert msg.user_name == "frank"
+ # Reply context must be parsed from the fallback.
+ assert msg.reply_to_message_id == "$target_media1"
+ assert msg.reply_to_text is not None
+ assert "nice photo" in msg.reply_to_text
+ assert msg.reply_to_author_id == "@erin:example.org"
+ assert msg.reply_to_author_name == "erin"