Coverage for session_buddy / memory / schema_v2.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-04 00:43 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-04 00:43 -0800
1"""
2Enhanced Memory Schema v2 - Memori-inspired categorization with DuckDB.
4Combines Memori's superior categorization with session-mgmt's ONNX vector search.
5"""
7from enum import Enum
10class MemoryCategory(str, Enum):
11 """
12 Memory categories inspired by Memori's structured categorization.
14 These categories enable intelligent memory organization and retrieval.
15 """
17 FACTS = "facts" # Factual information (project names, tech stack)
18 PREFERENCES = "preferences" # User preferences (coding style, tools)
19 SKILLS = "skills" # User knowledge/expertise (languages, frameworks)
20 RULES = "rules" # Learned patterns/rules (workflows, best practices)
21 CONTEXT = "context" # Contextual information (current tasks, environment)
24class MemoryTier(str, Enum):
25 """
26 Memory storage tiers for optimized retrieval.
28 Inspired by Memori's short-term/long-term memory architecture.
29 """
31 WORKING = "working" # Active context for current session (highest priority)
32 SHORT_TERM = "short_term" # Recently accessed or promoted memories
33 LONG_TERM = "long_term" # All historical memories
36# DuckDB schema migration SQL
37SCHEMA_V2_SQL = """
38-- Enhanced conversations table with Memori-inspired categorization
39CREATE TABLE IF NOT EXISTS conversations_v2 (
40 id TEXT PRIMARY KEY,
41 content TEXT NOT NULL,
42 embedding FLOAT[384], -- ONNX vector (session-mgmt's superior approach)
44 -- Memori-inspired categorization
45 category TEXT NOT NULL, -- facts, preferences, skills, rules, context
46 subcategory TEXT,
47 importance_score REAL DEFAULT 0.5, -- 0.0-1.0
49 -- Memory tier management
50 memory_tier TEXT DEFAULT 'long_term', -- working, short_term, long_term
51 access_count INTEGER DEFAULT 0,
52 last_accessed TIMESTAMP,
54 -- Metadata
55 project TEXT,
56 namespace TEXT DEFAULT 'default',
57 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
58 session_id TEXT,
59 user_id TEXT DEFAULT 'default',
61 -- Search optimization
62 searchable_content TEXT, -- For full-text fallback
63 reasoning TEXT -- Why this memory is important
64);
66-- Enhanced reflections table
67CREATE TABLE IF NOT EXISTS reflections_v2 (
68 id TEXT PRIMARY KEY,
69 content TEXT NOT NULL,
70 embedding FLOAT[384],
72 -- Memori-inspired structure
73 category TEXT NOT NULL,
74 importance_score REAL DEFAULT 0.5,
75 memory_tier TEXT DEFAULT 'long_term',
77 -- Tags and relationships
78 tags TEXT[],
79 related_entities TEXT[],
81 -- Metadata
82 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
83 project TEXT,
84 namespace TEXT DEFAULT 'default',
86 -- Access tracking
87 access_count INTEGER DEFAULT 0,
88 last_accessed TIMESTAMP
89);
91-- Entity extraction table (Memori pattern)
92CREATE TABLE IF NOT EXISTS memory_entities (
93 id TEXT PRIMARY KEY,
94 memory_id TEXT NOT NULL,
95 entity_type TEXT NOT NULL, -- person, technology, file, concept
96 entity_value TEXT NOT NULL,
97 confidence REAL DEFAULT 1.0,
98 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
99 FOREIGN KEY (memory_id) REFERENCES conversations_v2(id)
100);
102-- Entity relationships (Memori pattern)
103CREATE TABLE IF NOT EXISTS memory_relationships (
104 id TEXT PRIMARY KEY,
105 from_entity_id TEXT NOT NULL,
106 to_entity_id TEXT NOT NULL,
107 relationship_type TEXT NOT NULL, -- uses, extends, references, related_to
108 strength REAL DEFAULT 1.0,
109 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
110 FOREIGN KEY (from_entity_id) REFERENCES memory_entities(id),
111 FOREIGN KEY (to_entity_id) REFERENCES memory_entities(id)
112);
114-- Short-term memory promotion tracking
115CREATE TABLE IF NOT EXISTS memory_promotions (
116 id TEXT PRIMARY KEY,
117 memory_id TEXT NOT NULL,
118 from_tier TEXT NOT NULL,
119 to_tier TEXT NOT NULL,
120 reason TEXT,
121 priority_score REAL,
122 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
123 FOREIGN KEY (memory_id) REFERENCES conversations_v2(id)
124);
126-- Access patterns tracking (for Conscious Agent)
127CREATE TABLE IF NOT EXISTS memory_access_log (
128 id TEXT PRIMARY KEY,
129 memory_id TEXT NOT NULL,
130 access_type TEXT, -- search, retrieve, promote, demote
131 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
132 FOREIGN KEY (memory_id) REFERENCES conversations_v2(id)
133);
135-- Indexes for performance
136CREATE INDEX IF NOT EXISTS idx_conversations_category ON conversations_v2(category, namespace);
137CREATE INDEX IF NOT EXISTS idx_conversations_tier ON conversations_v2(memory_tier, importance_score DESC);
138CREATE INDEX IF NOT EXISTS idx_conversations_namespace ON conversations_v2(namespace, project);
139CREATE INDEX IF NOT EXISTS idx_conversations_timestamp ON conversations_v2(timestamp DESC);
140CREATE INDEX IF NOT EXISTS idx_conversations_access ON conversations_v2(last_accessed DESC);
142CREATE INDEX IF NOT EXISTS idx_reflections_category ON reflections_v2(category, namespace);
143CREATE INDEX IF NOT EXISTS idx_reflections_tier ON reflections_v2(memory_tier);
145CREATE INDEX IF NOT EXISTS idx_entities_type ON memory_entities(entity_type, entity_value);
146CREATE INDEX IF NOT EXISTS idx_entities_memory ON memory_entities(memory_id);
148CREATE INDEX IF NOT EXISTS idx_relationships_from ON memory_relationships(from_entity_id);
149CREATE INDEX IF NOT EXISTS idx_relationships_to ON memory_relationships(to_entity_id);
151CREATE INDEX IF NOT EXISTS idx_access_log_memory ON memory_access_log(memory_id, timestamp DESC);
153-- Full-text search (fallback when ONNX unavailable)
154CREATE INDEX IF NOT EXISTS idx_conversations_fts ON conversations_v2(searchable_content);
155"""
157# Migration from v1 to v2
158MIGRATION_SQL = """
159-- Migrate existing conversations to v2 schema
160INSERT INTO conversations_v2 (
161 id, content, embedding, category, memory_tier,
162 project, timestamp, searchable_content
163)
164SELECT
165 id,
166 content,
167 embedding,
168 CASE
169 WHEN content LIKE '%prefer%' THEN 'preferences'
170 WHEN content LIKE '%error%' OR content LIKE '%bug%' THEN 'context'
171 ELSE 'facts'
172 END as category,
173 'long_term' as memory_tier,
174 project,
175 timestamp,
176 content as searchable_content
177FROM conversations
178WHERE id NOT IN (SELECT id FROM conversations_v2);
180-- Migrate existing reflections
181INSERT INTO reflections_v2 (
182 id, content, embedding, category, tags, timestamp, project
183)
184SELECT
185 id,
186 content,
187 embedding,
188 'context' as category, -- Default category
189 tags,
190 timestamp,
191 NULL as project
192FROM reflections
193WHERE id NOT IN (SELECT id FROM reflections_v2);
194"""