fix: end TUI sessions in state.db to prevent ghost rows in /resume#18283
Closed
luyao618 wants to merge 1 commit into
Closed
fix: end TUI sessions in state.db to prevent ghost rows in /resume#18283luyao618 wants to merge 1 commit into
luyao618 wants to merge 1 commit into
Conversation
TUI sessions were eagerly created in state.db during session.create but never marked as ended — _finalize_session() and _shutdown_sessions() committed memory and fired hooks but skipped db.end_session(). This left every abandoned or normally-closed TUI session with ended_at=NULL, cluttering /resume with ghost rows. Add db.end_session() call to _finalize_session() with appropriate end_reason for each code path: - session.close → "tui_close" - session.branch → "branched" (mirrors CLI behavior) - atexit shutdown → "tui_shutdown" The call is wrapped in try/except to avoid disrupting the existing finalization flow if the DB is unavailable or locked. Closes NousResearch#18269
Collaborator
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
Fixes #18269 — TUI sessions created eagerly in
state.dbwere never marked as ended, leaving ghost rows (withended_at=NULL,message_count=0) cluttering/resume.Problem
_finalize_session()committed memory and fired hooks but never calleddb.end_session(). By contrast, CLI properly callsend_session()on quit,/new,/branch, and/resume. Every TUI session — even ones closed immediately without sending a message — persisted as an open row in the sessions table.Changes
tui_gateway/server.py_finalize_session()now accepts anend_reasonparameter (default"tui_close") and callsdb.end_session(session_key, end_reason)after the existing memory-commit and hook logic. Wrapped intry/exceptto avoid disrupting finalization if DB is unavailable._shutdown_sessions()passesend_reason="tui_shutdown"to distinguish atexit cleanup.session.branchhandler now callsdb.end_session(old_key, "branched")on the parent session, mirroring CLI behavior.tests/tui_gateway/test_ghost_sessions.py(new)end_sessionis called with correct reason, no double-finalize, graceful handling when DB isNoneor raises.Testing
No changes to
hermes_state.pyor CLI — theSessionDB.end_session()API already exists and is battle-tested.