The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): the boundary evict commits for a context engine without a provider
Erosikaf28fff4cf70d
diff --git a/gateway/run_agent_cache.py b/gateway/run_agent_cache.pyindex e66045453a7..060a4ac7d5a 100644--- a/gateway/run_agent_cache.py+++ b/gateway/run_agent_cache.py@@ -670,7 +670,7 @@ class GatewayAgentCacheMixin:if agent is None or agent is _AGENT_PENDING_SENTINEL or id(agent) in self._running_agent_ids():returnif commit_memory:- target, args = self._commit_then_release_soft, (agent, session_key)+ target, args = self._commit_then_release_soft, (agent, session_key, True)else:target, args = self._release_evicted_agent_soft, (agent,)self._spawn_release_thread(target, args, f"agent-evict-{str(session_key)[:24]}", inline_fallback=True,@@ -718,10 +718,14 @@ class GatewayAgentCacheMixin:with _profile_runtime_scope(home or get_hermes_home()):target(*args)- def _commit_memory_before_soft_evict(self, agent: Any, key: str) -> None:- """Commit the live transcript to memory providers before resource-only eviction."""- # No external memory provider (``_memory_manager`` None) — nothing to commit.- if agent is None or not hasattr(agent, "commit_memory_session") or getattr(agent, "_memory_manager", None) is None:+ def _commit_memory_before_soft_evict(self, agent: Any, key: str, boundary: bool = False) -> None:+ """Commit the live transcript to memory providers before resource-only eviction. A session+ boundary (reset, compress) also ends the context engine's session; that engine can run without a provider."""+ if agent is None or not hasattr(agent, "commit_memory_session"):+ return+ has_provider = getattr(agent, "_memory_manager", None) is not None+ has_engine = boundary and getattr(agent, "context_compressor", None) is not None+ if not has_provider and not has_engine:returntry:messages = getattr(agent, "_session_messages", None)@@ -733,10 +737,10 @@ class GatewayAgentCacheMixin:except Exception as _e:logger.debug("Pre-evict memory commit failed for %s: %s", key, _e)- def _commit_then_release_soft(self, agent: Any, key: str) -> None:+ def _commit_then_release_soft(self, agent: Any, key: str, boundary: bool = False) -> None:"""Commit end-of-session memory (if warranted), then soft-release — on the daemon evictionthread. Order matters: commit needs the live memory manager before ``release_clients``."""- self._commit_memory_before_soft_evict(agent, key)+ self._commit_memory_before_soft_evict(agent, key, boundary)self._release_evicted_agent_soft(agent)def _release_evicted_agent_soft(self, agent: Any) -> None:diff --git a/tests/gateway/test_boundary_evict_commits_memory.py b/tests/gateway/test_boundary_evict_commits_memory.pyindex 80878cfa7e9..eeba4271744 100644--- a/tests/gateway/test_boundary_evict_commits_memory.py+++ b/tests/gateway/test_boundary_evict_commits_memory.py@@ -39,6 +39,36 @@ def test_boundary_evict_commits_the_transcript_before_releasing(monkeypatch):assert 'route' not in runner._agent_cache+def test_boundary_evict_commits_for_a_context_engine_without_a_provider(monkeypatch):+ """``commit_memory_session`` also ends the context engine's session; that engine runs without any+ external memory provider, so ``_memory_manager`` alone must not gate the boundary commit."""+ runner = _runner(monkeypatch)+ calls = []+ agent = _agent(calls)+ agent._memory_manager = None+ agent.context_compressor = MagicMock()+ runner._agent_cache['route'] = (agent, 'sig')+ runner._evict_cached_agent_at_boundary('route')+ assert calls == [('commit', [{'role': 'user', 'content': 'hi'}]), ('release', None)]+++def test_evicts_with_nothing_to_notify_only_release(monkeypatch):+ """No provider and no engine: neither kind of evict has anything to commit. A resource evict with+ only an engine keeps the conversation open and does not end the engine's session either."""+ runner = _runner(monkeypatch)+ calls = []+ agent = _agent(calls)+ agent._memory_manager = None+ runner._agent_cache['route'] = (agent, 'sig')+ runner._evict_cached_agent_at_boundary('route')+ assert calls == [('release', None)]+ calls.clear()+ agent.context_compressor = MagicMock()+ runner._agent_cache['route'] = (agent, 'sig')+ runner._evict_cached_agent('route')+ assert calls == [('release', None)]++def test_plain_evict_still_skips_the_commit(monkeypatch):"""``/model`` and rewind keep the conversation open; only the clients are recycled."""runner = _runner(monkeypatch)