robit-agent 0.1.17

Agent runtime, tool system, skill system, and frontend trait for robit.
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
//! Context management — output truncation and history window management.

use async_openai::types::chat::{
    ChatCompletionRequestMessage, ChatCompletionRequestUserMessage,
    ChatCompletionRequestUserMessageContent,
};
use robit_ai::config::ContextConfig;

// ============================================================================
// Truncation result
// ============================================================================

/// Type of truncation action, determines how the caller should handle the result.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TruncationAction {
    /// Generate a new summary segment from full conversation rounds.
    /// The removed messages in `TruncationResult` are the full rounds to summarize.
    NewSegment,
    /// Merge multiple existing summary segments into one.
    /// `summaries` contains the text of segments to merge (oldest first).
    /// `start_position` is the index of the first segment in history.
    /// `count` is how many consecutive segments to merge.
    MergeSegments {
        summaries: Vec<String>,
        start_position: usize,
        count: usize,
    },
    /// No compression needed — history was truncated but the removed content
    /// is too small to justify an LLM summary call.
    TruncateOnly,
}

/// Result of context truncation, used for async compression.
#[derive(Debug)]
pub struct TruncationResult {
    /// Number of conversation rounds removed.
    pub rounds_removed: usize,
    /// Number of individual messages removed.
    pub messages_removed: usize,
    /// The removed messages (for generating summary — only for NewSegment action).
    pub removed_messages: Vec<ChatCompletionRequestMessage>,
    /// Position where summary should be inserted / replaced.
    pub insert_position: usize,
    /// Whether compression is needed (token count exceeds threshold).
    pub needs_compression: bool,
    /// The type of truncation action taken.
    pub action: TruncationAction,
}

// ============================================================================
// Tool output truncation (Layer 1)
// ============================================================================

/// Truncate tool output based on line count and byte limits.
pub fn truncate_output(content: &str, max_lines: usize, max_bytes: usize) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let total_lines = lines.len();
    let total_bytes = content.len();

    // Check if truncation is needed
    let line_truncated = total_lines > max_lines;
    let byte_truncated = total_bytes > max_bytes;

    if !line_truncated && !byte_truncated {
        return content.to_string();
    }

    let mut output = String::new();
    let mut byte_count = 0;
    let mut displayed_lines = 0;

    for (i, line) in lines.iter().enumerate() {
        if i >= max_lines {
            break;
        }
        let line_with_newline = if i < total_lines - 1 {
            format!("{}\n", line)
        } else {
            line.to_string()
        };

        if byte_count + line_with_newline.len() > max_bytes {
            break;
        }

        output.push_str(&line_with_newline);
        byte_count += line_with_newline.len();
        displayed_lines += 1;
    }

    if line_truncated {
        output.push_str(&format!(
            "\n... (Output truncated, {} lines total, showing first {}. Use offset/limit to read more)",
            total_lines, displayed_lines
        ));
    } else if byte_truncated {
        output.push_str(&format!(
            "\n... (Output truncated, {} bytes total, showing first {} bytes)",
            total_bytes, byte_count
        ));
    }

    output
}

// ============================================================================
// Token estimation
// ============================================================================

/// Estimate token count for a string.
///
/// Uses a more nuanced heuristic based on character type:
/// - ASCII letters/digits: ~3.5 chars/token (BPE tokenizer average)
/// - CJK characters: ~1.5 chars/token (most CJK chars are 1-2 tokens each)
/// - Whitespace: minimal token cost (usually merged with adjacent tokens)
/// - Punctuation/symbols: ~1 token per char (often individual tokens)
/// - Code (braces, operators): ~1 token per char
///
/// This is still an estimate; apply `token_safety_margin` at the message level.
pub fn estimate_tokens(text: &str) -> usize {
    if text.is_empty() {
        return 0;
    }

    let mut ascii_alnum = 0usize;
    let mut cjk = 0usize;
    let mut whitespace = 0usize;
    let mut other = 0usize; // punctuation, symbols, code characters

    for ch in text.chars() {
        if ch.is_whitespace() {
            whitespace += 1;
        } else if ch.is_ascii_alphanumeric() {
            ascii_alnum += 1;
        } else {
            let cp = ch as u32;
            // CJK Unified Ideographs + extensions + fullwidth forms
            // + Hiragana, Katakana, Hangul, CJK punctuation
            if (0x4E00..=0x9FFF).contains(&cp)
                || (0x3400..=0x4DBF).contains(&cp)
                || (0xF900..=0xFAFF).contains(&cp)
                || (0xFF00..=0xFFEF).contains(&cp)
                || (0x3000..=0x303F).contains(&cp)
                || (0x3040..=0x309F).contains(&cp)
                || (0x30A0..=0x30FF).contains(&cp)
                || (0xAC00..=0xD7AF).contains(&cp)
            {
                cjk += 1;
            } else {
                other += 1;
            }
        }
    }

    // BPE tokenizer averages:
    // - ASCII alphanumeric: ~3.5 chars per token
    // - CJK: ~1.5 chars per token (most are 1 token each, some pairs)
    // - Whitespace: negligible (merged with adjacent tokens)
    // - Other (punctuation/code): ~1 char per token
    let ascii_tokens = (ascii_alnum as f64 / 3.5).ceil() as usize;
    let cjk_tokens = (cjk as f64 / 1.5).ceil() as usize;
    let whitespace_tokens = (whitespace as f64 / 10.0).ceil() as usize;
    let other_tokens = other; // ~1:1

    ascii_tokens + cjk_tokens + whitespace_tokens + other_tokens
}

/// Estimate tokens for a list of messages.
pub fn estimate_messages_tokens(messages: &[ChatCompletionRequestMessage]) -> usize {
    let mut total = 0;
    for msg in messages {
        // Each message has ~4 tokens of overhead (role, delimiters)
        total += 4;
        total += estimate_message_content_tokens(msg);
    }
    total
}

/// Estimate tokens for messages, applying the configured safety margin.
pub fn estimate_messages_tokens_with_margin(
    messages: &[ChatCompletionRequestMessage],
    safety_margin: f32,
) -> usize {
    let raw = estimate_messages_tokens(messages);
    (raw as f32 * safety_margin).ceil() as usize
}

/// Estimate tokens for a single message's content.
fn estimate_message_content_tokens(msg: &ChatCompletionRequestMessage) -> usize {
    use async_openai::types::chat::ChatCompletionRequestUserMessageContentPart;

    // For user messages with multimodal (array) content, estimate each part
    // separately. Image base64 data URLs must NOT be counted by string length:
    // a 2K image is ~10MB of base64 but only ~1-2k tokens to the vision API.
    // Counting the raw base64 wildly overestimates tokens and triggers endless
    // truncation loops.
    if let ChatCompletionRequestMessage::User(user_msg) = msg {
        if let ChatCompletionRequestUserMessageContent::Array(parts) = &user_msg.content {
            let mut total = 0;
            for part in parts {
                match part {
                    ChatCompletionRequestUserMessageContentPart::Text(t) => {
                        total += estimate_tokens(&t.text);
                    }
                    ChatCompletionRequestUserMessageContentPart::ImageUrl(_) => {
                        // Vision models count image tokens by resolution,
                        // typically ~765-2000 tokens per image. Use a
                        // conservative flat estimate.
                        total += 2000;
                    }
                    _ => {
                        // InputAudio, File, etc. - not used in this codebase.
                    }
                }
            }
            return total;
        }
    }

    // Fallback: text-only messages - estimate from the JSON serialization.
    match serde_json::to_string(msg) {
        Ok(json) => estimate_tokens(&json),
        Err(_) => 0,
    }
}

// ============================================================================
// Context manager (Layer 2: history truncation)
// ============================================================================

/// Manages the context window, truncating history when approaching token limits.
pub struct ContextManager {
    /// Model's context window size in tokens.
    pub max_tokens: usize,
    /// Ratio of context window to reserve for LLM response (default 0.2 = 20%).
    pub reserve_ratio: f32,
    /// Fraction of max_tokens at which truncation triggers (default 0.7).
    pub truncation_ratio: f32,
    /// Minimum conversation rounds to keep after truncation (default 3).
    pub min_keep_rounds: usize,
    /// Safety multiplier for token estimates (default 1.3).
    pub token_safety_margin: f32,
    /// Max output lines for tool results.
    pub max_output_lines: usize,
    /// Max output bytes for tool results.
    pub max_output_bytes: usize,
    /// Token threshold for triggering compression.
    pub compression_token_threshold: usize,
    /// Whether compression is enabled.
    pub compression_enabled: bool,
    /// Maximum tool calls per turn (default 30).
    pub max_tool_calls_per_turn: usize,
    /// Whether progressive segmented compression is enabled (default true).
    pub progressive_compression: bool,
    /// Number of full rounds per summary segment (default 3).
    pub rounds_per_summary: usize,
    /// Maximum number of summary segments to keep (default 5).
    pub max_summary_segments: usize,
    /// Number of segments to merge at a time (default 2).
    pub merge_count: usize,
    /// Maximum merges per segment before discarding (default 2).
    pub max_merges_per_segment: usize,
}

impl ContextManager {
    pub fn new(context_window: Option<u64>, config: Option<&ContextConfig>) -> Self {
        let max_tokens = context_window.unwrap_or(65536) as usize;

        let (
            max_output_lines,
            max_output_bytes,
            reserve_ratio,
            truncation_ratio,
            min_keep_rounds,
            token_safety_margin,
            compression_token_threshold,
            compression_enabled,
            max_tool_calls_per_turn,
            progressive_compression,
            rounds_per_summary,
            max_summary_segments,
            merge_count,
            max_merges_per_segment,
        ) = match config {
            Some(c) => (
                c.max_output_lines.unwrap_or(500),
                c.max_output_bytes.unwrap_or(51200),
                c.reserve_ratio.unwrap_or(0.2),
                c.truncation_ratio.unwrap_or(0.7),
                c.min_keep_rounds.unwrap_or(3),
                c.token_safety_margin.unwrap_or(1.3),
                c.compression_token_threshold.unwrap_or(5000),
                c.compression_enabled.unwrap_or(true),
                c.max_tool_calls_per_turn.unwrap_or(30),
                c.progressive_compression.unwrap_or(true),
                c.rounds_per_summary.unwrap_or(3),
                c.max_summary_segments.unwrap_or(5),
                c.merge_count.unwrap_or(2),
                c.max_merges_per_segment.unwrap_or(2),
            ),
            None => (500, 51200, 0.2, 0.7, 3, 1.3, 5000, true, 30, true, 3, 5, 2, 2),
        };

        Self {
            max_tokens,
            reserve_ratio,
            truncation_ratio,
            min_keep_rounds,
            token_safety_margin,
            max_output_lines,
            max_output_bytes,
            compression_token_threshold,
            compression_enabled,
            max_tool_calls_per_turn,
            progressive_compression,
            rounds_per_summary,
            max_summary_segments,
            merge_count,
            max_merges_per_segment,
        }
    }

    /// Maximum tokens at which truncation is triggered.
    /// Uses `truncation_ratio` (default 0.7) to trigger earlier than the
    /// absolute limit, leaving headroom for estimation errors and LLM response.
    pub fn truncation_threshold(&self) -> usize {
        (self.max_tokens as f32 * self.truncation_ratio) as usize
    }

    /// Maximum tokens available for input (total - reserved for response).
    /// Note: this is the absolute upper bound; truncation actually triggers
    /// earlier via `truncation_threshold()`.
    pub fn available_tokens(&self) -> usize {
        (self.max_tokens as f32 * (1.0 - self.reserve_ratio)) as usize
    }

    /// Truncate tool output using the configured limits.
    pub fn truncate_tool_output(&self, content: &str) -> String {
        truncate_output(content, self.max_output_lines, self.max_output_bytes)
    }

