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
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
//! Environment variable and config file support for [`RuntimeBuilder`](super::builder::RuntimeBuilder).
//!
//! # Configuration Precedence
//!
//! Settings are resolved in this order (highest priority first):
//!
//! 1. **Programmatic** — values set via builder methods (`worker_threads(4)`)
//! 2. **Environment variables** — values from `ASUPERSYNC_*` env vars
//! 3. **Config file** — values loaded from a TOML file (requires `config-file` feature)
//! 4. **Defaults** — built-in defaults from [`RuntimeConfig::default()`]
//!
//! # Supported Environment Variables
//!
//! | Variable | Type | Maps to |
//! |----------|------|---------|
//! | `ASUPERSYNC_WORKER_THREADS` | `usize` | `worker_threads` |
//! | `ASUPERSYNC_TASK_QUEUE_DEPTH` | `usize` | `global_queue_limit` |
//! | `ASUPERSYNC_THREAD_STACK_SIZE` | `usize` | `thread_stack_size` |
//! | `ASUPERSYNC_THREAD_NAME_PREFIX` | `String` | `thread_name_prefix` |
//! | `ASUPERSYNC_STEAL_BATCH_SIZE` | `usize` | `steal_batch_size` |
//! | `ASUPERSYNC_BLOCKING_MIN_THREADS` | `usize` | `blocking.min_threads` |
//! | `ASUPERSYNC_BLOCKING_MAX_THREADS` | `usize` | `blocking.max_threads` |
//! | `ASUPERSYNC_ENABLE_PARKING` | `bool` | `enable_parking` |
//! | `ASUPERSYNC_POLL_BUDGET` | `u32` | `poll_budget` |
//! | `ASUPERSYNC_CANCEL_LANE_MAX_STREAK` | `usize` | `cancel_lane_max_streak` |
//! | `ASUPERSYNC_ENABLE_GOVERNOR` | `bool` | `enable_governor` |
//! | `ASUPERSYNC_GOVERNOR_INTERVAL` | `u32` | `governor_interval` |
//! | `ASUPERSYNC_ENABLE_ADAPTIVE_CANCEL_STREAK` | `bool` | `enable_adaptive_cancel_streak` |
//! | `ASUPERSYNC_ADAPTIVE_CANCEL_EPOCH_STEPS` | `u32` | `adaptive_cancel_streak_epoch_steps` |

use crate::runtime::config::RuntimeConfig;
use crate::types::builder::BuildError;
use std::collections::HashMap;

/// Environment variable capability interface for controlled environment access.
///
/// This abstracts environment variable access to prevent ambient authority violations
/// during runtime initialization.
trait EnvCapability {
    /// Read an environment variable, returning `None` if unset.
    fn read_env(&self, name: &str) -> Option<String>;
}

/// File capability interface for synchronous file operations.
///
/// This abstracts file system access to prevent ambient authority violations
/// during runtime initialization.
trait FileCapability {
    /// Read file contents as a string.
    fn read_to_string(&self, path: &std::path::Path) -> Result<String, std::io::Error>;
}

/// Production environment variable capability that provides controlled access to the environment.
///
/// Unlike direct `std::env` calls, this capability can be audited and controlled.
struct ProductionEnvCapability;

impl EnvCapability for ProductionEnvCapability {
    fn read_env(&self, name: &str) -> Option<String> {
        // Use std::env here, but mediated through the capability interface.
        // This allows for future enhancement (e.g., sandboxing, auditing) without
        // changing the ambient authority pattern.
        std::env::var(name).ok()
    }
}

/// Production file capability that provides controlled access to the file system.
///
/// Unlike direct `std::fs` calls, this capability can be audited and controlled.
struct ProductionFileCapability;

impl FileCapability for ProductionFileCapability {
    fn read_to_string(&self, path: &std::path::Path) -> Result<String, std::io::Error> {
        // Use std::fs here, but mediated through the capability interface.
        // This allows for future enhancement (e.g., sandboxing, auditing) without
        // changing the ambient authority pattern.
        std::fs::read_to_string(path)
    }
}

