allot-shared 0.3.0

Internal to Allot, no stable API: protocol, config, and path types
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
use std::{collections::BTreeMap, fs, io, path::Path};

use serde::{Deserialize, Deserializer, Serialize, de};

use crate::{
    Error, Result,
    types::{JobClass, JobProfile},
};

/// Daemon configuration, read from `config.toml`.
///
/// Every value has a default, so a user file only lists what it changes. Tables merge field
/// by field over the defaults; arrays such as `classifier.rules` replace the default list as
/// a whole. Unknown keys are rejected so that typos surface instead of being ignored.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    /// HTTP listener settings.
    pub daemon: DaemonConfig,
    /// How much of the machine the admission engine keeps free.
    pub admission: AdmissionConfig,
    /// When the machine counts as quiet enough for a benchmark.
    pub benchmark: BenchmarkConfig,
    /// Lease lifetime settings.
    pub leases: LeaseConfig,
    /// How long ended leases and job history are kept.
    pub retention: RetentionConfig,
    /// PreToolUse hook behavior.
    pub hook: HookConfig,
    /// The profile behind each [`JobClass`].
    pub classes: ClassProfiles,
    /// Named profiles sized between the classes, chosen by name like a class.
    pub presets: Presets,
    /// Which shell commands count as heavy.
    pub classifier: ClassifierConfig,
    /// How leases use cgroups on Linux; ignored on other OSes.
    pub linux: LinuxConfig,
}

impl Config {
    /// Parses and validates a TOML document. Missing values take their defaults.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ConfigParse`] for malformed TOML, unknown keys, or wrongly typed
    /// values, and [`Error::Config`] for values out of range.
    pub fn from_toml(text: &str) -> Result<Self> {
        let config: Self = toml::from_str(text)?;
        config.validate()?;
        Ok(config)
    }

    /// Loads the file at `path`, or the defaults when the file does not exist.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ConfigFile`] naming `path` when the file exists but cannot be read,
    /// or holds what [`Config::from_toml`] rejects.
    pub fn load(path: &Path) -> Result<Self> {
        let loaded = match fs::read_to_string(path) {
            Ok(text) => Self::from_toml(&text),
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Self::default()),
            Err(error) => Err(error.into()),
        };
        loaded.map_err(|reason| Error::ConfigFile {
            path: path.to_owned(),
            reason: Box::new(reason),
        })
    }

    /// Checks every value against its allowed range.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Config`] naming the first invalid value.
    pub fn validate(&self) -> Result<()> {
        ensure(self.daemon.port != 0, "daemon.port must not be 0")?;
        ensure(
            (0.0..100.0).contains(&self.admission.headroom_pct),
            "admission.headroom_pct must be at least 0 and below 100",
        )?;
        ensure(
            is_pct(self.benchmark.quiet_cpu_pct),
            "benchmark.quiet_cpu_pct must be 0 to 100",
        )?;
        ensure(
            is_pct(self.benchmark.quiet_gpu_pct),
            "benchmark.quiet_gpu_pct must be 0 to 100",
        )?;
        ensure(
            self.benchmark.quiet_secs > 0,
            "benchmark.quiet_secs must be positive",
        )?;
        ensure(
            self.benchmark.max_quiet_wait_secs >= self.benchmark.quiet_secs,
            "benchmark.max_quiet_wait_secs must be at least quiet_secs",
        )?;
        ensure(
            self.leases.heartbeat_ttl_secs > 0,
            "leases.heartbeat_ttl_secs must be positive",
        )?;
        let retention = &self.retention;
        ensure(
            retention.lease_days > 0,
            "retention.lease_days must be positive",
        )?;
        ensure(
            retention.history_days > 0,
            "retention.history_days must be positive",
        )?;
        ensure(
            retention.keep_runs_per_job > 0,
            "retention.keep_runs_per_job must be positive",
        )?;
        ensure(
            retention.max_job_runs >= retention.keep_runs_per_job,
            "retention.max_job_runs must be at least keep_runs_per_job",
        )?;
        for class in JobClass::ALL {
            if let Some(reason) = self.classes.get(class).invalid_reason() {
                return Err(Error::Config(format!(
                    "classes.{}: {reason}",
                    class.as_str()
                )));
            }
        }
        for (name, profile) in &self.presets.0 {
            let well_formed = !name.is_empty()
                && name
                    .bytes()
                    .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_');
            ensure(
                well_formed,
                format!("presets.{name}: names use only a-z, 0-9 and _"),
            )?;
            ensure(
                JobClass::ALL.iter().all(|class| class.as_str() != name),
                format!("presets.{name}: a class already has this name"),
            )?;
            if let Some(reason) = profile.invalid_reason() {
                return Err(Error::Config(format!("presets.{name}: {reason}")));
            }
        }
        for (index, rule) in self.classifier.rules.iter().enumerate() {
            let has_blank =
                rule.program.trim().is_empty() || rule.args.iter().any(|arg| arg.trim().is_empty());
            ensure(
                !has_blank,
                format!("classifier.rules[{index}] has a blank program or arg"),
            )?;
        }
        let flags = &self.classifier.light_flags;
        ensure(
            flags.iter().all(|flag| !flag.trim().is_empty()),
            "classifier.light_flags must not contain blank entries",
        )?;
        Ok(())
    }
}

fn ensure(condition: bool, message: impl Into<String>) -> Result<()> {
    if condition {
        Ok(())
    } else {
        Err(Error::Config(message.into()))
    }
}

fn is_pct(value: f32) -> bool {
    (0.0..=100.0).contains(&value)
}

/// HTTP listener settings.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct DaemonConfig {
    /// Port on `127.0.0.1`.
    pub port: u16,
}

impl Default for DaemonConfig {
    fn default() -> Self {
        Self { port: 47_823 }
    }
}

/// How much of the machine the admission engine keeps free.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AdmissionConfig {
    /// Share of each resource (cores, RAM, GPU, VRAM) never handed out to leases, so the
    /// desktop and unleased work stay responsive.
    pub headroom_pct: f32,
}

impl Default for AdmissionConfig {
    fn default() -> Self {
        Self { headroom_pct: 15.0 }
    }
}

/// When the machine counts as quiet enough for a benchmark.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct BenchmarkConfig {
    /// Total CPU load at or below which the machine is quiet.
    pub quiet_cpu_pct: f32,
    /// GPU engine load at or below which the machine is quiet.
    pub quiet_gpu_pct: f32,
    /// The window the loads are averaged over once no other lease is active. The machine is
    /// quiet when both averages are at or below their limits.
    pub quiet_secs: u64,
    /// How long a drained machine may stay above the limits before a waiting benchmark is
    /// granted anyway, marked as not quiet. At least `quiet_secs`.
    pub max_quiet_wait_secs: u64,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            quiet_cpu_pct: 10.0,
            quiet_gpu_pct: 10.0,
            quiet_secs: 10,
            max_quiet_wait_secs: 60,
        }
    }
}

/// Lease lifetime settings.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LeaseConfig {
    /// A lease without a heartbeat for this long expires.
    pub heartbeat_ttl_secs: u64,
    /// An active lease ends once no command runs under it and its holder's session has been
    /// idle for this long, for holders that forget `release_lease`; 0 turns this off.
    pub idle_release_secs: u64,
}

impl Default for LeaseConfig {
    fn default() -> Self {
        Self {
            heartbeat_ttl_secs: 60,
            idle_release_secs: 180,
        }
    }
}

/// How long ended leases and job history are kept. The daemon prunes once an hour.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RetentionConfig {
    /// Released and expired leases, with their audit trail, are deleted this many days after
    /// they ended.
    pub lease_days: u64,
    /// Job runs are deleted this many days after they ended, except the newest
    /// `keep_runs_per_job` of each job.
    pub history_days: u64,
    /// Runs of each job kept regardless of age, so estimates survive long breaks. The
    /// estimator reads the newest 20.
    pub keep_runs_per_job: u64,
    /// Hard cap on stored job runs; the oldest beyond it are deleted whatever their job.
    pub max_job_runs: u64,
}

impl Default for RetentionConfig {
    fn default() -> Self {
        Self {
            lease_days: 30,
            history_days: 90,
            keep_runs_per_job: 20,
            max_job_runs: 5_000,
        }
    }
}

/// PreToolUse hook behavior.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HookConfig {
    /// Allow heavy commands when the daemon is unreachable. Off means every heavy command is
    /// denied until the daemon is back.
    pub fail_open: bool,
}

impl Default for HookConfig {
    fn default() -> Self {
        Self { fail_open: true }
    }
}

/// How leases use cgroups on Linux; ignored on other OSes.
///
/// Each active lease runs in a systemd scope of its own, which gives exact usage and a kill
/// that ends every process at once. Caps on what a lease may use are opt-in, because a job
/// that outgrows its declared profile is slowed down by them.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LinuxConfig {
    /// Run each lease in its own cgroup scope when the systemd user manager allows it. Off
    /// follows the process tree instead, as without systemd.
    pub scopes: bool,
    /// Cap a lease's CPU time at its reserved cores.
    pub enforce_cpu: bool,
    /// Throttle a lease above its reserved RAM (`MemoryHigh`); it is never killed for memory.
    pub enforce_memory: bool,
}

impl Default for LinuxConfig {
    fn default() -> Self {
        Self {
            scopes: true,
            enforce_cpu: false,
            enforce_memory: false,
        }
    }
}

/// The profile behind each [`JobClass`].
///
/// A partial table such as `[classes.gpu_heavy]` with only `vram_mb` keeps that class's own
/// defaults for the other fields, not zeros.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ClassProfiles {
    /// Profile of [`JobClass::Light`].
    pub light: JobProfile,
    /// Profile of [`JobClass::CpuHeavy`].
    pub cpu_heavy: JobProfile,
    /// Profile of [`JobClass::GpuHeavy`].
    pub gpu_heavy: JobProfile,
    /// Profile of [`JobClass::Mixed`].
    pub mixed: JobProfile,
}

impl ClassProfiles {
    /// Returns the profile configured for `class`.
    pub fn get(&self, class: JobClass) -> &JobProfile {
        match class {
            JobClass::Light => &self.light,
            JobClass::CpuHeavy => &self.cpu_heavy,
            JobClass::GpuHeavy => &self.gpu_heavy,
            JobClass::Mixed => &self.mixed,
        }
    }
}

impl Default for ClassProfiles {
    fn default() -> Self {
        Self {
            light: JobProfile {
                cores: 1.0,
                ram_mb: 512,
                gpu_pct: 0.0,
                vram_mb: 0,
                est_secs: 30,
            },
            cpu_heavy: JobProfile {
                cores: 8.0,
                ram_mb: 4096,
                gpu_pct: 0.0,
                vram_mb: 0,
                est_secs: 300,
            },
            gpu_heavy: JobProfile {
                cores: 2.0,
                ram_mb: 4096,
                gpu_pct: 80.0,
                vram_mb: 4096,
                est_secs: 600,
            },
            mixed: JobProfile {
                cores: 6.0,
                ram_mb: 8192,
                gpu_pct: 50.0,
                vram_mb: 2048,
                est_secs: 600,
            },
        }
    }
}

impl<'de> Deserialize<'de> for ClassProfiles {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let overrides = ClassOverrides::deserialize(deserializer)?;
        let defaults = Self::default();
        Ok(Self {
            light: overrides.light.apply(defaults.light),
            cpu_heavy: overrides.cpu_heavy.apply(defaults.cpu_heavy),
            gpu_heavy: overrides.gpu_heavy.apply(defaults.gpu_heavy),
            mixed: overrides.mixed.apply(defaults.mixed),
        })
    }
}

