rto-graph 1.14.0

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

use std::path::Path;

use rusqlite::{Connection, OptionalExtension, params};

use crate::findings::{self, AnalysisRun, Finding, FindingsApplied, FindingsLayer};
use crate::media::{self, MediaFilter, MediaKind, MediaRecord, MediaWrite, ProducerSummary};
use crate::memory::{
    self, CacheEntry, CacheStats, CacheSweep, CacheWrite, MemoryError, MemoryFilter,
    MemoryForgotten, MemoryListing, MemoryRecord, MemoryWrite, Recall, RecallOptions,
};
use crate::migrations;
use crate::model::{Direction, Edge, EdgeKind, FactSet, Node, NodeKind, Span};
use crate::provenance::Provenance;

/// Errors raised by the store.
#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    /// Underlying `SQLite` failure.
    #[error("sqlite error: {0}")]
    Sqlite(#[from] rusqlite::Error),
    /// A node's `meta` could not be (de)serialized as JSON.
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
    /// An edge referenced a node key that does not exist in the store.
    #[error("unknown node key: {0}")]
    UnknownNode(String),
    /// An edge violated the provenance/confidence invariant.
    #[error("invalid edge: {0}")]
    InvalidEdge(String),
    /// A stored value could not be interpreted (database corruption).
    #[error("corrupt store: {0}")]
    Corrupt(String),
}

/// A store written by a build **newer** than this one: it records migrations
/// this binary has never heard of. Produced by [`Store::schema_ahead`].
///
/// # Why this is its own type, and not a `StoreError` variant
///
/// Opening such a store is not an error and reading it is provably sound —
/// migrations are additive in effect, so every column an older build selects is
/// still there (the one `DROP TABLE` in [`crate::migrations`] is a table rebuild
/// that re-selects every prior column). What is *not* sound is **rewriting** the
/// graph: this build would re-extract every file with its older extractor and
/// replace the newer build's content with worse content, silently. So the
/// refusal belongs on the write paths, and the carrier of that refusal has no
/// business in the type every `Store` call returns.
///
/// The second reason is semver, and it is the binding one. `StoreError` is a
/// public enum on a published 1.x crate and is not `#[non_exhaustive]`, so
/// adding a variant would stop a downstream exhaustive `match` from compiling —
/// a breaking change, which in this workspace means a major bump of all seven
/// crates. A new type is purely additive. Its fields are private and read
/// through accessors for the same reason: adding one later stays non-breaking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaAhead {
    store: u32,
    build: u32,
    unknown: Vec<u32>,
}

impl SchemaAhead {
    /// The highest schema version this store records — necessarily one this
    /// build does not know.
    #[must_use]
    pub fn store_version(&self) -> u32 {
        self.store
    }

    /// The highest schema version this build knows how to apply.
    #[must_use]
    pub fn build_version(&self) -> u32 {
        self.build
    }

    /// Every recorded version this build has never heard of, ascending. Never
    /// empty. More than one means the store ran several unknown migrations, not
    /// that anything is inconsistent.
    #[must_use]
    pub fn unknown_versions(&self) -> &[u32] {
        &self.unknown
    }
}

impl std::fmt::Display for SchemaAhead {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let unknown = self
            .unknown
            .iter()
            .map(u32::to_string)
            .collect::<Vec<_>>()
            .join(", ");
        // Both versions, and what to do about it. A bare "schema mismatch" would
        // leave the reader with nothing to act on: the useful facts are *which*
        // side is behind (this binary) and that the fix is upgrading it, not
        // deleting the store.
        write!(
            f,
            "this graph store was written by a newer Roteiro: it is at schema \
             version {store}, and this build knows only up to {build} \
             (unrecognised migration(s): {unknown}). Refusing to rewrite the \
             graph — this build would re-extract every file with its older \
             extractor and replace the newer build's graph with worse content, \
             silently. Upgrade the `roteiro` binary to one that knows schema \
             version {store} or later, then retry. Reading this store is \
             unaffected and needs no upgrade.",
            store = self.store,
            build = self.build,
        )
    }
}

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

/// A summary of applying/re-applying import layers (see
/// [`Store::apply_import_layer`] and [`Store::reapply_imports`]).
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
pub struct ImportApplied {
    /// Number of import layers processed.
    pub layers: usize,
    /// Import nodes upserted (across all layers).
    pub nodes: usize,
    /// Import edges applied — both endpoints resolved. A duplicate of an
    /// already-present edge is a harmless no-op but still counted as applied.
    pub edges_applied: usize,
    /// Import edges **pruned**: an endpoint was absent (a cross-reference to code
    /// that no longer exists), so the edge was dropped from the persisted layer
    /// rather than kept as stale data.
    pub edges_pruned: usize,
}

/// Qualified node columns for `SELECT`s that alias the `nodes` table as `n`.
const NODE_COLS: &str = "n.key, n.kind, n.name, n.path, n.lang, n.blob_hash, n.span_start, n.span_end, n.provenance, n.meta";

/// `SELECT` prefix that yields an [`Edge`] row (endpoints resolved back to keys).
const EDGE_SELECT: &str = "SELECT ns.key AS src, nd.key AS dst, e.kind, e.provenance, \
     e.confidence, e.src_ref \
     FROM edges e JOIN nodes ns ON ns.id = e.src JOIN nodes nd ON nd.id = e.dst";

/// A Roteiro graph store backed by a single `SQLite` database.
pub struct Store {
    conn: Connection,
}

impl Store {
    /// Open (creating if absent) a store at `path` and apply pending migrations.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] if the database cannot be opened or a
    /// migration fails.
    pub fn open(path: &Path) -> Result<Self, StoreError> {
        let conn = Connection::open(path)?;
        Self::from_conn(conn)
    }

    /// Open an in-memory store (tests, previews).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] if a migration fails.
    pub fn open_in_memory() -> Result<Self, StoreError> {
        let conn = Connection::open_in_memory()?;
        Self::from_conn(conn)
    }

    fn from_conn(mut conn: Connection) -> Result<Self, StoreError> {
        conn.execute_batch("PRAGMA foreign_keys = ON;")?;
        // Wait briefly for a concurrent writer instead of failing a read with
        // `database is locked`. Matters for workspace `serve` (ADR-0008), where a
        // long-lived server reads a project's graph while that repo's own
        // `roteiro sync` commits an update to the same file. Syncs are
        // sub-second, so this only ever costs a short wait, never a lost query.
        conn.busy_timeout(std::time::Duration::from_secs(5))?;
        migrations::apply(&mut conn)?;
        Ok(Self { conn })
    }

    /// The schema version this store has been migrated to: the highest `v` for
    /// which every migration `1..=v` is recorded as applied.
    ///
    /// **Not the maximum recorded version.** Migrations are selected by set
    /// membership rather than `> MAX(version)` (see [`crate::migrations`]), so a
    /// store *can* hold a gap — one written by a build that knew a higher
    /// migration but not a lower one. For a store recorded as `1..11, 13`, the
    /// maximum is 13 while none of migration 12's schema is present; reporting 13
    /// would be a higher number than the truth. The contiguous prefix reports 11,
    /// which is the version the store can actually be relied on to provide.
    ///
    /// The gap is transient in practice — the next [`Store::open`] repairs it —
    /// but this must not answer wrongly in the window where it exists, which is
    /// exactly the window in which someone is debugging.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn schema_version(&self) -> Result<u32, StoreError> {
        Ok(migrations::store_version(&self.conn)?)
    }

    /// The highest schema version **this build** knows how to apply — the
    /// binary's half of the comparison [`Store::schema_ahead`] makes.
    #[must_use]
    pub fn build_schema_version() -> u32 {
        migrations::latest_version()
    }

    /// `Some` when this store was written by a **newer** build: it records
    /// migrations this binary has never heard of. `None` — the ordinary case —
    /// when the store is level with this build or behind it.
    ///
    /// Call this before **rewriting** a graph (sync, reconcile, rebuild). Do not
    /// call it to gate reads: an older binary's reads are sound, which is the
    /// whole reason this is not checked in [`Store::open`] (issue #342).
    ///
    /// # Which version this compares, and why it is not [`Store::schema_version`]
    ///
    /// It compares the **set** of recorded versions against the migrations this
    /// build carries, so it answers *has anything newer than me written here?*
    /// [`Store::schema_version`] answers a different question — the highest
    /// **gap-free** version, a floor describing what a reader may rely on — and
    /// using it here would let the bug through: a store recorded `1..=13, 15`,
    /// read by a build that knows 13, has a gap-free version of 13, so
    /// `schema_version() > build` is false while migration 15's schema sits in
    /// the file and a newer build plainly assembled the graph.
    ///
    /// Conflating the two situations would also misreport both, so it does not.
    /// A **gap** — a store missing a lower migration this build knows — is not
    /// a store from the future, and [`Store::open`] has already repaired it by
    /// applying the missing migration before this can be called. What is left is
    /// only ever a version no build of this vintage could have written.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn schema_ahead(&self) -> Result<Option<SchemaAhead>, StoreError> {
        let unknown = migrations::versions_ahead_of_build(&self.conn)?;
        let Some(&store) = unknown.last() else {
            return Ok(None);
        };
        Ok(Some(SchemaAhead {
            store,
            build: Self::build_schema_version(),
            unknown,
        }))
    }

    /// Number of nodes currently in the store.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn node_count(&self) -> Result<u64, StoreError> {
        let n: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM nodes", [], |r| r.get(0))?;
        Ok(u64::try_from(n).unwrap_or(0))
    }

    /// Number of edges currently in the store.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn edge_count(&self) -> Result<u64, StoreError> {
        let n: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM edges", [], |r| r.get(0))?;
        Ok(u64::try_from(n).unwrap_or(0))
    }

    /// Insert or update a node, keyed by its natural [`Node::key`].
    ///
    /// # Errors
    /// Returns [`StoreError::Json`] if `meta` cannot be serialized, or
    /// [`StoreError::Sqlite`] on write failure.
    pub fn upsert_node(&self, node: &Node) -> Result<(), StoreError> {
        upsert_node(&self.conn, node)
    }

    /// Insert an edge. Both endpoints must already resolve to nodes.
    ///
    /// # Errors
    /// Returns [`StoreError::InvalidEdge`] if the provenance/confidence
    /// invariant is violated, [`StoreError::UnknownNode`] if an endpoint key is
    /// absent, or [`StoreError::Sqlite`] on write failure.
    pub fn insert_edge(&self, edge: &Edge) -> Result<(), StoreError> {
        insert_edge(&self.conn, edge)
    }

    /// Apply a fact set atomically: all nodes are upserted, then all edges are
    /// inserted, in a single transaction. On any error nothing is committed.
    ///
    /// # Errors
    /// Returns the first error encountered (see [`Store::upsert_node`] and
    /// [`Store::insert_edge`]); the transaction is rolled back.
    pub fn apply_factset(&mut self, facts: &FactSet) -> Result<(), StoreError> {
        let tx = self.conn.transaction()?;
        for node in &facts.nodes {
            upsert_node(&tx, node)?;
        }
        for edge in &facts.edges {
            insert_edge(&tx, edge)?;
        }
        tx.commit()?;
        Ok(())
    }

    /// The `HEAD` tree id recorded at the last successful [`Store::rebuild`], if
    /// any. Used by the sync engine to detect an unchanged tree.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn sync_state(&self) -> Result<Option<String>, StoreError> {
        Ok(self
            .conn
            .query_row("SELECT tree FROM sync_state WHERE id = 0", [], |r| r.get(0))
            .optional()?)
    }

    /// The extractor environment recorded with the last committed [`sync`],
    /// `None` if unset (a legacy row, or the last sync was a worktree/index
    /// preview). The incremental committed `sync` compares this to the current
    /// env and falls back to a full re-extraction when they differ.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn sync_env(&self) -> Result<Option<String>, StoreError> {
        Ok(self
            .conn
            .query_row("SELECT env FROM sync_state WHERE id = 0", [], |r| r.get(0))
            .optional()?
            .flatten())
    }

    /// Record the extractor environment for the current synced tree. Called by a
    /// committed `sync` right after it writes the tree, so a later sync can decide
    /// whether the incremental fast path is sound. A no-op if no tree is recorded.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn set_sync_env(&self, env: &str) -> Result<(), StoreError> {
        self.conn
            .execute("UPDATE sync_state SET env = ?1 WHERE id = 0", [env])?;
        Ok(())
    }

    /// Which working tree this graph was assembled from, or `None` when the store
    /// has never been synced or predates the column (issue #330).
    ///
    /// `graph.db` is an assembled view of **one** tree, so a store that came to
    /// describe a different tree — restored from a backup, copied along with a
    /// `.git` directory, or reached after a layout change — would otherwise
    /// answer confidently about the wrong one: `sync` reporting "up to date"
    /// against a state id belonging to someone else's tree, `check` validating a
    /// tree nobody is looking at. `None` reads as "unknown" and is *adopted*
    /// rather than treated as a mismatch, so an existing store is never rebuilt
    /// merely for predating the stamp.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn synced_worktree(&self) -> Result<Option<String>, StoreError> {
        Ok(self
            .conn
            .query_row("SELECT worktree FROM sync_state WHERE id = 0", [], |r| {
                r.get(0)
            })
            .optional()?
            .flatten())
    }

    /// Stamp this graph as assembled from `worktree`. Called by every sync entry
    /// point right after it writes the tree state, so the two always agree about
    /// whose tree the state describes. A no-op if no tree is recorded.
    ///
    /// Unlike [`Store::set_sync_env`]'s value, this survives a tree write: it
    /// identifies the *store*, not the tree, and a new commit does not move the
    /// graph to a different working tree.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn set_synced_worktree(&self, worktree: &str) -> Result<(), StoreError> {
        self.conn.execute(
            "UPDATE sync_state SET worktree = ?1 WHERE id = 0",
            [worktree],
        )?;
        Ok(())
    }

    /// Atomically replace the entire graph with `facts`, recording `tree` as the
    /// synced state (or clearing it when `tree` is `None`). All existing nodes
    /// and edges are deleted first, so the store reflects exactly the given fact
    /// set.
    ///
    /// Passing `None` records *no* synced tree — distinct from an empty string —
    /// so [`Store::sync_state`] returns `None` and a later `sync` will not
    /// spuriously short-circuit.
    ///
    /// # Errors
    /// Returns the first error encountered (see [`Store::apply_factset`]); on any
    /// error nothing is committed.
    pub fn rebuild(&mut self, facts: &FactSet, tree: Option<&str>) -> Result<(), StoreError> {
        let tx = self.conn.transaction()?;
        tx.execute("DELETE FROM edges", [])?;
        tx.execute("DELETE FROM nodes", [])?;
        for node in &facts.nodes {
            upsert_node(&tx, node)?;
        }
        for edge in &facts.edges {
            insert_edge(&tx, edge)?;
        }
        write_sync_state(&tx, tree)?;
        tx.commit()?;
        Ok(())
    }

    /// Bring the store to exactly `facts` (as [`Store::rebuild`] does) but writing
    /// only what **differs** instead of wiping and reinserting the whole graph —
    /// the git-style "write only the delta". Unchanged node rows (which carry the
    /// heavy JSON `meta`) and unchanged edge rows are left untouched; only removed
    /// rows are deleted and new/changed rows written. The final state — nodes,
    /// edges, and `sync_state` — is identical to `rebuild(facts, tree)`.
    ///
    /// Leaving unchanged edges in place means their row ids do not match a cold
    /// rebuild's — which is safe *because* every edge query is content-ordered
    /// (`(src, dst, kind, provenance)`, see [`Store::all_edges`]), never by row
    /// id. So an incrementally reconciled store and a fresh rebuild return every
    /// query identically; the delta is invisible above the storage layer.
    ///
    /// # Errors
    /// Returns [`StoreError`] on a query failure; the transaction is rolled back.
    pub fn reconcile(&mut self, facts: &FactSet, tree: Option<&str>) -> Result<(), StoreError> {
        let current_nodes = self.all_nodes()?;
        let current_edges = self.all_edges()?;
        let cur_by_key: std::collections::HashMap<&str, &Node> =
            current_nodes.iter().map(|n| (n.key.as_str(), n)).collect();
        let new_keys: std::collections::HashSet<&str> =
            facts.nodes.iter().map(|n| n.key.as_str()).collect();

        // Edge identity is the full tuple, so a changed `confidence`/`src_ref`
        // counts as remove-old + add-new — keeping the result identical to a
        // wholesale rebuild, not the store's insert-time `DO NOTHING` semantics.
        let new_edge_ids: std::collections::HashSet<EdgeId> =
            facts.edges.iter().map(edge_identity).collect();
        let cur_edge_ids: std::collections::HashSet<EdgeId> =
            current_edges.iter().map(edge_identity).collect();

        let tx = self.conn.transaction()?;
        // 1. Delete removed edges first, so any node they reference can then be
        //    dropped (edges are FK-constrained on node ids, with no cascade). A
        //    removed node's edges are all removals, so they are gone before step 2.
        for edge in &current_edges {
            if !new_edge_ids.contains(&edge_identity(edge)) {
                delete_edge(&tx, edge)?;
            }
        }
        // 2. Drop nodes that no longer exist.
        for old in &current_nodes {
            if !new_keys.contains(old.key.as_str()) {
                tx.execute("DELETE FROM nodes WHERE key = ?1", [&old.key])?;
            }
        }
        // 3. Upsert only the nodes that are new or whose content changed (an upsert
        //    keeps the row id, so unchanged edges stay valid).
        for node in &facts.nodes {
            if cur_by_key
                .get(node.key.as_str())
                .is_none_or(|cur| *cur != node)
            {
                upsert_node(&tx, node)?;
            }
        }
        // 4. Insert only the added edges (their endpoints now all exist).
        for edge in &facts.edges {
            if !cur_edge_ids.contains(&edge_identity(edge)) {
                insert_edge(&tx, edge)?;
            }
        }
        write_sync_state(&tx, tree)?;
        tx.commit()?;
        Ok(())
    }

    /// Fetch a node by its natural key.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] if a stored value cannot be decoded.
    pub fn get_node(&self, key: &str) -> Result<Option<Node>, StoreError> {
        let sql = format!("SELECT {NODE_COLS} FROM nodes n WHERE n.key = ?1");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([key])?;
        match rows.next()? {
            Some(row) => Ok(Some(row_to_node(row)?)),
            None => Ok(None),
        }
    }

    /// Every node key in the store, ordered. Useful for whole-graph exports.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn all_keys(&self) -> Result<Vec<String>, StoreError> {
        let mut stmt = self.conn.prepare("SELECT key FROM nodes ORDER BY key")?;
        let rows = stmt.query_map([], |r| r.get::<_, String>(0))?;
        let mut out = Vec::new();
        for row in rows {
            out.push(row?);
        }
        Ok(out)
    }

    /// Dump the entire graph as a single [`FactSet`], with nodes and edges in a
    /// deterministic order — suitable for a portable, content-stable artifact.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn export_factset(&self) -> Result<FactSet, StoreError> {
        let node_sql = format!("SELECT {NODE_COLS} FROM nodes n ORDER BY n.key");
        let mut node_stmt = self.conn.prepare(&node_sql)?;
        let mut node_rows = node_stmt.query([])?;
        let nodes = collect_nodes(&mut node_rows)?;

        // Order edges by their resolved endpoint keys (not row id) so the dump is
        // stable regardless of insertion order.
        let edge_sql = format!("{EDGE_SELECT} ORDER BY ns.key, nd.key, e.kind, e.provenance");
        let mut edge_stmt = self.conn.prepare(&edge_sql)?;
        let mut edge_rows = edge_stmt.query([])?;
        let edges = collect_edges(&mut edge_rows)?;

        Ok(FactSet { nodes, edges })
    }

    /// All nodes of a given kind.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn nodes_by_kind(&self, kind: &NodeKind) -> Result<Vec<Node>, StoreError> {
        let sql = format!("SELECT {NODE_COLS} FROM nodes n WHERE n.kind = ?1 ORDER BY n.key");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([kind.as_str()])?;
        collect_nodes(&mut rows)
    }

    /// Nodes of a given kind whose `name` equals `name_lower` **case-insensitively**,
    /// ordered by key. Narrows a lookup at the SQL layer — using the `kind` index and
    /// filtering `name` in-query — so only matching rows are decoded, never every
    /// node of that kind. Used by the cross-repo follow bridge to fetch just the
    /// candidate struct(s) for a config section rather than scanning all structs.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn nodes_by_kind_named(
        &self,
        kind: &NodeKind,
        name_lower: &str,
    ) -> Result<Vec<Node>, StoreError> {
        let sql = format!(
            "SELECT {NODE_COLS} FROM nodes n \
             WHERE n.kind = ?1 AND lower(n.name) = ?2 ORDER BY n.key"
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([kind.as_str(), name_lower])?;
        collect_nodes(&mut rows)
    }

    /// Every `config_key` node's flattened setting (ADR-0009), read back out of
    /// the graph as [`crate::ConfigKey`]s — the graph-native source the cross-repo
    /// link matcher (`roteiro links --infer`) consumes, so it never re-parses
    /// config files. Ordered by node key (deterministic). A node missing the
    /// `key`/`path` a well-formed `config_key` carries is skipped defensively.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn config_keys(&self) -> Result<Vec<crate::ConfigKey>, StoreError> {
        let nodes = self.nodes_by_kind(&NodeKind::Other(crate::config_keys::KIND.to_owned()))?;
        let mut out = Vec::with_capacity(nodes.len());
        for n in &nodes {
            let key = n.meta.get("key").and_then(serde_json::Value::as_str);
            // A `config_key` node carries a `value` in `meta` when it has a real
            // setting (file-derived keys always do, even an empty string). A
            // struct-derived key (`meta.source = "struct"`) omits it — its value is
            // *unknown*, not empty — so record that absence explicitly rather than
            // defaulting it to `""`, which would false-match in value agreement.
            let value = n.meta.get("value").and_then(serde_json::Value::as_str);
            if let (Some(key), Some(file)) = (key, n.path.as_deref()) {
                out.push(crate::ConfigKey {
                    file: file.to_owned(),
                    key: key.to_owned(),
                    value: value.unwrap_or_default().to_owned(),
                    value_known: value.is_some(),
                });
            }
        }
        Ok(out)
    }

    /// Every node whose source `path` is `path`, ordered by key — the file node
    /// plus the symbols and markers defined in it. Used to scope a change to the
    /// graph (e.g. `roteiro review`).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn nodes_by_path(&self, path: &str) -> Result<Vec<Node>, StoreError> {
        let sql = format!("SELECT {NODE_COLS} FROM nodes n WHERE n.path = ?1 ORDER BY n.key");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([path])?;
        collect_nodes(&mut rows)
    }

    /// Every node produced by a given layer, ordered by key. The incremental
    /// `sync` loads the `Derived` layer to reconstruct the extraction graph
    /// without re-reading every blob.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn nodes_by_provenance(&self, provenance: Provenance) -> Result<Vec<Node>, StoreError> {
        let sql = format!("SELECT {NODE_COLS} FROM nodes n WHERE n.provenance = ?1 ORDER BY n.key");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([provenance.as_str()])?;
        collect_nodes(&mut rows)
    }

    /// Every node in the store, ordered by key. Unlike [`Store::export_factset`]
    /// this decodes no edges, so it is cheap for node-only scans (e.g. search).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on decode failure.
    pub fn all_nodes(&self) -> Result<Vec<Node>, StoreError> {
        let sql = format!("SELECT {NODE_COLS} FROM nodes n ORDER BY n.key");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([])?;
        collect_nodes(&mut rows)
    }

    /// Every edge in the store, with endpoints resolved to their node keys. Used
    /// by [`Store::reconcile`] to diff the edge set.
    ///
    /// Ordered by the edge's **content** — `(src key, dst key, kind, provenance)`,
    /// the table's unique tuple — not by row id. This makes the order a function
    /// of the *graph*, not of insertion history, so an incrementally
    /// [`reconcile`](Store::reconcile)d store and a cold [`rebuild`](Store::rebuild)
    /// return edges identically. (The same reason node scans order by `key`.)
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] or [`StoreError::Corrupt`] on failure.
    pub fn all_edges(&self) -> Result<Vec<Edge>, StoreError> {
        let sql = format!("{EDGE_SELECT} ORDER BY ns.key, nd.key, e.kind, e.provenance");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([])?;
        collect_edges(&mut rows)
    }

    /// Edges whose source is the node with the given key, in content order
    /// (`(dst key, kind, provenance)` — `src` is fixed). Content-ordered rather
    /// than by row id so the result is history-independent; see [`Store::all_edges`].
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] or [`StoreError::Corrupt`] on failure.
    pub fn edges_from(&self, key: &str) -> Result<Vec<Edge>, StoreError> {
        let sql = format!("{EDGE_SELECT} WHERE ns.key = ?1 ORDER BY nd.key, e.kind, e.provenance");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([key])?;
        collect_edges(&mut rows)
    }

    /// Edges whose destination is the node with the given key, in content order
    /// (`(src key, kind, provenance)` — `dst` is fixed). See [`Store::all_edges`].
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] or [`StoreError::Corrupt`] on failure.
    pub fn edges_to(&self, key: &str) -> Result<Vec<Edge>, StoreError> {
        let sql = format!("{EDGE_SELECT} WHERE nd.key = ?1 ORDER BY ns.key, e.kind, e.provenance");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([key])?;
        collect_edges(&mut rows)
    }

    /// All edges with the given provenance, in content order
    /// (`(src key, dst key, kind)` — `provenance` is fixed). See [`Store::all_edges`].
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] or [`StoreError::Corrupt`] on failure.
    pub fn edges_by_provenance(&self, provenance: Provenance) -> Result<Vec<Edge>, StoreError> {
        let sql = format!("{EDGE_SELECT} WHERE e.provenance = ?1 ORDER BY ns.key, nd.key, e.kind");
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([provenance.as_str()])?;
        collect_edges(&mut rows)
    }

    /// Delete all edges with the given provenance, returning how many were
    /// removed. Used to re-derive a whole provenance class authoritatively (e.g.
    /// `inferred` edges when re-running inference with different parameters).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn delete_edges_by_provenance(&self, provenance: Provenance) -> Result<u64, StoreError> {
        let n = self.conn.execute(
            "DELETE FROM edges WHERE provenance = ?1",
            [provenance.as_str()],
        )?;
        Ok(u64::try_from(n).unwrap_or(0))
    }

    /// Delete all edges carrying the given `src_ref`, returning how many were
    /// removed. Lets one producer of `inferred` edges (e.g. the embedding layer,
    /// or a Graphify import) re-derive its own edges authoritatively without
    /// touching edges another producer contributed.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn delete_edges_by_src_ref(&self, src_ref: &str) -> Result<u64, StoreError> {
        let n = self
            .conn
            .execute("DELETE FROM edges WHERE src_ref = ?1", [src_ref])?;
        Ok(u64::try_from(n).unwrap_or(0))
    }

    /// Apply an import layer to the live graph **and** persist it durably under
    /// `src_ref`, validating as it goes: this ref's prior edges are cleared
    /// (an authoritative re-import), the layer's nodes are upserted, and each
    /// edge is applied only if both endpoints resolve. Dangling edges — cross-
    /// references to code that is not present — are dropped, and only the
    /// validated (trimmed) layer is persisted, so stale data is never stored.
    ///
    /// This is the "validate on import" half; [`Store::reapply_imports`] is the
    /// "validate on sync" half, re-checking layers against the rebuilt graph.
    ///
    /// # Errors
    /// Returns [`StoreError::Json`] if `facts` cannot be (de)serialized,
    /// [`StoreError::InvalidEdge`] on a malformed edge, or [`StoreError::Sqlite`]
    /// on write failure.
    pub fn apply_import_layer(
        &mut self,
        src_ref: &str,
        facts: &FactSet,
    ) -> Result<ImportApplied, StoreError> {
        let tx = self.conn.transaction()?;
        // Authoritative re-import: drop this ref's prior edges from the live graph.
        tx.execute("DELETE FROM edges WHERE src_ref = ?1", [src_ref])?;
        for node in &facts.nodes {
            upsert_node(&tx, node)?;
        }
        let (kept, applied) = apply_edges_pruning(&tx, &facts.edges)?;
        let trimmed = FactSet {
            nodes: facts.nodes.clone(),
            edges: kept,
        };
        put_import_row(&tx, src_ref, &trimmed)?;
        tx.commit()?;
        Ok(ImportApplied {
            layers: 1,
            nodes: facts.nodes.len(),
            ..applied
        })
    }

    /// Remove the persisted import layer for `src_ref`, returning whether one
    /// existed. Does not remove edges already in the live graph (use
    /// [`Store::delete_edges_by_src_ref`] for that).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn delete_import(&self, src_ref: &str) -> Result<bool, StoreError> {
        let n = self
            .conn
            .execute("DELETE FROM imports WHERE src_ref = ?1", [src_ref])?;
        Ok(n > 0)
    }

    /// The `src_ref`s of all persisted import layers, ordered.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn import_refs(&self) -> Result<Vec<String>, StoreError> {
        let mut stmt = self
            .conn
            .prepare("SELECT src_ref FROM imports ORDER BY src_ref")?;
        let rows = stmt.query_map([], |r| r.get::<_, String>(0))?;
        let mut out = Vec::new();
        for row in rows {
            out.push(row?);
        }
        Ok(out)
    }

    /// Re-apply every persisted import layer on top of the current graph and
    /// **re-validate** it: all import nodes are upserted first (so cross-layer
    /// and self references resolve), then each edge is applied; an edge whose
    /// endpoint is now absent — a cross-reference to code a sync removed — is
    /// pruned from the persisted layer, not merely skipped. So the durable store
    /// keeps only still-correct data. Idempotent; safe to run after each rebuild.
    ///
    /// # Errors
    /// Returns [`StoreError::Json`] if a stored layer cannot be (de)serialized,
    /// or [`StoreError::Sqlite`] on write failure.
    pub fn reapply_imports(&mut self) -> Result<ImportApplied, StoreError> {
        let layers = self.load_import_layers()?;
        let tx = self.conn.transaction()?;
        // Pass 1: upsert every layer's nodes so intra-import edges resolve
        // regardless of which layer defines the endpoint.
        for (_, facts) in &layers {
            for node in &facts.nodes {
                upsert_node(&tx, node)?;
            }
        }
        // Pass 2: apply edges, pruning (and rewriting) any that dangle.
        let mut applied = ImportApplied {
            layers: layers.len(),
            ..ImportApplied::default()
        };
        for (src_ref, facts) in &layers {
            applied.nodes += facts.nodes.len();
            let (kept, counts) = apply_edges_pruning(&tx, &facts.edges)?;
            applied.edges_applied += counts.edges_applied;
            applied.edges_pruned += counts.edges_pruned;
            if kept.len() != facts.edges.len() {
                let trimmed = FactSet {
                    nodes: facts.nodes.clone(),
                    edges: kept,
                };
                put_import_row(&tx, src_ref, &trimmed)?;
            }
        }
        tx.commit()?;
        Ok(applied)
    }

    /// Load and decode every persisted import layer as `(src_ref, FactSet)`, in
    /// `src_ref` order.
    fn load_import_layers(&self) -> Result<Vec<(String, FactSet)>, StoreError> {
        let mut stmt = self
            .conn
            .prepare("SELECT src_ref, facts FROM imports ORDER BY src_ref")?;
        let rows = stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
        let mut out = Vec::new();
        for row in rows {
            let (src_ref, json) = row?;
            let mut facts: FactSet = serde_json::from_str(&json)?;
            // An import-layer node is *never* derived (derivation is `sync`'s job),
            // so a `Derived` tag here is always wrong. It arises two ways, both
            // repaired the same: a legacy layer persisted before nodes carried
            // provenance (the field is absent → serde defaults `Derived`), or —
            // anomalously — a layer that stored an explicit `"provenance":"derived"`
            // (a producer/data bug). We deliberately repair *both* rather than only
            // the absent case: leaving an explicit-`derived` import node in place
            // would let a layer-scoped `sync` treat it as derived and delete it —
            // the exact corruption this guards against — so repair is the safe
            // recovery, not silent masking. Idempotent, runs on every reapply
            // (old stores self-heal), and a no-op for correctly-tagged fresh imports.
            for node in &mut facts.nodes {
                if node.provenance == Provenance::Derived {
                    node.provenance = import_node_provenance(&node.key);
                }
            }
            out.push((src_ref, facts));
        }
        Ok(out)
    }

    /// Neighbouring nodes reachable from `key` in the given direction. Returns
    /// an empty vector if the node does not exist.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`], [`StoreError::Json`], or
    /// [`StoreError::Corrupt`] on failure.
    pub fn neighbors(&self, key: &str, dir: Direction) -> Result<Vec<Node>, StoreError> {
        let out = format!(
            "SELECT {NODE_COLS} FROM nodes n JOIN edges e ON n.id = e.dst \
             JOIN nodes s ON s.id = e.src WHERE s.key = ?1"
        );
        let inc = format!(
            "SELECT {NODE_COLS} FROM nodes n JOIN edges e ON n.id = e.src \
             JOIN nodes d ON d.id = e.dst WHERE d.key = ?1"
        );
        // Order by output column 1 (the node key) so results are deterministic
        // across SQLite versions/plans. Positional ordering avoids both the
        // ambiguity of a bare `key` (present in every joined table) and the fact
        // that a table-qualified name cannot be used after the `Both` UNION.
        let sql = match dir {
            Direction::Outgoing => format!("{out} ORDER BY 1"),
            Direction::Incoming => format!("{inc} ORDER BY 1"),
            Direction::Both => format!("{out} UNION {inc} ORDER BY 1"),
        };
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query([key])?;
        collect_nodes(&mut rows)
    }

    /// Fetch the cached context bundle for `key` as `(fingerprint, json)`, if
    /// present. The caller compares the fingerprint to the node's current one to
    /// decide whether the entry is fresh (see [`crate::context`]).
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn context_cache_get(&self, key: &str) -> Result<Option<(String, String)>, StoreError> {
        let row = self
            .conn
            .query_row(
                "SELECT fingerprint, json FROM node_context WHERE key = ?1",
                [key],
                |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)),
            )
            .optional()?;
        Ok(row)
    }

    /// Fetch just the cached fingerprint for `key`, without reading the (larger)
    /// JSON payload — for a cheap freshness check.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn context_cache_fingerprint(&self, key: &str) -> Result<Option<String>, StoreError> {
        let fp = self
            .conn
            .query_row(
                "SELECT fingerprint FROM node_context WHERE key = ?1",
                [key],
                |r| r.get::<_, String>(0),
            )
            .optional()?;
        Ok(fp)
    }

    /// Store (or replace) the cached context bundle for `key`.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn context_cache_put(
        &self,
        key: &str,
        fingerprint: &str,
        json: &str,
    ) -> Result<(), StoreError> {
        self.conn.execute(
            "INSERT INTO node_context (key, fingerprint, json) VALUES (?1, ?2, ?3)
             ON CONFLICT(key) DO UPDATE SET
                 fingerprint = excluded.fingerprint, json = excluded.json",
            [key, fingerprint, json],
        )?;
        Ok(())
    }

    /// Delete the cached context entry for `key`, returning whether one existed.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn context_cache_delete(&self, key: &str) -> Result<bool, StoreError> {
        let n = self
            .conn
            .execute("DELETE FROM node_context WHERE key = ?1", [key])?;
        Ok(n > 0)
    }

    /// Every key with a cached context entry, ordered. Used to prune entries for
    /// nodes that no longer exist.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn context_cache_keys(&self) -> Result<Vec<String>, StoreError> {
        let mut stmt = self
            .conn
            .prepare("SELECT key FROM node_context ORDER BY key")?;
        let rows = stmt.query_map([], |r| r.get::<_, String>(0))?;
        let mut out = Vec::new();
        for row in rows {
            out.push(row?);
        }
        Ok(out)
    }

    // --- Analyzer findings (ADR-0012). A separate artifact store: these methods
    // touch `analysis_runs`/`findings` only, never `nodes`/`edges`, so
    // `export_factset` — and the published `GraphArtifact` — stays a pure
    // function of the tree no matter what an analyzer reports. ---

    /// Replace the findings layer `run.layer` **wholesale**, atomically: the
    /// previous run for that layer and every finding row it owned are deleted,
    /// then this run and its findings are written. A finding that has since been
    /// fixed therefore *disappears* instead of lingering, and re-ingesting an
    /// unchanged report is idempotent — the store ends up with the same rows and
    /// no growth.
    ///
    /// The owned-record cleanup is explicit rather than inherited. The import
    /// path ([`Store::apply_import_layer`]) deletes a layer's edges but leaves its
    /// obsolete *nodes* behind; copying that shape here would silently orphan
    /// findings, so the previous run's rows are deleted by hand and counted in
    /// [`FindingsApplied::removed`]. The schema's `ON DELETE CASCADE` is kept as
    /// defence in depth, not as the mechanism.
    ///
    /// `findings` must carry distinct [`FindingKey`](crate::FindingKey)s: a
    /// duplicate identity is a producer bug and is rejected by the unique index
    /// *inside* the transaction, so nothing is committed. Callers that parse
    /// untrusted reports should reject duplicates earlier, with a better message.
    ///
    /// # Errors
    /// Returns [`StoreError::Json`] if the run's command policy or a finding's
    /// `meta` cannot be serialized, or [`StoreError::Sqlite`] on write failure. On
    /// any error the transaction is rolled back and the previous layer survives
    /// intact.
    pub fn replace_findings_layer(
        &mut self,
        run: &AnalysisRun,
        findings: &[Finding],
    ) -> Result<FindingsApplied, StoreError> {
        let tx = self.conn.transaction()?;
        let applied = findings::replace_layer(&tx, run, findings)?;
        tx.commit()?;
        Ok(applied)
    }

    /// Delete a findings layer and every finding row it owns, returning how many
    /// findings went with it, or `None` if the layer was not live.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure; nothing is committed on
    /// error.
    pub fn delete_findings_layer(&mut self, layer: &str) -> Result<Option<usize>, StoreError> {
        let tx = self.conn.transaction()?;
        let removed = findings::delete_layer(&tx, layer)?;
        tx.commit()?;
        Ok(removed)
    }

    /// Every live findings layer with its findings, ordered by layer key and, in
    /// each layer, by finding key. Pass `analyzer` to narrow to one analyzer.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, [`StoreError::Json`] if a
    /// stored policy or `meta` cannot be decoded, or [`StoreError::Corrupt`] on an
    /// unrecognised stored token.
    pub fn findings_layers(
        &self,
        analyzer: Option<&str>,
    ) -> Result<Vec<FindingsLayer>, StoreError> {
        findings::layers(&self.conn, analyzer)
    }

    /// Number of findings currently stored, across every layer.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn finding_count(&self) -> Result<u64, StoreError> {
        findings::count_findings(&self.conn)
    }

    /// Number of live analysis runs — one per findings layer.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn analysis_run_count(&self) -> Result<u64, StoreError> {
        findings::count_runs(&self.conn)
    }

    // --- Generated media content (ADR-0015). A separate artifact store, on the
    // same terms as findings above: these methods touch `media_content` only,
    // never `nodes`/`edges`, so `export_factset` stays a pure function of the
    // tree across a `media build`, and generated text can never reach the
    // `authored` relevance boost in `search`. ---

    /// Write one generated-content record, returning `true` if a row was written.
    ///
    /// Keyed by `(blob_id, producer)`. A record for that exact pair already
    /// present is **left alone** and `false` is returned — which is what makes
    /// `media build` incremental and a second run free. A *different* producer is
    /// a new row beside it, never an overwrite: that is the point of keying on the
    /// producer identity, so a better model's description can be compared with the
    /// one it replaces and a distrusted producer can be dropped wholesale.
    /// [`MediaWrite::replace`] (only `media build --force`) is the one path that
    /// overwrites, and only for the identical producer.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on a write failure; the transaction is
    /// rolled back.
    pub fn record_media_content(&mut self, write: &MediaWrite<'_>) -> Result<bool, StoreError> {
        let tx = self.conn.transaction()?;
        let written = media::record(&tx, write)?;
        tx.commit()?;
        Ok(written)
    }

    /// Whether a record already exists for exactly this `(blob, producer)`.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn has_media_record(&self, blob_id: &str, producer: &str) -> Result<bool, StoreError> {
        media::exists(&self.conn, blob_id, producer)
    }

    /// Stored records matching `filter`, ordered by `(producer, blob id)`.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or
    /// [`StoreError::Corrupt`] if a row carries an unknown modality token.
    pub fn media_records(&self, filter: &MediaFilter<'_>) -> Result<Vec<MediaRecord>, StoreError> {
        media::records(&self.conn, filter)
    }

    /// Discard records — all of them, or only those written by `producer`.
    /// Returns how many rows went.
    ///
    /// Nothing in the graph is touched: dropping a model you no longer trust must
    /// not cost you a re-sync.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on a write failure.
    pub fn clear_media_content(&mut self, producer: Option<&str>) -> Result<usize, StoreError> {
        let tx = self.conn.transaction()?;
        let removed = media::delete(&tx, producer)?;
        tx.commit()?;
        Ok(removed)
    }

    /// Number of generated-content records currently stored.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn media_content_count(&self) -> Result<u64, StoreError> {
        media::count(&self.conn)
    }

    /// One summary per producer that owns records, ordered by producer id.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or
    /// [`StoreError::Corrupt`] if a row carries an unknown modality token.
    pub fn media_producer_summaries(&self) -> Result<Vec<ProducerSummary>, StoreError> {
        media::producer_summaries(&self.conn)
    }

    /// The blob ids that have at least one record carrying **generated text**
    /// for `kind`. A blob the pre-generation gate refused is not described, so it
    /// is not here — see [`Store::gated_media_blobs`].
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn described_media_blobs(
        &self,
        kind: MediaKind,
    ) -> Result<std::collections::BTreeSet<String>, StoreError> {
        media::described_blobs(&self.conn, kind)
    }

    /// The blob ids the [pre-generation gate](crate::media::gate) refused for
    /// `kind` — silent clips, blank images — each of which has a record naming
    /// the value that refused it.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn gated_media_blobs(
        &self,
        kind: MediaKind,
    ) -> Result<std::collections::BTreeSet<String>, StoreError> {
        media::gated_blobs(&self.conn, kind)
    }

    /// Records whose blob is no longer anywhere in `present`. Exposed so a caller
    /// can see what a tree change has orphaned; nothing deletes them implicitly,
    /// because a record is expensive to reproduce and a blob can return.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn orphan_media_records(
        &self,
        present: &std::collections::BTreeSet<String>,
    ) -> Result<Vec<MediaRecord>, StoreError> {
        Ok(self
            .media_records(&MediaFilter::default())?
            .into_iter()
            .filter(|r| !present.contains(&r.blob_id))
            .collect())
    }

    // --- Episodic agent memory (ADR-0013). A separate artifact store, on the
    // same terms as findings and media above: these methods write `agent_memory`
    // and nothing else — they *read* `nodes` to capture and check an anchor, and
    // never write one. So `export_factset` stays a pure function of the tree
    // across every memory write, and nothing an agent remembers can reach the
    // `authored` relevance boost in `search`.
    //
    // These rows are not touched by `rebuild`, following the `imports` precedent:
    // what has no generating function must not be destroyed by a re-derivation.
    // ---

    /// Record one memory, returning its id — the monotonic generation it was
    /// written at.
    ///
    /// The anchor's blob and path, and the `sync_state` tree witness, are
    /// captured **here, from the graph**, so a caller cannot record evidence the
    /// node never carried. An anchor key naming no node is accepted and reads
    /// back as [`crate::AnchorState::Vanished`].
    ///
    /// [`MemoryWrite::supersedes`] records the supersession explicitly, in the
    /// same transaction as the successor: either both land or neither does.
    ///
    /// # Errors
    /// Returns [`MemoryError::InvalidScope`] / [`MemoryError::InvalidBody`] /
    /// [`MemoryError::InvalidConfidence`] for a record that could not be recalled,
    /// [`MemoryError::NotFound`] or [`MemoryError::AlreadySuperseded`] for a bad
    /// supersession target, or [`MemoryError::Store`] on a write failure — in
    /// every case with nothing committed.
    pub fn record_memory(&mut self, write: &MemoryWrite<'_>) -> Result<i64, MemoryError> {
        let tx = self.conn.transaction().map_err(StoreError::from)?;
        let id = memory::record(&tx, write)?;
        tx.commit().map_err(StoreError::from)?;
        Ok(id)
    }

    /// Memory records matching `filter`, newest generation first.
    ///
    /// Live records only unless [`MemoryFilter::include_superseded`] is set: a
    /// superseded record drops out immediately and regardless of age, because the
    /// test is a recorded pointer and not a clock.
    ///
    /// Each record's [`crate::AnchorState`] is computed against the **current**
    /// graph on every call and is never stored.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or [`StoreError::Corrupt`]
    /// if a row carries an unknown kind token.
    pub fn memory_records(
        &self,
        filter: &MemoryFilter<'_>,
    ) -> Result<Vec<MemoryRecord>, StoreError> {
        memory::records(&self.conn, filter)
    }

    /// One memory record by id, or `None` if there is no such record.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or [`StoreError::Corrupt`]
    /// if the row carries an unknown kind token.
    pub fn memory_record(&self, id: i64) -> Result<Option<MemoryRecord>, StoreError> {
        memory::get(&self.conn, id)
    }

    /// A [`MemoryListing`]: the records matching `filter`, plus the whole store's
    /// live and superseded counts, so an empty result is legible as *nothing
    /// matched* rather than *nothing is stored*.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or [`StoreError::Corrupt`]
    /// if a row carries an unknown kind token.
    pub fn memory_listing(&self, filter: &MemoryFilter<'_>) -> Result<MemoryListing, StoreError> {
        let (live, superseded) = memory::counts(&self.conn)?;
        Ok(MemoryListing {
            schema: memory::MEMORY_SCHEMA,
            records: self.memory_records(filter)?,
            live,
            superseded,
        })
    }

    /// **The only way a memory record is ever removed.** Deletes it, and returns
    /// `None` if there was no such record.
    ///
    /// Episodic memory is unbounded and never auto-evicted — no sweep, no TTL, no
    /// capacity bound reaches this table — so an explicit call is the whole
    /// reclamation story. It is also the privacy story: memory has no redaction
    /// chokepoint, so a record that captured a token or a customer name is
    /// removed by asking.
    ///
    /// Anything the deleted record had superseded becomes **live again** and is
    /// named in [`MemoryForgotten::restored`]: leaving it superseded would hide it
    /// on the authority of a record that no longer exists.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on a write failure; the transaction is
    /// rolled back.
    pub fn forget_memory(&mut self, id: i64) -> Result<Option<MemoryForgotten>, StoreError> {
        let tx = self.conn.transaction()?;
        let forgotten = memory::forget(&tx, id)?;
        tx.commit()?;
        Ok(forgotten)
    }

    /// How many memory records are stored, as `(live, superseded)`.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn memory_counts(&self) -> Result<(u64, u64), StoreError> {
        memory::counts(&self.conn)
    }

    /// **Ranked recall**: the live records that match `opts`, scored
    /// `base_confidence × anchor_penalty × decay(age)` and ordered best first.
    ///
    /// Every term is computed **here, at retrieval time, and written to no
    /// column** (ADR-0013). A stored score that decayed would rewrite the store on
    /// every read and would be wrong in between, making recall depend on when you
    /// last looked. Three consequences follow, and each is a promise:
    ///
    /// - **This call mutates nothing.** Recall over an unchanged store and an
    ///   unchanged tree is idempotent, which is what makes
    ///   [`crate::Decay::None`] byte-identical across runs.
    /// - **A superseded record is never returned**, immediately and regardless of
    ///   age: the test is a recorded pointer, not a clock.
    /// - **A record whose anchor no longer resolves is still returned**, demoted
    ///   and labelled. Drift ranks it down; nothing deletes it.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure, or [`StoreError::Corrupt`]
    /// if a row carries an unknown kind token.
    pub fn recall_memory(&self, opts: &RecallOptions<'_>) -> Result<Recall, StoreError> {
        let (live, superseded) = memory::counts(&self.conn)?;
        Ok(Recall {
            schema: memory::RECALL_SCHEMA,
            generation: memory::generation(&self.conn)?,
            decay: opts.decay,
            reproducible: opts.decay.is_reproducible(),
            results: memory::recall(&self.conn, opts)?,
            live,
            superseded,
        })
    }

    // --- The bounded cache tier (ADR-0013, Tier 2). The *opposite* rules to the
    // episodic tier above, because it holds the opposite kind of knowledge:
    // everything here is re-derivable, so eviction costs cycles and never
    // information. Nothing in this section can reach `agent_memory` — it has no
    // `bytes`, no `last_used` and no `hits` for a capacity policy to grip. ---

    /// Write (or replace) one cache entry.
    ///
    /// The payload size is computed here and the anchor's blob is captured from
    /// the graph here, so the sweep can order and total the tier without reading
    /// it, and a caller cannot record evidence a node never carried.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn agent_cache_put(&self, write: &CacheWrite<'_>) -> Result<(), StoreError> {
        memory::cache_put(&self.conn, write)
    }

    /// Read one cache entry back, **recording the access** — `hits` increments and
    /// `last_used` advances.
    ///
    /// This is the one read in the memory store that writes, and what it writes is
    /// the cache's own bookkeeping: those two columns exist to be moved by exactly
    /// this, and a hit counter nothing increments is a column that lies. It
    /// touches nothing outside `agent_cache`, so [`Store::recall_memory`] — the
    /// read whose reproducibility is promised — stays free of it.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn agent_cache_get(&self, key: &str) -> Result<Option<CacheEntry>, StoreError> {
        memory::cache_get(&self.conn, key)
    }

    /// Every cache entry, ordered by key, **without** recording an access:
    /// inspecting a cache is not using it.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn agent_cache_entries(&self) -> Result<Vec<CacheEntry>, StoreError> {
        memory::cache_entries(&self.conn)
    }

    /// Delete one cache entry, returning whether there was one.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on write failure.
    pub fn agent_cache_forget(&self, key: &str) -> Result<bool, StoreError> {
        memory::cache_forget(&self.conn, key)
    }

    /// What the cache tier holds, against `budget_bytes`.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn agent_cache_stats(&self, budget_bytes: u64) -> Result<CacheStats, StoreError> {
        memory::cache_stats(&self.conn, budget_bytes)
    }

    /// **Sweep the cache tier down to `budget_bytes`**, evicting oldest-first on
    /// `(anchor_valid ASC, last_used ASC)`, and advance the generation.
    ///
    /// Called at the maintenance seam (beside `refresh_contexts`) and **never on
    /// the read path**, so an ordinary query never mutates the store. Three things
    /// are never evicted: anything episodic — structurally, there is no column to
    /// grip it by; an entry written in the current generation whose anchor still
    /// applies, which is the session's own work; and the most-recently-used entry,
    /// always, even if it alone exceeds the budget.
    ///
    /// That last pair means a sweep can legitimately finish still over budget.
    /// [`CacheSweep::over_budget`] reports it rather than leaving a bound that
    /// silently failed to bind.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on failure; the transaction is rolled back.
    pub fn sweep_agent_cache(&mut self, budget_bytes: u64) -> Result<CacheSweep, StoreError> {
        let tx = self.conn.transaction()?;
        let swept = memory::cache_sweep(&tx, budget_bytes)?;
        tx.commit()?;
        Ok(swept)
    }

    /// Findings whose owning run no longer exists. Always `0` in a healthy store;
    /// exposed so layer replacement can be asserted to clean up its own records
    /// rather than orphaning them.
    ///
    /// # Errors
    /// Returns [`StoreError::Sqlite`] on query failure.
    pub fn orphan_finding_count(&self) -> Result<u64, StoreError> {
        findings::count_orphan_findings(&self.conn)
    }
}

// --- Free helpers operating on a `Connection` (a `Transaction` derefs to one) ---

fn node_row_id(conn: &Connection, key: &str) -> rusqlite::Result<Option<i64>> {
    conn.query_row("SELECT id FROM nodes WHERE key = ?1", [key], |r| r.get(0))
        .optional()
}

/// Record (or clear) the last-synced `HEAD` tree id. Shared by `rebuild` and
/// `reconcile` so both leave identical `sync_state`.
fn write_sync_state(conn: &Connection, tree: Option<&str>) -> Result<(), StoreError> {
    match tree {
        // Clear `env` on every tree write: it is only valid for the tree a
        // committed `sync` set it against, and that sync re-records it (via
        // `set_sync_env`) immediately after. So a worktree/index sync, or any
        // path that does not re-set it, leaves `env` NULL — reading as "unknown"
        // and forcing the safe full re-extraction next time.
        Some(tree) => conn.execute(
            "INSERT INTO sync_state (id, tree) VALUES (0, ?1)
             ON CONFLICT(id) DO UPDATE SET tree = excluded.tree, env = NULL",
            [tree],
        )?,
        None => conn.execute("DELETE FROM sync_state WHERE id = 0", [])?,
    };
    Ok(())
}

fn upsert_node(conn: &Connection, node: &Node) -> Result<(), StoreError> {
    let meta = serde_json::to_string(&node.meta)?;
    let (span_start, span_end) = match node.span {
        Some(s) => (Some(i64::from(s.start)), Some(i64::from(s.end))),
        None => (None, None),
    };
    conn.execute(
        "INSERT INTO nodes (key, kind, name, path, lang, blob_hash, span_start, span_end, provenance, meta)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
         ON CONFLICT(key) DO UPDATE SET
             kind = excluded.kind, name = excluded.name, path = excluded.path,
             lang = excluded.lang, blob_hash = excluded.blob_hash,
             span_start = excluded.span_start, span_end = excluded.span_end,
             provenance = excluded.provenance, meta = excluded.meta",
        params![
            node.key,
            node.kind.as_str(),
            node.name,
            node.path,
            node.lang,
            node.blob_hash,
            span_start,
            span_end,
            node.provenance.as_str(),
            meta,
        ],
    )?;
    Ok(())
}

fn insert_edge(conn: &Connection, edge: &Edge) -> Result<(), StoreError> {
    validate_edge(edge)?;
    let src_id =
        node_row_id(conn, &edge.src)?.ok_or_else(|| StoreError::UnknownNode(edge.src.clone()))?;
    let dst_id =
        node_row_id(conn, &edge.dst)?.ok_or_else(|| StoreError::UnknownNode(edge.dst.clone()))?;
    insert_edge_row(conn, edge, src_id, dst_id)
}

/// Apply `edge` only if both endpoints already resolve to nodes, returning
/// whether it was **applied** (both endpoints resolved; a duplicate of an
/// existing edge is a harmless no-op via `ON CONFLICT DO NOTHING` but still
/// reports `true`). A missing endpoint returns `false` rather than erroring —
/// the caller prunes such dangling cross-references from the import layer.
fn insert_edge_if_present(conn: &Connection, edge: &Edge) -> Result<bool, StoreError> {
    validate_edge(edge)?;
    let (Some(src_id), Some(dst_id)) =
        (node_row_id(conn, &edge.src)?, node_row_id(conn, &edge.dst)?)
    else {
        return Ok(false);
    };
    insert_edge_row(conn, edge, src_id, dst_id)?;
    Ok(true)
}

/// Apply `edges`, keeping those whose endpoints resolve and pruning the rest.
/// Returns the kept edges plus the applied/pruned counts (in an [`ImportApplied`]
/// whose `layers`/`nodes` are left zero for the caller to fill).
fn apply_edges_pruning(
    conn: &Connection,
    edges: &[Edge],
) -> Result<(Vec<Edge>, ImportApplied), StoreError> {
    let mut kept = Vec::with_capacity(edges.len());
    let mut counts = ImportApplied::default();
    for edge in edges {
        if insert_edge_if_present(conn, edge)? {
            kept.push(edge.clone());
            counts.edges_applied += 1;
        } else {
            counts.edges_pruned += 1;
        }
    }
    Ok((kept, counts))
}

