infiniloom_engine/
constants.rs

1//! Centralized constants for Infiniloom
2//!
3//! All magic numbers and configuration defaults are defined here
4//! for easy maintenance and consistency across the codebase.
5
6/// Budget-related constants
7pub mod budget {
8    /// Default token budget for repository processing
9    pub const DEFAULT_BUDGET: u32 = 100_000;
10
11    /// Token reserve for headers, metadata, and overhead
12    pub const OVERHEAD_RESERVE: u32 = 1_000;
13
14    /// Minimum tokens needed to include a file partially
15    pub const MIN_PARTIAL_FIT_TOKENS: u32 = 100;
16
17    /// Default token budget for repository maps
18    pub const DEFAULT_MAP_BUDGET: u32 = 2_000;
19
20    /// Default chunk size in tokens
21    pub const DEFAULT_CHUNK_SIZE: u32 = 8_000;
22}
23
24/// Compression-related constants
25pub mod compression {
26    /// Compression ratio for minimal level (keeps 90%)
27    pub const MINIMAL_RATIO: f64 = 0.90;
28
29    /// Compression ratio for balanced level (keeps 70%)
30    pub const BALANCED_RATIO: f64 = 0.70;
31
32    /// Compression ratio for aggressive level (keeps 50%)
33    pub const AGGRESSIVE_RATIO: f64 = 0.50;
34
35    /// Compression ratio for extreme level (keeps 30%)
36    pub const EXTREME_RATIO: f64 = 0.30;
37
38    /// Compression ratio for focused level (keeps 25%)
39    pub const FOCUSED_RATIO: f64 = 0.25;
40
41    /// Default semantic compression budget ratio
42    pub const SEMANTIC_BUDGET_RATIO: f64 = 0.50;
43}
44
45/// Timeout constants (in seconds)
46pub mod timeouts {
47    /// Timeout for git operations
48    pub const GIT_OPERATION_SECS: u64 = 30;
49
50    /// Timeout for remote clone operations
51    pub const REMOTE_CLONE_SECS: u64 = 300;
52
53    /// Timeout for parsing operations
54    pub const PARSE_TIMEOUT_SECS: u64 = 60;
55}
56
57/// File processing constants
58pub mod files {
59    /// Maximum file size to process (10 MB)
60    pub const MAX_FILE_SIZE_BYTES: u64 = 10 * 1024 * 1024;
61
62    /// Number of bytes to check for binary detection
63    pub const BINARY_CHECK_BYTES: usize = 8192;
64
65    /// Default importance score for new files
66    pub const DEFAULT_IMPORTANCE: f32 = 0.5;
67
68    /// Maximum line length before truncation
69    pub const MAX_LINE_LENGTH: usize = 2000;
70
71    /// Number of signature lines to extract
72    pub const SIGNATURE_LINES: usize = 3;
73}
74
75/// PageRank constants
76pub mod pagerank {
77    /// Damping factor for PageRank algorithm
78    pub const DAMPING_FACTOR: f64 = 0.85;
79
80    /// Maximum iterations for PageRank convergence
81    pub const MAX_ITERATIONS: usize = 100;
82
83    /// Convergence threshold for PageRank
84    pub const CONVERGENCE_THRESHOLD: f64 = 1e-6;
85}
86
87/// Security scanning constants
88pub mod security {
89    /// Minimum entropy threshold for secret detection
90    pub const MIN_ENTROPY_THRESHOLD: f64 = 3.5;
91
92    /// Maximum length for secret patterns
93    pub const MAX_SECRET_LENGTH: usize = 500;
94}
95
96/// Index-related constants
97pub mod index {
98    /// Default context depth for diff operations
99    pub const DEFAULT_CONTEXT_DEPTH: u8 = 2;
100
101    /// Maximum context depth allowed
102    pub const MAX_CONTEXT_DEPTH: u8 = 3;
103
104    /// Default symbol ID when unknown
105    pub const UNKNOWN_SYMBOL_ID: u32 = 0;
106}
107
108/// Repository map generation constants
109pub mod repomap {
110    /// Estimated tokens per symbol entry in the repository map
111    /// Includes name, kind, file, line, signature (~25 tokens avg)
112    pub const TOKENS_PER_SYMBOL: u32 = 25;
113
114    /// Estimated tokens per file entry in the file index
115    /// Includes path, tokens, importance (~10 tokens avg)
116    pub const TOKENS_PER_FILE: u32 = 10;
117
118    /// Fixed token overhead for headers, summary, and formatting
119    pub const TOKEN_OVERHEAD: u32 = 100;
120
121    /// Divisor for computing max symbols from budget (includes safety margin)
122    /// Formula: max_symbols = budget / BUDGET_SYMBOL_DIVISOR
123    pub const BUDGET_SYMBOL_DIVISOR: usize = 20;
124
125    /// Maximum length for symbol summaries before truncation
126    pub const SUMMARY_MAX_LEN: usize = 120;
127
128    /// Minimum symbols to include regardless of budget
129    pub const MIN_SYMBOLS: usize = 5;
130
131    /// Maximum symbols to include regardless of budget
132    pub const MAX_SYMBOLS: usize = 500;
133}
134
135/// Parser constants
136pub mod parser {
137    /// Maximum number of symbols to extract per file
138    pub const MAX_SYMBOLS_PER_FILE: usize = 10_000;
139
140    /// Maximum recursion depth for AST traversal
141    pub const MAX_RECURSION_DEPTH: usize = 100;
142
143    /// Maximum query result size
144    pub const MAX_QUERY_MATCHES: usize = 50_000;
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    // These tests verify compile-time constants have sensible values.
152    // The assertions are intentionally on constants to document invariants.
153    #[allow(clippy::assertions_on_constants)]
154    #[test]
155    fn test_compression_ratios_are_valid() {
156        assert!(compression::MINIMAL_RATIO > 0.0 && compression::MINIMAL_RATIO <= 1.0);
157        assert!(compression::BALANCED_RATIO > 0.0 && compression::BALANCED_RATIO <= 1.0);
158        assert!(compression::AGGRESSIVE_RATIO > 0.0 && compression::AGGRESSIVE_RATIO <= 1.0);
159        assert!(compression::EXTREME_RATIO > 0.0 && compression::EXTREME_RATIO <= 1.0);
160        assert!(compression::FOCUSED_RATIO > 0.0 && compression::FOCUSED_RATIO <= 1.0);
161    }
162
163    #[allow(clippy::assertions_on_constants)]
164    #[test]
165    fn test_compression_ratios_ordering() {
166        // Minimal should keep more than balanced, etc.
167        assert!(compression::MINIMAL_RATIO > compression::BALANCED_RATIO);
168        assert!(compression::BALANCED_RATIO > compression::AGGRESSIVE_RATIO);
169        assert!(compression::AGGRESSIVE_RATIO > compression::EXTREME_RATIO);
170    }
171
172    #[allow(clippy::assertions_on_constants)]
173    #[test]
174    fn test_budget_defaults_are_reasonable() {
175        assert!(budget::DEFAULT_BUDGET > budget::OVERHEAD_RESERVE);
176        assert!(budget::DEFAULT_MAP_BUDGET > 0);
177        assert!(budget::DEFAULT_CHUNK_SIZE > 0);
178    }
179
180    #[allow(clippy::assertions_on_constants)]
181    #[test]
182    fn test_pagerank_constants_are_valid() {
183        assert!(pagerank::DAMPING_FACTOR > 0.0 && pagerank::DAMPING_FACTOR < 1.0);
184        assert!(pagerank::MAX_ITERATIONS > 0);
185        assert!(pagerank::CONVERGENCE_THRESHOLD > 0.0);
186    }
187
188    #[allow(clippy::assertions_on_constants)]
189    #[test]
190    fn test_timeouts_are_reasonable() {
191        assert!(timeouts::GIT_OPERATION_SECS > 0);
192        assert!(timeouts::REMOTE_CLONE_SECS > timeouts::GIT_OPERATION_SECS);
193    }
194}