ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
//! Apply (write) layer for hardware profile settings.
//!
//! Most of the [`super::ProfileInspector`] surface is read-only. This module
//! adds a narrow, audited write path for the settings that providers
//! explicitly opt into via [`Setting::writable`].
//!
//! ## Design principles
//!
//! - **Opt-in writes.** Each handler advertises an exact `(subsystem,
//!   setting_id)` it can write. There is no generic write path.
//! - **Audit everything.** Every attempt — whether allowed, refused, or
//!   failed — appends a JSON line to `<state_dir>/ironmon_profile_audit.log`.
//! - **No silent elevation.** If the OS rejects the write for permissions,
//!   the error bubbles up with a "needs admin/root" hint. We never re-launch
//!   ourselves elevated.
//! - **Confirmation required.** Callers (CLI/MCP) gate apply behind explicit
//!   confirmation. The library function itself does not prompt.
//!
//! Writing arbitrary NVAPI DWORDs, MSRs, or BIOS values is intentionally out
//! of scope. Add handlers only when the API is well-defined and the failure
//! mode is reversible.

use super::{Setting, SettingValue, Subsystem};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};

/// Result of an apply attempt — always returned (never panics), always logged.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ApplyOutcome {
    pub setting_id: String,
    /// `None` when no handler is registered for the id, so no subsystem can be
    /// named. This used to be a `Subsystem::Gpu` placeholder in that case.
    #[serde(default)]
    pub subsystem: Option<Subsystem>,
    pub requested: SettingValue,
    pub status: ApplyStatus,
    pub message: String,
    /// Unix epoch seconds.
    pub timestamp: u64,
    /// The value that was in effect before this write, when the handler could
    /// read it.
    ///
    /// This is what makes a write reversible, and until it existed nothing in
    /// this crate could undo one: an autonomous tuner could set a governor and
    /// then had no way to put back what it found. `None` means the prior value
    /// was not readable — not that there wasn't one — so
    /// [`revert_setting`] refuses rather than guessing at a default.
    #[serde(default)]
    pub previous: Option<SettingValue>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ApplyStatus {
    /// Write succeeded; new value is in effect.
    Applied,
    /// Setting is read-only (no registered handler).
    NotWritable,
    /// The OS rejected the call (permission, busy, etc.).
    Refused,
    /// The handler ran but reports the underlying API failed.
    Failed,
    /// The library would write but the caller did not confirm.
    NeedsConfirm,
}

/// Trait implemented by per-setting write handlers.
pub trait ApplyHandler: Send + Sync {
    /// The setting this handler covers (must match `Setting.id`).
    fn setting_id(&self) -> &str;
    /// The subsystem this setting belongs to.
    fn subsystem(&self) -> Subsystem;
    /// Try to write the requested value. Must not panic; surface any error
    /// via the returned [`ApplyStatus`].
    fn apply(&self, value: &SettingValue) -> ApplyOutcome;

    /// The value currently in effect, when it can be read.
    ///
    /// Defaults to `None` so existing handlers keep compiling, but a handler
    /// that does not implement this makes its setting one-way: `apply_setting`
    /// records no prior value, and [`revert_setting`] will refuse to guess one.
    /// Implement it wherever the source can be read back.
    fn read_current(&self) -> Option<SettingValue> {
        None
    }
}

/// Built-in handler registry. New handlers register themselves by appearing
/// in [`builtin_handlers`].
// Not a vec! literal: which handlers exist depends on `cfg`, and those attributes
// apply to statements, not to elements of an expression.
#[allow(clippy::vec_init_then_push)]
pub fn builtin_handlers() -> Vec<Box<dyn ApplyHandler>> {
    let mut out: Vec<Box<dyn ApplyHandler>> = Vec::new();
    #[cfg(all(feature = "nvidia", target_os = "linux"))]
    out.push(Box::new(NvidiaPersistenceModeHandler));
    #[cfg(target_os = "linux")]
    {
        out.push(Box::new(LinuxCpufreqGovernorHandler));
        out.push(Box::new(LinuxAmdPerfLevelHandler));
        out.push(Box::new(LinuxIntelGtMaxFreqHandler));
    }
    #[cfg(windows)]
    out.push(Box::new(WindowsActivePowerSchemeHandler));
    out
}

/// Read a handler's current value, retrying a transient failure.
///
/// The Win32 and sysfs reads behind `read_current` can fail for reasons that
/// have nothing to do with the setting — a loaded machine, a momentarily busy
/// device. A single failed read is not evidence that the setting is unreadable,
/// and treating it as such silently converts a reversible write into an
/// irreversible one: the write still happens, `previous` is `None`, and nothing
/// says so until someone tries to undo it.
///
/// Observed once during testing, where `PowerGetActiveScheme` returned failure
/// on a machine running several compiles, then read normally on the next
/// attempt. Three tries, because the failure was transient, not persistent.
pub(crate) fn read_current_with_retry(handler: &dyn ApplyHandler) -> Option<SettingValue> {
    for attempt in 0..3 {
        if let Some(v) = handler.read_current() {
            return Some(v);
        }
        if attempt < 2 {
            std::thread::sleep(std::time::Duration::from_millis(20));
        }
    }
    None
}

