autumn-web 0.5.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
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
use bytes::Bytes;
use futures::StreamExt as FuturesStreamExt;

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::task::{Context, Poll};
use std::time::{Duration, Instant};

use axum::body::Body;
use axum::http::{HeaderMap, Method, Request, Response, StatusCode, request::Parts};
use axum::response::IntoResponse;
use sha2::Digest as _;
use tower::{Layer, Service};

static IDEMPOTENCY_KEY_HEADER: &str = "idempotency-key";
static X_IDEMPOTENT_REPLAYED: &str = "x-idempotent-replayed";

/// Maximum response body size stored in the idempotency cache. Responses
/// larger than this are returned to the client as-is but not cached, so a
/// subsequent retry with the same key will re-execute the handler.
const MAX_CACHEABLE_RESPONSE_BODY: usize = 10 * 1024 * 1024; // 10 MiB

/// Fallback request body read limit used when the upload middleware extension
/// is absent (e.g. when `IdempotencyLayer` is used directly without the full
/// framework stack). Matches the framework default for
/// `security.upload.max_request_size_bytes`.
const DEFAULT_REQUEST_BODY_LIMIT: usize = 32 * 1024 * 1024; // 32 MiB

const fn is_mutating_method(method: &Method) -> bool {
    matches!(
        *method,
        Method::POST | Method::PUT | Method::PATCH | Method::DELETE
    )
}

fn compute_body_hash(bytes: &[u8], content_type: Option<&[u8]>) -> Vec<u8> {
    let mut hasher = sha2::Sha256::new();
    hasher.update(b"content-type:");
    if let Some(content_type) = content_type {
        hasher.update(content_type);
    }
    hasher.update(b"\nbody:");
    hasher.update(bytes);
    hasher.finalize().to_vec()
}

fn hex_lower(bytes: impl AsRef<[u8]>) -> String {
    bytes.as_ref().iter().fold(
        String::with_capacity(bytes.as_ref().len() * 2),
        |mut out, byte| {
            use std::fmt::Write as _;
            let _ = write!(out, "{byte:02x}");
            out
        },
    )
}

fn principal_scope_digest(session_id: Option<&str>) -> String {
    let mut hasher = sha2::Sha256::new();
    hasher.update(b"authorization:");
    hasher.update(b"\nsession:");
    if let Some(session_id) = session_id {
        hasher.update(session_id.as_bytes());
    }
    hex_lower(hasher.finalize())
}

fn push_storage_key_component(hasher: &mut sha2::Sha256, label: &str, value: &[u8]) {
    hasher.update(label.as_bytes());
    hasher.update(b":");
    hasher.update((value.len() as u64).to_be_bytes());
    hasher.update(b":");
    hasher.update(value);
    hasher.update(b";");
}

/// Namespace the cache key by method, path, a stable principal digest, and the
/// client-supplied idempotency key.
///
/// Namespacing by method+path prevents cross-endpoint cache collisions (P2).
/// Namespacing by session scope prevents cross-principal collisions (P1) for
/// cookie-backed authenticated sessions. Request headers, including
/// `Authorization`, are intentionally excluded: client-controlled headers must
/// not let a retry force a fresh miss after a successful mutation. Opaque route
/// layers that resolve tenants, bearer principals, or policy state must use the
/// fail-closed replay path instead of storage-key partitioning. Each component
/// is length-delimited inside a SHA-256 digest so raw `:` bytes in paths or
/// client-controlled keys cannot synthesize another storage key.
#[derive(Clone)]
struct StorageKeyContext {
    idempotency_key: String,
    method: Method,
    target: String,
}

impl StorageKeyContext {
    fn from_parts(idempotency_key: String, parts: &axum::http::request::Parts) -> Self {
        let target = parts
            .uri
            .path_and_query()
            .map_or_else(|| parts.uri.path().to_owned(), |pq| pq.as_str().to_owned());
        Self {
            idempotency_key,
            method: parts.method.clone(),
            target,
        }
    }

    fn storage_key(&self, session_id: Option<&str>) -> String {
        build_storage_key(
            &self.idempotency_key,
            self.method.as_str(),
            &self.target,
            session_id,
        )
    }
}

fn build_storage_key(
    idempotency_key: &str,
    method: &str,
    target: &str,
    session_id: Option<&str>,
) -> String {
    let principal = principal_scope_digest(session_id);
    let mut hasher = sha2::Sha256::new();
    push_storage_key_component(&mut hasher, "method", method.as_bytes());
    push_storage_key_component(&mut hasher, "target", target.as_bytes());
    push_storage_key_component(&mut hasher, "scope-header-count", b"0");
    push_storage_key_component(&mut hasher, "principal", principal.as_bytes());
    push_storage_key_component(&mut hasher, "idempotency-key", idempotency_key.as_bytes());
    format!("v2:{}", hex_lower(hasher.finalize()))
}

async fn storage_session_id_for_parts(parts: &axum::http::request::Parts) -> Option<String> {
    let session_scope = parts
        .extensions
        .get::<IdempotencySessionScope>()
        .and_then(|scope| scope.session_id.as_deref().map(str::to_owned));
    let session = parts.extensions.get::<crate::session::Session>().cloned();
    if session_scope.is_some() {
        session_scope
    } else if let Some(session) = session
        && session.is_cookie_backed().await
    {
        Some(session.id().await)
    } else {
        None
    }
}

fn stale_cookie_session_id_for_parts(parts: &axum::http::request::Parts) -> Option<String> {
    parts
        .extensions
        .get::<IdempotencySessionScope>()
        .and_then(|scope| scope.stale_cookie_session_id.as_deref().map(str::to_owned))
}

fn extract_replay_headers(headers: &HeaderMap) -> Vec<(String, Vec<u8>)> {
    extract_replay_headers_with_policy(headers, false)
}

fn extract_finalized_session_replay_headers(headers: &HeaderMap) -> Vec<(String, Vec<u8>)> {
    extract_replay_headers_with_policy(headers, true)
}

fn extract_replay_headers_with_policy(
    headers: &HeaderMap,
    include_set_cookie: bool,
) -> Vec<(String, Vec<u8>)> {
    // Headers that must not be cached or replayed.
    // `set-cookie` is replayed only for finalized session-mutating responses
    // so lost successful mutations can deliver the session state they created.
    const SKIP: &[&str] = &[
        "connection",
        "transfer-encoding",
        "keep-alive",
        "upgrade",
        "proxy-authenticate",
        "proxy-authorization",
        "te",
        "trailer",
        "x-idempotent-replayed",
    ];
    headers
        .iter()
        .filter(|(name, _)| {
            !SKIP.contains(&name.as_str()) && (include_set_cookie || name.as_str() != "set-cookie")
        })
        .map(|(name, value)| (name.to_string(), value.as_bytes().to_vec()))
        .collect()
}

// ── Public types ──────────────────────────────────────────────────────────────

/// Stored response associated with an idempotency key.
#[derive(Clone)]
pub struct IdempotencyRecord {
    pub status: u16,
    pub headers: Vec<(String, Vec<u8>)>,
    pub body: Vec<u8>,
    pub metadata: Vec<(String, Vec<u8>)>,
}

const FINALIZED_SESSION_SCOPE_METADATA: &str = "__autumn.idempotency.finalized-session-scope";
const FINALIZED_SESSION_OLD_SCOPE: &[u8] = b"old-session-scope";
const FINALIZED_SESSION_CURRENT_SCOPE: &[u8] = b"current-session-scope";

fn finalized_session_record(
    mut record: IdempotencyRecord,
    scope: &'static [u8],
) -> IdempotencyRecord {
    record
        .metadata
        .retain(|(name, _)| name != FINALIZED_SESSION_SCOPE_METADATA);
    record
        .metadata
        .push((FINALIZED_SESSION_SCOPE_METADATA.to_owned(), scope.to_vec()));
    record
}

#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
pub struct IdempotencyCacheCommittedErrorResponse;

#[derive(Clone, Debug)]
pub(crate) struct IdempotencySessionScope {
    session_id: Option<String>,
    stale_cookie_session_id: Option<String>,
}

impl IdempotencySessionScope {
    #[must_use]
    pub(crate) const fn new(
        session_id: Option<String>,
        stale_cookie_session_id: Option<String>,
    ) -> Self {
        Self {
            session_id,
            stale_cookie_session_id,
        }
    }
}

#[doc(hidden)]
#[derive(Clone, Debug, Default)]
pub struct IdempotencyReplayMetadata {
    entries: Vec<(String, Vec<u8>)>,
}

impl IdempotencyReplayMetadata {
    #[must_use]
    pub const fn new(entries: Vec<(String, Vec<u8>)>) -> Self {
        Self { entries }
    }

    fn into_entries(self) -> Vec<(String, Vec<u8>)> {
        self.entries
    }
}

/// Request-scoped idempotency metadata made available to inner handlers.
///
/// The raw `Idempotency-Key` header is available via [`Self::key`]. The
/// scoped key is the framework's collision-safe, principal-scoped storage key
/// and is the safer value to reuse for durable side-effect deduplication.
#[derive(Clone, Debug)]
pub struct IdempotencyContext {
    key: String,
    scoped_key: String,
    mutation_sequence: Arc<AtomicU64>,
}

impl IdempotencyContext {
    #[must_use]
    pub(crate) fn new(key: String, scoped_key: String) -> Self {
        Self {
            key,
            scoped_key,
            mutation_sequence: Arc::new(AtomicU64::new(0)),
        }
    }

    #[must_use]
    pub fn key(&self) -> &str {
        &self.key
    }

    #[must_use]
    pub fn scoped_key(&self) -> &str {
        &self.scoped_key
    }

    /// Return the next request-local mutation discriminator.
    ///
    /// Generated repository code uses this to distinguish multiple durable
    /// side effects produced by one idempotent request while keeping the same
    /// mutation slot stable across duplicate request attempts.
    #[must_use]
    pub fn next_mutation_discriminator(&self) -> String {
        self.mutation_sequence
            .fetch_add(1, Ordering::Relaxed)
            .to_string()
    }
}

impl PartialEq for IdempotencyContext {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key && self.scoped_key == other.scoped_key
    }
}

impl Eq for IdempotencyContext {}

/// Cache entry wrapping a record with expiry and request body fingerprint.
#[derive(Clone)]
pub struct IdempotencyEntry {
    pub record: IdempotencyRecord,
    pub body_hash: Vec<u8>,
    pub expires_at: Instant,
}

// ── Store trait ───────────────────────────────────────────────────────────────

/// Error returned when an idempotency backend fails to persist a successful
/// mutation response.
#[derive(Debug, Clone, thiserror::Error)]
#[error("{message}")]
pub struct IdempotencyStoreError {
    message: String,
}

impl IdempotencyStoreError {
    #[must_use]
    pub fn backend(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
}

/// Pluggable storage backend for idempotency entries.
///
/// Implementors must be `Send + Sync + 'static` to be used across async tasks.
/// All methods are synchronous; long-running I/O backends should use
/// [`tokio::task::block_in_place`] internally.
pub trait IdempotencyStore: Send + Sync + 'static {
    /// Return the cached entry if it exists and has not expired.
    fn get(&self, key: &str) -> Option<IdempotencyEntry>;

    /// Return the cached entry if it exists, surfacing backend read failures.
    ///
    /// Infallible stores can implement only [`Self::get`]. Fallible shared
    /// backends should override this method so lookup failures fail closed
    /// instead of being treated as cache misses that can duplicate mutations.
    ///
    /// # Errors
    ///
    /// Returns [`IdempotencyStoreError`] when the backend cannot determine
    /// whether a record exists for this key.
    fn try_get(&self, key: &str) -> Result<Option<IdempotencyEntry>, IdempotencyStoreError> {
        Ok(self.get(key))
    }

    /// Persist a response with the given TTL.
    fn set(&self, key: &str, record: IdempotencyRecord, body_hash: Vec<u8>, ttl: Duration);

    /// Persist a response with the given TTL, surfacing backend failures.
    ///
    /// Existing infallible stores can implement only [`Self::set`]. Fallible
    /// backends should override this method so the middleware can fail closed
    /// rather than reporting a cacheable success that was not stored.
    ///
    /// # Errors
    ///
    /// Returns [`IdempotencyStoreError`] when the backend cannot persist the
    /// response record.
    fn try_set(
        &self,
        key: &str,
        record: IdempotencyRecord,
        body_hash: Vec<u8>,
        ttl: Duration,
    ) -> Result<(), IdempotencyStoreError> {
        self.set(key, record, body_hash, ttl);
        Ok(())
    }

    /// Acquire an in-flight lock for `key`.
    ///
    /// Returns `true` if the lock was acquired (no concurrent request in flight)
    /// or `false` if another request is already processing this key.
    fn try_lock(&self, key: &str, lock_ttl: Duration) -> bool;

    /// Acquire an in-flight lock owned by a unique request token.
    ///
    /// Stores that support expiring locks should override this together with
    /// [`Self::unlock_owned`] so a stale request cannot release a newer lock
    /// acquired after the first lock expired.
    fn try_lock_owned(&self, key: &str, owner: &str, lock_ttl: Duration) -> bool {
        let _ = owner;
        self.try_lock(key, lock_ttl)
    }

    /// Release the in-flight lock for `key`.
    fn unlock(&self, key: &str);

    /// Release the in-flight lock only if it is still owned by `owner`.
    fn unlock_owned(&self, key: &str, owner: &str) {
        let _ = owner;
        self.unlock(key);
    }

    /// The preferred TTL for this store. Used by [`IdempotencyLayer::new`] as
    /// the default expiry when no explicit `.with_ttl()` is given. Defaults to
    /// 24 hours if the store does not override this method.
    fn default_ttl(&self) -> Duration {
        Duration::from_secs(86_400)
    }
}

// ── Memory store ──────────────────────────────────────────────────────────────

/// In-memory idempotency store backed by a `RwLock<HashMap>`.
///
/// Evicts expired entries lazily on `get`. In-flight markers remain held until
/// `unlock` or until their configured in-flight lock TTL expires.
///
/// Suitable for single-process deployments and integration tests. For
/// multi-replica deployments configure `backend = "redis"` in `autumn.toml`.
pub struct MemoryIdempotencyStore {
    entries: RwLock<HashMap<String, IdempotencyEntry>>,
    in_flight: RwLock<HashMap<String, MemoryInFlightLock>>,
    /// Counts `set` calls to trigger periodic expired-entry eviction.
    write_count: AtomicU64,
    default_ttl: Duration,
}

struct MemoryInFlightLock {
    owner: String,
    expires_at: Instant,
}

impl MemoryIdempotencyStore {
    #[must_use]
    pub fn new(default_ttl: Duration) -> Self {
        Self {
            entries: RwLock::new(HashMap::new()),
            in_flight: RwLock::new(HashMap::new()),
            write_count: AtomicU64::new(0),
            default_ttl,
        }
    }
}

impl IdempotencyStore for MemoryIdempotencyStore {
    fn get(&self, key: &str) -> Option<IdempotencyEntry> {
        // Release the read lock immediately after cloning.
        let entry = self.entries.read().unwrap().get(key).cloned();
        entry.filter(|e| e.expires_at > Instant::now())
    }

    fn set(&self, key: &str, record: IdempotencyRecord, body_hash: Vec<u8>, ttl: Duration) {
        let entry = IdempotencyEntry {
            record,
            body_hash,
            expires_at: Instant::now() + ttl,
        };
        let mut entries = self.entries.write().unwrap();
        entries.insert(key.to_owned(), entry);
        // Periodically evict expired entries to bound memory growth for
        // long-running processes. O(N) scan is amortised over every 128 writes.
        let n = self.write_count.fetch_add(1, Ordering::Relaxed);
        if n.is_multiple_of(128) {
            let now = Instant::now();
            entries.retain(|_, v| v.expires_at > now);
        }
    }

    fn try_lock(&self, key: &str, lock_ttl: Duration) -> bool {
        self.try_lock_owned(key, "", lock_ttl)
    }

