The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(honcho): bound local session cache growth
Aleksei Ivanov280ac2266362
diff --git a/plugins/memory/honcho/session.py b/plugins/memory/honcho/session.pyindex ec1a9effe6d..b7e568754b8 100644--- a/plugins/memory/honcho/session.py+++ b/plugins/memory/honcho/session.py@@ -27,6 +27,12 @@ _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+# Honcho persists every message durably and get_or_create() re-hydrates from it on a cache miss,+# so the local copies are bounded: synced messages beyond the retention cap are trimmed after a+# flush, and sessions idle past the TTL are evicted (next use costs one extra round trip).+_SESSION_MESSAGE_RETENTION = 200+_SESSION_IDLE_TTL_SECONDS = 3600+_SESSION_SWEEP_INTERVAL_SECONDS = 300@dataclass@@ -70,6 +76,7 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMi# honcho_session_id -> author peer IDs already joined to that session.self._joined_author_peers: dict[str, set[str]] = {}self._sessions_cache: dict[str, Any] = {}+ self._last_idle_sweep_ts: float = 0.0# Bumped (under _cache_lock) whenever _force_reauth rebuilds the client, so an# in-flight resolver never stores an object bound to the discarded client.self._client_generation = 0@@ -238,10 +245,36 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMihoncho_session = self._authed_call("session setup", lambda: self._sdk_session(session_id))return honcho_session, existing_messages+ def _sweep_idle_sessions_locked(self) -> int:+ """Evict sessions idle beyond _SESSION_IDLE_TTL_SECONDS together with their SDK session and+ prefetch entries. Caller holds _cache_lock."""+ cutoff = time.time() - _SESSION_IDLE_TTL_SECONDS+ evicted = 0+ for key, session in list(self._cache.items()):+ if session.updated_at.timestamp() >= cutoff:+ continue+ del self._cache[key]+ self._sessions_cache.pop(session.honcho_session_id, None)+ self._context_cache.pop(key, None)+ evicted += 1+ return evicted++ def _maybe_sweep_idle_sessions(self) -> None:+ """Rate-limited idle sweep, run from get_or_create so no watcher thread is needed."""+ now = time.time()+ with self._cache_lock:+ if now - self._last_idle_sweep_ts < _SESSION_SWEEP_INTERVAL_SECONDS:+ return+ self._last_idle_sweep_ts = now+ evicted = self._sweep_idle_sessions_locked()+ if evicted:+ logger.info("Honcho session cache idle sweep evicted %d session(s)", evicted)+def get_or_create(self, key: str, *, user_peer_id: str | None = None) -> HonchoSession:"""Get an existing session or create a new one for ``key`` (usually channel:chat_id).``user_peer_id`` replaces the resolved user peer when the session's participant is not theruntime user, e.g. the sender bot of an a2a session."""+ self._maybe_sweep_idle_sessions()with self._cache_lock:if key in self._cache:logger.debug("Local session cache hit: %s", key)@@ -301,6 +334,14 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMiwhile len(self._joined_author_peers) > _SESSION_CACHE_MAX_SIZE:self._joined_author_peers.pop(next(iter(self._joined_author_peers)))return peer+ @staticmethod+ def _trim_synced_messages(session: HonchoSession) -> None:+ """Drop the oldest synced messages beyond _SESSION_MESSAGE_RETENTION. Messages are appended in+ order, so trimming from the front stops at the first unsynced one and never drops it."""+ excess = len(session.messages) - _SESSION_MESSAGE_RETENTION+ while excess > 0 and session.messages and session.messages[0].get("_synced"):+ session.messages.pop(0)+ excess -= 1def _flush_session(self, session: HonchoSession) -> bool:"""Write unsynced messages to Honcho synchronously."""@@ -335,6 +376,8 @@ class HonchoSessionManager(SessionAuthMixin, SessionPeersMixin, SessionContextMiok = Falsefor msg in new_messages:msg["_synced"] = ok+ if ok:+ self._trim_synced_messages(session)with self._cache_lock:self._cache[session.key] = sessionreturn okdiff --git a/tests/test_honcho_session_cache_bounds.py b/tests/test_honcho_session_cache_bounds.pynew file mode 100644index 00000000000..46c6ec3b8b3--- /dev/null+++ b/tests/test_honcho_session_cache_bounds.py@@ -0,0 +1,157 @@+"""Regression tests for bounded growth of the Honcho local session cache.++Covers the fix for unbounded RSS growth in long-running gateways: prior to+this, ``HonchoSession.messages`` grew forever (never trimmed after a sync),+and ``HonchoSessionManager``'s ``_cache``/``_sessions_cache``/``_context_cache``+had no eviction path short of an explicit ``/new`` reset.+"""++import threading+import time+from datetime import datetime, timedelta+from types import SimpleNamespace++from plugins.memory.honcho.session import (+ HonchoSession,+ HonchoSessionManager,+ _SESSION_IDLE_TTL_SECONDS,+ _SESSION_MESSAGE_RETENTION,+)+++def _session(key="k", honcho_session_id=None):+ return HonchoSession(+ key=key,+ user_peer_id="user",+ assistant_peer_id="assistant",+ honcho_session_id=honcho_session_id or f"hs-{key}",+ )+++def _manager():+ cfg = SimpleNamespace(+ write_frequency="turn",+ dialectic_reasoning_level="low",+ dialectic_dynamic=True,+ dialectic_max_chars=600,+ observation_mode="directional",+ user_observe_me=True,+ user_observe_others=True,+ ai_observe_me=True,+ ai_observe_others=True,+ message_max_chars=25000,+ dialectic_max_input_chars=10000,+ )+ return HonchoSessionManager(honcho=SimpleNamespace(), config=cfg)+++def test_trim_synced_messages_caps_total_length():+ session = _session()+ for i in range(250):+ session.add_message("user", f"msg{i}", _synced=True)+ for i in range(250, 255):+ session.add_message("user", f"msg{i}", _synced=False)+ assert len(session.messages) == 255++ HonchoSessionManager._trim_synced_messages(session)++ assert len(session.messages) == _SESSION_MESSAGE_RETENTION+ # oldest synced messages are the ones dropped; the unsynced tail survives intact+ assert not any(m.get("_synced") is False for m in session.messages[:-5])+ assert all(m.get("_synced") is False for m in session.messages[-5:])+++def test_trim_synced_messages_never_drops_an_unsynced_message():+ session = _session()+ for i in range(300):+ session.add_message("user", f"m{i}", _synced=(i != 10))++ HonchoSessionManager._trim_synced_messages(session)++ contents = [m["content"] for m in session.messages]+ assert "m10" in contents+ idx = contents.index("m10")+ assert session.messages[idx].get("_synced") is False+ # trimming stops at the first unsynced message from the front — it does+ # not skip past it to keep reducing, so everything from there on survives+ assert contents[idx:] == [f"m{i}" for i in range(10, 300)]+++def test_trim_synced_messages_is_a_noop_under_the_cap():+ session = _session()+ for i in range(10):+ session.add_message("user", f"m{i}", _synced=True)++ HonchoSessionManager._trim_synced_messages(session)++ assert len(session.messages) == 10+++def test_sweep_idle_sessions_evicts_stale_entries_across_all_caches():+ mgr = _manager()+ stale = _session(key="stale", honcho_session_id="hs-stale")+ stale.updated_at = datetime.now() - timedelta(seconds=_SESSION_IDLE_TTL_SECONDS + 1)+ fresh = _session(key="fresh", honcho_session_id="hs-fresh")++ mgr._cache = {"stale": stale, "fresh": fresh}+ mgr._sessions_cache = {"hs-stale": object(), "hs-fresh": object()}+ mgr._context_cache = {"stale": {"x": 1}, "fresh": {"x": 1}}++ evicted = mgr._sweep_idle_sessions_locked()++ assert evicted == 1+ assert set(mgr._cache) == {"fresh"}+ assert set(mgr._sessions_cache) == {"hs-fresh"}+ assert set(mgr._context_cache) == {"fresh"}+++def test_sweep_idle_sessions_keeps_fresh_entries():+ mgr = _manager()+ fresh = _session(key="fresh")+ mgr._cache = {"fresh": fresh}+ mgr._sessions_cache = {fresh.honcho_session_id: object()}+ mgr._context_cache = {"fresh": {}}++ evicted = mgr._sweep_idle_sessions_locked()++ assert evicted == 0+ assert "fresh" in mgr._cache+++def test_maybe_sweep_idle_sessions_is_rate_limited():+ mgr = _manager()+ stale = _session(key="stale")+ stale.updated_at = datetime.now() - timedelta(seconds=_SESSION_IDLE_TTL_SECONDS + 1)+ mgr._cache = {"stale": stale}++ mgr._last_idle_sweep_ts = time.time() # just swept — this call should no-op+ mgr._maybe_sweep_idle_sessions()+ assert "stale" in mgr._cache++ mgr._last_idle_sweep_ts = 0.0 # force the interval to have elapsed+ mgr._maybe_sweep_idle_sessions()+ assert "stale" not in mgr._cache+++def test_get_or_create_triggers_sweep_without_blocking_on_lock_reentrancy():+ """`_cache_lock` is an RLock specifically so a sweep triggered from inside+ `get_or_create` (which also takes the lock) can't deadlock the manager's+ own thread. Guard against that regressing silently.+ """+ mgr = _manager()+ stale = _session(key="stale")+ stale.updated_at = datetime.now() - timedelta(seconds=_SESSION_IDLE_TTL_SECONDS + 1)+ mgr._cache = {"stale": stale}+ mgr._last_idle_sweep_ts = 0.0++ done = threading.Event()++ def call_it():+ mgr._maybe_sweep_idle_sessions()+ done.set()++ t = threading.Thread(target=call_it)+ t.start()+ t.join(timeout=5)+ assert done.is_set(), "sweep did not complete — possible deadlock"+ assert "stale" not in mgr._cache