autumn-web 0.9.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
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
//! Deterministic simulation testing (sim-testing, issue #1797).
//!
//! This module is the public 0.7.0 developer-experience surface for writing
//! **deterministic** simulation tests: a single [`#[sim_test]`](crate::sim_test)
//! attribute gives you a seeded [`Sim`] handle and a paused runtime, so a test
//! runs identically on every machine and every run, and a failure prints a
//! copy-pasteable line that reproduces it exactly.
//!
//! # Quick start
//!
//! ```rust,ignore
//! use autumn_web::sim::Sim;
//! use autumn_web::sim_test;
//!
//! #[sim_test]
//! async fn deterministic(mut sim: Sim) {
//!     // The seed comes from `AUTUMN_SIM_SEED` (hex `0x..` or decimal,
//!     // default 0). Everything derived from `sim` is seed-driven and
//!     // reproducible.
//!     assert_eq!(sim.seed, 0);
//!     let _rng = sim.rng();
//! }
//! ```
//!
//! Reproduce a failing run by copying the replay line printed on panic, e.g.:
//!
//! ```text
//! AUTUMN_SIM_SEED=0x9f3a cargo test -p my-crate deterministic
//! ```
//!
//! # What a `Sim` gives you
//!
//! - **Virtual time.** [`Sim::build`] mounts a [`crate::test::TestApp`] with a
//!   virtual clock. [`Sim::advance`] and [`Sim::advance_to`] step it together
//!   with tokio's paused timer, so `#[job]` backoff and `#[scheduled]` ticks fire
//!   in virtual time. [`Sim::run_to_idle`] drains jobs, due ticks and durable
//!   commit hooks.
//! - **Seeded identity.** [`Sim::rng`] ([`SimRng`]) draws deterministic values.
//!   [`Sim::build`] seeds the app's [`crate::entropy::Entropy`] from the seed,
//!   so framework-minted ids (jobs, request ids, idempotency keys, sessions)
//!   replay too. [`Sim::seeded_entropy`] returns the same source.
//! - **Faults.** [`Chaos`] ([`Sim::chaos`]) injects seed-sampled faults.
//!   [`FaultPlan`] (#1680), attached with
//!   [`crate::test::TestApp::with_fault_plan`], injects authored ones and
//!   records a serializable [`FaultOutcome`]. [`Sim::kill`] and
//!   [`Sim::restart`] model a process crash; `sim::llm` is a seeded LLM stub.
//! - **Assertions and sweeps.** [`always!`](crate::always) and
//!   [`sometimes!`](crate::sometimes) ([`mod@assert`]). Behind the
//!   `sim-testing` feature, `sim::op` generates workloads (`Sim::gen_ops`,
//!   `Sim::run_proptest` with shrinking) and `sim::sweep` runs one scenario
//!   across many seeds (`sweep_proptest`, driven in CI by the `sim-sweep` bin).
//! - **Deadlocks.** With `AUTUMN_SIM_LIVENESS_BUDGET_SECS` set, a `#[sim_test]`
//!   whose tasks all park panics with its replay line instead of hanging. See
//!   [`__with_liveness_budget`] for the limits.
//! - **Phase 2 (issue #2967).** `Sim::net` routes outbound HTTP through a
//!   seeded `SimNet`. [`Sim::interleave`] and [`Sim::spawn`] reorder ready work
//!   from the seed. [`crash_at`] drops an operation at any await.
//!   [`Sim::try_run_to_idle`] reports a drain that does not settle. Framework
//!   code with no clock in scope reads [`crate::time::ambient_now`] and its
//!   siblings, which follow the sim clock.
//! - **Replicas (issue #3067).** [`Sim::mount_replica`] mounts several apps on
//!   one clock, each with its own [`Replica`] clock (offset, drift, steps).
//!   `Sim::db_link` (with `sqlite`) faults one replica's database session
//!   and writes. [`Sim::run_for`] moves time one event at a time. [`runtime`] gates
//!   database work, so a seed replays it. [`mod@trace`] compares two runs of
//!   one seed.
//!
//! The clock and app handles inside a `Sim` are crate-private:
//!
//! ```compile_fail
//! use autumn_web::sim::SimClock;
//! ```
//!
//! ```compile_fail
//! use autumn_web::sim::SimApp;
//! ```
//!
//! Everything here is designed to grow additively (builder-style) without
//! breaking the frozen surface — hence the `#[non_exhaustive]` markers.

// Several thin accessors here could be `const fn`; they stay non-const so the
// frozen public surface can grow a non-const body later without a break.
#![allow(clippy::missing_const_for_fn)]

use std::sync::Arc;

use chrono::{DateTime, LocalResult, NaiveDateTime, Offset, TimeZone, Utc};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use uuid::Uuid;

use crate::entropy::{Entropy, SeededEntropy, uuid_v4_from_bytes, uuid_v7_from_parts};
use crate::time::TickingClock;

// The per-sim SQLite DB lane (W4, issue #1797): a fresh, migrated, in-process
// SQLite substrate the sim builds its app on. Self-contained and additive — W2
// consumes its pool when it mounts the app on `SimApp`. Only meaningful under the
// `sqlite` feature (the sim's DB substrate is SQLite by design).
//
// Exposed as `#[doc(hidden)] pub` — unstable test/sim plumbing, not the stable
// surface — mirroring this module's `__seed_from_env` / `__replay_line` hidden
// hooks. It is `pub` (not `pub(crate)`) purely so the W4 DoD consolidated
// integration test, which is an external crate, can drive the end-to-end drain;
// the stable mount API remains W2's `Sim::build`.
#[cfg(feature = "sqlite")]
#[doc(hidden)]
pub mod substrate;

// The chaos lane (W5, issue #1797): deterministic fault injection wired into
// `Sim::build`. Additive and opt-in — a default (empty) `Chaos` installs
// nothing. See the module docs for the determinism contract.
pub mod chaos;

#[cfg(feature = "mail")]
pub use chaos::MailFault;
pub use chaos::{Chaos, ChaosEvent, ChaosHook};

// The authored fault lane (issue #1680): `FaultPlan`, an ordinal-targeted,
// seed-deterministic fault schedule installed through `TestApp::with_fault_plan`
// (not through `Sim::chaos`), plus the serializable `FaultOutcome` a scenario
// asserts on. Additive and opt-in — a `TestApp` with no plan is untouched. See
// the module docs for the determinism contract and how it differs from `Chaos`.
pub mod fault;

pub use fault::{
    FaultEffect, FaultLedger, FaultOutcome, FaultPlan, FinalState, FiredFault, PlannedFault,
    ReportedError,
};

// The seeded LLM stub (W5.b, item 6, issue #1797): a deterministic fake
// completion client — canned responses + a seeded fault/latency schedule — for
// exercising agent retry/fallback paths under the virtual clock. Standalone and
// additive; it does not route through the `Chaos` builder. See the module docs
// for the determinism contract.
pub mod llm;

pub use llm::{LlmCall, LlmClient, LlmError, LlmRequest, LlmResponse, SeededLlm, SeededLlmBuilder};

// The crash lane (W5.c item 7, issue #1797): a seed-derived crash schedule plus
// the `Sim` kill/restart primitive for durable crash-recovery tests. Additive —
// the schedule is a pure function of the seed and installs nothing at build.
pub mod crash;

// The W6 semantic core (issue #1797): the `always!` / `sometimes!` assertion
// macros and the thread-local non-vacuity registry. Public (documented) module —
// the macros are `#[macro_export]`ed at the crate root (`autumn_web::always` /
// `autumn_web::sometimes`), and their hidden plumbing plus the sweep-facing
// registry API live here.
pub mod assert;

// The simulated network (issue #2967): seeded latency, drops and partitions
// for outbound `http_client::Client` calls, served by in-process hosts.
#[cfg(feature = "http-client")]
pub mod net;

#[cfg(feature = "http-client")]
pub use net::{NetEvent, NetFault, SimNet};

// The shared demo scenario (issue #2967): one `Op` vocabulary for the
// `sim-sweep` proptest sweep and the `sim_ops` cargo-fuzz target. Hidden,
// unstable harness plumbing.
#[doc(hidden)]
pub mod scenario;

// The interleaving shuffler (issue #2967): seeded poll order for
// `Sim::interleave` and seeded yields for `Sim::spawn`.
mod shuffle;

// Several apps on one sim clock (issue #3067).
pub mod replica;

pub use replica::{Replica, ReplicaClock};

// Faults on one replica's link to the shared SQLite database (issue #3067).
#[cfg(feature = "sqlite")]
pub mod dblink;

#[cfg(feature = "sqlite")]
pub use dblink::{DbFaultEvent, DbFaultKind, DbLink};

// The sim runtime and its blocking-work gate (issue #3067).
pub(crate) mod gate;

pub use gate::{SimRuntime, runtime};

// Same-seed trace capture and diff (issue #3067).
pub mod trace;

pub use trace::{Trace, TraceDiff};

// Framework scenarios (jobs, scheduler, lock) for the seed sweep (issue #3067).
#[cfg(feature = "sqlite")]
#[doc(hidden)]
pub mod fleet;

pub use assert::{
    SometimesRegistry, assert_all_sometimes_satisfied, reset_sometimes_registry,
    sometimes_snapshot, sometimes_unsatisfied,
};
pub use crash::{CrashOutcome, CrashPoint, CrashSchedule, crash_at};

// The W6 op-driver (PR2, issue #1797): `Sim::gen_ops`/`Sim::gen_ops_with` (deterministic,
// non-shrinking generation) and `Sim::run_proptest` (the shrink-capable
// runner-owning entrypoint). Behind the `sim-testing` feature because it needs
// `proptest` as a library (not just dev) dependency — see `autumn/Cargo.toml`.
#[cfg(feature = "sim-testing")]
pub mod op;

// The W6 seed-sweep runner (PR3, issue #1797): `sweep_proptest` runs
// `Sim::run_proptest` sequentially across a batch of seeds, reporting the
// first failing seed (if any), and folds `sometimes!` reachability — across
// every proptest case in every seed — across the whole swept range so a
// green sweep is provably non-vacuous. Sequential, not parallel: see
// `sim::sweep`'s module docs for why a `body` that mounts a real app makes
// OS-thread parallelism unsafe here. The `sim-sweep` `[[bin]]`
// (`autumn/src/bin/sim_sweep.rs`) is its CI-facing driver. Same
// `sim-testing` feature gate as `op` — it builds directly on
// `Sim::run_proptest_with_case_hook`.
#[cfg(feature = "sim-testing")]
pub mod sweep;

#[cfg(feature = "sim-testing")]
pub use sweep::{SweepFailure, SweepOutcome, sweep_proptest};

/// The fixed, deterministic epoch the simulation clock starts at:
/// `2020-01-01T00:00:00Z`.
///
/// Every sim run starts its virtual clock here so wall-clock-derived values are
/// reproducible across machines and runs. W2 drives this clock forward via
/// `SimClock`.
const SIM_EPOCH_UNIX_SECS: i64 = 1_577_836_800; // 2020-01-01T00:00:00Z

/// A deterministic simulation handle, constructed from a single `u64` seed.
///
/// `Sim` is the day-one **public, stability-frozen** entry point handed to a
/// [`#[sim_test]`](crate::sim_test) body. Its [`seed`](Sim::seed) is public so a
/// test can assert on or thread it; the injection handles it owns
/// ([`SimRng`], the clock, [`Chaos`] and the mounted app) are private and reached
/// through accessors, so their internals can evolve wave-over-wave without a
/// breaking change.
///
/// Marked `#[non_exhaustive]` so future waves can add handles without breaking
/// construction — always build one via [`Sim::from_seed`].
#[non_exhaustive]
pub struct Sim {
    /// The seed this simulation was constructed from.
    ///
    /// Reproduce a run by exporting `AUTUMN_SIM_SEED=0x<seed>` (the replay line
    /// printed on panic does this for you).
    pub seed: u64,

    /// Seeded deterministic RNG, reached through [`Sim::rng`].
    rng: SimRng,

    /// Virtual clock, started at the fixed sim epoch
    /// (`2020-01-01T00:00:00Z`). Stepped by [`Sim::advance`] in lockstep with
    /// tokio's paused timer.
    clock: SimClock,

    /// Fault-injection configuration installed at [`Sim::build`]. A default
    /// (empty) [`Chaos`] is inactive and installs nothing.
    chaos: Chaos,

    /// Shared chaos runtime state (decision stream + event log), populated by
    /// [`Sim::build`] when [`chaos`](Self::chaos) is active. Read through
    /// [`Sim::__chaos_events`].
    chaos_state: Option<Arc<chaos::ChaosState>>,

    /// Built [`crate::test::TestClient`] handle, mounted by [`Sim::build`] on
    /// the paused runtime with the virtual clock installed.
    app: SimApp,

    /// Real wall-clock budget for the `strict_wall_clock` real-time leak guard,
    /// or `None` (the default) when the guard is off. Set once via
    /// [`strict_wall_clock`](Sim::strict_wall_clock) /
    /// [`strict_wall_clock_budget`](Sim::strict_wall_clock_budget) *before* the
    /// run and only **read** by [`advance`](Sim::advance) /
    /// [`run_to_idle`](Sim::run_to_idle), so it is a plain `Option` with no
    /// interior mutability — that keeps `Sim: Sync` and the `&self`
    /// advance/drain futures `Send` (a `Cell`/`RefCell` field would break both).
    strict_budget: Option<std::time::Duration>,

    /// The simulated network installed at each mount, if any.
    #[cfg(feature = "http-client")]
    net: Option<net::SimNet>,

    /// How many shuffler calls ([`interleave`](Sim::interleave),
    /// [`spawn`](Sim::spawn)) this sim made. Each call draws its own seeded
    /// stream from the seed and this count. Atomic so `&self` stays `Sync`.
    shuffle_calls: std::sync::atomic::AtomicU64,

    /// The clock installed as this thread's ambient clock (issue #2967). See
    /// [`crate::time::ambient_now`].
    ambient: Arc<AmbientSimClock>,

    /// Keeps [`ambient`](Self::ambient) installed while the sim lives.
    _ambient_guard: crate::time::AmbientGuard,

    /// Keeps this sim on its thread's sim stack, which splits tokio time
    /// between the sims that share a runtime.
    stack_guard: SimStackGuard,

    /// How many times [`mount`](Sim::mount) has run. The first mount seeds the
    /// app's entropy from [`seed`](Sim::seed); each restart derives a new seed
    /// from it, so a restarted process does not replay the crashed one's ids.
    mounts: u64,

    /// The apps from [`mount_replica`](Sim::mount_replica), in mount order
    /// (issue #3067).
    replicas: Vec<ReplicaSlot>,

    /// The database link of each replica, made on first use.
    #[cfg(feature = "sqlite")]
    db_links: std::sync::Mutex<std::collections::BTreeMap<String, DbLink>>,

    /// This sim's place in its sim runtime's live count (issue #3067).
    gate_seat: gate::SimSeat,
}

/// The name a replica has as a network host. Two replica names with one key
/// would share one `SimNet` host, so they cannot both be mounted.
fn replica_host_key(name: &str) -> String {
    #[cfg(feature = "http-client")]
    {
        net::canonical_host(name)
    }
    #[cfg(not(feature = "http-client"))]
    {
        name.to_ascii_lowercase()
    }
}

/// One named replica: its spec, its clock, and its app while it is alive.
struct ReplicaSlot {
    name: String,
    clock: replica::NodeClock,
    client: Option<crate::test::TestClient>,
    /// The network this replica is a host of. [`Sim::net`] can set a new
    /// network later, so the kill and the drop clean this one.
    #[cfg(feature = "http-client")]
    net: Option<net::SimNet>,
}

impl Sim {
    /// Construct a simulation from `seed`.
    ///
    /// Infallible and cheap: it seeds the RNG and starts the virtual clock but
    /// does **not** boot a database or an app, so an empty
    /// [`#[sim_test]`](crate::sim_test) runs with zero setup. Mount an app with
    /// [`build`](Sim::build).
    ///
    /// Built inside its paused runtime, the sim's elapsed time starts here.
    /// Built before that runtime, call [`anchor`](Sim::anchor) first thing in
    /// it; `#[sim_test]` does this for you.
    #[must_use]
    pub fn from_seed(seed: u64) -> Self {
        // Each seed run starts with a clean reachability registry, so the sweep
        // can attribute observed/satisfied `sometimes!` labels to exactly one
        // seed before folding them into its cross-seed aggregate (W6, #1797).
        assert::reset_sometimes_registry();
        let clock = SimClock::new(TickingClock::starting_at(sim_epoch()));
        let ambient_clock = Arc::new(AmbientSimClock {
            wall: std::sync::RwLock::new(Arc::new(clock.ticking())),
            own_advanced: std::sync::Mutex::new(std::time::Duration::ZERO),
            auto_advanced: std::sync::Mutex::new(std::time::Duration::ZERO),
            alive: std::sync::atomic::AtomicBool::new(true),
        });
        let gate_seat = gate::SimSeat::default();
        gate_seat.take();
        let ambient_guard = crate::time::install_ambient(ambient_clock.clone());
        let sim_stack = SimStackGuard::enter(Arc::clone(&ambient_clock));
        Self {
            seed,
            rng: SimRng::new(seed),
            clock,
            ambient: ambient_clock,
            _ambient_guard: ambient_guard,
            stack_guard: sim_stack,
            chaos: Chaos::default(),
            chaos_state: None,
            app: SimApp::default(),
            strict_budget: None,
            #[cfg(feature = "http-client")]
            net: None,
            shuffle_calls: std::sync::atomic::AtomicU64::new(0),
            mounts: 0,
            replicas: Vec::new(),
            #[cfg(feature = "sqlite")]
            db_links: std::sync::Mutex::default(),
            gate_seat,
        }
    }

    /// Start this sim's elapsed time at tokio's current instant, if it has not
    /// started yet. Call it first thing in the runtime that drives a sim built
    /// before that runtime: tokio time that passes before the sim is anchored
    /// is not in its elapsed time. Calling it again changes nothing, and
    /// outside a runtime it does nothing.
    ///
    /// A sim built inside its runtime, or run by
    /// [`#[sim_test]`](crate::sim_test), is anchored already.
    pub fn anchor(&self) {
        if tokio::runtime::Handle::try_current().is_ok() {
            lock_sim_stack(&self.stack_guard.home, SimStack::settle);
            // A sim built before `sim::runtime()` joins its gate here.
            self.gate_seat.take();
        }
    }

    /// Configure deterministic fault injection for this simulation.
    ///
    /// The `chaos` builder's hooks (transient DB checkout errors, job duplicate
    /// delivery, clock skew) are installed at [`build`](Sim::build) time, each
    /// fault decision drawn from a dedicated seed-derived stream so the same
    /// seed and configuration replay the same fault schedule. A default
    /// [`Chaos`] is inactive and changes nothing.
    ///
    /// ```rust,ignore
    /// use autumn_web::sim::Chaos;
    /// sim.chaos(Chaos::default().db_transient_errors(0.1).job_duplicate_delivery(0.2));
    /// let client = sim.build(TestApp::new().routes(routes![touch]).jobs(jobs![work]));
    /// ```
    pub fn chaos(&mut self, chaos: Chaos) -> &mut Self {
        self.chaos = chaos;
        self
    }

    /// Route the app's outbound HTTP through a simulated network (issue
    /// #2967).
    ///
    /// [`build`](Sim::build) installs `net` in the app, so the
    /// [`crate::http_client::Client`] extractor sends through it: seeded
    /// latency and drops, partitions, and in-process hosts. Keep a clone to
    /// partition hosts and read [`SimNet::events`] later. See [`mod@net`].
    #[cfg(feature = "http-client")]
    pub fn net(&mut self, net: SimNet) -> &mut Self {
        self.net = Some(net);
        self
    }

    /// The seed this simulation was constructed from.
    ///
    /// Provided for API symmetry alongside the public [`seed`](Sim::seed)
    /// field.
    #[must_use]
    pub const fn seed(&self) -> u64 {
        self.seed
    }

    /// Borrow the seeded deterministic RNG handle.
    ///
    /// Draw deterministic values and UUIDs through it — e.g.
    /// [`SimRng::uuid_v4`] / [`SimRng::next_u64`]. The same seed always yields
    /// the same draw sequence.
    #[must_use]
    pub fn rng(&mut self) -> &mut SimRng {
        &mut self.rng
    }

    /// Build a shared, seeded [`Entropy`] source for this simulation's seed,
    /// ready to inject into a mounted app via
    /// [`crate::state::AppState::with_entropy`].
    ///
    /// [`build`](Self::build) already installs this source in the app it
    /// mounts, unless the test passed its own with
    /// [`crate::test::TestApp::with_entropy`]. Use this to seed state you build
    /// yourself.
    ///
    /// This is the bridge W3 provides for W2's app mounting: the app the
    /// simulation drives resolves the [`crate::entropy::Rng`] extractor and
    /// every framework-minted identifier (job ids, request ids, idempotency
    /// lock owners, session ids) from this source, so a fixed seed replays the
    /// whole identifier stream byte-for-byte.
    ///
    /// The returned source is seeded independently from the [`rng`](Self::rng)
    /// handle (both from [`seed`](Self::seed)), so drawing from one does not
    /// perturb the other's sequence.
    #[must_use]
    pub fn seeded_entropy(&self) -> Arc<dyn Entropy> {
        SeededEntropy::shared(self.seed)
    }

    /// Mount `app` on the paused runtime with the simulation's virtual clock
    /// installed, and return the resulting [`crate::test::TestClient`].
    ///
    /// The simulation's virtual clock is threaded in via
    /// [`crate::test::TestApp::with_clock`], so every handler that reads a
    /// [`crate::time::Clock`] extractor sees the virtual instant — starting at
    /// the fixed sim epoch (`2020-01-01T00:00:00Z`) and moving only when
    /// [`Sim::advance`] steps it. The built app also starts the in-process job
    /// runtime (the in-memory backend), so [`run_to_idle`](Sim::run_to_idle)
    /// can drain enqueued jobs deterministically, and starts its `#[scheduled]`
    /// tasks, whose ticks fire as virtual time crosses their deadlines.
    ///
    /// Unless `app` already has an entropy source from
    /// [`crate::test::TestApp::with_entropy`], `build` installs one seeded from
    /// [`seed`](Sim::seed), so framework-minted ids replay from the seed. A
    /// later mount through [`restart`](Sim::restart) derives a new seed from it,
    /// so a restarted process does not repeat the crashed one's ids.
    ///
    /// Configure `app` fully before handing it over: routes, jobs, tasks and a
    /// sim database are attached to the [`crate::test::TestApp`] before this
    /// call. Do **not** call [`crate::test::TestApp::with_clock`] yourself;
    /// `build` owns the clock so time stays in lockstep.
    ///
    /// The returned borrow is convenient for an immediate request; to interleave
    /// requests with [`advance`](Sim::advance) / [`run_to_idle`](Sim::run_to_idle)
    /// calls, reach the client through [`client`](Sim::client) instead.
    ///
    /// ```rust,ignore
    /// let client = sim.build(TestApp::new().routes(routes![hello]).jobs(jobs![work]));
    /// client.get("/hello").send().await.assert_ok();
    /// ```
    pub fn build(&mut self, app: crate::test::TestApp) -> &crate::test::TestClient {
        self.mount(app)
    }

    /// Mount `app` on the paused runtime with the simulation's virtual clock (and
    /// active chaos hooks) installed, replacing any previously-mounted client.
    ///
    /// Shared by [`build`](Self::build) and [`restart`](Self::restart) so the
    /// initial mount and a post-crash restart go through byte-for-byte the same
    /// path. When chaos is active this re-derives the chaos decision state from
    /// the seed, so a restart's fault schedule replays deterministically.
    fn mount(&mut self, app: crate::test::TestApp) -> &crate::test::TestClient {
        // Seed the app's entropy unless the test injected its own source, so
        // framework-minted ids replay from the seed with no extra call.
        let mount_seed = mount_entropy_seed(self.seed, self.mounts);
        let app = app.with_default_entropy(SeededEntropy::shared(mount_seed));
        self.mounts += 1;
        // Install the simulated network, with a stream fresh for this mount.
        #[cfg(feature = "http-client")]
        let app = match &self.net {
            Some(net) => {
                net.reseed(mount_seed);
                app.with_sim_net(net.clone())
            }
            None => app,
        };
        // When chaos is active, install its deterministic hooks (which also own
        // the clock so a skew wrapper can be applied); otherwise the build is
        // byte-for-byte the pre-W5 path — just the virtual clock.
        // The ambient wall clock matches the app clock, skewed or not.
        *self
            .ambient
            .wall
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner) =
            chaos::wall_clock(&self.chaos, self.seed, self.clock.ticking());
        let app = if self.chaos.is_active() {
            let state = chaos::ChaosState::new(self.seed, &self.chaos);
            self.chaos_state = Some(Arc::clone(&state));
            chaos::install(app, &self.chaos, self.seed, self.clock.ticking(), state)
        } else {
            app.with_clock(self.clock.ticking())
        };
        let client = app.build();
        self.app.client = Some(client);
        self.app.client()
    }

    /// Simulate a process crash: stop the app's job and scheduler tasks and
    /// drop the mounted app, so in-flight work is **cancelled without
    /// completing** (the client's [`Drop`] also cancels the runtime's shutdown
    /// token and clears the global job client), ready for durable recovery on
    /// [`restart`](Self::restart).
    ///
    /// This is the kill half of the W5.c crash-recovery primitive (item 7). It
    /// deliberately drops **only** the app/runtime, never the durable database:
    /// the caller holds the sim's DB substrate (e.g. an
    /// `SqliteSubstrate`) and its `pool()`, so every committed row — crucially
    /// the durable `autumn_repository_commit_hooks` queue — survives the crash
    /// and is still there when a fresh app is mounted on the same pool.
    ///
    /// A crash after [`build`](Self::build) has not run is a no-op.
    ///
    /// # Durability boundary (stated plainly)
    ///
    /// Under the `sqlite` sim substrate the app runs the **in-memory `local`
    /// job backend**, which is **not durable** — a kill drops its mid-flight and
    /// still-queued jobs by design, exactly as a real process crash would drop an
    /// in-memory queue. Item 7's durable guarantee is therefore asserted against
    /// the DB-backed repository commit-hook queue, **not** the local job queue;
    /// the in-memory job queue's by-design loss is documented, never pretended
    /// durable. See the [`crash`] module docs.
    pub fn kill(&mut self) {
        // Stop the app's tasks, as a process death does. Dropping the client
        // then runs `TestJobRuntime::drop` (shutdown.cancel() +
        // clear_global_job_client()).
        if let Some(client) = self.app.client.take() {
            abort_app_tasks(&client);
            drop(client);
        }
        // A fresh process has no in-memory chaos decision log; a restart
        // re-derives it deterministically from the seed.
        self.chaos_state = None;
    }

    /// Restart after a [`kill`](Self::kill): mount a fresh `app` on the paused
    /// runtime, modelling a process restart on the **same durable database**.
    ///
    /// The caller rebuilds the `TestApp` against the *same* substrate pool
    /// (`TestApp::new()…with_db(substrate.pool())`), so the restarted app sees
    /// every row the crashed process committed. Following the restart with
    /// [`run_to_idle`](Self::run_to_idle) drains the durable repository
    /// commit-hook queue, recovering and running any hook the crash left
    /// un-drained (at-least-once / idempotent). Registering the app's hook
    /// runners on the fresh app models a real app re-registering them on boot.
    pub fn restart(&mut self, app: crate::test::TestApp) -> &crate::test::TestClient {
        self.mount(app)
    }

    /// Kill the running app and immediately [`restart`](Self::restart) it on a
    /// fresh `app` — the kill-then-restart convenience over
    /// [`kill`](Self::kill) + [`restart`](Self::restart).
    ///
    /// The `app` must be rebuilt against the same durable substrate pool so the
    /// restarted process recovers the crashed one's committed rows.
    pub fn crash_and_restart(&mut self, app: crate::test::TestApp) -> &crate::test::TestClient {
        self.kill();
        self.restart(app)
    }

    /// Mount `app` as the replica `replica`, next to the other replicas
    /// (issue #3067).
    ///
    /// Each replica is its own app on the sim's one clock: its own state, job
    /// runtime and scheduled tasks. It reads its own [`Replica`] clock. Give
    /// each replica its own pool on one database, so they share data as a
    /// fleet does:
    ///
    /// ```rust,ignore
    /// let substrate = SqliteSubstrate::new()?;
    /// for name in ["a", "b"] {
    ///     let pool = substrate.replica_pool(&sim.db_link(name))?;
    ///     sim.mount_replica(name, TestApp::new().with_db(pool).jobs(jobs![work]));
    /// }
    /// sim.run_for(Duration::from_secs(30)).await;
    /// ```
    ///
    /// With [`net`](Sim::net) set, the replica is also a [`SimNet`] host under
    /// its name, so `http://b/` reaches the replica `b`.
    ///
    /// Process-wide state is not per replica: the cache, the event bus and the
    /// global job client belong to the replica mounted last. Enqueue through a
    /// replica's own `JobClient` extension instead of `job::enqueue`.
    ///
    /// # Panics
    ///
    /// Panics if a replica with this name is already mounted. A name that
    /// is the same host name, such as one that differs only in case, is the
    /// same name. Use
    /// [`restart_replica`](Sim::restart_replica) to mount it again.
    pub fn mount_replica(
        &mut self,
        replica: impl Into<Replica>,
        app: crate::test::TestApp,
    ) -> &crate::test::TestClient {
        let replica = replica.into();
        let key = replica_host_key(replica.name());
        if let Some(slot) = self
            .replicas
            .iter()
            .find(|slot| replica_host_key(&slot.name) == key)
        {
            panic!(
                "replica `{}` is already mounted as `{}`; use `restart_replica` to mount it again",
                replica.name(),
                slot.name
            );
        }
        let clock = replica::NodeClock::new(
            Arc::clone(&self.ambient) as Arc<dyn crate::time::ClockSource>,
            sim_epoch(),
            replica.resolve(self.seed),
        );
        self.replicas.push(ReplicaSlot {
            name: replica.name().to_owned(),
            clock,
            client: None,
            #[cfg(feature = "http-client")]
            net: None,
        });
        let index = self.replicas.len() - 1;
        self.mount_replica_at(index, app)
    }

    /// Kill the replica `name` if it is alive, then mount `app` in its place.
    /// The replica keeps its clock, as a restarted process keeps its host's
    /// clock. Rebuild `app` on the same database to recover its rows.
    ///
    /// # Panics
    ///
    /// Panics if no replica with this name was mounted.
    pub fn restart_replica(
        &mut self,
        name: &str,
        app: crate::test::TestApp,
    ) -> &crate::test::TestClient {
        self.kill_replica(name);
        let index = self.expect_replica_index(name);
        self.mount_replica_at(index, app)
    }

    /// Kill the replica `name`: stop its tasks and drop its app, as a process
    /// crash does. A job handler that was running stops at its next await and
    /// does not settle its claim. The database keeps every committed row. A
    /// kill of a dead replica changes nothing.
    ///
    /// # Panics
    ///
    /// Panics if no replica with this name was mounted.
    pub fn kill_replica(&mut self, name: &str) {
        let index = self.expect_replica_index(name);
        let Some(client) = self.replicas[index].client.take() else {
            return;
        };
        #[cfg(feature = "http-client")]
        if let Some(net) = self.replicas[index].net.take() {
            net.remove_host(name);
        }
        abort_app_tasks(&client);
        drop(client);
    }

    /// The client of the live replica `name`.
    ///
    /// # Panics
    ///
    /// Panics if no live replica has this name.
    #[must_use]
    pub fn replica(&self, name: &str) -> &crate::test::TestClient {
        self.try_replica(name)
            .unwrap_or_else(|| panic!("no live replica `{name}`: mount it with `mount_replica`"))
    }

    /// The client of the live replica `name`, or `None`.
    #[must_use]
    pub fn try_replica(&self, name: &str) -> Option<&crate::test::TestClient> {
        self.replica_index(name)
            .and_then(|index| self.replicas[index].client.as_ref())
    }

    /// The names of the live replicas, in mount order.
    #[must_use]
    pub fn replica_names(&self) -> Vec<&str> {
        self.replicas
            .iter()
            .filter(|slot| slot.client.is_some())
            .map(|slot| slot.name.as_str())
            .collect()
    }

    /// The clock settings of the replica `name` (alive or killed), or `None`.
    #[must_use]
    pub fn replica_clock(&self, name: &str) -> Option<ReplicaClock> {
        self.replica_index(name)
            .map(|index| self.replicas[index].clock.settings())
    }

    /// Jump the wall clock of the replica `name` by `by`, as an NTP step does.
    /// `by` can be negative: a step can go back. The monotonic clock does not
    /// move. Timers the replica already set do not move.
    ///
    /// # Panics
    ///
    /// Panics if no replica with this name was mounted.
    pub fn step_replica_clock(&self, name: &str, by: chrono::TimeDelta) {
        let index = self.expect_replica_index(name);
        self.replicas[index].clock.step(by);
    }

    /// Let `duration` of virtual time pass, one event at a time (issue #3067).
    ///
    /// The sim sleeps. Tokio moves its paused clock to the next timer only
    /// when all tasks wait and no blocking work is queued, such as a `SQLite`
    /// query. So database work ends before time moves on, and a seed replays
    /// the same order. With replicas on `SQLite`, use it, not
    /// [`advance`](Sim::advance).
    ///
    /// Replica clocks move with each step. The clock of the app from
    /// [`build`](Sim::build) moves once, at the end.
    pub async fn run_for(&self, duration: std::time::Duration) {
        let guard_start = self.wall_clock_guard_start();
        let start = tokio::time::Instant::now();
        tokio::time::sleep(duration).await;
        let moved = tokio::time::Instant::now().saturating_duration_since(start);
        // Tokio time already moved, so this is not an explicit advance: the
        // ambient clock counts it as auto-advanced time.
        self.clock.advance(moved);
        self.enforce_wall_clock_budget(guard_start);
    }

    /// The database link of the replica `name`, made on first use and seeded
    /// from the sim seed and the name. Pass it to
    /// [`SqliteSubstrate::replica_pool`](substrate::SqliteSubstrate::replica_pool),
    /// then fault the replica's database through it. See [`DbLink`].
    ///
    /// The link must exist before the mount, so this does not check the name.
    /// A wrong name gives a link that no replica uses.
    #[cfg(feature = "sqlite")]
    #[must_use]
    pub fn db_link(&self, name: &str) -> DbLink {
        self.db_links
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .entry(name.to_owned())
            .or_insert_with(|| {
                DbLink::new(name, derive_seed(self.seed, &format!("sim-db-link:{name}")))
            })
            .clone()
    }

    /// The app from [`build`](Sim::build), then each live replica.
    fn live_clients(&self) -> impl Iterator<Item = &crate::test::TestClient> {
        self.app
            .try_client()
            .into_iter()
            .chain(self.replicas.iter().filter_map(|slot| slot.client.as_ref()))
    }

    fn replica_index(&self, name: &str) -> Option<usize> {
        self.replicas.iter().position(|slot| slot.name == name)
    }

    fn expect_replica_index(&self, name: &str) -> usize {
        self.replica_index(name)
            .unwrap_or_else(|| panic!("no replica `{name}` was mounted"))
    }

    /// Build `app` for the replica slot `index` and store its client.
    fn mount_replica_at(
        &mut self,
        index: usize,
        app: crate::test::TestApp,
    ) -> &crate::test::TestClient {
        let mount_seed = mount_entropy_seed(self.seed, self.mounts);
        let first_mount = self.mounts == 0;
        let app = app.with_default_entropy(SeededEntropy::shared(mount_seed));
        self.mounts += 1;
        // One network for every replica: only the first mount seeds it.
        #[cfg(feature = "http-client")]
        let app = match &self.net {
            Some(net) => {
                if first_mount {
                    net.reseed(mount_seed);
                }
                app.with_sim_net(net.clone())
            }
            None => app,
        };
        #[cfg(not(feature = "http-client"))]
        let _ = first_mount;
        let slot = &mut self.replicas[index];
        #[cfg(feature = "http-client")]
        let app = if self.net.is_some() {
            app.trust_host(&slot.name)
        } else {
            app
        };
        let client = app.with_clock(slot.clock.clone()).build();
        #[cfg(feature = "http-client")]
        if let Some(net) = &self.net {
            net.insert_host(&slot.name, client.router());
            slot.net = Some(net.clone());
        }
        slot.client.insert(client)
    }

    /// Run `ops` concurrently and poll them in a seeded order (issue #2967).
    ///
    /// Each round polls the pending ops in an order drawn from the seed, and
    /// can hold one back for a round. So ops that share state (a lock, a row, a
    /// counter) meet in a different order per seed, and the same seed replays
    /// the same order. Outputs keep the input order.
    ///
    /// ```rust,ignore
    /// let client = sim.client();
    /// let ops = vec![client.post("/a").send(), client.post("/b").send()];
    /// let responses = sim.interleave(ops).await;
    /// ```
    pub async fn interleave<F: std::future::Future>(&self, ops: Vec<F>) -> Vec<F::Output> {
        shuffle::Interleave::new(ops, self.next_shuffle_stream()).await
    }

    /// Spawn `op` on the sim runtime with seeded yields (issue #2967).
    ///
    /// Before a poll, the task can yield from the seeded stream, which moves
    /// it behind the other ready tasks. Tasks spawned this way therefore run
    /// in a seed-driven order. Tasks the framework spawns keep tokio's order.
    pub fn spawn<F>(&self, op: F) -> tokio::task::JoinHandle<F::Output>
    where
        F: std::future::Future + Send + 'static,
        F::Output: Send + 'static,
    {
        tokio::spawn(shuffle::Shuffled::new(op, self.next_shuffle_stream()))
    }

    /// The seeded stream for the next shuffler call.
    fn next_shuffle_stream(&self) -> Arc<dyn Entropy> {
        let call = self
            .shuffle_calls
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        shuffle::stream(self.seed, call)
    }

    /// The seed-derived [`CrashSchedule`] for this simulation.
    ///
    /// A pure function of the [`seed`](Self::seed): two same-seed sims return an
    /// equal schedule. Pass a point's [`CrashPoint::await_index`] to
    /// [`crash_at`] to crash there.
    #[must_use]
    pub fn crash_schedule(&self) -> CrashSchedule {
        CrashSchedule::derive(self.seed, crash::DEFAULT_CRASH_SCHEDULE_LEN)
    }

    /// The first entry of the seed-derived
    /// [`crash_schedule`](Self::crash_schedule).
    ///
    /// `None` only if the schedule is empty (it never is under the default
    /// length). Deterministic for a given seed.
    #[must_use]
    pub fn crash_point(&self) -> Option<CrashPoint> {
        self.crash_schedule().first().cloned()
    }

    /// The recorded chaos fault schedule for this simulation.
    ///
    /// Returns one [`ChaosEvent`] per chaos-hook invocation, in the order the
    /// hooks fired — the reproducible *fault schedule* the run produced. Empty
    /// when chaos was inactive or [`build`](Sim::build) has not run.
    ///
    /// Unstable sim plumbing (hidden from the stable surface, like the module's
    /// other `__`-prefixed hooks); the W5 Definition-of-Done test asserts two
    /// same-seed runs return equal schedules.
    #[doc(hidden)]
    #[must_use]
    pub fn __chaos_events(&self) -> Vec<ChaosEvent> {
        self.chaos_state
            .as_ref()
            .map(|state| state.events())
            .unwrap_or_default()
    }

    /// Borrow the [`crate::test::TestClient`] mounted by [`build`](Sim::build).
    ///
    /// # Panics
    ///
    /// Panics if [`build`](Sim::build) has not been called yet.
    #[must_use]
    pub fn client(&self) -> &crate::test::TestClient {
        self.app.client()
    }

    /// Borrow the mounted [`crate::test::TestClient`], or `None` before
    /// [`build`](Sim::build).
    #[must_use]
    pub const fn try_client(&self) -> Option<&crate::test::TestClient> {
        self.app.try_client()
    }

    /// Enable the **real-time leak guard** (`strict_wall_clock`) with the
    /// default budget, panicking if a paused-sim step burns more than that much
    /// *real* wall-clock time.
    ///
    /// # What this guards (and what it deliberately does not)
    ///
    /// A paused sim runs on tokio's virtual timer: a
    /// [`tokio::time::sleep`](https://docs.rs/tokio) / job backoff / delayed
    /// enqueue consumes **zero** real time and only advances when
    /// [`advance`](Sim::advance) steps the clock. The one thing that breaks that
    /// invariant is code that escapes the virtual timer and blocks the real
    /// thread — a `std::thread::sleep`, a `spawn_blocking`, or a blocking
    /// syscall. This guard catches the **observable consequence** of that: real
    /// wall-clock time leaking into a step that should have taken virtually none.
    ///
    /// It is **not** off-seam-read detection. It cannot tell you that some code
    /// read `Utc::now()` / `Instant::now()` directly instead of through the
    /// injected clock — a free-function `now()` call has no runtime interception
    /// point in safe Rust, so its *absence* is not observable at runtime. Finding
    /// those reads is a static-analysis (deny-lint) job; this guard is the
    /// complementary runtime backstop for the worst pattern (a real blocking
    /// sleep). Enabling it does not slow a legitimate virtual advance: jumping a
    /// day of virtual time still costs microseconds of real time, well under
    /// budget.
    ///
    /// # Budget & the `AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS` override
    ///
    /// The default budget is deliberately generous (2000 ms) so ordinary CI
    /// scheduling jitter never trips it — the target is a *real* sleep (seconds),
    /// not sub-millisecond noise. Set the environment variable
    /// `AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS` (whole milliseconds) to override
    /// the budget globally for a run; a blank or unparseable value is ignored and
    /// the default (or the value passed to
    /// [`strict_wall_clock_budget`](Sim::strict_wall_clock_budget)) applies. This
    /// mirrors the `AUTUMN_SIM_SEED` idiom, so a too-tight budget on a slow
    /// runner can be loosened without editing test code.
    ///
    /// The guard is read-only during the run: enable it (chainably) *before* the
    /// first [`advance`](Sim::advance) / [`run_to_idle`](Sim::run_to_idle).
    ///
    /// ```rust,ignore
    /// let mut sim = Sim::from_seed(0);
    /// sim.strict_wall_clock();
    /// sim.build(TestApp::new());
    /// sim.advance(std::time::Duration::from_secs(24 * 3600)).await; // virtual, cheap
    /// sim.run_to_idle().await;
    /// ```
    pub fn strict_wall_clock(&mut self) -> &mut Self {
        self.strict_budget = Some(strict_budget_from_env_or(DEFAULT_STRICT_WALL_CLOCK_BUDGET));
        self
    }

    /// Enable the real-time leak guard with an explicit `budget`.
    ///
    /// Identical to [`strict_wall_clock`](Sim::strict_wall_clock) but starts from
    /// `budget` instead of the 100 ms default. The
    /// `AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS` environment variable still
    /// overrides `budget` when it holds a valid whole-millisecond value (so CI
    /// can loosen the guard globally); a blank/unparseable value leaves `budget`
    /// in effect. See [`strict_wall_clock`](Sim::strict_wall_clock) for what the
    /// guard does and does not catch.
    ///
    /// ```rust,ignore
    /// let mut sim = Sim::from_seed(0);
    /// sim.strict_wall_clock_budget(std::time::Duration::from_millis(5));
    /// ```
    pub fn strict_wall_clock_budget(&mut self, budget: std::time::Duration) -> &mut Self {
        self.strict_budget = Some(strict_budget_from_env_or(budget));
        self
    }

    /// Sample a real [`std::time::Instant`] at the start of a guarded step, or
    /// `None` when the leak guard is off. Held across the step's `.await`s (an
    /// `Instant` is `Send + Sync`, so the guarded `&self` futures stay `Send`).
    fn wall_clock_guard_start(&self) -> Option<std::time::Instant> {
        self.strict_budget.map(|_| std::time::Instant::now())
    }

    /// Enforce the real-time leak guard at the end of a step: panic if more than
    /// the configured budget of *real* wall-clock time elapsed since `start`.
    ///
    /// A no-op when the guard is off (`start` / `strict_budget` are `None`). The
    /// panic flows up through the [`#[sim_test]`](crate::sim_test) macro's
    /// `catch_unwind`, so the `AUTUMN_SIM_SEED=…` replay line still prints.
    ///
    /// # Panics
    ///
    /// Panics when the leak guard is enabled and real elapsed time exceeds the
    /// budget (see [`strict_wall_clock`](Sim::strict_wall_clock)).
    fn enforce_wall_clock_budget(&self, start: Option<std::time::Instant>) {
        if let (Some(budget), Some(start)) = (self.strict_budget, start) {
            let elapsed = start.elapsed();
            assert!(
                elapsed <= budget,
                "Sim::strict_wall_clock real-time leak guard tripped: {elapsed:?} of real \
                 wall-clock time elapsed inside a paused-sim step, exceeding the {budget:?} \
                 budget. This means real time leaked into the virtual timeline — usually a real \
                 `std::thread::sleep`, blocking I/O, or `spawn_blocking` that escaped tokio's \
                 paused timer. If this is CI scheduling jitter rather than a genuine leak, raise \
                 the budget via the AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS environment variable."
            );
        }
    }

    /// Advance virtual time by `duration`, stepping the injected wall clock and
    /// tokio's paused timer wheel **together**.
    ///
    /// The framework [`crate::time::Clock`] extractor (backed by the
    /// simulation's virtual clock) and tokio's virtual timer (`tokio::time::sleep`,
    /// job backoff delays, delayed enqueues) move by exactly the same amount, so
    /// `Utc::now()`-via-extractor and a sleeping task stay in lockstep — a job
    /// whose retry backs off 24 hours fires the instant this advances 24 hours,
    /// with zero wall-clock delay. Timers that come due within the window fire
    /// and their tasks are polled before this returns.
    ///
    /// Pair with [`run_to_idle`](Sim::run_to_idle) to then drain the work the
    /// fired timers enqueued.
    pub async fn advance(&self, duration: std::time::Duration) {
        // Real-time leak guard (no-op unless `strict_wall_clock` is enabled):
        // sample a REAL instant (not tokio's paused time) at entry and check the
        // real elapsed against the budget before returning. `advance_to` routes
        // through here, so it inherits the guard for free.
        let guard_start = self.wall_clock_guard_start();
        // `advance` yields and never parks, so database work runs without the
        // gate's turns, as before the gate (#3067).
        let _open = gate::Open::new();
        // Let every ready task take one step at the current instant before time
        // moves. A task spawned since the last yield (a `#[scheduled]` loop that
        // `build` started, a job worker) then registers its first timer at the
        // instant it was started, not at the end of this advance.
        tokio::task::yield_now().await;
        // Step the framework clock first so any task woken by the tokio timer
        // that reads the clock observes the already-advanced instant.
        self.clock.advance(duration);
        // Advance tokio's paused timer wheel; this fires due timers and yields
        // so their tasks are polled before returning.
        // `tokio::time::advance` moves the clock in its first poll, the same
        // poll as this note, and only then yields.
        note_own_advance(&self.ambient, duration);
        tokio::time::advance(duration).await;
        self.enforce_wall_clock_budget(guard_start);
    }

    /// Advance virtual time **to** a specific zoned instant, resolving the
    /// timezone (including any DST transition) to the correct UTC instant and
    /// then stepping forward by the delta from the current sim instant.
    ///
    /// This is the timezone/DST-aware companion to [`advance`](Sim::advance):
    /// where `advance` takes a raw [`std::time::Duration`], `advance_to` takes a
    /// *wall-clock target in any timezone* and computes the real (UTC) delta for
    /// you. It is generic over any [`chrono::TimeZone`] — pass a
    /// `chrono::DateTime<Utc>`, a `chrono::DateTime<chrono::FixedOffset>`, or a
    /// `chrono::DateTime<chrono_tz::Tz>` from the `chrono-tz` crate — so a caller
    /// can express a zoned target without this crate hard-depending on any
    /// particular timezone database.
    ///
    /// The target is converted to UTC via [`chrono::DateTime::with_timezone`],
    /// which is unambiguous (every zoned `DateTime` already names a single
    /// instant), and the sim then reuses [`advance`](Sim::advance) internally so
    /// the injected wall clock and tokio's paused timer wheel stay in **exact
    /// lockstep** — any `tokio::time::sleep` / job-backoff timer whose deadline
    /// falls inside the crossed wall-clock window (a DST "spring-forward" gap
    /// included) fires during the advance, then its task is polled before this
    /// returns. Because the injected clock is UTC and monotonic, a spring-forward
    /// boundary is just a shorter real interval — the timers inside it still fire
    /// correctly.
    ///
    /// # Forward-only semantics
    ///
    /// Virtual time never moves backward:
    ///
    /// - **Target equals the current sim instant** → this is a **no-op** (no
    ///   clock/timer step at all).
    /// - **Target is strictly before the current sim instant** → this
    ///   **panics** with a clear message. Silently doing nothing would hide a
    ///   test bug (a target computed to be in the past almost always means the
    ///   test's arithmetic is wrong), so the panic is deliberate.
    ///
    /// Pair with [`run_to_idle`](Sim::run_to_idle) afterward to drain the work
    /// the fired timers enqueued.
    ///
    /// # Panics
    ///
    /// Panics if `target` resolves to a UTC instant strictly before the current
    /// sim instant (see *Forward-only semantics*).
    ///
    /// ```rust,ignore
    /// use chrono::{TimeZone, Utc};
    /// // Advance the sim clock to a specific UTC instant.
    /// let target = Utc.with_ymd_and_hms(2020, 3, 8, 12, 0, 0).unwrap();
    /// sim.advance_to(&target).await;
    /// sim.run_to_idle().await;
    /// ```
    ///
    /// Written as a non-`async fn` returning a future so the generic zoned
    /// `target` is resolved to UTC **synchronously** and never captured across an
    /// `.await` — the returned future holds only `&self` and the resolved
    /// `DateTime<Utc>`, so it stays `Send` for any `Tz` (a borrowed or non-`Sync`
    /// `Tz` would otherwise poison the future).
    pub fn advance_to<Tz>(
        &self,
        target: &DateTime<Tz>,
    ) -> impl std::future::Future<Output = ()> + '_
    where
        Tz: TimeZone,
    {
        let target_utc = target.with_timezone(&Utc);
        self.advance_to_utc(target_utc)
    }

    /// Advance virtual time to a **naive local wall-clock time** interpreted in
    /// timezone `tz`, resolving DST edge cases explicitly (no `.unwrap()` on a
    /// [`chrono::LocalResult`]).
    ///
    /// Convenience wrapper over [`advance_to`](Sim::advance_to) for the common
    /// "advance to 2:30 AM local on this date" shape, where the naive local time
    /// must be mapped to a single UTC instant. `tz` is any
    /// [`chrono::TimeZone`] (e.g. a `chrono_tz::Tz`); the forward-only /
    /// panic-on-past semantics of [`advance_to`](Sim::advance_to) apply once the
    /// instant is resolved.
    ///
    /// # DST resolution (deterministic)
    ///
    /// A naive local time need not correspond to exactly one UTC instant:
    ///
    /// - **Unambiguous** ([`LocalResult::Single`])
    ///   → that instant.
    /// - **Fall-back / ambiguous** ([`LocalResult::Ambiguous`],
    ///   the wall time occurs twice as the clock rolls back) → the **earlier**
    ///   of the two UTC instants.
    /// - **Spring-forward gap** ([`LocalResult::None`],
    ///   the wall time never occurs because the clock jumps forward) → the
    ///   nonexistent wall time is carried **forward across** the gap to a single
    ///   deterministic post-transition instant (it is resolved by looking up the
    ///   zone's offset at the matching UTC-clock reading and applying it, which
    ///   shifts the requested time past the boundary rather than erroring). For
    ///   example a request for the nonexistent `02:30` on a US spring-forward
    ///   day resolves to `03:30` local (the same instant, one gap-length later).
    ///
    /// # Panics
    ///
    /// Panics if the resolved instant is strictly before the current sim instant
    /// (see [`advance_to`](Sim::advance_to)).
    ///
    /// ```rust,ignore
    /// use chrono::NaiveDate;
    /// let local = NaiveDate::from_ymd_opt(2020, 3, 8).unwrap()
    ///     .and_hms_opt(3, 30, 0).unwrap();
    /// sim.advance_to_local(local, &chrono_tz::America::New_York).await;
    /// ```
    ///
    /// Like [`advance_to`](Sim::advance_to), this is a non-`async fn` returning a
    /// future: the `&tz` reference is used only while resolving the instant
    /// synchronously and is **not** captured by the returned future, so the
    /// future stays `Send` even though `Tz` need not be `Sync`.
    pub fn advance_to_local<Tz>(
        &self,
        local: NaiveDateTime,
        tz: &Tz,
    ) -> impl std::future::Future<Output = ()> + '_
    where
        Tz: TimeZone,
    {
        self.advance_to_utc(resolve_local_to_utc(local, tz))
    }

    /// Shared UTC-target advance used by [`advance_to`](Sim::advance_to) and
    /// [`advance_to_local`](Sim::advance_to_local): plan the step against the
    /// current sim instant, then reuse [`advance`](Sim::advance) so the clock and
    /// timer wheel stay in lockstep.
    async fn advance_to_utc(&self, target: DateTime<Utc>) {
        match plan_advance_to(self.clock.now(), target) {
            AdvancePlan::NoOp => {}
            AdvancePlan::Advance(delta) => self.advance(delta).await,
        }
    }

    /// Drain all ready work — enqueued jobs the in-process runtime can run now,
    /// plus tasks woken by timers that have already come due — until the runtime
    /// is quiescent.
    ///
    /// The sim runtime is a single-threaded, current-thread runtime with the
    /// clock paused, so background tasks (the job worker consuming its queue, a
    /// retry timer that just fired, a delayed enqueue delivering) make progress
    /// only when the running task yields. This cooperatively yields for
    /// `MAX_DRAIN_STEPS` rounds.
    ///
    /// It does **not** fast-forward to a *future* timer — advancing the clock to
    /// reach a not-yet-due backoff/sleep is [`advance`](Sim::advance)'s job
    /// (tokio exposes no next-deadline hook, and a blind auto-advance would
    /// break the clock lockstep). The idiom is therefore
    /// [`advance`](Sim::advance) to the next interesting instant, then
    /// `run_to_idle` to settle the work it released.
    ///
    /// # Panics
    ///
    /// Panics with the seed when the drain does not settle: work still ran in
    /// its last rounds (issue #2967). A job that enqueues itself again is the
    /// usual cause. Use [`try_run_to_idle`](Sim::try_run_to_idle) to get the
    /// [`SimStall`] instead.
    pub async fn run_to_idle(&self) {
        if let Err(stall) = self.try_run_to_idle().await {
            panic!("{stall}");
        }
    }

    /// Like [`run_to_idle`](Sim::run_to_idle), but returns the stall.
    ///
    /// # Errors
    ///
    /// Returns [`SimStall`] when the drain never sees 64 quiet rounds in a row
    /// within 2048 rounds. A round is quiet when no job starts, no scheduled
    /// tick fires, no commit hook or outbox message drains and the task count
    /// does not change.
    /// A single long-running job, or a task that loops without spawning,
    /// does not count as work.
    pub async fn try_run_to_idle(&self) -> Result<(), SimStall> {
        // Real-time leak guard (no-op unless `strict_wall_clock` is enabled):
        // sample a REAL instant at entry and enforce the budget before
        // returning, catching a real blocking sleep in a drained task.
        let guard_start = self.wall_clock_guard_start();
        // The drain yields and never parks, so database work runs without the
        // gate's turns, as before the gate (#3067).
        let _open = gate::Open::new();

        // Resolve the mounted app's DB pool once (a cheap, cloned Arc-backed
        // handle). Durable repository commit hooks are rows, so there is
        // nothing to drain when the app was built without a database (e.g. the
        // in-memory job DoD path) — the lane is skipped entirely then.
        #[cfg(feature = "db")]
        let commit_hook_pools: Vec<_> = self
            .live_clients()
            .filter_map(|client| crate::db::DbState::pool(client.state()).cloned())
            .collect();

        // Run the full `MAX_DRAIN_STEPS`. Then stop at the first
        // `STALL_WINDOW` quiet rounds in a row; work that ends late still
        // settles. Work that never goes quiet is a stall.
        let mut quiet_rounds = 0;
        let mut rounds = 0;
        while rounds < MAX_DRAIN_STEPS
            || (quiet_rounds < STALL_WINDOW && rounds < MAX_DRAIN_STEPS * 2)
        {
            rounds += 1;
            let before = drain_fingerprint();
            // One yield lets each currently-ready spawned task take a step; a
            // zero-duration timer advance flushes any timers registered for the
            // current instant and yields again, so a chain of ready timer/task
            // wakeups settles without advancing the clock. This quiesces the
            // in-process job runtime (TestJobRuntime / JobAdminMemoryBackend)
            // and any tasks woken by already-due scheduler ticks.
            tokio::task::yield_now().await;
            tokio::time::advance(std::time::Duration::ZERO).await;

            // Third ready-work source: durable repository commit hooks. Drain
            // the ready set through the public test-harness wrapper
            // (`crate::test::drain_ready_repository_commit_hooks`), which runs
            // the same claim → run → ack wiring the background commit-hook
            // worker uses, but deterministically and worker-free. A hook may
            // itself enqueue a job, so draining inside the settle loop lets a
            // subsequent iteration pick that job up.
            #[cfg(feature = "db")]
            for pool in &commit_hook_pools {
                let hooks_drained =
                    crate::test::drain_ready_repository_commit_hooks(pool, MAX_DRAIN_STEPS).await;
                if hooks_drained > 0 {
                    note_drain_progress();
                }
            }

            // Fourth source: the transactional outbox (issue #3062). The same
            // claim → handle → mark path the relay worker runs, worker-free.
            #[cfg(feature = "db")]
            for state in self
                .live_clients()
                .map(crate::test::TestClient::state)
                .filter(|state| state.extension::<crate::outbox::OutboxRelay>().is_some())
            {
                match crate::outbox::drain(state, MAX_DRAIN_STEPS).await {
                    Ok(0) => {}
                    Ok(_) => note_drain_progress(),
                    Err(error) => tracing::warn!(%error, "sim outbox drain failed"),
                }
            }

            if drain_fingerprint() == before {
                quiet_rounds += 1;
            } else {
                quiet_rounds = 0;
            }
        }

        self.enforce_wall_clock_budget(guard_start);
        if quiet_rounds < STALL_WINDOW {
            return Err(SimStall {
                seed: self.seed,
                steps: rounds,
            });
        }
        Ok(())
    }
}

impl Drop for Sim {
    fn drop(&mut self) {
        // A replica is a host of the sim network, and its app holds the
        // network: a cycle. Remove the hosts, so the replica apps drop.
        #[cfg(feature = "http-client")]
        for slot in &mut self.replicas {
            if let Some(net) = slot.net.take() {
                net.remove_host(&slot.name);
            }
        }
        // The runtime drops the app tasks after the sim. A task with a query
        // in flight then waits for it, so let gated queries run (#3067).
        self.gate_seat.leave();
    }
}

/// A [`Sim::run_to_idle`] drain that did not settle (issue #2967).
///
/// Work still ran in the last rounds of the drain, so the app was not idle
/// when the drain gave up.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimStall {
    /// The seed of the stalled run.
    pub seed: u64,
    /// The drain rounds that ran.
    pub steps: usize,
}

impl std::fmt::Display for SimStall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "sim drain stall: work still ran after {steps} drain rounds (seed=0x{seed:x}). \
             A job, hook or task keeps making new work, so the app is never idle. \
             Replay with AUTUMN_SIM_SEED=0x{seed:x}.",
            steps = self.steps,
            seed = self.seed,
        )
    }
}

impl std::error::Error for SimStall {}

thread_local! {
    /// Work the drain can see on this thread: jobs run, ticks fired, hooks
    /// drained. [`Sim::try_run_to_idle`] compares it across a round.
    static DRAIN_PROGRESS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// Record one unit of drain-visible work on this thread. The job runner, the
/// task scheduler and the commit-hook drain call it.
pub(crate) fn note_drain_progress() {
    DRAIN_PROGRESS.with(|progress| progress.set(progress.get().wrapping_add(1)));
}

/// This thread's drain progress plus the runtime's live task count. A change
/// across a drain round means work ran in it.
fn drain_fingerprint() -> (u64, usize) {
    let tasks = tokio::runtime::Handle::try_current()
        .map_or(0, |handle| handle.metrics().num_alive_tasks());
    (DRAIN_PROGRESS.with(std::cell::Cell::get), tasks)
}

/// The fixed sim epoch as a `DateTime`.
fn sim_epoch() -> DateTime<Utc> {
    Utc.timestamp_opt(SIM_EPOCH_UNIX_SECS, 0)
        .single()
        .unwrap_or_else(|| Utc.timestamp_nanos(0))
}

/// A `u64` derived from `seed` and `purpose`.
#[cfg(feature = "sqlite")]
fn derive_seed(seed: u64, purpose: &str) -> u64 {
    SeededEntropy::new(seed)
        .derive_uuid(purpose)
        .as_u64_pair()
        .0
}

/// The background tasks one app spawned (issue #3067).
///
/// `TestApp::build` puts one in the app state. The job and scheduler loops
/// register here, so a sim kill can stop them as a process death does. A
/// production app has none, and its tasks run as before.
#[derive(Default)]
pub(crate) struct AppTasks {
    handles: std::sync::Mutex<Vec<tokio::task::AbortHandle>>,
}

impl AppTasks {
    fn track(&self, handle: tokio::task::AbortHandle) {
        let mut handles = self
            .handles
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        handles.retain(|handle| !handle.is_finished());
        handles.push(handle);
    }

    fn abort_all(&self) {
        let handles = std::mem::take(
            &mut *self
                .handles
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner),
        );
        for handle in handles {
            handle.abort();
        }
    }
}

/// Spawn an app background task. In a test app, a sim kill stops it.
pub(crate) fn spawn_app_task<F>(
    state: &crate::state::AppState,
    future: F,
) -> tokio::task::JoinHandle<F::Output>
where
    F: std::future::Future + Send + 'static,
    F::Output: Send + 'static,
{
    let handle = tokio::spawn(future);
    if let Some(tasks) = state.extension::<AppTasks>() {
        tasks.track(handle.abort_handle());
    }
    handle
}

/// Stop every background task of the app behind `client`.
fn abort_app_tasks(client: &crate::test::TestClient) {
    if let Some(tasks) = client.state().extension::<AppTasks>() {
        tasks.abort_all();
    }
}

/// The entropy seed for the `mount`-th app a simulation mounts.
///
/// Mount 0 uses `seed` itself, so the default equals
/// `with_entropy(SeededEntropy::new(sim.seed))`. A later mount (a restart after
/// [`Sim::kill`]) derives its seed from `seed` and the mount number: a real
/// restarted process draws new ids, and this keeps them seed-driven.
fn mount_entropy_seed(seed: u64, mount: u64) -> u64 {
    if mount == 0 {
        return seed;
    }
    let derived = SeededEntropy::new(seed).derive_uuid(format!("sim-mount-{mount}"));
    let bytes: [u8; 8] = derived.as_bytes()[..8]
        .try_into()
        .expect("a uuid has 16 bytes");
    u64::from_le_bytes(bytes)
}

/// The longest timer tokio accepts (about 2.2 years). A larger liveness
/// budget is clamped to it.
const MAX_LIVENESS_BUDGET: std::time::Duration = std::time::Duration::from_millis(68_719_476_734);

/// Resolve the liveness budget from the raw `AUTUMN_SIM_LIVENESS_BUDGET_SECS`
/// value. A positive whole number of seconds arms the watchdog. Unset, blank,
/// `0` or unparseable leaves it off.
fn parse_liveness_budget(raw: Option<&str>) -> Option<std::time::Duration> {
    let secs = raw.map(str::trim)?.parse::<u64>().ok()?;
    if secs == 0 {
        return None;
    }
    Some(std::time::Duration::from_secs(secs).min(MAX_LIVENESS_BUDGET))
}

/// Run a `#[sim_test]` body under the liveness watchdog, when
/// `AUTUMN_SIM_LIVENESS_BUDGET_SECS` arms it.
///
/// Hidden macro plumbing, like [`__seed_from_env`]. See
/// [`__with_liveness_budget`] for what the watchdog detects.
#[doc(hidden)]
pub async fn __with_liveness_watchdog<F: std::future::Future>(seed: u64, body: F) -> F::Output {
    // `#[sim_test]` builds the sim before its runtime. Start its elapsed
    // time as the runtime starts, before the body runs.
    anchor_current_sim();
    let raw = std::env::var("AUTUMN_SIM_LIVENESS_BUDGET_SECS").ok();
    __with_liveness_budget(seed, parse_liveness_budget(raw.as_deref()), body).await
}

/// Run `body`, and panic with a message that names `seed` when it is still
/// running after `budget` of virtual time. `None` runs `body` with no watchdog.
///
/// A deadlock parks every task with no timer that could wake one, so without a
/// watchdog the test hangs. The watchdog is itself a timer, so the paused
/// runtime advances straight to it and the test fails at once; the
/// `#[sim_test]` macro then prints the replay line.
///
/// Two limits. A busy loop that never parks keeps the runtime from advancing,
/// so it is not detected. And the paused runtime also advances while a task
/// waits on something outside it (a lock another thread holds, real I/O), so
/// arm the watchdog only where sim tests run one at a time
/// (`--test-threads=1`) and do no real I/O.
#[doc(hidden)]
pub async fn __with_liveness_budget<F: std::future::Future>(
    seed: u64,
    budget: Option<std::time::Duration>,
    body: F,
) -> F::Output {
    let Some(budget) = budget else {
        return body.await;
    };
    tokio::time::timeout(budget, body)
        .await
        .unwrap_or_else(|_elapsed| {
            panic!(
                "sim liveness: the test body did not finish within {budget:?} of virtual time \
                 (seed=0x{seed:x}). Every task was parked with no timer to wake one, which is \
                 a deadlock, or the body waited past the budget. Raise \
                 AUTUMN_SIM_LIVENESS_BUDGET_SECS for a legitimately long run."
            )
        })
}

/// Upper bound on cooperative yield rounds [`Sim::run_to_idle`] performs before
/// returning, so a misbehaving always-ready task can never hang the drain.
/// Generous relative to the handful of hops a job takes from the queue through
/// its handler to completion under the single-threaded paused runtime.
const MAX_DRAIN_STEPS: usize = 1024;

/// Quiet rounds in a row that settle a drain after `MAX_DRAIN_STEPS`. A drain
/// that does not reach them by twice `MAX_DRAIN_STEPS` is a [`SimStall`].
const STALL_WINDOW: usize = 64;

/// Default real wall-clock budget for the `strict_wall_clock` leak guard
/// ([`Sim::strict_wall_clock`]).
///
/// Deliberately generous (2000 ms): the guard exists to catch a *real* blocking
/// sleep escaping the virtual timer (seconds of wall time), so the budget must
/// sit far above ordinary current-thread scheduling jitter to avoid false
/// positives on a slow/contended CI runner. A legitimate virtual advance — even
/// jumping a day of virtual time — costs microseconds of real time, orders of
/// magnitude under this.
const DEFAULT_STRICT_WALL_CLOCK_BUDGET: std::time::Duration = std::time::Duration::from_secs(2);

/// Resolve the effective `strict_wall_clock` budget: the
/// `AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS` environment override when it holds a
/// valid whole-millisecond value, otherwise `default`.
///
/// Mirrors the [`__seed_from_env`] / [`parse_seed`] idiom so a too-tight budget
/// on a slow runner can be loosened globally without editing test code; a blank
/// or unparseable value falls back to `default`.
fn strict_budget_from_env_or(default: std::time::Duration) -> std::time::Duration {
    std::env::var("AUTUMN_SIM_STRICT_WALL_CLOCK_BUDGET_MS")
        .ok()
        .and_then(|raw| parse_strict_budget_ms(&raw))
        .unwrap_or(default)
}

/// Parse a whole-millisecond budget string into a [`std::time::Duration`],
/// returning `None` for empty/whitespace-only or unparseable input (so the
/// caller can fall back to the configured default).
fn parse_strict_budget_ms(raw: &str) -> Option<std::time::Duration> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }
    trimmed
        .parse::<u64>()
        .ok()
        .map(std::time::Duration::from_millis)
}

/// The forward-only step [`Sim::advance_to`] resolves a target instant into.
///
/// Kept as a small pure enum (rather than inlining the branch) so the
/// forward-only / no-op / panic-on-past decision is unit-testable without a
/// runtime or a paused clock.
#[derive(Debug, PartialEq, Eq)]
enum AdvancePlan {
    /// Target equals the current instant — advancing does nothing.
    NoOp,
    /// Target is in the future — step forward by exactly this real delta.
    Advance(std::time::Duration),
}

/// Decide how to advance from `now` to `target` under the forward-only clock
/// contract of [`Sim::advance_to`].
///
/// Returns [`AdvancePlan::NoOp`] when `target == now` and
/// [`AdvancePlan::Advance`] with the positive delta when `target` is in the
/// future.
///
/// # Panics
///
/// Panics when `target` is strictly before `now`: virtual time is forward-only,
/// and a past target signals a test-arithmetic bug that silent no-op behavior
/// would hide.
fn plan_advance_to(now: DateTime<Utc>, target: DateTime<Utc>) -> AdvancePlan {
    let delta = target - now;
    match delta.cmp(&chrono::Duration::zero()) {
        std::cmp::Ordering::Equal => AdvancePlan::NoOp,
        std::cmp::Ordering::Less => panic!(
            "Sim::advance_to target {target} is strictly before the current sim instant {now}; \
             virtual time is forward-only (advancing to a past instant is a test bug)"
        ),
        std::cmp::Ordering::Greater => AdvancePlan::Advance(
            delta
                .to_std()
                .expect("a strictly-positive chrono delta always converts to std::time::Duration"),
        ),
    }
}

/// Resolve a naive local wall-clock time in timezone `tz` to a single UTC
/// instant, handling DST edges deterministically (see
/// [`Sim::advance_to_local`] for the documented policy).
///
/// Ambiguous (fall-back) local times resolve to the **earlier** instant; a
/// nonexistent (spring-forward gap) local time is carried across the gap using
/// the post-transition UTC offset. Never `.unwrap()`s a
/// [`chrono::LocalResult`].
fn resolve_local_to_utc<Tz>(local: NaiveDateTime, tz: &Tz) -> DateTime<Utc>
where
    Tz: TimeZone,
{
    match tz.from_local_datetime(&local) {
        LocalResult::Single(dt) => dt.with_timezone(&Utc),
        LocalResult::Ambiguous(earlier, _later) => earlier.with_timezone(&Utc),
        LocalResult::None => {
            // Spring-forward gap: the wall time never occurs. Resolve it by
            // looking up the zone's offset at the UTC-clock reading numerically
            // equal to the wall time and applying it — a total, deterministic
            // mapping that carries the nonexistent time forward across the gap to
            // a single post-transition instant.
            let offset_secs =
                i64::from(tz.offset_from_utc_datetime(&local).fix().local_minus_utc());
            (local - chrono::Duration::seconds(offset_secs)).and_utc()
        }
    }
}

/// A seeded, deterministic random number generator handle.
///
/// Wraps a `ChaCha8Rng` seeded from the simulation seed, so the same seed
/// always yields the same draw sequence. Draw deterministic bytes and UUIDs
/// through the generation helpers below; they share their `Uuid` bit-stamping
/// with the [`crate::entropy::Entropy`] source an app is seeded with, so a
/// `SimRng` draw and an equivalently-seeded app draw agree.
pub struct SimRng {
    seed: u64,
    inner: ChaCha8Rng,
}

impl SimRng {
    /// Seed a fresh deterministic RNG from `seed`.
    pub(crate) fn new(seed: u64) -> Self {
        Self {
            seed,
            inner: ChaCha8Rng::seed_from_u64(seed),
        }
    }

    /// Derive a stable [`Uuid`] from this simulation's seed and a `purpose_tag`
    /// namespace, **independently of the draw stream** (seed-derived ids).
    ///
    /// Unlike [`uuid_v4`](Self::uuid_v4), this does **not** advance the RNG: the
    /// same seed and `purpose_tag` always produce the same UUID no matter how
    /// many other values have been drawn, so `derive_uuid("tenant:acme")` is a
    /// stable, byte-reproducible id for "acme" across runs and machines. Ideal
    /// for seeding multi-tenant fixtures without perturbing the deterministic id
    /// stream. See [`crate::entropy::SeededEntropy::derive_uuid`] for the shared
    /// mechanism and the version bits it sets (v4).
    #[must_use]
    pub fn derive_uuid(&self, purpose_tag: impl AsRef<[u8]>) -> Uuid {
        crate::entropy::derive_uuid_from(self.seed, purpose_tag.as_ref())
    }

    /// Draw the next deterministic `u64`.
    pub fn next_u64(&mut self) -> u64 {
        self.inner.next_u64()
    }

    /// Fill `dest` with deterministic bytes.
    pub fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.inner.fill_bytes(dest);
    }

    /// Draw a deterministic version-4 (fully random) [`Uuid`].
    ///
    /// The same seed and the same number of prior draws always yield the same
    /// UUID.
    #[must_use]
    pub fn uuid_v4(&mut self) -> Uuid {
        let mut bytes = [0u8; 16];
        self.inner.fill_bytes(&mut bytes);
        uuid_v4_from_bytes(bytes)
    }

    /// Draw a version-7 (time-ordered) [`Uuid`] whose 48-bit timestamp is
    /// `unix_millis` and whose remaining bits are drawn deterministically.
    #[must_use]
    pub fn uuid_v7(&mut self, unix_millis: u64) -> Uuid {
        let mut rand_bytes = [0u8; 10];
        self.inner.fill_bytes(&mut rand_bytes);
        uuid_v7_from_parts(unix_millis, rand_bytes)
    }

    /// Borrow the underlying `ChaCha8Rng`.
    ///
    /// Internal escape hatch for the determinism smoke test.
    #[cfg(test)]
    pub(crate) fn inner_mut(&mut self) -> &mut ChaCha8Rng {
        &mut self.inner
    }
}

/// A virtual clock handle for the simulation. Crate-private: no public API
/// returns it (issue #2967).
///
/// Wraps a [`TickingClock`] started at the fixed sim epoch. [`Sim::advance`]
/// steps this clock (the wall-clock time a [`crate::time::Clock`] extractor
/// reports) in lockstep with tokio's paused virtual timer, so
/// `Utc::now()`-via-extractor and `tokio::time::sleep` never drift apart.
pub(crate) struct SimClock {
    inner: TickingClock,
}

impl SimClock {
    /// Wrap a ticking clock as the simulation's virtual clock.
    pub(crate) fn new(inner: TickingClock) -> Self {
        Self { inner }
    }

    /// Step the injected wall clock forward by `duration`.
    ///
    /// This moves only the framework clock (the [`crate::time::Clock`]
    /// extractor / `ClockSource`); [`Sim::advance`] pairs it with
    /// `tokio::time::advance` so the tokio timer wheel moves the same amount.
    pub(crate) fn advance(&self, duration: std::time::Duration) {
        self.inner.advance(duration);
    }

    /// A [`TickingClock`] handle sharing this clock's instant.
    ///
    /// Handed to [`crate::test::TestApp::with_clock`] at [`Sim::build`] time so
    /// mounted handlers read the same virtual instant [`Sim::advance`] steps.
    pub(crate) fn ticking(&self) -> TickingClock {
        self.inner.clone()
    }

    /// The clock's current virtual UTC instant.
    ///
    /// Read by [`Sim::advance_to`] to compute the forward delta to a zoned
    /// target.
    pub(crate) fn now(&self) -> DateTime<Utc> {
        crate::time::ClockSource::now(&self.inner)
    }
}

/// The clock a `Sim` installs as its thread's ambient clock (issue #2967).
///
/// Wall time is the sim clock, so it moves only on [`Sim::advance`].
///
/// Elapsed time is kept per sim. Tokio's paused clock is one per runtime, and
/// several sims can share a runtime, so a sim's elapsed time is:
///
/// - the time its own [`Sim::advance`] calls moved tokio's clock, plus
/// - the time tokio's clock moved by itself (a `sleep` the paused runtime
///   auto-advanced) while this sim was the ambient clock.
///
/// So an ambient deadline and a `tokio::time::sleep` stay on one timeline, and
/// another sim's `advance` never moves this sim's elapsed time.
struct AmbientSimClock {
    /// The sim clock, or its chaos-skewed view once a skewed app mounts.
    wall: std::sync::RwLock<Arc<dyn crate::time::ClockSource>>,
    /// Time this sim's own `Sim::advance` calls moved tokio's clock.
    own_advanced: std::sync::Mutex<std::time::Duration>,
    /// Time tokio's clock moved by itself while this sim was ambient.
    auto_advanced: std::sync::Mutex<std::time::Duration>,
    /// Cleared when the sim drops, on any thread.
    alive: std::sync::atomic::AtomicBool,
}

impl AmbientSimClock {
    fn add(slot: &std::sync::Mutex<std::time::Duration>, duration: std::time::Duration) {
        let mut total = slot
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        *total = total.saturating_add(duration);
    }

    fn read(slot: &std::sync::Mutex<std::time::Duration>) -> std::time::Duration {
        *slot
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn is_alive(&self) -> bool {
        self.alive.load(std::sync::atomic::Ordering::Acquire)
    }
}

/// The sims alive on one thread, and tokio's instant at the last settle.
#[derive(Default)]
struct SimStack {
    /// Newest last. The newest live sim is the ambient one.
    clocks: Vec<Arc<AmbientSimClock>>,
    /// Tokio's instant up to which auto-advanced time is attributed. `None`
    /// until a sim's runtime starts on this thread.
    checkpoint: Option<tokio::time::Instant>,
    /// Whether this thread already warned about a sim that was not anchored.
    warned_unanchored: bool,
    /// `Sim::advance` time not yet taken out of a settle. The next settles
    /// take it out of the tokio time they see first, so an advance is never
    /// also counted as auto-advanced time: not when it is cancelled after the
    /// clock moved, and not when advances run at the same time.
    pending_advance: std::time::Duration,
    /// The runtime these sims run on, so a sim dropped on another thread can
    /// still read this runtime's clock and settle up to its drop.
    runtime: Option<tokio::runtime::Handle>,
}

impl SimStack {
    /// [`settle`](Self::settle), from a clock read or an advance. When no
    /// sim on this thread was anchored, warn once: tokio time that passed
    /// before this call is not in the sim's elapsed time.
    fn settle_lazily(&mut self) {
        if self.checkpoint.is_none()
            && !self.warned_unanchored
            && self.clocks.iter().any(|clock| clock.is_alive())
            && tokio::runtime::Handle::try_current().is_ok()
        {
            self.warned_unanchored = true;
            tracing::warn!(
                "a Sim built outside its runtime was not anchored; its elapsed \
                 time starts now. Call `Sim::anchor` first in the runtime, or use \
                 #[sim_test]"
            );
        }
        self.settle();
    }

    /// Give the tokio time since the last settle to the ambient sim, and
    /// drop entries whose sim has dropped (on any thread).
    fn settle(&mut self) {
        if let Ok(current) = tokio::runtime::Handle::try_current() {
            self.runtime = Some(current);
        }
        let Some(runtime) = self.runtime.clone() else {
            self.clocks.retain(|clock| clock.is_alive());
            return;
        };
        // Read this stack's runtime clock, also from another thread.
        let now = {
            let _enter = runtime.enter();
            tokio::time::Instant::now()
        };
        if let Some(checkpoint) = self.checkpoint {
            let moved = now.saturating_duration_since(checkpoint);
            let explicit = moved.min(self.pending_advance);
            self.pending_advance = self.pending_advance.saturating_sub(explicit);
            if let Some(top) = self.clocks.last() {
                AmbientSimClock::add(&top.auto_advanced, moved.saturating_sub(explicit));
            }
        }
        // Prune only after attributing: the time up to now belongs to the sim
        // that was ambient, even if it has since dropped on another thread.
        self.clocks.retain(|clock| clock.is_alive());
        if self.clocks.is_empty() {
            self.checkpoint = None;
            self.pending_advance = std::time::Duration::ZERO;
            self.runtime = None;
        } else {
            self.checkpoint = Some(now);
        }
    }
}

/// A thread's sim stack. Shared, so a sim dropped on another thread can
/// settle the stack of the thread it was built on.
type SharedSimStack = Arc<std::sync::Mutex<SimStack>>;

thread_local! {
    static SIM_STACK: SharedSimStack = SharedSimStack::default();
}

/// Run `f` on `stack`.
fn lock_sim_stack<T>(stack: &SharedSimStack, f: impl FnOnce(&mut SimStack) -> T) -> T {
    f(&mut stack
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner))
}

/// Run `f` on this thread's sim stack. `None` during thread teardown.
fn with_sim_stack<T>(f: impl FnOnce(&mut SimStack) -> T) -> Option<T> {
    SIM_STACK.try_with(|stack| lock_sim_stack(stack, f)).ok()
}

/// Start attributing tokio time on this thread from now, if nothing has
/// yet. `#[sim_test]` builds the sim before its runtime, so it calls this as
/// the runtime starts. Settling first keeps any time already attributed.
fn anchor_current_sim() {
    if tokio::runtime::Handle::try_current().is_err() {
        return;
    }
    with_sim_stack(SimStack::settle);
}

/// Keeps a sim on its thread's sim stack while the sim lives.
struct SimStackGuard {
    own: Arc<AmbientSimClock>,
    /// The stack of the thread the sim was built on.
    home: SharedSimStack,
}

impl SimStackGuard {
    fn enter(own: Arc<AmbientSimClock>) -> Self {
        let home = SIM_STACK.with(Arc::clone);
        lock_sim_stack(&home, |stack| {
            // Time so far belongs to the sim that was ambient until now.
            stack.settle();
            stack.clocks.push(Arc::clone(&own));
            if stack.checkpoint.is_none() && tokio::runtime::Handle::try_current().is_ok() {
                stack.checkpoint = Some(tokio::time::Instant::now());
            }
        });
        Self { own, home }
    }
}

impl Drop for SimStackGuard {
    fn drop(&mut self) {
        // On the home stack, from any thread: settle, so time up to the drop
        // goes to this sim while it is still ambient; then mark it dead and
        // settle again, which removes it. Later time goes to the next sim.
        lock_sim_stack(&self.home, |stack| {
            stack.settle();
            self.own
                .alive
                .store(false, std::sync::atomic::Ordering::Release);
            stack.settle();
        });
    }
}

/// Record `duration` as `sim`'s own advance. Call in the same poll that moves
/// tokio's clock, so a cancel cannot split the two.
fn note_own_advance(sim: &AmbientSimClock, duration: std::time::Duration) {
    with_sim_stack(|stack| {
        stack.settle_lazily();
        stack.pending_advance = stack.pending_advance.saturating_add(duration);
    });
    AmbientSimClock::add(&sim.own_advanced, duration);
}

impl crate::time::ClockSource for AmbientSimClock {
    fn now(&self) -> DateTime<Utc> {
        self.wall
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .now()
    }

    fn monotonic(&self) -> crate::time::MonotonicInstant {
        // With no runtime there is no paused clock to settle from. The total
        // as last settled still holds the auto-advanced time, so a thread
        // with no runtime reads the same time as the sim's.
        if tokio::runtime::Handle::try_current().is_ok() {
            with_sim_stack(SimStack::settle_lazily);
        }
        crate::time::MonotonicInstant::from_origin_elapsed(
            Self::read(&self.own_advanced).saturating_add(Self::read(&self.auto_advanced)),
        )
    }
}

/// The built application handle for a simulation.
///
/// Holds the [`crate::test::TestClient`] mounted by [`Sim::build`] on the paused
/// runtime with the simulation's virtual clock installed. Empty until
/// [`Sim::build`] is called (an empty [`#[sim_test]`](crate::sim_test) that only
/// drives time / RNG never mounts an app).
///
/// Crate-private: no public API returns it (issue #2967).
#[derive(Default)]
pub(crate) struct SimApp {
    /// The mounted test client, or `None` before [`Sim::build`].
    client: Option<crate::test::TestClient>,
}