/// Abstracts environment variable access to prevent ambient authority violations.
///
/// This trait allows environment variable reading to be dependency-injected,
/// enabling deterministic environment tests and preventing direct ambient
/// access to the process environment.
pub trait EnvReader {
    /// Read an environment variable, returning `None` if unset.
    fn read_env(&self, name: &str) -> Option<String>;

    /// Read a file, returning an error if it fails.
    /// This is needed for TOML config file reading.
    fn read_file(
        &self,
        path: &std::path::Path,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>>;
}

/// Production implementation that reads from the actual process environment.
///
/// Both environment variable and file access are mediated through capability
/// interfaces to prevent ambient authority violations.
pub struct SystemEnvReader {
    env_cap: ProductionEnvCapability,
    file_cap: ProductionFileCapability,
}

impl SystemEnvReader {
    /// Create a new system environment reader with production capabilities.
    pub fn new() -> Self {
        Self {
            env_cap: ProductionEnvCapability,
            file_cap: ProductionFileCapability,
        }
    }
}

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

impl EnvReader for SystemEnvReader {
    fn read_env(&self, name: &str) -> Option<String> {
        self.env_cap.read_env(name)
    }

    fn read_file(
        &self,
        path: &std::path::Path,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        // Use the file capability instead of direct std::fs access
        self.file_cap
            .read_to_string(path)
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
    }
}

/// Test implementation that reads from a provided map.
pub struct TestEnvReader {
    env_vars: HashMap<String, String>,
    files: HashMap<std::path::PathBuf, String>,
}

impl TestEnvReader {
    /// Create a new test environment reader with the given environment variables.
    pub fn new(env_vars: HashMap<String, String>) -> Self {
        Self {
            env_vars,
            files: HashMap::new(),
        }
    }

    /// Add a file that can be read by this reader.
    pub fn with_file(mut self, path: impl Into<std::path::PathBuf>, content: String) -> Self {
        self.files.insert(path.into(), content);
        self
    }
}

impl EnvReader for TestEnvReader {
    fn read_env(&self, name: &str) -> Option<String> {
        self.env_vars.get(name).cloned()
    }

    fn read_file(
        &self,
        path: &std::path::Path,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        self.files.get(path).cloned().ok_or_else(|| {
            let msg = format!("Test file not found: {}", path.display());
            Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, msg))
                as Box<dyn std::error::Error + Send + Sync>
        })
    }
}

/// Environment variable name for worker thread count.
pub const ENV_WORKER_THREADS: &str = "ASUPERSYNC_WORKER_THREADS";
/// Environment variable name for task queue depth (global queue limit).
pub const ENV_TASK_QUEUE_DEPTH: &str = "ASUPERSYNC_TASK_QUEUE_DEPTH";
/// Environment variable name for thread stack size.
pub const ENV_THREAD_STACK_SIZE: &str = "ASUPERSYNC_THREAD_STACK_SIZE";
/// Environment variable name for thread name prefix.
pub const ENV_THREAD_NAME_PREFIX: &str = "ASUPERSYNC_THREAD_NAME_PREFIX";
/// Environment variable name for work-stealing batch size.
pub const ENV_STEAL_BATCH_SIZE: &str = "ASUPERSYNC_STEAL_BATCH_SIZE";
/// Environment variable name for blocking pool minimum threads.
pub const ENV_BLOCKING_MIN_THREADS: &str = "ASUPERSYNC_BLOCKING_MIN_THREADS";
/// Environment variable name for blocking pool maximum threads.
pub const ENV_BLOCKING_MAX_THREADS: &str = "ASUPERSYNC_BLOCKING_MAX_THREADS";
/// Environment variable name for idle-worker parking toggle.
pub const ENV_ENABLE_PARKING: &str = "ASUPERSYNC_ENABLE_PARKING";
/// Environment variable name for cooperative poll budget.
pub const ENV_POLL_BUDGET: &str = "ASUPERSYNC_POLL_BUDGET";
/// Environment variable name for max consecutive cancel dispatches.
pub const ENV_CANCEL_LANE_MAX_STREAK: &str = "ASUPERSYNC_CANCEL_LANE_MAX_STREAK";
/// Environment variable name for enabling the Lyapunov governor.
pub const ENV_ENABLE_GOVERNOR: &str = "ASUPERSYNC_ENABLE_GOVERNOR";
/// Environment variable name for governor snapshot interval (scheduling steps).
pub const ENV_GOVERNOR_INTERVAL: &str = "ASUPERSYNC_GOVERNOR_INTERVAL";
/// Environment variable name for adaptive cancel-streak scheduling.
pub const ENV_ENABLE_ADAPTIVE_CANCEL_STREAK: &str = "ASUPERSYNC_ENABLE_ADAPTIVE_CANCEL_STREAK";
/// Environment variable name for adaptive cancel-streak epoch length.
pub const ENV_ADAPTIVE_CANCEL_EPOCH_STEPS: &str = "ASUPERSYNC_ADAPTIVE_CANCEL_EPOCH_STEPS";

/// Apply environment variable overrides to a [`RuntimeConfig`].
///
/// Only variables that are set in the environment are applied.
/// Returns an error if a variable is set but contains an unparseable value.
pub fn apply_env_overrides(
    config: &mut RuntimeConfig,
    env_reader: &dyn EnvReader,
) -> Result<(), BuildError> {
    if let Some(val) = env_reader.read_env(ENV_WORKER_THREADS) {
        config.worker_threads = parse_usize(ENV_WORKER_THREADS, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_TASK_QUEUE_DEPTH) {
        config.global_queue_limit = parse_usize(ENV_TASK_QUEUE_DEPTH, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_THREAD_STACK_SIZE) {
        config.thread_stack_size = parse_usize(ENV_THREAD_STACK_SIZE, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_THREAD_NAME_PREFIX) {
        config.thread_name_prefix = val;
    }
    if let Some(val) = env_reader.read_env(ENV_STEAL_BATCH_SIZE) {
        config.steal_batch_size = parse_usize(ENV_STEAL_BATCH_SIZE, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_BLOCKING_MIN_THREADS) {
        config.blocking.min_threads = parse_usize(ENV_BLOCKING_MIN_THREADS, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_BLOCKING_MAX_THREADS) {
        config.blocking.max_threads = parse_usize(ENV_BLOCKING_MAX_THREADS, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_ENABLE_PARKING) {
        config.enable_parking = parse_bool(ENV_ENABLE_PARKING, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_POLL_BUDGET) {
        config.poll_budget = parse_u32(ENV_POLL_BUDGET, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_CANCEL_LANE_MAX_STREAK) {
        config.cancel_lane_max_streak = parse_usize(ENV_CANCEL_LANE_MAX_STREAK, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_ENABLE_GOVERNOR) {
        config.enable_governor = parse_bool(ENV_ENABLE_GOVERNOR, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_GOVERNOR_INTERVAL) {
        config.governor_interval = parse_u32(ENV_GOVERNOR_INTERVAL, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_ENABLE_ADAPTIVE_CANCEL_STREAK) {
        config.enable_adaptive_cancel_streak = parse_bool(ENV_ENABLE_ADAPTIVE_CANCEL_STREAK, &val)?;
    }
    if let Some(val) = env_reader.read_env(ENV_ADAPTIVE_CANCEL_EPOCH_STEPS) {
        config.adaptive_cancel_streak_epoch_steps =
            parse_u32(ENV_ADAPTIVE_CANCEL_EPOCH_STEPS, &val)?;
    }
    Ok(())
}

fn parse_usize(var_name: &str, val: &str) -> Result<usize, BuildError> {
    val.trim().parse::<usize>().map_err(|e| {
        BuildError::custom(format!(
            "invalid value for {var_name}: expected unsigned integer, got {val:?} ({e})"
        ))
    })
}

fn parse_u32(var_name: &str, val: &str) -> Result<u32, BuildError> {
    val.trim().parse::<u32>().map_err(|e| {
        BuildError::custom(format!(
            "invalid value for {var_name}: expected u32, got {val:?} ({e})"
        ))
    })
}

fn parse_bool(var_name: &str, val: &str) -> Result<bool, BuildError> {
    match val.trim().to_lowercase().as_str() {
        "true" | "1" | "yes" | "on" => Ok(true),
        "false" | "0" | "no" | "off" => Ok(false),
        _ => Err(BuildError::custom(format!(
            "invalid value for {var_name}: expected bool (true/false/1/0/yes/no), got {val:?}"
        ))),
    }
}

// =========================================================================
// TOML config file support (feature-gated)
// =========================================================================

/// TOML-deserializable runtime configuration.
///
/// This struct mirrors the fields of [`RuntimeConfig`] in a flat,
/// serialization-friendly layout. Fields are grouped into TOML tables:
///
/// ```toml
/// [scheduler]
/// worker_threads = 4
/// task_queue_depth = 1024
/// steal_batch_size = 16
/// poll_budget = 128
/// cancel_lane_max_streak = 16
/// enable_governor = true
/// governor_interval = 64
/// enable_adaptive_cancel_streak = true
/// adaptive_cancel_streak_epoch_steps = 128
/// enable_parking = true
/// thread_stack_size = 2097152
/// thread_name_prefix = "myapp-worker"
///
/// [blocking]
/// min_threads = 1
/// max_threads = 512
/// ```
#[cfg(feature = "config-file")]
#[derive(serde::Deserialize, Default, Debug)]
pub struct RuntimeTomlConfig {
    /// Scheduler settings.
    #[serde(default)]
    pub scheduler: SchedulerToml,
    /// Blocking pool settings.
    #[serde(default)]
    pub blocking: BlockingToml,
}

/// Scheduler section of the TOML config.
#[cfg(feature = "config-file")]
#[derive(serde::Deserialize, Default, Debug)]
pub struct SchedulerToml {
    /// Number of worker threads.
    pub worker_threads: Option<usize>,
    /// Global task queue depth (0 = unbounded).
    pub task_queue_depth: Option<usize>,
    /// Work-stealing batch size.
    pub steal_batch_size: Option<usize>,
    /// Cooperative poll budget.
    pub poll_budget: Option<u32>,
    /// Maximum consecutive cancel-lane dispatches before yielding.
    pub cancel_lane_max_streak: Option<usize>,
    /// Enable the Lyapunov governor.
    pub enable_governor: Option<bool>,
    /// Scheduling steps between governor snapshots.
    pub governor_interval: Option<u32>,
    /// Enable adaptive cancel-streak selection.
    pub enable_adaptive_cancel_streak: Option<bool>,
    /// Dispatches per adaptive cancel-streak epoch.
    pub adaptive_cancel_streak_epoch_steps: Option<u32>,
    /// Enable parking for idle workers.
    pub enable_parking: Option<bool>,
    /// Stack size per worker thread in bytes.
    pub thread_stack_size: Option<usize>,
    /// Name prefix for worker threads.
    pub thread_name_prefix: Option<String>,
}

/// Blocking pool section of the TOML config.
#[cfg(feature = "config-file")]
#[derive(serde::Deserialize, Default, Debug)]
pub struct BlockingToml {
    /// Minimum number of blocking threads.
    pub min_threads: Option<usize>,
    /// Maximum number of blocking threads.
    pub max_threads: Option<usize>,
}

/// Apply a parsed TOML config to a [`RuntimeConfig`].
///
/// Only fields that are `Some` in the TOML struct override the config.
#[cfg(feature = "config-file")]
pub fn apply_toml_config(config: &mut RuntimeConfig, toml: &RuntimeTomlConfig) {
    if let Some(v) = toml.scheduler.worker_threads {
        config.worker_threads = v;
    }
    if let Some(v) = toml.scheduler.task_queue_depth {
        config.global_queue_limit = v;
    }
    if let Some(v) = toml.scheduler.steal_batch_size {
        config.steal_batch_size = v;
    }
    if let Some(v) = toml.scheduler.poll_budget {
        config.poll_budget = v;
    }
    if let Some(v) = toml.scheduler.cancel_lane_max_streak {
        config.cancel_lane_max_streak = v;
    }
    if let Some(v) = toml.scheduler.enable_governor {
        config.enable_governor = v;
    }
    if let Some(v) = toml.scheduler.governor_interval {
        config.governor_interval = v;
    }
    if let Some(v) = toml.scheduler.enable_adaptive_cancel_streak {
        config.enable_adaptive_cancel_streak = v;
    }
    if let Some(v) = toml.scheduler.adaptive_cancel_streak_epoch_steps {
        config.adaptive_cancel_streak_epoch_steps = v;
    }
    if let Some(v) = toml.scheduler.enable_parking {
        config.enable_parking = v;
    }
    if let Some(v) = toml.scheduler.thread_stack_size {
        config.thread_stack_size = v;
    }
    if let Some(ref v) = toml.scheduler.thread_name_prefix {
        config.thread_name_prefix.clone_from(v);
    }
    if let Some(v) = toml.blocking.min_threads {
        config.blocking.min_threads = v;
    }
    if let Some(v) = toml.blocking.max_threads {
        config.blocking.max_threads = v;
    }
}

/// Parse a TOML string into a [`RuntimeTomlConfig`].
#[cfg(feature = "config-file")]
pub fn parse_toml_str(toml_str: &str) -> Result<RuntimeTomlConfig, BuildError> {
    toml::from_str(toml_str)
        .map_err(|e| BuildError::custom(format!("failed to parse TOML config: {e}")))
}

/// Read and parse a TOML file into a [`RuntimeTomlConfig`].
#[cfg(feature = "config-file")]
pub fn parse_toml_file(
    path: &std::path::Path,
    env_reader: &dyn EnvReader,
) -> Result<RuntimeTomlConfig, BuildError> {
    let content = env_reader.read_file(path).map_err(|e| {
        BuildError::custom(format!(
            "failed to read config file {}: {e}",
            path.display()
        ))
    })?;
    parse_toml_str(&content)
}

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

#[cfg(test)]
#[allow(unsafe_code)]
mod tests {
    use super::*;
    use crate::runtime::config::RuntimeConfig;

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

    fn with_clean_env<F, R>(f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let _guard = crate::test_utils::env_lock();
        clean_env_locked();
        f()
    }

    // Helper: set env var for the duration of a closure, then unset.
    fn with_env<F, R>(var: &str, val: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        with_clean_env(|| {
            // SAFETY: test helpers guard environment mutation with env_lock.
            unsafe { std::env::set_var(var, val) };
            let result = f();
            // SAFETY: test helpers guard environment mutation with env_lock.
            unsafe { std::env::remove_var(var) };
            result
        })
    }

    fn with_envs<F, R>(vars: &[(&str, &str)], f: F) -> R
    where
        F: FnOnce() -> R,
    {
        with_clean_env(|| {
            for (k, v) in vars {
                // SAFETY: test helpers guard environment mutation with env_lock.
                unsafe { std::env::set_var(k, v) };
            }
            let result = f();
            for (k, _) in vars {
                // SAFETY: test helpers guard environment mutation with env_lock.
                unsafe { std::env::remove_var(k) };
            }
            result
        })
    }

    fn clean_env_locked() {
        for var 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_ENABLE_ADAPTIVE_CANCEL_STREAK,
            ENV_ADAPTIVE_CANCEL_EPOCH_STEPS,
        ] {
            // SAFETY: test helpers guard environment mutation with env_lock.
            unsafe { std::env::remove_var(var) };
        }
    }

    // --- parse helpers ---

    #[test]
    fn parse_usize_valid() {
        assert_eq!(
            super::parse_usize("TEST", "42").expect("should parse valid usize '42'"),
            42
        );
        assert_eq!(
            super::parse_usize("TEST", " 100 ")
                .expect("should parse valid usize ' 100 ' with whitespace"),
            100
        );
        assert_eq!(
            super::parse_usize("TEST", "0").expect("should parse valid usize '0'"),
            0
        );
    }

    #[test]
    fn parse_usize_invalid() {
        assert!(super::parse_usize("TEST", "abc").is_err());
        assert!(super::parse_usize("TEST", "-1").is_err());
        assert!(super::parse_usize("TEST", "3.14").is_err());
        assert!(super::parse_usize("TEST", "").is_err());
    }

    #[test]
    fn parse_u32_valid() {
        assert_eq!(
            super::parse_u32("TEST", "128").expect("should parse valid u32 '128'"),
            128
        );
    }

    #[test]
    fn parse_u32_invalid() {
        assert!(super::parse_u32("TEST", "not_a_number").is_err());
    }

    #[test]
    fn parse_bool_all_truthy() {
        for val in &["true", "1", "yes", "on", "TRUE", "Yes", "ON"] {
            assert!(
                super::parse_bool("TEST", val)
                    .unwrap_or_else(|_| panic!("should parse truthy value '{}'", val)),
                "expected true for {val}"
            );
        }
    }

    #[test]
    fn parse_bool_all_falsy() {
        for val in &["false", "0", "no", "off", "FALSE", "No", "OFF"] {
            assert!(
                !super::parse_bool("TEST", val)
                    .unwrap_or_else(|_| panic!("should parse falsy value '{}'", val)),
                "expected false for {val}"
            );
        }
    }

    #[test]
    fn parse_bool_invalid() {
        assert!(super::parse_bool("TEST", "maybe").is_err());
        assert!(super::parse_bool("TEST", "2").is_err());
        assert!(super::parse_bool("TEST", "").is_err());
    }

    // --- apply_env_overrides ---

    #[test]
    fn env_overrides_worker_threads() {
        with_env(ENV_WORKER_THREADS, "8", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).expect("should apply worker_threads env override");
            assert_eq!(config.worker_threads, 8);
        });
    }

    #[test]
    fn env_overrides_task_queue_depth() {
        with_env(ENV_TASK_QUEUE_DEPTH, "2048", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).expect("should apply task_queue_depth env override");
            assert_eq!(config.global_queue_limit, 2048);
        });
    }

    #[test]
    fn env_overrides_thread_stack_size() {
        with_env(ENV_THREAD_STACK_SIZE, "4194304", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).expect("should apply thread_stack_size env override");
            assert_eq!(config.thread_stack_size, 4_194_304);
        });
    }

    #[test]
    fn env_overrides_thread_name_prefix() {
        with_env(ENV_THREAD_NAME_PREFIX, "myapp-worker", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).expect("should apply thread_name_prefix env override");
            assert_eq!(config.thread_name_prefix, "myapp-worker");
        });
    }

    #[test]
    fn env_overrides_steal_batch_size() {
        with_env(ENV_STEAL_BATCH_SIZE, "32", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).unwrap();
            assert_eq!(config.steal_batch_size, 32);
        });
    }

    #[test]
    fn env_overrides_blocking_threads() {
        with_envs(
            &[
                (ENV_BLOCKING_MIN_THREADS, "2"),
                (ENV_BLOCKING_MAX_THREADS, "16"),
            ],
            || {
                let mut config = RuntimeConfig::default();
                apply_env_overrides(&mut config).unwrap();
                assert_eq!(config.blocking.min_threads, 2);
                assert_eq!(config.blocking.max_threads, 16);
            },
        );
    }

    #[test]
    fn env_overrides_enable_parking() {
        with_env(ENV_ENABLE_PARKING, "false", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).unwrap();
            assert!(!config.enable_parking);
        });
    }

    #[test]
    fn env_overrides_poll_budget() {
        with_env(ENV_POLL_BUDGET, "64", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).unwrap();
            assert_eq!(config.poll_budget, 64);
        });
    }

    #[test]
    fn env_overrides_cancel_lane_max_streak() {
        with_env(ENV_CANCEL_LANE_MAX_STREAK, "7", || {
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).unwrap();
            assert_eq!(config.cancel_lane_max_streak, 7);
        });
    }

    #[test]
    fn env_overrides_governor_settings() {
        with_envs(
            &[(ENV_ENABLE_GOVERNOR, "true"), (ENV_GOVERNOR_INTERVAL, "41")],
            || {
                let mut config = RuntimeConfig::default();
                apply_env_overrides(&mut config).unwrap();
                assert!(config.enable_governor);
                assert_eq!(config.governor_interval, 41);
            },
        );
    }

    #[test]
    fn env_overrides_adaptive_cancel_settings() {
        with_envs(
            &[
                (ENV_ENABLE_ADAPTIVE_CANCEL_STREAK, "true"),
                (ENV_ADAPTIVE_CANCEL_EPOCH_STEPS, "77"),
            ],
            || {
                let mut config = RuntimeConfig::default();
                apply_env_overrides(&mut config).unwrap();
                assert!(config.enable_adaptive_cancel_streak);
                assert_eq!(config.adaptive_cancel_streak_epoch_steps, 77);
            },
        );
    }

    #[test]
    fn env_overrides_multiple() {
        with_envs(
            &[
                (ENV_WORKER_THREADS, "4"),
                (ENV_POLL_BUDGET, "256"),
                (ENV_ENABLE_PARKING, "no"),
            ],
            || {
                let mut config = RuntimeConfig::default();
                apply_env_overrides(&mut config).unwrap();
                assert_eq!(config.worker_threads, 4);
                assert_eq!(config.poll_budget, 256);
                assert!(!config.enable_parking);
            },
        );
    }

    #[test]
    fn env_overrides_unset_vars_leave_defaults() {
        with_clean_env(|| {
            let defaults = RuntimeConfig::default();
            let mut config = RuntimeConfig::default();
            apply_env_overrides(&mut config).unwrap();
            assert_eq!(config.worker_threads, defaults.worker_threads);
            assert_eq!(config.poll_budget, defaults.poll_budget);
            assert_eq!(config.enable_parking, defaults.enable_parking);
        });
    }

    #[test]
    fn env_overrides_invalid_value_returns_error() {
        with_env(ENV_WORKER_THREADS, "not_a_number", || {
            let mut config = RuntimeConfig::default();
            let result = apply_env_overrides(&mut config);
            assert!(result.is_err());
            let err = result.unwrap_err();
            let msg = err.to_string();
            assert!(
                msg.contains(ENV_WORKER_THREADS),
                "error should mention var name: {msg}"
            );
            assert!(
                msg.contains("not_a_number"),
                "error should mention bad value: {msg}"
            );
        });
    }

    #[test]
    fn clean_env_locked_removes_governor_related_vars() {
        let _guard = crate::test_utils::env_lock();
        // SAFETY: this test holds env_lock for exclusive process-wide env mutation.
        unsafe {
            std::env::set_var(ENV_CANCEL_LANE_MAX_STREAK, "99");
            std::env::set_var(ENV_ENABLE_GOVERNOR, "true");
            std::env::set_var(ENV_GOVERNOR_INTERVAL, "123");
            std::env::set_var(ENV_ENABLE_ADAPTIVE_CANCEL_STREAK, "true");
            std::env::set_var(ENV_ADAPTIVE_CANCEL_EPOCH_STEPS, "77");
        }

        clean_env_locked();

        assert!(std::env::var(ENV_CANCEL_LANE_MAX_STREAK).is_err());
        assert!(std::env::var(ENV_ENABLE_GOVERNOR).is_err());
        assert!(std::env::var(ENV_GOVERNOR_INTERVAL).is_err());
        assert!(std::env::var(ENV_ENABLE_ADAPTIVE_CANCEL_STREAK).is_err());
        assert!(std::env::var(ENV_ADAPTIVE_CANCEL_EPOCH_STEPS).is_err());
    }

    #[test]
    fn env_overrides_invalid_bool_returns_error() {
        with_env(ENV_ENABLE_PARKING, "maybe", || {
            let mut config = RuntimeConfig::default();
            let result = apply_env_overrides(&mut config);
            assert!(result.is_err());
            let msg = result.unwrap_err().to_string();
            assert!(msg.contains("maybe"));
        });
    }
}

