dive-api 0.1.0

Embeddable Rust API of the dive TLA+ tools: parsing, semantic analysis, model checking, simulation and trace validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
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
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
//! Model checking (`dive check`): load, bind and check a specification, and
//! report the result as a versioned JSON document or TLC-style text.

use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use dive_eval::Profile;
use dive_runtime::{CheckOptions, CheckResult, Verdict};
use dive_sema::{Options, SearchPath};
use dive_store::StoreMode;
use serde_json::{Value, json};

use crate::{AnalyzeReport, ConfigReport};

#[path = "candidate_error.rs"]
mod candidate_error;

/// Version of ordinary safety JSON documents produced by [`CheckReport::to_json`].
pub const CHECK_SCHEMA_VERSION: u32 = crate::REPORT_SCHEMA_VERSION;

/// Version of temporal JSON documents, shared with all public reports.
/// Temporal claims require their actual final or prefix execution facts.
pub const TEMPORAL_CHECK_SCHEMA_VERSION: u32 = crate::REPORT_SCHEMA_VERSION;

/// Historical native exit codes, selected by `dive check --exit-codes native`
/// and the dive profile's [`CheckReport::exit_code`]. The CLI default uses TLC's
/// deciding status independently of evaluator profile (`EC.ExitStatus`: 0,
/// 10–14, 75–77, 150, 151, 255). Codes below marked "both" keep their meaning.
pub mod exit {
    /// No error found.
    pub const SUCCESS: u8 = 0;
    /// The specification or configuration was rejected, or TLC's binding
    /// of the model failed (dive profile; TLC's 150/151 under `--compat=tlc`).
    pub const REJECTED: u8 = 1;
    /// A malformed command line or unreadable input (both).
    pub const USAGE: u8 = 2;
    /// dive refused, naming the feature, or hit a resource limit; no verdict
    /// (both).
    pub const UNSUPPORTED: u8 = 3;
    /// An internal error in dive; no verdict (both).
    pub const INTERNAL: u8 = 4;
    /// The run's budget (`--time-limit`, `--max-memory`) ran out; no verdict
    /// (both).
    pub const STOPPED: u8 = 5;
    /// An invariant, property, action property or postcondition is violated.
    pub const VIOLATION: u8 = 10;
    /// A deadlock was reached.
    pub const DEADLOCK: u8 = 11;
    /// An assumption is false or failed to evaluate.
    pub const ASSUMPTION: u8 = 12;
    /// Evaluation failed: an error, a failed `Assert`, an overflow, an
    /// incompletely specified state.
    pub const EVALUATION: u8 = 13;
}

/// Process status conventions, independently of evaluator semantics.
///
/// `dive check` selects [`Self::Tlc`] by default. Existing
/// [`CheckReport::exit_code`] callers retain their profile's historical status;
/// select explicitly with [`CheckReport::exit_code_for`].
///
/// ```
/// use dive_api::{CheckReport, ExitConvention};
/// fn process_status(report: &CheckReport) -> u8 {
///     report.exit_code_for(ExitConvention::Tlc)
/// }
/// # let _ = process_status;
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExitConvention {
    /// Historical native statuses, including violation 10 and rejection 1.
    Native,
    /// The existing deciding TLC error-class status, without changing evaluation.
    Tlc,
}

impl ExitConvention {
    /// The CLI spelling reported in `options.exit_codes`.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Native => "native",
            Self::Tlc => "tlc",
        }
    }
}

/// The store `dive check` uses.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Store {
    /// Fingerprints (the default, as in TLC).
    #[default]
    Fingerprint,
    /// Full states (`--store exact`): an exhaustive result with no
    /// fingerprint-collision caveat.
    Exact,
}

/// A run-budget setting: the profile's default, no limit, or an
/// explicit value.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum BudgetSetting<T> {
    /// The profile's default: for the time limit none in both profiles, as in
    /// TLC; for memory [`dive_runtime::budget::default_memory`] in both.
    #[default]
    Default,
    /// No limit (`none`).
    Unlimited,
    /// This limit.
    Explicit(T),
}

impl<T: Copy> BudgetSetting<T> {
    pub(crate) fn resolve(self, default: Option<T>) -> (Option<T>, &'static str) {
        match self {
            Self::Default => (default, if default.is_some() { "default" } else { "none" }),
            Self::Unlimited => (None, "none"),
            Self::Explicit(value) => (Some(value), "explicit"),
        }
    }
}

/// Temporal checking strategy for a request.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TemporalMode {
    /// Ordinary checking. Eligible temporal requests use bounded final analysis
    /// in the TLC profile and deterministic completed-level checks in the dive
    /// profile.
    #[default]
    Ordinary,
}

/// What to check and how.
#[derive(Clone, Debug, Default)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "independent public checking options retain their boolean API"
)]
pub struct CheckRequest {
    /// Opt-in authenticated native pure-safety workers. None is TLC-equivalent.
    /// Requires explicit Core, seed and fingerprint; secret bytes never enter reports.
    ///
    /// ```no_run
    /// use dive_api::{CheckRequest, DistributedOptions, check_file};
    /// let request = CheckRequest {
    ///     file: "Counter.tla".into(), seed: Some(0), fp: Some(0),
    ///     distributed: Some(DistributedOptions::new(
    ///         vec!["127.0.0.1:7801".parse()?, "127.0.0.1:7802".parse()?],
    ///         "worker.key".into(),
    ///     )),
    ///     ..CheckRequest::default()
    /// };
    /// let report = check_file(&request)?;
    /// println!("{}", report.to_json());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub distributed: Option<crate::DistributedOptions>,
    /// Explicit producer-only Core debugger capability. None is the ordinary
    /// TLC-equivalent setting; use `debug::Session` for a managed session.
    pub debugger: Option<Arc<dyn dive_core::debug::Observer>>,
    /// `--source-coverage`: native source-expression work. Core does not
    /// implement it, so `true` is a named refusal.
    pub source_coverage: bool,
    /// `--coverage 0`: TLC's logical action-context costs. Core does not
    /// implement them, so `true` is a named refusal.
    pub tlc_coverage: bool,
    /// Explicit spec host categories, default-empty in both profiles: a
    /// specification gets host access only when it is granted.
    /// The first slice supports source-binding environment and TXT/UTF-8 reads.
    pub host_access: crate::HostAccess,
    /// Explicit bounded transient disk seen-state route. None keeps the memory
    /// reference; disk uses reported inline fallback and is not a total-RAM budget.
    pub seen_disk: Option<dive_runtime::SeenOptions>,
    /// Opt-in encoded frontier budget per current/next queue, in bytes. This
    /// does not bound seen sets, candidates, traces, graphs or process memory.
    pub frontier_memory_bytes: Option<usize>,
    /// Parent directory for private transient frontier spill files. Requires
    /// [`Self::frontier_memory_bytes`]; a directory alone is rejected.
    pub frontier_directory: Option<PathBuf>,
    /// Encoded candidate bytes per worker for explicit Core pure safety BFS.
    /// Zero forces disk. This excludes active evaluator roots, codec scratch,
    /// accepted Store values and whole-process memory.
    pub candidate_memory_bytes: Option<usize>,
    /// Parent directory for private candidate files; requires a candidate budget.
    pub candidate_directory: Option<PathBuf>,
    /// Opt-in completed-level measured Store-byte trigger under pure Core safety.
    /// Zero forces every boundary. This is not a live-root or process-memory cap.
    /// The TLC-equivalent setting is None. Recovery-only is supported.
    pub store_compact_bytes: Option<usize>,
    /// Separate retained predecessor-record capacity budget for safety BFS.
    /// Zero spills immediately; omitted preserves the in-memory counterpart.
    /// Reconstruction buffers and all other search storage are outside it.
    pub predecessor_memory_bytes: Option<usize>,
    /// Parent of private transient log directories. Requires a budget; omitted
    /// uses the system temporary directory. Files are removed before certification.
    pub predecessor_directory: Option<PathBuf>,
    /// The root module file.
    pub file: PathBuf,
    /// The configuration; `<file stem>.cfg` beside it by default, as in TLC.
    pub config: Option<PathBuf>,
    /// Module directories searched before the standard modules.
    pub library_paths: Vec<PathBuf>,
    /// Module directories searched after the standard modules.
    pub module_paths: Vec<PathBuf>,
    /// `--compat=tlc`.
    pub compat_tlc: bool,
    /// Whole-run symmetry/liveness policy; profile default when omitted.
    /// TLC-equivalent: `Tlc`, currently refused for quotient temporal checking.
    pub symmetry_liveness: Option<dive_model::SymmetryLiveness>,
    /// `--action-composition off|tlc|tla`; `None` is the profile's
    /// setting: `tla` by default, `off` under `--compat=tlc` (TLC without
    /// `-Dtlc2.tool.impl.Tool.cdot=true`).
    pub action_composition: Option<dive_eval::ActionComposition>,
    /// The store.
    pub store: Store,
    /// `--seed`; a fresh seed otherwise (always reported).
    pub seed: Option<i64>,
    /// `--core-sample N` (`--engine core`): after the search, compare the
    /// successors of `N` sampled expanded states with the dive-eval
    /// evaluator's. Diagnostic
    /// only: every result is unchanged. `None` samples nothing.
    pub core_sample: Option<usize>,
    /// `--fp`; TLC picks one at random under `--compat=tlc`, dive uses 0.
    pub fp: Option<usize>,
    /// `--workers`; `None` is the profile's default (TLC's 1 under
    /// `--compat=tlc`, the available parallelism otherwise).
    pub workers: Option<usize>,
    /// TLC's `-deadlock`: do not check for deadlock.
    pub no_deadlock: bool,
    /// `--sequential`: every level inline with one logical worker, as
    /// sequential mode does. Results are identical.
    pub sequential: bool,
    /// Test-only chaos scheduling (`--chaos SEED`, or `DIVE_CHAOS`); see
    /// `dive_runtime::CheckOptions::chaos`. Results must be identical.
    pub chaos: Option<u64>,
    /// Temporal checking strategy. `Ordinary` selects final analysis for
    /// eligible TLC-profile temporal roles and deterministic completed-level
    /// checks for eligible dive-profile roles. Unsupported combinations retain
    /// named refusals.
    pub temporal: TemporalMode,
    /// CLI-internal: refuse the model's temporal properties instead of
    /// checking them, appending this text to the refusal. The `dive` CLI sets
    /// it for strategies it accepts but Core does not implement, such as
    /// `--lncheck final --lasso short`. A model without temporal properties
    /// is checked as usual. Not part of the supported API.
    #[doc(hidden)]
    pub temporal_refusal: Option<String>,
    /// Completed-level schedule for ordinary dive-profile temporal checking
    /// (TLC checks only at the end). The default is the documented [`PeriodicSchedule::DEFAULT`];
    /// TLC-profile and explicit final requests ignore it and check once at the end.
    pub periodic: PeriodicSchedule,
    /// `--time-limit`: wall-clock time for the whole check.
    pub time_limit: BudgetSetting<std::time::Duration>,
    /// `--max-memory`: the whole process's resident-memory
    /// ceiling, in bytes.
    pub max_memory: BudgetSetting<u64>,
    /// Maximum admitted distinct in-model identities; `None` is TLC-equivalent.
    pub max_states: Option<u64>,
    /// Maximum BFS discovery depth (Init is depth one); `None` is TLC-equivalent.
    pub max_depth: Option<u64>,
}

/// Completed-level schedule for ordinary dive-profile temporal checking.
pub use dive_liveness::analyze::periodic::Schedule as PeriodicSchedule;

/// The phase a check ended in.
#[derive(Debug)]
enum Phase {
    /// Resource exhaustion observed before model binding begins.
    BudgetStopped(dive_runtime::budget::Stopped),
    /// The specification was rejected by the front end.
    SpecRejected(Box<AnalyzeReport>),
    /// The configuration was rejected.
    ConfigRejected(Box<ConfigReport>),
    /// The specification could not be lowered (a refusal).
    Unlowered(String),
    /// The checker ran.
    Checked(Box<CheckResult>),
    /// Independently validated bounded temporal analysis.
    Temporal(Box<crate::temporal::Report>),
    /// No temporal verdict: a refusal, limit, evaluation or internal failure.
    TemporalError(Box<crate::temporal::Failure>),
}

/// The outcome of `dive check`.
#[derive(Debug)]
pub struct CheckReport {
    checkpoint_request: Option<dive_runtime::checkpoint::Request>,
    checkpoint_receipt: Option<dive_runtime::checkpoint::Receipt>,
    file: String,
    config: String,
    profile: Profile,
    opts: CheckOptions,
    seed_given: bool,
    symmetry_liveness_explicit: bool,
    fp_given: bool,
    workers_requested: usize,
    /// Where the time and memory limits came from: `default`, `explicit`
    /// or `none`.
    budget_sources: (&'static str, &'static str),
    memory_requested_source: &'static str,
    memory_samples: Option<dive_runtime::budget::MemorySamples>,
    temporal_configuration: crate::temporal::Configuration,
    phase: Phase,
    /// The core engine's run, when `--engine=core` ran the check.
    core: Option<Box<crate::core_engine::CoreRun>>,
    /// Untrusted original source context captured while this run owns the IR.
    next_error_endpoint: Option<dive_trace::NextStateEndpoint>,
    /// Untrusted candidate-site observation; fresh replay proves its identities.
    candidate_error_endpoint: Option<dive_trace::CandidateStateEndpoint>,
    view_error_endpoint: Option<dive_trace::ViewSourceEndpoint>,
    /// Observed partial candidate captured while its original IR is owned.
    partial_terminal_endpoint: Option<dive_trace::PartialNextEndpoint>,
}

/// Names the first coordinator option that distributed safety does not admit,
/// before the model, configuration or key file is read.
fn distributed_refusal(req: &CheckRequest, checkpoint: bool) -> Option<String> {
    let missing = if req.seed.is_none() {
        Some("--seed")
    } else if req.fp.is_none() {
        Some("--fp")
    } else {
        None
    };
    if let Some(flag) = missing {
        return Some(format!(
            "distributed safety without an explicit {flag} is not implemented; give the same {flag} to the coordinator and every worker"
        ));
    }
    let feature = if checkpoint {
        "checkpoint or recovery"
    } else if req.core_sample.is_some() {
        "--core-sample"
    } else if !req.host_access.is_empty() {
        "spec host-access grants"
    } else if req.sequential {
        "--sequential"
    } else if req.chaos.is_some() {
        "chaos scheduling"
    } else if req.source_coverage {
        "source coverage"
    } else if req.seen_disk.is_some() {
        "disk-backed seen storage"
    } else if req.candidate_memory_bytes.is_some() {
        "candidate spill"
    } else {
        return None;
    };
    Some(format!(
        "distributed safety with {feature} is not implemented"
    ))
}

fn admit_storage_request(req: &CheckRequest) -> Result<(), crate::ApiError> {
    if req.store_compact_bytes.is_some() && req.core_sample.is_some() {
        return Err(crate::ApiError::Unsupported {
            feature: "Store reclamation with temporal capture or sampling".into(),
        });
    }
    if let Some(disk) = &req.seen_disk {
        disk.limits
            .validate()
            .map_err(|error| crate::ApiError::Rejected {
                message: error.to_string(),
            })?;
    }
    if req.candidate_directory.is_some() && req.candidate_memory_bytes.is_none() {
        return Err(crate::ApiError::Rejected {
            message: "DIVE-A0102: candidate_directory requires candidate_memory_bytes".into(),
        });
    }
    if req.frontier_directory.is_some() && req.frontier_memory_bytes.is_none() {
        return Err(crate::ApiError::Rejected {
            message: "DIVE-A0100: frontier_directory requires frontier_memory_bytes".into(),
        });
    }
    if req.predecessor_directory.is_some() && req.predecessor_memory_bytes.is_none() {
        return Err(crate::ApiError::Rejected {
            message: "DIVE-A0101: predecessor directory requires predecessor memory budget".into(),
        });
    }
    Ok(())
}

/// Loads, binds and checks the request with the compiled Core. What Core
/// does not implement is an `unsupported` verdict naming it.
///
/// ```no_run
/// use dive_api::{CheckRequest, check_file};
/// let req = CheckRequest { file: "Spec.tla".into(), ..CheckRequest::default() };
/// let report = check_file(&req).unwrap();
/// println!("{}", report.to_json()["engine"]);
/// ```
///
/// # Errors
/// [`crate::ApiError`] if the root file or the configuration cannot be read.
pub fn check_file(req: &CheckRequest) -> Result<CheckReport, crate::ApiError> {
    check_file_in(req, None, false, None)
}

/// Checks with Core under explicit strategy switches, as
/// `DIVE_CORE_OPT` gives them (for example `-memo,-delta-hash`); the
/// environment variable is ignored. Every switch must leave every result
/// unchanged.
///
/// # Errors
/// As [`check_file`].
pub fn check_file_core_opts(
    req: &CheckRequest,
    switches: &str,
) -> Result<CheckReport, crate::ApiError> {
    check_file_in(req, None, false, Some(switches))
}

/// Loads and checks files with TLC's configuration-first input error priority.
///
/// Used by the `dive tlc` and `tlc` checking entrypoints after resolving their
/// filenames. Unlike [`check_file`], this reads and parses the configuration
/// before analyzing the module: an unreadable or malformed configuration wins
/// when both inputs are invalid. Request defaults and path spellings are unchanged.
///
/// ```no_run
/// use dive_api::{CheckRequest, check_file_tlc_inputs};
/// let request = CheckRequest {
///     file: "Counter.tla".into(),
///     compat_tlc: true,
///     ..CheckRequest::default()
/// };
/// let report = check_file_tlc_inputs(&request)?;
/// # Ok::<(), dive_api::ApiError>(())
/// ```
///
/// # Errors
/// [`crate::ApiError`] if the configuration or root file cannot be read.
pub fn check_file_tlc_inputs(req: &CheckRequest) -> Result<CheckReport, crate::ApiError> {
    check_file_in(req, None, true, None)
}

/// Runs the native pure-safety checkpoint/recovery slice. It refuses
/// temporal combinations before loading files. Existing frontier/predecessor
/// budgets configure transient storage; native archive work is separately bounded
/// and excluded from both budgets. Recovery uses
/// saved seed/fingerprint defaults when not explicitly supplied; profile/store
/// and all semantic options must match. Actual workers are reported separately.
///
/// ```no_run
/// use dive_api::{CheckRequest, check_file_checkpoint, checkpoint};
/// let model = CheckRequest { file: "Counter.tla".into(), ..CheckRequest::default() };
/// let native = checkpoint::Request {
///     path: "counter.cp".into(),
///     action: checkpoint::Action::PauseAfterLevels(2),
///     counters: checkpoint::Counters::Tlc,
///     config_source_sha256: None,
/// };
/// let report = check_file_checkpoint(&model, &native)?;
/// assert_eq!(report.to_json()["options"]["checkpoint"]["action"], "pause");
/// # Ok::<(), dive_api::ApiError>(())
/// ```
///
/// # Errors
/// File I/O or malformed native request/metadata, without a model verdict.
pub fn check_file_checkpoint(
    req: &CheckRequest,
    checkpoint: &dive_runtime::checkpoint::Request,
) -> Result<CheckReport, crate::ApiError> {
    check_file_in(req, Some(checkpoint), false, None)
}

#[allow(
    clippy::too_many_lines,
    reason = "single ordered loader dispatch for ordinary and native recovery admission"
)]
fn check_file_in(
    req: &CheckRequest,
    checkpoint: Option<&dive_runtime::checkpoint::Request>,
    config_first: bool,
    core_switches: Option<&str>,
) -> Result<CheckReport, crate::ApiError> {
    if req.debugger.is_some() {
        let feature = if req.store_compact_bytes.is_some() {
            Some("compiled debugger with Store reclamation is not implemented")
        } else if req.tlc_coverage {
            Some("compiled debugger with TLC logical coverage is not implemented")
        } else if req.source_coverage {
            Some("compiled debugger with native source coverage is not implemented")
        } else if req.distributed.is_some() {
            Some("compiled debugger with distributed safety checking is not implemented")
        } else {
            None
        };
        if let Some(feature) = feature {
            return Err(crate::ApiError::Unsupported {
                feature: feature.into(),
            });
        }
    }
    admit_storage_request(req)?;
    if req.debugger.is_some()
        && (checkpoint.is_some() || req.temporal_refusal.is_some() || req.core_sample.is_some())
    {
        return Err(crate::ApiError::Unsupported { feature: "compiled debugger requires explicit Core ordinary safety checking without coverage, sampling or checkpoint".into() });
    }
    if req.distributed.is_some() && req.store_compact_bytes.is_some() {
        return Err(crate::ApiError::Unsupported {
            feature: "distributed safety with Store reclamation is not implemented".into(),
        });
    }
    if req.distributed.is_some() && req.tlc_coverage {
        return Err(crate::ApiError::Unsupported {
            feature: "distributed safety with TLC logical coverage is not implemented".into(),
        });
    }
    if req.distributed.is_some()
        && let Some(feature) = distributed_refusal(req, checkpoint.is_some())
    {
        return Err(crate::ApiError::Unsupported { feature });
    }
    if req.store_compact_bytes.is_some()
        && checkpoint
            .is_some_and(|request| request.action != dive_runtime::checkpoint::Action::Recover)
    {
        return Err(crate::ApiError::Unsupported {
            feature: "Store reclamation with checkpoint pause (discovery residency is unsupported)"
                .into(),
        });
    }
    if req.source_coverage && req.tlc_coverage {
        return Err(crate::ApiError::Unsupported {
            feature: "request native source work or TLC logical coverage separately".into(),
        });
    }
    if req.source_coverage || req.tlc_coverage {
        return Err(crate::ApiError::Unsupported { feature: "source coverage and TLC logical coverage (--source-coverage, --coverage) are not implemented on Core".into() });
    }
    if !req.host_access.is_empty() {
        return Err(crate::ApiError::Unsupported {
            feature: "external inputs (--allow-io, IOUtils/IOEnv) are not implemented on Core, nor with temporal checking or checkpoint/recovery"
                .into(),
        });
    }
    let profile = if req.compat_tlc {
        Profile::Tlc
    } else {
        Profile::Dive
    };
    let recovered_defaults = if let Some(request) = checkpoint
        && request.action == dive_runtime::checkpoint::Action::Recover
    {
        let defaults = crate::core_checkpoint::metadata(&request.path).map_err(|message| {
            crate::ApiError::Rejected {
                message: format!("DIVE-S0400: {message}"),
            }
        })?;
        Some(defaults)
    } else {
        None
    };
    let seed = req
        .seed
        .or(recovered_defaults.map(|p| p.0))
        .unwrap_or_else(dive_stdlib::random::fresh_seed);
    let fp = req
        .fp
        .or(recovered_defaults.map(|p| p.1))
        .unwrap_or_else(|| {
            if req.compat_tlc {
                // TLC: a random polynomial per run unless `-fp` is given
                // (`TLC.java`); reported either way.
                usize::try_from(seed.unsigned_abs() % 131).unwrap_or(0)
            } else {
                0
            }
        });
    let workers_requested = req.workers.unwrap_or_else(|| {
        if req.compat_tlc {
            1
        } else {
            std::thread::available_parallelism().map_or(1, std::num::NonZero::get)
        }
    });
    let mut opts = if req.compat_tlc {
        CheckOptions::tlc(fp, seed)
    } else {
        let mut o = CheckOptions::dive(seed);
        o.fp_index = fp;
        o
    };
    opts.eval.host_access = req.host_access;
    if !req.host_access.is_empty() {
        opts.inputs = Some(Arc::new(dive_stdlib::inputs::LiveInputs::new(
            req.host_access,
        )));
    }
    opts.store = match req.store {
        Store::Fingerprint => StoreMode::Fingerprint,
        Store::Exact => StoreMode::Exact,
    };
    opts.no_deadlock = req.no_deadlock;
    if let Some(policy) = req.symmetry_liveness {
        opts.symmetry_liveness = policy;
    }
    if let Some(mode) = req.action_composition {
        opts.eval.action_composition = mode;
    }
    opts.seen_disk.clone_from(&req.seen_disk);
    opts.frontier_memory_bytes = req.frontier_memory_bytes;
    opts.frontier_directory.clone_from(&req.frontier_directory);
    opts.candidate_memory_bytes = req.candidate_memory_bytes;
    opts.store_compact_bytes = req.store_compact_bytes;
    opts.candidate_directory
        .clone_from(&req.candidate_directory);
    opts.workers = workers_requested;
    opts.sequential = req.sequential;
    opts.chaos = req.chaos;
    opts.predecessor_memory_bytes = req.predecessor_memory_bytes;
    opts.max_states = req.max_states;
    opts.max_depth = req.max_depth;
    opts.predecessor_directory
        .clone_from(&req.predecessor_directory);
    let (time, time_source) = req.time_limit.resolve(None);
    let memory_resolution = resolve_memory(req.max_memory, dive_runtime::budget::default_memory);
    let memory_requested_source = match req.max_memory {
        BudgetSetting::Default => "default",
        BudgetSetting::Explicit(_) => "explicit",
        BudgetSetting::Unlimited => "none",
    };
    let (memory, memory_source) = memory_resolution.unwrap_or((None, "unavailable"));
    opts.budget = dive_runtime::budget::Budget::new(time, memory);
    let cfg_path = req
        .config
        .clone()
        .unwrap_or_else(|| req.file.with_extension("cfg"));
    let mut report = CheckReport {
        checkpoint_request: checkpoint.cloned(),
        checkpoint_receipt: None,
        file: req.file.display().to_string(),
        config: cfg_path.display().to_string(),
        profile,
        opts,
        seed_given: req.seed.is_some(),
        symmetry_liveness_explicit: req.symmetry_liveness.is_some(),
        fp_given: req.fp.is_some(),
        workers_requested,
        budget_sources: (time_source, memory_source),
        memory_requested_source,
        memory_samples: None,
        temporal_configuration: crate::temporal::file_configuration(),
        phase: Phase::Unlowered(String::new()),
        core: None,
        next_error_endpoint: None,
        candidate_error_endpoint: None,
        view_error_endpoint: None,
        partial_terminal_endpoint: None,
    };
    // TLC's Spec constructor parses ModelConfig before creating SpecProcessor
    // (tlaplus 94d0c50). Keep the file API's existing module-first order.
    let config = if config_first {
        let cfg = crate::parse_config_file(&cfg_path)?;
        if !cfg.accepted() {
            report.phase = Phase::ConfigRejected(Box::new(cfg));
            return Ok(report);
        }
        Some(cfg)
    } else {
        None
    };
    let search = Options {
        search: SearchPath {
            library_paths: req.library_paths.clone(),
            after_standard: req.module_paths.clone(),
        },
        ..Options::default()
    };
    let analysis =
        dive_sema::analyze_file(&req.file, &search).map_err(|error| crate::ApiError::Io {
            path: report.file.clone(),
            error,
        })?;
    if !analysis.accepted() {
        report.phase = Phase::SpecRejected(Box::new(AnalyzeReport::from_analysis(
            report.file.clone(),
            analysis,
        )));
        return Ok(report);
    }
    let cfg = match config {
        Some(cfg) => cfg,
        None => crate::parse_config_file(&cfg_path)?,
    };
    let Some(model) = cfg.config().cloned() else {
        report.phase = Phase::ConfigRejected(Box::new(cfg));
        return Ok(report);
    };
    let ir = match dive_ir::lower(&analysis) {
        Ok(ir) => Arc::new(ir),
        Err(e) => {
            report.phase = Phase::Unlowered(format!("lowering the specification: {e:?}"));
            return Ok(report);
        }
    };
    // One clock and one ceiling for every stage of the check itself; loading,
    // parsing and lowering above are not budgeted.
    memory_resolution.map_err(crate::ApiError::BudgetMeasurement)?;
    let checking_started = std::time::Instant::now();
    let (mut watchdog, armed) =
        dive_runtime::budget::arm(&report.opts).map_err(|error| match error {
            dive_runtime::budget::StartError::Measurement(error) => {
                crate::ApiError::BudgetMeasurement(error)
            }
            error => crate::ApiError::BudgetStartup(error),
        })?;
    let armed = armed.into_owned();
    report.phase = if let Some(exhausted) = armed
        .budget
        .poll()
        .map_err(crate::ApiError::BudgetMeasurement)?
    {
        Phase::BudgetStopped(exhausted.during(dive_runtime::budget::Phase::Startup))
    } else if let Some(request) = checkpoint {
        let mut request = request.clone();
        request.config_source_sha256 = Some(dive_runtime::checkpoint::config_source_hash(
            cfg.source_bytes(),
        ));
        let (native, run) =
            crate::core_checkpoint::check(&ir, &model, &armed, &request, profile == Profile::Tlc);
        report.core = Some(Box::new(run));
        report.checkpoint_receipt = native.receipt;
        Phase::Checked(Box::new(native.check))
    } else {
        let config_hash = dive_runtime::checkpoint::config_source_hash(cfg.source_bytes());
        let (result, run) = crate::core_engine::check(
            &ir,
            &model,
            &armed,
            req.compat_tlc,
            core_switches,
            req.core_sample,
            core_route(req, &armed),
            req.distributed
                .as_ref()
                .map(|options| (options, config_hash.as_str())),
            req.debugger.as_ref(),
        );
        report.core = Some(Box::new(run));
        result.into()
    };
    if report.core.is_some() {
        report.temporal_configuration = crate::temporal::core_file_configuration();
    }
    // A refused temporal strategy: name it in Core's temporal refusal.
    if report.core.is_some()
        && let Some(reason) = &req.temporal_refusal
        && let Phase::Checked(result) = &mut report.phase
        && let Verdict::Unsupported { feature } = &mut result.verdict
        && feature.contains("temporal properties (liveness checking)")
    {
        feature.push_str("; ");
        feature.push_str(reason);
    }
    if let Phase::Checked(result) = &mut report.phase {
        certify_next_receiver(&ir, &model, &armed, result);
    }
    if let Phase::Checked(result) = &report.phase
        && let Verdict::EvaluationError {
            site: dive_runtime::ErrorSite::Next,
            cause,
            reported,
            code,
            verified,
            ..
        } = &result.verdict
    {
        report.next_error_endpoint = if *verified && cause.exception_kind().is_some() {
            dive_trace::NextStateEndpoint::from_ordered_error(&ir, cause, *reported, *code)
        } else {
            dive_trace::NextStateEndpoint::from_error(&ir, cause)
        };
    }
    if let Phase::Checked(result) = &report.phase
        && let Verdict::EvaluationError {
            site,
            cause,
            reported,
            code,
            verified: true,
            ..
        } = &result.verdict
        && matches!(
            (cause.exception_kind(), *code),
            (Some(dive_eval::ExceptionKind::ToolRuntime), 2103)
                | (
                    Some(dive_eval::ExceptionKind::EvalException),
                    2132 | 2179 | 2178
                )
        )
    {
        let site = match site {
            dive_runtime::ErrorSite::Constraint => Some(dive_trace::CandidateErrorSite::Constraint),
            dive_runtime::ErrorSite::ActionConstraint => {
                Some(dive_trace::CandidateErrorSite::ActionConstraint)
            }
            dive_runtime::ErrorSite::ImpliedAction => {
                Some(dive_trace::CandidateErrorSite::ImpliedAction)
            }
            _ => None,
        };
        if let Some(site) = site {
            report.candidate_error_endpoint =
                dive_trace::CandidateStateEndpoint::from_error(&ir, cause, site, *reported, *code);
        }
    }
    if let Phase::Checked(result) = &report.phase
        && let Verdict::EvaluationError {
            site: dive_runtime::ErrorSite::View,
            cause,
            reported,
            code,
            verified: true,
            ..
        } = &result.verdict
        && matches!(
            (cause.exception_kind(), *code),
            (Some(dive_eval::ExceptionKind::ToolRuntime), 2103)
                | (
                    Some(dive_eval::ExceptionKind::EvalException),
                    2132 | 2179 | 2178
                )
        )
    {
        report.view_error_endpoint =
            dive_trace::ViewSourceEndpoint::from_error(&ir, cause, *reported, *code);
    }
    candidate_error::certify(&mut report, &ir, &model, &armed);
    candidate_error::certify_view(&mut report, &ir, &model, &armed);
    candidate_error::certify_invariant(&mut report, &ir, &model, &armed);
    if let Phase::Checked(result) = &report.phase
        && let Verdict::NotCompletelySpecified {
            initial,
            initial_candidate,
            ..
        } = &result.verdict
        && let Some(partial) = if *initial {
            initial_candidate.as_ref()
        } else {
            result
                .counterexample
                .as_ref()
                .and_then(|cx| cx.states.last())
                .and_then(|state| state.partial.as_ref())
        }
    {
        let mut assignments = serde_json::Map::new();
        let mut unassigned = Vec::new();
        let mut encodable = true;
        for (name, value) in result.var_names.iter().zip(&partial.values) {
            if *initial && !matches!(armed.budget.poll(), Ok(None)) {
                encodable = false;
                break;
            }
            if let Some(value) = value {
                // Init evidence must not force compound/deferred diagnostics
                // after the actual controlled deciding event. Scalars retain
                // the report's existing primitive serialization units.
                if *initial && (value.as_set().is_some() || value.as_fcn().is_some()) {
                    encodable = false;
                    break;
                }
                if let Ok(value) = dive_trace::value_json::to_json(value, report.opts.eval.ctx) {
                    assignments.insert(name.clone(), value);
                } else {
                    encodable = false;
                    break;
                }
            } else {
                unassigned.push(name.clone());
            }
        }
        if encodable && partial.values.len() == result.var_names.len() {
            report.partial_terminal_endpoint = if *initial {
                dive_trace::PartialStateEndpoint::from_initial_candidate(
                    &ir,
                    partial.expression,
                    assignments,
                    unassigned,
                )
            } else {
                dive_trace::PartialStateEndpoint::from_candidate(
                    &ir,
                    partial.expression,
                    assignments,
                    unassigned,
                )
            };
        }
    }
    if report.partial_terminal_endpoint.is_none()
        && let Phase::Checked(result) = &mut report.phase
        && matches!(
            result.verdict,
            Verdict::NotCompletelySpecified { initial: true, .. }
        )
    {
        result.notes.push("Init partial error witness remains unconfirmed: its observed source/bindings were unavailable, its compound assignment encoding is unsupported, or the controlling budget stopped evidence capture.".into());
    }
    if let Some(watchdog) = &mut watchdog {
        let finished = watchdog.finish();
        report.finish_budget(finished, checking_started.elapsed());
    }
    report.memory_samples = armed.budget.memory_samples();
    drop(watchdog);
    Ok(report)
}

/// The core's temporal route for this request: ordinary checking only,
/// with the default profile's completed-level schedule or, under the TLC
/// profile, the final check alone (as `check_backend` routes it). A refused
/// temporal strategy has no route.
fn core_route(req: &CheckRequest, options: &CheckOptions) -> Option<crate::core_temporal::Route> {
    req.temporal_refusal
        .is_none()
        .then(|| (options.profile == dive_eval::Profile::Dive).then_some(req.periodic))
}

impl From<crate::core_engine::CoreOutcome> for Phase {
    fn from(outcome: crate::core_engine::CoreOutcome) -> Self {
        match outcome {
            crate::core_engine::CoreOutcome::Checked(r) => Self::Checked(r),
            crate::core_engine::CoreOutcome::Temporal(r) => Self::Temporal(r),
            crate::core_engine::CoreOutcome::TemporalFailure(f) => Self::TemporalError(f),
        }
    }
}

pub(crate) fn resolve_memory(
    setting: BudgetSetting<u64>,
    default: impl FnOnce() -> Option<u64>,
) -> Result<(Option<u64>, &'static str), dive_runtime::budget::MeasurementError> {
    match setting {
        BudgetSetting::Default => default()
            .filter(|bytes| *bytes != 0)
            .map(|bytes| (Some(bytes), "default"))
            .ok_or_else(dive_runtime::budget::MeasurementError::default_ceiling_unavailable),
        BudgetSetting::Explicit(bytes) => Ok((Some(bytes), "explicit")),
        BudgetSetting::Unlimited => Ok((None, "none")),
    }
}

/// Admit a diagnostic wrapper only after fresh source-bound word validation.
/// The producer's typed receiver observation is a request, never a certificate.
fn certify_next_receiver(
    ir: &Arc<dive_ir::Ir>,
    config: &dive_config::ModelConfig,
    options: &CheckOptions,
    result: &mut CheckResult,
) {
    let Verdict::EvaluationError {
        site: dive_runtime::ErrorSite::Next,
        cause,
        reported,
        verified: false,
        ..
    } = &result.verdict
    else {
        return;
    };
    if !cause.value_method().is_some_and(|receiver| {
        receiver.method() == dive_eval::ValueMethod::FunctionMaterialize
            && receiver.source().is_none()
    }) {
        return;
    }
    let Some(path) = result
        .counterexample
        .as_ref()
        .filter(|path| path.states.len() == 1)
    else {
        return;
    };
    let Some(endpoint) =
        dive_trace::NextStateEndpoint::from_ordered_error(ir, cause, *reported, 2147)
    else {
        return;
    };
    let request = trace_doc(
        result,
        &path.states,
        options.eval.ctx,
        Some(&endpoint),
        None,
        None,
        None,
    )
    .and_then(|value| dive_trace::TraceDoc::from_json(&value));
    let Ok(document) = request else {
        return;
    };
    let control = match dive_runtime::budget::EvaluationControl::new(
        &options.budget,
        dive_runtime::budget::Phase::Validation,
    ) {
        Ok(control) => control,
        Err(error) => {
            result
                .notes
                .push(format!("Next receiver proof refused: {error}"));
            return;
        }
    };
    let recipe = dive_model::Recipe::new(
        ir.clone(),
        config.clone(),
        options.eval,
        options.no_deadlock,
    )
    .with_symmetry_liveness(options.symmetry_liveness);
    let session = dive_trace::Session::new(recipe, None);
    match session.read_controlled(&document, control) {
        Ok((report, _)) if report.validation == dive_trace::Validation::Valid => {
            if let Verdict::EvaluationError { code, name, verified, .. } = &mut result.verdict {
                *code = 2147;
                *name = "TLC_FINGERPRINT_EXCEPTION";
                *verified = true;
            }
        }
        Ok((report, _)) => result.notes.push(format!(
            "Next function receiver diagnostic lifetime remains unproved: {:?}",
            report.validation
        )),
        Err(error) => result.notes.push(format!(
            "Next function receiver diagnostic proof stopped; the original cause, class and counts are retained: {error}"
        )),
    }
}

impl CheckReport {
    fn finish_budget(
        &mut self,
        finished: Result<
            Option<dive_runtime::budget::Exhausted>,
            dive_runtime::budget::MeasurementError,
        >,
        elapsed: Duration,
    ) {
        match &mut self.phase {
            Phase::Checked(result) => {
                result.elapsed = elapsed;
                dive_runtime::budget::finish_result(
                    result,
                    finished,
                    dive_runtime::budget::Phase::Finalization,
                );
            }
            Phase::Temporal(report) => {
                if let Some(failure) = crate::temporal::finish_report(report, finished, elapsed) {
                    self.phase = Phase::TemporalError(Box::new(failure));
                }
            }
            // An earlier genuine error keeps its classification.
            _ => {}
        }
    }
    fn temporal_schema(&self) -> bool {
        matches!(&self.phase, Phase::Temporal(_) | Phase::TemporalError(_))
    }

    /// The safety checker's result, including a safety stop in final/short
    /// checking. Temporal holds/violations use [`Self::temporal_result`].
    #[must_use]
    pub fn result(&self) -> Option<&CheckResult> {
        match &self.phase {
            Phase::Checked(r) => Some(r),
            _ => None,
        }
    }

    /// Separate original initial-state diagnostic context. This is not a formal
    /// counterexample; its named cause and deciding replay need independent proof.
    #[must_use]
    pub fn diagnostic_witness(&self) -> Option<&dive_runtime::DiagnosticWitness> {
        self.result()
            .and_then(|result| result.diagnostic_witness.as_ref())
    }

    /// The validated temporal result, when final/short checking ran. Safety
    /// stops are exposed through [`Self::result`] instead.
    #[must_use]
    pub fn temporal_result(&self) -> Option<&crate::temporal::Report> {
        if let Phase::Temporal(r) = &self.phase {
            Some(r)
        } else {
            None
        }
    }