/// Apply a setting, but only if the change can be undone.
///
/// Identical to [`apply_setting`] except that it refuses when the prior value
/// could not be read. Use it for anything unattended: an autonomous loop that
/// makes a change it cannot reverse has taken a decision away from the person
/// who has to live with the result, and it does so invisibly — the write
/// succeeds, and only a later attempt to undo it reveals there is no way back.
///
/// Attended callers may still want [`apply_setting`]: a person at a terminal who
/// asks for a setting and is told "this cannot be undone" can decide for
/// themselves. That decision is theirs and not this function's, which is why
/// both exist.
pub fn apply_setting_reversible(
    setting_id: &str,
    value: SettingValue,
    confirm: bool,
) -> ApplyOutcome {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    if let Some(h) = builtin_handlers()
        .into_iter()
        .find(|h| h.setting_id() == setting_id)
    {
        if read_current_with_retry(h.as_ref()).is_none() {
            return ApplyOutcome {
                setting_id: setting_id.into(),
                subsystem: Some(h.subsystem()),
                requested: value,
                status: ApplyStatus::Refused,
                message: format!(
                    "Refused: the value currently in effect for {setting_id:?} could not be read, so this write could not be undone. Use apply_setting (or `ironmon profile set`) to write it anyway, accepting that it is one-way."
                ),
                timestamp: now,
                previous: None,
            };
        }
    }

    apply_setting(setting_id, value, confirm)
}

/// Put back the value an earlier [`apply_setting`] overwrote.
///
/// Takes the outcome of the write being undone rather than a setting id and a
/// value, because the whole point is that the caller does not have to have kept
/// the old value anywhere: it travels with the outcome.
///
/// Refuses when `previous` is `None`. That happens when the handler could not
/// read the setting before writing it, and the honest answer there is "this
/// cannot be undone" rather than a guess at a default — putting a machine into a
/// state it was never in is a worse failure than leaving it in the state the
/// caller chose.
///
/// Goes through [`apply_setting`], so a revert is confirmed and audit-logged on
/// exactly the same terms as the write it undoes. An autonomous loop that could
/// revert without confirmation would be a write path with no confirmation.
pub fn revert_setting(applied: &ApplyOutcome, confirm: bool) -> ApplyOutcome {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    // Only a write that took effect needs undoing. Reverting a refused or failed
    // apply would write a value the caller never asked for.
    if applied.status != ApplyStatus::Applied {
        return ApplyOutcome {
            setting_id: applied.setting_id.clone(),
            subsystem: applied.subsystem,
            requested: applied.requested.clone(),
            status: ApplyStatus::NotWritable,
            message: format!(
                "Nothing to revert: the apply being undone ended as {:?}, not Applied.",
                applied.status
            ),
            timestamp: now,
            previous: None,
        };
    }

    let Some(previous) = applied.previous.clone() else {
        return ApplyOutcome {
            setting_id: applied.setting_id.clone(),
            subsystem: applied.subsystem,
            requested: applied.requested.clone(),
            status: ApplyStatus::NotWritable,
            message: format!(
                "Cannot revert {:?}: no prior value was recorded, because the handler could not read the setting before writing it. The setting is still at the applied value.",
                applied.setting_id
            ),
            timestamp: now,
            previous: None,
        };
    };

    apply_setting(&applied.setting_id, previous, confirm)
}

/// Apply a setting by id. Returns an [`ApplyOutcome`] in every case (no
/// panicking, no unhandled errors). Audit-logs the attempt.
pub fn apply_setting(setting_id: &str, value: SettingValue, confirm: bool) -> ApplyOutcome {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let handler = builtin_handlers()
        .into_iter()
        .find(|h| h.setting_id() == setting_id);

    let outcome = match handler {
        None => ApplyOutcome {
            setting_id: setting_id.into(),
            subsystem: None,
            requested: value.clone(),
            status: ApplyStatus::NotWritable,
            message: format!(
                "No registered ApplyHandler for setting id {:?}. \
                The setting may be read-only on this platform or not yet implemented.",
                setting_id
            ),
            timestamp: now,
            previous: None,
        },
        Some(h) if !confirm => ApplyOutcome {
            setting_id: setting_id.into(),
            subsystem: Some(h.subsystem()),
            requested: value.clone(),
            status: ApplyStatus::NeedsConfirm,
            message:
                "Apply rejected: explicit confirmation required (pass confirm=true or --confirm)."
                    .into(),
            timestamp: now,
            previous: None,
        },
        Some(h) => {
            // Read before writing. Afterwards the old value is gone, and a
            // tuner that cannot say what it overwrote cannot put it back.
            let previous = read_current_with_retry(h.as_ref());
            let mut outcome = h.apply(&value);
            outcome.previous = previous;
            outcome
        }
    };

    audit_log(&outcome);
    outcome
}

/// Resolve the audit log path. Honors `IRONMON_AUDIT_LOG` if set; otherwise
/// falls back to `%LOCALAPPDATA%\ironmon\profile_audit.log` on Windows,
/// `$XDG_STATE_HOME/ironmon/profile_audit.log` on Linux, and the OS temp dir
/// elsewhere.
pub fn audit_log_path() -> PathBuf {
    static CACHED: OnceLock<PathBuf> = OnceLock::new();
    CACHED
        .get_or_init(|| {
            if let Ok(p) = std::env::var("IRONMON_AUDIT_LOG") {
                return PathBuf::from(p);
            }
            #[cfg(windows)]
            {
                if let Ok(local) = std::env::var("LOCALAPPDATA") {
                    return PathBuf::from(local)
                        .join("ironmon")
                        .join("profile_audit.log");
                }
            }
            #[cfg(unix)]
            {
                if let Ok(state) = std::env::var("XDG_STATE_HOME") {
                    return PathBuf::from(state)
                        .join("ironmon")
                        .join("profile_audit.log");
                }
                if let Ok(home) = std::env::var("HOME") {
                    return PathBuf::from(home)
                        .join(".local")
                        .join("state")
                        .join("ironmon")
                        .join("profile_audit.log");
                }
            }
            std::env::temp_dir().join("ironmon_profile_audit.log")
        })
        .clone()
}

/// Record an attempt made outside [`apply_setting`] in the same audit log.
///
/// The Jetson clock, power-mode and swap controls write system state but are
/// not profile settings, so they have no [`ApplyHandler`]. An attempt at them
/// is still written to the log in the same shape, so one file holds every
/// write this crate was asked to make. `setting_id` names the operation (for
/// example `"jetson.clocks"`). `subsystem` is the nearest profile subsystem and
/// is coarse for platform-wide operations; the id is the precise record.
///
/// `previous` is always `None` here: these operations do not read back the
/// value they replace, so they cannot be reverted through [`revert_setting`].
pub fn record_attempt(
    setting_id: &str,
    subsystem: Subsystem,
    requested: SettingValue,
    status: ApplyStatus,
    message: &str,
) -> ApplyOutcome {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let outcome = ApplyOutcome {
        setting_id: setting_id.into(),
        subsystem: Some(subsystem),
        requested,
        status,
        message: message.into(),
        timestamp,
        previous: None,
    };
    audit_log(&outcome);
    outcome
}

fn audit_log(outcome: &ApplyOutcome) {
    let path = audit_log_path();
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    // Carry forward the pre-rename log so the history of applied settings is
    // continuous across the rename rather than restarting at it. Idempotent:
    // once this file exists, adoption is a no-op.
    let _ = crate::legacy_paths::adopt(&path);
    use std::io::Write;
    if let Ok(mut f) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
    {
        if let Ok(line) = serde_json::to_string(outcome) {
            let _ = writeln!(f, "{}", line);
        }
    }
}

// ─── Built-in handlers ──────────────────────────────────────────────────────

/// Toggle NVIDIA's persistence mode on GPU 0 via NVML.
#[cfg(all(feature = "nvidia", target_os = "linux"))]
struct NvidiaPersistenceModeHandler;

#[cfg(all(feature = "nvidia", target_os = "linux"))]
impl ApplyHandler for NvidiaPersistenceModeHandler {
    fn setting_id(&self) -> &str {
        "persistence_mode"
    }
    fn subsystem(&self) -> Subsystem {
        Subsystem::Gpu
    }
    fn apply(&self, value: &SettingValue) -> ApplyOutcome {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let target = match value {
            SettingValue::Bool(b) => *b,
            other => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: other.clone(),
                    status: ApplyStatus::Failed,
                    message: "persistence_mode requires a Bool value.".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        use nvml_wrapper::Nvml;
        match Nvml::init().and_then(|n| {
            let mut dev = n.device_by_index(0)?;
            dev.set_persistent(target)?;
            Ok(())
        }) {
            Ok(_) => ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: ApplyStatus::Applied,
                message: format!("NVIDIA persistence mode on GPU 0 set to {}.", target),
                timestamp: now,
                previous: None,
            },
            Err(e) => {
                let s = e.to_string();
                let refused = s.contains("Permission")
                    || s.contains("permission")
                    || s.contains("denied")
                    || s.contains("PrivilegedDriver");
                ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: if refused {
                        ApplyStatus::Refused
                    } else {
                        ApplyStatus::Failed
                    },
                    message: if refused {
                        format!("NVML write rejected — run as administrator/root: {}", s)
                    } else {
                        format!("NVML write failed: {}", s)
                    },
                    timestamp: now,
                    previous: None,
                }
            }
        }
    }
}

/// Set the Linux CPU governor on CPU0 (which propagates to the policy).
#[cfg(target_os = "linux")]
struct LinuxCpufreqGovernorHandler;

#[cfg(target_os = "linux")]
impl ApplyHandler for LinuxCpufreqGovernorHandler {
    fn setting_id(&self) -> &str {
        "scaling_governor"
    }
    // Reading back the same path that `apply` writes. By inspection only: this
    // session had no Linux machine to run it on, and CI compiles but does not
    // exercise a sysfs write.
    fn subsystem(&self) -> Subsystem {
        Subsystem::Cpu
    }
    fn read_current(&self) -> Option<SettingValue> {
        std::fs::read_to_string("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
            .ok()
            .map(|s| SettingValue::Text(s.trim().to_string()))
    }
    fn apply(&self, value: &SettingValue) -> ApplyOutcome {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let target = match value {
            SettingValue::Text(s) => s.clone(),
            other => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: other.clone(),
                    status: ApplyStatus::Failed,
                    message: "scaling_governor requires a Text value (e.g. \"performance\")."
                        .into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        let path = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor";
        match std::fs::write(path, &target) {
            Ok(_) => ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: ApplyStatus::Applied,
                message: format!("CPU0 scaling_governor set to {:?}.", target),
                timestamp: now,
                previous: None,
            },
            Err(e) => {
                let refused = e.kind() == std::io::ErrorKind::PermissionDenied;
                ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: if refused {
                        ApplyStatus::Refused
                    } else {
                        ApplyStatus::Failed
                    },
                    message: if refused {
                        "Permission denied — write to /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor requires root.".into()
                    } else {
                        format!("sysfs write failed: {}", e)
                    },
                    timestamp: now,
                    previous: None,
                }
            }
        }
    }
}

/// Write `/sys/class/drm/cardN/device/power_dpm_force_performance_level` for
/// the first AMD GPU. Accepted values: "auto", "low", "high", "manual",
/// "profile_standard", "profile_min_sclk", "profile_min_mclk", "profile_peak".
#[cfg(target_os = "linux")]
struct LinuxAmdPerfLevelHandler;

#[cfg(target_os = "linux")]
impl ApplyHandler for LinuxAmdPerfLevelHandler {
    fn setting_id(&self) -> &str {
        "perf_level"
    }
    fn subsystem(&self) -> Subsystem {
        Subsystem::Gpu
    }
    fn apply(&self, value: &SettingValue) -> ApplyOutcome {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let target = match value {
            SettingValue::Text(s) => s.clone(),
            other => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: other.clone(),
                    status: ApplyStatus::Failed,
                    message: "perf_level requires a Text value (e.g. \"auto\").".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        // Locate the first AMD card.
        let card = match find_amd_card() {
            Some(c) => c,
            None => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: ApplyStatus::Failed,
                    message: "No AMD GPU card found in /sys/class/drm.".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        let path = card
            .join("device")
            .join("power_dpm_force_performance_level");
        match std::fs::write(&path, &target) {
            Ok(_) => ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: ApplyStatus::Applied,
                message: format!("AMD perf level set to {:?} via {}", target, path.display()),
                timestamp: now,
                previous: None,
            },
            Err(e) => {
                let refused = e.kind() == std::io::ErrorKind::PermissionDenied;
                ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: if refused {
                        ApplyStatus::Refused
                    } else {
                        ApplyStatus::Failed
                    },
                    message: if refused {
                        format!(
                            "Permission denied — sysfs write to {} requires root.",
                            path.display()
                        )
                    } else {
                        format!("sysfs write failed: {}", e)
                    },
                    timestamp: now,
                    previous: None,
                }
            }
        }
    }
}

/// Write `/sys/class/drm/cardN/gt_max_freq_mhz` for the first Intel GPU.
#[cfg(target_os = "linux")]
struct LinuxIntelGtMaxFreqHandler;

#[cfg(target_os = "linux")]
impl ApplyHandler for LinuxIntelGtMaxFreqHandler {
    fn setting_id(&self) -> &str {
        "gt_max_freq_mhz"
    }
    fn subsystem(&self) -> Subsystem {
        Subsystem::Gpu
    }
    fn apply(&self, value: &SettingValue) -> ApplyOutcome {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let mhz = match value {
            SettingValue::Uint(u) => *u,
            SettingValue::Int(i) if *i >= 0 => *i as u64,
            other => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: other.clone(),
                    status: ApplyStatus::Failed,
                    message: "gt_max_freq_mhz requires a non-negative integer (MHz).".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        let card = match find_intel_card() {
            Some(c) => c,
            None => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: ApplyStatus::Failed,
                    message: "No Intel GPU card found in /sys/class/drm.".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        let path = card.join("gt_max_freq_mhz");
        match std::fs::write(&path, mhz.to_string()) {
            Ok(_) => ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: ApplyStatus::Applied,
                message: format!("Intel GT max frequency set to {} MHz.", mhz),
                timestamp: now,
                previous: None,
            },
            Err(e) => {
                let refused = e.kind() == std::io::ErrorKind::PermissionDenied;
                ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: if refused {
                        ApplyStatus::Refused
                    } else {
                        ApplyStatus::Failed
                    },
                    message: if refused {
                        format!(
                            "Permission denied — sysfs write to {} requires root.",
                            path.display()
                        )
                    } else {
                        format!("sysfs write failed: {}", e)
                    },
                    timestamp: now,
                    previous: None,
                }
            }
        }
    }
}

#[cfg(target_os = "linux")]
fn find_amd_card() -> Option<std::path::PathBuf> {
    find_drm_card_with_vendor("0x1002")
}

#[cfg(target_os = "linux")]
fn find_intel_card() -> Option<std::path::PathBuf> {
    find_drm_card_with_vendor("0x8086")
}

#[cfg(target_os = "linux")]
fn find_drm_card_with_vendor(target_vendor: &str) -> Option<std::path::PathBuf> {
    let rd = std::fs::read_dir("/sys/class/drm").ok()?;
    for entry in rd.flatten() {
        let name = entry.file_name().to_string_lossy().to_string();
        if !name.starts_with("card") || name.contains('-') {
            continue;
        }
        let path = entry.path();
        let vendor = std::fs::read_to_string(path.join("device").join("vendor")).ok()?;
        if vendor.trim() == target_vendor {
            return Some(path);
        }
    }
    None
}

/// Switch the active Windows power scheme via `PowerSetActiveScheme`.
/// Accepts a string GUID in the canonical 8-4-4-4-12 format.
#[cfg(windows)]
struct WindowsActivePowerSchemeHandler;

