1"""

2 Enhanced Memory Schema v2 - Memori-inspired categorization with DuckDB.

3

4 Combines Memori's superior categorization with session-mgmt's ONNX vector search.

5"""

6

7 from enum import Enum

8

9

10 class MemoryCategory(str, Enum):

11 """

12 Memory categories inspired by Memori's structured categorization.

13

14 These categories enable intelligent memory organization and retrieval.

15 """

16

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)

22

23

24 class MemoryTier(str, Enum):

25 """

26 Memory storage tiers for optimized retrieval.

27

28 Inspired by Memori's short-term/long-term memory architecture.

29 """

30

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

34

35

36# DuckDB schema migration SQL

37 SCHEMA_V2_SQL = """

38-- Enhanced conversations table with Memori-inspired categorization

39 CREATE 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)

43

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

48

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,

53

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',

60

61 -- Search optimization

62 searchable_content TEXT, -- For full-text fallback

63 reasoning TEXT -- Why this memory is important

64);

65

66-- Enhanced reflections table

67 CREATE TABLE IF NOT EXISTS reflections_v2 (

68 id TEXT PRIMARY KEY,

69 content TEXT NOT NULL,

70 embedding FLOAT[384],

71

72 -- Memori-inspired structure

73 category TEXT NOT NULL,

74 importance_score REAL DEFAULT 0.5,

75 memory_tier TEXT DEFAULT 'long_term',

76

77 -- Tags and relationships

78 tags TEXT[],

79 related_entities TEXT[],

80

81 -- Metadata

82 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

83 project TEXT,

84 namespace TEXT DEFAULT 'default',

85

86 -- Access tracking

87 access_count INTEGER DEFAULT 0,

88 last_accessed TIMESTAMP

89);

90

91-- Entity extraction table (Memori pattern)

92 CREATE 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);

101

102-- Entity relationships (Memori pattern)

103 CREATE 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);

113

114-- Short-term memory promotion tracking

115 CREATE 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);

125

126-- Access patterns tracking (for Conscious Agent)

127 CREATE 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);

134

135-- Indexes for performance

136 CREATE INDEX IF NOT EXISTS idx_conversations_category ON conversations_v2(category, namespace);

137 CREATE INDEX IF NOT EXISTS idx_conversations_tier ON conversations_v2(memory_tier, importance_score DESC);

138 CREATE INDEX IF NOT EXISTS idx_conversations_namespace ON conversations_v2(namespace, project);

139 CREATE INDEX IF NOT EXISTS idx_conversations_timestamp ON conversations_v2(timestamp DESC);

140 CREATE INDEX IF NOT EXISTS idx_conversations_access ON conversations_v2(last_accessed DESC);

141

142 CREATE INDEX IF NOT EXISTS idx_reflections_category ON reflections_v2(category, namespace);

143 CREATE INDEX IF NOT EXISTS idx_reflections_tier ON reflections_v2(memory_tier);

144

145 CREATE INDEX IF NOT EXISTS idx_entities_type ON memory_entities(entity_type, entity_value);

146 CREATE INDEX IF NOT EXISTS idx_entities_memory ON memory_entities(memory_id);

147

148 CREATE INDEX IF NOT EXISTS idx_relationships_from ON memory_relationships(from_entity_id);

149 CREATE INDEX IF NOT EXISTS idx_relationships_to ON memory_relationships(to_entity_id);

150

151 CREATE INDEX IF NOT EXISTS idx_access_log_memory ON memory_access_log(memory_id, timestamp DESC);

152

153-- Full-text search (fallback when ONNX unavailable)

154 CREATE INDEX IF NOT EXISTS idx_conversations_fts ON conversations_v2(searchable_content);

155"""

156

157# Migration from v1 to v2

158 MIGRATION_SQL = """

159-- Migrate existing conversations to v2 schema

160 INSERT INTO conversations_v2 (

161 id, content, embedding, category, memory_tier,

162 project, timestamp, searchable_content

163)

164 SELECT

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

177 FROM conversations

178 WHERE id NOT IN (SELECT id FROM conversations_v2);

179

180-- Migrate existing reflections

181 INSERT INTO reflections_v2 (

182 id, content, embedding, category, tags, timestamp, project

183)

184 SELECT

185 id,

186 content,

187 embedding,

188 'context' as category, -- Default category

189 tags,

190 timestamp,

191 NULL as project

192 FROM reflections

193 WHERE id NOT IN (SELECT id FROM reflections_v2);

194"""