Skip to main content

pawan/
compaction.rs

1//! Structured conversation compaction for context overflow handling
2//!
3//! When conversation history exceeds context limits, this module provides
4//! tools to compact the history while preserving key information like:
5//! - User's original intent and requirements
6//! - Important decisions made
7//! - Code changes and their rationale
8//! - Error messages and debugging information
9
10use crate::agent::{Message, Role};
11use serde::{Deserialize, Serialize};
12
13/// Compaction strategy for preserving different types of information
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct CompactionStrategy {
16    /// Keep the most recent N messages (default: 10)
17    pub keep_recent: usize,
18    /// Keep messages with specific keywords (e.g., "error", "fix", "bug")
19    pub keep_keywords: Vec<String>,
20    /// Keep tool call results (default: true)
21    pub keep_tool_results: bool,
22    /// Keep system messages (default: true)
23    pub keep_system: bool,
24}
25
26impl Default for CompactionStrategy {
27    fn default() -> Self {
28        Self {
29            keep_recent: 10,
30            keep_keywords: vec![
31                "error".to_string(),
32                "fix".to_string(),
33                "bug".to_string(),
34                "issue".to_string(),
35                "problem".to_string(),
36                "solution".to_string(),
37                "important".to_string(),
38                "note".to_string(),
39                "warning".to_string(),
40            ],
41            keep_tool_results: true,
42            keep_system: true,
43        }
44    }
45}
46
47/// Compaction result with statistics
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct CompactionResult {
50    /// The compacted messages
51    pub messages: Vec<Message>,
52    /// Number of messages before compaction
53    pub original_count: usize,
54    /// Number of messages after compaction
55    pub compacted_count: usize,
56    /// Estimated tokens saved
57    pub tokens_saved: usize,
58}
59
60/// Build a structured compaction prompt for the LLM
61///
62/// This prompt instructs the LLM to create a structured summary that preserves
63/// essential information while reducing token count. The output format is
64/// designed to be machine-readable and easily parsed.
65pub fn build_compaction_prompt(messages: &[Message], _strategy: &CompactionStrategy) -> String {
66    let mut prompt = String::from(
67        r#"# Structured Conversation Compaction
68
69You are tasked with compacting a conversation history while preserving all essential information.
70
71## Your Goal
72
73Create a concise, structured summary that captures:
741. **User's Original Intent** - What the user wanted to accomplish
752. **Important Decisions** - Key decisions made during the conversation
763. **Code Changes** - What was changed and why
774. **Error Messages** - Any errors encountered and their solutions
785. **Debugging Information** - Important debugging steps and findings
796. **Warnings and Notes** - Any warnings or important notes
80
81## Output Format
82
83Your response MUST follow this exact structure:
84
85```
86# Conversation Summary
87
88## User Intent
89[Describe what the user wanted to accomplish in 1-2 sentences]
90
91## Key Decisions
92- [Decision 1]
93- [Decision 2]
94- [Decision 3]
95
96## Code Changes
97### File: [filename]
98- **Change**: [description of change]
99- **Rationale**: [why this change was made]
100- **Impact**: [what this affects]
101
102### File: [filename]
103- **Change**: [description of change]
104- **Rationale**: [why this change was made]
105- **Impact**: [what this affects]
106
107## Errors and Solutions
108### Error: [error description]
109- **Location**: [where the error occurred]
110- **Solution**: [how it was fixed]
111- **Prevention**: [how to prevent this in the future]
112
113## Debugging Steps
1141. [Step 1]
1152. [Step 2]
1163. [Step 3]
117
118## Warnings and Notes
119- [Warning or note 1]
120- [Warning or note 2]
121
122## Current State
123[Describe the current state of the work in 1-2 sentences]
124
125## Next Steps
1261. [Next step 1]
1272. [Next step 2]
1283. [Next step 3]
129```
130
131## Guidelines
132
133- Be concise but complete
134- Preserve all technical details (function names, file paths, error messages)
135- Use bullet points for lists
136- Keep each section focused and clear
137- If a section has no relevant information, write "None"
138- Maintain chronological order where relevant
139- Include specific values (numbers, strings, paths) when important
140
141## Original Conversation
142
143"#,
144    );
145
146    // Add messages to the prompt with clear section markers
147    for (i, msg) in messages.iter().enumerate() {
148        let role = match msg.role {
149            Role::System => "SYSTEM",
150            Role::User => "USER",
151            Role::Assistant => "ASSISTANT",
152            Role::Tool => "TOOL",
153        };
154        prompt.push_str(&format!(
155            "\n### Message {} [{}]\n\n{}\n",
156            i + 1,
157            role,
158            msg.content
159        ));
160
161        // Add tool call information if present
162        if !msg.tool_calls.is_empty() {
163            prompt.push_str("\n**Tool Calls:**\n");
164            for tc in &msg.tool_calls {
165                prompt.push_str(&format!("- `{}`: {}\n", tc.name, tc.arguments));
166            }
167        }
168
169        // Add tool result if present
170        if let Some(ref result) = msg.tool_result {
171            prompt.push_str(&format!("\n**Tool Result:**\n{}\n", result.content));
172        }
173    }
174
175    prompt.push_str(
176        r#"
177
178--- End of Original Conversation ---
179
180Please provide a structured summary following the exact format specified above.
181"#,
182    );
183
184    prompt
185}
186
187/// Compact messages based on the given strategy
188pub fn compact_messages(messages: Vec<Message>, strategy: &CompactionStrategy) -> CompactionResult {
189    let original_count = messages.len();
190    let mut compacted = Vec::new();
191
192    // Always keep system messages if enabled
193    if strategy.keep_system {
194        compacted.extend(messages.iter().filter(|m| m.role == Role::System).cloned());
195    }
196
197    // Keep messages with keywords
198    for msg in &messages {
199        let content_lower = msg.content.to_lowercase();
200        if strategy
201            .keep_keywords
202            .iter()
203            .any(|kw| content_lower.contains(&kw.to_lowercase()))
204            && !compacted.iter().any(|m| m.content == msg.content)
205        {
206            compacted.push(msg.clone());
207        }
208    }
209
210    // Keep tool results if enabled
211    if strategy.keep_tool_results {
212        for msg in &messages {
213            if msg.tool_result.is_some()
214                && !msg.tool_calls.is_empty()
215                && !compacted.iter().any(|m| m.content == msg.content)
216            {
217                compacted.push(msg.clone());
218            }
219        }
220    }
221
222    // Keep the most recent messages
223    let recent_start = if messages.len() > strategy.keep_recent {
224        messages.len() - strategy.keep_recent
225    } else {
226        0
227    };
228
229    for msg in &messages[recent_start..] {
230        if !compacted.iter().any(|m| m.content == msg.content) {
231            compacted.push(msg.clone());
232        }
233    }
234
235    // Sort by original order (approximate)
236    compacted.sort_by_key(|m| {
237        messages
238            .iter()
239            .position(|orig| orig.content == m.content)
240            .unwrap_or(usize::MAX)
241    });
242
243    let compacted_count = compacted.len();
244    let tokens_saved = estimate_tokens_saved(original_count, compacted_count);
245
246    CompactionResult {
247        messages: compacted,
248        original_count,
249        compacted_count,
250        tokens_saved,
251    }
252}
253
254/// Estimate tokens saved by compaction (rough approximation)
255fn estimate_tokens_saved(original: usize, compacted: usize) -> usize {
256    // Assume average of 4 tokens per message
257    let avg_tokens_per_message = 4;
258    (original - compacted) * avg_tokens_per_message
259}
260
261/// Parse a structured compaction summary
262///
263/// This function parses the structured output from the LLM and extracts
264/// the different sections into a structured format.
265#[derive(Debug, Clone, Serialize, Deserialize)]
266pub struct ParsedCompactionSummary {
267    /// User's original intent
268    pub user_intent: String,
269    /// Key decisions made
270    pub key_decisions: Vec<String>,
271    /// Code changes made
272    pub code_changes: Vec<CodeChange>,
273    /// Errors encountered and their solutions
274    pub errors_and_solutions: Vec<ErrorSolution>,
275    /// Debugging steps taken
276    pub debugging_steps: Vec<String>,
277    /// Warnings and notes
278    pub warnings_and_notes: Vec<String>,
279    /// Current state of the work
280    pub current_state: String,
281    /// Next steps to take
282    pub next_steps: Vec<String>,
283}
284
285/// A code change with metadata
286#[derive(Debug, Clone, Serialize, Deserialize)]
287pub struct CodeChange {
288    /// File that was changed
289    pub file: String,
290    /// Description of the change
291    pub change: String,
292    /// Rationale for the change
293    pub rationale: String,
294    /// Impact of the change
295    pub impact: String,
296}
297
298/// An error and its solution
299#[derive(Debug, Clone, Serialize, Deserialize)]
300pub struct ErrorSolution {
301    /// Error description
302    pub error: String,
303    /// Location of the error
304    pub location: String,
305    /// How it was fixed
306    pub solution: String,
307    /// How to prevent this in the future
308    pub prevention: String,
309}
310
311/// Parse a structured compaction summary from LLM output
312///
313/// This is a simple parser that extracts sections from the structured format.
314/// It's designed to be robust to minor variations in formatting.
315pub fn parse_compaction_summary(summary: &str) -> Result<ParsedCompactionSummary, String> {
316    let mut parsed = ParsedCompactionSummary {
317        user_intent: String::new(),
318        key_decisions: Vec::new(),
319        code_changes: Vec::new(),
320        errors_and_solutions: Vec::new(),
321        debugging_steps: Vec::new(),
322        warnings_and_notes: Vec::new(),
323        current_state: String::new(),
324        next_steps: Vec::new(),
325    };
326
327    let lines: Vec<&str> = summary.lines().collect();
328    let mut current_section: Option<String> = None;
329    let mut current_code_change: Option<CodeChange> = None;
330    let mut current_error: Option<ErrorSolution> = None;
331
332    for line in lines {
333        let line = line.trim();
334
335        // Detect section headers
336        if let Some(rest) = line.strip_prefix("## ") {
337            current_section = Some(rest.to_string());
338            continue;
339        }
340
341        // Detect subsection headers (###)
342        if let Some(subsection) = line.strip_prefix("### ") {
343            let subsection = subsection.to_string();
344
345            // If we were building a code change, save it
346            if let Some(code_change) = current_code_change.take() {
347                if !code_change.file.is_empty() {
348                    parsed.code_changes.push(code_change);
349                }
350            }
351
352            // If we were building an error solution, save it
353            if let Some(error) = current_error.take() {
354                if !error.error.is_empty() {
355                    parsed.errors_and_solutions.push(error);
356                }
357            }
358
359            // Start new code change or error
360            if let Some(file) = subsection.strip_prefix("File: ") {
361                current_code_change = Some(CodeChange {
362                    file: file.to_string(),
363                    change: String::new(),
364                    rationale: String::new(),
365                    impact: String::new(),
366                });
367            } else if let Some(error) = subsection.strip_prefix("Error: ") {
368                current_error = Some(ErrorSolution {
369                    error: error.to_string(),
370                    location: String::new(),
371                    solution: String::new(),
372                    prevention: String::new(),
373                });
374            }
375
376            continue;
377        }
378
379        // Process content based on current section
380        match current_section.as_deref() {
381            Some("User Intent") => {
382                parsed.user_intent.push_str(line);
383                parsed.user_intent.push(' ');
384            }
385            Some("Key Decisions") => {
386                if let Some(item) = line.strip_prefix("- ") {
387                    parsed.key_decisions.push(item.to_string());
388                }
389            }
390            Some("Code Changes") => {
391                if let Some(ref mut code_change) = current_code_change {
392                    if let Some(rest) = line.strip_prefix("- **Change**: ") {
393                        code_change.change = rest.to_string();
394                    } else if let Some(rest) = line.strip_prefix("- **Rationale**: ") {
395                        code_change.rationale = rest.to_string();
396                    } else if let Some(rest) = line.strip_prefix("- **Impact**: ") {
397                        code_change.impact = rest.to_string();
398                    }
399                }
400            }
401            Some("Errors and Solutions") => {
402                if let Some(ref mut error) = current_error {
403                    if let Some(rest) = line.strip_prefix("- **Location**: ") {
404                        error.location = rest.to_string();
405                    } else if let Some(rest) = line.strip_prefix("- **Solution**: ") {
406                        error.solution = rest.to_string();
407                    } else if let Some(rest) = line.strip_prefix("- **Prevention**: ") {
408                        error.prevention = rest.to_string();
409                    }
410                }
411            }
412            Some("Debugging Steps") => {
413                if let Some(rest) = line
414                    .strip_prefix("1. ")
415                    .or_else(|| line.strip_prefix("2. "))
416                    .or_else(|| line.strip_prefix("3. "))
417                {
418                    parsed.debugging_steps.push(rest.to_string());
419                }
420            }
421            Some("Warnings and Notes") => {
422                if let Some(item) = line.strip_prefix("- ") {
423                    parsed.warnings_and_notes.push(item.to_string());
424                }
425            }
426            Some("Current State") => {
427                parsed.current_state.push_str(line);
428                parsed.current_state.push(' ');
429            }
430            Some("Next Steps") => {
431                if let Some(rest) = line
432                    .strip_prefix("1. ")
433                    .or_else(|| line.strip_prefix("2. "))
434                    .or_else(|| line.strip_prefix("3. "))
435                {
436                    parsed.next_steps.push(rest.to_string());
437                }
438            }
439            _ => {}
440        }
441    }
442
443    // Save any remaining code change or error
444    if let Some(code_change) = current_code_change {
445        if !code_change.file.is_empty() {
446            parsed.code_changes.push(code_change);
447        }
448    }
449    if let Some(error) = current_error {
450        if !error.error.is_empty() {
451            parsed.errors_and_solutions.push(error);
452        }
453    }
454
455    // Trim whitespace
456    parsed.user_intent = parsed.user_intent.trim().to_string();
457    parsed.current_state = parsed.current_state.trim().to_string();
458
459    Ok(parsed)
460}
461
462/// Convert a parsed compaction summary back to a message
463///
464/// This function converts the structured summary back into a message
465/// that can be added to the conversation history.
466pub fn summary_to_message(summary: &ParsedCompactionSummary) -> Message {
467    let mut content = String::from("# Conversation Summary\n\n");
468
469    content.push_str("## User Intent\n");
470    content.push_str(&summary.user_intent);
471    content.push_str("\n\n");
472
473    if !summary.key_decisions.is_empty() {
474        content.push_str("## Key Decisions\n");
475        for decision in &summary.key_decisions {
476            content.push_str("- ");
477            content.push_str(decision);
478            content.push('\n');
479        }
480        content.push('\n');
481    }
482
483    if !summary.code_changes.is_empty() {
484        content.push_str("## Code Changes\n");
485        for change in &summary.code_changes {
486            content.push_str(&format!("### File: {}\n", change.file));
487            content.push_str(&format!("- **Change**: {}\n", change.change));
488            content.push_str(&format!("- **Rationale**: {}\n", change.rationale));
489            content.push_str(&format!("- **Impact**: {}\n", change.impact));
490            content.push('\n');
491        }
492    }
493
494    if !summary.errors_and_solutions.is_empty() {
495        content.push_str("## Errors and Solutions\n");
496        for error in &summary.errors_and_solutions {
497            content.push_str(&format!("### Error: {}\n", error.error));
498            content.push_str(&format!("- **Location**: {}\n", error.location));
499            content.push_str(&format!("- **Solution**: {}\n", error.solution));
500            content.push_str(&format!("- **Prevention**: {}\n", error.prevention));
501            content.push('\n');
502        }
503    }
504
505    if !summary.debugging_steps.is_empty() {
506        content.push_str("## Debugging Steps\n");
507        for (i, step) in summary.debugging_steps.iter().enumerate() {
508            content.push_str(&format!("{}. {}\n", i + 1, step));
509        }
510        content.push('\n');
511    }
512
513    if !summary.warnings_and_notes.is_empty() {
514        content.push_str("## Warnings and Notes\n");
515        for warning in &summary.warnings_and_notes {
516            content.push_str("- ");
517            content.push_str(warning);
518            content.push('\n');
519        }
520        content.push('\n');
521    }
522
523    content.push_str("## Current State\n");
524    content.push_str(&summary.current_state);
525    content.push_str("\n\n");
526
527    if !summary.next_steps.is_empty() {
528        content.push_str("## Next Steps\n");
529        for (i, step) in summary.next_steps.iter().enumerate() {
530            content.push_str(&format!("{}. {}\n", i + 1, step));
531        }
532    }
533
534    Message {
535        role: Role::System,
536        content,
537        tool_calls: vec![],
538        tool_result: None,
539    }
540}
541
542#[cfg(test)]
543mod tests {
544    use super::*;
545
546    #[test]
547    fn test_compaction_strategy_default() {
548        let strategy = CompactionStrategy::default();
549        assert_eq!(strategy.keep_recent, 10);
550        assert!(strategy.keep_keywords.contains(&"error".to_string()));
551        assert!(strategy.keep_tool_results);
552        assert!(strategy.keep_system);
553    }
554
555    #[test]
556    fn test_build_compaction_prompt() {
557        let messages = vec![
558            Message {
559                role: Role::User,
560                content: "Fix the bug in main.rs".to_string(),
561                tool_calls: vec![],
562                tool_result: None,
563            },
564            Message {
565                role: Role::Assistant,
566                content: "I'll read the file first.".to_string(),
567                tool_calls: vec![],
568                tool_result: None,
569            },
570        ];
571
572        let prompt = build_compaction_prompt(&messages, &CompactionStrategy::default());
573        assert!(prompt.contains("Fix the bug in main.rs"));
574        assert!(prompt.contains("I'll read the file first."));
575        assert!(prompt.contains("Original Conversation"));
576    }
577
578    #[test]
579    fn test_compact_messages() {
580        let messages = vec![
581            Message {
582                role: Role::System,
583                content: "You are a coding agent.".to_string(),
584                tool_calls: vec![],
585                tool_result: None,
586            },
587            Message {
588                role: Role::User,
589                content: "Fix the error".to_string(),
590                tool_calls: vec![],
591                tool_result: None,
592            },
593            Message {
594                role: Role::Assistant,
595                content: "I'll help.".to_string(),
596                tool_calls: vec![],
597                tool_result: None,
598            },
599        ];
600
601        // Use a strategy that will actually drop some messages
602        let strategy = CompactionStrategy {
603            keep_recent: 1,
604            keep_keywords: vec![],
605            keep_tool_results: false,
606            keep_system: false,
607        };
608        let result = compact_messages(messages, &strategy);
609
610        assert_eq!(result.original_count, 3);
611        // With keep_recent=1, only the most recent message (Assistant) is kept
612        // System and User are dropped because keep_system=false and keep_keywords=[]
613        assert!(result.compacted_count > 0);
614        assert!(result.tokens_saved > 0);
615    }
616
617    #[test]
618    fn test_compaction_preserves_system_messages() {
619        let messages = vec![
620            Message {
621                role: Role::System,
622                content: "System prompt".to_string(),
623                tool_calls: vec![],
624                tool_result: None,
625            },
626            Message {
627                role: Role::User,
628                content: "User message".to_string(),
629                tool_calls: vec![],
630                tool_result: None,
631            },
632        ];
633
634        let strategy = CompactionStrategy {
635            keep_system: true,
636            ..Default::default()
637        };
638
639        let result = compact_messages(messages, &strategy);
640        assert!(result
641            .messages
642            .iter()
643            .any(|m| m.role == Role::System && m.content == "System prompt"));
644    }
645
646    #[test]
647    fn test_compaction_preserves_keyword_messages() {
648        let messages = vec![
649            Message {
650                role: Role::User,
651                content: "Fix the error".to_string(),
652                tool_calls: vec![],
653                tool_result: None,
654            },
655            Message {
656                role: Role::User,
657                content: "Regular message".to_string(),
658                tool_calls: vec![],
659                tool_result: None,
660            },
661        ];
662
663        let strategy = CompactionStrategy {
664            keep_keywords: vec!["error".to_string()],
665            ..Default::default()
666        };
667
668        let result = compact_messages(messages, &strategy);
669        assert!(result.messages.iter().any(|m| m.content == "Fix the error"));
670    }
671}