Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/blacki/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions tests/test_telegram_tool_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
Loading