Skip to content

fix: wire on_pre_compress() into summary prompt and persist compaction summaries#11236

Open
jairodriguez wants to merge 1 commit into
NousResearch:mainfrom
jairodriguez:fix/pre-compaction-context-recovery
Open

fix: wire on_pre_compress() into summary prompt and persist compaction summaries#11236
jairodriguez wants to merge 1 commit into
NousResearch:mainfrom
jairodriguez:fix/pre-compaction-context-recovery

Conversation

@jairodriguez

Copy link
Copy Markdown

Bug: on_pre_compress() return value discarded + no summary persistence

Problem

When Hermes compacts context, two issues cause context loss:

  1. on_pre_compress() return value ignoredMemoryManager.on_pre_compress() collects insights from external memory providers (ByteRover, etc.) before context is compressed, but _compress_context() in run_agent.py calls it without capturing the return value. Provider insights are gathered but never injected into the compaction summary.

  2. No summary disk persistence — The compaction summary is held in memory (_previous_summary) for iterative updates within a session, but never written to disk. If the session restarts or crashes, the summary is lost entirely. There's no mechanism to recover context across sessions.

Changes

run_agent.py:

  • Capture on_pre_compress() return value into provider_context variable
  • Thread provider_context through to compress()_generate_summary()
  • After compression, persist the summary to ~/.hermes/sessions/compaction_summary_{session_id}.md

agent/context_compressor.py:

  • compress() accepts new provider_context parameter (backward-compatible, defaults to "")
  • _generate_summary() accepts new provider_context parameter
  • When provider context is non-empty, it's injected into the LLM summary prompt as "EXTERNAL MEMORY PROVIDER CONTEXT" section

Testing

40/40 context_compressor tests pass
46/46 memory provider tests pass
1117/1122 agent tests pass (5 pre-existing failures unrelated to this change)

Impact

  • External memory providers' pre-compression insights now actually survive compaction
  • Summaries are persisted to disk for cross-session recovery
  • Fully backward-compatible — no behavior change when no providers are registered

…n summaries to disk

Bug: MemoryManager.on_pre_compress() was called in _compress_context() but
its return value was discarded. External memory providers (ByteRover, etc.)
would extract insights before context was lost, but those insights were never
injected into the compaction summary prompt.

Changes:
- Capture on_pre_compress() return value and thread it through compress()
  -> _generate_summary() as provider_context parameter
- Inject provider context into the LLM summary prompt so insights survive
- Persist compaction summaries to ~/.hermes/sessions/compaction_summary_*.md
  for cross-session continuity and post-compaction recovery
- Add context-recovery skill for detecting compaction and reconstructing
  lost context from persisted summaries + session history

The persisted summaries are also available for the new context-recovery skill
which detects compaction markers and reconstructs context from multiple sources.

Tests: 40/40 context_compressor, 46/46 memory_provider, 1117/1122 agent tests
pass (5 pre-existing failures unrelated to this change).
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder labels Apr 25, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #7195 — both wire the on_pre_compress() return value into the compressor. This PR additionally adds disk persistence for compaction summaries.

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 P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants