subc-daemon 0.50.62

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
//! `supervisor.rescan_checked`: previewing caller-supplied configuration text
//! without writing or applying it, and applying the config file only while the
//! file and the staged configuration still match that preview.
//!
//! Both halves run through [`ControlHandler::handle_supervisor_rescan`], the
//! code `supervisor.rescan` uses, so a preview and an apply cannot drift from
//! what a plain rescan does. This file holds only what differs: where the
//! configuration text comes from, the apply's precondition, and the extra
//! detail a candidate preview reports.

use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Path, PathBuf},
    sync::atomic::Ordering,
    time::Duration,
};

use sha2::{Digest, Sha256};
use subc_control::{
    ops, BusyGaugeReading, LaunchSpecAgreement, RescanExpect, RescanModuleAction,
    RescanModuleActionKind, StagedGeneration, SupervisorRescanResult, CONFIG_CHANGED_SINCE_PREVIEW,
    RESCAN_CANDIDATE_MAX_BYTES, RESCAN_CANDIDATE_PREVIEW_CAPABILITY, RESCAN_CANDIDATE_TOO_LARGE,
};

use super::*;
use crate::{
    daemon_config::{ActivityConfig, ConfiguredModule, DaemonConfig},
    router::{RouteCtx, RouterError},
    supervise::{ModuleSpec, SupervisedModule},
    Frame, HealthConfig,
};

#[cfg(test)]
mod tests;

/// Where a rescan's configuration comes from, and whether it is applied.
#[derive(Clone)]
pub(super) enum RescanMode {
    /// `supervisor.rescan`: read the config file and apply it.
    Execute,
    /// `supervisor.rescan { preview: true }`: read the config file and apply
    /// nothing.
    Preview,
    /// A `supervisor.rescan_checked` preview: parse this text and apply
    /// nothing.
    Candidate(String),
    /// A `supervisor.rescan_checked` apply: read the config file and apply it,
    /// but only if the file and the staged configuration still match.
    Checked(RescanExpect),
}

const CONFIG_READ_DEADLINE: Duration = Duration::from_secs(5);
// A timed-out filesystem read cannot be cancelled. Its slot stays charged
// until it finishes, so repeated rescans cannot create unbounded threads.
static CONFIG_READ_SLOTS: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(4);

pub(super) struct RescanInput {
    pub(super) config: Option<(DaemonConfig, ActivityConfig)>,
    pub(super) approved_preview: Option<DaemonConfig>,
    pub(super) guard: tokio::sync::OwnedMutexGuard<()>,
}

pub(super) enum RescanInputError {
    Config(&'static str, String),
    Gate(crate::config_gate::GateError),
}

impl RescanInputError {
    pub(super) fn into_frame(self, request: &Frame) -> Result<Frame, RouterError> {
        match self {
            Self::Config(code, message) => control_error_frame(request, code, message),
            Self::Gate(error) => config_gate::config_error_frame(request, error),
        }
    }
}

impl RescanMode {
    pub(super) fn previews(&self) -> bool {
        matches!(self, Self::Preview | Self::Candidate(_))
    }
}

/// One supervised module as a rescan finds it: the module, its staged launch
/// spec and health policy, and whether it is enabled.
pub(super) type CurrentModule = (SupervisedModule, ModuleSpec, HealthConfig, bool);

#[derive(Debug, Default)]
pub(super) struct ModuleEntries {
    registration_channels: BTreeMap<String, bool>,
    configured: BTreeMap<String, ConfiguredModule>,
}

impl ModuleEntries {
    pub(super) fn reserved(&self, module_id: &str) -> bool {
        self.configured
            .get(module_id)
            .is_some_and(|entry| entry.reserved)
    }
    pub(super) fn seed(&mut self, modules: &[ConfiguredModule]) {
        self.configured = modules
            .iter()
            .cloned()
            .map(|m| (m.module_id.clone(), m))
            .collect();
        self.registration_channels = modules
            .iter()
            .map(|m| (m.module_id.clone(), m.registration_channel))
            .collect();
    }

    pub(super) fn record(
        &mut self,
        configured: &BTreeMap<String, ConfiguredModule>,
        added: &[String],
        removed: &[String],
    ) {
        for id in removed {
            self.registration_channels.remove(id);
        }
        for id in added {
            self.registration_channels
                .insert(id.clone(), configured[id].registration_channel);
        }
        self.configured = configured.clone();
    }
}

pub(super) fn entry_pending_fields(
    current: &ConfiguredModule,
    next: &ConfiguredModule,
    applies_configuration: bool,
) -> Vec<subc_control::SupervisorPendingField> {
    use subc_control::{PendingModuleApply as Apply, SupervisorPendingField};
    let mut fields = Vec::new();
    let mut add = |field: &str, applies, value| {
        fields.push(SupervisorPendingField {
            field: field.to_owned(),
            applies,
            value: Some(value),
        })
    };
    if current.register_timeout != next.register_timeout {
        add(
            "register_timeout_s",
            Apply::NextSupervision,
            serde_json::json!(next.register_timeout.as_secs()),
        );
    }
    if current.restart != next.restart {
        add(
            "restart",
            Apply::NextSupervision,
            serde_json::json!({
                "max_restarts": next.restart.max_restarts,
                "window_secs": next.restart.window.as_secs(),
                "backoff_ms": next.restart.backoff.as_millis() as u64,
                "max_backoff_ms": next.restart.max_backoff.as_millis() as u64,
            }),
        );
    }
    let drain = |module: &ConfiguredModule| {
        module
            .drain_timeout_ms
            .map(Duration::from_millis)
            .unwrap_or(crate::supervise::DEFAULT_DRAIN_TIMEOUT)
    };
    if !applies_configuration && drain(current) != drain(next) {
        add(
            "drain_timeout_ms",
            Apply::WithNextLaunchOrHealthChange,
            serde_json::json!(drain(next).as_millis() as u64),
        );
    }
    let relay = |module: &ConfiguredModule| {
        module
            .route_bind_relay_timeout_ms
            .map(Duration::from_millis)
            .unwrap_or(crate::DEFAULT_ROUTE_BIND_RELAY_TIMEOUT)
    };
    if relay(current) != relay(next) {
        add(
            "route_bind_relay_timeout_ms",
            Apply::DaemonRestart,
            serde_json::json!(relay(next).as_millis() as u64),
        );
    }
    if current.registration_channel != next.registration_channel {
        add(
            "registration_channel",
            Apply::ConfigGate,
            serde_json::json!(next.registration_channel),
        );
    }
    fields
}

impl ControlHandler {
    pub(super) fn registration_channel_in_force(
        declaration: Option<&str>,
        configured: Option<bool>,
    ) -> Option<bool> {
        match declaration {
            Some("declared") => Some(true),
            Some("undeclared") => Some(false),
            // Entries with `protocol: none` speak no control protocol and have
            // no registration channel; use their approved metadata instead.
            _ => configured,
        }
    }