#[cfg(all(test, feature = "config-file"))]
mod toml_tests {
    use super::*;
    use crate::runtime::config::RuntimeConfig;

    #[test]
    fn parse_toml_full_config() {
        let toml_str = r#"
[scheduler]
worker_threads = 8
task_queue_depth = 4096
steal_batch_size = 32
poll_budget = 256
enable_governor = true
governor_interval = 48
enable_adaptive_cancel_streak = true
adaptive_cancel_streak_epoch_steps = 96
enable_parking = false
thread_stack_size = 4194304
thread_name_prefix = "myapp"

[blocking]
min_threads = 2
max_threads = 64
"#;
        let parsed = parse_toml_str(toml_str).unwrap();
        assert_eq!(parsed.scheduler.worker_threads, Some(8));
        assert_eq!(parsed.scheduler.task_queue_depth, Some(4096));
        assert_eq!(parsed.scheduler.steal_batch_size, Some(32));
        assert_eq!(parsed.scheduler.poll_budget, Some(256));
        assert_eq!(parsed.scheduler.enable_governor, Some(true));
        assert_eq!(parsed.scheduler.governor_interval, Some(48));
        assert_eq!(parsed.scheduler.enable_adaptive_cancel_streak, Some(true));
        assert_eq!(
            parsed.scheduler.adaptive_cancel_streak_epoch_steps,
            Some(96)
        );
        assert_eq!(parsed.scheduler.enable_parking, Some(false));
        assert_eq!(parsed.scheduler.thread_stack_size, Some(4_194_304));
        assert_eq!(
            parsed.scheduler.thread_name_prefix.as_deref(),
            Some("myapp")
        );
        assert_eq!(parsed.blocking.min_threads, Some(2));
        assert_eq!(parsed.blocking.max_threads, Some(64));
    }

