Skip to content

The agent that grows with you

refactor(gateway/config): extract the config.yaml phase to gateway/config_loader.py; table-driven from_dict; defensive collapse

Teknium5820d0b0d53d
diff --git a/gateway/config.py b/gateway/config.py
index 04dbaaf7b7b..415450d1dfd 100644
--- a/gateway/config.py
+++ b/gateway/config.py
@@ -11,7 +11,6 @@ Handles loading and validating configuration for:
import logging
import math
import os
-import json
from pathlib import Path
from dataclasses import asdict, dataclass, field, is_dataclass
from typing import Dict, List, Optional, Any, Callable
@@ -63,12 +62,10 @@ def _normalize_multiplex_profile_allowlist(value: Any) -> Optional[List[str]]:
from hermes_cli.profiles import normalize_profile_name, validate_profile_name
normalized: List[str] = []
- seen = set()
for entry in value:
if not isinstance(entry, str):
logger.warning(
- "Skipping invalid gateway.multiplex_profile_allowlist entry %r "
- "(expected a profile name)",
+ "Skipping invalid gateway.multiplex_profile_allowlist entry %r (expected a profile name)",
entry,
)
continue
@@ -76,15 +73,10 @@ def _normalize_multiplex_profile_allowlist(value: Any) -> Optional[List[str]]:
name = normalize_profile_name(entry)
validate_profile_name(name)
except ValueError:
- logger.warning(
- "Skipping invalid gateway.multiplex_profile_allowlist entry %r",
- entry,
- )
+ logger.warning("Skipping invalid gateway.multiplex_profile_allowlist entry %r", entry)
continue
- if name == "default" or name in seen:
- continue
- seen.add(name)
- normalized.append(name)
+ if name != "default" and name not in normalized:
+ normalized.append(name)
return normalized
@@ -106,9 +98,7 @@ def _env_multiplex_profiles_override() -> "bool | None":
secret arrives as ``""`` and must NOT shadow a config.yaml opt-in.
"""
raw = os.getenv("GATEWAY_MULTIPLEX_PROFILES")
- if raw is None:
- return None
- token = raw.strip().lower()
+ token = (raw or "").strip().lower()
if not token:
return None
if token in _MULTIPLEX_TRUTHY_STRINGS:
@@ -172,14 +162,9 @@ def _coerce_optional_positive_int(value: Any, key: str) -> Optional[int]:
"""
if value is None:
return None
- if isinstance(value, bool):
- logger.warning(
- "Ignoring invalid %s=%r (expected a positive integer; 0/null disables)",
- key,
- value,
- )
- return None
try:
+ if isinstance(value, bool):
+ raise ValueError(value)
if isinstance(value, float):
if not value.is_integer():
raise ValueError(value)
@@ -190,14 +175,10 @@ def _coerce_optional_positive_int(value: Any, key: str) -> Optional[int]:
parsed = int(value)
except (TypeError, ValueError):
logger.warning(
- "Ignoring invalid %s=%r (expected a positive integer; 0/null disables)",
- key,
- value,
+ "Ignoring invalid %s=%r (expected a positive integer; 0/null disables)", key, value
)
return None
- if parsed <= 0:
- return None
- return parsed
+ return parsed if parsed > 0 else None
_SYSTEMD_WATCHDOG_MAX_SECONDS = 2_147_483_647
@@ -213,22 +194,17 @@ def coerce_systemd_watchdog_seconds(
"""
if value is None:
return 0
- if isinstance(value, bool):
- logger.warning("Ignoring invalid %s (expected a positive integer)", key)
- return 0
- if isinstance(value, int):
+ parsed: Optional[int] = None
+ if isinstance(value, int) and not isinstance(value, bool):
parsed = value
elif isinstance(value, str):
raw = value.strip()
- if not raw or not raw.isascii() or not raw.isdecimal():
- logger.warning("Ignoring invalid %s (expected a positive integer)", key)
- return 0
- try:
- parsed = int(raw, 10)
- except (TypeError, ValueError, OverflowError):
- logger.warning("Ignoring invalid %s (expected a positive integer)", key)
- return 0
- else:
+ if raw and raw.isascii() and raw.isdecimal():
+ try:
+ parsed = int(raw, 10)
+ except (TypeError, ValueError, OverflowError):
+ parsed = None
+ if parsed is None:
logger.warning("Ignoring invalid %s (expected a positive integer)", key)
return 0
if parsed == 0:
@@ -248,40 +224,22 @@ def _coerce_dict(value: Any) -> Dict[str, Any]:
return value if isinstance(value, dict) else {}
-def _normalize_unauthorized_dm_behavior(value: Any, default: str = "pair") -> str:
- """Normalize unauthorized DM behavior to a supported value."""
- if isinstance(value, str):
- normalized = value.strip().lower()
- if normalized in {"pair", "ignore"}:
- return normalized
- return default
-
-
-def _normalize_notice_delivery(value: Any, default: str = "public") -> str:
- """Normalize notice delivery mode to a supported value."""
+def _normalize_choice(value: Any, choices: set, default: str) -> str:
+ """Lower-cased *value* when it is one of *choices*, else *default*."""
if isinstance(value, str):
normalized = value.strip().lower()
- if normalized in {"public", "private"}:
+ if normalized in choices:
return normalized
return default
-def _ensure_platform_extra_dict(platforms_data: dict, name: str) -> tuple[dict, dict]:
- """Get-or-create ``platforms_data[name]`` and its nested ``extra`` dict.
-
- Both slots are coerced to ``{}`` if a non-dict value is encountered, so
- callers can safely write keys without type-checking. Returns
- ``(plat_data, extra)`` for in-place mutation.
- """
- plat_data = platforms_data.setdefault(name, {})
- if not isinstance(plat_data, dict):
- plat_data = {}
- platforms_data[name] = plat_data
- extra = plat_data.setdefault("extra", {})
- if not isinstance(extra, dict):
- extra = {}
- plat_data["extra"] = extra
- return plat_data, extra
+def _dict_slot(container: dict, key: str) -> dict:
+ """Get-or-create ``container[key]`` as a dict, replacing a non-dict value with ``{}``."""
+ value = container.setdefault(key, {})
+ if not isinstance(value, dict):
+ value = {}
+ container[key] = value
+ return value
def _getenv(name: str, default: Optional[str] = None) -> Optional[str]:
@@ -296,10 +254,7 @@ def _getenv(name: str, default: Optional[str] = None) -> Optional[str]:
if current_secret_scope() is not None:
scope_val = _get_secret(name, None)
return scope_val if scope_val is not None else default
- env_val = os.environ.get(name)
- if env_val is not None:
- return env_val
- return default
+ return os.environ.get(name, default)
def _getenv_str(name: str, default: str = "") -> str:
@@ -307,16 +262,6 @@ def _getenv_str(name: str, default: str = "") -> str:
return val if val is not None else default
-def _getenv_int(name: str, default: int) -> int:
- raw = _getenv(name, None)
- if raw is None:
- return default
- try:
- return int(str(raw).strip(), 10)
- except (TypeError, ValueError):
- return default
-
-
# Module-level cache for bundled platform plugin names (lives outside the
# enum so it doesn't become an accidental enum member).
_Platform__bundled_plugin_names: Optional[set] = None
@@ -376,29 +321,28 @@ class Platform(Enum):
if _Platform__bundled_plugin_names is None:
_Platform__bundled_plugin_names = cls._scan_bundled_plugin_platforms()
if value in _Platform__bundled_plugin_names:
- pseudo = object.__new__(cls)
- pseudo._value_ = value
- pseudo._name_ = value.upper().replace("-", "_").replace(" ", "_")
- cls._value2member_map_[value] = pseudo
- cls._member_map_[pseudo._name_] = pseudo
- return pseudo
+ return cls._add_pseudo_member(value)
# Runtime-registered plugins (e.g. user-installed, discovered after
# the enum was defined).
try:
from gateway.platform_registry import platform_registry
if platform_registry.is_registered(value):
- pseudo = object.__new__(cls)
- pseudo._value_ = value
- pseudo._name_ = value.upper().replace("-", "_").replace(" ", "_")
- cls._value2member_map_[value] = pseudo
- cls._member_map_[pseudo._name_] = pseudo
- return pseudo
+ return cls._add_pseudo_member(value)
except Exception:
pass
return None
+ @classmethod
+ def _add_pseudo_member(cls, value: str) -> "Platform":
+ pseudo = object.__new__(cls)
+ pseudo._value_ = value
+ pseudo._name_ = value.upper().replace("-", "_").replace(" ", "_")
+ cls._value2member_map_[value] = pseudo
+ cls._member_map_[pseudo._name_] = pseudo
+ return pseudo
+
@classmethod
def _scan_bundled_plugin_platforms(cls) -> set:
"""Return names of bundled platform plugins under ``plugins/platforms/``."""
@@ -407,13 +351,8 @@ class Platform(Enum):
platforms_dir = Path(__file__).parent.parent / "plugins" / "platforms"
if platforms_dir.is_dir():
for child in platforms_dir.iterdir():
- if (
- child.is_dir()
- and (child / "__init__.py").exists()
- and (
- (child / "plugin.yaml").exists()
- or (child / "plugin.yml").exists()
- )
+ if child.is_dir() and (child / "__init__.py").exists() and (
+ (child / "plugin.yaml").exists() or (child / "plugin.yml").exists()
):
names.add(child.name.lower())
except Exception:
@@ -497,12 +436,9 @@ class HomeChannel:
"chat_id": self.chat_id,
"name": self.name,
}
- if self.thread_id:
- result["thread_id"] = self.thread_id
- if self.user_id:
- result["user_id"] = self.user_id
- if self.scope_id:
- result["scope_id"] = self.scope_id
+ for key in ("thread_id", "user_id", "scope_id"):
+ if getattr(self, key):
+ result[key] = getattr(self, key)
return result
@classmethod
@@ -522,14 +458,7 @@ def persist_home_channel(home: HomeChannel, *, enabled_if_new: bool = False) ->
from hermes_cli.config import load_config, save_config
config = load_config()
- platforms = config.setdefault("platforms", {})
- if not isinstance(platforms, dict):
- platforms = {}
- config["platforms"] = platforms
- platform_config = platforms.setdefault(home.platform.value, {})
- if not isinstance(platform_config, dict):
- platform_config = {}
- platforms[home.platform.value] = platform_config
+ platform_config = _dict_slot(_dict_slot(config, "platforms"), home.platform.value)
if enabled_if_new:
platform_config.setdefault("enabled", True)
platform_config["home_channel"] = home.to_dict()
@@ -577,20 +506,20 @@ class SessionResetPolicy:
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "SessionResetPolicy":
data = _coerce_dict(data)
- # Handle both missing keys and explicit null values (YAML null → None)
- mode = data.get("mode")
- at_hour = data.get("at_hour")
- idle_minutes = data.get("idle_minutes")
- notify = data.get("notify")
+
+ def val(key: str, default: Any) -> Any:
+ # Handle both missing keys and explicit null values (YAML null → None)
+ value = data.get(key)
+ return default if value is None else value
+
exclude = data.get("notify_exclude_platforms")
- bg_max_age = data.get("bg_process_max_age_hours")
return cls(
- mode=mode if mode is not None else "none",
- at_hour=at_hour if at_hour is not None else 4,
- idle_minutes=idle_minutes if idle_minutes is not None else 1440,
- notify=_coerce_bool(notify, True),
+ mode=val("mode", "none"),
+ at_hour=val("at_hour", 4),
+ idle_minutes=val("idle_minutes", 1440),
+ notify=_coerce_bool(data.get("notify"), True),
notify_exclude_platforms=tuple(exclude) if exclude is not None else ("api_server", "webhook"),
- bg_process_max_age_hours=bg_max_age if bg_max_age is not None else 24,
+ bg_process_max_age_hours=val("bg_process_max_age_hours", 24),
)
@@ -608,14 +537,7 @@ class ChannelOverride:
system_prompt: Optional[str] = None
def to_dict(self) -> Dict[str, Any]:
- out: Dict[str, Any] = {}
- if self.model is not None:
- out["model"] = self.model
- if self.provider is not None:
- out["provider"] = self.provider
- if self.system_prompt is not None:
- out["system_prompt"] = self.system_prompt
- return out
+ return {k: v for k, v in asdict(self).items() if v is not None}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ChannelOverride":
@@ -718,27 +640,15 @@ class PlatformConfig:
if isinstance(data.get("home_channel"), dict):
home_channel = HomeChannel.from_dict(data["home_channel"])
- # gateway_restart_notification may be bridged into extra via the
- # shared-key loop in load_gateway_config(); check both top-level
- # and extra so YAML ``discord: gateway_restart_notification: false``
- # works without needing a separate platforms: block.
+ # gateway_restart_notification / typing_indicator / typing_status_text may
+ # arrive top-level or bridged into ``extra`` by the shared-key loop in
+ # load_gateway_config(), so YAML ``discord: gateway_restart_notification: false``
+ # works without a separate platforms: block. Check both (top-level wins).
extra = _coerce_dict(data.get("extra", {}))
- _grn = data.get("gateway_restart_notification")
- if _grn is None:
- _grn = extra.get("gateway_restart_notification")
-
- # typing_indicator mirrors gateway_restart_notification: it may arrive
- # top-level or bridged into extra by the shared-key loop in
- # load_gateway_config(), so check both.
- _typing = data.get("typing_indicator")
- if _typing is None:
- _typing = extra.get("typing_indicator")
-
- # typing_status_text takes the same two routes (top-level or bridged
- # into extra); string passthrough, no coercion.
- _typing_text = data.get("typing_status_text")
- if _typing_text is None:
- _typing_text = extra.get("typing_status_text")
+
+ def toplevel_or_extra(key: str) -> Any:
+ value = data.get(key)
+ return extra.get(key) if value is None else value
channel_overrides: Dict[str, ChannelOverride] = {}
raw_overrides = data.get("channel_overrides") or {}
@@ -753,9 +663,9 @@ class PlatformConfig:
api_key=data.get("api_key"),
home_channel=home_channel,
reply_to_mode=data.get("reply_to_mode", "first"),
- gateway_restart_notification=_coerce_bool(_grn, True),
- typing_indicator=_coerce_bool(_typing, True),
- typing_status_text=_typing_text,
+ gateway_restart_notification=_coerce_bool(toplevel_or_extra("gateway_restart_notification"), True),
+ typing_indicator=_coerce_bool(toplevel_or_extra("typing_indicator"), True),
+ typing_status_text=toplevel_or_extra("typing_status_text"), # string passthrough, no coercion
channel_overrides=channel_overrides,
extra=extra,
)
@@ -1052,30 +962,26 @@ class GatewayConfig:
insertion order is not a stable contract and a reorder busts the
prompt cache without any semantic change.
"""
- connected = []
- for platform, config in self.platforms.items():
- if not config.enabled:
- continue
- if self._is_platform_connected(platform, config):
- connected.append(platform)
+ connected = [
+ platform
+ for platform, config in self.platforms.items()
+ if config.enabled and self._is_platform_connected(platform, config)
+ ]
return sorted(connected, key=lambda p: str(p.value))
def _is_platform_connected(self, platform: Platform, config: PlatformConfig) -> bool:
"""Check whether a single platform is sufficiently configured."""
+ checker = _PLATFORM_CONNECTED_CHECKERS.get(platform)
# Weixin requires both a token and an account_id (checked first so
# the generic token branch doesn't let it through without account_id).
if platform == Platform.WEIXIN:
- return bool(
- config.extra.get("account_id")
- and (config.token or config.extra.get("token"))
- )
+ return checker(config)
# Generic token/api_key auth covers Telegram, Discord, Slack, etc.
if config.token or config.api_key:
return True
# Platform-specific check
- checker = _PLATFORM_CONNECTED_CHECKERS.get(platform)
if checker is not None:
return checker(config)
@@ -1105,9 +1011,7 @@ class GatewayConfig:
def get_home_channel(self, platform: Platform) -> Optional[HomeChannel]:
"""Get the home channel for a platform."""
config = self.platforms.get(platform)
- if config:
- return config.home_channel
- return None
+ return config.home_channel if config else None
def get_reset_policy(
self,
@@ -1172,146 +1076,80 @@ class GatewayConfig:
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "GatewayConfig":
data = _coerce_dict(data)
+ nested_gateway = _coerce_dict(data.get("gateway"))
+
+ def pick(key: str) -> Any:
+ """Top-level key wins by presence; else the nested ``gateway.<key>`` form."""
+ return data[key] if key in data else nested_gateway.get(key)
+
platforms = {}
- platforms_data = _coerce_dict(data.get("platforms", {}))
- for platform_name, platform_data in platforms_data.items():
+ for platform_name, platform_data in _coerce_dict(data.get("platforms", {})).items():
if not isinstance(platform_data, dict):
continue
try:
- platform = Platform(platform_name)
- platforms[platform] = PlatformConfig.from_dict(platform_data)
+ platforms[Platform(platform_name)] = PlatformConfig.from_dict(platform_data)
except ValueError:
pass # Skip unknown platforms
-
- reset_by_type = {}
- for type_name, policy_data in _coerce_dict(data.get("reset_by_type", {})).items():
- reset_by_type[type_name] = SessionResetPolicy.from_dict(policy_data)
-
+
reset_by_platform = {}
for platform_name, policy_data in _coerce_dict(data.get("reset_by_platform", {})).items():
try:
- platform = Platform(platform_name)
- reset_by_platform[platform] = SessionResetPolicy.from_dict(policy_data)
+ reset_by_platform[Platform(platform_name)] = SessionResetPolicy.from_dict(policy_data)
except ValueError:
pass
-
- default_policy = SessionResetPolicy()
- if "default_reset_policy" in data:
- default_policy = SessionResetPolicy.from_dict(data["default_reset_policy"])
-
- sessions_dir = get_hermes_home() / "sessions"
- if "sessions_dir" in data:
- sessions_dir = Path(data["sessions_dir"])
-
- quick_commands = data.get("quick_commands", {})
- if not isinstance(quick_commands, dict):
- quick_commands = {}
+ stt = _coerce_dict(data.get("stt"))
stt_enabled = data.get("stt_enabled")
if stt_enabled is None:
- stt_enabled = data.get("stt", {}).get("enabled") if isinstance(data.get("stt"), dict) else None
+ stt_enabled = stt.get("enabled")
stt_echo_transcripts = data.get("stt_echo_transcripts")
if stt_echo_transcripts is None:
- stt_echo_transcripts = (
- data.get("stt", {}).get("echo_transcripts")
- if isinstance(data.get("stt"), dict)
- else None
- )
+ stt_echo_transcripts = stt.get("echo_transcripts")
- group_sessions_per_user = data.get("group_sessions_per_user")
- thread_sessions_per_user = data.get("thread_sessions_per_user")
- multiplex_profiles = data.get("multiplex_profiles")
- raw_gateway = data.get("gateway")
- nested_gateway = raw_gateway if isinstance(raw_gateway, dict) else {}
- if "multiplex_profile_allowlist" in data:
- multiplex_profile_allowlist = data.get("multiplex_profile_allowlist")
- else:
- multiplex_profile_allowlist = nested_gateway.get(
- "multiplex_profile_allowlist"
- )
room_link_url = data.get("room_link_url")
- if room_link_url is not None and not isinstance(room_link_url, str):
+ if not isinstance(room_link_url, str):
room_link_url = None
- if "systemd_watchdog_seconds" in data:
- systemd_watchdog_raw = data.get("systemd_watchdog_seconds")
- systemd_watchdog_key = "systemd_watchdog_seconds"
- else:
- systemd_watchdog_raw = nested_gateway.get("systemd_watchdog_seconds")
- systemd_watchdog_key = "gateway.systemd_watchdog_seconds"
+
+ # Key prefix for the warning: "gateway." when the nested form was the one consulted.
+ def key_label(key: str) -> str:
+ return key if key in data else f"gateway.{key}"
+
+ # Watchdog knobs: out-of-range / non-finite values fall back to the shipped defaults.
+ probe_interval = _coerce_float(pick("loop_watchdog_probe_interval_s"), DEFAULT_LOOP_WATCHDOG_INTERVAL_S)
+ if not math.isfinite(probe_interval) or not 1.0 <= probe_interval <= 3600.0:
+ probe_interval = DEFAULT_LOOP_WATCHDOG_INTERVAL_S
+ probe_timeout = _coerce_float(pick("loop_watchdog_probe_timeout_s"), DEFAULT_LOOP_WATCHDOG_TIMEOUT_S)
+ if not math.isfinite(probe_timeout) or not 1.0 <= probe_timeout <= 600.0:
+ probe_timeout = DEFAULT_LOOP_WATCHDOG_TIMEOUT_S
+ max_strikes = _coerce_int(pick("loop_watchdog_max_strikes"), DEFAULT_LOOP_WATCHDOG_MAX_STRIKES)
+ if not 1 <= max_strikes <= 1000:
+ max_strikes = DEFAULT_LOOP_WATCHDOG_MAX_STRIKES
+
systemd_watchdog_seconds = coerce_systemd_watchdog_seconds(
- systemd_watchdog_raw, systemd_watchdog_key
- )
- if "loop_watchdog" in data:
- loop_watchdog_raw = data.get("loop_watchdog")
- else:
- loop_watchdog_raw = nested_gateway.get("loop_watchdog")
- loop_watchdog = _coerce_bool(loop_watchdog_raw, True)
- loop_watchdog_probe_interval_s = _coerce_float(
- data.get("loop_watchdog_probe_interval_s")
- if "loop_watchdog_probe_interval_s" in data
- else nested_gateway.get("loop_watchdog_probe_interval_s"),
- DEFAULT_LOOP_WATCHDOG_INTERVAL_S,
- )
- loop_watchdog_probe_timeout_s = _coerce_float(
- data.get("loop_watchdog_probe_timeout_s")
- if "loop_watchdog_probe_timeout_s" in data
- else nested_gateway.get("loop_watchdog_probe_timeout_s"),
- DEFAULT_LOOP_WATCHDOG_TIMEOUT_S,
- )
- loop_watchdog_max_strikes = _coerce_int(
- data.get("loop_watchdog_max_strikes")
- if "loop_watchdog_max_strikes" in data
- else nested_gateway.get("loop_watchdog_max_strikes"),
- DEFAULT_LOOP_WATCHDOG_MAX_STRIKES,
+ pick("systemd_watchdog_seconds"), key_label("systemd_watchdog_seconds")
)
- if (
- not math.isfinite(loop_watchdog_probe_interval_s)
- or loop_watchdog_probe_interval_s < 1.0
- or loop_watchdog_probe_interval_s > 3600.0
- ):
- loop_watchdog_probe_interval_s = DEFAULT_LOOP_WATCHDOG_INTERVAL_S
- if (
- not math.isfinite(loop_watchdog_probe_timeout_s)
- or loop_watchdog_probe_timeout_s < 1.0
- or loop_watchdog_probe_timeout_s > 600.0
- ):
- loop_watchdog_probe_timeout_s = DEFAULT_LOOP_WATCHDOG_TIMEOUT_S
- if loop_watchdog_max_strikes < 1 or loop_watchdog_max_strikes > 1000:
- loop_watchdog_max_strikes = DEFAULT_LOOP_WATCHDOG_MAX_STRIKES
- if multiplex_profiles is None and isinstance(nested_gateway, dict):
- # Also honor gateway.multiplex_profiles written by
- # ``hermes config set gateway.multiplex_profiles true``.
+
+ # Multiplexing is a genuine 3-tier chain: env > config.yaml > default False. The
+ # GATEWAY_MULTIPLEX_PROFILES operator override wins when set to a recognized value
+ # (hosted deployments stamp it on the container so the single multiplexed gateway the
+ # connector depends on is forced on at every boot regardless of the image's config.yaml);
+ # a blank or unrecognized env value falls through to config — a provisioned-but-
+ # unpopulated Fly secret must not shadow a config.yaml opt-in. Config side: the
+ # top-level VALUE wins when not None, else ``gateway.multiplex_profiles`` (written by
+ # ``hermes config set gateway.multiplex_profiles true``).
+ multiplex_profiles = data.get("multiplex_profiles")
+ if multiplex_profiles is None:
multiplex_profiles = nested_gateway.get("multiplex_profiles")
- # Operator override: GATEWAY_MULTIPLEX_PROFILES wins over config.yaml when
- # set to a recognized value. Hosted deployments (Nous Portal / Fly) stamp
- # it on the container so the single multiplexed gateway — which the
- # connector now depends on for per-profile relay routing — is forced on at
- # every boot regardless of the image's config.yaml, while self-hosted
- # users keep setting gateway.multiplex_profiles in config.yaml. A blank or
- # unrecognized env value falls through to config (the empty-secret trap:
- # a provisioned-but-unpopulated Fly secret must not shadow config), so
- # this is a genuine 3-tier chain: env > config.yaml > default False.
env_multiplex = _env_multiplex_profiles_override()
if env_multiplex is not None:
multiplex_profiles = env_multiplex
- if "max_concurrent_sessions" in data:
- max_concurrent_raw = data.get("max_concurrent_sessions")
- max_concurrent_key = "max_concurrent_sessions"
- else:
- max_concurrent_raw = nested_gateway.get("max_concurrent_sessions")
- max_concurrent_key = "gateway.max_concurrent_sessions"
+
max_concurrent_sessions = _coerce_optional_positive_int(
- max_concurrent_raw,
- max_concurrent_key,
- )
- unauthorized_dm_behavior = _normalize_unauthorized_dm_behavior(
- data.get("unauthorized_dm_behavior"),
- "pair",
+ pick("max_concurrent_sessions"), key_label("max_concurrent_sessions")
)
try:
- session_store_max_age_days = int(data.get("session_store_max_age_days", 90))
- session_store_max_age_days = max(session_store_max_age_days, 0)
+ session_store_max_age_days = max(int(data.get("session_store_max_age_days", 90)), 0)
except (TypeError, ValueError):
session_store_max_age_days = 90
@@ -1321,31 +1159,36 @@ class GatewayConfig:
return cls(
platforms=platforms,
- default_reset_policy=default_policy,
- reset_by_type=reset_by_type,
+ default_reset_policy=SessionResetPolicy.from_dict(data["default_reset_policy"])
+ if "default_reset_policy" in data
+ else SessionResetPolicy(),
+ reset_by_type={
+ type_name: SessionResetPolicy.from_dict(policy_data)
+ for type_name, policy_data in _coerce_dict(data.get("reset_by_type", {})).items()
+ },
reset_by_platform=reset_by_platform,
reset_triggers=data.get("reset_triggers", ["/new", "/reset"]),
- quick_commands=quick_commands,
- sessions_dir=sessions_dir,
+ quick_commands=_coerce_dict(data.get("quick_commands", {})),
+ sessions_dir=Path(data["sessions_dir"]) if "sessions_dir" in data else get_hermes_home() / "sessions",
write_sessions_json=_coerce_bool(data.get("write_sessions_json"), True),
always_log_local=_coerce_bool(data.get("always_log_local"), True),
- filter_silence_narration=_coerce_bool(
- data.get("filter_silence_narration"), True
- ),
+ filter_silence_narration=_coerce_bool(data.get("filter_silence_narration"), True),
stt_enabled=_coerce_bool(stt_enabled, True),
stt_echo_transcripts=_coerce_bool(stt_echo_transcripts, True),
- group_sessions_per_user=_coerce_bool(group_sessions_per_user, True),
- thread_sessions_per_user=_coerce_bool(thread_sessions_per_user, False),
+ group_sessions_per_user=_coerce_bool(data.get("group_sessions_per_user"), True),
+ thread_sessions_per_user=_coerce_bool(data.get("thread_sessions_per_user"), False),
multiplex_profiles=_coerce_bool(multiplex_profiles, False),
- multiplex_profile_allowlist=multiplex_profile_allowlist,
+ multiplex_profile_allowlist=pick("multiplex_profile_allowlist"),
room_link_url=room_link_url,
systemd_watchdog_seconds=systemd_watchdog_seconds,
- loop_watchdog=loop_watchdog,
- loop_watchdog_probe_interval_s=loop_watchdog_probe_interval_s,
- loop_watchdog_probe_timeout_s=loop_watchdog_probe_timeout_s,
- loop_watchdog_max_strikes=loop_watchdog_max_strikes,
+ loop_watchdog=_coerce_bool(pick("loop_watchdog"), True),
+ loop_watchdog_probe_interval_s=probe_interval,
+ loop_watchdog_probe_timeout_s=probe_timeout,
+ loop_watchdog_max_strikes=max_strikes,
max_concurrent_sessions=max_concurrent_sessions,
- unauthorized_dm_behavior=unauthorized_dm_behavior,
+ unauthorized_dm_behavior=_normalize_choice(
+ data.get("unauthorized_dm_behavior"), {"pair", "ignore"}, "pair"
+ ),
streaming=StreamingConfig.from_dict(data.get("streaming", {})),
session_store_max_age_days=session_store_max_age_days,
profile_routes=profile_routes,
@@ -1361,8 +1204,9 @@ class GatewayConfig:
if platform:
platform_cfg = self.platforms.get(platform)
if platform_cfg and "unauthorized_dm_behavior" in platform_cfg.extra:
- return _normalize_unauthorized_dm_behavior(
+ return _normalize_choice(
platform_cfg.extra.get("unauthorized_dm_behavior"),
+ {"pair", "ignore"},
self.unauthorized_dm_behavior,
)
if platform == Platform.EMAIL:
@@ -1374,9 +1218,8 @@ class GatewayConfig:
if platform:
platform_cfg = self.platforms.get(platform)
if platform_cfg and "notice_delivery" in platform_cfg.extra:
- return _normalize_notice_delivery(
- platform_cfg.extra.get("notice_delivery"),
- "public",
+ return _normalize_choice(
+ platform_cfg.extra.get("notice_delivery"), {"public", "private"}, "public"
)
return "public"
@@ -1391,515 +1234,12 @@ def load_gateway_config() -> GatewayConfig:
3. ~/.hermes/gateway.json (legacy — provides defaults under config.yaml)
4. Built-in defaults
"""
- _home = get_hermes_home()
- gw_data: dict = {}
-
- # Legacy fallback: gateway.json provides the base layer.
- # config.yaml keys always win when both specify the same setting.
- gateway_json_path = _home / "gateway.json"
- if gateway_json_path.exists():
- try:
- with open(gateway_json_path, "r", encoding="utf-8") as f:
- gw_data = json.load(f) or {}
- logger.info(
- "Loaded legacy %s — consider moving settings to config.yaml",
- gateway_json_path,
- )
- except Exception as e:
- logger.warning("Failed to load %s: %s", gateway_json_path, e)
+ from gateway import config_loader
- # Primary source: config.yaml
+ _home = get_hermes_home()
+ gw_data = config_loader.load_legacy_gateway_json(_home)
try:
- import yaml
- config_yaml_path = _home / "config.yaml"
- if config_yaml_path.exists():
- with open(config_yaml_path, encoding="utf-8") as f:
- yaml_cfg = yaml.safe_load(f) or {}
-
- # Managed scope: overlay administrator-pinned values so the gateway
- # honors them too. This loader builds its own dict instead of going
- # through hermes_cli.config.load_config, so without this a managed
- # session_reset / quick_commands / stt / model would be ignored by
- # the messaging gateway. Fail-open via the shared helper.
- from hermes_cli import managed_scope
- yaml_cfg = managed_scope.apply_managed_overlay(yaml_cfg)
-
- # Shared nested-fallback source: settings meant to be top-level
- # keys are also accepted when a user nests them under `gateway:`
- # (e.g. via `hermes config set gateway.<key> ...`, which naturally
- # produces that shape). Every key below mirrors the precedent
- # already established for gateway.multiplex_profiles/streaming/
- # write_sessions_json: top-level wins, nested gateway.* falls back.
- gateway_section = yaml_cfg.get("gateway")
-
- # Map config.yaml keys → GatewayConfig.from_dict() schema.
- # Each key overwrites whatever gateway.json may have set.
- # Precedence contract: key-presence at the TOP LEVEL wins; the
- # nested gateway.* form is consulted only when the top-level key
- # is absent (not merely falsy/mistyped), so a present-but-empty
- # top-level value is never silently replaced by the nested one.
- sr = yaml_cfg.get("session_reset")
- if "session_reset" not in yaml_cfg and isinstance(gateway_section, dict):
- sr = gateway_section.get("session_reset")
- if sr and isinstance(sr, dict):
- gw_data["default_reset_policy"] = sr
-
- qc = yaml_cfg.get("quick_commands")
- if qc is None and isinstance(gateway_section, dict):
- qc = gateway_section.get("quick_commands")
- if qc is not None:
- if isinstance(qc, dict):
- gw_data["quick_commands"] = qc
- else:
- logger.warning(
- "Ignoring invalid quick_commands in config.yaml "
- "(expected mapping, got %s)",
- type(qc).__name__,
- )
-
- stt_cfg = yaml_cfg.get("stt")
- if "stt" not in yaml_cfg and isinstance(gateway_section, dict):
- stt_cfg = gateway_section.get("stt")
- if isinstance(stt_cfg, dict):
- gw_data["stt"] = stt_cfg
- if "stt_echo_transcripts" in yaml_cfg:
- gw_data["stt_echo_transcripts"] = yaml_cfg["stt_echo_transcripts"]
- elif isinstance(gateway_section, dict) and "stt_echo_transcripts" in gateway_section:
- gw_data["stt_echo_transcripts"] = gateway_section["stt_echo_transcripts"]
-
- gateway_cfg = yaml_cfg.get("gateway")
-
- if "group_sessions_per_user" in yaml_cfg:
- gw_data["group_sessions_per_user"] = yaml_cfg["group_sessions_per_user"]
- elif isinstance(gateway_section, dict) and "group_sessions_per_user" in gateway_section:
- gw_data["group_sessions_per_user"] = gateway_section["group_sessions_per_user"]
-
- if "thread_sessions_per_user" in yaml_cfg:
- gw_data["thread_sessions_per_user"] = yaml_cfg["thread_sessions_per_user"]
- elif isinstance(gateway_section, dict) and "thread_sessions_per_user" in gateway_section:
- gw_data["thread_sessions_per_user"] = gateway_section["thread_sessions_per_user"]
-
- # Multiplexing flag: accept both the top-level key and the nested
- # gateway.multiplex_profiles form (written by
- # ``hermes config set gateway.multiplex_profiles true``).
- if "multiplex_profiles" in yaml_cfg:
- gw_data["multiplex_profiles"] = yaml_cfg["multiplex_profiles"]
-
- if "multiplex_profile_allowlist" in yaml_cfg:
- gw_data["multiplex_profile_allowlist"] = yaml_cfg[
- "multiplex_profile_allowlist"
- ]
- elif (
- isinstance(gateway_section, dict)
- and "multiplex_profile_allowlist" in gateway_section
- ):
- gw_data["multiplex_profile_allowlist"] = gateway_section[
- "multiplex_profile_allowlist"
- ]
-
- if "room_link_url" in yaml_cfg:
- gw_data["room_link_url"] = yaml_cfg["room_link_url"]
- elif isinstance(gateway_section, dict) and "room_link_url" in gateway_section:
- gw_data["room_link_url"] = gateway_section["room_link_url"]
-
- # Profile-based routing rules: accept either top-level
- # ``profile_routes`` or the nested ``gateway.profile_routes`` form
- # (matching the multiplex_profiles parity above).
- _pr = yaml_cfg.get("profile_routes")
- if _pr is None and isinstance(gateway_section, dict):
- _pr = gateway_section.get("profile_routes")
- if isinstance(_pr, list):
- gw_data["profile_routes"] = _pr
-
- if isinstance(gateway_section, dict):
- if "multiplex_profiles" in gateway_section and "multiplex_profiles" not in gw_data:
- # gateway.multiplex_profiles written by `hermes config set gateway.multiplex_profiles true`
- gw_data["multiplex_profiles"] = gateway_section["multiplex_profiles"]
- if "max_concurrent_sessions" in gateway_section:
- gw_data["max_concurrent_sessions"] = gateway_section["max_concurrent_sessions"]
- if "systemd_watchdog_seconds" in gateway_section:
- gw_data["systemd_watchdog_seconds"] = gateway_section[
- "systemd_watchdog_seconds"
- ]
-
- if "max_concurrent_sessions" in yaml_cfg:
- gw_data["max_concurrent_sessions"] = yaml_cfg["max_concurrent_sessions"]
-
- streaming_cfg = yaml_cfg.get("streaming")
- if not isinstance(streaming_cfg, dict) and isinstance(gateway_section, dict):
- # Fall back to nested gateway.streaming written by
- # ``hermes config set gateway.streaming.*``
- streaming_cfg = gateway_section.get("streaming")
- if isinstance(streaming_cfg, dict):
- gw_data["streaming"] = streaming_cfg
-
- if "reset_triggers" in yaml_cfg:
- gw_data["reset_triggers"] = yaml_cfg["reset_triggers"]
- elif isinstance(gateway_section, dict) and "reset_triggers" in gateway_section:
- gw_data["reset_triggers"] = gateway_section["reset_triggers"]
-
- if "always_log_local" in yaml_cfg:
- gw_data["always_log_local"] = yaml_cfg["always_log_local"]
- elif isinstance(gateway_section, dict) and "always_log_local" in gateway_section:
- gw_data["always_log_local"] = gateway_section["always_log_local"]
-
- # write_sessions_json: top-level wins; nested gateway.* fallback
- # (matches the gateway.streaming precedence pattern).
- if "write_sessions_json" in yaml_cfg:
- gw_data["write_sessions_json"] = yaml_cfg["write_sessions_json"]
- elif isinstance(gateway_section, dict) and "write_sessions_json" in gateway_section:
- gw_data["write_sessions_json"] = gateway_section["write_sessions_json"]
-
- # Loop-liveness watchdog toggle + tuning knobs: top-level wins;
- # nested gateway.* fallback. GatewayConfig.from_dict has its own
- # nested fallback, but this loader builds gw_data FLAT and never
- # forwards the yaml `gateway:` section — without this bridge the
- # documented keys (including the pre-existing loop_watchdog bool)
- # were silently ignored on the real gateway startup path.
- for _wd_key in (
- "loop_watchdog",
- "loop_watchdog_probe_interval_s",
- "loop_watchdog_probe_timeout_s",
- "loop_watchdog_max_strikes",
- ):
- if _wd_key in yaml_cfg:
- gw_data[_wd_key] = yaml_cfg[_wd_key]
- elif isinstance(gateway_section, dict) and _wd_key in gateway_section:
- gw_data[_wd_key] = gateway_section[_wd_key]
-
- if "filter_silence_narration" in yaml_cfg:
- gw_data["filter_silence_narration"] = yaml_cfg[
- "filter_silence_narration"
- ]
- elif isinstance(gateway_section, dict) and "filter_silence_narration" in gateway_section:
- gw_data["filter_silence_narration"] = gateway_section[
- "filter_silence_narration"
- ]
-
- if "unauthorized_dm_behavior" in yaml_cfg:
- gw_data["unauthorized_dm_behavior"] = _normalize_unauthorized_dm_behavior(
- yaml_cfg.get("unauthorized_dm_behavior"),
- "pair",
- )
- elif isinstance(gateway_section, dict) and "unauthorized_dm_behavior" in gateway_section:
- gw_data["unauthorized_dm_behavior"] = _normalize_unauthorized_dm_behavior(
- gateway_section.get("unauthorized_dm_behavior"),
- "pair",
- )
-
- # Merge platform config into gw_data so runtime-only settings under
- # ``gateway.platforms`` are loaded the same way as top-level
- # ``platforms``. Merge nested first so top-level config keeps
- # precedence, matching the existing gateway.streaming fallback.
- gateway_platforms = gateway_cfg.get("platforms") if isinstance(gateway_cfg, dict) else None
- platforms_data = gw_data.setdefault("platforms", {})
- if not isinstance(platforms_data, dict):
- platforms_data = {}
- gw_data["platforms"] = platforms_data
-
- def _merge_platform_map(source_platforms: Any) -> None:
- if not isinstance(source_platforms, dict):
- return
- for plat_name, plat_block in source_platforms.items():
- if not isinstance(plat_block, dict):
- continue
- existing = platforms_data.get(plat_name, {})
- if not isinstance(existing, dict):
- existing = {}
- # Deep-merge extra dicts so gateway.json defaults survive
- merged_extra = {**existing.get("extra", {}), **plat_block.get("extra", {})}
- if "enabled" in plat_block:
- merged_extra["_enabled_explicit"] = True
- merged = {**existing, **plat_block}
- if merged_extra:
- merged["extra"] = merged_extra
- platforms_data[plat_name] = merged
-
- _merge_platform_map(gateway_platforms)
- _merge_platform_map(yaml_cfg.get("platforms"))
-
- # Also merge platform configs placed directly under ``gateway.*``
- # (e.g. ``gateway.api_server``) so subsections are discovered the
- # same way ``gateway.streaming`` is handled elsewhere. Iterate
- # all ``gateway:*`` keys and merge only those that match a known
- # platform value, skipping reserved keys like ``platforms``.
- if isinstance(gateway_cfg, dict):
- _nested_platforms: dict = {}
- for _k, _v in gateway_cfg.items():
- if _k == "platforms":
- continue
- try:
- Platform(_k)
- except (ValueError, AttributeError):
- continue
- if isinstance(_v, dict):
- _nested_platforms[_k] = _v
- if _nested_platforms:
- _merge_platform_map(_nested_platforms)
-
- # Bridge api_server-specific keys (port, key, host, cors_origins,
- # model_name) into extra so PlatformConfig.from_dict preserves
- # them — adapting what _apply_env_overrides does for env vars to
- # the YAML path. Users writing ``gateway.api_server.port: 8642``
- # expect these to end up in the platform's extra dict.
- _api_plat = platforms_data.get("api_server")
- if isinstance(_api_plat, dict):
- _api_extra = _api_plat.get("extra")
- if not isinstance(_api_extra, dict):
- _api_extra = {}
- _api_plat["extra"] = _api_extra
- for _bridge_key in ("port", "key", "host", "cors_origins", "model_name"):
- if _bridge_key in _api_plat and _bridge_key not in _api_extra:
- _api_extra[_bridge_key] = _api_plat.pop(_bridge_key)
-
- if platforms_data:
- gw_data["platforms"] = platforms_data
- # Iterate built-in platforms plus any registered plugin platforms
- # so plugin authors get the same shared-key bridging (#24836).
- try:
- from hermes_cli.plugins import discover_plugins
- discover_plugins() # idempotent
- from gateway.platform_registry import platform_registry as _pr
- except Exception as e:
- logger.debug("plugin discovery skipped: %s", e)
- _pr = None
-
- _shared_loop_targets: list = list(Platform)
- if _pr is not None:
- for _entry in _pr.plugin_entries():
- try:
- _plat = Platform(_entry.name)
- except (ValueError, KeyError):
- continue
- if _plat not in _shared_loop_targets:
- _shared_loop_targets.append(_plat)
-
- for plat in _shared_loop_targets:
- if plat == Platform.LOCAL:
- continue
- platform_cfg = yaml_cfg.get(plat.value)
- _cfg_toplevel = isinstance(platform_cfg, dict)
- # Fall back to the platform's block under ``platforms`` /
- # ``gateway.platforms`` so shared-key bridging (allow_from,
- # require_mention, free_response_channels, …) still runs when
- # the user configured the platform only under those nested paths
- # and not via a top-level block. Mirrors the identical fallback
- # already applied to the apply_yaml_config_fn dispatch below
- # (#44f3e51).
- # Note: ``enabled`` is only written to plat_data from a
- # top-level block (``_cfg_toplevel``); for nested-only configs
- # ``_merge_platform_map`` already merged it with the correct
- # precedence, so re-applying it here would overwrite that.
- if not _cfg_toplevel:
- for _src in (gateway_platforms, yaml_cfg.get("platforms")):
- if isinstance(_src, dict):
- _candidate = _src.get(plat.value)
- if isinstance(_candidate, dict):
- platform_cfg = _candidate
- break
- if not isinstance(platform_cfg, dict):
- continue
- # Collect bridgeable keys from this platform section
- bridged = {}
- if "unauthorized_dm_behavior" in platform_cfg:
- bridged["unauthorized_dm_behavior"] = _normalize_unauthorized_dm_behavior(
- platform_cfg.get("unauthorized_dm_behavior"),
- gw_data.get("unauthorized_dm_behavior", "pair"),
- )
- if "notice_delivery" in platform_cfg:
- bridged["notice_delivery"] = _normalize_notice_delivery(
- platform_cfg.get("notice_delivery"),
- "public",
- )
- if "reply_prefix" in platform_cfg:
- bridged["reply_prefix"] = platform_cfg["reply_prefix"]
- if "reply_in_thread" in platform_cfg:
- bridged["reply_in_thread"] = platform_cfg["reply_in_thread"]
- if "cron_continuable_surface" in platform_cfg:
- bridged["cron_continuable_surface"] = platform_cfg["cron_continuable_surface"]
- if "require_mention" in platform_cfg:
- bridged["require_mention"] = platform_cfg["require_mention"]
- if "send_read_receipts" in platform_cfg:
- bridged["send_read_receipts"] = platform_cfg["send_read_receipts"]
- if plat == Platform.TELEGRAM and "allowed_chats" in platform_cfg:
- bridged["allowed_chats"] = platform_cfg["allowed_chats"]
- if plat == Platform.TELEGRAM and "group_allowed_chats" in platform_cfg:
- bridged["group_allowed_chats"] = platform_cfg["group_allowed_chats"]
- if plat == Platform.TELEGRAM and "allowed_topics" in platform_cfg:
- bridged["allowed_topics"] = platform_cfg["allowed_topics"]
- if "free_response_channels" in platform_cfg:
- bridged["free_response_channels"] = platform_cfg["free_response_channels"]
- if "mention_patterns" in platform_cfg:
- bridged["mention_patterns"] = platform_cfg["mention_patterns"]
- if "exclusive_bot_mentions" in platform_cfg:
- bridged["exclusive_bot_mentions"] = platform_cfg["exclusive_bot_mentions"]
- if plat == Platform.TELEGRAM and "observe_unmentioned_group_messages" in platform_cfg:
- bridged["observe_unmentioned_group_messages"] = platform_cfg["observe_unmentioned_group_messages"]
- if "dm_policy" in platform_cfg:
- bridged["dm_policy"] = platform_cfg["dm_policy"]
- if "allow_from" in platform_cfg:
- bridged["allow_from"] = platform_cfg["allow_from"]
- if "allow_admin_from" in platform_cfg:
- bridged["allow_admin_from"] = platform_cfg["allow_admin_from"]
- if "user_allowed_commands" in platform_cfg:
- bridged["user_allowed_commands"] = platform_cfg["user_allowed_commands"]
- if "group_policy" in platform_cfg:
- bridged["group_policy"] = platform_cfg["group_policy"]
- if "group_allow_from" in platform_cfg:
- bridged["group_allow_from"] = platform_cfg["group_allow_from"]
- if "group_allow_admin_from" in platform_cfg:
- bridged["group_allow_admin_from"] = platform_cfg["group_allow_admin_from"]
- if "group_user_allowed_commands" in platform_cfg:
- bridged["group_user_allowed_commands"] = platform_cfg["group_user_allowed_commands"]
- if plat in {Platform.DISCORD, Platform.SLACK} and "channel_skill_bindings" in platform_cfg:
- bridged["channel_skill_bindings"] = platform_cfg["channel_skill_bindings"]
- if "channel_prompts" in platform_cfg:
- channel_prompts = platform_cfg["channel_prompts"]
- if isinstance(channel_prompts, dict):
- bridged["channel_prompts"] = {str(k): v for k, v in channel_prompts.items()}
- else:
- bridged["channel_prompts"] = channel_prompts
- if "gateway_restart_notification" in platform_cfg:
- bridged["gateway_restart_notification"] = platform_cfg["gateway_restart_notification"]
- if "typing_indicator" in platform_cfg:
- bridged["typing_indicator"] = platform_cfg["typing_indicator"]
- if "typing_status_text" in platform_cfg:
- bridged["typing_status_text"] = platform_cfg["typing_status_text"]
- # Bridge top-level port/host/secret into extra for platforms
- # whose adapters read these from config.extra (webhook,
- # msgraph_webhook, api_server). Without this, YAML like:
- # platforms:
- # webhook:
- # enabled: true
- # port: 8649
- # silently falls back to the hardcoded DEFAULT_PORT because
- # PlatformConfig.from_dict only extracts ``extra`` from the
- # ``extra:`` sub-key, not from the top level.
- if plat in {Platform.WEBHOOK, Platform.MSGRAPH_WEBHOOK}:
- for _bridge_key in ("port", "host", "secret"):
- if _bridge_key in platform_cfg and _bridge_key not in platform_cfg.get("extra", {}):
- bridged[_bridge_key] = platform_cfg[_bridge_key]
- if plat == Platform.API_SERVER:
- for _bridge_key in ("port", "host"):
- if _bridge_key in platform_cfg and _bridge_key not in platform_cfg.get("extra", {}):
- bridged[_bridge_key] = platform_cfg[_bridge_key]
- has_channel_overrides = "channel_overrides" in platform_cfg
- if has_channel_overrides:
- raw_overrides = platform_cfg.get("channel_overrides")
- if isinstance(raw_overrides, dict):
- plat_data, _extra = _ensure_platform_extra_dict(
- platforms_data, plat.value
- )
- plat_data["channel_overrides"] = {
- str(cid): ov_data
- for cid, ov_data in raw_overrides.items()
- if isinstance(ov_data, dict)
- }
- enabled_was_explicit = _cfg_toplevel and "enabled" in platform_cfg
- if not bridged and not enabled_was_explicit and not has_channel_overrides:
- continue
- plat_data, extra = _ensure_platform_extra_dict(platforms_data, plat.value)
- if enabled_was_explicit:
- plat_data["enabled"] = platform_cfg["enabled"]
- # Mark the explicit enable/disable so the registry-driven
- # plugin-enable pass in _apply_env_overrides honors an
- # explicit ``enabled: false`` for migrated plugin platforms
- # (slack, telegram, matrix, dingtalk, whatsapp, feishu …)
- # instead of re-enabling them on token/SDK presence. #41112.
- extra["_enabled_explicit"] = True
- extra.update(bridged)
-
- # Plugin-owned YAML→env config bridges (#24836). See
- # ``PlatformEntry.apply_yaml_config_fn`` for the hook contract.
- # Order: shared-key loop (above) → this dispatch → legacy hardcoded
- # blocks (below; no-op when a hook already set their env var) →
- # ``_apply_env_overrides()`` after ``GatewayConfig.from_dict``.
- if _pr is not None:
- for entry in _pr.all_entries():
- if entry.apply_yaml_config_fn is None:
- continue
- platform_cfg = yaml_cfg.get(entry.name)
- # Fall back to the platform's block under ``platforms`` /
- # ``gateway.platforms`` so adapter hooks still run when the
- # user configured the platform only under those nested paths
- # (e.g. ``platforms.discord.extra.allow_from``) and not via a
- # top-level ``discord:`` block.
- if not isinstance(platform_cfg, dict):
- for _src in (gateway_platforms, yaml_cfg.get("platforms")):
- if isinstance(_src, dict):
- _candidate = _src.get(entry.name)
- if isinstance(_candidate, dict):
- platform_cfg = _candidate
- break
- if not isinstance(platform_cfg, dict):
- continue
- try:
- seeded = entry.apply_yaml_config_fn(yaml_cfg, platform_cfg)
- except Exception as e:
- logger.debug(
- "apply_yaml_config_fn for %s raised: %s",
- entry.name, e,
- )
- continue
- if not isinstance(seeded, dict) or not seeded:
- continue
- _, extra = _ensure_platform_extra_dict(platforms_data, entry.name)
- extra.update(seeded)
-
- # Slack settings → env vars: migrated to the slack plugin's
- # ``apply_yaml_config_fn`` hook (see plugins/platforms/slack/
- # adapter.py::_apply_yaml_config), dispatched in the
- # ``apply_yaml_config_fn`` loop above. #41112 / #3823.
-
- # Bridge top-level require_mention to Telegram when the telegram: section
- # does not already provide one. Users often write "require_mention: true"
- # at the top level alongside group_sessions_per_user, expecting it to work
- # the same way (#3979).
- _tl_require_mention = yaml_cfg.get("require_mention")
- if _tl_require_mention is not None:
- _tg_section = yaml_cfg.get("telegram") or {}
- if "require_mention" not in _tg_section:
- _tg_plat = platforms_data.setdefault(Platform.TELEGRAM.value, {})
- _tg_extra = _tg_plat.setdefault("extra", {})
- _tg_extra.setdefault("require_mention", _tl_require_mention)
- # Also bridge to the TELEGRAM_REQUIRE_MENTION env var that the
- # adapter reads at runtime. This used to live in the telegram_cfg
- # block in core; it stays in core because it keys off the TOP-LEVEL
- # require_mention (not a telegram: block), so the telegram plugin's
- # apply_yaml_config_fn hook — which only runs when a telegram config
- # block exists — can't cover the no-telegram-block case (#3979).
- if not os.getenv("TELEGRAM_REQUIRE_MENTION"):
- os.environ["TELEGRAM_REQUIRE_MENTION"] = str(_tl_require_mention).lower()
-
- # Telegram settings → env vars / extra: migrated to the telegram
- # plugin's apply_yaml_config_fn hook
- # (plugins/platforms/telegram/adapter.py). #41112 / #3823.
-
- # WhatsApp settings → env vars: migrated to the whatsapp plugin's
- # apply_yaml_config_fn hook (plugins/platforms/whatsapp/adapter.py).
- # #41112 / #3823.
-
- # Signal settings → env vars (env vars take precedence)
- signal_cfg = yaml_cfg.get("signal", {})
- if isinstance(signal_cfg, dict):
- if "require_mention" in signal_cfg and not os.getenv("SIGNAL_REQUIRE_MENTION"):
- os.environ["SIGNAL_REQUIRE_MENTION"] = str(signal_cfg["require_mention"]).lower()
-
- # DingTalk settings → env vars: migrated to the dingtalk plugin's
- # apply_yaml_config_fn hook (plugins/platforms/dingtalk/adapter.py).
- # #41112 / #3823.
-
- # Mattermost config bridge moved into plugins/platforms/mattermost/
- # adapter.py::_apply_yaml_config — see #25443 (apply_yaml_config_fn).
-
- # Matrix settings → env vars: migrated to the matrix plugin's
- # apply_yaml_config_fn hook (plugins/platforms/matrix/adapter.py).
- # #41112 / #3823.
-
- # Feishu settings → env vars: migrated to the feishu plugin's
- # apply_yaml_config_fn hook (plugins/platforms/feishu/adapter.py).
- # #41112 / #3823.
-
+ config_loader.load_yaml_layer(_home, gw_data)
except Exception as e:
logger.warning(
"Failed to process config.yaml — falling back to .env / gateway.json values. "
@@ -1909,13 +1249,8 @@ def load_gateway_config() -> GatewayConfig:
)
config = GatewayConfig.from_dict(gw_data)
-
- # Override with environment variables
_apply_env_overrides(config)
-
- # --- Validate loaded values ---
_validate_gateway_config(config)
-
return config
@@ -1942,11 +1277,10 @@ def _validate_gateway_config(config: "GatewayConfig") -> None:
# Warn about empty bot tokens — platforms that loaded an empty string
# won't connect and the cause can be confusing without a log line.
- _token_env_names = PLATFORM_TOKEN_ENV_NAMES
for platform, pconfig in config.platforms.items():
if not pconfig.enabled:
continue
- env_name = _token_env_names.get(platform)
+ env_name = PLATFORM_TOKEN_ENV_NAMES.get(platform)
if env_name and pconfig.token is not None and not pconfig.token.strip():
logger.warning(
"%s is enabled but %s is empty. "
@@ -1961,24 +1295,21 @@ def _validate_gateway_config(config: "GatewayConfig") -> None:
try:
from hermes_cli.auth import has_usable_secret
except ImportError:
- has_usable_secret = None # type: ignore[assignment]
+ return
- if has_usable_secret is not None:
- for platform, pconfig in config.platforms.items():
- if not pconfig.enabled:
- continue
- env_name = _token_env_names.get(platform)
- if not env_name:
- continue
- token = pconfig.token
- if token and token.strip() and not has_usable_secret(token, min_length=4):
- logger.error(
- "%s is enabled but %s is set to a placeholder value ('%s'). "
- "Set a real bot token before starting the gateway. "
- "The adapter will NOT be started.",
- platform.value, env_name, token.strip()[:6] + "...",
- )
- pconfig.enabled = False
+ for platform, pconfig in config.platforms.items():
+ env_name = PLATFORM_TOKEN_ENV_NAMES.get(platform)
+ token = pconfig.token
+ if not (pconfig.enabled and env_name and token and token.strip()):
+ continue
+ if not has_usable_secret(token, min_length=4):
+ logger.error(
+ "%s is enabled but %s is set to a placeholder value ('%s'). "
+ "Set a real bot token before starting the gateway. "
+ "The adapter will NOT be started.",
+ platform.value, env_name, token.strip()[:6] + "...",
+ )
+ pconfig.enabled = False
def _apply_env_overrides(config: GatewayConfig) -> None:
diff --git a/gateway/config_env.py b/gateway/config_env.py
index e977ec21c98..fc568819755 100644
--- a/gateway/config_env.py
+++ b/gateway/config_env.py
@@ -365,11 +365,10 @@ def _msgraph_webhook(config: GatewayConfig) -> None:
):
return
msgraph_cfg = config.platforms.setdefault(Platform.MSGRAPH_WEBHOOK, PlatformConfig())
- if enabled:
- # Same explicit-disable guard as the webhook branch, but READ (don't pop) the marker:
- # the relay-exclusive pass below still consults it; the end-of-function scrub removes it.
- if not msgraph_cfg.extra.get("_enabled_explicit", False) or msgraph_cfg.enabled:
- msgraph_cfg.enabled = True
+ # Same explicit-disable guard as the webhook branch, but READ (don't pop) the marker:
+ # the relay-exclusive pass below still consults it; the end-of-function scrub removes it.
+ if enabled and (not msgraph_cfg.extra.get("_enabled_explicit", False) or msgraph_cfg.enabled):
+ msgraph_cfg.enabled = True
_env_extras(msgraph_cfg.extra, (("port", "MSGRAPH_WEBHOOK_PORT", _INT),))
if client_state:
msgraph_cfg.extra["client_state"] = client_state
diff --git a/gateway/config_loader.py b/gateway/config_loader.py
new file mode 100644
index 00000000000..abec9891a2a
--- /dev/null
+++ b/gateway/config_loader.py
@@ -0,0 +1,396 @@
+"""config.yaml / gateway.json → ``GatewayConfig.from_dict`` schema (the ``load_gateway_config`` phases).
+
+Precedence contract for top-level keys: key-presence at the TOP LEVEL of config.yaml wins; the
+nested ``gateway.<key>`` form (what ``hermes config set gateway.<key>`` produces) is consulted only
+when the top-level key is absent — not merely falsy/mistyped — so a present-but-empty top-level
+value is never silently replaced by the nested one. Both overwrite whatever legacy gateway.json set.
+"""
+
+import json
+import logging
+import os
+from pathlib import Path
+from typing import Any
+
+from gateway.config import Platform, _dict_slot, _normalize_choice
+
+# Logger name parity with the origin module: records stay under "gateway.config".
+logger = logging.getLogger("gateway.config")
+
+
+def load_legacy_gateway_json(home: Path) -> Any:
+ """Legacy ``gateway.json`` base layer (config.yaml keys always win). Malformed → ``{}`` + warning."""
+ path = home / "gateway.json"
+ if not path.exists():
+ return {}
+ try:
+ with open(path, "r", encoding="utf-8") as f:
+ data = json.load(f) or {}
+ logger.info("Loaded legacy %s — consider moving settings to config.yaml", path)
+ return data
+ except Exception as e:
+ logger.warning("Failed to load %s: %s", path, e)
+ return {}
+
+
+# --- top-level key bridging ----------------------------------------------------
+#
+# Settings meant to be top-level keys are also accepted nested under ``gateway:`` (what
+# ``hermes config set gateway.<key> ...`` naturally produces). This loader builds gw_data FLAT
+# and never forwards the yaml ``gateway:`` section, so even keys GatewayConfig.from_dict can
+# fall back on itself (loop_watchdog*, multiplex_profiles, ...) must be bridged here or they
+# are silently ignored on the real gateway startup path.
+#
+# Fallback modes (how the nested ``gateway.<key>`` form is consulted):
+# "presence": top-level key present → its value; else nested key present.
+# "gwdata": like "presence", but nested only when NOTHING (config.yaml or gateway.json) set it yet.
+# "none": top-level VALUE is None → nested value.
+# "dict": top-level value is not a mapping → nested value; accepted only if a mapping.
+# "nested": nested form only (no top-level spelling is bridged).
+
+def _quick_commands_ok(value: Any) -> bool:
+ if isinstance(value, dict):
+ return True
+ logger.warning(
+ "Ignoring invalid quick_commands in config.yaml (expected mapping, got %s)",
+ type(value).__name__,
+ )
+ return False
+
+
+def _is_dict(value: Any) -> bool:
+ return isinstance(value, dict)
+
+
+# (yaml key, gw_data key, mode, accept(value) -> bool, transform(value))
+_TOPLEVEL_BRIDGE: tuple = (
+ ("session_reset", "default_reset_policy", "presence", lambda v: bool(v) and isinstance(v, dict), None),
+ ("quick_commands", "quick_commands", "none", _quick_commands_ok, None),
+ ("stt", "stt", "presence", _is_dict, None),
+ ("stt_echo_transcripts", "stt_echo_transcripts", "presence", None, None),
+ ("group_sessions_per_user", "group_sessions_per_user", "presence", None, None),
+ ("thread_sessions_per_user", "thread_sessions_per_user", "presence", None, None),
+ ("multiplex_profiles", "multiplex_profiles", "gwdata", None, None),
+ ("multiplex_profile_allowlist", "multiplex_profile_allowlist", "presence", None, None),
+ ("room_link_url", "room_link_url", "presence", None, None),
+ ("profile_routes", "profile_routes", "none", lambda v: isinstance(v, list), None),
+ ("max_concurrent_sessions", "max_concurrent_sessions", "presence", None, None),
+ ("systemd_watchdog_seconds", "systemd_watchdog_seconds", "nested", None, None),
+ ("streaming", "streaming", "dict", None, None),
+ ("reset_triggers", "reset_triggers", "presence", None, None),
+ ("always_log_local", "always_log_local", "presence", None, None),
+ ("write_sessions_json", "write_sessions_json", "presence", None, None),
+ ("loop_watchdog", "loop_watchdog", "presence", None, None),
+ ("loop_watchdog_probe_interval_s", "loop_watchdog_probe_interval_s", "presence", None, None),
+ ("loop_watchdog_probe_timeout_s", "loop_watchdog_probe_timeout_s", "presence", None, None),
+ ("loop_watchdog_max_strikes", "loop_watchdog_max_strikes", "presence", None, None),
+ ("filter_silence_narration", "filter_silence_narration", "presence", None, None),
+ ("unauthorized_dm_behavior", "unauthorized_dm_behavior", "presence", None,
+ lambda v: _normalize_choice(v, {"pair", "ignore"}, "pair")),
+)
+
+
+def _bridge_lookup(yaml_cfg: dict, gateway_section: Any, gw_data: dict, key: str, mode: str) -> tuple:
+ """Return ``(found, value)`` for *key* under the given fallback *mode*."""
+ nested = isinstance(gateway_section, dict)
+ if mode in ("presence", "gwdata"):
+ if key in yaml_cfg:
+ return True, yaml_cfg[key]
+ if nested and key in gateway_section and (mode == "presence" or key not in gw_data):
+ return True, gateway_section[key]
+ return False, None
+ if mode == "nested":
+ if nested and key in gateway_section:
+ return True, gateway_section[key]
+ return False, None
+ value = yaml_cfg.get(key)
+ if mode == "none":
+ if value is None and nested:
+ value = gateway_section.get(key)
+ return value is not None, value
+ # "dict"
+ if not isinstance(value, dict) and nested:
+ value = gateway_section.get(key)
+ return isinstance(value, dict), value
+
+
+def bridge_toplevel_keys(yaml_cfg: dict, gateway_section: Any, gw_data: dict) -> None:
+ for yaml_key, gw_key, mode, accept, transform in _TOPLEVEL_BRIDGE:
+ found, value = _bridge_lookup(yaml_cfg, gateway_section, gw_data, yaml_key, mode)
+ if not found or (accept is not None and not accept(value)):
+ continue
+ gw_data[gw_key] = transform(value) if transform else value
+
+
+# --- platform sections -----------------------------------------------------------
+
+def merge_platform_sections(yaml_cfg: dict, gateway_cfg: Any, gw_data: dict) -> dict:
+ """Merge every place a platform block may live into ``gw_data["platforms"]`` and return it.
+
+ Order (later wins on shared keys, ``extra`` deep-merged so gateway.json defaults survive):
+ ``gateway.platforms.*`` → top-level ``platforms.*`` → ``gateway.<platform>`` subsections
+ (nested first so top-level config keeps precedence, matching the gateway.streaming fallback).
+ An ``enabled`` key in any block sets the ``_enabled_explicit`` marker consumed by the env pass.
+ Finally api_server's port/key/host/cors_origins/model_name are bridged into ``extra`` so
+ ``gateway.api_server.port: 8642`` reaches the adapter (mirrors the env path).
+ """
+ platforms_data = _dict_slot(gw_data, "platforms")
+
+ def merge(source_platforms: Any) -> None:
+ if not isinstance(source_platforms, dict):
+ return
+ for plat_name, plat_block in source_platforms.items():
+ if not isinstance(plat_block, dict):
+ continue
+ existing = platforms_data.get(plat_name, {})
+ if not isinstance(existing, dict):
+ existing = {}
+ merged_extra = {**existing.get("extra", {}), **plat_block.get("extra", {})}
+ if "enabled" in plat_block:
+ merged_extra["_enabled_explicit"] = True
+ merged = {**existing, **plat_block}
+ if merged_extra:
+ merged["extra"] = merged_extra
+ platforms_data[plat_name] = merged
+
+ gateway_platforms = gateway_cfg.get("platforms") if isinstance(gateway_cfg, dict) else None
+ merge(gateway_platforms)
+ merge(yaml_cfg.get("platforms"))
+ if isinstance(gateway_cfg, dict):
+ nested: dict = {}
+ for key, value in gateway_cfg.items():
+ if key == "platforms":
+ continue
+ try:
+ Platform(key)
+ except (ValueError, AttributeError):
+ continue
+ if isinstance(value, dict):
+ nested[key] = value
+ merge(nested)
+
+ api_plat = platforms_data.get("api_server")
+ if isinstance(api_plat, dict):
+ api_extra = _dict_slot(api_plat, "extra")
+ for key in ("port", "key", "host", "cors_origins", "model_name"):
+ if key in api_plat and key not in api_extra:
+ api_extra[key] = api_plat.pop(key)
+ return platforms_data
+
+
+def platform_section(yaml_cfg: dict, name: str, gateway_platforms: Any) -> tuple:
+ """Return ``(section, is_toplevel)`` for platform *name*.
+
+ A top-level ``<name>:`` block wins; otherwise fall back to the block under ``gateway.platforms``
+ / ``platforms`` so shared-key bridging and adapter hooks still run when the user configured the
+ platform only under those nested paths.
+ """
+ section = yaml_cfg.get(name)
+ toplevel = isinstance(section, dict)
+ if not toplevel:
+ for src in (gateway_platforms, yaml_cfg.get("platforms")):
+ if isinstance(src, dict) and isinstance(src.get(name), dict):
+ section = src[name]
+ break
+ return section, toplevel
+
+
+def _str_keyed(value: Any) -> Any:
+ return {str(k): v for k, v in value.items()} if isinstance(value, dict) else value
+
+
+def _dm_behavior(gw_data: dict):
+ return lambda v: _normalize_choice(v, {"pair", "ignore"}, gw_data.get("unauthorized_dm_behavior", "pair"))
+
+
+_TELEGRAM = frozenset({Platform.TELEGRAM})
+_DISCORD_SLACK = frozenset({Platform.DISCORD, Platform.SLACK})
+
+# Keys copied from a platform's YAML section into ``extra`` (``None`` = every platform);
+# third element transforms the value.
+_SHARED_KEYS: tuple = (
+ ("unauthorized_dm_behavior", None, "dm"),
+ ("notice_delivery", None, lambda v: _normalize_choice(v, {"public", "private"}, "public")),
+ ("reply_prefix", None, None),
+ ("reply_in_thread", None, None),
+ ("cron_continuable_surface", None, None),
+ ("require_mention", None, None),
+ ("send_read_receipts", None, None),
+ ("allowed_chats", _TELEGRAM, None),
+ ("group_allowed_chats", _TELEGRAM, None),
+ ("allowed_topics", _TELEGRAM, None),
+ ("free_response_channels", None, None),
+ ("mention_patterns", None, None),
+ ("exclusive_bot_mentions", None, None),
+ ("observe_unmentioned_group_messages", _TELEGRAM, None),
+ ("dm_policy", None, None),
+ ("allow_from", None, None),
+ ("allow_admin_from", None, None),
+ ("user_allowed_commands", None, None),
+ ("group_policy", None, None),
+ ("group_allow_from", None, None),
+ ("group_allow_admin_from", None, None),
+ ("group_user_allowed_commands", None, None),
+ ("channel_skill_bindings", _DISCORD_SLACK, None),
+ ("channel_prompts", None, _str_keyed),
+ ("gateway_restart_notification", None, None),
+ ("typing_indicator", None, None),
+ ("typing_status_text", None, None),
+)
+
+# Top-level port/host/secret bridged into ``extra`` for adapters that read them from config.extra.
+# Without this ``platforms.webhook.port: 8649`` silently falls back to the hardcoded DEFAULT_PORT,
+# because PlatformConfig.from_dict only extracts ``extra`` from the ``extra:`` sub-key.
+_PORT_BRIDGE_KEYS: dict = {
+ Platform.WEBHOOK: ("port", "host", "secret"),
+ Platform.MSGRAPH_WEBHOOK: ("port", "host", "secret"),
+ Platform.API_SERVER: ("port", "host"),
+}
+
+
+def _bridged_keys(plat: Platform, platform_cfg: dict, gw_data: dict) -> dict:
+ bridged: dict = {}
+ for key, only, transform in _SHARED_KEYS:
+ if key not in platform_cfg or (only is not None and plat not in only):
+ continue
+ if transform == "dm":
+ transform = _dm_behavior(gw_data)
+ bridged[key] = transform(platform_cfg[key]) if transform else platform_cfg[key]
+ for key in _PORT_BRIDGE_KEYS.get(plat, ()):
+ if key in platform_cfg and key not in platform_cfg.get("extra", {}):
+ bridged[key] = platform_cfg[key]
+ return bridged
+
+
+def shared_loop_targets(registry) -> list:
+ """Built-in platforms plus registered plugin platforms (so plugin authors get shared-key bridging)."""
+ targets: list = list(Platform)
+ if registry is not None:
+ for entry in registry.plugin_entries():
+ try:
+ plat = Platform(entry.name)
+ except (ValueError, KeyError):
+ continue
+ if plat not in targets:
+ targets.append(plat)
+ return targets
+
+
+def bridge_platform_shared_keys(
+ yaml_cfg: dict, gateway_platforms: Any, gw_data: dict, platforms_data: dict, targets: list
+) -> None:
+ """Copy shared keys (allow_from, require_mention, …) from each platform's YAML section into ``extra``.
+
+ ``enabled`` is only written from a TOP-LEVEL block: for nested-only configs
+ ``merge_platform_sections`` already merged it with the correct precedence, and re-applying it here
+ would overwrite that. An explicit top-level enable/disable sets ``_enabled_explicit`` so the env
+ pass honors ``enabled: false`` for migrated plugin platforms instead of re-enabling them on
+ token/SDK presence (#41112).
+ """
+ for plat in targets:
+ if plat == Platform.LOCAL:
+ continue
+ platform_cfg, cfg_toplevel = platform_section(yaml_cfg, plat.value, gateway_platforms)
+ if not isinstance(platform_cfg, dict):
+ continue
+ bridged = _bridged_keys(plat, platform_cfg, gw_data)
+ has_channel_overrides = "channel_overrides" in platform_cfg
+ if has_channel_overrides and isinstance(platform_cfg.get("channel_overrides"), dict):
+ plat_data = _dict_slot(platforms_data, plat.value)
+ _dict_slot(plat_data, "extra")
+ plat_data["channel_overrides"] = {
+ str(cid): ov_data
+ for cid, ov_data in platform_cfg["channel_overrides"].items()
+ if isinstance(ov_data, dict)
+ }
+ enabled_was_explicit = cfg_toplevel and "enabled" in platform_cfg
+ if not bridged and not enabled_was_explicit and not has_channel_overrides:
+ continue
+ plat_data = _dict_slot(platforms_data, plat.value)
+ extra = _dict_slot(plat_data, "extra")
+ if enabled_was_explicit:
+ plat_data["enabled"] = platform_cfg["enabled"]
+ extra["_enabled_explicit"] = True
+ extra.update(bridged)
+
+
+def apply_plugin_yaml_hooks(yaml_cfg: dict, gateway_platforms: Any, platforms_data: dict, registry) -> None:
+ """Plugin-owned YAML→env config bridges (``PlatformEntry.apply_yaml_config_fn``, #24836).
+
+ Order: shared-key loop → this dispatch → core-only bridges (require_mention/signal) →
+ ``_apply_env_overrides()`` after ``GatewayConfig.from_dict``.
+ """
+ if registry is None:
+ return
+ for entry in registry.all_entries():
+ if entry.apply_yaml_config_fn is None:
+ continue
+ platform_cfg, _ = platform_section(yaml_cfg, entry.name, gateway_platforms)
+ if not isinstance(platform_cfg, dict):
+ continue
+ try:
+ seeded = entry.apply_yaml_config_fn(yaml_cfg, platform_cfg)
+ except Exception as e:
+ logger.debug("apply_yaml_config_fn for %s raised: %s", entry.name, e)
+ continue
+ if not isinstance(seeded, dict) or not seeded:
+ continue
+ _dict_slot(_dict_slot(platforms_data, entry.name), "extra").update(seeded)
+
+
+def bridge_core_env_settings(yaml_cfg: dict, platforms_data: dict) -> None:
+ """The two YAML→env bridges that stay in core.
+
+ Slack/Telegram/WhatsApp/DingTalk/Mattermost/Matrix/Feishu settings→env bridges migrated to
+ each plugin's ``apply_yaml_config_fn`` hook (#41112 / #3823 / #25443).
+
+ Top-level ``require_mention`` → Telegram when the ``telegram:`` section has none: users write it
+ alongside ``group_sessions_per_user`` expecting it to work (#3979). It keys off the TOP-LEVEL key,
+ so the telegram plugin's hook (which only runs when a telegram block exists) can't cover it.
+ Signal ``require_mention`` → ``SIGNAL_REQUIRE_MENTION`` (env wins when already set).
+ """
+ tl_require_mention = yaml_cfg.get("require_mention")
+ if tl_require_mention is not None and "require_mention" not in (yaml_cfg.get("telegram") or {}):
+ tg_plat = platforms_data.setdefault(Platform.TELEGRAM.value, {})
+ tg_plat.setdefault("extra", {}).setdefault("require_mention", tl_require_mention)
+ if not os.getenv("TELEGRAM_REQUIRE_MENTION"):
+ os.environ["TELEGRAM_REQUIRE_MENTION"] = str(tl_require_mention).lower()
+
+ signal_cfg = yaml_cfg.get("signal", {})
+ if isinstance(signal_cfg, dict) and "require_mention" in signal_cfg and not os.getenv("SIGNAL_REQUIRE_MENTION"):
+ os.environ["SIGNAL_REQUIRE_MENTION"] = str(signal_cfg["require_mention"]).lower()
+
+
+def load_yaml_layer(home: Path, gw_data: dict) -> None:
+ """Overlay ``config.yaml`` onto *gw_data* in place. Raises on any failure (caller warns + falls back)."""
+ import yaml
+
+ config_yaml_path = home / "config.yaml"
+ if not config_yaml_path.exists():
+ return
+ with open(config_yaml_path, encoding="utf-8") as f:
+ yaml_cfg = yaml.safe_load(f) or {}
+
+ # Managed scope: overlay administrator-pinned values so the gateway honors them too. This
+ # loader builds its own dict instead of going through hermes_cli.config.load_config, so
+ # without this a managed session_reset / quick_commands / stt would be ignored. Fail-open.
+ from hermes_cli import managed_scope
+ yaml_cfg = managed_scope.apply_managed_overlay(yaml_cfg)
+
+ gateway_section = yaml_cfg.get("gateway")
+ bridge_toplevel_keys(yaml_cfg, gateway_section, gw_data)
+ gateway_platforms = gateway_section.get("platforms") if isinstance(gateway_section, dict) else None
+ platforms_data = merge_platform_sections(yaml_cfg, gateway_section, gw_data)
+
+ try:
+ from hermes_cli.plugins import discover_plugins
+ discover_plugins() # idempotent
+ from gateway.platform_registry import platform_registry as registry
+ except Exception as e:
+ logger.debug("plugin discovery skipped: %s", e)
+ registry = None
+
+ targets = shared_loop_targets(registry)
+ bridge_platform_shared_keys(yaml_cfg, gateway_platforms, gw_data, platforms_data, targets)
+ apply_plugin_yaml_hooks(yaml_cfg, gateway_platforms, platforms_data, registry)
+ bridge_core_env_settings(yaml_cfg, platforms_data)
diff --git a/tests/hermes_cli/test_config_read_guard.py b/tests/hermes_cli/test_config_read_guard.py
index d54ea08e4e5..d47ca1068ec 100644
--- a/tests/hermes_cli/test_config_read_guard.py
+++ b/tests/hermes_cli/test_config_read_guard.py
@@ -36,6 +36,9 @@ ALLOWLIST = {
# Canonical loader owners.
"hermes_cli/config.py",
"gateway/config.py",
+ # load_gateway_config()'s config.yaml phase lives here (extracted from
+ # gateway/config.py); same owner, same managed-overlay contract.
+ "gateway/config_loader.py",
# _load_gateway_config()'s fallback path for tests that monkeypatch
# gateway.run._hermes_home (delegates to read_raw_config otherwise).
"gateway/run.py",