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
//! Self-stabilizing recovery protocol for obligation convergence.
//!
//! After partial failures (node crashes, network partitions, lost messages),
//! the recovery protocol drives the system toward a quiescent, leak-free
//! state using the CRDT obligation ledger.
//!
//! # Design
//!
//! The protocol is self-stabilizing: starting from *any* state (including
//! corrupted or partially-merged states), repeated application of the
//! recovery rules eventually reaches a legitimate state where:
//!
//! 1. No obligations are stuck in `Reserved` beyond the timeout.
//! 2. All conflicts are resolved (via abort-wins or operator escalation).
//! 3. Linearity violations are flagged and the offending obligations aborted.
//!
//! # Recovery State Machine
//!
//! ```text
//! Idle ──(timer)──► Scanning ──► Resolving ──► Idle
//!                       │              │
//!                       └──(clean)─────┘
//! ```
//!
//! - **Idle**: Waiting for the next recovery tick.
//! - **Scanning**: Inspecting the CRDT ledger for anomalies.
//! - **Resolving**: Applying conflict resolution and timeout rules.
//!
//! # Conflict Resolution Rules
//!
//! 1. **Stale obligations**: `Reserved` obligations older than `stale_timeout`
//!    are forcibly aborted (the holder is assumed crashed).
//! 2. **Conflict (Committed ⊔ Aborted)**: Abort-wins policy — the obligation
//!    is driven to `Aborted` for safety (the committed side-effect may need
//!    compensation, but that's outside the obligation system's scope).
//! 3. **Linearity violations**: Obligations acquired or resolved multiple times
//!    are flagged as errors and forcibly aborted.
//!
//! # Convergence Guarantee
//!
//! The protocol converges because:
//! - Each recovery step can only advance obligations toward terminal states.
//! - Terminal states are absorbing in the lattice.
//! - The number of non-terminal obligations is finite and monotonically
//!   decreasing under recovery.
//! - No recovery action can create new obligations or resurrect resolved ones.

use crate::obligation::crdt::{CrdtObligationLedger, LinearityViolation};
use crate::trace::distributed::lattice::LatticeState;
use crate::types::ObligationId;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

/// Configuration for the recovery protocol.
#[derive(Debug, Clone)]
pub struct RecoveryConfig {
    /// How long a `Reserved` obligation can remain before being considered stale (nanoseconds).
    pub stale_timeout_ns: u64,
    /// Maximum number of obligations to resolve per recovery tick (prevents storms).
    pub max_resolutions_per_tick: usize,
    /// Whether to auto-resolve conflicts (abort-wins) or just flag them.
    pub auto_resolve_conflicts: bool,
    /// Whether to auto-abort linearity violations.
    pub auto_abort_violations: bool,
}

impl RecoveryConfig {
    /// Returns a default configuration suitable for testing.
    #[must_use]
    pub fn default_for_test() -> Self {
        Self {
            stale_timeout_ns: 5_000_000_000, // 5 seconds
            max_resolutions_per_tick: 100,
            auto_resolve_conflicts: true,
            auto_abort_violations: true,
        }
    }
}

impl Default for RecoveryConfig {
    fn default() -> Self {
        Self {
            stale_timeout_ns: 30_000_000_000, // 30 seconds
            max_resolutions_per_tick: 50,
            auto_resolve_conflicts: true,
            auto_abort_violations: true,
        }
    }
}

/// The current phase of the recovery state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryPhase {
    /// Waiting for the next recovery tick.
    Idle,
    /// Scanning the CRDT ledger for anomalies.
    Scanning,
    /// Applying resolution rules.
    Resolving,
}

impl fmt::Display for RecoveryPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Idle => write!(f, "Idle"),
            Self::Scanning => write!(f, "Scanning"),
            Self::Resolving => write!(f, "Resolving"),
        }
    }
}

/// An action taken by the recovery protocol.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryAction {
    /// A stale obligation was forcibly aborted.
    StaleAbort {
        /// Obligation that was aborted.
        id: ObligationId,
        /// Age in nanoseconds at the time of abort.
        age_ns: u64,
    },
    /// A conflict was resolved by aborting.
    ConflictResolved {
        /// Obligation that was resolved by abort.
        id: ObligationId,
    },
    /// A linearity violation was detected and the obligation aborted.
    ViolationAborted {
        /// Obligation that violated linearity.
        id: ObligationId,
        /// Total acquire count observed.
        total_acquires: u64,
        /// Total resolve count observed.
        total_resolves: u64,
    },
    /// An anomaly was flagged but not auto-resolved.
    Flagged {
        /// Obligation that was flagged.
        id: ObligationId,
        /// Human-readable reason for the flag.
        reason: String,
    },
}

impl fmt::Display for RecoveryAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StaleAbort { id, age_ns } => {
                write!(f, "stale-abort {id:?} (age={age_ns}ns)")
            }
            Self::ConflictResolved { id } => {
                write!(f, "conflict-resolved {id:?}")
            }
            Self::ViolationAborted {
                id,
                total_acquires,
                total_resolves,
            } => {
                write!(
                    f,
                    "violation-aborted {id:?} (acquires={total_acquires}, resolves={total_resolves})"
                )
            }
            Self::Flagged { id, reason } => {
                write!(f, "flagged {id:?}: {reason}")
            }
        }
    }
}

/// Result of a single recovery tick.
#[derive(Debug, Clone)]
pub struct RecoveryTickResult {
    /// Actions taken during this tick.
    pub actions: Vec<RecoveryAction>,
    /// Number of obligations still pending after this tick.
    pub remaining_pending: usize,
    /// Number of unresolved conflicts.
    pub remaining_conflicts: usize,
    /// Number of unresolved linearity violations.
    pub remaining_violations: usize,
    /// Whether the system is in a quiescent (clean) state.
    pub is_quiescent: bool,
}

impl RecoveryTickResult {
    /// Returns the number of actions taken.
    #[must_use]
    pub fn action_count(&self) -> usize {
        self.actions.len()
    }
}

/// Self-stabilizing recovery governor for the CRDT obligation ledger.
///
/// The governor inspects the ledger periodically (or on demand) and applies
/// resolution rules to drive the system toward quiescence.
#[derive(Debug)]
pub struct RecoveryGovernor {
    config: RecoveryConfig,
    phase: RecoveryPhase,
    /// Timestamps for when each obligation was first observed as Reserved.
    /// Used for stale detection. BTreeMap for determinism.
    first_seen_reserved: BTreeMap<ObligationId, u64>,
    /// Total ticks executed.
    total_ticks: u64,
    /// Total actions taken across all ticks.
    total_actions: u64,
}

impl RecoveryGovernor {
    /// Creates a new recovery governor with the given configuration.
    #[must_use]
    pub fn new(config: RecoveryConfig) -> Self {
        Self {
            config,
            phase: RecoveryPhase::Idle,
            first_seen_reserved: BTreeMap::new(),
            total_ticks: 0,
            total_actions: 0,
        }
    }

    /// Returns the current recovery phase.
    #[must_use]
    pub fn phase(&self) -> RecoveryPhase {
        self.phase
    }

    /// Returns the total number of recovery ticks executed.
    #[must_use]
    pub fn total_ticks(&self) -> u64 {
        self.total_ticks
    }

    /// Returns the total number of recovery actions taken.
    #[must_use]
    pub fn total_actions(&self) -> u64 {
        self.total_actions
    }

