matrixcode_core/memory/config.rs
1//! Memory system constants and configuration.
2
3use serde::{Deserialize, Serialize};
4
5// ============================================================================
6// Constants
7// ============================================================================
8
9/// Maximum importance score ceiling (entries cannot exceed this).
10pub const MAX_IMPORTANCE_CEILING: f64 = 100.0;
11
12/// Minimum content length for similarity check (to avoid short words matching everything).
13pub const MIN_SIMILARITY_LENGTH: usize = 10;
14
15/// Similarity threshold for considering entries as duplicates (0.0-1.0).
16/// Higher value (0.85) reduces duplicate detection false negatives.
17pub const SIMILARITY_THRESHOLD: f64 = 0.85;
18
19/// Similarity threshold for merging similar memories (0.0-1.0).
20/// Lower than duplicate threshold to allow semantic merging.
21pub const MERGE_SIMILARITY_THRESHOLD: f64 = 0.7;
22
23/// Minimum content length for memory detection (to avoid capturing too generic content).
24/// Increased to 20 to filter out short fragments.
25pub const MIN_MEMORY_CONTENT_LENGTH: usize = 20;
26
27/// Maximum entries to return from detection (to avoid overwhelming).
28pub const MAX_DETECTED_ENTRIES: usize = 5;
29
30/// Maximum length for memory content before truncation.
31pub const MAX_MEMORY_CONTENT_LENGTH: usize = 200;
32
33/// Maximum length for display (shorter for terminal readability).
34pub const MAX_DISPLAY_LENGTH: usize = 60;
35
36/// Topic overlap threshold for conflict detection.
37pub const CONFLICT_OVERLAY_THRESHOLD: f64 = 0.5;
38
39/// Lower topic overlap threshold when change signal is present.
40pub const CONFLICT_OVERLAY_THRESHOLD_WITH_SIGNAL: f64 = 0.3;
41
42/// Importance threshold for displaying star marker (⭐).
43pub const IMPORTANCE_STAR_THRESHOLD: f64 = 80.0;
44
45/// Weight for relevance in contextual summary (relevance vs importance trade-off).
46pub const CONTEXT_RELEVANCE_WEIGHT: f64 = 0.6;
47
48/// Weight for importance in contextual summary (1.0 - CONTEXT_RELEVANCE_WEIGHT).
49pub const CONTEXT_IMPORTANCE_WEIGHT: f64 = 0.4;
50
51/// Default model for cost-effective memory extraction.
52pub const DEFAULT_MEMORY_EXTRACTOR_MODEL: &str = "claude-3-5-haiku-20241022";
53
54
55
56/// Default fast model for AI memory extraction.
57pub const DEFAULT_FAST_MODEL: &str = "claude-3-5-haiku-20241022";
58
59/// Default importance scores by category.
60/// Lower values allow for gradual importance growth through references.
61pub const DEFAULT_IMPORTANCE_DECISION: f64 = 75.0;
62pub const DEFAULT_IMPORTANCE_SOLUTION: f64 = 70.0;
63pub const DEFAULT_IMPORTANCE_PREF: f64 = 65.0;
64pub const DEFAULT_IMPORTANCE_FINDING: f64 = 55.0;
65pub const DEFAULT_IMPORTANCE_TECH: f64 = 45.0;
66pub const DEFAULT_IMPORTANCE_STRUCTURE: f64 = 35.0;
67
68
69
70/// AI memory detection mode.
71/// Controls whether AI is used for memory category detection.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
73pub enum AiDetectionMode {
74 /// Hybrid mode: rule-based detection, AI enriches when confidence is low (default).
75 #[default]
76 Auto,
77 /// Always use AI for memory detection (more accurate but slower).
78 Always,
79 /// Never use AI, only rule-based detection (fastest).
80 Never,
81}
82
83impl AiDetectionMode {
84 /// Parse from environment variable string.
85 pub fn from_env() -> Self {
86 match std::env::var("MEMORY_AI_DETECTION")
87 .unwrap_or_default()
88 .to_lowercase()
89 .as_str()
90 {
91 "always" | "true" | "1" => AiDetectionMode::Always,
92 "never" | "false" | "0" => AiDetectionMode::Never,
93 "auto" | "" => AiDetectionMode::Auto,
94 other => {
95 log::warn!(
96 "Unknown MEMORY_AI_DETECTION value: '{}', using 'auto'",
97 other
98 );
99 AiDetectionMode::Auto
100 }
101 }
102 }
103
104 /// Whether AI detection should be used.
105 pub fn should_use_ai(&self) -> bool {
106 match self {
107 AiDetectionMode::Always => true,
108 AiDetectionMode::Never => false,
109 AiDetectionMode::Auto => false, // Default to rule-based for speed
110 }
111 }
112
113 /// Whether AI detection should be used for given text length.
114 /// Longer texts benefit more from AI detection.
115 pub fn should_use_ai_for_text(&self, text_len: usize) -> bool {
116 match self {
117 AiDetectionMode::Always => true,
118 AiDetectionMode::Never => false,
119 AiDetectionMode::Auto => text_len > 500,
120 }
121 }
122}
123
124// ============================================================================
125// Memory Configuration
126// ============================================================================
127
128/// Configuration for the memory system.
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub struct MemoryConfig {
131 /// Maximum number of entries to keep.
132 pub max_entries: usize,
133 /// Minimum importance threshold to keep.
134 pub min_importance: f64,
135 /// Whether auto accumulation is enabled.
136 pub enabled: bool,
137 /// Days before time decay starts.
138 pub decay_start_days: i64,
139 /// Decay rate per period (0.0-1.0).
140 pub decay_rate: f64,
141 /// Importance increment per reference.
142 pub reference_increment: f64,
143 /// Maximum importance ceiling.
144 pub max_importance_ceiling: f64,
145}
146
147impl Default for MemoryConfig {
148 fn default() -> Self {
149 Self {
150 max_entries: 100,
151 min_importance: 30.0,
152 enabled: true,
153 decay_start_days: 30,
154 decay_rate: 0.5,
155 reference_increment: 1.0,
156 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
157 }
158 }
159}
160
161impl MemoryConfig {
162 /// Create a new config with custom max entries.
163 pub fn with_max_entries(max: usize) -> Self {
164 Self {
165 max_entries: max,
166 ..Self::default()
167 }
168 }
169
170 /// Create a minimal config for low-memory environments.
171 pub fn minimal() -> Self {
172 Self {
173 max_entries: 50,
174 min_importance: 50.0,
175 enabled: true,
176 decay_start_days: 14,
177 decay_rate: 0.6,
178 reference_increment: 1.0,
179 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
180 }
181 }
182
183 /// Create a config for long-term archival.
184 pub fn archival() -> Self {
185 Self {
186 max_entries: 500,
187 min_importance: 20.0,
188 enabled: true,
189 decay_start_days: 90,
190 decay_rate: 0.3,
191 reference_increment: 3.0,
192 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
193 }
194 }
195}