#[cfg(windows)]
impl ApplyHandler for WindowsActivePowerSchemeHandler {
    fn setting_id(&self) -> &str {
        "active_scheme_guid"
    }
    fn subsystem(&self) -> Subsystem {
        Subsystem::Cpu
    }
    fn read_current(&self) -> Option<SettingValue> {
        use windows::Win32::System::Power::PowerGetActiveScheme;
        let mut guid_ptr: *mut windows::core::GUID = std::ptr::null_mut();
        // SAFETY: PowerGetActiveScheme writes a pointer to a GUID allocated by
        // the system; None selects the same user root that `apply` writes to.
        // The allocation is released with LocalFree below, as the API documents.
        let err = unsafe { PowerGetActiveScheme(None, &mut guid_ptr) };
        if err.0 != 0 || guid_ptr.is_null() {
            return None;
        }
        // SAFETY: non-null and written by the call above.
        let guid = unsafe { *guid_ptr };
        // SAFETY: the API allocates with LocalAlloc, so LocalFree is the
        // documented counterpart. Leaking here would leak on every read, and a
        // tuning loop reads on every cycle.
        unsafe {
            let _ = windows::Win32::Foundation::LocalFree(windows::Win32::Foundation::HLOCAL(
                guid_ptr as *mut _,
            ));
        }
        Some(SettingValue::Text(format!("{:?}", guid).to_lowercase()))
    }
    fn apply(&self, value: &SettingValue) -> ApplyOutcome {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let guid_str = match value {
            SettingValue::Text(s) => s.trim_matches(|c| c == '{' || c == '}').to_string(),
            other => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: other.clone(),
                    status: ApplyStatus::Failed,
                    message: "active_scheme_guid requires a Text value (GUID).".into(),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        let guid = match parse_guid(&guid_str) {
            Some(g) => g,
            None => {
                return ApplyOutcome {
                    setting_id: self.setting_id().into(),
                    subsystem: Some(self.subsystem()),
                    requested: value.clone(),
                    status: ApplyStatus::Failed,
                    message: format!("Invalid GUID format: {:?}", guid_str),
                    timestamp: now,
                    previous: None,
                };
            }
        };
        use windows::Win32::System::Power::PowerSetActiveScheme;
        // SAFETY: PowerSetActiveScheme is a Win32 FFI call. The HKEY argument
        // None selects the system user root, which is the documented value
        // for "current user / default" usage. The GUID pointer is to a stack
        // value that lives for the duration of the call.
        let win_error = unsafe { PowerSetActiveScheme(None, Some(&guid)) };
        if win_error.0 == 0 {
            ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: ApplyStatus::Applied,
                message: format!("Windows active power scheme set to {{{}}}.", guid_str),
                timestamp: now,
                previous: None,
            }
        } else {
            // ERROR_ACCESS_DENIED = 5
            let refused = win_error.0 == 5;
            ApplyOutcome {
                setting_id: self.setting_id().into(),
                subsystem: Some(self.subsystem()),
                requested: value.clone(),
                status: if refused {
                    ApplyStatus::Refused
                } else {
                    ApplyStatus::Failed
                },
                message: if refused {
                    "Permission denied — try running as administrator.".into()
                } else {
                    format!("PowerSetActiveScheme failed: WIN32_ERROR {}", win_error.0)
                },
                timestamp: now,
                previous: None,
            }
        }
    }
}

#[cfg(windows)]
fn parse_guid(s: &str) -> Option<windows::core::GUID> {
    // Expected format: 8-4-4-4-12 hex chars, e.g. "381b4222-f694-41f0-9685-ff5bb260df2e"
    let parts: Vec<&str> = s.split('-').collect();
    if parts.len() != 5
        || parts[0].len() != 8
        || parts[1].len() != 4
        || parts[2].len() != 4
        || parts[3].len() != 4
        || parts[4].len() != 12
    {
        return None;
    }
    let d1 = u32::from_str_radix(parts[0], 16).ok()?;
    let d2 = u16::from_str_radix(parts[1], 16).ok()?;
    let d3 = u16::from_str_radix(parts[2], 16).ok()?;
    let d4_hi = u16::from_str_radix(parts[3], 16).ok()?;
    let d4_lo = u64::from_str_radix(parts[4], 16).ok()?;
    let mut d4 = [0u8; 8];
    d4[0] = (d4_hi >> 8) as u8;
    d4[1] = (d4_hi & 0xFF) as u8;
    for i in 0..6 {
        d4[2 + i] = ((d4_lo >> ((5 - i) * 8)) & 0xFF) as u8;
    }
    Some(windows::core::GUID::from_values(d1, d2, d3, d4))
}

/// Public list of writable setting ids on this build.
pub fn writable_setting_ids() -> Vec<String> {
    builtin_handlers()
        .into_iter()
        .map(|h| h.setting_id().to_string())
        .collect()
}