    /// Executes a single recovery tick against the given ledger.
    ///
    /// The `now_ns` parameter is the current time in nanoseconds (must be
    /// monotonically increasing for correct stale detection).
    ///
    /// Returns the actions taken and the resulting system state.
    pub fn tick(&mut self, ledger: &mut CrdtObligationLedger, now_ns: u64) -> RecoveryTickResult {
        self.total_ticks += 1;
        self.phase = RecoveryPhase::Scanning;

        let mut actions = Vec::new();
        let mut budget = self.config.max_resolutions_per_tick;

        // Phase 1: Scan for stale obligations
        self.update_first_seen(ledger, now_ns);

        // Phase 2: Resolve anomalies
        self.phase = RecoveryPhase::Resolving;

        // 2a: Stale obligations (Reserved beyond timeout)
        if budget > 0 {
            let stale_ids = self.find_stale(now_ns, budget);
            for id in stale_ids {
                if budget == 0 {
                    break;
                }
                ledger.record_abort(id);
                let age = now_ns
                    .saturating_sub(self.first_seen_reserved.get(&id).copied().unwrap_or(now_ns));
                actions.push(RecoveryAction::StaleAbort { id, age_ns: age });
                self.first_seen_reserved.remove(&id);
                budget -= 1;
            }
        }

        // 2b: Conflicts
        let mut unresolved_conflicts = BTreeSet::new();
        if budget > 0 {
            let conflicts: Vec<ObligationId> = ledger
                .conflicts_iter()
                .take(budget)
                .map(|(id, _)| id)
                .collect();
            for id in conflicts {
                if budget == 0 {
                    break;
                }
                if self.config.auto_resolve_conflicts {
                    // Abort-wins repair: collapse conflict into a single abort.
                    ledger.force_abort_repair(id);
                    actions.push(RecoveryAction::ConflictResolved { id });
                } else {
                    unresolved_conflicts.insert(id);
                    actions.push(RecoveryAction::Flagged {
                        id,
                        reason: "conflict: Committed ⊔ Aborted".to_string(),
                    });
                }
                budget -= 1;
            }
        }

        // 2c: Linearity violations
        if budget > 0 {
            let violations: Vec<LinearityViolation> =
                ledger.linearity_violations_iter().take(budget).collect();
            for v in violations {
                if budget == 0 {
                    break;
                }
                if unresolved_conflicts.contains(&v.id) {
                    // When conflict auto-resolution is disabled, the conflict
                    // lane owns this obligation. Do not silently repair the
                    // same entry through the linearity path.
                    continue;
                }
                if self.config.auto_abort_violations {
                    ledger.force_abort_repair(v.id);
                    actions.push(RecoveryAction::ViolationAborted {
                        id: v.id,
                        total_acquires: v.total_acquires,
                        total_resolves: v.total_resolves,
                    });
                } else {
                    actions.push(RecoveryAction::Flagged {
                        id: v.id,
                        reason: format!(
                            "linearity: acquires={}, resolves={}",
                            v.total_acquires, v.total_resolves
                        ),
                    });
                }
                budget -= 1;
            }
        }

        self.total_actions += actions.len() as u64;
        self.phase = RecoveryPhase::Idle;

        // Compute remaining anomalies
        let remaining_pending = ledger.pending().len();
        let remaining_conflicts = ledger.conflicts().len();
        let remaining_violations = ledger.linearity_violations().len();
        let is_quiescent =
            remaining_pending == 0 && remaining_conflicts == 0 && remaining_violations == 0;

        RecoveryTickResult {
            actions,
            remaining_pending,
            remaining_conflicts,
            remaining_violations,
            is_quiescent,
        }
    }

    /// Updates the first-seen timestamps for currently reserved obligations.
    fn update_first_seen(&mut self, ledger: &CrdtObligationLedger, now_ns: u64) {
        // Remove entries for obligations no longer in Reserved state
        self.first_seen_reserved
            .retain(|id, _| ledger.get(id) == LatticeState::Reserved);

        // Add new entries for newly-seen Reserved obligations.
        for id in ledger.pending() {
            self.first_seen_reserved.entry(id).or_insert(now_ns);
        }
    }

    /// Returns obligations that have exceeded the stale timeout.
    fn find_stale(&self, now_ns: u64, limit: usize) -> Vec<ObligationId> {
        self.first_seen_reserved
            .iter()
            .filter(|(_, first_seen)| {
                now_ns.saturating_sub(**first_seen) >= self.config.stale_timeout_ns
            })
            .take(limit)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Resets the governor state (for testing).
    pub fn reset(&mut self) {
        self.phase = RecoveryPhase::Idle;
        self.first_seen_reserved.clear();
        self.total_ticks = 0;
        self.total_actions = 0;
    }
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::obligation::crdt::CrdtObligationLedger;
    use crate::record::ObligationKind;
    use crate::remote::NodeId;
    use crate::trace::distributed::crdt::Merge;
    use crate::types::ObligationId;

    fn oid(index: u32) -> ObligationId {
        ObligationId::new_for_test(index, 0)
    }

    fn node(name: &str) -> NodeId {
        NodeId::new(name)
    }

    fn test_config() -> RecoveryConfig {
        RecoveryConfig {
            stale_timeout_ns: 1000,
            max_resolutions_per_tick: 100,
            auto_resolve_conflicts: true,
            auto_abort_violations: true,
        }
    }

    // ── Basic lifecycle ─────────────────────────────────────────────────

    #[test]
    fn governor_starts_idle() {
        let gov = RecoveryGovernor::new(test_config());
        assert_eq!(gov.phase(), RecoveryPhase::Idle);
        assert_eq!(gov.total_ticks(), 0);
    }

    #[test]
    fn clean_ledger_is_quiescent() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);
        ledger.record_commit(oid(1));

        let result = gov.tick(&mut ledger, 0);
        assert!(result.is_quiescent);
        assert_eq!(result.action_count(), 0);
    }

    #[test]
    fn pending_obligation_not_stale_yet() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);

        // First tick at t=0: discovers the obligation, sets first_seen
        let result = gov.tick(&mut ledger, 0);
        assert!(!result.is_quiescent);
        assert_eq!(result.action_count(), 0);
        assert_eq!(result.remaining_pending, 1);

        // Second tick at t=500: not yet stale (timeout=1000)
        let result = gov.tick(&mut ledger, 500);
        assert_eq!(result.action_count(), 0);
    }

    // ── Stale obligation recovery ───────────────────────────────────────

    #[test]
    fn stale_obligation_is_aborted() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);

        // First tick: discover and set first_seen
        gov.tick(&mut ledger, 0);

        // Second tick after timeout: should abort
        let result = gov.tick(&mut ledger, 2000);
        assert_eq!(result.action_count(), 1);
        assert!(matches!(
            &result.actions[0],
            RecoveryAction::StaleAbort { id, age_ns } if *id == oid(1) && *age_ns >= 1000
        ));
        assert_eq!(ledger.get(&oid(1)), LatticeState::Aborted);
    }

    #[test]
    fn resolved_obligation_not_considered_stale() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);

        // First tick at t=0
        gov.tick(&mut ledger, 0);

        // Resolve before timeout
        ledger.record_commit(oid(1));

        // Tick after timeout: should not abort (already committed)
        let result = gov.tick(&mut ledger, 2000);
        assert_eq!(result.action_count(), 0);
        assert_eq!(ledger.get(&oid(1)), LatticeState::Committed);
    }

    // ── Conflict resolution ─────────────────────────────────────────────

    #[test]
    fn conflict_auto_resolved_by_abort() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut a = CrdtObligationLedger::new(node("A"));
        a.record_acquire(oid(1), ObligationKind::SendPermit);
        a.record_commit(oid(1));

        let mut b = CrdtObligationLedger::new(node("B"));
        b.record_acquire(oid(1), ObligationKind::SendPermit);
        b.record_abort(oid(1));

        // Merge creates conflict
        a.merge(&b);
        assert_eq!(a.get(&oid(1)), LatticeState::Conflict);

        // Recovery resolves it
        let result = gov.tick(&mut a, 0);
        assert!(
            result
                .actions
                .iter()
                .any(|a| matches!(a, RecoveryAction::ConflictResolved { .. }))
        );
    }

    #[test]
    fn conflict_repair_survives_stale_merge() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut a = CrdtObligationLedger::new(node("A"));
        a.record_acquire(oid(1), ObligationKind::SendPermit);
        a.record_commit(oid(1));

        let mut b = CrdtObligationLedger::new(node("B"));
        b.record_acquire(oid(1), ObligationKind::SendPermit);
        b.record_abort(oid(1));

        a.merge(&b);
        let stale_conflict = a.clone();
        assert_eq!(a.get(&oid(1)), LatticeState::Conflict);

        let result = gov.tick(&mut a, 0);
        assert!(
            result
                .actions
                .iter()
                .any(|a| matches!(a, RecoveryAction::ConflictResolved { .. }))
        );

        a.merge(&stale_conflict);
        let repaired = a.get_entry(&oid(1)).expect("entry should exist");
        assert_eq!(repaired.state, LatticeState::Aborted);
        assert!(repaired.is_linear());
        assert!(!repaired.is_conflict());
    }

    #[test]
    fn conflict_flagged_when_auto_resolve_disabled() {
        let mut config = test_config();
        config.auto_resolve_conflicts = false;
        let mut gov = RecoveryGovernor::new(config);

        let mut a = CrdtObligationLedger::new(node("A"));
        a.record_acquire(oid(1), ObligationKind::SendPermit);
        a.record_commit(oid(1));

        let mut b = CrdtObligationLedger::new(node("B"));
        b.record_acquire(oid(1), ObligationKind::SendPermit);
        b.record_abort(oid(1));

        a.merge(&b);

        let result = gov.tick(&mut a, 0);
        assert_eq!(result.action_count(), 1);
        assert!(matches!(
            &result.actions[0],
            RecoveryAction::Flagged { id, reason }
                if *id == oid(1) && reason == "conflict: Committed ⊔ Aborted"
        ));
        assert!(result.remaining_conflicts > 0);
        assert_eq!(a.get(&oid(1)), LatticeState::Conflict);
    }

    // ── Linearity violation recovery ────────────────────────────────────

    #[test]
    fn linearity_violation_auto_aborted() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);
        ledger.record_acquire(oid(1), ObligationKind::SendPermit); // double acquire

        let result = gov.tick(&mut ledger, 0);
        assert!(
            result
                .actions
                .iter()
                .any(|a| matches!(a, RecoveryAction::ViolationAborted { .. }))
        );
    }

    #[test]
    fn linearity_violation_flagged_when_auto_disabled() {
        let mut config = test_config();
        config.auto_abort_violations = false;
        let mut gov = RecoveryGovernor::new(config);

        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);

        let result = gov.tick(&mut ledger, 0);
        assert!(
            result
                .actions
                .iter()
                .any(|a| matches!(a, RecoveryAction::Flagged { .. }))
        );
    }

    // ── Convergence ─────────────────────────────────────────────────────

    #[test]
    fn repeated_ticks_converge_to_quiescence() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));

        // Create a messy state: stale, conflict, violation
        ledger.record_acquire(oid(1), ObligationKind::SendPermit); // will go stale
        ledger.record_acquire(oid(2), ObligationKind::Ack);
        ledger.record_acquire(oid(2), ObligationKind::Ack); // violation

        let mut b = CrdtObligationLedger::new(node("B"));
        b.record_acquire(oid(3), ObligationKind::Lease);
        b.record_commit(oid(3));
        let mut c = CrdtObligationLedger::new(node("C"));
        c.record_acquire(oid(3), ObligationKind::Lease);
        c.record_abort(oid(3));
        b.merge(&c); // conflict on oid(3)
        ledger.merge(&b);

        // First tick at t=0: resolves conflict and violation, discovers stale
        let r1 = gov.tick(&mut ledger, 0);
        assert!(r1.action_count() > 0);

        // After timeout: stale obligation gets aborted
        let _r2 = gov.tick(&mut ledger, 2000);

        // Should converge: no more pending, conflicts, or violations
        let r3 = gov.tick(&mut ledger, 3000);
        assert!(
            r3.is_quiescent,
            "not quiescent: pending={}, conflicts={}, violations={}",
            r3.remaining_pending, r3.remaining_conflicts, r3.remaining_violations
        );
    }

    #[test]
    fn convergence_is_monotonic() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));

        // Create several pending obligations
        for i in 0..5 {
            ledger.record_acquire(oid(i), ObligationKind::SendPermit);
        }

        // Discover all at t=0
        gov.tick(&mut ledger, 0);

        // After timeout: all should be aborted
        let result = gov.tick(&mut ledger, 2000);
        assert_eq!(result.remaining_pending, 0);

        // Additional tick: no more actions needed
        let result2 = gov.tick(&mut ledger, 3000);
        assert_eq!(result2.action_count(), 0);
        assert!(result2.is_quiescent);
    }

    // ── Budget limiting ─────────────────────────────────────────────────

    #[test]
    fn max_resolutions_per_tick_respected() {
        let mut config = test_config();
        config.max_resolutions_per_tick = 2;
        let mut gov = RecoveryGovernor::new(config);

        let mut ledger = CrdtObligationLedger::new(node("A"));
        for i in 0..5 {
            ledger.record_acquire(oid(i), ObligationKind::SendPermit);
        }

        // Discover all
        gov.tick(&mut ledger, 0);

        // After timeout: limited to 2 resolutions
        let result = gov.tick(&mut ledger, 2000);
        assert_eq!(result.action_count(), 2);
        assert_eq!(result.remaining_pending, 3);

        // Next tick resolves 2 more
        let result2 = gov.tick(&mut ledger, 3000);
        assert_eq!(result2.action_count(), 2);
        assert_eq!(result2.remaining_pending, 1);
    }

    // ── Reset ───────────────────────────────────────────────────────────

    #[test]
    fn reset_clears_state() {
        let mut gov = RecoveryGovernor::new(test_config());
        let mut ledger = CrdtObligationLedger::new(node("A"));
        ledger.record_acquire(oid(1), ObligationKind::SendPermit);
        gov.tick(&mut ledger, 0);

        gov.reset();
        assert_eq!(gov.phase(), RecoveryPhase::Idle);
        assert_eq!(gov.total_ticks(), 0);
        assert_eq!(gov.total_actions(), 0);
    }

    // ── Display ─────────────────────────────────────────────────────────

    #[test]
    fn recovery_action_display() {
        let action = RecoveryAction::StaleAbort {
            id: oid(1),
            age_ns: 5000,
        };
        let display = format!("{action}");
        assert!(display.contains("stale-abort"));
        assert!(display.contains("5000"));
    }

    #[test]
    fn recovery_phase_display() {
        assert_eq!(format!("{}", RecoveryPhase::Idle), "Idle");
        assert_eq!(format!("{}", RecoveryPhase::Scanning), "Scanning");
        assert_eq!(format!("{}", RecoveryPhase::Resolving), "Resolving");
    }

    // ── Partition/heal scenario ─────────────────────────────────────────

    #[test]
    fn partition_heal_converges() {
        let mut gov = RecoveryGovernor::new(test_config());

        // Node A acquires and commits
        let mut a = CrdtObligationLedger::new(node("A"));
        a.record_acquire(oid(1), ObligationKind::SendPermit);
        a.record_commit(oid(1));

        // Node B (partitioned) only saw the acquire
        let mut b = CrdtObligationLedger::new(node("B"));
        b.record_acquire(oid(1), ObligationKind::SendPermit);

        // Node B runs recovery: oid(1) appears stale on B
        gov.tick(&mut b, 0);
        let _result = gov.tick(&mut b, 2000);
        // B aborts it (stale)
        assert_eq!(b.get(&oid(1)), LatticeState::Aborted);

        // Partition heals: merge A and B
        a.merge(&b);
        // Committed ⊔ Aborted = Conflict
        assert_eq!(a.get(&oid(1)), LatticeState::Conflict);

        // Recovery resolves the conflict
        let mut gov2 = RecoveryGovernor::new(test_config());
        let result = gov2.tick(&mut a, 0);
        assert!(
            result
                .actions
                .iter()
                .any(|a| matches!(a, RecoveryAction::ConflictResolved { .. }))
        );
    }

    #[test]
    fn recovery_config_debug_clone_default() {
        let c = RecoveryConfig::default();
        let dbg = format!("{c:?}");
        assert!(dbg.contains("RecoveryConfig"));

        let c2 = c;
        assert_eq!(c2.stale_timeout_ns, 30_000_000_000);
        assert_eq!(c2.max_resolutions_per_tick, 50);

        let c3 = RecoveryConfig::default_for_test();
        assert_eq!(c3.stale_timeout_ns, 5_000_000_000);
    }

    #[test]
    fn recovery_phase_debug_clone_copy_eq() {
        let p = RecoveryPhase::Idle;
        let dbg = format!("{p:?}");
        assert!(dbg.contains("Idle"));

        let p2 = p;
        assert_eq!(p, p2);

        let p3 = p;
        assert_eq!(p, p3);

        assert_ne!(RecoveryPhase::Idle, RecoveryPhase::Scanning);
    }

    #[test]
    fn recovery_action_debug_clone_eq() {
        let a = RecoveryAction::ConflictResolved { id: oid(42) };
        let dbg = format!("{a:?}");
        assert!(dbg.contains("ConflictResolved"));

        let a2 = a.clone();
        assert_eq!(a, a2);

        let a3 = RecoveryAction::Flagged {
            id: oid(1),
            reason: "test".into(),
        };
        assert_ne!(a, a3);
    }

    #[test]
    fn recovery_tick_result_debug_clone() {
        let r = RecoveryTickResult {
            actions: vec![],
            remaining_pending: 0,
            remaining_conflicts: 0,
            remaining_violations: 0,
            is_quiescent: true,
        };
        let dbg = format!("{r:?}");
        assert!(dbg.contains("RecoveryTickResult"));

        let r2 = r;
        assert!(r2.is_quiescent);
        assert!(r2.actions.is_empty());
    }
}