Skip to content

The agent that grows with you

fix(cron): read managed-scope gateway.profile_routes in preflight (#121212)

ahisblessed40715d95a073
diff --git a/cron/scheduler_preflight.py b/cron/scheduler_preflight.py
index b77c8fd3e83..4ee5b31b65e 100644
--- a/cron/scheduler_preflight.py
+++ b/cron/scheduler_preflight.py
@@ -135,6 +135,25 @@ def _credential_store_scope_label() -> str:
return f"[profile '{get_active_profile_name() or 'default'}', HERMES_HOME {get_hermes_home()}]"
+def _profile_routes_from_raw_config(raw) -> object:
+ """``gateway.profile_routes`` from a raw config dict (top-level or nested), else None."""
+ if not isinstance(raw, dict):
+ return None
+ routes = raw.get("profile_routes")
+ if routes is None and isinstance(raw.get("gateway"), dict):
+ routes = raw["gateway"].get("profile_routes")
+ return routes
+
+
+def _load_managed_raw_config() -> dict:
+ """Managed-scope config (e.g. ``/etc/hermes/config.yaml``), or {} when unavailable."""
+ try:
+ from hermes_cli import managed_scope
+ return managed_scope.load_managed_config() or {}
+ except Exception:
+ return {}
+
+
def _primary_profile_routes_for_current_home() -> list:
"""Primary gateway ``profile_routes`` targeting the profile being served; ``[]`` if this IS the
primary home. Satellite crons are ticked and delivered by the primary gateway (a satellite
@@ -163,9 +182,12 @@ def _primary_profile_routes_for_current_home() -> list:
from hermes_cli.config import read_user_config_raw
raw = read_user_config_raw(config_path) # raw primary file, not the merged current-profile config
- routes_raw = raw.get("profile_routes")
- if routes_raw is None and isinstance(raw.get("gateway"), dict):
- routes_raw = raw["gateway"].get("profile_routes")
+ # Managed-scope overlay wins over the user file, exactly like the merged config: routes
+ # pinned in /etc/hermes/config.yaml never reach the raw user read (#121212), which made
+ # preflight false-block and routed delivery fail closed on centrally-managed installs.
+ routes_raw = _profile_routes_from_raw_config(_load_managed_raw_config())
+ if routes_raw is None:
+ routes_raw = _profile_routes_from_raw_config(raw)
if not isinstance(routes_raw, list):
return []
diff --git a/tests/cron/test_preflight_managed_routes.py b/tests/cron/test_preflight_managed_routes.py
new file mode 100644
index 00000000000..017aeffe855
--- /dev/null
+++ b/tests/cron/test_preflight_managed_routes.py
@@ -0,0 +1,59 @@
+"""Managed-scope gateway.profile_routes must reach the satellite preflight (#121212)."""
+
+from types import SimpleNamespace
+
+import cron.scheduler_preflight as preflight
+
+
+def _route(profile="sat"):
+ return SimpleNamespace(enabled=True, profile=profile)
+
+
+def _setup(monkeypatch, tmp_path, user_raw, managed_raw):
+ import hermes_constants
+ import hermes_cli.config as cfg
+ import hermes_cli.managed_scope as ms
+ import hermes_cli.profiles as profiles
+ import gateway.profile_routing as pr
+
+ primary = tmp_path / "primary"
+ primary.mkdir()
+ (primary / "config.yaml").write_text("gateway: {}\n")
+ monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: primary)
+ monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: str(tmp_path / "satellite"))
+ monkeypatch.setattr(cfg, "read_user_config_raw", lambda path=None: user_raw)
+ monkeypatch.setattr(ms, "load_managed_config", lambda: managed_raw)
+ monkeypatch.setattr(pr, "parse_profile_routes", lambda routes: [_route()])
+ monkeypatch.setattr(profiles, "profile_matches_home", lambda p: True)
+
+
+def test_managed_scope_routes_resolve(monkeypatch, tmp_path):
+ _setup(
+ monkeypatch, tmp_path,
+ user_raw={"gateway": {}},
+ managed_raw={"gateway": {"profile_routes": [{"profile": "sat", "platforms": ["whatsapp"]}]}},
+ )
+ assert len(preflight._primary_profile_routes_for_current_home()) == 1
+
+
+def test_user_file_routes_still_resolve_without_managed_scope(monkeypatch, tmp_path):
+ _setup(
+ monkeypatch, tmp_path,
+ user_raw={"profile_routes": [{"profile": "sat", "platforms": ["whatsapp"]}]},
+ managed_raw={},
+ )
+ assert len(preflight._primary_profile_routes_for_current_home()) == 1
+
+
+def test_managed_scope_wins_over_user_file(monkeypatch, tmp_path):
+ calls = []
+ import gateway.profile_routing as pr
+
+ _setup(
+ monkeypatch, tmp_path,
+ user_raw={"profile_routes": [{"profile": "from-user"}]},
+ managed_raw={"gateway": {"profile_routes": [{"profile": "from-managed"}]}},
+ )
+ monkeypatch.setattr(pr, "parse_profile_routes", lambda routes: (calls.append(routes) or [_route()]))
+ assert len(preflight._primary_profile_routes_for_current_home()) == 1
+ assert calls == [[{"profile": "from-managed"}]]