From a7bc3c7b4bc91ebb1cef731036b1c3f4fab4934f Mon Sep 17 00:00:00 2001 From: Erosika Date: Tue, 8 Sep 2026 16:14:20 -0400 Subject: [PATCH] fix(gateway): charge the bot loop budget on the busy path _handle_active_session_busy_message only peeked at the guard, so a bot steering a running turn was never counted and the budget never tripped. The busy handler now admits the message once after the authorization verdict and marks the event. _hm_admit_event skips a marked event, so a follow-up that was queued on the busy path and drains later is not charged twice. --- gateway/run_busy.py | 4 ++++ gateway/run_inbound.py | 3 ++- tests/gateway/test_bot_loop_guard.py | 33 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/gateway/run_busy.py b/gateway/run_busy.py index 0acfda137a..c400963683 100644 --- a/gateway/run_busy.py +++ b/gateway/run_busy.py @@ -673,6 +673,10 @@ class GatewayBusySessionMixin: event.source.platform.value if event.source.platform else "unknown", session_key, ) return True # handled (silently dropped); do not fall through + # A steered or queued follow-up never reaches _hm_admit_event, so the budget is charged here. + if not self._admit_bot_message(event.source): + return True + event._bot_loop_admitted = True effective_mode = self._effective_busy_input_mode(event.source) if self._draining: # gateway restarting/stopping diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py index b4a7aa3427..991f509698 100644 --- a/gateway/run_inbound.py +++ b/gateway/run_inbound.py @@ -198,7 +198,8 @@ class GatewayInboundMixin: ): await self._hm_offer_pairing_code(source) return None - if not self._admit_bot_message(source): + # The busy path charged this event on arrival; a drained follow-up must not pay twice. + if not getattr(event, "_bot_loop_admitted", False) and not self._admit_bot_message(source): return None return event, source, False diff --git a/tests/gateway/test_bot_loop_guard.py b/tests/gateway/test_bot_loop_guard.py index 27192f3518..1613eb4cd9 100644 --- a/tests/gateway/test_bot_loop_guard.py +++ b/tests/gateway/test_bot_loop_guard.py @@ -110,6 +110,39 @@ async def test_ingress_gate_counts_an_authorized_bot_once_and_drops_it_when_refu assert admitted == [BOT_A, BOT_B] +@pytest.mark.asyncio +async def test_busy_path_counts_a_bot_message_once_before_steering(monkeypatch, runner, settings): + """A steer never reaches the ingress gate, so the busy handler charges the budget; the event it accepted is + not charged again when a queued copy drains through ``_hm_admit_event``.""" + from unittest.mock import AsyncMock + + from gateway.platforms.base import MessageEvent + + _incident_config(monkeypatch) + monkeypatch.setenv("HERMES_GATEWAY_BUSY_ACK_ENABLED", "false") + settings["value"] = BotLoopGuardSettings(max_events=1, window_seconds=60, cooldown_seconds=60) + runner._draining = False + runner._effective_busy_input_mode = lambda source: "steer" + runner._effective_busy_text_mode = lambda source: "steer" + runner._route_plaintext_approval_while_busy = AsyncMock(return_value=False) + runner._adapter_for_source = lambda source: SimpleNamespace(_pending_messages={}) + runner._peek_session_state = lambda key: SimpleNamespace(turn=SimpleNamespace(agent=object())) + steer = AsyncMock(return_value=SimpleNamespace(effective_mode="steer", redirected=False, steered=True)) + runner._resolve_busy_steer_or_redirect = steer + events = [MessageEvent(text="more", message_id=str(i), source=_bot(BOT_A)) for i in range(3)] + + handled = [await runner._handle_active_session_busy_message(event, "session") for event in events] + + assert handled == [True, True, True] + assert steer.await_count == 1 + + runner._scale_to_zero_note_real_inbound = lambda: None + runner._hm_pre_gateway_dispatch_hook = lambda event, source: event + runner._is_user_authorized_for_source = lambda source, **kw: True + runner._admit_bot_message = lambda source: pytest.fail("the busy path already charged this event") + assert (await runner._hm_admit_event(events[0]))[0] is events[0] + + @pytest.mark.parametrize("senders, chat_id, chat_type, group_allowlist", [ ([BOT_A, BOT_B], GROUP_CHAT, "group", True), ([BOT_A, BOT_B, BOT_C], GROUP_CHAT, "group", True),