The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a named-profile multiplexer ticks its own cron store and owns the shared adapters
Teknium1c08edccf073
diff --git a/gateway/run.py b/gateway/run.pyindex 83f737d5a64..5c3413fe43b 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -1645,6 +1645,23 @@ def _multiplex_profile_homes(config: object) -> list[tuple[str, "Path"]]:multiplex=True, profile_allowlist=getattr(config, "multiplex_profile_allowlist", None)))+def _cron_tick_profile_homes(config: object) -> list[tuple[str, "Path"]]:+ """Profile homes the in-process ticker visits under multiplex: the served set PLUS the+ process-active profile. ``profiles_to_serve`` starts at default + allowlist, so a+ ``--profile <name>`` multiplexer was omitted unless allowlisted — and allowlisting it would+ start a second adapter on its own bot token. Adapter startup already skips ``active``."""+ from hermes_cli.profiles import get_active_profile_name, get_profile_dir++ homes = _multiplex_profile_homes(config)+ active = get_active_profile_name() or "default"+ if any(name == active for name, _home in homes):+ return homes+ try:+ return homes + [(active, get_profile_dir(active))]+ except Exception:+ return homes++def _enable_multiplex_log_routing(config: object) -> bool:"""Route agent.log/errors.log/gateway.log records to their owning profile (inert single-profile).``setup_logging(mode="gateway")`` binds file handlers to the launch home, so under multiplexing@@ -5106,17 +5123,19 @@ def _start_gateway_start_cron_and_housekeeping(runner):resolve_cron_scheduler(), multiplex_profiles=multiplex_cron)cron_start_kwargs: Dict[str, Any] = {"adapters": runner.adapters, "loop": asyncio.get_running_loop()}- # Multiplex: tell the ticker which profile homes to tick, else secondary profiles' jobs never run.+ # Multiplex: tell the ticker which profile homes to tick (else secondary profiles' jobs never+ # run, #69377), including a ``--profile <name>`` multiplexer's OWN store.if isinstance(cron_provider, InProcessCronScheduler) and multiplex_cron:try:- profile_homes = _multiplex_profile_homes(runner.config)+ profile_homes = _cron_tick_profile_homes(runner.config)if profile_homes:cron_start_kwargs["profile_homes"] = profile_homes# Per-profile adapters so each profile's cron output goes via its own bot, not the default's.cron_start_kwargs["profile_adapters"] = getattr(runner, "_profile_adapters", None)- # runner.adapters belongs to "default"; naming it keeps the ticker from routing a secondary's- # cron through the default bot (even before that profile's adapter connects).- cron_start_kwargs["default_profile"] = "default"+ # runner.adapters belongs to the LAUNCH profile (``default``, or the ``--profile``+ # name); naming it keeps the ticker from routing a secondary's cron through that bot+ # and lets a named multiplexer's own jobs reuse its live adapters.+ cron_start_kwargs["default_profile"] = runner._primary_profile_namelogger.info("Cron scheduler will tick %d profile(s) under multiplex: %s", len(profile_homes),[p[0] if isinstance(p, tuple) else p for p in profile_homes])diff --git a/tests/gateway/test_multiplex_lifecycle.py b/tests/gateway/test_multiplex_lifecycle.pyindex 417360ab7a1..94aebccc8db 100644--- a/tests/gateway/test_multiplex_lifecycle.py+++ b/tests/gateway/test_multiplex_lifecycle.py@@ -41,6 +41,34 @@ def test_cron_profile_homes_follow_allowlist(tmp_path, monkeypatch):assert [name for name, _home in homes] == ["default", "worker"]+def test_cron_tick_homes_include_active_named_host(tmp_path, monkeypatch):+ """Named-profile gateway + allowlist must still tick the host store.++ Adapter homes stay allowlist-only so the host is not started a second+ time as a secondary (same bot token). Cron unions the active profile.+ """+ monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)+ default_home = tmp_path / ".hermes"+ for name in ("host", "worker"):+ (default_home / "profiles" / name).mkdir(parents=True)+ monkeypatch.setenv("HERMES_HOME", str(default_home / "profiles" / "host"))++ import gateway.run as gateway_run++ cfg = GatewayConfig(+ multiplex_profiles=True,+ multiplex_profile_allowlist=["worker"],+ )+ adapter_names = [name for name, _home in gateway_run._multiplex_profile_homes(cfg)]+ cron_homes = gateway_run._cron_tick_profile_homes(cfg)+ cron_names = [name for name, _home in cron_homes]+ cron_by_name = dict(cron_homes)++ assert adapter_names == ["default", "worker"]+ assert cron_names == ["default", "worker", "host"]+ assert cron_by_name["host"] == default_home / "profiles" / "host"++class TestNamedProfileMultiplexerGuard:"""_guard_named_profile_under_multiplexer is inert unless all conditions hold."""diff --git a/tests/gateway/test_multiplex_phase0.py b/tests/gateway/test_multiplex_phase0.pyindex 6558d67f54a..69f324519b5 100644--- a/tests/gateway/test_multiplex_phase0.py+++ b/tests/gateway/test_multiplex_phase0.py@@ -67,6 +67,36 @@ class TestSessionKeyNamespacedWhenOn:class TestMultiplexConfigFlag:"""gateway.multiplex_profiles defaults off and round-trips."""+ def test_cron_shared_adapter_owner_is_the_launch_profile(self, monkeypatch, tmp_path):+ """A ``--profile rex`` multiplexer owns ``runner.adapters``; its ticker must name rex (not the+ literal ``default``) as the shared-adapter owner or rex's own jobs fall to the fail-closed map."""+ import asyncio+ from types import SimpleNamespace+ from gateway import run as run_mod+ from cron.scheduler_provider import InProcessCronScheduler++ captured = {}++ class _Ticker(InProcessCronScheduler):+ def start(self, stop_event, **kwargs):+ captured.update(kwargs)++ monkeypatch.setattr("cron.scheduler_provider.resolve_cron_scheduler", lambda: _Ticker())+ monkeypatch.setattr(run_mod, "_cron_tick_profile_homes", lambda cfg: [("rex", tmp_path)])+ monkeypatch.setattr(run_mod, "_start_gateway_housekeeping", lambda *a, **k: None)+ runner = SimpleNamespace(+ config=GatewayConfig(multiplex_profiles=True), adapters={}, _profile_adapters={},+ _primary_profile_name="rex", _draining=False, _external_drain_active=False)++ async def _go():+ return run_mod._start_gateway_start_cron_and_housekeeping(runner)++ cron_stop, _provider, cron_thread, hk = asyncio.run(_go())+ cron_stop.set()+ cron_thread.join(timeout=5)+ hk.join(timeout=5)+ assert captured["default_profile"] == "rex"+def test_default_is_false(self):assert GatewayConfig().multiplex_profiles is False