Skip to content

fix(compression): flush messages before session rotation to prevent data loss#47204

Closed
x7peeps wants to merge 1 commit into
NousResearch:mainfrom
x7peeps:fix/compress-flush-unflushed
Closed

fix(compression): flush messages before session rotation to prevent data loss#47204
x7peeps wants to merge 1 commit into
NousResearch:mainfrom
x7peeps:fix/compress-flush-unflushed

Conversation

@x7peeps

@x7peeps x7peeps commented Jun 16, 2026

Copy link
Copy Markdown

Problem

compress_context() in conversation_compression.py and new_session() in cli.py call end_session() without first flushing in-memory messages to the session DB. When auto-compression triggers mid-turn (context exceeds threshold), messages generated during the current turn that haven't yet been persisted are permanently lost:

  • The old session ends with message_count from its last clean turn
  • All messages from the turn that triggered compression are gone
  • session_search, /resume, and offline backup all show a gap

This is especially painful for long tool-heavy turns (vision analysis, code execution, multi-step workflows) where 50+ messages can be generated in a single turn.

Fix

Add _flush_messages_to_session_db(messages) before end_session() in both sites:

  1. agent/conversation_compression.py:compress_context() — flush before end_session when auto-compression rotates the session
  2. cli.py:new_session() — flush before end_session when the /new command rotates the session

Both fixes use the existing _flush_messages_to_session_db() method which is already idempotent (tracks _last_flushed_db_idx) and has proper error handling.

Closes #47202

…ata loss

compress_context() and cli.new_session() both call end_session()
without first flushing in-memory messages to the session DB. When
auto-compression triggers mid-turn (context exceeds threshold),
messages generated during the current turn that haven't yet been
persisted are permanently lost — the old session ends with a gap
and the new session has no reference to them.

Fix: call _flush_messages_to_session_db() before end_session()
in both sites so all un-persisted messages are written to the
existing session_id before it rotates.

Closes NousResearch#47202
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent loop, run_agent.py, prompt builder comp/cli CLI entry point, hermes_cli/, setup wizard labels Jun 16, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related: this is the flush-before-rotation fix for the mid-turn compression data-loss bug (#47202, #46567). Competing/complementary open PRs target the same root cause via different mechanisms and file-sets:

This PR (#47204) is the only one that also covers the /new command path (cli.py:new_session()) in addition to compress_context(). Maintainer to pick the canonical mechanism and consolidate.

@teknium1

Copy link
Copy Markdown
Contributor

Closing as a duplicate of #47215, which fixed the same flush-before-rotate root cause and was merged via #48584. Both PRs landed the identical guard; #47215 was salvaged with its author's commit preserved. Thanks for the fix — same bug, credited to the merged commit.

@teknium1 teknium1 closed this Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder comp/cli CLI entry point, hermes_cli/, setup wizard P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context compression silently loses unflushed messages (end_session without flush)

3 participants