fix(gateway): keep idle cached agents alive until session expires#31856
Open
trismegistus-wanderer wants to merge 1 commit into
Open
Conversation
…pires The idle-TTL sweep (_sweep_idle_cached_agents) was evicting agents as soon as they passed _AGENT_CACHE_IDLE_TTL_SECS, even when the session hadn't expired yet. In daily-reset mode the reset can fire hours after the last user message — evicting the agent early means the session-expiry watcher has no agent in cache to call on_session_end() with, so memory providers miss the live transcript. Now the sweep checks the session store before evicting: if the session still exists and hasn't expired, the agent stays in cache so the expiry watcher can tear it down properly later. When the session store is unavailable or throws, falls back to the original eviction behavior (safe default). Fixes: NousResearch#11205
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.
What does this PR do?
The idle-TTL sweep (_sweep_idle_cached_agents) was evicting agents as soon as they passed _AGENT_CACHE_IDLE_TTL_SECS, even when the session hadn't expired yet. In daily-reset mode the reset can fire hours after the last user message — evicting the agent early means the session-expiry watcher has no agent in cache to call on_session_end() with, so memory providers miss the live transcript.
Now the sweep checks the session store before evicting: if the session still exists and hasn't expired, the agent stays in cache so the expiry watcher can tear it down properly later. When the session store is unavailable or throws, falls back to the original eviction behavior (safe default).
Related Issue
Fixes #11205
Type of Change
Changes Made
gateway/run.py—_sweep_idle_cached_agents()now checks the session store before evicting an agent past idle TTL. If the session hasn't actually expired (e.g., daily-reset mode where the reset fires hours after the last user message), the agent stays in cache so_session_expiry_watchercan find it later and callon_session_end()with the live transcript. Falls back to original eviction behavior when the session store is unavailable or throws.tests/gateway/test_agent_cache.py— Two new tests inTestAgentCacheBoundedGrowth:test_idle_sweep_keeps_agent_when_session_not_expired— agent past idle TTL is kept when session store says session is alivetest_idle_sweep_evicts_when_session_is_expired— agent past idle TTL is evicted when session store confirms session expiredHow to Test
_AGENT_CACHE_IDLE_TTL_SECSbut before the daily reset timeon_session_end()_session_expiry_watchertears it down properly — memory providers receive the live transcriptUnit test verification:
python -m pytest tests/gateway/test_agent_cache.py::TestAgentCacheBoundedGrowth -v -o 'addopts='Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — N/Acli-config.yaml.exampleif I added/changed config keys — N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/AScreenshots / Logs