config_loader.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. """config.yaml / gateway.json → ``GatewayConfig.from_dict`` schema (the ``load_gateway_config`` phases).
  2. Precedence for top-level keys: key-presence at the TOP LEVEL of config.yaml wins; the nested
  3. ``gateway.<key>`` form (what ``hermes config set gateway.<key>`` produces) is consulted only when the
  4. top-level key is absent — not merely falsy/mistyped — so a present-but-empty top-level value is never
  5. silently replaced by the nested one. Both overwrite whatever legacy gateway.json set.
  6. """
  7. import contextlib
  8. import json
  9. import logging
  10. import os
  11. from pathlib import Path
  12. from typing import Any, Optional
  13. from gateway.config import UNAUTHORIZED_DM_BEHAVIORS, Platform, PlatformConfig, _coerce_dict, _dict_slot, _normalize_choice
  14. # Logger name parity with the origin module: records stay under "gateway.config".
  15. logger = logging.getLogger("gateway.config")
  16. def load_legacy_gateway_json(home: Path) -> Any:
  17. """Legacy ``gateway.json`` base layer (config.yaml keys always win). Malformed → ``{}`` + warning."""
  18. path = home / "gateway.json"
  19. if not path.exists():
  20. return {}
  21. try:
  22. with open(path, "r", encoding="utf-8-sig") as f:
  23. data = json.load(f) or {}
  24. logger.info("Loaded legacy %s — consider moving settings to config.yaml", path)
  25. return data
  26. except Exception as e:
  27. logger.warning("Failed to load %s: %s", path, e)
  28. return {}
  29. # --- top-level key bridging ----------------------------------------------------
  30. #
  31. # Top-level settings are also accepted nested under ``gateway:`` (what ``hermes config set
  32. # gateway.<key>`` produces). This loader builds gw_data FLAT and never forwards the yaml ``gateway:``
  33. # section, so even keys GatewayConfig.from_dict can fall back on itself (loop_watchdog*,
  34. # multiplex_profiles, ...) must be bridged here or they are silently ignored on real startup.
  35. #
  36. # Fallback modes (how the nested ``gateway.<key>`` form is consulted):
  37. # "presence": top-level key present → its value; else nested key present.
  38. # "gwdata": like "presence", but nested only when NOTHING (config.yaml or gateway.json) set it yet.
  39. # "none": top-level VALUE is None → nested value.
  40. # "dict": top-level value is not a mapping → nested value; accepted only if a mapping.
  41. # "nested": nested form only (no top-level spelling is bridged).
  42. # True while GATEWAY_ALLOW_ALL_USERS in os.environ is the bridge's own write (from config.yaml), not an
  43. # operator's env var: only then may a reload overwrite/clear it, and restart env builders drop it so a
  44. # child gateway re-derives the grant from its config.yaml instead of inheriting a stale open posture.
  45. _BRIDGED_ALLOW_ALL_USERS = False
  46. def bridged_allow_all_users() -> Optional[str]:
  47. """``os.environ['GATEWAY_ALLOW_ALL_USERS']`` when it is the bridge's own write, else None."""
  48. return os.environ.get("GATEWAY_ALLOW_ALL_USERS") if _BRIDGED_ALLOW_ALL_USERS else None
  49. def drop_bridged_env(env: dict) -> dict:
  50. """Remove the bridge-owned ``GATEWAY_ALLOW_ALL_USERS`` from a child-process env (operator-set stays)."""
  51. if bridged_allow_all_users() is not None:
  52. env.pop("GATEWAY_ALLOW_ALL_USERS", None)
  53. return env
  54. def _quick_commands_ok(value: Any) -> bool:
  55. if isinstance(value, dict):
  56. return True
  57. logger.warning(
  58. "Ignoring invalid quick_commands in config.yaml (expected mapping, got %s)",
  59. type(value).__name__,
  60. )
  61. return False
  62. def _dm_behavior_choice(value: Any, default: str = "pair") -> str:
  63. return _normalize_choice(value, UNAUTHORIZED_DM_BEHAVIORS, default)
  64. def _presence(*keys: str) -> tuple:
  65. return tuple((k, k, "presence", None, None) for k in keys)
  66. # (yaml key, gw_data key, mode, accept(value) -> bool, transform(value))
  67. _TOPLEVEL_BRIDGE: tuple = (
  68. ("quick_commands", "quick_commands", "none", _quick_commands_ok, None),
  69. ("stt", "stt", "presence", lambda v: isinstance(v, dict), None),
  70. *_presence("stt_echo_transcripts", "group_sessions_per_user", "thread_sessions_per_user"),
  71. ("multiplex_profiles", "multiplex_profiles", "gwdata", None, None),
  72. *_presence("room_link_url"),
  73. ("profile_routes", "profile_routes", "none", lambda v: isinstance(v, list), None),
  74. *_presence("max_concurrent_sessions"),
  75. ("systemd_watchdog_seconds", "systemd_watchdog_seconds", "nested", None, None),
  76. ("streaming", "streaming", "dict", None, None),
  77. *_presence(
  78. "reset_triggers", "always_log_local", "write_sessions_json", "loop_watchdog",
  79. "loop_watchdog_probe_interval_s", "loop_watchdog_probe_timeout_s", "loop_watchdog_max_strikes",
  80. "filter_silence_narration",
  81. ),
  82. ("unauthorized_dm_behavior", "unauthorized_dm_behavior", "presence", None, _dm_behavior_choice),
  83. *_presence("unauthorized_dm_decline_message"),
  84. )
  85. def _bridge_lookup(yaml_cfg: dict, gateway_section: Any, gw_data: dict, key: str, mode: str) -> tuple:
  86. """Return ``(found, value)`` for *key* under the given fallback *mode*."""
  87. nested = isinstance(gateway_section, dict)
  88. if mode in ("presence", "gwdata"):
  89. if key in yaml_cfg:
  90. return True, yaml_cfg[key]
  91. if nested and key in gateway_section and (mode == "presence" or key not in gw_data):
  92. return True, gateway_section[key]
  93. return False, None
  94. if mode == "nested":
  95. return (True, gateway_section[key]) if nested and key in gateway_section else (False, None)
  96. value = yaml_cfg.get(key)
  97. if mode == "none":
  98. if value is None and nested:
  99. value = gateway_section.get(key)
  100. return value is not None, value
  101. # "dict"
  102. if not isinstance(value, dict) and nested:
  103. value = gateway_section.get(key)
  104. return isinstance(value, dict), value
  105. def bridge_toplevel_keys(yaml_cfg: dict, gateway_section: Any, gw_data: dict) -> None:
  106. for yaml_key, gw_key, mode, accept, transform in _TOPLEVEL_BRIDGE:
  107. found, value = _bridge_lookup(yaml_cfg, gateway_section, gw_data, yaml_key, mode)
  108. if not found or (accept is not None and not accept(value)):
  109. continue
  110. gw_data[gw_key] = transform(value) if transform else value
  111. # --- platform sections -----------------------------------------------------------
  112. def merge_platform_sections(yaml_cfg: dict, gateway_cfg: Any, gw_data: dict, *, also: frozenset = frozenset()) -> dict:
  113. """Merge every place a platform block may live into ``gw_data["platforms"]`` and return it.
  114. Order (later wins on shared keys, ``extra`` deep-merged so gateway.json defaults survive):
  115. ``gateway.platforms.*`` → top-level ``platforms.*`` → ``gateway.<platform>`` subsections (nested
  116. first so top-level config keeps precedence, matching the gateway.streaming fallback). An
  117. ``enabled`` key in any block sets the ``_enabled_explicit`` marker consumed by the env pass.
  118. Top-level adapter keys (``gateway.api_server.port: 8642``) reach ``extra`` in
  119. ``PlatformConfig.from_dict``. *also* names platforms whose ``gateway.<platform>`` shorthand counts
  120. even while no adapter is registered (a platform that left core, read before its plugin loads).
  121. """
  122. platforms_data = _dict_slot(gw_data, "platforms")
  123. def merge(source_platforms: Any) -> None:
  124. if not isinstance(source_platforms, dict):
  125. return
  126. for plat_name, plat_block in source_platforms.items():
  127. if not isinstance(plat_block, dict):
  128. continue
  129. existing = platforms_data.get(plat_name, {})
  130. existing = existing if isinstance(existing, dict) else {}
  131. merged_extra = {**existing.get("extra", {}), **plat_block.get("extra", {})}
  132. if "enabled" in plat_block:
  133. merged_extra["_enabled_explicit"] = True
  134. merged = {**existing, **plat_block}
  135. if merged_extra:
  136. merged["extra"] = merged_extra
  137. platforms_data[plat_name] = merged
  138. nested_gateway = gateway_cfg if isinstance(gateway_cfg, dict) else {}
  139. merge(nested_gateway.get("platforms"))
  140. merge(yaml_cfg.get("platforms"))
  141. merge({k: v for k, v in nested_gateway.items()
  142. if k != "platforms" and isinstance(v, dict) and (k in also or _is_platform_name(k))})
  143. return platforms_data
  144. def _is_platform_name(key: Any) -> bool:
  145. try:
  146. Platform(key)
  147. except (ValueError, AttributeError):
  148. return False
  149. return True
  150. def platform_section(yaml_cfg: dict, name: str, gateway_platforms: Any) -> tuple:
  151. """``(section, is_toplevel)`` for platform *name*: a top-level ``<name>:`` block wins; otherwise
  152. the block under ``gateway.platforms`` / ``platforms`` so shared-key bridging and adapter hooks
  153. still run for nested-only configs."""
  154. section = yaml_cfg.get(name)
  155. toplevel = isinstance(section, dict)
  156. if not toplevel:
  157. nested = (src[name] for src in (gateway_platforms, yaml_cfg.get("platforms")) if isinstance(src, dict) and isinstance(src.get(name), dict))
  158. section = next(nested, section)
  159. return section, toplevel
  160. def _str_keyed(value: Any) -> Any:
  161. return {str(k): v for k, v in value.items()} if isinstance(value, dict) else value
  162. _TELEGRAM = frozenset({Platform.TELEGRAM})
  163. _DISCORD_SLACK = frozenset({Platform.DISCORD, Platform.SLACK})
  164. def _plain(*keys: str) -> tuple:
  165. """Keys copied verbatim into ``extra`` for every platform."""
  166. return tuple((k, None, None) for k in keys)
  167. # (key, platforms-or-None, transform-or-None), in ``extra`` insertion order.
  168. # ``"dm"`` defers to the global unauthorized_dm_behavior.
  169. _SHARED_KEYS: tuple = (
  170. ("unauthorized_dm_behavior", None, "dm"),
  171. ("notice_delivery", None, lambda v: _normalize_choice(v, {"public", "private"}, "public")),
  172. *_plain("reply_prefix", "reply_in_thread", "cron_continuable_surface", "require_mention", "send_read_receipts"),
  173. ("allowed_chats", _TELEGRAM, None),
  174. ("group_allowed_chats", _TELEGRAM, None),
  175. ("allowed_topics", _TELEGRAM, None),
  176. *_plain("free_response_channels", "mention_patterns", "exclusive_bot_mentions"),
  177. ("observe_unmentioned_group_messages", _TELEGRAM, None),
  178. *_plain(
  179. "dm_policy", "allow_from", "allow_admin_from", "user_allowed_commands",
  180. "group_policy", "group_allow_from", "group_allow_admin_from", "group_user_allowed_commands",
  181. ),
  182. ("channel_skill_bindings", _DISCORD_SLACK, None),
  183. ("channel_prompts", None, _str_keyed),
  184. *_plain("gateway_restart_notification", "typing_indicator", "typing_status_text"),
  185. )
  186. def _bridged_keys(plat: Platform, platform_cfg: dict, gw_data: dict, *, root_block: bool = False) -> dict:
  187. """Shared-key bridge; a ROOT-level ``<platform>:`` block (which ``merge_platform_sections``
  188. never copies into ``platforms_data``) also gets its adapter keys promoted into ``extra``, with
  189. the same typed-key exclusion and explicit-``extra`` precedence as ``PlatformConfig.from_dict``."""
  190. bridged: dict = {}
  191. if root_block:
  192. typed = PlatformConfig._TYPED_KEYS | {"channel_overrides"}
  193. bridged.update({k: v for k, v in platform_cfg.items() if k not in typed})
  194. bridged.update(_coerce_dict(platform_cfg.get("extra", {})))
  195. for key, only, transform in _SHARED_KEYS:
  196. if key not in platform_cfg or (only is not None and plat not in only):
  197. continue
  198. if transform == "dm":
  199. bridged[key] = _dm_behavior_choice(platform_cfg[key], gw_data.get("unauthorized_dm_behavior", "pair"))
  200. else:
  201. bridged[key] = transform(platform_cfg[key]) if transform else platform_cfg[key]
  202. return bridged
  203. def shared_loop_targets(registry, entries=None) -> list:
  204. """Built-in platforms plus registered plugin platforms (so plugin authors get shared-key bridging).
  205. *entries* are the plugin entries to bridge (``load_yaml_layer`` passes only the configured
  206. ones, see ``gateway.platform_activation``); None means every registered plugin platform."""
  207. targets: list = list(Platform)
  208. if entries is None:
  209. entries = registry.plugin_entries() if registry is not None else ()
  210. for entry in entries:
  211. with contextlib.suppress(ValueError, KeyError):
  212. if (plat := Platform(entry.name)) not in targets:
  213. targets.append(plat)
  214. return targets
  215. def snapshot_authored_extra(platforms_data: dict) -> dict:
  216. """``{plat_name: extra}`` as the user authored it under ``platforms.<plat>.extra``, captured
  217. right after ``merge_platform_sections`` and BEFORE either copy site runs. Reading
  218. ``platforms_data`` live at a copy site would mistake values the earlier site bridged from the
  219. block's own ``extra:`` sub-dict for authored ones (``telegram: {reply_to_mode: all, extra:
  220. {reply_to_mode: off}}`` must still resolve to ``all``). Copies, never creates slots."""
  221. return {
  222. name: dict(_coerce_dict(plat.get("extra")))
  223. for name, plat in platforms_data.items() if isinstance(plat, dict)
  224. }
  225. def _apply_managed_extra(authored: dict, managed: dict, platforms_data: dict) -> dict:
  226. """Remove from *authored* (in place) every key the administrator pinned in a managed ``<plat>:``
  227. block: the user's ``platforms.<plat>.extra`` must not outrank it. A key the managed layer itself
  228. sets under ``platforms.<plat>.extra`` stays authored."""
  229. if not managed:
  230. return {}
  231. from hermes_cli.config import _deep_merge
  232. managed_platforms = merge_platform_sections(managed, managed.get("gateway"), {})
  233. hook_pins = {}
  234. for name, extra in authored.items():
  235. root = _coerce_dict(managed.get(name))
  236. nested = _coerce_dict(managed_platforms.get(name))
  237. pinned = set(root) | set(_coerce_dict(root.get("extra"))) | set(nested)
  238. for key in (pinned - set(_coerce_dict(nested.get("extra")))) & set(extra):
  239. del extra[key]
  240. # Cross-shape merges can replace managed gateway.platforms.extra with the
  241. # user platforms.extra. Restore values, not just their key membership.
  242. managed_extra = _coerce_dict(nested.get("extra"))
  243. extra.update(_deep_merge(extra, managed_extra))
  244. # Restore at the destination too: an unrelated root block need not mention
  245. # any pinned key, so the authored-conflict overlay alone cannot carry it.
  246. if managed_extra:
  247. destination = _dict_slot(_dict_slot(platforms_data, name), "extra")
  248. destination.update(_deep_merge(destination, managed_extra))
  249. hook_pins[name] = {key: extra[key] for key in managed_extra
  250. if key not in PlatformConfig._TYPED_KEYS}
  251. return hook_pins
  252. def _authored_wins(extra: dict, block: dict, plat_name: str, *, toplevel: bool, warned: Optional[set] = None) -> dict:
  253. """Effective ``<plat>:`` block for YAML→extra/env bridging: *block* (top-level or
  254. nested) overlaid with the keys the user authored in ``platforms.<plat>.extra``.
  255. Returns a NEW dict and never mutates *block* or *extra*. For every key present in
  256. both with DIFFERENT values, ONE warning logs that the authored extra took precedence
  257. (equal values are silent). *warned* dedupes across both copy sites within one
  258. ``load_yaml_layer`` pass; ``None`` gives the caller a fresh local set.
  259. """
  260. if warned is None:
  261. warned = set()
  262. where = f"the top-level '{plat_name}:' block" if toplevel else f"platforms.{plat_name}"
  263. def overlay_onto(source: dict) -> dict:
  264. overlay = {k: extra[k] for k in source if k in extra}
  265. for key, value in overlay.items():
  266. if source[key] != value and (plat_name, key) not in warned:
  267. warned.add((plat_name, key))
  268. logger.warning(
  269. "%s set in both %s and platforms.%s.extra; platforms.%s.extra took precedence",
  270. key, where, plat_name, plat_name,
  271. )
  272. return {**source, **overlay}
  273. effective = overlay_onto(block)
  274. # A root block may carry its own ``extra:`` sub-dict (``_bridged_keys`` merges it with
  275. # ``PlatformConfig.from_dict`` semantics); the authored extra outranks that too.
  276. if isinstance(block.get("extra"), dict):
  277. effective["extra"] = overlay_onto(block["extra"])
  278. # Hooks consume direct keys before global fallbacks. Expose the resolved
  279. # authored subdict values there too, without promoting typed platform fields.
  280. effective.update({k: extra[k] for k in block["extra"]
  281. if k in extra and k not in PlatformConfig._TYPED_KEYS})
  282. return effective
  283. def bridge_platform_shared_keys(
  284. yaml_cfg: dict, gateway_platforms: Any, gw_data: dict, platforms_data: dict, targets: list,
  285. warned: Optional[set] = None, authored: Optional[dict] = None,
  286. ) -> None:
  287. """Copy shared keys (allow_from, require_mention, …) from each platform's YAML section into ``extra``.
  288. ``enabled`` is only written from a TOP-LEVEL block: for nested-only configs ``merge_platform_sections``
  289. already merged it with the correct precedence. An explicit top-level enable/disable sets
  290. ``_enabled_explicit`` so the env pass honors ``enabled: false`` for migrated plugin platforms
  291. instead of re-enabling them on token/SDK presence.
  292. """
  293. if authored is None:
  294. authored = snapshot_authored_extra(platforms_data)
  295. for plat in targets:
  296. if plat == Platform.LOCAL:
  297. continue
  298. platform_cfg, cfg_toplevel = platform_section(yaml_cfg, plat.value, gateway_platforms)
  299. if not isinstance(platform_cfg, dict):
  300. continue
  301. # An authored ``platforms.<plat>.extra`` key beats the same key in the (top-level or
  302. # nested) block, so the shared-key bridge works on the effective block.
  303. effective = _authored_wins(
  304. authored.get(plat.value, {}), platform_cfg, plat.value, toplevel=cfg_toplevel, warned=warned)
  305. bridged = _bridged_keys(plat, effective, gw_data, root_block=cfg_toplevel)
  306. has_channel_overrides = "channel_overrides" in platform_cfg
  307. if has_channel_overrides and isinstance(platform_cfg.get("channel_overrides"), dict):
  308. plat_data = _dict_slot(platforms_data, plat.value)
  309. _dict_slot(plat_data, "extra")
  310. plat_data["channel_overrides"] = {
  311. str(cid): ov_data
  312. for cid, ov_data in platform_cfg["channel_overrides"].items()
  313. if isinstance(ov_data, dict)
  314. }
  315. enabled_was_explicit = cfg_toplevel and "enabled" in platform_cfg
  316. if not bridged and not enabled_was_explicit and not has_channel_overrides:
  317. continue
  318. plat_data = _dict_slot(platforms_data, plat.value)
  319. extra = _dict_slot(plat_data, "extra")
  320. if enabled_was_explicit:
  321. plat_data["enabled"] = platform_cfg["enabled"]
  322. extra["_enabled_explicit"] = True
  323. extra.update(bridged)
  324. def apply_plugin_yaml_hooks(
  325. yaml_cfg: dict, gateway_platforms: Any, platforms_data: dict, registry,
  326. warned: Optional[set] = None, authored: Optional[dict] = None, managed_extra: Optional[dict] = None,
  327. entries: Optional[list] = None,
  328. ) -> None:
  329. """Plugin-owned YAML→env config bridges (``PlatformEntry.apply_yaml_config_fn``). Order: shared-key
  330. loop → this dispatch → core-only bridges (require_mention/signal) → ``_apply_env_overrides()``.
  331. *entries* limits the dispatch to those platforms (None = every registered entry)."""
  332. if registry is None:
  333. return
  334. if authored is None:
  335. authored = snapshot_authored_extra(platforms_data)
  336. for entry in registry.all_entries() if entries is None else entries:
  337. # Plugin-owned YAML→env config bridges (#24836). See ``PlatformEntry.apply_yaml_config_fn`` for the
  338. # hook contract. Order: shared-key loop (above) → this dispatch → legacy hardcoded blocks (below;
  339. # no-op when a hook already set their env var) → ``_apply_env_overrides()`` after
  340. # ``GatewayConfig.from_dict``.
  341. if entry.apply_yaml_config_fn is None:
  342. continue
  343. platform_cfg, cfg_toplevel = platform_section(yaml_cfg, entry.name, gateway_platforms)
  344. if not isinstance(platform_cfg, dict):
  345. continue
  346. # Overlay the authored extra BEFORE the hook so the block the hook sees — and
  347. # therefore the values it bridges to env — already carry the authored nested
  348. # value for keys present in both (adapter readers are env-first).
  349. effective = _authored_wins(
  350. authored.get(entry.name, {}), platform_cfg, entry.name, toplevel=cfg_toplevel, warned=warned)
  351. # Restored managed pins must reach hooks before raw-YAML/global fallbacks
  352. # can seed a stale env value. Other authored-only keys keep existing behavior.
  353. effective.update((managed_extra or {}).get(entry.name, {}))
  354. try:
  355. seeded = entry.apply_yaml_config_fn(yaml_cfg, effective)
  356. except Exception as e:
  357. logger.debug("apply_yaml_config_fn for %s raised: %s", entry.name, e)
  358. continue
  359. if isinstance(seeded, dict) and seeded:
  360. # Safe: seeded values already equal the authored ones for keys present in both.
  361. _dict_slot(_dict_slot(platforms_data, entry.name), "extra").update(seeded)
  362. def bridge_core_env_settings(yaml_cfg: dict, platforms_data: dict) -> None:
  363. """The YAML→env bridges that stay in core (per-platform ones live in plugin hooks).
  364. Top-level ``require_mention`` → Telegram when the ``telegram:`` section has none: users write it
  365. alongside ``group_sessions_per_user`` expecting it to work, and the telegram plugin's hook only
  366. runs when a telegram block exists. Signal ``require_mention`` → ``SIGNAL_REQUIRE_MENTION`` (env wins).
  367. ``allow_all_users`` (top-level or ``gateway.allow_all_users``) → ``GATEWAY_ALLOW_ALL_USERS``: every
  368. allow-all reader (authz mixin, startup access check, own-policy adapters, plugin gates) consults
  369. that env var, so the bridge is the one seam that makes the YAML key reach all of them (#110690).
  370. Platform values are ALWAYS seeded into the owning platform's ``extra`` (the adapters read extra
  371. first); the process-env write is skipped while a multiplexed secondary profile's scope is active —
  372. the loader runs inside ``_profile_runtime_scope`` for every secondary, and a first-writer-wins write
  373. there would make the secondary's policy the DEFAULT profile's (#80099 class). A secondary profile
  374. sets ``GATEWAY_ALLOW_ALL_USERS`` in its own ``.env`` like every other scoped authorization gate.
  375. """
  376. global _BRIDGED_ALLOW_ALL_USERS
  377. from gateway.platforms._shared import profile_scoped
  378. skip_env_bridge = profile_scoped()
  379. gateway_section = yaml_cfg.get("gateway")
  380. allow_all = yaml_cfg.get("allow_all_users")
  381. if allow_all is None and isinstance(gateway_section, dict):
  382. allow_all = gateway_section.get("allow_all_users")
  383. # Only a value this bridge wrote may be overwritten/cleared by a later load (config flipped to
  384. # false + reload); an operator's explicit env var still wins. Only a truthy grant is exported:
  385. # presence-based readers treat any non-empty value as "auth configured".
  386. if not skip_env_bridge and (_BRIDGED_ALLOW_ALL_USERS or not os.getenv("GATEWAY_ALLOW_ALL_USERS")):
  387. _BRIDGED_ALLOW_ALL_USERS = str(allow_all).lower() in {"true", "1", "yes"}
  388. if _BRIDGED_ALLOW_ALL_USERS:
  389. os.environ["GATEWAY_ALLOW_ALL_USERS"] = "true"
  390. # The key was inert before it was bridged, so a forgotten line silently flips the
  391. # posture to open — name the grant source at startup.
  392. logger.warning(
  393. "config.yaml allow_all_users: true grants every sender on every platform access "
  394. "(bridged to GATEWAY_ALLOW_ALL_USERS; an explicit env var wins)."
  395. )
  396. else:
  397. os.environ.pop("GATEWAY_ALLOW_ALL_USERS", None)
  398. tl_require_mention = yaml_cfg.get("require_mention")
  399. if tl_require_mention is not None and "require_mention" not in (yaml_cfg.get("telegram") or {}):
  400. tg_plat = platforms_data.setdefault(Platform.TELEGRAM.value, {})
  401. tg_plat.setdefault("extra", {}).setdefault("require_mention", tl_require_mention)
  402. # Also bridge to the TELEGRAM_REQUIRE_MENTION env var that the adapter reads at runtime. This used
  403. # to live in the telegram_cfg block in core; it stays in core because it keys off the TOP-LEVEL
  404. # require_mention (not a telegram: block), so the telegram plugin's apply_yaml_config_fn hook —
  405. # which only runs when a telegram config block exists — can't cover the no-telegram-block case
  406. # (#3979).
  407. if not skip_env_bridge and not os.getenv("TELEGRAM_REQUIRE_MENTION"):
  408. os.environ["TELEGRAM_REQUIRE_MENTION"] = str(tl_require_mention).lower()
  409. # Telegram settings → env vars / extra: migrated to the telegram plugin's apply_yaml_config_fn hook
  410. # (plugins/platforms/telegram/adapter.py). #41112 / #3823.
  411. # WhatsApp settings → env vars: migrated to the whatsapp plugin's apply_yaml_config_fn hook
  412. # (plugins/platforms/whatsapp/adapter.py). #41112 / #3823.
  413. signal_cfg = yaml_cfg.get("signal", {})
  414. if isinstance(signal_cfg, dict) and "require_mention" in signal_cfg:
  415. sig_plat = platforms_data.setdefault(Platform.SIGNAL.value, {})
  416. sig_plat.setdefault("extra", {}).setdefault("require_mention", signal_cfg["require_mention"])
  417. if not skip_env_bridge and not os.getenv("SIGNAL_REQUIRE_MENTION"):
  418. os.environ["SIGNAL_REQUIRE_MENTION"] = str(signal_cfg["require_mention"]).lower()
  419. def read_yaml_layers(home: Path) -> dict:
  420. """User ``config.yaml`` with the managed overlay applied — the YAML the gateway loader sees.
  421. Raises on a malformed user file (the loader then falls back to env + gateway.json WITHOUT the
  422. managed layer). An ABSENT user file is an empty layer, not a reason to skip the administrator's
  423. values: a fleet host with no ``config.yaml`` must still honor them. Any pre-activation predicate
  424. (``gateway.relay.relay_explicitly_disabled``) reads through here so it cannot disagree with
  425. ``load_gateway_config()`` on which files count.
  426. """
  427. from hermes_cli.config_backend import config_exists, read_config_doc
  428. config_path = Path(home) / "config.yaml"
  429. yaml_cfg: dict = {}
  430. if config_exists(config_path):
  431. yaml_cfg = read_config_doc(config_path) or {}
  432. from hermes_cli.config import _expand_env_vars
  433. # ${VAR} / ${env:VAR} expansion — the same primitive the CLI loader applies, so platform
  434. # adapter settings (webhook secret, api_server key, teams credentials) arrive resolved
  435. # instead of as literal refs. User layer first: the managed overlay is expanded by
  436. # apply_managed_overlay itself and must not be re-resolved (config_effective._effective order).
  437. yaml_cfg = _expand_env_vars(yaml_cfg)
  438. # Managed scope: overlay administrator-pinned values (this loader bypasses
  439. # hermes_cli.config.load_config, so managed quick_commands / stt would otherwise be ignored).
  440. from hermes_cli import managed_scope
  441. return managed_scope.apply_managed_overlay(yaml_cfg)
  442. def load_yaml_layer(home: Path, gw_data: dict) -> None:
  443. """Overlay ``read_yaml_layers`` onto *gw_data* in place. Raises on any failure (caller warns + falls back)."""
  444. yaml_cfg = read_yaml_layers(home)
  445. if not yaml_cfg:
  446. return
  447. gateway_section = yaml_cfg.get("gateway")
  448. bridge_toplevel_keys(yaml_cfg, gateway_section, gw_data)
  449. gateway_platforms = gateway_section.get("platforms") if isinstance(gateway_section, dict) else None
  450. platforms_data = merge_platform_sections(yaml_cfg, gateway_section, gw_data)
  451. try:
  452. from hermes_cli.plugins import discover_plugins
  453. discover_plugins() # idempotent
  454. from gateway.platform_registry import platform_registry as registry
  455. except Exception as e:
  456. logger.debug("plugin discovery skipped: %s", e)
  457. registry = None
  458. # Only platforms this profile configures (a block above, a gateway.json row, their env/.env) are
  459. # materialized: importing every deferred adapter to read its hooks cost each cold start ~0.5 s.
  460. entries = None
  461. if registry is not None:
  462. from gateway.platform_activation import activation_predicate
  463. entries = registry.configured_entries(activation_predicate(
  464. yaml_cfg=yaml_cfg, platform_names=platforms_data, home=home, registry=registry))
  465. targets = shared_loop_targets(registry, None if entries is None else [e for e in entries if e.source == "plugin"])
  466. # "Authored" is config.yaml only: a scratch merge leaves out the legacy gateway.json extra
  467. # already in ``platforms_data``, which stays the base layer every config.yaml key overrides.
  468. authored = snapshot_authored_extra(merge_platform_sections(yaml_cfg, gateway_section, {}))
  469. from hermes_cli import managed_scope
  470. managed_extra = _apply_managed_extra(authored, managed_scope.apply_managed_overlay({}), platforms_data)
  471. warned: set = set() # one warning per conflicting (platform, key) across both copy sites
  472. bridge_platform_shared_keys(
  473. yaml_cfg, gateway_platforms, gw_data, platforms_data, targets, warned=warned, authored=authored)
  474. apply_plugin_yaml_hooks(yaml_cfg, gateway_platforms, platforms_data, registry,
  475. warned=warned, authored=authored, managed_extra=managed_extra, entries=entries)
  476. bridge_core_env_settings(yaml_cfg, platforms_data)
  477. def load_gateway_config_file(gateway_config_file: str):
  478. """``gateway --config <file>``: a ``GatewayConfig`` from an explicit local YAML file. Refused
  479. when the config backend has no file tooling (Remote Config: config comes only from the plane)."""
  480. import hermes_yaml as yaml
  481. from gateway.config import GatewayConfig
  482. from hermes_cli.config_backend import require_file_tooling
  483. require_file_tooling("gateway --config <file>")
  484. with open(gateway_config_file, encoding="utf-8-sig") as f:
  485. return GatewayConfig.from_dict(yaml.safe_load(f) or {})