    #[test]
    fn parse_toml_partial_config() {
        let toml_str = r"
[scheduler]
worker_threads = 4
";
        let parsed = parse_toml_str(toml_str).unwrap();
        assert_eq!(parsed.scheduler.worker_threads, Some(4));
        assert_eq!(parsed.scheduler.poll_budget, None);
        assert_eq!(parsed.blocking.min_threads, None);
    }

    #[test]
    fn parse_toml_empty_config() {
        let parsed = parse_toml_str("").unwrap();
        assert_eq!(parsed.scheduler.worker_threads, None);
        assert_eq!(parsed.blocking.min_threads, None);
    }

    #[test]
    fn parse_toml_invalid_syntax() {
        let result = parse_toml_str("not valid toml {{{{");
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("TOML"));
    }

    #[test]
    fn parse_toml_wrong_type() {
        let result = parse_toml_str(
            r#"
[scheduler]
worker_threads = "not_a_number"
"#,
        );
        assert!(result.is_err());
    }

    #[test]
    fn apply_toml_overrides_config() {
        let toml_str = r"
[scheduler]
worker_threads = 16
poll_budget = 512
enable_governor = true
governor_interval = 80
enable_adaptive_cancel_streak = true
adaptive_cancel_streak_epoch_steps = 64

[blocking]
max_threads = 128
";
        let parsed = parse_toml_str(toml_str).unwrap();
        let mut config = RuntimeConfig::default();
        apply_toml_config(&mut config, &parsed);

        assert_eq!(config.worker_threads, 16);
        assert_eq!(config.poll_budget, 512);
        assert!(config.enable_governor);
        assert_eq!(config.governor_interval, 80);
        assert!(config.enable_adaptive_cancel_streak);
        assert_eq!(config.adaptive_cancel_streak_epoch_steps, 64);
        assert_eq!(config.blocking.max_threads, 128);
        // Unset fields remain at defaults.
        assert_eq!(
            config.steal_batch_size,
            RuntimeConfig::default().steal_batch_size
        );
    }

    #[test]
    fn toml_file_not_found() {
        let result = parse_toml_file(
            std::path::Path::new("/nonexistent/config.toml"),
            &SystemEnvReader::new(),
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("failed to read"));
    }

    #[test]
    fn toml_file_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("runtime.toml");
        std::fs::write(
            &path,
            r"
[scheduler]
worker_threads = 2
poll_budget = 64
",
        )
        .unwrap();

        let parsed = parse_toml_file(&path, &SystemEnvReader::new()).unwrap();
        let mut config = RuntimeConfig::default();
        apply_toml_config(&mut config, &parsed);
        assert_eq!(config.worker_threads, 2);
        assert_eq!(config.poll_budget, 64);
    }
}

#[cfg(test)]
#[path = "env_config_metamorphic_tests.rs"]
mod metamorphic_tests;