reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! # Performance Tracking System
//!
//! This module provides comprehensive performance monitoring for long-horizon operations,
//! tracking tool calls, resource usage, and optimization opportunities across 100+ tool calling sequences.

use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::error::Error;

/// Real-time performance tracker for long-horizon operations
pub struct PerformanceTracker {
    /// Real-time metrics collector
    real_time_metrics: Arc<Mutex<RealTimeMetrics>>,
    /// Performance history for analysis
    performance_history: Arc<Mutex<VecDeque<PerformanceRecord>>>,
    /// Resource utilization tracker
    resource_tracker: Arc<Mutex<ResourceUtilizationTracker>>,
    /// Optimization analyzer
    optimizer: Arc<Mutex<PerformanceOptimizer>>,
    /// Configuration
    config: PerformanceTrackerConfig,
}

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

impl PerformanceTracker {
    pub fn new() -> Self {
        Self {
            real_time_metrics: Arc::new(Mutex::new(RealTimeMetrics::new())),
            performance_history: Arc::new(Mutex::new(VecDeque::new())),
            resource_tracker: Arc::new(Mutex::new(ResourceUtilizationTracker::new())),
            optimizer: Arc::new(Mutex::new(PerformanceOptimizer::new())),
            config: PerformanceTrackerConfig::default(),
        }
    }

    /// Record a tool call with performance metrics
    pub async fn record_tool_call(
        &self,
        tool_call_id: u32,
        duration_ms: u64,
        cost: f64,
    ) -> Result<(), Error> {
        let timestamp = chrono::Utc::now();

        // Update real-time metrics
        {
            let mut metrics = self.real_time_metrics.lock().await;
            metrics.record_tool_call(tool_call_id, duration_ms, cost, timestamp);
        }

        // Update resource utilization
        {
            let mut tracker = self.resource_tracker.lock().await;
            tracker.record_tool_call(duration_ms).await;
        }

        // Analyze performance for optimization opportunities
        {
            let mut optimizer = self.optimizer.lock().await;
            optimizer
                .analyze_tool_call(tool_call_id, duration_ms, cost)
                .await;
        }

        tracing::debug!(
            "Recorded tool call {}: {}ms, ${:.4}",
            tool_call_id,
            duration_ms,
            cost
        );

        Ok(())
    }

    /// Record execution of a complex operation
    pub async fn record_operation(
        &self,
        operation_name: &str,
        tool_calls_used: u32,
        duration_ms: u64,
        success: bool,
        metadata: serde_json::Value,
    ) -> Result<(), Error> {
        let timestamp = chrono::Utc::now();
        let record = PerformanceRecord {
            id: format!("op_{}_{}", operation_name, timestamp.timestamp()),
            operation_name: operation_name.to_string(),
            timestamp,
            tool_calls_used,
            duration_ms,
            success,
            metadata,
        };

        // Add to history
        {
            let mut history = self.performance_history.lock().await;
            history.push_back(record.clone());

            // Maintain history limit
            if history.len() > self.config.max_history_records {
                history.pop_front();
            }
        }

        // Update real-time metrics
        {
            let mut metrics = self.real_time_metrics.lock().await;
            metrics.record_operation(&record);
        }

        // Analyze operation performance
        {
            let mut optimizer = self.optimizer.lock().await;
            optimizer.analyze_operation(&record).await;
        }

        tracing::info!(
            "Recorded operation '{}': {} tool calls, {}ms, success: {}",
            operation_name,
            tool_calls_used,
            duration_ms,
            success
        );

        Ok(())
    }

    /// Get current real-time metrics
    pub async fn get_real_time_metrics(&self) -> Result<RealTimeMetrics, Error> {
        let metrics = self.real_time_metrics.lock().await;
        Ok(metrics.clone())
    }

    /// Get performance summary for a time window
    pub async fn get_performance_summary(
        &self,
        window_duration: chrono::Duration,
    ) -> Result<PerformanceSummary, Error> {
        let now = chrono::Utc::now();
        let cutoff = now - window_duration;

        let history = self.performance_history.lock().await;
        let recent_records: Vec<_> = history
            .iter()
            .filter(|record| record.timestamp >= cutoff)
            .cloned()
            .collect();

        if recent_records.is_empty() {
            return Ok(PerformanceSummary::empty());
        }

        let total_tool_calls: u32 = recent_records.iter().map(|r| r.tool_calls_used).sum();
        let total_duration_ms: u64 = recent_records.iter().map(|r| r.duration_ms).sum();
        let success_count = recent_records.iter().filter(|r| r.success).count();
        let total_records = recent_records.len();

        let avg_tool_calls_per_operation = total_tool_calls as f64 / total_records as f64;
        let avg_duration_per_operation = total_duration_ms as f64 / total_records as f64;
        let success_rate = success_count as f64 / total_records as f64;

        // Calculate throughput (operations per minute)
        let minutes = window_duration.num_minutes() as f64;
        let throughput_per_minute = if minutes > 0.0 {
            total_records as f64 / minutes
        } else {
            0.0
        };

        // Calculate efficiency score
        let efficiency_score = self.calculate_efficiency_score(&recent_records);

        // Identify performance bottlenecks
        let bottlenecks = self.identify_bottlenecks(&recent_records).await;

        Ok(PerformanceSummary {
            time_window: window_duration,
            total_operations: total_records,
            total_tool_calls,
            total_duration_ms,
            avg_tool_calls_per_operation,
            avg_duration_per_operation,
            success_rate,
            throughput_per_minute,
            efficiency_score,
            bottlenecks,
            top_performing_operations: self.get_top_performing_operations(&recent_records),
            recommendations: self
                .generate_optimization_recommendations(&recent_records)
                .await,
        })
    }

    /// Get resource utilization statistics
    pub async fn get_resource_utilization(&self) -> Result<ResourceUtilization, Error> {
        let tracker = self.resource_tracker.lock().await;
        Ok(tracker.get_utilization_stats())
    }

    /// Get optimization recommendations
    pub async fn get_optimization_recommendations(
        &self,
    ) -> Result<Vec<OptimizationRecommendation>, Error> {
        let optimizer = self.optimizer.lock().await;
        Ok(optimizer.get_recommendations().await)
    }

    /// Reset performance tracking
    pub async fn reset(&self) -> Result<(), Error> {
        {
            let mut metrics = self.real_time_metrics.lock().await;
            metrics.reset();
        }

        {
            let mut history = self.performance_history.lock().await;
            history.clear();
        }

        {
            let mut tracker = self.resource_tracker.lock().await;
            tracker.reset();
        }

        {
            let mut optimizer = self.optimizer.lock().await;
            optimizer.reset();
        }

        tracing::info!("Performance tracker reset");
        Ok(())
    }

    /// Calculate efficiency score based on multiple factors
    fn calculate_efficiency_score(&self, records: &[PerformanceRecord]) -> f64 {
        if records.is_empty() {
            return 1.0;
        }

        let success_rate =
            records.iter().filter(|r| r.success).count() as f64 / records.len() as f64;

        // Calculate average tool calls efficiency
        let avg_tool_calls =
            records.iter().map(|r| r.tool_calls_used).sum::<u32>() as f64 / records.len() as f64;
        let tool_call_efficiency = (avg_tool_calls / 100.0).min(1.0); // Normalize to 100 tool calls

        // Calculate time efficiency
        let avg_duration =
            records.iter().map(|r| r.duration_ms).sum::<u64>() as f64 / records.len() as f64;
        let time_efficiency = (60000.0 / avg_duration).min(1.0); // Normalize to 1 minute per operation

        // Weighted combination
        (success_rate * 0.4 + tool_call_efficiency * 0.3 + time_efficiency * 0.3).clamp(0.0, 1.0)
    }

    /// Identify performance bottlenecks
    async fn identify_bottlenecks(
        &self,
        records: &[PerformanceRecord],
    ) -> Vec<PerformanceBottleneck> {
        let mut bottlenecks = Vec::new();

        // Analyze operation durations
        let avg_duration =
            records.iter().map(|r| r.duration_ms).sum::<u64>() as f64 / records.len() as f64;
        let slow_operations: Vec<_> = records
            .iter()
            .filter(|r| r.duration_ms as f64 > avg_duration * 2.0)
            .collect();

        if !slow_operations.is_empty() {
            bottlenecks.push(PerformanceBottleneck {
                type_: BottleneckType::SlowOperations,
                severity: if slow_operations.len() as f64 / records.len() as f64 > 0.3 {
                    BottleneckSeverity::High
                } else {
                    BottleneckSeverity::Medium
                },
                description: format!(
                    "{} operations are taking longer than average ({}ms avg)",
                    slow_operations.len(),
                    avg_duration as u64
                ),
                affected_operations: slow_operations
                    .iter()
                    .map(|r| r.operation_name.clone())
                    .collect(),
            });
        }

        // Analyze tool call counts
        let avg_tool_calls =
            records.iter().map(|r| r.tool_calls_used).sum::<u32>() as f64 / records.len() as f64;
        let high_tool_call_ops: Vec<_> = records
            .iter()
            .filter(|r| r.tool_calls_used as f64 > avg_tool_calls * 2.0)
            .collect();

        if !high_tool_call_ops.is_empty() {
            bottlenecks.push(PerformanceBottleneck {
                type_: BottleneckType::ExcessiveToolCalls,
                severity: if high_tool_call_ops.len() as f64 / records.len() as f64 > 0.2 {
                    BottleneckSeverity::High
                } else {
                    BottleneckSeverity::Low
                },
                description: format!(
                    "{} operations use excessive tool calls ({} avg)",
                    high_tool_call_ops.len(),
                    avg_tool_calls
                ),
                affected_operations: high_tool_call_ops
                    .iter()
                    .map(|r| r.operation_name.clone())
                    .collect(),
            });
        }

        // Analyze failure rates
        let failure_rate =
            records.iter().filter(|r| !r.success).count() as f64 / records.len() as f64;
        if failure_rate > 0.1 {
            bottlenecks.push(PerformanceBottleneck {
                type_: BottleneckType::HighFailureRate,
                severity: if failure_rate > 0.3 {
                    BottleneckSeverity::Critical
                } else {
                    BottleneckSeverity::High
                },
                description: format!("High failure rate: {:.1}%", failure_rate * 100.0),
                affected_operations: records
                    .iter()
                    .filter(|r| !r.success)
                    .map(|r| r.operation_name.clone())
                    .collect(),
            });
        }

        bottlenecks
    }

    /// Get top performing operations
    fn get_top_performing_operations(
        &self,
        records: &[PerformanceRecord],
    ) -> Vec<OperationPerformance> {
        let mut operations: HashMap<String, Vec<&PerformanceRecord>> = HashMap::new();

        // Group records by operation name
        for record in records {
            operations
                .entry(record.operation_name.clone())
                .or_default()
                .push(record);
        }

        // Calculate performance metrics for each operation
        let mut performance_data = Vec::new();
        for (op_name, op_records) in operations {
            let total_calls: u32 = op_records.iter().map(|r| r.tool_calls_used).sum();
            let total_duration: u64 = op_records.iter().map(|r| r.duration_ms).sum();
            let success_count = op_records.iter().filter(|r| r.success).count();
            let total_ops = op_records.len();

            performance_data.push(OperationPerformance {
                operation_name: op_name,
                total_executions: total_ops,
                avg_tool_calls: total_calls as f64 / total_ops as f64,
                avg_duration_ms: total_duration as f64 / total_ops as f64,
                success_rate: success_count as f64 / total_ops as f64,
                efficiency_score: self.calculate_operation_efficiency(&op_records),
            });
        }

        // Sort by efficiency score and return top 5
        performance_data
            .sort_by(|a, b| b.efficiency_score.partial_cmp(&a.efficiency_score).unwrap());
        performance_data.into_iter().take(5).collect()
    }

    /// Calculate efficiency score for a specific operation
    fn calculate_operation_efficiency(&self, records: &[&PerformanceRecord]) -> f64 {
        if records.is_empty() {
            return 0.0;
        }

        let success_rate =
            records.iter().filter(|r| r.success).count() as f64 / records.len() as f64;
        let avg_duration =
            records.iter().map(|r| r.duration_ms).sum::<u64>() as f64 / records.len() as f64;
        let avg_tool_calls =
            records.iter().map(|r| r.tool_calls_used).sum::<u32>() as f64 / records.len() as f64;

        // Efficiency is inverse of duration and tool calls, weighted by success rate
        let duration_score = (10000.0 / avg_duration).min(1.0);
        let tool_call_score = (50.0 / avg_tool_calls).min(1.0);

        success_rate * 0.5 + duration_score * 0.3 + tool_call_score * 0.2
    }

    /// Generate optimization recommendations
    async fn generate_optimization_recommendations(
        &self,
        records: &[PerformanceRecord],
    ) -> Vec<OptimizationRecommendation> {
        let mut recommendations = Vec::new();

        // Analyze patterns and generate recommendations
        let avg_duration =
            records.iter().map(|r| r.duration_ms).sum::<u64>() as f64 / records.len() as f64;
        let avg_tool_calls =
            records.iter().map(|r| r.tool_calls_used).sum::<u32>() as f64 / records.len() as f64;

        if avg_duration > 30000.0 {
            recommendations.push(OptimizationRecommendation {
                priority: RecommendationPriority::High,
                category: RecommendationCategory::Performance,
                title: "Optimize Operation Duration".to_string(),
                description: format!(
                    "Average operation duration is {:.1} seconds. Consider optimizing algorithms or parallelizing operations.",
                    avg_duration / 1000.0
                ),
                estimated_impact: ImpactLevel::High,
                implementation_effort: ImplementationEffort::Medium,
            });
        }

        if avg_tool_calls > 50.0 {
            recommendations.push(OptimizationRecommendation {
                priority: RecommendationPriority::Medium,
                category: RecommendationCategory::ToolEfficiency,
                title: "Reduce Tool Call Count".to_string(),
                description: format!(
                    "Average tool calls per operation is {:.1}. Consider batching operations or caching results.",
                    avg_tool_calls
                ),
                estimated_impact: ImpactLevel::Medium,
                implementation_effort: ImplementationEffort::Low,
            });
        }

        // Check for consistent patterns that could benefit from caching
        let operation_counts: HashMap<String, usize> = records
            .iter()
            .map(|r| (r.operation_name.clone(), 1))
            .fold(HashMap::new(), |mut acc, (name, count)| {
                *acc.entry(name).or_insert(0) += count;
                acc
            });

        let frequently_used_ops: Vec<_> = operation_counts
            .iter()
            .filter(|(_, count)| **count >= 5)
            .map(|(name, _)| name)
            .collect();

        if !frequently_used_ops.is_empty() {
            recommendations.push(OptimizationRecommendation {
                priority: RecommendationPriority::Medium,
                category: RecommendationCategory::Caching,
                title: "Implement Result Caching".to_string(),
                description: format!(
                    "Operations {} are executed frequently. Consider implementing result caching to improve performance.",
                    frequently_used_ops.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ")
                ),
                estimated_impact: ImpactLevel::High,
                implementation_effort: ImplementationEffort::Low,
            });
        }

        recommendations
    }
}

/// Real-time performance metrics
#[derive(Debug, Clone)]
pub struct RealTimeMetrics {
    pub current_tool_call_id: u32,
    pub total_tool_calls_today: u32,
    pub avg_duration_ms: f64,
    pub avg_cost_per_call: f64,
    pub current_throughput_per_minute: f64,
    pub error_rate: f64,
    pub last_update: chrono::DateTime<chrono::Utc>,
    pub rolling_window: VecDeque<PerformanceSample>,
}

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

impl RealTimeMetrics {
    pub fn new() -> Self {
        Self {
            current_tool_call_id: 0,
            total_tool_calls_today: 0,
            avg_duration_ms: 0.0,
            avg_cost_per_call: 0.0,
            current_throughput_per_minute: 0.0,
            error_rate: 0.0,
            last_update: chrono::Utc::now(),
            rolling_window: VecDeque::new(),
        }
    }

    pub fn record_tool_call(
        &mut self,
        tool_call_id: u32,
        duration_ms: u64,
        cost: f64,
        timestamp: chrono::DateTime<chrono::Utc>,
    ) {
        self.current_tool_call_id = tool_call_id;
        self.total_tool_calls_today += 1;
        self.last_update = timestamp;

        // Add to rolling window (last 100 calls)
        let sample = PerformanceSample {
            timestamp,
            duration_ms,
            cost,
            success: true, // Would be determined by actual execution
        };

        self.rolling_window.push_back(sample);
        if self.rolling_window.len() > 100 {
            self.rolling_window.pop_front();
        }

        // Update averages
        let total_samples = self.rolling_window.len() as f64;
        self.avg_duration_ms = self
            .rolling_window
            .iter()
            .map(|s| s.duration_ms as f64)
            .sum::<f64>()
            / total_samples;

        self.avg_cost_per_call =
            self.rolling_window.iter().map(|s| s.cost).sum::<f64>() / total_samples;

        // Calculate throughput (calls per minute in rolling window)
        if let (Some(first), Some(last)) = (self.rolling_window.front(), self.rolling_window.back())
        {
            let time_span = (last.timestamp - first.timestamp).num_seconds() as f64;
            if time_span > 0.0 {
                self.current_throughput_per_minute = (total_samples * 60.0) / time_span;
            }
        }
    }

    pub fn record_operation(&mut self, record: &PerformanceRecord) {
        // Update error rate based on success
        let total_ops = self.rolling_window.len() as f64 + 1.0;
        let successful_ops = self.rolling_window.iter().filter(|s| s.success).count() as f64
            + if record.success { 1.0 } else { 0.0 };
        self.error_rate = 1.0 - (successful_ops / total_ops);
    }

    pub fn reset(&mut self) {
        self.current_tool_call_id = 0;
        self.total_tool_calls_today = 0;
        self.avg_duration_ms = 0.0;
        self.avg_cost_per_call = 0.0;
        self.current_throughput_per_minute = 0.0;
        self.error_rate = 0.0;
        self.rolling_window.clear();
    }
}

/// Performance sample for rolling window
#[derive(Debug, Clone)]
pub struct PerformanceSample {
    timestamp: chrono::DateTime<chrono::Utc>,
    duration_ms: u64,
    cost: f64,
    success: bool,
}

/// Resource utilization tracker
#[derive(Debug)]
struct ResourceUtilizationTracker {
    memory_usage_mb: VecDeque<f64>,
    cpu_usage_percent: VecDeque<f64>,
    network_io_mb: VecDeque<f64>,
    disk_io_mb: VecDeque<f64>,
    peak_memory_mb: f64,
    peak_cpu_percent: f64,
}

impl ResourceUtilizationTracker {
    fn new() -> Self {
        Self {
            memory_usage_mb: VecDeque::new(),
            cpu_usage_percent: VecDeque::new(),
            network_io_mb: VecDeque::new(),
            disk_io_mb: VecDeque::new(),
            peak_memory_mb: 0.0,
            peak_cpu_percent: 0.0,
        }
    }

    async fn record_tool_call(&mut self, duration_ms: u64) {
        // Simulate resource usage (in real implementation, would get actual system metrics)
        let simulated_memory = 100.0 + (duration_ms as f64 / 1000.0) * 10.0;
        let simulated_cpu = 10.0 + (duration_ms as f64 / 1000.0) * 5.0;
        let simulated_network = duration_ms as f64 / 1000.0 * 2.0;
        let simulated_disk = duration_ms as f64 / 1000.0 * 1.0;

        self.memory_usage_mb.push_back(simulated_memory);
        self.cpu_usage_percent.push_back(simulated_cpu);
        self.network_io_mb.push_back(simulated_network);
        self.disk_io_mb.push_back(simulated_disk);

        // Maintain rolling window
        let max_samples = 50;
        if self.memory_usage_mb.len() > max_samples {
            self.memory_usage_mb.pop_front();
            self.cpu_usage_percent.pop_front();
            self.network_io_mb.pop_front();
            self.disk_io_mb.pop_front();
        }

        // Update peaks
        self.peak_memory_mb = self.peak_memory_mb.max(simulated_memory);
        self.peak_cpu_percent = self.peak_cpu_percent.max(simulated_cpu);
    }

    fn get_utilization_stats(&self) -> ResourceUtilization {
        let avg_memory = if !self.memory_usage_mb.is_empty() {
            self.memory_usage_mb.iter().sum::<f64>() / self.memory_usage_mb.len() as f64
        } else {
            0.0
        };

        let avg_cpu = if !self.cpu_usage_percent.is_empty() {
            self.cpu_usage_percent.iter().sum::<f64>() / self.cpu_usage_percent.len() as f64
        } else {
            0.0
        };

        let avg_network = if !self.network_io_mb.is_empty() {
            self.network_io_mb.iter().sum::<f64>() / self.network_io_mb.len() as f64
        } else {
            0.0
        };

        let avg_disk = if !self.disk_io_mb.is_empty() {
            self.disk_io_mb.iter().sum::<f64>() / self.disk_io_mb.len() as f64
        } else {
            0.0
        };

        ResourceUtilization {
            avg_memory_usage_mb: avg_memory,
            peak_memory_usage_mb: self.peak_memory_mb,
            avg_cpu_usage_percent: avg_cpu,
            peak_cpu_usage_percent: self.peak_cpu_percent,
            avg_network_io_mb_per_call: avg_network,
            avg_disk_io_mb_per_call: avg_disk,
            memory_efficiency: if self.peak_memory_mb > 0.0 {
                avg_memory / self.peak_memory_mb
            } else {
                1.0
            },
        }
    }

    fn reset(&mut self) {
        self.memory_usage_mb.clear();
        self.cpu_usage_percent.clear();
        self.network_io_mb.clear();
        self.disk_io_mb.clear();
        self.peak_memory_mb = 0.0;
        self.peak_cpu_percent = 0.0;
    }
}

/// Performance optimizer for identifying improvements
#[derive(Debug)]
struct PerformanceOptimizer {
    analysis_cache: HashMap<String, PerformanceAnalysis>,
    optimization_history: VecDeque<OptimizationAction>,
}

impl PerformanceOptimizer {
    fn new() -> Self {
        Self {
            analysis_cache: HashMap::new(),
            optimization_history: VecDeque::new(),
        }
    }

    async fn analyze_tool_call(&mut self, _tool_call_id: u32, duration_ms: u64, cost: f64) {
        // Analyze individual tool call for patterns
        if duration_ms > 10_000 {
            // 10 seconds
            tracing::warn!("Slow tool call detected: {}ms", duration_ms);
        }

        if cost > 0.01 {
            // $0.01
            tracing::info!("High-cost tool call: ${:.4}", cost);
        }
    }

