Skip to content

The agent that grows with you

fix(gateway): mid-turn authorization reads the admitting bot's allowlist; shared-bot satellites keep a transport

Teknium77180acc2cbb
diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.py
index eb57a7f3343..285a207ac69 100644
--- a/gateway/authz_mixin.py
+++ b/gateway/authz_mixin.py
@@ -11,6 +11,7 @@ import contextlib
import logging
import os
import threading
+from pathlib import Path
from typing import Optional
from gateway.bot_loop_guard import BotLoopGuard
@@ -238,7 +239,10 @@ class GatewayAuthorizationMixin:
return self._primary_adapters()
profile_adapters = self._profile_adapters_map()
if profile_name in profile_adapters:
- return profile_adapters[profile_name]
+ adapters = profile_adapters[profile_name]
+ if adapters or not self._is_shared_bot_satellite(profile_name):
+ return adapters
+ return self._primary_adapters()
# Identity captured at construction, not the per-turn HERMES_HOME-derived name.
primary_profile = getattr(self, "_primary_profile_name", None)
if not primary_profile:
@@ -246,6 +250,28 @@ class GatewayAuthorizationMixin:
primary_profile = self._active_profile_name()
return self._primary_adapters() if profile_name == primary_profile else {}
+ def _is_shared_bot_satellite(self, profile_name: str) -> bool:
+ """A served profile with NO adapter of its own that a ``profile_routes`` entry targets through the
+ default profile's bot: it drains through the primary's adapters (gateway/AGENTS.md). Its
+ ``_profile_adapters`` entry is the ``{}`` startup placeholder; a secondary connected on ANY
+ platform is its own credential boundary and never borrows the primary. Restored/cached sources
+ carry no transport ref, so this is what keeps heartbeats, completions and goal notices for such a
+ profile deliverable after a restart (the same rule ``kanban_watchers_notifier`` and cron apply)."""
+ config = getattr(self, "config", None)
+ if not getattr(config, "multiplex_profiles", False):
+ return False
+ # A bot that failed to connect is queued for reconnect: that profile owns a credential.
+ if (getattr(self, "_profile_failed_platforms", None) or {}).get(profile_name):
+ return False
+ routes = getattr(config, "profile_routes", None) or []
+ if not any(r.enabled and r.profile == profile_name and r.bot_profile is None for r in routes):
+ return False
+ from gateway.run import _multiplex_profile_homes
+ try:
+ return profile_name in {name for name, _home in _multiplex_profile_homes(config)}
+ except Exception:
+ return False
+
def _adapter_for_source(self, source: Optional[SessionSource]):
"""Resolve the live adapter for an inbound ``SessionSource``."""
if source is None:
@@ -285,6 +311,29 @@ class GatewayAuthorizationMixin:
registered, profile = self._owning_profile(adapter, platform)
return (adapter, profile) if registered else None
+ def _authorization_home_for_source(self, source: SessionSource):
+ """HERMES_HOME whose allowlist admits *source*: the ingress-stamped transport home, else the home of
+ the profile owning the adapter that delivers it. ``None`` = authorize in the ambient scope
+ (multiplex off, or no live adapter — the check then fails closed on its own).
+
+ Inside a routed satellite's turn the ambient scope is the satellite's, whose ``.env`` has no
+ token/allowlist; every authorization decision made mid-turn (``/topic``, sibling ``/stop``, plugin
+ injection, voice, auto-resume) must read the admitting bot's allowlist instead."""
+ stamped = getattr(source, "_authorization_profile_home", None)
+ if stamped is not None:
+ return Path(stamped)
+ if not getattr(getattr(self, "config", None), "multiplex_profiles", False):
+ return None
+ adapter = self._adapter_for_source(source)
+ if adapter is None:
+ return None
+ _registered, profile = self._owning_profile(adapter, getattr(source, "platform", None))
+ if profile is None:
+ from hermes_constants import get_process_hermes_home
+ return get_process_hermes_home() # the primary bot's home, never the per-turn override
+ from hermes_cli.profiles import get_profile_dir
+ return get_profile_dir(profile)
+
def _adapter_profile_for_source(self, source: SessionSource) -> Optional[str]:
"""Resolve the transport-owning profile for adapter policy lookups."""
owner = self._transport_owner(source)
diff --git a/gateway/run.py b/gateway/run.py
index c0c88d717a6..83f737d5a64 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -3985,9 +3985,8 @@ class GatewayRunner(
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)
+ def _under_authorization_profile(self, source: SessionSource, check):
+ authorization_home = self._authorization_home_for_source(source)
if authorization_home is None:
return check()
with _profile_runtime_scope(Path(authorization_home)):
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 6fff563305c..2b4fb871be2 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -1789,7 +1789,7 @@ class GatewayInboundMixin:
source = dataclasses.replace(entry.origin)
try:
- authorized = self._is_user_authorized(source, allow_adapter_delegation=False)
+ authorized = self._is_user_authorized_for_source(source, allow_adapter_delegation=False)
except Exception:
logger.warning(
"Plugin message injection authorization check failed: plugin=%s session=%s",
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 91b19eac78f..fef6b7f7b11 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -1009,10 +1009,9 @@ class GatewayNotificationsMixin:
return owner[0]
if getattr(source, "delivered_via_upstream_relay", False) is True:
return self.adapters.get(Platform.RELAY)
- profile = getattr(source, "profile", None)
- adapters = self.adapters
- if profile and profile not in ("default", getattr(self, "_primary_profile_name", None)):
- adapters = (getattr(self, "_profile_adapters", None) or {}).get(profile, {})
+ # One resolver with authz/kanban/cron: a secondary's own map, or the primary's for a
+ # shared-bot satellite; a disconnected secondary fails closed to ``{}``.
+ adapters = self._adapters_for_profile(getattr(source, "profile", None))
try:
_transport = resolve_delivery_transport(Platform(platform_name), self.config, adapters)
except Exception:
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 8ce6a4c2f32..77825c3cdbe 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -501,7 +501,7 @@ class GatewayStartupMixin:
allowlist existed (or whose owner was since removed) must not silently receive a full agent
response just because it carries a resume marker."""
try:
- if self._is_user_authorized(source):
+ if self._is_user_authorized_for_source(source):
return True
logger.warning(
"Skipping auto-resume for %s: session owner is no "
diff --git a/gateway/run_voice.py b/gateway/run_voice.py
index d042733dadc..a5b993243a0 100644
--- a/gateway/run_voice.py
+++ b/gateway/run_voice.py
@@ -249,7 +249,7 @@ class GatewayVoiceMixin:
# before TELEGRAM_ALLOWED_USERS (or equivalent) was configured, or before the owner was removed from
# it, must not silently receive a full agent response on gateway restart just because it has a
# resume-pending marker (issue #23778).
- if not self._is_user_authorized(source):
+ if not self._is_user_authorized_for_source(source):
logger.debug("Unauthorized voice input from user %d, ignoring", user_id)
return
if self._is_duplicate_voice_transcript(guild_id, user_id, transcript):
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 4a778d31720..6d2b843272d 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -439,7 +439,7 @@ class GatewaySlashCommandsMixin(
# run another user started lives under a different key, yet authorized users must still be
# able to /stop it: fall back to sibling runs in this thread, gated on authorization.
sibling_keys = self._sibling_thread_run_keys(source, session_key)
- if sibling_keys and self._is_user_authorized(source):
+ if sibling_keys and self._is_user_authorized_for_source(source):
for sibling_key in sibling_keys:
await _stop(sibling_key, "stop_command_thread_sibling")
logger.info("STOP (thread sibling) by %s — interrupted %d run(s) in thread: %s",
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index b208310f0c5..f3a06e38afd 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -671,7 +671,7 @@ class GatewaySessionCommandsMixin:
# Defense in depth: /topic mutates SQLite side tables, so re-check the allowlist here.
try:
- if not self._is_user_authorized(source):
+ if not self._is_user_authorized_for_source(source):
return t("gateway.topic.unauthorized")
except Exception:
logger.debug("Topic auth check failed", exc_info=True)
diff --git a/tests/gateway/test_multiplex_interactive_auth.py b/tests/gateway/test_multiplex_interactive_auth.py
index 5ed97aa17c8..bfa08a4d9b5 100644
--- a/tests/gateway/test_multiplex_interactive_auth.py
+++ b/tests/gateway/test_multiplex_interactive_auth.py
@@ -146,12 +146,14 @@ def test_slack_interactive_auth_prefers_wired_profile_check(mux_home, monkeypatc
def test_authorization_adapter_ignores_per_turn_active_profile(mux_home):
"""#87240 egress half: inside a secondary profile's runtime scope the
default bot must not be handed to that profile (fail-closed None); the
- launch profile still resolves ``self.adapters``."""
+ launch profile still resolves ``self.adapters``. The secondary's own bot
+ is down (reconnect pending), so it is not a route-only satellite."""
from gateway.run import _profile_runtime_scope
runner = _runner(mux_home)
default_bot = object()
runner.adapters = {Platform.TELEGRAM: default_bot}
+ runner._profile_failed_platforms = {"secondary": {Platform.TELEGRAM: object()}}
with _profile_runtime_scope(mux_home / "profiles" / "secondary"):
assert runner._authorization_adapter(Platform.TELEGRAM, profile="secondary") is None