fix: guard memory provider initialize() against repeated calls (#20939)#20961
Closed
vominh1919 wants to merge 1 commit into
Closed
fix: guard memory provider initialize() against repeated calls (#20939)#20961vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
…esearch#20939) MemoryManager.initialize_all() now skips providers that are already initialized for the same session_id. This prevents resource-heavy providers (e.g. MemOS/memtensor bridge.cts) from spawning duplicate processes on every turn when the gateway recreates the AIAgent. Changes: - MemoryProvider base class: add _initialized and _initialized_session_id tracking attributes (set after successful initialize()) - MemoryManager.initialize_all(): skip providers already initialized for the same session_id; re-initialize when session_id changes (e.g. compression rotation) - Uses getattr() for backward compat with subclasses that don't call super().__init__() - 2 new tests: same-session skip + backward compat Fixes NousResearch#20939
This was referenced May 7, 2026
1 task
Contributor
|
Thanks for identifying the repeated initialization symptom. This is now implemented on
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Memory providers that spawn resource-heavy processes (e.g. MemOS/memtensor's
bridge.ctsNode.js pair, ~250MB each) re-initialize on every turn in gateway mode, causing:The root cause:
MemoryManager.initialize_all()calls each provider'sinitialize()unconditionally. When the gateway recreates theAIAgent(cache eviction, config change, first message),initialize()runs again, spawning duplicate processes.Fix
Add an initialization guard to the
MemoryProviderbase class andMemoryManager.initialize_all():MemoryProvider.__init__: Track_initialized(bool) and_initialized_session_id(str)MemoryManager.initialize_all(): Skip providers already initialized for the samesession_id; re-initialize whensession_idchanges (e.g. compression rotation)getattr()so subclasses that don't callsuper().__init__()still workChanges
agent/memory_provider.py__init__with_initialized+_initialized_session_idagent/memory_manager.pyinitialize_all()— skip same-session re-inittests/agent/test_memory_provider.pyTests
New tests:
test_initialize_all_skips_same_session— verifiesinitialize()called once per session, not per turntest_initialize_guard_backward_compat_no_super_init— verifies providers withoutsuper().__init__()work viagetattr()Fixes #20939