gpuviewer-tui 0.2.2

gpuviewer — the GPU flight recorder (terminal UI + --json streaming)
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
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
//! Collection engine shared by the TUI thread and `--json` mode.

use std::collections::HashMap;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use gpuviewer_core::{
    all_backends, normalize_pci_id, now_ms, Confidence, DeviceId, DynamicSample, Event,
    EventEngine, EventKind, GpuBackend, ProcessSample, Severity, StaticInfo,
};
use gpuviewer_history::{DataSource, HistoryStore, Recorder, SqliteStore, WriteTransition};
use serde::Serialize;

/// One collection tick's output for one device.
#[derive(Serialize)]
pub struct FrameDevice {
    pub id: DeviceId,
    pub name: String,
    /// Total device memory, so JSON consumers can compute used/total without a second
    /// query. VRAM on discrete boards; a working-set budget on unified-memory devices
    /// (the spec and the per-device `source_caveat` carry that label).
    pub mem_total_bytes: Option<u64>,
    pub sample: Option<DynamicSample>,
    /// `None` when the process probe FAILED this tick — not the same as `Some(vec![])`,
    /// which asserts the device genuinely had nothing attached. Consumers must render or
    /// serialize the two differently; an empty list stands for an observed emptiness.
    pub processes: Option<Vec<ProcessSample>>,
}

/// One collection tick across all devices. Internal shape only: `--json` serializes the
/// envelope structs in `main.rs` instead (events go out as separate lines, per
/// docs/spec/ndjson-v1.md), so this deliberately does not derive `Serialize`.
pub struct Frame {
    pub ts_ms: u64,
    pub devices: Vec<FrameDevice>,
    pub events: Vec<Event>,
}

// `normalize_pci_id` (cross-backend dedupe key) moved to `gpuviewer-core::model` per
// design cross-platform.md §5.4, so the Windows backends' LUID↔PCI matching and this
// collector share one rule; re-exported from core and imported above.

/// How long a between-tick gap must reach before the next tick narrates a `CollectorStall`:
/// the larger of three intervals or five seconds. A blocked backend probe leaves a hole in
/// the recording, and the flight recorder must say so rather than let the gap read as the
/// GPU having gone quiet. Pure (no clock, no I/O) so the threshold is unit-tested directly.
pub fn stall_threshold(interval: Duration) -> Duration {
    (interval * 3).max(Duration::from_secs(5))
}

/// Whether an observed inter-tick `gap` is long enough to count as a stall, given the
/// configured `interval`. Factored out so the boundary (exactly `3×interval` vs. just over)
/// is testable without spinning a real tick loop.
pub fn is_stall(gap: Duration, interval: Duration) -> bool {
    gap > stall_threshold(interval)
}

/// Consecutive panicked ticks the collector survives before declaring itself dead. One
/// panicked tick is dropped-frame weather (a decoder edge case on one bad sample — the
/// same per-tick family as `NVML_ERROR_NOT_SUPPORTED` or an absent sysfs file); this many
/// in a row is a deterministic fault, and retrying forever would narrate an event per tick
/// while burning a core on a known-broken probe.
pub const MAX_CONSECUTIVE_PANICS: u32 = 3;

/// Consecutive failed dynamic probes (`refresh_dynamic` → `Err`) before a device is
/// declared LOST. Per-metric absence never reaches this counter — `NOT_SUPPORTED` and a
/// missing sysfs file are `None` INSIDE an `Ok` sample (normal weather, rendered
/// "unavailable"); an `Err` is the whole probe producing nothing, the trait's documented
/// "device fell off the bus" shape. One or two of those are still transient (a driver
/// mid-reset drops a few queries and recovers); this many in a row is the device being
/// gone, and the audit's silently-vanishing-device hole (nvtop #459 is the genre's
/// cautionary tale: a device disappearing mid-run must be narrated, not crashed on or
/// ignored) demands the recorder say so as a fact.
///
/// The threshold counts TICKS, not wall-time — deterministic and testable. At the default
/// 1s interval five misses ≈ 5s of silence, matching the 5s floor of [`stall_threshold`]:
/// the recorder's established line for "this gap is worth narrating". A failing probe
/// pins full cadence ([`device_is_idle`] treats `sample: None` as not-idle), so after the
/// first failure the remaining misses are spaced at the configured interval; only the gap
/// *into* the first failure can be backoff-stretched. The event's evidence therefore
/// reports the measured wall-time alongside the tick count rather than assuming one from
/// the other.
pub const DEVICE_LOST_AFTER_FAILED_PROBES: u32 = 5;

/// Per-device dynamic-probe health, parallel to `Engine::devices` (collection order) —
/// the state behind the `device_lost`/`device_returned` facts.
#[derive(Clone, Default)]
struct ProbeHealth {
    /// Consecutive `refresh_dynamic` failures; any success resets it.
    consecutive_failures: u32,
    /// Unix-millis of the current failing streak's first failure (valid while
    /// `consecutive_failures > 0`) — the basis of the measured-wall-time evidence.
    first_fail_ms: u64,
    /// First and last error strings of the current streak (often identical — the
    /// evidence dedupes them, but a cause that *changes* mid-streak is worth keeping).
    first_error: String,
    last_error: String,
    /// `ts_ms` of the last successful sample; `None` until the device first answers.
    last_good_ms: Option<u64>,
    /// `Some(unix ms when declared)` while the device is lost; cleared on return.
    lost_at_ms: Option<u64>,
}

/// What one guarded tick ([`Engine::tick_guarded`]) produced. `Frame` is the normal path;
/// `Panicked` means the tick body panicked and its frame is lost — the carried event
/// narrates that hole (already persisted and `--on-event`-fired where those channels
/// survived), and `fatal` tells the caller whether the panic budget is spent and the
/// loop must stop, loudly.
pub enum TickOutcome {
    Frame(Frame),
    Panicked {
        /// The `CollectorStall` fact narrating this panic.
        event: Event,
        /// One-line panic message, for [`Shared::stopped`] and the stderr trail.
        summary: String,
        /// True once [`MAX_CONSECUTIVE_PANICS`] ticks panicked consecutively: the caller
        /// must stop collecting and say so — never keep rendering as if live.
        fatal: bool,
    },
}

/// Best-effort one-liner from a panic payload: `panic!`, `assert!`, and the built-in
/// index/overflow panics all carry `&str` or `String`; anything else is named rather
/// than displayed — the message must never be swallowed entirely.
fn panic_summary(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(s) = payload.downcast_ref::<&str>() {
        (*s).to_string()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "non-string panic payload".into()
    }
}

/// The `device_lost` fact. Severity is CRITICAL — the per-device analog of the
/// collector's own Critical stop fact: this device's recording ends here (until a
/// return), and a device vanishing under load almost always took its workload with it,
/// which is exactly the incident a flight recorder exists to capture. Confidence is
/// FACT: the claim is precisely "N consecutive probes produced nothing", which was
/// observed; the CAUSE is deliberately not asserted — driver reset, device removal, and
/// library death are indistinguishable from this side of the probe, and a guessed cause
/// would break the two-tier honesty contract.
fn device_lost_event(di: usize, id: &DeviceId, h: &ProbeHealth, at_ms: u64) -> Event {
    let wall = Duration::from_millis(at_ms.saturating_sub(h.first_fail_ms));
    let last_good = match h.last_good_ms {
        Some(ts) => format!("last good data {}", fmt_clock(ts)),
        None => "it never answered this session".to_string(),
    };
    let last_good_ev = match h.last_good_ms {
        Some(ts) => format!("last good sample at {ts} ms ({})", fmt_clock(ts)),
        None => "no good sample this session".to_string(),
    };
    let errors = if h.first_error == h.last_error {
        format!("error: {}", h.last_error)
    } else {
        format!(
            "first error: {}; last error: {}",
            h.first_error, h.last_error
        )
    };
    Event {
        ts_ms: at_ms,
        device: id.clone(),
        kind: EventKind::DeviceLost,
        severity: Severity::Critical,
        confidence: Confidence::Fact,
        title: format!(
            "GPU{di} stopped answering — device lost after {} consecutive failed probes; \
             {last_good}",
            h.consecutive_failures
        ),
        evidence: format!(
            "refresh_dynamic for {id} failed {} consecutive ticks over {} of wall time \
             (the threshold counts ticks; a failing probe pins full cadence, so only the \
             gap into the first failure can be backoff-stretched); {last_good_ev}; \
             {errors}; cause not asserted — driver reset, device removal, and library \
             death look identical from here",
            h.consecutive_failures,
            fmt_dur(wall),
        ),
    }
}

/// The `device_returned` fact — the recovery edge, so the story has both ends. Severity
/// is INFO: good news plainly stated (the Critical loss already alerted; alarming again
/// on recovery would teach users to tune the feed out). Still a FACT: "the probe
/// succeeded again" is observed.
fn device_returned_event(
    di: usize,
    id: &DeviceId,
    h: &ProbeHealth,
    lost_at_ms: u64,
    ts_ms: u64,
) -> Event {
    // The hole as the user experienced it runs from the last GOOD data, not from the
    // (later) loss declaration — the data went quiet before the verdict did.
    let gap_start = h.last_good_ms.unwrap_or(h.first_fail_ms);
    let gap = Duration::from_millis(ts_ms.saturating_sub(gap_start));
    Event {
        ts_ms,
        device: id.clone(),
        kind: EventKind::DeviceReturned,
        severity: Severity::Info,
        confidence: Confidence::Fact,
        title: format!(
            "GPU{di} answering again — device returned; {} of data are missing",
            fmt_dur(gap)
        ),
        evidence: format!(
            "refresh_dynamic for {id} succeeded after {} consecutive failures; declared \
             lost at {}; nothing was collected between {} and {} — the gap stays blank \
             in history, never zero-filled",
            h.consecutive_failures,
            fmt_clock(lost_at_ms),
            fmt_clock(gap_start),
            fmt_clock(ts_ms),
        ),
    }
}

/// The `recording_started` fact — the session-boundary mark on the recording's left edge.
/// WHY: the timeline renders unrecorded time blank, and without boundary marks a blank
/// stretch is ambiguous — "the GPU sat idle from 02:00–08:00" and "gpuviewer wasn't
/// running" look identical, an honesty hole for a product whose thesis is the trustworthy
/// recording. Info severity (routine lifecycle, not an alarm), FACT (the session opening
/// is observed). `dangling_start_ms` is the previous session's start mark when it has no
/// matching stop: that session died without flushing (crash, kill, power loss), and the
/// asymmetry is itself flight-recorder information — narrated here because the dead
/// session, by definition, could not narrate it.
/// Narrate a persistence edge as a fact. The recorder deliberately never propagates a
/// write error (a full disk must not take the live view down), but the silence that used
/// to accompany that was a promise the product could not keep: "scroll back to 02:14"
/// fails badly if the last hour was never written and nothing said so.
fn recording_degraded_event(ts_ms: u64, anchor: DeviceId, t: &WriteTransition) -> Event {
    let (severity, title, evidence) = match t {
        WriteTransition::Degraded { error } => (
            Severity::Critical,
            "recording degraded — history writes are failing".to_string(),
            format!(
                "the live view keeps running, but nothing is reaching the history \
                 database, so this period will NOT be replayable; first error: {error}"
            ),
        ),
        WriteTransition::Recovered { lost } => (
            Severity::Warning,
            format!("recording resumed — {lost} write(s) were lost"),
            format!(
                "history writes are succeeding again; the {lost} write(s) that failed are \
                 gone and that span stays blank in replay rather than being backfilled"
            ),
        ),
    };
    Event {
        ts_ms,
        device: anchor,
        kind: EventKind::RecordingDegraded,
        severity,
        confidence: Confidence::Fact,
        title,
        evidence,
    }
}

fn recording_started_event(
    ts_ms: u64,
    anchor: DeviceId,
    interval: Duration,
    backend_list: &str,
    n_devices: usize,
    db_name: &str,
    dangling_start_ms: Option<u64>,
) -> Event {
    let title = match dangling_start_ms {
        Some(_) => "recording started — previous session ended without a stop mark \
                    (crash, kill, or power loss)"
            .to_string(),
        None => format!(
            "recording started — gpuviewer {}",
            env!("CARGO_PKG_VERSION")
        ),
    };
    let devices_word = if n_devices == 1 { "device" } else { "devices" };
    let mut evidence = format!(
        "gpuviewer {}; interval {}; backends: {backend_list} ({n_devices} {devices_word}); \
         db {db_name}",
        env!("CARGO_PKG_VERSION"),
        fmt_dur(interval),
    );
    if let Some(prev) = dangling_start_ms {
        evidence.push_str(&format!(
            "; the previous session's start mark at {} has no matching stop — it died \
             without flushing (crash, kill, or power loss), so where its recording \
             actually ends is unknown and the gap size is unknowable",
            fmt_stamp(prev)
        ));
    }
    Event {
        ts_ms,
        device: anchor,
        kind: EventKind::RecordingStarted,
        severity: Severity::Info,
        confidence: Confidence::Fact,
        title,
        evidence,
    }
}

/// Wall-clock time the OS did not run us across one inter-tick gap (wall advance minus
/// monotonic advance) before it is narrated as a suspend. NTP slews, never steps, under
/// normal operation, and a scheduling hiccup shows up in BOTH clocks — so a divergence
/// this large is the host sleeping, or a manual/forward wall-clock step. The narration is
/// `Likely` for exactly that second possibility.
pub const SUSPEND_MIN_MS: u64 = 30_000;

/// The `host_suspended` inference: the machine slept between two ticks. Info — a laptop
/// lid closing is normal life, not a fault — but it must be narrated, because the hole it
/// leaves on the timeline is otherwise indistinguishable from the GPU sitting idle, or
/// from gpuviewer not running.
fn host_suspended_event(
    prev_wall_ms: u64,
    now_wall_ms: u64,
    mono_gap: Duration,
    unseen_ms: u64,
    anchor: DeviceId,
) -> Event {
    let unseen = Duration::from_millis(unseen_ms);
    Event {
        ts_ms: now_wall_ms,
        device: anchor,
        kind: EventKind::HostSuspended,
        severity: Severity::Info,
        confidence: Confidence::Likely,
        title: format!(
            "host likely asleep for {} ({}–{}) — nothing was recorded; the gap is the \
             machine suspended, not the GPU idle",
            fmt_dur(unseen),
            fmt_clock(prev_wall_ms),
            fmt_clock(now_wall_ms),
        ),
        evidence: format!(
            "wall clock advanced {} between ticks while the monotonic clock advanced {}; the \
             {} difference is time the OS did not run gpuviewer — a system suspend, or a \
             forward wall-clock step (manual or NTP), which this check cannot tell apart",
            fmt_dur(Duration::from_millis(
                now_wall_ms.saturating_sub(prev_wall_ms)
            )),
            fmt_dur(mono_gap),
            fmt_dur(unseen),
        ),
    }
}

/// The user paused collection with `p`: a deliberate hole, narrated so the timeline's
/// blank stretch carries its cause. Reuses `collector_stall` (it asserts exactly "the
/// recording has a hole") at Info severity — no new wire kind.
fn paused_event(paused_at_ms: u64, now_ms: u64, anchor: DeviceId) -> Event {
    let dur = Duration::from_millis(now_ms.saturating_sub(paused_at_ms));
    Event {
        ts_ms: now_ms,
        device: anchor,
        kind: EventKind::CollectorStall,
        severity: Severity::Info,
        confidence: Confidence::Fact,
        title: format!(
            "collection was paused for {} (since {}) — the recording has a hole there",
            fmt_dur(dur),
            fmt_clock(paused_at_ms)
        ),
        evidence: format!(
            "the user paused collection at {} and resumed at {}; no samples were taken \
             in between",
            fmt_stamp(paused_at_ms),
            fmt_stamp(now_ms)
        ),
    }
}

/// Repeats of the same Xid code on the same device inside this window are folded into a
/// count carried by the next narration.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const XID_REPEAT_WINDOW_MS: u64 = 60_000;

/// Xid codes NVIDIA documents as fatal to the running context or the device (fallen off
/// the bus, double-bit ECC, uncontained ECC, NVLink failure, GSP timeouts, row-remap
/// failure). Severity is presentation only; the narration quotes the driver either way.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const XID_CRITICAL: &[u32] = &[48, 64, 74, 79, 95, 119, 120, 154];

/// The `xid_error` fact: the driver's own words, attributed, never reinterpreted.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn xid_event(
    di: usize,
    id: &DeviceId,
    x: &gpuviewer_core::kmsg::XidRecord,
    folded: u32,
    at_ms: u64,
) -> Event {
    // Lift the optional `pid=…, name=…` prefix out of the description for the title.
    let mut pid = None;
    let mut pname = None;
    let mut rest = x.detail.as_str();
    loop {
        let r = rest.trim_start();
        if let Some(v) = r.strip_prefix("pid=") {
            let (val, tail) = v.split_once(',').unwrap_or((v, ""));
            pid = Some(val.trim().to_string());
            rest = tail;
        } else if let Some(v) = r.strip_prefix("name=") {
            let (val, tail) = v.split_once(',').unwrap_or((v, ""));
            pname = Some(val.trim().to_string());
            rest = tail;
        } else {
            rest = r;
            break;
        }
    }
    let who = match (pname, pid) {
        (Some(n), Some(p)) => format!(" ({n} pid {p})"),
        (None, Some(p)) => format!(" (pid {p})"),
        _ => String::new(),
    };
    let desc = if rest.is_empty() {
        String::new()
    } else {
        format!(": {rest}")
    };
    Event {
        ts_ms: at_ms,
        device: id.clone(),
        kind: EventKind::XidError,
        severity: if XID_CRITICAL.contains(&x.code) {
            Severity::Critical
        } else {
            Severity::Warning
        },
        confidence: Confidence::Fact,
        title: format!("GPU{di} driver reported Xid {}{desc}{who}", x.code),
        evidence: format!(
            "kernel log: {}{}",
            x.raw,
            if folded > 0 {
                format!(
                    " — {folded} earlier repeat{} of this Xid in the previous minute folded",
                    if folded == 1 { "" } else { "s" }
                )
            } else {
                String::new()
            }
        ),
    }
}

/// A single backend `refresh_dynamic` probe taking longer than this is worth a one-line
/// Info note ("nvidia probe took 1.2s — driver slow to respond"): NVML/sysfs calls can block
/// (PCIe-throughput queries, a sleeping GPU waking) and a slow probe foreshadows a stall.
const SLOW_PROBE: Duration = Duration::from_millis(700);

/// Rate-limit the per-device slow-probe note: at most one per this window per device, so a
/// persistently-slow driver does not flood the story feed.
const SLOW_PROBE_COOLDOWN: Duration = Duration::from_secs(300);

/// Configuration for [`Engine::new`]. Carries the knobs the collector needs that are not
/// derivable from the backend set: whether to force the mock, where (if anywhere) to persist,
/// the configured tick interval (for the stall threshold), and an optional `--on-event` sink.
pub struct EngineConfig {
    /// Use ONLY the simulated GPUs (the mock is the no-real-GPU fallback regardless).
    pub force_mock: bool,
    /// Open the SQLite store and record. Off → live-only, the monitor still runs.
    pub persist: bool,
    /// Override the history database path (else the per-OS default, mock-separated).
    pub db_path: Option<PathBuf>,
    /// The configured tick interval — the basis of the stall-gap threshold.
    pub interval: Duration,
    /// Shell command run for every emitted event (`sh -c CMD`; `cmd /C CMD` on Windows),
    /// or `None`.
    pub on_event: Option<String>,
}

impl Default for EngineConfig {
    fn default() -> Self {
        Self {
            force_mock: false,
            persist: false,
            db_path: None,
            interval: Duration::from_millis(1000),
            on_event: None,
        }
    }
}

pub struct Engine {
    backends: Vec<Box<dyn GpuBackend>>,
    /// (backend index, device id, static info)
    devices: Vec<(usize, DeviceId, StaticInfo)>,
    event_engine: EventEngine,
    /// SQLite recorder, present only when persistence opened successfully.
    recorder: Option<Recorder>,
    /// Path of the open store, for the UI/replay (`None` when not persisting).
    db_path: Option<PathBuf>,
    /// The configured interval, for the stall threshold.
    interval: Duration,
    /// Monotonic instant the last tick finished — the basis for stall-gap detection.
    last_tick_end: Option<Instant>,
    /// Wall-clock (unix ms) of the same moment. The monotonic clock stops while the host
    /// is suspended (Linux `CLOCK_MONOTONIC`, macOS `CLOCK_UPTIME_RAW`) and the wall clock
    /// does not, so their divergence across one inter-tick gap is how a suspend is seen.
    last_tick_end_wall_ms: Option<u64>,
    /// The sleep the caller is about to take before the next tick (the adaptive backoff
    /// may stretch it to 5× the interval, capped at 10s). The stall threshold scales from
    /// THIS, not the configured interval: judging a deliberate 5s backoff sleep against
    /// the 1s interval's 5s threshold narrated a false "collection stalled" on every
    /// low-power tick.
    cadence: Duration,
    /// Unix ms the user paused collection (TUI `p`), until the next tick narrates it.
    paused_since_ms: Option<u64>,
    /// Kernel-log reader for NVIDIA Xid errors — Linux, a real NVIDIA device registered,
    /// and a readable `/dev/kmsg`; otherwise `None` (`gpuviewer doctor` explains why).
    #[cfg(target_os = "linux")]
    xid: Option<gpuviewer_core::kmsg::XidWatcher>,
    /// Per (device index, Xid code): when it was last narrated and how many repeats were
    /// folded since — a driver can log the same Xid many times a second.
    #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
    xid_last: HashMap<(usize, u32), (u64, u32)>,
    /// Last time we emitted a slow-probe note for a given device.
    last_slow_note: HashMap<DeviceId, Instant>,
    /// A pending `HistoryReset` to fold into the very first tick's events (the store reported
    /// it had to quarantine a corrupt file and start fresh — say so, once).
    pending_reset: Option<Event>,
    /// The session's `recording_started` mark, queued to ride the first tick's frame so the
    /// `--json` stream, `--on-event`, and the story feed all see it like any other event.
    /// It is ALSO inserted into the store at construction (the mark must exist even if the
    /// process dies before its first tick completes — a first probe can block for minutes);
    /// the event log's UNIQUE dedupe index collapses the first tick's re-insert.
    pending_session_start: Option<Event>,
    /// `--on-event` sink, shared with the JSON path so both modes fire it.
    sink: Option<EventSink>,
    /// Consecutive panicked ticks ([`Engine::tick_guarded`]); any clean tick resets it.
    consecutive_panics: u32,
    /// Per-device probe health (parallel to `devices`) — drives device-lost/returned.
    health: Vec<ProbeHealth>,
    /// Unix-millis the session began — the basis of the stop mark's duration evidence.
    session_start_ms: u64,
    /// Clean ticks folded this session, for the stop mark's evidence.
    frames_folded: u64,
    /// Latched once [`Engine::finish`] wrote the stop mark, so the explicit shutdown paths
    /// and `Drop` (which both call it) can never record the session's end twice.
    finished: bool,
}

impl Engine {
    /// Build the engine from a configuration. Device discovery and dedupe are unchanged from
    /// the live-only path; persistence is best-effort: a store that fails to open (disk full,
    /// permission) logs to stderr and the monitor continues WITHOUT a recorder — losing the
    /// recording is never worth crashing the live view.
    pub fn new(config: EngineConfig) -> Self {
        Self::with_backends(all_backends(config.force_mock), config)
    }

    /// [`Engine::new`] over an explicit backend set instead of the registry — the seam
    /// the panic-firewall tests inject a scripted panicking backend through. The registry
    /// path is exactly this with `all_backends`.
    pub(crate) fn with_backends(
        mut backends: Vec<Box<dyn GpuBackend>>,
        config: EngineConfig,
    ) -> Self {
        let mut devices = Vec::new();
        let mut event_engine = EventEngine::new();
        // Normalized PCI address → name of the backend that registered it first.
        let mut seen_pci: HashMap<String, &'static str> = HashMap::new();

        for (bi, b) in backends.iter_mut().enumerate() {
            for id in b.devices() {
                // Cross-backend dedupe by PCI address (settled CLAUDE.md decision). First
                // backend wins: registry order is nvidia → amd → intel, so the richest
                // source for a device is canonical.
                let pci_key = normalize_pci_id(&id.0);
                if let Some(key) = &pci_key {
                    if let Some(first) = seen_pci.get(key.as_str()) {
                        eprintln!(
                            "gpuviewer: {id} ({}) duplicates a device already registered by {first}; skipping",
                            b.name()
                        );
                        continue;
                    }
                }
                match b.static_info(&id) {
                    Ok(info) => {
                        if let Some(key) = pci_key {
                            seen_pci.insert(key, b.name());
                        }
                        event_engine.register_device(id.clone(), format!("GPU{}", devices.len()));
                        event_engine.register_pcie_max(
                            id.clone(),
                            info.pcie_max_gen,
                            info.pcie_max_width,
                        );
                        devices.push((bi, id, info));
                    }
                    Err(e) => {
                        eprintln!("gpuviewer: skipping {id} ({}): {e}", b.name());
                    }
                }
            }
        }

        let mock_in_use = backends.iter().any(|b| b.name() == "mock");

        // Open the store (best-effort) and register the discovered devices into it so a replay
        // session can label history even for a GPU later removed.
        //
        // The open is a RECORDING open claiming the data source the backends ACTUALLY are —
        // not what the flags said. main.rs preflights an explicit --db against the flags,
        // but the mock backend is also the automatic no-real-GPU fallback: that session
        // records simulated data into a database the flags called "real". Claiming here,
        // where the engine knows the truth, means a mismatched database gets ZERO writes —
        // not even the device-identity upserts below — instead of leaking rows before a
        // later check could refuse.
        let session_start_ms = now_ms();
        let mut recorder = None;
        let mut db_path = None;
        let mut pending_reset = None;
        let mut pending_session_start = None;
        if config.persist {
            let source = if mock_in_use {
                DataSource::Mock
            } else {
                DataSource::Real
            };
            let opened = match &config.db_path {
                Some(p) => SqliteStore::open_recording(p, source),
                None => SqliteStore::open_default(mock_in_use),
            };
            match opened {
                Ok((store, was_reset)) => {
                    db_path = Some(store.path().to_path_buf());
                    let mut rec = Recorder::new(store);
                    for (_, id, info) in &devices {
                        let _ = rec.store_mut().register_device(
                            id,
                            &info.name,
                            info.vendor,
                            info.mem_total_bytes,
                        );
                    }
                    if was_reset {
                        // Fold a one-shot HistoryReset into the first tick: the gap from a
                        // quarantined-and-recreated file must not masquerade as device idleness.
                        let path_note = db_path
                            .as_ref()
                            .map(|p| p.display().to_string())
                            .unwrap_or_default();
                        let anchor = devices
                            .first()
                            .map(|(_, id, _)| id.clone())
                            .unwrap_or_else(|| DeviceId("history".into()));
                        pending_reset = Some(Event {
                            ts_ms: now_ms(),
                            device: anchor,
                            kind: EventKind::HistoryReset,
                            severity: Severity::Warning,
                            confidence: Confidence::Fact,
                            title: "history file was corrupt — renamed aside and started fresh"
                                .into(),
                            evidence: format!(
                                "previous file kept at {path_note}.corrupt-<unix_seconds>; \
                                 a fresh database was created at {path_note}"
                            ),
                        });
                    }

                    // Session boundary: detect a dangling start mark BEFORE writing this
                    // session's own, then record `recording_started`. Unclean iff the
                    // newest start mark is strictly newer than every stop mark; a tie
                    // reads clean — a confidently-wrong "previous session crashed" would
                    // damage trust more than a missed one.
                    let prev_start = rec
                        .store()
                        .latest_event_ms(Some(EventKind::RecordingStarted))
                        .ok()
                        .flatten();
                    let prev_stop = rec
                        .store()
                        .latest_event_ms(Some(EventKind::RecordingStopped))
                        .ok()
                        .flatten();
                    let dangling = prev_start.filter(|s| *s > prev_stop.unwrap_or(0));
                    // Backends as the recording will actually see them: only those that
                    // registered at least one device, in collection order.
                    let mut backend_names: Vec<&'static str> = Vec::new();
                    for (bi, _, _) in &devices {
                        let n = backends[*bi].name();
                        if !backend_names.contains(&n) {
                            backend_names.push(n);
                        }
                    }
                    let backend_list = if backend_names.is_empty() {
                        "none".to_string()
                    } else {
                        backend_names.join(", ")
                    };
                    let db_name = db_path
                        .as_ref()
                        .and_then(|p| p.file_name())
                        .map(|n| n.to_string_lossy().into_owned())
                        .unwrap_or_else(|| "history.db".into());
                    let anchor = devices
                        .first()
                        .map(|(_, id, _)| id.clone())
                        .unwrap_or_else(|| DeviceId("collector".into()));
                    let start_ev = recording_started_event(
                        session_start_ms,
                        anchor,
                        config.interval,
                        &backend_list,
                        devices.len(),
                        &db_name,
                        dangling,
                    );
                    rec.record_events(std::slice::from_ref(&start_ev));
                    pending_session_start = Some(start_ev);
                    recorder = Some(rec);
                }
                Err(e) => {
                    eprintln!("gpuviewer: history persistence disabled (store open failed): {e}");
                }
            }
        }

        let sink = config.on_event.map(EventSink::new);
        let health = vec![ProbeHealth::default(); devices.len()];
        #[cfg(target_os = "linux")]
        let xid = (!mock_in_use
            && devices
                .iter()
                .any(|(_, _, i)| i.vendor == gpuviewer_core::Vendor::Nvidia))
        .then(gpuviewer_core::kmsg::XidWatcher::open)
        .and_then(Result::ok);

        Self {
            backends,
            devices,
            event_engine,
            recorder,
            db_path,
            interval: config.interval,
            last_tick_end: None,
            last_tick_end_wall_ms: None,
            cadence: config.interval,
            paused_since_ms: None,
            #[cfg(target_os = "linux")]
            xid,
            xid_last: HashMap::new(),
            last_slow_note: HashMap::new(),
            pending_reset,
            pending_session_start,
            sink,
            consecutive_panics: 0,
            health,
            session_start_ms,
            frames_folded: 0,
            finished: false,
        }
    }

    pub fn static_infos(&self) -> Vec<StaticInfo> {
        self.devices.iter().map(|(_, _, i)| i.clone()).collect()
    }

    /// Whether the data on screen is simulated — true when the mock backend is active
    /// (forced via `--mock` or registered as the no-real-GPU fallback; it is exclusive
    /// either way). The UI labels mock data as mock, and must never label live data so.
    pub fn mock_in_use(&self) -> bool {
        self.backends.iter().any(|b| b.name() == "mock")
    }

    /// The on-disk history path, for the UI/replay. `None` when not persisting (or the store
    /// failed to open).
    pub fn db_path(&self) -> Option<PathBuf> {
        self.db_path.clone()
    }

    pub fn tick(&mut self) -> Frame {
        let mut frame_devices = Vec::with_capacity(self.devices.len());
        let mut events = Vec::new();

        // Self-honesty about the time BETWEEN ticks: an unrecorded hole must be narrated,
        // never left to read as the GPU having gone quiet. (A probe blocking INSIDE a tick
        // is the slow-probe note below — it never shows up in this gap.)
        let now_instant = Instant::now();
        let now_wall = now_ms();
        if let Some(paused_at) = self.paused_since_ms.take() {
            // The user paused collection — a deliberate hole, narrated as such (Info, not
            // a stall warning). The suspend/stall checks are skipped for this one gap: it
            // is already explained.
            events.push(paused_event(paused_at, now_wall, self.anchor_device()));
        } else if let (Some(prev_end), Some(prev_wall)) =
            (self.last_tick_end, self.last_tick_end_wall_ms)
        {
            let gap = now_instant.duration_since(prev_end);
            let wall_gap_ms = now_wall.saturating_sub(prev_wall);
            let unseen_ms = wall_gap_ms.saturating_sub(gap.as_millis() as u64);
            if unseen_ms >= SUSPEND_MIN_MS {
                events.push(host_suspended_event(
                    prev_wall,
                    now_wall,
                    gap,
                    unseen_ms,
                    self.anchor_device(),
                ));
            } else if is_stall(gap, self.cadence.max(self.interval)) {
                let last_good = fmt_clock(now_wall.saturating_sub(gap.as_millis() as u64));
                let anchor = self.anchor_device();
                events.push(Event {
                    ts_ms: now_wall,
                    device: anchor,
                    kind: EventKind::CollectorStall,
                    severity: Severity::Warning,
                    confidence: Confidence::Fact,
                    title: format!(
                        "collection stalled {} — no frames were taken; the data gap is \
                         recorded, last good frame at {last_good}",
                        fmt_dur(gap)
                    ),
                    evidence: format!(
                        "inter-tick gap {} exceeds the {} stall threshold (cadence {}, \
                         interval {}); the collector was not scheduled — the process was \
                         stopped, starved of CPU, or blocked writing its output",
                        fmt_dur(gap),
                        fmt_dur(stall_threshold(self.cadence.max(self.interval))),
                        fmt_dur(self.cadence),
                        fmt_dur(self.interval),
                    ),
                });
            }
        }

        // A pending HistoryReset rides out on the first tick, before any device events.
        if let Some(reset) = self.pending_reset.take() {
            events.push(reset);
        }
        // The session's start mark rides the first tick too — already in the store (the
        // dedupe index swallows the re-insert below), but the stream, `--on-event`, and
        // the story feed only see events that travel in a frame.
        if let Some(start) = self.pending_session_start.take() {
            events.push(start);
        }

        for (di, (bi, id, info)) in self.devices.iter().enumerate() {
            let backend = &mut self.backends[*bi];
            let probe_start = Instant::now();
            // Keep the error, not just its absence: when failures accumulate into a
            // `device_lost` fact, the evidence must carry what the driver actually said.
            let (sample, probe_err) = match backend.refresh_dynamic(id) {
                Ok(s) => (Some(s), None),
                Err(e) => (None, Some(e.to_string())),
            };
            let probe_dur = probe_start.elapsed();
            // `None` = the process probe FAILED, which is not "no processes". Collapsing
            // the two (this was `.unwrap_or_default()`) let a single failed probe narrate
            // fact-grade process exits for everything the device was holding.
            let processes = backend.refresh_processes(id).ok();

            // Device-lost / returned edges — the audit's silently-vanishing-device hole:
            // a device that stops answering (driver reset, NVML dying, eGPU unplug, xe
            // rebind) must enter the recording as a fact with both edges, never vanish
            // or freeze silently. One failed probe is weather; the streak threshold and
            // its rationale live on [`DEVICE_LOST_AFTER_FAILED_PROBES`].
            let h = &mut self.health[di];
            match &sample {
                Some(s) => {
                    if let Some(lost_at) = h.lost_at_ms.take() {
                        events.push(device_returned_event(di, id, h, lost_at, s.ts_ms));
                    }
                    h.consecutive_failures = 0;
                    h.last_good_ms = Some(s.ts_ms);
                }
                None => {
                    let err = probe_err.unwrap_or_else(|| "unknown error".into());
                    h.consecutive_failures += 1;
                    if h.consecutive_failures == 1 {
                        h.first_fail_ms = now_ms();
                        h.first_error = err.clone();
                    }
                    h.last_error = err;
                    if h.lost_at_ms.is_none()
                        && h.consecutive_failures >= DEVICE_LOST_AFTER_FAILED_PROBES
                    {
                        let at = now_ms();
                        h.lost_at_ms = Some(at);
                        events.push(device_lost_event(di, id, h, at));
                    }
                }
            }

            // A slow probe foreshadows a stall — note it (Info, rate-capped per device).
            if probe_dur > SLOW_PROBE {
                let fresh = self
                    .last_slow_note
                    .get(id)
                    .map(|t| now_instant.duration_since(*t) >= SLOW_PROBE_COOLDOWN)
                    .unwrap_or(true);
                if fresh {
                    self.last_slow_note.insert(id.clone(), now_instant);
                    events.push(Event {
                        ts_ms: now_ms(),
                        device: id.clone(),
                        kind: EventKind::CollectorStall,
                        severity: Severity::Info,
                        confidence: Confidence::Fact,
                        title: format!(
                            "{} probe took {} — driver slow to respond",
                            backend.name(),
                            fmt_dur(probe_dur)
                        ),
                        evidence: format!(
                            "refresh_dynamic for {id} took {} (> {} threshold)",
                            fmt_dur(probe_dur),
                            fmt_dur(SLOW_PROBE)
                        ),
                    });
                }
            }

            if let Some(s) = &sample {
                events.extend(self.event_engine.observe(
                    id,
                    s,
                    processes.as_deref(),
                    info.mem_total_bytes,
                    info.temp_slowdown_c,
                ));
                // Fold this frame into the persistent rollups (best-effort: a disk error
                // never reaches here — the Recorder swallows it and the live view runs on).
                // An unobservable process list records the device sample but no process
                // rows: writing an empty set would persist "nothing was running" as if
                // observed, and replay would read it back as fact.
                if let Some(rec) = &mut self.recorder {
                    rec.observe(id, s, processes.as_deref().unwrap_or(&[]));
                }
            }
            frame_devices.push(FrameDevice {
                id: id.clone(),
                name: info.name.clone(),
                mem_total_bytes: info.mem_total_bytes,
                sample,
                processes,
            });
        }

        // Driver-logged Xid errors since the last tick, attributed by PCI address.
        #[cfg(target_os = "linux")]
        {
            let records = self.xid.as_mut().map(|w| w.poll()).unwrap_or_default();
            let ids: Vec<DeviceId> = self.devices.iter().map(|(_, id, _)| id.clone()).collect();
            events.extend(self.xid_events(&ids, records, now_ms()));
        }

        // Persistence edges: the recorder swallowed any write error above, so ask it
        // whether its ability to record just changed and narrate the edge. Done before
        // the events are persisted below, so the degraded fact is itself in the batch —
        // if THAT write also fails, the event still reaches the live story feed and
        // `--on-event`, which is the point.
        if let Some(rec) = &mut self.recorder {
            if let Some(t) = rec.take_write_transition() {
                if let Some((_, anchor, _)) = self.devices.first() {
                    events.push(recording_degraded_event(now_ms(), anchor.clone(), &t));
                }
            }
        }

        // Persist and side-channel the events derived this tick.
        if let Some(rec) = &mut self.recorder {
            rec.record_events(&events);
        }
        if let Some(sink) = &mut self.sink {
            for e in &events {
                sink.fire(e);
            }
        }

        self.last_tick_end = Some(Instant::now());
        self.last_tick_end_wall_ms = Some(now_ms());
        self.frames_folded += 1;

        Frame {
            ts_ms: now_ms(),
            devices: frame_devices,
            events,
        }
    }

    /// Test seam for the inter-tick stall-gap narration: inject the previous tick's end
    /// instant directly. `tick` measures the gap against `Instant::now()` with no clock
    /// abstraction, so without this seam the only way to drive the stall path would be a
    /// real 5s+ sleep — exactly the wall-clock flakiness the suite avoids. The injected
    /// instant is a genuine `Instant` (typically `now - gap`), so the production
    /// comparison code runs unmodified; nothing about the measurement is faked.
    #[cfg(test)]
    pub(crate) fn set_last_tick_end(&mut self, t: Instant) {
        self.last_tick_end = Some(t);
        // Keep the wall twin consistent with the injected monotonic gap, so stall tests
        // do not accidentally read as a suspend.
        let gap = Instant::now().saturating_duration_since(t).as_millis() as u64;
        self.last_tick_end_wall_ms = Some(now_ms().saturating_sub(gap));
    }

    /// Test seam for the suspend check: the previous tick's wall-clock end, set
    /// independently of the monotonic one (that divergence IS the suspend signal).
    #[cfg(test)]
    pub(crate) fn set_last_tick_end_wall(&mut self, ms: u64) {
        self.last_tick_end_wall_ms = Some(ms);
    }

    /// Tell the engine the sleep about to be taken before the next tick, so the stall
    /// threshold tracks the adaptive cadence instead of the configured interval.
    pub fn set_cadence(&mut self, cadence: Duration) {
        self.cadence = cadence;
    }

    /// The user paused collection: the next tick narrates the hole as a pause (Info)
    /// rather than a stall. Idempotent — the earliest pause instant is kept.
    pub fn note_paused(&mut self) {
        self.paused_since_ms.get_or_insert_with(now_ms);
    }

    /// Turn parsed Xid records into `xid_error` facts, attributing each to the device
    /// whose PCI address it names (an Xid for a device we do not monitor is dropped — the
    /// wire contract says every event's `device` names a frame device). Repeats of the same
    /// (device, code) within [`XID_REPEAT_WINDOW_MS`] are folded and counted into the next
    /// narration instead of flooding the feed.
    // Only the Linux kernel-log reader (and tests) call this; elsewhere it is dead code
    // by platform, not by mistake.
    #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
    fn xid_events(
        &mut self,
        ids: &[DeviceId],
        records: Vec<gpuviewer_core::kmsg::XidRecord>,
        at_ms: u64,
    ) -> Vec<Event> {
        let mut out = Vec::new();
        for x in records {
            let Some(di) = ids
                .iter()
                .position(|id| gpuviewer_core::kmsg::xid_matches_device(&x.pci, &id.0))
            else {
                continue;
            };
            let key = (di, x.code);
            let folded = match self.xid_last.get_mut(&key) {
                Some((last, n)) if at_ms.saturating_sub(*last) < XID_REPEAT_WINDOW_MS => {
                    *n += 1;
                    continue;
                }
                Some((_, n)) => std::mem::take(n),
                None => 0,
            };
            self.xid_last.insert(key, (at_ms, 0));
            out.push(xid_event(di, &ids[di], &x, folded, at_ms));
        }
        out
    }

    /// Test seam: feed Xid records as if the kernel log produced them this tick.
    #[cfg(test)]
    pub(crate) fn inject_xid(
        &mut self,
        records: Vec<gpuviewer_core::kmsg::XidRecord>,
        at_ms: u64,
    ) -> Vec<Event> {
        let ids: Vec<DeviceId> = self.devices.iter().map(|(_, id, _)| id.clone()).collect();
        self.xid_events(&ids, records, at_ms)
    }

    /// The `device` anchor for collector-scoped events (stall, panic, session boundary):
    /// the first registered device, or a stable placeholder when none exists. One helper
    /// so every recorder-lifecycle fact follows the same convention.
    fn anchor_device(&self) -> DeviceId {
        self.devices
            .first()
            .map(|(_, id, _)| id.clone())
            .unwrap_or_else(|| DeviceId("collector".into()))
    }

    /// [`Engine::tick`] behind a panic firewall — the fix for the audit's tick-panic
    /// frozen-UI hole (docs/research/06-production-platform-deepdive.md §1.3): a panic
    /// anywhere in a tick (a backend parsing edge case, an arithmetic overflow in a
    /// decoder) used to kill the collector thread while the TUI kept rendering the last
    /// `Shared` snapshot forever — stale data masquerading as live, the worst possible
    /// failure for a product whose thesis is honest recording.
    ///
    /// Policy: drop the panicked tick, narrate it as a `CollectorStall` FACT (that kind
    /// asserts exactly "the recording has a hole"), and keep collecting — a one-off panic
    /// is the same per-tick weather as `NVML_ERROR_NOT_SUPPORTED` or a blocked probe, and
    /// losing the whole flight recording to one bad frame would contradict the product.
    /// [`MAX_CONSECUTIVE_PANICS`] panics in a row mean the fault is deterministic, not
    /// weather: the outcome turns `fatal` and the caller must stop, loudly. Any clean
    /// tick resets the count.
    pub fn tick_guarded(&mut self) -> TickOutcome {
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.tick())) {
            Ok(frame) => {
                self.consecutive_panics = 0;
                TickOutcome::Frame(frame)
            }
            Err(payload) => {
                self.consecutive_panics += 1;
                let summary = panic_summary(payload.as_ref());
                let fatal = self.consecutive_panics >= MAX_CONSECUTIVE_PANICS;
                // The default panic hook already printed the thread/location line at
                // unwind time; this adds the collector's own framing. Either way the
                // payload reaches stderr — a swallowed panic message would be its own
                // honesty bug.
                eprintln!(
                    "gpuviewer: collector tick panicked ({}/{MAX_CONSECUTIVE_PANICS} \
                     consecutive): {summary} (run with RUST_BACKTRACE=1 for a backtrace)",
                    self.consecutive_panics
                );
                let event = self.panic_event(&summary, fatal);
                // Record the fact while the recorder is still reachable — behind its own
                // firewall: if the panic originated inside the recorder fold or the sink,
                // recording the narration would panic again, and the narration must not
                // take down the narrator. stderr above remains the floor.
                let recorded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    if let Some(rec) = &mut self.recorder {
                        rec.record_events(std::slice::from_ref(&event));
                    }
                    if let Some(sink) = &mut self.sink {
                        sink.fire(&event);
                    }
                }));
                if recorded.is_err() {
                    eprintln!(
                        "gpuviewer: recording the panic event panicked too — the event \
                         reaches the live view only; stderr holds the full trail"
                    );
                }
                TickOutcome::Panicked {
                    event,
                    summary,
                    fatal,
                }
            }
        }
    }

    /// The `CollectorStall` fact for one panicked tick — reusing the existing kind (it is
    /// exactly "the recording has a hole") keeps the NDJSON contract untouched. Warning
    /// while collection will retry; Critical when this panic spent the budget and
    /// collection stops here.
    fn panic_event(&self, summary: &str, fatal: bool) -> Event {
        let anchor = self.anchor_device();
        let n = self.consecutive_panics;
        if fatal {
            Event {
                ts_ms: now_ms(),
                device: anchor,
                kind: EventKind::CollectorStall,
                severity: Severity::Critical,
                confidence: Confidence::Fact,
                title: format!("collection STOPPED — collector panicked {n} ticks in a row"),
                evidence: format!(
                    "panic: {summary}; {n} consecutive tick panics spent the \
                     {MAX_CONSECUTIVE_PANICS}-tick budget — collection will not resume \
                     and the recording ends here; run with RUST_BACKTRACE=1 for a backtrace"
                ),
            }
        } else {
            Event {
                ts_ms: now_ms(),
                device: anchor,
                kind: EventKind::CollectorStall,
                severity: Severity::Warning,
                confidence: Confidence::Fact,
                // The count is in the TITLE, not just the evidence: two consecutive
                // panics can land in the same millisecond, and the event log's dedupe
                // key is (ts, device, kind, title) — identical titles would collapse two
                // distinct dropped frames into one narration. Distinct events must
                // narrate distinctly.
                title: format!(
                    "collector tick panicked ({n}/{MAX_CONSECUTIVE_PANICS}) — frame \
                     dropped, the recording has a hole"
                ),
                evidence: format!(
                    "panic: {summary}; consecutive panic {n} of {MAX_CONSECUTIVE_PANICS} \
                     tolerated, collection retries next tick; run with RUST_BACKTRACE=1 \
                     for a backtrace"
                ),
            }
        }
    }

    /// End the recording session cleanly: write the `recording_stopped` mark, fire it
    /// through `--on-event`, and flush the partial rollup tail. Returns the stop event
    /// when one was written by THIS call so the `--json` path can put it on the stream
    /// while stdout is still open; the TUI path records it without emitting (the session
    /// is over, nobody is watching the feed).
    ///
    /// Idempotent — explicit shutdown paths and `Drop` may both arrive here, and the
    /// session's end must land exactly once. A SIGKILL/OOM-kill/power loss skips this
    /// entirely and writes nothing: that is exactly the asymmetry the next session's
    /// unclean-start narration covers.
    pub fn finish(&mut self) -> Option<Event> {
        let stop = if self.recorder.is_some() && !self.finished {
            self.finished = true;
            let ev = self.stop_event();
            if let Some(rec) = &mut self.recorder {
                rec.record_events(std::slice::from_ref(&ev));
            }
            if let Some(sink) = &mut self.sink {
                sink.fire(&ev);
            }
            Some(ev)
        } else {
            None
        };
        self.flush();
        stop
    }

    /// The `recording_stopped` fact — the session-boundary mark on the recording's right
    /// edge, twin of `recording_started`. Info/FACT for the same reasons; the evidence
    /// spells out what the mark means for the timeline: time past it is "gpuviewer not
    /// running", never "the GPU sat idle".
    fn stop_event(&self) -> Event {
        let now = now_ms();
        let dur = Duration::from_millis(now.saturating_sub(self.session_start_ms));
        let frames_word = if self.frames_folded == 1 {
            "frame"
        } else {
            "frames"
        };
        Event {
            ts_ms: now,
            device: self.anchor_device(),
            kind: EventKind::RecordingStopped,
            severity: Severity::Info,
            confidence: Confidence::Fact,
            title: format!("recording stopped — clean shutdown after {}", fmt_dur(dur)),
            evidence: format!(
                "session started {}; {} {frames_word} folded over {}; the partial rollup \
                 tail was flushed; time between this mark and the next recording_started \
                 is gpuviewer not running, not the GPU sitting idle",
                fmt_stamp(self.session_start_ms),
                self.frames_folded,
                fmt_dur(dur),
            ),
        }
    }

    /// Persist every device's partial (uncrossed) bucket. Call on shutdown so the last,
    /// incomplete 10s/1m window is not lost. A no-op when not persisting.
    pub fn flush(&mut self) {
        if let Some(rec) = &mut self.recorder {
            rec.flush();
        }
    }
}

impl Drop for Engine {
    fn drop(&mut self) {
        // The tail of the recording (and its stop mark) would otherwise be lost on a clean
        // exit — `finish` is idempotent, so paths that already called it lose nothing.
        let _ = self.finish();
    }
}

/// Fire-and-forget runner for the `--on-event` shell command. Each emitted event spawns the
/// platform shell (`sh -c CMD`; `cmd /C CMD` on Windows) with the event surfaced through
/// `GPV_EVENT_*` environment variables; the child is reaped lazily (a `try_wait` sweep on
/// the next fire) so a slow hook never blocks a tick.
///
/// Rate-capped at [`SINK_MAX_PER_MIN`] spawns per rolling minute: a throttle-flapping GPU can
/// produce a burst of events, and an unbounded fan-out of curl/notify-send processes would be
/// its own denial of service. Beyond the cap, events are dropped with a one-time warning per
/// minute.
pub struct EventSink {
    cmd: String,
    /// Live children awaiting reap, with their spawn instant; swept (non-blocking) on each
    /// fire, and killed once they outlive [`SINK_HOOK_TIMEOUT`].
    children: Vec<(Child, Instant)>,
    /// Spawn timestamps inside the current rolling 60s window.
    window: Vec<Instant>,
    /// Whether the "rate cap hit" warning was already printed in the current over-cap burst.
    warned: bool,
}

/// At most this many `--on-event` spawns per rolling minute.
const SINK_MAX_PER_MIN: usize = 60;

/// At most this many hooks alive at once. The per-minute cap bounds spawns, not
/// survivors: a hook that never exits (a `curl` against a dead host with no timeout, a
/// `read` waiting on a tty) used to accumulate one live child per event — up to ~86k a
/// day. Beyond this many, new events are dropped (with a warning) until some exit.
const SINK_MAX_LIVE: usize = 16;

/// A hook still running after this long is killed and reaped. Hooks are notifications;
/// one that runs for a minute is wedged, and leaving it would pin the live-children cap.
const SINK_HOOK_TIMEOUT: Duration = Duration::from_secs(60);

impl EventSink {
    pub fn new(cmd: String) -> Self {
        Self {
            cmd,
            children: Vec::new(),
            window: Vec::new(),
            warned: false,
        }
    }

    /// Spawn the hook for one event, unless the per-minute cap is exceeded.
    pub fn fire(&mut self, event: &Event) {
        self.reap();

        let now = Instant::now();
        self.window
            .retain(|t| now.duration_since(*t) < Duration::from_secs(60));
        if self.children.len() >= SINK_MAX_LIVE {
            if !self.warned {
                self.warned = true;
                eprintln!(
                    "gpuviewer: --on-event has {SINK_MAX_LIVE} hooks still running — dropping \
                     event hooks until some exit (hooks are killed after {}s)",
                    SINK_HOOK_TIMEOUT.as_secs()
                );
            }
            return;
        }
        if self.window.len() >= SINK_MAX_PER_MIN {
            if !self.warned {
                self.warned = true;
                eprintln!(
                    "gpuviewer: --on-event rate cap ({SINK_MAX_PER_MIN}/min) hit — dropping \
                     further event hooks this minute"
                );
            }
            return;
        }
        self.warned = false;

        let json = serde_json::to_string(event).unwrap_or_default();
        // The hook runs through the OS's native shell. WHY a dedicated Windows branch
        // instead of `sh` everywhere: Git-for-Windows drops an sh.exe onto the PATH of CI
        // runners and many dev boxes, so a Unix-only dispatch would pass CI green and then
        // fail on exactly the user machines that have no sh at all.
        //
        // WHY raw_arg instead of args(["/C", ..]): std's argument quoting targets
        // CommandLineToArgvW, but cmd.exe parses its line with its own rules. A hook
        // command containing spaces gets wrapped in quotes with its inner quotes
        // backslash-escaped — cmd understands neither, so e.g. a quoted redirect target
        // decays into a broken path (the first Windows CI run failed exactly there).
        // raw_arg hands the user's command to cmd verbatim, as if typed after `cmd /C `.
        #[cfg(windows)]
        let mut command = {
            use std::os::windows::process::CommandExt;
            let mut c = Command::new("cmd");
            c.arg("/C");
            c.raw_arg(&self.cmd);
            c
        };
        #[cfg(not(windows))]
        let mut command = {
            let mut c = Command::new("sh");
            c.args(["-c", &self.cmd]);
            c
        };
        command
            .env("GPV_EVENT_KIND", env_token(event.kind))
            .env("GPV_EVENT_SEVERITY", env_token(event.severity))
            .env("GPV_EVENT_CONFIDENCE", env_token(event.confidence))
            .env("GPV_EVENT_TITLE", &event.title)
            .env("GPV_EVENT_EVIDENCE", &event.evidence)
            .env("GPV_EVENT_DEVICE", &event.device.0)
            .env("GPV_EVENT_TS_MS", event.ts_ms.to_string())
            .env("GPV_EVENT_JSON", json);
        match command.spawn() {
            Ok(child) => {
                self.window.push(now);
                self.children.push((child, now));
            }
            Err(e) => eprintln!("gpuviewer: --on-event spawn failed: {e}"),
        }
    }

    /// Reap any finished children without blocking — a hook that is still running stays in the
    /// list and is checked again next time, unless it outlived [`SINK_HOOK_TIMEOUT`], in which
    /// case it is killed and reaped now.
    fn reap(&mut self) {
        let now = Instant::now();
        self.children.retain_mut(|(c, spawned)| {
            if matches!(c.try_wait(), Ok(Some(_))) {
                return false;
            }
            if now.duration_since(*spawned) >= SINK_HOOK_TIMEOUT {
                let _ = c.kill();
                let _ = c.wait();
                return false;
            }
            true
        });
    }

