Skip to content

The agent that grows with you

fix(gateway): count bot messages under the transport profile

Erosika445113dffa7f
diff --git a/gateway/run.py b/gateway/run.py
index 40215b808c5..b312450294b 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -3961,11 +3961,20 @@ class GatewayRunner(
return self._is_user_authorized(source)
return self._is_user_authorized(source, allow_adapter_delegation=False)
+ return self._under_authorization_profile(source, _check)
+
+ def _admit_bot_message_for_source(self, source: SessionSource) -> bool:
+ """Count a bot message under the profile that authorized it, so the guard's peek, count and
+ config all read the transport profile's ``gateway.bot_loop_guard``."""
+ return self._under_authorization_profile(source, lambda: self._admit_bot_message(source))
+
+ @staticmethod
+ def _under_authorization_profile(source: SessionSource, check):
authorization_home = getattr(source, "_authorization_profile_home", None)
- if authorization_home is not None:
- with _profile_runtime_scope(Path(authorization_home)):
- return _check()
- return _check()
+ if authorization_home is None:
+ return check()
+ with _profile_runtime_scope(Path(authorization_home)):
+ return check()
def _cache_session_source(self, session_key: str, source) -> None:
if not session_key or source is None:
diff --git a/gateway/run_busy.py b/gateway/run_busy.py
index c4009636830..2696831cfbf 100644
--- a/gateway/run_busy.py
+++ b/gateway/run_busy.py
@@ -674,7 +674,7 @@ class GatewayBusySessionMixin:
)
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):
+ if not self._admit_bot_message_for_source(event.source):
return True
event._bot_loop_admitted = True
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 991f5096987..6fff563305c 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -199,7 +199,7 @@ class GatewayInboundMixin:
await self._hm_offer_pairing_code(source)
return None
# 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):
+ if not getattr(event, "_bot_loop_admitted", False) and not self._admit_bot_message_for_source(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 1613eb4cd94..2822b069124 100644
--- a/tests/gateway/test_bot_loop_guard.py
+++ b/tests/gateway/test_bot_loop_guard.py
@@ -143,6 +143,36 @@ async def test_busy_path_counts_a_bot_message_once_before_steering(monkeypatch,
assert (await runner._hm_admit_event(events[0]))[0] is events[0]
+async def test_routed_bot_traffic_is_metered_by_the_transport_profiles_policy(tmp_path, monkeypatch, runner, clock):
+ """The verdict runs under the transport profile; the count must read the same ``bot_loop_guard`` block, not
+ the routed profile's, or the two disagree about the budget."""
+ from gateway.platforms.base import MessageEvent
+ from gateway.run import _profile_runtime_scope
+
+ transport, routed = tmp_path / "transport", tmp_path / "routed"
+ for home, block in ((transport, "enabled: false"), (routed, "enabled: true\n max_events: 1")):
+ home.mkdir()
+ (home / "config.yaml").write_text(f"gateway:\n bot_loop_guard:\n {block}\n")
+ monkeypatch.setenv("HERMES_HOME", str(transport))
+ runner._bot_loop_guard = BotLoopGuard(clock=clock.now)
+ runner._principal_authorized = lambda *a, **kw: True
+ runner._adapter_profile_for_source = lambda source: "transport"
+ runner._scale_to_zero_note_real_inbound = lambda: None
+ runner._hm_pre_gateway_dispatch_hook = lambda event, source: event
+
+ def _routed_bot(i: int) -> MessageEvent:
+ source = _bot(BOT_A)
+ source.profile = "routed"
+ source._authorization_profile_home = transport
+ return MessageEvent(text="ping", message_id=str(i), source=source)
+
+ with _profile_runtime_scope(routed):
+ admitted = [await runner._hm_admit_event(_routed_bot(i)) is not None for i in range(3)]
+
+ assert admitted == [True, True, True]
+
+
@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),