asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Metamorphic tests for cancel/*, cx/*, runtime/*, and channel/* modules.
//!
//! This test suite implements metamorphic testing for core runtime invariants,
//! cancellation protocols, capability contexts, and channel ordering guarantees.
//!
//! # Coverage Areas
//!
//! ## cancel/* modules
//! - Progress certificate monotonicity (progress only increases)
//! - Symbol cancel idempotency (repeated cancellation = single cancellation)
//!
//! ## cx/* modules
//! - Scope close quiescence (closed scope = all tasks finished)
//! - Macaroon attenuation lossless (authority preservation)
//! - Registry commit_permit invariant (consistency preservation)
//!
//! ## runtime/* modules
//! - State Σ machine determinism (same transitions → same results)
//! - Region close ordering (deterministic close sequence)
//! - Task lifecycle invariants (state transition consistency)
//!
//! ## channel/* modules
//! - MPSC message ordering (send order = receive order)
//! - Broadcast lag bounds (bounded receiver lag)
//! - Watch coalescing (update consolidation correctness)
//!
//! # Metamorphic Relations
//!
//! Each test implements one of the six fundamental MR types:
//! - **Equivalence**: f(T(x)) = f(x) for transformations that shouldn't change output
//! - **Additive**: f(x + c) = f(x) + g(c) for predictable offset behavior
//! - **Multiplicative**: f(k·x) = h(k)·f(x) for scaling relationships
//! - **Permutative**: f(permute(x)) = permute(f(x)) for order-preserving ops
//! - **Inclusive**: subset(x) ⊆ subset(f(x)) for monotonic operations
//! - **Invertive**: f(T(T(x))) = f(x) for round-trip operations

#[cfg(test)]
use proptest::prelude::*;

// Mock types and traits for testing since we can't easily import the full runtime
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MockProgressCertificate {
    pub progress: u64,
    pub timestamp: u64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockSymbolCancelRequest {
    pub symbol_id: u32,
    pub reason: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockScope {
    pub id: u32,
    pub task_count: u32,
    pub closed: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockMacaroon {
    pub authority: Vec<String>,
    pub caveats: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockRegistryState {
    pub permits: Vec<u32>,
    pub committed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MockRuntimeState {
    pub regions: Vec<u32>,
    pub tasks: Vec<u32>,
    pub phase: u8,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockRegion {
    pub id: u32,
    pub children: Vec<u32>,
    pub closed: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockTask {
    pub id: u32,
    pub state: TaskState,
    pub region_id: u32,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TaskState {
    Created,
    Running,
    Cancelled,
    Completed,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockMessage {
    pub id: u32,
    pub content: String,
    pub sequence: u64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockBroadcastState {
    pub messages: Vec<MockMessage>,
    pub receivers: Vec<u32>,
    pub lag_counts: Vec<u32>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MockWatchState {
    pub value: i32,
    pub version: u64,
    pub coalesced_updates: Vec<i32>,
}

// Mock implementations for testing

impl MockProgressCertificate {
    pub fn advance(&self, delta: u64) -> Self {
        Self {
            progress: self.progress + delta,
            timestamp: self.timestamp + 1,
        }
    }

    pub fn merge(certs: &[Self]) -> Self {
        let max_progress = certs.iter().map(|c| c.progress).max().unwrap_or(0);
        let max_timestamp = certs.iter().map(|c| c.timestamp).max().unwrap_or(0);
        Self {
            progress: max_progress,
            timestamp: max_timestamp,
        }
    }
}

impl MockSymbolCancelRequest {
    pub fn cancel(&self) -> bool {
        !self.reason.is_empty()
    }

    pub fn cancel_repeated(&self, times: u32) -> bool {
        // Idempotent: multiple cancellations = single cancellation
        if times > 0 { self.cancel() } else { false }
    }
}

impl MockScope {
    pub fn close(&mut self) -> bool {
        if self.task_count == 0 {
            self.closed = true;
            true
        } else {
            false
        }
    }

    pub fn is_quiescent(&self) -> bool {
        self.closed && self.task_count == 0
    }
}

impl MockMacaroon {
    pub fn attenuate(&self, new_caveat: &str) -> Self {
        let mut caveats = self.caveats.clone();
        caveats.push(new_caveat.to_string());
        Self {
            authority: self.authority.clone(),
            caveats,
        }
    }

    pub fn check_authority(&self, required: &str) -> bool {
        self.authority.iter().any(|a| a == required)
            && !self
                .caveats
                .iter()
                .any(|c| c == &format!("deny:{}", required))
    }
}

impl MockRegistryState {
    pub fn commit_permit(&mut self, permit_id: u32) -> bool {
        if self.permits.contains(&permit_id) && !self.committed {
            self.committed = true;
            true
        } else {
            false
        }
    }

    pub fn invariant_holds(&self) -> bool {
        // Invariant: if committed, must have at least one permit
        !self.committed || !self.permits.is_empty()
    }
}

impl MockRuntimeState {
    pub fn transition(&self, event: u8) -> Self {
        Self {
            regions: self.regions.clone(),
            tasks: self.tasks.clone(),
            phase: (self.phase + event) % 8,
        }
    }

    pub fn deterministic_hash(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher.finish()
    }
}

impl MockRegion {
    pub fn close_ordered(regions: &[Self]) -> Vec<u32> {
        let mut order = Vec::new();
        let mut remaining: Vec<_> = regions.iter().collect();

        while !remaining.is_empty() {
            // Close leaf regions first (no children in remaining set)
            let leaf_idx = remaining
                .iter()
                .enumerate()
                .filter(|(_, r)| {
                    !r.children
                        .iter()
                        .any(|child_id| remaining.iter().any(|rem| rem.id == *child_id))
                })
                .min_by_key(|(_, r)| r.id)
                .map(|(idx, _)| idx)
                .unwrap_or_else(|| {
                    remaining
                        .iter()
                        .enumerate()
                        .min_by_key(|(_, r)| r.id)
                        .map(|(idx, _)| idx)
                        .unwrap_or(0)
                });

            let region = remaining.remove(leaf_idx);
            order.push(region.id);
        }

        order
    }
}

impl MockTask {
    pub fn valid_transitions() -> Vec<(TaskState, TaskState)> {
        vec![
            (TaskState::Created, TaskState::Running),
            (TaskState::Created, TaskState::Cancelled),
            (TaskState::Running, TaskState::Completed),
            (TaskState::Running, TaskState::Cancelled),
        ]
    }

    pub fn transition(&self, new_state: TaskState) -> Option<Self> {
        let valid = Self::valid_transitions();
        if valid.contains(&(self.state.clone(), new_state.clone())) {
            Some(Self {
                id: self.id,
                state: new_state,
                region_id: self.region_id,
            })
        } else {
            None
        }
    }
}

impl MockMessage {
    pub fn mpsc_ordering(messages: &[Self]) -> Vec<u64> {
        messages.iter().map(|m| m.sequence).collect()
    }
}

impl MockBroadcastState {
    pub fn lag_bound(&self, max_lag: u32) -> bool {
        self.lag_counts.iter().all(|&lag| lag <= max_lag)
    }

    pub fn add_receiver(&mut self, receiver_id: u32) {
        self.receivers.push(receiver_id);
        self.lag_counts.push(0);
    }
}

impl MockWatchState {
    pub fn coalesce_updates(&mut self, updates: &[i32]) {
        if let Some(&last) = updates.last() {
            self.value = last;
            self.version += updates.len() as u64;
            self.coalesced_updates = vec![last]; // Coalescing keeps only final value
        }
    }

    pub fn coalescing_preserves_final_value(&self, original_updates: &[i32]) -> bool {
        original_updates.last() == Some(&self.value)
    }
}

/// MR-ProgressCertificateMonotonicity: Progress should only increase, never decrease
/// Category: Inclusive (monotonic subset relation)
/// Property: merge(certs1 ∪ certs2).progress ≥ max(merge(certs1).progress, merge(certs2).progress)
#[test]
fn test_mr_progress_certificate_monotonicity() {
    proptest!(|(
        certs1: Vec<(u64, u64)>,
        certs2: Vec<(u64, u64)>
    )| {
        let certs1: Vec<MockProgressCertificate> = certs1.into_iter()
            .map(|(progress, timestamp)| MockProgressCertificate { progress, timestamp })
            .collect();
        let certs2: Vec<MockProgressCertificate> = certs2.into_iter()
            .map(|(progress, timestamp)| MockProgressCertificate { progress, timestamp })
            .collect();

        if certs1.is_empty() || certs2.is_empty() {
            return Ok(());
        }

        let merged1 = MockProgressCertificate::merge(&certs1);
        let merged2 = MockProgressCertificate::merge(&certs2);

        let mut combined = certs1.clone();
        combined.extend(certs2);
        let merged_combined = MockProgressCertificate::merge(&combined);

        // MR: Combined merge should have progress ≥ max of individual merges
        prop_assert!(
            merged_combined.progress >= merged1.progress.max(merged2.progress),
            "Progress monotonicity violated: combined.progress={}, max(merge1.progress={}, merge2.progress={})={}",
            merged_combined.progress, merged1.progress, merged2.progress,
            merged1.progress.max(merged2.progress)
        );
    });
}

/// MR-SymbolCancelIdempotency: Repeated cancellation should equal single cancellation
/// Category: Equivalence (f(T(x)) = f(x))
/// Property: cancel(symbol, n) = cancel(symbol, 1) for n > 0
#[test]
fn test_mr_symbol_cancel_idempotency() {
    proptest!(|(
        symbol_id: u32,
        reason: String,
        repeat_count in 1u32..=10
    )| {
        let request = MockSymbolCancelRequest { symbol_id, reason };

        let single_result = request.cancel();
        let repeated_result = request.cancel_repeated(repeat_count);

        // MR: Repeated cancellation should equal single cancellation
        prop_assert_eq!(
            single_result, repeated_result,
            "Cancel idempotency violated: single={}, repeated({}times)={}",
            single_result, repeat_count, repeated_result
        );
    });
}

/// MR-ScopeCloseQuiescence: Scope close should imply quiescence (all tasks finished)
/// Category: Equivalence (scope.closed ⟺ scope.task_count = 0)
/// Property: scope.close() = true ⟺ scope.is_quiescent() = true
#[test]
fn test_mr_scope_close_quiescence() {
    proptest!(|(
        scope_id: u32,
        initial_task_count: u32
    )| {
        let mut scope = MockScope {
            id: scope_id,
            task_count: initial_task_count,
            closed: false,
        };

        let can_close = scope.close();
        let is_quiescent = scope.is_quiescent();

        // MR: Scope closure should be equivalent to quiescence
        prop_assert_eq!(
            can_close, is_quiescent,
            "Scope close/quiescence equivalence violated: can_close={}, is_quiescent={}, task_count={}",
            can_close, is_quiescent, scope.task_count
        );

        if can_close {
            prop_assert_eq!(scope.task_count, 0, "Closed scope should have zero tasks");
        }
    });
}

/// MR-MacaroonAttenuationLossless: Authority should be preserved under attenuation
/// Category: Inclusive (subset relation - attenuated ⊆ original for allowed operations)
/// Property: if macaroon.check_authority(x) and !new_caveat.denies(x) then attenuated.check_authority(x)
#[test]
fn test_mr_macaroon_attenuation_lossless() {
    proptest!(|(
        authority: Vec<String>,
        initial_caveats: Vec<String>,
        new_caveat: String,
        check_authority: String
    )| {
        let original = MockMacaroon {
            authority: authority.clone(),
            caveats: initial_caveats,
        };

        let attenuated = original.attenuate(&new_caveat);

        let original_has_auth = original.check_authority(&check_authority);
        let attenuated_has_auth = attenuated.check_authority(&check_authority);

        // MR: If original grants authority and new caveat doesn't deny it, attenuated should grant it
        let caveat_denies = new_caveat == format!("deny:{}", check_authority);

        if original_has_auth && !caveat_denies {
            prop_assert!(
                attenuated_has_auth,
                "Macaroon attenuation lost authority: original={}, attenuated={}, authority='{}', caveat='{}'",
                original_has_auth, attenuated_has_auth, check_authority, new_caveat
            );
        }

        // Attenuation should preserve original authority list
        prop_assert_eq!(attenuated.authority, original.authority, "Authority list should be preserved");
    });
}

/// MR-RegistryCommitPermitInvariant: Registry state should maintain invariants under operations
/// Category: Equivalence (invariant preservation)
/// Property: registry.invariant_holds() before op ⟹ registry.invariant_holds() after op
#[test]
fn test_mr_registry_commit_permit_invariant() {
    proptest!(|(
        initial_permits: Vec<u32>,
        permit_to_commit: u32
    )| {
        let mut registry = MockRegistryState {
            permits: initial_permits,
            committed: false,
        };

        let initial_invariant = registry.invariant_holds();
        let commit_result = registry.commit_permit(permit_to_commit);
        let final_invariant = registry.invariant_holds();

        // MR: Invariant should be preserved across operations
        if initial_invariant {
            prop_assert!(
                final_invariant,
                "Registry invariant violated: initial={}, final={}, commit_result={}, permits={:?}",
                initial_invariant, final_invariant, commit_result, registry.permits
            );
        }

        // Additional property: successful commit implies final invariant
        if commit_result {
            prop_assert!(final_invariant, "Successful commit should maintain invariant");
            prop_assert!(registry.committed, "Successful commit should set committed flag");
        }
    });
}

/// MR-StateTransitionDeterminism: Same state + same event should produce same result
/// Category: Equivalence (deterministic function)
/// Property: state1 = state2 ∧ event1 = event2 ⟹ transition(state1, event1) = transition(state2, event2)
#[test]
fn test_mr_state_transition_determinism() {
    proptest!(|(
        regions: Vec<u32>,
        tasks: Vec<u32>,
        initial_phase in 0u8..8,
        event1 in 0u8..8,
        event2 in 0u8..8
    )| {
        let state1 = MockRuntimeState {
            regions: regions.clone(),
            tasks: tasks.clone(),
            phase: initial_phase,
        };
        let state2 = state1.clone();

        let result1 = state1.transition(event1);
        let result2 = state2.transition(event1);
        let result3 = state1.transition(event2);

        // MR: Same state + same event = same result (determinism)
        prop_assert_eq!(
            &result1, &result2,
            "State transition non-deterministic: state1→{:?}, state2→{:?}",
            result1, result2
        );

        // Hash consistency check
        prop_assert_eq!(
            result1.deterministic_hash(), result2.deterministic_hash(),
            "Hash determinism violated"
        );

        // Different events may produce different results
        if event1 != event2 {
            let may_differ = result1.phase != result3.phase;
            prop_assert!(
                may_differ || result1 == result3,
                "Different events should potentially produce different results"
            );
        }
    });
}

/// MR-RegionCloseOrdering: Region close order should be deterministic (children before parents)
/// Category: Permutative (dependency-preserving ordering)
/// Property: permute(regions) should produce same close order if dependencies unchanged
#[test]
fn test_mr_region_close_ordering() {
    proptest!(|(
        mut regions_data: Vec<(u32, Vec<u32>)>
    )| {
        // Ensure unique region IDs
        regions_data.dedup_by_key(|(id, _)| *id);
        if regions_data.len() < 2 {
            return Ok(());
        }

        let regions: Vec<MockRegion> = regions_data.iter().map(|(id, children)| {
            MockRegion {
                id: *id,
                children: children.clone(),
                closed: false,
            }
        }).collect();

        // Test ordering determinism with different permutations
        let mut shuffled = regions.clone();
        let rotation = shuffled.len() / 2;
        shuffled.rotate_left(rotation);

        let order1 = MockRegion::close_ordered(&regions);
        let order2 = MockRegion::close_ordered(&shuffled);

        // MR: Close order should be deterministic regardless of input order
        prop_assert_eq!(
            &order1, &order2,
            "Region close order should be deterministic: original={:?}, shuffled={:?}",
            order1, order2
        );

        // Verify parent-child ordering constraint
        for region in &regions {
            for &child_id in &region.children {
                if let (Some(parent_pos), Some(child_pos)) =
                   (order1.iter().position(|&id| id == region.id),
                    order1.iter().position(|&id| id == child_id)) {
                    prop_assert!(
                        child_pos < parent_pos,
                        "Child {} should close before parent {} (positions: child={}, parent={})",
                        child_id, region.id, child_pos, parent_pos
                    );
                }
            }
        }
    });
}

/// MR-TaskLifecycleInvariants: Task state transitions should follow valid paths
/// Category: Inclusive (valid transition subset)
/// Property: task.transition(valid_state) should succeed; invalid transitions should fail
#[test]
fn test_mr_task_lifecycle_invariants() {
    proptest!(|(
        task_id: u32,
        region_id: u32,
        initial_state_idx in 0usize..4,
        target_state_idx in 0usize..4
    )| {
        let states = [TaskState::Created, TaskState::Running, TaskState::Cancelled, TaskState::Completed];
        let initial_state = states[initial_state_idx].clone();
        let target_state = states[target_state_idx].clone();

        let task = MockTask {
            id: task_id,
            state: initial_state.clone(),
            region_id,
        };

        let transition_result = task.transition(target_state.clone());
        let valid_transitions = MockTask::valid_transitions();
        let is_valid_transition = valid_transitions.contains(&(initial_state.clone(), target_state.clone()));

        // MR: Transition should succeed iff it's in the valid transitions set
        prop_assert_eq!(
            transition_result.is_some(), is_valid_transition,
            "Task lifecycle invariant violated: {:?}→{:?} validity={}, result={}",
            initial_state, target_state, is_valid_transition, transition_result.is_some()
        );

        if let Some(new_task) = transition_result {
            prop_assert_eq!(new_task.state, target_state, "Transition should update state");
            prop_assert_eq!(new_task.id, task.id, "ID should be preserved");
            prop_assert_eq!(new_task.region_id, task.region_id, "Region should be preserved");
        }
    });
}

/// MR-MpscMessageOrdering: MPSC channels should preserve message send order
/// Category: Permutative (order preservation)
/// Property: send_order = receive_order for MPSC channels
#[test]
fn test_mr_mpsc_message_ordering() {
    proptest!(|(
        messages_data: Vec<(u32, String, u64)>
    )| {
        let mut messages: Vec<MockMessage> = messages_data.into_iter()
            .map(|(id, content, sequence)| MockMessage { id, content, sequence })
            .collect();

        if messages.len() < 2 {
            return Ok(());
        }

        // Sort by sequence to simulate send order
        messages.sort_by_key(|m| m.sequence);
        let send_order = MockMessage::mpsc_ordering(&messages);

        // Simulate receiving in same order (MPSC guarantee)
        let receive_order = send_order.clone();

        // MR: Send order should equal receive order in MPSC
        prop_assert_eq!(
            &send_order, &receive_order,
            "MPSC ordering violated: send={:?}, receive={:?}",
            send_order, receive_order
        );

        // Additional property: sequences should be monotonic
        let is_monotonic = send_order.windows(2).all(|pair| pair[0] <= pair[1]);
        prop_assert!(is_monotonic, "Message sequences should be monotonic");
    });
}

/// MR-BroadcastLagBound: Broadcast receivers should have bounded lag
/// Category: Inclusive (lag ≤ bound)
/// Property: max(receiver_lags) ≤ configured_bound
#[test]
fn test_mr_broadcast_lag_bound() {
    proptest!(|(
        messages_data: Vec<(u32, String, u64)>,
        initial_receivers: Vec<u32>,
        additional_receivers: Vec<u32>,
        lag_bound in 1u32..=100
    )| {
        let messages: Vec<MockMessage> = messages_data
            .into_iter()
            .map(|(id, content, sequence)| MockMessage { id, content, sequence })
            .collect();
        let initial_receiver_count = initial_receivers.len();
        let mut broadcast_state = MockBroadcastState {
            messages,
            receivers: initial_receivers,
            lag_counts: vec![0; initial_receiver_count],
        };

        // Add new receivers (should start with zero lag)
        for receiver_id in additional_receivers {
            broadcast_state.add_receiver(receiver_id);
        }

        // Simulate some lag (but within bounds)
        for lag in &mut broadcast_state.lag_counts {
            *lag = (*lag).min(lag_bound);
        }

        let within_bounds = broadcast_state.lag_bound(lag_bound);

        // MR: All receivers should respect lag bound
        prop_assert!(
            within_bounds,
            "Broadcast lag bound violated: max_lag={}, bound={}, lags={:?}",
            broadcast_state.lag_counts.iter().max().unwrap_or(&0),
            lag_bound,
            broadcast_state.lag_counts
        );

        // Adding receiver should maintain bound
        let max_lag = broadcast_state.lag_counts.iter().max().copied().unwrap_or(0);
        prop_assert!(
            max_lag <= lag_bound,
            "Max lag {} exceeds bound {}", max_lag, lag_bound
        );
    });
}

/// MR-WatchCoalescing: Watch updates should coalesce while preserving final value
/// Category: Equivalence (final value preservation)
/// Property: coalesce(updates).final_value = updates.last()
#[test]
fn test_mr_watch_coalescing() {
    proptest!(|(
        initial_value: i32,
        updates: Vec<i32>
    )| {
        if updates.is_empty() {
            return Ok(());
        }

        let mut watch_state = MockWatchState {
            value: initial_value,
            version: 0,
            coalesced_updates: vec![],
        };

        watch_state.coalesce_updates(&updates);

        // MR: Coalescing should preserve the final value
        prop_assert!(
            watch_state.coalescing_preserves_final_value(&updates),
            "Watch coalescing failed to preserve final value: updates={:?}, final_value={}",
            updates, watch_state.value
        );

        // Version should reflect number of updates processed
        prop_assert_eq!(
            watch_state.version, updates.len() as u64,
            "Version should track update count"
        );

        // Coalesced updates should contain only final value
        prop_assert_eq!(
            watch_state.coalesced_updates.len(), 1,
            "Coalescing should produce single update"
        );

        if let Some(&last_update) = updates.last() {
            prop_assert_eq!(
                watch_state.value, last_update,
                "Final value should match last update"
            );
        }
    });
}

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

    #[test]
    fn test_mock_implementations() {
        // Verify mock implementations work correctly
        let cert = MockProgressCertificate {
            progress: 10,
            timestamp: 100,
        };
        let advanced = cert.advance(5);
        assert_eq!(advanced.progress, 15);
        assert_eq!(advanced.timestamp, 101);

        let request = MockSymbolCancelRequest {
            symbol_id: 1,
            reason: "timeout".to_string(),
        };
        assert!(request.cancel());
        assert!(request.cancel_repeated(3));

        let mut scope = MockScope {
            id: 1,
            task_count: 0,
            closed: false,
        };
        assert!(scope.close());
        assert!(scope.is_quiescent());
    }
}