fix(gateway): completions of a bot_profile-routed session reach the bot that received it
A session routed through another profile's bot (profile_routes with bot_profile) never got its background delegate_task / process completions: - The secondary bot's message handler scoped the event under the BOT's profile, so the bot profile's authority admitted the turn while the route lived in the runtime profile. The wake's automation admission then found no committed turn on the runtime's route (not_found) and the completion retried forever. The handler (and its busy twin) now scope under the identity's runtime home, as the primary's handler already does; authorization still reads the transport home. - The FIFO drain rebuilt the turn's source through restore_provenance, which restored the adapter ref but not the RoutingIdentity, so sessions.transport_profile was never persisted and every restored session lost the receiving bot. restore_provenance now re-pins the identity. - _resolve_injection_adapter looked the platform up in the runtime profile's adapter map; it now reads the pinned transport profile like _delivery_adapter_for (idea from #140009 by @kouyichi). Co-authored-by: qby <qby324@163.com>
This commit is contained in:
@@ -1728,15 +1728,25 @@ class GatewayAdapterLifecycleMixin:
|
||||
register_transport_home(self, profile_name, transport_home)
|
||||
|
||||
async def _handler(_runner, event):
|
||||
self._canonicalize(getattr(event, "source", None), transport_profile=profile_name)
|
||||
identity = self._canonicalize(getattr(event, "source", None), transport_profile=profile_name)
|
||||
if transport_home is not None:
|
||||
event.source._authorization_profile_home = transport_home
|
||||
with native_callback(self, event, transport_home, profile_name):
|
||||
async with self._async_scope_or_null(_async_profile_runtime_scope, profile_home):
|
||||
async with self._async_scope_or_null(
|
||||
_async_profile_runtime_scope, self._routed_runtime_home(identity, profile_home)):
|
||||
return await self._handle_message(event)
|
||||
|
||||
return _handler.__get__(self)
|
||||
|
||||
@staticmethod
|
||||
def _routed_runtime_home(identity, profile_home):
|
||||
"""A secondary bot's event runs in its ``bot_profile`` route's runtime scope (as the primary's handler
|
||||
does), so that runtime's authority admits the turn and later completion wakes find it on the same
|
||||
ledger (#139726). Authorization still reads the transport home (``_under_authorization_profile``)."""
|
||||
if identity is None or profile_home is UNRESOLVED_PROFILE_HOME:
|
||||
return profile_home
|
||||
return identity.runtime_home
|
||||
|
||||
def _make_profile_busy_session_handler(self, profile_name: str):
|
||||
"""Busy-path twin: canonicalize FIRST, then resolve busy policy under the profile scope
|
||||
(auth runs against the profile's own allowlist, same as the cold-path message handler)."""
|
||||
@@ -1744,8 +1754,9 @@ class GatewayAdapterLifecycleMixin:
|
||||
profile_home = self._routed_profile_home(profile_name)
|
||||
|
||||
async def _handler(event, _session_key):
|
||||
self._canonicalize(event.source, transport_profile=profile_name)
|
||||
async with self._async_scope_or_null(_async_profile_runtime_scope, profile_home):
|
||||
identity = self._canonicalize(event.source, transport_profile=profile_name)
|
||||
async with self._async_scope_or_null(
|
||||
_async_profile_runtime_scope, self._routed_runtime_home(identity, profile_home)):
|
||||
return await self._handle_active_session_busy_message(event, self._session_key_for_source(event.source))
|
||||
|
||||
return _handler
|
||||
|
||||
@@ -867,16 +867,22 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica
|
||||
return owner[0]
|
||||
if getattr(source, "delivered_via_upstream_relay", False) is True:
|
||||
return self.adapters.get(Platform.RELAY)
|
||||
# One resolver with authz/kanban/cron: a secondary's own map, or the primary's for a
|
||||
# shared-bot satellite; a disconnected secondary fails closed to ``{}``.
|
||||
adapters = self._adapters_for_profile(getattr(source, "profile", None))
|
||||
# A pinned identity names the bot that received the conversation: it answers, as
|
||||
# ``_delivery_adapter_for`` rules (a ``bot_profile`` route's runtime may own no adapter for this
|
||||
# platform, #139726). Otherwise one resolver with authz/kanban/cron: a secondary's own map, or the
|
||||
# primary's for a shared-bot satellite; a disconnected bot fails closed to ``{}``.
|
||||
from gateway.session_identity import identity_of
|
||||
identity = identity_of(source)
|
||||
pinned = identity is not None and identity.multiplexed and not identity.transport_inferred
|
||||
adapters = self._adapters_for_profile(
|
||||
identity.transport_profile if pinned else getattr(source, "profile", None))
|
||||
try:
|
||||
_transport = resolve_delivery_transport(Platform(platform_name), self.config, adapters)
|
||||
except Exception:
|
||||
_transport = None
|
||||
if _transport is not None:
|
||||
return _transport.adapter
|
||||
if source is not None and (borrowed := self._routed_primary_adapter(source)) is not None:
|
||||
if not pinned and source is not None and (borrowed := self._routed_primary_adapter(source)) is not None:
|
||||
return borrowed
|
||||
if source is not None and (relay := self._committed_relay_adapter(source)) is not None:
|
||||
return relay
|
||||
|
||||
@@ -228,6 +228,10 @@ def restore_provenance(runner, source, provenance):
|
||||
raise RuntimeStoreError('profile_mismatch')
|
||||
source.delivered_via_upstream_relay = candidate.delivered_via_upstream_relay
|
||||
source._transport_adapter_ref = weakref.ref(adapter)
|
||||
# Re-pin the identity ingress resolved: the drained turn's route records the receiving bot from it
|
||||
# (``SessionEntry.transport_profile``), so after a restart its session still answers through that bot.
|
||||
from gateway.session_identity import canonical_identity
|
||||
canonical_identity(source, runner=runner, adapter=adapter, transport_profile=provenance.get('transport_profile'))
|
||||
source._authorization_profile_home = home
|
||||
return runtime_home
|
||||
|
||||
|
||||
@@ -88,6 +88,8 @@ async def multiplex_probe(runner, authority, primary, state, mode, peer):
|
||||
await adapter.handle_message(event)
|
||||
await async_wait_until(lambda: not adapter._active_sessions, timeout=15, what='the adapter to finish its session')
|
||||
entry = runner.session_store.get_or_create_session(source)
|
||||
# The FIFO-drained turn's route records the receiving bot, so a restored session answers through it.
|
||||
assert entry.transport_profile == 'transport', entry
|
||||
committed = rows(entry.session_id)
|
||||
assert committed and committed[0]['outcome'] == 'completed', (committed, adapter.deliveries)
|
||||
provenance = committed[0]['payload']['native_text_v1']['provenance']
|
||||
|
||||
@@ -180,3 +180,40 @@ def test_inferred_primary_transport_does_not_hide_a_secondary_local_session_poli
|
||||
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
|
||||
|
||||
|
||||
def test_restored_completion_injects_through_the_pinned_receiving_bot(mux):
|
||||
"""A detached completion (async delegation, watch) for a restored row injects through the bot its
|
||||
routing entry persisted, as ``_delivery_adapter_for`` answers: the inverse row (team_b's bot ->
|
||||
default) never through the default bot, and nothing while team_b's bot is down (#139726)."""
|
||||
from gateway.session_identity import restore_identity
|
||||
|
||||
r = mux.runner
|
||||
restored = SessionSource(platform=Platform.TELEGRAM, chat_id="999", chat_type="dm", user_id="999")
|
||||
restore_identity(restored, runner=r, transport_profile="team_b")
|
||||
assert r._resolve_injection_adapter("telegram", restored) is r._delivery_adapter_for(restored) is mux.team_b
|
||||
r._profile_adapters["team_b"] = {}
|
||||
r._profile_bot_platforms = {"team_b": {Platform.TELEGRAM}}
|
||||
assert r._resolve_injection_adapter("telegram", restored) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_secondary_bot_route_runs_in_the_routed_runtime_scope(mux):
|
||||
"""A ``bot_profile`` route's event runs under its runtime profile's scope, as a shared-bot route
|
||||
does: that runtime's authority admits the turn, so a later completion wake finds the committed
|
||||
turn on the same ledger (#139726). The bot's own traffic keeps the bot profile's scope."""
|
||||
from gateway.platforms.event import MessageEvent
|
||||
from hermes_constants import get_hermes_home
|
||||
|
||||
r = mux.runner
|
||||
scopes = []
|
||||
|
||||
async def _handle_message(event):
|
||||
scopes.append(get_hermes_home())
|
||||
|
||||
r._handle_message = _handle_message
|
||||
handler = r._make_profile_message_handler("team_b")
|
||||
for chat in ("999", "1"):
|
||||
await handler(MessageEvent(text="hi", source=mux.team_b.build_source(
|
||||
chat_id=chat, chat_type="dm", user_id=chat)))
|
||||
assert scopes == [mux.home, mux.home / "profiles" / "team_b"]
|
||||
|
||||
Reference in New Issue
Block a user