    /// The typed temporal failure, when temporal checking stopped without a
    /// verdict: its [`crate::temporal::Error`], stage and available evidence.
    #[must_use]
    pub fn temporal_failure(&self) -> Option<&crate::temporal::Failure> {
        if let Phase::TemporalError(failure) = &self.phase {
            Some(failure)
        } else {
            None
        }
    }

    /// The verdict's name in the JSON document.
    #[must_use]
    pub fn verdict(&self) -> &'static str {
        match &self.phase {
            Phase::BudgetStopped(_) => "stopped",
            Phase::SpecRejected(_) | Phase::ConfigRejected(_) => "rejected",
            Phase::Unlowered(_) => "unsupported",
            Phase::Checked(r) => r.verdict.kind(),
            Phase::Temporal(r) => match r.outcome() {
                crate::temporal::Outcome::NativeHolds(_) => "success",
                crate::temporal::Outcome::NativeViolation(_)
                | crate::temporal::Outcome::NativePrefixViolation(_) => "liveness-violation",
                crate::temporal::Outcome::Safety(_) => "internal-error",
            },
            Phase::TemporalError(e) => temporal_failure_kind(e),
        }
    }

    /// The historical API profile's exit code (see [`exit`]).
    ///
    /// This remains native for the dive profile and TLC for the TLC profile.
    /// The CLI's independent default is selected with [`Self::exit_code_for`].
    #[must_use]
    pub fn exit_code(&self) -> u8 {
        self.exit_code_for(if self.profile == Profile::Tlc {
            ExitConvention::Tlc
        } else {
            ExitConvention::Native
        })
    }

    /// Selects a process status without changing the evaluator's profile.
    ///
    /// TLC statuses come from the deciding class, not its reported message.
    /// Refusal/resource 3 and internal error 4 are preserved in both conventions.
    /// This does not establish parity of an unverified class or model result.
    #[must_use]
    pub fn exit_code_for(&self, convention: ExitConvention) -> u8 {
        let tlc = convention == ExitConvention::Tlc;
        match &self.phase {
            Phase::BudgetStopped(_) => exit::STOPPED,
            Phase::SpecRejected(_) => {
                if tlc {
                    150
                } else {
                    exit::REJECTED
                }
            }
            Phase::ConfigRejected(_) => {
                if tlc {
                    151
                } else {
                    exit::REJECTED
                }
            }
            Phase::Unlowered(_) => exit::UNSUPPORTED,
            Phase::Temporal(r) => match r.outcome() {
                crate::temporal::Outcome::NativeHolds(_) => exit::SUCCESS,
                crate::temporal::Outcome::NativeViolation(_)
                | crate::temporal::Outcome::NativePrefixViolation(_) => {
                    if tlc {
                        13
                    } else {
                        exit::VIOLATION
                    }
                }
                crate::temporal::Outcome::Safety(_) => exit::INTERNAL,
            },
            Phase::TemporalError(e) => match temporal_failure_kind(e) {
                "internal-error" => exit::INTERNAL,
                "stopped" => exit::STOPPED,
                "model-error" => {
                    if tlc {
                        if let crate::temporal::Error::Binding(failure) = e.error() {
                            failure
                                .error
                                .code()
                                .map_or(exit::INTERNAL, dive_runtime::tlc_exit_status)
                        } else {
                            exit::INTERNAL
                        }
                    } else {
                        exit::REJECTED
                    }
                }
                "evaluation-error" => {
                    if tlc {
                        if temporal_startup_tautology(e) {
                            dive_runtime::tlc_exit_status(
                                dive_model::Ec::TlcLiveFormulaTautology.code(),
                            )
                        } else {
                            temporal_evaluation(e).map_or(exit::INTERNAL, |error| {
                                dive_runtime::tlc_exit_status(error.ec().code())
                            })
                        }
                    } else {
                        exit::EVALUATION
                    }
                }
                _ => exit::UNSUPPORTED,
            },
            Phase::Checked(r) => {
                let v = &r.verdict;
                match v {
                    Verdict::Unsupported { .. } | Verdict::ResourceLimit { .. } => {
                        exit::UNSUPPORTED
                    }
                    Verdict::Stopped(_) => exit::STOPPED,
                    Verdict::InternalError { .. } | Verdict::BudgetMeasurement { .. } => {
                        exit::INTERNAL
                    }
                    _ if tlc => v.tlc().map_or(exit::INTERNAL, |c| c.exit),
                    Verdict::Success => exit::SUCCESS,
                    Verdict::InvariantViolated { .. }
                    | Verdict::PropertyViolatedInitially { .. }
                    | Verdict::ActionPropertyViolated { .. }
                    | Verdict::PostconditionFalse { .. } => exit::VIOLATION,
                    Verdict::Deadlock => exit::DEADLOCK,
                    Verdict::AssumptionFalse { .. }
                    | Verdict::EvaluationError {
                        site: dive_runtime::ErrorSite::Assumption,
                        ..
                    } => exit::ASSUMPTION,
                    Verdict::ModelError { .. } => exit::REJECTED,
                    Verdict::EvaluationError { .. } | Verdict::NotCompletelySpecified { .. } => {
                        exit::EVALUATION
                    }
                    _ => exit::INTERNAL,
                }
            }
        }
    }

    fn profile_name(&self) -> &'static str {
        match self.profile {
            Profile::Tlc
                if self.opts.eval.action_composition == dive_eval::ActionComposition::Tla
                    || self.checkpoint_request.as_ref().is_some_and(|r| {
                        r.counters == dive_runtime::checkpoint::Counters::Retain
                    })
                    // TLC has no time limit. A memory ceiling is TLC's
                    // `-Xmx` counterpart, a host resource.
                    || self.budget_sources.0 == "explicit"
                    || self.opts.max_states.is_some() || self.opts.max_depth.is_some()
                    || self.opts.symmetry_liveness != dive_model::SymmetryLiveness::Tlc =>
            {
                "tlc+overrides"
            }
            Profile::Tlc => "tlc",
            Profile::Dive => "dive",
        }
    }

    fn profile_for_exit_convention(&self, convention: ExitConvention) -> &'static str {
        if self.profile == Profile::Tlc && convention == ExitConvention::Native {
            "tlc+overrides"
        } else {
            self.profile_name()
        }
    }

    /// Reports an explicitly selected physical status and its convention.
    ///
    /// The underlying evaluator settings and deciding class are unchanged.
    /// Native statuses with TLC evaluation are labeled `tlc+overrides`.
    /// [`Self::to_json`] retains its historical API document.
    #[must_use]
    pub fn to_json_with_exit_convention(&self, convention: ExitConvention) -> Value {
        let mut document = self.to_json();
        document["exit_code"] = json!(self.exit_code_for(convention));
        document["options"]["exit_codes"] = json!(convention.as_str());
        document["options"]["semantic_profile"] = json!(if self.profile == Profile::Tlc {
            "tlc"
        } else {
            "dive"
        });
        document["options"]["profile"] = json!(self.profile_for_exit_convention(convention));
        document
    }

    /// Human output with the resolved physical status and compatibility label.
    /// [`Self::render_human`] retains its historical API output.
    #[must_use]
    pub fn render_human_with_exit_convention(&self, convention: ExitConvention) -> String {
        use std::fmt::Write as _;
        let mut output = self.render_human_profile(self.profile_for_exit_convention(convention));
        let _ = writeln!(
            output,
            "Exit codes: {} (process status {})",
            convention.as_str(),
            self.exit_code_for(convention)
        );
        output
    }

    /// The `options` block: profile, options and seed.
    fn symmetry_liveness(&self) -> Option<&dive_model::SymmetryLivenessResolution> {
        match &self.phase {
            Phase::Checked(result) => result.symmetry_liveness.as_ref(),
            Phase::Temporal(report) => report.symmetry_liveness(),
            Phase::TemporalError(failure) => failure.symmetry_liveness(),
            _ => None,
        }
    }

    fn analysis_configuration(&self) -> crate::temporal::Configuration {
        match &self.phase {
            Phase::Temporal(report) => report.configuration(),
            Phase::TemporalError(failure) => failure.configuration(),
            _ => None,
        }
        .unwrap_or(self.temporal_configuration)
    }

    #[allow(
        clippy::too_many_lines,
        reason = "one resolved options document retains option sources and storage policies"
    )]
    fn options_json(&self) -> Value {
        let o = &self.opts;
        let e = &o.eval;
        let int = match e.ctx.int_mode {
            dive_value::IntMode::Tlc => "tlc",
            dive_value::IntMode::Big => "big",
        };
        let order = match e.ctx.order {
            dive_value::ValueOrder::Tlc => "tlc",
            dive_value::ValueOrder::Canonical => "canonical",
        };
        let explore = match e.exploration {
            dive_eval::ExplorationOrder::Tlc => "tlc",
            dive_eval::ExplorationOrder::Value => "value",
        };
        let comparisons = match e.comparisons {
            dive_eval::Comparisons::Tlc => "tlc",
            dive_eval::Comparisons::Tla => "tla",
        };
        let fingerprint = (o.store == StoreMode::Fingerprint).then(|| {
            json!({
                "polynomial_index": o.fp_index,
                "polynomial_source": if self.fp_given { "flag" } else if self.checkpoint_request.as_ref().is_some_and(|r| r.action == dive_runtime::checkpoint::Action::Recover) { "checkpoint" } else if self.profile == Profile::Tlc { "random" } else { "default" },
                "scheme": if o.tlc_fingerprints { "tlc" } else { "dive" },
            })
        });
        let mut options = json!({
            "profile": self.profile_name(),
            "symmetry_liveness": o.symmetry_liveness.as_str(),
            "symmetry_liveness_source": if self.symmetry_liveness_explicit { "explicit" } else { "profile-default" },
            "int": int,
            "choose_order": order,
            "explore_order": explore,
            "comparisons": comparisons,
            "warnings": e.warnings,
            "allow_io": e.host_access.names(),
            "action_composition": match e.action_composition {
                dive_eval::ActionComposition::Off => "off",
                dive_eval::ActionComposition::Tlc => "tlc",
                dive_eval::ActionComposition::Tla => "tla",
            },
            "store": o.store.name(),
            "fingerprint": fingerprint,
            "seed": o.seed,
            "seed_source": if self.seed_given { "flag" } else if self.checkpoint_request.as_ref().is_some_and(|r| r.action == dive_runtime::checkpoint::Action::Recover) { "checkpoint" } else { "fresh" },
            "deadlock_flag": o.no_deadlock,
            "workers": self.workers_json(),
            "candidate_memory_bytes_per_worker": o.candidate_memory_bytes,
            "store_compact_trigger_bytes": o.store_compact_bytes,
            "candidate_record_bytes_limit": o.candidate_memory_bytes.map(|_| dive_store::spill::MAX_RECORD_BYTES),
            "frontier_memory_bytes_per_queue": o.frontier_memory_bytes,
            "frontier_record_bytes_limit": o.frontier_memory_bytes.map(|_| dive_store::spill::MAX_RECORD_BYTES),
            "frontier_codec_nodes_per_value_limit": o.frontier_memory_bytes.filter(|_| self.core.is_none()).map(|_| dive_value::codec::FRONTIER_MAX_NODES),
            "predecessor_memory_bytes": o.predecessor_memory_bytes,
            "predecessor_path_steps_limit": o.predecessor_memory_bytes.map(|_| dive_store::predecessor::MAX_PATH_STEPS),
            "budget": {
                "time_limit_ms": o.budget.time.map(|t| u64::try_from(t.as_millis()).unwrap_or(u64::MAX)),
                "time_limit_source": self.budget_sources.0,
                "max_memory_bytes": o.budget.memory,
                "max_memory_source": self.budget_sources.1,
                "max_memory_requested_source": self.memory_requested_source,
                "max_states": o.max_states,
                "max_depth": o.max_depth,
                "scope": "the check after loading: evaluator-backed fresh model binding after structural configuration/evaluator construction, safety search, graph capture, temporal analysis and fresh native temporal-candidate validation including its rebinding, and producer ASSUME using existing amortized loop checkpoints; structural binding, independent validation ASSUME, post-binding symmetry preparation, serialization, quiet value/bulk/native units, deferred providers retained by other evaluators and diagnostic reconstruction after a deciding event are not interrupted; legacy TLC/session and standalone validation are not controlled; memory of the whole process",
            },
        });
        if let Some(disk) = &o.seen_disk {
            options["seen_disk"] = json!({"entries_limit": disk.limits.entries, "key_bytes_limit": disk.limits.key_bytes, "probe_steps_limit": disk.limits.probe_steps, "summary_pairs_limit": disk.limits.summary_pairs, "directory": disk.directory, "execution": "inline-fallback", "resident_key_collection_bytes": 0, "total_memory_budget": false});
        }
        if self.temporal_schema() {
            options["lncheck"] = json!(self.temporal_result().map_or_else(
                || if let Phase::TemporalError(failure) = &self.phase {
                    if failure.periodic().is_some() {
                        "deterministic"
                    } else {
                        "final"
                    }
                } else {
                    "final"
                },
                crate::temporal::Report::lncheck
            ));
            options["lasso"] = json!("short");
            options["temporal_bounds"] = temporal_limits_json(self.analysis_configuration());
            options["tlc_exact_temporal"] = json!(false);
        }
        if let Some(request) = &self.checkpoint_request {
            options["checkpoint"] = json!({
                "path": request.path,
                "action": match request.action { dive_runtime::checkpoint::Action::Recover => "recover", dive_runtime::checkpoint::Action::PauseAfterLevels(_) => "pause" },
                "pause_after_levels": match request.action { dive_runtime::checkpoint::Action::PauseAfterLevels(n) => Some(n), dive_runtime::checkpoint::Action::Recover => None },
                "counters": request.counters,
                "max_payload_bytes": dive_runtime::checkpoint::MAX_PAYLOAD_BYTES,
                "max_states": dive_runtime::checkpoint::MAX_STATES,
                // Core has no separate state-image limit, also when the run
                // stops before Core starts (a parse error, say).
                "max_state_bytes": Value::Null,
                "backend": "core",
                "archive_bounds_scope": "archive admission only; ordinary search, Store, codec workspaces and process memory are separate",
                "max_level": dive_runtime::checkpoint::MAX_LEVEL,
                "max_counter": dive_runtime::checkpoint::MAX_COUNTER,
                "trusted_local_archive": true,
                "independent_certificate": false,
                "parallel_recovery": true,
            });
        }
        options
    }

    /// The workers requested and used: `{ requested, used, sequential,
    /// reason, chaos }`. These, with the timings, are the only fields of the
    /// document that may differ between worker counts.
    fn workers_json(&self) -> Value {
        if let Phase::TemporalError(failure) = &self.phase {
            return match failure.workers(self.workers_requested) {
                Some(w) => json!({
                    "requested": w.requested, "used": w.used,
                    "sequential": w.sequential, "reason": w.reason,
                    "chaos": self.opts.chaos,
                }),
                None => json!({
                    "requested": self.workers_requested, "used": null,
                    "sequential": null, "reason": "execution evidence unavailable",
                    "chaos": self.opts.chaos,
                }),
            };
        }
        let w = self
            .result()
            .map(|r| r.workers.clone())
            .or_else(|| {
                self.temporal_result()
                    .and_then(temporal_evidence)
                    .map(|c| c.workers)
            })
            .unwrap_or_else(|| dive_runtime::Workers {
                requested: self.workers_requested,
                used: 0,
                sequential: false,
                reason: Some("no search ran".to_owned()),
            });
        json!({
            "requested": w.requested,
            "used": w.used,
            "sequential": w.sequential,
            "reason": w.reason,
            "chaos": self.opts.chaos,
        })
    }

    /// The workers used, for the human header.
    fn workers_used(&self) -> String {
        match &self.phase {
            Phase::Checked(r) => r.workers.used.to_string(),
            Phase::Temporal(r) => temporal_evidence(r)
                .map_or(0, |c| c.workers.used)
                .to_string(),
            Phase::TemporalError(failure) => failure
                .workers(self.workers_requested)
                .map_or_else(|| "unknown".into(), |w| w.used.to_string()),
            _ => "0".into(),
        }
    }

    /// The JSON document (schema [`CHECK_SCHEMA_VERSION`]). `check_kind` names
    /// the safety or temporal request/report family; it does not assert that
    /// checking ran or that capture completed. Final/short safety failures
    /// retain the temporal family and their actual execution evidence:
    ///
    /// ```text
    /// { "schema_version": 3, "command": "check", "file", "config",
    ///   "verdict": "success" | "invariant-violation" | "property-violation"
    ///            | "action-property-violation" | "deadlock" | "assumption-failure"
    ///            | "postcondition-failure" | "assertion-failure" | "overflow"
    ///            | "evaluation-error" | "incomplete-state" | "model-error"
    ///            | "rejected" | "unsupported" | "resource-limit" | "stopped"
    ///            | "internal-error",
    ///   "property": string | null, "message": string | null,
    ///   "error_site": string | null,
    ///   "tlc": { "code", "name", "exit_status", "reported", "verified" } | null,
    ///   "statistics": { "generated", "distinct", "left_on_queue", "depth",
    ///                   "initial_generated", "reported_by_tlc" } | null,
    ///   "counterexample": { "length", "ending": "finite",
    ///                       "states": [ { "index", "label", "action",
    ///                                     "values": { var: TLC JSON }, "text" } ],
    ///                       "trace": dive-trace document | null } | null,
    ///   "diagnostic_witness": { "trace": native-v2 named cause document }, // optional
    ///   "collision": { "store", "scheme", "optimistic", "actual", "assumption" } | null,
    ///   "output": [ string ], "warnings": [ { "code", "message" } ],
    ///   "diagnostics": [ ... ], "notes": [ string ],
    ///   "stopped": { "resource": "time" | "memory" | "states" | "depth", "limit", "observed", "elapsed_ms",
    ///                "unit": "ms" | "bytes" | "states" | "levels", "phase", "hint" }   (only when stopped),
    ///   "options": { "profile", "int", "choose_order", "explore_order",
    ///                "comparisons", "warnings", "store", "fingerprint",
    ///                "seed", "seed_source", "deadlock_flag",
    ///                "budget": { "time_limit_ms", "time_limit_source",
    ///                            "max_memory_bytes", "max_memory_source", "max_states", "max_depth", "scope" },
    ///                "workers": { "requested", "used", "sequential", "reason",
    ///                             "chaos" } },
    ///   "elapsed_ms" }
    /// ```
    ///
    /// `tlc` is how TLC classifies the result: `code` decides TLC's exit
    /// status, and `reported` is the code of the error message TLC prints
    /// first (an invariant that fails to evaluate is reported as
    /// `TLC_INVARIANT_EVALUATION_FAILED`, for example); `verified: false`
    /// means dive does not yet reproduce TLC's error class at that site and
    /// does not claim it. `reported_by_tlc` is false where TLC prints no statistics
    /// (a stop while generating initial states). Explicit final-only schema 3
    /// also declares the schedule/selection, fixed entry/work bounds and actual
    /// failure stage. Periodic traces include all configured property names and
    /// a stuttering ending or one-based lasso target; independent replay precedes
    /// any private-certified temporal violation.
    #[must_use]
    #[allow(clippy::too_many_lines, reason = "one document")]
    pub fn to_json(&self) -> Value {
        let mut doc = json!({
            "schema_version": CHECK_SCHEMA_VERSION,
            "check_kind": if self.temporal_schema() { "temporal" } else { "safety" },
            "command": "check",
            "file": self.file,
            "config": self.config,
            "verdict": self.verdict(),
            "property": Value::Null,
            "message": Value::Null,
            "error_site": Value::Null,
            "tlc": Value::Null,
            "statistics": Value::Null,
            "counterexample": Value::Null,
            "collision": Value::Null,
            "frontier_storage": Value::Null,
            "output": [],
            "warnings": [],
            "diagnostics": [],
            "notes": [],
            "options": self.options_json(),
            "symmetry_liveness": self.symmetry_liveness().map(symmetry_liveness_json),
            "elapsed_ms": 0,
        });
        if let Some(inputs) = &self.opts.inputs {
            doc["external_inputs"] = crate::host_inputs::inputs_json(&inputs.recorded());
        }
        let samples = self.memory_samples.unwrap_or_default();
        doc["resources"] = json!({"memory": {
            "metric": if cfg!(target_os = "linux") { "rss+swap" } else if cfg!(target_os = "macos") { "rss" } else { "unavailable" },
            "status": if self.opts.budget.memory.is_none() { if self.budget_sources.1 == "unavailable" { "unavailable" } else { "disabled" } } else if samples.unavailable.is_some() { "unavailable" } else if self.memory_samples.is_some() { "sampled" } else { "not-started" },
            "samples": samples.count, "observed_max_bytes": samples.maximum,
            "initial_sampled": samples.initial, "final_sampled": samples.final_sample,
            "unavailable_at": samples.unavailable.map(dive_runtime::budget::SamplePoint::name),
            "scope": "sampled process memory, not an allocation-time hard cap or a measurement of the true peak; excursions between observations are possible"
        }});
        match &self.phase {
            Phase::BudgetStopped(stopped) => {
                doc["message"] = json!(stopped.to_string());
                doc["stopped"] = stopped_json(stopped);
                doc["elapsed_ms"] = json!(stopped.elapsed_ms);
                doc["resources"]["checking_started"] = json!(false);
                if self.temporal_schema() {
                    doc["execution"] = json!({"stage": "check-startup", "capture_completed": false, "started": false});
                }
            }
            Phase::SpecRejected(a) => {
                doc["diagnostics"] = a.to_json()["diagnostics"].clone();
                // TLC reports every spec SANY rejects, in its parse or its
                // semantic phase, as `TLC_PARSING_FAILED` (3002), exit 150.
                // Verified against captured runs of the pinned TLC: harness
                // fixtures `ParseError` and `SemanticError`, and the goldens
                // of the probes `M1Syntax` (a syntax error) and `M2Undefined`
                // (an undefined name) in `conformance/corpus/probes/`. Like
                // every other verdict's class, it is reported in both
                // profiles.
                doc["tlc"] = json!({"code": 3002, "name": "TLC_PARSING_FAILED", "exit_status": 150, "verified": true});
            }
            Phase::ConfigRejected(c) => {
                doc["diagnostics"] = c.to_json()["diagnostics"].clone();
            }
            Phase::Unlowered(why) => doc["message"] = Value::from(why.as_str()),
            Phase::Temporal(r) => {
                temporal_json(&mut doc, r, self.opts.tlc_fingerprints);
                let violation = matches!(
                    r.outcome(),
                    crate::temporal::Outcome::NativeViolation(_)
                        | crate::temporal::Outcome::NativePrefixViolation(_)
                );
                doc["execution"]["exhausted"] =
                    json!(r.periodic().is_none_or(|checks| checks.exhausted));
                doc["tlc"] = if violation {
                    json!({"code":2116,"name":"TLC_TEMPORAL_PROPERTY_VIOLATED","exit_status":13,"verified":true})
                } else {
                    json!({"code":2193,"name":"TLC_SUCCESS","exit_status":0,"verified":true})
                };
            }
            Phase::TemporalError(e) => {
                if let Some(progress) = e.validation_progress() {
                    doc["resources"]["validation"] = json!({
                        "checks_passed": progress.checks_passed,
                        "state": progress.state,
                        "check": progress.check.map(dive_trace::Check::name),
                    });
                }
                doc["message"] = json!(e.to_string());
                doc["diagnostics"] = json!([crate::diagnostics::temporal_failure(e)]);
                doc["execution"] =
                    json!({"stage": e.stage().name(), "capture_completed": e.capture_completed()});
                if e.stage() == crate::temporal::Stage::Binding
                    && e.exploration().is_none()
                    && !matches!(e.error(), crate::temporal::Error::Binding(_))
                {
                    doc["resources"]["checking_started"] = json!(false);
                }
                if let Some(checks) = e.periodic() {
                    doc["periodic"] = periodic_json(checks);
                }
                doc["elapsed_ms"] =
                    json!(u64::try_from(e.elapsed().as_millis()).unwrap_or(u64::MAX));
                if let crate::temporal::Error::Binding(failure) = e.error()
                    && let Some(class) = Verdict::from(&failure.error).tlc()
                {
                    doc["tlc"] = json!({
                        "code": class.code,
                        "name": class.name,
                        "exit_status": class.exit,
                        "reported": class.reported,
                        "verified": class.verified,
                    });
                } else if temporal_startup_tautology(e) {
                    let class = dive_model::Ec::TlcLiveFormulaTautology;
                    doc["tlc"] = json!({
                        "code": class.code(), "name": class.name(),
                        "reported": class.code(), "verified": true,
                        "exit_status": dive_runtime::tlc_exit_status(class.code()),
                    });
                } else if let Some(error) = temporal_evaluation(e) {
                    let code = error.ec().code();
                    doc["tlc"] = json!({
                        "code": code,
                        "name": error.ec().name(),
                        "exit_status": dive_runtime::tlc_exit_status(code),
                        "verified": if e.capture_row_error().is_some() { true } else if temporal_action_evaluation(e).is_some() {
                            error.ec() == dive_eval::Ec::General && !error.is_eval_exception()
                        } else { error.ec() == dive_eval::Ec::ExpectedValue },
                    });
                }
                if temporal_action_evaluation(e).is_some() {
                    doc["error_site"] = json!("temporal-action");
                }
                if let crate::temporal::Error::Analysis(
                    dive_liveness::analyze::AnalysisError::Stopped(stopped),
                ) = e.error()
                {
                    doc["stopped"] = stopped_json(stopped);
                }
                if let Some(capture) = e.exploration() {
                    temporal_evidence_json(&mut doc, capture, self.opts.tlc_fingerprints);
                    doc["notes"] = json!([
                        "Temporal checking failed; capture_completed records whether these actual exploration statistics describe completion. No temporal verdict was produced."
                    ]);
                } else {
                    doc["warnings"] = json!(
                        e.binding_output()
                            .iter()
                            .filter_map(|m| {
                                if let dive_model::Message::Warning { code, message } = m {
                                    Some(json!({"code": code.code(), "message": message}))
                                } else {
                                    None
                                }
                            })
                            .collect::<Vec<_>>()
                    );
                    doc["output"] = json!(
                        e.binding_output()
                            .iter()
                            .filter_map(|m| match m {
                                dive_model::Message::Print(text) => Some(text.clone()),
                                dive_model::Message::Warning { .. } => None,
                            })
                            .collect::<Vec<_>>()
                    );
                }
                if let Some((error, document, _)) = e.capture_row_error() {
                    doc["message"] = json!(error.to_string());
                    doc["error_site"] =
                        json!(if e.state_evaluation().is_some_and(|state| state.site()
                            == dive_liveness::analyze::StateEvaluationSite::CaptureSourceRow)
                        {
                            "temporal-source-row"
                        } else {
                            "temporal-state-row"
                        });
                    doc["tlc"]["reported"] = json!(error.ec().code());
                    doc["statistics"]["reported_by_tlc"] = json!(true);
                    doc["counterexample"] = json!({
                        "length": document.states.len(), "ending": "finite",
                        "states": document.states.iter().enumerate().map(|(i, values)| json!({"index":i+1,"label": if i==0 {Some("<Initial predicate>")} else {None},"action":null,"values":values,"text":null})).collect::<Vec<_>>(),
                        "trace": document.to_json(), "validation": "valid",
                    });
                    doc["notes"] = json!([
                        "The finite behavior and first ordered temporal demand error were independently reconstructed and validated on a fresh source-bound runtime. No BFS counters, lasso or violated-property claim is certified by endpoint validation."
                    ]);
                }
            }
            Phase::Checked(r) => {
                if let Some(storage) = &r.seen {
                    let facts = |s: dive_store::seen::Stats| json!({ "physical_entries": s.physical_entries, "expected_disk_bytes": s.disk_bytes, "resident_key_collection_bytes": s.resident_key_bytes, "poisoned": s.poisoned, "cleanup_completed": s.cleanup_completed });
                    doc["seen_storage"] = json!({
                        "mode": "fallible-disk", "admitted_keys": storage.admitted_keys,
                        "scope": "seen-key/offset collections only; supplied keys, state values, frontiers, predecessors, candidates, traces, graphs, OS page cache and native archives are outside these bounds",
                        "before_cleanup": storage.before_cleanup.map(facts), "after_cleanup": storage.after_cleanup.map(facts),
                        "first_failure": storage.first_failure.as_ref().map(|failure| json!({"phase": format!("{:?}", failure.phase), "operation": format!("{:?}", failure.error.operation), "code": failure.error.code(), "message": failure.error.message, "cleanup_failure": failure.error.cleanup_failure, "generated": failure.generated, "predecessor": failure.predecessor, "successor_ordinal": failure.successor_ordinal})),
                        "deciding_event_before_failure": storage.deciding_event_before_failure.as_ref().map(|v| format!("{v:?}")),
                        "replay": storage.replay.as_ref().map(|r| json!({"original_error": format!("{:?}", r.original_error), "predecessor": r.predecessor, "stopped_successor_ordinal": r.stopped_successor_ordinal, "generated": r.generated, "admitted_keys": r.admitted_keys, "queued": r.queued, "unproved_print_output": r.unproved_print_output, "halted_by": r.halted_by.as_ref().map(|v| format!("{v:?}"))})),
                        "crash_durable": false, "checkpoint_import_supported": false,
                        "checkpoint_key_reconstitution": self.core.is_some() && self.checkpoint_request.is_some(),
                    });
                }
                doc["frontier_storage"] = json!({
                    "mode": if r.frontier.memory_bytes_per_queue.is_some() { "encoded-spill" } else { "memory-reference" },
                    "scope": if self.core.is_some() && r.frontier.memory_bytes_per_queue.is_some() {
                        "encoded current and next frontier capacities observed at queue operations only; retained live Store and construction history, decoded chunks, seen sets, candidates, arenas, traces, allocator workspace and native archives are not measured or bounded"
                    } else { "encoded current and next frontier queues only; legacy value-frontier heap, candidates, seen sets, traces, graphs and native archive work are not measured or bounded" },
                    "memory_bytes_per_queue": r.frontier.memory_bytes_per_queue,
                    "max_record_bytes": r.frontier.max_record_bytes,
                    "max_codec_nodes_per_value": r.frontier.memory_bytes_per_queue.filter(|_| self.core.is_none()).map(|_| dive_value::codec::FRONTIER_MAX_NODES),
                    "peak_retained_bytes": r.frontier.peak_retained_bytes,
                    "retained_bytes": r.frontier.retained_bytes,
                    "committed_spill_bytes": r.frontier.spilled_bytes,
                    "write_accounting_complete": r.frontier.failures.is_empty(),
                    "failures": r.frontier.failures,
                    "search_event_before_storage_failure": r.frontier.deciding_event_before_failure,
                });
                if self.opts.predecessor_memory_bytes.is_some() {
                    let p = &r.predecessor;
                    doc["predecessor_storage"] = json!({
                        "scope": "predecessor-record-capacity-only",
                        "retained_bytes_before_cleanup": p.storage.retained_bytes,
                        "peak_retained_bytes": p.storage.peak_retained_bytes,
                        "confirmed_write_bytes": p.storage.written_bytes,
                        "committed_records": p.storage.records,
                        "spilled": p.storage.spilled,
                        "write_accounting_complete": p.failures.is_empty(),
                        "cleanup_completed": p.cleanup_completed,
                        "failures": p.failures,
                        "deciding_event_before_failure": p.deciding_event_before_failure,
                        "excludes": ["seen-set", "frontiers", "candidate-vectors", "graphs", "reconstructed-paths", "decoded-states", "allocator-workspace", "native-archive-work"]
                    });
                }
                let ctx = self.opts.eval.ctx;
                let v = &r.verdict;
                doc["property"] = v.property().map_or(Value::Null, Value::from);
                doc["message"] = match v {
                    Verdict::AssumptionFalse { message, .. }
                    | Verdict::EvaluationError { message, .. }
                    | Verdict::ModelError { message, .. }
                    | Verdict::PostconditionFalse { message, .. }
                    | Verdict::NotCompletelySpecified { message, .. }
                    | Verdict::ResourceLimit { message }
                    | Verdict::InternalError { message } => Value::from(message.as_str()),
                    Verdict::Unsupported { feature } => Value::from(feature.as_str()),
                    Verdict::Stopped(stopped) => Value::from(stopped.to_string()),
                    Verdict::BudgetMeasurement { error } => Value::from(error.to_string()),
                    _ => Value::Null,
                };
                if let Verdict::BudgetMeasurement { error } = v {
                    doc["diagnostics"] = json!([{"code": dive_runtime::budget::MEASUREMENT_FAILURE_CODE, "message": error.to_string(), "cause": error.to_string(), "span": null, "help": crate::diagnostics::explain(dive_runtime::budget::MEASUREMENT_FAILURE_CODE).map(|entry| entry.remediation)}]);
                }
                if let Verdict::Stopped(stopped) = v {
                    doc["stopped"] = stopped_json(stopped);
                }
                if let Verdict::EvaluationError { site, .. } = v {
                    doc["error_site"] = Value::from(site.name());
                }
                if let Some(c) = v.tlc() {
                    doc["tlc"] = json!({
                        "code": c.code,
                        "name": c.name,
                        "exit_status": c.exit,
                        "reported": c.reported,
                        "verified": c.verified,
                    });
                }
                if let Some(s) = &r.stats {
                    doc["statistics"] = json!({
                        "generated": s.generated,
                        "distinct": s.distinct,
                        "left_on_queue": s.left_on_queue,
                        "depth": s.depth,
                        "initial_generated": s.initial_generated,
                        // A stop's statistics are progress, not TLC's report.
                        "reported_by_tlc": r.tlc_prints_stats
                            && !matches!(r.verdict, Verdict::Stopped(_) | Verdict::BudgetMeasurement { .. }),
                    });
                }
                if let Some(cx) = &r.counterexample {
                    let states: Vec<Value> = cx
                        .states
                        .iter()
                        .enumerate()
                        .map(|(i, s)| {
                            let values: serde_json::Map<String, Value> = r
                                .tlc_var_order
                                .iter()
                                .filter_map(|&k| {
                                    let name = r.var_names.get(k)?.clone();
                                    let v = s.values.get(k)?;
                                    let j = dive_value::json::to_tlc_json(v, ctx)
                                        .ok()
                                        .and_then(|t| serde_json::from_str(&t).ok())
                                        .unwrap_or(Value::Null);
                                    Some((name, j))
                                })
                                .collect();
                            json!({
                                "index": i + 1,
                                "label": s.label,
                                "action": s.action,
                                "values": values,
                                "text": dive_runtime::format_trace_state(r, s, ctx),
                            })
                        })
                        .collect();
                    let transported = trace_doc(
                        r,
                        &cx.states,
                        ctx,
                        self.next_error_endpoint.as_ref(),
                        self.partial_terminal_endpoint.as_ref(),
                        self.candidate_error_endpoint.as_ref(),
                        self.view_error_endpoint.as_ref(),
                    );
                    doc["counterexample"] = json!({
                        "length": states.len(),
                        "ending": "finite",
                        "states": states,
                        "trace": transported.unwrap_or(Value::Null),
                    });
                }
                if let Some(witness) = &r.diagnostic_witness {
                    let transported = trace_doc(r, witness.states(), ctx, None, None, None, None);
                    doc["diagnostic_witness"] =
                        json!({"trace": transported.unwrap_or(Value::Null)});
                }
                if matches!(
                    r.verdict,
                    Verdict::NotCompletelySpecified { initial: true, .. }
                ) && let Some(endpoint) = &self.partial_terminal_endpoint
                {
                    let witness = dive_trace::TraceDoc {
                        variables: r.var_names.clone(),
                        states: Vec::new(),
                        labels: Vec::new(),
                        ending: Some(dive_trace::Ending::PartialInit {
                            endpoint: endpoint.clone(),
                        }),
                        length: Some(1),
                        source: dive_trace::Source::Dive,
                        properties: Vec::new(),
                    };
                    doc["error_witness"] = witness.to_json();
                }
                if let Some(c) = &r.collision {
                    doc["collision"] = json!({
                        "store": "fingerprint",
                        "scheme": if r.tlc_fingerprints { "tlc" } else { "dive" },
                        "optimistic": c.optimistic,
                        "actual": c.actual,
                        "assumption": "fingerprints behave as random 64-bit values (TLC's assumption too)",
                    });
                }
                doc["output"] = json!(r.output);
                doc["warnings"] = r
                    .warnings
                    .iter()
                    .map(|(c, m)| json!({"code": c, "message": m}))
                    .collect();
                doc["notes"] = json!(r.notes);
                doc["elapsed_ms"] = json!(u64::try_from(r.elapsed.as_millis()).unwrap_or(u64::MAX));
            }
        }
        if self.checkpoint_request.is_some() {
            doc["checkpoint"] = json!(self.checkpoint_receipt);
        }
        let measurement_failure = match &self.phase {
            Phase::Checked(result) => matches!(result.verdict, Verdict::BudgetMeasurement { .. }),
            Phase::TemporalError(failure) => {
                matches!(
                    failure.error(),
                    crate::temporal::Error::Analysis(
                        dive_liveness::analyze::AnalysisError::BudgetMeasurement(_)
                    )
                ) || matches!(failure.error(), crate::temporal::Error::Analysis(dive_liveness::analyze::AnalysisError::Exploration(result)) if matches!(result.verdict, Verdict::BudgetMeasurement { .. }))
            }
            _ => false,
        };
        if (self.verdict() == "stopped" || measurement_failure) && doc["statistics"].is_object() {
            doc["statistics"]["exhaustive"] = json!(false);
        }
        if let Some(core) = &self.core {
            doc["engine"] = json!("core");
            doc["core"] = json!({"disabled": core.opts, "metrics": core.metrics});
            if !core.sample.is_null() {
                doc["core"]["sample"] = core.sample.clone();
            }
        }
        doc
    }

    /// The TLC-style text report (see `dive_runtime::render_human`), preceded
    /// by the run's options and the `Print` output.
    #[must_use]
    pub fn render_human(&self) -> String {
        self.render_human_profile(self.profile_name())
    }

    #[allow(clippy::too_many_lines, reason = "one complete human check report")]
    fn render_human_profile(&self, profile: &str) -> String {
        use std::fmt::Write as _;
        let mut out = String::new();
        let o = &self.opts;
        let fp = if o.store == StoreMode::Fingerprint {
            format!(" (fp {})", o.fp_index)
        } else {
            String::new()
        };
        let _ = writeln!(
            out,
            "dive check {}: profile {}, store {}{fp}, seed {}, {} worker(s) requested, {} used",
            self.file,
            profile,
            o.store.name(),
            o.seed,
            self.workers_requested,
            self.workers_used()
        );
        if !o.eval.host_access.is_empty() {
            let _ = writeln!(
                out,
                "External inputs: explicit {} grants; recorded source-binding values are assumptions.",
                o.eval.host_access.names().join(",")
            );
        }
        if let Some(symmetry) = self.symmetry_liveness()
            && symmetry.unreduced()
        {
            let _ = writeln!(
                out,
                "Declared SYMMETRY {}; whole-run policy unreduced (no symmetry reduction); actual progress follows.",
                symmetry.declared().unwrap_or_default()
            );
        }
        if let Phase::Checked(r) = &self.phase
            && let Some(why) = &r.workers.reason
        {
            let _ = writeln!(out, "{why}");
        }
        if let Some(request) = &self.checkpoint_request {
            let _ = writeln!(
                out,
                "native checkpoint {:?}, counters {:?}; trusted local archive, not an independent certificate",
                request.action, request.counters
            );
            if let Some(receipt) = &self.checkpoint_receipt {
                let _ = writeln!(
                    out,
                    "{}: {} discoveries, {} queued, next level{}",
                    receipt.status, receipt.discovered, receipt.queued, receipt.next_level
                );
            }
        }
        match &self.phase {
            Phase::BudgetStopped(stopped) => {
                let _ = writeln!(out, "{stopped}; no verdict produced");
            }
            Phase::SpecRejected(a) => out.push_str(&a.render_human()),
            Phase::ConfigRejected(c) => out.push_str(&c.render_human()),
            Phase::Unlowered(why) => {
                let _ = writeln!(out, "unsupported: {why}; no verdict produced");
            }
            Phase::Temporal(r) => out.push_str(&temporal_human(r)),
            Phase::TemporalError(e) => {
                let _ = writeln!(
                    out,
                    "{}: {e}; no verdict produced",
                    temporal_failure_kind(e)
                );
                let _ = writeln!(out, "Execution stage: {}", e.stage().name());
                if let Some(explanation) = crate::diagnostics::explain(e.error().code()) {
                    let _ = writeln!(out, "Help: {}", explanation.remediation);
                }
                if let Some(capture) = e.exploration() {
                    for (_, warning) in &capture.warnings {
                        let _ = writeln!(out, "Warning: {warning}");
                    }
                    for line in &capture.output {
                        let _ = writeln!(out, "{line}");
                    }
                    let stats = &capture.stats;
                    let completion = match e.capture_completed() {
                        Some(true) => "completed",
                        Some(false) => "incomplete",
                        None => "completion unknown",
                    };
                    let _ = writeln!(
                        out,
                        "Capture {completion}: {} generated, {} distinct, depth {}; no temporal verdict.",
                        stats.generated, stats.distinct, stats.depth
                    );
                } else if !matches!(e.error(), crate::temporal::Error::Binding(_)) {
                    for message in e.binding_output() {
                        match message {
                            dive_model::Message::Print(text) => {
                                let _ = writeln!(out, "{text}");
                            }
                            dive_model::Message::Warning { message, .. } => {
                                let _ = writeln!(out, "Warning: {message}");
                            }
                        }
                    }
                }
                if let Some((_, document, _)) = e.capture_row_error() {
                    let _ = writeln!(out, "The behavior up to this point is:");
                    for (index, state) in document.states.iter().enumerate() {
                        let _ = writeln!(out, "State {}: {}", index + 1, json!(state));
                    }
                }
            }
            Phase::Checked(r) => {
                for (_, w) in &r.warnings {
                    let _ = writeln!(out, "Warning: {w}");
                }
                for line in &r.output {
                    let _ = writeln!(out, "{line}");
                }
                out.push_str(&dive_runtime::render_human(r, o.eval.ctx));
            }
        }
        out
    }
}

