fix(gateway): call on_session_end before evicting cached agent - #55045
Closed
kumaxs wants to merge 1 commit into
Closed
fix(gateway): call on_session_end before evicting cached agent#55045kumaxs wants to merge 1 commit into
kumaxs wants to merge 1 commit into
Conversation
12 tasks
When an agent is evicted from the cache (idle TTL eviction, /new, /reset, cap enforcement), memory providers lose their in-memory buffer without being notified. Providers like Hindsight accumulate turns in _session_turns and only flush them on sync_turn every N turns or on on_session_switch. A session that ends with fewer than N buffered turns would lose that data silently. Call agent._memory_manager.on_session_end() before releasing the evicted agent's resources, giving providers a chance to flush unsaved data. This covers: - _session_expiry_watcher -> _sweep_idle_cached_agents -> _evict_cached_agent - _evict_cached_agent (explicit /new, /reset, /model, CLI handoff) - test/cleanup paths that call _evict_cached_agent directly Note: _enforce_agent_cache_cap pops cache entries directly without going through _evict_cached_agent; a follow-up should align it too.
kumaxs
force-pushed
the
pr/on-session-end-gateway
branch
from
June 30, 2026 15:57
a5ec2e0 to
21b8eec
Compare
1 task
Contributor
|
Thanks for identifying the missing memory-finalization path. This is now implemented on
Automated hermes-sweeper review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the gateway evicts a cached AIAgent (idle TTL expiry, /new, /reset, cache-cap enforcement), memory providers lose their in-memory buffer without any notification. Providers like Hindsight accumulate conversation turns in
_session_turnsand only flush them periodically (every N turns viasync_turn) or onon_session_switch. If the session ends with fewer than N buffered turns, those turns are silently discarded.Fix
Call
agent._memory_manager.on_session_end(messages)in_evict_cached_agentbefore releasing the evicted agent's resources. This mirrors the existingon_session_endcall in the CLI shutdown path (run_agent.py:3054) and gives memory providers a hook to flush unsaved data when the session ends.Scope
This covers:
_session_expiry_watcher→_sweep_idle_cached_agents→_evict_cached_agent(idle TTL eviction)_evict_cached_agent(explicit /new, /reset, /model, CLI handoff)Note:
_enforce_agent_cache_capcurrently pops cache entries directly without going through_evict_cached_agent; a follow-up should align it to also callon_session_end.