fix(gateway): sync cached agent session_id on session reset#3220
Open
Mibayy wants to merge 1 commit intoNousResearch:mainfrom
Open
fix(gateway): sync cached agent session_id on session reset#3220Mibayy wants to merge 1 commit intoNousResearch:mainfrom
Mibayy wants to merge 1 commit intoNousResearch:mainfrom
Conversation
When a session resets (idle expiry, daily reset, /new), the agent cache still holds a reference to the old AIAgent instance with its original session_id. On the next turn the cached agent is reused — _flush_messages_to_session_db() then writes new messages to the old (ended) session. load_transcript(new_session_id) returns empty, so the model sees only the current message with no conversation history. Fix: after pulling an agent from the cache, compare its session_id to the current one. If they diverge, update both session_id and reset _last_flushed_db_idx to 0 so the next flush writes from the start of the new session. Closes NousResearch#3210
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.
Summary
Closes #3210
Fixes Telegram (and all gateway platforms) losing conversation history after a session reset.
Root Cause
Gateway agents are cached by
session_keyto preserve the frozen system prompt and tool schemas across turns. When a session resets (idle expiry, daily reset,/new), a newsession_idis assigned — but the cachedAIAgentinstance still holds its originalsession_id.On the next message after a reset:
get_or_create_session()creates a new session → newsession_id_run_agent()hits the agent cache → reuses the oldAIAgent(session_id = old)agent.run_conversation()completes →_flush_messages_to_session_db()writes toagent.session_id(the old session)load_transcript(new_session_id)returns empty → the model sees only 1 messageThis explains the reporter's exact symptom: "sometimes it knows about previous messages" (the first few turns after restart, before any reset) and "most of the time it just completely forgets" (every turn after the first reset).
Fix
One block added after the cache hit, inside
_run_agent():_last_flushed_db_idxis reset to 0 so that_flush_messages_to_session_db()writes all messages from the start of the new session (no messages have been flushed to it yet).Why
_last_flushed_db_idx = 0is safeThe flush cursor tracks how many messages have already been persisted for the current session. After a reset, the new session has zero messages in the DB — so starting the cursor at 0 is correct. Without this reset,
flush_from = max(len(history), 0)would still be 0 for an empty history, so the reset is belt-and-suspenders but necessary for correctness when_last_flushed_db_idxaccumulated from the previous session's run.Scope
Single file, 14 lines added. All 1453 gateway tests pass.