serdes-ai-agent 0.2.6

Agent implementation for serdes-ai
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
//! Message history processing.
//!
//! History processors can modify the message history before it's sent to the model.
//! Common use cases include truncation, summarization, and filtering.

use crate::context::RunContext;
use async_trait::async_trait;
use serdes_ai_core::{ModelRequest, ModelRequestPart, ModelResponsePart};
use std::collections::HashSet;
use std::marker::PhantomData;

// Conditional tracing - use no-op macros when tracing feature is disabled
#[cfg(feature = "tracing-integration")]
use tracing::debug;

#[cfg(not(feature = "tracing-integration"))]
macro_rules! debug {
    ($($arg:tt)*) => {};
}

// ============================================================================
// Tool Pair Helpers
// ============================================================================

/// Extract all tool_call_ids from ToolCallPart in ModelResponse parts.
///
/// This looks for `ModelRequestPart::ModelResponse` and extracts `tool_call_id`
/// from any `ToolCallPart` or `BuiltinToolCallPart` within.
fn extract_tool_use_ids(message: &ModelRequest) -> Vec<String> {
    let mut ids = Vec::new();
    for part in &message.parts {
        if let ModelRequestPart::ModelResponse(response) = part {
            for response_part in &response.parts {
                match response_part {
                    ModelResponsePart::ToolCall(tc) => {
                        if let Some(id) = &tc.tool_call_id {
                            ids.push(id.clone());
                        }
                    }
                    ModelResponsePart::BuiltinToolCall(btc) => {
                        if let Some(id) = &btc.tool_call_id {
                            ids.push(id.clone());
                        }
                    }
                    _ => {}
                }
            }
        }
    }
    ids
}

/// Extract all tool_call_ids from ToolReturnPart and BuiltinToolReturnPart.
///
/// This looks for `ModelRequestPart::ToolReturn` and `ModelRequestPart::BuiltinToolReturn`
/// and extracts their `tool_call_id` values.
fn extract_tool_result_ids(message: &ModelRequest) -> Vec<String> {
    let mut ids = Vec::new();
    for part in &message.parts {
        match part {
            ModelRequestPart::ToolReturn(tr) => {
                if let Some(id) = &tr.tool_call_id {
                    ids.push(id.clone());
                }
            }
            ModelRequestPart::BuiltinToolReturn(btr) => {
                // BuiltinToolReturnPart has non-optional tool_call_id
                ids.push(btr.tool_call_id.clone());
            }
            ModelRequestPart::RetryPrompt(rp) => {
                // RetryPrompt can also have a tool_call_id
                if let Some(id) = &rp.tool_call_id {
                    ids.push(id.clone());
                }
            }
            _ => {}
        }
    }
    ids
}

/// Collect all tool_use IDs from a list of messages.
fn collect_all_tool_use_ids(messages: &[ModelRequest]) -> HashSet<String> {
    messages.iter().flat_map(extract_tool_use_ids).collect()
}

/// Collect all tool_result IDs from a list of messages.
fn collect_all_tool_result_ids(messages: &[ModelRequest]) -> HashSet<String> {
    messages.iter().flat_map(extract_tool_result_ids).collect()
}

/// Remove orphaned tool results from messages.
///
/// An orphaned tool_result is one whose `tool_call_id` has no corresponding
/// `tool_use` (ToolCallPart) in the message history. This can happen when
/// truncation removes earlier messages containing the tool_use.
///
/// This function modifies messages in-place, removing orphaned ToolReturn,
/// BuiltinToolReturn, and RetryPrompt parts. If a message becomes empty
/// after removal, it is filtered out entirely.
///
/// # Behavior Notes
///
/// **Asymmetric handling of `tool_call_id` is intentional:**
///
/// - `ToolReturn` and `RetryPrompt`: If `tool_call_id` is `None`, the part is KEPT.
///   This handles edge cases where tools may return results without IDs (e.g., legacy
///   tools, certain provider quirks). Keeping these is safe - the worst case is a
///   slightly larger context, but it won't cause API errors.
///
/// - `BuiltinToolReturn`: Has non-optional `tool_call_id: String`. Empty strings are
///   treated as invalid (removed) since they indicate malformed data that would likely
///   cause API errors anyway.
///
/// This design prioritizes avoiding false-positive removals over strict validation.
fn remove_orphaned_tool_results(
    messages: Vec<ModelRequest>,
    valid_tool_ids: &HashSet<String>,
) -> Vec<ModelRequest> {
    messages
        .into_iter()
        .filter_map(|mut msg| {
            msg.parts.retain(|part| {
                match part {
                    ModelRequestPart::ToolReturn(tr) => {
                        // INTENTIONAL: Keep if no tool_call_id (None) to handle edge cases
                        // gracefully. Only remove if we have an ID that doesn't match.
                        // See function docs for rationale.
                        let dominated = tr
                            .tool_call_id
                            .as_ref()
                            .map_or(true, |id| valid_tool_ids.contains(id));
                        if !dominated {
                            debug!(
                                tool_name = %tr.tool_name,
                                tool_call_id = ?tr.tool_call_id,
                                "Removing orphaned ToolReturn: no matching tool_use found"
                            );
                        }
                        dominated
                    }
                    ModelRequestPart::BuiltinToolReturn(btr) => {
                        // BuiltinToolReturn has non-optional tool_call_id: String
                        // Empty string is treated as invalid (likely malformed data)
                        let dominated = !btr.tool_call_id.is_empty()
                            && valid_tool_ids.contains(&btr.tool_call_id);
                        if !dominated {
                            debug!(
                                tool_name = %btr.tool_name,
                                tool_call_id = %btr.tool_call_id,
                                "Removing orphaned BuiltinToolReturn: no matching tool_use found"
                            );
                        }
                        dominated
                    }
                    ModelRequestPart::RetryPrompt(rp) => {
                        // INTENTIONAL: Keep if no tool_call_id (None) - same rationale as ToolReturn
                        let keep = rp
                            .tool_call_id
                            .as_ref()
                            .map_or(true, |id| valid_tool_ids.contains(id));
                        if !keep {
                            debug!(
                                tool_name = ?rp.tool_name,
                                tool_call_id = ?rp.tool_call_id,
                                "Removing orphaned RetryPrompt: no matching tool_use found"
                            );
                        }
                        keep
                    }
                    // Keep all other parts
                    _ => true,
                }
            });
            // Only keep messages that still have parts
            if msg.parts.is_empty() {
                None
            } else {
                Some(msg)
            }
        })
        .collect()
}

/// Remove orphaned tool uses from messages.
///
/// An orphaned tool_use is a `ToolCallPart` or `BuiltinToolCallPart` whose `tool_call_id`
/// has no corresponding `tool_result` (ToolReturnPart/BuiltinToolReturnPart) in the
/// message history. This can happen when truncation removes later messages containing
/// the tool_result.
///
/// This function modifies messages in-place:
/// 1. For each `ModelRequestPart::ModelResponse`, filter out orphaned ToolCall/BuiltinToolCall parts
/// 2. If the ModelResponse becomes empty after filtering, remove it from the message
/// 3. If the message becomes empty after removing ModelResponses, filter it out entirely
///
/// # Behavior Notes
///
/// **Asymmetric handling mirrors `remove_orphaned_tool_results`:**
///
/// - `ToolCallPart`: If `tool_call_id` is `None`, the part is KEPT (edge case handling).
/// - `BuiltinToolCallPart`: If `tool_call_id` is `None` or empty string, treated as invalid.
///
/// This ensures we don't accidentally remove tool calls that might be handled differently
/// by certain providers.
fn remove_orphaned_tool_uses(
    messages: Vec<ModelRequest>,
    valid_result_ids: &HashSet<String>,
) -> Vec<ModelRequest> {
    messages
        .into_iter()
        .filter_map(|mut msg| {
            // Process each part in the message
            msg.parts = msg.parts
                .into_iter()
                .filter_map(|part| {
                    match part {
                        ModelRequestPart::ModelResponse(mut response) => {
                            // Filter out orphaned tool calls from the response
                            response.parts.retain(|response_part| {
                                match response_part {
                                    ModelResponsePart::ToolCall(tc) => {
                                        // INTENTIONAL: Keep if no tool_call_id (None)
                                        let keep = tc.tool_call_id
                                            .as_ref()
                                            .map_or(true, |id| valid_result_ids.contains(id));
                                        if !keep {
                                            debug!(
                                                tool_name = %tc.tool_name,
                                                tool_call_id = ?tc.tool_call_id,
                                                "Removing orphaned ToolCall: no matching tool_result found"
                                            );
                                        }
                                        keep
                                    }
                                    ModelResponsePart::BuiltinToolCall(btc) => {
                                        // BuiltinToolCall has Option<String> tool_call_id
                                        let keep = btc.tool_call_id
                                            .as_ref()
                                            .map_or(true, |id| !id.is_empty() && valid_result_ids.contains(id));
                                        if !keep {
                                            debug!(
                                                tool_name = %btc.tool_name,
                                                tool_call_id = ?btc.tool_call_id,
                                                "Removing orphaned BuiltinToolCall: no matching tool_result found"
                                            );
                                        }
                                        keep
                                    }
                                    // Keep all other response parts (Text, Thinking, File, etc.)
                                    _ => true,
                                }
                            });

                            // Keep the ModelResponse only if it still has parts
                            if response.parts.is_empty() {
                                debug!("Removing empty ModelResponse after filtering orphaned tool calls");
                                None
                            } else {
                                Some(ModelRequestPart::ModelResponse(response))
                            }
                        }
                        // Keep all other request parts as-is
                        other => Some(other),
                    }
                })
                .collect();

            // Only keep messages that still have parts
            if msg.parts.is_empty() {
                None
            } else {
                Some(msg)
            }
        })
        .collect()
}

/// Trait for processing message history before model calls.
#[async_trait]
pub trait HistoryProcessor<Deps>: Send + Sync {
    /// Process the message history.
    ///
    /// Returns the modified history.
    async fn process(
        &self,
        ctx: &RunContext<Deps>,
        messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest>;
}

// ============================================================================
// Truncation Processors
// ============================================================================

/// Truncate history to keep only the most recent messages.
#[derive(Debug, Clone)]
pub struct TruncateHistory {
    /// Maximum number of messages to keep.
    max_messages: usize,
    /// Always keep the first message (usually system prompt).
    keep_first: bool,
}

impl TruncateHistory {
    /// Create a new truncation processor.
    pub fn new(max_messages: usize) -> Self {
        Self {
            max_messages,
            keep_first: true,
        }
    }

    /// Set whether to always keep the first message.
    pub fn keep_first(mut self, keep: bool) -> Self {
        self.keep_first = keep;
        self
    }
}

#[async_trait]
impl<Deps: Send + Sync> HistoryProcessor<Deps> for TruncateHistory {
    async fn process(
        &self,
        _ctx: &RunContext<Deps>,
        mut messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        if messages.len() <= self.max_messages {
            return messages;
        }

        let result = if self.keep_first && !messages.is_empty() {
            // Keep first message, truncate the rest
            let first = messages.remove(0);
            let keep_count = self.max_messages.saturating_sub(1);
            let start = messages.len().saturating_sub(keep_count);
            let mut result = vec![first];
            result.extend(messages.drain(start..));
            result
        } else {
            // Just keep the most recent
            let start = messages.len().saturating_sub(self.max_messages);
            messages.drain(start..).collect()
        };

        // Post-processing: Remove orphaned tool pairs
        // This prevents Claude API errors for both directions:
        // 1. "unexpected tool_use_id in tool_result" (orphaned results)
        // 2. "tool_use ids found without tool_result" (orphaned uses)
        let valid_tool_use_ids = collect_all_tool_use_ids(&result);
        let result = remove_orphaned_tool_results(result, &valid_tool_use_ids);

        let valid_tool_result_ids = collect_all_tool_result_ids(&result);
        remove_orphaned_tool_uses(result, &valid_tool_result_ids)
    }
}

/// Truncate based on token count.
#[derive(Debug, Clone)]
pub struct TruncateByTokens {
    /// Maximum tokens to keep.
    max_tokens: u64,
    /// Token estimator (chars per token).
    chars_per_token: f64,
    /// Number of messages to always keep at the beginning (e.g., system prompt + first user message).
    keep_first_n: usize,
}

impl TruncateByTokens {
    /// Create a new token-based truncation processor.
    ///
    /// By default, keeps the first 2 messages (system prompt + first user message).
    pub fn new(max_tokens: u64) -> Self {
        Self {
            max_tokens,
            chars_per_token: 4.0, // Reasonable default for English
            keep_first_n: 2,
        }
    }

    /// Set chars per token ratio.
    pub fn chars_per_token(mut self, ratio: f64) -> Self {
        self.chars_per_token = ratio;
        self
    }

    /// Set the number of messages to keep at the beginning.
    ///
    /// Common values:
    /// - 0: Don't preserve any messages at the start
    /// - 1: Keep just the system prompt
    /// - 2: Keep system prompt + first user message (default)
    pub fn keep_first_n(mut self, n: usize) -> Self {
        self.keep_first_n = n;
        self
    }

    /// Set whether to keep the first message.
    ///
    /// **Deprecated:** Use `keep_first_n()` instead for more control.
    ///
    /// - `keep: true` sets `keep_first_n` to 1
    /// - `keep: false` sets `keep_first_n` to 0
    pub fn keep_first(mut self, keep: bool) -> Self {
        self.keep_first_n = if keep { 1 } else { 0 };
        self
    }

    fn estimate_tokens(&self, message: &ModelRequest) -> u64 {
        let chars: usize = message
            .parts
            .iter()
            .map(|p| {
                match p {
                    serdes_ai_core::ModelRequestPart::SystemPrompt(s) => s.content.len(),
                    serdes_ai_core::ModelRequestPart::UserPrompt(u) => {
                        // Estimate based on content
                        match &u.content {
                            serdes_ai_core::messages::UserContent::Text(t) => t.len(),
                            serdes_ai_core::messages::UserContent::Parts(parts) => {
                                parts
                                    .iter()
                                    .map(|p| {
                                        match p {
                                            serdes_ai_core::messages::UserContentPart::Text {
                                                text,
                                            } => text.len(),
                                            _ => 100, // Estimate for non-text
                                        }
                                    })
                                    .sum()
                            }
                        }
                    }
                    serdes_ai_core::ModelRequestPart::ToolReturn(t) => {
                        t.content.to_string_content().len()
                    }
                    serdes_ai_core::ModelRequestPart::RetryPrompt(r) => r.content.message().len(),
                    serdes_ai_core::ModelRequestPart::BuiltinToolReturn(b) => {
                        // Estimate based on content type
                        b.content_type().len() + 100
                    }
                    serdes_ai_core::ModelRequestPart::ModelResponse(r) => {
                        // Estimate based on response parts
                        r.parts
                            .iter()
                            .map(|p| match p {
                                serdes_ai_core::ModelResponsePart::Text(t) => t.content.len(),
                                serdes_ai_core::ModelResponsePart::ToolCall(tc) => {
                                    tc.tool_name.len()
                                        + tc.args.to_json_string().map(|s| s.len()).unwrap_or(50)
                                }
                                serdes_ai_core::ModelResponsePart::Thinking(t) => t.content.len(),
                                serdes_ai_core::ModelResponsePart::File(_) => 100,
                                serdes_ai_core::ModelResponsePart::BuiltinToolCall(_) => 100,
                            })
                            .sum::<usize>()
                    }
                }
            })
            .sum();

        (chars as f64 / self.chars_per_token).ceil() as u64
    }
}

#[async_trait]
impl<Deps: Send + Sync> HistoryProcessor<Deps> for TruncateByTokens {
    async fn process(
        &self,
        _ctx: &RunContext<Deps>,
        messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        if messages.is_empty() {
            return messages;
        }

        let mut result = Vec::new();
        let mut total_tokens = 0u64;

        // How many messages to unconditionally keep at the start
        let keep_n = self.keep_first_n.min(messages.len());

        // Add the first N messages unconditionally
        for msg in messages.iter().take(keep_n) {
            let tokens = self.estimate_tokens(msg);
            result.push(msg.clone());
            total_tokens += tokens;
        }

        // Iterate through remaining messages from the end
        let remaining = &messages[keep_n..];
        let mut to_append = Vec::new();

        for msg in remaining.iter().rev() {
            let tokens = self.estimate_tokens(msg);
            if total_tokens + tokens > self.max_tokens {
                break;
            }
            total_tokens += tokens;
            to_append.push(msg.clone());
        }

        // Reverse and append (we iterated backwards)
        to_append.reverse();
        result.extend(to_append);

        // Post-processing: Remove orphaned tool pairs
        // This prevents Claude API errors for both directions:
        // 1. "unexpected tool_use_id in tool_result" (orphaned results)
        // 2. "tool_use ids found without tool_result" (orphaned uses)
        let valid_tool_use_ids = collect_all_tool_use_ids(&result);
        let result = remove_orphaned_tool_results(result, &valid_tool_use_ids);

        let valid_tool_result_ids = collect_all_tool_result_ids(&result);
        remove_orphaned_tool_uses(result, &valid_tool_result_ids)
    }
}

// ============================================================================
// Filter Processors
// ============================================================================

/// Filter out specific message types.
#[derive(Debug, Clone)]
pub struct FilterHistory {
    /// Remove system prompts.
    remove_system: bool,
    /// Remove tool returns.
    remove_tool_returns: bool,
    /// Remove retry prompts.
    remove_retries: bool,
}

impl FilterHistory {
    /// Create a new filter processor.
    pub fn new() -> Self {
        Self {
            remove_system: false,
            remove_tool_returns: false,
            remove_retries: false,
        }
    }

    /// Remove system prompts.
    pub fn remove_system(mut self, remove: bool) -> Self {
        self.remove_system = remove;
        self
    }

    /// Remove tool returns.
    pub fn remove_tool_returns(mut self, remove: bool) -> Self {
        self.remove_tool_returns = remove;
        self
    }

    /// Remove retry prompts.
    pub fn remove_retries(mut self, remove: bool) -> Self {
        self.remove_retries = remove;
        self
    }
}

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

#[async_trait]
impl<Deps: Send + Sync> HistoryProcessor<Deps> for FilterHistory {
    async fn process(
        &self,
        _ctx: &RunContext<Deps>,
        messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        messages
            .into_iter()
            .map(|mut msg| {
                msg.parts.retain(|part| {
                    use serdes_ai_core::ModelRequestPart::*;
                    match part {
                        SystemPrompt(_) => !self.remove_system,
                        ToolReturn(_) => !self.remove_tool_returns,
                        RetryPrompt(_) => !self.remove_retries,
                        _ => true,
                    }
                });
                msg
            })
            .filter(|msg| !msg.parts.is_empty())
            .collect()
    }
}

// ============================================================================
// Combination Processors
// ============================================================================

/// Chain multiple processors together.
pub struct ChainedProcessor<Deps> {
    processors: Vec<Box<dyn HistoryProcessor<Deps>>>,
}

impl<Deps: Send + Sync + 'static> ChainedProcessor<Deps> {
    /// Create a new chained processor.
    pub fn new() -> Self {
        Self {
            processors: Vec::new(),
        }
    }

    /// Add a processor to the chain.
    #[allow(clippy::should_implement_trait)]
    pub fn add<P: HistoryProcessor<Deps> + 'static>(mut self, processor: P) -> Self {
        self.processors.push(Box::new(processor));
        self
    }
}

impl<Deps: Send + Sync + 'static> Default for ChainedProcessor<Deps> {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<Deps: Send + Sync> HistoryProcessor<Deps> for ChainedProcessor<Deps> {
    async fn process(
        &self,
        ctx: &RunContext<Deps>,
        mut messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        for processor in &self.processors {
            messages = processor.process(ctx, messages).await;
        }
        messages
    }
}

// ============================================================================
// Summarization (Placeholder)
// ============================================================================

/// Summarize old messages using a model.
///
/// This is a placeholder - actual implementation would require
/// calling a model to generate summaries.
#[derive(Debug, Clone)]
pub struct SummarizeHistory {
    /// Number of recent messages to keep.
    keep_recent: usize,
    /// Token threshold before summarization.
    #[allow(dead_code)]
    threshold_tokens: u64,
}

impl SummarizeHistory {
    /// Create a new summarization processor.
    pub fn new(keep_recent: usize, threshold_tokens: u64) -> Self {
        Self {
            keep_recent,
            threshold_tokens,
        }
    }
}

#[async_trait]
impl<Deps: Send + Sync> HistoryProcessor<Deps> for SummarizeHistory {
    async fn process(
        &self,
        _ctx: &RunContext<Deps>,
        messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        // For now, just truncate. Full implementation would:
        // 1. Estimate tokens
        // 2. If above threshold, summarize older messages
        // 3. Replace old messages with summary
        if messages.len() <= self.keep_recent {
            return messages;
        }

        // Keep the most recent messages
        let start = messages.len().saturating_sub(self.keep_recent);
        messages[start..].to_vec()
    }
}

// ============================================================================
// Custom Processor
// ============================================================================

/// Processor that uses a custom function.
pub struct FnProcessor<F, Deps>
where
    F: Fn(&RunContext<Deps>, Vec<ModelRequest>) -> Vec<ModelRequest> + Send + Sync,
{
    func: F,
    _phantom: PhantomData<Deps>,
}

impl<F, Deps> FnProcessor<F, Deps>
where
    F: Fn(&RunContext<Deps>, Vec<ModelRequest>) -> Vec<ModelRequest> + Send + Sync,
{
    /// Create a new function processor.
    pub fn new(func: F) -> Self {
        Self {
            func,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps> HistoryProcessor<Deps> for FnProcessor<F, Deps>
where
    F: Fn(&RunContext<Deps>, Vec<ModelRequest>) -> Vec<ModelRequest> + Send + Sync,
    Deps: Send + Sync,
{
    async fn process(
        &self,
        ctx: &RunContext<Deps>,
        messages: Vec<ModelRequest>,
    ) -> Vec<ModelRequest> {
        (self.func)(ctx, messages)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use std::sync::Arc;

    fn make_test_context() -> RunContext<()> {
        RunContext {
            deps: Arc::new(()),
            run_id: "test".to_string(),
            start_time: Utc::now(),
            model_name: "test".to_string(),
            model_settings: Default::default(),
            tool_name: None,
            tool_call_id: None,
            retry_count: 0,
            metadata: None,
        }
    }

    fn make_messages(count: usize) -> Vec<ModelRequest> {
        (0..count)
            .map(|i| {
                let mut req = ModelRequest::new();
                req.add_user_prompt(format!("Message {}", i));
                req
            })
            .collect()
    }

    #[tokio::test]
    async fn test_truncate_history() {
        let processor = TruncateHistory::new(3).keep_first(false);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_truncate_keep_first() {
        let processor = TruncateHistory::new(3).keep_first(true);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_truncate_no_change() {
        let processor = TruncateHistory::new(10);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 5);
    }

    #[tokio::test]
    async fn test_chained_processor() {
        let processor = ChainedProcessor::<()>::new()
            .add(TruncateHistory::new(5))
            .add(TruncateHistory::new(3));

        let ctx = make_test_context();
        let messages = make_messages(10);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_fn_processor() {
        let processor = FnProcessor::new(|_ctx: &RunContext<()>, mut msgs: Vec<ModelRequest>| {
            msgs.pop();
            msgs
        });

        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 4);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_default_keeps_first_two() {
        // Default behavior: keep_first_n = 2 (system prompt + first user message)
        let processor = TruncateByTokens::new(1); // Very low token limit
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        // Should keep at least the first 2 messages even with low token limit
        assert!(result.len() >= 2);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_keep_first_n() {
        // Explicitly set keep_first_n to 3
        let processor = TruncateByTokens::new(1).keep_first_n(3);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        // Should keep at least the first 3 messages
        assert!(result.len() >= 3);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_keep_first_n_zero() {
        // Set keep_first_n to 0 (don't preserve any)
        let processor = TruncateByTokens::new(1).keep_first_n(0);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        // With very low token limit and no preserved messages, might get 0 or 1
        assert!(result.len() <= 1);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_backwards_compat_keep_first_true() {
        // Using deprecated keep_first(true) should set keep_first_n to 1
        let processor = TruncateByTokens::new(1).keep_first(true);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        // Should keep at least the first message
        assert!(!result.is_empty());
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_backwards_compat_keep_first_false() {
        // Using deprecated keep_first(false) should set keep_first_n to 0
        let processor = TruncateByTokens::new(1).keep_first(false);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        // With very low token limit and keep_first_n=0, might get 0 or 1
        assert!(result.len() <= 1);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_with_sufficient_tokens() {
        // With enough tokens, should keep all messages
        let processor = TruncateByTokens::new(10000);
        let ctx = make_test_context();
        let messages = make_messages(5);

        let result = processor.process(&ctx, messages).await;
        assert_eq!(result.len(), 5);
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_keeps_most_recent() {
        // Should keep the first N + most recent messages
        let processor = TruncateByTokens::new(100).keep_first_n(1); // Keep first + some recent
        let ctx = make_test_context();
        let messages = make_messages(10);

        let result = processor.process(&ctx, messages).await;
        // First message should always be present
        assert!(!result.is_empty());
    }

    // ========================================================================
    // Tool Pair Aware Truncation Tests
    // ========================================================================

    use serdes_ai_core::{
        messages::tool_return::ToolReturnContent, ModelResponse, ToolCallPart, ToolReturnPart,
    };

    /// Create a message with a tool call (ModelResponse containing ToolCallPart)
    fn make_tool_call_message(tool_call_id: &str) -> ModelRequest {
        let mut response = ModelResponse::new();
        let tool_call = ToolCallPart::new("test_tool", serde_json::json!({"arg": "value"}))
            .with_tool_call_id(tool_call_id);
        response.add_part(ModelResponsePart::ToolCall(tool_call));

        ModelRequest::with_parts(vec![ModelRequestPart::ModelResponse(Box::new(response))])
    }

    /// Create a message with a tool return (ToolReturnPart)
    fn make_tool_return_message(tool_call_id: &str) -> ModelRequest {
        let tool_return = ToolReturnPart::new("test_tool", ToolReturnContent::text("result"))
            .with_tool_call_id(tool_call_id);

        ModelRequest::with_parts(vec![ModelRequestPart::ToolReturn(tool_return)])
    }

    #[test]
    fn test_extract_tool_use_ids() {
        let msg = make_tool_call_message("call_123");
        let ids = extract_tool_use_ids(&msg);
        assert_eq!(ids, vec!["call_123"]);
    }

    #[test]
    fn test_extract_tool_use_ids_empty() {
        let msg = make_messages(1).pop().unwrap();
        let ids = extract_tool_use_ids(&msg);
        assert!(ids.is_empty());
    }

    #[test]
    fn test_extract_tool_result_ids() {
        let msg = make_tool_return_message("call_456");
        let ids = extract_tool_result_ids(&msg);
        assert_eq!(ids, vec!["call_456"]);
    }

    #[test]
    fn test_extract_tool_result_ids_empty() {
        let msg = make_messages(1).pop().unwrap();
        let ids = extract_tool_result_ids(&msg);
        assert!(ids.is_empty());
    }

    #[test]
    fn test_remove_orphaned_tool_results() {
        // Create messages with a tool call and a tool return
        let tool_call_msg = make_tool_call_message("call_abc");
        let tool_return_msg = make_tool_return_message("call_abc");
        let orphan_return_msg = make_tool_return_message("call_orphan");

        let messages = vec![tool_call_msg, tool_return_msg, orphan_return_msg];
        let valid_ids = collect_all_tool_use_ids(&messages);

        // valid_ids should only contain "call_abc"
        assert!(valid_ids.contains("call_abc"));
        assert!(!valid_ids.contains("call_orphan"));

        let result = remove_orphaned_tool_results(messages, &valid_ids);

        // Should have 2 messages: tool_call and matching tool_return
        // The orphan_return_msg should be removed entirely (it only had the orphan part)
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_remove_orphaned_preserves_mixed_messages() {
        // Create a message that has both a user prompt AND an orphaned tool return
        let mut mixed_msg = ModelRequest::new();
        mixed_msg.add_user_prompt("This is a user message");
        let orphan_return =
            ToolReturnPart::new("test_tool", ToolReturnContent::text("orphan result"))
                .with_tool_call_id("orphan_id");
        mixed_msg.add_part(ModelRequestPart::ToolReturn(orphan_return));

        let messages = vec![mixed_msg];
        let valid_ids: HashSet<String> = HashSet::new(); // No valid IDs

        let result = remove_orphaned_tool_results(messages, &valid_ids);

        // Message should still exist because it has a user prompt
        assert_eq!(result.len(), 1);
        // But it should only have 1 part (the user prompt, not the orphan return)
        assert_eq!(result[0].parts.len(), 1);
        assert!(matches!(
            result[0].parts[0],
            ModelRequestPart::UserPrompt(_)
        ));
    }

    #[tokio::test]
    async fn test_truncate_history_removes_orphaned_tool_results() {
        // Create a conversation with tool interactions
        // Message 0: User prompt
        // Message 1: Tool call (call_1)
        // Message 2: Tool return (call_1)
        // Message 3: Tool call (call_2)
        // Message 4: Tool return (call_2)
        let mut messages = Vec::new();

        let mut user_msg = ModelRequest::new();
        user_msg.add_user_prompt("Hello");
        messages.push(user_msg);

        messages.push(make_tool_call_message("call_1"));
        messages.push(make_tool_return_message("call_1"));
        messages.push(make_tool_call_message("call_2"));
        messages.push(make_tool_return_message("call_2"));

        // Truncate to 3 messages (keeping last 3)
        // This would normally keep: tool_return(call_1), tool_call(call_2), tool_return(call_2)
        // But tool_return(call_1) is orphaned because tool_call(call_1) was truncated
        let processor = TruncateHistory::new(3).keep_first(false);
        let ctx = make_test_context();

        let result = processor.process(&ctx, messages).await;

        // The orphaned tool_return(call_1) should be removed
        // So we should have: tool_call(call_2), tool_return(call_2)
        assert_eq!(result.len(), 2);

        // Verify no orphaned tool results
        let tool_use_ids = collect_all_tool_use_ids(&result);
        let tool_result_ids = collect_all_tool_result_ids(&result);

        // All tool_result_ids should have matching tool_use_ids
        for id in &tool_result_ids {
            assert!(
                tool_use_ids.contains(id),
                "Orphaned tool_result found: {}",
                id
            );
        }
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_removes_orphaned_tool_results() {
        // Similar test but for TruncateByTokens
        let mut messages = Vec::new();

        let mut user_msg = ModelRequest::new();
        user_msg.add_user_prompt("Hello");
        messages.push(user_msg);

        messages.push(make_tool_call_message("call_a"));
        messages.push(make_tool_return_message("call_a"));
        messages.push(make_tool_call_message("call_b"));
        messages.push(make_tool_return_message("call_b"));

        // Use a small token limit to force truncation
        let processor = TruncateByTokens::new(200).keep_first_n(0);
        let ctx = make_test_context();

        let result = processor.process(&ctx, messages).await;

        // Verify no orphaned tool results
        let tool_use_ids = collect_all_tool_use_ids(&result);
        let tool_result_ids = collect_all_tool_result_ids(&result);

        for id in &tool_result_ids {
            assert!(
                tool_use_ids.contains(id),
                "Orphaned tool_result found: {}",
                id
            );
        }
    }

    #[tokio::test]
    async fn test_tool_pair_aware_truncation_keeps_complete_pairs() {
        // Test that when we have complete pairs, they're preserved
        let messages = vec![
            make_tool_call_message("call_x"),
            make_tool_return_message("call_x"),
        ];

        let processor = TruncateByTokens::new(10000).keep_first_n(0);
        let ctx = make_test_context();

        let result = processor.process(&ctx, messages).await;

        // Both should be preserved (no truncation needed)
        assert_eq!(result.len(), 2);

        let tool_use_ids = collect_all_tool_use_ids(&result);
        let tool_result_ids = collect_all_tool_result_ids(&result);

        assert_eq!(tool_use_ids.len(), 1);
        assert_eq!(tool_result_ids.len(), 1);
        assert!(tool_use_ids.contains("call_x"));
        assert!(tool_result_ids.contains("call_x"));
    }

    #[test]
    fn test_collect_all_tool_use_ids() {
        let messages = vec![
            make_tool_call_message("id_1"),
            make_tool_call_message("id_2"),
            make_tool_return_message("id_1"),
        ];

        let ids = collect_all_tool_use_ids(&messages);

        assert_eq!(ids.len(), 2);
        assert!(ids.contains("id_1"));
        assert!(ids.contains("id_2"));
    }

    #[test]
    fn test_collect_all_tool_result_ids() {
        let messages = vec![
            make_tool_call_message("id_1"),
            make_tool_return_message("id_1"),
            make_tool_return_message("id_2"),
        ];

        let ids = collect_all_tool_result_ids(&messages);

        assert_eq!(ids.len(), 2);
        assert!(ids.contains("id_1"));
        assert!(ids.contains("id_2"));
    }

    #[test]
    fn test_tool_return_with_none_id_is_kept() {
        // ToolReturn with None tool_call_id should be kept (intentional edge case handling)
        let tool_return_no_id = ToolReturnPart::new("test_tool", ToolReturnContent::text("result"));
        // Note: no .with_tool_call_id() - so it's None

        let msg = ModelRequest::with_parts(vec![ModelRequestPart::ToolReturn(tool_return_no_id)]);

        let messages = vec![msg];
        let valid_ids: HashSet<String> = HashSet::new(); // No valid IDs

        let result = remove_orphaned_tool_results(messages, &valid_ids);

        // Should be kept because tool_call_id is None
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].parts.len(), 1);
    }

    #[test]
    fn test_builtin_tool_return_with_empty_string_id_is_removed() {
        use serdes_ai_core::messages::parts::{BuiltinToolReturnContent, WebSearchResults};

        // BuiltinToolReturn with empty string tool_call_id should be removed
        let empty_results = WebSearchResults::new("query", vec![]);
        let content = BuiltinToolReturnContent::web_search(empty_results);
        // Create with empty string ID - this is malformed data
        let builtin_return = serdes_ai_core::BuiltinToolReturnPart::new(
            "web_search",
            content,
            "", // Empty string!
        );

        let msg =
            ModelRequest::with_parts(vec![ModelRequestPart::BuiltinToolReturn(builtin_return)]);

        let messages = vec![msg];
        // Even with an empty valid_ids set, empty string should be treated as invalid
        let valid_ids: HashSet<String> = HashSet::new();

        let result = remove_orphaned_tool_results(messages, &valid_ids);

        // Should be removed because empty string ID is invalid
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_builtin_tool_return_with_valid_id_is_kept() {
        use serdes_ai_core::messages::parts::{BuiltinToolReturnContent, WebSearchResults};

        // BuiltinToolReturn with matching ID should be kept
        let empty_results = WebSearchResults::new("query", vec![]);
        let content = BuiltinToolReturnContent::web_search(empty_results);
        let builtin_return =
            serdes_ai_core::BuiltinToolReturnPart::new("web_search", content, "valid_call_id");

        let msg =
            ModelRequest::with_parts(vec![ModelRequestPart::BuiltinToolReturn(builtin_return)]);

        let messages = vec![msg];
        let mut valid_ids: HashSet<String> = HashSet::new();
        valid_ids.insert("valid_call_id".to_string());

        let result = remove_orphaned_tool_results(messages, &valid_ids);

        // Should be kept because ID matches
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].parts.len(), 1);
    }

    // ========================================================================
    // Remove Orphaned Tool Uses Tests
    // ========================================================================

    #[test]
    fn test_remove_orphaned_tool_uses_basic() {
        // Create a tool call without matching tool result
        let orphan_call_msg = make_tool_call_message("orphan_call");

        let messages = vec![orphan_call_msg];
        let valid_result_ids: HashSet<String> = HashSet::new(); // No matching results

        let result = remove_orphaned_tool_uses(messages, &valid_result_ids);

        // The message should be removed because the ModelResponse becomes empty
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_remove_orphaned_tool_uses_keeps_matched() {
        // Create a tool call with matching tool result ID
        let tool_call_msg = make_tool_call_message("matched_call");

        let messages = vec![tool_call_msg];
        let mut valid_result_ids: HashSet<String> = HashSet::new();
        valid_result_ids.insert("matched_call".to_string());

        let result = remove_orphaned_tool_uses(messages, &valid_result_ids);

        // Should be kept because there's a matching result
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_remove_orphaned_tool_uses_preserves_text() {
        // Create a ModelResponse with both text and an orphaned tool call
        let mut response = ModelResponse::new();
        response.add_part(ModelResponsePart::Text(serdes_ai_core::TextPart::new(
            "Some text",
        )));
        let tool_call = ToolCallPart::new("test_tool", serde_json::json!({"arg": "value"}))
            .with_tool_call_id("orphan_id");
        response.add_part(ModelResponsePart::ToolCall(tool_call));

        let msg =
            ModelRequest::with_parts(vec![ModelRequestPart::ModelResponse(Box::new(response))]);

        let messages = vec![msg];
        let valid_result_ids: HashSet<String> = HashSet::new(); // No matching results

        let result = remove_orphaned_tool_uses(messages, &valid_result_ids);

        // Message should be kept because it still has the Text part
        assert_eq!(result.len(), 1);

        // Check that the ModelResponse only has the Text part now
        if let ModelRequestPart::ModelResponse(ref resp) = result[0].parts[0] {
            assert_eq!(resp.parts.len(), 1);
            assert!(matches!(resp.parts[0], ModelResponsePart::Text(_)));
        } else {
            panic!("Expected ModelResponse");
        }
    }

    #[test]
    fn test_remove_orphaned_tool_uses_with_none_id_is_kept() {
        // ToolCallPart with None tool_call_id should be kept
        let mut response = ModelResponse::new();
        let tool_call = ToolCallPart::new("test_tool", serde_json::json!({"arg": "value"}));
        // Note: no .with_tool_call_id() - so it's None
        response.add_part(ModelResponsePart::ToolCall(tool_call));

        let msg =
            ModelRequest::with_parts(vec![ModelRequestPart::ModelResponse(Box::new(response))]);

        let messages = vec![msg];
        let valid_result_ids: HashSet<String> = HashSet::new(); // No valid IDs

        let result = remove_orphaned_tool_uses(messages, &valid_result_ids);

        // Should be kept because tool_call_id is None
        assert_eq!(result.len(), 1);
    }

    #[tokio::test]
    async fn test_truncate_removes_both_orphaned_directions() {
        // Test that truncation removes orphans in BOTH directions:
        // - Orphaned tool_results (tool_use was truncated)
        // - Orphaned tool_uses (tool_result was truncated)

        // Message 0: Tool call (call_1) - will be kept
        // Message 1: Tool return (call_1) - will be truncated
        // Message 2: Tool call (call_2) - will be truncated
        // Message 3: Tool return (call_2) - will be kept
        let messages = vec![
            make_tool_call_message("call_1"),   // Index 0
            make_tool_return_message("call_1"), // Index 1
            make_tool_call_message("call_2"),   // Index 2
            make_tool_return_message("call_2"), // Index 3
        ];

        // Truncate to 2 messages, keeping first (keep_first=true, max=2)
        // This keeps message 0 and 3: tool_call(1) and tool_return(2)
        // Both are orphaned!
        let processor = TruncateHistory::new(2).keep_first(true);
        let ctx = make_test_context();

        let result = processor.process(&ctx, messages).await;

        // Both orphans should be removed
        // The result should have no tool calls or returns with mismatched IDs
        let tool_use_ids = collect_all_tool_use_ids(&result);
        let tool_result_ids = collect_all_tool_result_ids(&result);

        // All remaining tool_results should have matching tool_uses
        for id in &tool_result_ids {
            assert!(
                tool_use_ids.contains(id),
                "Orphaned tool_result found: {}",
                id
            );
        }

        // All remaining tool_uses should have matching tool_results
        for id in &tool_use_ids {
            assert!(
                tool_result_ids.contains(id),
                "Orphaned tool_use found: {}",
                id
            );
        }
    }

    #[tokio::test]
    async fn test_truncate_by_tokens_removes_both_orphaned_directions() {
        // Same test but for TruncateByTokens
        let messages = vec![
            make_tool_call_message("call_a"),
            make_tool_return_message("call_a"),
            make_tool_call_message("call_b"),
            make_tool_return_message("call_b"),
        ];

        let processor = TruncateByTokens::new(50).keep_first_n(0); // Very low to force truncation
        let ctx = make_test_context();

        let result = processor.process(&ctx, messages).await;

        // Verify bidirectional consistency
        let tool_use_ids = collect_all_tool_use_ids(&result);
        let tool_result_ids = collect_all_tool_result_ids(&result);

        for id in &tool_result_ids {
            assert!(
                tool_use_ids.contains(id),
                "Orphaned tool_result found: {}",
                id
            );
        }

        for id in &tool_use_ids {
            assert!(
                tool_result_ids.contains(id),
                "Orphaned tool_use found: {}",
                id
            );
        }
    }
}