    fn try_lock_owned(&self, key: &str, owner: &str, lock_ttl: Duration) -> bool {
        let now = Instant::now();
        let mut in_flight = self.in_flight.write().unwrap();
        // Check only the requested key's active in-flight marker.
        if let Some(lock) = in_flight.get(key)
            && lock.expires_at > now
        {
            return false; // still in flight
        }
        // Not locked: acquire until the handler finishes, unlocks, or the
        // safety TTL expires after cancellation.
        let ttl = if lock_ttl.is_zero() {
            Duration::from_secs(1)
        } else {
            lock_ttl
        };
        in_flight.insert(
            key.to_owned(),
            MemoryInFlightLock {
                owner: owner.to_owned(),
                expires_at: now + ttl,
            },
        );
        true
    }

    fn unlock(&self, key: &str) {
        self.in_flight.write().unwrap().remove(key);
    }

    fn unlock_owned(&self, key: &str, owner: &str) {
        let mut in_flight = self.in_flight.write().unwrap();
        if in_flight
            .get(key)
            .is_some_and(|lock| lock.owner.as_str() == owner)
        {
            in_flight.remove(key);
        }
    }

    fn default_ttl(&self) -> Duration {
        self.default_ttl
    }
}

// ── Redis store ───────────────────────────────────────────────────────────────

#[cfg(feature = "redis")]
mod redis_store {
    use super::{IdempotencyEntry, IdempotencyRecord, IdempotencyStore, IdempotencyStoreError};
    use redis::{AsyncCommands, Client, aio::ConnectionManager, aio::ConnectionManagerConfig};
    use serde::{Deserialize, Serialize};
    use std::time::{Duration, Instant};

    #[derive(Serialize, Deserialize)]
    struct StoredEntry {
        status: u16,
        headers: Vec<(String, Vec<u8>)>,
        body: Vec<u8>,
        #[serde(default)]
        metadata: Vec<(String, Vec<u8>)>,
        body_hash: Vec<u8>,
    }

    /// Redis-backed idempotency store for multi-replica deployments.
    ///
    /// Configured via `[idempotency.redis]` in `autumn.toml` or
    /// `AUTUMN_IDEMPOTENCY__REDIS__URL` env var.
    pub struct RedisIdempotencyStore {
        connection: ConnectionManager,
        key_prefix: String,
    }

    impl RedisIdempotencyStore {
        /// Creates a [`RedisIdempotencyStore`] from the application idempotency config.
        ///
        /// # Errors
        /// Returns an error string if no Redis URL is configured or if the Redis
        /// client cannot be opened.
        pub fn from_config(config: &crate::config::IdempotencyConfig) -> Result<Self, String> {
            let url = config
                .redis
                .url
                .as_deref()
                .filter(|u| !u.trim().is_empty())
                .ok_or_else(|| {
                    "Redis idempotency backend requires a URL. \
                     Set AUTUMN_IDEMPOTENCY__REDIS__URL or \
                     [idempotency.redis] url in autumn.toml."
                        .to_owned()
                })?;
            let client = Client::open(url).map_err(|e| e.to_string())?;
            let connection =
                ConnectionManager::new_lazy_with_config(client, ConnectionManagerConfig::new())
                    .map_err(|e| e.to_string())?;
            Ok(Self {
                connection,
                key_prefix: config.redis.key_prefix.clone(),
            })
        }

        fn entry_key(&self, key: &str) -> String {
            format!("{}:entry:{}", self.key_prefix, key)
        }

        fn lock_key(&self, key: &str) -> String {
            format!("{}:lock:{}", self.key_prefix, key)
        }
    }

    impl IdempotencyStore for RedisIdempotencyStore {
        fn get(&self, key: &str) -> Option<IdempotencyEntry> {
            match self.try_get(key) {
                Ok(entry) => entry,
                Err(error) => {
                    tracing::warn!(
                        error = %error,
                        "Redis GET failed for idempotency key"
                    );
                    None
                }
            }
        }

        fn try_get(&self, key: &str) -> Result<Option<IdempotencyEntry>, IdempotencyStoreError> {
            let redis_key = self.entry_key(key);
            let mut conn = self.connection.clone();
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async move {
                    let data: Option<Vec<u8>> = conn.get(&redis_key).await.map_err(|e| {
                        IdempotencyStoreError::backend(format!(
                            "failed to read idempotency entry from Redis: {e}"
                        ))
                    })?;
                    data.map(|bytes| {
                        serde_json::from_slice::<StoredEntry>(&bytes)
                            .map(|e| {
                                IdempotencyEntry {
                                    record: IdempotencyRecord {
                                        status: e.status,
                                        headers: e.headers,
                                        body: e.body,
                                        metadata: e.metadata,
                                    },
                                    body_hash: e.body_hash,
                                    // Redis manages TTL natively. Use a fixed 24 h offset
                                    // so the in-process expiry check never fires early.
                                    expires_at: Instant::now() + Duration::from_secs(86_400),
                                }
                            })
                            .map_err(|e| {
                                IdempotencyStoreError::backend(format!(
                                    "failed to deserialize idempotency entry from Redis: {e}"
                                ))
                            })
                    })
                    .transpose()
                })
            })
        }

        fn set(&self, key: &str, record: IdempotencyRecord, body_hash: Vec<u8>, ttl: Duration) {
            if let Err(error) = self.try_set(key, record, body_hash, ttl) {
                tracing::warn!(
                    error = %error,
                    "Failed to persist idempotency entry to Redis"
                );
            }
        }

        fn try_set(
            &self,
            key: &str,
            record: IdempotencyRecord,
            body_hash: Vec<u8>,
            ttl: Duration,
        ) -> Result<(), IdempotencyStoreError> {
            let redis_key = self.entry_key(key);
            let mut conn = self.connection.clone();
            let entry = StoredEntry {
                status: record.status,
                headers: record.headers,
                body: record.body,
                metadata: record.metadata,
                body_hash,
            };
            let bytes = serde_json::to_vec(&entry).map_err(|e| {
                IdempotencyStoreError::backend(format!(
                    "failed to serialize idempotency entry: {e}"
                ))
            })?;
            let ttl_secs = ttl.as_secs().max(1);
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async move {
                    conn.set_ex::<_, _, ()>(&redis_key, bytes, ttl_secs)
                        .await
                        .map_err(|e| {
                            IdempotencyStoreError::backend(format!(
                                "failed to persist idempotency entry to Redis: {e}"
                            ))
                        })
                })
            })
        }

        fn try_lock(&self, key: &str, lock_ttl: Duration) -> bool {
            self.try_lock_owned(key, "", lock_ttl)
        }

        fn try_lock_owned(&self, key: &str, owner: &str, lock_ttl: Duration) -> bool {
            let lock_key = self.lock_key(key);
            let lock_ttl_secs = lock_ttl.as_secs().max(1);
            let mut conn = self.connection.clone();
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async move {
                    let result: Result<Option<String>, _> = redis::cmd("SET")
                        .arg(&lock_key)
                        .arg(owner)
                        .arg("NX")
                        .arg("EX")
                        .arg(lock_ttl_secs)
                        .query_async(&mut conn)
                        .await;
                    match result {
                        Ok(opt) => opt.is_some(), // Some("OK") = acquired, None = already held
                        Err(e) => {
                            // Redis unavailable: fail closed so concurrent retries during an
                            // outage cannot both enter the handler and duplicate side effects.
                            // Clients receive 409 and should retry; once Redis recovers the
                            // lock can be acquired normally.
                            tracing::warn!(
                                error = %e,
                                "Redis idempotency lock unavailable; \
                                 failing closed to prevent duplicate processing"
                            );
                            false
                        }
                    }
                })
            })
        }

        fn unlock(&self, key: &str) {
            let lock_key = self.lock_key(key);
            let mut conn = self.connection.clone();
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async move {
                    let _: Result<(), _> = conn.del(&lock_key).await;
                });
            });
        }

        fn unlock_owned(&self, key: &str, owner: &str) {
            let lock_key = self.lock_key(key);
            let mut conn = self.connection.clone();
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async move {
                    let _: Result<i32, _> = redis::Script::new(
                        "if redis.call('GET', KEYS[1]) == ARGV[1] then \
                         return redis.call('DEL', KEYS[1]) else return 0 end",
                    )
                    .key(&lock_key)
                    .arg(owner)
                    .invoke_async(&mut conn)
                    .await;
                });
            });
        }
    }
}

#[cfg(feature = "redis")]
pub use redis_store::RedisIdempotencyStore;

#[doc(hidden)]
#[derive(Clone)]
pub struct IdempotencyReplayResponse {
    record: IdempotencyRecord,
}

impl IdempotencyReplayResponse {
    fn into_response(self) -> Response<Body> {
        response_from_record(self.record)
    }

    #[must_use]
    pub fn metadata(&self, key: &str) -> Option<&[u8]> {
        self.record
            .metadata
            .iter()
            .find(|(name, _)| name == key)
            .map(|(_, value)| value.as_slice())
    }
}

#[doc(hidden)]
#[must_use]
pub fn __replay_response(
    replay: &Option<axum::extract::Extension<IdempotencyReplayResponse>>,
) -> Option<Response<Body>> {
    replay
        .as_ref()
        .map(|axum::extract::Extension(replay)| replay.clone().into_response())
}

#[doc(hidden)]
#[must_use]
pub fn __replay_finalized_session_response(
    replay: &Option<axum::extract::Extension<IdempotencyReplayResponse>>,
) -> Option<Response<Body>> {
    replay
        .as_ref()
        .and_then(|axum::extract::Extension(replay)| {
            let has_finalized_session_cookie = replay
                .record
                .headers
                .iter()
                .any(|(name, _)| name.eq_ignore_ascii_case("set-cookie"));
            let is_old_session_scope = replay.metadata(FINALIZED_SESSION_SCOPE_METADATA)
                == Some(FINALIZED_SESSION_OLD_SCOPE);
            (has_finalized_session_cookie && is_old_session_scope)
                .then(|| replay.clone().into_response())
        })
}

#[doc(hidden)]
pub async fn __replay_finalized_session_response_for_anonymous(
    session: &crate::session::Session,
    auth_session_key: &str,
    replay: &Option<axum::extract::Extension<IdempotencyReplayResponse>>,
) -> Option<Response<Body>> {
    if session.get(auth_session_key).await.is_some() {
        return None;
    }
    __replay_finalized_session_response(replay)
}

#[doc(hidden)]
#[must_use]
pub const fn __cache_committed_error_response(error: crate::AutumnError) -> crate::AutumnError {
    error.cache_idempotency_response()
}

#[doc(hidden)]
#[must_use]
pub fn __replay_metadata(
    replay: &Option<axum::extract::Extension<IdempotencyReplayResponse>>,
    key: &str,
) -> Option<Vec<u8>> {
    replay
        .as_ref()
        .and_then(|axum::extract::Extension(replay)| replay.metadata(key).map(<[u8]>::to_vec))
}

#[doc(hidden)]
pub enum IdempotencyReplayOr<T> {
    Replay(Response<Body>),
    Inner(T),
    InnerWithReplayMetadata(T, Vec<(String, Vec<u8>)>),
}

impl<T> IntoResponse for IdempotencyReplayOr<T>
where
    T: IntoResponse,
{
    fn into_response(self) -> Response<Body> {
        match self {
            Self::Replay(response) => response,
            Self::Inner(inner) => inner.into_response(),
            Self::InnerWithReplayMetadata(inner, metadata) => {
                let mut response = inner.into_response();
                response
                    .extensions_mut()
                    .insert(IdempotencyReplayMetadata::new(metadata));
                response
            }
        }
    }
}

/// Inner route layer used by Autumn-generated handlers to stop before the
/// mutating handler when an outer idempotency layer has already found a replay.
#[derive(Clone, Copy, Debug, Default)]
pub struct IdempotencyReplayLayer;

#[derive(Clone)]
pub struct IdempotencyReplayService<S> {
    inner: S,
}

impl<S> Layer<S> for IdempotencyReplayLayer {
    type Service = IdempotencyReplayService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        IdempotencyReplayService { inner }
    }
}

impl<S> Service<Request<Body>> for IdempotencyReplayService<S>
where
    S: Service<Request<Body>, Response = Response<Body>, Error = std::convert::Infallible>
        + Clone
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    type Response = Response<Body>;
    type Error = std::convert::Infallible;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: Request<Body>) -> Self::Future {
        if let Some(replay) = req.extensions_mut().remove::<IdempotencyReplayResponse>() {
            return Box::pin(async move { Ok(replay.into_response()) });
        }

        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);
        Box::pin(async move { inner.call(req).await })
    }
}

// ── Layer ─────────────────────────────────────────────────────────────────────

/// Tower [`Layer`] that enforces HTTP idempotency semantics per IETF
/// `draft-ietf-httpapi-idempotency-key-header`.
///
/// Applies only to mutating HTTP methods (POST, PUT, PATCH, DELETE).
/// Requests without an `Idempotency-Key` header are passed through unchanged.
///
/// - **Cache hit, same body**: replays the stored response with
///   `X-Idempotent-Replayed: true` and skips the handler.
/// - **Cache hit, different body**: returns `422 Unprocessable Entity`.
/// - **Concurrent duplicate** (same key, already in flight): returns
///   `409 Conflict` with `Retry-After: 1`.
/// - **Cache miss**: forwards to the handler, stores the response.
#[derive(Clone)]
pub struct IdempotencyLayer {
    store: Arc<dyn IdempotencyStore>,
    ttl: Duration,
    in_flight_ttl: Duration,
    replay_through_inner: bool,
    fail_closed_on_replay: bool,
    metrics: Option<crate::middleware::MetricsCollector>,
}

impl IdempotencyLayer {
    #[must_use]
    pub fn new(store: Arc<dyn IdempotencyStore>) -> Self {
        let ttl = store.default_ttl();
        Self {
            store,
            ttl,
            in_flight_ttl: ttl,
            replay_through_inner: false,
            fail_closed_on_replay: false,
            metrics: None,
        }
    }

    #[must_use]
    pub const fn with_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = ttl;
        self
    }

    #[must_use]
    pub const fn with_in_flight_ttl(mut self, ttl: Duration) -> Self {
        self.in_flight_ttl = ttl;
        self
    }

    #[must_use]
    pub const fn replay_through_inner(mut self) -> Self {
        self.replay_through_inner = true;
        self.fail_closed_on_replay = false;
        self
    }

    #[must_use]
    pub const fn fail_closed_on_replay(mut self) -> Self {
        self.replay_through_inner = false;
        self.fail_closed_on_replay = true;
        self
    }

    #[must_use]
    pub fn with_metrics(mut self, metrics: crate::middleware::MetricsCollector) -> Self {
        self.metrics = Some(metrics);
        self
    }
}

impl<S> Layer<S> for IdempotencyLayer {
    type Service = IdempotencyService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        IdempotencyService {
            inner,
            store: self.store.clone(),
            ttl: self.ttl,
            in_flight_ttl: self.in_flight_ttl,
            replay_through_inner: self.replay_through_inner,
            fail_closed_on_replay: self.fail_closed_on_replay,
            metrics: self.metrics.clone(),
        }
    }
}

// ── Service ───────────────────────────────────────────────────────────────────

/// Tower [`Service`] produced by [`IdempotencyLayer`].
#[derive(Clone)]
pub struct IdempotencyService<S> {
    inner: S,
    store: Arc<dyn IdempotencyStore>,
    ttl: Duration,
    in_flight_ttl: Duration,
    replay_through_inner: bool,
    fail_closed_on_replay: bool,
    metrics: Option<crate::middleware::MetricsCollector>,
}

struct IdempotencyRequestConfig {
    store: Arc<dyn IdempotencyStore>,
    ttl: Duration,
    in_flight_ttl: Duration,
    replay_through_inner: bool,
    fail_closed_on_replay: bool,
    metrics: Option<crate::middleware::MetricsCollector>,
}

impl<S> Service<Request<Body>> for IdempotencyService<S>
where
    S: Service<Request<Body>, Response = Response<Body>, Error = std::convert::Infallible>
        + Clone
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    type Response = Response<Body>;
    type Error = std::convert::Infallible;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        // Clone-and-swap: the instance that was polled ready becomes `inner`
        // for this call; `self.inner` is replaced with a fresh clone for the
        // next call. This preserves Tower backpressure semantics.
        let clone = self.inner.clone();
        let inner = std::mem::replace(&mut self.inner, clone);
        let config = IdempotencyRequestConfig {
            store: self.store.clone(),
            ttl: self.ttl,
            in_flight_ttl: self.in_flight_ttl,
            replay_through_inner: self.replay_through_inner,
            fail_closed_on_replay: self.fail_closed_on_replay,
            metrics: self.metrics.clone(),
        };
        Box::pin(handle_idempotent_request(inner, config, req))
    }
}

fn request_body_too_large_response() -> Response<Body> {
    Response::builder()
        .status(StatusCode::PAYLOAD_TOO_LARGE)
        .body(Body::from(
            "request body too large for idempotency middleware",
        ))
        .unwrap()
}

fn in_flight_conflict_response() -> Response<Body> {
    Response::builder()
        .status(StatusCode::CONFLICT)
        .header("retry-after", "1")
        .body(Body::from(
            "a request with this idempotency key is already being processed; \
             retry after 1 second",
        ))
        .unwrap()
}

fn replay_requires_inner_stop_response() -> Response<Body> {
    Response::builder()
        .status(StatusCode::CONFLICT)
        .body(Body::from(
            "idempotency replay requires an inner replay stop for this route",
        ))
        .unwrap()
}

pub(crate) fn persistence_failed_response() -> Response<Body> {
    Response::builder()
        .status(StatusCode::SERVICE_UNAVAILABLE)
        .body(Body::from("idempotency persistence unavailable"))
        .unwrap()
}

struct PreparedIdempotencyRequest {
    idempotency_key: String,
    storage_key: String,
    stale_cookie_storage_key: Option<String>,
    key_context: StorageKeyContext,
    body_hash: Vec<u8>,
    session: Option<crate::session::Session>,
    parts: Parts,
    body_bytes: Bytes,
}

struct InFlightLockGuard {
    store: Arc<dyn IdempotencyStore>,
    key: String,
    owner: String,
    unlock_on_drop: bool,
}

impl InFlightLockGuard {
    fn new(store: Arc<dyn IdempotencyStore>, key: String, owner: String) -> Self {
        Self {
            store,
            key,
            owner,
            unlock_on_drop: true,
        }
    }

    fn unlock_now(&mut self) {
        if self.unlock_on_drop {
            self.store.unlock_owned(&self.key, &self.owner);
            self.unlock_on_drop = false;
        }
    }

    const fn keep_locked_until_ttl(&mut self) {
        self.unlock_on_drop = false;
    }
}

impl Drop for InFlightLockGuard {
    fn drop(&mut self) {
        if self.unlock_on_drop {
            self.store.unlock_owned(&self.key, &self.owner);
        }
    }
}

#[derive(Clone)]
pub(crate) struct DeferredIdempotencyCommit {
    inner: Arc<Mutex<Option<DeferredIdempotencyState>>>,
}

struct DeferredIdempotencyState {
    store: Arc<dyn IdempotencyStore>,
    storage_key: String,
    key_context: StorageKeyContext,
    alias_storage_keys: Vec<String>,
    primary_replay_after_guard_denial: bool,
    idempotency_key: String,
    record: IdempotencyRecord,
    body_hash: Vec<u8>,
    ttl: Duration,
    lock_guard: InFlightLockGuard,
}

impl DeferredIdempotencyCommit {
    fn new(state: DeferredIdempotencyState) -> Self {
        Self {
            inner: Arc::new(Mutex::new(Some(state))),
        }
    }

    fn commit_with_final_headers(&self, headers: &HeaderMap) -> Result<(), IdempotencyStoreError> {
        let Some(mut state) = self
            .inner
            .lock()
            .expect("deferred idempotency commit lock poisoned")
            .take()
        else {
            return Ok(());
        };

        state.record.headers = extract_finalized_session_replay_headers(headers);
        let primary_scope = if state.primary_replay_after_guard_denial {
            FINALIZED_SESSION_OLD_SCOPE
        } else {
            FINALIZED_SESSION_CURRENT_SCOPE
        };
        let primary_record = finalized_session_record(state.record.clone(), primary_scope);
        if let Err(error) = state.store.try_set(
            &state.storage_key,
            primary_record,
            state.body_hash.clone(),
            state.ttl,
        ) {
            tracing::error!(
                idempotency.key = %state.idempotency_key,
                error = %error,
                "Deferred idempotency persistence failed after finalized session response; failing closed"
            );
            state.lock_guard.keep_locked_until_ttl();
            return Err(error);
        }
        let alias_record = finalized_session_record(state.record, FINALIZED_SESSION_CURRENT_SCOPE);
        for storage_key in state.alias_storage_keys {
            if let Err(error) = state.store.try_set(
                &storage_key,
                alias_record.clone(),
                state.body_hash.clone(),
                state.ttl,
            ) {
                tracing::error!(
                    idempotency.key = %state.idempotency_key,
                    error = %error,
                    "Deferred idempotency persistence failed after finalized session response; failing closed"
                );
                state.lock_guard.keep_locked_until_ttl();
                return Err(error);
            }
        }
        state.lock_guard.unlock_now();
        Ok(())
    }

    fn add_session_alias(&self, session_id: Option<&str>, primary_replay_after_guard_denial: bool) {
        let mut guard = self
            .inner
            .lock()
            .expect("deferred idempotency commit lock poisoned");
        let Some(state) = guard.as_mut() else {
            return;
        };

        let storage_key = state.key_context.storage_key(session_id);
        if primary_replay_after_guard_denial && storage_key != state.storage_key {
            state.primary_replay_after_guard_denial = true;
        }
        if storage_key != state.storage_key
            && !state
                .alias_storage_keys
                .iter()
                .any(|existing| existing == &storage_key)
        {
            state.alias_storage_keys.push(storage_key);
        }
        drop(guard);
    }

    fn keep_locked_until_ttl(&self) {
        let Some(mut state) = self
            .inner
            .lock()
            .expect("deferred idempotency commit lock poisoned")
            .take()
        else {
            return;
        };
        state.lock_guard.keep_locked_until_ttl();
    }
}

pub(crate) fn finalize_deferred_session_commit(
    response: &mut Response<Body>,
) -> Result<(), IdempotencyStoreError> {
    let Some(commit) = response
        .extensions_mut()
        .remove::<DeferredIdempotencyCommit>()
    else {
        return Ok(());
    };
    commit.commit_with_final_headers(response.headers())
}

pub(crate) fn add_deferred_session_replay_key(
    response: &Response<Body>,
    session_id: Option<&str>,
    primary_replay_after_guard_denial: bool,
) {
    if let Some(commit) = response.extensions().get::<DeferredIdempotencyCommit>() {
        commit.add_session_alias(session_id, primary_replay_after_guard_denial);
    }
}

pub(crate) fn keep_deferred_session_commit_locked(response: &mut Response<Body>) {
    if let Some(commit) = response
        .extensions_mut()
        .remove::<DeferredIdempotencyCommit>()
    {
        commit.keep_locked_until_ttl();
    }
}

fn request_idempotency_key(req: &Request<Body>) -> Option<String> {
    let key = req
        .headers()
        .get(IDEMPOTENCY_KEY_HEADER)?
        .to_str()
        .unwrap_or("");
    (!key.is_empty()).then(|| key.to_owned())
}

fn in_flight_lock_owner() -> String {
    uuid::Uuid::new_v4().to_string()
}

async fn prepare_idempotency_request(
    idempotency_key: String,
    req: Request<Body>,
) -> Result<PreparedIdempotencyRequest, Response<Body>> {
    let (mut parts, body) = req.into_parts();
    let key_context = StorageKeyContext::from_parts(idempotency_key.clone(), &parts);
    let session_id = storage_session_id_for_parts(&parts).await;
    let storage_key = key_context.storage_key(session_id.as_deref());
    let stale_cookie_storage_key = stale_cookie_session_id_for_parts(&parts).and_then(|id| {
        let key = key_context.storage_key(Some(&id));
        (key != storage_key).then_some(key)
    });
    parts.extensions.insert(IdempotencyContext::new(
        idempotency_key.clone(),
        storage_key.clone(),
    ));
    let session = parts.extensions.get::<crate::session::Session>().cloned();
    let content_type = parts
        .headers
        .get(axum::http::header::CONTENT_TYPE)
        .map(axum::http::HeaderValue::as_bytes);
    let body_limit = parts
        .extensions
        .get::<crate::security::config::UploadConfig>()
        .map_or(DEFAULT_REQUEST_BODY_LIMIT, |c| c.max_request_size_bytes);
    let body_bytes = axum::body::to_bytes(body, body_limit)
        .await
        .map_err(|_| request_body_too_large_response())?;
    let body_hash = compute_body_hash(&body_bytes, content_type);

    Ok(PreparedIdempotencyRequest {
        idempotency_key,
        storage_key,
        stale_cookie_storage_key,
        key_context,
        body_hash,
        session,
        parts,
        body_bytes,
    })
}

fn lookup_prepared_entry(
    store: &dyn IdempotencyStore,
    prepared: &PreparedIdempotencyRequest,
) -> Result<Option<IdempotencyEntry>, IdempotencyStoreError> {
    if let Some(key) = prepared.stale_cookie_storage_key.as_deref()
        && let Some(entry) = store.try_get(key)?
    {
        return Ok(Some(entry));
    }

    store.try_get(&prepared.storage_key)
}

fn stale_cookie_fallback_in_flight(
    store: &dyn IdempotencyStore,
    prepared: &PreparedIdempotencyRequest,
    in_flight_ttl: Duration,
) -> bool {
    let Some(key) = prepared.stale_cookie_storage_key.as_deref() else {
        return false;
    };

    let owner = in_flight_lock_owner();
    if store.try_lock_owned(key, &owner, in_flight_ttl) {
        store.unlock_owned(key, &owner);
        false
    } else {
        true
    }
}

fn cacheable_response_record(
    status: u16,
    headers: &HeaderMap,
    body: &Bytes,
    metadata: Vec<(String, Vec<u8>)>,
) -> IdempotencyRecord {
    IdempotencyRecord {
        status,
        headers: extract_replay_headers(headers),
        body: body.to_vec(),
        metadata,
    }
}

async fn handle_idempotent_request<S>(
    mut inner: S,
    config: IdempotencyRequestConfig,
    req: Request<Body>,
) -> Result<Response<Body>, std::convert::Infallible>
where
    S: Service<Request<Body>, Response = Response<Body>, Error = std::convert::Infallible>
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    let IdempotencyRequestConfig {
        store,
        ttl,
        in_flight_ttl,
        replay_through_inner,
        fail_closed_on_replay,
        metrics,
    } = config;

    if !is_mutating_method(req.method()) {
        return inner.call(req).await;
    }

    let Some(idempotency_key) = request_idempotency_key(&req) else {
        return inner.call(req).await;
    };

    let prepared = match prepare_idempotency_request(idempotency_key, req).await {
        Ok(prepared) => prepared,
        Err(response) => return Ok(response),
    };

    // ── Cache hit ──────────────────────────────────────────────────────────
    match lookup_prepared_entry(store.as_ref(), &prepared) {
        Ok(Some(entry)) => {
            return replay_cache_hit(
                &mut inner,
                entry,
                prepared,
                metrics.as_ref(),
                replay_through_inner,
                fail_closed_on_replay,
            )
            .await;
        }
        Ok(None) => {}
        Err(error) => {
            tracing::error!(
                idempotency.key = %prepared.idempotency_key,
                error = %error,
                "Idempotency lookup failed; failing closed"
            );
            return Ok(persistence_failed_response());
        }
    }

    if stale_cookie_fallback_in_flight(store.as_ref(), &prepared, in_flight_ttl) {
        tracing::debug!(
            idempotency.key = %prepared.idempotency_key,
            "Stale session cookie idempotency key already in flight — returning 409"
        );
        metrics
            .as_ref()
            .inspect(|m| m.record_idempotency_conflict());
        return Ok(in_flight_conflict_response());
    }

    // ── In-flight check (concurrent duplicate) ─────────────────────────────
    let lock_owner = in_flight_lock_owner();
    if !store.try_lock_owned(&prepared.storage_key, &lock_owner, in_flight_ttl) {
        tracing::debug!(
            idempotency.key = %prepared.idempotency_key,
            "Idempotency key already in flight — returning 409"
        );
        metrics
            .as_ref()
            .inspect(|m| m.record_idempotency_conflict());
        return Ok(in_flight_conflict_response());
    }
    let mut lock_guard =
        InFlightLockGuard::new(store.clone(), prepared.storage_key.clone(), lock_owner);

    // Double-check after acquiring the lock: a concurrent request may have
    // completed between our miss check and lock acquisition.
    match lookup_prepared_entry(store.as_ref(), &prepared) {
        Ok(Some(entry)) => {
            lock_guard.unlock_now();
            return replay_cache_hit(
                &mut inner,
                entry,
                prepared,
                metrics.as_ref(),
                replay_through_inner,
                fail_closed_on_replay,
            )
            .await;
        }
        Ok(None) => {}
        Err(error) => {
            lock_guard.keep_locked_until_ttl();
            tracing::error!(
                idempotency.key = %prepared.idempotency_key,
                error = %error,
                "Idempotency lookup failed after lock acquisition; failing closed"
            );
            return Ok(persistence_failed_response());
        }
    }

    handle_cache_miss(inner, store, ttl, prepared, metrics.as_ref(), lock_guard).await
}

async fn handle_cache_miss<S>(
    mut inner: S,
    store: Arc<dyn IdempotencyStore>,
    ttl: Duration,
    prepared: PreparedIdempotencyRequest,
    metrics: Option<&crate::middleware::MetricsCollector>,
    mut lock_guard: InFlightLockGuard,
) -> Result<Response<Body>, std::convert::Infallible>
where
    S: Service<Request<Body>, Response = Response<Body>, Error = std::convert::Infallible>
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    let PreparedIdempotencyRequest {
        idempotency_key,
        storage_key,
        key_context,
        body_hash,
        session,
        parts,
        body_bytes,
        ..
    } = prepared;

    tracing::debug!(
        idempotency.key = %idempotency_key,
        "Idempotency cache miss — forwarding to handler"
    );

    let response = inner
        .call(Request::from_parts(parts, Body::from(body_bytes)))
        .await?;
    let (mut resp_parts, resp_body) = response.into_parts();

    // Collect up to the cache cap; stream oversized bodies through without
    // buffering to avoid materialising large responses in memory.
    let resp_bytes = match collect_response_for_cache(resp_body).await {
        CollectedResponseBody::StreamError(passthrough_body) => {
            lock_guard.unlock_now();
            tracing::warn!(
                idempotency.key = %idempotency_key,
                "I/O error reading response body; passing the body error through without storing idempotency entry"
            );
            return Ok(Response::from_parts(resp_parts, passthrough_body));
        }
        CollectedResponseBody::TooLarge {
            passthrough_body, ..
        } => {
            // Body exceeded MAX_CACHEABLE_RESPONSE_BODY — stream through.
            lock_guard.unlock_now();
            tracing::debug!(
                idempotency.key = %idempotency_key,
                limit_bytes = MAX_CACHEABLE_RESPONSE_BODY,
                "Response body exceeded cache limit; streaming through without caching"
            );
            return Ok(Response::from_parts(resp_parts, passthrough_body));
        }
        CollectedResponseBody::Cacheable(bytes) => bytes,
    };

    let replay_metadata = resp_parts
        .extensions
        .remove::<IdempotencyReplayMetadata>()
        .map_or_else(Vec::new, IdempotencyReplayMetadata::into_entries);
    let cache_committed_error = resp_parts
        .extensions
        .remove::<IdempotencyCacheCommittedErrorResponse>()
        .is_some();

    // Cache successful 2xx/3xx responses and explicit "mutation committed"
    // errors; store before unlocking so concurrent duplicates still see a
    // locked key rather than racing to re-execute the handler.
    let status = resp_parts.status.as_u16();
    if (200u32..400).contains(&u32::from(status)) || cache_committed_error {
        let session_mutated = if let Some(session) = &session {
            session.has_pending_changes().await
        } else {
            false
        };
        let record =
            cacheable_response_record(status, &resp_parts.headers, &resp_bytes, replay_metadata);
        if session_mutated {
            tracing::debug!(
                idempotency.key = %idempotency_key,
                "Session changed during idempotent request; deferring cache write until SessionLayer finalizes Set-Cookie"
            );
            resp_parts.extensions.insert(DeferredIdempotencyCommit::new(
                DeferredIdempotencyState {
                    store,
                    storage_key,
                    key_context,
                    alias_storage_keys: Vec::new(),
                    primary_replay_after_guard_denial: false,
                    idempotency_key,
                    record,
                    body_hash,
                    ttl,
                    lock_guard,
                },
            ));
            if let Some(m) = metrics {
                m.record_idempotency_miss();
            }
            return Ok(Response::from_parts(resp_parts, Body::from(resp_bytes)));
        }
        if let Err(error) = store.try_set(&storage_key, record, body_hash, ttl) {
            tracing::error!(
                idempotency.key = %idempotency_key,
                error = %error,
                "Idempotency persistence failed after handler success; failing closed"
            );
            lock_guard.keep_locked_until_ttl();
            return Ok(persistence_failed_response());
        }
    }
    lock_guard.unlock_now();

    if let Some(m) = metrics {
        m.record_idempotency_miss();
    }

    // Reconstruct from original parts — preserves set-cookie and extensions.
    Ok(Response::from_parts(resp_parts, Body::from(resp_bytes)))
}

/// Collect response body bytes up to `MAX_CACHEABLE_RESPONSE_BODY`.
///
/// Returns:
/// - `Cacheable(bytes)` — body is within the limit and fully collected
/// - `TooLarge(body)` — body exceeded the limit; the returned `Body` chains the
///   already-read bytes with the remaining stream for pass-through delivery
/// - `StreamError(body)` — reading the body stream failed; the returned `Body`
///   preserves the already-read bytes and then yields the original stream error
enum CollectedResponseBody {
    Cacheable(Bytes),
    TooLarge {
        passthrough_body: Body,
        #[cfg_attr(not(test), allow(dead_code))]
        buffered_len: usize,
    },
    StreamError(Body),
}

async fn collect_response_for_cache(body: Body) -> CollectedResponseBody {
    collect_response_for_cache_with_limit(body, MAX_CACHEABLE_RESPONSE_BODY).await
}

async fn collect_response_for_cache_with_limit(body: Body, limit: usize) -> CollectedResponseBody {
    let mut buf = Vec::<u8>::new();
    let mut data_stream = body.into_data_stream();
    loop {
        match data_stream.next().await {
            None => break,
            Some(Err(err)) => {
                let leading = Bytes::from(buf);
                let passthrough =
                    Body::from_stream(futures::stream::iter(vec![Ok(leading), Err(err)]));
                return CollectedResponseBody::StreamError(passthrough);
            }
            Some(Ok(chunk)) => {
                if chunk.len() > limit.saturating_sub(buf.len()) {
                    let buffered_len = buf.len();
                    let mut leading_chunks = Vec::with_capacity(2);
                    if !buf.is_empty() {
                        leading_chunks.push(Ok::<Bytes, axum::Error>(Bytes::from(buf)));
                    }
                    leading_chunks.push(Ok::<Bytes, axum::Error>(chunk));
                    let passthrough =
                        Body::from_stream(futures::stream::iter(leading_chunks).chain(data_stream));
                    return CollectedResponseBody::TooLarge {
                        passthrough_body: passthrough,
                        buffered_len,
                    };
                }
                buf.extend_from_slice(&chunk);
            }
        }
    }
    CollectedResponseBody::Cacheable(Bytes::from(buf))
}

async fn replay_cache_hit<S>(
    inner: &mut S,
    entry: IdempotencyEntry,
    prepared: PreparedIdempotencyRequest,
    metrics: Option<&crate::middleware::MetricsCollector>,
    replay_through_inner: bool,
    fail_closed_on_replay: bool,
) -> Result<Response<Body>, std::convert::Infallible>
where
    S: Service<Request<Body>, Response = Response<Body>, Error = std::convert::Infallible>
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    let PreparedIdempotencyRequest {
        idempotency_key,
        body_hash,
        mut parts,
        body_bytes,
        ..
    } = prepared;

    if entry.body_hash != body_hash {
        tracing::debug!(
            idempotency.key = %idempotency_key,
            "Idempotency payload mismatch — returning 422"
        );
        return Ok(Response::builder()
            .status(StatusCode::UNPROCESSABLE_ENTITY)
            .body(Body::from("idempotency key reused with different payload"))
            .unwrap());
    }

    if fail_closed_on_replay {
        tracing::warn!(
            idempotency.key = %idempotency_key,
            "Idempotency cache hit reached a route without an inner replay stop; failing closed"
        );
        return Ok(replay_requires_inner_stop_response());
    }

    tracing::debug!(
        idempotency.key = %idempotency_key,
        idempotency.replayed = true,
        "Idempotency cache hit — replaying stored response"
    );

    if let Some(m) = metrics {
        m.record_idempotency_hit();
    }

    let replay = IdempotencyReplayResponse {
        record: entry.record,
    };
    if replay_through_inner {
        parts.extensions.insert(replay);
        return inner
            .call(Request::from_parts(parts, Body::from(body_bytes)))
            .await;
    }

    Ok(replay.into_response())
}

fn response_from_record(record: IdempotencyRecord) -> Response<Body> {
    let mut builder = Response::builder().status(record.status);
    for (name, value) in &record.headers {
        builder = builder.header(name.as_str(), value.as_slice());
    }
    builder
        .header(X_IDEMPOTENT_REPLAYED, "true")
        .body(Body::from(record.body))
        .unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::http::header::AUTHORIZATION;
    use std::collections::HashMap;
    use std::convert::Infallible;
    use std::sync::Mutex;
    use tower::ServiceExt;

    #[derive(Clone, Default)]
    struct RecordingStore {
        keys: Arc<Mutex<Vec<String>>>,
    }

    impl RecordingStore {
        fn keys(&self) -> Vec<String> {
            self.keys
                .lock()
                .expect("recording store lock poisoned")
                .clone()
        }

        fn record_key(&self, key: &str) {
            self.keys
                .lock()
                .expect("recording store lock poisoned")
                .push(key.to_owned());
        }
    }

    impl IdempotencyStore for RecordingStore {
        fn get(&self, key: &str) -> Option<IdempotencyEntry> {
            self.record_key(key);
            None
        }

        fn set(&self, key: &str, _record: IdempotencyRecord, _body_hash: Vec<u8>, _ttl: Duration) {
            self.record_key(key);
        }

        fn try_lock(&self, key: &str, _lock_ttl: Duration) -> bool {
            self.record_key(key);
            true
        }

        fn unlock(&self, key: &str) {
            self.record_key(key);
        }
    }

    fn idempotent_post(path: &str, key: &str, body: &'static str) -> Request<Body> {
        Request::builder()
            .method(Method::POST)
            .uri(path)
            .header(IDEMPOTENCY_KEY_HEADER, key)
            .body(Body::from(body))
            .expect("request builder should be valid")
    }

    fn session_with_user(session_id: &str, user_id: &str) -> crate::session::Session {
        let mut data = HashMap::new();
        data.insert("user_id".to_owned(), user_id.to_owned());
        crate::session::Session::new_for_test(session_id.to_owned(), data)
    }

    fn hex_lower(bytes: impl AsRef<[u8]>) -> String {
        bytes.as_ref().iter().fold(
            String::with_capacity(bytes.as_ref().len() * 2),
            |mut out, byte| {
                use std::fmt::Write as _;
                let _ = write!(out, "{byte:02x}");
                out
            },
        )
    }

    fn expected_principal_digest(session_id: Option<&str>) -> String {
        use sha2::Digest as _;
        let mut hasher = sha2::Sha256::new();
        hasher.update(b"authorization:");
        hasher.update(b"\nsession:");
        if let Some(session_id) = session_id {
            hasher.update(session_id.as_bytes());
        }
        hex_lower(hasher.finalize())
    }

    fn expected_storage_key(
        method: &str,
        path: &str,
        session_id: Option<&str>,
        idempotency_key: &str,
    ) -> String {
        use sha2::Digest as _;
        let principal = expected_principal_digest(session_id);
        let mut hasher = sha2::Sha256::new();
        push_storage_key_component(&mut hasher, "method", method.as_bytes());
        push_storage_key_component(&mut hasher, "target", path.as_bytes());
        push_storage_key_component(&mut hasher, "scope-header-count", b"0");
        push_storage_key_component(&mut hasher, "principal", principal.as_bytes());
        push_storage_key_component(&mut hasher, "idempotency-key", idempotency_key.as_bytes());
        format!("v2:{}", hex_lower(hasher.finalize()))
    }

    #[test]
    fn idempotency_context_clones_share_mutation_discriminator_sequence() {
        let context = IdempotencyContext::new("client-key".to_owned(), "scoped-key".to_owned());
        let cloned = context.clone();

        assert_eq!(context.next_mutation_discriminator(), "0");
        assert_eq!(cloned.next_mutation_discriminator(), "1");
        assert_eq!(context.next_mutation_discriminator(), "2");
    }

    #[test]
    fn memory_lock_unlock_owned_does_not_release_newer_owner() {
        let store = MemoryIdempotencyStore::new(Duration::from_secs(60));

        assert!(store.try_lock_owned("key", "owner-a", Duration::from_millis(5)));
        std::thread::sleep(Duration::from_millis(20));
        assert!(store.try_lock_owned("key", "owner-b", Duration::from_secs(60)));

        store.unlock_owned("key", "owner-a");
        assert!(
            !store.try_lock_owned("key", "owner-c", Duration::from_secs(60)),
            "stale owners must not release a newer in-flight lock"
        );

        store.unlock_owned("key", "owner-b");
        assert!(store.try_lock_owned("key", "owner-c", Duration::from_secs(60)));
    }

    #[tokio::test]
    async fn response_body_stream_errors_are_not_replaced_with_empty_success() {
        let store = Arc::new(MemoryIdempotencyStore::new(Duration::from_secs(60)));
        let service = IdempotencyLayer::new(store).layer(tower::service_fn(
            |_req: Request<Body>| async move {
                let stream = futures::stream::iter(vec![
                    Ok::<Bytes, std::io::Error>(Bytes::from_static(b"partial")),
                    Err(std::io::Error::other("stream failed")),
                ]);
                Ok::<_, Infallible>(
                    Response::builder()
                        .status(StatusCode::OK)
                        .body(Body::from_stream(stream))
                        .expect("response builder should be valid"),
                )
            },
        ));

        let response = service
            .oneshot(idempotent_post("/stream", "stream-key", "same"))
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::OK);
        let body = axum::body::to_bytes(response.into_body(), usize::MAX).await;
        assert!(
            body.is_err(),
            "idempotency middleware must preserve response body stream errors"
        );
    }

    #[tokio::test]
    async fn collect_response_checks_chunk_size_before_buffering_past_cap() {
        let chunk = Bytes::from(vec![b'x'; 64]);
        match collect_response_for_cache_with_limit(Body::from(chunk), 10).await {
            CollectedResponseBody::TooLarge {
                passthrough_body,
                buffered_len,
            } => {
                assert_eq!(
                    buffered_len, 0,
                    "single over-cap chunk must not be copied into the cache buffer first"
                );
                let delivered = axum::body::to_bytes(passthrough_body, usize::MAX)
                    .await
                    .expect("passthrough body should collect");
                assert_eq!(delivered.len(), 64);
            }
            CollectedResponseBody::Cacheable(_) => {
                panic!("over-cap chunk must not be considered cacheable")
            }
            CollectedResponseBody::StreamError(_) => panic!("body should not stream-error"),
        }
    }

    #[tokio::test]
    async fn cookie_session_extension_scopes_idempotency_storage_key() {
        let store = Arc::new(MemoryIdempotencyStore::new(Duration::from_secs(60)));
        let mut service = IdempotencyLayer::new(store).layer(tower::service_fn(
            |req: Request<Body>| async move {
                let session = req
                    .extensions()
                    .get::<crate::session::Session>()
                    .cloned()
                    .expect("session extension should be present");
                let user_id = session
                    .get("user_id")
                    .await
                    .expect("session should contain user_id");
                Ok::<_, Infallible>(Response::new(Body::from(user_id)))
            },
        ));

        let mut alice_req = idempotent_post("/orders", "shared-key", "same");
        alice_req
            .extensions_mut()
            .insert(session_with_user("session-alice", "alice"));
        let alice_response = service
            .ready()
            .await
            .expect("service should be ready")
            .call(alice_req)
            .await
            .expect("alice request should complete");
        let alice_body = axum::body::to_bytes(alice_response.into_body(), usize::MAX)
            .await
            .expect("alice body should collect");
        assert_eq!(alice_body, Bytes::from_static(b"alice"));

        let mut bob_req = idempotent_post("/orders", "shared-key", "same");
        bob_req
            .extensions_mut()
            .insert(session_with_user("session-bob", "bob"));
        let bob_response = service
            .ready()
            .await
            .expect("service should be ready")
            .call(bob_req)
            .await
            .expect("bob request should complete");
        assert!(
            bob_response.headers().get(X_IDEMPOTENT_REPLAYED).is_none(),
            "a different cookie-backed session must not replay another user's response"
        );
        let bob_body = axum::body::to_bytes(bob_response.into_body(), usize::MAX)
            .await
            .expect("bob body should collect");
        assert_eq!(bob_body, Bytes::from_static(b"bob"));
    }

    #[tokio::test]
    async fn storage_key_hashes_length_delimited_components() {
        let observed_store = RecordingStore::default();
        let service = IdempotencyLayer::new(Arc::new(observed_store.clone())).layer(
            tower::service_fn(|_req: Request<Body>| async {
                Ok::<_, Infallible>(Response::new(Body::from("ok")))
            }),
        );
        let request = Request::builder()
            .method(Method::POST)
            .uri("/payments")
            .header(IDEMPOTENCY_KEY_HEADER, "pay-once")
            .header(AUTHORIZATION, "Bearer stable-token")
            .body(Body::from("same"))
            .expect("request builder should be valid");

        let response = service
            .oneshot(request)
            .await
            .expect("request should complete");
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("response body should collect");
        assert_eq!(body, Bytes::from_static(b"ok"));

        let keys = observed_store.keys();
        let storage_key = keys.first().expect("storage key should be recorded");
        assert_eq!(
            storage_key,
            &expected_storage_key("POST", "/payments", None, "pay-once")
        );
        assert!(!storage_key.contains("/payments"));
        assert!(!storage_key.contains("pay-once"));
    }

    #[tokio::test]
    async fn forwarded_request_carries_scoped_idempotency_context() {
        let observed = Arc::new(Mutex::new(None::<(String, String)>));
        let observed_context = observed.clone();
        let service = IdempotencyLayer::new(Arc::new(MemoryIdempotencyStore::new(
            Duration::from_secs(60),
        )))
        .layer(tower::service_fn(move |req: Request<Body>| {
            let observed_context = observed_context.clone();
            async move {
                let context = req
                    .extensions()
                    .get::<IdempotencyContext>()
                    .cloned()
                    .expect("idempotency context should be available to inner handlers");
                *observed_context
                    .lock()
                    .expect("observed context lock poisoned") =
                    Some((context.key().to_owned(), context.scoped_key().to_owned()));
                Ok::<_, Infallible>(Response::new(Body::from("ok")))
            }
        }));
        let request = Request::builder()
            .method(Method::POST)
            .uri("/payments")
            .header(IDEMPOTENCY_KEY_HEADER, "pay-once")
            .header(AUTHORIZATION, "Bearer stable-token")
            .body(Body::from("same"))
            .expect("request builder should be valid");

        let response = service
            .oneshot(request)
            .await
            .expect("request should complete");
        assert_eq!(response.status(), StatusCode::OK);

        let observed = observed
            .lock()
            .expect("observed context lock poisoned")
            .clone()
            .expect("inner handler should record idempotency context");
        assert_eq!(observed.0, "pay-once");
        assert_eq!(
            observed.1,
            expected_storage_key("POST", "/payments", None, "pay-once")
        );
    }
}