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:
parent
73cd8622f9
commit
81ff916e57
@ -512,6 +512,16 @@ def compress_context(
|
|||||||
old_title = agent._session_db.get_session_title(agent.session_id)
|
old_title = agent._session_db.get_session_title(agent.session_id)
|
||||||
# Trigger memory extraction on the old session before it rotates.
|
# Trigger memory extraction on the old session before it rotates.
|
||||||
agent.commit_memory_session(messages)
|
agent.commit_memory_session(messages)
|
||||||
|
# Flush any un-persisted messages from the current turn to the
|
||||||
|
# old session *before* rotating. compress_context() can be
|
||||||
|
# called mid-turn (auto-compress when context exceeds threshold)
|
||||||
|
# at a point 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).
|
||||||
|
try:
|
||||||
|
agent._flush_messages_to_session_db(messages)
|
||||||
|
except Exception:
|
||||||
|
pass # best-effort — don't block compression on a flush error
|
||||||
agent._session_db.end_session(agent.session_id, "compression")
|
agent._session_db.end_session(agent.session_id, "compression")
|
||||||
old_session_id = agent.session_id
|
old_session_id = agent.session_id
|
||||||
agent.session_id = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}"
|
agent.session_id = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}"
|
||||||
|
|||||||
12
cli.py
12
cli.py
@ -5975,6 +5975,18 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin):
|
|||||||
|
|
||||||
old_session_id = self.session_id
|
old_session_id = self.session_id
|
||||||
if self._session_db and old_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:
|
try:
|
||||||
self._session_db.end_session(old_session_id, "new_session")
|
self._session_db.end_session(old_session_id, "new_session")
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user