Files
fast-next-template/backend/app/models/memory/working_memory.py
Felipe Cardoso c9d8c0835c feat(memory): add database schema and storage layer (Issue #88)
Add SQLAlchemy models for the Agent Memory System:
- WorkingMemory: Key-value storage with TTL for active sessions
- Episode: Experiential memories from task executions
- Fact: Semantic knowledge triples with confidence scores
- Procedure: Learned skills and procedures with success tracking
- MemoryConsolidationLog: Tracks consolidation jobs between memory tiers

Create enums for memory system:
- ScopeType: global, project, agent_type, agent_instance, session
- EpisodeOutcome: success, failure, partial
- ConsolidationType: working_to_episodic, episodic_to_semantic, etc.
- ConsolidationStatus: pending, running, completed, failed

Add Alembic migration (0005) for all memory tables with:
- Foreign key relationships to projects, agent_instances, agent_types
- Comprehensive indexes for query patterns
- Unique constraints for key lookups and triple uniqueness
- Vector embedding column placeholders (Text fallback until pgvector enabled)

Fix timezone-naive datetime.now() in types.py TaskState (review feedback)

Includes 30 unit tests for models and enums.

Closes #88

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-05 01:37:58 +01:00

59 lines
1.6 KiB
Python

# app/models/memory/working_memory.py
"""
Working Memory database model.
Stores ephemeral key-value data for active sessions with TTL support.
Used as database backup when Redis is unavailable.
"""
from sqlalchemy import Column, DateTime, Enum, Index, String
from sqlalchemy.dialects.postgresql import JSONB
from app.models.base import Base, TimestampMixin, UUIDMixin
from .enums import ScopeType
class WorkingMemory(Base, UUIDMixin, TimestampMixin):
"""
Working memory storage table.
Provides database-backed working memory as fallback when
Redis is unavailable. Supports TTL-based expiration.
"""
__tablename__ = "working_memory"
# Scoping
scope_type: Column[ScopeType] = Column(
Enum(ScopeType),
nullable=False,
index=True,
)
scope_id = Column(String(255), nullable=False, index=True)
# Key-value storage
key = Column(String(255), nullable=False)
value = Column(JSONB, nullable=False)
# TTL support
expires_at = Column(DateTime(timezone=True), nullable=True, index=True)
__table_args__ = (
# Primary lookup: scope + key
Index(
"ix_working_memory_scope_key",
"scope_type",
"scope_id",
"key",
unique=True,
),
# For cleanup of expired entries
Index("ix_working_memory_expires", "expires_at"),
# For listing all keys in a scope
Index("ix_working_memory_scope_list", "scope_type", "scope_id"),
)
def __repr__(self) -> str:
return f"<WorkingMemory {self.scope_type.value}:{self.scope_id}:{self.key}>"