    /// Hooks currently alive (test visibility for the live cap; the never-exiting-hook
    /// fixture is `sleep`, so unix-only).
    #[cfg(all(test, unix))]
    fn live(&self) -> usize {
        self.children.len()
    }

    /// Block until every live hook has exited (tests only).
    #[cfg(test)]
    fn wait_all(&mut self) {
        for (c, _) in &mut self.children {
            let _ = c.wait();
        }
        self.children.clear();
    }

    /// Kill every live hook (tests only — the never-exiting-hook fixture, unix-only).
    #[cfg(all(test, unix))]
    fn kill_all(&mut self) {
        for (c, _) in &mut self.children {
            let _ = c.kill();
            let _ = c.wait();
        }
        self.children.clear();
    }
}

impl Drop for EventSink {
    fn drop(&mut self) {
        // Best-effort reap on shutdown; we do not block on a long-running hook.
        self.reap();
    }
}

/// Serialize an enum to its bare wire token (the same `snake_case`/`lowercase` spelling the
/// NDJSON contract uses), for the `GPV_EVENT_*` environment. Infallible for these C-like enums.
fn env_token<T: Serialize>(v: T) -> String {
    serde_json::to_string(&v)
        .ok()
        .map(|s| s.trim_matches('"').to_string())
        .unwrap_or_default()
}

/// Format a duration as a compact human string for narration ("1.2s", "8s", "3m 20s").
fn fmt_dur(d: Duration) -> String {
    let ms = d.as_millis() as u64;
    if ms < 10_000 {
        format!("{:.1}s", ms as f64 / 1000.0)
    } else {
        let s = (ms + 500) / 1000;
        if s >= 60 {
            format!("{}m {}s", s / 60, s % 60)
        } else {
            format!("{s}s")
        }
    }
}

/// Format a unix-millis instant as a wall HH:MM:SS for the "last good frame at …" narration.
fn fmt_clock(ms: u64) -> String {
    use chrono::{Local, TimeZone};
    Local
        .timestamp_millis_opt(ms as i64)
        .single()
        .map(|t| t.format("%H:%M:%S").to_string())
        .unwrap_or_else(|| "--:--:--".into())
}

/// Date + time of day, for session-boundary narration: a dangling start mark can be days
/// old, where a bare HH:MM:SS would be ambiguous.
fn fmt_stamp(ms: u64) -> String {
    use chrono::{Local, TimeZone};
    Local
        .timestamp_millis_opt(ms as i64)
        .single()
        .map(|t| t.format("%Y-%m-%d %H:%M:%S").to_string())
        .unwrap_or_else(|| "????-??-?? --:--:--".into())
}

/// Whether a single device's frame reads as idle for the adaptive-backoff decision. Pure so
/// the backoff state machine is unit-tested without a tick loop.
///
/// `util_pct`, when reported, is authoritative: below the threshold AND no attached process
/// itself showing real GPU use → idle. When the device CANNOT report utilization — Intel
/// never does (the device-level PMU needs root/CAP_PERFMON; intel.rs hardcodes honest
/// `None`), so the old "None → not idle" rule pinned the whole loop at full cadence forever
/// on any machine containing such a device, killing the bottom #1291 mitigation exactly
/// where it matters (docs/research/06-production-platform-deepdive.md, "cross-cutting
/// defect") — fall back to activity signals the device DOES report, via
/// [`util_less_activity`]. With no signal at all the device is backoff-eligible: unknown
/// must not mean "busy". The worry behind the old rule — backing off through a throttle
/// onset — is covered by the fallback: an onset asserts a throttle reason and moves clocks,
/// both of which snap the cadence back. No utilization value is fabricated anywhere;
/// absence stays absence in everything rendered or recorded.
///
/// A failed probe (`sample` = `None`) still reads as NOT idle: that is a fault to watch at
/// full rate, not an unknown-but-quiet device.
pub fn device_is_idle(
    prev: Option<&DynamicSample>,
    sample: Option<&DynamicSample>,
    procs: &[ProcessSample],
) -> bool {
    let Some(s) = sample else { return false };
    // An attached process provably computing holds full cadence regardless of how (or
    // whether) the device-level story is told.
    if procs
        .iter()
        .any(|p| p.util_pct.map(|u| u >= IDLE_UTIL_PCT).unwrap_or(false))
    {
        return false;
    }
    match s.util_pct {
        Some(util) => util < IDLE_UTIL_PCT,
        None => !util_less_activity(prev, s),
    }
}

/// Activity signals for a device that cannot report utilization. Every check is a genuine
/// observation already carried by the model — nothing here invents a util number.
fn util_less_activity(prev: Option<&DynamicSample>, s: &DynamicSample) -> bool {
    // Video engines busy: encode/decode is real work even with zero 3D/compute load.
    if s.encoder_pct.map(|u| u >= IDLE_UTIL_PCT).unwrap_or(false)
        || s.decoder_pct.map(|u| u >= IDLE_UTIL_PCT).unwrap_or(false)
    {
        return true;
    }
    // An asserted throttle reason means the device is being limited, hence working.
    // (`None` — throttle unobservable on this source — asserts nothing either way.)
    if s.throttle.is_some_and(|t| t.any()) {
        return true;
    }
    // The remaining signals are deltas against the last good sample; with no baseline yet
    // there is nothing to compare (the streak threshold absorbs the first tick anyway).
    let Some(p) = prev else { return false };
    // VRAM moved since we last looked: something allocated or freed.
    if let (Some(a), Some(b)) = (p.mem_used_bytes, s.mem_used_bytes) {
        if a.abs_diff(b) >= IDLE_MEM_DELTA_BYTES {
            return true;
        }
    }
    // Clocks moved past wobble — or the device left a parked state where the actual
    // frequency reads as absent (Intel act-freq is None inside RC6): it woke up to work.
    match (p.sm_clock_mhz, s.sm_clock_mhz) {
        (Some(a), Some(b)) => a.abs_diff(b) > IDLE_CLOCK_JITTER_MHZ,
        (None, Some(_)) => true,
        _ => false,
    }
}

/// A device counts as idle below this util (device or any process). Matches the "low-power
/// cadence" wedge: a desktop GPU at <5% with nothing computing is genuinely asleep.
const IDLE_UTIL_PCT: f32 = 5.0;

/// VRAM movement between consecutive good samples below this is allocator churn (a desktop
/// compositor recycles small buffers constantly), not activity worth full cadence.
const IDLE_MEM_DELTA_BYTES: u64 = 4 * 1024 * 1024;

/// Clock movement at or below this is sensor wobble around a parked frequency. Any real
/// load (or throttle onset) shifts clocks by hundreds of MHz, so the signal still catches
/// a wake-up within one tick.
const IDLE_CLOCK_JITTER_MHZ: u32 = 25;

/// Consecutive all-idle ticks before the cadence stretches.
pub const BACKOFF_AFTER_IDLE_TICKS: u32 = 60;

/// Idle cadence is this multiple of the configured interval, capped at [`BACKOFF_CAP`].
const BACKOFF_MULTIPLIER: u32 = 5;
const BACKOFF_CAP: Duration = Duration::from_secs(10);

/// The effective sleep for the next loop iteration given how many consecutive all-idle ticks
/// have elapsed. ANY non-idle tick resets `idle_streak` to 0 ([`Backoff::observe`]'s job),
/// which snaps the cadence back to the configured interval instantly. Pure for unit testing.
///
/// WHY this exists (CLAUDE.md / bottom #1291): polling has side effects — NVIDIA temp polling
/// keeps GPUs awake and AMD GRBM register reads break GFXOFF. On a genuinely idle GPU the
/// fast 1 Hz cadence is pure harm: it prevents the deep sleep states it is trying to observe.
/// Stretching to 5× (≤10s) once a GPU has sat idle a full minute lets it sleep while still
/// catching a wake-up within one stretched tick.
pub fn effective_interval(interval: Duration, idle_streak: u32) -> Duration {
    if idle_streak >= BACKOFF_AFTER_IDLE_TICKS {
        (interval * BACKOFF_MULTIPLIER).min(BACKOFF_CAP)
    } else {
        interval
    }
}

/// The adaptive-backoff state machine: consecutive all-idle ticks stretch the cadence
/// ([`effective_interval`]); any non-idle tick snaps it back instantly. Owns the
/// previous-sample baseline that [`device_is_idle`]'s util-less fallback diffs against —
/// kept off `Shared` so the whole policy is a plain value a scripted-stream test drives
/// without threads, locks, or a clock. The footer never lies about the result: it renders
/// the same `effective_interval_ms` atomic the loop actually sleeps on.
pub struct Backoff {
    /// `--no-backoff` → false: always the configured interval, streak irrelevant.
    enabled: bool,
    interval: Duration,
    /// Consecutive all-idle ticks; any non-idle tick resets it to 0.
    idle_streak: u32,
    /// Last good sample per device (collection order) — the delta baseline. Carried across
    /// failed-probe ticks: "VRAM moved since we last could look" is still activity.
    prev: Vec<Option<DynamicSample>>,
}

impl Backoff {
    pub fn new(enabled: bool, interval: Duration, devices: usize) -> Self {
        Self {
            enabled,
            interval,
            idle_streak: 0,
            prev: vec![None; devices],
        }
    }

    /// Fold one tick's frame in; returns the sleep before the next tick.
    pub fn observe(&mut self, frame: &Frame) -> Duration {
        let mut all_idle = true;
        for (i, fd) in frame.devices.iter().enumerate() {
            let prev = self.prev.get(i).and_then(Option::as_ref);
            // Unobservable processes fall back to an empty slice for the IDLE decision
            // only: device_is_idle reads process util as one activity signal among
            // several, and an absent list must not by itself vote "busy" (which would
            // pin a genuinely idle GPU at full poll rate) nor "idle" on its own — the
            // device-level signals still decide. Nothing here is narrated or persisted.
            let procs = fd.processes.as_deref().unwrap_or(&[]);
            if !device_is_idle(prev, fd.sample.as_ref(), procs) {
                all_idle = false;
            }
            if let (Some(slot), Some(s)) = (self.prev.get_mut(i), &fd.sample) {
                *slot = Some(s.clone());
            }
        }
        if !self.enabled {
            return self.interval;
        }
        if all_idle {
            self.idle_streak = self.idle_streak.saturating_add(1);
        } else {
            self.idle_streak = 0;
        }
        effective_interval(self.interval, self.idle_streak)
    }

    /// A paused tick observes nothing: do not let the idle streak grow (it would back off
    /// on resume against stale state) and poll the pause flag at the configured interval.
    pub fn skip(&mut self) -> Duration {
        self.idle_streak = 0;
        self.interval
    }
}

/// Why and when the collector died, for the UI. Once set, the live panes can only show
/// the last folded frame, and they must say so — stale data masquerading as live is the
/// audit's worst failure shape (the tick-panic frozen-UI hole).
#[derive(Clone)]
pub struct CollectorStop {
    /// Unix-millis instant the collector declared itself dead.
    pub at_ms: u64,
    /// One-line panic summary (the full trail is on stderr and in the event evidence).
    pub reason: String,
}

/// State shared between the collector thread and the UI.
pub struct Shared {
    pub infos: Vec<StaticInfo>,
    pub latest: Vec<Option<DynamicSample>>,
    pub processes: Vec<Vec<ProcessSample>>,
    pub history: HistoryStore,
    /// True when the data is simulated (mock backend active) — drives the footer's
    /// "(mock data)" tag, which must track the actual data source.
    pub mock: bool,
    /// The on-disk history path (`None` when not persisting), for the replay view.
    pub db_path: Option<PathBuf>,
    /// The configured tick interval in millis — the footer compares the effective cadence
    /// against this to decide whether low-power backoff is active.
    pub interval_ms: u64,
    /// Current effective tick interval in millis — stretched under low-power backoff so the
    /// footer can show "low-power cadence 5s". Published via an atomic so the UI reads it
    /// without taking the (already heavily-held) Shared lock on the collector's hot path.
    pub effective_interval_ms: Arc<AtomicU64>,
    /// Collector ticks folded into this state so far, bumped once per frame (a panicked
    /// tick bumps it too — its narration event is new state). The timeline overview
    /// re-queries its cached window when this advances — and only then, keeping SQLite
    /// off the render path. Stays 0 forever in [`Collector::stationary`] sessions:
    /// a recording does not grow.
    pub tick_seq: u64,
    /// `Some` once the collector thread is dead ([`MAX_CONSECUTIVE_PANICS`] consecutive
    /// tick panics): the UI must state collection STOPPED — with time and cause — and
    /// mark every live pane stale, never current.
    pub stopped: Option<CollectorStop>,
    /// Per-device loss marker, parallel to `infos`: `Some(unix ms the device was declared
    /// lost)` while its probe is dead ([`DEVICE_LOST_AFTER_FAILED_PROBES`] consecutive
    /// failures), cleared when it answers again. Drives the per-device STALE affordances:
    /// a lost device's panes are frozen at its last good data and must say so. This is
    /// deliberately NOT [`Shared::stopped`] — the other devices are still collecting, so
    /// the footer must not claim collection stopped.
    pub lost: Vec<Option<u64>>,
}

pub struct Collector {
    pub shared: Arc<Mutex<Shared>>,
    pub paused: Arc<AtomicBool>,
    /// Cooperative stop flag for the collection thread ([`Collector::shutdown`]). The
    /// thread exits its loop on the next slice boundary, dropping the [`Engine`] — whose
    /// `Drop` writes the session's `recording_stopped` mark and flushes the rollup tail.
    stop: Arc<AtomicBool>,
    /// The collection thread's handle (`None` for [`Collector::stationary`] sessions),
    /// so `shutdown` can wait — boundedly — for the recording tail to land.
    handle: Option<std::thread::JoinHandle<()>>,
}

/// The collection thread sleeps its (possibly backoff-stretched, up to 10s) cadence in
/// slices of this size so a shutdown request is noticed within one slice instead of one
/// whole cadence — quitting the TUI must not hang for seconds on a sleeping collector.
const SLEEP_SLICE: Duration = Duration::from_millis(100);

/// How long [`Collector::shutdown`] waits for the collection thread to finish before
/// giving up. The thread normally reacts within one [`SLEEP_SLICE`]; the budget exists
/// for a tick blocked inside a backend probe (the stall scenario) — quit must not wedge
/// on a hung driver, and a missing stop mark is exactly what the next session's
/// unclean-start narration reports.
const SHUTDOWN_GRACE: Duration = Duration::from_secs(2);

impl Collector {
    /// Spawn the background collection thread. `backoff` enables the adaptive idle cadence
    /// (`--no-backoff` disables it for users who want a fixed, predictable poll rate).
    pub fn start(mut engine: Engine, interval: Duration, backoff: bool) -> Self {
        let infos = engine.static_infos();
        let n = infos.len();
        let mock = engine.mock_in_use();
        let db_path = engine.db_path();
        let effective_interval_ms = Arc::new(AtomicU64::new(interval.as_millis() as u64));
        let shared = Arc::new(Mutex::new(Shared {
            infos,
            latest: vec![None; n],
            processes: vec![Vec::new(); n],
            // Live window: 30 min at 1s ticks; event log capped generously.
            history: HistoryStore::new(1800, 5000),
            mock,
            db_path,
            interval_ms: interval.as_millis() as u64,
            effective_interval_ms: Arc::clone(&effective_interval_ms),
            tick_seq: 0,
            stopped: None,
            lost: vec![None; n],
        }));
        let paused = Arc::new(AtomicBool::new(false));
        let stop = Arc::new(AtomicBool::new(false));

        let s = Arc::clone(&shared);
        let p = Arc::clone(&paused);
        let st = Arc::clone(&stop);
        let handle = std::thread::spawn(move || {
            let mut cadence = Backoff::new(backoff, interval, n);
            loop {
                // Cooperative shutdown: returning drops `engine`, whose Drop writes the
                // `recording_stopped` mark and flushes the recording tail.
                if st.load(Ordering::Relaxed) {
                    return;
                }
                let effective = if !p.load(Ordering::Relaxed) {
                    // The guarded tick runs BEFORE the Shared lock is taken (as tick()
                    // always did), so even a panic that somehow escaped the firewall
                    // could never poison the mutex under the UI.
                    match engine.tick_guarded() {
                        TickOutcome::Frame(frame) => {
                            // Classify before the lock — Backoff carries its own delta
                            // baseline, so the idle decision never touches (or contends
                            // on) Shared.
                            let effective = cadence.observe(&frame);
                            engine.set_cadence(effective);
                            let mut sh = s.lock().unwrap();
                            for (i, fd) in frame.devices.iter().enumerate() {
                                if let Some(sample) = &fd.sample {
                                    sh.history.push_sample(&fd.id, sample.clone());
                                    sh.latest[i] = Some(sample.clone());
                                    // The process list only updates alongside a good
                                    // probe: a failed refresh_dynamic almost certainly
                                    // took the process probe down with it (same dead
                                    // device), and replacing the table with that empty
                                    // result would render "no processes" as if current —
                                    // the same lie as a frozen chart. The kept list is
                                    // the device's last good data, which the per-device
                                    // stale affordances label once the device is lost.
                                    //
                                    // The `if let` extends that guard to the narrower
                                    // case the outer check misses: refresh_dynamic
                                    // succeeding while refresh_processes alone fails.
                                    // Same lie, same rule — keep the last good list.
                                    if let Some(procs) = &fd.processes {
                                        sh.processes[i] = procs.clone();
                                    }
                                }
                            }
                            // Device-lost edges ride in the frame's own events; fold
                            // them into the per-device markers the UI renders from.
                            for e in &frame.events {
                                let mark = match e.kind {
                                    EventKind::DeviceLost => Some(Some(e.ts_ms)),
                                    EventKind::DeviceReturned => Some(None),
                                    _ => None,
                                };
                                if let Some(mark) = mark {
                                    if let Some(i) =
                                        sh.infos.iter().position(|inf| inf.id == e.device)
                                    {
                                        sh.lost[i] = mark;
                                    }
                                }
                            }
                            sh.history.push_events(frame.events);
                            // Publish the tick AFTER its data is folded in, so a reader
                            // seeing the new seq always sees the new frame too.
                            sh.tick_seq += 1;
                            effective
                        }
                        TickOutcome::Panicked {
                            event,
                            summary,
                            fatal,
                        } => {
                            let mut sh = s.lock().unwrap();
                            sh.history.push_events([event]);
                            sh.tick_seq += 1;
                            if fatal {
                                // Dead, and saying so: `stopped` drives the UI's stop
                                // banner and stale tags, the Critical fact above sits in
                                // the story feed and the store, and ending the thread
                                // drops the Engine — whose Drop flushes the recording
                                // tail, so history is complete up to the stop.
                                sh.stopped = Some(CollectorStop {
                                    at_ms: now_ms(),
                                    reason: summary,
                                });
                                return;
                            }
                            // A panicked tick observed nothing — same cadence rule as a
                            // paused one, which also keeps a faulting collector watched
                            // at the full configured rate rather than backed off.
                            cadence.skip()
                        }
                    }
                } else {
                    engine.note_paused();
                    let skip = cadence.skip();
                    engine.set_cadence(skip);
                    skip
                };
                effective_interval_ms.store(effective.as_millis() as u64, Ordering::Relaxed);
                // Sleep the cadence in slices so shutdown is noticed promptly (see
                // [`SLEEP_SLICE`]); the effective cadence itself is unchanged.
                let deadline = Instant::now() + effective;
                loop {
                    if st.load(Ordering::Relaxed) {
                        return;
                    }
                    let now = Instant::now();
                    if now >= deadline {
                        break;
                    }
                    std::thread::sleep((deadline - now).min(SLEEP_SLICE));
                }
            }
        });

        Self {
            shared,
            paused,
            stop,
            handle: Some(handle),
        }
    }

