From 409aed7a9eef0b2327b5a1fea838ff4865f6c43d Mon Sep 17 00:00:00 2001 From: Offending Commit Date: Fri, 30 Oct 2026 14:18:21 -0500 Subject: [PATCH] feat(media): strip copied sent-media history notes from final replies Hermes' send_message records each media send as an assistant line ("[Sent voice message]", "[Sent audio attachment]", ...) so the model remembers it. Models copy those lines into real replies and users receive them as text: 142 such lines in the private members' final replies, every one on its own line. strip_media_mirror_lines removes whole matching lines and returns None when there are none; a reply left empty or bare NO_REPLY becomes MEDIA_DELIVERED_REPLY. A contract test feeds it every shape the host's own _describe_media_for_mirror produces. Claude-Session: https://claude.ai/code/session_01B6XeAU5Gn7ZaLQJVSg4AKC --- hermes_plugin_kit/__init__.py | 22 ++++++++++++++++++++++ tests/test_hermes_contract.py | 17 +++++++++++++++++ tests/test_kit.py | 10 ++++++++++ 3 files changed, 49 insertions(+) diff --git a/hermes_plugin_kit/__init__.py b/hermes_plugin_kit/__init__.py index 2011259..19e97db 100644 --- a/hermes_plugin_kit/__init__.py +++ b/hermes_plugin_kit/__init__.py @@ -95,6 +95,7 @@ def register(ctx): "deliver_media", "resolve_delivery_target", "MEDIA_DELIVERED_REPLY", + "strip_media_mirror_lines", "transform_media_delivery_output", "clear_media_delivery_state", "MediaType", @@ -1860,6 +1861,27 @@ def _mark_media_delivery_success(context: dict[str, Any]) -> None: # The turn's visible reply after the attachment itself was already sent. MEDIA_DELIVERED_REPLY = "✅" +# Hermes' send_message records each media send as an assistant line so the model +# remembers it (tools/send_message_tool.py _describe_media_for_mirror). Models copy +# those lines into real replies, where the user receives them as text. +_MEDIA_MIRROR_LINE = re.compile( + r"\[Sent (?:voice message|(?:image|video|audio|document) attachment|\d+ media attachments)\]" +) + + +def strip_media_mirror_lines(response_text: str) -> str | None: + """Remove whole lines that copy Hermes' sent-media history notes from a final reply. + + Returns ``None`` when there is nothing to remove. A reply left empty, or holding + only ``NO_REPLY``, becomes :data:`MEDIA_DELIVERED_REPLY`: the note follows a send. + """ + lines = str(response_text or "").splitlines() + kept = [line for line in lines if not _MEDIA_MIRROR_LINE.fullmatch(line.strip())] + if len(kept) == len(lines): + return None + rest = re.sub(r"\n{3,}", "\n\n", "\n".join(kept)).strip() + return MEDIA_DELIVERED_REPLY if rest in ("", "NO_REPLY") else rest + def transform_media_delivery_output( response_text: str = "", diff --git a/tests/test_hermes_contract.py b/tests/test_hermes_contract.py index 19a86e9..d10cf4e 100644 --- a/tests/test_hermes_contract.py +++ b/tests/test_hermes_contract.py @@ -696,6 +696,23 @@ def test_media_delivered_reply_is_not_a_hermes_silence_marker(self) -> None: self.assertTrue(is_intentional_silence_response("NO_REPLY")) self.assertFalse(is_intentional_silence_response(hpk.MEDIA_DELIVERED_REPLY)) + def test_strip_media_mirror_lines_matches_every_hermes_mirror_shape(self) -> None: + """Each note Hermes' send_message writes into history must be strippable.""" + from tools.send_message_tool import _describe_media_for_mirror # type: ignore + + cases = [ + [("/m/memo.ogg", True)], + [("/m/pic.png", False)], + [("/m/clip.mp4", False)], + [("/m/song.mp3", False)], + [("/m/notes.pdf", False)], + [("/m/a.png", False), ("/m/b.png", False)], + ] + for media in cases: + note = _describe_media_for_mirror(media) + with self.subTest(note=note): + self.assertEqual(hpk.strip_media_mirror_lines(f"Done.\n{note}"), "Done.") + def test_host_tool_invocation_reaches_real_telegram_media_contract(self) -> None: """Exercise Hermes media parsing and Telegram formatting without network I/O.""" import asyncio diff --git a/tests/test_kit.py b/tests/test_kit.py index 1ed7990..bdf3ad1 100644 --- a/tests/test_kit.py +++ b/tests/test_kit.py @@ -1221,6 +1221,16 @@ def test_successful_delivery_suppresses_the_same_turn_final_response_once(self) ) ) + def test_strip_media_mirror_lines_removes_only_whole_placeholder_lines(self) -> None: + self.assertEqual( + hpk.strip_media_mirror_lines("Here she is.\n\nMEDIA:/x.jpg\n[Sent voice message]\n"), + "Here she is.\n\nMEDIA:/x.jpg", + ) + self.assertEqual(hpk.strip_media_mirror_lines("NO_REPLY\n[Sent image attachment]"), "✅") + self.assertEqual(hpk.strip_media_mirror_lines("[Sent 3 media attachments]"), "✅") + self.assertIsNone(hpk.strip_media_mirror_lines("I saw [Sent voice message] in the log")) + self.assertIsNone(hpk.strip_media_mirror_lines("hello")) + def test_successful_delivery_does_not_suppress_another_session(self) -> None: with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "memo.ogg"