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
//! Metamorphic tests for EnvConfig component.
//!
//! These tests verify invariant relationships for runtime configuration parsing
//! and precedence resolution, addressing the oracle problem for complex
//! configuration validation logic. Each test focuses on a specific metamorphic
//! relation derived from configuration system domain properties.

#![allow(dead_code, clippy::pedantic, clippy::nursery, clippy::unwrap_used)]

use proptest::prelude::*;
use std::collections::HashMap;
use std::env;

use super::*;
use crate::runtime::config::RuntimeConfig;

const ENV_CONFIG_VARS: &[&str] = &[
    ENV_WORKER_THREADS,
    ENV_TASK_QUEUE_DEPTH,
    ENV_THREAD_STACK_SIZE,
    ENV_THREAD_NAME_PREFIX,
    ENV_STEAL_BATCH_SIZE,
    ENV_BLOCKING_MIN_THREADS,
    ENV_BLOCKING_MAX_THREADS,
    ENV_ENABLE_PARKING,
    ENV_POLL_BUDGET,
    ENV_CANCEL_LANE_MAX_STREAK,
    ENV_ENABLE_GOVERNOR,
    ENV_GOVERNOR_INTERVAL,
    ENV_ENABLE_ADAPTIVE_CANCEL_STREAK,
    ENV_ADAPTIVE_CANCEL_EPOCH_STEPS,
];

fn apply_env_overrides(config: &mut RuntimeConfig) -> Result<(), BuildError> {
    super::apply_env_overrides(config, &SystemEnvReader::new())
}

/// Test environment variable setting for controlled testing.
#[derive(Debug, Clone)]
struct TestEnvVar {
    name: String,
    value: String,
}

/// Generate arbitrary valid environment variables.
fn arb_env_var() -> impl Strategy<Value = TestEnvVar> {
    prop_oneof![
        (1usize..1000).prop_map(|n| TestEnvVar {
            name: ENV_WORKER_THREADS.to_string(),
            value: n.to_string()
        }),
        (1usize..10000).prop_map(|n| TestEnvVar {
            name: ENV_TASK_QUEUE_DEPTH.to_string(),
            value: n.to_string()
        }),
        (1024usize..8388608).prop_map(|n| TestEnvVar {
            name: ENV_THREAD_STACK_SIZE.to_string(),
            value: n.to_string()
        }),
        "[a-z]{3,10}-worker".prop_map(|s| TestEnvVar {
            name: ENV_THREAD_NAME_PREFIX.to_string(),
            value: s
        }),
        (1usize..100).prop_map(|n| TestEnvVar {
            name: ENV_STEAL_BATCH_SIZE.to_string(),
            value: n.to_string()
        }),
        (1usize..10).prop_map(|n| TestEnvVar {
            name: ENV_BLOCKING_MIN_THREADS.to_string(),
            value: n.to_string()
        }),
        (10usize..1000).prop_map(|n| TestEnvVar {
            name: ENV_BLOCKING_MAX_THREADS.to_string(),
            value: n.to_string()
        }),
        prop_oneof!["true", "false", "1", "0", "yes", "no", "on", "off"].prop_map(|s| TestEnvVar {
            name: ENV_ENABLE_PARKING.to_string(),
            value: s.to_string()
        }),
        (1u32..1000).prop_map(|n| TestEnvVar {
            name: ENV_POLL_BUDGET.to_string(),
            value: n.to_string()
        }),
    ]
}

/// Boolean value representations for testing equivalence.
#[derive(Debug, Clone)]
enum BoolRepr {
    True(String),  // "true", "1", "yes", "on"
    False(String), // "false", "0", "no", "off"
}

impl BoolRepr {
    fn value(&self) -> &str {
        match self {
            BoolRepr::True(s) | BoolRepr::False(s) => s,
        }
    }

    fn expected_bool(&self) -> bool {
        match self {
            BoolRepr::True(_) => true,
            BoolRepr::False(_) => false,
        }
    }
}

/// Generate arbitrary boolean representations.
fn arb_bool_repr() -> impl Strategy<Value = BoolRepr> {
    prop_oneof![
        prop_oneof!["true", "1", "yes", "on"].prop_map(|s| BoolRepr::True(s.to_string())),
        prop_oneof!["false", "0", "no", "off"].prop_map(|s| BoolRepr::False(s.to_string())),
    ]
}

/// Configuration field operations for metamorphic testing.
#[derive(Debug, Clone)]
enum ConfigOperation {
    SetEnvVar { var: TestEnvVar },
    UnsetEnvVar { name: String },
    ApplyDefaults,
    ParseField { field_name: String, value: String },
}

/// Generate arbitrary config operations.
fn arb_config_operation() -> impl Strategy<Value = ConfigOperation> {
    prop_oneof![
        arb_env_var().prop_map(|var| ConfigOperation::SetEnvVar { var }),
        prop_oneof![
            ENV_WORKER_THREADS,
            ENV_TASK_QUEUE_DEPTH,
            ENV_ENABLE_PARKING,
            ENV_POLL_BUDGET
        ]
        .prop_map(|name| ConfigOperation::UnsetEnvVar {
            name: name.to_string()
        }),
        Just(ConfigOperation::ApplyDefaults),
        ("[a-z_]+", "[0-9]+").prop_map(|(name, value)| ConfigOperation::ParseField {
            field_name: name,
            value
        }),
    ]
}

/// Helper to set environment variables with cleanup.
struct EnvGuard {
    _lock: parking_lot::MutexGuard<'static, ()>,
    vars_to_unset: Vec<String>,
    vars_to_restore: HashMap<String, String>,
}

impl EnvGuard {
    fn new() -> Self {
        let mut guard = Self {
            _lock: crate::test_utils::env_lock(),
            vars_to_unset: Vec::new(),
            vars_to_restore: HashMap::new(),
        };

        for &name in ENV_CONFIG_VARS {
            guard.unset(name);
        }

        guard
    }

    #[allow(unsafe_code)]
    fn set(&mut self, name: &str, value: &str) {
        // Save original value for restoration
        if let Ok(original) = env::var(name) {
            self.vars_to_restore.insert(name.to_string(), original);
        } else {
            self.vars_to_unset.push(name.to_string());
        }
        // SAFETY: These tests serialize all environment mutation through the
        // crate-wide env_lock, use crate-scoped variable names, and do not
        // spawn worker threads while the guard is live.
        unsafe { env::set_var(name, value) };
    }

    #[allow(unsafe_code)]
    fn unset(&mut self, name: &str) {
        if let Ok(original) = env::var(name) {
            self.vars_to_restore.insert(name.to_string(), original);
        }
        // SAFETY: See EnvGuard::set; the same serialized test-only mutation
        // discipline applies to removal.
        unsafe { env::remove_var(name) };
    }
}

impl Drop for EnvGuard {
    #[allow(unsafe_code)]
    fn drop(&mut self) {
        // Restore original environment
        for name in &self.vars_to_unset {
            // SAFETY: EnvGuard owns the module-wide environment mutation lock.
            unsafe { env::remove_var(name) };
        }
        for (name, value) in &self.vars_to_restore {
            // SAFETY: EnvGuard owns the module-wide environment mutation lock.
            unsafe { env::set_var(name, value) };
        }
    }
}

/// Capture configuration state for comparison.
#[derive(Debug, Clone, PartialEq)]
struct ConfigSnapshot {
    worker_threads: usize,
    global_queue_limit: usize,
    thread_stack_size: usize,
    thread_name_prefix: String,
    steal_batch_size: usize,
    blocking_min_threads: usize,
    blocking_max_threads: usize,
    enable_parking: bool,
    poll_budget: u32,
    cancel_lane_max_streak: usize,
    enable_governor: bool,
    governor_interval: u32,
}

impl ConfigSnapshot {
    fn capture(config: &RuntimeConfig) -> Self {
        Self {
            worker_threads: config.worker_threads,
            global_queue_limit: config.global_queue_limit,
            thread_stack_size: config.thread_stack_size,
            thread_name_prefix: config.thread_name_prefix.clone(),
            steal_batch_size: config.steal_batch_size,
            blocking_min_threads: config.blocking.min_threads,
            blocking_max_threads: config.blocking.max_threads,
            enable_parking: config.enable_parking,
            poll_budget: config.poll_budget,
            cancel_lane_max_streak: config.cancel_lane_max_streak,
            enable_governor: config.enable_governor,
            governor_interval: config.governor_interval,
        }
    }
}

//
// METAMORPHIC RELATIONS - Configuration system invariants
//

/// MR1: EQUIVALENCE - Default Consistency
/// Default config should be identical regardless of construction path.
#[test]
fn mr_default_consistency() {
    proptest!(|(_case in Just(()))| {
        let mut env_guard = EnvGuard::new();

        // Clear all asupersync env vars for clean test
        for var_name in &[
            ENV_WORKER_THREADS,
            ENV_TASK_QUEUE_DEPTH,
            ENV_THREAD_STACK_SIZE,
            ENV_THREAD_NAME_PREFIX,
            ENV_STEAL_BATCH_SIZE,
            ENV_BLOCKING_MIN_THREADS,
            ENV_BLOCKING_MAX_THREADS,
            ENV_ENABLE_PARKING,
            ENV_POLL_BUDGET,
            ENV_CANCEL_LANE_MAX_STREAK,
            ENV_ENABLE_GOVERNOR,
            ENV_GOVERNOR_INTERVAL,
        ] {
            env_guard.unset(var_name);
        }

        // Create config via default constructor
        let config1 = RuntimeConfig::default();

        // Create config via env application to defaults
        let mut config2 = RuntimeConfig::default();
        let result = apply_env_overrides(&mut config2);
        prop_assert!(
            result.is_ok(),
            "Apply env overrides should succeed with no env vars"
        );

        // Both should be identical
        let snapshot1 = ConfigSnapshot::capture(&config1);
        let snapshot2 = ConfigSnapshot::capture(&config2);

        prop_assert_eq!(
            snapshot1,
            snapshot2,
            "Default configs should be identical: direct vs env_applied"
        );
    });
}

/// MR2: EQUIVALENCE - Boolean Parsing Equivalence
/// Different representations of the same boolean value should parse identically.
#[test]
fn mr_boolean_parsing_equivalence() {
    proptest!(|(repr1 in arb_bool_repr(), repr2 in arb_bool_repr())| {
        // Skip if different expected values
        if repr1.expected_bool() != repr2.expected_bool() {
            return Ok(());
        }

        let mut env_guard = EnvGuard::new();

        // Set same boolean field with different representations
        env_guard.set(ENV_ENABLE_PARKING, repr1.value());
        let mut config1 = RuntimeConfig::default();
        let result1 = apply_env_overrides(&mut config1);

        env_guard.set(ENV_ENABLE_PARKING, repr2.value());
        let mut config2 = RuntimeConfig::default();
        let result2 = apply_env_overrides(&mut config2);

        prop_assert!(result1.is_ok() && result2.is_ok(),
            "Both boolean representations should parse successfully");

        prop_assert_eq!(config1.enable_parking, config2.enable_parking,
            "Boolean equivalence violated: '{}' vs '{}' should produce same result",
            repr1.value(), repr2.value());

        prop_assert_eq!(config1.enable_parking, repr1.expected_bool(),
            "Boolean value should match expected: got {}, expected {}",
            config1.enable_parking, repr1.expected_bool());
    });
}

/// MR3: EQUIVALENCE - Whitespace Invariance
/// Values with different whitespace should parse identically.
#[test]
fn mr_whitespace_invariance() {
    proptest!(|(base_value in 1usize..=1000, whitespace_prefix in ".{0,5}", whitespace_suffix in ".{0,5}")| {
        let base_value = base_value % 1000 + 1; // 1-1000
        let prefix = whitespace_prefix.chars().filter(|c| c.is_whitespace()).take(5).collect::<String>();
        let suffix = whitespace_suffix.chars().filter(|c| c.is_whitespace()).take(5).collect::<String>();

        let mut env_guard = EnvGuard::new();

        // Test with clean value
        env_guard.set(ENV_WORKER_THREADS, &base_value.to_string());
        let mut config1 = RuntimeConfig::default();
        let result1 = apply_env_overrides(&mut config1);

        // Test with whitespace-padded value
        let padded_value = format!("{}{}{}", prefix, base_value, suffix);
        env_guard.set(ENV_WORKER_THREADS, &padded_value);
        let mut config2 = RuntimeConfig::default();
        let result2 = apply_env_overrides(&mut config2);

        prop_assert!(result1.is_ok() && result2.is_ok(),
            "Both clean and whitespace-padded values should parse successfully");

        prop_assert_eq!(config1.worker_threads, config2.worker_threads,
            "Whitespace invariance violated: '{}' vs '{}' should produce same result",
            base_value, padded_value);
    });
}

