Skip to content

fix(agent): flush messages before compression session rotation (#46567)#46587

Closed
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/46567-compression-flush-before-rotate
Closed

fix(agent): flush messages before compression session rotation (#46567)#46587
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/46567-compression-flush-before-rotate

Conversation

@Tranquil-Flow

Copy link
Copy Markdown
Contributor

Summary

Fixes #46567 by flushing pending messages to the SQLite session DB before compression closes and rotates away from the current session.

The initial fix added the missing flush before end_session(...). During Path B review, I tightened the patch so the compression-time flush receives conversation_history, which preserves the existing _flush_messages_to_session_db(...) contract: already-persisted history rows are skipped, and only the pending mid-turn tail is written to the old session before rotation.

What changed

  • compress_context(...) accepts optional conversation_history and passes it through to _flush_messages_to_session_db(...) before end_session(...).
  • The main conversation loop supplies the active turn's conversation_history when auto-compression fires.
  • Added regression coverage for:
    • flush is called before end_session
    • flush runs against the old session id before rotation
    • flush is present during compression rotation
    • conversation history is passed through to avoid duplicate SQLite rows

Verification

  • RED proof from build artifact: 2/3 original regression tests failed on upstream/main because compression never called _flush_messages_to_session_db(...) before rotation.
  • Focused tests after reviewer revision:
    • /Users/evinova-self/.hermes/hermes-agent/venv/bin/python3 -m pytest tests/agent/test_compression_flush_before_rotate.py -v -o "addopts=" --tb=short
    • Result: 4 passed
  • Nearby compression tests:
    • /Users/evinova-self/.hermes/hermes-agent/venv/bin/python3 -m pytest tests/agent/test_compression*.py -v -o "addopts=" --tb=short
    • Result: 9 passed, 1 warning (discord.player audioop deprecation)
  • Branch check: git rev-list --left-right --count upstream/main...HEAD0 1

Duplicate / competitor check

  • Same-author PR search for 46567 in title/body: none
  • Same-author branch search for fix/46567*: none
  • Open PR issue-number search: none
  • Open PR keyword search for compression flush session rotation: none

Auto-published by Moonsong via Path B automated pipeline.

@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent loop, run_agent.py, prompt builder P1 High — major feature broken, no workaround labels Jun 15, 2026
@liuhao1024

Copy link
Copy Markdown
Contributor

✅ Code Review — Clean

Reviewed the full diff (4 files, +214/-2). This is a well-scoped regression fix for #46567.

What was checked:

  1. Flush ordering_flush_messages_to_session_db is correctly placed before end_session in compress_context, and before session_id rotation. The test test_flush_called_before_end_session verifies this via call_order tracking.

  2. conversation_history threading — The new parameter flows correctly from conversation_loop.py_compress_contextcompress_context_flush_messages_to_session_db. The test test_flush_receives_conversation_history_to_avoid_duplicate_rows confirms the value passes through.

  3. Dead variable checkconversation_history is used exactly once (passed to _flush_messages_to_session_db). No dead code.

  4. Exception ordering — No new exception handling; the existing commit_memory_session call precedes the new flush, which is the correct order (memory extraction on the old session, then flush, then close).

  5. Test coverage — 4 tests covering: call order, session ID used for flush, regression on unpatched code, and conversation_history forwarding. All use call_order tracking for verification.

No issues found. The fix correctly addresses the data loss in intermediate sessions during chained mid-turn compressions.

@teknium1

Copy link
Copy Markdown
Contributor

Closing — the flush-before-rotate fix for #46567 landed via #48584 (salvaged from #47215, authorship preserved). That PR also fixed the sibling #47202 with the same root cause, plus a follow-up commit guarding the /compress unconditional rewrite (#44794/#39704). Your larger variant here is superseded by the merged minimal fix. Thanks for the detailed diagnosis and repro.

@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 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.

Mid-turn compression loses messages: flush before session rotation

4 participants