ccswarm 0.4.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Context Compaction System
//!
//! Provides intelligent context compression for sessions to manage token usage.
//! Inspired by Claude Agent SDK's compaction strategy.

use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

/// Configuration for context compaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionConfig {
    /// Maximum tokens before triggering compaction
    pub max_tokens: usize,
    /// Threshold ratio (0.0-1.0) to trigger automatic compaction
    pub threshold_ratio: f64,
    /// Strategy to use for compaction
    pub strategy: CompactionStrategy,
    /// Whether to preserve system messages
    pub preserve_system_messages: bool,
    /// Whether to preserve recent context
    pub preserve_recent_count: usize,
    /// Compression level for zstd (1-22)
    pub compression_level: i32,
}

impl Default for CompactionConfig {
    fn default() -> Self {
        Self {
            max_tokens: 200_000, // Claude's context limit
            threshold_ratio: 0.8,
            strategy: CompactionStrategy::SmartSummarize,
            preserve_system_messages: true,
            preserve_recent_count: 5,
            compression_level: 3,
        }
    }
}

/// Strategy for context compaction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompactionStrategy {
    /// Simple truncation of oldest messages
    Truncate,
    /// Summarize old context into a condensed form
    Summarize,
    /// Smart summarization with importance scoring
    SmartSummarize,
    /// Sliding window that keeps only recent context
    SlidingWindow,
    /// Hybrid approach combining multiple strategies
    Hybrid,
}

impl std::fmt::Display for CompactionStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompactionStrategy::Truncate => write!(f, "truncate"),
            CompactionStrategy::Summarize => write!(f, "summarize"),
            CompactionStrategy::SmartSummarize => write!(f, "smart_summarize"),
            CompactionStrategy::SlidingWindow => write!(f, "sliding_window"),
            CompactionStrategy::Hybrid => write!(f, "hybrid"),
        }
    }
}

/// Result of a compaction operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionResult {
    /// Whether compaction was performed
    pub compacted: bool,
    /// Original token count
    pub original_tokens: usize,
    /// Token count after compaction
    pub final_tokens: usize,
    /// Number of messages removed
    pub messages_removed: usize,
    /// Number of messages summarized
    pub messages_summarized: usize,
    /// Bytes saved through compression
    pub bytes_saved: usize,
    /// Compression ratio achieved (0.0-1.0)
    pub compression_ratio: f64,
    /// Strategy used
    pub strategy_used: CompactionStrategy,
    /// Generated summary (if applicable)
    pub summary: Option<String>,
}

impl CompactionResult {
    /// Create result for no compaction needed
    pub fn no_compaction(token_count: usize) -> Self {
        Self {
            compacted: false,
            original_tokens: token_count,
            final_tokens: token_count,
            messages_removed: 0,
            messages_summarized: 0,
            bytes_saved: 0,
            compression_ratio: 1.0,
            strategy_used: CompactionStrategy::Truncate,
            summary: None,
        }
    }

    /// Calculate savings percentage
    pub fn savings_percentage(&self) -> f64 {
        if self.original_tokens == 0 {
            return 0.0;
        }
        (1.0 - (self.final_tokens as f64 / self.original_tokens as f64)) * 100.0
    }
}

/// Trait for context compaction
#[async_trait]
pub trait ContextCompactor: Send + Sync {
    /// Compact the context using the configured strategy
    async fn compact(&mut self, config: &CompactionConfig) -> Result<CompactionResult>;

    /// Check if compaction is needed
    async fn needs_compaction(&self, threshold_tokens: usize) -> bool;

    /// Get current token count
    async fn get_token_count(&self) -> Result<usize>;

    /// Get context size in bytes
    async fn get_context_size(&self) -> Result<usize>;

    /// Clear all context
    async fn clear_context(&mut self) -> Result<()>;
}

/// Message for context history
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextMessage {
    /// Message role (system, user, assistant)
    pub role: String,
    /// Message content
    pub content: String,
    /// Approximate token count
    pub token_count: usize,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Importance score (0.0-1.0, higher = more important)
    pub importance: f64,
    /// Whether this message should be preserved
    pub preserve: bool,
}

impl ContextMessage {
    /// Create a new context message
    pub fn new(role: impl Into<String>, content: impl Into<String>) -> Self {
        let content_str: String = content.into();
        // Rough token estimation: ~4 chars per token
        let token_count = content_str.len() / 4;

        Self {
            role: role.into(),
            content: content_str,
            token_count,
            timestamp: chrono::Utc::now(),
            importance: 0.5,
            preserve: false,
        }
    }

    /// Set importance score
    pub fn with_importance(mut self, importance: f64) -> Self {
        self.importance = importance.clamp(0.0, 1.0);
        self
    }

    /// Mark as preserved
    pub fn preserve(mut self) -> Self {
        self.preserve = true;
        self
    }
}

/// Context history manager
#[derive(Debug, Clone, Default)]
pub struct ContextHistory {
    /// Messages in the context
    messages: Vec<ContextMessage>,
    /// Total token count
    total_tokens: usize,
}

impl ContextHistory {
    /// Create a new empty history
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a message
    pub fn add_message(&mut self, message: ContextMessage) {
        self.total_tokens += message.token_count;
        self.messages.push(message);
    }

    /// Get total token count
    pub fn token_count(&self) -> usize {
        self.total_tokens
    }

    /// Get message count
    pub fn message_count(&self) -> usize {
        self.messages.len()
    }

