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/// Default fast model for AI memory extraction.
55pub const DEFAULT_FAST_MODEL: &str = "claude-3-5-haiku-20241022";
56
57/// Default importance scores by category.
58/// Lower values allow for gradual importance growth through references.
59pub const DEFAULT_IMPORTANCE_DECISION: f64 = 75.0;
60pub const DEFAULT_IMPORTANCE_SOLUTION: f64 = 70.0;
61pub const DEFAULT_IMPORTANCE_PREF: f64 = 65.0;
62pub const DEFAULT_IMPORTANCE_FINDING: f64 = 55.0;
63pub const DEFAULT_IMPORTANCE_TECH: f64 = 45.0;
64pub const DEFAULT_IMPORTANCE_STRUCTURE: f64 = 35.0;
65
66/// AI memory detection mode.
67/// Controls whether AI is used for memory category detection.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
69pub enum AiDetectionMode {
70 /// Hybrid mode: rule-based detection, AI enriches when confidence is low (default).
71 #[default]
72 Auto,
73 /// Always use AI for memory detection (more accurate but slower).
74 Always,
75 /// Never use AI, only rule-based detection (fastest).
76 Never,
77}
78
79impl AiDetectionMode {
80 /// Parse from environment variable string.
81 pub fn from_env() -> Self {
82 match std::env::var("MEMORY_AI_DETECTION")
83 .unwrap_or_default()
84 .to_lowercase()
85 .as_str()
86 {
87 "always" | "true" | "1" => AiDetectionMode::Always,
88 "never" | "false" | "0" => AiDetectionMode::Never,
89 "auto" | "" => AiDetectionMode::Auto,
90 other => {
91 log::warn!(
92 "Unknown MEMORY_AI_DETECTION value: '{}', using 'auto'",
93 other
94 );
95 AiDetectionMode::Auto
96 }
97 }
98 }
99
100 /// Whether AI detection should be used.
101 pub fn should_use_ai(&self) -> bool {
102 match self {
103 AiDetectionMode::Always => true,
104 AiDetectionMode::Never => false,
105 AiDetectionMode::Auto => false, // Default to rule-based for speed
106 }
107 }
108
109 /// Whether AI detection should be used for given text length.
110 /// Longer texts benefit more from AI detection.
111 pub fn should_use_ai_for_text(&self, text_len: usize) -> bool {
112 match self {
113 AiDetectionMode::Always => true,
114 AiDetectionMode::Never => false,
115 AiDetectionMode::Auto => text_len > 500,
116 }
117 }
118}
119
120// ============================================================================
121// Memory Configuration
122// ============================================================================
123
124/// Configuration for the memory system.
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct MemoryConfig {
127 /// Maximum number of entries to keep.
128 pub max_entries: usize,
129 /// Minimum importance threshold to keep.
130 pub min_importance: f64,
131 /// Whether auto accumulation is enabled.
132 pub enabled: bool,
133 /// Days before time decay starts.
134 pub decay_start_days: i64,
135 /// Decay rate per period (0.0-1.0).
136 pub decay_rate: f64,
137 /// Importance increment per reference.
138 pub reference_increment: f64,
139 /// Maximum importance ceiling.
140 pub max_importance_ceiling: f64,
141}
142
143impl Default for MemoryConfig {
144 fn default() -> Self {
145 Self {
146 max_entries: 100,
147 min_importance: 30.0,
148 enabled: true,
149 decay_start_days: 30,
150 decay_rate: 0.5,
151 reference_increment: 1.0,
152 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
153 }
154 }
155}
156
157impl MemoryConfig {
158 /// Create a new config with custom max entries.
159 pub fn with_max_entries(max: usize) -> Self {
160 Self {
161 max_entries: max,
162 ..Self::default()
163 }
164 }
165
166 /// Create a minimal config for low-memory environments.
167 pub fn minimal() -> Self {
168 Self {
169 max_entries: 50,
170 min_importance: 50.0,
171 enabled: true,
172 decay_start_days: 14,
173 decay_rate: 0.6,
174 reference_increment: 1.0,
175 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
176 }
177 }
178
179 /// Create a config for long-term archival.
180 pub fn archival() -> Self {
181 Self {
182 max_entries: 500,
183 min_importance: 20.0,
184 enabled: true,
185 decay_start_days: 90,
186 decay_rate: 0.3,
187 reference_increment: 3.0,
188 max_importance_ceiling: MAX_IMPORTANCE_CEILING,
189 }
190 }
191}