    /// Ask the collection thread to stop and wait — boundedly — for it to finish, so a
    /// clean quit ends with the `recording_stopped` mark and the rollup tail on disk.
    /// The wait is capped at [`SHUTDOWN_GRACE`]: a tick wedged inside a hung backend
    /// probe must not wedge quit with it (the thread still drops the engine — and writes
    /// the mark — whenever the probe finally returns; if the process dies first, the next
    /// session narrates the missing stop mark, which is the honest record of what
    /// happened). A no-op for [`Collector::stationary`] sessions, which have no thread.
    pub fn shutdown(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        let Some(handle) = self.handle.take() else {
            return;
        };
        let deadline = Instant::now() + SHUTDOWN_GRACE;
        while !handle.is_finished() && Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(10));
        }
        if handle.is_finished() {
            // Joining a finished thread cannot block; a panicked collector already
            // narrated itself (panic firewall), so the result carries nothing new.
            let _ = handle.join();
        } else {
            eprintln!(
                "gpuviewer: the collector did not stop within {}s (a backend probe is \
                 likely blocked); exiting without the recording's stop mark — the next \
                 session will report it",
                SHUTDOWN_GRACE.as_secs()
            );
        }
    }

    /// A collector with NO engine and NO background thread, for the file viewer
    /// (`gpuviewer view`): the recording is the only data source, so nothing ticks,
    /// nothing records, and no backend is probed — an exported incident replays on a
    /// machine with no GPU at all. `Shared` stays at its empty initial state; the replay
    /// view reads the store at `db_path` directly.
    pub fn stationary(infos: Vec<StaticInfo>, db_path: Option<PathBuf>) -> Self {
        let n = infos.len();
        Self {
            shared: Arc::new(Mutex::new(Shared {
                infos,
                latest: vec![None; n],
                processes: vec![Vec::new(); n],
                history: HistoryStore::new(1800, 5000),
                // A recording's provenance is unstated — never label it mock (or live).
                mock: false,
                db_path,
                interval_ms: 1000,
                effective_interval_ms: Arc::new(AtomicU64::new(1000)),
                tick_seq: 0,
                stopped: None,
                lost: vec![None; n],
            })),
            paused: Arc::new(AtomicBool::new(false)),
            stop: Arc::new(AtomicBool::new(false)),
            handle: None,
        }
    }
}

/// A collector with NO background thread, for UI tests: `Collector::start` ticks the mock
/// once at spawn, which would race assertions that mutate `Shared` directly. Tests own the
/// entire `Shared` state instead and drive draws/keys by hand.
#[cfg(test)]
pub(crate) fn test_collector(db_path: Option<PathBuf>) -> Collector {
    let engine = Engine::new(EngineConfig {
        force_mock: true,
        ..Default::default()
    });
    let collector = Collector::stationary(engine.static_infos(), db_path);
    // The infos ARE mock devices; the footer tests assert the "(mock data)" label.
    collector.shared.lock().unwrap().mock = true;
    collector
}

#[cfg(test)]
mod tests {
    use super::*;
    use gpuviewer_core::{BackendError, ProcessKind, ThrottleReasons, Vendor};
    use gpuviewer_history::Tier;

    fn sample(util: Option<f32>) -> DynamicSample {
        DynamicSample {
            ts_ms: 1_000,
            util_pct: util,
            util_engine: None,
            mem_used_bytes: None,
            power_mw: None,
            temp_c: None,
            fan_pct: None,
            sm_clock_mhz: None,
            mem_clock_mhz: None,
            encoder_pct: None,
            decoder_pct: None,
            // Most scripted ticks model an observing source that sees no throttle;
            // the bare-sample "no signal at all" test overrides this to None.
            throttle: Some(ThrottleReasons::default()),
            power_limit_mw: None,
            pcie_gen: None,
            pcie_width: None,
            ecc_uncorrected: None,
        }
    }

    fn proc(util: Option<f32>) -> ProcessSample {
        ProcessSample {
            pid: 1,
            name: "x".into(),
            kind: ProcessKind::Compute,
            mem_bytes: None,
            util_pct: util,
            cpu_pct: None,
            container: None,
            cmdline: None,
        }
    }

    const GIB: u64 = 1024 * 1024 * 1024;

    /// Script one tick's frame for [`Backoff`] from per-device (sample, processes) pairs —
    /// the scripted-stream harness for the cadence tests, same spirit as the MockBackend's
    /// scripted streams but with full control over every Option field.
    fn frame_of(devs: Vec<(Option<DynamicSample>, Vec<ProcessSample>)>) -> Frame {
        Frame {
            ts_ms: 0,
            devices: devs
                .into_iter()
                .enumerate()
                .map(|(i, (sample, processes))| FrameDevice {
                    id: DeviceId(format!("test:{i}")),
                    name: format!("dev{i}"),
                    mem_total_bytes: None,
                    sample,
                    // Backoff fixtures always script an OBSERVED list (possibly empty);
                    // the unobservable case has its own test.
                    processes: Some(processes),
                })
                .collect(),
            events: Vec::new(),
        }
    }

    /// The dev machine's Intel iGPU, as the audit describes it: util NEVER reported, no
    /// mem-used, clock parked at the minimum tick after tick, a quiet compositor attached.
    fn quiet_igpu() -> (Option<DynamicSample>, Vec<ProcessSample>) {
        let mut s = sample(None);
        s.sm_clock_mhz = Some(300);
        (Some(s), vec![proc(Some(1.0))])
    }

    // normalize_pci_id's unit tests moved to gpuviewer-core::model with the function
    // (design §5.4) — extended there with the wddm:/apple: refuse-to-dedupe cases.

    // ---- stall-gap threshold (pure) ----

    #[test]
    fn stall_threshold_is_max_of_three_intervals_and_five_seconds() {
        // Fast cadence: the 5s floor dominates (3×100ms = 300ms < 5s).
        assert_eq!(
            stall_threshold(Duration::from_millis(100)),
            Duration::from_secs(5)
        );
        // Slow cadence: 3×interval dominates (3×3s = 9s > 5s).
        assert_eq!(
            stall_threshold(Duration::from_secs(3)),
            Duration::from_secs(9)
        );
        // Exactly at the crossover (3×~1.667s ≈ 5s).
        assert_eq!(
            stall_threshold(Duration::from_secs(2)),
            Duration::from_secs(6)
        );
    }

    #[test]
    fn is_stall_fires_only_strictly_past_the_threshold() {
        let interval = Duration::from_secs(1); // threshold = 5s
        assert!(
            !is_stall(Duration::from_secs(5), interval),
            "at threshold: not yet"
        );
        assert!(!is_stall(Duration::from_secs(4), interval));
        assert!(
            is_stall(Duration::from_millis(5_001), interval),
            "just over: stall"
        );
        assert!(is_stall(Duration::from_secs(30), interval));
    }

    // ---- adaptive backoff (pure) ----

    #[test]
    fn effective_interval_holds_until_the_idle_streak_threshold() {
        let interval = Duration::from_secs(1);
        // Below the threshold: configured interval unchanged.
        assert_eq!(effective_interval(interval, 0), interval);
        assert_eq!(
            effective_interval(interval, BACKOFF_AFTER_IDLE_TICKS - 1),
            interval
        );
        // At/after the threshold: stretched to 5× (within the 10s cap).
        assert_eq!(
            effective_interval(interval, BACKOFF_AFTER_IDLE_TICKS),
            Duration::from_secs(5)
        );
        assert_eq!(
            effective_interval(interval, BACKOFF_AFTER_IDLE_TICKS + 1000),
            Duration::from_secs(5)
        );
    }

    #[test]
    fn effective_interval_caps_at_ten_seconds() {
        // 5×3s = 15s would exceed the cap, so it clamps to 10s.
        assert_eq!(
            effective_interval(Duration::from_secs(3), BACKOFF_AFTER_IDLE_TICKS),
            Duration::from_secs(10)
        );
    }

    #[test]
    fn device_is_idle_requires_low_device_and_process_util() {
        // Device below 5% and no busy process → idle.
        assert!(device_is_idle(
            None,
            Some(&sample(Some(2.0))),
            &[proc(Some(1.0))]
        ));
        // Device busy → not idle.
        assert!(!device_is_idle(None, Some(&sample(Some(40.0))), &[]));
        // Device idle but a process is computing → not idle (work is queued/running).
        assert!(!device_is_idle(
            None,
            Some(&sample(Some(1.0))),
            &[proc(Some(80.0))]
        ));
        // No sample at all (failed probe) → not idle: a fault is watched at full rate.
        assert!(!device_is_idle(None, None, &[]));
    }

    /// The audit's cross-cutting defect (06-production-platform-deepdive.md): Intel reports
    /// `util_pct: None` on every tick, and the old "None → not idle" rule made such a device
    /// pin the loop at full cadence forever. A util-less device with no other activity
    /// signal must be backoff-eligible — unknown is not "busy".
    #[test]
    fn util_less_device_with_no_activity_signal_is_backoff_eligible() {
        // Bare sample (everything None, throttle unobservable too — the wddm shape):
        // no signal at all.
        let mut bare = sample(None);
        bare.throttle = None;
        assert!(device_is_idle(None, Some(&bare), &[]));
        // An observing source reporting "no throttle" is equally signal-less.
        assert!(device_is_idle(None, Some(&sample(None)), &[]));
        // The ubiquitous iGPU shape: a quiet compositor attached is not activity…
        assert!(device_is_idle(
            Some(&sample(None)),
            Some(&sample(None)),
            &[proc(Some(1.0))]
        ));
        // …and a process whose own util is unknown is not proof of work either (the old
        // process check already treated unknown process util as not-busy).
        assert!(device_is_idle(None, Some(&sample(None)), &[proc(None)]));
    }

    /// Every fallback signal is a real observation; each one alone must hold full cadence
    /// on a device that cannot report utilization.
    #[test]
    fn util_less_device_with_genuine_activity_is_not_idle() {
        // fdinfo engine-busy on an attached process.
        assert!(!device_is_idle(
            None,
            Some(&sample(None)),
            &[proc(Some(80.0))]
        ));

        // VRAM moved at least the churn threshold since the last good sample…
        let mut prev = sample(None);
        prev.mem_used_bytes = Some(GIB);
        let mut cur = sample(None);
        cur.mem_used_bytes = Some(GIB + IDLE_MEM_DELTA_BYTES);
        assert!(!device_is_idle(Some(&prev), Some(&cur), &[]));
        // …but sub-threshold churn (compositor buffer recycling) is not activity.
        cur.mem_used_bytes = Some(GIB + IDLE_MEM_DELTA_BYTES - 1);
        assert!(device_is_idle(Some(&prev), Some(&cur), &[]));

        // Clocks moved past wobble.
        let mut prev = sample(None);
        prev.sm_clock_mhz = Some(300);
        let mut cur = sample(None);
        cur.sm_clock_mhz = Some(900);
        assert!(!device_is_idle(Some(&prev), Some(&cur), &[]));
        // Single-bin wobble around a parked frequency is not activity.
        cur.sm_clock_mhz = Some(300 + IDLE_CLOCK_JITTER_MHZ);
        assert!(device_is_idle(Some(&prev), Some(&cur), &[]));
        // Waking out of a parked/RC6 state (clock absent → present) is, even with no
        // delta to compute.
        prev.sm_clock_mhz = None;
        cur.sm_clock_mhz = Some(300);
        assert!(!device_is_idle(Some(&prev), Some(&cur), &[]));

        // A busy video engine.
        let mut enc = sample(None);
        enc.encoder_pct = Some(60.0);
        assert!(!device_is_idle(None, Some(&enc), &[]));

        // An asserted throttle reason — the throttle-onset worry behind the old rule.
        let mut thr = sample(None);
        thr.throttle = Some(ThrottleReasons {
            thermal: true,
            ..Default::default()
        });
        assert!(!device_is_idle(None, Some(&thr), &[]));
    }

    // ---- backoff state machine (scripted streams) ----

    /// Regression for the audit's backoff-vs-Intel defect: a device that can NEVER report
    /// utilization but is otherwise quiet must not pin the loop at full cadence — after the
    /// idle-streak threshold the cadence stretches exactly as for a measurably-idle GPU.
    #[test]
    fn always_none_util_device_does_not_pin_cadence() {
        let interval = Duration::from_secs(1);
        let mut cadence = Backoff::new(true, interval, 1);
        for i in 1..BACKOFF_AFTER_IDLE_TICKS {
            assert_eq!(
                cadence.observe(&frame_of(vec![quiet_igpu()])),
                interval,
                "tick {i}: configured cadence until the streak threshold"
            );
        }
        assert_eq!(
            cadence.observe(&frame_of(vec![quiet_igpu()])),
            Duration::from_secs(5),
            "a quiet always-None-util device must reach the low-power cadence"
        );
    }

    /// A device with real activity holds full cadence forever — both the measured form
    /// (util reported high) and the util-less-but-provably-active form (clocks moving
    /// under a load we cannot measure directly).
    #[test]
    fn active_device_holds_full_cadence() {
        let interval = Duration::from_secs(1);

        let mut cadence = Backoff::new(true, interval, 1);
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS * 2 {
            let f = frame_of(vec![(Some(sample(Some(90.0))), vec![])]);
            assert_eq!(
                cadence.observe(&f),
                interval,
                "measured load: never stretch"
            );
        }

        let mut cadence = Backoff::new(true, interval, 1);
        let mut clock = 600u32;
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS * 2 {
            clock = if clock == 600 { 1_100 } else { 600 };
            let mut s = sample(None);
            s.sm_clock_mhz = Some(clock);
            assert_eq!(
                cadence.observe(&frame_of(vec![(Some(s), vec![])])),
                interval,
                "util-less device with moving clocks: never stretch"
            );
        }
    }

    /// Backoff requires ALL devices idle: the util-less iGPU no longer pins the cadence,
    /// but it must not unpin a machine whose other GPU is genuinely busy (the NVIDIA+Intel
    /// hybrid from the audit, with the dGPU under load this time).
    #[test]
    fn busy_device_next_to_util_less_one_holds_full_cadence() {
        let interval = Duration::from_secs(1);
        let mut cadence = Backoff::new(true, interval, 2);
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS * 2 {
            let f = frame_of(vec![(Some(sample(Some(95.0))), vec![]), quiet_igpu()]);
            assert_eq!(cadence.observe(&f), interval);
        }
    }

    /// Once stretched, a wake on the util-less device — visible only through a process
    /// turning busy — snaps the cadence back within one tick.
    #[test]
    fn wake_on_util_less_device_snaps_cadence_back() {
        let interval = Duration::from_secs(1);
        let mut cadence = Backoff::new(true, interval, 1);
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS + 5 {
            cadence.observe(&frame_of(vec![quiet_igpu()]));
        }
        assert_eq!(
            cadence.observe(&frame_of(vec![quiet_igpu()])),
            Duration::from_secs(5),
            "precondition: well into low-power cadence"
        );
        let (s, _) = quiet_igpu();
        let woke = frame_of(vec![(s, vec![proc(Some(40.0))])]);
        assert_eq!(
            cadence.observe(&woke),
            interval,
            "wake snaps back instantly"
        );
    }

    /// `--no-backoff` means exactly that: the configured interval regardless of idleness.
    #[test]
    fn disabled_backoff_never_stretches() {
        let interval = Duration::from_secs(1);
        let mut cadence = Backoff::new(false, interval, 1);
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS * 2 {
            assert_eq!(cadence.observe(&frame_of(vec![quiet_igpu()])), interval);
        }
    }

    /// Paused ticks observe nothing, so they must reset the streak rather than let it
    /// ride into a stretch that would apply on resume against stale state.
    #[test]
    fn paused_ticks_reset_the_streak() {
        let interval = Duration::from_secs(1);
        let mut cadence = Backoff::new(true, interval, 1);
        for _ in 0..BACKOFF_AFTER_IDLE_TICKS - 1 {
            cadence.observe(&frame_of(vec![quiet_igpu()]));
        }
        assert_eq!(cadence.skip(), interval);
        // One more idle tick would have crossed the threshold pre-pause; post-pause the
        // streak starts over.
        assert_eq!(cadence.observe(&frame_of(vec![quiet_igpu()])), interval);
    }

    // ---- --on-event sink ----

    /// The hook receives the event through the GPV_EVENT_* environment. We echo
    /// GPV_EVENT_KIND to a tmpfile through this OS's real shell dispatch (`sh -c` /
    /// `cmd /C`) for a synthetic event and read the file back, after waiting for the
    /// (detached) child — proving the env wiring, not just spawn.
    #[test]
    fn event_sink_passes_event_through_environment() {
        use std::sync::atomic::{AtomicU64, Ordering};
        static SEQ: AtomicU64 = AtomicU64::new(0);
        let n = SEQ.fetch_add(1, Ordering::Relaxed);
        let out = std::env::temp_dir().join(format!(
            "gpuviewer-onevent-test-{}-{n}.txt",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&out);

        // Per-OS hook command: each side must only ever run under its own dispatch (`cmd`
        // expands %VAR%, `sh` expands via printenv). No space before cmd's `>` — cmd's
        // echo would copy a trailing space into the file.
        #[cfg(windows)]
        let cmd = format!("echo %GPV_EVENT_KIND%> \"{}\"", out.display());
        #[cfg(not(windows))]
        let cmd = format!("printenv GPV_EVENT_KIND > \"{}\"", out.display());
        let mut sink = EventSink::new(cmd);
        sink.fire(&Event {
            ts_ms: 42,
            device: DeviceId("0000:01:00.0".into()),
            kind: EventKind::ThrottleStart,
            severity: Severity::Warning,
            confidence: Confidence::Fact,
            title: "t".into(),
            evidence: "e".into(),
        });

        // Wait (bounded) for the detached child to write the file. The budget is ~10s of
        // 10ms polls: Windows CI runners spawn cmd.exe slowly enough that a 2s budget is
        // flake territory, and a real wiring regression must fail THIS assertion (with
        // the diff below), never decay into a timeout that reads as infrastructure noise.
        // The fast path is unchanged — the loop exits on the first poll that sees output.
        let mut got = String::new();
        for _ in 0..1000 {
            if let Ok(s) = std::fs::read_to_string(&out) {
                if !s.trim().is_empty() {
                    got = s;
                    break;
                }
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        let _ = std::fs::remove_file(&out);
        assert_eq!(
            got.trim(),
            "throttle_start",
            "the hook must see the event kind as the wire token in GPV_EVENT_KIND"
        );
    }

    // ---- panic firewall (tick_guarded) ----

    /// A backend whose `refresh_dynamic` panics on scripted tick indices — the audit's
    /// "tick-panic frozen-UI hole" made flesh: the code path least exercised by the
    /// mock-based suite is a backend decoder edge case blowing up mid-tick.
    struct PanickyBackend<F: Fn(u32) -> bool + Send> {
        tick: u32,
        panics: F,
    }

    impl<F: Fn(u32) -> bool + Send> GpuBackend for PanickyBackend<F> {
        fn name(&self) -> &'static str {
            "panicky"
        }
        fn devices(&mut self) -> Vec<DeviceId> {
            vec![DeviceId("panic:0".into())]
        }
        fn static_info(&mut self, dev: &DeviceId) -> Result<StaticInfo, BackendError> {
            Ok(StaticInfo {
                id: dev.clone(),
                vendor: Vendor::Unknown,
                name: "Panicky GPU".into(),
                backend: "panicky".into(),
                mem_total_bytes: Some(8 << 30),
                power_limit_mw: None,
                max_sm_clock_mhz: None,
                temp_slowdown_c: None,
                driver_version: None,
                process_hint: None,
                source_caveat: None,
                pcie_max_gen: None,
                pcie_max_width: None,
            })
        }
        fn refresh_dynamic(&mut self, _dev: &DeviceId) -> Result<DynamicSample, BackendError> {
            let t = self.tick;
            self.tick += 1;
            if (self.panics)(t) {
                panic!("decoder choked on tick {t}");
            }
            Ok(sample(Some(50.0)))
        }
        fn refresh_processes(
            &mut self,
            _dev: &DeviceId,
        ) -> Result<Vec<ProcessSample>, BackendError> {
            Ok(Vec::new())
        }
    }

    /// An engine over a [`PanickyBackend`] with the given panic script, persisting to
    /// `db` when given (otherwise live-only).
    fn panicky_engine(
        db: Option<PathBuf>,
        panics: impl Fn(u32) -> bool + Send + 'static,
    ) -> Engine {
        Engine::with_backends(
            vec![Box::new(PanickyBackend { tick: 0, panics })],
            EngineConfig {
                persist: db.is_some(),
                db_path: db,
                ..Default::default()
            },
        )
    }

    fn scratch_db() -> PathBuf {
        use std::sync::atomic::{AtomicU64, Ordering};
        static SEQ: AtomicU64 = AtomicU64::new(0);
        let n = SEQ.fetch_add(1, Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "gpuviewer-panic-test-{}-{n}.db",
            std::process::id()
        ))
    }

    fn cleanup_db(path: &PathBuf) {
        let _ = std::fs::remove_file(path);
        for ext in ["-wal", "-shm", ".lock"] {
            let mut p = path.as_os_str().to_os_string();
            p.push(ext);
            let _ = std::fs::remove_file(p);
        }
    }

    /// Design (b)'s documented behavior for ONE transient panic: the tick is dropped and
    /// narrated as a `CollectorStall` FACT carrying the payload, and the very next clean
    /// tick collects normally — one decoder edge case must not end the flight recording.
    #[test]
    fn transient_tick_panic_narrates_and_collection_resumes() {
        let mut engine = panicky_engine(None, |t| t == 0);

        let TickOutcome::Panicked {
            event,
            summary,
            fatal,
        } = engine.tick_guarded()
        else {
            panic!("the scripted first tick must panic");
        };
        assert!(!fatal, "one panic is weather, not death");
        assert_eq!(event.kind, EventKind::CollectorStall);
        assert_eq!(event.confidence, Confidence::Fact);
        assert_eq!(event.severity, Severity::Warning);
        assert!(
            summary.contains("decoder choked on tick 0"),
            "the panic payload must survive into the summary: {summary}"
        );
        assert!(
            event.evidence.contains("decoder choked on tick 0"),
            "the payload must reach the event evidence: {}",
            event.evidence
        );
        assert!(
            event.evidence.contains("RUST_BACKTRACE"),
            "the evidence must carry the backtrace hint: {}",
            event.evidence
        );

        let TickOutcome::Frame(frame) = engine.tick_guarded() else {
            panic!("the next tick is clean and must produce a frame");
        };
        assert!(
            frame.devices[0].sample.is_some(),
            "collection resumed with real data"
        );
    }

    /// The budget counts CONSECUTIVE panics: a clean tick resets it, so an intermittent
    /// fault keeps collecting indefinitely; only [`MAX_CONSECUTIVE_PANICS`] in a row die.
    #[test]
    fn panic_budget_resets_on_a_clean_tick() {
        // Script: panic, clean, panic, panic, panic — fatal only at the third CONSECUTIVE
        // panic (tick 4); a total-panic counter would have died at tick 3.
        let mut engine = panicky_engine(None, |t| t != 1);
        let fatals: Vec<bool> = (0..5)
            .map(|_| match engine.tick_guarded() {
                TickOutcome::Frame(_) => false,
                TickOutcome::Panicked { fatal, .. } => fatal,
            })
            .collect();
        assert_eq!(
            fatals,
            vec![false, false, false, false, true],
            "fatal exactly when {MAX_CONSECUTIVE_PANICS} panics run consecutively"
        );
    }

    /// Spending the budget produces the Critical stop fact, still under the existing
    /// `CollectorStall` kind — the NDJSON contract (spec/schema/suite) is untouched.
    #[test]
    fn deterministic_panic_turns_fatal_with_a_critical_fact() {
        let mut engine = panicky_engine(None, |_| true);
        for i in 1..MAX_CONSECUTIVE_PANICS {
            match engine.tick_guarded() {
                TickOutcome::Panicked { fatal, .. } => {
                    assert!(!fatal, "panic {i} is within the budget")
                }
                TickOutcome::Frame(_) => panic!("scripted to panic"),
            }
        }
        let TickOutcome::Panicked { event, fatal, .. } = engine.tick_guarded() else {
            panic!("scripted to panic");
        };
        assert!(
            fatal,
            "the {MAX_CONSECUTIVE_PANICS}th consecutive panic is fatal"
        );
        assert_eq!(event.kind, EventKind::CollectorStall);
        assert_eq!(event.severity, Severity::Critical);
        assert_eq!(event.confidence, Confidence::Fact);
        assert!(
            event.title.contains("STOPPED"),
            "the stop fact must say collection stopped: {}",
            event.title
        );
    }

    /// The panic facts reach the persistent event log while the recorder is reachable:
    /// the hole survives into replay and `report`, not just the live session.
    #[test]
    fn panic_events_are_recorded_to_the_store() {
        let path = scratch_db();
        let mut engine = panicky_engine(Some(path.clone()), |_| true);
        for _ in 0..MAX_CONSECUTIVE_PANICS {
            assert!(matches!(
                engine.tick_guarded(),
                TickOutcome::Panicked { .. }
            ));
        }
        drop(engine); // Drop flushes the recording tail.

        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        let stalls: Vec<_> = events
            .iter()
            .filter(|e| e.kind == EventKind::CollectorStall)
            .collect();
        assert_eq!(
            stalls.len(),
            MAX_CONSECUTIVE_PANICS as usize,
            "every panicked tick must persist its narration"
        );
        assert!(
            stalls.iter().any(|e| e.severity == Severity::Critical),
            "the stop itself is a Critical fact in the recording"
        );
        cleanup_db(&path);
    }

    /// The end-to-end shape of the fix: through [`Collector::start`]'s real thread, a
    /// deterministic backend panic must surface as `Shared::stopped` (the UI's stop
    /// banner) plus `CollectorStall` narration in the story feed — and the process keeps
    /// running rather than aborting (the old behavior froze the UI with zero signal).
    #[test]
    fn collector_thread_survives_panics_and_marks_shared_stopped() {
        let engine = panicky_engine(None, |_| true);
        let collector = Collector::start(engine, Duration::from_millis(5), false);

        // Bounded wait for the thread to spend the panic budget and declare death.
        let mut stopped = None;
        for _ in 0..400 {
            if let Some(stop) = collector.shared.lock().unwrap().stopped.clone() {
                stopped = Some(stop);
                break;
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        let stop = stopped.expect("the collector must declare itself dead, never vanish silently");
        assert!(
            stop.reason.contains("decoder choked"),
            "the stop reason carries the panic summary: {}",
            stop.reason
        );
        assert!(stop.at_ms > 0);

        let sh = collector.shared.lock().unwrap();
        let stalls: Vec<_> = sh
            .history
            .events()
            .iter()
            .filter(|e| e.kind == EventKind::CollectorStall)
            .collect();
        assert_eq!(
            stalls.len(),
            MAX_CONSECUTIVE_PANICS as usize,
            "every panicked tick narrates into the story feed"
        );
        assert_eq!(
            stalls.last().unwrap().severity,
            Severity::Critical,
            "the final narration is the Critical stop fact"
        );
        assert!(
            sh.latest[0].is_none(),
            "no fabricated sample may appear for a device that never answered"
        );
        // tick_seq advanced per narration, so an open timeline re-queries and sees them.
        assert!(sh.tick_seq >= MAX_CONSECUTIVE_PANICS as u64);
    }

    // ---- device lost / returned (scripted flaky backend) ----

    /// A backend whose `refresh_dynamic` errors on scripted tick indices — the audit's
    /// silently-disappearing device (driver reset, eGPU unplug, xe rebind) made flesh.
    /// Sample timestamps advance 10s per tick so the persistence test lands each good
    /// tick in its own 10s rollup bucket. The process probe mirrors the dynamic one (a
    /// dead device cannot list processes either), with one process on good ticks so the
    /// frozen-process-list assertion has something to freeze.
    struct FlakyBackend<F: Fn(u32) -> bool + Send> {
        tick: u32,
        fails: F,
    }

    impl<F: Fn(u32) -> bool + Send> GpuBackend for FlakyBackend<F> {
        fn name(&self) -> &'static str {
            "flaky"
        }
        fn devices(&mut self) -> Vec<DeviceId> {
            vec![DeviceId("flaky:0".into())]
        }
        fn static_info(&mut self, dev: &DeviceId) -> Result<StaticInfo, BackendError> {
            Ok(StaticInfo {
                id: dev.clone(),
                vendor: Vendor::Unknown,
                name: "Flaky GPU".into(),
                backend: "flaky".into(),
                mem_total_bytes: Some(8 << 30),
                power_limit_mw: None,
                max_sm_clock_mhz: None,
                temp_slowdown_c: None,
                driver_version: None,
                process_hint: None,
                source_caveat: None,
                pcie_max_gen: None,
                pcie_max_width: None,
            })
        }
        fn refresh_dynamic(&mut self, _dev: &DeviceId) -> Result<DynamicSample, BackendError> {
            let t = self.tick;
            self.tick += 1;
            if (self.fails)(t) {
                Err(BackendError::Unavailable("probe timed out".into()))
            } else {
                let mut s = sample(Some(50.0));
                s.ts_ms = 10_000 * (t as u64 + 1);
                Ok(s)
            }
        }
        fn refresh_processes(
            &mut self,
            _dev: &DeviceId,
        ) -> Result<Vec<ProcessSample>, BackendError> {
            // `tick` was already advanced by this tick's refresh_dynamic.
            if self.tick > 0 && (self.fails)(self.tick - 1) {
                Err(BackendError::Unavailable("probe timed out".into()))
            } else {
                Ok(vec![proc(Some(10.0))])
            }
        }
    }

    /// An engine over a [`FlakyBackend`] with the given failure script, persisting to
    /// `db` when given (otherwise live-only).
    fn flaky_engine(db: Option<PathBuf>, fails: impl Fn(u32) -> bool + Send + 'static) -> Engine {
        Engine::with_backends(
            vec![Box::new(FlakyBackend { tick: 0, fails })],
            EngineConfig {
                persist: db.is_some(),
                db_path: db,
                ..Default::default()
            },
        )
    }

    /// A never-failing persisting [`flaky_engine`] over `path`, retrying (bounded) while
    /// the store's instance lock is transiently held. WHY: the lock is `flock`-based and
    /// lives on the open file DESCRIPTION — when an unrelated test's `Command::spawn`
    /// forks while a previous holder's lock fd is open, the child inherits the fd and
    /// keeps the lock alive until exec closes it (CLOEXEC). The spawning thread is
    /// vfork-blocked through that window, but OTHER test threads (this one) keep running
    /// into it, so a test that re-acquires a lock its own process just dropped can lose
    /// the race for a few microseconds. Transient by construction; a REAL lock leak
    /// still fails the budget loudly. Tests opening a path's lock for the FIRST time
    /// are not exposed (a never-held lock cannot have been inherited) and use
    /// [`flaky_engine`] directly.
    fn flaky_engine_persisting(path: &std::path::Path) -> Engine {
        for _ in 0..500 {
            let engine = flaky_engine(Some(path.to_path_buf()), |_| false);
            if engine.db_path().is_some() {
                return engine;
            }
            drop(engine);
            std::thread::sleep(Duration::from_millis(10));
        }
        panic!(
            "instance lock on {} still held after 5s — a real lock leak, not the \
             fork-inheritance window",
            path.display()
        );
    }

    /// The new kinds' wire tokens must match the spec/schema/suite spelling exactly.
    /// The conformance suite cannot see them on a mock run (the mock never loses a
    /// device), so the token spelling is pinned here instead.
    #[test]
    fn device_lifecycle_kinds_use_documented_wire_tokens() {
        assert_eq!(env_token(EventKind::DeviceLost), "device_lost");
        assert_eq!(env_token(EventKind::DeviceReturned), "device_returned");
    }

    /// The detection policy end to end: failures below the threshold are silent weather
    /// (the frame's `sample: null` already shows the gap on the wire), the Nth
    /// consecutive failure declares the loss as a Critical FACT with auditable evidence,
    /// and a dead device stays listed — and stays lost without re-narrating.
    #[test]
    fn device_lost_fires_exactly_at_the_threshold_with_evidence() {
        const N: u32 = DEVICE_LOST_AFTER_FAILED_PROBES;
        // Two good ticks, then the device falls off the bus for good.
        let mut engine = flaky_engine(None, |t| t >= 2);
        let lost_tick = 2 + N - 1;
        for t in 0..(lost_tick + 4) {
            let TickOutcome::Frame(frame) = engine.tick_guarded() else {
                panic!("flaky ticks never panic");
            };
            assert_eq!(frame.devices.len(), 1, "a lost device must stay listed");
            assert_eq!(frame.devices[0].sample.is_some(), t < 2);
            let lost: Vec<&Event> = frame
                .events
                .iter()
                .filter(|e| e.kind == EventKind::DeviceLost)
                .collect();
            if t == lost_tick {
                assert_eq!(lost.len(), 1, "the {N}th consecutive failure declares it");
                let e = lost[0];
                assert_eq!(e.severity, Severity::Critical);
                assert_eq!(e.confidence, Confidence::Fact, "the silence is observed");
                assert_eq!(e.device.0, "flaky:0");
                assert!(
                    e.title.contains("GPU0") && e.title.contains("device lost"),
                    "title must name the device and the loss: {}",
                    e.title
                );
                assert!(
                    !e.title.contains("likely"),
                    "a fact must not read hedged: {}",
                    e.title
                );
                assert!(
                    e.evidence.contains(&format!("{N} consecutive")),
                    "evidence must carry the tick count: {}",
                    e.evidence
                );
                // The last good sample was tick 1 → ts 20000 ms.
                assert!(
                    e.evidence.contains("20000 ms"),
                    "evidence must carry the last-good timestamp: {}",
                    e.evidence
                );
                assert!(
                    e.evidence.contains("probe timed out"),
                    "evidence must carry the driver's error string: {}",
                    e.evidence
                );
                assert!(
                    e.evidence.contains("cause not asserted"),
                    "the cause must be explicitly unclaimed: {}",
                    e.evidence
                );
            } else {
                assert!(
                    lost.is_empty(),
                    "tick {t}: device_lost only at the threshold tick"
                );
            }
            assert!(
                frame
                    .events
                    .iter()
                    .all(|e| e.kind != EventKind::DeviceReturned),
                "the device never recovers in this script"
            );
        }
    }

    /// Below the threshold a failing streak is per-tick weather: the frame's
    /// `sample: null` shows the gap, but neither lifecycle event may narrate (recovering
    /// from an undeclared loss is no story either).
    #[test]
    fn transient_probe_failures_below_threshold_stay_silent() {
        const N: u32 = DEVICE_LOST_AFTER_FAILED_PROBES;
        // N-1 consecutive failures, then the device answers again.
        let mut engine = flaky_engine(None, move |t| (2..2 + N - 1).contains(&t));
        for _ in 0..12 {
            let TickOutcome::Frame(frame) = engine.tick_guarded() else {
                panic!("flaky ticks never panic");
            };
            assert!(
                frame.events.iter().all(|e| {
                    e.kind != EventKind::DeviceLost && e.kind != EventKind::DeviceReturned
                }),
                "a sub-threshold blip must not narrate device loss"
            );
        }
    }

    /// The recovery edge: a lost device answering again narrates `device_returned`
    /// (Info, FACT) — and a second outage re-narrates `device_lost`, so a flapping
    /// device tells each chapter exactly once.
    #[test]
    fn device_returned_closes_the_story_and_a_second_outage_reopens_it() {
        const N: u32 = DEVICE_LOST_AFTER_FAILED_PROBES;
        assert_eq!(
            N, 5,
            "the scripted outage windows below assume the threshold"
        );
        // Outage 1: ticks 2..=6 (lost at 6); good 7..=8 (returned at 7);
        // outage 2: ticks 9..=13 (lost at 13); good from 14 (returned at 14).
        let mut engine = flaky_engine(None, |t| matches!(t, 2..=6 | 9..=13));
        let mut lost = Vec::new();
        let mut returned = Vec::new();
        for _ in 0..16 {
            let TickOutcome::Frame(frame) = engine.tick_guarded() else {
                panic!("flaky ticks never panic");
            };
            for e in frame.events {
                match e.kind {
                    EventKind::DeviceLost => lost.push(e),
                    EventKind::DeviceReturned => returned.push(e),
                    _ => {}
                }
            }
        }
        assert_eq!(lost.len(), 2, "each outage narrates its own loss");
        assert_eq!(returned.len(), 2, "each recovery narrates its own return");
        let r = &returned[0];
        assert_eq!(
            r.severity,
            Severity::Info,
            "recovery is good news, not an alarm"
        );
        assert_eq!(r.confidence, Confidence::Fact);
        assert!(
            r.title.contains("GPU0") && r.title.contains("returned"),
            "return title must name the device: {}",
            r.title
        );
        assert!(
            r.title.contains("data are missing"),
            "the return must size the hole it closes: {}",
            r.title
        );
        assert!(
            r.evidence.contains("never zero-filled"),
            "the return must state the gap stays blank: {}",
            r.evidence
        );
    }

    /// The full pipeline through [`Collector::start`]'s real thread: a device that stops
    /// answering surfaces as a per-device `Shared::lost` marker (the UI's stale
    /// affordances) WITHOUT tripping `Shared::stopped` (collection itself is fine, other
    /// devices would still be live), keeps its last process list frozen rather than
    /// flashing empty, and the marker clears when the device answers again — with both
    /// edges in the story feed.
    #[test]
    fn lost_device_marks_shared_and_recovery_clears_it() {
        // Good ticks 0..=2 (so a last process list exists), a long outage (declared lost
        // at tick 3 + threshold - 1), recovery from tick 100.
        let engine = flaky_engine(None, |t| (3..100).contains(&t));
        let collector = Collector::start(engine, Duration::from_millis(5), false);

        // Bounded wait for the loss to be declared.
        let mut lost_seen = false;
        for _ in 0..1000 {
            let sh = collector.shared.lock().unwrap();
            if sh.lost[0].is_some() {
                lost_seen = true;
                assert!(sh.stopped.is_none(), "device loss is not a collector stop");
                assert!(
                    !sh.processes[0].is_empty(),
                    "the last good process list stays frozen, never flashes empty"
                );
                assert!(sh.latest[0].is_some(), "the last good sample is kept");
                break;
            }
            drop(sh);
            std::thread::sleep(Duration::from_millis(2));
        }
        assert!(lost_seen, "the device loss must reach Shared");

        // Bounded wait for the recovery to clear the marker.
        let mut cleared = false;
        for _ in 0..2000 {
            let sh = collector.shared.lock().unwrap();
            if sh.lost[0].is_none() {
                cleared = true;
                break;
            }
            drop(sh);
            std::thread::sleep(Duration::from_millis(5));
        }
        assert!(cleared, "recovery must clear the per-device marker");

        let sh = collector.shared.lock().unwrap();
        let kinds: Vec<EventKind> = sh.history.events().iter().map(|e| e.kind).collect();
        assert!(kinds.contains(&EventKind::DeviceLost));
        assert!(kinds.contains(&EventKind::DeviceReturned));
    }

    /// The recording half: both lifecycle facts reach the persistent event log, and the
    /// lost stretch leaves NO sample rollups — the gap stays blank (a hole), never
    /// zero-filled (CLAUDE.md's "never write raw zeros for absence" rule applied to an
    /// absent device).
    #[test]
    fn device_lifecycle_events_persist_and_the_gap_has_no_rollups() {
        const N: u32 = DEVICE_LOST_AFTER_FAILED_PROBES;
        let path = scratch_db();
        // Good tick 0 (bucket 10000), outage ticks 1..=N (lost at tick N), good from
        // tick N+1 (bucket 10000·(N+2)).
        let mut engine = flaky_engine(Some(path.clone()), move |t| (1..=N).contains(&t));
        for _ in 0..(N + 2) {
            assert!(matches!(engine.tick_guarded(), TickOutcome::Frame(_)));
        }
        drop(engine); // Drop flushes the recording tail.

        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        assert_eq!(
            events
                .iter()
                .filter(|e| e.kind == EventKind::DeviceLost)
                .count(),
            1,
            "the loss is recorded exactly once"
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| e.kind == EventKind::DeviceReturned)
                .count(),
            1,
            "the return is recorded exactly once"
        );
        let buckets: Vec<u64> = store
            .samples_between(&DeviceId("flaky:0".into()), 0, 1_000_000, Tier::TenSec)
            .unwrap()
            .iter()
            .map(|r| r.bucket_ms)
            .collect();
        assert_eq!(
            buckets,
            vec![10_000, 10_000 * (N as u64 + 2)],
            "only the good ticks produced rollups — the lost stretch is a blank gap"
        );
        cleanup_db(&path);
    }

    // ---- collector self-honesty: stall gap + slow probe (CollectorStall) ----

    /// A backend whose `refresh_dynamic` genuinely takes longer than [`SLOW_PROBE`] on
    /// scripted tick indices — the slow-probe note's trigger (NVML PCIe-throughput calls
    /// and a sleeping GPU waking really do block like this). The probe duration is
    /// measured with a real `Instant` inside `tick` (no clock seam exists there), so the
    /// scripted slowness must be real elapsed time inside the probe; sleeping 50ms past
    /// the threshold means scheduler jitter can only ADD time — the test can never flake
    /// toward "not slow".
    struct SluggishBackend<F: Fn(u32) -> bool + Send> {
        tick: u32,
        slow: F,
    }

    impl<F: Fn(u32) -> bool + Send> GpuBackend for SluggishBackend<F> {
        fn name(&self) -> &'static str {
            "sluggish"
        }
        fn devices(&mut self) -> Vec<DeviceId> {
            vec![DeviceId("sluggish:0".into())]
        }
        fn static_info(&mut self, dev: &DeviceId) -> Result<StaticInfo, BackendError> {
            Ok(StaticInfo {
                id: dev.clone(),
                vendor: Vendor::Unknown,
                name: "Sluggish GPU".into(),
                backend: "sluggish".into(),
                mem_total_bytes: Some(8 << 30),
                power_limit_mw: None,
                max_sm_clock_mhz: None,
                temp_slowdown_c: None,
                driver_version: None,
                process_hint: None,
                source_caveat: None,
                pcie_max_gen: None,
                pcie_max_width: None,
            })
        }
        fn refresh_dynamic(&mut self, _dev: &DeviceId) -> Result<DynamicSample, BackendError> {
            let t = self.tick;
            self.tick += 1;
            if (self.slow)(t) {
                std::thread::sleep(SLOW_PROBE + Duration::from_millis(50));
            }
            Ok(sample(Some(50.0)))
        }
        fn refresh_processes(
            &mut self,
            _dev: &DeviceId,
        ) -> Result<Vec<ProcessSample>, BackendError> {
            Ok(Vec::new())
        }
    }

    /// An engine over a [`SluggishBackend`] with the given slowness script, live-only.
    fn sluggish_engine(slow: impl Fn(u32) -> bool + Send + 'static) -> Engine {
        Engine::with_backends(
            vec![Box::new(SluggishBackend { tick: 0, slow })],
            EngineConfig::default(),
        )
    }

    /// The inter-tick stall gap — the collector narrating its OWN hole: a between-tick
    /// gap past [`stall_threshold`] must surface as a `CollectorStall` WARNING fact on
    /// the next successful tick, anchored to the first device, with the threshold in the
    /// auditable evidence — and a normal next gap must NOT re-narrate (the stall is a
    /// per-occurrence fact, not a latched state). Driven through the `set_last_tick_end`
    /// seam: `tick` reads `Instant::now()` directly, and sleeping a real 5s+ gap would
    /// be exactly the wall-clock flake the suite forbids.
    #[test]
    fn inter_tick_gap_past_threshold_narrates_a_collector_stall() {
        let mut engine = flaky_engine(None, |_| false);

        // First tick: no previous tick end exists, so no gap can be claimed.
        let TickOutcome::Frame(first) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            first
                .events
                .iter()
                .all(|e| e.kind != EventKind::CollectorStall),
            "no stall may be narrated before a gap was ever measurable"
        );

        // Inject a previous-tick end 6s in the past — past the 5s threshold at the
        // default 1s interval (stall_threshold = max(3×1s, 5s)).
        let past = Instant::now()
            .checked_sub(Duration::from_secs(6))
            .expect("system uptime exceeds 6s");
        engine.set_last_tick_end(past);
        let TickOutcome::Frame(stalled) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        let stalls: Vec<&Event> = stalled
            .events
            .iter()
            .filter(|e| e.kind == EventKind::CollectorStall)
            .collect();
        assert_eq!(stalls.len(), 1, "the gap must be narrated exactly once");
        let e = stalls[0];
        assert_eq!(
            e.severity,
            Severity::Warning,
            "a recording hole is a warning"
        );
        assert_eq!(
            e.confidence,
            Confidence::Fact,
            "the gap was measured, not inferred"
        );
        assert_eq!(
            e.device.0, "flaky:0",
            "anchored to the first device like every collector-scoped fact"
        );
        assert!(
            e.title.contains("collection stalled"),
            "title must say collection stalled: {}",
            e.title
        );
        assert!(
            e.title.contains("last good frame at"),
            "title must place the last good frame in time: {}",
            e.title
        );
        assert!(
            e.evidence.contains("inter-tick gap") && e.evidence.contains("stall threshold"),
            "evidence must carry the measured gap and the threshold it crossed: {}",
            e.evidence
        );
        assert!(
            stalled.devices[0].sample.is_some(),
            "the narrating tick itself collected normally — the hole is behind it"
        );

        // Recovery: the next tick's gap is normal and must stay silent.
        let TickOutcome::Frame(after) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            after
                .events
                .iter()
                .all(|e| e.kind != EventKind::CollectorStall),
            "a normal gap after a narrated stall must not re-narrate"
        );
    }

    /// Regression: the adaptive backoff sleeps 5× the interval (capped at 10s). The stall
    /// threshold used to be computed from the configured interval alone, so a deliberate
    /// 5s low-power sleep at the default 1s interval (threshold max(3s, 5s) = 5s) plus a
    /// few microseconds of bookkeeping narrated "collection stalled" on every backed-off
    /// tick. Told the cadence, the engine judges the gap against it.
    #[test]
    fn a_backed_off_sleep_is_not_a_stall() {
        let mut engine = flaky_engine(None, |_| false);
        let _ = engine.tick_guarded();
        engine.set_cadence(Duration::from_secs(5));
        engine.set_last_tick_end(
            Instant::now()
                .checked_sub(Duration::from_millis(5_050))
                .expect("uptime exceeds 6s"),
        );
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            f.events.iter().all(|e| e.kind != EventKind::CollectorStall),
            "a gap that matches the backoff cadence is the schedule, not a stall: {:?}",
            f.events.iter().map(|e| &e.title).collect::<Vec<_>>()
        );

        // A gap well past the STRETCHED cadence's threshold (3×5s = 15s) still narrates.
        engine.set_last_tick_end(
            Instant::now()
                .checked_sub(Duration::from_secs(16))
                .expect("uptime exceeds 16s"),
        );
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        let stall = f
            .events
            .iter()
            .find(|e| e.kind == EventKind::CollectorStall)
            .expect("a real hole past the cadence threshold must still be narrated");
        assert!(
            !stall.title.contains("probe blocked"),
            "an inter-tick gap cannot be a blocked probe (probes run inside a tick): {}",
            stall.title
        );
        assert!(
            stall.evidence.contains("cadence 5.0s"),
            "{}",
            stall.evidence
        );
    }

    /// The suspend signal: the monotonic clock barely moved but the wall clock jumped an
    /// hour. That is narrated as a hedged `host_suspended` (never a stall warning —
    /// nothing was wrong with the collector), with both clocks in the evidence.
    #[test]
    fn wall_clock_jump_without_monotonic_advance_narrates_a_suspend() {
        let mut engine = flaky_engine(None, |_| false);
        let _ = engine.tick_guarded();
        engine.set_last_tick_end(
            Instant::now()
                .checked_sub(Duration::from_secs(1))
                .expect("uptime exceeds 1s"),
        );
        engine.set_last_tick_end_wall(now_ms() - 3_600_000);
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        let sus: Vec<&Event> = f
            .events
            .iter()
            .filter(|e| e.kind == EventKind::HostSuspended)
            .collect();
        assert_eq!(sus.len(), 1, "exactly one suspend narration");
        let e = sus[0];
        assert_eq!(
            e.confidence,
            Confidence::Likely,
            "a clock step looks the same"
        );
        assert_eq!(e.severity, Severity::Info, "sleeping is normal life");
        assert!(
            e.title.contains("likely"),
            "inference titles hedge: {}",
            e.title
        );
        assert!(
            e.evidence.contains("wall clock") && e.evidence.contains("monotonic"),
            "{}",
            e.evidence
        );
        assert!(
            f.events.iter().all(|e| e.kind != EventKind::CollectorStall),
            "a suspend is not a collector stall"
        );

        // A small divergence (well under SUSPEND_MIN_MS) is scheduling noise: silent.
        engine.set_last_tick_end(
            Instant::now()
                .checked_sub(Duration::from_secs(1))
                .expect("uptime exceeds 1s"),
        );
        engine.set_last_tick_end_wall(now_ms() - 3_000);
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(f.events.iter().all(|e| e.kind != EventKind::HostSuspended));
    }

    /// A user pause is a deliberate hole: narrated once, at Info, as a pause.
    #[test]
    fn a_user_pause_is_narrated_as_a_pause_not_a_stall() {
        let mut engine = flaky_engine(None, |_| false);
        let _ = engine.tick_guarded();
        engine.note_paused();
        engine.set_last_tick_end(
            Instant::now()
                .checked_sub(Duration::from_secs(60))
                .expect("uptime exceeds 60s"),
        );
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        let holes: Vec<&Event> = f
            .events
            .iter()
            .filter(|e| e.kind == EventKind::CollectorStall)
            .collect();
        assert_eq!(holes.len(), 1);
        assert_eq!(holes[0].severity, Severity::Info);
        assert!(holes[0].title.contains("paused"), "{}", holes[0].title);
        let TickOutcome::Frame(f) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            f.events.iter().all(|e| e.kind != EventKind::CollectorStall),
            "the pause narrates once"
        );
    }

    /// The slow-probe foreshadowing note: one probe past [`SLOW_PROBE`] narrates an INFO
    /// `CollectorStall` fact against the slow device itself; a second slow probe inside
    /// the cooldown stays silent (a persistently-slow driver must not flood the feed);
    /// a fast probe afterwards narrates nothing — and every tick, slow or not, still
    /// collects real data (slow is not lost).
    #[test]
    fn slow_probe_notes_once_per_cooldown_and_recovers_silently() {
        // Ticks 0 and 1 are slow; tick 2 is fast.
        let mut engine = sluggish_engine(|t| t <= 1);

        let TickOutcome::Frame(first) = engine.tick_guarded() else {
            panic!("sluggish ticks never panic");
        };
        let notes: Vec<&Event> = first
            .events
            .iter()
            .filter(|e| e.kind == EventKind::CollectorStall)
            .collect();
        assert_eq!(notes.len(), 1, "a slow probe must be noted");
        let e = notes[0];
        assert_eq!(
            e.severity,
            Severity::Info,
            "a slow probe is a foreshadowing note, not an alarm"
        );
        assert_eq!(
            e.confidence,
            Confidence::Fact,
            "the probe duration was measured"
        );
        assert_eq!(
            e.device.0, "sluggish:0",
            "the note is anchored to the slow device itself"
        );
        assert!(
            e.title.contains("probe took") && e.title.contains("driver slow to respond"),
            "title must name the slowness and its shape: {}",
            e.title
        );
        assert!(
            e.title.contains("sluggish"),
            "title must name which backend was slow: {}",
            e.title
        );
        assert!(
            e.evidence.contains("refresh_dynamic") && e.evidence.contains("threshold"),
            "evidence must carry the probe and the threshold it crossed: {}",
            e.evidence
        );
        assert!(
            first.devices[0].sample.is_some(),
            "the slow tick still collected — slow is not lost"
        );

        // A second slow probe within the cooldown: rate-capped, no second note.
        let TickOutcome::Frame(second) = engine.tick_guarded() else {
            panic!("sluggish ticks never panic");
        };
        assert!(
            second
                .events
                .iter()
                .all(|e| e.kind != EventKind::CollectorStall),
            "a persistently-slow driver must not flood the story feed"
        );
        assert!(second.devices[0].sample.is_some());

        // Recovery: a fast probe narrates nothing and collection is normal.
        let TickOutcome::Frame(third) = engine.tick_guarded() else {
            panic!("sluggish ticks never panic");
        };
        assert!(
            third
                .events
                .iter()
                .all(|e| e.kind != EventKind::CollectorStall),
            "a recovered (fast) probe must stay silent"
        );
        assert!(third.devices[0].sample.is_some());
    }

    // ---- history reset (corrupt-db quarantine narration) ----

    /// The recorder narrating its own amnesia: a corrupt history file is quarantined at
    /// open and the store starts fresh — the collector must say so as a one-shot
    /// `HistoryReset` WARNING fact riding the FIRST tick (before the session's start
    /// mark), and the fresh store must work normally underneath it. Without the
    /// narration, the vanished history would masquerade as the GPU having no past.
    #[test]
    fn corrupt_history_file_narrates_history_reset_on_the_first_tick() {
        const GARBAGE: &[u8] = b"definitely not a sqlite database";
        let path = scratch_db();
        std::fs::write(&path, GARBAGE).unwrap();

        let mut engine = flaky_engine(Some(path.clone()), |_| false);
        let TickOutcome::Frame(first) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        let resets: Vec<usize> = first
            .events
            .iter()
            .enumerate()
            .filter(|(_, e)| e.kind == EventKind::HistoryReset)
            .map(|(i, _)| i)
            .collect();
        assert_eq!(
            resets.len(),
            1,
            "the quarantine must be narrated exactly once"
        );
        let e = &first.events[resets[0]];
        assert_eq!(
            e.severity,
            Severity::Warning,
            "lost history is a warning, not routine lifecycle"
        );
        assert_eq!(
            e.confidence,
            Confidence::Fact,
            "the quarantine happened — observed, not inferred"
        );
        assert_eq!(
            e.device.0, "flaky:0",
            "anchored to the first device like every collector-scoped fact"
        );
        assert!(
            e.title.contains("corrupt") && e.title.contains("started fresh"),
            "title must state the loss and the recovery: {}",
            e.title
        );
        assert!(
            e.evidence.contains(".corrupt-"),
            "evidence must point at the preserved quarantine file: {}",
            e.evidence
        );
        assert!(
            e.evidence.contains("fresh database"),
            "evidence must state a fresh database was created: {}",
            e.evidence
        );
        let start_pos = first
            .events
            .iter()
            .position(|ev| ev.kind == EventKind::RecordingStarted)
            .expect("the session's start mark rides the first frame too");
        assert!(
            resets[0] < start_pos,
            "the reset narrates before this session's start mark — the amnesia \
             predates the session"
        );

        // One-shot: the second tick must not repeat it.
        let TickOutcome::Frame(second) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            second
                .events
                .iter()
                .all(|e| e.kind != EventKind::HistoryReset),
            "the reset is narrated once, on the first tick only"
        );
        drop(engine); // Drop flushes the recording tail.

        // Downstream coherence: the fresh database is real and working — the narration
        // itself persisted into it (so replay and `report` see the reset), the start
        // mark landed, and the devices registered.
        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        assert_eq!(
            events
                .iter()
                .filter(|e| e.kind == EventKind::HistoryReset)
                .count(),
            1,
            "the reset fact survives into replay and report"
        );
        assert!(
            events.iter().any(|e| e.kind == EventKind::RecordingStarted),
            "the fresh store carries this session's start mark"
        );
        assert!(
            !store.devices().unwrap().is_empty(),
            "the fresh store registered the devices"
        );
        drop(store);

        // The corrupt original was preserved (renamed aside, never deleted) — find it,
        // verify the bytes survived, and clean it up with the db.
        let dir = path.parent().unwrap();
        let prefix = format!("{}.corrupt-", path.file_name().unwrap().to_string_lossy());
        let quarantined: Vec<PathBuf> = std::fs::read_dir(dir)
            .unwrap()
            .filter_map(|d| d.ok())
            .map(|d| d.path())
            .filter(|p| {
                p.file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.starts_with(&prefix))
            })
            .collect();
        assert!(
            !quarantined.is_empty(),
            "the corrupt file must be preserved for manual recovery, not deleted"
        );
        assert_eq!(
            std::fs::read(&quarantined[0]).unwrap(),
            GARBAGE,
            "preservation means the original bytes survive untouched"
        );
        for q in quarantined {
            let _ = std::fs::remove_file(q);
        }
        cleanup_db(&path);
    }

    // ---- session boundaries (recording_started / recording_stopped) ----

    /// The new kinds' wire tokens must match the spec/schema/suite spelling exactly —
    /// pinned here like the device-lifecycle tokens, since the conformance suite covers
    /// the mock run's shape, not every spelling source.
    #[test]
    fn session_boundary_kinds_use_documented_wire_tokens() {
        assert_eq!(env_token(EventKind::RecordingStarted), "recording_started");
        assert_eq!(env_token(EventKind::RecordingStopped), "recording_stopped");
    }

    /// The audit's recording-visibility hole, closed end to end: one Engine
    /// create→tick→finish cycle lands exactly one start mark (with auditable evidence:
    /// version, interval, backends, device count, db name) and exactly one stop mark
    /// (duration + frames folded) — and `finish` stays idempotent across the Drop that
    /// follows it. A second, clean session must then start WITHOUT indicting its
    /// predecessor.
    #[test]
    fn session_start_and_stop_marks_land_in_the_store() {
        let path = scratch_db();
        let mut engine = flaky_engine(Some(path.clone()), |_| false);
        for _ in 0..3 {
            assert!(matches!(engine.tick_guarded(), TickOutcome::Frame(_)));
        }
        assert!(
            engine.finish().is_some(),
            "finish must return the stop mark it wrote"
        );
        drop(engine); // Drop calls finish again — the mark must not double.

        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        let starts: Vec<&Event> = events
            .iter()
            .filter(|e| e.kind == EventKind::RecordingStarted)
            .collect();
        let stops: Vec<&Event> = events
            .iter()
            .filter(|e| e.kind == EventKind::RecordingStopped)
            .collect();
        assert_eq!(
            starts.len(),
            1,
            "exactly one start mark (the first tick's re-insert is deduped)"
        );
        assert_eq!(
            stops.len(),
            1,
            "exactly one stop mark (finish is idempotent across Drop)"
        );

        let start = starts[0];
        assert_eq!(start.severity, Severity::Info);
        assert_eq!(start.confidence, Confidence::Fact);
        assert_eq!(start.device.0, "flaky:0", "anchored like CollectorStall");
        assert!(
            start.title.contains("recording started"),
            "title: {}",
            start.title
        );
        assert!(
            !start.title.contains("without a stop mark"),
            "a fresh database has no predecessor to indict: {}",
            start.title
        );
        assert!(
            start.evidence.contains(env!("CARGO_PKG_VERSION")),
            "evidence must carry the binary version: {}",
            start.evidence
        );
        assert!(
            start.evidence.contains("interval 1.0s"),
            "evidence must carry the tick interval: {}",
            start.evidence
        );
        assert!(
            start.evidence.contains("flaky (1 device)"),
            "evidence must carry backend names and device count: {}",
            start.evidence
        );
        let db_name = path.file_name().unwrap().to_str().unwrap();
        assert!(
            start.evidence.contains(db_name),
            "evidence must carry the db basename: {}",
            start.evidence
        );

        let stop = stops[0];
        assert_eq!(stop.severity, Severity::Info);
        assert_eq!(stop.confidence, Confidence::Fact);
        assert_eq!(stop.device.0, "flaky:0");
        assert!(
            stop.title.contains("recording stopped"),
            "title: {}",
            stop.title
        );
        assert!(
            stop.evidence.contains("3 frames folded"),
            "evidence must count the session's frames: {}",
            stop.evidence
        );
        assert!(stop.ts_ms >= start.ts_ms, "the stop mark follows the start");
        drop(store);

        // A clean predecessor: the next session must start plainly. (The 2ms separator
        // is not a race wait — it only guarantees the two start marks land on distinct
        // millisecond timestamps, so the dedupe index cannot collapse them.)
        std::thread::sleep(Duration::from_millis(2));
        drop(flaky_engine_persisting(&path));
        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        let second_start = events
            .iter()
            .filter(|e| e.kind == EventKind::RecordingStarted)
            .max_by_key(|e| e.ts_ms)
            .unwrap();
        assert!(
            !second_start.title.contains("without a stop mark"),
            "a cleanly-stopped predecessor must not be narrated as a crash: {}",
            second_start.title
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| e.kind == EventKind::RecordingStopped)
                .count(),
            2,
            "both sessions wrote their stop marks"
        );
        cleanup_db(&path);
    }

    /// The unclean-shutdown asymmetry: a session that died without its stop mark
    /// (SIGKILL, OOM kill, power loss — simulated by inserting a dangling start mark)
    /// is narrated by the NEXT session's start event, title and evidence both, because
    /// the dead session by definition could not narrate itself.
    #[test]
    fn unclean_shutdown_is_narrated_by_the_next_start() {
        let path = scratch_db();
        {
            // Simulate the kill: the previous session's start mark exists, its stop
            // mark never landed. (The store handle drops here, releasing the lock.)
            let (mut store, _) = SqliteStore::open(&path).unwrap();
            store
                .insert_events(&[Event {
                    ts_ms: 1_000,
                    device: DeviceId("flaky:0".into()),
                    kind: EventKind::RecordingStarted,
                    severity: Severity::Info,
                    confidence: Confidence::Fact,
                    title: "recording started — gpuviewer (previous session)".into(),
                    evidence: "test fixture".into(),
                }])
                .unwrap();
        }

        drop(flaky_engine_persisting(&path));

        let store = SqliteStore::open_readonly(&path).unwrap();
        let events = store.events_between(0, now_ms() + 60_000).unwrap();
        let new_start = events
            .iter()
            .filter(|e| e.kind == EventKind::RecordingStarted)
            .max_by_key(|e| e.ts_ms)
            .expect("the new session must write its own start mark");
        assert!(new_start.ts_ms > 1_000, "must be the NEW session's mark");
        assert_eq!(
            new_start.confidence,
            Confidence::Fact,
            "the missing stop mark is observed, not inferred"
        );
        assert!(
            new_start
                .title
                .contains("previous session ended without a stop mark"),
            "the title must narrate the unclean predecessor: {}",
            new_start.title
        );
        assert!(
            new_start.title.contains("crash, kill, or power loss"),
            "the title names the possible causes without asserting one: {}",
            new_start.title
        );
        assert!(
            new_start.evidence.contains("gap size is unknowable"),
            "the evidence must state the gap cannot be sized: {}",
            new_start.evidence
        );
        cleanup_db(&path);
    }

    /// Stream visibility: the start mark rides the FIRST tick's frame (that is how the
    /// `--json` stream, `--on-event`, and the story feed see it) and never repeats; a
    /// live-only session (no recorder) has no boundary marks at all — the marks describe
    /// the recording, and there is none.
    #[test]
    fn start_mark_rides_the_first_frame_only() {
        let path = scratch_db();
        let mut engine = flaky_engine(Some(path.clone()), |_| false);
        let TickOutcome::Frame(first) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert_eq!(
            first
                .events
                .iter()
                .filter(|e| e.kind == EventKind::RecordingStarted)
                .count(),
            1,
            "the start mark must ride the first frame"
        );
        let TickOutcome::Frame(second) = engine.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            second
                .events
                .iter()
                .all(|e| e.kind != EventKind::RecordingStarted),
            "the start mark must not repeat"
        );
        drop(engine);
        cleanup_db(&path);

        let mut live_only = flaky_engine(None, |_| false);
        let TickOutcome::Frame(frame) = live_only.tick_guarded() else {
            panic!("scripted to never fail");
        };
        assert!(
            frame.events.iter().all(|e| {
                e.kind != EventKind::RecordingStarted && e.kind != EventKind::RecordingStopped
            }),
            "a live-only session records nothing, so it must not claim a recording began"
        );
        assert!(
            live_only.finish().is_none(),
            "no recorder, no stop mark to write"
        );
    }

    /// The TUI quit path: `Collector::shutdown` stops the thread cooperatively, which
    /// drops the engine and writes the stop mark — a quit must read as a clean session
    /// end, never as a crash, to the next session.
    #[test]
    fn collector_shutdown_writes_the_stop_mark() {
        let path = scratch_db();
        let engine = flaky_engine(Some(path.clone()), |_| false);
        let mut collector = Collector::start(engine, Duration::from_millis(5), false);

        // Bounded wait for at least one folded frame, so the session resembles a real one.
        let mut ticked = false;
        for _ in 0..1000 {
            if collector.shared.lock().unwrap().tick_seq > 0 {
                ticked = true;
                break;
            }
            std::thread::sleep(Duration::from_millis(2));
        }
        assert!(ticked, "the collector must tick before the shutdown");

        collector.shutdown();

        // The mark lands when the engine drops. `shutdown` waits boundedly; poll the
        // store rather than assume the join won the race (a wedged thread writes the
        // mark whenever it finally exits).
        let mut found = false;
        for _ in 0..1000 {
            let store = SqliteStore::open_readonly(&path).unwrap();
            let events = store.events_between(0, now_ms() + 60_000).unwrap();
            if events.iter().any(|e| e.kind == EventKind::RecordingStopped) {
                found = true;
                break;
            }
            drop(store);
            std::thread::sleep(Duration::from_millis(5));
        }
        assert!(
            found,
            "a TUI-style quit must write the recording_stopped mark"
        );
        cleanup_db(&path);
    }

    // ---- engine-side data-source claim (the mock-fallback --db hole) ----

    /// The narrow no-GPU→mock-fallback case: the engine opens the store knowing its
    /// backends are mock, so a real-stamped --db is refused AT THE OPEN — zero writes,
    /// including the device-identity upserts that used to leak before main.rs's
    /// post-construction check could refuse.
    #[test]
    fn mock_fallback_engine_refuses_a_real_stamped_db_with_zero_writes() {
        let path = scratch_db();
        drop(SqliteStore::open_recording(&path, DataSource::Real).unwrap());

        let engine = Engine::with_backends(
            vec![Box::new(gpuviewer_core::mock::MockBackend::new())],
            EngineConfig {
                persist: true,
                db_path: Some(path.clone()),
                ..Default::default()
            },
        );
        assert!(engine.mock_in_use(), "precondition: the fallback shape");
        assert!(
            engine.db_path().is_none(),
            "the engine must not hold a recorder on a mismatched database"
        );
        drop(engine);

        let store = SqliteStore::open_readonly(&path).unwrap();
        assert_eq!(
            store.data_source().unwrap(),
            Some(DataSource::Real),
            "the stamp survives untouched"
        );
        assert!(
            store.devices().unwrap().is_empty(),
            "ZERO writes — not even device-identity rows may leak"
        );
        assert!(
            store
                .events_between(0, now_ms() + 60_000)
                .unwrap()
                .is_empty(),
            "no events either — including session marks"
        );
        cleanup_db(&path);
    }

    /// The engine stamps a fresh --db with what the backends ACTUALLY are, not what the
    /// flags said: mock backends claim mock, real (non-mock) backends claim real.
    #[test]
    fn engine_stamps_a_fresh_db_with_the_backends_actual_source() {
        let path = scratch_db();
        let engine = Engine::with_backends(
            vec![Box::new(gpuviewer_core::mock::MockBackend::new())],
            EngineConfig {
                persist: true,
                db_path: Some(path.clone()),
                ..Default::default()
            },
        );
        assert!(engine.db_path().is_some());
        drop(engine);
        let store = SqliteStore::open_readonly(&path).unwrap();
        assert_eq!(store.data_source().unwrap(), Some(DataSource::Mock));
        drop(store);
        cleanup_db(&path);

        let path = scratch_db();
        drop(flaky_engine(Some(path.clone()), |_| false));
        let store = SqliteStore::open_readonly(&path).unwrap();
        assert_eq!(
            store.data_source().unwrap(),
            Some(DataSource::Real),
            "a non-mock backend set claims real"
        );
        drop(store);
        cleanup_db(&path);
    }

    #[test]
    fn event_sink_rate_caps_spawns_per_minute() {
        let mut sink = EventSink::new("true".into());
        let ev = Event {
            ts_ms: 1,
            device: DeviceId("d".into()),
            kind: EventKind::ProcessExited,
            severity: Severity::Info,
            confidence: Confidence::Fact,
            title: "t".into(),
            evidence: "e".into(),
        };
        // Fire well past the cap; the window must never exceed SINK_MAX_PER_MIN. Each hook
        // is waited out before the next fire so the separate live-children cap never
        // engages here — this test is about the per-minute spawn cap alone.
        for _ in 0..(SINK_MAX_PER_MIN + 25) {
            sink.fire(&ev);
            sink.wait_all();
        }
        assert_eq!(
            sink.window.len(),
            SINK_MAX_PER_MIN,
            "spawns within the minute must be capped"
        );
        assert!(
            sink.warned,
            "exceeding the cap must set the one-time warning"
        );
    }

    /// A hook that never exits must not accumulate: the live-children cap holds new
    /// spawns at [`SINK_MAX_LIVE`] and warns, instead of growing one zombie per event.
    #[cfg(unix)]
    #[test]
    fn event_sink_caps_live_hooks_that_never_exit() {
        let mut sink = EventSink::new("sleep 30".into());
        let ev = Event {
            ts_ms: 1,
            device: DeviceId("d".into()),
            kind: EventKind::ProcessExited,
            severity: Severity::Info,
            confidence: Confidence::Fact,
            title: "t".into(),
            evidence: "e".into(),
        };
        for _ in 0..(SINK_MAX_LIVE + 10) {
            sink.fire(&ev);
        }
        assert_eq!(sink.live(), SINK_MAX_LIVE, "survivors are capped");
        assert!(sink.warned, "hitting the live cap warns");
        sink.kill_all();
    }

    /// Xid attribution, wording, and repeat folding — exercised through the injection
    /// seam (the kernel log itself is a hardware/privilege matter for the checklist).
    #[test]
    fn xid_records_become_attributed_facts_with_repeats_folded() {
        use gpuviewer_core::kmsg::parse_xid;
        let mut engine = flaky_engine(None, |_| false);
        // flaky_engine's device id is not a PCI address; re-point it at one.
        engine.devices[0].1 = DeviceId("0000:01:00.0".into());
        let x79 = parse_xid(
            "NVRM: Xid (PCI:0000:01:00): 79, pid=4521, name=python, GPU has fallen off the bus.",
        )
        .unwrap();
        let other = parse_xid("NVRM: Xid (PCI:0000:02:00): 13, Graphics Exception").unwrap();

        let ev = engine.inject_xid(vec![x79.clone(), other], 1_000);
        assert_eq!(ev.len(), 1, "an Xid for an unmonitored device is dropped");
        let e = &ev[0];
        assert_eq!(e.kind, EventKind::XidError);
        assert_eq!(e.confidence, Confidence::Fact);
        assert_eq!(e.severity, Severity::Critical, "Xid 79 is fatal");
        assert_eq!(
            e.title,
            "GPU0 driver reported Xid 79: GPU has fallen off the bus (python pid 4521)"
        );
        assert!(
            e.evidence.contains("NVRM: Xid (PCI:0000:01:00): 79"),
            "{}",
            e.evidence
        );

        // Two repeats inside the minute fold silently...
        assert!(engine
            .inject_xid(vec![x79.clone(), x79.clone()], 2_000)
            .is_empty());
        // ...and are counted into the next narration once the window passes.
        let ev = engine.inject_xid(vec![x79], 70_000);
        assert_eq!(ev.len(), 1);
        assert!(
            ev[0].evidence.contains("2 earlier repeats"),
            "{}",
            ev[0].evidence
        );
    }
}