Implement Task Memory (raw memory) into Mirix#110
Closed
LiaoJianhe wants to merge 1 commit intoMirix-AI:re-orgfrom
Closed
Implement Task Memory (raw memory) into Mirix#110LiaoJianhe wants to merge 1 commit intoMirix-AI:re-orgfrom
LiaoJianhe wants to merge 1 commit intoMirix-AI:re-orgfrom
Conversation
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.
This is the pahse 1: basic Mirix part of implemention for the Task Memory design.
The search implementation is not implemented in this phase.
Status: - All core components, REST APIs, cleanup jobs, and comprehensive tests implemented and verified.
Implementation Date: January 2026
Total Lines of Code: ~1,800 lines across 5 new files + 5 modified files
Overview
Task Memory (Raw Memory) is a new memory type in MIRIX that stores unprocessed task context without LLM extraction, designed specifically for agent-to-agent task sharing use cases.
This implementation adds a complete memory subsystem following MIRIX's standard architecture patterns, including ORM models, service managers, REST APIs, Redis caching, TTL enforcement, and comprehensive test coverage.
High-Level Design
Core Design Principles
updated_attimestamp)filter_tags.scopefor multi-tenant isolation (same pattern as other memory types)*Itemnaming convention (RawMemoryItem, notRawMemory) to match other memory typesArchitecture Flow
Database Schema
raw_memoryid,user_id,organization_id,context,filter_tags(JSONB), timestamps_created_by_id,_last_update_by_id(track which client created/updated)Files Added (New Implementations)
1.
mirix/orm/raw_memory.py(152 lines)Purpose: SQLAlchemy ORM model for the
raw_memorytableKey Features:
OrganizationMixinandUserMixin(noAgentMixin)filter_tagsJSONB field for scope-based access control_created_by_id,_last_update_by_id)PydanticRawMemoryItemfor Pydantic serializationWhy: Defines database schema, relationships (organization, user), and enables ORM-based CRUD operations with proper indexing for performance.
2.
mirix/schemas/raw_memory.py(131 lines)Purpose: Pydantic schemas for raw memory validation and serialization
Key Classes:
RawMemoryItemBase- Base schema with context and filter_tagsRawMemoryItem- Full response schema (excludes audit fields per Pydantic v2 requirements)RawMemoryItemCreate- Creation schema with optional ID and timestampsRawMemoryItemUpdate- Update schema with context/tag merge modesWhy: Defines API request/response contracts, provides validation, and ensures type safety across the stack. Uses
*Itemnaming to match other MIRIX memory types.3.
mirix/services/raw_memory_manager.py(360 lines)Purpose: Service manager implementing business logic for raw memory CRUD
Key Methods:
create_raw_memory()- Direct PostgreSQL write with Redis cachingget_raw_memory_by_id()- Cache-first read with fallback to DBupdate_raw_memory()- Supports append/replace modes with cache invalidationdelete_raw_memory()- Hard delete with cache removalKey Features:
get_json()andset_json()occurred_at,created_at,updated_atWhy: Centralizes all operations with Redis caching, access control, logging, and error handling. Provides consistent interface for both API and internal usage.
4.
mirix/jobs/cleanup_raw_memories.py(107 lines)Purpose: Nightly cleanup job for TTL enforcement
Key Function:
delete_stale_raw_memories(days_threshold=14)Features:
updated_at)delete_raw_memory()Why: Automatically enforces the 14-day TTL requirement for raw memories, preventing database bloat and ensuring compliance with data retention policies.
5.
tests/test_raw_memory.py(889 lines, 18 tests)Purpose: Comprehensive test suite covering all functionality
Test Coverage:
Manager Unit Tests (6 tests):
test_manager_create_raw_memory- Create operationstest_manager_get_raw_memory_by_id- Read operationstest_manager_update_raw_memory_replace- Update with replace modetest_manager_update_raw_memory_append- Update with append modetest_manager_update_raw_memory_merge_tags- Update with tag mergingtest_manager_delete_raw_memory- Delete operationsCleanup Job Tests (2 tests):
test_cleanup_job_deletes_stale_memories- Verifies 14-day TTL deletiontest_cleanup_job_respects_custom_threshold- Tests custom day thresholdsRedis Caching Tests (5 tests):
test_raw_memory_create_with_redis- Verifies creation caches to Redis JSONtest_raw_memory_cache_hit_performance- Benchmarks cache hit speed (< 10ms target)test_raw_memory_update_invalidates_cache- Verifies cache invalidation on updatestest_raw_memory_delete_removes_cache- Verifies cache removal on deletestest_raw_memory_works_without_redis- Tests graceful fallback when Redis disabledREST API Integration Tests (5 tests):
test_api_create_and_get_raw_memory- GET endpointtest_api_update_raw_memory_replace- PATCH endpoint (replace mode)test_api_update_raw_memory_append_and_merge- PATCH endpoint (append/merge modes)test_api_delete_raw_memory- DELETE endpointtest_api_get_nonexistent_memory- 404 error handlingWhy: Validates all functionality, ensures Redis caching works correctly, verifies API authentication/authorization, and provides regression protection.
Files Modified (Existing Files Extended)
6.
mirix/server/rest_api.py(3 new endpoints)Changes Added:
GET /memory/raw/{memory_id}- Fetch single raw memoryPATCH /memory/raw/{memory_id}- Update context/filter_tags with append/replace/merge modesDELETE /memory/raw/{memory_id}- Hard delete memoryAuthentication: Uses
get_client_from_jwt_or_api_key()(supports both JWT and API keys)Why: Exposes raw memory functionality via HTTP API following existing MIRIX endpoint patterns (
/memory/episodic/,/memory/semantic/, etc.).7.
mirix/database/redis_client.py(1 line added)Change: Added
RAW_MEMORY_PREFIX = "raw_memory:"constantWhy: Enables Redis caching with namespace isolation. Required for key generation in manager and ORM caching logic.
8.
mirix/orm/sqlalchemy_base.py(1 line added - CRITICAL FIX)Change: Added
"raw_memory": redis_client.RAW_MEMORY_PREFIXto thememory_tablesdictionary in_update_redis_cache()method (line ~1010)Why: This is the most critical change - without this entry, Redis caching doesn't work at all. The ORM's
create_with_redis()method calls_update_redis_cache(), which checks this dictionary to determine if a table uses JSON-based caching. Without this entry, records are logged as "Cached" but not actually stored in Redis.Impact: This was the root cause of initial Redis caching failure discovered during testing.
9.
mirix/server/server.py(registration)Change: Added
self.raw_memory_manager = RawMemoryManager()in server initializationWhy: Makes the manager accessible to REST API endpoints via
server.raw_memory_manager.10.
mirix/jobs/__init__.py(new directory)Change: Created jobs package directory with
__init__.pyWhy: Standard Python package structure for scheduled jobs. Enables import of cleanup job module.