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
//! Symbol obligation integration with the core obligation tracking system.
//!
//! Bridges the RaptorQ symbol layer with the runtime's existing two-phase
//! obligation protocol ([`ObligationRecord`]). Provides epoch-aware validity
//! windows, deadline-based expiry, and RAII guards for automatic resolution.

use std::collections::HashMap;

use crate::record::obligation::{
    ObligationAbortReason, ObligationKind, ObligationRecord, ObligationState,
};
use crate::types::symbol::{ObjectId, SymbolId};
use crate::types::{ObligationId, RegionId, TaskId, Time};

// ============================================================================
// EpochId and EpochWindow
// ============================================================================

/// Identifier for an epoch in the distributed system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EpochId(pub u64);

/// Window of epochs during which an obligation is valid.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EpochWindow {
    /// Starting epoch (inclusive).
    pub start: EpochId,
    /// Ending epoch (inclusive).
    pub end: EpochId,
}

// ============================================================================
// SymbolObligationKind
// ============================================================================

/// Extended obligation kinds for symbol operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolObligationKind {
    /// Obligation to transmit a symbol to a destination.
    /// Committed when acknowledged, aborted on timeout/failure.
    SymbolTransmit {
        /// The symbol being transmitted.
        symbol_id: SymbolId,
        /// Destination region.
        destination: RegionId,
    },

    /// Obligation to acknowledge receipt of a symbol.
    /// Must be committed before region close.
    SymbolAck {
        /// The symbol being acknowledged.
        symbol_id: SymbolId,
        /// Source region.
        source: RegionId,
    },

    /// Obligation representing a decoding operation in progress.
    /// Committed when object is fully decoded.
    DecodingInProgress {
        /// Object being decoded.
        object_id: ObjectId,
        /// Symbols received so far.
        symbols_received: u32,
        /// Total symbols needed.
        symbols_needed: u32,
    },

    /// Obligation for holding an encoding session open.
    /// Must be resolved before session resources are released.
    EncodingSession {
        /// Object being encoded.
        object_id: ObjectId,
        /// Symbols encoded so far.
        symbols_encoded: u32,
    },

    /// Lease obligation for remote resource access.
    /// Must be renewed or released before expiry.
    SymbolLease {
        /// The leased object.
        object_id: ObjectId,
        /// When the lease expires.
        lease_expires: Time,
    },
}

/// Error returned when updating decoding progress.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecodingProgressUpdateError {
    /// Progress updates are only valid for decoding obligations.
    NotDecodingObligation,
    /// Reported progress exceeds the required symbol count.
    SymbolsReceivedExceedsNeeded {
        /// The number of symbols received so far.
        received: u32,
        /// The total number of symbols needed to complete decoding.
        needed: u32,
    },
    /// Reported progress moved backwards from the previously observed count.
    SymbolsReceivedRegressed {
        /// The previously recorded number of symbols received.
        previous: u32,
        /// The newly reported number of symbols received.
        attempted: u32,
    },
}

impl std::fmt::Display for DecodingProgressUpdateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotDecodingObligation => {
                write!(
                    f,
                    "decoding progress can only be updated for decoding obligations"
                )
            }
            Self::SymbolsReceivedExceedsNeeded { received, needed } => write!(
                f,
                "symbols_received ({received}) exceeds symbols_needed ({needed})"
            ),
            Self::SymbolsReceivedRegressed {
                previous,
                attempted,
            } => write!(
                f,
                "symbols_received regressed from {previous} to {attempted}"
            ),
        }
    }
}

impl std::error::Error for DecodingProgressUpdateError {}

// ============================================================================
// SymbolObligation
// ============================================================================

/// A symbol obligation that wraps the core [`ObligationRecord`] with
/// symbol-specific metadata.
///
/// Bridges between the distributed symbol layer and the runtime's existing
/// two-phase obligation protocol.
#[derive(Debug)]
pub struct SymbolObligation {
    /// The underlying obligation record.
    inner: ObligationRecord,
    /// Symbol-specific obligation details.
    kind: SymbolObligationKind,
    /// The epoch window during which this obligation is valid.
    /// None means valid for any epoch (local-only obligation).
    valid_epoch: Option<EpochWindow>,
    /// Optional deadline for automatic abort if not resolved.
    deadline: Option<Time>,
}

impl SymbolObligation {
    /// Creates a new symbol transmit obligation.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn transmit(
        id: ObligationId,
        holder: TaskId,
        region: RegionId,
        symbol_id: SymbolId,
        destination: RegionId,
        deadline: Option<Time>,
        epoch_window: Option<EpochWindow>,
        now: Time,
    ) -> Self {
        Self {
            inner: ObligationRecord::new(id, ObligationKind::IoOp, holder, region, now),
            kind: SymbolObligationKind::SymbolTransmit {
                symbol_id,
                destination,
            },
            valid_epoch: epoch_window,
            deadline,
        }
    }

    /// Creates a new symbol acknowledgment obligation.
    #[must_use]
    pub fn ack(
        id: ObligationId,
        holder: TaskId,
        region: RegionId,
        symbol_id: SymbolId,
        source: RegionId,
        now: Time,
    ) -> Self {
        Self {
            inner: ObligationRecord::new(id, ObligationKind::Ack, holder, region, now),
            kind: SymbolObligationKind::SymbolAck { symbol_id, source },
            valid_epoch: None,
            deadline: None,
        }
    }

    /// Creates a decoding progress obligation.
    #[must_use]
    pub fn decoding(
        id: ObligationId,
        holder: TaskId,
        region: RegionId,
        object_id: ObjectId,
        symbols_needed: u32,
        epoch_window: EpochWindow,
        now: Time,
    ) -> Self {
        Self {
            inner: ObligationRecord::new(id, ObligationKind::IoOp, holder, region, now),
            kind: SymbolObligationKind::DecodingInProgress {
                object_id,
                symbols_received: 0,
                symbols_needed,
            },
            valid_epoch: Some(epoch_window),
            deadline: None,
        }
    }

    /// Creates a lease obligation.
    #[must_use]
    pub fn lease(
        id: ObligationId,
        holder: TaskId,
        region: RegionId,
        object_id: ObjectId,
        lease_expires: Time,
        now: Time,
    ) -> Self {
        Self {
            inner: ObligationRecord::new(id, ObligationKind::Lease, holder, region, now),
            kind: SymbolObligationKind::SymbolLease {
                object_id,
                lease_expires,
            },
            valid_epoch: None,
            deadline: Some(lease_expires),
        }
    }

    /// Returns true if this obligation is pending (not resolved).
    #[must_use]
    #[inline]
    pub fn is_pending(&self) -> bool {
        self.inner.is_pending()
    }

    /// Returns true if this obligation is within its valid epoch window.
    #[must_use]
    pub fn is_epoch_valid(&self, current_epoch: EpochId) -> bool {
        self.valid_epoch
            .is_none_or(|window| current_epoch >= window.start && current_epoch <= window.end)
    }

    /// Returns true if this obligation has passed its deadline.
    #[must_use]
    pub fn is_expired(&self, now: Time) -> bool {
        self.deadline.is_some_and(|deadline| now > deadline)
    }

    /// Commits the obligation (successful resolution).
    ///
    /// # Panics
    /// Panics if already resolved.
    pub fn commit(&mut self, now: Time) {
        self.inner.commit(now);
    }

    /// Aborts the obligation (clean cancellation).
    ///
    /// # Panics
    /// Panics if already resolved.
    pub fn abort(&mut self, now: Time) {
        self.inner.abort(now, ObligationAbortReason::Explicit);
    }

    /// Marks the obligation as leaked.
    ///
    /// Called by the runtime when it detects that an obligation holder
    /// completed without resolving the obligation.
    ///
    /// # Panics
    /// Panics if already resolved.
    pub fn mark_leaked(&mut self, now: Time) {
        self.inner.mark_leaked(now);
    }

    /// Updates decoding progress.
    ///
    /// Returns an error when called for a non-decoding obligation or when the
    /// provided count exceeds the decode target or moves backwards.
    pub fn update_decoding_progress(
        &mut self,
        symbols_received: u32,
    ) -> Result<(), DecodingProgressUpdateError> {
        if let SymbolObligationKind::DecodingInProgress {
            symbols_received: ref mut count,
            symbols_needed,
            ..
        } = self.kind
        {
            if symbols_received > symbols_needed {
                return Err(DecodingProgressUpdateError::SymbolsReceivedExceedsNeeded {
                    received: symbols_received,
                    needed: symbols_needed,
                });
            }
            if symbols_received < *count {
                return Err(DecodingProgressUpdateError::SymbolsReceivedRegressed {
                    previous: *count,
                    attempted: symbols_received,
                });
            }
            *count = symbols_received;
            Ok(())
        } else {
            Err(DecodingProgressUpdateError::NotDecodingObligation)
        }
    }

    /// Returns the symbol-specific obligation kind.
    #[must_use]
    #[inline]
    pub fn symbol_kind(&self) -> &SymbolObligationKind {
        &self.kind
    }

    /// Returns the underlying obligation state.
    #[must_use]
    #[inline]
    pub fn state(&self) -> ObligationState {
        self.inner.state
    }

    /// Returns the obligation ID.
    #[must_use]
    #[inline]
    pub fn id(&self) -> ObligationId {
        self.inner.id
    }
}

// ============================================================================
// SymbolObligationTracker
// ============================================================================

/// Tracker for managing symbolic obligations within a region.
///
/// Maintains indices by symbol ID and object ID for fast lookup.
/// Supports epoch-based and deadline-based expiry.
#[derive(Debug)]
pub struct SymbolObligationTracker {
    /// Pending obligations indexed by ID.
    obligations: HashMap<ObligationId, SymbolObligation>,
    /// Index by symbol ID for fast lookup.
    by_symbol: HashMap<SymbolId, Vec<ObligationId>>,
    /// Index by object ID for decoding/encoding obligations.
    by_object: HashMap<ObjectId, Vec<ObligationId>>,
    /// The region this tracker belongs to.
    region_id: RegionId,
}

impl SymbolObligationTracker {
    fn assert_registration_valid(&self, obligation: &SymbolObligation) {
        assert_eq!(
            obligation.inner.region, self.region_id,
            "symbol obligation tracker region mismatch"
        );
        assert!(
            !self.obligations.contains_key(&obligation.id()),
            "duplicate symbol obligation id registered"
        );
    }

    fn index_obligation_id(&mut self, id: ObligationId, kind: &SymbolObligationKind) {
        match kind {
            SymbolObligationKind::SymbolTransmit { symbol_id, .. }
            | SymbolObligationKind::SymbolAck { symbol_id, .. } => {
                self.by_symbol
                    .entry(*symbol_id)
                    .or_insert_with(|| Vec::with_capacity(2))
                    .push(id);
            }
            SymbolObligationKind::DecodingInProgress { object_id, .. }
            | SymbolObligationKind::EncodingSession { object_id, .. }
            | SymbolObligationKind::SymbolLease { object_id, .. } => {
                self.by_object
                    .entry(*object_id)
                    .or_insert_with(|| Vec::with_capacity(2))
                    .push(id);
            }
        }
    }