    pub(super) fn pending_module_fields(
        &self,
        module: &SupervisedModule,
        proposed: Option<&ConfiguredModule>,
        applies_configuration: bool,
        approved_registration_channel: Option<bool>,
        publication: Option<&crate::config_gate::runtime::Published>,
    ) -> Result<Vec<subc_control::SupervisorPendingField>, String> {
        let id = module.module_id();
        let (recorded, registration_channel) = self
            .rescan
            .as_ref()
            .map(|context| {
                let entries = context
                    .module_entries
                    .lock()
                    .unwrap_or_else(|e| e.into_inner());
                (
                    entries.configured.get(id).cloned(),
                    entries
                        .registration_channels
                        .get(id)
                        .copied()
                        .unwrap_or(false),
                )
            })
            .unwrap_or_default();
        let Some(next) = proposed.cloned().or(recorded) else {
            return Ok(Vec::new());
        };
        let status = module.status().map_err(|e| e.to_string())?;
        let mut current = next.clone();
        current.register_timeout = module
            .configured_register_timeout()
            .map_err(|e| e.to_string())?;
        current.restart = crate::supervise::RestartPolicy {
            max_restarts: status.max_restarts,
            window: status.restart_window,
            backoff: status.restart_backoff,
            max_backoff: status.restart_max_backoff,
        };
        current.drain_timeout_ms = Some(status.drain_timeout.as_millis() as u64);
        current.route_bind_relay_timeout_ms =
            Some(self.route_bind_relay_timeout_for(id).as_millis() as u64);
        current.registration_channel =
            approved_registration_channel.unwrap_or(registration_channel);
        let mut fields = entry_pending_fields(&current, &next, applies_configuration);
        // The caller passes the publication it already read for the whole
        // list, so a status of many modules reads it once. With no settings
        // owner, or one reporting an unstamped install, nothing is pending.
        // An unreadable publication keeps the field, as the safer report.
        let unstamped = match &self.config_runtime {
            None => true,
            Some(_) => publication.is_some_and(|view| {
                matches!(
                    view.approved_daemon,
                    crate::config_gate::runtime::ApprovedDaemonView::NotStamped
                )
            }),
        };
        if unstamped {
            fields.retain(|field| field.field != "registration_channel");
        }
        Ok(fields)
    }
}

/// The comparison a rescan makes between the configured modules and the
/// supervised ones, before anything is applied.
pub(super) struct RescanPlan {
    pub(super) current: BTreeMap<String, CurrentModule>,
    pub(super) configured: BTreeMap<String, ConfiguredModule>,
    pub(super) added: Vec<String>,
    pub(super) removed: Vec<String>,
    /// Modules whose launch spec changes. Their running process keeps the old
    /// one until it restarts.
    pub(super) changed_pending_reload: Vec<String>,
    pub(super) changed_pending: Vec<subc_control::SupervisorPendingModule>,
    /// Modules whose launch spec or health policy changes.
    pub(super) configuration_changes: BTreeSet<String>,
    pub(super) enabled_changes: BTreeSet<String>,
    pub(super) unchanged: u32,
}

impl RescanPlan {
    pub(super) fn preview_result(&self) -> SupervisorRescanResult {
        SupervisorRescanResult {
            added: self.added.clone(),
            removed: self.removed.clone(),
            changed_pending_reload: self.changed_pending_reload.clone(),
            changed_pending: self.changed_pending.clone(),
            enabled_changes: self.enabled_changes.iter().cloned().collect(),
            unchanged: self.unchanged,
            preview: true,
            // Filled by the caller on both paths, so the preview reports
            // restart-required sections identically to an executed rescan --
            // the preview is where an operator is most likely to be looking.
            restart_required: Vec::new(),
            capability_warnings: Vec::new(),
            module_actions: Vec::new(),
            earlier_pending: Vec::new(),
            expect: None,
        }
    }
}

/// The configuration path `server.describe` reports: the path the daemon
/// reads, unchanged, or nothing when that path is relative. A relative path
/// resolves against the daemon's working directory, so a client reading it
/// would find a different file. A path that is not valid UTF-8 is left out
/// too, because the reply carries it as a JSON string.
pub(super) fn reported_config_path(path: &Path) -> Option<PathBuf> {
    (path.is_absolute() && path.to_str().is_some()).then(|| path.to_path_buf())
}

fn sha256_hex(bytes: &[u8]) -> String {
    Sha256::digest(bytes)
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn duration_ms(duration: Duration) -> u64 {
    u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
}

/// The parts of a module's entry that differ between its staged state and the
/// configured entry, named as `RescanModuleAction::changed` documents.
fn changed_parts(
    current_spec: &ModuleSpec,
    current_health: &HealthConfig,
    configured: &ConfiguredModule,
) -> Vec<String> {
    let next = configured.module_spec();
    [
        ("program", current_spec.program != next.program),
        ("arguments", current_spec.args != next.args),
        ("environment", current_spec.env != next.env),
        ("protocol", current_spec.protocol != next.protocol),
        ("reserved", current_spec.reserved != next.reserved),
        (
            "reserved_prefixes",
            current_spec.reserved_prefixes != next.reserved_prefixes,
        ),
        ("overlap", current_spec.overlap != next.overlap),
        (
            "carry_containment",
            current_spec.carry_containment != next.carry_containment,
        ),
        ("health", *current_health != configured.health),
    ]
    .into_iter()
    .filter(|(_, changed)| *changed)
    .map(|(part, _)| part.to_string())
    .collect()
}

impl ControlHandler {
    /// The capabilities `server.describe` lists. `supervisor.rescan_checked`
    /// is announced only by a daemon that has a config file to rescan; any
    /// other daemon refuses the op as `rescan_unavailable`.
    pub(super) fn describe_capabilities(&self) -> Vec<String> {
        let mut capabilities = self.capabilities_with_settings();
        if self.rescan.is_some() {
            capabilities.push(RESCAN_CANDIDATE_PREVIEW_CAPABILITY.to_string());
        }
        capabilities
    }

    fn staged_generation(&self, context: &SupervisorRescanContext) -> StagedGeneration {
        StagedGeneration {
            incarnation: self.supervisor.spawn_snapshot().cursor.daemon_incarnation,
            counter: context.generation.load(Ordering::SeqCst),
        }
    }

    pub(super) async fn handle_supervisor_rescan_checked(
        &self,
        ctx: &RouteCtx,
        frame: Frame,
        preview: bool,
        candidate: Option<String>,
        expect: Option<RescanExpect>,
    ) -> Result<Vec<Frame>, RouterError> {
        let corr = frame.header.corr;
        let mode = match (preview, candidate, expect) {
            (true, Some(candidate), None) => {
                if candidate.len() > RESCAN_CANDIDATE_MAX_BYTES {
                    return Ok(vec![control_error_frame(
                        &frame,
                        RESCAN_CANDIDATE_TOO_LARGE,
                        format!(
                            "the candidate config is {} bytes; a preview accepts at most {RESCAN_CANDIDATE_MAX_BYTES}",
                            candidate.len()
                        ),
                    )?]);
                }
                RescanMode::Candidate(candidate)
            }
            (false, None, Some(expect)) => {
                self.log_supervisor_request_received(
                    ctx,
                    corr,
                    ops::SUPERVISOR_RESCAN_CHECKED,
                    None,
                    None,
                )?;
                RescanMode::Checked(expect)
            }
            _ => {
                return Ok(vec![control_error_frame(
                    &frame,
                    "invalid_control_body",
                    "supervisor.rescan_checked takes either preview: true with a candidate and \
                     no expect, or preview: false with an expect and no candidate",
                )?]);
            }
        };
        let requester = self.supervisor_requester(ctx, corr)?;
        self.handle_supervisor_rescan(frame, mode, requester).await
    }

    // The caller holds the rescan lock through both read and apply. Take the
    // supervisor operation lock only after the read so other operations can
    // proceed during file I/O. Check the staged generation for changes made
    // outside the rescan lock, then build the plan from current module state.
    pub(super) async fn rescan_input(
        &self,
        mode: &RescanMode,
        context: &SupervisorRescanContext,
    ) -> Result<RescanInput, RescanInputError> {
        use tokio::time::{timeout_at, Instant};
        use RescanInputError::Config;

        let staged = self.staged_generation(context);
        let (config, approved_preview) = if matches!(mode, RescanMode::Candidate(_)) {
            (Self::read_rescan_config(mode, &context.config_path)?, None)
        } else {
            let deadline = Instant::now() + CONFIG_READ_DEADLINE;
            let timed_out = || {
                Config(
                    "rescan_config_read_timeout",
                    format!(
                        "supervisor rescan timed out reading daemon config at {}; nothing was applied",
                        context.config_path.display()
                    ),
                )
            };
            let permit = timeout_at(deadline, CONFIG_READ_SLOTS.acquire())
                .await
                .map_err(|_| timed_out())?
                .expect("rescan read slots never close");
            if Instant::now() >= deadline {
                return Err(timed_out());
            }
            let path = context.config_path.clone();
            let owner = if matches!(mode, RescanMode::Preview) {
                self.config_runtime.clone()
            } else {
                None
            };
            let mode = mode.clone();
            let (send, recv) = tokio::sync::oneshot::channel();
            std::thread::Builder::new()
                .name("rescan-config-read".into())
                .spawn(move || {
                    let _permit = permit;
                    let result = (|| {
                        let config = Self::read_rescan_config(&mode, &path)?;
                        // Preview can record a file proposal, but only the caller
                        // (after the deadline and generation checks) can apply a
                        // rescan. The worker has no supervisor handle.
                        let approved = if let Some(owner) = owner.filter(|_| !send.is_closed()) {
                            owner
                                .observe_file_proposal()
                                .map_err(RescanInputError::Gate)?;
                            owner
                                .approved_daemon_configuration()
                                .map_err(RescanInputError::Gate)?
                        } else {
                            None
                        };
                        Ok((config, approved))
                    })();
                    let _ = send.send(result);
                })
                .map_err(|error| {
                    Config(
                        "rescan_failed",
                        format!("could not start supervisor rescan config read: {error}"),
                    )
                })?;
            let result = timeout_at(deadline, recv).await.map_err(|_| timed_out())?;
            // Tokio polls the result before the timer when both are ready.
            // Never apply a read after its deadline, even in that race.
            if Instant::now() >= deadline {
                return Err(timed_out());
            }
            result.map_err(|error| {
                Config(
                    "rescan_failed",
                    format!("supervisor rescan config reader stopped: {error}"),
                )
            })??
        };

        let guard = self.supervisor.operation_lock().lock_owned().await;
        let current = self.staged_generation(context);
        let changed = match mode {
            RescanMode::Candidate(_) => None,
            RescanMode::Checked(expect) if current != expect.staged_generation => Some((
                CONFIG_CHANGED_SINCE_PREVIEW,
                &expect.staged_generation,
                "after the preview",
            )),
            _ if current != staged => {
                Some(("rescan_failed", &staged, "while reading daemon config"))
            }
            _ => None,
        };
        if let Some((code, before, reason)) = changed {
            return Err(Config(code, format!(
                "the daemon's staged configuration changed {reason} (was {}:{}, now {}:{}); nothing was applied",
                before.incarnation, before.counter, current.incarnation, current.counter,
            )));
        }
        Ok(RescanInput {
            config,
            approved_preview,
            guard,
        })
    }

    fn read_rescan_config(
        mode: &RescanMode,
        path: &Path,
    ) -> Result<Option<(DaemonConfig, ActivityConfig)>, RescanInputError> {
        let rejected = |err: crate::daemon_config::DaemonConfigError| {
            RescanInputError::Config(
                "invalid_daemon_config",
                format!("supervisor rescan rejected daemon config: {err}"),
            )
        };
        match mode {
            RescanMode::Execute | RescanMode::Preview => {
                crate::daemon_config::load_with_activity(path).map_err(rejected)
            }
            RescanMode::Candidate(candidate) => {
                crate::daemon_config::parse_with_activity(candidate, path)
                    .map(Some)
                    .map_err(rejected)
            }
            RescanMode::Checked(expect) => {
                let doc = Self::checked_config_doc(path, expect)?;
                crate::daemon_config::parse_with_activity(&doc, path)
                    .map(Some)
                    .map_err(rejected)
            }
        }
    }

    /// Read and hash the bytes that will be parsed, so a write landing after
    /// the hash check cannot be what gets applied.
    fn checked_config_doc(path: &Path, expect: &RescanExpect) -> Result<String, RescanInputError> {
        use RescanInputError::Config;
        let bytes = match std::fs::read(path) {
            Ok(bytes) => bytes,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                return Err(Config(
                    CONFIG_CHANGED_SINCE_PREVIEW,
                    format!(
                        "daemon config not found at {}; nothing was applied",
                        path.display()
                    ),
                ));
            }
            Err(err) => {
                return Err(Config(
                    "invalid_daemon_config",
                    format!(
                        "supervisor rescan could not read daemon config at {}: {err}",
                        path.display()
                    ),
                ));
            }
        };
        if !sha256_hex(&bytes).eq_ignore_ascii_case(&expect.config_sha256) {
            return Err(Config(
                CONFIG_CHANGED_SINCE_PREVIEW,
                format!(
                    "daemon config at {} is not the text that was previewed; nothing was applied",
                    path.display(),
                ),
            ));
        }
        // The hash matched text that arrived as a JSON string, so this only
        // fails on a hash collision; refuse rather than assume.
        String::from_utf8(bytes).map_err(|err| {
            Config(
                "invalid_daemon_config",
                format!("daemon config at {} is not UTF-8: {err}", path.display()),
            )
        })
    }

    /// The preview of `candidate` (already parsed into `modules`): the plain
    /// rescan preview plus what applying it would do to each module, the
    /// modules still waiting for a restart from an earlier rescan, and the
    /// `expect` an apply must send. Applies nothing.
    pub(super) fn preview_candidate(
        &self,
        context: &SupervisorRescanContext,
        modules: Vec<ConfiguredModule>,
        candidate: &str,
    ) -> Result<SupervisorRescanResult, String> {
        let plan = self.plan_rescan(modules)?;
        let mut result = plan.preview_result();
        let default_drain_ms = duration_ms(context.supervisor.default_drain_timeout());
        let module_ids = plan
            .current
            .keys()
            .chain(plan.configured.keys())
            .collect::<BTreeSet<_>>();
        let mut drained = BTreeSet::new();
        for module_id in module_ids {
            let current = plan.current.get(module_id);
            let configured = plan.configured.get(module_id);
            let (action, changed) = match (current, configured) {
                (None, Some(configured)) if configured.enabled => {
                    (RescanModuleActionKind::Start, Vec::new())
                }
                (Some((_, _, _, true)), None) => (RescanModuleActionKind::Stop, Vec::new()),
                (Some((_, _, _, enabled)), Some(configured)) if *enabled != configured.enabled => {
                    if configured.enabled {
                        (RescanModuleActionKind::Start, Vec::new())
                    } else {
                        (RescanModuleActionKind::Stop, Vec::new())
                    }
                }
                (Some((_, spec, health, enabled)), Some(configured))
                    if plan.configuration_changes.contains(module_id) =>
                {
                    let changed = changed_parts(spec, health, configured);
                    // A rescan stores new launch inputs without touching the
                    // running process, so only a module that is enabled needs
                    // a restart to use them. A disabled one starts with them
                    // whenever it is enabled.
                    if *enabled && plan.changed_pending_reload.contains(module_id) {
                        (RescanModuleActionKind::Restart, changed)
                    } else {
                        (RescanModuleActionKind::Reload, changed)
                    }
                }
                _ => continue,
            };
            let mut entry = RescanModuleAction {
                module_id: module_id.clone(),
                action,
                changed,
                drain_timeout_ms: None,
                busy_gauges: Vec::new(),
            };
            if matches!(
                entry.action,
                RescanModuleActionKind::Restart | RescanModuleActionKind::Stop
            ) {
                if let Some((module, ..)) = current {
                    let status = module.status().map_err(|err| {
                        format!("failed to read status for module_id '{module_id}': {err}")
                    })?;
                    // A rescan that changes the entry also installs its drain
                    // budget before anything drains; otherwise the module
                    // keeps the budget it runs with now.
                    entry.drain_timeout_ms = Some(match configured {
                        Some(configured) if plan.configuration_changes.contains(module_id) => {
                            configured.drain_timeout_ms.unwrap_or(default_drain_ms)
                        }
                        _ => duration_ms(status.drain_timeout),
                    });
                    let metrics = status
                        .health
                        .metrics
                        .as_ref()
                        .and_then(serde_json::Value::as_object);
                    entry.busy_gauges =
                        crate::supervise::declared_busy_gauges(&self.registry, module_id)
                            .map_err(|err| {
                                format!(
                                    "failed to read busy gauges for module_id '{module_id}': {err}"
                                )
                            })?
                            .into_iter()
                            .map(|gauge| BusyGaugeReading {
                                value: metrics.and_then(|metrics| metrics.get(&gauge)).cloned(),
                                gauge,
                            })
                            .collect();
                }
                drained.insert(module_id.clone());
            }
            result.module_actions.push(entry);
        }
        for (module_id, (module, spec, _, _)) in &plan.current {
            if drained.contains(module_id) {
                continue;
            }
            let status = module.status().map_err(|err| {
                format!("failed to read status for module_id '{module_id}': {err}")
            })?;
            if launch_spec_agreement(
                spec,
                status.process_alive,
                status.launch_spec_digest.as_ref(),
            ) == LaunchSpecAgreement::Mismatch
            {
                result.earlier_pending.push(module_id.clone());
            }
        }
        result.expect = Some(RescanExpect {
            config_sha256: sha256_hex(candidate.as_bytes()),
            staged_generation: self.staged_generation(context),
        });
        Ok(result)
    }
}

#[cfg(all(test, unix))]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stalled_config_read_times_out_without_holding_operation_lock() {
    use crate::{Registry, RestartPolicy, Supervisor, SupervisorHandle};
    use cortexkit_test_support::ScratchDir;
    use std::io::Write;
    use std::sync::{mpsc, Arc};
    use subc_protocol::{FrameType, Principal};

    let dir = ScratchDir::new("rescan-stalled-file");
    let path = dir.join("subc.jsonc");
    assert!(std::process::Command::new("mkfifo")
        .arg(&path)
        .status()
        .unwrap()
        .success());
    let registry = Arc::new(Registry::default());
    let handle = SupervisorHandle::new();
    let supervisor =
        Supervisor::new(registry.clone(), RestartPolicy::default()).with_handle(handle.clone());
    let handler = Arc::new(
        ControlHandler::new(registry)
            .with_supervisor(handle.clone())
            .with_supervisor_rescan(supervisor, &path, None),
    );
    let (opened, is_open) = tokio::sync::oneshot::channel();
    let (release, released) = mpsc::channel();
    let feeder = std::thread::spawn(move || {
        let mut fifo = std::fs::OpenOptions::new().write(true).open(path).unwrap();
        opened.send(()).unwrap();
        released.recv_timeout(Duration::from_secs(10)).unwrap();
        let _ = fifo.write_all(br#"{"version":1,"modules":{"never-applied":{"program":"/bin/true","protocol":"none","enabled":false}}}"#);
    });
    let request = tokio::spawn({
        let handler = Arc::clone(&handler);
        async move {
            let frame = Frame::build(
                FrameType::Request,
                super::control_flags(),
                0,
                0,
                1,
                Vec::new(),
            )
            .unwrap();
            handler
                .handle_supervisor_rescan(frame, RescanMode::Execute, Principal::Direct)
                .await
                .unwrap()
                .remove(0)
        }
    });
    tokio::time::timeout(Duration::from_secs(2), is_open)
        .await
        .unwrap()
        .unwrap();

    // Restart takes the operation lock even for an unknown module. Receive
    // that ordinary refusal while the reader is still waiting for file data.
    let frame = Frame::build(
        FrameType::Request,
        super::control_flags(),
        0,
        0,
        2,
        Vec::new(),
    )
    .unwrap();
    let restart = tokio::time::timeout(
        Duration::from_secs(1),
        handler.handle_supervisor_restart(frame, "missing".into(), None, Principal::Direct),
    )
    .await;
    let mut request = request;
    let reply = tokio::time::timeout(Duration::from_secs(6), &mut request).await;
    release.send(()).unwrap();
    feeder.join().unwrap();
    let restart = restart
        .expect("the rescan held the supervisor operation lock during its config read")
        .unwrap()
        .remove(0);
    let body: serde_json::Value = serde_json::from_slice(&restart.body).unwrap();
    assert_eq!(body["code"], "unknown_module");
    let reply = reply
        .expect("rescan did not answer before the config reader was released")
        .unwrap();
    assert_eq!(reply.header.ty, FrameType::Error);
    let body: serde_json::Value = serde_json::from_slice(&reply.body).unwrap();
    assert_eq!(body["code"], "rescan_config_read_timeout");
    assert!(body["message"].as_str().unwrap().contains("subc.jsonc"));
    assert!(handle.list().is_empty());
    assert_eq!(
        handler
            .rescan
            .as_ref()
            .unwrap()
            .generation
            .load(Ordering::SeqCst),
        0
    );
}

#[derive(Debug, Clone)]
pub(super) struct SupervisorRescanContext {
    pub(super) supervisor: Supervisor,
    pub(super) config_path: PathBuf,
    pub(super) configured_port: Option<u16>,
    pub(super) storage_config: Option<crate::daemon_config::StorageConfig>,
    pub(super) admission_facts_carrier_module_id: Option<String>,
    pub(super) admission_facts_targets: Option<Vec<String>>,
    pub(super) scope_authority_owners: Vec<String>,
    federated_scope_owner: String,
    pub(super) privileged_operator_prompt: PrivilegedOperatorPrompt,
    /// Counts executed rescans that set out to change the supervised modules'
    /// staged or applied configuration, counted before the first change so a
    /// rescan that fails partway still counts. With the daemon incarnation it
    /// names the state a `supervisor.rescan_checked` preview was computed
    /// against.
    generation: Arc<std::sync::atomic::AtomicU64>,
    rescan_lock: Arc<tokio::sync::Mutex<()>>,
    pub(super) module_entries: Arc<Mutex<rescan_checked::ModuleEntries>>,
}

impl ControlHandler {
    pub fn with_supervisor_rescan(
        mut self,
        supervisor: Supervisor,
        config_path: impl Into<PathBuf>,
        configured_port: Option<u16>,
    ) -> Self {
        self.rescan = Some(SupervisorRescanContext {
            supervisor,
            config_path: config_path.into(),
            configured_port,
            storage_config: self.storage_config.clone(),
            admission_facts_carrier_module_id: self.admission_facts_carrier_module_id.clone(),
            admission_facts_targets: self.admission_facts_targets.clone(),
            scope_authority_owners: self.scope_authority_owners.clone(),
            federated_scope_owner: self.federated_scope_owner.clone(),
            privileged_operator_prompt: self.privileged_operator_prompt,
            generation: Arc::default(),
            rescan_lock: Arc::default(),
            module_entries: Arc::default(),
        });
        self
    }

    pub(crate) fn with_supervisor_rescan_entries(
        self,
        modules: &[crate::daemon_config::ConfiguredModule],
    ) -> Self {
        if let Some(context) = &self.rescan {
            let mut entries = context
                .module_entries
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            entries.seed(modules);
        }
        self
    }
}

impl ControlHandler {
    /// `supervisor.rescan` and `supervisor.rescan_checked`. `mode` says where
    /// the configuration comes from and whether to apply it; every mode then
    /// runs the same comparison, so a preview describes the apply it previews.
    pub(super) async fn handle_supervisor_rescan(
        &self,
        frame: Frame,
        mode: RescanMode,
        requester: Principal,
    ) -> Result<Vec<Frame>, RouterError> {
        let preview = mode.previews();
        let Some(context) = self.rescan.clone() else {
            return Ok(vec![control_error_frame(
                &frame,
                "rescan_unavailable",
                "the daemon was not started with a reloadable config path".to_string(),
            )?]);
        };

        let _rescan_guard = context.rescan_lock.lock().await;
        let rescan_checked::RescanInput {
            config: loaded,
            approved_preview,
            guard: _operation_guard,
        } = match self.rescan_input(&mode, &context).await {
            Ok(input) => input,
            Err(error) => return Ok(vec![error.into_frame(&frame)?]),
        };
        // `load` reports a missing file as Ok(None), which is correct at boot
        // (no config, nothing to supervise) and catastrophic here: rescan treats
        // "not in the config" as "remove it", so an absent file would read as an
        // empty module list and retire the entire running fleet. An editor
        // writing via write-new-then-rename, or a half-finished edit, is enough
        // to open that window. Refuse instead: a config that cannot be read
        // carries no instruction to remove anything.
        let Some((config, activity_config)) = loaded else {
            return Ok(vec![control_error_frame(
                &frame,
                "invalid_daemon_config",
                format!(
                    "daemon config not found at {}; refusing to rescan (an absent config would \
                     retire every supervised module)",
                    context.config_path.display()
                ),
            )?]);
        };
        let (
            configured_port,
            storage_config,
            admission_facts_carrier_module_id,
            admission_facts_targets,
            scope_authority_owners,
            federated_scope_owner,
            privileged_operator_prompt,
            modules,
            reserved_capabilities,
        ) = (
            config.port,
            config.storage,
            config.admission_facts_carrier_module_id,
            config.admission_facts_targets,
            config.scope_authority_owners,
            config.federated_scope_owner,
            config.privileged_operator_prompt,
            config.modules,
            config.reserved_capabilities,
        );

        // Collect the sections rescan cannot apply, so the REPLY carries them.
        //
        // The warning below has always been correct and has always gone only to
        // the journal -- addressed to whoever reads logs, while the person who
        // just edited the config is looking at the CLI. Naming each section
        // individually rather than setting a flag: "something outside modules
        // changed" sends the operator back to diffing their own file, which is
        // the work this is meant to save.
        let mut restart_required = Vec::new();
        for section in RestartRequiredSection::ALL {
            let changed = match section {
                RestartRequiredSection::Port => configured_port != context.configured_port,
                RestartRequiredSection::Storage => storage_config != context.storage_config,
                RestartRequiredSection::AdmissionFactsCarrierModuleId => {
                    admission_facts_carrier_module_id != context.admission_facts_carrier_module_id
                }
                RestartRequiredSection::AdmissionFactsTargets => {
                    admission_facts_targets != context.admission_facts_targets
                }
                RestartRequiredSection::ScopeAuthorityOwners => {
                    scope_authority_owners != context.scope_authority_owners
                }
                RestartRequiredSection::FederatedScopeOwner => {
                    federated_scope_owner != context.federated_scope_owner
                }
                RestartRequiredSection::PrivilegedOperatorPrompt => {
                    privileged_operator_prompt != context.privileged_operator_prompt
                }
            };
            if changed {
                restart_required.push(section.label().to_string());
            }
        }
        if !restart_required.is_empty() {
            warn!(
                config_path = %context.config_path.display(),
                sections = %restart_required.join(", "),
                "daemon config changed outside the modules section; restart the daemon to apply those changes"
            );
        }

        for configured in &modules {
            if let Err(err) = validate_spec(&configured.module_spec()) {
                return Ok(vec![control_error_frame(
                    &frame,
                    "invalid_daemon_config",
                    format!("supervisor rescan rejected daemon config: {err}"),
                )?]);
            }
        }

        let configured_capabilities = modules
            .iter()
            .map(|module| (module.module_id.clone(), module.enabled))
            .collect::<Vec<_>>();
        let preview_capability_warnings = if preview {
            let (_, registrations) = self.runtime_capability_snapshot()?;
            let current_modules = self
                .supervisor
                .list()
                .into_iter()
                .map(|module| module.module_id().to_string())
                .collect::<BTreeSet<_>>();
            let resulting_modules = configured_capabilities.clone();
            let removed = current_modules
                .into_iter()
                .filter(|module_id| {
                    !resulting_modules
                        .iter()
                        .any(|(configured_id, _)| configured_id == module_id)
                })
                .collect::<Vec<_>>();
            // A candidate preview also names providers the candidate disables.
            // `supervisor.rescan`'s own preview keeps its removal-only warnings.
            let disabled = match &mode {
                RescanMode::Candidate(_) => resulting_modules
                    .iter()
                    .filter(|(_, enabled)| !enabled)
                    .map(|(module_id, _)| module_id.clone())
                    .collect::<Vec<_>>(),
                _ => Vec::new(),
            };
            self.capability_evaluator.preview_warnings(
                resulting_modules,
                &removed,
                &disabled,
                &registrations,
            )
        } else {
            Vec::new()
        };
        let reconciled = match (&mode, approved_preview.as_ref()) {
            (RescanMode::Preview, Some(approved)) => Ok((
                config_gate::preview_approved_daemon(approved, &modules),
                Vec::new(),
            )),
            (RescanMode::Candidate(candidate), _) => self
                .preview_candidate(&context, modules, candidate)
                .map(|result| (result, Vec::new())),
            _ => {
                self.reconcile_supervised_modules(
                    &context.supervisor,
                    modules,
                    preview,
                    requester.clone(),
                )
                .await
            }
        };
        let (result, changed) = match reconciled {
            Ok(result) => result,
            Err(message) => {
                return Ok(vec![control_error_frame(&frame, "rescan_failed", message)?])
            }
        };
        if !preview {
            self.rescan_policy_root(config.agent_policy_root).await;
            if let Some(ledger) = &self.activity {
                ledger.module_rescanned(&result.added, &result.removed, requester.clone());
                ledger.config_changed(&result.added, &result.removed, &changed, requester);
            }
            if let Err(code) = self.configure_activity(activity_config).await {
                warn!(
                    code,
                    "activity retention could not be applied during rescan"
                );
            }
            self.capability_evaluator
                .configure(configured_capabilities, reserved_capabilities);
            self.capability_evaluator.wake_deadline_loop();
            self.refresh_capability_requirements();
        }
        let mut result = result;
        result.restart_required = restart_required;
        result.capability_warnings = preview_capability_warnings;
        let response = ClientControlResponse::SupervisorRescan { result };
        Ok(vec![control_response_body_frame(
            &frame,
            &response,
            "ClientControlResponse::SupervisorRescan",
        )?])
    }
}

impl ControlHandler {
    /// Compare the configured module set with the supervised one, changing
    /// nothing. The staged spec each supervised module holds is what the
    /// configured entry is compared with.
    fn plan_rescan(
        &self,
        configured_modules: Vec<crate::daemon_config::ConfiguredModule>,
    ) -> Result<RescanPlan, String> {
        let mut current = BTreeMap::new();
        for module in self.supervisor.list() {
            let (spec, health) = module.configuration().map_err(|err| {
                format!(
                    "failed to read configuration for module_id '{}': {err}",
                    module.module_id()
                )
            })?;
            let enabled = module
                .status()
                .map_err(|err| {
                    format!(
                        "failed to read status for module_id '{}': {err}",
                        module.module_id()
                    )
                })?
                .enabled;
            current.insert(
                module.module_id().to_string(),
                (module, spec, health, enabled),
            );
        }
        let configured = configured_modules
            .into_iter()
            .map(|module| (module.module_id.clone(), module))
            .collect::<BTreeMap<_, _>>();

        let added = configured
            .keys()
            .filter(|module_id| !current.contains_key(*module_id))
            .cloned()
            .collect::<Vec<_>>();
        let removed = current
            .keys()
            .filter(|module_id| !configured.contains_key(*module_id))
            .cloned()
            .collect::<Vec<_>>();
        let mut changed_pending_reload = Vec::new();
        let mut changed_pending = Vec::new();
        let mut configuration_changes = BTreeSet::new();
        let mut enabled_changes = BTreeSet::new();
        let mut unchanged = 0_u32;
        let publication = self
            .config_runtime
            .as_ref()
            .and_then(|owner| owner.polled_snapshot().ok());

        for (module_id, configured_module) in &configured {
            let Some((module, current_spec, current_health, current_enabled)) =
                current.get(module_id)
            else {
                continue;
            };
            // Compare the whole launch spec so a future launch field cannot
            // accidentally become a live-only policy change. Health is stored
            // separately and applies live without replacing the process.
            let launch_changed = *current_spec != configured_module.module_spec();
            let configuration_changed =
                launch_changed || *current_health != configured_module.health;
            let pending = self.pending_module_fields(
                module,
                Some(configured_module),
                configuration_changed,
                Self::registration_channel_in_force(
                    publication
                        .as_ref()
                        .and_then(|view| view.registration_declaration(module_id))
                        .as_deref(),
                    publication
                        .as_ref()
                        .and_then(|view| match &view.approved_daemon {
                            crate::config_gate::runtime::ApprovedDaemonView::Approved {
                                config,
                                ..
                            } => config
                                .modules
                                .iter()
                                .find(|entry| entry.module_id == *module_id)
                                .map(|entry| entry.registration_channel),
                            _ => None,
                        }),
                ),
                publication.as_deref(),
            )?;
            let has_pending = !pending.is_empty();
            if has_pending {
                changed_pending.push(subc_control::SupervisorPendingModule {
                    module_id: module_id.clone(),
                    fields: pending
                        .into_iter()
                        .map(|mut field| {
                            field.value = None;
                            field
                        })
                        .collect(),
                });
            }
            // Applying a rescan also replaces the recorded config value, even
            // when an operator already put the runtime in the requested state.
            let enabled_changed = *current_enabled != configured_module.enabled
                || self
                    .capability_evaluator
                    .configured_enabled(module_id)
                    .is_some_and(|enabled| enabled != configured_module.enabled);
            if configuration_changed {
                configuration_changes.insert(module_id.clone());
            }
            if launch_changed {
                changed_pending_reload.push(module_id.clone());
            }
            if enabled_changed {
                enabled_changes.insert(module_id.clone());
            }
            if !configuration_changed && !enabled_changed && !has_pending {
                unchanged = unchanged.saturating_add(1);
            }
        }
        Ok(RescanPlan {
            current,
            configured,
            added,
            removed,
            changed_pending_reload,
            changed_pending,
            configuration_changes,
            enabled_changes,
            unchanged,
        })
    }