    /// Check if history needs truncation and perform it if necessary.
    /// Returns `TruncationResult` with removed messages for async compression.
    ///
    /// Strategy (progressive compression):
    /// 1. Uses `truncation_threshold()` (default 70% of max_tokens) as trigger point.
    /// 2. When over threshold, performs exactly one compression action per call:
    ///    - Priority 1: Compress the oldest `rounds_per_summary` full rounds into a new summary segment.
    ///    - Priority 2: Merge the oldest `merge_count` summary segments into one.
    ///    - Priority 3: Discard the oldest summary segment (if merge limit reached).
    ///    - Priority 4: Fall back to aggressive truncation (old behavior).
    /// 3. If `progressive_compression` is disabled, falls back to single-shot truncation.
    pub fn maybe_truncate(
        &self,
        messages: &mut Vec<ChatCompletionRequestMessage>,
    ) -> TruncationResult {
        let estimated = estimate_messages_tokens_with_margin(messages, self.token_safety_margin);
        let threshold = self.truncation_threshold();

        tracing::debug!("maybe_truncate: initial message count = {}, estimated tokens = {}, threshold = {}",
            messages.len(), estimated, threshold);

        if estimated <= threshold {
            tracing::debug!("maybe_truncate: No truncation needed ({} <= {})", estimated, threshold);
            return TruncationResult {
                rounds_removed: 0,
                messages_removed: 0,
                removed_messages: Vec::new(),
                insert_position: 0,
                needs_compression: false,
                action: TruncationAction::TruncateOnly,
            };
        }

        tracing::info!(
            "=== Context truncation triggered ==="
        );
        tracing::info!(
            "Estimated: {} tokens (with {:.1}x margin), Threshold: {} tokens, Max: {} tokens",
            estimated,
            self.token_safety_margin,
            threshold,
            self.max_tokens
        );
        tracing::info!("Compression enabled: {}, Progressive: {}",
            self.compression_enabled, self.progressive_compression);

        // Fall back to legacy single-shot truncation if progressive is disabled
        if !self.progressive_compression {
            tracing::info!("Progressive compression disabled, using legacy single-shot truncation");
            return self.legacy_truncate(messages, threshold);
        }

        // Try progressive compression actions in priority order
        if let Some(result) = self.try_new_segment(messages) {
            tracing::info!("Progressive action: NewSegment ({} rounds)", result.rounds_removed);
            return result;
        }

        if let Some(result) = self.try_merge_segments(messages) {
            tracing::info!("Progressive action: MergeSegments");
            return result;
        }

        if let Some(result) = self.try_discard_oldest_segment(messages) {
            tracing::info!("Progressive action: Discard oldest segment");
            return result;
        }

        // Fallback: aggressive single-shot truncation
        tracing::warn!("All progressive actions exhausted, falling back to legacy truncation");
        self.legacy_truncate(messages, threshold)
    }

    // ------------------------------------------------------------------------
    // Progressive compression: Priority 1 — new summary segment
    // ------------------------------------------------------------------------

    fn try_new_segment(
        &self,
        messages: &mut Vec<ChatCompletionRequestMessage>,
    ) -> Option<TruncationResult> {
        if !self.compression_enabled {
            return None;
        }

        // Find all full (non-summary, non-system) user rounds,
        // skipping summary segments and the discard notice.
        let round_starts: Vec<usize> = messages
            .iter()
            .enumerate()
            .filter(|(_, m)| is_user_message(m) && !is_summary_segment(m) && !is_discard_notice(m))
            .map(|(i, _)| i)
            .collect();

        if round_starts.len() <= self.min_keep_rounds + self.rounds_per_summary {
            tracing::debug!("Not enough full rounds to compress (have {}, need min_keep + rounds_per_summary = {})",
                round_starts.len(), self.min_keep_rounds + self.rounds_per_summary);
            return None;
        }

        // Take the oldest `rounds_per_summary` rounds
        let take_rounds = self.rounds_per_summary.min(round_starts.len() - self.min_keep_rounds);
        if take_rounds == 0 {
            return None;
        }

        let start_idx = round_starts[0];
        let end_idx = if take_rounds < round_starts.len() {
            round_starts[take_rounds]
        } else {
            messages.len()
        };

        let removed_messages: Vec<ChatCompletionRequestMessage> =
            messages[start_idx..end_idx].to_vec();
        let messages_removed = removed_messages.len();
        let removed_tokens = estimate_messages_tokens(&removed_messages);

        // Need enough tokens to justify compression
        if removed_tokens < self.compression_token_threshold {
            tracing::debug!("Removed tokens ({}) below compression threshold ({})",
                removed_tokens, self.compression_token_threshold);
            return None;
        }

        // Remove the rounds
        messages.drain(start_idx..end_idx);

        // Insert placeholder after system messages + discard notice (if any)
        // i.e. before any other summary segments
        let system_msg_count = messages.iter().take_while(|m| is_system_message(m)).count();
        let has_discard = find_discard_notice_pos(messages).is_some();
        let insert_pos = system_msg_count + if has_discard { 1 } else { 0 };

        let placeholder = make_summary_placeholder(take_rounds);
        messages.insert(insert_pos, placeholder);

        tracing::info!(
            "New summary segment: removed {} rounds ({} messages, ~{} tokens), insert at {}",
            take_rounds, messages_removed, removed_tokens, insert_pos
        );

        Some(TruncationResult {
            rounds_removed: take_rounds,
            messages_removed,
            removed_messages,
            insert_position: insert_pos,
            needs_compression: true,
            action: TruncationAction::NewSegment,
        })
    }

    // ------------------------------------------------------------------------
    // Progressive compression: Priority 2 — merge summary segments
    // ------------------------------------------------------------------------

    fn try_merge_segments(
        &self,
        messages: &mut Vec<ChatCompletionRequestMessage>,
    ) -> Option<TruncationResult> {
        if !self.compression_enabled {
            return None;
        }

        let segments = find_summary_segments(messages);
        if segments.len() <= self.max_summary_segments {
            tracing::debug!("Segment count ({}) within limit ({})", segments.len(), self.max_summary_segments);
            return None;
        }

        // Check if the oldest segment can still be merged
        let oldest = segments.first()?;
        if oldest.merge_level >= self.max_merges_per_segment {
            tracing::debug!("Oldest segment at merge level {} >= max {}, will discard instead",
                oldest.merge_level, self.max_merges_per_segment);
            return None;
        }

        // Take the oldest `merge_count` segments
        let take_count = self.merge_count.min(segments.len());
        if take_count < 2 {
            return None;
        }

        let start_pos = segments[0].index;
        let end_pos = segments[take_count - 1].index + 1;

        let summaries: Vec<String> = segments[..take_count]
            .iter()
            .map(|s| s.content.clone())
            .collect();

        let max_level = segments[..take_count]
            .iter()
            .map(|s| s.merge_level)
            .max()
            .unwrap_or(0);
        let new_level = max_level + 1;

        // Remove the old segments
        messages.drain(start_pos..end_pos);

        // Insert merged placeholder
        let placeholder = make_merge_placeholder(new_level, take_count);
        messages.insert(start_pos, placeholder);

        tracing::info!(
            "Merging {} summary segments into one (level {}), start pos {}",
            take_count, new_level, start_pos
        );

        Some(TruncationResult {
            rounds_removed: 0,
            messages_removed: take_count,
            removed_messages: Vec::new(),
            insert_position: start_pos,
            needs_compression: true,
            action: TruncationAction::MergeSegments {
                summaries,
                start_position: start_pos,
                count: take_count,
            },
        })
    }

    // ------------------------------------------------------------------------
    // Progressive compression: Priority 3 — discard oldest summary segment
    // ------------------------------------------------------------------------

    fn try_discard_oldest_segment(
        &self,
        messages: &mut Vec<ChatCompletionRequestMessage>,
    ) -> Option<TruncationResult> {
        let segments = find_summary_segments(messages);
        if segments.len() <= self.max_summary_segments {
            return None;
        }

        let oldest = segments.first()?;
        if oldest.merge_level < self.max_merges_per_segment {
            // Should have been handled by try_merge_segments
            return None;
        }

        // Remove the oldest segment
        let pos = oldest.index;
        messages.remove(pos);

        tracing::info!("Discarded oldest summary segment at position {}", pos);

        // Ensure discard notice exists
        let system_msg_count = messages.iter().take_while(|m| is_system_message(m)).count();
        if find_discard_notice_pos(messages).is_none() {
            let notice = make_discard_notice();
            messages.insert(system_msg_count, notice);
            tracing::debug!("Added discard notice at position {}", system_msg_count);
        }

        Some(TruncationResult {
            rounds_removed: 0,
            messages_removed: 1,
            removed_messages: Vec::new(),
            insert_position: pos,
            needs_compression: false,
            action: TruncationAction::TruncateOnly,
        })
    }

    // ------------------------------------------------------------------------
    // Legacy single-shot truncation (fallback)
    // ------------------------------------------------------------------------

    fn legacy_truncate(
        &self,
        messages: &mut Vec<ChatCompletionRequestMessage>,
        threshold: usize,
    ) -> TruncationResult {
        // Find round boundaries: a round starts with a User message
        let mut round_starts: Vec<usize> = Vec::new();
        for (i, msg) in messages.iter().enumerate() {
            if is_user_message(msg) {
                round_starts.push(i);
            }
        }
        tracing::debug!("Found {} user message round boundaries", round_starts.len());

        if round_starts.is_empty() {
            tracing::debug!("No user messages found, no truncation performed");
            return TruncationResult {
                rounds_removed: 0,
                messages_removed: 0,
                removed_messages: Vec::new(),
                insert_position: 0,
                needs_compression: false,
                action: TruncationAction::TruncateOnly,
            };
        }

        let total_rounds = round_starts.len();
        let must_keep = self.min_keep_rounds.min(total_rounds);
        tracing::debug!("Total rounds: {}, Must keep at least: {} rounds", total_rounds, must_keep);

        let mut removed_messages: Vec<ChatCompletionRequestMessage> = Vec::new();
        let mut rounds_removed = 0;
        let mut messages_removed = 0;

        while round_starts.len() > must_keep
            && estimate_messages_tokens_with_margin(messages, self.token_safety_margin) > threshold
        {
            let start_idx = round_starts[0];
            let end_idx = if round_starts.len() > 1 {
                round_starts[1]
            } else {
                messages.len()
            };

            if self.compression_enabled {
                removed_messages.extend(messages[start_idx..end_idx].to_vec());
            }

            let count = end_idx - start_idx;
            messages.drain(start_idx..end_idx);

            round_starts.remove(0);
            for idx in round_starts.iter_mut() {
                *idx = idx.saturating_sub(count);
            }

            rounds_removed += 1;
            messages_removed += count;
        }

        if rounds_removed == 0 {
            tracing::debug!("No rounds removed after checks");
            return TruncationResult {
                rounds_removed: 0,
                messages_removed: 0,
                removed_messages: Vec::new(),
                insert_position: 0,
                needs_compression: false,
                action: TruncationAction::TruncateOnly,
            };
        }

        let removed_tokens = estimate_messages_tokens(&removed_messages);
        let needs_compression =
            self.compression_enabled && removed_tokens >= self.compression_token_threshold;

        let system_msg_count = messages
            .iter()
            .take_while(|m| is_system_message(m))
            .count();

        let notice = if needs_compression {
            format!(
                "[Context compressed: {} earlier rounds ({} messages, ~{} tokens) have been summarized. {} most recent rounds preserved.]",
                rounds_removed, messages_removed, removed_tokens, round_starts.len()
            )
        } else {
            format!(
                "[Context truncated: {} earlier rounds ({} messages) removed to stay within token limit. {} most recent rounds preserved.]",
                rounds_removed, messages_removed, round_starts.len()
            )
        };

        let notice_msg = ChatCompletionRequestMessage::User(
            async_openai::types::chat::ChatCompletionRequestUserMessage {
                content: notice.into(),
                name: Some("system_notice".to_string()),
            }
            .into(),
        );

        messages.insert(system_msg_count, notice_msg);

        tracing::info!(
            "Legacy truncation: removed {} rounds ({} messages), kept {} rounds",
            rounds_removed, messages_removed, round_starts.len()
        );

        TruncationResult {
            rounds_removed,
            messages_removed,
            removed_messages,
            insert_position: system_msg_count,
            needs_compression,
            action: if needs_compression {
                // For legacy mode, treat single-shot summary as NewSegment
                // (one summary from many rounds)
                TruncationAction::NewSegment
            } else {
                TruncationAction::TruncateOnly
            },
        }
    }
}

fn is_user_message(msg: &ChatCompletionRequestMessage) -> bool {
    matches!(msg, ChatCompletionRequestMessage::User(_))
}

fn is_system_message(msg: &ChatCompletionRequestMessage) -> bool {
    matches!(msg, ChatCompletionRequestMessage::System(_))
}

// ============================================================================
// Summary segment helpers (progressive compression)
// ============================================================================

const SUMMARY_SEGMENT_PREFIX: &str = "summary_segment";
const DISCARD_NOTICE_NAME: &str = "discard_notice";
const LEGACY_NOTICE_NAME: &str = "system_notice";

/// Returns the `name` field of a User message, if any.
fn user_message_name(msg: &ChatCompletionRequestMessage) -> Option<&str> {
    match msg {
        ChatCompletionRequestMessage::User(u) => u.name.as_deref(),
        _ => None,
    }
}

/// Returns the text content of a User message, empty if not text.
fn user_message_text(msg: &ChatCompletionRequestMessage) -> String {
    match msg {
        ChatCompletionRequestMessage::User(u) => match &u.content {
            ChatCompletionRequestUserMessageContent::Text(t) => t.clone(),
            ChatCompletionRequestUserMessageContent::Array(parts) => parts
                .iter()
                .filter_map(|p| match p {
                    async_openai::types::chat::ChatCompletionRequestUserMessageContentPart::Text(t) => Some(t.text.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join(" "),
        },
        _ => String::new(),
    }
}

/// Check whether a message is a summary segment (any merge level, including legacy notice).
pub fn is_summary_segment(msg: &ChatCompletionRequestMessage) -> bool {
    match user_message_name(msg) {
        Some(name) => {
            name.starts_with(SUMMARY_SEGMENT_PREFIX) || name == LEGACY_NOTICE_NAME
        }
        None => false,
    }
}

/// Get the merge level (number of times this segment has been merged).
/// 0 = fresh segment, 1 = merged once, etc.
pub fn get_merge_level(msg: &ChatCompletionRequestMessage) -> usize {
    match user_message_name(msg) {
        Some(name) => {
            if name == LEGACY_NOTICE_NAME {
                0
            } else if let Some(suffix) = name.strip_prefix(SUMMARY_SEGMENT_PREFIX) {
                if suffix.is_empty() {
                    0
                } else if let Some(num_str) = suffix.strip_prefix("_m") {
                    num_str.parse::<usize>().unwrap_or(0)
                } else {
                    0
                }
            } else {
                0
            }
        }
        None => 0,
    }
}

/// Check if a message is the discard notice.
fn is_discard_notice(msg: &ChatCompletionRequestMessage) -> bool {
    matches!(user_message_name(msg), Some(name) if name == DISCARD_NOTICE_NAME)
}

/// Build the `name` value for a summary segment at the given merge level.
fn summary_segment_name(merge_level: usize) -> String {
    if merge_level == 0 {
        SUMMARY_SEGMENT_PREFIX.to_string()
    } else {
        format!("{}_m{}", SUMMARY_SEGMENT_PREFIX, merge_level)
    }
}

/// Build a placeholder summary segment message (pending LLM generation).
fn make_summary_placeholder(rounds_removed: usize) -> ChatCompletionRequestMessage {
    let text = format!(
        "[Compressing {} earlier conversation rounds into a summary...]",
        rounds_removed
    );
    ChatCompletionRequestMessage::User(
        ChatCompletionRequestUserMessage {
            content: text.into(),
            name: Some(summary_segment_name(0)),
        }
        .into(),
    )
}

/// Build a placeholder for merged segments (pending LLM generation).
fn make_merge_placeholder(merge_level: usize, count: usize) -> ChatCompletionRequestMessage {
    let text = format!(
        "[Merging {} earlier summary segments...]",
        count
    );
    ChatCompletionRequestMessage::User(
        ChatCompletionRequestUserMessage {
            content: text.into(),
            name: Some(summary_segment_name(merge_level)),
        }
        .into(),
    )
}

/// Build the discard notice message.
fn make_discard_notice() -> ChatCompletionRequestMessage {
    let text = "[Note: Earlier conversation history beyond the earliest summary has been discarded to save context space.]";
    ChatCompletionRequestMessage::User(
        ChatCompletionRequestUserMessage {
            content: text.into(),
            name: Some(DISCARD_NOTICE_NAME.to_string()),
        }
        .into(),
    )
}

/// Information about a summary segment found in history.
#[derive(Debug, Clone)]
struct SummarySegmentInfo {
    index: usize,
    merge_level: usize,
    content: String,
}

/// Scan message history and collect all summary segments, ordered from oldest to newest.
fn find_summary_segments(messages: &[ChatCompletionRequestMessage]) -> Vec<SummarySegmentInfo> {
    let mut segments = Vec::new();
    for (i, msg) in messages.iter().enumerate() {
        if is_summary_segment(msg) {
            segments.push(SummarySegmentInfo {
                index: i,
                merge_level: get_merge_level(msg),
                content: user_message_text(msg),
            });
        }
    }
    segments
}

/// Find the position of the discard notice, if any.
fn find_discard_notice_pos(messages: &[ChatCompletionRequestMessage]) -> Option<usize> {
    messages.iter().position(is_discard_notice)
}

// ============================================================================
// Transcript formatting for summary compression
// ============================================================================

/// Format removed messages into a compact transcript for summary generation.
/// Extracts user messages, assistant text, and tool call names only.
/// Truncates each message to keep the transcript concise.
pub fn format_removed_messages_as_transcript(
    messages: &[ChatCompletionRequestMessage],
) -> String {
    let mut transcript = String::new();

    for msg in messages {
        match msg {
            ChatCompletionRequestMessage::User(user_msg) => {
                let text = match &user_msg.content {
                    async_openai::types::chat::ChatCompletionRequestUserMessageContent::Text(t) => {
                        t.clone()
                    }
                    async_openai::types::chat::ChatCompletionRequestUserMessageContent::Array(parts) => {
                        parts.iter()
                            .filter_map(|p| match p {
                                async_openai::types::chat::ChatCompletionRequestUserMessageContentPart::Text(t) => Some(t.text.as_str()),
                                _ => None,
                            })
                            .collect::<Vec<_>>()
                            .join(" ")
                    }
                };
                let truncated = truncate_str(&text, 200);
                transcript.push_str(&format!("User: {}\n", truncated));
            }
            ChatCompletionRequestMessage::Assistant(assistant_msg) => {
                let content_str = assistant_msg.content.as_ref().map(|c| {
                    match serde_json::to_string(c) {
                        Ok(json) => json.trim_matches('"').to_string(),
                        Err(_) => format!("{:?}", c),
                    }
                }).unwrap_or_default();
                let truncated = truncate_str(&content_str, 300);
                transcript.push_str(&format!("Assistant: {}\n", truncated));
                if let Some(tool_calls) = &assistant_msg.tool_calls {
                    for tc in tool_calls {
                        if let async_openai::types::chat::ChatCompletionMessageToolCalls::Function(f) = tc {
                            transcript.push_str(&format!(
                                "  [Tool: {}({})]\n",
                                f.function.name,
                                truncate_str(&f.function.arguments, 100)
                            ));
                        }
                    }
                }
            }
            ChatCompletionRequestMessage::Tool(tool_msg) => {
                let content_str = match serde_json::to_string(&tool_msg.content) {
                    Ok(json) => json.trim_matches('"').to_string(),
                    Err(_) => format!("{:?}", tool_msg.content),
                };
                let truncated = truncate_str(&content_str, 150);
                transcript.push_str(&format!("  [Result: {}]\n", truncated));
            }
            _ => {}
        }
    }

    if transcript.is_empty() {
        transcript.push_str("(no conversation content)");
    }

    transcript
}

/// Truncate a string to at most `max_chars` characters, adding "..." if truncated.
/// Respects UTF-8 character boundaries.
fn truncate_str(s: &str, max_chars: usize) -> String {
    if s.len() <= max_chars {
        s.to_string()
    } else {
        let mut end = max_chars;
        while end > 0 && !s.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}...", &s[..end])
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use async_openai::types::chat::ChatCompletionRequestUserMessage;

    fn make_user_message(content: &str) -> ChatCompletionRequestMessage {
        ChatCompletionRequestMessage::User(
            ChatCompletionRequestUserMessage {
                content: content.into(),
                name: None,
            }
            .into(),
        )
    }

    fn make_system_message(content: &str) -> ChatCompletionRequestMessage {
        ChatCompletionRequestMessage::System(
            async_openai::types::chat::ChatCompletionRequestSystemMessage {
                content: content.into(),
                name: None,
            }
            .into(),
        )
    }

    fn make_test_config() -> ContextConfig {
        ContextConfig {
            max_output_lines: Some(500),
            max_output_bytes: Some(51200),
            reserve_ratio: Some(0.2),
            truncation_ratio: Some(0.7),
            min_keep_rounds: Some(3),
            token_safety_margin: Some(1.3),
            compression_token_threshold: Some(5000),
            compression_enabled: Some(true),
            max_tool_calls_per_turn: Some(30),
            progressive_compression: Some(true),
            rounds_per_summary: Some(3),
            max_summary_segments: Some(5),
            merge_count: Some(2),
            max_merges_per_segment: Some(2),
        }
    }

    fn make_user_message_named(content: &str, name: &str) -> ChatCompletionRequestMessage {
        ChatCompletionRequestMessage::User(
            ChatCompletionRequestUserMessage {
                content: content.into(),
                name: Some(name.to_string()),
            }
            .into(),
        )
    }

    fn make_summary_segment(content: &str, merge_level: usize) -> ChatCompletionRequestMessage {
        let name = if merge_level == 0 {
            "summary_segment".to_string()
        } else {
            format!("summary_segment_m{}", merge_level)
        };
        make_user_message_named(content, &name)
    }

    fn make_legacy_notice(content: &str) -> ChatCompletionRequestMessage {
        make_user_message_named(content, "system_notice")
    }

    // fn make_discard_notice_msg() -> ChatCompletionRequestMessage {
    //     make_user_message_named(
    //         "[Note: Earlier conversation history beyond the earliest summary has been discarded.]",
    //         "discard_notice",
    //     )
    // }

    // ==========================================================================
    // estimate_tokens tests
    // ==========================================================================

    #[test]
    fn test_estimate_tokens_english() {
        let text = "Hello world, this is a test of the token estimation system.";
        let tokens = estimate_tokens(text);
        assert!(tokens >= 10, "Expected at least 10 tokens, got {}", tokens);
        assert!(tokens <= 30, "Expected at most 30 tokens, got {}", tokens);
    }

    #[test]
    fn test_estimate_tokens_chinese() {
        let chinese = "你好世界,这是一个测试。";
        let tokens = estimate_tokens(chinese);
        assert!(tokens >= 5, "Expected at least 5 tokens, got {}", tokens);
        assert!(tokens <= 15, "Expected at most 15 tokens, got {}", tokens);
    }

    #[test]
    fn test_estimate_tokens_code() {
        let code = "fn main() {\n    println!(\"Hello\");\n}";
        let tokens = estimate_tokens(code);
        assert!(tokens >= 10, "Expected at least 10 tokens, got {}", tokens);
        assert!(tokens <= 40, "Expected at most 40 tokens, got {}", tokens);
    }

    #[test]
    fn test_estimate_tokens_empty() {
        assert_eq!(estimate_tokens(""), 0);
    }

    #[test]
    fn test_estimate_tokens_mixed() {
        let mixed = "Hello 你好 world 世界!fn test() {}";
        let tokens = estimate_tokens(mixed);
        assert!(tokens > 0);
        assert!(tokens <= 40, "Expected at most 40 tokens, got {}", tokens);
    }

    #[test]
    fn test_estimate_messages_tokens_with_margin() {
        let messages = vec![
            make_system_message("You are a helpful assistant"),
            make_user_message("Hello world"),
        ];
        let raw = estimate_messages_tokens(&messages);
        let with_margin = estimate_messages_tokens_with_margin(&messages, 1.3);
        assert!(with_margin > raw);
        // 1.3x margin should be ~30% higher
        let expected = (raw as f32 * 1.3).ceil() as usize;
        assert_eq!(with_margin, expected);
    }

    // ==========================================================================
    // ContextManager tests
    // ==========================================================================

    #[test]
    fn test_truncation_threshold() {
        let config = make_test_config();
        let manager = ContextManager::new(Some(65536), Some(&config));
        // 65536 * 0.7 = 45875
        assert_eq!(manager.truncation_threshold(), 45875);
    }

    #[test]
    fn test_truncation_result_no_truncation() {
        let mut messages = vec![
            make_system_message("You are a helpful assistant"),
            make_user_message("Hello"),
        ];

        let config = make_test_config();
        let manager = ContextManager::new(Some(65536), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        assert_eq!(result.rounds_removed, 0);
        assert!(!result.needs_compression);
    }

    #[test]
    fn test_truncation_respects_min_keep_rounds() {
        let mut messages = vec![
            make_system_message("You are a helpful assistant"),
        ];

        // Add 10 rounds of large messages
        for i in 0..10 {
            let content = format!("User message {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.min_keep_rounds = Some(3); // Must keep at least 3 rounds

        // Use small context window to force aggressive truncation
        let manager = ContextManager::new(Some(5000), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        // Should have removed some rounds...
        assert!(
            result.rounds_removed > 0,
            "Should have removed some rounds"
        );
        // ...but should still have at least 3 user rounds + notice
        let user_count = messages
            .iter()
            .filter(|m| matches!(m, ChatCompletionRequestMessage::User(_)))
            .count();
        assert!(
            user_count >= 4,
            "Should have at least 3 user rounds + notice, got {}",
            user_count
        );
    }

    #[test]
    fn test_truncation_early_trigger() {
        let mut messages = vec![
            make_system_message("You are a helpful assistant"),
        ];

        // Add 8 rounds of messages, each ~2000 chars
        for i in 0..8 {
            let content = format!("User message {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.truncation_ratio = Some(0.7);
        config.min_keep_rounds = Some(2);
        config.token_safety_margin = Some(1.3);

        // With 65536 context, truncation threshold = 45875
        // 8 rounds * ~2000 chars each ≈ much less than 45875, so no truncation
        let manager = ContextManager::new(Some(65536), Some(&config));
        let result = manager.maybe_truncate(&mut messages);
        assert_eq!(
            result.rounds_removed, 0,
            "Should not truncate small messages in large window"
        );

        // With 8000 context, truncation threshold = 5600
        let manager2 = ContextManager::new(Some(8000), Some(&config));
        let mut messages2 = messages.clone();
        let result2 = manager2.maybe_truncate(&mut messages2);
        assert!(
            result2.rounds_removed > 0,
            "Should truncate when exceeding small window"
        );
    }

    #[test]
    fn test_token_safety_margin_effect() {
        let mut messages = vec![
            make_system_message("You are a helpful assistant"),
        ];

        for i in 0..10 {
            let content = format!("User message {}: {}", i, "x".repeat(500));
            messages.push(make_user_message(&content));
        }

        // With margin 1.0 (no safety), truncation may not trigger
        let mut config_low = make_test_config();
        config_low.token_safety_margin = Some(1.0);
        config_low.truncation_ratio = Some(0.7);
        config_low.min_keep_rounds = Some(1);

        let mut msgs_low = messages.clone();
        let manager_low = ContextManager::new(Some(8000), Some(&config_low));
        let result_low = manager_low.maybe_truncate(&mut msgs_low);

        // With margin 2.0 (very conservative), truncation more likely triggers
        let mut config_high = make_test_config();
        config_high.token_safety_margin = Some(2.0);
        config_high.truncation_ratio = Some(0.7);
        config_high.min_keep_rounds = Some(1);

        let mut msgs_high = messages.clone();
        let manager_high = ContextManager::new(Some(8000), Some(&config_high));
        let result_high = manager_high.maybe_truncate(&mut msgs_high);

        // Higher margin should result in >= rounds removed
        assert!(
            result_high.rounds_removed >= result_low.rounds_removed,
            "Higher safety margin should trigger at least as much truncation: high={}, low={}",
            result_high.rounds_removed,
            result_low.rounds_removed
        );
    }

    #[test]
    fn test_compression_flag_in_old_tests() {
        let mut messages = vec![
            make_system_message("You are a helpful assistant"),
        ];

        // Add 20 rounds of large messages
        for i in 0..20 {
            let content = format!("User message {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.compression_enabled = Some(false);
        config.compression_token_threshold = Some(1000);
        config.min_keep_rounds = Some(1);

        let manager = ContextManager::new(Some(5000), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        assert!(result.rounds_removed > 0);
        assert!(
            !result.needs_compression,
            "Should be false when compression disabled"
        );
    }

    #[test]
    fn test_truncate_output() {
        let content = "line1\nline2\nline3\nline4\nline5";
        let truncated = truncate_output(content, 3, 100);
        assert!(truncated.contains("line1"));
        assert!(truncated.contains("line2"));
        assert!(truncated.contains("line3"));
        assert!(!truncated.contains("line4"));
        assert!(truncated.contains("Output truncated"));
    }

    // ==========================================================================
    // Transcript formatting tests
    // ==========================================================================

    #[test]
    fn test_truncate_str_no_truncation() {
        let result = truncate_str("hello", 10);
        assert_eq!(result, "hello");
    }

    #[test]
    fn test_truncate_str_with_truncation() {
        let result = truncate_str("hello world this is long", 10);
        assert_eq!(result, "hello worl...");
    }

    #[test]
    fn test_format_transcript_user_and_assistant() {
        let messages = vec![
            make_user_message("Fix the bug in auth.rs"),
            make_system_message("System message should be skipped"),
        ];

        let transcript = format_removed_messages_as_transcript(&messages);
        assert!(transcript.contains("User: Fix the bug in auth.rs"));
        assert!(!transcript.contains("System message"), "System messages should be skipped");
    }

    #[test]
    fn test_format_transcript_empty() {
        let messages: Vec<ChatCompletionRequestMessage> = vec![];
        let transcript = format_removed_messages_as_transcript(&messages);
        assert!(transcript.contains("no conversation content"));
    }

    #[test]
    fn test_format_transcript_truncates_long_messages() {
        let long_text = "x".repeat(500);
        let messages = vec![
            make_user_message(&long_text),
        ];

        let transcript = format_removed_messages_as_transcript(&messages);
        assert!(transcript.contains("..."));
        // Should not contain the full 500 chars
        assert!(transcript.len() < long_text.len() + 50);
    }

    // ==========================================================================
    // Progressive compression tests
    // ==========================================================================

    #[test]
    fn test_is_summary_segment_recognizes_all_levels() {
        let m0 = make_summary_segment("summary 0", 0);
        let m1 = make_summary_segment("summary 1", 1);
        let m2 = make_summary_segment("summary 2", 2);
        let legacy = make_legacy_notice("old notice");
        let normal = make_user_message("hello");

        assert!(is_summary_segment(&m0));
        assert!(is_summary_segment(&m1));
        assert!(is_summary_segment(&m2));
        assert!(is_summary_segment(&legacy));
        assert!(!is_summary_segment(&normal));
    }

    #[test]
    fn test_get_merge_level() {
        assert_eq!(get_merge_level(&make_summary_segment("a", 0)), 0);
        assert_eq!(get_merge_level(&make_summary_segment("b", 1)), 1);
        assert_eq!(get_merge_level(&make_summary_segment("c", 2)), 2);
        assert_eq!(get_merge_level(&make_legacy_notice("d")), 0);
        assert_eq!(get_merge_level(&make_user_message("e")), 0);
    }

    #[test]
    fn test_progressive_no_truncation_needed() {
        let mut messages = vec![
            make_system_message("sys"),
            make_user_message("hi"),
        ];
        let config = make_test_config();
        let manager = ContextManager::new(Some(65536), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        assert_eq!(result.rounds_removed, 0);
        assert!(!result.needs_compression);
        assert_eq!(result.action, TruncationAction::TruncateOnly);
    }

    #[test]
    fn test_progressive_new_segment() {
        let mut messages = vec![make_system_message("sys")];
        // 10 rounds of large content
        for i in 0..10 {
            let content = format!("Round {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.min_keep_rounds = Some(3);
        config.rounds_per_summary = Some(3);
        config.compression_token_threshold = Some(100); // low threshold

        let manager = ContextManager::new(Some(8000), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        assert_eq!(result.action, TruncationAction::NewSegment);
        assert!(result.needs_compression);
        assert_eq!(result.rounds_removed, 3);
        assert!(result.removed_messages.len() > 0);

        // Verify the placeholder was inserted
        let has_seg = messages.iter().any(|m| is_summary_segment(m));
        assert!(has_seg, "Should have a summary segment placeholder");
    }

    #[test]
    fn test_progressive_disabled_falls_back_to_legacy() {
        let mut messages = vec![make_system_message("sys")];
        // 20 rounds of large content — definitely over threshold
        for i in 0..20 {
            let content = format!("Round {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.progressive_compression = Some(false);
        config.min_keep_rounds = Some(3);
        config.compression_token_threshold = Some(100);

        let manager = ContextManager::new(Some(8000), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        // Legacy mode removes as many rounds as needed to go below threshold,
        // which for 20 rounds of 2000 chars in an 8000-token window is more than 3.
        assert!(result.rounds_removed > 3,
            "Legacy should remove more than rounds_per_summary (3) rounds, removed {}",
            result.rounds_removed);
        // And the action type should be NewSegment (legacy single summary)
        assert!(matches!(result.action, TruncationAction::NewSegment));
    }

    #[test]
    fn test_progressive_merge_segments() {
        // Build history where full rounds are within budget (fewer than min_keep + rounds_per_summary)
        // but we have too many summary segments, forcing a merge.
        let mut messages = vec![make_system_message("sys")];
        // 6 summary segments at level 0 — exceeds max of 5, each large enough to matter
        for i in 0..6 {
            let content = format!("Summary {}: {}", i, "x".repeat(500));
            messages.push(make_summary_segment(&content, 0));
        }
        // Only 2 full user messages — less than min_keep, so NewSegment won't trigger
        for i in 0..2 {
            let content = format!("User {}: {}", i, "x".repeat(300));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.max_summary_segments = Some(5);
        config.merge_count = Some(2);
        config.min_keep_rounds = Some(3);
        config.rounds_per_summary = Some(3);
        config.compression_token_threshold = Some(10);

        // Tiny context window to force over-threshold
        let manager = ContextManager::new(Some(2000), Some(&config));

        // First check: confirm we're over threshold
        let estimated = estimate_messages_tokens_with_margin(&messages, 1.3);
        assert!(estimated > manager.truncation_threshold(),
            "Test setup error: should be over threshold, est={}, threshold={}",
            estimated, manager.truncation_threshold());

        let result = manager.maybe_truncate(&mut messages);

        // With full rounds < min_keep + rounds_per_summary, and segments > max,
        // try_new_segment returns None, try_merge_segments should run
        match &result.action {
            TruncationAction::MergeSegments { summaries, start_position, count } => {
                assert_eq!(*count, 2, "Should merge 2 segments");
                assert_eq!(summaries.len(), 2);
                assert!(*start_position >= 1, "Start after system message");
            }
            other => {
                panic!("Expected MergeSegments, got {:?}", other);
            }
        }

        // After merge: 6 - 2 + 1 = 5 segments (including the placeholder)
        let seg_count = messages.iter().filter(|m| is_summary_segment(m)).count();
        assert_eq!(seg_count, 5, "Should have 5 segments after merge");
    }

    #[test]
    fn test_progressive_discard_after_merge_limit() {
        // 6 summary segments at merge level 2 (at the max), plus some full rounds
        let mut messages = vec![make_system_message("sys")];
        for i in 0..6 {
            messages.push(make_summary_segment(&format!("Old summary {}", i), 2));
        }
        for i in 0..5 {
            let content = format!("User {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.max_summary_segments = Some(5);
        config.max_merges_per_segment = Some(2);
        config.merge_count = Some(2);

        let manager = ContextManager::new(Some(8000), Some(&config));

        // First call may do NewSegment, so call a few times to reach discard
        let mut did_discard = false;
        for _ in 0..5 {
            let result = manager.maybe_truncate(&mut messages);
            if result.rounds_removed == 0 && result.messages_removed > 0 && !result.needs_compression {
                // Likely a discard
                if find_discard_notice_pos(&messages).is_some() {
                    did_discard = true;
                    break;
                }
            }
        }

        // Verify segment count went down or discard notice appeared
        let seg_count = messages.iter().filter(|m| is_summary_segment(m)).count();
        assert!(
            seg_count <= 6,
            "Segment count should decrease or stay same, got {}",
            seg_count
        );

        // Just verify no panics and something happened
        let _ = did_discard;
    }

    #[test]
    fn test_legacy_notice_recognized_as_summary_segment() {
        let mut messages = vec![
            make_system_message("sys"),
            make_legacy_notice("[Old compressed context notice]"),
        ];
        // Add several full rounds to push over threshold
        for i in 0..8 {
            let content = format!("User {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.min_keep_rounds = Some(3);
        config.max_summary_segments = Some(5);
        config.compression_token_threshold = Some(100);

        let manager = ContextManager::new(Some(8000), Some(&config));
        let result = manager.maybe_truncate(&mut messages);

        // Should not crash; legacy notice is treated as a summary segment
        assert!(result.messages_removed > 0 || result.rounds_removed > 0);
    }

    #[test]
    fn test_discard_notice_inserted_once() {
        let mut messages = vec![
            make_system_message("sys"),
            make_summary_segment("old seg 1", 2),
            make_summary_segment("old seg 2", 2),
            make_summary_segment("old seg 3", 2),
            make_summary_segment("old seg 4", 2),
            make_summary_segment("old seg 5", 2),
            make_summary_segment("old seg 6", 2),
        ];
        for i in 0..4 {
            let content = format!("User {}: {}", i, "x".repeat(2000));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.max_summary_segments = Some(5);
        config.max_merges_per_segment = Some(2);

        let manager = ContextManager::new(Some(6000), Some(&config));

        // Trigger a few discards
        for _ in 0..3 {
            let _ = manager.maybe_truncate(&mut messages);
        }

        // Count discard notices — should be at most 1
        let discard_count = messages.iter().filter(|m| is_discard_notice(m)).count();
        assert!(
            discard_count <= 1,
            "Should have at most 1 discard notice, found {}",
            discard_count
        );
    }

    #[test]
    fn test_multiple_progressive_rounds_gradual() {
        // Build a long history and verify compression happens gradually
        let mut messages = vec![make_system_message("sys")];
        for i in 0..20 {
            let content = format!("Round {} user message: {}", i, "x".repeat(1500));
            messages.push(make_user_message(&content));
        }

        let mut config = make_test_config();
        config.min_keep_rounds = Some(3);
        config.rounds_per_summary = Some(3);
        config.max_summary_segments = Some(4);
        config.compression_token_threshold = Some(100);

        let manager = ContextManager::new(Some(10000), Some(&config));

        let mut seg_count_before = 0;
        let mut did_new_segment = false;
        let mut did_merge = false;

        for round in 0..10 {
            let result = manager.maybe_truncate(&mut messages);

            let seg_count = messages.iter().filter(|m| is_summary_segment(m)).count();

            match &result.action {
                TruncationAction::NewSegment => {
                    did_new_segment = true;
                    assert_eq!(result.rounds_removed, 3);
                    assert!(seg_count > seg_count_before || seg_count_before == 0);
                }
                TruncationAction::MergeSegments { .. } => {
                    did_merge = true;
                    assert!(seg_count <= seg_count_before);
                }
                TruncationAction::TruncateOnly => {
                    // Could be discard or nothing
                }
            }

            seg_count_before = seg_count;

            let estimated = estimate_messages_tokens_with_margin(&messages, 1.3);
            if estimated <= manager.truncation_threshold() {
                break;
            }

            tracing::debug!("Round {}: segments={}, estimated={}", round, seg_count, estimated);
        }

        // With 20 rounds and a small window, we should see at least new segments
        assert!(did_new_segment, "Should have created at least one new summary segment");
        let _ = did_merge;
    }
}