Skip to content

fix(gateway): set end_reason='session_reset' on expiry finalization to prevent recovery loop - #62672

Closed
kh-mitya wants to merge 1 commit into
NousResearch:mainfrom
kh-mitya:fix/61220-session-reset-recovery-loop
Closed

fix(gateway): set end_reason='session_reset' on expiry finalization to prevent recovery loop#62672
kh-mitya wants to merge 1 commit into
NousResearch:mainfrom
kh-mitya:fix/61220-session-reset-recovery-loop

Conversation

@kh-mitya

Copy link
Copy Markdown

Problem

set_expiry_finalized() in gateway/session.py marks sessions as finalized via the expiry_finalized flag, but does not set end_reason='session_reset' in state.db.

The session expiry watcher triggers this. Later, when the agent is cleaned up, run_agent.py calls end_session(session_id, "agent_close"). The recovery query in find_latest_gateway_session_for_peer treats agent_close as 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_reset check is bypassed during recovery, so the session lives forever (Groundhog Day loop).

Fix

After setting expiry_finalized, also call reopen_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 with agent_close, and end_session() no-ops when ended_at IS NOT NULL (first end_reason wins).

Related

Verification

The session expiry watcher flow:

  1. Expiry watcher → set_expiry_finalized() → now sets end_reason='session_reset'
  2. find_latest_gateway_session_for_peer → does NOT match (requires agent_close or NULL) ✅
  3. Next inbound message → _is_session_ended_in_db → True → recovery skipped → falls through to _should_reset
  4. _should_reset → returns None (session already ended with session_reset) → fresh session created ✅

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
@kh-mitya

Copy link
Copy Markdown
Author

Duplicate of #61242 by liuhao1024 — same fix already submitted on Jul 9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Session expiry finalization doesn't set end_reason='session_reset' — stale recovery reopens expired sessions with full history (#54878)

1 participant