Skip to content

The agent that grows with you

fix(gateway): preserve secondary local session policy

KoNit-K24a8c36f3b0b
diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.py
index 20bb5945763..c87b3ac2c80 100644
--- a/gateway/authz_mixin.py
+++ b/gateway/authz_mixin.py
@@ -276,7 +276,12 @@ class GatewayAuthorizationMixin:
platform = getattr(source, "platform", None)
from gateway.session_identity import identity_of
identity = identity_of(source)
- if identity is not None and identity.multiplexed:
+ # A LOCAL session has no receiving bot. Its default transport is inferred as the
+ # primary profile, but the policy and delivery adapter belong to the session authority.
+ # Let the delivery fallback resolve that profile instead of treating a lazily-created
+ # primary LocalSessionAdapter as the receiver (#139060).
+ if (identity is not None and identity.multiplexed
+ and not (platform == Platform.LOCAL and identity.transport_inferred)):
return self._adapters_for_profile(identity.transport_profile).get(platform) if platform else None
if not getattr(getattr(self, "config", None), "multiplex_profiles", False):
return self._primary_adapters().get(platform) if platform else None
diff --git a/tests/gateway/test_multiplex_transport_matrix.py b/tests/gateway/test_multiplex_transport_matrix.py
index fd2b7b27d27..3c6d836486b 100644
--- a/tests/gateway/test_multiplex_transport_matrix.py
+++ b/tests/gateway/test_multiplex_transport_matrix.py
@@ -154,3 +154,29 @@ def test_restored_rows_fail_closed_on_intake_and_deliver_only_via_a_unique_owner
bare = SessionSource(platform=Platform.TELEGRAM, chat_id="42", chat_type="dm", user_id="42")
assert solo.runner._intake_adapter_for(bare) is solo.runner._delivery_adapter_for(bare) is solo.primary
assert solo.runner._intake_adapter_for(None) is None and solo.runner._delivery_adapter_for(None) is None
+
+
+def test_inferred_primary_transport_does_not_hide_a_secondary_local_session_policy(mux):
+ """A LOCAL source belongs to its session authority, not the inferred primary transport (#139060)."""
+ from gateway.session_local import LocalSessionAdapter
+ from gateway.session_policy import policy_for_source
+
+ runner = mux.runner
+ primary_authority = SimpleNamespace(sessions={})
+ secondary_authority = SimpleNamespace(sessions={})
+ primary_adapter = LocalSessionAdapter(primary_authority)
+ secondary_adapter = LocalSessionAdapter(secondary_authority)
+ runner.adapters[Platform.LOCAL] = primary_adapter
+ runner._profile_adapters["team_b"][Platform.LOCAL] = secondary_adapter
+
+ source = SessionSource(platform=Platform.LOCAL, chat_id="desktop-secondary", chat_type="dm",
+ user_id="viewer", profile="team_b")
+ secondary_authority.sessions[source.chat_id] = SimpleNamespace(source=source)
+ secondary_adapter.register_source(source)
+ picked_policy = object()
+ secondary_adapter.policies[source.chat_id] = picked_policy
+
+ identity = resolve_identity(source, runner=runner)
+ assert identity.transport_inferred and identity.transport_profile == "default"
+ assert policy_for_source(runner, source) is picked_policy
+ assert runner._delivery_adapter_for(source) is secondary_adapter