Skip to content

The agent that grows with you

fix(honcho): bound the joined author peer memory by session count

Erosika431cd9084b17
diff --git a/plugins/memory/honcho/session.py b/plugins/memory/honcho/session.py
index 5c54fdd5f8c..3c04e19b45a 100644
--- a/plugins/memory/honcho/session.py
+++ b/plugins/memory/honcho/session.py
@@ -24,6 +24,9 @@ logger = logging.getLogger(__name__)
# Sentinel to signal the async writer thread to shut down
_ASYNC_SHUTDOWN = object()
+# Sessions remembered in _joined_author_peers; the oldest is dropped past this and its authors rejoin on their next write.
+_SESSION_CACHE_MAX_SIZE = 128
+
@dataclass
class HonchoSession:
@@ -260,7 +263,7 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMi
Joins are remembered per session, so this costs one API call per author."""
peer = self._get_or_create_peer(author_peer_id)
with self._cache_lock:
- if author_peer_id in self._joined_author_peers.setdefault(honcho_session_id, set()):
+ if author_peer_id in self._joined_author_peers.get(honcho_session_id, ()):
return peer
try:
from honcho.session import SessionPeerConfig
@@ -273,6 +276,8 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMi
return peer
with self._cache_lock:
self._joined_author_peers.setdefault(honcho_session_id, set()).add(author_peer_id)
+ while len(self._joined_author_peers) > _SESSION_CACHE_MAX_SIZE:
+ self._joined_author_peers.pop(next(iter(self._joined_author_peers)))
return peer
def _flush_session(self, session: HonchoSession) -> bool:
diff --git a/tests/honcho_plugin/test_turn_author_peers.py b/tests/honcho_plugin/test_turn_author_peers.py
index 4a05bf22fa9..b09f83f393c 100644
--- a/tests/honcho_plugin/test_turn_author_peers.py
+++ b/tests/honcho_plugin/test_turn_author_peers.py
@@ -281,6 +281,42 @@ class TestFlushAttributesMessages:
(_peer, config), = honcho_session.add_peers.call_args[0][0]
assert (config.observe_me, config.observe_others) == (False, False)
+ def test_joined_authors_forget_the_oldest_session_past_the_cap(self):
+ """The join memory is bounded by session count; a forgotten session's author rejoins on its next write."""
+ from plugins.memory.honcho import session as session_module
+
+ mgr = _manager(_config(), runtime_id="7654321")
+ first = self._session(mgr, key="telegram:group0")
+ first_honcho = MagicMock()
+ mgr._sessions_cache[first.honcho_session_id] = first_honcho
+ first.add_message("user", "hi", author_peer_id="alice")
+ mgr._flush_session(first)
+
+ for i in range(1, session_module._SESSION_CACHE_MAX_SIZE + 1):
+ session = self._session(mgr, key=f"telegram:group{i}")
+ mgr._sessions_cache[session.honcho_session_id] = MagicMock()
+ session.add_message("user", "hi", author_peer_id="alice")
+ mgr._flush_session(session)
+
+ assert len(mgr._joined_author_peers) == session_module._SESSION_CACHE_MAX_SIZE
+ assert first.honcho_session_id not in mgr._joined_author_peers
+
+ first.add_message("user", "again", author_peer_id="alice")
+ mgr._flush_session(first)
+ assert first_honcho.add_peers.call_count == 2
+
+ def test_a_failed_join_leaves_no_session_entry_behind(self):
+ """Only a successful join occupies one of the bounded slots."""
+ mgr = _manager(_config(), runtime_id="7654321")
+ session = self._session(mgr)
+ honcho_session = MagicMock()
+ honcho_session.add_peers.side_effect = RuntimeError("network")
+ mgr._sessions_cache[session.honcho_session_id] = honcho_session
+
+ session.add_message("user", "alice speaking", author_peer_id="alice")
+ mgr._flush_session(session)
+ assert mgr._joined_author_peers == {}
+
class TestProviderReadsTheAuthor:
def _provider(self) -> HonchoMemoryProvider: