Skip to content

The agent that grows with you

fix(gateway,tools): per-profile Yuanbao home, env_passthrough allowlist and Slack ignored-channel guard under multiplex

Teknium388b881b3325
diff --git a/gateway/platforms/yuanbao.py b/gateway/platforms/yuanbao.py
index d2a8d045368..c8917f86bae 100644
--- a/gateway/platforms/yuanbao.py
+++ b/gateway/platforms/yuanbao.py
@@ -48,7 +48,7 @@ from gateway.platforms.base import (
)
from gateway.platforms.event import MessageEvent, MessageType
from gateway.platforms import helpers as _mdchunk
-from gateway.platforms._shared import get_scoped_secret as _yb_secret
+from gateway.platforms._shared import get_scoped_secret as _yb_secret, profile_scoped as _profile_scoped
from gateway.platforms.helpers import MessageDeduplicator
from gateway.platforms.yuanbao_media import (
download_url as media_download_url, get_cos_credentials, upload_to_cos,
@@ -768,7 +768,7 @@ class AutoSetHomeMiddleware(InboundMiddleware):
async def handle(self, ctx: InboundContext, next_fn) -> None:
adapter = ctx.adapter
if not adapter._auto_sethome_done and adapter._sender_may_designate_home(ctx):
- _cur_home = os.getenv("YUANBAO_HOME_CHANNEL", "")
+ _cur_home = _yb_secret("YUANBAO_HOME_CHANNEL", "") or ""
_should_set = not _cur_home or (_cur_home.startswith("group:") and ctx.chat_type == "dm")
if ctx.chat_type == "dm":
adapter._auto_sethome_done = True # DM seen — no further upgrades needed
@@ -786,7 +786,11 @@ class AutoSetHomeMiddleware(InboundMiddleware):
user_config: dict = read_user_config_raw(config_path)
user_config["YUANBAO_HOME_CHANNEL"] = ctx.chat_id
atomic_config_write(config_path, user_config)
- os.environ["YUANBAO_HOME_CHANNEL"] = str(ctx.chat_id)
+ # The profile's config.yaml (scoped home above) is the durable record. Under a multiplexed
+ # secondary's scope the process env is the DEFAULT profile's; writing there would make this
+ # tenant's chat the default profile's cron/notification home.
+ if not _profile_scoped():
+ os.environ["YUANBAO_HOME_CHANNEL"] = str(ctx.chat_id)
logger.info("[%s] Auto-sethome: designated %s (%s) as Yuanbao home channel", adapter.name, ctx.chat_id, ctx.chat_name)
except Exception as e:
logger.warning("[%s] Auto-sethome failed: %s", adapter.name, e)
@@ -2649,7 +2653,7 @@ class YuanbaoAdapter(BasePlatformAdapter):
self._access_policy = AccessPolicy(*_policy("dm"), *_policy("group"))
self._inbound_pipeline: InboundPipeline = InboundPipelineBuilder.build()
# Auto-sethome stays open when no home is set or the home is a group (upgradable by first DM).
- _existing_home = os.getenv("YUANBAO_HOME_CHANNEL") or (config.home_channel.chat_id if config.home_channel else "")
+ _existing_home = _yb_secret("YUANBAO_HOME_CHANNEL", "") or (config.home_channel.chat_id if config.home_channel else "")
self._auto_sethome_done: bool = bool(_existing_home) and not _existing_home.startswith("group:")
def _track_task(self, task: asyncio.Task) -> asyncio.Task:
diff --git a/gateway/run.py b/gateway/run.py
index 5c3413fe43b..9b7c877c169 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -1159,18 +1159,23 @@ def _csv_or_list_to_set(raw: Any) -> set[str]:
return {part.strip() for part in str(raw).split(",") if part.strip()}
-def _slack_ignored_channels_from_gateway_config(config: Any) -> set[str]:
+def _slack_ignored_channels_from_gateway_config(config: Any, adapter: Any = None) -> set[str]:
"""Return Slack channels that the generic gateway must never dispatch.
- Duplicates the adapter's drop as a fail-safe so bypasses can't reach auth, pairing or sessions."""
- platform_cfg = getattr(config, "platforms", {}).get(Platform.SLACK)
+ Duplicates the adapter's drop as a fail-safe so bypasses can't reach auth, pairing or sessions.
+ ``adapter`` is the source's routed adapter: under multiplex ``config`` is the DEFAULT profile's
+ GatewayConfig, so a secondary Slack bot's list lives only in its adapter's ``extra``."""
raw = None
- if platform_cfg is not None:
+ if adapter is not None:
+ raw = (getattr(getattr(adapter, "config", None), "extra", None) or {}).get("ignored_channels")
+ platform_cfg = getattr(config, "platforms", {}).get(Platform.SLACK)
+ if raw is None and platform_cfg is not None and adapter is None:
raw = getattr(platform_cfg, "extra", {}).get("ignored_channels")
if raw is None:
- # Top-level ``slack.ignored_channels`` arrives via the plugin's YAML→env bridge, not PlatformConfig.extra.
- # See #46925.
- raw = os.getenv("SLACK_IGNORED_CHANNELS") or None
+ # Top-level ``slack.ignored_channels`` arrives via the plugin's YAML→env bridge, not PlatformConfig.extra
+ # (#46925); scoped read so a secondary never inherits the default profile's list (first-writer env).
+ from gateway.authz_mixin import _platform_gate_env
+ raw = _platform_gate_env("SLACK_IGNORED_CHANNELS") or None
return _csv_or_list_to_set(raw)
@@ -1179,10 +1184,11 @@ def _slack_parent_channel_id(chat_id: Any) -> str:
return str(chat_id).split(":", 1)[0] if chat_id else ""
-def _is_slack_ignored_channel(config: Any, chat_id: Any) -> bool:
- """Check the generic Slack gateway blacklist for channel or thread IDs."""
+def _is_slack_ignored_channel(config: Any, chat_id: Any, adapter: Any = None) -> bool:
+ """Check the generic Slack gateway blacklist for channel or thread IDs (``adapter`` = the source's
+ routed adapter, whose ``extra`` is authoritative for a secondary profile)."""
channel_id = _slack_parent_channel_id(chat_id)
- ignored = _slack_ignored_channels_from_gateway_config(config)
+ ignored = _slack_ignored_channels_from_gateway_config(config, adapter)
return bool(channel_id and ("*" in ignored or channel_id in ignored))
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 2b4fb871be2..da9770ad938 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -155,11 +155,16 @@ class GatewayInboundMixin:
# Ignored-channel guard runs FIRST — before startup-restore queueing, plugin hooks, auth,
# and session setup — so an ignored channel can never reach pairing/auth/session state.
_chat_id = getattr(source, "chat_id", None)
+ if not is_internal and getattr(source, "platform", None) == Platform.SLACK:
+ # The routed adapter's extra carries a secondary profile's own list; ``_config`` is the default's.
+ _slack_adapter = None
+ with suppress(Exception):
+ _slack_adapter = self._adapter_for_source(source)
if (
# See #51899.
not is_internal
and getattr(source, "platform", None) == Platform.SLACK
- and _is_slack_ignored_channel(_config, _chat_id)
+ and _is_slack_ignored_channel(_config, _chat_id, _slack_adapter)
):
logger.info("Dropping Slack message from configured ignored channel %s", _chat_id)
return None
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index ea4fe963898..2329a58a693 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -110,7 +110,7 @@ class GatewayNotificationsMixin:
return
config = getattr(self, "config", None)
chat_id = getattr(source, "chat_id", None)
- if config and getattr(source, "platform", None) == Platform.SLACK and _is_slack_ignored_channel(config, chat_id):
+ if config and getattr(source, "platform", None) == Platform.SLACK and _is_slack_ignored_channel(config, chat_id, adapter):
logger.info("Skipping Slack platform notice for configured ignored channel %s", chat_id)
return
notice_delivery = (
diff --git a/tests/tools/test_env_passthrough.py b/tests/tools/test_env_passthrough.py
index 56eab9ba2ba..9afb4948a74 100644
--- a/tests/tools/test_env_passthrough.py
+++ b/tests/tools/test_env_passthrough.py
@@ -19,11 +19,11 @@ from tools.env_passthrough import (
def _clean_passthrough():
"""Ensure a clean passthrough state for every test."""
clear_env_passthrough()
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
ss.set_multiplex_active(False)
yield
clear_env_passthrough()
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
ss.set_multiplex_active(False)
@@ -46,7 +46,7 @@ class TestConfigPassthrough:
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(config), encoding="utf-8")
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
assert is_env_passthrough("MY_CUSTOM_KEY")
assert is_env_passthrough("ANOTHER_TOKEN")
@@ -58,7 +58,7 @@ class TestConfigPassthrough:
config_path = tmp_path / "config.yaml"
config_path.write_text(yaml.dump(config), encoding="utf-8")
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
register_env_passthrough(["SKILL_KEY"])
all_pt = get_all_passthrough()
diff --git a/tests/tools/test_skill_env_passthrough.py b/tests/tools/test_skill_env_passthrough.py
index 1988efb992e..7d78915858d 100644
--- a/tests/tools/test_skill_env_passthrough.py
+++ b/tests/tools/test_skill_env_passthrough.py
@@ -20,10 +20,10 @@ _AGENTMAIL_SKILL_SRC = (
@pytest.fixture(autouse=True)
def _clean_passthrough():
clear_env_passthrough()
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
yield
clear_env_passthrough()
- _ep_mod._config_passthrough = None
+ _ep_mod._config_passthrough.clear()
def _create_skill(tmp_path, name, frontmatter_extra=""):
diff --git a/tools/env_passthrough.py b/tools/env_passthrough.py
index 0c9b4a23791..827c1893cd7 100644
--- a/tools/env_passthrough.py
+++ b/tools/env_passthrough.py
@@ -28,8 +28,10 @@ def _get_allowed() -> set[str]:
return val
-# Cache for the config-based allowlist (loaded once per process).
-_config_passthrough: frozenset[str] | None = None
+# Config-based allowlist, keyed by Hermes home: under gateway.multiplex_profiles one process serves
+# many profiles, and a single slot would let the first profile's operator allowlist decide which env
+# vars tunnel into every other profile's sandbox children.
+_config_passthrough: dict[str, frozenset[str]] = {}
def _is_hermes_provider_credential(name: str) -> bool:
@@ -82,9 +84,12 @@ def _load_config_passthrough() -> frozenset[str]:
"""Load ``tools.env_passthrough`` from config.yaml (cached). Same credential
filter as register_env_passthrough: operator config must not tunnel provider
credentials into sandbox children either (GHSA-rhgp-j443-p4rf)."""
- global _config_passthrough
- if _config_passthrough is not None:
- return _config_passthrough
+ from hermes_constants import hermes_home_key
+
+ home_key = hermes_home_key()
+ cached = _config_passthrough.get(home_key)
+ if cached is not None:
+ return cached
result: set[str] = set()
try:
passthrough = cfg_get(read_raw_config(), "terminal", "env_passthrough")
@@ -99,8 +104,8 @@ def _load_config_passthrough() -> frozenset[str]:
)))
except Exception as e:
logger.debug("Could not read tools.env_passthrough from config: %s", e)
- _config_passthrough = frozenset(result)
- return _config_passthrough
+ _config_passthrough[home_key] = frozenset(result)
+ return _config_passthrough[home_key]
def is_env_passthrough(var_name: str) -> bool: