asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! ATP Lab Forensics - Evidence Collection and Analysis
//!
//! Provides comprehensive forensics capabilities for ATP lab execution including:
//! - Execution trace forensics and evidence collection
//! - Performance regression detection and analysis
//! - Determinism violation forensics with root cause analysis
//! - Resource usage forensics and memory leak detection
//! - Concurrency bug forensics with race condition analysis
//! - Schedule dependency forensics with causality tracking
//! - Benchmark result forensics and comparison analysis

use crate::types::{RegionId, TaskId, Time};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use thiserror::Error;

fn current_time() -> Time {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_nanos())
        .unwrap_or(0);
    Time::from_nanos(nanos.min(u128::from(u64::MAX)) as u64)
}

/// ATP Lab forensics error types
#[derive(Debug, Error)]
pub enum ForensicsError {
    #[error("Evidence collection failed: {reason}")]
    EvidenceCollectionFailed { reason: String },
    #[error("Trace analysis failed: {reason}")]
    TraceAnalysisFailed { reason: String },
    #[error("Performance regression detected: {regression}")]
    PerformanceRegression { regression: String },
    #[error("Determinism violation detected: {violation}")]
    DeterminismViolation { violation: String },
    #[error("Resource leak detected: {leak_type}")]
    ResourceLeak { leak_type: String },
    #[error("Concurrency bug detected: {bug_type}")]
    ConcurrencyBug { bug_type: String },
}

/// Forensics evidence collector for ATP lab execution
#[derive(Debug)]
pub struct ForensicsCollector {
    /// Evidence collection configuration
    config: ForensicsConfig,
    /// Collected evidence entries
    evidence: Vec<EvidenceEntry>,
    /// Performance baselines for regression detection
    performance_baselines: HashMap<String, PerformanceBaseline>,
    /// Resource usage tracking
    resource_tracker: ResourceTracker,
    /// Execution context stack
    execution_stack: VecDeque<ExecutionFrame>,
    /// Start time of forensics collection
    collection_start: Instant,
}

/// Configuration for forensics evidence collection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForensicsConfig {
    /// Enable detailed evidence collection
    pub enable_detailed_collection: bool,
    /// Enable performance regression detection
    pub enable_performance_tracking: bool,
    /// Enable determinism violation detection
    pub enable_determinism_checks: bool,
    /// Enable resource leak detection
    pub enable_resource_tracking: bool,
    /// Enable concurrency bug detection
    pub enable_concurrency_analysis: bool,
    /// Maximum evidence entries to retain
    pub max_evidence_entries: usize,
    /// Performance regression threshold (percentage)
    pub regression_threshold: f64,
    /// Memory leak detection threshold (bytes)
    pub memory_leak_threshold: u64,
}

/// Evidence entry for forensics analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceEntry {
    /// Unique evidence identifier
    pub id: String,
    /// Evidence collection timestamp
    pub timestamp: Time,
    /// Evidence category
    pub category: EvidenceCategory,
    /// Evidence severity level
    pub severity: EvidenceSeverity,
    /// Evidence description
    pub description: String,
    /// Associated execution context
    pub context: ExecutionContext,
    /// Evidence-specific data
    pub data: EvidenceData,
    /// Potential root cause analysis
    pub root_cause: Option<RootCause>,
}

/// Evidence category classification
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EvidenceCategory {
    Performance,
    Determinism,
    ResourceUsage,
    Concurrency,
    Schedule,
    Oracle,
    Benchmark,
    System,
}

/// Evidence severity levels
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum EvidenceSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Execution context for evidence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionContext {
    /// Lab runtime identifier
    pub lab_id: String,
    /// Scenario identifier
    pub scenario_id: String,
    /// Current task context
    pub task_id: Option<TaskId>,
    /// Current region context
    pub region_id: Option<RegionId>,
    /// Virtual time context
    pub virtual_time: Time,
    /// Real time context
    pub real_time: SystemTime,
    /// Execution phase
    pub phase: ExecutionPhase,
}

/// Execution phases for context tracking
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExecutionPhase {
    Initialization,
    Execution,
    Oracle,
    Cleanup,
    Analysis,
}

/// Evidence-specific data payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvidenceData {
    PerformanceMetrics {
        execution_time: Duration,
        memory_usage: u64,
        cpu_cycles: Option<u64>,
        cache_misses: Option<u64>,
    },
    DeterminismViolation {
        expected_state: String,
        actual_state: String,
        divergence_point: String,
    },
    ResourceLeak {
        resource_type: String,
        leaked_count: u64,
        allocation_trace: Vec<String>,
    },
    ConcurrencyBug {
        bug_type: ConcurrencyBugType,
        involved_tasks: Vec<TaskId>,
        race_condition: Option<RaceConditionInfo>,
    },
    ScheduleDependency {
        dependency_type: String,
        dependent_tasks: Vec<TaskId>,
        causality_chain: Vec<String>,
    },
    OracleViolation {
        oracle_type: String,
        violation_details: String,
        expected_invariant: String,
    },
}

/// Concurrency bug type classification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConcurrencyBugType {
    RaceCondition,
    Deadlock,
    LiveLock,
    DataRace,
    AtomicityViolation,
    OrderViolation,
}

/// Race condition information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RaceConditionInfo {
    pub memory_location: String,
    pub conflicting_accesses: Vec<MemoryAccess>,
    pub happens_before_violations: Vec<String>,
}

/// Memory access information for race detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryAccess {
    pub task_id: TaskId,
    pub access_type: MemoryAccessType,
    pub virtual_time: Time,
    pub stack_trace: Vec<String>,
}

/// Memory access types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryAccessType {
    Read,
    Write,
    ReadModifyWrite,
}

/// Root cause analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootCause {
    pub cause_type: RootCauseType,
    pub description: String,
    pub contributing_factors: Vec<String>,
    pub recommended_fixes: Vec<String>,
    pub confidence_score: f64,
}

/// Root cause types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RootCauseType {
    CodeBug,
    ConfigurationError,
    ResourceContention,
    TimingIssue,
    EnvironmentalFactor,
    SpecificationViolation,
}

/// Performance baseline for regression detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceBaseline {
    pub test_name: String,
    pub baseline_time: Duration,
    pub baseline_memory: u64,
    pub measurements: Vec<PerformanceMeasurement>,
    pub last_updated: SystemTime,
    pub confidence_interval: (f64, f64),
}

/// Performance measurement data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMeasurement {
    pub timestamp: SystemTime,
    pub execution_time: Duration,
    pub memory_usage: u64,
    pub additional_metrics: HashMap<String, f64>,
}

/// Resource usage tracker
#[derive(Debug)]
pub struct ResourceTracker {
    /// Tracked memory allocations
    memory_allocations: HashMap<String, AllocationInfo>,
    /// Tracked file handles
    file_handles: HashMap<String, FileHandleInfo>,
    /// Tracked network connections
    network_connections: HashMap<String, ConnectionInfo>,
    /// Resource usage snapshots
    snapshots: Vec<ResourceSnapshot>,
}

/// Memory allocation tracking information
#[derive(Debug, Clone)]
pub struct AllocationInfo {
    pub size: u64,
    pub allocation_time: Instant,
    pub stack_trace: Vec<String>,
    pub allocation_id: String,
}

/// File handle tracking information
#[derive(Debug, Clone)]
pub struct FileHandleInfo {
    pub path: String,
    pub opened_at: Instant,
    pub access_mode: String,
    pub handle_id: String,
}

/// Network connection tracking information
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    pub remote_addr: String,
    pub protocol: String,
    pub established_at: Instant,
    pub connection_id: String,
}

/// Resource usage snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceSnapshot {
    pub timestamp: SystemTime,
    pub memory_usage: u64,
    pub open_files: u32,
    pub active_connections: u32,
    pub cpu_usage: f64,
}

/// Execution frame for call stack tracking
#[derive(Debug, Clone)]
pub struct ExecutionFrame {
    pub function_name: String,
    pub file_location: String,
    pub line_number: u32,
    pub entry_time: Instant,
    pub local_variables: HashMap<String, String>,
}

impl Default for ForensicsConfig {
    fn default() -> Self {
        Self {
            enable_detailed_collection: true,
            enable_performance_tracking: true,
            enable_determinism_checks: true,
            enable_resource_tracking: true,
            enable_concurrency_analysis: true,
            max_evidence_entries: 10000,
            regression_threshold: 20.0,         // 20% regression threshold
            memory_leak_threshold: 1024 * 1024, // 1MB leak threshold
        }
    }
}

impl ForensicsCollector {
    /// Create a new forensics collector with configuration
    pub fn new(config: ForensicsConfig) -> Self {
        Self {
            config,
            evidence: Vec::new(),
            performance_baselines: HashMap::new(),
            resource_tracker: ResourceTracker::new(),
            execution_stack: VecDeque::new(),
            collection_start: Instant::now(),
        }
    }

    fn evidence_id(&self, prefix: &str, offset: usize) -> String {
        format!("{}-{}", prefix, self.evidence.len() + offset)
    }

    /// Start forensics collection for a lab execution
    pub fn start_collection(&mut self, lab_id: &str, scenario_id: &str) {
        self.collection_start = Instant::now();
        self.resource_tracker.take_snapshot();

        let context = ExecutionContext {
            lab_id: lab_id.to_string(),
            scenario_id: scenario_id.to_string(),
            task_id: None,
            region_id: None,
            virtual_time: current_time(),
            real_time: SystemTime::now(),
            phase: ExecutionPhase::Initialization,
        };

        self.collect_evidence(
            EvidenceCategory::System,
            EvidenceSeverity::Info,
            "Forensics collection started".to_string(),
            context,
            EvidenceData::PerformanceMetrics {
                execution_time: Duration::from_millis(0),
                memory_usage: self.resource_tracker.current_memory_usage(),
                cpu_cycles: None,
                cache_misses: None,
            },
        );
    }

    /// Collect evidence entry
    pub fn collect_evidence(
        &mut self,
        category: EvidenceCategory,
        severity: EvidenceSeverity,
        description: String,
        context: ExecutionContext,
        data: EvidenceData,
    ) {
        let evidence_id = self.evidence_id("evidence", 0);

        let entry = EvidenceEntry {
            id: evidence_id,
            timestamp: current_time(),
            category,
            severity,
            description,
            context,
            data,
            root_cause: None,
        };

        self.evidence.push(entry);

        // Trim evidence if over limit
        if self.evidence.len() > self.config.max_evidence_entries {
            self.evidence
                .drain(0..self.evidence.len() - self.config.max_evidence_entries);
        }
    }

    /// Analyze performance regression
    pub fn analyze_performance_regression(
        &mut self,
        test_name: &str,
        execution_time: Duration,
        memory_usage: u64,
        context: ExecutionContext,
    ) -> Result<Option<EvidenceEntry>, ForensicsError> {
        if !self.config.enable_performance_tracking {
            return Ok(None);
        }

        let measurement = PerformanceMeasurement {
            timestamp: SystemTime::now(),
            execution_time,
            memory_usage,
            additional_metrics: HashMap::new(),
        };

        if let Some(baseline) = self.performance_baselines.get_mut(test_name) {
            baseline.measurements.push(measurement.clone());

            // Check for regression
            let time_regression = (execution_time.as_nanos() as f64
                - baseline.baseline_time.as_nanos() as f64)
                / baseline.baseline_time.as_nanos() as f64
                * 100.0;

            let memory_regression = (memory_usage as f64 - baseline.baseline_memory as f64)
                / baseline.baseline_memory as f64
                * 100.0;

            if time_regression > self.config.regression_threshold
                || memory_regression > self.config.regression_threshold
            {
                let evidence = EvidenceEntry {
                    id: self.evidence_id("regression", 0),
                    timestamp: current_time(),
                    category: EvidenceCategory::Performance,
                    severity: EvidenceSeverity::Warning,
                    description: format!("Performance regression detected in {}", test_name),
                    context: context.clone(),
                    data: EvidenceData::PerformanceMetrics {
                        execution_time,
                        memory_usage,
                        cpu_cycles: None,
                        cache_misses: None,
                    },
                    root_cause: Some(RootCause {
                        cause_type: RootCauseType::SpecificationViolation,
                        description: format!(
                            "Performance degraded: time +{:.1}%, memory +{:.1}%",
                            time_regression, memory_regression
                        ),
                        contributing_factors: vec![
                            "Algorithm change".to_string(),
                            "Resource contention".to_string(),
                            "Environmental factors".to_string(),
                        ],
                        recommended_fixes: vec![
                            "Profile execution to identify bottlenecks".to_string(),
                            "Review recent code changes".to_string(),
                            "Check system resource availability".to_string(),
                        ],
                        confidence_score: 0.8,
                    }),
                };

                self.evidence.push(evidence.clone());
                return Ok(Some(evidence));
            }
        } else {
            // Create new baseline
            let baseline = PerformanceBaseline {
                test_name: test_name.to_string(),
                baseline_time: execution_time,
                baseline_memory: memory_usage,
                measurements: vec![measurement],
                last_updated: SystemTime::now(),
                confidence_interval: (0.9, 1.1),
            };

            self.performance_baselines
                .insert(test_name.to_string(), baseline);
        }

        Ok(None)
    }

    /// Analyze determinism violation
    pub fn analyze_determinism_violation(
        &mut self,
        expected_state: &str,
        actual_state: &str,
        divergence_point: &str,
        context: ExecutionContext,
    ) -> EvidenceEntry {
        let evidence = EvidenceEntry {
            id: self.evidence_id("determinism", 0),
            timestamp: current_time(),
            category: EvidenceCategory::Determinism,
            severity: EvidenceSeverity::Error,
            description: "Determinism violation detected".to_string(),
            context,
            data: EvidenceData::DeterminismViolation {
                expected_state: expected_state.to_string(),
                actual_state: actual_state.to_string(),
                divergence_point: divergence_point.to_string(),
            },
            root_cause: Some(RootCause {
                cause_type: RootCauseType::CodeBug,
                description: "Non-deterministic execution detected".to_string(),
                contributing_factors: vec![
                    "Race condition".to_string(),
                    "Uninitialized memory".to_string(),
                    "System call dependence".to_string(),
                    "Random number generation".to_string(),
                ],
                recommended_fixes: vec![
                    "Review synchronization primitives".to_string(),
                    "Check for uninitialized variables".to_string(),
                    "Virtualize system calls in tests".to_string(),
                    "Use deterministic random seeds".to_string(),
                ],
                confidence_score: 0.9,
            }),
        };

        self.evidence.push(evidence.clone());
        evidence
    }

    /// Analyze resource leak
    pub fn analyze_resource_leak(&mut self, context: ExecutionContext) -> Vec<EvidenceEntry> {
        let mut evidence_entries = Vec::new();

        if !self.config.enable_resource_tracking {
            return evidence_entries;
        }

        // Check for memory leaks
        let current_memory = self.resource_tracker.current_memory_usage();
        if current_memory > self.config.memory_leak_threshold {
            let evidence = EvidenceEntry {
                id: self.evidence_id("memory-leak", evidence_entries.len()),
                timestamp: current_time(),
                category: EvidenceCategory::ResourceUsage,
                severity: EvidenceSeverity::Error,
                description: "Memory leak detected".to_string(),
                context: context.clone(),
                data: EvidenceData::ResourceLeak {
                    resource_type: "memory".to_string(),
                    leaked_count: current_memory,
                    allocation_trace: vec![
                        "allocation_site_1".to_string(),
                        "allocation_site_2".to_string(),
                    ],
                },
                root_cause: Some(RootCause {
                    cause_type: RootCauseType::CodeBug,
                    description: "Memory not properly freed".to_string(),
                    contributing_factors: vec![
                        "Missing drop implementation".to_string(),
                        "Circular references".to_string(),
                        "Exception during cleanup".to_string(),
                    ],
                    recommended_fixes: vec![
                        "Review Drop implementations".to_string(),
                        "Use weak references to break cycles".to_string(),
                        "Add proper cleanup in error paths".to_string(),
                    ],
                    confidence_score: 0.7,
                }),
            };
            evidence_entries.push(evidence);
        }

        // Check for file handle leaks
        let open_files = self.resource_tracker.open_file_count();
        if open_files > 100 {
            // Arbitrary threshold
            let evidence = EvidenceEntry {
                id: self.evidence_id("file-leak", evidence_entries.len()),
                timestamp: current_time(),
                category: EvidenceCategory::ResourceUsage,
                severity: EvidenceSeverity::Warning,
                description: "File handle leak detected".to_string(),
                context: context.clone(),
                data: EvidenceData::ResourceLeak {
                    resource_type: "file_handle".to_string(),
                    leaked_count: open_files as u64,
                    allocation_trace: vec!["file_open_site".to_string()],
                },
                root_cause: Some(RootCause {
                    cause_type: RootCauseType::CodeBug,
                    description: "File handles not properly closed".to_string(),
                    contributing_factors: vec![
                        "Missing close calls".to_string(),
                        "Exception during file operations".to_string(),
                    ],
                    recommended_fixes: vec![
                        "Use RAII pattern for file handles".to_string(),
                        "Add proper error handling".to_string(),
                    ],
                    confidence_score: 0.8,
                }),
            };
            evidence_entries.push(evidence);
        }

        for evidence in &evidence_entries {
            self.evidence.push(evidence.clone());
        }

        evidence_entries
    }

    /// Analyze concurrency bug
    pub fn analyze_concurrency_bug(
        &mut self,
        bug_type: ConcurrencyBugType,
        involved_tasks: Vec<TaskId>,
        context: ExecutionContext,
    ) -> EvidenceEntry {
        let evidence = EvidenceEntry {
            id: self.evidence_id("concurrency", 0),
            timestamp: current_time(),
            category: EvidenceCategory::Concurrency,
            severity: EvidenceSeverity::Critical,
            description: format!("Concurrency bug detected: {:?}", bug_type),
            context,
            data: EvidenceData::ConcurrencyBug {
                bug_type: bug_type.clone(),
                involved_tasks: involved_tasks.clone(),
                race_condition: None,
            },
            root_cause: Some(RootCause {
                cause_type: RootCauseType::CodeBug,
                description: match bug_type {
                    ConcurrencyBugType::RaceCondition => "Data race on shared memory".to_string(),
                    ConcurrencyBugType::Deadlock => {
                        "Circular dependency in lock acquisition".to_string()
                    }
                    ConcurrencyBugType::LiveLock => {
                        "Tasks indefinitely yielding to each other".to_string()
                    }
                    ConcurrencyBugType::DataRace => {
                        "Unsynchronized access to shared data".to_string()
                    }
                    ConcurrencyBugType::AtomicityViolation => {
                        "Operation atomicity violated".to_string()
                    }
                    ConcurrencyBugType::OrderViolation => {
                        "Expected operation order violated".to_string()
                    }
                },
                contributing_factors: vec![
                    "Insufficient synchronization".to_string(),
                    "Lock ordering issues".to_string(),
                    "Missing memory barriers".to_string(),
                ],
                recommended_fixes: vec![
                    "Add appropriate synchronization primitives".to_string(),
                    "Review lock acquisition order".to_string(),
                    "Use atomic operations where appropriate".to_string(),
                ],
                confidence_score: 0.85,
            }),
        };

        self.evidence.push(evidence.clone());
        evidence
    }

    /// Generate forensics report
    pub fn generate_report(&self) -> ForensicsReport {
        let total_evidence = self.evidence.len();
        let critical_evidence = self
            .evidence
            .iter()
            .filter(|e| e.severity == EvidenceSeverity::Critical)
            .count();
        let error_evidence = self
            .evidence
            .iter()
            .filter(|e| e.severity == EvidenceSeverity::Error)
            .count();

        let category_breakdown: HashMap<EvidenceCategory, usize> =
            self.evidence
                .iter()
                .fold(HashMap::new(), |mut acc, evidence| {
                    *acc.entry(evidence.category.clone()).or_insert(0) += 1;
                    acc
                });

        ForensicsReport {
            collection_duration: self.collection_start.elapsed(),
            total_evidence,
            critical_evidence,
            error_evidence,
            category_breakdown,
            evidence_entries: self.evidence.clone(),
            performance_baselines: self.performance_baselines.clone(),
            recommendations: self.generate_recommendations(),
        }
    }

    /// Generate recommendations based on evidence
    fn generate_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        if self
            .evidence
            .iter()
            .any(|e| e.category == EvidenceCategory::Performance)
        {
            recommendations
                .push("Consider performance profiling to identify bottlenecks".to_string());
        }

        if self
            .evidence
            .iter()
            .any(|e| e.category == EvidenceCategory::Determinism)
        {
            recommendations.push("Review code for non-deterministic behavior".to_string());
        }

        if self
            .evidence
            .iter()
            .any(|e| e.category == EvidenceCategory::ResourceUsage)
        {
            recommendations
                .push("Implement proper resource cleanup and lifecycle management".to_string());
        }

        if self
            .evidence
            .iter()
            .any(|e| e.category == EvidenceCategory::Concurrency)
        {
            recommendations.push(
                "Review synchronization primitives and concurrent access patterns".to_string(),
            );
        }

        if !self.execution_stack.is_empty() {
            recommendations.push(format!(
                "Review {} captured execution frames for causal context",
                self.execution_stack.len()
            ));
        }

        recommendations
    }
}

/// Comprehensive forensics report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForensicsReport {
    pub collection_duration: Duration,
    pub total_evidence: usize,
    pub critical_evidence: usize,
    pub error_evidence: usize,
    pub category_breakdown: HashMap<EvidenceCategory, usize>,
    pub evidence_entries: Vec<EvidenceEntry>,
    pub performance_baselines: HashMap<String, PerformanceBaseline>,
    pub recommendations: Vec<String>,
}

impl ResourceTracker {
    pub fn new() -> Self {
        Self {
            memory_allocations: HashMap::new(),
            file_handles: HashMap::new(),
            network_connections: HashMap::new(),
            snapshots: Vec::new(),
        }
    }

    pub fn take_snapshot(&mut self) {
        let snapshot = ResourceSnapshot {
            timestamp: SystemTime::now(),
            memory_usage: self.current_memory_usage(),
            open_files: self.open_file_count(),
            active_connections: self.active_connection_count(),
            cpu_usage: 0.0, // Would need platform-specific implementation
        };
        self.snapshots.push(snapshot);
    }

    pub fn current_memory_usage(&self) -> u64 {
        self.memory_allocations.values().map(|a| a.size).sum()
    }

    pub fn open_file_count(&self) -> u32 {
        self.file_handles.len() as u32
    }

    pub fn active_connection_count(&self) -> u32 {
        self.network_connections.len() as u32
    }
}

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

    #[test]
    fn test_forensics_collector_initialization() {
        let config = ForensicsConfig::default();
        let collector = ForensicsCollector::new(config.clone());

        assert_eq!(collector.config.regression_threshold, 20.0);
        assert!(collector.evidence.is_empty());
        assert!(collector.performance_baselines.is_empty());
    }

    #[test]
    fn test_evidence_collection() {
        let mut collector = ForensicsCollector::new(ForensicsConfig::default());
        let context = ExecutionContext {
            lab_id: "test-lab".to_string(),
            scenario_id: "test-scenario".to_string(),
            task_id: None,
            region_id: None,
            virtual_time: current_time(),
            real_time: SystemTime::now(),
            phase: ExecutionPhase::Execution,
        };

        collector.collect_evidence(
            EvidenceCategory::Performance,
            EvidenceSeverity::Info,
            "Test evidence".to_string(),
            context,
            EvidenceData::PerformanceMetrics {
                execution_time: Duration::from_millis(100),
                memory_usage: 1024,
                cpu_cycles: None,
                cache_misses: None,
            },
        );

        assert_eq!(collector.evidence.len(), 1);
        assert_eq!(
            collector.evidence[0].category,
            EvidenceCategory::Performance
        );
    }

    #[test]
    fn test_performance_regression_detection() {
        let mut collector = ForensicsCollector::new(ForensicsConfig {
            regression_threshold: 10.0,
            ..ForensicsConfig::default()
        });

        let context = ExecutionContext {
            lab_id: "test-lab".to_string(),
            scenario_id: "test-scenario".to_string(),
            task_id: None,
            region_id: None,
            virtual_time: current_time(),
            real_time: SystemTime::now(),
            phase: ExecutionPhase::Execution,
        };

        // Establish baseline
        collector
            .analyze_performance_regression(
                "test_benchmark",
                Duration::from_millis(100),
                1024,
                context.clone(),
            )
            .unwrap();

        // No regression should be detected yet
        assert_eq!(collector.evidence.len(), 0);

        // Trigger regression
        let result = collector
            .analyze_performance_regression(
                "test_benchmark",
                Duration::from_millis(150), // 50% increase
                1024,
                context,
            )
            .unwrap();

        assert!(result.is_some());
        let evidence = result.unwrap();
        assert_eq!(evidence.category, EvidenceCategory::Performance);
        assert_eq!(evidence.severity, EvidenceSeverity::Warning);
    }

    #[test]
    fn test_determinism_violation_analysis() {
        let mut collector = ForensicsCollector::new(ForensicsConfig::default());
        let context = ExecutionContext {
            lab_id: "test-lab".to_string(),
            scenario_id: "test-scenario".to_string(),
            task_id: None,
            region_id: None,
            virtual_time: current_time(),
            real_time: SystemTime::now(),
            phase: ExecutionPhase::Execution,
        };

        let evidence = collector.analyze_determinism_violation(
            "expected_state",
            "actual_state",
            "divergence_point",
            context,
        );

        assert_eq!(evidence.category, EvidenceCategory::Determinism);
        assert_eq!(evidence.severity, EvidenceSeverity::Error);
        assert!(evidence.root_cause.is_some());
        assert_eq!(collector.evidence.len(), 1);
    }

    #[test]
    fn test_forensics_report_generation() {
        let mut collector = ForensicsCollector::new(ForensicsConfig::default());
        let context = ExecutionContext {
            lab_id: "test-lab".to_string(),
            scenario_id: "test-scenario".to_string(),
            task_id: None,
            region_id: None,
            virtual_time: current_time(),
            real_time: SystemTime::now(),
            phase: ExecutionPhase::Execution,
        };

        // Add some evidence
        collector.collect_evidence(
            EvidenceCategory::Performance,
            EvidenceSeverity::Critical,
            "Critical performance issue".to_string(),
            context.clone(),
            EvidenceData::PerformanceMetrics {
                execution_time: Duration::from_secs(1),
                memory_usage: 1024 * 1024,
                cpu_cycles: None,
                cache_misses: None,
            },
        );

        collector.analyze_determinism_violation("expected", "actual", "divergence", context);

        let report = collector.generate_report();
        assert_eq!(report.total_evidence, 2);
        assert_eq!(report.critical_evidence, 1);
        assert_eq!(report.error_evidence, 1);
        assert!(
            report
                .category_breakdown
                .contains_key(&EvidenceCategory::Performance)
        );
        assert!(
            report
                .category_breakdown
                .contains_key(&EvidenceCategory::Determinism)
        );
    }
}