hermes - 💡(How to fix) Fix [Feature] Wire on_pre_compress into the context compression pipeline for MCP servers

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

In run_agent.py, just before context_compressor.compress() at line 9701:

for server_name, server in self._mcp_servers.items(): if hasattr(server, 'on_pre_compress'): try: server.on_pre_compress(messages) except Exception as e: logger.warning(f"MCP server {server_name} on_pre_compress failed: {e}")

Fix Action

Fix / Workaround

Current workaround

Code Example

# In run_agent.py, just before context_compressor.compress() at line 9701:
for server_name, server in self._mcp_servers.items():
    if hasattr(server, 'on_pre_compress'):
        try:
            server.on_pre_compress(messages)
        except Exception as e:
            logger.warning(f"MCP server {server_name} on_pre_compress failed: {e}")

---

context_engine:
  compressor:
    pre_compress_hook: "python3 /path/to/backup_script.py"
RAW_BUFFERClick to expand / collapse

Problem

When Hermes Agent's context compression fires (triggered when prompt_tokens >= threshold), it destroys conversation history without any pre-hook to preserve state. This causes catastrophic data loss: the agent loses access to everything that was in context before compression.

What already exists

agent/memory_manager.py defines on_pre_compress() (line 438) which calls provider.on_pre_compress(messages) on all memory providers. run_agent.py already calls this hook at line 9696, before calling context_compressor.compress() at line 9701.

However, hermes-knowledge-base runs as an MCP server, not as a MemoryProvider plugin. The MCP server is completely separate from the MemoryProvider lifecycle, so it never receives the on_pre_compress call.

Proposed Solution

Expose a pre-compression hook accessible to MCP servers. Two options:

Option A — MCP server pre-compression hook (recommended)

Before compressing, Hermes makes a blocking call to each MCP server's pre-compression hook:

# In run_agent.py, just before context_compressor.compress() at line 9701:
for server_name, server in self._mcp_servers.items():
    if hasattr(server, 'on_pre_compress'):
        try:
            server.on_pre_compress(messages)
        except Exception as e:
            logger.warning(f"MCP server {server_name} on_pre_compress failed: {e}")

This mirrors how MemoryProvider.on_pre_compress() already works for native plugins.

Option B — context_engine.pre_compress_hook config

Allow users to configure an external script that Hermes calls before compression:

context_engine:
  compressor:
    pre_compress_hook: "python3 /path/to/backup_script.py"

The configured script receives the full message list as stdin JSON and can run hk_retain or any other backup logic.

Current workaround

Users currently work around this with aggressive cron-based polling (*/10 * * * *) that checks sessions.json and backs up when last_prompt_tokens >= 59%. This is unreliable — compression can fire between checks, causing data loss.

Impact

Without a pre-compression hook, any memory system running as MCP (including hermes-knowledge-base) has a race condition: compression fires → context is compacted → the next tool call (e.g. hk_retain) receives the already-compacted context and backs up incomplete data.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - 💡(How to fix) Fix [Feature] Wire on_pre_compress into the context compression pipeline for MCP servers