/// The counterexample in `dive-trace`'s format, the
/// document the independent validator reads: `dive
/// validate-trace` and the conformance harness take it from
/// `counterexample.trace`. `null` when a value has no JSON form or the
/// verdict has no ending the format names.
fn trace_doc(
    r: &dive_runtime::CheckResult,
    recorded: &[dive_runtime::TraceState],
    ctx: dive_value::Ctx,
    next_endpoint: Option<&dive_trace::NextStateEndpoint>,
    partial_endpoint: Option<&dive_trace::PartialNextEndpoint>,
    candidate_endpoint: Option<&dive_trace::CandidateStateEndpoint>,
    view_endpoint: Option<&dive_trace::ViewSourceEndpoint>,
) -> Result<Value, String> {
    use dive_runtime::Verdict as V;
    use dive_trace::Ending;
    let ending = match &r.verdict {
        V::InvariantViolated { property, .. } => Ending::Invariant(property.clone()),
        V::PropertyViolatedInitially { property } | V::ActionPropertyViolated { property } => {
            Ending::Property(property.clone())
        }
        V::Deadlock => Ending::Deadlock,
        V::NotCompletelySpecified { initial: false, .. } if partial_endpoint.is_some() => {
            Ending::PartialNext {
                endpoint: partial_endpoint
                    .cloned()
                    .ok_or("missing partial Next source")?,
            }
        }
        // A named word preserves its original cause. The independent reader
        // must separately prove the initial2147/2102 diagnostic relationship;
        // the successor2111 relationship retains its existing checks.
        V::EvaluationError {
            site: dive_runtime::ErrorSite::Invariant,
            code,
            reported,
            verified: true,
            invariant: Some(name),
            cause,
            ..
        } if (*reported == 2102 && *code == 2147)
            || (*reported == 2111
                && (matches!(*code, 2111 | 2103) || *code == cause.ec().code())) =>
        {
            Ending::InvariantError {
                name: name.clone(),
                ec: cause.ec().name().to_owned(),
            }
        }
        V::EvaluationError {
            site: dive_runtime::ErrorSite::Next,
            cause,
            ..
        } if next_endpoint.is_some() => Ending::NextStateError {
            ec: cause.ec().name().to_owned(),
            endpoint: next_endpoint.cloned().ok_or("missing Next error source")?,
        },
        V::EvaluationError { cause, .. } if view_endpoint.is_some() => Ending::ViewSourceError {
            ec: cause.ec().name().to_owned(),
            endpoint: view_endpoint.cloned().ok_or("missing VIEW source")?,
        },
        V::EvaluationError { cause, .. } if candidate_endpoint.is_some() => {
            Ending::CandidateStateError {
                ec: cause.ec().name().to_owned(),
                endpoint: candidate_endpoint
                    .cloned()
                    .ok_or("missing candidate source")?,
            }
        }
        V::EvaluationError { cause, .. } => {
            if cause.ec() == dive_eval::Ec::AssertFailed {
                Ending::Assertion
            } else {
                Ending::Error {
                    ec: Some(cause.ec().name().to_owned()),
                }
            }
        }
        _ => return Err("verdict has no supported trace ending".into()),
    };
    let mut states = Vec::with_capacity(recorded.len());
    let mut labels = Vec::with_capacity(recorded.len());
    for (i, s) in recorded.iter().enumerate() {
        if s.partial.is_some() {
            if i + 1 != recorded.len() || !matches!(ending, Ending::PartialNext { .. }) {
                return Err("partial bindings must be a separate final terminal descriptor".into());
            }
            continue;
        }
        let mut m = serde_json::Map::new();
        for (k, name) in r.var_names.iter().enumerate() {
            let Some(v) = s.values.get(k) else {
                return Err("counterexample has an unassigned variable".into());
            };
            m.insert(name.clone(), dive_trace::value_json::to_json(v, ctx)?);
        }
        states.push(m);
        labels.push(if i == 0 {
            None
        } else {
            s.action.as_ref().map(|name| dive_trace::Label {
                name: name.clone(),
                location: Some(s.label.clone()),
            })
        });
    }
    let doc = dive_trace::TraceDoc {
        variables: r.var_names.clone(),
        states,
        labels,
        ending: Some(ending),
        length: Some(recorded.len()),
        source: dive_trace::Source::Dive,
        properties: Vec::new(),
    };
    Ok(doc.to_json())
}

/// The `stopped` block of a run its budget stopped.
pub(crate) fn stopped_json(stopped: &dive_runtime::budget::Stopped) -> Value {
    json!({
        "resource": stopped.resource.name(),
        "limit": stopped.limit,
        "observed": stopped.observed,
        "elapsed_ms": stopped.elapsed_ms,
        "unit": stopped.resource.unit(),
        "phase": stopped.phase.name(),
        "hint": stopped.hint(),
    })
}

fn temporal_planning_evaluation(error: &crate::temporal::Error) -> Option<&dive_eval::EvalError> {
    match error {
        crate::temporal::Error::Analysis(dive_liveness::analyze::AnalysisError::Planning(
            dive_liveness::plan::PlanningError::Evaluation { error, .. },
        )) => Some(error),
        _ => None,
    }
}

fn temporal_action_evaluation(failure: &crate::temporal::Failure) -> Option<&dive_eval::EvalError> {
    let action = failure.action_evaluation()?;
    // TLC combines negated claims before simplification; this backend still
    // analyzes them separately. Do not invent a verified error for an action
    // that a different property could eliminate from the combined formula.
    let cause = action.core_cause()?;
    (action.properties() == 1
        && action.direct_operand()
        && cause.class() == dive_eval::ErrorClass::Eval
        && cause.ec() == dive_eval::Ec::General
        && !cause.is_eval_exception())
    .then_some(cause)
}

fn temporal_evaluation(failure: &crate::temporal::Failure) -> Option<&dive_eval::EvalError> {
    failure
        .capture_row_error()
        .map(|(cause, _, _)| cause)
        .or_else(|| temporal_action_evaluation(failure))
        .or_else(|| temporal_planning_evaluation(failure.error()))
}

fn temporal_startup_tautology(failure: &crate::temporal::Failure) -> bool {
    matches!(
        failure.error(),
        crate::temporal::Error::Analysis(
            dive_liveness::analyze::AnalysisError::StartupTautology { .. }
        )
    )
}

fn temporal_failure_kind(failure: &crate::temporal::Failure) -> &'static str {
    use crate::temporal::Error;
    use dive_liveness::analyze::AnalysisError;
    use dive_liveness::formula::FormulaError;
    use dive_liveness::plan::PlanningError;
    use dive_liveness::product::ProductError;
    if matches!(failure.error(), Error::Analysis(AnalysisError::Stopped(_))) {
        return "stopped";
    }
    if temporal_startup_tautology(failure) || failure.capture_row_error().is_some() {
        return "evaluation-error";
    }
    if let Some(action) = failure.action_evaluation() {
        return match action.cause().class() {
            dive_eval::ErrorClass::Eval if temporal_action_evaluation(failure).is_some() => {
                "evaluation-error"
            }
            dive_eval::ErrorClass::Eval | dive_eval::ErrorClass::Unsupported => "unsupported",
            dive_eval::ErrorClass::ResourceLimit => "resource-limit",
            dive_eval::ErrorClass::Bug | dive_eval::ErrorClass::Interrupted => "internal-error",
        };
    }
    match failure.error() {
        Error::Analysis(AnalysisError::Exploration(result))
            if matches!(result.verdict, Verdict::BudgetMeasurement { .. }) =>
        {
            "internal-error"
        }
        Error::Analysis(AnalysisError::Planning(PlanningError::Evaluation { .. })) => {
            "evaluation-error"
        }
        Error::NativeValidation { report, .. }
        | Error::Validation { report, .. }
        | Error::PrefixValidation { report, .. }
        | Error::EndpointValidation { report }
            if matches!(report.validation, dive_trace::Validation::Invalid(_)) =>
        {
            "internal-error"
        }
        Error::Thread(_)
        | Error::Document(_)
        | Error::Analysis(
            AnalysisError::Inconsistent(_)
            | AnalysisError::CaptureThread(_)
            | AnalysisError::BudgetStartup(_)
            | AnalysisError::BudgetMeasurement(_)
            | AnalysisError::Tableau(FormulaError::Reference(_))
            | AnalysisError::Planning(PlanningError::Formula {
                error: FormulaError::Reference(_),
                ..
            }),
        ) => "internal-error",
        Error::Binding(f) => match f.error.kind {
            dive_model::ErrorKind::Bug => "internal-error",
            dive_model::ErrorKind::Tlc(_) | dive_model::ErrorKind::Eval { .. } => "model-error",
            dive_model::ErrorKind::Unsupported { .. } => "unsupported",
        },
        Error::ValidationResource(_)
        | Error::Analysis(
            AnalysisError::Product(ProductError::Limit(_))
            | AnalysisError::Limit(_)
            | AnalysisError::Tableau(FormulaError::Limit(_))
            | AnalysisError::Planning(PlanningError::Formula {
                error: FormulaError::Limit(_),
                ..
            }),
        ) => "resource-limit",
        _ => "unsupported",
    }
}

fn temporal_native_complete(
    report: &crate::temporal::Report,
) -> Option<&dive_runtime::capture::CompleteEvidence> {
    match report.outcome() {
        crate::temporal::Outcome::NativeHolds(evidence) => Some(evidence),
        crate::temporal::Outcome::NativeViolation(word) => Some(word.evidence()),
        _ => None,
    }
}

fn producer_facts(
    result: &CheckResult,
    stats: dive_runtime::Stats,
) -> dive_liveness::analyze::ExplorationEvidence {
    dive_liveness::analyze::ExplorationEvidence {
        stats,
        workers: result.workers.clone(),
        collision: result.collision.clone(),
        output: result.output.clone(),
        warnings: result.warnings.clone(),
    }
}

fn temporal_evidence(
    report: &crate::temporal::Report,
) -> Option<dive_liveness::analyze::ExplorationEvidence> {
    if let Some(complete) = temporal_native_complete(report) {
        return Some(producer_facts(complete.safety_result(), complete.stats()));
    }
    if let crate::temporal::Outcome::NativePrefixViolation(word) = report.outcome() {
        return Some(producer_facts(
            word.evidence().safety_result(),
            word.evidence().stats(),
        ));
    }
    None
}

/// The store of the exploration behind a temporal report.
fn temporal_store(report: &crate::temporal::Report) -> Option<dive_store::StoreMode> {
    if let Some(complete) = temporal_native_complete(report) {
        return Some(complete.safety_result().store);
    }
    if let crate::temporal::Outcome::NativePrefixViolation(word) = report.outcome() {
        return Some(word.evidence().safety_result().store);
    }
    None
}

fn temporal_word(report: &crate::temporal::Report) -> Option<(&str, &dive_trace::TraceDoc, usize)> {
    match report.outcome() {
        crate::temporal::Outcome::NativeViolation(word) => Some((
            &word.word().target_property,
            word.document(),
            word.word().back_to,
        )),
        crate::temporal::Outcome::NativePrefixViolation(word) => Some((
            &word.word().target_property,
            word.document(),
            word.word().back_to,
        )),
        crate::temporal::Outcome::NativeHolds(_) | crate::temporal::Outcome::Safety(_) => None,
    }
}

fn periodic_json(checks: dive_liveness::analyze::periodic::Checks) -> Value {
    json!({"min_states": checks.schedule.min_states,
        "growth_factor": checks.schedule.growth_factor,
        "prefix_checks": checks.prefixes, "last_level": checks.last_level, "last_size": checks.last_size,
        "exhausted": checks.exhausted, "final_check": checks.final_check,
        "bounds_scope": "capture cumulative; label and product per property per check; no total-run work or byte cap"})
}

const TEMPORAL_NOTE: &str = "Bounded short-lasso analysis: TLC-exact lasso selection and periodic timing are not claimed. Only independently validated periodic counterexamples are returned; prefix statistics describe the actual committed boundary.";

fn temporal_evidence_json(
    doc: &mut Value,
    capture: &dive_liveness::analyze::ExplorationEvidence,
    tlc_fingerprints: bool,
) {
    let s = &capture.stats;
    doc["statistics"] = json!({
        "generated": s.generated, "distinct": s.distinct,
        "left_on_queue": s.left_on_queue, "depth": s.depth,
        "initial_generated": s.initial_generated, "reported_by_tlc": false,
    });
    doc["output"] = json!(capture.output);
    doc["warnings"] = json!(
        capture
            .warnings
            .iter()
            .map(|(code, message)| { json!({"code": code, "message": message}) })
            .collect::<Vec<_>>()
    );
    if let Some(collision) = &capture.collision {
        doc["collision"] = json!({
            "store": "fingerprint", "scheme": if tlc_fingerprints {"tlc"} else {"dive"},
            "optimistic": collision.optimistic, "actual": collision.actual,
            "assumption": "fingerprints behave as random 64-bit values (TLC's assumption too)",
        });
    }
}

fn temporal_json(doc: &mut Value, report: &crate::temporal::Report, tlc_fingerprints: bool) {
    let Some(evidence) = temporal_evidence(report) else {
        doc["verdict"] = json!("internal-error");
        doc["message"] = json!("safety stop was not routed through the safety report");
        return;
    };
    let complete = temporal_native_complete(report).is_some();
    doc["execution"] = json!({"stage": if complete {"complete"} else {"prefix"}, "capture_completed": complete, "exhausted": complete});
    if let crate::temporal::Outcome::NativePrefixViolation(word) = report.outcome() {
        doc["execution"]["level"] = json!(word.evidence().level());
    }
    if let Some(checks) = report.periodic() {
        doc["periodic"] = periodic_json(checks);
    }
    temporal_evidence_json(doc, &evidence, tlc_fingerprints);
    doc["notes"] = json!([TEMPORAL_NOTE]);
    doc["elapsed_ms"] = json!(u64::try_from(report.elapsed().as_millis()).unwrap_or(u64::MAX));
    if let Some((_, document, back_to)) = temporal_word(report) {
        doc["property"] = json!(temporal_property_names(&document.properties));
        doc["properties"] = json!(document.properties);
        doc["counterexample"] = json!({
            "length": document.states.len(), "ending": "lasso", "back_to": back_to + 1,
            "states": document.states.iter().enumerate().map(|(i,s)| json!({"index":i+1,"label":null,"action":null,"values":s})).collect::<Vec<_>>(),
            "trace": document.to_json(), "validation": "valid",
        });
    }
}

// Pinned TLC MP::TLC_TEMPORAL_PROPERTY_VIOLATED names the properties false
// on the actual printed word, in configured order.
fn temporal_property_names(names: &[String]) -> String {
    match names.split_last() {
        None => String::new(),
        Some((last, [])) => last.clone(),
        Some((last, [first])) => format!("{first} and {last}"),
        Some((last, rest)) => format!("{}, and {last}", rest.join(", ")),
    }
}

fn temporal_human(report: &crate::temporal::Report) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    let Some(complete) = temporal_evidence(report) else {
        return "internal-error: unrouted temporal safety stop; no verdict produced\n".to_owned();
    };
    for (_, warning) in &complete.warnings {
        let _ = writeln!(out, "Warning: {warning}");
    }
    for line in &complete.output {
        let _ = writeln!(out, "{line}");
    }
    if let Some(reason) = &complete.workers.reason {
        let _ = writeln!(out, "{reason}");
    }
    if matches!(report.outcome(), crate::temporal::Outcome::NativeHolds(_)) {
        let _ = writeln!(
            out,
            "Complete final temporal analysis: no error has been found."
        );
    } else if let Some((_, document, back_to)) = temporal_word(report) {
        let names = temporal_property_names(&document.properties);
        if document.properties.len() == 1 {
            let _ = writeln!(out, "Temporal property {names} was violated.");
        } else {
            let _ = writeln!(out, "Temporal properties {names} were violated.");
        }
        let _ = writeln!(out, "Independently validated counterexample:");
        for (i, state) in document.states.iter().enumerate() {
            let _ = writeln!(out, "State {}: {}", i + 1, json!(state));
        }
        let _ = writeln!(out, "Back to state {}.", back_to + 1);
    }
    if let Some(checks) = report.periodic() {
        let _ = writeln!(
            out,
            "Deterministic liveness: {} prefix checks, final check {}, exhausted {}.",
            checks.prefixes, checks.final_check, checks.exhausted
        );
    }
    let s = &complete.stats;
    let _ = writeln!(
        out,
        "{} states generated, {} distinct states, depth {}.",
        s.generated, s.distinct, s.depth
    );
    // The store, not the absence of collision figures, decides the claim:
    // Core computes no collision probability for temporal runs (ADR 0158).
    match (temporal_store(report), &complete.collision) {
        (Some(dive_store::StoreMode::Exact), _) => {
            let _ = writeln!(out, "Exact full-state storage.");
        }
        (_, Some(c)) => {
            let _ = writeln!(
                out,
                "Fingerprint collision probability (random 64-bit assumption): optimistic {}, actual {}.",
                c.optimistic,
                c.actual.as_deref().unwrap_or("not reported")
            );
        }
        (_, None) => {
            let _ = writeln!(
                out,
                "Fingerprint storage (64-bit); collision probability not computed."
            );
        }
    }
    let _ = writeln!(out, "{TEMPORAL_NOTE}");
    out
}

fn temporal_limits_json(configuration: crate::temporal::Configuration) -> Value {
    use dive_liveness::analyze::GraphLabelPolicy;
    use dive_liveness::product::AcceptancePolicy;
    let limits = configuration.limits;
    let graph_labels = match configuration.graph_labels {
        GraphLabelPolicy::Cells(cells) => json!({"kind": "cells", "cells": cells}),
        GraphLabelPolicy::NoCellLimit => json!({"kind": "run-budgets"}),
    };
    let (acceptance, bytes, work) = match configuration.acceptance {
        AcceptancePolicy::Cells => (
            json!({"kind": "cells", "cells": limits.product.acceptance_cells}),
            None,
            None,
        ),
        AcceptancePolicy::Buffers { bytes, work } => (
            json!({"kind": "buffers", "bytes": bytes, "materialization_work": work}),
            Some(bytes),
            Some(work),
        ),
    };
    json!({
        "kind": "entry-work-counts-and-acceptance-buffers", "states": limits.capture.states,
        "edges": limits.capture.edges, "generated": limits.capture.generated,
        "successors_per_unit": limits.capture.successors_per_unit,
        "max_expansion_units": dive_runtime::capture::MAX_EXPANSION_UNITS,
        "properties": limits.properties,
        "cell_limits": {
            "graph_labels": graph_labels,
            "attribution_rows": {"kind": "cells", "cells": limits.atom_cells},
            "word_truth": {"kind": "cells", "cells": limits.atom_cells},
        },
        "formula_nodes": limits.formula.formula_nodes,
        "tableau_nodes": limits.formula.tableau_nodes,
        "formula_steps": limits.formula.expansion_steps,
        "product_vertices": limits.product.vertices,
        "product_edges": limits.product.edges,
        "acceptance_buffer_bytes": bytes,
        "acceptance_materialization_work": work,
        "acceptance_policy": acceptance,
        "total_memory_budget": false,
        "product_steps": limits.product.steps,
    })
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic, reason = "test assertions")]
mod evidence_controls {
    use super::*;

    /// A Core report for `source` and `cfg` under `--compat=tlc`, from files
    /// in a fresh directory.
    fn core_check(source: &str, cfg: &str, req: CheckRequest) -> CheckReport {
        static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let directory = std::env::temp_dir().join(format!(
            "dive-evidence-controls-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        std::fs::create_dir_all(&directory).expect("directory");
        let file = directory.join("M.tla");
        std::fs::write(&file, source).expect("module");
        std::fs::write(file.with_extension("cfg"), cfg).expect("configuration");
        let report = check_file_in(
            &CheckRequest {
                file,
                compat_tlc: true,
                seed: Some(0),
                workers: Some(1),
                ..req
            },
            None,
            false,
            None,
        )
        .expect("report");
        let _ = std::fs::remove_dir_all(&directory);
        report
    }

    #[test]
    fn terminal_sampling_preserves_completed_progress_without_a_proof() {
        let source = "---- MODULE M ----\nVARIABLE x\nInit == x = FALSE\nNext == x' = ~x\nP == []<>(x \\/ ~x)\n====\n";
        for finished in [
            Err(dive_runtime::budget::MeasurementError::unavailable(
                dive_runtime::budget::SamplePoint::Final,
            )),
            Ok(Some(dive_runtime::budget::Exhausted {
                resource: dive_runtime::budget::Resource::Memory,
                limit: 100,
                observed: 101,
                elapsed_ms: 2,
            })),
        ] {
            let mut report = core_check(
                source,
                "INIT Init\nNEXT Next\nPROPERTY P\n",
                CheckRequest::default(),
            );
            assert_eq!(report.verdict(), "success", "{}", report.to_json());
            report.finish_budget(finished, Duration::from_millis(2));
            let json = report.to_json();
            assert_eq!(json["execution"]["stage"], "finalization", "{json}");
            assert_eq!(json["execution"]["capture_completed"], true);
            assert_eq!(json["statistics"]["distinct"], 2);
            assert_eq!(json["statistics"]["generated"], 3);
            assert_eq!(json["statistics"]["depth"], 2);
            assert_eq!(json["statistics"]["exhaustive"], false);
            assert!(json["tlc"].is_null());
            assert!(json["counterexample"].is_null());
            if finished.is_err() {
                assert_eq!(json["verdict"], "internal-error");
                assert_eq!(
                    json["diagnostics"][0]["code"],
                    dive_runtime::budget::MEASUREMENT_FAILURE_CODE
                );
                assert!(json["stopped"].is_null());
            } else {
                assert_eq!(json["verdict"], "stopped");
                assert_eq!(json["stopped"]["phase"], "finalization");
                assert_eq!(json["stopped"]["observed"], 101);
            }
        }
    }

    #[test]
    fn a_safety_measurement_error_has_actionable_help_without_an_invented_span() {
        let mut report = core_check(
            "---- MODULE M ----\nVARIABLE x\nInit == x = FALSE\nNext == x' = ~x\n====\n",
            "INIT Init\nNEXT Next\n",
            CheckRequest::default(),
        );
        assert_eq!(report.verdict(), "success", "{}", report.to_json());
        report.finish_budget(
            Err(dive_runtime::budget::MeasurementError::unavailable(
                dive_runtime::budget::SamplePoint::Final,
            )),
            Duration::from_millis(2),
        );
        let json = report.to_json();
        assert_eq!(json["verdict"], "internal-error");
        assert_eq!(
            json["diagnostics"][0]["code"],
            dive_runtime::budget::MEASUREMENT_FAILURE_CODE
        );
        assert_eq!(json["diagnostics"][0]["cause"], json["message"]);
        assert!(
            json["diagnostics"][0]["help"]
                .as_str()
                .is_some_and(|help| help.contains("--max-memory=none"))
        );
        assert!(json["diagnostics"][0]["span"].is_null());
        assert_eq!(json["statistics"]["exhaustive"], false);
        assert_eq!(report.exit_code(), exit::INTERNAL);
        assert!(json["tlc"].is_null());
        assert!(json["stopped"].is_null());
        assert!(json["counterexample"].is_null());
    }

    #[test]
    fn default_ceiling_resolution_failure_is_distinct_and_never_unlimited() {
        use dive_runtime::budget::{MeasurementKind, SamplePoint};
        let error = resolve_memory(BudgetSetting::Default, || None).expect_err("RAM unknown");
        assert_eq!(error.kind, MeasurementKind::DefaultCeiling);
        assert_eq!(error.point, SamplePoint::Initial);
        assert!(resolve_memory(BudgetSetting::Default, || Some(0)).is_err());
        assert_eq!(
            resolve_memory(BudgetSetting::Explicit(1), || None),
            Ok((Some(1), "explicit"))
        );
        assert_eq!(
            resolve_memory(BudgetSetting::Unlimited, || None),
            Ok((None, "none"))
        );
    }
}

#[cfg(test)]
mod binding_report_controls;

// Explanatory bound metadata only. It never supplies execution or proof facts.
fn symmetry_liveness_json(resolution: &dive_model::SymmetryLivenessResolution) -> Value {
    json!({
        "declared": resolution.declared(),
        "requested": resolution.requested().as_str(),
        "genuine_temporal": resolution.genuine_temporal(),
        "effective": resolution.effective(),
        "scope": if resolution.unreduced() { Some("whole-run") } else { None },
    })
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    reason = "finite configuration serialization assertions"
)]
mod product_vertex_report_controls {
    use super::*;

    #[test]
    fn actual_scalar_is_reported_without_changing_other_owner_settings() {
        let file = crate::temporal::file_configuration();
        assert_eq!(file.limits.product.vertices, usize::MAX);
        let mut legacy = file;
        legacy.limits = dive_liveness::analyze::Limits::default();
        assert_eq!(legacy.limits.product.vertices, 1_000_000);
        for vertices in [0, 1, 2, 1_000_000, usize::MAX] {
            let mut configured = legacy;
            configured.limits.product.vertices = vertices;
            let actual = temporal_limits_json(configured);
            assert_eq!(actual["product_vertices"], json!(vertices));
            let mut expected = temporal_limits_json(file);
            expected["product_vertices"] = json!(vertices);
            assert_eq!(actual, expected, "only the vertex scalar differs");
        }
    }
}