syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Conversation history management with forge-style compaction support
//!
//! This module provides conversation history storage with intelligent compaction
//! inspired by forge's context management approach:
//! - Configurable thresholds (tokens, turns, messages)
//! - Smart eviction strategy (protects tool-call/result adjacency)
//! - Droppable message support for ephemeral content
//! - Summary frame generation for compressed history

use super::compact::{
    CompactConfig, CompactThresholds, CompactionStrategy, ContextSummary, SummaryFrame,
};
use rig::completion::Message;
use serde::{Deserialize, Serialize};

/// Rough token estimate: ~4 characters per token
const CHARS_PER_TOKEN: usize = 4;

/// A conversation turn containing user input and assistant response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationTurn {
    pub user_message: String,
    pub assistant_response: String,
    /// Tool calls made during this turn (for context preservation)
    pub tool_calls: Vec<ToolCallRecord>,
    /// Estimated token count for this turn
    pub estimated_tokens: usize,
    /// Whether this turn can be dropped entirely (ephemeral content)
    /// Droppable turns are typically file reads or directory listings
    /// that can be re-fetched if needed
    #[serde(default)]
    pub droppable: bool,
}

/// Record of a tool call for history tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallRecord {
    pub tool_name: String,
    pub args_summary: String,
    pub result_summary: String,
    /// Tool call ID for proper message pairing (optional for backwards compat)
    #[serde(default)]
    pub tool_id: Option<String>,
    /// Whether this tool result is droppable (ephemeral content like file reads)
    #[serde(default)]
    pub droppable: bool,
}

/// Conversation history manager with forge-style compaction support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationHistory {
    /// Full conversation turns
    turns: Vec<ConversationTurn>,
    /// Compressed summary using SummaryFrame (if any)
    summary_frame: Option<SummaryFrame>,
    /// Total estimated tokens in history
    total_tokens: usize,
    /// Compaction configuration
    compact_config: CompactConfig,
    /// Number of user turns (for threshold checking)
    user_turn_count: usize,
    /// Context summary for tracking file operations and decisions
    context_summary: ContextSummary,
}

impl Default for ConversationHistory {
    fn default() -> Self {
        Self::new()
    }
}

impl ConversationHistory {
    pub fn new() -> Self {
        Self {
            turns: Vec::new(),
            summary_frame: None,
            total_tokens: 0,
            compact_config: CompactConfig::default(),
            user_turn_count: 0,
            context_summary: ContextSummary::new(),
        }
    }

    /// Create with custom compaction configuration
    pub fn with_config(config: CompactConfig) -> Self {
        Self {
            turns: Vec::new(),
            summary_frame: None,
            total_tokens: 0,
            compact_config: config,
            user_turn_count: 0,
            context_summary: ContextSummary::new(),
        }
    }

    /// Create with aggressive compaction for limited context windows
    pub fn aggressive() -> Self {
        Self::with_config(CompactConfig {
            retention_window: 5,
            eviction_window: 0.7,
            thresholds: CompactThresholds::aggressive(),
        })
    }

    /// Create with relaxed compaction for large context windows
    pub fn relaxed() -> Self {
        Self::with_config(CompactConfig {
            retention_window: 20,
            eviction_window: 0.5,
            thresholds: CompactThresholds::relaxed(),
        })
    }

    /// Estimate tokens in a string (~4 characters per token)
    /// Public so it can be used for pre-request context size estimation
    pub fn estimate_tokens(text: &str) -> usize {
        text.len() / CHARS_PER_TOKEN
    }

    /// Add a new conversation turn
    pub fn add_turn(
        &mut self,
        user_message: String,
        assistant_response: String,
        tool_calls: Vec<ToolCallRecord>,
    ) {
        // Determine if this turn is droppable based on tool calls
        // Turns that only read files or list directories are droppable
        let droppable = !tool_calls.is_empty()
            && tool_calls.iter().all(|tc| {
                matches!(
                    tc.tool_name.as_str(),
                    "read_file" | "list_directory" | "analyze_project"
                )
            });

        let turn_tokens = Self::estimate_tokens(&user_message)
            + Self::estimate_tokens(&assistant_response)
            + tool_calls
                .iter()
                .map(|tc| {
                    Self::estimate_tokens(&tc.tool_name)
                        + Self::estimate_tokens(&tc.args_summary)
                        + Self::estimate_tokens(&tc.result_summary)
                })
                .sum::<usize>();

        self.turns.push(ConversationTurn {
            user_message,
            assistant_response,
            tool_calls,
            estimated_tokens: turn_tokens,
            droppable,
        });
        self.total_tokens += turn_tokens;
        self.user_turn_count += 1;
    }

    /// Add a turn with explicit droppable flag
    pub fn add_turn_droppable(
        &mut self,
        user_message: String,
        assistant_response: String,
        tool_calls: Vec<ToolCallRecord>,
        droppable: bool,
    ) {
        let turn_tokens = Self::estimate_tokens(&user_message)
            + Self::estimate_tokens(&assistant_response)
            + tool_calls
                .iter()
                .map(|tc| {
                    Self::estimate_tokens(&tc.tool_name)
                        + Self::estimate_tokens(&tc.args_summary)
                        + Self::estimate_tokens(&tc.result_summary)
                })
                .sum::<usize>();

        self.turns.push(ConversationTurn {
            user_message,
            assistant_response,
            tool_calls,
            estimated_tokens: turn_tokens,
            droppable,
        });
        self.total_tokens += turn_tokens;
        self.user_turn_count += 1;
    }

    /// Check if compaction is needed using forge-style thresholds
    pub fn needs_compaction(&self) -> bool {
        let last_is_user = self
            .turns
            .last()
            .map(|t| !t.user_message.is_empty())
            .unwrap_or(false);

        self.compact_config.should_compact(
            self.total_tokens,
            self.user_turn_count,
            self.turns.len(),
            last_is_user,
        )
    }

    /// Get the reason for compaction (for logging)
    pub fn compaction_reason(&self) -> Option<String> {
        self.compact_config.compaction_reason(
            self.total_tokens,
            self.user_turn_count,
            self.turns.len(),
        )
    }

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

    /// Get number of turns
    pub fn turn_count(&self) -> usize {
        self.turns.len()
    }

    /// Get number of user turns
    pub fn user_turn_count(&self) -> usize {
        self.user_turn_count
    }

    /// Clear all history
    pub fn clear(&mut self) {
        self.turns.clear();
        self.summary_frame = None;
        self.total_tokens = 0;
        self.user_turn_count = 0;
        self.context_summary = ContextSummary::new();
    }

    /// Clear turns but preserve the summary frame (for sync with truncated raw_chat_history)
    ///
    /// Use this instead of clear() when raw_chat_history is truncated but we want to
    /// preserve the accumulated context from prior compaction.
    pub fn clear_turns_preserve_context(&mut self) {
        // First compact any remaining turns into the summary
        if self.turns.len() > 1 {
            let _ = self.compact();
        }

        // Now clear turns but keep summary_frame and context_summary
        self.turns.clear();

        // Recalculate tokens (just summary frame now)
        self.total_tokens = self
            .summary_frame
            .as_ref()
            .map(|f| f.token_count)
            .unwrap_or(0);

        // User turn count stays as-is for statistics
    }

    /// Perform forge-style compaction with smart eviction
    /// Returns the summary that was created (for logging/display)
    pub fn compact(&mut self) -> Option<String> {
        use super::compact::strategy::{MessageMeta, MessageRole};
        use super::compact::summary::{
            ToolCallSummary, TurnSummary, extract_assistant_action, extract_user_intent,
        };

        if self.turns.len() < 2 {
            return None; // Nothing to compact
        }

        // Build message metadata for strategy
        let messages: Vec<MessageMeta> = self
            .turns
            .iter()
            .enumerate()
            .flat_map(|(turn_idx, turn)| {
                let mut metas = vec![];

                // User message
                metas.push(MessageMeta {
                    index: turn_idx * 2,
                    role: MessageRole::User,
                    droppable: turn.droppable,
                    has_tool_call: false,
                    is_tool_result: false,
                    tool_id: None,
                    token_count: Self::estimate_tokens(&turn.user_message),
                });

                // Assistant message (may have tool calls)
                let has_tool_call = !turn.tool_calls.is_empty();
                let tool_id = turn.tool_calls.first().and_then(|tc| tc.tool_id.clone());

                metas.push(MessageMeta {
                    index: turn_idx * 2 + 1,
                    role: MessageRole::Assistant,
                    droppable: turn.droppable,
                    has_tool_call,
                    is_tool_result: false,
                    tool_id,
                    token_count: Self::estimate_tokens(&turn.assistant_response),
                });

                metas
            })
            .collect();

        // Use default strategy (evict 60% or retain 10, whichever is more conservative)
        let strategy = CompactionStrategy::default();

        // Calculate eviction range with tool-call safety
        let range =
            strategy.calculate_eviction_range(&messages, self.compact_config.retention_window)?;

        if range.is_empty() {
            return None;
        }

        // Convert message indices to turn indices
        let start_turn = range.start / 2;
        let end_turn = range.end.div_ceil(2);

        if start_turn >= end_turn || end_turn > self.turns.len() {
            return None;
        }

        // Build context summary from turns to evict
        let mut new_context = ContextSummary::new();

        for (i, turn) in self.turns[start_turn..end_turn].iter().enumerate() {
            let turn_summary = TurnSummary {
                turn_number: start_turn + i + 1,
                user_intent: extract_user_intent(&turn.user_message, 80),
                assistant_action: extract_assistant_action(&turn.assistant_response, 100),
                tool_calls: turn
                    .tool_calls
                    .iter()
                    .map(|tc| ToolCallSummary {
                        tool_name: tc.tool_name.clone(),
                        args_summary: tc.args_summary.clone(),
                        result_summary: truncate_text(&tc.result_summary, 100),
                        success: !tc.result_summary.to_lowercase().contains("error"),
                    })
                    .collect(),
                key_decisions: vec![], // Could extract from assistant response
            };
            new_context.add_turn(turn_summary);
        }

        // Merge with existing context summary
        self.context_summary.merge(new_context);

        // Generate summary frame
        let new_frame = SummaryFrame::from_summary(&self.context_summary);

        // Merge with existing frame if present
        if let Some(existing) = &self.summary_frame {
            let merged_content = format!("{}\n\n{}", existing.content, new_frame.content);
            let merged_tokens = existing.token_count + new_frame.token_count;
            self.summary_frame = Some(SummaryFrame {
                content: merged_content,
                token_count: merged_tokens,
            });
        } else {
            self.summary_frame = Some(new_frame);
        }

        // Keep only recent turns (non-evicted)
        let preserved_turns: Vec<_> = self.turns[end_turn..].to_vec();
        let evicted_count = end_turn - start_turn;
        self.turns = preserved_turns;

        // Recalculate token count
        self.total_tokens = self
            .summary_frame
            .as_ref()
            .map(|f| f.token_count)
            .unwrap_or(0)
            + self.turns.iter().map(|t| t.estimated_tokens).sum::<usize>();

        Some(format!(
            "Compacted {} turns ({} → {} tokens)",
            evicted_count,
            self.total_tokens + evicted_count * 500, // rough estimate of evicted tokens
            self.total_tokens
        ))
    }

    /// Emergency compaction - more aggressive than normal
    /// Used when "input too long" error occurs and we need to reduce context urgently.
    /// Temporarily switches to aggressive config, compacts, then restores original.
    pub fn emergency_compact(&mut self) -> Option<String> {
        // Switch to aggressive config temporarily
        let original_config = self.compact_config.clone();
        self.compact_config = CompactConfig {
            retention_window: 3,  // Keep only 3 most recent turns
            eviction_window: 0.9, // Evict 90% of context
            thresholds: CompactThresholds::aggressive(),
        };

        let result = self.compact();

        // Restore original config
        self.compact_config = original_config;
        result
    }

    /// Convert history to Rig Message format for the agent
    /// Uses structured summary frames to preserve context
    pub fn to_messages(&self) -> Vec<Message> {
        use rig::OneOrMany;
        use rig::completion::message::{AssistantContent, Text, UserContent};

        let mut messages = Vec::new();

        // Add summary frame as initial context if present
        if let Some(frame) = &self.summary_frame {
            // Add as a user message with the summary, followed by acknowledgment
            messages.push(Message::User {
                content: OneOrMany::one(UserContent::Text(Text {
                    text: format!("[Previous conversation context]\n{}", frame.content),
                })),
            });
            messages.push(Message::Assistant {
                id: None,
                content: OneOrMany::one(AssistantContent::Text(Text {
                    text:
                        "I understand the previous context. I'll continue from where we left off."
                            .to_string(),
                })),
            });
        }

        // Add recent turns with tool call context as text
        for turn in &self.turns {
            // User message
            messages.push(Message::User {
                content: OneOrMany::one(UserContent::Text(Text {
                    text: turn.user_message.clone(),
                })),
            });

            // Build assistant response that includes tool call context
            let mut response_text = String::new();

            // If there were tool calls, include them as text context
            if !turn.tool_calls.is_empty() {
                response_text.push_str("[Tools used in this turn:\n");
                for tc in &turn.tool_calls {
                    response_text.push_str(&format!(
                        "  - {}({}) → {}\n",
                        tc.tool_name,
                        truncate_text(&tc.args_summary, 50),
                        truncate_text(&tc.result_summary, 100)
                    ));
                }
                response_text.push_str("]\n\n");
            }

            // Add the actual response
            response_text.push_str(&turn.assistant_response);

            messages.push(Message::Assistant {
                id: None,
                content: OneOrMany::one(AssistantContent::Text(Text {
                    text: response_text,
                })),
            });
        }

        messages
    }

    /// Check if there's any history
    pub fn is_empty(&self) -> bool {
        self.turns.is_empty() && self.summary_frame.is_none()
    }

    /// Get a brief status string for display
    pub fn status(&self) -> String {
        let compressed_info = if self.summary_frame.is_some() {
            format!(" (+{} compacted)", self.context_summary.turns_compacted)
        } else {
            String::new()
        };
        format!(
            "{} turns, ~{} tokens{}",
            self.turns.len(),
            self.total_tokens,
            compressed_info
        )
    }

    /// Get files that have been read during this session
    pub fn files_read(&self) -> impl Iterator<Item = &str> {
        self.context_summary.files_read.iter().map(|s| s.as_str())
    }

    /// Get files that have been written during this session
    pub fn files_written(&self) -> impl Iterator<Item = &str> {
        self.context_summary
            .files_written
            .iter()
            .map(|s| s.as_str())
    }

    /// Serialize to JSON for session persistence
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Deserialize from JSON (for session restore)
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

/// Helper to truncate text with ellipsis
fn truncate_text(text: &str, max_len: usize) -> String {
    if text.len() <= max_len {
        text.to_string()
    } else {
        format!("{}...", &text[..max_len.saturating_sub(3)])
    }
}

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

    #[test]
    fn test_add_turn() {
        let mut history = ConversationHistory::new();
        history.add_turn("Hello".to_string(), "Hi there!".to_string(), vec![]);
        assert_eq!(history.turn_count(), 1);
        assert!(!history.is_empty());
    }

    #[test]
    fn test_droppable_detection() {
        let mut history = ConversationHistory::new();

        // Turn with only read_file should be droppable
        history.add_turn(
            "Read the file".to_string(),
            "Here's the content".to_string(),
            vec![ToolCallRecord {
                tool_name: "read_file".to_string(),
                args_summary: "src/main.rs".to_string(),
                result_summary: "file content...".to_string(),
                tool_id: Some("tool_1".to_string()),
                droppable: true,
            }],
        );
        assert!(history.turns[0].droppable);

        // Turn with write_file should NOT be droppable
        history.add_turn(
            "Write the file".to_string(),
            "Done".to_string(),
            vec![ToolCallRecord {
                tool_name: "write_file".to_string(),
                args_summary: "src/new.rs".to_string(),
                result_summary: "success".to_string(),
                tool_id: Some("tool_2".to_string()),
                droppable: false,
            }],
        );
        assert!(!history.turns[1].droppable);
    }

    #[test]
    fn test_compaction() {
        // Use aggressive config for easier testing
        let mut history = ConversationHistory::with_config(CompactConfig {
            retention_window: 2,
            eviction_window: 0.6,
            thresholds: CompactThresholds {
                token_threshold: Some(500),
                turn_threshold: Some(5),
                message_threshold: Some(10),
                on_turn_end: None,
            },
        });

        // Add many turns to trigger compaction
        for i in 0..10 {
            history.add_turn(
                format!("Question {} with lots of text to increase token count", i),
                format!(
                    "Answer {} with lots of detail to increase token count even more",
                    i
                ),
                vec![ToolCallRecord {
                    tool_name: "analyze".to_string(),
                    args_summary: "path: .".to_string(),
                    result_summary: "Found rust project with many files".to_string(),
                    tool_id: Some(format!("tool_{}", i)),
                    droppable: false,
                }],
            );
        }

        if history.needs_compaction() {
            let summary = history.compact();
            assert!(summary.is_some());
            assert!(history.turn_count() < 10);
            assert!(history.summary_frame.is_some());
        }
    }

    #[test]
    fn test_to_messages() {
        let mut history = ConversationHistory::new();
        history.add_turn(
            "What is this project?".to_string(),
            "This is a Rust CLI tool.".to_string(),
            vec![],
        );

        let messages = history.to_messages();
        assert_eq!(messages.len(), 2); // 1 user + 1 assistant
    }

    #[test]
    fn test_clear() {
        let mut history = ConversationHistory::new();
        history.add_turn("Test".to_string(), "Response".to_string(), vec![]);
        history.clear();
        assert!(history.is_empty());
        assert_eq!(history.token_count(), 0);
    }

    #[test]
    fn test_compaction_reason() {
        let mut history = ConversationHistory::with_config(CompactConfig {
            retention_window: 2,
            eviction_window: 0.6,
            thresholds: CompactThresholds {
                token_threshold: Some(100),
                turn_threshold: Some(3),
                message_threshold: Some(5),
                on_turn_end: None,
            },
        });

        // Add turns to exceed threshold
        for i in 0..5 {
            history.add_turn(format!("Question {}", i), format!("Answer {}", i), vec![]);
        }

        assert!(history.needs_compaction());
        let reason = history.compaction_reason();
        assert!(reason.is_some());
    }

    #[test]
    fn test_clear_turns_preserve_context() {
        // Create history with aggressive compaction to trigger summary
        let mut history = ConversationHistory::with_config(CompactConfig {
            retention_window: 2,
            eviction_window: 0.6,
            thresholds: CompactThresholds {
                token_threshold: Some(200),
                turn_threshold: Some(3),
                message_threshold: Some(5),
                on_turn_end: None,
            },
        });

        // Add turns to trigger compaction
        for i in 0..6 {
            history.add_turn(
                format!("Question {} with extra text", i),
                format!("Answer {} with more detail", i),
                vec![],
            );
        }

        // Trigger compaction to build summary
        if history.needs_compaction() {
            let _ = history.compact();
        }

        // Verify we have a summary frame now
        let had_summary_before = history.summary_frame.is_some();

        // Now clear turns while preserving context
        history.clear_turns_preserve_context();

        // Verify turns are cleared but summary is preserved
        assert_eq!(history.turn_count(), 0, "Turns should be cleared");
        assert!(
            history.summary_frame.is_some() == had_summary_before,
            "Summary frame should be preserved"
        );

        // Token count should only include summary frame
        if history.summary_frame.is_some() {
            assert!(history.token_count() > 0, "Should have tokens from summary");
        }

        // to_messages should still work and include summary
        let messages = history.to_messages();
        if history.summary_frame.is_some() {
            assert!(
                !messages.is_empty(),
                "Should still have summary in messages"
            );
        }
    }

    #[test]
    fn test_clear_vs_clear_preserve_context() {
        let mut history = ConversationHistory::new();

        // Add some turns
        for i in 0..5 {
            history.add_turn(format!("Q{}", i), format!("A{}", i), vec![]);
        }

        // Force compaction
        let _ = history.compact();
        let had_summary = history.summary_frame.is_some();

        // Test clear_turns_preserve_context
        let mut history_preserve = history.clone();
        history_preserve.clear_turns_preserve_context();

        // Test regular clear
        let mut history_clear = history.clone();
        history_clear.clear();

        // Verify difference
        if had_summary {
            assert!(
                history_preserve.summary_frame.is_some(),
                "preserve should keep summary"
            );
            assert!(
                history_clear.summary_frame.is_none(),
                "clear removes summary"
            );
        }

        // Both should have no turns
        assert_eq!(history_preserve.turn_count(), 0);
        assert_eq!(history_clear.turn_count(), 0);
    }

    #[test]
    fn test_history_serialization() {
        let mut history = ConversationHistory::new();

        // Add some turns
        history.add_turn(
            "What is this project?".to_string(),
            "This is a Rust CLI tool.".to_string(),
            vec![ToolCallRecord {
                tool_name: "analyze".to_string(),
                args_summary: "path: .".to_string(),
                result_summary: "Found Rust project".to_string(),
                tool_id: Some("tool_1".to_string()),
                droppable: false,
            }],
        );

        // Serialize
        let json = history.to_json().expect("Should serialize");
        assert!(!json.is_empty());

        // Deserialize
        let restored = ConversationHistory::from_json(&json).expect("Should deserialize");
        assert_eq!(restored.turn_count(), 1);
        assert_eq!(restored.user_turn_count(), 1);

        // Verify tool call preserved
        let messages = restored.to_messages();
        assert!(!messages.is_empty());
    }

    #[test]
    fn test_history_serialization_with_compaction() {
        // Create history with compaction triggered
        let mut history = ConversationHistory::with_config(CompactConfig {
            retention_window: 2,
            eviction_window: 0.6,
            thresholds: CompactThresholds {
                token_threshold: Some(200),
                turn_threshold: Some(3),
                message_threshold: Some(5),
                on_turn_end: None,
            },
        });

        // Add many turns to trigger compaction
        for i in 0..6 {
            history.add_turn(
                format!("Question {} with some text", i),
                format!("Answer {} with more detail", i),
                vec![],
            );
        }

        // Trigger compaction
        if history.needs_compaction() {
            let _ = history.compact();
        }

        let had_summary = history.summary_frame.is_some();

        // Serialize with summary
        let json = history.to_json().expect("Should serialize");

        // Deserialize and verify summary preserved
        let restored = ConversationHistory::from_json(&json).expect("Should deserialize");
        assert_eq!(
            restored.summary_frame.is_some(),
            had_summary,
            "Summary frame should be preserved"
        );

        // to_messages should include summary
        let messages = restored.to_messages();
        if had_summary {
            // Summary adds 2 messages (user + assistant acknowledgment)
            assert!(messages.len() >= 2, "Should have summary messages");
        }
    }
}