optionchain_simulator 0.2.29

OptionChain-Simulator is a lightweight REST API service that simulates an evolving option chain with every request. It is designed for developers building or testing trading systems, backtesters, and visual tools that depend on option data streams but want to avoid relying on live data feeds.
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
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
//! Lifecycle for v2 rolling simulations.
//!
//! The v2 counterpart of [`crate::session::SessionManager`]: it owns the
//! [`SimulationStore`], the per-simulation factor tapes, and the bounded
//! snapshot cache, and it is the only thing the API layer talks to. Handlers
//! never reach into `domain` — that module is private, and the layering says
//! api → session → domain.
//!
//! # What it guarantees
//!
//! - **Serve-then-advance.** `advance` serves the snapshot at the current
//!   cursor and *then* moves it, so a simulation with `steps = N` serves
//!   exactly indices `0..N-1` over `N` advances. This is v1's semantics,
//!   deliberately carried forward.
//! - **A peek changes nothing.** `peek` builds the same snapshot and writes
//!   nothing back, so calling it repeatedly is safe and returns the same
//!   answer until an advance moves the cursor.
//! - **No lost advance.** Every advance persists through a compare-and-swap on
//!   the revision it read, so two concurrent advances cannot both commit: the
//!   loser gets a `Conflict` and retries.
//! - **Caches are never authoritative.** A factor tape or a snapshot can be
//!   dropped at any time; both rebuild identically from the effective
//!   parameters, so eviction changes latency and nothing else.

use crate::domain::factors::FactorTape;
use crate::domain::series::{SeriesBuilder, SeriesSnapshot, SnapshotCache};
use crate::infrastructure::{
    MetricsCollector, SimulationSnapshotRepository, SimulationV2Config, SnapshotRecord,
};
use crate::session::model::SessionState;
use crate::session::snapshot_record::{snapshot_quote_count, snapshot_record};
use crate::session::store::{BuildClaim, SharedTapeCache, SimulationStore, tape_key};
use crate::session::{SessionV2, SimulationParametersV2};
use crate::utils::ChainError;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::{broadcast, mpsc};
use tracing::{debug, info, instrument, warn};
use uuid::Uuid;

/// How long a caller waits for the instance that claimed a build.
///
/// Long enough for a tape of the maximum step count, short enough that an
/// owner which died leaves the others building rather than waiting out the
/// claim's own expiry.
const SHARED_BUILD_WAIT: Duration = Duration::from_secs(30);

/// How often a waiter looks for the tape the owner is building.
const SHARED_BUILD_POLL: Duration = Duration::from_millis(50);

/// What asking for the deployment-wide build claim produced.
enum SharedBuild {
    /// This caller builds. The token, when there is one, is what releases the
    /// claim; there is none when no cache is configured, when the cache could
    /// not answer, or when the wait for another instance ran out.
    Owner(Option<String>),
    /// Another instance built it, and here it is.
    Waited(FactorTape),
}

/// Everything the owner of a tape build needs, owned rather than borrowed.
///
/// Owned because the workflow runs in a task that outlives the request that
/// started it: the build cannot be cancelled once it is on the blocking pool,
/// and the waiters, the shared cache and the deployment-wide claim must be
/// settled whether the caller is still there or not.
struct OwnedBuild {
    id: Uuid,
    /// The tape's shared-cache key, computed once by the caller.
    key: String,
    parameters: SimulationParametersV2,
    tapes: Arc<Mutex<HashMap<Uuid, TapeEntry>>>,
    builds: Arc<Mutex<HashMap<Uuid, TapeBuilds>>>,
    shared: Option<Arc<dyn SharedTapeCache>>,
    max_cached_tapes: usize,
    shared_build_wait: Duration,
}

impl OwnedBuild {
    /// Builds the tape (or takes another instance's), publishes it, and gives
    /// back every claim it took.
    async fn run(self, sender: TapeBuilds) {
        let claim = self.claim().await;
        let built_here = !matches!(claim, SharedBuild::Waited(_));
        // Kept out of the match, which consumes the claim, because the token
        // is what releases it at the end.
        let token = match &claim {
            SharedBuild::Owner(token) => token.clone(),
            SharedBuild::Waited(_) => None,
        };

        let result = match claim {
            SharedBuild::Waited(tape) => {
                SimulationManager::cache_tape(
                    &self.tapes,
                    self.max_cached_tapes,
                    self.id,
                    tape.clone(),
                );
                Ok(tape)
            }
            SharedBuild::Owner(_) => {
                SimulationManager::build_tape_into(
                    self.parameters.clone(),
                    self.id,
                    Arc::clone(&self.tapes),
                    self.max_cached_tapes,
                )
                .await
            }
        };

        // Publish to whoever is waiting and stop being the owner, in that
        // order: a caller that subscribes after the removal misses the
        // broadcast, retries, and finds the tape in the cache.
        //
        // Before the shared write, deliberately. A slow Redis would otherwise
        // hold every local waiter behind a round trip they do not need.
        {
            let mut builds = match self.builds.lock() {
                Ok(builds) => builds,
                Err(poisoned) => poisoned.into_inner(),
            };
            builds.remove(&self.id);
        }
        let published = match &result {
            Ok(tape) => Ok(tape.clone()),
            Err(error) => Err(error.to_string()),
        };
        // An error means nobody was waiting, which happens whenever the
        // request that started this went away.
        let _ = sender.send(published);

        // Only now offer it to the other instances: this process is already
        // served, so a slow or failing write costs the deployment a rebuild
        // elsewhere and costs this request nothing. A tape another instance
        // published is already there, so only one built here is worth writing.
        if let (Some(shared), true, Ok(tape)) = (self.shared.as_ref(), built_here, &result) {
            SimulationManager::share_tape_to(shared.as_ref(), &self.key, self.id, tape).await;
        }

        // The claim goes back after the shared write, not before: an instance
        // that wakes on the release must find the tape rather than an empty
        // key. Released whether the build succeeded or not, so a failure does
        // not make the others wait out the expiry for a tape nobody wrote.
        if let (Some(shared), Some(token)) = (self.shared.as_ref(), &token) {
            shared.release_build(&self.key, token).await;
        }
    }

    /// Claims the deployment-wide right to build this tape, or waits for the
    /// instance that holds it.
    ///
    /// The wait is bounded and polls the shared cache rather than a channel:
    /// there is no channel between processes, and an owner that dies would
    /// otherwise leave every waiter hanging until its claim expired. When the
    /// wait runs out this caller builds, which is duplicated work rather than
    /// a request that never answers.
    async fn claim(&self) -> SharedBuild {
        let Some(shared) = self.shared.as_ref() else {
            return SharedBuild::Owner(None);
        };

        match shared.claim_build(&self.key).await {
            // Nothing to release: without a claim there is nothing this
            // instance holds, and deleting the key would take a claim another
            // instance owns.
            BuildClaim::Unclaimed => return SharedBuild::Owner(None),
            BuildClaim::Held(token) => return SharedBuild::Owner(Some(token)),
            BuildClaim::Taken => {}
        }

        debug!(
            simulation_id = %self.id,
            "another instance is building this tape; waiting for it"
        );
        let deadline = Instant::now() + self.shared_build_wait;
        while Instant::now() < deadline {
            tokio::time::sleep(SHARED_BUILD_POLL).await;
            if let Some(tape) =
                SimulationManager::shared_tape_from(shared.as_ref(), &self.key, self.id).await
            {
                return SharedBuild::Waited(tape);
            }
        }

        warn!(
            simulation_id = %self.id,
            waited_secs = self.shared_build_wait.as_secs(),
            "the instance building this tape did not publish in time; building it here"
        );
        SharedBuild::Owner(None)
    }
}

/// One cached factor tape and the last time it was used.
struct TapeEntry {
    tape: FactorTape,
    last_access: Instant,
}

/// A snapshot's identity in the in-flight map: which simulation, which step.
type SnapshotKey = (Uuid, usize);

/// How a snapshot build publishes its result to the callers waiting on it.
///
/// The error is a `String` rather than a `ChainError` because `broadcast`
/// requires `Clone` and `ChainError` is not; the waiter rebuilds it as an
/// internal error, which is what the owner would have reported anyway.
type SnapshotBuilds = broadcast::Sender<Result<SeriesSnapshot, String>>;

/// How a tape build publishes its result to the callers waiting on it.
type TapeBuilds = broadcast::Sender<Result<FactorTape, String>>;

/// Owns the lifecycle of v2 rolling simulations.
pub struct SimulationManager {
    store: Arc<dyn SimulationStore>,
    config: SimulationV2Config,
    tapes: Arc<Mutex<HashMap<Uuid, TapeEntry>>>,
    /// Tape builds currently running, one entry per simulation.
    ///
    /// Without it, N concurrent first reads of one simulation start N identical
    /// builds — and a build is the one place a v2 request does seconds of CPU,
    /// so the duplicates are not a wasted allocation, they are the machine.
    /// `spawn_blocking` does not bound that: its pool grows to hundreds of
    /// threads, so the cache would still be cold while every core was busy
    /// filling it with the same answer.
    builds: Arc<Mutex<HashMap<Uuid, TapeBuilds>>>,
    /// Snapshot builds currently running, one entry per `(simulation, step)`.
    ///
    /// The same argument as [`SimulationManager::builds`], one level down and
    /// sharper since issue #74: with a warehouse registered a snapshot build
    /// prices up to `OCS_MAX_SNAPSHOT_CONTRACTS` contracts WITH both greek
    /// snapshots, so N concurrent readers of one step used to commit the
    /// machine to N copies of the same seconds-long job. One owner builds, the
    /// rest wait on its result.
    snapshot_builds: Mutex<HashMap<SnapshotKey, SnapshotBuilds>>,
    snapshots: Arc<Mutex<SnapshotCache>>,
    /// Where a built tape is left for the OTHER instances, when one is
    /// configured.
    ///
    /// The local map above is the first level and stays: a hit there costs
    /// nothing, where this costs a round trip. What it removes is the rebuild
    /// a second instance would otherwise do for a tape its neighbour already
    /// walked, which is the expensive half of serving a step (issue #136).
    ///
    /// `None` is a deployment with no shared cache configured, and every
    /// failure of the shared one is treated as `None` for that call: a cache
    /// that cannot be reached is a miss, never a failed request.
    shared_tapes: Option<Arc<dyn SharedTapeCache>>,
    /// How long to wait for the instance that claimed a build before building
    /// it here.
    ///
    /// A field rather than a constant so a test can prove the give-up path
    /// without waiting out the production value, which is deliberately as long
    /// as the slowest build.
    shared_build_wait: Duration,
    /// Where the count of filed rows is reported, when the binary wired it.
    ///
    /// Set before [`Self::with_warehouse`], which is what the writer task
    /// captures. `None` in a test or a library caller that has no collector,
    /// and the writer then files without counting.
    snapshot_metrics: Option<Arc<MetricsCollector>>,
    /// Where served snapshots are queued for filing, when the operator turned
    /// persistence on. `None` is the default and the whole feature is then
    /// absent from the serving path — no connection, no latency, no failure
    /// mode.
    warehouse: Option<Warehouse>,
}

/// The queue in front of the warehouse, and what is currently in it.
struct Warehouse {
    /// The repository itself, so a reader — the export — can consult the same
    /// warehouse the writer fills without being handed a second handle to it.
    repository: Arc<dyn SimulationSnapshotRepository>,
    sender: mpsc::Sender<SnapshotRecord>,
    /// Quote rows queued but not yet written. Incremented before a send and
    /// decremented by the writer once the record leaves the queue, so it
    /// measures what is resident rather than what has been served.
    queued_contracts: Arc<AtomicUsize>,
}

/// How many snapshots may be waiting to be filed.
///
/// The queue is what keeps a degraded warehouse from becoming a memory leak. An
/// unbounded spawn-per-advance cannot delay a response, but at a sustained
/// advance rate against a warehouse that is timing out it accumulates records
/// until the process dies — which fails every request, not just the write.
const SNAPSHOT_QUEUE_DEPTH: usize = 1_024;

/// How many quote rows may be waiting to be filed, across every queued record.
///
/// A depth in *records* is the wrong unit for the same reason an entry count
/// was the wrong unit for the snapshot cache: a record is a few hundred quotes
/// in the reference configuration and up to the per-snapshot cap in a large
/// one, so 1 024 of them is anywhere from a hundred thousand to two hundred
/// million rows. This bounds what is actually resident.
///
/// Like the cache bound, the count is derived from a byte budget of roughly
/// 850 MB rather than chosen: `size_of::<QuoteRow>()` is 620 bytes since issue
/// #74 gave it both greek snapshots (212 bytes before), so 1 350 000 × 620 B ≈
/// 837 MB. The count came down from 4 000 000 in the same change, to hold the
/// budget the previous count expressed.
///
/// Neither bound is a knob. A deployment that needs to tune them is one whose
/// warehouse cannot keep up with its advance rate, and the answer there is the
/// warehouse, not a deeper buffer in front of it.
const SNAPSHOT_QUEUE_CONTRACTS: usize = 1_350_000;

impl SimulationManager {
    /// Creates a manager over a simulation store.
    ///
    /// The only method the binary needs; everything else is crate-internal,
    /// because it deals in `domain` types that are not part of this crate's
    /// public API. The v2 REST surface is the contract, not these signatures.
    #[must_use]
    pub fn new(store: Arc<dyn SimulationStore>, config: SimulationV2Config) -> Self {
        Self {
            store,
            config,
            tapes: Arc::new(Mutex::new(HashMap::new())),
            builds: Arc::new(Mutex::new(HashMap::new())),
            snapshot_builds: Mutex::new(HashMap::new()),
            snapshots: Arc::new(Mutex::new(SnapshotCache::with_bounds(
                config.max_cached_snapshots,
                config.max_cached_snapshot_contracts,
            ))),
            shared_tapes: None,
            shared_build_wait: SHARED_BUILD_WAIT,
            snapshot_metrics: None,
            warehouse: None,
        }
    }

    /// Shares built tapes with the other instances through `cache`.
    ///
    /// Opt-in for the same reason the warehouse is: a single-instance
    /// deployment should not have to name the feature to not use it, and the
    /// serving path should express "no shared cache" as a missing dependency
    /// rather than as a flag it branches on.
    #[must_use]
    pub fn with_shared_tapes(mut self, cache: Arc<dyn SharedTapeCache>) -> Self {
        self.shared_tapes = Some(cache);
        self
    }

    /// How long to wait for another instance's build before building here.
    ///
    /// Exists for tests: production wants the default, which is sized for the
    /// slowest build, and a test that has to reach the give-up path should not
    /// spend that long doing it.
    #[must_use]
    pub fn with_shared_build_wait(mut self, wait: Duration) -> Self {
        self.shared_build_wait = wait;
        self
    }

    /// Reports filed rows to `metrics`.
    ///
    /// Call it BEFORE [`Self::with_warehouse`]: that method spawns the writer,
    /// and the writer captures whatever this left behind. Separate from it
    /// rather than a parameter of it, because the warehouse is wired by
    /// library callers that have no collector and the signature is public.
    ///
    /// What it buys is a signal that does not exist otherwise: the write is
    /// detached from the advance that produced it, so "the step was served"
    /// says nothing about "the row is stored", and `v2_snapshot_rows_filed_total`
    /// is how anything outside the process can tell (issue #149).
    #[must_use]
    pub fn with_snapshot_metrics(mut self, metrics: Arc<MetricsCollector>) -> Self {
        self.snapshot_metrics = Some(metrics);
        self
    }

    /// Files every served snapshot in `warehouse`.
    ///
    /// Opt-in, and deliberately a separate constructor rather than an argument
    /// to [`SimulationManager::new`]: a deployment without ClickHouse should not
    /// have to name the feature to not use it, and the serving path should not
    /// branch on a config flag it can express as a missing dependency.
    #[must_use]
    pub fn with_warehouse(mut self, repository: Arc<dyn SimulationSnapshotRepository>) -> Self {
        let (sender, mut receiver) = mpsc::channel::<SnapshotRecord>(SNAPSHOT_QUEUE_DEPTH);
        let queued_contracts = Arc::new(AtomicUsize::new(0));
        let writer_contracts = Arc::clone(&queued_contracts);
        let warehouse = Arc::clone(&repository);
        let writer_metrics = self.snapshot_metrics.clone();

        // One writer, not one task per advance: the queue bounds what a slow
        // warehouse can accumulate, and serialising the writes means two steps
        // of one simulation reach the warehouse in the order they were served.
        tokio::spawn(async move {
            while let Some(record) = receiver.recv().await {
                let simulation = record.simulation;
                let step = record.step;
                let contracts = record.quote_count();

                let result = warehouse.persist(record).await;
                writer_contracts.fetch_sub(contracts, Ordering::SeqCst);

                // Counted on the write that SUCCEEDED: a queue that never
                // drains must not look like storage that is filling.
                if result.is_ok()
                    && let Some(metrics) = writer_metrics.as_ref()
                {
                    metrics.record_snapshot_rows_filed(contracts);
                }

                if let Err(error) = result {
                    warn!(
                        simulation_id = %simulation,
                        step,
                        error = %error,
                        "Could not file the snapshot; the step can be replayed and rewritten"
                    );
                }
            }
        });

        self.warehouse = Some(Warehouse {
            repository,
            sender,
            queued_contracts,
        });
        self
    }

    /// The warehouse this manager files into, if any.
    ///
    /// Exists so the export can prefer persisted snapshots over replay without
    /// the binary threading a second handle through the server: the manager
    /// already owns the one the writer uses, and two handles could drift to two
    /// different configurations.
    #[must_use]
    pub fn warehouse(&self) -> Option<Arc<dyn SimulationSnapshotRepository>> {
        self.warehouse
            .as_ref()
            .map(|warehouse| Arc::clone(&warehouse.repository))
    }

    /// The operational configuration this manager applies.
    #[must_use]
    pub fn config(&self) -> SimulationV2Config {
        self.config
    }

    /// Creates a simulation from resolved parameters.
    ///
    /// The factor tape is **not** built here. Creation stays cheap and
    /// predictable, and the first peek or advance pays for the tape — which it
    /// would have to be able to rebuild after an eviction anyway.
    ///
    /// # Errors
    ///
    /// Returns [`ChainError::AlreadyExists`] on an id collision, or any storage
    /// failure.
    #[instrument(skip(self, parameters), level = "debug")]
    pub(crate) async fn create(
        &self,
        parameters: SimulationParametersV2,
    ) -> Result<SessionV2, ChainError> {
        let simulation = SessionV2::new(parameters);
        self.store.create(simulation.clone()).await?;

        info!(
            simulation_id = %simulation.id,
            steps = simulation.total_steps,
            seed = simulation.parameters.seed,
            "Created a v2 rolling simulation"
        );
        Ok(simulation)
    }

    /// Reads a simulation's metadata without touching its cursor.
    ///
    /// # Errors
    ///
    /// Returns [`ChainError::NotFound`] when no simulation has that id.
    #[instrument(skip(self), level = "debug")]
    pub(crate) async fn get(&self, id: Uuid) -> Result<SessionV2, ChainError> {
        self.store.get(id).await
    }

    /// Builds the snapshot at the current cursor **without** advancing or
    /// persisting anything.
    ///
    /// Safe and repeatable: the same call returns the same snapshot until an
    /// advance moves the cursor. The only side effect is a warmer cache.
    ///
    /// # Errors
    ///
    /// Returns [`ChainError::NotFound`] for an unknown id,
    /// [`ChainError::SimulatorError`] when the simulation has already served
    /// every step (410 at the boundary, matching v1's exhausted path),
    /// [`ChainError::InvalidState`] for a simulation in the terminal error
    /// state, and whatever the tape or snapshot build surfaces.
    #[instrument(skip(self), level = "debug")]
    pub(crate) async fn peek(&self, id: Uuid) -> Result<(SessionV2, SeriesSnapshot), ChainError> {
        let simulation = self.store.get(id).await?;
        Self::reject_terminal(&simulation, "no current step")?;

        let snapshot = self
            .snapshot_at(&simulation, simulation.current_step)
            .await?;
        Ok((simulation, snapshot))
    }

    /// Serves the snapshot at the current cursor, then advances it exactly
    /// once.
    ///
    /// The advance that serves the last snapshot marks the simulation
    /// `Completed` and drops its cached state: its tape and snapshots can never
    /// be served again, and a re-created simulation would rebuild them
    /// identically anyway.
    ///
    /// # Errors
    ///
    /// As [`SimulationManager::peek`], plus [`ChainError::Conflict`] when a
    /// concurrent advance committed first — the caller re-reads and retries,
    /// and there is deliberately no silent retry loop here.
    #[instrument(skip(self), level = "debug")]
    pub(crate) async fn advance(
        &self,
        id: Uuid,
    ) -> Result<(SessionV2, SeriesSnapshot), ChainError> {
        let mut simulation = self.store.get(id).await?;

        // The revision read here is what the compare-and-swap below commits
        // against, so two concurrent advances that both read this snapshot
        // cannot both persist.
        let expected_version = simulation.version;
        Self::reject_terminal(&simulation, "no further steps")?;

        let snapshot = self
            .snapshot_at(&simulation, simulation.current_step)
            .await?;

        simulation.current_step = simulation
            .current_step
            .checked_add(1)
            .ok_or_else(|| ChainError::Internal("the cursor overflowed".to_string()))?;
        simulation.state = if simulation.is_complete() {
            SessionState::Completed
        } else {
            SessionState::InProgress
        };

        simulation.bump_version()?;
        self.store
            .save_cas(simulation.clone(), expected_version)
            .await?;

        // After the commit, never before: a snapshot is only real once the
        // cursor that served it is durable, and persisting first would leave a
        // row for a step a losing writer never served.
        self.file_snapshot(&simulation, &snapshot);

        if simulation.state == SessionState::Completed {
            self.evict(id);
            debug!(simulation_id = %id, "Simulation completed; cached state evicted");
        }

        Ok((simulation, snapshot))
    }