/// Upsert a persisted import layer row. Free helper so it can run inside the same
/// transaction as an apply/prune pass.
fn put_import_row(conn: &Connection, src_ref: &str, facts: &FactSet) -> Result<(), StoreError> {
    let json = serde_json::to_string(facts)?;
    conn.execute(
        "INSERT INTO imports (src_ref, facts) VALUES (?1, ?2)
         ON CONFLICT(src_ref) DO UPDATE SET facts = excluded.facts, imported_at = datetime('now')",
        params![src_ref, json],
    )?;
    Ok(())
}

/// The provenance/confidence invariant guard shared by the strict and tolerant
/// edge inserts.
fn validate_edge(edge: &Edge) -> Result<(), StoreError> {
    if edge.is_valid() {
        Ok(())
    } else {
        Err(StoreError::InvalidEdge(format!(
            "confidence must be present iff provenance is inferred (src={}, dst={})",
            edge.src, edge.dst
        )))
    }
}

/// Insert an edge row given already-resolved endpoint ids. Edges are a set: a
/// duplicate `(src, dst, kind, provenance)` is a no-op via `ON CONFLICT … DO
/// NOTHING`, so re-applying a fact set never accumulates duplicates. Other
/// constraint violations (guarded in Rust above) still surface.
fn insert_edge_row(
    conn: &Connection,
    edge: &Edge,
    src_id: i64,
    dst_id: i64,
) -> Result<(), StoreError> {
    conn.execute(
        "INSERT INTO edges (src, dst, kind, provenance, confidence, src_ref)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)
         ON CONFLICT(src, dst, kind, provenance) DO NOTHING",
        params![
            src_id,
            dst_id,
            edge.kind.as_str(),
            edge.provenance.as_str(),
            edge.confidence,
            edge.src_ref,
        ],
    )?;
    Ok(())
}

/// A hashable identity for an edge over **all** its fields — used by
/// [`Store::reconcile`] to diff the edge set. A tuple (not a delimiter-joined
/// string) so no field value can be confused with a separator: node keys embed
/// git paths, which may legally contain any byte (including control characters),
/// so a joined string could collapse distinct edges to one identity and drop an
/// edge. Confidence is compared by its exact bit pattern (`f64::to_bits`, wrapped
/// in `Option` so `None` and `Some(_)` stay distinct), the only non-`Eq` field.
fn edge_identity(edge: &Edge) -> EdgeId {
    (
        edge.src.clone(),
        edge.dst.clone(),
        edge.kind.as_str().to_owned(),
        edge.provenance.as_str().to_owned(),
        edge.confidence.map(f64::to_bits),
        edge.src_ref.clone(),
    )
}

/// The tuple form of an edge's full-field identity (see [`edge_identity`]):
/// `(src, dst, kind, provenance, confidence-bits, src_ref)`.
type EdgeId = (String, String, String, String, Option<u64>, Option<String>);

/// Delete the edge row identified by `(src, dst, kind, provenance)` — the table's
/// unique key — resolving the endpoint node keys to ids. A no-op if absent.
fn delete_edge(conn: &Connection, edge: &Edge) -> Result<(), StoreError> {
    conn.execute(
        "DELETE FROM edges
         WHERE src = (SELECT id FROM nodes WHERE key = ?1)
           AND dst = (SELECT id FROM nodes WHERE key = ?2)
           AND kind = ?3 AND provenance = ?4",
        params![
            edge.src,
            edge.dst,
            edge.kind.as_str(),
            edge.provenance.as_str()
        ],
    )?;
    Ok(())
}

fn collect_nodes(rows: &mut rusqlite::Rows) -> Result<Vec<Node>, StoreError> {
    let mut out = Vec::new();
    while let Some(row) = rows.next()? {
        out.push(row_to_node(row)?);
    }
    Ok(out)
}

fn collect_edges(rows: &mut rusqlite::Rows) -> Result<Vec<Edge>, StoreError> {
    let mut out = Vec::new();
    while let Some(row) = rows.next()? {
        out.push(row_to_edge(row)?);
    }
    Ok(out)
}

fn row_to_node(row: &rusqlite::Row) -> Result<Node, StoreError> {
    let kind: String = row.get("kind")?;
    let span_start: Option<i64> = row.get("span_start")?;
    let span_end: Option<i64> = row.get("span_end")?;
    let span = match (span_start, span_end) {
        (Some(s), Some(e)) => Some(Span::new(to_u32(s)?, to_u32(e)?)),
        _ => None,
    };
    let meta: String = row.get("meta")?;
    let provenance: String = row.get("provenance")?;
    let provenance = Provenance::from_token(&provenance)
        .ok_or_else(|| StoreError::Corrupt(format!("unknown node provenance: {provenance}")))?;
    Ok(Node {
        key: row.get("key")?,
        kind: NodeKind::from_token(&kind),
        name: row.get("name")?,
        path: row.get("path")?,
        lang: row.get("lang")?,
        blob_hash: row.get("blob_hash")?,
        span,
        provenance,
        meta: serde_json::from_str(&meta)?,
    })
}

fn row_to_edge(row: &rusqlite::Row) -> Result<Edge, StoreError> {
    let kind: String = row.get("kind")?;
    let provenance: String = row.get("provenance")?;
    let provenance = Provenance::from_token(&provenance)
        .ok_or_else(|| StoreError::Corrupt(format!("unknown provenance: {provenance}")))?;
    Ok(Edge {
        src: row.get("src")?,
        dst: row.get("dst")?,
        kind: EdgeKind::from_token(&kind),
        provenance,
        confidence: row.get("confidence")?,
        src_ref: row.get("src_ref")?,
    })
}

fn to_u32(v: i64) -> Result<u32, StoreError> {
    u32::try_from(v).map_err(|_| StoreError::Corrupt(format!("span offset out of range: {v}")))
}

/// The true provenance of an import-layer node, from its key namespace: Graphify
/// nodes (`graphify:`) are [`Provenance::Inferred`]; every other import node (lat,
/// …) is [`Provenance::Authored`]. Import-layer nodes are never derived, so this
/// is used to repair a legacy `Derived` tag on load (see `load_import_layers`).
fn import_node_provenance(key: &str) -> Provenance {
    if key.starts_with("graphify:") {
        Provenance::Inferred
    } else {
        Provenance::Authored
    }
}

#[cfg(test)]
mod tests {
    use super::Store;
    use crate::model::{Direction, Edge, EdgeKind, FactSet, Node, NodeKind, Span};
    use crate::provenance::Provenance;

    fn sample_node(key: &str) -> Node {
        Node {
            key: key.to_owned(),
            kind: NodeKind::Fn,
            name: "sample".to_owned(),
            path: Some("src/lib.rs".to_owned()),
            lang: Some("rust".to_owned()),
            blob_hash: Some("deadbeef".to_owned()),
            span: Some(Span::new(10, 42)),
            provenance: Provenance::Derived,
            meta: serde_json::json!({"vis": "pub"}),
        }
    }

    #[test]
    fn reconcile_matches_a_full_rebuild() {
        // reconcile must leave the store identical to a fresh rebuild, across an
        // add, a remove, a content change, and edge churn.
        let node = |k: &str, name: &str| {
            let mut n = sample_node(k);
            n.name = name.to_owned();
            n
        };
        let edge =
            |src: &str, dst: &str| Edge::derived(src.to_owned(), dst.to_owned(), EdgeKind::Calls);
        // An inferred edge carries confidence and a src_ref — exercise both so the
        // equivalence claim covers every edge field, not just derived calls.
        let inferred = |src: &str, dst: &str, conf: f64| {
            let mut e = Edge::inferred(src.to_owned(), dst.to_owned(), EdgeKind::Related, conf);
            e.src_ref = Some("import:demo".to_owned());
            e
        };

        let facts1 = FactSet {
            nodes: vec![node("a", "A"), node("b", "B"), node("c", "C")],
            edges: vec![edge("a", "b"), edge("b", "c"), inferred("a", "c", 0.7)],
        };
        // b changes (name), c is removed, d is added; edge b->c drops, a->d added,
        // and the inferred edge's confidence changes.
        let facts2 = FactSet {
            nodes: vec![node("a", "A"), node("b", "B2"), node("d", "D")],
            edges: vec![edge("a", "b"), edge("a", "d"), inferred("a", "d", 0.9)],
        };

        // Path 1: rebuild facts1, then reconcile to facts2.
        let mut reconciled = Store::open_in_memory().expect("open");
        reconciled.rebuild(&facts1, Some("t1")).expect("rebuild");
        reconciled
            .reconcile(&facts2, Some("t2"))
            .expect("reconcile");

        // Path 2: a fresh full rebuild of facts2.
        let mut rebuilt = Store::open_in_memory().expect("open");
        rebuilt.rebuild(&facts2, Some("t2")).expect("rebuild");

        let canon = |fs: FactSet| {
            let mut nodes = fs.nodes;
            nodes.sort_by(|a, b| a.key.cmp(&b.key));
            let mut edges: Vec<String> = fs
                .edges
                .iter()
                .map(|e| {
                    format!(
                        "{}\0{}\0{}\0{}\0{:?}\0{:?}",
                        e.kind.as_str(),
                        e.src,
                        e.dst,
                        e.provenance.as_str(),
                        e.confidence,
                        e.src_ref
                    )
                })
                .collect();
            edges.sort();
            (nodes, edges)
        };
        assert_eq!(
            canon(reconciled.export_factset().expect("export")),
            canon(rebuilt.export_factset().expect("export")),
            "reconcile must match a full rebuild",
        );
        assert_eq!(
            reconciled.sync_state().expect("state").as_deref(),
            Some("t2")
        );
    }

    /// The worktree stamp (issue #330) identifies the *store*, not the tree: an
    /// unstamped store reads as "unknown" and is adopted rather than rebuilt, and
    /// a new tree state must not clear the stamp the way it clears `env`.
    #[test]
    fn the_worktree_stamp_survives_tree_writes_and_starts_unknown() {
        let mut store = Store::open_in_memory().expect("open");
        let facts = FactSet::new().with_node(sample_node("sym:a"));

        // Never synced ⇒ no stamp. A legacy store reads the same way, which is
        // why "unknown" must mean adopt, not mismatch.
        assert_eq!(store.synced_worktree().expect("stamp"), None);

        store.reconcile(&facts, Some("t1")).expect("reconcile");
        assert_eq!(
            store.synced_worktree().expect("stamp"),
            None,
            "writing a tree does not invent a stamp; the sync engine records it"
        );

        store.set_synced_worktree("/w/one").expect("stamp");
        store.set_sync_env("env-1").expect("env");
        assert_eq!(
            store.synced_worktree().expect("stamp").as_deref(),
            Some("/w/one")
        );

        // A later tree write clears `env` (it is only valid for one tree) but must
        // KEEP the stamp: a new commit does not move the graph to another tree.
        store.reconcile(&facts, Some("t2")).expect("reconcile");
        assert_eq!(store.sync_env().expect("env"), None, "env is tree-scoped");
        assert_eq!(
            store.synced_worktree().expect("stamp").as_deref(),
            Some("/w/one"),
            "the stamp identifies the store, not the tree, so it must survive"
        );

        // Re-stamping replaces it (the store was adopted by another tree).
        store.set_synced_worktree("/w/two").expect("restamp");
        assert_eq!(
            store.synced_worktree().expect("stamp").as_deref(),
            Some("/w/two")
        );

        // Clearing the synced state clears the stamp with it: a store with no
        // recorded tree describes no working tree either.
        store.rebuild(&facts, None).expect("rebuild unstated");
        assert_eq!(store.sync_state().expect("state"), None);
        assert_eq!(store.synced_worktree().expect("stamp"), None);
    }

    #[test]
    fn reconcile_writes_only_the_edge_delta() {
        // An unchanged edge must keep its row (proving reconcile does not wipe and
        // reinsert the whole edge set); a removed edge's row goes; a new edge's row
        // appears. Row identity is the SQLite `rowid` — stable unless deleted.
        let n = |k: &str| sample_node(k);
        let e =
            |src: &str, dst: &str| Edge::derived(src.to_owned(), dst.to_owned(), EdgeKind::Calls);

        let mut store = Store::open_in_memory().expect("open");
        store
            .rebuild(
                &FactSet {
                    nodes: vec![n("a"), n("b"), n("c")],
                    edges: vec![e("a", "b"), e("b", "c")],
                },
                None,
            )
            .expect("rebuild");

        // Map (src_key, dst_key) → rowid via the private connection.
        let rowids = |store: &Store| -> std::collections::HashMap<(String, String), i64> {
            let mut stmt = store
                .conn
                .prepare(
                    "SELECT ns.key, nd.key, e.rowid FROM edges e \
                     JOIN nodes ns ON ns.id = e.src JOIN nodes nd ON nd.id = e.dst",
                )
                .expect("prepare");
            stmt.query_map([], |r| {
                Ok((
                    (r.get::<_, String>(0)?, r.get::<_, String>(1)?),
                    r.get::<_, i64>(2)?,
                ))
            })
            .expect("query")
            .map(Result::unwrap)
            .collect()
        };

        let before = rowids(&store);
        let ab_rowid = before[&("a".to_owned(), "b".to_owned())];

        // Keep a->b, drop b->c, add a->c.
        store
            .reconcile(
                &FactSet {
                    nodes: vec![n("a"), n("b"), n("c")],
                    edges: vec![e("a", "b"), e("a", "c")],
                },
                None,
            )
            .expect("reconcile");

        let after = rowids(&store);
        assert_eq!(
            after.get(&("a".to_owned(), "b".to_owned())),
            Some(&ab_rowid),
            "the unchanged edge keeps its row (not rewritten)"
        );
        assert!(
            !after.contains_key(&("b".to_owned(), "c".to_owned())),
            "the removed edge's row is gone"
        );
        assert!(
            after.contains_key(&("a".to_owned(), "c".to_owned())),
            "the added edge has a new row"
        );
    }

    #[test]
    fn reconcile_is_history_independent_for_edge_queries() {
        // The whole point of the edge delta: it must be invisible above storage.
        // A store reached by rebuild(f1)+reconcile(f2) has different edge row ids
        // than a cold rebuild at f2, yet every edge query must return byte-for-byte
        // the same result — order included — because the queries are content-ordered.
        let n = |k: &str| sample_node(k);
        let d =
            |src: &str, dst: &str| Edge::derived(src.to_owned(), dst.to_owned(), EdgeKind::Calls);
        let inf = |src: &str, dst: &str, c: f64| {
            Edge::inferred(src.to_owned(), dst.to_owned(), EdgeKind::Related, c)
        };
        let f1 = FactSet {
            nodes: vec![n("a"), n("b"), n("c")],
            edges: vec![d("a", "b"), d("b", "c"), inf("a", "c", 0.7)],
        };
        let f2 = FactSet {
            nodes: vec![n("a"), n("b"), n("d")],
            edges: vec![d("a", "b"), d("a", "d"), inf("a", "d", 0.9)],
        };

        let mut incremental = Store::open_in_memory().expect("open");
        incremental.rebuild(&f1, None).expect("rebuild");
        incremental.reconcile(&f2, None).expect("reconcile");
        let mut cold = Store::open_in_memory().expect("open");
        cold.rebuild(&f2, None).expect("rebuild");

        // Project to all fields so the comparison covers order *and* content.
        let proj = |es: Vec<Edge>| -> Vec<String> {
            es.into_iter()
                .map(|e| {
                    format!(
                        "{}|{}|{}|{}|{:?}|{:?}",
                        e.src,
                        e.dst,
                        e.kind.as_str(),
                        e.provenance.as_str(),
                        e.confidence,
                        e.src_ref
                    )
                })
                .collect()
        };

        for key in ["a", "b", "d"] {
            assert_eq!(
                proj(incremental.edges_from(key).expect("from")),
                proj(cold.edges_from(key).expect("from")),
                "edges_from({key}) must match a cold rebuild"
            );
            assert_eq!(
                proj(incremental.edges_to(key).expect("to")),
                proj(cold.edges_to(key).expect("to")),
                "edges_to({key}) must match a cold rebuild"
            );
        }
        assert_eq!(
            proj(incremental.all_edges().expect("all")),
            proj(cold.all_edges().expect("all")),
            "all_edges must match a cold rebuild"
        );
        for p in [Provenance::Derived, Provenance::Inferred] {
            assert_eq!(
                proj(incremental.edges_by_provenance(p).expect("prov")),
                proj(cold.edges_by_provenance(p).expect("prov")),
                "edges_by_provenance({}) must match a cold rebuild",
                p.as_str()
            );
        }
    }

    #[test]
    fn reconcile_updates_confidence_on_an_unchanged_tuple() {
        // A change to *only* an edge's confidence — same (src, dst, kind,
        // provenance) — must still be applied. Edge identity includes confidence,
        // so it is delete+add (matching a full rebuild), not the insert-time
        // `DO NOTHING` that would leave the stale confidence in place.
        let n = |k: &str| sample_node(k);
        let inf = |c: f64| {
            let mut e = Edge::inferred("a".to_owned(), "b".to_owned(), EdgeKind::Related, c);
            e.src_ref = Some("import:demo".to_owned());
            e
        };

        let mut store = Store::open_in_memory().expect("open");
        store
            .rebuild(
                &FactSet {
                    nodes: vec![n("a"), n("b")],
                    edges: vec![inf(0.5)],
                },
                None,
            )
            .expect("rebuild");
        store
            .reconcile(
                &FactSet {
                    nodes: vec![n("a"), n("b")],
                    edges: vec![inf(0.9)],
                },
                None,
            )
            .expect("reconcile");

        let edges = store.edges_from("a").expect("edges");
        assert_eq!(edges.len(), 1);
        assert_eq!(
            edges[0].confidence,
            Some(0.9),
            "confidence updated, not left stale"
        );
    }

    #[test]
    fn edge_identity_does_not_collide_across_field_boundaries() {
        // Node keys embed git paths, which may contain any byte — including the
        // unit separator (`\x1f`). A delimiter-joined identity would map these two
        // distinct edges to the same string (`a\x1fb\x1fc\x1f…`); the tuple identity
        // must keep them apart, or reconcile would drop one edge as a "duplicate".
        let e1 = super::edge_identity(&Edge::derived(
            "a\u{1f}b".to_owned(),
            "c".to_owned(),
            EdgeKind::Calls,
        ));
        let e2 = super::edge_identity(&Edge::derived(
            "a".to_owned(),
            "b\u{1f}c".to_owned(),
            EdgeKind::Calls,
        ));
        assert_ne!(
            e1, e2,
            "control chars in a key must not collapse identities"
        );

        // A confidence-only difference (same tuple otherwise) also stays distinct,
        // and `None` (derived) never equals `Some(0.0)`.
        let derived = super::edge_identity(&Edge::derived(
            "a".to_owned(),
            "b".to_owned(),
            EdgeKind::Related,
        ));
        let inferred0 = super::edge_identity(&Edge::inferred(
            "a".to_owned(),
            "b".to_owned(),
            EdgeKind::Related,
            0.0,
        ));
        assert_ne!(derived, inferred0, "None vs Some(0.0) confidence differ");
    }

    #[test]
    fn open_in_memory_applies_schema() {
        let store = Store::open_in_memory().expect("open");
        assert_eq!(store.node_count().expect("count"), 0);
        // Written against `latest_version()` rather than the literal that stood
        // here (8 for analyzer findings, then 9, 10, 11 as the media and
        // agent-memory tables landed). The literal was defended as making someone
        // confirm a new migration is meant to apply on open — but it could not do
        // that job: `apply` applies every migration not already recorded, so
        // "applies on open" is not a per-migration choice there is anything to
        // confirm. What the literal actually asserted was the value of a shared
        // constant, which every future migration then has to come here and edit,
        // in a file it otherwise has no business in — the brittleness #329
        // replaced with a property test elsewhere. This is the idiom
        // `migrations::tests::a_later_migration_is_additive_on_a_populated_store`
        // already uses, for the same reason.
        //
        // Since migrations are selected by set membership, the property is now
        // strictly stronger than "the biggest recorded number is N":
        // `schema_version` is the highest **gap-free** version, so equality with
        // `latest_version` also rules out a store that skipped one and stamped a
        // higher one. A fresh store cannot exhibit that gap, so
        // `reopening_repairs_a_skipped_migration` exercises it directly.
        assert_eq!(
            store.schema_version().expect("version"),
            crate::migrations::latest_version(),
            "opening a store must apply every known migration, with no gaps"
        );
    }

    /// Reopening a store that is **missing a lower-numbered migration while
    /// carrying a higher one** repairs it — through the real public API, which
    /// is where it matters to callers.
    ///
    /// This is not a hypothetical: it is the shape `main` itself carries.
    /// `feat/stage25-memory-recall` merged migration **13** while migration
    /// **12** was still on this branch, so every store opened by a current-`main`
    /// build — including this repository's own `.git/roteiro/graph.db` and each
    /// linked worktree's — records `1..11, 13`. Under the old
    /// `version > MAX(recorded)` rule, `12 > 13` is false and migration 12 would
    /// never be applied to any of them, permanently: the store opens cleanly,
    /// reports a schema it does not have, and `synced_worktree()` fails with
    /// `no such column: worktree` — the issue #330 tree stamp, silently absent.
    ///
    /// The two migrations are deliberately named by number rather than derived
    /// from `latest_version()`. Which one is missing and which is present is the
    /// specific historical fact being tested; arithmetic on the newest version
    /// would quietly re-aim the test at whatever lands next and stop covering
    /// this.
    #[test]
    fn reopening_repairs_a_skipped_migration() {
        /// The migration this branch adds — absent from a current-`main` store.
        const SKIPPED: u32 = 12;
        /// The migration `main` added in parallel — present, and stamped higher.
        const AHEAD: u32 = 13;

        let path = std::env::temp_dir().join(format!(
            "roteiro-migration-gap-{}-{:?}.db",
            std::process::id(),
            std::thread::current().id()
        ));
        std::fs::remove_file(&path).ok();

        {
            let mut store = Store::open(&path).expect("open");
            // Reconcile rather than a bare upsert: it records a synced tree, so
            // `sync_state` has the row the worktree stamp updates. Without one,
            // `set_synced_worktree` is a documented no-op and would prove nothing.
            let facts = FactSet::new().with_node(sample_node("kept"));
            store
                .reconcile(&facts, Some("t1"))
                .expect("seed graph and sync state");
        }
        // Rewind to exactly what a current-`main` build leaves behind: migration
        // 12's column and record gone, 13's schema and record intact.
        {
            let conn = rusqlite::Connection::open(&path).expect("reopen raw");
            conn.execute_batch("ALTER TABLE sync_state DROP COLUMN worktree;")
                .expect("undo migration 12");
            conn.execute(
                "DELETE FROM schema_migrations WHERE version = ?1",
                [SKIPPED],
            )
            .expect("unrecord 12");
            // Sanity: the store really is in the damaged shape, 13 and all.
            let recorded: i64 = conn
                .query_row(
                    "SELECT COUNT(*) FROM schema_migrations WHERE version = ?1",
                    [AHEAD],
                    |r| r.get(0),
                )
                .expect("count 13");
            assert_eq!(recorded, 1, "migration {AHEAD} must still be recorded");
        }
        // Migration 13's own state, captured before the repair so it can be shown
        // untouched afterwards — a repair that healed the gap by disturbing the
        // other branch's tables would be no better than the bug.
        let clock_before = cache_clock(&path);

        // Before the repair the store under-reports rather than over-reports: it
        // claims the last gap-free version, never the higher stamped one.
        {
            let damaged = rusqlite::Connection::open(&path).expect("reopen raw");
            assert_eq!(
                crate::migrations::store_version(&damaged).expect("version"),
                SKIPPED - 1,
                "a gapped store must report the version it actually provides, \
                 not the maximum recorded ({AHEAD})"
            );
        }

        // Reopening through the public API repairs the gap…
        let store = Store::open(&path).expect("reopen");
        assert_eq!(
            store.schema_version().expect("version"),
            crate::migrations::latest_version(),
            "1..={AHEAD} is contiguous once the skipped migration is applied"
        );
        // …migration 12's schema is really back, which is what the caller
        // depends on — recorded-but-absent would be the same bug in a new
        // costume…
        assert!(
            store.synced_worktree().is_ok(),
            "the repaired migration's column must exist, not merely be recorded"
        );
        store
            .set_synced_worktree("/w/repaired")
            .expect("the stamp must be writable, not just readable");
        assert_eq!(
            store.synced_worktree().expect("stamp").as_deref(),
            Some("/w/repaired")
        );
        // …migration 13's tables are untouched, clock row and all…
        assert_eq!(
            cache_clock(&path),
            clock_before,
            "the other branch's seeded `agent_cache_clock` row must survive the \
             repair unchanged"
        );
        {
            let conn = rusqlite::Connection::open(&path).expect("reopen raw");
            let rows: i64 = conn
                .query_row("SELECT COUNT(*) FROM agent_cache", [], |r| r.get(0))
                .expect("agent_cache must still exist");
            assert_eq!(rows, 0, "`agent_cache` is present and empty, not recreated");
        }
        // …and no data was lost.
        assert!(store.get_node("kept").expect("get").is_some());
        assert_eq!(store.node_count().expect("count"), 1);

        std::fs::remove_file(&path).expect("cleanup");
    }

    /// Migration 13's single `agent_cache_clock` row as `(ticks, generation)`,
    /// read raw so this test does not depend on the recall API another branch
    /// owns.
    fn cache_clock(path: &std::path::Path) -> (i64, i64) {
        let conn = rusqlite::Connection::open(path).expect("open raw");
        conn.query_row(
            "SELECT ticks, generation FROM agent_cache_clock WHERE id = 0",
            [],
            |r| Ok((r.get(0)?, r.get(1)?)),
        )
        .expect("the seeded clock row must exist")
    }

    /// Record `version` in `store`'s migration table as though a build that knew
    /// that migration had run it — the only way to manufacture a store from the
    /// future without a time machine.
    ///
    /// Only the *record* is written, not any schema the migration would have
    /// added. That is exactly the position an older binary is in: it can see the
    /// stamp and cannot see, or reason about, the shape.
    fn stamp_version(store: &Store, version: u32) {
        store
            .conn
            .execute(
                "INSERT INTO schema_migrations (version) VALUES (?1)",
                [version],
            )
            .expect("stamp a version this build does not know");
    }

    #[test]
    fn a_store_this_build_wrote_is_not_ahead_of_it() {
        let store = Store::open_in_memory().expect("open");
        assert_eq!(
            store.schema_ahead().expect("schema_ahead"),
            None,
            "a store this very build just migrated cannot be ahead of it"
        );
    }

    /// A store carrying migrations this build has never heard of is reported
    /// ahead, and the report names **both** sides plus what to do about it.
    ///
    /// Versions are `latest_version() + n` rather than literals: the property is
    /// "beyond what this build knows", and a literal would silently stop testing
    /// that the day a real migration reached the same number.
    #[test]
    fn a_store_stamped_beyond_this_build_is_reported_ahead() {
        let build = Store::build_schema_version();
        let store = Store::open_in_memory().expect("open");
        stamp_version(&store, build + 1);
        stamp_version(&store, build + 2);

        let ahead = store
            .schema_ahead()
            .expect("schema_ahead")
            .expect("a store two migrations beyond this build must be reported");
        assert_eq!(ahead.build_version(), build);
        assert_eq!(
            ahead.store_version(),
            build + 2,
            "the store's version is the highest it records, not the first \
             unknown one"
        );
        assert_eq!(ahead.unknown_versions(), [build + 1, build + 2]);

        // The message has to be actionable, not merely correct: both versions by
        // number, and the fix (upgrade the binary — never "delete the store").
        let message = ahead.to_string();
        for expected in [
            &format!("{}", build + 2),
            &format!("{build}"),
            &"pgrade".to_owned(),
        ] {
            assert!(
                message.contains(expected.as_str()),
                "the refusal must name `{expected}`: {message}"
            );
        }
    }

    /// A **gap** — a store missing a lower migration while carrying a higher one
    /// this build knows — is not a store from the future, and must not be
    /// reported as one. Conflating them would name a version the store is not
    /// at, in a message telling the reader to upgrade a binary that is already
    /// new enough.
    ///
    /// This is the shape `main` itself carried (see
    /// `reopening_repairs_a_skipped_migration`, which proves `Store::open`
    /// repairs it), so it is a live case, not a hypothetical.
    ///
    /// The second half is the one that matters: a store that is *both* gapped
    /// and from the future must be described only by the half the reader can act
    /// on. Naming the gap would tell someone to upgrade a binary that is already
    /// new enough for it.
    #[test]
    fn a_gap_below_this_build_is_not_a_store_from_the_future() {
        let build = Store::build_schema_version();

        // A gap and nothing else: behind this build, so never ahead of it.
        let gapped = Store::open_in_memory().expect("open");
        gapped
            .conn
            .execute("DELETE FROM schema_migrations WHERE version = ?1", [build])
            .expect("open a gap");
        assert!(
            gapped.schema_version().expect("version") < build,
            "the gapped store must under-report, or this test proves nothing"
        );
        assert_eq!(
            gapped.schema_ahead().expect("schema_ahead"),
            None,
            "a store *missing* a migration is behind this build, never ahead"
        );

        // A gap *and* a version from the future: reported ahead, and reported
        // only in terms of the future version.
        stamp_version(&gapped, build + 1);
        let ahead = gapped
            .schema_ahead()
            .expect("schema_ahead")
            .expect("the unknown migration is still unknown, gap or no gap");
        assert_eq!(ahead.unknown_versions(), [build + 1]);
        assert_eq!(
            ahead.store_version(),
            build + 1,
            "the gap is a separate condition and must not colour this report"
        );
    }

    /// The reason the guard compares the recorded **set** and not
    /// [`Store::schema_version`].
    ///
    /// `schema_version` is the highest *gap-free* version — a floor for readers.
    /// A store recorded `1..=build, build + 2` therefore reports `build`, and a
    /// `schema_version() > build` test sees a perfectly ordinary store, while
    /// migration `build + 2`'s schema is in the file and a newer binary plainly
    /// assembled the graph. That is issue #342 surviving the check written to
    /// catch it, so it is asserted directly rather than trusted.
    #[test]
    fn the_gap_free_version_alone_would_miss_a_store_from_the_future() {
        let build = Store::build_schema_version();
        let store = Store::open_in_memory().expect("open");
        stamp_version(&store, build + 2); // note: `build + 1` deliberately absent

        assert_eq!(
            store.schema_version().expect("version"),
            build,
            "the gap-free version stops at the last contiguous migration, so it \
             cannot see the one above the hole"
        );
        let ahead = store
            .schema_ahead()
            .expect("schema_ahead")
            .expect("the set difference sees what the contiguity walk cannot");
        assert_eq!(ahead.store_version(), build + 2);
        assert_eq!(ahead.unknown_versions(), [build + 2]);
    }

    #[test]
    fn upsert_and_get_round_trips_all_fields() {
        let store = Store::open_in_memory().expect("open");
        let node = sample_node("sym:rust:src/lib.rs#sample");
        store.upsert_node(&node).expect("upsert");
        let got = store.get_node(&node.key).expect("get").expect("present");
        assert_eq!(got, node);
    }

    #[test]
    fn upsert_updates_in_place() {
        let store = Store::open_in_memory().expect("open");
        let mut node = sample_node("k");
        store.upsert_node(&node).expect("insert");
        node.name = "renamed".to_owned();
        node.kind = NodeKind::Struct;
        store.upsert_node(&node).expect("update");
        assert_eq!(store.node_count().expect("count"), 1);
        let got = store.get_node("k").expect("get").expect("present");
        assert_eq!(got.name, "renamed");
        assert_eq!(got.kind, NodeKind::Struct);
    }