    async fn analyze_operation(&mut self, record: &PerformanceRecord) {
        // Analyze operation patterns
        let analysis_key = format!(
            "{}_{}",
            record.operation_name,
            record.timestamp.date_naive()
        );

        let analysis = PerformanceAnalysis {
            operation_name: record.operation_name.clone(),
            avg_duration_ms: record.duration_ms as f64,
            avg_tool_calls: record.tool_calls_used as f64,
            success_rate: if record.success { 1.0 } else { 0.0 },
            last_analysis: chrono::Utc::now(),
        };

        self.analysis_cache.insert(analysis_key, analysis);
    }

    async fn get_recommendations(&self) -> Vec<OptimizationRecommendation> {
        // Generate recommendations based on analysis cache
        let mut recommendations = Vec::new();

        for analysis in self.analysis_cache.values() {
            if analysis.avg_duration_ms > 30000.0 {
                recommendations.push(OptimizationRecommendation {
                    priority: RecommendationPriority::High,
                    category: RecommendationCategory::Performance,
                    title: format!("Optimize {} duration", analysis.operation_name),
                    description: format!(
                        "Operation '{}' averages {:.1} seconds. Consider optimization.",
                        analysis.operation_name,
                        analysis.avg_duration_ms / 1000.0
                    ),
                    estimated_impact: ImpactLevel::High,
                    implementation_effort: ImplementationEffort::Medium,
                });
            }

            if analysis.avg_tool_calls > 30.0 {
                recommendations.push(OptimizationRecommendation {
                    priority: RecommendationPriority::Medium,
                    category: RecommendationCategory::ToolEfficiency,
                    title: format!("Reduce {} tool calls", analysis.operation_name),
                    description: format!(
                        "Operation '{}' uses {:.1} tool calls on average.",
                        analysis.operation_name, analysis.avg_tool_calls
                    ),
                    estimated_impact: ImpactLevel::Medium,
                    implementation_effort: ImplementationEffort::Low,
                });
            }
        }

        recommendations
    }

    fn reset(&mut self) {
        self.analysis_cache.clear();
        self.optimization_history.clear();
    }
}

/// Performance record for history tracking
#[derive(Debug, Clone)]
pub struct PerformanceRecord {
    pub id: String,
    pub operation_name: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub tool_calls_used: u32,
    pub duration_ms: u64,
    pub success: bool,
    pub metadata: serde_json::Value,
}

/// Performance analysis
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct PerformanceAnalysis {
    operation_name: String,
    avg_duration_ms: f64,
    avg_tool_calls: f64,
    success_rate: f64,
    last_analysis: chrono::DateTime<chrono::Utc>,
}

/// Performance summary
#[derive(Debug, Clone)]
pub struct PerformanceSummary {
    pub time_window: chrono::Duration,
    pub total_operations: usize,
    pub total_tool_calls: u32,
    pub total_duration_ms: u64,
    pub avg_tool_calls_per_operation: f64,
    pub avg_duration_per_operation: f64,
    pub success_rate: f64,
    pub throughput_per_minute: f64,
    pub efficiency_score: f64,
    pub bottlenecks: Vec<PerformanceBottleneck>,
    pub top_performing_operations: Vec<OperationPerformance>,
    pub recommendations: Vec<OptimizationRecommendation>,
}

impl PerformanceSummary {
    pub fn empty() -> Self {
        Self {
            time_window: chrono::Duration::minutes(0),
            total_operations: 0,
            total_tool_calls: 0,
            total_duration_ms: 0,
            avg_tool_calls_per_operation: 0.0,
            avg_duration_per_operation: 0.0,
            success_rate: 1.0,
            throughput_per_minute: 0.0,
            efficiency_score: 1.0,
            bottlenecks: Vec::new(),
            top_performing_operations: Vec::new(),
            recommendations: Vec::new(),
        }
    }
}

/// Performance bottleneck
#[derive(Debug, Clone)]
pub struct PerformanceBottleneck {
    pub type_: BottleneckType,
    pub severity: BottleneckSeverity,
    pub description: String,
    pub affected_operations: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum BottleneckType {
    SlowOperations,
    ExcessiveToolCalls,
    HighFailureRate,
    ResourceContention,
    NetworkLatency,
}

#[derive(Debug, Clone, PartialEq)]
pub enum BottleneckSeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Operation performance metrics
#[derive(Debug, Clone)]
pub struct OperationPerformance {
    pub operation_name: String,
    pub total_executions: usize,
    pub avg_tool_calls: f64,
    pub avg_duration_ms: f64,
    pub success_rate: f64,
    pub efficiency_score: f64,
}

/// Optimization recommendation
#[derive(Debug, Clone)]
pub struct OptimizationRecommendation {
    pub priority: RecommendationPriority,
    pub category: RecommendationCategory,
    pub title: String,
    pub description: String,
    pub estimated_impact: ImpactLevel,
    pub implementation_effort: ImplementationEffort,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RecommendationPriority {
    Low,
    Medium,
    High,
    Critical,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RecommendationCategory {
    Performance,
    ToolEfficiency,
    Caching,
    ResourceManagement,
    ErrorHandling,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ImpactLevel {
    Low,
    Medium,
    High,
    VeryHigh,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ImplementationEffort {
    Low,
    Medium,
    High,
}

/// Resource utilization statistics
#[derive(Debug, Clone)]
pub struct ResourceUtilization {
    pub avg_memory_usage_mb: f64,
    pub peak_memory_usage_mb: f64,
    pub avg_cpu_usage_percent: f64,
    pub peak_cpu_usage_percent: f64,
    pub avg_network_io_mb_per_call: f64,
    pub avg_disk_io_mb_per_call: f64,
    pub memory_efficiency: f64,
}

/// Optimization action
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct OptimizationAction {
    timestamp: chrono::DateTime<chrono::Utc>,
    action_type: String,
    target: String,
    expected_improvement: f64,
    actual_improvement: Option<f64>,
}

/// Configuration for performance tracker
#[derive(Debug, Clone)]
pub struct PerformanceTrackerConfig {
    pub max_history_records: usize,
    pub rolling_window_size: usize,
    pub enable_real_time_monitoring: bool,
    pub enable_resource_tracking: bool,
    pub enable_optimization: bool,
    pub analysis_interval_minutes: u32,
}

impl Default for PerformanceTrackerConfig {
    fn default() -> Self {
        Self {
            max_history_records: 10000,
            rolling_window_size: 100,
            enable_real_time_monitoring: true,
            enable_resource_tracking: true,
            enable_optimization: true,
            analysis_interval_minutes: 5,
        }
    }
}

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

    #[tokio::test]
    async fn test_performance_tracker_creation() {
        let tracker = PerformanceTracker::new();
        assert!(tracker.record_tool_call(1, 1000, 0.001).await.is_ok());
    }

    #[tokio::test]
    async fn test_real_time_metrics() {
        let mut metrics = RealTimeMetrics::new();
        metrics.record_tool_call(1, 2000, 0.002, chrono::Utc::now());

        assert_eq!(metrics.total_tool_calls_today, 1);
        assert_eq!(metrics.avg_duration_ms, 2000.0);
    }

    #[tokio::test]
    async fn test_performance_summary() {
        let tracker = PerformanceTracker::new();

        let _record = PerformanceRecord {
            id: "test1".to_string(),
            operation_name: "test_op".to_string(),
            timestamp: chrono::Utc::now(),
            tool_calls_used: 10,
            duration_ms: 5000,
            success: true,
            metadata: serde_json::json!({}),
        };

        assert!(tracker
            .record_operation("test_op", 10, 5000, true, serde_json::json!({}))
            .await
            .is_ok());
    }
}