Skip to content

The agent that grows with you

fix(cron): satellite route lookup reads the primary's layered config (#121212, salvage #121595)

teknium1b9372c409f72
diff --git a/cron/scheduler_preflight.py b/cron/scheduler_preflight.py
index 4ee5b31b65e..f70c1c48d20 100644
--- a/cron/scheduler_preflight.py
+++ b/cron/scheduler_preflight.py
@@ -135,32 +135,13 @@ 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
- holding its own token is a ``duplicate_credential`` fatal). Reads the primary config.yaml
- directly (top-level or nested ``gateway.``) instead of ``load_gateway_config()`` so no primary
- platform config leaks into this process. Shared by preflight rescue and delivery-time
- resolution so they cannot drift.
+ holding its own token is a ``duplicate_credential`` fatal). Reads the primary home's YAML
+ layers (user file + managed-scope overlay, top-level or nested ``gateway.``) through the same
+ loader ``load_gateway_config()`` uses, without bridging platform config into this process.
+ Shared by preflight rescue and delivery-time resolution so they cannot drift.
Under ``gateway.multiplex_profiles`` a satellite profile's cron jobs are ticked by the primary gateway's
in-process ticker (#69377) and delivered through the primary gateway's live adapters — the satellite
@@ -176,18 +157,15 @@ def _primary_profile_routes_for_current_home() -> list:
== current_home.expanduser().resolve(strict=False)
):
return [] # this IS the primary home — nothing to consult
- config_path = primary_home.expanduser() / "config.yaml"
- if not config_path.exists():
- return []
- 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
- # 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)
+ # Same layers the primary gateway's own loader reads: routes pinned in the managed scope
+ # (/etc/hermes/config.yaml) never reached the raw user-file read (#121212), so preflight
+ # false-blocked and routed delivery failed closed on centrally-managed installs.
+ from gateway.config_loader import read_yaml_layers
+ layered = read_yaml_layers(primary_home.expanduser())
+ routes_raw = layered.get("profile_routes")
+ if routes_raw is None and isinstance(layered.get("gateway"), dict):
+ routes_raw = layered["gateway"].get("profile_routes")
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
index 017aeffe855..92b1faa501e 100644
--- a/tests/cron/test_preflight_managed_routes.py
+++ b/tests/cron/test_preflight_managed_routes.py
@@ -1,59 +1,65 @@
-"""Managed-scope gateway.profile_routes must reach the satellite preflight (#121212)."""
+"""Managed-scope ``gateway.profile_routes`` must reach the satellite cron path (#121212).
-from types import SimpleNamespace
+The primary gateway reads routes through the layered loader (user file + managed overlay), so
+on a centrally-managed install the routes live only in ``/etc/hermes/config.yaml``. The
+satellite-side helper shared by preflight rescue and delivery-time ``SharedRouteAdapters`` read
+the raw user file alone, false-blocking every routed job and failing delivery closed.
+"""
-import cron.scheduler_preflight as preflight
+import hermes_yaml as yaml
+import pytest
+from cron.scheduler_preflight import (
+ SharedRouteAdapters,
+ _delivery_platform_routed_from_primary_gateway,
+ _primary_profile_routes_for_current_home,
+)
+from hermes_cli import managed_scope
+from hermes_constants import reset_hermes_home_override, set_hermes_home_override
-def _route(profile="sat"):
- return SimpleNamespace(enabled=True, profile=profile)
+def _routes(*entries):
+ return {"gateway": {"multiplex_profiles": True, "profile_routes": list(entries)}}
-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)
+WA_ROUTE = {"name": "sat-wa", "platform": "whatsapp", "chat_id": "[email protected]", "profile": "sat"}
+TG_ROUTE = {"name": "sat-tg", "platform": "telegram", "chat_id": "-100", "profile": "sat"}
-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 satellite_home(tmp_path, monkeypatch):
+ """Serve profile ``sat`` under a primary root whose managed scope pins the routes."""
+ root = tmp_path / "root"
+ sat_home = root / "profiles" / "sat"
+ sat_home.mkdir(parents=True)
+ managed_dir = tmp_path / "managed"
+ managed_dir.mkdir()
+ monkeypatch.setattr("hermes_constants.get_default_hermes_root", lambda: root)
+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(managed_dir))
+ managed_scope.invalidate_managed_cache()
+ token = set_hermes_home_override(str(sat_home))
+ try:
+ yield root, managed_dir
+ finally:
+ reset_hermes_home_override(token)
+ managed_scope.invalidate_managed_cache()
-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_routes_reach_satellite_preflight_and_delivery(satellite_home):
+ root, managed_dir = satellite_home
+ (root / "config.yaml").write_text(
+ yaml.safe_dump({"platforms": {"whatsapp": {"enabled": True}}}), encoding="utf-8")
+ (managed_dir / "config.yaml").write_text(yaml.safe_dump(_routes(WA_ROUTE)), encoding="utf-8")
+ assert _delivery_platform_routed_from_primary_gateway("whatsapp")
+ shared = SharedRouteAdapters({"whatsapp": object()}, _primary_profile_routes_for_current_home())
+ assert shared # delivery-time fallback no longer fails closed
+ assert shared.get("whatsapp", {"chat_id": "[email protected]"}) is not None
-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"}]]
+def test_managed_routes_replace_user_file_routes(satellite_home):
+ root, managed_dir = satellite_home
+ (root / "config.yaml").write_text(yaml.safe_dump(_routes(TG_ROUTE)), encoding="utf-8")
+ (managed_dir / "config.yaml").write_text(yaml.safe_dump(_routes(WA_ROUTE)), encoding="utf-8")
+
+ assert [r.platform for r in _primary_profile_routes_for_current_home()] == ["whatsapp"]