    /// Queues a served snapshot for filing, if a warehouse is configured.
    ///
    /// **Off the request's clock.** A failure cannot fail the advance — the
    /// cursor has already committed and the client already has its snapshot —
    /// and neither can a slow one delay it: the record goes into a bounded
    /// queue that one writer task drains.
    ///
    /// A full queue **drops** the record with a `WARN` naming the step. That is
    /// the same trade as a failed write, made explicit: the step stays
    /// reproducible, replay rebuilds it, and a retry writes the same rows. What
    /// it costs is a gap, and the honest way to find one is to compare a
    /// simulation's cursor against what `read_range` returns — a log line can be
    /// lost with the process, a missing row cannot.
    ///
    /// Filing is idempotent because both tables sort on
    /// `(simulation, generation, step, …)` and their `ReplacingMergeTree` engine
    /// collapses on that sorting key, so a retry of a step that did land
    /// replaces its rows rather than adding a second copy. The derived
    /// `snapshot_id` rides along as a payload column and is verified on read; it
    /// is not what does the replacing.
    ///
    /// The trade this accepts: a snapshot filed after the response means a
    /// client that advances and immediately queries the warehouse may not find
    /// the step yet. Deterministic replay is the read path that is always
    /// current; the warehouse is the one that is durable.
    fn file_snapshot(&self, simulation: &SessionV2, snapshot: &SeriesSnapshot) {
        let Some(warehouse) = &self.warehouse else {
            return;
        };

        // Decide before building anything. Materialising a record clones every
        // quote, so doing it and *then* discovering the queue is full would put
        // the cost of a degraded warehouse back on the advance — which is the
        // one thing this path exists to avoid.
        let incoming = snapshot_quote_count(snapshot);
        let queued = warehouse.queued_contracts.load(Ordering::SeqCst);
        if warehouse.sender.capacity() == 0
            || queued.saturating_add(incoming) > SNAPSHOT_QUEUE_CONTRACTS
        {
            warn!(
                simulation_id = %simulation.id,
                step = snapshot.step,
                queued,
                "The snapshot queue is full; the step was not filed and can be replayed"
            );
            return;
        }

        let record = snapshot_record(simulation.id, &simulation.parameters.symbol, snapshot);
        warehouse
            .queued_contracts
            .fetch_add(incoming, Ordering::SeqCst);

        if let Err(error) = warehouse.sender.try_send(record) {
            // Lost the race with another advance; undo the reservation.
            warehouse
                .queued_contracts
                .fetch_sub(incoming, Ordering::SeqCst);
            warn!(
                simulation_id = %simulation.id,
                step = snapshot.step,
                error = %error,
                "The snapshot queue is full; the step was not filed and can be replayed"
            );
        }
    }

    /// Deletes a simulation and everything cached for it.
    ///
    /// # Errors
    ///
    /// Returns any storage failure. A missing id is `Ok(false)`, not an error.
    #[instrument(skip(self), level = "debug")]
    pub(crate) async fn delete(&self, id: Uuid) -> Result<bool, ChainError> {
        let deleted = self.store.delete(id).await?;
        // Evict regardless: a delete that found nothing may still be cleaning
        // up after a simulation the store expired on its own.
        self.evict(id);
        self.forget_shared(id).await;
        Ok(deleted)
    }

    /// Expires idle simulations and evicts everything cached for them.
    ///
    /// Returns the ids that went, which is what makes the eviction possible at
    /// all — a count could not tell the caches which entries to drop.
    ///
    /// # Errors
    ///
    /// Returns any storage failure.
    #[instrument(skip(self), level = "debug")]
    pub async fn cleanup(&self) -> Result<Vec<Uuid>, ChainError> {
        let expired = self.store.cleanup().await?;
        for id in &expired {
            self.evict(*id);
            self.forget_shared(*id).await;
        }
        Ok(expired)
    }

    /// The number of factor tapes currently cached.
    #[must_use]
    pub fn cached_tapes(&self) -> usize {
        match self.tapes.lock() {
            Ok(tapes) => tapes.len(),
            Err(poisoned) => poisoned.into_inner().len(),
        }
    }

    /// The number of snapshots currently cached.
    #[must_use]
    pub fn cached_snapshots(&self) -> usize {
        match self.snapshots.lock() {
            Ok(snapshots) => snapshots.len(),
            Err(poisoned) => poisoned.into_inner().len(),
        }
    }

    /// Rejects a simulation that can no longer serve a snapshot.
    ///
    /// `Completed` maps to `410 Gone` at the boundary, matching v1's exhausted
    /// path; the terminal error state maps to `400`.
    fn reject_terminal(simulation: &SessionV2, what: &str) -> Result<(), ChainError> {
        if simulation.state == SessionState::Completed || simulation.is_complete() {
            return Err(ChainError::SimulatorError(format!(
                "simulation completed; {what}"
            )));
        }
        if simulation.state == SessionState::Error {
            return Err(ChainError::InvalidState(
                "simulation is in error state".to_string(),
            ));
        }
        Ok(())
    }

    /// Returns the snapshot at `step`, building whatever is missing.
    ///
    /// Locks are held only for the map operations, never across a build: the
    /// tape and the snapshot are produced outside any critical section, so a
    /// slow build cannot stall another simulation's request.
    async fn snapshot_at(
        &self,
        simulation: &SessionV2,
        step: usize,
    ) -> Result<SeriesSnapshot, ChainError> {
        if let Some(cached) = self.cached_snapshot(simulation.id, step) {
            return Ok(cached);
        }

        let key = (simulation.id, step);

        // Either this call owns the build or it waits on the one already
        // running, decided under the lock so two callers cannot both decide
        // they are the owner. Without this, N concurrent readers of the same
        // cold step each start the same priced build.
        let subscription = {
            let mut builds = match self.snapshot_builds.lock() {
                Ok(builds) => builds,
                Err(poisoned) => poisoned.into_inner(),
            };
            match builds.get(&key) {
                Some(running) => Some(running.subscribe()),
                None => {
                    let (sender, _) = broadcast::channel(1);
                    builds.insert(key, sender);
                    None
                }
            }
        };

        if let Some(mut waiting) = subscription {
            return match waiting.recv().await {
                Ok(Ok(snapshot)) => Ok(snapshot),
                // The owner failed; report what it reported rather than
                // starting a second build that would fail the same way.
                Ok(Err(reason)) => Err(ChainError::Internal(reason)),
                // The owner's task died without publishing. Rare, and the
                // honest answer is to build it here rather than hang.
                Err(_) => self.build_snapshot(simulation, step).await,
            };
        }

        let result = self.build_snapshot(simulation, step).await;

        // Publish, then stop being the owner — in that order, so a caller that
        // subscribes after the removal misses the broadcast, retries, and finds
        // the snapshot in the cache.
        let sender = {
            let mut builds = match self.snapshot_builds.lock() {
                Ok(builds) => builds,
                Err(poisoned) => poisoned.into_inner(),
            };
            builds.remove(&key)
        };
        if let Some(sender) = sender {
            let published = match &result {
                Ok(snapshot) => Ok(snapshot.clone()),
                Err(error) => Err(error.to_string()),
            };
            // An error means nobody was waiting, which is the common case.
            let _ = sender.send(published);
        }

        result
    }

    /// Prices one snapshot, off the runtime and under the shared bound.
    ///
    /// The greek snapshots are built only when something will read them: a
    /// registered warehouse files every step, and a filed step has to carry
    /// what a replayed one does (issue #74). Without a warehouse the API prices
    /// them per request instead, and only when asked.
    ///
    /// Pricing is real synchronous CPU — up to `OCS_MAX_SNAPSHOT_CONTRACTS`
    /// contracts, about 1.54x that again with the greek snapshots on — so it
    /// runs under the same bound the API renderers use rather than a second
    /// one: they compete for the same cores.
    ///
    /// The result is cached **inside** the job, exactly as [`Self::build_tape`]
    /// files a tape. A blocking task cannot be cancelled but awaiting it can,
    /// so a client that disconnects mid-build would otherwise throw away a
    /// snapshot that ran to completion anyway and leave the cache cold for the
    /// next reader.
    async fn build_snapshot(
        &self,
        simulation: &SessionV2,
        step: usize,
    ) -> Result<SeriesSnapshot, ChainError> {
        let tape = self.tape_for(simulation).await?;
        let greek_snapshots = self.warehouse.is_some();
        let parameters = simulation.parameters.clone();
        let snapshots = Arc::clone(&self.snapshots);
        let id = simulation.id;

        crate::utils::admission::admit_blocking(move || {
            let snapshot = SeriesBuilder::new(&parameters, &tape)?
                .with_greek_snapshots(greek_snapshots)
                .snapshot(step)?;
            Self::cache_snapshot_into(&snapshots, id, snapshot.clone());
            Ok(snapshot)
        })
        .await
    }

    /// Reads a cached snapshot, refreshing its recency.
    fn cached_snapshot(&self, id: Uuid, step: usize) -> Option<SeriesSnapshot> {
        let mut snapshots = match self.snapshots.lock() {
            Ok(snapshots) => snapshots,
            Err(poisoned) => poisoned.into_inner(),
        };
        snapshots.get(id, step).cloned()
    }

    /// Stores a built snapshot.
    ///
    /// Static so a blocking job can file its own result without borrowing the
    /// manager; see [`Self::build_snapshot`] for why filing happens there.
    fn cache_snapshot_into(snapshots: &Mutex<SnapshotCache>, id: Uuid, snapshot: SeriesSnapshot) {
        let mut snapshots = match snapshots.lock() {
            Ok(snapshots) => snapshots,
            Err(poisoned) => poisoned.into_inner(),
        };
        snapshots.insert(id, snapshot);
    }

    /// Returns the simulation's factor tape, building it on a miss.
    ///
    /// Built outside the lock — holding the map while it runs would serialise
    /// every other simulation behind it — and off the runtime. `FactorTape::build`
    /// is pure and synchronous, and it is the one place a v2 request does real
    /// CPU work up front: a historical walk estimates a volatility per step,
    /// which at the 10 000-step cap measures over three seconds. Left on a
    /// worker that would stall every other request the worker holds, so it goes
    /// to the blocking pool, exactly as the export path already does with the
    /// same call.
    ///
    /// The result is filed **inside** the blocking task rather than after the
    /// await. A `spawn_blocking` task cannot be cancelled, but awaiting it can:
    /// a client that disconnects or times out mid-build drops this future, and
    /// filing afterwards would throw away a build that ran to completion
    /// anyway. At three seconds a caller retrying under a shorter timeout would
    /// then never warm the cache and would pin a blocking thread on every
    /// attempt.
    async fn tape_for(&self, simulation: &SessionV2) -> Result<FactorTape, ChainError> {
        if let Some(tape) = self.cached_tape(simulation.id) {
            return Ok(tape);
        }

        let id = simulation.id;
        // Computed ONCE. The key serialises every parameter, and a historical
        // simulation carries its prices, so recomputing it per lookup — the
        // waiter below polls twenty times a second — would put a large
        // synchronous serialization on the runtime for every poll.
        let key = tape_key(id, &simulation.parameters);

        // Another instance may already have walked this. Consulted before the
        // build lock rather than after, so a hit costs one round trip instead
        // of queueing behind a build that is about to be redundant.
        if let Some(tape) = self.shared_tape(&key, id).await {
            Self::cache_tape(&self.tapes, self.config.max_cached_tapes, id, tape.clone());
            return Ok(tape);
        }

        // The lookup above is a round trip, and another request in THIS
        // process can finish and cache the tape while it is in flight. Without
        // this recheck that caller would elect itself owner and rebuild a tape
        // that is already warm.
        if let Some(tape) = self.cached_tape(id) {
            return Ok(tape);
        }

        // Either this call owns the build or it waits on the one already
        // running. Decided under the lock, so two callers cannot both decide
        // they are the owner.
        let (mut waiting, owned) = {
            let mut builds = match self.builds.lock() {
                Ok(builds) => builds,
                Err(poisoned) => poisoned.into_inner(),
            };

            match builds.get(&id) {
                Some(running) => (running.subscribe(), None),
                None => {
                    let (sender, receiver) = broadcast::channel(1);
                    builds.insert(id, sender.clone());
                    (receiver, Some(sender))
                }
            }
        };

        // The owner's work runs in a task that OUTLIVES this future, and the
        // owner waits on the same channel as everyone else. A blocking build
        // keeps running when the client that asked for it disconnects, so
        // doing this inline would let a cancelled owner leave its entry in
        // `builds` with nobody to publish it, its deployment-wide claim held
        // until expiry, and its tape unshared — local waiters hanging on a
        // tape that was in fact built.
        if let Some(sender) = owned {
            tokio::spawn(
                OwnedBuild {
                    id,
                    key,
                    parameters: simulation.parameters.clone(),
                    tapes: Arc::clone(&self.tapes),
                    builds: Arc::clone(&self.builds),
                    shared: self.shared_tapes.clone(),
                    max_cached_tapes: self.config.max_cached_tapes,
                    shared_build_wait: self.shared_build_wait,
                }
                .run(sender),
            );
        }

        match waiting.recv().await {
            Ok(Ok(tape)) => Ok(tape),
            // The owner failed; report what it reported rather than
            // starting a second build that would fail the same way.
            Ok(Err(reason)) => Err(ChainError::Internal(reason)),
            // The owner's task died without publishing. Rare, and the
            // honest answer is to build it here rather than hang.
            Err(_) => self.build_tape(simulation).await,
        }
    }

    /// Builds a tape off the runtime and files it.
    ///
    /// `FactorTape::build` is pure and synchronous, and it is the one place a
    /// v2 request does real CPU work up front: a historical walk estimates a
    /// volatility per step, which at the 10 000-step cap measures over three
    /// seconds. Left on a worker it would stall every other request that worker
    /// holds, so it goes to the blocking pool, exactly as the export path
    /// already does with the same call.
    ///
    /// The result is filed **inside** the blocking task rather than after the
    /// await. A `spawn_blocking` task cannot be cancelled, but awaiting it can:
    /// a client that disconnects or times out mid-build drops that future, and
    /// filing afterwards would throw away a build that ran to completion
    /// anyway.
    async fn build_tape(&self, simulation: &SessionV2) -> Result<FactorTape, ChainError> {
        Self::build_tape_into(
            simulation.parameters.clone(),
            simulation.id,
            Arc::clone(&self.tapes),
            self.config.max_cached_tapes,
        )
        .await
    }

    /// As [`Self::build_tape`], without borrowing the manager.
    ///
    /// Owned so the build can run in a task that outlives the request that
    /// asked for it; see [`OwnedBuild`].
    async fn build_tape_into(
        parameters: SimulationParametersV2,
        id: Uuid,
        tapes: Arc<Mutex<HashMap<Uuid, TapeEntry>>>,
        max_cached_tapes: usize,
    ) -> Result<FactorTape, ChainError> {
        tokio::task::spawn_blocking(move || {
            let tape = FactorTape::build(&parameters, &parameters.method)?;
            Self::cache_tape(&tapes, max_cached_tapes, id, tape.clone());
            Ok(tape)
        })
        .await
        .map_err(|e| ChainError::Internal(format!("the factor tape build did not finish: {e}")))?
    }

    /// The tape another instance left, decoded, or `None`.
    ///
    /// Takes the key rather than the simulation because computing it
    /// serialises every parameter, which is expensive enough that the caller
    /// does it once and reuses it.
    async fn shared_tape(&self, key: &str, id: Uuid) -> Option<FactorTape> {
        let shared = self.shared_tapes.as_ref()?;
        Self::shared_tape_from(shared.as_ref(), key, id).await
    }

    /// As [`Self::shared_tape`], against a cache the caller already has.
    ///
    /// A stored document this build cannot decode is a miss, not an error: the
    /// key carries the snapshot generation and a fingerprint of the
    /// parameters, so it should not happen, and rebuilding is the answer that
    /// still serves the request.
    async fn shared_tape_from(
        shared: &dyn SharedTapeCache,
        key: &str,
        id: Uuid,
    ) -> Option<FactorTape> {
        let encoded = shared.get(key).await?;

        match serde_json::from_str::<FactorTape>(&encoded) {
            Ok(tape) => Some(tape),
            Err(error) => {
                warn!(
                    %error,
                    simulation_id = %id,
                    "a shared tape did not decode; rebuilding it"
                );
                None
            }
        }
    }

    /// Leaves a built tape where the other instances can find it.
    async fn share_tape_to(shared: &dyn SharedTapeCache, key: &str, id: Uuid, tape: &FactorTape) {
        match serde_json::to_string(tape) {
            Ok(encoded) => shared.put(key, &encoded).await,
            Err(error) => warn!(
                %error,
                simulation_id = %id,
                "a built tape did not encode; it stays local to this instance"
            ),
        }
    }

    /// Reads a cached tape, refreshing its recency.
    fn cached_tape(&self, id: Uuid) -> Option<FactorTape> {
        let mut tapes = match self.tapes.lock() {
            Ok(tapes) => tapes,
            Err(poisoned) => poisoned.into_inner(),
        };
        let entry = tapes.get_mut(&id)?;
        entry.last_access = Instant::now();
        Some(entry.tape.clone())
    }

    /// Stores a built tape, evicting the least recently used first.
    ///
    /// Takes the map rather than `&self` so the builder can file its result
    /// from inside the blocking task, where no caller can drop it.
    ///
    /// One race that follows from filing there, recorded because it is benign
    /// only under the current routes: a build already running when the
    /// simulation is deleted, completed or reaped will file afterwards, leaving
    /// a tape for an id the store no longer knows. Nothing can serve it — every
    /// path reads the store before the cache — so it costs memory until the LRU
    /// pushes it out. It would stop being benign the day v2 gains a route that
    /// changes a simulation's parameters in place, because the stale tape would
    /// then be a tape of the *old* parameters under a live id.
    fn cache_tape(
        tapes: &Mutex<HashMap<Uuid, TapeEntry>>,
        max_cached_tapes: usize,
        id: Uuid,
        tape: FactorTape,
    ) {
        let mut tapes = match tapes.lock() {
            Ok(tapes) => tapes,
            Err(poisoned) => poisoned.into_inner(),
        };

        tapes.remove(&id);
        // The capacity is validated `>= 1` when the configuration loads, so
        // `- 1` cannot underflow. Evicting before the insert keeps the id being
        // inserted out of the running for victim.
        let max = max_cached_tapes;
        debug_assert!(
            max >= 1,
            "the configured capacity is validated >= 1 at load"
        );
        while tapes.len() > max - 1 {
            let victim = tapes
                .iter()
                .min_by_key(|(_, entry)| entry.last_access)
                .map(|(id, _)| *id);
            match victim {
                Some(victim) => {
                    tapes.remove(&victim);
                }
                None => break,
            }
        }

        tapes.insert(
            id,
            TapeEntry {
                tape,
                last_access: Instant::now(),
            },
        );
    }

    /// Drops everything cached for a simulation.
    /// Drops what this simulation left in the shared cache.
    ///
    /// Separate from [`SimulationManager::evict`] because it is I/O and that
    /// is not: the local eviction must stay callable from anywhere, including
    /// a blocking task.
    async fn forget_shared(&self, id: Uuid) {
        if let Some(shared) = self.shared_tapes.as_ref() {
            shared.forget_simulation(&id.to_string()).await;
        }
    }

    fn evict(&self, id: Uuid) {
        match self.tapes.lock() {
            Ok(mut tapes) => {
                tapes.remove(&id);
            }
            Err(poisoned) => {
                poisoned.into_inner().remove(&id);
            }
        }
        match self.snapshots.lock() {
            Ok(mut snapshots) => {
                snapshots.evict_simulation(id);
            }
            Err(poisoned) => {
                poisoned.into_inner().evict_simulation(id);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::rest::models::{ApiTimeFrame, ApiWalkType};
    use crate::api::rest::requests_v2::CreateSimulationRequest;
    use crate::infrastructure::{ContractQuote, ContractSeriesQuery, SnapshotRecord};
    use crate::session::store::InMemorySimulationStore;
    use crate::session::{ExpiryRule, ExpiryRuleKind};
    use chrono::{TimeZone, Utc, Weekday};

    fn request(steps: usize) -> CreateSimulationRequest {
        let rules = vec![
            match ExpiryRule::new("zero_dte", ExpiryRuleKind::Daily, 1) {
                Ok(rule) => rule,
                Err(error) => panic!("the test rule must be valid: {error}"),
            },
            match ExpiryRule::new(
                "weeklies",
                ExpiryRuleKind::weekly([Weekday::Mon, Weekday::Fri]),
                2,
            ) {
                Ok(rule) => rule,
                Err(error) => panic!("the test rule must be valid: {error}"),
            },
        ];
        let start_at = match Utc.with_ymd_and_hms(2026, 1, 5, 14, 30, 0).single() {
            Some(instant) => instant,
            None => panic!("the test instant must be valid"),
        };

        CreateSimulationRequest {
            symbol: "SPX".to_string(),
            steps,
            start_at: Some(start_at),
            step_interval_seconds: Some(86_400),
            timezone: "America/New_York".to_string(),
            calendar: None,
            expiration_time: "17:00".to_string(),
            schedules: rules,
            initial_price: 5000.0,
            volatility: 0.18,
            risk_free_rate: 0.04,
            dividend_yield: 0.0,
            method: ApiWalkType::Brownian {
                dt: 1.0 / 252.0,
                drift: 0.0,
                volatility: 0.18,
            },
            time_frame: ApiTimeFrame::Day,
            chain_size: Some(3),
            strike_interval: Some(25.0),
            skew_slope: None,
            smile_curve: None,
            spread: Some(0.02),
            strike_ladder: Default::default(),
            spread_proportional: None,
            spread_moneyness_widening: None,
            spread_tenor_widening: None,
            spread_tick: None,
            seed: Some(42),
        }
    }

    fn parameters(steps: usize) -> SimulationParametersV2 {
        match SimulationParametersV2::try_from(request(steps)) {
            Ok(parameters) => parameters,
            Err(error) => panic!("the request must convert: {error}"),
        }
    }

    fn manager() -> SimulationManager {
        SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
    }

    /// A shared tape cache in memory, standing in for Redis, that counts what
    /// it was asked to do.
    ///
    /// The counts are the point: a manager that BUILT a tape always writes one,
    /// so "no write" is how a test sees that the second instance did not
    /// rebuild.
    #[derive(Default)]
    struct SharedTapes {
        entries: Mutex<HashMap<String, String>>,
        /// The keys some instance has claimed the right to build.
        building: Mutex<std::collections::HashSet<String>>,
        reads: AtomicUsize,
        writes: AtomicUsize,
        claims: AtomicUsize,
    }

    impl SharedTapes {
        fn writes(&self) -> usize {
            self.writes.load(Ordering::SeqCst)
        }

        fn reads(&self) -> usize {
            self.reads.load(Ordering::SeqCst)
        }

        fn claims(&self) -> usize {
            self.claims.load(Ordering::SeqCst)
        }

        fn keys(&self) -> Vec<String> {
            match self.entries.lock() {
                Ok(entries) => entries.keys().cloned().collect(),
                Err(poisoned) => poisoned.into_inner().keys().cloned().collect(),
            }
        }
    }

    #[async_trait::async_trait]
    impl SharedTapeCache for SharedTapes {
        async fn get(&self, key: &str) -> Option<String> {
            self.reads.fetch_add(1, Ordering::SeqCst);
            match self.entries.lock() {
                Ok(entries) => entries.get(key).cloned(),
                Err(poisoned) => poisoned.into_inner().get(key).cloned(),
            }
        }

        async fn put(&self, key: &str, encoded: &str) {
            self.writes.fetch_add(1, Ordering::SeqCst);
            match self.entries.lock() {
                Ok(mut entries) => entries.insert(key.to_string(), encoded.to_string()),
                Err(poisoned) => poisoned
                    .into_inner()
                    .insert(key.to_string(), encoded.to_string()),
            };
        }

        async fn forget_simulation(&self, id: &str) {
            let suffix = format!(":{id}");
            match self.entries.lock() {
                Ok(mut entries) => entries.retain(|key, _| !key.ends_with(&suffix)),
                Err(poisoned) => poisoned
                    .into_inner()
                    .retain(|key, _| !key.ends_with(&suffix)),
            }
        }

        async fn claim_build(&self, key: &str) -> BuildClaim {
            self.claims.fetch_add(1, Ordering::SeqCst);
            let mut held = match self.building.lock() {
                Ok(held) => held,
                Err(poisoned) => poisoned.into_inner(),
            };
            if held.insert(key.to_string()) {
                BuildClaim::Held(format!("token:{key}"))
            } else {
                BuildClaim::Taken
            }
        }

        async fn release_build(&self, key: &str, token: &str) {
            let mut held = match self.building.lock() {
                Ok(held) => held,
                Err(poisoned) => poisoned.into_inner(),
            };
            // Ownership-safe like the Redis script: a token that is not this
            // claim's releases nothing.
            if token == format!("token:{key}") {
                held.remove(key);
            }
        }
    }

    /// A shared cache that is always down, which is what an unreachable Redis
    /// looks like from here.
    struct BrokenTapes;

    #[async_trait::async_trait]
    impl SharedTapeCache for BrokenTapes {
        async fn get(&self, _key: &str) -> Option<String> {
            None
        }

        async fn put(&self, _key: &str, _encoded: &str) {}

        async fn forget_simulation(&self, _id: &str) {}

        /// A gate that cannot be reached must let the caller build, or an
        /// unreachable Redis becomes an unanswerable request.
        async fn claim_build(&self, _key: &str) -> BuildClaim {
            BuildClaim::Unclaimed
        }

        async fn release_build(&self, _key: &str, _token: &str) {}
    }

    /// A warehouse that records what it was asked to file, and can be told to
    /// fail — the two behaviours the wiring promises something about.
    #[derive(Default)]
    struct RecordingWarehouse {
        filed: Mutex<Vec<SnapshotRecord>>,
        fail: bool,
    }

    impl RecordingWarehouse {
        fn failing() -> Self {
            Self {
                filed: Mutex::new(Vec::new()),
                fail: true,
            }
        }

        /// The records it was handed, whole — so a test can assert on what is
        /// inside one, not just that one arrived.
        fn records(&self) -> Vec<SnapshotRecord> {
            match self.filed.lock() {
                Ok(filed) => filed.clone(),
                Err(poisoned) => poisoned.into_inner().clone(),
            }
        }

        fn filed(&self) -> Vec<(Uuid, usize)> {
            self.records()
                .iter()
                .map(|record| (record.simulation, record.step))
                .collect()
        }
    }

    #[async_trait::async_trait]
    impl SimulationSnapshotRepository for RecordingWarehouse {
        /// No server behind it; reachable exactly as long as the process is.
        async fn ping(&self) -> Result<(), ChainError> {
            Ok(())
        }

        async fn persist(&self, record: SnapshotRecord) -> Result<(), ChainError> {
            if self.fail {
                return Err(ChainError::Internal("the warehouse is down".to_string()));
            }
            match self.filed.lock() {
                Ok(mut filed) => filed.push(record),
                Err(poisoned) => poisoned.into_inner().push(record),
            }
            Ok(())
        }

        async fn get(
            &self,
            _simulation: Uuid,
            _generation: u64,
            _step: usize,
        ) -> Result<Option<SnapshotRecord>, ChainError> {
            Ok(None)
        }

        async fn read_range(
            &self,
            _simulation: Uuid,
            _generation: u64,
            _from_step: usize,
            _to_step: usize,
        ) -> Result<Vec<SnapshotRecord>, ChainError> {
            Ok(Vec::new())
        }

        async fn contract_series(
            &self,
            _query: ContractSeriesQuery,
        ) -> Result<Vec<ContractQuote>, ChainError> {
            Ok(Vec::new())
        }
    }

    /// A warehouse whose first write never completes — the shape a degraded
    /// deployment has, and the one an unbounded queue cannot survive.
    #[derive(Default)]
    struct StallingWarehouse {
        started: AtomicUsize,
    }

    impl StallingWarehouse {
        fn started(&self) -> usize {
            self.started.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl SimulationSnapshotRepository for StallingWarehouse {
        /// No server behind it; reachable exactly as long as the process is.
        async fn ping(&self) -> Result<(), ChainError> {
            Ok(())
        }

        async fn persist(&self, _record: SnapshotRecord) -> Result<(), ChainError> {
            self.started.fetch_add(1, Ordering::SeqCst);
            std::future::pending::<()>().await;
            Ok(())
        }

        async fn get(
            &self,
            _simulation: Uuid,
            _generation: u64,
            _step: usize,
        ) -> Result<Option<SnapshotRecord>, ChainError> {
            Ok(None)
        }

        async fn read_range(
            &self,
            _simulation: Uuid,
            _generation: u64,
            _from_step: usize,
            _to_step: usize,
        ) -> Result<Vec<SnapshotRecord>, ChainError> {
            Ok(Vec::new())
        }

        async fn contract_series(
            &self,
            _query: ContractSeriesQuery,
        ) -> Result<Vec<ContractQuote>, ChainError> {
            Ok(Vec::new())
        }
    }

    /// Filing is detached, so a test has to let the spawned write run before it
    /// can observe it. One yield is enough on the current-thread runtime the
    /// tests use; the loop keeps it from being a race on a busier one.
    async fn settle() {
        for _ in 0..16 {
            tokio::task::yield_now().await;
        }
    }

    /// The simulation id is not an input to anything seeded.
    ///
    /// This is what the switch to random ids rests on. Two simulations built
    /// from one set of parameters have different ids and must still produce the
    /// same snapshots, strike for strike — `SeriesSnapshot`'s equality compares
    /// premiums, Greeks and the underlying price, not lengths. If the id ever
    /// leaked into the tape, the planner or the chain build, this fails.
    #[tokio::test]
    async fn test_the_simulation_id_does_not_reach_the_tape() {
        let manager = manager();

        let first = created(&manager, 3).await;
        let second = created(&manager, 3).await;
        assert_ne!(first.id, second.id, "ids are random, so two differ");
        assert_eq!(
            first.parameters.seed, second.parameters.seed,
            "the fixture must pin the seed, or this proves nothing"
        );

        for _ in 0..3 {
            let left = match manager.advance(first.id).await {
                Ok((_, snapshot)) => snapshot,
                Err(error) => panic!("the first simulation must advance: {error}"),
            };
            let right = match manager.advance(second.id).await {
                Ok((_, snapshot)) => snapshot,
                Err(error) => panic!("the second simulation must advance: {error}"),
            };

            assert_eq!(
                left, right,
                "step {} differs between two simulations that share every parameter",
                left.step
            );
        }
    }

    /// Concurrent readers of one cold STEP share one priced build.
    ///
    /// The tape has its own single-flight; the snapshot needs the same and for
    /// a sharper reason since issue #74. With a warehouse registered a snapshot
    /// build prices up to the per-snapshot cap with both greek sets on, so N
    /// concurrent readers of the same step used to commit the machine to N
    /// copies of the same seconds-long job.
    ///
    /// Every reader must get the same snapshot, one entry must be cached, and
    /// the owner must have cleaned up after itself — a stale entry in the
    /// in-flight map would make the NEXT reader of that step wait forever on a
    /// broadcast nobody will send.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn test_concurrent_readers_of_one_step_share_one_snapshot_build() {
        let manager = Arc::new(manager().with_warehouse(
            Arc::new(RecordingWarehouse::default()) as Arc<dyn SimulationSnapshotRepository>
        ));
        let simulation = created(&manager, 3).await;

        let mut readers = Vec::new();
        for _ in 0..8 {
            let manager = Arc::clone(&manager);
            let id = simulation.id;
            readers.push(tokio::spawn(async move { manager.peek(id).await }));
        }

        let mut snapshots = Vec::new();
        for reader in readers {
            match reader.await {
                Ok(Ok((_, snapshot))) => snapshots.push(snapshot),
                Ok(Err(error)) => panic!("every reader must be served: {error}"),
                Err(error) => panic!("a reader panicked: {error}"),
            }
        }

        assert_eq!(snapshots.len(), 8);
        for snapshot in &snapshots {
            assert_eq!(
                snapshot, &snapshots[0],
                "every reader must see the same snapshot"
            );
        }
        assert_eq!(
            manager.cached_snapshots(),
            1,
            "eight readers of one step must leave one snapshot"
        );
        assert!(
            match manager.snapshot_builds.lock() {
                Ok(builds) => builds.is_empty(),
                Err(poisoned) => poisoned.into_inner().is_empty(),
            },
            "the owner must stop being the owner once it has published"
        );
    }

    /// Concurrent first reads of one simulation share a single build.
    ///
    /// The build is the one place a v2 request does seconds of CPU, so N
    /// concurrent peeks starting N identical builds is not wasted allocation,
    /// it is the machine. What proves the sharing is the snapshots: every
    /// caller gets the same tape, and only one entry is cached.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn test_concurrent_first_reads_share_one_build() {
        let manager = Arc::new(manager());
        let simulation = created(&manager, 3).await;

        let mut readers = Vec::new();
        for _ in 0..8 {
            let manager = Arc::clone(&manager);
            let id = simulation.id;
            readers.push(tokio::spawn(async move { manager.peek(id).await }));
        }

        let mut snapshots = Vec::new();
        for reader in readers {
            match reader.await {
                Ok(Ok((_, snapshot))) => snapshots.push(snapshot),
                Ok(Err(error)) => panic!("every reader must be served: {error}"),
                Err(error) => panic!("a reader panicked: {error}"),
            }
        }

        assert_eq!(snapshots.len(), 8);
        for snapshot in &snapshots {
            assert_eq!(
                snapshot, &snapshots[0],
                "every reader must see the same tape"
            );
        }
        assert_eq!(
            manager.cached_tapes(),
            1,
            "eight readers of one simulation must leave one tape"
        );
    }

    /// An advance files exactly the step it served.
    #[tokio::test]
    async fn test_an_advance_files_the_step_it_served() {
        let warehouse = Arc::new(RecordingWarehouse::default());
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(Arc::clone(&warehouse) as Arc<dyn SimulationSnapshotRepository>);

        let simulation = created(&manager, 3).await;
        match manager.advance(simulation.id).await {
            Ok(_) => {}
            Err(error) => panic!("the advance must serve: {error}"),
        }
        settle().await;

        assert_eq!(
            warehouse.filed(),
            vec![(simulation.id, 0)],
            "the step the advance served is the step that is filed"
        );
    }

    /// A registered warehouse is what turns the greek snapshots on.
    ///
    /// The wiring that makes issue #74 work: `SeriesBuilder` builds them only
    /// when asked, and the manager asks exactly when there is a warehouse to
    /// file them into. A filed record with empty snapshots would persist a tape
    /// strictly poorer than a replayed one — the asymmetry the issue removes —
    /// and every existing test would still pass, because none of them looks
    /// inside a filed record.
    #[tokio::test]
    async fn test_a_registered_warehouse_files_the_greeks() {
        let warehouse = Arc::new(RecordingWarehouse::default());
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(Arc::clone(&warehouse) as Arc<dyn SimulationSnapshotRepository>);

        let simulation = created(&manager, 3).await;
        match manager.advance(simulation.id).await {
            Ok(_) => {}
            Err(error) => panic!("the advance must serve: {error}"),
        }
        settle().await;

        let records = warehouse.records();
        let record = match records.first() {
            Some(record) => record,
            None => panic!("the advance must file a record"),
        };
        let quotes: Vec<_> = record
            .expirations
            .iter()
            .flat_map(|expiration| expiration.quotes.iter())
            .collect();
        assert!(!quotes.is_empty(), "the filed record must carry quotes");
        assert!(
            quotes
                .iter()
                .all(|quote| quote.greeks_call.is_some() && quote.greeks_put.is_some()),
            "every filed quote must carry both snapshots"
        );
    }

    /// A registered warehouse does not change the tape.
    ///
    /// The flag it turns on selects a different upstream branch inside
    /// `OptionChain::build_chain`, so two deployments of the same build, same
    /// parameters and same seed take two different code paths to the same
    /// market. If they ever disagreed, whether a step was filed would change
    /// what a client was served — the worst regression this service can have,
    /// and one no other test would see, because every other comparison is
    /// between two managers configured the same way.
    #[tokio::test]
    async fn test_a_warehouse_does_not_change_the_served_market() {
        let filing = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(
            Arc::new(RecordingWarehouse::default()) as Arc<dyn SimulationSnapshotRepository>
        );
        let plain = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        );

        let served = async |manager: &SimulationManager| {
            let simulation = created(manager, 3).await;
            match manager.peek(simulation.id).await {
                Ok((_, snapshot)) => snapshot,
                Err(error) => panic!("the peek must serve: {error}"),
            }
        };
        let with_warehouse = served(&filing).await;
        let without = served(&plain).await;

        assert_eq!(
            with_warehouse.spot, without.spot,
            "the seeded price path must not depend on the warehouse"
        );
        assert_eq!(with_warehouse.base_volatility, without.base_volatility);
        assert_eq!(with_warehouse.chains.len(), without.chains.len());

        // Every priced value, strike by strike. `PartialEq` on `ExpiryChain`
        // compares the whole chain including the greek snapshots, which DO
        // differ by design, so the comparison is over what is served.
        //
        // Counted, not just zipped: a `zip` over two chains of different
        // lengths truncates silently, and over two EMPTY ones it asserts
        // nothing at all — which is the outcome at the low volatilities where a
        // chain serves no valid strike.
        let mut compared = 0_usize;
        for (filed, replayed) in with_warehouse.chains.iter().zip(without.chains.iter()) {
            assert_eq!(filed.expires_at, replayed.expires_at);
            assert_eq!(filed.days_to_expiration, replayed.days_to_expiration);
            assert_eq!(
                filed.chain.iter().count(),
                replayed.chain.iter().count(),
                "the two deployments must quote the same strikes"
            );
            for (left, right) in filed.chain.iter().zip(replayed.chain.iter()) {
                compared += 1;
                assert_eq!(left.strike_price, right.strike_price);
                assert_eq!(left.implied_volatility, right.implied_volatility);
                assert_eq!(left.call_bid, right.call_bid);
                assert_eq!(left.call_ask, right.call_ask);
                assert_eq!(left.call_middle, right.call_middle);
                assert_eq!(left.put_bid, right.put_bid);
                assert_eq!(left.put_ask, right.put_ask);
                assert_eq!(left.put_middle, right.put_middle);
                assert_eq!(left.delta_call, right.delta_call);
                assert_eq!(left.delta_put, right.delta_put);
                assert_eq!(left.gamma, right.gamma);
            }
        }
        assert!(compared > 0, "the fixture must actually quote something");

        // And the one thing that IS meant to differ.
        assert!(
            with_warehouse
                .chains
                .iter()
                .flat_map(|chain| chain.chain.iter())
                .all(|data| data.greeks_call.is_some())
        );
        assert!(
            without
                .chains
                .iter()
                .flat_map(|chain| chain.chain.iter())
                .all(|data| data.greeks_call.is_none())
        );
    }

    /// Without a warehouse nothing pays for the greeks.
    ///
    /// The other half of the same wiring: a deployment that files nothing and
    /// whose clients never ask must not be charged about 1.5x a chain build on
    /// every advance. When a client does ask, the API prices them per request
    /// instead.
    #[tokio::test]
    async fn test_a_manager_without_a_warehouse_does_not_price_the_greeks() {
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        );

        let simulation = created(&manager, 3).await;
        let (_, snapshot) = match manager.peek(simulation.id).await {
            Ok(served) => served,
            Err(error) => panic!("the peek must serve: {error}"),
        };

        let contracts: Vec<_> = snapshot
            .chains
            .iter()
            .flat_map(|chain| chain.chain.iter())
            .collect();
        assert!(!contracts.is_empty(), "the snapshot must quote something");
        assert!(
            contracts
                .iter()
                .all(|data| data.greeks_call.is_none() && data.greeks_put.is_none()),
            "no snapshot should have been priced"
        );
    }

    /// A warehouse that never drains stops receiving, rather than accumulating
    /// records until the process dies.
    ///
    /// The bound that matters is rows, not records: a record is a few hundred
    /// quotes in this fixture and up to the per-snapshot cap in a large
    /// configuration, so a depth in records says nothing about what is
    /// resident.
    #[tokio::test]
    async fn test_a_stalled_warehouse_stops_being_queued() {
        let warehouse = Arc::new(StallingWarehouse::default());
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(Arc::clone(&warehouse) as Arc<dyn SimulationSnapshotRepository>);

        // More advances than the queue can hold, against a warehouse whose
        // first write never returns.
        for _ in 0..(SNAPSHOT_QUEUE_DEPTH + 8) {
            let simulation = created(&manager, 2).await;
            match manager.advance(simulation.id).await {
                Ok(_) => {}
                Err(error) => panic!("the advance must serve regardless: {error}"),
            }
        }
        settle().await;

        assert!(
            warehouse.started() <= SNAPSHOT_QUEUE_DEPTH + 1,
            "a stalled warehouse must stop receiving, got {} starts",
            warehouse.started()
        );
    }

    /// A warehouse that is down does not fail the advance. This is the whole
    /// point of filing after the commit and off the request's clock.
    #[tokio::test]
    async fn test_a_failing_warehouse_does_not_fail_the_advance() {
        let warehouse = Arc::new(RecordingWarehouse::failing());
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(warehouse as Arc<dyn SimulationSnapshotRepository>);

        let simulation = created(&manager, 3).await;

        match manager.advance(simulation.id).await {
            Ok((advanced, _)) => assert_eq!(advanced.current_step, 1, "the cursor still moved"),
            Err(error) => panic!("a warehouse failure must not fail the advance: {error}"),
        }
        settle().await;
    }

    /// A peek serves a snapshot and files nothing: it moves no cursor, so there
    /// is no step to file.
    #[tokio::test]
    async fn test_a_peek_files_nothing() {
        let warehouse = Arc::new(RecordingWarehouse::default());
        let manager = SimulationManager::new(
            Arc::new(InMemorySimulationStore::new()),
            SimulationV2Config::default(),
        )
        .with_warehouse(Arc::clone(&warehouse) as Arc<dyn SimulationSnapshotRepository>);

        let simulation = created(&manager, 3).await;
        match manager.peek(simulation.id).await {
            Ok(_) => {}
            Err(error) => panic!("the peek must serve: {error}"),
        }
        settle().await;

        assert!(warehouse.filed().is_empty(), "a peek persists nothing");
    }

    /// Without a warehouse the serving path is unchanged — there is nothing to
    /// call and nothing to fail.
    #[tokio::test]
    async fn test_a_manager_without_a_warehouse_serves_normally() {
        let manager = manager();
        let simulation = created(&manager, 2).await;

        match manager.advance(simulation.id).await {
            Ok((advanced, snapshot)) => {
                assert_eq!(advanced.current_step, 1);
                assert_eq!(snapshot.step, 0);
            }
            Err(error) => panic!("the advance must serve: {error}"),
        }
    }

    async fn created(manager: &SimulationManager, steps: usize) -> SessionV2 {
        match manager.create(parameters(steps)).await {
            Ok(simulation) => simulation,
            Err(error) => panic!("the simulation must be created: {error}"),
        }
    }

    /// A created simulation starts at cursor zero and is readable back.
    #[tokio::test]
    async fn test_create_then_get_returns_the_simulation() {
        let manager = manager();
        let created = created(&manager, 5).await;

        match manager.get(created.id).await {
            Ok(loaded) => {
                assert_eq!(loaded, created);
                assert_eq!(loaded.current_step, 0);
                assert_eq!(loaded.state, SessionState::Initialized);
            }
            Err(error) => panic!("the simulation must load: {error}"),
        }
    }

    /// Creation does not build the factor tape: it stays cheap and predictable,
    /// and the first peek pays for it.
    #[tokio::test]
    async fn test_creation_does_not_build_the_tape() {
        let manager = manager();
        let created = created(&manager, 5).await;

        assert_eq!(manager.cached_tapes(), 0);

        match manager.peek(created.id).await {
            Ok(_) => assert_eq!(manager.cached_tapes(), 1),
            Err(error) => panic!("the peek must succeed: {error}"),
        }
    }

    /// The tape cache still honours the configured capacity now that the build
    /// files its own result from inside the blocking task and the cap travels
    /// as a parameter rather than through `&self`.
    #[tokio::test]
    async fn test_the_tape_cache_still_honours_its_capacity() {
        let config = SimulationV2Config {
            max_cached_tapes: 2,
            ..SimulationV2Config::default()
        };
        let manager = SimulationManager::new(Arc::new(InMemorySimulationStore::new()), config);

        for _ in 0..4 {
            let created = created(&manager, 5).await;
            if let Err(error) = manager.peek(created.id).await {
                panic!("the peek must succeed: {error}");
            }
        }

        assert_eq!(
            manager.cached_tapes(),
            2,
            "four tapes were built under a cap of two"
        );
    }

    /// A peek is repeatable and changes nothing.
    #[tokio::test]
    async fn test_a_peek_is_repeatable_and_changes_nothing() {
        let manager = manager();
        let created = created(&manager, 5).await;

        let first = match manager.peek(created.id).await {
            Ok((_, snapshot)) => snapshot,
            Err(error) => panic!("the peek must succeed: {error}"),
        };
        let second = match manager.peek(created.id).await {
            Ok((_, snapshot)) => snapshot,
            Err(error) => panic!("the peek must succeed: {error}"),
        };

        assert_eq!(first, second);
        match manager.get(created.id).await {
            Ok(loaded) => {
                assert_eq!(loaded.current_step, 0, "a peek must not move the cursor");
                assert_eq!(loaded.version, created.version, "a peek must not persist");
                assert_eq!(loaded.state, SessionState::Initialized);
            }
            Err(error) => panic!("the simulation must load: {error}"),
        }
    }

    /// An advance serves the current snapshot and then moves the cursor.
    #[tokio::test]
    async fn test_an_advance_serves_then_advances() {
        let manager = manager();
        let created = created(&manager, 5).await;

        let peeked = match manager.peek(created.id).await {
            Ok((_, snapshot)) => snapshot,
            Err(error) => panic!("the peek must succeed: {error}"),
        };
        let (advanced, served) = match manager.advance(created.id).await {
            Ok(result) => result,
            Err(error) => panic!("the advance must succeed: {error}"),
        };

        assert_eq!(
            served, peeked,
            "the advance must serve the snapshot the peek showed"
        );
        assert_eq!(advanced.current_step, 1);
        assert_eq!(advanced.state, SessionState::InProgress);
    }

    /// Walking a simulation serves indices 0..N-1 and then completes.
    #[tokio::test]
    async fn test_walking_serves_every_index_once_then_completes() {
        let manager = manager();
        let created = created(&manager, 3).await;

        let mut served = Vec::new();
        for _ in 0..3 {
            match manager.advance(created.id).await {
                Ok((_, snapshot)) => served.push(snapshot.step),
                Err(error) => panic!("the advance must succeed: {error}"),
            }
        }
        assert_eq!(served, vec![0, 1, 2]);

        match manager.get(created.id).await {
            Ok(loaded) => assert_eq!(loaded.state, SessionState::Completed),
            Err(error) => panic!("the simulation must load: {error}"),
        }

        // A completed simulation has nothing left to serve, on either path.
        match manager.advance(created.id).await {
            Err(ChainError::SimulatorError(message)) => assert!(message.contains("completed")),
            other => panic!("expected the exhausted path, got {other:?}"),
        }
        match manager.peek(created.id).await {
            Err(ChainError::SimulatorError(message)) => assert!(message.contains("completed")),
            other => panic!("expected the exhausted path, got {other:?}"),
        }
    }

    /// Completing a simulation drops everything cached for it.
    #[tokio::test]
    async fn test_completion_evicts_the_cached_state() {
        let manager = manager();
        let created = created(&manager, 1).await;

        match manager.advance(created.id).await {
            Ok(_) => {}
            Err(error) => panic!("the advance must succeed: {error}"),
        }

        assert_eq!(manager.cached_tapes(), 0);
        assert_eq!(manager.cached_snapshots(), 0);
    }

    /// Two advances that read the same revision produce one winner.
    #[tokio::test]
    async fn test_a_lost_race_is_a_conflict() {
        let store = Arc::new(InMemorySimulationStore::new());
        let manager = SimulationManager::new(store.clone(), SimulationV2Config::default());
        let created = created(&manager, 5).await;

        // Advance once through the manager, then replay an advance built from
        // the pre-advance revision — exactly what a concurrent caller holds.
        match manager.advance(created.id).await {
            Ok(_) => {}
            Err(error) => panic!("the first advance must succeed: {error}"),
        }

        // The mutation a concurrent caller would hold: it read the simulation
        // before the advance, so it carries the pre-advance revision but an
        // otherwise valid post-advance state.
        let mut stale = created.clone();
        stale.current_step = 1;
        stale.state = SessionState::InProgress;
        let expected = match stale.bump_version() {
            Ok(expected) => expected,
            Err(error) => panic!("must bump: {error}"),
        };
        match store.save_cas(stale, expected).await {
            Err(ChainError::Conflict(_)) => {}
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    /// Deleting removes the simulation and its cached state.
    #[tokio::test]
    async fn test_delete_removes_the_simulation_and_its_caches() {
        let manager = manager();
        let created = created(&manager, 5).await;
        match manager.peek(created.id).await {
            Ok(_) => {}
            Err(error) => panic!("the peek must succeed: {error}"),
        }
        assert_eq!(manager.cached_tapes(), 1);

        match manager.delete(created.id).await {
            Ok(deleted) => assert!(deleted),
            Err(error) => panic!("the delete must succeed: {error}"),
        }

        assert_eq!(manager.cached_tapes(), 0);
        assert_eq!(manager.cached_snapshots(), 0);
        assert!(manager.get(created.id).await.is_err());
    }

    /// Deleting something that is not there is not an error, and still clears
    /// any cache left behind by a store that expired it on its own.
    #[tokio::test]
    async fn test_deleting_a_missing_simulation_is_not_an_error() {
        let manager = manager();

        match manager.delete(Uuid::new_v4()).await {
            Ok(deleted) => assert!(!deleted),
            Err(error) => panic!("a missing delete must not error: {error}"),
        }
    }

    /// An unknown id is not found, on every read path.
    #[tokio::test]
    async fn test_an_unknown_id_is_not_found() {
        let manager = manager();
        let missing = Uuid::new_v4();

        assert!(matches!(
            manager.get(missing).await,
            Err(ChainError::NotFound(_))
        ));
        assert!(matches!(
            manager.peek(missing).await,
            Err(ChainError::NotFound(_))
        ));
        assert!(matches!(
            manager.advance(missing).await,
            Err(ChainError::NotFound(_))
        ));
    }

    /// A second instance serves a step the first one built, without building
    /// it again.
    ///
    /// Two managers over one store and one shared cache is what a replicated
    /// deployment is, with the balancer's choice made explicit. The second
    /// manager's local cache is empty, so a rebuild is the only alternative to
    /// a shared hit — and a rebuild always writes, so the write count is what
    /// distinguishes them.
    #[tokio::test]
    async fn test_a_second_instance_serves_a_tape_it_did_not_build() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let first = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);
        let second = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);

        let created = match first.create(parameters(4)).await {
            Ok(created) => created,
            Err(error) => panic!("the simulation must be created: {error}"),
        };

        // Peeked rather than advanced, so both instances describe the SAME
        // step: an advance would move the shared cursor and the second
        // instance would legitimately serve the next one.
        let served_first = match first.peek(created.id).await {
            Ok(snapshot) => snapshot,
            Err(error) => panic!("the first instance must serve: {error}"),
        };
        assert_eq!(shared.writes(), 1, "building a tape must share it");
        assert_eq!(
            shared.keys().len(),
            1,
            "one simulation is one shared entry: {:?}",
            shared.keys()
        );

        let writes_after_build = shared.writes();
        let served_second = match second.peek(created.id).await {
            Ok(snapshot) => snapshot,
            Err(error) => panic!("the second instance must serve: {error}"),
        };

        assert_eq!(
            shared.writes(),
            writes_after_build,
            "the second instance wrote a tape, so it built one rather than reading the shared \
             one"
        );
        assert!(shared.reads() >= 1, "the second instance must have looked");
        assert_eq!(
            second.cached_tapes(),
            1,
            "a shared hit must still populate the local cache, or every step pays the round trip"
        );

        // Step zero's spot is the initial price whatever the walk did, so
        // comparing what both instances served proves nothing about the round
        // trip. The stored tape itself is compared instead, every row of it,
        // against one built directly from the same parameters: that is the
        // same-seed identical-tape contract surviving encode and decode.
        let stored = match shared.entries.lock() {
            Ok(entries) => entries.values().next().cloned(),
            Err(poisoned) => poisoned.into_inner().values().next().cloned(),
        };
        let stored = match stored {
            Some(stored) => stored,
            None => panic!("the tape must be in the shared cache to be compared"),
        };
        let decoded: FactorTape = match serde_json::from_str(&stored) {
            Ok(decoded) => decoded,
            Err(error) => panic!("a shared tape must decode: {error}"),
        };
        let built = match FactorTape::build(&created.parameters, &created.parameters.method) {
            Ok(built) => built,
            Err(error) => panic!("the tape must build: {error}"),
        };
        assert_eq!(
            decoded, built,
            "the tape that came back from the shared cache is not the tape that was built"
        );

        // And it is a tape with something in it: a walk that never moved would
        // make the comparison above vacuous.
        assert!(decoded.len() > 1, "the tape must cover its steps");
        assert!(
            decoded
                .rows()
                .iter()
                .any(|row| row.spot != decoded.rows()[0].spot),
            "the walk must move, or comparing it proves nothing"
        );

        // The step both instances described is the same one, which is what a
        // client sees.
        assert_eq!(served_first.1.step, served_second.1.step);
        assert_eq!(served_first.1.spot, served_second.1.spot);
    }

    /// A deleted simulation takes its shared tape with it.
    ///
    /// Without this the cache keeps a tape nobody can use until its TTL, which
    /// on a busy deployment is most of what it holds.
    #[tokio::test]
    async fn test_deleting_a_simulation_drops_its_shared_tape() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let manager = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);

        let created = match manager.create(parameters(3)).await {
            Ok(created) => created,
            Err(error) => panic!("{error}"),
        };
        let _ = manager.peek(created.id).await;
        assert_eq!(shared.keys().len(), 1, "the tape must be shared first");

        match manager.delete(created.id).await {
            Ok(deleted) => assert!(deleted, "the simulation must have been there"),
            Err(error) => panic!("{error}"),
        }

        assert!(
            shared.keys().is_empty(),
            "the deleted simulation left {:?} behind in the shared cache",
            shared.keys()
        );
    }

    /// Only one instance builds a tape, even when both ask at once.
    ///
    /// Two managers over one store and one shared cache, both told to serve
    /// the same simulation at the same moment. The write count is what
    /// distinguishes a build from a read, and exactly one build may happen:
    /// this is the coalescing that exists inside a process, extended across
    /// them.
    #[tokio::test]
    async fn test_only_one_instance_builds_a_tape() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let first = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);
        let second = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);

        let created = match first.create(parameters(4)).await {
            Ok(created) => created,
            Err(error) => panic!("the simulation must be created: {error}"),
        };

        // Both peek the same step at the same time, which is the shape a
        // balancer produces when two clients arrive together.
        let (one, two) = tokio::join!(first.peek(created.id), second.peek(created.id));
        let one = match one {
            Ok(snapshot) => snapshot,
            Err(error) => panic!("the first instance must serve: {error}"),
        };
        let two = match two {
            Ok(snapshot) => snapshot,
            Err(error) => panic!("the second instance must serve: {error}"),
        };

        assert_eq!(
            shared.writes(),
            1,
            "two instances wrote {} tapes for one simulation, so both built it",
            shared.writes()
        );
        assert!(
            shared.claims() >= 2,
            "both instances must have asked for the build claim, got {}",
            shared.claims()
        );
        assert_eq!(
            one.1.spot, two.1.spot,
            "the instance that waited must serve what the builder built"
        );
    }

    /// A claim that nobody releases does not wedge the next caller.
    ///
    /// The claim is held and never given back, which is what an instance
    /// killed mid-build leaves behind. The next caller waits, gives up, and
    /// builds rather than hanging.
    #[tokio::test]
    async fn test_an_abandoned_claim_does_not_block_a_build() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let manager = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>)
        .with_shared_build_wait(Duration::from_millis(200));

        let created = match manager.create(parameters(3)).await {
            Ok(created) => created,
            Err(error) => panic!("{error}"),
        };

        // Somebody else claims the build and dies without publishing.
        let key = tape_key(created.id, &created.parameters);
        assert!(
            matches!(shared.claim_build(&key).await, BuildClaim::Held(_)),
            "the claim must be free first"
        );

        match manager.peek(created.id).await {
            Ok(_) => {}
            Err(error) => panic!("an abandoned claim must not stop a build: {error}"),
        }
    }

    /// A client that goes away mid-build strands nothing.
    ///
    /// The build is on the blocking pool and keeps running whatever the caller
    /// does, so the owner's workflow lives in a task of its own. Were it in the
    /// request's future, a disconnect would leave the local waiters with an
    /// entry in `builds` nobody will ever publish, the deployment-wide claim
    /// held until it expires, and the finished tape unshared.
    #[tokio::test]
    async fn test_an_abandoned_owner_still_publishes_and_releases() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let manager = Arc::new(
            SimulationManager::new(
                Arc::clone(&store) as Arc<dyn SimulationStore>,
                SimulationV2Config::default(),
            )
            .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>),
        );

        let created = match manager.create(parameters(11)).await {
            Ok(created) => created,
            Err(error) => panic!("the simulation must be created: {error}"),
        };

        // The owner starts the build and its client disconnects.
        let owner = {
            let manager = Arc::clone(&manager);
            let id = created.id;
            tokio::spawn(async move { manager.peek(id).await })
        };
        owner.abort();

        // Whoever is left must still be served.
        match tokio::time::timeout(Duration::from_secs(5), manager.peek(created.id)).await {
            Ok(Ok(_)) => {}
            other => panic!("an abandoned owner must not strand the request after it: {other:?}"),
        }

        // And the deployment-wide claim came back rather than being left to
        // expire, so another instance is not blocked behind a client that left.
        let held = match shared.building.lock() {
            Ok(held) => held.len(),
            Err(poisoned) => poisoned.into_inner().len(),
        };
        assert_eq!(held, 0, "the build claim must have been released");
        assert_eq!(
            shared.keys().len(),
            1,
            "the built tape must have been shared even though its caller left"
        );
    }

    /// A shared cache that cannot be reached costs a rebuild, not a failure.
    #[tokio::test]
    async fn test_an_unreachable_shared_cache_degrades_to_building() {
        let store = Arc::new(InMemorySimulationStore::new());
        let manager = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::new(BrokenTapes) as Arc<dyn SharedTapeCache>);

        let created = match manager.create(parameters(3)).await {
            Ok(created) => created,
            Err(error) => panic!("the simulation must be created: {error}"),
        };

        for step in 0..3 {
            match manager.advance(created.id).await {
                Ok(_) => {}
                Err(error) => panic!("step {step} must serve with the cache down: {error}"),
            }
        }
    }

    /// Two simulations, two entries: a shared cache must not let one tape be
    /// served for another.
    #[tokio::test]
    async fn test_the_shared_cache_keys_each_simulation_separately() {
        let store = Arc::new(InMemorySimulationStore::new());
        let shared = Arc::new(SharedTapes::default());
        let manager = SimulationManager::new(
            Arc::clone(&store) as Arc<dyn SimulationStore>,
            SimulationV2Config::default(),
        )
        .with_shared_tapes(Arc::clone(&shared) as Arc<dyn SharedTapeCache>);

        let one = match manager.create(parameters(3)).await {
            Ok(created) => created,
            Err(error) => panic!("{error}"),
        };
        let two = match manager.create(parameters(3)).await {
            Ok(created) => created,
            Err(error) => panic!("{error}"),
        };
        assert_ne!(one.id, two.id);

        let _ = manager.advance(one.id).await;
        let _ = manager.advance(two.id).await;

        assert_eq!(
            shared.keys().len(),
            2,
            "two simulations must occupy two entries, whatever their parameters: {:?}",
            shared.keys()
        );
    }

    /// Cleanup expires idle simulations and evicts what they left cached.
    #[tokio::test]
    async fn test_cleanup_expires_and_evicts() {
        let store = Arc::new(InMemorySimulationStore::with_idle_retention(
            std::time::Duration::from_secs(1),
        ));
        let manager = SimulationManager::new(store, SimulationV2Config::default());
        let created = created(&manager, 5).await;
        match manager.peek(created.id).await {
            Ok(_) => {}
            Err(error) => panic!("the peek must succeed: {error}"),
        }
        assert_eq!(manager.cached_tapes(), 1);

        // Age the stored document past its retention window.
        let mut aged = created.clone();
        aged.updated_at = std::time::SystemTime::now() - std::time::Duration::from_secs(3_600);
        // Save at the same revision it was read at: the point is to age the
        // document, not to change it.
        let expected = aged.version;
        match manager.store.save_cas(aged, expected).await {
            Ok(()) => {}
            Err(error) => panic!("the aged document must save: {error}"),
        }

        match manager.cleanup().await {
            Ok(expired) => assert_eq!(expired, vec![created.id]),
            Err(error) => panic!("the cleanup must succeed: {error}"),
        }
        assert_eq!(manager.cached_tapes(), 0);
        assert_eq!(manager.cached_snapshots(), 0);
    }

    /// A snapshot survives an eviction of its tape, because both rebuild.
    #[tokio::test]
    async fn test_an_evicted_tape_rebuilds_identically() {
        let manager = manager();
        let created = created(&manager, 4).await;

        let before = match manager.peek(created.id).await {
            Ok((_, snapshot)) => snapshot,
            Err(error) => panic!("the peek must succeed: {error}"),
        };

        manager.evict(created.id);
        assert_eq!(manager.cached_tapes(), 0);

        let after = match manager.peek(created.id).await {
            Ok((_, snapshot)) => snapshot,
            Err(error) => panic!("the peek must succeed: {error}"),
        };
        assert_eq!(before, after, "a rebuild must be indistinguishable");
    }
}