The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: keep authority subscriptions stable across repeated resume
Teknium8c88976587e4
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 109b4c7d786..ffa15b7e379 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -76,7 +76,8 @@ class SessionAuthority:async def attach(self, actor, ref):self.authorize(actor, ref, 'session:read')live = self.sessions[ref.session_id]- subscription = uuid.uuid4().hex+ subscription = next((key for key, member in live.subscribers.items()+ if member == actor), None) or uuid.uuid4().hexlive.subscribers[subscription] = actorreturn SubscriptionSnapshot(subscription, self._handle(ref), self.instance_id,live.sequence, tuple(self.db.get_messages_as_conversation(ref.session_id)),diff --git a/gateway/session_controls.py b/gateway/session_controls.pyindex 9ec4a642070..534ce3d4b5b 100644--- a/gateway/session_controls.py+++ b/gateway/session_controls.py@@ -73,4 +73,5 @@ class AuthorityConnection:async def close(self):for subscription in self.subscriptions.values():await self.authority.detach(self.actor, subscription)+ self.subscriptions.clear()self.authority.events.pop(self.actor.transport_id, None)diff --git a/tests/gateway/test_authority_connection.py b/tests/gateway/test_authority_connection.pynew file mode 100644index 00000000000..19bf84f7235--- /dev/null+++ b/tests/gateway/test_authority_connection.py@@ -0,0 +1,36 @@+"""Connection membership against the real authority and temporary SQLite."""+from types import SimpleNamespace++import pytest++from gateway.session_authority import LiveSession, SessionAuthority+from gateway.session_controls import AuthorityConnection+from hermes_state import SessionDB+from hermes_state_runtime import begin_runtime_epoch+++async def test_repeated_resume_and_close_preserve_only_live_memberships(tmp_path):+ db = SessionDB(db_path=tmp_path / 'state.db')+ try:+ db.create_session('s', source='test')+ epoch = begin_runtime_epoch(db, instance_id='test')+ authority = SessionAuthority(SimpleNamespace(), profile_id='test',+ instance_id='test', db=db, epoch=epoch)+ authority.sessions['s'] = LiveSession(None, 'route')+ first = AuthorityConnection(authority, object(), {'user_id': 'human'})+ peer = AuthorityConnection(authority, object(), {'user_id': 'human'})+ request = {'id': 1, 'method': 'session.resume', 'params': {'session_id': 's'}}+ for connection in (first, peer, first, first):+ assert 'result' in await connection.dispatch(request)+ members = authority.sessions['s'].subscribers+ assert len(members) == 2+ await first.close()+ assert list(members.values()) == [peer.actor]+ assert first.actor.transport_id not in authority.events+ await first.close()+ assert list(members.values()) == [peer.actor]+ await peer.close()+ assert not members and not authority.events+ finally:+ db.close()