From 8d3b2e86a9b8e3e4453cecb02268300dbff6e765 Mon Sep 17 00:00:00 2001 From: Muhammad Usama <55662931+MuhammadUsamaMX@users.noreply.github.com> Date: Tue, 28 Jul 2026 18:28:12 +0500 Subject: [PATCH 1/2] fix(cron): deliver each profile's cron via its own adapter in multiplex Builds on a61183b (per-profile HERMES_HOME scoping in the multiplex ticker). Cron EXECUTION is now scoped per profile, but _start_multiplex still passed the default profile's `adapters` to `cron_tick` for every profile. As a result all secondary profiles' cron delivery (origin/home-channel replies and failure summaries) was sent through the DEFAULT profile's platform adapter -- e.g. a secondary Telegram profile's scheduled-job output went out the DEFAULT bot instead of that profile's own bot. Thread a `profile_adapters` map (profile name -> adapters) from the gateway (GatewayRunner._profile_adapters) into InProcessCronScheduler.start and select the per-profile adapter set in the multiplex tick loop, falling back to the shared `adapters` for the default profile (which owns them). No change to single-profile behavior. --- cron/scheduler_provider.py | 15 ++++++++++++++- gateway/run.py | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cron/scheduler_provider.py b/cron/scheduler_provider.py index 4c20db01c6d42..7016694c28f7e 100644 --- a/cron/scheduler_provider.py +++ b/cron/scheduler_provider.py @@ -182,6 +182,7 @@ class InProcessCronScheduler(CronScheduler): interval=60, can_dispatch=None, profile_homes=None, + profile_adapters=None, ): import logging from cron.scheduler import tick as cron_tick @@ -209,6 +210,7 @@ class InProcessCronScheduler(CronScheduler): loop=loop, interval=interval, can_dispatch=can_dispatch, + profile_adapters=profile_adapters, ) return @@ -269,6 +271,7 @@ class InProcessCronScheduler(CronScheduler): loop=None, interval=60, can_dispatch=None, + profile_adapters=None, ): """Tick every served profile's cron store when multiplex_profiles is on. @@ -319,13 +322,23 @@ class InProcessCronScheduler(CronScheduler): logger.debug("Cron dispatch paused while gateway drains existing work") else: for entry in profile_homes: + _pname = entry[0] if isinstance(entry, tuple) else None home = entry[1] if isinstance(entry, tuple) else entry home_token = set_hermes_home_override(str(home)) try: with use_cron_store(home): + # Deliver each profile's cron via ITS OWN bot/adapter. + # Secondary profiles live in profile_adapters[name]; + # the default profile has no entry there and owns the + # shared `adapters` set — so fall back to it. Without + # this, every profile's cron output ships through the + # default profile's bot (wrong-bot cross-delivery). + _tick_adapters = adapters + if profile_adapters and _pname and profile_adapters.get(_pname): + _tick_adapters = profile_adapters[_pname] cron_tick( verbose=False, - adapters=adapters, + adapters=_tick_adapters, loop=loop, sync=False, can_dispatch=can_dispatch, diff --git a/gateway/run.py b/gateway/run.py index 06a26d73aeabf..5f58b40f7755f 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -25512,6 +25512,12 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool = profile_homes = list(profiles_to_serve(multiplex=True)) if profile_homes: cron_start_kwargs["profile_homes"] = profile_homes + # Per-profile adapters so each profile's cron output is + # delivered via its own bot/adapter instead of the default + # profile's. + cron_start_kwargs["profile_adapters"] = getattr( + runner, "_profile_adapters", None + ) logger.info( "Cron scheduler will tick %d profile(s) under multiplex: %s", len(profile_homes), From 3b25d7a0184f20b5fee79d16a11afe44258da293 Mon Sep 17 00:00:00 2001 From: Muhammad Usama <55662931+MuhammadUsamaMX@users.noreply.github.com> Date: Thu, 30 Jul 2026 21:16:27 +0500 Subject: [PATCH 2/2] fix(cron): reserve shared adapters for the default profile only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses review feedback on #73363. The previous truthy `profile_adapters.get(name)` check fell back to the shared (default-profile) adapters whenever a secondary profile's adapter map was empty — which is the normal state before that profile's bot connects (the map is created empty and filled only on a successful connection). That reintroduced wrong-bot cross-delivery for the secondary until it connected. Thread the default profile identity and reserve the shared `adapters` set for it alone; every other profile uses its own adapter map, or an empty set when its bot has not connected yet (so it simply does not deliver that tick). Add regression coverage for the default, connected-secondary, empty-secondary and missing-secondary adapter-map cases. --- cron/scheduler_provider.py | 25 ++++--- gateway/run.py | 7 ++ tests/cron/test_scheduler_provider.py | 95 +++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 9 deletions(-) diff --git a/cron/scheduler_provider.py b/cron/scheduler_provider.py index 7016694c28f7e..5fb20dc7b1e2a 100644 --- a/cron/scheduler_provider.py +++ b/cron/scheduler_provider.py @@ -183,6 +183,7 @@ class InProcessCronScheduler(CronScheduler): can_dispatch=None, profile_homes=None, profile_adapters=None, + default_profile=None, ): import logging from cron.scheduler import tick as cron_tick @@ -211,6 +212,7 @@ class InProcessCronScheduler(CronScheduler): interval=interval, can_dispatch=can_dispatch, profile_adapters=profile_adapters, + default_profile=default_profile, ) return @@ -272,6 +274,7 @@ class InProcessCronScheduler(CronScheduler): interval=60, can_dispatch=None, profile_adapters=None, + default_profile=None, ): """Tick every served profile's cron store when multiplex_profiles is on. @@ -327,15 +330,19 @@ class InProcessCronScheduler(CronScheduler): home_token = set_hermes_home_override(str(home)) try: with use_cron_store(home): - # Deliver each profile's cron via ITS OWN bot/adapter. - # Secondary profiles live in profile_adapters[name]; - # the default profile has no entry there and owns the - # shared `adapters` set — so fall back to it. Without - # this, every profile's cron output ships through the - # default profile's bot (wrong-bot cross-delivery). - _tick_adapters = adapters - if profile_adapters and _pname and profile_adapters.get(_pname): - _tick_adapters = profile_adapters[_pname] + # Deliver each profile's cron via ITS OWN adapters. + # The shared `adapters` set belongs to the default + # profile only. A secondary profile uses its own map + # in profile_adapters[name], which is populated only + # once that profile's bot connects. A secondary must + # NEVER fall back to the default profile's `adapters` + # (that ships its cron output through the wrong bot), + # so before its adapter connects — map absent or empty + # — it simply does not deliver this tick. + if _pname is None or _pname == default_profile: + _tick_adapters = adapters + else: + _tick_adapters = (profile_adapters or {}).get(_pname) or {} cron_tick( verbose=False, adapters=_tick_adapters, diff --git a/gateway/run.py b/gateway/run.py index 5f58b40f7755f..2b629493f597c 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -25518,6 +25518,13 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool = cron_start_kwargs["profile_adapters"] = getattr( runner, "_profile_adapters", None ) + # runner.adapters belongs to the default profile, which + # profiles_to_serve() names "default" in its multiplex list. + # Thread that identity so the ticker reserves the shared adapters + # for the default profile alone and never routes a secondary's + # cron through the default bot (even before its adapter connects, + # when profile_adapters[name] is still absent/empty). + cron_start_kwargs["default_profile"] = "default" logger.info( "Cron scheduler will tick %d profile(s) under multiplex: %s", len(profile_homes), diff --git a/tests/cron/test_scheduler_provider.py b/tests/cron/test_scheduler_provider.py index 0229d8dd48c56..fde568844fda2 100644 --- a/tests/cron/test_scheduler_provider.py +++ b/tests/cron/test_scheduler_provider.py @@ -414,3 +414,98 @@ def test_multiplex_ticker_ticks_each_profile_once(tmp_path, monkeypatch): f"Expected >= {len(profile_homes)} tick calls, got {len(tick_count)}" + + +def _run_multiplex_capture(tmp_path, *, profile_adapters, shared_adapters): + """Run the multiplex ticker one full cycle and return the ``adapters`` + object passed to ``tick()`` for the default profile and the secondary + profile (in ``profile_homes`` order: default first, secondary second).""" + from cron.scheduler_provider import InProcessCronScheduler + + p_default = tmp_path / "default" + p_sec = tmp_path / "home-ops" + for d in (p_default, p_sec): + (d / "cron").mkdir(parents=True) + profile_homes = [("default", p_default), ("home-ops", p_sec)] + + captured: list = [] # adapters seen per tick call; order follows profile_homes + + def _capturing_tick(*args, **kwargs): + captured.append(kwargs.get("adapters")) + return 0 + + stop = threading.Event() + prov = InProcessCronScheduler() + with patch("cron.scheduler.tick", side_effect=_capturing_tick), \ + patch("cron.jobs.record_ticker_heartbeat", lambda **kw: None): + t = threading.Thread( + target=prov.start, + args=(stop,), + kwargs={ + "interval": 0, + "profile_homes": profile_homes, + "adapters": shared_adapters, + "profile_adapters": profile_adapters, + "default_profile": "default", + }, + daemon=True, + ) + t.start() + deadline = time.monotonic() + 10 + while len(captured) < 2 and time.monotonic() < deadline: + time.sleep(0.005) + stop.set() + t.join(timeout=5) + + assert not t.is_alive() + assert len(captured) >= 2, f"expected >= 2 tick calls, got {len(captured)}" + return captured[0], captured[1] # (default, secondary) of the first cycle + + +def test_multiplex_default_profile_uses_shared_adapters(tmp_path): + """The default profile's cron is delivered via the shared ``adapters`` set + (which belongs to the default profile).""" + shared = {"kind": "shared"} + default_ad, _ = _run_multiplex_capture( + tmp_path, profile_adapters={"home-ops": {"kind": "secondary"}}, + shared_adapters=shared, + ) + assert default_ad is shared + + +def test_multiplex_connected_secondary_uses_its_own_adapters(tmp_path): + """A connected secondary is delivered via ITS OWN adapters, not the shared + default-profile set.""" + shared = {"kind": "shared"} + sec = {"kind": "secondary"} + default_ad, sec_ad = _run_multiplex_capture( + tmp_path, profile_adapters={"home-ops": sec}, shared_adapters=shared, + ) + assert default_ad is shared + assert sec_ad is sec + + +def test_multiplex_empty_secondary_does_not_fall_back_to_shared(tmp_path): + """A secondary whose adapter map is present-but-empty (its bot has not + connected yet) must NOT fall back to the default profile's shared adapters + — otherwise its cron output ships through the wrong bot. It receives an + empty adapter set and simply does not deliver this tick.""" + shared = {"kind": "shared"} + default_ad, sec_ad = _run_multiplex_capture( + tmp_path, profile_adapters={"home-ops": {}}, shared_adapters=shared, + ) + assert default_ad is shared + assert sec_ad is not shared + assert not sec_ad # empty → no delivery, not the default bot + + +def test_multiplex_missing_secondary_does_not_fall_back_to_shared(tmp_path): + """A secondary absent from profile_adapters entirely (its adapter map has + not been created yet) also must not fall back to the shared adapters.""" + shared = {"kind": "shared"} + default_ad, sec_ad = _run_multiplex_capture( + tmp_path, profile_adapters={}, shared_adapters=shared, + ) + assert default_ad is shared + assert sec_ad is not shared + assert not sec_ad