Skip to content

The agent that grows with you

fix(gateway): enter the secondary profile scope per callback, not from a configure-time snapshot

liuhao10243e8fe26a2e93
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index 66033d797d1..350d0420f4e 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -1789,22 +1789,18 @@ class GatewayAdapterLifecycleMixin:
Without this an inline-button caller approved only in the routed profile's pairing store was denied
(#86296), because the adapter's callback source was never route-stamped. A secondary-owned bot's
- callback runs outside the profile runtime scope (built at configure time, invoked from the
- adapter's event loop), so it re-enters the owning profile's scope per call — the gate's scoped
- env read otherwise falls back to os.environ (the default profile's env) and denies the
- secondary's own allowlisted callers (#120639).
+ callback fires outside the profile runtime scope (straight off the adapter's event loop), so the
+ check re-enters the owning profile's scope per call — the gate's scoped env read otherwise falls
+ back to os.environ (the default profile's env) and denies the secondary's own allowlisted
+ callers (#120639).
"""
from gateway.run import get_hermes_home
transport_home = Path(get_hermes_home()) if self._multiplex_on() and profile_name is None else None
- # Prebuild the secondary's secret scope at configure time so entering it per callback never
- # touches the adapter event loop with file IO; an unresolvable profile home keeps the
- # credential-reads-fail-closed behavior of every other secondary handler.
- secondary_scope = None
- if profile_name:
- profile_home = self._routed_profile_home(profile_name)
- if profile_home is not UNRESOLVED_PROFILE_HOME:
- from agent.secret_scope import build_profile_secret_scope
- secondary_scope = (profile_home, build_profile_secret_scope(profile_home))
+ # Resolve the owning profile's home once; the scope itself is entered per call so a callback
+ # reads the same per-turn allowlist freshness (and external-secret hydration) as the message
+ # path — ``_make_profile_message_handler`` re-reads the profile's ``.env`` per message, and a
+ # snapshot built here would keep a tap denied after an operator edits that ``.env``.
+ profile_home = self._routed_profile_home(profile_name) if profile_name else None
def check(
user_id: str, chat_type: Optional[str] = None, chat_id: Optional[str] = None, *,
@@ -1825,12 +1821,12 @@ class GatewayAdapterLifecycleMixin:
if adapter is not None:
source._transport_adapter_ref = _weakref.ref(adapter)
if transport_home is None:
- if secondary_scope is None:
- return self._is_user_authorized(source)
- # The cold-path message handler authorizes under ``_profile_runtime_scope``; a
- # callback must read the same allowlist scope, not ambient os.environ.
+ # Per call, like every sibling secondary handler: same ``.env`` freshness and hydration
+ # as the message path. ``_scope_or_null`` keeps the fail-closed behavior for an
+ # unresolvable profile home (bind nothing; an unscoped read raises instead of
+ # borrowing another profile's env).
from gateway.run import _profile_runtime_scope
- with _profile_runtime_scope(secondary_scope[0], secondary_scope[1]):
+ with self._scope_or_null(_profile_runtime_scope, profile_home):
return self._is_user_authorized(source)
# Canonicalize FIRST (callback sources never went through ``build_source``): the routed
# profile's pairing store is consulted, allowlists read under the transport home.
diff --git a/tests/gateway/test_multiplex_interactive_auth.py b/tests/gateway/test_multiplex_interactive_auth.py
index 76ca0f353f8..86727ce7a9b 100644
--- a/tests/gateway/test_multiplex_interactive_auth.py
+++ b/tests/gateway/test_multiplex_interactive_auth.py
@@ -129,6 +129,27 @@ def test_secondary_owned_callback_reads_own_profile_allowlist(mux_home):
assert tg._is_callback_user_authorized("999", chat_id="111", chat_type="private") is False
+def test_secondary_callback_allowlist_follows_env_edits(mux_home):
+ """#120639 review: the secondary's callback check re-enters the owning
+ profile's scope per call, so an operator's runtime edit to
+ ``profiles/<name>/.env`` reaches the NEXT button tap — the same freshness
+ the message path gives (``_make_profile_message_handler`` re-reads the
+ ``.env`` per message), no allow/deny split until the adapter reconnects."""
+ runner = _runner(mux_home)
+ env_path = mux_home / "profiles" / "secondary" / ".env"
+ env_path.write_text("TELEGRAM_ALLOWED_USERS=555\n")
+ tg = _telegram(runner)
+ tg._hermes_profile_name = "secondary"
+ runner._profile_adapters = {"secondary": {Platform.TELEGRAM: tg}}
+ tg.set_authorization_check(
+ runner._make_adapter_auth_check(Platform.TELEGRAM, profile_name="secondary")
+ )
+
+ assert tg._is_callback_user_authorized("777", chat_id="111", chat_type="private") is False
+ env_path.write_text("TELEGRAM_ALLOWED_USERS=555,777\n") # operator adds a user at runtime
+ assert tg._is_callback_user_authorized("777", chat_id="111", chat_type="private") is True
+
+
def test_slack_interactive_auth_prefers_wired_profile_check(mux_home, monkeypatch):
"""#72657: a multiplexed Slack adapter's button gate resolves through the
wired ``_make_adapter_auth_check`` for its own profile; the DEFAULT