sombrax_agentic_core 0.1.1

SombraX Agentic Core (SAC) — a provider-agnostic Rust library for building LLM agents: content-modifying hooks, tool/MCP integration, and context optimization.
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
//! File-History Context Management - Message Classification
//!
//! This module implements classification of conversation messages for file-based
//! context management, enabling de-duplication of file reads/edits and ensuring
//! the latest file content is maintained at the end of the dialog.
//!
//! # Architecture Overview
//!
//! The classification system operates on conversation messages to:
//! 1. Extract metadata signals from tool calls and results
//! 2. Track file artifacts and their versions
//! 3. Apply supersedence rules to determine which messages to keep/drop
//! 4. Reorder context to place latest file snapshots at the end
//!
//! # Classification Signals
//!
//! Each message is classified based on:
//! - **Message Role**: `user` (tool results) or `assistant` (tool calls)
//! - **Operation Type**: `read`, `write`, `edit`, or `other`
//! - **File Path**: Normalized absolute path to the file
//! - **Line Range**: Optional `(offset, limit)` for partial reads
//! - **Content Hash**: BLAKE3 hash of file content for version tracking
//! - **Message ID**: Unique identifier for the message
//! - **Timestamp**: Monotonic sequence number for ordering
//!
//! # Important Design Notes
//!
//! ## Classification Keys
//!
//! Classifications are keyed by a composite key of `(message_id, tool_call_id)` to
//! handle multiple tool calls in a single assistant message correctly. Each tool
//! call gets its own classification entry.
//!
//! ## Unclassified Messages
//!
//! The optimization result only tracks file-related operations. Messages without
//! file operations (normal chat, non-file tools) are not classified and should
//! be preserved by default. Use `is_classified()` to check if a message ID has
//! any classifications before applying optimization decisions.

pub mod edge_cases;
pub mod hook;
#[cfg(test)]
mod test_transcripts;

pub use hook::{FileContextExt, FileContextHook, FILE_CONTEXT_KEY};

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};

// ============================================================================
// Classification Metadata Types
// ============================================================================

/// Operation type extracted from tool calls/results
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FileOperation {
    /// Full or partial file read (read tool)
    Read,
    /// Complete file write/creation (write tool)
    Write,
    /// Patch/edit to existing file (edit tool)
    Edit,
    /// Non-file operation
    Other,
}

impl FileOperation {
    /// Returns true if this operation produces a full file snapshot
    pub fn is_full_snapshot(&self) -> bool {
        matches!(self, FileOperation::Write)
    }

    /// Returns true if this operation reads file content
    pub fn is_read(&self) -> bool {
        matches!(self, FileOperation::Read)
    }

    /// Returns true if this operation modifies file content
    pub fn is_mutating(&self) -> bool {
        matches!(self, FileOperation::Write | FileOperation::Edit)
    }
}

/// Line range for partial file reads
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LineRange {
    /// Starting line (0-indexed offset)
    pub offset: usize,
    /// Maximum lines to read (None = unlimited)
    pub limit: Option<usize>,
}

impl LineRange {
    /// Create a new line range
    pub fn new(offset: usize, limit: Option<usize>) -> Self {
        Self { offset, limit }
    }

    /// Check if this is a full file read (offset=0, no limit)
    pub fn is_full_read(&self) -> bool {
        self.offset == 0 && self.limit.is_none()
    }

    /// Check if this range contains another range
    pub fn contains(&self, other: &LineRange) -> bool {
        // This range contains other if:
        // 1. This starts at or before other
        // 2. This ends at or after other (or is unlimited)
        if self.offset > other.offset {
            return false;
        }

        match (self.limit, other.limit) {
            (None, _) => true,        // Unlimited contains everything after offset
            (Some(_), None) => false, // Limited cannot contain unlimited
            (Some(self_limit), Some(other_limit)) => {
                let self_end = self.offset + self_limit;
                let other_end = other.offset + other_limit;
                self_end >= other_end
            }
        }
    }

    /// Check if two ranges overlap
    pub fn overlaps(&self, other: &LineRange) -> bool {
        let self_start = self.offset;
        let other_start = other.offset;

        match (self.limit, other.limit) {
            (None, None) => true, // Both unlimited overlap
            (None, Some(_)) => {
                // self is unlimited, overlaps if other starts >= self start
                // OR if other ends after self starts
                other_start >= self_start || other_start + other.limit.unwrap_or(0) > self_start
            }
            (Some(_), None) => {
                // other is unlimited, overlaps if self starts >= other start
                // OR if self ends after other starts
                self_start >= other_start || self_start + self.limit.unwrap_or(0) > other_start
            }
            (Some(self_limit), Some(other_limit)) => {
                let self_end = self_start + self_limit;
                let other_end = other_start + other_limit;
                self_start < other_end && other_start < self_end
            }
        }
    }
}

/// Content hash for file version tracking (using BLAKE3 for speed)
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ContentHash(pub String);

impl ContentHash {
    /// Compute hash from content string
    pub fn from_content(content: &str) -> Self {
        let hash = blake3::hash(content.as_bytes());
        Self(hash.to_hex().to_string())
    }

    /// Create from pre-computed hex string
    pub fn from_hex(hex: impl Into<String>) -> Self {
        Self(hex.into())
    }
}

/// Composite key for classification lookups
///
/// This enables tracking multiple tool calls within a single message.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ClassificationKey {
    /// The message ID
    pub message_id: String,
    /// The tool call ID (unique per tool call)
    pub tool_call_id: String,
}

impl ClassificationKey {
    /// Create a new classification key
    pub fn new(message_id: impl Into<String>, tool_call_id: impl Into<String>) -> Self {
        Self {
            message_id: message_id.into(),
            tool_call_id: tool_call_id.into(),
        }
    }
}

impl std::fmt::Display for ClassificationKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.message_id, self.tool_call_id)
    }
}

/// Classification metadata extracted from a message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageClassification {
    /// Unique message identifier
    pub message_id: String,
    /// Tool call ID (unique per tool call within a message)
    pub tool_call_id: String,
    /// Monotonic sequence number for ordering
    pub sequence: u64,
    /// Message role (user for tool results, assistant for tool calls)
    pub role: MessageRole,
    /// Type of file operation
    pub operation: FileOperation,
    /// Normalized file path (if file-related)
    pub file_path: Option<PathBuf>,
    /// Line range for partial reads
    pub line_range: Option<LineRange>,
    /// Content hash for version tracking
    pub content_hash: Option<ContentHash>,
    /// Size of content in bytes (for prioritization)
    pub content_size: usize,
}

impl MessageClassification {
    /// Get the classification key for this classification
    pub fn key(&self) -> ClassificationKey {
        ClassificationKey::new(&self.message_id, &self.tool_call_id)
    }
}

/// Message role in classification context
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageRole {
    /// User message (contains tool results)
    User,
    /// Assistant message (contains tool calls)
    Assistant,
}

// ============================================================================
// File Artifact Model
// ============================================================================

/// Represents a tracked file artifact with version history
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileArtifact {
    /// Normalized absolute path
    pub path: PathBuf,
    /// Current known state of the file
    pub state: FileState,
    /// Operations on this file (ordered by sequence)
    pub operations: Vec<FileOperationRecord>,
    /// Latest full snapshot classification key (if any) - can be from read or write
    pub latest_snapshot_key: Option<ClassificationKey>,
    /// Latest content hash
    pub latest_hash: Option<ContentHash>,
}

/// Current state of a file artifact
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileState {
    /// File has been read but not modified in this session
    Read,
    /// File has been created or overwritten (full snapshot exists)
    Written,
    /// File has been edited (patches applied, may need synthetic snapshot)
    Edited,
    /// File has been deleted
    Deleted,
    /// File was renamed (contains old path reference)
    Renamed,
}

/// Record of an operation on a file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileOperationRecord {
    /// Classification key for this operation
    pub key: ClassificationKey,
    /// Sequence number for ordering
    pub sequence: u64,
    /// Type of operation
    pub operation: FileOperation,
    /// Line range (for reads)
    pub line_range: Option<LineRange>,
    /// Content hash after operation
    pub content_hash: Option<ContentHash>,
    /// Whether this is the tool call or result message
    pub is_result: bool,
}

impl FileArtifact {
    /// Create a new file artifact
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            state: FileState::Read,
            operations: Vec::new(),
            latest_snapshot_key: None,
            latest_hash: None,
        }
    }

    /// Record an operation on this file
    pub fn record_operation(&mut self, record: FileOperationRecord) {
        // Update state based on operation
        match record.operation {
            FileOperation::Read => {
                // Read doesn't change written/edited state
                if self.state == FileState::Deleted {
                    self.state = FileState::Read;
                }
                // Full read results become the latest snapshot
                if record.is_result && record.line_range.map(|r| r.is_full_read()).unwrap_or(true) {
                    self.latest_snapshot_key = Some(record.key.clone());
                }
            }
            FileOperation::Write => {
                self.state = FileState::Written;
                if record.is_result {
                    self.latest_snapshot_key = Some(record.key.clone());
                }
            }
            FileOperation::Edit => {
                // Edit after write keeps written state (write is more complete)
                if self.state != FileState::Written {
                    self.state = FileState::Edited;
                }
            }
            FileOperation::Other => {}
        }

        // Update hash if present
        if record.content_hash.is_some() {
            self.latest_hash = record.content_hash.clone();
        }

        self.operations.push(record);
    }

    /// Get the most recent full read that covers the entire file
    pub fn latest_full_read(&self) -> Option<&FileOperationRecord> {
        self.operations.iter().rev().find(|op| {
            op.operation == FileOperation::Read
                && op.is_result
                && op.line_range.map(|r| r.is_full_read()).unwrap_or(true)
        })
    }

    /// Check if there's a newer operation after the given sequence
    pub fn has_newer_operation(&self, sequence: u64) -> bool {
        self.operations.iter().any(|op| op.sequence > sequence)
    }
}

// ============================================================================
// Supersedence Rules
// ============================================================================

/// Decision on what to do with a message during context optimization
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SupersedenceAction {
    /// Keep the message in place
    Keep,
    /// Drop the message entirely (superseded by newer operation)
    Drop,
    /// Move the message to the end of context
    MoveToEnd,
    /// Replace with a synthetic snapshot
    ReplaceWithSnapshot,
}

/// Supersedence rules for file operations
///
/// # Rules
///
/// 1. **Read supersedence**: A newer full read of the same file supersedes
///    older reads (full or partial) of that file.
///
/// 2. **Write supersedence**: A write operation supersedes all prior reads
///    and edits of the same file.
///
/// 3. **Edit accumulation**: Edits don't supersede each other unless a newer
///    write or full read exists. Multiple edits may be kept to show change history.
///
/// 4. **Partial read rules**: A full read supersedes partial reads. Overlapping
///    partial reads: the newer one supersedes if it fully contains the older,
///    or if they overlap and the newer has broader coverage.
///
/// 5. **Snapshot requirement**: The latest full file state must always be available.
///    If only edits exist, patches must be applied to create a synthetic snapshot,
///    or a read-after-write must be enforced.
#[derive(Debug, Clone)]
pub struct SupersedenceRules {
    /// Whether to drop older reads when a newer full read exists
    pub drop_superseded_reads: bool,
    /// Whether to drop reads after a write to the same file
    pub drop_reads_after_write: bool,
    /// Whether to keep edit history or only the final state
    pub preserve_edit_history: bool,
    /// Maximum number of edits to preserve per file
    pub max_edits_per_file: usize,
    /// Whether to enforce read-after-write for files without snapshots
    pub enforce_read_after_write: bool,
}

impl Default for SupersedenceRules {
    fn default() -> Self {
        Self {
            drop_superseded_reads: true,
            drop_reads_after_write: true,
            preserve_edit_history: false,
            max_edits_per_file: 3,
            enforce_read_after_write: true,
        }
    }
}

impl SupersedenceRules {
    /// Determine action for a read operation
    pub fn evaluate_read(
        &self,
        artifact: &FileArtifact,
        classification: &MessageClassification,
    ) -> SupersedenceAction {
        let current_range = classification.line_range;
        let current_seq = classification.sequence;

        // Check if there's a newer write
        let has_newer_write = artifact.operations.iter().any(|op| {
            op.sequence > current_seq && op.operation == FileOperation::Write && op.is_result
        });

        if has_newer_write && self.drop_reads_after_write {
            return SupersedenceAction::Drop;
        }

        // Check if there's a newer full read
        let has_newer_full_read = artifact.operations.iter().any(|op| {
            op.sequence > current_seq
                && op.operation == FileOperation::Read
                && op.is_result
                && op.line_range.map(|r| r.is_full_read()).unwrap_or(true)
        });

        if has_newer_full_read && self.drop_superseded_reads {
            // If this is a partial read and the newer is full, drop this
            if current_range.map(|r| !r.is_full_read()).unwrap_or(false) {
                return SupersedenceAction::Drop;
            }

            // If both are full reads, drop the older one
            return SupersedenceAction::Drop;
        }

        // Check for overlapping partial reads
        // Drop this read if a newer read either contains it OR overlaps with broader coverage
        if let Some(current_range) = current_range {
            for op in &artifact.operations {
                if op.sequence > current_seq && op.operation == FileOperation::Read && op.is_result
                {
                    if let Some(other_range) = op.line_range {
                        // Drop if newer contains this one
                        if other_range.contains(&current_range) {
                            return SupersedenceAction::Drop;
                        }
                        // Also drop if they overlap and newer is broader
                        // (starts earlier or ends later)
                        if other_range.overlaps(&current_range) {
                            let other_coverage = other_range.limit.unwrap_or(usize::MAX);
                            let self_coverage = current_range.limit.unwrap_or(usize::MAX);
                            if other_range.offset <= current_range.offset
                                && other_coverage >= self_coverage
                            {
                                return SupersedenceAction::Drop;
                            }
                        }
                    }
                }
            }
        }

        // This read should be kept, but may need to move to end
        // if it's the latest full snapshot
        if artifact.latest_snapshot_key.as_ref() == Some(&classification.key()) {
            return SupersedenceAction::MoveToEnd;
        }

        SupersedenceAction::Keep
    }

    /// Determine action for a write operation
    pub fn evaluate_write(
        &self,
        artifact: &FileArtifact,
        classification: &MessageClassification,
    ) -> SupersedenceAction {
        // Write results are always the canonical snapshot
        // Move to end if this is the latest write
        let is_latest_write = !artifact.operations.iter().any(|op| {
            op.sequence > classification.sequence
                && op.operation == FileOperation::Write
                && op.is_result
        });

        if is_latest_write && classification.role == MessageRole::User {
            SupersedenceAction::MoveToEnd
        } else {
            SupersedenceAction::Keep
        }
    }

    /// Determine action for an edit operation
    pub fn evaluate_edit(
        &self,
        artifact: &FileArtifact,
        classification: &MessageClassification,
    ) -> SupersedenceAction {
        // Count edits after this one
        let edits_after = artifact
            .operations
            .iter()
            .filter(|op| {
                op.sequence > classification.sequence
                    && op.operation == FileOperation::Edit
                    && op.is_result
            })
            .count();

        // Check if there's a newer write or full read
        let has_newer_snapshot = artifact.operations.iter().any(|op| {
            op.sequence > classification.sequence
                && (op.operation == FileOperation::Write
                    || (op.operation == FileOperation::Read
                        && op.line_range.map(|r| r.is_full_read()).unwrap_or(true)))
                && op.is_result
        });

        if has_newer_snapshot {
            // Edits before a snapshot can be dropped (snapshot reflects final state)
            return SupersedenceAction::Drop;
        }

        if !self.preserve_edit_history {
            // Only keep the most recent edit
            if edits_after > 0 {
                return SupersedenceAction::Drop;
            }
        } else if edits_after >= self.max_edits_per_file {
            // Too many edits, drop older ones
            return SupersedenceAction::Drop;
        }

        SupersedenceAction::Keep
    }

    /// Evaluate a message classification and return the appropriate action
    pub fn evaluate(
        &self,
        artifact: &FileArtifact,
        classification: &MessageClassification,
    ) -> SupersedenceAction {
        match classification.operation {
            FileOperation::Read => self.evaluate_read(artifact, classification),
            FileOperation::Write => self.evaluate_write(artifact, classification),
            FileOperation::Edit => self.evaluate_edit(artifact, classification),
            FileOperation::Other => SupersedenceAction::Keep,
        }
    }
}

// ============================================================================
// Context Classifier
// ============================================================================

/// Global sequence counter for unique IDs across classifier instances
static GLOBAL_SEQUENCE: AtomicU64 = AtomicU64::new(0);

/// Classifier for extracting file operation metadata from messages
#[derive(Debug, Default)]
pub struct ContextClassifier {
    /// Tracked file artifacts by normalized path
    artifacts: HashMap<PathBuf, FileArtifact>,
    /// Message classifications by composite key (message_id, tool_call_id)
    classifications: HashMap<ClassificationKey, MessageClassification>,
    /// Index from message_id to all classification keys for that message
    message_index: HashMap<String, Vec<ClassificationKey>>,
    /// Current sequence counter (local, but seeded from global)
    sequence_counter: u64,
}

impl ContextClassifier {
    /// Create a new context classifier
    pub fn new() -> Self {
        Self {
            sequence_counter: GLOBAL_SEQUENCE.fetch_add(1000, Ordering::SeqCst),
            ..Default::default()
        }
    }

    /// Generate next sequence number (globally unique)
    fn next_sequence(&mut self) -> u64 {
        self.sequence_counter += 1;
        self.sequence_counter
    }

    /// Normalize a file path to absolute form
    fn normalize_path(&self, path: &str) -> PathBuf {
        let path = PathBuf::from(path);
        if path.is_absolute() {
            path
        } else {
            // In a real implementation, this would resolve against working directory
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("/"))
                .join(path)
        }
    }

    /// Classify a tool call (from assistant message)
    pub fn classify_tool_call(
        &mut self,
        tool_name: &str,
        args: &serde_json::Value,
        message_id: &str,
        tool_call_id: &str,
    ) -> MessageClassification {
        let sequence = self.next_sequence();
        let (operation, file_path, line_range) = self.extract_operation_metadata(tool_name, args);

        let classification = MessageClassification {
            message_id: message_id.to_string(),
            tool_call_id: tool_call_id.to_string(),
            sequence,
            role: MessageRole::Assistant,
            operation,
            file_path: file_path.clone(),
            line_range,
            content_hash: None, // Calls don't have content yet
            content_size: 0,
        };

        let key = classification.key();

        // Record in artifact if file-related
        if let Some(path) = file_path {
            let artifact = self
                .artifacts
                .entry(path.clone())
                .or_insert_with(|| FileArtifact::new(path));

            artifact.record_operation(FileOperationRecord {
                key: key.clone(),
                sequence,
                operation,
                line_range,
                content_hash: None,
                is_result: false,
            });
        }

        // Add to message index
        self.message_index
            .entry(message_id.to_string())
            .or_default()
            .push(key.clone());

        self.classifications.insert(key, classification.clone());
        classification
    }

    /// Classify a tool result (from user message)
    pub fn classify_tool_result(
        &mut self,
        tool_name: &str,
        result: &str,
        message_id: &str,
        tool_call_id: &str,
    ) -> MessageClassification {
        let sequence = self.next_sequence();

        // Try to find the original tool call to get file path
        // Note: The call would have a different message_id, so search by tool_call_id
        let call_classification = self
            .classifications
            .values()
            .find(|c| c.tool_call_id == tool_call_id && c.role == MessageRole::Assistant);

        let (operation, file_path, line_range) = if let Some(call) = call_classification {
            (call.operation, call.file_path.clone(), call.line_range)
        } else {
            // Parse result to extract metadata
            self.extract_result_metadata(tool_name, result)
        };

        let content_hash = if operation.is_read() || operation.is_mutating() {
            Some(ContentHash::from_content(result))
        } else {
            None
        };

        let classification = MessageClassification {
            message_id: message_id.to_string(),
            tool_call_id: tool_call_id.to_string(),
            sequence,
            role: MessageRole::User,
            operation,
            file_path: file_path.clone(),
            line_range,
            content_hash: content_hash.clone(),
            content_size: result.len(),
        };

        let key = classification.key();

        // Record in artifact if file-related
        if let Some(path) = file_path {
            let artifact = self
                .artifacts
                .entry(path.clone())
                .or_insert_with(|| FileArtifact::new(path));

            artifact.record_operation(FileOperationRecord {
                key: key.clone(),
                sequence,
                operation,
                line_range,
                content_hash,
                is_result: true,
            });
        }

        // Add to message index
        self.message_index
            .entry(message_id.to_string())
            .or_default()
            .push(key.clone());

        self.classifications.insert(key, classification.clone());
        classification
    }

    /// Extract operation metadata from tool call arguments
    fn extract_operation_metadata(
        &self,
        tool_name: &str,
        args: &serde_json::Value,
    ) -> (FileOperation, Option<PathBuf>, Option<LineRange>) {
        match tool_name {
            "read" => {
                let file_path = args
                    .get("file_path")
                    .and_then(|v| v.as_str())
                    .map(|s| self.normalize_path(s));

                let offset = args
                    .get("offset")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as usize)
                    .unwrap_or(0);

                let limit = args
                    .get("limit")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as usize);

                let line_range = if offset == 0 && limit.is_none() {
                    None // Full read
                } else {
                    Some(LineRange::new(offset, limit))
                };

                (FileOperation::Read, file_path, line_range)
            }
            "write" => {
                let file_path = args
                    .get("file_path")
                    .and_then(|v| v.as_str())
                    .map(|s| self.normalize_path(s));

                (FileOperation::Write, file_path, None)
            }
            "edit" => {
                let file_path = args
                    .get("file_path")
                    .and_then(|v| v.as_str())
                    .map(|s| self.normalize_path(s));

                (FileOperation::Edit, file_path, None)
            }
            _ => (FileOperation::Other, None, None),
        }
    }

    /// Extract metadata from tool result (fallback when call not found)
    fn extract_result_metadata(
        &self,
        tool_name: &str,
        result: &str,
    ) -> (FileOperation, Option<PathBuf>, Option<LineRange>) {
        // Try to parse JSON result for file path
        if let Ok(json) = serde_json::from_str::<serde_json::Value>(result) {
            let file_path = json
                .get("file_path")
                .and_then(|v| v.as_str())
                .map(|s| self.normalize_path(s));

            let operation = match tool_name {
                "read" => FileOperation::Read,
                "write" => FileOperation::Write,
                "edit" => FileOperation::Edit,
                _ => FileOperation::Other,
            };

            return (operation, file_path, None);
        }

        (FileOperation::Other, None, None)
    }

    /// Get artifact for a file path
    pub fn get_artifact(&self, path: &PathBuf) -> Option<&FileArtifact> {
        self.artifacts.get(path)
    }

    /// Get all tracked artifacts
    pub fn artifacts(&self) -> &HashMap<PathBuf, FileArtifact> {
        &self.artifacts
    }

    /// Get classification by composite key
    pub fn get_classification_by_key(
        &self,
        key: &ClassificationKey,
    ) -> Option<&MessageClassification> {
        self.classifications.get(key)
    }

    /// Get all classifications for a message ID
    pub fn get_classifications_for_message(&self, message_id: &str) -> Vec<&MessageClassification> {
        self.message_index
            .get(message_id)
            .map(|keys| {
                keys.iter()
                    .filter_map(|k| self.classifications.get(k))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Check if a message has any classifications
    pub fn is_message_classified(&self, message_id: &str) -> bool {
        self.message_index.contains_key(message_id)
    }

    /// Get classification for a message (legacy API - returns first classification)
    #[deprecated(note = "Use get_classifications_for_message for multi-tool support")]
    pub fn get_classification(&self, message_id: &str) -> Option<&MessageClassification> {
        self.get_classifications_for_message(message_id)
            .into_iter()
            .next()
    }

    /// Get all classifications
    pub fn classifications(&self) -> &HashMap<ClassificationKey, MessageClassification> {
        &self.classifications
    }

    /// Get all classification keys for a message
    pub fn get_keys_for_message(&self, message_id: &str) -> Option<&Vec<ClassificationKey>> {
        self.message_index.get(message_id)
    }

    /// Clear all state
    pub fn clear(&mut self) {
        self.artifacts.clear();
        self.classifications.clear();
        self.message_index.clear();
        self.sequence_counter = GLOBAL_SEQUENCE.fetch_add(1000, Ordering::SeqCst);
    }
}

// ============================================================================
// Context Manager
// ============================================================================

/// Manages context optimization for file operations
///
/// The ContextManager uses the classifier and supersedence rules to:
/// 1. Track all file operations in a conversation
/// 2. Determine which messages should be kept, dropped, or moved
/// 3. Produce an optimized message ordering for LLM context
#[derive(Debug)]
pub struct ContextManager {
    /// The classifier for extracting metadata
    classifier: ContextClassifier,
    /// Supersedence rules to apply
    rules: SupersedenceRules,
}

impl ContextManager {
    /// Create a new context manager with default rules
    pub fn new() -> Self {
        Self {
            classifier: ContextClassifier::new(),
            rules: SupersedenceRules::default(),
        }
    }

    /// Create with custom supersedence rules
    pub fn with_rules(rules: SupersedenceRules) -> Self {
        Self {
            classifier: ContextClassifier::new(),
            rules,
        }
    }

    /// Get a reference to the classifier
    pub fn classifier(&self) -> &ContextClassifier {
        &self.classifier
    }

    /// Get a mutable reference to the classifier
    pub fn classifier_mut(&mut self) -> &mut ContextClassifier {
        &mut self.classifier
    }

    /// Get the supersedence rules
    pub fn rules(&self) -> &SupersedenceRules {
        &self.rules
    }

    /// Compute the optimized order of classification keys
    ///
    /// Returns actions for each classification (not message). A single message
    /// may have multiple classifications if it contains multiple tool calls.
    pub fn compute_optimized_order(&self) -> ContextOptimizationResult<'_> {
        let mut keep: Vec<ClassificationKey> = Vec::new();
        let mut drop: Vec<ClassificationKey> = Vec::new();
        let mut move_to_end: Vec<ClassificationKey> = Vec::new();

        // Sort classifications by sequence
        let mut sorted_classifications: Vec<_> = self.classifier.classifications.values().collect();
        sorted_classifications.sort_by_key(|c| c.sequence);

        for classification in sorted_classifications {
            let action = if let Some(path) = &classification.file_path {
                if let Some(artifact) = self.classifier.artifacts.get(path) {
                    self.rules.evaluate(artifact, classification)
                } else {
                    SupersedenceAction::Keep
                }
            } else {
                SupersedenceAction::Keep
            };

            let key = classification.key();

            match action {
                SupersedenceAction::Keep => {
                    keep.push(key);
                }
                SupersedenceAction::Drop => {
                    drop.push(key);
                }
                SupersedenceAction::MoveToEnd => {
                    move_to_end.push(key);
                }
                SupersedenceAction::ReplaceWithSnapshot => {
                    // For now, treat as keep - synthetic snapshot generation
                    // would be handled separately
                    keep.push(key);
                }
            }
        }

        ContextOptimizationResult {
            keep,
            drop,
            move_to_end,
            classifier_ref: &self.classifier,
        }
    }

    /// Check if a file needs a read-after-write
    ///
    /// Returns true if the file has been written/edited but has no
    /// subsequent full read, and enforce_read_after_write is enabled.
    pub fn needs_read_after_write(&self, path: &PathBuf) -> bool {
        if !self.rules.enforce_read_after_write {
            return false;
        }

        if let Some(artifact) = self.classifier.artifacts.get(path) {
            // Check if file was mutated
            let was_mutated = artifact
                .operations
                .iter()
                .any(|op| op.operation.is_mutating() && op.is_result);

            if !was_mutated {
                return false;
            }

            // Check if there's a full read after the last mutation
            let last_mutation_seq = artifact
                .operations
                .iter()
                .filter(|op| op.operation.is_mutating() && op.is_result)
                .map(|op| op.sequence)
                .max()
                .unwrap_or(0);

            let has_read_after = artifact.operations.iter().any(|op| {
                op.sequence > last_mutation_seq
                    && op.operation == FileOperation::Read
                    && op.is_result
                    && op.line_range.map(|r| r.is_full_read()).unwrap_or(true)
            });

            !has_read_after
        } else {
            false
        }
    }

    /// Get files that need read-after-write
    pub fn files_needing_read(&self) -> Vec<PathBuf> {
        self.classifier
            .artifacts
            .keys()
            .filter(|path| self.needs_read_after_write(path))
            .cloned()
            .collect()
    }

    /// Clear all state
    pub fn clear(&mut self) {
        self.classifier.clear();
    }
}

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

/// Result of context optimization computation
#[derive(Debug)]
pub struct ContextOptimizationResult<'a> {
    /// Classification keys to keep in their original position
    pub keep: Vec<ClassificationKey>,
    /// Classification keys to drop
    pub drop: Vec<ClassificationKey>,
    /// Classification keys to move to the end of context
    pub move_to_end: Vec<ClassificationKey>,
    /// Reference to the classifier for message lookups
    classifier_ref: &'a ContextClassifier,
}

impl<'a> ContextOptimizationResult<'a> {
    /// Get the final ordered list of classification keys
    pub fn final_order(&self) -> Vec<ClassificationKey> {
        let mut result = self.keep.clone();
        result.extend(self.move_to_end.iter().cloned());
        result
    }

    /// Check if a classification key should be included
    pub fn should_include_key(&self, key: &ClassificationKey) -> bool {
        self.keep.contains(key) || self.move_to_end.contains(key)
    }

    /// Check if a classification key should be dropped
    pub fn should_drop_key(&self, key: &ClassificationKey) -> bool {
        self.drop.contains(key)
    }

    /// Check if a message (by ID) has any classifications that should be included
    ///
    /// For messages with multiple tool calls, returns true if ANY of them should be included.
    pub fn should_include(&self, message_id: &str) -> bool {
        if let Some(keys) = self.classifier_ref.get_keys_for_message(message_id) {
            keys.iter().any(|k| self.should_include_key(k))
        } else {
            // Message is not classified - it's not a file operation
            // The caller should decide how to handle unclassified messages
            false
        }
    }

    /// Check if a message (by ID) should be dropped
    ///
    /// Returns true only if ALL classifications for this message should be dropped.
    /// Returns false if the message has no classifications (it's not a file operation).
    pub fn should_drop(&self, message_id: &str) -> bool {
        if let Some(keys) = self.classifier_ref.get_keys_for_message(message_id) {
            !keys.is_empty() && keys.iter().all(|k| self.should_drop_key(k))
        } else {
            // Message is not classified - don't drop it
            false
        }
    }

    /// Check if a message ID has any classifications
    pub fn is_classified(&self, message_id: &str) -> bool {
        self.classifier_ref.is_message_classified(message_id)
    }

    /// Get all unique message IDs in final order
    ///
    /// Note: This only includes classified messages. Unclassified messages
    /// (normal chat, non-file tools) should be preserved separately.
    pub fn final_message_order(&self) -> Vec<String> {
        let mut seen = std::collections::HashSet::new();
        let mut result = Vec::new();

        for key in self.final_order() {
            if seen.insert(key.message_id.clone()) {
                result.push(key.message_id);
            }
        }

        result
    }
}

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

    #[test]
    fn test_line_range_is_full_read() {
        assert!(LineRange::new(0, None).is_full_read());
        assert!(!LineRange::new(10, None).is_full_read());
        assert!(!LineRange::new(0, Some(100)).is_full_read());
    }

    #[test]
    fn test_line_range_contains() {
        let full = LineRange::new(0, None);
        let partial1 = LineRange::new(10, Some(50));
        let partial2 = LineRange::new(20, Some(30));

        assert!(full.contains(&partial1));
        assert!(full.contains(&partial2));
        assert!(!partial1.contains(&full));
        assert!(partial1.contains(&partial2));
    }

    #[test]
    fn test_line_range_overlaps() {
        let range1 = LineRange::new(0, Some(50));
        let range2 = LineRange::new(40, Some(30));
        let range3 = LineRange::new(60, Some(20));

        assert!(range1.overlaps(&range2));
        assert!(!range1.overlaps(&range3));
    }

    #[test]
    fn test_content_hash() {
        let hash1 = ContentHash::from_content("hello world");
        let hash2 = ContentHash::from_content("hello world");
        let hash3 = ContentHash::from_content("different content");

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_classifier_read_operation() {
        let mut classifier = ContextClassifier::new();

        let args = serde_json::json!({
            "file_path": "/test/file.rs"
        });

        let classification = classifier.classify_tool_call("read", &args, "msg-1", "call-1");

        assert_eq!(classification.operation, FileOperation::Read);
        assert!(classification.file_path.is_some());
        assert!(classification.line_range.is_none()); // Full read
    }

    #[test]
    fn test_classifier_partial_read() {
        let mut classifier = ContextClassifier::new();

        let args = serde_json::json!({
            "file_path": "/test/file.rs",
            "offset": 10,
            "limit": 50
        });

        let classification = classifier.classify_tool_call("read", &args, "msg-1", "call-1");

        assert_eq!(classification.operation, FileOperation::Read);
        assert!(classification.line_range.is_some());

        let range = classification.line_range.unwrap();
        assert_eq!(range.offset, 10);
        assert_eq!(range.limit, Some(50));
    }

    #[test]
    fn test_classifier_write_operation() {
        let mut classifier = ContextClassifier::new();

        let args = serde_json::json!({
            "file_path": "/test/new_file.rs",
            "content": "fn main() {}"
        });

        let classification = classifier.classify_tool_call("write", &args, "msg-1", "call-1");

        assert_eq!(classification.operation, FileOperation::Write);
        assert!(classification.file_path.is_some());
    }

    #[test]
    fn test_classifier_edit_operation() {
        let mut classifier = ContextClassifier::new();

        let args = serde_json::json!({
            "file_path": "/test/file.rs",
            "old_string": "old",
            "new_string": "new"
        });

        let classification = classifier.classify_tool_call("edit", &args, "msg-1", "call-1");

        assert_eq!(classification.operation, FileOperation::Edit);
        assert!(classification.file_path.is_some());
    }

    #[test]
    fn test_classifier_multiple_tool_calls_same_message() {
        let mut classifier = ContextClassifier::new();

        // Two tool calls in the same assistant message
        classifier.classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/test/file1.rs"}),
            "msg-1",
            "call-1",
        );
        classifier.classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/test/file2.rs"}),
            "msg-1",
            "call-2",
        );

        // Both should be recorded
        let classifications = classifier.get_classifications_for_message("msg-1");
        assert_eq!(classifications.len(), 2);

        // Check both files are tracked
        assert!(classifier.is_message_classified("msg-1"));
        let keys = classifier.get_keys_for_message("msg-1").unwrap();
        assert_eq!(keys.len(), 2);
    }

    #[test]
    fn test_file_artifact_state_transitions() {
        let mut artifact = FileArtifact::new(PathBuf::from("/test/file.rs"));

        // Initial read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-1", "call-1"),
            sequence: 1,
            operation: FileOperation::Read,
            line_range: None,
            content_hash: Some(ContentHash::from_content("v1")),
            is_result: true,
        });
        assert_eq!(artifact.state, FileState::Read);
        // Full read should set latest_snapshot_key
        assert!(artifact.latest_snapshot_key.is_some());

        // Edit changes state
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-2", "call-2"),
            sequence: 2,
            operation: FileOperation::Edit,
            line_range: None,
            content_hash: Some(ContentHash::from_content("v2")),
            is_result: true,
        });
        assert_eq!(artifact.state, FileState::Edited);

        // Write trumps edit
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-3", "call-3"),
            sequence: 3,
            operation: FileOperation::Write,
            line_range: None,
            content_hash: Some(ContentHash::from_content("v3")),
            is_result: true,
        });
        assert_eq!(artifact.state, FileState::Written);
        assert_eq!(
            artifact.latest_snapshot_key,
            Some(ClassificationKey::new("msg-3", "call-3"))
        );
    }

    #[test]
    fn test_supersedence_read_after_write() {
        let rules = SupersedenceRules::default();
        let mut artifact = FileArtifact::new(PathBuf::from("/test/file.rs"));

        // Record a read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-1", "call-1"),
            sequence: 1,
            operation: FileOperation::Read,
            line_range: None,
            content_hash: Some(ContentHash::from_content("v1")),
            is_result: true,
        });

        // Record a write after the read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-2", "call-2"),
            sequence: 2,
            operation: FileOperation::Write,
            line_range: None,
            content_hash: Some(ContentHash::from_content("v2")),
            is_result: true,
        });

        // The old read should be dropped
        let classification = MessageClassification {
            message_id: "msg-1".to_string(),
            tool_call_id: "call-1".to_string(),
            sequence: 1,
            role: MessageRole::User,
            operation: FileOperation::Read,
            file_path: Some(PathBuf::from("/test/file.rs")),
            line_range: None,
            content_hash: Some(ContentHash::from_content("v1")),
            content_size: 100,
        };

        let action = rules.evaluate(&artifact, &classification);
        assert_eq!(action, SupersedenceAction::Drop);
    }

    #[test]
    fn test_supersedence_newer_full_read() {
        let rules = SupersedenceRules::default();
        let mut artifact = FileArtifact::new(PathBuf::from("/test/file.rs"));

        // Old partial read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-1", "call-1"),
            sequence: 1,
            operation: FileOperation::Read,
            line_range: Some(LineRange::new(10, Some(50))),
            content_hash: Some(ContentHash::from_content("partial")),
            is_result: true,
        });

        // Newer full read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-2", "call-2"),
            sequence: 2,
            operation: FileOperation::Read,
            line_range: None,
            content_hash: Some(ContentHash::from_content("full")),
            is_result: true,
        });

        // Old partial read should be dropped
        let classification = MessageClassification {
            message_id: "msg-1".to_string(),
            tool_call_id: "call-1".to_string(),
            sequence: 1,
            role: MessageRole::User,
            operation: FileOperation::Read,
            file_path: Some(PathBuf::from("/test/file.rs")),
            line_range: Some(LineRange::new(10, Some(50))),
            content_hash: Some(ContentHash::from_content("partial")),
            content_size: 100,
        };

        let action = rules.evaluate(&artifact, &classification);
        assert_eq!(action, SupersedenceAction::Drop);
    }

    #[test]
    fn test_latest_full_read_moves_to_end() {
        let rules = SupersedenceRules::default();
        let mut artifact = FileArtifact::new(PathBuf::from("/test/file.rs"));

        // Full read
        artifact.record_operation(FileOperationRecord {
            key: ClassificationKey::new("msg-1", "call-1"),
            sequence: 1,
            operation: FileOperation::Read,
            line_range: None,
            content_hash: Some(ContentHash::from_content("full")),
            is_result: true,
        });

        // The latest full read should move to end
        let classification = MessageClassification {
            message_id: "msg-1".to_string(),
            tool_call_id: "call-1".to_string(),
            sequence: 1,
            role: MessageRole::User,
            operation: FileOperation::Read,
            file_path: Some(PathBuf::from("/test/file.rs")),
            line_range: None,
            content_hash: Some(ContentHash::from_content("full")),
            content_size: 100,
        };

        let action = rules.evaluate(&artifact, &classification);
        assert_eq!(action, SupersedenceAction::MoveToEnd);
    }

    #[test]
    fn test_context_manager_optimization() {
        let mut manager = ContextManager::new();

        // Simulate: read -> edit -> read sequence
        let args = serde_json::json!({"file_path": "/test/file.rs"});

        // First read
        manager
            .classifier
            .classify_tool_call("read", &args, "msg-1", "call-1");
        manager.classifier.classify_tool_result(
            "read",
            r#"{"file_path":"/test/file.rs","content":"v1"}"#,
            "msg-2",
            "call-1",
        );

        // Edit
        let edit_args = serde_json::json!({
            "file_path": "/test/file.rs",
            "old_string": "old",
            "new_string": "new"
        });
        manager
            .classifier
            .classify_tool_call("edit", &edit_args, "msg-3", "call-2");
        manager.classifier.classify_tool_result(
            "edit",
            r#"{"file_path":"/test/file.rs"}"#,
            "msg-4",
            "call-2",
        );

        // Second read (after edit)
        manager
            .classifier
            .classify_tool_call("read", &args, "msg-5", "call-3");
        manager.classifier.classify_tool_result(
            "read",
            r#"{"file_path":"/test/file.rs","content":"v2"}"#,
            "msg-6",
            "call-3",
        );

        let result = manager.compute_optimized_order();

        // First read result (msg-2) should be dropped
        // because there's a newer full read (msg-6)
        assert!(result.should_drop("msg-2"));

        // Edit should be dropped because there's a newer read
        assert!(result.should_drop("msg-4"));

        // Latest read should be kept and moved to end
        assert!(!result.should_drop("msg-6"));
    }

    #[test]
    fn test_unclassified_messages_not_dropped() {
        let mut manager = ContextManager::new();

        // Only classify one message
        manager.classifier.classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/test/file.rs"}),
            "msg-1",
            "call-1",
        );

        let result = manager.compute_optimized_order();

        // Unclassified messages should not be reported as dropped
        assert!(!result.should_drop("msg-unclassified"));
        assert!(!result.is_classified("msg-unclassified"));
    }

    #[test]
    fn test_needs_read_after_write() {
        let mut manager = ContextManager::new();
        let path = PathBuf::from("/test/file.rs");

        // Write without subsequent read
        let args = serde_json::json!({
            "file_path": "/test/file.rs",
            "content": "fn main() {}"
        });
        manager
            .classifier
            .classify_tool_call("write", &args, "msg-1", "call-1");
        manager.classifier.classify_tool_result(
            "write",
            r#"{"file_path":"/test/file.rs"}"#,
            "msg-2",
            "call-1",
        );

        assert!(manager.needs_read_after_write(&path));

        // Add a full read
        let read_args = serde_json::json!({"file_path": "/test/file.rs"});
        manager
            .classifier
            .classify_tool_call("read", &read_args, "msg-3", "call-2");
        manager.classifier.classify_tool_result(
            "read",
            r#"{"file_path":"/test/file.rs","content":"fn main() {}"}"#,
            "msg-4",
            "call-2",
        );

        assert!(!manager.needs_read_after_write(&path));
    }
}