asupersync 0.3.0

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
//! Cancellation Debt Accumulation Monitor
//!
//! Tracks when cancellation work accumulates faster than it can be processed,
//! potentially leading to resource exhaustion or delayed cleanup. Provides
//! early warning and debt management capabilities.

use crate::types::{CancelKind, CancelReason};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

/// Configuration for the cancellation debt monitor.
#[derive(Debug, Clone)]
pub struct CancellationDebtConfig {
    /// Maximum queue depth before triggering debt alerts.
    pub max_queue_depth: usize,
    /// Maximum time cancellation work can remain pending.
    pub max_pending_duration: Duration,
    /// Sampling window for processing rate calculations.
    pub rate_sampling_window: Duration,
    /// Minimum processing rate (items/sec) before triggering alerts.
    pub min_processing_rate: f64,
    /// Debt threshold as percentage of queue capacity.
    pub debt_threshold_percentage: f64,
    /// Enable automatic debt relief mechanisms.
    pub enable_auto_relief: bool,
    /// Maximum memory for debt tracking.
    pub max_tracking_memory_mb: usize,
}

impl Default for CancellationDebtConfig {
    fn default() -> Self {
        Self {
            max_queue_depth: 10_000,
            max_pending_duration: Duration::from_secs(30),
            rate_sampling_window: Duration::from_secs(60),
            min_processing_rate: 100.0,      // 100 items/sec minimum
            debt_threshold_percentage: 75.0, // 75% of capacity
            enable_auto_relief: false,       // Conservative default
            max_tracking_memory_mb: 50,
        }
    }
}

/// Types of cancellation work that can accumulate debt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WorkType {
    /// Task cancellation cleanup.
    TaskCleanup,
    /// Region closure cleanup.
    RegionCleanup,
    /// Resource finalization.
    ResourceFinalization,
    /// Obligation settlement.
    ObligationSettlement,
    /// Waker cleanup.
    WakerCleanup,
    /// Channel cleanup.
    ChannelCleanup,
}

/// A piece of cancellation work pending processing.
#[derive(Debug, Clone)]
pub struct PendingWork {
    /// Unique identifier for this work item.
    pub work_id: u64,
    /// Type of work.
    pub work_type: WorkType,
    /// Entity responsible for the work.
    pub entity_id: String,
    /// When the work was queued.
    pub queued_at: SystemTime,
    /// Priority level (higher = more urgent).
    pub priority: u32,
    /// Estimated processing cost (arbitrary units).
    pub estimated_cost: u32,
    /// Cancellation reason that triggered this work.
    pub cancel_reason: String,
    /// Cancel kind.
    pub cancel_kind: String,
    /// Dependencies that must complete first.
    pub dependencies: Vec<u64>,
}

/// Snapshot of debt accumulation state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebtSnapshot {
    /// Current time of snapshot.
    pub snapshot_time: SystemTime,
    /// Total pending work items.
    pub total_pending: usize,
    /// Pending work by type.
    pub pending_by_type: HashMap<WorkType, usize>,
    /// Current debt percentage (0-100).
    pub debt_percentage: f64,
    /// Processing rate over last window.
    pub processing_rate: f64,
    /// Queue depth by entity.
    pub entity_queue_depths: HashMap<String, usize>,
    /// Oldest pending work age.
    pub oldest_work_age: Duration,
    /// Memory usage for debt tracking.
    pub memory_usage_mb: f64,
    /// Current alert level.
    pub alert_level: DebtAlertLevel,
}

/// Alert levels for debt accumulation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DebtAlertLevel {
    /// Normal operation, no issues.
    Normal,
    /// Elevated debt levels, monitoring recommended.
    Watch,
    /// High debt levels, intervention recommended.
    Warning,
    /// Critical debt levels, immediate action required.
    Critical,
    /// Debt overflow, system may be unstable.
    Emergency,
}

/// A debt alert notification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebtAlert {
    /// Alert level.
    pub level: DebtAlertLevel,
    /// Alert message.
    pub message: String,
    /// Affected work type.
    pub work_type: Option<WorkType>,
    /// Affected entity.
    pub entity_id: Option<String>,
    /// Current metric value.
    pub metric_value: f64,
    /// Threshold that was exceeded.
    pub threshold: f64,
    /// When alert was generated.
    pub generated_at: SystemTime,
    /// Suggested remediation actions.
    pub remediation_suggestions: Vec<String>,
}

