From 55e1698979d860e34af3bf791e4ec4e18a5a137a Mon Sep 17 00:00:00 2001 From: Adolanium <94890352+Adolanium@users.noreply.github.com> Date: Sat, 29 Aug 2026 08:29:20 +0300 Subject: [PATCH] fix(gateway): a secondary profile handoff fails closed when its config cannot load _process_handoff caught a config load failure for a secondary profile, logged a warning, and kept going with self.config, which is the primary profile's config. The handoff then went out through the right bot to the primary's home channel and the row was reported completed. That is the exact wrong delivery the multi-profile handoff work exists to prevent, and the same fail closed posture the no-live-adapters branch already takes. A load failure now logs an error and raises, which marks the row failed so the CLI can report and retry it. The default profile path is untouched, it never reloaded config. --- gateway/run.py | 14 ++++++++-- .../test_handoff_secondary_profile_adapter.py | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) 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.