diff --git a/gateway/run.py b/gateway/run.py index 1833842c67..2c546c0126 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -14964,14 +14964,22 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew # The watcher already entered _profile_runtime_scope for this # profile, so a fresh load resolves that profile's config.yaml # and .env (home channel, tokens) rather than the primary's. + # Fail closed on a load error: self.config is the primary's, so + # falling back would deliver through the right bot to the + # WRONG chat and report completed. A failed row the CLI can + # retry beats a wrong delivery. try: handoff_config = load_gateway_config() - except Exception: - logger.warning( + except Exception as exc: + logger.error( "Handoff: could not load config for profile %s; " - "falling back to the primary's config", + "failing the handoff instead of delivering via the " + "primary's config", profile_name, exc_info=True, ) + raise RuntimeError( + f"could not load config for profile '{profile_name}': {exc}" + ) from exc # Adapter must be live. A relay-fronted gateway registers ONE adapter # under Platform.RELAY that fronts N logical platforms — so a literal diff --git a/tests/gateway/test_handoff_secondary_profile_adapter.py b/tests/gateway/test_handoff_secondary_profile_adapter.py index e637b293c1..b65e75bfd6 100644 --- a/tests/gateway/test_handoff_secondary_profile_adapter.py +++ b/tests/gateway/test_handoff_secondary_profile_adapter.py @@ -167,6 +167,34 @@ async def test_default_profile_handoff_keeps_primary_adapter(monkeypatch): assert used["home_chat_id"] == "1111" +@pytest.mark.asyncio +async def test_secondary_profile_config_load_failure_fails_closed(monkeypatch): + """A secondary profile whose config cannot load must fail the handoff. + + Falling back to the primary's config delivers through the right bot to + the WRONG chat (the primary's home channel) and reports completed. + """ + runner, _ = _make_multiplex_runner() + used = {} + + def _boom(): + raise RuntimeError("config.yaml exploded") + + monkeypatch.setattr( + "gateway.run.resolve_delivery_transport", _spy_transport_factory(used), + ) + monkeypatch.setattr("gateway.run.load_gateway_config", _boom) + + with pytest.raises(RuntimeError, match="could not load config"): + await runner._process_handoff( + {"id": "cli-session", "title": "work", "handoff_platform": "telegram"}, + profile_name="medicina", + ) + assert used == {}, ( + "nothing may be delivered when the profile config fails to load" + ) + + @pytest.mark.asyncio async def test_secondary_profile_without_live_adapters_fails_loudly(monkeypatch): """Never silently fall back to the primary's bot — that ships to the wrong chat.