From 996efbf46b68db8ed240845440afe88c0c5df92a Mon Sep 17 00:00:00 2001 From: QueryPlanner Date: Sun, 24 May 2026 16:50:49 +0530 Subject: [PATCH] fix: remove tool notification rate limit - Remove rate limiting from notify_telegram_before_tool - Each parallel tool call now gets its own notification - Keep rate limiting for intermediate LLM text notifications - Update tests to expect all notifications through --- src/blacki/callbacks.py | 23 ----------------------- tests/test_telegram_tool_notifications.py | 7 ++++--- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/src/blacki/callbacks.py b/src/blacki/callbacks.py index 80c0be6..a953a19 100644 --- a/src/blacki/callbacks.py +++ b/src/blacki/callbacks.py @@ -27,10 +27,6 @@ logger = logging.getLogger(__name__) # Per-chat monotonic timestamps for rate limiting (bounded; see _touch_rate_limit). -_TOOL_NOTIFY_LAST: dict[str, float] = {} -_TOOL_NOTIFY_MIN_INTERVAL_SEC = 0.1 -_MAX_TOOL_NOTIFY_RATE_ENTRIES = 8192 -_TOOL_NOTIFY_LOCK = asyncio.Lock() _INTERMEDIATE_NOTIFY_LAST: dict[str, float] = {} _INTERMEDIATE_NOTIFY_MIN_INTERVAL_SEC = 0.35 @@ -130,8 +126,6 @@ async def _shared_telegram_notify_client(token: str) -> TelegramApiClient: async def reset_telegram_tool_notify_rate_limiter_for_tests() -> None: """Clear per-chat rate limit state and env lookup cache (tests only).""" - async with _TOOL_NOTIFY_LOCK: - _TOOL_NOTIFY_LAST.clear() async with _INTERMEDIATE_NOTIFY_LOCK: _INTERMEDIATE_NOTIFY_LAST.clear() _telegram_tool_notifications_enabled_impl.cache_clear() @@ -236,23 +230,6 @@ async def notify_telegram_before_tool( thread_id = _parse_optional_int(tool_context.state.get("telegram_thread_id")) - chat_key = str(chat_id) - now = time.monotonic() - if not await _rate_limit_allows_notification( - chat_key, - now, - storage=_TOOL_NOTIFY_LAST, - min_interval=_TOOL_NOTIFY_MIN_INTERVAL_SEC, - max_entries=_MAX_TOOL_NOTIFY_RATE_ENTRIES, - lock=_TOOL_NOTIFY_LOCK, - ): - logger.debug( - "Skipping Telegram tool notify (rate limit) chat_id=%s tool=%s", - chat_id, - tool.name, - ) - return None - logger.debug( "notify_telegram_before_tool: sending notification for tool=%s chat_id=%s", tool.name, diff --git a/tests/test_telegram_tool_notifications.py b/tests/test_telegram_tool_notifications.py index 013f322..44b4240 100644 --- a/tests/test_telegram_tool_notifications.py +++ b/tests/test_telegram_tool_notifications.py @@ -387,10 +387,10 @@ async def test_notify_sends_to_telegram_with_chat_and_thread( @pytest.mark.asyncio -async def test_notify_rate_limits_per_chat( +async def test_notify_sends_for_each_tool_call( monkeypatch: pytest.MonkeyPatch, ) -> None: - """Two tool calls within the throttle window only produce one Telegram send.""" + """Each tool call gets its own Telegram notification (no rate limiting).""" monkeypatch.setenv("TELEGRAM_ENABLED", "true") monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "tok") monkeypatch.setenv("TELEGRAM_TOOL_NOTIFICATIONS", "true") @@ -416,7 +416,8 @@ async def test_notify_rate_limits_per_chat( cast(ToolContext, ctx), ) - assert len(mock_client.send_message.await_args_list) == 1 + # Both tool calls should send notifications + assert len(mock_client.send_message.await_args_list) == 2 @pytest.mark.asyncio