Skip to main content

vtcode_config/
optimization.rs

1//! Configuration for performance optimization features
2
3use serde::{Deserialize, Serialize};
4
5/// Configuration for all optimization features
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8pub struct OptimizationConfig {
9    /// Memory pool configuration
10    #[serde(default)]
11    pub memory_pool: MemoryPoolConfig,
12
13    /// Tool registry optimization settings
14    #[serde(default)]
15    pub tool_registry: ToolRegistryConfig,
16
17    /// Async pipeline configuration
18    #[serde(default)]
19    pub async_pipeline: AsyncPipelineConfig,
20
21    /// LLM client optimization settings
22    #[serde(default)]
23    pub llm_client: LLMClientConfig,
24
25    /// Agent execution optimization
26    #[serde(default)]
27    pub agent_execution: AgentExecutionConfig,
28
29    /// Performance profiling settings
30    #[serde(default)]
31    pub profiling: ProfilingConfig,
32
33    /// File read cache configuration
34    #[serde(default)]
35    pub file_read_cache: FileReadCacheConfig,
36
37    /// Read-only command result cache
38    #[serde(default)]
39    pub command_cache: CommandCacheConfig,
40}
41
42/// File read cache configuration
43#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct FileReadCacheConfig {
46    /// Enable file read caching
47    pub enabled: bool,
48
49    /// Minimum file size (bytes) before caching
50    pub min_size_bytes: usize,
51
52    /// Maximum cached file size (bytes)
53    pub max_size_bytes: usize,
54
55    /// Cache TTL in seconds
56    pub ttl_secs: u64,
57
58    /// Maximum number of cached entries
59    pub max_entries: usize,
60}
61
62/// Read-only command cache configuration
63#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct CommandCacheConfig {
66    /// Enable command caching
67    pub enabled: bool,
68
69    /// Cache TTL in milliseconds
70    pub ttl_ms: u64,
71
72    /// Maximum number of cached entries
73    pub max_entries: usize,
74
75    /// Allowlist of command prefixes eligible for caching
76    pub allowlist: Vec<String>,
77}
78
79/// Memory pool configuration
80#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct MemoryPoolConfig {
83    /// Enable memory pool (can be disabled for debugging)
84    pub enabled: bool,
85
86    /// Maximum number of strings to pool
87    pub max_string_pool_size: usize,
88
89    /// Maximum number of Values to pool
90    pub max_value_pool_size: usize,
91
92    /// Maximum number of Vec<String> to pool
93    pub max_vec_pool_size: usize,
94}
95
96/// Tool registry optimization configuration
97#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct ToolRegistryConfig {
100    /// Enable optimized registry
101    pub use_optimized_registry: bool,
102
103    /// Maximum concurrent tool executions
104    pub max_concurrent_tools: usize,
105
106    /// Hot cache size for frequently used tools
107    pub hot_cache_size: usize,
108
109    /// Tool execution timeout in seconds
110    pub default_timeout_secs: u64,
111}
112
113/// Async pipeline configuration
114#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct AsyncPipelineConfig {
117    /// Enable request batching
118    pub enable_batching: bool,
119
120    /// Enable result caching
121    pub enable_caching: bool,
122
123    /// Maximum batch size for tool requests
124    pub max_batch_size: usize,
125
126    /// Batch timeout in milliseconds
127    pub batch_timeout_ms: u64,
128
129    /// Result cache size
130    pub cache_size: usize,
131}
132
133/// LLM client optimization configuration
134#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct LLMClientConfig {
137    /// Enable connection pooling
138    pub enable_connection_pooling: bool,
139
140    /// Enable response caching
141    pub enable_response_caching: bool,
142
143    /// Enable request batching
144    pub enable_request_batching: bool,
145
146    /// Connection pool size
147    pub connection_pool_size: usize,
148
149    /// Response cache size
150    pub response_cache_size: usize,
151
152    /// Response cache TTL in seconds
153    pub cache_ttl_secs: u64,
154
155    /// Rate limit: requests per second
156    pub rate_limit_rps: f64,
157
158    /// Rate limit burst capacity
159    pub rate_limit_burst: usize,
160}
161
162/// Agent execution optimization configuration
163#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
164#[derive(Debug, Clone, Serialize, Deserialize)]
165pub struct AgentExecutionConfig {
166    /// Enable optimized agent execution loop
167    pub use_optimized_loop: bool,
168
169    /// Enable performance prediction
170    pub enable_performance_prediction: bool,
171
172    /// State transition history size
173    pub state_history_size: usize,
174
175    /// Resource monitoring interval in milliseconds
176    pub resource_monitor_interval_ms: u64,
177
178    /// Maximum memory usage in MB
179    pub max_memory_mb: u64,
180
181    /// Maximum execution time in seconds
182    pub max_execution_time_secs: u64,
183
184    /// Idle detection timeout in milliseconds (0 to disable)
185    /// When the agent is idle for this duration, it will enter a low-power state
186    pub idle_timeout_ms: u64,
187
188    /// Back-off duration in milliseconds when no work is pending
189    /// This reduces CPU usage during idle periods
190    pub idle_backoff_ms: u64,
191
192    /// Maximum consecutive idle cycles before entering deep sleep
193    pub max_idle_cycles: usize,
194}
195
196/// Performance profiling configuration
197#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
198#[derive(Debug, Clone, Serialize, Deserialize)]
199pub struct ProfilingConfig {
200    /// Enable performance profiling
201    pub enabled: bool,
202
203    /// Resource monitoring interval in milliseconds
204    pub monitor_interval_ms: u64,
205
206    /// Maximum benchmark history size
207    pub max_history_size: usize,
208
209    /// Auto-export results to file
210    pub auto_export_results: bool,
211
212    /// Export file path
213    pub export_file_path: String,
214
215    /// Enable regression testing
216    pub enable_regression_testing: bool,
217
218    /// Maximum allowed performance regression percentage
219    pub max_regression_percent: f64,
220}
221
222impl Default for MemoryPoolConfig {
223    fn default() -> Self {
224        Self {
225            enabled: true,
226            max_string_pool_size: 64,
227            max_value_pool_size: 32,
228            max_vec_pool_size: 16,
229        }
230    }
231}
232
233impl Default for FileReadCacheConfig {
234    fn default() -> Self {
235        Self {
236            enabled: true,
237            min_size_bytes: crate::constants::optimization::FILE_READ_CACHE_MIN_SIZE_BYTES,
238            max_size_bytes: crate::constants::optimization::FILE_READ_CACHE_MAX_SIZE_BYTES,
239            ttl_secs: crate::constants::optimization::FILE_READ_CACHE_TTL_SECS,
240            max_entries: crate::constants::optimization::FILE_READ_CACHE_MAX_ENTRIES,
241        }
242    }
243}
244
245impl Default for CommandCacheConfig {
246    fn default() -> Self {
247        Self {
248            enabled: true,
249            ttl_ms: crate::constants::optimization::COMMAND_CACHE_TTL_MS,
250            max_entries: crate::constants::optimization::COMMAND_CACHE_MAX_ENTRIES,
251            allowlist: crate::constants::optimization::COMMAND_CACHE_ALLOWLIST
252                .iter()
253                .map(|s| s.to_string())
254                .collect(),
255        }
256    }
257}
258
259impl Default for ToolRegistryConfig {
260    fn default() -> Self {
261        Self {
262            use_optimized_registry: true, // Enable by default for better performance
263            max_concurrent_tools: 4,
264            hot_cache_size: 16,
265            default_timeout_secs: 180,
266        }
267    }
268}
269
270impl Default for AsyncPipelineConfig {
271    fn default() -> Self {
272        Self {
273            enable_batching: false, // Conservative default
274            enable_caching: true,
275            max_batch_size: 5,
276            batch_timeout_ms: 100,
277            cache_size: 100,
278        }
279    }
280}
281
282impl Default for LLMClientConfig {
283    fn default() -> Self {
284        Self {
285            enable_connection_pooling: false, // Conservative default
286            enable_response_caching: true,
287            enable_request_batching: false, // Conservative default
288            connection_pool_size: 4,
289            response_cache_size: 50,
290            cache_ttl_secs: 300,
291            rate_limit_rps: 10.0,
292            rate_limit_burst: 20,
293        }
294    }
295}
296
297impl Default for AgentExecutionConfig {
298    fn default() -> Self {
299        Self {
300            use_optimized_loop: true, // Enable by default for better performance
301            enable_performance_prediction: false, // Conservative default
302            state_history_size: 1000,
303            resource_monitor_interval_ms: 100,
304            max_memory_mb: 1024,
305            max_execution_time_secs: 300,
306            idle_timeout_ms: 5000, // 5 seconds idle timeout
307            idle_backoff_ms: 100,  // 100ms backoff during idle
308            max_idle_cycles: 10,   // 10 consecutive idle cycles before deep sleep
309        }
310    }
311}
312
313impl Default for ProfilingConfig {
314    fn default() -> Self {
315        Self {
316            enabled: false, // Disabled by default to avoid overhead
317            monitor_interval_ms: 100,
318            max_history_size: 1000,
319            auto_export_results: false,
320            export_file_path: "benchmark_results.json".to_string(),
321            enable_regression_testing: false,
322            max_regression_percent: 10.0,
323        }
324    }
325}
326
327impl OptimizationConfig {
328    /// Get optimized configuration for development
329    pub fn development() -> Self {
330        Self {
331            memory_pool: MemoryPoolConfig {
332                enabled: true,
333                ..Default::default()
334            },
335            tool_registry: ToolRegistryConfig {
336                use_optimized_registry: true,
337                max_concurrent_tools: 2,
338                ..Default::default()
339            },
340            async_pipeline: AsyncPipelineConfig {
341                enable_batching: true,
342                enable_caching: true,
343                max_batch_size: 3,
344                ..Default::default()
345            },
346            llm_client: LLMClientConfig {
347                enable_connection_pooling: true,
348                enable_response_caching: true,
349                connection_pool_size: 2,
350                rate_limit_rps: 5.0,
351                ..Default::default()
352            },
353            agent_execution: AgentExecutionConfig {
354                use_optimized_loop: true,
355                enable_performance_prediction: false, // Disabled for dev
356                max_memory_mb: 512,
357                idle_timeout_ms: 2000, // Shorter idle timeout for development
358                idle_backoff_ms: 50,   // Shorter backoff for development
359                max_idle_cycles: 5,    // Fewer idle cycles for development
360                ..Default::default()
361            },
362            profiling: ProfilingConfig {
363                enabled: true, // Enabled for development
364                auto_export_results: true,
365                ..Default::default()
366            },
367            file_read_cache: FileReadCacheConfig::default(),
368            command_cache: CommandCacheConfig::default(),
369        }
370    }
371
372    /// Get optimized configuration for production
373    pub fn production() -> Self {
374        Self {
375            memory_pool: MemoryPoolConfig {
376                enabled: true,
377                max_string_pool_size: 128,
378                max_value_pool_size: 64,
379                max_vec_pool_size: 32,
380            },
381            tool_registry: ToolRegistryConfig {
382                use_optimized_registry: true,
383                max_concurrent_tools: 8,
384                hot_cache_size: 32,
385                default_timeout_secs: 300,
386            },
387            async_pipeline: AsyncPipelineConfig {
388                enable_batching: true,
389                enable_caching: true,
390                max_batch_size: 10,
391                batch_timeout_ms: 50,
392                cache_size: 200,
393            },
394            llm_client: LLMClientConfig {
395                enable_connection_pooling: true,
396                enable_response_caching: true,
397                enable_request_batching: true,
398                connection_pool_size: 8,
399                response_cache_size: 100,
400                cache_ttl_secs: 600,
401                rate_limit_rps: 20.0,
402                rate_limit_burst: 50,
403            },
404            agent_execution: AgentExecutionConfig {
405                use_optimized_loop: true,
406                enable_performance_prediction: true,
407                state_history_size: 2000,
408                resource_monitor_interval_ms: 50,
409                max_memory_mb: 2048,
410                max_execution_time_secs: 600,
411                idle_timeout_ms: 10000, // Longer idle timeout for production
412                idle_backoff_ms: 200,   // Longer backoff for production
413                max_idle_cycles: 20,    // More idle cycles for production
414            },
415            profiling: ProfilingConfig {
416                enabled: false, // Disabled in production unless needed
417                monitor_interval_ms: 1000,
418                max_history_size: 500,
419                auto_export_results: false,
420                export_file_path: "/var/log/vtcode/benchmark_results.json".to_string(),
421                enable_regression_testing: true,
422                max_regression_percent: 5.0,
423            },
424            file_read_cache: FileReadCacheConfig {
425                enabled: true,
426                min_size_bytes: crate::constants::optimization::FILE_READ_CACHE_PROD_MIN_SIZE_BYTES,
427                max_size_bytes: crate::constants::optimization::FILE_READ_CACHE_PROD_MAX_SIZE_BYTES,
428                ttl_secs: crate::constants::optimization::FILE_READ_CACHE_PROD_TTL_SECS,
429                max_entries: crate::constants::optimization::FILE_READ_CACHE_PROD_MAX_ENTRIES,
430            },
431            command_cache: CommandCacheConfig {
432                enabled: true,
433                ttl_ms: crate::constants::optimization::COMMAND_CACHE_PROD_TTL_MS,
434                max_entries: crate::constants::optimization::COMMAND_CACHE_PROD_MAX_ENTRIES,
435                allowlist: crate::constants::optimization::COMMAND_CACHE_PROD_ALLOWLIST
436                    .iter()
437                    .map(|s| s.to_string())
438                    .collect(),
439            },
440        }
441    }
442}