autumn-web 0.6.0

An opinionated, convention-over-configuration web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
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
//! ACME issuance, the renewal loop, the self-signed placeholder, and the
//! observability seam (issue #1608).
//!
//! At boot the TLS listener needs *a* certificate to bind `:443` immediately.
//! If a valid certificate is already stored it is served; otherwise a
//! short-lived self-signed placeholder ([`self_signed_placeholder`]) is served
//! so the port comes up, and the background renewal task
//! ([`AcmeRenewalTask::run`]) obtains the real certificate over HTTP-01 and
//! hot-swaps it into the SAME [`ReloadableCertResolver`](crate::tls::ReloadableCertResolver).
//!
//! The renewal loop is spawned unconditionally whenever ACME is configured (a
//! pure `web` replica must renew its own cert). It leader-elects through the
//! existing [`SchedulerCoordinator`](crate::scheduler::SchedulerCoordinator) so
//! that, across a fleet, only one replica orders per certificate.

use std::sync::{Arc, RwLock};
use std::time::Duration;

use rustls::crypto::CryptoProvider;

use crate::acme::challenge::Http01Tokens;
use crate::acme::store::{AcmeStore, CertId, StoredCert};
use crate::config::AcmeConfig;
use crate::scheduler::SchedulerCoordinator;
use crate::task::TaskCoordination;
use crate::tls::ReloadableCertResolver;

/// How often the renewal loop wakes to re-check expiry. An hour is frequent
/// enough to act well inside the (default 30-day) renew-before window while
/// costing almost nothing.
const RENEWAL_CHECK_INTERVAL: Duration = Duration::from_secs(3600);

/// The scheduler task name used for ACME renewal leader-election.
const RENEWAL_TASK_NAME: &str = "acme-renewal";

/// A per-failure callback invoked by the renewal task with a message.
///
/// The app wires this to the registered [`ErrorReporter`] chain (behind the
/// `reporting` feature); by default it is a no-op and failures still log via
/// `tracing` inside the loop.
///
/// [`ErrorReporter`]: crate::reporting::ErrorReporter
pub type ReporterFn = Arc<dyn Fn(String) + Send + Sync>;

/// Decide whether a certificate whose leaf expires at `not_after_unix` should be
/// renewed now.
///
/// Renews once fewer than `renew_before_days` of validity remain (and, of
/// course, once already expired). `now_unix` is injected so the decision is
/// deterministically unit-testable, mirroring `tls.rs`'s `now_unix` style.
#[must_use]
pub const fn needs_renewal(not_after_unix: i64, renew_before_days: u32, now_unix: i64) -> bool {
    let threshold = (renew_before_days as i64).saturating_mul(86_400);
    not_after_unix.saturating_sub(now_unix) < threshold
}

/// Generate a short-lived self-signed placeholder certificate covering
/// `domains` (CN = first domain), so `:443` can bind before the first real
/// issuance completes.
///
/// # Errors
///
/// Returns a message if certificate generation fails.
pub fn self_signed_placeholder(domains: &[String]) -> Result<StoredCert, String> {
    use rcgen::{CertificateParams, DistinguishedName, DnType, KeyPair};

    if domains.is_empty() {
        return Err("cannot build a placeholder certificate with no domains".to_owned());
    }
    let key_pair =
        KeyPair::generate().map_err(|e| format!("failed to generate placeholder key: {e}"))?;
    let mut params = CertificateParams::new(domains.to_vec())
        .map_err(|e| format!("failed to build placeholder params: {e}"))?;
    let mut dn = DistinguishedName::new();
    dn.push(DnType::CommonName, domains[0].clone());
    params.distinguished_name = dn;
    let cert = params
        .self_signed(&key_pair)
        .map_err(|e| format!("failed to self-sign placeholder: {e}"))?;
    Ok(StoredCert {
        chain_pem: cert.pem(),
        key_pem: key_pair.serialize_pem(),
    })
}

/// Live status of ACME provisioning, written by the renewal task and read by
/// [`AcmeHealthIndicator`]. Cheap to clone (an `Arc`).
#[derive(Clone, Default)]
pub struct AcmeStatus(Arc<RwLock<AcmeStatusInner>>);

#[derive(Default)]
struct AcmeStatusInner {
    last_success_unix: Option<i64>,
    last_failure: Option<(i64, String)>,
    cert_not_after_unix: Option<i64>,
}

/// An immutable snapshot of [`AcmeStatus`].
#[derive(Clone, Default)]
pub struct AcmeStatusSnapshot {
    /// UNIX time of the last successful issuance/renewal, if any.
    pub last_success_unix: Option<i64>,
    /// UNIX time and message of the last failure, if any.
    pub last_failure: Option<(i64, String)>,
    /// The currently served certificate's `notAfter`, if known.
    pub cert_not_after_unix: Option<i64>,
}

impl AcmeStatus {
    /// Create an empty status.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    fn write(&self) -> std::sync::RwLockWriteGuard<'_, AcmeStatusInner> {
        self.0
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Record a successful issuance/renewal serving a cert with the given expiry.
    pub fn record_success(&self, now_unix: i64, cert_not_after_unix: i64) {
        let mut inner = self.write();
        inner.last_success_unix = Some(now_unix);
        inner.cert_not_after_unix = Some(cert_not_after_unix);
        inner.last_failure = None;
    }

    /// Record a failed issuance/renewal attempt.
    pub fn record_failure(&self, now_unix: i64, message: impl Into<String>) {
        self.write().last_failure = Some((now_unix, message.into()));
    }

    /// Note the expiry of the certificate currently being served (e.g. the
    /// stored cert or placeholder loaded at boot).
    pub fn set_cert_not_after(&self, not_after_unix: i64) {
        self.write().cert_not_after_unix = Some(not_after_unix);
    }

    /// Take an immutable snapshot.
    #[must_use]
    pub fn snapshot(&self) -> AcmeStatusSnapshot {
        let inner = self
            .0
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        AcmeStatusSnapshot {
            last_success_unix: inner.last_success_unix,
            last_failure: inner.last_failure.clone(),
            cert_not_after_unix: inner.cert_not_after_unix,
        }
    }
}

/// A [`HealthIndicator`](crate::actuator::HealthIndicator) reporting ACME
/// provisioning health.
///
/// Registered in the `HealthOnly` group so a transient renewal failure surfaces
/// in `/actuator/health` WITHOUT failing `/ready` (a working, not-yet-expired
/// certificate is still being served). It grades `Down` only when a failure
/// coincides with real expiry danger — the served cert is inside its
/// renew-before window — otherwise `Up` with diagnostic details.
pub struct AcmeHealthIndicator {
    status: AcmeStatus,
    renew_before_days: u32,
    now_unix: fn() -> i64,
}

impl AcmeHealthIndicator {
    /// Build an indicator reading `status`, using `renew_before_days` to decide
    /// when a failure is dangerous.
    #[must_use]
    pub fn new(status: AcmeStatus, renew_before_days: u32) -> Self {
        Self {
            status,
            renew_before_days,
            now_unix: default_now_unix,
        }
    }