    fn remove_indexed_obligation_id(&mut self, id: ObligationId, kind: &SymbolObligationKind) {
        match kind {
            SymbolObligationKind::SymbolTransmit { symbol_id, .. }
            | SymbolObligationKind::SymbolAck { symbol_id, .. } => {
                if let Some(ids) = self.by_symbol.get_mut(symbol_id) {
                    ids.retain(|i| *i != id);
                    if ids.is_empty() {
                        self.by_symbol.remove(symbol_id);
                    }
                }
            }
            SymbolObligationKind::DecodingInProgress { object_id, .. }
            | SymbolObligationKind::EncodingSession { object_id, .. }
            | SymbolObligationKind::SymbolLease { object_id, .. } => {
                if let Some(ids) = self.by_object.get_mut(object_id) {
                    ids.retain(|i| *i != id);
                    if ids.is_empty() {
                        self.by_object.remove(object_id);
                    }
                }
            }
        }
    }

    fn remove_obligation(&mut self, id: ObligationId) -> Option<SymbolObligation> {
        let obligation = self.obligations.remove(&id)?;
        self.remove_indexed_obligation_id(id, &obligation.kind);
        Some(obligation)
    }

    /// Creates a new tracker for the given region.
    #[must_use]
    pub fn new(region_id: RegionId) -> Self {
        Self {
            obligations: HashMap::with_capacity(16),
            by_symbol: HashMap::with_capacity(16),
            by_object: HashMap::with_capacity(16),
            region_id,
        }
    }

    /// Returns the region ID for this tracker.
    #[must_use]
    pub fn region_id(&self) -> RegionId {
        self.region_id
    }

    /// Registers a new symbolic obligation.
    ///
    /// # Panics
    /// Panics if the obligation belongs to a different region or reuses an
    /// already-pending obligation ID.
    pub fn register(&mut self, obligation: SymbolObligation) -> ObligationId {
        self.assert_registration_valid(&obligation);
        let id = obligation.id();
        self.index_obligation_id(id, &obligation.kind);
        self.obligations.insert(id, obligation);
        id
    }

    /// Resolves an obligation by ID.
    ///
    /// If `commit` is true, commits the obligation; otherwise aborts it.
    pub fn resolve(
        &mut self,
        id: ObligationId,
        commit: bool,
        now: Time,
    ) -> Option<SymbolObligation> {
        self.remove_obligation(id).map(|mut ob| {
            if ob.is_pending() {
                if commit {
                    ob.commit(now);
                } else {
                    ob.abort(now);
                }
            }
            ob
        })
    }

    /// Returns an iterator over all pending obligations.
    pub fn pending(&self) -> impl Iterator<Item = &SymbolObligation> {
        self.obligations.values().filter(|o| o.is_pending())
    }

    /// Returns obligations for a specific symbol.
    #[must_use]
    pub fn by_symbol(&self, symbol_id: SymbolId) -> Vec<&SymbolObligation> {
        self.by_symbol
            .get(&symbol_id)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.obligations.get(id).filter(|ob| ob.is_pending()))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Returns the count of pending obligations.
    #[must_use]
    pub fn pending_count(&self) -> usize {
        self.obligations.values().filter(|o| o.is_pending()).count()
    }

    /// Checks for leaked obligations and marks them.
    /// Called during region close.
    pub fn check_leaks(&mut self, now: Time) -> Vec<ObligationId> {
        let leaked: Vec<ObligationId> = self
            .obligations
            .iter()
            .filter_map(|(id, ob)| ob.is_pending().then_some(*id))
            .collect();

        for id in &leaked {
            if let Some(mut ob) = self.remove_obligation(*id) {
                ob.mark_leaked(now);
            }
        }
        leaked
    }

    /// Aborts all pending obligations outside the given epoch window.
    pub fn abort_expired_epoch(&mut self, current_epoch: EpochId, now: Time) -> Vec<ObligationId> {
        let aborted: Vec<ObligationId> = self
            .obligations
            .iter()
            .filter_map(|(id, ob)| {
                (ob.is_pending() && !ob.is_epoch_valid(current_epoch)).then_some(*id)
            })
            .collect();

        for id in &aborted {
            if let Some(mut ob) = self.remove_obligation(*id) {
                ob.abort(now);
            }
        }
        aborted
    }

    /// Aborts all pending obligations that have passed their deadline.
    pub fn abort_expired_deadlines(&mut self, now: Time) -> Vec<ObligationId> {
        let aborted: Vec<ObligationId> = self
            .obligations
            .iter()
            .filter_map(|(id, ob)| (ob.is_pending() && ob.is_expired(now)).then_some(*id))
            .collect();

        for id in &aborted {
            if let Some(mut ob) = self.remove_obligation(*id) {
                ob.abort(now);
            }
        }
        aborted
    }
}

// ============================================================================
// ObligationGuard
// ============================================================================

/// Guard that aborts an obligation on drop if not explicitly resolved.
///
/// Provides RAII-style automatic resolution. If the guard is dropped without
/// calling `commit()` or `abort()`, the obligation is aborted.
pub struct ObligationGuard<'a> {
    /// The tracker holding the obligation.
    tracker: &'a mut SymbolObligationTracker,
    /// The obligation ID.
    id: ObligationId,
    /// Whether the obligation has been explicitly resolved.
    resolved: bool,
}

impl<'a> ObligationGuard<'a> {
    /// Creates a new guard for the given obligation.
    pub fn new(tracker: &'a mut SymbolObligationTracker, id: ObligationId) -> Self {
        Self {
            tracker,
            id,
            resolved: false,
        }
    }

    /// Commits the obligation and marks the guard as resolved.
    pub fn commit(mut self, now: Time) {
        self.tracker.resolve(self.id, true, now);
        self.resolved = true;
    }

    /// Aborts the obligation and marks the guard as resolved.
    pub fn abort(mut self, now: Time) {
        self.tracker.resolve(self.id, false, now);
        self.resolved = true;
    }
}

impl Drop for ObligationGuard<'_> {
    fn drop(&mut self) {
        if !self.resolved {
            // Best-effort abort with zero time (runtime can set proper time)
            self.tracker.resolve(self.id, false, Time::ZERO);
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    fn test_ids() -> (ObligationId, TaskId, RegionId) {
        (
            ObligationId::from_arena(ArenaIndex::new(0, 0)),
            TaskId::from_arena(ArenaIndex::new(0, 0)),
            RegionId::from_arena(ArenaIndex::new(0, 0)),
        )
    }

    // Test 1: Basic obligation creation and commit
    #[test]
    fn test_transmit_obligation_lifecycle_commit() {
        let (oid, tid, rid) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let mut ob =
            SymbolObligation::transmit(oid, tid, rid, symbol_id, dest, None, None, Time::ZERO);

        assert!(ob.is_pending());
        ob.commit(Time::from_millis(100));
        assert!(!ob.is_pending());
        assert_eq!(ob.state(), ObligationState::Committed);
    }

    // Test 2: Basic obligation abort
    #[test]
    fn test_transmit_obligation_lifecycle_abort() {
        let (oid, tid, rid) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let mut ob =
            SymbolObligation::transmit(oid, tid, rid, symbol_id, dest, None, None, Time::ZERO);

        ob.abort(Time::from_millis(100));
        assert_eq!(ob.state(), ObligationState::Aborted);
    }

    // Test 3: Epoch validity checking
    #[test]
    fn test_epoch_window_validity() {
        let (oid, tid, rid) = test_ids();
        let object_id = ObjectId::new_for_test(1);
        let window = EpochWindow {
            start: EpochId(10),
            end: EpochId(20),
        };

        let ob = SymbolObligation::decoding(oid, tid, rid, object_id, 10, window, Time::ZERO);

        assert!(!ob.is_epoch_valid(EpochId(5))); // Before window
        assert!(ob.is_epoch_valid(EpochId(10))); // Start of window
        assert!(ob.is_epoch_valid(EpochId(15))); // Middle of window
        assert!(ob.is_epoch_valid(EpochId(20))); // End of window
        assert!(!ob.is_epoch_valid(EpochId(25))); // After window
    }

    // Test 4: Deadline expiry detection
    #[test]
    fn test_deadline_expiry() {
        let (oid, tid, rid) = test_ids();
        let object_id = ObjectId::new_for_test(1);
        let deadline = Time::from_millis(1000);

        let ob = SymbolObligation::lease(oid, tid, rid, object_id, deadline, Time::ZERO);

        assert!(!ob.is_expired(Time::from_millis(500)));
        assert!(!ob.is_expired(Time::from_millis(1000)));
        assert!(ob.is_expired(Time::from_millis(1001)));
    }

    // Test 5: Tracker registration and lookup
    #[test]
    fn test_tracker_registration() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid, tid, _) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let ob = SymbolObligation::transmit(oid, tid, rid, symbol_id, dest, None, None, Time::ZERO);

        let id = tracker.register(ob);
        assert_eq!(tracker.pending_count(), 1);

        let found = tracker.by_symbol(symbol_id);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].id(), id);
    }

    // Regression: duplicate registration must fail closed and preserve the
    // original pending obligation instead of silently discarding it.
    #[test]
    fn test_register_same_id_panics_and_preserves_original_obligation() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid, tid, _) = test_ids();
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));
        let first_symbol = SymbolId::new_for_test(11, 0, 0);
        let second_symbol = SymbolId::new_for_test(12, 0, 0);

        let first =
            SymbolObligation::transmit(oid, tid, rid, first_symbol, dest, None, None, Time::ZERO);
        tracker.register(first);
        assert_eq!(tracker.by_symbol(first_symbol).len(), 1);

        let second = SymbolObligation::transmit(
            oid,
            tid,
            rid,
            second_symbol,
            dest,
            None,
            None,
            Time::from_nanos(1),
        );

        let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            tracker.register(second);
        }));
        assert!(panic.is_err());

        let original = tracker.by_symbol(first_symbol);
        assert_eq!(original.len(), 1);
        assert_eq!(original[0].id(), oid);
        assert!(tracker.by_symbol(second_symbol).is_empty());
        assert_eq!(tracker.pending_count(), 1);
    }

    // Regression: the tracker must reject obligations that belong to another
    // region instead of silently tracking them under the wrong owner.
    #[test]
    fn test_register_cross_region_obligation_panics() {
        let tracker_region = RegionId::from_arena(ArenaIndex::new(0, 0));
        let other_region = RegionId::from_arena(ArenaIndex::new(9, 0));
        let mut tracker = SymbolObligationTracker::new(tracker_region);

        let (oid, tid, _) = test_ids();
        let symbol_id = SymbolId::new_for_test(77, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));
        let obligation = SymbolObligation::transmit(
            oid,
            tid,
            other_region,
            symbol_id,
            dest,
            None,
            None,
            Time::ZERO,
        );

        let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            tracker.register(obligation);
        }));
        assert!(panic.is_err());
        assert_eq!(tracker.pending_count(), 0);
        assert!(tracker.by_symbol(symbol_id).is_empty());
    }

    // Test 6: Tracker resolution (commit)
    #[test]
    fn test_tracker_resolve_commit() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid, tid, _) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let ob = SymbolObligation::transmit(oid, tid, rid, symbol_id, dest, None, None, Time::ZERO);

        let id = tracker.register(ob);
        let resolved = tracker.resolve(id, true, Time::from_millis(100));

        assert!(resolved.is_some());
        assert_eq!(resolved.unwrap().state(), ObligationState::Committed);
        assert_eq!(tracker.pending_count(), 0);
    }

    // Test 7: Leak detection during region close
    #[test]
    fn test_leak_detection() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid1, tid, _) = test_ids();
        let oid2 = ObligationId::from_arena(ArenaIndex::new(1, 0));
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let ob1 =
            SymbolObligation::transmit(oid1, tid, rid, symbol_id, dest, None, None, Time::ZERO);
        let ob2 = SymbolObligation::ack(oid2, tid, rid, symbol_id, dest, Time::ZERO);

        tracker.register(ob1);
        let id2 = tracker.register(ob2);

        // Resolve one, leave the other
        tracker.resolve(id2, true, Time::from_millis(100));

        let leaked = tracker.check_leaks(Time::from_millis(200));
        assert_eq!(leaked.len(), 1);
        assert_eq!(tracker.pending_count(), 0);
        assert!(tracker.by_symbol(symbol_id).is_empty());
    }

    // Test 8: Epoch-based abort
    #[test]
    fn test_abort_expired_epoch() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid, tid, _) = test_ids();
        let object_id = ObjectId::new_for_test(1);
        let window = EpochWindow {
            start: EpochId(10),
            end: EpochId(20),
        };

        let ob = SymbolObligation::decoding(oid, tid, rid, object_id, 10, window, Time::ZERO);
        tracker.register(ob);

        // Epoch 15 is valid, nothing aborted
        let aborted = tracker.abort_expired_epoch(EpochId(15), Time::from_millis(100));
        assert_eq!(aborted.len(), 0);

        // Epoch 25 is past window, obligation aborted
        let aborted = tracker.abort_expired_epoch(EpochId(25), Time::from_millis(200));
        assert_eq!(aborted.len(), 1);
        assert_eq!(tracker.pending_count(), 0);
        assert!(tracker.obligations.is_empty());
        assert!(tracker.by_object.is_empty());
    }

    // Test 9: Deadline-based abort
    #[test]
    fn test_abort_expired_deadlines() {
        let rid = RegionId::from_arena(ArenaIndex::new(0, 0));
        let mut tracker = SymbolObligationTracker::new(rid);

        let (oid, tid, _) = test_ids();
        let object_id = ObjectId::new_for_test(1);
        let deadline = Time::from_millis(1000);

        let ob = SymbolObligation::lease(oid, tid, rid, object_id, deadline, Time::ZERO);
        tracker.register(ob);

        // Before deadline
        let aborted = tracker.abort_expired_deadlines(Time::from_millis(500));
        assert_eq!(aborted.len(), 0);

        // After deadline
        let aborted = tracker.abort_expired_deadlines(Time::from_millis(1500));
        assert_eq!(aborted.len(), 1);
        assert_eq!(tracker.pending_count(), 0);
        assert!(tracker.obligations.is_empty());
        assert!(tracker.by_object.is_empty());
    }

    // Test 10: Decoding progress updates
    #[test]
    fn test_decoding_progress_update() {
        let (oid, tid, rid) = test_ids();
        let object_id = ObjectId::new_for_test(1);
        let window = EpochWindow {
            start: EpochId(1),
            end: EpochId(100),
        };

        let mut ob = SymbolObligation::decoding(oid, tid, rid, object_id, 10, window, Time::ZERO);

        // Initial state
        if let SymbolObligationKind::DecodingInProgress {
            symbols_received, ..
        } = ob.symbol_kind()
        {
            assert_eq!(*symbols_received, 0);
        }

        // Update progress
        assert!(ob.update_decoding_progress(5).is_ok());

        if let SymbolObligationKind::DecodingInProgress {
            symbols_received, ..
        } = ob.symbol_kind()
        {
            assert_eq!(*symbols_received, 5);
        }
    }

    // Test 11b: Updating progress on a non-decoding obligation returns error.
    #[test]
    fn test_decoding_progress_update_rejects_non_decoding_obligation() {
        let (oid, tid, rid) = test_ids();
        let symbol_id = SymbolId::new_for_test(42, 0, 0);

        let mut ob = SymbolObligation::ack(oid, tid, rid, symbol_id, rid, Time::ZERO);

        let result = ob.update_decoding_progress(1);
        assert_eq!(
            result,
            Err(DecodingProgressUpdateError::NotDecodingObligation)
        );
    }

    // Test 11c: Updating progress beyond the decode target returns error.
    #[test]
    fn test_decoding_progress_update_rejects_received_above_needed() {
        let (oid, tid, rid) = test_ids();
        let object_id = ObjectId::new_for_test(7);
        let window = EpochWindow {
            start: EpochId(1),
            end: EpochId(2),
        };

        let mut ob = SymbolObligation::decoding(oid, tid, rid, object_id, 3, window, Time::ZERO);
        let result = ob.update_decoding_progress(4);
        assert_eq!(
            result,
            Err(DecodingProgressUpdateError::SymbolsReceivedExceedsNeeded {
                received: 4,
                needed: 3,
            })
        );

        if let SymbolObligationKind::DecodingInProgress {
            symbols_received, ..
        } = ob.symbol_kind()
        {
            assert_eq!(*symbols_received, 0);
        }
    }

    // Test 11d: Decoding progress must not move backwards.
    #[test]
    fn test_decoding_progress_update_rejects_regression() {
        let (oid, tid, rid) = test_ids();
        let object_id = ObjectId::new_for_test(8);
        let window = EpochWindow {
            start: EpochId(1),
            end: EpochId(2),
        };

        let mut ob = SymbolObligation::decoding(oid, tid, rid, object_id, 6, window, Time::ZERO);
        assert!(ob.update_decoding_progress(4).is_ok());

        let result = ob.update_decoding_progress(2);
        assert_eq!(
            result,
            Err(DecodingProgressUpdateError::SymbolsReceivedRegressed {
                previous: 4,
                attempted: 2,
            })
        );

        if let SymbolObligationKind::DecodingInProgress {
            symbols_received, ..
        } = ob.symbol_kind()
        {
            assert_eq!(*symbols_received, 4);
        }
    }

    // Test 11: Double resolution panics
    #[test]
    #[should_panic(expected = "obligation already resolved")]
    fn test_double_commit_panics() {
        let (oid, tid, rid) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);
        let dest = RegionId::from_arena(ArenaIndex::new(1, 0));

        let mut ob =
            SymbolObligation::transmit(oid, tid, rid, symbol_id, dest, None, None, Time::ZERO);

        ob.commit(Time::from_millis(100));
        ob.commit(Time::from_millis(200)); // Should panic
    }

    // Test 12: No epoch constraint means always valid
    #[test]
    fn test_no_epoch_constraint_always_valid() {
        let (oid, tid, rid) = test_ids();
        let symbol_id = SymbolId::new_for_test(1, 0, 0);

        let ob = SymbolObligation::ack(oid, tid, rid, symbol_id, rid, Time::ZERO);

        assert!(ob.is_epoch_valid(EpochId(0)));
        assert!(ob.is_epoch_valid(EpochId(u64::MAX)));
    }

    #[test]
    fn epoch_id_debug_clone_copy_eq_ord_hash() {
        use std::collections::HashSet;
        let a = EpochId(42);
        let b = a; // Copy
        let c = a;
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_ne!(a, EpochId(99));
        assert!(a < EpochId(100));
        let dbg = format!("{a:?}");
        assert!(dbg.contains("EpochId"));
        let mut set = HashSet::new();
        set.insert(a);
        assert!(set.contains(&b));
    }

    #[test]
    fn epoch_window_debug_clone_copy_eq() {
        let a = EpochWindow {
            start: EpochId(10),
            end: EpochId(20),
        };
        let b = a; // Copy
        let c = a;
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_ne!(
            a,
            EpochWindow {
                start: EpochId(0),
                end: EpochId(5)
            }
        );
        let dbg = format!("{a:?}");
        assert!(dbg.contains("EpochWindow"));
    }
}