Skip to content

The agent that grows with you

fix(cron): reserve shared adapters for the default profile only

Muhammad Usamaa07370abd05c
diff --git a/cron/scheduler_provider.py b/cron/scheduler_provider.py
index ee26506278b..3b43ce48a3b 100644
--- a/cron/scheduler_provider.py
+++ b/cron/scheduler_provider.py
@@ -559,6 +559,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
@@ -587,6 +588,7 @@ class InProcessCronScheduler(CronScheduler):
interval=interval,
can_dispatch=can_dispatch,
profile_adapters=profile_adapters,
+ default_profile=default_profile,
)
return
@@ -659,6 +661,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.
@@ -719,15 +722,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 01204e1a44f..ad0b31ed34c 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -32679,6 +32679,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 dcf1627bdaa..12ac73560c0 100644
--- a/tests/cron/test_scheduler_provider.py
+++ b/tests/cron/test_scheduler_provider.py
@@ -691,3 +691,98 @@ def test_existing_profile_homes_filters_deleted(tmp_path):
as_paths = _existing_profile_homes([live, deleted])
assert [p for p in as_paths] == [live]
+
+
+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