This guide covers the migration from the original memory system to the enhanced MetaMemory v2 system. The v2 system introduces meta-cognitive capabilities, hierarchical goal management, and improved type safety while maintaining backward compatibility.
Before:
from memory.meta_memory import MemoryType, Memory, NarrativeStateAfter:
from memory.types import MemoryType, Memory, NarrativeState
from memory import MetaMemoryBefore:
meta_memory = MetaMemory(embedding_service, long_term_memory)After:
meta_memory = MetaMemory(embedding_service, long_term_memory) # Optional parameter# Install new dependencies if not already installed
pip install psycopg pgvector # For database support
pip install mypy # For type checking
# Update project dependencies
uv sync# BEFORE
from memory import MemoryManager
from memory.meta_memory import MetaMemory, MemoryType
# AFTER
from memory import MemoryManager, MetaMemory
from memory.types import MemoryType, MemoryStability# BEFORE
from memory.meta_memory import MemoryType, create_default_narrative_state
# AFTER
from memory.types import MemoryType, create_default_narrative_state# BEFORE (required long_term_memory)
embedding_service = EmbeddingService()
long_term_memory = LongTermMemory(...)
meta_memory = MetaMemory(embedding_service, long_term_memory)
# AFTER (optional long_term_memory)
meta_memory = MetaMemory(embedding_service, long_term_memory) # Still works
# OR
meta_memory = MetaMemory(embedding_service) # Works without persistence# BEFORE
memory = meta_memory.add_memory("content", MemoryType.FACT, session_id="test")
# AFTER (same API, but now with optional persistence)
memory = meta_memory.add_memory(
"content",
MemoryType.FACT,
session_id="test",
persist=True, # New optional parameter
user_id="user123" # New optional parameter
)# BEFORE
from memory import AgentState
state = AgentState(
messages=[...],
session_id="session_123",
user_id="user_456",
turn_count=0,
# ... other fields
)
# AFTER
state = AgentState(
messages=[...],
session_id="session_123",
user_id="user_456",
turn_count=0,
# ... existing fields
# NEW: MetaMemory fields
narrative_state={}, # Will be loaded automatically
coherence_violations=[],
meta_reasoning="",
long_term_memory=[]
)# BEFORE
memory_manager = MemoryManager(redis_url, postgres_url)
# AFTER (same initialization)
memory_manager = MemoryManager(redis_url, postgres_url)
# NEW: Load narrative state
narrative_state = memory_manager.load_narrative_state(agent_state)
# NEW: Access MetaMemory directly
coherence_result = memory_manager.meta_memory.check_coherence(
"proposed action", narrative_state
)# BEFORE
def process_memory(memory):
return memory.content
# AFTER
from memory.types import Memory
def process_memory(memory: Memory) -> str:
return memory.content# BEFORE
def create_memory(content, memory_type):
return Memory(...)
# AFTER
from memory.types import MemoryType
from typing import Optional
def create_memory(content: str, memory_type: MemoryType, confidence: Optional[float] = None):
return Memory(
content=content,
type=memory_type,
confidence=confidence or 0.8,
# ... other required fields
)# BEFORE
@pytest.fixture
def embedding_service():
return EmbeddingService()
# AFTER (add new fixtures)
@pytest.fixture
def embedding_service():
os.environ['OPENROUTER_API_KEY'] = 'test-key'
return EmbeddingService()
@pytest.fixture
def mock_long_term_memory():
from unittest.mock import Mock
mock = Mock()
mock.store_memory = Mock(return_value=True)
return mock
@pytest.fixture
def narrative_state():
from memory.types import create_default_narrative_state
return create_default_narrative_state()# BEFORE
from memory.meta_memory import MetaMemory
# AFTER
from memory import MetaMemory
from memory.types import MemoryType# pyproject.toml
[tool.mypy]
python_version = "3.11"
warn_return_any = True
[[tool.mypy.overrides]]
module = "memory.*"
disallow_untyped_defs = False # Start permissive, enable later# Initial type check (expect some errors)
mypy src/memory/
# Fix obvious type errors
# Gradually enable stricter checking# BEFORE
from memory.meta_memory import create_default_narrative_state
# AFTER
from memory.types import create_default_narrative_state# BEFORE
from memory import MemoryManager
from memory.meta_memory import MetaMemory
# AFTER
from memory import MemoryManager, MetaMemory
from memory.types import GoalLevel# Run unit tests
pytest tests/unit/ -v
# Check that core functionality works
pytest tests/unit/test_meta_memory.py::TestMetaMemoryBasics::test_meta_memory_initialization -v# Run integration tests (requires Docker)
pytest tests/integration/ -v -m integration# Ensure no type errors
mypy src/memory/- Python: 3.11+ (tested)
- PostgreSQL: 16+ (with pgvector)
- Redis: 7+ (with JSON support)
- ✅ MemoryManager API: Unchanged
- ✅ AgentState structure: Extended, not broken
- ✅ Core memory operations: Unchanged
⚠️ Import paths: Updated for better organization- ❌ Old MetaMemory imports: Deprecated (use new paths)
Problem:
ImportError: No module named 'memory.types'Solution:
# Ensure you're using the updated package
pip install -e .
# Or
uv syncProblem:
error: Library stubs not installed for "psycopg"
Solution:
pip install types-psycopg2
# Or ignore in mypy.ini
[mypy-psycopg.*]
ignore_missing_imports = TrueProblem: Tests fail with database connection errors
Solution:
# Start Docker services
docker-compose up -d
# Wait for services to be healthy
docker-compose ps
# Run tests again
pytest tests/integration/ -m integrationProblem: Still getting circular import issues
Solution: The migration should have resolved this. If not:
# Check that all imports use the new paths
# Ensure no old imports remain
grep -r "from memory.meta_memory import" src/
# Should return no results (except in old backup files)If migration fails, you can rollback:
- Revert import changes to use old paths temporarily
- Keep using v1 API until issues are resolved
- Gradual migration - update one module at a time
- ✅ Better IDE support with type hints
- ✅ Catch errors early with static analysis
- ✅ Improved documentation with type annotations
- ✅ Easier testing with better isolation
- ✅ Zero circular dependencies
- ✅ Professional architecture following SOLID principles
- ✅ Comprehensive test coverage framework
- ✅ Type safety throughout the codebase
- ✅ Meta-cognitive reasoning with coherence validation
- ✅ Hierarchical goal management (Life → Session → Tactical)
- ✅ Semantic conflict detection between memories
- ✅ Narrative state persistence across sessions
- Run the migration following the steps above
- Enable type checking with mypy
- Run full test suite to ensure functionality
- Update CI/CD to include type checking
- Enable stricter type checking gradually
- Add performance benchmarks for MetaMemory operations
- Implement advanced conflict resolution strategies
- Add monitoring and observability for narrative states
If you encounter issues during migration:
- Check the examples -
examples/integration_test.pyshows working usage - Review test failures - Tests provide clear error messages
- Enable debug logging - Set
LOG_LEVEL=DEBUGfor detailed output - Check Docker services - Ensure PostgreSQL and Redis are running
The migration provides significant improvements in code quality and functionality while maintaining a clear upgrade path.