autumn-cli 0.10.0

CLI tool for the Autumn web framework
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
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
//! The post-cutover bake (issue #3069).
//!
//! After a host cuts over, the bake samples the new release's
//! `/actuator/metrics` over SSH for a set time. It returns a breach when:
//!
//! - the 5xx ratio of the bake window is above the limit,
//! - a gated latency quantile is above the limit,
//! - the process restarted (systemd `NRestarts` went up, or a counter went
//!   down), or
//! - a sample cannot be read two times in a row.
//!
//! Thin traffic never causes a breach: the bake needs `min_requests` new
//! responses, and at least [`MIN_BREACH_ERRORS`] new 5xx for an error breach.
//! The bake does not count its own metric requests as traffic. The caller
//! rolls the host back on a breach.
//!
//! [`judge`] is the decision. `verification/bake_verdict.rs` is its Verus
//! model. The property test `judge_matches_the_verus_model` checks the
//! runtime against a Rust copy of `spec_judge`.
//!
//! The bake reads no clock. Each sample is one remote command that sleeps
//! and then runs `curl`, so the fake executor drives the tests.

// The items are crate-internal, as in `fleet.rs`. `deploy` is a private module
// of this bin crate, so clippy calls each `pub(crate)` redundant.
#![allow(clippy::redundant_pub_crate)]

use std::fmt::{self, Write as _};

use autumn_web::config::DeployBakeConfig;
use autumn_web::slo::{PPM, ROLLBACK_BURN_TENTHS, Sli, Slo, format_decimal, max_error_ppm};
use serde::Deserialize;

use super::exec::{DeployExecutor, RemoteCommand, shell_quote};

/// The step label of a bake failure, in fleet reports.
pub(crate) const BAKE_LABEL: &str = "bake";

/// The label of one metrics sample.
pub(crate) const SAMPLE_LABEL: &str = "bake-sample";

/// The error-ratio limit when no SLO and no `max_error_rate` set one: 5 %,
/// the same as the `[alerts]` default.
pub(crate) const DEFAULT_MAX_ERROR_PPM: u32 = 50_000;

/// The fewest new 5xx responses for an error breach. One bad request alone
/// never rolls a release back.
pub(crate) const MIN_BREACH_ERRORS: u64 = 2;

/// Separates the metrics JSON from the systemd restart count in a sample.
pub(crate) const RESTARTS_MARKER: &str = "---autumn-bake-nrestarts---";

/// A latency quantile from `/actuator/metrics`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Quantile {
    P50,
    P95,
    P99,
}

impl fmt::Display for Quantile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::P50 => "p50",
            Self::P95 => "p95",
            Self::P99 => "p99",
        })
    }
}

/// A latency limit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LatencyGate {
    pub quantile: Quantile,
    pub max_ms: u64,
}

/// The resolved bake settings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BakePolicy {
    pub duration_secs: u64,
    pub interval_secs: u64,
    pub min_requests: u64,
    pub max_error_ppm: u32,
    /// At most one gate for each quantile.
    pub latency: Vec<LatencyGate>,
    /// Where the limits come from, for the operator.
    pub source: String,
    /// Limits of the bake that the operator must know.
    pub warnings: Vec<String>,
}

/// The p50, p95 and p99 latency of the app, in milliseconds.
///
/// The app computes them over its last 10,000 requests, not over the bake
/// window only.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Latencies {
    pub p50: u64,
    pub p95: u64,
    pub p99: u64,
}

impl Latencies {
    pub(crate) const fn get(self, quantile: Quantile) -> u64 {
        match quantile {
            Quantile::P50 => self.p50,
            Quantile::P95 => self.p95,
            Quantile::P99 => self.p99,
        }
    }
}

/// One metrics sample. The counters are cumulative since the process started.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Sample {
    /// All responses (2xx to 5xx).
    pub responses: u64,
    /// 5xx responses.
    pub errors: u64,
    pub latency: Latencies,
    /// systemd `NRestarts` of the slot unit. `None` when systemd did not
    /// report it.
    pub restarts: Option<u64>,
}

/// Why the bake failed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Breach {
    /// The process restarted during the bake.
    Restarted,
    /// The 5xx ratio is above the limit.
    ErrorRate {
        errors: u64,
        responses: u64,
        max_ppm: u32,
    },
    /// A latency quantile is above the limit.
    Latency {
        quantile: Quantile,
        ms: u64,
        max_ms: u64,
    },
    /// A sample could not be read two times in a row.
    Unreachable(String),
}

impl fmt::Display for Breach {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Restarted => f.write_str("the new release restarted during the bake"),
            Self::ErrorRate {
                errors,
                responses,
                max_ppm,
            } => write!(
                f,
                "{errors} of {responses} responses were 5xx ({}%), above the limit of {}%",
                percent(u128::from(*errors) * u128::from(PPM) / u128::from((*responses).max(1))),
                percent(u128::from(*max_ppm)),
            ),
            Self::Latency {
                quantile,
                ms,
                max_ms,
            } => write!(
                f,
                "{quantile} latency is {ms} ms, above the limit of {max_ms} ms"
            ),
            Self::Unreachable(why) => write!(f, "the metrics sample failed: {why}"),
        }
    }
}

/// A ppm value as a percentage with up to four decimals, rounded down.
fn percent(ppm: u128) -> String {
    format_decimal(u64::try_from(ppm).unwrap_or(u64::MAX), 4)
}

/// The verdict on one sample.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Verdict {
    Pass,
    TooFewRequests,
    Breach(Breach),
}

/// The new responses since `baseline`, less the bake's own `own_requests`.
pub(crate) const fn window_responses(baseline: &Sample, now: &Sample, own_requests: u64) -> u64 {
    now.responses
        .saturating_sub(baseline.responses)
        .saturating_sub(own_requests)
}

/// The bake's own metric requests that a sample can count. They are not
/// traffic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Own {
    /// All the bake's requests since the baseline.
    pub requests: u64,
    /// The failed ones (a retried sample). A failed request can be a 5xx.
    pub failed: u64,
}

/// Judge `now` against the bake `baseline`. The Verus model is `spec_judge`.
pub(crate) fn judge(baseline: &Sample, now: &Sample, own: Own, policy: &BakePolicy) -> Verdict {
    let restarted = match (baseline.restarts, now.restarts) {
        (Some(before), Some(after)) => after != before,
        _ => false,
    };
    if restarted || now.responses < baseline.responses || now.errors < baseline.errors {
        return Verdict::Breach(Breach::Restarted);
    }
    let responses = window_responses(baseline, now, own.requests);
    // A failed own request may be a 5xx. Leave it out; at worst this hides one
    // app error for each retried sample, which only delays a rollback.
    let errors = (now.errors - baseline.errors).saturating_sub(own.failed);
    if responses < policy.min_requests {
        return Verdict::TooFewRequests;
    }
    // u128: a product of two u64 values cannot overflow.
    if errors >= MIN_BREACH_ERRORS
        && u128::from(errors) * u128::from(PPM)
            > u128::from(policy.max_error_ppm) * u128::from(responses)
    {
        return Verdict::Breach(Breach::ErrorRate {
            errors,
            responses,
            max_ppm: policy.max_error_ppm,
        });
    }
    for gate in &policy.latency {
        let ms = now.latency.get(gate.quantile);
        if ms > gate.max_ms {
            return Verdict::Breach(Breach::Latency {
                quantile: gate.quantile,
                ms,
                max_ms: gate.max_ms,
            });
        }
    }
    Verdict::Pass
}

/// Parse one sample: the `/actuator/metrics` JSON, then optionally the
/// restart marker and the systemd `NRestarts` value.
pub(crate) fn parse_sample(stdout: &str) -> Result<Sample, String> {
    #[derive(Deserialize)]
    struct Metrics {
        http: Http,
    }
    #[derive(Deserialize)]
    struct Http {
        by_status: Status,
        latency_ms: Latency,
    }
    #[derive(Deserialize)]
    struct Status {
        #[serde(rename = "2xx", default)]
        s2xx: u64,
        #[serde(rename = "3xx", default)]
        s3xx: u64,
        #[serde(rename = "4xx", default)]
        s4xx: u64,
        #[serde(rename = "5xx", default)]
        s5xx: u64,
    }
    #[derive(Deserialize)]
    struct Latency {
        #[serde(default)]
        p50: u64,
        #[serde(default)]
        p95: u64,
        #[serde(default)]
        p99: u64,
    }
    let (json, restarts) = stdout
        .split_once(RESTARTS_MARKER)
        .map_or((stdout, None), |(json, rest)| (json, Some(rest)));
    let metrics: Metrics = serde_json::from_str(json.trim())
        .map_err(|e| format!("the response is not /actuator/metrics JSON: {e}"))?;
    let status = metrics.http.by_status;
    let latency = metrics.http.latency_ms;
    Ok(Sample {
        responses: status
            .s2xx
            .saturating_add(status.s3xx)
            .saturating_add(status.s4xx)
            .saturating_add(status.s5xx),
        errors: status.s5xx,
        latency: Latencies {
            p50: latency.p50,
            p95: latency.p95,
            p99: latency.p99,
        },
        restarts: restarts.and_then(|r| r.trim().parse().ok()),
    })
}

/// Where a bake reads its samples.
#[derive(Debug, Clone, Copy)]
pub(crate) struct BakeTarget<'a> {
    /// The loopback port of the new release.
    pub port: u16,
    /// The `/actuator/metrics` path, already normalized.
    pub metrics_path: &'a str,
    /// A `Host` header the app trusts. The `prod` profile rejects
    /// `127.0.0.1` unless `[security.trusted_hosts]` lists it.
    pub host_header: Option<&'a str>,
    /// The systemd unit of the new release.
    pub unit: &'a str,
}

/// The remote command for one sample: sleep, read the metrics, then read the
/// systemd restart count.
pub(crate) fn sample_command(target: &BakeTarget<'_>, sleep_secs: u64) -> RemoteCommand {
    let host = target.host_header.map_or_else(String::new, |host| {
        format!(" -H {}", shell_quote(&format!("Host: {host}")))
    });
    let curl = format!(
        "curl -fsSg -m 5{host} {}",
        shell_quote(&format!(
            "http://127.0.0.1:{}{}",
            target.port, target.metrics_path
        ))
    );
    let restarts = format!(
        "printf '\\n%s\\n' {} && (systemctl show -p NRestarts --value {} || true)",
        shell_quote(RESTARTS_MARKER),
        shell_quote(&format!("{}.service", target.unit)),
    );
    let shell = if sleep_secs == 0 {
        format!("{curl} && {restarts}")
    } else {
        format!("sleep {sleep_secs} && {curl} && {restarts}")
    };
    RemoteCommand::new(SAMPLE_LABEL, shell)
}

/// Run one sample. A failed sample is tried one more time at once, so one
/// lost SSH connection does not roll a release back.
///
/// Returns the sample and the number of attempts.
fn take_sample<E: DeployExecutor>(
    executor: &E,
    target: &BakeTarget<'_>,
    sleep_secs: u64,
) -> Result<(Sample, u64), Breach> {
    let attempt = |sleep| {
        executor
            .run(&sample_command(target, sleep))
            .map_err(|e| e.to_string())
            .and_then(|output| parse_sample(&output.stdout))
    };
    attempt(sleep_secs).map_or_else(
        |_| {
            attempt(0)
                .map(|sample| (sample, 2))
                .map_err(Breach::Unreachable)
        },
        |sample| Ok((sample, 1)),
    )
}

/// The result of a bake.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum BakeOutcome {
    /// No breach. `responses` is the traffic the bake saw.
    Passed { responses: u64, judged: bool },
    /// A breach. The caller rolls back.
    Breached(Breach),
}

/// Run the bake against `target`. `progress` gets one line per sample.
pub(crate) fn run<E: DeployExecutor>(
    policy: &BakePolicy,
    target: &BakeTarget<'_>,
    executor: &E,
    progress: &mut dyn FnMut(&str),
) -> BakeOutcome {
    let (baseline, _) = match take_sample(executor, target, 0) {
        Ok(sample) => sample,
        Err(breach) => return BakeOutcome::Breached(breach),
    };
    // The app counts a metric request after it answers it. A failed baseline
    // attempt is already in the baseline counters, so only the successful
    // baseline request falls in the window.
    let mut own_requests = 1;
    let mut own_failed = 0;
    let interval = policy.interval_secs.max(1);
    let mut elapsed = 0;
    let mut responses = 0;
    let mut judged = false;
    while elapsed < policy.duration_secs {
        let sleep = interval.min(policy.duration_secs - elapsed);
        elapsed += sleep;
        let (now, attempts) = match take_sample(executor, target, sleep) {
            Ok(sample) => sample,
            Err(breach) => return BakeOutcome::Breached(breach),
        };
        // A retried sample can count its own failed first attempt.
        own_failed += attempts - 1;
        let own = Own {
            requests: own_requests + attempts - 1,
            failed: own_failed,
        };
        own_requests += attempts;
        responses = window_responses(&baseline, &now, own.requests);
        progress(&format!(
            "bake {elapsed}/{} s: {responses} responses, {} 5xx, p99 {} ms",
            policy.duration_secs,
            now.errors
                .saturating_sub(baseline.errors)
                .saturating_sub(own.failed),
            now.latency.p99,
        ));
        match judge(&baseline, &now, own, policy) {
            Verdict::Breach(breach) => return BakeOutcome::Breached(breach),
            Verdict::Pass => judged = true,
            Verdict::TooFewRequests => {}
        }
    }
    BakeOutcome::Passed { responses, judged }
}

/// The `/actuator/metrics` path for an `[actuator] prefix`. It matches the
/// app's own prefix rules: trim, add a leading `/` when it is missing, drop trailing `/`.
pub(crate) fn metrics_path(prefix: &str) -> String {
    // The same rules as `normalize_actuator_prefix` and `actuator_route_path`
    // in autumn-web: trailing `/` go, leading `/` stay.
    let trimmed = prefix.trim();
    if trimmed.is_empty() || trimmed == "/" {
        return "/metrics".to_owned();
    }
    let trimmed = trimmed.trim_end_matches('/');
    if trimmed.starts_with('/') {
        format!("{trimmed}/metrics")
    } else {
        format!("/{trimmed}/metrics")
    }
}

/// A `Host` header that `[security.trusted_hosts]` accepts: the first
/// entry, without a leading `.`. `None` when the list is empty or allows any
/// host.
pub(crate) fn host_header(trusted_hosts: &[String]) -> Option<String> {
    let first = trusted_hosts
        .iter()
        .map(|h| h.trim().trim_end_matches('.'))
        .find(|h| !h.is_empty())?;
    if first == "*" {
        return None;
    }
    Some(first.trim_start_matches('.').to_owned())
}

/// Resolve the bake settings from `[deploy.bake]`, the `[[slo]]` tables and
/// the `--bake-secs` flag. `None` means the bake is off.
///
/// # Errors
///
/// Returns an error for a `max_error_rate` outside `0.0..=1.0`.
pub(crate) fn resolve_policy(
    config: &DeployBakeConfig,
    slos: &[Slo],
    duration_override: Option<u64>,
) -> Result<Option<BakePolicy>, String> {
    let duration_secs = duration_override.unwrap_or(config.duration_secs);
    if duration_secs == 0 {
        return Ok(None);
    }
    let mut sources = Vec::new();

    let max_error_ppm = if let Some(rate) = config.max_error_rate {
        if !rate.is_finite() || !(0.0..=1.0).contains(&rate) {
            return Err(format!(
                "[deploy.bake] max_error_rate = {rate} must be from 0.0 to 1.0"
            ));
        }
        sources.push("[deploy.bake] max_error_rate".to_owned());
        // The range check keeps the product in 0..=1e6.
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let ppm = (rate * f64::from(PPM)).round() as u32;
        ppm
    } else if let Some(strict) = slos
        .iter()
        .filter(|s| matches!(s.sli, Sli::Availability { route: None }))
        .min_by_key(|s| s.budget_ppm())
    {
        sources.push(format!(
            "SLO {} at {}x burn",
            strict.name,
            format_decimal(u64::from(ROLLBACK_BURN_TENTHS), 1)
        ));
        max_error_ppm(strict.budget_ppm(), ROLLBACK_BURN_TENTHS)
    } else {
        sources.push("the default 5% error limit".to_owned());
        DEFAULT_MAX_ERROR_PPM
    };

    let mut warnings = Vec::new();
    let latency = if let Some(max_ms) = config.max_p99_ms {
        sources.push("[deploy.bake] max_p99_ms".to_owned());
        vec![LatencyGate {
            quantile: Quantile::P99,
            max_ms,
        }]
    } else {
        // One gate for each quantile: the smallest limit wins.
        let mut gates: Vec<(LatencyGate, &str)> = Vec::new();
        for slo in slos {
            let Sli::Latency {
                route: None,
                threshold_ms,
            } = slo.sli
            else {
                continue;
            };
            let (quantile, exact) = quantile_for(slo.budget_ppm());
            if !exact {
                warnings.push(format!(
                    "SLO {}: at the {}x rollback burn rate, more than {}% of requests may be \
                     slow. The bake reads p99 only, so it does not see a smaller slow share.",
                    slo.name,
                    format_decimal(u64::from(ROLLBACK_BURN_TENTHS), 1),
                    percent(u128::from(autumn_web::slo::max_error_ppm(
                        slo.budget_ppm(),
                        ROLLBACK_BURN_TENTHS
                    ))),
                ));
            }
            let gate = LatencyGate {
                quantile,
                max_ms: threshold_ms,
            };
            match gates.iter_mut().find(|(g, _)| g.quantile == gate.quantile) {
                Some(entry) if entry.0.max_ms <= gate.max_ms => {}
                Some(entry) => *entry = (gate, &slo.name),
                None => gates.push((gate, &slo.name)),
            }
        }
        gates.sort_by_key(|(g, _)| g.quantile);
        for (_, name) in &gates {
            sources.push(format!("SLO {name}"));
        }
        if !gates.is_empty() {
            warnings.push(APP_WIDE_LATENCY.to_owned());
        }
        gates.into_iter().map(|(g, _)| g).collect()
    };

    Ok(Some(BakePolicy {
        duration_secs,
        interval_secs: config.interval_secs.clamp(1, duration_secs),
        min_requests: config.min_requests,
        max_error_ppm,
        latency,
        source: sources.join(", "),
        warnings,
    }))
}

/// The note for a latency gate that comes from an SLO.
pub(crate) const APP_WIDE_LATENCY: &str = "The latency gates read the app-wide quantiles of \
     /actuator/metrics. They count every response, also 5xx, unmatched paths and the bake's own \
     requests, so they only approximate the SLO. The Prometheus burn-rate alerts measure the SLO \
     itself.";

/// The quantile that gates a latency SLO with `budget_ppm`.
///
/// At the rollback burn rate (14.4×), a share `f` of requests may be slow.
/// The gate is the quantile whose tail is the largest one at or below `f`, so
/// the bake never misses a burn above 14.4×. `/actuator/metrics` has only
/// p50, p95 and p99. When `f` is below 1 %, p99 is the best it has, and the
/// second value is `false`: the bake cannot see the whole burn.
fn quantile_for(budget_ppm: u32) -> (Quantile, bool) {
    let slow_ppm = max_error_ppm(budget_ppm, ROLLBACK_BURN_TENTHS);
    if slow_ppm >= 500_000 {
        (Quantile::P50, true)
    } else if slow_ppm >= 50_000 {
        (Quantile::P95, true)
    } else {
        (Quantile::P99, slow_ppm >= 10_000)
    }
}

impl BakePolicy {
    /// One line that describes the policy, for the operator.
    pub(crate) fn describe(&self) -> String {
        let mut latency = String::new();
        for gate in &self.latency {
            let _ = write!(
                latency,
                " or {} latency is above {} ms",
                gate.quantile, gate.max_ms
            );
        }
        format!(
            "bake for {} s, sample every {} s; roll back when the 5xx ratio is above {}%{latency} \
             (after {} responses; limits from {})",
            self.duration_secs,
            self.interval_secs,
            percent(u128::from(self.max_error_ppm)),
            self.min_requests,
            self.source,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::deploy::exec::test_support::{RecordedCall, RecordingExecutor};
    use autumn_web::slo::{SliKind, SloConfig};

    fn policy() -> BakePolicy {
        BakePolicy {
            duration_secs: 30,
            interval_secs: 10,
            min_requests: 20,
            max_error_ppm: 14_400,
            latency: vec![LatencyGate {
                quantile: Quantile::P99,
                max_ms: 250,
            }],
            source: "test".to_owned(),
            warnings: Vec::new(),
        }
    }

    const fn own(requests: u64) -> Own {
        Own {
            requests,
            failed: 0,
        }
    }

    fn sample(responses: u64, errors: u64, p99: u64) -> Sample {
        Sample {
            responses,
            errors,
            latency: Latencies {
                p50: 1,
                p95: 2,
                p99,
            },
            restarts: Some(0),
        }
    }

    /// A sample's stdout: the metrics JSON, the marker and `NRestarts`.
    pub(crate) fn sample_stdout(ok: u64, server_errors: u64, p99: u64, restarts: u64) -> String {
        format!(
            r#"{{"http":{{"requests_total":{total},"requests_active":0,
            "latency_ms":{{"p50":1,"p95":2,"p99":{p99}}},
            "by_status":{{"2xx":{ok},"3xx":0,"4xx":0,"5xx":{server_errors}}},
            "by_route":{{}}}}}}
{RESTARTS_MARKER}
{restarts}
"#,
            total = ok + server_errors
        )
    }

    const TARGET: BakeTarget<'static> = BakeTarget {
        port: 3001,
        metrics_path: "/actuator/metrics",
        host_header: Some("app.example.com"),
        unit: "myapp-green",
    };

    // ── judge ────────────────────────────────────────────────────────────────

    #[test]
    fn judge_passes_healthy_traffic() {
        let v = judge(&sample(100, 0, 0), &sample(200, 1, 100), own(0), &policy());
        assert_eq!(v, Verdict::Pass);
    }

    #[test]
    fn judge_waits_for_enough_traffic() {
        // 19 new responses, all 5xx: still no verdict.
        let v = judge(&sample(100, 0, 0), &sample(119, 19, 900), own(0), &policy());
        assert_eq!(v, Verdict::TooFewRequests);
    }

    #[test]
    fn judge_does_not_count_its_own_requests() {
        // 25 new responses, but 6 are the bake's own metric requests.
        let v = judge(&sample(0, 0, 0), &sample(25, 0, 0), own(6), &policy());
        assert_eq!(v, Verdict::TooFewRequests);
        let v = judge(&sample(0, 0, 0), &sample(25, 0, 0), own(5), &policy());
        assert_eq!(v, Verdict::Pass);
    }

    #[test]
    fn judge_flags_an_error_spike() {
        // 10 % 5xx against a 1.44 % limit.
        let v = judge(&sample(100, 0, 0), &sample(200, 10, 10), own(0), &policy());
        assert_eq!(
            v,
            Verdict::Breach(Breach::ErrorRate {
                errors: 10,
                responses: 100,
                max_ppm: 14_400,
            })
        );
    }

    #[test]
    fn one_error_alone_never_breaches() {
        // 1 of 20 is 5 %, above 1.44 %, but one bad request is not a trend.
        let v = judge(&sample(0, 0, 0), &sample(20, 1, 0), own(0), &policy());
        assert_eq!(v, Verdict::Pass);
        let v = judge(&sample(0, 0, 0), &sample(20, 2, 0), own(0), &policy());
        assert!(
            matches!(v, Verdict::Breach(Breach::ErrorRate { .. })),
            "{v:?}"
        );
    }

    #[test]
    fn judge_is_exact_at_the_limit() {
        let mut p = policy();
        p.max_error_ppm = 20_000; // 2 %
        assert_eq!(
            judge(&sample(0, 0, 0), &sample(100, 2, 0), own(0), &p),
            Verdict::Pass
        );
        assert!(matches!(
            judge(&sample(0, 0, 0), &sample(99, 2, 0), own(0), &p),
            Verdict::Breach(Breach::ErrorRate { .. })
        ));
    }

    #[test]
    fn judge_flags_slow_latency_on_each_gate() {
        let v = judge(&sample(0, 0, 0), &sample(100, 0, 251), own(0), &policy());
        assert_eq!(
            v,
            Verdict::Breach(Breach::Latency {
                quantile: Quantile::P99,
                ms: 251,
                max_ms: 250,
            })
        );
        let mut p = policy();
        p.latency.insert(
            0,
            LatencyGate {
                quantile: Quantile::P50,
                max_ms: 0,
            },
        );
        assert_eq!(
            judge(&sample(0, 0, 0), &sample(100, 0, 1), own(0), &p),
            Verdict::Breach(Breach::Latency {
                quantile: Quantile::P50,
                ms: 1,
                max_ms: 0,
            })
        );
        p.latency.clear();
        assert_eq!(
            judge(&sample(0, 0, 0), &sample(100, 0, 9_999), own(0), &p),
            Verdict::Pass
        );
    }

    #[test]
    fn judge_flags_a_restart() {
        let v = judge(&sample(500, 3, 0), &sample(10, 0, 0), own(0), &policy());
        assert_eq!(v, Verdict::Breach(Breach::Restarted));
        let v = judge(&sample(500, 3, 0), &sample(600, 2, 0), own(0), &policy());
        assert_eq!(v, Verdict::Breach(Breach::Restarted));
        // systemd restarted the unit, and the counters grew again.
        let mut after = sample(900, 3, 0);
        after.restarts = Some(1);
        assert_eq!(
            judge(&sample(500, 3, 0), &after, own(0), &policy()),
            Verdict::Breach(Breach::Restarted)
        );
        // An unknown restart count is not a restart.
        after.restarts = None;
        assert_eq!(
            judge(&sample(500, 3, 0), &after, own(0), &policy()),
            Verdict::Pass
        );
    }

    #[test]
    fn judge_checks_errors_before_latency() {
        let v = judge(&sample(0, 0, 0), &sample(100, 50, 9_999), own(0), &policy());
        assert!(
            matches!(v, Verdict::Breach(Breach::ErrorRate { .. })),
            "{v:?}"
        );
    }

    #[test]
    fn judge_does_not_overflow() {
        let mut p = policy();
        p.max_error_ppm = PPM;
        p.latency.clear();
        let v = judge(&sample(0, 0, 0), &sample(u64::MAX, u64::MAX, 0), own(0), &p);
        assert_eq!(v, Verdict::Pass);
    }

    mod props {
        use super::*;
        use proptest::prelude::*;

        /// `spec_judge` from `verification/bake_verdict.rs`, over `i128`.
        fn model(
            base: Sample,
            now: Sample,
            own: u64,
            failed: u64,
            latency_over: bool,
            policy: &BakePolicy,
        ) -> &'static str {
            let restarted = matches!((base.restarts, now.restarts), (Some(x), Some(y)) if x != y);
            if restarted || now.responses < base.responses || now.errors < base.errors {
                return "restart";
            }
            let dr =
                (i128::from(now.responses) - i128::from(base.responses) - i128::from(own)).max(0);
            let de = (i128::from(now.errors) - i128::from(base.errors) - i128::from(failed)).max(0);
            if dr < i128::from(policy.min_requests) {
                return "few";
            }
            if de >= i128::from(MIN_BREACH_ERRORS)
                && de * 1_000_000 > i128::from(policy.max_error_ppm) * dr
            {
                return "errors";
            }
            if latency_over {
                return "latency";
            }
            "pass"
        }

        fn name(v: &Verdict) -> &'static str {
            match v {
                Verdict::Pass => "pass",
                Verdict::TooFewRequests => "few",
                Verdict::Breach(Breach::Restarted) => "restart",
                Verdict::Breach(Breach::ErrorRate { .. }) => "errors",
                Verdict::Breach(Breach::Latency { .. }) => "latency",
                Verdict::Breach(Breach::Unreachable(_)) => "unreachable",
            }
        }

        proptest! {
            #[test]
            fn judge_matches_the_verus_model(
                b_r in 0u64..10_000, b_e in 0u64..100,
                n_r in 0u64..20_000, n_e in 0u64..2_000,
                own in 0u64..50, failed in 0u64..5, lat in 0u64..1_000, max_ppm in 0u32..=PPM,
                min in 0u64..200, gate in proptest::option::of(0u64..1_000),
                b_restarts in proptest::option::of(0u64..3),
                n_restarts in proptest::option::of(0u64..3),
            ) {
                let p = BakePolicy {
                    min_requests: min,
                    max_error_ppm: max_ppm,
                    latency: gate
                        .map(|max_ms| LatencyGate { quantile: Quantile::P99, max_ms })
                        .into_iter()
                        .collect(),
                    ..policy()
                };
                let mut b = sample(b_r, b_e, 0);
                b.restarts = b_restarts;
                let mut n = sample(n_r, n_e, lat);
                n.restarts = n_restarts;
                let latency_over = gate.is_some_and(|max| lat > max);
                let mine = Own { requests: own, failed };
                prop_assert_eq!(
                    name(&judge(&b, &n, mine, &p)),
                    model(b, n, own, failed, latency_over, &p)
                );
            }
        }
    }

    // ── parse_sample ─────────────────────────────────────────────────────────

    #[test]
    fn parse_sample_reads_counters_latency_and_restarts() {
        let s = parse_sample(&sample_stdout(90, 10, 42, 3)).expect("parse");
        assert_eq!(s.responses, 100);
        assert_eq!(s.errors, 10);
        assert_eq!(
            s.latency,
            Latencies {
                p50: 1,
                p95: 2,
                p99: 42
            }
        );
        assert_eq!(s.restarts, Some(3));
    }

    #[test]
    fn parse_sample_without_a_restart_count() {
        let json = sample_stdout(1, 0, 1, 0);
        let (json, _) = json.split_once(RESTARTS_MARKER).expect("marker");
        assert_eq!(parse_sample(json).expect("parse").restarts, None);
        let blank = format!("{json}{RESTARTS_MARKER}\n\n");
        assert_eq!(parse_sample(&blank).expect("parse").restarts, None);
    }

    #[test]
    fn parse_sample_reads_the_real_actuator_json() {
        // The shape `/actuator/metrics` serializes (autumn_web::middleware::metrics).
        let collector = autumn_web::middleware::MetricsCollector::new();
        collector.record("GET", "/a", 200, 7);
        collector.record("GET", "/a", 404, 7);
        collector.record("POST", "/b", 503, 7);
        let json = serde_json::to_string(&collector.snapshot()).expect("serialize");
        let s = parse_sample(&json).expect("parse the real shape");
        assert_eq!((s.responses, s.errors), (3, 1));
        assert_eq!(s.latency.p99, 7);
    }

    #[test]
    fn parse_sample_rejects_bad_json() {
        let error = parse_sample("<html>").expect_err("bad");
        assert!(error.contains("JSON"), "{error}");
    }

    // ── sample_command ───────────────────────────────────────────────────────

    #[test]
    fn sample_command_sends_a_trusted_host_and_reads_restarts() {
        let cmd = sample_command(&TARGET, 10);
        assert_eq!(cmd.label, SAMPLE_LABEL);
        assert_eq!(
            cmd.shell,
            "sleep 10 && curl -fsSg -m 5 -H 'Host: app.example.com' \
             'http://127.0.0.1:3001/actuator/metrics' && printf '\\n%s\\n' \
             '---autumn-bake-nrestarts---' && (systemctl show -p NRestarts --value \
             'myapp-green.service' || true)"
        );
        let first = sample_command(
            &BakeTarget {
                host_header: None,
                ..TARGET
            },
            0,
        );
        assert!(
            first
                .shell
                .starts_with("curl -fsSg -m 5 'http://127.0.0.1:3001/actuator/metrics' && "),
            "{}",
            first.shell
        );
    }

    #[test]
    fn sample_command_quotes_hostile_values() {
        let cmd = sample_command(
            &BakeTarget {
                metrics_path: "/a'; rm -rf /; '/metrics",
                host_header: Some("x'$(id)"),
                ..TARGET
            },
            0,
        );
        assert!(
            cmd.shell
                .contains(r"'http://127.0.0.1:3001/a'\''; rm -rf /; '\''/metrics'")
        );
        assert!(cmd.shell.contains(r"-H 'Host: x'\''$(id)'"));
    }

    #[test]
    fn metrics_path_follows_the_app_prefix_rules() {
        assert_eq!(metrics_path("/actuator"), "/actuator/metrics");
        assert_eq!(metrics_path("ops"), "/ops/metrics");
        assert_eq!(metrics_path(" /ops/ "), "/ops/metrics");
        assert_eq!(metrics_path("/"), "/metrics");
        assert_eq!(metrics_path(""), "/metrics");
        // The app keeps repeated leading slashes (`normalize_actuator_prefix`).
        assert_eq!(metrics_path("//ops"), "//ops/metrics");
        assert_eq!(metrics_path("ops//"), "/ops/metrics");
    }

    #[test]
    fn host_header_takes_the_first_trusted_host() {
        let hosts = |list: &[&str]| list.iter().map(|h| (*h).to_owned()).collect::<Vec<_>>();
        assert_eq!(
            host_header(&hosts(&["app.example.com", "x"])).as_deref(),
            Some("app.example.com")
        );
        assert_eq!(
            host_header(&hosts(&[".example.com"])).as_deref(),
            Some("example.com")
        );
        assert_eq!(
            host_header(&hosts(&[" ", "b.example.com."])).as_deref(),
            Some("b.example.com")
        );
        assert_eq!(host_header(&hosts(&["*"])), None);
        assert_eq!(host_header(&[]), None);
    }

    // ── run ──────────────────────────────────────────────────────────────────

    fn exec_with(samples: &[String]) -> RecordingExecutor {
        let mut exec = RecordingExecutor::new().strict();
        for (index, body) in samples.iter().enumerate() {
            exec = exec.with_stdout_on_occurrence(SAMPLE_LABEL, index + 1, body.clone());
        }
        exec
    }

    fn run_quiet(exec: &RecordingExecutor) -> BakeOutcome {
        run(&policy(), &TARGET, exec, &mut |_| {})
    }

    fn shells(exec: &RecordingExecutor) -> Vec<String> {
        exec.calls()
            .into_iter()
            .filter_map(|c| match c {
                RecordedCall::Run { shell, .. } => Some(shell),
                RecordedCall::Upload { .. } => None,
            })
            .collect()
    }

    #[test]
    fn a_healthy_bake_passes_after_every_sample() {
        let exec = exec_with(&[
            sample_stdout(10, 0, 5, 0),
            sample_stdout(110, 0, 5, 0),
            sample_stdout(210, 1, 5, 0),
            sample_stdout(310, 1, 5, 0),
        ]);
        // 301 new responses, less the 3 metric requests of the bake.
        assert_eq!(
            run_quiet(&exec),
            BakeOutcome::Passed {
                responses: 298,
                judged: true,
            }
        );
        // 30 s at 10 s: a baseline and three samples.
        assert_eq!(exec.run_labels(), vec![SAMPLE_LABEL; 4]);
        let shells = shells(&exec);
        assert!(shells[0].starts_with("curl "), "{shells:?}");
        assert!(shells[1].starts_with("sleep 10 && "), "{shells:?}");
    }

    #[test]
    fn an_injected_error_spike_breaches_and_stops_sampling() {
        let exec = exec_with(&[
            sample_stdout(10, 0, 5, 0),
            sample_stdout(110, 0, 5, 0),
            // The spike: 30 new 5xx.
            sample_stdout(210, 30, 5, 0),
            sample_stdout(310, 30, 5, 0),
        ]);
        assert_eq!(
            run_quiet(&exec),
            BakeOutcome::Breached(Breach::ErrorRate {
                errors: 30,
                responses: 228,
                max_ppm: 14_400,
            })
        );
        assert_eq!(exec.run_labels().len(), 3, "no sample after the breach");
    }

    #[test]
    fn a_systemd_restart_breaches() {
        let exec = exec_with(&[sample_stdout(10, 0, 5, 0), sample_stdout(500, 0, 5, 1)]);
        assert_eq!(run_quiet(&exec), BakeOutcome::Breached(Breach::Restarted));
    }

    #[test]
    fn an_idle_app_does_not_pass_on_its_own_metric_requests() {
        // Each sample sees only the previous metric request.
        let exec = exec_with(&[
            sample_stdout(0, 0, 1, 0),
            sample_stdout(1, 0, 1, 0),
            sample_stdout(2, 0, 1, 0),
            sample_stdout(3, 0, 1, 0),
        ]);
        assert_eq!(
            run_quiet(&exec),
            BakeOutcome::Passed {
                responses: 0,
                judged: false,
            }
        );
    }

    #[test]
    fn one_failed_sample_is_retried() {
        let exec = RecordingExecutor::new()
            .with_stdout_on_occurrence(SAMPLE_LABEL, 1, sample_stdout(0, 0, 1, 0))
            .failing_on_occurrence(SAMPLE_LABEL, 2)
            .with_stdout(SAMPLE_LABEL, sample_stdout(500, 0, 1, 0));
        let outcome = run_quiet(&exec);
        assert!(
            matches!(outcome, BakeOutcome::Passed { judged: true, .. }),
            "{outcome:?}"
        );
        let shells = shells(&exec);
        assert!(shells[1].starts_with("sleep 10 && "), "{shells:?}");
        assert!(
            shells[2].starts_with("curl "),
            "the retry does not sleep: {shells:?}"
        );
    }

    #[test]
    fn a_retried_baseline_subtracts_only_its_successful_request() {
        let mut p = policy();
        p.duration_secs = 10;
        let exec = RecordingExecutor::new()
            .failing_on_occurrence(SAMPLE_LABEL, 1)
            .with_stdout_on_occurrence(SAMPLE_LABEL, 2, sample_stdout(0, 0, 1, 0))
            .with_stdout_on_occurrence(SAMPLE_LABEL, 3, sample_stdout(21, 0, 1, 0));
        // 21 new responses, less 1 metric request: exactly min_requests.
        assert_eq!(
            run(&p, &TARGET, &exec, &mut |_| {}),
            BakeOutcome::Passed {
                responses: 20,
                judged: true,
            }
        );
    }

    #[test]
    fn a_retried_sample_does_not_count_its_failed_request_as_an_app_error() {
        let mut p = policy();
        p.duration_secs = 20;
        // Each sample's first attempt fails with a 5xx from /actuator/metrics.
        // The app counts that 5xx, so the retry sees one more error each time.
        let exec = RecordingExecutor::new()
            .with_stdout_on_occurrence(SAMPLE_LABEL, 1, sample_stdout(0, 0, 1, 0))
            .failing_on_occurrence(SAMPLE_LABEL, 2)
            .with_stdout_on_occurrence(SAMPLE_LABEL, 3, sample_stdout(100, 1, 1, 0))
            .failing_on_occurrence(SAMPLE_LABEL, 4)
            .with_stdout_on_occurrence(SAMPLE_LABEL, 5, sample_stdout(200, 2, 1, 0));
        let outcome = run(&p, &TARGET, &exec, &mut |_| {});
        assert!(
            matches!(outcome, BakeOutcome::Passed { judged: true, .. }),
            "the bake's own failed requests are not app errors: {outcome:?}"
        );
    }

    #[test]
    fn two_failed_samples_in_a_row_breach() {
        let exec = RecordingExecutor::new()
            .with_stdout_on_occurrence(SAMPLE_LABEL, 1, sample_stdout(0, 0, 1, 0))
            .failing_on_occurrence(SAMPLE_LABEL, 2)
            .failing_on_occurrence(SAMPLE_LABEL, 3);
        let BakeOutcome::Breached(Breach::Unreachable(why)) = run_quiet(&exec) else {
            panic!("expected an unreachable breach");
        };
        assert!(why.contains("bake-sample"), "{why}");
    }

    #[test]
    fn an_unreadable_baseline_is_a_breach() {
        let exec = RecordingExecutor::new().with_stdout(SAMPLE_LABEL, "not json");
        assert!(matches!(
            run_quiet(&exec),
            BakeOutcome::Breached(Breach::Unreachable(_))
        ));
    }

    #[test]
    fn the_last_sample_sleeps_only_the_remainder() {
        let mut p = policy();
        p.duration_secs = 25;
        let exec = exec_with(&[
            sample_stdout(0, 0, 1, 0),
            sample_stdout(100, 0, 1, 0),
            sample_stdout(200, 0, 1, 0),
            sample_stdout(300, 0, 1, 0),
        ]);
        run(&p, &TARGET, &exec, &mut |_| {});
        let last = shells(&exec).pop().expect("a sample");
        assert!(last.starts_with("sleep 5 && "), "{last}");
    }

    #[test]
    fn progress_reports_each_sample() {
        let exec = exec_with(&[
            sample_stdout(0, 0, 1, 0),
            sample_stdout(100, 0, 1, 0),
            sample_stdout(200, 0, 1, 0),
            sample_stdout(300, 0, 1, 0),
        ]);
        let mut lines = Vec::new();
        run(&policy(), &TARGET, &exec, &mut |l| {
            lines.push(l.to_owned());
        });
        assert_eq!(
            lines,
            vec![
                "bake 10/30 s: 99 responses, 0 5xx, p99 1 ms",
                "bake 20/30 s: 198 responses, 0 5xx, p99 1 ms",
                "bake 30/30 s: 297 responses, 0 5xx, p99 1 ms",
            ]
        );
    }

    // ── resolve_policy ───────────────────────────────────────────────────────

    fn slo(name: &str, objective: f64, sli: SliKind, route: Option<&str>, ms: Option<u64>) -> Slo {
        autumn_web::slo::validate(&[SloConfig {
            name: name.to_owned(),
            objective,
            sli,
            route: route.map(str::to_owned),
            threshold_ms: ms,
            description: None,
        }])
        .expect("valid")
        .remove(0)
    }

    fn on(duration_secs: u64) -> DeployBakeConfig {
        DeployBakeConfig {
            duration_secs,
            ..DeployBakeConfig::default()
        }
    }

    #[test]
    fn the_bake_is_off_by_default() {
        assert_eq!(
            resolve_policy(&DeployBakeConfig::default(), &[], None),
            Ok(None)
        );
        assert_eq!(resolve_policy(&on(300), &[], Some(0)), Ok(None));
    }

    #[test]
    fn the_flag_turns_the_bake_on() {
        let p = resolve_policy(&DeployBakeConfig::default(), &[], Some(60))
            .expect("ok")
            .expect("on");
        assert_eq!(p.duration_secs, 60);
        assert_eq!(p.interval_secs, 10);
        assert_eq!(p.min_requests, 20);
        assert_eq!(p.max_error_ppm, DEFAULT_MAX_ERROR_PPM);
        assert!(p.latency.is_empty());
    }

    #[test]
    fn availability_slos_set_the_error_limit_at_the_fast_burn_rate() {
        let slos = [
            slo("loose", 99.0, SliKind::Availability, None, None),
            slo("strict", 99.9, SliKind::Availability, None, None),
            // A route SLO is not measurable from the whole-app counters.
            slo("route", 99.99, SliKind::Availability, Some("/x"), None),
        ];
        let p = resolve_policy(&on(60), &slos, None)
            .expect("ok")
            .expect("on");
        assert_eq!(p.max_error_ppm, max_error_ppm(1_000, ROLLBACK_BURN_TENTHS));
        assert!(p.source.contains("strict"), "{}", p.source);
    }

    #[test]
    fn latency_slos_set_one_gate_per_quantile() {
        let slos = [
            slo("slow", 99.9, SliKind::Latency, None, Some(500)),
            slo("slower", 99.95, SliKind::Latency, None, Some(1_000)),
            slo("fast", 99.0, SliKind::Latency, None, Some(100)),
            slo("route", 99.9, SliKind::Latency, Some("/x"), Some(5)),
        ];
        let p = resolve_policy(&on(60), &slos, None)
            .expect("ok")
            .expect("on");
        assert_eq!(
            p.latency,
            vec![
                LatencyGate {
                    quantile: Quantile::P95,
                    max_ms: 100,
                },
                LatencyGate {
                    quantile: Quantile::P99,
                    max_ms: 500,
                },
            ]
        );
        assert!(p.source.contains("SLO fast, SLO slow"), "{}", p.source);
        // 99.95 % allows 0.72 % slow requests at 14.4x: below what p99 sees.
        assert_eq!(p.warnings.len(), 2, "{:?}", p.warnings);
        assert!(p.warnings[0].starts_with("SLO slower:"), "{:?}", p.warnings);
        assert_eq!(p.warnings[1], APP_WIDE_LATENCY);
    }

    #[test]
    fn an_slo_latency_gate_says_it_reads_the_app_wide_quantile() {
        let p = resolve_policy(
            &on(60),
            &[slo("l", 99.0, SliKind::Latency, None, Some(250))],
            None,
        )
        .expect("ok")
        .expect("on");
        assert_eq!(p.warnings, vec![APP_WIDE_LATENCY.to_owned()]);
        // An operator limit is not an SLO, so it needs no such note.
        let mut config = on(60);
        config.max_p99_ms = Some(250);
        let p = resolve_policy(
            &config,
            &[slo("l", 99.0, SliKind::Latency, None, Some(250))],
            None,
        )
        .expect("ok")
        .expect("on");
        assert!(p.warnings.is_empty(), "{:?}", p.warnings);
    }

    #[test]
    fn the_quantile_follows_the_rollback_burn() {
        // The share of slow requests at 14.4x: 1.44 %, 14.4 %, 28.8 %, 72 %, 100 %.
        for (objective, quantile) in [
            (99.9, Quantile::P99),
            (99.0, Quantile::P95),
            (98.0, Quantile::P95),
            (95.0, Quantile::P50),
            (90.0, Quantile::P50),
        ] {
            let p = resolve_policy(
                &on(60),
                &[slo("l", objective, SliKind::Latency, None, Some(250))],
                None,
            )
            .expect("ok")
            .expect("on");
            assert_eq!(p.latency[0].quantile, quantile, "{objective}");
            assert_eq!(p.warnings, vec![APP_WIDE_LATENCY], "{objective}");
        }
        // 99.99 %: 0.144 % slow at 14.4x, which p99 cannot see.
        let p = resolve_policy(
            &on(60),
            &[slo("l", 99.99, SliKind::Latency, None, Some(250))],
            None,
        )
        .expect("ok")
        .expect("on");
        assert_eq!(p.latency[0].quantile, Quantile::P99);
        assert!(p.warnings[0].contains("0.144%"), "{:?}", p.warnings);
    }

    #[test]
    fn explicit_limits_win_over_slos() {
        let config = DeployBakeConfig {
            duration_secs: 60,
            max_error_rate: Some(0.02),
            max_p99_ms: Some(800),
            ..DeployBakeConfig::default()
        };
        let slos = [
            slo("a", 99.9, SliKind::Availability, None, None),
            slo("l", 95.0, SliKind::Latency, None, Some(100)),
        ];
        let p = resolve_policy(&config, &slos, None)
            .expect("ok")
            .expect("on");
        assert_eq!(p.max_error_ppm, 20_000);
        assert_eq!(
            p.latency,
            vec![LatencyGate {
                quantile: Quantile::P99,
                max_ms: 800,
            }]
        );
    }

    #[test]
    fn a_bad_error_rate_is_rejected() {
        for bad in [-0.1, 1.5, f64::NAN] {
            let config = DeployBakeConfig {
                duration_secs: 60,
                max_error_rate: Some(bad),
                ..DeployBakeConfig::default()
            };
            assert!(resolve_policy(&config, &[], None).is_err(), "{bad}");
        }
    }

    #[test]
    fn the_interval_is_at_least_one_and_at_most_the_duration() {
        let config = DeployBakeConfig {
            duration_secs: 5,
            interval_secs: 0,
            ..DeployBakeConfig::default()
        };
        let p = resolve_policy(&config, &[], None).expect("ok").expect("on");
        assert_eq!(p.interval_secs, 1);
        let config = DeployBakeConfig {
            duration_secs: 5,
            interval_secs: 60,
            ..DeployBakeConfig::default()
        };
        let p = resolve_policy(&config, &[], None).expect("ok").expect("on");
        assert_eq!(p.interval_secs, 5);
    }

    #[test]
    fn describe_states_every_limit() {
        let mut p = policy();
        p.duration_secs = 300;
        p.source = "SLO availability at 14.4x burn, SLO latency".to_owned();
        assert_eq!(
            p.describe(),
            "bake for 300 s, sample every 10 s; roll back when the 5xx ratio is above 1.44% \
             or p99 latency is above 250 ms (after 20 responses; limits from SLO \
             availability at 14.4x burn, SLO latency)"
        );
        p.latency.clear();
        p.source = "the default 5% error limit".to_owned();
        p.max_error_ppm = DEFAULT_MAX_ERROR_PPM;
        assert_eq!(
            p.describe(),
            "bake for 300 s, sample every 10 s; roll back when the 5xx ratio is above 5% \
             (after 20 responses; limits from the default 5% error limit)"
        );
    }

    #[test]
    fn breach_messages_name_the_numbers() {
        let text = Breach::ErrorRate {
            errors: 37,
            responses: 431,
            max_ppm: 14_400,
        }
        .to_string();
        assert_eq!(
            text,
            "37 of 431 responses were 5xx (8.5846%), above the limit of 1.44%"
        );
        let text = Breach::Latency {
            quantile: Quantile::P99,
            ms: 900,
            max_ms: 250,
        }
        .to_string();
        assert_eq!(text, "p99 latency is 900 ms, above the limit of 250 ms");
    }
}