/// Statistics for processing rate calculation.
#[derive(Debug)]
struct ProcessingStats {
    /// Items processed in the current window.
    items_processed: VecDeque<(SystemTime, usize)>,
    /// Total items processed since startup.
    total_processed: AtomicU64,
    /// Last processing rate calculation.
    last_rate: f64,
    /// Rate calculation timestamp.
    last_rate_time: SystemTime,
}

impl ProcessingStats {
    fn new() -> Self {
        Self {
            items_processed: VecDeque::new(),
            total_processed: AtomicU64::new(0),
            last_rate: 0.0,
            last_rate_time: SystemTime::now(),
        }
    }

    fn record_processing(&mut self, count: usize, now: SystemTime) {
        self.items_processed.push_back((now, count));
        self.total_processed
            .fetch_add(count as u64, Ordering::Relaxed);

        // Keep only samples within the window
        let cutoff = now - Duration::from_secs(60); // 1 minute window
        while let Some(&(time, _)) = self.items_processed.front() {
            if time < cutoff {
                self.items_processed.pop_front();
            } else {
                break;
            }
        }
    }

    fn calculate_rate(&mut self, window: Duration, now: SystemTime) -> f64 {
        // Only recalculate if enough time has passed
        if now
            .duration_since(self.last_rate_time)
            .unwrap_or(Duration::ZERO)
            < Duration::from_secs(5)
        {
            return self.last_rate;
        }

        let cutoff = now - window;
        let total_in_window: usize = self
            .items_processed
            .iter()
            .filter(|&&(time, _)| time >= cutoff)
            .map(|&(_, count)| count)
            .sum();

        let rate = if window.as_secs() > 0 {
            total_in_window as f64 / window.as_secs() as f64
        } else {
            0.0
        };

        self.last_rate = rate;
        self.last_rate_time = now;
        rate
    }
}

/// Cancellation debt accumulation monitor.
pub struct CancellationDebtMonitor {
    config: CancellationDebtConfig,
    /// Pending work by work type.
    pending_work: Arc<Mutex<HashMap<WorkType, HashMap<u64, PendingWork>>>>,
    /// Processing statistics by work type.
    processing_stats: Arc<Mutex<HashMap<WorkType, ProcessingStats>>>,
    /// Next work ID.
    next_work_id: AtomicU64,
    /// Current alert level.
    current_alert_level: Arc<Mutex<DebtAlertLevel>>,
    /// Recent alerts.
    recent_alerts: Arc<Mutex<VecDeque<DebtAlert>>>,
    /// Total memory usage estimate.
    memory_usage_bytes: AtomicUsize,
}

impl CancellationDebtMonitor {
    /// Creates a new debt monitor with the given configuration.
    #[must_use]
    pub fn new(config: CancellationDebtConfig) -> Self {
        Self {
            config,
            pending_work: Arc::new(Mutex::new(HashMap::new())),
            processing_stats: Arc::new(Mutex::new(HashMap::new())),
            next_work_id: AtomicU64::new(1),
            current_alert_level: Arc::new(Mutex::new(DebtAlertLevel::Normal)),
            recent_alerts: Arc::new(Mutex::new(VecDeque::new())),
            memory_usage_bytes: AtomicUsize::new(0),
        }
    }

    /// Creates a debt monitor with default configuration.
    #[must_use]
    pub fn default() -> Self {
        Self::new(CancellationDebtConfig::default())
    }

    /// Queue a new piece of cancellation work.
    pub fn queue_work(
        &self,
        work_type: WorkType,
        entity_id: String,
        priority: u32,
        estimated_cost: u32,
        cancel_reason: &CancelReason,
        cancel_kind: CancelKind,
        dependencies: Vec<u64>,
    ) -> u64 {
        let work_id = self.next_work_id.fetch_add(1, Ordering::Relaxed);
        let now = SystemTime::now();

        let work = PendingWork {
            work_id,
            work_type,
            entity_id,
            queued_at: now,
            priority,
            estimated_cost,
            cancel_reason: format!("{cancel_reason:?}"),
            cancel_kind: format!("{cancel_kind:?}"),
            dependencies,
        };

        // Update memory usage estimate
        let work_size = std::mem::size_of::<PendingWork>()
            + work.entity_id.len()
            + work.cancel_reason.len()
            + work.cancel_kind.len();
        self.memory_usage_bytes
            .fetch_add(work_size, Ordering::Relaxed);

        // Add to pending work
        {
            let mut pending = self.pending_work.lock().unwrap();
            pending.entry(work_type).or_default().insert(work_id, work);
        }

        // Check if we need to trigger debt alerts
        self.check_debt_levels();

        work_id
    }

    /// Mark work as completed and remove from pending.
    pub fn complete_work(&self, work_id: u64) -> bool {
        let now = SystemTime::now();
        let mut found_work = None;

        // Find and remove the work
        {
            let mut pending = self.pending_work.lock().unwrap();
            for (work_type, work_map) in pending.iter_mut() {
                if let Some(work) = work_map.remove(&work_id) {
                    found_work = Some((*work_type, work));
                    break;
                }
            }
        }

        if let Some((work_type, work)) = found_work {
            // Update memory usage
            let work_size = std::mem::size_of::<PendingWork>()
                + work.entity_id.len()
                + work.cancel_reason.len()
                + work.cancel_kind.len();
            self.memory_usage_bytes
                .fetch_sub(work_size, Ordering::Relaxed);

            // Update processing statistics
            {
                let mut stats = self.processing_stats.lock().unwrap();
                stats
                    .entry(work_type)
                    .or_insert_with(ProcessingStats::new)
                    .record_processing(1, now);
            }

            true
        } else {
            false
        }
    }

    /// Complete multiple work items at once (batch completion).
    pub fn complete_work_batch(&self, work_ids: &[u64]) -> usize {
        let now = SystemTime::now();
        let mut completed_count = 0;
        let mut completed_by_type: HashMap<WorkType, usize> = HashMap::new();

        // Process completions
        {
            let mut pending = self.pending_work.lock().unwrap();
            for &work_id in work_ids {
                for (work_type, work_map) in pending.iter_mut() {
                    if let Some(work) = work_map.remove(&work_id) {
                        completed_count += 1;
                        *completed_by_type.entry(*work_type).or_default() += 1;

                        // Update memory usage
                        let work_size = std::mem::size_of::<PendingWork>()
                            + work.entity_id.len()
                            + work.cancel_reason.len()
                            + work.cancel_kind.len();
                        self.memory_usage_bytes
                            .fetch_sub(work_size, Ordering::Relaxed);
                        break;
                    }
                }
            }
        }

        // Update processing statistics
        {
            let mut stats = self.processing_stats.lock().unwrap();
            for (work_type, count) in completed_by_type {
                stats
                    .entry(work_type)
                    .or_insert_with(ProcessingStats::new)
                    .record_processing(count, now);
            }
        }

        completed_count
    }

    /// Get current debt snapshot.
    pub fn get_debt_snapshot(&self) -> DebtSnapshot {
        let now = SystemTime::now();
        let pending = self.pending_work.lock().unwrap();

        // Calculate totals
        let mut total_pending = 0;
        let mut pending_by_type = HashMap::new();
        let mut entity_queue_depths = HashMap::new();
        let mut oldest_work_age = Duration::ZERO;

        for (work_type, work_map) in pending.iter() {
            let type_count = work_map.len();
            total_pending += type_count;
            pending_by_type.insert(*work_type, type_count);

            for work in work_map.values() {
                // Track queue depth per entity
                *entity_queue_depths
                    .entry(work.entity_id.clone())
                    .or_default() += 1;

                // Find oldest work
                if let Ok(age) = now.duration_since(work.queued_at) {
                    oldest_work_age = oldest_work_age.max(age);
                }
            }
        }

        // Calculate debt percentage
        let debt_percentage = if self.config.max_queue_depth > 0 {
            (total_pending as f64 / self.config.max_queue_depth as f64) * 100.0
        } else {
            0.0
        };

        // Calculate processing rate
        let processing_rate = {
            let mut stats = self.processing_stats.lock().unwrap();
            let mut total_rate = 0.0;
            for (_, stat) in stats.iter_mut() {
                total_rate += stat.calculate_rate(self.config.rate_sampling_window, now);
            }
            total_rate
        };

        // Memory usage
        let memory_usage_mb =
            self.memory_usage_bytes.load(Ordering::Relaxed) as f64 / (1024.0 * 1024.0);

        // Current alert level
        let alert_level = *self.current_alert_level.lock().unwrap();

        DebtSnapshot {
            snapshot_time: now,
            total_pending,
            pending_by_type,
            debt_percentage,
            processing_rate,
            entity_queue_depths,
            oldest_work_age,
            memory_usage_mb,
            alert_level,
        }
    }

    /// Get pending work for a specific entity.
    pub fn get_entity_pending_work(&self, entity_id: &str) -> Vec<PendingWork> {
        let pending = self.pending_work.lock().unwrap();
        let mut result = Vec::new();

        for work_map in pending.values() {
            for work in work_map.values() {
                if work.entity_id == entity_id {
                    result.push(work.clone());
                }
            }
        }

        result.sort_by(|a, b| b.priority.cmp(&a.priority));
        result
    }

    /// Get the highest priority pending work items.
    pub fn get_priority_work(&self, limit: usize) -> Vec<PendingWork> {
        let pending = self.pending_work.lock().unwrap();
        let mut result = Vec::new();

        for work_map in pending.values() {
            for work in work_map.values() {
                result.push(work.clone());
            }
        }

        result.sort_by(|a, b| {
            // Sort by priority desc, then by age desc
            match b.priority.cmp(&a.priority) {
                std::cmp::Ordering::Equal => b.queued_at.cmp(&a.queued_at),
                other => other,
            }
        });

        result.truncate(limit);
        result
    }

    /// Get recent debt alerts.
    pub fn get_recent_alerts(&self, limit: usize) -> Vec<DebtAlert> {
        let alerts = self.recent_alerts.lock().unwrap();
        alerts.iter().rev().take(limit).cloned().collect()
    }

    /// Clear old alerts beyond a certain age.
    pub fn clear_old_alerts(&self, max_age: Duration) {
        let cutoff = SystemTime::now() - max_age;
        let mut alerts = self.recent_alerts.lock().unwrap();
        alerts.retain(|alert| alert.generated_at > cutoff);
    }

    /// Force cleanup of old pending work (emergency debt relief).
    pub fn emergency_cleanup(&self, max_age: Duration) -> usize {
        let cutoff = SystemTime::now() - max_age;
        let mut cleaned_count = 0;

        {
            let mut pending = self.pending_work.lock().unwrap();
            for work_map in pending.values_mut() {
                let before_count = work_map.len();
                work_map.retain(|_, work| work.queued_at > cutoff);
                cleaned_count += before_count - work_map.len();
            }
        }

        if cleaned_count > 0 {
            self.generate_alert(DebtAlert {
                level: DebtAlertLevel::Emergency,
                message: format!("Emergency cleanup removed {cleaned_count} stale work items"),
                work_type: None,
                entity_id: None,
                metric_value: cleaned_count as f64,
                threshold: 0.0,
                generated_at: SystemTime::now(),
                remediation_suggestions: vec![
                    "Investigate why work items are not being processed".to_string(),
                    "Check for deadlocks or blocked entities".to_string(),
                    "Consider increasing processing capacity".to_string(),
                ],
            });
        }

        cleaned_count
    }

    /// Check current debt levels and trigger alerts if needed.
    fn check_debt_levels(&self) {
        let snapshot = self.get_debt_snapshot();
        let new_alert_level = self.calculate_alert_level(&snapshot);

        let mut current_level = self.current_alert_level.lock().unwrap();
        if new_alert_level != *current_level {
            let old_level = *current_level;
            *current_level = new_alert_level;

            // Generate alert for level change
            self.generate_debt_level_alert(old_level, new_alert_level, &snapshot);
        }

        // Check for specific threshold violations
        self.check_threshold_violations(&snapshot);
    }

    /// Calculate alert level based on current snapshot.
    fn calculate_alert_level(&self, snapshot: &DebtSnapshot) -> DebtAlertLevel {
        // Emergency: Memory usage > 90% or debt > 95%
        if snapshot.memory_usage_mb > (self.config.max_tracking_memory_mb as f64 * 0.9)
            || snapshot.debt_percentage > 95.0
        {
            return DebtAlertLevel::Emergency;
        }

        // Critical: Debt > 90% or very slow processing
        if snapshot.debt_percentage > 90.0
            || (snapshot.processing_rate < self.config.min_processing_rate * 0.1
                && snapshot.total_pending > 100)
        {
            return DebtAlertLevel::Critical;
        }

        // Warning: Debt above threshold or slow processing
        if snapshot.debt_percentage > self.config.debt_threshold_percentage
            || snapshot.processing_rate < self.config.min_processing_rate * 0.5
        {
            return DebtAlertLevel::Warning;
        }

        // Watch: Debt > 50% or oldest work is aging
        if snapshot.debt_percentage > 50.0
            || snapshot.oldest_work_age > self.config.max_pending_duration * 2
        {
            return DebtAlertLevel::Watch;
        }

        DebtAlertLevel::Normal
    }

    /// Generate alert for debt level changes.
    fn generate_debt_level_alert(
        &self,
        _old_level: DebtAlertLevel,
        new_level: DebtAlertLevel,
        snapshot: &DebtSnapshot,
    ) {
        let message = match new_level {
            DebtAlertLevel::Emergency => {
                "EMERGENCY: Cancellation debt overflow detected".to_string()
            }
            DebtAlertLevel::Critical => {
                "CRITICAL: Severe cancellation debt accumulation".to_string()
            }
            DebtAlertLevel::Warning => "WARNING: Elevated cancellation debt levels".to_string(),
            DebtAlertLevel::Watch => "WATCH: Cancellation debt increasing".to_string(),
            DebtAlertLevel::Normal => "INFO: Cancellation debt levels normal".to_string(),
        };

        let remediation_suggestions = match new_level {
            DebtAlertLevel::Emergency => vec![
                "Execute emergency cleanup immediately".to_string(),
                "Scale up processing capacity".to_string(),
                "Investigate system bottlenecks".to_string(),
            ],
            DebtAlertLevel::Critical => vec![
                "Increase cancellation processing rate".to_string(),
                "Consider work prioritization".to_string(),
                "Check for deadlocks or stuck entities".to_string(),
            ],
            DebtAlertLevel::Warning => vec![
                "Monitor processing rates closely".to_string(),
                "Optimize cancellation handlers".to_string(),
                "Consider load shedding if applicable".to_string(),
            ],
            DebtAlertLevel::Watch => vec![
                "Monitor debt accumulation trends".to_string(),
                "Verify processing pipeline health".to_string(),
            ],
            DebtAlertLevel::Normal => vec!["Continue monitoring".to_string()],
        };

        self.generate_alert(DebtAlert {
            level: new_level,
            message,
            work_type: None,
            entity_id: None,
            metric_value: snapshot.debt_percentage,
            threshold: match new_level {
                DebtAlertLevel::Emergency => 95.0,
                DebtAlertLevel::Critical => 90.0,
                DebtAlertLevel::Warning => self.config.debt_threshold_percentage,
                DebtAlertLevel::Watch => 50.0,
                DebtAlertLevel::Normal => 0.0,
            },
            generated_at: snapshot.snapshot_time,
            remediation_suggestions,
        });
    }

    /// Check for specific threshold violations.
    fn check_threshold_violations(&self, snapshot: &DebtSnapshot) {
        // Check processing rate violations by type
        let stats = self.processing_stats.lock().unwrap();
        for (work_type, stat) in stats.iter() {
            if stat.last_rate < self.config.min_processing_rate * 0.1 {
                self.generate_alert(DebtAlert {
                    level: DebtAlertLevel::Warning,
                    message: format!(
                        "Very slow processing rate for {:?}: {:.1}/sec",
                        work_type, stat.last_rate
                    ),
                    work_type: Some(*work_type),
                    entity_id: None,
                    metric_value: stat.last_rate,
                    threshold: self.config.min_processing_rate * 0.1,
                    generated_at: snapshot.snapshot_time,
                    remediation_suggestions: vec![
                        format!("Optimize {:?} processing handlers", work_type),
                        "Check for blocking operations".to_string(),
                    ],
                });
            }
        }

        // Check for entities with excessive queue depths
        for (entity_id, &depth) in &snapshot.entity_queue_depths {
            if depth > 1000 {
                self.generate_alert(DebtAlert {
                    level: DebtAlertLevel::Warning,
                    message: format!("Entity {entity_id} has excessive queue depth: {depth}"),
                    work_type: None,
                    entity_id: Some(entity_id.clone()),
                    metric_value: depth as f64,
                    threshold: 1000.0,
                    generated_at: snapshot.snapshot_time,
                    remediation_suggestions: vec![
                        "Investigate entity-specific bottlenecks".to_string(),
                        "Check for resource leaks in entity cleanup".to_string(),
                    ],
                });
            }
        }
    }

    /// Generate and store an alert.
    #[allow(unused_variables)]
    fn generate_alert(&self, alert: DebtAlert) {
        {
            let mut alerts = self.recent_alerts.lock().unwrap();
            alerts.push_back(alert.clone());

            // Keep alerts bounded
            while alerts.len() > 1000 {
                alerts.pop_front();
            }
        }

        crate::tracing_compat::warn!(
            level = ?alert.level,
            work_type = ?alert.work_type,
            entity_id = ?alert.entity_id,
            metric_value = alert.metric_value,
            threshold = alert.threshold,
            generated_at = ?alert.generated_at,
            message = %alert.message,
            "cancellation debt alert"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{CancelKind, CancelReason};

    #[test]
    fn test_debt_monitor_creation() {
        let config = CancellationDebtConfig::default();
        let monitor = CancellationDebtMonitor::new(config);

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 0);
        assert_eq!(snapshot.debt_percentage, 0.0);
    }

    #[test]
    fn test_work_lifecycle() {
        let monitor = CancellationDebtMonitor::default();

        let work_id = monitor.queue_work(
            WorkType::TaskCleanup,
            "test-task".to_string(),
            10,
            100,
            &CancelReason::user("test"),
            CancelKind::User,
            Vec::new(),
        );

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 1);
        assert!(
            snapshot
                .pending_by_type
                .contains_key(&WorkType::TaskCleanup)
        );

        let completed = monitor.complete_work(work_id);
        assert!(completed);

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 0);
    }

    #[test]
    fn test_debt_calculation() {
        let mut config = CancellationDebtConfig::default();
        config.max_queue_depth = 100;
        let monitor = CancellationDebtMonitor::new(config);

        // Queue 75 items (should trigger warning at 75% threshold)
        for i in 0..75 {
            monitor.queue_work(
                WorkType::TaskCleanup,
                format!("task-{}", i),
                1,
                10,
                &CancelReason::user("test"),
                CancelKind::User,
                Vec::new(),
            );
        }

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 75);
        assert_eq!(snapshot.debt_percentage, 75.0);
    }

    #[test]
    fn test_batch_completion() {
        let monitor = CancellationDebtMonitor::default();

        let work_ids: Vec<u64> = (0..5)
            .map(|i| {
                monitor.queue_work(
                    WorkType::ResourceFinalization,
                    format!("resource-{}", i),
                    1,
                    50,
                    &CancelReason::user("batch_test"),
                    CancelKind::User,
                    Vec::new(),
                )
            })
            .collect();

        let completed = monitor.complete_work_batch(&work_ids);
        assert_eq!(completed, 5);

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 0);
    }

    #[test]
    fn test_priority_work_retrieval() {
        let monitor = CancellationDebtMonitor::default();

        // Queue work with different priorities
        monitor.queue_work(
            WorkType::TaskCleanup,
            "low-priority".to_string(),
            1,
            10,
            &CancelReason::user("test"),
            CancelKind::User,
            Vec::new(),
        );

        monitor.queue_work(
            WorkType::TaskCleanup,
            "high-priority".to_string(),
            100,
            10,
            &CancelReason::user("test"),
            CancelKind::User,
            Vec::new(),
        );

        let priority_work = monitor.get_priority_work(5);
        assert_eq!(priority_work.len(), 2);
        assert_eq!(priority_work[0].priority, 100); // High priority first
        assert_eq!(priority_work[1].priority, 1);
    }

    #[test]
    fn test_emergency_cleanup() {
        let monitor = CancellationDebtMonitor::default();

        // Queue some work and artificially age it
        monitor.queue_work(
            WorkType::ChannelCleanup,
            "old-work".to_string(),
            1,
            10,
            &CancelReason::user("test"),
            CancelKind::User,
            Vec::new(),
        );

        // Emergency cleanup with very short age (should clean everything)
        let cleaned = monitor.emergency_cleanup(Duration::from_millis(1));
        assert!(cleaned > 0);

        let snapshot = monitor.get_debt_snapshot();
        assert_eq!(snapshot.total_pending, 0);
    }
}