binscout 0.9.2

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

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
use serde::{Deserialize, Serialize};
use xxhash_rust::xxh3::xxh3_64;

use crate::analysis::{Analysis, FunctionLayout, Xref};
use crate::disasm::Arch;
use crate::error::{Error, Result};
use crate::loader::BinaryFormat;
use crate::loader::strings::StringHit;

/// Schema version stored under `meta["version"]`.
///
/// Version 6 strengthens cache identity with SHA-256 plus size alongside
/// the content hash, so same bytes opened under different
/// raw options never share cached layouts and old snapshots re-analyze
/// rather than synthesizing clean diagnostics.
///
/// Version 7 adds the `func_seeds` table plus the `seeds_hash` snapshot
/// identity (see [`FuncSeed`]): user-defined function seeds persist
/// across opens and join analysis on every open.
///
/// Version 8 stores derived rows as compact binary: functions keep one
/// row per function (carrying its block layouts, no per-block keys) while
/// xrefs and strings pack many entries per small chunked blob row; key
/// twins, per-instruction JSON objects, and `format!`-built keys are gone.
/// Old sidecars stay readable as stale (the stale path never opens derived
/// tables), so they re-analyze instead of erroring.
///
/// Version 9 replaces the FNV-1a content hash with xxh3-64 (see
/// [`content_hash`]): v8 snapshots miss on the version gate and re-analyze,
/// so the hash change never replays stale layouts.
pub const SCHEMA_VERSION: u32 = 9;
/// Revision of the analysis engine covered by cached snapshots.
///
/// Re-exported from [`crate::analysis`] so cache identity has one source:
/// a stored revision mismatch re-analyzes instead of replaying layouts.
pub use crate::analysis::ANALYSIS_REVISION;
/// Cap on cached function rows (mirrors analysis [`MAX_FUNCTIONS`](crate::analysis::MAX_FUNCTIONS)).
pub const MAX_SNAPSHOT_FUNCTIONS: usize = 100_000;
/// Cap on cached block rows (blocks never exceed decoded instructions).
pub const MAX_SNAPSHOT_BLOCKS: usize = 1_000_000;
/// Max xrefs per `xrefs` blob row (fixed 17-byte entries): small enough
/// to pack like small rows while row visits stay in the dozens.
const XREFS_PER_ROW: usize = 512;
/// Max strings per `strings` blob row: small enough to pack like small
/// rows while row visits stay in the dozens.
const STRINGS_PER_ROW: usize = 128;
/// Cap on cached xref rows (1M per kind times three kinds).
pub const MAX_SNAPSHOT_XREFS: usize = 3_000_000;
/// Cap on cached string rows (10k per segment times 256 segments, rounded up).
pub const MAX_SNAPSHOT_STRINGS: usize = 3_000_000;
/// Cap on stored annotation rows (user-created; 10k is 100x typical use).
pub const MAX_SNAPSHOT_ANNOTATIONS: usize = 10_000;
/// Cap on stored user-seed rows (user-created; mirrors annotations).
pub const MAX_SNAPSHOT_SEEDS: usize = 10_000;
/// Cap on cached diagnostic rows (mirrors analysis [`MAX_DIAGNOSTICS`](crate::analysis::MAX_DIAGNOSTICS)).
pub const MAX_SNAPSHOT_DIAGNOSTICS: usize = 100_000;
/// Cap on cached inferred-edge rows (mirrors analysis [`MAX_INFERRED_EDGES`](crate::analysis::MAX_INFERRED_EDGES)).
pub const MAX_SNAPSHOT_INFERRED_EDGES: usize = 100_000;
/// Cap on one annotation name/comment in bytes.
///
/// 8 KiB holds pasted stack traces while keeping a single row trivial.
pub const MAX_ANNOTATION_BYTES: usize = 8192;

const META: TableDefinition<&str, String> = TableDefinition::new("meta");
const BINARIES: TableDefinition<u64, String> = TableDefinition::new("binaries");
const FUNCTIONS: TableDefinition<u64, Vec<u8>> = TableDefinition::new("functions");
const XREFS: TableDefinition<u64, Vec<u8>> = TableDefinition::new("xrefs");
const STRINGS: TableDefinition<u64, Vec<u8>> = TableDefinition::new("strings");
const ANNOTATIONS: TableDefinition<u64, String> = TableDefinition::new("annotations");
const FUNC_SEEDS: TableDefinition<u64, String> = TableDefinition::new("func_seeds");
const DIAGNOSTICS: TableDefinition<(u64, u8), ()> = TableDefinition::new("diagnostics");

/// Deterministic 64-bit xxh3 hash of the binary bytes.
///
/// Non-cryptographic: it only detects "binary changed, re-analyze", it is not
/// a tamper check. Deterministic across processes, unlike `DefaultHasher`.
/// Single pass at memory bandwidth (gigabytes per second), so opens hash
/// once up front instead of byte-at-a-time.
#[must_use]
pub fn content_hash(bytes: &[u8]) -> u64 {
    xxh3_64(bytes)
}

/// Sidecar DB path for a binary: `foo.exe` -> `foo.exe.rsdbg.redb`.
///
/// Appends `.rsdbg.redb` to the full file name so `foo.exe` and `foo.dll`
/// never share a sidecar. There is no fallback to the old colliding path.
#[must_use]
pub fn default_db_path(binary: &Path) -> PathBuf {
    let mut s = binary.as_os_str().to_owned();
    s.push(".rsdbg.redb");
    PathBuf::from(s)
}

/// Size of the project DB file in bytes.
///
/// Returns `0` when no sidecar exists yet (a project opened but never
/// saved has no file to measure). Used by the `stats` command so
/// operators see the storage cost of a cached snapshot.
///
/// # Errors
/// [`Error::Storage`] when the path exists but its metadata is unreadable.
pub fn db_file_size(path: &Path) -> Result<u64> {
    match std::fs::metadata(path) {
        Ok(m) => Ok(m.len()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(0),
        Err(e) => Err(Error::Storage(format!("stat {}: {e}", path.display()))),
    }
}

/// Reclaim free pages in the project DB file (`redb` compaction).
///
/// `save` rewrites every table, so repeated re-analysis can leave free
/// pages behind; compaction shrinks the file back down. Returns `true`
/// when compaction reclaimed space. There is no separate vacuum step:
/// `redb` has no `VACUUM` command, `compact` is the equivalent.
///
/// # Errors
/// [`Error::Storage`] when the file cannot be opened or compacted
/// (e.g. another process holds a live transaction on it).
pub fn compact_db(path: &Path) -> Result<bool> {
    let mut db = open_db(path)?;
    db.compact()
        .map_err(|e| Error::Storage(format!("compact {}: {e}", path.display())))
}

/// Canonical arch string used in [`BinaryMeta`] and [`ProjectInfo`].
#[must_use]
pub fn arch_name(arch: Arch) -> &'static str {
    match arch {
        Arch::X86_64 => "x86_64",
        Arch::X86_32 => "x86_32",
        Arch::Aarch64 => "aarch64",
        Arch::Arm32 => "arm32",
        Arch::Thumb => "thumb",
        Arch::Riscv32 => "riscv32",
        Arch::Riscv64 => "riscv64",
        Arch::Mips32 => "mips32",
        Arch::Ppc32 => "ppc32",
        Arch::Ppc64 => "ppc64",
    }
}

/// Inverse of [`arch_name`].
///
/// # Errors
/// [`Error::Storage`] on an unknown persisted value (corrupt/forged DB).
pub fn parse_arch(s: &str) -> Result<Arch> {
    match s {
        "x86_64" => Ok(Arch::X86_64),
        "x86_32" => Ok(Arch::X86_32),
        "aarch64" => Ok(Arch::Aarch64),
        "arm32" => Ok(Arch::Arm32),
        "thumb" => Ok(Arch::Thumb),
        "riscv32" => Ok(Arch::Riscv32),
        "riscv64" => Ok(Arch::Riscv64),
        "mips32" => Ok(Arch::Mips32),
        "ppc32" => Ok(Arch::Ppc32),
        "ppc64" => Ok(Arch::Ppc64),
        other => Err(Error::Storage(format!(
            "unknown arch in project db: {other}"
        ))),
    }
}

/// Canonical format string used in [`BinaryMeta`] and [`ProjectInfo`].
#[must_use]
pub fn format_name(format: BinaryFormat) -> &'static str {
    match format {
        BinaryFormat::Elf => "elf",
        BinaryFormat::Pe => "pe",
        BinaryFormat::MachO => "macho",
        BinaryFormat::Coff => "coff",
        BinaryFormat::Le => "le",
        BinaryFormat::Raw => "raw",
    }
}

/// Inverse of [`format_name`].
///
/// # Errors
/// [`Error::Storage`] on an unknown persisted value (corrupt/forged DB).
pub fn parse_format(s: &str) -> Result<BinaryFormat> {
    match s {
        "elf" => Ok(BinaryFormat::Elf),
        "pe" => Ok(BinaryFormat::Pe),
        "macho" => Ok(BinaryFormat::MachO),
        "coff" => Ok(BinaryFormat::Coff),
        "le" => Ok(BinaryFormat::Le),
        "raw" => Ok(BinaryFormat::Raw),
        other => Err(Error::Storage(format!(
            "unknown format in project db: {other}"
        ))),
    }
}

/// Effective loader configuration recorded in a snapshot.
///
/// `Auto` means the binary was opened without explicit raw options
/// (container parse with raw x86-64-at-0 fallback for unrecognized bytes).
/// `Explicit` records the caller-supplied raw mapping. `Fat` records a fat
/// Mach-O slice open by canonical slice arch name: different slices never
/// share snapshots, even over identical container bytes. Auto, explicit,
/// and fat never alias each other.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "lowercase")]
pub enum StoredMapping {
    /// Opened without explicit raw options.
    Auto,
    /// Opened with explicit raw `arch`/`base`/`entry`.
    Explicit {
        /// Canonical arch string (see [`arch_name`]).
        arch: String,
        /// Virtual address of the first byte.
        base: u64,
        /// Entry-point address, if supplied.
        entry: Option<u64>,
    },
    /// Opened with a fat Mach-O slice selection.
    Fat {
        /// Canonical slice arch name (see
        /// [`crate::loader::load_with_slice`]).
        arch: String,
    },
}

impl StoredMapping {
    /// Mapping identity for an open request (`None` = auto detection).
    #[must_use]
    pub fn for_request(raw: Option<crate::loader::RawOptions>) -> Self {
        match raw {
            None => Self::Auto,
            Some(opts) => Self::Explicit {
                arch: arch_name(opts.arch).to_string(),
                base: opts.base,
                entry: opts.entry,
            },
        }
    }

    /// Mapping identity for a fat Mach-O slice open.
    ///
    /// `arch` is the caller-supplied slice selector (the CLI `--fat-arch` /
    /// MCP `fat_arch` value the follow-up plumbing passes through
    /// unchanged); it canonicalizes through
    /// [`crate::loader::normalize_fat_selector`] so aliases share one
    /// snapshot (`arm64` ≡ `aarch64`, `x86-64` ≡ `x86_64`,
    /// case-insensitively). Unknown spellings lowercase as before; the
    /// loader rejects them at open, so they never persist a snapshot.
    #[must_use]
    pub fn for_fat_slice(arch: &str) -> Self {
        Self::Fat {
            arch: crate::loader::normalize_fat_selector(arch)
                .map(str::to_string)
                .unwrap_or_else(|| arch.to_lowercase()),
        }
    }

    /// One-line description for mapping-conflict errors.
    #[must_use]
    pub fn describe(&self) -> String {
        match self {
            Self::Auto => "auto".to_string(),
            Self::Explicit { arch, base, entry } => match entry {
                Some(e) => format!("explicit raw {arch} base {base:#x} entry {e:#x}"),
                None => format!("explicit raw {arch} base {base:#x}"),
            },
            Self::Fat { arch } => format!("fat slice {arch}"),
        }
    }
}

/// Identity of the explicit debug inputs a snapshot was taken over.
///
/// Content hash plus size per file: the same binary analyzed with different
/// (or absent) `--pdb` / `--debug-file` bytes never shares layouts. The all-
/// zero value means no explicit debug files (and decodes from pre-debug
/// snapshots via `serde(default)`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct DebugId {
    /// xxh3 hash of the `--pdb` bytes (0 when absent).
    pub pdb_hash: u64,
    /// `--pdb` file size in bytes (0 when absent).
    pub pdb_size: u64,
    /// xxh3 hash of the `--debug-file` bytes (0 when absent).
    pub dwarf_hash: u64,
    /// `--debug-file` size in bytes (0 when absent).
    pub dwarf_size: u64,
}

/// Mapping-conflict error: the sidecar holds `stored_count` live
/// annotation(s) under `stored`, but the current open requests `expected`.
///
/// Annotations are never remapped across address spaces; the caller must
/// open with a separate `--db PATH` for this mapping.
fn mapping_conflict(
    stored: &StoredMapping,
    stored_count: usize,
    expected: &StoredMapping,
) -> Error {
    Error::Storage(format!(
        "mapping mismatch: project db holds {stored_count} annotation(s) under {}, but current open requests {}; use a separate --db PATH for this mapping (annotations are never remapped)",
        stored.describe(),
        expected.describe(),
    ))
}

/// Legacy-ambiguous error: a pre-mapping snapshot holds annotations whose
/// address space cannot be established, so no mapping can safely reuse it.
fn legacy_ambiguous(stored_count: usize, version: u32) -> Error {
    Error::Storage(format!(
        "mapping mismatch: project db holds {stored_count} annotation(s) under an unknown legacy mapping (schema v{version}); use a separate --db PATH for this mapping (annotations are never remapped)"
    ))
}

/// What binary a snapshot was taken over; row 0 of the `binaries` table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BinaryMeta {
    /// Snapshot schema version.
    pub version: u32,
    /// Analyzed binary path.
    pub binary_path: String,
    /// Hash of the file contents.
    pub content_hash: u64,
    /// Hex SHA-256 of the file contents.
    ///
    /// Strengthens [`BinaryMeta::content_hash`] (xxh3-64) against
    /// collisions: cache identity compares both plus [`BinaryMeta::size`].
    /// Defaults to empty so pre-v6 snapshots without the field still
    /// decode; such snapshots never hit the cache (empty never equals a
    /// real digest) and re-analyze instead.
    #[serde(default)]
    pub content_sha256: String,
    /// File size in bytes.
    pub size: u64,
    /// Architecture string.
    pub arch: String,
    /// Format string.
    pub format: String,
    /// Entry-point address, if known.
    pub entry: Option<u64>,
    /// Whether the format is supported for analysis.
    ///
    /// Always true for persisted snapshots: only successfully opened
    /// (hence supported) formats are saved. Defaults to true so pre-L1
    /// snapshots without the field still decode.
    #[serde(default = "default_supported")]
    pub supported: bool,
    /// Why `supported` is false, if so.
    #[serde(default)]
    pub reason: Option<String>,
    /// Effective loader mapping the snapshot was taken over.
    ///
    /// `None` marks a pre-v3 snapshot whose mapping cannot be established;
    /// such snapshots are rebuilt, never trusted, and require a separate DB
    /// when they hold annotations.
    #[serde(default)]
    pub mapping: Option<StoredMapping>,
    /// Analysis revision the snapshot was taken over.
    ///
    /// `None` marks a pre-v3 snapshot; a mismatch re-analyzes while
    /// carrying same-mapping annotations forward.
    #[serde(default)]
    pub analysis_rev: Option<u32>,
    /// Hash of the active user-seed set the snapshot was taken over.
    ///
    /// Computed by [`seeds_hash`] over the non-excluded rows: a change
    /// (define/undefine) re-analyzes and rewrites the snapshot, so
    /// derived rows always match the stored seeds. Defaults to zero so
    /// pre-v7 snapshots without the field still decode; such snapshots
    /// never hit the cache (their schema version mismatches) and
    /// re-analyze instead.
    #[serde(default)]
    pub seeds_hash: u64,
    /// Whether discovery produced more diagnostics than the record cap.
    ///
    /// Defaults to false so pre-v4 snapshots without the field still
    /// decode; such snapshots never hit the cache (their schema version
    /// mismatches) and re-analyze instead.
    #[serde(default)]
    pub diagnostics_truncated: bool,
    /// Diagnostics omitted by the record cap (zero unless truncated).
    ///
    /// Defaults to zero for pre-v4 snapshots; see
    /// [`BinaryMeta::diagnostics_truncated`].
    #[serde(default)]
    pub diagnostics_omitted: usize,
    /// Whether indirect-jump resolution produced more inferred edges than
    /// the project cap.
    ///
    /// Defaults to false so pre-v5 snapshots without the field still
    /// decode; such snapshots never hit the cache (their schema version
    /// mismatches) and re-analyze instead.
    #[serde(default)]
    pub inferred_truncated: bool,
    /// Inferred edges omitted by the project cap (zero unless truncated).
    ///
    /// Defaults to zero for pre-v5 snapshots; see
    /// [`BinaryMeta::inferred_truncated`].
    #[serde(default)]
    pub inferred_omitted: usize,
    /// Debug inputs the snapshot was taken over.
    ///
    /// Defaults to empty for pre-debug snapshots; see [`DebugId`].
    #[serde(default)]
    pub debug: DebugId,
}

fn default_supported() -> bool {
    true
}

impl BinaryMeta {
    /// Equality for fast annotation writes: all content identity except host path.
    ///
    /// [`BinaryMeta::binary_path`] is display-only: opening the same file as
    /// `./a` versus `/abs/a` must take the single-row fast path, not force a
    /// full-snapshot fallback. Every other field (hash, SHA-256, size, arch,
    /// format, entry, mapping, revisions, seed identity, truncation flags)
    /// still gates the fast path.
    #[must_use]
    pub fn fast_write_eq(&self, other: &Self) -> bool {
        self.version == other.version
            && self.content_hash == other.content_hash
            && self.content_sha256 == other.content_sha256
            && self.size == other.size
            && self.arch == other.arch
            && self.format == other.format
            && self.entry == other.entry
            && self.supported == other.supported
            && self.reason == other.reason
            && self.mapping == other.mapping
            && self.analysis_rev == other.analysis_rev
            && self.diagnostics_truncated == other.diagnostics_truncated
            && self.diagnostics_omitted == other.diagnostics_omitted
            && self.inferred_truncated == other.inferred_truncated
            && self.inferred_omitted == other.inferred_omitted
            && self.seeds_hash == other.seeds_hash
            && self.debug == other.debug
    }
}

/// Whether stale user rows plausibly belong to the current binary.
///
/// True when the stored snapshot names the same host path, or when the
/// stored arch, format, and entry all match the freshly loaded binary.
/// Otherwise the sidecar was reused for an unrelated binary and its
/// renames/comments/seeds must be dropped rather than carried by address.
#[must_use]
pub fn stale_carry_plausible(
    stored: &BinaryMeta,
    current_path: &str,
    current_arch: &str,
    current_format: &str,
    current_entry: Option<u64>,
) -> bool {
    if stored.binary_path == current_path {
        return true;
    }
    stored.arch == current_arch && stored.format == current_format && stored.entry == current_entry
}

/// User annotation at one address: rename and/or free-text comment.
///
/// Stored only in the project DB; the analyzed binary is never modified.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Annotation {
    /// Function rename.
    pub name: Option<String>,
    /// Free-text comment.
    pub comment: Option<String>,
}

impl Annotation {
    /// Whether both fields are empty (row can be dropped).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.name.as_deref().is_none_or(str::is_empty)
            && self.comment.as_deref().is_none_or(str::is_empty)
    }
}

/// One user-defined function seed at one address.
///
/// `define-func` inserts (or re-activates) a row; `undefine-func` marks it
/// excluded. Excluded rows never seed analysis but are kept as data, so an
/// agent's "this isn't a function" verdict survives reloads and transfers.
/// Stored only in the project DB; the analyzed binary is never modified.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct FuncSeed {
    /// Decode-mode override (`None` = project arch).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<crate::analysis::SeedMode>,
    /// Whether the address is excluded from seeding (kept as data).
    #[serde(default)]
    pub excluded: bool,
}

/// Snapshot identity of the active user-seed set.
///
/// Deterministic FNV-1a over the sorted non-excluded rows (`addr` plus
/// mode); excluded rows are kept as data but never seed analysis, so they
/// stay out of the identity. Stored in [`BinaryMeta::seeds_hash`]:
/// derived snapshot rows always match the stored seeds because every
/// seed change re-analyzes before saving.
#[must_use]
pub fn seeds_hash(seeds: &BTreeMap<u64, FuncSeed>) -> u64 {
    const OFFSET: u64 = 0xcbf29ce484222325;
    const PRIME: u64 = 0x100000001b3;
    let mut h = OFFSET;
    for (&addr, seed) in seeds {
        if seed.excluded {
            continue;
        }
        for b in addr.to_le_bytes() {
            h ^= u64::from(b);
            h = h.wrapping_mul(PRIME);
        }
        let m = match seed.mode {
            None => 0,
            Some(crate::analysis::SeedMode::Arm) => 1,
            Some(crate::analysis::SeedMode::Thumb) => 2,
        };
        h ^= m;
        h = h.wrapping_mul(PRIME);
    }
    h
}

/// Agent-facing project summary; always JSON-serializable (M6 CLI/MCP).
///
/// JSON pairs `entry` with `entry_hex` (`null` on both sides when the
/// container names no entry), `base` with `base_hex` (`null` on both
/// sides in auto mode), and `content_hash` with `content_hash_hex`
/// (exact `0x`-hex; the number can lose precision in f64 JSON clients).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct ProjectInfo {
    /// Analyzed binary path.
    pub binary: String,
    /// Sidecar DB path.
    pub db_path: String,
    /// Crate version.
    pub version: String,
    /// Wire protocol revision (see `docs/book/src/json-protocol.md`).
    #[serde(default = "default_protocol")]
    pub protocol: u32,
    /// Effective loader mode: `"auto"`, `"raw"`, or `"fat"`.
    #[serde(default = "default_mode")]
    pub mode: String,
    /// Effective raw base address (`None` in auto mode).
    #[serde(default)]
    pub base: Option<u64>,
    /// Architecture string.
    pub arch: String,
    /// Format string.
    pub format: String,
    /// Entry-point address, if known.
    pub entry: Option<u64>,
    /// Hash of the file contents.
    pub content_hash: u64,
    /// File size in bytes.
    pub size: u64,
    /// Whether the format is supported for analysis.
    ///
    /// Always true for successfully opened projects: unsupported formats
    /// fail at open instead of returning a `supported:false` project.
    ///
    /// [`Error::Unsupported`]: crate::error::Error::Unsupported
    #[serde(default = "default_supported")]
    pub supported: bool,
    /// Why `supported` is false, if so.
    #[serde(default)]
    pub reason: Option<String>,
    /// Discovered functions.
    pub functions: usize,
    /// Basic blocks across all functions.
    pub blocks: usize,
    /// Decoded instructions.
    pub insns: usize,
    /// Recorded cross-references.
    pub xrefs: usize,
    /// Extracted strings.
    pub strings: usize,
    /// Stored annotations.
    pub annotations: usize,
    /// Retained diagnostic records (see [`crate::analysis::Diagnostics`]).
    ///
    /// Evidence about where analysis is incomplete, not a completeness
    /// proof: zero retained diagnostics never means complete recovery.
    #[serde(default)]
    pub diagnostics: usize,
    /// Whether discovery produced more diagnostics than the record cap.
    #[serde(default)]
    pub diagnostics_truncated: bool,
    /// Diagnostics omitted by the record cap (zero unless truncated).
    #[serde(default)]
    pub diagnostics_omitted: usize,
    /// Whether indirect-jump resolution produced more inferred edges than
    /// the project cap (see [`crate::analysis::Diagnostics`]).
    #[serde(default)]
    pub inferred_truncated: bool,
    /// Inferred edges omitted by the project cap (zero unless truncated).
    #[serde(default)]
    pub inferred_omitted: usize,
    /// Analysis revision the in-memory result was taken over (see
    /// [`crate::analysis::ANALYSIS_REVISION`]).
    #[serde(default)]
    pub analysis_revision: u32,
    /// Debug-info function rows attached at open (DWARF subprograms and
    /// PDB publics/procedures): zero without debug info.
    #[serde(default)]
    pub debug_funcs: usize,
}

fn default_protocol() -> u32 {
    crate::WIRE_PROTOCOL
}

fn default_mode() -> String {
    "auto".to_string()
}

impl Serialize for ProjectInfo {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = serializer.serialize_struct("ProjectInfo", 29)?;
        st.serialize_field("binary", &self.binary)?;
        st.serialize_field("db_path", &self.db_path)?;
        st.serialize_field("version", &self.version)?;
        st.serialize_field("protocol", &self.protocol)?;
        st.serialize_field("mode", &self.mode)?;
        st.serialize_field("base", &self.base)?;
        st.serialize_field("base_hex", &self.base.map(crate::addr_hex))?;
        st.serialize_field("arch", &self.arch)?;
        st.serialize_field("format", &self.format)?;
        st.serialize_field("entry", &self.entry)?;
        st.serialize_field("entry_hex", &self.entry.map(crate::addr_hex))?;
        st.serialize_field("content_hash", &self.content_hash)?;
        st.serialize_field("content_hash_hex", &crate::addr_hex(self.content_hash))?;
        st.serialize_field("size", &self.size)?;
        st.serialize_field("supported", &self.supported)?;
        st.serialize_field("reason", &self.reason)?;
        st.serialize_field("functions", &self.functions)?;
        st.serialize_field("blocks", &self.blocks)?;
        st.serialize_field("insns", &self.insns)?;
        st.serialize_field("xrefs", &self.xrefs)?;
        st.serialize_field("strings", &self.strings)?;
        st.serialize_field("annotations", &self.annotations)?;
        st.serialize_field("diagnostics", &self.diagnostics)?;
        st.serialize_field("diagnostics_truncated", &self.diagnostics_truncated)?;
        st.serialize_field("diagnostics_omitted", &self.diagnostics_omitted)?;
        st.serialize_field("inferred_truncated", &self.inferred_truncated)?;
        st.serialize_field("inferred_omitted", &self.inferred_omitted)?;
        st.serialize_field("analysis_revision", &self.analysis_revision)?;
        st.serialize_field("debug_funcs", &self.debug_funcs)?;
        st.end()
    }
}

/// Snapshot read back by [`load`]: metadata, cached layouts, annotations.
///
/// Instructions are not included; the caller rebuilds the [`Analysis`]
/// from the layouts plus the binary via
/// [`rebuild`](crate::analysis::rebuild).
#[derive(Debug)]
pub struct Loaded {
    /// Snapshot metadata (row 0).
    pub meta: BinaryMeta,
    /// Disassembler target the snapshot was taken as.
    pub arch: Arch,
    /// Cached function layouts (entry, name, callees, block ranges,
    /// reasons, decode-arch overrides).
    pub functions: Vec<FunctionLayout>,
    /// Cached callgraph edges as `(caller, callee)` pairs.
    pub call_edges: Vec<(u64, u64)>,
    /// Cached cross-references.
    pub xrefs: Vec<Xref>,
    /// Cached extracted strings.
    pub strings: Vec<StringHit>,
    /// Cached diagnostics sorted by `(addr, kind)`.
    pub diagnostics: Vec<crate::analysis::Diagnostic>,
    /// Annotations keyed by address.
    pub annotations: BTreeMap<u64, Annotation>,
    /// User-defined function seeds keyed by address (excluded rows kept).
    pub seeds: BTreeMap<u64, FuncSeed>,
}

impl Loaded {
    /// Re-decode snapshot instructions from `bin` into a full [`Analysis`].
    ///
    /// The snapshot holds block layouts only; this steps each range with the
    /// same decoder the analysis walker uses, so the result equals a fresh
    /// analysis of the same bytes — including identical diagnostics and
    /// discovery reasons. Stored annotations are not part of the
    /// returned value; read [`Loaded::annotations`] directly.
    ///
    /// # Errors
    /// [`Error::Storage`] on corrupt layouts (see
    /// [`rebuild`](crate::analysis::rebuild)), [`Error::Limit`] past
    /// analysis budgets.
    pub fn rebuild(self, bin: &crate::loader::LoadedBinary) -> Result<Analysis> {
        crate::analysis::rebuild(
            self.arch,
            bin,
            &self.functions,
            &self.call_edges,
            self.xrefs,
            self.strings,
            crate::analysis::Diagnostics {
                records: self.diagnostics,
                truncated: self.meta.diagnostics_truncated,
                omitted: self.meta.diagnostics_omitted,
                inferred_truncated: self.meta.inferred_truncated,
                inferred_omitted: self.meta.inferred_omitted,
            },
        )
    }
}

/// Open (creating if needed) the project DB at `path`.
///
/// Scope this to writers ([`save`], [`save_annotation`], [`compact_db`]):
/// read paths take [`open_db_read_only`] so a large sidecar costs no
/// write-side open work and reads never create the file.
///
/// # Errors
/// [`Error::Storage`] when the file cannot be created or opened.
pub fn open_db(path: &Path) -> Result<Database> {
    Database::create(path).map_err(|e| Error::Storage(format!("open db: {e}")))
}

/// Open the project DB at `path` for reads only.
///
/// Unlike [`open_db`] this never creates the file, never takes the write
/// lock, and skips write-side open work that scales with the file (debug
/// builds of `redb` walk every page on writable open, which made opening a
/// small binary against a large stale sidecar cost seconds and hundreds of
/// megabytes). All [`Project`](crate::Project) read opens use this; writers
/// open separately via [`open_db`].
///
/// # Errors
/// [`Error::Storage`] when the file does not exist or cannot be opened.
pub fn open_db_read_only(path: &Path) -> Result<redb::ReadOnlyDatabase> {
    redb::Database::builder()
        .open_read_only(path)
        .map_err(|e| Error::Storage(format!("open db: {e}")))
}

/// Read only the snapshot identity row of `db`.
///
/// Lets open paths check the cheap identity (version, content hash, size,
/// mapping, analysis revision) before paying for a SHA-256 over the input:
/// only a tentative match needs the wide hash to confirm the cache hit.
/// Accepts any readable handle, like [`load_cached`].
///
/// # Errors
/// [`Error::Storage`] on unreadable or undecodable metadata.
pub fn read_meta(db: &impl ReadableDatabase) -> Result<Option<BinaryMeta>> {
    let rtxn = db
        .begin_read()
        .map_err(|e| Error::Storage(format!("begin read: {e}")))?;
    read_meta_row(&rtxn)
}

fn json<T: Serialize>(v: &T) -> Result<String> {
    serde_json::to_string(v).map_err(|e| Error::Storage(format!("encode row: {e}")))
}

fn decode<T: for<'de> Deserialize<'de>>(s: &str) -> Result<T> {
    serde_json::from_str(s).map_err(|e| Error::Storage(format!("decode row: {e}")))
}

/// Little-endian binary writer for derived snapshot rows.
///
/// `functions` and `strings` chunked blobs plus `xrefs` chunked entries
/// store compact binary instead of JSON: per-instruction JSON objects,
/// `0x`-hex twins, and `format!`-built keys once made small binaries
/// produce ~20x sidecars. Counts are `u32` (row sizes stay far below 4 GiB:
/// mapped bytes cap at 256 MiB), addresses `u64`, sizes single bytes.
struct Writer {
    buf: Vec<u8>,
}

impl Writer {
    fn new() -> Self {
        Self { buf: Vec::new() }
    }

    fn u8(&mut self, v: u8) {
        self.buf.push(v);
    }

    fn u32(&mut self, v: u32) {
        self.buf.extend_from_slice(&v.to_le_bytes());
    }

    fn u64(&mut self, v: u64) {
        self.buf.extend_from_slice(&v.to_le_bytes());
    }

    /// Length-prefixed bytes (`u32` length, then the bytes).
    fn bytes(&mut self, v: &[u8]) {
        // Infallible: every stored blob derives from mapped bytes or their
        // names, all bounded by the 256 MiB loader cap.
        self.u32(v.len() as u32);
        self.buf.extend_from_slice(v);
    }

    fn str(&mut self, v: &str) {
        self.bytes(v.as_bytes());
    }

    /// Raw bytes, appended without a length prefix.
    ///
    /// Only for concatenating self-delimiting rows in chunked blobs: the
    /// reader consumes exactly one row per call and checks the chunk end
    /// afterwards.
    fn raw(&mut self, v: &[u8]) {
        self.buf.extend_from_slice(v);
    }

    /// Unsigned LEB128: block-start deltas ascend, so small gaps take one
    /// or two bytes instead of eight.
    fn uleb128(&mut self, mut v: u64) {
        loop {
            let b = (v & 0x7f) as u8;
            v >>= 7;
            if v == 0 {
                self.u8(b);
                break;
            }
            self.u8(b | 0x80);
        }
    }

    /// Flag byte (`0` = none, `1` = some) plus the length-prefixed string.
    fn opt_str(&mut self, v: Option<&str>) {
        match v {
            Some(s) => {
                self.u8(1);
                self.str(s);
            }
            None => self.u8(0),
        }
    }

    /// Count plus the `u64` items.
    fn u64_vec(&mut self, vs: &[u64]) {
        // Infallible: row item counts stay far below `u32::MAX` (snapshot
        // row caps bound every vector encoded here).
        self.u32(vs.len() as u32);
        for &v in vs {
            self.u64(v);
        }
    }

    /// Successor and inferred-successor lists of one block.
    ///
    /// Direct successors never exceed two per block (branch target plus
    /// fallthrough at most) and inferred never exceed fifteen, so both
    /// counts usually pack into one byte: the low two bits hold the direct
    /// count, the high six the inferred count. Longer lists (only reachable
    /// if analysis ever grows new edge shapes) take the `0xFF` escape hatch
    /// plus two `u32` counts.
    fn succ_lists(&mut self, succs: &[u64], inferred: &[u64]) {
        if succs.len() <= 3 && inferred.len() <= 15 {
            self.u8(succs.len() as u8 | ((inferred.len() as u8) << 2));
        } else {
            self.u8(0xFF);
            // Infallible: lengths stay far below `u32::MAX` (see above).
            self.u32(succs.len() as u32);
            self.u32(inferred.len() as u32);
        }
        for &v in succs.iter().chain(inferred) {
            self.u64(v);
        }
    }

    fn finish(self) -> Vec<u8> {
        self.buf
    }
}

/// Little-endian binary reader for derived snapshot rows.
///
/// Every failure reports `decode row`, the same marker the stale-cache
/// policy keys corrupt-row errors on.
struct Reader<'a> {
    buf: &'a [u8],
}

impl<'a> Reader<'a> {
    fn new(buf: &'a [u8]) -> Self {
        Self { buf }
    }

    fn u8(&mut self) -> Result<u8> {
        let (&b, rest) = self
            .buf
            .split_first()
            .ok_or_else(|| Error::Storage("decode row: truncated".to_string()))?;
        self.buf = rest;
        Ok(b)
    }

    fn u32(&mut self) -> Result<u32> {
        let (b, rest) = self
            .buf
            .split_at_checked(4)
            .ok_or_else(|| Error::Storage("decode row: truncated".to_string()))?;
        self.buf = rest;
        Ok(u32::from_le_bytes(b.try_into().expect("split 4 bytes")))
    }

    fn u64(&mut self) -> Result<u64> {
        let (b, rest) = self
            .buf
            .split_at_checked(8)
            .ok_or_else(|| Error::Storage("decode row: truncated".to_string()))?;
        self.buf = rest;
        Ok(u64::from_le_bytes(b.try_into().expect("split 8 bytes")))
    }

    /// A `u32` count of `width`-byte items, bounded by the bytes left so a
    /// forged count cannot pre-size a huge allocation.
    fn count(&mut self, width: usize) -> Result<usize> {
        let n = self.u32()? as usize;
        if n > self.buf.len() / width.max(1) {
            return Err(Error::Storage("decode row: count exceeds row".to_string()));
        }
        Ok(n)
    }

    fn bytes(&mut self) -> Result<&'a [u8]> {
        let n = self.count(1)?;
        let (b, rest) = self.buf.split_at(n);
        self.buf = rest;
        Ok(b)
    }

    fn str(&mut self) -> Result<&'a str> {
        let b = self.bytes()?;
        std::str::from_utf8(b).map_err(|_| Error::Storage("decode row: bad utf-8".to_string()))
    }

    fn opt_string(&mut self) -> Result<Option<String>> {
        match self.u8()? {
            0 => Ok(None),
            1 => Ok(Some(self.str()?.to_string())),
            _ => Err(Error::Storage("decode row: bad option flag".to_string())),
        }
    }

    /// Count plus the `u64` items.
    fn u64_vec(&mut self) -> Result<Vec<u64>> {
        let n = self.count(8)?;
        let mut out = Vec::with_capacity(n);
        for _ in 0..n {
            out.push(self.u64()?);
        }
        Ok(out)
    }

    /// Successor and inferred-successor lists of one block (see
    /// [`Writer::succ_lists`]).
    fn succ_lists(&mut self) -> Result<(Vec<u64>, Vec<u64>)> {
        let (n, m) = match self.u8()? {
            // Guarded below like [`Reader::count`]: a forged escape must
            // not pre-size a huge allocation.
            0xFF => (self.u32()? as usize, self.u32()? as usize),
            b => {
                let m = (b >> 2) as usize;
                if m > 15 {
                    return Err(Error::Storage(
                        "decode row: bad successor counts".to_string(),
                    ));
                }
                ((b & 0x03) as usize, m)
            }
        };
        let total = n
            .checked_add(m)
            .ok_or_else(|| Error::Storage("decode row: bad successor counts".to_string()))?;
        if total > self.buf.len() / 8 {
            return Err(Error::Storage("decode row: count exceeds row".to_string()));
        }
        let mut out = Vec::with_capacity(total);
        for _ in 0..total {
            out.push(self.u64()?);
        }
        let inferred = out.split_off(n.min(out.len()));
        Ok((out, inferred))
    }

    /// Flag byte (`0` = absent, `1` = present) plus the `u64` item.
    fn opt_u64(&mut self) -> Result<Option<u64>> {
        match self.u8()? {
            0 => Ok(None),
            1 => Ok(Some(self.u64()?)),
            _ => Err(Error::Storage("decode row: bad option flag".to_string())),
        }
    }

    /// Unsigned LEB128 (see [`Writer::uleb128`]).
    fn uleb128(&mut self) -> Result<u64> {
        let mut out = 0u64;
        for i in 0..10 {
            let b = self.u8()?;
            if i == 9 && b & 0x7e != 0 {
                return Err(Error::Storage("decode row: varint overflow".to_string()));
            }
            out |= ((b & 0x7f) as u64) << (7 * i);
            if b & 0x80 == 0 {
                return Ok(out);
            }
        }
        Err(Error::Storage("decode row: bad varint".to_string()))
    }

    fn end(&self) -> Result<()> {
        if self.buf.is_empty() {
            Ok(())
        } else {
            Err(Error::Storage("decode row: trailing bytes".to_string()))
        }
    }
}

/// Discriminant of an [`XrefKind`](crate::analysis::XrefKind) for xref keys.
///
/// Order matches the kind's `Ord` (code, data, string, import,
/// inferred-code) so key order equals the `(from, kind, to)` sort.
fn xref_kind_code(kind: crate::analysis::XrefKind) -> u8 {
    match kind {
        crate::analysis::XrefKind::Code => 0,
        crate::analysis::XrefKind::Data => 1,
        crate::analysis::XrefKind::String => 2,
        crate::analysis::XrefKind::Import => 3,
        crate::analysis::XrefKind::InferredCode => 4,
    }
}

/// Inverse of [`xref_kind_code`].
fn decode_xref_kind(v: u8) -> Result<crate::analysis::XrefKind> {
    match v {
        0 => Ok(crate::analysis::XrefKind::Code),
        1 => Ok(crate::analysis::XrefKind::Data),
        2 => Ok(crate::analysis::XrefKind::String),
        3 => Ok(crate::analysis::XrefKind::Import),
        4 => Ok(crate::analysis::XrefKind::InferredCode),
        _ => Err(Error::Storage(format!("decode row: bad xref kind {v}"))),
    }
}

/// Discriminant of a [`DiscoveryReason`](crate::analysis::DiscoveryReason).
fn reason_code(reason: crate::analysis::DiscoveryReason) -> u8 {
    match reason {
        crate::analysis::DiscoveryReason::Entry => 0,
        crate::analysis::DiscoveryReason::Export => 1,
        crate::analysis::DiscoveryReason::Symbol => 2,
        crate::analysis::DiscoveryReason::DirectCall => 3,
        crate::analysis::DiscoveryReason::Prologue => 4,
        crate::analysis::DiscoveryReason::User => 5,
        crate::analysis::DiscoveryReason::Pdata => 6,
        crate::analysis::DiscoveryReason::TlsCallback => 7,
        crate::analysis::DiscoveryReason::InitArray => 8,
        crate::analysis::DiscoveryReason::FunctionStarts => 9,
        crate::analysis::DiscoveryReason::Pointer => 10,
        crate::analysis::DiscoveryReason::Debug => 11,
    }
}

/// Inverse of [`reason_code`].
fn decode_reason(v: u8) -> Result<crate::analysis::DiscoveryReason> {
    match v {
        0 => Ok(crate::analysis::DiscoveryReason::Entry),
        1 => Ok(crate::analysis::DiscoveryReason::Export),
        2 => Ok(crate::analysis::DiscoveryReason::Symbol),
        3 => Ok(crate::analysis::DiscoveryReason::DirectCall),
        4 => Ok(crate::analysis::DiscoveryReason::Prologue),
        5 => Ok(crate::analysis::DiscoveryReason::User),
        6 => Ok(crate::analysis::DiscoveryReason::Pdata),
        7 => Ok(crate::analysis::DiscoveryReason::TlsCallback),
        8 => Ok(crate::analysis::DiscoveryReason::InitArray),
        9 => Ok(crate::analysis::DiscoveryReason::FunctionStarts),
        10 => Ok(crate::analysis::DiscoveryReason::Pointer),
        11 => Ok(crate::analysis::DiscoveryReason::Debug),
        _ => Err(Error::Storage(format!("decode row: bad reason {v}"))),
    }
}

/// Discriminant of a [`DiagnosticKind`](crate::analysis::DiagnosticKind)
/// for diagnostics keys; order matches its `Ord` so key order equals the
/// `(addr, kind)` sort.
fn diagnostic_kind_code(kind: crate::analysis::DiagnosticKind) -> u8 {
    match kind {
        crate::analysis::DiagnosticKind::UnresolvedIndirectJump => 0,
        crate::analysis::DiagnosticKind::UnresolvedIndirectCall => 1,
        crate::analysis::DiagnosticKind::DecodeFailure => 2,
        crate::analysis::DiagnosticKind::NoreturnCut => 3,
    }
}

/// Inverse of [`diagnostic_kind_code`].
fn decode_diagnostic_kind(v: u8) -> Result<crate::analysis::DiagnosticKind> {
    match v {
        0 => Ok(crate::analysis::DiagnosticKind::UnresolvedIndirectJump),
        1 => Ok(crate::analysis::DiagnosticKind::UnresolvedIndirectCall),
        2 => Ok(crate::analysis::DiagnosticKind::DecodeFailure),
        3 => Ok(crate::analysis::DiagnosticKind::NoreturnCut),
        _ => Err(Error::Storage(format!(
            "decode row: bad diagnostic kind {v}"
        ))),
    }
}

/// Discriminant of a [`Encoding`](crate::loader::strings::Encoding).
fn encoding_code(enc: crate::loader::strings::Encoding) -> u8 {
    match enc {
        crate::loader::strings::Encoding::Ascii => 0,
        crate::loader::strings::Encoding::Utf16Le => 1,
        crate::loader::strings::Encoding::Utf16Be => 2,
        crate::loader::strings::Encoding::Utf8 => 3,
        crate::loader::strings::Encoding::Utf16LeBmp => 4,
        crate::loader::strings::Encoding::Base64 => 5,
    }
}

/// Inverse of [`encoding_code`].
fn decode_encoding(v: u8) -> Result<crate::loader::strings::Encoding> {
    match v {
        0 => Ok(crate::loader::strings::Encoding::Ascii),
        1 => Ok(crate::loader::strings::Encoding::Utf16Le),
        2 => Ok(crate::loader::strings::Encoding::Utf16Be),
        3 => Ok(crate::loader::strings::Encoding::Utf8),
        4 => Ok(crate::loader::strings::Encoding::Utf16LeBmp),
        5 => Ok(crate::loader::strings::Encoding::Base64),
        _ => Err(Error::Storage(format!("decode row: bad encoding {v}"))),
    }
}

/// Encode one function row: name, sorted callees, reasons, decode-arch
/// override, and its block layouts in start order. The entry rides in the
/// table key; blocks ride in the value so one row holds the whole function
/// and no per-block key repeats the entry. The first block start is
/// absolute, the rest are unsigned LEB128 deltas from the previous start
/// (starts ascend, so deltas never go negative and small gaps take one or
/// two bytes). Lengths are `u32`: blocks never exceed the 256 MiB loader
/// cap.
fn encode_function(f: &crate::analysis::FunctionLayout) -> Vec<u8> {
    let mut w = Writer::new();
    w.opt_str(f.name.as_deref());
    w.u64_vec(&f.calls);
    // Infallible: reasons stay in the single digits.
    w.u32(f.reasons.len() as u32);
    for &r in &f.reasons {
        w.u8(reason_code(r));
    }
    w.opt_str(f.arch.as_deref());
    let mut blocks: Vec<&crate::analysis::BlockLayout> = f.blocks.iter().collect();
    blocks.sort_by_key(|b| b.start);
    // Infallible: block counts stay far below `u32::MAX` (snapshot row caps
    // bound every vector encoded here).
    w.u32(blocks.len() as u32);
    let mut prev = 0u64;
    let mut first = true;
    for b in blocks {
        if first {
            w.u64(b.start);
            first = false;
        } else {
            w.uleb128(b.start.wrapping_sub(prev));
        }
        prev = b.start;
        // Infallible: block lengths stay far below `u32::MAX` (blocks lie
        // within mapped bytes, capped at 256 MiB).
        w.u32(b.end.wrapping_sub(b.start) as u32);
        // Infallible: blocks hold far fewer than `u32::MAX` instructions.
        w.u32(b.insns.len() as u32);
        for i in &b.insns {
            w.u8(i.size);
        }
        let mut addr = b.start;
        let mut contiguous = true;
        for i in &b.insns {
            if i.addr != addr {
                contiguous = false;
            }
            addr = addr.wrapping_add(u64::from(i.size));
        }
        if contiguous {
            w.u8(0);
        } else {
            w.u8(1);
            for i in &b.insns {
                w.u64(i.addr);
            }
        }
        w.succ_lists(&b.succs, &b.inferred_succs);
    }
    w.finish()
}

/// Decode one function row; `entry` rides in the table key.
fn decode_function(entry: u64, raw: &[u8]) -> Result<(crate::analysis::FunctionLayout, usize)> {
    let mut r = Reader::new(raw);
    let name = r.opt_string()?;
    let calls = r.u64_vec()?;
    let n_reasons = r.count(1)?;
    let mut reasons = Vec::with_capacity(n_reasons);
    for _ in 0..n_reasons {
        reasons.push(decode_reason(r.u8()?)?);
    }
    let arch = r.opt_string()?;
    let n_blocks = r.count(8)?;
    let mut blocks = Vec::with_capacity(n_blocks.min(1024));
    let mut prev = 0u64;
    let mut first = true;
    for _ in 0..n_blocks {
        let start = if first {
            first = false;
            r.u64()?
        } else {
            prev.wrapping_add(r.uleb128()?)
        };
        prev = start;
        let len = r.u32()?;
        let end = start.wrapping_add(u64::from(len));
        let n = r.count(1)?;
        let mut sizes = Vec::with_capacity(n.min(1024));
        for _ in 0..n {
            sizes.push(r.u8()?);
        }
        let mut addrs: Vec<u64> = Vec::with_capacity(n.min(1024));
        match r.u8()? {
            0 => {
                let mut addr = start;
                for &size in &sizes {
                    addrs.push(addr);
                    addr = addr.wrapping_add(u64::from(size));
                }
            }
            1 => {
                for _ in 0..n {
                    addrs.push(r.u64()?);
                }
            }
            f => {
                return Err(Error::Storage(format!(
                    "decode row: bad block address flag {f}"
                )));
            }
        }
        let (succs, inferred_succs) = r.succ_lists()?;
        let insns = sizes
            .into_iter()
            .zip(addrs)
            .map(|(size, addr)| crate::analysis::InsnLayout { addr, size })
            .collect();
        blocks.push(crate::analysis::BlockLayout {
            start,
            end,
            insns,
            succs,
            inferred_succs,
        });
    }
    let layout = crate::analysis::FunctionLayout {
        entry,
        name,
        calls,
        blocks,
        reasons,
        arch,
    };
    let n = layout.blocks.len();
    r.end()?;
    Ok((layout, n))
}

/// Encode one string row without the wire `vaddr_hex` twin.
fn encode_string(h: &StringHit) -> Vec<u8> {
    let mut w = Writer::new();
    // Infallible: offsets index mapped bytes, bounded by the 256 MiB loader
    // cap, so they always fit `u32`.
    w.u32(h.offset as u32);
    match h.vaddr {
        Some(v) => {
            w.u8(1);
            w.u64(v);
        }
        None => w.u8(0),
    }
    w.u8(encoding_code(h.encoding));
    w.str(&h.value);
    w.opt_str(h.section.as_deref());
    w.u8(u8::from(h.executable));
    w.finish()
}

/// Decode one string body from the shared chunk reader. Consumes exactly
/// one body: the caller checks the chunk end afterwards.
fn decode_string(r: &mut Reader<'_>) -> Result<StringHit> {
    let offset = r.u32()? as usize;
    let vaddr = r.opt_u64()?;
    let encoding = decode_encoding(r.u8()?)?;
    let value = r.str()?.to_string();
    let section = r.opt_string()?;
    let executable = match r.u8()? {
        0 => false,
        1 => true,
        f => {
            return Err(Error::Storage(format!(
                "decode row: bad executable flag {f}"
            )));
        }
    };
    Ok(StringHit {
        offset,
        vaddr,
        encoding,
        value,
        section,
        executable,
    })
}

/// Reject one annotation whose name/comment exceeds the per-field budget.
///
/// 8 KiB bounds a single row before it ever reaches the sidecar.
fn check_annotation_text(ann: &Annotation) -> Result<()> {
    for (label, s) in [("name", &ann.name), ("comment", &ann.comment)] {
        if let Some(t) = s
            && t.len() > MAX_ANNOTATION_BYTES
        {
            return Err(Error::Limit(format!(
                "annotation {label} of {} bytes exceeds {MAX_ANNOTATION_BYTES}",
                t.len()
            )));
        }
    }
    Ok(())
}

/// Pre-write row-count budget check so [`save`] never writes a snapshot
/// that [`load`] would reject. Counts mirror the analysis caps plus the
/// annotation and seed caps.
fn check_analysis_budgets(
    analysis: &Analysis,
    annotations: &BTreeMap<u64, Annotation>,
    seeds: &BTreeMap<u64, FuncSeed>,
) -> Result<()> {
    if analysis.functions.len() > MAX_SNAPSHOT_FUNCTIONS {
        return Err(Error::Limit("snapshot functions exceed cap".to_string()));
    }
    let blocks: usize = analysis.functions.values().map(|f| f.blocks.len()).sum();
    if blocks > MAX_SNAPSHOT_BLOCKS {
        return Err(Error::Limit("snapshot blocks exceed cap".to_string()));
    }
    if analysis.xrefs.len() > MAX_SNAPSHOT_XREFS {
        return Err(Error::Limit("snapshot xrefs exceed cap".to_string()));
    }
    if analysis.strings.len() > MAX_SNAPSHOT_STRINGS {
        return Err(Error::Limit("snapshot strings exceed cap".to_string()));
    }
    if analysis.diagnostics.records.len() > MAX_SNAPSHOT_DIAGNOSTICS {
        return Err(Error::Limit("snapshot diagnostics exceed cap".to_string()));
    }
    let inferred: usize = analysis.functions.values().map(|f| f.inferred_len()).sum();
    if inferred > MAX_SNAPSHOT_INFERRED_EDGES {
        return Err(Error::Limit(
            "snapshot inferred edges exceed cap".to_string(),
        ));
    }
    let live = annotations.values().filter(|a| !a.is_empty()).count();
    if live > MAX_SNAPSHOT_ANNOTATIONS {
        return Err(Error::Limit("snapshot annotations exceed cap".to_string()));
    }
    if seeds.len() > MAX_SNAPSHOT_SEEDS {
        return Err(Error::Limit("snapshot func seeds exceed cap".to_string()));
    }
    for ann in annotations.values() {
        if !ann.is_empty() {
            check_annotation_text(ann)?;
        }
    }
    Ok(())
}

fn delete_if_exists(wtxn: &redb::WriteTransaction, table: &str) -> Result<()> {
    let deleted = match table {
        "meta" => wtxn.delete_table(META),
        "binaries" => wtxn.delete_table(BINARIES),
        "functions" => wtxn.delete_table(FUNCTIONS),
        "xrefs" => wtxn.delete_table(XREFS),
        "strings" => wtxn.delete_table(STRINGS),
        "annotations" => wtxn.delete_table(ANNOTATIONS),
        "func_seeds" => wtxn.delete_table(FUNC_SEEDS),
        "diagnostics" => wtxn.delete_table(DIAGNOSTICS),
        _ => return Err(Error::Storage(format!("unknown table {table}"))),
    };
    match deleted {
        Ok(_) => Ok(()),
        Err(redb::TableError::TableDoesNotExist(_)) => Ok(()),
        Err(e) => Err(Error::Storage(format!("clear {table}: {e}"))),
    }
}

/// Count live user rows (non-empty annotations plus all seed rows) inside
/// a write transaction.
///
/// Used by writers to decide whether overwriting a mapping-incompatible
/// snapshot would drop user data. Corrupt rows error rather than reading
/// as empty.
fn live_user_rows_in_wtxn(wtxn: &redb::WriteTransaction) -> Result<usize> {
    let t = wtxn
        .open_table(ANNOTATIONS)
        .map_err(|e| Error::Storage(format!("open annotations: {e}")))?;
    let mut live = 0usize;
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read annotations: {e}")))?
    {
        let (_, v) = row.map_err(|e| Error::Storage(format!("read annotations: {e}")))?;
        let ann: Annotation = decode(&v.value())?;
        check_annotation_text(&ann)?;
        if !ann.is_empty() {
            live += 1;
        }
    }
    let t = wtxn
        .open_table(FUNC_SEEDS)
        .map_err(|e| Error::Storage(format!("open func_seeds: {e}")))?;
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?
    {
        let (_, v) = row.map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?;
        let _: FuncSeed = decode(&v.value())?;
        live += 1;
    }
    Ok(live)
}

/// Outcome of [`save_annotation`]: whether the single-row fast path applied.
///
/// [`AnnotationSave::Fast`] means only the `annotations` table changed;
/// [`AnnotationSave::NeedsFull`] means there is no matching snapshot and the
/// caller must fall back to [`save`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnnotationSave {
    /// Single annotation row inserted/deleted; analysis tables untouched.
    Fast,
    /// No matching snapshot (fresh DB or metadata mismatch); needs full save.
    NeedsFull,
}

/// Persist one annotation row when the durable snapshot matches `meta`.
///
/// Reads the persisted `meta`/`binaries` rows inside the same write
/// transaction that would modify `annotations`, so an annotation never lands
/// on a snapshot written for different bytes by another process. On match,
/// inserts (`Some` non-empty) or deletes (`None`/empty) only that row and
/// commits; the `functions`/`xrefs`/`strings` tables are never
/// opened for writing. Identity ignores [`BinaryMeta::binary_path`]
/// (see [`BinaryMeta::fast_write_eq`]): opening the same file via
/// different relative/absolute spellings still takes the fast path.
/// On a fresh DB or any content/revision mismatch under
/// the same mapping, aborts without writing and returns
/// [`AnnotationSave::NeedsFull`]. A populated sidecar under an incompatible
/// mapping (or an ambiguous legacy mapping) errors instead of returning
/// `NeedsFull`, so a single-row write never migrates annotations across
/// address spaces. Never touches the analyzed binary.
///
/// # Errors
/// [`Error::Limit`] when the annotation text exceeds
/// [`MAX_ANNOTATION_BYTES`], [`Error::Storage`] on any `redb` or encoding
/// failure (including write contention with another process) and on
/// mapping conflicts holding live annotations.
pub fn save_annotation(
    db: &Database,
    meta: &BinaryMeta,
    addr: u64,
    ann: Option<&Annotation>,
) -> Result<AnnotationSave> {
    if let Some(a) = ann
        && !a.is_empty()
    {
        check_annotation_text(a)?;
    }
    let encoded: Option<String> = match ann {
        Some(a) if !a.is_empty() => Some(json(a)?),
        _ => None,
    };
    let wtxn = db
        .begin_write()
        .map_err(|e| Error::Storage(format!("begin write: {e}")))?;
    {
        let meta_table = wtxn
            .open_table(META)
            .map_err(|e| Error::Storage(format!("open meta: {e}")))?;
        let version = meta_table
            .get("version")
            .map_err(|e| Error::Storage(format!("read meta: {e}")))?;
        if version.is_none() {
            return Ok(AnnotationSave::NeedsFull);
        }
    }
    {
        let binaries = wtxn
            .open_table(BINARIES)
            .map_err(|e| Error::Storage(format!("open binaries: {e}")))?;
        let row = binaries
            .get(0u64)
            .map_err(|e| Error::Storage(format!("read binaries: {e}")))?;
        let Some(row) = row else {
            return Ok(AnnotationSave::NeedsFull);
        };
        let persisted: BinaryMeta = decode(&row.value())?;
        if persisted.mapping != meta.mapping {
            let live = live_user_rows_in_wtxn(&wtxn)?;
            if live > 0 {
                let expected = meta.mapping.clone().unwrap_or(StoredMapping::Auto);
                match persisted.mapping {
                    Some(stored) => return Err(mapping_conflict(&stored, live, &expected)),
                    None => return Err(legacy_ambiguous(live, persisted.version)),
                }
            }
            return Ok(AnnotationSave::NeedsFull);
        }
        if !persisted.fast_write_eq(meta) {
            return Ok(AnnotationSave::NeedsFull);
        }
    }
    {
        let mut t = wtxn
            .open_table(ANNOTATIONS)
            .map_err(|e| Error::Storage(format!("open annotations: {e}")))?;
        match encoded {
            Some(s) => {
                t.insert(addr, s)
                    .map_err(|e| Error::Storage(format!("write annotations: {e}")))?;
            }
            None => {
                t.remove(addr)
                    .map_err(|e| Error::Storage(format!("remove annotations: {e}")))?;
            }
        }
    }
    wtxn.commit()
        .map_err(|e| Error::Storage(format!("commit: {e}")))?;
    Ok(AnnotationSave::Fast)
}

/// Persist validated annotation rows in one transaction, for imports.
///
/// Same guards as [`save_annotation`], applied once for the whole batch:
/// reads the persisted `meta`/`binaries` rows inside the write transaction
/// that applies every change, so rows never land on a snapshot written for
/// different bytes by another process. Identity ignores
/// [`BinaryMeta::binary_path`] (see [`BinaryMeta::fast_write_eq`]).
/// On match, upserts (`Some` non-empty)
/// or deletes (`None`) every entry of `changes` and commits once; the
/// `functions`/`xrefs`/`strings` tables are never opened for
/// writing. An empty `changes` map commits nothing and returns
/// [`AnnotationSave::Fast`]. On a fresh DB or any content/revision mismatch
/// under the same mapping, aborts without writing and returns
/// [`AnnotationSave::NeedsFull`]. A populated sidecar under an incompatible
/// mapping (or an ambiguous legacy mapping) errors instead of returning
/// `NeedsFull`, so a batch write never migrates annotations across address
/// spaces. Validate the document fully before calling: this commits
/// everything or nothing at the storage layer, but semantic validation is
/// the caller's job. Never touches the analyzed binary.
///
/// # Errors
/// [`Error::Limit`] when an annotation text exceeds
/// [`MAX_ANNOTATION_BYTES`], [`Error::Storage`] on any `redb` or encoding
/// failure (including write contention with another process) and on
/// mapping conflicts holding live annotations.
pub fn save_annotation_batch(
    db: &Database,
    meta: &BinaryMeta,
    changes: &BTreeMap<u64, Option<Annotation>>,
) -> Result<AnnotationSave> {
    for ann in changes.values().flatten() {
        check_annotation_text(ann)?;
    }
    if changes.is_empty() {
        return Ok(AnnotationSave::Fast);
    }
    let wtxn = db
        .begin_write()
        .map_err(|e| Error::Storage(format!("begin write: {e}")))?;
    {
        let meta_table = wtxn
            .open_table(META)
            .map_err(|e| Error::Storage(format!("open meta: {e}")))?;
        let version = meta_table
            .get("version")
            .map_err(|e| Error::Storage(format!("read meta: {e}")))?;
        if version.is_none() {
            return Ok(AnnotationSave::NeedsFull);
        }
    }
    {
        let binaries = wtxn
            .open_table(BINARIES)
            .map_err(|e| Error::Storage(format!("open binaries: {e}")))?;
        let row = binaries
            .get(0u64)
            .map_err(|e| Error::Storage(format!("read binaries: {e}")))?;
        let Some(row) = row else {
            return Ok(AnnotationSave::NeedsFull);
        };
        let persisted: BinaryMeta = decode(&row.value())?;
        if persisted.mapping != meta.mapping {
            let live = live_user_rows_in_wtxn(&wtxn)?;
            if live > 0 {
                let expected = meta.mapping.clone().unwrap_or(StoredMapping::Auto);
                match persisted.mapping {
                    Some(stored) => return Err(mapping_conflict(&stored, live, &expected)),
                    None => return Err(legacy_ambiguous(live, persisted.version)),
                }
            }
            return Ok(AnnotationSave::NeedsFull);
        }
        if !persisted.fast_write_eq(meta) {
            return Ok(AnnotationSave::NeedsFull);
        }
    }
    {
        let mut t = wtxn
            .open_table(ANNOTATIONS)
            .map_err(|e| Error::Storage(format!("open annotations: {e}")))?;
        for (&addr, next) in changes {
            match next {
                Some(ann) => {
                    t.insert(addr, json(ann)?)
                        .map_err(|e| Error::Storage(format!("write annotations: {e}")))?;
                }
                None => {
                    t.remove(addr)
                        .map_err(|e| Error::Storage(format!("remove annotations: {e}")))?;
                }
            }
        }
    }
    wtxn.commit()
        .map_err(|e| Error::Storage(format!("commit: {e}")))?;
    Ok(AnnotationSave::Fast)
}

/// Refuse to overwrite a populated snapshot under an incompatible mapping.
///
/// Reads the existing `binaries` row plus live user rows inside the same
/// write transaction the caller will commit, so a concurrent mapping change
/// cannot slip past the check. A fresh DB (no version marker or binary row)
/// never conflicts; an incompatible or ambiguous legacy mapping with live
/// annotations or seeds errors, while an empty sidecar overwrites freely.
fn reject_mapping_overwrite(wtxn: &redb::WriteTransaction, meta: &BinaryMeta) -> Result<()> {
    let version: Option<String> = {
        let t = wtxn
            .open_table(META)
            .map_err(|e| Error::Storage(format!("open meta: {e}")))?;
        t.get("version")
            .map_err(|e| Error::Storage(format!("read meta: {e}")))?
            .map(|v| v.value())
    };
    let Some(_) = version else {
        return Ok(());
    };
    let persisted: Option<BinaryMeta> = {
        let t = wtxn
            .open_table(BINARIES)
            .map_err(|e| Error::Storage(format!("open binaries: {e}")))?;
        match t
            .get(0u64)
            .map_err(|e| Error::Storage(format!("read binaries: {e}")))?
        {
            Some(row) => Some(decode(&row.value())?),
            None => None,
        }
    };
    let Some(persisted) = persisted else {
        return Ok(());
    };
    if persisted.mapping == meta.mapping {
        return Ok(());
    }
    let live = live_user_rows_in_wtxn(wtxn)?;
    if live == 0 {
        return Ok(());
    }
    let expected = meta.mapping.clone().unwrap_or(StoredMapping::Auto);
    match persisted.mapping {
        Some(stored) => Err(mapping_conflict(&stored, live, &expected)),
        None => Err(legacy_ambiguous(live, persisted.version)),
    }
}

/// Persist `meta`, `analysis`, `annotations`, and `seeds` to `db` in one transaction.
///
/// Clears all eight tables first so a smaller re-analysis leaves no stale
/// rows. Row counts are checked against the snapshot budgets before
/// committing, so a snapshot that [`load`] could not read back is never
/// written. Only block layouts persist (inside their function chunk);
/// instructions re-decode on open.
/// Never touches the analyzed binary. Overwriting a populated sidecar under
/// an incompatible (or ambiguous legacy) mapping errors instead of dropping
/// user rows; use a separate DB path for the new mapping.
///
/// # Errors
/// [`Error::Limit`] when row budgets are exceeded,
/// [`Error::Storage`] on any `redb` or encoding failure, and on mapping
/// conflicts holding live user rows.
pub fn save(
    db: &Database,
    meta: &BinaryMeta,
    analysis: &Analysis,
    annotations: &BTreeMap<u64, Annotation>,
    seeds: &BTreeMap<u64, FuncSeed>,
) -> Result<()> {
    check_analysis_budgets(analysis, annotations, seeds)?;
    let wtxn = db
        .begin_write()
        .map_err(|e| Error::Storage(format!("begin write: {e}")))?;
    reject_mapping_overwrite(&wtxn, meta)?;
    for t in [
        "meta",
        "binaries",
        "functions",
        "xrefs",
        "strings",
        "annotations",
        "func_seeds",
        "diagnostics",
    ] {
        delete_if_exists(&wtxn, t)?;
    }
    {
        let mut t = wtxn
            .open_table(META)
            .map_err(|e| Error::Storage(format!("open meta: {e}")))?;
        t.insert("version", SCHEMA_VERSION.to_string())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("binary_path", meta.binary_path.clone())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("content_hash", meta.content_hash.to_string())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("content_sha256", meta.content_sha256.clone())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("size", meta.size.to_string())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("arch", meta.arch.clone())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert("format", meta.format.clone())
            .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
        t.insert(
            "entry",
            meta.entry.map(|e| e.to_string()).unwrap_or_default(),
        )
        .map_err(|e| Error::Storage(format!("write meta: {e}")))?;
    }
    {
        let mut t = wtxn
            .open_table(BINARIES)
            .map_err(|e| Error::Storage(format!("open binaries: {e}")))?;
        t.insert(0u64, json(meta)?)
            .map_err(|e| Error::Storage(format!("write binaries: {e}")))?;
    }
    let layouts = crate::analysis::layout(analysis);
    {
        let mut t = wtxn
            .open_table(FUNCTIONS)
            .map_err(|e| Error::Storage(format!("open functions: {e}")))?;
        for f in &layouts {
            t.insert(f.entry, encode_function(f))
                .map_err(|e| Error::Storage(format!("write functions: {e}")))?;
        }
    }
    {
        let mut t = wtxn
            .open_table(XREFS)
            .map_err(|e| Error::Storage(format!("open xrefs: {e}")))?;
        // Chunked blob rows: one row per xref costs microseconds in
        // debug builds, so fixed 17-byte entries pack `XREFS_PER_ROW`
        // per row behind a count prefix. Chunks stay small enough to
        // pack like small rows.
        for (chunk, group) in analysis.xrefs.chunks(XREFS_PER_ROW).enumerate() {
            let mut w = Writer::new();
            // Infallible: chunks hold at most `XREFS_PER_ROW` entries.
            w.u32(group.len() as u32);
            for x in group {
                w.u64(x.from);
                w.u8(xref_kind_code(x.kind));
                w.u64(x.to);
            }
            t.insert(chunk as u64, w.finish())
                .map_err(|e| Error::Storage(format!("write xrefs: {e}")))?;
        }
    }
    {
        let mut t = wtxn
            .open_table(STRINGS)
            .map_err(|e| Error::Storage(format!("open strings: {e}")))?;
        // Chunked blob rows like `xrefs`: self-delimiting bodies need no
        // per-row framing beyond the chunk count.
        for (chunk, group) in analysis.strings.chunks(STRINGS_PER_ROW).enumerate() {
            let mut w = Writer::new();
            // Infallible: chunks hold at most `STRINGS_PER_ROW` bodies.
            w.u32(group.len() as u32);
            for h in group {
                w.raw(&encode_string(h));
            }
            t.insert(chunk as u64, w.finish())
                .map_err(|e| Error::Storage(format!("write strings: {e}")))?;
        }
    }
    {
        let mut t = wtxn
            .open_table(ANNOTATIONS)
            .map_err(|e| Error::Storage(format!("open annotations: {e}")))?;
        for (&addr, ann) in annotations {
            if ann.is_empty() {
                continue;
            }
            t.insert(addr, json(ann)?)
                .map_err(|e| Error::Storage(format!("write annotations: {e}")))?;
        }
    }
    {
        let mut t = wtxn
            .open_table(FUNC_SEEDS)
            .map_err(|e| Error::Storage(format!("open func_seeds: {e}")))?;
        for (&addr, seed) in seeds {
            t.insert(addr, json(seed)?)
                .map_err(|e| Error::Storage(format!("write func_seeds: {e}")))?;
        }
    }
    {
        let mut t = wtxn
            .open_table(DIAGNOSTICS)
            .map_err(|e| Error::Storage(format!("open diagnostics: {e}")))?;
        for d in &analysis.diagnostics.records {
            t.insert((d.addr, diagnostic_kind_code(d.kind)), ())
                .map_err(|e| Error::Storage(format!("write diagnostics: {e}")))?;
        }
    }
    wtxn.commit()
        .map_err(|e| Error::Storage(format!("commit: {e}")))?;
    Ok(())
}

/// Read the `meta` version marker plus the `binaries` row 0.
///
/// Returns `Ok(None)` when the DB holds no snapshot yet (fresh file,
/// missing `meta` table, or no version row). A present `meta` table without
/// a `binaries` row, or an undecodable `binaries` row, is corruption and
/// errors: without metadata no hash comparison is possible.
fn read_meta_row(rtxn: &redb::ReadTransaction) -> Result<Option<BinaryMeta>> {
    let meta_table = match rtxn.open_table(META) {
        Ok(t) => t,
        Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None),
        Err(e) => return Err(Error::Storage(format!("open meta: {e}"))),
    };
    let has_version = meta_table
        .get("version")
        .map_err(|e| Error::Storage(format!("read meta: {e}")))?;
    if has_version.is_none() {
        return Ok(None);
    }
    drop(meta_table);

    let t = rtxn
        .open_table(BINARIES)
        .map_err(|e| Error::Storage(format!("open binaries: {e}")))?;
    let row = t
        .get(0u64)
        .map_err(|e| Error::Storage(format!("read binaries: {e}")))?
        .ok_or_else(|| Error::Storage("project db has meta but no binary row".to_string()))?;
    Ok(Some(decode(&row.value())?))
}

/// Current snapshot identity plus user rows from one read transaction:
/// metadata, annotations, and function seeds.
pub type MetaState = (
    BinaryMeta,
    BTreeMap<u64, Annotation>,
    BTreeMap<u64, FuncSeed>,
);

/// Current snapshot identity plus user rows, without decoding layouts.
///
/// Used by write fallbacks to merge one batch on top of rows another
/// process wrote in between: returns `None` on a fresh DB.
/// Missing `annotations`/`func_seeds` tables read as empty; corrupt rows
/// still error so user data is never silently dropped.
///
/// # Errors
/// [`Error::Storage`] on unreadable metadata or corrupt user rows.
/// [`Error::Limit`] when row budgets are exceeded.
pub fn load_meta_and_annotations(db: &Database) -> Result<Option<MetaState>> {
    let rtxn = db
        .begin_read()
        .map_err(|e| Error::Storage(format!("begin read: {e}")))?;
    let Some(meta) = read_meta_row(&rtxn)? else {
        return Ok(None);
    };
    let annotations = read_annotations_or_empty(&rtxn)?;
    let seeds = read_func_seeds_or_empty(&rtxn)?;
    Ok(Some((meta, annotations, seeds)))
}

/// Persisted derived rows (`functions`/block layouts, call edges, xrefs,
/// strings, diagnostics) shared by [`load`] and the hit path of [`load_cached`].
///
/// Instructions are not part of the snapshot; the caller re-decodes them
/// from the binary via
/// [`rebuild`](crate::analysis::rebuild).
struct SnapshotParts {
    functions: Vec<FunctionLayout>,
    call_edges: Vec<(u64, u64)>,
    xrefs: Vec<Xref>,
    strings: Vec<StringHit>,
    diagnostics: Vec<crate::analysis::Diagnostic>,
}

