fix(channels): bound origin reply fingerprint cache

Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com>
This commit is contained in:
Lanre Shittu
2026-09-03 15:37:22 +08:00
committed by Xubin Ren
parent 588be51d8c
commit 30509b6d99
2 changed files with 52 additions and 5 deletions
+36 -2
View File
@@ -7,6 +7,7 @@ import json
import subprocess
import sys
import tomllib
from collections import OrderedDict
from dataclasses import replace
from importlib.metadata import PackageNotFoundError
from pathlib import Path
@@ -32,7 +33,7 @@ from nanobot.channels.contracts import (
SetupRequirement,
channel_default_config,
)
from nanobot.channels.manager import ChannelManager
from nanobot.channels.manager import ORIGIN_REPLY_FINGERPRINTS_MAX_SIZE, ChannelManager
from nanobot.channels.plugin import ChannelPlugin, load_channel_package
from nanobot.config.loader import load_config, save_config
from nanobot.config.schema import ChannelsConfig, Config
@@ -3179,7 +3180,7 @@ def test_outbound_duplicate_suppression_is_scoped_to_origin_message() -> None:
mgr.bus = MessageBus()
mgr.channels = {}
mgr._dispatch_task = None
mgr._origin_reply_fingerprints = {}
mgr._origin_reply_fingerprints = OrderedDict()
first = OutboundMessage(
channel="feishu",
@@ -3212,6 +3213,39 @@ def test_outbound_duplicate_suppression_is_scoped_to_origin_message() -> None:
assert mgr._should_suppress_outbound(new_origin_content) is False
def test_outbound_duplicate_suppression_cache_is_bounded() -> None:
mgr = ChannelManager.__new__(ChannelManager)
mgr._origin_reply_fingerprints = OrderedDict()
for index in range(ORIGIN_REPLY_FINGERPRINTS_MAX_SIZE):
msg = OutboundMessage(
channel="feishu",
chat_id="chat123",
content="Done",
metadata={"message_id": f"msg-{index}"},
)
assert mgr._should_suppress_outbound(msg) is False
duplicate = OutboundMessage(
channel="feishu",
chat_id="chat123",
content="Done",
metadata={"origin_message_id": "msg-0"},
)
newest = OutboundMessage(
channel="feishu",
chat_id="chat123",
content="Done",
metadata={"message_id": f"msg-{ORIGIN_REPLY_FINGERPRINTS_MAX_SIZE}"},
)
assert mgr._should_suppress_outbound(duplicate) is True
assert mgr._should_suppress_outbound(newest) is False
assert len(mgr._origin_reply_fingerprints) == ORIGIN_REPLY_FINGERPRINTS_MAX_SIZE
assert ("feishu", "chat123", "msg-0") in mgr._origin_reply_fingerprints
assert ("feishu", "chat123", "msg-1") not in mgr._origin_reply_fingerprints
@pytest.mark.asyncio
async def test_send_with_retry_propagates_cancelled_error():
"""_send_with_retry should re-raise CancelledError for graceful shutdown."""