feat(memory): Cross Agent Memory#541
Merged
Merged
Conversation
Feature/personalization
feat(memory): agents shared memory
aiosfoundation
approved these changes
Apr 23, 2026
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.
feat: Cross-Agent Shared Memory & Natural Language Injection
Summary
Elevates the kernel's memory subsystem from agent-scoped storage to a first-class OS service for cross-agent personalization. Agents like ProfileAgent and TaskAgent can now write shared memories that the kernel automatically discovers and injects into AssistantAgent's LLM context — with JSON content converted to natural language so smaller models can actually use it.
Problem
The kernel's
ContextInjectorresolveduser_idfrom the requesting agent's name instead of from memory metadata. Whenassistant_agentmade anllm_chatcall, the injector searched foruser_id=assistant_agent, missing all profile/task memories written by other agents under the real user identifier (e.g.,user_id=alex_chen). Additionally, raw JSON injected into system prompts hurt small model performance — Phase 2 benchmark scores were worse than Phase 1 across 30 trials.Changes
Cross-Agent Shared Memory Retrieval
_apply_sharing_filter()inproviders/base.py— centralized post-retrieval filter enforcing the privacy invariant: memories from other agents are only returned whensharing_policy="shared"MemoryManager.known_user_idsregistry — tracks real user_ids as agents write memories, enabling cross-agent discovery without requiring the requesting agent to have prior memories_analyze_query_to_memory()fix — preserves the full SDK metadata dict (user_id,owner_agent,sharing_policy,memory_type) onMemoryNote.metadatainstead of silently dropping itclient.search()now usesfilters={"user_id": ...}instead of top-level kwargs (required by Mem0 >= 0.1.29)Mem0Provider.add_memory()writesowner_agent,sharing_policy,memory_typeinto the Mem0 metadata dict so_apply_sharing_filter()can read them on retrievalmax_memoriesfrom Mem0 to prevent private conversation memories from crowding out shared memories in the native relevance rankingUser ID Propagation
ContextInjectorexposesresolved_user_idin injection diagnosticsSyscallExecutorpassesresolved_user_idtoConversationExtractor.extract_async()ConversationExtractorstores conversation memories under the real user_id (not agent name), with full cross-agent metadata (owner_agent,memory_type,sharing_policy)Natural Language Memory Formatting
MemoryFormatter(memory_formatter.py) — pure function converting JSON to natural language at inject time, with three tiers:"profile"→ hardcoded template:"User profile: Their name is Alice. They prefer coding in Python...""task_context"→ hardcoded template:"Current task context: Working on project AIOS. Goals: fix bug..."memory_typewith valid JSON → generic fallback: type name becomes label (e.g.,"research_notes"→"Research notes: topic: transformers. summary: attention is all you need.")"conversation"/ plain text → pass-throughContextInjector.inject()between relevance filtering and token budget truncationmemory_typevalues and get readable formatting without kernel changesInjection Diagnostics
inject()returnstuple[LLMQuery, dict]with structured metrics:candidate_count,injected_count,source_agents,memory_types,prompt_tokens_before/after,resolved_user_idFiles Changed (10 files, +1103 / -127)
aios/memory/context_injector.pyknown_user_idsfallback, formatting step, diagnostics, over-fetch, loggingaios/memory/memory_formatter.pyformat_memory(),_format_profile(),_format_task_context(),_format_generic()aios/memory/manager.pyknown_user_idsregistry, metadata preservation in_analyze_query_to_memory()aios/memory/conversation_extractor.pyuser_idparameter, full cross-agent metadata on stored memoriesaios/memory/providers/base.py_apply_sharing_filter(),_enrich_metadata()aios/memory/providers/mem0.pyfiltersAPI, cross-agent metadata storage, search/retrieval filteringaios/memory/providers/in_house.py_apply_sharing_filter()and_enrich_metadata()aios/memory/providers/zep.py_apply_sharing_filter()and_enrich_metadata()aios/memory/note.pymetadatadict attributeaios/syscall/syscall.pyresolved_user_idpropagation to extractorCommit History
de722ef_apply_sharing_filter(),_enrich_metadata(), provider integration,MemoryNote.metadata4b32f89MemoryManagermetadata preservation,agent_nameinjection into query paramsd28659cknown_user_idsregistry, restructuredinject()to not early-return on empty own memories9da127cConversationExtractorpropagatesresolved_user_id,SyscallExecutorwiringdb068bdfiltersAPI, over-fetch strategy, diagnostic logging0d7094dMemoryFormattermodule with hardcoded profile/task templates and generic JSON fallback, integration intoContextInjectorBackward Compatibility
user_idorsharing_policyin metadata continue to work as before (own-agent-scoped injection only)sharing_policyfilter ensures no private memories leak cross-agentauto_inject: true/falseandauto_extract: true/falseconfig flags are unchangedcreate_memory()andllm_chat()as beforeTesting
Validated via the Cerebrum SDK benchmark harness:
python benchmarks/shared_memory/run_evaluation.py --trials 30 --condition phase1 # restart kernel python benchmarks/shared_memory/run_evaluation.py --trials 30 --condition phase2Known Limitations
known_user_idsis in-memory only — not persisted across kernel restartsknown_user_idsfallback — multi-user scenarios need SDK-leveluser_idpropagation