Skip to content

fix(gateway): prevent stale memory overwrites by flush agent (#2670)#2687

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-e8325591
Mar 23, 2026
Merged

fix(gateway): prevent stale memory overwrites by flush agent (#2670)#2687
teknium1 merged 1 commit into
mainfrom
hermes/hermes-e8325591

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

Fixes #2670. Salvages ideas from #2675 (devorun) and #2676 (dlkakbs).

The gateway memory flush agent spawns a temporary AIAgent on session reset/expiry to review old conversation history and save memories. It had no awareness of memory changes made after that conversation ended — by the live agent, cron jobs, or concurrent sessions — causing silent overwrites of newer entries.

Root cause

The flush agent received the old conversation + a generic "save important facts" prompt. While current memory was technically present in its system prompt, there was no explicit connection between "here's what's already saved" and "review this conversation." The model would focus on the old conversation and replace entries with stale versions, reverting changes that happened after the conversation ended.

The fix (two parts)

1. Cron session bypass (from #2675)
Skip memory flush entirely for cron sessions (cron_* session IDs). Cron sessions are headless with no meaningful user conversation to extract memories from.

2. Live memory injection into flush prompt
Read the current MEMORY.md and USER.md from disk and inject them directly into the flush prompt (user turn). The flush agent now sees the current memory state right next to its instructions, so it can:

  • See what's already captured and skip duplicates
  • Make informed replace decisions only when the conversation genuinely supersedes existing entries
  • Avoid blindly overwriting entries added by other sessions or cron jobs

This is better than the add-only constraint (#2676) because the model retains full tool access and can make intelligent decisions with complete information.

Why not the other approaches?

Changes

  • gateway/run.py: Added cron session bypass + live memory content injection in _flush_memories_for_session()
  • tests/gateway/test_flush_memory_stale_guard.py: 7 tests covering cron bypass, memory injection, empty/missing files, and prompt structure

Test plan

  1. All 6051 tests pass (pytest tests/ -n0 -q)
  2. Cron sessions are confirmed skipped (no load_transcript call)
  3. Memory content verified present in flush prompt when files exist
  4. Flush still works normally when memory files are empty or missing
  5. Core flush instructions preserved in all cases

Credit

Salvaged from:

The gateway memory flush agent reviews old conversation history on session
reset/expiry and writes to memory. It had no awareness of memory changes
made after that conversation ended (by the live agent, cron jobs, or other
sessions), causing silent overwrites of newer entries.

Two fixes:

1. Skip memory flush entirely for cron sessions (session IDs starting with
   'cron_'). Cron sessions are headless with no meaningful user conversation
   to extract memories from.

2. Inject the current live memory state (MEMORY.md + USER.md) directly into
   the flush prompt. The flush agent can now see what's already saved and
   make informed decisions — only adding genuinely new information rather
   than blindly overwriting entries that may have been updated since the
   conversation ended.

Addresses the root cause identified in #2670: the flush agent was making
memory decisions blind to the current state of memory, causing stale
context to overwrite newer entries on gateway restarts and session resets.

Co-authored-by: devorun <devorun@users.noreply.github.com>
Co-authored-by: dlkakbs <dlkakbs@users.noreply.github.com>
@teknium1 teknium1 marked this pull request as ready for review March 23, 2026 23:08
@teknium1 teknium1 merged commit 48b5bc6 into main Mar 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory flush agent overwrites live memory on session reset/gateway restart

1 participant