/// Decode the derived `functions`/`xrefs`/`strings` tables into
/// persistable layouts.
///
/// Function rows carry their block layouts, so no separate block table is
/// read. A missing table or an undecodable row is corruption and errors; callers holding a known-stale snapshot must not
/// call this at all (see the stale-cache policy on [`load_cached`]). Row
/// counts are capped so a forged sidecar fails fast with [`Error::Limit`].
fn read_snapshot_parts(rtxn: &redb::ReadTransaction) -> Result<SnapshotParts> {
    let mut records: Vec<crate::analysis::FunctionLayout> = Vec::new();
    let mut block_count: usize = 0;
    let mut inferred_count: usize = 0;
    {
        let t = rtxn
            .open_table(FUNCTIONS)
            .map_err(|e| Error::Storage(format!("open functions: {e}")))?;
        for row in t
            .iter()
            .map_err(|e| Error::Storage(format!("read functions: {e}")))?
        {
            let (k, v) = row.map_err(|e| Error::Storage(format!("read functions: {e}")))?;
            let raw = v.value();
            if records.len() >= MAX_SNAPSHOT_FUNCTIONS {
                return Err(Error::Limit("snapshot functions exceed cap".to_string()));
            }
            let (mut layout, n) = decode_function(k.value(), &raw)?;
            block_count += n;
            if block_count > MAX_SNAPSHOT_BLOCKS {
                return Err(Error::Limit("snapshot blocks exceed cap".to_string()));
            }
            inferred_count += layout
                .blocks
                .iter()
                .map(|b| b.inferred_succs.len())
                .sum::<usize>();
            if inferred_count > MAX_SNAPSHOT_INFERRED_EDGES {
                return Err(Error::Limit(
                    "snapshot inferred edges exceed cap".to_string(),
                ));
            }
            layout.blocks.sort_by_key(|b| b.start);
            let mut reasons = layout.reasons.clone();
            reasons.sort_unstable();
            reasons.dedup();
            layout.reasons = reasons;
            records.push(layout);
        }
    }
    // Numeric entry order, independent of key encoding: `call_edges` below
    // follows this order.
    records.sort_by_key(|f| f.entry);
    let mut functions: Vec<FunctionLayout> = Vec::new();
    let mut call_edges: Vec<(u64, u64)> = Vec::new();
    for rec in records {
        let entry = rec.entry;
        call_edges.extend(rec.calls.iter().map(|c| (entry, *c)));
        functions.push(rec);
    }
    let mut xrefs: Vec<Xref> = Vec::new();
    {
        let t = rtxn
            .open_table(XREFS)
            .map_err(|e| Error::Storage(format!("open xrefs: {e}")))?;
        for row in t
            .iter()
            .map_err(|e| Error::Storage(format!("read xrefs: {e}")))?
        {
            let (_, v) = row.map_err(|e| Error::Storage(format!("read xrefs: {e}")))?;
            let raw = v.value();
            let mut r = Reader::new(&raw);
            // Width 17 (one fixed entry): valid chunks never trip this.
            let n = r.count(17)?;
            for _ in 0..n {
                if xrefs.len() >= MAX_SNAPSHOT_XREFS {
                    return Err(Error::Limit("snapshot xrefs exceed cap".to_string()));
                }
                let from = r.u64()?;
                let kind = decode_xref_kind(r.u8()?)?;
                let to = r.u64()?;
                xrefs.push(Xref { from, to, kind });
            }
            r.end()?;
        }
    }
    xrefs.sort_by_key(|a| (a.from, a.kind, a.to));
    let mut strings: Vec<StringHit> = Vec::new();
    {
        let t = rtxn
            .open_table(STRINGS)
            .map_err(|e| Error::Storage(format!("open strings: {e}")))?;
        for row in t
            .iter()
            .map_err(|e| Error::Storage(format!("read strings: {e}")))?
        {
            let (_, v) = row.map_err(|e| Error::Storage(format!("read strings: {e}")))?;
            let raw = v.value();
            let mut r = Reader::new(&raw);
            // Width 8 (below the 11-byte minimum body): valid chunks
            // never trip this.
            let n = r.count(8)?;
            for _ in 0..n {
                if strings.len() >= MAX_SNAPSHOT_STRINGS {
                    return Err(Error::Limit("snapshot strings exceed cap".to_string()));
                }
                strings.push(decode_string(&mut r)?);
            }
            r.end()?;
        }
    }
    let diagnostics = read_diagnostics(rtxn)?;
    Ok(SnapshotParts {
        functions,
        call_edges,
        xrefs,
        strings,
        diagnostics,
    })
}

/// Decode stored diagnostics, sorted by `(addr, kind)`.
///
/// A missing table or an undecodable row is corruption and errors. Row
/// counts are capped so a forged sidecar fails fast with [`Error::Limit`].
fn read_diagnostics(rtxn: &redb::ReadTransaction) -> Result<Vec<crate::analysis::Diagnostic>> {
    let t = rtxn
        .open_table(DIAGNOSTICS)
        .map_err(|e| Error::Storage(format!("open diagnostics: {e}")))?;
    let mut diagnostics: Vec<crate::analysis::Diagnostic> = Vec::new();
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read diagnostics: {e}")))?
    {
        let (k, _) = row.map_err(|e| Error::Storage(format!("read diagnostics: {e}")))?;
        let (addr, kind) = k.value();
        if diagnostics.len() >= MAX_SNAPSHOT_DIAGNOSTICS {
            return Err(Error::Limit("snapshot diagnostics exceed cap".to_string()));
        }
        diagnostics.push(crate::analysis::Diagnostic {
            addr,
            kind: decode_diagnostic_kind(kind)?,
        });
    }
    diagnostics.sort_unstable();
    diagnostics.dedup();
    Ok(diagnostics)
}

/// Decode stored seed rows, reading a missing table as empty.
///
/// Snapshots carry seeds forward even when the sidecar has no seeds table
/// (fresh or partially written DBs); a corrupt row still errors, since
/// silently dropping user seeds would lose the `define-func` intent a
/// re-analysis keeps. Row counts are capped so a forged sidecar fails fast
/// with [`Error::Limit`].
fn read_func_seeds_or_empty(rtxn: &redb::ReadTransaction) -> Result<BTreeMap<u64, FuncSeed>> {
    let t = match rtxn.open_table(FUNC_SEEDS) {
        Ok(t) => t,
        Err(redb::TableError::TableDoesNotExist(_)) => return Ok(BTreeMap::new()),
        Err(e) => return Err(Error::Storage(format!("open func_seeds: {e}"))),
    };
    let mut seeds: BTreeMap<u64, FuncSeed> = BTreeMap::new();
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?
    {
        let (k, v) = row.map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?;
        let raw = v.value();
        if seeds.len() >= MAX_SNAPSHOT_SEEDS {
            return Err(Error::Limit("snapshot func seeds exceed cap".to_string()));
        }
        seeds.insert(k.value(), decode(&raw)?);
    }
    Ok(seeds)
}

/// Decode every stored seed row.
///
/// A missing table or an undecodable row is corruption and errors. Row
/// counts are capped so a forged sidecar fails fast with [`Error::Limit`].
fn read_func_seeds(rtxn: &redb::ReadTransaction) -> Result<BTreeMap<u64, FuncSeed>> {
    let t = rtxn
        .open_table(FUNC_SEEDS)
        .map_err(|e| Error::Storage(format!("open func_seeds: {e}")))?;
    let mut seeds: BTreeMap<u64, FuncSeed> = BTreeMap::new();
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?
    {
        let (k, v) = row.map_err(|e| Error::Storage(format!("read func_seeds: {e}")))?;
        let raw = v.value();
        if seeds.len() >= MAX_SNAPSHOT_SEEDS {
            return Err(Error::Limit("snapshot func seeds exceed cap".to_string()));
        }
        seeds.insert(k.value(), decode(&raw)?);
    }
    Ok(seeds)
}

/// Decode every stored annotation, skipping empty rows.
///
/// A missing table or an undecodable row is corruption and errors. Row
/// counts and per-field text are capped so a forged
/// sidecar fails fast with [`Error::Limit`].
fn read_annotations(rtxn: &redb::ReadTransaction) -> Result<BTreeMap<u64, Annotation>> {
    let t = rtxn
        .open_table(ANNOTATIONS)
        .map_err(|e| Error::Storage(format!("open annotations: {e}")))?;
    let mut annotations: BTreeMap<u64, Annotation> = BTreeMap::new();
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read annotations: {e}")))?
    {
        let (k, v) = row.map_err(|e| Error::Storage(format!("read annotations: {e}")))?;
        let raw = v.value();
        if annotations.len() >= MAX_SNAPSHOT_ANNOTATIONS {
            return Err(Error::Limit("snapshot annotations exceed cap".to_string()));
        }
        let ann: Annotation = decode(&raw)?;
        check_annotation_text(&ann)?;
        if !ann.is_empty() {
            annotations.insert(k.value(), ann);
        }
    }
    Ok(annotations)
}

/// Decode stored annotations, reading a missing table as empty.
///
/// Stale snapshots carry annotations forward even when the sidecar has no
/// annotations table (hand-built or partially written DBs); a corrupt
/// annotation row still errors, since silently dropping user renames and
/// comments would lose data the re-analysis keeps. Same row/text
/// budgets as [`read_annotations`](fn@read_annotations) apply.
fn read_annotations_or_empty(rtxn: &redb::ReadTransaction) -> Result<BTreeMap<u64, Annotation>> {
    let t = match rtxn.open_table(ANNOTATIONS) {
        Ok(t) => t,
        Err(redb::TableError::TableDoesNotExist(_)) => return Ok(BTreeMap::new()),
        Err(e) => return Err(Error::Storage(format!("open annotations: {e}"))),
    };
    let mut annotations: BTreeMap<u64, Annotation> = BTreeMap::new();
    for row in t
        .iter()
        .map_err(|e| Error::Storage(format!("read annotations: {e}")))?
    {
        let (k, v) = row.map_err(|e| Error::Storage(format!("read annotations: {e}")))?;
        let raw = v.value();
        if annotations.len() >= MAX_SNAPSHOT_ANNOTATIONS {
            return Err(Error::Limit("snapshot annotations exceed cap".to_string()));
        }
        let ann: Annotation = decode(&raw)?;
        check_annotation_text(&ann)?;
        if !ann.is_empty() {
            annotations.insert(k.value(), ann);
        }
    }
    Ok(annotations)
}

/// Hash- and mapping-filtered snapshot read back by [`load_cached`].
///
/// The stale variant carries only what a content-hash, mapping, revision, or
/// schema mismatch preserves: metadata plus annotations. Derived rows are
/// never decoded on that path (see the stale-cache policy on
/// [`load_cached`]).
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum Snapshot {
    /// Stored hash, mapping, and revisions match on the current schema:
    /// cached layouts plus annotations and seeds. The caller re-decodes
    /// instructions from the binary via [`rebuild`](crate::analysis::rebuild).
    Hit(Box<Loaded>),
    /// Stored hash, mapping, analysis revision, or schema version differs:
    /// metadata plus annotations and seeds only; the derived
    /// `functions`/`xrefs`/`strings` tables were never opened.
    Stale {
        /// Snapshot metadata (row 0 of `binaries`).
        meta: BinaryMeta,
        /// Annotations keyed by address.
        annotations: BTreeMap<u64, Annotation>,
        /// User-defined function seeds keyed by address.
        seeds: BTreeMap<u64, FuncSeed>,
    },
    /// No snapshot yet (fresh file, missing `meta` table, or no version row).
    Miss,
}

/// Load the snapshot for one binary identity and loader mapping in a single
/// read transaction.
///
/// Accepts any readable handle ([`Database`] or [`redb::ReadOnlyDatabase`]);
/// read opens pass a read-only handle from [`open_db_read_only`].
///
/// Cache identity covers the binary content (`expected_hash` plus
/// `expected_sha256` and `expected_size`), the
/// loader mapping — auto, explicit raw arch/base/entry, or fat slice arch
/// (`expected_mapping`) — the explicit debug inputs (`expected_debug`: a
/// `--pdb` / `--debug-file` mismatch re-analyzes while carrying same-mapping
/// annotations forward, never an error), and the analysis revision: layouts
/// replay only when all agree with the stored snapshot on the current
/// [`SCHEMA_VERSION`]. The 64-bit xxh3 hash is a fast pre-check; the
/// SHA-256 plus size is the collision-resistant gate, so a constructed
/// hash collision cannot replay another binary's layouts. A mapping mismatch
/// never reuses layouts, even when the bytes are identical; auto and
/// explicit opens never alias.
///
/// Reads and validates the `meta`/`binaries` rows first. On any identity
/// mismatch the derived tables are never opened, so opening a small binary
/// against a sidecar written for a large one costs no decoding proportional
/// to the discarded rows. A version, content, or revision mismatch under the
/// same mapping returns [`Snapshot::Stale`] with the stored annotations and
/// seeds and the binary re-analyzes. A legacy snapshot whose mapping cannot be
/// established is rebuilt, never trusted. A mapping mismatch (or an
/// ambiguous legacy mapping) over a populated annotation sidecar errors
/// with a separate-DB message instead of carrying user rows across address
/// spaces or silently dropping them. Metadata and rows come from one read
/// transaction, so a concurrent writer cannot interleave a metadata row
/// from one snapshot with rows from another.
///
/// Stale-cache policy (a deliberate behavior change; strict all-input
/// equivalence with [`load`] is not claimed): a stale snapshot's derived
/// rows are discarded unchecked, so corrupt derived rows (bad encodings,
/// shapes) and missing derived tables no longer error — [`load`] would have
/// errored on them before the caller ever compared hashes. Schema `version`,
/// `arch`, and `format` are likewise unchecked on the stale path since they
/// only gate analysis decoding. What is still validated on the stale path:
/// the `meta` version marker, the `binaries` row (missing/undecodable rows
/// error, as without metadata no hash comparison is possible), and every
/// annotation and seed row (corrupt user rows error because they carry
/// over into the re-analysis when [`stale_carry_plausible`] holds; missing
/// tables read as empty).
/// A full identity match keeps the exact [`load`] validation:
/// unknown arch/format, and any corrupt or missing table error.
///
/// Callers filter stale user rows with [`stale_carry_plausible`]:
/// [`load_cached`] itself returns the stored rows so one read transaction
/// stays coherent (see its coherence test); [`Project::open`](crate::Project::open)
/// drops implausible rows and reports them via its stale warning.
///
/// # Errors
/// [`Error::Storage`] on unreadable metadata, on corrupt user rows, on
/// mapping conflicts holding live user rows, and — for full identity
/// matches only — on corrupt derived rows, missing tables, or unknown
/// arch/format values. [`Error::Limit`] when snapshot row
/// budgets are exceeded (forged sidecar).
pub fn load_cached(
    db: &impl ReadableDatabase,
    expected_hash: u64,
    expected_sha256: &str,
    expected_size: u64,
    expected_mapping: &StoredMapping,
    expected_debug: &DebugId,
) -> Result<Snapshot> {
    let rtxn = db
        .begin_read()
        .map_err(|e| Error::Storage(format!("begin read: {e}")))?;
    let meta = match read_meta_row(&rtxn)? {
        None => return Ok(Snapshot::Miss),
        Some(meta) => meta,
    };
    if meta.version != SCHEMA_VERSION {
        let annotations = read_annotations_or_empty(&rtxn)?;
        let seeds = read_func_seeds_or_empty(&rtxn)?;
        let live = annotations.len() + seeds.len();
        if live > 0 {
            match meta.mapping.clone() {
                None => return Err(legacy_ambiguous(live, meta.version)),
                Some(stored) if &stored != expected_mapping => {
                    return Err(mapping_conflict(&stored, live, expected_mapping));
                }
                Some(_) => {}
            }
        }
        return Ok(Snapshot::Stale {
            meta,
            annotations,
            seeds,
        });
    }
    if meta.mapping.as_ref() != Some(expected_mapping) {
        let annotations = read_annotations_or_empty(&rtxn)?;
        let seeds = read_func_seeds_or_empty(&rtxn)?;
        let live = annotations.len() + seeds.len();
        if live > 0 {
            match meta.mapping.clone() {
                Some(stored) => {
                    return Err(mapping_conflict(&stored, live, expected_mapping));
                }
                None => return Err(legacy_ambiguous(live, meta.version)),
            }
        }
        return Ok(Snapshot::Stale {
            meta,
            annotations,
            seeds,
        });
    }
    if meta.content_hash != expected_hash
        || meta.content_sha256 != expected_sha256
        || meta.size != expected_size
        || meta.analysis_rev != Some(ANALYSIS_REVISION)
        || meta.debug != *expected_debug
    {
        let annotations = read_annotations_or_empty(&rtxn)?;
        let seeds = read_func_seeds_or_empty(&rtxn)?;
        return Ok(Snapshot::Stale {
            meta,
            annotations,
            seeds,
        });
    }
    let arch = parse_arch(&meta.arch)?;
    parse_format(&meta.format)?;
    let parts = read_snapshot_parts(&rtxn)?;
    let annotations = read_annotations(&rtxn)?;
    let seeds = read_func_seeds(&rtxn)?;
    Ok(Snapshot::Hit(Box::new(Loaded {
        meta,
        arch,
        functions: parts.functions,
        call_edges: parts.call_edges,
        xrefs: parts.xrefs,
        strings: parts.strings,
        diagnostics: parts.diagnostics,
        annotations,
        seeds,
    })))
}

/// Load a snapshot written by [`save`].
///
/// Returns `Ok(None)` when the DB has no snapshot yet (fresh file or no
/// `meta` table). Rebuilds function layouts, CFGs, and the callgraph from
/// stored edges; the caller re-decodes instructions via
/// [`rebuild`](crate::analysis::rebuild). Prefer [`load_cached`] on open
/// paths: it skips decoding derived rows the caller would discard after a
/// content-hash mismatch.
///
/// # Errors
/// [`Error::Storage`] on corrupt rows or unknown arch/format values,
/// [`Error::Limit`] when snapshot row budgets are exceeded.
pub fn load(db: &Database) -> Result<Option<Loaded>> {
    let rtxn = db
        .begin_read()
        .map_err(|e| Error::Storage(format!("begin read: {e}")))?;
    let meta = match read_meta_row(&rtxn)? {
        None => return Ok(None),
        Some(meta) => meta,
    };
    if meta.version != SCHEMA_VERSION {
        return Err(Error::Storage(format!(
            "project db version {} != {}",
            meta.version, SCHEMA_VERSION
        )));
    }
    let arch = parse_arch(&meta.arch)?;
    parse_format(&meta.format)?;
    let parts = read_snapshot_parts(&rtxn)?;
    let annotations = read_annotations(&rtxn)?;
    let seeds = read_func_seeds(&rtxn)?;
    Ok(Some(Loaded {
        meta,
        arch,
        functions: parts.functions,
        call_edges: parts.call_edges,
        xrefs: parts.xrefs,
        strings: parts.strings,
        diagnostics: parts.diagnostics,
        annotations,
        seeds,
    }))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hash_is_deterministic_and_sensitive() {
        assert_eq!(content_hash(b"abc"), content_hash(b"abc"));
        assert_ne!(content_hash(b"abc"), content_hash(b"abd"));
        assert_ne!(content_hash(b"abc"), content_hash(b""));
    }

    #[test]
    fn arch_and_format_round_trip() {
        for a in [
            Arch::X86_64,
            Arch::X86_32,
            Arch::Aarch64,
            Arch::Arm32,
            Arch::Thumb,
            Arch::Riscv32,
            Arch::Riscv64,
            Arch::Mips32,
            Arch::Ppc32,
            Arch::Ppc64,
        ] {
            assert_eq!(parse_arch(arch_name(a)).unwrap(), a);
        }
        for f in [
            BinaryFormat::Elf,
            BinaryFormat::Pe,
            BinaryFormat::MachO,
            BinaryFormat::Coff,
            BinaryFormat::Le,
            BinaryFormat::Raw,
        ] {
            assert_eq!(parse_format(format_name(f)).unwrap(), f);
        }
        assert!(parse_arch("mips").is_err());
        assert!(parse_format("wasm").is_err());
    }

    #[test]
    fn empty_annotation_prunes() {
        assert!(Annotation::default().is_empty());
        assert!(
            !Annotation {
                name: Some("f".to_string()),
                comment: None,
            }
            .is_empty()
        );
        assert!(
            !Annotation {
                name: Some(String::new()),
                comment: Some("c".to_string()),
            }
            .is_empty()
        );
    }

    fn sample_layout() -> crate::analysis::FunctionLayout {
        use crate::analysis::{BlockLayout, DiscoveryReason, FunctionLayout, InsnLayout};
        FunctionLayout {
            entry: 0x1000,
            name: Some("f".to_string()),
            calls: vec![0x1020, 0x1040],
            blocks: vec![
                BlockLayout {
                    start: 0x1000,
                    end: 0x1008,
                    insns: vec![
                        InsnLayout {
                            addr: 0x1000,
                            size: 5,
                        },
                        InsnLayout {
                            addr: 0x1005,
                            size: 3,
                        },
                    ],
                    succs: vec![0x1008, 0x1010],
                    inferred_succs: vec![0x1020],
                },
                BlockLayout {
                    start: 0x1008,
                    end: 0x1009,
                    insns: vec![InsnLayout {
                        addr: 0x1008,
                        size: 1,
                    }],
                    succs: vec![],
                    inferred_succs: vec![],
                },
            ],
            reasons: vec![DiscoveryReason::Entry, DiscoveryReason::DirectCall],
            arch: Some("thumb".to_string()),
        }
    }

    fn decode_body(entry: u64, raw: &[u8]) -> Result<crate::analysis::FunctionLayout> {
        let (layout, _) = decode_function(entry, raw)?;
        Ok(layout)
    }

    #[test]
    fn binary_function_row_round_trip() {
        let layout = sample_layout();
        let back = decode_body(layout.entry, &encode_function(&layout)).expect("round trip");
        assert_eq!(back, layout);
        let minimal = crate::analysis::FunctionLayout {
            entry: 0x2000,
            name: None,
            calls: vec![],
            blocks: vec![],
            reasons: vec![],
            arch: None,
        };
        let back = decode_body(minimal.entry, &encode_function(&minimal)).expect("round trip");
        assert_eq!(back, minimal);
    }

    #[test]
    fn binary_explicit_addresses_survive() {
        // Overlapping decodes never share one block from real analysis, but
        // the explicit-address flag must not lose them if they ever do.
        let mut layout = sample_layout();
        layout.blocks[0].insns[1].addr = 0x1001;
        let raw = encode_function(&layout);
        let back = decode_body(layout.entry, &raw).expect("round trip");
        assert_eq!(back, layout);
        assert_eq!(back.blocks[0].insns[1].addr, 0x1001);
    }

    fn decode_str(raw: &[u8]) -> Result<StringHit> {
        let mut r = Reader::new(raw);
        let hit = decode_string(&mut r)?;
        r.end()?;
        Ok(hit)
    }

    #[test]
    fn binary_rows_reject_garbage() {
        for bad in [
            vec![2u8],               // bad option flag
            vec![1, 9, 0, 0, 0, 65], // bad UTF-8
            vec![255u8],             // bad varint
            vec![1, 0, 0, 0],        // truncated string
        ] {
            let err = decode_str(&bad).unwrap_err();
            assert!(
                err.to_string().contains("decode row"),
                "unexpected: {err:?}"
            );
        }
        for v in [255u8, 12] {
            assert!(decode_xref_kind(v).is_err());
            assert!(decode_diagnostic_kind(v).is_err());
            assert!(decode_reason(v).is_err());
            assert!(decode_encoding(v).is_err());
        }
        // Truncations and trailing bytes fail.
        let good = encode_function(&sample_layout());
        for i in 0..good.len() {
            let mut bad = good.clone();
            bad.truncate(i);
            let err = decode_body(sample_layout().entry, &bad).unwrap_err();
            assert!(
                err.to_string().contains("decode row"),
                "prefix {i} decoded: {bad:?}"
            );
        }
        let mut bad = good.clone();
        bad.push(0);
        assert!(decode_body(sample_layout().entry, &bad).is_err());
    }

    #[test]
    fn codec_uleb128_round_trip() {
        for v in [0u64, 1, 127, 128, 300, 0xFFFF_FFFF, u64::MAX] {
            let mut w = Writer::new();
            w.uleb128(v);
            let raw = w.finish();
            let mut r = Reader::new(&raw);
            assert_eq!(r.uleb128().unwrap(), v);
            assert!(r.end().is_ok());
        }
        assert!(Reader::new(&[0x80]).uleb128().is_err());
        let mut bad = vec![0xFF; 9];
        bad.push(0x7F);
        assert!(Reader::new(&bad).uleb128().is_err());
    }

    #[test]
    fn codec_string_row_round_trip() {
        let hit = StringHit {
            offset: 12,
            vaddr: Some(0x1012),
            encoding: crate::loader::strings::Encoding::Ascii,
            value: "hi".to_string(),
            section: Some(".text".to_string()),
            executable: true,
        };
        assert_eq!(decode_str(&encode_string(&hit)).unwrap(), hit);
        let bare = StringHit {
            offset: 0,
            vaddr: None,
            encoding: crate::loader::strings::Encoding::Utf16Le,
            value: "AB".to_string(),
            section: None,
            executable: false,
        };
        assert_eq!(decode_str(&encode_string(&bare)).unwrap(), bare);
    }

    #[test]
    fn default_db_path_appends_suffix() {
        assert_eq!(
            default_db_path(Path::new("foo.exe")),
            PathBuf::from("foo.exe.rsdbg.redb")
        );
        assert_eq!(
            default_db_path(Path::new("foo.dll")),
            PathBuf::from("foo.dll.rsdbg.redb")
        );
        assert_ne!(
            default_db_path(Path::new("foo.exe")),
            default_db_path(Path::new("foo.dll"))
        );
        assert_eq!(
            default_db_path(Path::new("ls.arm64e")),
            PathBuf::from("ls.arm64e.rsdbg.redb")
        );
        assert_eq!(
            default_db_path(Path::new("prog")),
            PathBuf::from("prog.rsdbg.redb")
        );
    }
}