Skip to content

fix(agent): notify context_compressor on commit_memory_session - #22433

Closed
wesleysimplicio wants to merge 1 commit into
NousResearch:mainfrom
wesleysimplicio:fix/commit-memory-session-notify-compressor
Closed

fix(agent): notify context_compressor on commit_memory_session#22433
wesleysimplicio wants to merge 1 commit into
NousResearch:mainfrom
wesleysimplicio:fix/commit-memory-session-notify-compressor

Conversation

@wesleysimplicio

Copy link
Copy Markdown
Contributor

Summary

AIAgent.commit_memory_session() is invoked when the session_id rotates
without tearing providers down — CLI /new, gateway session expiry, and
in-flight context compression. It already calls
_memory_manager.on_session_end(), but never called
context_compressor.on_session_end(), so plugin context engines like
hermes-lcm miss the final session-end flush:

  • Messages that arrived after the last compress() call are never
    persisted to lcm.db.
  • The LCM lifecycle store keeps the session marked active forever.

shutdown_memory_provider() already does both calls; commit_memory_session
was the asymmetric outlier.

Fix

Mirror the shutdown_memory_provider() pattern: the compressor flush is
now invoked under the same hasattr(...) and self.context_compressor
guard, and each backend is checked independently so a missing
_memory_manager no longer skips the compressor.

if self._memory_manager:
    try:
        self._memory_manager.on_session_end(messages or [])
    except Exception:
        pass
if hasattr(self, "context_compressor") and self.context_compressor:
    try:
        self.context_compressor.on_session_end(self.session_id or "", messages or [])
    except Exception:
        pass

Tests

New regression tests in tests/agent/test_commit_memory_session_compressor.py:

  • test_notifies_both_memory_manager_and_context_compressor
  • test_compressor_flushed_even_when_memory_manager_absent
  • test_memory_manager_failure_does_not_block_compressor
  • test_compressor_failure_is_swallowed
  • test_no_op_when_neither_backend_present
  • test_uses_empty_string_session_id_when_none

Stash-verified: 4/6 fail without the fix (the two no-op cases pass either
way), all 6 pass with it.

Test plan

  • New regression tests pass with fix; fail without it
  • Wider regression: pytest tests/agent/test_memory_provider.py tests/run_agent/ tests/cli/test_cli_new_session.py tests/agent/test_commit_memory_session_compressor.py → 1332 passed, 9 skipped
  • Full regression: pytest → 21240 passed; the 115 failures match the pre-existing flaky baseline on origin/main (test_discord_, test_gateway_service, test_gateway_wsl, test_file_, test_terminal_tool_requirements, test_tts_media_routing, test_setup_openclaw_migration, test_tencent_tokenhub_provider, test_list_picker_providers, test_model_switch_custom_providers, test_tui_gateway_server, test_web_server::test_pub_broadcasts) — none touched by this PR

Closes #22394

commit_memory_session() is called when the session_id rotates without
tearing providers down (CLI /new, gateway session expiry, in-flight
context compression).  It correctly notified _memory_manager but never
called context_compressor.on_session_end(), so plugin context engines
like hermes-lcm missed the final flush:

- messages that arrived after the last compress() call were never
  persisted to lcm.db
- the LCM lifecycle store kept the session marked active forever
- shutdown_memory_provider() already does both calls; commit_memory_session
  was the asymmetric outlier

Mirror the shutdown_memory_provider() pattern so the compressor receives
on_session_end(self.session_id or "", messages or []), guarded by the
same hasattr / try-except as the shutdown path.  Each backend is now
checked independently — a missing _memory_manager no longer skips the
compressor flush.

Closes NousResearch#22394
Copilot AI review requested due to automatic review settings May 9, 2026 09:16

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

This PR fixes an asymmetry in AIAgent.commit_memory_session() by ensuring it notifies the configured context engine (context_compressor) on session rotation, matching the behavior already implemented in shutdown_memory_provider(). This prevents plugin-based context engines (e.g., hermes-lcm) from missing the final session-end flush when sessions rotate without provider teardown (e.g., /new, gateway expiry, compression-driven rollover).

Changes:

  • Update AIAgent.commit_memory_session() to independently flush _memory_manager and context_compressor (each guarded and exception-swallowed).
  • Add regression tests covering fan-out behavior, failure isolation, and edge cases (missing backends, session_id=None).

Reviewed changes

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

File Description
run_agent.py Ensures commit_memory_session() also calls context_compressor.on_session_end(...) under the same guards used elsewhere, and does not skip compressor flush when _memory_manager is absent.
tests/agent/test_commit_memory_session_compressor.py Adds targeted regression tests verifying both backends are notified and failures in one backend do not block the other.

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

@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 May 9, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing fix with #22431 — both address #22394. Same fix: notify context_compressor.on_session_end() in commit_memory_session().

@wesleysimplicio

Copy link
Copy Markdown
Contributor Author

Closing this one to reduce queue noise. The underlying issue (#22394) was resolved via merged PR #22764, so this branch is now superseded.

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.

commit_memory_session() missing context_compressor.on_session_end() call

3 participants