    #[test]
    fn edge_with_unknown_endpoint_is_rejected() {
        let store = Store::open_in_memory().expect("open");
        store
            .upsert_node(&Node::new("a", NodeKind::Fn, "a"))
            .expect("a");
        let edge = Edge::derived("a", "missing", EdgeKind::Calls);
        let err = store.insert_edge(&edge).expect_err("should reject");
        assert!(matches!(err, super::StoreError::UnknownNode(k) if k == "missing"));
    }

    #[test]
    fn inferred_edge_requires_confidence() {
        let store = Store::open_in_memory().expect("open");
        store
            .upsert_node(&Node::new("a", NodeKind::Fn, "a"))
            .expect("a");
        store
            .upsert_node(&Node::new("b", NodeKind::Fn, "b"))
            .expect("b");
        // Hand-build an inferred edge with no confidence to violate the invariant.
        let bad = Edge {
            src: "a".to_owned(),
            dst: "b".to_owned(),
            kind: EdgeKind::References,
            provenance: Provenance::Inferred,
            confidence: None,
            src_ref: None,
        };
        assert!(matches!(
            store.insert_edge(&bad).expect_err("reject"),
            super::StoreError::InvalidEdge(_)
        ));
    }

    #[test]
    fn apply_factset_is_atomic() {
        let mut store = Store::open_in_memory().expect("open");
        // Second edge references a missing node, so the whole set must roll back.
        let facts = FactSet::new()
            .with_node(Node::new("a", NodeKind::Fn, "a"))
            .with_node(Node::new("b", NodeKind::Fn, "b"))
            .with_edge(Edge::derived("a", "b", EdgeKind::Calls))
            .with_edge(Edge::derived("a", "ghost", EdgeKind::Calls));
        assert!(store.apply_factset(&facts).is_err());
        assert_eq!(store.node_count().expect("count"), 0, "rolled back");
        assert_eq!(store.edge_count().expect("count"), 0, "rolled back");
    }

    #[test]
    fn neighbors_and_provenance_queries() {
        let mut store = Store::open_in_memory().expect("open");
        let facts = FactSet::new()
            .with_node(Node::new("a", NodeKind::Fn, "a"))
            .with_node(Node::new("b", NodeKind::Fn, "b"))
            .with_node(Node::new("c", NodeKind::Fn, "c"))
            .with_edge(Edge::derived("a", "b", EdgeKind::Calls))
            .with_edge(Edge::inferred("a", "c", EdgeKind::References, 0.5));
        store.apply_factset(&facts).expect("apply");

        let out = store.neighbors("a", Direction::Outgoing).expect("out");
        let mut keys: Vec<_> = out.iter().map(|n| n.key.clone()).collect();
        keys.sort();
        assert_eq!(keys, ["b", "c"]);

        assert!(
            store
                .neighbors("b", Direction::Outgoing)
                .expect("b out")
                .is_empty()
        );
        assert_eq!(
            store
                .neighbors("b", Direction::Incoming)
                .expect("b in")
                .len(),
            1
        );

        let inferred = store
            .edges_by_provenance(Provenance::Inferred)
            .expect("inf");
        assert_eq!(inferred.len(), 1);
        assert_eq!(inferred[0].confidence, Some(0.5));
    }

    #[test]
    fn neighbors_of_absent_node_is_empty() {
        let store = Store::open_in_memory().expect("open");
        assert!(
            store
                .neighbors("nope", Direction::Both)
                .expect("q")
                .is_empty()
        );
    }

    #[test]
    fn get_missing_node_is_none() {
        let store = Store::open_in_memory().expect("open");
        assert!(store.get_node("absent").expect("get").is_none());
    }

    #[test]
    fn nodes_by_kind_and_edges_to() {
        let mut store = Store::open_in_memory().expect("open");
        let facts = FactSet::new()
            .with_node(Node::new("f1", NodeKind::Fn, "f1"))
            .with_node(Node::new("f2", NodeKind::Fn, "f2"))
            .with_node(Node::new("s1", NodeKind::Struct, "s1"))
            .with_edge(Edge::derived("f1", "s1", EdgeKind::References))
            .with_edge(Edge::derived("f2", "s1", EdgeKind::References));
        store.apply_factset(&facts).expect("apply");

        let fns = store.nodes_by_kind(&NodeKind::Fn).expect("fns");
        assert_eq!(
            fns.iter().map(|n| n.key.as_str()).collect::<Vec<_>>(),
            ["f1", "f2"]
        );
        assert!(
            store
                .nodes_by_kind(&NodeKind::Enum)
                .expect("enums")
                .is_empty()
        );

        let into_s1 = store.edges_to("s1").expect("edges_to");
        assert_eq!(into_s1.len(), 2);
        assert!(into_s1.iter().all(|e| e.dst == "s1"));
    }

    #[test]
    fn open_persists_across_reopen() {
        let path =
            std::env::temp_dir().join(format!("roteiro-open-test-{}.db", std::process::id()));
        std::fs::remove_file(&path).ok();
        {
            let store = Store::open(&path).expect("open");
            store
                .upsert_node(&sample_node("persisted"))
                .expect("upsert");
        }
        {
            let store = Store::open(&path).expect("reopen");
            assert_eq!(store.node_count().expect("count"), 1);
            // The subject here is that a *reopen* is at the same schema as a
            // fresh open — not what number that happens to be. See
            // `open_in_memory_applies_schema` on why the literal went.
            assert_eq!(
                store.schema_version().expect("version"),
                crate::migrations::latest_version(),
                "reopening applies any migration added since the file was written"
            );
            assert!(store.get_node("persisted").expect("get").is_some());
        }
        std::fs::remove_file(&path).expect("cleanup");
    }

    fn graphify_layer(dst: &str) -> FactSet {
        FactSet::new()
            .with_node(Node::new("graphify:doc1", NodeKind::Doc, "Doc 1"))
            .with_edge({
                let mut e = Edge::inferred("graphify:doc1", dst, EdgeKind::References, 0.9);
                e.src_ref = Some("import:graphify".to_owned());
                e
            })
    }

    /// A persisted import layer is re-applied after a `rebuild` wipes the graph,
    /// so imported facts survive a code-changing sync.
    #[test]
    fn imports_survive_rebuild() {
        let mut store = Store::open_in_memory().expect("open");
        let derived = FactSet::new().with_node(Node::new("file:a.rs", NodeKind::File, "a.rs"));
        store.rebuild(&derived, Some("tree1")).expect("rebuild");

        // apply_import_layer applies to the live graph and persists in one step.
        let applied = store
            .apply_import_layer("import:graphify", &graphify_layer("file:a.rs"))
            .expect("apply import");
        assert_eq!(applied.edges_applied, 1);
        assert_eq!(applied.edges_pruned, 0);
        assert_eq!(store.import_refs().expect("refs"), vec!["import:graphify"]);

        // Simulate a code-changing sync: the derived graph is rebuilt (still has
        // file:a.rs), which drops the imported doc + edge from the live graph.
        store.rebuild(&derived, Some("tree2")).expect("rebuild2");
        assert!(store.get_node("graphify:doc1").expect("get").is_none());

        // Re-applying imports restores them; nothing is pruned (target present).
        let applied = store.reapply_imports().expect("reapply");
        assert_eq!(applied.layers, 1);
        assert_eq!(applied.nodes, 1);
        assert_eq!(applied.edges_applied, 1);
        assert_eq!(applied.edges_pruned, 0);
        assert!(store.get_node("graphify:doc1").expect("get").is_some());
        assert_eq!(store.edges_from("graphify:doc1").expect("edges").len(), 1);
    }

    /// When a sync removes an edge's target (e.g. a deleted file), re-applying
    /// **prunes** that stale cross-reference from the persisted layer — it is not
    /// kept and retried forever. The import node itself is preserved.
    #[test]
    fn reapply_prunes_stale_cross_references() {
        let mut store = Store::open_in_memory().expect("open");
        let derived = FactSet::new().with_node(Node::new("file:gone.rs", NodeKind::File, "g"));
        store.rebuild(&derived, Some("t1")).expect("rebuild");
        store
            .apply_import_layer("import:graphify", &graphify_layer("file:gone.rs"))
            .expect("import");

        // Code-changing sync: file:gone.rs is deleted from the derived graph.
        store
            .rebuild(&FactSet::new(), Some("t2"))
            .expect("rebuild2");
        let applied = store.reapply_imports().expect("reapply");
        assert_eq!(applied.nodes, 1);
        assert_eq!(applied.edges_applied, 0);
        assert_eq!(
            applied.edges_pruned, 1,
            "the edge to the deleted file is pruned"
        );
        assert!(store.get_node("graphify:doc1").expect("get").is_some());

        // The prune is durable: a second reapply finds nothing left to prune,
        // proving the stale edge was removed from the persisted layer.
        let again = store.reapply_imports().expect("reapply2");
        assert_eq!(again.edges_applied, 0);
        assert_eq!(again.edges_pruned, 0, "already pruned; not retried");
    }

    /// `apply_import_layer` validates on import: a dangling edge in the incoming
    /// layer is dropped and never persisted.
    #[test]
    fn apply_import_layer_prunes_on_import() {
        let mut store = Store::open_in_memory().expect("open");
        let present = || FactSet::new().with_node(Node::new("file:a.rs", NodeKind::File, "a"));
        store.rebuild(&present(), Some("t")).expect("rebuild");

        let layer = graphify_layer("file:a.rs").with_edge({
            // Points at a file that does not exist → pruned on import.
            let mut e = Edge::inferred("graphify:doc1", "file:ghost.rs", EdgeKind::References, 0.9);
            e.src_ref = Some("import:graphify".to_owned());
            e
        });
        let applied = store
            .apply_import_layer("import:graphify", &layer)
            .expect("import");
        assert_eq!(applied.edges_applied, 1);
        assert_eq!(applied.edges_pruned, 1);

        // A rebuild + reapply confirms only the valid edge was persisted.
        store.rebuild(&present(), Some("t2")).expect("rebuild2");
        let re = store.reapply_imports().expect("reapply");
        assert_eq!(re.edges_applied, 1);
        assert_eq!(re.edges_pruned, 0, "ghost edge was not persisted");
    }

    #[test]
    fn legacy_import_layer_nodes_are_retagged_non_derived() {
        // A layer persisted before nodes carried provenance: its node objects have
        // no `provenance` field, so serde defaults them to Derived. On reapply the
        // store must repair them — a Graphify node to Inferred, a lat node to
        // Authored — so a later derived-only sync never mistakes them for derived.
        let mut store = Store::open_in_memory().expect("open");
        let legacy = r#"{"nodes":[
            {"key":"graphify:doc1","kind":"doc","name":"d","path":null,"lang":null,"blob_hash":null,"span":null,"meta":null},
            {"key":"lat:lat.md/a.md","kind":"doc","name":"a","path":null,"lang":null,"blob_hash":null,"span":null,"meta":null}
        ],"edges":[]}"#;
        store
            .conn
            .execute(
                "INSERT INTO imports (src_ref, facts) VALUES ('import:legacy', ?1)",
                [legacy],
            )
            .expect("seed legacy import row");

        store.reapply_imports().expect("reapply");

        let g = store
            .get_node("graphify:doc1")
            .expect("get")
            .expect("graphify node");
        assert_eq!(
            g.provenance,
            Provenance::Inferred,
            "graphify import node repaired to inferred"
        );
        let l = store
            .get_node("lat:lat.md/a.md")
            .expect("get")
            .expect("lat node");
        assert_eq!(
            l.provenance,
            Provenance::Authored,
            "lat import node repaired to authored"
        );
    }

    /// `apply_import_layer` replaces the layer for a ref; `delete_import` removes.
    #[test]
    fn apply_import_replaces_and_delete_removes() {
        let mut store = Store::open_in_memory().expect("open");
        let a = FactSet::new().with_node(Node::new("graphify:x", NodeKind::Doc, "x"));
        let b = FactSet::new().with_node(Node::new("graphify:y", NodeKind::Doc, "y"));
        store.apply_import_layer("import:graphify", &a).expect("a");
        store.apply_import_layer("import:graphify", &b).expect("b");
        assert_eq!(
            store.import_refs().expect("refs").len(),
            1,
            "same ref replaced"
        );
        assert!(store.delete_import("import:graphify").expect("del"));
        assert!(store.import_refs().expect("refs").is_empty());
        assert!(!store.delete_import("import:graphify").expect("del again"));
    }
}