    /// Grade the current status against `now_unix` (pure; used by `check` and
    /// unit tests).
    #[must_use]
    pub fn grade(&self, now_unix: i64) -> crate::actuator::HealthCheckOutput {
        use crate::actuator::{HealthCheckOutput, HealthStatus};
        let snap = self.status.snapshot();
        let mut details = std::collections::HashMap::new();

        if let Some(not_after) = snap.cert_not_after_unix {
            let days = (not_after - now_unix) / 86_400;
            details.insert("days_until_expiry".to_owned(), serde_json::json!(days));
        }
        if let Some(ts) = snap.last_success_unix {
            details.insert("last_success_unix".to_owned(), serde_json::json!(ts));
        }
        if let Some((ts, msg)) = &snap.last_failure {
            details.insert("last_failure_unix".to_owned(), serde_json::json!(ts));
            details.insert("last_failure".to_owned(), serde_json::json!(msg));
        }

        // An already-expired served certificate is Down on its own, INDEPENDENTLY
        // of whether a renewal failure has been recorded. At boot
        // `build_acme_tls_listener` records the stored cert's `cert_not_after_unix`
        // and serves it while renewal runs; if leadership is skipped/degraded or
        // issuance is merely pending, `last_failure` stays `None` and the
        // failure-in-danger-window rule below would report `Up` for a TLS cert
        // that is already invalid. Treat `not_after <= now` as Down and say so.
        let cert_expired = snap
            .cert_not_after_unix
            .is_some_and(|not_after| not_after <= now_unix);
        if cert_expired {
            details.insert("cert_expired".to_owned(), serde_json::json!(true));
        }

        // Down ALSO when a failure coincides with real expiry danger: the served
        // certificate is already inside its renew-before window (or we have no
        // certificate at all). A failure with plenty of validity left is a blip.
        let in_danger = snap
            .cert_not_after_unix
            .is_none_or(|not_after| needs_renewal(not_after, self.renew_before_days, now_unix));
        let status = if cert_expired || (snap.last_failure.is_some() && in_danger) {
            HealthStatus::Down
        } else {
            HealthStatus::Up
        };
        HealthCheckOutput { status, details }
    }
}

impl crate::actuator::HealthIndicator for AcmeHealthIndicator {
    fn check(&self) -> futures::future::BoxFuture<'_, crate::actuator::HealthCheckOutput> {
        let now = (self.now_unix)();
        Box::pin(async move { self.grade(now) })
    }

    fn group(&self) -> crate::actuator::IndicatorGroup {
        crate::actuator::IndicatorGroup::HealthOnly
    }
}

fn default_now_unix() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX))
}

/// Everything the background renewal task needs to run.
///
/// Built by the app at bind time (after the initial resolver is constructed) and
/// spawned as a sibling of the main server under `server_shutdown`.
pub struct AcmeRenewalTask {
    /// The resolver the TLS listener serves; issued certs are swapped in here.
    pub resolver: Arc<ReloadableCertResolver>,
    /// The crypto provider (shared with the listener).
    pub provider: Arc<CryptoProvider>,
    /// Persistent store for the account + issued certificate.
    pub store: Arc<dyn AcmeStore>,
    /// The certificate id (hash of the sorted domain set).
    pub cert_id: CertId,
    /// The HTTP-01 token map shared with the `:80` challenge listener.
    pub tokens: Http01Tokens,
    /// Live status for the health indicator.
    pub status: AcmeStatus,
    /// Resolved ACME configuration.
    pub config: AcmeConfig,
    /// Whether a valid stored certificate is already being served (so the first
    /// tick can skip ordering unless it is due for renewal).
    pub serving_stored_cert: bool,
    /// Set when a *distributed* scheduler backend was configured (multi-replica
    /// intent) but this process could not build the distributed coordinator and
    /// fell back to a per-process in-process one. In that state we must NOT
    /// order: every replica would grab its OWN local lease and order the SAME
    /// certificate concurrently, racing the per-process HTTP-01 token maps and
    /// local stores and burning the CA's rate limits. Instead each cycle records
    /// an ACME failure and keeps serving the existing/placeholder cert until
    /// leadership is restored. Never set for a genuinely single-replica
    /// deployment (`scheduler.backend = in_process` → in-process is correct).
    pub leadership_degraded: bool,
    /// Runtime backstop against a re-renewal loop that burns CA rate limits.
    ///
    /// Set once a freshly-issued certificate STILL immediately satisfies
    /// [`needs_renewal`] — i.e. the configured `renew_before_days` is >= the
    /// certificate's own lifetime (a value `AcmeConfig::validate()` rejects for
    /// public CAs, but a custom directory issuing shorter-lived certificates can
    /// still trip). While set, the loop refuses to order again — otherwise every
    /// hourly tick would order a brand-new certificate until the CA rate-limits
    /// the account. Cleared only by a restart (which re-runs config validation).
    pub renew_window_misconfigured: std::sync::atomic::AtomicBool,
}

/// RAII guard tracking the HTTP-01 tokens published for one order.
///
/// Every token handed to [`PublishedTokens::publish`] is inserted into the
/// shared [`Http01Tokens`] map immediately and removed again when the guard
/// drops — so no matter which `?` in the order flow returns `Err`, published
/// tokens never leak into the map to accumulate across repeated failures.
struct PublishedTokens<'a> {
    tokens: &'a Http01Tokens,
    published: Vec<String>,
}

impl<'a> PublishedTokens<'a> {
    const fn new(tokens: &'a Http01Tokens) -> Self {
        Self {
            tokens,
            published: Vec::new(),
        }
    }

    /// Publish `token → key_authorization` and track it for cleanup on drop.
    fn publish(&mut self, token: String, key_authorization: String) {
        self.tokens.insert(token.clone(), key_authorization);
        self.published.push(token);
    }
}

impl Drop for PublishedTokens<'_> {
    fn drop(&mut self) {
        for token in &self.published {
            self.tokens.remove(token);
        }
    }
}

/// Decide whether the boot-time immediate order should fire, given the
/// `notAfter` of the stored leaf we would serve (`None` while still on the
/// self-signed placeholder — i.e. no real cert yet).
///
/// Fires when there is no real stored certificate yet OR the stored leaf is
/// already inside its renew-before window (including already expired) —
/// otherwise a loadable but dead/expiring stored cert would be served for up to
/// [`RENEWAL_CHECK_INTERVAL`] before the first scheduled check renews it.
///
/// Pure and injectable (`now_unix`) so the decision is unit-testable without the
/// network, mirroring [`needs_renewal`].
#[must_use]
fn due_at_boot(stored_not_after: Option<i64>, renew_before_days: u32, now_unix: i64) -> bool {
    stored_not_after.is_none_or(|not_after| needs_renewal(not_after, renew_before_days, now_unix))
}

impl AcmeRenewalTask {
    /// Run the renewal loop until `shutdown` fires.
    ///
    /// On boot it ensures a certificate is present (ordering immediately when
    /// none is stored or the stored one is due), then wakes hourly to renew
    /// before expiry. Ordering is leader-elected via `coordinator` so only one
    /// replica per certificate orders; a failure is logged, reported through
    /// `reporter`, and retried on the next tick — it never tears down the
    /// listener (the previously served certificate keeps working).
    pub async fn run(
        self,
        coordinator: Arc<dyn SchedulerCoordinator>,
        reporter: ReporterFn,
        shutdown: tokio_util::sync::CancellationToken,
    ) {
        // Immediate check on boot: order now if we have no real cert yet, or the
        // stored cert we would serve is already inside its renew-before window
        // (or expired). Without the latter, a loadable-but-expired stored cert
        // sets `serving_stored_cert = true` and we would serve a dead cert for
        // up to `RENEWAL_CHECK_INTERVAL` before the first scheduled renewal.
        let stored_not_after = if self.serving_stored_cert {
            self.stored_not_after().await
        } else {
            None
        };
        if due_at_boot(stored_not_after, self.config.renew_before_days, now_unix()) {
            self.try_renew_once(&coordinator, &reporter).await;
        }

        loop {
            tokio::select! {
                () = tokio::time::sleep(RENEWAL_CHECK_INTERVAL) => {}
                () = shutdown.cancelled() => break,
            }
            self.maybe_renew(&coordinator, &reporter).await;
        }
    }

    /// Check expiry and renew if within the renew-before window.
    async fn maybe_renew(
        &self,
        coordinator: &Arc<dyn SchedulerCoordinator>,
        reporter: &ReporterFn,
    ) {
        // Every tick, BEFORE the renewal decision, hot-swap in a newer/healthier
        // stored certificate if one has appeared. When another replica (or an
        // out-of-band tool) persists a fresh cert to the shared/local store, a
        // process that did not issue it must adopt it immediately — otherwise a
        // follower keeps serving the placeholder or a stale in-memory cert until
        // its own renew window opens (which, for a freshly-renewed cert, is up to
        // a full renew-before period away). Adoption never downgrades: it only
        // swaps to a strictly-newer, fully-loadable pair.
        self.adopt_stored_cert_if_newer().await;

        // Runtime backstop: a previous issuance produced a certificate that STILL
        // immediately satisfied `needs_renewal` (the renew-before window is >= the
        // cert's own lifetime). Ordering again would loop and burn the CA's rate
        // limits, so refuse to order until a restart re-runs config validation.
        // The failure was already recorded/reported when the flag was set.
        if self
            .renew_window_misconfigured
            .load(std::sync::atomic::Ordering::SeqCst)
        {
            return;
        }

        // No stored cert (still on the placeholder), or a torn/mismatched pair
        // that does not load → treated as absent → always due.
        let due = self.stored_not_after().await.is_none_or(|not_after| {
            needs_renewal(not_after, self.config.renew_before_days, now_unix())
        });
        if due {
            self.try_renew_once(coordinator, reporter).await;
        }
    }

    /// Attempt one leader-elected issuance/renewal, updating status.
    async fn try_renew_once(
        &self,
        coordinator: &Arc<dyn SchedulerCoordinator>,
        reporter: &ReporterFn,
    ) {
        // A distributed scheduler backend was configured (multi-replica intent)
        // but this process could not build the distributed coordinator and fell
        // back to a per-process in-process one. Ordering now would give this
        // replica its OWN local lease, so every replica would order the SAME
        // certificate concurrently — racing the per-process HTTP-01 token maps
        // and local stores and burning the CA's rate limits. Refuse to order:
        // record the failure and dispatch it through the reporter (same seam as
        // the coordinator-error path) so health does not stay `Up` while serving
        // only the placeholder, then keep serving the existing cert this cycle.
        if self.leadership_degraded {
            let msg = "ACME renewal: a distributed scheduler backend is configured but its \
                coordinator is unavailable in this process — refusing to order to avoid racing \
                replicas and Let's Encrypt rate limits (fix the scheduler backend / database \
                connectivity, or run ACME on a single host)"
                .to_owned();
            tracing::error!("{msg}");
            self.status.record_failure(now_unix(), msg.clone());
            reporter(msg);
            return;
        }

        // `Fleet` is the single-leader mode: the in-process backend always
        // grants (correct single-replica behavior), while the Postgres backend
        // grants to exactly one replica via an advisory lock keyed on the cert.
        let tick_key = format!("acme:{}", self.cert_id.as_str());
        let lease = match coordinator
            .try_acquire(RENEWAL_TASK_NAME, &tick_key, TaskCoordination::Fleet)
            .await
        {
            Ok(Some(lease)) => lease,
            Ok(None) => {
                // Another replica is the leader for this cert. Try to pick up a
                // cert it may have already persisted to a shared store.
                self.adopt_stored_cert_if_newer().await;
                return;
            }
            Err(e) => {
                // The coordinator itself errored (e.g. the Postgres advisory-lock
                // pool is unavailable at first boot). We do NOT know whether we
                // are the leader, so we must NOT order — but this is a real
                // failure, not a benign "someone else leads". Record it and
                // dispatch through the reporter (same as an issuance failure) so
                // the health indicator does not stay `Up` while serving only the
                // self-signed placeholder, then skip this cycle and retry next
                // tick.
                let msg = format!("ACME renewal leader election failed: {e}");
                tracing::error!("{msg}");
                self.status.record_failure(now_unix(), msg.clone());
                reporter(msg);
                return;
            }
        };

        let outcome = self.issue().await;
        // Always release the lease, regardless of outcome.
        if let Err(e) = lease.release().await {
            tracing::warn!(error = %e, "failed to release ACME renewal lease");
        }

        self.handle_issue_outcome(outcome, reporter);
    }

    /// Record the result of one [`issue`](Self::issue) attempt and apply the
    /// misconfigured-renew-window backstop.
    ///
    /// Extracted from [`try_renew_once`](Self::try_renew_once) (and kept
    /// network-free) so the backstop is unit-testable without ordering a real
    /// certificate.
    fn handle_issue_outcome(&self, outcome: Result<i64, String>, reporter: &ReporterFn) {
        match outcome {
            Ok(not_after) => {
                self.status.record_success(now_unix(), not_after);
                tracing::info!(
                    cert_id = self.cert_id.as_str(),
                    "ACME certificate issued/renewed and hot-swapped into the TLS listener"
                );

                // Backstop (defense in depth): if the freshly-issued certificate
                // ALREADY satisfies `needs_renewal`, the configured
                // `renew_before_days` is >= the certificate's own lifetime.
                // `AcmeConfig::validate()` rejects this for a public CA, but a
                // custom directory issuing shorter-lived certs can still reach it.
                // Re-ordering on the next tick would loop and burn the CA's rate
                // limits, so record a failure, report it, and refuse to order
                // again until a restart re-runs config validation.
                if needs_renewal(not_after, self.config.renew_before_days, now_unix()) {
                    let msg = format!(
                        "ACME renewal: renew_before_days ({}) is >= the issued certificate \
                         lifetime; refusing to re-order to avoid burning CA rate limits — lower \
                         [server.tls.acme] renew_before_days below the certificate's validity \
                         period",
                        self.config.renew_before_days
                    );
                    tracing::error!("{msg}");
                    self.status.record_failure(now_unix(), msg.clone());
                    reporter(msg);
                    self.renew_window_misconfigured
                        .store(true, std::sync::atomic::Ordering::SeqCst);
                }
            }
            Err(e) => {
                let msg = format!("ACME certificate issuance failed: {e}");
                tracing::error!("{msg}");
                self.status.record_failure(now_unix(), msg.clone());
                reporter(msg);
            }
        }
    }

    /// The stored certificate's leaf `notAfter`, if a *usable* pair is persisted.
    ///
    /// Returns the expiry ONLY when the full chain+key pair loads as a valid
    /// [`CertifiedKey`](rustls::sign::CertifiedKey). A torn write (a new chain
    /// left with the old/mismatched key by a crash between `save_cert`'s two
    /// renames — or observed mid-write by another reader), or any otherwise
    /// malformed pair, is treated as ABSENT (`None`) rather than trusting the
    /// future-dated-but-unusable chain: otherwise `maybe_renew` would see a
    /// healthy far-future `notAfter`, decide renewal is not due, and stay stuck on
    /// the placeholder/old cert until the wrong chain finally entered its renew
    /// window. `None` here makes the renewal decision proceed instead.
    async fn stored_not_after(&self) -> Option<i64> {
        let stored = self.store.load_cert(&self.cert_id).await.ok().flatten()?;
        // Validate the FULL pair (same load/validation path the resolver uses) so
        // a mismatched or malformed pair is detected and counts as absent.
        crate::tls::certified_key_from_pem(
            stored.chain_pem.as_bytes(),
            stored.key_pem.as_bytes(),
            &self.provider,
        )
        .ok()?;
        crate::tls::leaf_not_after_from_pem(stored.chain_pem.as_bytes()).ok()
    }

    /// If the store holds a strictly-newer, fully-loadable certificate than we
    /// currently serve, adopt it into the resolver.
    ///
    /// Only swaps when the stored pair loads as a valid `CertifiedKey` AND its
    /// leaf `notAfter` is strictly later than the currently-served cert's (or we
    /// have no served expiry recorded yet) — it never downgrades to an older cert
    /// and never swaps in a torn/mismatched pair.
    async fn adopt_stored_cert_if_newer(&self) {
        let Some(stored) = self.store.load_cert(&self.cert_id).await.ok().flatten() else {
            return;
        };
        let Ok(not_after) = crate::tls::leaf_not_after_from_pem(stored.chain_pem.as_bytes()) else {
            return;
        };
        let serving = self.status.snapshot().cert_not_after_unix;
        if serving.is_none_or(|current| not_after > current)
            && let Ok(certified) = crate::tls::certified_key_from_pem(
                stored.chain_pem.as_bytes(),
                stored.key_pem.as_bytes(),
                &self.provider,
            )
        {
            self.resolver.store(certified);
            self.status.set_cert_not_after(not_after);
            tracing::info!("adopted a newer ACME certificate from the store");
        }
    }

    /// The full HTTP-01 issuance flow: order → challenge → finalize → persist →
    /// hot-swap. Returns the issued leaf's `notAfter` on success.
    async fn issue(&self) -> Result<i64, String> {
        use instant_acme::{
            AuthorizationStatus, ChallengeType, Identifier, NewOrder, OrderStatus, RetryPolicy,
        };

        let account = self.load_or_register_account().await?;

        let identifiers: Vec<Identifier> = self
            .config
            .domains
            .iter()
            .map(|d| Identifier::Dns(d.clone()))
            .collect();
        let mut order = account
            .new_order(&NewOrder::new(&identifiers))
            .await
            .map_err(|e| format!("failed to create ACME order: {e}"))?;

        // Publish an HTTP-01 response for each pending authorization. The RAII
        // guard removes every published token from the shared map on ALL exit
        // paths (early `?` inside this scope, the `poll_ready` error below, or
        // normal completion), so a transient failure cannot leak tokens that
        // accumulate across repeated renewal attempts.
        let mut published = PublishedTokens::new(&self.tokens);
        {
            let mut authorizations = order.authorizations();
            while let Some(result) = authorizations.next().await {
                let mut authz =
                    result.map_err(|e| format!("failed to fetch authorization: {e}"))?;
                if authz.status == AuthorizationStatus::Valid {
                    continue;
                }
                let mut challenge = authz
                    .challenge(ChallengeType::Http01)
                    .ok_or_else(|| "authorization offered no http-01 challenge".to_owned())?;
                let token = challenge.token.clone();
                let key_auth = challenge.key_authorization().as_str().to_owned();
                // Publish BEFORE signalling ready so the CA can fetch it — and
                // track it in the guard so a failing `set_ready` still cleans up.
                published.publish(token, key_auth);
                challenge
                    .set_ready()
                    .await
                    .map_err(|e| format!("failed to signal challenge ready: {e}"))?;
            }
        }

        // Wait for the order to become ready. The guard clears published tokens
        // when it drops at the end of `issue`, on both the error and Ok paths.
        let status = order
            .poll_ready(&RetryPolicy::default())
            .await
            .map_err(|e| format!("order did not become ready: {e}"))?;
        if status != OrderStatus::Ready {
            return Err(format!("ACME order ended in unexpected state {status:?}"));
        }

        // Finalize with a FRESH rcgen keypair + CSR for exactly these domains.
        let (csr_der, key_pem) = self.generate_csr()?;
        order
            .finalize_csr(&csr_der)
            .await
            .map_err(|e| format!("failed to finalize order: {e}"))?;
        let chain_pem = order
            .poll_certificate(&RetryPolicy::default())
            .await
            .map_err(|e| format!("failed to download certificate: {e}"))?;

        let stored = StoredCert { chain_pem, key_pem };
        let not_after = crate::tls::leaf_not_after_from_pem(stored.chain_pem.as_bytes())?;

        // Persist BEFORE swapping so a crash mid-swap still has the cert on disk.
        self.store
            .save_cert(&self.cert_id, &stored)
            .await
            .map_err(|e| format!("failed to persist issued certificate: {e}"))?;

        let certified = crate::tls::certified_key_from_pem(
            stored.chain_pem.as_bytes(),
            stored.key_pem.as_bytes(),
            &self.provider,
        )?;
        self.resolver.store(certified);
        Ok(not_after)
    }

    /// Generate a fresh keypair + CSR (DER) for the configured domains. Returns
    /// the CSR DER and the private key PEM.
    fn generate_csr(&self) -> Result<(Vec<u8>, String), String> {
        use rcgen::{CertificateParams, DistinguishedName, KeyPair};
        let key_pair =
            KeyPair::generate().map_err(|e| format!("failed to generate cert key: {e}"))?;
        let mut params = CertificateParams::new(self.config.domains.clone())
            .map_err(|e| format!("failed to build CSR params: {e}"))?;
        params.distinguished_name = DistinguishedName::new();
        let csr = params
            .serialize_request(&key_pair)
            .map_err(|e| format!("failed to serialize CSR: {e}"))?;
        Ok((csr.der().to_vec(), key_pair.serialize_pem()))
    }

    /// Load the persisted ACME account, or register a fresh one and persist it.
    async fn load_or_register_account(&self) -> Result<instant_acme::Account, String> {
        use instant_acme::{Account, AccountCredentials, NewAccount};

        let directory_url = crate::acme::directory_url(&self.config.directory);

        if let Some(bytes) = self
            .store
            .load_account()
            .await
            .map_err(|e| format!("failed to read stored ACME account: {e}"))?
        {
            let credentials: AccountCredentials = serde_json::from_slice(&bytes)
                .map_err(|e| format!("stored ACME account is corrupt: {e}"))?;
            let account = Account::builder()
                .map_err(|e| format!("failed to build ACME client: {e}"))?
                .from_credentials(credentials)
                .await
                .map_err(|e| format!("failed to restore ACME account: {e}"))?;
            return Ok(account);
        }

        let contact = format!("mailto:{}", self.config.contact_email.trim());
        let contacts = [contact.as_str()];
        let (account, credentials) = Account::builder()
            .map_err(|e| format!("failed to build ACME client: {e}"))?
            .create(
                &NewAccount {
                    contact: &contacts,
                    terms_of_service_agreed: true,
                    only_return_existing: false,
                },
                directory_url,
                None,
            )
            .await
            .map_err(|e| format!("failed to register ACME account: {e}"))?;

        let serialized = serde_json::to_vec(&credentials)
            .map_err(|e| format!("failed to serialize ACME account: {e}"))?;
        self.store
            .save_account(&serialized)
            .await
            .map_err(|e| format!("failed to persist ACME account: {e}"))?;
        Ok(account)
    }
}

/// Current UNIX time in seconds.
fn now_unix() -> i64 {
    default_now_unix()
}

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

    const DAY: i64 = 86_400;

    #[test]
    fn needs_renewal_matrix() {
        let now = 1_000_000_000;
        // Far from expiry (60 days out, 30-day window) → not due.
        assert!(!needs_renewal(now + 60 * DAY, 30, now));
        // Just inside the window (29 days out) → due.
        assert!(needs_renewal(now + 29 * DAY, 30, now));
        // Exactly at the threshold (30 days out) → not due (strictly less-than).
        assert!(!needs_renewal(now + 30 * DAY, 30, now));
        // Already expired → due.
        assert!(needs_renewal(now - DAY, 30, now));
    }

    #[test]
    fn placeholder_self_signed_loads_and_swaps_via_resolver() {
        let placeholder =
            self_signed_placeholder(&["app.example.com".to_owned()]).expect("placeholder builds");
        let provider = crate::tls::crypto_provider();
        let certified = crate::tls::certified_key_from_pem(
            placeholder.chain_pem.as_bytes(),
            placeholder.key_pem.as_bytes(),
            &provider,
        )
        .expect("placeholder loads as a CertifiedKey");

        // It swaps into a ReloadableCertResolver (the #1603 hot-swap seam).
        let resolver = Arc::new(ReloadableCertResolver::new(Arc::clone(&certified)));
        let next = self_signed_placeholder(&["app.example.com".to_owned()]).unwrap();
        let next_key = crate::tls::certified_key_from_pem(
            next.chain_pem.as_bytes(),
            next.key_pem.as_bytes(),
            &provider,
        )
        .unwrap();
        resolver.store(Arc::clone(&next_key));
        assert!(Arc::ptr_eq(&resolver.current(), &next_key));

        // And its notAfter is readable for the renewal decision.
        assert!(crate::tls::leaf_not_after_from_pem(placeholder.chain_pem.as_bytes()).is_ok());
    }

    #[test]
    fn placeholder_requires_a_domain() {
        assert!(self_signed_placeholder(&[]).is_err());
    }

    #[test]
    fn health_up_when_healthy() {
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.record_success(now, now + 60 * DAY);
        let indicator = AcmeHealthIndicator::new(status, 30);
        assert_eq!(indicator.grade(now).status, HealthStatus::Up);
    }

    #[test]
    fn health_up_on_failure_while_cert_still_valid() {
        // A renewal blip must NOT flip health Down while the served cert has
        // plenty of validity left.
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.set_cert_not_after(now + 60 * DAY);
        status.record_failure(now, "temporary CA error");
        let indicator = AcmeHealthIndicator::new(status, 30);
        let out = indicator.grade(now);
        assert_eq!(out.status, HealthStatus::Up);
        assert!(out.details.contains_key("last_failure"));
    }

    #[test]
    fn health_down_on_failure_within_expiry_danger() {
        // A failure AND the cert is inside its renew-before window → Down.
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.set_cert_not_after(now + 5 * DAY);
        status.record_failure(now, "CA outage");
        let indicator = AcmeHealthIndicator::new(status, 30);
        assert_eq!(indicator.grade(now).status, HealthStatus::Down);
    }

    #[test]
    fn health_down_when_no_cert_and_failure() {
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.record_failure(now, "never issued");
        let indicator = AcmeHealthIndicator::new(status, 30);
        assert_eq!(indicator.grade(now).status, HealthStatus::Down);
    }

    #[test]
    fn health_down_when_served_cert_already_expired_without_failure() {
        // An already-expired stored cert served at boot (not_after <= now) must be
        // Down even though NO renewal failure has been recorded yet (leadership
        // skipped/degraded or issuance still pending). Otherwise health hides an
        // already-invalid TLS certificate.
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.set_cert_not_after(now - DAY);
        assert!(
            status.snapshot().last_failure.is_none(),
            "precondition: no failure recorded"
        );
        let indicator = AcmeHealthIndicator::new(status, 30);
        let out = indicator.grade(now);
        assert_eq!(out.status, HealthStatus::Down);
        assert_eq!(
            out.details.get("cert_expired"),
            Some(&serde_json::json!(true))
        );
    }

    #[test]
    fn health_up_when_cert_has_plenty_of_validity_and_no_failure() {
        // A healthy served cert with lots of validity left and no failure is Up,
        // and is NOT flagged as expired.
        let now = 1_000_000_000;
        let status = AcmeStatus::new();
        status.set_cert_not_after(now + 60 * DAY);
        let indicator = AcmeHealthIndicator::new(status, 30);
        let out = indicator.grade(now);
        assert_eq!(out.status, HealthStatus::Up);
        assert!(!out.details.contains_key("cert_expired"));
    }

    #[test]
    fn published_tokens_are_cleared_on_error_path() {
        // Simulate an order body that publishes N tokens and then returns Err
        // (e.g. `set_ready` or `poll_ready` failing). The guard must leave the
        // shared token map empty rather than leaking the published tokens.
        let tokens = Http01Tokens::new();
        // The inner block scopes the guard: it drops at the closing brace, just
        // as the guard in `issue` drops when that function returns `Err`.
        let outcome: Result<(), String> = {
            let mut published = PublishedTokens::new(&tokens);
            published.publish("token-a".to_owned(), "key-a".to_owned());
            published.publish("token-b".to_owned(), "key-b".to_owned());
            // Both are visible while the order is in flight.
            assert_eq!(tokens.get("token-a").as_deref(), Some("key-a"));
            assert_eq!(tokens.get("token-b").as_deref(), Some("key-b"));
            Err("simulated failure after publishing".to_owned())
        };

        assert!(outcome.is_err());
        // Every published token was removed on the error path.
        assert!(tokens.get("token-a").is_none());
        assert!(tokens.get("token-b").is_none());
    }

    #[test]
    fn published_tokens_are_cleared_on_success_path() {
        let tokens = Http01Tokens::new();
        {
            let mut published = PublishedTokens::new(&tokens);
            published.publish("token-a".to_owned(), "key-a".to_owned());
            assert_eq!(tokens.get("token-a").as_deref(), Some("key-a"));
        }
        assert!(tokens.get("token-a").is_none());
    }

    #[test]
    fn due_at_boot_matrix() {
        let now = 1_000_000_000;
        // No real cert yet (still on the placeholder) → order immediately.
        assert!(due_at_boot(None, 30, now));
        // Serving a healthy stored cert (60 days out) → no immediate order.
        assert!(!due_at_boot(Some(now + 60 * DAY), 30, now));
        // Serving a stored cert already inside its renew-before window (5 days
        // out, 30-day window) → order immediately.
        assert!(due_at_boot(Some(now + 5 * DAY), 30, now));
        // Serving a loadable-but-EXPIRED stored cert → order immediately (the
        // bug: previously this waited a full check interval serving a dead cert).
        assert!(due_at_boot(Some(now - DAY), 30, now));
    }

    /// A coordinator whose `try_acquire` always errors — simulates the Postgres
    /// advisory-lock pool being unavailable at first boot.
    struct FailingCoordinator;

    impl SchedulerCoordinator for FailingCoordinator {
        fn backend(&self) -> &'static str {
            "failing"
        }
        fn replica_id(&self) -> &'static str {
            "test-replica"
        }
        fn try_acquire<'a>(
            &'a self,
            _task_name: &'a str,
            _tick_key: &'a str,
            _coordination: TaskCoordination,
        ) -> crate::scheduler::SchedulerFuture<
            'a,
            crate::AutumnResult<Option<crate::scheduler::SchedulerLease>>,
        > {
            Box::pin(async {
                Err(crate::AutumnError::service_unavailable_msg(
                    "advisory-lock pool unavailable",
                ))
            })
        }
    }

    // Regression (#1608, Codex): when the coordinator itself errors, renewal is
    // skipped, but the failure MUST be recorded in AcmeStatus AND dispatched to
    // the reporter — otherwise health can stay `Up` while serving only the
    // self-signed placeholder and operators lose the error signal.
    #[tokio::test]
    async fn coordinator_error_records_failure_and_reports() {
        let domains = vec!["app.example.com".to_owned()];

        // A resolver serving the self-signed placeholder (the pre-issuance state).
        let placeholder = self_signed_placeholder(&domains).expect("placeholder builds");
        let provider = crate::tls::crypto_provider();
        let certified = crate::tls::certified_key_from_pem(
            placeholder.chain_pem.as_bytes(),
            placeholder.key_pem.as_bytes(),
            &provider,
        )
        .expect("placeholder loads");
        let resolver = Arc::new(ReloadableCertResolver::new(certified));

        // The store is never touched on the coordinator-error path, but the task
        // needs one.
        let store_dir = tempfile::tempdir().unwrap();
        let store: Arc<dyn AcmeStore> = Arc::new(crate::acme::store::FsAcmeStore::new(
            store_dir.path(),
            "staging",
        ));

        let status = AcmeStatus::new();
        let config = AcmeConfig {
            domains: domains.clone(),
            contact_email: "ops@example.com".to_owned(),
            directory: crate::config::AcmeDirectory::Staging,
            cache_dir: store_dir.path().to_path_buf(),
            http_challenge_port: 80,
            renew_before_days: 30,
        };
        let task = AcmeRenewalTask {
            resolver,
            provider: crate::tls::crypto_provider(),
            store,
            cert_id: CertId::from_domains(&domains),
            tokens: Http01Tokens::new(),
            status: status.clone(),
            config,
            serving_stored_cert: false,
            leadership_degraded: false,
            renew_window_misconfigured: std::sync::atomic::AtomicBool::new(false),
        };

        // Capture reporter invocations.
        let captured = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let sink = Arc::clone(&captured);
        let reporter: ReporterFn = Arc::new(move |msg| sink.lock().unwrap().push(msg));

        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(FailingCoordinator);
        task.try_renew_once(&coordinator, &reporter).await;

        // The failure is recorded in status...
        let snap = status.snapshot();
        let failure = snap
            .last_failure
            .expect("coordinator error must be recorded as a failure");
        assert!(
            failure.1.contains("leader election failed"),
            "unexpected failure message: {}",
            failure.1
        );

        // ...and dispatched through the reporter exactly once. Clone out so the
        // mutex guard is released immediately.
        let msgs = captured.lock().unwrap().clone();
        assert_eq!(msgs.len(), 1, "reporter must be invoked once");
        assert!(msgs[0].contains("leader election failed"));
    }

    /// A coordinator that records whether leader election was ever attempted,
    /// then returns `Ok(None)` ("another replica leads") so the ordering path
    /// exits without a network round-trip. Lets a test assert whether the
    /// renewal task reached leader election at all.
    struct RecordingCoordinator {
        acquired: Arc<std::sync::atomic::AtomicBool>,
    }

    impl SchedulerCoordinator for RecordingCoordinator {
        fn backend(&self) -> &'static str {
            "in_process"
        }
        fn replica_id(&self) -> &'static str {
            "test-replica"
        }
        fn try_acquire<'a>(
            &'a self,
            _task_name: &'a str,
            _tick_key: &'a str,
            _coordination: TaskCoordination,
        ) -> crate::scheduler::SchedulerFuture<
            'a,
            crate::AutumnResult<Option<crate::scheduler::SchedulerLease>>,
        > {
            self.acquired
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Box::pin(async { Ok(None) })
        }
    }

    /// Build a renewal task serving the self-signed placeholder (pre-issuance
    /// state) with the given `leadership_degraded` flag. The returned `TempDir`
    /// must be kept alive for the store to stay valid.
    fn degraded_test_task(
        leadership_degraded: bool,
    ) -> (AcmeRenewalTask, tempfile::TempDir, AcmeStatus) {
        let domains = vec!["app.example.com".to_owned()];
        let placeholder = self_signed_placeholder(&domains).expect("placeholder builds");
        let provider = crate::tls::crypto_provider();
        let certified = crate::tls::certified_key_from_pem(
            placeholder.chain_pem.as_bytes(),
            placeholder.key_pem.as_bytes(),
            &provider,
        )
        .expect("placeholder loads");
        let resolver = Arc::new(ReloadableCertResolver::new(certified));

        let store_dir = tempfile::tempdir().unwrap();
        let store: Arc<dyn AcmeStore> = Arc::new(crate::acme::store::FsAcmeStore::new(
            store_dir.path(),
            "staging",
        ));

        let status = AcmeStatus::new();
        let config = AcmeConfig {
            domains: domains.clone(),
            contact_email: "ops@example.com".to_owned(),
            directory: crate::config::AcmeDirectory::Staging,
            cache_dir: store_dir.path().to_path_buf(),
            http_challenge_port: 80,
            renew_before_days: 30,
        };
        let task = AcmeRenewalTask {
            resolver,
            provider: crate::tls::crypto_provider(),
            store,
            cert_id: CertId::from_domains(&domains),
            tokens: Http01Tokens::new(),
            status: status.clone(),
            config,
            serving_stored_cert: false,
            leadership_degraded,
            renew_window_misconfigured: std::sync::atomic::AtomicBool::new(false),
        };
        (task, store_dir, status)
    }

    // Regression (#1608, Codex P2): a *distributed* scheduler backend was
    // configured (multi-replica intent) but this process could not build the
    // distributed coordinator and fell back to a per-process in-process one.
    // Ordering would let every replica grab its OWN local lease and race the CA.
    // The renewal task MUST refuse to order — record a failure, dispatch it
    // through the reporter, and never consult the coordinator this cycle.
    #[tokio::test]
    async fn leadership_degraded_refuses_to_order_and_reports() {
        let (task, _store_dir, status) = degraded_test_task(true);

        let captured = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let sink = Arc::clone(&captured);
        let reporter: ReporterFn = Arc::new(move |msg| sink.lock().unwrap().push(msg));

        // The degraded gate must return before touching the coordinator, so this
        // flag stays false.
        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(RecordingCoordinator {
            acquired: Arc::clone(&acquired),
        });
        task.try_renew_once(&coordinator, &reporter).await;

        // No lease was acquired / no order was attempted.
        assert!(
            !acquired.load(std::sync::atomic::Ordering::SeqCst),
            "degraded leadership must NOT acquire a lease or order"
        );

        // The failure is recorded in status...
        let snap = status.snapshot();
        let failure = snap
            .last_failure
            .expect("degraded leadership must be recorded as a failure");
        assert!(
            failure.1.contains("refusing to order"),
            "unexpected failure message: {}",
            failure.1
        );

        // ...and dispatched through the reporter exactly once.
        let msgs = captured.lock().unwrap().clone();
        assert_eq!(msgs.len(), 1, "reporter must be invoked once");
        assert!(msgs[0].contains("refusing to order"));
    }

    // The genuinely single-replica path (in-process coordinator by design) is
    // unaffected by the degraded gate: it proceeds to leader election as normal.
    // A full network order is out of scope for a unit test, so the coordinator
    // returns `Ok(None)`; the assertion is that the gate did NOT short-circuit
    // (the coordinator WAS consulted) and no spurious degraded failure surfaced.
    #[tokio::test]
    async fn single_replica_path_is_unaffected_and_proceeds_to_order() {
        let (task, _store_dir, status) = degraded_test_task(false);

        let captured = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let sink = Arc::clone(&captured);
        let reporter: ReporterFn = Arc::new(move |msg| sink.lock().unwrap().push(msg));

        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(RecordingCoordinator {
            acquired: Arc::clone(&acquired),
        });
        task.try_renew_once(&coordinator, &reporter).await;

        // The renewal task reached leader election rather than short-circuiting.
        assert!(
            acquired.load(std::sync::atomic::Ordering::SeqCst),
            "single-replica path must proceed to leader election / ordering"
        );

        // No degraded failure was recorded and the reporter was not invoked.
        assert!(
            status.snapshot().last_failure.is_none(),
            "single-replica path must not record a degraded failure"
        );
        assert!(
            captured.lock().unwrap().is_empty(),
            "single-replica path must not report a degraded failure"
        );
    }

    /// Build a real, loadable self-signed cert for `app.example.com` (matching
    /// the `CertId` `degraded_test_task` uses) valid until `year-month-day`.
    /// Returns the stored pair and its leaf `notAfter` in UNIX seconds.
    fn cert_valid_until_ymd(year: i32, month: u8, day: u8) -> (StoredCert, i64) {
        use rcgen::{CertificateParams, DistinguishedName, DnType, KeyPair, date_time_ymd};
        let key = KeyPair::generate().expect("keypair");
        let mut params =
            CertificateParams::new(vec!["app.example.com".to_owned()]).expect("params");
        params.not_before = date_time_ymd(2020, 1, 1);
        params.not_after = date_time_ymd(year, month, day);
        let mut dn = DistinguishedName::new();
        dn.push(DnType::CommonName, "app.example.com");
        params.distinguished_name = dn;
        let cert = params.self_signed(&key).expect("self-sign");
        let stored = StoredCert {
            chain_pem: cert.pem(),
            key_pem: key.serialize_pem(),
        };
        let not_after = crate::tls::leaf_not_after_from_pem(stored.chain_pem.as_bytes())
            .expect("leaf notAfter parses");
        (stored, not_after)
    }

    /// A torn stored pair: a valid, future-dated chain paired with a FRESH,
    /// MISMATCHED key (as a crash between `save_cert`'s two renames would leave —
    /// a new chain with the old/unrelated key).
    fn torn_cert_future() -> StoredCert {
        let (valid, _) = cert_valid_until_ymd(2200, 1, 1);
        let other = rcgen::KeyPair::generate().expect("mismatched keypair");
        StoredCert {
            chain_pem: valid.chain_pem,
            key_pem: other.serialize_pem(),
        }
    }

    // FIX 1 (#1608, Codex P2): a strictly-newer, fully-loadable stored cert is
    // hot-swapped into the resolver on adoption, independent of whether renewal
    // is due — so a follower / a process that did not issue picks it up at once.
    #[tokio::test]
    async fn adopt_swaps_in_a_strictly_newer_stored_cert() {
        let (task, _dir, status) = degraded_test_task(false);
        let initial = task.resolver.current();
        let (newer, newer_na) = cert_valid_until_ymd(2200, 1, 1);
        // The served baseline is OLDER than the stored cert.
        status.set_cert_not_after(newer_na - 100 * DAY);
        task.store.save_cert(&task.cert_id, &newer).await.unwrap();

        task.adopt_stored_cert_if_newer().await;

        assert!(
            !Arc::ptr_eq(&task.resolver.current(), &initial),
            "a strictly-newer stored cert must be swapped into the resolver"
        );
        assert_eq!(status.snapshot().cert_not_after_unix, Some(newer_na));
    }

    // FIX 1: adoption must never DOWNGRADE — an older stored cert than the one we
    // serve is left in place.
    #[tokio::test]
    async fn adopt_does_not_downgrade_to_an_older_stored_cert() {
        let (task, _dir, status) = degraded_test_task(false);
        let initial = task.resolver.current();
        let (older, older_na) = cert_valid_until_ymd(2100, 1, 1);
        // The served baseline is NEWER than the stored cert.
        let served = older_na + 100 * DAY;
        status.set_cert_not_after(served);
        task.store.save_cert(&task.cert_id, &older).await.unwrap();

        task.adopt_stored_cert_if_newer().await;

        assert!(
            Arc::ptr_eq(&task.resolver.current(), &initial),
            "must not downgrade to an older stored cert"
        );
        assert_eq!(
            status.snapshot().cert_not_after_unix,
            Some(served),
            "served expiry must be unchanged"
        );
    }

    // FIX 1: no stored cert at all → nothing to adopt, resolver untouched.
    #[tokio::test]
    async fn adopt_no_swap_when_store_is_empty() {
        let (task, _dir, status) = degraded_test_task(false);
        let initial = task.resolver.current();
        status.set_cert_not_after(1_000_000_000);

        task.adopt_stored_cert_if_newer().await;

        assert!(Arc::ptr_eq(&task.resolver.current(), &initial));
        assert_eq!(status.snapshot().cert_not_after_unix, Some(1_000_000_000));
    }

    // FIX 1 (integration): `maybe_renew` adopts a newer stored cert on a tick even
    // when renewal is NOT due — and, because the adopted cert is far from expiry,
    // it does NOT proceed to leader election / ordering.
    #[tokio::test]
    async fn maybe_renew_adopts_newer_cert_without_ordering_when_not_due() {
        let (task, _dir, status) = degraded_test_task(false);
        let initial = task.resolver.current();
        let (newer, newer_na) = cert_valid_until_ymd(2200, 1, 1);
        status.set_cert_not_after(newer_na - 100 * DAY);
        task.store.save_cert(&task.cert_id, &newer).await.unwrap();

        let reporter: ReporterFn = Arc::new(|_| {});
        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(RecordingCoordinator {
            acquired: Arc::clone(&acquired),
        });

        task.maybe_renew(&coordinator, &reporter).await;

        // The newer cert was adopted into the resolver...
        assert!(
            !Arc::ptr_eq(&task.resolver.current(), &initial),
            "maybe_renew must adopt a newer stored cert every tick"
        );
        assert_eq!(status.snapshot().cert_not_after_unix, Some(newer_na));
        // ...and renewal was NOT due, so the coordinator was never consulted.
        assert!(
            !acquired.load(std::sync::atomic::Ordering::SeqCst),
            "adoption must not trigger ordering when renewal is not due"
        );
    }

    // FIX 2 (#1608, Codex P2): a torn/mismatched stored pair (valid future-dated
    // chain + wrong key) must count as ABSENT for the renewal decision, so a
    // follower does not treat the unusable future-dated chain as healthy and
    // stall on the placeholder/old cert.
    #[tokio::test]
    async fn stored_not_after_is_absent_for_a_torn_pair() {
        let (task, _dir, _status) = degraded_test_task(false);
        let torn = torn_cert_future();
        // The chain alone parses a future notAfter (the trap the old code fell
        // into)...
        assert!(
            crate::tls::leaf_not_after_from_pem(torn.chain_pem.as_bytes()).is_ok(),
            "precondition: the torn chain is itself a valid, future-dated leaf"
        );
        task.store.save_cert(&task.cert_id, &torn).await.unwrap();

        // ...but the renewal decision treats the unusable PAIR as absent.
        assert!(
            task.stored_not_after().await.is_none(),
            "a torn/mismatched pair must count as absent for the renewal decision"
        );
    }

    // FIX 2: a valid matching pair reports its expiry normally (healthy path).
    #[tokio::test]
    async fn stored_not_after_is_present_for_a_valid_pair() {
        let (task, _dir, _status) = degraded_test_task(false);
        let (valid, na) = cert_valid_until_ymd(2200, 1, 1);
        task.store.save_cert(&task.cert_id, &valid).await.unwrap();

        assert_eq!(
            task.stored_not_after().await,
            Some(na),
            "a valid matching pair reports its leaf notAfter normally"
        );
    }

    // FIX 1 (#1608, Codex P2): a freshly-issued certificate that STILL immediately
    // satisfies `needs_renewal` means the renew-before window is >= the cert's own
    // lifetime. The loop must record a failure, report it, set the backstop flag,
    // and — critically — NOT re-order on the next tick (no tight loop that burns CA
    // rate limits).
    #[tokio::test]
    async fn backstop_refuses_reorder_when_fresh_cert_still_needs_renewal() {
        // Default renew window is 30 days; simulate an issued cert that expires in
        // only 1 day, so `needs_renewal` is immediately true.
        let (task, _dir, status) = degraded_test_task(false);

        let captured = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let sink = Arc::clone(&captured);
        let reporter: ReporterFn = Arc::new(move |msg| sink.lock().unwrap().push(msg));

        // Feed the post-issuance handler a "just issued" cert whose notAfter is
        // inside the renew window — exactly what `issue()` would return for a
        // misconfigured window.
        task.handle_issue_outcome(Ok(now_unix() + DAY), &reporter);

        // A failure is recorded and dispatched, naming the misconfiguration.
        let snap = status.snapshot();
        let failure = snap
            .last_failure
            .expect("a fresh cert still due for renewal must record a failure");
        assert!(
            failure.1.contains("refusing to re-order"),
            "unexpected failure message: {}",
            failure.1
        );
        let msgs = captured.lock().unwrap().clone();
        assert_eq!(msgs.len(), 1, "reporter must be invoked once");
        assert!(msgs[0].contains("refusing to re-order"));

        // The backstop flag is now set.
        assert!(
            task.renew_window_misconfigured
                .load(std::sync::atomic::Ordering::SeqCst),
            "the misconfigured-window backstop must be set"
        );

        // A subsequent tick must NOT order, even though the store is empty (which
        // otherwise counts as due): the coordinator is never consulted.
        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(RecordingCoordinator {
            acquired: Arc::clone(&acquired),
        });
        task.maybe_renew(&coordinator, &reporter).await;
        assert!(
            !acquired.load(std::sync::atomic::Ordering::SeqCst),
            "a backed-off loop must NOT re-order (no tight loop)"
        );
    }

    // FIX 1 companion: a healthy issuance (cert far from expiry) must NOT trip the
    // backstop — the loop stays free to renew normally when the next window opens.
    #[tokio::test]
    async fn healthy_issuance_does_not_trip_backstop() {
        let (task, _dir, status) = degraded_test_task(false);
        let reporter: ReporterFn = Arc::new(|_| {});

        // Issued cert with 60 days of validity, 30-day window → not immediately due.
        task.handle_issue_outcome(Ok(now_unix() + 60 * DAY), &reporter);

        let snap = status.snapshot();
        assert!(
            snap.last_failure.is_none(),
            "a healthy issuance must not record a failure"
        );
        assert!(
            snap.last_success_unix.is_some(),
            "a healthy issuance must record success"
        );
        assert!(
            !task
                .renew_window_misconfigured
                .load(std::sync::atomic::Ordering::SeqCst),
            "a healthy issuance must not trip the backstop"
        );

        // And the loop is free to order when due (empty store → due) — the
        // coordinator IS consulted.
        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let coordinator: Arc<dyn SchedulerCoordinator> = Arc::new(RecordingCoordinator {
            acquired: Arc::clone(&acquired),
        });
        task.maybe_renew(&coordinator, &reporter).await;
        assert!(
            acquired.load(std::sync::atomic::Ordering::SeqCst),
            "a healthy issuance must leave the loop free to renew"
        );
    }
}