    /// Get all messages
    pub fn messages(&self) -> &[ContextMessage] {
        &self.messages
    }

    /// Clear all messages
    pub fn clear(&mut self) {
        self.messages.clear();
        self.total_tokens = 0;
    }

    /// Apply truncation strategy
    pub fn apply_truncation(&mut self, target_tokens: usize, preserve_recent: usize) -> usize {
        if self.total_tokens <= target_tokens {
            return 0;
        }

        let mut removed = 0;
        let preserve_from = self.messages.len().saturating_sub(preserve_recent);

        // Remove oldest non-preserved messages until we're under target
        while self.total_tokens > target_tokens && !self.messages.is_empty() {
            // Find the first removable message (not preserved, not in recent window)
            let remove_idx = self.messages.iter().enumerate().find_map(|(i, m)| {
                if i < preserve_from && !m.preserve {
                    Some(i)
                } else {
                    None
                }
            });

            match remove_idx {
                Some(idx) => {
                    let msg = self.messages.remove(idx);
                    self.total_tokens = self.total_tokens.saturating_sub(msg.token_count);
                    removed += 1;
                }
                None => break, // No more removable messages
            }
        }

        removed
    }

    /// Apply sliding window strategy
    pub fn apply_sliding_window(&mut self, window_size: usize) -> usize {
        if self.messages.len() <= window_size {
            return 0;
        }

        let to_remove = self.messages.len() - window_size;

        // Separate preserved and non-preserved
        let (preserved, removable): (Vec<_>, Vec<_>) =
            self.messages.drain(..to_remove).partition(|m| m.preserve);

        // Put preserved messages back at the start
        let preserved_tokens: usize = preserved.iter().map(|m| m.token_count).sum();
        let removed_tokens: usize = removable.iter().map(|m| m.token_count).sum();

        // Prepend preserved messages
        for msg in preserved.into_iter().rev() {
            self.messages.insert(0, msg);
        }

        self.total_tokens = self.total_tokens.saturating_sub(removed_tokens);
        self.total_tokens += preserved_tokens; // Add back preserved tokens

        removable.len()
    }

    /// Score messages by importance for smart summarization
    pub fn score_importance(&mut self) {
        let message_count = self.messages.len();
        for (i, msg) in self.messages.iter_mut().enumerate() {
            let mut score = 0.5;

            // System messages are important
            if msg.role == "system" {
                score += 0.3;
            }

            // Recent messages are more important
            let recency = i as f64 / message_count.max(1) as f64;
            score += recency * 0.2;

            // Longer messages might be more substantive
            if msg.content.len() > 500 {
                score += 0.1;
            }

            // Messages with code blocks might be important
            if msg.content.contains("```") {
                score += 0.1;
            }

            // Messages with errors are important
            if msg.content.to_lowercase().contains("error") {
                score += 0.1;
            }

            msg.importance = score.clamp(0.0, 1.0);
        }
    }
}

/// Compress context data using zstd
pub fn compress_context(data: &[u8], level: i32) -> Result<Vec<u8>> {
    // Simple wrapper - in production would use zstd crate
    // For now, return data as-is if compression isn't available
    let _ = level;
    Ok(data.to_vec())
}

/// Decompress context data
pub fn decompress_context(data: &[u8]) -> Result<Vec<u8>> {
    // Simple wrapper - in production would use zstd crate
    Ok(data.to_vec())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_context_message_creation() {
        let msg = ContextMessage::new("user", "Hello, world!")
            .with_importance(0.8)
            .preserve();

        assert_eq!(msg.role, "user");
        assert_eq!(msg.importance, 0.8);
        assert!(msg.preserve);
    }

    #[test]
    fn test_context_history() {
        let mut history = ContextHistory::new();

        history.add_message(ContextMessage::new("user", "Hello"));
        history.add_message(ContextMessage::new("assistant", "Hi there!"));

        assert_eq!(history.message_count(), 2);
        assert!(history.token_count() > 0);
    }

    #[test]
    fn test_truncation() {
        let mut history = ContextHistory::new();

        // Add 10 messages with longer content to exceed token limit
        for i in 0..10 {
            // Each message is ~50 chars = ~12 tokens
            let content = format!("This is a longer message number {} with more content", i);
            history.add_message(ContextMessage::new("user", content));
        }

        let original_count = history.message_count();
        let original_tokens = history.token_count();
        // Set target to 50 tokens, which is less than our ~120 tokens
        let removed = history.apply_truncation(50, 3);

        // We should have removed some messages since we exceeded target
        assert!(
            removed > 0,
            "Expected to remove messages, original_tokens={}, target=50",
            original_tokens
        );
        assert!(history.message_count() < original_count);
    }

    #[test]
    fn test_sliding_window() {
        let mut history = ContextHistory::new();

        for i in 0..10 {
            history.add_message(ContextMessage::new("user", format!("Message {}", i)));
        }

        let removed = history.apply_sliding_window(5);
        assert_eq!(removed, 5);
        assert_eq!(history.message_count(), 5);
    }

    #[test]
    fn test_compaction_result_savings() {
        let result = CompactionResult {
            compacted: true,
            original_tokens: 100_000,
            final_tokens: 7_000,
            messages_removed: 50,
            messages_summarized: 10,
            bytes_saved: 500_000,
            compression_ratio: 0.07,
            strategy_used: CompactionStrategy::SmartSummarize,
            summary: Some("Summary of conversation".to_string()),
        };

        assert_eq!(result.savings_percentage(), 93.0);
    }
}