impl SimApp {
    /// Borrow the mounted [`crate::test::TestClient`].
    ///
    /// # Panics
    ///
    /// Panics if no app has been mounted yet — call [`Sim::build`] first.
    #[must_use]
    pub fn client(&self) -> &crate::test::TestClient {
        self.try_client()
            .expect("no app mounted: call `sim.build(TestApp::new()...)` before `client()`")
    }

    /// Borrow the mounted [`crate::test::TestClient`], or `None` before
    /// [`Sim::build`].
    #[must_use]
    pub const fn try_client(&self) -> Option<&crate::test::TestClient> {
        self.client.as_ref()
    }
}

/// Read and parse the simulation seed from the `AUTUMN_SIM_SEED` environment
/// variable.
///
/// Accepts a hex (`0x`-prefixed) or decimal `u64`; an absent or unparseable
/// value falls back to `0`. Called by the [`#[sim_test]`](crate::sim_test)
/// macro so the parsing is unit-tested in one place and the macro stays tiny.
#[doc(hidden)]
#[must_use]
pub fn __seed_from_env() -> u64 {
    std::env::var("AUTUMN_SIM_SEED").map_or(0, |raw| parse_seed(&raw))
}

/// Parse a seed string: hex (`0x`/`0X` prefixed) or decimal, defaulting to `0`
/// on any parse failure or empty input.
fn parse_seed(raw: &str) -> u64 {
    let trimmed = raw.trim();
    trimmed
        .strip_prefix("0x")
        .or_else(|| trimmed.strip_prefix("0X"))
        .map_or_else(
            || trimmed.parse::<u64>().unwrap_or(0),
            |hex| u64::from_str_radix(hex, 16).unwrap_or(0),
        )
}

/// Build the deterministic replay line printed on a sim-test panic.
///
/// Returns exactly
/// `AUTUMN_SIM_SEED=0x<seed-hex> cargo test -p <pkg> <test>` — copy-paste it to
/// reproduce the failing run bit-for-bit. Called by the
/// [`#[sim_test]`](crate::sim_test) macro.
#[doc(hidden)]
#[must_use]
pub fn __replay_line(seed: u64, pkg: &str, test: &str) -> String {
    format!("AUTUMN_SIM_SEED=0x{seed:x} cargo test -p {pkg} {test}")
}

#[cfg(test)]
mod tests {
    use super::{
        __replay_line, AdvancePlan, DEFAULT_STRICT_WALL_CLOCK_BUDGET, MAX_LIVENESS_BUDGET, Sim,
        mount_entropy_seed, parse_liveness_budget, parse_seed, parse_strict_budget_ms,
        plan_advance_to, resolve_local_to_utc, strict_budget_from_env_or,
    };
    use chrono::{NaiveDate, TimeZone, Utc};
    use rand::Rng;

    #[tokio::test(start_paused = true)]
    async fn blocking_work_reads_auto_advanced_sim_time() {
        let sim = Sim::from_seed(11);
        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
        sim.advance(std::time::Duration::from_secs(2)).await;
        let here = (
            crate::time::ambient_monotonic(),
            crate::time::ambient_instant(),
            crate::time::ambient_now(),
        );
        assert_eq!(here.0.since_origin(), std::time::Duration::from_secs(7));
        let there = crate::time::spawn_blocking(|| {
            (
                crate::time::ambient_monotonic(),
                crate::time::ambient_instant(),
                crate::time::ambient_now(),
            )
        })
        .await
        .unwrap();
        assert_eq!(there, here, "a blocking worker reads the caller's sim time");
    }

    #[tokio::test(start_paused = true)]
    async fn a_thread_with_no_runtime_reads_auto_advanced_sim_time() {
        let sim = Sim::from_seed(12);
        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
        let here = crate::time::ambient_monotonic();
        assert_eq!(here.since_origin(), std::time::Duration::from_secs(5));
        let clock: std::sync::Arc<dyn crate::time::ClockSource> = sim.ambient.clone();
        let there = std::thread::spawn(move || clock.monotonic())
            .join()
            .unwrap();
        assert_eq!(there, here);
    }

    #[test]
    fn liveness_budget_is_off_unless_armed() {
        assert_eq!(parse_liveness_budget(None), None);
        assert_eq!(parse_liveness_budget(Some("")), None);
        assert_eq!(parse_liveness_budget(Some("0")), None);
        assert_eq!(parse_liveness_budget(Some("soon")), None);
        assert_eq!(parse_liveness_budget(Some("-5")), None);
    }

    #[test]
    fn liveness_budget_takes_seconds_and_clamps_to_the_tokio_maximum() {
        assert_eq!(
            parse_liveness_budget(Some(" 90 ")),
            Some(std::time::Duration::from_secs(90))
        );
        assert_eq!(
            parse_liveness_budget(Some("18446744073709551615")),
            Some(MAX_LIVENESS_BUDGET)
        );
    }

    #[test]
    fn mount_entropy_seed_is_the_sim_seed_first_then_derived() {
        assert_eq!(mount_entropy_seed(7, 0), 7);
        let restart = mount_entropy_seed(7, 1);
        assert_ne!(restart, 7, "a restart draws a new stream");
        assert_eq!(restart, mount_entropy_seed(7, 1), "and it is deterministic");
        assert_ne!(restart, mount_entropy_seed(7, 2));
        assert_ne!(restart, mount_entropy_seed(8, 1));
    }

    #[test]
    fn replay_line_zero_seed_is_exact() {
        assert_eq!(
            __replay_line(0, "autumn-web", "my_test"),
            "AUTUMN_SIM_SEED=0x0 cargo test -p autumn-web my_test"
        );
    }

    #[test]
    fn replay_line_formats_seed_as_hex() {
        let line = __replay_line(0x9f3a, "autumn-web", "my_test");
        assert!(
            line.contains("0x9f3a"),
            "seed must be rendered in hex: {line}"
        );
        assert_eq!(
            line,
            "AUTUMN_SIM_SEED=0x9f3a cargo test -p autumn-web my_test"
        );
    }

    #[test]
    fn parse_seed_covers_hex_decimal_and_garbage() {
        assert_eq!(parse_seed("0"), 0);
        assert_eq!(parse_seed("0x9f3a"), 0x9f3a);
        assert_eq!(parse_seed("0X9F3A"), 0x9f3a);
        assert_eq!(parse_seed("42"), 42);
        assert_eq!(parse_seed("garbage"), 0);
        assert_eq!(parse_seed(""), 0);
        assert_eq!(parse_seed("  0x10  "), 0x10);
    }

    #[test]
    fn from_seed_exposes_the_seed() {
        assert_eq!(Sim::from_seed(0).seed, 0);
        assert_eq!(Sim::from_seed(7).seed(), 7);
    }

    #[test]
    fn same_seed_produces_identical_first_draw() {
        let mut a = Sim::from_seed(7);
        let mut b = Sim::from_seed(7);
        let da = a.rng().inner_mut().next_u64();
        let db = b.rng().inner_mut().next_u64();
        assert_eq!(da, db, "same seed must yield the same first RNG draw");
    }

    #[test]
    fn plan_advance_to_equal_target_is_noop() {
        let now = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
        assert_eq!(plan_advance_to(now, now), AdvancePlan::NoOp);
    }

    #[test]
    fn plan_advance_to_future_target_is_exact_delta() {
        let now = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
        let target = Utc.with_ymd_and_hms(2020, 1, 1, 1, 0, 0).unwrap();
        assert_eq!(
            plan_advance_to(now, target),
            AdvancePlan::Advance(std::time::Duration::from_secs(3600))
        );
    }

    #[test]
    #[should_panic(expected = "forward-only")]
    fn plan_advance_to_past_target_panics() {
        let now = Utc.with_ymd_and_hms(2020, 1, 1, 1, 0, 0).unwrap();
        let target = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
        let _ = plan_advance_to(now, target);
    }

    #[test]
    fn parse_strict_budget_ms_covers_valid_and_garbage() {
        use std::time::Duration;
        assert_eq!(
            parse_strict_budget_ms("100"),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            parse_strict_budget_ms("  250  "),
            Some(Duration::from_millis(250))
        );
        assert_eq!(parse_strict_budget_ms("0"), Some(Duration::ZERO));
        assert_eq!(parse_strict_budget_ms(""), None);
        assert_eq!(parse_strict_budget_ms("   "), None);
        assert_eq!(parse_strict_budget_ms("garbage"), None);
        assert_eq!(parse_strict_budget_ms("-5"), None);
        assert_eq!(parse_strict_budget_ms("1.5"), None);
    }

    #[test]
    fn strict_budget_from_env_falls_back_to_default_when_unset() {
        // The env var is not set in this unit-test process, so the default is
        // returned verbatim (the override path is exercised via the public
        // integration tests, which never set the var).
        let default = std::time::Duration::from_millis(7);
        assert_eq!(strict_budget_from_env_or(default), default);
        assert_eq!(
            strict_budget_from_env_or(DEFAULT_STRICT_WALL_CLOCK_BUDGET),
            DEFAULT_STRICT_WALL_CLOCK_BUDGET
        );
    }

    #[test]
    fn strict_wall_clock_builders_set_the_budget() {
        let mut sim = Sim::from_seed(0);
        assert!(sim.strict_budget.is_none(), "guard is off by default");
        sim.strict_wall_clock();
        assert_eq!(sim.strict_budget, Some(DEFAULT_STRICT_WALL_CLOCK_BUDGET));

        let mut custom = Sim::from_seed(0);
        custom.strict_wall_clock_budget(std::time::Duration::from_millis(5));
        assert_eq!(
            custom.strict_budget,
            Some(std::time::Duration::from_millis(5))
        );
    }

    #[test]
    fn resolve_local_unambiguous_maps_to_single_instant() {
        // A plain UTC-offset zone: 12:00 at +00:00 is exactly 12:00Z.
        let local = NaiveDate::from_ymd_opt(2020, 6, 1)
            .unwrap()
            .and_hms_opt(12, 0, 0)
            .unwrap();
        let got = resolve_local_to_utc(local, &Utc);
        assert_eq!(got, Utc.with_ymd_and_hms(2020, 6, 1, 12, 0, 0).unwrap());
    }

    /// A replica on the sim network is a host of that network, and its app
    /// holds the network. The sim must break that cycle when it drops, or a
    /// seed sweep keeps every replica app of every seed in memory.
    #[cfg(feature = "http-client")]
    #[tokio::test(start_paused = true)]
    async fn a_dropped_sim_frees_its_replica_apps() {
        let marker = std::sync::Arc::new(());
        let freed = std::sync::Arc::downgrade(&marker);
        {
            let mut sim = Sim::from_seed(21);
            sim.net(super::net::SimNet::new());
            let held = std::sync::Arc::clone(&marker);
            let router = axum::Router::new().route(
                "/held",
                axum::routing::get(move || {
                    let holders = std::sync::Arc::strong_count(&held);
                    async move { holders.to_string() }
                }),
            );
            sim.mount_replica("a", crate::test::TestApp::new().merge(router));
            sim.mount_replica("b", crate::test::TestApp::new());
        }
        drop(marker);
        assert!(
            freed.upgrade().is_none(),
            "the replica app is still alive after its sim dropped"
        );
    }

    /// A replica stays a host of the network it joined, even after
    /// [`Sim::net`] sets a new network. The kill and the drop must clean that
    /// network, and leave a host of the same name on the new one.
    #[cfg(feature = "http-client")]
    #[tokio::test(start_paused = true)]
    async fn a_replica_is_cleaned_from_the_network_it_joined() {
        let marker = std::sync::Arc::new(());
        let freed = std::sync::Arc::downgrade(&marker);
        let first = super::net::SimNet::new();
        let held = std::sync::Arc::clone(&marker);
        let router = axum::Router::new().route(
            "/held",
            axum::routing::get(move || {
                let holders = std::sync::Arc::strong_count(&held);
                async move { holders.to_string() }
            }),
        );
        let second = super::net::SimNet::new().host("a", axum::Router::new());
        {
            let mut sim = Sim::from_seed(22);
            sim.net(first.clone());
            sim.mount_replica("a", crate::test::TestApp::new().merge(router));
            sim.net(second.clone());
        }
        drop(marker);
        assert!(
            freed.upgrade().is_none(),
            "the replica app is still alive in the network it joined"
        );
        assert!(first.service("a").is_none());
        assert!(
            second.service("a").is_some(),
            "a host of another network stays"
        );
    }
}