fix(agent): flush un-persisted messages before session rotation (#47202)

compress_context() rotates the session (end_session -> create_session)
mid-turn when auto-compress triggers, but never called
_flush_messages_to_session_db() first. Messages generated during the
current turn that hadn't been persisted to state.db were silently lost.

The same bug existed in cli.py:new_session() (/new command). Both paths
now flush un-persisted messages before ending the old session.
This commit is contained in:
kyssta-exe 2026-06-16 12:24:00 +00:00 committed by Teknium
parent 73cd8622f9
commit 81ff916e57
2 changed files with 22 additions and 0 deletions

12
cli.py
View file

@ -5975,6 +5975,18 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin):
old_session_id = self.session_id
if self._session_db and old_session_id:
# Flush any un-persisted messages from the current turn to the
# old session *before* rotating. /new can be called mid-turn
# when _flush_messages_to_session_db() has not yet run — without
# this, messages generated during the current turn are silently
# lost on session rotation (#47202).
if self.agent:
try:
self.agent._flush_messages_to_session_db(
self.conversation_history
)
except Exception:
pass # best-effort
try:
self._session_db.end_session(old_session_id, "new_session")
except Exception: