Skip to content

fix(gateway): call on_session_end before evicting cached agent - #55045

Closed
kumaxs wants to merge 1 commit into
NousResearch:mainfrom
kumaxs:pr/on-session-end-gateway
Closed

fix(gateway): call on_session_end before evicting cached agent#55045
kumaxs wants to merge 1 commit into
NousResearch:mainfrom
kumaxs:pr/on-session-end-gateway

Conversation

@kumaxs

@kumaxs kumaxs commented Jun 29, 2026

Copy link
Copy Markdown

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_turns and only flush them periodically (every N turns via sync_turn) or on on_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_agent before releasing the evicted agent's resources. This mirrors the existing on_session_end call 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_cap currently pops cache entries directly without going through _evict_cached_agent; a follow-up should align it to also call on_session_end.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery tool/memory Memory tool and memory providers sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Jun 29, 2026
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.
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for identifying the missing memory-finalization path. This is now implemented on main through the broader merged follow-up in PR #57378.

  • Commit 201b646d672733f75fc8d213f7b5b6c6efbc97de adds policy-aware coverage for agent-cache eviction paths.
  • gateway/run.py:7692-7713 finalizes expired sessions through _cleanup_agent_resources_off_loop; gateway/run.py:6042-6057 passes the live transcript to shutdown_memory_provider().
  • gateway/run.py:16347-16411 and gateway/run.py:16505-16522 cover LRU-cap eviction without turning every resumable soft eviction into a session boundary.
  • tests/gateway/test_agent_cache.py:682-764 covers both the finalizable commit and the non-finalizable no-premature-commit case.

Automated hermes-sweeper review.

@teknium1 teknium1 closed this Jul 15, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants