Skip to content

The agent that grows with you

fix(messaging): hermes send reports dropped MEDIA attachments instead of success:true

teknium1314076ca63e8
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 8be693f44f3..a20670f7b40 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -1119,9 +1119,12 @@ def _log_safe_path(path: str) -> str:
return _LOG_UNSAFE_CHARS.sub("?", str(path))[:200]
-def _validated_delivery_path(raw_path, session_key: str, label: str) -> Optional[str]:
+def _validated_delivery_path(raw_path, session_key: str, label: str,
+ dropped: Optional[List[dict]] = None) -> Optional[str]:
"""``validate_media_delivery_path`` plus the shared "Skipping unsafe ..." warning. A path the
- host cannot see is retried against the active remote sandbox (ssh/modal/...; #466)."""
+ host cannot see is retried against the active remote sandbox (ssh/modal/...; #466). When
+ ``dropped`` is a list, a rejected path is appended as ``{"path", "reason"}`` so the caller can
+ report the drop instead of booking a delivery that never happened (#115908)."""
raw = str(raw_path)
safe_path = validate_media_delivery_path(raw, session_key=session_key)
if not safe_path:
@@ -1132,6 +1135,8 @@ def _validated_delivery_path(raw_path, session_key: str, label: str) -> Optional
# a sandbox path failed to translate) and is not a security rejection.
reason = "not found on this host" if not _existing_regular_file(raw) else "denied by the delivery policy"
logger.warning("Skipping %s (%s): %s", label, reason, _log_safe_path(raw))
+ if dropped is not None:
+ dropped.append({"path": raw, "reason": reason})
return safe_path
@@ -3102,11 +3107,12 @@ class BasePlatformAdapter(ABC):
return validate_media_delivery_path(path, session_key=session_key)
@staticmethod
- def filter_media_delivery_paths(media_files, session_key: str = "") -> List[Tuple[str, bool]]:
- """Drop unsafe MEDIA paths and normalize accepted paths."""
+ def filter_media_delivery_paths(media_files, session_key: str = "",
+ dropped: Optional[List[dict]] = None) -> List[Tuple[str, bool]]:
+ """Drop unsafe MEDIA paths and normalize accepted paths; ``dropped`` collects the rejects."""
return [
(safe_path, bool(is_voice)) for media_path, is_voice in media_files or []
- if (safe_path := _validated_delivery_path(media_path, session_key, "MEDIA directive path"))]
+ if (safe_path := _validated_delivery_path(media_path, session_key, "MEDIA directive path", dropped))]
@staticmethod
def filter_local_delivery_paths(file_paths, session_key: str = "") -> List[str]:
diff --git a/tests/tools/test_send_message_tool.py b/tests/tools/test_send_message_tool.py
index 0f53d78b9b8..8a5af891613 100644
--- a/tests/tools/test_send_message_tool.py
+++ b/tests/tools/test_send_message_tool.py
@@ -345,7 +345,10 @@ class TestSendMessageTool:
)
)
- assert result["success"] is True
+ # The text still goes out without the attachment, but the caller is told (#115908).
+ assert result["success"] is False
+ assert result["partial_success"] is True
+ assert result["media_dropped"] == [{"path": str(secret), "reason": "denied by the delivery policy"}]
send_mock.assert_awaited_once_with(
Platform.TELEGRAM,
telegram_cfg,
@@ -356,6 +359,40 @@ class TestSendMessageTool:
force_document=False,
)
+ def test_missing_media_is_reported_to_the_caller_and_hermes_send_exits_nonzero(self, tmp_path, monkeypatch):
+ """#115908: a MEDIA path that does not exist on the host was dropped with only a host-side
+ warning while ``hermes send`` printed success:true and exited 0. The surviving attachment is
+ still sent; the payload names the drop and the CLI exit code follows it."""
+ from hermes_cli.send_cmd import _emit_result
+
+ monkeypatch.setenv("HERMES_MEDIA_DELIVERY_STRICT", "0")
+ config, telegram_cfg = _make_config()
+ report = tmp_path / "report.pdf"
+ report.write_bytes(b"%PDF report")
+ missing = tmp_path / "missing.pdf"
+
+ with patch("gateway.config.load_gateway_config", return_value=config), \
+ patch("tools.interrupt.is_interrupted", return_value=False), \
+ patch("model_tools._run_async", side_effect=_run_async_immediately), \
+ patch("tools.send_message_tool._send_to_platform", new=AsyncMock(return_value={"success": True})) as send_mock, \
+ patch("gateway.mirror.mirror_to_session", return_value=True):
+ raw = send_message_tool({
+ "action": "send",
+ "target": "telegram:12345",
+ "message": f"report\nMEDIA:{report}\nMEDIA:{missing}",
+ })
+
+ result = json.loads(raw)
+ assert result["success"] is False
+ assert result["partial_success"] is True
+ assert result["media_dropped"] == [{"path": str(missing), "reason": "not found on this host"}]
+ assert "Delivery incomplete" in result["error"]
+ send_mock.assert_awaited_once_with(
+ Platform.TELEGRAM, telegram_cfg, "12345", "report", thread_id=None,
+ media_files=[(str(report.resolve()), False)], force_document=False,
+ )
+ assert _emit_result(raw, json_mode=True, quiet=True) != 0
+
def test_top_level_send_failure_redacts_query_token(self):
config, _telegram_cfg = _make_config()
leaked = "very-secret-query-token-123456"
diff --git a/tools/send_message_tool.py b/tools/send_message_tool.py
index ebc3cbf813b..48c7e9646cf 100644
--- a/tools/send_message_tool.py
+++ b/tools/send_message_tool.py
@@ -230,7 +230,8 @@ def _handle_send(args):
# Capture [[as_document]] before extract_media strips it (images keep original bytes via send_document).
force_document_attachments = "[[as_document]]" in message
media_files, cleaned_message = BasePlatformAdapter.extract_media(message)
- media_files = BasePlatformAdapter.filter_media_delivery_paths(media_files)
+ media_dropped: list = []
+ media_files = BasePlatformAdapter.filter_media_delivery_paths(media_files, dropped=media_dropped)
mirror_text = cleaned_message.strip() or _describe_media_for_mirror(media_files)
used_home_channel = not chat_id
if used_home_channel:
@@ -277,6 +278,15 @@ def _handle_send(args):
result["note"] = f"Sent to {platform_name} home channel (chat_id: {chat_id})"
if mirror_text and _mirror_sent_message(platform_name, chat_id, mirror_text, thread_id):
result["mirrored"] = True
+ if media_dropped:
+ # The text went out but an attachment the caller asked for did not: a script reading
+ # ``success`` / exit 0 must not book a delivery that never happened (#115908).
+ result["success"] = False
+ result["partial_success"] = True
+ result["error"] = (f"Delivery incomplete: {len(media_dropped)} requested MEDIA attachment(s) "
+ "dropped before delivery (see media_dropped)")
+ if isinstance(result, dict) and media_dropped:
+ result["media_dropped"] = media_dropped
if isinstance(result, dict) and "error" in result:
result["error"] = _sanitize_error_text(result["error"])
return json.dumps(result)