#[allow(dead_code)]
fn _doctest_anchor(_s: &Setting) {}

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

    #[test]
    fn unknown_setting_is_not_writable() {
        let outcome = apply_setting(
            "definitely-not-a-real-setting",
            SettingValue::Bool(true),
            true,
        );
        assert_eq!(outcome.status, ApplyStatus::NotWritable);
    }

    #[test]
    fn confirm_required() {
        // Pick any registered id (or skip if none on this build).
        let ids = writable_setting_ids();
        if let Some(id) = ids.first() {
            // Even without OS permission, the no-confirm path should return
            // NeedsConfirm before attempting the write.
            let outcome = apply_setting(id, SettingValue::Bool(true), false);
            assert_eq!(outcome.status, ApplyStatus::NeedsConfirm);
        }
    }

    #[test]
    fn audit_log_path_resolves() {
        let p = audit_log_path();
        assert!(p.to_string_lossy().to_lowercase().contains("profile_audit"));
    }

    #[test]
    fn outcome_serializable() {
        let outcome = ApplyOutcome {
            setting_id: "x".into(),
            subsystem: Some(Subsystem::Cpu),
            requested: SettingValue::Bool(true),
            status: ApplyStatus::NotWritable,
            message: "test".into(),
            timestamp: 0,
            previous: None,
        };
        let s = serde_json::to_string(&outcome).unwrap();
        assert!(s.contains("not_writable"));
    }

    /// A write that cannot say what it overwrote cannot be undone, and the
    /// refusal has to be explicit rather than a silent no-op.
    #[test]
    fn revert_refuses_when_no_prior_value_was_recorded() {
        let applied = ApplyOutcome {
            setting_id: "scaling_governor".into(),
            subsystem: Some(Subsystem::Cpu),
            requested: SettingValue::Text("performance".into()),
            status: ApplyStatus::Applied,
            message: "applied".into(),
            timestamp: 0,
            previous: None,
        };
        let out = revert_setting(&applied, true);
        assert_eq!(out.status, ApplyStatus::NotWritable);
        assert!(
            out.message.contains("no prior value"),
            "the refusal should say why, got: {}",
            out.message
        );
    }

    /// Reverting something that never took effect would write a value the
    /// caller never asked for.
    #[test]
    fn revert_refuses_an_apply_that_did_not_take_effect() {
        for status in [
            ApplyStatus::Refused,
            ApplyStatus::Failed,
            ApplyStatus::NeedsConfirm,
            ApplyStatus::NotWritable,
        ] {
            let applied = ApplyOutcome {
                setting_id: "scaling_governor".into(),
                subsystem: Some(Subsystem::Cpu),
                requested: SettingValue::Text("performance".into()),
                status,
                message: String::new(),
                timestamp: 0,
                previous: Some(SettingValue::Text("powersave".into())),
            };
            let out = revert_setting(&applied, true);
            assert_eq!(
                out.status,
                ApplyStatus::NotWritable,
                "reverting a {status:?} apply must refuse"
            );
        }
    }

    /// A revert is a write, so it needs confirmation on the same terms.
    /// Otherwise an autonomous loop would have an unconfirmed write path.
    ///
    /// The refusal's *form* is platform-dependent and the invariant is not.
    /// `apply_setting` looks the handler up before checking confirmation, so a
    /// setting with no handler on this platform is refused as `NotWritable`
    /// rather than `NeedsConfirm` — deliberately, because telling someone to
    /// pass `--confirm` for a setting that can never be written here sends them
    /// down a dead end.
    ///
    /// This test asserted `NeedsConfirm` unconditionally against
    /// `active_scheme_guid`, which only exists on Windows. It passed on the
    /// machine that wrote it and failed on macOS in CI, where the crate
    /// registers no writable settings at all. The registry is asked directly
    /// rather than restating its `cfg`s here, so a handler that gains or loses
    /// a platform does not silently make this test vacuous.
    #[test]
    fn revert_without_confirmation_is_refused() {
        const SETTING: &str = "active_scheme_guid";

        let applied = ApplyOutcome {
            setting_id: SETTING.into(),
            subsystem: Some(Subsystem::Cpu),
            requested: SettingValue::Text("381b4222-f694-41f0-9685-ff5bb260df2e".into()),
            status: ApplyStatus::Applied,
            message: "applied".into(),
            timestamp: 0,
            previous: Some(SettingValue::Text(
                "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c".into(),
            )),
        };

        let registered = builtin_handlers()
            .into_iter()
            .any(|h| h.setting_id() == SETTING);
        let out = revert_setting(&applied, false);

        // The part that holds everywhere, and the part that matters: an
        // unconfirmed revert never writes.
        assert_ne!(
            out.status,
            ApplyStatus::Applied,
            "an unconfirmed revert must never take effect"
        );

        if registered {
            assert_eq!(
                out.status,
                ApplyStatus::NeedsConfirm,
                "a handler exists, so the refusal must be about the missing confirmation"
            );
        } else {
            assert_eq!(
                out.status,
                ApplyStatus::NotWritable,
                "no handler on this platform, so the refusal must say that instead \
                 of asking for a confirmation that would not help"
            );
        }
    }

    /// The prior value round-trips through serialisation, so an outcome
    /// recorded in an audit log stays revertible after being read back.
    #[test]
    fn a_recorded_prior_value_survives_serialisation() {
        let outcome = ApplyOutcome {
            setting_id: "scaling_governor".into(),
            subsystem: Some(Subsystem::Cpu),
            requested: SettingValue::Text("performance".into()),
            status: ApplyStatus::Applied,
            message: String::new(),
            timestamp: 0,
            previous: Some(SettingValue::Text("schedutil".into())),
        };
        let json = serde_json::to_string(&outcome).unwrap();
        let back: ApplyOutcome = serde_json::from_str(&json).unwrap();
        assert_eq!(back.previous, Some(SettingValue::Text("schedutil".into())));
    }

    /// Outcomes written before `previous` existed still deserialise, so an
    /// existing audit log does not become unreadable.
    #[test]
    fn an_outcome_without_previous_still_deserialises() {
        // Built by removing the key from a real outcome rather than hand-written,
        // so the test cannot pass or fail on a guess at the wire format.
        let outcome = ApplyOutcome {
            setting_id: "scaling_governor".into(),
            subsystem: Some(Subsystem::Cpu),
            requested: SettingValue::Text("performance".into()),
            status: ApplyStatus::Applied,
            message: String::new(),
            timestamp: 0,
            previous: Some(SettingValue::Text("schedutil".into())),
        };
        let mut value: serde_json::Value =
            serde_json::from_str(&serde_json::to_string(&outcome).unwrap()).unwrap();
        value.as_object_mut().unwrap().remove("previous");
        let back: ApplyOutcome = serde_json::from_value(value).unwrap();
        assert_eq!(back.previous, None);
    }

    /// The Windows handler can read the scheme it writes.
    ///
    /// Without this the setting is one-way: `apply_setting` records no prior
    /// value and `revert_setting` refuses. Cross-checked against
    /// `ironmon profile explain active_scheme_guid`, which reaches the same
    /// registry through entirely different code in `profile::cpu`.
    #[cfg(windows)]
    #[test]
    fn the_windows_power_scheme_reads_back_as_a_guid() {
        let current = WindowsActivePowerSchemeHandler.read_current();
        let Some(SettingValue::Text(guid)) = current else {
            panic!("active power scheme did not read back as text: {current:?}");
        };
        println!("read_current -> {guid}");
        assert_eq!(guid.len(), 36, "expected a bare 36-char GUID, got {guid:?}");
        assert_eq!(
            guid.matches('-').count(),
            4,
            "expected GUID dash grouping, got {guid:?}"
        );
        assert!(
            guid.chars().all(|c| c.is_ascii_hexdigit() || c == '-'),
            "GUID should be hex and dashes, got {guid:?}"
        );
    }

    /// What `read_current` emits must be what `apply` accepts.
    ///
    /// This is the seam a revert crosses, and it is invisible if only one side
    /// is tested: `read_current` formats a GUID with `Debug`, `apply` parses one
    /// with `parse_guid`, and nothing forces those two to agree. If the Debug
    /// representation ever gains braces or uppercase or a `GUID { .. }` wrapper,
    /// every revert of this setting starts failing and the unit tests above stay
    /// green.
    #[cfg(windows)]
    #[test]
    fn the_windows_guid_round_trips_from_read_to_parse() {
        let Some(SettingValue::Text(read)) = WindowsActivePowerSchemeHandler.read_current() else {
            panic!("no scheme readable");
        };
        let parsed = parse_guid(&read).expect(
            "the GUID that read_current produced must parse with the same function apply uses",
        );
        // And round again: formatting the parsed value must reproduce the string,
        // so a revert writes the identical scheme rather than a near-miss.
        let reformatted = format!("{parsed:?}").to_lowercase();
        assert_eq!(
            reformatted, read,
            "read -> parse -> format must be lossless"
        );
    }

    /// Every GUID Windows offers must survive the same round trip, not just
    /// whichever one happens to be active on the development machine.
    #[cfg(windows)]
    #[test]
    fn every_known_scheme_guid_round_trips() {
        for guid in [
            "381b4222-f694-41f0-9685-ff5bb260df2e", // Balanced
            "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c", // High performance
            "a1841308-3541-4fab-bc81-f71556f20b4a", // Power saver
            "00000000-0000-0000-0000-000000000000", // all-zero edge
            "ffffffff-ffff-ffff-ffff-ffffffffffff", // all-ones edge
        ] {
            let parsed = parse_guid(guid).unwrap_or_else(|| panic!("{guid} should parse"));
            assert_eq!(
                format!("{parsed:?}").to_lowercase(),
                guid,
                "{guid} did not survive parse -> format"
            );
        }
    }

    /// Malformed input is rejected rather than silently truncated into a
    /// different, valid-looking GUID.
    #[test]
    #[cfg(windows)]
    fn malformed_guids_are_rejected() {
        for bad in [
            "",
            "381b4222",
            "381b4222-f694-41f0-9685",
            "381b4222-f694-41f0-9685-ff5bb260df2e-extra",
            "381b4222-f694-41f0-9685-ff5bb260df2ee", // one too long
            "zzzzzzzz-f694-41f0-9685-ff5bb260df2e",  // not hex
            "381b4222 f694 41f0 9685 ff5bb260df2e",  // spaces, not dashes
        ] {
            assert!(
                parse_guid(bad).is_none(),
                "{bad:?} should not parse as a GUID"
            );
        }
    }

    /// Hammer the Win32 read from many threads at once.
    ///
    /// `read_current` allocates through `PowerGetActiveScheme` and frees with
    /// `LocalFree` on every call, and the tuning loop calls it on every cycle
    /// for every setting. A mistake there is heap corruption, which shows up as
    /// an intermittent failure somewhere else entirely — the worst kind to chase
    /// and the reason this is a stress test rather than a single call.
    #[cfg(windows)]
    #[test]
    fn concurrent_reads_of_the_power_scheme_agree_and_do_not_corrupt() {
        use std::sync::Arc;
        let first = WindowsActivePowerSchemeHandler
            .read_current()
            .expect("scheme must be readable");
        let expected = Arc::new(first);

        let mut handles = Vec::new();
        for _ in 0..8 {
            let expected = Arc::clone(&expected);
            handles.push(std::thread::spawn(move || {
                for _ in 0..250 {
                    let got = WindowsActivePowerSchemeHandler.read_current();
                    assert_eq!(
                        got.as_ref(),
                        Some(&*expected),
                        "a concurrent read disagreed with the first one"
                    );
                }
            }));
        }
        for h in handles {
            h.join()
                .expect("a reader thread panicked — suspect the LocalFree");
        }
    }

    /// A write that cannot be undone is refused for unattended callers, and the
    /// refusal names the way to override it.
    #[test]
    fn reversible_apply_refuses_when_the_prior_value_is_unreadable() {
        // No handler at all is the strongest case of "cannot be read": the
        // fallback must not be to write anyway.
        let out = apply_setting_reversible(
            "definitely_not_a_registered_setting",
            SettingValue::Bool(true),
            true,
        );
        assert_ne!(
            out.status,
            ApplyStatus::Applied,
            "an unregistered setting must never report as applied"
        );
    }

    /// The retry gives a flaky read three chances before declaring the setting
    /// unreadable. A single transient failure silently turning a reversible
    /// write into a one-way one is the bug this exists to prevent.
    #[test]
    fn read_current_retries_before_giving_up() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        struct FlakyOnce(AtomicUsize);
        impl ApplyHandler for FlakyOnce {
            fn setting_id(&self) -> &str {
                "flaky"
            }
            fn subsystem(&self) -> Subsystem {
                Subsystem::Cpu
            }
            fn apply(&self, _v: &SettingValue) -> ApplyOutcome {
                unreachable!("this test never writes")
            }
            fn read_current(&self) -> Option<SettingValue> {
                // Fails the first time, succeeds after — the shape of the
                // failure actually observed on a loaded machine.
                if self.0.fetch_add(1, Ordering::SeqCst) == 0 {
                    None
                } else {
                    Some(SettingValue::Text("recovered".into()))
                }
            }
        }

        let h = FlakyOnce(AtomicUsize::new(0));
        assert_eq!(
            read_current_with_retry(&h),
            Some(SettingValue::Text("recovered".into())),
            "a read that fails once then succeeds must be retried, not given up on"
        );

        struct AlwaysNone;
        impl ApplyHandler for AlwaysNone {
            fn setting_id(&self) -> &str {
                "never"
            }
            fn subsystem(&self) -> Subsystem {
                Subsystem::Cpu
            }
            fn apply(&self, _v: &SettingValue) -> ApplyOutcome {
                unreachable!("this test never writes")
            }
        }
        assert_eq!(
            read_current_with_retry(&AlwaysNone),
            None,
            "a genuinely unreadable setting still reports unreadable"
        );
    }

    /// The whole point, end to end, against the real machine: apply a setting,
    /// confirm it changed, put it back, confirm it is back.
    ///
    /// `#[ignore]` because it writes to the machine it runs on. Every other test
    /// here proves a piece in isolation — that a refusal refuses, that a GUID
    /// round-trips — and none of them proves the pieces compose into a change
    /// that can actually be undone. Run it deliberately:
    ///
    /// ```text
    /// cargo test --all-features --lib -- --ignored round_trip
    /// ```
    ///
    /// Cross-check from outside with `powercfg /getactivescheme` before and
    /// after; if this test is wrong, the tool it is testing is not the one to
    /// ask about the result.
    #[cfg(windows)]
    #[test]
    #[ignore = "writes to the machine's active power scheme"]
    fn round_trip_the_active_power_scheme_on_real_hardware() {
        const HIGH_PERFORMANCE: &str = "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c";
        const BALANCED: &str = "381b4222-f694-41f0-9685-ff5bb260df2e";

        let Some(SettingValue::Text(start)) = WindowsActivePowerSchemeHandler.read_current() else {
            panic!("cannot read the starting scheme; refusing to write blind");
        };
        println!("start:  {start}");

        // Move to whichever standard scheme is not the current one, so the test
        // is a real change on any machine rather than only on a Balanced one.
        let target = if start == HIGH_PERFORMANCE {
            BALANCED
        } else {
            HIGH_PERFORMANCE
        };

        let applied = apply_setting_reversible(
            "active_scheme_guid",
            SettingValue::Text(target.into()),
            true,
        );
        println!("apply:  {:?} — {}", applied.status, applied.message);
        assert_eq!(
            applied.status,
            ApplyStatus::Applied,
            "the write did not take effect: {}",
            applied.message
        );
        assert_eq!(
            applied.previous,
            Some(SettingValue::Text(start.clone())),
            "the outcome must record exactly what it overwrote"
        );

        // Confirm from the machine, not from the outcome we just built.
        let after = WindowsActivePowerSchemeHandler.read_current();
        println!("during: {after:?}");
        assert_eq!(
            after,
            Some(SettingValue::Text(target.into())),
            "the machine did not actually change"
        );

        let reverted = revert_setting(&applied, true);
        println!("revert: {:?} — {}", reverted.status, reverted.message);
        assert_eq!(
            reverted.status,
            ApplyStatus::Applied,
            "the revert did not take effect: {} — THE MACHINE IS LEFT ON {target}",
            reverted.message
        );

        let end = WindowsActivePowerSchemeHandler.read_current();
        println!("end:    {end:?}");
        assert_eq!(
            end,
            Some(SettingValue::Text(start)),
            "the machine was not returned to where it started"
        );
    }
}