Skip to content

fix: prevent OpenViking memory loss during context compression - #8362

Closed
fancydirty wants to merge 2 commits into
NousResearch:mainfrom
fancydirty:fix/openviking-memory-loss-during-compression
Closed

fix: prevent OpenViking memory loss during context compression#8362
fancydirty wants to merge 2 commits into
NousResearch:mainfrom
fancydirty:fix/openviking-memory-loss-during-compression

Conversation

@fancydirty

Copy link
Copy Markdown
Contributor

Summary

Fixes two bugs that cause long-term memory to be lost when context compression triggers a session split while OpenViking is enabled.

Changes

run_agent.py

  • _compress_context – call self._memory_manager.on_session_end(messages) before rotating the session ID, and initialize_all(session_id=...) after.
  • flush_memories – expose both memory and viking_remember tools to the summary model, and actually execute viking_remember tool calls.

Tests

  • tests/run_agent/test_compression_memory_provider.py (new) – verifies memory-provider lifecycle hooks are called correctly during compression.
  • tests/run_agent/test_flush_memories_codex.py – added TestFlushMemoriesExposesVikingRemember to ensure viking_remember is visible and executable.

Verification

  • Targeted compression/flush tests: 64 passed
  • Full suite: no new regressions (75 pre-existing failures unrelated to this change)

Related issues

Memory loss during long-running sessions with OpenViking enabled.

- Call memory_manager.on_session_end() before session split in _compress_context
  so archived messages are committed to OpenViking instead of being dropped.
- Re-initialize memory providers with the new session_id after compression.
- Expose viking_remember to the summary model in flush_memories and execute
  the tool call through the memory manager, enabling OpenViking writes.

Closes memory-loss issues during long-running sessions.
Copilot AI review requested due to automatic review settings April 12, 2026 12:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes loss of long-term memory for external memory providers (notably OpenViking) when context compression triggers a SQLite session split, by ensuring provider lifecycle hooks and flush tooling run across the split boundary.

Changes:

  • Update flush_memories() to surface both memory and viking_remember tools to the summary/flush call and execute viking_remember tool calls.
  • Update _compress_context() to notify memory providers before rotating session_id and re-initialize providers after the split.
  • Add regression tests covering provider lifecycle during compression and viking_remember exposure/execution in flush.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
run_agent.py Adds session-end + re-init lifecycle calls around compression session split; expands flush tool exposure/execution to include viking_remember.
tests/run_agent/test_compression_memory_provider.py New tests asserting memory provider lifecycle hooks are invoked correctly during compression-driven session splits.
tests/run_agent/test_flush_memories_codex.py Adds tests ensuring viking_remember is included in flush tool lists and that returned tool calls are executed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread run_agent.py
Comment on lines +6468 to +6472
# Make one API call with memory-related tools available
memory_tool_defs = []
for t in (self.tools or []):
if t.get("function", {}).get("name") == "memory":
memory_tool_def = t
break
name = t.get("function", {}).get("name")
if name in ("memory", "viking_remember"):
Comment thread run_agent.py
Comment on lines +6654 to +6660
# subsequent sync_turn calls target the new session
if self._memory_manager:
try:
self._memory_manager.initialize_all(
session_id=self.session_id,
platform=self.platform or os.environ.get("HERMES_SESSION_SOURCE", "cli"),
)
Comment on lines +286 to +296
agent = run_agent.AIAgent(
api_key="test-key",
base_url="https://test.example.com/v1",
provider=provider,
api_mode=api_mode,
max_iterations=4,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
agent._memory_store = MagicMock()

@ZaynJarvis ZaynJarvis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Compression Path Memory Commit + flush_memories Viking Support

Verdict: Approve — good fix with solid test coverage

Checklist Pass

  • _compress_context() calls memory_manager.on_session_end(messages) before session ID rotation — correct, the old messages are committed under the old session_id
  • _compress_context() calls memory_manager.initialize_all(session_id=self.session_id) after new session_id is set — providers re-bind to the new session
  • flush_memories() now collects both "memory" and "viking_remember" tools — the model can use either during the flush pass
  • flush_memories() handles viking_remember tool calls by dispatching to memory_manager.handle_tool_call()
  • Tests: 100-line test_compression_memory_provider.py + 124-line extension to test_flush_memories_codex.py — covers both the on_session_end call ordering and the tool inclusion
  • No new imports at module level, no circular dependencies
  • AGENTS.md note about not bypassing abstraction layers is a good addition

One Minor Issue: initialize_all Call After Compression

The call to memory_manager.initialize_all() uses platform=self.platform or os.environ.get("HERMES_SESSION_SOURCE", "cli"). This is fine but note that initialize_all typically calls each provider's initialize() method which may reset prefetch state. Since we're mid-compression this is the correct behavior — new session, fresh state.

Merge Order

This PR touches run_agent.py which also has diffs in #8474. Merge this after #7762 and alongside or before #9167 to avoid conflicts. The flush_memories changes here are orthogonal to #9167's session management changes.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/plugins Plugin system and bundled plugins tool/memory Memory tool and memory providers labels Apr 28, 2026
@ZaynJarvis

ZaynJarvis commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

would recommend to close because fix on #10463 is merged to commit long session.

@fancydirty fancydirty closed this Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/plugins Plugin system and bundled plugins P2 Medium — degraded but workaround exists tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants