Skip to main content

do_memory_core/
constants.rs

1//! Global constants for memory-core
2//!
3//! This module centralizes all magic numbers and string constants used throughout
4//! the memory system, making them easier to maintain and configure.
5
6/// Default configuration values
7pub mod defaults {
8    use std::time::Duration;
9
10    // Cache and storage
11    pub const DEFAULT_CACHE_SIZE: usize = 1000;
12    pub const DEFAULT_CACHE_TTL_SECONDS: u64 = 3600; // 1 hour
13    pub const DEFAULT_POOL_SIZE: usize = 10;
14    pub const MAX_EPISODES_CACHE: usize = 10000;
15
16    // Batch processing
17    pub const DEFAULT_BATCH_SIZE: usize = 100;
18    pub const MAX_BATCH_SIZE: usize = 1000;
19    pub const MIN_BATCH_SIZE: usize = 10;
20
21    // Performance tuning
22    pub const DEFAULT_BUFFER_SIZE: usize = 1024;
23    pub const DEFAULT_QUEUE_SIZE: usize = 100;
24    pub const MAX_CONCURRENT_OPERATIONS: usize = 100;
25
26    // Timeouts
27    pub const DEFAULT_OPERATION_TIMEOUT: Duration = Duration::from_secs(30);
28    pub const DEFAULT_CONNECTION_TIMEOUT: Duration = Duration::from_secs(10);
29    pub const DEFAULT_QUERY_TIMEOUT: Duration = Duration::from_secs(5);
30
31    // Retry configuration
32    pub const DEFAULT_MAX_RETRIES: usize = 3;
33    pub const DEFAULT_RETRY_DELAY_MS: u64 = 100;
34    pub const DEFAULT_RETRY_BACKOFF_MULTIPLIER: f64 = 2.0;
35
36    // Pattern extraction
37    pub const MIN_PATTERN_OCCURRENCES: usize = 2;
38    pub const MIN_PATTERN_SUCCESS_RATE: f32 = 0.6;
39    pub const MAX_PATTERNS_PER_EPISODE: usize = 10;
40
41    // Embedding dimensions
42    pub const EMBEDDING_DIMENSION_384: usize = 384;
43    pub const EMBEDDING_DIMENSION_768: usize = 768;
44    pub const EMBEDDING_DIMENSION_1536: usize = 1536;
45
46    // Similarity thresholds
47    pub const DEFAULT_SIMILARITY_THRESHOLD: f32 = 0.7;
48    pub const MIN_SIMILARITY_THRESHOLD: f32 = 0.5;
49    pub const MAX_SIMILARITY_THRESHOLD: f32 = 1.0;
50
51    // Monitoring and metrics
52    pub const DEFAULT_HEALTH_CHECK_INTERVAL_SECONDS: u64 = 30;
53    pub const DEFAULT_METRICS_COLLECTION_INTERVAL_SECONDS: u64 = 60;
54
55    // Memory management
56    pub const DEFAULT_MAX_MEMORY_MB: usize = 512;
57    pub const WARNING_MEMORY_THRESHOLD: f32 = 0.8; // 80%
58}
59
60/// Error messages
61pub mod errors {
62    pub const EPISODE_NOT_FOUND: &str = "Episode not found";
63    pub const PATTERN_NOT_FOUND: &str = "Pattern not found";
64    pub const HEURISTIC_NOT_FOUND: &str = "Heuristic not found";
65    pub const INVALID_EPISODE_ID: &str = "Invalid episode ID format";
66    pub const INVALID_PATTERN_ID: &str = "Invalid pattern ID format";
67    pub const STORAGE_CONNECTION_FAILED: &str = "Failed to connect to storage backend";
68    pub const SERIALIZATION_FAILED: &str = "Failed to serialize data";
69    pub const DESERIALIZATION_FAILED: &str = "Failed to deserialize data";
70    pub const EMBEDDING_GENERATION_FAILED: &str = "Failed to generate embedding";
71    pub const CACHE_OPERATION_FAILED: &str = "Cache operation failed";
72}
73
74/// Log messages and prefixes
75pub mod logging {
76    pub const LOG_PREFIX_EPISODE: &str = "[EPISODE]";
77    pub const LOG_PREFIX_PATTERN: &str = "[PATTERN]";
78    pub const LOG_PREFIX_STORAGE: &str = "[STORAGE]";
79    pub const LOG_PREFIX_CACHE: &str = "[CACHE]";
80    pub const LOG_PREFIX_EMBEDDING: &str = "[EMBEDDING]";
81    pub const LOG_PREFIX_MONITOR: &str = "[MONITOR]";
82}
83
84/// File paths and extensions
85pub mod paths {
86    pub const DEFAULT_DATA_DIR: &str = "./data";
87    pub const DEFAULT_BACKUP_DIR: &str = "./backups";
88    pub const DEFAULT_LOG_DIR: &str = "./logs";
89    pub const DEFAULT_CACHE_DIR: &str = "./cache";
90
91    pub const DB_FILE_EXTENSION: &str = ".db";
92    pub const REDB_FILE_EXTENSION: &str = ".redb";
93    pub const LOG_FILE_EXTENSION: &str = ".log";
94    pub const BACKUP_FILE_EXTENSION: &str = ".backup";
95}
96
97/// Database table and column names
98pub mod db {
99    // Table names
100    pub const TABLE_EPISODES: &str = "episodes";
101    pub const TABLE_PATTERNS: &str = "patterns";
102    pub const TABLE_HEURISTICS: &str = "heuristics";
103    pub const TABLE_EMBEDDINGS: &str = "embeddings";
104    pub const TABLE_SUMMARIES: &str = "episode_summaries";
105    pub const TABLE_METRICS: &str = "agent_metrics";
106    pub const TABLE_EXECUTION_RECORDS: &str = "execution_records";
107    pub const TABLE_TASK_METRICS: &str = "task_metrics";
108
109    // Common column names
110    pub const COL_ID: &str = "id";
111    pub const COL_EPISODE_ID: &str = "episode_id";
112    pub const COL_PATTERN_ID: &str = "pattern_id";
113    pub const COL_CREATED_AT: &str = "created_at";
114    pub const COL_UPDATED_AT: &str = "updated_at";
115    pub const COL_DELETED_AT: &str = "deleted_at";
116}
117
118/// HTTP and API constants
119pub mod api {
120    pub const DEFAULT_API_TIMEOUT_SECONDS: u64 = 30;
121    pub const DEFAULT_MAX_RETRIES: usize = 3;
122    pub const DEFAULT_RATE_LIMIT_PER_MINUTE: usize = 60;
123
124    // User agent
125    pub const USER_AGENT: &str = concat!("memory-core/", env!("CARGO_PKG_VERSION"),);
126}
127
128/// Feature flags (for conditional compilation)
129#[allow(unexpected_cfgs)]
130pub mod features {
131    #[cfg(feature = "openai")]
132    pub const OPENAI_ENABLED: bool = true;
133    #[cfg(not(feature = "openai"))]
134    pub const OPENAI_ENABLED: bool = false;
135
136    #[cfg(feature = "cohere-embeddings")]
137    #[allow(unexpected_cfgs)]
138    pub const COHERE_ENABLED: bool = true;
139    #[cfg(not(feature = "cohere-embeddings"))]
140    #[allow(unexpected_cfgs)]
141    pub const COHERE_ENABLED: bool = false;
142
143    #[cfg(feature = "local-embeddings")]
144    pub const LOCAL_EMBEDDINGS_ENABLED: bool = true;
145    #[cfg(not(feature = "local-embeddings"))]
146    pub const LOCAL_EMBEDDINGS_ENABLED: bool = false;
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    #[allow(clippy::assertions_on_constants)]
155    fn test_default_values() {
156        assert!(defaults::DEFAULT_CACHE_SIZE > 0);
157        assert!(defaults::DEFAULT_BATCH_SIZE > 0);
158        assert!(defaults::MAX_BATCH_SIZE >= defaults::DEFAULT_BATCH_SIZE);
159        assert!(defaults::MIN_BATCH_SIZE <= defaults::DEFAULT_BATCH_SIZE);
160    }
161
162    #[test]
163    #[allow(clippy::assertions_on_constants)]
164    fn test_similarity_thresholds() {
165        assert!(defaults::MIN_SIMILARITY_THRESHOLD >= 0.0);
166        assert!(defaults::MAX_SIMILARITY_THRESHOLD <= 1.0);
167        assert!(defaults::DEFAULT_SIMILARITY_THRESHOLD >= defaults::MIN_SIMILARITY_THRESHOLD);
168        assert!(defaults::DEFAULT_SIMILARITY_THRESHOLD <= defaults::MAX_SIMILARITY_THRESHOLD);
169    }
170
171    #[test]
172    #[allow(clippy::const_is_empty)]
173    fn test_error_messages_not_empty() {
174        assert!(!errors::EPISODE_NOT_FOUND.is_empty());
175        assert!(!errors::PATTERN_NOT_FOUND.is_empty());
176        assert!(!errors::STORAGE_CONNECTION_FAILED.is_empty());
177    }
178}