Skip to content

The agent that grows with you

fix: apply managed gateway config without user YAML

Victor Kyriazakos6ce2b72281c8
diff --git a/gateway/config_loader.py b/gateway/config_loader.py
index e076336ba79..a5f147b9f0d 100644
--- a/gateway/config_loader.py
+++ b/gateway/config_loader.py
@@ -344,19 +344,22 @@ def bridge_core_env_settings(yaml_cfg: dict, platforms_data: dict) -> None:
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 (this loader bypasses
- # hermes_cli.config.load_config, so managed quick_commands / stt would otherwise be ignored).
+ """Overlay raw user + managed YAML onto *gw_data* without introducing CLI defaults."""
+ from hermes_cli.config import read_user_config_raw
from hermes_cli import managed_scope
+
+ # Match the raw runtime/predicate readers: absent, unreadable or malformed
+ # user YAML is an empty layer, not a reason to skip administrator settings.
+ # Use the explicit home rather than a process-global path (multiplex-safe).
+ try:
+ yaml_cfg = read_user_config_raw(home / "config.yaml")
+ except Exception as e:
+ logger.warning("Failed to read %s; applying managed config over an empty user layer: %s",
+ home / "config.yaml", e)
+ yaml_cfg = {}
yaml_cfg = managed_scope.apply_managed_overlay(yaml_cfg)
+ if not yaml_cfg:
+ return
gateway_section = yaml_cfg.get("gateway")
bridge_toplevel_keys(yaml_cfg, gateway_section, gw_data)
diff --git a/tests/gateway/relay/test_cold_opt_out.py b/tests/gateway/relay/test_cold_opt_out.py
index fcf7ac768ba..b113739fa57 100644
--- a/tests/gateway/relay/test_cold_opt_out.py
+++ b/tests/gateway/relay/test_cold_opt_out.py
@@ -10,7 +10,7 @@ import pytest
import yaml
[email protected]("case", ["native", "url-only", "disabled", "managed", "scoped", "scoped-json"])
[email protected]("case", ["native", "url-only", "disabled", "managed", "managed-only", "scoped", "scoped-json"])
def test_cold_fronted_platforms_is_read_only(tmp_path, case):
home = tmp_path / "primary"
home.mkdir()
@@ -21,9 +21,9 @@ def test_cold_fronted_platforms_is_read_only(tmp_path, case):
config = {"platforms": {"relay": {"enabled": True}}}
if case == "disabled":
config["platforms"]["relay"]["enabled"] = False
- if case not in {"native", "url-only"}:
+ if case not in {"native", "url-only", "managed-only"}:
(home / "config.yaml").write_text(yaml.safe_dump(config))
- if case == "managed":
+ if case in {"managed", "managed-only"}:
(managed / "config.yaml").write_text("platforms:\n relay:\n enabled: false\n")
if case == "scoped":
(scoped / "config.yaml").write_text("platforms:\n relay:\n enabled: false\n")
diff --git a/tests/gateway/relay/test_explicit_disable.py b/tests/gateway/relay/test_explicit_disable.py
index ce366e36469..55019dc0580 100644
--- a/tests/gateway/relay/test_explicit_disable.py
+++ b/tests/gateway/relay/test_explicit_disable.py
@@ -51,6 +51,20 @@ def configure(home, state, source, monkeypatch):
"slack": {"require_mention": False}}
if state is not None:
cfg["platforms"]["relay"] = {"enabled": state, "extra": {"relay_url": URL}}
+ if source == "managed-only":
+ # No user YAML: the managed overlay must still own relay intent before
+ # the injected URL can exclusively suppress this legacy native Slack.
+ (home / "gateway.json").write_text(json.dumps({"platforms": {"slack": cfg["platforms"]["slack"]}}))
+ managed = home / "managed"
+ managed.mkdir()
+ managed_cfg: dict = {"gateway": {"relay_url": URL}, "slack": {"require_mention": False}}
+ if state is not None:
+ managed_cfg["platforms"] = {"relay": {"enabled": state}}
+ (managed / "config.yaml").write_text(yaml.safe_dump(managed_cfg))
+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(managed))
+ monkeypatch.setenv("GATEWAY_RELAY_URL", URL)
+ assert not (home / "config.yaml").exists()
+ return
if source == "legacy-json":
(home / "gateway.json").write_text(json.dumps({"platforms": cfg.pop("platforms")}))
monkeypatch.setenv("GATEWAY_RELAY_URL", URL)
@@ -75,11 +89,11 @@ def descriptor():
@pytest.mark.asyncio
@pytest.mark.parametrize("state", [False, True, None], ids=["disabled", "enabled", "legacy"])
[email protected]("source", ["yaml", "env", "managed"])
[email protected]("source", ["yaml", "env", "managed", "managed-only"])
@pytest.mark.parametrize("pinned", [False, True], ids=["unprovisioned", "inherited-secret"])
async def test_production_startup_respects_profile_opt_out(profile, monkeypatch, state, source, pinned):
configure(profile, state, source, monkeypatch)
- if state is True:
+ if state is True and source != "managed-only":
# The winning top-level enable must override a lower-priority disable.
path = profile / "config.yaml"
cfg = yaml.safe_load(path.read_text())
@@ -172,12 +186,12 @@ async def test_production_startup_respects_profile_opt_out(profile, monkeypatch,
assert token.call_count == provision.call_count
policy.assert_called_once()
# Existing env-exclusive behavior is unchanged. YAML activation is additive.
- assert native.connect.await_count == (0 if source == "env" else 1)
+ assert native.connect.await_count == (0 if source in {"env", "managed-only"} else 1)
# The env path already auto-enables relay in the platform loader.
- if source == "env" or state is True:
+ if source in {"env", "managed-only"} or state is True:
relay_connect.assert_awaited_once()
assert Platform.RELAY in runner.adapters
- if source == "env":
+ if source in {"env", "managed-only"}:
from cron.scheduler_delivery import _resolve_target_transport
resolved, error = _resolve_target_transport(
@@ -221,6 +235,59 @@ def test_legacy_json_disable_agrees_with_normalized_config(profile, monkeypatch,
assert config.platforms[Platform.TELEGRAM].enabled
[email protected]("user_yaml", ["no-files", "absent", "empty", "null", "list", "malformed", "unreadable", "sibling", "enabled", "disabled"])
[email protected]("managed_state", ["absent", "url-only", "enabled", "disabled"])
+def test_managed_layer_agrees_with_native_config(profile, monkeypatch, user_yaml, managed_state):
+ """Managed leaves override user leaves, not CLI defaults or a second merge policy."""
+ path = profile / "config.yaml"
+ user_docs = {
+ "empty": "", "null": "null\n", "list": "- not-a-config\n",
+ "malformed": "platforms: [\n",
+ "sibling": "platforms:\n relay:\n extra:\n user_sibling: kept\n",
+ "enabled": "platforms:\n relay:\n enabled: true\n",
+ "disabled": "platforms:\n relay:\n enabled: false\n",
+ }
+ if user_yaml == "unreadable":
+ path.mkdir() # A directory fails open even when tests run as root.
+ elif user_yaml not in {"absent", "no-files"}:
+ path.write_text(user_docs[user_yaml])
+ managed = profile / "managed"
+ managed.mkdir()
+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(managed))
+ if managed_state != "absent":
+ block: dict = {"extra": {"relay_url": "${GATEWAY_RELAY_URL}", "managed_sibling": "pinned"}}
+ if managed_state != "url-only":
+ block["enabled"] = managed_state == "enabled"
+ (managed / "config.yaml").write_text(yaml.safe_dump({
+ "platforms": {"relay": block},
+ "gateway": {"max_concurrent_sessions": 7},
+ }))
+ if user_yaml == "no-files":
+ monkeypatch.setenv("SLACK_BOT_TOKEN", "native-test-token")
+ else:
+ (profile / "gateway.json").write_text(json.dumps({"platforms": {
+ "slack": {"enabled": True, "token": "native-test-token"},
+ "telegram": {"enabled": False},
+ }}))
+ monkeypatch.setenv("GATEWAY_RELAY_URL", URL)
+ monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "native-telegram-test-token")
+ # An absent managed enabled leaf must not introduce DEFAULT_CONFIG's false.
+ disabled = managed_state == "disabled" or (
+ managed_state in {"absent", "url-only"} and user_yaml == "disabled"
+ )
+ config = load_gateway_config()
+ assert relay.relay_explicitly_disabled() is disabled
+ assert config.platforms[Platform.RELAY].enabled is not disabled
+ assert config.platforms[Platform.SLACK].enabled is disabled
+ assert config.platforms[Platform.TELEGRAM].enabled is disabled
+ if managed_state != "absent":
+ assert config.max_concurrent_sessions == 7
+ assert config.platforms[Platform.RELAY].extra["managed_sibling"] == "pinned"
+ assert config.platforms[Platform.RELAY].extra["relay_url"] == URL
+ if user_yaml == "sibling":
+ assert config.platforms[Platform.RELAY].extra["user_sibling"] == "kept"
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("entry", ["url", "provision", "policy", "registration", "forced-registration",
"fronted", "adapter-connect", "adapter-reconnect",