fix(gateway): set end_reason='session_reset' on expiry finalization to prevent recovery loop - #62672
Closed
kh-mitya wants to merge 1 commit into
Closed
Conversation
set_expiry_finalized() in gateway/session.py now calls reopen_session() + end_session(session_id, 'session_reset') after setting the expiry_finalized flag, so that find_latest_gateway_session_for_peer does NOT match the finalized session during the NousResearch#54878 stale-routing self-heal recovery. Without this, the agent cleanup path may later end the session with 'agent_close', which the recovery query treats as recoverable — causing the session to be silently resurrected with full history instead of starting a fresh session after a daily/idle reset. Closes NousResearch#61220
Author
|
Duplicate of #61242 by liuhao1024 — same fix already submitted on Jul 9. |
This was referenced Aug 3, 2026
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
set_expiry_finalized()ingateway/session.pymarks sessions as finalized via theexpiry_finalizedflag, but does not setend_reason='session_reset'in state.db.The session expiry watcher triggers this. Later, when the agent is cleaned up,
run_agent.pycallsend_session(session_id, "agent_close"). The recovery query infind_latest_gateway_session_for_peertreatsagent_closeas recoverable (ended_at IS NULL OR end_reason = 'agent_close').Result: the next inbound message after a daily/idle reset triggers the #54878 stale-routing self-heal, which reopens the expired session with full history instead of starting a fresh session. The
_should_resetcheck is bypassed during recovery, so the session lives forever (Groundhog Day loop).Fix
After setting
expiry_finalized, also callreopen_session()+end_session(session_id, "session_reset")so the recovery query does not match the session.reopen_session()is needed first because the agent cleanup may have already ended the session withagent_close, andend_session()no-ops whenended_at IS NOT NULL(firstend_reasonwins).Related
Verification
The session expiry watcher flow:
set_expiry_finalized()→ now setsend_reason='session_reset'✅find_latest_gateway_session_for_peer→ does NOT match (requiresagent_closeor NULL) ✅_is_session_ended_in_db→ True → recovery skipped → falls through to_should_reset✅_should_reset→ returns None (session already ended withsession_reset) → fresh session created ✅