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.
This commit is contained in:
Erosika
2026-09-08 16:14:20 -04:00
committed by Teknium
parent 6881e4d3fc
commit a7bc3c7b4b
3 changed files with 39 additions and 1 deletions
+4
View File
@@ -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
+2 -1
View File
@@ -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
+33
View File
@@ -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),