    /// Reconcile the running module set against the configured one.
    ///
    /// With `preview` set, the diff is computed and returned WITHOUT applying any
    /// of it: nothing is retired, reconfigured, enabled or spawned. The preview
    /// deliberately shares this function with the executing path rather than
    /// computing the same diff somewhere else -- two implementations of one
    /// decision agree until they do not, and the whole value of a preview is that
    /// it describes the operation that will actually run.
    pub(super) async fn reconcile_supervised_modules(
        &self,
        supervisor: &Supervisor,
        configured_modules: Vec<crate::daemon_config::ConfiguredModule>,
        preview: bool,
        requester: Principal,
    ) -> Result<(SupervisorRescanResult, Vec<String>), String> {
        let plan = self.plan_rescan(configured_modules)?;

        // Everything above this point is pure computation over two snapshots.
        // Everything below MUTATES. The preview returns here so the boundary is a
        // single early return rather than a condition repeated at each mutation
        // site, where one missed guard would apply part of a change the caller was
        // told would not happen.
        if preview {
            return Ok((plan.preview_result(), Vec::new()));
        }
        let changed = plan
            .configuration_changes
            .union(&plan.enabled_changes)
            .cloned()
            .collect::<Vec<_>>();
        let RescanPlan {
            current,
            configured,
            added,
            removed,
            changed_pending_reload,
            changed_pending,
            configuration_changes,
            enabled_changes,
            unchanged,
        } = plan;
        // The staged generation moves before the first change rather than
        // after the last, so a rescan that fails partway still invalidates
        // every earlier `supervisor.rescan_checked` preview: part of its diff
        // may have run.
        if !(added.is_empty()
            && removed.is_empty()
            && configuration_changes.is_empty()
            && changed_pending.is_empty()
            && enabled_changes.is_empty())
        {
            if let Some(context) = &self.rescan {
                context
                    .generation
                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            }
        }

        for module_id in &removed {
            let module = &current
                .get(module_id)
                .expect("removed module came from current supervisor state")
                .0;
            module
                .retire_requested(Some(requester.clone()))
                .await
                .map_err(|err| {
                    format!("failed to retire module_id '{module_id}' during rescan: {err}")
                })?;
            // TOMBSTONE BEFORE RETIRE, and the order is the whole fix.
            //
            // `handle_route_open` resolves an absent module in three steps:
            // registry, then supervisor status, then tombstone. Retiring first
            // opens a window where ALL THREE ARE ABSENT -- the registry entry
            // went with the teardown above, the supervisor entry went with
            // `retire`, and the tombstone does not exist yet -- so a route.open
            // landing in it gets `unknown_module` (RETRYABLE, "never heard of
            // it") for a module that was deliberately removed and whose caller
            // should get `module_removed` (TERMINAL, carrying a removal age).
            //
            // Writing the tombstone first closes it: during the window the
            // supervisor entry still answers, so the caller gets
            // `target_unavailable` -- retryable, and TRUE, because the module
            // is mid-teardown. After both statements it is `module_removed`.
            // No instant remains where a removed module reads as one that
            // never existed.
            //
            // NOT DETERMINISTICALLY TESTABLE FROM HERE, said plainly because
            // the absence of a test beside a fix invites deletion: these are
            // two sync statements with no await between them, so reaching the
            // window needs a second worker thread to land exactly between them
            // and there is no hook to force it. MEASURED: the 25 daemon_config
            // tests pass identically with the old order and the new one, so
            // the existing suite cannot see this and a green run is not
            // evidence either way. What the suite does hold is the
            // post-condition -- a removed module answers `module_removed` --
            // which this preserves.
            //
            // Found by an Athena panel reading the shipped tree against a
            // design note (2026-09-19), as the one concrete instance of that
            // note's class that survived contact with source. Direction is
            // benign: retryable where terminal was intended, never the reverse.
            self.supervisor.record_rescan_removal(module_id);
            self.supervisor.retire(module_id);
            self.route_outages.forget(module_id);
        }

        for module_id in configured.keys() {
            let Some((module, _, _, current_enabled)) = current.get(module_id) else {
                continue;
            };
            let configured_module = configured
                .get(module_id)
                .expect("configured module id came from configured map");
            if configuration_changes.contains(module_id) {
                module
                    .update_configuration(
                        configured_module.module_spec(),
                        configured_module.health.clone(),
                        configured_module.drain_timeout_ms,
                    )
                    .await
                    .map_err(|err| {
                        format!(
                            "failed to update module_id '{module_id}' configuration during rescan: {err}"
                        )
                    })?;
            }
            // Config-only drift is applied by the evaluator's config update;
            // do not turn it into a runtime start or stop.
            if *current_enabled != configured_module.enabled {
                // A rescan that starts or stops a module applies an operator's
                // edit to the config, so the resulting outage was asked for.
                self.route_outages.mark_operator_action(module_id);
                module
                    .set_enabled_requested(configured_module.enabled, Some(requester.clone()))
                    .await
                    .map_err(|err| {
                        self.route_outages.operator_action_ended_unrefused(module_id);
                        format!(
                            "failed to apply module_id '{module_id}' enabled={} during rescan: {err}",
                            configured_module.enabled
                        )
                    })?;
            }
        }

        for module_id in &added {
            let configured_module = configured
                .get(module_id)
                .expect("added module id came from configured map");
            supervisor
                .supervise_configured_requested_async(
                    configured_module.module_spec(),
                    configured_module.enabled,
                    configured_module.health.clone(),
                    configured_module.drain_timeout_ms,
                    configured_module.restart,
                    configured_module.register_timeout,
                    Some(requester.clone()),
                )
                .await
                .map_err(|err| {
                    format!("failed to add module_id '{module_id}' during rescan: {err}")
                })?;
        }

        if let Some(context) = &self.rescan {
            let mut entries = context
                .module_entries
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            entries.record(&configured, &added, &removed);
        }

        Ok((
            SupervisorRescanResult {
                added,
                removed,
                changed_pending_reload,
                changed_pending,
                enabled_changes: enabled_changes.iter().cloned().collect(),
                unchanged,
                preview: false,
                // Filled by the caller, which is the only layer that can see the
                // previous config to diff against.
                restart_required: Vec::new(),
                capability_warnings: Vec::new(),
                module_actions: Vec::new(),
                earlier_pending: Vec::new(),
                expect: None,
            },
            changed,
        ))
    }
}