Skip to main content

ai_agent/services/compact/
time_based_mc_config.rs

1// Source: ~/claudecode/openclaudecode/src/services/compact/timeBasedMCConfig.ts
2//! Time-based microcompact configuration.
3//!
4//! Triggered when gap since last assistant message exceeds threshold.
5//! Fetched from GrowthBook feature flag 'tengu_slate_heron' in TypeScript.
6
7/// Configuration for time-based microcompact
8#[derive(Debug, Clone)]
9pub struct TimeBasedMCConfig {
10    /// Whether time-based MC is enabled
11    pub enabled: bool,
12    /// Gap threshold in minutes (default: 60)
13    pub gap_threshold_minutes: u64,
14    /// Number of recent compactable tool results to keep (default: 5)
15    pub keep_recent: usize,
16}
17
18impl Default for TimeBasedMCConfig {
19    fn default() -> Self {
20        Self {
21            enabled: true,
22            gap_threshold_minutes: 60,
23            keep_recent: 5,
24        }
25    }
26}
27
28/// Get the time-based MC config.
29/// In TypeScript, this fetches from GrowthBook. For Rust, use defaults + env override.
30pub fn get_time_based_mc_config() -> TimeBasedMCConfig {
31    let mut config = TimeBasedMCConfig::default();
32
33    // Allow env override for gap threshold
34    if let Ok(val) = std::env::var("AI_MC_GAP_THRESHOLD_MINUTES") {
35        if let Ok(parsed) = val.parse::<u64>() {
36            if parsed > 0 {
37                config.gap_threshold_minutes = parsed;
38            }
39        }
40    }
41
42    // Allow env override for keep_recent
43    if let Ok(val) = std::env::var("AI_MC_KEEP_RECENT") {
44        if let Ok(parsed) = val.parse::<usize>() {
45            if parsed > 0 {
46                config.keep_recent = parsed;
47            }
48        }
49    }
50
51    config
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_default_config() {
60        let config = TimeBasedMCConfig::default();
61        assert!(config.enabled);
62        assert_eq!(config.gap_threshold_minutes, 60);
63        assert_eq!(config.keep_recent, 5);
64    }
65
66    #[test]
67    fn test_get_time_based_mc_config() {
68        let config = get_time_based_mc_config();
69        assert!(config.enabled);
70        assert!(config.gap_threshold_minutes > 0);
71        assert!(config.keep_recent > 0);
72    }
73}