diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py index 318e67d0faf..5c7d299f0a4 100644 --- a/agent/conversation_compression.py +++ b/agent/conversation_compression.py @@ -512,6 +512,16 @@ def compress_context( old_title = agent._session_db.get_session_title(agent.session_id) # Trigger memory extraction on the old session before it rotates. 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") old_session_id = agent.session_id agent.session_id = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}" diff --git a/cli.py b/cli.py index 07fa2b72513..ff5db7d01e0 100644 --- a/cli.py +++ b/cli.py @@ -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: