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
//! Test Transcripts for File-History Context Management
//!
//! This module contains example conversation transcripts that validate the
//! ordering and deduplication behavior of the classification system.
//!
//! # Transcript Format
//!
//! Each transcript shows:
//! 1. Initial messages in the conversation
//! 2. Expected classification of each message
//! 3. Expected supersedence decisions
//! 4. Final context order after optimization
//!
//! # Test Scenarios
//!
//! - Read → Patch → Read sequences
//! - Multiple file operations interleaved
//! - Partial read handling
//! - Write supersedence
//! - Edge cases (renames, deletes, large files)

#[cfg(test)]
mod tests {
    use crate::context::classification::*;
    use std::path::PathBuf;

    /// Helper to create a test context manager with default rules
    fn test_manager() -> ContextManager {
        ContextManager::new()
    }

    /// Helper to create a manager with custom rules
    fn test_manager_with_rules(rules: SupersedenceRules) -> ContextManager {
        ContextManager::with_rules(rules)
    }

    // ========================================================================
    // Transcript 1: Simple Read → Edit → Read
    // ========================================================================

    /// Tests the basic read-edit-read pattern where:
    /// - Initial read should be dropped (superseded by final read)
    /// - Edit should be dropped (superseded by final read)
    /// - Final read should be kept and moved to end
    #[test]
    fn transcript_read_edit_read() {
        let mut manager = test_manager();

        // === Conversation Transcript ===
        //
        // [msg-1] Assistant: "Let me read the file"
        //         ToolCall(read, {file_path: "/src/main.rs"})
        //
        // [msg-2] User: ToolResult for msg-1
        //         Content: "fn main() { old_code(); }"
        //
        // [msg-3] Assistant: "I'll edit the file"
        //         ToolCall(edit, {file_path: "/src/main.rs", old: "old_code", new: "new_code"})
        //
        // [msg-4] User: ToolResult for msg-3
        //         Content: "Edit successful"
        //
        // [msg-5] Assistant: "Let me verify the change"
        //         ToolCall(read, {file_path: "/src/main.rs"})
        //
        // [msg-6] User: ToolResult for msg-5
        //         Content: "fn main() { new_code(); }"

        // Classify each operation
        let read_args_1 = serde_json::json!({"file_path": "/src/main.rs"});
        manager
            .classifier_mut()
            .classify_tool_call("read", &read_args_1, "msg-1", "call-1");
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/main.rs","content":"fn main() { old_code(); }"}"#,
            "msg-2",
            "call-1",
        );

        let edit_args = serde_json::json!({
            "file_path": "/src/main.rs",
            "old_string": "old_code",
            "new_string": "new_code"
        });
        manager
            .classifier_mut()
            .classify_tool_call("edit", &edit_args, "msg-3", "call-2");
        manager.classifier_mut().classify_tool_result(
            "edit",
            r#"{"file_path":"/src/main.rs","replacements":1}"#,
            "msg-4",
            "call-2",
        );

        let read_args_2 = serde_json::json!({"file_path": "/src/main.rs"});
        manager
            .classifier_mut()
            .classify_tool_call("read", &read_args_2, "msg-5", "call-3");
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/main.rs","content":"fn main() { new_code(); }"}"#,
            "msg-6",
            "call-3",
        );

        // Compute optimization
        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // Dropped messages:
        // - msg-2: First read result (superseded by msg-6)
        // - msg-4: Edit result (superseded by msg-6 read)
        //
        // Kept messages:
        // - msg-1, msg-3, msg-5: Tool calls (always kept for context)
        // - msg-6: Latest read (moved to end)

        assert!(
            result.should_drop("msg-2"),
            "First read result should be dropped"
        );
        assert!(result.should_drop("msg-4"), "Edit result should be dropped");
        assert!(
            result.should_include("msg-6"),
            "Final read should be included"
        );

        // Verify final order includes latest read at end
        let final_order = result.final_order();
        assert!(
            final_order.last().map(|k| k.message_id.as_str()) == Some("msg-6")
                || result.move_to_end.iter().any(|k| k.message_id == "msg-6"),
            "Latest read should be at end of context"
        );
    }

    // ========================================================================
    // Transcript 2: Multiple Files Interleaved
    // ========================================================================

    /// Tests handling of operations on multiple files:
    /// - Operations on different files don't affect each other
    /// - Each file tracks its own supersedence independently
    #[test]
    fn transcript_multiple_files() {
        let mut manager = test_manager();

        // === Conversation Transcript ===
        //
        // [msg-1] Read file_a.rs
        // [msg-2] Result: content of file_a.rs v1
        // [msg-3] Read file_b.rs
        // [msg-4] Result: content of file_b.rs v1
        // [msg-5] Edit file_a.rs
        // [msg-6] Result: edit success
        // [msg-7] Read file_a.rs (verification)
        // [msg-8] Result: content of file_a.rs v2

        // File A operations
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file_a.rs"}),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file_a.rs","content":"v1"}"#,
            "msg-2",
            "call-1",
        );

        // File B operations
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file_b.rs"}),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file_b.rs","content":"v1"}"#,
            "msg-4",
            "call-2",
        );

        // Edit file A
        manager.classifier_mut().classify_tool_call(
            "edit",
            &serde_json::json!({
                "file_path": "/src/file_a.rs",
                "old_string": "old",
                "new_string": "new"
            }),
            "msg-5",
            "call-3",
        );
        manager.classifier_mut().classify_tool_result(
            "edit",
            r#"{"file_path":"/src/file_a.rs"}"#,
            "msg-6",
            "call-3",
        );

        // Read file A again
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file_a.rs"}),
            "msg-7",
            "call-4",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file_a.rs","content":"v2"}"#,
            "msg-8",
            "call-4",
        );

        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // File A: msg-2 (old read) dropped, msg-8 (new read) kept
        // File B: msg-4 kept (no superseding operation)

        assert!(
            result.should_drop("msg-2"),
            "Old read of file_a should be dropped"
        );
        assert!(
            result.should_include("msg-4"),
            "Read of file_b should be kept (no newer read)"
        );
        assert!(
            result.should_include("msg-8"),
            "New read of file_a should be kept"
        );
    }

    // ========================================================================
    // Transcript 3: Write Supersedes All
    // ========================================================================

    /// Tests that a write operation supersedes all prior reads and edits
    #[test]
    fn transcript_write_supersedes() {
        let mut manager = test_manager();

        // === Conversation Transcript ===
        //
        // [msg-1] Read file.rs
        // [msg-2] Result: content v1
        // [msg-3] Edit file.rs
        // [msg-4] Result: edit success
        // [msg-5] Write file.rs (complete rewrite)
        // [msg-6] Result: write success

        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"v1"}"#,
            "msg-2",
            "call-1",
        );

        manager.classifier_mut().classify_tool_call(
            "edit",
            &serde_json::json!({
                "file_path": "/src/file.rs",
                "old_string": "a",
                "new_string": "b"
            }),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "edit",
            r#"{"file_path":"/src/file.rs"}"#,
            "msg-4",
            "call-2",
        );

        manager.classifier_mut().classify_tool_call(
            "write",
            &serde_json::json!({
                "file_path": "/src/file.rs",
                "content": "completely new content"
            }),
            "msg-5",
            "call-3",
        );
        manager.classifier_mut().classify_tool_result(
            "write",
            r#"{"file_path":"/src/file.rs","bytes_written":22}"#,
            "msg-6",
            "call-3",
        );

        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // All prior operations on the file should be dropped
        // Only the write result should be kept (as latest snapshot)

        assert!(
            result.should_drop("msg-2"),
            "Read before write should be dropped"
        );
        assert!(
            result.should_drop("msg-4"),
            "Edit before write should be dropped"
        );
        assert!(
            result.should_include("msg-6"),
            "Write result should be kept"
        );
    }

    // ========================================================================
    // Transcript 4: Partial Reads
    // ========================================================================

    /// Tests handling of partial reads with offset/limit
    #[test]
    fn transcript_partial_reads() {
        let mut manager = test_manager();

        // === Conversation Transcript ===
        //
        // [msg-1] Read file.rs lines 0-50
        // [msg-2] Result: partial content
        // [msg-3] Read file.rs lines 100-150
        // [msg-4] Result: partial content
        // [msg-5] Read file.rs (full)
        // [msg-6] Result: full content

        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({
                "file_path": "/src/file.rs",
                "offset": 0,
                "limit": 50
            }),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"lines 0-50"}"#,
            "msg-2",
            "call-1",
        );

        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({
                "file_path": "/src/file.rs",
                "offset": 100,
                "limit": 50
            }),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"lines 100-150"}"#,
            "msg-4",
            "call-2",
        );

        // Full read supersedes partial reads
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-5",
            "call-3",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"full file content"}"#,
            "msg-6",
            "call-3",
        );

        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // Partial reads should be dropped when full read exists

        assert!(
            result.should_drop("msg-2"),
            "Partial read 0-50 should be dropped"
        );
        assert!(
            result.should_drop("msg-4"),
            "Partial read 100-150 should be dropped"
        );
        assert!(result.should_include("msg-6"), "Full read should be kept");
    }

    // ========================================================================
    // Transcript 5: Read-After-Write Enforcement
    // ========================================================================

    /// Tests that the system identifies when read-after-write is needed
    #[test]
    fn transcript_read_after_write_needed() {
        let mut manager = test_manager();

        // === Conversation Transcript ===
        //
        // [msg-1] Write file.rs
        // [msg-2] Result: write success
        // (No subsequent read)

        manager.classifier_mut().classify_tool_call(
            "write",
            &serde_json::json!({
                "file_path": "/src/file.rs",
                "content": "new content"
            }),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "write",
            r#"{"file_path":"/src/file.rs"}"#,
            "msg-2",
            "call-1",
        );

        let path = PathBuf::from("/src/file.rs");

        // === Expected Results ===
        //
        // File should be flagged as needing read-after-write

        assert!(
            manager.needs_read_after_write(&path),
            "Write without read should need read-after-write"
        );

        // Now add a read
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"new content"}"#,
            "msg-4",
            "call-2",
        );

        assert!(
            !manager.needs_read_after_write(&path),
            "After read, should not need read-after-write"
        );
    }

    // ========================================================================
    // Transcript 6: Preserve Edit History Mode
    // ========================================================================

    /// Tests behavior when edit history preservation is enabled
    #[test]
    fn transcript_preserve_edit_history() {
        let rules = SupersedenceRules {
            preserve_edit_history: true,
            max_edits_per_file: 3,
            ..Default::default()
        };
        let mut manager = test_manager_with_rules(rules);

        // === Conversation Transcript ===
        //
        // Multiple edits without a final read

        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs","content":"v1"}"#,
            "msg-2",
            "call-1",
        );

        // Multiple edits
        for i in 0..4 {
            let msg_call = format!("msg-{}", 3 + i * 2);
            let msg_result = format!("msg-{}", 4 + i * 2);
            let call_id = format!("call-{}", 2 + i);

            manager.classifier_mut().classify_tool_call(
                "edit",
                &serde_json::json!({
                    "file_path": "/src/file.rs",
                    "old_string": format!("v{}", i + 1),
                    "new_string": format!("v{}", i + 2)
                }),
                &msg_call,
                &call_id,
            );
            manager.classifier_mut().classify_tool_result(
                "edit",
                &format!(r#"{{"file_path":"/src/file.rs","version":"v{}"}}"#, i + 2),
                &msg_result,
                &call_id,
            );
        }

        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // With preserve_edit_history=true and max_edits=3:
        // - Original read should be kept (no superseding read)
        // - First edit (msg-4) should be dropped (older than max_edits)
        // - Last 3 edits should be kept

        // Count how many edit results are kept
        let kept_edits: Vec<_> = ["msg-4", "msg-6", "msg-8", "msg-10"]
            .iter()
            .filter(|id| result.should_include(id))
            .collect();

        assert!(
            kept_edits.len() <= 3,
            "Should keep at most 3 edits, got {}",
            kept_edits.len()
        );
    }

    // ========================================================================
    // Transcript 7: Context Optimization Result Structure
    // ========================================================================

    /// Tests the structure of the optimization result
    #[test]
    fn transcript_optimization_result_structure() {
        let mut manager = test_manager();

        // Simple read-edit-read
        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs"}"#,
            "msg-2",
            "call-1",
        );

        manager.classifier_mut().classify_tool_call(
            "read",
            &serde_json::json!({"file_path": "/src/file.rs"}),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "read",
            r#"{"file_path":"/src/file.rs"}"#,
            "msg-4",
            "call-2",
        );

        let result = manager.compute_optimized_order();

        // Verify result structure
        assert!(
            !result.keep.is_empty() || !result.move_to_end.is_empty(),
            "Should have some messages to keep"
        );

        // Final order should not contain duplicates
        let final_order = result.final_order();
        let unique: std::collections::HashSet<_> = final_order.iter().collect();
        assert_eq!(
            final_order.len(),
            unique.len(),
            "Final order should not have duplicates"
        );

        // Dropped classification keys should not appear in final order
        for dropped in &result.drop {
            assert!(
                !final_order.contains(dropped),
                "Dropped classification key {:?} should not be in final order",
                dropped
            );
        }
    }

    // ========================================================================
    // Transcript 8: Non-File Operations Unaffected
    // ========================================================================

    /// Tests that non-file operations are not affected by optimization
    #[test]
    fn transcript_non_file_operations() {
        let mut manager = test_manager();

        // Non-file tool calls
        manager.classifier_mut().classify_tool_call(
            "web_search",
            &serde_json::json!({"query": "rust tutorials"}),
            "msg-1",
            "call-1",
        );
        manager.classifier_mut().classify_tool_result(
            "web_search",
            r#"{"results": ["result1", "result2"]}"#,
            "msg-2",
            "call-1",
        );

        manager.classifier_mut().classify_tool_call(
            "bash",
            &serde_json::json!({"command": "ls -la"}),
            "msg-3",
            "call-2",
        );
        manager.classifier_mut().classify_tool_result(
            "bash",
            r#"total 100\ndrwxr-xr-x..."#,
            "msg-4",
            "call-2",
        );

        let result = manager.compute_optimized_order();

        // === Expected Results ===
        //
        // All non-file operations should be kept

        assert!(
            result.should_include("msg-1"),
            "Non-file tool calls should be kept"
        );
        assert!(
            result.should_include("msg-2"),
            "Non-file tool results should be kept"
        );
        assert!(
            result.should_include("msg-3"),
            "Bash tool calls should be kept"
        );
        assert!(
            result.should_include("msg-4"),
            "Bash tool results should be kept"
        );
    }

    // ========================================================================
    // Final Order Visualization
    // ========================================================================

    /// Helper to print the optimization result for debugging
    #[allow(dead_code)]
    fn visualize_result(result: &ContextOptimizationResult) -> String {
        let mut output = String::new();
        output.push_str("=== Context Optimization Result ===\n\n");

        output.push_str("KEPT (in place):\n");
        for key in &result.keep {
            output.push_str(&format!("  - {}\n", key));
        }

        output.push_str("\nDROPPED:\n");
        for key in &result.drop {
            output.push_str(&format!("  - {}\n", key));
        }

        output.push_str("\nMOVED TO END:\n");
        for key in &result.move_to_end {
            output.push_str(&format!("  - {}\n", key));
        }

        output.push_str("\nFINAL ORDER:\n");
        for (i, key) in result.final_order().iter().enumerate() {
            output.push_str(&format!("  {}. {}\n", i + 1, key));
        }

        output
    }
}