fix: require anchors for Telegram DM topic deliveries

This commit is contained in:
Hermes Agent 2026-05-16 18:55:33 +00:00
parent 3d585f8db5
commit 6daafb3fd4
2 changed files with 60 additions and 8 deletions

View file

@ -259,16 +259,33 @@ class DeliveryRouter:
send_metadata = dict(metadata or {})
if target.thread_id:
has_explicit_direct_topic = (
"direct_messages_topic_id" in send_metadata
or "telegram_direct_messages_topic_id" in send_metadata
)
if (
target.platform == Platform.TELEGRAM
and _looks_like_telegram_private_chat_id(target.chat_id)
and "thread_id" not in send_metadata
and "message_thread_id" not in send_metadata
and "direct_messages_topic_id" not in send_metadata
and "telegram_direct_messages_topic_id" not in send_metadata
and not has_explicit_direct_topic
):
send_metadata["telegram_direct_messages_topic_id"] = target.thread_id
elif "thread_id" not in send_metadata:
# Telegram has two similar-but-not-equivalent private topic modes:
# true Bot API Direct Messages topics use direct_messages_topic_id,
# while Hermes-created private DM lanes only route reliably with
# message_thread_id plus a reply anchor to a message in that lane.
# DeliveryRouter often handles proactive/cron sends, so an anchor
# may not exist. Refuse the send rather than reporting success for
# a message that lands in General/All Messages or is invisible.
reply_anchor = send_metadata.get("telegram_reply_to_message_id")
if reply_anchor is None:
raise RuntimeError(
"Telegram private DM topic delivery requires telegram_reply_to_message_id; "
"send to the bare chat or provide a reply anchor"
)
send_metadata["thread_id"] = target.thread_id
send_metadata["telegram_dm_topic_reply_fallback"] = True
elif "thread_id" not in send_metadata and "message_thread_id" not in send_metadata and not has_explicit_direct_topic:
send_metadata["thread_id"] = target.thread_id
result = await adapter.send(target.chat_id, content, metadata=send_metadata or None)
if getattr(result, "success", True) is False: