Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): duplicate-reply suppression covers int message ids (#1476)

Haozhe Wu8bbdd78a0b7e
diff --git a/agent/src/channels/manager.py b/agent/src/channels/manager.py
index cd2650399..ca142dc5c 100644
--- a/agent/src/channels/manager.py
+++ b/agent/src/channels/manager.py
@@ -33,6 +33,10 @@ _BOOL_CAMEL_ALIASES: dict[str, str] = {
}
+# Inbound messages whose reply fingerprint is kept for duplicate suppression.
+_MAX_REPLY_FINGERPRINTS = 4096
+
+
class ChannelManager:
"""Manages chat channels and coordinates message routing.
@@ -266,20 +270,18 @@ class ChannelManager:
if not fingerprint:
return False
- origin_message_id = metadata.get("origin_message_id")
- if isinstance(origin_message_id, str) and origin_message_id:
- key = (msg.channel, msg.chat_id, origin_message_id)
- if self._origin_reply_fingerprints.get(key) == fingerprint:
- return True
- self._origin_reply_fingerprints[key] = fingerprint
-
+ # Telegram and NapCat message ids are ints; they dedupe like strings.
message_id = metadata.get("message_id")
- if isinstance(message_id, str) and message_id:
- key = (msg.channel, msg.chat_id, message_id)
- if self._origin_reply_fingerprints.get(key) == fingerprint:
- return True
- self._origin_reply_fingerprints[key] = fingerprint
-
+ if message_id is None or message_id == "" or isinstance(message_id, bool):
+ return False
+ key = (msg.channel, msg.chat_id, str(message_id))
+ if self._origin_reply_fingerprints.get(key) == fingerprint:
+ return True
+ self._origin_reply_fingerprints[key] = fingerprint
+ # Oldest first (insertion order): a long-running process keeps only the
+ # most recent inbound messages' fingerprints.
+ while len(self._origin_reply_fingerprints) > _MAX_REPLY_FINGERPRINTS:
+ del self._origin_reply_fingerprints[next(iter(self._origin_reply_fingerprints))]
return False
async def _dispatch_outbound(self) -> None:
diff --git a/agent/tests/test_channels_runtime.py b/agent/tests/test_channels_runtime.py
index a1e2f5cf0..483e1582f 100644
--- a/agent/tests/test_channels_runtime.py
+++ b/agent/tests/test_channels_runtime.py
@@ -108,6 +108,50 @@ def test_should_suppress_outbound_rejects_duplicate_reply_to_same_message() -> N
assert manager._should_suppress_outbound(msg2) is True
+def _dedup_manager() -> ChannelManager:
+ return ChannelManager(
+ {"websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]}},
+ MessageBus(),
+ )
+
+
+def test_integer_message_ids_dedupe_like_strings() -> None:
+ """Telegram and NapCat carry int message ids; they were never deduped (#1476)."""
+ manager = _dedup_manager()
+ reply = lambda: OutboundMessage( # noqa: E731
+ channel="telegram", chat_id="123", content="Here is your answer.", metadata={"message_id": 42}
+ )
+
+ assert manager._should_suppress_outbound(reply()) is False
+ assert manager._should_suppress_outbound(reply()) is True
+
+
+def test_a_different_reply_to_the_same_message_is_sent() -> None:
+ manager = _dedup_manager()
+ first = OutboundMessage(channel="telegram", chat_id="1", content="first", metadata={"message_id": "m"})
+ second = OutboundMessage(channel="telegram", chat_id="1", content="second", metadata={"message_id": "m"})
+
+ assert manager._should_suppress_outbound(first) is False
+ assert manager._should_suppress_outbound(second) is False
+
+
+def test_reply_fingerprints_are_bounded(monkeypatch) -> None:
+ import src.channels.manager as manager_module
+
+ monkeypatch.setattr(manager_module, "_MAX_REPLY_FINGERPRINTS", 3)
+ manager = _dedup_manager()
+ for i in range(5):
+ manager._should_suppress_outbound(
+ OutboundMessage(channel="telegram", chat_id="1", content="same", metadata={"message_id": f"m{i}"})
+ )
+
+ assert list(manager._origin_reply_fingerprints) == [
+ ("telegram", "1", "m2"),
+ ("telegram", "1", "m3"),
+ ("telegram", "1", "m4"),
+ ]
+
+
def test_registry_reports_all_built_in_channels_with_dependency_recovery() -> None:
expected = {
"dingtalk",