/// MR4: EQUIVALENCE - Case Insensitive Boolean Parsing
/// Boolean values should be case-insensitive.
#[test]
fn mr_case_insensitive_boolean_parsing() {
    proptest!(|(base_bool in prop::sample::select(vec!["true", "false", "yes", "no", "on", "off", "1", "0"]).prop_map(|s| s.to_string()))| {
        let mut env_guard = EnvGuard::new();

        // Test original case
        env_guard.set(ENV_ENABLE_GOVERNOR, &base_bool);
        let mut config1 = RuntimeConfig::default();
        let result1 = apply_env_overrides(&mut config1);

        // Test uppercase
        env_guard.set(ENV_ENABLE_GOVERNOR, &base_bool.to_uppercase());
        let mut config2 = RuntimeConfig::default();
        let result2 = apply_env_overrides(&mut config2);

        // Test mixed case
        let mixed_case: String = base_bool.chars()
            .enumerate()
            .map(|(i, c)| if i % 2 == 0 { c.to_uppercase().next().unwrap_or(c) } else { c })
            .collect();
        env_guard.set(ENV_ENABLE_GOVERNOR, &mixed_case);
        let mut config3 = RuntimeConfig::default();
        let result3 = apply_env_overrides(&mut config3);

        prop_assert!(result1.is_ok() && result2.is_ok() && result3.is_ok(),
            "All case variants should parse successfully");

        prop_assert_eq!(config1.enable_governor, config2.enable_governor,
            "Case insensitivity violated: '{}' vs '{}' should produce same result",
            base_bool, base_bool.to_uppercase());

        prop_assert_eq!(config1.enable_governor, config3.enable_governor,
            "Case insensitivity violated: '{}' vs '{}' should produce same result",
            base_bool, mixed_case);
    });
}

/// MR5: EQUIVALENCE - Field Independence
/// Setting one configuration field should not affect others.
#[test]
fn mr_field_independence() {
    proptest!(|(worker_threads in 1usize..=100, poll_budget in 1u32..=1000)| {

        let mut env_guard = EnvGuard::new();

        // Set only worker_threads
        env_guard.set(ENV_WORKER_THREADS, &worker_threads.to_string());
        env_guard.unset(ENV_POLL_BUDGET);
        let mut config1 = RuntimeConfig::default();
        let baseline_poll_budget = config1.poll_budget;
        let result1 = apply_env_overrides(&mut config1);

        // Set both worker_threads and poll_budget
        env_guard.set(ENV_WORKER_THREADS, &worker_threads.to_string());
        env_guard.set(ENV_POLL_BUDGET, &poll_budget.to_string());
        let mut config2 = RuntimeConfig::default();
        let result2 = apply_env_overrides(&mut config2);

        prop_assert!(result1.is_ok() && result2.is_ok(),
            "Both configurations should parse successfully");

        prop_assert_eq!(config1.worker_threads, config2.worker_threads,
            "Worker threads should be identical when poll_budget is also set");

        prop_assert_eq!(config1.poll_budget, baseline_poll_budget,
            "Poll budget should remain at default when only worker_threads is set");

        prop_assert_eq!(config2.poll_budget, poll_budget,
            "Poll budget should be set to specified value");
    });
}

/// MR6: INCLUSIVE - Error Type Consistency
/// Invalid values should produce consistent error types.
#[test]
fn mr_error_type_consistency() {
    proptest!(|(invalid_values in prop::collection::vec("[a-z]{1,8}", 1..=5))| {
        let invalid_values: Vec<String> = invalid_values.into_iter()
            .filter(|s| !s.trim().is_empty() && s.chars().any(|c| !c.is_ascii_digit()))
            .take(5)
            .collect();

        if invalid_values.is_empty() {
            return Ok(());
        }

        let mut env_guard = EnvGuard::new();
        let mut error_types = Vec::new();

        for invalid_value in &invalid_values {
            env_guard.set(ENV_WORKER_THREADS, invalid_value);
            let mut config = RuntimeConfig::default();
            let result = apply_env_overrides(&mut config);

            prop_assert!(result.is_err(),
                "Invalid value '{}' should produce an error", invalid_value);

            if let Err(err) = result {
                error_types.push(format!("{:?}", err));
            }
        }

        // All error messages should contain the variable name and invalid value info
        for (i, error_msg) in error_types.iter().enumerate() {
            prop_assert!(error_msg.contains(ENV_WORKER_THREADS),
                "Error message should contain variable name: {}", error_msg);
            prop_assert!(error_msg.contains(&invalid_values[i]),
                "Error message should contain invalid value: {}", error_msg);
        }
    });
}

/// MR7: MULTIPLICATIVE - Precedence Override Consistency
/// Environment variables should always override defaults.
#[test]
fn mr_precedence_override_consistency() {
    proptest!(|(override_value in 1usize..=100)| {
        let mut env_guard = EnvGuard::new();

        // Get default value
        env_guard.unset(ENV_STEAL_BATCH_SIZE);
        let default_config = RuntimeConfig::default();
        let mut config1 = default_config.clone();
        let result1 = apply_env_overrides(&mut config1);
        prop_assert!(result1.is_ok(), "Default config should apply cleanly");

        // Apply environment override
        env_guard.set(ENV_STEAL_BATCH_SIZE, &override_value.to_string());
        let mut config2 = default_config.clone();
        let result2 = apply_env_overrides(&mut config2);
        prop_assert!(result2.is_ok(), "Override config should apply cleanly");

        // Override should take precedence if different from default
        if default_config.steal_batch_size != override_value {
            prop_assert_ne!(config1.steal_batch_size, config2.steal_batch_size,
                "Override should change value from default");
            prop_assert_eq!(config2.steal_batch_size, override_value,
                "Override value should be applied: expected {}, got {}",
                override_value, config2.steal_batch_size);
        }
    });
}

/// MR8: EQUIVALENCE - Boundary Value Consistency
/// Boundary values (min/max) should be handled consistently.
#[test]
fn mr_boundary_value_consistency() {
    proptest!(|(_case in Just(()))| {
        let mut env_guard = EnvGuard::new();

        // Test minimum valid values
        env_guard.set(ENV_WORKER_THREADS, "1");
        env_guard.set(ENV_BLOCKING_MIN_THREADS, "1");
        let mut config_min = RuntimeConfig::default();
        let result_min = apply_env_overrides(&mut config_min);

        // Test zero values (should be invalid for some fields)
        env_guard.set(ENV_WORKER_THREADS, "0");
        env_guard.set(ENV_BLOCKING_MIN_THREADS, "0");
        let mut config_zero = RuntimeConfig::default();
        let result_zero = apply_env_overrides(&mut config_zero);

        prop_assert!(
            result_min.is_ok(),
            "Minimum valid values should parse successfully"
        );

        // Zero worker threads should be valid (runtime decides actual count)
        // Zero blocking threads should be valid (pool handles minimum)
        prop_assert!(
            result_zero.is_ok(),
            "Zero values should be handled gracefully"
        );

        prop_assert_eq!(
            config_min.worker_threads,
            1,
            "Minimum worker threads should be 1"
        );
        prop_assert_eq!(
            config_min.blocking.min_threads,
            1,
            "Minimum blocking threads should be 1"
        );
    });
}

/// MR9: INVERTIVE - Set-Unset Round Trip
/// Setting a value then unsetting should restore default behavior.
#[test]
fn mr_set_unset_round_trip() {
    proptest!(|(test_value in 50usize..=549)| {
        let mut env_guard = EnvGuard::new();

        // Get baseline (no env var set)
        env_guard.unset(ENV_THREAD_STACK_SIZE);
        let default_config = RuntimeConfig::default();
        let mut config1 = default_config.clone();
        let result1 = apply_env_overrides(&mut config1);
        prop_assert!(result1.is_ok(), "Baseline config should apply cleanly");

        // Set environment variable
        env_guard.set(ENV_THREAD_STACK_SIZE, &test_value.to_string());
        let mut config2 = default_config.clone();
        let result2 = apply_env_overrides(&mut config2);
        prop_assert!(result2.is_ok(), "Override config should apply cleanly");

        // Unset environment variable (round trip)
        env_guard.unset(ENV_THREAD_STACK_SIZE);
        let mut config3 = default_config.clone();
        let result3 = apply_env_overrides(&mut config3);
        prop_assert!(result3.is_ok(), "Round trip config should apply cleanly");

        // Round trip should restore original state
        prop_assert_eq!(config1.thread_stack_size, config3.thread_stack_size,
            "Round trip should restore original value: baseline={}, after_roundtrip={}",
            config1.thread_stack_size, config3.thread_stack_size);

        // Middle state should be different (if test value != default)
        if default_config.thread_stack_size != test_value {
            prop_assert_ne!(config1.thread_stack_size, config2.thread_stack_size,
                "Override should change from default");
            prop_assert_eq!(config2.thread_stack_size, test_value,
                "Override should set specified value");
        }
    });
}

/// MR10: ADDITIVE - Blocking Pool Min/Max Relationship
/// Min threads should always be ≤ max threads after applying overrides.
#[test]
fn mr_blocking_pool_min_max_relationship() {
    proptest!(|(min_threads in 1usize..=20, extra_threads in 0usize..=100)| {
        let max_threads = min_threads + extra_threads;
        let mut env_guard = EnvGuard::new();

        env_guard.set(ENV_BLOCKING_MIN_THREADS, &min_threads.to_string());
        env_guard.set(ENV_BLOCKING_MAX_THREADS, &max_threads.to_string());

        let mut config = RuntimeConfig::default();
        let result = apply_env_overrides(&mut config);

        prop_assert!(result.is_ok(),
            "Valid min/max configuration should apply successfully");

        prop_assert!(config.blocking.min_threads <= config.blocking.max_threads,
            "Min threads {} should be ≤ max threads {} after applying overrides",
            config.blocking.min_threads, config.blocking.max_threads);

        prop_assert_eq!(config.blocking.min_threads, min_threads,
            "Min threads should be set to specified value");
        prop_assert_eq!(config.blocking.max_threads, max_threads,
            "Max threads should be set to specified value");
    });
}

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

    /// Composite MR: Independence + Precedence + Consistency
    /// Tests that field independence, precedence override, and default consistency
    /// all hold simultaneously under complex configuration scenarios.
    #[test]
    fn mr_composite_config_invariants() {
        proptest!(|(operations in prop::collection::vec(arb_config_operation(), 0..=20))| {
            let mut env_guard = EnvGuard::new();

            // Start with clean environment
            for var_name in &[ENV_WORKER_THREADS, ENV_ENABLE_PARKING, ENV_POLL_BUDGET] {
                env_guard.unset(var_name);
            }

            let baseline_config = RuntimeConfig::default();
            let baseline_snapshot = ConfigSnapshot::capture(&baseline_config);

            for op in operations.iter().take(5) {
                match op {
                    ConfigOperation::SetEnvVar { var } => {
                        env_guard.set(&var.name, &var.value);
                    }
                    ConfigOperation::UnsetEnvVar { name } => {
                        env_guard.unset(name);
                    }
                    _ => {} // Skip non-env operations for this test
                }

                let mut config = RuntimeConfig::default();
                let result = apply_env_overrides(&mut config);

                if result.is_ok() {
                    let snapshot = ConfigSnapshot::capture(&config);

                    // MR5: Field independence - unset fields should remain at defaults
                    if env::var(ENV_WORKER_THREADS).is_err() {
                        prop_assert_eq!(snapshot.worker_threads, baseline_snapshot.worker_threads,
                            "Worker threads should remain at default when not set");
                    }

                    if env::var(ENV_ENABLE_PARKING).is_err() {
                        prop_assert_eq!(snapshot.enable_parking, baseline_snapshot.enable_parking,
                            "Parking should remain at default when not set");
                    }

                    // MR7: Precedence - env vars should override defaults when set
                    if let Ok(val) = env::var(ENV_POLL_BUDGET) {
                        if let Ok(parsed_val) = val.parse::<u32>() {
                            prop_assert_eq!(snapshot.poll_budget, parsed_val,
                                "Poll budget should be overridden by env var");
                        }
                    }

                    // MR10: Min/max relationship should always hold
                    prop_assert!(snapshot.blocking_min_threads <= snapshot.blocking_max_threads,
                        "Blocking pool min/max relationship violated");
                }
            }
        });
    }
}