The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: preserve authority ownership through scoped adapter callbacks
Teknium42ce26af2e48
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.pyindex 18f5c026915..95b27759d0a 100644--- a/gateway/run_adapters.py+++ b/gateway/run_adapters.py@@ -1298,7 +1298,7 @@ class GatewayAdapterLifecycleMixin:from gateway.session_ingress_context import native_callback, register_transport_homeregister_transport_home(self, profile_name, profile_home)- async def _handler(event):+ async def _handler(_runner, event):self._stamp_event_profile(event, profile_name)if profile_home is not None:event.source._authorization_profile_home = profile_home@@ -1306,7 +1306,7 @@ class GatewayAdapterLifecycleMixin:async with self._scope_or_null(_async_profile_runtime_scope, profile_home):return await self._handle_message(event)- return _handler+ return _handler.__get__(self)def _make_profile_busy_session_handler(self, profile_name: str):"""Stamp an owning adapter's profile before resolving busy policy."""@@ -1324,7 +1324,7 @@ class GatewayAdapterLifecycleMixin:from gateway.session_ingress_context import register_transport_homeregister_transport_home(self, None, default_home)- async def _handler(event):+ async def _handler(_runner, event):source = event.source# In-process only (serialization ignores dynamic attrs); route ≠ admitting bot.source._authorization_profile_home = default_home@@ -1344,7 +1344,7 @@ class GatewayAdapterLifecycleMixin:async with _async_profile_runtime_scope(profile_home):return await self._handle_message(event)- return _handler+ return _handler.__get__(self)def _stamp_routed_profile(self, source) -> bool:"""Stamp ``source.profile`` from ``profile_routes``; False when the route is rejected."""@@ -1357,7 +1357,8 @@ class GatewayAdapterLifecycleMixin:def _primary_message_handler(self):"""Return the correctly scoped handler for a primary adapter."""- return self._make_default_profile_message_handler() if self._multiplex_on() else self._handle_message+ shared = getattr(self, 'session_authority', None) is not None+ return self._make_default_profile_message_handler() if self._multiplex_on() or shared else self._handle_messagedef _multiplex_on(self) -> bool:return bool(getattr(self.config, "multiplex_profiles", False))diff --git a/tests/gateway/fixtures/native_ingress_trust_multiplex.py b/tests/gateway/fixtures/native_ingress_trust_multiplex.pyindex ac1679f8033..d5aa5cee6ba 100644--- a/tests/gateway/fixtures/native_ingress_trust_multiplex.py+++ b/tests/gateway/fixtures/native_ingress_trust_multiplex.py@@ -37,7 +37,7 @@ async def multiplex_probe(runner, authority, primary, state, mode, peer):adapter.config.token = 'secondary-connector-fixture'adapter.gateway_runner = runnerrunner._profile_adapters = {'transport': {Platform.TELEGRAM: adapter}}- adapter.set_message_handler(runner._make_profile_message_handler('transport'))+ runner._configure_profile_adapter(adapter, 'transport', Platform.TELEGRAM)source = adapter.build_source(chat_id='mux-chat', chat_type='dm', user_id='fixture-user')def rows(sid):@@ -108,6 +108,24 @@ async def multiplex_probe(runner, authority, primary, state, mode, peer):while adapter._active_sessions:await asyncio.sleep(0.01)assert rows(entry.session_id) == committed+ if mode == 'multiplex':+ await adapter.handle_message(MessageEvent(text='BLOCK_FIFO', source=source, message_id='busy-1'))+ assert await asyncio.to_thread(peer.blocked.wait, 5), 'model gate not reached'+ queued = MessageEvent(text='MULTIPLEX_QUEUED', source=source, message_id='busy-2')+ task = asyncio.create_task(adapter.handle_message(queued))+ try:+ async with asyncio.timeout(3):+ while not any(row['request_id'] == 'busy-2' for row in rows(entry.session_id)):+ await asyncio.sleep(0.01)+ assert not adapter._pending_messages, 'callback wrapper restored adapter-local queue'+ finally:+ peer.release.set()+ await task+ async with asyncio.timeout(10):+ while adapter._active_sessions:+ await asyncio.sleep(0.01)+ assert all(row['outcome'] == 'completed' for row in rows(entry.session_id))+ assert worker_scopes == [(str(state), 'runtime-fixture-key')] * 3, worker_scopesif mode == 'capture':# A queued storage fixture derived from the actual committed callback envelope.# This is restart reauthorization evidence, not a native post-ACK kill claim.diff --git a/tests/gateway/fixtures/native_ingress_trust_peer.py b/tests/gateway/fixtures/native_ingress_trust_peer.pyindex feda023d2e2..c16d7d0db4b 100644--- a/tests/gateway/fixtures/native_ingress_trust_peer.py+++ b/tests/gateway/fixtures/native_ingress_trust_peer.py@@ -51,7 +51,7 @@ async def probe(mode, peer):adapter.gateway_runner = runnerrunner.adapters[Platform.TELEGRAM] = adapter# Exercise the real callback which stamps the authorization home, not a synthetic runner call.- adapter.set_message_handler(runner._make_default_profile_message_handler())+ runner._wire_adapter_handlers(adapter)if mode in {'multiplex', 'capture', 'recover', 'recover-again'}:from native_ingress_trust_multiplex import multiplex_probeawait multiplex_probe(runner, authority, adapter, state, mode, peer)@@ -82,6 +82,7 @@ async def probe(mode, peer):entry = runner.session_store.get_or_create_session(source)rows = list_session_admissions(authority.db, session_id=entry.session_id, pending_only=False)assert rows and rows[0]['status'] == 'terminal' and rows[0]['outcome'] == 'completed', (rows, adapter.deliveries)+ assert 'provenance' in rows[0]['payload']['native_text_v1'], rowsassert len(peer.requests) == 1, peer.requestsassert any('LOCAL_ACK_MESSAGING_WARM' in d['content'] for d in adapter.deliveries), adapter.deliveriesprint(json.dumps({'rows': rows, 'deliveries': adapter.deliveries, 'model_calls': len(peer.requests)}))