asupersync 0.3.1

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
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
//! Channel Waker Deduplication Verifier
//!
//! This oracle verifies that the Arc<AtomicBool> waker deduplication pattern
//! in channels correctly prevents spurious wakeups while ensuring no lost wakeups.
//! Incorrect implementation can cause deadlocks where tasks aren't properly woken.
//!
//! # Core Patterns Verified
//!
//! 1. **Queued State Consistency**: Arc<AtomicBool> accurately reflects queued state
//! 2. **No Lost Wakeups**: Tasks that should be woken are actually woken
//! 3. **No Spurious Wakeups**: Tasks aren't woken unnecessarily
//! 4. **Race-Free Registration**: Waker registration doesn't race with wakeup
//! 5. **Proper Cleanup**: Dropped wakers are properly cleaned up
//!
//! # Usage
//!
//! ```rust
//! use asupersync::lab::oracle::waker_dedup::{WakerDedupOracle, WakerDedupConfig};
//!
//! let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
//!     track_queued_state: true,
//!     track_wakeup_events: true,
//!     enforcement: EnforcementMode::Panic,
//!     max_tracked_wakers: 10000,
//!     ..Default::default()
//! });
//!
//! // Hook into waker operations
//! oracle.on_waker_registered(waker_id, channel_id, is_queued);
//! oracle.on_waker_wake_requested(waker_id, reason);
//! oracle.on_waker_actually_woken(waker_id);
//! oracle.on_waker_dropped(waker_id);
//! ```

use crate::trace::distributed::TraceId;
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{SystemTime, UNIX_EPOCH};

/// Unique identifier for a waker instance
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WakerId(pub u64);

/// Unique identifier for a channel
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ChannelId(pub u64);

/// Configuration for the Waker Deduplication Oracle
#[derive(Debug, Clone)]
pub struct WakerDedupConfig {
    /// Whether to track waker queued state changes
    pub track_queued_state: bool,
    /// Whether to track wakeup events and timing
    pub track_wakeup_events: bool,
    /// Whether to detect registration races
    pub detect_registration_races: bool,
    /// Whether to track waker cleanup on drop
    pub track_cleanup: bool,
    /// Enforcement mode for violations
    pub enforcement: EnforcementMode,
    /// Enable structured logging for violations
    pub structured_logging: bool,
    /// Include stack traces in violation records
    pub include_stack_traces: bool,
    /// Enable replay command generation for violations
    pub enable_replay_commands: bool,
    /// Maximum number of violations to track before dropping oldest
    pub max_violations_tracked: usize,
    /// Maximum number of wakers to track per channel
    pub max_wakers_per_channel: usize,
    /// Maximum number of total wakers to track
    pub max_tracked_wakers: usize,
    /// Time window for detecting race conditions (milliseconds)
    pub race_detection_window_ms: u64,
}

impl Default for WakerDedupConfig {
    fn default() -> Self {
        Self {
            track_queued_state: true,
            track_wakeup_events: true,
            detect_registration_races: true,
            track_cleanup: true,
            enforcement: EnforcementMode::Warn,
            structured_logging: false,
            include_stack_traces: false,
            enable_replay_commands: false,
            max_violations_tracked: 1000,
            max_wakers_per_channel: 1000,
            max_tracked_wakers: 10000,
            race_detection_window_ms: 10, // 10ms race detection window
        }
    }
}

/// Enforcement mode for waker deduplication violations
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EnforcementMode {
    /// Only collect violations, no immediate action
    Collect,
    /// Emit warnings for violations
    Warn,
    /// Panic on violations (for testing)
    Panic,
}

/// Types of waker deduplication violations
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WakerDedupViolation {
    /// Waker was registered but never woken when it should have been
    LostWakeup {
        /// ID of the waker that was lost.
        waker_id: WakerId,
        /// ID of the channel where the wakeup was lost.
        channel_id: ChannelId,
        /// When the waker was originally registered.
        registered_at: SystemTime,
        /// When the wakeup was expected to occur.
        expected_wake_at: SystemTime,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Waker was woken when it wasn't supposed to be (spurious wakeup)
    SpuriousWakeup {
        /// ID of the spuriously woken waker.
        waker_id: WakerId,
        /// ID of the channel where spurious wakeup occurred.
        channel_id: ChannelId,
        /// When the spurious wakeup occurred.
        woken_at: SystemTime,
        /// Human-readable reason for the spurious wakeup.
        reason: String,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Waker state inconsistency between oracle tracking and actual state
    InconsistentQueuedState {
        /// ID of the waker with inconsistent state.
        waker_id: WakerId,
        /// ID of the channel with the inconsistency.
        channel_id: ChannelId,
        /// Whether the waker was expected to be queued.
        expected_queued: bool,
        /// Whether the waker was actually queued.
        actual_queued: bool,
        /// When the inconsistency was detected.
        detected_at: SystemTime,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Race condition detected between waker registration and wakeup
    RegistrationRace {
        /// ID of the waker involved in the race.
        waker_id: WakerId,
        /// ID of the channel where the race occurred.
        channel_id: ChannelId,
        /// When the waker registration started.
        registration_time: SystemTime,
        /// When the wakeup occurred.
        wakeup_time: SystemTime,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Waker was woken multiple times without re-registration
    DoubleWakeup {
        /// ID of the doubly-woken waker.
        waker_id: WakerId,
        /// ID of the channel where double wakeup occurred.
        channel_id: ChannelId,
        /// When the first wakeup occurred.
        first_wake_at: SystemTime,
        /// When the second wakeup occurred.
        second_wake_at: SystemTime,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Waker was leaked (not properly cleaned up)
    WakerLeak {
        /// ID of the leaked waker.
        waker_id: WakerId,
        /// ID of the channel where the waker was leaked.
        channel_id: ChannelId,
        /// When the waker was originally registered.
        registered_at: SystemTime,
        /// When the leak was detected.
        detected_at: SystemTime,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
    /// Waker operation on unknown or already-dropped waker
    UseAfterDrop {
        /// ID of the dropped waker that was used.
        waker_id: WakerId,
        /// ID of the channel where use-after-drop occurred.
        channel_id: ChannelId,
        /// When the waker was originally dropped.
        dropped_at: SystemTime,
        /// When the illegal operation was attempted.
        operation_at: SystemTime,
        /// Description of the operation that was attempted.
        operation: String,
        /// Optional trace ID for correlation.
        trace_id: Option<TraceId>,
    },
}

impl std::fmt::Display for WakerDedupViolation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::LostWakeup {
                waker_id,
                channel_id,
                registered_at,
                expected_wake_at,
                trace_id,
            } => {
                write!(
                    f,
                    "Lost wakeup: waker {waker_id:?} on channel {channel_id:?} registered at {registered_at:?}, expected wake at {expected_wake_at:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::SpuriousWakeup {
                waker_id,
                channel_id,
                woken_at,
                reason,
                trace_id,
            } => {
                write!(
                    f,
                    "Spurious wakeup: waker {waker_id:?} on channel {channel_id:?} woken at {woken_at:?}, reason: {reason}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::InconsistentQueuedState {
                waker_id,
                channel_id,
                expected_queued,
                actual_queued,
                detected_at,
                trace_id,
            } => {
                write!(
                    f,
                    "Inconsistent queued state: waker {waker_id:?} on channel {channel_id:?} expected queued={expected_queued}, actual queued={actual_queued}, detected at {detected_at:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::RegistrationRace {
                waker_id,
                channel_id,
                registration_time,
                wakeup_time,
                trace_id,
            } => {
                write!(
                    f,
                    "Registration race: waker {waker_id:?} on channel {channel_id:?} registered at {registration_time:?}, woken at {wakeup_time:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::DoubleWakeup {
                waker_id,
                channel_id,
                first_wake_at,
                second_wake_at,
                trace_id,
            } => {
                write!(
                    f,
                    "Double wakeup: waker {waker_id:?} on channel {channel_id:?} first woken at {first_wake_at:?}, second at {second_wake_at:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::WakerLeak {
                waker_id,
                channel_id,
                registered_at,
                detected_at,
                trace_id,
            } => {
                write!(
                    f,
                    "Waker leak: waker {waker_id:?} on channel {channel_id:?} registered at {registered_at:?}, detected at {detected_at:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
            Self::UseAfterDrop {
                waker_id,
                channel_id,
                dropped_at,
                operation_at,
                operation,
                trace_id,
            } => {
                write!(
                    f,
                    "Use after drop: waker {waker_id:?} on channel {channel_id:?} dropped at {dropped_at:?}, operation '{operation}' at {operation_at:?}"
                )?;
                if let Some(trace) = trace_id {
                    write!(f, " (trace: {trace:?})")?;
                }
                Ok(())
            }
        }
    }
}

/// Enhanced violation record with diagnostics
#[derive(Debug, Clone)]
pub struct ViolationRecord {
    /// The underlying waker deduplication violation.
    pub violation: WakerDedupViolation,
    /// When the violation was recorded.
    pub timestamp: SystemTime,
    /// Optional trace ID for correlation across systems.
    pub trace_id: Option<TraceId>,
    /// Optional stack trace captured at violation time.
    pub stack_trace: Option<String>,
    /// Optional command to replay the scenario.
    pub replay_command: Option<String>,
}

impl ViolationRecord {
    /// Create a new violation record with enhanced diagnostics.
    #[must_use]
    pub fn new(violation: WakerDedupViolation, config: &WakerDedupConfig) -> Self {
        let trace_id = match &violation {
            WakerDedupViolation::LostWakeup { trace_id, .. } => *trace_id,
            WakerDedupViolation::SpuriousWakeup { trace_id, .. } => *trace_id,
            WakerDedupViolation::InconsistentQueuedState { trace_id, .. } => *trace_id,
            WakerDedupViolation::RegistrationRace { trace_id, .. } => *trace_id,
            WakerDedupViolation::DoubleWakeup { trace_id, .. } => *trace_id,
            WakerDedupViolation::WakerLeak { trace_id, .. } => *trace_id,
            WakerDedupViolation::UseAfterDrop { trace_id, .. } => *trace_id,
        };

        let stack_trace = if config.include_stack_traces {
            Some(Self::capture_stack_trace())
        } else {
            None
        };

        let replay_command = if config.enable_replay_commands {
            trace_id.map(|tid| format!("asupersync-replay --trace-id {tid:?}"))
        } else {
            None
        };

        Self {
            violation,
            timestamp: SystemTime::now(),
            trace_id,
            stack_trace,
            replay_command,
        }
    }

    /// Emit a structured log entry for this violation record.
    #[allow(unused_variables)]
    pub fn emit_structured_log(&self) {
        let timestamp_millis = self
            .timestamp
            .duration_since(UNIX_EPOCH)
            .map_or(0, |d| d.as_millis());

        crate::tracing_compat::error!(
            violation_type = "waker_dedup_violation",
            timestamp_millis = timestamp_millis,
            violation = %self.violation,
            trace_id = ?self.trace_id,
            replay_command = ?self.replay_command,
            stack_trace = ?self.stack_trace,
            "waker deduplication violation"
        );
    }

    fn capture_stack_trace() -> String {
        // In a real implementation, this would capture the actual stack trace
        "Stack trace capture not implemented in this preview".to_string()
    }
}

/// State tracking for a waker
#[derive(Debug, Clone)]
pub struct WakerState {
    /// Unique identifier for this waker.
    pub waker_id: WakerId,
    /// ID of the channel this waker is associated with.
    pub channel_id: ChannelId,
    /// When this waker was registered.
    pub registered_at: SystemTime,
    /// Optional trace ID for correlation.
    pub trace_id: Option<TraceId>,
    /// Current status of the waker.
    pub status: WakerStatus,
    /// When the last operation on this waker occurred.
    pub last_operation_at: SystemTime,
}

/// Current status of a waker in the deduplication system.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WakerStatus {
    /// Waker is registered and queued for wakeup
    Queued,
    /// Waker has been woken and is no longer queued
    Woken {
        /// When the waker was woken.
        at: SystemTime,
    },
    /// Waker has been dropped/cleaned up
    Dropped {
        /// When the waker was dropped.
        at: SystemTime,
    },
}

/// Channel-level statistics for waker deduplication
#[derive(Debug, Clone, Default)]
pub struct WakerDedupStatistics {
    /// Total number of wakers registered.
    pub total_wakers_registered: u64,
    /// Total number of wakers successfully woken.
    pub total_wakers_woken: u64,
    /// Total number of wakers that were dropped/cleaned up.
    pub total_wakers_dropped: u64,
    /// Total number of lost wakeup incidents.
    pub total_lost_wakeups: u64,
    /// Total number of spurious wakeup incidents.
    pub total_spurious_wakeups: u64,
    /// Total number of double wakeup incidents.
    pub total_double_wakeups: u64,
    /// Total number of registration race incidents.
    pub total_registration_races: u64,
    /// Total number of state inconsistency incidents.
    pub total_state_inconsistencies: u64,
    /// Total number of waker leak incidents.
    pub total_leaks: u64,
    /// Total number of use-after-drop incidents.
    pub total_use_after_drop: u64,
    /// Total number of violations across all categories.
    pub total_violations: u64,
    /// Number of currently active wakers.
    pub active_wakers: u64,
}

/// Waker Deduplication Oracle
#[derive(Debug)]
pub struct WakerDedupOracle {
    config: WakerDedupConfig,
    /// Track waker state by waker ID
    wakers: HashMap<WakerId, WakerState>,
    /// Track wakers by channel for cleanup
    channel_wakers: HashMap<ChannelId, HashSet<WakerId>>,
    /// Track recent registration events for race detection
    recent_registrations: VecDeque<(WakerId, SystemTime)>,
    /// Track recent wakeup events for race detection
    recent_wakeups: VecDeque<(WakerId, SystemTime)>,
    /// Violations found (legacy format for compatibility)
    violations: Vec<WakerDedupViolation>,
    /// Enhanced violation records with diagnostics
    violation_records: VecDeque<ViolationRecord>,
    /// Statistics
    stats: WakerDedupStatistics,
}

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

impl WakerDedupOracle {
    /// Create a new oracle with the given configuration
    #[must_use]
    pub fn new(config: WakerDedupConfig) -> Self {
        Self {
            config,
            wakers: HashMap::new(),
            channel_wakers: HashMap::new(),
            recent_registrations: VecDeque::new(),
            recent_wakeups: VecDeque::new(),
            violations: Vec::new(),
            violation_records: VecDeque::new(),
            stats: WakerDedupStatistics::default(),
        }
    }

    /// Create oracle with default configuration
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(WakerDedupConfig::default())
    }

    /// Create oracle for runtime use with enhanced detection
    #[must_use]
    pub fn for_runtime() -> Self {
        Self::new(WakerDedupConfig {
            track_queued_state: true,
            track_wakeup_events: true,
            detect_registration_races: true,
            track_cleanup: true,
            enforcement: EnforcementMode::Panic,
            structured_logging: true,
            include_stack_traces: true,
            enable_replay_commands: true,
            max_violations_tracked: 100,
            max_wakers_per_channel: 100,
            max_tracked_wakers: 1000,
            race_detection_window_ms: 5,
        })
    }

    /// Record a waker registration event
    pub fn on_waker_registered(
        &mut self,
        waker_id: WakerId,
        channel_id: ChannelId,
        is_queued: bool,
        trace_id: Option<TraceId>,
    ) {
        if !self.config.track_queued_state {
            return;
        }

        let now = SystemTime::now();

        // Check for race with recent wakeups
        if self.config.detect_registration_races {
            self.check_registration_races(waker_id, now);
        }

        // Record the waker state
        let state = WakerState {
            waker_id,
            channel_id,
            registered_at: now,
            trace_id,
            status: if is_queued {
                WakerStatus::Queued
            } else {
                WakerStatus::Woken { at: now }
            },
            last_operation_at: now,
        };

        self.wakers.insert(waker_id, state);
        self.channel_wakers
            .entry(channel_id)
            .or_default()
            .insert(waker_id);

        // Track for race detection
        self.recent_registrations.push_back((waker_id, now));

        self.stats.total_wakers_registered += 1;
        if is_queued {
            self.stats.active_wakers += 1;
        }

        self.cleanup_tracking_data();
    }

    /// Record a waker wake request (should lead to actual wakeup)
    pub fn on_waker_wake_requested(
        &mut self,
        waker_id: WakerId,
        _reason: String,
        trace_id: Option<TraceId>,
    ) {
        if !self.config.track_wakeup_events {
            return;
        }

        let now = SystemTime::now();

        if let Some(state) = self.wakers.get(&waker_id) {
            match &state.status {
                WakerStatus::Queued => {
                    // Expected: queued waker should be woken
                    // We'll verify this in on_waker_actually_woken
                }
                WakerStatus::Woken { at } => {
                    // Violation: trying to wake already-woken waker
                    let violation = WakerDedupViolation::DoubleWakeup {
                        waker_id,
                        channel_id: state.channel_id,
                        first_wake_at: *at,
                        second_wake_at: now,
                        trace_id,
                    };
                    self.record_violation(violation);
                    self.stats.total_double_wakeups += 1;
                }
                WakerStatus::Dropped { at } => {
                    // Violation: trying to wake dropped waker
                    let violation = WakerDedupViolation::UseAfterDrop {
                        waker_id,
                        channel_id: state.channel_id,
                        dropped_at: *at,
                        operation_at: now,
                        operation: "wake_request".to_string(),
                        trace_id,
                    };
                    self.record_violation(violation);
                    self.stats.total_use_after_drop += 1;
                }
            }
        } else {
            // Spurious wake request - no registered waker
            // We need channel_id, but we don't have it without waker state
            // This might be a legitimate case where waker was already cleaned up
        }
    }

    /// Record an actual waker wakeup event
    pub fn on_waker_actually_woken(&mut self, waker_id: WakerId, trace_id: Option<TraceId>) {
        if !self.config.track_wakeup_events {
            return;
        }

        let now = SystemTime::now();

        if let Some(state) = self.wakers.get_mut(&waker_id) {
            match &state.status {
                WakerStatus::Queued => {
                    // Expected: queued waker is being woken
                    state.status = WakerStatus::Woken { at: now };
                    state.last_operation_at = now;
                    self.stats.total_wakers_woken += 1;
                    self.stats.active_wakers = self.stats.active_wakers.saturating_sub(1);
                }
                WakerStatus::Woken { at } => {
                    // Violation: already-woken waker woken again
                    let violation = WakerDedupViolation::DoubleWakeup {
                        waker_id,
                        channel_id: state.channel_id,
                        first_wake_at: *at,
                        second_wake_at: now,
                        trace_id,
                    };
                    self.record_violation(violation);
                    self.stats.total_double_wakeups += 1;
                }
                WakerStatus::Dropped { at } => {
                    // Violation: dropped waker woken
                    let violation = WakerDedupViolation::UseAfterDrop {
                        waker_id,
                        channel_id: state.channel_id,
                        dropped_at: *at,
                        operation_at: now,
                        operation: "wakeup".to_string(),
                        trace_id,
                    };
                    self.record_violation(violation);
                    self.stats.total_use_after_drop += 1;
                }
            }
        } else {
            // Spurious wakeup - no registered waker
            let violation = WakerDedupViolation::SpuriousWakeup {
                waker_id,
                channel_id: ChannelId(0), // We don't know the channel, use dummy
                woken_at: now,
                reason: "unknown waker".to_string(),
                trace_id,
            };
            self.record_violation(violation);
            self.stats.total_spurious_wakeups += 1;
        }

        // Track for race detection
        self.recent_wakeups.push_back((waker_id, now));
    }

    /// Record a waker drop/cleanup event
    pub fn on_waker_dropped(&mut self, waker_id: WakerId) {
        if !self.config.track_cleanup {
            return;
        }

        let now = SystemTime::now();

        if let Some(state) = self.wakers.get_mut(&waker_id) {
            match &state.status {
                WakerStatus::Queued => {
                    // Waker was dropped while queued - this is normal cleanup
                    self.stats.active_wakers = self.stats.active_wakers.saturating_sub(1);
                }
                WakerStatus::Woken { .. } => {
                    // Waker was dropped after being woken - normal
                }
                WakerStatus::Dropped { .. } => {
                    // Already dropped - shouldn't happen
                }
            }

            state.status = WakerStatus::Dropped { at: now };
            state.last_operation_at = now;
            self.stats.total_wakers_dropped += 1;
        }

        // Note: We don't remove from wakers HashMap immediately to allow
        // detection of use-after-drop violations
    }

    /// Verify queued state consistency
    pub fn verify_queued_state(
        &mut self,
        waker_id: WakerId,
        actual_queued: bool,
        trace_id: Option<TraceId>,
    ) {
        if !self.config.track_queued_state {
            return;
        }

        if let Some(state) = self.wakers.get(&waker_id) {
            let expected_queued = matches!(state.status, WakerStatus::Queued);
            if expected_queued != actual_queued {
                let violation = WakerDedupViolation::InconsistentQueuedState {
                    waker_id,
                    channel_id: state.channel_id,
                    expected_queued,
                    actual_queued,
                    detected_at: SystemTime::now(),
                    trace_id,
                };
                self.record_violation(violation);
                self.stats.total_state_inconsistencies += 1;
            }
        }
    }

    /// Check for violations and return them
    pub fn check_for_violations(&mut self) -> Result<Vec<WakerDedupViolation>, String> {
        self.check_for_leaked_wakers();
        self.check_for_lost_wakeups();
        Ok(self.violations.clone())
    }

    /// Get current statistics
    #[must_use]
    pub fn statistics(&self) -> WakerDedupStatistics {
        self.stats.clone()
    }

    /// Reset the oracle state
    pub fn reset(&mut self) {
        self.wakers.clear();
        self.channel_wakers.clear();
        self.recent_registrations.clear();
        self.recent_wakeups.clear();
        self.violations.clear();
        self.violation_records.clear();
        self.stats = WakerDedupStatistics::default();
    }

    /// Get violation records with diagnostics
    #[must_use]
    pub fn violation_records(&self) -> Vec<ViolationRecord> {
        self.violation_records.iter().cloned().collect()
    }

    /// Record a violation with appropriate enforcement
    fn record_violation(&mut self, violation: WakerDedupViolation) {
        // Legacy format for backward compatibility
        self.violations.push(violation.clone());

        // Enhanced format with diagnostics
        let record = ViolationRecord::new(violation.clone(), &self.config);

        // Emit structured log if enabled
        if self.config.structured_logging {
            record.emit_structured_log();
        }

        // Store the record
        self.violation_records.push_back(record);

        // Limit memory usage
        if self.violation_records.len() > self.config.max_violations_tracked {
            self.violation_records.pop_front();
        }

        self.stats.total_violations += 1;

        // Apply enforcement mode
        match self.config.enforcement {
            EnforcementMode::Panic => {
                panic!("Waker deduplication violation detected: {violation}")
            }
            EnforcementMode::Warn => {
                crate::tracing_compat::warn!(
                    violation = %violation,
                    "waker deduplication violation"
                );
            }
            EnforcementMode::Collect => {} // Just collect, no immediate action
        }
    }

    /// Check for registration races
    fn check_registration_races(&mut self, waker_id: WakerId, registration_time: SystemTime) {
        let window = std::time::Duration::from_millis(self.config.race_detection_window_ms);
        let mut violations_to_record = Vec::new();

        // Check if this waker was recently woken
        for (recent_waker_id, recent_wakeup_time) in &self.recent_wakeups {
            if *recent_waker_id == waker_id {
                if let Ok(time_diff) = registration_time.duration_since(*recent_wakeup_time) {
                    if time_diff <= window {
                        if let Some(state) = self.wakers.get(&waker_id) {
                            let violation = WakerDedupViolation::RegistrationRace {
                                waker_id,
                                channel_id: state.channel_id,
                                registration_time,
                                wakeup_time: *recent_wakeup_time,
                                trace_id: state.trace_id,
                            };
                            violations_to_record.push(violation);
                        }
                    }
                }
            }
        }

        // Record violations
        for violation in violations_to_record {
            self.record_violation(violation);
            self.stats.total_registration_races += 1;
        }
    }

    /// Check for leaked wakers
    fn check_for_leaked_wakers(&mut self) {
        let now = SystemTime::now();
        let leak_threshold = std::time::Duration::from_secs(60); // 1 minute
        let mut leaked_wakers = Vec::new();
        let mut violations_to_record = Vec::new();

        for (waker_id, state) in &self.wakers {
            if matches!(state.status, WakerStatus::Queued) {
                if let Ok(age) = now.duration_since(state.registered_at) {
                    if age > leak_threshold {
                        leaked_wakers.push(*waker_id);
                        let violation = WakerDedupViolation::WakerLeak {
                            waker_id: *waker_id,
                            channel_id: state.channel_id,
                            registered_at: state.registered_at,
                            detected_at: now,
                            trace_id: state.trace_id,
                        };
                        violations_to_record.push(violation);
                    }
                }
            }
        }

        // Record violations
        for violation in violations_to_record {
            self.record_violation(violation);
            self.stats.total_leaks += 1;
        }

        // Mark leaked wakers as dropped
        for waker_id in leaked_wakers {
            if let Some(state) = self.wakers.get_mut(&waker_id) {
                state.status = WakerStatus::Dropped { at: now };
                state.last_operation_at = now;
                self.stats.active_wakers = self.stats.active_wakers.saturating_sub(1);
            }
        }
    }

    /// Check for lost wakeups
    fn check_for_lost_wakeups(&mut self) {
        let now = SystemTime::now();
        let lost_threshold = std::time::Duration::from_secs(30); // 30 seconds
        let mut violations_to_record = Vec::new();

        for (waker_id, state) in &self.wakers {
            if matches!(state.status, WakerStatus::Queued) {
                if let Ok(age) = now.duration_since(state.registered_at) {
                    if age > lost_threshold {
                        let violation = WakerDedupViolation::LostWakeup {
                            waker_id: *waker_id,
                            channel_id: state.channel_id,
                            registered_at: state.registered_at,
                            expected_wake_at: state.registered_at + lost_threshold,
                            trace_id: state.trace_id,
                        };
                        violations_to_record.push(violation);
                    }
                }
            }
        }

        // Record violations
        for violation in violations_to_record {
            self.record_violation(violation);
            self.stats.total_lost_wakeups += 1;
        }
    }

    /// Clean up tracking data to prevent memory leaks
    fn cleanup_tracking_data(&mut self) {
        let now = SystemTime::now();
        let cleanup_window = std::time::Duration::from_secs(300); // 5 minutes

        // Clean up old registration events
        while let Some((_, time)) = self.recent_registrations.front() {
            if now.duration_since(*time).unwrap_or_default() > cleanup_window {
                self.recent_registrations.pop_front();
            } else {
                break;
            }
        }

        // Clean up old wakeup events
        while let Some((_, time)) = self.recent_wakeups.front() {
            if now.duration_since(*time).unwrap_or_default() > cleanup_window {
                self.recent_wakeups.pop_front();
            } else {
                break;
            }
        }

        // Clean up old dropped wakers
        let dropped_cleanup_window = std::time::Duration::from_secs(600); // 10 minutes
        self.wakers.retain(|_, state| {
            if let WakerStatus::Dropped { at } = state.status {
                now.duration_since(at).unwrap_or_default() <= dropped_cleanup_window
            } else {
                true
            }
        });

        // Limit total tracked wakers
        if self.wakers.len() > self.config.max_tracked_wakers {
            // Remove oldest dropped wakers first
            let mut to_remove = Vec::new();
            for (waker_id, state) in &self.wakers {
                if matches!(state.status, WakerStatus::Dropped { .. }) {
                    to_remove.push(*waker_id);
                }
            }
            // Sort by last operation time and remove oldest
            to_remove.sort_by_key(|id| self.wakers[id].last_operation_at);
            let remove_count =
                (self.wakers.len() - self.config.max_tracked_wakers).min(to_remove.len());
            for waker_id in &to_remove[..remove_count] {
                if let Some(state) = self.wakers.remove(waker_id) {
                    if let Some(channel_set) = self.channel_wakers.get_mut(&state.channel_id) {
                        channel_set.remove(waker_id);
                    }
                }
            }
        }
    }
}

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

    #[test]
    fn test_normal_waker_lifecycle() {
        let mut oracle = WakerDedupOracle::with_defaults();

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        // Register waker
        oracle.on_waker_registered(waker_id, channel_id, true, None);

        // Request wake
        oracle.on_waker_wake_requested(waker_id, "channel_send".to_string(), None);

        // Actually wake
        oracle.on_waker_actually_woken(waker_id, None);

        // Drop waker
        oracle.on_waker_dropped(waker_id);

        // Should have no violations
        let violations = oracle.check_for_violations().unwrap();
        assert!(violations.is_empty());

        let stats = oracle.statistics();
        assert_eq!(stats.total_wakers_registered, 1);
        assert_eq!(stats.total_wakers_woken, 1);
        assert_eq!(stats.total_wakers_dropped, 1);
        assert_eq!(stats.total_violations, 0);
    }

    #[test]
    fn test_double_wakeup_detection() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            ..Default::default()
        });

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        oracle.on_waker_registered(waker_id, channel_id, true, None);
        oracle.on_waker_actually_woken(waker_id, None);
        oracle.on_waker_actually_woken(waker_id, None); // This should be a violation

        let violations = oracle.check_for_violations().unwrap();
        assert_eq!(violations.len(), 1);
        assert!(matches!(
            violations[0],
            WakerDedupViolation::DoubleWakeup { .. }
        ));
    }

    #[test]
    fn test_spurious_wakeup_detection() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            ..Default::default()
        });

        let waker_id = WakerId(1);

        // Wakeup without registration
        oracle.on_waker_actually_woken(waker_id, None);

        let violations = oracle.check_for_violations().unwrap();
        assert_eq!(violations.len(), 1);
        assert!(matches!(
            violations[0],
            WakerDedupViolation::SpuriousWakeup { .. }
        ));
    }

    #[test]
    fn test_use_after_drop_detection() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            ..Default::default()
        });

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        oracle.on_waker_registered(waker_id, channel_id, true, None);
        oracle.on_waker_dropped(waker_id);
        oracle.on_waker_actually_woken(waker_id, None); // Use after drop

        let violations = oracle.check_for_violations().unwrap();
        assert_eq!(violations.len(), 1);
        assert!(matches!(
            violations[0],
            WakerDedupViolation::UseAfterDrop { .. }
        ));
    }

    #[test]
    fn test_queued_state_verification() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            ..Default::default()
        });

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        oracle.on_waker_registered(waker_id, channel_id, true, None);
        oracle.verify_queued_state(waker_id, false, None); // Inconsistent state

        let violations = oracle.check_for_violations().unwrap();
        assert_eq!(violations.len(), 1);
        assert!(matches!(
            violations[0],
            WakerDedupViolation::InconsistentQueuedState { .. }
        ));
    }

    #[test]
    fn test_registration_race_detection() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            race_detection_window_ms: 100, // 100ms window
            ..Default::default()
        });

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        oracle.on_waker_registered(waker_id, channel_id, true, None);

        // Simulate recent wakeup
        oracle
            .recent_wakeups
            .push_back((waker_id, SystemTime::now()));

        // Register again soon after wakeup (simulating race)
        oracle.on_waker_registered(waker_id, channel_id, true, None);

        let _violations = oracle.check_for_violations().unwrap();
        // May or may not detect race depending on timing
    }

    #[test]
    fn test_waker_leak_detection() {
        let mut oracle = WakerDedupOracle::new(WakerDedupConfig {
            enforcement: EnforcementMode::Collect,
            ..Default::default()
        });

        let waker_id = WakerId(1);
        let channel_id = ChannelId(1);

        // Register waker with old timestamp
        oracle.on_waker_registered(waker_id, channel_id, true, None);

        // Manually set old registration time to trigger leak detection
        if let Some(state) = oracle.wakers.get_mut(&waker_id) {
            state.registered_at = SystemTime::now() - Duration::from_secs(120); // 2 minutes ago
        }

        let violations = oracle.check_for_violations().unwrap();
        assert_eq!(violations.len(), 1);
        assert!(matches!(
            violations[0],
            WakerDedupViolation::WakerLeak { .. }
        ));
    }

    #[test]
    fn test_violation_record_creation() {
        let config = WakerDedupConfig {
            include_stack_traces: true,
            enable_replay_commands: true,
            structured_logging: false,
            ..Default::default()
        };

        let violation = WakerDedupViolation::LostWakeup {
            waker_id: WakerId(1),
            channel_id: ChannelId(1),
            registered_at: SystemTime::now(),
            expected_wake_at: SystemTime::now(),
            trace_id: Some(TraceId::new_for_test(1)),
        };

        let record = ViolationRecord::new(violation, &config);

        assert!(record.trace_id.is_some());
        assert!(record.stack_trace.is_some());
        assert!(record.replay_command.is_some());
    }

    #[test]
    fn test_oracle_reset() {
        let mut oracle = WakerDedupOracle::with_defaults();

        // Add some state
        oracle.on_waker_registered(WakerId(1), ChannelId(1), true, None);
        oracle.on_waker_actually_woken(WakerId(999), None); // Spurious wakeup

        // Verify state exists
        assert!(!oracle.wakers.is_empty());
        assert!(!oracle.violations.is_empty());

        // Reset and verify clean state
        oracle.reset();

        assert!(oracle.wakers.is_empty());
        assert!(oracle.violations.is_empty());
        assert_eq!(oracle.statistics().total_wakers_registered, 0);
    }
}