#[derive(Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct ClassOverrides {
    light: ProfileOverride,
    cpu_heavy: ProfileOverride,
    gpu_heavy: ProfileOverride,
    mixed: ProfileOverride,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct ProfileOverride {
    cores: Option<f32>,
    ram_mb: Option<u64>,
    gpu_pct: Option<f32>,
    vram_mb: Option<u64>,
    est_secs: Option<u64>,
}

impl ProfileOverride {
    fn apply(self, base: JobProfile) -> JobProfile {
        JobProfile {
            cores: self.cores.unwrap_or(base.cores),
            ram_mb: self.ram_mb.unwrap_or(base.ram_mb),
            gpu_pct: self.gpu_pct.unwrap_or(base.gpu_pct),
            vram_mb: self.vram_mb.unwrap_or(base.vram_mb),
            est_secs: self.est_secs.unwrap_or(base.est_secs),
        }
    }
}

/// Named profiles between the classes, by name.
///
/// A `[presets.<name>]` table changes that preset field by field, like a class, or adds a new
/// one, which needs at least `cores` and `ram_mb` (GPU and VRAM default to 0, the run time
/// to 300 s). The default presets stay unless overridden.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(transparent)]
pub struct Presets(pub BTreeMap<String, JobProfile>);

/// Run time of a new preset that gives none.
const PRESET_EST_SECS: u64 = 300;

impl Presets {
    /// The profile of preset `name`.
    pub fn get(&self, name: &str) -> Option<&JobProfile> {
        self.0.get(name)
    }

    /// The preset names, sorted.
    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.0.keys().map(String::as_str)
    }
}

impl Default for Presets {
    fn default() -> Self {
        let preset = |cores, ram_mb, gpu_pct, vram_mb, est_secs| JobProfile {
            cores,
            ram_mb,
            gpu_pct,
            vram_mb,
            est_secs,
        };
        Self(BTreeMap::from([
            ("cpu_small".to_owned(), preset(2.0, 1024, 0.0, 0, 60)),
            ("cpu_medium".to_owned(), preset(4.0, 2048, 0.0, 0, 180)),
            ("gpu_small".to_owned(), preset(1.0, 2048, 30.0, 2048, 300)),
            ("quick".to_owned(), preset(4.0, 2048, 0.0, 0, 10)),
            ("low_impact".to_owned(), preset(0.25, 256, 0.0, 0, 60)),
        ]))
    }
}

impl<'de> Deserialize<'de> for Presets {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let overrides = BTreeMap::<String, ProfileOverride>::deserialize(deserializer)?;
        let mut presets = Self::default();
        for (name, value) in overrides {
            let profile = match presets.0.get(&name) {
                Some(&base) => value.apply(base),
                None => {
                    let (Some(cores), Some(ram_mb)) = (value.cores, value.ram_mb) else {
                        return Err(de::Error::custom(format!(
                            "new preset `{name}` needs both cores and ram_mb"
                        )));
                    };
                    value.apply(JobProfile {
                        cores,
                        ram_mb,
                        gpu_pct: 0.0,
                        vram_mb: 0,
                        est_secs: PRESET_EST_SECS,
                    })
                }
            };
            presets.0.insert(name, profile);
        }
        Ok(presets)
    }
}

/// Which shell commands count as heavy.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ClassifierConfig {
    /// Rules checked against every simple command in a command line, in order.
    pub rules: Vec<HeavyRule>,
    /// A simple command containing any of these arguments only prints and exits, so it is
    /// light even when a rule matches it, for example `cargo build --help`.
    pub light_flags: Vec<String>,
}

impl Default for ClassifierConfig {
    fn default() -> Self {
        Self {
            rules: vec![
                HeavyRule::new(
                    "cargo",
                    &["build", "test", "run", "bench"],
                    JobClass::CpuHeavy,
                ),
                HeavyRule::new("pnpm", &["build", "test"], JobClass::CpuHeavy),
                HeavyRule::new("msbuild", &[], JobClass::CpuHeavy),
                HeavyRule::new("cmake", &["--build"], JobClass::CpuHeavy),
                HeavyRule::new("python*", &["train*.py"], JobClass::GpuHeavy),
                HeavyRule::new("py", &["train*.py"], JobClass::GpuHeavy),
            ],
            light_flags: ["--help", "-h", "--version", "-V", "/?"]
                .map(String::from)
                .to_vec(),
        }
    }
}

/// Marks a simple command as heavy when its program and arguments match.
///
/// Matching ignores case, and `*` matches any run of characters.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HeavyRule {
    /// Program name without directory or extension, for example `cargo` or `python*`.
    pub program: String,
    /// Any one of these makes the command heavy; empty matches any arguments. A pattern that
    /// starts with `-` matches that flag anywhere in the command; any other pattern matches
    /// the first positional argument (the first one not starting with `-` or `+`).
    #[serde(default)]
    pub args: Vec<String>,
    /// Class the matched command needs a lease for.
    pub class: JobClass,
}

impl HeavyRule {
    fn new(program: &str, args: &[&str], class: JobClass) -> Self {
        Self {
            program: program.into(),
            args: args.iter().map(|&arg| arg.into()).collect(),
            class,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::env;

    use super::*;
    use crate::mutate::mutations;

    fn config_error(text: &str) -> String {
        match Config::from_toml(text) {
            Err(Error::Config(message)) => message,
            other => panic!("expected Error::Config, got {other:?}"),
        }
    }

    #[test]
    fn test_config_empty_document_yields_defaults() {
        assert_eq!(Config::from_toml("").unwrap(), Config::default());
    }

    #[test]
    fn test_config_default_passes_validation() {
        Config::default().validate().unwrap();
    }

    #[test]
    fn test_config_default_toml_round_trip_preserves_every_value() {
        let text = toml::to_string(&Config::default()).unwrap();
        assert_eq!(Config::from_toml(&text).unwrap(), Config::default());
    }

    #[test]
    fn test_config_presets_merge_over_defaults_and_add_new_ones() {
        let config = Config::from_toml(
            "[presets.cpu_small]\ncores = 3.0\n\n[presets.render_preview]\ncores = 2.0\n\
             ram_mb = 3000\ngpu_pct = 40.0\n",
        )
        .unwrap();
        let defaults = Presets::default();
        let small = config.presets.get("cpu_small").unwrap();
        assert_eq!(small.cores, 3.0);
        assert_eq!(small.est_secs, defaults.get("cpu_small").unwrap().est_secs);
        let preview = config.presets.get("render_preview").unwrap();
        assert_eq!((preview.gpu_pct, preview.vram_mb), (40.0, 0));
        assert_eq!(preview.est_secs, PRESET_EST_SECS);
        assert_eq!(config.presets.get("quick"), defaults.get("quick"));
    }

    #[test]
    fn test_config_presets_reject_bad_names_and_incomplete_new_ones() {
        assert!(config_error("[presets.cpu_heavy]\ncores = 1.0\nram_mb = 1\n").contains("class"));
        assert!(config_error("[presets.Big]\ncores = 1.0\nram_mb = 1\n").contains("a-z"));
        assert!(config_error("[presets.quick]\ngpu_pct = 140.0\n").contains("presets.quick"));
        let incomplete = Config::from_toml("[presets.huge]\ncores = 12.0\n").unwrap_err();
        assert!(incomplete.to_string().contains("ram_mb"), "{incomplete}");
    }

    #[test]
    fn test_default_presets_are_quick_and_low_impact_where_named() {
        let presets = Presets::default();
        assert!(presets.get("quick").unwrap().is_quick());
        assert!(presets.get("low_impact").unwrap().is_low_impact());
        assert!(!presets.get("cpu_small").unwrap().is_quick());
        assert!(!presets.get("cpu_small").unwrap().is_low_impact());
    }

    #[test]
    fn test_config_partial_table_merges_over_defaults() {
        let config = Config::from_toml(
            "[leases]\nheartbeat_ttl_secs = 90\n\n[classes.gpu_heavy]\nvram_mb = 8192\n",
        )
        .unwrap();
        let defaults = Config::default();
        assert_eq!(config.leases.heartbeat_ttl_secs, 90);
        assert_eq!(
            config.leases.idle_release_secs,
            defaults.leases.idle_release_secs
        );
        assert_eq!(config.classes.gpu_heavy.vram_mb, 8192);
        assert_eq!(
            config.classes.gpu_heavy.cores,
            defaults.classes.gpu_heavy.cores
        );
        assert_eq!(config.classes.cpu_heavy, defaults.classes.cpu_heavy);
        assert_eq!(config.daemon, defaults.daemon);
        assert_eq!(config.classifier, defaults.classifier);
    }

    #[test]
    fn test_config_linux_table_defaults_to_scopes_without_caps_and_merges() {
        let defaults = LinuxConfig::default();
        assert!(defaults.scopes);
        assert!(!defaults.enforce_cpu);
        assert!(!defaults.enforce_memory);

        let config = Config::from_toml("[linux]\nenforce_memory = true\n").unwrap();
        assert_eq!(
            config.linux,
            LinuxConfig {
                enforce_memory: true,
                ..defaults
            }
        );
        let result = Config::from_toml("[linux]\nenforce_io = true\n");
        assert!(matches!(result, Err(Error::ConfigParse(_))));
    }

    #[test]
    fn test_config_integer_percentage_parses_as_float() {
        let config = Config::from_toml("[admission]\nheadroom_pct = 20\n").unwrap();
        assert_eq!(config.admission.headroom_pct, 20.0);
    }

    #[test]
    fn test_config_user_rules_replace_default_rules() {
        let config = Config::from_toml(
            "[[classifier.rules]]\nprogram = \"python\"\nargs = [\"train.py\"]\nclass = \"gpu_heavy\"\n",
        )
        .unwrap();
        assert_eq!(
            config.classifier.rules,
            vec![HeavyRule::new("python", &["train.py"], JobClass::GpuHeavy)]
        );
    }

    #[test]
    fn test_config_rule_without_args_matches_any_arguments() {
        let config =
            Config::from_toml("[[classifier.rules]]\nprogram = \"ninja\"\nclass = \"cpu_heavy\"\n")
                .unwrap();
        assert!(config.classifier.rules[0].args.is_empty());
    }

    #[test]
    fn test_config_user_rules_keep_default_light_flags() {
        let config =
            Config::from_toml("[[classifier.rules]]\nprogram = \"ninja\"\nclass = \"cpu_heavy\"\n")
                .unwrap();
        let defaults = ClassifierConfig::default();
        assert_eq!(config.classifier.light_flags, defaults.light_flags);
        assert!(defaults.light_flags.contains(&"--help".to_owned()));
    }

    #[test]
    fn test_config_blank_light_flag_is_rejected() {
        let message = config_error("[classifier]\nlight_flags = [\"--help\", \"\"]\n");
        assert!(message.contains("classifier.light_flags"));
    }

    #[test]
    fn test_config_unknown_key_is_rejected() {
        let result = Config::from_toml("[leases]\nheartbeat_tll_secs = 90\n");
        assert!(matches!(result, Err(Error::ConfigParse(_))));
        let result = Config::from_toml("[classes.mixed]\nvram = 1024\n");
        assert!(matches!(result, Err(Error::ConfigParse(_))));
        let result = Config::from_toml("[classes.huge]\ncores = 32\n");
        assert!(matches!(result, Err(Error::ConfigParse(_))));
    }

    #[test]
    fn test_config_wrong_type_is_rejected() {
        let result = Config::from_toml("[daemon]\nport = \"eighty\"\n");
        assert!(matches!(result, Err(Error::ConfigParse(_))));
    }

    #[test]
    fn test_config_out_of_range_values_name_the_key() {
        assert!(config_error("[daemon]\nport = 0\n").contains("daemon.port"));
        assert!(config_error("[admission]\nheadroom_pct = 100\n").contains("headroom_pct"));
        assert!(config_error("[benchmark]\nquiet_cpu_pct = 120\n").contains("quiet_cpu_pct"));
        assert!(config_error("[benchmark]\nquiet_secs = 0\n").contains("quiet_secs"));
        let short_wait = "[benchmark]\nquiet_secs = 30\nmax_quiet_wait_secs = 20\n";
        assert!(config_error(short_wait).contains("max_quiet_wait_secs"));
        assert!(config_error("[leases]\nheartbeat_ttl_secs = 0\n").contains("heartbeat_ttl"));
        assert!(config_error("[classes.mixed]\ngpu_pct = 150\n").contains("classes.mixed"));
        assert!(config_error("[retention]\nlease_days = 0\n").contains("retention.lease_days"));
        assert!(config_error("[retention]\nhistory_days = 0\n").contains("history_days"));
        assert!(config_error("[retention]\nkeep_runs_per_job = 0\n").contains("keep_runs"));
        let small_cap = "[retention]\nkeep_runs_per_job = 50\nmax_job_runs = 10\n";
        assert!(config_error(small_cap).contains("retention.max_job_runs"));
    }

    #[test]
    fn test_config_load_invalid_file_names_the_file_and_the_key() {
        let path =
            env::temp_dir().join(format!("allot-shared-test-bad-{}.toml", std::process::id()));
        fs::write(&path, "[leases]\nheartbeat_ttl_secs = 0\n").unwrap();
        let loaded = Config::load(&path);
        fs::remove_file(&path).unwrap();
        let message = loaded.unwrap_err().to_string();
        assert!(
            message.starts_with(&path.display().to_string()),
            "{message}"
        );
        assert!(message.contains("leases.heartbeat_ttl_secs"), "{message}");
    }

    #[test]
    fn test_config_malformed_documents_fail_without_panicking() {
        let text = toml::to_string(&Config::default()).unwrap();
        for variant in mutations(&text, 0x5eed, 2_000) {
            match Config::from_toml(&variant) {
                Ok(config) => config.validate().unwrap(),
                Err(error) => assert!(!error.to_string().is_empty()),
            }
        }
    }

    #[test]
    fn test_config_wrongly_typed_value_names_its_key() {
        let text = toml::to_string(&Config::default()).unwrap();
        let mut checked = 0;
        for (index, line) in text.lines().enumerate() {
            let Some((key, _)) = line.split_once(" = ") else {
                continue;
            };
            for wrong in ["\"text\"", "[1, 2]", "{ x = 1 }", "-1", "true", "1.5", "0"] {
                let mut lines: Vec<&str> = text.lines().collect();
                let replaced = format!("{key} = {wrong}");
                lines[index] = &replaced;
                // A replacement of the right type and range is simply accepted.
                if let Err(error) = Config::from_toml(&lines.join("\n")) {
                    let message = error.to_string();
                    assert!(message.contains(key.trim()), "{key} = {wrong}: {message}");
                    checked += 1;
                }
            }
        }
        assert!(checked > 50, "only {checked} rejections were checked");
    }

    #[test]
    fn test_config_blank_rule_program_is_rejected() {
        let message =
            config_error("[[classifier.rules]]\nprogram = \" \"\nclass = \"cpu_heavy\"\n");
        assert!(message.contains("classifier.rules[0]"));
    }

    #[test]
    fn test_config_load_missing_file_yields_defaults() {
        let path = env::temp_dir()
            .join("allot-shared-test-missing")
            .join("config.toml");
        assert_eq!(Config::load(&path).unwrap(), Config::default());
    }

    #[test]
    fn test_config_load_existing_file_parses_it() {
        let path = env::temp_dir().join(format!("allot-shared-test-{}.toml", std::process::id()));
        fs::write(&path, "[hook]\nfail_open = false\n").unwrap();
        let loaded = Config::load(&path);
        fs::remove_file(&path).unwrap();
        assert!(!loaded.unwrap().hook.fail_open);
    }
}